2 * Copyright (c) 2011 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 learn_min_len(uint16_t header)
65 int n_bits = header & NX_LEARN_N_BITS_MASK;
66 int src_type = header & NX_LEARN_SRC_MASK;
67 int dst_type = header & NX_LEARN_DST_MASK;
71 if (src_type == NX_LEARN_SRC_FIELD) {
72 min_len += sizeof(ovs_be32); /* src_field */
73 min_len += sizeof(ovs_be16); /* src_ofs */
75 min_len += DIV_ROUND_UP(n_bits, 16);
77 if (dst_type == NX_LEARN_DST_MATCH ||
78 dst_type == NX_LEARN_DST_LOAD) {
79 min_len += sizeof(ovs_be32); /* dst_field */
80 min_len += sizeof(ovs_be16); /* dst_ofs */
86 learn_check_header(uint16_t header, size_t len)
88 int src_type = header & NX_LEARN_SRC_MASK;
89 int dst_type = header & NX_LEARN_DST_MASK;
91 /* Check for valid src and dst type combination. */
92 if (dst_type == NX_LEARN_DST_MATCH ||
93 dst_type == NX_LEARN_DST_LOAD ||
94 (dst_type == NX_LEARN_DST_OUTPUT &&
95 src_type == NX_LEARN_SRC_FIELD)) {
98 return OFPERR_OFPBAC_BAD_ARGUMENT;
101 /* Check that the arguments don't overrun the end of the action. */
102 if (len < learn_min_len(header)) {
103 return OFPERR_OFPBAC_BAD_LEN;
109 /* Checks that 'learn' (which must be at least 'sizeof *learn' bytes long) is a
110 * valid action on 'flow'. */
112 learn_check(const struct nx_action_learn *learn, const struct flow *flow)
114 struct cls_rule rule;
117 cls_rule_init_catchall(&rule, 0);
119 if (learn->flags & ~htons(OFPFF_SEND_FLOW_REM)
120 || !is_all_zeros(learn->pad, sizeof learn->pad)
121 || learn->table_id == 0xff) {
122 return OFPERR_OFPBAC_BAD_ARGUMENT;
125 end = (char *) learn + ntohs(learn->len);
126 for (p = learn + 1; p != end; ) {
127 uint16_t header = ntohs(get_be16(&p));
128 int n_bits = header & NX_LEARN_N_BITS_MASK;
129 int src_type = header & NX_LEARN_SRC_MASK;
130 int dst_type = header & NX_LEARN_DST_MASK;
139 error = learn_check_header(header, (char *) end - (char *) p);
144 /* Check the source. */
145 if (src_type == NX_LEARN_SRC_FIELD) {
146 ovs_be32 src_field = get_be32(&p);
147 int src_ofs = ntohs(get_be16(&p));
149 error = nxm_src_check(src_field, src_ofs, n_bits, flow);
155 value = get_bits(n_bits, &p);
158 /* Check the destination. */
159 if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) {
160 ovs_be32 dst_field = get_be32(&p);
161 int dst_ofs = ntohs(get_be16(&p));
163 error = (dst_type == NX_LEARN_DST_LOAD
164 ? nxm_dst_check(dst_field, dst_ofs, n_bits, &rule.flow)
165 : nxm_src_check(dst_field, dst_ofs, n_bits, &rule.flow));
170 if (dst_type == NX_LEARN_DST_MATCH
171 && src_type == NX_LEARN_SRC_IMMEDIATE) {
172 mf_set_subfield(mf_from_nxm_header(ntohl(dst_field)), value,
173 dst_ofs, n_bits, &rule);
177 if (!is_all_zeros(p, (char *) end - (char *) p)) {
178 return OFPERR_OFPBAC_BAD_ARGUMENT;
185 learn_execute(const struct nx_action_learn *learn, const struct flow *flow,
186 struct ofputil_flow_mod *fm)
189 struct ofpbuf actions;
191 cls_rule_init_catchall(&fm->cr, ntohs(learn->priority));
192 fm->cookie = learn->cookie;
193 fm->table_id = learn->table_id;
194 fm->command = OFPFC_MODIFY_STRICT;
195 fm->idle_timeout = ntohs(learn->idle_timeout);
196 fm->hard_timeout = ntohs(learn->hard_timeout);
197 fm->buffer_id = UINT32_MAX;
198 fm->out_port = OFPP_NONE;
199 fm->flags = ntohs(learn->flags) & OFPFF_SEND_FLOW_REM;
203 ofpbuf_init(&actions, 64);
205 for (p = learn + 1, end = (char *) learn + ntohs(learn->len); p != end; ) {
206 uint16_t header = ntohs(get_be16(&p));
207 int n_bits = header & NX_LEARN_N_BITS_MASK;
208 int src_type = header & NX_LEARN_SRC_MASK;
209 int dst_type = header & NX_LEARN_DST_MASK;
212 struct nx_action_reg_load *load;
220 if (src_type == NX_LEARN_SRC_FIELD) {
221 ovs_be32 src_field = get_be32(&p);
222 int src_ofs = ntohs(get_be16(&p));
224 value = nxm_read_field_bits(src_field,
225 nxm_encode_ofs_nbits(src_ofs, n_bits),
228 value = get_bits(n_bits, &p);
232 case NX_LEARN_DST_MATCH:
233 dst_field = get_be32(&p);
234 dst_ofs = ntohs(get_be16(&p));
235 mf_set_subfield(mf_from_nxm_header(ntohl(dst_field)), value,
236 dst_ofs, n_bits, &fm->cr);
239 case NX_LEARN_DST_LOAD:
240 dst_field = get_be32(&p);
241 dst_ofs = ntohs(get_be16(&p));
242 load = ofputil_put_NXAST_REG_LOAD(&actions);
243 load->ofs_nbits = nxm_encode_ofs_nbits(dst_ofs, n_bits);
244 load->dst = dst_field;
245 load->value = htonll(value);
248 case NX_LEARN_DST_OUTPUT:
249 ofputil_put_OFPAT_OUTPUT(&actions)->port = htons(value);
254 fm->actions = ofpbuf_steal_data(&actions);
255 fm->n_actions = actions.size / sizeof(struct ofp_action_header);
259 put_be16(struct ofpbuf *b, ovs_be16 x)
261 ofpbuf_put(b, &x, sizeof x);
265 put_be32(struct ofpbuf *b, ovs_be32 x)
267 ofpbuf_put(b, &x, sizeof x);
271 put_u16(struct ofpbuf *b, uint16_t x)
273 put_be16(b, htons(x));
277 put_u32(struct ofpbuf *b, uint32_t x)
279 put_be32(b, htonl(x));
286 const struct mf_field *src;
288 uint8_t src_imm[sizeof(union mf_value)];
291 const struct mf_field *dst;
296 learn_parse_spec(const char *orig, char *name, char *value,
297 struct learn_spec *spec)
299 if (mf_from_name(name)) {
300 const struct mf_field *dst = mf_from_name(name);
304 error = mf_parse_value(dst, value, &imm);
306 ovs_fatal(0, "%s", error);
309 spec->n_bits = dst->n_bits;
310 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
313 memcpy(spec->src_imm, &imm, dst->n_bytes);
314 spec->dst_type = NX_LEARN_DST_MATCH;
317 } else if (strchr(name, '[')) {
318 uint32_t src_header, dst_header;
319 int src_ofs, dst_ofs;
322 /* Parse destination and check prerequisites. */
323 if (nxm_parse_field_bits(name, &dst_header, &dst_ofs,
324 &n_bits)[0] != '\0') {
325 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
329 /* Parse source and check prerequisites. */
330 if (value[0] != '\0') {
333 if (nxm_parse_field_bits(value, &src_header, &src_ofs,
334 &src_nbits)[0] != '\0') {
335 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
338 if (src_nbits != n_bits) {
339 ovs_fatal(0, "%s: bit widths of %s (%d) and %s (%d) differ",
340 orig, name, dst_header, value, dst_header);
343 src_header = dst_header;
347 spec->n_bits = n_bits;
348 spec->src_type = NX_LEARN_SRC_FIELD;
349 spec->src = mf_from_nxm_header(src_header);
350 spec->src_ofs = src_ofs;
351 spec->dst_type = NX_LEARN_DST_MATCH;
352 spec->dst = mf_from_nxm_header(dst_header);
354 } else if (!strcmp(name, "load")) {
355 if (value[strcspn(value, "[-")] == '-') {
356 struct nx_action_reg_load load;
357 int nbits, imm_bytes;
361 nxm_parse_reg_load(&load, value);
362 nbits = nxm_decode_n_bits(load.ofs_nbits);
363 imm_bytes = DIV_ROUND_UP(nbits, 8);
364 imm = ntohll(load.value);
366 spec->n_bits = nbits;
367 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
370 for (i = 0; i < imm_bytes; i++) {
371 spec->src_imm[i] = imm >> ((imm_bytes - i - 1) * 8);
373 spec->dst_type = NX_LEARN_DST_LOAD;
374 spec->dst = mf_from_nxm_header(ntohl(load.dst));
375 spec->dst_ofs = nxm_decode_ofs(load.ofs_nbits);
377 struct nx_action_reg_move move;
379 nxm_parse_reg_move(&move, value);
381 spec->n_bits = ntohs(move.n_bits);
382 spec->src_type = NX_LEARN_SRC_FIELD;
383 spec->src = mf_from_nxm_header(ntohl(move.src));
384 spec->src_ofs = ntohs(move.src_ofs);
385 spec->dst_type = NX_LEARN_DST_LOAD;
386 spec->dst = mf_from_nxm_header(ntohl(move.dst));
387 spec->dst_ofs = ntohs(move.dst_ofs);
389 } else if (!strcmp(name, "output")) {
393 if (nxm_parse_field_bits(value, &header, &ofs, &n_bits)[0] != '\0') {
394 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
398 spec->n_bits = n_bits;
399 spec->src_type = NX_LEARN_SRC_FIELD;
400 spec->src = mf_from_nxm_header(header);
402 spec->dst_type = NX_LEARN_DST_OUTPUT;
406 ovs_fatal(0, "%s: unknown keyword %s", orig, name);
411 learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow)
413 char *orig = xstrdup(arg);
419 struct nx_action_learn *learn;
420 struct cls_rule rule;
423 learn = ofputil_put_NXAST_LEARN(b);
424 learn->idle_timeout = htons(OFP_FLOW_PERMANENT);
425 learn->hard_timeout = htons(OFP_FLOW_PERMANENT);
426 learn->priority = htons(OFP_DEFAULT_PRIORITY);
427 learn->cookie = htonll(0);
428 learn->flags = htons(0);
431 cls_rule_init_catchall(&rule, 0);
432 while (ofputil_parse_key_value(&arg, &name, &value)) {
433 learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn);
434 if (!strcmp(name, "table")) {
435 learn->table_id = atoi(value);
436 if (learn->table_id == 255) {
437 ovs_fatal(0, "%s: table id 255 not valid for `learn' action",
440 } else if (!strcmp(name, "priority")) {
441 learn->priority = htons(atoi(value));
442 } else if (!strcmp(name, "idle_timeout")) {
443 learn->idle_timeout = htons(atoi(value));
444 } else if (!strcmp(name, "hard_timeout")) {
445 learn->hard_timeout = htons(atoi(value));
446 } else if (!strcmp(name, "cookie")) {
447 learn->cookie = htonll(strtoull(value, NULL, 0));
449 struct learn_spec spec;
451 learn_parse_spec(orig, name, value, &spec);
453 /* Check prerequisites. */
454 if (spec.src_type == NX_LEARN_SRC_FIELD
455 && !mf_are_prereqs_ok(spec.src, flow)) {
456 ovs_fatal(0, "%s: cannot specify source field %s because "
457 "prerequisites are not satisfied",
458 orig, spec.src->name);
460 if ((spec.dst_type == NX_LEARN_DST_MATCH
461 || spec.dst_type == NX_LEARN_DST_LOAD)
462 && !mf_are_prereqs_ok(spec.dst, &rule.flow)) {
463 ovs_fatal(0, "%s: cannot specify destination field %s because "
464 "prerequisites are not satisfied",
465 orig, spec.dst->name);
468 /* Update 'rule' to allow for satisfying destination
470 if (spec.src_type == NX_LEARN_SRC_IMMEDIATE
471 && spec.dst_type == NX_LEARN_DST_MATCH
473 && spec.n_bits == spec.dst->n_bytes * 8) {
476 memcpy(&imm, spec.src_imm, spec.dst->n_bytes);
477 mf_set_value(spec.dst, &imm, &rule);
480 /* Output the flow_mod_spec. */
481 put_u16(b, spec.n_bits | spec.src_type | spec.dst_type);
482 if (spec.src_type == NX_LEARN_SRC_IMMEDIATE) {
483 int n_bytes = DIV_ROUND_UP(spec.n_bits, 8);
485 ofpbuf_put_zeros(b, 1);
487 ofpbuf_put(b, spec.src_imm, n_bytes);
489 put_u32(b, spec.src->nxm_header);
490 put_u16(b, spec.src_ofs);
492 if (spec.dst_type == NX_LEARN_DST_MATCH ||
493 spec.dst_type == NX_LEARN_DST_LOAD) {
494 put_u32(b, spec.dst->nxm_header);
495 put_u16(b, spec.dst_ofs);
497 assert(spec.dst_type == NX_LEARN_DST_OUTPUT);
504 len = b->size - learn_ofs;
506 ofpbuf_put_zeros(b, 8 - len % 8);
509 learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn);
510 learn->len = htons(b->size - learn_ofs);
512 /* In theory the above should have caught any errors, but... */
513 error = learn_check(learn, flow);
515 ovs_fatal(0, "%s: %s", orig, ofperr_to_string(error));
521 learn_format(const struct nx_action_learn *learn, struct ds *s)
523 struct cls_rule rule;
526 cls_rule_init_catchall(&rule, 0);
528 ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
529 if (learn->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
530 ds_put_format(s, ",idle_timeout=%"PRIu16, ntohs(learn->idle_timeout));
532 if (learn->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
533 ds_put_format(s, ",hard_timeout=%"PRIu16, ntohs(learn->hard_timeout));
535 if (learn->priority != htons(OFP_DEFAULT_PRIORITY)) {
536 ds_put_format(s, ",priority=%"PRIu16, ntohs(learn->priority));
538 if (learn->flags & htons(OFPFF_SEND_FLOW_REM)) {
539 ds_put_cstr(s, ",OFPFF_SEND_FLOW_REM");
541 if (learn->flags & htons(~OFPFF_SEND_FLOW_REM)) {
542 ds_put_format(s, ",***flags=%"PRIu16"***",
543 ntohs(learn->flags) & ~OFPFF_SEND_FLOW_REM);
545 if (learn->cookie != htonll(0)) {
546 ds_put_format(s, ",cookie=0x%"PRIx64, ntohll(learn->cookie));
548 if (!is_all_zeros(learn->pad, sizeof learn->pad)) {
549 ds_put_cstr(s, ",***nonzero pad***");
552 end = (char *) learn + ntohs(learn->len);
553 for (p = learn + 1; p != end; ) {
554 uint16_t header = ntohs(get_be16(&p));
555 int n_bits = header & NX_LEARN_N_BITS_MASK;
557 int src_type = header & NX_LEARN_SRC_MASK;
560 const uint8_t *src_value;
563 int dst_type = header & NX_LEARN_DST_MASK;
566 const struct mf_field *dst_field;
575 error = learn_check_header(header, (char *) end - (char *) p);
576 if (error == OFPERR_OFPBAC_BAD_ARGUMENT) {
577 ds_put_format(s, ",***bad flow_mod_spec header %"PRIx16"***)",
580 } else if (error == OFPERR_OFPBAC_BAD_LEN) {
581 ds_put_format(s, ",***flow_mod_spec at offset %td is %u bytes "
582 "long but only %td bytes are left***)",
583 (char *) p - (char *) (learn + 1) - 2,
584 learn_min_len(header) + 2,
585 (char *) end - (char *) p + 2);
590 /* Get the source. */
591 if (src_type == NX_LEARN_SRC_FIELD) {
592 src_header = ntohl(get_be32(&p));
593 src_ofs = ntohs(get_be16(&p));
599 src_value_bytes = 2 * DIV_ROUND_UP(n_bits, 16);
601 p = (const void *) ((const uint8_t *) p + src_value_bytes);
604 /* Get the destination. */
605 if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) {
606 dst_header = ntohl(get_be32(&p));
607 dst_field = mf_from_nxm_header(dst_header);
608 dst_ofs = ntohs(get_be16(&p));
617 switch (src_type | dst_type) {
618 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
619 if (dst_field && dst_ofs == 0 && n_bits == dst_field->n_bits) {
620 union mf_value value;
621 uint8_t *bytes = (uint8_t *) &value;
623 if (src_value_bytes > dst_field->n_bytes) {
624 /* The destination field is an odd number of bytes, which
625 * got rounded up to a multiple of 2 to be put into the
626 * learning action. Skip over the leading byte, which
627 * should be zero anyway. Otherwise the memcpy() below
628 * will overrun the start of 'value'. */
629 int diff = src_value_bytes - dst_field->n_bytes;
631 src_value_bytes -= diff;
634 memset(&value, 0, sizeof value);
635 memcpy(&bytes[dst_field->n_bytes - src_value_bytes],
636 src_value, src_value_bytes);
637 ds_put_format(s, "%s=", dst_field->name);
638 mf_format(dst_field, &value, NULL, s);
640 nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
641 ds_put_cstr(s, "=0x");
642 for (i = 0; i < src_value_bytes; i++) {
643 ds_put_format(s, "%02"PRIx8, src_value[i]);
648 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
649 nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
650 if (src_header != dst_header || src_ofs != dst_ofs) {
652 nxm_format_field_bits(s, src_header, src_ofs, n_bits);
656 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
657 ds_put_cstr(s, "load:0x");
658 for (i = 0; i < src_value_bytes; i++) {
659 ds_put_format(s, "%02"PRIx8, src_value[i]);
661 ds_put_cstr(s, "->");
662 nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
665 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
666 ds_put_cstr(s, "load:");
667 nxm_format_field_bits(s, src_header, src_ofs, n_bits);
668 ds_put_cstr(s, "->");
669 nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
672 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
673 ds_put_cstr(s, "output:");
674 nxm_format_field_bits(s, src_header, src_ofs, n_bits);
678 if (!is_all_zeros(p, (char *) end - (char *) p)) {
679 ds_put_cstr(s, ",***nonzero trailer***");