2 * Copyright (c) 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "byte-order.h"
22 #include "dynamic-string.h"
24 #include "meta-flow.h"
26 #include "ofp-actions.h"
27 #include "ofp-errors.h"
30 #include "openflow/openflow.h"
31 #include "unaligned.h"
34 get_be16(const void **pp)
36 const ovs_be16 *p = *pp;
43 get_be32(const void **pp)
45 const ovs_be32 *p = *pp;
46 ovs_be32 value = get_unaligned_be32(p);
52 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
54 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
55 sf->ofs = ntohs(get_be16(p));
60 learn_min_len(uint16_t header)
62 int n_bits = header & NX_LEARN_N_BITS_MASK;
63 int src_type = header & NX_LEARN_SRC_MASK;
64 int dst_type = header & NX_LEARN_DST_MASK;
68 if (src_type == NX_LEARN_SRC_FIELD) {
69 min_len += sizeof(ovs_be32); /* src_field */
70 min_len += sizeof(ovs_be16); /* src_ofs */
72 min_len += DIV_ROUND_UP(n_bits, 16);
74 if (dst_type == NX_LEARN_DST_MATCH ||
75 dst_type == NX_LEARN_DST_LOAD) {
76 min_len += sizeof(ovs_be32); /* dst_field */
77 min_len += sizeof(ovs_be16); /* dst_ofs */
82 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
83 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
85 learn_from_openflow(const struct nx_action_learn *nal, struct ofpbuf *ofpacts)
87 struct ofpact_learn *learn;
91 return OFPERR_OFPBAC_BAD_ARGUMENT;
94 learn = ofpact_put_LEARN(ofpacts);
96 learn->idle_timeout = ntohs(nal->idle_timeout);
97 learn->hard_timeout = ntohs(nal->hard_timeout);
98 learn->priority = ntohs(nal->priority);
99 learn->cookie = nal->cookie;
100 learn->table_id = nal->table_id;
101 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
102 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
104 /* We only support "send-flow-removed" for now. */
105 switch (ntohs(nal->flags)) {
109 case OFPFF_SEND_FLOW_REM:
110 learn->flags = OFPUTIL_FF_SEND_FLOW_REM;
113 return OFPERR_OFPBAC_BAD_ARGUMENT;
116 if (learn->table_id == 0xff) {
117 return OFPERR_OFPBAC_BAD_ARGUMENT;
120 end = (char *) nal + ntohs(nal->len);
121 for (p = nal + 1; p != end; ) {
122 struct ofpact_learn_spec *spec;
123 uint16_t header = ntohs(get_be16(&p));
129 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
130 learn = ofpacts->frame;
133 spec->src_type = header & NX_LEARN_SRC_MASK;
134 spec->dst_type = header & NX_LEARN_DST_MASK;
135 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
137 /* Check for valid src and dst type combination. */
138 if (spec->dst_type == NX_LEARN_DST_MATCH ||
139 spec->dst_type == NX_LEARN_DST_LOAD ||
140 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
141 spec->src_type == NX_LEARN_SRC_FIELD)) {
144 return OFPERR_OFPBAC_BAD_ARGUMENT;
147 /* Check that the arguments don't overrun the end of the action. */
148 if ((char *) end - (char *) p < learn_min_len(header)) {
149 return OFPERR_OFPBAC_BAD_LEN;
152 /* Get the source. */
153 if (spec->src_type == NX_LEARN_SRC_FIELD) {
154 get_subfield(spec->n_bits, &p, &spec->src);
156 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
158 bitwise_copy(p, p_bytes, 0,
159 &spec->src_imm, sizeof spec->src_imm, 0,
161 p = (const uint8_t *) p + p_bytes;
164 /* Get the destination. */
165 if (spec->dst_type == NX_LEARN_DST_MATCH ||
166 spec->dst_type == NX_LEARN_DST_LOAD) {
167 get_subfield(spec->n_bits, &p, &spec->dst);
170 ofpact_update_len(ofpacts, &learn->ofpact);
172 if (!is_all_zeros(p, (char *) end - (char *) p)) {
173 return OFPERR_OFPBAC_BAD_ARGUMENT;
179 /* Checks that 'learn' is a valid action on 'flow'. Returns 0 if it is valid,
180 * otherwise an OFPERR_*. */
182 learn_check(const struct ofpact_learn *learn, const struct flow *flow)
184 const struct ofpact_learn_spec *spec;
187 match_init_catchall(&match);
188 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
191 /* Check the source. */
192 if (spec->src_type == NX_LEARN_SRC_FIELD) {
193 error = mf_check_src(&spec->src, flow);
199 /* Check the destination. */
200 switch (spec->dst_type) {
201 case NX_LEARN_DST_MATCH:
202 error = mf_check_src(&spec->dst, &match.flow);
207 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
210 case NX_LEARN_DST_LOAD:
211 error = mf_check_dst(&spec->dst, &match.flow);
217 case NX_LEARN_DST_OUTPUT:
226 put_be16(struct ofpbuf *b, ovs_be16 x)
228 ofpbuf_put(b, &x, sizeof x);
232 put_be32(struct ofpbuf *b, ovs_be32 x)
234 ofpbuf_put(b, &x, sizeof x);
238 put_u16(struct ofpbuf *b, uint16_t x)
240 put_be16(b, htons(x));
244 put_u32(struct ofpbuf *b, uint32_t x)
246 put_be32(b, htonl(x));
249 /* Converts 'learn' into a "struct nx_action_learn" and appends that action to
252 learn_to_nxast(const struct ofpact_learn *learn, struct ofpbuf *openflow)
254 const struct ofpact_learn_spec *spec;
255 struct nx_action_learn *nal;
258 start_ofs = ofpbuf_size(openflow);
259 nal = ofputil_put_NXAST_LEARN(openflow);
260 nal->idle_timeout = htons(learn->idle_timeout);
261 nal->hard_timeout = htons(learn->hard_timeout);
262 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
263 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
264 nal->priority = htons(learn->priority);
265 nal->cookie = learn->cookie;
266 nal->flags = htons(learn->flags);
267 nal->table_id = learn->table_id;
269 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
270 put_u16(openflow, spec->n_bits | spec->dst_type | spec->src_type);
272 if (spec->src_type == NX_LEARN_SRC_FIELD) {
273 put_u32(openflow, spec->src.field->nxm_header);
274 put_u16(openflow, spec->src.ofs);
276 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
277 uint8_t *bits = ofpbuf_put_zeros(openflow, n_dst_bytes);
278 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
279 bits, n_dst_bytes, 0,
283 if (spec->dst_type == NX_LEARN_DST_MATCH ||
284 spec->dst_type == NX_LEARN_DST_LOAD) {
285 put_u32(openflow, spec->dst.field->nxm_header);
286 put_u16(openflow, spec->dst.ofs);
290 if ((ofpbuf_size(openflow) - start_ofs) % 8) {
291 ofpbuf_put_zeros(openflow, 8 - (ofpbuf_size(openflow) - start_ofs) % 8);
294 nal = ofpbuf_at_assert(openflow, start_ofs, sizeof *nal);
295 nal->len = htons(ofpbuf_size(openflow) - start_ofs);
298 /* Composes 'fm' so that executing it will implement 'learn' given that the
299 * packet being processed has 'flow' as its flow.
301 * Uses 'ofpacts' to store the flow mod's actions. The caller must initialize
302 * 'ofpacts' and retains ownership of it. 'fm->ofpacts' will point into the
305 * The caller has to actually execute 'fm'. */
307 learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
308 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
310 const struct ofpact_learn_spec *spec;
312 match_init_catchall(&fm->match);
313 fm->priority = learn->priority;
314 fm->cookie = htonll(0);
315 fm->cookie_mask = htonll(0);
316 fm->new_cookie = learn->cookie;
317 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
318 fm->table_id = learn->table_id;
319 fm->command = OFPFC_MODIFY_STRICT;
320 fm->idle_timeout = learn->idle_timeout;
321 fm->hard_timeout = learn->hard_timeout;
322 fm->buffer_id = UINT32_MAX;
323 fm->out_port = OFPP_NONE;
324 fm->flags = learn->flags;
327 fm->delete_reason = OFPRR_DELETE;
329 if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
330 struct ofpact_fin_timeout *oft;
332 oft = ofpact_put_FIN_TIMEOUT(ofpacts);
333 oft->fin_idle_timeout = learn->fin_idle_timeout;
334 oft->fin_hard_timeout = learn->fin_hard_timeout;
337 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
338 union mf_subvalue value;
341 if (spec->src_type == NX_LEARN_SRC_FIELD) {
342 mf_read_subfield(&spec->src, flow, &value);
344 value = spec->src_imm;
347 switch (spec->dst_type) {
348 case NX_LEARN_DST_MATCH:
349 mf_write_subfield(&spec->dst, &value, &fm->match);
352 case NX_LEARN_DST_LOAD:
353 for (ofs = 0; ofs < spec->n_bits; ofs += chunk) {
354 struct ofpact_reg_load *load;
356 chunk = MIN(spec->n_bits - ofs, 64);
358 load = ofpact_put_REG_LOAD(ofpacts);
359 load->dst.field = spec->dst.field;
360 load->dst.ofs = spec->dst.ofs + ofs;
361 load->dst.n_bits = chunk;
362 bitwise_copy(&value, sizeof value, ofs,
363 &load->subvalue, sizeof load->subvalue, 0,
368 case NX_LEARN_DST_OUTPUT:
369 if (spec->n_bits <= 16
370 || is_all_zeros(value.u8, sizeof value - 2)) {
371 ofp_port_t port = u16_to_ofp(ntohs(value.be16[7]));
373 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
374 || port == OFPP_IN_PORT
375 || port == OFPP_FLOOD
376 || port == OFPP_LOCAL
377 || port == OFPP_ALL) {
378 ofpact_put_OUTPUT(ofpacts)->port = port;
386 fm->ofpacts = ofpbuf_data(ofpacts);
387 fm->ofpacts_len = ofpbuf_size(ofpacts);
390 /* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
391 * the learn action 'learn'. */
393 learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
395 const struct ofpact_learn_spec *spec;
396 union mf_subvalue value;
398 memset(&value, 0xff, sizeof value);
399 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
400 if (spec->src_type == NX_LEARN_SRC_FIELD) {
401 mf_write_subfield_flow(&spec->src, &value, &wc->masks);
406 /* Returns NULL if successful, otherwise a malloc()'d string describing the
407 * error. The caller is responsible for freeing the returned string. */
408 static char * WARN_UNUSED_RESULT
409 learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
411 const char *full_s = s;
412 const char *arrow = strstr(s, "->");
413 struct mf_subfield dst;
414 union mf_subvalue imm;
417 memset(&imm, 0, sizeof imm);
418 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && arrow) {
419 const char *in = arrow - 1;
420 uint8_t *out = imm.u8 + sizeof imm.u8 - 1;
421 int n = arrow - (s + 2);
424 for (i = 0; i < n; i++) {
425 int hexit = hexit_value(in[-i]);
427 return xasprintf("%s: bad hex digit in value", full_s);
429 out[-(i / 2)] |= i % 2 ? hexit << 4 : hexit;
433 imm.be64[1] = htonll(strtoull(s, (char **) &s, 0));
436 if (strncmp(s, "->", 2)) {
437 return xasprintf("%s: missing `->' following value", full_s);
441 error = mf_parse_subfield(&dst, s);
446 if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
447 (8 * sizeof imm) - dst.n_bits)) {
448 return xasprintf("%s: value does not fit into %u bits",
452 spec->n_bits = dst.n_bits;
453 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
455 spec->dst_type = NX_LEARN_DST_LOAD;
460 /* Returns NULL if successful, otherwise a malloc()'d string describing the
461 * error. The caller is responsible for freeing the returned string. */
462 static char * WARN_UNUSED_RESULT
463 learn_parse_spec(const char *orig, char *name, char *value,
464 struct ofpact_learn_spec *spec)
466 if (mf_from_name(name)) {
467 const struct mf_field *dst = mf_from_name(name);
471 error = mf_parse_value(dst, value, &imm);
476 spec->n_bits = dst->n_bits;
477 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
478 memset(&spec->src_imm, 0, sizeof spec->src_imm);
479 memcpy(&spec->src_imm.u8[sizeof spec->src_imm - dst->n_bytes],
481 spec->dst_type = NX_LEARN_DST_MATCH;
482 spec->dst.field = dst;
484 spec->dst.n_bits = dst->n_bits;
485 } else if (strchr(name, '[')) {
486 /* Parse destination and check prerequisites. */
489 error = mf_parse_subfield(&spec->dst, name);
494 /* Parse source and check prerequisites. */
495 if (value[0] != '\0') {
496 error = mf_parse_subfield(&spec->src, value);
500 if (spec->src.n_bits != spec->dst.n_bits) {
501 return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
502 "differ", orig, name, spec->src.n_bits, value,
506 spec->src = spec->dst;
509 spec->n_bits = spec->src.n_bits;
510 spec->src_type = NX_LEARN_SRC_FIELD;
511 spec->dst_type = NX_LEARN_DST_MATCH;
512 } else if (!strcmp(name, "load")) {
513 if (value[strcspn(value, "[-")] == '-') {
514 char *error = learn_parse_load_immediate(value, spec);
519 struct ofpact_reg_move move;
522 error = nxm_parse_reg_move(&move, value);
527 spec->n_bits = move.src.n_bits;
528 spec->src_type = NX_LEARN_SRC_FIELD;
529 spec->src = move.src;
530 spec->dst_type = NX_LEARN_DST_LOAD;
531 spec->dst = move.dst;
533 } else if (!strcmp(name, "output")) {
534 char *error = mf_parse_subfield(&spec->src, value);
539 spec->n_bits = spec->src.n_bits;
540 spec->src_type = NX_LEARN_SRC_FIELD;
541 spec->dst_type = NX_LEARN_DST_OUTPUT;
543 return xasprintf("%s: unknown keyword %s", orig, name);
549 /* Returns NULL if successful, otherwise a malloc()'d string describing the
550 * error. The caller is responsible for freeing the returned string. */
551 static char * WARN_UNUSED_RESULT
552 learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
554 struct ofpact_learn *learn;
558 learn = ofpact_put_LEARN(ofpacts);
559 learn->idle_timeout = OFP_FLOW_PERMANENT;
560 learn->hard_timeout = OFP_FLOW_PERMANENT;
561 learn->priority = OFP_DEFAULT_PRIORITY;
564 match_init_catchall(&match);
565 while (ofputil_parse_key_value(&arg, &name, &value)) {
566 if (!strcmp(name, "table")) {
567 learn->table_id = atoi(value);
568 if (learn->table_id == 255) {
569 return xasprintf("%s: table id 255 not valid for `learn' "
572 } else if (!strcmp(name, "priority")) {
573 learn->priority = atoi(value);
574 } else if (!strcmp(name, "idle_timeout")) {
575 learn->idle_timeout = atoi(value);
576 } else if (!strcmp(name, "hard_timeout")) {
577 learn->hard_timeout = atoi(value);
578 } else if (!strcmp(name, "fin_idle_timeout")) {
579 learn->fin_idle_timeout = atoi(value);
580 } else if (!strcmp(name, "fin_hard_timeout")) {
581 learn->fin_hard_timeout = atoi(value);
582 } else if (!strcmp(name, "cookie")) {
583 learn->cookie = htonll(strtoull(value, NULL, 0));
584 } else if (!strcmp(name, "send_flow_rem")) {
585 learn->flags |= OFPFF_SEND_FLOW_REM;
587 struct ofpact_learn_spec *spec;
590 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
591 learn = ofpacts->frame;
594 error = learn_parse_spec(orig, name, value, spec);
599 /* Update 'match' to allow for satisfying destination
601 if (spec->src_type == NX_LEARN_SRC_IMMEDIATE
602 && spec->dst_type == NX_LEARN_DST_MATCH) {
603 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
607 ofpact_update_len(ofpacts, &learn->ofpact);
612 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
613 * matching OFPACT_LEARN action to 'ofpacts'. ovs-ofctl(8) describes the
616 * Returns NULL if successful, otherwise a malloc()'d string describing the
617 * error. The caller is responsible for freeing the returned string.
619 * If 'flow' is nonnull, then it should be the flow from a struct match that is
620 * the matching rule for the learning action. This helps to better validate
621 * the action's arguments.
624 char * WARN_UNUSED_RESULT
625 learn_parse(char *arg, struct ofpbuf *ofpacts)
627 char *orig = xstrdup(arg);
628 char *error = learn_parse__(orig, arg, ofpacts);
633 /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
636 learn_format(const struct ofpact_learn *learn, struct ds *s)
638 const struct ofpact_learn_spec *spec;
641 match_init_catchall(&match);
643 ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
644 if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
645 ds_put_format(s, ",idle_timeout=%"PRIu16, learn->idle_timeout);
647 if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
648 ds_put_format(s, ",hard_timeout=%"PRIu16, learn->hard_timeout);
650 if (learn->fin_idle_timeout) {
651 ds_put_format(s, ",fin_idle_timeout=%"PRIu16, learn->fin_idle_timeout);
653 if (learn->fin_hard_timeout) {
654 ds_put_format(s, ",fin_hard_timeout=%"PRIu16, learn->fin_hard_timeout);
656 if (learn->priority != OFP_DEFAULT_PRIORITY) {
657 ds_put_format(s, ",priority=%"PRIu16, learn->priority);
659 if (learn->flags & OFPFF_SEND_FLOW_REM) {
660 ds_put_cstr(s, ",send_flow_rem");
662 if (learn->cookie != 0) {
663 ds_put_format(s, ",cookie=%#"PRIx64, ntohll(learn->cookie));
666 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
669 switch (spec->src_type | spec->dst_type) {
670 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
671 if (spec->dst.ofs == 0
672 && spec->dst.n_bits == spec->dst.field->n_bits) {
673 union mf_value value;
675 memset(&value, 0, sizeof value);
676 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
677 &value, spec->dst.field->n_bytes, 0,
678 spec->dst.field->n_bits);
679 ds_put_format(s, "%s=", spec->dst.field->name);
680 mf_format(spec->dst.field, &value, NULL, s);
682 mf_format_subfield(&spec->dst, s);
684 mf_format_subvalue(&spec->src_imm, s);
688 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
689 mf_format_subfield(&spec->dst, s);
690 if (spec->src.field != spec->dst.field ||
691 spec->src.ofs != spec->dst.ofs) {
693 mf_format_subfield(&spec->src, s);
697 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
698 ds_put_format(s, "load:");
699 mf_format_subvalue(&spec->src_imm, s);
700 ds_put_cstr(s, "->");
701 mf_format_subfield(&spec->dst, s);
704 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
705 ds_put_cstr(s, "load:");
706 mf_format_subfield(&spec->src, s);
707 ds_put_cstr(s, "->");
708 mf_format_subfield(&spec->dst, s);
711 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
712 ds_put_cstr(s, "output:");
713 mf_format_subfield(&spec->src, s);