1 /* Copyright (c) 2015, 2016 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "dynamic-string.h"
21 #include "ofp-actions.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"
31 VLOG_DEFINE_THIS_MODULE(lflow);
35 /* Contains "struct expr_symbol"s for fields supported by OVN lflows. */
36 static struct shash symtab;
39 add_logical_register(struct shash *symtab, enum mf_field_id id)
43 snprintf(name, sizeof name, "reg%d", id - MFF_REG0);
44 expr_symtab_add_field(symtab, name, id, NULL, false);
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);
58 /* Logical registers. */
59 #define MFF_LOG_REG(ID) add_logical_register(&symtab, ID);
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);
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]");
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",
93 expr_symtab_add_subfield(&symtab, "vlan.vid", "vlan.present",
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);
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");
108 expr_symtab_add_predicate(&symtab, "icmp4", "ip4 && ip.proto == 1");
109 expr_symtab_add_field(&symtab, "icmp4.type", MFF_ICMPV4_TYPE, "icmp4",
111 expr_symtab_add_field(&symtab, "icmp4.code", MFF_ICMPV4_CODE, "icmp4",
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);
118 expr_symtab_add_predicate(&symtab, "icmp6", "ip6 && ip.proto == 58");
119 expr_symtab_add_field(&symtab, "icmp6.type", MFF_ICMPV6_TYPE, "icmp6",
121 expr_symtab_add_field(&symtab, "icmp6.code", MFF_ICMPV6_CODE, "icmp6",
124 expr_symtab_add_predicate(&symtab, "icmp", "icmp4 || icmp6");
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");
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);
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);
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);
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);
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);
161 struct lookup_port_aux {
162 const struct lport_index *lports;
163 const struct mcgroup_index *mcgroups;
164 const struct sbrec_datapath_binding *dp;
168 lookup_port_cb(const void *aux_, const char *port_name, unsigned int *portp)
170 const struct lookup_port_aux *aux = aux_;
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;
179 const struct sbrec_multicast_group *mg
180 = mcgroup_lookup_by_dp_name(aux->mcgroups, aux->dp, port_name);
182 *portp = mg->tunnel_key;
190 is_switch(const struct sbrec_datapath_binding *ldp)
192 return smap_get(&ldp->external_ids, "logical-switch") != NULL;
196 /* Adds the logical flows from the Logical_Flow table to 'flow_table'. */
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)
203 uint32_t conj_id_ofs = 1;
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");
210 const struct sbrec_datapath_binding *ldp = lflow->logical_datapath;
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
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
226 * A better approach would be a kind of "flood fill" algorithm:
228 * 1. Initialize set S to the logical datapaths that have a port
229 * located on the hypervisor.
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.
236 struct hmap_node *ld;
237 ld = hmap_first_with_hash(local_datapaths, ldp->tunnel_key);
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);
252 /* Translate OVN actions into OpenFlow actions.
254 * XXX Deny changes to 'outport' in egress pipeline. */
255 uint64_t ofpacts_stub[64 / 8];
256 struct ofpbuf ofpacts;
257 struct expr *prereqs;
260 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
261 struct lookup_port_aux aux = {
263 .mcgroups = mcgroups,
264 .dp = lflow->logical_datapath
266 struct action_params ap = {
268 .lookup_port = lookup_port_cb,
270 .ct_zones = ct_zones,
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,
278 error = actions_parse_string(lflow->actions, &ap, &ofpacts, &prereqs);
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);
287 /* Translate OVN match into table of OpenFlow matches. */
291 expr = expr_parse_string(lflow->match, &symtab, &error);
294 expr = expr_combine(EXPR_T_AND, expr, prereqs);
297 expr = expr_annotate(expr, &symtab, &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);
309 expr = expr_simplify(expr);
310 expr = expr_normalize(expr);
311 uint32_t n_conjs = expr_to_matches(expr, lookup_port_cb, &aux,
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;
324 ofctrl_add_flow(flow_table, ptable, lflow->priority,
325 &m->match, &ofpacts);
327 uint64_t conj_stubs[64 / 8];
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;
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;
340 ofctrl_add_flow(flow_table, ptable, lflow->priority,
342 ofpbuf_uninit(&conj);
347 expr_matches_destroy(&matches);
348 ofpbuf_uninit(&ofpacts);
349 conj_id_ofs += n_conjs;
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)
358 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
359 sf->field = mf_from_id(dst);
360 sf->flow_has_vlan = false;
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);
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
370 add_neighbor_flows(struct controller_ctx *ctx,
371 const struct lport_index *lports, struct hmap *flow_table)
373 struct ofpbuf ofpacts;
375 match_init_catchall(&match);
376 ofpbuf_init(&ofpacts, 0);
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);
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);
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);
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));
404 ofpbuf_clear(&ofpacts);
405 put_load(mac.ea, sizeof mac.ea, MFF_ETH_DST, 0, 48, &ofpacts);
407 ofctrl_add_flow(flow_table, OFTABLE_MAC_BINDING, 100,
410 ofpbuf_uninit(&ofpacts);
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. */
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)
421 add_logical_flows(ctx, lports, mcgroups, local_datapaths,
422 ct_zones, flow_table);
423 add_neighbor_flows(ctx, lports, flow_table);
429 expr_symtab_destroy(&symtab);