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