2 * Copyright (c) 2008-2017 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.
18 #include <netinet/in.h>
21 #include "byte-order.h"
25 #include "openvswitch/hmap.h"
27 #include "multipath.h"
29 #include "odp-netlink.h"
30 #include "openvswitch/dynamic-string.h"
31 #include "openvswitch/meta-flow.h"
32 #include "openvswitch/ofp-actions.h"
33 #include "openvswitch/ofp-util.h"
34 #include "openvswitch/ofp-parse.h"
35 #include "openvswitch/ofp-prop.h"
36 #include "openvswitch/ofpbuf.h"
37 #include "openvswitch/vlog.h"
38 #include "unaligned.h"
40 #include "vl-mff-map.h"
42 VLOG_DEFINE_THIS_MODULE(ofp_actions
);
44 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
46 struct ofp_action_header
;
48 /* Header for Open vSwitch and ONF vendor extension actions.
50 * This is the entire header for a few Open vSwitch vendor extension actions,
51 * the ones that either have no arguments or for which variable-length
52 * arguments follow the header.
54 * This cannot be used as an entirely generic vendor extension action header,
55 * because OpenFlow does not specify the location or size of the action
56 * subtype; it just happens that ONF extensions and Nicira extensions share
58 struct ext_action_header
{
59 ovs_be16 type
; /* OFPAT_VENDOR. */
60 ovs_be16 len
; /* At least 16. */
61 ovs_be32 vendor
; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
62 ovs_be16 subtype
; /* See enum ofp_raw_action_type. */
65 OFP_ASSERT(sizeof(struct ext_action_header
) == 16);
67 /* Raw identifiers for OpenFlow actions.
69 * Decoding and encoding OpenFlow actions across multiple versions is difficult
70 * to do in a clean, consistent way. This enumeration lays out all of the
71 * forms of actions that Open vSwitch supports.
73 * The comments here must follow a stylized form because the
74 * "extract-ofp-actions" program parses them at build time to generate data
77 * - The first part of each comment specifies the vendor, OpenFlow versions,
78 * and type for each protocol that supports the action:
80 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
81 * extension actions. (Support for other vendors can be added, but
82 * it can't be done just based on a vendor ID definition alone
83 * because OpenFlow doesn't define a standard way to specify a
84 * subtype for vendor actions, so other vendors might do it different
87 * # The version can specify a specific OpenFlow version, a version
88 * range delimited by "-", or an open-ended range with "+".
90 * # The type, in parentheses, is the action type number (for standard
91 * OpenFlow actions) or subtype (for vendor extension actions).
93 * # Optionally one may add "is deprecated" followed by a
94 * human-readable reason in parentheses (which will be used in log
95 * messages), if a particular action should no longer be used.
97 * Multiple such specifications may be separated by commas.
99 * - The second part describes the action's wire format. It may be:
101 * # "struct <name>": The struct fully specifies the wire format. The
102 * action is exactly the size of the struct. (Thus, the struct must
103 * be an exact multiple of 8 bytes in size.)
105 * # "struct <name>, ...": The struct specifies the beginning of the
106 * wire format. An instance of the action is either the struct's
107 * exact size, or a multiple of 8 bytes longer.
109 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
110 * vendor extension) header, followed by 0 or more pad bytes to align
111 * to a multiple of <N> bits, followed by an argument of the given
112 * type, followed by 0 or more pad bytes to bring the total action up
113 * to a multiple of 8 bytes.
115 * # "void": The action is just a (standard or vendor extension)
118 * # Optionally, one may add "VLMFF" in the end of the second part if
119 * the Openflow action may use a variable length meta-flow field
120 * (i.e. tun_metadata). Adding "VLMFF" will pass the per-switch based
121 * variable length meta-flow field mapping map (struct vl_mff_map) to
122 * the corresponding action decoding function.
124 * - Optional additional text enclosed in square brackets is commentary for
127 enum ofp_raw_action_type
{
128 /* ## ----------------- ## */
129 /* ## Standard actions. ## */
130 /* ## ----------------- ## */
132 /* OF1.0(0): struct ofp10_action_output. */
134 /* OF1.1+(0): struct ofp11_action_output. */
137 /* OF1.0(1): uint16_t. */
138 OFPAT_RAW10_SET_VLAN_VID
,
139 /* OF1.0(2): uint8_t. */
140 OFPAT_RAW10_SET_VLAN_PCP
,
142 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
144 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
145 * modification actions: the 1.0 versions push a VLAN header if none is
146 * present, but the 1.1 versions do not. That is the only reason that we
147 * distinguish their raw action types.] */
148 OFPAT_RAW11_SET_VLAN_VID
,
149 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
150 OFPAT_RAW11_SET_VLAN_PCP
,
152 /* OF1.1+(17): ovs_be16.
154 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
156 OFPAT_RAW11_PUSH_VLAN
,
158 /* OF1.0(3): void. */
159 OFPAT_RAW10_STRIP_VLAN
,
160 /* OF1.1+(18): void. */
161 OFPAT_RAW11_POP_VLAN
,
163 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
164 * ofp_action_dl_addr. */
165 OFPAT_RAW_SET_DL_SRC
,
167 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
168 * ofp_action_dl_addr. */
169 OFPAT_RAW_SET_DL_DST
,
171 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
173 OFPAT_RAW_SET_NW_SRC
,
175 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
177 OFPAT_RAW_SET_NW_DST
,
179 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
180 OFPAT_RAW_SET_NW_TOS
,
182 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
183 OFPAT_RAW11_SET_NW_ECN
,
185 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
187 OFPAT_RAW_SET_TP_SRC
,
189 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
191 OFPAT_RAW_SET_TP_DST
,
193 /* OF1.0(11): struct ofp10_action_enqueue. */
196 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
198 OFPAT_RAW_SET_MPLS_LABEL
,
200 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
202 OFPAT_RAW_SET_MPLS_TC
,
204 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
206 OFPAT_RAW_SET_MPLS_TTL
,
208 /* NX1.0(26), OF1.1+(16): void. */
209 OFPAT_RAW_DEC_MPLS_TTL
,
211 /* NX1.0(23), OF1.1+(19): ovs_be16.
213 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
216 /* NX1.0(24), OF1.1+(20): ovs_be16.
218 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
219 * ETH_TYPE_MPLS otherwise, not the label.] */
222 /* NX1.0(4), OF1.1+(21): uint32_t. */
225 /* NX1.0(40), OF1.1+(22): uint32_t. */
228 /* OF1.1+(23): uint8_t. */
229 OFPAT_RAW11_SET_NW_TTL
,
231 /* NX1.0(18), OF1.1+(24): void. */
232 OFPAT_RAW_DEC_NW_TTL
,
233 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
234 NXAST_RAW_DEC_TTL_CNT_IDS
,
236 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... VLMFF */
237 OFPAT_RAW12_SET_FIELD
,
238 /* OF1.5+(25): struct ofp12_action_set_field, ... VLMFF */
239 OFPAT_RAW15_SET_FIELD
,
240 /* NX1.0-1.4(7): struct nx_action_reg_load. VLMFF
242 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
243 * we drop reg_load.] */
245 /* NX1.0-1.4(33): struct ext_action_header, ... VLMFF
247 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
248 * we drop reg_load2.] */
251 /* OF1.5+(28): struct ofp15_action_copy_field, ... VLMFF */
252 OFPAT_RAW15_COPY_FIELD
,
253 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... VLMFF */
254 ONFACT_RAW13_COPY_FIELD
,
255 /* NX1.0-1.4(6): struct nx_action_reg_move, ... VLMFF */
258 /* ## ------------------------- ## */
259 /* ## Nicira extension actions. ## */
260 /* ## ------------------------- ## */
262 /* Actions similar to standard actions are listed with the standard actions. */
264 /* NX1.0+(1): uint16_t. */
266 /* NX1.0+(14): struct nx_action_resubmit. */
267 NXAST_RAW_RESUBMIT_TABLE
,
268 /* NX1.0+(44): struct nx_action_resubmit. */
269 NXAST_RAW_RESUBMIT_TABLE_CT
,
271 /* NX1.0+(2): uint32_t. */
272 NXAST_RAW_SET_TUNNEL
,
273 /* NX1.0+(9): uint64_t. */
274 NXAST_RAW_SET_TUNNEL64
,
276 /* NX1.0+(5): void. */
279 /* NX1.0+(8): struct nx_action_note, ... */
282 /* NX1.0+(10): struct nx_action_multipath. VLMFF */
285 /* NX1.0+(12): struct nx_action_bundle, ... */
287 /* NX1.0+(13): struct nx_action_bundle, ... VLMFF */
288 NXAST_RAW_BUNDLE_LOAD
,
290 /* NX1.0+(15): struct nx_action_output_reg. VLMFF */
291 NXAST_RAW_OUTPUT_REG
,
292 /* NX1.0+(32): struct nx_action_output_reg2. VLMFF */
293 NXAST_RAW_OUTPUT_REG2
,
295 /* NX1.0+(16): struct nx_action_learn, ... VLMFF */
297 /* NX1.0+(45): struct nx_action_learn2, ... VLMFF */
300 /* NX1.0+(17): void. */
303 /* NX1.0+(19): struct nx_action_fin_timeout. */
304 NXAST_RAW_FIN_TIMEOUT
,
306 /* NX1.0+(20): struct nx_action_controller. */
307 NXAST_RAW_CONTROLLER
,
308 /* NX1.0+(37): struct ext_action_header, ... */
309 NXAST_RAW_CONTROLLER2
,
311 /* NX1.0+(22): struct nx_action_write_metadata. */
312 NXAST_RAW_WRITE_METADATA
,
314 /* NX1.0+(27): struct nx_action_stack. VLMFF */
315 NXAST_RAW_STACK_PUSH
,
317 /* NX1.0+(28): struct nx_action_stack. VLMFF */
320 /* NX1.0+(29): struct nx_action_sample. */
322 /* NX1.0+(38): struct nx_action_sample2. */
324 /* NX1.0+(41): struct nx_action_sample2. */
327 /* NX1.0+(34): struct nx_action_conjunction. */
328 NXAST_RAW_CONJUNCTION
,
330 /* NX1.0+(35): struct nx_action_conntrack, ... VLMFF */
333 /* NX1.0+(36): struct nx_action_nat, ... */
336 /* NX1.0+(39): struct nx_action_output_trunc. */
337 NXAST_RAW_OUTPUT_TRUNC
,
339 /* NX1.0+(42): struct ext_action_header, ... VLMFF */
342 /* NX1.0+(43): void. */
345 /* ## ------------------ ## */
346 /* ## Debugging actions. ## */
347 /* ## ------------------ ## */
349 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
350 /* accepts them only if started with --enable-dummy. */
352 /* NX1.0+(255): void. */
353 NXAST_RAW_DEBUG_RECIRC
,
356 /* OpenFlow actions are always a multiple of 8 bytes in length. */
357 #define OFP_ACTION_ALIGN 8
359 /* Define a few functions for working with instructions. */
360 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
361 static inline const struct STRUCT * OVS_UNUSED \
362 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
364 ovs_assert(inst->type == htons(ENUM)); \
365 return ALIGNED_CAST(struct STRUCT *, inst); \
368 static inline void OVS_UNUSED \
369 instruction_init_##ENUM(struct STRUCT *s) \
371 memset(s, 0, sizeof *s); \
372 s->type = htons(ENUM); \
373 s->len = htons(sizeof *s); \
376 static inline struct STRUCT * OVS_UNUSED \
377 instruction_put_##ENUM(struct ofpbuf *buf) \
379 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
380 instruction_init_##ENUM(s); \
386 static void ofpacts_update_instruction_actions(struct ofpbuf
*openflow
,
388 static void pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
);
390 static enum ofperr
ofpacts_verify(const struct ofpact
[], size_t ofpacts_len
,
391 uint32_t allowed_ovsinsts
,
392 enum ofpact_type outer_action
);
394 static void put_set_field(struct ofpbuf
*openflow
, enum ofp_version
,
395 enum mf_field_id
, uint64_t value
);
397 static void put_reg_load(struct ofpbuf
*openflow
,
398 const struct mf_subfield
*, uint64_t value
);
400 static enum ofperr
ofpact_pull_raw(struct ofpbuf
*, enum ofp_version
,
401 enum ofp_raw_action_type
*, uint64_t *arg
);
402 static void *ofpact_put_raw(struct ofpbuf
*, enum ofp_version
,
403 enum ofp_raw_action_type
, uint64_t arg
);
405 static char *OVS_WARN_UNUSED_RESULT
ofpacts_parse(
406 char *str
, struct ofpbuf
*ofpacts
, enum ofputil_protocol
*usable_protocols
,
407 bool allow_instructions
, enum ofpact_type outer_action
);
408 static enum ofperr
ofpacts_pull_openflow_actions__(
409 struct ofpbuf
*openflow
, unsigned int actions_len
,
410 enum ofp_version version
, uint32_t allowed_ovsinsts
,
411 struct ofpbuf
*ofpacts
, enum ofpact_type outer_action
,
412 const struct vl_mff_map
*vl_mff_map
, uint64_t *ofpacts_tlv_bitmap
);
413 static char * OVS_WARN_UNUSED_RESULT
ofpacts_parse_copy(
414 const char *s_
, struct ofpbuf
*ofpacts
,
415 enum ofputil_protocol
*usable_protocols
,
416 bool allow_instructions
, enum ofpact_type outer_action
);
418 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
419 * nested ofpacts it returns the first one. */
421 ofpact_next_flattened(const struct ofpact
*ofpact
)
423 switch (ofpact
->type
) {
426 case OFPACT_CONTROLLER
:
428 case OFPACT_OUTPUT_REG
:
429 case OFPACT_OUTPUT_TRUNC
:
431 case OFPACT_SET_FIELD
:
432 case OFPACT_SET_VLAN_VID
:
433 case OFPACT_SET_VLAN_PCP
:
434 case OFPACT_STRIP_VLAN
:
435 case OFPACT_PUSH_VLAN
:
436 case OFPACT_SET_ETH_SRC
:
437 case OFPACT_SET_ETH_DST
:
438 case OFPACT_SET_IPV4_SRC
:
439 case OFPACT_SET_IPV4_DST
:
440 case OFPACT_SET_IP_DSCP
:
441 case OFPACT_SET_IP_ECN
:
442 case OFPACT_SET_IP_TTL
:
443 case OFPACT_SET_L4_SRC_PORT
:
444 case OFPACT_SET_L4_DST_PORT
:
445 case OFPACT_REG_MOVE
:
446 case OFPACT_STACK_PUSH
:
447 case OFPACT_STACK_POP
:
449 case OFPACT_SET_MPLS_LABEL
:
450 case OFPACT_SET_MPLS_TC
:
451 case OFPACT_SET_MPLS_TTL
:
452 case OFPACT_DEC_MPLS_TTL
:
453 case OFPACT_PUSH_MPLS
:
454 case OFPACT_POP_MPLS
:
455 case OFPACT_SET_TUNNEL
:
456 case OFPACT_SET_QUEUE
:
457 case OFPACT_POP_QUEUE
:
458 case OFPACT_FIN_TIMEOUT
:
459 case OFPACT_RESUBMIT
:
461 case OFPACT_CONJUNCTION
:
462 case OFPACT_MULTIPATH
:
466 case OFPACT_UNROLL_XLATE
:
467 case OFPACT_CT_CLEAR
:
468 case OFPACT_DEBUG_RECIRC
:
470 case OFPACT_CLEAR_ACTIONS
:
471 case OFPACT_WRITE_METADATA
:
472 case OFPACT_GOTO_TABLE
:
474 return ofpact_next(ofpact
);
477 return ofpact_get_CLONE(ofpact
)->actions
;
480 return ofpact_get_CT(ofpact
)->actions
;
482 case OFPACT_WRITE_ACTIONS
:
483 return ofpact_get_WRITE_ACTIONS(ofpact
)->actions
;
489 /* Pull off existing actions or instructions. Used by nesting actions to keep
490 * ofpacts_parse() oblivious of actions nesting.
492 * Push the actions back on after nested parsing, e.g.:
494 * size_t ofs = ofpacts_pull(ofpacts);
495 * ...nested parsing...
496 * ofpbuf_push_uninit(ofpacts, ofs);
499 ofpacts_pull(struct ofpbuf
*ofpacts
)
504 ofpbuf_pull(ofpacts
, ofs
);
509 #include "ofp-actions.inc1"
511 /* Output actions. */
513 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
514 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
515 * number of bytes to send. A 'max_len' of zero means no bytes of the
516 * packet should be sent. */
517 struct ofp10_action_output
{
518 ovs_be16 type
; /* OFPAT10_OUTPUT. */
519 ovs_be16 len
; /* Length is 8. */
520 ovs_be16 port
; /* Output port. */
521 ovs_be16 max_len
; /* Max length to send to controller. */
523 OFP_ASSERT(sizeof(struct ofp10_action_output
) == 8);
525 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
526 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
527 * number of bytes to send. A 'max_len' of zero means no bytes of the
528 * packet should be sent.*/
529 struct ofp11_action_output
{
530 ovs_be16 type
; /* OFPAT11_OUTPUT. */
531 ovs_be16 len
; /* Length is 16. */
532 ovs_be32 port
; /* Output port. */
533 ovs_be16 max_len
; /* Max length to send to controller. */
534 uint8_t pad
[6]; /* Pad to 64 bits. */
536 OFP_ASSERT(sizeof(struct ofp11_action_output
) == 16);
539 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output
*oao
,
540 enum ofp_version ofp_version OVS_UNUSED
,
543 struct ofpact_output
*output
;
545 output
= ofpact_put_OUTPUT(out
);
546 output
->port
= u16_to_ofp(ntohs(oao
->port
));
547 output
->max_len
= ntohs(oao
->max_len
);
549 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
553 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output
*oao
,
554 enum ofp_version ofp_version OVS_UNUSED
,
557 struct ofpact_output
*output
;
560 output
= ofpact_put_OUTPUT(out
);
561 output
->max_len
= ntohs(oao
->max_len
);
563 error
= ofputil_port_from_ofp11(oao
->port
, &output
->port
);
568 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
572 encode_OUTPUT(const struct ofpact_output
*output
,
573 enum ofp_version ofp_version
, struct ofpbuf
*out
)
575 if (ofp_version
== OFP10_VERSION
) {
576 struct ofp10_action_output
*oao
;
578 oao
= put_OFPAT10_OUTPUT(out
);
579 oao
->port
= htons(ofp_to_u16(output
->port
));
580 oao
->max_len
= htons(output
->max_len
);
582 struct ofp11_action_output
*oao
;
584 oao
= put_OFPAT11_OUTPUT(out
);
585 oao
->port
= ofputil_port_to_ofp11(output
->port
);
586 oao
->max_len
= htons(output
->max_len
);
590 static char * OVS_WARN_UNUSED_RESULT
591 parse_truncate_subfield(struct ofpact_output_trunc
*output_trunc
,
595 char *arg
= CONST_CAST(char *, arg_
);
597 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
598 if (!strcmp(key
, "port")) {
599 if (!ofputil_port_from_string(value
, &output_trunc
->port
)) {
600 return xasprintf("output to unknown truncate port: %s",
603 if (ofp_to_u16(output_trunc
->port
) > ofp_to_u16(OFPP_MAX
)) {
604 if (output_trunc
->port
!= OFPP_LOCAL
&&
605 output_trunc
->port
!= OFPP_IN_PORT
)
606 return xasprintf("output to unsupported truncate port: %s",
609 } else if (!strcmp(key
, "max_len")) {
612 err
= str_to_u32(value
, &output_trunc
->max_len
);
617 return xasprintf("invalid key '%s' in output_trunc argument",
624 static char * OVS_WARN_UNUSED_RESULT
625 parse_OUTPUT(const char *arg
, struct ofpbuf
*ofpacts
,
626 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
628 if (strstr(arg
, "port") && strstr(arg
, "max_len")) {
629 struct ofpact_output_trunc
*output_trunc
;
631 output_trunc
= ofpact_put_OUTPUT_TRUNC(ofpacts
);
632 return parse_truncate_subfield(output_trunc
, arg
);
634 struct mf_subfield src
;
635 char *error
= mf_parse_subfield(&src
, arg
);
637 struct ofpact_output_reg
*output_reg
;
639 output_reg
= ofpact_put_OUTPUT_REG(ofpacts
);
640 output_reg
->max_len
= UINT16_MAX
;
641 output_reg
->src
= src
;
644 struct ofpact_output
*output
;
646 output
= ofpact_put_OUTPUT(ofpacts
);
647 if (!ofputil_port_from_string(arg
, &output
->port
)) {
648 return xasprintf("%s: output to unknown port", arg
);
650 output
->max_len
= output
->port
== OFPP_CONTROLLER
? UINT16_MAX
: 0;
657 format_OUTPUT(const struct ofpact_output
*a
, struct ds
*s
)
659 if (ofp_to_u16(a
->port
) < ofp_to_u16(OFPP_MAX
)) {
660 ds_put_format(s
, "%soutput:%s%"PRIu32
,
661 colors
.special
, colors
.end
, a
->port
);
663 ofputil_format_port(a
->port
, s
);
664 if (a
->port
== OFPP_CONTROLLER
) {
665 ds_put_format(s
, ":%"PRIu16
, a
->max_len
);
673 decode_OFPAT_RAW_GROUP(uint32_t group_id
,
674 enum ofp_version ofp_version OVS_UNUSED
,
677 ofpact_put_GROUP(out
)->group_id
= group_id
;
682 encode_GROUP(const struct ofpact_group
*group
,
683 enum ofp_version ofp_version
, struct ofpbuf
*out
)
685 put_OFPAT_GROUP(out
, ofp_version
, group
->group_id
);
688 static char * OVS_WARN_UNUSED_RESULT
689 parse_GROUP(char *arg
, struct ofpbuf
*ofpacts
,
690 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
692 return str_to_u32(arg
, &ofpact_put_GROUP(ofpacts
)->group_id
);
696 format_GROUP(const struct ofpact_group
*a
, struct ds
*s
)
698 ds_put_format(s
, "%sgroup:%s%"PRIu32
,
699 colors
.special
, colors
.end
, a
->group_id
);
702 /* Action structure for NXAST_CONTROLLER.
704 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
705 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
708 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
710 * - 'controller_id': The ID of the controller connection to which the
711 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
712 * sent only to controllers that have the specified controller connection
713 * ID. See "struct nx_controller_id" for more information. */
714 struct nx_action_controller
{
715 ovs_be16 type
; /* OFPAT_VENDOR. */
716 ovs_be16 len
; /* Length is 16. */
717 ovs_be32 vendor
; /* NX_VENDOR_ID. */
718 ovs_be16 subtype
; /* NXAST_CONTROLLER. */
719 ovs_be16 max_len
; /* Maximum length to send to controller. */
720 ovs_be16 controller_id
; /* Controller ID to send packet-in. */
721 uint8_t reason
; /* enum ofp_packet_in_reason (OFPR_*). */
722 uint8_t zero
; /* Must be zero. */
724 OFP_ASSERT(sizeof(struct nx_action_controller
) == 16);
726 /* Properties for NXAST_CONTROLLER2.
728 * For more information on the effect of NXAC2PT_PAUSE, see the large comment
729 * on NXT_PACKET_IN2 in nicira-ext.h */
730 enum nx_action_controller2_prop_type
{
731 NXAC2PT_MAX_LEN
, /* ovs_be16 max bytes to send (default all). */
732 NXAC2PT_CONTROLLER_ID
, /* ovs_be16 dest controller ID (default 0). */
733 NXAC2PT_REASON
, /* uint8_t reason (OFPR_*), default 0. */
734 NXAC2PT_USERDATA
, /* Data to copy into NXPINT_USERDATA. */
735 NXAC2PT_PAUSE
, /* Flag to pause pipeline to resume later. */
738 /* The action structure for NXAST_CONTROLLER2 is "struct ext_action_header",
739 * followed by NXAC2PT_* properties. */
742 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller
*nac
,
743 enum ofp_version ofp_version OVS_UNUSED
,
746 struct ofpact_controller
*oc
;
748 oc
= ofpact_put_CONTROLLER(out
);
749 oc
->ofpact
.raw
= NXAST_RAW_CONTROLLER
;
750 oc
->max_len
= ntohs(nac
->max_len
);
751 oc
->controller_id
= ntohs(nac
->controller_id
);
752 oc
->reason
= nac
->reason
;
753 ofpact_finish_CONTROLLER(out
, &oc
);
759 decode_NXAST_RAW_CONTROLLER2(const struct ext_action_header
*eah
,
760 enum ofp_version ofp_version OVS_UNUSED
,
763 if (!is_all_zeros(eah
->pad
, sizeof eah
->pad
)) {
764 return OFPERR_NXBRC_MUST_BE_ZERO
;
767 size_t start_ofs
= out
->size
;
768 struct ofpact_controller
*oc
= ofpact_put_CONTROLLER(out
);
769 oc
->ofpact
.raw
= NXAST_RAW_CONTROLLER2
;
770 oc
->max_len
= UINT16_MAX
;
771 oc
->reason
= OFPR_ACTION
;
773 struct ofpbuf properties
;
774 ofpbuf_use_const(&properties
, eah
, ntohs(eah
->len
));
775 ofpbuf_pull(&properties
, sizeof *eah
);
777 while (properties
.size
> 0) {
778 struct ofpbuf payload
;
781 enum ofperr error
= ofpprop_pull(&properties
, &payload
, &type
);
787 case NXAC2PT_MAX_LEN
:
788 error
= ofpprop_parse_u16(&payload
, &oc
->max_len
);
791 case NXAC2PT_CONTROLLER_ID
:
792 error
= ofpprop_parse_u16(&payload
, &oc
->controller_id
);
795 case NXAC2PT_REASON
: {
797 error
= ofpprop_parse_u8(&payload
, &u8
);
802 case NXAC2PT_USERDATA
:
803 out
->size
= start_ofs
+ OFPACT_CONTROLLER_SIZE
;
804 ofpbuf_put(out
, payload
.msg
, ofpbuf_msgsize(&payload
));
805 oc
= ofpbuf_at_assert(out
, start_ofs
, sizeof *oc
);
806 oc
->userdata_len
= ofpbuf_msgsize(&payload
);
814 error
= OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type
);
822 ofpact_finish_CONTROLLER(out
, &oc
);
828 encode_CONTROLLER(const struct ofpact_controller
*controller
,
829 enum ofp_version ofp_version OVS_UNUSED
,
832 if (controller
->userdata_len
834 || controller
->ofpact
.raw
== NXAST_RAW_CONTROLLER2
) {
835 size_t start_ofs
= out
->size
;
836 put_NXAST_CONTROLLER2(out
);
837 if (controller
->max_len
!= UINT16_MAX
) {
838 ofpprop_put_u16(out
, NXAC2PT_MAX_LEN
, controller
->max_len
);
840 if (controller
->controller_id
!= 0) {
841 ofpprop_put_u16(out
, NXAC2PT_CONTROLLER_ID
,
842 controller
->controller_id
);
844 if (controller
->reason
!= OFPR_ACTION
) {
845 ofpprop_put_u8(out
, NXAC2PT_REASON
, controller
->reason
);
847 if (controller
->userdata_len
!= 0) {
848 ofpprop_put(out
, NXAC2PT_USERDATA
, controller
->userdata
,
849 controller
->userdata_len
);
851 if (controller
->pause
) {
852 ofpprop_put_flag(out
, NXAC2PT_PAUSE
);
854 pad_ofpat(out
, start_ofs
);
856 struct nx_action_controller
*nac
;
858 nac
= put_NXAST_CONTROLLER(out
);
859 nac
->max_len
= htons(controller
->max_len
);
860 nac
->controller_id
= htons(controller
->controller_id
);
861 nac
->reason
= controller
->reason
;
865 static char * OVS_WARN_UNUSED_RESULT
866 parse_CONTROLLER(char *arg
, struct ofpbuf
*ofpacts
,
867 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
869 enum ofp_packet_in_reason reason
= OFPR_ACTION
;
870 uint16_t controller_id
= 0;
871 uint16_t max_len
= UINT16_MAX
;
872 const char *userdata
= NULL
;
877 } else if (strspn(arg
, "0123456789") == strlen(arg
)) {
878 char *error
= str_to_u16(arg
, "max_len", &max_len
);
885 while (ofputil_parse_key_value(&arg
, &name
, &value
)) {
886 if (!strcmp(name
, "reason")) {
887 if (!ofputil_packet_in_reason_from_string(value
, &reason
)) {
888 return xasprintf("unknown reason \"%s\"", value
);
890 } else if (!strcmp(name
, "max_len")) {
891 char *error
= str_to_u16(value
, "max_len", &max_len
);
895 } else if (!strcmp(name
, "id")) {
896 char *error
= str_to_u16(value
, "id", &controller_id
);
900 } else if (!strcmp(name
, "userdata")) {
902 } else if (!strcmp(name
, "pause")) {
905 return xasprintf("unknown key \"%s\" parsing controller "
911 if (reason
== OFPR_ACTION
&& controller_id
== 0 && !userdata
&& !pause
) {
912 struct ofpact_output
*output
;
914 output
= ofpact_put_OUTPUT(ofpacts
);
915 output
->port
= OFPP_CONTROLLER
;
916 output
->max_len
= max_len
;
918 struct ofpact_controller
*controller
;
920 controller
= ofpact_put_CONTROLLER(ofpacts
);
921 controller
->max_len
= max_len
;
922 controller
->reason
= reason
;
923 controller
->controller_id
= controller_id
;
924 controller
->pause
= pause
;
927 size_t start_ofs
= ofpacts
->size
;
928 const char *end
= ofpbuf_put_hex(ofpacts
, userdata
, NULL
);
930 return xstrdup("bad hex digit in `controller' "
931 "action `userdata'");
933 size_t userdata_len
= ofpacts
->size
- start_ofs
;
934 controller
= ofpacts
->header
;
935 controller
->userdata_len
= userdata_len
;
937 ofpact_finish_CONTROLLER(ofpacts
, &controller
);
944 format_hex_arg(struct ds
*s
, const uint8_t *data
, size_t len
)
946 for (size_t i
= 0; i
< len
; i
++) {
950 ds_put_format(s
, "%02"PRIx8
, data
[i
]);
955 format_CONTROLLER(const struct ofpact_controller
*a
, struct ds
*s
)
957 if (a
->reason
== OFPR_ACTION
&& !a
->controller_id
&& !a
->userdata_len
959 ds_put_format(s
, "%sCONTROLLER:%s%"PRIu16
,
960 colors
.special
, colors
.end
, a
->max_len
);
962 enum ofp_packet_in_reason reason
= a
->reason
;
964 ds_put_format(s
, "%scontroller(%s", colors
.paren
, colors
.end
);
965 if (reason
!= OFPR_ACTION
) {
966 char reasonbuf
[OFPUTIL_PACKET_IN_REASON_BUFSIZE
];
968 ds_put_format(s
, "%sreason=%s%s,", colors
.param
, colors
.end
,
969 ofputil_packet_in_reason_to_string(
970 reason
, reasonbuf
, sizeof reasonbuf
));
972 if (a
->max_len
!= UINT16_MAX
) {
973 ds_put_format(s
, "%smax_len=%s%"PRIu16
",",
974 colors
.param
, colors
.end
, a
->max_len
);
976 if (a
->controller_id
!= 0) {
977 ds_put_format(s
, "%sid=%s%"PRIu16
",",
978 colors
.param
, colors
.end
, a
->controller_id
);
980 if (a
->userdata_len
) {
981 ds_put_format(s
, "%suserdata=%s", colors
.param
, colors
.end
);
982 format_hex_arg(s
, a
->userdata
, a
->userdata_len
);
986 ds_put_format(s
, "%spause%s,", colors
.value
, colors
.end
);
989 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
993 /* Enqueue action. */
994 struct ofp10_action_enqueue
{
995 ovs_be16 type
; /* OFPAT10_ENQUEUE. */
996 ovs_be16 len
; /* Len is 16. */
997 ovs_be16 port
; /* Port that queue belongs. Should
998 refer to a valid physical port
999 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
1000 uint8_t pad
[6]; /* Pad for 64-bit alignment. */
1001 ovs_be32 queue_id
; /* Where to enqueue the packets. */
1003 OFP_ASSERT(sizeof(struct ofp10_action_enqueue
) == 16);
1006 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue
*oae
,
1007 enum ofp_version ofp_version OVS_UNUSED
,
1010 struct ofpact_enqueue
*enqueue
;
1012 enqueue
= ofpact_put_ENQUEUE(out
);
1013 enqueue
->port
= u16_to_ofp(ntohs(oae
->port
));
1014 enqueue
->queue
= ntohl(oae
->queue_id
);
1015 if (ofp_to_u16(enqueue
->port
) >= ofp_to_u16(OFPP_MAX
)
1016 && enqueue
->port
!= OFPP_IN_PORT
1017 && enqueue
->port
!= OFPP_LOCAL
) {
1018 return OFPERR_OFPBAC_BAD_OUT_PORT
;
1024 encode_ENQUEUE(const struct ofpact_enqueue
*enqueue
,
1025 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1027 if (ofp_version
== OFP10_VERSION
) {
1028 struct ofp10_action_enqueue
*oae
;
1030 oae
= put_OFPAT10_ENQUEUE(out
);
1031 oae
->port
= htons(ofp_to_u16(enqueue
->port
));
1032 oae
->queue_id
= htonl(enqueue
->queue
);
1034 put_OFPAT_SET_QUEUE(out
, ofp_version
, enqueue
->queue
);
1036 struct ofp11_action_output
*oao
= put_OFPAT11_OUTPUT(out
);
1037 oao
->port
= ofputil_port_to_ofp11(enqueue
->port
);
1038 oao
->max_len
= OVS_BE16_MAX
;
1040 put_NXAST_POP_QUEUE(out
);
1044 static char * OVS_WARN_UNUSED_RESULT
1045 parse_ENQUEUE(char *arg
, struct ofpbuf
*ofpacts
,
1046 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1049 char *port
= strtok_r(arg
, ":q,", &sp
);
1050 char *queue
= strtok_r(NULL
, "", &sp
);
1051 struct ofpact_enqueue
*enqueue
;
1053 if (port
== NULL
|| queue
== NULL
) {
1054 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
1055 "\"enqueue(PORT,QUEUE)\"");
1058 enqueue
= ofpact_put_ENQUEUE(ofpacts
);
1059 if (!ofputil_port_from_string(port
, &enqueue
->port
)) {
1060 return xasprintf("%s: enqueue to unknown port", port
);
1062 return str_to_u32(queue
, &enqueue
->queue
);
1066 format_ENQUEUE(const struct ofpact_enqueue
*a
, struct ds
*s
)
1068 ds_put_format(s
, "%senqueue:%s", colors
.param
, colors
.end
);
1069 ofputil_format_port(a
->port
, s
);
1070 ds_put_format(s
, ":%"PRIu32
, a
->queue
);
1073 /* Action structure for NXAST_OUTPUT_REG.
1075 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
1077 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
1078 * the NXAST_REG_LOAD action.
1080 * The acceptable nxm_header values for 'src' are the same as the acceptable
1081 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
1083 * The 'max_len' field indicates the number of bytes to send when the chosen
1084 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
1085 * field of OFPAT_OUTPUT.
1087 * The 'zero' field is required to be zeroed for forward compatibility. */
1088 struct nx_action_output_reg
{
1089 ovs_be16 type
; /* OFPAT_VENDOR. */
1090 ovs_be16 len
; /* 24. */
1091 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1092 ovs_be16 subtype
; /* NXAST_OUTPUT_REG. */
1094 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1095 ovs_be32 src
; /* Source. */
1097 ovs_be16 max_len
; /* Max length to send to controller. */
1099 uint8_t zero
[6]; /* Reserved, must be zero. */
1101 OFP_ASSERT(sizeof(struct nx_action_output_reg
) == 24);
1103 /* Action structure for NXAST_OUTPUT_REG2.
1105 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
1106 * experimenter OXM as 'src'.
1108 struct nx_action_output_reg2
{
1109 ovs_be16 type
; /* OFPAT_VENDOR. */
1110 ovs_be16 len
; /* 24. */
1111 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1112 ovs_be16 subtype
; /* NXAST_OUTPUT_REG2. */
1114 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1115 ovs_be16 max_len
; /* Max length to send to controller. */
1118 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1119 * - Enough 0-bytes to pad the action out to 24 bytes. */
1122 OFP_ASSERT(sizeof(struct nx_action_output_reg2
) == 24);
1125 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg
*naor
,
1126 enum ofp_version ofp_version OVS_UNUSED
,
1127 const struct vl_mff_map
*vl_mff_map
,
1128 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
1130 struct ofpact_output_reg
*output_reg
;
1133 if (!is_all_zeros(naor
->zero
, sizeof naor
->zero
)) {
1134 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1137 output_reg
= ofpact_put_OUTPUT_REG(out
);
1138 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG
;
1139 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
1140 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
1141 output_reg
->max_len
= ntohs(naor
->max_len
);
1142 error
= mf_vl_mff_mf_from_nxm_header(ntohl(naor
->src
), vl_mff_map
,
1143 &output_reg
->src
.field
, tlv_bitmap
);
1148 return mf_check_src(&output_reg
->src
, NULL
);
1152 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2
*naor
,
1153 enum ofp_version ofp_version OVS_UNUSED
,
1154 const struct vl_mff_map
*vl_mff_map
,
1155 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
1157 struct ofpact_output_reg
*output_reg
;
1160 output_reg
= ofpact_put_OUTPUT_REG(out
);
1161 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG2
;
1162 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
1163 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
1164 output_reg
->max_len
= ntohs(naor
->max_len
);
1166 struct ofpbuf b
= ofpbuf_const_initializer(naor
, ntohs(naor
->len
));
1167 ofpbuf_pull(&b
, OBJECT_OFFSETOF(naor
, pad
));
1169 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &output_reg
->src
.field
,
1175 if (!is_all_zeros(b
.data
, b
.size
)) {
1176 return OFPERR_NXBRC_MUST_BE_ZERO
;
1179 return mf_check_src(&output_reg
->src
, NULL
);
1183 encode_OUTPUT_REG(const struct ofpact_output_reg
*output_reg
,
1184 enum ofp_version ofp_version OVS_UNUSED
,
1187 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1188 * cannot be encoded in the older form, encode it as
1189 * NXAST_RAW_OUTPUT_REG2. */
1190 if (output_reg
->ofpact
.raw
== NXAST_RAW_OUTPUT_REG2
1191 || !mf_nxm_header(output_reg
->src
.field
->id
)) {
1192 struct nx_action_output_reg2
*naor
= put_NXAST_OUTPUT_REG2(out
);
1193 size_t size
= out
->size
;
1195 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
1196 output_reg
->src
.n_bits
);
1197 naor
->max_len
= htons(output_reg
->max_len
);
1199 out
->size
= size
- sizeof naor
->pad
;
1200 nx_put_mff_header(out
, output_reg
->src
.field
, 0, false);
1203 struct nx_action_output_reg
*naor
= put_NXAST_OUTPUT_REG(out
);
1205 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
1206 output_reg
->src
.n_bits
);
1207 naor
->src
= htonl(nxm_header_from_mff(output_reg
->src
.field
));
1208 naor
->max_len
= htons(output_reg
->max_len
);
1212 static char * OVS_WARN_UNUSED_RESULT
1213 parse_OUTPUT_REG(const char *arg
, struct ofpbuf
*ofpacts
,
1214 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1216 return parse_OUTPUT(arg
, ofpacts
, usable_protocols
);
1220 format_OUTPUT_REG(const struct ofpact_output_reg
*a
, struct ds
*s
)
1222 ds_put_format(s
, "%soutput:%s", colors
.special
, colors
.end
);
1223 mf_format_subfield(&a
->src
, s
);
1226 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1228 * The bundle actions choose a slave from a supplied list of options.
1229 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
1230 * selection to a register.
1232 * The list of possible slaves follows the nx_action_bundle structure. The size
1233 * of each slave is governed by its type as indicated by the 'slave_type'
1234 * parameter. The list of slaves should be padded at its end with zeros to make
1235 * the total length of the action a multiple of 8.
1237 * Switches infer from the 'slave_type' parameter the size of each slave. All
1238 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
1239 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
1240 * 2 byte width. Switches should reject actions which indicate unknown or
1241 * unsupported slave types.
1243 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1244 * slave. If the switch does not support the specified 'algorithm' parameter,
1245 * it should reject the action.
1247 * Several algorithms take into account liveness when selecting slaves. The
1248 * liveness of a slave is implementation defined (with one exception), but will
1249 * generally take into account things like its carrier status and the results
1250 * of any link monitoring protocols which happen to be running on it. In order
1251 * to give controllers a place-holder value, the OFPP_NONE port is always
1252 * considered live, that is, NXAST_BUNDLE_LOAD stores OFPP_NONE in the output
1253 * register if no slave is live.
1255 * Some slave selection strategies require the use of a hash function, in which
1256 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
1257 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
1258 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
1259 * 'basis' parameter is used as a universal hash parameter. Different values
1260 * of 'basis' yield different hash results.
1262 * The 'zero' parameter at the end of the action structure is reserved for
1263 * future use. Switches are required to reject actions which have nonzero
1264 * bytes in the 'zero' field.
1266 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
1267 * should reject actions which have nonzero bytes in either of these fields.
1269 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
1270 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
1271 * similar to those for the NXAST_REG_LOAD action. */
1272 struct nx_action_bundle
{
1273 ovs_be16 type
; /* OFPAT_VENDOR. */
1274 ovs_be16 len
; /* Length including slaves. */
1275 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1276 ovs_be16 subtype
; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1278 /* Slave choice algorithm to apply to hash value. */
1279 ovs_be16 algorithm
; /* One of NX_BD_ALG_*. */
1281 /* What fields to hash and how. */
1282 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
1283 ovs_be16 basis
; /* Universal hash parameter. */
1285 ovs_be32 slave_type
; /* NXM_OF_IN_PORT. */
1286 ovs_be16 n_slaves
; /* Number of slaves. */
1288 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1289 ovs_be32 dst
; /* Destination. */
1291 uint8_t zero
[4]; /* Reserved. Must be zero. */
1293 OFP_ASSERT(sizeof(struct nx_action_bundle
) == 32);
1296 decode_bundle(bool load
, const struct nx_action_bundle
*nab
,
1297 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
1298 struct ofpbuf
*ofpacts
)
1300 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1301 struct ofpact_bundle
*bundle
;
1302 uint32_t slave_type
;
1303 size_t slaves_size
, i
;
1306 bundle
= ofpact_put_BUNDLE(ofpacts
);
1308 bundle
->n_slaves
= ntohs(nab
->n_slaves
);
1309 bundle
->basis
= ntohs(nab
->basis
);
1310 bundle
->fields
= ntohs(nab
->fields
);
1311 bundle
->algorithm
= ntohs(nab
->algorithm
);
1312 slave_type
= ntohl(nab
->slave_type
);
1313 slaves_size
= ntohs(nab
->len
) - sizeof *nab
;
1315 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1316 if (!flow_hash_fields_valid(bundle
->fields
)) {
1317 VLOG_WARN_RL(&rl
, "unsupported fields %d", (int) bundle
->fields
);
1318 } else if (bundle
->n_slaves
> BUNDLE_MAX_SLAVES
) {
1319 VLOG_WARN_RL(&rl
, "too many slaves");
1320 } else if (bundle
->algorithm
!= NX_BD_ALG_HRW
1321 && bundle
->algorithm
!= NX_BD_ALG_ACTIVE_BACKUP
) {
1322 VLOG_WARN_RL(&rl
, "unsupported algorithm %d", (int) bundle
->algorithm
);
1323 } else if (slave_type
!= mf_nxm_header(MFF_IN_PORT
)) {
1324 VLOG_WARN_RL(&rl
, "unsupported slave type %"PRIu32
, slave_type
);
1329 if (!is_all_zeros(nab
->zero
, sizeof nab
->zero
)) {
1330 VLOG_WARN_RL(&rl
, "reserved field is nonzero");
1331 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1335 bundle
->dst
.ofs
= nxm_decode_ofs(nab
->ofs_nbits
);
1336 bundle
->dst
.n_bits
= nxm_decode_n_bits(nab
->ofs_nbits
);
1337 error
= mf_vl_mff_mf_from_nxm_header(ntohl(nab
->dst
), vl_mff_map
,
1338 &bundle
->dst
.field
, tlv_bitmap
);
1343 if (bundle
->dst
.n_bits
< 16) {
1344 VLOG_WARN_RL(&rl
, "bundle_load action requires at least 16 bit "
1346 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1349 if (nab
->ofs_nbits
|| nab
->dst
) {
1350 VLOG_WARN_RL(&rl
, "bundle action has nonzero reserved fields");
1351 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1355 if (slaves_size
< bundle
->n_slaves
* sizeof(ovs_be16
)) {
1356 VLOG_WARN_RL(&rl
, "Nicira action %s only has %"PRIuSIZE
" bytes "
1357 "allocated for slaves. %"PRIuSIZE
" bytes are required "
1359 load
? "bundle_load" : "bundle", slaves_size
,
1360 bundle
->n_slaves
* sizeof(ovs_be16
), bundle
->n_slaves
);
1361 error
= OFPERR_OFPBAC_BAD_LEN
;
1364 for (i
= 0; i
< bundle
->n_slaves
; i
++) {
1365 ofp_port_t ofp_port
= u16_to_ofp(ntohs(((ovs_be16
*)(nab
+ 1))[i
]));
1366 ofpbuf_put(ofpacts
, &ofp_port
, sizeof ofp_port
);
1367 bundle
= ofpacts
->header
;
1370 ofpact_finish_BUNDLE(ofpacts
, &bundle
);
1372 error
= bundle_check(bundle
, OFPP_MAX
, NULL
);
1378 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle
*nab
,
1379 enum ofp_version ofp_version OVS_UNUSED
,
1382 return decode_bundle(false, nab
, NULL
, NULL
, out
);
1386 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle
*nab
,
1387 enum ofp_version ofp_version OVS_UNUSED
,
1388 const struct vl_mff_map
*vl_mff_map
,
1389 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
1391 return decode_bundle(true, nab
, vl_mff_map
, tlv_bitmap
, out
);
1395 encode_BUNDLE(const struct ofpact_bundle
*bundle
,
1396 enum ofp_version ofp_version OVS_UNUSED
,
1399 int slaves_len
= ROUND_UP(2 * bundle
->n_slaves
, OFP_ACTION_ALIGN
);
1400 struct nx_action_bundle
*nab
;
1404 nab
= (bundle
->dst
.field
1405 ? put_NXAST_BUNDLE_LOAD(out
)
1406 : put_NXAST_BUNDLE(out
));
1407 nab
->len
= htons(ntohs(nab
->len
) + slaves_len
);
1408 nab
->algorithm
= htons(bundle
->algorithm
);
1409 nab
->fields
= htons(bundle
->fields
);
1410 nab
->basis
= htons(bundle
->basis
);
1411 nab
->slave_type
= htonl(mf_nxm_header(MFF_IN_PORT
));
1412 nab
->n_slaves
= htons(bundle
->n_slaves
);
1413 if (bundle
->dst
.field
) {
1414 nab
->ofs_nbits
= nxm_encode_ofs_nbits(bundle
->dst
.ofs
,
1415 bundle
->dst
.n_bits
);
1416 nab
->dst
= htonl(nxm_header_from_mff(bundle
->dst
.field
));
1419 slaves
= ofpbuf_put_zeros(out
, slaves_len
);
1420 for (i
= 0; i
< bundle
->n_slaves
; i
++) {
1421 slaves
[i
] = htons(ofp_to_u16(bundle
->slaves
[i
]));
1425 static char * OVS_WARN_UNUSED_RESULT
1426 parse_BUNDLE(const char *arg
, struct ofpbuf
*ofpacts
,
1427 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1429 return bundle_parse(arg
, ofpacts
);
1432 static char * OVS_WARN_UNUSED_RESULT
1433 parse_bundle_load(const char *arg
, struct ofpbuf
*ofpacts
)
1435 return bundle_parse_load(arg
, ofpacts
);
1439 format_BUNDLE(const struct ofpact_bundle
*a
, struct ds
*s
)
1441 bundle_format(a
, s
);
1444 /* Set VLAN actions. */
1447 decode_set_vlan_vid(uint16_t vid
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1450 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1452 struct ofpact_vlan_vid
*vlan_vid
= ofpact_put_SET_VLAN_VID(out
);
1453 vlan_vid
->vlan_vid
= vid
;
1454 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1460 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid
,
1461 enum ofp_version ofp_version OVS_UNUSED
,
1464 return decode_set_vlan_vid(vid
, true, out
);
1468 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid
,
1469 enum ofp_version ofp_version OVS_UNUSED
,
1472 return decode_set_vlan_vid(vid
, false, out
);
1476 encode_SET_VLAN_VID(const struct ofpact_vlan_vid
*vlan_vid
,
1477 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1479 uint16_t vid
= vlan_vid
->vlan_vid
;
1481 /* Push a VLAN tag, if none is present and this form of the action calls
1482 * for such a feature. */
1483 if (ofp_version
> OFP10_VERSION
1484 && vlan_vid
->push_vlan_if_needed
1485 && !vlan_vid
->flow_has_vlan
) {
1486 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1489 if (ofp_version
== OFP10_VERSION
) {
1490 put_OFPAT10_SET_VLAN_VID(out
, vid
);
1491 } else if (ofp_version
== OFP11_VERSION
) {
1492 put_OFPAT11_SET_VLAN_VID(out
, vid
);
1494 put_set_field(out
, ofp_version
, MFF_VLAN_VID
, vid
| OFPVID12_PRESENT
);
1498 static char * OVS_WARN_UNUSED_RESULT
1499 parse_set_vlan_vid(char *arg
, struct ofpbuf
*ofpacts
, bool push_vlan_if_needed
)
1501 struct ofpact_vlan_vid
*vlan_vid
;
1505 error
= str_to_u16(arg
, "VLAN VID", &vid
);
1510 if (vid
& ~VLAN_VID_MASK
) {
1511 return xasprintf("%s: not a valid VLAN VID", arg
);
1513 vlan_vid
= ofpact_put_SET_VLAN_VID(ofpacts
);
1514 vlan_vid
->vlan_vid
= vid
;
1515 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1519 static char * OVS_WARN_UNUSED_RESULT
1520 parse_SET_VLAN_VID(char *arg
, struct ofpbuf
*ofpacts
,
1521 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1523 return parse_set_vlan_vid(arg
, ofpacts
, false);
1527 format_SET_VLAN_VID(const struct ofpact_vlan_vid
*a
, struct ds
*s
)
1529 ds_put_format(s
, "%s%s:%s%"PRIu16
, colors
.param
,
1530 a
->push_vlan_if_needed
? "mod_vlan_vid" : "set_vlan_vid",
1531 colors
.end
, a
->vlan_vid
);
1534 /* Set PCP actions. */
1537 decode_set_vlan_pcp(uint8_t pcp
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1540 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1542 struct ofpact_vlan_pcp
*vlan_pcp
= ofpact_put_SET_VLAN_PCP(out
);
1543 vlan_pcp
->vlan_pcp
= pcp
;
1544 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1550 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp
,
1551 enum ofp_version ofp_version OVS_UNUSED
,
1554 return decode_set_vlan_pcp(pcp
, true, out
);
1558 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp
,
1559 enum ofp_version ofp_version OVS_UNUSED
,
1562 return decode_set_vlan_pcp(pcp
, false, out
);
1566 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*vlan_pcp
,
1567 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1569 uint8_t pcp
= vlan_pcp
->vlan_pcp
;
1571 /* Push a VLAN tag, if none is present and this form of the action calls
1572 * for such a feature. */
1573 if (ofp_version
> OFP10_VERSION
1574 && vlan_pcp
->push_vlan_if_needed
1575 && !vlan_pcp
->flow_has_vlan
) {
1576 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1579 if (ofp_version
== OFP10_VERSION
) {
1580 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
1581 } else if (ofp_version
== OFP11_VERSION
) {
1582 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
1584 put_set_field(out
, ofp_version
, MFF_VLAN_PCP
, pcp
);
1588 static char * OVS_WARN_UNUSED_RESULT
1589 parse_set_vlan_pcp(char *arg
, struct ofpbuf
*ofpacts
, bool push_vlan_if_needed
)
1591 struct ofpact_vlan_pcp
*vlan_pcp
;
1595 error
= str_to_u8(arg
, "VLAN PCP", &pcp
);
1601 return xasprintf("%s: not a valid VLAN PCP", arg
);
1603 vlan_pcp
= ofpact_put_SET_VLAN_PCP(ofpacts
);
1604 vlan_pcp
->vlan_pcp
= pcp
;
1605 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1609 static char * OVS_WARN_UNUSED_RESULT
1610 parse_SET_VLAN_PCP(char *arg
, struct ofpbuf
*ofpacts
,
1611 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1613 return parse_set_vlan_pcp(arg
, ofpacts
, false);
1617 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*a
, struct ds
*s
)
1619 ds_put_format(s
, "%s%s:%s%"PRIu8
, colors
.param
,
1620 a
->push_vlan_if_needed
? "mod_vlan_pcp" : "set_vlan_pcp",
1621 colors
.end
, a
->vlan_pcp
);
1624 /* Strip VLAN actions. */
1627 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf
*out
)
1629 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1634 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf
*out
)
1636 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1641 encode_STRIP_VLAN(const struct ofpact_null
*null OVS_UNUSED
,
1642 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1644 if (ofp_version
== OFP10_VERSION
) {
1645 put_OFPAT10_STRIP_VLAN(out
);
1647 put_OFPAT11_POP_VLAN(out
);
1651 static char * OVS_WARN_UNUSED_RESULT
1652 parse_STRIP_VLAN(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
1653 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1655 ofpact_put_STRIP_VLAN(ofpacts
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1659 static char * OVS_WARN_UNUSED_RESULT
1660 parse_pop_vlan(struct ofpbuf
*ofpacts
)
1662 ofpact_put_STRIP_VLAN(ofpacts
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1667 format_STRIP_VLAN(const struct ofpact_null
*a
, struct ds
*s
)
1669 ds_put_format(s
, (a
->ofpact
.raw
== OFPAT_RAW11_POP_VLAN
1671 : "%sstrip_vlan%s"),
1672 colors
.value
, colors
.end
);
1675 /* Push VLAN action. */
1678 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type
,
1679 enum ofp_version ofp_version OVS_UNUSED
,
1682 struct ofpact_push_vlan
*push_vlan
;
1683 if (!eth_type_vlan(eth_type
)) {
1684 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1686 push_vlan
= ofpact_put_PUSH_VLAN(out
);
1687 push_vlan
->ethertype
= eth_type
;
1692 encode_PUSH_VLAN(const struct ofpact_push_vlan
*push_vlan
,
1693 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1695 if (ofp_version
== OFP10_VERSION
) {
1696 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1697 * follow this action. */
1699 put_OFPAT11_PUSH_VLAN(out
, push_vlan
->ethertype
);
1703 static char * OVS_WARN_UNUSED_RESULT
1704 parse_PUSH_VLAN(char *arg
, struct ofpbuf
*ofpacts
,
1705 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1707 struct ofpact_push_vlan
*push_vlan
;
1711 *usable_protocols
&= OFPUTIL_P_OF11_UP
;
1712 error
= str_to_u16(arg
, "ethertype", ðertype
);
1717 if (!eth_type_vlan(htons(ethertype
))) {
1718 return xasprintf("%s: not a valid VLAN ethertype", arg
);
1720 push_vlan
= ofpact_put_PUSH_VLAN(ofpacts
);
1721 push_vlan
->ethertype
= htons(ethertype
);
1726 format_PUSH_VLAN(const struct ofpact_push_vlan
*push_vlan
, struct ds
*s
)
1728 ds_put_format(s
, "%spush_vlan:%s%#"PRIx16
,
1729 colors
.param
, colors
.end
, ntohs(push_vlan
->ethertype
));
1732 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1733 struct ofp_action_dl_addr
{
1734 ovs_be16 type
; /* Type. */
1735 ovs_be16 len
; /* Length is 16. */
1736 struct eth_addr dl_addr
; /* Ethernet address. */
1739 OFP_ASSERT(sizeof(struct ofp_action_dl_addr
) == 16);
1742 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr
*a
,
1743 enum ofp_version ofp_version OVS_UNUSED
,
1746 ofpact_put_SET_ETH_SRC(out
)->mac
= a
->dl_addr
;
1751 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr
*a
,
1752 enum ofp_version ofp_version OVS_UNUSED
,
1755 ofpact_put_SET_ETH_DST(out
)->mac
= a
->dl_addr
;
1760 encode_SET_ETH_addr(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1761 enum ofp_raw_action_type raw
, enum mf_field_id field
,
1764 if (ofp_version
< OFP12_VERSION
) {
1765 struct ofp_action_dl_addr
*oada
;
1767 oada
= ofpact_put_raw(out
, ofp_version
, raw
, 0);
1768 oada
->dl_addr
= mac
->mac
;
1770 put_set_field(out
, ofp_version
, field
, eth_addr_to_uint64(mac
->mac
));
1775 encode_SET_ETH_SRC(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1778 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_SRC
, MFF_ETH_SRC
,
1784 encode_SET_ETH_DST(const struct ofpact_mac
*mac
,
1785 enum ofp_version ofp_version
,
1788 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_DST
, MFF_ETH_DST
,
1793 static char * OVS_WARN_UNUSED_RESULT
1794 parse_SET_ETH_SRC(char *arg
, struct ofpbuf
*ofpacts
,
1795 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1797 return str_to_mac(arg
, &ofpact_put_SET_ETH_SRC(ofpacts
)->mac
);
1800 static char * OVS_WARN_UNUSED_RESULT
1801 parse_SET_ETH_DST(char *arg
, struct ofpbuf
*ofpacts
,
1802 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1804 return str_to_mac(arg
, &ofpact_put_SET_ETH_DST(ofpacts
)->mac
);
1808 format_SET_ETH_SRC(const struct ofpact_mac
*a
, struct ds
*s
)
1810 ds_put_format(s
, "%smod_dl_src:%s"ETH_ADDR_FMT
,
1811 colors
.param
, colors
.end
, ETH_ADDR_ARGS(a
->mac
));
1815 format_SET_ETH_DST(const struct ofpact_mac
*a
, struct ds
*s
)
1817 ds_put_format(s
, "%smod_dl_dst:%s"ETH_ADDR_FMT
,
1818 colors
.param
, colors
.end
, ETH_ADDR_ARGS(a
->mac
));
1821 /* Set IPv4 address actions. */
1824 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4
,
1825 enum ofp_version ofp_version OVS_UNUSED
,
1828 ofpact_put_SET_IPV4_SRC(out
)->ipv4
= ipv4
;
1833 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4
,
1834 enum ofp_version ofp_version OVS_UNUSED
,
1837 ofpact_put_SET_IPV4_DST(out
)->ipv4
= ipv4
;
1842 encode_SET_IPV4_addr(const struct ofpact_ipv4
*ipv4
,
1843 enum ofp_version ofp_version
,
1844 enum ofp_raw_action_type raw
, enum mf_field_id field
,
1847 ovs_be32 addr
= ipv4
->ipv4
;
1848 if (ofp_version
< OFP12_VERSION
) {
1849 ofpact_put_raw(out
, ofp_version
, raw
, ntohl(addr
));
1851 put_set_field(out
, ofp_version
, field
, ntohl(addr
));
1856 encode_SET_IPV4_SRC(const struct ofpact_ipv4
*ipv4
,
1857 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1859 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_SRC
, MFF_IPV4_SRC
,
1864 encode_SET_IPV4_DST(const struct ofpact_ipv4
*ipv4
,
1865 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1867 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_DST
, MFF_IPV4_DST
,
1871 static char * OVS_WARN_UNUSED_RESULT
1872 parse_SET_IPV4_SRC(char *arg
, struct ofpbuf
*ofpacts
,
1873 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1875 return str_to_ip(arg
, &ofpact_put_SET_IPV4_SRC(ofpacts
)->ipv4
);
1878 static char * OVS_WARN_UNUSED_RESULT
1879 parse_SET_IPV4_DST(char *arg
, struct ofpbuf
*ofpacts
,
1880 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1882 return str_to_ip(arg
, &ofpact_put_SET_IPV4_DST(ofpacts
)->ipv4
);
1886 format_SET_IPV4_SRC(const struct ofpact_ipv4
*a
, struct ds
*s
)
1888 ds_put_format(s
, "%smod_nw_src:%s"IP_FMT
,
1889 colors
.param
, colors
.end
, IP_ARGS(a
->ipv4
));
1893 format_SET_IPV4_DST(const struct ofpact_ipv4
*a
, struct ds
*s
)
1895 ds_put_format(s
, "%smod_nw_dst:%s"IP_FMT
,
1896 colors
.param
, colors
.end
, IP_ARGS(a
->ipv4
));
1899 /* Set IPv4/v6 TOS actions. */
1902 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp
,
1903 enum ofp_version ofp_version OVS_UNUSED
,
1906 if (dscp
& ~IP_DSCP_MASK
) {
1907 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1909 ofpact_put_SET_IP_DSCP(out
)->dscp
= dscp
;
1915 encode_SET_IP_DSCP(const struct ofpact_dscp
*dscp
,
1916 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1918 if (ofp_version
< OFP12_VERSION
) {
1919 put_OFPAT_SET_NW_TOS(out
, ofp_version
, dscp
->dscp
);
1921 put_set_field(out
, ofp_version
, MFF_IP_DSCP_SHIFTED
, dscp
->dscp
>> 2);
1925 static char * OVS_WARN_UNUSED_RESULT
1926 parse_SET_IP_DSCP(char *arg
, struct ofpbuf
*ofpacts
,
1927 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1932 error
= str_to_u8(arg
, "TOS", &tos
);
1937 if (tos
& ~IP_DSCP_MASK
) {
1938 return xasprintf("%s: not a valid TOS", arg
);
1940 ofpact_put_SET_IP_DSCP(ofpacts
)->dscp
= tos
;
1945 format_SET_IP_DSCP(const struct ofpact_dscp
*a
, struct ds
*s
)
1947 ds_put_format(s
, "%smod_nw_tos:%s%d", colors
.param
, colors
.end
, a
->dscp
);
1950 /* Set IPv4/v6 ECN actions. */
1953 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn
,
1954 enum ofp_version ofp_version OVS_UNUSED
,
1957 if (ecn
& ~IP_ECN_MASK
) {
1958 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1960 ofpact_put_SET_IP_ECN(out
)->ecn
= ecn
;
1966 encode_SET_IP_ECN(const struct ofpact_ecn
*ip_ecn
,
1967 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1969 uint8_t ecn
= ip_ecn
->ecn
;
1970 if (ofp_version
== OFP10_VERSION
) {
1971 struct mf_subfield dst
= { .field
= mf_from_id(MFF_IP_ECN
),
1972 .ofs
= 0, .n_bits
= 2 };
1973 put_reg_load(out
, &dst
, ecn
);
1974 } else if (ofp_version
== OFP11_VERSION
) {
1975 put_OFPAT11_SET_NW_ECN(out
, ecn
);
1977 put_set_field(out
, ofp_version
, MFF_IP_ECN
, ecn
);
1981 static char * OVS_WARN_UNUSED_RESULT
1982 parse_SET_IP_ECN(char *arg
, struct ofpbuf
*ofpacts
,
1983 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1988 error
= str_to_u8(arg
, "ECN", &ecn
);
1993 if (ecn
& ~IP_ECN_MASK
) {
1994 return xasprintf("%s: not a valid ECN", arg
);
1996 ofpact_put_SET_IP_ECN(ofpacts
)->ecn
= ecn
;
2001 format_SET_IP_ECN(const struct ofpact_ecn
*a
, struct ds
*s
)
2003 ds_put_format(s
, "%smod_nw_ecn:%s%d",
2004 colors
.param
, colors
.end
, a
->ecn
);
2007 /* Set IPv4/v6 TTL actions. */
2010 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl
,
2011 enum ofp_version ofp_version OVS_UNUSED
,
2014 ofpact_put_SET_IP_TTL(out
)->ttl
= ttl
;
2019 encode_SET_IP_TTL(const struct ofpact_ip_ttl
*ttl
,
2020 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2022 if (ofp_version
>= OFP11_VERSION
) {
2023 put_OFPAT11_SET_NW_TTL(out
, ttl
->ttl
);
2025 struct mf_subfield dst
= { .field
= mf_from_id(MFF_IP_TTL
),
2026 .ofs
= 0, .n_bits
= 8 };
2027 put_reg_load(out
, &dst
, ttl
->ttl
);
2031 static char * OVS_WARN_UNUSED_RESULT
2032 parse_SET_IP_TTL(char *arg
, struct ofpbuf
*ofpacts
,
2033 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2038 error
= str_to_u8(arg
, "TTL", &ttl
);
2043 ofpact_put_SET_IP_TTL(ofpacts
)->ttl
= ttl
;
2048 format_SET_IP_TTL(const struct ofpact_ip_ttl
*a
, struct ds
*s
)
2050 ds_put_format(s
, "%smod_nw_ttl:%s%d", colors
.param
, colors
.end
, a
->ttl
);
2053 /* Set TCP/UDP/SCTP port actions. */
2056 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port
,
2057 enum ofp_version ofp_version OVS_UNUSED
,
2060 ofpact_put_SET_L4_SRC_PORT(out
)->port
= ntohs(port
);
2065 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port
,
2066 enum ofp_version ofp_version OVS_UNUSED
,
2069 ofpact_put_SET_L4_DST_PORT(out
)->port
= ntohs(port
);
2074 encode_SET_L4_port(const struct ofpact_l4_port
*l4_port
,
2075 enum ofp_version ofp_version
, enum ofp_raw_action_type raw
,
2076 enum mf_field_id field
, struct ofpbuf
*out
)
2078 uint16_t port
= l4_port
->port
;
2080 if (ofp_version
>= OFP12_VERSION
&& field
!= MFF_N_IDS
) {
2081 put_set_field(out
, ofp_version
, field
, port
);
2083 ofpact_put_raw(out
, ofp_version
, raw
, port
);
2088 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port
*l4_port
,
2089 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2091 uint8_t proto
= l4_port
->flow_ip_proto
;
2092 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_SRC
2093 : proto
== IPPROTO_UDP
? MFF_UDP_SRC
2094 : proto
== IPPROTO_SCTP
? MFF_SCTP_SRC
2097 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_SRC
, field
, out
);
2101 encode_SET_L4_DST_PORT(const struct ofpact_l4_port
*l4_port
,
2102 enum ofp_version ofp_version
,
2105 uint8_t proto
= l4_port
->flow_ip_proto
;
2106 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_DST
2107 : proto
== IPPROTO_UDP
? MFF_UDP_DST
2108 : proto
== IPPROTO_SCTP
? MFF_SCTP_DST
2111 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_DST
, field
, out
);
2114 static char * OVS_WARN_UNUSED_RESULT
2115 parse_SET_L4_SRC_PORT(char *arg
, struct ofpbuf
*ofpacts
,
2116 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2118 return str_to_u16(arg
, "source port",
2119 &ofpact_put_SET_L4_SRC_PORT(ofpacts
)->port
);
2122 static char * OVS_WARN_UNUSED_RESULT
2123 parse_SET_L4_DST_PORT(char *arg
, struct ofpbuf
*ofpacts
,
2124 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2126 return str_to_u16(arg
, "destination port",
2127 &ofpact_put_SET_L4_DST_PORT(ofpacts
)->port
);
2131 format_SET_L4_SRC_PORT(const struct ofpact_l4_port
*a
, struct ds
*s
)
2133 ds_put_format(s
, "%smod_tp_src:%s%d", colors
.param
, colors
.end
, a
->port
);
2137 format_SET_L4_DST_PORT(const struct ofpact_l4_port
*a
, struct ds
*s
)
2139 ds_put_format(s
, "%smod_tp_dst:%s%d", colors
.param
, colors
.end
, a
->port
);
2142 /* Action structure for OFPAT_COPY_FIELD. */
2143 struct ofp15_action_copy_field
{
2144 ovs_be16 type
; /* OFPAT_COPY_FIELD. */
2145 ovs_be16 len
; /* Length is padded to 64 bits. */
2146 ovs_be16 n_bits
; /* Number of bits to copy. */
2147 ovs_be16 src_offset
; /* Starting bit offset in source. */
2148 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
2151 * - OXM header for source field.
2152 * - OXM header for destination field.
2153 * - Padding with 0-bytes to a multiple of 8 bytes.
2154 * The "pad2" member is the beginning of the above. */
2157 OFP_ASSERT(sizeof(struct ofp15_action_copy_field
) == 16);
2159 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
2160 struct onf_action_copy_field
{
2161 ovs_be16 type
; /* OFPAT_EXPERIMENTER. */
2162 ovs_be16 len
; /* Length is padded to 64 bits. */
2163 ovs_be32 experimenter
; /* ONF_VENDOR_ID. */
2164 ovs_be16 exp_type
; /* 3200. */
2165 uint8_t pad
[2]; /* Not used. */
2166 ovs_be16 n_bits
; /* Number of bits to copy. */
2167 ovs_be16 src_offset
; /* Starting bit offset in source. */
2168 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
2169 uint8_t pad2
[2]; /* Not used. */
2171 * - OXM header for source field.
2172 * - OXM header for destination field.
2173 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2174 * The "pad3" member is the beginning of the above. */
2175 uint8_t pad3
[4]; /* Not used. */
2177 OFP_ASSERT(sizeof(struct onf_action_copy_field
) == 24);
2179 /* Action structure for NXAST_REG_MOVE.
2181 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2182 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2183 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
2184 * the next most significant bit, and so on.
2186 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
2187 * sense to use nxm_hasmask=1 because the action does not do any kind of
2188 * matching; it uses the actual value of a field.)
2190 * The following nxm_header values are potentially acceptable as 'src':
2205 * - NXM_OF_ICMP_TYPE
2206 * - NXM_OF_ICMP_CODE
2213 * - NXM_NX_ICMPV6_TYPE
2214 * - NXM_NX_ICMPV6_CODE
2217 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2219 * - NXM_NX_TUN_IPV4_SRC
2220 * - NXM_NX_TUN_IPV4_DST
2222 * The following nxm_header values are potentially acceptable as 'dst':
2233 * - NXM_OF_ICMP_TYPE
2234 * - NXM_OF_ICMP_CODE
2235 * - NXM_NX_ICMPV6_TYPE
2236 * - NXM_NX_ICMPV6_CODE
2242 * Modifying any of the above fields changes the corresponding packet
2247 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2251 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
2252 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
2253 * header (if any), ignoring the other bits. Setting a value with CFI=1
2254 * adds or modifies the 802.1Q header appropriately, setting the TCI field
2255 * to the field's new value (with the CFI bit masked out).
2257 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
2258 * any of these values modifies the corresponding tunnel header field used
2259 * for the packet's next tunnel encapsulation, if allowed by the
2260 * configuration of the output tunnel port.
2262 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2263 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
2264 * used only if the flow's nx_match includes an nxm_entry that specifies
2265 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2267 * The switch will reject actions for which src_ofs+n_bits is greater than the
2268 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2269 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2271 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2272 * behaves as if 'src' were copied out to a temporary buffer, then the
2273 * temporary buffer copied to 'dst'.
2275 struct nx_action_reg_move
{
2276 ovs_be16 type
; /* OFPAT_VENDOR. */
2277 ovs_be16 len
; /* Length is 24. */
2278 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2279 ovs_be16 subtype
; /* NXAST_REG_MOVE. */
2280 ovs_be16 n_bits
; /* Number of bits. */
2281 ovs_be16 src_ofs
; /* Starting bit offset in source. */
2282 ovs_be16 dst_ofs
; /* Starting bit offset in destination. */
2284 * - OXM/NXM header for source field (4 or 8 bytes).
2285 * - OXM/NXM header for destination field (4 or 8 bytes).
2286 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2288 OFP_ASSERT(sizeof(struct nx_action_reg_move
) == 16);
2291 decode_copy_field__(ovs_be16 src_offset
, ovs_be16 dst_offset
, ovs_be16 n_bits
,
2292 const void *action
, ovs_be16 action_len
, size_t oxm_offset
,
2293 const struct vl_mff_map
*vl_mff_map
,
2294 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2296 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2299 move
->ofpact
.raw
= ONFACT_RAW13_COPY_FIELD
;
2300 move
->src
.ofs
= ntohs(src_offset
);
2301 move
->src
.n_bits
= ntohs(n_bits
);
2302 move
->dst
.ofs
= ntohs(dst_offset
);
2303 move
->dst
.n_bits
= ntohs(n_bits
);
2305 struct ofpbuf b
= ofpbuf_const_initializer(action
, ntohs(action_len
));
2306 ofpbuf_pull(&b
, oxm_offset
);
2308 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->src
.field
, NULL
,
2313 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->dst
.field
, NULL
,
2319 if (!is_all_zeros(b
.data
, b
.size
)) {
2320 return OFPERR_NXBRC_MUST_BE_ZERO
;
2323 return nxm_reg_move_check(move
, NULL
);
2327 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field
*oacf
,
2328 enum ofp_version ofp_version OVS_UNUSED
,
2329 const struct vl_mff_map
*vl_mff_map
,
2330 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2332 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
2333 oacf
->n_bits
, oacf
, oacf
->len
,
2334 OBJECT_OFFSETOF(oacf
, pad2
), vl_mff_map
,
2335 tlv_bitmap
, ofpacts
);
2339 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field
*oacf
,
2340 enum ofp_version ofp_version OVS_UNUSED
,
2341 const struct vl_mff_map
*vl_mff_map
,
2342 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2344 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
2345 oacf
->n_bits
, oacf
, oacf
->len
,
2346 OBJECT_OFFSETOF(oacf
, pad3
), vl_mff_map
,
2347 tlv_bitmap
, ofpacts
);
2351 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move
*narm
,
2352 enum ofp_version ofp_version OVS_UNUSED
,
2353 const struct vl_mff_map
*vl_mff_map
,
2354 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2356 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2359 move
->ofpact
.raw
= NXAST_RAW_REG_MOVE
;
2360 move
->src
.ofs
= ntohs(narm
->src_ofs
);
2361 move
->src
.n_bits
= ntohs(narm
->n_bits
);
2362 move
->dst
.ofs
= ntohs(narm
->dst_ofs
);
2363 move
->dst
.n_bits
= ntohs(narm
->n_bits
);
2365 struct ofpbuf b
= ofpbuf_const_initializer(narm
, ntohs(narm
->len
));
2366 ofpbuf_pull(&b
, sizeof *narm
);
2368 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->src
.field
, NULL
,
2374 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->dst
.field
, NULL
,
2380 if (!is_all_zeros(b
.data
, b
.size
)) {
2381 return OFPERR_NXBRC_MUST_BE_ZERO
;
2384 return nxm_reg_move_check(move
, NULL
);
2388 encode_REG_MOVE(const struct ofpact_reg_move
*move
,
2389 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2391 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2392 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2393 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2394 * probe for support. Until we have that ability, we currently prefer
2395 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2397 size_t start_ofs
= out
->size
;
2398 if (ofp_version
>= OFP15_VERSION
) {
2399 struct ofp15_action_copy_field
*copy
= put_OFPAT15_COPY_FIELD(out
);
2400 copy
->n_bits
= htons(move
->dst
.n_bits
);
2401 copy
->src_offset
= htons(move
->src
.ofs
);
2402 copy
->dst_offset
= htons(move
->dst
.ofs
);
2403 out
->size
= out
->size
- sizeof copy
->pad2
;
2404 nx_put_mff_header(out
, move
->src
.field
, ofp_version
, false);
2405 nx_put_mff_header(out
, move
->dst
.field
, ofp_version
, false);
2406 } else if (ofp_version
== OFP13_VERSION
2407 && move
->ofpact
.raw
== ONFACT_RAW13_COPY_FIELD
) {
2408 struct onf_action_copy_field
*copy
= put_ONFACT13_COPY_FIELD(out
);
2409 copy
->n_bits
= htons(move
->dst
.n_bits
);
2410 copy
->src_offset
= htons(move
->src
.ofs
);
2411 copy
->dst_offset
= htons(move
->dst
.ofs
);
2412 out
->size
= out
->size
- sizeof copy
->pad3
;
2413 nx_put_mff_header(out
, move
->src
.field
, ofp_version
, false);
2414 nx_put_mff_header(out
, move
->dst
.field
, ofp_version
, false);
2416 struct nx_action_reg_move
*narm
= put_NXAST_REG_MOVE(out
);
2417 narm
->n_bits
= htons(move
->dst
.n_bits
);
2418 narm
->src_ofs
= htons(move
->src
.ofs
);
2419 narm
->dst_ofs
= htons(move
->dst
.ofs
);
2420 nx_put_mff_header(out
, move
->src
.field
, 0, false);
2421 nx_put_mff_header(out
, move
->dst
.field
, 0, false);
2423 pad_ofpat(out
, start_ofs
);
2426 static char * OVS_WARN_UNUSED_RESULT
2427 parse_REG_MOVE(const char *arg
, struct ofpbuf
*ofpacts
,
2428 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2430 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2431 return nxm_parse_reg_move(move
, arg
);
2435 format_REG_MOVE(const struct ofpact_reg_move
*a
, struct ds
*s
)
2437 nxm_format_reg_move(a
, s
);
2440 /* Action structure for OFPAT12_SET_FIELD. */
2441 struct ofp12_action_set_field
{
2442 ovs_be16 type
; /* OFPAT12_SET_FIELD. */
2443 ovs_be16 len
; /* Length is padded to 64 bits. */
2446 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2447 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2449 * The "pad" member is the beginning of the above. */
2452 OFP_ASSERT(sizeof(struct ofp12_action_set_field
) == 8);
2454 /* Action structure for NXAST_REG_LOAD.
2456 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2457 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2458 * starts at 0 for the least-significant bit, 1 for the next most significant
2461 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2462 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2465 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2466 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2467 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2468 * also stored as one less than its true value:
2471 * +------------------------------+------------------+
2472 * | ofs | n_bits - 1 |
2473 * +------------------------------+------------------+
2475 * The switch will reject actions for which ofs+n_bits is greater than the
2476 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2477 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2478 * OFPBAC_BAD_ARGUMENT.
2480 struct nx_action_reg_load
{
2481 ovs_be16 type
; /* OFPAT_VENDOR. */
2482 ovs_be16 len
; /* Length is 24. */
2483 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2484 ovs_be16 subtype
; /* NXAST_REG_LOAD. */
2485 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
2486 ovs_be32 dst
; /* Destination register. */
2487 ovs_be64 value
; /* Immediate value. */
2489 OFP_ASSERT(sizeof(struct nx_action_reg_load
) == 24);
2491 /* The NXAST_REG_LOAD2 action structure is "struct ext_action_header",
2494 * - An NXM/OXM header, value, and optionally a mask.
2495 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2497 * The "pad" member is the beginning of the above. */
2500 decode_ofpat_set_field(const struct ofp12_action_set_field
*oasf
,
2501 bool may_mask
, const struct vl_mff_map
*vl_mff_map
,
2502 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2504 struct ofpbuf b
= ofpbuf_const_initializer(oasf
, ntohs(oasf
->len
));
2505 ofpbuf_pull(&b
, OBJECT_OFFSETOF(oasf
, pad
));
2507 union mf_value value
, mask
;
2508 const struct mf_field
*field
;
2510 error
= mf_vl_mff_nx_pull_entry(&b
, vl_mff_map
, &field
, &value
,
2511 may_mask
? &mask
: NULL
, tlv_bitmap
);
2513 return (error
== OFPERR_OFPBMC_BAD_MASK
2514 ? OFPERR_OFPBAC_BAD_SET_MASK
2519 memset(&mask
, 0xff, field
->n_bytes
);
2522 if (!is_all_zeros(b
.data
, b
.size
)) {
2523 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2526 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2528 if (field
->id
== MFF_IN_PORT_OXM
) {
2529 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2532 /* oxm_length is now validated to be compatible with mf_value. */
2533 if (!field
->writable
) {
2534 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2536 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2539 /* The value must be valid for match. OpenFlow 1.5 also says,
2540 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2541 * a 1-bit in oxm_value and in oxm_mask." */
2542 if (!mf_is_value_valid(field
, &value
)
2543 || (field
->id
== MFF_VLAN_VID
2544 && (!(mask
.be16
& htons(OFPVID12_PRESENT
))
2545 || !(value
.be16
& htons(OFPVID12_PRESENT
))))) {
2546 struct ds ds
= DS_EMPTY_INITIALIZER
;
2547 mf_format(field
, &value
, NULL
, &ds
);
2548 VLOG_WARN_RL(&rl
, "Invalid value for set field %s: %s",
2549 field
->name
, ds_cstr(&ds
));
2552 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2555 ofpact_put_set_field(ofpacts
, field
, &value
, &mask
);
2560 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2561 enum ofp_version ofp_version OVS_UNUSED
,
2562 const struct vl_mff_map
*vl_mff_map
,
2563 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2565 return decode_ofpat_set_field(oasf
, false, vl_mff_map
, tlv_bitmap
,
2570 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2571 enum ofp_version ofp_version OVS_UNUSED
,
2572 const struct vl_mff_map
*vl_mff_map
,
2573 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2575 return decode_ofpat_set_field(oasf
, true, vl_mff_map
, tlv_bitmap
, ofpacts
);
2579 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load
*narl
,
2580 enum ofp_version ofp_version OVS_UNUSED
,
2581 const struct vl_mff_map
*vl_mff_map
,
2582 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
2584 struct mf_subfield dst
;
2587 dst
.ofs
= nxm_decode_ofs(narl
->ofs_nbits
);
2588 dst
.n_bits
= nxm_decode_n_bits(narl
->ofs_nbits
);
2589 error
= mf_vl_mff_mf_from_nxm_header(ntohl(narl
->dst
), vl_mff_map
,
2590 &dst
.field
, tlv_bitmap
);
2595 error
= mf_check_dst(&dst
, NULL
);
2600 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2602 if (dst
.n_bits
< 64 && ntohll(narl
->value
) >> dst
.n_bits
) {
2603 return OFPERR_OFPBAC_BAD_ARGUMENT
;
2606 struct ofpact_set_field
*sf
= ofpact_put_reg_load(out
, dst
.field
, NULL
,
2608 bitwise_put(ntohll(narl
->value
),
2609 sf
->value
, dst
.field
->n_bytes
, dst
.ofs
,
2611 bitwise_put(UINT64_MAX
,
2612 ofpact_set_field_mask(sf
), dst
.field
->n_bytes
, dst
.ofs
,
2618 decode_NXAST_RAW_REG_LOAD2(const struct ext_action_header
*eah
,
2619 enum ofp_version ofp_version OVS_UNUSED
,
2620 const struct vl_mff_map
*vl_mff_map
,
2621 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
2623 struct ofpbuf b
= ofpbuf_const_initializer(eah
, ntohs(eah
->len
));
2624 ofpbuf_pull(&b
, OBJECT_OFFSETOF(eah
, pad
));
2626 union mf_value value
, mask
;
2627 const struct mf_field
*field
;
2629 error
= mf_vl_mff_nx_pull_entry(&b
, vl_mff_map
, &field
, &value
, &mask
,
2635 if (!is_all_zeros(b
.data
, b
.size
)) {
2636 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2639 if (!field
->writable
) {
2640 VLOG_WARN_RL(&rl
, "destination field %s is not writable", field
->name
);
2641 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2644 /* Put value and mask. */
2645 ofpact_put_reg_load2(out
, field
, &value
, &mask
);
2650 put_set_field(struct ofpbuf
*openflow
, enum ofp_version ofp_version
,
2651 enum mf_field_id field
, uint64_t value_
)
2653 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
2654 int n_bytes
= mf_from_id(field
)->n_bytes
;
2655 size_t start_ofs
= openflow
->size
;
2656 union mf_value value
;
2658 value
.be64
= htonll(value_
<< (8 * (8 - n_bytes
)));
2660 oasf
= put_OFPAT12_SET_FIELD(openflow
);
2661 openflow
->size
= openflow
->size
- sizeof oasf
->pad
;
2662 nx_put_entry(openflow
, mf_from_id(field
), ofp_version
, &value
, NULL
);
2663 pad_ofpat(openflow
, start_ofs
);
2667 put_reg_load(struct ofpbuf
*openflow
,
2668 const struct mf_subfield
*dst
, uint64_t value
)
2670 ovs_assert(dst
->n_bits
<= 64);
2672 struct nx_action_reg_load
*narl
= put_NXAST_REG_LOAD(openflow
);
2673 narl
->ofs_nbits
= nxm_encode_ofs_nbits(dst
->ofs
, dst
->n_bits
);
2674 narl
->dst
= htonl(nxm_header_from_mff(dst
->field
));
2675 narl
->value
= htonll(value
);
2679 next_load_segment(const struct ofpact_set_field
*sf
,
2680 struct mf_subfield
*dst
, uint64_t *value
)
2682 int n_bits
= sf
->field
->n_bits
;
2683 int n_bytes
= sf
->field
->n_bytes
;
2684 int start
= dst
->ofs
+ dst
->n_bits
;
2686 if (start
< n_bits
) {
2687 dst
->field
= sf
->field
;
2688 dst
->ofs
= bitwise_scan(ofpact_set_field_mask(sf
), n_bytes
, 1, start
,
2690 if (dst
->ofs
< n_bits
) {
2691 dst
->n_bits
= bitwise_scan(ofpact_set_field_mask(sf
), n_bytes
, 0,
2693 MIN(dst
->ofs
+ 64, n_bits
)) - dst
->ofs
;
2694 *value
= bitwise_get(sf
->value
, n_bytes
, dst
->ofs
, dst
->n_bits
);
2701 /* Convert 'sf' to a series of REG_LOADs. */
2703 set_field_to_nxast(const struct ofpact_set_field
*sf
, struct ofpbuf
*openflow
)
2705 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2706 * experimenter OXM or is variable length (or if it came in as
2707 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2708 * NXAST_REG_LOAD, which is backward compatible. */
2709 if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
2710 || !mf_nxm_header(sf
->field
->id
) || sf
->field
->variable_len
) {
2711 struct ext_action_header
*eah OVS_UNUSED
;
2712 size_t start_ofs
= openflow
->size
;
2714 eah
= put_NXAST_REG_LOAD2(openflow
);
2715 openflow
->size
= openflow
->size
- sizeof eah
->pad
;
2716 nx_put_entry(openflow
, sf
->field
, 0, sf
->value
,
2717 ofpact_set_field_mask(sf
));
2718 pad_ofpat(openflow
, start_ofs
);
2720 struct mf_subfield dst
;
2723 dst
.ofs
= dst
.n_bits
= 0;
2724 while (next_load_segment(sf
, &dst
, &value
)) {
2725 put_reg_load(openflow
, &dst
, value
);
2730 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2731 * actions, if we can, falling back to Nicira extensions if we must.
2733 * We check only meta-flow types that can appear within set field actions and
2734 * that have a mapping to compatible action types. These struct mf_field
2735 * definitions have a defined OXM or NXM header value and specify the field as
2738 set_field_to_legacy_openflow(const struct ofpact_set_field
*sf
,
2739 enum ofp_version ofp_version
,
2742 switch ((int) sf
->field
->id
) {
2743 case MFF_VLAN_TCI
: {
2744 ovs_be16 tci
= sf
->value
->be16
;
2745 bool cfi
= (tci
& htons(VLAN_CFI
)) != 0;
2746 uint16_t vid
= vlan_tci_to_vid(tci
);
2747 uint8_t pcp
= vlan_tci_to_pcp(tci
);
2749 if (ofp_version
< OFP11_VERSION
) {
2750 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2752 * If CFI=1, Add or modify VLAN VID & PCP.
2753 * If CFI=0, strip VLAN header, if any.
2756 put_OFPAT10_SET_VLAN_VID(out
, vid
);
2757 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
2759 put_OFPAT10_STRIP_VLAN(out
);
2762 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2764 * If CFI=1, Add or modify VLAN VID & PCP.
2765 * OpenFlow 1.1 set actions only apply if the packet
2766 * already has VLAN tags. To be sure that is the case
2767 * we have to push a VLAN header. As we do not support
2768 * multiple layers of VLANs, this is a no-op, if a VLAN
2769 * header already exists. This may backfire, however,
2770 * when we start supporting multiple layers of VLANs.
2771 * If CFI=0, strip VLAN header, if any.
2774 /* Push a VLAN tag, if one was not seen at action validation
2776 if (!sf
->flow_has_vlan
) {
2777 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
2779 put_OFPAT11_SET_VLAN_VID(out
, vid
);
2780 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
2782 /* If the flow did not match on vlan, we have no way of
2783 * knowing if the vlan tag exists, so we must POP just to be
2785 put_OFPAT11_POP_VLAN(out
);
2791 case MFF_VLAN_VID
: {
2792 uint16_t vid
= ntohs(sf
->value
->be16
) & VLAN_VID_MASK
;
2793 if (ofp_version
== OFP10_VERSION
) {
2794 put_OFPAT10_SET_VLAN_VID(out
, vid
);
2796 put_OFPAT11_SET_VLAN_VID(out
, vid
);
2802 if (ofp_version
== OFP10_VERSION
) {
2803 put_OFPAT10_SET_VLAN_PCP(out
, sf
->value
->u8
);
2805 put_OFPAT11_SET_VLAN_PCP(out
, sf
->value
->u8
);
2810 put_OFPAT_SET_DL_SRC(out
, ofp_version
)->dl_addr
= sf
->value
->mac
;
2814 put_OFPAT_SET_DL_DST(out
, ofp_version
)->dl_addr
= sf
->value
->mac
;
2818 put_OFPAT_SET_NW_SRC(out
, ofp_version
, sf
->value
->be32
);
2822 put_OFPAT_SET_NW_DST(out
, ofp_version
, sf
->value
->be32
);
2826 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
->u8
);
2829 case MFF_IP_DSCP_SHIFTED
:
2830 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
->u8
<< 2);
2834 struct ofpact_ecn ip_ecn
= { .ecn
= sf
->value
->u8
};
2835 encode_SET_IP_ECN(&ip_ecn
, ofp_version
, out
);
2841 put_OFPAT_SET_TP_SRC(out
, sf
->value
->be16
);
2846 put_OFPAT_SET_TP_DST(out
, sf
->value
->be16
);
2850 set_field_to_nxast(sf
, out
);
2856 set_field_to_set_field(const struct ofpact_set_field
*sf
,
2857 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2859 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
2860 size_t start_ofs
= out
->size
;
2862 oasf
= put_OFPAT12_SET_FIELD(out
);
2863 out
->size
= out
->size
- sizeof oasf
->pad
;
2864 nx_put_entry(out
, sf
->field
, ofp_version
, sf
->value
,
2865 ofpact_set_field_mask(sf
));
2866 pad_ofpat(out
, start_ofs
);
2870 encode_SET_FIELD(const struct ofpact_set_field
*sf
,
2871 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2873 if (ofp_version
>= OFP15_VERSION
) {
2874 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2876 set_field_to_set_field(sf
, ofp_version
, out
);
2877 } else if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD
||
2878 sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
) {
2879 /* It came in as reg_load, send it out the same way. */
2880 set_field_to_nxast(sf
, out
);
2881 } else if (ofp_version
< OFP12_VERSION
) {
2882 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2883 set_field_to_legacy_openflow(sf
, ofp_version
, out
);
2884 } else if (is_all_ones(ofpact_set_field_mask(sf
), sf
->field
->n_bytes
)) {
2885 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2886 * entire field, so encode it as OFPAT_SET_FIELD. */
2887 set_field_to_set_field(sf
, ofp_version
, out
);
2889 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2890 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2891 * so encode it as reg_load. */
2892 set_field_to_nxast(sf
, out
);
2896 /* Parses the input argument 'arg' into the key, value, and delimiter
2897 * components that are common across the reg_load and set_field action format.
2899 * With an argument like "1->metadata", sets the following pointers to
2900 * point within 'arg':
2905 * Returns NULL if successful, otherwise a malloc()'d string describing the
2906 * error. The caller is responsible for freeing the returned string. */
2907 static char * OVS_WARN_UNUSED_RESULT
2908 set_field_split_str(char *arg
, char **key
, char **value
, char **delim
)
2913 value_end
= strstr(arg
, "->");
2914 *key
= value_end
+ strlen("->");
2920 return xasprintf("%s: missing `->'", arg
);
2922 if (strlen(value_end
) <= strlen("->")) {
2923 return xasprintf("%s: missing field name following `->'", arg
);
2929 /* Parses a "set_field" action with argument 'arg', appending the parsed
2930 * action to 'ofpacts'.
2932 * Returns NULL if successful, otherwise a malloc()'d string describing the
2933 * error. The caller is responsible for freeing the returned string. */
2934 static char * OVS_WARN_UNUSED_RESULT
2935 set_field_parse__(char *arg
, struct ofpbuf
*ofpacts
,
2936 enum ofputil_protocol
*usable_protocols
)
2941 const struct mf_field
*mf
;
2942 union mf_value sf_value
, sf_mask
;
2945 error
= set_field_split_str(arg
, &key
, &value
, &delim
);
2950 mf
= mf_from_name(key
);
2952 return xasprintf("%s is not a valid OXM field name", key
);
2954 if (!mf
->writable
) {
2955 return xasprintf("%s is read-only", key
);
2959 error
= mf_parse(mf
, value
, &sf_value
, &sf_mask
);
2964 if (!mf_is_value_valid(mf
, &sf_value
)) {
2965 return xasprintf("%s is not a valid value for field %s", value
, key
);
2968 *usable_protocols
&= mf
->usable_protocols_exact
;
2970 ofpact_put_set_field(ofpacts
, mf
, &sf_value
, &sf_mask
);
2974 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2975 * action to 'ofpacts'.
2977 * Returns NULL if successful, otherwise a malloc()'d string describing the
2978 * error. The caller is responsible for freeing the returned string. */
2979 static char * OVS_WARN_UNUSED_RESULT
2980 parse_SET_FIELD(const char *arg
, struct ofpbuf
*ofpacts
,
2981 enum ofputil_protocol
*usable_protocols
)
2983 char *copy
= xstrdup(arg
);
2984 char *error
= set_field_parse__(copy
, ofpacts
, usable_protocols
);
2989 static char * OVS_WARN_UNUSED_RESULT
2990 parse_reg_load(char *arg
, struct ofpbuf
*ofpacts
)
2992 struct mf_subfield dst
;
2993 char *key
, *value_str
;
2994 union mf_value value
;
2997 error
= set_field_split_str(arg
, &key
, &value_str
, NULL
);
3002 error
= mf_parse_subfield(&dst
, key
);
3007 if (parse_int_string(value_str
, (uint8_t *)&value
, dst
.field
->n_bytes
,
3009 return xasprintf("%s: cannot parse integer value", arg
);
3012 if (!bitwise_is_all_zeros(&value
, dst
.field
->n_bytes
, dst
.n_bits
,
3013 dst
.field
->n_bytes
* 8 - dst
.n_bits
)) {
3017 mf_format(dst
.field
, &value
, NULL
, &ds
);
3018 error
= xasprintf("%s: value %s does not fit into %d bits",
3019 arg
, ds_cstr(&ds
), dst
.n_bits
);
3024 struct ofpact_set_field
*sf
= ofpact_put_reg_load(ofpacts
, dst
.field
, NULL
,
3027 bitwise_copy(&value
, dst
.field
->n_bytes
, 0, sf
->value
,
3028 dst
.field
->n_bytes
, dst
.ofs
, dst
.n_bits
);
3029 bitwise_one(ofpact_set_field_mask(sf
), dst
.field
->n_bytes
, dst
.ofs
,
3035 format_SET_FIELD(const struct ofpact_set_field
*a
, struct ds
*s
)
3037 if (a
->ofpact
.raw
== NXAST_RAW_REG_LOAD
) {
3038 struct mf_subfield dst
;
3041 dst
.ofs
= dst
.n_bits
= 0;
3042 while (next_load_segment(a
, &dst
, &value
)) {
3043 ds_put_format(s
, "%sload:%s%#"PRIx64
"%s->%s",
3044 colors
.special
, colors
.end
, value
,
3045 colors
.special
, colors
.end
);
3046 mf_format_subfield(&dst
, s
);
3047 ds_put_char(s
, ',');
3051 ds_put_format(s
, "%sset_field:%s", colors
.special
, colors
.end
);
3052 mf_format(a
->field
, a
->value
, ofpact_set_field_mask(a
), s
);
3053 ds_put_format(s
, "%s->%s%s",
3054 colors
.special
, colors
.end
, a
->field
->name
);
3058 /* Appends an OFPACT_SET_FIELD ofpact with enough space for the value and mask
3059 * for the 'field' to 'ofpacts' and returns it. Fills in the value from
3060 * 'value', if non-NULL, and mask from 'mask' if non-NULL. If 'value' is
3061 * non-NULL and 'mask' is NULL, an all-ones mask will be filled in. */
3062 struct ofpact_set_field
*
3063 ofpact_put_set_field(struct ofpbuf
*ofpacts
, const struct mf_field
*field
,
3064 const void *value
, const void *mask
)
3066 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(ofpacts
);
3069 /* Fill in the value and mask if given, otherwise put zeroes so that the
3070 * caller may fill in the value and mask itself. */
3072 ofpbuf_put_uninit(ofpacts
, 2 * field
->n_bytes
);
3073 sf
= ofpacts
->header
;
3074 memcpy(sf
->value
, value
, field
->n_bytes
);
3076 memcpy(ofpact_set_field_mask(sf
), mask
, field
->n_bytes
);
3078 memset(ofpact_set_field_mask(sf
), 0xff, field
->n_bytes
);
3081 ofpbuf_put_zeros(ofpacts
, 2 * field
->n_bytes
);
3082 sf
= ofpacts
->header
;
3084 /* Update length. */
3085 ofpact_finish_SET_FIELD(ofpacts
, &sf
);
3090 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
3091 * is marked such that, if possible, it will be translated to OpenFlow as
3092 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
3093 * that was the way that the action was expressed when it came into OVS or for
3094 * backward compatibility. */
3095 struct ofpact_set_field
*
3096 ofpact_put_reg_load(struct ofpbuf
*ofpacts
, const struct mf_field
*field
,
3097 const void *value
, const void *mask
)
3099 struct ofpact_set_field
*sf
= ofpact_put_set_field(ofpacts
, field
, value
,
3101 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD
;
3106 struct ofpact_set_field
*
3107 ofpact_put_reg_load2(struct ofpbuf
*ofpacts
, const struct mf_field
*field
,
3108 const void *value
, const void *mask
)
3110 struct ofpact_set_field
*sf
= ofpact_put_set_field(ofpacts
, field
, value
,
3112 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD2
;
3118 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
3120 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
3123 struct nx_action_stack
{
3124 ovs_be16 type
; /* OFPAT_VENDOR. */
3125 ovs_be16 len
; /* Length is 16. */
3126 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3127 ovs_be16 subtype
; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
3128 ovs_be16 offset
; /* Bit offset into the field. */
3130 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
3131 * - ovs_be16 'n_bits', the number of bits to extract from the field.
3132 * - Enough 0-bytes to pad out the action to 24 bytes. */
3133 uint8_t pad
[12]; /* See above. */
3135 OFP_ASSERT(sizeof(struct nx_action_stack
) == 24);
3138 decode_stack_action(const struct nx_action_stack
*nasp
,
3139 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
3140 struct ofpact_stack
*stack_action
)
3143 stack_action
->subfield
.ofs
= ntohs(nasp
->offset
);
3145 struct ofpbuf b
= ofpbuf_const_initializer(nasp
, sizeof *nasp
);
3146 ofpbuf_pull(&b
, OBJECT_OFFSETOF(nasp
, pad
));
3147 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
,
3148 &stack_action
->subfield
.field
, NULL
,
3154 stack_action
->subfield
.n_bits
= ntohs(*(const ovs_be16
*) b
.data
);
3156 if (!is_all_zeros(b
.data
, b
.size
)) {
3157 return OFPERR_NXBRC_MUST_BE_ZERO
;
3164 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack
*nasp
,
3165 enum ofp_version ofp_version OVS_UNUSED
,
3166 const struct vl_mff_map
*vl_mff_map
,
3167 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
3169 struct ofpact_stack
*push
= ofpact_put_STACK_PUSH(ofpacts
);
3170 enum ofperr error
= decode_stack_action(nasp
, vl_mff_map
, tlv_bitmap
,
3172 return error
? error
: nxm_stack_push_check(push
, NULL
);
3176 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack
*nasp
,
3177 enum ofp_version ofp_version OVS_UNUSED
,
3178 const struct vl_mff_map
*vl_mff_map
,
3179 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
3181 struct ofpact_stack
*pop
= ofpact_put_STACK_POP(ofpacts
);
3182 enum ofperr error
= decode_stack_action(nasp
, vl_mff_map
, tlv_bitmap
,
3184 return error
? error
: nxm_stack_pop_check(pop
, NULL
);
3188 encode_STACK_op(const struct ofpact_stack
*stack_action
,
3189 struct nx_action_stack
*nasp
)
3194 nasp
->offset
= htons(stack_action
->subfield
.ofs
);
3196 ofpbuf_use_stack(&b
, nasp
, ntohs(nasp
->len
));
3197 ofpbuf_put_uninit(&b
, OBJECT_OFFSETOF(nasp
, pad
));
3198 nx_put_mff_header(&b
, stack_action
->subfield
.field
, 0, false);
3199 n_bits
= htons(stack_action
->subfield
.n_bits
);
3200 ofpbuf_put(&b
, &n_bits
, sizeof n_bits
);
3204 encode_STACK_PUSH(const struct ofpact_stack
*stack
,
3205 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3207 encode_STACK_op(stack
, put_NXAST_STACK_PUSH(out
));
3211 encode_STACK_POP(const struct ofpact_stack
*stack
,
3212 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3214 encode_STACK_op(stack
, put_NXAST_STACK_POP(out
));
3217 static char * OVS_WARN_UNUSED_RESULT
3218 parse_STACK_PUSH(char *arg
, struct ofpbuf
*ofpacts
,
3219 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3221 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts
), arg
);
3224 static char * OVS_WARN_UNUSED_RESULT
3225 parse_STACK_POP(char *arg
, struct ofpbuf
*ofpacts
,
3226 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3228 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts
), arg
);
3232 format_STACK_PUSH(const struct ofpact_stack
*a
, struct ds
*s
)
3234 nxm_format_stack_push(a
, s
);
3238 format_STACK_POP(const struct ofpact_stack
*a
, struct ds
*s
)
3240 nxm_format_stack_pop(a
, s
);
3243 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
3245 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
3246 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
3247 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3248 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3250 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3251 * sent only to controllers with id 0.)
3253 struct nx_action_cnt_ids
{
3254 ovs_be16 type
; /* OFPAT_VENDOR. */
3255 ovs_be16 len
; /* Length including slaves. */
3256 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3257 ovs_be16 subtype
; /* NXAST_DEC_TTL_CNT_IDS. */
3259 ovs_be16 n_controllers
; /* Number of controllers. */
3260 uint8_t zeros
[4]; /* Must be zero. */
3262 /* Followed by 1 or more controller ids.
3264 * uint16_t cnt_ids[]; // Controller ids.
3265 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
3268 OFP_ASSERT(sizeof(struct nx_action_cnt_ids
) == 16);
3271 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf
*out
)
3274 struct ofpact_cnt_ids
*ids
;
3275 enum ofperr error
= 0;
3277 ids
= ofpact_put_DEC_TTL(out
);
3278 ids
->n_controllers
= 1;
3279 ofpbuf_put(out
, &id
, sizeof id
);
3281 ofpact_finish_DEC_TTL(out
, &ids
);
3286 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids
*nac_ids
,
3287 enum ofp_version ofp_version OVS_UNUSED
,
3290 struct ofpact_cnt_ids
*ids
;
3294 ids
= ofpact_put_DEC_TTL(out
);
3295 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
3296 ids
->n_controllers
= ntohs(nac_ids
->n_controllers
);
3297 ids_size
= ntohs(nac_ids
->len
) - sizeof *nac_ids
;
3299 if (!is_all_zeros(nac_ids
->zeros
, sizeof nac_ids
->zeros
)) {
3300 return OFPERR_NXBRC_MUST_BE_ZERO
;
3303 if (ids_size
< ids
->n_controllers
* sizeof(ovs_be16
)) {
3304 VLOG_WARN_RL(&rl
, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE
" "
3305 "bytes allocated for controller ids. %"PRIuSIZE
" bytes "
3306 "are required for %u controllers.",
3307 ids_size
, ids
->n_controllers
* sizeof(ovs_be16
),
3308 ids
->n_controllers
);
3309 return OFPERR_OFPBAC_BAD_LEN
;
3312 for (i
= 0; i
< ids
->n_controllers
; i
++) {
3313 uint16_t id
= ntohs(((ovs_be16
*)(nac_ids
+ 1))[i
]);
3314 ofpbuf_put(out
, &id
, sizeof id
);
3318 ofpact_finish_DEC_TTL(out
, &ids
);
3324 encode_DEC_TTL(const struct ofpact_cnt_ids
*dec_ttl
,
3325 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3327 if (dec_ttl
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
3328 || dec_ttl
->n_controllers
!= 1
3329 || dec_ttl
->cnt_ids
[0] != 0) {
3330 struct nx_action_cnt_ids
*nac_ids
= put_NXAST_DEC_TTL_CNT_IDS(out
);
3331 int ids_len
= ROUND_UP(2 * dec_ttl
->n_controllers
, OFP_ACTION_ALIGN
);
3335 nac_ids
->len
= htons(ntohs(nac_ids
->len
) + ids_len
);
3336 nac_ids
->n_controllers
= htons(dec_ttl
->n_controllers
);
3338 ids
= ofpbuf_put_zeros(out
, ids_len
);
3339 for (i
= 0; i
< dec_ttl
->n_controllers
; i
++) {
3340 ids
[i
] = htons(dec_ttl
->cnt_ids
[i
]);
3343 put_OFPAT_DEC_NW_TTL(out
, ofp_version
);
3348 parse_noargs_dec_ttl(struct ofpbuf
*ofpacts
)
3350 struct ofpact_cnt_ids
*ids
;
3353 ofpact_put_DEC_TTL(ofpacts
);
3354 ofpbuf_put(ofpacts
, &id
, sizeof id
);
3355 ids
= ofpacts
->header
;
3356 ids
->n_controllers
++;
3357 ofpact_finish_DEC_TTL(ofpacts
, &ids
);
3360 static char * OVS_WARN_UNUSED_RESULT
3361 parse_DEC_TTL(char *arg
, struct ofpbuf
*ofpacts
,
3362 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3365 parse_noargs_dec_ttl(ofpacts
);
3367 struct ofpact_cnt_ids
*ids
;
3370 ids
= ofpact_put_DEC_TTL(ofpacts
);
3371 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
3372 for (cntr
= strtok_r(arg
, ", ", &arg
); cntr
!= NULL
;
3373 cntr
= strtok_r(NULL
, ", ", &arg
)) {
3374 uint16_t id
= atoi(cntr
);
3376 ofpbuf_put(ofpacts
, &id
, sizeof id
);
3377 ids
= ofpacts
->header
;
3378 ids
->n_controllers
++;
3380 if (!ids
->n_controllers
) {
3381 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3384 ofpact_finish_DEC_TTL(ofpacts
, &ids
);
3390 format_DEC_TTL(const struct ofpact_cnt_ids
*a
, struct ds
*s
)
3394 ds_put_format(s
, "%sdec_ttl%s", colors
.paren
, colors
.end
);
3395 if (a
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
) {
3396 ds_put_format(s
, "%s(%s", colors
.paren
, colors
.end
);
3397 for (i
= 0; i
< a
->n_controllers
; i
++) {
3399 ds_put_cstr(s
, ",");
3401 ds_put_format(s
, "%"PRIu16
, a
->cnt_ids
[i
]);
3403 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
3407 /* Set MPLS label actions. */
3410 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label
,
3411 enum ofp_version ofp_version OVS_UNUSED
,
3414 ofpact_put_SET_MPLS_LABEL(out
)->label
= label
;
3419 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label
*label
,
3420 enum ofp_version ofp_version
,
3423 if (ofp_version
< OFP12_VERSION
) {
3424 put_OFPAT_SET_MPLS_LABEL(out
, ofp_version
, label
->label
);
3426 put_set_field(out
, ofp_version
, MFF_MPLS_LABEL
, ntohl(label
->label
));
3430 static char * OVS_WARN_UNUSED_RESULT
3431 parse_SET_MPLS_LABEL(char *arg
, struct ofpbuf
*ofpacts
,
3432 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3434 struct ofpact_mpls_label
*mpls_label
= ofpact_put_SET_MPLS_LABEL(ofpacts
);
3436 return xstrdup("set_mpls_label: expected label.");
3439 mpls_label
->label
= htonl(atoi(arg
));
3444 format_SET_MPLS_LABEL(const struct ofpact_mpls_label
*a
, struct ds
*s
)
3446 ds_put_format(s
, "%sset_mpls_label(%s%"PRIu32
"%s)%s",
3447 colors
.paren
, colors
.end
, ntohl(a
->label
),
3448 colors
.paren
, colors
.end
);
3451 /* Set MPLS TC actions. */
3454 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc
,
3455 enum ofp_version ofp_version OVS_UNUSED
,
3458 ofpact_put_SET_MPLS_TC(out
)->tc
= tc
;
3463 encode_SET_MPLS_TC(const struct ofpact_mpls_tc
*tc
,
3464 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3466 if (ofp_version
< OFP12_VERSION
) {
3467 put_OFPAT_SET_MPLS_TC(out
, ofp_version
, tc
->tc
);
3469 put_set_field(out
, ofp_version
, MFF_MPLS_TC
, tc
->tc
);
3473 static char * OVS_WARN_UNUSED_RESULT
3474 parse_SET_MPLS_TC(char *arg
, struct ofpbuf
*ofpacts
,
3475 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3477 struct ofpact_mpls_tc
*mpls_tc
= ofpact_put_SET_MPLS_TC(ofpacts
);
3480 return xstrdup("set_mpls_tc: expected tc.");
3483 mpls_tc
->tc
= atoi(arg
);
3488 format_SET_MPLS_TC(const struct ofpact_mpls_tc
*a
, struct ds
*s
)
3490 ds_put_format(s
, "%sset_mpls_ttl(%s%"PRIu8
"%s)%s",
3491 colors
.paren
, colors
.end
, a
->tc
,
3492 colors
.paren
, colors
.end
);
3495 /* Set MPLS TTL actions. */
3498 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl
,
3499 enum ofp_version ofp_version OVS_UNUSED
,
3502 ofpact_put_SET_MPLS_TTL(out
)->ttl
= ttl
;
3507 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*ttl
,
3508 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3510 put_OFPAT_SET_MPLS_TTL(out
, ofp_version
, ttl
->ttl
);
3513 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3514 * action to 'ofpacts'.
3516 * Returns NULL if successful, otherwise a malloc()'d string describing the
3517 * error. The caller is responsible for freeing the returned string. */
3518 static char * OVS_WARN_UNUSED_RESULT
3519 parse_SET_MPLS_TTL(char *arg
, struct ofpbuf
*ofpacts
,
3520 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3522 struct ofpact_mpls_ttl
*mpls_ttl
= ofpact_put_SET_MPLS_TTL(ofpacts
);
3525 return xstrdup("set_mpls_ttl: expected ttl.");
3528 mpls_ttl
->ttl
= atoi(arg
);
3533 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*a
, struct ds
*s
)
3535 ds_put_format(s
, "%sset_mpls_ttl(%s%"PRIu8
"%s)%s",
3536 colors
.paren
, colors
.end
, a
->ttl
,
3537 colors
.paren
, colors
.end
);
3540 /* Decrement MPLS TTL actions. */
3543 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf
*out
)
3545 ofpact_put_DEC_MPLS_TTL(out
);
3550 encode_DEC_MPLS_TTL(const struct ofpact_null
*null OVS_UNUSED
,
3551 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3553 put_OFPAT_DEC_MPLS_TTL(out
, ofp_version
);
3556 static char * OVS_WARN_UNUSED_RESULT
3557 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
3558 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3560 ofpact_put_DEC_MPLS_TTL(ofpacts
);
3565 format_DEC_MPLS_TTL(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
3567 ds_put_format(s
, "%sdec_mpls_ttl%s", colors
.value
, colors
.end
);
3570 /* Push MPLS label action. */
3573 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype
,
3574 enum ofp_version ofp_version OVS_UNUSED
,
3577 struct ofpact_push_mpls
*oam
;
3579 if (!eth_type_mpls(ethertype
)) {
3580 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3582 oam
= ofpact_put_PUSH_MPLS(out
);
3583 oam
->ethertype
= ethertype
;
3589 encode_PUSH_MPLS(const struct ofpact_push_mpls
*push_mpls
,
3590 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3592 put_OFPAT_PUSH_MPLS(out
, ofp_version
, push_mpls
->ethertype
);
3595 static char * OVS_WARN_UNUSED_RESULT
3596 parse_PUSH_MPLS(char *arg
, struct ofpbuf
*ofpacts
,
3597 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3602 error
= str_to_u16(arg
, "push_mpls", ðertype
);
3604 ofpact_put_PUSH_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
3610 format_PUSH_MPLS(const struct ofpact_push_mpls
*a
, struct ds
*s
)
3612 ds_put_format(s
, "%spush_mpls:%s0x%04"PRIx16
,
3613 colors
.param
, colors
.end
, ntohs(a
->ethertype
));
3616 /* Pop MPLS label action. */
3619 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype
,
3620 enum ofp_version ofp_version OVS_UNUSED
,
3623 ofpact_put_POP_MPLS(out
)->ethertype
= ethertype
;
3628 encode_POP_MPLS(const struct ofpact_pop_mpls
*pop_mpls
,
3629 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3631 put_OFPAT_POP_MPLS(out
, ofp_version
, pop_mpls
->ethertype
);
3634 static char * OVS_WARN_UNUSED_RESULT
3635 parse_POP_MPLS(char *arg
, struct ofpbuf
*ofpacts
,
3636 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3641 error
= str_to_u16(arg
, "pop_mpls", ðertype
);
3643 ofpact_put_POP_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
3649 format_POP_MPLS(const struct ofpact_pop_mpls
*a
, struct ds
*s
)
3651 ds_put_format(s
, "%spop_mpls:%s0x%04"PRIx16
,
3652 colors
.param
, colors
.end
, ntohs(a
->ethertype
));
3655 /* Set tunnel ID actions. */
3658 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id
,
3659 enum ofp_version ofp_version OVS_UNUSED
,
3662 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
3663 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL
;
3664 tunnel
->tun_id
= tun_id
;
3669 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id
,
3670 enum ofp_version ofp_version OVS_UNUSED
,
3673 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
3674 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL64
;
3675 tunnel
->tun_id
= tun_id
;
3680 encode_SET_TUNNEL(const struct ofpact_tunnel
*tunnel
,
3681 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3683 uint64_t tun_id
= tunnel
->tun_id
;
3685 if (ofp_version
< OFP12_VERSION
) {
3686 if (tun_id
<= UINT32_MAX
3687 && tunnel
->ofpact
.raw
!= NXAST_RAW_SET_TUNNEL64
) {
3688 put_NXAST_SET_TUNNEL(out
, tun_id
);
3690 put_NXAST_SET_TUNNEL64(out
, tun_id
);
3693 put_set_field(out
, ofp_version
, MFF_TUN_ID
, tun_id
);
3697 static char * OVS_WARN_UNUSED_RESULT
3698 parse_set_tunnel(char *arg
, struct ofpbuf
*ofpacts
,
3699 enum ofp_raw_action_type raw
)
3701 struct ofpact_tunnel
*tunnel
;
3703 tunnel
= ofpact_put_SET_TUNNEL(ofpacts
);
3704 tunnel
->ofpact
.raw
= raw
;
3705 return str_to_u64(arg
, &tunnel
->tun_id
);
3708 static char * OVS_WARN_UNUSED_RESULT
3709 parse_SET_TUNNEL(char *arg
, struct ofpbuf
*ofpacts
,
3710 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3712 return parse_set_tunnel(arg
, ofpacts
, NXAST_RAW_SET_TUNNEL
);
3716 format_SET_TUNNEL(const struct ofpact_tunnel
*a
, struct ds
*s
)
3718 ds_put_format(s
, "%sset_tunnel%s:%s%#"PRIx64
, colors
.param
,
3719 (a
->tun_id
> UINT32_MAX
3720 || a
->ofpact
.raw
== NXAST_RAW_SET_TUNNEL64
? "64" : ""),
3721 colors
.end
, a
->tun_id
);
3724 /* Set queue action. */
3727 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id
,
3728 enum ofp_version ofp_version OVS_UNUSED
,
3731 ofpact_put_SET_QUEUE(out
)->queue_id
= queue_id
;
3736 encode_SET_QUEUE(const struct ofpact_queue
*queue
,
3737 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3739 put_OFPAT_SET_QUEUE(out
, ofp_version
, queue
->queue_id
);
3742 static char * OVS_WARN_UNUSED_RESULT
3743 parse_SET_QUEUE(char *arg
, struct ofpbuf
*ofpacts
,
3744 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3746 return str_to_u32(arg
, &ofpact_put_SET_QUEUE(ofpacts
)->queue_id
);
3750 format_SET_QUEUE(const struct ofpact_queue
*a
, struct ds
*s
)
3752 ds_put_format(s
, "%sset_queue:%s%"PRIu32
,
3753 colors
.param
, colors
.end
, a
->queue_id
);
3756 /* Pop queue action. */
3759 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf
*out
)
3761 ofpact_put_POP_QUEUE(out
);
3766 encode_POP_QUEUE(const struct ofpact_null
*null OVS_UNUSED
,
3767 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3769 put_NXAST_POP_QUEUE(out
);
3772 static char * OVS_WARN_UNUSED_RESULT
3773 parse_POP_QUEUE(const char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
3774 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3776 ofpact_put_POP_QUEUE(ofpacts
);
3781 format_POP_QUEUE(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
3783 ds_put_format(s
, "%spop_queue%s", colors
.value
, colors
.end
);
3786 /* Action structure for NXAST_FIN_TIMEOUT.
3788 * This action changes the idle timeout or hard timeout, or both, of this
3789 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3790 * When such a packet is observed, the action reduces the rule's idle timeout
3791 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3792 * action has no effect on an existing timeout that is already shorter than the
3793 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3794 * of zero has no effect on the respective timeout.
3796 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3797 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3798 * receipt of the FIN or RST.
3800 * This is useful for quickly discarding learned TCP flows that otherwise will
3801 * take a long time to expire.
3803 * This action is intended for use with an OpenFlow rule that matches only a
3804 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3805 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3806 * RST in any of those flows will cause the entire OpenFlow rule to expire
3807 * early, which is not normally desirable.
3809 struct nx_action_fin_timeout
{
3810 ovs_be16 type
; /* OFPAT_VENDOR. */
3811 ovs_be16 len
; /* 16. */
3812 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3813 ovs_be16 subtype
; /* NXAST_FIN_TIMEOUT. */
3814 ovs_be16 fin_idle_timeout
; /* New idle timeout, if nonzero. */
3815 ovs_be16 fin_hard_timeout
; /* New hard timeout, if nonzero. */
3816 ovs_be16 pad
; /* Must be zero. */
3818 OFP_ASSERT(sizeof(struct nx_action_fin_timeout
) == 16);
3821 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout
*naft
,
3822 enum ofp_version ofp_version OVS_UNUSED
,
3825 struct ofpact_fin_timeout
*oft
;
3827 oft
= ofpact_put_FIN_TIMEOUT(out
);
3828 oft
->fin_idle_timeout
= ntohs(naft
->fin_idle_timeout
);
3829 oft
->fin_hard_timeout
= ntohs(naft
->fin_hard_timeout
);
3834 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout
*fin_timeout
,
3835 enum ofp_version ofp_version OVS_UNUSED
,
3838 struct nx_action_fin_timeout
*naft
= put_NXAST_FIN_TIMEOUT(out
);
3839 naft
->fin_idle_timeout
= htons(fin_timeout
->fin_idle_timeout
);
3840 naft
->fin_hard_timeout
= htons(fin_timeout
->fin_hard_timeout
);
3843 static char * OVS_WARN_UNUSED_RESULT
3844 parse_FIN_TIMEOUT(char *arg
, struct ofpbuf
*ofpacts
,
3845 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3847 struct ofpact_fin_timeout
*oft
= ofpact_put_FIN_TIMEOUT(ofpacts
);
3850 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
3853 if (!strcmp(key
, "idle_timeout")) {
3854 error
= str_to_u16(value
, key
, &oft
->fin_idle_timeout
);
3855 } else if (!strcmp(key
, "hard_timeout")) {
3856 error
= str_to_u16(value
, key
, &oft
->fin_hard_timeout
);
3858 error
= xasprintf("invalid key '%s' in 'fin_timeout' argument",
3870 format_FIN_TIMEOUT(const struct ofpact_fin_timeout
*a
, struct ds
*s
)
3872 ds_put_format(s
, "%sfin_timeout(%s", colors
.paren
, colors
.end
);
3873 if (a
->fin_idle_timeout
) {
3874 ds_put_format(s
, "%sidle_timeout=%s%"PRIu16
",",
3875 colors
.param
, colors
.end
, a
->fin_idle_timeout
);
3877 if (a
->fin_hard_timeout
) {
3878 ds_put_format(s
, "%shard_timeout=%s%"PRIu16
",",
3879 colors
.param
, colors
.end
, a
->fin_hard_timeout
);
3882 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
3885 /* Action structures for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE, and
3886 * NXAST_RESUBMIT_TABLE_CT.
3888 * These actions search one of the switch's flow tables:
3890 * - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the
3891 * 'table' member is not 255, then it specifies the table to search.
3893 * - Otherwise (for NXAST_RESUBMIT_TABLE or NXAST_RESUBMIT_TABLE_CT with a
3894 * 'table' of 255, or for NXAST_RESUBMIT regardless of 'table'), it
3895 * searches the current flow table, that is, the OpenFlow flow table that
3896 * contains the flow from which this action was obtained. If this action
3897 * did not come from a flow table (e.g. it came from an OFPT_PACKET_OUT
3898 * message), then table 0 is the current table.
3900 * The flow table lookup uses a flow that may be slightly modified from the
3903 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3904 * is used as the flow's in_port.
3906 * - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the 'in_port'
3907 * member is not OFPP_IN_PORT, then its value is used as the flow's
3908 * in_port. Otherwise, the original in_port is used.
3910 * - For NXAST_RESUBMIT_TABLE_CT the Conntrack 5-tuple fields are used as
3911 * the packets IP header fields during the lookup.
3913 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3914 * resubmit action, then the flow is updated with the new values.
3916 * Following the lookup, the original in_port is restored.
3918 * If the modified flow matched in the flow table, then the corresponding
3919 * actions are executed. Afterward, actions following the resubmit in the
3920 * original set of actions, if any, are executed; any changes made to the
3921 * packet (e.g. changes to VLAN) by secondary actions persist when those
3922 * actions are executed, although the original in_port is restored.
3924 * Resubmit actions may be used any number of times within a set of actions.
3926 * Resubmit actions may nest. To prevent infinite loops and excessive resource
3927 * use, the implementation may limit nesting depth and the total number of
3930 * - Open vSwitch 1.0.1 and earlier did not support recursion.
3932 * - Open vSwitch 1.0.2 and 1.0.3 limited recursion to 8 levels.
3934 * - Open vSwitch 1.1 and 1.2 limited recursion to 16 levels.
3936 * - Open vSwitch 1.2 through 1.8 limited recursion to 32 levels.
3938 * - Open vSwitch 1.9 through 2.0 limited recursion to 64 levels.
3940 * - Open vSwitch 2.1 through 2.5 limited recursion to 64 levels and impose
3941 * a total limit of 4,096 resubmits per flow translation (earlier versions
3942 * did not impose any total limit).
3944 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE and
3945 * NXAST_RESUBMIT_TABLE_CT require 'pad' to be all-bits-zero.
3947 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3948 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE. Open vSwitch before
3949 * 2.8.0 did not support NXAST_RESUBMIT_TABLE_CT.
3951 struct nx_action_resubmit
{
3952 ovs_be16 type
; /* OFPAT_VENDOR. */
3953 ovs_be16 len
; /* Length is 16. */
3954 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3955 ovs_be16 subtype
; /* NXAST_RESUBMIT. */
3956 ovs_be16 in_port
; /* New in_port for checking flow table. */
3957 uint8_t table
; /* NXAST_RESUBMIT_TABLE: table to use. */
3960 OFP_ASSERT(sizeof(struct nx_action_resubmit
) == 16);
3963 decode_NXAST_RAW_RESUBMIT(uint16_t port
,
3964 enum ofp_version ofp_version OVS_UNUSED
,
3967 struct ofpact_resubmit
*resubmit
;
3969 resubmit
= ofpact_put_RESUBMIT(out
);
3970 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT
;
3971 resubmit
->in_port
= u16_to_ofp(port
);
3972 resubmit
->table_id
= 0xff;
3977 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit
*nar
,
3978 enum ofp_version ofp_version OVS_UNUSED
,
3981 struct ofpact_resubmit
*resubmit
;
3983 if (nar
->pad
[0] || nar
->pad
[1] || nar
->pad
[2]) {
3984 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3987 resubmit
= ofpact_put_RESUBMIT(out
);
3988 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT_TABLE
;
3989 resubmit
->in_port
= u16_to_ofp(ntohs(nar
->in_port
));
3990 resubmit
->table_id
= nar
->table
;
3995 decode_NXAST_RAW_RESUBMIT_TABLE_CT(const struct nx_action_resubmit
*nar
,
3996 enum ofp_version ofp_version OVS_UNUSED
,
3999 enum ofperr error
= decode_NXAST_RAW_RESUBMIT_TABLE(nar
, ofp_version
, out
);
4003 struct ofpact_resubmit
*resubmit
= out
->header
;
4004 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT_TABLE_CT
;
4005 resubmit
->with_ct_orig
= true;
4010 encode_RESUBMIT(const struct ofpact_resubmit
*resubmit
,
4011 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4013 uint16_t in_port
= ofp_to_u16(resubmit
->in_port
);
4015 if (resubmit
->table_id
== 0xff
4016 && resubmit
->ofpact
.raw
== NXAST_RAW_RESUBMIT
) {
4017 put_NXAST_RESUBMIT(out
, in_port
);
4019 struct nx_action_resubmit
*nar
;
4020 nar
= resubmit
->with_ct_orig
4021 ? put_NXAST_RESUBMIT_TABLE_CT(out
) : put_NXAST_RESUBMIT_TABLE(out
);
4022 nar
->table
= resubmit
->table_id
;
4023 nar
->in_port
= htons(in_port
);
4027 static char * OVS_WARN_UNUSED_RESULT
4028 parse_RESUBMIT(char *arg
, struct ofpbuf
*ofpacts
,
4029 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4031 struct ofpact_resubmit
*resubmit
;
4032 char *in_port_s
, *table_s
, *ct_s
;
4034 resubmit
= ofpact_put_RESUBMIT(ofpacts
);
4036 in_port_s
= strsep(&arg
, ",");
4037 if (in_port_s
&& in_port_s
[0]) {
4038 if (!ofputil_port_from_string(in_port_s
, &resubmit
->in_port
)) {
4039 return xasprintf("%s: resubmit to unknown port", in_port_s
);
4042 resubmit
->in_port
= OFPP_IN_PORT
;
4045 table_s
= strsep(&arg
, ",");
4046 if (table_s
&& table_s
[0]) {
4047 uint32_t table_id
= 0;
4050 error
= str_to_u32(table_s
, &table_id
);
4054 resubmit
->table_id
= table_id
;
4056 resubmit
->table_id
= 255;
4059 ct_s
= strsep(&arg
, ",");
4060 if (ct_s
&& ct_s
[0]) {
4061 if (strcmp(ct_s
, "ct")) {
4062 return xasprintf("%s: unknown parameter", ct_s
);
4064 resubmit
->with_ct_orig
= true;
4066 resubmit
->with_ct_orig
= false;
4069 if (resubmit
->in_port
== OFPP_IN_PORT
&& resubmit
->table_id
== 255) {
4070 return xstrdup("at least one \"in_port\" or \"table\" must be "
4071 "specified on resubmit");
4077 format_RESUBMIT(const struct ofpact_resubmit
*a
, struct ds
*s
)
4079 if (a
->in_port
!= OFPP_IN_PORT
&& a
->table_id
== 255) {
4080 ds_put_format(s
, "%sresubmit:%s", colors
.special
, colors
.end
);
4081 ofputil_format_port(a
->in_port
, s
);
4083 ds_put_format(s
, "%sresubmit(%s", colors
.paren
, colors
.end
);
4084 if (a
->in_port
!= OFPP_IN_PORT
) {
4085 ofputil_format_port(a
->in_port
, s
);
4087 ds_put_char(s
, ',');
4088 if (a
->table_id
!= 255) {
4089 ds_put_format(s
, "%"PRIu8
, a
->table_id
);
4091 if (a
->with_ct_orig
) {
4092 ds_put_cstr(s
, ",ct");
4094 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
4098 /* Action structure for NXAST_LEARN and NXAST_LEARN2.
4100 * This action adds or modifies a flow in an OpenFlow table, similar to
4101 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
4102 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
4103 * flow's match criteria and actions are built by applying each of the series
4104 * of flow_mod_spec elements included as part of the action.
4106 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
4107 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
4108 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
4109 * bits from a source to a destination. In this case, the header contains
4112 * 15 14 13 12 11 10 0
4113 * +------+---+------+---------------------------------+
4114 * | 0 |src| dst | n_bits |
4115 * +------+---+------+---------------------------------+
4117 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
4118 * following table summarizes the meaning of each possible combination.
4119 * Details follow the table:
4122 * --- --- ----------------------------------------------------------
4123 * 0 0 Add match criteria based on value in a field.
4124 * 1 0 Add match criteria based on an immediate value.
4125 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
4126 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
4127 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
4128 * All other combinations are undefined and not allowed.
4130 * The flow_mod_spec header is followed by a source specification and a
4131 * destination specification. The format and meaning of the source
4132 * specification depends on 'src':
4134 * - If 'src' is 0, the source bits are taken from a field in the flow to
4135 * which this action is attached. (This should be a wildcarded field. If
4136 * its value is fully specified then the source bits being copied have
4139 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
4140 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
4141 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
4142 * 'field' and 'ofs' are subject to the same restrictions as the source
4143 * field in NXAST_REG_MOVE.
4145 * - If 'src' is 1, the source bits are a constant value. The source
4146 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
4147 * number in network order, the source bits are the 'n_bits'
4148 * least-significant bits. The switch will report an error if other bits
4149 * in the constant are nonzero.
4151 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
4152 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
4153 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
4154 * meaning of the flow_mod_spec depends on 'dst':
4156 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
4157 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
4158 * packet equal the source bits. 'field' may be any nxm_header with
4159 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
4161 * Order is significant. Earlier flow_mod_specs must satisfy any
4162 * prerequisites for matching fields specified later, by copying constant
4163 * values into prerequisite fields.
4165 * The switch will reject flow_mod_specs that do not satisfy NXM masking
4168 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
4169 * the new flow. The new flow copies the source bits into
4170 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
4173 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
4174 * greater than 64 yields multiple NXAST_REG_LOAD actions.
4176 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
4177 * empty. It has the following meaning:
4179 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
4180 * The new flow outputs to the OpenFlow port specified by the source field.
4181 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
4182 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
4185 * Resource Management
4186 * -------------------
4188 * A switch has a finite amount of flow table space available for learning.
4189 * When this space is exhausted, no new learning table entries will be learned
4190 * until some existing flow table entries expire. The controller should be
4191 * prepared to handle this by flooding (which can be implemented as a
4192 * low-priority flow).
4194 * If a learned flow matches a single TCP stream with a relatively long
4195 * timeout, one may make the best of resource constraints by setting
4196 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
4197 * both, to shorter timeouts. When either of these is specified as a nonzero
4198 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
4204 * The following examples give a prose description of the flow_mod_specs along
4205 * with informal notation for how those would be represented and a hex dump of
4206 * the bytes that would be required.
4208 * These examples could work with various nx_action_learn parameters. Typical
4209 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
4210 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
4212 * 1. Learn input port based on the source MAC, with lookup into
4213 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
4215 * Match on in_port=99:
4216 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
4217 * ovs_be16(99), 00 63
4218 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4220 * Match Ethernet destination on Ethernet source from packet:
4221 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4222 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4223 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4225 * Set NXM_NX_REG1[16:31] to the packet's input port:
4226 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
4227 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4228 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
4230 * Given a packet that arrived on port A with Ethernet source address B,
4231 * this would set up the flow "in_port=99, dl_dst=B,
4232 * actions=load:A->NXM_NX_REG1[16..31]".
4234 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
4235 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4236 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
4238 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
4239 * into NXM_NX_REG1[16:31]:
4241 * Match on same VLAN ID as packet:
4242 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
4243 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4244 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4246 * Match Ethernet destination on Ethernet source from packet:
4247 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4248 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4249 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4251 * Output to the packet's input port:
4252 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
4253 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4255 * Given a packet that arrived on port A with Ethernet source address B in
4256 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
4257 * actions=output:A".
4259 * In syntax accepted by ovs-ofctl, this action is:
4260 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4261 * output:NXM_OF_IN_PORT[])
4263 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
4264 * 10-second MAC expiration time to make it easier to see what's going on
4266 * ovs-vsctl del-controller br0
4267 * ovs-ofctl del-flows br0
4268 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
4269 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
4270 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
4271 output:NXM_OF_IN_PORT[]), resubmit(,1)"
4272 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
4274 * You can then dump the MAC learning table with:
4276 * ovs-ofctl dump-flows br0 table=1
4281 * For best performance, segregate learned flows into a table that is not used
4282 * for any other flows except possibly for a lowest-priority "catch-all" flow
4283 * (a flow with no match criteria). If different learning actions specify
4284 * different match criteria, use different tables for the learned flows.
4286 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
4287 * These timeouts apply to the flow that is added, which means that a flow with
4288 * an idle timeout will expire when no traffic has been sent *to* the learned
4289 * address. This is not usually the intent in MAC learning; instead, we want
4290 * the MAC learn entry to expire when no traffic has been sent *from* the
4291 * learned address. Use a hard timeout for that.
4294 * Visibility of Changes
4295 * ---------------------
4297 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
4298 * flow translation are visible to flow table lookups made later in the flow
4299 * translation. This means that, in the example above, a MAC learned by the
4300 * learn action in table 0 would be found in table 1 (if the packet being
4301 * processed had the same source and destination MAC address).
4303 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
4304 * modify a flow) by a "learn" action are visible only for later flow
4305 * translations, not for later lookups within the same flow translation. In
4306 * the MAC learning example, a MAC learned by the learn action in table 0 would
4307 * not be found in table 1 if the flow translation would resubmit to table 1
4308 * after the processing of the learn action, meaning that if this MAC had not
4309 * been learned before then the packet would be flooded. */
4310 struct nx_action_learn
{
4311 ovs_be16 type
; /* OFPAT_VENDOR. */
4312 ovs_be16 len
; /* At least 24. */
4313 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4314 ovs_be16 subtype
; /* NXAST_LEARN. */
4315 ovs_be16 idle_timeout
; /* Idle time before discarding (seconds). */
4316 ovs_be16 hard_timeout
; /* Max time before discarding (seconds). */
4317 ovs_be16 priority
; /* Priority level of flow entry. */
4318 ovs_be64 cookie
; /* Cookie for new flow. */
4319 ovs_be16 flags
; /* NX_LEARN_F_*. */
4320 uint8_t table_id
; /* Table to insert flow entry. */
4321 uint8_t pad
; /* Must be zero. */
4322 ovs_be16 fin_idle_timeout
; /* Idle timeout after FIN, if nonzero. */
4323 ovs_be16 fin_hard_timeout
; /* Hard timeout after FIN, if nonzero. */
4324 /* Followed by a sequence of flow_mod_spec elements, as described above,
4325 * until the end of the action is reached. */
4327 OFP_ASSERT(sizeof(struct nx_action_learn
) == 32);
4329 struct nx_action_learn2
{
4330 struct nx_action_learn up
; /* The wire format includes nx_action_learn. */
4331 ovs_be32 limit
; /* Maximum number of learned flows. */
4333 /* Where to store the result. */
4334 ovs_be16 result_dst_ofs
; /* Starting bit offset in destination. */
4336 ovs_be16 pad2
; /* Must be zero. */
4338 * - OXM/NXM header for destination field (4 or 8 bytes),
4339 * if NX_LEARN_F_WRITE_RESULT is set in 'flags'
4340 * - a sequence of flow_mod_spec elements, as described above,
4341 * until the end of the action is reached. */
4343 OFP_ASSERT(sizeof(struct nx_action_learn2
) == 40);
4346 get_be16(const void **pp
)
4348 const ovs_be16
*p
= *pp
;
4349 ovs_be16 value
= *p
;
4355 get_be32(const void **pp
)
4357 const ovs_be32
*p
= *pp
;
4358 ovs_be32 value
= get_unaligned_be32(p
);
4364 get_subfield(int n_bits
, const void **p
, struct mf_subfield
*sf
,
4365 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
)
4369 error
= mf_vl_mff_mf_from_nxm_header(ntohl(get_be32(p
)), vl_mff_map
,
4370 &sf
->field
, tlv_bitmap
);
4371 sf
->ofs
= ntohs(get_be16(p
));
4372 sf
->n_bits
= n_bits
;
4377 learn_min_len(uint16_t header
)
4379 int n_bits
= header
& NX_LEARN_N_BITS_MASK
;
4380 int src_type
= header
& NX_LEARN_SRC_MASK
;
4381 int dst_type
= header
& NX_LEARN_DST_MASK
;
4382 unsigned int min_len
;
4385 if (src_type
== NX_LEARN_SRC_FIELD
) {
4386 min_len
+= sizeof(ovs_be32
); /* src_field */
4387 min_len
+= sizeof(ovs_be16
); /* src_ofs */
4389 min_len
+= DIV_ROUND_UP(n_bits
, 16);
4391 if (dst_type
== NX_LEARN_DST_MATCH
||
4392 dst_type
== NX_LEARN_DST_LOAD
) {
4393 min_len
+= sizeof(ovs_be32
); /* dst_field */
4394 min_len
+= sizeof(ovs_be16
); /* dst_ofs */
4400 decode_LEARN_common(const struct nx_action_learn
*nal
,
4401 struct ofpact_learn
*learn
)
4404 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4407 learn
->idle_timeout
= ntohs(nal
->idle_timeout
);
4408 learn
->hard_timeout
= ntohs(nal
->hard_timeout
);
4409 learn
->priority
= ntohs(nal
->priority
);
4410 learn
->cookie
= nal
->cookie
;
4411 learn
->table_id
= nal
->table_id
;
4412 learn
->fin_idle_timeout
= ntohs(nal
->fin_idle_timeout
);
4413 learn
->fin_hard_timeout
= ntohs(nal
->fin_hard_timeout
);
4414 learn
->flags
= ntohs(nal
->flags
);
4416 if (learn
->table_id
== 0xff) {
4417 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4424 decode_LEARN_specs(const void *p
, const void *end
,
4425 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
4426 struct ofpbuf
*ofpacts
)
4428 struct ofpact_learn
*learn
= ofpacts
->header
;
4431 struct ofpact_learn_spec
*spec
;
4432 uint16_t header
= ntohs(get_be16(&p
));
4438 spec
= ofpbuf_put_zeros(ofpacts
, sizeof *spec
);
4439 learn
= ofpacts
->header
;
4441 spec
->src_type
= header
& NX_LEARN_SRC_MASK
;
4442 spec
->dst_type
= header
& NX_LEARN_DST_MASK
;
4443 spec
->n_bits
= header
& NX_LEARN_N_BITS_MASK
;
4445 /* Check for valid src and dst type combination. */
4446 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
4447 spec
->dst_type
== NX_LEARN_DST_LOAD
||
4448 (spec
->dst_type
== NX_LEARN_DST_OUTPUT
&&
4449 spec
->src_type
== NX_LEARN_SRC_FIELD
)) {
4452 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4455 /* Check that the arguments don't overrun the end of the action. */
4456 if ((char *) end
- (char *) p
< learn_min_len(header
)) {
4457 return OFPERR_OFPBAC_BAD_LEN
;
4460 /* Get the source. */
4461 const uint8_t *imm
= NULL
;
4462 unsigned int imm_bytes
= 0;
4464 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
4465 error
= get_subfield(spec
->n_bits
, &p
, &spec
->src
, vl_mff_map
,
4471 int p_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
4472 p
= (const uint8_t *) p
+ p_bytes
;
4474 imm_bytes
= DIV_ROUND_UP(spec
->n_bits
, 8);
4475 imm
= (const uint8_t *) p
- imm_bytes
;
4478 /* Get the destination. */
4479 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
4480 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
4481 error
= get_subfield(spec
->n_bits
, &p
, &spec
->dst
, vl_mff_map
,
4489 uint8_t *src_imm
= ofpbuf_put_zeros(ofpacts
,
4490 OFPACT_ALIGN(imm_bytes
));
4491 memcpy(src_imm
, imm
, imm_bytes
);
4493 learn
= ofpacts
->header
;
4496 ofpact_finish_LEARN(ofpacts
, &learn
);
4498 if (!is_all_zeros(p
, (char *) end
- (char *) p
)) {
4499 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4505 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
4506 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
4508 decode_NXAST_RAW_LEARN(const struct nx_action_learn
*nal
,
4509 enum ofp_version ofp_version OVS_UNUSED
,
4510 const struct vl_mff_map
*vl_mff_map
,
4511 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
4513 struct ofpact_learn
*learn
;
4516 learn
= ofpact_put_LEARN(ofpacts
);
4518 error
= decode_LEARN_common(nal
, learn
);
4523 if (learn
->flags
& ~(NX_LEARN_F_SEND_FLOW_REM
|
4524 NX_LEARN_F_DELETE_LEARNED
)) {
4525 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4528 return decode_LEARN_specs(nal
+ 1, (char *) nal
+ ntohs(nal
->len
),
4529 vl_mff_map
, tlv_bitmap
, ofpacts
);
4532 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
4533 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
4535 decode_NXAST_RAW_LEARN2(const struct nx_action_learn2
*nal
,
4536 enum ofp_version ofp_version OVS_UNUSED
,
4537 const struct vl_mff_map
*vl_mff_map
,
4538 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
4540 struct ofpbuf b
= ofpbuf_const_initializer(nal
, ntohs(nal
->up
.len
));
4541 struct ofpact_learn
*learn
;
4545 return OFPERR_NXBAC_MUST_BE_ZERO
;
4548 learn
= ofpact_put_LEARN(ofpacts
);
4549 error
= decode_LEARN_common(&nal
->up
, learn
);
4554 learn
->limit
= ntohl(nal
->limit
);
4556 if (learn
->flags
& ~(NX_LEARN_F_SEND_FLOW_REM
|
4557 NX_LEARN_F_DELETE_LEARNED
|
4558 NX_LEARN_F_WRITE_RESULT
)) {
4559 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4562 ofpbuf_pull(&b
, sizeof *nal
);
4564 if (learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
4565 error
= nx_pull_header(&b
, vl_mff_map
, &learn
->result_dst
.field
, NULL
);
4569 if (!learn
->result_dst
.field
->writable
) {
4570 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
4572 learn
->result_dst
.ofs
= ntohs(nal
->result_dst_ofs
);
4573 learn
->result_dst
.n_bits
= 1;
4574 } else if (nal
->result_dst_ofs
) {
4575 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4578 return decode_LEARN_specs(b
.data
, (char *) nal
+ ntohs(nal
->up
.len
),
4579 vl_mff_map
, tlv_bitmap
, ofpacts
);
4583 put_be16(struct ofpbuf
*b
, ovs_be16 x
)
4585 ofpbuf_put(b
, &x
, sizeof x
);
4589 put_be32(struct ofpbuf
*b
, ovs_be32 x
)
4591 ofpbuf_put(b
, &x
, sizeof x
);
4595 put_u16(struct ofpbuf
*b
, uint16_t x
)
4597 put_be16(b
, htons(x
));
4601 put_u32(struct ofpbuf
*b
, uint32_t x
)
4603 put_be32(b
, htonl(x
));
4607 encode_LEARN(const struct ofpact_learn
*learn
,
4608 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4610 const struct ofpact_learn_spec
*spec
;
4611 struct nx_action_learn
*nal
;
4614 start_ofs
= out
->size
;
4616 if (learn
->ofpact
.raw
== NXAST_RAW_LEARN2
4617 || learn
->limit
!= 0
4618 || learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
4619 struct nx_action_learn2
*nal2
;
4621 nal2
= put_NXAST_LEARN2(out
);
4622 nal2
->limit
= htonl(learn
->limit
);
4623 nal2
->result_dst_ofs
= htons(learn
->result_dst
.ofs
);
4626 nal
= put_NXAST_LEARN(out
);
4628 nal
->idle_timeout
= htons(learn
->idle_timeout
);
4629 nal
->hard_timeout
= htons(learn
->hard_timeout
);
4630 nal
->fin_idle_timeout
= htons(learn
->fin_idle_timeout
);
4631 nal
->fin_hard_timeout
= htons(learn
->fin_hard_timeout
);
4632 nal
->priority
= htons(learn
->priority
);
4633 nal
->cookie
= learn
->cookie
;
4634 nal
->flags
= htons(learn
->flags
);
4635 nal
->table_id
= learn
->table_id
;
4637 if (learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
4638 nx_put_header(out
, learn
->result_dst
.field
->id
, 0, false);
4641 OFPACT_LEARN_SPEC_FOR_EACH (spec
, learn
) {
4642 put_u16(out
, spec
->n_bits
| spec
->dst_type
| spec
->src_type
);
4644 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
4645 put_u32(out
, nxm_header_from_mff(spec
->src
.field
));
4646 put_u16(out
, spec
->src
.ofs
);
4648 size_t n_dst_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
4649 uint8_t *bits
= ofpbuf_put_zeros(out
, n_dst_bytes
);
4650 unsigned int n_bytes
= DIV_ROUND_UP(spec
->n_bits
, 8);
4652 memcpy(bits
+ n_dst_bytes
- n_bytes
, ofpact_learn_spec_imm(spec
),
4656 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
4657 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
4658 put_u32(out
, nxm_header_from_mff(spec
->dst
.field
));
4659 put_u16(out
, spec
->dst
.ofs
);
4663 pad_ofpat(out
, start_ofs
);
4666 static char * OVS_WARN_UNUSED_RESULT
4667 parse_LEARN(char *arg
, struct ofpbuf
*ofpacts
,
4668 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4670 return learn_parse(arg
, ofpacts
);
4674 format_LEARN(const struct ofpact_learn
*a
, struct ds
*s
)
4679 /* Action structure for NXAST_CONJUNCTION. */
4680 struct nx_action_conjunction
{
4681 ovs_be16 type
; /* OFPAT_VENDOR. */
4682 ovs_be16 len
; /* At least 16. */
4683 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4684 ovs_be16 subtype
; /* See enum ofp_raw_action_type. */
4689 OFP_ASSERT(sizeof(struct nx_action_conjunction
) == 16);
4692 add_conjunction(struct ofpbuf
*out
,
4693 uint32_t id
, uint8_t clause
, uint8_t n_clauses
)
4695 struct ofpact_conjunction
*oc
;
4697 oc
= ofpact_put_CONJUNCTION(out
);
4699 oc
->clause
= clause
;
4700 oc
->n_clauses
= n_clauses
;
4704 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction
*nac
,
4705 enum ofp_version ofp_version OVS_UNUSED
,
4708 if (nac
->n_clauses
< 2 || nac
->n_clauses
> 64
4709 || nac
->clause
>= nac
->n_clauses
) {
4710 return OFPERR_NXBAC_BAD_CONJUNCTION
;
4712 add_conjunction(out
, ntohl(nac
->id
), nac
->clause
, nac
->n_clauses
);
4718 encode_CONJUNCTION(const struct ofpact_conjunction
*oc
,
4719 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4721 struct nx_action_conjunction
*nac
= put_NXAST_CONJUNCTION(out
);
4722 nac
->clause
= oc
->clause
;
4723 nac
->n_clauses
= oc
->n_clauses
;
4724 nac
->id
= htonl(oc
->id
);
4728 format_CONJUNCTION(const struct ofpact_conjunction
*oc
, struct ds
*s
)
4730 ds_put_format(s
, "%sconjunction(%s%"PRIu32
",%d/%"PRIu8
"%s)%s",
4731 colors
.paren
, colors
.end
,
4732 oc
->id
, oc
->clause
+ 1, oc
->n_clauses
,
4733 colors
.paren
, colors
.end
);
4736 static char * OVS_WARN_UNUSED_RESULT
4737 parse_CONJUNCTION(const char *arg
, struct ofpbuf
*ofpacts
,
4738 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4745 if (!ovs_scan(arg
, "%"SCNi32
" , %"SCNu8
" / %"SCNu8
" %n",
4746 &id
, &clause
, &n_clauses
, &n
) || n
!= strlen(arg
)) {
4747 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4750 if (n_clauses
< 2) {
4751 return xstrdup("conjunction must have at least 2 clauses");
4752 } else if (n_clauses
> 64) {
4753 return xstrdup("conjunction must have at most 64 clauses");
4754 } else if (clause
< 1) {
4755 return xstrdup("clause index must be positive");
4756 } else if (clause
> n_clauses
) {
4757 return xstrdup("clause index must be less than or equal to "
4758 "number of clauses");
4761 add_conjunction(ofpacts
, id
, clause
- 1, n_clauses
);
4765 /* Action structure for NXAST_MULTIPATH.
4767 * This action performs the following steps in sequence:
4769 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4770 * Refer to the definition of "enum nx_mp_fields" for details.
4772 * The 'basis' value is used as a universal hash parameter, that is,
4773 * different values of 'basis' yield different hash functions. The
4774 * particular universal hash function used is implementation-defined.
4776 * The hashed fields' values are drawn from the current state of the
4777 * flow, including all modifications that have been made by actions up to
4780 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4781 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4784 * The output of the algorithm is 'link', an unsigned integer less than
4785 * or equal to 'max_link'.
4787 * Some algorithms use 'arg' as an additional argument.
4789 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4790 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4793 * The switch will reject actions that have an unknown 'fields', or an unknown
4794 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4795 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4796 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4798 struct nx_action_multipath
{
4799 ovs_be16 type
; /* OFPAT_VENDOR. */
4800 ovs_be16 len
; /* Length is 32. */
4801 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4802 ovs_be16 subtype
; /* NXAST_MULTIPATH. */
4804 /* What fields to hash and how. */
4805 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
4806 ovs_be16 basis
; /* Universal hash parameter. */
4809 /* Multipath link choice algorithm to apply to hash value. */
4810 ovs_be16 algorithm
; /* One of NX_MP_ALG_*. */
4811 ovs_be16 max_link
; /* Number of output links, minus 1. */
4812 ovs_be32 arg
; /* Algorithm-specific argument. */
4815 /* Where to store the result. */
4816 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
4817 ovs_be32 dst
; /* Destination. */
4819 OFP_ASSERT(sizeof(struct nx_action_multipath
) == 32);
4822 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath
*nam
,
4823 enum ofp_version ofp_version OVS_UNUSED
,
4824 const struct vl_mff_map
*vl_mff_map
,
4825 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
4827 uint32_t n_links
= ntohs(nam
->max_link
) + 1;
4828 size_t min_n_bits
= log_2_ceil(n_links
);
4829 struct ofpact_multipath
*mp
;
4832 mp
= ofpact_put_MULTIPATH(out
);
4833 mp
->fields
= ntohs(nam
->fields
);
4834 mp
->basis
= ntohs(nam
->basis
);
4835 mp
->algorithm
= ntohs(nam
->algorithm
);
4836 mp
->max_link
= ntohs(nam
->max_link
);
4837 mp
->arg
= ntohl(nam
->arg
);
4838 mp
->dst
.ofs
= nxm_decode_ofs(nam
->ofs_nbits
);
4839 mp
->dst
.n_bits
= nxm_decode_n_bits(nam
->ofs_nbits
);
4840 error
= mf_vl_mff_mf_from_nxm_header(ntohl(nam
->dst
), vl_mff_map
,
4841 &mp
->dst
.field
, tlv_bitmap
);
4846 if (!flow_hash_fields_valid(mp
->fields
)) {
4847 VLOG_WARN_RL(&rl
, "unsupported fields %d", (int) mp
->fields
);
4848 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4849 } else if (mp
->algorithm
!= NX_MP_ALG_MODULO_N
4850 && mp
->algorithm
!= NX_MP_ALG_HASH_THRESHOLD
4851 && mp
->algorithm
!= NX_MP_ALG_HRW
4852 && mp
->algorithm
!= NX_MP_ALG_ITER_HASH
) {
4853 VLOG_WARN_RL(&rl
, "unsupported algorithm %d", (int) mp
->algorithm
);
4854 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4855 } else if (mp
->dst
.n_bits
< min_n_bits
) {
4856 VLOG_WARN_RL(&rl
, "multipath action requires at least %"PRIuSIZE
" bits for "
4857 "%"PRIu32
" links", min_n_bits
, n_links
);
4858 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4861 return multipath_check(mp
, NULL
);
4865 encode_MULTIPATH(const struct ofpact_multipath
*mp
,
4866 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4868 struct nx_action_multipath
*nam
= put_NXAST_MULTIPATH(out
);
4870 nam
->fields
= htons(mp
->fields
);
4871 nam
->basis
= htons(mp
->basis
);
4872 nam
->algorithm
= htons(mp
->algorithm
);
4873 nam
->max_link
= htons(mp
->max_link
);
4874 nam
->arg
= htonl(mp
->arg
);
4875 nam
->ofs_nbits
= nxm_encode_ofs_nbits(mp
->dst
.ofs
, mp
->dst
.n_bits
);
4876 nam
->dst
= htonl(nxm_header_from_mff(mp
->dst
.field
));
4879 static char * OVS_WARN_UNUSED_RESULT
4880 parse_MULTIPATH(const char *arg
, struct ofpbuf
*ofpacts
,
4881 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4883 return multipath_parse(ofpact_put_MULTIPATH(ofpacts
), arg
);
4887 format_MULTIPATH(const struct ofpact_multipath
*a
, struct ds
*s
)
4889 multipath_format(a
, s
);
4892 /* Action structure for NXAST_NOTE.
4894 * This action has no effect. It is variable length. The switch does not
4895 * attempt to interpret the user-defined 'note' data in any way. A controller
4896 * can use this action to attach arbitrary metadata to a flow.
4898 * This action might go away in the future.
4900 struct nx_action_note
{
4901 ovs_be16 type
; /* OFPAT_VENDOR. */
4902 ovs_be16 len
; /* A multiple of 8, but at least 16. */
4903 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4904 ovs_be16 subtype
; /* NXAST_NOTE. */
4905 uint8_t note
[6]; /* Start of user-defined data. */
4906 /* Possibly followed by additional user-defined data. */
4908 OFP_ASSERT(sizeof(struct nx_action_note
) == 16);
4911 decode_NXAST_RAW_NOTE(const struct nx_action_note
*nan
,
4912 enum ofp_version ofp_version OVS_UNUSED
,
4915 struct ofpact_note
*note
;
4916 unsigned int length
;
4918 length
= ntohs(nan
->len
) - offsetof(struct nx_action_note
, note
);
4919 note
= ofpact_put_NOTE(out
);
4920 note
->length
= length
;
4921 ofpbuf_put(out
, nan
->note
, length
);
4923 ofpact_finish_NOTE(out
, ¬e
);
4929 encode_NOTE(const struct ofpact_note
*note
,
4930 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4932 size_t start_ofs
= out
->size
;
4933 struct nx_action_note
*nan
;
4935 put_NXAST_NOTE(out
);
4936 out
->size
= out
->size
- sizeof nan
->note
;
4938 ofpbuf_put(out
, note
->data
, note
->length
);
4939 pad_ofpat(out
, start_ofs
);
4942 static char * OVS_WARN_UNUSED_RESULT
4943 parse_NOTE(const char *arg
, struct ofpbuf
*ofpacts
,
4944 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4946 size_t start_ofs
= ofpacts
->size
;
4947 ofpact_put_NOTE(ofpacts
);
4948 arg
= ofpbuf_put_hex(ofpacts
, arg
, NULL
);
4950 return xstrdup("bad hex digit in `note' argument");
4952 struct ofpact_note
*note
= ofpbuf_at_assert(ofpacts
, start_ofs
,
4954 note
->length
= ofpacts
->size
- (start_ofs
+ sizeof *note
);
4955 ofpact_finish_NOTE(ofpacts
, ¬e
);
4960 format_NOTE(const struct ofpact_note
*a
, struct ds
*s
)
4962 ds_put_format(s
, "%snote:%s", colors
.param
, colors
.end
);
4963 format_hex_arg(s
, a
->data
, a
->length
);
4969 decode_NXAST_RAW_EXIT(struct ofpbuf
*out
)
4971 ofpact_put_EXIT(out
);
4976 encode_EXIT(const struct ofpact_null
*null OVS_UNUSED
,
4977 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4979 put_NXAST_EXIT(out
);
4982 static char * OVS_WARN_UNUSED_RESULT
4983 parse_EXIT(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
4984 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4986 ofpact_put_EXIT(ofpacts
);
4991 format_EXIT(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
4993 ds_put_format(s
, "%sexit%s", colors
.special
, colors
.end
);
4996 /* Unroll xlate action. */
4999 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate
*unroll OVS_UNUSED
,
5000 enum ofp_version ofp_version OVS_UNUSED
,
5001 struct ofpbuf
*out OVS_UNUSED
)
5006 static char * OVS_WARN_UNUSED_RESULT
5007 parse_UNROLL_XLATE(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts OVS_UNUSED
,
5008 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
5015 format_UNROLL_XLATE(const struct ofpact_unroll_xlate
*a
, struct ds
*s
)
5017 ds_put_format(s
, "%sunroll_xlate(%s%stable=%s%"PRIu8
5018 ", %scookie=%s%"PRIu64
"%s)%s",
5019 colors
.paren
, colors
.end
,
5020 colors
.special
, colors
.end
, a
->rule_table_id
,
5021 colors
.param
, colors
.end
, ntohll(a
->rule_cookie
),
5022 colors
.paren
, colors
.end
);
5025 /* The NXAST_CLONE action is "struct ext_action_header", followed by zero or
5026 * more embedded OpenFlow actions. */
5029 decode_NXAST_RAW_CLONE(const struct ext_action_header
*eah
,
5030 enum ofp_version ofp_version
,
5031 const struct vl_mff_map
*vl_mff_map
,
5032 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
5035 struct ofpbuf openflow
;
5036 const size_t clone_offset
= ofpacts_pull(out
);
5037 struct ofpact_nest
*clone
= ofpact_put_CLONE(out
);
5039 /* decode action list */
5040 ofpbuf_pull(out
, sizeof(*clone
));
5041 openflow
= ofpbuf_const_initializer(
5042 eah
+ 1, ntohs(eah
->len
) - sizeof *eah
);
5043 error
= ofpacts_pull_openflow_actions__(&openflow
, openflow
.size
,
5045 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
5046 out
, 0, vl_mff_map
, tlv_bitmap
);
5047 clone
= ofpbuf_push_uninit(out
, sizeof *clone
);
5048 out
->header
= &clone
->ofpact
;
5049 ofpact_finish_CLONE(out
, &clone
);
5050 ofpbuf_push_uninit(out
, clone_offset
);
5055 encode_CLONE(const struct ofpact_nest
*clone
,
5056 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5059 const size_t ofs
= out
->size
;
5060 struct ext_action_header
*eah
;
5062 eah
= put_NXAST_CLONE(out
);
5063 len
= ofpacts_put_openflow_actions(clone
->actions
,
5064 ofpact_nest_get_action_len(clone
),
5067 eah
= ofpbuf_at(out
, ofs
, sizeof *eah
);
5068 eah
->len
= htons(len
);
5071 static char * OVS_WARN_UNUSED_RESULT
5072 parse_CLONE(char *arg
, struct ofpbuf
*ofpacts
,
5073 enum ofputil_protocol
*usable_protocols
)
5075 const size_t clone_offset
= ofpacts_pull(ofpacts
);
5076 struct ofpact_nest
*clone
= ofpact_put_CLONE(ofpacts
);
5079 ofpbuf_pull(ofpacts
, sizeof *clone
);
5080 error
= ofpacts_parse_copy(arg
, ofpacts
, usable_protocols
, false, 0);
5081 /* header points to the action list */
5082 ofpacts
->header
= ofpbuf_push_uninit(ofpacts
, sizeof *clone
);
5083 clone
= ofpacts
->header
;
5085 ofpact_finish_CLONE(ofpacts
, &clone
);
5086 ofpbuf_push_uninit(ofpacts
, clone_offset
);
5091 format_CLONE(const struct ofpact_nest
*a
, struct ds
*s
)
5093 ds_put_format(s
, "%sclone(%s", colors
.paren
, colors
.end
);
5094 ofpacts_format(a
->actions
, ofpact_nest_get_action_len(a
), s
);
5095 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
5098 /* Action structure for NXAST_SAMPLE.
5100 * Samples matching packets with the given probability and sends them
5101 * each to the set of collectors identified with the given ID. The
5102 * probability is expressed as a number of packets to be sampled out
5103 * of USHRT_MAX packets, and must be >0.
5105 * When sending packet samples to IPFIX collectors, the IPFIX flow
5106 * record sent for each sampled packet is associated with the given
5107 * observation domain ID and observation point ID. Each IPFIX flow
5108 * record contain the sampled packet's headers when executing this
5109 * rule. If a sampled packet's headers are modified by previous
5110 * actions in the flow, those modified headers are sent. */
5111 struct nx_action_sample
{
5112 ovs_be16 type
; /* OFPAT_VENDOR. */
5113 ovs_be16 len
; /* Length is 24. */
5114 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5115 ovs_be16 subtype
; /* NXAST_SAMPLE. */
5116 ovs_be16 probability
; /* Fraction of packets to sample. */
5117 ovs_be32 collector_set_id
; /* ID of collector set in OVSDB. */
5118 ovs_be32 obs_domain_id
; /* ID of sampling observation domain. */
5119 ovs_be32 obs_point_id
; /* ID of sampling observation point. */
5121 OFP_ASSERT(sizeof(struct nx_action_sample
) == 24);
5123 /* Action structure for NXAST_SAMPLE2 and NXAST_SAMPLE3.
5125 * NXAST_SAMPLE2 was added in Open vSwitch 2.5.90. Compared to NXAST_SAMPLE,
5126 * it adds support for exporting egress tunnel information.
5128 * NXAST_SAMPLE3 was added in Open vSwitch 2.6.90. Compared to NXAST_SAMPLE2,
5129 * it adds support for the 'direction' field. */
5130 struct nx_action_sample2
{
5131 ovs_be16 type
; /* OFPAT_VENDOR. */
5132 ovs_be16 len
; /* Length is 32. */
5133 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5134 ovs_be16 subtype
; /* NXAST_SAMPLE. */
5135 ovs_be16 probability
; /* Fraction of packets to sample. */
5136 ovs_be32 collector_set_id
; /* ID of collector set in OVSDB. */
5137 ovs_be32 obs_domain_id
; /* ID of sampling observation domain. */
5138 ovs_be32 obs_point_id
; /* ID of sampling observation point. */
5139 ovs_be16 sampling_port
; /* Sampling port. */
5140 uint8_t direction
; /* NXAST_SAMPLE3 only. */
5141 uint8_t zeros
[5]; /* Pad to a multiple of 8 bytes */
5143 OFP_ASSERT(sizeof(struct nx_action_sample2
) == 32);
5146 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample
*nas
,
5147 enum ofp_version ofp_version OVS_UNUSED
,
5150 struct ofpact_sample
*sample
;
5152 sample
= ofpact_put_SAMPLE(out
);
5153 sample
->ofpact
.raw
= NXAST_RAW_SAMPLE
;
5154 sample
->probability
= ntohs(nas
->probability
);
5155 sample
->collector_set_id
= ntohl(nas
->collector_set_id
);
5156 sample
->obs_domain_id
= ntohl(nas
->obs_domain_id
);
5157 sample
->obs_point_id
= ntohl(nas
->obs_point_id
);
5158 sample
->sampling_port
= OFPP_NONE
;
5159 sample
->direction
= NX_ACTION_SAMPLE_DEFAULT
;
5161 if (sample
->probability
== 0) {
5162 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5169 decode_SAMPLE2(const struct nx_action_sample2
*nas
,
5170 enum ofp_raw_action_type raw
,
5171 enum nx_action_sample_direction direction
,
5172 struct ofpact_sample
*sample
)
5174 sample
->ofpact
.raw
= raw
;
5175 sample
->probability
= ntohs(nas
->probability
);
5176 sample
->collector_set_id
= ntohl(nas
->collector_set_id
);
5177 sample
->obs_domain_id
= ntohl(nas
->obs_domain_id
);
5178 sample
->obs_point_id
= ntohl(nas
->obs_point_id
);
5179 sample
->sampling_port
= u16_to_ofp(ntohs(nas
->sampling_port
));
5180 sample
->direction
= direction
;
5182 if (sample
->probability
== 0) {
5183 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5190 decode_NXAST_RAW_SAMPLE2(const struct nx_action_sample2
*nas
,
5191 enum ofp_version ofp_version OVS_UNUSED
,
5194 return decode_SAMPLE2(nas
, NXAST_RAW_SAMPLE2
, NX_ACTION_SAMPLE_DEFAULT
,
5195 ofpact_put_SAMPLE(out
));
5199 decode_NXAST_RAW_SAMPLE3(const struct nx_action_sample2
*nas
,
5200 enum ofp_version ofp_version OVS_UNUSED
,
5203 struct ofpact_sample
*sample
= ofpact_put_SAMPLE(out
);
5204 if (!is_all_zeros(nas
->zeros
, sizeof nas
->zeros
)) {
5205 return OFPERR_NXBRC_MUST_BE_ZERO
;
5207 if (nas
->direction
!= NX_ACTION_SAMPLE_DEFAULT
&&
5208 nas
->direction
!= NX_ACTION_SAMPLE_INGRESS
&&
5209 nas
->direction
!= NX_ACTION_SAMPLE_EGRESS
) {
5210 VLOG_WARN_RL(&rl
, "invalid sample direction %"PRIu8
, nas
->direction
);
5211 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5213 return decode_SAMPLE2(nas
, NXAST_RAW_SAMPLE3
, nas
->direction
, sample
);
5217 encode_SAMPLE2(const struct ofpact_sample
*sample
,
5218 struct nx_action_sample2
*nas
)
5220 nas
->probability
= htons(sample
->probability
);
5221 nas
->collector_set_id
= htonl(sample
->collector_set_id
);
5222 nas
->obs_domain_id
= htonl(sample
->obs_domain_id
);
5223 nas
->obs_point_id
= htonl(sample
->obs_point_id
);
5224 nas
->sampling_port
= htons(ofp_to_u16(sample
->sampling_port
));
5225 nas
->direction
= sample
->direction
;
5229 encode_SAMPLE(const struct ofpact_sample
*sample
,
5230 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
5232 if (sample
->ofpact
.raw
== NXAST_RAW_SAMPLE3
5233 || sample
->direction
!= NX_ACTION_SAMPLE_DEFAULT
) {
5234 encode_SAMPLE2(sample
, put_NXAST_SAMPLE3(out
));
5235 } else if (sample
->ofpact
.raw
== NXAST_RAW_SAMPLE2
5236 || sample
->sampling_port
!= OFPP_NONE
) {
5237 encode_SAMPLE2(sample
, put_NXAST_SAMPLE2(out
));
5239 struct nx_action_sample
*nas
= put_NXAST_SAMPLE(out
);
5240 nas
->probability
= htons(sample
->probability
);
5241 nas
->collector_set_id
= htonl(sample
->collector_set_id
);
5242 nas
->obs_domain_id
= htonl(sample
->obs_domain_id
);
5243 nas
->obs_point_id
= htonl(sample
->obs_point_id
);
5247 /* Parses 'arg' as the argument to a "sample" action, and appends such an
5248 * action to 'ofpacts'.
5250 * Returns NULL if successful, otherwise a malloc()'d string describing the
5251 * error. The caller is responsible for freeing the returned string. */
5252 static char * OVS_WARN_UNUSED_RESULT
5253 parse_SAMPLE(char *arg
, struct ofpbuf
*ofpacts
,
5254 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
5256 struct ofpact_sample
*os
= ofpact_put_SAMPLE(ofpacts
);
5257 os
->sampling_port
= OFPP_NONE
;
5258 os
->direction
= NX_ACTION_SAMPLE_DEFAULT
;
5261 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
5264 if (!strcmp(key
, "probability")) {
5265 error
= str_to_u16(value
, "probability", &os
->probability
);
5266 if (!error
&& os
->probability
== 0) {
5267 error
= xasprintf("invalid probability value \"%s\"", value
);
5269 } else if (!strcmp(key
, "collector_set_id")) {
5270 error
= str_to_u32(value
, &os
->collector_set_id
);
5271 } else if (!strcmp(key
, "obs_domain_id")) {
5272 error
= str_to_u32(value
, &os
->obs_domain_id
);
5273 } else if (!strcmp(key
, "obs_point_id")) {
5274 error
= str_to_u32(value
, &os
->obs_point_id
);
5275 } else if (!strcmp(key
, "sampling_port")) {
5276 if (!ofputil_port_from_string(value
, &os
->sampling_port
)) {
5277 error
= xasprintf("%s: unknown port", value
);
5279 } else if (!strcmp(key
, "ingress")) {
5280 os
->direction
= NX_ACTION_SAMPLE_INGRESS
;
5281 } else if (!strcmp(key
, "egress")) {
5282 os
->direction
= NX_ACTION_SAMPLE_EGRESS
;
5284 error
= xasprintf("invalid key \"%s\" in \"sample\" argument",
5291 if (os
->probability
== 0) {
5292 return xstrdup("non-zero \"probability\" must be specified on sample");
5299 format_SAMPLE(const struct ofpact_sample
*a
, struct ds
*s
)
5301 ds_put_format(s
, "%ssample(%s%sprobability=%s%"PRIu16
5302 ",%scollector_set_id=%s%"PRIu32
5303 ",%sobs_domain_id=%s%"PRIu32
5304 ",%sobs_point_id=%s%"PRIu32
,
5305 colors
.paren
, colors
.end
,
5306 colors
.param
, colors
.end
, a
->probability
,
5307 colors
.param
, colors
.end
, a
->collector_set_id
,
5308 colors
.param
, colors
.end
, a
->obs_domain_id
,
5309 colors
.param
, colors
.end
, a
->obs_point_id
);
5310 if (a
->sampling_port
!= OFPP_NONE
) {
5311 ds_put_format(s
, ",%ssampling_port=%s%"PRIu32
,
5312 colors
.param
, colors
.end
, a
->sampling_port
);
5314 if (a
->direction
== NX_ACTION_SAMPLE_INGRESS
) {
5315 ds_put_format(s
, ",%singress%s", colors
.param
, colors
.end
);
5316 } else if (a
->direction
== NX_ACTION_SAMPLE_EGRESS
) {
5317 ds_put_format(s
, ",%segress%s", colors
.param
, colors
.end
);
5319 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
5322 /* debug_recirc instruction. */
5324 static bool enable_debug
;
5327 ofpact_dummy_enable(void)
5329 enable_debug
= true;
5333 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf
*out
)
5335 if (!enable_debug
) {
5336 return OFPERR_OFPBAC_BAD_VENDOR_TYPE
;
5339 ofpact_put_DEBUG_RECIRC(out
);
5344 encode_DEBUG_RECIRC(const struct ofpact_null
*n OVS_UNUSED
,
5345 enum ofp_version ofp_version OVS_UNUSED
,
5348 put_NXAST_DEBUG_RECIRC(out
);
5351 static char * OVS_WARN_UNUSED_RESULT
5352 parse_DEBUG_RECIRC(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
5353 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
5355 ofpact_put_DEBUG_RECIRC(ofpacts
);
5360 format_DEBUG_RECIRC(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
5362 ds_put_format(s
, "%sdebug_recirc%s", colors
.value
, colors
.end
);
5365 /* Action structure for NXAST_CT.
5367 * Pass traffic to the connection tracker.
5369 * There are two important concepts to understanding the connection tracking
5370 * interface: Packet state and Connection state. Packets may be "Untracked" or
5371 * "Tracked". Connections may be "Uncommitted" or "Committed".
5375 * Untracked packets have not yet passed through the connection tracker,
5376 * and the connection state for such packets is unknown. In most cases,
5377 * packets entering the OpenFlow pipeline will initially be in the
5378 * untracked state. Untracked packets may become tracked by executing
5379 * NXAST_CT with a "recirc_table" specified. This makes various aspects
5380 * about the connection available, in particular the connection state.
5382 * Tracked packets have previously passed through the connection tracker.
5383 * These packets will remain tracked through until the end of the OpenFlow
5384 * pipeline. Tracked packets which have NXAST_CT executed with a
5385 * "recirc_table" specified will return to the tracked state.
5387 * The packet state is only significant for the duration of packet
5388 * processing within the OpenFlow pipeline.
5390 * - Connection State:
5392 * Multiple packets may be associated with a single connection. Initially,
5393 * all connections are uncommitted. The connection state corresponding to
5394 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
5396 * Uncommitted connections have no state stored about them. Uncommitted
5397 * connections may transition into the committed state by executing
5398 * NXAST_CT with the NX_CT_F_COMMIT flag.
5400 * Once a connection becomes committed, information may be gathered about
5401 * the connection by passing subsequent packets through the connection
5402 * tracker, and the state of the connection will be stored beyond the
5403 * lifetime of packet processing.
5405 * A committed connection always has the directionality of the packet that
5406 * caused the connection to be committed in the first place. This is the
5407 * "original direction" of the connection, and the opposite direction is
5408 * the "reply direction". If a connection is already committed, but it is
5409 * then decided that the original direction should be the opposite of the
5410 * existing connection, NX_CT_F_FORCE flag may be used in addition to
5411 * NX_CT_F_COMMIT flag to in effect terminate the existing connection and
5412 * start a new one in the current direction.
5414 * Connections may transition back into the uncommitted state due to
5415 * external timers, or due to the contents of packets that are sent to the
5416 * connection tracker. This behaviour is outside of the scope of the
5417 * OpenFlow interface.
5419 * The "zone" specifies a context within which the tracking is done:
5421 * The connection tracking zone is a 16-bit number. Each zone is an
5422 * independent connection tracking context. The connection state for each
5423 * connection is completely separate for each zone, so if a connection
5424 * is committed to zone A, then it will remain uncommitted in zone B.
5425 * If NXAST_CT is executed with the same zone multiple times, later
5426 * executions have no effect.
5428 * If 'zone_src' is nonzero, this specifies that the zone should be
5429 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
5430 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
5431 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
5432 * are the same as the acceptable nxm_header values for the 'src' field of
5435 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
5436 * connection tracking zone.
5438 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
5440 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
5441 * packet will be logically cloned prior to executing this action. One
5442 * copy will be sent to the connection tracker, then will be re-injected
5443 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
5444 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
5445 * fields will be populated. The original instance of the packet will
5446 * continue the current actions list. This can be thought of as similar to
5447 * the effect of the "output" action: One copy is sent out (in this case,
5448 * to the connection tracker), but the current copy continues processing.
5450 * It is strongly recommended that this table is later than the current
5451 * table, to prevent loops.
5453 * The "alg" attaches protocol-specific behaviour to this action:
5455 * The ALG is a 16-bit number which specifies that additional
5456 * processing should be applied to this traffic.
5458 * Protocol | Value | Meaning
5459 * --------------------------------------------------------------------
5460 * None | 0 | No protocol-specific behaviour.
5461 * FTP | 21 | Parse FTP control connections and observe the
5462 * | | negotiation of related data connections.
5463 * Other | Other | Unsupported protocols.
5465 * By way of example, if FTP control connections have this action applied
5466 * with the ALG set to FTP (21), then the connection tracker will observe
5467 * the negotiation of data connections. This allows the connection
5468 * tracker to identify subsequent data connections as "related" to this
5469 * existing connection. The "related" flag will be populated in the
5470 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
5473 * Zero or more actions may immediately follow this action. These actions will
5474 * be executed within the context of the connection tracker, and they require
5475 * NX_CT_F_COMMIT flag be set.
5477 struct nx_action_conntrack
{
5478 ovs_be16 type
; /* OFPAT_VENDOR. */
5479 ovs_be16 len
; /* At least 24. */
5480 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5481 ovs_be16 subtype
; /* NXAST_CT. */
5482 ovs_be16 flags
; /* Zero or more NX_CT_F_* flags.
5483 * Unspecified flag bits must be zero. */
5484 ovs_be32 zone_src
; /* Connection tracking context. */
5486 ovs_be16 zone_ofs_nbits
;/* Range to use from source field. */
5487 ovs_be16 zone_imm
; /* Immediate value for zone. */
5489 uint8_t recirc_table
; /* Recirculate to a specific table, or
5490 NX_CT_RECIRC_NONE for no recirculation. */
5491 uint8_t pad
[3]; /* Zeroes */
5492 ovs_be16 alg
; /* Well-known port number for the protocol.
5493 * 0 indicates no ALG is required. */
5494 /* Followed by a sequence of zero or more OpenFlow actions. The length of
5495 * these is included in 'len'. */
5497 OFP_ASSERT(sizeof(struct nx_action_conntrack
) == 24);
5500 decode_ct_zone(const struct nx_action_conntrack
*nac
,
5501 struct ofpact_conntrack
*out
,
5502 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
)
5504 if (nac
->zone_src
) {
5507 out
->zone_src
.ofs
= nxm_decode_ofs(nac
->zone_ofs_nbits
);
5508 out
->zone_src
.n_bits
= nxm_decode_n_bits(nac
->zone_ofs_nbits
);
5509 error
= mf_vl_mff_mf_from_nxm_header(ntohl(nac
->zone_src
),
5510 vl_mff_map
, &out
->zone_src
.field
,
5516 error
= mf_check_src(&out
->zone_src
, NULL
);
5521 if (out
->zone_src
.n_bits
!= 16) {
5522 VLOG_WARN_RL(&rl
, "zone n_bits %d not within valid range [16..16]",
5523 out
->zone_src
.n_bits
);
5524 return OFPERR_OFPBAC_BAD_SET_LEN
;
5527 out
->zone_src
.field
= NULL
;
5528 out
->zone_imm
= ntohs(nac
->zone_imm
);
5535 decode_NXAST_RAW_CT(const struct nx_action_conntrack
*nac
,
5536 enum ofp_version ofp_version
,
5537 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
5540 const size_t ct_offset
= ofpacts_pull(out
);
5541 struct ofpact_conntrack
*conntrack
= ofpact_put_CT(out
);
5544 conntrack
->flags
= ntohs(nac
->flags
);
5545 if (conntrack
->flags
& NX_CT_F_FORCE
&&
5546 !(conntrack
->flags
& NX_CT_F_COMMIT
)) {
5547 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
5551 error
= decode_ct_zone(nac
, conntrack
, vl_mff_map
, tlv_bitmap
);
5555 conntrack
->recirc_table
= nac
->recirc_table
;
5556 conntrack
->alg
= ntohs(nac
->alg
);
5558 ofpbuf_pull(out
, sizeof(*conntrack
));
5560 struct ofpbuf openflow
= ofpbuf_const_initializer(
5561 nac
+ 1, ntohs(nac
->len
) - sizeof(*nac
));
5562 error
= ofpacts_pull_openflow_actions__(&openflow
, openflow
.size
,
5564 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
5565 out
, OFPACT_CT
, vl_mff_map
,
5571 conntrack
= ofpbuf_push_uninit(out
, sizeof(*conntrack
));
5572 out
->header
= &conntrack
->ofpact
;
5573 ofpact_finish_CT(out
, &conntrack
);
5575 if (conntrack
->ofpact
.len
> sizeof(*conntrack
)
5576 && !(conntrack
->flags
& NX_CT_F_COMMIT
)) {
5577 const struct ofpact
*a
;
5578 size_t ofpacts_len
= conntrack
->ofpact
.len
- sizeof(*conntrack
);
5580 OFPACT_FOR_EACH (a
, conntrack
->actions
, ofpacts_len
) {
5581 if (a
->type
!= OFPACT_NAT
|| ofpact_get_NAT(a
)->flags
5582 || ofpact_get_NAT(a
)->range_af
!= AF_UNSPEC
) {
5583 VLOG_WARN_RL(&rl
, "CT action requires commit flag if actions "
5584 "other than NAT without arguments are specified.");
5585 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
5592 ofpbuf_push_uninit(out
, ct_offset
);
5597 encode_CT(const struct ofpact_conntrack
*conntrack
,
5598 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5600 struct nx_action_conntrack
*nac
;
5601 const size_t ofs
= out
->size
;
5604 nac
= put_NXAST_CT(out
);
5605 nac
->flags
= htons(conntrack
->flags
);
5606 if (conntrack
->zone_src
.field
) {
5607 nac
->zone_src
= htonl(nxm_header_from_mff(conntrack
->zone_src
.field
));
5608 nac
->zone_ofs_nbits
= nxm_encode_ofs_nbits(conntrack
->zone_src
.ofs
,
5609 conntrack
->zone_src
.n_bits
);
5611 nac
->zone_src
= htonl(0);
5612 nac
->zone_imm
= htons(conntrack
->zone_imm
);
5614 nac
->recirc_table
= conntrack
->recirc_table
;
5615 nac
->alg
= htons(conntrack
->alg
);
5617 len
= ofpacts_put_openflow_actions(conntrack
->actions
,
5618 ofpact_ct_get_action_len(conntrack
),
5620 len
+= sizeof(*nac
);
5621 nac
= ofpbuf_at(out
, ofs
, sizeof(*nac
));
5622 nac
->len
= htons(len
);
5625 static char * OVS_WARN_UNUSED_RESULT
parse_NAT(char *arg
, struct ofpbuf
*,
5626 enum ofputil_protocol
* OVS_UNUSED
);
5628 /* Parses 'arg' as the argument to a "ct" action, and appends such an
5629 * action to 'ofpacts'.
5631 * Returns NULL if successful, otherwise a malloc()'d string describing the
5632 * error. The caller is responsible for freeing the returned string. */
5633 static char * OVS_WARN_UNUSED_RESULT
5634 parse_CT(char *arg
, struct ofpbuf
*ofpacts
,
5635 enum ofputil_protocol
*usable_protocols
)
5637 const size_t ct_offset
= ofpacts_pull(ofpacts
);
5638 struct ofpact_conntrack
*oc
;
5642 oc
= ofpact_put_CT(ofpacts
);
5644 oc
->recirc_table
= NX_CT_RECIRC_NONE
;
5645 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
5646 if (!strcmp(key
, "commit")) {
5647 oc
->flags
|= NX_CT_F_COMMIT
;
5648 } else if (!strcmp(key
, "force")) {
5649 oc
->flags
|= NX_CT_F_FORCE
;
5650 } else if (!strcmp(key
, "table")) {
5651 error
= str_to_u8(value
, "recirc_table", &oc
->recirc_table
);
5652 if (!error
&& oc
->recirc_table
== NX_CT_RECIRC_NONE
) {
5653 error
= xasprintf("invalid table %#"PRIx8
, oc
->recirc_table
);
5655 } else if (!strcmp(key
, "zone")) {
5656 error
= str_to_u16(value
, "zone", &oc
->zone_imm
);
5660 error
= mf_parse_subfield(&oc
->zone_src
, value
);
5665 } else if (!strcmp(key
, "alg")) {
5666 error
= str_to_connhelper(value
, &oc
->alg
);
5667 } else if (!strcmp(key
, "nat")) {
5668 const size_t nat_offset
= ofpacts_pull(ofpacts
);
5670 error
= parse_NAT(value
, ofpacts
, usable_protocols
);
5671 /* Update CT action pointer and length. */
5672 ofpacts
->header
= ofpbuf_push_uninit(ofpacts
, nat_offset
);
5673 oc
= ofpacts
->header
;
5674 } else if (!strcmp(key
, "exec")) {
5675 /* Hide existing actions from ofpacts_parse_copy(), so the
5676 * nesting can be handled transparently. */
5677 enum ofputil_protocol usable_protocols2
;
5678 const size_t exec_offset
= ofpacts_pull(ofpacts
);
5680 /* Initializes 'usable_protocol2', fold it back to
5681 * '*usable_protocols' afterwards, so that we do not lose
5682 * restrictions already in there. */
5683 error
= ofpacts_parse_copy(value
, ofpacts
, &usable_protocols2
,
5685 *usable_protocols
&= usable_protocols2
;
5686 ofpacts
->header
= ofpbuf_push_uninit(ofpacts
, exec_offset
);
5687 oc
= ofpacts
->header
;
5689 error
= xasprintf("invalid argument to \"ct\" action: `%s'", key
);
5695 if (oc
->flags
& NX_CT_F_FORCE
&& !(oc
->flags
& NX_CT_F_COMMIT
)) {
5696 error
= xasprintf("\"force\" flag requires \"commit\" flag.");
5698 ofpact_finish_CT(ofpacts
, &oc
);
5699 ofpbuf_push_uninit(ofpacts
, ct_offset
);
5704 format_alg(int port
, struct ds
*s
)
5708 ds_put_format(s
, "%salg=%sftp,", colors
.param
, colors
.end
);
5711 ds_put_format(s
, "%salg=%stftp,", colors
.param
, colors
.end
);
5717 ds_put_format(s
, "%salg=%s%d,", colors
.param
, colors
.end
, port
);
5722 static void format_NAT(const struct ofpact_nat
*a
, struct ds
*ds
);
5725 format_CT(const struct ofpact_conntrack
*a
, struct ds
*s
)
5727 ds_put_format(s
, "%sct(%s", colors
.paren
, colors
.end
);
5728 if (a
->flags
& NX_CT_F_COMMIT
) {
5729 ds_put_format(s
, "%scommit%s,", colors
.value
, colors
.end
);
5731 if (a
->flags
& NX_CT_F_FORCE
) {
5732 ds_put_format(s
, "%sforce%s,", colors
.value
, colors
.end
);
5734 if (a
->recirc_table
!= NX_CT_RECIRC_NONE
) {
5735 ds_put_format(s
, "%stable=%s%"PRIu8
",",
5736 colors
.special
, colors
.end
, a
->recirc_table
);
5738 if (a
->zone_src
.field
) {
5739 ds_put_format(s
, "%szone=%s", colors
.param
, colors
.end
);
5740 mf_format_subfield(&a
->zone_src
, s
);
5741 ds_put_char(s
, ',');
5742 } else if (a
->zone_imm
) {
5743 ds_put_format(s
, "%szone=%s%"PRIu16
",",
5744 colors
.param
, colors
.end
, a
->zone_imm
);
5746 /* If the first action is a NAT action, format it outside of the 'exec'
5748 const struct ofpact
*action
= a
->actions
;
5749 size_t actions_len
= ofpact_ct_get_action_len(a
);
5750 if (actions_len
&& action
->type
== OFPACT_NAT
) {
5751 format_NAT(ofpact_get_NAT(action
), s
);
5752 ds_put_char(s
, ',');
5753 actions_len
-= OFPACT_ALIGN(action
->len
);
5754 action
= ofpact_next(action
);
5757 ds_put_format(s
, "%sexec(%s", colors
.paren
, colors
.end
);
5758 ofpacts_format(action
, actions_len
, s
);
5759 ds_put_format(s
, "%s),%s", colors
.paren
, colors
.end
);
5761 format_alg(a
->alg
, s
);
5763 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
5766 /* ct_clear action. */
5769 decode_NXAST_RAW_CT_CLEAR(struct ofpbuf
*out
)
5771 ofpact_put_CT_CLEAR(out
);
5776 encode_CT_CLEAR(const struct ofpact_null
*null OVS_UNUSED
,
5777 enum ofp_version ofp_version OVS_UNUSED
,
5780 put_NXAST_CT_CLEAR(out
);
5783 static char * OVS_WARN_UNUSED_RESULT
5784 parse_CT_CLEAR(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
5785 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
5787 ofpact_put_CT_CLEAR(ofpacts
);
5792 format_CT_CLEAR(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
5794 ds_put_format(s
, "%sct_clear%s", colors
.value
, colors
.end
);
5798 /* Which optional fields are present? */
5800 NX_NAT_RANGE_IPV4_MIN
= 1 << 0, /* ovs_be32 */
5801 NX_NAT_RANGE_IPV4_MAX
= 1 << 1, /* ovs_be32 */
5802 NX_NAT_RANGE_IPV6_MIN
= 1 << 2, /* struct in6_addr */
5803 NX_NAT_RANGE_IPV6_MAX
= 1 << 3, /* struct in6_addr */
5804 NX_NAT_RANGE_PROTO_MIN
= 1 << 4, /* ovs_be16 */
5805 NX_NAT_RANGE_PROTO_MAX
= 1 << 5, /* ovs_be16 */
5808 /* Action structure for NXAST_NAT. */
5809 struct nx_action_nat
{
5810 ovs_be16 type
; /* OFPAT_VENDOR. */
5811 ovs_be16 len
; /* At least 16. */
5812 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5813 ovs_be16 subtype
; /* NXAST_NAT. */
5814 uint8_t pad
[2]; /* Must be zero. */
5815 ovs_be16 flags
; /* Zero or more NX_NAT_F_* flags.
5816 * Unspecified flag bits must be zero. */
5817 ovs_be16 range_present
; /* NX_NAT_RANGE_* */
5818 /* Followed by optional parameters as specified by 'range_present' */
5820 OFP_ASSERT(sizeof(struct nx_action_nat
) == 16);
5823 encode_NAT(const struct ofpact_nat
*nat
,
5824 enum ofp_version ofp_version OVS_UNUSED
,
5827 struct nx_action_nat
*nan
;
5828 const size_t ofs
= out
->size
;
5829 uint16_t range_present
= 0;
5831 nan
= put_NXAST_NAT(out
);
5832 nan
->flags
= htons(nat
->flags
);
5833 if (nat
->range_af
== AF_INET
) {
5834 if (nat
->range
.addr
.ipv4
.min
) {
5835 ovs_be32
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
5836 *min
= nat
->range
.addr
.ipv4
.min
;
5837 range_present
|= NX_NAT_RANGE_IPV4_MIN
;
5839 if (nat
->range
.addr
.ipv4
.max
) {
5840 ovs_be32
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
5841 *max
= nat
->range
.addr
.ipv4
.max
;
5842 range_present
|= NX_NAT_RANGE_IPV4_MAX
;
5844 } else if (nat
->range_af
== AF_INET6
) {
5845 if (!ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.min
)) {
5846 struct in6_addr
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
5847 *min
= nat
->range
.addr
.ipv6
.min
;
5848 range_present
|= NX_NAT_RANGE_IPV6_MIN
;
5850 if (!ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.max
)) {
5851 struct in6_addr
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
5852 *max
= nat
->range
.addr
.ipv6
.max
;
5853 range_present
|= NX_NAT_RANGE_IPV6_MAX
;
5856 if (nat
->range_af
!= AF_UNSPEC
) {
5857 if (nat
->range
.proto
.min
) {
5858 ovs_be16
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
5859 *min
= htons(nat
->range
.proto
.min
);
5860 range_present
|= NX_NAT_RANGE_PROTO_MIN
;
5862 if (nat
->range
.proto
.max
) {
5863 ovs_be16
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
5864 *max
= htons(nat
->range
.proto
.max
);
5865 range_present
|= NX_NAT_RANGE_PROTO_MAX
;
5868 pad_ofpat(out
, ofs
);
5869 nan
= ofpbuf_at(out
, ofs
, sizeof *nan
);
5870 nan
->range_present
= htons(range_present
);
5874 decode_NXAST_RAW_NAT(const struct nx_action_nat
*nan
,
5875 enum ofp_version ofp_version OVS_UNUSED
,
5878 struct ofpact_nat
*nat
;
5879 uint16_t range_present
= ntohs(nan
->range_present
);
5880 const char *opts
= (char *)(nan
+ 1);
5881 uint16_t len
= ntohs(nan
->len
) - sizeof *nan
;
5883 nat
= ofpact_put_NAT(out
);
5884 nat
->flags
= ntohs(nan
->flags
);
5886 /* Check for unknown or mutually exclusive flags. */
5887 if ((nat
->flags
& ~NX_NAT_F_MASK
)
5888 || (nat
->flags
& NX_NAT_F_SRC
&& nat
->flags
& NX_NAT_F_DST
)
5889 || (nat
->flags
& NX_NAT_F_PROTO_HASH
5890 && nat
->flags
& NX_NAT_F_PROTO_RANDOM
)) {
5891 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5894 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
5895 (LEN >= sizeof(TYPE) \
5896 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
5897 SRC += sizeof(TYPE)) \
5900 nat
->range_af
= AF_UNSPEC
;
5901 if (range_present
& NX_NAT_RANGE_IPV4_MIN
) {
5902 if (range_present
& (NX_NAT_RANGE_IPV6_MIN
| NX_NAT_RANGE_IPV6_MAX
)) {
5903 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5906 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv4
.min
, opts
, len
, ovs_be32
)
5907 || !nat
->range
.addr
.ipv4
.min
) {
5908 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5911 nat
->range_af
= AF_INET
;
5913 if (range_present
& NX_NAT_RANGE_IPV4_MAX
) {
5914 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv4
.max
, opts
, len
,
5916 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5918 if (ntohl(nat
->range
.addr
.ipv4
.max
)
5919 < ntohl(nat
->range
.addr
.ipv4
.min
)) {
5920 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5923 } else if (range_present
& NX_NAT_RANGE_IPV4_MAX
) {
5924 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5925 } else if (range_present
& NX_NAT_RANGE_IPV6_MIN
) {
5926 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv6
.min
, opts
, len
,
5928 || ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.min
)) {
5929 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5932 nat
->range_af
= AF_INET6
;
5934 if (range_present
& NX_NAT_RANGE_IPV6_MAX
) {
5935 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv6
.max
, opts
, len
,
5937 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5939 if (memcmp(&nat
->range
.addr
.ipv6
.max
, &nat
->range
.addr
.ipv6
.min
,
5940 sizeof(struct in6_addr
)) < 0) {
5941 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5944 } else if (range_present
& NX_NAT_RANGE_IPV6_MAX
) {
5945 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5948 if (range_present
& NX_NAT_RANGE_PROTO_MIN
) {
5951 if (nat
->range_af
== AF_UNSPEC
) {
5952 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5954 if (!NX_NAT_GET_OPT(&proto
, opts
, len
, ovs_be16
) || proto
== 0) {
5955 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5957 nat
->range
.proto
.min
= ntohs(proto
);
5958 if (range_present
& NX_NAT_RANGE_PROTO_MAX
) {
5959 if (!NX_NAT_GET_OPT(&proto
, opts
, len
, ovs_be16
)) {
5960 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5962 nat
->range
.proto
.max
= ntohs(proto
);
5963 if (nat
->range
.proto
.max
< nat
->range
.proto
.min
) {
5964 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5967 } else if (range_present
& NX_NAT_RANGE_PROTO_MAX
) {
5968 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5975 format_NAT(const struct ofpact_nat
*a
, struct ds
*ds
)
5977 ds_put_format(ds
, "%snat%s", colors
.paren
, colors
.end
);
5979 if (a
->flags
& (NX_NAT_F_SRC
| NX_NAT_F_DST
)) {
5980 ds_put_format(ds
, "%s(%s", colors
.paren
, colors
.end
);
5981 ds_put_format(ds
, a
->flags
& NX_NAT_F_SRC
? "%ssrc%s" : "%sdst%s",
5982 colors
.param
, colors
.end
);
5984 if (a
->range_af
!= AF_UNSPEC
) {
5985 ds_put_format(ds
, "%s=%s", colors
.param
, colors
.end
);
5987 if (a
->range_af
== AF_INET
) {
5988 ds_put_format(ds
, IP_FMT
, IP_ARGS(a
->range
.addr
.ipv4
.min
));
5990 if (a
->range
.addr
.ipv4
.max
5991 && a
->range
.addr
.ipv4
.max
!= a
->range
.addr
.ipv4
.min
) {
5992 ds_put_format(ds
, "-"IP_FMT
,
5993 IP_ARGS(a
->range
.addr
.ipv4
.max
));
5995 } else if (a
->range_af
== AF_INET6
) {
5996 ipv6_format_addr_bracket(&a
->range
.addr
.ipv6
.min
, ds
,
5997 a
->range
.proto
.min
);
5999 if (!ipv6_mask_is_any(&a
->range
.addr
.ipv6
.max
)
6000 && memcmp(&a
->range
.addr
.ipv6
.max
, &a
->range
.addr
.ipv6
.min
,
6001 sizeof(struct in6_addr
)) != 0) {
6002 ds_put_char(ds
, '-');
6003 ipv6_format_addr_bracket(&a
->range
.addr
.ipv6
.max
, ds
,
6004 a
->range
.proto
.min
);
6007 if (a
->range
.proto
.min
) {
6008 ds_put_char(ds
, ':');
6009 ds_put_format(ds
, "%"PRIu16
, a
->range
.proto
.min
);
6011 if (a
->range
.proto
.max
6012 && a
->range
.proto
.max
!= a
->range
.proto
.min
) {
6013 ds_put_format(ds
, "-%"PRIu16
, a
->range
.proto
.max
);
6016 ds_put_char(ds
, ',');
6018 if (a
->flags
& NX_NAT_F_PERSISTENT
) {
6019 ds_put_format(ds
, "%spersistent%s,",
6020 colors
.value
, colors
.end
);
6022 if (a
->flags
& NX_NAT_F_PROTO_HASH
) {
6023 ds_put_format(ds
, "%shash%s,", colors
.value
, colors
.end
);
6025 if (a
->flags
& NX_NAT_F_PROTO_RANDOM
) {
6026 ds_put_format(ds
, "%srandom%s,", colors
.value
, colors
.end
);
6030 ds_put_format(ds
, "%s)%s", colors
.paren
, colors
.end
);
6034 static char * OVS_WARN_UNUSED_RESULT
6035 str_to_nat_range(const char *s
, struct ofpact_nat
*on
)
6037 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
6040 on
->range_af
= AF_UNSPEC
;
6041 if (ovs_scan_len(s
, &n
, IP_SCAN_FMT
,
6042 IP_SCAN_ARGS(&on
->range
.addr
.ipv4
.min
))) {
6043 on
->range_af
= AF_INET
;
6047 if (!ovs_scan_len(s
, &n
, IP_SCAN_FMT
,
6048 IP_SCAN_ARGS(&on
->range
.addr
.ipv4
.max
))
6049 || (ntohl(on
->range
.addr
.ipv4
.max
)
6050 < ntohl(on
->range
.addr
.ipv4
.min
))) {
6054 } else if ((ovs_scan_len(s
, &n
, IPV6_SCAN_FMT
, ipv6_s
)
6055 || ovs_scan_len(s
, &n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
6056 && inet_pton(AF_INET6
, ipv6_s
, &on
->range
.addr
.ipv6
.min
) == 1) {
6057 on
->range_af
= AF_INET6
;
6061 if (!(ovs_scan_len(s
, &n
, IPV6_SCAN_FMT
, ipv6_s
)
6062 || ovs_scan_len(s
, &n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
6063 || inet_pton(AF_INET6
, ipv6_s
, &on
->range
.addr
.ipv6
.max
) != 1
6064 || memcmp(&on
->range
.addr
.ipv6
.max
, &on
->range
.addr
.ipv6
.min
,
6065 sizeof on
->range
.addr
.ipv6
.max
) < 0) {
6070 if (on
->range_af
!= AF_UNSPEC
&& s
[n
] == ':') {
6072 if (!ovs_scan_len(s
, &n
, "%"SCNu16
, &on
->range
.proto
.min
)) {
6077 if (!ovs_scan_len(s
, &n
, "%"SCNu16
, &on
->range
.proto
.max
)
6078 || on
->range
.proto
.max
< on
->range
.proto
.min
) {
6083 if (strlen(s
) != n
) {
6084 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
6089 return xasprintf("invalid nat range \"%s\"", s
);
6093 /* Parses 'arg' as the argument to a "nat" action, and appends such an
6094 * action to 'ofpacts'.
6096 * Returns NULL if successful, otherwise a malloc()'d string describing the
6097 * error. The caller is responsible for freeing the returned string. */
6098 static char * OVS_WARN_UNUSED_RESULT
6099 parse_NAT(char *arg
, struct ofpbuf
*ofpacts
,
6100 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
6102 struct ofpact_nat
*on
= ofpact_put_NAT(ofpacts
);
6106 on
->range_af
= AF_UNSPEC
;
6108 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
6111 if (!strcmp(key
, "src")) {
6112 on
->flags
|= NX_NAT_F_SRC
;
6113 error
= str_to_nat_range(value
, on
);
6114 } else if (!strcmp(key
, "dst")) {
6115 on
->flags
|= NX_NAT_F_DST
;
6116 error
= str_to_nat_range(value
, on
);
6117 } else if (!strcmp(key
, "persistent")) {
6118 on
->flags
|= NX_NAT_F_PERSISTENT
;
6119 } else if (!strcmp(key
, "hash")) {
6120 on
->flags
|= NX_NAT_F_PROTO_HASH
;
6121 } else if (!strcmp(key
, "random")) {
6122 on
->flags
|= NX_NAT_F_PROTO_RANDOM
;
6124 error
= xasprintf("invalid key \"%s\" in \"nat\" argument",
6131 if (on
->flags
& NX_NAT_F_SRC
&& on
->flags
& NX_NAT_F_DST
) {
6132 return xasprintf("May only specify one of \"src\" or \"dst\".");
6134 if (!(on
->flags
& NX_NAT_F_SRC
|| on
->flags
& NX_NAT_F_DST
)) {
6136 return xasprintf("Flags allowed only with \"src\" or \"dst\".");
6138 if (on
->range_af
!= AF_UNSPEC
) {
6139 return xasprintf("Range allowed only with \"src\" or \"dst\".");
6142 if (on
->flags
& NX_NAT_F_PROTO_HASH
&& on
->flags
& NX_NAT_F_PROTO_RANDOM
) {
6143 return xasprintf("Both \"hash\" and \"random\" are not allowed.");
6149 /* Truncate output action. */
6150 struct nx_action_output_trunc
{
6151 ovs_be16 type
; /* OFPAT_VENDOR. */
6152 ovs_be16 len
; /* At least 16. */
6153 ovs_be32 vendor
; /* NX_VENDOR_ID. */
6154 ovs_be16 subtype
; /* NXAST_OUTPUT_TRUNC. */
6155 ovs_be16 port
; /* Output port */
6156 ovs_be32 max_len
; /* Truncate packet to size bytes */
6158 OFP_ASSERT(sizeof(struct nx_action_output_trunc
) == 16);
6161 decode_NXAST_RAW_OUTPUT_TRUNC(const struct nx_action_output_trunc
*natrc
,
6162 enum ofp_version ofp_version OVS_UNUSED
,
6165 struct ofpact_output_trunc
*output_trunc
;
6167 output_trunc
= ofpact_put_OUTPUT_TRUNC(out
);
6168 output_trunc
->max_len
= ntohl(natrc
->max_len
);
6169 output_trunc
->port
= u16_to_ofp(ntohs(natrc
->port
));
6171 if (output_trunc
->max_len
< ETH_HEADER_LEN
) {
6172 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6178 encode_OUTPUT_TRUNC(const struct ofpact_output_trunc
*output_trunc
,
6179 enum ofp_version ofp_version OVS_UNUSED
,
6182 struct nx_action_output_trunc
*natrc
= put_NXAST_OUTPUT_TRUNC(out
);
6184 natrc
->max_len
= htonl(output_trunc
->max_len
);
6185 natrc
->port
= htons(ofp_to_u16(output_trunc
->port
));
6188 static char * OVS_WARN_UNUSED_RESULT
6189 parse_OUTPUT_TRUNC(const char *arg
, struct ofpbuf
*ofpacts OVS_UNUSED
,
6190 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
6192 /* Disable output_trunc parsing. Expose as output(port=N,max_len=M) and
6193 * reuse parse_OUTPUT to parse output_trunc action. */
6194 return xasprintf("unknown action %s", arg
);
6198 format_OUTPUT_TRUNC(const struct ofpact_output_trunc
*a
, struct ds
*s
)
6200 ds_put_format(s
, "%soutput%s(port=%"PRIu32
",max_len=%"PRIu32
")",
6201 colors
.special
, colors
.end
, a
->port
, a
->max_len
);
6205 /* Meter instruction. */
6208 encode_METER(const struct ofpact_meter
*meter
,
6209 enum ofp_version ofp_version
, struct ofpbuf
*out
)
6211 if (ofp_version
>= OFP13_VERSION
) {
6212 instruction_put_OFPIT13_METER(out
)->meter_id
= htonl(meter
->meter_id
);
6216 static char * OVS_WARN_UNUSED_RESULT
6217 parse_METER(char *arg
, struct ofpbuf
*ofpacts
,
6218 enum ofputil_protocol
*usable_protocols
)
6220 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
6221 return str_to_u32(arg
, &ofpact_put_METER(ofpacts
)->meter_id
);
6225 format_METER(const struct ofpact_meter
*a
, struct ds
*s
)
6227 ds_put_format(s
, "%smeter:%s%"PRIu32
,
6228 colors
.param
, colors
.end
, a
->meter_id
);
6231 /* Clear-Actions instruction. */
6234 encode_CLEAR_ACTIONS(const struct ofpact_null
*null OVS_UNUSED
,
6235 enum ofp_version ofp_version OVS_UNUSED
,
6236 struct ofpbuf
*out OVS_UNUSED
)
6238 if (ofp_version
> OFP10_VERSION
) {
6239 instruction_put_OFPIT11_CLEAR_ACTIONS(out
);
6243 static char * OVS_WARN_UNUSED_RESULT
6244 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
6245 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
6247 ofpact_put_CLEAR_ACTIONS(ofpacts
);
6252 format_CLEAR_ACTIONS(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
6254 ds_put_format(s
, "%sclear_actions%s", colors
.value
, colors
.end
);
6257 /* Write-Actions instruction. */
6260 encode_WRITE_ACTIONS(const struct ofpact_nest
*actions
,
6261 enum ofp_version ofp_version
, struct ofpbuf
*out
)
6263 if (ofp_version
> OFP10_VERSION
) {
6264 const size_t ofs
= out
->size
;
6266 instruction_put_OFPIT11_WRITE_ACTIONS(out
);
6267 ofpacts_put_openflow_actions(actions
->actions
,
6268 ofpact_nest_get_action_len(actions
),
6270 ofpacts_update_instruction_actions(out
, ofs
);
6274 static char * OVS_WARN_UNUSED_RESULT
6275 parse_WRITE_ACTIONS(char *arg
, struct ofpbuf
*ofpacts
,
6276 enum ofputil_protocol
*usable_protocols
)
6278 size_t ofs
= ofpacts_pull(ofpacts
);
6279 struct ofpact_nest
*on
;
6282 /* Add a Write-Actions instruction and then pull it off. */
6283 ofpact_put(ofpacts
, OFPACT_WRITE_ACTIONS
, sizeof *on
);
6284 ofpbuf_pull(ofpacts
, sizeof *on
);
6286 /* Parse nested actions.
6288 * We pulled off "write-actions" and the previous actions because the
6289 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
6290 * that it doesn't actually include the nested actions. That means that
6291 * ofpacts_parse() would reject them as being part of an Apply-Actions that
6292 * follows a Write-Actions, which is an invalid order. */
6293 error
= ofpacts_parse(arg
, ofpacts
, usable_protocols
, false,
6294 OFPACT_WRITE_ACTIONS
);
6296 /* Put the Write-Actions back on and update its length. */
6297 on
= ofpbuf_push_uninit(ofpacts
, sizeof *on
);
6298 on
->ofpact
.len
= ofpacts
->size
;
6300 /* Put any previous actions or instructions back on. */
6301 ofpbuf_push_uninit(ofpacts
, ofs
);
6307 format_WRITE_ACTIONS(const struct ofpact_nest
*a
, struct ds
*s
)
6309 ds_put_format(s
, "%swrite_actions(%s", colors
.paren
, colors
.end
);
6310 ofpacts_format(a
->actions
, ofpact_nest_get_action_len(a
), s
);
6311 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
6314 /* Action structure for NXAST_WRITE_METADATA.
6316 * Modifies the 'mask' bits of the metadata value. */
6317 struct nx_action_write_metadata
{
6318 ovs_be16 type
; /* OFPAT_VENDOR. */
6319 ovs_be16 len
; /* Length is 32. */
6320 ovs_be32 vendor
; /* NX_VENDOR_ID. */
6321 ovs_be16 subtype
; /* NXAST_WRITE_METADATA. */
6322 uint8_t zeros
[6]; /* Must be zero. */
6323 ovs_be64 metadata
; /* Metadata register. */
6324 ovs_be64 mask
; /* Metadata mask. */
6326 OFP_ASSERT(sizeof(struct nx_action_write_metadata
) == 32);
6329 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata
*nawm
,
6330 enum ofp_version ofp_version OVS_UNUSED
,
6333 struct ofpact_metadata
*om
;
6335 if (!is_all_zeros(nawm
->zeros
, sizeof nawm
->zeros
)) {
6336 return OFPERR_NXBRC_MUST_BE_ZERO
;
6339 om
= ofpact_put_WRITE_METADATA(out
);
6340 om
->metadata
= nawm
->metadata
;
6341 om
->mask
= nawm
->mask
;
6347 encode_WRITE_METADATA(const struct ofpact_metadata
*metadata
,
6348 enum ofp_version ofp_version
, struct ofpbuf
*out
)
6350 if (ofp_version
== OFP10_VERSION
) {
6351 struct nx_action_write_metadata
*nawm
;
6353 nawm
= put_NXAST_WRITE_METADATA(out
);
6354 nawm
->metadata
= metadata
->metadata
;
6355 nawm
->mask
= metadata
->mask
;
6357 struct ofp11_instruction_write_metadata
*oiwm
;
6359 oiwm
= instruction_put_OFPIT11_WRITE_METADATA(out
);
6360 oiwm
->metadata
= metadata
->metadata
;
6361 oiwm
->metadata_mask
= metadata
->mask
;
6365 static char * OVS_WARN_UNUSED_RESULT
6366 parse_WRITE_METADATA(char *arg
, struct ofpbuf
*ofpacts
,
6367 enum ofputil_protocol
*usable_protocols
)
6369 struct ofpact_metadata
*om
;
6370 char *mask
= strchr(arg
, '/');
6372 *usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
6374 om
= ofpact_put_WRITE_METADATA(ofpacts
);
6379 error
= str_to_be64(mask
+ 1, &om
->mask
);
6384 om
->mask
= OVS_BE64_MAX
;
6387 return str_to_be64(arg
, &om
->metadata
);
6391 format_WRITE_METADATA(const struct ofpact_metadata
*a
, struct ds
*s
)
6393 ds_put_format(s
, "%swrite_metadata:%s%#"PRIx64
,
6394 colors
.param
, colors
.end
, ntohll(a
->metadata
));
6395 if (a
->mask
!= OVS_BE64_MAX
) {
6396 ds_put_format(s
, "/%#"PRIx64
, ntohll(a
->mask
));
6400 /* Goto-Table instruction. */
6403 encode_GOTO_TABLE(const struct ofpact_goto_table
*goto_table
,
6404 enum ofp_version ofp_version
, struct ofpbuf
*out
)
6406 if (ofp_version
== OFP10_VERSION
) {
6407 struct nx_action_resubmit
*nar
;
6409 nar
= put_NXAST_RESUBMIT_TABLE(out
);
6410 nar
->table
= goto_table
->table_id
;
6411 nar
->in_port
= htons(ofp_to_u16(OFPP_IN_PORT
));
6413 struct ofp11_instruction_goto_table
*oigt
;
6415 oigt
= instruction_put_OFPIT11_GOTO_TABLE(out
);
6416 oigt
->table_id
= goto_table
->table_id
;
6417 memset(oigt
->pad
, 0, sizeof oigt
->pad
);
6421 static char * OVS_WARN_UNUSED_RESULT
6422 parse_GOTO_TABLE(char *arg
, struct ofpbuf
*ofpacts
,
6423 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
6425 struct ofpact_goto_table
*ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
6426 char *table_s
= strsep(&arg
, ",");
6427 if (!table_s
|| !table_s
[0]) {
6428 return xstrdup("instruction goto-table needs table id");
6430 return str_to_u8(table_s
, "table", &ogt
->table_id
);
6434 format_GOTO_TABLE(const struct ofpact_goto_table
*a
, struct ds
*s
)
6436 ds_put_format(s
, "%sgoto_table:%s%"PRIu8
,
6437 colors
.param
, colors
.end
, a
->table_id
);
6441 log_bad_action(const struct ofp_action_header
*actions
, size_t actions_len
,
6442 const struct ofp_action_header
*bad_action
, enum ofperr error
)
6444 if (!VLOG_DROP_WARN(&rl
)) {
6448 ds_put_hex_dump(&s
, actions
, actions_len
, 0, false);
6449 VLOG_WARN("bad action at offset %#"PRIxPTR
" (%s):\n%s",
6450 (char *)bad_action
- (char *)actions
,
6451 ofperr_get_name(error
), ds_cstr(&s
));
6457 ofpacts_decode(const void *actions
, size_t actions_len
,
6458 enum ofp_version ofp_version
,
6459 const struct vl_mff_map
*vl_mff_map
,
6460 uint64_t *ofpacts_tlv_bitmap
, struct ofpbuf
*ofpacts
)
6462 struct ofpbuf openflow
= ofpbuf_const_initializer(actions
, actions_len
);
6463 while (openflow
.size
) {
6464 const struct ofp_action_header
*action
= openflow
.data
;
6465 enum ofp_raw_action_type raw
;
6469 error
= ofpact_pull_raw(&openflow
, ofp_version
, &raw
, &arg
);
6471 error
= ofpact_decode(action
, raw
, ofp_version
, arg
, vl_mff_map
,
6472 ofpacts_tlv_bitmap
, ofpacts
);
6476 log_bad_action(actions
, actions_len
, action
, error
);
6484 ofpacts_pull_openflow_actions__(struct ofpbuf
*openflow
,
6485 unsigned int actions_len
,
6486 enum ofp_version version
,
6487 uint32_t allowed_ovsinsts
,
6488 struct ofpbuf
*ofpacts
,
6489 enum ofpact_type outer_action
,
6490 const struct vl_mff_map
*vl_mff_map
,
6491 uint64_t *ofpacts_tlv_bitmap
)
6493 const struct ofp_action_header
*actions
;
6494 size_t orig_size
= ofpacts
->size
;
6497 if (actions_len
% OFP_ACTION_ALIGN
!= 0) {
6498 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u is not a "
6499 "multiple of %d", actions_len
, OFP_ACTION_ALIGN
);
6500 return OFPERR_OFPBRC_BAD_LEN
;
6503 actions
= ofpbuf_try_pull(openflow
, actions_len
);
6504 if (actions
== NULL
) {
6505 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u exceeds "
6506 "remaining message length (%"PRIu32
")",
6507 actions_len
, openflow
->size
);
6508 return OFPERR_OFPBRC_BAD_LEN
;
6511 error
= ofpacts_decode(actions
, actions_len
, version
, vl_mff_map
,
6512 ofpacts_tlv_bitmap
, ofpacts
);
6514 ofpacts
->size
= orig_size
;
6518 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
, allowed_ovsinsts
,
6521 ofpacts
->size
= orig_size
;
6526 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
6527 * of 'openflow' into ofpacts. On success, appends the converted actions to
6528 * 'ofpacts'; on failure, 'ofpacts' is unchanged (but might be reallocated) .
6529 * Returns 0 if successful, otherwise an OpenFlow error.
6531 * Actions are processed according to their OpenFlow version which
6532 * is provided in the 'version' parameter.
6534 * In most places in OpenFlow, actions appear encapsulated in instructions, so
6535 * you should call ofpacts_pull_openflow_instructions() instead of this
6538 * 'vl_mff_map' and 'ofpacts_tlv_bitmap' are optional. If 'vl_mff_map' is
6539 * provided, it is used to get variable length mf_fields with configured
6540 * length in the actions. If an action uses a variable length mf_field,
6541 * 'ofpacts_tlv_bitmap' is updated accordingly for ref counting. If
6542 * 'vl_mff_map' is not provided, the default mf_fields with maximum length
6545 * The parsed actions are valid generically, but they may not be valid in a
6546 * specific context. For example, port numbers up to OFPP_MAX are valid
6547 * generically, but specific datapaths may only support port numbers in a
6548 * smaller range. Use ofpacts_check() to additional check whether actions are
6549 * valid in a specific context. */
6551 ofpacts_pull_openflow_actions(struct ofpbuf
*openflow
,
6552 unsigned int actions_len
,
6553 enum ofp_version version
,
6554 const struct vl_mff_map
*vl_mff_map
,
6555 uint64_t *ofpacts_tlv_bitmap
,
6556 struct ofpbuf
*ofpacts
)
6558 return ofpacts_pull_openflow_actions__(openflow
, actions_len
, version
,
6559 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
6560 ofpacts
, 0, vl_mff_map
,
6561 ofpacts_tlv_bitmap
);
6564 /* OpenFlow 1.1 actions. */
6567 /* True if an action sets the value of a field
6568 * in a way that is compatibile with the action set.
6569 * The field can be set via either a set or a move action.
6570 * False otherwise. */
6572 ofpact_is_set_or_move_action(const struct ofpact
*a
)
6575 case OFPACT_SET_FIELD
:
6576 case OFPACT_REG_MOVE
:
6577 case OFPACT_SET_ETH_DST
:
6578 case OFPACT_SET_ETH_SRC
:
6579 case OFPACT_SET_IP_DSCP
:
6580 case OFPACT_SET_IP_ECN
:
6581 case OFPACT_SET_IP_TTL
:
6582 case OFPACT_SET_IPV4_DST
:
6583 case OFPACT_SET_IPV4_SRC
:
6584 case OFPACT_SET_L4_DST_PORT
:
6585 case OFPACT_SET_L4_SRC_PORT
:
6586 case OFPACT_SET_MPLS_LABEL
:
6587 case OFPACT_SET_MPLS_TC
:
6588 case OFPACT_SET_MPLS_TTL
:
6589 case OFPACT_SET_QUEUE
:
6590 case OFPACT_SET_TUNNEL
:
6591 case OFPACT_SET_VLAN_PCP
:
6592 case OFPACT_SET_VLAN_VID
:
6595 case OFPACT_CLEAR_ACTIONS
:
6597 case OFPACT_CT_CLEAR
:
6600 case OFPACT_CONTROLLER
:
6601 case OFPACT_DEC_MPLS_TTL
:
6602 case OFPACT_DEC_TTL
:
6603 case OFPACT_ENQUEUE
:
6605 case OFPACT_UNROLL_XLATE
:
6606 case OFPACT_FIN_TIMEOUT
:
6607 case OFPACT_GOTO_TABLE
:
6610 case OFPACT_CONJUNCTION
:
6612 case OFPACT_MULTIPATH
:
6615 case OFPACT_OUTPUT_REG
:
6616 case OFPACT_OUTPUT_TRUNC
:
6617 case OFPACT_POP_MPLS
:
6618 case OFPACT_POP_QUEUE
:
6619 case OFPACT_PUSH_MPLS
:
6620 case OFPACT_PUSH_VLAN
:
6621 case OFPACT_RESUBMIT
:
6623 case OFPACT_STACK_POP
:
6624 case OFPACT_STACK_PUSH
:
6625 case OFPACT_STRIP_VLAN
:
6626 case OFPACT_WRITE_ACTIONS
:
6627 case OFPACT_WRITE_METADATA
:
6628 case OFPACT_DEBUG_RECIRC
:
6635 /* True if an action is allowed in the action set.
6636 * False otherwise. */
6638 ofpact_is_allowed_in_actions_set(const struct ofpact
*a
)
6641 case OFPACT_DEC_MPLS_TTL
:
6642 case OFPACT_DEC_TTL
:
6645 case OFPACT_OUTPUT_TRUNC
:
6646 case OFPACT_POP_MPLS
:
6647 case OFPACT_PUSH_MPLS
:
6648 case OFPACT_PUSH_VLAN
:
6649 case OFPACT_REG_MOVE
:
6650 case OFPACT_SET_FIELD
:
6651 case OFPACT_SET_ETH_DST
:
6652 case OFPACT_SET_ETH_SRC
:
6653 case OFPACT_SET_IP_DSCP
:
6654 case OFPACT_SET_IP_ECN
:
6655 case OFPACT_SET_IP_TTL
:
6656 case OFPACT_SET_IPV4_DST
:
6657 case OFPACT_SET_IPV4_SRC
:
6658 case OFPACT_SET_L4_DST_PORT
:
6659 case OFPACT_SET_L4_SRC_PORT
:
6660 case OFPACT_SET_MPLS_LABEL
:
6661 case OFPACT_SET_MPLS_TC
:
6662 case OFPACT_SET_MPLS_TTL
:
6663 case OFPACT_SET_QUEUE
:
6664 case OFPACT_SET_TUNNEL
:
6665 case OFPACT_SET_VLAN_PCP
:
6666 case OFPACT_SET_VLAN_VID
:
6667 case OFPACT_STRIP_VLAN
:
6670 /* In general these actions are excluded because they are not part of
6671 * the OpenFlow specification nor map to actions that are defined in
6672 * the specification. Thus the order in which they should be applied
6673 * in the action set is undefined. */
6676 case OFPACT_CONTROLLER
:
6678 case OFPACT_CT_CLEAR
:
6680 case OFPACT_ENQUEUE
:
6682 case OFPACT_UNROLL_XLATE
:
6683 case OFPACT_FIN_TIMEOUT
:
6685 case OFPACT_CONJUNCTION
:
6686 case OFPACT_MULTIPATH
:
6688 case OFPACT_OUTPUT_REG
:
6689 case OFPACT_POP_QUEUE
:
6690 case OFPACT_RESUBMIT
:
6692 case OFPACT_STACK_POP
:
6693 case OFPACT_STACK_PUSH
:
6694 case OFPACT_DEBUG_RECIRC
:
6696 /* The action set may only include actions and thus
6697 * may not include any instructions */
6698 case OFPACT_CLEAR_ACTIONS
:
6699 case OFPACT_GOTO_TABLE
:
6701 case OFPACT_WRITE_ACTIONS
:
6702 case OFPACT_WRITE_METADATA
:
6709 /* Append ofpact 'a' onto the tail of 'out' */
6711 ofpact_copy(struct ofpbuf
*out
, const struct ofpact
*a
)
6713 ofpbuf_put(out
, a
, OFPACT_ALIGN(a
->len
));
6716 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
6718 ofpacts_copy_last(struct ofpbuf
*out
, const struct ofpbuf
*in
,
6719 enum ofpact_type filter
)
6721 const struct ofpact
*target
;
6722 const struct ofpact
*a
;
6725 OFPACT_FOR_EACH (a
, in
->data
, in
->size
) {
6726 if (a
->type
== filter
) {
6731 ofpact_copy(out
, target
);
6733 return target
!= NULL
;
6736 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
6737 * The order of appended ofpacts is preserved between 'in' and 'out' */
6739 ofpacts_copy_all(struct ofpbuf
*out
, const struct ofpbuf
*in
,
6740 bool (*filter
)(const struct ofpact
*))
6742 const struct ofpact
*a
;
6744 OFPACT_FOR_EACH (a
, in
->data
, in
->size
) {
6746 ofpact_copy(out
, a
);
6751 /* Reads 'action_set', which contains ofpacts accumulated by
6752 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
6753 * executed directly into 'action_list'. (These names correspond to the
6754 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
6756 * In general this involves appending the last instance of each action that is
6757 * admissible in the action set in the order described in the OpenFlow
6761 * + output action is only appended if no group action was present in 'in'.
6762 * + As a simplification all set actions are copied in the order the are
6763 * provided in 'in' as many set actions applied to a field has the same
6764 * affect as only applying the last action that sets a field and
6765 * duplicates are removed by do_xlate_actions().
6766 * This has an unwanted side-effect of compsoting multiple
6767 * LOAD_REG actions that touch different regions of the same field. */
6769 ofpacts_execute_action_set(struct ofpbuf
*action_list
,
6770 const struct ofpbuf
*action_set
)
6772 /* The OpenFlow spec "Action Set" section specifies this order. */
6773 ofpacts_copy_last(action_list
, action_set
, OFPACT_STRIP_VLAN
);
6774 ofpacts_copy_last(action_list
, action_set
, OFPACT_POP_MPLS
);
6775 ofpacts_copy_last(action_list
, action_set
, OFPACT_PUSH_MPLS
);
6776 ofpacts_copy_last(action_list
, action_set
, OFPACT_PUSH_VLAN
);
6777 ofpacts_copy_last(action_list
, action_set
, OFPACT_DEC_TTL
);
6778 ofpacts_copy_last(action_list
, action_set
, OFPACT_DEC_MPLS_TTL
);
6779 ofpacts_copy_all(action_list
, action_set
, ofpact_is_set_or_move_action
);
6780 ofpacts_copy_last(action_list
, action_set
, OFPACT_SET_QUEUE
);
6782 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
6783 * we should execute only OFPACT_GROUP.
6785 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
6786 * all the actions because there's no point in modifying a packet that will
6787 * not be sent anywhere. */
6788 if (!ofpacts_copy_last(action_list
, action_set
, OFPACT_GROUP
) &&
6789 !ofpacts_copy_last(action_list
, action_set
, OFPACT_OUTPUT
) &&
6790 !ofpacts_copy_last(action_list
, action_set
, OFPACT_RESUBMIT
) &&
6791 !ofpacts_copy_last(action_list
, action_set
, OFPACT_CT
)) {
6792 ofpbuf_clear(action_list
);
6798 ofpacts_decode_for_action_set(const struct ofp_action_header
*in
,
6799 size_t n_in
, enum ofp_version version
,
6800 const struct vl_mff_map
*vl_mff_map
,
6801 uint64_t *ofpacts_tlv_bitmap
,
6806 size_t start
= out
->size
;
6808 error
= ofpacts_decode(in
, n_in
, version
, vl_mff_map
, ofpacts_tlv_bitmap
,
6815 OFPACT_FOR_EACH (a
, ofpact_end(out
->data
, start
), out
->size
- start
) {
6816 if (!ofpact_is_allowed_in_actions_set(a
)) {
6817 VLOG_WARN_RL(&rl
, "disallowed action in action set");
6818 return OFPERR_OFPBAC_BAD_TYPE
;
6825 /* OpenFlow 1.1 instructions. */
6827 struct instruction_type_info
{
6828 enum ovs_instruction_type type
;
6832 static const struct instruction_type_info inst_info
[] = {
6833 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
6839 ovs_instruction_name_from_type(enum ovs_instruction_type type
)
6841 return inst_info
[type
].name
;
6845 ovs_instruction_type_from_name(const char *name
)
6847 const struct instruction_type_info
*p
;
6848 for (p
= inst_info
; p
< &inst_info
[ARRAY_SIZE(inst_info
)]; p
++) {
6849 if (!strcasecmp(name
, p
->name
)) {
6856 enum ovs_instruction_type
6857 ovs_instruction_type_from_ofpact_type(enum ofpact_type type
)
6861 return OVSINST_OFPIT13_METER
;
6862 case OFPACT_CLEAR_ACTIONS
:
6863 return OVSINST_OFPIT11_CLEAR_ACTIONS
;
6864 case OFPACT_WRITE_ACTIONS
:
6865 return OVSINST_OFPIT11_WRITE_ACTIONS
;
6866 case OFPACT_WRITE_METADATA
:
6867 return OVSINST_OFPIT11_WRITE_METADATA
;
6868 case OFPACT_GOTO_TABLE
:
6869 return OVSINST_OFPIT11_GOTO_TABLE
;
6873 case OFPACT_CONTROLLER
:
6874 case OFPACT_ENQUEUE
:
6875 case OFPACT_OUTPUT_REG
:
6876 case OFPACT_OUTPUT_TRUNC
:
6878 case OFPACT_SET_VLAN_VID
:
6879 case OFPACT_SET_VLAN_PCP
:
6880 case OFPACT_STRIP_VLAN
:
6881 case OFPACT_PUSH_VLAN
:
6882 case OFPACT_SET_ETH_SRC
:
6883 case OFPACT_SET_ETH_DST
:
6884 case OFPACT_SET_IPV4_SRC
:
6885 case OFPACT_SET_IPV4_DST
:
6886 case OFPACT_SET_IP_DSCP
:
6887 case OFPACT_SET_IP_ECN
:
6888 case OFPACT_SET_IP_TTL
:
6889 case OFPACT_SET_L4_SRC_PORT
:
6890 case OFPACT_SET_L4_DST_PORT
:
6891 case OFPACT_REG_MOVE
:
6892 case OFPACT_SET_FIELD
:
6893 case OFPACT_STACK_PUSH
:
6894 case OFPACT_STACK_POP
:
6895 case OFPACT_DEC_TTL
:
6896 case OFPACT_SET_MPLS_LABEL
:
6897 case OFPACT_SET_MPLS_TC
:
6898 case OFPACT_SET_MPLS_TTL
:
6899 case OFPACT_DEC_MPLS_TTL
:
6900 case OFPACT_PUSH_MPLS
:
6901 case OFPACT_POP_MPLS
:
6902 case OFPACT_SET_TUNNEL
:
6903 case OFPACT_SET_QUEUE
:
6904 case OFPACT_POP_QUEUE
:
6905 case OFPACT_FIN_TIMEOUT
:
6906 case OFPACT_RESUBMIT
:
6908 case OFPACT_CONJUNCTION
:
6909 case OFPACT_MULTIPATH
:
6912 case OFPACT_UNROLL_XLATE
:
6914 case OFPACT_DEBUG_RECIRC
:
6916 case OFPACT_CT_CLEAR
:
6919 return OVSINST_OFPIT11_APPLY_ACTIONS
;
6924 ovs_instruction_type_from_inst_type(enum ovs_instruction_type
*instruction_type
,
6925 const uint16_t inst_type
)
6927 switch (inst_type
) {
6929 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6931 *instruction_type = OVSINST_##ENUM; \
6937 return OFPERR_OFPBIC_UNKNOWN_INST
;
6941 /* Two-way translation between OVS's internal "OVSINST_*" representation of
6942 * instructions and the "OFPIT_*" representation used in OpenFlow. */
6943 struct ovsinst_map
{
6944 enum ovs_instruction_type ovsinst
; /* Internal name for instruction. */
6945 int ofpit
; /* OFPIT_* number from OpenFlow spec. */
6948 static const struct ovsinst_map
*
6949 get_ovsinst_map(enum ofp_version version
)
6951 /* OpenFlow 1.1 and 1.2 instructions. */
6952 static const struct ovsinst_map of11
[] = {
6953 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
6954 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
6955 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
6956 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
6957 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
6961 /* OpenFlow 1.3+ instructions. */
6962 static const struct ovsinst_map of13
[] = {
6963 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
6964 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
6965 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
6966 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
6967 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
6968 { OVSINST_OFPIT13_METER
, 6 },
6972 return version
< OFP13_VERSION
? of11
: of13
;
6975 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
6976 * values, into a bitmap of instructions suitable for OpenFlow 'version'
6977 * (OFP11_VERSION or later), and returns the result. */
6979 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap
, enum ofp_version version
)
6981 uint32_t ofpit_bitmap
= 0;
6982 const struct ovsinst_map
*x
;
6984 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
6985 if (ovsinst_bitmap
& (1u << x
->ovsinst
)) {
6986 ofpit_bitmap
|= 1u << x
->ofpit
;
6989 return htonl(ofpit_bitmap
);
6992 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
6993 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
6994 * correspond to OVSINST_* values, and returns the result. */
6996 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap
, enum ofp_version version
)
6998 uint32_t ovsinst_bitmap
= 0;
6999 const struct ovsinst_map
*x
;
7001 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
7002 if (ofpit_bitmap
& htonl(1u << x
->ofpit
)) {
7003 ovsinst_bitmap
|= 1u << x
->ovsinst
;
7006 return ovsinst_bitmap
;
7009 static inline struct ofp11_instruction
*
7010 instruction_next(const struct ofp11_instruction
*inst
)
7012 return ((struct ofp11_instruction
*) (void *)
7013 ((uint8_t *) inst
+ ntohs(inst
->len
)));
7017 instruction_is_valid(const struct ofp11_instruction
*inst
,
7018 size_t n_instructions
)
7020 uint16_t len
= ntohs(inst
->len
);
7021 return (!(len
% OFP11_INSTRUCTION_ALIGN
)
7022 && len
>= sizeof *inst
7023 && len
/ sizeof *inst
<= n_instructions
);
7026 /* This macro is careful to check for instructions with bad lengths. */
7027 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
7028 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
7029 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
7030 ((LEFT) -= (ntohs((ITER)->len) \
7031 / sizeof(struct ofp11_instruction)), \
7032 (ITER) = instruction_next(ITER)))
7035 decode_openflow11_instruction(const struct ofp11_instruction
*inst
,
7036 enum ovs_instruction_type
*type
)
7038 uint16_t len
= ntohs(inst
->len
);
7040 switch (inst
->type
) {
7041 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER
):
7042 return OFPERR_OFPBIC_BAD_EXPERIMENTER
;
7044 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
7045 case CONSTANT_HTONS(ENUM): \
7047 ? len >= sizeof(struct STRUCT) \
7048 : len == sizeof(struct STRUCT)) { \
7049 *type = OVSINST_##ENUM; \
7052 return OFPERR_OFPBIC_BAD_LEN; \
7058 return OFPERR_OFPBIC_UNKNOWN_INST
;
7063 decode_openflow11_instructions(const struct ofp11_instruction insts
[],
7065 const struct ofp11_instruction
*out
[])
7067 const struct ofp11_instruction
*inst
;
7070 memset(out
, 0, N_OVS_INSTRUCTIONS
* sizeof *out
);
7071 INSTRUCTION_FOR_EACH (inst
, left
, insts
, n_insts
) {
7072 enum ovs_instruction_type type
;
7075 error
= decode_openflow11_instruction(inst
, &type
);
7081 return OFPERR_OFPBIC_DUP_INST
;
7087 VLOG_WARN_RL(&rl
, "bad instruction format at offset %"PRIuSIZE
,
7088 (n_insts
- left
) * sizeof *inst
);
7089 return OFPERR_OFPBIC_BAD_LEN
;
7095 get_actions_from_instruction(const struct ofp11_instruction
*inst
,
7096 const struct ofp_action_header
**actions
,
7097 size_t *actions_len
)
7099 *actions
= ALIGNED_CAST(const struct ofp_action_header
*, inst
+ 1);
7100 *actions_len
= ntohs(inst
->len
) - sizeof *inst
;
7104 ofpacts_pull_openflow_instructions(struct ofpbuf
*openflow
,
7105 unsigned int instructions_len
,
7106 enum ofp_version version
,
7107 const struct vl_mff_map
*vl_mff_map
,
7108 uint64_t *ofpacts_tlv_bitmap
,
7109 struct ofpbuf
*ofpacts
)
7111 const struct ofp11_instruction
*instructions
;
7112 const struct ofp11_instruction
*insts
[N_OVS_INSTRUCTIONS
];
7115 ofpbuf_clear(ofpacts
);
7116 if (version
== OFP10_VERSION
) {
7117 return ofpacts_pull_openflow_actions__(openflow
, instructions_len
,
7119 (1u << N_OVS_INSTRUCTIONS
) - 1,
7120 ofpacts
, 0, vl_mff_map
,
7121 ofpacts_tlv_bitmap
);
7124 if (instructions_len
% OFP11_INSTRUCTION_ALIGN
!= 0) {
7125 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u is not a "
7127 instructions_len
, OFP11_INSTRUCTION_ALIGN
);
7128 error
= OFPERR_OFPBIC_BAD_LEN
;
7132 instructions
= ofpbuf_try_pull(openflow
, instructions_len
);
7133 if (instructions
== NULL
) {
7134 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u exceeds "
7135 "remaining message length (%"PRIu32
")",
7136 instructions_len
, openflow
->size
);
7137 error
= OFPERR_OFPBIC_BAD_LEN
;
7141 error
= decode_openflow11_instructions(
7142 instructions
, instructions_len
/ OFP11_INSTRUCTION_ALIGN
,
7148 if (insts
[OVSINST_OFPIT13_METER
]) {
7149 const struct ofp13_instruction_meter
*oim
;
7150 struct ofpact_meter
*om
;
7152 oim
= ALIGNED_CAST(const struct ofp13_instruction_meter
*,
7153 insts
[OVSINST_OFPIT13_METER
]);
7155 om
= ofpact_put_METER(ofpacts
);
7156 om
->meter_id
= ntohl(oim
->meter_id
);
7157 om
->provider_meter_id
= UINT32_MAX
; /* No provider meter ID. */
7159 if (insts
[OVSINST_OFPIT11_APPLY_ACTIONS
]) {
7160 const struct ofp_action_header
*actions
;
7163 get_actions_from_instruction(insts
[OVSINST_OFPIT11_APPLY_ACTIONS
],
7164 &actions
, &actions_len
);
7165 error
= ofpacts_decode(actions
, actions_len
, version
, vl_mff_map
,
7166 ofpacts_tlv_bitmap
, ofpacts
);
7171 if (insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]) {
7172 instruction_get_OFPIT11_CLEAR_ACTIONS(
7173 insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]);
7174 ofpact_put_CLEAR_ACTIONS(ofpacts
);
7176 if (insts
[OVSINST_OFPIT11_WRITE_ACTIONS
]) {
7177 struct ofpact_nest
*on
;
7178 const struct ofp_action_header
*actions
;
7180 size_t start
= ofpacts
->size
;
7181 ofpact_put(ofpacts
, OFPACT_WRITE_ACTIONS
,
7182 offsetof(struct ofpact_nest
, actions
));
7183 get_actions_from_instruction(insts
[OVSINST_OFPIT11_WRITE_ACTIONS
],
7184 &actions
, &actions_len
);
7185 error
= ofpacts_decode_for_action_set(actions
, actions_len
,
7186 version
, vl_mff_map
,
7187 ofpacts_tlv_bitmap
, ofpacts
);
7191 on
= ofpbuf_at_assert(ofpacts
, start
, sizeof *on
);
7192 on
->ofpact
.len
= ofpacts
->size
- start
;
7194 if (insts
[OVSINST_OFPIT11_WRITE_METADATA
]) {
7195 const struct ofp11_instruction_write_metadata
*oiwm
;
7196 struct ofpact_metadata
*om
;
7198 oiwm
= ALIGNED_CAST(const struct ofp11_instruction_write_metadata
*,
7199 insts
[OVSINST_OFPIT11_WRITE_METADATA
]);
7201 om
= ofpact_put_WRITE_METADATA(ofpacts
);
7202 om
->metadata
= oiwm
->metadata
;
7203 om
->mask
= oiwm
->metadata_mask
;
7205 if (insts
[OVSINST_OFPIT11_GOTO_TABLE
]) {
7206 const struct ofp11_instruction_goto_table
*oigt
;
7207 struct ofpact_goto_table
*ogt
;
7209 oigt
= instruction_get_OFPIT11_GOTO_TABLE(
7210 insts
[OVSINST_OFPIT11_GOTO_TABLE
]);
7211 ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
7212 ogt
->table_id
= oigt
->table_id
;
7215 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
,
7216 (1u << N_OVS_INSTRUCTIONS
) - 1, 0);
7219 ofpbuf_clear(ofpacts
);
7224 /* Update the length of the instruction that begins at offset 'ofs' within
7225 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
7226 * If the instruction contains no nested actions, deletes it entirely. */
7228 ofpacts_update_instruction_actions(struct ofpbuf
*openflow
, size_t ofs
)
7230 struct ofp11_instruction_actions
*oia
;
7232 oia
= ofpbuf_at_assert(openflow
, ofs
, sizeof *oia
);
7233 if (openflow
->size
> ofs
+ sizeof *oia
) {
7234 oia
->len
= htons(openflow
->size
- ofs
);
7236 openflow
->size
= ofs
;
7240 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
7241 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
7242 * valid, otherwise an OpenFlow error code. */
7244 ofpact_check_output_port(ofp_port_t port
, ofp_port_t max_ports
)
7252 case OFPP_CONTROLLER
:
7257 return OFPERR_OFPBAC_BAD_OUT_PORT
;
7260 if (ofp_to_u16(port
) < ofp_to_u16(max_ports
)) {
7263 return OFPERR_OFPBAC_BAD_OUT_PORT
;
7267 /* Removes the protocols that require consistency between match and actions
7268 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
7270 * (An example of an inconsistency between match and actions is a flow that
7271 * does not match on an MPLS Ethertype but has an action that pops an MPLS
7274 inconsistent_match(enum ofputil_protocol
*usable_protocols
)
7276 *usable_protocols
&= OFPUTIL_P_OF10_ANY
;
7279 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
7280 * caller must restore them.
7282 * Modifies some actions, filling in fields that could not be properly set
7283 * without context. */
7285 ofpact_check__(enum ofputil_protocol
*usable_protocols
, struct ofpact
*a
,
7286 struct match
*match
, ofp_port_t max_ports
,
7287 uint8_t table_id
, uint8_t n_tables
)
7289 struct flow
*flow
= &match
->flow
;
7290 const struct ofpact_enqueue
*enqueue
;
7291 const struct mf_field
*mf
;
7295 return ofpact_check_output_port(ofpact_get_OUTPUT(a
)->port
,
7298 case OFPACT_CONTROLLER
:
7301 case OFPACT_ENQUEUE
:
7302 enqueue
= ofpact_get_ENQUEUE(a
);
7303 if (ofp_to_u16(enqueue
->port
) >= ofp_to_u16(max_ports
)
7304 && enqueue
->port
!= OFPP_IN_PORT
7305 && enqueue
->port
!= OFPP_LOCAL
) {
7306 return OFPERR_OFPBAC_BAD_OUT_PORT
;
7310 case OFPACT_OUTPUT_REG
:
7311 return mf_check_src(&ofpact_get_OUTPUT_REG(a
)->src
, match
);
7313 case OFPACT_OUTPUT_TRUNC
:
7314 return ofpact_check_output_port(ofpact_get_OUTPUT_TRUNC(a
)->port
,
7318 return bundle_check(ofpact_get_BUNDLE(a
), max_ports
, match
);
7320 case OFPACT_SET_VLAN_VID
:
7321 /* Remember if we saw a vlan tag in the flow to aid translating to
7322 * OpenFlow 1.1+ if need be. */
7323 ofpact_get_SET_VLAN_VID(a
)->flow_has_vlan
=
7324 (flow
->vlans
[0].tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
7325 if (!(flow
->vlans
[0].tci
& htons(VLAN_CFI
)) &&
7326 !ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
7327 inconsistent_match(usable_protocols
);
7329 /* Temporary mark that we have a vlan tag. */
7330 flow
->vlans
[0].tci
|= htons(VLAN_CFI
);
7333 case OFPACT_SET_VLAN_PCP
:
7334 /* Remember if we saw a vlan tag in the flow to aid translating to
7335 * OpenFlow 1.1+ if need be. */
7336 ofpact_get_SET_VLAN_PCP(a
)->flow_has_vlan
=
7337 (flow
->vlans
[0].tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
7338 if (!(flow
->vlans
[0].tci
& htons(VLAN_CFI
)) &&
7339 !ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
7340 inconsistent_match(usable_protocols
);
7342 /* Temporary mark that we have a vlan tag. */
7343 flow
->vlans
[0].tci
|= htons(VLAN_CFI
);
7346 case OFPACT_STRIP_VLAN
:
7347 if (!(flow
->vlans
[0].tci
& htons(VLAN_CFI
))) {
7348 inconsistent_match(usable_protocols
);
7350 flow_pop_vlan(flow
, NULL
);
7353 case OFPACT_PUSH_VLAN
:
7354 if (flow
->vlans
[FLOW_MAX_VLAN_HEADERS
- 1].tci
& htons(VLAN_CFI
)) {
7355 /* Support maximum (FLOW_MAX_VLAN_HEADERS) VLAN headers. */
7356 return OFPERR_OFPBAC_BAD_TAG
;
7358 /* Temporary mark that we have a vlan tag. */
7359 flow_push_vlan_uninit(flow
, NULL
);
7360 flow
->vlans
[0].tci
|= htons(VLAN_CFI
);
7363 case OFPACT_SET_ETH_SRC
:
7364 case OFPACT_SET_ETH_DST
:
7367 case OFPACT_SET_IPV4_SRC
:
7368 case OFPACT_SET_IPV4_DST
:
7369 if (flow
->dl_type
!= htons(ETH_TYPE_IP
)) {
7370 inconsistent_match(usable_protocols
);
7374 case OFPACT_SET_IP_DSCP
:
7375 case OFPACT_SET_IP_ECN
:
7376 case OFPACT_SET_IP_TTL
:
7377 case OFPACT_DEC_TTL
:
7378 if (!is_ip_any(flow
)) {
7379 inconsistent_match(usable_protocols
);
7383 case OFPACT_SET_L4_SRC_PORT
:
7384 case OFPACT_SET_L4_DST_PORT
:
7385 if (!is_ip_any(flow
) || (flow
->nw_frag
& FLOW_NW_FRAG_LATER
) ||
7386 (flow
->nw_proto
!= IPPROTO_TCP
&& flow
->nw_proto
!= IPPROTO_UDP
7387 && flow
->nw_proto
!= IPPROTO_SCTP
)) {
7388 inconsistent_match(usable_protocols
);
7390 /* Note on which transport protocol the port numbers are set.
7391 * This allows this set action to be converted to an OF1.2 set field
7393 if (a
->type
== OFPACT_SET_L4_SRC_PORT
) {
7394 ofpact_get_SET_L4_SRC_PORT(a
)->flow_ip_proto
= flow
->nw_proto
;
7396 ofpact_get_SET_L4_DST_PORT(a
)->flow_ip_proto
= flow
->nw_proto
;
7400 case OFPACT_REG_MOVE
:
7401 return nxm_reg_move_check(ofpact_get_REG_MOVE(a
), match
);
7403 case OFPACT_SET_FIELD
:
7404 mf
= ofpact_get_SET_FIELD(a
)->field
;
7405 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
7406 if (!mf_are_prereqs_ok(mf
, flow
, NULL
) ||
7407 (mf
->id
== MFF_VLAN_VID
&&
7408 !(flow
->vlans
[0].tci
& htons(VLAN_CFI
)))) {
7409 VLOG_WARN_RL(&rl
, "set_field %s lacks correct prerequisities",
7411 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
7413 /* Remember if we saw a vlan tag in the flow to aid translating to
7414 * OpenFlow 1.1 if need be. */
7415 ofpact_get_SET_FIELD(a
)->flow_has_vlan
=
7416 (flow
->vlans
[0].tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
7417 if (mf
->id
== MFF_VLAN_TCI
) {
7418 /* The set field may add or remove the vlan tag,
7419 * Mark the status temporarily. */
7420 flow
->vlans
[0].tci
= ofpact_get_SET_FIELD(a
)->value
->be16
;
7424 case OFPACT_STACK_PUSH
:
7425 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a
), match
);
7427 case OFPACT_STACK_POP
:
7428 return nxm_stack_pop_check(ofpact_get_STACK_POP(a
), match
);
7430 case OFPACT_SET_MPLS_LABEL
:
7431 case OFPACT_SET_MPLS_TC
:
7432 case OFPACT_SET_MPLS_TTL
:
7433 case OFPACT_DEC_MPLS_TTL
:
7434 if (!eth_type_mpls(flow
->dl_type
)) {
7435 inconsistent_match(usable_protocols
);
7439 case OFPACT_SET_TUNNEL
:
7440 case OFPACT_SET_QUEUE
:
7441 case OFPACT_POP_QUEUE
:
7444 case OFPACT_RESUBMIT
: {
7445 struct ofpact_resubmit
*resubmit
= ofpact_get_RESUBMIT(a
);
7447 if (resubmit
->with_ct_orig
&& !is_ct_valid(flow
, &match
->wc
, NULL
)) {
7448 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
7452 case OFPACT_FIN_TIMEOUT
:
7453 if (flow
->nw_proto
!= IPPROTO_TCP
) {
7454 inconsistent_match(usable_protocols
);
7459 return learn_check(ofpact_get_LEARN(a
), match
);
7461 case OFPACT_CONJUNCTION
:
7464 case OFPACT_MULTIPATH
:
7465 return multipath_check(ofpact_get_MULTIPATH(a
), match
);
7471 case OFPACT_PUSH_MPLS
:
7472 flow
->dl_type
= ofpact_get_PUSH_MPLS(a
)->ethertype
;
7473 /* The packet is now MPLS and the MPLS payload is opaque.
7474 * Thus nothing can be assumed about the network protocol.
7475 * Temporarily mark that we have no nw_proto. */
7479 case OFPACT_POP_MPLS
:
7480 if (!eth_type_mpls(flow
->dl_type
)) {
7481 inconsistent_match(usable_protocols
);
7483 flow
->dl_type
= ofpact_get_POP_MPLS(a
)->ethertype
;
7489 case OFPACT_CLONE
: {
7490 struct ofpact_nest
*on
= ofpact_get_CLONE(a
);
7491 return ofpacts_check(on
->actions
, ofpact_nest_get_action_len(on
),
7492 match
, max_ports
, table_id
, n_tables
,
7497 struct ofpact_conntrack
*oc
= ofpact_get_CT(a
);
7499 if (!dl_type_is_ip_any(flow
->dl_type
)
7500 || (flow
->ct_state
& CS_INVALID
&& oc
->flags
& NX_CT_F_COMMIT
)
7501 || (oc
->alg
== IPPORT_FTP
&& flow
->nw_proto
!= IPPROTO_TCP
)
7502 || (oc
->alg
== IPPORT_TFTP
&& flow
->nw_proto
!= IPPROTO_UDP
)) {
7503 /* We can't downgrade to OF1.0 and expect inconsistent CT actions
7504 * be silently discarded. Instead, datapath flow install fails, so
7505 * it is better to flag inconsistent CT actions as hard errors. */
7506 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
7509 if (oc
->zone_src
.field
) {
7510 return mf_check_src(&oc
->zone_src
, match
);
7513 return ofpacts_check(oc
->actions
, ofpact_ct_get_action_len(oc
),
7514 match
, max_ports
, table_id
, n_tables
,
7518 case OFPACT_CT_CLEAR
:
7522 struct ofpact_nat
*on
= ofpact_get_NAT(a
);
7524 if (!dl_type_is_ip_any(flow
->dl_type
) ||
7525 (on
->range_af
== AF_INET
&& flow
->dl_type
!= htons(ETH_TYPE_IP
)) ||
7526 (on
->range_af
== AF_INET6
7527 && flow
->dl_type
!= htons(ETH_TYPE_IPV6
))) {
7528 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
7533 case OFPACT_CLEAR_ACTIONS
:
7536 case OFPACT_WRITE_ACTIONS
: {
7537 /* Use a temporary copy of 'usable_protocols' because we can't check
7538 * consistency of an action set. */
7539 struct ofpact_nest
*on
= ofpact_get_WRITE_ACTIONS(a
);
7540 enum ofputil_protocol p
= *usable_protocols
;
7541 return ofpacts_check(on
->actions
, ofpact_nest_get_action_len(on
),
7542 match
, max_ports
, table_id
, n_tables
, &p
);
7545 case OFPACT_WRITE_METADATA
:
7548 case OFPACT_METER
: {
7549 uint32_t mid
= ofpact_get_METER(a
)->meter_id
;
7550 if (mid
== 0 || mid
> OFPM13_MAX
) {
7551 return OFPERR_OFPMMFC_INVALID_METER
;
7556 case OFPACT_GOTO_TABLE
: {
7557 uint8_t goto_table
= ofpact_get_GOTO_TABLE(a
)->table_id
;
7558 if ((table_id
!= 255 && goto_table
<= table_id
)
7559 || (n_tables
!= 255 && goto_table
>= n_tables
)) {
7560 return OFPERR_OFPBIC_BAD_TABLE_ID
;
7568 case OFPACT_UNROLL_XLATE
:
7569 /* UNROLL is an internal action that should never be seen via
7571 return OFPERR_OFPBAC_BAD_TYPE
;
7573 case OFPACT_DEBUG_RECIRC
:
7581 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
7582 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
7583 * switch with no more than 'max_ports' ports.
7585 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
7586 * '*usable_protocols' the protocols that forbid the inconsistency. (An
7587 * example of an inconsistency between match and actions is a flow that does
7588 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
7590 * May annotate ofpacts with information gathered from the 'match'.
7592 * May temporarily modify 'match', but restores the changes before
7595 ofpacts_check(struct ofpact ofpacts
[], size_t ofpacts_len
,
7596 struct match
*match
, ofp_port_t max_ports
,
7597 uint8_t table_id
, uint8_t n_tables
,
7598 enum ofputil_protocol
*usable_protocols
)
7601 ovs_be16 dl_type
= match
->flow
.dl_type
;
7602 uint8_t nw_proto
= match
->flow
.nw_proto
;
7603 enum ofperr error
= 0;
7604 union flow_vlan_hdr vlans
[FLOW_MAX_VLAN_HEADERS
];
7606 memcpy(&vlans
, &match
->flow
.vlans
, sizeof(vlans
));
7608 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7609 error
= ofpact_check__(usable_protocols
, a
, match
,
7610 max_ports
, table_id
, n_tables
);
7615 /* Restore fields that may have been modified. */
7616 match
->flow
.dl_type
= dl_type
;
7617 memcpy(&match
->flow
.vlans
, &vlans
, sizeof(vlans
));
7618 match
->flow
.nw_proto
= nw_proto
;
7622 /* Like ofpacts_check(), but reports inconsistencies as
7623 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
7625 ofpacts_check_consistency(struct ofpact ofpacts
[], size_t ofpacts_len
,
7626 struct match
*match
, ofp_port_t max_ports
,
7627 uint8_t table_id
, uint8_t n_tables
,
7628 enum ofputil_protocol usable_protocols
)
7630 enum ofputil_protocol p
= usable_protocols
;
7633 error
= ofpacts_check(ofpacts
, ofpacts_len
, match
, max_ports
,
7634 table_id
, n_tables
, &p
);
7635 return (error
? error
7636 : p
!= usable_protocols
? OFPERR_OFPBAC_MATCH_INCONSISTENT
7640 /* Returns the destination field that 'ofpact' would write to, or NULL
7641 * if the action would not write to an mf_field. */
7642 const struct mf_field
*
7643 ofpact_get_mf_dst(const struct ofpact
*ofpact
)
7645 if (ofpact
->type
== OFPACT_SET_FIELD
) {
7646 const struct ofpact_set_field
*orl
;
7648 orl
= CONTAINER_OF(ofpact
, struct ofpact_set_field
, ofpact
);
7650 } else if (ofpact
->type
== OFPACT_REG_MOVE
) {
7651 const struct ofpact_reg_move
*orm
;
7653 orm
= CONTAINER_OF(ofpact
, struct ofpact_reg_move
, ofpact
);
7654 return orm
->dst
.field
;
7661 unsupported_nesting(enum ofpact_type action
, enum ofpact_type outer_action
)
7663 VLOG_WARN("%s action doesn't support nested action %s",
7664 ofpact_name(outer_action
), ofpact_name(action
));
7665 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7669 field_requires_ct(enum mf_field_id field
)
7671 return field
== MFF_CT_MARK
|| field
== MFF_CT_LABEL
;
7674 /* Apply nesting constraints for actions */
7676 ofpacts_verify_nested(const struct ofpact
*a
, enum ofpact_type outer_action
)
7678 const struct mf_field
*field
= ofpact_get_mf_dst(a
);
7680 if (field
&& field_requires_ct(field
->id
) && outer_action
!= OFPACT_CT
) {
7681 VLOG_WARN("cannot set CT fields outside of ct action");
7682 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
7684 if (a
->type
== OFPACT_NAT
) {
7685 if (outer_action
!= OFPACT_CT
) {
7686 VLOG_WARN("Cannot have NAT action outside of \"ct\" action");
7687 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
7693 ovs_assert(outer_action
== OFPACT_WRITE_ACTIONS
7694 || outer_action
== OFPACT_CT
);
7696 if (outer_action
== OFPACT_CT
) {
7698 return unsupported_nesting(a
->type
, outer_action
);
7699 } else if (!field_requires_ct(field
->id
)) {
7700 VLOG_WARN("%s action doesn't support nested modification "
7701 "of %s", ofpact_name(outer_action
), field
->name
);
7702 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7710 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
7711 * appropriate order as defined by the OpenFlow spec and as required by Open
7714 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
7715 * instructions that are allowed within 'ofpacts[]'.
7717 * If 'outer_action' is not zero, it specifies that the actions are nested
7718 * within another action of type 'outer_action'. */
7720 ofpacts_verify(const struct ofpact ofpacts
[], size_t ofpacts_len
,
7721 uint32_t allowed_ovsinsts
, enum ofpact_type outer_action
)
7723 const struct ofpact
*a
;
7724 enum ovs_instruction_type inst
;
7726 inst
= OVSINST_OFPIT13_METER
;
7727 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7728 enum ovs_instruction_type next
;
7731 if (a
->type
== OFPACT_CONJUNCTION
) {
7732 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7733 if (a
->type
!= OFPACT_CONJUNCTION
&& a
->type
!= OFPACT_NOTE
) {
7734 VLOG_WARN("\"conjunction\" actions may be used along with "
7735 "\"note\" but not any other kind of action "
7736 "(such as the \"%s\" action used here)",
7737 ofpact_name(a
->type
));
7738 return OFPERR_NXBAC_BAD_CONJUNCTION
;
7744 error
= ofpacts_verify_nested(a
, outer_action
);
7749 next
= ovs_instruction_type_from_ofpact_type(a
->type
);
7751 && (inst
== OVSINST_OFPIT11_APPLY_ACTIONS
7754 const char *name
= ovs_instruction_name_from_type(inst
);
7755 const char *next_name
= ovs_instruction_name_from_type(next
);
7758 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
7759 "1.1+ compatibility", name
);
7761 VLOG_WARN("invalid instruction ordering: %s must appear "
7762 "before %s, for OpenFlow 1.1+ compatibility",
7765 return OFPERR_OFPBAC_UNSUPPORTED_ORDER
;
7767 if (!((1u << next
) & allowed_ovsinsts
)) {
7768 const char *name
= ovs_instruction_name_from_type(next
);
7770 VLOG_WARN("%s instruction not allowed here", name
);
7771 return OFPERR_OFPBIC_UNSUP_INST
;
7780 /* Converting ofpacts to OpenFlow. */
7783 encode_ofpact(const struct ofpact
*a
, enum ofp_version ofp_version
,
7787 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7788 case OFPACT_##ENUM: \
7789 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
7798 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
7799 * actions in 'openflow', appending the actions to any existing data in
7802 ofpacts_put_openflow_actions(const struct ofpact ofpacts
[], size_t ofpacts_len
,
7803 struct ofpbuf
*openflow
,
7804 enum ofp_version ofp_version
)
7806 const struct ofpact
*a
;
7807 size_t start_size
= openflow
->size
;
7809 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7810 encode_ofpact(a
, ofp_version
, openflow
);
7812 return openflow
->size
- start_size
;
7815 static enum ovs_instruction_type
7816 ofpact_is_apply_actions(const struct ofpact
*a
)
7818 return (ovs_instruction_type_from_ofpact_type(a
->type
)
7819 == OVSINST_OFPIT11_APPLY_ACTIONS
);
7823 ofpacts_put_openflow_instructions(const struct ofpact ofpacts
[],
7825 struct ofpbuf
*openflow
,
7826 enum ofp_version ofp_version
)
7828 const struct ofpact
*end
= ofpact_end(ofpacts
, ofpacts_len
);
7829 const struct ofpact
*a
;
7831 if (ofp_version
== OFP10_VERSION
) {
7832 ofpacts_put_openflow_actions(ofpacts
, ofpacts_len
, openflow
,
7839 if (ofpact_is_apply_actions(a
)) {
7840 size_t ofs
= openflow
->size
;
7842 instruction_put_OFPIT11_APPLY_ACTIONS(openflow
);
7844 encode_ofpact(a
, ofp_version
, openflow
);
7846 } while (a
< end
&& ofpact_is_apply_actions(a
));
7847 ofpacts_update_instruction_actions(openflow
, ofs
);
7849 encode_ofpact(a
, ofp_version
, openflow
);
7855 /* Sets of supported actions. */
7857 /* Two-way translation between OVS's internal "OFPACT_*" representation of
7858 * actions and the "OFPAT_*" representation used in some OpenFlow version.
7859 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
7860 * instance is specific to one OpenFlow version.) */
7862 enum ofpact_type ofpact
; /* Internal name for action type. */
7863 int ofpat
; /* OFPAT_* number from OpenFlow spec. */
7866 static const struct ofpact_map
*
7867 get_ofpact_map(enum ofp_version version
)
7869 /* OpenFlow 1.0 actions. */
7870 static const struct ofpact_map of10
[] = {
7871 { OFPACT_OUTPUT
, 0 },
7872 { OFPACT_SET_VLAN_VID
, 1 },
7873 { OFPACT_SET_VLAN_PCP
, 2 },
7874 { OFPACT_STRIP_VLAN
, 3 },
7875 { OFPACT_SET_ETH_SRC
, 4 },
7876 { OFPACT_SET_ETH_DST
, 5 },
7877 { OFPACT_SET_IPV4_SRC
, 6 },
7878 { OFPACT_SET_IPV4_DST
, 7 },
7879 { OFPACT_SET_IP_DSCP
, 8 },
7880 { OFPACT_SET_L4_SRC_PORT
, 9 },
7881 { OFPACT_SET_L4_DST_PORT
, 10 },
7882 { OFPACT_ENQUEUE
, 11 },
7886 /* OpenFlow 1.1 actions. */
7887 static const struct ofpact_map of11
[] = {
7888 { OFPACT_OUTPUT
, 0 },
7889 { OFPACT_SET_VLAN_VID
, 1 },
7890 { OFPACT_SET_VLAN_PCP
, 2 },
7891 { OFPACT_SET_ETH_SRC
, 3 },
7892 { OFPACT_SET_ETH_DST
, 4 },
7893 { OFPACT_SET_IPV4_SRC
, 5 },
7894 { OFPACT_SET_IPV4_DST
, 6 },
7895 { OFPACT_SET_IP_DSCP
, 7 },
7896 { OFPACT_SET_IP_ECN
, 8 },
7897 { OFPACT_SET_L4_SRC_PORT
, 9 },
7898 { OFPACT_SET_L4_DST_PORT
, 10 },
7899 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7900 /* OFPAT_COPY_TTL_IN (12) not supported. */
7901 { OFPACT_SET_MPLS_LABEL
, 13 },
7902 { OFPACT_SET_MPLS_TC
, 14 },
7903 { OFPACT_SET_MPLS_TTL
, 15 },
7904 { OFPACT_DEC_MPLS_TTL
, 16 },
7905 { OFPACT_PUSH_VLAN
, 17 },
7906 { OFPACT_STRIP_VLAN
, 18 },
7907 { OFPACT_PUSH_MPLS
, 19 },
7908 { OFPACT_POP_MPLS
, 20 },
7909 { OFPACT_SET_QUEUE
, 21 },
7910 { OFPACT_GROUP
, 22 },
7911 { OFPACT_SET_IP_TTL
, 23 },
7912 { OFPACT_DEC_TTL
, 24 },
7916 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
7917 static const struct ofpact_map of12
[] = {
7918 { OFPACT_OUTPUT
, 0 },
7919 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7920 /* OFPAT_COPY_TTL_IN (12) not supported. */
7921 { OFPACT_SET_MPLS_TTL
, 15 },
7922 { OFPACT_DEC_MPLS_TTL
, 16 },
7923 { OFPACT_PUSH_VLAN
, 17 },
7924 { OFPACT_STRIP_VLAN
, 18 },
7925 { OFPACT_PUSH_MPLS
, 19 },
7926 { OFPACT_POP_MPLS
, 20 },
7927 { OFPACT_SET_QUEUE
, 21 },
7928 { OFPACT_GROUP
, 22 },
7929 { OFPACT_SET_IP_TTL
, 23 },
7930 { OFPACT_DEC_TTL
, 24 },
7931 { OFPACT_SET_FIELD
, 25 },
7932 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
7933 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
7954 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
7955 * values, into a bitmap of actions suitable for OpenFlow 'version', and
7956 * returns the result. */
7958 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap
, enum ofp_version version
)
7960 uint32_t openflow_bitmap
= 0;
7961 const struct ofpact_map
*x
;
7963 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
7964 if (ofpacts_bitmap
& (UINT64_C(1) << x
->ofpact
)) {
7965 openflow_bitmap
|= 1u << x
->ofpat
;
7968 return htonl(openflow_bitmap
);
7971 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
7972 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
7973 * and returns the result. */
7975 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap
, enum ofp_version version
)
7977 uint64_t ofpact_bitmap
= 0;
7978 const struct ofpact_map
*x
;
7980 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
7981 if (ofpat_bitmap
& htonl(1u << x
->ofpat
)) {
7982 ofpact_bitmap
|= UINT64_C(1) << x
->ofpact
;
7985 return ofpact_bitmap
;
7988 /* Appends to 's' a string representation of the set of OFPACT_* represented
7989 * by 'ofpacts_bitmap'. */
7991 ofpact_bitmap_format(uint64_t ofpacts_bitmap
, struct ds
*s
)
7993 if (!ofpacts_bitmap
) {
7994 ds_put_cstr(s
, "<none>");
7996 while (ofpacts_bitmap
) {
7997 ds_put_format(s
, "%s ",
7998 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap
)));
7999 ofpacts_bitmap
= zero_rightmost_1bit(ofpacts_bitmap
);
8005 /* Returns true if 'action' outputs to 'port', false otherwise. */
8007 ofpact_outputs_to_port(const struct ofpact
*ofpact
, ofp_port_t port
)
8009 switch (ofpact
->type
) {
8011 return ofpact_get_OUTPUT(ofpact
)->port
== port
;
8012 case OFPACT_ENQUEUE
:
8013 return ofpact_get_ENQUEUE(ofpact
)->port
== port
;
8014 case OFPACT_CONTROLLER
:
8015 return port
== OFPP_CONTROLLER
;
8017 case OFPACT_OUTPUT_REG
:
8018 case OFPACT_OUTPUT_TRUNC
:
8020 case OFPACT_SET_VLAN_VID
:
8021 case OFPACT_SET_VLAN_PCP
:
8022 case OFPACT_STRIP_VLAN
:
8023 case OFPACT_PUSH_VLAN
:
8024 case OFPACT_SET_ETH_SRC
:
8025 case OFPACT_SET_ETH_DST
:
8026 case OFPACT_SET_IPV4_SRC
:
8027 case OFPACT_SET_IPV4_DST
:
8028 case OFPACT_SET_IP_DSCP
:
8029 case OFPACT_SET_IP_ECN
:
8030 case OFPACT_SET_IP_TTL
:
8031 case OFPACT_SET_L4_SRC_PORT
:
8032 case OFPACT_SET_L4_DST_PORT
:
8033 case OFPACT_REG_MOVE
:
8034 case OFPACT_SET_FIELD
:
8035 case OFPACT_STACK_PUSH
:
8036 case OFPACT_STACK_POP
:
8037 case OFPACT_DEC_TTL
:
8038 case OFPACT_SET_MPLS_LABEL
:
8039 case OFPACT_SET_MPLS_TC
:
8040 case OFPACT_SET_MPLS_TTL
:
8041 case OFPACT_DEC_MPLS_TTL
:
8042 case OFPACT_SET_TUNNEL
:
8043 case OFPACT_WRITE_METADATA
:
8044 case OFPACT_SET_QUEUE
:
8045 case OFPACT_POP_QUEUE
:
8046 case OFPACT_FIN_TIMEOUT
:
8047 case OFPACT_RESUBMIT
:
8049 case OFPACT_CONJUNCTION
:
8050 case OFPACT_MULTIPATH
:
8053 case OFPACT_UNROLL_XLATE
:
8054 case OFPACT_PUSH_MPLS
:
8055 case OFPACT_POP_MPLS
:
8057 case OFPACT_CLEAR_ACTIONS
:
8059 case OFPACT_WRITE_ACTIONS
:
8060 case OFPACT_GOTO_TABLE
:
8063 case OFPACT_DEBUG_RECIRC
:
8065 case OFPACT_CT_CLEAR
:
8072 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
8073 * to 'port', false otherwise. */
8075 ofpacts_output_to_port(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
8078 const struct ofpact
*a
;
8080 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
8081 if (ofpact_outputs_to_port(a
, port
)) {
8089 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
8090 * to 'group', false otherwise. */
8092 ofpacts_output_to_group(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
8095 const struct ofpact
*a
;
8097 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
8098 if (a
->type
== OFPACT_GROUP
8099 && ofpact_get_GROUP(a
)->group_id
== group_id
) {
8108 ofpacts_equal(const struct ofpact
*a
, size_t a_len
,
8109 const struct ofpact
*b
, size_t b_len
)
8111 return a_len
== b_len
&& !memcmp(a
, b
, a_len
);
8114 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
8115 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
8117 * This function relies on the order of 'ofpacts' being correct (as checked by
8118 * ofpacts_verify()). */
8120 ofpacts_get_meter(const struct ofpact ofpacts
[], size_t ofpacts_len
)
8122 const struct ofpact
*a
;
8124 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8125 enum ovs_instruction_type inst
;
8127 inst
= ovs_instruction_type_from_ofpact_type(a
->type
);
8128 if (a
->type
== OFPACT_METER
) {
8129 return ofpact_get_METER(a
)->meter_id
;
8130 } else if (inst
> OVSINST_OFPIT13_METER
) {
8138 /* Formatting ofpacts. */
8141 ofpact_format(const struct ofpact
*a
, struct ds
*s
)
8144 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
8145 case OFPACT_##ENUM: \
8146 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
8155 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
8156 * 'ofpacts' to 'string'. */
8158 ofpacts_format(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
8162 ds_put_format(string
, "%sdrop%s", colors
.drop
, colors
.end
);
8164 const struct ofpact
*a
;
8166 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8168 ds_put_char(string
, ',');
8171 ofpact_format(a
, string
);
8176 /* Internal use by helpers. */
8178 /* Implementation of ofpact_put_<ENUM>(). */
8180 ofpact_put(struct ofpbuf
*ofpacts
, enum ofpact_type type
, size_t len
)
8182 struct ofpact
*ofpact
;
8184 ofpacts
->header
= ofpbuf_put_uninit(ofpacts
, len
);
8185 ofpact
= ofpacts
->header
;
8186 ofpact_init(ofpact
, type
, len
);
8190 /* Implementation of ofpact_init_<ENUM>(). */
8192 ofpact_init(struct ofpact
*ofpact
, enum ofpact_type type
, size_t len
)
8194 memset(ofpact
, 0, len
);
8195 ofpact
->type
= type
;
8200 /* Implementation of ofpact_finish_<ENUM>().
8202 * Finishes composing a variable-length action (begun using
8203 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
8204 * bytes and updating its embedded length field. See the large comment near
8205 * the end of ofp-actions.h for more information.
8207 * May reallocate 'ofpacts'. Callers should consider updating their 'ofpact'
8208 * pointer to the return value of this function. */
8210 ofpact_finish(struct ofpbuf
*ofpacts
, struct ofpact
*ofpact
)
8214 ovs_assert(ofpact
== ofpacts
->header
);
8215 len
= (char *) ofpbuf_tail(ofpacts
) - (char *) ofpact
;
8216 ovs_assert(len
> 0 && len
<= UINT16_MAX
);
8218 ofpbuf_padto(ofpacts
, OFPACT_ALIGN(ofpacts
->size
));
8220 return ofpacts
->header
;
8223 static char * OVS_WARN_UNUSED_RESULT
8224 ofpact_parse(enum ofpact_type type
, char *value
, struct ofpbuf
*ofpacts
,
8225 enum ofputil_protocol
*usable_protocols
)
8228 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
8229 case OFPACT_##ENUM: \
8230 return parse_##ENUM(value, ofpacts, usable_protocols);
8239 ofpact_type_from_name(const char *name
, enum ofpact_type
*type
)
8241 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
8242 if (!strcasecmp(name, NAME)) { \
8243 *type = OFPACT_##ENUM; \
8252 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
8254 * Returns NULL if successful, otherwise a malloc()'d string describing the
8255 * error. The caller is responsible for freeing the returned string.
8257 * If 'outer_action' is specified, indicates that the actions being parsed
8258 * are nested within another action of the type specified in 'outer_action'. */
8259 static char * OVS_WARN_UNUSED_RESULT
8260 ofpacts_parse__(char *str
, struct ofpbuf
*ofpacts
,
8261 enum ofputil_protocol
*usable_protocols
,
8262 bool allow_instructions
, enum ofpact_type outer_action
)
8271 while (ofputil_parse_key_value(&pos
, &key
, &value
)) {
8272 enum ovs_instruction_type inst
= OVSINST_OFPIT11_APPLY_ACTIONS
;
8273 enum ofpact_type type
;
8277 if (ofpact_type_from_name(key
, &type
)) {
8278 error
= ofpact_parse(type
, value
, ofpacts
, usable_protocols
);
8279 inst
= ovs_instruction_type_from_ofpact_type(type
);
8280 } else if (!strcasecmp(key
, "mod_vlan_vid")) {
8281 error
= parse_set_vlan_vid(value
, ofpacts
, true);
8282 } else if (!strcasecmp(key
, "mod_vlan_pcp")) {
8283 error
= parse_set_vlan_pcp(value
, ofpacts
, true);
8284 } else if (!strcasecmp(key
, "set_nw_ttl")) {
8285 error
= parse_SET_IP_TTL(value
, ofpacts
, usable_protocols
);
8286 } else if (!strcasecmp(key
, "pop_vlan")) {
8287 error
= parse_pop_vlan(ofpacts
);
8288 } else if (!strcasecmp(key
, "set_tunnel64")) {
8289 error
= parse_set_tunnel(value
, ofpacts
,
8290 NXAST_RAW_SET_TUNNEL64
);
8291 } else if (!strcasecmp(key
, "load")) {
8292 error
= parse_reg_load(value
, ofpacts
);
8293 } else if (!strcasecmp(key
, "bundle_load")) {
8294 error
= parse_bundle_load(value
, ofpacts
);
8295 } else if (!strcasecmp(key
, "drop")) {
8297 } else if (!strcasecmp(key
, "apply_actions")) {
8298 return xstrdup("apply_actions is the default instruction");
8299 } else if (ofputil_port_from_string(key
, &port
)) {
8300 ofpact_put_OUTPUT(ofpacts
)->port
= port
;
8302 return xasprintf("unknown action %s", key
);
8308 if (inst
!= OVSINST_OFPIT11_APPLY_ACTIONS
) {
8309 if (!allow_instructions
) {
8310 return xasprintf("only actions are allowed here (not "
8312 ovs_instruction_name_from_type(inst
));
8314 if (inst
== prev_inst
) {
8315 return xasprintf("instruction %s may be specified only once",
8316 ovs_instruction_name_from_type(inst
));
8319 if (prev_inst
!= -1 && inst
< prev_inst
) {
8320 return xasprintf("instruction %s must be specified before %s",
8321 ovs_instruction_name_from_type(inst
),
8322 ovs_instruction_name_from_type(prev_inst
));
8327 if (drop
&& ofpacts
->size
) {
8328 return xstrdup("\"drop\" must not be accompanied by any other action "
8332 retval
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
,
8334 ? (1u << N_OVS_INSTRUCTIONS
) - 1
8335 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS
),
8338 return xstrdup("Incorrect instruction ordering");
8344 static char * OVS_WARN_UNUSED_RESULT
8345 ofpacts_parse(char *str
, struct ofpbuf
*ofpacts
,
8346 enum ofputil_protocol
*usable_protocols
, bool allow_instructions
,
8347 enum ofpact_type outer_action
)
8349 uint32_t orig_size
= ofpacts
->size
;
8350 char *error
= ofpacts_parse__(str
, ofpacts
, usable_protocols
,
8351 allow_instructions
, outer_action
);
8353 ofpacts
->size
= orig_size
;
8358 static char * OVS_WARN_UNUSED_RESULT
8359 ofpacts_parse_copy(const char *s_
, struct ofpbuf
*ofpacts
,
8360 enum ofputil_protocol
*usable_protocols
,
8361 bool allow_instructions
, enum ofpact_type outer_action
)
8365 *usable_protocols
= OFPUTIL_P_ANY
;
8368 error
= ofpacts_parse(s
, ofpacts
, usable_protocols
, allow_instructions
,
8375 /* Parses 's' as a set of OpenFlow actions and appends the actions to
8376 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
8377 * that are nested within the action of type 'outer_action'.
8379 * Returns NULL if successful, otherwise a malloc()'d string describing the
8380 * error. The caller is responsible for freeing the returned string. */
8381 char * OVS_WARN_UNUSED_RESULT
8382 ofpacts_parse_actions(const char *s
, struct ofpbuf
*ofpacts
,
8383 enum ofputil_protocol
*usable_protocols
)
8385 return ofpacts_parse_copy(s
, ofpacts
, usable_protocols
, false, 0);
8388 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
8391 * Returns NULL if successful, otherwise a malloc()'d string describing the
8392 * error. The caller is responsible for freeing the returned string. */
8393 char * OVS_WARN_UNUSED_RESULT
8394 ofpacts_parse_instructions(const char *s
, struct ofpbuf
*ofpacts
,
8395 enum ofputil_protocol
*usable_protocols
)
8397 return ofpacts_parse_copy(s
, ofpacts
, usable_protocols
, true, 0);
8401 ofpact_name(enum ofpact_type type
)
8404 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
8411 /* Low-level action decoding and encoding functions. */
8413 /* Everything needed to identify a particular OpenFlow action. */
8414 struct ofpact_hdrs
{
8415 uint32_t vendor
; /* 0 if standard, otherwise a vendor code. */
8416 uint16_t type
; /* Type if standard, otherwise subtype. */
8417 uint8_t ofp_version
; /* From ofp_header. */
8420 /* Information about a particular OpenFlow action. */
8421 struct ofpact_raw_instance
{
8422 /* The action's identity. */
8423 struct ofpact_hdrs hdrs
;
8424 enum ofp_raw_action_type raw
;
8426 /* Looking up the action. */
8427 struct hmap_node decode_node
; /* Based on 'hdrs'. */
8428 struct hmap_node encode_node
; /* Based on 'raw' + 'hdrs.ofp_version'. */
8430 /* The action's encoded size.
8432 * If this action is fixed-length, 'min_length' == 'max_length'.
8433 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
8434 * OFP_ACTION_ALIGN) == 65528. */
8435 unsigned short int min_length
;
8436 unsigned short int max_length
;
8438 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
8439 * offset of that argument from the beginning of the action and 'arg_len'
8440 * its length, both in bytes.
8442 * For actions that take other forms, these are both zero. */
8443 unsigned short int arg_ofs
;
8444 unsigned short int arg_len
;
8446 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
8449 /* If this action is deprecated, a human-readable string with a brief
8451 const char *deprecation
;
8454 /* Action header. */
8455 struct ofp_action_header
{
8456 /* The meaning of other values of 'type' generally depends on the OpenFlow
8457 * version (see enum ofp_raw_action_type).
8459 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
8460 * designates an OpenFlow vendor ID and that the remainder of the action
8461 * structure has a vendor-defined meaning.
8463 #define OFPAT_VENDOR 0xffff
8466 /* Always a multiple of 8. */
8469 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
8470 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
8473 OFP_ASSERT(sizeof(struct ofp_action_header
) == 8);
8476 ofpact_hdrs_equal(const struct ofpact_hdrs
*a
,
8477 const struct ofpact_hdrs
*b
)
8479 return (a
->vendor
== b
->vendor
8480 && a
->type
== b
->type
8481 && a
->ofp_version
== b
->ofp_version
);
8485 ofpact_hdrs_hash(const struct ofpact_hdrs
*hdrs
)
8487 return hash_2words(hdrs
->vendor
, (hdrs
->type
<< 16) | hdrs
->ofp_version
);
8490 #include "ofp-actions.inc2"
8492 static struct hmap
*
8493 ofpact_decode_hmap(void)
8495 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
8496 static struct hmap hmap
;
8498 if (ovsthread_once_start(&once
)) {
8499 struct ofpact_raw_instance
*inst
;
8502 for (inst
= all_raw_instances
;
8503 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
8505 hmap_insert(&hmap
, &inst
->decode_node
,
8506 ofpact_hdrs_hash(&inst
->hdrs
));
8508 ovsthread_once_done(&once
);
8513 static struct hmap
*
8514 ofpact_encode_hmap(void)
8516 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
8517 static struct hmap hmap
;
8519 if (ovsthread_once_start(&once
)) {
8520 struct ofpact_raw_instance
*inst
;
8523 for (inst
= all_raw_instances
;
8524 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
8526 hmap_insert(&hmap
, &inst
->encode_node
,
8527 hash_2words(inst
->raw
, inst
->hdrs
.ofp_version
));
8529 ovsthread_once_done(&once
);
8535 ofpact_decode_raw(enum ofp_version ofp_version
,
8536 const struct ofp_action_header
*oah
, size_t length
,
8537 const struct ofpact_raw_instance
**instp
)
8539 const struct ofpact_raw_instance
*inst
;
8540 struct ofpact_hdrs hdrs
;
8543 if (length
< sizeof *oah
) {
8544 return OFPERR_OFPBAC_BAD_LEN
;
8547 /* Get base action type. */
8548 if (oah
->type
== htons(OFPAT_VENDOR
)) {
8550 hdrs
.vendor
= ntohl(oah
->vendor
);
8551 if (hdrs
.vendor
== NX_VENDOR_ID
|| hdrs
.vendor
== ONF_VENDOR_ID
) {
8552 /* Get extension subtype. */
8553 const struct ext_action_header
*nah
;
8555 nah
= ALIGNED_CAST(const struct ext_action_header
*, oah
);
8556 if (length
< sizeof *nah
) {
8557 return OFPERR_OFPBAC_BAD_LEN
;
8559 hdrs
.type
= ntohs(nah
->subtype
);
8561 VLOG_WARN_RL(&rl
, "OpenFlow action has unknown vendor %#"PRIx32
,
8563 return OFPERR_OFPBAC_BAD_VENDOR
;
8567 hdrs
.type
= ntohs(oah
->type
);
8570 hdrs
.ofp_version
= ofp_version
;
8571 HMAP_FOR_EACH_WITH_HASH (inst
, decode_node
, ofpact_hdrs_hash(&hdrs
),
8572 ofpact_decode_hmap()) {
8573 if (ofpact_hdrs_equal(&hdrs
, &inst
->hdrs
)) {
8580 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
8581 : OFPERR_OFPBAC_BAD_TYPE
);
8585 ofpact_pull_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
8586 enum ofp_raw_action_type
*raw
, uint64_t *arg
)
8588 const struct ofp_action_header
*oah
= buf
->data
;
8589 const struct ofpact_raw_instance
*action
;
8590 unsigned int length
;
8594 error
= ofpact_decode_raw(ofp_version
, oah
, buf
->size
, &action
);
8599 if (action
->deprecation
) {
8600 VLOG_INFO_RL(&rl
, "%s is deprecated in %s (%s)",
8601 action
->name
, ofputil_version_to_string(ofp_version
),
8602 action
->deprecation
);
8605 length
= ntohs(oah
->len
);
8606 if (length
> buf
->size
) {
8607 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u exceeds action buffer "
8608 "length %"PRIu32
, action
->name
, length
, buf
->size
);
8609 return OFPERR_OFPBAC_BAD_LEN
;
8611 if (length
< action
->min_length
|| length
> action
->max_length
) {
8612 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u not in valid range "
8613 "[%hu,%hu]", action
->name
, length
,
8614 action
->min_length
, action
->max_length
);
8615 return OFPERR_OFPBAC_BAD_LEN
;
8618 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u is not a multiple "
8619 "of 8", action
->name
, length
);
8620 return OFPERR_OFPBAC_BAD_LEN
;
8625 if (action
->arg_len
) {
8629 p
= ofpbuf_at_assert(buf
, action
->arg_ofs
, action
->arg_len
);
8630 for (i
= 0; i
< action
->arg_len
; i
++) {
8631 *arg
= (*arg
<< 8) | p
[i
];
8635 ofpbuf_pull(buf
, length
);
8640 static const struct ofpact_raw_instance
*
8641 ofpact_raw_lookup(enum ofp_version ofp_version
, enum ofp_raw_action_type raw
)
8643 const struct ofpact_raw_instance
*inst
;
8645 HMAP_FOR_EACH_WITH_HASH (inst
, encode_node
, hash_2words(raw
, ofp_version
),
8646 ofpact_encode_hmap()) {
8647 if (inst
->raw
== raw
&& inst
->hdrs
.ofp_version
== ofp_version
) {
8655 ofpact_put_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
8656 enum ofp_raw_action_type raw
, uint64_t arg
)
8658 const struct ofpact_raw_instance
*inst
;
8659 struct ofp_action_header
*oah
;
8660 const struct ofpact_hdrs
*hdrs
;
8662 inst
= ofpact_raw_lookup(ofp_version
, raw
);
8665 oah
= ofpbuf_put_zeros(buf
, inst
->min_length
);
8666 oah
->type
= htons(hdrs
->vendor
? OFPAT_VENDOR
: hdrs
->type
);
8667 oah
->len
= htons(inst
->min_length
);
8668 oah
->vendor
= htonl(hdrs
->vendor
);
8670 switch (hdrs
->vendor
) {
8675 case ONF_VENDOR_ID
: {
8676 struct ext_action_header
*nah
= (struct ext_action_header
*) oah
;
8677 nah
->subtype
= htons(hdrs
->type
);
8685 if (inst
->arg_len
) {
8686 uint8_t *p
= (uint8_t *) oah
+ inst
->arg_ofs
+ inst
->arg_len
;
8689 for (i
= 0; i
< inst
->arg_len
; i
++) {
8701 pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
)
8703 struct ofp_action_header
*oah
;
8705 ofpbuf_put_zeros(openflow
, PAD_SIZE(openflow
->size
- start_ofs
,
8708 oah
= ofpbuf_at_assert(openflow
, start_ofs
, sizeof *oah
);
8709 oah
->len
= htons(openflow
->size
- start_ofs
);