2 * Copyright (c) 2011, 2012, 2013, 2014, 2015 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;
105 fm->buffer_id = UINT32_MAX;
106 fm->out_port = OFPP_NONE;
108 if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
109 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
113 fm->delete_reason = OFPRR_DELETE;
115 if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
116 struct ofpact_fin_timeout *oft;
118 oft = ofpact_put_FIN_TIMEOUT(ofpacts);
119 oft->fin_idle_timeout = learn->fin_idle_timeout;
120 oft->fin_hard_timeout = learn->fin_hard_timeout;
123 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
124 struct ofpact_set_field *sf;
125 union mf_subvalue value;
127 if (spec->src_type == NX_LEARN_SRC_FIELD) {
128 mf_read_subfield(&spec->src, flow, &value);
130 value = spec->src_imm;
133 switch (spec->dst_type) {
134 case NX_LEARN_DST_MATCH:
135 mf_write_subfield(&spec->dst, &value, &fm->match);
138 case NX_LEARN_DST_LOAD:
139 sf = ofpact_put_reg_load(ofpacts);
140 sf->field = spec->dst.field;
141 bitwise_copy(&value, sizeof value, 0,
142 &sf->value, spec->dst.field->n_bytes, spec->dst.ofs,
144 bitwise_one(&sf->mask, spec->dst.field->n_bytes, spec->dst.ofs,
148 case NX_LEARN_DST_OUTPUT:
149 if (spec->n_bits <= 16
150 || is_all_zeros(value.u8, sizeof value - 2)) {
151 ofp_port_t port = u16_to_ofp(ntohll(value.integer));
153 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
154 || port == OFPP_IN_PORT
155 || port == OFPP_FLOOD
156 || port == OFPP_LOCAL
157 || port == OFPP_ALL) {
158 ofpact_put_OUTPUT(ofpacts)->port = port;
166 fm->ofpacts = ofpacts->data;
167 fm->ofpacts_len = ofpacts->size;
170 /* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
171 * the learn action 'learn'. */
173 learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
175 const struct ofpact_learn_spec *spec;
176 union mf_subvalue value;
178 memset(&value, 0xff, sizeof value);
179 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
180 if (spec->src_type == NX_LEARN_SRC_FIELD) {
181 mf_write_subfield_flow(&spec->src, &value, &wc->masks);
186 /* Returns NULL if successful, otherwise a malloc()'d string describing the
187 * error. The caller is responsible for freeing the returned string. */
188 static char * OVS_WARN_UNUSED_RESULT
189 learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
191 const char *full_s = s;
192 struct mf_subfield dst;
193 union mf_subvalue imm;
197 err = parse_int_string(s, imm.u8, sizeof imm.u8, (char **) &s);
199 return xasprintf("%s: too many bits in immediate value", full_s);
202 if (strncmp(s, "->", 2)) {
203 return xasprintf("%s: missing `->' following value", full_s);
207 error = mf_parse_subfield(&dst, s);
211 if (!mf_nxm_header(dst.field->id)) {
212 return xasprintf("%s: experimenter OXM field '%s' not supported",
216 if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
217 (8 * sizeof imm) - dst.n_bits)) {
218 return xasprintf("%s: value does not fit into %u bits",
222 spec->n_bits = dst.n_bits;
223 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
225 spec->dst_type = NX_LEARN_DST_LOAD;
230 /* Returns NULL if successful, otherwise a malloc()'d string describing the
231 * error. The caller is responsible for freeing the returned string. */
232 static char * OVS_WARN_UNUSED_RESULT
233 learn_parse_spec(const char *orig, char *name, char *value,
234 struct ofpact_learn_spec *spec)
236 if (mf_from_name(name)) {
237 const struct mf_field *dst = mf_from_name(name);
241 error = mf_parse_value(dst, value, &imm);
246 spec->n_bits = dst->n_bits;
247 spec->src_type = NX_LEARN_SRC_IMMEDIATE;
248 memset(&spec->src_imm, 0, sizeof spec->src_imm);
249 memcpy(&spec->src_imm.u8[sizeof spec->src_imm - dst->n_bytes],
251 spec->dst_type = NX_LEARN_DST_MATCH;
252 spec->dst.field = dst;
254 spec->dst.n_bits = dst->n_bits;
255 } else if (strchr(name, '[')) {
256 /* Parse destination and check prerequisites. */
259 error = mf_parse_subfield(&spec->dst, name);
263 if (!mf_nxm_header(spec->dst.field->id)) {
264 return xasprintf("%s: experimenter OXM field '%s' not supported",
268 /* Parse source and check prerequisites. */
269 if (value[0] != '\0') {
270 error = mf_parse_subfield(&spec->src, value);
274 if (spec->src.n_bits != spec->dst.n_bits) {
275 return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
276 "differ", orig, name, spec->src.n_bits, value,
280 spec->src = spec->dst;
283 spec->n_bits = spec->src.n_bits;
284 spec->src_type = NX_LEARN_SRC_FIELD;
285 spec->dst_type = NX_LEARN_DST_MATCH;
286 } else if (!strcmp(name, "load")) {
287 if (value[strcspn(value, "[-")] == '-') {
288 char *error = learn_parse_load_immediate(value, spec);
293 struct ofpact_reg_move move;
296 error = nxm_parse_reg_move(&move, value);
301 spec->n_bits = move.src.n_bits;
302 spec->src_type = NX_LEARN_SRC_FIELD;
303 spec->src = move.src;
304 spec->dst_type = NX_LEARN_DST_LOAD;
305 spec->dst = move.dst;
307 } else if (!strcmp(name, "output")) {
308 char *error = mf_parse_subfield(&spec->src, value);
313 spec->n_bits = spec->src.n_bits;
314 spec->src_type = NX_LEARN_SRC_FIELD;
315 spec->dst_type = NX_LEARN_DST_OUTPUT;
317 return xasprintf("%s: unknown keyword %s", orig, name);
323 /* Returns NULL if successful, otherwise a malloc()'d string describing the
324 * error. The caller is responsible for freeing the returned string. */
325 static char * OVS_WARN_UNUSED_RESULT
326 learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
328 struct ofpact_learn *learn;
332 learn = ofpact_put_LEARN(ofpacts);
333 learn->idle_timeout = OFP_FLOW_PERMANENT;
334 learn->hard_timeout = OFP_FLOW_PERMANENT;
335 learn->priority = OFP_DEFAULT_PRIORITY;
338 match_init_catchall(&match);
339 while (ofputil_parse_key_value(&arg, &name, &value)) {
340 if (!strcmp(name, "table")) {
341 learn->table_id = atoi(value);
342 if (learn->table_id == 255) {
343 return xasprintf("%s: table id 255 not valid for `learn' "
346 } else if (!strcmp(name, "priority")) {
347 learn->priority = atoi(value);
348 } else if (!strcmp(name, "idle_timeout")) {
349 learn->idle_timeout = atoi(value);
350 } else if (!strcmp(name, "hard_timeout")) {
351 learn->hard_timeout = atoi(value);
352 } else if (!strcmp(name, "fin_idle_timeout")) {
353 learn->fin_idle_timeout = atoi(value);
354 } else if (!strcmp(name, "fin_hard_timeout")) {
355 learn->fin_hard_timeout = atoi(value);
356 } else if (!strcmp(name, "cookie")) {
357 learn->cookie = htonll(strtoull(value, NULL, 0));
358 } else if (!strcmp(name, "send_flow_rem")) {
359 learn->flags |= NX_LEARN_F_SEND_FLOW_REM;
360 } else if (!strcmp(name, "delete_learned")) {
361 learn->flags |= NX_LEARN_F_DELETE_LEARNED;
363 struct ofpact_learn_spec *spec;
366 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
367 learn = ofpacts->header;
370 error = learn_parse_spec(orig, name, value, spec);
375 /* Update 'match' to allow for satisfying destination
377 if (spec->src_type == NX_LEARN_SRC_IMMEDIATE
378 && spec->dst_type == NX_LEARN_DST_MATCH) {
379 mf_write_subfield(&spec->dst, &spec->src_imm, &match);
383 ofpact_update_len(ofpacts, &learn->ofpact);
388 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
389 * matching OFPACT_LEARN action to 'ofpacts'. ovs-ofctl(8) describes the
392 * Returns NULL if successful, otherwise a malloc()'d string describing the
393 * error. The caller is responsible for freeing the returned string.
395 * If 'flow' is nonnull, then it should be the flow from a struct match that is
396 * the matching rule for the learning action. This helps to better validate
397 * the action's arguments.
400 char * OVS_WARN_UNUSED_RESULT
401 learn_parse(char *arg, struct ofpbuf *ofpacts)
403 char *orig = xstrdup(arg);
404 char *error = learn_parse__(orig, arg, ofpacts);
409 /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
412 learn_format(const struct ofpact_learn *learn, struct ds *s)
414 const struct ofpact_learn_spec *spec;
417 match_init_catchall(&match);
419 ds_put_format(s, "learn(table=%"PRIu8, learn->table_id);
420 if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
421 ds_put_format(s, ",idle_timeout=%"PRIu16, learn->idle_timeout);
423 if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
424 ds_put_format(s, ",hard_timeout=%"PRIu16, learn->hard_timeout);
426 if (learn->fin_idle_timeout) {
427 ds_put_format(s, ",fin_idle_timeout=%"PRIu16, learn->fin_idle_timeout);
429 if (learn->fin_hard_timeout) {
430 ds_put_format(s, ",fin_hard_timeout=%"PRIu16, learn->fin_hard_timeout);
432 if (learn->priority != OFP_DEFAULT_PRIORITY) {
433 ds_put_format(s, ",priority=%"PRIu16, learn->priority);
435 if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
436 ds_put_cstr(s, ",send_flow_rem");
438 if (learn->flags & NX_LEARN_F_DELETE_LEARNED) {
439 ds_put_cstr(s, ",delete_learned");
441 if (learn->cookie != 0) {
442 ds_put_format(s, ",cookie=%#"PRIx64, ntohll(learn->cookie));
445 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
448 switch (spec->src_type | spec->dst_type) {
449 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
450 if (spec->dst.ofs == 0
451 && spec->dst.n_bits == spec->dst.field->n_bits) {
452 union mf_value value;
454 memset(&value, 0, sizeof value);
455 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
456 &value, spec->dst.field->n_bytes, 0,
457 spec->dst.field->n_bits);
458 ds_put_format(s, "%s=", spec->dst.field->name);
459 mf_format(spec->dst.field, &value, NULL, s);
461 mf_format_subfield(&spec->dst, s);
463 mf_format_subvalue(&spec->src_imm, s);
467 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
468 mf_format_subfield(&spec->dst, s);
469 if (spec->src.field != spec->dst.field ||
470 spec->src.ofs != spec->dst.ofs) {
472 mf_format_subfield(&spec->src, s);
476 case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD:
477 ds_put_format(s, "load:");
478 mf_format_subvalue(&spec->src_imm, s);
479 ds_put_cstr(s, "->");
480 mf_format_subfield(&spec->dst, s);
483 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
484 ds_put_cstr(s, "load:");
485 mf_format_subfield(&spec->src, s);
486 ds_put_cstr(s, "->");
487 mf_format_subfield(&spec->dst, s);
490 case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
491 ds_put_cstr(s, "output:");
492 mf_format_subfield(&spec->src, s);