2 * Copyright (c) 2011, 2012 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"
23 #include "meta-flow.h"
25 #include "ofp-errors.h"
28 #include "openflow/openflow.h"
29 #include "unaligned.h"
32 get_be16(const void **pp)
34 const ovs_be16 *p = *pp;
41 get_be32(const void **pp)
43 const ovs_be32 *p = *pp;
44 ovs_be32 value = get_unaligned_be32(p);
50 get_bits(int n_bits, const void **p)
52 int n_segs = DIV_ROUND_UP(n_bits, 16);
56 while (n_segs-- > 0) {
57 value = (value << 16) | ntohs(get_be16(p));
63 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
65 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
66 sf->ofs = ntohs(get_be16(p));
71 learn_min_len(uint16_t header)
73 int n_bits = header & NX_LEARN_N_BITS_MASK;
74 int src_type = header & NX_LEARN_SRC_MASK;
75 int dst_type = header & NX_LEARN_DST_MASK;
79 if (src_type == NX_LEARN_SRC_FIELD) {
80 min_len += sizeof(ovs_be32); /* src_field */
81 min_len += sizeof(ovs_be16); /* src_ofs */
83 min_len += DIV_ROUND_UP(n_bits, 16);
85 if (dst_type == NX_LEARN_DST_MATCH ||
86 dst_type == NX_LEARN_DST_LOAD) {
87 min_len += sizeof(ovs_be32); /* dst_field */
88 min_len += sizeof(ovs_be16); /* dst_ofs */
94 learn_check_header(uint16_t header, size_t len)
96 int src_type = header & NX_LEARN_SRC_MASK;
97 int dst_type = header & NX_LEARN_DST_MASK;
99 /* Check for valid src and dst type combination. */
100 if (dst_type == NX_LEARN_DST_MATCH ||
101 dst_type == NX_LEARN_DST_LOAD ||
102 (dst_type == NX_LEARN_DST_OUTPUT &&
103 src_type == NX_LEARN_SRC_FIELD)) {
106 return OFPERR_OFPBAC_BAD_ARGUMENT;
109 /* Check that the arguments don't overrun the end of the action. */
110 if (len < learn_min_len(header)) {
111 return OFPERR_OFPBAC_BAD_LEN;
117 /* Checks that 'learn' (which must be at least 'sizeof *learn' bytes long) is a
118 * valid action on 'flow'. */
120 learn_check(const struct nx_action_learn *learn, const struct flow *flow)
122 struct cls_rule rule;
125 cls_rule_init_catchall(&rule, 0);
127 if (learn->flags & ~htons(OFPFF_SEND_FLOW_REM)
129 || learn->table_id == 0xff) {
130 return OFPERR_OFPBAC_BAD_ARGUMENT;
133 end = (char *) learn + ntohs(learn->len);
134 for (p = learn + 1; p != end; ) {
135 uint16_t header = ntohs(get_be16(&p));
136 int n_bits = header & NX_LEARN_N_BITS_MASK;
137 int src_type = header & NX_LEARN_SRC_MASK;
138 int dst_type = header & NX_LEARN_DST_MASK;
147 error = learn_check_header(header, (char *) end - (char *) p);
152 /* Check the source. */
153 if (src_type == NX_LEARN_SRC_FIELD) {
154 struct mf_subfield src;
156 get_subfield(n_bits, &p, &src);
157 error = mf_check_src(&src, flow);
163 value = get_bits(n_bits, &p);
166 /* Check the destination. */
167 if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) {
168 struct mf_subfield dst;
170 get_subfield(n_bits, &p, &dst);
171 error = (dst_type == NX_LEARN_DST_LOAD
172 ? mf_check_dst(&dst, &rule.flow)
173 : mf_check_src(&dst, &rule.flow));
178 if (dst_type == NX_LEARN_DST_MATCH
179 && src_type == NX_LEARN_SRC_IMMEDIATE) {
181 mf_set_subfield(&dst, value, &rule);
183 /* We're only setting subfields to allow us to check
184 * prerequisites. No prerequisite depends on the value of
185 * a field that is wider than 64 bits. So just skip
186 * setting it entirely. */
187 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 11);
192 if (!is_all_zeros(p, (char *) end - (char *) p)) {
193 return OFPERR_OFPBAC_BAD_ARGUMENT;
200 learn_execute(const struct nx_action_learn *learn, const struct flow *flow,
201 struct ofputil_flow_mod *fm)
204 struct ofpbuf actions;
206 cls_rule_init_catchall(&fm->cr, ntohs(learn->priority));
207 fm->cookie = htonll(0);
208 fm->cookie_mask = htonll(0);
209 fm->new_cookie = learn->cookie;
210 fm->table_id = learn->table_id;
211 fm->command = OFPFC_MODIFY_STRICT;
212 fm->idle_timeout = ntohs(learn->idle_timeout);
213 fm->hard_timeout = ntohs(learn->hard_timeout);
214 fm->buffer_id = UINT32_MAX;
215 fm->out_port = OFPP_NONE;
216 fm->flags = ntohs(learn->flags) & OFPFF_SEND_FLOW_REM;
220 ofpbuf_init(&actions, 64);
222 if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
223 struct nx_action_fin_timeout *naft;
225 naft = ofputil_put_NXAST_FIN_TIMEOUT(&actions);
226 naft->fin_idle_timeout = learn->fin_idle_timeout;
227 naft->fin_hard_timeout = learn->fin_hard_timeout;
230 for (p = learn + 1, end = (char *) learn + ntohs(learn->len); p != end; ) {
231 uint16_t header = ntohs(get_be16(&p));
232 int n_bits = header & NX_LEARN_N_BITS_MASK;
233 int src_type = header & NX_LEARN_SRC_MASK;
234 int dst_type = header & NX_LEARN_DST_MASK;
235 union mf_subvalue value;
237 struct mf_subfield dst;
244 if (src_type == NX_LEARN_SRC_FIELD) {
245 struct mf_subfield src;
247 get_subfield(n_bits, &p, &src);
248 mf_read_subfield(&src, flow, &value);
250 int p_bytes = 2 * DIV_ROUND_UP(n_bits, 16);
252 memset(&value, 0, sizeof value);
253 bitwise_copy(p, p_bytes, 0,
254 &value, sizeof value, 0,
256 p = (const uint8_t *) p + p_bytes;
260 case NX_LEARN_DST_MATCH:
261 get_subfield(n_bits, &p, &dst);
262 mf_write_subfield(&dst, &value, &fm->cr);
265 case NX_LEARN_DST_LOAD:
266 get_subfield(n_bits, &p, &dst);
267 for (ofs = 0; ofs < n_bits; ofs += chunk) {
268 struct nx_action_reg_load *load;
270 chunk = MIN(n_bits - ofs, 64);
272 load = ofputil_put_NXAST_REG_LOAD(&actions);
273 load->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs + ofs, chunk);
274 load->dst = htonl(dst.field->nxm_header);
275 bitwise_copy(&value, sizeof value, ofs,
276 &load->value, sizeof load->value, 0,
281 case NX_LEARN_DST_OUTPUT:
282 if (n_bits <= 16 || is_all_zeros(value.u8, sizeof value - 2)) {
283 ofputil_put_OFPAT10_OUTPUT(&actions)->port = value.be16[7];
289 fm->actions = ofpbuf_steal_data(&actions);
290 fm->n_actions = actions.size / sizeof(struct ofp_action_header);
294 put_be16(struct ofpbuf *b, ovs_be16 x)
296 ofpbuf_put(b, &x, sizeof x);
300 put_be32(struct ofpbuf *b, ovs_be32 x)
302 ofpbuf_put(b, &x, sizeof x);
306 put_u16(struct ofpbuf *b, uint16_t x)
308 put_be16(b, htons(x));
312 put_u32(struct ofpbuf *b, uint32_t x)
314 put_be32(b, htonl(x));
321 struct mf_subfield src;
322 union mf_subvalue src_imm;
325 struct mf_subfield dst;
329 learn_parse_load_immediate(const char *s, struct learn_spec *spec)
331 const char *full_s = s;
332 const char *arrow = strstr(s, "->");
333 struct mf_subfield dst;
334 union mf_subvalue imm;
336 memset(&imm, 0, sizeof imm);
337 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && arrow) {
338 const char *in = arrow - 1;
339 uint8_t *out = imm.u8 + sizeof imm.u8 - 1;
340 int n = arrow - (s + 2);
343 for (i = 0; i < n; i++) {
344 int hexit = hexit_value(in[-i]);
346 ovs_fatal(0, "%s: bad hex digit in value", full_s);
348 out[-(i / 2)] |= i % 2 ? hexit << 4 : hexit;
352 imm.be64[1] = htonll(strtoull(s, (char **) &s, 0));
355 if (strncmp(s, "->", 2)) {
356 ovs_fatal(0, "%s: missing `->' following value", full_s);
360 s = mf_parse_subfield(&dst, s);
362 ovs_fatal(0, "%s: trailing garbage following destination", full_s);
365 if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
366 (8 * sizeof imm) - dst.n_bits)) {
367 ovs_fatal(0, "%s: value does not fit into %u bits",
371 spec->n_bits = dst.n_bits;
372 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
374 spec->dst_type = NX_LEARN_DST_LOAD;
379 learn_parse_spec(const char *orig, char *name, char *value,
380 struct learn_spec *spec)
382 memset(spec, 0, sizeof *spec);
383 if (mf_from_name(name)) {
384 const struct mf_field *dst = mf_from_name(name);
388 error = mf_parse_value(dst, value, &imm);
390 ovs_fatal(0, "%s", error);
393 spec->n_bits = dst->n_bits;
394 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
395 memset(&spec->src_imm, 0, sizeof spec->src_imm);
396 memcpy(&spec->src_imm.u8[sizeof spec->src_imm - dst->n_bytes],
398 spec->dst_type = NX_LEARN_DST_MATCH;
399 spec->dst.field = dst;
401 spec->dst.n_bits = dst->n_bits;
402 } else if (strchr(name, '[')) {
403 /* Parse destination and check prerequisites. */
404 if (mf_parse_subfield(&spec->dst, name)[0] != '\0') {
405 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
409 /* Parse source and check prerequisites. */
410 if (value[0] != '\0') {
411 if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
412 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
415 if (spec->src.n_bits != spec->dst.n_bits) {
416 ovs_fatal(0, "%s: bit widths of %s (%u) and %s (%u) differ",
417 orig, name, spec->src.n_bits, value,
421 spec->src = spec->dst;
424 spec->n_bits = spec->src.n_bits;
425 spec->src_type = NX_LEARN_SRC_FIELD;
426 spec->dst_type = NX_LEARN_DST_MATCH;
427 } else if (!strcmp(name, "load")) {
428 if (value[strcspn(value, "[-")] == '-') {
429 learn_parse_load_immediate(value, spec);
431 struct nx_action_reg_move move;
433 nxm_parse_reg_move(&move, value);
435 spec->n_bits = ntohs(move.n_bits);
436 spec->src_type = NX_LEARN_SRC_FIELD;
437 nxm_decode_discrete(&spec->src,
438 move.src, move.src_ofs, move.n_bits);
439 spec->dst_type = NX_LEARN_DST_LOAD;
440 nxm_decode_discrete(&spec->dst,
441 move.dst, move.dst_ofs, move.n_bits);
443 } else if (!strcmp(name, "output")) {
444 if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
445 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
449 spec->n_bits = spec->src.n_bits;
450 spec->src_type = NX_LEARN_SRC_FIELD;
451 spec->dst_type = NX_LEARN_DST_OUTPUT;
453 ovs_fatal(0, "%s: unknown keyword %s", orig, name);
457 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
458 * matching NXAST_LEARN action to 'b'. The format parsed is described in
461 * Prints an error on stderr and aborts the program if 'arg' syntax is invalid.
463 * If 'flow' is nonnull, then it should be the flow from a cls_rule that is
464 * the matching rule for the learning action. This helps to better validate
465 * the action's arguments.
469 learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow)
471 char *orig = xstrdup(arg);
477 struct nx_action_learn *learn;
478 struct cls_rule rule;
481 learn = ofputil_put_NXAST_LEARN(b);
482 learn->idle_timeout = htons(OFP_FLOW_PERMANENT);
483 learn->hard_timeout = htons(OFP_FLOW_PERMANENT);
484 learn->priority = htons(OFP_DEFAULT_PRIORITY);
485 learn->cookie = htonll(0);
486 learn->flags = htons(0);
489 cls_rule_init_catchall(&rule, 0);
490 while (ofputil_parse_key_value(&arg, &name, &value)) {
491 learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn);
492 if (!strcmp(name, "table")) {
493 learn->table_id = atoi(value);
494 if (learn->table_id == 255) {
495 ovs_fatal(0, "%s: table id 255 not valid for `learn' action",
498 } else if (!strcmp(name, "priority")) {
499 learn->priority = htons(atoi(value));
500 } else if (!strcmp(name, "idle_timeout")) {
501 learn->idle_timeout = htons(atoi(value));
502 } else if (!strcmp(name, "hard_timeout")) {
503 learn->hard_timeout = htons(atoi(value));
504 } else if (!strcmp(name, "fin_idle_timeout")) {
505 learn->fin_idle_timeout = htons(atoi(value));
506 } else if (!strcmp(name, "fin_hard_timeout")) {
507 learn->fin_hard_timeout = htons(atoi(value));
508 } else if (!strcmp(name, "cookie")) {
509 learn->cookie = htonll(strtoull(value, NULL, 0));
511 struct learn_spec spec;
513 learn_parse_spec(orig, name, value, &spec);
515 /* Check prerequisites. */
516 if (spec.src_type == NX_LEARN_SRC_FIELD
517 && flow && !mf_are_prereqs_ok(spec.src.field, flow)) {
518 ovs_fatal(0, "%s: cannot specify source field %s because "
519 "prerequisites are not satisfied",
520 orig, spec.src.field->name);
522 if ((spec.dst_type == NX_LEARN_DST_MATCH
523 || spec.dst_type == NX_LEARN_DST_LOAD)
524 && !mf_are_prereqs_ok(spec.dst.field, &rule.flow)) {
525 ovs_fatal(0, "%s: cannot specify destination field %s because "
526 "prerequisites are not satisfied",
527 orig, spec.dst.field->name);
530 /* Update 'rule' to allow for satisfying destination
532 if (spec.src_type == NX_LEARN_SRC_IMMEDIATE
533 && spec.dst_type == NX_LEARN_DST_MATCH) {
534 mf_write_subfield(&spec.dst, &spec.src_imm, &rule);
537 /* Output the flow_mod_spec. */
538 put_u16(b, spec.n_bits | spec.src_type | spec.dst_type);
539 if (spec.src_type == NX_LEARN_SRC_IMMEDIATE) {
540 int n_bytes = DIV_ROUND_UP(spec.n_bits, 16) * 2;
541 int ofs = sizeof spec.src_imm - n_bytes;
542 ofpbuf_put(b, &spec.src_imm.u8[ofs], n_bytes);
544 put_u32(b, spec.src.field->nxm_header);
545 put_u16(b, spec.src.ofs);
547 if (spec.dst_type == NX_LEARN_DST_MATCH ||
548 spec.dst_type == NX_LEARN_DST_LOAD) {
549 put_u32(b, spec.dst.field->nxm_header);
550 put_u16(b, spec.dst.ofs);
552 assert(spec.dst_type == NX_LEARN_DST_OUTPUT);
559 len = b->size - learn_ofs;
561 ofpbuf_put_zeros(b, 8 - len % 8);
564 learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn);
565 learn->len = htons(b->size - learn_ofs);
567 /* In theory the above should have caught any errors, but... */
569 error = learn_check(learn, flow);
571 ovs_fatal(0, "%s: %s", orig, ofperr_to_string(error));
578 learn_format(const struct nx_action_learn *learn, struct ds *s)
580 struct cls_rule rule;
583 cls_rule_init_catchall(&rule, 0);
585 ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
586 if (learn->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
587 ds_put_format(s, ",idle_timeout=%"PRIu16, ntohs(learn->idle_timeout));
589 if (learn->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
590 ds_put_format(s, ",hard_timeout=%"PRIu16, ntohs(learn->hard_timeout));
592 if (learn->fin_idle_timeout) {
593 ds_put_format(s, ",fin_idle_timeout=%"PRIu16,
594 ntohs(learn->fin_idle_timeout));
596 if (learn->fin_hard_timeout) {
597 ds_put_format(s, ",fin_hard_timeout=%"PRIu16,
598 ntohs(learn->fin_hard_timeout));
600 if (learn->priority != htons(OFP_DEFAULT_PRIORITY)) {
601 ds_put_format(s, ",priority=%"PRIu16, ntohs(learn->priority));
603 if (learn->flags & htons(OFPFF_SEND_FLOW_REM)) {
604 ds_put_cstr(s, ",OFPFF_SEND_FLOW_REM");
606 if (learn->flags & htons(~OFPFF_SEND_FLOW_REM)) {
607 ds_put_format(s, ",***flags=%"PRIu16"***",
608 ntohs(learn->flags) & ~OFPFF_SEND_FLOW_REM);
610 if (learn->cookie != htonll(0)) {
611 ds_put_format(s, ",cookie=0x%"PRIx64, ntohll(learn->cookie));
613 if (learn->pad != 0) {
614 ds_put_cstr(s, ",***nonzero pad***");
617 end = (char *) learn + ntohs(learn->len);
618 for (p = learn + 1; p != end; ) {
619 uint16_t header = ntohs(get_be16(&p));
620 int n_bits = header & NX_LEARN_N_BITS_MASK;
622 int src_type = header & NX_LEARN_SRC_MASK;
623 struct mf_subfield src;
624 const uint8_t *src_value;
627 int dst_type = header & NX_LEARN_DST_MASK;
628 struct mf_subfield dst;
637 error = learn_check_header(header, (char *) end - (char *) p);
638 if (error == OFPERR_OFPBAC_BAD_ARGUMENT) {
639 ds_put_format(s, ",***bad flow_mod_spec header %"PRIx16"***)",
642 } else if (error == OFPERR_OFPBAC_BAD_LEN) {
643 ds_put_format(s, ",***flow_mod_spec at offset %td is %u bytes "
644 "long but only %td bytes are left***)",
645 (char *) p - (char *) (learn + 1) - 2,
646 learn_min_len(header) + 2,
647 (char *) end - (char *) p + 2);
652 /* Get the source. */
653 if (src_type == NX_LEARN_SRC_FIELD) {
654 get_subfield(n_bits, &p, &src);
661 src_value_bytes = 2 * DIV_ROUND_UP(n_bits, 16);
663 p = (const void *) ((const uint8_t *) p + src_value_bytes);
666 /* Get the destination. */
667 if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) {
668 get_subfield(n_bits, &p, &dst);
677 switch (src_type | dst_type) {
678 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
679 if (dst.field && dst.ofs == 0 && n_bits == dst.field->n_bits) {
680 union mf_value value;
681 uint8_t *bytes = (uint8_t *) &value;
683 if (src_value_bytes > dst.field->n_bytes) {
684 /* The destination field is an odd number of bytes, which
685 * got rounded up to a multiple of 2 to be put into the
686 * learning action. Skip over the leading byte, which
687 * should be zero anyway. Otherwise the memcpy() below
688 * will overrun the start of 'value'. */
689 int diff = src_value_bytes - dst.field->n_bytes;
691 src_value_bytes -= diff;
694 memset(&value, 0, sizeof value);
695 memcpy(&bytes[dst.field->n_bytes - src_value_bytes],
696 src_value, src_value_bytes);
697 ds_put_format(s, "%s=", dst.field->name);
698 mf_format(dst.field, &value, NULL, s);
700 mf_format_subfield(&dst, s);
701 ds_put_cstr(s, "=0x");
702 for (i = 0; i < src_value_bytes; i++) {
703 ds_put_format(s, "%02"PRIx8, src_value[i]);
708 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
709 mf_format_subfield(&dst, s);
710 if (src.field != dst.field || src.ofs != dst.ofs) {
712 mf_format_subfield(&src, s);
716 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
717 ds_put_cstr(s, "load:0x");
718 for (i = 0; i < src_value_bytes; i++) {
719 ds_put_format(s, "%02"PRIx8, src_value[i]);
721 ds_put_cstr(s, "->");
722 mf_format_subfield(&dst, s);
725 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
726 ds_put_cstr(s, "load:");
727 mf_format_subfield(&src, s);
728 ds_put_cstr(s, "->");
729 mf_format_subfield(&dst, s);
732 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
733 ds_put_cstr(s, "output:");
734 mf_format_subfield(&src, s);
738 if (!is_all_zeros(p, (char *) end - (char *) p)) {
739 ds_put_cstr(s, ",***nonzero trailer***");