2 * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 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"
24 #include "openflow/openflow.h"
25 #include "openvswitch/dynamic-string.h"
26 #include "openvswitch/match.h"
27 #include "openvswitch/meta-flow.h"
28 #include "openvswitch/ofp-actions.h"
29 #include "openvswitch/ofp-errors.h"
30 #include "openvswitch/ofp-util.h"
31 #include "openvswitch/ofpbuf.h"
32 #include "unaligned.h"
35 /* Checks that 'learn' is a valid action on 'flow'. Returns 0 if it is valid,
36 * otherwise an OFPERR_*. */
38 learn_check(const struct ofpact_learn
*learn
, const struct match
*src_match
)
40 const struct ofpact_learn_spec
*spec
;
41 struct match dst_match
;
43 match_init_catchall(&dst_match
);
44 OFPACT_LEARN_SPEC_FOR_EACH (spec
, learn
) {
47 /* Check the source. */
48 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
49 error
= mf_check_src(&spec
->src
, src_match
);
55 /* Check the destination. */
56 switch (spec
->dst_type
) {
57 case NX_LEARN_DST_MATCH
:
58 error
= mf_check_src(&spec
->dst
, &dst_match
);
62 if (spec
->src_type
& NX_LEARN_SRC_IMMEDIATE
) {
63 mf_write_subfield_value(&spec
->dst
,
64 ofpact_learn_spec_imm(spec
),
69 case NX_LEARN_DST_LOAD
:
70 error
= mf_check_dst(&spec
->dst
, &dst_match
);
76 case NX_LEARN_DST_OUTPUT
:
84 /* Composes 'fm' so that executing it will implement 'learn' given that the
85 * packet being processed has 'flow' as its flow.
87 * Uses 'ofpacts' to store the flow mod's actions. The caller must initialize
88 * 'ofpacts' and retains ownership of it. 'fm->ofpacts' will point into the
91 * The caller has to actually execute 'fm'. */
93 learn_execute(const struct ofpact_learn
*learn
, const struct flow
*flow
,
94 struct ofputil_flow_mod
*fm
, struct ofpbuf
*ofpacts
)
96 const struct ofpact_learn_spec
*spec
;
98 match_init_catchall(&fm
->match
);
99 fm
->priority
= learn
->priority
;
100 fm
->cookie
= htonll(0);
101 fm
->cookie_mask
= htonll(0);
102 fm
->new_cookie
= learn
->cookie
;
103 fm
->modify_cookie
= fm
->new_cookie
!= OVS_BE64_MAX
;
104 fm
->table_id
= learn
->table_id
;
105 fm
->command
= OFPFC_MODIFY_STRICT
;
106 fm
->idle_timeout
= learn
->idle_timeout
;
107 fm
->hard_timeout
= learn
->hard_timeout
;
109 fm
->buffer_id
= UINT32_MAX
;
110 fm
->out_port
= OFPP_NONE
;
112 if (learn
->flags
& NX_LEARN_F_SEND_FLOW_REM
) {
113 fm
->flags
|= OFPUTIL_FF_SEND_FLOW_REM
;
118 if (learn
->fin_idle_timeout
|| learn
->fin_hard_timeout
) {
119 struct ofpact_fin_timeout
*oft
;
121 oft
= ofpact_put_FIN_TIMEOUT(ofpacts
);
122 oft
->fin_idle_timeout
= learn
->fin_idle_timeout
;
123 oft
->fin_hard_timeout
= learn
->fin_hard_timeout
;
126 OFPACT_LEARN_SPEC_FOR_EACH (spec
, learn
) {
127 struct ofpact_set_field
*sf
;
128 union mf_subvalue value
;
130 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
131 mf_read_subfield(&spec
->src
, flow
, &value
);
133 mf_subvalue_from_value(&spec
->dst
, &value
,
134 ofpact_learn_spec_imm(spec
));
137 switch (spec
->dst_type
) {
138 case NX_LEARN_DST_MATCH
:
139 mf_write_subfield(&spec
->dst
, &value
, &fm
->match
);
142 case NX_LEARN_DST_LOAD
:
143 sf
= ofpact_put_reg_load(ofpacts
, spec
->dst
.field
, NULL
, NULL
);
144 bitwise_copy(&value
, sizeof value
, 0,
145 sf
->value
, spec
->dst
.field
->n_bytes
, spec
->dst
.ofs
,
147 bitwise_one(ofpact_set_field_mask(sf
), spec
->dst
.field
->n_bytes
,
148 spec
->dst
.ofs
, spec
->n_bits
);
151 case NX_LEARN_DST_OUTPUT
:
152 if (spec
->n_bits
<= 16
153 || is_all_zeros(value
.u8
, sizeof value
- 2)) {
154 ofp_port_t port
= u16_to_ofp(ntohll(value
.integer
));
156 if (ofp_to_u16(port
) < ofp_to_u16(OFPP_MAX
)
157 || port
== OFPP_IN_PORT
158 || port
== OFPP_FLOOD
159 || port
== OFPP_LOCAL
160 || port
== OFPP_ALL
) {
161 ofpact_put_OUTPUT(ofpacts
)->port
= port
;
168 fm
->ofpacts
= ofpacts
->data
;
169 fm
->ofpacts_len
= ofpacts
->size
;
172 /* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
173 * the learn action 'learn'. */
175 learn_mask(const struct ofpact_learn
*learn
, struct flow_wildcards
*wc
)
177 const struct ofpact_learn_spec
*spec
;
178 union mf_subvalue value
;
180 memset(&value
, 0xff, sizeof value
);
181 OFPACT_LEARN_SPEC_FOR_EACH (spec
, learn
) {
182 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
183 mf_write_subfield_flow(&spec
->src
, &value
, &wc
->masks
);
188 /* Returns NULL if successful, otherwise a malloc()'d string describing the
189 * error. The caller is responsible for freeing the returned string. */
190 static char * OVS_WARN_UNUSED_RESULT
191 learn_parse_load_immediate(union mf_subvalue
*imm
, const char *s
,
192 const char *full_s
, struct ofpact_learn_spec
*spec
,
193 struct ofpbuf
*ofpacts
)
195 struct mf_subfield dst
;
198 error
= mf_parse_subfield(&dst
, s
);
202 if (!mf_nxm_header(dst
.field
->id
)) {
203 return xasprintf("%s: experimenter OXM field '%s' not supported",
207 if (!bitwise_is_all_zeros(imm
, sizeof *imm
, dst
.n_bits
,
208 (8 * sizeof *imm
) - dst
.n_bits
)) {
209 return xasprintf("%s: value does not fit into %u bits",
213 spec
->n_bits
= dst
.n_bits
;
214 spec
->src_type
= NX_LEARN_SRC_IMMEDIATE
;
215 spec
->dst_type
= NX_LEARN_DST_LOAD
;
218 /* Push value last, as this may reallocate 'spec'! */
219 unsigned int n_bytes
= DIV_ROUND_UP(dst
.n_bits
, 8);
220 uint8_t *src_imm
= ofpbuf_put_zeros(ofpacts
, OFPACT_ALIGN(n_bytes
));
221 memcpy(src_imm
, &imm
->u8
[sizeof imm
->u8
- n_bytes
], n_bytes
);
226 /* Returns NULL if successful, otherwise a malloc()'d string describing the
227 * error. The caller is responsible for freeing the returned string. */
228 static char * OVS_WARN_UNUSED_RESULT
229 learn_parse_spec(const char *orig
, char *name
, char *value
,
230 struct ofpact_learn_spec
*spec
,
231 struct ofpbuf
*ofpacts
, struct match
*match
)
233 /* Parse destination and check prerequisites. */
234 struct mf_subfield dst
;
237 error
= mf_parse_subfield(&dst
, name
);
239 if (!mf_nxm_header(dst
.field
->id
)) {
240 return xasprintf("%s: experimenter OXM field '%s' not supported",
244 spec
->n_bits
= dst
.n_bits
;
245 spec
->dst_type
= NX_LEARN_DST_MATCH
;
247 /* Parse source and check prerequisites. */
248 if (value
[0] != '\0') {
249 struct mf_subfield src
;
250 error
= mf_parse_subfield(&src
, value
);
253 char *imm_error
= NULL
;
255 /* Try an immediate value. */
256 if (dst
.ofs
== 0 && dst
.n_bits
== dst
.field
->n_bits
) {
257 /* Full field value. */
258 imm_error
= mf_parse_value(dst
.field
, value
, &imm
);
261 /* Partial field value. */
262 if (parse_int_string(value
, (uint8_t *)&imm
,
263 dst
.field
->n_bytes
, &tail
)
265 imm_error
= xasprintf("%s: cannot parse integer value", orig
);
269 !bitwise_is_all_zeros(&imm
, dst
.field
->n_bytes
,
271 dst
.field
->n_bytes
* 8 - dst
.n_bits
)) {
275 mf_format(dst
.field
, &imm
, NULL
, &ds
);
276 imm_error
= xasprintf("%s: value %s does not fit into %d bits",
277 orig
, ds_cstr(&ds
), dst
.n_bits
);
282 char *err
= xasprintf("%s: %s value %s cannot be parsed as a subfield (%s) or an immediate value (%s)",
283 orig
, name
, value
, error
, imm_error
);
289 spec
->src_type
= NX_LEARN_SRC_IMMEDIATE
;
291 /* Update 'match' to allow for satisfying destination
293 mf_write_subfield_value(&dst
, &imm
, match
);
295 /* Push value last, as this may reallocate 'spec'! */
296 unsigned int imm_bytes
= DIV_ROUND_UP(dst
.n_bits
, 8);
297 uint8_t *src_imm
= ofpbuf_put_zeros(ofpacts
,
298 OFPACT_ALIGN(imm_bytes
));
299 memcpy(src_imm
, &imm
, imm_bytes
);
305 if (spec
->src
.n_bits
!= spec
->dst
.n_bits
) {
306 return xasprintf("%s: bit widths of %s (%u) and %s (%u) "
307 "differ", orig
, name
, spec
->src
.n_bits
, value
,
311 spec
->src
= spec
->dst
;
314 spec
->src_type
= NX_LEARN_SRC_FIELD
;
315 } else if (!strcmp(name
, "load")) {
316 union mf_subvalue imm
;
318 char *dst_value
= strstr(value
, "->");
320 if (dst_value
== value
) {
321 return xasprintf("%s: missing source before `->' in `%s'", name
,
325 return xasprintf("%s: missing `->' in `%s'", name
, value
);
328 if (!parse_int_string(value
, imm
.u8
, sizeof imm
.u8
, (char **) &tail
)
330 if (tail
!= dst_value
) {
331 return xasprintf("%s: garbage before `->' in `%s'",
335 char *error
= learn_parse_load_immediate(&imm
, dst_value
+ 2, value
, spec
,
341 struct ofpact_reg_move move
;
344 error
= nxm_parse_reg_move(&move
, value
);
349 spec
->n_bits
= move
.src
.n_bits
;
350 spec
->src_type
= NX_LEARN_SRC_FIELD
;
351 spec
->src
= move
.src
;
352 spec
->dst_type
= NX_LEARN_DST_LOAD
;
353 spec
->dst
= move
.dst
;
355 } else if (!strcmp(name
, "output")) {
356 char *error
= mf_parse_subfield(&spec
->src
, value
);
361 spec
->n_bits
= spec
->src
.n_bits
;
362 spec
->src_type
= NX_LEARN_SRC_FIELD
;
363 spec
->dst_type
= NX_LEARN_DST_OUTPUT
;
365 return xasprintf("%s: unknown keyword %s", orig
, name
);
371 /* Returns NULL if successful, otherwise a malloc()'d string describing the
372 * error. The caller is responsible for freeing the returned string. */
373 static char * OVS_WARN_UNUSED_RESULT
374 learn_parse__(char *orig
, char *arg
, struct ofpbuf
*ofpacts
)
376 struct ofpact_learn
*learn
;
380 learn
= ofpact_put_LEARN(ofpacts
);
381 learn
->idle_timeout
= OFP_FLOW_PERMANENT
;
382 learn
->hard_timeout
= OFP_FLOW_PERMANENT
;
383 learn
->priority
= OFP_DEFAULT_PRIORITY
;
386 match_init_catchall(&match
);
387 while (ofputil_parse_key_value(&arg
, &name
, &value
)) {
388 if (!strcmp(name
, "table")) {
389 learn
->table_id
= atoi(value
);
390 if (learn
->table_id
== 255) {
391 return xasprintf("%s: table id 255 not valid for `learn' "
394 } else if (!strcmp(name
, "priority")) {
395 learn
->priority
= atoi(value
);
396 } else if (!strcmp(name
, "idle_timeout")) {
397 learn
->idle_timeout
= atoi(value
);
398 } else if (!strcmp(name
, "hard_timeout")) {
399 learn
->hard_timeout
= atoi(value
);
400 } else if (!strcmp(name
, "fin_idle_timeout")) {
401 learn
->fin_idle_timeout
= atoi(value
);
402 } else if (!strcmp(name
, "fin_hard_timeout")) {
403 learn
->fin_hard_timeout
= atoi(value
);
404 } else if (!strcmp(name
, "cookie")) {
405 learn
->cookie
= htonll(strtoull(value
, NULL
, 0));
406 } else if (!strcmp(name
, "send_flow_rem")) {
407 learn
->flags
|= NX_LEARN_F_SEND_FLOW_REM
;
408 } else if (!strcmp(name
, "delete_learned")) {
409 learn
->flags
|= NX_LEARN_F_DELETE_LEARNED
;
411 struct ofpact_learn_spec
*spec
;
414 spec
= ofpbuf_put_zeros(ofpacts
, sizeof *spec
);
415 error
= learn_parse_spec(orig
, name
, value
, spec
, ofpacts
, &match
);
419 learn
= ofpacts
->header
;
422 ofpact_finish_LEARN(ofpacts
, &learn
);
427 /* Parses 'arg' as a set of arguments to the "learn" action and appends a
428 * matching OFPACT_LEARN action to 'ofpacts'. ovs-ofctl(8) describes the
431 * Returns NULL if successful, otherwise a malloc()'d string describing the
432 * error. The caller is responsible for freeing the returned string.
434 * If 'flow' is nonnull, then it should be the flow from a struct match that is
435 * the matching rule for the learning action. This helps to better validate
436 * the action's arguments.
439 char * OVS_WARN_UNUSED_RESULT
440 learn_parse(char *arg
, struct ofpbuf
*ofpacts
)
442 char *orig
= xstrdup(arg
);
443 char *error
= learn_parse__(orig
, arg
, ofpacts
);
448 /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
451 learn_format(const struct ofpact_learn
*learn
, struct ds
*s
)
453 const struct ofpact_learn_spec
*spec
;
456 match_init_catchall(&match
);
458 ds_put_format(s
, "%slearn(%s%stable=%s%"PRIu8
,
459 colors
.learn
, colors
.end
, colors
.special
, colors
.end
,
461 if (learn
->idle_timeout
!= OFP_FLOW_PERMANENT
) {
462 ds_put_format(s
, ",%sidle_timeout=%s%"PRIu16
,
463 colors
.param
, colors
.end
, learn
->idle_timeout
);
465 if (learn
->hard_timeout
!= OFP_FLOW_PERMANENT
) {
466 ds_put_format(s
, ",%shard_timeout=%s%"PRIu16
,
467 colors
.param
, colors
.end
, learn
->hard_timeout
);
469 if (learn
->fin_idle_timeout
) {
470 ds_put_format(s
, ",%sfin_idle_timeout=%s%"PRIu16
,
471 colors
.param
, colors
.end
, learn
->fin_idle_timeout
);
473 if (learn
->fin_hard_timeout
) {
474 ds_put_format(s
, "%s,fin_hard_timeout=%s%"PRIu16
,
475 colors
.param
, colors
.end
, learn
->fin_hard_timeout
);
477 if (learn
->priority
!= OFP_DEFAULT_PRIORITY
) {
478 ds_put_format(s
, "%s,priority=%s%"PRIu16
,
479 colors
.special
, colors
.end
, learn
->priority
);
481 if (learn
->flags
& NX_LEARN_F_SEND_FLOW_REM
) {
482 ds_put_format(s
, ",%ssend_flow_rem%s", colors
.value
, colors
.end
);
484 if (learn
->flags
& NX_LEARN_F_DELETE_LEARNED
) {
485 ds_put_format(s
, ",%sdelete_learned%s", colors
.value
, colors
.end
);
487 if (learn
->cookie
!= 0) {
488 ds_put_format(s
, ",%scookie=%s%#"PRIx64
,
489 colors
.param
, colors
.end
, ntohll(learn
->cookie
));
492 OFPACT_LEARN_SPEC_FOR_EACH (spec
, learn
) {
493 unsigned int n_bytes
= DIV_ROUND_UP(spec
->n_bits
, 8);
496 switch (spec
->src_type
| spec
->dst_type
) {
497 case NX_LEARN_SRC_IMMEDIATE
| NX_LEARN_DST_MATCH
: {
498 if (spec
->dst
.ofs
== 0
499 && spec
->dst
.n_bits
== spec
->dst
.field
->n_bits
) {
500 union mf_value value
;
502 memset(&value
, 0, sizeof value
);
503 memcpy(&value
.b
[spec
->dst
.field
->n_bytes
- n_bytes
],
504 ofpact_learn_spec_imm(spec
), n_bytes
);
505 ds_put_format(s
, "%s%s=%s", colors
.param
,
506 spec
->dst
.field
->name
, colors
.end
);
507 mf_format(spec
->dst
.field
, &value
, NULL
, s
);
509 ds_put_format(s
, "%s", colors
.param
);
510 mf_format_subfield(&spec
->dst
, s
);
511 ds_put_format(s
, "=%s", colors
.end
);
512 ds_put_hex(s
, ofpact_learn_spec_imm(spec
), n_bytes
);
516 case NX_LEARN_SRC_FIELD
| NX_LEARN_DST_MATCH
:
517 ds_put_format(s
, "%s", colors
.param
);
518 mf_format_subfield(&spec
->dst
, s
);
519 ds_put_format(s
, "%s", colors
.end
);
520 if (spec
->src
.field
!= spec
->dst
.field
||
521 spec
->src
.ofs
!= spec
->dst
.ofs
) {
522 ds_put_format(s
, "%s=%s", colors
.param
, colors
.end
);
523 mf_format_subfield(&spec
->src
, s
);
527 case NX_LEARN_SRC_IMMEDIATE
| NX_LEARN_DST_LOAD
:
528 ds_put_format(s
, "%sload:%s", colors
.special
, colors
.end
);
529 ds_put_hex(s
, ofpact_learn_spec_imm(spec
), n_bytes
);
530 ds_put_format(s
, "%s->%s", colors
.special
, colors
.end
);
531 mf_format_subfield(&spec
->dst
, s
);
534 case NX_LEARN_SRC_FIELD
| NX_LEARN_DST_LOAD
:
535 ds_put_format(s
, "%sload:%s", colors
.special
, colors
.end
);
536 mf_format_subfield(&spec
->src
, s
);
537 ds_put_format(s
, "%s->%s", colors
.special
, colors
.end
);
538 mf_format_subfield(&spec
->dst
, s
);
541 case NX_LEARN_SRC_FIELD
| NX_LEARN_DST_OUTPUT
:
542 ds_put_format(s
, "%soutput:%s", colors
.special
, colors
.end
);
543 mf_format_subfield(&spec
->src
, s
);
547 ds_put_format(s
, "%s)%s", colors
.learn
, colors
.end
);