ovn-controller: Add support for load balancing.
[cascardo/ovs.git] / ovn / controller / lflow.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 "lflow.h"
18 #include "lport.h"
19 #include "ofctrl.h"
20 #include "openvswitch/dynamic-string.h"
21 #include "openvswitch/ofp-actions.h"
22 #include "openvswitch/ofpbuf.h"
23 #include "openvswitch/vlog.h"
24 #include "ovn-controller.h"
25 #include "ovn/lib/actions.h"
26 #include "ovn/lib/expr.h"
27 #include "ovn/lib/ovn-dhcp.h"
28 #include "ovn/lib/ovn-sb-idl.h"
29 #include "packets.h"
30 #include "simap.h"
31 #include "sset.h"
32
33 VLOG_DEFINE_THIS_MODULE(lflow);
34 \f
35 /* Symbol table. */
36
37 /* Contains "struct expr_symbol"s for fields supported by OVN lflows. */
38 static struct shash symtab;
39
40 /* Contains an internal expr datastructure that represents an address set. */
41 static struct shash expr_address_sets;
42
43 static void
44 add_logical_register(struct shash *symtab, enum mf_field_id id)
45 {
46     char name[8];
47
48     snprintf(name, sizeof name, "reg%d", id - MFF_REG0);
49     expr_symtab_add_field(symtab, name, id, NULL, false);
50 }
51
52 void
53 lflow_init(void)
54 {
55     shash_init(&symtab);
56     shash_init(&expr_address_sets);
57
58     /* Reserve a pair of registers for the logical inport and outport.  A full
59      * 32-bit register each is bigger than we need, but the expression code
60      * doesn't yet support string fields that occupy less than a full OXM. */
61     expr_symtab_add_string(&symtab, "inport", MFF_LOG_INPORT, NULL);
62     expr_symtab_add_string(&symtab, "outport", MFF_LOG_OUTPORT, NULL);
63
64     /* Logical registers. */
65 #define MFF_LOG_REG(ID) add_logical_register(&symtab, ID);
66     MFF_LOG_REGS;
67 #undef MFF_LOG_REG
68
69     /* Connection tracking state. */
70     expr_symtab_add_field(&symtab, "ct_mark", MFF_CT_MARK, NULL, false);
71     expr_symtab_add_field(&symtab, "ct_label", MFF_CT_LABEL, NULL, false);
72     expr_symtab_add_field(&symtab, "ct_state", MFF_CT_STATE, NULL, false);
73     char ct_state_str[16];
74     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_TRACKED_BIT);
75     expr_symtab_add_predicate(&symtab, "ct.trk", ct_state_str);
76     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_NEW_BIT);
77     expr_symtab_add_subfield(&symtab, "ct.new", "ct.trk", ct_state_str);
78     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_ESTABLISHED_BIT);
79     expr_symtab_add_subfield(&symtab, "ct.est", "ct.trk", ct_state_str);
80     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_RELATED_BIT);
81     expr_symtab_add_subfield(&symtab, "ct.rel", "ct.trk", ct_state_str);
82     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_REPLY_DIR_BIT);
83     expr_symtab_add_subfield(&symtab, "ct.rpl", "ct.trk", ct_state_str);
84     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_INVALID_BIT);
85     expr_symtab_add_subfield(&symtab, "ct.inv", "ct.trk", ct_state_str);
86
87     /* Data fields. */
88     expr_symtab_add_field(&symtab, "eth.src", MFF_ETH_SRC, NULL, false);
89     expr_symtab_add_field(&symtab, "eth.dst", MFF_ETH_DST, NULL, false);
90     expr_symtab_add_field(&symtab, "eth.type", MFF_ETH_TYPE, NULL, true);
91     expr_symtab_add_predicate(&symtab, "eth.bcast",
92                               "eth.dst == ff:ff:ff:ff:ff:ff");
93     expr_symtab_add_subfield(&symtab, "eth.mcast", NULL, "eth.dst[40]");
94
95     expr_symtab_add_field(&symtab, "vlan.tci", MFF_VLAN_TCI, NULL, false);
96     expr_symtab_add_predicate(&symtab, "vlan.present", "vlan.tci[12]");
97     expr_symtab_add_subfield(&symtab, "vlan.pcp", "vlan.present",
98                              "vlan.tci[13..15]");
99     expr_symtab_add_subfield(&symtab, "vlan.vid", "vlan.present",
100                              "vlan.tci[0..11]");
101
102     expr_symtab_add_predicate(&symtab, "ip4", "eth.type == 0x800");
103     expr_symtab_add_predicate(&symtab, "ip6", "eth.type == 0x86dd");
104     expr_symtab_add_predicate(&symtab, "ip", "ip4 || ip6");
105     expr_symtab_add_field(&symtab, "ip.proto", MFF_IP_PROTO, "ip", true);
106     expr_symtab_add_field(&symtab, "ip.dscp", MFF_IP_DSCP, "ip", false);
107     expr_symtab_add_field(&symtab, "ip.ecn", MFF_IP_ECN, "ip", false);
108     expr_symtab_add_field(&symtab, "ip.ttl", MFF_IP_TTL, "ip", false);
109
110     expr_symtab_add_field(&symtab, "ip4.src", MFF_IPV4_SRC, "ip4", false);
111     expr_symtab_add_field(&symtab, "ip4.dst", MFF_IPV4_DST, "ip4", false);
112     expr_symtab_add_predicate(&symtab, "ip4.mcast", "ip4.dst[28..31] == 0xe");
113
114     expr_symtab_add_predicate(&symtab, "icmp4", "ip4 && ip.proto == 1");
115     expr_symtab_add_field(&symtab, "icmp4.type", MFF_ICMPV4_TYPE, "icmp4",
116               false);
117     expr_symtab_add_field(&symtab, "icmp4.code", MFF_ICMPV4_CODE, "icmp4",
118               false);
119
120     expr_symtab_add_field(&symtab, "ip6.src", MFF_IPV6_SRC, "ip6", false);
121     expr_symtab_add_field(&symtab, "ip6.dst", MFF_IPV6_DST, "ip6", false);
122     expr_symtab_add_field(&symtab, "ip6.label", MFF_IPV6_LABEL, "ip6", false);
123
124     expr_symtab_add_predicate(&symtab, "icmp6", "ip6 && ip.proto == 58");
125     expr_symtab_add_field(&symtab, "icmp6.type", MFF_ICMPV6_TYPE, "icmp6",
126                           true);
127     expr_symtab_add_field(&symtab, "icmp6.code", MFF_ICMPV6_CODE, "icmp6",
128                           true);
129
130     expr_symtab_add_predicate(&symtab, "icmp", "icmp4 || icmp6");
131
132     expr_symtab_add_field(&symtab, "ip.frag", MFF_IP_FRAG, "ip", false);
133     expr_symtab_add_predicate(&symtab, "ip.is_frag", "ip.frag[0]");
134     expr_symtab_add_predicate(&symtab, "ip.later_frag", "ip.frag[1]");
135     expr_symtab_add_predicate(&symtab, "ip.first_frag",
136                               "ip.is_frag && !ip.later_frag");
137
138     expr_symtab_add_predicate(&symtab, "arp", "eth.type == 0x806");
139     expr_symtab_add_field(&symtab, "arp.op", MFF_ARP_OP, "arp", false);
140     expr_symtab_add_field(&symtab, "arp.spa", MFF_ARP_SPA, "arp", false);
141     expr_symtab_add_field(&symtab, "arp.sha", MFF_ARP_SHA, "arp", false);
142     expr_symtab_add_field(&symtab, "arp.tpa", MFF_ARP_TPA, "arp", false);
143     expr_symtab_add_field(&symtab, "arp.tha", MFF_ARP_THA, "arp", false);
144
145     expr_symtab_add_predicate(&symtab, "nd",
146                               "icmp6.type == {135, 136} && icmp6.code == 0");
147     expr_symtab_add_field(&symtab, "nd.target", MFF_ND_TARGET, "nd", false);
148     expr_symtab_add_field(&symtab, "nd.sll", MFF_ND_SLL,
149               "nd && icmp6.type == 135", false);
150     expr_symtab_add_field(&symtab, "nd.tll", MFF_ND_TLL,
151               "nd && icmp6.type == 136", false);
152
153     expr_symtab_add_predicate(&symtab, "tcp", "ip.proto == 6");
154     expr_symtab_add_field(&symtab, "tcp.src", MFF_TCP_SRC, "tcp", false);
155     expr_symtab_add_field(&symtab, "tcp.dst", MFF_TCP_DST, "tcp", false);
156     expr_symtab_add_field(&symtab, "tcp.flags", MFF_TCP_FLAGS, "tcp", false);
157
158     expr_symtab_add_predicate(&symtab, "udp", "ip.proto == 17");
159     expr_symtab_add_field(&symtab, "udp.src", MFF_UDP_SRC, "udp", false);
160     expr_symtab_add_field(&symtab, "udp.dst", MFF_UDP_DST, "udp", false);
161
162     expr_symtab_add_predicate(&symtab, "sctp", "ip.proto == 132");
163     expr_symtab_add_field(&symtab, "sctp.src", MFF_SCTP_SRC, "sctp", false);
164     expr_symtab_add_field(&symtab, "sctp.dst", MFF_SCTP_DST, "sctp", false);
165 }
166
167 /* Details of an address set currently in address_sets. We keep a cached
168  * copy of sets still in their string form here to make it easier to compare
169  * with the current values in the OVN_Southbound database. */
170 struct address_set {
171     char **addresses;
172     size_t n_addresses;
173 };
174
175 /* struct address_set instances for address sets currently in the symtab,
176  * hashed on the address set name. */
177 static struct shash local_address_sets = SHASH_INITIALIZER(&local_address_sets);
178
179 static int
180 addr_cmp(const void *p1, const void *p2)
181 {
182     const char *s1 = p1;
183     const char *s2 = p2;
184     return strcmp(s1, s2);
185 }
186
187 /* Return true if the address sets match, false otherwise. */
188 static bool
189 address_sets_match(const struct address_set *addr_set,
190                    const struct sbrec_address_set *addr_set_rec)
191 {
192     char **addrs1;
193     char **addrs2;
194
195     if (addr_set->n_addresses != addr_set_rec->n_addresses) {
196         return false;
197     }
198     size_t n_addresses = addr_set->n_addresses;
199
200     addrs1 = xmemdup(addr_set->addresses,
201                      n_addresses * sizeof addr_set->addresses[0]);
202     addrs2 = xmemdup(addr_set_rec->addresses,
203                      n_addresses * sizeof addr_set_rec->addresses[0]);
204
205     qsort(addrs1, n_addresses, sizeof *addrs1, addr_cmp);
206     qsort(addrs2, n_addresses, sizeof *addrs2, addr_cmp);
207
208     bool res = true;
209     size_t i;
210     for (i = 0; i <  n_addresses; i++) {
211         if (strcmp(addrs1[i], addrs2[i])) {
212             res = false;
213             break;
214         }
215     }
216
217     free(addrs1);
218     free(addrs2);
219
220     return res;
221 }
222
223 static void
224 address_set_destroy(struct address_set *addr_set)
225 {
226     size_t i;
227     for (i = 0; i < addr_set->n_addresses; i++) {
228         free(addr_set->addresses[i]);
229     }
230     if (addr_set->n_addresses) {
231         free(addr_set->addresses);
232     }
233     free(addr_set);
234 }
235
236 static void
237 update_address_sets(struct controller_ctx *ctx)
238 {
239     /* Remember the names of all address sets currently in expr_address_sets
240      * so we can detect address sets that have been deleted. */
241     struct sset cur_addr_set_names = SSET_INITIALIZER(&cur_addr_set_names);
242
243     struct shash_node *node;
244     SHASH_FOR_EACH (node, &local_address_sets) {
245         sset_add(&cur_addr_set_names, node->name);
246     }
247
248     /* Iterate address sets in the southbound database.  Create and update the
249      * corresponding symtab entries as necessary. */
250     const struct sbrec_address_set *addr_set_rec;
251     SBREC_ADDRESS_SET_FOR_EACH (addr_set_rec, ctx->ovnsb_idl) {
252         struct address_set *addr_set =
253             shash_find_data(&local_address_sets, addr_set_rec->name);
254
255         bool create_set = false;
256         if (addr_set) {
257             /* This address set has already been added.  We must determine
258              * if the symtab entry needs to be updated due to a change. */
259             sset_find_and_delete(&cur_addr_set_names, addr_set_rec->name);
260             if (!address_sets_match(addr_set, addr_set_rec)) {
261                 expr_macros_remove(&expr_address_sets, addr_set_rec->name);
262                 address_set_destroy(addr_set);
263                 addr_set = NULL;
264                 create_set = true;
265             }
266         } else {
267             /* This address set is not yet in the symtab, so add it. */
268             create_set = true;
269         }
270
271         if (create_set) {
272             /* The address set is either new or has changed.  Create a symbol
273              * that resolves to the full set of addresses.  Store it in
274              * address_sets to remember that we created this symbol. */
275             addr_set = xzalloc(sizeof *addr_set);
276             addr_set->n_addresses = addr_set_rec->n_addresses;
277             if (addr_set_rec->n_addresses) {
278                 addr_set->addresses = xmalloc(addr_set_rec->n_addresses
279                                               * sizeof addr_set->addresses[0]);
280                 size_t i;
281                 for (i = 0; i < addr_set_rec->n_addresses; i++) {
282                     addr_set->addresses[i] = xstrdup(addr_set_rec->addresses[i]);
283                 }
284             }
285             shash_add(&local_address_sets, addr_set_rec->name, addr_set);
286
287             expr_macros_add(&expr_address_sets, addr_set_rec->name,
288                             (const char * const *) addr_set->addresses,
289                             addr_set->n_addresses);
290         }
291     }
292
293     /* Anything remaining in cur_addr_set_names refers to an address set that
294      * has been deleted from the southbound database.  We should delete
295      * the corresponding symtab entry. */
296     const char *cur_node, *next_node;
297     SSET_FOR_EACH_SAFE (cur_node, next_node, &cur_addr_set_names) {
298         expr_macros_remove(&expr_address_sets, cur_node);
299
300         struct address_set *addr_set
301             = shash_find_and_delete(&local_address_sets, cur_node);
302         address_set_destroy(addr_set);
303
304         struct sset_node *sset_node = SSET_NODE_FROM_NAME(cur_node);
305         sset_delete(&cur_addr_set_names, sset_node);
306     }
307
308     sset_destroy(&cur_addr_set_names);
309 }
310 \f
311 struct lookup_port_aux {
312     const struct lport_index *lports;
313     const struct mcgroup_index *mcgroups;
314     const struct sbrec_datapath_binding *dp;
315 };
316
317 static void consider_logical_flow(const struct lport_index *lports,
318                                   const struct mcgroup_index *mcgroups,
319                                   const struct sbrec_logical_flow *lflow,
320                                   const struct hmap *local_datapaths,
321                                   const struct hmap *patched_datapaths,
322                                   struct group_table *group_table,
323                                   const struct simap *ct_zones,
324                                   struct hmap *dhcp_opts_p,
325                                   uint32_t *conj_id_ofs_p,
326                                   struct hmap *flow_table);
327
328 static bool
329 lookup_port_cb(const void *aux_, const char *port_name, unsigned int *portp)
330 {
331     const struct lookup_port_aux *aux = aux_;
332
333     const struct sbrec_port_binding *pb
334         = lport_lookup_by_name(aux->lports, port_name);
335     if (pb && pb->datapath == aux->dp) {
336         *portp = pb->tunnel_key;
337         return true;
338     }
339
340     const struct sbrec_multicast_group *mg
341         = mcgroup_lookup_by_dp_name(aux->mcgroups, aux->dp, port_name);
342     if (mg) {
343         *portp = mg->tunnel_key;
344         return true;
345     }
346
347     return false;
348 }
349
350 static bool
351 is_switch(const struct sbrec_datapath_binding *ldp)
352 {
353     return smap_get(&ldp->external_ids, "logical-switch") != NULL;
354
355 }
356
357 /* Adds the logical flows from the Logical_Flow table to 'flow_table'. */
358 static void
359 add_logical_flows(struct controller_ctx *ctx, const struct lport_index *lports,
360                   const struct mcgroup_index *mcgroups,
361                   const struct hmap *local_datapaths,
362                   const struct hmap *patched_datapaths,
363                   struct group_table *group_table,
364                   const struct simap *ct_zones, struct hmap *flow_table)
365 {
366     uint32_t conj_id_ofs = 1;
367
368     struct hmap dhcp_opts = HMAP_INITIALIZER(&dhcp_opts);
369     const struct sbrec_dhcp_options *dhcp_opt_row;
370     SBREC_DHCP_OPTIONS_FOR_EACH(dhcp_opt_row, ctx->ovnsb_idl) {
371         dhcp_opt_add(&dhcp_opts, dhcp_opt_row->name, dhcp_opt_row->code,
372                      dhcp_opt_row->type);
373     }
374
375     const struct sbrec_logical_flow *lflow;
376     SBREC_LOGICAL_FLOW_FOR_EACH (lflow, ctx->ovnsb_idl) {
377         consider_logical_flow(lports, mcgroups, lflow, local_datapaths,
378                               patched_datapaths, group_table, ct_zones,
379                               &dhcp_opts, &conj_id_ofs, flow_table);
380     }
381
382     dhcp_opts_destroy(&dhcp_opts);
383 }
384
385 static void
386 consider_logical_flow(const struct lport_index *lports,
387                       const struct mcgroup_index *mcgroups,
388                       const struct sbrec_logical_flow *lflow,
389                       const struct hmap *local_datapaths,
390                       const struct hmap *patched_datapaths,
391                       struct group_table *group_table,
392                       const struct simap *ct_zones,
393                       struct hmap *dhcp_opts_p,
394                       uint32_t *conj_id_ofs_p,
395                       struct hmap *flow_table)
396 {
397     /* Determine translation of logical table IDs to physical table IDs. */
398     bool ingress = !strcmp(lflow->pipeline, "ingress");
399
400     const struct sbrec_datapath_binding *ldp = lflow->logical_datapath;
401     if (!ldp) {
402         return;
403     }
404     if (is_switch(ldp)) {
405         /* For a logical switch datapath, local_datapaths tells us if there
406          * are any local ports for this datapath.  If not, we can skip
407          * processing logical flows if that logical switch datapath is not
408          * patched to any logical router.
409          *
410          * Otherwise, we still need both ingress and egress pipeline
411          * because even if there are no local ports, we still may need to
412          * execute the ingress pipeline after a packet leaves a logical
413          * router and we need to do egress pipeline for a switch that
414          * is connected to only routers.  Further optimization is possible,
415          * but not based on what we know with local_datapaths right now.
416          *
417          * A better approach would be a kind of "flood fill" algorithm:
418          *
419          *   1. Initialize set S to the logical datapaths that have a port
420          *      located on the hypervisor.
421          *
422          *   2. For each patch port P in a logical datapath in S, add the
423          *      logical datapath of the remote end of P to S.  Iterate
424          *      until S reaches a fixed point.
425          *
426          * This can be implemented in northd, which can generate the sets and
427          * save it on each port-binding record in SB, and ovn-controller can
428          * use the information directly. However, there can be update storms
429          * when a pair of patch ports are added/removed to connect/disconnect
430          * large lrouters and lswitches. This need to be studied further.
431          */
432
433         if (!get_local_datapath(local_datapaths, ldp->tunnel_key)) {
434             if (!get_patched_datapath(patched_datapaths,
435                                       ldp->tunnel_key)) {
436                 return;
437             }
438         }
439     }
440
441     /* Determine translation of logical table IDs to physical table IDs. */
442     uint8_t first_ptable = (ingress
443                             ? OFTABLE_LOG_INGRESS_PIPELINE
444                             : OFTABLE_LOG_EGRESS_PIPELINE);
445     uint8_t ptable = first_ptable + lflow->table_id;
446     uint8_t output_ptable = (ingress
447                              ? OFTABLE_REMOTE_OUTPUT
448                              : OFTABLE_LOG_TO_PHY);
449
450     /* Translate OVN actions into OpenFlow actions.
451      *
452      * XXX Deny changes to 'outport' in egress pipeline. */
453     uint64_t ofpacts_stub[64 / 8];
454     struct ofpbuf ofpacts;
455     struct expr *prereqs;
456     char *error;
457
458     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
459     struct lookup_port_aux aux = {
460         .lports = lports,
461         .mcgroups = mcgroups,
462         .dp = lflow->logical_datapath
463     };
464     struct action_params ap = {
465         .symtab = &symtab,
466         .dhcp_opts = dhcp_opts_p,
467         .lookup_port = lookup_port_cb,
468         .aux = &aux,
469         .ct_zones = ct_zones,
470         .group_table = group_table,
471
472         .n_tables = LOG_PIPELINE_LEN,
473         .first_ptable = first_ptable,
474         .cur_ltable = lflow->table_id,
475         .output_ptable = output_ptable,
476         .arp_ptable = OFTABLE_MAC_BINDING,
477     };
478     error = actions_parse_string(lflow->actions, &ap, &ofpacts, &prereqs);
479     if (error) {
480         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
481         VLOG_WARN_RL(&rl, "error parsing actions \"%s\": %s",
482                      lflow->actions, error);
483         free(error);
484         return;
485     }
486
487     /* Translate OVN match into table of OpenFlow matches. */
488     struct hmap matches;
489     struct expr *expr;
490
491     expr = expr_parse_string(lflow->match, &symtab,
492                              &expr_address_sets, &error);
493     if (!error) {
494         if (prereqs) {
495             expr = expr_combine(EXPR_T_AND, expr, prereqs);
496             prereqs = NULL;
497         }
498         expr = expr_annotate(expr, &symtab, &error);
499     }
500     if (error) {
501         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
502         VLOG_WARN_RL(&rl, "error parsing match \"%s\": %s",
503                      lflow->match, error);
504         expr_destroy(prereqs);
505         ofpbuf_uninit(&ofpacts);
506         free(error);
507         return;
508     }
509
510     expr = expr_simplify(expr);
511     expr = expr_normalize(expr);
512     uint32_t n_conjs = expr_to_matches(expr, lookup_port_cb, &aux,
513                                        &matches);
514     expr_destroy(expr);
515
516     /* Prepare the OpenFlow matches for adding to the flow table. */
517     struct expr_match *m;
518     HMAP_FOR_EACH (m, hmap_node, &matches) {
519         match_set_metadata(&m->match,
520                            htonll(lflow->logical_datapath->tunnel_key));
521         if (m->match.wc.masks.conj_id) {
522             m->match.flow.conj_id += *conj_id_ofs_p;
523         }
524         if (!m->n) {
525             ofctrl_add_flow(flow_table, ptable, lflow->priority,
526                             &m->match, &ofpacts);
527         } else {
528             uint64_t conj_stubs[64 / 8];
529             struct ofpbuf conj;
530
531             ofpbuf_use_stub(&conj, conj_stubs, sizeof conj_stubs);
532             for (int i = 0; i < m->n; i++) {
533                 const struct cls_conjunction *src = &m->conjunctions[i];
534                 struct ofpact_conjunction *dst;
535
536                 dst = ofpact_put_CONJUNCTION(&conj);
537                 dst->id = src->id + *conj_id_ofs_p;
538                 dst->clause = src->clause;
539                 dst->n_clauses = src->n_clauses;
540             }
541             ofctrl_add_flow(flow_table, ptable, lflow->priority,
542                             &m->match, &conj);
543             ofpbuf_uninit(&conj);
544         }
545     }
546
547     /* Clean up. */
548     expr_matches_destroy(&matches);
549     ofpbuf_uninit(&ofpacts);
550     *conj_id_ofs_p += n_conjs;
551 }
552
553 static void
554 put_load(const uint8_t *data, size_t len,
555          enum mf_field_id dst, int ofs, int n_bits,
556          struct ofpbuf *ofpacts)
557 {
558     struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
559     sf->field = mf_from_id(dst);
560     sf->flow_has_vlan = false;
561
562     bitwise_copy(data, len, 0, &sf->value, sf->field->n_bytes, ofs, n_bits);
563     bitwise_one(&sf->mask, sf->field->n_bytes, ofs, n_bits);
564 }
565
566 static void
567 consider_neighbor_flow(struct hmap *flow_table,
568                        const struct lport_index *lports,
569                        const struct sbrec_mac_binding *b,
570                        struct ofpbuf *ofpacts_p,
571                        struct match *match_p)
572 {
573     const struct sbrec_port_binding *pb
574         = lport_lookup_by_name(lports, b->logical_port);
575     if (!pb) {
576         return;
577     }
578
579     struct eth_addr mac;
580     if (!eth_addr_from_string(b->mac, &mac)) {
581         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
582         VLOG_WARN_RL(&rl, "bad 'mac' %s", b->mac);
583         return;
584     }
585
586     ovs_be32 ip;
587     if (!ip_parse(b->ip, &ip)) {
588         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
589         VLOG_WARN_RL(&rl, "bad 'ip' %s", b->ip);
590         return;
591     }
592
593     match_set_metadata(match_p, htonll(pb->datapath->tunnel_key));
594     match_set_reg(match_p, MFF_LOG_OUTPORT - MFF_REG0, pb->tunnel_key);
595     match_set_reg(match_p, 0, ntohl(ip));
596
597     ofpbuf_clear(ofpacts_p);
598     put_load(mac.ea, sizeof mac.ea, MFF_ETH_DST, 0, 48, ofpacts_p);
599
600     ofctrl_add_flow(flow_table, OFTABLE_MAC_BINDING, 100, match_p, ofpacts_p);
601 }
602
603 /* Adds an OpenFlow flow to 'flow_table' for each MAC binding in the OVN
604  * southbound database, using 'lports' to resolve logical port names to
605  * numbers. */
606 static void
607 add_neighbor_flows(struct controller_ctx *ctx,
608                    const struct lport_index *lports, struct hmap *flow_table)
609 {
610     struct ofpbuf ofpacts;
611     struct match match;
612     match_init_catchall(&match);
613     ofpbuf_init(&ofpacts, 0);
614
615     const struct sbrec_mac_binding *b;
616     SBREC_MAC_BINDING_FOR_EACH (b, ctx->ovnsb_idl) {
617         consider_neighbor_flow(flow_table, lports, b, &ofpacts, &match);
618     }
619     ofpbuf_uninit(&ofpacts);
620 }
621 \f
622 /* Translates logical flows in the Logical_Flow table in the OVN_SB database
623  * into OpenFlow flows.  See ovn-architecture(7) for more information. */
624 void
625 lflow_run(struct controller_ctx *ctx, const struct lport_index *lports,
626           const struct mcgroup_index *mcgroups,
627           const struct hmap *local_datapaths,
628           const struct hmap *patched_datapaths,
629           struct group_table *group_table,
630           const struct simap *ct_zones, struct hmap *flow_table)
631 {
632     update_address_sets(ctx);
633     add_logical_flows(ctx, lports, mcgroups, local_datapaths,
634                       patched_datapaths, group_table, ct_zones, flow_table);
635     add_neighbor_flows(ctx, lports, flow_table);
636 }
637
638 void
639 lflow_destroy(void)
640 {
641     expr_symtab_destroy(&symtab);
642     shash_destroy(&symtab);
643     expr_macros_destroy(&expr_address_sets);
644     shash_destroy(&expr_address_sets);
645 }