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)
128 || !is_all_zeros(learn->pad, sizeof learn->pad)
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->table_id = learn->table_id;
201 fm->command = OFPFC_MODIFY_STRICT;
202 fm->idle_timeout = ntohs(learn->idle_timeout);
203 fm->hard_timeout = ntohs(learn->hard_timeout);
204 fm->buffer_id = UINT32_MAX;
205 fm->out_port = OFPP_NONE;
206 fm->flags = ntohs(learn->flags) & OFPFF_SEND_FLOW_REM;
210 ofpbuf_init(&actions, 64);
212 for (p = learn + 1, end = (char *) learn + ntohs(learn->len); p != end; ) {
213 uint16_t header = ntohs(get_be16(&p));
214 int n_bits = header & NX_LEARN_N_BITS_MASK;
215 int src_type = header & NX_LEARN_SRC_MASK;
216 int dst_type = header & NX_LEARN_DST_MASK;
219 struct nx_action_reg_load *load;
220 struct mf_subfield dst;
226 if (src_type == NX_LEARN_SRC_FIELD) {
227 struct mf_subfield src;
229 get_subfield(n_bits, &p, &src);
230 value = mf_get_subfield(&src, flow);
232 value = get_bits(n_bits, &p);
236 case NX_LEARN_DST_MATCH:
237 get_subfield(n_bits, &p, &dst);
238 mf_set_subfield(&dst, value, &fm->cr);
241 case NX_LEARN_DST_LOAD:
242 get_subfield(n_bits, &p, &dst);
243 load = ofputil_put_NXAST_REG_LOAD(&actions);
244 load->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
245 load->dst = htonl(dst.field->nxm_header);
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 struct mf_subfield src;
288 uint8_t src_imm[sizeof(union mf_value)];
291 struct mf_subfield dst;
295 learn_parse_spec(const char *orig, char *name, char *value,
296 struct learn_spec *spec)
298 memset(spec, 0, sizeof *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;
311 memcpy(spec->src_imm, &imm, dst->n_bytes);
312 spec->dst_type = NX_LEARN_DST_MATCH;
313 spec->dst.field = dst;
315 spec->dst.n_bits = dst->n_bits;
316 } else if (strchr(name, '[')) {
317 /* Parse destination and check prerequisites. */
318 if (mf_parse_subfield(&spec->dst, name)[0] != '\0') {
319 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
323 /* Parse source and check prerequisites. */
324 if (value[0] != '\0') {
325 if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
326 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
329 if (spec->src.n_bits != spec->dst.n_bits) {
330 ovs_fatal(0, "%s: bit widths of %s (%u) and %s (%u) differ",
331 orig, name, spec->src.n_bits, value,
335 spec->src = spec->dst;
338 spec->n_bits = spec->src.n_bits;
339 spec->src_type = NX_LEARN_SRC_FIELD;
340 spec->dst_type = NX_LEARN_DST_MATCH;
341 } else if (!strcmp(name, "load")) {
342 if (value[strcspn(value, "[-")] == '-') {
343 struct nx_action_reg_load load;
344 int nbits, imm_bytes;
348 nxm_parse_reg_load(&load, value);
349 nbits = nxm_decode_n_bits(load.ofs_nbits);
350 imm_bytes = DIV_ROUND_UP(nbits, 8);
351 imm = ntohll(load.value);
353 spec->n_bits = nbits;
354 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
355 for (i = 0; i < imm_bytes; i++) {
356 spec->src_imm[i] = imm >> ((imm_bytes - i - 1) * 8);
358 spec->dst_type = NX_LEARN_DST_LOAD;
359 nxm_decode(&spec->dst, load.dst, load.ofs_nbits);
361 struct nx_action_reg_move move;
363 nxm_parse_reg_move(&move, value);
365 spec->n_bits = ntohs(move.n_bits);
366 spec->src_type = NX_LEARN_SRC_FIELD;
367 nxm_decode_discrete(&spec->src,
368 move.src, move.src_ofs, move.n_bits);
369 spec->dst_type = NX_LEARN_DST_LOAD;
370 nxm_decode_discrete(&spec->dst,
371 move.dst, move.dst_ofs, move.n_bits);
373 } else if (!strcmp(name, "output")) {
374 if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
375 ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
379 spec->n_bits = spec->src.n_bits;
380 spec->src_type = NX_LEARN_SRC_FIELD;
381 spec->dst_type = NX_LEARN_DST_OUTPUT;
383 ovs_fatal(0, "%s: unknown keyword %s", orig, name);
388 learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow)
390 char *orig = xstrdup(arg);
396 struct nx_action_learn *learn;
397 struct cls_rule rule;
400 learn = ofputil_put_NXAST_LEARN(b);
401 learn->idle_timeout = htons(OFP_FLOW_PERMANENT);
402 learn->hard_timeout = htons(OFP_FLOW_PERMANENT);
403 learn->priority = htons(OFP_DEFAULT_PRIORITY);
404 learn->cookie = htonll(0);
405 learn->flags = htons(0);
408 cls_rule_init_catchall(&rule, 0);
409 while (ofputil_parse_key_value(&arg, &name, &value)) {
410 learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn);
411 if (!strcmp(name, "table")) {
412 learn->table_id = atoi(value);
413 if (learn->table_id == 255) {
414 ovs_fatal(0, "%s: table id 255 not valid for `learn' action",
417 } else if (!strcmp(name, "priority")) {
418 learn->priority = htons(atoi(value));
419 } else if (!strcmp(name, "idle_timeout")) {
420 learn->idle_timeout = htons(atoi(value));
421 } else if (!strcmp(name, "hard_timeout")) {
422 learn->hard_timeout = htons(atoi(value));
423 } else if (!strcmp(name, "cookie")) {
424 learn->cookie = htonll(strtoull(value, NULL, 0));
426 struct learn_spec spec;
428 learn_parse_spec(orig, name, value, &spec);
430 /* Check prerequisites. */
431 if (spec.src_type == NX_LEARN_SRC_FIELD
432 && !mf_are_prereqs_ok(spec.src.field, flow)) {
433 ovs_fatal(0, "%s: cannot specify source field %s because "
434 "prerequisites are not satisfied",
435 orig, spec.src.field->name);
437 if ((spec.dst_type == NX_LEARN_DST_MATCH
438 || spec.dst_type == NX_LEARN_DST_LOAD)
439 && !mf_are_prereqs_ok(spec.dst.field, &rule.flow)) {
440 ovs_fatal(0, "%s: cannot specify destination field %s because "
441 "prerequisites are not satisfied",
442 orig, spec.dst.field->name);
445 /* Update 'rule' to allow for satisfying destination
447 if (spec.src_type == NX_LEARN_SRC_IMMEDIATE
448 && spec.dst_type == NX_LEARN_DST_MATCH
450 && spec.n_bits == spec.dst.field->n_bytes * 8) {
453 memcpy(&imm, spec.src_imm, spec.dst.field->n_bytes);
454 mf_set_value(spec.dst.field, &imm, &rule);
457 /* Output the flow_mod_spec. */
458 put_u16(b, spec.n_bits | spec.src_type | spec.dst_type);
459 if (spec.src_type == NX_LEARN_SRC_IMMEDIATE) {
460 int n_bytes = DIV_ROUND_UP(spec.n_bits, 8);
462 ofpbuf_put_zeros(b, 1);
464 ofpbuf_put(b, spec.src_imm, n_bytes);
466 put_u32(b, spec.src.field->nxm_header);
467 put_u16(b, spec.src.ofs);
469 if (spec.dst_type == NX_LEARN_DST_MATCH ||
470 spec.dst_type == NX_LEARN_DST_LOAD) {
471 put_u32(b, spec.dst.field->nxm_header);
472 put_u16(b, spec.dst.ofs);
474 assert(spec.dst_type == NX_LEARN_DST_OUTPUT);
481 len = b->size - learn_ofs;
483 ofpbuf_put_zeros(b, 8 - len % 8);
486 learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn);
487 learn->len = htons(b->size - learn_ofs);
489 /* In theory the above should have caught any errors, but... */
490 error = learn_check(learn, flow);
492 ovs_fatal(0, "%s: %s", orig, ofperr_to_string(error));
498 learn_format(const struct nx_action_learn *learn, struct ds *s)
500 struct cls_rule rule;
503 cls_rule_init_catchall(&rule, 0);
505 ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
506 if (learn->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
507 ds_put_format(s, ",idle_timeout=%"PRIu16, ntohs(learn->idle_timeout));
509 if (learn->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
510 ds_put_format(s, ",hard_timeout=%"PRIu16, ntohs(learn->hard_timeout));
512 if (learn->priority != htons(OFP_DEFAULT_PRIORITY)) {
513 ds_put_format(s, ",priority=%"PRIu16, ntohs(learn->priority));
515 if (learn->flags & htons(OFPFF_SEND_FLOW_REM)) {
516 ds_put_cstr(s, ",OFPFF_SEND_FLOW_REM");
518 if (learn->flags & htons(~OFPFF_SEND_FLOW_REM)) {
519 ds_put_format(s, ",***flags=%"PRIu16"***",
520 ntohs(learn->flags) & ~OFPFF_SEND_FLOW_REM);
522 if (learn->cookie != htonll(0)) {
523 ds_put_format(s, ",cookie=0x%"PRIx64, ntohll(learn->cookie));
525 if (!is_all_zeros(learn->pad, sizeof learn->pad)) {
526 ds_put_cstr(s, ",***nonzero pad***");
529 end = (char *) learn + ntohs(learn->len);
530 for (p = learn + 1; p != end; ) {
531 uint16_t header = ntohs(get_be16(&p));
532 int n_bits = header & NX_LEARN_N_BITS_MASK;
534 int src_type = header & NX_LEARN_SRC_MASK;
535 struct mf_subfield src;
536 const uint8_t *src_value;
539 int dst_type = header & NX_LEARN_DST_MASK;
540 struct mf_subfield dst;
549 error = learn_check_header(header, (char *) end - (char *) p);
550 if (error == OFPERR_OFPBAC_BAD_ARGUMENT) {
551 ds_put_format(s, ",***bad flow_mod_spec header %"PRIx16"***)",
554 } else if (error == OFPERR_OFPBAC_BAD_LEN) {
555 ds_put_format(s, ",***flow_mod_spec at offset %td is %u bytes "
556 "long but only %td bytes are left***)",
557 (char *) p - (char *) (learn + 1) - 2,
558 learn_min_len(header) + 2,
559 (char *) end - (char *) p + 2);
564 /* Get the source. */
565 if (src_type == NX_LEARN_SRC_FIELD) {
566 get_subfield(n_bits, &p, &src);
573 src_value_bytes = 2 * DIV_ROUND_UP(n_bits, 16);
575 p = (const void *) ((const uint8_t *) p + src_value_bytes);
578 /* Get the destination. */
579 if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) {
580 get_subfield(n_bits, &p, &dst);
589 switch (src_type | dst_type) {
590 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
591 if (dst.field && dst.ofs == 0 && n_bits == dst.field->n_bits) {
592 union mf_value value;
593 uint8_t *bytes = (uint8_t *) &value;
595 if (src_value_bytes > dst.field->n_bytes) {
596 /* The destination field is an odd number of bytes, which
597 * got rounded up to a multiple of 2 to be put into the
598 * learning action. Skip over the leading byte, which
599 * should be zero anyway. Otherwise the memcpy() below
600 * will overrun the start of 'value'. */
601 int diff = src_value_bytes - dst.field->n_bytes;
603 src_value_bytes -= diff;
606 memset(&value, 0, sizeof value);
607 memcpy(&bytes[dst.field->n_bytes - src_value_bytes],
608 src_value, src_value_bytes);
609 ds_put_format(s, "%s=", dst.field->name);
610 mf_format(dst.field, &value, NULL, s);
612 mf_format_subfield(&dst, s);
613 ds_put_cstr(s, "=0x");
614 for (i = 0; i < src_value_bytes; i++) {
615 ds_put_format(s, "%02"PRIx8, src_value[i]);
620 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
621 mf_format_subfield(&dst, s);
622 if (src.field != dst.field || src.ofs != dst.ofs) {
624 mf_format_subfield(&src, s);
628 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
629 ds_put_cstr(s, "load:0x");
630 for (i = 0; i < src_value_bytes; i++) {
631 ds_put_format(s, "%02"PRIx8, src_value[i]);
633 ds_put_cstr(s, "->");
634 mf_format_subfield(&dst, s);
637 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
638 ds_put_cstr(s, "load:");
639 mf_format_subfield(&src, s);
640 ds_put_cstr(s, "->");
641 mf_format_subfield(&dst, s);
644 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
645 ds_put_cstr(s, "output:");
646 mf_format_subfield(&src, s);
650 if (!is_all_zeros(p, (char *) end - (char *) p)) {
651 ds_put_cstr(s, ",***nonzero trailer***");