hmap: Add HMAP_FOR_EACH_POP.
[cascardo/ovs.git] / ovn / controller / physical.c
1 /* Copyright (c) 2015, 2016 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17 #include "byte-order.h"
18 #include "flow.h"
19 #include "lflow.h"
20 #include "ofctrl.h"
21 #include "openvswitch/match.h"
22 #include "openvswitch/ofp-actions.h"
23 #include "openvswitch/ofpbuf.h"
24 #include "openvswitch/vlog.h"
25 #include "ovn-controller.h"
26 #include "ovn/lib/ovn-sb-idl.h"
27 #include "physical.h"
28 #include "shash.h"
29 #include "simap.h"
30 #include "smap.h"
31 #include "sset.h"
32 #include "vswitch-idl.h"
33
34 VLOG_DEFINE_THIS_MODULE(physical);
35
36 void
37 physical_register_ovs_idl(struct ovsdb_idl *ovs_idl)
38 {
39     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_bridge);
40     ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_ports);
41
42     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_port);
43     ovsdb_idl_add_column(ovs_idl, &ovsrec_port_col_name);
44     ovsdb_idl_add_column(ovs_idl, &ovsrec_port_col_interfaces);
45     ovsdb_idl_add_column(ovs_idl, &ovsrec_port_col_external_ids);
46
47     ovsdb_idl_add_table(ovs_idl, &ovsrec_table_interface);
48     ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_name);
49     ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_ofport);
50     ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_external_ids);
51 }
52
53 /* Maps from a chassis to the OpenFlow port number of the tunnel that can be
54  * used to reach that chassis. */
55 struct chassis_tunnel {
56     struct hmap_node hmap_node;
57     const char *chassis_id;
58     ofp_port_t ofport;
59     enum chassis_tunnel_type type;
60 };
61
62 static struct chassis_tunnel *
63 chassis_tunnel_find(struct hmap *tunnels, const char *chassis_id)
64 {
65     struct chassis_tunnel *tun;
66     HMAP_FOR_EACH_WITH_HASH (tun, hmap_node, hash_string(chassis_id, 0),
67                              tunnels) {
68         if (!strcmp(tun->chassis_id, chassis_id)) {
69             return tun;
70         }
71     }
72     return NULL;
73 }
74
75 static void
76 put_load(uint64_t value, enum mf_field_id dst, int ofs, int n_bits,
77          struct ofpbuf *ofpacts)
78 {
79     struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
80     sf->field = mf_from_id(dst);
81     sf->flow_has_vlan = false;
82
83     ovs_be64 n_value = htonll(value);
84     bitwise_copy(&n_value, 8, 0, &sf->value, sf->field->n_bytes, ofs, n_bits);
85     bitwise_one(&sf->mask, sf->field->n_bytes, ofs, n_bits);
86 }
87
88 static void
89 put_move(enum mf_field_id src, int src_ofs,
90          enum mf_field_id dst, int dst_ofs,
91          int n_bits,
92          struct ofpbuf *ofpacts)
93 {
94     struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
95     move->src.field = mf_from_id(src);
96     move->src.ofs = src_ofs;
97     move->src.n_bits = n_bits;
98     move->dst.field = mf_from_id(dst);
99     move->dst.ofs = dst_ofs;
100     move->dst.n_bits = n_bits;
101 }
102
103 static void
104 put_resubmit(uint8_t table_id, struct ofpbuf *ofpacts)
105 {
106     struct ofpact_resubmit *resubmit = ofpact_put_RESUBMIT(ofpacts);
107     resubmit->in_port = OFPP_IN_PORT;
108     resubmit->table_id = table_id;
109 }
110
111 static void
112 put_encapsulation(enum mf_field_id mff_ovn_geneve,
113                   const struct chassis_tunnel *tun,
114                   const struct sbrec_datapath_binding *datapath,
115                   uint16_t outport, struct ofpbuf *ofpacts)
116 {
117     if (tun->type == GENEVE) {
118         put_load(datapath->tunnel_key, MFF_TUN_ID, 0, 24, ofpacts);
119         put_load(outport, mff_ovn_geneve, 0, 32, ofpacts);
120         put_move(MFF_LOG_INPORT, 0, mff_ovn_geneve, 16, 15, ofpacts);
121     } else if (tun->type == STT) {
122         put_load(datapath->tunnel_key | (outport << 24), MFF_TUN_ID, 0, 64,
123                  ofpacts);
124         put_move(MFF_LOG_INPORT, 0, MFF_TUN_ID, 40, 15, ofpacts);
125     } else if (tun->type == VXLAN) {
126         put_load(datapath->tunnel_key, MFF_TUN_ID, 0, 24, ofpacts);
127     } else {
128         OVS_NOT_REACHED();
129     }
130 }
131
132 static void
133 put_stack(enum mf_field_id field, struct ofpact_stack *stack)
134 {
135     stack->subfield.field = mf_from_id(field);
136     stack->subfield.ofs = 0;
137     stack->subfield.n_bits = stack->subfield.field->n_bits;
138 }
139
140 static const struct sbrec_port_binding*
141 get_localnet_port(struct hmap *local_datapaths, int64_t tunnel_key)
142 {
143     struct local_datapath *ld = get_local_datapath(local_datapaths,
144                                                    tunnel_key);
145     return ld ? ld->localnet_port : NULL;
146 }
147
148 void
149 physical_run(struct controller_ctx *ctx, enum mf_field_id mff_ovn_geneve,
150              const struct ovsrec_bridge *br_int, const char *this_chassis_id,
151              const struct simap *ct_zones, struct hmap *flow_table,
152              struct hmap *local_datapaths, struct hmap *patched_datapaths)
153 {
154     struct simap localvif_to_ofport = SIMAP_INITIALIZER(&localvif_to_ofport);
155     struct hmap tunnels = HMAP_INITIALIZER(&tunnels);
156
157     for (int i = 0; i < br_int->n_ports; i++) {
158         const struct ovsrec_port *port_rec = br_int->ports[i];
159         if (!strcmp(port_rec->name, br_int->name)) {
160             continue;
161         }
162
163         const char *chassis_id = smap_get(&port_rec->external_ids,
164                                           "ovn-chassis-id");
165         if (chassis_id && !strcmp(chassis_id, this_chassis_id)) {
166             continue;
167         }
168
169         const char *localnet = smap_get(&port_rec->external_ids,
170                                         "ovn-localnet-port");
171         const char *logpatch = smap_get(&port_rec->external_ids,
172                                         "ovn-logical-patch-port");
173
174         for (int j = 0; j < port_rec->n_interfaces; j++) {
175             const struct ovsrec_interface *iface_rec = port_rec->interfaces[j];
176
177             /* Get OpenFlow port number. */
178             if (!iface_rec->n_ofport) {
179                 continue;
180             }
181             int64_t ofport = iface_rec->ofport[0];
182             if (ofport < 1 || ofport > ofp_to_u16(OFPP_MAX)) {
183                 continue;
184             }
185
186             /* Record as patch to local net, logical patch port, chassis, or
187              * local logical port. */
188             bool is_patch = !strcmp(iface_rec->type, "patch");
189             if (is_patch && localnet) {
190                 /* localnet patch ports can be handled just like VIFs. */
191                 simap_put(&localvif_to_ofport, localnet, ofport);
192                 break;
193             } else if (is_patch && logpatch) {
194                 /* Logical patch ports can be handled just like VIFs. */
195                 simap_put(&localvif_to_ofport, logpatch, ofport);
196                 break;
197             } else if (chassis_id) {
198                 enum chassis_tunnel_type tunnel_type;
199                 if (!strcmp(iface_rec->type, "geneve")) {
200                     tunnel_type = GENEVE;
201                     if (!mff_ovn_geneve) {
202                         continue;
203                     }
204                 } else if (!strcmp(iface_rec->type, "stt")) {
205                     tunnel_type = STT;
206                 } else if (!strcmp(iface_rec->type, "vxlan")) {
207                     tunnel_type = VXLAN;
208                 } else {
209                     continue;
210                 }
211
212                 struct chassis_tunnel *tun = xmalloc(sizeof *tun);
213                 hmap_insert(&tunnels, &tun->hmap_node,
214                             hash_string(chassis_id, 0));
215                 tun->chassis_id = chassis_id;
216                 tun->ofport = u16_to_ofp(ofport);
217                 tun->type = tunnel_type;
218                 break;
219             } else {
220                 const char *iface_id = smap_get(&iface_rec->external_ids,
221                                                 "iface-id");
222                 if (iface_id) {
223                     simap_put(&localvif_to_ofport, iface_id, ofport);
224                 }
225             }
226         }
227     }
228
229     struct ofpbuf ofpacts;
230     ofpbuf_init(&ofpacts, 0);
231
232     /* Set up flows in table 0 for physical-to-logical translation and in table
233      * 64 for logical-to-physical translation. */
234     const struct sbrec_port_binding *binding;
235     SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
236         /* Skip the port binding if the port is on a datapath that is neither
237          * local nor with any logical patch port connected, because local ports
238          * would never need to talk to those ports.
239          *
240          * Even with this approach there could still be unnecessary port
241          * bindings processed. A better approach would be a kind of "flood
242          * fill" algorithm:
243          *
244          *   1. Initialize set S to the logical datapaths that have a port
245          *      located on the hypervisor.
246          *
247          *   2. For each patch port P in a logical datapath in S, add the
248          *      logical datapath of the remote end of P to S.  Iterate
249          *      until S reaches a fixed point.
250          *
251          * This can be implemented in northd, which can generate the sets and
252          * save it on each port-binding record in SB, and ovn-controller can
253          * use the information directly. However, there can be update storms
254          * when a pair of patch ports are added/removed to connect/disconnect
255          * large lrouters and lswitches. This need to be studied further.
256          */
257         uint32_t dp_key = binding->datapath->tunnel_key;
258         uint32_t port_key = binding->tunnel_key;
259         if (!get_local_datapath(local_datapaths, dp_key)
260             && !get_patched_datapath(patched_datapaths, dp_key)) {
261             continue;
262         }
263
264         /* Find the OpenFlow port for the logical port, as 'ofport'.  This is
265          * one of:
266          *
267          *     - If the port is a VIF on the chassis we're managing, the
268          *       OpenFlow port for the VIF.  'tun' will be NULL.
269          *
270          *       The same logic handles logical patch ports, as well as
271          *       localnet patch ports.
272          *
273          *       For a container nested inside a VM and accessible via a VLAN,
274          *       'tag' is the VLAN ID; otherwise 'tag' is 0.
275          *
276          *       For a localnet patch port, if a VLAN ID was configured, 'tag'
277          *       is set to that VLAN ID; otherwise 'tag' is 0.
278          *
279          *     - If the port is on a remote chassis, the OpenFlow port for a
280          *       tunnel to the VIF's remote chassis.  'tun' identifies that
281          *       tunnel.
282          */
283
284         int tag = 0;
285         ofp_port_t ofport;
286         bool is_remote = false;
287         if (binding->parent_port && *binding->parent_port) {
288             if (!binding->tag) {
289                 continue;
290             }
291             ofport = u16_to_ofp(simap_get(&localvif_to_ofport,
292                                           binding->parent_port));
293             if (ofport) {
294                 tag = *binding->tag;
295             }
296         } else {
297             ofport = u16_to_ofp(simap_get(&localvif_to_ofport,
298                                           binding->logical_port));
299             if (!strcmp(binding->type, "localnet") && ofport && binding->tag) {
300                 tag = *binding->tag;
301             }
302         }
303
304         const struct chassis_tunnel *tun = NULL;
305         const struct sbrec_port_binding *localnet_port =
306             get_localnet_port(local_datapaths, dp_key);
307         if (!ofport) {
308             /* It is remote port, may be reached by tunnel or localnet port */
309             is_remote = true;
310             if (!binding->chassis) {
311                 continue;
312             }
313             if (localnet_port) {
314                 ofport = u16_to_ofp(simap_get(&localvif_to_ofport,
315                                               localnet_port->logical_port));
316                 if (!ofport) {
317                     continue;
318                 }
319             } else {
320                 tun = chassis_tunnel_find(&tunnels, binding->chassis->name);
321                 if (!tun) {
322                     continue;
323                 }
324                 ofport = tun->ofport;
325             }
326         }
327
328         struct match match;
329         if (!is_remote) {
330             int zone_id = simap_get(ct_zones, binding->logical_port);
331             /* Packets that arrive from a vif can belong to a VM or
332              * to a container located inside that VM. Packets that
333              * arrive from containers have a tag (vlan) associated with them.
334              */
335
336             /* Table 0, Priority 150 and 100.
337              * ==============================
338              *
339              * Priority 150 is for tagged traffic. This may be containers in a
340              * VM or a VLAN on a local network. For such traffic, match on the
341              * tags and then strip the tag.
342              *
343              * Priority 100 is for traffic belonging to VMs or untagged locally
344              * connected networks.
345              *
346              * For both types of traffic: set MFF_LOG_INPORT to the logical
347              * input port, MFF_LOG_DATAPATH to the logical datapath, and
348              * resubmit into the logical ingress pipeline starting at table
349              * 16. */
350             ofpbuf_clear(&ofpacts);
351             match_init_catchall(&match);
352             match_set_in_port(&match, ofport);
353
354             /* Match a VLAN tag and strip it, including stripping priority tags
355              * (e.g. VLAN ID 0).  In the latter case we'll add a second flow
356              * for frames that lack any 802.1Q header later. */
357             if (tag || !strcmp(binding->type, "localnet")) {
358                 match_set_dl_vlan(&match, htons(tag));
359                 ofpact_put_STRIP_VLAN(&ofpacts);
360             }
361
362             /* Remember the size with just strip vlan added so far,
363              * as we're going to remove this with ofpbuf_pull() later. */
364             uint32_t ofpacts_orig_size = ofpacts.size;
365
366             if (zone_id) {
367                 put_load(zone_id, MFF_LOG_CT_ZONE, 0, 32, &ofpacts);
368             }
369
370             /* Set MFF_LOG_DATAPATH and MFF_LOG_INPORT. */
371             put_load(dp_key, MFF_LOG_DATAPATH, 0, 64, &ofpacts);
372             put_load(port_key, MFF_LOG_INPORT, 0, 32, &ofpacts);
373
374             /* Resubmit to first logical ingress pipeline table. */
375             put_resubmit(OFTABLE_LOG_INGRESS_PIPELINE, &ofpacts);
376             ofctrl_add_flow(flow_table, OFTABLE_PHY_TO_LOG,
377                             tag ? 150 : 100, &match, &ofpacts);
378
379             if (!tag && !strcmp(binding->type, "localnet")) {
380                 /* Add a second flow for frames that lack any 802.1Q
381                  * header.  For these, drop the OFPACT_STRIP_VLAN
382                  * action. */
383                 ofpbuf_pull(&ofpacts, ofpacts_orig_size);
384                 match_set_dl_tci_masked(&match, 0, htons(VLAN_CFI));
385                 ofctrl_add_flow(flow_table, 0, 100, &match, &ofpacts);
386             }
387
388             /* Table 33, priority 100.
389              * =======================
390              *
391              * Implements output to local hypervisor.  Each flow matches a
392              * logical output port on the local hypervisor, and resubmits to
393              * table 34.
394              */
395
396             match_init_catchall(&match);
397             ofpbuf_clear(&ofpacts);
398
399             /* Match MFF_LOG_DATAPATH, MFF_LOG_OUTPORT. */
400             match_set_metadata(&match, htonll(dp_key));
401             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, port_key);
402
403             if (zone_id) {
404                 put_load(zone_id, MFF_LOG_CT_ZONE, 0, 32, &ofpacts);
405             }
406
407             /* Resubmit to table 34. */
408             put_resubmit(OFTABLE_DROP_LOOPBACK, &ofpacts);
409             ofctrl_add_flow(flow_table, OFTABLE_LOCAL_OUTPUT, 100, &match,
410                             &ofpacts);
411
412             /* Table 34, Priority 100.
413              * =======================
414              *
415              * Drop packets whose logical inport and outport are the same. */
416             match_init_catchall(&match);
417             ofpbuf_clear(&ofpacts);
418             match_set_metadata(&match, htonll(dp_key));
419             match_set_reg(&match, MFF_LOG_INPORT - MFF_REG0, port_key);
420             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, port_key);
421             ofctrl_add_flow(flow_table, OFTABLE_DROP_LOOPBACK, 100,
422                             &match, &ofpacts);
423
424             /* Table 64, Priority 100.
425              * =======================
426              *
427              * Deliver the packet to the local vif. */
428             match_init_catchall(&match);
429             ofpbuf_clear(&ofpacts);
430             match_set_metadata(&match, htonll(dp_key));
431             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, port_key);
432             if (tag) {
433                 /* For containers sitting behind a local vif, tag the packets
434                  * before delivering them. */
435                 struct ofpact_vlan_vid *vlan_vid;
436                 vlan_vid = ofpact_put_SET_VLAN_VID(&ofpacts);
437                 vlan_vid->vlan_vid = tag;
438                 vlan_vid->push_vlan_if_needed = true;
439
440                 /* A packet might need to hair-pin back into its ingress
441                  * OpenFlow port (to a different logical port, which we already
442                  * checked back in table 34), so set the in_port to zero. */
443                 put_stack(MFF_IN_PORT, ofpact_put_STACK_PUSH(&ofpacts));
444                 put_load(0, MFF_IN_PORT, 0, 16, &ofpacts);
445             }
446             ofpact_put_OUTPUT(&ofpacts)->port = ofport;
447             if (tag) {
448                 /* Revert the tag added to the packets headed to containers
449                  * in the previous step. If we don't do this, the packets
450                  * that are to be broadcasted to a VM in the same logical
451                  * switch will also contain the tag. Also revert the zero'd
452                  * in_port. */
453                 ofpact_put_STRIP_VLAN(&ofpacts);
454                 put_stack(MFF_IN_PORT, ofpact_put_STACK_POP(&ofpacts));
455             }
456             ofctrl_add_flow(flow_table, OFTABLE_LOG_TO_PHY, 100,
457                             &match, &ofpacts);
458         } else if (!tun) {
459             /* Remote port connected by localnet port */
460             /* Table 33, priority 100.
461              * =======================
462              *
463              * Implements switching to localnet port. Each flow matches a
464              * logical output port on remote hypervisor, switch the output port
465              * to connected localnet port and resubmits to same table.
466              */
467
468             match_init_catchall(&match);
469             ofpbuf_clear(&ofpacts);
470
471             /* Match MFF_LOG_DATAPATH, MFF_LOG_OUTPORT. */
472             match_set_metadata(&match, htonll(dp_key));
473             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, port_key);
474
475             put_load(localnet_port->tunnel_key, MFF_LOG_OUTPORT, 0, 32, &ofpacts);
476
477             /* Resubmit to table 33. */
478             put_resubmit(OFTABLE_LOCAL_OUTPUT, &ofpacts);
479             ofctrl_add_flow(flow_table, OFTABLE_LOCAL_OUTPUT, 100, &match,
480                             &ofpacts);
481         } else {
482             /* Remote port connected by tunnel */
483             /* Table 32, priority 100.
484              * =======================
485              *
486              * Implements output to remote hypervisors.  Each flow matches an
487              * output port that includes a logical port on a remote hypervisor,
488              * and tunnels the packet to that hypervisor.
489              */
490
491             match_init_catchall(&match);
492             ofpbuf_clear(&ofpacts);
493
494             /* Match MFF_LOG_DATAPATH, MFF_LOG_OUTPORT. */
495             match_set_metadata(&match, htonll(dp_key));
496             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, port_key);
497
498             put_encapsulation(mff_ovn_geneve, tun, binding->datapath,
499                               port_key, &ofpacts);
500
501             /* Output to tunnel. */
502             ofpact_put_OUTPUT(&ofpacts)->port = ofport;
503             ofctrl_add_flow(flow_table, OFTABLE_REMOTE_OUTPUT, 100,
504                             &match, &ofpacts);
505         }
506     }
507
508     /* Handle output to multicast groups, in tables 32 and 33. */
509     const struct sbrec_multicast_group *mc;
510     struct ofpbuf remote_ofpacts;
511     ofpbuf_init(&remote_ofpacts, 0);
512     SBREC_MULTICAST_GROUP_FOR_EACH (mc, ctx->ovnsb_idl) {
513         struct sset remote_chassis = SSET_INITIALIZER(&remote_chassis);
514         struct match match;
515
516         match_init_catchall(&match);
517         match_set_metadata(&match, htonll(mc->datapath->tunnel_key));
518         match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, mc->tunnel_key);
519
520         /* Go through all of the ports in the multicast group:
521          *
522          *    - For remote ports, add the chassis to 'remote_chassis'.
523          *
524          *    - For local ports (other than logical patch ports), add actions
525          *      to 'ofpacts' to set the output port and resubmit.
526          *
527          *    - For logical patch ports, add actions to 'remote_ofpacts'
528          *      instead.  (If we put them in 'ofpacts', then the output
529          *      would happen on every hypervisor in the multicast group,
530          *      effectively duplicating the packet.)
531          */
532         ofpbuf_clear(&ofpacts);
533         ofpbuf_clear(&remote_ofpacts);
534         for (size_t i = 0; i < mc->n_ports; i++) {
535             struct sbrec_port_binding *port = mc->ports[i];
536
537             if (port->datapath != mc->datapath) {
538                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
539                 VLOG_WARN_RL(&rl, UUID_FMT": multicast group contains ports "
540                              "in wrong datapath",
541                              UUID_ARGS(&mc->header_.uuid));
542                 continue;
543             }
544
545             int zone_id = simap_get(ct_zones, port->logical_port);
546             if (zone_id) {
547                 put_load(zone_id, MFF_LOG_CT_ZONE, 0, 32, &ofpacts);
548             }
549
550             if (!strcmp(port->type, "patch")) {
551                 put_load(port->tunnel_key, MFF_LOG_OUTPORT, 0, 32,
552                          &remote_ofpacts);
553                 put_resubmit(OFTABLE_DROP_LOOPBACK, &remote_ofpacts);
554             } else if (simap_contains(&localvif_to_ofport,
555                                (port->parent_port && *port->parent_port)
556                                ? port->parent_port : port->logical_port)) {
557                 put_load(port->tunnel_key, MFF_LOG_OUTPORT, 0, 32, &ofpacts);
558                 put_resubmit(OFTABLE_DROP_LOOPBACK, &ofpacts);
559             } else if (port->chassis && !get_localnet_port(local_datapaths,
560                                              mc->datapath->tunnel_key)) {
561                 /* Add remote chassis only when localnet port not exist,
562                  * otherwise multicast will reach remote ports through localnet
563                  * port. */
564                 sset_add(&remote_chassis, port->chassis->name);
565             }
566         }
567
568         /* Table 33, priority 100.
569          * =======================
570          *
571          * Handle output to the local logical ports in the multicast group, if
572          * any. */
573         bool local_ports = ofpacts.size > 0;
574         if (local_ports) {
575             /* Following delivery to local logical ports, restore the multicast
576              * group as the logical output port. */
577             put_load(mc->tunnel_key, MFF_LOG_OUTPORT, 0, 32, &ofpacts);
578
579             ofctrl_add_flow(flow_table, OFTABLE_LOCAL_OUTPUT, 100,
580                             &match, &ofpacts);
581         }
582
583         /* Table 32, priority 100.
584          * =======================
585          *
586          * Handle output to the remote chassis in the multicast group, if
587          * any. */
588         if (!sset_is_empty(&remote_chassis) || remote_ofpacts.size > 0) {
589             if (remote_ofpacts.size > 0) {
590                 /* Following delivery to logical patch ports, restore the
591                  * multicast group as the logical output port. */
592                 put_load(mc->tunnel_key, MFF_LOG_OUTPORT, 0, 32,
593                          &remote_ofpacts);
594             }
595
596             const char *chassis;
597             const struct chassis_tunnel *prev = NULL;
598             SSET_FOR_EACH (chassis, &remote_chassis) {
599                 const struct chassis_tunnel *tun
600                     = chassis_tunnel_find(&tunnels, chassis);
601                 if (!tun) {
602                     continue;
603                 }
604
605                 if (!prev || tun->type != prev->type) {
606                     put_encapsulation(mff_ovn_geneve, tun, mc->datapath,
607                                       mc->tunnel_key, &remote_ofpacts);
608                     prev = tun;
609                 }
610                 ofpact_put_OUTPUT(&remote_ofpacts)->port = tun->ofport;
611             }
612
613             if (remote_ofpacts.size) {
614                 if (local_ports) {
615                     put_resubmit(OFTABLE_LOCAL_OUTPUT, &remote_ofpacts);
616                 }
617                 ofctrl_add_flow(flow_table, OFTABLE_REMOTE_OUTPUT, 100,
618                                 &match, &remote_ofpacts);
619             }
620         }
621         sset_destroy(&remote_chassis);
622     }
623     ofpbuf_uninit(&remote_ofpacts);
624
625     /* Table 0, priority 100.
626      * ======================
627      *
628      * Process packets that arrive from a remote hypervisor (by matching
629      * on tunnel in_port). */
630
631     /* Add flows for Geneve and STT encapsulations.  These
632      * encapsulations have metadata about the ingress and egress logical
633      * ports.  We set MFF_LOG_DATAPATH, MFF_LOG_INPORT, and
634      * MFF_LOG_OUTPORT from the tunnel key data, then resubmit to table
635      * 33 to handle packets to the local hypervisor. */
636     struct chassis_tunnel *tun;
637     HMAP_FOR_EACH (tun, hmap_node, &tunnels) {
638         struct match match = MATCH_CATCHALL_INITIALIZER;
639         match_set_in_port(&match, tun->ofport);
640
641         ofpbuf_clear(&ofpacts);
642         if (tun->type == GENEVE) {
643             put_move(MFF_TUN_ID, 0,  MFF_LOG_DATAPATH, 0, 24, &ofpacts);
644             put_move(mff_ovn_geneve, 16, MFF_LOG_INPORT, 0, 15,
645                      &ofpacts);
646             put_move(mff_ovn_geneve, 0, MFF_LOG_OUTPORT, 0, 16,
647                      &ofpacts);
648         } else if (tun->type == STT) {
649             put_move(MFF_TUN_ID, 40, MFF_LOG_INPORT,   0, 15, &ofpacts);
650             put_move(MFF_TUN_ID, 24, MFF_LOG_OUTPORT,  0, 16, &ofpacts);
651             put_move(MFF_TUN_ID,  0, MFF_LOG_DATAPATH, 0, 24, &ofpacts);
652         } else if (tun->type == VXLAN) {
653             /* We'll handle VXLAN later. */
654             continue;
655         } else {
656             OVS_NOT_REACHED();
657         }
658
659         put_resubmit(OFTABLE_LOCAL_OUTPUT, &ofpacts);
660
661         ofctrl_add_flow(flow_table, OFTABLE_PHY_TO_LOG, 100, &match, &ofpacts);
662     }
663
664     /* Add flows for VXLAN encapsulations.  Due to the limited amount of
665      * metadata, we only support VXLAN for connections to gateways.  The
666      * VNI is used to populate MFF_LOG_DATAPATH.  The gateway's logical
667      * port is set to MFF_LOG_INPORT.  Then the packet is resubmitted to
668      * table 16 to determine the logical egress port.
669      *
670      * xxx Due to resubmitting to table 16, broadcasts will be re-sent to
671      * xxx all logical ports, including non-local ones which could cause
672      * xxx duplicate packets to be received by multiply-connected gateways. */
673     HMAP_FOR_EACH (tun, hmap_node, &tunnels) {
674         if (tun->type != VXLAN) {
675             continue;
676         }
677
678         SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
679             struct match match = MATCH_CATCHALL_INITIALIZER;
680
681             if (!binding->chassis ||
682                 strcmp(tun->chassis_id, binding->chassis->name)) {
683                 continue;
684             }
685
686             match_set_in_port(&match, tun->ofport);
687             match_set_tun_id(&match, htonll(binding->datapath->tunnel_key));
688
689             ofpbuf_clear(&ofpacts);
690             put_move(MFF_TUN_ID, 0,  MFF_LOG_DATAPATH, 0, 24, &ofpacts);
691             put_load(binding->tunnel_key, MFF_LOG_INPORT, 0, 15, &ofpacts);
692             put_resubmit(OFTABLE_LOG_INGRESS_PIPELINE, &ofpacts);
693
694             ofctrl_add_flow(flow_table, OFTABLE_PHY_TO_LOG, 100, &match,
695                     &ofpacts);
696         }
697     }
698
699     /* Table 32, Priority 0.
700      * =======================
701      *
702      * Resubmit packets that are not directed at tunnels or part of a
703      * multicast group to the local output table. */
704     struct match match;
705     match_init_catchall(&match);
706     ofpbuf_clear(&ofpacts);
707     put_resubmit(OFTABLE_LOCAL_OUTPUT, &ofpacts);
708     ofctrl_add_flow(flow_table, OFTABLE_REMOTE_OUTPUT, 0, &match, &ofpacts);
709
710     /* Table 34, Priority 0.
711      * =======================
712      *
713      * Resubmit packets that don't output to the ingress port (already checked
714      * in table 33) to the logical egress pipeline, clearing the logical
715      * registers (for consistent behavior with packets that get tunneled). */
716     match_init_catchall(&match);
717     ofpbuf_clear(&ofpacts);
718 #define MFF_LOG_REG(ID) put_load(0, ID, 0, 32, &ofpacts);
719     MFF_LOG_REGS;
720 #undef MFF_LOG_REGS
721     put_resubmit(OFTABLE_LOG_EGRESS_PIPELINE, &ofpacts);
722     ofctrl_add_flow(flow_table, OFTABLE_DROP_LOOPBACK, 0, &match, &ofpacts);
723
724     ofpbuf_uninit(&ofpacts);
725     simap_destroy(&localvif_to_ofport);
726     HMAP_FOR_EACH_POP (tun, hmap_node, &tunnels) {
727         free(tun);
728     }
729     hmap_destroy(&tunnels);
730 }