ovn: Implement basic ARP support for L3 logical routers.
[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 "dynamic-string.h"
20 #include "ofctrl.h"
21 #include "ofp-actions.h"
22 #include "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 simap *ct_zones, struct hmap *flow_table)
202 {
203     uint32_t conj_id_ofs = 1;
204
205     const struct sbrec_logical_flow *lflow;
206     SBREC_LOGICAL_FLOW_FOR_EACH (lflow, ctx->ovnsb_idl) {
207         /* Determine translation of logical table IDs to physical table IDs. */
208         bool ingress = !strcmp(lflow->pipeline, "ingress");
209
210         const struct sbrec_datapath_binding *ldp = lflow->logical_datapath;
211         if (!ldp) {
212             continue;
213         }
214         if (!ingress && is_switch(ldp)) {
215             /* For a logical switch datapath, local_datapaths tells us if there
216              * are any local ports for this datapath.  If not, processing
217              * logical flows for the egress pipeline of this datapath is
218              * unnecessary.
219              *
220              * We still need the ingress pipeline because even if there are no
221              * local ports, we still may need to execute the ingress pipeline
222              * after a packet leaves a logical router.  Further optimization
223              * is possible, but not based on what we know with local_datapaths
224              * right now.
225              *
226              * A better approach would be a kind of "flood fill" algorithm:
227              *
228              *   1. Initialize set S to the logical datapaths that have a port
229              *      located on the hypervisor.
230              *
231              *   2. For each patch port P in a logical datapath in S, add the
232              *      logical datapath of the remote end of P to S.  Iterate
233              *      until S reaches a fixed point.
234              */
235
236             struct hmap_node *ld;
237             ld = hmap_first_with_hash(local_datapaths, ldp->tunnel_key);
238             if (!ld) {
239                 continue;
240             }
241         }
242
243         /* Determine translation of logical table IDs to physical table IDs. */
244         uint8_t first_ptable = (ingress
245                                 ? OFTABLE_LOG_INGRESS_PIPELINE
246                                 : OFTABLE_LOG_EGRESS_PIPELINE);
247         uint8_t ptable = first_ptable + lflow->table_id;
248         uint8_t output_ptable = (ingress
249                                  ? OFTABLE_REMOTE_OUTPUT
250                                  : OFTABLE_LOG_TO_PHY);
251
252         /* Translate OVN actions into OpenFlow actions.
253          *
254          * XXX Deny changes to 'outport' in egress pipeline. */
255         uint64_t ofpacts_stub[64 / 8];
256         struct ofpbuf ofpacts;
257         struct expr *prereqs;
258         char *error;
259
260         ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
261         struct lookup_port_aux aux = {
262             .lports = lports,
263             .mcgroups = mcgroups,
264             .dp = lflow->logical_datapath
265         };
266         struct action_params ap = {
267             .symtab = &symtab,
268             .lookup_port = lookup_port_cb,
269             .aux = &aux,
270             .ct_zones = ct_zones,
271
272             .n_tables = LOG_PIPELINE_LEN,
273             .first_ptable = first_ptable,
274             .cur_ltable = lflow->table_id,
275             .output_ptable = output_ptable,
276             .arp_ptable = OFTABLE_MAC_BINDING,
277         };
278         error = actions_parse_string(lflow->actions, &ap, &ofpacts, &prereqs);
279         if (error) {
280             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
281             VLOG_WARN_RL(&rl, "error parsing actions \"%s\": %s",
282                          lflow->actions, error);
283             free(error);
284             continue;
285         }
286
287         /* Translate OVN match into table of OpenFlow matches. */
288         struct hmap matches;
289         struct expr *expr;
290
291         expr = expr_parse_string(lflow->match, &symtab, &error);
292         if (!error) {
293             if (prereqs) {
294                 expr = expr_combine(EXPR_T_AND, expr, prereqs);
295                 prereqs = NULL;
296             }
297             expr = expr_annotate(expr, &symtab, &error);
298         }
299         if (error) {
300             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
301             VLOG_WARN_RL(&rl, "error parsing match \"%s\": %s",
302                          lflow->match, error);
303             expr_destroy(prereqs);
304             ofpbuf_uninit(&ofpacts);
305             free(error);
306             continue;
307         }
308
309         expr = expr_simplify(expr);
310         expr = expr_normalize(expr);
311         uint32_t n_conjs = expr_to_matches(expr, lookup_port_cb, &aux,
312                                            &matches);
313         expr_destroy(expr);
314
315         /* Prepare the OpenFlow matches for adding to the flow table. */
316         struct expr_match *m;
317         HMAP_FOR_EACH (m, hmap_node, &matches) {
318             match_set_metadata(&m->match,
319                                htonll(lflow->logical_datapath->tunnel_key));
320             if (m->match.wc.masks.conj_id) {
321                 m->match.flow.conj_id += conj_id_ofs;
322             }
323             if (!m->n) {
324                 ofctrl_add_flow(flow_table, ptable, lflow->priority,
325                                 &m->match, &ofpacts);
326             } else {
327                 uint64_t conj_stubs[64 / 8];
328                 struct ofpbuf conj;
329
330                 ofpbuf_use_stub(&conj, conj_stubs, sizeof conj_stubs);
331                 for (int i = 0; i < m->n; i++) {
332                     const struct cls_conjunction *src = &m->conjunctions[i];
333                     struct ofpact_conjunction *dst;
334
335                     dst = ofpact_put_CONJUNCTION(&conj);
336                     dst->id = src->id + conj_id_ofs;
337                     dst->clause = src->clause;
338                     dst->n_clauses = src->n_clauses;
339                 }
340                 ofctrl_add_flow(flow_table, ptable, lflow->priority,
341                                 &m->match, &conj);
342                 ofpbuf_uninit(&conj);
343             }
344         }
345
346         /* Clean up. */
347         expr_matches_destroy(&matches);
348         ofpbuf_uninit(&ofpacts);
349         conj_id_ofs += n_conjs;
350     }
351 }
352
353 static void
354 put_load(const uint8_t *data, size_t len,
355          enum mf_field_id dst, int ofs, int n_bits,
356          struct ofpbuf *ofpacts)
357 {
358     struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
359     sf->field = mf_from_id(dst);
360     sf->flow_has_vlan = false;
361
362     bitwise_copy(data, len, 0, &sf->value, sf->field->n_bytes, ofs, n_bits);
363     bitwise_one(&sf->mask, sf->field->n_bytes, ofs, n_bits);
364 }
365
366 /* Adds an OpenFlow flow to 'flow_table' for each MAC binding in the OVN
367  * southbound database, using 'lports' to resolve logical port names to
368  * numbers. */
369 static void
370 add_neighbor_flows(struct controller_ctx *ctx,
371                    const struct lport_index *lports, struct hmap *flow_table)
372 {
373     struct ofpbuf ofpacts;
374     struct match match;
375     match_init_catchall(&match);
376     ofpbuf_init(&ofpacts, 0);
377
378     const struct sbrec_mac_binding *b;
379     SBREC_MAC_BINDING_FOR_EACH (b, ctx->ovnsb_idl) {
380         const struct sbrec_port_binding *pb
381             = lport_lookup_by_name(lports, b->logical_port);
382         if (!pb) {
383             continue;
384         }
385
386         struct eth_addr mac;
387         if (!eth_addr_from_string(b->mac, &mac)) {
388             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
389             VLOG_WARN_RL(&rl, "bad 'mac' %s", b->mac);
390             continue;
391         }
392
393         ovs_be32 ip;
394         if (!ip_parse(b->ip, &ip)) {
395             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
396             VLOG_WARN_RL(&rl, "bad 'ip' %s", b->ip);
397             continue;
398         }
399
400         match_set_metadata(&match, htonll(pb->datapath->tunnel_key));
401         match_set_reg(&match, MFF_LOG_OUTPORT - MFF_REG0, pb->tunnel_key);
402         match_set_reg(&match, 0, ntohl(ip));
403
404         ofpbuf_clear(&ofpacts);
405         put_load(mac.ea, sizeof mac.ea, MFF_ETH_DST, 0, 48, &ofpacts);
406
407         ofctrl_add_flow(flow_table, OFTABLE_MAC_BINDING, 100,
408                         &match, &ofpacts);
409     }
410     ofpbuf_uninit(&ofpacts);
411 }
412 \f
413 /* Translates logical flows in the Logical_Flow table in the OVN_SB database
414  * into OpenFlow flows.  See ovn-architecture(7) for more information. */
415 void
416 lflow_run(struct controller_ctx *ctx, const struct lport_index *lports,
417           const struct mcgroup_index *mcgroups,
418           const struct hmap *local_datapaths,
419           const struct simap *ct_zones, struct hmap *flow_table)
420 {
421     add_logical_flows(ctx, lports, mcgroups, local_datapaths,
422                       ct_zones, flow_table);
423     add_neighbor_flows(ctx, lports, flow_table);
424 }
425
426 void
427 lflow_destroy(void)
428 {
429     expr_symtab_destroy(&symtab);
430 }