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.
19 #include <sys/types.h>
20 #include <netinet/in.h>
23 #include "byte-order.h"
27 #include "openvswitch/hmap.h"
29 #include "multipath.h"
31 #include "odp-netlink.h"
32 #include "openvswitch/dynamic-string.h"
33 #include "openvswitch/meta-flow.h"
34 #include "openvswitch/ofp-actions.h"
35 #include "openvswitch/ofp-packet.h"
36 #include "openvswitch/ofp-parse.h"
37 #include "openvswitch/ofp-port.h"
38 #include "openvswitch/ofp-prop.h"
39 #include "openvswitch/ofp-table.h"
40 #include "openvswitch/ofpbuf.h"
41 #include "openvswitch/vlog.h"
42 #include "unaligned.h"
44 #include "vl-mff-map.h"
46 VLOG_DEFINE_THIS_MODULE(ofp_actions
);
48 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
50 struct ofp_action_header
;
52 /* Header for Open vSwitch and ONF vendor extension actions.
54 * This is the entire header for a few Open vSwitch vendor extension actions,
55 * the ones that either have no arguments or for which variable-length
56 * arguments follow the header.
58 * This cannot be used as an entirely generic vendor extension action header,
59 * because OpenFlow does not specify the location or size of the action
60 * subtype; it just happens that ONF extensions and Nicira extensions share
62 struct ext_action_header
{
63 ovs_be16 type
; /* OFPAT_VENDOR. */
64 ovs_be16 len
; /* At least 16. */
65 ovs_be32 vendor
; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
66 ovs_be16 subtype
; /* See enum ofp_raw_action_type. */
69 OFP_ASSERT(sizeof(struct ext_action_header
) == 16);
71 /* Raw identifiers for OpenFlow actions.
73 * Decoding and encoding OpenFlow actions across multiple versions is difficult
74 * to do in a clean, consistent way. This enumeration lays out all of the
75 * forms of actions that Open vSwitch supports.
77 * The comments here must follow a stylized form because the
78 * "extract-ofp-actions" program parses them at build time to generate data
81 * - The first part of each comment specifies the vendor, OpenFlow versions,
82 * and type for each protocol that supports the action:
84 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
85 * extension actions. (Support for other vendors can be added, but
86 * it can't be done just based on a vendor ID definition alone
87 * because OpenFlow doesn't define a standard way to specify a
88 * subtype for vendor actions, so other vendors might do it different
91 * # The version can specify a specific OpenFlow version, a version
92 * range delimited by "-", or an open-ended range with "+".
94 * # The type, in parentheses, is the action type number (for standard
95 * OpenFlow actions) or subtype (for vendor extension actions).
97 * # Optionally one may add "is deprecated" followed by a
98 * human-readable reason in parentheses (which will be used in log
99 * messages), if a particular action should no longer be used.
101 * Multiple such specifications may be separated by commas.
103 * - The second part describes the action's wire format. It may be:
105 * # "struct <name>": The struct fully specifies the wire format. The
106 * action is exactly the size of the struct. (Thus, the struct must
107 * be an exact multiple of 8 bytes in size.)
109 * # "struct <name>, ...": The struct specifies the beginning of the
110 * wire format. An instance of the action is either the struct's
111 * exact size, or a multiple of 8 bytes longer.
113 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
114 * vendor extension) header, followed by 0 or more pad bytes to align
115 * to a multiple of <N> bits, followed by an argument of the given
116 * type, followed by 0 or more pad bytes to bring the total action up
117 * to a multiple of 8 bytes.
119 * # "void": The action is just a (standard or vendor extension)
122 * # Optionally, one may add "VLMFF" in the end of the second part if
123 * the Openflow action may use a variable length meta-flow field
124 * (i.e. tun_metadata). Adding "VLMFF" will pass the per-switch based
125 * variable length meta-flow field mapping map (struct vl_mff_map) to
126 * the corresponding action decoding function.
128 * - Optional additional text enclosed in square brackets is commentary for
131 enum ofp_raw_action_type
{
132 /* ## ----------------- ## */
133 /* ## Standard actions. ## */
134 /* ## ----------------- ## */
136 /* OF1.0(0): struct ofp10_action_output. */
138 /* OF1.1+(0): struct ofp11_action_output. */
141 /* OF1.0(1): uint16_t. */
142 OFPAT_RAW10_SET_VLAN_VID
,
143 /* OF1.0(2): uint8_t. */
144 OFPAT_RAW10_SET_VLAN_PCP
,
146 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
148 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
149 * modification actions: the 1.0 versions push a VLAN header if none is
150 * present, but the 1.1 versions do not. That is the only reason that we
151 * distinguish their raw action types.] */
152 OFPAT_RAW11_SET_VLAN_VID
,
153 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
154 OFPAT_RAW11_SET_VLAN_PCP
,
156 /* OF1.1+(17): ovs_be16.
158 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
160 OFPAT_RAW11_PUSH_VLAN
,
162 /* OF1.0(3): void. */
163 OFPAT_RAW10_STRIP_VLAN
,
164 /* OF1.1+(18): void. */
165 OFPAT_RAW11_POP_VLAN
,
167 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
168 * ofp_action_dl_addr. */
169 OFPAT_RAW_SET_DL_SRC
,
171 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
172 * ofp_action_dl_addr. */
173 OFPAT_RAW_SET_DL_DST
,
175 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
177 OFPAT_RAW_SET_NW_SRC
,
179 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
181 OFPAT_RAW_SET_NW_DST
,
183 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
184 OFPAT_RAW_SET_NW_TOS
,
186 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
187 OFPAT_RAW11_SET_NW_ECN
,
189 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
191 OFPAT_RAW_SET_TP_SRC
,
193 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
195 OFPAT_RAW_SET_TP_DST
,
197 /* OF1.0(11): struct ofp10_action_enqueue. */
200 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
202 OFPAT_RAW_SET_MPLS_LABEL
,
204 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
206 OFPAT_RAW_SET_MPLS_TC
,
208 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
210 OFPAT_RAW_SET_MPLS_TTL
,
212 /* NX1.0(26), OF1.1+(16): void. */
213 OFPAT_RAW_DEC_MPLS_TTL
,
215 /* NX1.0(23), OF1.1+(19): ovs_be16.
217 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
220 /* NX1.0(24), OF1.1+(20): ovs_be16.
222 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
223 * ETH_TYPE_MPLS otherwise, not the label.] */
226 /* NX1.0(4), OF1.1+(21): uint32_t. */
229 /* NX1.0(40), OF1.1+(22): uint32_t. */
232 /* OF1.1+(23): uint8_t. */
233 OFPAT_RAW11_SET_NW_TTL
,
235 /* NX1.0(18), OF1.1+(24): void. */
236 OFPAT_RAW_DEC_NW_TTL
,
237 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
238 NXAST_RAW_DEC_TTL_CNT_IDS
,
240 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... VLMFF */
241 OFPAT_RAW12_SET_FIELD
,
242 /* OF1.5+(25): struct ofp12_action_set_field, ... VLMFF */
243 OFPAT_RAW15_SET_FIELD
,
244 /* NX1.0-1.4(7): struct nx_action_reg_load. VLMFF
246 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
247 * we drop reg_load.] */
249 /* NX1.0-1.4(33): struct ext_action_header, ... VLMFF
251 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
252 * we drop reg_load2.] */
255 /* OF1.5+(28): struct ofp15_action_copy_field, ... VLMFF */
256 OFPAT_RAW15_COPY_FIELD
,
257 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... VLMFF */
258 ONFACT_RAW13_COPY_FIELD
,
259 /* NX1.0-1.4(6): struct nx_action_reg_move, ... VLMFF */
262 /* ## ------------------------- ## */
263 /* ## Nicira extension actions. ## */
264 /* ## ------------------------- ## */
266 /* Actions similar to standard actions are listed with the standard actions. */
268 /* NX1.0+(1): uint16_t. */
270 /* NX1.0+(14): struct nx_action_resubmit. */
271 NXAST_RAW_RESUBMIT_TABLE
,
272 /* NX1.0+(44): struct nx_action_resubmit. */
273 NXAST_RAW_RESUBMIT_TABLE_CT
,
275 /* NX1.0+(2): uint32_t. */
276 NXAST_RAW_SET_TUNNEL
,
277 /* NX1.0+(9): uint64_t. */
278 NXAST_RAW_SET_TUNNEL64
,
280 /* NX1.0+(5): void. */
283 /* NX1.0+(8): struct nx_action_note, ... */
286 /* NX1.0+(10): struct nx_action_multipath. VLMFF */
289 /* NX1.0+(12): struct nx_action_bundle, ... */
291 /* NX1.0+(13): struct nx_action_bundle, ... VLMFF */
292 NXAST_RAW_BUNDLE_LOAD
,
294 /* NX1.0+(15): struct nx_action_output_reg. VLMFF */
295 NXAST_RAW_OUTPUT_REG
,
296 /* NX1.0+(32): struct nx_action_output_reg2. VLMFF */
297 NXAST_RAW_OUTPUT_REG2
,
299 /* NX1.0+(16): struct nx_action_learn, ... VLMFF */
301 /* NX1.0+(45): struct nx_action_learn2, ... VLMFF */
304 /* NX1.0+(17): void. */
307 /* NX1.0+(19): struct nx_action_fin_timeout. */
308 NXAST_RAW_FIN_TIMEOUT
,
310 /* NX1.0+(20): struct nx_action_controller. */
311 NXAST_RAW_CONTROLLER
,
312 /* NX1.0+(37): struct ext_action_header, ... */
313 NXAST_RAW_CONTROLLER2
,
315 /* NX1.0+(22): struct nx_action_write_metadata. */
316 NXAST_RAW_WRITE_METADATA
,
318 /* NX1.0+(27): struct nx_action_stack. VLMFF */
319 NXAST_RAW_STACK_PUSH
,
321 /* NX1.0+(28): struct nx_action_stack. VLMFF */
324 /* NX1.0+(29): struct nx_action_sample. */
326 /* NX1.0+(38): struct nx_action_sample2. */
328 /* NX1.0+(41): struct nx_action_sample2. */
331 /* NX1.0+(34): struct nx_action_conjunction. */
332 NXAST_RAW_CONJUNCTION
,
334 /* NX1.0+(35): struct nx_action_conntrack, ... VLMFF */
337 /* NX1.0+(36): struct nx_action_nat, ... */
340 /* NX1.0+(39): struct nx_action_output_trunc. */
341 NXAST_RAW_OUTPUT_TRUNC
,
343 /* NX1.0+(42): struct ext_action_header, ... VLMFF */
346 /* NX1.0+(43): void. */
349 /* NX1.3+(46): struct nx_action_encap, ... */
352 /* NX1.3+(47): struct nx_action_decap, ... */
355 /* NX1.3+(48): void. */
356 NXAST_RAW_DEC_NSH_TTL
,
358 /* ## ------------------ ## */
359 /* ## Debugging actions. ## */
360 /* ## ------------------ ## */
362 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
363 /* accepts them only if started with --enable-dummy. */
365 /* NX1.0+(254): void. */
366 NXAST_RAW_DEBUG_SLOW
,
368 /* NX1.0+(255): void. */
369 NXAST_RAW_DEBUG_RECIRC
,
372 /* OpenFlow actions are always a multiple of 8 bytes in length. */
373 #define OFP_ACTION_ALIGN 8
375 /* Define a few functions for working with instructions. */
376 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
377 static inline const struct STRUCT * OVS_UNUSED \
378 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
380 ovs_assert(inst->type == htons(ENUM)); \
381 return ALIGNED_CAST(struct STRUCT *, inst); \
384 static inline void OVS_UNUSED \
385 instruction_init_##ENUM(struct STRUCT *s) \
387 memset(s, 0, sizeof *s); \
388 s->type = htons(ENUM); \
389 s->len = htons(sizeof *s); \
392 static inline struct STRUCT * OVS_UNUSED \
393 instruction_put_##ENUM(struct ofpbuf *buf) \
395 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
396 instruction_init_##ENUM(s); \
402 static void ofpacts_update_instruction_actions(struct ofpbuf
*openflow
,
404 static void pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
);
406 static enum ofperr
ofpacts_verify(const struct ofpact
[], size_t ofpacts_len
,
407 uint32_t allowed_ovsinsts
,
408 enum ofpact_type outer_action
);
410 static void put_set_field(struct ofpbuf
*openflow
, enum ofp_version
,
411 enum mf_field_id
, uint64_t value
);
413 static void put_reg_load(struct ofpbuf
*openflow
,
414 const struct mf_subfield
*, uint64_t value
);
416 static enum ofperr
ofpact_pull_raw(struct ofpbuf
*, enum ofp_version
,
417 enum ofp_raw_action_type
*, uint64_t *arg
);
418 static void *ofpact_put_raw(struct ofpbuf
*, enum ofp_version
,
419 enum ofp_raw_action_type
, uint64_t arg
);
421 static char *OVS_WARN_UNUSED_RESULT
ofpacts_parse(
422 char *str
, const struct ofpact_parse_params
*pp
,
423 bool allow_instructions
, enum ofpact_type outer_action
);
424 static enum ofperr
ofpacts_pull_openflow_actions__(
425 struct ofpbuf
*openflow
, unsigned int actions_len
,
426 enum ofp_version version
, uint32_t allowed_ovsinsts
,
427 struct ofpbuf
*ofpacts
, enum ofpact_type outer_action
,
428 const struct vl_mff_map
*vl_mff_map
, uint64_t *ofpacts_tlv_bitmap
);
429 static char * OVS_WARN_UNUSED_RESULT
ofpacts_parse_copy(
430 const char *s_
, const struct ofpact_parse_params
*pp
,
431 bool allow_instructions
, enum ofpact_type outer_action
);
433 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
434 * nested ofpacts it returns the first one. */
436 ofpact_next_flattened(const struct ofpact
*ofpact
)
438 switch (ofpact
->type
) {
441 case OFPACT_CONTROLLER
:
443 case OFPACT_OUTPUT_REG
:
444 case OFPACT_OUTPUT_TRUNC
:
446 case OFPACT_SET_FIELD
:
447 case OFPACT_SET_VLAN_VID
:
448 case OFPACT_SET_VLAN_PCP
:
449 case OFPACT_STRIP_VLAN
:
450 case OFPACT_PUSH_VLAN
:
451 case OFPACT_SET_ETH_SRC
:
452 case OFPACT_SET_ETH_DST
:
453 case OFPACT_SET_IPV4_SRC
:
454 case OFPACT_SET_IPV4_DST
:
455 case OFPACT_SET_IP_DSCP
:
456 case OFPACT_SET_IP_ECN
:
457 case OFPACT_SET_IP_TTL
:
458 case OFPACT_SET_L4_SRC_PORT
:
459 case OFPACT_SET_L4_DST_PORT
:
460 case OFPACT_REG_MOVE
:
461 case OFPACT_STACK_PUSH
:
462 case OFPACT_STACK_POP
:
464 case OFPACT_SET_MPLS_LABEL
:
465 case OFPACT_SET_MPLS_TC
:
466 case OFPACT_SET_MPLS_TTL
:
467 case OFPACT_DEC_MPLS_TTL
:
468 case OFPACT_PUSH_MPLS
:
469 case OFPACT_POP_MPLS
:
470 case OFPACT_SET_TUNNEL
:
471 case OFPACT_SET_QUEUE
:
472 case OFPACT_POP_QUEUE
:
473 case OFPACT_FIN_TIMEOUT
:
474 case OFPACT_RESUBMIT
:
476 case OFPACT_CONJUNCTION
:
477 case OFPACT_MULTIPATH
:
481 case OFPACT_UNROLL_XLATE
:
482 case OFPACT_CT_CLEAR
:
483 case OFPACT_DEBUG_RECIRC
:
484 case OFPACT_DEBUG_SLOW
:
486 case OFPACT_CLEAR_ACTIONS
:
487 case OFPACT_WRITE_METADATA
:
488 case OFPACT_GOTO_TABLE
:
492 case OFPACT_DEC_NSH_TTL
:
493 return ofpact_next(ofpact
);
496 return ofpact_get_CLONE(ofpact
)->actions
;
499 return ofpact_get_CT(ofpact
)->actions
;
501 case OFPACT_WRITE_ACTIONS
:
502 return ofpact_get_WRITE_ACTIONS(ofpact
)->actions
;
508 /* Pull off existing actions or instructions. Used by nesting actions to keep
509 * ofpacts_parse() oblivious of actions nesting.
511 * Push the actions back on after nested parsing, e.g.:
513 * size_t ofs = ofpacts_pull(ofpacts);
514 * ...nested parsing...
515 * ofpbuf_push_uninit(ofpacts, ofs);
518 ofpacts_pull(struct ofpbuf
*ofpacts
)
523 ofpbuf_pull(ofpacts
, ofs
);
528 #include "ofp-actions.inc1"
530 /* Output actions. */
532 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
533 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
534 * number of bytes to send. A 'max_len' of zero means no bytes of the
535 * packet should be sent. */
536 struct ofp10_action_output
{
537 ovs_be16 type
; /* OFPAT10_OUTPUT. */
538 ovs_be16 len
; /* Length is 8. */
539 ovs_be16 port
; /* Output port. */
540 ovs_be16 max_len
; /* Max length to send to controller. */
542 OFP_ASSERT(sizeof(struct ofp10_action_output
) == 8);
544 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
545 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
546 * number of bytes to send. A 'max_len' of zero means no bytes of the
547 * packet should be sent.*/
548 struct ofp11_action_output
{
549 ovs_be16 type
; /* OFPAT11_OUTPUT. */
550 ovs_be16 len
; /* Length is 16. */
551 ovs_be32 port
; /* Output port. */
552 ovs_be16 max_len
; /* Max length to send to controller. */
553 uint8_t pad
[6]; /* Pad to 64 bits. */
555 OFP_ASSERT(sizeof(struct ofp11_action_output
) == 16);
558 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output
*oao
,
559 enum ofp_version ofp_version OVS_UNUSED
,
562 struct ofpact_output
*output
;
564 output
= ofpact_put_OUTPUT(out
);
565 output
->port
= u16_to_ofp(ntohs(oao
->port
));
566 output
->max_len
= ntohs(oao
->max_len
);
568 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
572 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output
*oao
,
573 enum ofp_version ofp_version OVS_UNUSED
,
576 struct ofpact_output
*output
;
579 output
= ofpact_put_OUTPUT(out
);
580 output
->max_len
= ntohs(oao
->max_len
);
582 error
= ofputil_port_from_ofp11(oao
->port
, &output
->port
);
587 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
591 encode_OUTPUT(const struct ofpact_output
*output
,
592 enum ofp_version ofp_version
, struct ofpbuf
*out
)
594 if (ofp_version
== OFP10_VERSION
) {
595 struct ofp10_action_output
*oao
;
597 oao
= put_OFPAT10_OUTPUT(out
);
598 oao
->port
= htons(ofp_to_u16(output
->port
));
599 oao
->max_len
= htons(output
->max_len
);
601 struct ofp11_action_output
*oao
;
603 oao
= put_OFPAT11_OUTPUT(out
);
604 oao
->port
= ofputil_port_to_ofp11(output
->port
);
605 oao
->max_len
= htons(output
->max_len
);
609 static char * OVS_WARN_UNUSED_RESULT
610 parse_truncate_subfield(const char *arg_
,
611 const struct ofpact_parse_params
*pp
,
612 struct ofpact_output_trunc
*output_trunc
)
615 char *arg
= CONST_CAST(char *, arg_
);
617 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
618 if (!strcmp(key
, "port")) {
619 if (!ofputil_port_from_string(value
, pp
->port_map
,
620 &output_trunc
->port
)) {
621 return xasprintf("output to unknown truncate port: %s",
624 if (ofp_to_u16(output_trunc
->port
) > ofp_to_u16(OFPP_MAX
)) {
625 if (output_trunc
->port
!= OFPP_LOCAL
&&
626 output_trunc
->port
!= OFPP_IN_PORT
)
627 return xasprintf("output to unsupported truncate port: %s",
630 } else if (!strcmp(key
, "max_len")) {
633 err
= str_to_u32(value
, &output_trunc
->max_len
);
638 return xasprintf("invalid key '%s' in output_trunc argument",
645 static char * OVS_WARN_UNUSED_RESULT
646 parse_OUTPUT(const char *arg
, const struct ofpact_parse_params
*pp
)
648 if (strstr(arg
, "port") && strstr(arg
, "max_len")) {
649 struct ofpact_output_trunc
*output_trunc
;
651 output_trunc
= ofpact_put_OUTPUT_TRUNC(pp
->ofpacts
);
652 return parse_truncate_subfield(arg
, pp
, output_trunc
);
656 if (ofputil_port_from_string(arg
, pp
->port_map
, &port
)) {
657 struct ofpact_output
*output
= ofpact_put_OUTPUT(pp
->ofpacts
);
659 output
->max_len
= output
->port
== OFPP_CONTROLLER
? UINT16_MAX
: 0;
663 struct mf_subfield src
;
664 char *error
= mf_parse_subfield(&src
, arg
);
666 struct ofpact_output_reg
*output_reg
;
668 output_reg
= ofpact_put_OUTPUT_REG(pp
->ofpacts
);
669 output_reg
->max_len
= UINT16_MAX
;
670 output_reg
->src
= src
;
675 return xasprintf("%s: output to unknown port", arg
);
679 format_OUTPUT(const struct ofpact_output
*a
,
680 const struct ofpact_format_params
*fp
)
682 if (ofp_to_u16(a
->port
) < ofp_to_u16(OFPP_MAX
)) {
683 ds_put_format(fp
->s
, "%soutput:%s", colors
.special
, colors
.end
);
685 ofputil_format_port(a
->port
, fp
->port_map
, fp
->s
);
686 if (a
->port
== OFPP_CONTROLLER
) {
687 ds_put_format(fp
->s
, ":%"PRIu16
, a
->max_len
);
694 decode_OFPAT_RAW_GROUP(uint32_t group_id
,
695 enum ofp_version ofp_version OVS_UNUSED
,
698 ofpact_put_GROUP(out
)->group_id
= group_id
;
703 encode_GROUP(const struct ofpact_group
*group
,
704 enum ofp_version ofp_version
, struct ofpbuf
*out
)
706 put_OFPAT_GROUP(out
, ofp_version
, group
->group_id
);
709 static char * OVS_WARN_UNUSED_RESULT
710 parse_GROUP(char *arg
, const struct ofpact_parse_params
*pp
)
712 return str_to_u32(arg
, &ofpact_put_GROUP(pp
->ofpacts
)->group_id
);
716 format_GROUP(const struct ofpact_group
*a
,
717 const struct ofpact_format_params
*fp
)
719 ds_put_format(fp
->s
, "%sgroup:%s%"PRIu32
,
720 colors
.special
, colors
.end
, a
->group_id
);
723 /* Action structure for NXAST_CONTROLLER.
725 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
726 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
729 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
731 * - 'controller_id': The ID of the controller connection to which the
732 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
733 * sent only to controllers that have the specified controller connection
734 * ID. See "struct nx_controller_id" for more information. */
735 struct nx_action_controller
{
736 ovs_be16 type
; /* OFPAT_VENDOR. */
737 ovs_be16 len
; /* Length is 16. */
738 ovs_be32 vendor
; /* NX_VENDOR_ID. */
739 ovs_be16 subtype
; /* NXAST_CONTROLLER. */
740 ovs_be16 max_len
; /* Maximum length to send to controller. */
741 ovs_be16 controller_id
; /* Controller ID to send packet-in. */
742 uint8_t reason
; /* enum ofp_packet_in_reason (OFPR_*). */
743 uint8_t zero
; /* Must be zero. */
745 OFP_ASSERT(sizeof(struct nx_action_controller
) == 16);
747 /* Properties for NXAST_CONTROLLER2.
749 * For more information on the effect of NXAC2PT_PAUSE, see the large comment
750 * on NXT_PACKET_IN2 in nicira-ext.h */
751 enum nx_action_controller2_prop_type
{
752 NXAC2PT_MAX_LEN
, /* ovs_be16 max bytes to send (default all). */
753 NXAC2PT_CONTROLLER_ID
, /* ovs_be16 dest controller ID (default 0). */
754 NXAC2PT_REASON
, /* uint8_t reason (OFPR_*), default 0. */
755 NXAC2PT_USERDATA
, /* Data to copy into NXPINT_USERDATA. */
756 NXAC2PT_PAUSE
, /* Flag to pause pipeline to resume later. */
759 /* The action structure for NXAST_CONTROLLER2 is "struct ext_action_header",
760 * followed by NXAC2PT_* properties. */
763 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller
*nac
,
764 enum ofp_version ofp_version OVS_UNUSED
,
767 struct ofpact_controller
*oc
;
769 oc
= ofpact_put_CONTROLLER(out
);
770 oc
->ofpact
.raw
= NXAST_RAW_CONTROLLER
;
771 oc
->max_len
= ntohs(nac
->max_len
);
772 oc
->controller_id
= ntohs(nac
->controller_id
);
773 oc
->reason
= nac
->reason
;
774 ofpact_finish_CONTROLLER(out
, &oc
);
780 decode_NXAST_RAW_CONTROLLER2(const struct ext_action_header
*eah
,
781 enum ofp_version ofp_version OVS_UNUSED
,
784 if (!is_all_zeros(eah
->pad
, sizeof eah
->pad
)) {
785 return OFPERR_NXBRC_MUST_BE_ZERO
;
788 size_t start_ofs
= out
->size
;
789 struct ofpact_controller
*oc
= ofpact_put_CONTROLLER(out
);
790 oc
->ofpact
.raw
= NXAST_RAW_CONTROLLER2
;
791 oc
->max_len
= UINT16_MAX
;
792 oc
->reason
= OFPR_ACTION
;
794 struct ofpbuf properties
;
795 ofpbuf_use_const(&properties
, eah
, ntohs(eah
->len
));
796 ofpbuf_pull(&properties
, sizeof *eah
);
798 while (properties
.size
> 0) {
799 struct ofpbuf payload
;
802 enum ofperr error
= ofpprop_pull(&properties
, &payload
, &type
);
808 case NXAC2PT_MAX_LEN
:
809 error
= ofpprop_parse_u16(&payload
, &oc
->max_len
);
812 case NXAC2PT_CONTROLLER_ID
:
813 error
= ofpprop_parse_u16(&payload
, &oc
->controller_id
);
816 case NXAC2PT_REASON
: {
818 error
= ofpprop_parse_u8(&payload
, &u8
);
823 case NXAC2PT_USERDATA
:
824 out
->size
= start_ofs
+ OFPACT_CONTROLLER_SIZE
;
825 ofpbuf_put(out
, payload
.msg
, ofpbuf_msgsize(&payload
));
826 oc
= ofpbuf_at_assert(out
, start_ofs
, sizeof *oc
);
827 oc
->userdata_len
= ofpbuf_msgsize(&payload
);
835 error
= OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type
);
843 ofpact_finish_CONTROLLER(out
, &oc
);
849 encode_CONTROLLER(const struct ofpact_controller
*controller
,
850 enum ofp_version ofp_version OVS_UNUSED
,
853 if (controller
->userdata_len
855 || controller
->ofpact
.raw
== NXAST_RAW_CONTROLLER2
) {
856 size_t start_ofs
= out
->size
;
857 put_NXAST_CONTROLLER2(out
);
858 if (controller
->max_len
!= UINT16_MAX
) {
859 ofpprop_put_u16(out
, NXAC2PT_MAX_LEN
, controller
->max_len
);
861 if (controller
->controller_id
!= 0) {
862 ofpprop_put_u16(out
, NXAC2PT_CONTROLLER_ID
,
863 controller
->controller_id
);
865 if (controller
->reason
!= OFPR_ACTION
) {
866 ofpprop_put_u8(out
, NXAC2PT_REASON
, controller
->reason
);
868 if (controller
->userdata_len
!= 0) {
869 ofpprop_put(out
, NXAC2PT_USERDATA
, controller
->userdata
,
870 controller
->userdata_len
);
872 if (controller
->pause
) {
873 ofpprop_put_flag(out
, NXAC2PT_PAUSE
);
875 pad_ofpat(out
, start_ofs
);
877 struct nx_action_controller
*nac
;
879 nac
= put_NXAST_CONTROLLER(out
);
880 nac
->max_len
= htons(controller
->max_len
);
881 nac
->controller_id
= htons(controller
->controller_id
);
882 nac
->reason
= controller
->reason
;
886 static char * OVS_WARN_UNUSED_RESULT
887 parse_CONTROLLER(char *arg
, const struct ofpact_parse_params
*pp
)
889 enum ofp_packet_in_reason reason
= OFPR_ACTION
;
890 uint16_t controller_id
= 0;
891 uint16_t max_len
= UINT16_MAX
;
892 const char *userdata
= NULL
;
897 } else if (strspn(arg
, "0123456789") == strlen(arg
)) {
898 char *error
= str_to_u16(arg
, "max_len", &max_len
);
905 while (ofputil_parse_key_value(&arg
, &name
, &value
)) {
906 if (!strcmp(name
, "reason")) {
907 if (!ofputil_packet_in_reason_from_string(value
, &reason
)) {
908 return xasprintf("unknown reason \"%s\"", value
);
910 } else if (!strcmp(name
, "max_len")) {
911 char *error
= str_to_u16(value
, "max_len", &max_len
);
915 } else if (!strcmp(name
, "id")) {
916 char *error
= str_to_u16(value
, "id", &controller_id
);
920 } else if (!strcmp(name
, "userdata")) {
922 } else if (!strcmp(name
, "pause")) {
925 return xasprintf("unknown key \"%s\" parsing controller "
931 if (reason
== OFPR_ACTION
&& controller_id
== 0 && !userdata
&& !pause
) {
932 struct ofpact_output
*output
;
934 output
= ofpact_put_OUTPUT(pp
->ofpacts
);
935 output
->port
= OFPP_CONTROLLER
;
936 output
->max_len
= max_len
;
938 struct ofpact_controller
*controller
;
940 controller
= ofpact_put_CONTROLLER(pp
->ofpacts
);
941 controller
->max_len
= max_len
;
942 controller
->reason
= reason
;
943 controller
->controller_id
= controller_id
;
944 controller
->pause
= pause
;
947 size_t start_ofs
= pp
->ofpacts
->size
;
948 const char *end
= ofpbuf_put_hex(pp
->ofpacts
, userdata
, NULL
);
950 return xstrdup("bad hex digit in `controller' "
951 "action `userdata'");
953 size_t userdata_len
= pp
->ofpacts
->size
- start_ofs
;
954 controller
= pp
->ofpacts
->header
;
955 controller
->userdata_len
= userdata_len
;
957 ofpact_finish_CONTROLLER(pp
->ofpacts
, &controller
);
964 format_hex_arg(struct ds
*s
, const uint8_t *data
, size_t len
)
966 for (size_t i
= 0; i
< len
; i
++) {
970 ds_put_format(s
, "%02"PRIx8
, data
[i
]);
975 format_CONTROLLER(const struct ofpact_controller
*a
,
976 const struct ofpact_format_params
*fp
)
978 if (a
->reason
== OFPR_ACTION
&& !a
->controller_id
&& !a
->userdata_len
980 ds_put_format(fp
->s
, "%sCONTROLLER:%s%"PRIu16
,
981 colors
.special
, colors
.end
, a
->max_len
);
983 enum ofp_packet_in_reason reason
= a
->reason
;
985 ds_put_format(fp
->s
, "%scontroller(%s", colors
.paren
, colors
.end
);
986 if (reason
!= OFPR_ACTION
) {
987 char reasonbuf
[OFPUTIL_PACKET_IN_REASON_BUFSIZE
];
989 ds_put_format(fp
->s
, "%sreason=%s%s,", colors
.param
, colors
.end
,
990 ofputil_packet_in_reason_to_string(
991 reason
, reasonbuf
, sizeof reasonbuf
));
993 if (a
->max_len
!= UINT16_MAX
) {
994 ds_put_format(fp
->s
, "%smax_len=%s%"PRIu16
",",
995 colors
.param
, colors
.end
, a
->max_len
);
997 if (a
->controller_id
!= 0) {
998 ds_put_format(fp
->s
, "%sid=%s%"PRIu16
",",
999 colors
.param
, colors
.end
, a
->controller_id
);
1001 if (a
->userdata_len
) {
1002 ds_put_format(fp
->s
, "%suserdata=%s", colors
.param
, colors
.end
);
1003 format_hex_arg(fp
->s
, a
->userdata
, a
->userdata_len
);
1004 ds_put_char(fp
->s
, ',');
1007 ds_put_format(fp
->s
, "%spause%s,", colors
.value
, colors
.end
);
1009 ds_chomp(fp
->s
, ',');
1010 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
1014 /* Enqueue action. */
1015 struct ofp10_action_enqueue
{
1016 ovs_be16 type
; /* OFPAT10_ENQUEUE. */
1017 ovs_be16 len
; /* Len is 16. */
1018 ovs_be16 port
; /* Port that queue belongs. Should
1019 refer to a valid physical port
1020 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
1021 uint8_t pad
[6]; /* Pad for 64-bit alignment. */
1022 ovs_be32 queue_id
; /* Where to enqueue the packets. */
1024 OFP_ASSERT(sizeof(struct ofp10_action_enqueue
) == 16);
1027 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue
*oae
,
1028 enum ofp_version ofp_version OVS_UNUSED
,
1031 struct ofpact_enqueue
*enqueue
;
1033 enqueue
= ofpact_put_ENQUEUE(out
);
1034 enqueue
->port
= u16_to_ofp(ntohs(oae
->port
));
1035 enqueue
->queue
= ntohl(oae
->queue_id
);
1036 if (ofp_to_u16(enqueue
->port
) >= ofp_to_u16(OFPP_MAX
)
1037 && enqueue
->port
!= OFPP_IN_PORT
1038 && enqueue
->port
!= OFPP_LOCAL
) {
1039 return OFPERR_OFPBAC_BAD_OUT_PORT
;
1045 encode_ENQUEUE(const struct ofpact_enqueue
*enqueue
,
1046 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1048 if (ofp_version
== OFP10_VERSION
) {
1049 struct ofp10_action_enqueue
*oae
;
1051 oae
= put_OFPAT10_ENQUEUE(out
);
1052 oae
->port
= htons(ofp_to_u16(enqueue
->port
));
1053 oae
->queue_id
= htonl(enqueue
->queue
);
1055 put_OFPAT_SET_QUEUE(out
, ofp_version
, enqueue
->queue
);
1057 struct ofp11_action_output
*oao
= put_OFPAT11_OUTPUT(out
);
1058 oao
->port
= ofputil_port_to_ofp11(enqueue
->port
);
1059 oao
->max_len
= OVS_BE16_MAX
;
1061 put_NXAST_POP_QUEUE(out
);
1065 static char * OVS_WARN_UNUSED_RESULT
1066 parse_ENQUEUE(char *arg
, const struct ofpact_parse_params
*pp
)
1069 char *port
= strtok_r(arg
, ":q,", &sp
);
1070 char *queue
= strtok_r(NULL
, "", &sp
);
1071 struct ofpact_enqueue
*enqueue
;
1073 if (port
== NULL
|| queue
== NULL
) {
1074 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
1075 "\"enqueue(PORT,QUEUE)\"");
1078 enqueue
= ofpact_put_ENQUEUE(pp
->ofpacts
);
1079 if (!ofputil_port_from_string(port
, pp
->port_map
, &enqueue
->port
)) {
1080 return xasprintf("%s: enqueue to unknown port", port
);
1082 return str_to_u32(queue
, &enqueue
->queue
);
1086 format_ENQUEUE(const struct ofpact_enqueue
*a
,
1087 const struct ofpact_format_params
*fp
)
1089 ds_put_format(fp
->s
, "%senqueue:%s", colors
.param
, colors
.end
);
1090 ofputil_format_port(a
->port
, fp
->port_map
, fp
->s
);
1091 ds_put_format(fp
->s
, ":%"PRIu32
, a
->queue
);
1094 /* Action structure for NXAST_OUTPUT_REG.
1096 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
1098 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
1099 * the NXAST_REG_LOAD action.
1101 * The acceptable nxm_header values for 'src' are the same as the acceptable
1102 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
1104 * The 'max_len' field indicates the number of bytes to send when the chosen
1105 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
1106 * field of OFPAT_OUTPUT.
1108 * The 'zero' field is required to be zeroed for forward compatibility. */
1109 struct nx_action_output_reg
{
1110 ovs_be16 type
; /* OFPAT_VENDOR. */
1111 ovs_be16 len
; /* 24. */
1112 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1113 ovs_be16 subtype
; /* NXAST_OUTPUT_REG. */
1115 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1116 ovs_be32 src
; /* Source. */
1118 ovs_be16 max_len
; /* Max length to send to controller. */
1120 uint8_t zero
[6]; /* Reserved, must be zero. */
1122 OFP_ASSERT(sizeof(struct nx_action_output_reg
) == 24);
1124 /* Action structure for NXAST_OUTPUT_REG2.
1126 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
1127 * experimenter OXM as 'src'.
1129 struct nx_action_output_reg2
{
1130 ovs_be16 type
; /* OFPAT_VENDOR. */
1131 ovs_be16 len
; /* 24. */
1132 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1133 ovs_be16 subtype
; /* NXAST_OUTPUT_REG2. */
1135 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1136 ovs_be16 max_len
; /* Max length to send to controller. */
1139 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1140 * - Enough 0-bytes to pad the action out to 24 bytes. */
1143 OFP_ASSERT(sizeof(struct nx_action_output_reg2
) == 24);
1146 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg
*naor
,
1147 enum ofp_version ofp_version OVS_UNUSED
,
1148 const struct vl_mff_map
*vl_mff_map
,
1149 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
1151 struct ofpact_output_reg
*output_reg
;
1154 if (!is_all_zeros(naor
->zero
, sizeof naor
->zero
)) {
1155 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1158 output_reg
= ofpact_put_OUTPUT_REG(out
);
1159 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG
;
1160 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
1161 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
1162 output_reg
->max_len
= ntohs(naor
->max_len
);
1163 error
= mf_vl_mff_mf_from_nxm_header(ntohl(naor
->src
), vl_mff_map
,
1164 &output_reg
->src
.field
, tlv_bitmap
);
1169 return mf_check_src(&output_reg
->src
, NULL
);
1173 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2
*naor
,
1174 enum ofp_version ofp_version OVS_UNUSED
,
1175 const struct vl_mff_map
*vl_mff_map
,
1176 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
1178 struct ofpact_output_reg
*output_reg
;
1181 output_reg
= ofpact_put_OUTPUT_REG(out
);
1182 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG2
;
1183 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
1184 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
1185 output_reg
->max_len
= ntohs(naor
->max_len
);
1187 struct ofpbuf b
= ofpbuf_const_initializer(naor
, ntohs(naor
->len
));
1188 ofpbuf_pull(&b
, OBJECT_OFFSETOF(naor
, pad
));
1190 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &output_reg
->src
.field
,
1196 if (!is_all_zeros(b
.data
, b
.size
)) {
1197 return OFPERR_NXBRC_MUST_BE_ZERO
;
1200 return mf_check_src(&output_reg
->src
, NULL
);
1204 encode_OUTPUT_REG(const struct ofpact_output_reg
*output_reg
,
1205 enum ofp_version ofp_version OVS_UNUSED
,
1208 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1209 * cannot be encoded in the older form, encode it as
1210 * NXAST_RAW_OUTPUT_REG2. */
1211 if (output_reg
->ofpact
.raw
== NXAST_RAW_OUTPUT_REG2
1212 || !mf_nxm_header(output_reg
->src
.field
->id
)) {
1213 struct nx_action_output_reg2
*naor
= put_NXAST_OUTPUT_REG2(out
);
1214 size_t size
= out
->size
;
1216 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
1217 output_reg
->src
.n_bits
);
1218 naor
->max_len
= htons(output_reg
->max_len
);
1220 out
->size
= size
- sizeof naor
->pad
;
1221 nx_put_mff_header(out
, output_reg
->src
.field
, 0, false);
1224 struct nx_action_output_reg
*naor
= put_NXAST_OUTPUT_REG(out
);
1226 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
1227 output_reg
->src
.n_bits
);
1228 naor
->src
= htonl(nxm_header_from_mff(output_reg
->src
.field
));
1229 naor
->max_len
= htons(output_reg
->max_len
);
1233 static char * OVS_WARN_UNUSED_RESULT
1234 parse_OUTPUT_REG(const char *arg
, const struct ofpact_parse_params
*pp
)
1236 return parse_OUTPUT(arg
, pp
);
1240 format_OUTPUT_REG(const struct ofpact_output_reg
*a
,
1241 const struct ofpact_format_params
*fp
)
1243 ds_put_format(fp
->s
, "%soutput:%s", colors
.special
, colors
.end
);
1244 mf_format_subfield(&a
->src
, fp
->s
);
1247 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1249 * The bundle actions choose a slave from a supplied list of options.
1250 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
1251 * selection to a register.
1253 * The list of possible slaves follows the nx_action_bundle structure. The size
1254 * of each slave is governed by its type as indicated by the 'slave_type'
1255 * parameter. The list of slaves should be padded at its end with zeros to make
1256 * the total length of the action a multiple of 8.
1258 * Switches infer from the 'slave_type' parameter the size of each slave. All
1259 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
1260 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
1261 * 2 byte width. Switches should reject actions which indicate unknown or
1262 * unsupported slave types.
1264 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1265 * slave. If the switch does not support the specified 'algorithm' parameter,
1266 * it should reject the action.
1268 * Several algorithms take into account liveness when selecting slaves. The
1269 * liveness of a slave is implementation defined (with one exception), but will
1270 * generally take into account things like its carrier status and the results
1271 * of any link monitoring protocols which happen to be running on it. In order
1272 * to give controllers a place-holder value, the OFPP_NONE port is always
1273 * considered live, that is, NXAST_BUNDLE_LOAD stores OFPP_NONE in the output
1274 * register if no slave is live.
1276 * Some slave selection strategies require the use of a hash function, in which
1277 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
1278 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
1279 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
1280 * 'basis' parameter is used as a universal hash parameter. Different values
1281 * of 'basis' yield different hash results.
1283 * The 'zero' parameter at the end of the action structure is reserved for
1284 * future use. Switches are required to reject actions which have nonzero
1285 * bytes in the 'zero' field.
1287 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
1288 * should reject actions which have nonzero bytes in either of these fields.
1290 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
1291 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
1292 * similar to those for the NXAST_REG_LOAD action. */
1293 struct nx_action_bundle
{
1294 ovs_be16 type
; /* OFPAT_VENDOR. */
1295 ovs_be16 len
; /* Length including slaves. */
1296 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1297 ovs_be16 subtype
; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1299 /* Slave choice algorithm to apply to hash value. */
1300 ovs_be16 algorithm
; /* One of NX_BD_ALG_*. */
1302 /* What fields to hash and how. */
1303 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
1304 ovs_be16 basis
; /* Universal hash parameter. */
1306 ovs_be32 slave_type
; /* NXM_OF_IN_PORT. */
1307 ovs_be16 n_slaves
; /* Number of slaves. */
1309 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1310 ovs_be32 dst
; /* Destination. */
1312 uint8_t zero
[4]; /* Reserved. Must be zero. */
1314 OFP_ASSERT(sizeof(struct nx_action_bundle
) == 32);
1317 decode_bundle(bool load
, const struct nx_action_bundle
*nab
,
1318 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
1319 struct ofpbuf
*ofpacts
)
1321 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 5);
1322 struct ofpact_bundle
*bundle
;
1323 uint32_t slave_type
;
1324 size_t slaves_size
, i
;
1327 bundle
= ofpact_put_BUNDLE(ofpacts
);
1329 bundle
->n_slaves
= ntohs(nab
->n_slaves
);
1330 bundle
->basis
= ntohs(nab
->basis
);
1331 bundle
->fields
= ntohs(nab
->fields
);
1332 bundle
->algorithm
= ntohs(nab
->algorithm
);
1333 slave_type
= ntohl(nab
->slave_type
);
1334 slaves_size
= ntohs(nab
->len
) - sizeof *nab
;
1336 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1337 if (!flow_hash_fields_valid(bundle
->fields
)) {
1338 VLOG_WARN_RL(&rll
, "unsupported fields %d", (int) bundle
->fields
);
1339 } else if (bundle
->n_slaves
> BUNDLE_MAX_SLAVES
) {
1340 VLOG_WARN_RL(&rll
, "too many slaves");
1341 } else if (bundle
->algorithm
!= NX_BD_ALG_HRW
1342 && bundle
->algorithm
!= NX_BD_ALG_ACTIVE_BACKUP
) {
1343 VLOG_WARN_RL(&rll
, "unsupported algorithm %d", (int) bundle
->algorithm
);
1344 } else if (slave_type
!= mf_nxm_header(MFF_IN_PORT
)) {
1345 VLOG_WARN_RL(&rll
, "unsupported slave type %"PRIu32
, slave_type
);
1350 if (!is_all_zeros(nab
->zero
, sizeof nab
->zero
)) {
1351 VLOG_WARN_RL(&rll
, "reserved field is nonzero");
1352 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1356 bundle
->dst
.ofs
= nxm_decode_ofs(nab
->ofs_nbits
);
1357 bundle
->dst
.n_bits
= nxm_decode_n_bits(nab
->ofs_nbits
);
1358 error
= mf_vl_mff_mf_from_nxm_header(ntohl(nab
->dst
), vl_mff_map
,
1359 &bundle
->dst
.field
, tlv_bitmap
);
1364 if (bundle
->dst
.n_bits
< 16) {
1365 VLOG_WARN_RL(&rll
, "bundle_load action requires at least 16 bit "
1367 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1370 if (nab
->ofs_nbits
|| nab
->dst
) {
1371 VLOG_WARN_RL(&rll
, "bundle action has nonzero reserved fields");
1372 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1376 if (slaves_size
< bundle
->n_slaves
* sizeof(ovs_be16
)) {
1377 VLOG_WARN_RL(&rll
, "Nicira action %s only has %"PRIuSIZE
" bytes "
1378 "allocated for slaves. %"PRIuSIZE
" bytes are required "
1380 load
? "bundle_load" : "bundle", slaves_size
,
1381 bundle
->n_slaves
* sizeof(ovs_be16
), bundle
->n_slaves
);
1382 error
= OFPERR_OFPBAC_BAD_LEN
;
1385 for (i
= 0; i
< bundle
->n_slaves
; i
++) {
1386 ofp_port_t ofp_port
= u16_to_ofp(ntohs(((ovs_be16
*)(nab
+ 1))[i
]));
1387 ofpbuf_put(ofpacts
, &ofp_port
, sizeof ofp_port
);
1388 bundle
= ofpacts
->header
;
1391 ofpact_finish_BUNDLE(ofpacts
, &bundle
);
1393 error
= bundle_check(bundle
, OFPP_MAX
, NULL
);
1399 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle
*nab
,
1400 enum ofp_version ofp_version OVS_UNUSED
,
1403 return decode_bundle(false, nab
, NULL
, NULL
, out
);
1407 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle
*nab
,
1408 enum ofp_version ofp_version OVS_UNUSED
,
1409 const struct vl_mff_map
*vl_mff_map
,
1410 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
1412 return decode_bundle(true, nab
, vl_mff_map
, tlv_bitmap
, out
);
1416 encode_BUNDLE(const struct ofpact_bundle
*bundle
,
1417 enum ofp_version ofp_version OVS_UNUSED
,
1420 int slaves_len
= ROUND_UP(2 * bundle
->n_slaves
, OFP_ACTION_ALIGN
);
1421 struct nx_action_bundle
*nab
;
1425 nab
= (bundle
->dst
.field
1426 ? put_NXAST_BUNDLE_LOAD(out
)
1427 : put_NXAST_BUNDLE(out
));
1428 nab
->len
= htons(ntohs(nab
->len
) + slaves_len
);
1429 nab
->algorithm
= htons(bundle
->algorithm
);
1430 nab
->fields
= htons(bundle
->fields
);
1431 nab
->basis
= htons(bundle
->basis
);
1432 nab
->slave_type
= htonl(mf_nxm_header(MFF_IN_PORT
));
1433 nab
->n_slaves
= htons(bundle
->n_slaves
);
1434 if (bundle
->dst
.field
) {
1435 nab
->ofs_nbits
= nxm_encode_ofs_nbits(bundle
->dst
.ofs
,
1436 bundle
->dst
.n_bits
);
1437 nab
->dst
= htonl(nxm_header_from_mff(bundle
->dst
.field
));
1440 slaves
= ofpbuf_put_zeros(out
, slaves_len
);
1441 for (i
= 0; i
< bundle
->n_slaves
; i
++) {
1442 slaves
[i
] = htons(ofp_to_u16(bundle
->slaves
[i
]));
1446 static char * OVS_WARN_UNUSED_RESULT
1447 parse_BUNDLE(const char *arg
, const struct ofpact_parse_params
*pp
)
1449 return bundle_parse(arg
, pp
->port_map
, pp
->ofpacts
);
1452 static char * OVS_WARN_UNUSED_RESULT
1453 parse_bundle_load(const char *arg
, const struct ofpact_parse_params
*pp
)
1455 return bundle_parse_load(arg
, pp
->port_map
, pp
->ofpacts
);
1459 format_BUNDLE(const struct ofpact_bundle
*a
,
1460 const struct ofpact_format_params
*fp
)
1462 bundle_format(a
, fp
->port_map
, fp
->s
);
1465 /* Set VLAN actions. */
1468 decode_set_vlan_vid(uint16_t vid
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1471 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1473 struct ofpact_vlan_vid
*vlan_vid
= ofpact_put_SET_VLAN_VID(out
);
1474 vlan_vid
->vlan_vid
= vid
;
1475 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1481 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid
,
1482 enum ofp_version ofp_version OVS_UNUSED
,
1485 return decode_set_vlan_vid(vid
, true, out
);
1489 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid
,
1490 enum ofp_version ofp_version OVS_UNUSED
,
1493 return decode_set_vlan_vid(vid
, false, out
);
1497 encode_SET_VLAN_VID(const struct ofpact_vlan_vid
*vlan_vid
,
1498 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1500 uint16_t vid
= vlan_vid
->vlan_vid
;
1502 /* Push a VLAN tag, if none is present and this form of the action calls
1503 * for such a feature. */
1504 if (ofp_version
> OFP10_VERSION
1505 && vlan_vid
->push_vlan_if_needed
1506 && !vlan_vid
->flow_has_vlan
) {
1507 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1510 if (ofp_version
== OFP10_VERSION
) {
1511 put_OFPAT10_SET_VLAN_VID(out
, vid
);
1512 } else if (ofp_version
== OFP11_VERSION
) {
1513 put_OFPAT11_SET_VLAN_VID(out
, vid
);
1515 put_set_field(out
, ofp_version
, MFF_VLAN_VID
, vid
| OFPVID12_PRESENT
);
1519 static char * OVS_WARN_UNUSED_RESULT
1520 parse_set_vlan_vid(char *arg
, bool push_vlan_if_needed
,
1521 const struct ofpact_parse_params
*pp
)
1523 struct ofpact_vlan_vid
*vlan_vid
;
1527 error
= str_to_u16(arg
, "VLAN VID", &vid
);
1532 if (vid
& ~VLAN_VID_MASK
) {
1533 return xasprintf("%s: not a valid VLAN VID", arg
);
1535 vlan_vid
= ofpact_put_SET_VLAN_VID(pp
->ofpacts
);
1536 vlan_vid
->vlan_vid
= vid
;
1537 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1541 static char * OVS_WARN_UNUSED_RESULT
1542 parse_SET_VLAN_VID(char *arg
, const struct ofpact_parse_params
*pp
)
1544 return parse_set_vlan_vid(arg
, false, pp
);
1548 format_SET_VLAN_VID(const struct ofpact_vlan_vid
*a
,
1549 const struct ofpact_format_params
*fp
)
1551 ds_put_format(fp
->s
, "%s%s:%s%"PRIu16
, colors
.param
,
1552 a
->push_vlan_if_needed
? "mod_vlan_vid" : "set_vlan_vid",
1553 colors
.end
, a
->vlan_vid
);
1556 /* Set PCP actions. */
1559 decode_set_vlan_pcp(uint8_t pcp
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1562 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1564 struct ofpact_vlan_pcp
*vlan_pcp
= ofpact_put_SET_VLAN_PCP(out
);
1565 vlan_pcp
->vlan_pcp
= pcp
;
1566 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1572 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp
,
1573 enum ofp_version ofp_version OVS_UNUSED
,
1576 return decode_set_vlan_pcp(pcp
, true, out
);
1580 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp
,
1581 enum ofp_version ofp_version OVS_UNUSED
,
1584 return decode_set_vlan_pcp(pcp
, false, out
);
1588 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*vlan_pcp
,
1589 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1591 uint8_t pcp
= vlan_pcp
->vlan_pcp
;
1593 /* Push a VLAN tag, if none is present and this form of the action calls
1594 * for such a feature. */
1595 if (ofp_version
> OFP10_VERSION
1596 && vlan_pcp
->push_vlan_if_needed
1597 && !vlan_pcp
->flow_has_vlan
) {
1598 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1601 if (ofp_version
== OFP10_VERSION
) {
1602 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
1603 } else if (ofp_version
== OFP11_VERSION
) {
1604 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
1606 put_set_field(out
, ofp_version
, MFF_VLAN_PCP
, pcp
);
1610 static char * OVS_WARN_UNUSED_RESULT
1611 parse_set_vlan_pcp(char *arg
, bool push_vlan_if_needed
,
1612 const struct ofpact_parse_params
*pp
)
1614 struct ofpact_vlan_pcp
*vlan_pcp
;
1618 error
= str_to_u8(arg
, "VLAN PCP", &pcp
);
1624 return xasprintf("%s: not a valid VLAN PCP", arg
);
1626 vlan_pcp
= ofpact_put_SET_VLAN_PCP(pp
->ofpacts
);
1627 vlan_pcp
->vlan_pcp
= pcp
;
1628 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1632 static char * OVS_WARN_UNUSED_RESULT
1633 parse_SET_VLAN_PCP(char *arg
, const struct ofpact_parse_params
*pp
)
1635 return parse_set_vlan_pcp(arg
, false, pp
);
1639 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*a
,
1640 const struct ofpact_format_params
*fp
)
1642 ds_put_format(fp
->s
, "%s%s:%s%"PRIu8
, colors
.param
,
1643 a
->push_vlan_if_needed
? "mod_vlan_pcp" : "set_vlan_pcp",
1644 colors
.end
, a
->vlan_pcp
);
1647 /* Strip VLAN actions. */
1650 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf
*out
)
1652 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1657 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf
*out
)
1659 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1664 encode_STRIP_VLAN(const struct ofpact_null
*null OVS_UNUSED
,
1665 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1667 if (ofp_version
== OFP10_VERSION
) {
1668 put_OFPAT10_STRIP_VLAN(out
);
1670 put_OFPAT11_POP_VLAN(out
);
1674 static char * OVS_WARN_UNUSED_RESULT
1675 parse_STRIP_VLAN(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
1677 ofpact_put_STRIP_VLAN(pp
->ofpacts
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1681 static char * OVS_WARN_UNUSED_RESULT
1682 parse_pop_vlan(const struct ofpact_parse_params
*pp
)
1684 ofpact_put_STRIP_VLAN(pp
->ofpacts
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1689 format_STRIP_VLAN(const struct ofpact_null
*a
,
1690 const struct ofpact_format_params
*fp
)
1692 ds_put_format(fp
->s
, (a
->ofpact
.raw
== OFPAT_RAW11_POP_VLAN
1694 : "%sstrip_vlan%s"),
1695 colors
.value
, colors
.end
);
1698 /* Push VLAN action. */
1701 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type
,
1702 enum ofp_version ofp_version OVS_UNUSED
,
1705 struct ofpact_push_vlan
*push_vlan
;
1706 if (!eth_type_vlan(eth_type
)) {
1707 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1709 push_vlan
= ofpact_put_PUSH_VLAN(out
);
1710 push_vlan
->ethertype
= eth_type
;
1715 encode_PUSH_VLAN(const struct ofpact_push_vlan
*push_vlan
,
1716 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1718 if (ofp_version
== OFP10_VERSION
) {
1719 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1720 * follow this action. */
1722 put_OFPAT11_PUSH_VLAN(out
, push_vlan
->ethertype
);
1726 static char * OVS_WARN_UNUSED_RESULT
1727 parse_PUSH_VLAN(char *arg
, const struct ofpact_parse_params
*pp
)
1729 struct ofpact_push_vlan
*push_vlan
;
1733 *pp
->usable_protocols
&= OFPUTIL_P_OF11_UP
;
1734 error
= str_to_u16(arg
, "ethertype", ðertype
);
1739 if (!eth_type_vlan(htons(ethertype
))) {
1740 return xasprintf("%s: not a valid VLAN ethertype", arg
);
1742 push_vlan
= ofpact_put_PUSH_VLAN(pp
->ofpacts
);
1743 push_vlan
->ethertype
= htons(ethertype
);
1748 format_PUSH_VLAN(const struct ofpact_push_vlan
*push_vlan
,
1749 const struct ofpact_format_params
*fp
)
1751 ds_put_format(fp
->s
, "%spush_vlan:%s%#"PRIx16
,
1752 colors
.param
, colors
.end
, ntohs(push_vlan
->ethertype
));
1755 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1756 struct ofp_action_dl_addr
{
1757 ovs_be16 type
; /* Type. */
1758 ovs_be16 len
; /* Length is 16. */
1759 struct eth_addr dl_addr
; /* Ethernet address. */
1762 OFP_ASSERT(sizeof(struct ofp_action_dl_addr
) == 16);
1765 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr
*a
,
1766 enum ofp_version ofp_version OVS_UNUSED
,
1769 ofpact_put_SET_ETH_SRC(out
)->mac
= a
->dl_addr
;
1774 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr
*a
,
1775 enum ofp_version ofp_version OVS_UNUSED
,
1778 ofpact_put_SET_ETH_DST(out
)->mac
= a
->dl_addr
;
1783 encode_SET_ETH_addr(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1784 enum ofp_raw_action_type raw
, enum mf_field_id field
,
1787 if (ofp_version
< OFP12_VERSION
) {
1788 struct ofp_action_dl_addr
*oada
;
1790 oada
= ofpact_put_raw(out
, ofp_version
, raw
, 0);
1791 oada
->dl_addr
= mac
->mac
;
1793 put_set_field(out
, ofp_version
, field
, eth_addr_to_uint64(mac
->mac
));
1798 encode_SET_ETH_SRC(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1801 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_SRC
, MFF_ETH_SRC
,
1807 encode_SET_ETH_DST(const struct ofpact_mac
*mac
,
1808 enum ofp_version ofp_version
,
1811 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_DST
, MFF_ETH_DST
,
1816 static char * OVS_WARN_UNUSED_RESULT
1817 parse_SET_ETH_SRC(char *arg
, const struct ofpact_parse_params
*pp
)
1819 return str_to_mac(arg
, &ofpact_put_SET_ETH_SRC(pp
->ofpacts
)->mac
);
1822 static char * OVS_WARN_UNUSED_RESULT
1823 parse_SET_ETH_DST(char *arg
, const struct ofpact_parse_params
*pp
)
1825 return str_to_mac(arg
, &ofpact_put_SET_ETH_DST(pp
->ofpacts
)->mac
);
1829 format_SET_ETH_SRC(const struct ofpact_mac
*a
,
1830 const struct ofpact_format_params
*fp
)
1832 ds_put_format(fp
->s
, "%smod_dl_src:%s"ETH_ADDR_FMT
,
1833 colors
.param
, colors
.end
, ETH_ADDR_ARGS(a
->mac
));
1837 format_SET_ETH_DST(const struct ofpact_mac
*a
,
1838 const struct ofpact_format_params
*fp
)
1840 ds_put_format(fp
->s
, "%smod_dl_dst:%s"ETH_ADDR_FMT
,
1841 colors
.param
, colors
.end
, ETH_ADDR_ARGS(a
->mac
));
1844 /* Set IPv4 address actions. */
1847 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4
,
1848 enum ofp_version ofp_version OVS_UNUSED
,
1851 ofpact_put_SET_IPV4_SRC(out
)->ipv4
= ipv4
;
1856 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4
,
1857 enum ofp_version ofp_version OVS_UNUSED
,
1860 ofpact_put_SET_IPV4_DST(out
)->ipv4
= ipv4
;
1865 encode_SET_IPV4_addr(const struct ofpact_ipv4
*ipv4
,
1866 enum ofp_version ofp_version
,
1867 enum ofp_raw_action_type raw
, enum mf_field_id field
,
1870 ovs_be32 addr
= ipv4
->ipv4
;
1871 if (ofp_version
< OFP12_VERSION
) {
1872 ofpact_put_raw(out
, ofp_version
, raw
, ntohl(addr
));
1874 put_set_field(out
, ofp_version
, field
, ntohl(addr
));
1879 encode_SET_IPV4_SRC(const struct ofpact_ipv4
*ipv4
,
1880 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1882 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_SRC
, MFF_IPV4_SRC
,
1887 encode_SET_IPV4_DST(const struct ofpact_ipv4
*ipv4
,
1888 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1890 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_DST
, MFF_IPV4_DST
,
1894 static char * OVS_WARN_UNUSED_RESULT
1895 parse_SET_IPV4_SRC(char *arg
, const struct ofpact_parse_params
*pp
)
1897 return str_to_ip(arg
, &ofpact_put_SET_IPV4_SRC(pp
->ofpacts
)->ipv4
);
1900 static char * OVS_WARN_UNUSED_RESULT
1901 parse_SET_IPV4_DST(char *arg
, const struct ofpact_parse_params
*pp
)
1903 return str_to_ip(arg
, &ofpact_put_SET_IPV4_DST(pp
->ofpacts
)->ipv4
);
1907 format_SET_IPV4_SRC(const struct ofpact_ipv4
*a
,
1908 const struct ofpact_format_params
*fp
)
1910 ds_put_format(fp
->s
, "%smod_nw_src:%s"IP_FMT
,
1911 colors
.param
, colors
.end
, IP_ARGS(a
->ipv4
));
1915 format_SET_IPV4_DST(const struct ofpact_ipv4
*a
,
1916 const struct ofpact_format_params
*fp
)
1918 ds_put_format(fp
->s
, "%smod_nw_dst:%s"IP_FMT
,
1919 colors
.param
, colors
.end
, IP_ARGS(a
->ipv4
));
1922 /* Set IPv4/v6 TOS actions. */
1925 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp
,
1926 enum ofp_version ofp_version OVS_UNUSED
,
1929 if (dscp
& ~IP_DSCP_MASK
) {
1930 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1932 ofpact_put_SET_IP_DSCP(out
)->dscp
= dscp
;
1938 encode_SET_IP_DSCP(const struct ofpact_dscp
*dscp
,
1939 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1941 if (ofp_version
< OFP12_VERSION
) {
1942 put_OFPAT_SET_NW_TOS(out
, ofp_version
, dscp
->dscp
);
1944 put_set_field(out
, ofp_version
, MFF_IP_DSCP_SHIFTED
, dscp
->dscp
>> 2);
1948 static char * OVS_WARN_UNUSED_RESULT
1949 parse_SET_IP_DSCP(char *arg
, const struct ofpact_parse_params
*pp
)
1955 error
= str_to_u8(arg
, "TOS", &tos
);
1960 if (tos
& ~IP_DSCP_MASK
) {
1961 return xasprintf("%s: not a valid TOS", arg
);
1963 ofpact_put_SET_IP_DSCP(pp
->ofpacts
)->dscp
= tos
;
1968 format_SET_IP_DSCP(const struct ofpact_dscp
*a
,
1969 const struct ofpact_format_params
*fp
)
1971 ds_put_format(fp
->s
, "%smod_nw_tos:%s%d",
1972 colors
.param
, colors
.end
, a
->dscp
);
1975 /* Set IPv4/v6 ECN actions. */
1978 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn
,
1979 enum ofp_version ofp_version OVS_UNUSED
,
1982 if (ecn
& ~IP_ECN_MASK
) {
1983 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1985 ofpact_put_SET_IP_ECN(out
)->ecn
= ecn
;
1991 encode_SET_IP_ECN(const struct ofpact_ecn
*ip_ecn
,
1992 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1994 uint8_t ecn
= ip_ecn
->ecn
;
1995 if (ofp_version
== OFP10_VERSION
) {
1996 struct mf_subfield dst
= { .field
= mf_from_id(MFF_IP_ECN
),
1997 .ofs
= 0, .n_bits
= 2 };
1998 put_reg_load(out
, &dst
, ecn
);
1999 } else if (ofp_version
== OFP11_VERSION
) {
2000 put_OFPAT11_SET_NW_ECN(out
, ecn
);
2002 put_set_field(out
, ofp_version
, MFF_IP_ECN
, ecn
);
2006 static char * OVS_WARN_UNUSED_RESULT
2007 parse_SET_IP_ECN(char *arg
, const struct ofpact_parse_params
*pp
)
2012 error
= str_to_u8(arg
, "ECN", &ecn
);
2017 if (ecn
& ~IP_ECN_MASK
) {
2018 return xasprintf("%s: not a valid ECN", arg
);
2020 ofpact_put_SET_IP_ECN(pp
->ofpacts
)->ecn
= ecn
;
2025 format_SET_IP_ECN(const struct ofpact_ecn
*a
,
2026 const struct ofpact_format_params
*fp
)
2028 ds_put_format(fp
->s
, "%smod_nw_ecn:%s%d",
2029 colors
.param
, colors
.end
, a
->ecn
);
2032 /* Set IPv4/v6 TTL actions. */
2035 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl
,
2036 enum ofp_version ofp_version OVS_UNUSED
,
2039 ofpact_put_SET_IP_TTL(out
)->ttl
= ttl
;
2044 encode_SET_IP_TTL(const struct ofpact_ip_ttl
*ttl
,
2045 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2047 if (ofp_version
>= OFP11_VERSION
) {
2048 put_OFPAT11_SET_NW_TTL(out
, ttl
->ttl
);
2050 struct mf_subfield dst
= { .field
= mf_from_id(MFF_IP_TTL
),
2051 .ofs
= 0, .n_bits
= 8 };
2052 put_reg_load(out
, &dst
, ttl
->ttl
);
2056 static char * OVS_WARN_UNUSED_RESULT
2057 parse_SET_IP_TTL(char *arg
, const struct ofpact_parse_params
*pp
)
2063 error
= str_to_u8(arg
, "TTL", &ttl
);
2068 ofpact_put_SET_IP_TTL(pp
->ofpacts
)->ttl
= ttl
;
2073 format_SET_IP_TTL(const struct ofpact_ip_ttl
*a
,
2074 const struct ofpact_format_params
*fp
)
2076 ds_put_format(fp
->s
, "%smod_nw_ttl:%s%d",
2077 colors
.param
, colors
.end
, a
->ttl
);
2080 /* Set TCP/UDP/SCTP port actions. */
2083 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port
,
2084 enum ofp_version ofp_version OVS_UNUSED
,
2087 ofpact_put_SET_L4_SRC_PORT(out
)->port
= ntohs(port
);
2092 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port
,
2093 enum ofp_version ofp_version OVS_UNUSED
,
2096 ofpact_put_SET_L4_DST_PORT(out
)->port
= ntohs(port
);
2101 encode_SET_L4_port(const struct ofpact_l4_port
*l4_port
,
2102 enum ofp_version ofp_version
, enum ofp_raw_action_type raw
,
2103 enum mf_field_id field
, struct ofpbuf
*out
)
2105 uint16_t port
= l4_port
->port
;
2107 if (ofp_version
>= OFP12_VERSION
&& field
!= MFF_N_IDS
) {
2108 put_set_field(out
, ofp_version
, field
, port
);
2110 ofpact_put_raw(out
, ofp_version
, raw
, port
);
2115 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port
*l4_port
,
2116 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2118 uint8_t proto
= l4_port
->flow_ip_proto
;
2119 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_SRC
2120 : proto
== IPPROTO_UDP
? MFF_UDP_SRC
2121 : proto
== IPPROTO_SCTP
? MFF_SCTP_SRC
2124 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_SRC
, field
, out
);
2128 encode_SET_L4_DST_PORT(const struct ofpact_l4_port
*l4_port
,
2129 enum ofp_version ofp_version
,
2132 uint8_t proto
= l4_port
->flow_ip_proto
;
2133 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_DST
2134 : proto
== IPPROTO_UDP
? MFF_UDP_DST
2135 : proto
== IPPROTO_SCTP
? MFF_SCTP_DST
2138 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_DST
, field
, out
);
2141 static char * OVS_WARN_UNUSED_RESULT
2142 parse_SET_L4_SRC_PORT(char *arg
, const struct ofpact_parse_params
*pp
)
2144 return str_to_u16(arg
, "source port",
2145 &ofpact_put_SET_L4_SRC_PORT(pp
->ofpacts
)->port
);
2148 static char * OVS_WARN_UNUSED_RESULT
2149 parse_SET_L4_DST_PORT(char *arg
, const struct ofpact_parse_params
*pp
)
2151 return str_to_u16(arg
, "destination port",
2152 &ofpact_put_SET_L4_DST_PORT(pp
->ofpacts
)->port
);
2156 format_SET_L4_SRC_PORT(const struct ofpact_l4_port
*a
,
2157 const struct ofpact_format_params
*fp
)
2159 ds_put_format(fp
->s
, "%smod_tp_src:%s%d",
2160 colors
.param
, colors
.end
, a
->port
);
2164 format_SET_L4_DST_PORT(const struct ofpact_l4_port
*a
,
2165 const struct ofpact_format_params
*fp
)
2167 ds_put_format(fp
->s
, "%smod_tp_dst:%s%d",
2168 colors
.param
, colors
.end
, a
->port
);
2171 /* Action structure for OFPAT_COPY_FIELD. */
2172 struct ofp15_action_copy_field
{
2173 ovs_be16 type
; /* OFPAT_COPY_FIELD. */
2174 ovs_be16 len
; /* Length is padded to 64 bits. */
2175 ovs_be16 n_bits
; /* Number of bits to copy. */
2176 ovs_be16 src_offset
; /* Starting bit offset in source. */
2177 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
2180 * - OXM header for source field.
2181 * - OXM header for destination field.
2182 * - Padding with 0-bytes to a multiple of 8 bytes.
2183 * The "pad2" member is the beginning of the above. */
2186 OFP_ASSERT(sizeof(struct ofp15_action_copy_field
) == 16);
2188 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
2189 struct onf_action_copy_field
{
2190 ovs_be16 type
; /* OFPAT_EXPERIMENTER. */
2191 ovs_be16 len
; /* Length is padded to 64 bits. */
2192 ovs_be32 experimenter
; /* ONF_VENDOR_ID. */
2193 ovs_be16 exp_type
; /* 3200. */
2194 uint8_t pad
[2]; /* Not used. */
2195 ovs_be16 n_bits
; /* Number of bits to copy. */
2196 ovs_be16 src_offset
; /* Starting bit offset in source. */
2197 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
2198 uint8_t pad2
[2]; /* Not used. */
2200 * - OXM header for source field.
2201 * - OXM header for destination field.
2202 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2203 * The "pad3" member is the beginning of the above. */
2204 uint8_t pad3
[4]; /* Not used. */
2206 OFP_ASSERT(sizeof(struct onf_action_copy_field
) == 24);
2208 /* Action structure for NXAST_REG_MOVE.
2210 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2211 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2212 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
2213 * the next most significant bit, and so on.
2215 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
2216 * sense to use nxm_hasmask=1 because the action does not do any kind of
2217 * matching; it uses the actual value of a field.)
2219 * The following nxm_header values are potentially acceptable as 'src':
2234 * - NXM_OF_ICMP_TYPE
2235 * - NXM_OF_ICMP_CODE
2242 * - NXM_NX_ICMPV6_TYPE
2243 * - NXM_NX_ICMPV6_CODE
2246 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2248 * - NXM_NX_TUN_IPV4_SRC
2249 * - NXM_NX_TUN_IPV4_DST
2251 * The following nxm_header values are potentially acceptable as 'dst':
2262 * - NXM_OF_ICMP_TYPE
2263 * - NXM_OF_ICMP_CODE
2264 * - NXM_NX_ICMPV6_TYPE
2265 * - NXM_NX_ICMPV6_CODE
2271 * Modifying any of the above fields changes the corresponding packet
2276 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2280 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
2281 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
2282 * header (if any), ignoring the other bits. Setting a value with CFI=1
2283 * adds or modifies the 802.1Q header appropriately, setting the TCI field
2284 * to the field's new value (with the CFI bit masked out).
2286 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
2287 * any of these values modifies the corresponding tunnel header field used
2288 * for the packet's next tunnel encapsulation, if allowed by the
2289 * configuration of the output tunnel port.
2291 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2292 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
2293 * used only if the flow's nx_match includes an nxm_entry that specifies
2294 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2296 * The switch will reject actions for which src_ofs+n_bits is greater than the
2297 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2298 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2300 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2301 * behaves as if 'src' were copied out to a temporary buffer, then the
2302 * temporary buffer copied to 'dst'.
2304 struct nx_action_reg_move
{
2305 ovs_be16 type
; /* OFPAT_VENDOR. */
2306 ovs_be16 len
; /* Length is 24. */
2307 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2308 ovs_be16 subtype
; /* NXAST_REG_MOVE. */
2309 ovs_be16 n_bits
; /* Number of bits. */
2310 ovs_be16 src_ofs
; /* Starting bit offset in source. */
2311 ovs_be16 dst_ofs
; /* Starting bit offset in destination. */
2313 * - OXM/NXM header for source field (4 or 8 bytes).
2314 * - OXM/NXM header for destination field (4 or 8 bytes).
2315 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2317 OFP_ASSERT(sizeof(struct nx_action_reg_move
) == 16);
2320 decode_copy_field__(ovs_be16 src_offset
, ovs_be16 dst_offset
, ovs_be16 n_bits
,
2321 const void *action
, ovs_be16 action_len
, size_t oxm_offset
,
2322 const struct vl_mff_map
*vl_mff_map
,
2323 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2325 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2328 move
->ofpact
.raw
= ONFACT_RAW13_COPY_FIELD
;
2329 move
->src
.ofs
= ntohs(src_offset
);
2330 move
->src
.n_bits
= ntohs(n_bits
);
2331 move
->dst
.ofs
= ntohs(dst_offset
);
2332 move
->dst
.n_bits
= ntohs(n_bits
);
2334 struct ofpbuf b
= ofpbuf_const_initializer(action
, ntohs(action_len
));
2335 ofpbuf_pull(&b
, oxm_offset
);
2337 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->src
.field
, NULL
,
2342 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->dst
.field
, NULL
,
2348 if (!is_all_zeros(b
.data
, b
.size
)) {
2349 return OFPERR_NXBRC_MUST_BE_ZERO
;
2352 return nxm_reg_move_check(move
, NULL
);
2356 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field
*oacf
,
2357 enum ofp_version ofp_version OVS_UNUSED
,
2358 const struct vl_mff_map
*vl_mff_map
,
2359 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2361 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
2362 oacf
->n_bits
, oacf
, oacf
->len
,
2363 OBJECT_OFFSETOF(oacf
, pad2
), vl_mff_map
,
2364 tlv_bitmap
, ofpacts
);
2368 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field
*oacf
,
2369 enum ofp_version ofp_version OVS_UNUSED
,
2370 const struct vl_mff_map
*vl_mff_map
,
2371 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2373 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
2374 oacf
->n_bits
, oacf
, oacf
->len
,
2375 OBJECT_OFFSETOF(oacf
, pad3
), vl_mff_map
,
2376 tlv_bitmap
, ofpacts
);
2380 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move
*narm
,
2381 enum ofp_version ofp_version OVS_UNUSED
,
2382 const struct vl_mff_map
*vl_mff_map
,
2383 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2385 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2388 move
->ofpact
.raw
= NXAST_RAW_REG_MOVE
;
2389 move
->src
.ofs
= ntohs(narm
->src_ofs
);
2390 move
->src
.n_bits
= ntohs(narm
->n_bits
);
2391 move
->dst
.ofs
= ntohs(narm
->dst_ofs
);
2392 move
->dst
.n_bits
= ntohs(narm
->n_bits
);
2394 struct ofpbuf b
= ofpbuf_const_initializer(narm
, ntohs(narm
->len
));
2395 ofpbuf_pull(&b
, sizeof *narm
);
2397 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->src
.field
, NULL
,
2403 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->dst
.field
, NULL
,
2409 if (!is_all_zeros(b
.data
, b
.size
)) {
2410 return OFPERR_NXBRC_MUST_BE_ZERO
;
2413 return nxm_reg_move_check(move
, NULL
);
2417 encode_REG_MOVE(const struct ofpact_reg_move
*move
,
2418 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2420 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2421 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2422 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2423 * probe for support. Until we have that ability, we currently prefer
2424 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2426 size_t start_ofs
= out
->size
;
2427 if (ofp_version
>= OFP15_VERSION
) {
2428 struct ofp15_action_copy_field
*copy
= put_OFPAT15_COPY_FIELD(out
);
2429 copy
->n_bits
= htons(move
->dst
.n_bits
);
2430 copy
->src_offset
= htons(move
->src
.ofs
);
2431 copy
->dst_offset
= htons(move
->dst
.ofs
);
2432 out
->size
= out
->size
- sizeof copy
->pad2
;
2433 nx_put_mff_header(out
, move
->src
.field
, ofp_version
, false);
2434 nx_put_mff_header(out
, move
->dst
.field
, ofp_version
, false);
2435 } else if (ofp_version
== OFP13_VERSION
2436 && move
->ofpact
.raw
== ONFACT_RAW13_COPY_FIELD
) {
2437 struct onf_action_copy_field
*copy
= put_ONFACT13_COPY_FIELD(out
);
2438 copy
->n_bits
= htons(move
->dst
.n_bits
);
2439 copy
->src_offset
= htons(move
->src
.ofs
);
2440 copy
->dst_offset
= htons(move
->dst
.ofs
);
2441 out
->size
= out
->size
- sizeof copy
->pad3
;
2442 nx_put_mff_header(out
, move
->src
.field
, ofp_version
, false);
2443 nx_put_mff_header(out
, move
->dst
.field
, ofp_version
, false);
2445 struct nx_action_reg_move
*narm
= put_NXAST_REG_MOVE(out
);
2446 narm
->n_bits
= htons(move
->dst
.n_bits
);
2447 narm
->src_ofs
= htons(move
->src
.ofs
);
2448 narm
->dst_ofs
= htons(move
->dst
.ofs
);
2449 nx_put_mff_header(out
, move
->src
.field
, 0, false);
2450 nx_put_mff_header(out
, move
->dst
.field
, 0, false);
2452 pad_ofpat(out
, start_ofs
);
2455 static char * OVS_WARN_UNUSED_RESULT
2456 parse_REG_MOVE(const char *arg
, const struct ofpact_parse_params
*pp
)
2458 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(pp
->ofpacts
);
2459 return nxm_parse_reg_move(move
, arg
);
2463 format_REG_MOVE(const struct ofpact_reg_move
*a
,
2464 const struct ofpact_format_params
*fp
)
2466 nxm_format_reg_move(a
, fp
->s
);
2469 /* Action structure for OFPAT12_SET_FIELD. */
2470 struct ofp12_action_set_field
{
2471 ovs_be16 type
; /* OFPAT12_SET_FIELD. */
2472 ovs_be16 len
; /* Length is padded to 64 bits. */
2475 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2476 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2478 * The "pad" member is the beginning of the above. */
2481 OFP_ASSERT(sizeof(struct ofp12_action_set_field
) == 8);
2483 /* Action structure for NXAST_REG_LOAD.
2485 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2486 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2487 * starts at 0 for the least-significant bit, 1 for the next most significant
2490 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2491 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2494 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2495 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2496 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2497 * also stored as one less than its true value:
2500 * +------------------------------+------------------+
2501 * | ofs | n_bits - 1 |
2502 * +------------------------------+------------------+
2504 * The switch will reject actions for which ofs+n_bits is greater than the
2505 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2506 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2507 * OFPBAC_BAD_ARGUMENT.
2509 struct nx_action_reg_load
{
2510 ovs_be16 type
; /* OFPAT_VENDOR. */
2511 ovs_be16 len
; /* Length is 24. */
2512 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2513 ovs_be16 subtype
; /* NXAST_REG_LOAD. */
2514 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
2515 ovs_be32 dst
; /* Destination register. */
2516 ovs_be64 value
; /* Immediate value. */
2518 OFP_ASSERT(sizeof(struct nx_action_reg_load
) == 24);
2520 /* The NXAST_REG_LOAD2 action structure is "struct ext_action_header",
2523 * - An NXM/OXM header, value, and optionally a mask.
2524 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2526 * The "pad" member is the beginning of the above. */
2529 decode_ofpat_set_field(const struct ofp12_action_set_field
*oasf
,
2530 bool may_mask
, const struct vl_mff_map
*vl_mff_map
,
2531 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2533 struct ofpbuf b
= ofpbuf_const_initializer(oasf
, ntohs(oasf
->len
));
2534 ofpbuf_pull(&b
, OBJECT_OFFSETOF(oasf
, pad
));
2536 union mf_value value
, mask
;
2537 const struct mf_field
*field
;
2539 error
= mf_vl_mff_nx_pull_entry(&b
, vl_mff_map
, &field
, &value
,
2540 may_mask
? &mask
: NULL
, tlv_bitmap
);
2542 return (error
== OFPERR_OFPBMC_BAD_MASK
2543 ? OFPERR_OFPBAC_BAD_SET_MASK
2548 memset(&mask
, 0xff, field
->n_bytes
);
2551 if (!is_all_zeros(b
.data
, b
.size
)) {
2552 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2555 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2557 if (field
->id
== MFF_IN_PORT_OXM
) {
2558 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2561 /* oxm_length is now validated to be compatible with mf_value. */
2562 if (!field
->writable
) {
2563 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2565 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2568 /* The value must be valid for match. OpenFlow 1.5 also says,
2569 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2570 * a 1-bit in oxm_value and in oxm_mask." */
2571 if (!mf_is_value_valid(field
, &value
)
2572 || (field
->id
== MFF_VLAN_VID
2573 && (!(mask
.be16
& htons(OFPVID12_PRESENT
))
2574 || !(value
.be16
& htons(OFPVID12_PRESENT
))))) {
2575 struct ds ds
= DS_EMPTY_INITIALIZER
;
2576 mf_format(field
, &value
, NULL
, NULL
, &ds
);
2577 VLOG_WARN_RL(&rl
, "Invalid value for set field %s: %s",
2578 field
->name
, ds_cstr(&ds
));
2581 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2584 ofpact_put_set_field(ofpacts
, field
, &value
, &mask
);
2589 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2590 enum ofp_version ofp_version OVS_UNUSED
,
2591 const struct vl_mff_map
*vl_mff_map
,
2592 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2594 return decode_ofpat_set_field(oasf
, false, vl_mff_map
, tlv_bitmap
,
2599 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2600 enum ofp_version ofp_version OVS_UNUSED
,
2601 const struct vl_mff_map
*vl_mff_map
,
2602 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2604 return decode_ofpat_set_field(oasf
, true, vl_mff_map
, tlv_bitmap
, ofpacts
);
2608 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load
*narl
,
2609 enum ofp_version ofp_version OVS_UNUSED
,
2610 const struct vl_mff_map
*vl_mff_map
,
2611 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
2613 struct mf_subfield dst
;
2616 dst
.ofs
= nxm_decode_ofs(narl
->ofs_nbits
);
2617 dst
.n_bits
= nxm_decode_n_bits(narl
->ofs_nbits
);
2618 error
= mf_vl_mff_mf_from_nxm_header(ntohl(narl
->dst
), vl_mff_map
,
2619 &dst
.field
, tlv_bitmap
);
2624 error
= mf_check_dst(&dst
, NULL
);
2629 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2631 if (dst
.n_bits
< 64 && ntohll(narl
->value
) >> dst
.n_bits
) {
2632 return OFPERR_OFPBAC_BAD_ARGUMENT
;
2635 struct ofpact_set_field
*sf
= ofpact_put_reg_load(out
, dst
.field
, NULL
,
2637 bitwise_put(ntohll(narl
->value
),
2638 sf
->value
, dst
.field
->n_bytes
, dst
.ofs
,
2640 bitwise_put(UINT64_MAX
,
2641 ofpact_set_field_mask(sf
), dst
.field
->n_bytes
, dst
.ofs
,
2647 decode_NXAST_RAW_REG_LOAD2(const struct ext_action_header
*eah
,
2648 enum ofp_version ofp_version OVS_UNUSED
,
2649 const struct vl_mff_map
*vl_mff_map
,
2650 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
2652 struct ofpbuf b
= ofpbuf_const_initializer(eah
, ntohs(eah
->len
));
2653 ofpbuf_pull(&b
, OBJECT_OFFSETOF(eah
, pad
));
2655 union mf_value value
, mask
;
2656 const struct mf_field
*field
;
2658 error
= mf_vl_mff_nx_pull_entry(&b
, vl_mff_map
, &field
, &value
, &mask
,
2664 if (!is_all_zeros(b
.data
, b
.size
)) {
2665 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2668 if (!field
->writable
) {
2669 VLOG_WARN_RL(&rl
, "destination field %s is not writable", field
->name
);
2670 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2673 /* Put value and mask. */
2674 ofpact_put_reg_load2(out
, field
, &value
, &mask
);
2679 put_set_field(struct ofpbuf
*openflow
, enum ofp_version ofp_version
,
2680 enum mf_field_id field
, uint64_t value_
)
2682 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
2683 int n_bytes
= mf_from_id(field
)->n_bytes
;
2684 size_t start_ofs
= openflow
->size
;
2685 union mf_value value
;
2687 value
.be64
= htonll(value_
<< (8 * (8 - n_bytes
)));
2689 oasf
= put_OFPAT12_SET_FIELD(openflow
);
2690 openflow
->size
= openflow
->size
- sizeof oasf
->pad
;
2691 nx_put_entry(openflow
, mf_from_id(field
), ofp_version
, &value
, NULL
);
2692 pad_ofpat(openflow
, start_ofs
);
2696 put_reg_load(struct ofpbuf
*openflow
,
2697 const struct mf_subfield
*dst
, uint64_t value
)
2699 ovs_assert(dst
->n_bits
<= 64);
2701 struct nx_action_reg_load
*narl
= put_NXAST_REG_LOAD(openflow
);
2702 narl
->ofs_nbits
= nxm_encode_ofs_nbits(dst
->ofs
, dst
->n_bits
);
2703 narl
->dst
= htonl(nxm_header_from_mff(dst
->field
));
2704 narl
->value
= htonll(value
);
2708 next_load_segment(const struct ofpact_set_field
*sf
,
2709 struct mf_subfield
*dst
, uint64_t *value
)
2711 int n_bits
= sf
->field
->n_bits
;
2712 int n_bytes
= sf
->field
->n_bytes
;
2713 int start
= dst
->ofs
+ dst
->n_bits
;
2715 if (start
< n_bits
) {
2716 dst
->field
= sf
->field
;
2717 dst
->ofs
= bitwise_scan(ofpact_set_field_mask(sf
), n_bytes
, 1, start
,
2719 if (dst
->ofs
< n_bits
) {
2720 dst
->n_bits
= bitwise_scan(ofpact_set_field_mask(sf
), n_bytes
, 0,
2722 MIN(dst
->ofs
+ 64, n_bits
)) - dst
->ofs
;
2723 *value
= bitwise_get(sf
->value
, n_bytes
, dst
->ofs
, dst
->n_bits
);
2730 /* Convert 'sf' to a series of REG_LOADs. */
2732 set_field_to_nxast(const struct ofpact_set_field
*sf
, struct ofpbuf
*openflow
)
2734 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2735 * experimenter OXM or is variable length (or if it came in as
2736 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2737 * NXAST_REG_LOAD, which is backward compatible. */
2738 if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
2739 || !mf_nxm_header(sf
->field
->id
) || sf
->field
->variable_len
) {
2740 struct ext_action_header
*eah OVS_UNUSED
;
2741 size_t start_ofs
= openflow
->size
;
2743 eah
= put_NXAST_REG_LOAD2(openflow
);
2744 openflow
->size
= openflow
->size
- sizeof eah
->pad
;
2745 nx_put_entry(openflow
, sf
->field
, 0, sf
->value
,
2746 ofpact_set_field_mask(sf
));
2747 pad_ofpat(openflow
, start_ofs
);
2749 struct mf_subfield dst
;
2752 dst
.ofs
= dst
.n_bits
= 0;
2753 while (next_load_segment(sf
, &dst
, &value
)) {
2754 put_reg_load(openflow
, &dst
, value
);
2759 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2760 * actions, if we can, falling back to Nicira extensions if we must.
2762 * We check only meta-flow types that can appear within set field actions and
2763 * that have a mapping to compatible action types. These struct mf_field
2764 * definitions have a defined OXM or NXM header value and specify the field as
2767 set_field_to_legacy_openflow(const struct ofpact_set_field
*sf
,
2768 enum ofp_version ofp_version
,
2771 switch ((int) sf
->field
->id
) {
2772 case MFF_VLAN_TCI
: {
2773 ovs_be16 tci
= sf
->value
->be16
;
2774 bool cfi
= (tci
& htons(VLAN_CFI
)) != 0;
2775 uint16_t vid
= vlan_tci_to_vid(tci
);
2776 uint8_t pcp
= vlan_tci_to_pcp(tci
);
2778 if (ofp_version
< OFP11_VERSION
) {
2779 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2781 * If CFI=1, Add or modify VLAN VID & PCP.
2782 * If CFI=0, strip VLAN header, if any.
2785 put_OFPAT10_SET_VLAN_VID(out
, vid
);
2786 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
2788 put_OFPAT10_STRIP_VLAN(out
);
2791 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2793 * If CFI=1, Add or modify VLAN VID & PCP.
2794 * OpenFlow 1.1 set actions only apply if the packet
2795 * already has VLAN tags. To be sure that is the case
2796 * we have to push a VLAN header. As we do not support
2797 * multiple layers of VLANs, this is a no-op, if a VLAN
2798 * header already exists. This may backfire, however,
2799 * when we start supporting multiple layers of VLANs.
2800 * If CFI=0, strip VLAN header, if any.
2803 /* Push a VLAN tag, if one was not seen at action validation
2805 if (!sf
->flow_has_vlan
) {
2806 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
2808 put_OFPAT11_SET_VLAN_VID(out
, vid
);
2809 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
2811 /* If the flow did not match on vlan, we have no way of
2812 * knowing if the vlan tag exists, so we must POP just to be
2814 put_OFPAT11_POP_VLAN(out
);
2820 case MFF_VLAN_VID
: {
2821 uint16_t vid
= ntohs(sf
->value
->be16
) & VLAN_VID_MASK
;
2822 if (ofp_version
== OFP10_VERSION
) {
2823 put_OFPAT10_SET_VLAN_VID(out
, vid
);
2825 put_OFPAT11_SET_VLAN_VID(out
, vid
);
2831 if (ofp_version
== OFP10_VERSION
) {
2832 put_OFPAT10_SET_VLAN_PCP(out
, sf
->value
->u8
);
2834 put_OFPAT11_SET_VLAN_PCP(out
, sf
->value
->u8
);
2839 put_OFPAT_SET_DL_SRC(out
, ofp_version
)->dl_addr
= sf
->value
->mac
;
2843 put_OFPAT_SET_DL_DST(out
, ofp_version
)->dl_addr
= sf
->value
->mac
;
2847 put_OFPAT_SET_NW_SRC(out
, ofp_version
, sf
->value
->be32
);
2851 put_OFPAT_SET_NW_DST(out
, ofp_version
, sf
->value
->be32
);
2855 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
->u8
);
2858 case MFF_IP_DSCP_SHIFTED
:
2859 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
->u8
<< 2);
2863 struct ofpact_ecn ip_ecn
= { .ecn
= sf
->value
->u8
};
2864 encode_SET_IP_ECN(&ip_ecn
, ofp_version
, out
);
2870 put_OFPAT_SET_TP_SRC(out
, sf
->value
->be16
);
2875 put_OFPAT_SET_TP_DST(out
, sf
->value
->be16
);
2879 set_field_to_nxast(sf
, out
);
2885 set_field_to_set_field(const struct ofpact_set_field
*sf
,
2886 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2888 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
2889 size_t start_ofs
= out
->size
;
2891 oasf
= put_OFPAT12_SET_FIELD(out
);
2892 out
->size
= out
->size
- sizeof oasf
->pad
;
2893 nx_put_entry(out
, sf
->field
, ofp_version
, sf
->value
,
2894 ofpact_set_field_mask(sf
));
2895 pad_ofpat(out
, start_ofs
);
2899 encode_SET_FIELD(const struct ofpact_set_field
*sf
,
2900 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2902 if (ofp_version
>= OFP15_VERSION
) {
2903 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2905 set_field_to_set_field(sf
, ofp_version
, out
);
2906 } else if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD
||
2907 sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
) {
2908 /* It came in as reg_load, send it out the same way. */
2909 set_field_to_nxast(sf
, out
);
2910 } else if (ofp_version
< OFP12_VERSION
) {
2911 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2912 set_field_to_legacy_openflow(sf
, ofp_version
, out
);
2913 } else if (is_all_ones(ofpact_set_field_mask(sf
), sf
->field
->n_bytes
)) {
2914 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2915 * entire field, so encode it as OFPAT_SET_FIELD. */
2916 set_field_to_set_field(sf
, ofp_version
, out
);
2918 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2919 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2920 * so encode it as reg_load. */
2921 set_field_to_nxast(sf
, out
);
2925 /* Parses the input argument 'arg' into the key, value, and delimiter
2926 * components that are common across the reg_load and set_field action format.
2928 * With an argument like "1->metadata", sets the following pointers to
2929 * point within 'arg':
2934 * Returns NULL if successful, otherwise a malloc()'d string describing the
2935 * error. The caller is responsible for freeing the returned string. */
2936 static char * OVS_WARN_UNUSED_RESULT
2937 set_field_split_str(char *arg
, char **key
, char **value
, char **delim
)
2942 value_end
= strstr(arg
, "->");
2943 *key
= value_end
+ strlen("->");
2949 return xasprintf("%s: missing `->'", arg
);
2951 if (strlen(value_end
) <= strlen("->")) {
2952 return xasprintf("%s: missing field name following `->'", arg
);
2958 /* Parses a "set_field" action with argument 'arg', appending the parsed
2959 * action to 'pp->ofpacts'.
2961 * Returns NULL if successful, otherwise a malloc()'d string describing the
2962 * error. The caller is responsible for freeing the returned string. */
2963 static char * OVS_WARN_UNUSED_RESULT
2964 set_field_parse__(char *arg
, const struct ofpact_parse_params
*pp
)
2969 const struct mf_field
*mf
;
2970 union mf_value sf_value
, sf_mask
;
2973 error
= set_field_split_str(arg
, &key
, &value
, &delim
);
2978 mf
= mf_from_name(key
);
2980 return xasprintf("%s is not a valid OXM field name", key
);
2982 if (!mf
->writable
) {
2983 return xasprintf("%s is read-only", key
);
2987 error
= mf_parse(mf
, value
, pp
->port_map
, &sf_value
, &sf_mask
);
2992 if (!mf_is_value_valid(mf
, &sf_value
)) {
2993 return xasprintf("%s is not a valid value for field %s", value
, key
);
2996 *pp
->usable_protocols
&= mf
->usable_protocols_exact
;
2998 ofpact_put_set_field(pp
->ofpacts
, mf
, &sf_value
, &sf_mask
);
3002 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
3003 * action to 'pp->ofpacts'.
3005 * Returns NULL if successful, otherwise a malloc()'d string describing the
3006 * error. The caller is responsible for freeing the returned string. */
3007 static char * OVS_WARN_UNUSED_RESULT
3008 parse_SET_FIELD(const char *arg
, const struct ofpact_parse_params
*pp
)
3010 char *copy
= xstrdup(arg
);
3011 char *error
= set_field_parse__(copy
, pp
);
3016 static char * OVS_WARN_UNUSED_RESULT
3017 parse_reg_load(char *arg
, const struct ofpact_parse_params
*pp
)
3019 struct mf_subfield dst
;
3020 char *key
, *value_str
;
3021 union mf_value value
;
3024 error
= set_field_split_str(arg
, &key
, &value_str
, NULL
);
3029 error
= mf_parse_subfield(&dst
, key
);
3034 if (parse_int_string(value_str
, (uint8_t *)&value
, dst
.field
->n_bytes
,
3036 return xasprintf("%s: cannot parse integer value", arg
);
3039 if (!bitwise_is_all_zeros(&value
, dst
.field
->n_bytes
, dst
.n_bits
,
3040 dst
.field
->n_bytes
* 8 - dst
.n_bits
)) {
3044 mf_format(dst
.field
, &value
, NULL
, NULL
, &ds
);
3045 error
= xasprintf("%s: value %s does not fit into %d bits",
3046 arg
, ds_cstr(&ds
), dst
.n_bits
);
3051 struct ofpact_set_field
*sf
= ofpact_put_reg_load(pp
->ofpacts
, dst
.field
,
3054 bitwise_copy(&value
, dst
.field
->n_bytes
, 0, sf
->value
,
3055 dst
.field
->n_bytes
, dst
.ofs
, dst
.n_bits
);
3056 bitwise_one(ofpact_set_field_mask(sf
), dst
.field
->n_bytes
, dst
.ofs
,
3062 format_SET_FIELD(const struct ofpact_set_field
*a
,
3063 const struct ofpact_format_params
*fp
)
3065 if (a
->ofpact
.raw
== NXAST_RAW_REG_LOAD
) {
3066 struct mf_subfield dst
;
3069 dst
.ofs
= dst
.n_bits
= 0;
3070 while (next_load_segment(a
, &dst
, &value
)) {
3071 ds_put_format(fp
->s
, "%sload:%s%#"PRIx64
"%s->%s",
3072 colors
.special
, colors
.end
, value
,
3073 colors
.special
, colors
.end
);
3074 mf_format_subfield(&dst
, fp
->s
);
3075 ds_put_char(fp
->s
, ',');
3077 ds_chomp(fp
->s
, ',');
3079 ds_put_format(fp
->s
, "%sset_field:%s", colors
.special
, colors
.end
);
3080 mf_format(a
->field
, a
->value
, ofpact_set_field_mask(a
),
3081 fp
->port_map
, fp
->s
);
3082 ds_put_format(fp
->s
, "%s->%s%s",
3083 colors
.special
, colors
.end
, a
->field
->name
);
3087 /* Appends an OFPACT_SET_FIELD ofpact with enough space for the value and mask
3088 * for the 'field' to 'ofpacts' and returns it. Fills in the value from
3089 * 'value', if non-NULL, and mask from 'mask' if non-NULL. If 'value' is
3090 * non-NULL and 'mask' is NULL, an all-ones mask will be filled in. */
3091 struct ofpact_set_field
*
3092 ofpact_put_set_field(struct ofpbuf
*ofpacts
, const struct mf_field
*field
,
3093 const void *value
, const void *mask
)
3095 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(ofpacts
);
3098 /* Fill in the value and mask if given, otherwise put zeroes so that the
3099 * caller may fill in the value and mask itself. */
3101 ofpbuf_put_uninit(ofpacts
, 2 * field
->n_bytes
);
3102 sf
= ofpacts
->header
;
3103 memcpy(sf
->value
, value
, field
->n_bytes
);
3105 memcpy(ofpact_set_field_mask(sf
), mask
, field
->n_bytes
);
3107 memset(ofpact_set_field_mask(sf
), 0xff, field
->n_bytes
);
3110 ofpbuf_put_zeros(ofpacts
, 2 * field
->n_bytes
);
3111 sf
= ofpacts
->header
;
3113 /* Update length. */
3114 ofpact_finish_SET_FIELD(ofpacts
, &sf
);
3119 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
3120 * is marked such that, if possible, it will be translated to OpenFlow as
3121 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
3122 * that was the way that the action was expressed when it came into OVS or for
3123 * backward compatibility. */
3124 struct ofpact_set_field
*
3125 ofpact_put_reg_load(struct ofpbuf
*ofpacts
, const struct mf_field
*field
,
3126 const void *value
, const void *mask
)
3128 struct ofpact_set_field
*sf
= ofpact_put_set_field(ofpacts
, field
, value
,
3130 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD
;
3135 struct ofpact_set_field
*
3136 ofpact_put_reg_load2(struct ofpbuf
*ofpacts
, const struct mf_field
*field
,
3137 const void *value
, const void *mask
)
3139 struct ofpact_set_field
*sf
= ofpact_put_set_field(ofpacts
, field
, value
,
3141 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD2
;
3147 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
3149 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
3152 struct nx_action_stack
{
3153 ovs_be16 type
; /* OFPAT_VENDOR. */
3154 ovs_be16 len
; /* Length is 16. */
3155 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3156 ovs_be16 subtype
; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
3157 ovs_be16 offset
; /* Bit offset into the field. */
3159 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
3160 * - ovs_be16 'n_bits', the number of bits to extract from the field.
3161 * - Enough 0-bytes to pad out the action to 24 bytes. */
3162 uint8_t pad
[12]; /* See above. */
3164 OFP_ASSERT(sizeof(struct nx_action_stack
) == 24);
3167 decode_stack_action(const struct nx_action_stack
*nasp
,
3168 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
3169 struct ofpact_stack
*stack_action
)
3172 stack_action
->subfield
.ofs
= ntohs(nasp
->offset
);
3174 struct ofpbuf b
= ofpbuf_const_initializer(nasp
, sizeof *nasp
);
3175 ofpbuf_pull(&b
, OBJECT_OFFSETOF(nasp
, pad
));
3176 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
,
3177 &stack_action
->subfield
.field
, NULL
,
3183 stack_action
->subfield
.n_bits
= ntohs(*(const ovs_be16
*) b
.data
);
3185 if (!is_all_zeros(b
.data
, b
.size
)) {
3186 return OFPERR_NXBRC_MUST_BE_ZERO
;
3193 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack
*nasp
,
3194 enum ofp_version ofp_version OVS_UNUSED
,
3195 const struct vl_mff_map
*vl_mff_map
,
3196 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
3198 struct ofpact_stack
*push
= ofpact_put_STACK_PUSH(ofpacts
);
3199 enum ofperr error
= decode_stack_action(nasp
, vl_mff_map
, tlv_bitmap
,
3201 return error
? error
: nxm_stack_push_check(push
, NULL
);
3205 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack
*nasp
,
3206 enum ofp_version ofp_version OVS_UNUSED
,
3207 const struct vl_mff_map
*vl_mff_map
,
3208 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
3210 struct ofpact_stack
*pop
= ofpact_put_STACK_POP(ofpacts
);
3211 enum ofperr error
= decode_stack_action(nasp
, vl_mff_map
, tlv_bitmap
,
3213 return error
? error
: nxm_stack_pop_check(pop
, NULL
);
3217 encode_STACK_op(const struct ofpact_stack
*stack_action
,
3218 struct nx_action_stack
*nasp
)
3223 nasp
->offset
= htons(stack_action
->subfield
.ofs
);
3225 ofpbuf_use_stack(&b
, nasp
, ntohs(nasp
->len
));
3226 ofpbuf_put_uninit(&b
, OBJECT_OFFSETOF(nasp
, pad
));
3227 nx_put_mff_header(&b
, stack_action
->subfield
.field
, 0, false);
3228 n_bits
= htons(stack_action
->subfield
.n_bits
);
3229 ofpbuf_put(&b
, &n_bits
, sizeof n_bits
);
3233 encode_STACK_PUSH(const struct ofpact_stack
*stack
,
3234 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3236 encode_STACK_op(stack
, put_NXAST_STACK_PUSH(out
));
3240 encode_STACK_POP(const struct ofpact_stack
*stack
,
3241 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3243 encode_STACK_op(stack
, put_NXAST_STACK_POP(out
));
3246 static char * OVS_WARN_UNUSED_RESULT
3247 parse_STACK_PUSH(char *arg
, const struct ofpact_parse_params
*pp
)
3249 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(pp
->ofpacts
), arg
);
3252 static char * OVS_WARN_UNUSED_RESULT
3253 parse_STACK_POP(char *arg
, const struct ofpact_parse_params
*pp
)
3255 return nxm_parse_stack_action(ofpact_put_STACK_POP(pp
->ofpacts
), arg
);
3259 format_STACK_PUSH(const struct ofpact_stack
*a
,
3260 const struct ofpact_format_params
*fp
)
3262 nxm_format_stack_push(a
, fp
->s
);
3266 format_STACK_POP(const struct ofpact_stack
*a
,
3267 const struct ofpact_format_params
*fp
)
3269 nxm_format_stack_pop(a
, fp
->s
);
3272 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
3274 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
3275 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
3276 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3277 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3279 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3280 * sent only to controllers with id 0.)
3282 struct nx_action_cnt_ids
{
3283 ovs_be16 type
; /* OFPAT_VENDOR. */
3284 ovs_be16 len
; /* Length including slaves. */
3285 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3286 ovs_be16 subtype
; /* NXAST_DEC_TTL_CNT_IDS. */
3288 ovs_be16 n_controllers
; /* Number of controllers. */
3289 uint8_t zeros
[4]; /* Must be zero. */
3291 /* Followed by 1 or more controller ids.
3293 * uint16_t cnt_ids[]; // Controller ids.
3294 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
3297 OFP_ASSERT(sizeof(struct nx_action_cnt_ids
) == 16);
3300 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf
*out
)
3303 struct ofpact_cnt_ids
*ids
;
3304 enum ofperr error
= 0;
3306 ids
= ofpact_put_DEC_TTL(out
);
3307 ids
->n_controllers
= 1;
3308 ofpbuf_put(out
, &id
, sizeof id
);
3310 ofpact_finish_DEC_TTL(out
, &ids
);
3315 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids
*nac_ids
,
3316 enum ofp_version ofp_version OVS_UNUSED
,
3319 struct ofpact_cnt_ids
*ids
;
3323 ids
= ofpact_put_DEC_TTL(out
);
3324 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
3325 ids
->n_controllers
= ntohs(nac_ids
->n_controllers
);
3326 ids_size
= ntohs(nac_ids
->len
) - sizeof *nac_ids
;
3328 if (!is_all_zeros(nac_ids
->zeros
, sizeof nac_ids
->zeros
)) {
3329 return OFPERR_NXBRC_MUST_BE_ZERO
;
3332 if (ids_size
< ids
->n_controllers
* sizeof(ovs_be16
)) {
3333 VLOG_WARN_RL(&rl
, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE
" "
3334 "bytes allocated for controller ids. %"PRIuSIZE
" bytes "
3335 "are required for %u controllers.",
3336 ids_size
, ids
->n_controllers
* sizeof(ovs_be16
),
3337 ids
->n_controllers
);
3338 return OFPERR_OFPBAC_BAD_LEN
;
3341 for (i
= 0; i
< ids
->n_controllers
; i
++) {
3342 uint16_t id
= ntohs(((ovs_be16
*)(nac_ids
+ 1))[i
]);
3343 ofpbuf_put(out
, &id
, sizeof id
);
3347 ofpact_finish_DEC_TTL(out
, &ids
);
3353 encode_DEC_TTL(const struct ofpact_cnt_ids
*dec_ttl
,
3354 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3356 if (dec_ttl
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
3357 || dec_ttl
->n_controllers
!= 1
3358 || dec_ttl
->cnt_ids
[0] != 0) {
3359 struct nx_action_cnt_ids
*nac_ids
= put_NXAST_DEC_TTL_CNT_IDS(out
);
3360 int ids_len
= ROUND_UP(2 * dec_ttl
->n_controllers
, OFP_ACTION_ALIGN
);
3364 nac_ids
->len
= htons(ntohs(nac_ids
->len
) + ids_len
);
3365 nac_ids
->n_controllers
= htons(dec_ttl
->n_controllers
);
3367 ids
= ofpbuf_put_zeros(out
, ids_len
);
3368 for (i
= 0; i
< dec_ttl
->n_controllers
; i
++) {
3369 ids
[i
] = htons(dec_ttl
->cnt_ids
[i
]);
3372 put_OFPAT_DEC_NW_TTL(out
, ofp_version
);
3377 parse_noargs_dec_ttl(const struct ofpact_parse_params
*pp
)
3379 struct ofpact_cnt_ids
*ids
;
3382 ofpact_put_DEC_TTL(pp
->ofpacts
);
3383 ofpbuf_put(pp
->ofpacts
, &id
, sizeof id
);
3384 ids
= pp
->ofpacts
->header
;
3385 ids
->n_controllers
++;
3386 ofpact_finish_DEC_TTL(pp
->ofpacts
, &ids
);
3389 static char * OVS_WARN_UNUSED_RESULT
3390 parse_DEC_TTL(char *arg
, const struct ofpact_parse_params
*pp
)
3393 parse_noargs_dec_ttl(pp
);
3395 struct ofpact_cnt_ids
*ids
;
3398 ids
= ofpact_put_DEC_TTL(pp
->ofpacts
);
3399 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
3400 for (cntr
= strtok_r(arg
, ", ", &arg
); cntr
!= NULL
;
3401 cntr
= strtok_r(NULL
, ", ", &arg
)) {
3402 uint16_t id
= atoi(cntr
);
3404 ofpbuf_put(pp
->ofpacts
, &id
, sizeof id
);
3405 ids
= pp
->ofpacts
->header
;
3406 ids
->n_controllers
++;
3408 if (!ids
->n_controllers
) {
3409 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3412 ofpact_finish_DEC_TTL(pp
->ofpacts
, &ids
);
3418 format_DEC_TTL(const struct ofpact_cnt_ids
*a
,
3419 const struct ofpact_format_params
*fp
)
3423 ds_put_format(fp
->s
, "%sdec_ttl%s", colors
.paren
, colors
.end
);
3424 if (a
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
) {
3425 ds_put_format(fp
->s
, "%s(%s", colors
.paren
, colors
.end
);
3426 for (i
= 0; i
< a
->n_controllers
; i
++) {
3428 ds_put_cstr(fp
->s
, ",");
3430 ds_put_format(fp
->s
, "%"PRIu16
, a
->cnt_ids
[i
]);
3432 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
3436 /* Set MPLS label actions. */
3439 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label
,
3440 enum ofp_version ofp_version OVS_UNUSED
,
3443 ofpact_put_SET_MPLS_LABEL(out
)->label
= label
;
3448 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label
*label
,
3449 enum ofp_version ofp_version
,
3452 if (ofp_version
< OFP12_VERSION
) {
3453 put_OFPAT_SET_MPLS_LABEL(out
, ofp_version
, label
->label
);
3455 put_set_field(out
, ofp_version
, MFF_MPLS_LABEL
, ntohl(label
->label
));
3459 static char * OVS_WARN_UNUSED_RESULT
3460 parse_SET_MPLS_LABEL(char *arg
, const struct ofpact_parse_params
*pp
)
3462 struct ofpact_mpls_label
*mpls_label
3463 = ofpact_put_SET_MPLS_LABEL(pp
->ofpacts
);
3465 return xstrdup("set_mpls_label: expected label.");
3468 mpls_label
->label
= htonl(atoi(arg
));
3473 format_SET_MPLS_LABEL(const struct ofpact_mpls_label
*a
,
3474 const struct ofpact_format_params
*fp
)
3476 ds_put_format(fp
->s
, "%sset_mpls_label(%s%"PRIu32
"%s)%s",
3477 colors
.paren
, colors
.end
, ntohl(a
->label
),
3478 colors
.paren
, colors
.end
);
3481 /* Set MPLS TC actions. */
3484 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc
,
3485 enum ofp_version ofp_version OVS_UNUSED
,
3488 ofpact_put_SET_MPLS_TC(out
)->tc
= tc
;
3493 encode_SET_MPLS_TC(const struct ofpact_mpls_tc
*tc
,
3494 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3496 if (ofp_version
< OFP12_VERSION
) {
3497 put_OFPAT_SET_MPLS_TC(out
, ofp_version
, tc
->tc
);
3499 put_set_field(out
, ofp_version
, MFF_MPLS_TC
, tc
->tc
);
3503 static char * OVS_WARN_UNUSED_RESULT
3504 parse_SET_MPLS_TC(char *arg
, const struct ofpact_parse_params
*pp
)
3506 struct ofpact_mpls_tc
*mpls_tc
= ofpact_put_SET_MPLS_TC(pp
->ofpacts
);
3509 return xstrdup("set_mpls_tc: expected tc.");
3512 mpls_tc
->tc
= atoi(arg
);
3517 format_SET_MPLS_TC(const struct ofpact_mpls_tc
*a
,
3518 const struct ofpact_format_params
*fp
)
3520 ds_put_format(fp
->s
, "%sset_mpls_ttl(%s%"PRIu8
"%s)%s",
3521 colors
.paren
, colors
.end
, a
->tc
,
3522 colors
.paren
, colors
.end
);
3525 /* Set MPLS TTL actions. */
3528 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl
,
3529 enum ofp_version ofp_version OVS_UNUSED
,
3532 ofpact_put_SET_MPLS_TTL(out
)->ttl
= ttl
;
3537 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*ttl
,
3538 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3540 put_OFPAT_SET_MPLS_TTL(out
, ofp_version
, ttl
->ttl
);
3543 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3544 * action to 'pp->ofpacts'.
3546 * Returns NULL if successful, otherwise a malloc()'d string describing the
3547 * error. The caller is responsible for freeing the returned string. */
3548 static char * OVS_WARN_UNUSED_RESULT
3549 parse_SET_MPLS_TTL(char *arg
, const struct ofpact_parse_params
*pp
)
3551 struct ofpact_mpls_ttl
*mpls_ttl
= ofpact_put_SET_MPLS_TTL(pp
->ofpacts
);
3554 return xstrdup("set_mpls_ttl: expected ttl.");
3557 mpls_ttl
->ttl
= atoi(arg
);
3562 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*a
,
3563 const struct ofpact_format_params
*fp
)
3565 ds_put_format(fp
->s
, "%sset_mpls_ttl(%s%"PRIu8
"%s)%s",
3566 colors
.paren
, colors
.end
, a
->ttl
,
3567 colors
.paren
, colors
.end
);
3570 /* Decrement MPLS TTL actions. */
3573 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf
*out
)
3575 ofpact_put_DEC_MPLS_TTL(out
);
3580 encode_DEC_MPLS_TTL(const struct ofpact_null
*null OVS_UNUSED
,
3581 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3583 put_OFPAT_DEC_MPLS_TTL(out
, ofp_version
);
3586 static char * OVS_WARN_UNUSED_RESULT
3587 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
3589 ofpact_put_DEC_MPLS_TTL(pp
->ofpacts
);
3594 format_DEC_MPLS_TTL(const struct ofpact_null
*a OVS_UNUSED
,
3595 const struct ofpact_format_params
*fp
)
3597 ds_put_format(fp
->s
, "%sdec_mpls_ttl%s", colors
.value
, colors
.end
);
3600 /* Push MPLS label action. */
3603 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype
,
3604 enum ofp_version ofp_version OVS_UNUSED
,
3607 struct ofpact_push_mpls
*oam
;
3609 if (!eth_type_mpls(ethertype
)) {
3610 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3612 oam
= ofpact_put_PUSH_MPLS(out
);
3613 oam
->ethertype
= ethertype
;
3619 encode_PUSH_MPLS(const struct ofpact_push_mpls
*push_mpls
,
3620 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3622 put_OFPAT_PUSH_MPLS(out
, ofp_version
, push_mpls
->ethertype
);
3625 static char * OVS_WARN_UNUSED_RESULT
3626 parse_PUSH_MPLS(char *arg
, const struct ofpact_parse_params
*pp
)
3631 error
= str_to_u16(arg
, "push_mpls", ðertype
);
3633 ofpact_put_PUSH_MPLS(pp
->ofpacts
)->ethertype
= htons(ethertype
);
3639 format_PUSH_MPLS(const struct ofpact_push_mpls
*a
,
3640 const struct ofpact_format_params
*fp
)
3642 ds_put_format(fp
->s
, "%spush_mpls:%s0x%04"PRIx16
,
3643 colors
.param
, colors
.end
, ntohs(a
->ethertype
));
3646 /* Pop MPLS label action. */
3649 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype
,
3650 enum ofp_version ofp_version OVS_UNUSED
,
3653 ofpact_put_POP_MPLS(out
)->ethertype
= ethertype
;
3658 encode_POP_MPLS(const struct ofpact_pop_mpls
*pop_mpls
,
3659 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3661 put_OFPAT_POP_MPLS(out
, ofp_version
, pop_mpls
->ethertype
);
3664 static char * OVS_WARN_UNUSED_RESULT
3665 parse_POP_MPLS(char *arg
, const struct ofpact_parse_params
*pp
)
3670 error
= str_to_u16(arg
, "pop_mpls", ðertype
);
3672 ofpact_put_POP_MPLS(pp
->ofpacts
)->ethertype
= htons(ethertype
);
3678 format_POP_MPLS(const struct ofpact_pop_mpls
*a
,
3679 const struct ofpact_format_params
*fp
)
3681 ds_put_format(fp
->s
, "%spop_mpls:%s0x%04"PRIx16
,
3682 colors
.param
, colors
.end
, ntohs(a
->ethertype
));
3685 /* Set tunnel ID actions. */
3688 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id
,
3689 enum ofp_version ofp_version OVS_UNUSED
,
3692 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
3693 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL
;
3694 tunnel
->tun_id
= tun_id
;
3699 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id
,
3700 enum ofp_version ofp_version OVS_UNUSED
,
3703 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
3704 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL64
;
3705 tunnel
->tun_id
= tun_id
;
3710 encode_SET_TUNNEL(const struct ofpact_tunnel
*tunnel
,
3711 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3713 uint64_t tun_id
= tunnel
->tun_id
;
3715 if (ofp_version
< OFP12_VERSION
) {
3716 if (tun_id
<= UINT32_MAX
3717 && tunnel
->ofpact
.raw
!= NXAST_RAW_SET_TUNNEL64
) {
3718 put_NXAST_SET_TUNNEL(out
, tun_id
);
3720 put_NXAST_SET_TUNNEL64(out
, tun_id
);
3723 put_set_field(out
, ofp_version
, MFF_TUN_ID
, tun_id
);
3727 static char * OVS_WARN_UNUSED_RESULT
3728 parse_set_tunnel(char *arg
, enum ofp_raw_action_type raw
,
3729 const struct ofpact_parse_params
*pp
)
3731 struct ofpact_tunnel
*tunnel
;
3733 tunnel
= ofpact_put_SET_TUNNEL(pp
->ofpacts
);
3734 tunnel
->ofpact
.raw
= raw
;
3735 return str_to_u64(arg
, &tunnel
->tun_id
);
3738 static char * OVS_WARN_UNUSED_RESULT
3739 parse_SET_TUNNEL(char *arg
, const struct ofpact_parse_params
*pp
)
3741 return parse_set_tunnel(arg
, NXAST_RAW_SET_TUNNEL
, pp
);
3745 format_SET_TUNNEL(const struct ofpact_tunnel
*a
,
3746 const struct ofpact_format_params
*fp
)
3748 ds_put_format(fp
->s
, "%sset_tunnel%s:%s%#"PRIx64
, colors
.param
,
3749 (a
->tun_id
> UINT32_MAX
3750 || a
->ofpact
.raw
== NXAST_RAW_SET_TUNNEL64
? "64" : ""),
3751 colors
.end
, a
->tun_id
);
3754 /* Set queue action. */
3757 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id
,
3758 enum ofp_version ofp_version OVS_UNUSED
,
3761 ofpact_put_SET_QUEUE(out
)->queue_id
= queue_id
;
3766 encode_SET_QUEUE(const struct ofpact_queue
*queue
,
3767 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3769 put_OFPAT_SET_QUEUE(out
, ofp_version
, queue
->queue_id
);
3772 static char * OVS_WARN_UNUSED_RESULT
3773 parse_SET_QUEUE(char *arg
, const struct ofpact_parse_params
*pp
)
3775 return str_to_u32(arg
, &ofpact_put_SET_QUEUE(pp
->ofpacts
)->queue_id
);
3779 format_SET_QUEUE(const struct ofpact_queue
*a
,
3780 const struct ofpact_format_params
*fp
)
3782 ds_put_format(fp
->s
, "%sset_queue:%s%"PRIu32
,
3783 colors
.param
, colors
.end
, a
->queue_id
);
3786 /* Pop queue action. */
3789 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf
*out
)
3791 ofpact_put_POP_QUEUE(out
);
3796 encode_POP_QUEUE(const struct ofpact_null
*null OVS_UNUSED
,
3797 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3799 put_NXAST_POP_QUEUE(out
);
3802 static char * OVS_WARN_UNUSED_RESULT
3803 parse_POP_QUEUE(const char *arg OVS_UNUSED
,
3804 const struct ofpact_parse_params
*pp
)
3806 ofpact_put_POP_QUEUE(pp
->ofpacts
);
3811 format_POP_QUEUE(const struct ofpact_null
*a OVS_UNUSED
,
3812 const struct ofpact_format_params
*fp
)
3814 ds_put_format(fp
->s
, "%spop_queue%s", colors
.value
, colors
.end
);
3817 /* Action structure for NXAST_FIN_TIMEOUT.
3819 * This action changes the idle timeout or hard timeout, or both, of this
3820 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3821 * When such a packet is observed, the action reduces the rule's idle timeout
3822 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3823 * action has no effect on an existing timeout that is already shorter than the
3824 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3825 * of zero has no effect on the respective timeout.
3827 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3828 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3829 * receipt of the FIN or RST.
3831 * This is useful for quickly discarding learned TCP flows that otherwise will
3832 * take a long time to expire.
3834 * This action is intended for use with an OpenFlow rule that matches only a
3835 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3836 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3837 * RST in any of those flows will cause the entire OpenFlow rule to expire
3838 * early, which is not normally desirable.
3840 struct nx_action_fin_timeout
{
3841 ovs_be16 type
; /* OFPAT_VENDOR. */
3842 ovs_be16 len
; /* 16. */
3843 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3844 ovs_be16 subtype
; /* NXAST_FIN_TIMEOUT. */
3845 ovs_be16 fin_idle_timeout
; /* New idle timeout, if nonzero. */
3846 ovs_be16 fin_hard_timeout
; /* New hard timeout, if nonzero. */
3847 ovs_be16 pad
; /* Must be zero. */
3849 OFP_ASSERT(sizeof(struct nx_action_fin_timeout
) == 16);
3852 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout
*naft
,
3853 enum ofp_version ofp_version OVS_UNUSED
,
3856 struct ofpact_fin_timeout
*oft
;
3858 oft
= ofpact_put_FIN_TIMEOUT(out
);
3859 oft
->fin_idle_timeout
= ntohs(naft
->fin_idle_timeout
);
3860 oft
->fin_hard_timeout
= ntohs(naft
->fin_hard_timeout
);
3865 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout
*fin_timeout
,
3866 enum ofp_version ofp_version OVS_UNUSED
,
3869 struct nx_action_fin_timeout
*naft
= put_NXAST_FIN_TIMEOUT(out
);
3870 naft
->fin_idle_timeout
= htons(fin_timeout
->fin_idle_timeout
);
3871 naft
->fin_hard_timeout
= htons(fin_timeout
->fin_hard_timeout
);
3874 static char * OVS_WARN_UNUSED_RESULT
3875 parse_FIN_TIMEOUT(char *arg
, const struct ofpact_parse_params
*pp
)
3877 struct ofpact_fin_timeout
*oft
= ofpact_put_FIN_TIMEOUT(pp
->ofpacts
);
3880 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
3883 if (!strcmp(key
, "idle_timeout")) {
3884 error
= str_to_u16(value
, key
, &oft
->fin_idle_timeout
);
3885 } else if (!strcmp(key
, "hard_timeout")) {
3886 error
= str_to_u16(value
, key
, &oft
->fin_hard_timeout
);
3888 error
= xasprintf("invalid key '%s' in 'fin_timeout' argument",
3900 format_FIN_TIMEOUT(const struct ofpact_fin_timeout
*a
,
3901 const struct ofpact_format_params
*fp
)
3903 ds_put_format(fp
->s
, "%sfin_timeout(%s", colors
.paren
, colors
.end
);
3904 if (a
->fin_idle_timeout
) {
3905 ds_put_format(fp
->s
, "%sidle_timeout=%s%"PRIu16
",",
3906 colors
.param
, colors
.end
, a
->fin_idle_timeout
);
3908 if (a
->fin_hard_timeout
) {
3909 ds_put_format(fp
->s
, "%shard_timeout=%s%"PRIu16
",",
3910 colors
.param
, colors
.end
, a
->fin_hard_timeout
);
3912 ds_chomp(fp
->s
, ',');
3913 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
3916 /* Action structure for NXAST_ENCAP */
3917 struct nx_action_encap
{
3918 ovs_be16 type
; /* OFPAT_VENDOR. */
3919 ovs_be16 len
; /* Total size including any property TLVs. */
3920 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3921 ovs_be16 subtype
; /* NXAST_ENCAP. */
3922 ovs_be16 hdr_size
; /* Header size in bytes, 0 = 'not specified'.*/
3923 ovs_be32 new_pkt_type
; /* Header type to add and PACKET_TYPE of result. */
3924 struct ofp_ed_prop_header props
[]; /* Encap TLV properties. */
3926 OFP_ASSERT(sizeof(struct nx_action_encap
) == 16);
3929 decode_NXAST_RAW_ENCAP(const struct nx_action_encap
*nae
,
3930 enum ofp_version ofp_version OVS_UNUSED
,
3933 struct ofpact_encap
*encap
;
3934 const struct ofp_ed_prop_header
*ofp_prop
;
3936 uint16_t n_props
= 0;
3939 encap
= ofpact_put_ENCAP(out
);
3940 encap
->ofpact
.raw
= NXAST_RAW_ENCAP
;
3941 switch (ntohl(nae
->new_pkt_type
)) {
3944 /* Add supported encap header types here. */
3947 return OFPERR_NXBAC_BAD_HEADER_TYPE
;
3949 encap
->new_pkt_type
= nae
->new_pkt_type
;
3950 encap
->hdr_size
= ntohs(nae
->hdr_size
);
3952 ofp_prop
= nae
->props
;
3953 props_len
= ntohs(nae
->len
) - offsetof(struct nx_action_encap
, props
);
3955 while (props_len
> 0) {
3956 err
= decode_ed_prop(&ofp_prop
, out
, &props_len
);
3962 encap
->n_props
= n_props
;
3963 out
->header
= &encap
->ofpact
;
3964 ofpact_finish_ENCAP(out
, &encap
);
3970 encode_ENCAP(const struct ofpact_encap
*encap
,
3971 enum ofp_version ofp_version OVS_UNUSED
,
3974 size_t start_ofs
= out
->size
;
3975 struct nx_action_encap
*nae
= put_NXAST_ENCAP(out
);
3978 nae
->new_pkt_type
= encap
->new_pkt_type
;
3979 nae
->hdr_size
= htons(encap
->hdr_size
);
3980 const struct ofpact_ed_prop
*prop
= encap
->props
;
3981 for (i
= 0; i
< encap
->n_props
; i
++) {
3982 encode_ed_prop(&prop
, out
);
3984 pad_ofpat(out
, start_ofs
);
3988 parse_encap_header(const char *hdr
, ovs_be32
*packet_type
)
3990 if (strcmp(hdr
, "ethernet") == 0) {
3991 *packet_type
= htonl(PT_ETH
);
3992 } else if (strcmp(hdr
, "nsh") == 0) {
3993 *packet_type
= htonl(PT_NSH
);
4001 parse_ed_props(const uint16_t prop_class
, char **arg
, int *n_props
, struct ofpbuf
*out
)
4003 char *key
, *value
, *err
;
4006 while (ofputil_parse_key_value(arg
, &key
, &value
)) {
4007 if (!parse_ed_prop_type(prop_class
, key
, &prop_type
)) {
4008 return xasprintf("Invalid property: %s", key
);
4010 if (value
== NULL
) {
4011 return xasprintf("Missing the value for property: %s", key
);
4013 err
= parse_ed_prop_value(prop_class
, prop_type
, value
, out
);
4022 /* The string representation of the encap action is
4023 * encap(header_type(prop=<value>,tlv(<class>,<type>,<value>),...))
4026 static char * OVS_WARN_UNUSED_RESULT
4027 parse_ENCAP(char *arg
, const struct ofpact_parse_params
*pp
)
4029 struct ofpact_encap
*encap
;
4030 char *key
, *value
, *str
;
4032 uint16_t prop_class
;
4035 encap
= ofpact_put_ENCAP(pp
->ofpacts
);
4036 encap
->hdr_size
= 0;
4037 /* Parse encap header type. */
4039 if (!ofputil_parse_key_value(&arg
, &key
, &value
)) {
4040 return xasprintf("Missing encap hdr: %s", str
);
4042 if (!parse_encap_header(key
, &encap
->new_pkt_type
)) {
4043 return xasprintf("Encap hdr not supported: %s", value
);
4045 if (!parse_ed_prop_class(key
, &prop_class
)) {
4046 return xasprintf("Invalid encap prop class: %s", key
);
4048 /* Parse encap properties. */
4049 error
= parse_ed_props(prop_class
, &value
, &n_props
, pp
->ofpacts
);
4050 if (error
!= NULL
) {
4053 /* ofpbuf may have been re-allocated. */
4054 encap
= pp
->ofpacts
->header
;
4055 encap
->n_props
= n_props
;
4056 ofpact_finish_ENCAP(pp
->ofpacts
, &encap
);
4061 format_encap_pkt_type(const ovs_be32 pkt_type
)
4063 switch (ntohl(pkt_type
)) {
4074 format_ed_props(struct ds
*s
, uint16_t n_props
,
4075 const struct ofpact_ed_prop
*prop
)
4077 const uint8_t *p
= (uint8_t *) prop
;
4083 for (i
= 0; i
< n_props
; i
++) {
4084 format_ed_prop(s
, prop
);
4085 ds_put_char(s
, ',');
4086 p
+= ROUND_UP(prop
->len
, 8);
4087 prop
= ALIGNED_CAST(const struct ofpact_ed_prop
*, p
);
4095 format_ENCAP(const struct ofpact_encap
*a
,
4096 const struct ofpact_format_params
*fp
)
4098 ds_put_format(fp
->s
, "%sencap(%s", colors
.paren
, colors
.end
);
4099 ds_put_format(fp
->s
, "%s", format_encap_pkt_type(a
->new_pkt_type
));
4100 if (a
->n_props
> 0) {
4101 ds_put_format(fp
->s
, "%s(%s", colors
.paren
, colors
.end
);
4102 format_ed_props(fp
->s
, a
->n_props
, a
->props
);
4103 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
4105 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
4108 /* Action structure for NXAST_DECAP */
4109 struct nx_action_decap
{
4110 ovs_be16 type
; /* OFPAT_VENDOR. */
4111 ovs_be16 len
; /* Total size including any property TLVs. */
4112 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4113 ovs_be16 subtype
; /* NXAST_DECAP. */
4114 uint8_t pad
[2]; /* 2 bytes padding */
4116 /* Packet type or result.
4118 * The special value (0,0xFFFE) "Use next proto"
4119 * is used to request OVS to automatically set the new packet type based
4120 * on the decap'ed header's next protocol.
4122 ovs_be32 new_pkt_type
;
4124 OFP_ASSERT(sizeof(struct nx_action_decap
) == 16);
4127 decode_NXAST_RAW_DECAP(const struct nx_action_decap
*nad
,
4128 enum ofp_version ofp_version OVS_UNUSED
,
4129 struct ofpbuf
*ofpacts
)
4131 struct ofpact_decap
* decap
;
4133 if (ntohs(nad
->len
) > sizeof *nad
) {
4134 /* No properties supported yet. */
4135 return OFPERR_NXBAC_UNKNOWN_ED_PROP
;
4138 decap
= ofpact_put_DECAP(ofpacts
);
4139 decap
->ofpact
.raw
= NXAST_RAW_DECAP
;
4140 decap
->new_pkt_type
= nad
->new_pkt_type
;
4145 encode_DECAP(const struct ofpact_decap
*decap
,
4146 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4148 struct nx_action_decap
*nad
= put_NXAST_DECAP(out
);
4150 nad
->len
= htons(sizeof(struct nx_action_decap
));
4151 nad
->new_pkt_type
= decap
->new_pkt_type
;
4154 static char * OVS_WARN_UNUSED_RESULT
4155 parse_DECAP(char *arg
, const struct ofpact_parse_params
*pp
)
4157 struct ofpact_decap
*decap
;
4158 char *key
, *value
, *pos
;
4162 decap
= ofpact_put_DECAP(pp
->ofpacts
);
4163 /* Default next packet_type is PT_USE_NEXT_PROTO. */
4164 decap
->new_pkt_type
= htonl(PT_USE_NEXT_PROTO
);
4166 /* Parse decap packet_type if given. */
4167 if (ofputil_parse_key_value(&arg
, &key
, &value
)) {
4168 if (strcmp(key
, "packet_type") == 0) {
4170 if (!ofputil_parse_key_value(&pos
, &key
, &value
)
4171 || strcmp(key
, "ns") != 0) {
4172 return xstrdup("Missing packet_type attribute ns");
4174 error
= str_to_u16(value
, "ns", &ns
);
4178 if (ns
>= OFPHTN_N_TYPES
) {
4179 return xasprintf("Unsupported ns value: %"PRIu16
, ns
);
4181 if (!ofputil_parse_key_value(&pos
, &key
, &value
)
4182 || strcmp(key
, "type") != 0) {
4183 return xstrdup("Missing packet_type attribute type");
4185 error
= str_to_u16(value
, "type", &type
);
4189 decap
->new_pkt_type
= htonl(PACKET_TYPE(ns
, type
));
4191 return xasprintf("Invalid decap argument: %s", key
);
4198 format_DECAP(const struct ofpact_decap
*a
,
4199 const struct ofpact_format_params
*fp
)
4201 ds_put_format(fp
->s
, "%sdecap(%s", colors
.paren
, colors
.end
);
4202 if (a
->new_pkt_type
!= htonl(PT_USE_NEXT_PROTO
)) {
4203 ds_put_format(fp
->s
, "packet_type(ns=%"PRIu16
",id=%#"PRIx16
")",
4204 pt_ns(a
->new_pkt_type
),
4205 pt_ns_type(a
->new_pkt_type
));
4207 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
4210 /* Action dec_nsh_ttl */
4213 decode_NXAST_RAW_DEC_NSH_TTL(struct ofpbuf
*out
)
4215 ofpact_put_DEC_NSH_TTL(out
);
4220 encode_DEC_NSH_TTL(const struct ofpact_null
*null OVS_UNUSED
,
4221 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4223 put_NXAST_DEC_NSH_TTL(out
);
4226 static char * OVS_WARN_UNUSED_RESULT
4227 parse_DEC_NSH_TTL(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
4229 ofpact_put_DEC_NSH_TTL(pp
->ofpacts
);
4234 format_DEC_NSH_TTL(const struct ofpact_null
*a OVS_UNUSED
,
4235 const struct ofpact_format_params
*fp
)
4237 ds_put_format(fp
->s
, "%sdec_nsh_ttl%s", colors
.special
, colors
.end
);
4241 /* Action structures for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE, and
4242 * NXAST_RESUBMIT_TABLE_CT.
4244 * These actions search one of the switch's flow tables:
4246 * - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the
4247 * 'table' member is not 255, then it specifies the table to search.
4249 * - Otherwise (for NXAST_RESUBMIT_TABLE or NXAST_RESUBMIT_TABLE_CT with a
4250 * 'table' of 255, or for NXAST_RESUBMIT regardless of 'table'), it
4251 * searches the current flow table, that is, the OpenFlow flow table that
4252 * contains the flow from which this action was obtained. If this action
4253 * did not come from a flow table (e.g. it came from an OFPT_PACKET_OUT
4254 * message), then table 0 is the current table.
4256 * The flow table lookup uses a flow that may be slightly modified from the
4259 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
4260 * is used as the flow's in_port.
4262 * - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the 'in_port'
4263 * member is not OFPP_IN_PORT, then its value is used as the flow's
4264 * in_port. Otherwise, the original in_port is used.
4266 * - For NXAST_RESUBMIT_TABLE_CT the Conntrack 5-tuple fields are used as
4267 * the packets IP header fields during the lookup.
4269 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
4270 * resubmit action, then the flow is updated with the new values.
4272 * Following the lookup, the original in_port is restored.
4274 * If the modified flow matched in the flow table, then the corresponding
4275 * actions are executed. Afterward, actions following the resubmit in the
4276 * original set of actions, if any, are executed; any changes made to the
4277 * packet (e.g. changes to VLAN) by secondary actions persist when those
4278 * actions are executed, although the original in_port is restored.
4280 * Resubmit actions may be used any number of times within a set of actions.
4282 * Resubmit actions may nest. To prevent infinite loops and excessive resource
4283 * use, the implementation may limit nesting depth and the total number of
4286 * - Open vSwitch 1.0.1 and earlier did not support recursion.
4288 * - Open vSwitch 1.0.2 and 1.0.3 limited recursion to 8 levels.
4290 * - Open vSwitch 1.1 and 1.2 limited recursion to 16 levels.
4292 * - Open vSwitch 1.2 through 1.8 limited recursion to 32 levels.
4294 * - Open vSwitch 1.9 through 2.0 limited recursion to 64 levels.
4296 * - Open vSwitch 2.1 through 2.5 limited recursion to 64 levels and impose
4297 * a total limit of 4,096 resubmits per flow translation (earlier versions
4298 * did not impose any total limit).
4300 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE and
4301 * NXAST_RESUBMIT_TABLE_CT require 'pad' to be all-bits-zero.
4303 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
4304 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE. Open vSwitch before
4305 * 2.8.0 did not support NXAST_RESUBMIT_TABLE_CT.
4307 struct nx_action_resubmit
{
4308 ovs_be16 type
; /* OFPAT_VENDOR. */
4309 ovs_be16 len
; /* Length is 16. */
4310 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4311 ovs_be16 subtype
; /* NXAST_RESUBMIT. */
4312 ovs_be16 in_port
; /* New in_port for checking flow table. */
4313 uint8_t table
; /* NXAST_RESUBMIT_TABLE: table to use. */
4316 OFP_ASSERT(sizeof(struct nx_action_resubmit
) == 16);
4319 decode_NXAST_RAW_RESUBMIT(uint16_t port
,
4320 enum ofp_version ofp_version OVS_UNUSED
,
4323 struct ofpact_resubmit
*resubmit
;
4325 resubmit
= ofpact_put_RESUBMIT(out
);
4326 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT
;
4327 resubmit
->in_port
= u16_to_ofp(port
);
4328 resubmit
->table_id
= 0xff;
4333 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit
*nar
,
4334 enum ofp_version ofp_version OVS_UNUSED
,
4337 struct ofpact_resubmit
*resubmit
;
4339 if (nar
->pad
[0] || nar
->pad
[1] || nar
->pad
[2]) {
4340 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4343 resubmit
= ofpact_put_RESUBMIT(out
);
4344 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT_TABLE
;
4345 resubmit
->in_port
= u16_to_ofp(ntohs(nar
->in_port
));
4346 resubmit
->table_id
= nar
->table
;
4351 decode_NXAST_RAW_RESUBMIT_TABLE_CT(const struct nx_action_resubmit
*nar
,
4352 enum ofp_version ofp_version OVS_UNUSED
,
4355 enum ofperr error
= decode_NXAST_RAW_RESUBMIT_TABLE(nar
, ofp_version
, out
);
4359 struct ofpact_resubmit
*resubmit
= out
->header
;
4360 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT_TABLE_CT
;
4361 resubmit
->with_ct_orig
= true;
4366 encode_RESUBMIT(const struct ofpact_resubmit
*resubmit
,
4367 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4369 uint16_t in_port
= ofp_to_u16(resubmit
->in_port
);
4371 if (resubmit
->table_id
== 0xff
4372 && resubmit
->ofpact
.raw
== NXAST_RAW_RESUBMIT
) {
4373 put_NXAST_RESUBMIT(out
, in_port
);
4375 struct nx_action_resubmit
*nar
;
4376 nar
= resubmit
->with_ct_orig
4377 ? put_NXAST_RESUBMIT_TABLE_CT(out
) : put_NXAST_RESUBMIT_TABLE(out
);
4378 nar
->table
= resubmit
->table_id
;
4379 nar
->in_port
= htons(in_port
);
4383 static char * OVS_WARN_UNUSED_RESULT
4384 parse_RESUBMIT(char *arg
, const struct ofpact_parse_params
*pp
)
4386 struct ofpact_resubmit
*resubmit
;
4387 char *in_port_s
, *table_s
, *ct_s
;
4389 resubmit
= ofpact_put_RESUBMIT(pp
->ofpacts
);
4391 in_port_s
= strsep(&arg
, ",");
4392 if (in_port_s
&& in_port_s
[0]) {
4393 if (!ofputil_port_from_string(in_port_s
, pp
->port_map
,
4394 &resubmit
->in_port
)) {
4395 return xasprintf("%s: resubmit to unknown port", in_port_s
);
4398 resubmit
->in_port
= OFPP_IN_PORT
;
4401 table_s
= strsep(&arg
, ",");
4402 if (table_s
&& table_s
[0]) {
4403 if (!ofputil_table_from_string(table_s
, pp
->table_map
,
4404 &resubmit
->table_id
)) {
4405 return xasprintf("%s: resubmit to unknown table", table_s
);
4408 resubmit
->table_id
= 255;
4411 ct_s
= strsep(&arg
, ",");
4412 if (ct_s
&& ct_s
[0]) {
4413 if (strcmp(ct_s
, "ct")) {
4414 return xasprintf("%s: unknown parameter", ct_s
);
4416 resubmit
->with_ct_orig
= true;
4418 resubmit
->with_ct_orig
= false;
4421 if (resubmit
->in_port
== OFPP_IN_PORT
&& resubmit
->table_id
== 255) {
4422 return xstrdup("at least one \"in_port\" or \"table\" must be "
4423 "specified on resubmit");
4429 format_RESUBMIT(const struct ofpact_resubmit
*a
,
4430 const struct ofpact_format_params
*fp
)
4432 if (a
->in_port
!= OFPP_IN_PORT
&& a
->table_id
== 255) {
4433 ds_put_format(fp
->s
, "%sresubmit:%s", colors
.special
, colors
.end
);
4434 ofputil_format_port(a
->in_port
, fp
->port_map
, fp
->s
);
4436 ds_put_format(fp
->s
, "%sresubmit(%s", colors
.paren
, colors
.end
);
4437 if (a
->in_port
!= OFPP_IN_PORT
) {
4438 ofputil_format_port(a
->in_port
, fp
->port_map
, fp
->s
);
4440 ds_put_char(fp
->s
, ',');
4441 if (a
->table_id
!= 255) {
4442 ofputil_format_table(a
->table_id
, fp
->table_map
, fp
->s
);
4444 if (a
->with_ct_orig
) {
4445 ds_put_cstr(fp
->s
, ",ct");
4447 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
4451 /* Action structure for NXAST_LEARN and NXAST_LEARN2.
4453 * This action adds or modifies a flow in an OpenFlow table, similar to
4454 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
4455 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
4456 * flow's match criteria and actions are built by applying each of the series
4457 * of flow_mod_spec elements included as part of the action.
4459 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
4460 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
4461 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
4462 * bits from a source to a destination. In this case, the header contains
4465 * 15 14 13 12 11 10 0
4466 * +------+---+------+---------------------------------+
4467 * | 0 |src| dst | n_bits |
4468 * +------+---+------+---------------------------------+
4470 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
4471 * following table summarizes the meaning of each possible combination.
4472 * Details follow the table:
4475 * --- --- ----------------------------------------------------------
4476 * 0 0 Add match criteria based on value in a field.
4477 * 1 0 Add match criteria based on an immediate value.
4478 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
4479 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
4480 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
4481 * All other combinations are undefined and not allowed.
4483 * The flow_mod_spec header is followed by a source specification and a
4484 * destination specification. The format and meaning of the source
4485 * specification depends on 'src':
4487 * - If 'src' is 0, the source bits are taken from a field in the flow to
4488 * which this action is attached. (This should be a wildcarded field. If
4489 * its value is fully specified then the source bits being copied have
4492 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
4493 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
4494 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
4495 * 'field' and 'ofs' are subject to the same restrictions as the source
4496 * field in NXAST_REG_MOVE.
4498 * - If 'src' is 1, the source bits are a constant value. The source
4499 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
4500 * number in network order, the source bits are the 'n_bits'
4501 * least-significant bits. The switch will report an error if other bits
4502 * in the constant are nonzero.
4504 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
4505 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
4506 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
4507 * meaning of the flow_mod_spec depends on 'dst':
4509 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
4510 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
4511 * packet equal the source bits. 'field' may be any nxm_header with
4512 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
4514 * Order is significant. Earlier flow_mod_specs must satisfy any
4515 * prerequisites for matching fields specified later, by copying constant
4516 * values into prerequisite fields.
4518 * The switch will reject flow_mod_specs that do not satisfy NXM masking
4521 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
4522 * the new flow. The new flow copies the source bits into
4523 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
4526 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
4527 * greater than 64 yields multiple NXAST_REG_LOAD actions.
4529 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
4530 * empty. It has the following meaning:
4532 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
4533 * The new flow outputs to the OpenFlow port specified by the source field.
4534 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
4535 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
4538 * Resource Management
4539 * -------------------
4541 * A switch has a finite amount of flow table space available for learning.
4542 * When this space is exhausted, no new learning table entries will be learned
4543 * until some existing flow table entries expire. The controller should be
4544 * prepared to handle this by flooding (which can be implemented as a
4545 * low-priority flow).
4547 * If a learned flow matches a single TCP stream with a relatively long
4548 * timeout, one may make the best of resource constraints by setting
4549 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
4550 * both, to shorter timeouts. When either of these is specified as a nonzero
4551 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
4557 * The following examples give a prose description of the flow_mod_specs along
4558 * with informal notation for how those would be represented and a hex dump of
4559 * the bytes that would be required.
4561 * These examples could work with various nx_action_learn parameters. Typical
4562 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
4563 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
4565 * 1. Learn input port based on the source MAC, with lookup into
4566 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
4568 * Match on in_port=99:
4569 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
4570 * ovs_be16(99), 00 63
4571 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4573 * Match Ethernet destination on Ethernet source from packet:
4574 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4575 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4576 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4578 * Set NXM_NX_REG1[16:31] to the packet's input port:
4579 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
4580 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4581 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
4583 * Given a packet that arrived on port A with Ethernet source address B,
4584 * this would set up the flow "in_port=99, dl_dst=B,
4585 * actions=load:A->NXM_NX_REG1[16..31]".
4587 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
4588 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4589 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
4591 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
4592 * into NXM_NX_REG1[16:31]:
4594 * Match on same VLAN ID as packet:
4595 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
4596 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4597 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4599 * Match Ethernet destination on Ethernet source from packet:
4600 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4601 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4602 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4604 * Output to the packet's input port:
4605 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
4606 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4608 * Given a packet that arrived on port A with Ethernet source address B in
4609 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
4610 * actions=output:A".
4612 * In syntax accepted by ovs-ofctl, this action is:
4613 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4614 * output:NXM_OF_IN_PORT[])
4616 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
4617 * 10-second MAC expiration time to make it easier to see what's going on
4619 * ovs-vsctl del-controller br0
4620 * ovs-ofctl del-flows br0
4621 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
4622 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
4623 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
4624 output:NXM_OF_IN_PORT[]), resubmit(,1)"
4625 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
4627 * You can then dump the MAC learning table with:
4629 * ovs-ofctl dump-flows br0 table=1
4634 * For best performance, segregate learned flows into a table that is not used
4635 * for any other flows except possibly for a lowest-priority "catch-all" flow
4636 * (a flow with no match criteria). If different learning actions specify
4637 * different match criteria, use different tables for the learned flows.
4639 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
4640 * These timeouts apply to the flow that is added, which means that a flow with
4641 * an idle timeout will expire when no traffic has been sent *to* the learned
4642 * address. This is not usually the intent in MAC learning; instead, we want
4643 * the MAC learn entry to expire when no traffic has been sent *from* the
4644 * learned address. Use a hard timeout for that.
4647 * Visibility of Changes
4648 * ---------------------
4650 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
4651 * flow translation are visible to flow table lookups made later in the flow
4652 * translation. This means that, in the example above, a MAC learned by the
4653 * learn action in table 0 would be found in table 1 (if the packet being
4654 * processed had the same source and destination MAC address).
4656 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
4657 * modify a flow) by a "learn" action are visible only for later flow
4658 * translations, not for later lookups within the same flow translation. In
4659 * the MAC learning example, a MAC learned by the learn action in table 0 would
4660 * not be found in table 1 if the flow translation would resubmit to table 1
4661 * after the processing of the learn action, meaning that if this MAC had not
4662 * been learned before then the packet would be flooded. */
4663 struct nx_action_learn
{
4664 ovs_be16 type
; /* OFPAT_VENDOR. */
4665 ovs_be16 len
; /* At least 24. */
4666 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4667 ovs_be16 subtype
; /* NXAST_LEARN. */
4668 ovs_be16 idle_timeout
; /* Idle time before discarding (seconds). */
4669 ovs_be16 hard_timeout
; /* Max time before discarding (seconds). */
4670 ovs_be16 priority
; /* Priority level of flow entry. */
4671 ovs_be64 cookie
; /* Cookie for new flow. */
4672 ovs_be16 flags
; /* NX_LEARN_F_*. */
4673 uint8_t table_id
; /* Table to insert flow entry. */
4674 uint8_t pad
; /* Must be zero. */
4675 ovs_be16 fin_idle_timeout
; /* Idle timeout after FIN, if nonzero. */
4676 ovs_be16 fin_hard_timeout
; /* Hard timeout after FIN, if nonzero. */
4677 /* Followed by a sequence of flow_mod_spec elements, as described above,
4678 * until the end of the action is reached. */
4680 OFP_ASSERT(sizeof(struct nx_action_learn
) == 32);
4682 struct nx_action_learn2
{
4683 struct nx_action_learn up
; /* The wire format includes nx_action_learn. */
4684 ovs_be32 limit
; /* Maximum number of learned flows.
4685 * 0 indicates unlimited. */
4687 /* Where to store the result. */
4688 ovs_be16 result_dst_ofs
; /* Starting bit offset in destination. */
4690 ovs_be16 pad2
; /* Must be zero. */
4692 * - OXM/NXM header for destination field (4 or 8 bytes),
4693 * if NX_LEARN_F_WRITE_RESULT is set in 'flags'
4694 * - a sequence of flow_mod_spec elements, as described above,
4695 * until the end of the action is reached. */
4697 OFP_ASSERT(sizeof(struct nx_action_learn2
) == 40);
4700 get_be16(const void **pp
)
4702 const ovs_be16
*p
= *pp
;
4703 ovs_be16 value
= *p
;
4709 get_be32(const void **pp
)
4711 const ovs_be32
*p
= *pp
;
4712 ovs_be32 value
= get_unaligned_be32(p
);
4718 get_subfield(int n_bits
, const void **p
, struct mf_subfield
*sf
,
4719 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
)
4723 error
= mf_vl_mff_mf_from_nxm_header(ntohl(get_be32(p
)), vl_mff_map
,
4724 &sf
->field
, tlv_bitmap
);
4725 sf
->ofs
= ntohs(get_be16(p
));
4726 sf
->n_bits
= n_bits
;
4731 learn_min_len(uint16_t header
)
4733 int n_bits
= header
& NX_LEARN_N_BITS_MASK
;
4734 int src_type
= header
& NX_LEARN_SRC_MASK
;
4735 int dst_type
= header
& NX_LEARN_DST_MASK
;
4736 unsigned int min_len
;
4739 if (src_type
== NX_LEARN_SRC_FIELD
) {
4740 min_len
+= sizeof(ovs_be32
); /* src_field */
4741 min_len
+= sizeof(ovs_be16
); /* src_ofs */
4743 min_len
+= DIV_ROUND_UP(n_bits
, 16);
4745 if (dst_type
== NX_LEARN_DST_MATCH
||
4746 dst_type
== NX_LEARN_DST_LOAD
) {
4747 min_len
+= sizeof(ovs_be32
); /* dst_field */
4748 min_len
+= sizeof(ovs_be16
); /* dst_ofs */
4754 decode_LEARN_common(const struct nx_action_learn
*nal
,
4755 enum ofp_raw_action_type raw
,
4756 struct ofpact_learn
*learn
)
4759 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4762 learn
->ofpact
.raw
= raw
;
4763 learn
->idle_timeout
= ntohs(nal
->idle_timeout
);
4764 learn
->hard_timeout
= ntohs(nal
->hard_timeout
);
4765 learn
->priority
= ntohs(nal
->priority
);
4766 learn
->cookie
= nal
->cookie
;
4767 learn
->table_id
= nal
->table_id
;
4768 learn
->fin_idle_timeout
= ntohs(nal
->fin_idle_timeout
);
4769 learn
->fin_hard_timeout
= ntohs(nal
->fin_hard_timeout
);
4770 learn
->flags
= ntohs(nal
->flags
);
4772 if (learn
->table_id
== 0xff) {
4773 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4780 decode_LEARN_specs(const void *p
, const void *end
,
4781 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
4782 struct ofpbuf
*ofpacts
)
4784 struct ofpact_learn
*learn
= ofpacts
->header
;
4787 struct ofpact_learn_spec
*spec
;
4788 uint16_t header
= ntohs(get_be16(&p
));
4794 spec
= ofpbuf_put_zeros(ofpacts
, sizeof *spec
);
4795 learn
= ofpacts
->header
;
4797 spec
->src_type
= header
& NX_LEARN_SRC_MASK
;
4798 spec
->dst_type
= header
& NX_LEARN_DST_MASK
;
4799 spec
->n_bits
= header
& NX_LEARN_N_BITS_MASK
;
4801 /* Check for valid src and dst type combination. */
4802 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
4803 spec
->dst_type
== NX_LEARN_DST_LOAD
||
4804 (spec
->dst_type
== NX_LEARN_DST_OUTPUT
&&
4805 spec
->src_type
== NX_LEARN_SRC_FIELD
)) {
4808 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4811 /* Check that the arguments don't overrun the end of the action. */
4812 if ((char *) end
- (char *) p
< learn_min_len(header
)) {
4813 return OFPERR_OFPBAC_BAD_LEN
;
4816 /* Get the source. */
4817 const uint8_t *imm
= NULL
;
4818 unsigned int imm_bytes
= 0;
4820 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
4821 error
= get_subfield(spec
->n_bits
, &p
, &spec
->src
, vl_mff_map
,
4827 int p_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
4828 p
= (const uint8_t *) p
+ p_bytes
;
4830 imm_bytes
= DIV_ROUND_UP(spec
->n_bits
, 8);
4831 imm
= (const uint8_t *) p
- imm_bytes
;
4834 /* Get the destination. */
4835 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
4836 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
4837 error
= get_subfield(spec
->n_bits
, &p
, &spec
->dst
, vl_mff_map
,
4845 uint8_t *src_imm
= ofpbuf_put_zeros(ofpacts
,
4846 OFPACT_ALIGN(imm_bytes
));
4847 memcpy(src_imm
, imm
, imm_bytes
);
4849 learn
= ofpacts
->header
;
4852 ofpact_finish_LEARN(ofpacts
, &learn
);
4854 if (!is_all_zeros(p
, (char *) end
- (char *) p
)) {
4855 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4861 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
4862 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
4864 decode_NXAST_RAW_LEARN(const struct nx_action_learn
*nal
,
4865 enum ofp_version ofp_version OVS_UNUSED
,
4866 const struct vl_mff_map
*vl_mff_map
,
4867 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
4869 struct ofpact_learn
*learn
;
4872 learn
= ofpact_put_LEARN(ofpacts
);
4874 error
= decode_LEARN_common(nal
, NXAST_RAW_LEARN
, learn
);
4879 if (learn
->flags
& ~(NX_LEARN_F_SEND_FLOW_REM
|
4880 NX_LEARN_F_DELETE_LEARNED
)) {
4881 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4884 return decode_LEARN_specs(nal
+ 1, (char *) nal
+ ntohs(nal
->len
),
4885 vl_mff_map
, tlv_bitmap
, ofpacts
);
4888 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
4889 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
4891 decode_NXAST_RAW_LEARN2(const struct nx_action_learn2
*nal
,
4892 enum ofp_version ofp_version OVS_UNUSED
,
4893 const struct vl_mff_map
*vl_mff_map
,
4894 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
4896 struct ofpbuf b
= ofpbuf_const_initializer(nal
, ntohs(nal
->up
.len
));
4897 struct ofpact_learn
*learn
;
4901 return OFPERR_NXBAC_MUST_BE_ZERO
;
4904 learn
= ofpact_put_LEARN(ofpacts
);
4905 error
= decode_LEARN_common(&nal
->up
, NXAST_RAW_LEARN2
, learn
);
4910 learn
->limit
= ntohl(nal
->limit
);
4912 if (learn
->flags
& ~(NX_LEARN_F_SEND_FLOW_REM
|
4913 NX_LEARN_F_DELETE_LEARNED
|
4914 NX_LEARN_F_WRITE_RESULT
)) {
4915 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4918 ofpbuf_pull(&b
, sizeof *nal
);
4920 if (learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
4921 error
= nx_pull_header(&b
, vl_mff_map
, &learn
->result_dst
.field
, NULL
);
4925 if (!learn
->result_dst
.field
->writable
) {
4926 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
4928 learn
->result_dst
.ofs
= ntohs(nal
->result_dst_ofs
);
4929 learn
->result_dst
.n_bits
= 1;
4930 } else if (nal
->result_dst_ofs
) {
4931 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4934 return decode_LEARN_specs(b
.data
, (char *) nal
+ ntohs(nal
->up
.len
),
4935 vl_mff_map
, tlv_bitmap
, ofpacts
);
4939 put_be16(struct ofpbuf
*b
, ovs_be16 x
)
4941 ofpbuf_put(b
, &x
, sizeof x
);
4945 put_be32(struct ofpbuf
*b
, ovs_be32 x
)
4947 ofpbuf_put(b
, &x
, sizeof x
);
4951 put_u16(struct ofpbuf
*b
, uint16_t x
)
4953 put_be16(b
, htons(x
));
4957 put_u32(struct ofpbuf
*b
, uint32_t x
)
4959 put_be32(b
, htonl(x
));
4963 encode_LEARN(const struct ofpact_learn
*learn
,
4964 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4966 const struct ofpact_learn_spec
*spec
;
4967 struct nx_action_learn
*nal
;
4970 start_ofs
= out
->size
;
4972 if (learn
->ofpact
.raw
== NXAST_RAW_LEARN2
4973 || learn
->limit
!= 0
4974 || learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
4975 struct nx_action_learn2
*nal2
;
4977 nal2
= put_NXAST_LEARN2(out
);
4978 nal2
->limit
= htonl(learn
->limit
);
4979 nal2
->result_dst_ofs
= htons(learn
->result_dst
.ofs
);
4982 nal
= put_NXAST_LEARN(out
);
4984 nal
->idle_timeout
= htons(learn
->idle_timeout
);
4985 nal
->hard_timeout
= htons(learn
->hard_timeout
);
4986 nal
->fin_idle_timeout
= htons(learn
->fin_idle_timeout
);
4987 nal
->fin_hard_timeout
= htons(learn
->fin_hard_timeout
);
4988 nal
->priority
= htons(learn
->priority
);
4989 nal
->cookie
= learn
->cookie
;
4990 nal
->flags
= htons(learn
->flags
);
4991 nal
->table_id
= learn
->table_id
;
4993 if (learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
4994 nx_put_header(out
, learn
->result_dst
.field
->id
, 0, false);
4997 OFPACT_LEARN_SPEC_FOR_EACH (spec
, learn
) {
4998 put_u16(out
, spec
->n_bits
| spec
->dst_type
| spec
->src_type
);
5000 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
5001 put_u32(out
, nxm_header_from_mff(spec
->src
.field
));
5002 put_u16(out
, spec
->src
.ofs
);
5004 size_t n_dst_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
5005 uint8_t *bits
= ofpbuf_put_zeros(out
, n_dst_bytes
);
5006 unsigned int n_bytes
= DIV_ROUND_UP(spec
->n_bits
, 8);
5008 memcpy(bits
+ n_dst_bytes
- n_bytes
, ofpact_learn_spec_imm(spec
),
5012 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
5013 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
5014 put_u32(out
, nxm_header_from_mff(spec
->dst
.field
));
5015 put_u16(out
, spec
->dst
.ofs
);
5019 pad_ofpat(out
, start_ofs
);
5022 static char * OVS_WARN_UNUSED_RESULT
5023 parse_LEARN(char *arg
, const struct ofpact_parse_params
*pp
)
5025 return learn_parse(arg
, pp
->port_map
, pp
->table_map
, pp
->ofpacts
);
5029 format_LEARN(const struct ofpact_learn
*a
,
5030 const struct ofpact_format_params
*fp
)
5032 learn_format(a
, fp
->port_map
, fp
->table_map
, fp
->s
);
5035 /* Action structure for NXAST_CONJUNCTION. */
5036 struct nx_action_conjunction
{
5037 ovs_be16 type
; /* OFPAT_VENDOR. */
5038 ovs_be16 len
; /* At least 16. */
5039 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5040 ovs_be16 subtype
; /* See enum ofp_raw_action_type. */
5045 OFP_ASSERT(sizeof(struct nx_action_conjunction
) == 16);
5048 add_conjunction(struct ofpbuf
*out
,
5049 uint32_t id
, uint8_t clause
, uint8_t n_clauses
)
5051 struct ofpact_conjunction
*oc
;
5053 oc
= ofpact_put_CONJUNCTION(out
);
5055 oc
->clause
= clause
;
5056 oc
->n_clauses
= n_clauses
;
5060 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction
*nac
,
5061 enum ofp_version ofp_version OVS_UNUSED
,
5064 if (nac
->n_clauses
< 2 || nac
->n_clauses
> 64
5065 || nac
->clause
>= nac
->n_clauses
) {
5066 return OFPERR_NXBAC_BAD_CONJUNCTION
;
5068 add_conjunction(out
, ntohl(nac
->id
), nac
->clause
, nac
->n_clauses
);
5074 encode_CONJUNCTION(const struct ofpact_conjunction
*oc
,
5075 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
5077 struct nx_action_conjunction
*nac
= put_NXAST_CONJUNCTION(out
);
5078 nac
->clause
= oc
->clause
;
5079 nac
->n_clauses
= oc
->n_clauses
;
5080 nac
->id
= htonl(oc
->id
);
5084 format_CONJUNCTION(const struct ofpact_conjunction
*oc
,
5085 const struct ofpact_format_params
*fp
)
5087 ds_put_format(fp
->s
, "%sconjunction(%s%"PRIu32
",%d/%"PRIu8
"%s)%s",
5088 colors
.paren
, colors
.end
,
5089 oc
->id
, oc
->clause
+ 1, oc
->n_clauses
,
5090 colors
.paren
, colors
.end
);
5093 static char * OVS_WARN_UNUSED_RESULT
5094 parse_CONJUNCTION(const char *arg
, const struct ofpact_parse_params
*pp
)
5101 if (!ovs_scan(arg
, "%"SCNi32
" , %"SCNu8
" / %"SCNu8
" %n",
5102 &id
, &clause
, &n_clauses
, &n
) || n
!= strlen(arg
)) {
5103 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
5106 if (n_clauses
< 2) {
5107 return xstrdup("conjunction must have at least 2 clauses");
5108 } else if (n_clauses
> 64) {
5109 return xstrdup("conjunction must have at most 64 clauses");
5110 } else if (clause
< 1) {
5111 return xstrdup("clause index must be positive");
5112 } else if (clause
> n_clauses
) {
5113 return xstrdup("clause index must be less than or equal to "
5114 "number of clauses");
5117 add_conjunction(pp
->ofpacts
, id
, clause
- 1, n_clauses
);
5121 /* Action structure for NXAST_MULTIPATH.
5123 * This action performs the following steps in sequence:
5125 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
5126 * Refer to the definition of "enum nx_mp_fields" for details.
5128 * The 'basis' value is used as a universal hash parameter, that is,
5129 * different values of 'basis' yield different hash functions. The
5130 * particular universal hash function used is implementation-defined.
5132 * The hashed fields' values are drawn from the current state of the
5133 * flow, including all modifications that have been made by actions up to
5136 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
5137 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
5140 * The output of the algorithm is 'link', an unsigned integer less than
5141 * or equal to 'max_link'.
5143 * Some algorithms use 'arg' as an additional argument.
5145 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
5146 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
5149 * The switch will reject actions that have an unknown 'fields', or an unknown
5150 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
5151 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
5152 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
5154 struct nx_action_multipath
{
5155 ovs_be16 type
; /* OFPAT_VENDOR. */
5156 ovs_be16 len
; /* Length is 32. */
5157 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5158 ovs_be16 subtype
; /* NXAST_MULTIPATH. */
5160 /* What fields to hash and how. */
5161 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
5162 ovs_be16 basis
; /* Universal hash parameter. */
5165 /* Multipath link choice algorithm to apply to hash value. */
5166 ovs_be16 algorithm
; /* One of NX_MP_ALG_*. */
5167 ovs_be16 max_link
; /* Number of output links, minus 1. */
5168 ovs_be32 arg
; /* Algorithm-specific argument. */
5171 /* Where to store the result. */
5172 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
5173 ovs_be32 dst
; /* Destination. */
5175 OFP_ASSERT(sizeof(struct nx_action_multipath
) == 32);
5178 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath
*nam
,
5179 enum ofp_version ofp_version OVS_UNUSED
,
5180 const struct vl_mff_map
*vl_mff_map
,
5181 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
5183 uint32_t n_links
= ntohs(nam
->max_link
) + 1;
5184 size_t min_n_bits
= log_2_ceil(n_links
);
5185 struct ofpact_multipath
*mp
;
5188 mp
= ofpact_put_MULTIPATH(out
);
5189 mp
->fields
= ntohs(nam
->fields
);
5190 mp
->basis
= ntohs(nam
->basis
);
5191 mp
->algorithm
= ntohs(nam
->algorithm
);
5192 mp
->max_link
= ntohs(nam
->max_link
);
5193 mp
->arg
= ntohl(nam
->arg
);
5194 mp
->dst
.ofs
= nxm_decode_ofs(nam
->ofs_nbits
);
5195 mp
->dst
.n_bits
= nxm_decode_n_bits(nam
->ofs_nbits
);
5196 error
= mf_vl_mff_mf_from_nxm_header(ntohl(nam
->dst
), vl_mff_map
,
5197 &mp
->dst
.field
, tlv_bitmap
);
5202 if (!flow_hash_fields_valid(mp
->fields
)) {
5203 VLOG_WARN_RL(&rl
, "unsupported fields %d", (int) mp
->fields
);
5204 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5205 } else if (mp
->algorithm
!= NX_MP_ALG_MODULO_N
5206 && mp
->algorithm
!= NX_MP_ALG_HASH_THRESHOLD
5207 && mp
->algorithm
!= NX_MP_ALG_HRW
5208 && mp
->algorithm
!= NX_MP_ALG_ITER_HASH
) {
5209 VLOG_WARN_RL(&rl
, "unsupported algorithm %d", (int) mp
->algorithm
);
5210 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5211 } else if (mp
->dst
.n_bits
< min_n_bits
) {
5212 VLOG_WARN_RL(&rl
, "multipath action requires at least %"PRIuSIZE
" bits for "
5213 "%"PRIu32
" links", min_n_bits
, n_links
);
5214 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5217 return multipath_check(mp
, NULL
);
5221 encode_MULTIPATH(const struct ofpact_multipath
*mp
,
5222 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
5224 struct nx_action_multipath
*nam
= put_NXAST_MULTIPATH(out
);
5226 nam
->fields
= htons(mp
->fields
);
5227 nam
->basis
= htons(mp
->basis
);
5228 nam
->algorithm
= htons(mp
->algorithm
);
5229 nam
->max_link
= htons(mp
->max_link
);
5230 nam
->arg
= htonl(mp
->arg
);
5231 nam
->ofs_nbits
= nxm_encode_ofs_nbits(mp
->dst
.ofs
, mp
->dst
.n_bits
);
5232 nam
->dst
= htonl(nxm_header_from_mff(mp
->dst
.field
));
5235 static char * OVS_WARN_UNUSED_RESULT
5236 parse_MULTIPATH(const char *arg
, const struct ofpact_parse_params
*pp
)
5238 return multipath_parse(ofpact_put_MULTIPATH(pp
->ofpacts
), arg
);
5242 format_MULTIPATH(const struct ofpact_multipath
*a
,
5243 const struct ofpact_format_params
*fp
)
5245 multipath_format(a
, fp
->s
);
5248 /* Action structure for NXAST_NOTE.
5250 * This action has no effect. It is variable length. The switch does not
5251 * attempt to interpret the user-defined 'note' data in any way. A controller
5252 * can use this action to attach arbitrary metadata to a flow.
5254 * This action might go away in the future.
5256 struct nx_action_note
{
5257 ovs_be16 type
; /* OFPAT_VENDOR. */
5258 ovs_be16 len
; /* A multiple of 8, but at least 16. */
5259 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5260 ovs_be16 subtype
; /* NXAST_NOTE. */
5261 uint8_t note
[6]; /* Start of user-defined data. */
5262 /* Possibly followed by additional user-defined data. */
5264 OFP_ASSERT(sizeof(struct nx_action_note
) == 16);
5267 decode_NXAST_RAW_NOTE(const struct nx_action_note
*nan
,
5268 enum ofp_version ofp_version OVS_UNUSED
,
5271 struct ofpact_note
*note
;
5272 unsigned int length
;
5274 length
= ntohs(nan
->len
) - offsetof(struct nx_action_note
, note
);
5275 note
= ofpact_put_NOTE(out
);
5276 note
->length
= length
;
5277 ofpbuf_put(out
, nan
->note
, length
);
5279 ofpact_finish_NOTE(out
, ¬e
);
5285 encode_NOTE(const struct ofpact_note
*note
,
5286 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
5288 size_t start_ofs
= out
->size
;
5289 struct nx_action_note
*nan
;
5291 put_NXAST_NOTE(out
);
5292 out
->size
= out
->size
- sizeof nan
->note
;
5294 ofpbuf_put(out
, note
->data
, note
->length
);
5295 pad_ofpat(out
, start_ofs
);
5298 static char * OVS_WARN_UNUSED_RESULT
5299 parse_NOTE(const char *arg
, const struct ofpact_parse_params
*pp
)
5301 size_t start_ofs
= pp
->ofpacts
->size
;
5302 ofpact_put_NOTE(pp
->ofpacts
);
5303 arg
= ofpbuf_put_hex(pp
->ofpacts
, arg
, NULL
);
5305 return xstrdup("bad hex digit in `note' argument");
5307 struct ofpact_note
*note
= ofpbuf_at_assert(pp
->ofpacts
, start_ofs
,
5309 note
->length
= pp
->ofpacts
->size
- (start_ofs
+ sizeof *note
);
5310 ofpact_finish_NOTE(pp
->ofpacts
, ¬e
);
5315 format_NOTE(const struct ofpact_note
*a
,
5316 const struct ofpact_format_params
*fp
)
5318 ds_put_format(fp
->s
, "%snote:%s", colors
.param
, colors
.end
);
5319 format_hex_arg(fp
->s
, a
->data
, a
->length
);
5325 decode_NXAST_RAW_EXIT(struct ofpbuf
*out
)
5327 ofpact_put_EXIT(out
);
5332 encode_EXIT(const struct ofpact_null
*null OVS_UNUSED
,
5333 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
5335 put_NXAST_EXIT(out
);
5338 static char * OVS_WARN_UNUSED_RESULT
5339 parse_EXIT(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
5341 ofpact_put_EXIT(pp
->ofpacts
);
5346 format_EXIT(const struct ofpact_null
*a OVS_UNUSED
,
5347 const struct ofpact_format_params
*fp
)
5349 ds_put_format(fp
->s
, "%sexit%s", colors
.special
, colors
.end
);
5352 /* Unroll xlate action. */
5355 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate
*unroll OVS_UNUSED
,
5356 enum ofp_version ofp_version OVS_UNUSED
,
5357 struct ofpbuf
*out OVS_UNUSED
)
5362 static char * OVS_WARN_UNUSED_RESULT
5363 parse_UNROLL_XLATE(char *arg OVS_UNUSED
,
5364 const struct ofpact_parse_params
*pp OVS_UNUSED
)
5370 format_UNROLL_XLATE(const struct ofpact_unroll_xlate
*a
,
5371 const struct ofpact_format_params
*fp
)
5373 ds_put_format(fp
->s
, "%sunroll_xlate(%s%stable=%s",
5374 colors
.paren
, colors
.end
,
5375 colors
.special
, colors
.end
);
5376 ofputil_format_table(a
->rule_table_id
, fp
->table_map
, fp
->s
);
5377 ds_put_format(fp
->s
, ", %scookie=%s%"PRIu64
"%s)%s",
5378 colors
.param
, colors
.end
, ntohll(a
->rule_cookie
),
5379 colors
.paren
, colors
.end
);
5382 /* The NXAST_CLONE action is "struct ext_action_header", followed by zero or
5383 * more embedded OpenFlow actions. */
5386 decode_NXAST_RAW_CLONE(const struct ext_action_header
*eah
,
5387 enum ofp_version ofp_version
,
5388 const struct vl_mff_map
*vl_mff_map
,
5389 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
5392 struct ofpbuf openflow
;
5393 const size_t clone_offset
= ofpacts_pull(out
);
5394 struct ofpact_nest
*clone
= ofpact_put_CLONE(out
);
5396 /* decode action list */
5397 ofpbuf_pull(out
, sizeof(*clone
));
5398 openflow
= ofpbuf_const_initializer(
5399 eah
+ 1, ntohs(eah
->len
) - sizeof *eah
);
5400 error
= ofpacts_pull_openflow_actions__(&openflow
, openflow
.size
,
5402 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
5403 out
, 0, vl_mff_map
, tlv_bitmap
);
5404 clone
= ofpbuf_push_uninit(out
, sizeof *clone
);
5405 out
->header
= &clone
->ofpact
;
5406 ofpact_finish_CLONE(out
, &clone
);
5407 ofpbuf_push_uninit(out
, clone_offset
);
5412 encode_CLONE(const struct ofpact_nest
*clone
,
5413 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5416 const size_t ofs
= out
->size
;
5417 struct ext_action_header
*eah
;
5419 put_NXAST_CLONE(out
);
5420 len
= ofpacts_put_openflow_actions(clone
->actions
,
5421 ofpact_nest_get_action_len(clone
),
5424 eah
= ofpbuf_at(out
, ofs
, sizeof *eah
);
5425 eah
->len
= htons(len
);
5428 static char * OVS_WARN_UNUSED_RESULT
5429 parse_CLONE(char *arg
, const struct ofpact_parse_params
*pp
)
5431 const size_t clone_offset
= ofpacts_pull(pp
->ofpacts
);
5432 struct ofpact_nest
*clone
= ofpact_put_CLONE(pp
->ofpacts
);
5435 ofpbuf_pull(pp
->ofpacts
, sizeof *clone
);
5436 error
= ofpacts_parse_copy(arg
, pp
, false, 0);
5437 /* header points to the action list */
5438 pp
->ofpacts
->header
= ofpbuf_push_uninit(pp
->ofpacts
, sizeof *clone
);
5439 clone
= pp
->ofpacts
->header
;
5441 ofpact_finish_CLONE(pp
->ofpacts
, &clone
);
5442 ofpbuf_push_uninit(pp
->ofpacts
, clone_offset
);
5447 format_CLONE(const struct ofpact_nest
*a
,
5448 const struct ofpact_format_params
*fp
)
5450 ds_put_format(fp
->s
, "%sclone(%s", colors
.paren
, colors
.end
);
5451 ofpacts_format(a
->actions
, ofpact_nest_get_action_len(a
), fp
);
5452 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
5455 /* Action structure for NXAST_SAMPLE.
5457 * Samples matching packets with the given probability and sends them
5458 * each to the set of collectors identified with the given ID. The
5459 * probability is expressed as a number of packets to be sampled out
5460 * of USHRT_MAX packets, and must be >0.
5462 * When sending packet samples to IPFIX collectors, the IPFIX flow
5463 * record sent for each sampled packet is associated with the given
5464 * observation domain ID and observation point ID. Each IPFIX flow
5465 * record contain the sampled packet's headers when executing this
5466 * rule. If a sampled packet's headers are modified by previous
5467 * actions in the flow, those modified headers are sent. */
5468 struct nx_action_sample
{
5469 ovs_be16 type
; /* OFPAT_VENDOR. */
5470 ovs_be16 len
; /* Length is 24. */
5471 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5472 ovs_be16 subtype
; /* NXAST_SAMPLE. */
5473 ovs_be16 probability
; /* Fraction of packets to sample. */
5474 ovs_be32 collector_set_id
; /* ID of collector set in OVSDB. */
5475 ovs_be32 obs_domain_id
; /* ID of sampling observation domain. */
5476 ovs_be32 obs_point_id
; /* ID of sampling observation point. */
5478 OFP_ASSERT(sizeof(struct nx_action_sample
) == 24);
5480 /* Action structure for NXAST_SAMPLE2 and NXAST_SAMPLE3.
5482 * NXAST_SAMPLE2 was added in Open vSwitch 2.5.90. Compared to NXAST_SAMPLE,
5483 * it adds support for exporting egress tunnel information.
5485 * NXAST_SAMPLE3 was added in Open vSwitch 2.6.90. Compared to NXAST_SAMPLE2,
5486 * it adds support for the 'direction' field. */
5487 struct nx_action_sample2
{
5488 ovs_be16 type
; /* OFPAT_VENDOR. */
5489 ovs_be16 len
; /* Length is 32. */
5490 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5491 ovs_be16 subtype
; /* NXAST_SAMPLE. */
5492 ovs_be16 probability
; /* Fraction of packets to sample. */
5493 ovs_be32 collector_set_id
; /* ID of collector set in OVSDB. */
5494 ovs_be32 obs_domain_id
; /* ID of sampling observation domain. */
5495 ovs_be32 obs_point_id
; /* ID of sampling observation point. */
5496 ovs_be16 sampling_port
; /* Sampling port. */
5497 uint8_t direction
; /* NXAST_SAMPLE3 only. */
5498 uint8_t zeros
[5]; /* Pad to a multiple of 8 bytes */
5500 OFP_ASSERT(sizeof(struct nx_action_sample2
) == 32);
5503 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample
*nas
,
5504 enum ofp_version ofp_version OVS_UNUSED
,
5507 struct ofpact_sample
*sample
;
5509 sample
= ofpact_put_SAMPLE(out
);
5510 sample
->ofpact
.raw
= NXAST_RAW_SAMPLE
;
5511 sample
->probability
= ntohs(nas
->probability
);
5512 sample
->collector_set_id
= ntohl(nas
->collector_set_id
);
5513 sample
->obs_domain_id
= ntohl(nas
->obs_domain_id
);
5514 sample
->obs_point_id
= ntohl(nas
->obs_point_id
);
5515 sample
->sampling_port
= OFPP_NONE
;
5516 sample
->direction
= NX_ACTION_SAMPLE_DEFAULT
;
5518 if (sample
->probability
== 0) {
5519 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5526 decode_SAMPLE2(const struct nx_action_sample2
*nas
,
5527 enum ofp_raw_action_type raw
,
5528 enum nx_action_sample_direction direction
,
5529 struct ofpact_sample
*sample
)
5531 sample
->ofpact
.raw
= raw
;
5532 sample
->probability
= ntohs(nas
->probability
);
5533 sample
->collector_set_id
= ntohl(nas
->collector_set_id
);
5534 sample
->obs_domain_id
= ntohl(nas
->obs_domain_id
);
5535 sample
->obs_point_id
= ntohl(nas
->obs_point_id
);
5536 sample
->sampling_port
= u16_to_ofp(ntohs(nas
->sampling_port
));
5537 sample
->direction
= direction
;
5539 if (sample
->probability
== 0) {
5540 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5547 decode_NXAST_RAW_SAMPLE2(const struct nx_action_sample2
*nas
,
5548 enum ofp_version ofp_version OVS_UNUSED
,
5551 return decode_SAMPLE2(nas
, NXAST_RAW_SAMPLE2
, NX_ACTION_SAMPLE_DEFAULT
,
5552 ofpact_put_SAMPLE(out
));
5556 decode_NXAST_RAW_SAMPLE3(const struct nx_action_sample2
*nas
,
5557 enum ofp_version ofp_version OVS_UNUSED
,
5560 struct ofpact_sample
*sample
= ofpact_put_SAMPLE(out
);
5561 if (!is_all_zeros(nas
->zeros
, sizeof nas
->zeros
)) {
5562 return OFPERR_NXBRC_MUST_BE_ZERO
;
5564 if (nas
->direction
!= NX_ACTION_SAMPLE_DEFAULT
&&
5565 nas
->direction
!= NX_ACTION_SAMPLE_INGRESS
&&
5566 nas
->direction
!= NX_ACTION_SAMPLE_EGRESS
) {
5567 VLOG_WARN_RL(&rl
, "invalid sample direction %"PRIu8
, nas
->direction
);
5568 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5570 return decode_SAMPLE2(nas
, NXAST_RAW_SAMPLE3
, nas
->direction
, sample
);
5574 encode_SAMPLE2(const struct ofpact_sample
*sample
,
5575 struct nx_action_sample2
*nas
)
5577 nas
->probability
= htons(sample
->probability
);
5578 nas
->collector_set_id
= htonl(sample
->collector_set_id
);
5579 nas
->obs_domain_id
= htonl(sample
->obs_domain_id
);
5580 nas
->obs_point_id
= htonl(sample
->obs_point_id
);
5581 nas
->sampling_port
= htons(ofp_to_u16(sample
->sampling_port
));
5582 nas
->direction
= sample
->direction
;
5586 encode_SAMPLE(const struct ofpact_sample
*sample
,
5587 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
5589 if (sample
->ofpact
.raw
== NXAST_RAW_SAMPLE3
5590 || sample
->direction
!= NX_ACTION_SAMPLE_DEFAULT
) {
5591 encode_SAMPLE2(sample
, put_NXAST_SAMPLE3(out
));
5592 } else if (sample
->ofpact
.raw
== NXAST_RAW_SAMPLE2
5593 || sample
->sampling_port
!= OFPP_NONE
) {
5594 encode_SAMPLE2(sample
, put_NXAST_SAMPLE2(out
));
5596 struct nx_action_sample
*nas
= put_NXAST_SAMPLE(out
);
5597 nas
->probability
= htons(sample
->probability
);
5598 nas
->collector_set_id
= htonl(sample
->collector_set_id
);
5599 nas
->obs_domain_id
= htonl(sample
->obs_domain_id
);
5600 nas
->obs_point_id
= htonl(sample
->obs_point_id
);
5604 /* Parses 'arg' as the argument to a "sample" action, and appends such an
5605 * action to 'pp->ofpacts'.
5607 * Returns NULL if successful, otherwise a malloc()'d string describing the
5608 * error. The caller is responsible for freeing the returned string. */
5609 static char * OVS_WARN_UNUSED_RESULT
5610 parse_SAMPLE(char *arg
, const struct ofpact_parse_params
*pp
)
5612 struct ofpact_sample
*os
= ofpact_put_SAMPLE(pp
->ofpacts
);
5613 os
->sampling_port
= OFPP_NONE
;
5614 os
->direction
= NX_ACTION_SAMPLE_DEFAULT
;
5617 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
5620 if (!strcmp(key
, "probability")) {
5621 error
= str_to_u16(value
, "probability", &os
->probability
);
5622 if (!error
&& os
->probability
== 0) {
5623 error
= xasprintf("invalid probability value \"%s\"", value
);
5625 } else if (!strcmp(key
, "collector_set_id")) {
5626 error
= str_to_u32(value
, &os
->collector_set_id
);
5627 } else if (!strcmp(key
, "obs_domain_id")) {
5628 error
= str_to_u32(value
, &os
->obs_domain_id
);
5629 } else if (!strcmp(key
, "obs_point_id")) {
5630 error
= str_to_u32(value
, &os
->obs_point_id
);
5631 } else if (!strcmp(key
, "sampling_port")) {
5632 if (!ofputil_port_from_string(value
, pp
->port_map
,
5633 &os
->sampling_port
)) {
5634 error
= xasprintf("%s: unknown port", value
);
5636 } else if (!strcmp(key
, "ingress")) {
5637 os
->direction
= NX_ACTION_SAMPLE_INGRESS
;
5638 } else if (!strcmp(key
, "egress")) {
5639 os
->direction
= NX_ACTION_SAMPLE_EGRESS
;
5641 error
= xasprintf("invalid key \"%s\" in \"sample\" argument",
5648 if (os
->probability
== 0) {
5649 return xstrdup("non-zero \"probability\" must be specified on sample");
5656 format_SAMPLE(const struct ofpact_sample
*a
,
5657 const struct ofpact_format_params
*fp
)
5659 ds_put_format(fp
->s
, "%ssample(%s%sprobability=%s%"PRIu16
5660 ",%scollector_set_id=%s%"PRIu32
5661 ",%sobs_domain_id=%s%"PRIu32
5662 ",%sobs_point_id=%s%"PRIu32
,
5663 colors
.paren
, colors
.end
,
5664 colors
.param
, colors
.end
, a
->probability
,
5665 colors
.param
, colors
.end
, a
->collector_set_id
,
5666 colors
.param
, colors
.end
, a
->obs_domain_id
,
5667 colors
.param
, colors
.end
, a
->obs_point_id
);
5668 if (a
->sampling_port
!= OFPP_NONE
) {
5669 ds_put_format(fp
->s
, ",%ssampling_port=%s", colors
.param
, colors
.end
);
5670 ofputil_format_port(a
->sampling_port
, fp
->port_map
, fp
->s
);
5672 if (a
->direction
== NX_ACTION_SAMPLE_INGRESS
) {
5673 ds_put_format(fp
->s
, ",%singress%s", colors
.param
, colors
.end
);
5674 } else if (a
->direction
== NX_ACTION_SAMPLE_EGRESS
) {
5675 ds_put_format(fp
->s
, ",%segress%s", colors
.param
, colors
.end
);
5677 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
5680 /* debug instructions. */
5682 static bool enable_debug
;
5685 ofpact_dummy_enable(void)
5687 enable_debug
= true;
5691 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf
*out
)
5693 if (!enable_debug
) {
5694 return OFPERR_OFPBAC_BAD_VENDOR_TYPE
;
5697 ofpact_put_DEBUG_RECIRC(out
);
5702 encode_DEBUG_RECIRC(const struct ofpact_null
*n OVS_UNUSED
,
5703 enum ofp_version ofp_version OVS_UNUSED
,
5706 put_NXAST_DEBUG_RECIRC(out
);
5709 static char * OVS_WARN_UNUSED_RESULT
5710 parse_DEBUG_RECIRC(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
5712 ofpact_put_DEBUG_RECIRC(pp
->ofpacts
);
5717 format_DEBUG_RECIRC(const struct ofpact_null
*a OVS_UNUSED
,
5718 const struct ofpact_format_params
*fp
)
5720 ds_put_format(fp
->s
, "%sdebug_recirc%s", colors
.value
, colors
.end
);
5724 decode_NXAST_RAW_DEBUG_SLOW(struct ofpbuf
*out
)
5726 if (!enable_debug
) {
5727 return OFPERR_OFPBAC_BAD_VENDOR_TYPE
;
5730 ofpact_put_DEBUG_SLOW(out
);
5735 encode_DEBUG_SLOW(const struct ofpact_null
*n OVS_UNUSED
,
5736 enum ofp_version ofp_version OVS_UNUSED
,
5739 put_NXAST_DEBUG_SLOW(out
);
5742 static char * OVS_WARN_UNUSED_RESULT
5743 parse_DEBUG_SLOW(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
5745 ofpact_put_DEBUG_SLOW(pp
->ofpacts
);
5750 format_DEBUG_SLOW(const struct ofpact_null
*a OVS_UNUSED
,
5751 const struct ofpact_format_params
*fp
)
5753 ds_put_format(fp
->s
, "%sdebug_slow%s", colors
.value
, colors
.end
);
5756 /* Action structure for NXAST_CT.
5758 * Pass traffic to the connection tracker.
5760 * There are two important concepts to understanding the connection tracking
5761 * interface: Packet state and Connection state. Packets may be "Untracked" or
5762 * "Tracked". Connections may be "Uncommitted" or "Committed".
5766 * Untracked packets have an unknown connection state. In most
5767 * cases, packets entering the OpenFlow pipeline will initially be
5768 * in the untracked state. Untracked packets may become tracked by
5769 * executing NXAST_CT with a "recirc_table" specified. This makes
5770 * various aspects about the connection available, in particular
5771 * the connection state.
5773 * An NXAST_CT action always puts the packet into an untracked
5774 * state for the current processing path. If "recirc_table" is
5775 * set, execution is forked and the packet passes through the
5776 * connection tracker. The specified table's processing path is
5777 * able to match on Connection state until the end of the OpenFlow
5778 * pipeline or NXAST_CT is called again.
5780 * - Connection State:
5782 * Multiple packets may be associated with a single connection. Initially,
5783 * all connections are uncommitted. The connection state corresponding to
5784 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
5786 * Uncommitted connections have no state stored about them. Uncommitted
5787 * connections may transition into the committed state by executing
5788 * NXAST_CT with the NX_CT_F_COMMIT flag.
5790 * Once a connection becomes committed, information may be gathered about
5791 * the connection by passing subsequent packets through the connection
5792 * tracker, and the state of the connection will be stored beyond the
5793 * lifetime of packet processing.
5795 * A committed connection always has the directionality of the packet that
5796 * caused the connection to be committed in the first place. This is the
5797 * "original direction" of the connection, and the opposite direction is
5798 * the "reply direction". If a connection is already committed, but it is
5799 * then decided that the original direction should be the opposite of the
5800 * existing connection, NX_CT_F_FORCE flag may be used in addition to
5801 * NX_CT_F_COMMIT flag to in effect terminate the existing connection and
5802 * start a new one in the current direction.
5804 * Connections may transition back into the uncommitted state due to
5805 * external timers, or due to the contents of packets that are sent to the
5806 * connection tracker. This behaviour is outside of the scope of the
5807 * OpenFlow interface.
5809 * The "zone" specifies a context within which the tracking is done:
5811 * The connection tracking zone is a 16-bit number. Each zone is an
5812 * independent connection tracking context. The connection state for each
5813 * connection is completely separate for each zone, so if a connection
5814 * is committed to zone A, then it will remain uncommitted in zone B.
5815 * If NXAST_CT is executed with the same zone multiple times, later
5816 * executions have no effect.
5818 * If 'zone_src' is nonzero, this specifies that the zone should be
5819 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
5820 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
5821 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
5822 * are the same as the acceptable nxm_header values for the 'src' field of
5825 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
5826 * connection tracking zone.
5828 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
5830 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
5831 * packet will be logically cloned prior to executing this action. One
5832 * copy will be sent to the connection tracker, then will be re-injected
5833 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
5834 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
5835 * fields will be populated. The original instance of the packet will
5836 * continue the current actions list. This can be thought of as similar to
5837 * the effect of the "output" action: One copy is sent out (in this case,
5838 * to the connection tracker), but the current copy continues processing.
5840 * It is strongly recommended that this table is later than the current
5841 * table, to prevent loops.
5843 * The "alg" attaches protocol-specific behaviour to this action:
5845 * The ALG is a 16-bit number which specifies that additional
5846 * processing should be applied to this traffic.
5848 * Protocol | Value | Meaning
5849 * --------------------------------------------------------------------
5850 * None | 0 | No protocol-specific behaviour.
5851 * FTP | 21 | Parse FTP control connections and observe the
5852 * | | negotiation of related data connections.
5853 * Other | Other | Unsupported protocols.
5855 * By way of example, if FTP control connections have this action applied
5856 * with the ALG set to FTP (21), then the connection tracker will observe
5857 * the negotiation of data connections. This allows the connection
5858 * tracker to identify subsequent data connections as "related" to this
5859 * existing connection. The "related" flag will be populated in the
5860 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
5863 * Zero or more actions may immediately follow this action. These actions will
5864 * be executed within the context of the connection tracker, and they require
5865 * NX_CT_F_COMMIT flag be set.
5867 struct nx_action_conntrack
{
5868 ovs_be16 type
; /* OFPAT_VENDOR. */
5869 ovs_be16 len
; /* At least 24. */
5870 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5871 ovs_be16 subtype
; /* NXAST_CT. */
5872 ovs_be16 flags
; /* Zero or more NX_CT_F_* flags.
5873 * Unspecified flag bits must be zero. */
5874 ovs_be32 zone_src
; /* Connection tracking context. */
5876 ovs_be16 zone_ofs_nbits
;/* Range to use from source field. */
5877 ovs_be16 zone_imm
; /* Immediate value for zone. */
5879 uint8_t recirc_table
; /* Recirculate to a specific table, or
5880 NX_CT_RECIRC_NONE for no recirculation. */
5881 uint8_t pad
[3]; /* Zeroes */
5882 ovs_be16 alg
; /* Well-known port number for the protocol.
5883 * 0 indicates no ALG is required. */
5884 /* Followed by a sequence of zero or more OpenFlow actions. The length of
5885 * these is included in 'len'. */
5887 OFP_ASSERT(sizeof(struct nx_action_conntrack
) == 24);
5890 decode_ct_zone(const struct nx_action_conntrack
*nac
,
5891 struct ofpact_conntrack
*out
,
5892 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
)
5894 if (nac
->zone_src
) {
5897 out
->zone_src
.ofs
= nxm_decode_ofs(nac
->zone_ofs_nbits
);
5898 out
->zone_src
.n_bits
= nxm_decode_n_bits(nac
->zone_ofs_nbits
);
5899 error
= mf_vl_mff_mf_from_nxm_header(ntohl(nac
->zone_src
),
5900 vl_mff_map
, &out
->zone_src
.field
,
5906 error
= mf_check_src(&out
->zone_src
, NULL
);
5911 if (out
->zone_src
.n_bits
!= 16) {
5912 VLOG_WARN_RL(&rl
, "zone n_bits %d not within valid range [16..16]",
5913 out
->zone_src
.n_bits
);
5914 return OFPERR_OFPBAC_BAD_SET_LEN
;
5917 out
->zone_src
.field
= NULL
;
5918 out
->zone_imm
= ntohs(nac
->zone_imm
);
5925 decode_NXAST_RAW_CT(const struct nx_action_conntrack
*nac
,
5926 enum ofp_version ofp_version
,
5927 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
5930 const size_t ct_offset
= ofpacts_pull(out
);
5931 struct ofpact_conntrack
*conntrack
= ofpact_put_CT(out
);
5934 conntrack
->flags
= ntohs(nac
->flags
);
5935 if (conntrack
->flags
& NX_CT_F_FORCE
&&
5936 !(conntrack
->flags
& NX_CT_F_COMMIT
)) {
5937 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
5941 error
= decode_ct_zone(nac
, conntrack
, vl_mff_map
, tlv_bitmap
);
5945 conntrack
->recirc_table
= nac
->recirc_table
;
5946 conntrack
->alg
= ntohs(nac
->alg
);
5948 ofpbuf_pull(out
, sizeof(*conntrack
));
5950 struct ofpbuf openflow
= ofpbuf_const_initializer(
5951 nac
+ 1, ntohs(nac
->len
) - sizeof(*nac
));
5952 error
= ofpacts_pull_openflow_actions__(&openflow
, openflow
.size
,
5954 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
5955 out
, OFPACT_CT
, vl_mff_map
,
5961 conntrack
= ofpbuf_push_uninit(out
, sizeof(*conntrack
));
5962 out
->header
= &conntrack
->ofpact
;
5963 ofpact_finish_CT(out
, &conntrack
);
5965 if (conntrack
->ofpact
.len
> sizeof(*conntrack
)
5966 && !(conntrack
->flags
& NX_CT_F_COMMIT
)) {
5967 const struct ofpact
*a
;
5968 size_t ofpacts_len
= conntrack
->ofpact
.len
- sizeof(*conntrack
);
5970 OFPACT_FOR_EACH (a
, conntrack
->actions
, ofpacts_len
) {
5971 if (a
->type
!= OFPACT_NAT
|| ofpact_get_NAT(a
)->flags
5972 || ofpact_get_NAT(a
)->range_af
!= AF_UNSPEC
) {
5973 VLOG_WARN_RL(&rl
, "CT action requires commit flag if actions "
5974 "other than NAT without arguments are specified.");
5975 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
5982 ofpbuf_push_uninit(out
, ct_offset
);
5987 encode_CT(const struct ofpact_conntrack
*conntrack
,
5988 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5990 struct nx_action_conntrack
*nac
;
5991 const size_t ofs
= out
->size
;
5994 nac
= put_NXAST_CT(out
);
5995 nac
->flags
= htons(conntrack
->flags
);
5996 if (conntrack
->zone_src
.field
) {
5997 nac
->zone_src
= htonl(nxm_header_from_mff(conntrack
->zone_src
.field
));
5998 nac
->zone_ofs_nbits
= nxm_encode_ofs_nbits(conntrack
->zone_src
.ofs
,
5999 conntrack
->zone_src
.n_bits
);
6001 nac
->zone_src
= htonl(0);
6002 nac
->zone_imm
= htons(conntrack
->zone_imm
);
6004 nac
->recirc_table
= conntrack
->recirc_table
;
6005 nac
->alg
= htons(conntrack
->alg
);
6007 len
= ofpacts_put_openflow_actions(conntrack
->actions
,
6008 ofpact_ct_get_action_len(conntrack
),
6010 len
+= sizeof(*nac
);
6011 nac
= ofpbuf_at(out
, ofs
, sizeof(*nac
));
6012 nac
->len
= htons(len
);
6015 static char *OVS_WARN_UNUSED_RESULT
6016 parse_NAT(char *arg
, const struct ofpact_parse_params
*pp
);
6018 /* Parses 'arg' as the argument to a "ct" action, and appends such an
6019 * action to 'pp->ofpacts'.
6021 * Returns NULL if successful, otherwise a malloc()'d string describing the
6022 * error. The caller is responsible for freeing the returned string. */
6023 static char * OVS_WARN_UNUSED_RESULT
6024 parse_CT(char *arg
, const struct ofpact_parse_params
*pp
)
6026 const size_t ct_offset
= ofpacts_pull(pp
->ofpacts
);
6027 struct ofpact_conntrack
*oc
;
6031 oc
= ofpact_put_CT(pp
->ofpacts
);
6033 oc
->recirc_table
= NX_CT_RECIRC_NONE
;
6034 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
6035 if (!strcmp(key
, "commit")) {
6036 oc
->flags
|= NX_CT_F_COMMIT
;
6037 } else if (!strcmp(key
, "force")) {
6038 oc
->flags
|= NX_CT_F_FORCE
;
6039 } else if (!strcmp(key
, "table")) {
6040 if (!ofputil_table_from_string(value
, pp
->table_map
,
6041 &oc
->recirc_table
)) {
6042 error
= xasprintf("unknown table %s", value
);
6043 } else if (oc
->recirc_table
== NX_CT_RECIRC_NONE
) {
6044 error
= xasprintf("invalid table %#"PRIx8
, oc
->recirc_table
);
6046 } else if (!strcmp(key
, "zone")) {
6047 error
= str_to_u16(value
, "zone", &oc
->zone_imm
);
6051 error
= mf_parse_subfield(&oc
->zone_src
, value
);
6056 } else if (!strcmp(key
, "alg")) {
6057 error
= str_to_connhelper(value
, &oc
->alg
);
6058 } else if (!strcmp(key
, "nat")) {
6059 const size_t nat_offset
= ofpacts_pull(pp
->ofpacts
);
6061 error
= parse_NAT(value
, pp
);
6062 /* Update CT action pointer and length. */
6063 pp
->ofpacts
->header
= ofpbuf_push_uninit(pp
->ofpacts
, nat_offset
);
6064 oc
= pp
->ofpacts
->header
;
6065 } else if (!strcmp(key
, "exec")) {
6066 /* Hide existing actions from ofpacts_parse_copy(), so the
6067 * nesting can be handled transparently. */
6068 enum ofputil_protocol usable_protocols2
;
6069 const size_t exec_offset
= ofpacts_pull(pp
->ofpacts
);
6071 /* Initializes 'usable_protocol2', fold it back to
6072 * '*usable_protocols' afterwards, so that we do not lose
6073 * restrictions already in there. */
6074 struct ofpact_parse_params pp2
= *pp
;
6075 pp2
.usable_protocols
= &usable_protocols2
;
6076 error
= ofpacts_parse_copy(value
, &pp2
, false, OFPACT_CT
);
6077 *pp
->usable_protocols
&= usable_protocols2
;
6078 pp
->ofpacts
->header
= ofpbuf_push_uninit(pp
->ofpacts
, exec_offset
);
6079 oc
= pp
->ofpacts
->header
;
6081 error
= xasprintf("invalid argument to \"ct\" action: `%s'", key
);
6087 if (!error
&& oc
->flags
& NX_CT_F_FORCE
&& !(oc
->flags
& NX_CT_F_COMMIT
)) {
6088 error
= xasprintf("\"force\" flag requires \"commit\" flag.");
6090 ofpact_finish_CT(pp
->ofpacts
, &oc
);
6091 ofpbuf_push_uninit(pp
->ofpacts
, ct_offset
);
6096 format_alg(int port
, struct ds
*s
)
6100 ds_put_format(s
, "%salg=%sftp,", colors
.param
, colors
.end
);
6103 ds_put_format(s
, "%salg=%stftp,", colors
.param
, colors
.end
);
6109 ds_put_format(s
, "%salg=%s%d,", colors
.param
, colors
.end
, port
);
6114 static void format_NAT(const struct ofpact_nat
*,
6115 const struct ofpact_format_params
*fp
);
6118 format_CT(const struct ofpact_conntrack
*a
,
6119 const struct ofpact_format_params
*fp
)
6121 ds_put_format(fp
->s
, "%sct(%s", colors
.paren
, colors
.end
);
6122 if (a
->flags
& NX_CT_F_COMMIT
) {
6123 ds_put_format(fp
->s
, "%scommit%s,", colors
.value
, colors
.end
);
6125 if (a
->flags
& NX_CT_F_FORCE
) {
6126 ds_put_format(fp
->s
, "%sforce%s,", colors
.value
, colors
.end
);
6128 if (a
->recirc_table
!= NX_CT_RECIRC_NONE
) {
6129 ds_put_format(fp
->s
, "%stable=%s", colors
.special
, colors
.end
);
6130 ofputil_format_table(a
->recirc_table
, fp
->table_map
, fp
->s
);
6131 ds_put_char(fp
->s
, ',');
6133 if (a
->zone_src
.field
) {
6134 ds_put_format(fp
->s
, "%szone=%s", colors
.param
, colors
.end
);
6135 mf_format_subfield(&a
->zone_src
, fp
->s
);
6136 ds_put_char(fp
->s
, ',');
6137 } else if (a
->zone_imm
) {
6138 ds_put_format(fp
->s
, "%szone=%s%"PRIu16
",",
6139 colors
.param
, colors
.end
, a
->zone_imm
);
6141 /* If the first action is a NAT action, format it outside of the 'exec'
6143 const struct ofpact
*action
= a
->actions
;
6144 size_t actions_len
= ofpact_ct_get_action_len(a
);
6145 if (actions_len
&& action
->type
== OFPACT_NAT
) {
6146 format_NAT(ofpact_get_NAT(action
), fp
);
6147 ds_put_char(fp
->s
, ',');
6148 actions_len
-= OFPACT_ALIGN(action
->len
);
6149 action
= ofpact_next(action
);
6152 ds_put_format(fp
->s
, "%sexec(%s", colors
.paren
, colors
.end
);
6153 ofpacts_format(action
, actions_len
, fp
);
6154 ds_put_format(fp
->s
, "%s),%s", colors
.paren
, colors
.end
);
6156 format_alg(a
->alg
, fp
->s
);
6157 ds_chomp(fp
->s
, ',');
6158 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
6161 /* ct_clear action. */
6164 decode_NXAST_RAW_CT_CLEAR(struct ofpbuf
*out
)
6166 ofpact_put_CT_CLEAR(out
);
6171 encode_CT_CLEAR(const struct ofpact_null
*null OVS_UNUSED
,
6172 enum ofp_version ofp_version OVS_UNUSED
,
6175 put_NXAST_CT_CLEAR(out
);
6178 static char * OVS_WARN_UNUSED_RESULT
6179 parse_CT_CLEAR(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
6181 ofpact_put_CT_CLEAR(pp
->ofpacts
);
6186 format_CT_CLEAR(const struct ofpact_null
*a OVS_UNUSED
,
6187 const struct ofpact_format_params
*fp
)
6189 ds_put_format(fp
->s
, "%sct_clear%s", colors
.value
, colors
.end
);
6194 /* Which optional fields are present? */
6196 NX_NAT_RANGE_IPV4_MIN
= 1 << 0, /* ovs_be32 */
6197 NX_NAT_RANGE_IPV4_MAX
= 1 << 1, /* ovs_be32 */
6198 NX_NAT_RANGE_IPV6_MIN
= 1 << 2, /* struct in6_addr */
6199 NX_NAT_RANGE_IPV6_MAX
= 1 << 3, /* struct in6_addr */
6200 NX_NAT_RANGE_PROTO_MIN
= 1 << 4, /* ovs_be16 */
6201 NX_NAT_RANGE_PROTO_MAX
= 1 << 5, /* ovs_be16 */
6204 /* Action structure for NXAST_NAT. */
6205 struct nx_action_nat
{
6206 ovs_be16 type
; /* OFPAT_VENDOR. */
6207 ovs_be16 len
; /* At least 16. */
6208 ovs_be32 vendor
; /* NX_VENDOR_ID. */
6209 ovs_be16 subtype
; /* NXAST_NAT. */
6210 uint8_t pad
[2]; /* Must be zero. */
6211 ovs_be16 flags
; /* Zero or more NX_NAT_F_* flags.
6212 * Unspecified flag bits must be zero. */
6213 ovs_be16 range_present
; /* NX_NAT_RANGE_* */
6214 /* Followed by optional parameters as specified by 'range_present' */
6216 OFP_ASSERT(sizeof(struct nx_action_nat
) == 16);
6219 encode_NAT(const struct ofpact_nat
*nat
,
6220 enum ofp_version ofp_version OVS_UNUSED
,
6223 struct nx_action_nat
*nan
;
6224 const size_t ofs
= out
->size
;
6225 uint16_t range_present
= 0;
6227 nan
= put_NXAST_NAT(out
);
6228 nan
->flags
= htons(nat
->flags
);
6229 if (nat
->range_af
== AF_INET
) {
6230 if (nat
->range
.addr
.ipv4
.min
) {
6231 ovs_be32
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
6232 *min
= nat
->range
.addr
.ipv4
.min
;
6233 range_present
|= NX_NAT_RANGE_IPV4_MIN
;
6235 if (nat
->range
.addr
.ipv4
.max
) {
6236 ovs_be32
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
6237 *max
= nat
->range
.addr
.ipv4
.max
;
6238 range_present
|= NX_NAT_RANGE_IPV4_MAX
;
6240 } else if (nat
->range_af
== AF_INET6
) {
6241 if (!ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.min
)) {
6242 struct in6_addr
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
6243 *min
= nat
->range
.addr
.ipv6
.min
;
6244 range_present
|= NX_NAT_RANGE_IPV6_MIN
;
6246 if (!ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.max
)) {
6247 struct in6_addr
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
6248 *max
= nat
->range
.addr
.ipv6
.max
;
6249 range_present
|= NX_NAT_RANGE_IPV6_MAX
;
6252 if (nat
->range_af
!= AF_UNSPEC
) {
6253 if (nat
->range
.proto
.min
) {
6254 ovs_be16
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
6255 *min
= htons(nat
->range
.proto
.min
);
6256 range_present
|= NX_NAT_RANGE_PROTO_MIN
;
6258 if (nat
->range
.proto
.max
) {
6259 ovs_be16
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
6260 *max
= htons(nat
->range
.proto
.max
);
6261 range_present
|= NX_NAT_RANGE_PROTO_MAX
;
6264 pad_ofpat(out
, ofs
);
6265 nan
= ofpbuf_at(out
, ofs
, sizeof *nan
);
6266 nan
->range_present
= htons(range_present
);
6270 decode_NXAST_RAW_NAT(const struct nx_action_nat
*nan
,
6271 enum ofp_version ofp_version OVS_UNUSED
,
6274 struct ofpact_nat
*nat
;
6275 uint16_t range_present
= ntohs(nan
->range_present
);
6276 const char *opts
= (char *)(nan
+ 1);
6277 uint16_t len
= ntohs(nan
->len
) - sizeof *nan
;
6279 nat
= ofpact_put_NAT(out
);
6280 nat
->flags
= ntohs(nan
->flags
);
6282 /* Check for unknown or mutually exclusive flags. */
6283 if ((nat
->flags
& ~NX_NAT_F_MASK
)
6284 || (nat
->flags
& NX_NAT_F_SRC
&& nat
->flags
& NX_NAT_F_DST
)
6285 || (nat
->flags
& NX_NAT_F_PROTO_HASH
6286 && nat
->flags
& NX_NAT_F_PROTO_RANDOM
)) {
6287 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6290 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
6291 (LEN >= sizeof(TYPE) \
6292 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
6293 SRC += sizeof(TYPE)) \
6296 nat
->range_af
= AF_UNSPEC
;
6297 if (range_present
& NX_NAT_RANGE_IPV4_MIN
) {
6298 if (range_present
& (NX_NAT_RANGE_IPV6_MIN
| NX_NAT_RANGE_IPV6_MAX
)) {
6299 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6302 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv4
.min
, opts
, len
, ovs_be32
)
6303 || !nat
->range
.addr
.ipv4
.min
) {
6304 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6307 nat
->range_af
= AF_INET
;
6309 if (range_present
& NX_NAT_RANGE_IPV4_MAX
) {
6310 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv4
.max
, opts
, len
,
6312 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6314 if (ntohl(nat
->range
.addr
.ipv4
.max
)
6315 < ntohl(nat
->range
.addr
.ipv4
.min
)) {
6316 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6319 } else if (range_present
& NX_NAT_RANGE_IPV4_MAX
) {
6320 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6321 } else if (range_present
& NX_NAT_RANGE_IPV6_MIN
) {
6322 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv6
.min
, opts
, len
,
6324 || ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.min
)) {
6325 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6328 nat
->range_af
= AF_INET6
;
6330 if (range_present
& NX_NAT_RANGE_IPV6_MAX
) {
6331 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv6
.max
, opts
, len
,
6333 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6335 if (memcmp(&nat
->range
.addr
.ipv6
.max
, &nat
->range
.addr
.ipv6
.min
,
6336 sizeof(struct in6_addr
)) < 0) {
6337 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6340 } else if (range_present
& NX_NAT_RANGE_IPV6_MAX
) {
6341 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6344 if (range_present
& NX_NAT_RANGE_PROTO_MIN
) {
6347 if (nat
->range_af
== AF_UNSPEC
) {
6348 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6350 if (!NX_NAT_GET_OPT(&proto
, opts
, len
, ovs_be16
) || proto
== 0) {
6351 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6353 nat
->range
.proto
.min
= ntohs(proto
);
6354 if (range_present
& NX_NAT_RANGE_PROTO_MAX
) {
6355 if (!NX_NAT_GET_OPT(&proto
, opts
, len
, ovs_be16
)) {
6356 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6358 nat
->range
.proto
.max
= ntohs(proto
);
6359 if (nat
->range
.proto
.max
< nat
->range
.proto
.min
) {
6360 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6363 } else if (range_present
& NX_NAT_RANGE_PROTO_MAX
) {
6364 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6371 format_NAT(const struct ofpact_nat
*a
, const struct ofpact_format_params
*fp
)
6373 ds_put_format(fp
->s
, "%snat%s", colors
.paren
, colors
.end
);
6375 if (a
->flags
& (NX_NAT_F_SRC
| NX_NAT_F_DST
)) {
6376 ds_put_format(fp
->s
, "%s(%s", colors
.paren
, colors
.end
);
6377 ds_put_format(fp
->s
, a
->flags
& NX_NAT_F_SRC
? "%ssrc%s" : "%sdst%s",
6378 colors
.param
, colors
.end
);
6380 if (a
->range_af
!= AF_UNSPEC
) {
6381 ds_put_format(fp
->s
, "%s=%s", colors
.param
, colors
.end
);
6383 if (a
->range_af
== AF_INET
) {
6384 ds_put_format(fp
->s
, IP_FMT
, IP_ARGS(a
->range
.addr
.ipv4
.min
));
6386 if (a
->range
.addr
.ipv4
.max
6387 && a
->range
.addr
.ipv4
.max
!= a
->range
.addr
.ipv4
.min
) {
6388 ds_put_format(fp
->s
, "-"IP_FMT
,
6389 IP_ARGS(a
->range
.addr
.ipv4
.max
));
6391 } else if (a
->range_af
== AF_INET6
) {
6392 ipv6_format_addr_bracket(&a
->range
.addr
.ipv6
.min
, fp
->s
,
6393 a
->range
.proto
.min
);
6395 if (!ipv6_mask_is_any(&a
->range
.addr
.ipv6
.max
)
6396 && memcmp(&a
->range
.addr
.ipv6
.max
, &a
->range
.addr
.ipv6
.min
,
6397 sizeof(struct in6_addr
)) != 0) {
6398 ds_put_char(fp
->s
, '-');
6399 ipv6_format_addr_bracket(&a
->range
.addr
.ipv6
.max
, fp
->s
,
6400 a
->range
.proto
.min
);
6403 if (a
->range
.proto
.min
) {
6404 ds_put_char(fp
->s
, ':');
6405 ds_put_format(fp
->s
, "%"PRIu16
, a
->range
.proto
.min
);
6407 if (a
->range
.proto
.max
6408 && a
->range
.proto
.max
!= a
->range
.proto
.min
) {
6409 ds_put_format(fp
->s
, "-%"PRIu16
, a
->range
.proto
.max
);
6412 ds_put_char(fp
->s
, ',');
6414 if (a
->flags
& NX_NAT_F_PERSISTENT
) {
6415 ds_put_format(fp
->s
, "%spersistent%s,",
6416 colors
.value
, colors
.end
);
6418 if (a
->flags
& NX_NAT_F_PROTO_HASH
) {
6419 ds_put_format(fp
->s
, "%shash%s,", colors
.value
, colors
.end
);
6421 if (a
->flags
& NX_NAT_F_PROTO_RANDOM
) {
6422 ds_put_format(fp
->s
, "%srandom%s,", colors
.value
, colors
.end
);
6425 ds_chomp(fp
->s
, ',');
6426 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
6430 static char * OVS_WARN_UNUSED_RESULT
6431 str_to_nat_range(const char *s
, struct ofpact_nat
*on
)
6433 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
6436 on
->range_af
= AF_UNSPEC
;
6437 if (ovs_scan_len(s
, &n
, IP_SCAN_FMT
,
6438 IP_SCAN_ARGS(&on
->range
.addr
.ipv4
.min
))) {
6439 on
->range_af
= AF_INET
;
6443 if (!ovs_scan_len(s
, &n
, IP_SCAN_FMT
,
6444 IP_SCAN_ARGS(&on
->range
.addr
.ipv4
.max
))
6445 || (ntohl(on
->range
.addr
.ipv4
.max
)
6446 < ntohl(on
->range
.addr
.ipv4
.min
))) {
6450 } else if ((ovs_scan_len(s
, &n
, IPV6_SCAN_FMT
, ipv6_s
)
6451 || ovs_scan_len(s
, &n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
6452 && inet_pton(AF_INET6
, ipv6_s
, &on
->range
.addr
.ipv6
.min
) == 1) {
6453 on
->range_af
= AF_INET6
;
6457 if (!(ovs_scan_len(s
, &n
, IPV6_SCAN_FMT
, ipv6_s
)
6458 || ovs_scan_len(s
, &n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
6459 || inet_pton(AF_INET6
, ipv6_s
, &on
->range
.addr
.ipv6
.max
) != 1
6460 || memcmp(&on
->range
.addr
.ipv6
.max
, &on
->range
.addr
.ipv6
.min
,
6461 sizeof on
->range
.addr
.ipv6
.max
) < 0) {
6466 if (on
->range_af
!= AF_UNSPEC
&& s
[n
] == ':') {
6468 if (!ovs_scan_len(s
, &n
, "%"SCNu16
, &on
->range
.proto
.min
)) {
6473 if (!ovs_scan_len(s
, &n
, "%"SCNu16
, &on
->range
.proto
.max
)
6474 || on
->range
.proto
.max
< on
->range
.proto
.min
) {
6479 if (strlen(s
) != n
) {
6480 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
6485 return xasprintf("invalid nat range \"%s\"", s
);
6489 /* Parses 'arg' as the argument to a "nat" action, and appends such an
6490 * action to 'pp->ofpacts'.
6492 * Returns NULL if successful, otherwise a malloc()'d string describing the
6493 * error. The caller is responsible for freeing the returned string. */
6494 static char * OVS_WARN_UNUSED_RESULT
6495 parse_NAT(char *arg
, const struct ofpact_parse_params
*pp
)
6497 struct ofpact_nat
*on
= ofpact_put_NAT(pp
->ofpacts
);
6501 on
->range_af
= AF_UNSPEC
;
6503 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
6506 if (!strcmp(key
, "src")) {
6507 on
->flags
|= NX_NAT_F_SRC
;
6508 error
= str_to_nat_range(value
, on
);
6509 } else if (!strcmp(key
, "dst")) {
6510 on
->flags
|= NX_NAT_F_DST
;
6511 error
= str_to_nat_range(value
, on
);
6512 } else if (!strcmp(key
, "persistent")) {
6513 on
->flags
|= NX_NAT_F_PERSISTENT
;
6514 } else if (!strcmp(key
, "hash")) {
6515 on
->flags
|= NX_NAT_F_PROTO_HASH
;
6516 } else if (!strcmp(key
, "random")) {
6517 on
->flags
|= NX_NAT_F_PROTO_RANDOM
;
6519 error
= xasprintf("invalid key \"%s\" in \"nat\" argument",
6526 if (on
->flags
& NX_NAT_F_SRC
&& on
->flags
& NX_NAT_F_DST
) {
6527 return xasprintf("May only specify one of \"src\" or \"dst\".");
6529 if (!(on
->flags
& NX_NAT_F_SRC
|| on
->flags
& NX_NAT_F_DST
)) {
6531 return xasprintf("Flags allowed only with \"src\" or \"dst\".");
6533 if (on
->range_af
!= AF_UNSPEC
) {
6534 return xasprintf("Range allowed only with \"src\" or \"dst\".");
6537 if (on
->flags
& NX_NAT_F_PROTO_HASH
&& on
->flags
& NX_NAT_F_PROTO_RANDOM
) {
6538 return xasprintf("Both \"hash\" and \"random\" are not allowed.");
6544 /* Truncate output action. */
6545 struct nx_action_output_trunc
{
6546 ovs_be16 type
; /* OFPAT_VENDOR. */
6547 ovs_be16 len
; /* At least 16. */
6548 ovs_be32 vendor
; /* NX_VENDOR_ID. */
6549 ovs_be16 subtype
; /* NXAST_OUTPUT_TRUNC. */
6550 ovs_be16 port
; /* Output port */
6551 ovs_be32 max_len
; /* Truncate packet to size bytes */
6553 OFP_ASSERT(sizeof(struct nx_action_output_trunc
) == 16);
6556 decode_NXAST_RAW_OUTPUT_TRUNC(const struct nx_action_output_trunc
*natrc
,
6557 enum ofp_version ofp_version OVS_UNUSED
,
6560 struct ofpact_output_trunc
*output_trunc
;
6562 output_trunc
= ofpact_put_OUTPUT_TRUNC(out
);
6563 output_trunc
->max_len
= ntohl(natrc
->max_len
);
6564 output_trunc
->port
= u16_to_ofp(ntohs(natrc
->port
));
6566 if (output_trunc
->max_len
< ETH_HEADER_LEN
) {
6567 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6573 encode_OUTPUT_TRUNC(const struct ofpact_output_trunc
*output_trunc
,
6574 enum ofp_version ofp_version OVS_UNUSED
,
6577 struct nx_action_output_trunc
*natrc
= put_NXAST_OUTPUT_TRUNC(out
);
6579 natrc
->max_len
= htonl(output_trunc
->max_len
);
6580 natrc
->port
= htons(ofp_to_u16(output_trunc
->port
));
6583 static char * OVS_WARN_UNUSED_RESULT
6584 parse_OUTPUT_TRUNC(const char *arg
,
6585 const struct ofpact_parse_params
*pp OVS_UNUSED
)
6587 /* Disable output_trunc parsing. Expose as output(port=N,max_len=M) and
6588 * reuse parse_OUTPUT to parse output_trunc action. */
6589 return xasprintf("unknown action %s", arg
);
6593 format_OUTPUT_TRUNC(const struct ofpact_output_trunc
*a
,
6594 const struct ofpact_format_params
*fp
)
6596 ds_put_format(fp
->s
, "%soutput%s(port=", colors
.special
, colors
.end
);
6597 ofputil_format_port(a
->port
, fp
->port_map
, fp
->s
);
6598 ds_put_format(fp
->s
, ",max_len=%"PRIu32
")", a
->max_len
);
6602 /* Meter instruction. */
6605 encode_METER(const struct ofpact_meter
*meter
,
6606 enum ofp_version ofp_version
, struct ofpbuf
*out
)
6608 if (ofp_version
>= OFP13_VERSION
) {
6609 instruction_put_OFPIT13_METER(out
)->meter_id
= htonl(meter
->meter_id
);
6613 static char * OVS_WARN_UNUSED_RESULT
6614 parse_METER(char *arg
, const struct ofpact_parse_params
*pp
)
6616 *pp
->usable_protocols
&= OFPUTIL_P_OF13_UP
;
6617 return str_to_u32(arg
, &ofpact_put_METER(pp
->ofpacts
)->meter_id
);
6621 format_METER(const struct ofpact_meter
*a
,
6622 const struct ofpact_format_params
*fp
)
6624 ds_put_format(fp
->s
, "%smeter:%s%"PRIu32
,
6625 colors
.param
, colors
.end
, a
->meter_id
);
6628 /* Clear-Actions instruction. */
6631 encode_CLEAR_ACTIONS(const struct ofpact_null
*null OVS_UNUSED
,
6632 enum ofp_version ofp_version OVS_UNUSED
,
6633 struct ofpbuf
*out OVS_UNUSED
)
6635 if (ofp_version
> OFP10_VERSION
) {
6636 instruction_put_OFPIT11_CLEAR_ACTIONS(out
);
6640 static char * OVS_WARN_UNUSED_RESULT
6641 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
6643 ofpact_put_CLEAR_ACTIONS(pp
->ofpacts
);
6648 format_CLEAR_ACTIONS(const struct ofpact_null
*a OVS_UNUSED
,
6649 const struct ofpact_format_params
*fp
)
6651 ds_put_format(fp
->s
, "%sclear_actions%s", colors
.value
, colors
.end
);
6654 /* Write-Actions instruction. */
6657 encode_WRITE_ACTIONS(const struct ofpact_nest
*actions
,
6658 enum ofp_version ofp_version
, struct ofpbuf
*out
)
6660 if (ofp_version
> OFP10_VERSION
) {
6661 const size_t ofs
= out
->size
;
6663 instruction_put_OFPIT11_WRITE_ACTIONS(out
);
6664 ofpacts_put_openflow_actions(actions
->actions
,
6665 ofpact_nest_get_action_len(actions
),
6667 ofpacts_update_instruction_actions(out
, ofs
);
6671 static char * OVS_WARN_UNUSED_RESULT
6672 parse_WRITE_ACTIONS(char *arg
, const struct ofpact_parse_params
*pp
)
6674 size_t ofs
= ofpacts_pull(pp
->ofpacts
);
6675 struct ofpact_nest
*on
;
6678 /* Add a Write-Actions instruction and then pull it off. */
6679 ofpact_put(pp
->ofpacts
, OFPACT_WRITE_ACTIONS
, sizeof *on
);
6680 ofpbuf_pull(pp
->ofpacts
, sizeof *on
);
6682 /* Parse nested actions.
6684 * We pulled off "write-actions" and the previous actions because the
6685 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
6686 * that it doesn't actually include the nested actions. That means that
6687 * ofpacts_parse() would reject them as being part of an Apply-Actions that
6688 * follows a Write-Actions, which is an invalid order. */
6689 error
= ofpacts_parse(arg
, pp
, false, OFPACT_WRITE_ACTIONS
);
6691 /* Put the Write-Actions back on and update its length. */
6692 on
= ofpbuf_push_uninit(pp
->ofpacts
, sizeof *on
);
6693 on
->ofpact
.len
= pp
->ofpacts
->size
;
6695 /* Put any previous actions or instructions back on. */
6696 ofpbuf_push_uninit(pp
->ofpacts
, ofs
);
6702 format_WRITE_ACTIONS(const struct ofpact_nest
*a
,
6703 const struct ofpact_format_params
*fp
)
6705 ds_put_format(fp
->s
, "%swrite_actions(%s", colors
.paren
, colors
.end
);
6706 ofpacts_format(a
->actions
, ofpact_nest_get_action_len(a
), fp
);
6707 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
6710 /* Action structure for NXAST_WRITE_METADATA.
6712 * Modifies the 'mask' bits of the metadata value. */
6713 struct nx_action_write_metadata
{
6714 ovs_be16 type
; /* OFPAT_VENDOR. */
6715 ovs_be16 len
; /* Length is 32. */
6716 ovs_be32 vendor
; /* NX_VENDOR_ID. */
6717 ovs_be16 subtype
; /* NXAST_WRITE_METADATA. */
6718 uint8_t zeros
[6]; /* Must be zero. */
6719 ovs_be64 metadata
; /* Metadata register. */
6720 ovs_be64 mask
; /* Metadata mask. */
6722 OFP_ASSERT(sizeof(struct nx_action_write_metadata
) == 32);
6725 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata
*nawm
,
6726 enum ofp_version ofp_version OVS_UNUSED
,
6729 struct ofpact_metadata
*om
;
6731 if (!is_all_zeros(nawm
->zeros
, sizeof nawm
->zeros
)) {
6732 return OFPERR_NXBRC_MUST_BE_ZERO
;
6735 om
= ofpact_put_WRITE_METADATA(out
);
6736 om
->metadata
= nawm
->metadata
;
6737 om
->mask
= nawm
->mask
;
6743 encode_WRITE_METADATA(const struct ofpact_metadata
*metadata
,
6744 enum ofp_version ofp_version
, struct ofpbuf
*out
)
6746 if (ofp_version
== OFP10_VERSION
) {
6747 struct nx_action_write_metadata
*nawm
;
6749 nawm
= put_NXAST_WRITE_METADATA(out
);
6750 nawm
->metadata
= metadata
->metadata
;
6751 nawm
->mask
= metadata
->mask
;
6753 struct ofp11_instruction_write_metadata
*oiwm
;
6755 oiwm
= instruction_put_OFPIT11_WRITE_METADATA(out
);
6756 oiwm
->metadata
= metadata
->metadata
;
6757 oiwm
->metadata_mask
= metadata
->mask
;
6761 static char * OVS_WARN_UNUSED_RESULT
6762 parse_WRITE_METADATA(char *arg
, const struct ofpact_parse_params
*pp
)
6764 struct ofpact_metadata
*om
;
6765 char *mask
= strchr(arg
, '/');
6767 *pp
->usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
6769 om
= ofpact_put_WRITE_METADATA(pp
->ofpacts
);
6774 error
= str_to_be64(mask
+ 1, &om
->mask
);
6779 om
->mask
= OVS_BE64_MAX
;
6782 return str_to_be64(arg
, &om
->metadata
);
6786 format_WRITE_METADATA(const struct ofpact_metadata
*a
,
6787 const struct ofpact_format_params
*fp
)
6789 ds_put_format(fp
->s
, "%swrite_metadata:%s%#"PRIx64
,
6790 colors
.param
, colors
.end
, ntohll(a
->metadata
));
6791 if (a
->mask
!= OVS_BE64_MAX
) {
6792 ds_put_format(fp
->s
, "/%#"PRIx64
, ntohll(a
->mask
));
6796 /* Goto-Table instruction. */
6799 encode_GOTO_TABLE(const struct ofpact_goto_table
*goto_table
,
6800 enum ofp_version ofp_version
, struct ofpbuf
*out
)
6802 if (ofp_version
== OFP10_VERSION
) {
6803 struct nx_action_resubmit
*nar
;
6805 nar
= put_NXAST_RESUBMIT_TABLE(out
);
6806 nar
->table
= goto_table
->table_id
;
6807 nar
->in_port
= htons(ofp_to_u16(OFPP_IN_PORT
));
6809 struct ofp11_instruction_goto_table
*oigt
;
6811 oigt
= instruction_put_OFPIT11_GOTO_TABLE(out
);
6812 oigt
->table_id
= goto_table
->table_id
;
6813 memset(oigt
->pad
, 0, sizeof oigt
->pad
);
6817 static char * OVS_WARN_UNUSED_RESULT
6818 parse_GOTO_TABLE(char *arg
, const struct ofpact_parse_params
*pp
)
6820 struct ofpact_goto_table
*ogt
= ofpact_put_GOTO_TABLE(pp
->ofpacts
);
6821 if (!ofputil_table_from_string(arg
, pp
->table_map
, &ogt
->table_id
)) {
6822 return xasprintf("unknown table \"%s\"", arg
);
6828 format_GOTO_TABLE(const struct ofpact_goto_table
*a
,
6829 const struct ofpact_format_params
*fp
)
6831 ds_put_format(fp
->s
, "%sgoto_table:%s", colors
.param
, colors
.end
);
6832 ofputil_format_table(a
->table_id
, fp
->table_map
, fp
->s
);
6836 log_bad_action(const struct ofp_action_header
*actions
, size_t actions_len
,
6837 const struct ofp_action_header
*bad_action
, enum ofperr error
)
6839 if (!VLOG_DROP_WARN(&rl
)) {
6843 ds_put_hex_dump(&s
, actions
, actions_len
, 0, false);
6844 VLOG_WARN("bad action at offset %#"PRIxPTR
" (%s):\n%s",
6845 (char *)bad_action
- (char *)actions
,
6846 ofperr_get_name(error
), ds_cstr(&s
));
6852 ofpacts_decode(const void *actions
, size_t actions_len
,
6853 enum ofp_version ofp_version
,
6854 const struct vl_mff_map
*vl_mff_map
,
6855 uint64_t *ofpacts_tlv_bitmap
, struct ofpbuf
*ofpacts
)
6857 struct ofpbuf openflow
= ofpbuf_const_initializer(actions
, actions_len
);
6858 while (openflow
.size
) {
6859 const struct ofp_action_header
*action
= openflow
.data
;
6860 enum ofp_raw_action_type raw
;
6864 error
= ofpact_pull_raw(&openflow
, ofp_version
, &raw
, &arg
);
6866 error
= ofpact_decode(action
, raw
, ofp_version
, arg
, vl_mff_map
,
6867 ofpacts_tlv_bitmap
, ofpacts
);
6871 log_bad_action(actions
, actions_len
, action
, error
);
6879 ofpacts_pull_openflow_actions__(struct ofpbuf
*openflow
,
6880 unsigned int actions_len
,
6881 enum ofp_version version
,
6882 uint32_t allowed_ovsinsts
,
6883 struct ofpbuf
*ofpacts
,
6884 enum ofpact_type outer_action
,
6885 const struct vl_mff_map
*vl_mff_map
,
6886 uint64_t *ofpacts_tlv_bitmap
)
6888 const struct ofp_action_header
*actions
;
6889 size_t orig_size
= ofpacts
->size
;
6892 if (actions_len
% OFP_ACTION_ALIGN
!= 0) {
6893 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u is not a "
6894 "multiple of %d", actions_len
, OFP_ACTION_ALIGN
);
6895 return OFPERR_OFPBRC_BAD_LEN
;
6898 actions
= ofpbuf_try_pull(openflow
, actions_len
);
6899 if (actions
== NULL
) {
6900 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u exceeds "
6901 "remaining message length (%"PRIu32
")",
6902 actions_len
, openflow
->size
);
6903 return OFPERR_OFPBRC_BAD_LEN
;
6906 error
= ofpacts_decode(actions
, actions_len
, version
, vl_mff_map
,
6907 ofpacts_tlv_bitmap
, ofpacts
);
6909 ofpacts
->size
= orig_size
;
6913 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
, allowed_ovsinsts
,
6916 ofpacts
->size
= orig_size
;
6921 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
6922 * of 'openflow' into ofpacts. On success, appends the converted actions to
6923 * 'ofpacts'; on failure, 'ofpacts' is unchanged (but might be reallocated) .
6924 * Returns 0 if successful, otherwise an OpenFlow error.
6926 * Actions are processed according to their OpenFlow version which
6927 * is provided in the 'version' parameter.
6929 * In most places in OpenFlow, actions appear encapsulated in instructions, so
6930 * you should call ofpacts_pull_openflow_instructions() instead of this
6933 * 'vl_mff_map' and 'ofpacts_tlv_bitmap' are optional. If 'vl_mff_map' is
6934 * provided, it is used to get variable length mf_fields with configured
6935 * length in the actions. If an action uses a variable length mf_field,
6936 * 'ofpacts_tlv_bitmap' is updated accordingly for ref counting. If
6937 * 'vl_mff_map' is not provided, the default mf_fields with maximum length
6940 * The parsed actions are valid generically, but they may not be valid in a
6941 * specific context. For example, port numbers up to OFPP_MAX are valid
6942 * generically, but specific datapaths may only support port numbers in a
6943 * smaller range. Use ofpacts_check() to additional check whether actions are
6944 * valid in a specific context. */
6946 ofpacts_pull_openflow_actions(struct ofpbuf
*openflow
,
6947 unsigned int actions_len
,
6948 enum ofp_version version
,
6949 const struct vl_mff_map
*vl_mff_map
,
6950 uint64_t *ofpacts_tlv_bitmap
,
6951 struct ofpbuf
*ofpacts
)
6953 return ofpacts_pull_openflow_actions__(openflow
, actions_len
, version
,
6954 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
6955 ofpacts
, 0, vl_mff_map
,
6956 ofpacts_tlv_bitmap
);
6959 /* OpenFlow 1.1 actions. */
6962 /* True if an action sets the value of a field
6963 * in a way that is compatibile with the action set.
6964 * The field can be set via either a set or a move action.
6965 * False otherwise. */
6967 ofpact_is_set_or_move_action(const struct ofpact
*a
)
6970 case OFPACT_SET_FIELD
:
6971 case OFPACT_REG_MOVE
:
6972 case OFPACT_SET_ETH_DST
:
6973 case OFPACT_SET_ETH_SRC
:
6974 case OFPACT_SET_IP_DSCP
:
6975 case OFPACT_SET_IP_ECN
:
6976 case OFPACT_SET_IP_TTL
:
6977 case OFPACT_SET_IPV4_DST
:
6978 case OFPACT_SET_IPV4_SRC
:
6979 case OFPACT_SET_L4_DST_PORT
:
6980 case OFPACT_SET_L4_SRC_PORT
:
6981 case OFPACT_SET_MPLS_LABEL
:
6982 case OFPACT_SET_MPLS_TC
:
6983 case OFPACT_SET_MPLS_TTL
:
6984 case OFPACT_SET_QUEUE
:
6985 case OFPACT_SET_TUNNEL
:
6986 case OFPACT_SET_VLAN_PCP
:
6987 case OFPACT_SET_VLAN_VID
:
6990 case OFPACT_DEC_NSH_TTL
:
6993 case OFPACT_CLEAR_ACTIONS
:
6995 case OFPACT_CT_CLEAR
:
6998 case OFPACT_CONTROLLER
:
6999 case OFPACT_DEC_MPLS_TTL
:
7000 case OFPACT_DEC_TTL
:
7001 case OFPACT_ENQUEUE
:
7003 case OFPACT_UNROLL_XLATE
:
7004 case OFPACT_FIN_TIMEOUT
:
7005 case OFPACT_GOTO_TABLE
:
7008 case OFPACT_CONJUNCTION
:
7010 case OFPACT_MULTIPATH
:
7013 case OFPACT_OUTPUT_REG
:
7014 case OFPACT_OUTPUT_TRUNC
:
7015 case OFPACT_POP_MPLS
:
7016 case OFPACT_POP_QUEUE
:
7017 case OFPACT_PUSH_MPLS
:
7018 case OFPACT_PUSH_VLAN
:
7019 case OFPACT_RESUBMIT
:
7021 case OFPACT_STACK_POP
:
7022 case OFPACT_STACK_PUSH
:
7023 case OFPACT_STRIP_VLAN
:
7024 case OFPACT_WRITE_ACTIONS
:
7025 case OFPACT_WRITE_METADATA
:
7026 case OFPACT_DEBUG_RECIRC
:
7027 case OFPACT_DEBUG_SLOW
:
7034 /* True if an action is allowed in the action set.
7035 * False otherwise. */
7037 ofpact_is_allowed_in_actions_set(const struct ofpact
*a
)
7040 case OFPACT_DEC_MPLS_TTL
:
7041 case OFPACT_DEC_TTL
:
7044 case OFPACT_OUTPUT_TRUNC
:
7045 case OFPACT_POP_MPLS
:
7046 case OFPACT_PUSH_MPLS
:
7047 case OFPACT_PUSH_VLAN
:
7048 case OFPACT_REG_MOVE
:
7049 case OFPACT_SET_FIELD
:
7050 case OFPACT_SET_ETH_DST
:
7051 case OFPACT_SET_ETH_SRC
:
7052 case OFPACT_SET_IP_DSCP
:
7053 case OFPACT_SET_IP_ECN
:
7054 case OFPACT_SET_IP_TTL
:
7055 case OFPACT_SET_IPV4_DST
:
7056 case OFPACT_SET_IPV4_SRC
:
7057 case OFPACT_SET_L4_DST_PORT
:
7058 case OFPACT_SET_L4_SRC_PORT
:
7059 case OFPACT_SET_MPLS_LABEL
:
7060 case OFPACT_SET_MPLS_TC
:
7061 case OFPACT_SET_MPLS_TTL
:
7062 case OFPACT_SET_QUEUE
:
7063 case OFPACT_SET_TUNNEL
:
7064 case OFPACT_SET_VLAN_PCP
:
7065 case OFPACT_SET_VLAN_VID
:
7066 case OFPACT_STRIP_VLAN
:
7069 case OFPACT_DEC_NSH_TTL
:
7072 /* In general these actions are excluded because they are not part of
7073 * the OpenFlow specification nor map to actions that are defined in
7074 * the specification. Thus the order in which they should be applied
7075 * in the action set is undefined. */
7078 case OFPACT_CONTROLLER
:
7080 case OFPACT_CT_CLEAR
:
7082 case OFPACT_ENQUEUE
:
7084 case OFPACT_UNROLL_XLATE
:
7085 case OFPACT_FIN_TIMEOUT
:
7087 case OFPACT_CONJUNCTION
:
7088 case OFPACT_MULTIPATH
:
7090 case OFPACT_OUTPUT_REG
:
7091 case OFPACT_POP_QUEUE
:
7092 case OFPACT_RESUBMIT
:
7094 case OFPACT_STACK_POP
:
7095 case OFPACT_STACK_PUSH
:
7096 case OFPACT_DEBUG_RECIRC
:
7097 case OFPACT_DEBUG_SLOW
:
7099 /* The action set may only include actions and thus
7100 * may not include any instructions */
7101 case OFPACT_CLEAR_ACTIONS
:
7102 case OFPACT_GOTO_TABLE
:
7104 case OFPACT_WRITE_ACTIONS
:
7105 case OFPACT_WRITE_METADATA
:
7112 /* Append ofpact 'a' onto the tail of 'out' */
7114 ofpact_copy(struct ofpbuf
*out
, const struct ofpact
*a
)
7116 ofpbuf_put(out
, a
, OFPACT_ALIGN(a
->len
));
7119 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
7121 ofpacts_copy_last(struct ofpbuf
*out
, const struct ofpbuf
*in
,
7122 enum ofpact_type filter
)
7124 const struct ofpact
*target
;
7125 const struct ofpact
*a
;
7128 OFPACT_FOR_EACH (a
, in
->data
, in
->size
) {
7129 if (a
->type
== filter
) {
7134 ofpact_copy(out
, target
);
7136 return target
!= NULL
;
7139 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
7140 * The order of appended ofpacts is preserved between 'in' and 'out' */
7142 ofpacts_copy_all(struct ofpbuf
*out
, const struct ofpbuf
*in
,
7143 bool (*filter
)(const struct ofpact
*))
7145 const struct ofpact
*a
;
7147 OFPACT_FOR_EACH (a
, in
->data
, in
->size
) {
7149 ofpact_copy(out
, a
);
7154 /* Reads 'action_set', which contains ofpacts accumulated by
7155 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
7156 * executed directly into 'action_list'. (These names correspond to the
7157 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
7159 * In general this involves appending the last instance of each action that is
7160 * admissible in the action set in the order described in the OpenFlow
7164 * + output action is only appended if no group action was present in 'in'.
7165 * + As a simplification all set actions are copied in the order the are
7166 * provided in 'in' as many set actions applied to a field has the same
7167 * affect as only applying the last action that sets a field and
7168 * duplicates are removed by do_xlate_actions().
7169 * This has an unwanted side-effect of compsoting multiple
7170 * LOAD_REG actions that touch different regions of the same field. */
7172 ofpacts_execute_action_set(struct ofpbuf
*action_list
,
7173 const struct ofpbuf
*action_set
)
7175 /* The OpenFlow spec "Action Set" section specifies this order. */
7176 ofpacts_copy_last(action_list
, action_set
, OFPACT_STRIP_VLAN
);
7177 ofpacts_copy_last(action_list
, action_set
, OFPACT_POP_MPLS
);
7178 ofpacts_copy_last(action_list
, action_set
, OFPACT_DECAP
);
7179 ofpacts_copy_last(action_list
, action_set
, OFPACT_ENCAP
);
7180 ofpacts_copy_last(action_list
, action_set
, OFPACT_PUSH_MPLS
);
7181 ofpacts_copy_last(action_list
, action_set
, OFPACT_PUSH_VLAN
);
7182 ofpacts_copy_last(action_list
, action_set
, OFPACT_DEC_TTL
);
7183 ofpacts_copy_last(action_list
, action_set
, OFPACT_DEC_MPLS_TTL
);
7184 ofpacts_copy_last(action_list
, action_set
, OFPACT_DEC_NSH_TTL
);
7185 ofpacts_copy_all(action_list
, action_set
, ofpact_is_set_or_move_action
);
7186 ofpacts_copy_last(action_list
, action_set
, OFPACT_SET_QUEUE
);
7188 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
7189 * we should execute only OFPACT_GROUP.
7191 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
7192 * all the actions because there's no point in modifying a packet that will
7193 * not be sent anywhere. */
7194 if (!ofpacts_copy_last(action_list
, action_set
, OFPACT_GROUP
) &&
7195 !ofpacts_copy_last(action_list
, action_set
, OFPACT_OUTPUT
) &&
7196 !ofpacts_copy_last(action_list
, action_set
, OFPACT_RESUBMIT
) &&
7197 !ofpacts_copy_last(action_list
, action_set
, OFPACT_CT_CLEAR
) &&
7198 !ofpacts_copy_last(action_list
, action_set
, OFPACT_CT
)) {
7199 ofpbuf_clear(action_list
);
7205 ofpacts_decode_for_action_set(const struct ofp_action_header
*in
,
7206 size_t n_in
, enum ofp_version version
,
7207 const struct vl_mff_map
*vl_mff_map
,
7208 uint64_t *ofpacts_tlv_bitmap
,
7213 size_t start
= out
->size
;
7215 error
= ofpacts_decode(in
, n_in
, version
, vl_mff_map
, ofpacts_tlv_bitmap
,
7222 OFPACT_FOR_EACH (a
, ofpact_end(out
->data
, start
), out
->size
- start
) {
7223 if (!ofpact_is_allowed_in_actions_set(a
)) {
7224 VLOG_WARN_RL(&rl
, "disallowed action in action set");
7225 return OFPERR_OFPBAC_BAD_TYPE
;
7232 /* OpenFlow 1.1 instructions. */
7234 struct instruction_type_info
{
7235 enum ovs_instruction_type type
;
7239 static const struct instruction_type_info inst_info
[] = {
7240 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
7246 ovs_instruction_name_from_type(enum ovs_instruction_type type
)
7248 return type
< ARRAY_SIZE(inst_info
) ? inst_info
[type
].name
: NULL
;
7252 ovs_instruction_type_from_name(const char *name
)
7254 const struct instruction_type_info
*p
;
7255 for (p
= inst_info
; p
< &inst_info
[ARRAY_SIZE(inst_info
)]; p
++) {
7256 if (!strcasecmp(name
, p
->name
)) {
7263 enum ovs_instruction_type
7264 ovs_instruction_type_from_ofpact_type(enum ofpact_type type
)
7268 return OVSINST_OFPIT13_METER
;
7269 case OFPACT_CLEAR_ACTIONS
:
7270 return OVSINST_OFPIT11_CLEAR_ACTIONS
;
7271 case OFPACT_WRITE_ACTIONS
:
7272 return OVSINST_OFPIT11_WRITE_ACTIONS
;
7273 case OFPACT_WRITE_METADATA
:
7274 return OVSINST_OFPIT11_WRITE_METADATA
;
7275 case OFPACT_GOTO_TABLE
:
7276 return OVSINST_OFPIT11_GOTO_TABLE
;
7280 case OFPACT_CONTROLLER
:
7281 case OFPACT_ENQUEUE
:
7282 case OFPACT_OUTPUT_REG
:
7283 case OFPACT_OUTPUT_TRUNC
:
7285 case OFPACT_SET_VLAN_VID
:
7286 case OFPACT_SET_VLAN_PCP
:
7287 case OFPACT_STRIP_VLAN
:
7288 case OFPACT_PUSH_VLAN
:
7289 case OFPACT_SET_ETH_SRC
:
7290 case OFPACT_SET_ETH_DST
:
7291 case OFPACT_SET_IPV4_SRC
:
7292 case OFPACT_SET_IPV4_DST
:
7293 case OFPACT_SET_IP_DSCP
:
7294 case OFPACT_SET_IP_ECN
:
7295 case OFPACT_SET_IP_TTL
:
7296 case OFPACT_SET_L4_SRC_PORT
:
7297 case OFPACT_SET_L4_DST_PORT
:
7298 case OFPACT_REG_MOVE
:
7299 case OFPACT_SET_FIELD
:
7300 case OFPACT_STACK_PUSH
:
7301 case OFPACT_STACK_POP
:
7302 case OFPACT_DEC_TTL
:
7303 case OFPACT_SET_MPLS_LABEL
:
7304 case OFPACT_SET_MPLS_TC
:
7305 case OFPACT_SET_MPLS_TTL
:
7306 case OFPACT_DEC_MPLS_TTL
:
7307 case OFPACT_PUSH_MPLS
:
7308 case OFPACT_POP_MPLS
:
7309 case OFPACT_SET_TUNNEL
:
7310 case OFPACT_SET_QUEUE
:
7311 case OFPACT_POP_QUEUE
:
7312 case OFPACT_FIN_TIMEOUT
:
7313 case OFPACT_RESUBMIT
:
7315 case OFPACT_CONJUNCTION
:
7316 case OFPACT_MULTIPATH
:
7319 case OFPACT_UNROLL_XLATE
:
7321 case OFPACT_DEBUG_RECIRC
:
7322 case OFPACT_DEBUG_SLOW
:
7324 case OFPACT_CT_CLEAR
:
7328 case OFPACT_DEC_NSH_TTL
:
7330 return OVSINST_OFPIT11_APPLY_ACTIONS
;
7335 ovs_instruction_type_from_inst_type(enum ovs_instruction_type
*instruction_type
,
7336 const uint16_t inst_type
)
7338 switch (inst_type
) {
7340 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
7342 *instruction_type = OVSINST_##ENUM; \
7348 return OFPERR_OFPBIC_UNKNOWN_INST
;
7352 /* Two-way translation between OVS's internal "OVSINST_*" representation of
7353 * instructions and the "OFPIT_*" representation used in OpenFlow. */
7354 struct ovsinst_map
{
7355 enum ovs_instruction_type ovsinst
; /* Internal name for instruction. */
7356 int ofpit
; /* OFPIT_* number from OpenFlow spec. */
7359 static const struct ovsinst_map
*
7360 get_ovsinst_map(enum ofp_version version
)
7362 /* OpenFlow 1.1 and 1.2 instructions. */
7363 static const struct ovsinst_map of11
[] = {
7364 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
7365 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
7366 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
7367 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
7368 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
7372 /* OpenFlow 1.3+ instructions. */
7373 static const struct ovsinst_map of13
[] = {
7374 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
7375 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
7376 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
7377 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
7378 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
7379 { OVSINST_OFPIT13_METER
, 6 },
7383 return version
< OFP13_VERSION
? of11
: of13
;
7386 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
7387 * values, into a bitmap of instructions suitable for OpenFlow 'version'
7388 * (OFP11_VERSION or later), and returns the result. */
7390 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap
, enum ofp_version version
)
7392 uint32_t ofpit_bitmap
= 0;
7393 const struct ovsinst_map
*x
;
7395 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
7396 if (ovsinst_bitmap
& (1u << x
->ovsinst
)) {
7397 ofpit_bitmap
|= 1u << x
->ofpit
;
7400 return htonl(ofpit_bitmap
);
7403 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
7404 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
7405 * correspond to OVSINST_* values, and returns the result. */
7407 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap
, enum ofp_version version
)
7409 uint32_t ovsinst_bitmap
= 0;
7410 const struct ovsinst_map
*x
;
7412 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
7413 if (ofpit_bitmap
& htonl(1u << x
->ofpit
)) {
7414 ovsinst_bitmap
|= 1u << x
->ovsinst
;
7417 return ovsinst_bitmap
;
7420 static inline struct ofp11_instruction
*
7421 instruction_next(const struct ofp11_instruction
*inst
)
7423 return ((struct ofp11_instruction
*) (void *)
7424 ((uint8_t *) inst
+ ntohs(inst
->len
)));
7428 instruction_is_valid(const struct ofp11_instruction
*inst
,
7429 size_t n_instructions
)
7431 uint16_t len
= ntohs(inst
->len
);
7432 return (!(len
% OFP11_INSTRUCTION_ALIGN
)
7433 && len
>= sizeof *inst
7434 && len
/ sizeof *inst
<= n_instructions
);
7437 /* This macro is careful to check for instructions with bad lengths. */
7438 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
7439 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
7440 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
7441 ((LEFT) -= (ntohs((ITER)->len) \
7442 / sizeof(struct ofp11_instruction)), \
7443 (ITER) = instruction_next(ITER)))
7446 decode_openflow11_instruction(const struct ofp11_instruction
*inst
,
7447 enum ovs_instruction_type
*type
)
7449 uint16_t len
= ntohs(inst
->len
);
7451 switch (inst
->type
) {
7452 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER
):
7453 return OFPERR_OFPBIC_BAD_EXPERIMENTER
;
7455 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
7456 case CONSTANT_HTONS(ENUM): \
7458 ? len >= sizeof(struct STRUCT) \
7459 : len == sizeof(struct STRUCT)) { \
7460 *type = OVSINST_##ENUM; \
7463 return OFPERR_OFPBIC_BAD_LEN; \
7469 return OFPERR_OFPBIC_UNKNOWN_INST
;
7474 decode_openflow11_instructions(const struct ofp11_instruction insts
[],
7476 const struct ofp11_instruction
*out
[])
7478 const struct ofp11_instruction
*inst
;
7481 memset(out
, 0, N_OVS_INSTRUCTIONS
* sizeof *out
);
7482 INSTRUCTION_FOR_EACH (inst
, left
, insts
, n_insts
) {
7483 enum ovs_instruction_type type
;
7486 error
= decode_openflow11_instruction(inst
, &type
);
7492 return OFPERR_OFPBIC_DUP_INST
;
7498 VLOG_WARN_RL(&rl
, "bad instruction format at offset %"PRIuSIZE
,
7499 (n_insts
- left
) * sizeof *inst
);
7500 return OFPERR_OFPBIC_BAD_LEN
;
7506 get_actions_from_instruction(const struct ofp11_instruction
*inst
,
7507 const struct ofp_action_header
**actions
,
7508 size_t *actions_len
)
7510 *actions
= ALIGNED_CAST(const struct ofp_action_header
*, inst
+ 1);
7511 *actions_len
= ntohs(inst
->len
) - sizeof *inst
;
7515 ofpacts_pull_openflow_instructions(struct ofpbuf
*openflow
,
7516 unsigned int instructions_len
,
7517 enum ofp_version version
,
7518 const struct vl_mff_map
*vl_mff_map
,
7519 uint64_t *ofpacts_tlv_bitmap
,
7520 struct ofpbuf
*ofpacts
)
7522 const struct ofp11_instruction
*instructions
;
7523 const struct ofp11_instruction
*insts
[N_OVS_INSTRUCTIONS
];
7526 ofpbuf_clear(ofpacts
);
7527 if (version
== OFP10_VERSION
) {
7528 return ofpacts_pull_openflow_actions__(openflow
, instructions_len
,
7530 (1u << N_OVS_INSTRUCTIONS
) - 1,
7531 ofpacts
, 0, vl_mff_map
,
7532 ofpacts_tlv_bitmap
);
7535 if (instructions_len
% OFP11_INSTRUCTION_ALIGN
!= 0) {
7536 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u is not a "
7538 instructions_len
, OFP11_INSTRUCTION_ALIGN
);
7539 error
= OFPERR_OFPBIC_BAD_LEN
;
7543 instructions
= ofpbuf_try_pull(openflow
, instructions_len
);
7544 if (instructions
== NULL
) {
7545 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u exceeds "
7546 "remaining message length (%"PRIu32
")",
7547 instructions_len
, openflow
->size
);
7548 error
= OFPERR_OFPBIC_BAD_LEN
;
7552 error
= decode_openflow11_instructions(
7553 instructions
, instructions_len
/ OFP11_INSTRUCTION_ALIGN
,
7559 if (insts
[OVSINST_OFPIT13_METER
]) {
7560 const struct ofp13_instruction_meter
*oim
;
7561 struct ofpact_meter
*om
;
7563 oim
= ALIGNED_CAST(const struct ofp13_instruction_meter
*,
7564 insts
[OVSINST_OFPIT13_METER
]);
7566 om
= ofpact_put_METER(ofpacts
);
7567 om
->meter_id
= ntohl(oim
->meter_id
);
7568 om
->provider_meter_id
= UINT32_MAX
; /* No provider meter ID. */
7570 if (insts
[OVSINST_OFPIT11_APPLY_ACTIONS
]) {
7571 const struct ofp_action_header
*actions
;
7574 get_actions_from_instruction(insts
[OVSINST_OFPIT11_APPLY_ACTIONS
],
7575 &actions
, &actions_len
);
7576 error
= ofpacts_decode(actions
, actions_len
, version
, vl_mff_map
,
7577 ofpacts_tlv_bitmap
, ofpacts
);
7582 if (insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]) {
7583 instruction_get_OFPIT11_CLEAR_ACTIONS(
7584 insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]);
7585 ofpact_put_CLEAR_ACTIONS(ofpacts
);
7587 if (insts
[OVSINST_OFPIT11_WRITE_ACTIONS
]) {
7588 struct ofpact_nest
*on
;
7589 const struct ofp_action_header
*actions
;
7591 size_t start
= ofpacts
->size
;
7592 ofpact_put(ofpacts
, OFPACT_WRITE_ACTIONS
,
7593 offsetof(struct ofpact_nest
, actions
));
7594 get_actions_from_instruction(insts
[OVSINST_OFPIT11_WRITE_ACTIONS
],
7595 &actions
, &actions_len
);
7596 error
= ofpacts_decode_for_action_set(actions
, actions_len
,
7597 version
, vl_mff_map
,
7598 ofpacts_tlv_bitmap
, ofpacts
);
7602 on
= ofpbuf_at_assert(ofpacts
, start
, sizeof *on
);
7603 on
->ofpact
.len
= ofpacts
->size
- start
;
7605 if (insts
[OVSINST_OFPIT11_WRITE_METADATA
]) {
7606 const struct ofp11_instruction_write_metadata
*oiwm
;
7607 struct ofpact_metadata
*om
;
7609 oiwm
= ALIGNED_CAST(const struct ofp11_instruction_write_metadata
*,
7610 insts
[OVSINST_OFPIT11_WRITE_METADATA
]);
7612 om
= ofpact_put_WRITE_METADATA(ofpacts
);
7613 om
->metadata
= oiwm
->metadata
;
7614 om
->mask
= oiwm
->metadata_mask
;
7616 if (insts
[OVSINST_OFPIT11_GOTO_TABLE
]) {
7617 const struct ofp11_instruction_goto_table
*oigt
;
7618 struct ofpact_goto_table
*ogt
;
7620 oigt
= instruction_get_OFPIT11_GOTO_TABLE(
7621 insts
[OVSINST_OFPIT11_GOTO_TABLE
]);
7622 ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
7623 ogt
->table_id
= oigt
->table_id
;
7626 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
,
7627 (1u << N_OVS_INSTRUCTIONS
) - 1, 0);
7630 ofpbuf_clear(ofpacts
);
7635 /* Update the length of the instruction that begins at offset 'ofs' within
7636 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
7637 * If the instruction contains no nested actions, deletes it entirely. */
7639 ofpacts_update_instruction_actions(struct ofpbuf
*openflow
, size_t ofs
)
7641 struct ofp11_instruction_actions
*oia
;
7643 oia
= ofpbuf_at_assert(openflow
, ofs
, sizeof *oia
);
7644 if (openflow
->size
> ofs
+ sizeof *oia
) {
7645 oia
->len
= htons(openflow
->size
- ofs
);
7647 openflow
->size
= ofs
;
7651 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
7652 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
7653 * valid, otherwise an OpenFlow error code. */
7655 ofpact_check_output_port(ofp_port_t port
, ofp_port_t max_ports
)
7663 case OFPP_CONTROLLER
:
7668 return OFPERR_OFPBAC_BAD_OUT_PORT
;
7671 if (ofp_to_u16(port
) < ofp_to_u16(max_ports
)) {
7674 return OFPERR_OFPBAC_BAD_OUT_PORT
;
7678 /* Removes the protocols that require consistency between match and actions
7679 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
7681 * (An example of an inconsistency between match and actions is a flow that
7682 * does not match on an MPLS Ethertype but has an action that pops an MPLS
7685 inconsistent_match(enum ofputil_protocol
*usable_protocols
)
7687 *usable_protocols
&= OFPUTIL_P_OF10_ANY
;
7690 /* May modify flow->packet_type, flow->dl_type, flow->nw_proto and
7691 * flow->vlan_tci, caller must restore them.
7693 * Modifies some actions, filling in fields that could not be properly set
7694 * without context. */
7696 ofpact_check__(enum ofputil_protocol
*usable_protocols
, struct ofpact
*a
,
7697 struct match
*match
, ofp_port_t max_ports
,
7698 uint8_t table_id
, uint8_t n_tables
)
7700 struct flow
*flow
= &match
->flow
;
7701 const struct ofpact_enqueue
*enqueue
;
7702 const struct mf_field
*mf
;
7703 ovs_be16 dl_type
= get_dl_type(flow
);
7707 return ofpact_check_output_port(ofpact_get_OUTPUT(a
)->port
,
7710 case OFPACT_CONTROLLER
:
7713 case OFPACT_ENQUEUE
:
7714 enqueue
= ofpact_get_ENQUEUE(a
);
7715 if (ofp_to_u16(enqueue
->port
) >= ofp_to_u16(max_ports
)
7716 && enqueue
->port
!= OFPP_IN_PORT
7717 && enqueue
->port
!= OFPP_LOCAL
) {
7718 return OFPERR_OFPBAC_BAD_OUT_PORT
;
7722 case OFPACT_OUTPUT_REG
:
7723 return mf_check_src(&ofpact_get_OUTPUT_REG(a
)->src
, match
);
7725 case OFPACT_OUTPUT_TRUNC
:
7726 return ofpact_check_output_port(ofpact_get_OUTPUT_TRUNC(a
)->port
,
7730 return bundle_check(ofpact_get_BUNDLE(a
), max_ports
, match
);
7732 case OFPACT_SET_VLAN_VID
:
7733 /* Remember if we saw a vlan tag in the flow to aid translating to
7734 * OpenFlow 1.1+ if need be. */
7735 ofpact_get_SET_VLAN_VID(a
)->flow_has_vlan
=
7736 (flow
->vlans
[0].tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
7737 if (!(flow
->vlans
[0].tci
& htons(VLAN_CFI
)) &&
7738 !ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
7739 inconsistent_match(usable_protocols
);
7741 /* Temporary mark that we have a vlan tag. */
7742 flow
->vlans
[0].tci
|= htons(VLAN_CFI
);
7745 case OFPACT_SET_VLAN_PCP
:
7746 /* Remember if we saw a vlan tag in the flow to aid translating to
7747 * OpenFlow 1.1+ if need be. */
7748 ofpact_get_SET_VLAN_PCP(a
)->flow_has_vlan
=
7749 (flow
->vlans
[0].tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
7750 if (!(flow
->vlans
[0].tci
& htons(VLAN_CFI
)) &&
7751 !ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
7752 inconsistent_match(usable_protocols
);
7754 /* Temporary mark that we have a vlan tag. */
7755 flow
->vlans
[0].tci
|= htons(VLAN_CFI
);
7758 case OFPACT_STRIP_VLAN
:
7759 if (!(flow
->vlans
[0].tci
& htons(VLAN_CFI
))) {
7760 inconsistent_match(usable_protocols
);
7762 flow_pop_vlan(flow
, NULL
);
7765 case OFPACT_PUSH_VLAN
:
7766 if (flow
->vlans
[FLOW_MAX_VLAN_HEADERS
- 1].tci
& htons(VLAN_CFI
)) {
7767 /* Support maximum (FLOW_MAX_VLAN_HEADERS) VLAN headers. */
7768 return OFPERR_OFPBAC_BAD_TAG
;
7770 /* Temporary mark that we have a vlan tag. */
7771 flow_push_vlan_uninit(flow
, NULL
);
7772 flow
->vlans
[0].tci
|= htons(VLAN_CFI
);
7775 case OFPACT_SET_ETH_SRC
:
7776 case OFPACT_SET_ETH_DST
:
7779 case OFPACT_SET_IPV4_SRC
:
7780 case OFPACT_SET_IPV4_DST
:
7781 if (dl_type
!= htons(ETH_TYPE_IP
)) {
7782 inconsistent_match(usable_protocols
);
7786 case OFPACT_SET_IP_DSCP
:
7787 case OFPACT_SET_IP_ECN
:
7788 case OFPACT_SET_IP_TTL
:
7789 case OFPACT_DEC_TTL
:
7790 if (!is_ip_any(flow
)) {
7791 inconsistent_match(usable_protocols
);
7795 case OFPACT_SET_L4_SRC_PORT
:
7796 case OFPACT_SET_L4_DST_PORT
:
7797 if (!is_ip_any(flow
) || (flow
->nw_frag
& FLOW_NW_FRAG_LATER
) ||
7798 (flow
->nw_proto
!= IPPROTO_TCP
&& flow
->nw_proto
!= IPPROTO_UDP
7799 && flow
->nw_proto
!= IPPROTO_SCTP
)) {
7800 inconsistent_match(usable_protocols
);
7802 /* Note on which transport protocol the port numbers are set.
7803 * This allows this set action to be converted to an OF1.2 set field
7805 if (a
->type
== OFPACT_SET_L4_SRC_PORT
) {
7806 ofpact_get_SET_L4_SRC_PORT(a
)->flow_ip_proto
= flow
->nw_proto
;
7808 ofpact_get_SET_L4_DST_PORT(a
)->flow_ip_proto
= flow
->nw_proto
;
7812 case OFPACT_REG_MOVE
:
7813 return nxm_reg_move_check(ofpact_get_REG_MOVE(a
), match
);
7815 case OFPACT_SET_FIELD
:
7816 mf
= ofpact_get_SET_FIELD(a
)->field
;
7817 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
7818 if (!mf_are_prereqs_ok(mf
, flow
, NULL
) ||
7819 (mf
->id
== MFF_VLAN_VID
&&
7820 !(flow
->vlans
[0].tci
& htons(VLAN_CFI
)))) {
7821 VLOG_WARN_RL(&rl
, "set_field %s lacks correct prerequisites",
7823 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
7825 /* Remember if we saw a vlan tag in the flow to aid translating to
7826 * OpenFlow 1.1 if need be. */
7827 ofpact_get_SET_FIELD(a
)->flow_has_vlan
=
7828 (flow
->vlans
[0].tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
7829 if (mf
->id
== MFF_VLAN_TCI
) {
7830 /* The set field may add or remove the vlan tag,
7831 * Mark the status temporarily. */
7832 flow
->vlans
[0].tci
= ofpact_get_SET_FIELD(a
)->value
->be16
;
7836 case OFPACT_STACK_PUSH
:
7837 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a
), match
);
7839 case OFPACT_STACK_POP
:
7840 return nxm_stack_pop_check(ofpact_get_STACK_POP(a
), match
);
7842 case OFPACT_SET_MPLS_LABEL
:
7843 case OFPACT_SET_MPLS_TC
:
7844 case OFPACT_SET_MPLS_TTL
:
7845 case OFPACT_DEC_MPLS_TTL
:
7846 if (!eth_type_mpls(dl_type
)) {
7847 inconsistent_match(usable_protocols
);
7851 case OFPACT_SET_TUNNEL
:
7852 case OFPACT_SET_QUEUE
:
7853 case OFPACT_POP_QUEUE
:
7856 case OFPACT_RESUBMIT
: {
7857 struct ofpact_resubmit
*resubmit
= ofpact_get_RESUBMIT(a
);
7859 if (resubmit
->with_ct_orig
&& !is_ct_valid(flow
, &match
->wc
, NULL
)) {
7860 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
7864 case OFPACT_FIN_TIMEOUT
:
7865 if (flow
->nw_proto
!= IPPROTO_TCP
) {
7866 inconsistent_match(usable_protocols
);
7871 return learn_check(ofpact_get_LEARN(a
), match
);
7873 case OFPACT_CONJUNCTION
:
7876 case OFPACT_MULTIPATH
:
7877 return multipath_check(ofpact_get_MULTIPATH(a
), match
);
7883 case OFPACT_PUSH_MPLS
:
7884 if (flow
->packet_type
!= htonl(PT_ETH
)) {
7885 inconsistent_match(usable_protocols
);
7887 flow
->dl_type
= ofpact_get_PUSH_MPLS(a
)->ethertype
;
7888 /* The packet is now MPLS and the MPLS payload is opaque.
7889 * Thus nothing can be assumed about the network protocol.
7890 * Temporarily mark that we have no nw_proto. */
7894 case OFPACT_POP_MPLS
:
7895 if (flow
->packet_type
!= htonl(PT_ETH
)
7896 || !eth_type_mpls(dl_type
)) {
7897 inconsistent_match(usable_protocols
);
7899 flow
->dl_type
= ofpact_get_POP_MPLS(a
)->ethertype
;
7905 case OFPACT_CLONE
: {
7906 struct ofpact_nest
*on
= ofpact_get_CLONE(a
);
7907 return ofpacts_check(on
->actions
, ofpact_nest_get_action_len(on
),
7908 match
, max_ports
, table_id
, n_tables
,
7913 struct ofpact_conntrack
*oc
= ofpact_get_CT(a
);
7915 if (!dl_type_is_ip_any(dl_type
)
7916 || (flow
->ct_state
& CS_INVALID
&& oc
->flags
& NX_CT_F_COMMIT
)
7917 || (oc
->alg
== IPPORT_FTP
&& flow
->nw_proto
!= IPPROTO_TCP
)
7918 || (oc
->alg
== IPPORT_TFTP
&& flow
->nw_proto
!= IPPROTO_UDP
)) {
7919 /* We can't downgrade to OF1.0 and expect inconsistent CT actions
7920 * be silently discarded. Instead, datapath flow install fails, so
7921 * it is better to flag inconsistent CT actions as hard errors. */
7922 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
7925 if (oc
->zone_src
.field
) {
7926 return mf_check_src(&oc
->zone_src
, match
);
7929 return ofpacts_check(oc
->actions
, ofpact_ct_get_action_len(oc
),
7930 match
, max_ports
, table_id
, n_tables
,
7934 case OFPACT_CT_CLEAR
:
7938 struct ofpact_nat
*on
= ofpact_get_NAT(a
);
7940 if (!dl_type_is_ip_any(dl_type
) ||
7941 (on
->range_af
== AF_INET
&& dl_type
!= htons(ETH_TYPE_IP
)) ||
7942 (on
->range_af
== AF_INET6
7943 && dl_type
!= htons(ETH_TYPE_IPV6
))) {
7944 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
7949 case OFPACT_CLEAR_ACTIONS
:
7952 case OFPACT_WRITE_ACTIONS
: {
7953 /* Use a temporary copy of 'usable_protocols' because we can't check
7954 * consistency of an action set. */
7955 struct ofpact_nest
*on
= ofpact_get_WRITE_ACTIONS(a
);
7956 enum ofputil_protocol p
= *usable_protocols
;
7957 return ofpacts_check(on
->actions
, ofpact_nest_get_action_len(on
),
7958 match
, max_ports
, table_id
, n_tables
, &p
);
7961 case OFPACT_WRITE_METADATA
:
7964 case OFPACT_METER
: {
7965 uint32_t mid
= ofpact_get_METER(a
)->meter_id
;
7966 if (mid
== 0 || mid
> OFPM13_MAX
) {
7967 return OFPERR_OFPMMFC_INVALID_METER
;
7972 case OFPACT_GOTO_TABLE
: {
7973 uint8_t goto_table
= ofpact_get_GOTO_TABLE(a
)->table_id
;
7974 if ((table_id
!= 255 && goto_table
<= table_id
)
7975 || (n_tables
!= 255 && goto_table
>= n_tables
)) {
7976 return OFPERR_OFPBIC_BAD_TABLE_ID
;
7984 case OFPACT_UNROLL_XLATE
:
7985 /* UNROLL is an internal action that should never be seen via
7987 return OFPERR_OFPBAC_BAD_TYPE
;
7989 case OFPACT_DEBUG_RECIRC
:
7990 case OFPACT_DEBUG_SLOW
:
7994 flow
->packet_type
= ofpact_get_ENCAP(a
)->new_pkt_type
;
7995 if (pt_ns(flow
->packet_type
) == OFPHTN_ETHERTYPE
) {
7996 flow
->dl_type
= htons(pt_ns_type(flow
->packet_type
));
7998 if (!is_ip_any(flow
)) {
8004 if (flow
->packet_type
== htonl(PT_ETH
)) {
8005 /* Adjust the packet_type to allow subsequent actions. */
8006 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
8007 ntohs(flow
->dl_type
));
8009 /* The actual packet_type is only known after decapsulation.
8010 * Do not allow subsequent actions that depend on packet headers. */
8011 flow
->packet_type
= htonl(PT_UNKNOWN
);
8012 flow
->dl_type
= OVS_BE16_MAX
;
8016 case OFPACT_DEC_NSH_TTL
:
8017 if ((flow
->packet_type
!= htonl(PT_NSH
)) &&
8018 (flow
->dl_type
!= htons(ETH_TYPE_NSH
))) {
8019 inconsistent_match(usable_protocols
);
8028 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
8029 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
8030 * switch with no more than 'max_ports' ports.
8032 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
8033 * '*usable_protocols' the protocols that forbid the inconsistency. (An
8034 * example of an inconsistency between match and actions is a flow that does
8035 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
8037 * May annotate ofpacts with information gathered from the 'match'.
8039 * May temporarily modify 'match', but restores the changes before
8042 ofpacts_check(struct ofpact ofpacts
[], size_t ofpacts_len
,
8043 struct match
*match
, ofp_port_t max_ports
,
8044 uint8_t table_id
, uint8_t n_tables
,
8045 enum ofputil_protocol
*usable_protocols
)
8048 ovs_be32 packet_type
= match
->flow
.packet_type
;
8049 ovs_be16 dl_type
= match
->flow
.dl_type
;
8050 uint8_t nw_proto
= match
->flow
.nw_proto
;
8051 enum ofperr error
= 0;
8052 union flow_vlan_hdr vlans
[FLOW_MAX_VLAN_HEADERS
];
8054 memcpy(&vlans
, &match
->flow
.vlans
, sizeof(vlans
));
8056 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8057 error
= ofpact_check__(usable_protocols
, a
, match
,
8058 max_ports
, table_id
, n_tables
);
8063 /* Restore fields that may have been modified. */
8064 match
->flow
.packet_type
= packet_type
;
8065 match
->flow
.dl_type
= dl_type
;
8066 memcpy(&match
->flow
.vlans
, &vlans
, sizeof(vlans
));
8067 match
->flow
.nw_proto
= nw_proto
;
8071 /* Like ofpacts_check(), but reports inconsistencies as
8072 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
8074 ofpacts_check_consistency(struct ofpact ofpacts
[], size_t ofpacts_len
,
8075 struct match
*match
, ofp_port_t max_ports
,
8076 uint8_t table_id
, uint8_t n_tables
,
8077 enum ofputil_protocol usable_protocols
)
8079 enum ofputil_protocol p
= usable_protocols
;
8082 error
= ofpacts_check(ofpacts
, ofpacts_len
, match
, max_ports
,
8083 table_id
, n_tables
, &p
);
8084 return (error
? error
8085 : p
!= usable_protocols
? OFPERR_OFPBAC_MATCH_INCONSISTENT
8089 /* Returns the destination field that 'ofpact' would write to, or NULL
8090 * if the action would not write to an mf_field. */
8091 const struct mf_field
*
8092 ofpact_get_mf_dst(const struct ofpact
*ofpact
)
8094 if (ofpact
->type
== OFPACT_SET_FIELD
) {
8095 const struct ofpact_set_field
*orl
;
8097 orl
= CONTAINER_OF(ofpact
, struct ofpact_set_field
, ofpact
);
8099 } else if (ofpact
->type
== OFPACT_REG_MOVE
) {
8100 const struct ofpact_reg_move
*orm
;
8102 orm
= CONTAINER_OF(ofpact
, struct ofpact_reg_move
, ofpact
);
8103 return orm
->dst
.field
;
8110 unsupported_nesting(enum ofpact_type action
, enum ofpact_type outer_action
)
8112 VLOG_WARN("%s action doesn't support nested action %s",
8113 ofpact_name(outer_action
), ofpact_name(action
));
8114 return OFPERR_OFPBAC_BAD_ARGUMENT
;
8118 field_requires_ct(enum mf_field_id field
)
8120 return field
== MFF_CT_MARK
|| field
== MFF_CT_LABEL
;
8123 /* Apply nesting constraints for actions */
8125 ofpacts_verify_nested(const struct ofpact
*a
, enum ofpact_type outer_action
)
8127 const struct mf_field
*field
= ofpact_get_mf_dst(a
);
8129 if (field
&& field_requires_ct(field
->id
) && outer_action
!= OFPACT_CT
) {
8130 VLOG_WARN("cannot set CT fields outside of ct action");
8131 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
8133 if (a
->type
== OFPACT_NAT
) {
8134 if (outer_action
!= OFPACT_CT
) {
8135 VLOG_WARN("Cannot have NAT action outside of \"ct\" action");
8136 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
8142 ovs_assert(outer_action
== OFPACT_WRITE_ACTIONS
8143 || outer_action
== OFPACT_CT
);
8145 if (outer_action
== OFPACT_CT
) {
8147 return unsupported_nesting(a
->type
, outer_action
);
8148 } else if (!field_requires_ct(field
->id
)) {
8149 VLOG_WARN("%s action doesn't support nested modification "
8150 "of %s", ofpact_name(outer_action
), field
->name
);
8151 return OFPERR_OFPBAC_BAD_ARGUMENT
;
8159 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
8160 * appropriate order as defined by the OpenFlow spec and as required by Open
8163 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
8164 * instructions that are allowed within 'ofpacts[]'.
8166 * If 'outer_action' is not zero, it specifies that the actions are nested
8167 * within another action of type 'outer_action'. */
8169 ofpacts_verify(const struct ofpact ofpacts
[], size_t ofpacts_len
,
8170 uint32_t allowed_ovsinsts
, enum ofpact_type outer_action
)
8172 const struct ofpact
*a
;
8173 enum ovs_instruction_type inst
;
8175 inst
= OVSINST_OFPIT13_METER
;
8176 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8177 enum ovs_instruction_type next
;
8180 if (a
->type
== OFPACT_CONJUNCTION
) {
8181 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8182 if (a
->type
!= OFPACT_CONJUNCTION
&& a
->type
!= OFPACT_NOTE
) {
8183 VLOG_WARN("\"conjunction\" actions may be used along with "
8184 "\"note\" but not any other kind of action "
8185 "(such as the \"%s\" action used here)",
8186 ofpact_name(a
->type
));
8187 return OFPERR_NXBAC_BAD_CONJUNCTION
;
8193 error
= ofpacts_verify_nested(a
, outer_action
);
8198 next
= ovs_instruction_type_from_ofpact_type(a
->type
);
8200 && (inst
== OVSINST_OFPIT11_APPLY_ACTIONS
8203 const char *name
= ovs_instruction_name_from_type(inst
);
8204 const char *next_name
= ovs_instruction_name_from_type(next
);
8207 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
8208 "1.1+ compatibility", name
);
8210 VLOG_WARN("invalid instruction ordering: %s must appear "
8211 "before %s, for OpenFlow 1.1+ compatibility",
8214 return OFPERR_OFPBAC_UNSUPPORTED_ORDER
;
8216 if (!((1u << next
) & allowed_ovsinsts
)) {
8217 const char *name
= ovs_instruction_name_from_type(next
);
8219 VLOG_WARN("%s instruction not allowed here", name
);
8220 return OFPERR_OFPBIC_UNSUP_INST
;
8229 /* Converting ofpacts to OpenFlow. */
8232 encode_ofpact(const struct ofpact
*a
, enum ofp_version ofp_version
,
8236 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
8237 case OFPACT_##ENUM: \
8238 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
8247 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
8248 * actions in 'openflow', appending the actions to any existing data in
8251 ofpacts_put_openflow_actions(const struct ofpact ofpacts
[], size_t ofpacts_len
,
8252 struct ofpbuf
*openflow
,
8253 enum ofp_version ofp_version
)
8255 const struct ofpact
*a
;
8256 size_t start_size
= openflow
->size
;
8258 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8259 encode_ofpact(a
, ofp_version
, openflow
);
8261 return openflow
->size
- start_size
;
8264 static enum ovs_instruction_type
8265 ofpact_is_apply_actions(const struct ofpact
*a
)
8267 return (ovs_instruction_type_from_ofpact_type(a
->type
)
8268 == OVSINST_OFPIT11_APPLY_ACTIONS
);
8272 ofpacts_put_openflow_instructions(const struct ofpact ofpacts
[],
8274 struct ofpbuf
*openflow
,
8275 enum ofp_version ofp_version
)
8277 const struct ofpact
*end
= ofpact_end(ofpacts
, ofpacts_len
);
8278 const struct ofpact
*a
;
8280 if (ofp_version
== OFP10_VERSION
) {
8281 ofpacts_put_openflow_actions(ofpacts
, ofpacts_len
, openflow
,
8288 if (ofpact_is_apply_actions(a
)) {
8289 size_t ofs
= openflow
->size
;
8291 instruction_put_OFPIT11_APPLY_ACTIONS(openflow
);
8293 encode_ofpact(a
, ofp_version
, openflow
);
8295 } while (a
< end
&& ofpact_is_apply_actions(a
));
8296 ofpacts_update_instruction_actions(openflow
, ofs
);
8298 encode_ofpact(a
, ofp_version
, openflow
);
8304 /* Sets of supported actions. */
8306 /* Two-way translation between OVS's internal "OFPACT_*" representation of
8307 * actions and the "OFPAT_*" representation used in some OpenFlow version.
8308 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
8309 * instance is specific to one OpenFlow version.) */
8311 enum ofpact_type ofpact
; /* Internal name for action type. */
8312 int ofpat
; /* OFPAT_* number from OpenFlow spec. */
8315 static const struct ofpact_map
*
8316 get_ofpact_map(enum ofp_version version
)
8318 /* OpenFlow 1.0 actions. */
8319 static const struct ofpact_map of10
[] = {
8320 { OFPACT_OUTPUT
, 0 },
8321 { OFPACT_SET_VLAN_VID
, 1 },
8322 { OFPACT_SET_VLAN_PCP
, 2 },
8323 { OFPACT_STRIP_VLAN
, 3 },
8324 { OFPACT_SET_ETH_SRC
, 4 },
8325 { OFPACT_SET_ETH_DST
, 5 },
8326 { OFPACT_SET_IPV4_SRC
, 6 },
8327 { OFPACT_SET_IPV4_DST
, 7 },
8328 { OFPACT_SET_IP_DSCP
, 8 },
8329 { OFPACT_SET_L4_SRC_PORT
, 9 },
8330 { OFPACT_SET_L4_DST_PORT
, 10 },
8331 { OFPACT_ENQUEUE
, 11 },
8335 /* OpenFlow 1.1 actions. */
8336 static const struct ofpact_map of11
[] = {
8337 { OFPACT_OUTPUT
, 0 },
8338 { OFPACT_SET_VLAN_VID
, 1 },
8339 { OFPACT_SET_VLAN_PCP
, 2 },
8340 { OFPACT_SET_ETH_SRC
, 3 },
8341 { OFPACT_SET_ETH_DST
, 4 },
8342 { OFPACT_SET_IPV4_SRC
, 5 },
8343 { OFPACT_SET_IPV4_DST
, 6 },
8344 { OFPACT_SET_IP_DSCP
, 7 },
8345 { OFPACT_SET_IP_ECN
, 8 },
8346 { OFPACT_SET_L4_SRC_PORT
, 9 },
8347 { OFPACT_SET_L4_DST_PORT
, 10 },
8348 /* OFPAT_COPY_TTL_OUT (11) not supported. */
8349 /* OFPAT_COPY_TTL_IN (12) not supported. */
8350 { OFPACT_SET_MPLS_LABEL
, 13 },
8351 { OFPACT_SET_MPLS_TC
, 14 },
8352 { OFPACT_SET_MPLS_TTL
, 15 },
8353 { OFPACT_DEC_MPLS_TTL
, 16 },
8354 { OFPACT_PUSH_VLAN
, 17 },
8355 { OFPACT_STRIP_VLAN
, 18 },
8356 { OFPACT_PUSH_MPLS
, 19 },
8357 { OFPACT_POP_MPLS
, 20 },
8358 { OFPACT_SET_QUEUE
, 21 },
8359 { OFPACT_GROUP
, 22 },
8360 { OFPACT_SET_IP_TTL
, 23 },
8361 { OFPACT_DEC_TTL
, 24 },
8365 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
8366 static const struct ofpact_map of12
[] = {
8367 { OFPACT_OUTPUT
, 0 },
8368 /* OFPAT_COPY_TTL_OUT (11) not supported. */
8369 /* OFPAT_COPY_TTL_IN (12) not supported. */
8370 { OFPACT_SET_MPLS_TTL
, 15 },
8371 { OFPACT_DEC_MPLS_TTL
, 16 },
8372 { OFPACT_PUSH_VLAN
, 17 },
8373 { OFPACT_STRIP_VLAN
, 18 },
8374 { OFPACT_PUSH_MPLS
, 19 },
8375 { OFPACT_POP_MPLS
, 20 },
8376 { OFPACT_SET_QUEUE
, 21 },
8377 { OFPACT_GROUP
, 22 },
8378 { OFPACT_SET_IP_TTL
, 23 },
8379 { OFPACT_DEC_TTL
, 24 },
8380 { OFPACT_SET_FIELD
, 25 },
8381 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
8382 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
8403 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
8404 * values, into a bitmap of actions suitable for OpenFlow 'version', and
8405 * returns the result. */
8407 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap
, enum ofp_version version
)
8409 uint32_t openflow_bitmap
= 0;
8410 const struct ofpact_map
*x
;
8412 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
8413 if (ofpacts_bitmap
& (UINT64_C(1) << x
->ofpact
)) {
8414 openflow_bitmap
|= 1u << x
->ofpat
;
8417 return htonl(openflow_bitmap
);
8420 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
8421 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
8422 * and returns the result. */
8424 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap
, enum ofp_version version
)
8426 uint64_t ofpact_bitmap
= 0;
8427 const struct ofpact_map
*x
;
8429 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
8430 if (ofpat_bitmap
& htonl(1u << x
->ofpat
)) {
8431 ofpact_bitmap
|= UINT64_C(1) << x
->ofpact
;
8434 return ofpact_bitmap
;
8437 /* Appends to 's' a string representation of the set of OFPACT_* represented
8438 * by 'ofpacts_bitmap'. */
8440 ofpact_bitmap_format(uint64_t ofpacts_bitmap
, struct ds
*s
)
8442 if (!ofpacts_bitmap
) {
8443 ds_put_cstr(s
, "<none>");
8445 while (ofpacts_bitmap
) {
8446 ds_put_format(s
, "%s ",
8447 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap
)));
8448 ofpacts_bitmap
= zero_rightmost_1bit(ofpacts_bitmap
);
8454 /* Returns true if 'action' outputs to 'port', false otherwise. */
8456 ofpact_outputs_to_port(const struct ofpact
*ofpact
, ofp_port_t port
)
8458 switch (ofpact
->type
) {
8460 return ofpact_get_OUTPUT(ofpact
)->port
== port
;
8461 case OFPACT_ENQUEUE
:
8462 return ofpact_get_ENQUEUE(ofpact
)->port
== port
;
8463 case OFPACT_CONTROLLER
:
8464 return port
== OFPP_CONTROLLER
;
8466 case OFPACT_OUTPUT_REG
:
8467 case OFPACT_OUTPUT_TRUNC
:
8469 case OFPACT_SET_VLAN_VID
:
8470 case OFPACT_SET_VLAN_PCP
:
8471 case OFPACT_STRIP_VLAN
:
8472 case OFPACT_PUSH_VLAN
:
8473 case OFPACT_SET_ETH_SRC
:
8474 case OFPACT_SET_ETH_DST
:
8475 case OFPACT_SET_IPV4_SRC
:
8476 case OFPACT_SET_IPV4_DST
:
8477 case OFPACT_SET_IP_DSCP
:
8478 case OFPACT_SET_IP_ECN
:
8479 case OFPACT_SET_IP_TTL
:
8480 case OFPACT_SET_L4_SRC_PORT
:
8481 case OFPACT_SET_L4_DST_PORT
:
8482 case OFPACT_REG_MOVE
:
8483 case OFPACT_SET_FIELD
:
8484 case OFPACT_STACK_PUSH
:
8485 case OFPACT_STACK_POP
:
8486 case OFPACT_DEC_TTL
:
8487 case OFPACT_SET_MPLS_LABEL
:
8488 case OFPACT_SET_MPLS_TC
:
8489 case OFPACT_SET_MPLS_TTL
:
8490 case OFPACT_DEC_MPLS_TTL
:
8491 case OFPACT_SET_TUNNEL
:
8492 case OFPACT_WRITE_METADATA
:
8493 case OFPACT_SET_QUEUE
:
8494 case OFPACT_POP_QUEUE
:
8495 case OFPACT_FIN_TIMEOUT
:
8496 case OFPACT_RESUBMIT
:
8498 case OFPACT_CONJUNCTION
:
8499 case OFPACT_MULTIPATH
:
8502 case OFPACT_UNROLL_XLATE
:
8503 case OFPACT_PUSH_MPLS
:
8504 case OFPACT_POP_MPLS
:
8506 case OFPACT_CLEAR_ACTIONS
:
8508 case OFPACT_WRITE_ACTIONS
:
8509 case OFPACT_GOTO_TABLE
:
8512 case OFPACT_DEBUG_RECIRC
:
8513 case OFPACT_DEBUG_SLOW
:
8515 case OFPACT_CT_CLEAR
:
8519 case OFPACT_DEC_NSH_TTL
:
8525 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
8526 * to 'port', false otherwise. */
8528 ofpacts_output_to_port(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
8531 const struct ofpact
*a
;
8533 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
8534 if (ofpact_outputs_to_port(a
, port
)) {
8542 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
8543 * to 'group', false otherwise. */
8545 ofpacts_output_to_group(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
8548 const struct ofpact
*a
;
8550 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
8551 if (a
->type
== OFPACT_GROUP
8552 && ofpact_get_GROUP(a
)->group_id
== group_id
) {
8560 /* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of
8561 * actions in 'b' are bytewise identical. */
8563 ofpacts_equal(const struct ofpact
*a
, size_t a_len
,
8564 const struct ofpact
*b
, size_t b_len
)
8566 return a_len
== b_len
&& !memcmp(a
, b
, a_len
);
8569 /* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of
8570 * actions in 'b' are identical when formatted as strings. (Converting actions
8571 * to string form suppresses some rarely meaningful differences, such as the
8572 * 'compat' member of actions.) */
8574 ofpacts_equal_stringwise(const struct ofpact
*a
, size_t a_len
,
8575 const struct ofpact
*b
, size_t b_len
)
8577 struct ds a_s
= DS_EMPTY_INITIALIZER
;
8578 struct ofpact_format_params a_fp
= { .s
= &a_s
};
8579 ofpacts_format(a
, a_len
, &a_fp
);
8581 struct ds b_s
= DS_EMPTY_INITIALIZER
;
8582 struct ofpact_format_params b_fp
= { .s
= &b_s
};
8583 ofpacts_format(b
, b_len
, &b_fp
);
8585 bool equal
= !strcmp(ds_cstr(&a_s
), ds_cstr(&b_s
));
8593 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
8594 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
8596 * This function relies on the order of 'ofpacts' being correct (as checked by
8597 * ofpacts_verify()). */
8599 ofpacts_get_meter(const struct ofpact ofpacts
[], size_t ofpacts_len
)
8601 const struct ofpact
*a
;
8603 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8604 enum ovs_instruction_type inst
;
8606 inst
= ovs_instruction_type_from_ofpact_type(a
->type
);
8607 if (a
->type
== OFPACT_METER
) {
8608 return ofpact_get_METER(a
)->meter_id
;
8609 } else if (inst
> OVSINST_OFPIT13_METER
) {
8617 /* Formatting ofpacts. */
8620 ofpact_format(const struct ofpact
*a
,
8621 const struct ofpact_format_params
*fp
)
8624 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
8625 case OFPACT_##ENUM: \
8626 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), fp); \
8635 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
8636 * 'ofpacts' to 'fp->s'. If 'port_map' is nonnull, uses it to translate port
8637 * numbers to names in output. */
8639 ofpacts_format(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
8640 const struct ofpact_format_params
*fp
)
8643 ds_put_format(fp
->s
, "%sdrop%s", colors
.drop
, colors
.end
);
8645 const struct ofpact
*a
;
8647 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8649 ds_put_char(fp
->s
, ',');
8652 ofpact_format(a
, fp
);
8657 /* Internal use by helpers. */
8659 /* Implementation of ofpact_put_<ENUM>(). */
8661 ofpact_put(struct ofpbuf
*ofpacts
, enum ofpact_type type
, size_t len
)
8663 struct ofpact
*ofpact
;
8665 ofpacts
->header
= ofpbuf_put_uninit(ofpacts
, len
);
8666 ofpact
= ofpacts
->header
;
8667 ofpact_init(ofpact
, type
, len
);
8671 /* Implementation of ofpact_init_<ENUM>(). */
8673 ofpact_init(struct ofpact
*ofpact
, enum ofpact_type type
, size_t len
)
8675 memset(ofpact
, 0, len
);
8676 ofpact
->type
= type
;
8681 /* Implementation of ofpact_finish_<ENUM>().
8683 * Finishes composing a variable-length action (begun using
8684 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
8685 * bytes and updating its embedded length field. See the large comment near
8686 * the end of ofp-actions.h for more information.
8688 * May reallocate 'ofpacts'. Callers should consider updating their 'ofpact'
8689 * pointer to the return value of this function. */
8691 ofpact_finish(struct ofpbuf
*ofpacts
, struct ofpact
*ofpact
)
8695 ovs_assert(ofpact
== ofpacts
->header
);
8696 len
= (char *) ofpbuf_tail(ofpacts
) - (char *) ofpact
;
8697 ovs_assert(len
> 0 && len
<= UINT16_MAX
);
8699 ofpbuf_padto(ofpacts
, OFPACT_ALIGN(ofpacts
->size
));
8701 return ofpacts
->header
;
8704 static char * OVS_WARN_UNUSED_RESULT
8705 ofpact_parse(enum ofpact_type type
, char *value
,
8706 const struct ofpact_parse_params
*pp
)
8709 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
8710 case OFPACT_##ENUM: \
8711 return parse_##ENUM(value, pp);
8720 ofpact_type_from_name(const char *name
, enum ofpact_type
*type
)
8722 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
8723 if (!strcasecmp(name, NAME)) { \
8724 *type = OFPACT_##ENUM; \
8733 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
8735 * Returns NULL if successful, otherwise a malloc()'d string describing the
8736 * error. The caller is responsible for freeing the returned string.
8738 * If 'outer_action' is specified, indicates that the actions being parsed
8739 * are nested within another action of the type specified in 'outer_action'. */
8740 static char * OVS_WARN_UNUSED_RESULT
8741 ofpacts_parse__(char *str
, const struct ofpact_parse_params
*pp
,
8742 bool allow_instructions
, enum ofpact_type outer_action
)
8751 while (ofputil_parse_key_value(&pos
, &key
, &value
)) {
8752 enum ovs_instruction_type inst
= OVSINST_OFPIT11_APPLY_ACTIONS
;
8753 enum ofpact_type type
;
8757 if (ofpact_type_from_name(key
, &type
)) {
8758 error
= ofpact_parse(type
, value
, pp
);
8759 inst
= ovs_instruction_type_from_ofpact_type(type
);
8760 } else if (!strcasecmp(key
, "mod_vlan_vid")) {
8761 error
= parse_set_vlan_vid(value
, true, pp
);
8762 } else if (!strcasecmp(key
, "mod_vlan_pcp")) {
8763 error
= parse_set_vlan_pcp(value
, true, pp
);
8764 } else if (!strcasecmp(key
, "set_nw_ttl")) {
8765 error
= parse_SET_IP_TTL(value
, pp
);
8766 } else if (!strcasecmp(key
, "pop_vlan")) {
8767 error
= parse_pop_vlan(pp
);
8768 } else if (!strcasecmp(key
, "set_tunnel64")) {
8769 error
= parse_set_tunnel(value
, NXAST_RAW_SET_TUNNEL64
, pp
);
8770 } else if (!strcasecmp(key
, "load")) {
8771 error
= parse_reg_load(value
, pp
);
8772 } else if (!strcasecmp(key
, "bundle_load")) {
8773 error
= parse_bundle_load(value
, pp
);
8774 } else if (!strcasecmp(key
, "drop")) {
8776 } else if (!strcasecmp(key
, "apply_actions")) {
8777 return xstrdup("apply_actions is the default instruction");
8778 } else if (ofputil_port_from_string(key
, pp
->port_map
, &port
)) {
8779 ofpact_put_OUTPUT(pp
->ofpacts
)->port
= port
;
8781 return xasprintf("unknown action %s", key
);
8787 if (inst
!= OVSINST_OFPIT11_APPLY_ACTIONS
) {
8788 if (!allow_instructions
) {
8789 return xasprintf("only actions are allowed here (not "
8791 ovs_instruction_name_from_type(inst
));
8793 if (inst
== prev_inst
) {
8794 return xasprintf("instruction %s may be specified only once",
8795 ovs_instruction_name_from_type(inst
));
8798 if (prev_inst
!= -1 && inst
< prev_inst
) {
8799 return xasprintf("instruction %s must be specified before %s",
8800 ovs_instruction_name_from_type(inst
),
8801 ovs_instruction_name_from_type(prev_inst
));
8806 if (drop
&& pp
->ofpacts
->size
) {
8807 return xstrdup("\"drop\" must not be accompanied by any other action "
8811 retval
= ofpacts_verify(pp
->ofpacts
->data
, pp
->ofpacts
->size
,
8813 ? (1u << N_OVS_INSTRUCTIONS
) - 1
8814 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS
),
8817 return xstrdup("Incorrect instruction ordering");
8823 static char * OVS_WARN_UNUSED_RESULT
8824 ofpacts_parse(char *str
, const struct ofpact_parse_params
*pp
,
8825 bool allow_instructions
, enum ofpact_type outer_action
)
8827 uint32_t orig_size
= pp
->ofpacts
->size
;
8828 char *error
= ofpacts_parse__(str
, pp
, allow_instructions
, outer_action
);
8830 pp
->ofpacts
->size
= orig_size
;
8835 static char * OVS_WARN_UNUSED_RESULT
8836 ofpacts_parse_copy(const char *s_
, const struct ofpact_parse_params
*pp
,
8837 bool allow_instructions
, enum ofpact_type outer_action
)
8841 *pp
->usable_protocols
= OFPUTIL_P_ANY
;
8844 error
= ofpacts_parse(s
, pp
, allow_instructions
, outer_action
);
8850 /* Parses 's' as a set of OpenFlow actions and appends the actions to
8851 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
8852 * that are nested within the action of type 'outer_action'.
8854 * Returns NULL if successful, otherwise a malloc()'d string describing the
8855 * error. The caller is responsible for freeing the returned string. */
8856 char * OVS_WARN_UNUSED_RESULT
8857 ofpacts_parse_actions(const char *s
, const struct ofpact_parse_params
*pp
)
8859 return ofpacts_parse_copy(s
, pp
, false, 0);
8862 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
8865 * Returns NULL if successful, otherwise a malloc()'d string describing the
8866 * error. The caller is responsible for freeing the returned string. */
8867 char * OVS_WARN_UNUSED_RESULT
8868 ofpacts_parse_instructions(const char *s
, const struct ofpact_parse_params
*pp
)
8870 return ofpacts_parse_copy(s
, pp
, true, 0);
8874 ofpact_name(enum ofpact_type type
)
8877 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
8884 /* Low-level action decoding and encoding functions. */
8886 /* Everything needed to identify a particular OpenFlow action. */
8887 struct ofpact_hdrs
{
8888 uint32_t vendor
; /* 0 if standard, otherwise a vendor code. */
8889 uint16_t type
; /* Type if standard, otherwise subtype. */
8890 uint8_t ofp_version
; /* From ofp_header. */
8893 /* Information about a particular OpenFlow action. */
8894 struct ofpact_raw_instance
{
8895 /* The action's identity. */
8896 struct ofpact_hdrs hdrs
;
8897 enum ofp_raw_action_type raw
;
8899 /* Looking up the action. */
8900 struct hmap_node decode_node
; /* Based on 'hdrs'. */
8901 struct hmap_node encode_node
; /* Based on 'raw' + 'hdrs.ofp_version'. */
8903 /* The action's encoded size.
8905 * If this action is fixed-length, 'min_length' == 'max_length'.
8906 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
8907 * OFP_ACTION_ALIGN) == 65528. */
8908 unsigned short int min_length
;
8909 unsigned short int max_length
;
8911 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
8912 * offset of that argument from the beginning of the action and 'arg_len'
8913 * its length, both in bytes.
8915 * For actions that take other forms, these are both zero. */
8916 unsigned short int arg_ofs
;
8917 unsigned short int arg_len
;
8919 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
8922 /* If this action is deprecated, a human-readable string with a brief
8924 const char *deprecation
;
8927 /* Action header. */
8928 struct ofp_action_header
{
8929 /* The meaning of other values of 'type' generally depends on the OpenFlow
8930 * version (see enum ofp_raw_action_type).
8932 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
8933 * designates an OpenFlow vendor ID and that the remainder of the action
8934 * structure has a vendor-defined meaning.
8936 #define OFPAT_VENDOR 0xffff
8939 /* Always a multiple of 8. */
8942 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
8943 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
8946 OFP_ASSERT(sizeof(struct ofp_action_header
) == 8);
8949 ofpact_hdrs_equal(const struct ofpact_hdrs
*a
,
8950 const struct ofpact_hdrs
*b
)
8952 return (a
->vendor
== b
->vendor
8953 && a
->type
== b
->type
8954 && a
->ofp_version
== b
->ofp_version
);
8958 ofpact_hdrs_hash(const struct ofpact_hdrs
*hdrs
)
8960 return hash_2words(hdrs
->vendor
, (hdrs
->type
<< 16) | hdrs
->ofp_version
);
8963 #include "ofp-actions.inc2"
8965 static struct hmap
*
8966 ofpact_decode_hmap(void)
8968 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
8969 static struct hmap hmap
;
8971 if (ovsthread_once_start(&once
)) {
8972 struct ofpact_raw_instance
*inst
;
8975 for (inst
= all_raw_instances
;
8976 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
8978 hmap_insert(&hmap
, &inst
->decode_node
,
8979 ofpact_hdrs_hash(&inst
->hdrs
));
8981 ovsthread_once_done(&once
);
8986 static struct hmap
*
8987 ofpact_encode_hmap(void)
8989 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
8990 static struct hmap hmap
;
8992 if (ovsthread_once_start(&once
)) {
8993 struct ofpact_raw_instance
*inst
;
8996 for (inst
= all_raw_instances
;
8997 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
8999 hmap_insert(&hmap
, &inst
->encode_node
,
9000 hash_2words(inst
->raw
, inst
->hdrs
.ofp_version
));
9002 ovsthread_once_done(&once
);
9008 ofpact_decode_raw(enum ofp_version ofp_version
,
9009 const struct ofp_action_header
*oah
, size_t length
,
9010 const struct ofpact_raw_instance
**instp
)
9012 const struct ofpact_raw_instance
*inst
;
9013 struct ofpact_hdrs hdrs
;
9016 if (length
< sizeof *oah
) {
9017 return OFPERR_OFPBAC_BAD_LEN
;
9020 /* Get base action type. */
9021 if (oah
->type
== htons(OFPAT_VENDOR
)) {
9023 hdrs
.vendor
= ntohl(oah
->vendor
);
9024 if (hdrs
.vendor
== NX_VENDOR_ID
|| hdrs
.vendor
== ONF_VENDOR_ID
) {
9025 /* Get extension subtype. */
9026 const struct ext_action_header
*nah
;
9028 nah
= ALIGNED_CAST(const struct ext_action_header
*, oah
);
9029 if (length
< sizeof *nah
) {
9030 return OFPERR_OFPBAC_BAD_LEN
;
9032 hdrs
.type
= ntohs(nah
->subtype
);
9034 VLOG_WARN_RL(&rl
, "OpenFlow action has unknown vendor %#"PRIx32
,
9036 return OFPERR_OFPBAC_BAD_VENDOR
;
9040 hdrs
.type
= ntohs(oah
->type
);
9043 hdrs
.ofp_version
= ofp_version
;
9044 HMAP_FOR_EACH_WITH_HASH (inst
, decode_node
, ofpact_hdrs_hash(&hdrs
),
9045 ofpact_decode_hmap()) {
9046 if (ofpact_hdrs_equal(&hdrs
, &inst
->hdrs
)) {
9052 VLOG_WARN_RL(&rl
, "unknown %s action for vendor %#"PRIx32
" and "
9053 "type %"PRIu16
, ofputil_version_to_string(ofp_version
),
9054 hdrs
.vendor
, hdrs
.type
);
9056 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
9057 : OFPERR_OFPBAC_BAD_TYPE
);
9061 ofpact_pull_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
9062 enum ofp_raw_action_type
*raw
, uint64_t *arg
)
9064 const struct ofp_action_header
*oah
= buf
->data
;
9065 const struct ofpact_raw_instance
*action
;
9066 unsigned int length
;
9070 error
= ofpact_decode_raw(ofp_version
, oah
, buf
->size
, &action
);
9075 if (action
->deprecation
) {
9076 VLOG_INFO_RL(&rl
, "%s is deprecated in %s (%s)",
9077 action
->name
, ofputil_version_to_string(ofp_version
),
9078 action
->deprecation
);
9081 length
= ntohs(oah
->len
);
9082 if (length
> buf
->size
) {
9083 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u exceeds action buffer "
9084 "length %"PRIu32
, action
->name
, length
, buf
->size
);
9085 return OFPERR_OFPBAC_BAD_LEN
;
9087 if (length
< action
->min_length
|| length
> action
->max_length
) {
9088 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u not in valid range "
9089 "[%hu,%hu]", action
->name
, length
,
9090 action
->min_length
, action
->max_length
);
9091 return OFPERR_OFPBAC_BAD_LEN
;
9094 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u is not a multiple "
9095 "of 8", action
->name
, length
);
9096 return OFPERR_OFPBAC_BAD_LEN
;
9101 if (action
->arg_len
) {
9105 p
= ofpbuf_at_assert(buf
, action
->arg_ofs
, action
->arg_len
);
9106 for (i
= 0; i
< action
->arg_len
; i
++) {
9107 *arg
= (*arg
<< 8) | p
[i
];
9111 ofpbuf_pull(buf
, length
);
9116 static const struct ofpact_raw_instance
*
9117 ofpact_raw_lookup(enum ofp_version ofp_version
, enum ofp_raw_action_type raw
)
9119 const struct ofpact_raw_instance
*inst
;
9121 HMAP_FOR_EACH_WITH_HASH (inst
, encode_node
, hash_2words(raw
, ofp_version
),
9122 ofpact_encode_hmap()) {
9123 if (inst
->raw
== raw
&& inst
->hdrs
.ofp_version
== ofp_version
) {
9131 ofpact_put_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
9132 enum ofp_raw_action_type raw
, uint64_t arg
)
9134 const struct ofpact_raw_instance
*inst
;
9135 struct ofp_action_header
*oah
;
9136 const struct ofpact_hdrs
*hdrs
;
9138 inst
= ofpact_raw_lookup(ofp_version
, raw
);
9141 oah
= ofpbuf_put_zeros(buf
, inst
->min_length
);
9142 oah
->type
= htons(hdrs
->vendor
? OFPAT_VENDOR
: hdrs
->type
);
9143 oah
->len
= htons(inst
->min_length
);
9144 oah
->vendor
= htonl(hdrs
->vendor
);
9146 switch (hdrs
->vendor
) {
9151 case ONF_VENDOR_ID
: {
9152 struct ext_action_header
*nah
= (struct ext_action_header
*) oah
;
9153 nah
->subtype
= htons(hdrs
->type
);
9161 if (inst
->arg_len
) {
9162 uint8_t *p
= (uint8_t *) oah
+ inst
->arg_ofs
+ inst
->arg_len
;
9165 for (i
= 0; i
< inst
->arg_len
; i
++) {
9177 pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
)
9179 struct ofp_action_header
*oah
;
9181 ofpbuf_put_zeros(openflow
, PAD_SIZE(openflow
->size
- start_ofs
,
9184 oah
= ofpbuf_at_assert(openflow
, start_ofs
, sizeof *oah
);
9185 oah
->len
= htons(openflow
->size
- start_ofs
);