2 * Copyright (c) 2011, 2012 Nicira Networks.
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) {
180 mf_set_subfield(&dst, value, &rule);
184 if (!is_all_zeros(p, (char *) end - (char *) p)) {
185 return OFPERR_OFPBAC_BAD_ARGUMENT;
192 learn_execute(const struct nx_action_learn *learn, const struct flow *flow,
193 struct ofputil_flow_mod *fm)
196 struct ofpbuf actions;
198 cls_rule_init_catchall(&fm->cr, ntohs(learn->priority));
199 fm->cookie = learn->cookie;
200 fm->cookie_mask = htonll(UINT64_MAX);
201 fm->table_id = learn->table_id;
202 fm->command = OFPFC_MODIFY_STRICT;
203 fm->idle_timeout = ntohs(learn->idle_timeout);
204 fm->hard_timeout = ntohs(learn->hard_timeout);
205 fm->buffer_id = UINT32_MAX;
206 fm->out_port = OFPP_NONE;
207 fm->flags = ntohs(learn->flags) & OFPFF_SEND_FLOW_REM;
211 ofpbuf_init(&actions, 64);
213 if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
214 struct nx_action_fin_timeout *naft;
216 naft = ofputil_put_NXAST_FIN_TIMEOUT(&actions);
217 naft->fin_idle_timeout = learn->fin_idle_timeout;
218 naft->fin_hard_timeout = learn->fin_hard_timeout;
221 for (p = learn + 1, end = (char *) learn + ntohs(learn->len); p != end; ) {
222 uint16_t header = ntohs(get_be16(&p));
223 int n_bits = header & NX_LEARN_N_BITS_MASK;
224 int src_type = header & NX_LEARN_SRC_MASK;
225 int dst_type = header & NX_LEARN_DST_MASK;
228 struct nx_action_reg_load *load;
229 struct mf_subfield dst;
235 if (src_type == NX_LEARN_SRC_FIELD) {
236 struct mf_subfield src;
238 get_subfield(n_bits, &p, &src);
239 value = mf_get_subfield(&src, flow);
241 value = get_bits(n_bits, &p);
245 case NX_LEARN_DST_MATCH:
246 get_subfield(n_bits, &p, &dst);
247 mf_set_subfield(&dst, value, &fm->cr);
250 case NX_LEARN_DST_LOAD:
251 get_subfield(n_bits, &p, &dst);
252 load = ofputil_put_NXAST_REG_LOAD(&actions);
253 load->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
254 load->dst = htonl(dst.field->nxm_header);
255 load->value = htonll(value);
258 case NX_LEARN_DST_OUTPUT:
259 ofputil_put_OFPAT10_OUTPUT(&actions)->port = htons(value);
264 fm->actions = ofpbuf_steal_data(&actions);
265 fm->n_actions = actions.size / sizeof(struct ofp_action_header);
269 put_be16(struct ofpbuf *b, ovs_be16 x)
271 ofpbuf_put(b, &x, sizeof x);
275 put_be32(struct ofpbuf *b, ovs_be32 x)
277 ofpbuf_put(b, &x, sizeof x);
281 put_u16(struct ofpbuf *b, uint16_t x)
283 put_be16(b, htons(x));
287 put_u32(struct ofpbuf *b, uint32_t x)
289 put_be32(b, htonl(x));
296 struct mf_subfield src;
297 uint8_t src_imm[sizeof(union mf_value)];
300 struct mf_subfield dst;
304 learn_parse_spec(const char *orig, char *name, char *value,
305 struct learn_spec *spec)
307 memset(spec, 0, sizeof *spec);
308 if (mf_from_name(name)) {
309 const struct mf_field *dst = mf_from_name(name);
313 error = mf_parse_value(dst, value, &imm);
315 ovs_fatal(0, "%s", error);
318 spec->n_bits = dst->n_bits;
319 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
320 memcpy(spec->src_imm, &imm, dst->n_bytes);
321 spec->dst_type = NX_LEARN_DST_MATCH;
322 spec->dst.field = dst;
324 spec->dst.n_bits = dst->n_bits;
325 } else if (strchr(name, '[')) {
326 /* Parse destination and check prerequisites. */
327 if (mf_parse_subfield(&spec->dst, name)[0] != '\0') {
328 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
332 /* Parse source and check prerequisites. */
333 if (value[0] != '\0') {
334 if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
335 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
338 if (spec->src.n_bits != spec->dst.n_bits) {
339 ovs_fatal(0, "%s: bit widths of %s (%u) and %s (%u) differ",
340 orig, name, spec->src.n_bits, value,
344 spec->src = spec->dst;
347 spec->n_bits = spec->src.n_bits;
348 spec->src_type = NX_LEARN_SRC_FIELD;
349 spec->dst_type = NX_LEARN_DST_MATCH;
350 } else if (!strcmp(name, "load")) {
351 if (value[strcspn(value, "[-")] == '-') {
352 struct nx_action_reg_load load;
353 int nbits, imm_bytes;
357 nxm_parse_reg_load(&load, value);
358 nbits = nxm_decode_n_bits(load.ofs_nbits);
359 imm_bytes = DIV_ROUND_UP(nbits, 8);
360 imm = ntohll(load.value);
362 spec->n_bits = nbits;
363 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
364 for (i = 0; i < imm_bytes; i++) {
365 spec->src_imm[i] = imm >> ((imm_bytes - i - 1) * 8);
367 spec->dst_type = NX_LEARN_DST_LOAD;
368 nxm_decode(&spec->dst, load.dst, load.ofs_nbits);
370 struct nx_action_reg_move move;
372 nxm_parse_reg_move(&move, value);
374 spec->n_bits = ntohs(move.n_bits);
375 spec->src_type = NX_LEARN_SRC_FIELD;
376 nxm_decode_discrete(&spec->src,
377 move.src, move.src_ofs, move.n_bits);
378 spec->dst_type = NX_LEARN_DST_LOAD;
379 nxm_decode_discrete(&spec->dst,
380 move.dst, move.dst_ofs, move.n_bits);
382 } else if (!strcmp(name, "output")) {
383 if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
384 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
388 spec->n_bits = spec->src.n_bits;
389 spec->src_type = NX_LEARN_SRC_FIELD;
390 spec->dst_type = NX_LEARN_DST_OUTPUT;
392 ovs_fatal(0, "%s: unknown keyword %s", orig, name);
396 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
397 * matching NXAST_LEARN action to 'b'. The format parsed is described in
400 * Prints an error on stderr and aborts the program if 'arg' syntax is invalid.
402 * If 'flow' is nonnull, then it should be the flow from a cls_rule that is
403 * the matching rule for the learning action. This helps to better validate
404 * the action's arguments.
408 learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow)
410 char *orig = xstrdup(arg);
416 struct nx_action_learn *learn;
417 struct cls_rule rule;
420 learn = ofputil_put_NXAST_LEARN(b);
421 learn->idle_timeout = htons(OFP_FLOW_PERMANENT);
422 learn->hard_timeout = htons(OFP_FLOW_PERMANENT);
423 learn->priority = htons(OFP_DEFAULT_PRIORITY);
424 learn->cookie = htonll(0);
425 learn->flags = htons(0);
428 cls_rule_init_catchall(&rule, 0);
429 while (ofputil_parse_key_value(&arg, &name, &value)) {
430 learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn);
431 if (!strcmp(name, "table")) {
432 learn->table_id = atoi(value);
433 if (learn->table_id == 255) {
434 ovs_fatal(0, "%s: table id 255 not valid for `learn' action",
437 } else if (!strcmp(name, "priority")) {
438 learn->priority = htons(atoi(value));
439 } else if (!strcmp(name, "idle_timeout")) {
440 learn->idle_timeout = htons(atoi(value));
441 } else if (!strcmp(name, "hard_timeout")) {
442 learn->hard_timeout = htons(atoi(value));
443 } else if (!strcmp(name, "fin_idle_timeout")) {
444 learn->fin_idle_timeout = htons(atoi(value));
445 } else if (!strcmp(name, "fin_hard_timeout")) {
446 learn->fin_hard_timeout = htons(atoi(value));
447 } else if (!strcmp(name, "cookie")) {
448 learn->cookie = htonll(strtoull(value, NULL, 0));
450 struct learn_spec spec;
452 learn_parse_spec(orig, name, value, &spec);
454 /* Check prerequisites. */
455 if (spec.src_type == NX_LEARN_SRC_FIELD
456 && flow && !mf_are_prereqs_ok(spec.src.field, flow)) {
457 ovs_fatal(0, "%s: cannot specify source field %s because "
458 "prerequisites are not satisfied",
459 orig, spec.src.field->name);
461 if ((spec.dst_type == NX_LEARN_DST_MATCH
462 || spec.dst_type == NX_LEARN_DST_LOAD)
463 && !mf_are_prereqs_ok(spec.dst.field, &rule.flow)) {
464 ovs_fatal(0, "%s: cannot specify destination field %s because "
465 "prerequisites are not satisfied",
466 orig, spec.dst.field->name);
469 /* Update 'rule' to allow for satisfying destination
471 if (spec.src_type == NX_LEARN_SRC_IMMEDIATE
472 && spec.dst_type == NX_LEARN_DST_MATCH
474 && spec.n_bits == spec.dst.field->n_bytes * 8) {
477 memcpy(&imm, spec.src_imm, spec.dst.field->n_bytes);
478 mf_set_value(spec.dst.field, &imm, &rule);
481 /* Output the flow_mod_spec. */
482 put_u16(b, spec.n_bits | spec.src_type | spec.dst_type);
483 if (spec.src_type == NX_LEARN_SRC_IMMEDIATE) {
484 int n_bytes = DIV_ROUND_UP(spec.n_bits, 8);
486 ofpbuf_put_zeros(b, 1);
488 ofpbuf_put(b, spec.src_imm, n_bytes);
490 put_u32(b, spec.src.field->nxm_header);
491 put_u16(b, spec.src.ofs);
493 if (spec.dst_type == NX_LEARN_DST_MATCH ||
494 spec.dst_type == NX_LEARN_DST_LOAD) {
495 put_u32(b, spec.dst.field->nxm_header);
496 put_u16(b, spec.dst.ofs);
498 assert(spec.dst_type == NX_LEARN_DST_OUTPUT);
505 len = b->size - learn_ofs;
507 ofpbuf_put_zeros(b, 8 - len % 8);
510 learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn);
511 learn->len = htons(b->size - learn_ofs);
513 /* In theory the above should have caught any errors, but... */
515 error = learn_check(learn, flow);
517 ovs_fatal(0, "%s: %s", orig, ofperr_to_string(error));
524 learn_format(const struct nx_action_learn *learn, struct ds *s)
526 struct cls_rule rule;
529 cls_rule_init_catchall(&rule, 0);
531 ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
532 if (learn->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
533 ds_put_format(s, ",idle_timeout=%"PRIu16, ntohs(learn->idle_timeout));
535 if (learn->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
536 ds_put_format(s, ",hard_timeout=%"PRIu16, ntohs(learn->hard_timeout));
538 if (learn->fin_idle_timeout) {
539 ds_put_format(s, ",fin_idle_timeout=%"PRIu16,
540 ntohs(learn->fin_idle_timeout));
542 if (learn->fin_hard_timeout) {
543 ds_put_format(s, ",fin_hard_timeout=%"PRIu16,
544 ntohs(learn->fin_hard_timeout));
546 if (learn->priority != htons(OFP_DEFAULT_PRIORITY)) {
547 ds_put_format(s, ",priority=%"PRIu16, ntohs(learn->priority));
549 if (learn->flags & htons(OFPFF_SEND_FLOW_REM)) {
550 ds_put_cstr(s, ",OFPFF_SEND_FLOW_REM");
552 if (learn->flags & htons(~OFPFF_SEND_FLOW_REM)) {
553 ds_put_format(s, ",***flags=%"PRIu16"***",
554 ntohs(learn->flags) & ~OFPFF_SEND_FLOW_REM);
556 if (learn->cookie != htonll(0)) {
557 ds_put_format(s, ",cookie=0x%"PRIx64, ntohll(learn->cookie));
559 if (learn->pad != 0) {
560 ds_put_cstr(s, ",***nonzero pad***");
563 end = (char *) learn + ntohs(learn->len);
564 for (p = learn + 1; p != end; ) {
565 uint16_t header = ntohs(get_be16(&p));
566 int n_bits = header & NX_LEARN_N_BITS_MASK;
568 int src_type = header & NX_LEARN_SRC_MASK;
569 struct mf_subfield src;
570 const uint8_t *src_value;
573 int dst_type = header & NX_LEARN_DST_MASK;
574 struct mf_subfield dst;
583 error = learn_check_header(header, (char *) end - (char *) p);
584 if (error == OFPERR_OFPBAC_BAD_ARGUMENT) {
585 ds_put_format(s, ",***bad flow_mod_spec header %"PRIx16"***)",
588 } else if (error == OFPERR_OFPBAC_BAD_LEN) {
589 ds_put_format(s, ",***flow_mod_spec at offset %td is %u bytes "
590 "long but only %td bytes are left***)",
591 (char *) p - (char *) (learn + 1) - 2,
592 learn_min_len(header) + 2,
593 (char *) end - (char *) p + 2);
598 /* Get the source. */
599 if (src_type == NX_LEARN_SRC_FIELD) {
600 get_subfield(n_bits, &p, &src);
607 src_value_bytes = 2 * DIV_ROUND_UP(n_bits, 16);
609 p = (const void *) ((const uint8_t *) p + src_value_bytes);
612 /* Get the destination. */
613 if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) {
614 get_subfield(n_bits, &p, &dst);
623 switch (src_type | dst_type) {
624 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
625 if (dst.field && dst.ofs == 0 && n_bits == dst.field->n_bits) {
626 union mf_value value;
627 uint8_t *bytes = (uint8_t *) &value;
629 if (src_value_bytes > dst.field->n_bytes) {
630 /* The destination field is an odd number of bytes, which
631 * got rounded up to a multiple of 2 to be put into the
632 * learning action. Skip over the leading byte, which
633 * should be zero anyway. Otherwise the memcpy() below
634 * will overrun the start of 'value'. */
635 int diff = src_value_bytes - dst.field->n_bytes;
637 src_value_bytes -= diff;
640 memset(&value, 0, sizeof value);
641 memcpy(&bytes[dst.field->n_bytes - src_value_bytes],
642 src_value, src_value_bytes);
643 ds_put_format(s, "%s=", dst.field->name);
644 mf_format(dst.field, &value, NULL, s);
646 mf_format_subfield(&dst, s);
647 ds_put_cstr(s, "=0x");
648 for (i = 0; i < src_value_bytes; i++) {
649 ds_put_format(s, "%02"PRIx8, src_value[i]);
654 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
655 mf_format_subfield(&dst, s);
656 if (src.field != dst.field || src.ofs != dst.ofs) {
658 mf_format_subfield(&src, s);
662 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
663 ds_put_cstr(s, "load:0x");
664 for (i = 0; i < src_value_bytes; i++) {
665 ds_put_format(s, "%02"PRIx8, src_value[i]);
667 ds_put_cstr(s, "->");
668 mf_format_subfield(&dst, s);
671 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
672 ds_put_cstr(s, "load:");
673 mf_format_subfield(&src, s);
674 ds_put_cstr(s, "->");
675 mf_format_subfield(&dst, s);
678 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
679 ds_put_cstr(s, "output:");
680 mf_format_subfield(&src, s);
684 if (!is_all_zeros(p, (char *) end - (char *) p)) {
685 ds_put_cstr(s, ",***nonzero trailer***");