2 * Copyright (c) 2011, 2012, 2013 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 = ntohll(nal->cookie);
100 learn->flags = ntohs(nal->flags);
101 learn->table_id = nal->table_id;
102 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
103 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
105 if (learn->flags & ~OFPFF_SEND_FLOW_REM || learn->table_id == 0xff) {
106 return OFPERR_OFPBAC_BAD_ARGUMENT;
109 end = (char *) nal + ntohs(nal->len);
110 for (p = nal + 1; p != end; ) {
111 struct ofpact_learn_spec *spec;
112 uint16_t header = ntohs(get_be16(&p));
118 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
122 spec->src_type = header & NX_LEARN_SRC_MASK;
123 spec->dst_type = header & NX_LEARN_DST_MASK;
124 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
126 /* Check for valid src and dst type combination. */
127 if (spec->dst_type == NX_LEARN_DST_MATCH ||
128 spec->dst_type == NX_LEARN_DST_LOAD ||
129 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
130 spec->src_type == NX_LEARN_SRC_FIELD)) {
133 return OFPERR_OFPBAC_BAD_ARGUMENT;
136 /* Check that the arguments don't overrun the end of the action. */
137 if ((char *) end - (char *) p < learn_min_len(header)) {
138 return OFPERR_OFPBAC_BAD_LEN;
141 /* Get the source. */
142 if (spec->src_type == NX_LEARN_SRC_FIELD) {
143 get_subfield(spec->n_bits, &p, &spec->src);
145 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
147 bitwise_copy(p, p_bytes, 0,
148 &spec->src_imm, sizeof spec->src_imm, 0,
150 p = (const uint8_t *) p + p_bytes;
153 /* Get the destination. */
154 if (spec->dst_type == NX_LEARN_DST_MATCH ||
155 spec->dst_type == NX_LEARN_DST_LOAD) {
156 get_subfield(spec->n_bits, &p, &spec->dst);
159 ofpact_update_len(ofpacts, &learn->ofpact);
161 if (!is_all_zeros(p, (char *) end - (char *) p)) {
162 return OFPERR_OFPBAC_BAD_ARGUMENT;
168 /* Checks that 'learn' is a valid action on 'flow'. Returns 0 if it is valid,
169 * otherwise an OFPERR_*. */
171 learn_check(const struct ofpact_learn *learn, const struct flow *flow)
173 const struct ofpact_learn_spec *spec;
176 match_init_catchall(&match);
177 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
180 /* Check the source. */
181 if (spec->src_type == NX_LEARN_SRC_FIELD) {
182 error = mf_check_src(&spec->src, flow);
188 /* Check the destination. */
189 switch (spec->dst_type) {
190 case NX_LEARN_DST_MATCH:
191 error = mf_check_src(&spec->dst, &match.flow);
196 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
199 case NX_LEARN_DST_LOAD:
200 error = mf_check_dst(&spec->dst, &match.flow);
206 case NX_LEARN_DST_OUTPUT:
215 put_be16(struct ofpbuf *b, ovs_be16 x)
217 ofpbuf_put(b, &x, sizeof x);
221 put_be32(struct ofpbuf *b, ovs_be32 x)
223 ofpbuf_put(b, &x, sizeof x);
227 put_u16(struct ofpbuf *b, uint16_t x)
229 put_be16(b, htons(x));
233 put_u32(struct ofpbuf *b, uint32_t x)
235 put_be32(b, htonl(x));
238 /* Converts 'learn' into a "struct nx_action_learn" and appends that action to
241 learn_to_nxast(const struct ofpact_learn *learn, struct ofpbuf *openflow)
243 const struct ofpact_learn_spec *spec;
244 struct nx_action_learn *nal;
247 start_ofs = openflow->size;
248 nal = ofputil_put_NXAST_LEARN(openflow);
249 nal->idle_timeout = htons(learn->idle_timeout);
250 nal->hard_timeout = htons(learn->hard_timeout);
251 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
252 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
253 nal->priority = htons(learn->priority);
254 nal->cookie = htonll(learn->cookie);
255 nal->flags = htons(learn->flags);
256 nal->table_id = learn->table_id;
258 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
259 put_u16(openflow, spec->n_bits | spec->dst_type | spec->src_type);
261 if (spec->src_type == NX_LEARN_SRC_FIELD) {
262 put_u32(openflow, spec->src.field->nxm_header);
263 put_u16(openflow, spec->src.ofs);
265 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
266 uint8_t *bits = ofpbuf_put_zeros(openflow, n_dst_bytes);
267 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
268 bits, n_dst_bytes, 0,
272 if (spec->dst_type == NX_LEARN_DST_MATCH ||
273 spec->dst_type == NX_LEARN_DST_LOAD) {
274 put_u32(openflow, spec->dst.field->nxm_header);
275 put_u16(openflow, spec->dst.ofs);
279 if ((openflow->size - start_ofs) % 8) {
280 ofpbuf_put_zeros(openflow, 8 - (openflow->size - start_ofs) % 8);
283 nal = ofpbuf_at_assert(openflow, start_ofs, sizeof *nal);
284 nal->len = htons(openflow->size - start_ofs);
287 /* Composes 'fm' so that executing it will implement 'learn' given that the
288 * packet being processed has 'flow' as its flow.
290 * Uses 'ofpacts' to store the flow mod's actions. The caller must initialize
291 * 'ofpacts' and retains ownership of it. 'fm->ofpacts' will point into the
294 * The caller has to actually execute 'fm'. */
296 learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
297 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
299 const struct ofpact_learn_spec *spec;
301 match_init_catchall(&fm->match);
302 fm->priority = learn->priority;
303 fm->cookie = htonll(0);
304 fm->cookie_mask = htonll(0);
305 fm->new_cookie = htonll(learn->cookie);
306 fm->modify_cookie = fm->new_cookie != htonll(UINT64_MAX);
307 fm->table_id = learn->table_id;
308 fm->command = OFPFC_MODIFY_STRICT;
309 fm->idle_timeout = learn->idle_timeout;
310 fm->hard_timeout = learn->hard_timeout;
311 fm->buffer_id = UINT32_MAX;
312 fm->out_port = OFPP_NONE;
313 fm->flags = learn->flags;
317 if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
318 struct ofpact_fin_timeout *oft;
320 oft = ofpact_put_FIN_TIMEOUT(ofpacts);
321 oft->fin_idle_timeout = learn->fin_idle_timeout;
322 oft->fin_hard_timeout = learn->fin_hard_timeout;
325 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
326 union mf_subvalue value;
329 if (spec->src_type == NX_LEARN_SRC_FIELD) {
330 mf_read_subfield(&spec->src, flow, &value);
332 value = spec->src_imm;
335 switch (spec->dst_type) {
336 case NX_LEARN_DST_MATCH:
337 mf_write_subfield(&spec->dst, &value, &fm->match);
340 case NX_LEARN_DST_LOAD:
341 for (ofs = 0; ofs < spec->n_bits; ofs += chunk) {
342 struct ofpact_reg_load *load;
344 chunk = MIN(spec->n_bits - ofs, 64);
346 load = ofpact_put_REG_LOAD(ofpacts);
347 load->dst.field = spec->dst.field;
348 load->dst.ofs = spec->dst.ofs + ofs;
349 load->dst.n_bits = chunk;
350 bitwise_copy(&value, sizeof value, ofs,
351 &load->subvalue, sizeof load->subvalue, 0,
356 case NX_LEARN_DST_OUTPUT:
357 if (spec->n_bits <= 16
358 || is_all_zeros(value.u8, sizeof value - 2)) {
359 ofp_port_t port = u16_to_ofp(ntohs(value.be16[7]));
361 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
362 || port == OFPP_IN_PORT
363 || port == OFPP_FLOOD
364 || port == OFPP_LOCAL
365 || port == OFPP_ALL) {
366 ofpact_put_OUTPUT(ofpacts)->port = port;
374 fm->ofpacts = ofpacts->data;
375 fm->ofpacts_len = ofpacts->size;
378 /* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
379 * the learn action 'learn'. */
381 learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
383 const struct ofpact_learn_spec *spec;
384 union mf_subvalue value;
386 memset(&value, 0xff, sizeof value);
387 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
388 if (spec->src_type == NX_LEARN_SRC_FIELD) {
389 mf_write_subfield_flow(&spec->src, &value, &wc->masks);
394 /* Returns NULL if successful, otherwise a malloc()'d string describing the
395 * error. The caller is responsible for freeing the returned string. */
396 static char * WARN_UNUSED_RESULT
397 learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
399 const char *full_s = s;
400 const char *arrow = strstr(s, "->");
401 struct mf_subfield dst;
402 union mf_subvalue imm;
405 memset(&imm, 0, sizeof imm);
406 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && arrow) {
407 const char *in = arrow - 1;
408 uint8_t *out = imm.u8 + sizeof imm.u8 - 1;
409 int n = arrow - (s + 2);
412 for (i = 0; i < n; i++) {
413 int hexit = hexit_value(in[-i]);
415 return xasprintf("%s: bad hex digit in value", full_s);
417 out[-(i / 2)] |= i % 2 ? hexit << 4 : hexit;
421 imm.be64[1] = htonll(strtoull(s, (char **) &s, 0));
424 if (strncmp(s, "->", 2)) {
425 return xasprintf("%s: missing `->' following value", full_s);
429 error = mf_parse_subfield(&dst, s);
434 if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
435 (8 * sizeof imm) - dst.n_bits)) {
436 return xasprintf("%s: value does not fit into %u bits",
440 spec->n_bits = dst.n_bits;
441 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
443 spec->dst_type = NX_LEARN_DST_LOAD;
448 /* Returns NULL if successful, otherwise a malloc()'d string describing the
449 * error. The caller is responsible for freeing the returned string. */
450 static char * WARN_UNUSED_RESULT
451 learn_parse_spec(const char *orig, char *name, char *value,
452 struct ofpact_learn_spec *spec)
454 if (mf_from_name(name)) {
455 const struct mf_field *dst = mf_from_name(name);
459 error = mf_parse_value(dst, value, &imm);
464 spec->n_bits = dst->n_bits;
465 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
466 memset(&spec->src_imm, 0, sizeof spec->src_imm);
467 memcpy(&spec->src_imm.u8[sizeof spec->src_imm - dst->n_bytes],
469 spec->dst_type = NX_LEARN_DST_MATCH;
470 spec->dst.field = dst;
472 spec->dst.n_bits = dst->n_bits;
473 } else if (strchr(name, '[')) {
474 /* Parse destination and check prerequisites. */
477 error = mf_parse_subfield(&spec->dst, name);
482 /* Parse source and check prerequisites. */
483 if (value[0] != '\0') {
484 error = mf_parse_subfield(&spec->src, value);
488 if (spec->src.n_bits != spec->dst.n_bits) {
489 return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
490 "differ", orig, name, spec->src.n_bits, value,
494 spec->src = spec->dst;
497 spec->n_bits = spec->src.n_bits;
498 spec->src_type = NX_LEARN_SRC_FIELD;
499 spec->dst_type = NX_LEARN_DST_MATCH;
500 } else if (!strcmp(name, "load")) {
501 if (value[strcspn(value, "[-")] == '-') {
502 char *error = learn_parse_load_immediate(value, spec);
507 struct ofpact_reg_move move;
510 error = nxm_parse_reg_move(&move, value);
515 spec->n_bits = move.src.n_bits;
516 spec->src_type = NX_LEARN_SRC_FIELD;
517 spec->src = move.src;
518 spec->dst_type = NX_LEARN_DST_LOAD;
519 spec->dst = move.dst;
521 } else if (!strcmp(name, "output")) {
522 char *error = mf_parse_subfield(&spec->src, value);
527 spec->n_bits = spec->src.n_bits;
528 spec->src_type = NX_LEARN_SRC_FIELD;
529 spec->dst_type = NX_LEARN_DST_OUTPUT;
531 return xasprintf("%s: unknown keyword %s", orig, name);
537 /* Returns NULL if successful, otherwise a malloc()'d string describing the
538 * error. The caller is responsible for freeing the returned string. */
539 static char * WARN_UNUSED_RESULT
540 learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
542 struct ofpact_learn *learn;
546 learn = ofpact_put_LEARN(ofpacts);
547 learn->idle_timeout = OFP_FLOW_PERMANENT;
548 learn->hard_timeout = OFP_FLOW_PERMANENT;
549 learn->priority = OFP_DEFAULT_PRIORITY;
552 match_init_catchall(&match);
553 while (ofputil_parse_key_value(&arg, &name, &value)) {
554 if (!strcmp(name, "table")) {
555 learn->table_id = atoi(value);
556 if (learn->table_id == 255) {
557 return xasprintf("%s: table id 255 not valid for `learn' "
560 } else if (!strcmp(name, "priority")) {
561 learn->priority = atoi(value);
562 } else if (!strcmp(name, "idle_timeout")) {
563 learn->idle_timeout = atoi(value);
564 } else if (!strcmp(name, "hard_timeout")) {
565 learn->hard_timeout = atoi(value);
566 } else if (!strcmp(name, "fin_idle_timeout")) {
567 learn->fin_idle_timeout = atoi(value);
568 } else if (!strcmp(name, "fin_hard_timeout")) {
569 learn->fin_hard_timeout = atoi(value);
570 } else if (!strcmp(name, "cookie")) {
571 learn->cookie = strtoull(value, NULL, 0);
573 struct ofpact_learn_spec *spec;
576 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
580 error = learn_parse_spec(orig, name, value, spec);
585 /* Update 'match' to allow for satisfying destination
587 if (spec->src_type == NX_LEARN_SRC_IMMEDIATE
588 && spec->dst_type == NX_LEARN_DST_MATCH) {
589 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
593 ofpact_update_len(ofpacts, &learn->ofpact);
598 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
599 * matching OFPACT_LEARN action to 'ofpacts'. ovs-ofctl(8) describes the
602 * Returns NULL if successful, otherwise a malloc()'d string describing the
603 * error. The caller is responsible for freeing the returned string.
605 * If 'flow' is nonnull, then it should be the flow from a struct match that is
606 * the matching rule for the learning action. This helps to better validate
607 * the action's arguments.
610 char * WARN_UNUSED_RESULT
611 learn_parse(char *arg, struct ofpbuf *ofpacts)
613 char *orig = xstrdup(arg);
614 char *error = learn_parse__(orig, arg, ofpacts);
619 /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
622 learn_format(const struct ofpact_learn *learn, struct ds *s)
624 const struct ofpact_learn_spec *spec;
627 match_init_catchall(&match);
629 ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
630 if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
631 ds_put_format(s, ",idle_timeout=%"PRIu16, learn->idle_timeout);
633 if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
634 ds_put_format(s, ",hard_timeout=%"PRIu16, learn->hard_timeout);
636 if (learn->fin_idle_timeout) {
637 ds_put_format(s, ",fin_idle_timeout=%"PRIu16, learn->fin_idle_timeout);
639 if (learn->fin_hard_timeout) {
640 ds_put_format(s, ",fin_hard_timeout=%"PRIu16, learn->fin_hard_timeout);
642 if (learn->priority != OFP_DEFAULT_PRIORITY) {
643 ds_put_format(s, ",priority=%"PRIu16, learn->priority);
645 if (learn->flags & OFPFF_SEND_FLOW_REM) {
646 ds_put_cstr(s, ",OFPFF_SEND_FLOW_REM");
648 if (learn->cookie != 0) {
649 ds_put_format(s, ",cookie=%#"PRIx64, learn->cookie);
652 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
655 switch (spec->src_type | spec->dst_type) {
656 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
657 if (spec->dst.ofs == 0
658 && spec->dst.n_bits == spec->dst.field->n_bits) {
659 union mf_value value;
661 memset(&value, 0, sizeof value);
662 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
663 &value, spec->dst.field->n_bytes, 0,
664 spec->dst.field->n_bits);
665 ds_put_format(s, "%s=", spec->dst.field->name);
666 mf_format(spec->dst.field, &value, NULL, s);
668 mf_format_subfield(&spec->dst, s);
670 mf_format_subvalue(&spec->src_imm, s);
674 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
675 mf_format_subfield(&spec->dst, s);
676 if (spec->src.field != spec->dst.field ||
677 spec->src.ofs != spec->dst.ofs) {
679 mf_format_subfield(&spec->src, s);
683 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
684 ds_put_format(s, "load:");
685 mf_format_subvalue(&spec->src_imm, s);
686 ds_put_cstr(s, "->");
687 mf_format_subfield(&spec->dst, s);
690 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
691 ds_put_cstr(s, "load:");
692 mf_format_subfield(&spec->src, s);
693 ds_put_cstr(s, "->");
694 mf_format_subfield(&spec->dst, s);
697 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
698 ds_put_cstr(s, "output:");
699 mf_format_subfield(&spec->src, s);