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 /* Checks that 'learn' is a valid action on 'flow'. Returns 0 if it is valid,
35 * otherwise an OFPERR_*. */
37 learn_check(const struct ofpact_learn *learn, const struct flow *flow)
39 const struct ofpact_learn_spec *spec;
42 match_init_catchall(&match);
43 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
46 /* Check the source. */
47 if (spec->src_type == NX_LEARN_SRC_FIELD) {
48 error = mf_check_src(&spec->src, flow);
54 /* Check the destination. */
55 switch (spec->dst_type) {
56 case NX_LEARN_DST_MATCH:
57 error = mf_check_src(&spec->dst, &match.flow);
62 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
65 case NX_LEARN_DST_LOAD:
66 error = mf_check_dst(&spec->dst, &match.flow);
72 case NX_LEARN_DST_OUTPUT:
80 /* Composes 'fm' so that executing it will implement 'learn' given that the
81 * packet being processed has 'flow' as its flow.
83 * Uses 'ofpacts' to store the flow mod's actions. The caller must initialize
84 * 'ofpacts' and retains ownership of it. 'fm->ofpacts' will point into the
87 * The caller has to actually execute 'fm'. */
89 learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
90 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
92 const struct ofpact_learn_spec *spec;
94 match_init_catchall(&fm->match);
95 fm->priority = learn->priority;
96 fm->cookie = htonll(0);
97 fm->cookie_mask = htonll(0);
98 fm->new_cookie = learn->cookie;
99 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
100 fm->table_id = learn->table_id;
101 fm->command = OFPFC_MODIFY_STRICT;
102 fm->idle_timeout = learn->idle_timeout;
103 fm->hard_timeout = learn->hard_timeout;
104 fm->buffer_id = UINT32_MAX;
105 fm->out_port = OFPP_NONE;
107 if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
108 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
112 fm->delete_reason = OFPRR_DELETE;
114 if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
115 struct ofpact_fin_timeout *oft;
117 oft = ofpact_put_FIN_TIMEOUT(ofpacts);
118 oft->fin_idle_timeout = learn->fin_idle_timeout;
119 oft->fin_hard_timeout = learn->fin_hard_timeout;
122 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
123 struct ofpact_set_field *sf;
124 union mf_subvalue value;
126 if (spec->src_type == NX_LEARN_SRC_FIELD) {
127 mf_read_subfield(&spec->src, flow, &value);
129 value = spec->src_imm;
132 switch (spec->dst_type) {
133 case NX_LEARN_DST_MATCH:
134 mf_write_subfield(&spec->dst, &value, &fm->match);
137 case NX_LEARN_DST_LOAD:
138 sf = ofpact_put_reg_load(ofpacts);
139 sf->field = spec->dst.field;
140 bitwise_copy(&value, sizeof value, 0,
141 &sf->value, spec->dst.field->n_bytes, spec->dst.ofs,
143 bitwise_one(&sf->mask, spec->dst.field->n_bytes, spec->dst.ofs,
147 case NX_LEARN_DST_OUTPUT:
148 if (spec->n_bits <= 16
149 || is_all_zeros(value.u8, sizeof value - 2)) {
150 ofp_port_t port = u16_to_ofp(ntohs(value.be16[7]));
152 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
153 || port == OFPP_IN_PORT
154 || port == OFPP_FLOOD
155 || port == OFPP_LOCAL
156 || port == OFPP_ALL) {
157 ofpact_put_OUTPUT(ofpacts)->port = port;
165 fm->ofpacts = ofpbuf_data(ofpacts);
166 fm->ofpacts_len = ofpbuf_size(ofpacts);
169 /* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
170 * the learn action 'learn'. */
172 learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
174 const struct ofpact_learn_spec *spec;
175 union mf_subvalue value;
177 memset(&value, 0xff, sizeof value);
178 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
179 if (spec->src_type == NX_LEARN_SRC_FIELD) {
180 mf_write_subfield_flow(&spec->src, &value, &wc->masks);
185 /* Returns NULL if successful, otherwise a malloc()'d string describing the
186 * error. The caller is responsible for freeing the returned string. */
187 static char * WARN_UNUSED_RESULT
188 learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
190 const char *full_s = s;
191 const char *arrow = strstr(s, "->");
192 struct mf_subfield dst;
193 union mf_subvalue imm;
196 memset(&imm, 0, sizeof imm);
197 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && arrow) {
198 const char *in = arrow - 1;
199 uint8_t *out = imm.u8 + sizeof imm.u8 - 1;
200 int n = arrow - (s + 2);
203 for (i = 0; i < n; i++) {
204 int hexit = hexit_value(in[-i]);
206 return xasprintf("%s: bad hex digit in value", full_s);
208 out[-(i / 2)] |= i % 2 ? hexit << 4 : hexit;
212 imm.be64[1] = htonll(strtoull(s, (char **) &s, 0));
215 if (strncmp(s, "->", 2)) {
216 return xasprintf("%s: missing `->' following value", full_s);
220 error = mf_parse_subfield(&dst, s);
225 if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
226 (8 * sizeof imm) - dst.n_bits)) {
227 return xasprintf("%s: value does not fit into %u bits",
231 spec->n_bits = dst.n_bits;
232 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
234 spec->dst_type = NX_LEARN_DST_LOAD;
239 /* Returns NULL if successful, otherwise a malloc()'d string describing the
240 * error. The caller is responsible for freeing the returned string. */
241 static char * WARN_UNUSED_RESULT
242 learn_parse_spec(const char *orig, char *name, char *value,
243 struct ofpact_learn_spec *spec)
245 if (mf_from_name(name)) {
246 const struct mf_field *dst = mf_from_name(name);
250 error = mf_parse_value(dst, value, &imm);
255 spec->n_bits = dst->n_bits;
256 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
257 memset(&spec->src_imm, 0, sizeof spec->src_imm);
258 memcpy(&spec->src_imm.u8[sizeof spec->src_imm - dst->n_bytes],
260 spec->dst_type = NX_LEARN_DST_MATCH;
261 spec->dst.field = dst;
263 spec->dst.n_bits = dst->n_bits;
264 } else if (strchr(name, '[')) {
265 /* Parse destination and check prerequisites. */
268 error = mf_parse_subfield(&spec->dst, name);
273 /* Parse source and check prerequisites. */
274 if (value[0] != '\0') {
275 error = mf_parse_subfield(&spec->src, value);
279 if (spec->src.n_bits != spec->dst.n_bits) {
280 return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
281 "differ", orig, name, spec->src.n_bits, value,
285 spec->src = spec->dst;
288 spec->n_bits = spec->src.n_bits;
289 spec->src_type = NX_LEARN_SRC_FIELD;
290 spec->dst_type = NX_LEARN_DST_MATCH;
291 } else if (!strcmp(name, "load")) {
292 if (value[strcspn(value, "[-")] == '-') {
293 char *error = learn_parse_load_immediate(value, spec);
298 struct ofpact_reg_move move;
301 error = nxm_parse_reg_move(&move, value);
306 spec->n_bits = move.src.n_bits;
307 spec->src_type = NX_LEARN_SRC_FIELD;
308 spec->src = move.src;
309 spec->dst_type = NX_LEARN_DST_LOAD;
310 spec->dst = move.dst;
312 } else if (!strcmp(name, "output")) {
313 char *error = mf_parse_subfield(&spec->src, value);
318 spec->n_bits = spec->src.n_bits;
319 spec->src_type = NX_LEARN_SRC_FIELD;
320 spec->dst_type = NX_LEARN_DST_OUTPUT;
322 return xasprintf("%s: unknown keyword %s", orig, name);
328 /* Returns NULL if successful, otherwise a malloc()'d string describing the
329 * error. The caller is responsible for freeing the returned string. */
330 static char * WARN_UNUSED_RESULT
331 learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
333 struct ofpact_learn *learn;
337 learn = ofpact_put_LEARN(ofpacts);
338 learn->idle_timeout = OFP_FLOW_PERMANENT;
339 learn->hard_timeout = OFP_FLOW_PERMANENT;
340 learn->priority = OFP_DEFAULT_PRIORITY;
343 match_init_catchall(&match);
344 while (ofputil_parse_key_value(&arg, &name, &value)) {
345 if (!strcmp(name, "table")) {
346 learn->table_id = atoi(value);
347 if (learn->table_id == 255) {
348 return xasprintf("%s: table id 255 not valid for `learn' "
351 } else if (!strcmp(name, "priority")) {
352 learn->priority = atoi(value);
353 } else if (!strcmp(name, "idle_timeout")) {
354 learn->idle_timeout = atoi(value);
355 } else if (!strcmp(name, "hard_timeout")) {
356 learn->hard_timeout = atoi(value);
357 } else if (!strcmp(name, "fin_idle_timeout")) {
358 learn->fin_idle_timeout = atoi(value);
359 } else if (!strcmp(name, "fin_hard_timeout")) {
360 learn->fin_hard_timeout = atoi(value);
361 } else if (!strcmp(name, "cookie")) {
362 learn->cookie = htonll(strtoull(value, NULL, 0));
363 } else if (!strcmp(name, "send_flow_rem")) {
364 learn->flags |= NX_LEARN_F_SEND_FLOW_REM;
365 } else if (!strcmp(name, "delete_learned")) {
366 learn->flags |= NX_LEARN_F_DELETE_LEARNED;
368 struct ofpact_learn_spec *spec;
371 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
372 learn = ofpacts->frame;
375 error = learn_parse_spec(orig, name, value, spec);
380 /* Update 'match' to allow for satisfying destination
382 if (spec->src_type == NX_LEARN_SRC_IMMEDIATE
383 && spec->dst_type == NX_LEARN_DST_MATCH) {
384 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
388 ofpact_update_len(ofpacts, &learn->ofpact);
393 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
394 * matching OFPACT_LEARN action to 'ofpacts'. ovs-ofctl(8) describes the
397 * Returns NULL if successful, otherwise a malloc()'d string describing the
398 * error. The caller is responsible for freeing the returned string.
400 * If 'flow' is nonnull, then it should be the flow from a struct match that is
401 * the matching rule for the learning action. This helps to better validate
402 * the action's arguments.
405 char * WARN_UNUSED_RESULT
406 learn_parse(char *arg, struct ofpbuf *ofpacts)
408 char *orig = xstrdup(arg);
409 char *error = learn_parse__(orig, arg, ofpacts);
414 /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
417 learn_format(const struct ofpact_learn *learn, struct ds *s)
419 const struct ofpact_learn_spec *spec;
422 match_init_catchall(&match);
424 ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
425 if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
426 ds_put_format(s, ",idle_timeout=%"PRIu16, learn->idle_timeout);
428 if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
429 ds_put_format(s, ",hard_timeout=%"PRIu16, learn->hard_timeout);
431 if (learn->fin_idle_timeout) {
432 ds_put_format(s, ",fin_idle_timeout=%"PRIu16, learn->fin_idle_timeout);
434 if (learn->fin_hard_timeout) {
435 ds_put_format(s, ",fin_hard_timeout=%"PRIu16, learn->fin_hard_timeout);
437 if (learn->priority != OFP_DEFAULT_PRIORITY) {
438 ds_put_format(s, ",priority=%"PRIu16, learn->priority);
440 if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
441 ds_put_cstr(s, ",send_flow_rem");
443 if (learn->flags & NX_LEARN_F_DELETE_LEARNED) {
444 ds_put_cstr(s, ",delete_learned");
446 if (learn->cookie != 0) {
447 ds_put_format(s, ",cookie=%#"PRIx64, ntohll(learn->cookie));
450 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
453 switch (spec->src_type | spec->dst_type) {
454 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
455 if (spec->dst.ofs == 0
456 && spec->dst.n_bits == spec->dst.field->n_bits) {
457 union mf_value value;
459 memset(&value, 0, sizeof value);
460 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
461 &value, spec->dst.field->n_bytes, 0,
462 spec->dst.field->n_bits);
463 ds_put_format(s, "%s=", spec->dst.field->name);
464 mf_format(spec->dst.field, &value, NULL, s);
466 mf_format_subfield(&spec->dst, s);
468 mf_format_subvalue(&spec->src_imm, s);
472 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
473 mf_format_subfield(&spec->dst, s);
474 if (spec->src.field != spec->dst.field ||
475 spec->src.ofs != spec->dst.ofs) {
477 mf_format_subfield(&spec->src, s);
481 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
482 ds_put_format(s, "load:");
483 mf_format_subvalue(&spec->src_imm, s);
484 ds_put_cstr(s, "->");
485 mf_format_subfield(&spec->dst, s);
488 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
489 ds_put_cstr(s, "load:");
490 mf_format_subfield(&spec->src, s);
491 ds_put_cstr(s, "->");
492 mf_format_subfield(&spec->dst, s);
495 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
496 ds_put_cstr(s, "output:");
497 mf_format_subfield(&spec->src, s);