ovn: Connect to remote lports through localnet port.
[cascardo/ovs.git] / ovn / controller / physical.c
1 /* Copyright (c) 2015 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 "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;
142     ld = CONTAINER_OF(hmap_first_with_hash(local_datapaths, tunnel_key),
143                       struct local_datapath, hmap_node);
144     return ld ? ld->localnet_port : NULL;
145 }
146
147 void
148 physical_run(struct controller_ctx *ctx, enum mf_field_id mff_ovn_geneve,
149              const struct ovsrec_bridge *br_int, const char *this_chassis_id,
150              const struct simap *ct_zones, struct hmap *flow_table,
151              struct hmap *local_datapaths)
152 {
153     struct simap localvif_to_ofport = SIMAP_INITIALIZER(&localvif_to_ofport);
154     struct hmap tunnels = HMAP_INITIALIZER(&tunnels);
155
156     for (int i = 0; i < br_int->n_ports; i++) {
157         const struct ovsrec_port *port_rec = br_int->ports[i];
158         if (!strcmp(port_rec->name, br_int->name)) {
159             continue;
160         }
161
162         const char *chassis_id = smap_get(&port_rec->external_ids,
163                                           "ovn-chassis-id");
164         if (chassis_id && !strcmp(chassis_id, this_chassis_id)) {
165             continue;
166         }
167
168         const char *localnet = smap_get(&port_rec->external_ids,
169                                         "ovn-localnet-port");
170         const char *logpatch = smap_get(&port_rec->external_ids,
171                                         "ovn-logical-patch-port");
172
173         for (int j = 0; j < port_rec->n_interfaces; j++) {
174             const struct ovsrec_interface *iface_rec = port_rec->interfaces[j];
175
176             /* Get OpenFlow port number. */
177             if (!iface_rec->n_ofport) {
178                 continue;
179             }
180             int64_t ofport = iface_rec->ofport[0];
181             if (ofport < 1 || ofport > ofp_to_u16(OFPP_MAX)) {
182                 continue;
183             }
184
185             /* Record as patch to local net, logical patch port, chassis, or
186              * local logical port. */
187             bool is_patch = !strcmp(iface_rec->type, "patch");
188             if (is_patch && localnet) {
189                 /* localnet patch ports can be handled just like VIFs. */
190                 simap_put(&localvif_to_ofport, localnet, ofport);
191                 break;
192             } else if (is_patch && logpatch) {
193                 /* Logical patch ports can be handled just like VIFs. */
194                 simap_put(&localvif_to_ofport, logpatch, ofport);
195                 break;
196             } else if (chassis_id) {
197                 enum chassis_tunnel_type tunnel_type;
198                 if (!strcmp(iface_rec->type, "geneve")) {
199                     tunnel_type = GENEVE;
200                     if (!mff_ovn_geneve) {
201                         continue;
202                     }
203                 } else if (!strcmp(iface_rec->type, "stt")) {
204                     tunnel_type = STT;
205                 } else if (!strcmp(iface_rec->type, "vxlan")) {
206                     tunnel_type = VXLAN;
207                 } else {
208                     continue;
209                 }
210
211                 struct chassis_tunnel *tun = xmalloc(sizeof *tun);
212                 hmap_insert(&tunnels, &tun->hmap_node,
213                             hash_string(chassis_id, 0));
214                 tun->chassis_id = chassis_id;
215                 tun->ofport = u16_to_ofp(ofport);
216                 tun->type = tunnel_type;
217                 break;
218             } else {
219                 const char *iface_id = smap_get(&iface_rec->external_ids,
220                                                 "iface-id");
221                 if (iface_id) {
222                     simap_put(&localvif_to_ofport, iface_id, ofport);
223                 }
224             }
225         }
226     }
227
228     struct ofpbuf ofpacts;
229     ofpbuf_init(&ofpacts, 0);
230
231     /* Set up flows in table 0 for physical-to-logical translation and in table
232      * 64 for logical-to-physical translation. */
233     const struct sbrec_port_binding *binding;
234     SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
235         /* Find the OpenFlow port for the logical port, as 'ofport'.  This is
236          * one of:
237          *
238          *     - If the port is a VIF on the chassis we're managing, the
239          *       OpenFlow port for the VIF.  'tun' will be NULL.
240          *
241          *       The same logic handles logical patch ports, as well as
242          *       localnet patch ports.
243          *
244          *       For a container nested inside a VM and accessible via a VLAN,
245          *       'tag' is the VLAN ID; otherwise 'tag' is 0.
246          *
247          *       For a localnet patch port, if a VLAN ID was configured, 'tag'
248          *       is set to that VLAN ID; otherwise 'tag' is 0.
249          *
250          *     - If the port is on a remote chassis, the OpenFlow port for a
251          *       tunnel to the VIF's remote chassis.  'tun' identifies that
252          *       tunnel.
253          */
254
255         int tag = 0;
256         ofp_port_t ofport;
257         bool is_remote = false;
258         if (binding->parent_port && *binding->parent_port) {
259             if (!binding->tag) {
260                 continue;
261             }
262             ofport = u16_to_ofp(simap_get(&localvif_to_ofport,
263                                           binding->parent_port));
264             if (ofport) {
265                 tag = *binding->tag;
266             }
267         } else {
268             ofport = u16_to_ofp(simap_get(&localvif_to_ofport,
269                                           binding->logical_port));
270             if (!strcmp(binding->type, "localnet") && ofport && binding->tag) {
271                 tag = *binding->tag;
272             }
273         }
274
275         const struct chassis_tunnel *tun = NULL;
276         const struct sbrec_port_binding *localnet_port =
277             get_localnet_port(local_datapaths,
278                               binding->datapath->tunnel_key);
279         if (!ofport) {
280             /* It is remote port, may be reached by tunnel or localnet port */
281             is_remote = true;
282             if (!binding->chassis) {
283                 continue;
284             }
285             if (localnet_port) {
286                 ofport = u16_to_ofp(simap_get(&localvif_to_ofport,
287                                               localnet_port->logical_port));
288                 if (!ofport) {
289                     continue;
290                 }
291             } else {
292                 tun = chassis_tunnel_find(&tunnels, binding->chassis->name);
293                 if (!tun) {
294                     continue;
295                 }
296                 ofport = tun->ofport;
297             }
298         }
299
300         struct match match;
301         if (!is_remote) {
302             int zone_id = simap_get(ct_zones, binding->logical_port);
303             /* Packets that arrive from a vif can belong to a VM or
304              * to a container located inside that VM. Packets that
305              * arrive from containers have a tag (vlan) associated with them.
306              */
307
308             /* Table 0, Priority 150 and 100.
309              * ==============================
310              *
311              * Priority 150 is for tagged traffic. This may be containers in a
312              * VM or a VLAN on a local network. For such traffic, match on the
313              * tags and then strip the tag.
314              *
315              * Priority 100 is for traffic belonging to VMs or untagged locally
316              * connected networks.
317              *
318              * For both types of traffic: set MFF_LOG_INPORT to the logical
319              * input port, MFF_LOG_DATAPATH to the logical datapath, and
320              * resubmit into the logical ingress pipeline starting at table
321              * 16. */
322             ofpbuf_clear(&ofpacts);
323             match_init_catchall(&match);
324             match_set_in_port(&match, ofport);
325
326             /* Match a VLAN tag and strip it, including stripping priority tags
327              * (e.g. VLAN ID 0).  In the latter case we'll add a second flow
328              * for frames that lack any 802.1Q header later. */
329             if (tag || !strcmp(binding->type, "localnet")) {
330                 match_set_dl_vlan(&match, htons(tag));
331                 ofpact_put_STRIP_VLAN(&ofpacts);
332             }
333
334             /* Remember the size with just strip vlan added so far,
335              * as we're going to remove this with ofpbuf_pull() later. */
336             uint32_t ofpacts_orig_size = ofpacts.size;
337
338             if (zone_id) {
339                 put_load(zone_id, MFF_LOG_CT_ZONE, 0, 32, &ofpacts);
340             }
341
342             /* Set MFF_LOG_DATAPATH and MFF_LOG_INPORT. */
343             put_load(binding->datapath->tunnel_key, MFF_LOG_DATAPATH, 0, 64,
344                      &ofpacts);
345             put_load(binding->tunnel_key, MFF_LOG_INPORT, 0, 32,
346                      &ofpacts);
347
348             /* Resubmit to first logical ingress pipeline table. */
349             put_resubmit(OFTABLE_LOG_INGRESS_PIPELINE, &ofpacts);
350             ofctrl_add_flow(flow_table, OFTABLE_PHY_TO_LOG,
351                             tag ? 150 : 100, &match, &ofpacts);
352
353             if (!tag && !strcmp(binding->type, "localnet")) {
354                 /* Add a second flow for frames that lack any 802.1Q
355                  * header.  For these, drop the OFPACT_STRIP_VLAN
356                  * action. */
357                 ofpbuf_pull(&ofpacts, ofpacts_orig_size);
358                 match_set_dl_tci_masked(&match, 0, htons(VLAN_CFI));
359                 ofctrl_add_flow(flow_table, 0, 100, &match, &ofpacts);
360             }
361
362             /* Table 33, priority 100.
363              * =======================
364              *
365              * Implements output to local hypervisor.  Each flow matches a
366              * logical output port on the local hypervisor, and resubmits to
367              * table 34.
368              */
369
370             match_init_catchall(&match);
371             ofpbuf_clear(&ofpacts);
372
373             /* Match MFF_LOG_DATAPATH, MFF_LOG_OUTPORT. */
374             match_set_metadata(&match, htonll(binding->datapath->tunnel_key));
375             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0,
376                           binding->tunnel_key);
377
378             if (zone_id) {
379                 put_load(zone_id, MFF_LOG_CT_ZONE, 0, 32, &ofpacts);
380             }
381
382             /* Resubmit to table 34. */
383             put_resubmit(OFTABLE_DROP_LOOPBACK, &ofpacts);
384             ofctrl_add_flow(flow_table, OFTABLE_LOCAL_OUTPUT, 100, &match,
385                             &ofpacts);
386
387             /* Table 64, Priority 100.
388              * =======================
389              *
390              * Deliver the packet to the local vif. */
391             match_init_catchall(&match);
392             ofpbuf_clear(&ofpacts);
393             match_set_metadata(&match, htonll(binding->datapath->tunnel_key));
394             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0,
395                           binding->tunnel_key);
396             if (tag) {
397                 /* For containers sitting behind a local vif, tag the packets
398                  * before delivering them. */
399                 struct ofpact_vlan_vid *vlan_vid;
400                 vlan_vid = ofpact_put_SET_VLAN_VID(&ofpacts);
401                 vlan_vid->vlan_vid = tag;
402                 vlan_vid->push_vlan_if_needed = true;
403
404                 /* A packet might need to hair-pin back into its ingress
405                  * OpenFlow port (to a different logical port, which we already
406                  * checked back in table 34), so set the in_port to zero. */
407                 put_stack(MFF_IN_PORT, ofpact_put_STACK_PUSH(&ofpacts));
408                 put_load(0, MFF_IN_PORT, 0, 16, &ofpacts);
409             }
410             ofpact_put_OUTPUT(&ofpacts)->port = ofport;
411             if (tag) {
412                 /* Revert the tag added to the packets headed to containers
413                  * in the previous step. If we don't do this, the packets
414                  * that are to be broadcasted to a VM in the same logical
415                  * switch will also contain the tag. Also revert the zero'd
416                  * in_port. */
417                 ofpact_put_STRIP_VLAN(&ofpacts);
418                 put_stack(MFF_IN_PORT, ofpact_put_STACK_POP(&ofpacts));
419             }
420             ofctrl_add_flow(flow_table, OFTABLE_LOG_TO_PHY, 100,
421                             &match, &ofpacts);
422         } else if (!tun) {
423             /* Remote port connected by localnet port */
424             /* Table 33, priority 100.
425              * =======================
426              *
427              * Implements switching to localnet port. Each flow matches a
428              * logical output port on remote hypervisor, switch the output port
429              * to connected localnet port and resubmits to same table.
430              */
431
432             match_init_catchall(&match);
433             ofpbuf_clear(&ofpacts);
434
435             /* Match MFF_LOG_DATAPATH, MFF_LOG_OUTPORT. */
436             match_set_metadata(&match, htonll(binding->datapath->tunnel_key));
437             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0,
438                           binding->tunnel_key);
439
440             put_load(localnet_port->tunnel_key, MFF_LOG_OUTPORT, 0, 32, &ofpacts);
441
442             /* Resubmit to table 33. */
443             put_resubmit(OFTABLE_LOCAL_OUTPUT, &ofpacts);
444             ofctrl_add_flow(flow_table, OFTABLE_LOCAL_OUTPUT, 100, &match,
445                             &ofpacts);
446         } else {
447             /* Remote port connected by tunnel */
448             /* Table 32, priority 100.
449              * =======================
450              *
451              * Implements output to remote hypervisors.  Each flow matches an
452              * output port that includes a logical port on a remote hypervisor,
453              * and tunnels the packet to that hypervisor.
454              */
455
456             match_init_catchall(&match);
457             ofpbuf_clear(&ofpacts);
458
459             /* Match MFF_LOG_DATAPATH, MFF_LOG_OUTPORT. */
460             match_set_metadata(&match, htonll(binding->datapath->tunnel_key));
461             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0,
462                           binding->tunnel_key);
463
464             put_encapsulation(mff_ovn_geneve, tun, binding->datapath,
465                               binding->tunnel_key, &ofpacts);
466
467             /* Output to tunnel. */
468             ofpact_put_OUTPUT(&ofpacts)->port = ofport;
469             ofctrl_add_flow(flow_table, OFTABLE_REMOTE_OUTPUT, 100,
470                             &match, &ofpacts);
471         }
472
473         /* Table 34, Priority 100.
474          * =======================
475          *
476          * Drop packets whose logical inport and outport are the same. */
477         match_init_catchall(&match);
478         ofpbuf_clear(&ofpacts);
479         match_set_metadata(&match, htonll(binding->datapath->tunnel_key));
480         match_set_reg(&match, MFF_LOG_INPORT - MFF_REG0, binding->tunnel_key);
481         match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, binding->tunnel_key);
482         ofctrl_add_flow(flow_table, OFTABLE_DROP_LOOPBACK, 100,
483                         &match, &ofpacts);
484     }
485
486     /* Handle output to multicast groups, in tables 32 and 33. */
487     const struct sbrec_multicast_group *mc;
488     struct ofpbuf remote_ofpacts;
489     ofpbuf_init(&remote_ofpacts, 0);
490     SBREC_MULTICAST_GROUP_FOR_EACH (mc, ctx->ovnsb_idl) {
491         struct sset remote_chassis = SSET_INITIALIZER(&remote_chassis);
492         struct match match;
493
494         match_init_catchall(&match);
495         match_set_metadata(&match, htonll(mc->datapath->tunnel_key));
496         match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, mc->tunnel_key);
497
498         /* Go through all of the ports in the multicast group:
499          *
500          *    - For remote ports, add the chassis to 'remote_chassis'.
501          *
502          *    - For local ports (other than logical patch ports), add actions
503          *      to 'ofpacts' to set the output port and resubmit.
504          *
505          *    - For logical patch ports, add actions to 'remote_ofpacts'
506          *      instead.  (If we put them in 'ofpacts', then the output
507          *      would happen on every hypervisor in the multicast group,
508          *      effectively duplicating the packet.)
509          */
510         ofpbuf_clear(&ofpacts);
511         ofpbuf_clear(&remote_ofpacts);
512         for (size_t i = 0; i < mc->n_ports; i++) {
513             struct sbrec_port_binding *port = mc->ports[i];
514
515             if (port->datapath != mc->datapath) {
516                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
517                 VLOG_WARN_RL(&rl, UUID_FMT": multicast group contains ports "
518                              "in wrong datapath",
519                              UUID_ARGS(&mc->header_.uuid));
520                 continue;
521             }
522
523             int zone_id = simap_get(ct_zones, port->logical_port);
524             if (zone_id) {
525                 put_load(zone_id, MFF_LOG_CT_ZONE, 0, 32, &ofpacts);
526             }
527
528             if (!strcmp(port->type, "patch")) {
529                 put_load(port->tunnel_key, MFF_LOG_OUTPORT, 0, 32,
530                          &remote_ofpacts);
531                 put_resubmit(OFTABLE_DROP_LOOPBACK, &remote_ofpacts);
532             } else if (simap_contains(&localvif_to_ofport,
533                                (port->parent_port && *port->parent_port)
534                                ? port->parent_port : port->logical_port)) {
535                 put_load(port->tunnel_key, MFF_LOG_OUTPORT, 0, 32, &ofpacts);
536                 put_resubmit(OFTABLE_DROP_LOOPBACK, &ofpacts);
537             } else if (port->chassis && !get_localnet_port(local_datapaths,
538                                              mc->datapath->tunnel_key)) {
539                 /* Add remote chassis only when localnet port not exist,
540                  * otherwise multicast will reach remote ports through localnet
541                  * port. */
542                 sset_add(&remote_chassis, port->chassis->name);
543             }
544         }
545
546         /* Table 33, priority 100.
547          * =======================
548          *
549          * Handle output to the local logical ports in the multicast group, if
550          * any. */
551         bool local_ports = ofpacts.size > 0;
552         if (local_ports) {
553             /* Following delivery to local logical ports, restore the multicast
554              * group as the logical output port. */
555             put_load(mc->tunnel_key, MFF_LOG_OUTPORT, 0, 32, &ofpacts);
556
557             ofctrl_add_flow(flow_table, OFTABLE_LOCAL_OUTPUT, 100,
558                             &match, &ofpacts);
559         }
560
561         /* Table 32, priority 100.
562          * =======================
563          *
564          * Handle output to the remote chassis in the multicast group, if
565          * any. */
566         if (!sset_is_empty(&remote_chassis) || remote_ofpacts.size > 0) {
567             if (remote_ofpacts.size > 0) {
568                 /* Following delivery to logical patch ports, restore the
569                  * multicast group as the logical output port. */
570                 put_load(mc->tunnel_key, MFF_LOG_OUTPORT, 0, 32,
571                          &remote_ofpacts);
572             }
573
574             const char *chassis;
575             const struct chassis_tunnel *prev = NULL;
576             SSET_FOR_EACH (chassis, &remote_chassis) {
577                 const struct chassis_tunnel *tun
578                     = chassis_tunnel_find(&tunnels, chassis);
579                 if (!tun) {
580                     continue;
581                 }
582
583                 if (!prev || tun->type != prev->type) {
584                     put_encapsulation(mff_ovn_geneve, tun, mc->datapath,
585                                       mc->tunnel_key, &remote_ofpacts);
586                     prev = tun;
587                 }
588                 ofpact_put_OUTPUT(&remote_ofpacts)->port = tun->ofport;
589             }
590
591             if (remote_ofpacts.size) {
592                 if (local_ports) {
593                     put_resubmit(OFTABLE_LOCAL_OUTPUT, &remote_ofpacts);
594                 }
595                 ofctrl_add_flow(flow_table, OFTABLE_REMOTE_OUTPUT, 100,
596                                 &match, &remote_ofpacts);
597             }
598         }
599         sset_destroy(&remote_chassis);
600     }
601     ofpbuf_uninit(&remote_ofpacts);
602
603     /* Table 0, priority 100.
604      * ======================
605      *
606      * Process packets that arrive from a remote hypervisor (by matching
607      * on tunnel in_port). */
608
609     /* Add flows for Geneve and STT encapsulations.  These
610      * encapsulations have metadata about the ingress and egress logical
611      * ports.  We set MFF_LOG_DATAPATH, MFF_LOG_INPORT, and
612      * MFF_LOG_OUTPORT from the tunnel key data, then resubmit to table
613      * 33 to handle packets to the local hypervisor. */
614     struct chassis_tunnel *tun;
615     HMAP_FOR_EACH (tun, hmap_node, &tunnels) {
616         struct match match = MATCH_CATCHALL_INITIALIZER;
617         match_set_in_port(&match, tun->ofport);
618
619         ofpbuf_clear(&ofpacts);
620         if (tun->type == GENEVE) {
621             put_move(MFF_TUN_ID, 0,  MFF_LOG_DATAPATH, 0, 24, &ofpacts);
622             put_move(mff_ovn_geneve, 16, MFF_LOG_INPORT, 0, 15,
623                      &ofpacts);
624             put_move(mff_ovn_geneve, 0, MFF_LOG_OUTPORT, 0, 16,
625                      &ofpacts);
626         } else if (tun->type == STT) {
627             put_move(MFF_TUN_ID, 40, MFF_LOG_INPORT,   0, 15, &ofpacts);
628             put_move(MFF_TUN_ID, 24, MFF_LOG_OUTPORT,  0, 16, &ofpacts);
629             put_move(MFF_TUN_ID,  0, MFF_LOG_DATAPATH, 0, 24, &ofpacts);
630         } else if (tun->type == VXLAN) {
631             /* We'll handle VXLAN later. */
632             continue;
633         } else {
634             OVS_NOT_REACHED();
635         }
636
637         put_resubmit(OFTABLE_LOCAL_OUTPUT, &ofpacts);
638
639         ofctrl_add_flow(flow_table, OFTABLE_PHY_TO_LOG, 100, &match, &ofpacts);
640     }
641
642     /* Add flows for VXLAN encapsulations.  Due to the limited amount of
643      * metadata, we only support VXLAN for connections to gateways.  The
644      * VNI is used to populate MFF_LOG_DATAPATH.  The gateway's logical
645      * port is set to MFF_LOG_INPORT.  Then the packet is resubmitted to
646      * table 16 to determine the logical egress port.
647      *
648      * xxx Due to resubmitting to table 16, broadcasts will be re-sent to
649      * xxx all logical ports, including non-local ones which could cause
650      * xxx duplicate packets to be received by multiply-connected gateways. */
651     HMAP_FOR_EACH (tun, hmap_node, &tunnels) {
652         if (tun->type != VXLAN) {
653             continue;
654         }
655
656         SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
657             struct match match = MATCH_CATCHALL_INITIALIZER;
658
659             if (!binding->chassis ||
660                 strcmp(tun->chassis_id, binding->chassis->name)) {
661                 continue;
662             }
663
664             match_set_in_port(&match, tun->ofport);
665             match_set_tun_id(&match, htonll(binding->datapath->tunnel_key));
666
667             ofpbuf_clear(&ofpacts);
668             put_move(MFF_TUN_ID, 0,  MFF_LOG_DATAPATH, 0, 24, &ofpacts);
669             put_load(binding->tunnel_key, MFF_LOG_INPORT, 0, 15, &ofpacts);
670             put_resubmit(OFTABLE_LOG_INGRESS_PIPELINE, &ofpacts);
671
672             ofctrl_add_flow(flow_table, OFTABLE_PHY_TO_LOG, 100, &match,
673                     &ofpacts);
674         }
675     }
676
677     /* Table 32, Priority 0.
678      * =======================
679      *
680      * Resubmit packets that are not directed at tunnels or part of a
681      * multicast group to the local output table. */
682     struct match match;
683     match_init_catchall(&match);
684     ofpbuf_clear(&ofpacts);
685     put_resubmit(OFTABLE_LOCAL_OUTPUT, &ofpacts);
686     ofctrl_add_flow(flow_table, OFTABLE_REMOTE_OUTPUT, 0, &match, &ofpacts);
687
688     /* Table 34, Priority 0.
689      * =======================
690      *
691      * Resubmit packets that don't output to the ingress port (already checked
692      * in table 33) to the logical egress pipeline, clearing the logical
693      * registers (for consistent behavior with packets that get tunneled). */
694     match_init_catchall(&match);
695     ofpbuf_clear(&ofpacts);
696 #define MFF_LOG_REG(ID) put_load(0, ID, 0, 32, &ofpacts);
697     MFF_LOG_REGS;
698 #undef MFF_LOG_REGS
699     put_resubmit(OFTABLE_LOG_EGRESS_PIPELINE, &ofpacts);
700     ofctrl_add_flow(flow_table, OFTABLE_DROP_LOOPBACK, 0, &match, &ofpacts);
701
702     ofpbuf_uninit(&ofpacts);
703     simap_destroy(&localvif_to_ofport);
704     struct chassis_tunnel *tun_next;
705     HMAP_FOR_EACH_SAFE (tun, tun_next, hmap_node, &tunnels) {
706         hmap_remove(&tunnels, &tun->hmap_node);
707         free(tun);
708     }
709     hmap_destroy(&tunnels);
710 }