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"
27 #include "openflow/openflow.h"
28 #include "unaligned.h"
31 get_be16(const void **pp)
33 const ovs_be16 *p = *pp;
40 get_be32(const void **pp)
42 const ovs_be32 *p = *pp;
43 ovs_be32 value = get_unaligned_be32(p);
49 get_bits(int n_bits, const void **p)
51 int n_segs = DIV_ROUND_UP(n_bits, 16);
55 while (n_segs-- > 0) {
56 value = (value << 16) | ntohs(get_be16(p));
62 learn_min_len(uint16_t header)
64 int n_bits = header & NX_LEARN_N_BITS_MASK;
65 int src_type = header & NX_LEARN_SRC_MASK;
66 int dst_type = header & NX_LEARN_DST_MASK;
70 if (src_type == NX_LEARN_SRC_FIELD) {
71 min_len += sizeof(ovs_be32); /* src_field */
72 min_len += sizeof(ovs_be16); /* src_ofs */
74 min_len += DIV_ROUND_UP(n_bits, 16);
76 if (dst_type == NX_LEARN_DST_MATCH ||
77 dst_type == NX_LEARN_DST_LOAD) {
78 min_len += sizeof(ovs_be32); /* dst_field */
79 min_len += sizeof(ovs_be16); /* dst_ofs */
85 learn_check_header(uint16_t header, size_t len)
87 int src_type = header & NX_LEARN_SRC_MASK;
88 int dst_type = header & NX_LEARN_DST_MASK;
90 /* Check for valid src and dst type combination. */
91 if (dst_type == NX_LEARN_DST_MATCH ||
92 dst_type == NX_LEARN_DST_LOAD ||
93 (dst_type == NX_LEARN_DST_OUTPUT &&
94 src_type == NX_LEARN_SRC_FIELD)) {
97 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
100 /* Check that the arguments don't overrun the end of the action. */
101 if (len < learn_min_len(header)) {
102 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
108 /* Checks that 'learn' (which must be at least 'sizeof *learn' bytes long) is a
109 * valid action on 'flow'. */
111 learn_check(const struct nx_action_learn *learn, const struct flow *flow)
113 struct cls_rule rule;
116 cls_rule_init_catchall(&rule, 0);
118 if (learn->flags & ~htons(OFPFF_SEND_FLOW_REM)
119 || !is_all_zeros(learn->pad, sizeof learn->pad)
120 || learn->table_id == 0xff) {
121 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
124 end = (char *) learn + ntohs(learn->len);
125 for (p = learn + 1; p != end; ) {
126 uint16_t header = ntohs(get_be16(&p));
127 int n_bits = header & NX_LEARN_N_BITS_MASK;
128 int src_type = header & NX_LEARN_SRC_MASK;
129 int dst_type = header & NX_LEARN_DST_MASK;
138 error = learn_check_header(header, (char *) end - (char *) p);
143 /* Check the source. */
144 if (src_type == NX_LEARN_SRC_FIELD) {
145 ovs_be32 src_field = get_be32(&p);
146 int src_ofs = ntohs(get_be16(&p));
148 error = nxm_src_check(src_field, src_ofs, n_bits, flow);
154 value = get_bits(n_bits, &p);
157 /* Check the destination. */
158 if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) {
159 ovs_be32 dst_field = get_be32(&p);
160 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 ofp_mkerr(OFPET_BAD_ACTION, 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->cookie_mask = htonll(UINT64_MAX);
194 fm->table_id = learn->table_id;
195 fm->command = OFPFC_MODIFY_STRICT;
196 fm->idle_timeout = ntohs(learn->idle_timeout);
197 fm->hard_timeout = ntohs(learn->hard_timeout);
198 fm->buffer_id = UINT32_MAX;
199 fm->out_port = OFPP_NONE;
200 fm->flags = ntohs(learn->flags) & OFPFF_SEND_FLOW_REM;
204 ofpbuf_init(&actions, 64);
206 for (p = learn + 1, end = (char *) learn + ntohs(learn->len); p != end; ) {
207 uint16_t header = ntohs(get_be16(&p));
208 int n_bits = header & NX_LEARN_N_BITS_MASK;
209 int src_type = header & NX_LEARN_SRC_MASK;
210 int dst_type = header & NX_LEARN_DST_MASK;
213 struct nx_action_reg_load *load;
221 if (src_type == NX_LEARN_SRC_FIELD) {
222 ovs_be32 src_field = get_be32(&p);
223 int src_ofs = ntohs(get_be16(&p));
225 value = nxm_read_field_bits(src_field,
226 nxm_encode_ofs_nbits(src_ofs, n_bits),
229 value = get_bits(n_bits, &p);
233 case NX_LEARN_DST_MATCH:
234 dst_field = get_be32(&p);
235 dst_ofs = ntohs(get_be16(&p));
236 mf_set_subfield(mf_from_nxm_header(ntohl(dst_field)), value,
237 dst_ofs, n_bits, &fm->cr);
240 case NX_LEARN_DST_LOAD:
241 dst_field = get_be32(&p);
242 dst_ofs = ntohs(get_be16(&p));
243 load = ofputil_put_NXAST_REG_LOAD(&actions);
244 load->ofs_nbits = nxm_encode_ofs_nbits(dst_ofs, n_bits);
245 load->dst = dst_field;
246 load->value = htonll(value);
249 case NX_LEARN_DST_OUTPUT:
250 ofputil_put_OFPAT_OUTPUT(&actions)->port = htons(value);
255 fm->actions = ofpbuf_steal_data(&actions);
256 fm->n_actions = actions.size / sizeof(struct ofp_action_header);
260 put_be16(struct ofpbuf *b, ovs_be16 x)
262 ofpbuf_put(b, &x, sizeof x);
266 put_be32(struct ofpbuf *b, ovs_be32 x)
268 ofpbuf_put(b, &x, sizeof x);
272 put_u16(struct ofpbuf *b, uint16_t x)
274 put_be16(b, htons(x));
278 put_u32(struct ofpbuf *b, uint32_t x)
280 put_be32(b, htonl(x));
287 const struct mf_field *src;
289 uint8_t src_imm[sizeof(union mf_value)];
292 const struct mf_field *dst;
297 learn_parse_spec(const char *orig, char *name, char *value,
298 struct learn_spec *spec)
300 if (mf_from_name(name)) {
301 const struct mf_field *dst = mf_from_name(name);
305 error = mf_parse_value(dst, value, &imm);
307 ovs_fatal(0, "%s", error);
310 spec->n_bits = dst->n_bits;
311 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
314 memcpy(spec->src_imm, &imm, dst->n_bytes);
315 spec->dst_type = NX_LEARN_DST_MATCH;
318 } else if (strchr(name, '[')) {
319 uint32_t src_header, dst_header;
320 int src_ofs, dst_ofs;
323 /* Parse destination and check prerequisites. */
324 if (nxm_parse_field_bits(name, &dst_header, &dst_ofs,
325 &n_bits)[0] != '\0') {
326 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
330 /* Parse source and check prerequisites. */
331 if (value[0] != '\0') {
334 if (nxm_parse_field_bits(value, &src_header, &src_ofs,
335 &src_nbits)[0] != '\0') {
336 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
339 if (src_nbits != n_bits) {
340 ovs_fatal(0, "%s: bit widths of %s (%d) and %s (%d) differ",
341 orig, name, dst_header, value, dst_header);
344 src_header = dst_header;
348 spec->n_bits = n_bits;
349 spec->src_type = NX_LEARN_SRC_FIELD;
350 spec->src = mf_from_nxm_header(src_header);
351 spec->src_ofs = src_ofs;
352 spec->dst_type = NX_LEARN_DST_MATCH;
353 spec->dst = mf_from_nxm_header(dst_header);
355 } else if (!strcmp(name, "load")) {
356 if (value[strcspn(value, "[-")] == '-') {
357 struct nx_action_reg_load load;
358 int nbits, imm_bytes;
362 nxm_parse_reg_load(&load, value);
363 nbits = nxm_decode_n_bits(load.ofs_nbits);
364 imm_bytes = DIV_ROUND_UP(nbits, 8);
365 imm = ntohll(load.value);
367 spec->n_bits = nbits;
368 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
371 for (i = 0; i < imm_bytes; i++) {
372 spec->src_imm[i] = imm >> ((imm_bytes - i - 1) * 8);
374 spec->dst_type = NX_LEARN_DST_LOAD;
375 spec->dst = mf_from_nxm_header(ntohl(load.dst));
376 spec->dst_ofs = nxm_decode_ofs(load.ofs_nbits);
378 struct nx_action_reg_move move;
380 nxm_parse_reg_move(&move, value);
382 spec->n_bits = ntohs(move.n_bits);
383 spec->src_type = NX_LEARN_SRC_FIELD;
384 spec->src = mf_from_nxm_header(ntohl(move.src));
385 spec->src_ofs = ntohs(move.src_ofs);
386 spec->dst_type = NX_LEARN_DST_LOAD;
387 spec->dst = mf_from_nxm_header(ntohl(move.dst));
388 spec->dst_ofs = ntohs(move.dst_ofs);
390 } else if (!strcmp(name, "output")) {
394 if (nxm_parse_field_bits(value, &header, &ofs, &n_bits)[0] != '\0') {
395 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
399 spec->n_bits = n_bits;
400 spec->src_type = NX_LEARN_SRC_FIELD;
401 spec->src = mf_from_nxm_header(header);
403 spec->dst_type = NX_LEARN_DST_OUTPUT;
407 ovs_fatal(0, "%s: unknown keyword %s", orig, name);
412 learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow)
414 char *orig = xstrdup(arg);
420 struct nx_action_learn *learn;
421 struct cls_rule rule;
424 learn = ofputil_put_NXAST_LEARN(b);
425 learn->idle_timeout = htons(OFP_FLOW_PERMANENT);
426 learn->hard_timeout = htons(OFP_FLOW_PERMANENT);
427 learn->priority = htons(OFP_DEFAULT_PRIORITY);
428 learn->cookie = htonll(0);
429 learn->flags = htons(0);
432 cls_rule_init_catchall(&rule, 0);
433 while (ofputil_parse_key_value(&arg, &name, &value)) {
434 learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn);
435 if (!strcmp(name, "table")) {
436 learn->table_id = atoi(value);
437 if (learn->table_id == 255) {
438 ovs_fatal(0, "%s: table id 255 not valid for `learn' action",
441 } else if (!strcmp(name, "priority")) {
442 learn->priority = htons(atoi(value));
443 } else if (!strcmp(name, "idle_timeout")) {
444 learn->idle_timeout = htons(atoi(value));
445 } else if (!strcmp(name, "hard_timeout")) {
446 learn->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 && !mf_are_prereqs_ok(spec.src, flow)) {
457 ovs_fatal(0, "%s: cannot specify source field %s because "
458 "prerequisites are not satisfied",
459 orig, spec.src->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, &rule.flow)) {
464 ovs_fatal(0, "%s: cannot specify destination field %s because "
465 "prerequisites are not satisfied",
466 orig, spec.dst->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->n_bytes * 8) {
477 memcpy(&imm, spec.src_imm, spec.dst->n_bytes);
478 mf_set_value(spec.dst, &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->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->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... */
514 error = learn_check(learn, flow);
516 char *msg = ofputil_error_to_string(error);
517 ovs_fatal(0, "%s: %s", orig, msg);
523 learn_format(const struct nx_action_learn *learn, struct ds *s)
525 struct cls_rule rule;
528 cls_rule_init_catchall(&rule, 0);
530 ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
531 if (learn->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
532 ds_put_format(s, ",idle_timeout=%"PRIu16, ntohs(learn->idle_timeout));
534 if (learn->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
535 ds_put_format(s, ",hard_timeout=%"PRIu16, ntohs(learn->hard_timeout));
537 if (learn->priority != htons(OFP_DEFAULT_PRIORITY)) {
538 ds_put_format(s, ",priority=%"PRIu16, ntohs(learn->priority));
540 if (learn->flags & htons(OFPFF_SEND_FLOW_REM)) {
541 ds_put_cstr(s, ",OFPFF_SEND_FLOW_REM");
543 if (learn->flags & htons(~OFPFF_SEND_FLOW_REM)) {
544 ds_put_format(s, ",***flags=%"PRIu16"***",
545 ntohs(learn->flags) & ~OFPFF_SEND_FLOW_REM);
547 if (learn->cookie != htonll(0)) {
548 ds_put_format(s, ",cookie=0x%"PRIx64, ntohll(learn->cookie));
550 if (!is_all_zeros(learn->pad, sizeof learn->pad)) {
551 ds_put_cstr(s, ",***nonzero pad***");
554 end = (char *) learn + ntohs(learn->len);
555 for (p = learn + 1; p != end; ) {
556 uint16_t header = ntohs(get_be16(&p));
557 int n_bits = header & NX_LEARN_N_BITS_MASK;
559 int src_type = header & NX_LEARN_SRC_MASK;
562 const uint8_t *src_value;
565 int dst_type = header & NX_LEARN_DST_MASK;
568 const struct mf_field *dst_field;
577 error = learn_check_header(header, (char *) end - (char *) p);
578 if (error == ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT)) {
579 ds_put_format(s, ",***bad flow_mod_spec header %"PRIx16"***)",
582 } else if (error == ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN)) {
583 ds_put_format(s, ",***flow_mod_spec at offset %td is %u bytes "
584 "long but only %td bytes are left***)",
585 (char *) p - (char *) (learn + 1) - 2,
586 learn_min_len(header) + 2,
587 (char *) end - (char *) p + 2);
592 /* Get the source. */
593 if (src_type == NX_LEARN_SRC_FIELD) {
594 src_header = ntohl(get_be32(&p));
595 src_ofs = ntohs(get_be16(&p));
601 src_value_bytes = 2 * DIV_ROUND_UP(n_bits, 16);
603 p = (const void *) ((const uint8_t *) p + src_value_bytes);
606 /* Get the destination. */
607 if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) {
608 dst_header = ntohl(get_be32(&p));
609 dst_field = mf_from_nxm_header(dst_header);
610 dst_ofs = ntohs(get_be16(&p));
619 switch (src_type | dst_type) {
620 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
621 if (dst_field && dst_ofs == 0 && n_bits == dst_field->n_bits) {
622 union mf_value value;
623 uint8_t *bytes = (uint8_t *) &value;
625 if (src_value_bytes > dst_field->n_bytes) {
626 /* The destination field is an odd number of bytes, which
627 * got rounded up to a multiple of 2 to be put into the
628 * learning action. Skip over the leading byte, which
629 * should be zero anyway. Otherwise the memcpy() below
630 * will overrun the start of 'value'. */
631 int diff = src_value_bytes - dst_field->n_bytes;
633 src_value_bytes -= diff;
636 memset(&value, 0, sizeof value);
637 memcpy(&bytes[dst_field->n_bytes - src_value_bytes],
638 src_value, src_value_bytes);
639 ds_put_format(s, "%s=", dst_field->name);
640 mf_format(dst_field, &value, NULL, s);
642 nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
643 ds_put_cstr(s, "=0x");
644 for (i = 0; i < src_value_bytes; i++) {
645 ds_put_format(s, "%02"PRIx8, src_value[i]);
650 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
651 nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
652 if (src_header != dst_header || src_ofs != dst_ofs) {
654 nxm_format_field_bits(s, src_header, src_ofs, n_bits);
658 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
659 ds_put_cstr(s, "load:0x");
660 for (i = 0; i < src_value_bytes; i++) {
661 ds_put_format(s, "%02"PRIx8, src_value[i]);
663 ds_put_cstr(s, "->");
664 nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
667 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
668 ds_put_cstr(s, "load:");
669 nxm_format_field_bits(s, src_header, src_ofs, n_bits);
670 ds_put_cstr(s, "->");
671 nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
674 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
675 ds_put_cstr(s, "output:");
676 nxm_format_field_bits(s, src_header, src_ofs, n_bits);
680 if (!is_all_zeros(p, (char *) end - (char *) p)) {
681 ds_put_cstr(s, ",***nonzero trailer***");