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