ovn-controller: Add missing shash_destroy().
[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 "openvswitch/dynamic-string.h"
20 #include "ofctrl.h"
21 #include "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-sb-idl.h"
28 #include "packets.h"
29 #include "simap.h"
30
31 VLOG_DEFINE_THIS_MODULE(lflow);
32 \f
33 /* Symbol table. */
34
35 /* Contains "struct expr_symbol"s for fields supported by OVN lflows. */
36 static struct shash symtab;
37
38 static void
39 add_logical_register(struct shash *symtab, enum mf_field_id id)
40 {
41     char name[8];
42
43     snprintf(name, sizeof name, "reg%d", id - MFF_REG0);
44     expr_symtab_add_field(symtab, name, id, NULL, false);
45 }
46
47 void
48 lflow_init(void)
49 {
50     shash_init(&symtab);
51
52     /* Reserve a pair of registers for the logical inport and outport.  A full
53      * 32-bit register each is bigger than we need, but the expression code
54      * doesn't yet support string fields that occupy less than a full OXM. */
55     expr_symtab_add_string(&symtab, "inport", MFF_LOG_INPORT, NULL);
56     expr_symtab_add_string(&symtab, "outport", MFF_LOG_OUTPORT, NULL);
57
58     /* Logical registers. */
59 #define MFF_LOG_REG(ID) add_logical_register(&symtab, ID);
60     MFF_LOG_REGS;
61 #undef MFF_LOG_REG
62
63     /* Connection tracking state. */
64     expr_symtab_add_field(&symtab, "ct_mark", MFF_CT_MARK, NULL, false);
65     expr_symtab_add_field(&symtab, "ct_label", MFF_CT_LABEL, NULL, false);
66     expr_symtab_add_field(&symtab, "ct_state", MFF_CT_STATE, NULL, false);
67     char ct_state_str[16];
68     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_TRACKED_BIT);
69     expr_symtab_add_predicate(&symtab, "ct.trk", ct_state_str);
70     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_NEW_BIT);
71     expr_symtab_add_subfield(&symtab, "ct.new", "ct.trk", ct_state_str);
72     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_ESTABLISHED_BIT);
73     expr_symtab_add_subfield(&symtab, "ct.est", "ct.trk", ct_state_str);
74     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_RELATED_BIT);
75     expr_symtab_add_subfield(&symtab, "ct.rel", "ct.trk", ct_state_str);
76     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_REPLY_DIR_BIT);
77     expr_symtab_add_subfield(&symtab, "ct.rpl", "ct.trk", ct_state_str);
78     snprintf(ct_state_str, sizeof ct_state_str, "ct_state[%d]", CS_INVALID_BIT);
79     expr_symtab_add_subfield(&symtab, "ct.inv", "ct.trk", ct_state_str);
80
81     /* Data fields. */
82     expr_symtab_add_field(&symtab, "eth.src", MFF_ETH_SRC, NULL, false);
83     expr_symtab_add_field(&symtab, "eth.dst", MFF_ETH_DST, NULL, false);
84     expr_symtab_add_field(&symtab, "eth.type", MFF_ETH_TYPE, NULL, true);
85     expr_symtab_add_predicate(&symtab, "eth.bcast",
86                               "eth.dst == ff:ff:ff:ff:ff:ff");
87     expr_symtab_add_subfield(&symtab, "eth.mcast", NULL, "eth.dst[40]");
88
89     expr_symtab_add_field(&symtab, "vlan.tci", MFF_VLAN_TCI, NULL, false);
90     expr_symtab_add_predicate(&symtab, "vlan.present", "vlan.tci[12]");
91     expr_symtab_add_subfield(&symtab, "vlan.pcp", "vlan.present",
92                              "vlan.tci[13..15]");
93     expr_symtab_add_subfield(&symtab, "vlan.vid", "vlan.present",
94                              "vlan.tci[0..11]");
95
96     expr_symtab_add_predicate(&symtab, "ip4", "eth.type == 0x800");
97     expr_symtab_add_predicate(&symtab, "ip6", "eth.type == 0x86dd");
98     expr_symtab_add_predicate(&symtab, "ip", "ip4 || ip6");
99     expr_symtab_add_field(&symtab, "ip.proto", MFF_IP_PROTO, "ip", true);
100     expr_symtab_add_field(&symtab, "ip.dscp", MFF_IP_DSCP, "ip", false);
101     expr_symtab_add_field(&symtab, "ip.ecn", MFF_IP_ECN, "ip", false);
102     expr_symtab_add_field(&symtab, "ip.ttl", MFF_IP_TTL, "ip", false);
103
104     expr_symtab_add_field(&symtab, "ip4.src", MFF_IPV4_SRC, "ip4", false);
105     expr_symtab_add_field(&symtab, "ip4.dst", MFF_IPV4_DST, "ip4", false);
106     expr_symtab_add_predicate(&symtab, "ip4.mcast", "ip4.dst[28..31] == 0xe");
107
108     expr_symtab_add_predicate(&symtab, "icmp4", "ip4 && ip.proto == 1");
109     expr_symtab_add_field(&symtab, "icmp4.type", MFF_ICMPV4_TYPE, "icmp4",
110               false);
111     expr_symtab_add_field(&symtab, "icmp4.code", MFF_ICMPV4_CODE, "icmp4",
112               false);
113
114     expr_symtab_add_field(&symtab, "ip6.src", MFF_IPV6_SRC, "ip6", false);
115     expr_symtab_add_field(&symtab, "ip6.dst", MFF_IPV6_DST, "ip6", false);
116     expr_symtab_add_field(&symtab, "ip6.label", MFF_IPV6_LABEL, "ip6", false);
117
118     expr_symtab_add_predicate(&symtab, "icmp6", "ip6 && ip.proto == 58");
119     expr_symtab_add_field(&symtab, "icmp6.type", MFF_ICMPV6_TYPE, "icmp6",
120                           true);
121     expr_symtab_add_field(&symtab, "icmp6.code", MFF_ICMPV6_CODE, "icmp6",
122                           true);
123
124     expr_symtab_add_predicate(&symtab, "icmp", "icmp4 || icmp6");
125
126     expr_symtab_add_field(&symtab, "ip.frag", MFF_IP_FRAG, "ip", false);
127     expr_symtab_add_predicate(&symtab, "ip.is_frag", "ip.frag[0]");
128     expr_symtab_add_predicate(&symtab, "ip.later_frag", "ip.frag[1]");
129     expr_symtab_add_predicate(&symtab, "ip.first_frag",
130                               "ip.is_frag && !ip.later_frag");
131
132     expr_symtab_add_predicate(&symtab, "arp", "eth.type == 0x806");
133     expr_symtab_add_field(&symtab, "arp.op", MFF_ARP_OP, "arp", false);
134     expr_symtab_add_field(&symtab, "arp.spa", MFF_ARP_SPA, "arp", false);
135     expr_symtab_add_field(&symtab, "arp.sha", MFF_ARP_SHA, "arp", false);
136     expr_symtab_add_field(&symtab, "arp.tpa", MFF_ARP_TPA, "arp", false);
137     expr_symtab_add_field(&symtab, "arp.tha", MFF_ARP_THA, "arp", false);
138
139     expr_symtab_add_predicate(&symtab, "nd",
140                               "icmp6.type == {135, 136} && icmp6.code == 0");
141     expr_symtab_add_field(&symtab, "nd.target", MFF_ND_TARGET, "nd", false);
142     expr_symtab_add_field(&symtab, "nd.sll", MFF_ND_SLL,
143               "nd && icmp6.type == 135", false);
144     expr_symtab_add_field(&symtab, "nd.tll", MFF_ND_TLL,
145               "nd && icmp6.type == 136", false);
146
147     expr_symtab_add_predicate(&symtab, "tcp", "ip.proto == 6");
148     expr_symtab_add_field(&symtab, "tcp.src", MFF_TCP_SRC, "tcp", false);
149     expr_symtab_add_field(&symtab, "tcp.dst", MFF_TCP_DST, "tcp", false);
150     expr_symtab_add_field(&symtab, "tcp.flags", MFF_TCP_FLAGS, "tcp", false);
151
152     expr_symtab_add_predicate(&symtab, "udp", "ip.proto == 17");
153     expr_symtab_add_field(&symtab, "udp.src", MFF_UDP_SRC, "udp", false);
154     expr_symtab_add_field(&symtab, "udp.dst", MFF_UDP_DST, "udp", false);
155
156     expr_symtab_add_predicate(&symtab, "sctp", "ip.proto == 132");
157     expr_symtab_add_field(&symtab, "sctp.src", MFF_SCTP_SRC, "sctp", false);
158     expr_symtab_add_field(&symtab, "sctp.dst", MFF_SCTP_DST, "sctp", false);
159 }
160 \f
161 struct lookup_port_aux {
162     const struct lport_index *lports;
163     const struct mcgroup_index *mcgroups;
164     const struct sbrec_datapath_binding *dp;
165 };
166
167 static bool
168 lookup_port_cb(const void *aux_, const char *port_name, unsigned int *portp)
169 {
170     const struct lookup_port_aux *aux = aux_;
171
172     const struct sbrec_port_binding *pb
173         = lport_lookup_by_name(aux->lports, port_name);
174     if (pb && pb->datapath == aux->dp) {
175         *portp = pb->tunnel_key;
176         return true;
177     }
178
179     const struct sbrec_multicast_group *mg
180         = mcgroup_lookup_by_dp_name(aux->mcgroups, aux->dp, port_name);
181     if (mg) {
182         *portp = mg->tunnel_key;
183         return true;
184     }
185
186     return false;
187 }
188
189 static bool
190 is_switch(const struct sbrec_datapath_binding *ldp)
191 {
192     return smap_get(&ldp->external_ids, "logical-switch") != NULL;
193
194 }
195
196 /* Adds the logical flows from the Logical_Flow table to 'flow_table'. */
197 static void
198 add_logical_flows(struct controller_ctx *ctx, const struct lport_index *lports,
199                   const struct mcgroup_index *mcgroups,
200                   const struct hmap *local_datapaths,
201                   const struct hmap *patched_datapaths,
202                   const struct simap *ct_zones, struct hmap *flow_table)
203 {
204     uint32_t conj_id_ofs = 1;
205
206     const struct sbrec_logical_flow *lflow;
207     SBREC_LOGICAL_FLOW_FOR_EACH (lflow, ctx->ovnsb_idl) {
208         /* Determine translation of logical table IDs to physical table IDs. */
209         bool ingress = !strcmp(lflow->pipeline, "ingress");
210
211         const struct sbrec_datapath_binding *ldp = lflow->logical_datapath;
212         if (!ldp) {
213             continue;
214         }
215         if (is_switch(ldp)) {
216             /* For a logical switch datapath, local_datapaths tells us if there
217              * are any local ports for this datapath.  If not, we can skip
218              * processing logical flows if the flow belongs to egress pipeline
219              * or if that logical switch datapath is not patched to any logical
220              * router.
221              *
222              * Otherwise, we still need the ingress pipeline because even if
223              * there are no local ports, we still may need to execute the ingress
224              * pipeline after a packet leaves a logical router.  Further
225              * optimization is possible, but not based on what we know with
226              * local_datapaths right now.
227              *
228              * A better approach would be a kind of "flood fill" algorithm:
229              *
230              *   1. Initialize set S to the logical datapaths that have a port
231              *      located on the hypervisor.
232              *
233              *   2. For each patch port P in a logical datapath in S, add the
234              *      logical datapath of the remote end of P to S.  Iterate
235              *      until S reaches a fixed point.
236              *
237              * This can be implemented in northd, which can generate the sets and
238              * save it on each port-binding record in SB, and ovn-controller can
239              * use the information directly. However, there can be update storms
240              * when a pair of patch ports are added/removed to connect/disconnect
241              * large lrouters and lswitches. This need to be studied further.
242              */
243
244             struct hmap_node *ld;
245             ld = hmap_first_with_hash(local_datapaths, ldp->tunnel_key);
246             if (!ld) {
247                 if (!ingress) {
248                     continue;
249                 }
250                 struct hmap_node *pd;
251                 pd = hmap_first_with_hash(patched_datapaths, ldp->tunnel_key);
252                 if (!pd) {
253                     continue;
254                 }
255             }
256         }
257
258         /* Determine translation of logical table IDs to physical table IDs. */
259         uint8_t first_ptable = (ingress
260                                 ? OFTABLE_LOG_INGRESS_PIPELINE
261                                 : OFTABLE_LOG_EGRESS_PIPELINE);
262         uint8_t ptable = first_ptable + lflow->table_id;
263         uint8_t output_ptable = (ingress
264                                  ? OFTABLE_REMOTE_OUTPUT
265                                  : OFTABLE_LOG_TO_PHY);
266
267         /* Translate OVN actions into OpenFlow actions.
268          *
269          * XXX Deny changes to 'outport' in egress pipeline. */
270         uint64_t ofpacts_stub[64 / 8];
271         struct ofpbuf ofpacts;
272         struct expr *prereqs;
273         char *error;
274
275         ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
276         struct lookup_port_aux aux = {
277             .lports = lports,
278             .mcgroups = mcgroups,
279             .dp = lflow->logical_datapath
280         };
281         struct action_params ap = {
282             .symtab = &symtab,
283             .lookup_port = lookup_port_cb,
284             .aux = &aux,
285             .ct_zones = ct_zones,
286
287             .n_tables = LOG_PIPELINE_LEN,
288             .first_ptable = first_ptable,
289             .cur_ltable = lflow->table_id,
290             .output_ptable = output_ptable,
291             .arp_ptable = OFTABLE_MAC_BINDING,
292         };
293         error = actions_parse_string(lflow->actions, &ap, &ofpacts, &prereqs);
294         if (error) {
295             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
296             VLOG_WARN_RL(&rl, "error parsing actions \"%s\": %s",
297                          lflow->actions, error);
298             free(error);
299             continue;
300         }
301
302         /* Translate OVN match into table of OpenFlow matches. */
303         struct hmap matches;
304         struct expr *expr;
305
306         expr = expr_parse_string(lflow->match, &symtab, &error);
307         if (!error) {
308             if (prereqs) {
309                 expr = expr_combine(EXPR_T_AND, expr, prereqs);
310                 prereqs = NULL;
311             }
312             expr = expr_annotate(expr, &symtab, &error);
313         }
314         if (error) {
315             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
316             VLOG_WARN_RL(&rl, "error parsing match \"%s\": %s",
317                          lflow->match, error);
318             expr_destroy(prereqs);
319             ofpbuf_uninit(&ofpacts);
320             free(error);
321             continue;
322         }
323
324         expr = expr_simplify(expr);
325         expr = expr_normalize(expr);
326         uint32_t n_conjs = expr_to_matches(expr, lookup_port_cb, &aux,
327                                            &matches);
328         expr_destroy(expr);
329
330         /* Prepare the OpenFlow matches for adding to the flow table. */
331         struct expr_match *m;
332         HMAP_FOR_EACH (m, hmap_node, &matches) {
333             match_set_metadata(&m->match,
334                                htonll(lflow->logical_datapath->tunnel_key));
335             if (m->match.wc.masks.conj_id) {
336                 m->match.flow.conj_id += conj_id_ofs;
337             }
338             if (!m->n) {
339                 ofctrl_add_flow(flow_table, ptable, lflow->priority,
340                                 &m->match, &ofpacts);
341             } else {
342                 uint64_t conj_stubs[64 / 8];
343                 struct ofpbuf conj;
344
345                 ofpbuf_use_stub(&conj, conj_stubs, sizeof conj_stubs);
346                 for (int i = 0; i < m->n; i++) {
347                     const struct cls_conjunction *src = &m->conjunctions[i];
348                     struct ofpact_conjunction *dst;
349
350                     dst = ofpact_put_CONJUNCTION(&conj);
351                     dst->id = src->id + conj_id_ofs;
352                     dst->clause = src->clause;
353                     dst->n_clauses = src->n_clauses;
354                 }
355                 ofctrl_add_flow(flow_table, ptable, lflow->priority,
356                                 &m->match, &conj);
357                 ofpbuf_uninit(&conj);
358             }
359         }
360
361         /* Clean up. */
362         expr_matches_destroy(&matches);
363         ofpbuf_uninit(&ofpacts);
364         conj_id_ofs += n_conjs;
365     }
366 }
367
368 static void
369 put_load(const uint8_t *data, size_t len,
370          enum mf_field_id dst, int ofs, int n_bits,
371          struct ofpbuf *ofpacts)
372 {
373     struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
374     sf->field = mf_from_id(dst);
375     sf->flow_has_vlan = false;
376
377     bitwise_copy(data, len, 0, &sf->value, sf->field->n_bytes, ofs, n_bits);
378     bitwise_one(&sf->mask, sf->field->n_bytes, ofs, n_bits);
379 }
380
381 /* Adds an OpenFlow flow to 'flow_table' for each MAC binding in the OVN
382  * southbound database, using 'lports' to resolve logical port names to
383  * numbers. */
384 static void
385 add_neighbor_flows(struct controller_ctx *ctx,
386                    const struct lport_index *lports, struct hmap *flow_table)
387 {
388     struct ofpbuf ofpacts;
389     struct match match;
390     match_init_catchall(&match);
391     ofpbuf_init(&ofpacts, 0);
392
393     const struct sbrec_mac_binding *b;
394     SBREC_MAC_BINDING_FOR_EACH (b, ctx->ovnsb_idl) {
395         const struct sbrec_port_binding *pb
396             = lport_lookup_by_name(lports, b->logical_port);
397         if (!pb) {
398             continue;
399         }
400
401         struct eth_addr mac;
402         if (!eth_addr_from_string(b->mac, &mac)) {
403             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
404             VLOG_WARN_RL(&rl, "bad 'mac' %s", b->mac);
405             continue;
406         }
407
408         ovs_be32 ip;
409         if (!ip_parse(b->ip, &ip)) {
410             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
411             VLOG_WARN_RL(&rl, "bad 'ip' %s", b->ip);
412             continue;
413         }
414
415         match_set_metadata(&match, htonll(pb->datapath->tunnel_key));
416         match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, pb->tunnel_key);
417         match_set_reg(&match, 0, ntohl(ip));
418
419         ofpbuf_clear(&ofpacts);
420         put_load(mac.ea, sizeof mac.ea, MFF_ETH_DST, 0, 48, &ofpacts);
421
422         ofctrl_add_flow(flow_table, OFTABLE_MAC_BINDING, 100,
423                         &match, &ofpacts);
424     }
425     ofpbuf_uninit(&ofpacts);
426 }
427 \f
428 /* Translates logical flows in the Logical_Flow table in the OVN_SB database
429  * into OpenFlow flows.  See ovn-architecture(7) for more information. */
430 void
431 lflow_run(struct controller_ctx *ctx, const struct lport_index *lports,
432           const struct mcgroup_index *mcgroups,
433           const struct hmap *local_datapaths,
434           const struct hmap *patched_datapaths,
435           const struct simap *ct_zones, struct hmap *flow_table)
436 {
437     add_logical_flows(ctx, lports, mcgroups, local_datapaths,
438                       patched_datapaths, ct_zones, flow_table);
439     add_neighbor_flows(ctx, lports, flow_table);
440 }
441
442 void
443 lflow_destroy(void)
444 {
445     expr_symtab_destroy(&symtab);
446     shash_destroy(&symtab);
447 }