ovn: Add new predicates for matching broadcast and multicast packets.
[cascardo/ovs.git] / ovn / controller / lflow.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 "lflow.h"
18 #include "dynamic-string.h"
19 #include "ofctrl.h"
20 #include "ofp-actions.h"
21 #include "ofpbuf.h"
22 #include "openvswitch/vlog.h"
23 #include "ovn/controller/ovn-controller.h"
24 #include "ovn/lib/actions.h"
25 #include "ovn/lib/expr.h"
26 #include "ovn/lib/ovn-sb-idl.h"
27 #include "simap.h"
28
29 VLOG_DEFINE_THIS_MODULE(lflow);
30 \f
31 /* Symbol table. */
32
33 /* Contains "struct expr_symbol"s for fields supported by OVN lflows. */
34 static struct shash symtab;
35
36 static void
37 add_logical_register(struct shash *symtab, enum mf_field_id id)
38 {
39     char name[8];
40
41     snprintf(name, sizeof name, "reg%d", id - MFF_REG0);
42     expr_symtab_add_field(symtab, name, id, NULL, false);
43 }
44
45 static void
46 symtab_init(void)
47 {
48     shash_init(&symtab);
49
50     /* Reserve a pair of registers for the logical inport and outport.  A full
51      * 32-bit register each is bigger than we need, but the expression code
52      * doesn't yet support string fields that occupy less than a full OXM. */
53     expr_symtab_add_string(&symtab, "inport", MFF_LOG_INPORT, NULL);
54     expr_symtab_add_string(&symtab, "outport", MFF_LOG_OUTPORT, NULL);
55
56     /* Logical registers. */
57 #define MFF_LOG_REG(ID) add_logical_register(&symtab, ID);
58     MFF_LOG_REGS;
59 #undef MFF_LOG_REG
60
61     /* Connection tracking state. */
62     expr_symtab_add_field(&symtab, "ct_state", MFF_CT_STATE, NULL, false);
63     expr_symtab_add_predicate(&symtab, "ct.trk", "ct_state[7]");
64     expr_symtab_add_subfield(&symtab, "ct.new", "ct.trk", "ct_state[0]");
65     expr_symtab_add_subfield(&symtab, "ct.est", "ct.trk", "ct_state[1]");
66     expr_symtab_add_subfield(&symtab, "ct.rel", "ct.trk", "ct_state[2]");
67     expr_symtab_add_subfield(&symtab, "ct.inv", "ct.trk", "ct_state[5]");
68     expr_symtab_add_subfield(&symtab, "ct.rpl", "ct.trk", "ct_state[6]");
69
70     /* Data fields. */
71     expr_symtab_add_field(&symtab, "eth.src", MFF_ETH_SRC, NULL, false);
72     expr_symtab_add_field(&symtab, "eth.dst", MFF_ETH_DST, NULL, false);
73     expr_symtab_add_field(&symtab, "eth.type", MFF_ETH_TYPE, NULL, true);
74     expr_symtab_add_predicate(&symtab, "eth.bcast",
75                               "eth.dst == ff:ff:ff:ff:ff:ff");
76     expr_symtab_add_subfield(&symtab, "eth.mcast", NULL, "eth.dst[40]");
77
78     expr_symtab_add_field(&symtab, "vlan.tci", MFF_VLAN_TCI, NULL, false);
79     expr_symtab_add_predicate(&symtab, "vlan.present", "vlan.tci[12]");
80     expr_symtab_add_subfield(&symtab, "vlan.pcp", "vlan.present",
81                              "vlan.tci[13..15]");
82     expr_symtab_add_subfield(&symtab, "vlan.vid", "vlan.present",
83                              "vlan.tci[0..11]");
84
85     expr_symtab_add_predicate(&symtab, "ip4", "eth.type == 0x800");
86     expr_symtab_add_predicate(&symtab, "ip6", "eth.type == 0x86dd");
87     expr_symtab_add_predicate(&symtab, "ip", "ip4 || ip6");
88     expr_symtab_add_field(&symtab, "ip.proto", MFF_IP_PROTO, "ip", true);
89     expr_symtab_add_field(&symtab, "ip.dscp", MFF_IP_DSCP, "ip", false);
90     expr_symtab_add_field(&symtab, "ip.ecn", MFF_IP_ECN, "ip", false);
91     expr_symtab_add_field(&symtab, "ip.ttl", MFF_IP_TTL, "ip", false);
92
93     expr_symtab_add_field(&symtab, "ip4.src", MFF_IPV4_SRC, "ip4", false);
94     expr_symtab_add_field(&symtab, "ip4.dst", MFF_IPV4_DST, "ip4", false);
95     expr_symtab_add_predicate(&symtab, "ip4.mcast", "ip4.dst[28..31] == 0xe");
96
97     expr_symtab_add_predicate(&symtab, "icmp4", "ip4 && ip.proto == 1");
98     expr_symtab_add_field(&symtab, "icmp4.type", MFF_ICMPV4_TYPE, "icmp4",
99               false);
100     expr_symtab_add_field(&symtab, "icmp4.code", MFF_ICMPV4_CODE, "icmp4",
101               false);
102
103     expr_symtab_add_field(&symtab, "ip6.src", MFF_IPV6_SRC, "ip6", false);
104     expr_symtab_add_field(&symtab, "ip6.dst", MFF_IPV6_DST, "ip6", false);
105     expr_symtab_add_field(&symtab, "ip6.label", MFF_IPV6_LABEL, "ip6", false);
106
107     expr_symtab_add_predicate(&symtab, "icmp6", "ip6 && ip.proto == 58");
108     expr_symtab_add_field(&symtab, "icmp6.type", MFF_ICMPV6_TYPE, "icmp6",
109                           true);
110     expr_symtab_add_field(&symtab, "icmp6.code", MFF_ICMPV6_CODE, "icmp6",
111                           true);
112
113     expr_symtab_add_predicate(&symtab, "icmp", "icmp4 || icmp6");
114
115     expr_symtab_add_field(&symtab, "ip.frag", MFF_IP_FRAG, "ip", false);
116     expr_symtab_add_predicate(&symtab, "ip.is_frag", "ip.frag[0]");
117     expr_symtab_add_predicate(&symtab, "ip.later_frag", "ip.frag[1]");
118     expr_symtab_add_predicate(&symtab, "ip.first_frag",
119                               "ip.is_frag && !ip.later_frag");
120
121     expr_symtab_add_predicate(&symtab, "arp", "eth.type == 0x806");
122     expr_symtab_add_field(&symtab, "arp.op", MFF_ARP_OP, "arp", false);
123     expr_symtab_add_field(&symtab, "arp.spa", MFF_ARP_SPA, "arp", false);
124     expr_symtab_add_field(&symtab, "arp.sha", MFF_ARP_SHA, "arp", false);
125     expr_symtab_add_field(&symtab, "arp.tpa", MFF_ARP_TPA, "arp", false);
126     expr_symtab_add_field(&symtab, "arp.tha", MFF_ARP_THA, "arp", false);
127
128     expr_symtab_add_predicate(&symtab, "nd",
129                               "icmp6.type == {135, 136} && icmp6.code == 0");
130     expr_symtab_add_field(&symtab, "nd.target", MFF_ND_TARGET, "nd", false);
131     expr_symtab_add_field(&symtab, "nd.sll", MFF_ND_SLL,
132               "nd && icmp6.type == 135", false);
133     expr_symtab_add_field(&symtab, "nd.tll", MFF_ND_TLL,
134               "nd && icmp6.type == 136", false);
135
136     expr_symtab_add_predicate(&symtab, "tcp", "ip.proto == 6");
137     expr_symtab_add_field(&symtab, "tcp.src", MFF_TCP_SRC, "tcp", false);
138     expr_symtab_add_field(&symtab, "tcp.dst", MFF_TCP_DST, "tcp", false);
139     expr_symtab_add_field(&symtab, "tcp.flags", MFF_TCP_FLAGS, "tcp", false);
140
141     expr_symtab_add_predicate(&symtab, "udp", "ip.proto == 17");
142     expr_symtab_add_field(&symtab, "udp.src", MFF_UDP_SRC, "udp", false);
143     expr_symtab_add_field(&symtab, "udp.dst", MFF_UDP_DST, "udp", false);
144
145     expr_symtab_add_predicate(&symtab, "sctp", "ip.proto == 132");
146     expr_symtab_add_field(&symtab, "sctp.src", MFF_SCTP_SRC, "sctp", false);
147     expr_symtab_add_field(&symtab, "sctp.dst", MFF_SCTP_DST, "sctp", false);
148 }
149 \f
150 /* Logical datapaths and logical port numbers. */
151
152 /* A logical datapath.
153  *
154  * 'ports' maps 'logical_port' names to 'tunnel_key' values in the OVN_SB
155  * Port_Binding table within the logical datapath. */
156 struct logical_datapath {
157     struct hmap_node hmap_node; /* Indexed on 'uuid'. */
158     struct uuid uuid;           /* UUID from Datapath_Binding row. */
159     uint32_t tunnel_key;        /* 'tunnel_key' from Datapath_Binding row. */
160     struct simap ports;         /* Logical port name to port number. */
161 };
162
163 /* Contains "struct logical_datapath"s. */
164 static struct hmap logical_datapaths = HMAP_INITIALIZER(&logical_datapaths);
165
166 /* Finds and returns the logical_datapath for 'binding', or NULL if no such
167  * logical_datapath exists. */
168 static struct logical_datapath *
169 ldp_lookup(const struct sbrec_datapath_binding *binding)
170 {
171     struct logical_datapath *ldp;
172     HMAP_FOR_EACH_IN_BUCKET (ldp, hmap_node, uuid_hash(&binding->header_.uuid),
173                              &logical_datapaths) {
174         if (uuid_equals(&ldp->uuid, &binding->header_.uuid)) {
175             return ldp;
176         }
177     }
178     return NULL;
179 }
180
181 /* Creates a new logical_datapath for the given 'binding'. */
182 static struct logical_datapath *
183 ldp_create(const struct sbrec_datapath_binding *binding)
184 {
185     struct logical_datapath *ldp;
186
187     ldp = xmalloc(sizeof *ldp);
188     hmap_insert(&logical_datapaths, &ldp->hmap_node,
189                 uuid_hash(&binding->header_.uuid));
190     ldp->uuid = binding->header_.uuid;
191     ldp->tunnel_key = binding->tunnel_key;
192     simap_init(&ldp->ports);
193     return ldp;
194 }
195
196 static struct logical_datapath *
197 ldp_lookup_or_create(const struct sbrec_datapath_binding *binding)
198 {
199     struct logical_datapath *ldp = ldp_lookup(binding);
200     return ldp ? ldp : ldp_create(binding);
201 }
202
203 static void
204 ldp_free(struct logical_datapath *ldp)
205 {
206     simap_destroy(&ldp->ports);
207     hmap_remove(&logical_datapaths, &ldp->hmap_node);
208     free(ldp);
209 }
210
211 /* Iterates through all of the records in the Port_Binding table, updating the
212  * table of logical_datapaths to match the values found in active
213  * Port_Bindings. */
214 static void
215 ldp_run(struct controller_ctx *ctx)
216 {
217     struct logical_datapath *ldp;
218     HMAP_FOR_EACH (ldp, hmap_node, &logical_datapaths) {
219         simap_clear(&ldp->ports);
220     }
221
222     const struct sbrec_port_binding *binding;
223     SBREC_PORT_BINDING_FOR_EACH (binding, ctx->ovnsb_idl) {
224         struct logical_datapath *ldp = ldp_lookup_or_create(binding->datapath);
225
226         simap_put(&ldp->ports, binding->logical_port, binding->tunnel_key);
227     }
228
229     const struct sbrec_multicast_group *mc;
230     SBREC_MULTICAST_GROUP_FOR_EACH (mc, ctx->ovnsb_idl) {
231         struct logical_datapath *ldp = ldp_lookup_or_create(mc->datapath);
232         simap_put(&ldp->ports, mc->name, mc->tunnel_key);
233     }
234
235     struct logical_datapath *next_ldp;
236     HMAP_FOR_EACH_SAFE (ldp, next_ldp, hmap_node, &logical_datapaths) {
237         if (simap_is_empty(&ldp->ports)) {
238             ldp_free(ldp);
239         }
240     }
241 }
242
243 static void
244 ldp_destroy(void)
245 {
246     struct logical_datapath *ldp, *next_ldp;
247     HMAP_FOR_EACH_SAFE (ldp, next_ldp, hmap_node, &logical_datapaths) {
248         ldp_free(ldp);
249     }
250 }
251 \f
252 void
253 lflow_init(void)
254 {
255     symtab_init();
256 }
257
258 /* Translates logical flows in the Logical_Flow table in the OVN_SB database
259  * into OpenFlow flows.  See ovn-architecture(7) for more information. */
260 void
261 lflow_run(struct controller_ctx *ctx, struct hmap *flow_table,
262           const struct simap *ct_zones)
263 {
264     struct hmap flows = HMAP_INITIALIZER(&flows);
265     uint32_t conj_id_ofs = 1;
266
267     ldp_run(ctx);
268
269     const struct sbrec_logical_flow *lflow;
270     SBREC_LOGICAL_FLOW_FOR_EACH (lflow, ctx->ovnsb_idl) {
271         /* Find the "struct logical_datapath" asssociated with this
272          * Logical_Flow row.  If there's no such struct, that must be because
273          * no logical ports are bound to that logical datapath, so there's no
274          * point in maintaining any flows for it anyway, so skip it. */
275         const struct logical_datapath *ldp;
276         ldp = ldp_lookup(lflow->logical_datapath);
277         if (!ldp) {
278             continue;
279         }
280
281         /* Determine translation of logical table IDs to physical table IDs. */
282         bool ingress = !strcmp(lflow->pipeline, "ingress");
283         uint8_t first_ptable = (ingress
284                                 ? OFTABLE_LOG_INGRESS_PIPELINE
285                                 : OFTABLE_LOG_EGRESS_PIPELINE);
286         uint8_t ptable = first_ptable + lflow->table_id;
287         uint8_t output_ptable = (ingress
288                                  ? OFTABLE_REMOTE_OUTPUT
289                                  : OFTABLE_LOG_TO_PHY);
290
291         /* Translate OVN actions into OpenFlow actions.
292          *
293          * XXX Deny changes to 'outport' in egress pipeline. */
294         uint64_t ofpacts_stub[64 / 8];
295         struct ofpbuf ofpacts;
296         struct expr *prereqs;
297         char *error;
298
299         ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
300         error = actions_parse_string(lflow->actions, &symtab, &ldp->ports,
301                                      ct_zones, first_ptable, LOG_PIPELINE_LEN,
302                                      lflow->table_id, output_ptable,
303                                      &ofpacts, &prereqs);
304         if (error) {
305             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
306             VLOG_WARN_RL(&rl, "error parsing actions \"%s\": %s",
307                          lflow->actions, error);
308             free(error);
309             continue;
310         }
311
312         /* Translate OVN match into table of OpenFlow matches. */
313         struct hmap matches;
314         struct expr *expr;
315
316         expr = expr_parse_string(lflow->match, &symtab, &error);
317         if (!error) {
318             if (prereqs) {
319                 expr = expr_combine(EXPR_T_AND, expr, prereqs);
320                 prereqs = NULL;
321             }
322             expr = expr_annotate(expr, &symtab, &error);
323         }
324         if (error) {
325             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
326             VLOG_WARN_RL(&rl, "error parsing match \"%s\": %s",
327                          lflow->match, error);
328             expr_destroy(prereqs);
329             ofpbuf_uninit(&ofpacts);
330             free(error);
331             continue;
332         }
333
334         expr = expr_simplify(expr);
335         expr = expr_normalize(expr);
336         uint32_t n_conjs = expr_to_matches(expr, &ldp->ports, &matches);
337         expr_destroy(expr);
338
339         /* Prepare the OpenFlow matches for adding to the flow table. */
340         struct expr_match *m;
341         HMAP_FOR_EACH (m, hmap_node, &matches) {
342             match_set_metadata(&m->match, htonll(ldp->tunnel_key));
343             if (m->match.wc.masks.conj_id) {
344                 m->match.flow.conj_id += conj_id_ofs;
345             }
346             if (!m->n) {
347                 ofctrl_add_flow(flow_table, ptable, lflow->priority,
348                                 &m->match, &ofpacts);
349             } else {
350                 uint64_t conj_stubs[64 / 8];
351                 struct ofpbuf conj;
352
353                 ofpbuf_use_stub(&conj, conj_stubs, sizeof conj_stubs);
354                 for (int i = 0; i < m->n; i++) {
355                     const struct cls_conjunction *src = &m->conjunctions[i];
356                     struct ofpact_conjunction *dst;
357
358                     dst = ofpact_put_CONJUNCTION(&conj);
359                     dst->id = src->id + conj_id_ofs;
360                     dst->clause = src->clause;
361                     dst->n_clauses = src->n_clauses;
362                 }
363                 ofctrl_add_flow(flow_table, ptable, lflow->priority,
364                                 &m->match, &conj);
365                 ofpbuf_uninit(&conj);
366             }
367         }
368
369         /* Clean up. */
370         expr_matches_destroy(&matches);
371         ofpbuf_uninit(&ofpacts);
372         conj_id_ofs += n_conjs;
373     }
374 }
375
376 void
377 lflow_destroy(void)
378 {
379     expr_symtab_destroy(&symtab);
380     ldp_destroy();
381 }