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