lib/daemon: support --user option for all OVS daemon
[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 { GENEVE, STT } 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 {
124         OVS_NOT_REACHED();
125     }
126 }
127
128 static void
129 put_stack(enum mf_field_id field, struct ofpact_stack *stack)
130 {
131     stack->subfield.field = mf_from_id(field);
132     stack->subfield.ofs = 0;
133     stack->subfield.n_bits = stack->subfield.field->n_bits;
134 }
135
136 void
137 physical_run(struct controller_ctx *ctx, enum mf_field_id mff_ovn_geneve,
138              const struct ovsrec_bridge *br_int, const char *this_chassis_id,
139              struct hmap *flow_table)
140 {
141     struct simap localvif_to_ofport = SIMAP_INITIALIZER(&localvif_to_ofport);
142     struct hmap tunnels = HMAP_INITIALIZER(&tunnels);
143     struct simap localnet_to_ofport = SIMAP_INITIALIZER(&localnet_to_ofport);
144
145     for (int i = 0; i < br_int->n_ports; i++) {
146         const struct ovsrec_port *port_rec = br_int->ports[i];
147         if (!strcmp(port_rec->name, br_int->name)) {
148             continue;
149         }
150
151         const char *chassis_id = smap_get(&port_rec->external_ids,
152                                           "ovn-chassis-id");
153         if (chassis_id && !strcmp(chassis_id, this_chassis_id)) {
154             continue;
155         }
156
157         const char *localnet = smap_get(&port_rec->external_ids,
158                                         "ovn-patch-port");
159
160         for (int j = 0; j < port_rec->n_interfaces; j++) {
161             const struct ovsrec_interface *iface_rec = port_rec->interfaces[j];
162
163             /* Get OpenFlow port number. */
164             if (!iface_rec->n_ofport) {
165                 continue;
166             }
167             int64_t ofport = iface_rec->ofport[0];
168             if (ofport < 1 || ofport > ofp_to_u16(OFPP_MAX)) {
169                 continue;
170             }
171
172             /* Record as patch to local net, chassis, or local logical port. */
173             if (!strcmp(iface_rec->type, "patch") && localnet) {
174                 simap_put(&localnet_to_ofport, localnet, ofport);
175                 break;
176             } else if (chassis_id) {
177                 enum chassis_tunnel_type tunnel_type;
178                 if (!strcmp(iface_rec->type, "geneve")) {
179                     tunnel_type = GENEVE;
180                     if (!mff_ovn_geneve) {
181                         continue;
182                     }
183                 } else if (!strcmp(iface_rec->type, "stt")) {
184                     tunnel_type = STT;
185                 } else {
186                     continue;
187                 }
188
189                 struct chassis_tunnel *tun = xmalloc(sizeof *tun);
190                 hmap_insert(&tunnels, &tun->hmap_node,
191                             hash_string(chassis_id, 0));
192                 tun->chassis_id = chassis_id;
193                 tun->ofport = u16_to_ofp(ofport);
194                 tun->type = tunnel_type;
195                 break;
196             } else {
197                 const char *iface_id = smap_get(&iface_rec->external_ids,
198                                                 "iface-id");
199                 if (iface_id) {
200                     simap_put(&localvif_to_ofport, iface_id, ofport);
201                 }
202             }
203         }
204     }
205
206     struct ofpbuf ofpacts;
207     ofpbuf_init(&ofpacts, 0);
208
209     struct binding_elem {
210         struct ovs_list list_elem;
211         const struct sbrec_port_binding *binding;
212     };
213     struct localnet_bindings {
214         ofp_port_t ofport;
215         struct ovs_list bindings;
216     };
217     /* Maps from network name to "struct localnet_bindings". */
218     struct shash localnet_inputs = SHASH_INITIALIZER(&localnet_inputs);
219
220     /* Contains bare "struct hmap_node"s whose hash values are the tunnel_key
221      * of datapaths with at least one local port binding. */
222     struct hmap local_datapaths = HMAP_INITIALIZER(&local_datapaths);
223
224     /* Set up flows in table 0 for physical-to-logical translation and in table
225      * 64 for logical-to-physical translation. */
226     const struct sbrec_port_binding *binding;
227     SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
228         /* Find the OpenFlow port for the logical port, as 'ofport'.  This is
229          * one of:
230          *
231          *     - If the port is a VIF on the chassis we're managing, the
232          *       OpenFlow port for the VIF.  'tun' will be NULL.
233          *
234          *       In this or the next case, for a container nested inside a VM
235          *       and accessible via a VLAN, 'tag' is the VLAN ID; otherwise
236          *       'tag' is 0.
237          *
238          *     - If the port is on a remote chassis, the OpenFlow port for a
239          *       tunnel to the VIF's remote chassis.  'tun' identifies that
240          *       tunnel.
241          *
242          *     - If the port is a "localnet" port for a network that is
243          *       attached to the chassis we're managing, the OpenFlow port for
244          *       the localnet port (a patch port).
245          */
246
247         int tag = 0;
248         ofp_port_t ofport;
249         if (!strcmp(binding->type, "localnet")) {
250             const char *network = smap_get(&binding->options, "network_name");
251             if (!network) {
252                 continue;
253             }
254             ofport = u16_to_ofp(simap_get(&localnet_to_ofport, network));
255         } else if (binding->parent_port) {
256             if (!binding->tag || !*binding->tag) {
257                 continue;
258             }
259             ofport = u16_to_ofp(simap_get(&localvif_to_ofport,
260                                           binding->parent_port));
261             if (ofport) {
262                 tag = *binding->tag;
263             }
264         } else {
265             ofport = u16_to_ofp(simap_get(&localvif_to_ofport,
266                                           binding->logical_port));
267         }
268
269         const struct chassis_tunnel *tun = NULL;
270         if (!ofport) {
271             if (!binding->chassis) {
272                 continue;
273             }
274             tun = chassis_tunnel_find(&tunnels, binding->chassis->name);
275             if (!tun) {
276                 continue;
277             }
278             ofport = tun->ofport;
279         }
280
281         struct match match;
282         if (!tun) {
283             /* Packets that arrive from a vif can belong to a VM or
284              * to a container located inside that VM. Packets that
285              * arrive from containers have a tag (vlan) associated with them.
286              */
287
288             /* Table 0, Priority 150 and 100.
289              * ==============================
290              *
291              * Priority 150 is for traffic belonging to containers. For such
292              * traffic, match on the tags and then strip the tag.
293              * Priority 100 is for traffic belonging to VMs or locally connected
294              * networks.
295              *
296              * For both types of traffic: set MFF_LOG_INPORT to the logical
297              * input port, MFF_LOG_DATAPATH to the logical datapath, and
298              * resubmit into the logical ingress pipeline starting at table
299              * 16. */
300             if (!strcmp(binding->type, "localnet")) {
301                 /* The same OpenFlow port may correspond to localnet ports
302                  * attached to more than one logical datapath, so keep track of
303                  * all associated bindings and add a flow at the end. */
304
305                 const char *network
306                     = smap_get(&binding->options, "network_name");
307                 struct localnet_bindings *ln_bindings;
308
309                 ln_bindings = shash_find_data(&localnet_inputs, network);
310                 if (!ln_bindings) {
311                     ln_bindings = xmalloc(sizeof *ln_bindings);
312                     ln_bindings->ofport = ofport;
313                     list_init(&ln_bindings->bindings);
314                     shash_add(&localnet_inputs, network, ln_bindings);
315                 }
316
317                 struct binding_elem *b = xmalloc(sizeof *b);
318                 b->binding = binding;
319                 list_insert(&ln_bindings->bindings, &b->list_elem);
320             } else {
321                 struct hmap_node *ld;
322                 ld = hmap_first_with_hash(&local_datapaths,
323                                           binding->datapath->tunnel_key);
324                 if (!ld) {
325                     ld = xmalloc(sizeof *ld);
326                     hmap_insert(&local_datapaths, ld,
327                                 binding->datapath->tunnel_key);
328                 }
329
330                 ofpbuf_clear(&ofpacts);
331                 match_init_catchall(&match);
332                 match_set_in_port(&match, ofport);
333                 if (tag) {
334                     match_set_dl_vlan(&match, htons(tag));
335                 }
336
337                 /* Set MFF_LOG_DATAPATH and MFF_LOG_INPORT. */
338                 put_load(binding->datapath->tunnel_key, MFF_LOG_DATAPATH, 0, 64,
339                          &ofpacts);
340                 put_load(binding->tunnel_key, MFF_LOG_INPORT, 0, 32,
341                          &ofpacts);
342
343                 /* Strip vlans. */
344                 if (tag) {
345                     ofpact_put_STRIP_VLAN(&ofpacts);
346                 }
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
354             /* Table 33, priority 100.
355              * =======================
356              *
357              * Implements output to local hypervisor.  Each flow matches a
358              * logical output port on the local hypervisor, and resubmits to
359              * table 34.
360              */
361
362             match_init_catchall(&match);
363             ofpbuf_clear(&ofpacts);
364
365             /* Match MFF_LOG_DATAPATH, MFF_LOG_OUTPORT. */
366             match_set_metadata(&match, htonll(binding->datapath->tunnel_key));
367             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0,
368                           binding->tunnel_key);
369
370             /* Resubmit to table 34. */
371             put_resubmit(OFTABLE_DROP_LOOPBACK, &ofpacts);
372             ofctrl_add_flow(flow_table, OFTABLE_LOCAL_OUTPUT, 100, &match,
373                             &ofpacts);
374
375             /* Table 64, Priority 100.
376              * =======================
377              *
378              * Deliver the packet to the local vif. */
379             match_init_catchall(&match);
380             ofpbuf_clear(&ofpacts);
381             match_set_metadata(&match, htonll(binding->datapath->tunnel_key));
382             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0,
383                           binding->tunnel_key);
384             if (tag) {
385                 /* For containers sitting behind a local vif, tag the packets
386                  * before delivering them. */
387                 struct ofpact_vlan_vid *vlan_vid;
388                 vlan_vid = ofpact_put_SET_VLAN_VID(&ofpacts);
389                 vlan_vid->vlan_vid = tag;
390                 vlan_vid->push_vlan_if_needed = true;
391
392                 /* A packet might need to hair-pin back into its ingress
393                  * OpenFlow port (to a different logical port, which we already
394                  * checked back in table 34), so set the in_port to zero. */
395                 put_stack(MFF_IN_PORT, ofpact_put_STACK_PUSH(&ofpacts));
396                 put_load(0, MFF_IN_PORT, 0, 16, &ofpacts);
397             }
398             ofpact_put_OUTPUT(&ofpacts)->port = ofport;
399             if (tag) {
400                 /* Revert the tag added to the packets headed to containers
401                  * in the previous step. If we don't do this, the packets
402                  * that are to be broadcasted to a VM in the same logical
403                  * switch will also contain the tag. Also revert the zero'd
404                  * in_port. */
405                 ofpact_put_STRIP_VLAN(&ofpacts);
406                 put_stack(MFF_IN_PORT, ofpact_put_STACK_POP(&ofpacts));
407             }
408             ofctrl_add_flow(flow_table, OFTABLE_LOG_TO_PHY, 100,
409                             &match, &ofpacts);
410         } else {
411             /* Table 32, priority 100.
412              * =======================
413              *
414              * Implements output to remote hypervisors.  Each flow matches an
415              * output port that includes a logical port on a remote hypervisor,
416              * and tunnels the packet to that hypervisor.
417              */
418
419             match_init_catchall(&match);
420             ofpbuf_clear(&ofpacts);
421
422             /* Match MFF_LOG_DATAPATH, MFF_LOG_OUTPORT. */
423             match_set_metadata(&match, htonll(binding->datapath->tunnel_key));
424             match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0,
425                           binding->tunnel_key);
426
427             put_encapsulation(mff_ovn_geneve, tun, binding->datapath,
428                               binding->tunnel_key, &ofpacts);
429
430             /* Output to tunnel. */
431             ofpact_put_OUTPUT(&ofpacts)->port = ofport;
432             ofctrl_add_flow(flow_table, OFTABLE_REMOTE_OUTPUT, 100,
433                             &match, &ofpacts);
434         }
435
436         /* Table 34, Priority 100.
437          * =======================
438          *
439          * Drop packets whose logical inport and outport are the same. */
440         match_init_catchall(&match);
441         ofpbuf_clear(&ofpacts);
442         match_set_metadata(&match, htonll(binding->datapath->tunnel_key));
443         match_set_reg(&match, MFF_LOG_INPORT - MFF_REG0, binding->tunnel_key);
444         match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, binding->tunnel_key);
445         ofctrl_add_flow(flow_table, OFTABLE_DROP_LOOPBACK, 100,
446                         &match, &ofpacts);
447     }
448
449     /* Handle output to multicast groups, in tables 32 and 33. */
450     const struct sbrec_multicast_group *mc;
451     SBREC_MULTICAST_GROUP_FOR_EACH (mc, ctx->ovnsb_idl) {
452         struct sset remote_chassis = SSET_INITIALIZER(&remote_chassis);
453         struct match match;
454
455         match_init_catchall(&match);
456         match_set_metadata(&match, htonll(mc->datapath->tunnel_key));
457         match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, mc->tunnel_key);
458
459         /* Go through all of the ports in the multicast group:
460          *
461          *    - For local ports, add actions to 'ofpacts' to set the output
462          *      port and resubmit.
463          *
464          *    - For remote ports, add the chassis to 'remote_chassis'. */
465         ofpbuf_clear(&ofpacts);
466         for (size_t i = 0; i < mc->n_ports; i++) {
467             struct sbrec_port_binding *port = mc->ports[i];
468
469             if (port->datapath != mc->datapath) {
470                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
471                 VLOG_WARN_RL(&rl, UUID_FMT": multicast group contains ports "
472                              "in wrong datapath",
473                              UUID_ARGS(&mc->header_.uuid));
474                 continue;
475             }
476
477             if (simap_contains(&localvif_to_ofport,
478                                port->parent_port
479                                ? port->parent_port : port->logical_port)) {
480                 put_load(port->tunnel_key, MFF_LOG_OUTPORT, 0, 32, &ofpacts);
481                 put_resubmit(OFTABLE_DROP_LOOPBACK, &ofpacts);
482             } else if (port->chassis) {
483                 sset_add(&remote_chassis, port->chassis->name);
484             } else if (!strcmp(port->type, "localnet")) {
485                 const char *network = smap_get(&port->options, "network_name");
486                 if (!network) {
487                     continue;
488                 }
489                 if (!simap_contains(&localnet_to_ofport, network)) {
490                     continue;
491                 }
492                 put_load(port->tunnel_key, MFF_LOG_OUTPORT, 0, 32, &ofpacts);
493                 put_resubmit(OFTABLE_DROP_LOOPBACK, &ofpacts);
494             }
495         }
496
497         /* Table 33, priority 100.
498          * =======================
499          *
500          * Handle output to the local logical ports in the multicast group, if
501          * any. */
502         bool local_ports = ofpacts.size > 0;
503         if (local_ports) {
504             ofctrl_add_flow(flow_table, OFTABLE_LOCAL_OUTPUT, 100,
505                             &match, &ofpacts);
506         }
507
508         /* Table 32, priority 100.
509          * =======================
510          *
511          * Handle output to the remote chassis in the multicast group, if
512          * any. */
513         if (!sset_is_empty(&remote_chassis)) {
514             ofpbuf_clear(&ofpacts);
515
516             const char *chassis;
517             const struct chassis_tunnel *prev = NULL;
518             SSET_FOR_EACH (chassis, &remote_chassis) {
519                 const struct chassis_tunnel *tun
520                     = chassis_tunnel_find(&tunnels, chassis);
521                 if (!tun) {
522                     continue;
523                 }
524
525                 if (!prev || tun->type != prev->type) {
526                     put_encapsulation(mff_ovn_geneve, tun,
527                                       mc->datapath, mc->tunnel_key, &ofpacts);
528                     prev = tun;
529                 }
530                 ofpact_put_OUTPUT(&ofpacts)->port = tun->ofport;
531             }
532
533             if (ofpacts.size) {
534                 if (local_ports) {
535                     put_resubmit(OFTABLE_LOCAL_OUTPUT, &ofpacts);
536                 }
537                 ofctrl_add_flow(flow_table, OFTABLE_REMOTE_OUTPUT, 100,
538                                 &match, &ofpacts);
539             }
540         }
541         sset_destroy(&remote_chassis);
542     }
543
544     /* Table 0, priority 100.
545      * ======================
546      *
547      * For packets that arrive from a remote hypervisor (by matching a tunnel
548      * in_port), set MFF_LOG_DATAPATH, MFF_LOG_INPORT, and MFF_LOG_OUTPORT from
549      * the tunnel key data, then resubmit to table 33 to handle packets to the
550      * local hypervisor. */
551
552     struct chassis_tunnel *tun;
553     HMAP_FOR_EACH (tun, hmap_node, &tunnels) {
554         struct match match = MATCH_CATCHALL_INITIALIZER;
555         match_set_in_port(&match, tun->ofport);
556
557         ofpbuf_clear(&ofpacts);
558         if (tun->type == GENEVE) {
559             put_move(MFF_TUN_ID, 0,  MFF_LOG_DATAPATH, 0, 24, &ofpacts);
560             put_move(mff_ovn_geneve, 16, MFF_LOG_INPORT, 0, 15,
561                      &ofpacts);
562             put_move(mff_ovn_geneve, 0, MFF_LOG_OUTPORT, 0, 16,
563                      &ofpacts);
564         } else if (tun->type == STT) {
565             put_move(MFF_TUN_ID, 40, MFF_LOG_INPORT,   0, 15, &ofpacts);
566             put_move(MFF_TUN_ID, 24, MFF_LOG_OUTPORT,  0, 16, &ofpacts);
567             put_move(MFF_TUN_ID,  0, MFF_LOG_DATAPATH, 0, 24, &ofpacts);
568         } else {
569             OVS_NOT_REACHED();
570         }
571         put_resubmit(OFTABLE_LOCAL_OUTPUT, &ofpacts);
572
573         ofctrl_add_flow(flow_table, OFTABLE_PHY_TO_LOG, 100, &match, &ofpacts);
574     }
575
576     /* Table 32, Priority 0.
577      * =======================
578      *
579      * Resubmit packets that are not directed at tunnels or part of a
580      * multicast group to the local output table. */
581     struct match match;
582     match_init_catchall(&match);
583     ofpbuf_clear(&ofpacts);
584     put_resubmit(OFTABLE_LOCAL_OUTPUT, &ofpacts);
585     ofctrl_add_flow(flow_table, OFTABLE_REMOTE_OUTPUT, 0, &match, &ofpacts);
586
587     /* Table 34, Priority 0.
588      * =======================
589      *
590      * Resubmit packets that don't output to the ingress port (already checked
591      * in table 33) to the logical egress pipeline, clearing the logical
592      * registers (for consistent behavior with packets that get tunneled). */
593     match_init_catchall(&match);
594     ofpbuf_clear(&ofpacts);
595 #define MFF_LOG_REG(ID) put_load(0, ID, 0, 32, &ofpacts);
596     MFF_LOG_REGS;
597 #undef MFF_LOG_REGS
598     put_resubmit(OFTABLE_LOG_EGRESS_PIPELINE, &ofpacts);
599     ofctrl_add_flow(flow_table, OFTABLE_DROP_LOOPBACK, 0, &match, &ofpacts);
600
601     ofpbuf_uninit(&ofpacts);
602     simap_destroy(&localvif_to_ofport);
603     struct chassis_tunnel *tun_next;
604     HMAP_FOR_EACH_SAFE (tun, tun_next, hmap_node, &tunnels) {
605         hmap_remove(&tunnels, &tun->hmap_node);
606         free(tun);
607     }
608     hmap_destroy(&tunnels);
609
610     /* Table 0, Priority 100
611      * =====================
612      *
613      * We have now determined the full set of port bindings associated with
614      * each "localnet" network.  Only create flows for datapaths that have
615      * another local binding.  Otherwise, we know it would just be dropped.
616      */
617     struct shash_node *ln_bindings_node, *ln_bindings_node_next;
618     SHASH_FOR_EACH_SAFE (ln_bindings_node, ln_bindings_node_next,
619                          &localnet_inputs) {
620         struct localnet_bindings *ln_bindings = ln_bindings_node->data;
621
622         struct match match;
623         match_init_catchall(&match);
624         match_set_in_port(&match, ln_bindings->ofport);
625
626         struct ofpbuf ofpacts;
627         ofpbuf_init(&ofpacts, 0);
628
629         struct binding_elem *b;
630         LIST_FOR_EACH_POP (b, list_elem, &ln_bindings->bindings) {
631             struct hmap_node *ld;
632             ld = hmap_first_with_hash(&local_datapaths,
633                                       b->binding->datapath->tunnel_key);
634             if (ld) {
635                 /* Set MFF_LOG_DATAPATH and MFF_LOG_INPORT. */
636                 put_load(b->binding->datapath->tunnel_key, MFF_LOG_DATAPATH,
637                          0, 64, &ofpacts);
638                 put_load(b->binding->tunnel_key, MFF_LOG_INPORT, 0, 32,
639                          &ofpacts);
640                 put_resubmit(OFTABLE_LOG_INGRESS_PIPELINE, &ofpacts);
641             }
642
643             free(b);
644         }
645
646         if (ofpacts.size) {
647             ofctrl_add_flow(flow_table, 0, 100, &match, &ofpacts);
648         }
649
650         ofpbuf_uninit(&ofpacts);
651
652         shash_delete(&localnet_inputs, ln_bindings_node);
653         free(ln_bindings);
654     }
655     shash_destroy(&localnet_inputs);
656
657     struct hmap_node *node;
658     while ((node = hmap_first(&local_datapaths))) {
659         hmap_remove(&local_datapaths, node);
660         free(node);
661     }
662     hmap_destroy(&local_datapaths);
663
664     simap_destroy(&localnet_to_ofport);
665 }