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