2 * Copyright (c) 2011, 2012, 2013, 2014 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"
24 #include "meta-flow.h"
26 #include "ofp-actions.h"
27 #include "ofp-errors.h"
30 #include "openflow/openflow.h"
31 #include "unaligned.h"
34 get_be16(const void **pp)
36 const ovs_be16 *p = *pp;
43 get_be32(const void **pp)
45 const ovs_be32 *p = *pp;
46 ovs_be32 value = get_unaligned_be32(p);
52 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
54 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
55 sf->ofs = ntohs(get_be16(p));
60 learn_min_len(uint16_t header)
62 int n_bits = header & NX_LEARN_N_BITS_MASK;
63 int src_type = header & NX_LEARN_SRC_MASK;
64 int dst_type = header & NX_LEARN_DST_MASK;
68 if (src_type == NX_LEARN_SRC_FIELD) {
69 min_len += sizeof(ovs_be32); /* src_field */
70 min_len += sizeof(ovs_be16); /* src_ofs */
72 min_len += DIV_ROUND_UP(n_bits, 16);
74 if (dst_type == NX_LEARN_DST_MATCH ||
75 dst_type == NX_LEARN_DST_LOAD) {
76 min_len += sizeof(ovs_be32); /* dst_field */
77 min_len += sizeof(ovs_be16); /* dst_ofs */
82 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
83 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
85 learn_from_openflow(const struct nx_action_learn *nal, struct ofpbuf *ofpacts)
87 struct ofpact_learn *learn;
91 return OFPERR_OFPBAC_BAD_ARGUMENT;
94 learn = ofpact_put_LEARN(ofpacts);
96 learn->idle_timeout = ntohs(nal->idle_timeout);
97 learn->hard_timeout = ntohs(nal->hard_timeout);
98 learn->priority = ntohs(nal->priority);
99 learn->cookie = nal->cookie;
100 learn->table_id = nal->table_id;
101 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
102 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
104 learn->flags = ntohs(nal->flags);
105 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
106 NX_LEARN_F_DELETE_LEARNED)) {
107 return OFPERR_OFPBAC_BAD_ARGUMENT;
110 if (learn->table_id == 0xff) {
111 return OFPERR_OFPBAC_BAD_ARGUMENT;
114 end = (char *) nal + ntohs(nal->len);
115 for (p = nal + 1; p != end; ) {
116 struct ofpact_learn_spec *spec;
117 uint16_t header = ntohs(get_be16(&p));
123 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
124 learn = ofpacts->frame;
127 spec->src_type = header & NX_LEARN_SRC_MASK;
128 spec->dst_type = header & NX_LEARN_DST_MASK;
129 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
131 /* Check for valid src and dst type combination. */
132 if (spec->dst_type == NX_LEARN_DST_MATCH ||
133 spec->dst_type == NX_LEARN_DST_LOAD ||
134 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
135 spec->src_type == NX_LEARN_SRC_FIELD)) {
138 return OFPERR_OFPBAC_BAD_ARGUMENT;
141 /* Check that the arguments don't overrun the end of the action. */
142 if ((char *) end - (char *) p < learn_min_len(header)) {
143 return OFPERR_OFPBAC_BAD_LEN;
146 /* Get the source. */
147 if (spec->src_type == NX_LEARN_SRC_FIELD) {
148 get_subfield(spec->n_bits, &p, &spec->src);
150 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
152 bitwise_copy(p, p_bytes, 0,
153 &spec->src_imm, sizeof spec->src_imm, 0,
155 p = (const uint8_t *) p + p_bytes;
158 /* Get the destination. */
159 if (spec->dst_type == NX_LEARN_DST_MATCH ||
160 spec->dst_type == NX_LEARN_DST_LOAD) {
161 get_subfield(spec->n_bits, &p, &spec->dst);
164 ofpact_update_len(ofpacts, &learn->ofpact);
166 if (!is_all_zeros(p, (char *) end - (char *) p)) {
167 return OFPERR_OFPBAC_BAD_ARGUMENT;
173 /* Checks that 'learn' is a valid action on 'flow'. Returns 0 if it is valid,
174 * otherwise an OFPERR_*. */
176 learn_check(const struct ofpact_learn *learn, const struct flow *flow)
178 const struct ofpact_learn_spec *spec;
181 match_init_catchall(&match);
182 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
185 /* Check the source. */
186 if (spec->src_type == NX_LEARN_SRC_FIELD) {
187 error = mf_check_src(&spec->src, flow);
193 /* Check the destination. */
194 switch (spec->dst_type) {
195 case NX_LEARN_DST_MATCH:
196 error = mf_check_src(&spec->dst, &match.flow);
201 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
204 case NX_LEARN_DST_LOAD:
205 error = mf_check_dst(&spec->dst, &match.flow);
211 case NX_LEARN_DST_OUTPUT:
220 put_be16(struct ofpbuf *b, ovs_be16 x)
222 ofpbuf_put(b, &x, sizeof x);
226 put_be32(struct ofpbuf *b, ovs_be32 x)
228 ofpbuf_put(b, &x, sizeof x);
232 put_u16(struct ofpbuf *b, uint16_t x)
234 put_be16(b, htons(x));
238 put_u32(struct ofpbuf *b, uint32_t x)
240 put_be32(b, htonl(x));
243 /* Converts 'learn' into a "struct nx_action_learn" and appends that action to
246 learn_to_nxast(const struct ofpact_learn *learn, struct ofpbuf *openflow)
248 const struct ofpact_learn_spec *spec;
249 struct nx_action_learn *nal;
252 start_ofs = ofpbuf_size(openflow);
253 nal = ofputil_put_NXAST_LEARN(openflow);
254 nal->idle_timeout = htons(learn->idle_timeout);
255 nal->hard_timeout = htons(learn->hard_timeout);
256 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
257 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
258 nal->priority = htons(learn->priority);
259 nal->cookie = learn->cookie;
260 nal->flags = htons(learn->flags);
261 nal->table_id = learn->table_id;
263 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
264 put_u16(openflow, spec->n_bits | spec->dst_type | spec->src_type);
266 if (spec->src_type == NX_LEARN_SRC_FIELD) {
267 put_u32(openflow, spec->src.field->nxm_header);
268 put_u16(openflow, spec->src.ofs);
270 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
271 uint8_t *bits = ofpbuf_put_zeros(openflow, n_dst_bytes);
272 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
273 bits, n_dst_bytes, 0,
277 if (spec->dst_type == NX_LEARN_DST_MATCH ||
278 spec->dst_type == NX_LEARN_DST_LOAD) {
279 put_u32(openflow, spec->dst.field->nxm_header);
280 put_u16(openflow, spec->dst.ofs);
284 if ((ofpbuf_size(openflow) - start_ofs) % 8) {
285 ofpbuf_put_zeros(openflow, 8 - (ofpbuf_size(openflow) - start_ofs) % 8);
288 nal = ofpbuf_at_assert(openflow, start_ofs, sizeof *nal);
289 nal->len = htons(ofpbuf_size(openflow) - start_ofs);
292 /* Composes 'fm' so that executing it will implement 'learn' given that the
293 * packet being processed has 'flow' as its flow.
295 * Uses 'ofpacts' to store the flow mod's actions. The caller must initialize
296 * 'ofpacts' and retains ownership of it. 'fm->ofpacts' will point into the
299 * The caller has to actually execute 'fm'. */
301 learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
302 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
304 const struct ofpact_learn_spec *spec;
306 match_init_catchall(&fm->match);
307 fm->priority = learn->priority;
308 fm->cookie = htonll(0);
309 fm->cookie_mask = htonll(0);
310 fm->new_cookie = learn->cookie;
311 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
312 fm->table_id = learn->table_id;
313 fm->command = OFPFC_MODIFY_STRICT;
314 fm->idle_timeout = learn->idle_timeout;
315 fm->hard_timeout = learn->hard_timeout;
316 fm->buffer_id = UINT32_MAX;
317 fm->out_port = OFPP_NONE;
319 if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
320 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
324 fm->delete_reason = OFPRR_DELETE;
326 if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
327 struct ofpact_fin_timeout *oft;
329 oft = ofpact_put_FIN_TIMEOUT(ofpacts);
330 oft->fin_idle_timeout = learn->fin_idle_timeout;
331 oft->fin_hard_timeout = learn->fin_hard_timeout;
334 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
335 union mf_subvalue value;
338 if (spec->src_type == NX_LEARN_SRC_FIELD) {
339 mf_read_subfield(&spec->src, flow, &value);
341 value = spec->src_imm;
344 switch (spec->dst_type) {
345 case NX_LEARN_DST_MATCH:
346 mf_write_subfield(&spec->dst, &value, &fm->match);
349 case NX_LEARN_DST_LOAD:
350 for (ofs = 0; ofs < spec->n_bits; ofs += chunk) {
351 struct ofpact_reg_load *load;
353 chunk = MIN(spec->n_bits - ofs, 64);
355 load = ofpact_put_REG_LOAD(ofpacts);
356 load->dst.field = spec->dst.field;
357 load->dst.ofs = spec->dst.ofs + ofs;
358 load->dst.n_bits = chunk;
359 bitwise_copy(&value, sizeof value, ofs,
360 &load->subvalue, sizeof load->subvalue, 0,
365 case NX_LEARN_DST_OUTPUT:
366 if (spec->n_bits <= 16
367 || is_all_zeros(value.u8, sizeof value - 2)) {
368 ofp_port_t port = u16_to_ofp(ntohs(value.be16[7]));
370 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
371 || port == OFPP_IN_PORT
372 || port == OFPP_FLOOD
373 || port == OFPP_LOCAL
374 || port == OFPP_ALL) {
375 ofpact_put_OUTPUT(ofpacts)->port = port;
383 fm->ofpacts = ofpbuf_data(ofpacts);
384 fm->ofpacts_len = ofpbuf_size(ofpacts);
387 /* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
388 * the learn action 'learn'. */
390 learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
392 const struct ofpact_learn_spec *spec;
393 union mf_subvalue value;
395 memset(&value, 0xff, sizeof value);
396 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
397 if (spec->src_type == NX_LEARN_SRC_FIELD) {
398 mf_write_subfield_flow(&spec->src, &value, &wc->masks);
403 /* Returns NULL if successful, otherwise a malloc()'d string describing the
404 * error. The caller is responsible for freeing the returned string. */
405 static char * WARN_UNUSED_RESULT
406 learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
408 const char *full_s = s;
409 const char *arrow = strstr(s, "->");
410 struct mf_subfield dst;
411 union mf_subvalue imm;
414 memset(&imm, 0, sizeof imm);
415 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && arrow) {
416 const char *in = arrow - 1;
417 uint8_t *out = imm.u8 + sizeof imm.u8 - 1;
418 int n = arrow - (s + 2);
421 for (i = 0; i < n; i++) {
422 int hexit = hexit_value(in[-i]);
424 return xasprintf("%s: bad hex digit in value", full_s);
426 out[-(i / 2)] |= i % 2 ? hexit << 4 : hexit;
430 imm.be64[1] = htonll(strtoull(s, (char **) &s, 0));
433 if (strncmp(s, "->", 2)) {
434 return xasprintf("%s: missing `->' following value", full_s);
438 error = mf_parse_subfield(&dst, s);
443 if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
444 (8 * sizeof imm) - dst.n_bits)) {
445 return xasprintf("%s: value does not fit into %u bits",
449 spec->n_bits = dst.n_bits;
450 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
452 spec->dst_type = NX_LEARN_DST_LOAD;
457 /* Returns NULL if successful, otherwise a malloc()'d string describing the
458 * error. The caller is responsible for freeing the returned string. */
459 static char * WARN_UNUSED_RESULT
460 learn_parse_spec(const char *orig, char *name, char *value,
461 struct ofpact_learn_spec *spec)
463 if (mf_from_name(name)) {
464 const struct mf_field *dst = mf_from_name(name);
468 error = mf_parse_value(dst, value, &imm);
473 spec->n_bits = dst->n_bits;
474 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
475 memset(&spec->src_imm, 0, sizeof spec->src_imm);
476 memcpy(&spec->src_imm.u8[sizeof spec->src_imm - dst->n_bytes],
478 spec->dst_type = NX_LEARN_DST_MATCH;
479 spec->dst.field = dst;
481 spec->dst.n_bits = dst->n_bits;
482 } else if (strchr(name, '[')) {
483 /* Parse destination and check prerequisites. */
486 error = mf_parse_subfield(&spec->dst, name);
491 /* Parse source and check prerequisites. */
492 if (value[0] != '\0') {
493 error = mf_parse_subfield(&spec->src, value);
497 if (spec->src.n_bits != spec->dst.n_bits) {
498 return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
499 "differ", orig, name, spec->src.n_bits, value,
503 spec->src = spec->dst;
506 spec->n_bits = spec->src.n_bits;
507 spec->src_type = NX_LEARN_SRC_FIELD;
508 spec->dst_type = NX_LEARN_DST_MATCH;
509 } else if (!strcmp(name, "load")) {
510 if (value[strcspn(value, "[-")] == '-') {
511 char *error = learn_parse_load_immediate(value, spec);
516 struct ofpact_reg_move move;
519 error = nxm_parse_reg_move(&move, value);
524 spec->n_bits = move.src.n_bits;
525 spec->src_type = NX_LEARN_SRC_FIELD;
526 spec->src = move.src;
527 spec->dst_type = NX_LEARN_DST_LOAD;
528 spec->dst = move.dst;
530 } else if (!strcmp(name, "output")) {
531 char *error = mf_parse_subfield(&spec->src, value);
536 spec->n_bits = spec->src.n_bits;
537 spec->src_type = NX_LEARN_SRC_FIELD;
538 spec->dst_type = NX_LEARN_DST_OUTPUT;
540 return xasprintf("%s: unknown keyword %s", orig, name);
546 /* Returns NULL if successful, otherwise a malloc()'d string describing the
547 * error. The caller is responsible for freeing the returned string. */
548 static char * WARN_UNUSED_RESULT
549 learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
551 struct ofpact_learn *learn;
555 learn = ofpact_put_LEARN(ofpacts);
556 learn->idle_timeout = OFP_FLOW_PERMANENT;
557 learn->hard_timeout = OFP_FLOW_PERMANENT;
558 learn->priority = OFP_DEFAULT_PRIORITY;
561 match_init_catchall(&match);
562 while (ofputil_parse_key_value(&arg, &name, &value)) {
563 if (!strcmp(name, "table")) {
564 learn->table_id = atoi(value);
565 if (learn->table_id == 255) {
566 return xasprintf("%s: table id 255 not valid for `learn' "
569 } else if (!strcmp(name, "priority")) {
570 learn->priority = atoi(value);
571 } else if (!strcmp(name, "idle_timeout")) {
572 learn->idle_timeout = atoi(value);
573 } else if (!strcmp(name, "hard_timeout")) {
574 learn->hard_timeout = atoi(value);
575 } else if (!strcmp(name, "fin_idle_timeout")) {
576 learn->fin_idle_timeout = atoi(value);
577 } else if (!strcmp(name, "fin_hard_timeout")) {
578 learn->fin_hard_timeout = atoi(value);
579 } else if (!strcmp(name, "cookie")) {
580 learn->cookie = htonll(strtoull(value, NULL, 0));
581 } else if (!strcmp(name, "send_flow_rem")) {
582 learn->flags |= NX_LEARN_F_SEND_FLOW_REM;
583 } else if (!strcmp(name, "delete_learned")) {
584 learn->flags |= NX_LEARN_F_DELETE_LEARNED;
586 struct ofpact_learn_spec *spec;
589 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
590 learn = ofpacts->frame;
593 error = learn_parse_spec(orig, name, value, spec);
598 /* Update 'match' to allow for satisfying destination
600 if (spec->src_type == NX_LEARN_SRC_IMMEDIATE
601 && spec->dst_type == NX_LEARN_DST_MATCH) {
602 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
606 ofpact_update_len(ofpacts, &learn->ofpact);
611 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
612 * matching OFPACT_LEARN action to 'ofpacts'. ovs-ofctl(8) describes the
615 * Returns NULL if successful, otherwise a malloc()'d string describing the
616 * error. The caller is responsible for freeing the returned string.
618 * If 'flow' is nonnull, then it should be the flow from a struct match that is
619 * the matching rule for the learning action. This helps to better validate
620 * the action's arguments.
623 char * WARN_UNUSED_RESULT
624 learn_parse(char *arg, struct ofpbuf *ofpacts)
626 char *orig = xstrdup(arg);
627 char *error = learn_parse__(orig, arg, ofpacts);
632 /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
635 learn_format(const struct ofpact_learn *learn, struct ds *s)
637 const struct ofpact_learn_spec *spec;
640 match_init_catchall(&match);
642 ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
643 if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
644 ds_put_format(s, ",idle_timeout=%"PRIu16, learn->idle_timeout);
646 if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
647 ds_put_format(s, ",hard_timeout=%"PRIu16, learn->hard_timeout);
649 if (learn->fin_idle_timeout) {
650 ds_put_format(s, ",fin_idle_timeout=%"PRIu16, learn->fin_idle_timeout);
652 if (learn->fin_hard_timeout) {
653 ds_put_format(s, ",fin_hard_timeout=%"PRIu16, learn->fin_hard_timeout);
655 if (learn->priority != OFP_DEFAULT_PRIORITY) {
656 ds_put_format(s, ",priority=%"PRIu16, learn->priority);
658 if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
659 ds_put_cstr(s, ",send_flow_rem");
661 if (learn->flags & NX_LEARN_F_DELETE_LEARNED) {
662 ds_put_cstr(s, ",delete_learned");
664 if (learn->cookie != 0) {
665 ds_put_format(s, ",cookie=%#"PRIx64, ntohll(learn->cookie));
668 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
671 switch (spec->src_type | spec->dst_type) {
672 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
673 if (spec->dst.ofs == 0
674 && spec->dst.n_bits == spec->dst.field->n_bits) {
675 union mf_value value;
677 memset(&value, 0, sizeof value);
678 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
679 &value, spec->dst.field->n_bytes, 0,
680 spec->dst.field->n_bits);
681 ds_put_format(s, "%s=", spec->dst.field->name);
682 mf_format(spec->dst.field, &value, NULL, s);
684 mf_format_subfield(&spec->dst, s);
686 mf_format_subvalue(&spec->src_imm, s);
690 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
691 mf_format_subfield(&spec->dst, s);
692 if (spec->src.field != spec->dst.field ||
693 spec->src.ofs != spec->dst.ofs) {
695 mf_format_subfield(&spec->src, s);
699 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
700 ds_put_format(s, "load:");
701 mf_format_subvalue(&spec->src_imm, s);
702 ds_put_cstr(s, "->");
703 mf_format_subfield(&spec->dst, s);
706 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
707 ds_put_cstr(s, "load:");
708 mf_format_subfield(&spec->src, s);
709 ds_put_cstr(s, "->");
710 mf_format_subfield(&spec->dst, s);
713 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
714 ds_put_cstr(s, "output:");
715 mf_format_subfield(&spec->src, s);