2 * Copyright (c) 2008-2017, 2019-2020 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 /* OF1.5+(29): uint32_t. */
265 /* ## ------------------------- ## */
266 /* ## Nicira extension actions. ## */
267 /* ## ------------------------- ## */
269 /* Actions similar to standard actions are listed with the standard actions. */
271 /* NX1.0+(1): uint16_t. */
273 /* NX1.0+(14): struct nx_action_resubmit. */
274 NXAST_RAW_RESUBMIT_TABLE
,
275 /* NX1.0+(44): struct nx_action_resubmit. */
276 NXAST_RAW_RESUBMIT_TABLE_CT
,
278 /* NX1.0+(2): uint32_t. */
279 NXAST_RAW_SET_TUNNEL
,
280 /* NX1.0+(9): uint64_t. */
281 NXAST_RAW_SET_TUNNEL64
,
283 /* NX1.0+(5): void. */
286 /* NX1.0+(8): struct nx_action_note, ... */
289 /* NX1.0+(10): struct nx_action_multipath. VLMFF */
292 /* NX1.0+(12): struct nx_action_bundle, ... */
294 /* NX1.0+(13): struct nx_action_bundle, ... VLMFF */
295 NXAST_RAW_BUNDLE_LOAD
,
297 /* NX1.0+(15): struct nx_action_output_reg. VLMFF */
298 NXAST_RAW_OUTPUT_REG
,
299 /* NX1.0+(32): struct nx_action_output_reg2. VLMFF */
300 NXAST_RAW_OUTPUT_REG2
,
302 /* NX1.0+(16): struct nx_action_learn, ... VLMFF */
304 /* NX1.0+(45): struct nx_action_learn2, ... VLMFF */
307 /* NX1.0+(17): void. */
310 /* NX1.0+(19): struct nx_action_fin_timeout. */
311 NXAST_RAW_FIN_TIMEOUT
,
313 /* NX1.0+(20): struct nx_action_controller. */
314 NXAST_RAW_CONTROLLER
,
315 /* NX1.0+(37): struct ext_action_header, ... */
316 NXAST_RAW_CONTROLLER2
,
318 /* NX1.0+(22): struct nx_action_write_metadata. */
319 NXAST_RAW_WRITE_METADATA
,
321 /* NX1.0+(27): struct nx_action_stack. VLMFF */
322 NXAST_RAW_STACK_PUSH
,
324 /* NX1.0+(28): struct nx_action_stack. VLMFF */
327 /* NX1.0+(29): struct nx_action_sample. */
329 /* NX1.0+(38): struct nx_action_sample2. */
331 /* NX1.0+(41): struct nx_action_sample2. */
334 /* NX1.0+(34): struct nx_action_conjunction. */
335 NXAST_RAW_CONJUNCTION
,
337 /* NX1.0+(35): struct nx_action_conntrack, ... VLMFF */
340 /* NX1.0+(36): struct nx_action_nat, ... */
343 /* NX1.0+(39): struct nx_action_output_trunc. */
344 NXAST_RAW_OUTPUT_TRUNC
,
346 /* NX1.0+(42): struct ext_action_header, ... VLMFF */
349 /* NX1.0+(43): void. */
352 /* NX1.3+(46): struct nx_action_encap, ... */
355 /* NX1.3+(47): struct nx_action_decap, ... */
358 /* NX1.3+(48): void. */
359 NXAST_RAW_DEC_NSH_TTL
,
361 /* NX1.0+(49): struct nx_action_check_pkt_larger, ... VLMFF */
362 NXAST_RAW_CHECK_PKT_LARGER
,
364 /* NX1.0+(50): struct nx_action_delete_field. VLMFF */
365 NXAST_RAW_DELETE_FIELD
,
367 /* ## ------------------ ## */
368 /* ## Debugging actions. ## */
369 /* ## ------------------ ## */
371 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
372 /* accepts them only if started with --enable-dummy. */
374 /* NX1.0+(254): void. */
375 NXAST_RAW_DEBUG_SLOW
,
377 /* NX1.0+(255): void. */
378 NXAST_RAW_DEBUG_RECIRC
,
381 /* OpenFlow actions are always a multiple of 8 bytes in length. */
382 #define OFP_ACTION_ALIGN 8
384 /* Define a few functions for working with instructions. */
385 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
386 static inline const struct STRUCT * OVS_UNUSED \
387 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
389 ovs_assert(inst->type == htons(ENUM)); \
390 return ALIGNED_CAST(struct STRUCT *, inst); \
393 static inline void OVS_UNUSED \
394 instruction_init_##ENUM(struct STRUCT *s) \
396 memset(s, 0, sizeof *s); \
397 s->type = htons(ENUM); \
398 s->len = htons(sizeof *s); \
401 static inline struct STRUCT * OVS_UNUSED \
402 instruction_put_##ENUM(struct ofpbuf *buf) \
404 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
405 instruction_init_##ENUM(s); \
411 static void ofpacts_update_instruction_actions(struct ofpbuf
*openflow
,
413 static void pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
);
415 static enum ofperr
ofpacts_verify(const struct ofpact
[], size_t ofpacts_len
,
416 enum ofp_version
, uint32_t allowed_ovsinsts
,
417 enum ofpact_type outer_action
,
420 static void put_set_field(struct ofpbuf
*openflow
, enum ofp_version
,
421 enum mf_field_id
, uint64_t value
);
423 static void put_reg_load(struct ofpbuf
*openflow
,
424 const struct mf_subfield
*, uint64_t value
);
426 static enum ofperr
ofpact_pull_raw(struct ofpbuf
*, enum ofp_version
,
427 enum ofp_raw_action_type
*, uint64_t *arg
);
428 static void *ofpact_put_raw(struct ofpbuf
*, enum ofp_version
,
429 enum ofp_raw_action_type
, uint64_t arg
);
431 static char *OVS_WARN_UNUSED_RESULT
ofpacts_parse(
432 char *str
, const struct ofpact_parse_params
*pp
,
433 bool allow_instructions
, enum ofpact_type outer_action
);
434 static enum ofperr
ofpacts_pull_openflow_actions__(
435 struct ofpbuf
*openflow
, unsigned int actions_len
,
436 enum ofp_version version
, uint32_t allowed_ovsinsts
,
437 struct ofpbuf
*ofpacts
, enum ofpact_type outer_action
,
438 const struct vl_mff_map
*vl_mff_map
, uint64_t *ofpacts_tlv_bitmap
);
439 static char * OVS_WARN_UNUSED_RESULT
ofpacts_parse_copy(
440 const char *s_
, const struct ofpact_parse_params
*pp
,
441 bool allow_instructions
, enum ofpact_type outer_action
);
443 static void inconsistent_match(enum ofputil_protocol
*usable_protocols
);
445 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
446 * nested ofpacts it returns the first one. */
448 ofpact_next_flattened(const struct ofpact
*ofpact
)
450 switch (ofpact
->type
) {
453 case OFPACT_CONTROLLER
:
455 case OFPACT_OUTPUT_REG
:
456 case OFPACT_OUTPUT_TRUNC
:
458 case OFPACT_SET_FIELD
:
459 case OFPACT_SET_VLAN_VID
:
460 case OFPACT_SET_VLAN_PCP
:
461 case OFPACT_STRIP_VLAN
:
462 case OFPACT_PUSH_VLAN
:
463 case OFPACT_SET_ETH_SRC
:
464 case OFPACT_SET_ETH_DST
:
465 case OFPACT_SET_IPV4_SRC
:
466 case OFPACT_SET_IPV4_DST
:
467 case OFPACT_SET_IP_DSCP
:
468 case OFPACT_SET_IP_ECN
:
469 case OFPACT_SET_IP_TTL
:
470 case OFPACT_SET_L4_SRC_PORT
:
471 case OFPACT_SET_L4_DST_PORT
:
472 case OFPACT_REG_MOVE
:
473 case OFPACT_STACK_PUSH
:
474 case OFPACT_STACK_POP
:
476 case OFPACT_SET_MPLS_LABEL
:
477 case OFPACT_SET_MPLS_TC
:
478 case OFPACT_SET_MPLS_TTL
:
479 case OFPACT_DEC_MPLS_TTL
:
480 case OFPACT_PUSH_MPLS
:
481 case OFPACT_POP_MPLS
:
482 case OFPACT_SET_TUNNEL
:
483 case OFPACT_SET_QUEUE
:
484 case OFPACT_POP_QUEUE
:
485 case OFPACT_FIN_TIMEOUT
:
486 case OFPACT_RESUBMIT
:
488 case OFPACT_CONJUNCTION
:
489 case OFPACT_MULTIPATH
:
493 case OFPACT_UNROLL_XLATE
:
494 case OFPACT_CT_CLEAR
:
495 case OFPACT_DEBUG_RECIRC
:
496 case OFPACT_DEBUG_SLOW
:
498 case OFPACT_CLEAR_ACTIONS
:
499 case OFPACT_WRITE_METADATA
:
500 case OFPACT_GOTO_TABLE
:
504 case OFPACT_DEC_NSH_TTL
:
505 case OFPACT_CHECK_PKT_LARGER
:
506 case OFPACT_DELETE_FIELD
:
507 return ofpact_next(ofpact
);
510 return ofpact_get_CLONE(ofpact
)->actions
;
513 return ofpact_get_CT(ofpact
)->actions
;
515 case OFPACT_WRITE_ACTIONS
:
516 return ofpact_get_WRITE_ACTIONS(ofpact
)->actions
;
522 /* Pull off existing actions or instructions. Used by nesting actions to keep
523 * ofpacts_parse() oblivious of actions nesting.
525 * Push the actions back on after nested parsing, e.g.:
527 * size_t ofs = ofpacts_pull(ofpacts);
528 * ...nested parsing...
529 * ofpbuf_push_uninit(ofpacts, ofs);
532 ofpacts_pull(struct ofpbuf
*ofpacts
)
537 ofpbuf_pull(ofpacts
, ofs
);
542 #include "ofp-actions.inc1"
544 /* Output actions. */
546 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
547 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
548 * number of bytes to send. A 'max_len' of zero means no bytes of the
549 * packet should be sent. */
550 struct ofp10_action_output
{
551 ovs_be16 type
; /* OFPAT10_OUTPUT. */
552 ovs_be16 len
; /* Length is 8. */
553 ovs_be16 port
; /* Output port. */
554 ovs_be16 max_len
; /* Max length to send to controller. */
556 OFP_ASSERT(sizeof(struct ofp10_action_output
) == 8);
558 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
559 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
560 * number of bytes to send. A 'max_len' of zero means no bytes of the
561 * packet should be sent.*/
562 struct ofp11_action_output
{
563 ovs_be16 type
; /* OFPAT11_OUTPUT. */
564 ovs_be16 len
; /* Length is 16. */
565 ovs_be32 port
; /* Output port. */
566 ovs_be16 max_len
; /* Max length to send to controller. */
567 uint8_t pad
[6]; /* Pad to 64 bits. */
569 OFP_ASSERT(sizeof(struct ofp11_action_output
) == 16);
572 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output
*oao
,
573 enum ofp_version ofp_version OVS_UNUSED
,
576 struct ofpact_output
*output
;
578 output
= ofpact_put_OUTPUT(out
);
579 output
->port
= u16_to_ofp(ntohs(oao
->port
));
580 output
->max_len
= ntohs(oao
->max_len
);
582 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
586 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output
*oao
,
587 enum ofp_version ofp_version OVS_UNUSED
,
590 struct ofpact_output
*output
;
593 output
= ofpact_put_OUTPUT(out
);
594 output
->max_len
= ntohs(oao
->max_len
);
596 error
= ofputil_port_from_ofp11(oao
->port
, &output
->port
);
601 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
605 encode_OUTPUT(const struct ofpact_output
*output
,
606 enum ofp_version ofp_version
, struct ofpbuf
*out
)
608 if (ofp_version
== OFP10_VERSION
) {
609 struct ofp10_action_output
*oao
;
611 oao
= put_OFPAT10_OUTPUT(out
);
612 oao
->port
= htons(ofp_to_u16(output
->port
));
613 oao
->max_len
= htons(output
->max_len
);
615 struct ofp11_action_output
*oao
;
617 oao
= put_OFPAT11_OUTPUT(out
);
618 oao
->port
= ofputil_port_to_ofp11(output
->port
);
619 oao
->max_len
= htons(output
->max_len
);
623 static char * OVS_WARN_UNUSED_RESULT
624 parse_truncate_subfield(const char *arg_
,
625 const struct ofpact_parse_params
*pp
,
626 struct ofpact_output_trunc
*output_trunc
)
629 char *arg
= CONST_CAST(char *, arg_
);
631 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
632 if (!strcmp(key
, "port")) {
633 if (!ofputil_port_from_string(value
, pp
->port_map
,
634 &output_trunc
->port
)) {
635 return xasprintf("output to unknown truncate port: %s",
638 if (ofp_to_u16(output_trunc
->port
) > ofp_to_u16(OFPP_MAX
)) {
639 if (output_trunc
->port
!= OFPP_LOCAL
&&
640 output_trunc
->port
!= OFPP_IN_PORT
)
641 return xasprintf("output to unsupported truncate port: %s",
644 } else if (!strcmp(key
, "max_len")) {
647 err
= str_to_u32(value
, &output_trunc
->max_len
);
652 if (output_trunc
->max_len
< ETH_HEADER_LEN
) {
653 return xasprintf("max_len %"PRIu32
" is less than the minimum "
655 output_trunc
->max_len
, ETH_HEADER_LEN
);
658 return xasprintf("invalid key '%s' in output_trunc argument",
665 static char * OVS_WARN_UNUSED_RESULT
666 parse_OUTPUT(const char *arg
, const struct ofpact_parse_params
*pp
)
668 if (strstr(arg
, "port") && strstr(arg
, "max_len")) {
669 struct ofpact_output_trunc
*output_trunc
;
671 output_trunc
= ofpact_put_OUTPUT_TRUNC(pp
->ofpacts
);
672 return parse_truncate_subfield(arg
, pp
, output_trunc
);
676 if (ofputil_port_from_string(arg
, pp
->port_map
, &port
)) {
677 struct ofpact_output
*output
= ofpact_put_OUTPUT(pp
->ofpacts
);
679 output
->max_len
= output
->port
== OFPP_CONTROLLER
? UINT16_MAX
: 0;
683 struct mf_subfield src
;
684 char *error
= mf_parse_subfield(&src
, arg
);
686 struct ofpact_output_reg
*output_reg
;
688 output_reg
= ofpact_put_OUTPUT_REG(pp
->ofpacts
);
689 output_reg
->max_len
= UINT16_MAX
;
690 output_reg
->src
= src
;
695 return xasprintf("%s: output to unknown port", arg
);
699 format_OUTPUT(const struct ofpact_output
*a
,
700 const struct ofpact_format_params
*fp
)
702 if (ofp_to_u16(a
->port
) < ofp_to_u16(OFPP_MAX
)) {
703 ds_put_format(fp
->s
, "%soutput:%s", colors
.special
, colors
.end
);
705 ofputil_format_port(a
->port
, fp
->port_map
, fp
->s
);
706 if (a
->port
== OFPP_CONTROLLER
) {
707 ds_put_format(fp
->s
, ":%"PRIu16
, a
->max_len
);
712 check_OUTPUT(const struct ofpact_output
*a
,
713 const struct ofpact_check_params
*cp
)
715 return ofpact_check_output_port(a
->port
, cp
->max_ports
);
721 decode_OFPAT_RAW_GROUP(uint32_t group_id
,
722 enum ofp_version ofp_version OVS_UNUSED
,
725 ofpact_put_GROUP(out
)->group_id
= group_id
;
730 encode_GROUP(const struct ofpact_group
*group
,
731 enum ofp_version ofp_version
, struct ofpbuf
*out
)
733 put_OFPAT_GROUP(out
, ofp_version
, group
->group_id
);
736 static char * OVS_WARN_UNUSED_RESULT
737 parse_GROUP(char *arg
, const struct ofpact_parse_params
*pp
)
739 return str_to_u32(arg
, &ofpact_put_GROUP(pp
->ofpacts
)->group_id
);
743 format_GROUP(const struct ofpact_group
*a
,
744 const struct ofpact_format_params
*fp
)
746 ds_put_format(fp
->s
, "%sgroup:%s%"PRIu32
,
747 colors
.special
, colors
.end
, a
->group_id
);
751 check_GROUP(const struct ofpact_group
*a OVS_UNUSED
,
752 const struct ofpact_check_params
*cp OVS_UNUSED
)
757 /* Action structure for NXAST_CONTROLLER.
759 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
760 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
763 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
765 * - 'controller_id': The ID of the controller connection to which the
766 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
767 * sent only to controllers that have the specified controller connection
768 * ID. See "struct nx_controller_id" for more information. */
769 struct nx_action_controller
{
770 ovs_be16 type
; /* OFPAT_VENDOR. */
771 ovs_be16 len
; /* Length is 16. */
772 ovs_be32 vendor
; /* NX_VENDOR_ID. */
773 ovs_be16 subtype
; /* NXAST_CONTROLLER. */
774 ovs_be16 max_len
; /* Maximum length to send to controller. */
775 ovs_be16 controller_id
; /* Controller ID to send packet-in. */
776 uint8_t reason
; /* enum ofp_packet_in_reason (OFPR_*). */
777 uint8_t zero
; /* Must be zero. */
779 OFP_ASSERT(sizeof(struct nx_action_controller
) == 16);
781 /* Properties for NXAST_CONTROLLER2.
783 * For more information on the effect of NXAC2PT_PAUSE, see the large comment
784 * on NXT_PACKET_IN2 in nicira-ext.h */
785 enum nx_action_controller2_prop_type
{
786 NXAC2PT_MAX_LEN
, /* ovs_be16 max bytes to send (default all). */
787 NXAC2PT_CONTROLLER_ID
, /* ovs_be16 dest controller ID (default 0). */
788 NXAC2PT_REASON
, /* uint8_t reason (OFPR_*), default 0. */
789 NXAC2PT_USERDATA
, /* Data to copy into NXPINT_USERDATA. */
790 NXAC2PT_PAUSE
, /* Flag to pause pipeline to resume later. */
791 NXAC2PT_METER_ID
, /* ovs_b32 meter (default NX_CTLR_NO_METER). */
794 /* The action structure for NXAST_CONTROLLER2 is "struct ext_action_header",
795 * followed by NXAC2PT_* properties. */
798 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller
*nac
,
799 enum ofp_version ofp_version OVS_UNUSED
,
802 struct ofpact_controller
*oc
;
804 oc
= ofpact_put_CONTROLLER(out
);
805 oc
->ofpact
.raw
= NXAST_RAW_CONTROLLER
;
806 oc
->max_len
= ntohs(nac
->max_len
);
807 oc
->controller_id
= ntohs(nac
->controller_id
);
808 oc
->reason
= nac
->reason
;
809 oc
->meter_id
= NX_CTLR_NO_METER
;
810 ofpact_finish_CONTROLLER(out
, &oc
);
816 decode_NXAST_RAW_CONTROLLER2(const struct ext_action_header
*eah
,
817 enum ofp_version ofp_version OVS_UNUSED
,
820 if (!is_all_zeros(eah
->pad
, sizeof eah
->pad
)) {
821 return OFPERR_NXBRC_MUST_BE_ZERO
;
824 size_t start_ofs
= out
->size
;
825 struct ofpact_controller
*oc
= ofpact_put_CONTROLLER(out
);
826 oc
->ofpact
.raw
= NXAST_RAW_CONTROLLER2
;
827 oc
->max_len
= UINT16_MAX
;
828 oc
->reason
= OFPR_ACTION
;
829 oc
->meter_id
= NX_CTLR_NO_METER
;
831 struct ofpbuf properties
;
832 ofpbuf_use_const(&properties
, eah
, ntohs(eah
->len
));
833 ofpbuf_pull(&properties
, sizeof *eah
);
835 while (properties
.size
> 0) {
836 struct ofpbuf payload
;
839 enum ofperr error
= ofpprop_pull(&properties
, &payload
, &type
);
845 case NXAC2PT_MAX_LEN
:
846 error
= ofpprop_parse_u16(&payload
, &oc
->max_len
);
849 case NXAC2PT_CONTROLLER_ID
:
850 error
= ofpprop_parse_u16(&payload
, &oc
->controller_id
);
853 case NXAC2PT_REASON
: {
855 error
= ofpprop_parse_u8(&payload
, &u8
);
860 case NXAC2PT_USERDATA
:
861 out
->size
= start_ofs
+ sizeof(struct ofpact_controller
);
862 ofpbuf_put(out
, payload
.msg
, ofpbuf_msgsize(&payload
));
863 oc
= ofpbuf_at_assert(out
, start_ofs
, sizeof *oc
);
864 oc
->userdata_len
= ofpbuf_msgsize(&payload
);
871 case NXAC2PT_METER_ID
:
872 error
= ofpprop_parse_u32(&payload
, &oc
->meter_id
);
876 error
= OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type
);
884 ofpact_finish_CONTROLLER(out
, &oc
);
890 encode_CONTROLLER(const struct ofpact_controller
*controller
,
891 enum ofp_version ofp_version OVS_UNUSED
,
894 if (controller
->userdata_len
896 || controller
->meter_id
!= NX_CTLR_NO_METER
897 || controller
->ofpact
.raw
== NXAST_RAW_CONTROLLER2
) {
898 size_t start_ofs
= out
->size
;
899 put_NXAST_CONTROLLER2(out
);
900 if (controller
->max_len
!= UINT16_MAX
) {
901 ofpprop_put_u16(out
, NXAC2PT_MAX_LEN
, controller
->max_len
);
903 if (controller
->controller_id
!= 0) {
904 ofpprop_put_u16(out
, NXAC2PT_CONTROLLER_ID
,
905 controller
->controller_id
);
907 if (controller
->reason
!= OFPR_ACTION
) {
908 ofpprop_put_u8(out
, NXAC2PT_REASON
, controller
->reason
);
910 if (controller
->userdata_len
!= 0) {
911 ofpprop_put(out
, NXAC2PT_USERDATA
, controller
->userdata
,
912 controller
->userdata_len
);
914 if (controller
->pause
) {
915 ofpprop_put_flag(out
, NXAC2PT_PAUSE
);
917 if (controller
->meter_id
!= NX_CTLR_NO_METER
) {
918 ofpprop_put_u32(out
, NXAC2PT_METER_ID
, controller
->meter_id
);
920 pad_ofpat(out
, start_ofs
);
922 struct nx_action_controller
*nac
;
924 nac
= put_NXAST_CONTROLLER(out
);
925 nac
->max_len
= htons(controller
->max_len
);
926 nac
->controller_id
= htons(controller
->controller_id
);
927 nac
->reason
= controller
->reason
;
931 static char * OVS_WARN_UNUSED_RESULT
932 parse_CONTROLLER(char *arg
, const struct ofpact_parse_params
*pp
)
934 enum ofp_packet_in_reason reason
= OFPR_ACTION
;
935 uint16_t controller_id
= 0;
936 uint16_t max_len
= UINT16_MAX
;
937 uint32_t meter_id
= NX_CTLR_NO_METER
;
938 const char *userdata
= NULL
;
943 } else if (strspn(arg
, "0123456789") == strlen(arg
)) {
944 char *error
= str_to_u16(arg
, "max_len", &max_len
);
951 while (ofputil_parse_key_value(&arg
, &name
, &value
)) {
952 if (!strcmp(name
, "reason")) {
953 if (!ofputil_packet_in_reason_from_string(value
, &reason
)) {
954 return xasprintf("unknown reason \"%s\"", value
);
956 } else if (!strcmp(name
, "max_len")) {
957 char *error
= str_to_u16(value
, "max_len", &max_len
);
961 } else if (!strcmp(name
, "id")) {
962 char *error
= str_to_u16(value
, "id", &controller_id
);
966 } else if (!strcmp(name
, "userdata")) {
968 } else if (!strcmp(name
, "pause")) {
970 } else if (!strcmp(name
, "meter_id")) {
971 char *error
= str_to_u32(value
, &meter_id
);
976 return xasprintf("unknown key \"%s\" parsing controller "
982 if (reason
== OFPR_ACTION
&& controller_id
== 0 && !userdata
&& !pause
983 && meter_id
== NX_CTLR_NO_METER
) {
984 struct ofpact_output
*output
;
986 output
= ofpact_put_OUTPUT(pp
->ofpacts
);
987 output
->port
= OFPP_CONTROLLER
;
988 output
->max_len
= max_len
;
990 struct ofpact_controller
*controller
;
992 controller
= ofpact_put_CONTROLLER(pp
->ofpacts
);
993 controller
->max_len
= max_len
;
994 controller
->reason
= reason
;
995 controller
->controller_id
= controller_id
;
996 controller
->pause
= pause
;
997 controller
->meter_id
= meter_id
;
1000 size_t start_ofs
= pp
->ofpacts
->size
;
1001 const char *end
= ofpbuf_put_hex(pp
->ofpacts
, userdata
, NULL
);
1003 return xstrdup("bad hex digit in `controller' "
1004 "action `userdata'");
1006 size_t userdata_len
= pp
->ofpacts
->size
- start_ofs
;
1007 controller
= pp
->ofpacts
->header
;
1008 controller
->userdata_len
= userdata_len
;
1011 if (ofpbuf_oversized(pp
->ofpacts
)) {
1012 return xasprintf("input too big");
1015 ofpact_finish_CONTROLLER(pp
->ofpacts
, &controller
);
1022 format_hex_arg(struct ds
*s
, const uint8_t *data
, size_t len
)
1024 for (size_t i
= 0; i
< len
; i
++) {
1026 ds_put_char(s
, '.');
1028 ds_put_format(s
, "%02"PRIx8
, data
[i
]);
1033 format_CONTROLLER(const struct ofpact_controller
*a
,
1034 const struct ofpact_format_params
*fp
)
1036 if (a
->reason
== OFPR_ACTION
&& !a
->controller_id
&& !a
->userdata_len
1037 && !a
->pause
&& a
->meter_id
== NX_CTLR_NO_METER
) {
1038 ds_put_format(fp
->s
, "%sCONTROLLER:%s%"PRIu16
,
1039 colors
.special
, colors
.end
, a
->max_len
);
1041 enum ofp_packet_in_reason reason
= a
->reason
;
1043 ds_put_format(fp
->s
, "%scontroller(%s", colors
.paren
, colors
.end
);
1044 if (reason
!= OFPR_ACTION
) {
1045 char reasonbuf
[OFPUTIL_PACKET_IN_REASON_BUFSIZE
];
1047 ds_put_format(fp
->s
, "%sreason=%s%s,", colors
.param
, colors
.end
,
1048 ofputil_packet_in_reason_to_string(
1049 reason
, reasonbuf
, sizeof reasonbuf
));
1051 if (a
->max_len
!= UINT16_MAX
) {
1052 ds_put_format(fp
->s
, "%smax_len=%s%"PRIu16
",",
1053 colors
.param
, colors
.end
, a
->max_len
);
1055 if (a
->controller_id
!= 0) {
1056 ds_put_format(fp
->s
, "%sid=%s%"PRIu16
",",
1057 colors
.param
, colors
.end
, a
->controller_id
);
1059 if (a
->userdata_len
) {
1060 ds_put_format(fp
->s
, "%suserdata=%s", colors
.param
, colors
.end
);
1061 format_hex_arg(fp
->s
, a
->userdata
, a
->userdata_len
);
1062 ds_put_char(fp
->s
, ',');
1065 ds_put_format(fp
->s
, "%spause%s,", colors
.value
, colors
.end
);
1067 if (a
->meter_id
!= NX_CTLR_NO_METER
) {
1068 ds_put_format(fp
->s
, "%smeter_id=%s%"PRIu32
",",
1069 colors
.param
, colors
.end
, a
->meter_id
);
1071 ds_chomp(fp
->s
, ',');
1072 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
1077 check_CONTROLLER(const struct ofpact_controller
*a OVS_UNUSED
,
1078 const struct ofpact_check_params
*cp OVS_UNUSED
)
1083 /* Enqueue action. */
1084 struct ofp10_action_enqueue
{
1085 ovs_be16 type
; /* OFPAT10_ENQUEUE. */
1086 ovs_be16 len
; /* Len is 16. */
1087 ovs_be16 port
; /* Port that queue belongs. Should
1088 refer to a valid physical port
1089 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
1090 uint8_t pad
[6]; /* Pad for 64-bit alignment. */
1091 ovs_be32 queue_id
; /* Where to enqueue the packets. */
1093 OFP_ASSERT(sizeof(struct ofp10_action_enqueue
) == 16);
1096 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue
*oae
,
1097 enum ofp_version ofp_version OVS_UNUSED
,
1100 struct ofpact_enqueue
*enqueue
;
1102 enqueue
= ofpact_put_ENQUEUE(out
);
1103 enqueue
->port
= u16_to_ofp(ntohs(oae
->port
));
1104 enqueue
->queue
= ntohl(oae
->queue_id
);
1105 if (ofp_to_u16(enqueue
->port
) >= ofp_to_u16(OFPP_MAX
)
1106 && enqueue
->port
!= OFPP_IN_PORT
1107 && enqueue
->port
!= OFPP_LOCAL
) {
1108 return OFPERR_OFPBAC_BAD_OUT_PORT
;
1114 encode_ENQUEUE(const struct ofpact_enqueue
*enqueue
,
1115 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1117 if (ofp_version
== OFP10_VERSION
) {
1118 struct ofp10_action_enqueue
*oae
;
1120 oae
= put_OFPAT10_ENQUEUE(out
);
1121 oae
->port
= htons(ofp_to_u16(enqueue
->port
));
1122 oae
->queue_id
= htonl(enqueue
->queue
);
1124 put_OFPAT_SET_QUEUE(out
, ofp_version
, enqueue
->queue
);
1126 struct ofp11_action_output
*oao
= put_OFPAT11_OUTPUT(out
);
1127 oao
->port
= ofputil_port_to_ofp11(enqueue
->port
);
1128 oao
->max_len
= OVS_BE16_MAX
;
1130 put_NXAST_POP_QUEUE(out
);
1134 static char * OVS_WARN_UNUSED_RESULT
1135 parse_ENQUEUE(char *arg
, const struct ofpact_parse_params
*pp
)
1138 char *port
= strtok_r(arg
, ":q,", &sp
);
1139 char *queue
= strtok_r(NULL
, "", &sp
);
1140 struct ofpact_enqueue
*enqueue
;
1142 if (port
== NULL
|| queue
== NULL
) {
1143 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
1144 "\"enqueue(PORT,QUEUE)\"");
1147 enqueue
= ofpact_put_ENQUEUE(pp
->ofpacts
);
1148 if (!ofputil_port_from_string(port
, pp
->port_map
, &enqueue
->port
)) {
1149 return xasprintf("%s: enqueue to unknown port", port
);
1151 return str_to_u32(queue
, &enqueue
->queue
);
1155 format_ENQUEUE(const struct ofpact_enqueue
*a
,
1156 const struct ofpact_format_params
*fp
)
1158 ds_put_format(fp
->s
, "%senqueue:%s", colors
.param
, colors
.end
);
1159 ofputil_format_port(a
->port
, fp
->port_map
, fp
->s
);
1160 ds_put_format(fp
->s
, ":%"PRIu32
, a
->queue
);
1164 check_ENQUEUE(const struct ofpact_enqueue
*a
,
1165 const struct ofpact_check_params
*cp
)
1167 if (ofp_to_u16(a
->port
) >= ofp_to_u16(cp
->max_ports
)
1168 && a
->port
!= OFPP_IN_PORT
1169 && a
->port
!= OFPP_LOCAL
) {
1170 return OFPERR_OFPBAC_BAD_OUT_PORT
;
1175 /* Action structure for NXAST_OUTPUT_REG.
1177 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
1179 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
1180 * the NXAST_REG_LOAD action.
1182 * The acceptable nxm_header values for 'src' are the same as the acceptable
1183 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
1185 * The 'max_len' field indicates the number of bytes to send when the chosen
1186 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
1187 * field of OFPAT_OUTPUT.
1189 * The 'zero' field is required to be zeroed for forward compatibility. */
1190 struct nx_action_output_reg
{
1191 ovs_be16 type
; /* OFPAT_VENDOR. */
1192 ovs_be16 len
; /* 24. */
1193 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1194 ovs_be16 subtype
; /* NXAST_OUTPUT_REG. */
1196 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1197 ovs_be32 src
; /* Source. */
1199 ovs_be16 max_len
; /* Max length to send to controller. */
1201 uint8_t zero
[6]; /* Reserved, must be zero. */
1203 OFP_ASSERT(sizeof(struct nx_action_output_reg
) == 24);
1205 /* Action structure for NXAST_OUTPUT_REG2.
1207 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
1208 * experimenter OXM as 'src'.
1210 struct nx_action_output_reg2
{
1211 ovs_be16 type
; /* OFPAT_VENDOR. */
1212 ovs_be16 len
; /* 24. */
1213 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1214 ovs_be16 subtype
; /* NXAST_OUTPUT_REG2. */
1216 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1217 ovs_be16 max_len
; /* Max length to send to controller. */
1220 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1221 * - Enough 0-bytes to pad the action out to 24 bytes. */
1224 OFP_ASSERT(sizeof(struct nx_action_output_reg2
) == 24);
1227 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg
*naor
,
1228 enum ofp_version ofp_version OVS_UNUSED
,
1229 const struct vl_mff_map
*vl_mff_map
,
1230 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
1232 struct ofpact_output_reg
*output_reg
;
1235 if (!is_all_zeros(naor
->zero
, sizeof naor
->zero
)) {
1236 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1239 output_reg
= ofpact_put_OUTPUT_REG(out
);
1240 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG
;
1241 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
1242 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
1243 output_reg
->max_len
= ntohs(naor
->max_len
);
1244 error
= mf_vl_mff_mf_from_nxm_header(ntohl(naor
->src
), vl_mff_map
,
1245 &output_reg
->src
.field
, tlv_bitmap
);
1250 return mf_check_src(&output_reg
->src
, NULL
);
1254 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2
*naor
,
1255 enum ofp_version ofp_version OVS_UNUSED
,
1256 const struct vl_mff_map
*vl_mff_map
,
1257 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
1259 struct ofpact_output_reg
*output_reg
;
1262 output_reg
= ofpact_put_OUTPUT_REG(out
);
1263 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG2
;
1264 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
1265 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
1266 output_reg
->max_len
= ntohs(naor
->max_len
);
1268 struct ofpbuf b
= ofpbuf_const_initializer(naor
, ntohs(naor
->len
));
1269 ofpbuf_pull(&b
, OBJECT_OFFSETOF(naor
, pad
));
1271 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &output_reg
->src
.field
,
1277 if (!is_all_zeros(b
.data
, b
.size
)) {
1278 return OFPERR_NXBRC_MUST_BE_ZERO
;
1281 return mf_check_src(&output_reg
->src
, NULL
);
1285 encode_OUTPUT_REG(const struct ofpact_output_reg
*output_reg
,
1286 enum ofp_version ofp_version OVS_UNUSED
,
1289 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1290 * cannot be encoded in the older form, encode it as
1291 * NXAST_RAW_OUTPUT_REG2. */
1292 if (output_reg
->ofpact
.raw
== NXAST_RAW_OUTPUT_REG2
1293 || !mf_nxm_header(output_reg
->src
.field
->id
)) {
1294 struct nx_action_output_reg2
*naor
= put_NXAST_OUTPUT_REG2(out
);
1295 size_t size
= out
->size
;
1297 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
1298 output_reg
->src
.n_bits
);
1299 naor
->max_len
= htons(output_reg
->max_len
);
1301 out
->size
= size
- sizeof naor
->pad
;
1302 nx_put_mff_header(out
, output_reg
->src
.field
, 0, false);
1305 struct nx_action_output_reg
*naor
= put_NXAST_OUTPUT_REG(out
);
1307 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
1308 output_reg
->src
.n_bits
);
1309 naor
->src
= htonl(nxm_header_from_mff(output_reg
->src
.field
));
1310 naor
->max_len
= htons(output_reg
->max_len
);
1314 static char * OVS_WARN_UNUSED_RESULT
1315 parse_OUTPUT_REG(const char *arg
, const struct ofpact_parse_params
*pp
)
1317 return parse_OUTPUT(arg
, pp
);
1321 format_OUTPUT_REG(const struct ofpact_output_reg
*a
,
1322 const struct ofpact_format_params
*fp
)
1324 ds_put_format(fp
->s
, "%soutput:%s", colors
.special
, colors
.end
);
1325 mf_format_subfield(&a
->src
, fp
->s
);
1329 check_OUTPUT_REG(const struct ofpact_output_reg
*a
,
1330 const struct ofpact_check_params
*cp
)
1332 return mf_check_src(&a
->src
, cp
->match
);
1335 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1337 * The bundle actions choose a member from a supplied list of options.
1338 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
1339 * selection to a register.
1341 * The list of possible members follows the nx_action_bundle structure. The
1342 * size of each member is governed by its type as indicated by the
1343 * 'member_type' parameter. The list of members should be padded at its end
1344 * with zeros to make the total length of the action a multiple of 8.
1346 * Switches infer from the 'member_type' parameter the size of each member.
1347 * All implementations must support the NXM_OF_IN_PORT 'member_type' which
1348 * indicates that the members are OpenFlow port numbers with
1349 * NXM_LENGTH(NXM_OF_IN_PORT) == 2 byte width. Switches should reject actions
1350 * which indicate unknown or unsupported member types.
1352 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1353 * member. If the switch does not support the specified 'algorithm' parameter,
1354 * it should reject the action.
1356 * Several algorithms take into account liveness when selecting members. The
1357 * liveness of a member is implementation defined (with one exception), but
1358 * will generally take into account things like its carrier status and the
1359 * results of any link monitoring protocols which happen to be running on it.
1360 * In order to give controllers a place-holder value, the OFPP_NONE port is
1361 * always considered live, that is, NXAST_BUNDLE_LOAD stores OFPP_NONE in the
1362 * output register if no member is live.
1364 * Some member selection strategies require the use of a hash function, in
1365 * which case the 'fields' and 'basis' parameters should be populated. The
1366 * 'fields' parameter (one of NX_HASH_FIELDS_*) designates which parts of the
1367 * flow to hash. Refer to the definition of "enum nx_hash_fields" for details.
1368 * The 'basis' parameter is used as a universal hash parameter. Different
1369 * values of 'basis' yield different hash results.
1371 * The 'zero' parameter at the end of the action structure is reserved for
1372 * future use. Switches are required to reject actions which have nonzero
1373 * bytes in the 'zero' field.
1375 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
1376 * should reject actions which have nonzero bytes in either of these fields.
1378 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected member in
1379 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
1380 * similar to those for the NXAST_REG_LOAD action. */
1381 struct nx_action_bundle
{
1382 ovs_be16 type
; /* OFPAT_VENDOR. */
1383 ovs_be16 len
; /* Length including members. */
1384 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1385 ovs_be16 subtype
; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1387 /* Member choice algorithm to apply to hash value. */
1388 ovs_be16 algorithm
; /* One of NX_BD_ALG_*. */
1390 /* What fields to hash and how. */
1391 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
1392 ovs_be16 basis
; /* Universal hash parameter. */
1394 ovs_be32 member_type
; /* NXM_OF_IN_PORT. */
1395 ovs_be16 n_members
; /* Number of members. */
1397 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1398 ovs_be32 dst
; /* Destination. */
1400 uint8_t zero
[4]; /* Reserved. Must be zero. */
1402 OFP_ASSERT(sizeof(struct nx_action_bundle
) == 32);
1405 decode_bundle(bool load
, const struct nx_action_bundle
*nab
,
1406 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
1407 struct ofpbuf
*ofpacts
)
1409 static struct vlog_rate_limit rll
= VLOG_RATE_LIMIT_INIT(1, 5);
1410 struct ofpact_bundle
*bundle
;
1411 uint32_t member_type
;
1412 size_t members_size
, i
;
1415 bundle
= ofpact_put_BUNDLE(ofpacts
);
1417 bundle
->n_members
= ntohs(nab
->n_members
);
1418 bundle
->basis
= ntohs(nab
->basis
);
1419 bundle
->fields
= ntohs(nab
->fields
);
1420 bundle
->algorithm
= ntohs(nab
->algorithm
);
1421 member_type
= ntohl(nab
->member_type
);
1422 members_size
= ntohs(nab
->len
) - sizeof *nab
;
1424 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1425 if (!flow_hash_fields_valid(bundle
->fields
)) {
1426 VLOG_WARN_RL(&rll
, "unsupported fields %d", (int) bundle
->fields
);
1427 } else if (bundle
->n_members
> BUNDLE_MAX_MEMBERS
) {
1428 VLOG_WARN_RL(&rll
, "too many members");
1429 } else if (bundle
->algorithm
!= NX_BD_ALG_HRW
1430 && bundle
->algorithm
!= NX_BD_ALG_ACTIVE_BACKUP
) {
1431 VLOG_WARN_RL(&rll
, "unsupported algorithm %d", (int) bundle
->algorithm
);
1432 } else if (member_type
!= mf_nxm_header(MFF_IN_PORT
)) {
1433 VLOG_WARN_RL(&rll
, "unsupported member type %"PRIu32
, member_type
);
1438 if (!is_all_zeros(nab
->zero
, sizeof nab
->zero
)) {
1439 VLOG_WARN_RL(&rll
, "reserved field is nonzero");
1440 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1444 bundle
->dst
.ofs
= nxm_decode_ofs(nab
->ofs_nbits
);
1445 bundle
->dst
.n_bits
= nxm_decode_n_bits(nab
->ofs_nbits
);
1446 error
= mf_vl_mff_mf_from_nxm_header(ntohl(nab
->dst
), vl_mff_map
,
1447 &bundle
->dst
.field
, tlv_bitmap
);
1452 if (bundle
->dst
.n_bits
< 16) {
1453 VLOG_WARN_RL(&rll
, "bundle_load action requires at least 16 bit "
1455 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1458 if (nab
->ofs_nbits
|| nab
->dst
) {
1459 VLOG_WARN_RL(&rll
, "bundle action has nonzero reserved fields");
1460 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1464 if (members_size
< bundle
->n_members
* sizeof(ovs_be16
)) {
1465 VLOG_WARN_RL(&rll
, "Nicira action %s only has %"PRIuSIZE
" bytes "
1466 "allocated for members. %"PRIuSIZE
" bytes are "
1467 "required for %u members.",
1468 load
? "bundle_load" : "bundle", members_size
,
1469 bundle
->n_members
* sizeof(ovs_be16
), bundle
->n_members
);
1470 error
= OFPERR_OFPBAC_BAD_LEN
;
1472 for (i
= 0; i
< bundle
->n_members
; i
++) {
1474 = u16_to_ofp(ntohs(((ovs_be16
*)(nab
+ 1))[i
]));
1475 ofpbuf_put(ofpacts
, &ofp_port
, sizeof ofp_port
);
1476 bundle
= ofpacts
->header
;
1480 ofpact_finish_BUNDLE(ofpacts
, &bundle
);
1482 error
= bundle_check(bundle
, OFPP_MAX
, NULL
);
1488 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle
*nab
,
1489 enum ofp_version ofp_version OVS_UNUSED
,
1492 return decode_bundle(false, nab
, NULL
, NULL
, out
);
1496 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle
*nab
,
1497 enum ofp_version ofp_version OVS_UNUSED
,
1498 const struct vl_mff_map
*vl_mff_map
,
1499 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
1501 return decode_bundle(true, nab
, vl_mff_map
, tlv_bitmap
, out
);
1505 encode_BUNDLE(const struct ofpact_bundle
*bundle
,
1506 enum ofp_version ofp_version OVS_UNUSED
,
1509 int members_len
= ROUND_UP(2 * bundle
->n_members
, OFP_ACTION_ALIGN
);
1510 struct nx_action_bundle
*nab
;
1514 nab
= (bundle
->dst
.field
1515 ? put_NXAST_BUNDLE_LOAD(out
)
1516 : put_NXAST_BUNDLE(out
));
1517 nab
->len
= htons(ntohs(nab
->len
) + members_len
);
1518 nab
->algorithm
= htons(bundle
->algorithm
);
1519 nab
->fields
= htons(bundle
->fields
);
1520 nab
->basis
= htons(bundle
->basis
);
1521 nab
->member_type
= htonl(mf_nxm_header(MFF_IN_PORT
));
1522 nab
->n_members
= htons(bundle
->n_members
);
1523 if (bundle
->dst
.field
) {
1524 nab
->ofs_nbits
= nxm_encode_ofs_nbits(bundle
->dst
.ofs
,
1525 bundle
->dst
.n_bits
);
1526 nab
->dst
= htonl(nxm_header_from_mff(bundle
->dst
.field
));
1529 members
= ofpbuf_put_zeros(out
, members_len
);
1530 for (i
= 0; i
< bundle
->n_members
; i
++) {
1531 members
[i
] = htons(ofp_to_u16(bundle
->members
[i
]));
1535 static char * OVS_WARN_UNUSED_RESULT
1536 parse_BUNDLE(const char *arg
, const struct ofpact_parse_params
*pp
)
1538 return bundle_parse(arg
, pp
->port_map
, pp
->ofpacts
);
1541 static char * OVS_WARN_UNUSED_RESULT
1542 parse_bundle_load(const char *arg
, const struct ofpact_parse_params
*pp
)
1544 return bundle_parse_load(arg
, pp
->port_map
, pp
->ofpacts
);
1548 format_BUNDLE(const struct ofpact_bundle
*a
,
1549 const struct ofpact_format_params
*fp
)
1551 bundle_format(a
, fp
->port_map
, fp
->s
);
1555 check_BUNDLE(const struct ofpact_bundle
*a
,
1556 const struct ofpact_check_params
*cp
)
1558 return bundle_check(a
, cp
->max_ports
, cp
->match
);
1561 /* Set VLAN actions. */
1564 decode_set_vlan_vid(uint16_t vid
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1567 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1569 struct ofpact_vlan_vid
*vlan_vid
= ofpact_put_SET_VLAN_VID(out
);
1570 vlan_vid
->vlan_vid
= vid
;
1571 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1577 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid
,
1578 enum ofp_version ofp_version OVS_UNUSED
,
1581 return decode_set_vlan_vid(vid
, true, out
);
1585 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid
,
1586 enum ofp_version ofp_version OVS_UNUSED
,
1589 return decode_set_vlan_vid(vid
, false, out
);
1593 encode_SET_VLAN_VID(const struct ofpact_vlan_vid
*vlan_vid
,
1594 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1596 uint16_t vid
= vlan_vid
->vlan_vid
;
1598 /* Push a VLAN tag, if none is present and this form of the action calls
1599 * for such a feature. */
1600 if (ofp_version
> OFP10_VERSION
1601 && vlan_vid
->push_vlan_if_needed
1602 && !vlan_vid
->flow_has_vlan
) {
1603 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1606 if (ofp_version
== OFP10_VERSION
) {
1607 put_OFPAT10_SET_VLAN_VID(out
, vid
);
1608 } else if (ofp_version
== OFP11_VERSION
) {
1609 put_OFPAT11_SET_VLAN_VID(out
, vid
);
1611 put_set_field(out
, ofp_version
, MFF_VLAN_VID
, vid
| OFPVID12_PRESENT
);
1615 static char * OVS_WARN_UNUSED_RESULT
1616 parse_set_vlan_vid(char *arg
, bool push_vlan_if_needed
,
1617 const struct ofpact_parse_params
*pp
)
1619 struct ofpact_vlan_vid
*vlan_vid
;
1623 error
= str_to_u16(arg
, "VLAN VID", &vid
);
1628 if (vid
& ~VLAN_VID_MASK
) {
1629 return xasprintf("%s: not a valid VLAN VID", arg
);
1631 vlan_vid
= ofpact_put_SET_VLAN_VID(pp
->ofpacts
);
1632 vlan_vid
->vlan_vid
= vid
;
1633 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1637 static char * OVS_WARN_UNUSED_RESULT
1638 parse_SET_VLAN_VID(char *arg
, const struct ofpact_parse_params
*pp
)
1640 return parse_set_vlan_vid(arg
, false, pp
);
1644 format_SET_VLAN_VID(const struct ofpact_vlan_vid
*a
,
1645 const struct ofpact_format_params
*fp
)
1647 ds_put_format(fp
->s
, "%s%s:%s%"PRIu16
, colors
.param
,
1648 a
->push_vlan_if_needed
? "mod_vlan_vid" : "set_vlan_vid",
1649 colors
.end
, a
->vlan_vid
);
1653 check_SET_VLAN_VID(struct ofpact_vlan_vid
*a
, struct ofpact_check_params
*cp
)
1655 /* Remember if we saw a vlan tag in the flow to aid translating to OpenFlow
1656 * 1.1+ if need be. */
1657 ovs_be16
*tci
= &cp
->match
->flow
.vlans
[0].tci
;
1658 a
->flow_has_vlan
= (*tci
& htons(VLAN_CFI
)) != 0;
1659 if (!a
->flow_has_vlan
&& !a
->push_vlan_if_needed
) {
1660 inconsistent_match(&cp
->usable_protocols
);
1663 /* Temporarily mark that we have a vlan tag. */
1664 *tci
|= htons(VLAN_CFI
);
1669 /* Set PCP actions. */
1672 decode_set_vlan_pcp(uint8_t pcp
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1675 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1677 struct ofpact_vlan_pcp
*vlan_pcp
= ofpact_put_SET_VLAN_PCP(out
);
1678 vlan_pcp
->vlan_pcp
= pcp
;
1679 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1685 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp
,
1686 enum ofp_version ofp_version OVS_UNUSED
,
1689 return decode_set_vlan_pcp(pcp
, true, out
);
1693 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp
,
1694 enum ofp_version ofp_version OVS_UNUSED
,
1697 return decode_set_vlan_pcp(pcp
, false, out
);
1701 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*vlan_pcp
,
1702 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1704 uint8_t pcp
= vlan_pcp
->vlan_pcp
;
1706 /* Push a VLAN tag, if none is present and this form of the action calls
1707 * for such a feature. */
1708 if (ofp_version
> OFP10_VERSION
1709 && vlan_pcp
->push_vlan_if_needed
1710 && !vlan_pcp
->flow_has_vlan
) {
1711 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1714 if (ofp_version
== OFP10_VERSION
) {
1715 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
1716 } else if (ofp_version
== OFP11_VERSION
) {
1717 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
1719 put_set_field(out
, ofp_version
, MFF_VLAN_PCP
, pcp
);
1723 static char * OVS_WARN_UNUSED_RESULT
1724 parse_set_vlan_pcp(char *arg
, bool push_vlan_if_needed
,
1725 const struct ofpact_parse_params
*pp
)
1727 struct ofpact_vlan_pcp
*vlan_pcp
;
1731 error
= str_to_u8(arg
, "VLAN PCP", &pcp
);
1737 return xasprintf("%s: not a valid VLAN PCP", arg
);
1739 vlan_pcp
= ofpact_put_SET_VLAN_PCP(pp
->ofpacts
);
1740 vlan_pcp
->vlan_pcp
= pcp
;
1741 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1745 static char * OVS_WARN_UNUSED_RESULT
1746 parse_SET_VLAN_PCP(char *arg
, const struct ofpact_parse_params
*pp
)
1748 return parse_set_vlan_pcp(arg
, false, pp
);
1752 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*a
,
1753 const struct ofpact_format_params
*fp
)
1755 ds_put_format(fp
->s
, "%s%s:%s%"PRIu8
, colors
.param
,
1756 a
->push_vlan_if_needed
? "mod_vlan_pcp" : "set_vlan_pcp",
1757 colors
.end
, a
->vlan_pcp
);
1761 check_SET_VLAN_PCP(struct ofpact_vlan_pcp
*a
, struct ofpact_check_params
*cp
)
1763 /* Remember if we saw a vlan tag in the flow to aid translating to OpenFlow
1764 * 1.1+ if need be. */
1765 ovs_be16
*tci
= &cp
->match
->flow
.vlans
[0].tci
;
1766 a
->flow_has_vlan
= (*tci
& htons(VLAN_CFI
)) != 0;
1767 if (!a
->flow_has_vlan
&& !a
->push_vlan_if_needed
) {
1768 inconsistent_match(&cp
->usable_protocols
);
1771 /* Temporarily mark that we have a vlan tag. */
1772 *tci
|= htons(VLAN_CFI
);
1777 /* Strip VLAN actions. */
1780 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf
*out
)
1782 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1787 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf
*out
)
1789 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1794 encode_STRIP_VLAN(const struct ofpact_null
*null OVS_UNUSED
,
1795 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1797 if (ofp_version
== OFP10_VERSION
) {
1798 put_OFPAT10_STRIP_VLAN(out
);
1800 put_OFPAT11_POP_VLAN(out
);
1804 static char * OVS_WARN_UNUSED_RESULT
1805 parse_STRIP_VLAN(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
1807 ofpact_put_STRIP_VLAN(pp
->ofpacts
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1811 static char * OVS_WARN_UNUSED_RESULT
1812 parse_pop_vlan(const struct ofpact_parse_params
*pp
)
1814 ofpact_put_STRIP_VLAN(pp
->ofpacts
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1819 format_STRIP_VLAN(const struct ofpact_null
*a
,
1820 const struct ofpact_format_params
*fp
)
1822 ds_put_format(fp
->s
, (a
->ofpact
.raw
== OFPAT_RAW11_POP_VLAN
1824 : "%sstrip_vlan%s"),
1825 colors
.value
, colors
.end
);
1829 check_STRIP_VLAN(const struct ofpact_null
*a OVS_UNUSED
,
1830 struct ofpact_check_params
*cp
)
1832 if (!(cp
->match
->flow
.vlans
[0].tci
& htons(VLAN_CFI
))) {
1833 inconsistent_match(&cp
->usable_protocols
);
1835 flow_pop_vlan(&cp
->match
->flow
, NULL
);
1839 /* Push VLAN action. */
1842 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type
,
1843 enum ofp_version ofp_version OVS_UNUSED
,
1846 struct ofpact_push_vlan
*push_vlan
;
1847 if (!eth_type_vlan(eth_type
)) {
1848 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1850 push_vlan
= ofpact_put_PUSH_VLAN(out
);
1851 push_vlan
->ethertype
= eth_type
;
1856 encode_PUSH_VLAN(const struct ofpact_push_vlan
*push_vlan
,
1857 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1859 if (ofp_version
== OFP10_VERSION
) {
1860 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1861 * follow this action. */
1863 put_OFPAT11_PUSH_VLAN(out
, push_vlan
->ethertype
);
1867 static char * OVS_WARN_UNUSED_RESULT
1868 parse_PUSH_VLAN(char *arg
, const struct ofpact_parse_params
*pp
)
1870 struct ofpact_push_vlan
*push_vlan
;
1874 *pp
->usable_protocols
&= OFPUTIL_P_OF11_UP
;
1875 error
= str_to_u16(arg
, "ethertype", ðertype
);
1880 if (!eth_type_vlan(htons(ethertype
))) {
1881 return xasprintf("%s: not a valid VLAN ethertype", arg
);
1883 push_vlan
= ofpact_put_PUSH_VLAN(pp
->ofpacts
);
1884 push_vlan
->ethertype
= htons(ethertype
);
1889 format_PUSH_VLAN(const struct ofpact_push_vlan
*push_vlan
,
1890 const struct ofpact_format_params
*fp
)
1892 ds_put_format(fp
->s
, "%spush_vlan:%s%#"PRIx16
,
1893 colors
.param
, colors
.end
, ntohs(push_vlan
->ethertype
));
1897 check_PUSH_VLAN(const struct ofpact_push_vlan
*a OVS_UNUSED
,
1898 struct ofpact_check_params
*cp
)
1900 struct flow
*flow
= &cp
->match
->flow
;
1901 if (flow
->vlans
[FLOW_MAX_VLAN_HEADERS
- 1].tci
& htons(VLAN_CFI
)) {
1902 /* Support maximum (FLOW_MAX_VLAN_HEADERS) VLAN headers. */
1903 return OFPERR_OFPBAC_BAD_TAG
;
1905 /* Temporary mark that we have a vlan tag. */
1906 flow_push_vlan_uninit(flow
, NULL
);
1907 flow
->vlans
[0].tci
|= htons(VLAN_CFI
);
1911 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1912 struct ofp_action_dl_addr
{
1913 ovs_be16 type
; /* Type. */
1914 ovs_be16 len
; /* Length is 16. */
1915 struct eth_addr dl_addr
; /* Ethernet address. */
1918 OFP_ASSERT(sizeof(struct ofp_action_dl_addr
) == 16);
1921 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr
*a
,
1922 enum ofp_version ofp_version OVS_UNUSED
,
1925 ofpact_put_SET_ETH_SRC(out
)->mac
= a
->dl_addr
;
1930 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr
*a
,
1931 enum ofp_version ofp_version OVS_UNUSED
,
1934 ofpact_put_SET_ETH_DST(out
)->mac
= a
->dl_addr
;
1939 encode_SET_ETH_addr(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1940 enum ofp_raw_action_type raw
, enum mf_field_id field
,
1943 if (ofp_version
< OFP12_VERSION
) {
1944 struct ofp_action_dl_addr
*oada
;
1946 oada
= ofpact_put_raw(out
, ofp_version
, raw
, 0);
1947 oada
->dl_addr
= mac
->mac
;
1949 put_set_field(out
, ofp_version
, field
, eth_addr_to_uint64(mac
->mac
));
1954 encode_SET_ETH_SRC(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1957 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_SRC
, MFF_ETH_SRC
,
1963 encode_SET_ETH_DST(const struct ofpact_mac
*mac
,
1964 enum ofp_version ofp_version
,
1967 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_DST
, MFF_ETH_DST
,
1972 static char * OVS_WARN_UNUSED_RESULT
1973 parse_SET_ETH_SRC(char *arg
, const struct ofpact_parse_params
*pp
)
1975 return str_to_mac(arg
, &ofpact_put_SET_ETH_SRC(pp
->ofpacts
)->mac
);
1978 static char * OVS_WARN_UNUSED_RESULT
1979 parse_SET_ETH_DST(char *arg
, const struct ofpact_parse_params
*pp
)
1981 return str_to_mac(arg
, &ofpact_put_SET_ETH_DST(pp
->ofpacts
)->mac
);
1985 format_SET_ETH_SRC(const struct ofpact_mac
*a
,
1986 const struct ofpact_format_params
*fp
)
1988 ds_put_format(fp
->s
, "%smod_dl_src:%s"ETH_ADDR_FMT
,
1989 colors
.param
, colors
.end
, ETH_ADDR_ARGS(a
->mac
));
1993 format_SET_ETH_DST(const struct ofpact_mac
*a
,
1994 const struct ofpact_format_params
*fp
)
1996 ds_put_format(fp
->s
, "%smod_dl_dst:%s"ETH_ADDR_FMT
,
1997 colors
.param
, colors
.end
, ETH_ADDR_ARGS(a
->mac
));
2001 check_SET_ETH_SRC(const struct ofpact_mac
*a OVS_UNUSED
,
2002 const struct ofpact_check_params
*cp OVS_UNUSED
)
2008 check_SET_ETH_DST(const struct ofpact_mac
*a OVS_UNUSED
,
2009 const struct ofpact_check_params
*cp OVS_UNUSED
)
2014 /* Set IPv4 address actions. */
2017 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4
,
2018 enum ofp_version ofp_version OVS_UNUSED
,
2021 ofpact_put_SET_IPV4_SRC(out
)->ipv4
= ipv4
;
2026 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4
,
2027 enum ofp_version ofp_version OVS_UNUSED
,
2030 ofpact_put_SET_IPV4_DST(out
)->ipv4
= ipv4
;
2035 encode_SET_IPV4_addr(const struct ofpact_ipv4
*ipv4
,
2036 enum ofp_version ofp_version
,
2037 enum ofp_raw_action_type raw
, enum mf_field_id field
,
2040 ovs_be32 addr
= ipv4
->ipv4
;
2041 if (ofp_version
< OFP12_VERSION
) {
2042 ofpact_put_raw(out
, ofp_version
, raw
, ntohl(addr
));
2044 put_set_field(out
, ofp_version
, field
, ntohl(addr
));
2049 encode_SET_IPV4_SRC(const struct ofpact_ipv4
*ipv4
,
2050 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2052 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_SRC
, MFF_IPV4_SRC
,
2057 encode_SET_IPV4_DST(const struct ofpact_ipv4
*ipv4
,
2058 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2060 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_DST
, MFF_IPV4_DST
,
2064 static char * OVS_WARN_UNUSED_RESULT
2065 parse_SET_IPV4_SRC(char *arg
, const struct ofpact_parse_params
*pp
)
2067 return str_to_ip(arg
, &ofpact_put_SET_IPV4_SRC(pp
->ofpacts
)->ipv4
);
2070 static char * OVS_WARN_UNUSED_RESULT
2071 parse_SET_IPV4_DST(char *arg
, const struct ofpact_parse_params
*pp
)
2073 return str_to_ip(arg
, &ofpact_put_SET_IPV4_DST(pp
->ofpacts
)->ipv4
);
2077 format_SET_IPV4_SRC(const struct ofpact_ipv4
*a
,
2078 const struct ofpact_format_params
*fp
)
2080 ds_put_format(fp
->s
, "%smod_nw_src:%s"IP_FMT
,
2081 colors
.param
, colors
.end
, IP_ARGS(a
->ipv4
));
2085 format_SET_IPV4_DST(const struct ofpact_ipv4
*a
,
2086 const struct ofpact_format_params
*fp
)
2088 ds_put_format(fp
->s
, "%smod_nw_dst:%s"IP_FMT
,
2089 colors
.param
, colors
.end
, IP_ARGS(a
->ipv4
));
2093 check_set_ipv4(struct ofpact_check_params
*cp
)
2095 ovs_be16 dl_type
= get_dl_type(&cp
->match
->flow
);
2096 if (dl_type
!= htons(ETH_TYPE_IP
)) {
2097 inconsistent_match(&cp
->usable_protocols
);
2103 check_SET_IPV4_SRC(const struct ofpact_ipv4
*a OVS_UNUSED
,
2104 struct ofpact_check_params
*cp
)
2106 return check_set_ipv4(cp
);
2110 check_SET_IPV4_DST(const struct ofpact_ipv4
*a OVS_UNUSED
,
2111 struct ofpact_check_params
*cp
)
2113 return check_set_ipv4(cp
);
2116 /* Set IPv4/v6 TOS actions. */
2119 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp
,
2120 enum ofp_version ofp_version OVS_UNUSED
,
2123 if (dscp
& ~IP_DSCP_MASK
) {
2124 return OFPERR_OFPBAC_BAD_ARGUMENT
;
2126 ofpact_put_SET_IP_DSCP(out
)->dscp
= dscp
;
2132 encode_SET_IP_DSCP(const struct ofpact_dscp
*dscp
,
2133 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2135 if (ofp_version
< OFP12_VERSION
) {
2136 put_OFPAT_SET_NW_TOS(out
, ofp_version
, dscp
->dscp
);
2138 put_set_field(out
, ofp_version
, MFF_IP_DSCP_SHIFTED
, dscp
->dscp
>> 2);
2142 static char * OVS_WARN_UNUSED_RESULT
2143 parse_SET_IP_DSCP(char *arg
, const struct ofpact_parse_params
*pp
)
2149 error
= str_to_u8(arg
, "TOS", &tos
);
2154 if (tos
& ~IP_DSCP_MASK
) {
2155 return xasprintf("%s: not a valid TOS", arg
);
2157 ofpact_put_SET_IP_DSCP(pp
->ofpacts
)->dscp
= tos
;
2162 format_SET_IP_DSCP(const struct ofpact_dscp
*a
,
2163 const struct ofpact_format_params
*fp
)
2165 ds_put_format(fp
->s
, "%smod_nw_tos:%s%d",
2166 colors
.param
, colors
.end
, a
->dscp
);
2170 check_set_ip(struct ofpact_check_params
*cp
)
2172 if (!is_ip_any(&cp
->match
->flow
)) {
2173 inconsistent_match(&cp
->usable_protocols
);
2179 check_SET_IP_DSCP(const struct ofpact_dscp
*a OVS_UNUSED
,
2180 struct ofpact_check_params
*cp
)
2182 return check_set_ip(cp
);
2185 /* Set IPv4/v6 ECN actions. */
2188 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn
,
2189 enum ofp_version ofp_version OVS_UNUSED
,
2192 if (ecn
& ~IP_ECN_MASK
) {
2193 return OFPERR_OFPBAC_BAD_ARGUMENT
;
2195 ofpact_put_SET_IP_ECN(out
)->ecn
= ecn
;
2201 encode_SET_IP_ECN(const struct ofpact_ecn
*ip_ecn
,
2202 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2204 uint8_t ecn
= ip_ecn
->ecn
;
2205 if (ofp_version
== OFP10_VERSION
) {
2206 struct mf_subfield dst
= { .field
= mf_from_id(MFF_IP_ECN
),
2207 .ofs
= 0, .n_bits
= 2 };
2208 put_reg_load(out
, &dst
, ecn
);
2209 } else if (ofp_version
== OFP11_VERSION
) {
2210 put_OFPAT11_SET_NW_ECN(out
, ecn
);
2212 put_set_field(out
, ofp_version
, MFF_IP_ECN
, ecn
);
2216 static char * OVS_WARN_UNUSED_RESULT
2217 parse_SET_IP_ECN(char *arg
, const struct ofpact_parse_params
*pp
)
2222 error
= str_to_u8(arg
, "ECN", &ecn
);
2227 if (ecn
& ~IP_ECN_MASK
) {
2228 return xasprintf("%s: not a valid ECN", arg
);
2230 ofpact_put_SET_IP_ECN(pp
->ofpacts
)->ecn
= ecn
;
2235 format_SET_IP_ECN(const struct ofpact_ecn
*a
,
2236 const struct ofpact_format_params
*fp
)
2238 ds_put_format(fp
->s
, "%smod_nw_ecn:%s%d",
2239 colors
.param
, colors
.end
, a
->ecn
);
2243 check_SET_IP_ECN(const struct ofpact_ecn
*a OVS_UNUSED
,
2244 struct ofpact_check_params
*cp
)
2246 return check_set_ip(cp
);
2249 /* Set IPv4/v6 TTL actions. */
2252 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl
,
2253 enum ofp_version ofp_version OVS_UNUSED
,
2256 ofpact_put_SET_IP_TTL(out
)->ttl
= ttl
;
2261 encode_SET_IP_TTL(const struct ofpact_ip_ttl
*ttl
,
2262 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2264 if (ofp_version
>= OFP11_VERSION
) {
2265 put_OFPAT11_SET_NW_TTL(out
, ttl
->ttl
);
2267 struct mf_subfield dst
= { .field
= mf_from_id(MFF_IP_TTL
),
2268 .ofs
= 0, .n_bits
= 8 };
2269 put_reg_load(out
, &dst
, ttl
->ttl
);
2273 static char * OVS_WARN_UNUSED_RESULT
2274 parse_SET_IP_TTL(char *arg
, const struct ofpact_parse_params
*pp
)
2280 error
= str_to_u8(arg
, "TTL", &ttl
);
2285 ofpact_put_SET_IP_TTL(pp
->ofpacts
)->ttl
= ttl
;
2290 format_SET_IP_TTL(const struct ofpact_ip_ttl
*a
,
2291 const struct ofpact_format_params
*fp
)
2293 ds_put_format(fp
->s
, "%smod_nw_ttl:%s%d",
2294 colors
.param
, colors
.end
, a
->ttl
);
2298 check_SET_IP_TTL(const struct ofpact_ip_ttl
*a OVS_UNUSED
,
2299 struct ofpact_check_params
*cp
)
2301 return check_set_ip(cp
);
2304 /* Set TCP/UDP/SCTP port actions. */
2307 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port
,
2308 enum ofp_version ofp_version OVS_UNUSED
,
2311 ofpact_put_SET_L4_SRC_PORT(out
)->port
= ntohs(port
);
2316 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port
,
2317 enum ofp_version ofp_version OVS_UNUSED
,
2320 ofpact_put_SET_L4_DST_PORT(out
)->port
= ntohs(port
);
2325 encode_SET_L4_port(const struct ofpact_l4_port
*l4_port
,
2326 enum ofp_version ofp_version
, enum ofp_raw_action_type raw
,
2327 enum mf_field_id field
, struct ofpbuf
*out
)
2329 uint16_t port
= l4_port
->port
;
2331 if (ofp_version
>= OFP12_VERSION
&& field
!= MFF_N_IDS
) {
2332 put_set_field(out
, ofp_version
, field
, port
);
2334 ofpact_put_raw(out
, ofp_version
, raw
, port
);
2339 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port
*l4_port
,
2340 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2342 uint8_t proto
= l4_port
->flow_ip_proto
;
2343 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_SRC
2344 : proto
== IPPROTO_UDP
? MFF_UDP_SRC
2345 : proto
== IPPROTO_SCTP
? MFF_SCTP_SRC
2348 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_SRC
, field
, out
);
2352 encode_SET_L4_DST_PORT(const struct ofpact_l4_port
*l4_port
,
2353 enum ofp_version ofp_version
,
2356 uint8_t proto
= l4_port
->flow_ip_proto
;
2357 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_DST
2358 : proto
== IPPROTO_UDP
? MFF_UDP_DST
2359 : proto
== IPPROTO_SCTP
? MFF_SCTP_DST
2362 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_DST
, field
, out
);
2365 static char * OVS_WARN_UNUSED_RESULT
2366 parse_SET_L4_SRC_PORT(char *arg
, const struct ofpact_parse_params
*pp
)
2368 return str_to_u16(arg
, "source port",
2369 &ofpact_put_SET_L4_SRC_PORT(pp
->ofpacts
)->port
);
2372 static char * OVS_WARN_UNUSED_RESULT
2373 parse_SET_L4_DST_PORT(char *arg
, const struct ofpact_parse_params
*pp
)
2375 return str_to_u16(arg
, "destination port",
2376 &ofpact_put_SET_L4_DST_PORT(pp
->ofpacts
)->port
);
2380 format_SET_L4_SRC_PORT(const struct ofpact_l4_port
*a
,
2381 const struct ofpact_format_params
*fp
)
2383 ds_put_format(fp
->s
, "%smod_tp_src:%s%d",
2384 colors
.param
, colors
.end
, a
->port
);
2388 format_SET_L4_DST_PORT(const struct ofpact_l4_port
*a
,
2389 const struct ofpact_format_params
*fp
)
2391 ds_put_format(fp
->s
, "%smod_tp_dst:%s%d",
2392 colors
.param
, colors
.end
, a
->port
);
2396 check_set_l4_port(struct ofpact_l4_port
*a
, struct ofpact_check_params
*cp
)
2398 const struct flow
*flow
= &cp
->match
->flow
;
2399 if (!is_ip_any(flow
)
2400 || flow
->nw_frag
& FLOW_NW_FRAG_LATER
2401 || (flow
->nw_proto
!= IPPROTO_TCP
&&
2402 flow
->nw_proto
!= IPPROTO_UDP
&&
2403 flow
->nw_proto
!= IPPROTO_SCTP
)) {
2404 inconsistent_match(&cp
->usable_protocols
);
2407 /* Note the transport protocol in use, to allow this action to be converted
2408 * to an OF1.2 set_field action later if necessary. */
2409 a
->flow_ip_proto
= flow
->nw_proto
;
2415 check_SET_L4_SRC_PORT(struct ofpact_l4_port
*a
, struct ofpact_check_params
*cp
)
2417 return check_set_l4_port(a
, cp
);
2421 check_SET_L4_DST_PORT(struct ofpact_l4_port
*a
, struct ofpact_check_params
*cp
)
2423 return check_set_l4_port(a
, cp
);
2426 /* Action structure for OFPAT_COPY_FIELD. */
2427 struct ofp15_action_copy_field
{
2428 ovs_be16 type
; /* OFPAT_COPY_FIELD. */
2429 ovs_be16 len
; /* Length is padded to 64 bits. */
2430 ovs_be16 n_bits
; /* Number of bits to copy. */
2431 ovs_be16 src_offset
; /* Starting bit offset in source. */
2432 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
2435 * - OXM header for source field.
2436 * - OXM header for destination field.
2437 * - Padding with 0-bytes to a multiple of 8 bytes.
2438 * The "pad2" member is the beginning of the above. */
2441 OFP_ASSERT(sizeof(struct ofp15_action_copy_field
) == 16);
2443 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
2444 struct onf_action_copy_field
{
2445 ovs_be16 type
; /* OFPAT_EXPERIMENTER. */
2446 ovs_be16 len
; /* Length is padded to 64 bits. */
2447 ovs_be32 experimenter
; /* ONF_VENDOR_ID. */
2448 ovs_be16 exp_type
; /* 3200. */
2449 uint8_t pad
[2]; /* Not used. */
2450 ovs_be16 n_bits
; /* Number of bits to copy. */
2451 ovs_be16 src_offset
; /* Starting bit offset in source. */
2452 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
2453 uint8_t pad2
[2]; /* Not used. */
2455 * - OXM header for source field.
2456 * - OXM header for destination field.
2457 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2458 * The "pad3" member is the beginning of the above. */
2459 uint8_t pad3
[4]; /* Not used. */
2461 OFP_ASSERT(sizeof(struct onf_action_copy_field
) == 24);
2463 /* Action structure for NXAST_REG_MOVE.
2465 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2466 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2467 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
2468 * the next most significant bit, and so on.
2470 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
2471 * sense to use nxm_hasmask=1 because the action does not do any kind of
2472 * matching; it uses the actual value of a field.)
2474 * The following nxm_header values are potentially acceptable as 'src':
2489 * - NXM_OF_ICMP_TYPE
2490 * - NXM_OF_ICMP_CODE
2497 * - NXM_NX_ICMPV6_TYPE
2498 * - NXM_NX_ICMPV6_CODE
2501 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2503 * - NXM_NX_TUN_IPV4_SRC
2504 * - NXM_NX_TUN_IPV4_DST
2506 * The following nxm_header values are potentially acceptable as 'dst':
2517 * - NXM_OF_ICMP_TYPE
2518 * - NXM_OF_ICMP_CODE
2519 * - NXM_NX_ICMPV6_TYPE
2520 * - NXM_NX_ICMPV6_CODE
2526 * Modifying any of the above fields changes the corresponding packet
2531 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2535 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
2536 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
2537 * header (if any), ignoring the other bits. Setting a value with CFI=1
2538 * adds or modifies the 802.1Q header appropriately, setting the TCI field
2539 * to the field's new value (with the CFI bit masked out).
2541 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
2542 * any of these values modifies the corresponding tunnel header field used
2543 * for the packet's next tunnel encapsulation, if allowed by the
2544 * configuration of the output tunnel port.
2546 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2547 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
2548 * used only if the flow's nx_match includes an nxm_entry that specifies
2549 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2551 * The switch will reject actions for which src_ofs+n_bits is greater than the
2552 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2553 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2555 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2556 * behaves as if 'src' were copied out to a temporary buffer, then the
2557 * temporary buffer copied to 'dst'.
2559 struct nx_action_reg_move
{
2560 ovs_be16 type
; /* OFPAT_VENDOR. */
2561 ovs_be16 len
; /* Length is 24. */
2562 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2563 ovs_be16 subtype
; /* NXAST_REG_MOVE. */
2564 ovs_be16 n_bits
; /* Number of bits. */
2565 ovs_be16 src_ofs
; /* Starting bit offset in source. */
2566 ovs_be16 dst_ofs
; /* Starting bit offset in destination. */
2568 * - OXM/NXM header for source field (4 or 8 bytes).
2569 * - OXM/NXM header for destination field (4 or 8 bytes).
2570 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2572 OFP_ASSERT(sizeof(struct nx_action_reg_move
) == 16);
2575 decode_copy_field__(ovs_be16 src_offset
, ovs_be16 dst_offset
, ovs_be16 n_bits
,
2576 const void *action
, ovs_be16 action_len
, size_t oxm_offset
,
2577 const struct vl_mff_map
*vl_mff_map
,
2578 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2580 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2583 move
->ofpact
.raw
= ONFACT_RAW13_COPY_FIELD
;
2584 move
->src
.ofs
= ntohs(src_offset
);
2585 move
->src
.n_bits
= ntohs(n_bits
);
2586 move
->dst
.ofs
= ntohs(dst_offset
);
2587 move
->dst
.n_bits
= ntohs(n_bits
);
2589 struct ofpbuf b
= ofpbuf_const_initializer(action
, ntohs(action_len
));
2590 ofpbuf_pull(&b
, oxm_offset
);
2592 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->src
.field
, NULL
,
2597 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->dst
.field
, NULL
,
2603 if (!is_all_zeros(b
.data
, b
.size
)) {
2604 return OFPERR_NXBRC_MUST_BE_ZERO
;
2607 return nxm_reg_move_check(move
, NULL
);
2611 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field
*oacf
,
2612 enum ofp_version ofp_version OVS_UNUSED
,
2613 const struct vl_mff_map
*vl_mff_map
,
2614 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2616 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
2617 oacf
->n_bits
, oacf
, oacf
->len
,
2618 OBJECT_OFFSETOF(oacf
, pad2
), vl_mff_map
,
2619 tlv_bitmap
, ofpacts
);
2623 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field
*oacf
,
2624 enum ofp_version ofp_version OVS_UNUSED
,
2625 const struct vl_mff_map
*vl_mff_map
,
2626 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2628 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
2629 oacf
->n_bits
, oacf
, oacf
->len
,
2630 OBJECT_OFFSETOF(oacf
, pad3
), vl_mff_map
,
2631 tlv_bitmap
, ofpacts
);
2635 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move
*narm
,
2636 enum ofp_version ofp_version OVS_UNUSED
,
2637 const struct vl_mff_map
*vl_mff_map
,
2638 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2640 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2643 move
->ofpact
.raw
= NXAST_RAW_REG_MOVE
;
2644 move
->src
.ofs
= ntohs(narm
->src_ofs
);
2645 move
->src
.n_bits
= ntohs(narm
->n_bits
);
2646 move
->dst
.ofs
= ntohs(narm
->dst_ofs
);
2647 move
->dst
.n_bits
= ntohs(narm
->n_bits
);
2649 struct ofpbuf b
= ofpbuf_const_initializer(narm
, ntohs(narm
->len
));
2650 ofpbuf_pull(&b
, sizeof *narm
);
2652 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->src
.field
, NULL
,
2658 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &move
->dst
.field
, NULL
,
2664 if (!is_all_zeros(b
.data
, b
.size
)) {
2665 return OFPERR_NXBRC_MUST_BE_ZERO
;
2668 return nxm_reg_move_check(move
, NULL
);
2672 encode_REG_MOVE(const struct ofpact_reg_move
*move
,
2673 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2675 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2676 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2677 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2678 * probe for support. Until we have that ability, we currently prefer
2679 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2681 size_t start_ofs
= out
->size
;
2682 if (ofp_version
>= OFP15_VERSION
) {
2683 struct ofp15_action_copy_field
*copy
= put_OFPAT15_COPY_FIELD(out
);
2684 copy
->n_bits
= htons(move
->dst
.n_bits
);
2685 copy
->src_offset
= htons(move
->src
.ofs
);
2686 copy
->dst_offset
= htons(move
->dst
.ofs
);
2687 out
->size
= out
->size
- sizeof copy
->pad2
;
2688 nx_put_mff_header(out
, move
->src
.field
, ofp_version
, false);
2689 nx_put_mff_header(out
, move
->dst
.field
, ofp_version
, false);
2690 } else if (ofp_version
== OFP13_VERSION
2691 && move
->ofpact
.raw
== ONFACT_RAW13_COPY_FIELD
) {
2692 struct onf_action_copy_field
*copy
= put_ONFACT13_COPY_FIELD(out
);
2693 copy
->n_bits
= htons(move
->dst
.n_bits
);
2694 copy
->src_offset
= htons(move
->src
.ofs
);
2695 copy
->dst_offset
= htons(move
->dst
.ofs
);
2696 out
->size
= out
->size
- sizeof copy
->pad3
;
2697 nx_put_mff_header(out
, move
->src
.field
, ofp_version
, false);
2698 nx_put_mff_header(out
, move
->dst
.field
, ofp_version
, false);
2700 struct nx_action_reg_move
*narm
= put_NXAST_REG_MOVE(out
);
2701 narm
->n_bits
= htons(move
->dst
.n_bits
);
2702 narm
->src_ofs
= htons(move
->src
.ofs
);
2703 narm
->dst_ofs
= htons(move
->dst
.ofs
);
2704 nx_put_mff_header(out
, move
->src
.field
, 0, false);
2705 nx_put_mff_header(out
, move
->dst
.field
, 0, false);
2707 pad_ofpat(out
, start_ofs
);
2710 static char * OVS_WARN_UNUSED_RESULT
2711 parse_REG_MOVE(const char *arg
, const struct ofpact_parse_params
*pp
)
2713 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(pp
->ofpacts
);
2714 return nxm_parse_reg_move(move
, arg
);
2718 format_REG_MOVE(const struct ofpact_reg_move
*a
,
2719 const struct ofpact_format_params
*fp
)
2721 nxm_format_reg_move(a
, fp
->s
);
2725 check_REG_MOVE(const struct ofpact_reg_move
*a
,
2726 const struct ofpact_check_params
*cp
)
2728 return nxm_reg_move_check(a
, cp
->match
);
2731 /* Action structure for OFPAT12_SET_FIELD. */
2732 struct ofp12_action_set_field
{
2733 ovs_be16 type
; /* OFPAT12_SET_FIELD. */
2734 ovs_be16 len
; /* Length is padded to 64 bits. */
2737 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2738 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2740 * The "pad" member is the beginning of the above. */
2743 OFP_ASSERT(sizeof(struct ofp12_action_set_field
) == 8);
2745 /* Action structure for NXAST_REG_LOAD.
2747 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2748 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2749 * starts at 0 for the least-significant bit, 1 for the next most significant
2752 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2753 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2756 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2757 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2758 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2759 * also stored as one less than its true value:
2762 * +------------------------------+------------------+
2763 * | ofs | n_bits - 1 |
2764 * +------------------------------+------------------+
2766 * The switch will reject actions for which ofs+n_bits is greater than the
2767 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2768 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2769 * OFPBAC_BAD_ARGUMENT.
2771 struct nx_action_reg_load
{
2772 ovs_be16 type
; /* OFPAT_VENDOR. */
2773 ovs_be16 len
; /* Length is 24. */
2774 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2775 ovs_be16 subtype
; /* NXAST_REG_LOAD. */
2776 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
2777 ovs_be32 dst
; /* Destination register. */
2778 ovs_be64 value
; /* Immediate value. */
2780 OFP_ASSERT(sizeof(struct nx_action_reg_load
) == 24);
2782 /* The NXAST_REG_LOAD2 action structure is "struct ext_action_header",
2785 * - An NXM/OXM header, value, and optionally a mask.
2786 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2788 * The "pad" member is the beginning of the above. */
2791 decode_ofpat_set_field(const struct ofp12_action_set_field
*oasf
,
2792 bool may_mask
, const struct vl_mff_map
*vl_mff_map
,
2793 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2795 struct ofpbuf b
= ofpbuf_const_initializer(oasf
, ntohs(oasf
->len
));
2796 ofpbuf_pull(&b
, OBJECT_OFFSETOF(oasf
, pad
));
2798 union mf_value value
, mask
;
2799 const struct mf_field
*field
;
2801 error
= mf_vl_mff_nx_pull_entry(&b
, vl_mff_map
, &field
, &value
,
2802 may_mask
? &mask
: NULL
, tlv_bitmap
);
2804 return (error
== OFPERR_OFPBMC_BAD_MASK
2805 ? OFPERR_OFPBAC_BAD_SET_MASK
2810 memset(&mask
, 0xff, field
->n_bytes
);
2813 if (!is_all_zeros(b
.data
, b
.size
)) {
2814 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2817 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2819 if (field
->id
== MFF_IN_PORT_OXM
) {
2820 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2823 /* oxm_length is now validated to be compatible with mf_value. */
2824 if (!field
->writable
) {
2825 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2827 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2830 /* The value must be valid for match. OpenFlow 1.5 also says,
2831 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2832 * a 1-bit in oxm_value and in oxm_mask." */
2833 if (!mf_is_value_valid(field
, &value
)
2834 || (field
->id
== MFF_VLAN_VID
2835 && (!(mask
.be16
& htons(OFPVID12_PRESENT
))
2836 || !(value
.be16
& htons(OFPVID12_PRESENT
))))) {
2837 struct ds ds
= DS_EMPTY_INITIALIZER
;
2838 mf_format(field
, &value
, NULL
, NULL
, &ds
);
2839 VLOG_WARN_RL(&rl
, "Invalid value for set field %s: %s",
2840 field
->name
, ds_cstr(&ds
));
2843 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2846 ofpact_put_set_field(ofpacts
, field
, &value
, &mask
);
2851 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2852 enum ofp_version ofp_version OVS_UNUSED
,
2853 const struct vl_mff_map
*vl_mff_map
,
2854 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2856 return decode_ofpat_set_field(oasf
, false, vl_mff_map
, tlv_bitmap
,
2861 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2862 enum ofp_version ofp_version OVS_UNUSED
,
2863 const struct vl_mff_map
*vl_mff_map
,
2864 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
2866 return decode_ofpat_set_field(oasf
, true, vl_mff_map
, tlv_bitmap
, ofpacts
);
2870 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load
*narl
,
2871 enum ofp_version ofp_version OVS_UNUSED
,
2872 const struct vl_mff_map
*vl_mff_map
,
2873 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
2875 struct mf_subfield dst
;
2878 dst
.ofs
= nxm_decode_ofs(narl
->ofs_nbits
);
2879 dst
.n_bits
= nxm_decode_n_bits(narl
->ofs_nbits
);
2880 error
= mf_vl_mff_mf_from_nxm_header(ntohl(narl
->dst
), vl_mff_map
,
2881 &dst
.field
, tlv_bitmap
);
2886 error
= mf_check_dst(&dst
, NULL
);
2891 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2893 if (dst
.n_bits
< 64 && ntohll(narl
->value
) >> dst
.n_bits
) {
2894 return OFPERR_OFPBAC_BAD_ARGUMENT
;
2897 struct ofpact_set_field
*sf
= ofpact_put_reg_load(out
, dst
.field
, NULL
,
2899 bitwise_put(ntohll(narl
->value
),
2900 sf
->value
, dst
.field
->n_bytes
, dst
.ofs
,
2902 bitwise_put(UINT64_MAX
,
2903 ofpact_set_field_mask(sf
), dst
.field
->n_bytes
, dst
.ofs
,
2909 decode_NXAST_RAW_REG_LOAD2(const struct ext_action_header
*eah
,
2910 enum ofp_version ofp_version OVS_UNUSED
,
2911 const struct vl_mff_map
*vl_mff_map
,
2912 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
2914 struct ofpbuf b
= ofpbuf_const_initializer(eah
, ntohs(eah
->len
));
2915 ofpbuf_pull(&b
, OBJECT_OFFSETOF(eah
, pad
));
2917 union mf_value value
, mask
;
2918 const struct mf_field
*field
;
2920 error
= mf_vl_mff_nx_pull_entry(&b
, vl_mff_map
, &field
, &value
, &mask
,
2926 if (!is_all_zeros(b
.data
, b
.size
)) {
2927 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2930 if (!field
->writable
) {
2931 VLOG_WARN_RL(&rl
, "destination field %s is not writable", field
->name
);
2932 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2935 /* Put value and mask. */
2936 ofpact_put_reg_load2(out
, field
, &value
, &mask
);
2941 put_set_field(struct ofpbuf
*openflow
, enum ofp_version ofp_version
,
2942 enum mf_field_id field
, uint64_t value_
)
2944 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
2945 int n_bytes
= mf_from_id(field
)->n_bytes
;
2946 size_t start_ofs
= openflow
->size
;
2947 union mf_value value
;
2949 value
.be64
= htonll(value_
<< (8 * (8 - n_bytes
)));
2951 oasf
= put_OFPAT12_SET_FIELD(openflow
);
2952 openflow
->size
= openflow
->size
- sizeof oasf
->pad
;
2953 nx_put_entry(openflow
, mf_from_id(field
), ofp_version
, &value
, NULL
);
2954 pad_ofpat(openflow
, start_ofs
);
2958 put_reg_load(struct ofpbuf
*openflow
,
2959 const struct mf_subfield
*dst
, uint64_t value
)
2961 ovs_assert(dst
->n_bits
<= 64);
2963 struct nx_action_reg_load
*narl
= put_NXAST_REG_LOAD(openflow
);
2964 narl
->ofs_nbits
= nxm_encode_ofs_nbits(dst
->ofs
, dst
->n_bits
);
2965 narl
->dst
= htonl(nxm_header_from_mff(dst
->field
));
2966 narl
->value
= htonll(value
);
2970 next_load_segment(const struct ofpact_set_field
*sf
,
2971 struct mf_subfield
*dst
, uint64_t *value
)
2973 int n_bits
= sf
->field
->n_bits
;
2974 int n_bytes
= sf
->field
->n_bytes
;
2975 int start
= dst
->ofs
+ dst
->n_bits
;
2977 if (start
< n_bits
) {
2978 dst
->field
= sf
->field
;
2979 dst
->ofs
= bitwise_scan(ofpact_set_field_mask(sf
), n_bytes
, 1, start
,
2981 if (dst
->ofs
< n_bits
) {
2982 dst
->n_bits
= bitwise_scan(ofpact_set_field_mask(sf
), n_bytes
, 0,
2984 MIN(dst
->ofs
+ 64, n_bits
)) - dst
->ofs
;
2985 *value
= bitwise_get(sf
->value
, n_bytes
, dst
->ofs
, dst
->n_bits
);
2992 /* Convert 'sf' to a series of REG_LOADs. */
2994 set_field_to_nxast(const struct ofpact_set_field
*sf
, struct ofpbuf
*openflow
)
2996 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2997 * experimenter OXM or is variable length (or if it came in as
2998 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2999 * NXAST_REG_LOAD, which is backward compatible. */
3000 if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
3001 || !mf_nxm_header(sf
->field
->id
) || sf
->field
->variable_len
) {
3002 struct ext_action_header
*eah OVS_UNUSED
;
3003 size_t start_ofs
= openflow
->size
;
3005 eah
= put_NXAST_REG_LOAD2(openflow
);
3006 openflow
->size
= openflow
->size
- sizeof eah
->pad
;
3007 nx_put_entry(openflow
, sf
->field
, 0, sf
->value
,
3008 ofpact_set_field_mask(sf
));
3009 pad_ofpat(openflow
, start_ofs
);
3011 struct mf_subfield dst
;
3014 dst
.ofs
= dst
.n_bits
= 0;
3015 while (next_load_segment(sf
, &dst
, &value
)) {
3016 put_reg_load(openflow
, &dst
, value
);
3021 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
3022 * actions, if we can, falling back to Nicira extensions if we must.
3024 * We check only meta-flow types that can appear within set field actions and
3025 * that have a mapping to compatible action types. These struct mf_field
3026 * definitions have a defined OXM or NXM header value and specify the field as
3029 set_field_to_legacy_openflow(const struct ofpact_set_field
*sf
,
3030 enum ofp_version ofp_version
,
3033 switch ((int) sf
->field
->id
) {
3034 case MFF_VLAN_TCI
: {
3035 ovs_be16 tci
= sf
->value
->be16
;
3036 bool cfi
= (tci
& htons(VLAN_CFI
)) != 0;
3037 uint16_t vid
= vlan_tci_to_vid(tci
);
3038 uint8_t pcp
= vlan_tci_to_pcp(tci
);
3040 if (ofp_version
< OFP11_VERSION
) {
3041 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
3043 * If CFI=1, Add or modify VLAN VID & PCP.
3044 * If CFI=0, strip VLAN header, if any.
3047 put_OFPAT10_SET_VLAN_VID(out
, vid
);
3048 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
3050 put_OFPAT10_STRIP_VLAN(out
);
3053 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
3055 * If CFI=1, Add or modify VLAN VID & PCP.
3056 * OpenFlow 1.1 set actions only apply if the packet
3057 * already has VLAN tags. To be sure that is the case
3058 * we have to push a VLAN header. As we do not support
3059 * multiple layers of VLANs, this is a no-op, if a VLAN
3060 * header already exists. This may backfire, however,
3061 * when we start supporting multiple layers of VLANs.
3062 * If CFI=0, strip VLAN header, if any.
3065 /* Push a VLAN tag, if one was not seen at action validation
3067 if (!sf
->flow_has_vlan
) {
3068 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
3070 put_OFPAT11_SET_VLAN_VID(out
, vid
);
3071 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
3073 /* If the flow did not match on vlan, we have no way of
3074 * knowing if the vlan tag exists, so we must POP just to be
3076 put_OFPAT11_POP_VLAN(out
);
3082 case MFF_VLAN_VID
: {
3083 uint16_t vid
= ntohs(sf
->value
->be16
) & VLAN_VID_MASK
;
3084 if (ofp_version
== OFP10_VERSION
) {
3085 put_OFPAT10_SET_VLAN_VID(out
, vid
);
3087 put_OFPAT11_SET_VLAN_VID(out
, vid
);
3093 if (ofp_version
== OFP10_VERSION
) {
3094 put_OFPAT10_SET_VLAN_PCP(out
, sf
->value
->u8
);
3096 put_OFPAT11_SET_VLAN_PCP(out
, sf
->value
->u8
);
3101 put_OFPAT_SET_DL_SRC(out
, ofp_version
)->dl_addr
= sf
->value
->mac
;
3105 put_OFPAT_SET_DL_DST(out
, ofp_version
)->dl_addr
= sf
->value
->mac
;
3109 put_OFPAT_SET_NW_SRC(out
, ofp_version
, sf
->value
->be32
);
3113 put_OFPAT_SET_NW_DST(out
, ofp_version
, sf
->value
->be32
);
3117 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
->u8
);
3120 case MFF_IP_DSCP_SHIFTED
:
3121 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
->u8
<< 2);
3125 struct ofpact_ecn ip_ecn
= { .ecn
= sf
->value
->u8
};
3126 encode_SET_IP_ECN(&ip_ecn
, ofp_version
, out
);
3132 put_OFPAT_SET_TP_SRC(out
, sf
->value
->be16
);
3137 put_OFPAT_SET_TP_DST(out
, sf
->value
->be16
);
3141 set_field_to_nxast(sf
, out
);
3147 set_field_to_set_field(const struct ofpact_set_field
*sf
,
3148 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3150 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
3151 size_t start_ofs
= out
->size
;
3153 oasf
= put_OFPAT12_SET_FIELD(out
);
3154 out
->size
= out
->size
- sizeof oasf
->pad
;
3155 nx_put_entry(out
, sf
->field
, ofp_version
, sf
->value
,
3156 ofpact_set_field_mask(sf
));
3157 pad_ofpat(out
, start_ofs
);
3161 encode_SET_FIELD(const struct ofpact_set_field
*sf
,
3162 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3164 if (ofp_version
>= OFP15_VERSION
) {
3165 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
3167 set_field_to_set_field(sf
, ofp_version
, out
);
3168 } else if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD
||
3169 sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
) {
3170 /* It came in as reg_load, send it out the same way. */
3171 set_field_to_nxast(sf
, out
);
3172 } else if (ofp_version
< OFP12_VERSION
) {
3173 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
3174 set_field_to_legacy_openflow(sf
, ofp_version
, out
);
3175 } else if (is_all_ones(ofpact_set_field_mask(sf
), sf
->field
->n_bytes
)) {
3176 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
3177 * entire field, so encode it as OFPAT_SET_FIELD. */
3178 set_field_to_set_field(sf
, ofp_version
, out
);
3180 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
3181 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
3182 * so encode it as reg_load. */
3183 set_field_to_nxast(sf
, out
);
3187 /* Parses the input argument 'arg' into the key, value, and delimiter
3188 * components that are common across the reg_load and set_field action format.
3190 * With an argument like "1->metadata", sets the following pointers to
3191 * point within 'arg':
3196 * Returns NULL if successful, otherwise a malloc()'d string describing the
3197 * error. The caller is responsible for freeing the returned string. */
3198 static char * OVS_WARN_UNUSED_RESULT
3199 set_field_split_str(char *arg
, char **key
, char **value
, char **delim
)
3204 value_end
= strstr(arg
, "->");
3205 *key
= value_end
+ strlen("->");
3211 return xasprintf("%s: missing `->'", arg
);
3213 if (strlen(value_end
) <= strlen("->")) {
3214 return xasprintf("%s: missing field name following `->'", arg
);
3220 /* Parses a "set_field" action with argument 'arg', appending the parsed
3221 * action to 'pp->ofpacts'.
3223 * Returns NULL if successful, otherwise a malloc()'d string describing the
3224 * error. The caller is responsible for freeing the returned string. */
3225 static char * OVS_WARN_UNUSED_RESULT
3226 set_field_parse__(char *arg
, const struct ofpact_parse_params
*pp
)
3231 const struct mf_field
*mf
;
3232 union mf_value sf_value
, sf_mask
;
3235 error
= set_field_split_str(arg
, &key
, &value
, &delim
);
3240 mf
= mf_from_name(key
);
3242 return xasprintf("%s is not a valid OXM field name", key
);
3244 if (!mf
->writable
) {
3245 return xasprintf("%s is read-only", key
);
3249 error
= mf_parse(mf
, value
, pp
->port_map
, &sf_value
, &sf_mask
);
3254 if (!mf_is_value_valid(mf
, &sf_value
)) {
3255 return xasprintf("%s is not a valid value for field %s", value
, key
);
3258 *pp
->usable_protocols
&= mf
->usable_protocols_exact
;
3260 ofpact_put_set_field(pp
->ofpacts
, mf
, &sf_value
, &sf_mask
);
3264 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
3265 * action to 'pp->ofpacts'.
3267 * Returns NULL if successful, otherwise a malloc()'d string describing the
3268 * error. The caller is responsible for freeing the returned string. */
3269 static char * OVS_WARN_UNUSED_RESULT
3270 parse_SET_FIELD(const char *arg
, const struct ofpact_parse_params
*pp
)
3272 char *copy
= xstrdup(arg
);
3273 char *error
= set_field_parse__(copy
, pp
);
3278 static char * OVS_WARN_UNUSED_RESULT
3279 parse_reg_load(char *arg
, const struct ofpact_parse_params
*pp
)
3281 struct mf_subfield dst
;
3282 char *key
, *value_str
;
3283 union mf_value value
;
3286 error
= set_field_split_str(arg
, &key
, &value_str
, NULL
);
3291 error
= mf_parse_subfield(&dst
, key
);
3296 if (parse_int_string(value_str
, (uint8_t *)&value
, dst
.field
->n_bytes
,
3298 return xasprintf("%s: cannot parse integer value", arg
);
3301 if (!bitwise_is_all_zeros(&value
, dst
.field
->n_bytes
, dst
.n_bits
,
3302 dst
.field
->n_bytes
* 8 - dst
.n_bits
)) {
3306 mf_format(dst
.field
, &value
, NULL
, NULL
, &ds
);
3307 error
= xasprintf("%s: value %s does not fit into %d bits",
3308 arg
, ds_cstr(&ds
), dst
.n_bits
);
3313 struct ofpact_set_field
*sf
= ofpact_put_reg_load(pp
->ofpacts
, dst
.field
,
3316 bitwise_copy(&value
, dst
.field
->n_bytes
, 0, sf
->value
,
3317 dst
.field
->n_bytes
, dst
.ofs
, dst
.n_bits
);
3318 bitwise_one(ofpact_set_field_mask(sf
), dst
.field
->n_bytes
, dst
.ofs
,
3324 format_SET_FIELD(const struct ofpact_set_field
*a
,
3325 const struct ofpact_format_params
*fp
)
3327 if (a
->ofpact
.raw
== NXAST_RAW_REG_LOAD
) {
3328 struct mf_subfield dst
;
3331 dst
.ofs
= dst
.n_bits
= 0;
3332 while (next_load_segment(a
, &dst
, &value
)) {
3333 ds_put_format(fp
->s
, "%sload:%s%#"PRIx64
"%s->%s",
3334 colors
.special
, colors
.end
, value
,
3335 colors
.special
, colors
.end
);
3336 mf_format_subfield(&dst
, fp
->s
);
3337 ds_put_char(fp
->s
, ',');
3339 ds_chomp(fp
->s
, ',');
3341 ds_put_format(fp
->s
, "%sset_field:%s", colors
.special
, colors
.end
);
3342 mf_format(a
->field
, a
->value
, ofpact_set_field_mask(a
),
3343 fp
->port_map
, fp
->s
);
3344 ds_put_format(fp
->s
, "%s->%s%s",
3345 colors
.special
, colors
.end
, a
->field
->name
);
3350 check_SET_FIELD(struct ofpact_set_field
*a
,
3351 const struct ofpact_check_params
*cp
)
3353 const struct mf_field
*mf
= a
->field
;
3354 struct flow
*flow
= &cp
->match
->flow
;
3355 ovs_be16
*tci
= &flow
->vlans
[0].tci
;
3357 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
3358 if (!mf_are_prereqs_ok(mf
, flow
, NULL
)
3359 || (mf
->id
== MFF_VLAN_VID
&& !(*tci
& htons(VLAN_CFI
)))) {
3360 VLOG_WARN_RL(&rl
, "set_field %s lacks correct prerequisites",
3362 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
3365 /* Remember if we saw a VLAN tag in the flow, to aid translating to
3366 * OpenFlow 1.1 if need be. */
3367 a
->flow_has_vlan
= (*tci
& htons(VLAN_CFI
)) != 0;
3368 if (mf
->id
== MFF_VLAN_TCI
) {
3369 /* The set field may add or remove the VLAN tag,
3370 * Mark the status temporarily. */
3371 *tci
= a
->value
->be16
;
3377 /* Appends an OFPACT_SET_FIELD ofpact with enough space for the value and mask
3378 * for the 'field' to 'ofpacts' and returns it. Fills in the value from
3379 * 'value', if non-NULL, and mask from 'mask' if non-NULL. If 'value' is
3380 * non-NULL and 'mask' is NULL, an all-ones mask will be filled in. */
3381 struct ofpact_set_field
*
3382 ofpact_put_set_field(struct ofpbuf
*ofpacts
, const struct mf_field
*field
,
3383 const void *value
, const void *mask
)
3385 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(ofpacts
);
3388 /* Fill in the value and mask if given, otherwise put zeroes so that the
3389 * caller may fill in the value and mask itself. */
3391 ofpbuf_put_uninit(ofpacts
, 2 * field
->n_bytes
);
3392 sf
= ofpacts
->header
;
3393 memcpy(sf
->value
, value
, field
->n_bytes
);
3395 memcpy(ofpact_set_field_mask(sf
), mask
, field
->n_bytes
);
3397 memset(ofpact_set_field_mask(sf
), 0xff, field
->n_bytes
);
3400 ofpbuf_put_zeros(ofpacts
, 2 * field
->n_bytes
);
3401 sf
= ofpacts
->header
;
3403 /* Update length. */
3404 ofpact_finish_SET_FIELD(ofpacts
, &sf
);
3409 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
3410 * is marked such that, if possible, it will be translated to OpenFlow as
3411 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
3412 * that was the way that the action was expressed when it came into OVS or for
3413 * backward compatibility. */
3414 struct ofpact_set_field
*
3415 ofpact_put_reg_load(struct ofpbuf
*ofpacts
, const struct mf_field
*field
,
3416 const void *value
, const void *mask
)
3418 struct ofpact_set_field
*sf
= ofpact_put_set_field(ofpacts
, field
, value
,
3420 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD
;
3425 struct ofpact_set_field
*
3426 ofpact_put_reg_load2(struct ofpbuf
*ofpacts
, const struct mf_field
*field
,
3427 const void *value
, const void *mask
)
3429 struct ofpact_set_field
*sf
= ofpact_put_set_field(ofpacts
, field
, value
,
3431 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD2
;
3437 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
3439 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
3442 struct nx_action_stack
{
3443 ovs_be16 type
; /* OFPAT_VENDOR. */
3444 ovs_be16 len
; /* Length is 16. */
3445 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3446 ovs_be16 subtype
; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
3447 ovs_be16 offset
; /* Bit offset into the field. */
3449 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
3450 * - ovs_be16 'n_bits', the number of bits to extract from the field.
3451 * - Enough 0-bytes to pad out the action to 24 bytes. */
3452 uint8_t pad
[12]; /* See above. */
3454 OFP_ASSERT(sizeof(struct nx_action_stack
) == 24);
3457 decode_stack_action(const struct nx_action_stack
*nasp
,
3458 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
3459 struct ofpact_stack
*stack_action
)
3462 stack_action
->subfield
.ofs
= ntohs(nasp
->offset
);
3464 struct ofpbuf b
= ofpbuf_const_initializer(nasp
, sizeof *nasp
);
3465 ofpbuf_pull(&b
, OBJECT_OFFSETOF(nasp
, pad
));
3466 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
,
3467 &stack_action
->subfield
.field
, NULL
,
3473 stack_action
->subfield
.n_bits
= ntohs(*(const ovs_be16
*) b
.data
);
3475 if (!is_all_zeros(b
.data
, b
.size
)) {
3476 return OFPERR_NXBRC_MUST_BE_ZERO
;
3483 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack
*nasp
,
3484 enum ofp_version ofp_version OVS_UNUSED
,
3485 const struct vl_mff_map
*vl_mff_map
,
3486 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
3488 struct ofpact_stack
*push
= ofpact_put_STACK_PUSH(ofpacts
);
3489 enum ofperr error
= decode_stack_action(nasp
, vl_mff_map
, tlv_bitmap
,
3491 return error
? error
: nxm_stack_push_check(push
, NULL
);
3495 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack
*nasp
,
3496 enum ofp_version ofp_version OVS_UNUSED
,
3497 const struct vl_mff_map
*vl_mff_map
,
3498 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
3500 struct ofpact_stack
*pop
= ofpact_put_STACK_POP(ofpacts
);
3501 enum ofperr error
= decode_stack_action(nasp
, vl_mff_map
, tlv_bitmap
,
3503 return error
? error
: nxm_stack_pop_check(pop
, NULL
);
3507 encode_STACK_op(const struct ofpact_stack
*stack_action
,
3508 struct nx_action_stack
*nasp
)
3513 nasp
->offset
= htons(stack_action
->subfield
.ofs
);
3515 ofpbuf_use_stack(&b
, nasp
, ntohs(nasp
->len
));
3516 ofpbuf_put_uninit(&b
, OBJECT_OFFSETOF(nasp
, pad
));
3517 nx_put_mff_header(&b
, stack_action
->subfield
.field
, 0, false);
3518 n_bits
= htons(stack_action
->subfield
.n_bits
);
3519 ofpbuf_put(&b
, &n_bits
, sizeof n_bits
);
3523 encode_STACK_PUSH(const struct ofpact_stack
*stack
,
3524 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3526 encode_STACK_op(stack
, put_NXAST_STACK_PUSH(out
));
3530 encode_STACK_POP(const struct ofpact_stack
*stack
,
3531 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3533 encode_STACK_op(stack
, put_NXAST_STACK_POP(out
));
3536 static char * OVS_WARN_UNUSED_RESULT
3537 parse_STACK_PUSH(char *arg
, const struct ofpact_parse_params
*pp
)
3539 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(pp
->ofpacts
), arg
);
3542 static char * OVS_WARN_UNUSED_RESULT
3543 parse_STACK_POP(char *arg
, const struct ofpact_parse_params
*pp
)
3545 return nxm_parse_stack_action(ofpact_put_STACK_POP(pp
->ofpacts
), arg
);
3549 format_STACK_PUSH(const struct ofpact_stack
*a
,
3550 const struct ofpact_format_params
*fp
)
3552 nxm_format_stack_push(a
, fp
->s
);
3556 format_STACK_POP(const struct ofpact_stack
*a
,
3557 const struct ofpact_format_params
*fp
)
3559 nxm_format_stack_pop(a
, fp
->s
);
3563 check_STACK_PUSH(const struct ofpact_stack
*a
,
3564 const struct ofpact_check_params
*cp
)
3566 return nxm_stack_push_check(a
, cp
->match
);
3570 check_STACK_POP(const struct ofpact_stack
*a
,
3571 const struct ofpact_check_params
*cp
)
3573 return nxm_stack_pop_check(a
, cp
->match
);
3576 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
3578 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
3579 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
3580 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3581 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3583 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3584 * sent only to controllers with id 0.)
3586 struct nx_action_cnt_ids
{
3587 ovs_be16 type
; /* OFPAT_VENDOR. */
3588 ovs_be16 len
; /* Length including cnt_ids. */
3589 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3590 ovs_be16 subtype
; /* NXAST_DEC_TTL_CNT_IDS. */
3592 ovs_be16 n_controllers
; /* Number of controllers. */
3593 uint8_t zeros
[4]; /* Must be zero. */
3595 /* Followed by 1 or more controller ids:
3597 * uint16_t cnt_ids[]; -- Controller ids.
3598 * uint8_t pad[]; -- Must be 0 to 8-byte align cnt_ids[].
3601 OFP_ASSERT(sizeof(struct nx_action_cnt_ids
) == 16);
3604 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf
*out
)
3607 struct ofpact_cnt_ids
*ids
;
3608 enum ofperr error
= 0;
3610 ids
= ofpact_put_DEC_TTL(out
);
3611 ids
->n_controllers
= 1;
3612 ofpbuf_put(out
, &id
, sizeof id
);
3614 ofpact_finish_DEC_TTL(out
, &ids
);
3619 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids
*nac_ids
,
3620 enum ofp_version ofp_version OVS_UNUSED
,
3623 struct ofpact_cnt_ids
*ids
;
3627 ids
= ofpact_put_DEC_TTL(out
);
3628 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
3629 ids
->n_controllers
= ntohs(nac_ids
->n_controllers
);
3630 ids_size
= ntohs(nac_ids
->len
) - sizeof *nac_ids
;
3632 if (!is_all_zeros(nac_ids
->zeros
, sizeof nac_ids
->zeros
)) {
3633 return OFPERR_NXBRC_MUST_BE_ZERO
;
3636 if (ids_size
< ids
->n_controllers
* sizeof(ovs_be16
)) {
3637 VLOG_WARN_RL(&rl
, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE
" "
3638 "bytes allocated for controller ids. %"PRIuSIZE
" bytes "
3639 "are required for %u controllers.",
3640 ids_size
, ids
->n_controllers
* sizeof(ovs_be16
),
3641 ids
->n_controllers
);
3642 return OFPERR_OFPBAC_BAD_LEN
;
3645 for (i
= 0; i
< ids
->n_controllers
; i
++) {
3646 uint16_t id
= ntohs(((ovs_be16
*)(nac_ids
+ 1))[i
]);
3647 ofpbuf_put(out
, &id
, sizeof id
);
3651 ofpact_finish_DEC_TTL(out
, &ids
);
3657 encode_DEC_TTL(const struct ofpact_cnt_ids
*dec_ttl
,
3658 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3660 if (dec_ttl
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
3661 || dec_ttl
->n_controllers
!= 1
3662 || dec_ttl
->cnt_ids
[0] != 0) {
3663 struct nx_action_cnt_ids
*nac_ids
= put_NXAST_DEC_TTL_CNT_IDS(out
);
3664 int ids_len
= ROUND_UP(2 * dec_ttl
->n_controllers
, OFP_ACTION_ALIGN
);
3668 nac_ids
->len
= htons(ntohs(nac_ids
->len
) + ids_len
);
3669 nac_ids
->n_controllers
= htons(dec_ttl
->n_controllers
);
3671 ids
= ofpbuf_put_zeros(out
, ids_len
);
3672 for (i
= 0; i
< dec_ttl
->n_controllers
; i
++) {
3673 ids
[i
] = htons(dec_ttl
->cnt_ids
[i
]);
3676 put_OFPAT_DEC_NW_TTL(out
, ofp_version
);
3681 parse_noargs_dec_ttl(const struct ofpact_parse_params
*pp
)
3683 struct ofpact_cnt_ids
*ids
;
3686 ofpact_put_DEC_TTL(pp
->ofpacts
);
3687 ofpbuf_put(pp
->ofpacts
, &id
, sizeof id
);
3688 ids
= pp
->ofpacts
->header
;
3689 ids
->n_controllers
++;
3690 ofpact_finish_DEC_TTL(pp
->ofpacts
, &ids
);
3693 static char * OVS_WARN_UNUSED_RESULT
3694 parse_DEC_TTL(char *arg
, const struct ofpact_parse_params
*pp
)
3697 parse_noargs_dec_ttl(pp
);
3699 struct ofpact_cnt_ids
*ids
;
3702 ids
= ofpact_put_DEC_TTL(pp
->ofpacts
);
3703 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
3704 for (cntr
= strtok_r(arg
, ", ", &arg
); cntr
!= NULL
;
3705 cntr
= strtok_r(NULL
, ", ", &arg
)) {
3706 uint16_t id
= atoi(cntr
);
3708 ofpbuf_put(pp
->ofpacts
, &id
, sizeof id
);
3709 ids
= pp
->ofpacts
->header
;
3710 ids
->n_controllers
++;
3712 if (!ids
->n_controllers
) {
3713 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3717 if (ofpbuf_oversized(pp
->ofpacts
)) {
3718 return xasprintf("input too big");
3721 ofpact_finish_DEC_TTL(pp
->ofpacts
, &ids
);
3727 format_DEC_TTL(const struct ofpact_cnt_ids
*a
,
3728 const struct ofpact_format_params
*fp
)
3732 ds_put_format(fp
->s
, "%sdec_ttl%s", colors
.paren
, colors
.end
);
3733 if (a
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
) {
3734 ds_put_format(fp
->s
, "%s(%s", colors
.paren
, colors
.end
);
3735 for (i
= 0; i
< a
->n_controllers
; i
++) {
3737 ds_put_cstr(fp
->s
, ",");
3739 ds_put_format(fp
->s
, "%"PRIu16
, a
->cnt_ids
[i
]);
3741 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
3746 check_DEC_TTL(const struct ofpact_cnt_ids
*a OVS_UNUSED
,
3747 struct ofpact_check_params
*cp
)
3749 return check_set_ip(cp
);
3752 /* Set MPLS label actions. */
3755 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label
,
3756 enum ofp_version ofp_version OVS_UNUSED
,
3759 ofpact_put_SET_MPLS_LABEL(out
)->label
= label
;
3764 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label
*label
,
3765 enum ofp_version ofp_version
,
3768 if (ofp_version
< OFP12_VERSION
) {
3769 put_OFPAT_SET_MPLS_LABEL(out
, ofp_version
, label
->label
);
3771 put_set_field(out
, ofp_version
, MFF_MPLS_LABEL
, ntohl(label
->label
));
3775 static char * OVS_WARN_UNUSED_RESULT
3776 parse_SET_MPLS_LABEL(char *arg
, const struct ofpact_parse_params
*pp
)
3778 struct ofpact_mpls_label
*mpls_label
3779 = ofpact_put_SET_MPLS_LABEL(pp
->ofpacts
);
3781 return xstrdup("set_mpls_label: expected label.");
3784 mpls_label
->label
= htonl(atoi(arg
));
3789 format_SET_MPLS_LABEL(const struct ofpact_mpls_label
*a
,
3790 const struct ofpact_format_params
*fp
)
3792 ds_put_format(fp
->s
, "%sset_mpls_label(%s%"PRIu32
"%s)%s",
3793 colors
.paren
, colors
.end
, ntohl(a
->label
),
3794 colors
.paren
, colors
.end
);
3798 check_set_mpls(struct ofpact_check_params
*cp
)
3800 ovs_be16 dl_type
= get_dl_type(&cp
->match
->flow
);
3801 if (!eth_type_mpls(dl_type
)) {
3802 inconsistent_match(&cp
->usable_protocols
);
3808 check_SET_MPLS_LABEL(const struct ofpact_mpls_label
*a OVS_UNUSED
,
3809 struct ofpact_check_params
*cp
)
3811 return check_set_mpls(cp
);
3814 /* Set MPLS TC actions. */
3817 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc
,
3818 enum ofp_version ofp_version OVS_UNUSED
,
3821 ofpact_put_SET_MPLS_TC(out
)->tc
= tc
;
3826 encode_SET_MPLS_TC(const struct ofpact_mpls_tc
*tc
,
3827 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3829 if (ofp_version
< OFP12_VERSION
) {
3830 put_OFPAT_SET_MPLS_TC(out
, ofp_version
, tc
->tc
);
3832 put_set_field(out
, ofp_version
, MFF_MPLS_TC
, tc
->tc
);
3836 static char * OVS_WARN_UNUSED_RESULT
3837 parse_SET_MPLS_TC(char *arg
, const struct ofpact_parse_params
*pp
)
3839 struct ofpact_mpls_tc
*mpls_tc
= ofpact_put_SET_MPLS_TC(pp
->ofpacts
);
3842 return xstrdup("set_mpls_tc: expected tc.");
3845 mpls_tc
->tc
= atoi(arg
);
3850 format_SET_MPLS_TC(const struct ofpact_mpls_tc
*a
,
3851 const struct ofpact_format_params
*fp
)
3853 ds_put_format(fp
->s
, "%sset_mpls_ttl(%s%"PRIu8
"%s)%s",
3854 colors
.paren
, colors
.end
, a
->tc
,
3855 colors
.paren
, colors
.end
);
3859 check_SET_MPLS_TC(const struct ofpact_mpls_tc
*a OVS_UNUSED
,
3860 struct ofpact_check_params
*cp
)
3862 return check_set_mpls(cp
);
3865 /* Set MPLS TTL actions. */
3868 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl
,
3869 enum ofp_version ofp_version OVS_UNUSED
,
3872 ofpact_put_SET_MPLS_TTL(out
)->ttl
= ttl
;
3877 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*ttl
,
3878 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3880 put_OFPAT_SET_MPLS_TTL(out
, ofp_version
, ttl
->ttl
);
3883 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3884 * action to 'pp->ofpacts'.
3886 * Returns NULL if successful, otherwise a malloc()'d string describing the
3887 * error. The caller is responsible for freeing the returned string. */
3888 static char * OVS_WARN_UNUSED_RESULT
3889 parse_SET_MPLS_TTL(char *arg
, const struct ofpact_parse_params
*pp
)
3891 struct ofpact_mpls_ttl
*mpls_ttl
= ofpact_put_SET_MPLS_TTL(pp
->ofpacts
);
3894 return xstrdup("set_mpls_ttl: expected ttl.");
3897 mpls_ttl
->ttl
= atoi(arg
);
3902 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*a
,
3903 const struct ofpact_format_params
*fp
)
3905 ds_put_format(fp
->s
, "%sset_mpls_ttl(%s%"PRIu8
"%s)%s",
3906 colors
.paren
, colors
.end
, a
->ttl
,
3907 colors
.paren
, colors
.end
);
3911 check_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*a OVS_UNUSED
,
3912 struct ofpact_check_params
*cp
)
3914 return check_set_mpls(cp
);
3917 /* Decrement MPLS TTL actions. */
3920 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf
*out
)
3922 ofpact_put_DEC_MPLS_TTL(out
);
3927 encode_DEC_MPLS_TTL(const struct ofpact_null
*null OVS_UNUSED
,
3928 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3930 put_OFPAT_DEC_MPLS_TTL(out
, ofp_version
);
3933 static char * OVS_WARN_UNUSED_RESULT
3934 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
3936 ofpact_put_DEC_MPLS_TTL(pp
->ofpacts
);
3941 format_DEC_MPLS_TTL(const struct ofpact_null
*a OVS_UNUSED
,
3942 const struct ofpact_format_params
*fp
)
3944 ds_put_format(fp
->s
, "%sdec_mpls_ttl%s", colors
.value
, colors
.end
);
3948 check_DEC_MPLS_TTL(const struct ofpact_null
*a OVS_UNUSED
,
3949 struct ofpact_check_params
*cp
)
3951 return check_set_mpls(cp
);
3954 /* Push MPLS label action. */
3957 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype
,
3958 enum ofp_version ofp_version OVS_UNUSED
,
3961 struct ofpact_push_mpls
*oam
;
3963 if (!eth_type_mpls(ethertype
)) {
3964 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3966 oam
= ofpact_put_PUSH_MPLS(out
);
3967 oam
->ethertype
= ethertype
;
3973 encode_PUSH_MPLS(const struct ofpact_push_mpls
*push_mpls
,
3974 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3976 put_OFPAT_PUSH_MPLS(out
, ofp_version
, push_mpls
->ethertype
);
3979 static char * OVS_WARN_UNUSED_RESULT
3980 parse_PUSH_MPLS(char *arg
, const struct ofpact_parse_params
*pp
)
3985 error
= str_to_u16(arg
, "push_mpls", ðertype
);
3987 ofpact_put_PUSH_MPLS(pp
->ofpacts
)->ethertype
= htons(ethertype
);
3993 format_PUSH_MPLS(const struct ofpact_push_mpls
*a
,
3994 const struct ofpact_format_params
*fp
)
3996 ds_put_format(fp
->s
, "%spush_mpls:%s0x%04"PRIx16
,
3997 colors
.param
, colors
.end
, ntohs(a
->ethertype
));
4001 check_PUSH_MPLS(const struct ofpact_push_mpls
*a
,
4002 struct ofpact_check_params
*cp
)
4004 struct flow
*flow
= &cp
->match
->flow
;
4006 if (flow
->packet_type
!= htonl(PT_ETH
)) {
4007 inconsistent_match(&cp
->usable_protocols
);
4009 flow
->dl_type
= a
->ethertype
;
4011 /* The packet is now MPLS and the MPLS payload is opaque.
4012 * Thus nothing can be assumed about the network protocol.
4013 * Temporarily mark that we have no nw_proto. */
4019 /* Pop MPLS label action. */
4022 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype
,
4023 enum ofp_version ofp_version OVS_UNUSED
,
4026 ofpact_put_POP_MPLS(out
)->ethertype
= ethertype
;
4031 encode_POP_MPLS(const struct ofpact_pop_mpls
*pop_mpls
,
4032 enum ofp_version ofp_version
, struct ofpbuf
*out
)
4034 put_OFPAT_POP_MPLS(out
, ofp_version
, pop_mpls
->ethertype
);
4037 static char * OVS_WARN_UNUSED_RESULT
4038 parse_POP_MPLS(char *arg
, const struct ofpact_parse_params
*pp
)
4043 error
= str_to_u16(arg
, "pop_mpls", ðertype
);
4045 ofpact_put_POP_MPLS(pp
->ofpacts
)->ethertype
= htons(ethertype
);
4051 format_POP_MPLS(const struct ofpact_pop_mpls
*a
,
4052 const struct ofpact_format_params
*fp
)
4054 ds_put_format(fp
->s
, "%spop_mpls:%s0x%04"PRIx16
,
4055 colors
.param
, colors
.end
, ntohs(a
->ethertype
));
4059 check_POP_MPLS(const struct ofpact_pop_mpls
*a
, struct ofpact_check_params
*cp
)
4061 struct flow
*flow
= &cp
->match
->flow
;
4062 ovs_be16 dl_type
= get_dl_type(flow
);
4064 if (flow
->packet_type
!= htonl(PT_ETH
) || !eth_type_mpls(dl_type
)) {
4065 inconsistent_match(&cp
->usable_protocols
);
4067 flow
->dl_type
= a
->ethertype
;
4071 /* Set tunnel ID actions. */
4074 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id
,
4075 enum ofp_version ofp_version OVS_UNUSED
,
4078 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
4079 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL
;
4080 tunnel
->tun_id
= tun_id
;
4085 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id
,
4086 enum ofp_version ofp_version OVS_UNUSED
,
4089 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
4090 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL64
;
4091 tunnel
->tun_id
= tun_id
;
4096 encode_SET_TUNNEL(const struct ofpact_tunnel
*tunnel
,
4097 enum ofp_version ofp_version
, struct ofpbuf
*out
)
4099 uint64_t tun_id
= tunnel
->tun_id
;
4101 if (ofp_version
< OFP12_VERSION
) {
4102 if (tun_id
<= UINT32_MAX
4103 && tunnel
->ofpact
.raw
!= NXAST_RAW_SET_TUNNEL64
) {
4104 put_NXAST_SET_TUNNEL(out
, tun_id
);
4106 put_NXAST_SET_TUNNEL64(out
, tun_id
);
4109 put_set_field(out
, ofp_version
, MFF_TUN_ID
, tun_id
);
4113 static char * OVS_WARN_UNUSED_RESULT
4114 parse_set_tunnel(char *arg
, enum ofp_raw_action_type raw
,
4115 const struct ofpact_parse_params
*pp
)
4117 struct ofpact_tunnel
*tunnel
;
4119 tunnel
= ofpact_put_SET_TUNNEL(pp
->ofpacts
);
4120 tunnel
->ofpact
.raw
= raw
;
4121 return str_to_u64(arg
, &tunnel
->tun_id
);
4124 static char * OVS_WARN_UNUSED_RESULT
4125 parse_SET_TUNNEL(char *arg
, const struct ofpact_parse_params
*pp
)
4127 return parse_set_tunnel(arg
, NXAST_RAW_SET_TUNNEL
, pp
);
4131 format_SET_TUNNEL(const struct ofpact_tunnel
*a
,
4132 const struct ofpact_format_params
*fp
)
4134 ds_put_format(fp
->s
, "%sset_tunnel%s:%s%#"PRIx64
, colors
.param
,
4135 (a
->tun_id
> UINT32_MAX
4136 || a
->ofpact
.raw
== NXAST_RAW_SET_TUNNEL64
? "64" : ""),
4137 colors
.end
, a
->tun_id
);
4141 check_SET_TUNNEL(const struct ofpact_tunnel
*a OVS_UNUSED
,
4142 const struct ofpact_check_params
*cp OVS_UNUSED
)
4147 /* Delete field action. */
4149 /* Action structure for DELETE_FIELD */
4150 struct nx_action_delete_field
{
4151 ovs_be16 type
; /* OFPAT_VENDOR */
4152 ovs_be16 len
; /* Length is 24. */
4153 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4154 ovs_be16 subtype
; /* NXAST_DELETE_FIELD. */
4156 * - OXM/NXM header for field to delete (4 or 8 bytes).
4157 * - Enough 0-bytes to pad out the action to 24 bytes. */
4160 OFP_ASSERT(sizeof(struct nx_action_delete_field
) == 24);
4163 decode_NXAST_RAW_DELETE_FIELD(const struct nx_action_delete_field
*nadf
,
4164 enum ofp_version ofp_version OVS_UNUSED
,
4165 const struct vl_mff_map
*vl_mff_map
,
4166 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
4168 struct ofpact_delete_field
*delete_field
;
4171 delete_field
= ofpact_put_DELETE_FIELD(out
);
4172 delete_field
->ofpact
.raw
= NXAST_RAW_DELETE_FIELD
;
4174 struct ofpbuf b
= ofpbuf_const_initializer(nadf
, ntohs(nadf
->len
));
4175 ofpbuf_pull(&b
, OBJECT_OFFSETOF(nadf
, pad
));
4177 err
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
, &delete_field
->field
,
4187 encode_DELETE_FIELD(const struct ofpact_delete_field
*delete_field
,
4188 enum ofp_version ofp_version OVS_UNUSED
,
4191 struct nx_action_delete_field
*nadf
= put_NXAST_DELETE_FIELD(out
);
4192 size_t size
= out
->size
;
4194 out
->size
= size
- sizeof nadf
->pad
;
4195 nx_put_mff_header(out
, delete_field
->field
, 0, false);
4199 static char * OVS_WARN_UNUSED_RESULT
4200 parse_DELETE_FIELD(char *arg
, const struct ofpact_parse_params
*pp
)
4202 struct ofpact_delete_field
*delete_field
;
4204 delete_field
= ofpact_put_DELETE_FIELD(pp
->ofpacts
);
4205 return mf_parse_field(&delete_field
->field
, arg
);
4209 format_DELETE_FIELD(const struct ofpact_delete_field
*odf
,
4210 const struct ofpact_format_params
*fp
)
4212 ds_put_format(fp
->s
, "%sdelete_field:%s", colors
.param
,
4214 ds_put_format(fp
->s
, "%s", odf
->field
->name
);
4218 check_DELETE_FIELD(const struct ofpact_delete_field
*odf
,
4219 struct ofpact_check_params
*cp OVS_UNUSED
)
4221 if (odf
->field
->id
< MFF_TUN_METADATA0
||
4222 odf
->field
->id
> MFF_TUN_METADATA63
) {
4223 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4228 /* Set queue action. */
4231 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id
,
4232 enum ofp_version ofp_version OVS_UNUSED
,
4235 ofpact_put_SET_QUEUE(out
)->queue_id
= queue_id
;
4240 encode_SET_QUEUE(const struct ofpact_queue
*queue
,
4241 enum ofp_version ofp_version
, struct ofpbuf
*out
)
4243 put_OFPAT_SET_QUEUE(out
, ofp_version
, queue
->queue_id
);
4246 static char * OVS_WARN_UNUSED_RESULT
4247 parse_SET_QUEUE(char *arg
, const struct ofpact_parse_params
*pp
)
4249 return str_to_u32(arg
, &ofpact_put_SET_QUEUE(pp
->ofpacts
)->queue_id
);
4253 format_SET_QUEUE(const struct ofpact_queue
*a
,
4254 const struct ofpact_format_params
*fp
)
4256 ds_put_format(fp
->s
, "%sset_queue:%s%"PRIu32
,
4257 colors
.param
, colors
.end
, a
->queue_id
);
4261 check_SET_QUEUE(const struct ofpact_queue
*a OVS_UNUSED
,
4262 const struct ofpact_check_params
*cp OVS_UNUSED
)
4267 /* Pop queue action. */
4270 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf
*out
)
4272 ofpact_put_POP_QUEUE(out
);
4277 encode_POP_QUEUE(const struct ofpact_null
*null OVS_UNUSED
,
4278 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4280 put_NXAST_POP_QUEUE(out
);
4283 static char * OVS_WARN_UNUSED_RESULT
4284 parse_POP_QUEUE(const char *arg OVS_UNUSED
,
4285 const struct ofpact_parse_params
*pp
)
4287 ofpact_put_POP_QUEUE(pp
->ofpacts
);
4292 format_POP_QUEUE(const struct ofpact_null
*a OVS_UNUSED
,
4293 const struct ofpact_format_params
*fp
)
4295 ds_put_format(fp
->s
, "%spop_queue%s", colors
.value
, colors
.end
);
4299 check_POP_QUEUE(const struct ofpact_null
*a OVS_UNUSED
,
4300 const struct ofpact_check_params
*cp OVS_UNUSED
)
4305 /* Action structure for NXAST_FIN_TIMEOUT.
4307 * This action changes the idle timeout or hard timeout, or both, of this
4308 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
4309 * When such a packet is observed, the action reduces the rule's idle timeout
4310 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
4311 * action has no effect on an existing timeout that is already shorter than the
4312 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
4313 * of zero has no effect on the respective timeout.
4315 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
4316 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
4317 * receipt of the FIN or RST.
4319 * This is useful for quickly discarding learned TCP flows that otherwise will
4320 * take a long time to expire.
4322 * This action is intended for use with an OpenFlow rule that matches only a
4323 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
4324 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
4325 * RST in any of those flows will cause the entire OpenFlow rule to expire
4326 * early, which is not normally desirable.
4328 struct nx_action_fin_timeout
{
4329 ovs_be16 type
; /* OFPAT_VENDOR. */
4330 ovs_be16 len
; /* 16. */
4331 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4332 ovs_be16 subtype
; /* NXAST_FIN_TIMEOUT. */
4333 ovs_be16 fin_idle_timeout
; /* New idle timeout, if nonzero. */
4334 ovs_be16 fin_hard_timeout
; /* New hard timeout, if nonzero. */
4335 ovs_be16 pad
; /* Must be zero. */
4337 OFP_ASSERT(sizeof(struct nx_action_fin_timeout
) == 16);
4340 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout
*naft
,
4341 enum ofp_version ofp_version OVS_UNUSED
,
4344 struct ofpact_fin_timeout
*oft
;
4346 oft
= ofpact_put_FIN_TIMEOUT(out
);
4347 oft
->fin_idle_timeout
= ntohs(naft
->fin_idle_timeout
);
4348 oft
->fin_hard_timeout
= ntohs(naft
->fin_hard_timeout
);
4353 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout
*fin_timeout
,
4354 enum ofp_version ofp_version OVS_UNUSED
,
4357 struct nx_action_fin_timeout
*naft
= put_NXAST_FIN_TIMEOUT(out
);
4358 naft
->fin_idle_timeout
= htons(fin_timeout
->fin_idle_timeout
);
4359 naft
->fin_hard_timeout
= htons(fin_timeout
->fin_hard_timeout
);
4362 static char * OVS_WARN_UNUSED_RESULT
4363 parse_FIN_TIMEOUT(char *arg
, const struct ofpact_parse_params
*pp
)
4365 struct ofpact_fin_timeout
*oft
= ofpact_put_FIN_TIMEOUT(pp
->ofpacts
);
4368 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
4371 if (!strcmp(key
, "idle_timeout")) {
4372 error
= str_to_u16(value
, key
, &oft
->fin_idle_timeout
);
4373 } else if (!strcmp(key
, "hard_timeout")) {
4374 error
= str_to_u16(value
, key
, &oft
->fin_hard_timeout
);
4376 error
= xasprintf("invalid key '%s' in 'fin_timeout' argument",
4388 format_FIN_TIMEOUT(const struct ofpact_fin_timeout
*a
,
4389 const struct ofpact_format_params
*fp
)
4391 ds_put_format(fp
->s
, "%sfin_timeout(%s", colors
.paren
, colors
.end
);
4392 if (a
->fin_idle_timeout
) {
4393 ds_put_format(fp
->s
, "%sidle_timeout=%s%"PRIu16
",",
4394 colors
.param
, colors
.end
, a
->fin_idle_timeout
);
4396 if (a
->fin_hard_timeout
) {
4397 ds_put_format(fp
->s
, "%shard_timeout=%s%"PRIu16
",",
4398 colors
.param
, colors
.end
, a
->fin_hard_timeout
);
4400 ds_chomp(fp
->s
, ',');
4401 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
4406 check_FIN_TIMEOUT(const struct ofpact_fin_timeout
*a OVS_UNUSED
,
4407 struct ofpact_check_params
*cp
)
4409 if (cp
->match
->flow
.nw_proto
!= IPPROTO_TCP
) {
4410 inconsistent_match(&cp
->usable_protocols
);
4415 /* Action structure for NXAST_ENCAP */
4416 struct nx_action_encap
{
4417 ovs_be16 type
; /* OFPAT_VENDOR. */
4418 ovs_be16 len
; /* Total size including any property TLVs. */
4419 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4420 ovs_be16 subtype
; /* NXAST_ENCAP. */
4421 ovs_be16 hdr_size
; /* Header size in bytes, 0 = 'not specified'.*/
4422 ovs_be32 new_pkt_type
; /* Header type to add and PACKET_TYPE of result. */
4423 struct ofp_ed_prop_header props
[]; /* Encap TLV properties. */
4425 OFP_ASSERT(sizeof(struct nx_action_encap
) == 16);
4428 decode_NXAST_RAW_ENCAP(const struct nx_action_encap
*nae
,
4429 enum ofp_version ofp_version OVS_UNUSED
,
4432 struct ofpact_encap
*encap
;
4433 const struct ofp_ed_prop_header
*ofp_prop
;
4435 uint16_t n_props
= 0;
4438 encap
= ofpact_put_ENCAP(out
);
4439 encap
->ofpact
.raw
= NXAST_RAW_ENCAP
;
4440 switch (ntohl(nae
->new_pkt_type
)) {
4443 /* Add supported encap header types here. */
4446 return OFPERR_NXBAC_BAD_HEADER_TYPE
;
4448 encap
->new_pkt_type
= nae
->new_pkt_type
;
4449 encap
->hdr_size
= ntohs(nae
->hdr_size
);
4451 ofp_prop
= nae
->props
;
4452 props_len
= ntohs(nae
->len
) - offsetof(struct nx_action_encap
, props
);
4454 while (props_len
> 0) {
4455 err
= decode_ed_prop(&ofp_prop
, out
, &props_len
);
4461 encap
->n_props
= n_props
;
4462 out
->header
= &encap
->ofpact
;
4463 ofpact_finish_ENCAP(out
, &encap
);
4469 encode_ENCAP(const struct ofpact_encap
*encap
,
4470 enum ofp_version ofp_version OVS_UNUSED
,
4473 size_t start_ofs
= out
->size
;
4474 struct nx_action_encap
*nae
= put_NXAST_ENCAP(out
);
4477 nae
->new_pkt_type
= encap
->new_pkt_type
;
4478 nae
->hdr_size
= htons(encap
->hdr_size
);
4479 const struct ofpact_ed_prop
*prop
= encap
->props
;
4480 for (i
= 0; i
< encap
->n_props
; i
++) {
4481 encode_ed_prop(&prop
, out
);
4483 pad_ofpat(out
, start_ofs
);
4487 parse_encap_header(const char *hdr
, ovs_be32
*packet_type
)
4489 if (strcmp(hdr
, "ethernet") == 0) {
4490 *packet_type
= htonl(PT_ETH
);
4491 } else if (strcmp(hdr
, "nsh") == 0) {
4492 *packet_type
= htonl(PT_NSH
);
4500 parse_ed_props(const uint16_t prop_class
, char **arg
, int *n_props
, struct ofpbuf
*out
)
4502 char *key
, *value
, *err
;
4505 while (ofputil_parse_key_value(arg
, &key
, &value
)) {
4506 if (!parse_ed_prop_type(prop_class
, key
, &prop_type
)) {
4507 return xasprintf("Invalid property: %s", key
);
4509 if (value
== NULL
) {
4510 return xasprintf("Missing the value for property: %s", key
);
4512 err
= parse_ed_prop_value(prop_class
, prop_type
, value
, out
);
4521 /* The string representation of the encap action is
4522 * encap(header_type(prop=<value>,tlv(<class>,<type>,<value>),...))
4525 static char * OVS_WARN_UNUSED_RESULT
4526 parse_ENCAP(char *arg
, const struct ofpact_parse_params
*pp
)
4528 *pp
->usable_protocols
&= OFPUTIL_P_OF13_UP
;
4530 struct ofpact_encap
*encap
;
4531 char *key
, *value
, *str
;
4533 uint16_t prop_class
;
4536 encap
= ofpact_put_ENCAP(pp
->ofpacts
);
4537 encap
->hdr_size
= 0;
4538 /* Parse encap header type. */
4540 if (!ofputil_parse_key_value(&arg
, &key
, &value
)) {
4541 return xasprintf("Missing encap hdr: %s", str
);
4543 if (!parse_encap_header(key
, &encap
->new_pkt_type
)) {
4544 return xasprintf("Encap hdr not supported: %s", value
);
4546 if (!parse_ed_prop_class(key
, &prop_class
)) {
4547 return xasprintf("Invalid encap prop class: %s", key
);
4549 /* Parse encap properties. */
4550 error
= parse_ed_props(prop_class
, &value
, &n_props
, pp
->ofpacts
);
4551 if (error
!= NULL
) {
4554 /* ofpbuf may have been re-allocated. */
4555 encap
= pp
->ofpacts
->header
;
4556 encap
->n_props
= n_props
;
4558 if (ofpbuf_oversized(pp
->ofpacts
)) {
4559 return xasprintf("input too big");
4562 ofpact_finish_ENCAP(pp
->ofpacts
, &encap
);
4567 format_encap_pkt_type(const ovs_be32 pkt_type
)
4569 switch (ntohl(pkt_type
)) {
4580 format_ed_props(struct ds
*s
, uint16_t n_props
,
4581 const struct ofpact_ed_prop
*prop
)
4583 const uint8_t *p
= (uint8_t *) prop
;
4589 for (i
= 0; i
< n_props
; i
++) {
4590 format_ed_prop(s
, prop
);
4591 ds_put_char(s
, ',');
4592 p
+= ROUND_UP(prop
->len
, 8);
4593 prop
= ALIGNED_CAST(const struct ofpact_ed_prop
*, p
);
4601 format_ENCAP(const struct ofpact_encap
*a
,
4602 const struct ofpact_format_params
*fp
)
4604 ds_put_format(fp
->s
, "%sencap(%s", colors
.paren
, colors
.end
);
4605 ds_put_format(fp
->s
, "%s", format_encap_pkt_type(a
->new_pkt_type
));
4606 if (a
->n_props
> 0) {
4607 ds_put_format(fp
->s
, "%s(%s", colors
.paren
, colors
.end
);
4608 format_ed_props(fp
->s
, a
->n_props
, a
->props
);
4609 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
4611 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
4615 check_ENCAP(const struct ofpact_encap
*a
, struct ofpact_check_params
*cp
)
4617 struct flow
*flow
= &cp
->match
->flow
;
4618 flow
->packet_type
= a
->new_pkt_type
;
4619 if (pt_ns(flow
->packet_type
) == OFPHTN_ETHERTYPE
) {
4620 flow
->dl_type
= htons(pt_ns_type(flow
->packet_type
));
4622 if (!is_ip_any(flow
)) {
4628 /* Action structure for NXAST_DECAP */
4629 struct nx_action_decap
{
4630 ovs_be16 type
; /* OFPAT_VENDOR. */
4631 ovs_be16 len
; /* Total size including any property TLVs. */
4632 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4633 ovs_be16 subtype
; /* NXAST_DECAP. */
4634 uint8_t pad
[2]; /* 2 bytes padding */
4636 /* Packet type or result.
4638 * The special value (0,0xFFFE) "Use next proto"
4639 * is used to request OVS to automatically set the new packet type based
4640 * on the decap'ed header's next protocol.
4642 ovs_be32 new_pkt_type
;
4644 OFP_ASSERT(sizeof(struct nx_action_decap
) == 16);
4647 decode_NXAST_RAW_DECAP(const struct nx_action_decap
*nad
,
4648 enum ofp_version ofp_version OVS_UNUSED
,
4649 struct ofpbuf
*ofpacts
)
4651 struct ofpact_decap
* decap
;
4653 if (ntohs(nad
->len
) > sizeof *nad
) {
4654 /* No properties supported yet. */
4655 return OFPERR_NXBAC_UNKNOWN_ED_PROP
;
4658 decap
= ofpact_put_DECAP(ofpacts
);
4659 decap
->ofpact
.raw
= NXAST_RAW_DECAP
;
4660 decap
->new_pkt_type
= nad
->new_pkt_type
;
4665 encode_DECAP(const struct ofpact_decap
*decap
,
4666 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4668 struct nx_action_decap
*nad
= put_NXAST_DECAP(out
);
4670 nad
->len
= htons(sizeof(struct nx_action_decap
));
4671 nad
->new_pkt_type
= decap
->new_pkt_type
;
4674 static char * OVS_WARN_UNUSED_RESULT
4675 parse_DECAP(char *arg
, const struct ofpact_parse_params
*pp
)
4677 struct ofpact_decap
*decap
;
4678 char *key
, *value
, *pos
;
4682 decap
= ofpact_put_DECAP(pp
->ofpacts
);
4683 /* Default next packet_type is PT_USE_NEXT_PROTO. */
4684 decap
->new_pkt_type
= htonl(PT_USE_NEXT_PROTO
);
4686 /* Parse decap packet_type if given. */
4687 if (ofputil_parse_key_value(&arg
, &key
, &value
)) {
4688 if (strcmp(key
, "packet_type") == 0) {
4690 if (!ofputil_parse_key_value(&pos
, &key
, &value
)
4691 || strcmp(key
, "ns") != 0) {
4692 return xstrdup("Missing packet_type attribute ns");
4694 error
= str_to_u16(value
, "ns", &ns
);
4698 if (ns
>= OFPHTN_N_TYPES
) {
4699 return xasprintf("Unsupported ns value: %"PRIu16
, ns
);
4701 if (!ofputil_parse_key_value(&pos
, &key
, &value
)
4702 || strcmp(key
, "type") != 0) {
4703 return xstrdup("Missing packet_type attribute type");
4705 error
= str_to_u16(value
, "type", &type
);
4709 decap
->new_pkt_type
= htonl(PACKET_TYPE(ns
, type
));
4711 return xasprintf("Invalid decap argument: %s", key
);
4718 format_DECAP(const struct ofpact_decap
*a
,
4719 const struct ofpact_format_params
*fp
)
4721 ds_put_format(fp
->s
, "%sdecap(%s", colors
.paren
, colors
.end
);
4722 if (a
->new_pkt_type
!= htonl(PT_USE_NEXT_PROTO
)) {
4723 ds_put_format(fp
->s
, "packet_type(ns=%"PRIu16
",type=%#"PRIx16
")",
4724 pt_ns(a
->new_pkt_type
),
4725 pt_ns_type(a
->new_pkt_type
));
4727 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
4731 check_DECAP(const struct ofpact_decap
*a OVS_UNUSED
,
4732 struct ofpact_check_params
*cp
)
4734 struct flow
*flow
= &cp
->match
->flow
;
4735 if (flow
->packet_type
== htonl(PT_ETH
)) {
4736 /* Adjust the packet_type to allow subsequent actions. */
4737 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
4738 ntohs(flow
->dl_type
));
4740 /* The actual packet_type is only known after decapsulation.
4741 * Do not allow subsequent actions that depend on packet headers. */
4742 flow
->packet_type
= htonl(PT_UNKNOWN
);
4743 flow
->dl_type
= OVS_BE16_MAX
;
4748 /* Action dec_nsh_ttl */
4751 decode_NXAST_RAW_DEC_NSH_TTL(struct ofpbuf
*out
)
4753 ofpact_put_DEC_NSH_TTL(out
);
4758 encode_DEC_NSH_TTL(const struct ofpact_null
*null OVS_UNUSED
,
4759 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4761 put_NXAST_DEC_NSH_TTL(out
);
4764 static char * OVS_WARN_UNUSED_RESULT
4765 parse_DEC_NSH_TTL(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
4767 ofpact_put_DEC_NSH_TTL(pp
->ofpacts
);
4772 format_DEC_NSH_TTL(const struct ofpact_null
*a OVS_UNUSED
,
4773 const struct ofpact_format_params
*fp
)
4775 ds_put_format(fp
->s
, "%sdec_nsh_ttl%s", colors
.special
, colors
.end
);
4779 check_DEC_NSH_TTL(const struct ofpact_null
*a OVS_UNUSED
,
4780 struct ofpact_check_params
*cp
)
4782 struct flow
*flow
= &cp
->match
->flow
;
4783 if (flow
->packet_type
!= htonl(PT_NSH
) &&
4784 flow
->dl_type
!= htons(ETH_TYPE_NSH
)) {
4785 inconsistent_match(&cp
->usable_protocols
);
4790 /* Action structures for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE, and
4791 * NXAST_RESUBMIT_TABLE_CT.
4793 * These actions search one of the switch's flow tables:
4795 * - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the
4796 * 'table' member is not 255, then it specifies the table to search.
4798 * - Otherwise (for NXAST_RESUBMIT_TABLE or NXAST_RESUBMIT_TABLE_CT with a
4799 * 'table' of 255, or for NXAST_RESUBMIT regardless of 'table'), it
4800 * searches the current flow table, that is, the OpenFlow flow table that
4801 * contains the flow from which this action was obtained. If this action
4802 * did not come from a flow table (e.g. it came from an OFPT_PACKET_OUT
4803 * message), then table 0 is the current table.
4805 * The flow table lookup uses a flow that may be slightly modified from the
4808 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
4809 * is used as the flow's in_port.
4811 * - For NXAST_RESUBMIT_TABLE and NXAST_RESUBMIT_TABLE_CT, if the 'in_port'
4812 * member is not OFPP_IN_PORT, then its value is used as the flow's
4813 * in_port. Otherwise, the original in_port is used.
4815 * - For NXAST_RESUBMIT_TABLE_CT the Conntrack 5-tuple fields are used as
4816 * the packets IP header fields during the lookup.
4818 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
4819 * resubmit action, then the flow is updated with the new values.
4821 * Following the lookup, the original in_port is restored.
4823 * If the modified flow matched in the flow table, then the corresponding
4824 * actions are executed. Afterward, actions following the resubmit in the
4825 * original set of actions, if any, are executed; any changes made to the
4826 * packet (e.g. changes to VLAN) by secondary actions persist when those
4827 * actions are executed, although the original in_port is restored.
4829 * Resubmit actions may be used any number of times within a set of actions.
4831 * Resubmit actions may nest. To prevent infinite loops and excessive resource
4832 * use, the implementation may limit nesting depth and the total number of
4835 * - Open vSwitch 1.0.1 and earlier did not support recursion.
4837 * - Open vSwitch 1.0.2 and 1.0.3 limited recursion to 8 levels.
4839 * - Open vSwitch 1.1 and 1.2 limited recursion to 16 levels.
4841 * - Open vSwitch 1.2 through 1.8 limited recursion to 32 levels.
4843 * - Open vSwitch 1.9 through 2.0 limited recursion to 64 levels.
4845 * - Open vSwitch 2.1 through 2.5 limited recursion to 64 levels and impose
4846 * a total limit of 4,096 resubmits per flow translation (earlier versions
4847 * did not impose any total limit).
4849 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE and
4850 * NXAST_RESUBMIT_TABLE_CT require 'pad' to be all-bits-zero.
4852 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
4853 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE. Open vSwitch before
4854 * 2.8.0 did not support NXAST_RESUBMIT_TABLE_CT.
4856 struct nx_action_resubmit
{
4857 ovs_be16 type
; /* OFPAT_VENDOR. */
4858 ovs_be16 len
; /* Length is 16. */
4859 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4860 ovs_be16 subtype
; /* NXAST_RESUBMIT. */
4861 ovs_be16 in_port
; /* New in_port for checking flow table. */
4862 uint8_t table
; /* NXAST_RESUBMIT_TABLE: table to use. */
4865 OFP_ASSERT(sizeof(struct nx_action_resubmit
) == 16);
4868 decode_NXAST_RAW_RESUBMIT(uint16_t port
,
4869 enum ofp_version ofp_version OVS_UNUSED
,
4872 struct ofpact_resubmit
*resubmit
;
4874 resubmit
= ofpact_put_RESUBMIT(out
);
4875 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT
;
4876 resubmit
->in_port
= u16_to_ofp(port
);
4877 resubmit
->table_id
= 0xff;
4882 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit
*nar
,
4883 enum ofp_version ofp_version OVS_UNUSED
,
4886 struct ofpact_resubmit
*resubmit
;
4888 if (nar
->pad
[0] || nar
->pad
[1] || nar
->pad
[2]) {
4889 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4892 resubmit
= ofpact_put_RESUBMIT(out
);
4893 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT_TABLE
;
4894 resubmit
->in_port
= u16_to_ofp(ntohs(nar
->in_port
));
4895 resubmit
->table_id
= nar
->table
;
4900 decode_NXAST_RAW_RESUBMIT_TABLE_CT(const struct nx_action_resubmit
*nar
,
4901 enum ofp_version ofp_version OVS_UNUSED
,
4904 enum ofperr error
= decode_NXAST_RAW_RESUBMIT_TABLE(nar
, ofp_version
, out
);
4908 struct ofpact_resubmit
*resubmit
= out
->header
;
4909 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT_TABLE_CT
;
4910 resubmit
->with_ct_orig
= true;
4915 encode_RESUBMIT(const struct ofpact_resubmit
*resubmit
,
4916 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4918 uint16_t in_port
= ofp_to_u16(resubmit
->in_port
);
4920 if (resubmit
->table_id
== 0xff
4921 && resubmit
->ofpact
.raw
== NXAST_RAW_RESUBMIT
) {
4922 put_NXAST_RESUBMIT(out
, in_port
);
4924 struct nx_action_resubmit
*nar
;
4925 nar
= resubmit
->with_ct_orig
4926 ? put_NXAST_RESUBMIT_TABLE_CT(out
) : put_NXAST_RESUBMIT_TABLE(out
);
4927 nar
->table
= resubmit
->table_id
;
4928 nar
->in_port
= htons(in_port
);
4932 static char * OVS_WARN_UNUSED_RESULT
4933 parse_RESUBMIT(char *arg
, const struct ofpact_parse_params
*pp
)
4935 struct ofpact_resubmit
*resubmit
;
4936 char *in_port_s
, *table_s
, *ct_s
;
4938 resubmit
= ofpact_put_RESUBMIT(pp
->ofpacts
);
4940 in_port_s
= strsep(&arg
, ",");
4941 if (in_port_s
&& in_port_s
[0]) {
4942 if (!ofputil_port_from_string(in_port_s
, pp
->port_map
,
4943 &resubmit
->in_port
)) {
4944 return xasprintf("%s: resubmit to unknown port", in_port_s
);
4947 resubmit
->in_port
= OFPP_IN_PORT
;
4950 table_s
= strsep(&arg
, ",");
4951 if (table_s
&& table_s
[0]) {
4952 if (!ofputil_table_from_string(table_s
, pp
->table_map
,
4953 &resubmit
->table_id
)) {
4954 return xasprintf("%s: resubmit to unknown table", table_s
);
4957 resubmit
->table_id
= 255;
4960 ct_s
= strsep(&arg
, ",");
4961 if (ct_s
&& ct_s
[0]) {
4962 if (strcmp(ct_s
, "ct")) {
4963 return xasprintf("%s: unknown parameter", ct_s
);
4965 resubmit
->with_ct_orig
= true;
4967 resubmit
->with_ct_orig
= false;
4970 if (resubmit
->in_port
== OFPP_IN_PORT
&& resubmit
->table_id
== 255) {
4971 return xstrdup("at least one \"in_port\" or \"table\" must be "
4972 "specified on resubmit");
4978 format_RESUBMIT(const struct ofpact_resubmit
*a
,
4979 const struct ofpact_format_params
*fp
)
4981 if (a
->in_port
!= OFPP_IN_PORT
&& a
->table_id
== 255) {
4982 ds_put_format(fp
->s
, "%sresubmit:%s", colors
.special
, colors
.end
);
4983 ofputil_format_port(a
->in_port
, fp
->port_map
, fp
->s
);
4985 ds_put_format(fp
->s
, "%sresubmit(%s", colors
.paren
, colors
.end
);
4986 if (a
->in_port
!= OFPP_IN_PORT
) {
4987 ofputil_format_port(a
->in_port
, fp
->port_map
, fp
->s
);
4989 ds_put_char(fp
->s
, ',');
4990 if (a
->table_id
!= 255) {
4991 ofputil_format_table(a
->table_id
, fp
->table_map
, fp
->s
);
4993 if (a
->with_ct_orig
) {
4994 ds_put_cstr(fp
->s
, ",ct");
4996 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
5001 check_RESUBMIT(const struct ofpact_resubmit
*a
,
5002 const struct ofpact_check_params
*cp
)
5004 if (a
->with_ct_orig
&& !is_ct_valid(&cp
->match
->flow
, &cp
->match
->wc
,
5006 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
5011 /* Action structure for NXAST_LEARN and NXAST_LEARN2.
5013 * This action adds or modifies a flow in an OpenFlow table, similar to
5014 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
5015 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
5016 * flow's match criteria and actions are built by applying each of the series
5017 * of flow_mod_spec elements included as part of the action.
5019 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
5020 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
5021 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
5022 * bits from a source to a destination. In this case, the header contains
5025 * 15 14 13 12 11 10 0
5026 * +------+---+------+---------------------------------+
5027 * | 0 |src| dst | n_bits |
5028 * +------+---+------+---------------------------------+
5030 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
5031 * following table summarizes the meaning of each possible combination.
5032 * Details follow the table:
5035 * --- --- ----------------------------------------------------------
5036 * 0 0 Add match criteria based on value in a field.
5037 * 1 0 Add match criteria based on an immediate value.
5038 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
5039 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
5040 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
5041 * All other combinations are undefined and not allowed.
5043 * The flow_mod_spec header is followed by a source specification and a
5044 * destination specification. The format and meaning of the source
5045 * specification depends on 'src':
5047 * - If 'src' is 0, the source bits are taken from a field in the flow to
5048 * which this action is attached. (This should be a wildcarded field. If
5049 * its value is fully specified then the source bits being copied have
5052 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
5053 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
5054 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
5055 * 'field' and 'ofs' are subject to the same restrictions as the source
5056 * field in NXAST_REG_MOVE.
5058 * - If 'src' is 1, the source bits are a constant value. The source
5059 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
5060 * number in network order, the source bits are the 'n_bits'
5061 * least-significant bits. The switch will report an error if other bits
5062 * in the constant are nonzero.
5064 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
5065 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
5066 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
5067 * meaning of the flow_mod_spec depends on 'dst':
5069 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
5070 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
5071 * packet equal the source bits. 'field' may be any nxm_header with
5072 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
5074 * Order is significant. Earlier flow_mod_specs must satisfy any
5075 * prerequisites for matching fields specified later, by copying constant
5076 * values into prerequisite fields.
5078 * The switch will reject flow_mod_specs that do not satisfy NXM masking
5081 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
5082 * the new flow. The new flow copies the source bits into
5083 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
5086 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
5087 * greater than 64 yields multiple NXAST_REG_LOAD actions.
5089 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
5090 * empty. It has the following meaning:
5092 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
5093 * The new flow outputs to the OpenFlow port specified by the source field.
5094 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
5095 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
5098 * Resource Management
5099 * -------------------
5101 * A switch has a finite amount of flow table space available for learning.
5102 * When this space is exhausted, no new learning table entries will be learned
5103 * until some existing flow table entries expire. The controller should be
5104 * prepared to handle this by flooding (which can be implemented as a
5105 * low-priority flow).
5107 * If a learned flow matches a single TCP stream with a relatively long
5108 * timeout, one may make the best of resource constraints by setting
5109 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
5110 * both, to shorter timeouts. When either of these is specified as a nonzero
5111 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
5117 * The following examples give a prose description of the flow_mod_specs along
5118 * with informal notation for how those would be represented and a hex dump of
5119 * the bytes that would be required.
5121 * These examples could work with various nx_action_learn parameters. Typical
5122 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
5123 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
5125 * 1. Learn input port based on the source MAC, with lookup into
5126 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
5128 * Match on in_port=99:
5129 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
5130 * ovs_be16(99), 00 63
5131 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
5133 * Match Ethernet destination on Ethernet source from packet:
5134 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
5135 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
5136 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
5138 * Set NXM_NX_REG1[16:31] to the packet's input port:
5139 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
5140 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
5141 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
5143 * Given a packet that arrived on port A with Ethernet source address B,
5144 * this would set up the flow "in_port=99, dl_dst=B,
5145 * actions=load:A->NXM_NX_REG1[16..31]".
5147 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
5148 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
5149 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
5151 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
5152 * into NXM_NX_REG1[16:31]:
5154 * Match on same VLAN ID as packet:
5155 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
5156 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
5157 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
5159 * Match Ethernet destination on Ethernet source from packet:
5160 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
5161 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
5162 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
5164 * Output to the packet's input port:
5165 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
5166 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
5168 * Given a packet that arrived on port A with Ethernet source address B in
5169 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
5170 * actions=output:A".
5172 * In syntax accepted by ovs-ofctl, this action is:
5173 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
5174 * output:NXM_OF_IN_PORT[])
5176 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
5177 * 10-second MAC expiration time to make it easier to see what's going on
5179 * ovs-vsctl del-controller br0
5180 * ovs-ofctl del-flows br0
5181 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
5182 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
5183 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
5184 output:NXM_OF_IN_PORT[]), resubmit(,1)"
5185 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
5187 * You can then dump the MAC learning table with:
5189 * ovs-ofctl dump-flows br0 table=1
5194 * For best performance, segregate learned flows into a table that is not used
5195 * for any other flows except possibly for a lowest-priority "catch-all" flow
5196 * (a flow with no match criteria). If different learning actions specify
5197 * different match criteria, use different tables for the learned flows.
5199 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
5200 * These timeouts apply to the flow that is added, which means that a flow with
5201 * an idle timeout will expire when no traffic has been sent *to* the learned
5202 * address. This is not usually the intent in MAC learning; instead, we want
5203 * the MAC learn entry to expire when no traffic has been sent *from* the
5204 * learned address. Use a hard timeout for that.
5207 * Visibility of Changes
5208 * ---------------------
5210 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
5211 * flow translation are visible to flow table lookups made later in the flow
5212 * translation. This means that, in the example above, a MAC learned by the
5213 * learn action in table 0 would be found in table 1 (if the packet being
5214 * processed had the same source and destination MAC address).
5216 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
5217 * modify a flow) by a "learn" action are visible only for later flow
5218 * translations, not for later lookups within the same flow translation. In
5219 * the MAC learning example, a MAC learned by the learn action in table 0 would
5220 * not be found in table 1 if the flow translation would resubmit to table 1
5221 * after the processing of the learn action, meaning that if this MAC had not
5222 * been learned before then the packet would be flooded. */
5223 struct nx_action_learn
{
5224 ovs_be16 type
; /* OFPAT_VENDOR. */
5225 ovs_be16 len
; /* At least 24. */
5226 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5227 ovs_be16 subtype
; /* NXAST_LEARN. */
5228 ovs_be16 idle_timeout
; /* Idle time before discarding (seconds). */
5229 ovs_be16 hard_timeout
; /* Max time before discarding (seconds). */
5230 ovs_be16 priority
; /* Priority level of flow entry. */
5231 ovs_be64 cookie
; /* Cookie for new flow. */
5232 ovs_be16 flags
; /* NX_LEARN_F_*. */
5233 uint8_t table_id
; /* Table to insert flow entry. */
5234 uint8_t pad
; /* Must be zero. */
5235 ovs_be16 fin_idle_timeout
; /* Idle timeout after FIN, if nonzero. */
5236 ovs_be16 fin_hard_timeout
; /* Hard timeout after FIN, if nonzero. */
5237 /* Followed by a sequence of flow_mod_spec elements, as described above,
5238 * until the end of the action is reached. */
5240 OFP_ASSERT(sizeof(struct nx_action_learn
) == 32);
5242 struct nx_action_learn2
{
5243 struct nx_action_learn up
; /* The wire format includes nx_action_learn. */
5244 ovs_be32 limit
; /* Maximum number of learned flows.
5245 * 0 indicates unlimited. */
5247 /* Where to store the result. */
5248 ovs_be16 result_dst_ofs
; /* Starting bit offset in destination. */
5250 ovs_be16 pad2
; /* Must be zero. */
5252 * - OXM/NXM header for destination field (4 or 8 bytes),
5253 * if NX_LEARN_F_WRITE_RESULT is set in 'flags'
5254 * - a sequence of flow_mod_spec elements, as described above,
5255 * until the end of the action is reached. */
5257 OFP_ASSERT(sizeof(struct nx_action_learn2
) == 40);
5260 get_be16(const void **pp
)
5262 const ovs_be16
*p
= *pp
;
5263 ovs_be16 value
= *p
;
5269 get_be32(const void **pp
)
5271 const ovs_be32
*p
= *pp
;
5272 ovs_be32 value
= get_unaligned_be32(p
);
5278 get_subfield(int n_bits
, const void **p
, struct mf_subfield
*sf
,
5279 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
)
5283 error
= mf_vl_mff_mf_from_nxm_header(ntohl(get_be32(p
)), vl_mff_map
,
5284 &sf
->field
, tlv_bitmap
);
5285 sf
->ofs
= ntohs(get_be16(p
));
5286 sf
->n_bits
= n_bits
;
5291 learn_min_len(uint16_t header
)
5293 int n_bits
= header
& NX_LEARN_N_BITS_MASK
;
5294 int src_type
= header
& NX_LEARN_SRC_MASK
;
5295 int dst_type
= header
& NX_LEARN_DST_MASK
;
5296 unsigned int min_len
;
5299 if (src_type
== NX_LEARN_SRC_FIELD
) {
5300 min_len
+= sizeof(ovs_be32
); /* src_field */
5301 min_len
+= sizeof(ovs_be16
); /* src_ofs */
5303 min_len
+= 2 * DIV_ROUND_UP(n_bits
, 16);
5305 if (dst_type
== NX_LEARN_DST_MATCH
||
5306 dst_type
== NX_LEARN_DST_LOAD
) {
5307 min_len
+= sizeof(ovs_be32
); /* dst_field */
5308 min_len
+= sizeof(ovs_be16
); /* dst_ofs */
5314 decode_LEARN_common(const struct nx_action_learn
*nal
,
5315 enum ofp_raw_action_type raw
,
5316 struct ofpact_learn
*learn
)
5319 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5322 learn
->ofpact
.raw
= raw
;
5323 learn
->idle_timeout
= ntohs(nal
->idle_timeout
);
5324 learn
->hard_timeout
= ntohs(nal
->hard_timeout
);
5325 learn
->priority
= ntohs(nal
->priority
);
5326 learn
->cookie
= nal
->cookie
;
5327 learn
->table_id
= nal
->table_id
;
5328 learn
->fin_idle_timeout
= ntohs(nal
->fin_idle_timeout
);
5329 learn
->fin_hard_timeout
= ntohs(nal
->fin_hard_timeout
);
5330 learn
->flags
= ntohs(nal
->flags
);
5332 if (learn
->table_id
== 0xff) {
5333 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5340 decode_LEARN_specs(const void *p
, const void *end
,
5341 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
5342 struct ofpbuf
*ofpacts
)
5344 struct ofpact_learn
*learn
= ofpacts
->header
;
5347 struct ofpact_learn_spec
*spec
;
5348 uint16_t header
= ntohs(get_be16(&p
));
5354 spec
= ofpbuf_put_zeros(ofpacts
, sizeof *spec
);
5355 learn
= ofpacts
->header
;
5357 spec
->src_type
= header
& NX_LEARN_SRC_MASK
;
5358 spec
->dst_type
= header
& NX_LEARN_DST_MASK
;
5359 spec
->n_bits
= header
& NX_LEARN_N_BITS_MASK
;
5361 /* Check for valid src and dst type combination. */
5362 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
5363 spec
->dst_type
== NX_LEARN_DST_LOAD
||
5364 (spec
->dst_type
== NX_LEARN_DST_OUTPUT
&&
5365 spec
->src_type
== NX_LEARN_SRC_FIELD
)) {
5368 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5371 /* Check that the arguments don't overrun the end of the action. */
5372 if ((char *) end
- (char *) p
< learn_min_len(header
)) {
5373 return OFPERR_OFPBAC_BAD_LEN
;
5376 /* Get the source. */
5377 const uint8_t *imm
= NULL
;
5378 unsigned int imm_bytes
= 0;
5380 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
5381 error
= get_subfield(spec
->n_bits
, &p
, &spec
->src
, vl_mff_map
,
5387 int p_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
5388 p
= (const uint8_t *) p
+ p_bytes
;
5390 imm_bytes
= DIV_ROUND_UP(spec
->n_bits
, 8);
5391 imm
= (const uint8_t *) p
- imm_bytes
;
5394 /* Get the destination. */
5395 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
5396 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
5397 error
= get_subfield(spec
->n_bits
, &p
, &spec
->dst
, vl_mff_map
,
5405 uint8_t *src_imm
= ofpbuf_put_zeros(ofpacts
,
5406 OFPACT_ALIGN(imm_bytes
));
5407 memcpy(src_imm
, imm
, imm_bytes
);
5409 learn
= ofpacts
->header
;
5412 ofpact_finish_LEARN(ofpacts
, &learn
);
5414 if (!is_all_zeros(p
, (char *) end
- (char *) p
)) {
5415 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5421 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
5422 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
5424 decode_NXAST_RAW_LEARN(const struct nx_action_learn
*nal
,
5425 enum ofp_version ofp_version OVS_UNUSED
,
5426 const struct vl_mff_map
*vl_mff_map
,
5427 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
5429 struct ofpact_learn
*learn
;
5432 learn
= ofpact_put_LEARN(ofpacts
);
5434 error
= decode_LEARN_common(nal
, NXAST_RAW_LEARN
, learn
);
5439 if (learn
->flags
& ~(NX_LEARN_F_SEND_FLOW_REM
|
5440 NX_LEARN_F_DELETE_LEARNED
)) {
5441 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5444 return decode_LEARN_specs(nal
+ 1, (char *) nal
+ ntohs(nal
->len
),
5445 vl_mff_map
, tlv_bitmap
, ofpacts
);
5448 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
5449 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
5451 decode_NXAST_RAW_LEARN2(const struct nx_action_learn2
*nal
,
5452 enum ofp_version ofp_version OVS_UNUSED
,
5453 const struct vl_mff_map
*vl_mff_map
,
5454 uint64_t *tlv_bitmap
, struct ofpbuf
*ofpacts
)
5456 struct ofpbuf b
= ofpbuf_const_initializer(nal
, ntohs(nal
->up
.len
));
5457 struct ofpact_learn
*learn
;
5461 return OFPERR_NXBAC_MUST_BE_ZERO
;
5464 learn
= ofpact_put_LEARN(ofpacts
);
5465 error
= decode_LEARN_common(&nal
->up
, NXAST_RAW_LEARN2
, learn
);
5470 learn
->limit
= ntohl(nal
->limit
);
5472 if (learn
->flags
& ~(NX_LEARN_F_SEND_FLOW_REM
|
5473 NX_LEARN_F_DELETE_LEARNED
|
5474 NX_LEARN_F_WRITE_RESULT
)) {
5475 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5478 ofpbuf_pull(&b
, sizeof *nal
);
5480 if (learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
5481 error
= nx_pull_header(&b
, vl_mff_map
, &learn
->result_dst
.field
, NULL
);
5485 if (!learn
->result_dst
.field
->writable
) {
5486 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
5488 learn
->result_dst
.ofs
= ntohs(nal
->result_dst_ofs
);
5489 learn
->result_dst
.n_bits
= 1;
5490 } else if (nal
->result_dst_ofs
) {
5491 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5494 return decode_LEARN_specs(b
.data
, (char *) nal
+ ntohs(nal
->up
.len
),
5495 vl_mff_map
, tlv_bitmap
, ofpacts
);
5499 put_be16(struct ofpbuf
*b
, ovs_be16 x
)
5501 ofpbuf_put(b
, &x
, sizeof x
);
5505 put_be32(struct ofpbuf
*b
, ovs_be32 x
)
5507 ofpbuf_put(b
, &x
, sizeof x
);
5511 put_u16(struct ofpbuf
*b
, uint16_t x
)
5513 put_be16(b
, htons(x
));
5517 put_u32(struct ofpbuf
*b
, uint32_t x
)
5519 put_be32(b
, htonl(x
));
5523 encode_LEARN(const struct ofpact_learn
*learn
,
5524 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
5526 const struct ofpact_learn_spec
*spec
;
5527 struct nx_action_learn
*nal
;
5530 start_ofs
= out
->size
;
5532 if (learn
->ofpact
.raw
== NXAST_RAW_LEARN2
5533 || learn
->limit
!= 0
5534 || learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
5535 struct nx_action_learn2
*nal2
;
5537 nal2
= put_NXAST_LEARN2(out
);
5538 nal2
->limit
= htonl(learn
->limit
);
5539 nal2
->result_dst_ofs
= htons(learn
->result_dst
.ofs
);
5542 nal
= put_NXAST_LEARN(out
);
5544 nal
->idle_timeout
= htons(learn
->idle_timeout
);
5545 nal
->hard_timeout
= htons(learn
->hard_timeout
);
5546 nal
->fin_idle_timeout
= htons(learn
->fin_idle_timeout
);
5547 nal
->fin_hard_timeout
= htons(learn
->fin_hard_timeout
);
5548 nal
->priority
= htons(learn
->priority
);
5549 nal
->cookie
= learn
->cookie
;
5550 nal
->flags
= htons(learn
->flags
);
5551 nal
->table_id
= learn
->table_id
;
5553 if (learn
->flags
& NX_LEARN_F_WRITE_RESULT
) {
5554 nx_put_header(out
, learn
->result_dst
.field
->id
, 0, false);
5557 OFPACT_LEARN_SPEC_FOR_EACH (spec
, learn
) {
5558 put_u16(out
, spec
->n_bits
| spec
->dst_type
| spec
->src_type
);
5560 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
5561 put_u32(out
, nxm_header_from_mff(spec
->src
.field
));
5562 put_u16(out
, spec
->src
.ofs
);
5564 size_t n_dst_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
5565 uint8_t *bits
= ofpbuf_put_zeros(out
, n_dst_bytes
);
5566 unsigned int n_bytes
= DIV_ROUND_UP(spec
->n_bits
, 8);
5568 memcpy(bits
+ n_dst_bytes
- n_bytes
, ofpact_learn_spec_imm(spec
),
5572 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
5573 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
5574 put_u32(out
, nxm_header_from_mff(spec
->dst
.field
));
5575 put_u16(out
, spec
->dst
.ofs
);
5579 pad_ofpat(out
, start_ofs
);
5582 static char * OVS_WARN_UNUSED_RESULT
5583 parse_LEARN(char *arg
, const struct ofpact_parse_params
*pp
)
5585 return learn_parse(arg
, pp
->port_map
, pp
->table_map
, pp
->ofpacts
);
5589 format_LEARN(const struct ofpact_learn
*a
,
5590 const struct ofpact_format_params
*fp
)
5592 learn_format(a
, fp
->port_map
, fp
->table_map
, fp
->s
);
5596 check_LEARN(const struct ofpact_learn
*a
,
5597 const struct ofpact_check_params
*cp
)
5599 return learn_check(a
, cp
->match
);
5602 /* Action structure for NXAST_CONJUNCTION. */
5603 struct nx_action_conjunction
{
5604 ovs_be16 type
; /* OFPAT_VENDOR. */
5605 ovs_be16 len
; /* At least 16. */
5606 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5607 ovs_be16 subtype
; /* See enum ofp_raw_action_type. */
5612 OFP_ASSERT(sizeof(struct nx_action_conjunction
) == 16);
5615 add_conjunction(struct ofpbuf
*out
,
5616 uint32_t id
, uint8_t clause
, uint8_t n_clauses
)
5618 struct ofpact_conjunction
*oc
;
5620 oc
= ofpact_put_CONJUNCTION(out
);
5622 oc
->clause
= clause
;
5623 oc
->n_clauses
= n_clauses
;
5627 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction
*nac
,
5628 enum ofp_version ofp_version OVS_UNUSED
,
5631 if (nac
->n_clauses
< 2 || nac
->n_clauses
> 64
5632 || nac
->clause
>= nac
->n_clauses
) {
5633 return OFPERR_NXBAC_BAD_CONJUNCTION
;
5635 add_conjunction(out
, ntohl(nac
->id
), nac
->clause
, nac
->n_clauses
);
5641 encode_CONJUNCTION(const struct ofpact_conjunction
*oc
,
5642 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
5644 struct nx_action_conjunction
*nac
= put_NXAST_CONJUNCTION(out
);
5645 nac
->clause
= oc
->clause
;
5646 nac
->n_clauses
= oc
->n_clauses
;
5647 nac
->id
= htonl(oc
->id
);
5651 format_CONJUNCTION(const struct ofpact_conjunction
*oc
,
5652 const struct ofpact_format_params
*fp
)
5654 ds_put_format(fp
->s
, "%sconjunction(%s%"PRIu32
",%d/%"PRIu8
"%s)%s",
5655 colors
.paren
, colors
.end
,
5656 oc
->id
, oc
->clause
+ 1, oc
->n_clauses
,
5657 colors
.paren
, colors
.end
);
5660 static char * OVS_WARN_UNUSED_RESULT
5661 parse_CONJUNCTION(const char *arg
, const struct ofpact_parse_params
*pp
)
5668 if (!ovs_scan(arg
, "%"SCNi32
" , %"SCNu8
" / %"SCNu8
" %n",
5669 &id
, &clause
, &n_clauses
, &n
) || n
!= strlen(arg
)) {
5670 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
5673 if (n_clauses
< 2) {
5674 return xstrdup("conjunction must have at least 2 clauses");
5675 } else if (n_clauses
> 64) {
5676 return xstrdup("conjunction must have at most 64 clauses");
5677 } else if (clause
< 1) {
5678 return xstrdup("clause index must be positive");
5679 } else if (clause
> n_clauses
) {
5680 return xstrdup("clause index must be less than or equal to "
5681 "number of clauses");
5684 add_conjunction(pp
->ofpacts
, id
, clause
- 1, n_clauses
);
5689 check_CONJUNCTION(const struct ofpact_conjunction
*a OVS_UNUSED
,
5690 const struct ofpact_check_params
*cp OVS_UNUSED
)
5695 /* Action structure for NXAST_MULTIPATH.
5697 * This action performs the following steps in sequence:
5699 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
5700 * Refer to the definition of "enum nx_mp_fields" for details.
5702 * The 'basis' value is used as a universal hash parameter, that is,
5703 * different values of 'basis' yield different hash functions. The
5704 * particular universal hash function used is implementation-defined.
5706 * The hashed fields' values are drawn from the current state of the
5707 * flow, including all modifications that have been made by actions up to
5710 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
5711 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
5714 * The output of the algorithm is 'link', an unsigned integer less than
5715 * or equal to 'max_link'.
5717 * Some algorithms use 'arg' as an additional argument.
5719 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
5720 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
5723 * The switch will reject actions that have an unknown 'fields', or an unknown
5724 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
5725 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
5726 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
5728 struct nx_action_multipath
{
5729 ovs_be16 type
; /* OFPAT_VENDOR. */
5730 ovs_be16 len
; /* Length is 32. */
5731 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5732 ovs_be16 subtype
; /* NXAST_MULTIPATH. */
5734 /* What fields to hash and how. */
5735 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
5736 ovs_be16 basis
; /* Universal hash parameter. */
5739 /* Multipath link choice algorithm to apply to hash value. */
5740 ovs_be16 algorithm
; /* One of NX_MP_ALG_*. */
5741 ovs_be16 max_link
; /* Number of output links, minus 1. */
5742 ovs_be32 arg
; /* Algorithm-specific argument. */
5745 /* Where to store the result. */
5746 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
5747 ovs_be32 dst
; /* Destination. */
5749 OFP_ASSERT(sizeof(struct nx_action_multipath
) == 32);
5752 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath
*nam
,
5753 enum ofp_version ofp_version OVS_UNUSED
,
5754 const struct vl_mff_map
*vl_mff_map
,
5755 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
5757 uint32_t n_links
= ntohs(nam
->max_link
) + 1;
5758 size_t min_n_bits
= log_2_ceil(n_links
);
5759 struct ofpact_multipath
*mp
;
5762 mp
= ofpact_put_MULTIPATH(out
);
5763 mp
->fields
= ntohs(nam
->fields
);
5764 mp
->basis
= ntohs(nam
->basis
);
5765 mp
->algorithm
= ntohs(nam
->algorithm
);
5766 mp
->max_link
= ntohs(nam
->max_link
);
5767 mp
->arg
= ntohl(nam
->arg
);
5768 mp
->dst
.ofs
= nxm_decode_ofs(nam
->ofs_nbits
);
5769 mp
->dst
.n_bits
= nxm_decode_n_bits(nam
->ofs_nbits
);
5770 error
= mf_vl_mff_mf_from_nxm_header(ntohl(nam
->dst
), vl_mff_map
,
5771 &mp
->dst
.field
, tlv_bitmap
);
5776 if (!flow_hash_fields_valid(mp
->fields
)) {
5777 VLOG_WARN_RL(&rl
, "unsupported fields %d", (int) mp
->fields
);
5778 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5779 } else if (mp
->algorithm
!= NX_MP_ALG_MODULO_N
5780 && mp
->algorithm
!= NX_MP_ALG_HASH_THRESHOLD
5781 && mp
->algorithm
!= NX_MP_ALG_HRW
5782 && mp
->algorithm
!= NX_MP_ALG_ITER_HASH
) {
5783 VLOG_WARN_RL(&rl
, "unsupported algorithm %d", (int) mp
->algorithm
);
5784 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5785 } else if (mp
->dst
.n_bits
< min_n_bits
) {
5786 VLOG_WARN_RL(&rl
, "multipath action requires at least %"PRIuSIZE
" bits for "
5787 "%"PRIu32
" links", min_n_bits
, n_links
);
5788 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5791 return multipath_check(mp
, NULL
);
5795 encode_MULTIPATH(const struct ofpact_multipath
*mp
,
5796 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
5798 struct nx_action_multipath
*nam
= put_NXAST_MULTIPATH(out
);
5800 nam
->fields
= htons(mp
->fields
);
5801 nam
->basis
= htons(mp
->basis
);
5802 nam
->algorithm
= htons(mp
->algorithm
);
5803 nam
->max_link
= htons(mp
->max_link
);
5804 nam
->arg
= htonl(mp
->arg
);
5805 nam
->ofs_nbits
= nxm_encode_ofs_nbits(mp
->dst
.ofs
, mp
->dst
.n_bits
);
5806 nam
->dst
= htonl(nxm_header_from_mff(mp
->dst
.field
));
5809 static char * OVS_WARN_UNUSED_RESULT
5810 parse_MULTIPATH(const char *arg
, const struct ofpact_parse_params
*pp
)
5812 return multipath_parse(ofpact_put_MULTIPATH(pp
->ofpacts
), arg
);
5816 format_MULTIPATH(const struct ofpact_multipath
*a
,
5817 const struct ofpact_format_params
*fp
)
5819 multipath_format(a
, fp
->s
);
5823 check_MULTIPATH(const struct ofpact_multipath
*a
,
5824 const struct ofpact_check_params
*cp
)
5826 return multipath_check(a
, cp
->match
);
5829 /* Action structure for NXAST_NOTE.
5831 * This action has no effect. It is variable length. The switch does not
5832 * attempt to interpret the user-defined 'note' data in any way. A controller
5833 * can use this action to attach arbitrary metadata to a flow.
5835 * This action might go away in the future.
5837 struct nx_action_note
{
5838 ovs_be16 type
; /* OFPAT_VENDOR. */
5839 ovs_be16 len
; /* A multiple of 8, but at least 16. */
5840 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5841 ovs_be16 subtype
; /* NXAST_NOTE. */
5842 uint8_t note
[6]; /* Start of user-defined data. */
5843 /* Possibly followed by additional user-defined data. */
5845 OFP_ASSERT(sizeof(struct nx_action_note
) == 16);
5848 decode_NXAST_RAW_NOTE(const struct nx_action_note
*nan
,
5849 enum ofp_version ofp_version OVS_UNUSED
,
5852 struct ofpact_note
*note
;
5853 unsigned int length
;
5855 length
= ntohs(nan
->len
) - offsetof(struct nx_action_note
, note
);
5856 note
= ofpact_put_NOTE(out
);
5857 note
->length
= length
;
5858 ofpbuf_put(out
, nan
->note
, length
);
5860 ofpact_finish_NOTE(out
, ¬e
);
5866 encode_NOTE(const struct ofpact_note
*note
,
5867 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
5869 size_t start_ofs
= out
->size
;
5870 struct nx_action_note
*nan
;
5872 put_NXAST_NOTE(out
);
5873 out
->size
= out
->size
- sizeof nan
->note
;
5875 ofpbuf_put(out
, note
->data
, note
->length
);
5876 pad_ofpat(out
, start_ofs
);
5879 static char * OVS_WARN_UNUSED_RESULT
5880 parse_NOTE(const char *arg
, const struct ofpact_parse_params
*pp
)
5882 size_t start_ofs
= pp
->ofpacts
->size
;
5883 ofpact_put_NOTE(pp
->ofpacts
);
5884 arg
= ofpbuf_put_hex(pp
->ofpacts
, arg
, NULL
);
5886 return xstrdup("bad hex digit in `note' argument");
5888 struct ofpact_note
*note
= ofpbuf_at_assert(pp
->ofpacts
, start_ofs
,
5890 note
->length
= pp
->ofpacts
->size
- (start_ofs
+ sizeof *note
);
5892 if (ofpbuf_oversized(pp
->ofpacts
)) {
5893 return xasprintf("input too big");
5896 ofpact_finish_NOTE(pp
->ofpacts
, ¬e
);
5901 format_NOTE(const struct ofpact_note
*a
,
5902 const struct ofpact_format_params
*fp
)
5904 ds_put_format(fp
->s
, "%snote:%s", colors
.param
, colors
.end
);
5905 format_hex_arg(fp
->s
, a
->data
, a
->length
);
5909 check_NOTE(const struct ofpact_note
*a OVS_UNUSED
,
5910 const struct ofpact_check_params
*cp OVS_UNUSED
)
5918 decode_NXAST_RAW_EXIT(struct ofpbuf
*out
)
5920 ofpact_put_EXIT(out
);
5925 encode_EXIT(const struct ofpact_null
*null OVS_UNUSED
,
5926 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
5928 put_NXAST_EXIT(out
);
5931 static char * OVS_WARN_UNUSED_RESULT
5932 parse_EXIT(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
5934 ofpact_put_EXIT(pp
->ofpacts
);
5939 format_EXIT(const struct ofpact_null
*a OVS_UNUSED
,
5940 const struct ofpact_format_params
*fp
)
5942 ds_put_format(fp
->s
, "%sexit%s", colors
.special
, colors
.end
);
5946 check_EXIT(const struct ofpact_null
*a OVS_UNUSED
,
5947 const struct ofpact_check_params
*cp OVS_UNUSED
)
5952 /* Unroll xlate action. */
5955 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate
*unroll OVS_UNUSED
,
5956 enum ofp_version ofp_version OVS_UNUSED
,
5957 struct ofpbuf
*out OVS_UNUSED
)
5962 static char * OVS_WARN_UNUSED_RESULT
5963 parse_UNROLL_XLATE(char *arg OVS_UNUSED
,
5964 const struct ofpact_parse_params
*pp OVS_UNUSED
)
5966 return xasprintf("UNROLL is an internal action "
5967 "that shouldn't be used via OpenFlow");
5971 format_UNROLL_XLATE(const struct ofpact_unroll_xlate
*a
,
5972 const struct ofpact_format_params
*fp
)
5974 ds_put_format(fp
->s
, "%sunroll_xlate(%s%stable=%s",
5975 colors
.paren
, colors
.end
,
5976 colors
.special
, colors
.end
);
5977 ofputil_format_table(a
->rule_table_id
, fp
->table_map
, fp
->s
);
5978 ds_put_format(fp
->s
, ", %scookie=%s%"PRIu64
"%s)%s",
5979 colors
.param
, colors
.end
, ntohll(a
->rule_cookie
),
5980 colors
.paren
, colors
.end
);
5984 check_UNROLL_XLATE(const struct ofpact_unroll_xlate
*a OVS_UNUSED
,
5985 const struct ofpact_check_params
*cp OVS_UNUSED
)
5987 /* UNROLL is an internal action that should never be seen via OpenFlow. */
5988 return OFPERR_OFPBAC_BAD_TYPE
;
5991 /* The NXAST_CLONE action is "struct ext_action_header", followed by zero or
5992 * more embedded OpenFlow actions. */
5995 decode_NXAST_RAW_CLONE(const struct ext_action_header
*eah
,
5996 enum ofp_version ofp_version
,
5997 const struct vl_mff_map
*vl_mff_map
,
5998 uint64_t *tlv_bitmap
, struct ofpbuf
*out
)
6001 struct ofpbuf openflow
;
6002 const size_t clone_offset
= ofpacts_pull(out
);
6003 struct ofpact_nest
*clone
= ofpact_put_CLONE(out
);
6005 /* decode action list */
6006 ofpbuf_pull(out
, sizeof(*clone
));
6007 openflow
= ofpbuf_const_initializer(
6008 eah
+ 1, ntohs(eah
->len
) - sizeof *eah
);
6009 error
= ofpacts_pull_openflow_actions__(&openflow
, openflow
.size
,
6011 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
6012 out
, 0, vl_mff_map
, tlv_bitmap
);
6016 clone
= ofpbuf_push_uninit(out
, sizeof *clone
);
6017 out
->header
= &clone
->ofpact
;
6018 ofpact_finish_CLONE(out
, &clone
);
6019 ofpbuf_push_uninit(out
, clone_offset
);
6024 encode_CLONE(const struct ofpact_nest
*clone
,
6025 enum ofp_version ofp_version
, struct ofpbuf
*out
)
6028 const size_t ofs
= out
->size
;
6029 struct ext_action_header
*eah
;
6031 put_NXAST_CLONE(out
);
6032 len
= ofpacts_put_openflow_actions(clone
->actions
,
6033 ofpact_nest_get_action_len(clone
),
6036 eah
= ofpbuf_at(out
, ofs
, sizeof *eah
);
6037 eah
->len
= htons(len
);
6040 static char * OVS_WARN_UNUSED_RESULT
6041 parse_CLONE(char *arg
, const struct ofpact_parse_params
*pp
)
6043 const size_t clone_offset
= ofpacts_pull(pp
->ofpacts
);
6044 struct ofpact_nest
*clone
= ofpact_put_CLONE(pp
->ofpacts
);
6047 ofpbuf_pull(pp
->ofpacts
, sizeof *clone
);
6048 error
= ofpacts_parse_copy(arg
, pp
, false, OFPACT_CLONE
);
6049 /* header points to the action list */
6050 pp
->ofpacts
->header
= ofpbuf_push_uninit(pp
->ofpacts
, sizeof *clone
);
6051 clone
= pp
->ofpacts
->header
;
6053 if (ofpbuf_oversized(pp
->ofpacts
)) {
6055 return xasprintf("input too big");
6058 ofpact_finish_CLONE(pp
->ofpacts
, &clone
);
6059 ofpbuf_push_uninit(pp
->ofpacts
, clone_offset
);
6064 format_CLONE(const struct ofpact_nest
*a
,
6065 const struct ofpact_format_params
*fp
)
6067 ds_put_format(fp
->s
, "%sclone(%s", colors
.paren
, colors
.end
);
6068 ofpacts_format(a
->actions
, ofpact_nest_get_action_len(a
), fp
);
6069 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
6073 check_subactions(struct ofpact
*ofpacts
, size_t ofpacts_len
,
6074 struct ofpact_check_params
*cp
)
6076 struct ofpact_check_params sub
= *cp
;
6077 enum ofperr error
= ofpacts_check(ofpacts
, ofpacts_len
, &sub
);
6078 cp
->usable_protocols
&= sub
.usable_protocols
;
6083 check_CLONE(struct ofpact_nest
*a
, struct ofpact_check_params
*cp
)
6085 return check_subactions(a
->actions
, ofpact_nest_get_action_len(a
), cp
);
6088 /* Action structure for NXAST_SAMPLE.
6090 * Samples matching packets with the given probability and sends them
6091 * each to the set of collectors identified with the given ID. The
6092 * probability is expressed as a number of packets to be sampled out
6093 * of USHRT_MAX packets, and must be >0.
6095 * When sending packet samples to IPFIX collectors, the IPFIX flow
6096 * record sent for each sampled packet is associated with the given
6097 * observation domain ID and observation point ID. Each IPFIX flow
6098 * record contain the sampled packet's headers when executing this
6099 * rule. If a sampled packet's headers are modified by previous
6100 * actions in the flow, those modified headers are sent. */
6101 struct nx_action_sample
{
6102 ovs_be16 type
; /* OFPAT_VENDOR. */
6103 ovs_be16 len
; /* Length is 24. */
6104 ovs_be32 vendor
; /* NX_VENDOR_ID. */
6105 ovs_be16 subtype
; /* NXAST_SAMPLE. */
6106 ovs_be16 probability
; /* Fraction of packets to sample. */
6107 ovs_be32 collector_set_id
; /* ID of collector set in OVSDB. */
6108 ovs_be32 obs_domain_id
; /* ID of sampling observation domain. */
6109 ovs_be32 obs_point_id
; /* ID of sampling observation point. */
6111 OFP_ASSERT(sizeof(struct nx_action_sample
) == 24);
6113 /* Action structure for NXAST_SAMPLE2 and NXAST_SAMPLE3.
6115 * NXAST_SAMPLE2 was added in Open vSwitch 2.5.90. Compared to NXAST_SAMPLE,
6116 * it adds support for exporting egress tunnel information.
6118 * NXAST_SAMPLE3 was added in Open vSwitch 2.6.90. Compared to NXAST_SAMPLE2,
6119 * it adds support for the 'direction' field. */
6120 struct nx_action_sample2
{
6121 ovs_be16 type
; /* OFPAT_VENDOR. */
6122 ovs_be16 len
; /* Length is 32. */
6123 ovs_be32 vendor
; /* NX_VENDOR_ID. */
6124 ovs_be16 subtype
; /* NXAST_SAMPLE. */
6125 ovs_be16 probability
; /* Fraction of packets to sample. */
6126 ovs_be32 collector_set_id
; /* ID of collector set in OVSDB. */
6127 ovs_be32 obs_domain_id
; /* ID of sampling observation domain. */
6128 ovs_be32 obs_point_id
; /* ID of sampling observation point. */
6129 ovs_be16 sampling_port
; /* Sampling port. */
6130 uint8_t direction
; /* NXAST_SAMPLE3 only. */
6131 uint8_t zeros
[5]; /* Pad to a multiple of 8 bytes */
6133 OFP_ASSERT(sizeof(struct nx_action_sample2
) == 32);
6136 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample
*nas
,
6137 enum ofp_version ofp_version OVS_UNUSED
,
6140 struct ofpact_sample
*sample
;
6142 sample
= ofpact_put_SAMPLE(out
);
6143 sample
->ofpact
.raw
= NXAST_RAW_SAMPLE
;
6144 sample
->probability
= ntohs(nas
->probability
);
6145 sample
->collector_set_id
= ntohl(nas
->collector_set_id
);
6146 sample
->obs_domain_id
= ntohl(nas
->obs_domain_id
);
6147 sample
->obs_point_id
= ntohl(nas
->obs_point_id
);
6148 sample
->sampling_port
= OFPP_NONE
;
6149 sample
->direction
= NX_ACTION_SAMPLE_DEFAULT
;
6151 if (sample
->probability
== 0) {
6152 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6159 decode_SAMPLE2(const struct nx_action_sample2
*nas
,
6160 enum ofp_raw_action_type raw
,
6161 enum nx_action_sample_direction direction
,
6162 struct ofpact_sample
*sample
)
6164 sample
->ofpact
.raw
= raw
;
6165 sample
->probability
= ntohs(nas
->probability
);
6166 sample
->collector_set_id
= ntohl(nas
->collector_set_id
);
6167 sample
->obs_domain_id
= ntohl(nas
->obs_domain_id
);
6168 sample
->obs_point_id
= ntohl(nas
->obs_point_id
);
6169 sample
->sampling_port
= u16_to_ofp(ntohs(nas
->sampling_port
));
6170 sample
->direction
= direction
;
6172 if (sample
->probability
== 0) {
6173 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6180 decode_NXAST_RAW_SAMPLE2(const struct nx_action_sample2
*nas
,
6181 enum ofp_version ofp_version OVS_UNUSED
,
6184 return decode_SAMPLE2(nas
, NXAST_RAW_SAMPLE2
, NX_ACTION_SAMPLE_DEFAULT
,
6185 ofpact_put_SAMPLE(out
));
6189 decode_NXAST_RAW_SAMPLE3(const struct nx_action_sample2
*nas
,
6190 enum ofp_version ofp_version OVS_UNUSED
,
6193 struct ofpact_sample
*sample
= ofpact_put_SAMPLE(out
);
6194 if (!is_all_zeros(nas
->zeros
, sizeof nas
->zeros
)) {
6195 return OFPERR_NXBRC_MUST_BE_ZERO
;
6197 if (nas
->direction
!= NX_ACTION_SAMPLE_DEFAULT
&&
6198 nas
->direction
!= NX_ACTION_SAMPLE_INGRESS
&&
6199 nas
->direction
!= NX_ACTION_SAMPLE_EGRESS
) {
6200 VLOG_WARN_RL(&rl
, "invalid sample direction %"PRIu8
, nas
->direction
);
6201 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6203 return decode_SAMPLE2(nas
, NXAST_RAW_SAMPLE3
, nas
->direction
, sample
);
6207 encode_SAMPLE2(const struct ofpact_sample
*sample
,
6208 struct nx_action_sample2
*nas
)
6210 nas
->probability
= htons(sample
->probability
);
6211 nas
->collector_set_id
= htonl(sample
->collector_set_id
);
6212 nas
->obs_domain_id
= htonl(sample
->obs_domain_id
);
6213 nas
->obs_point_id
= htonl(sample
->obs_point_id
);
6214 nas
->sampling_port
= htons(ofp_to_u16(sample
->sampling_port
));
6215 nas
->direction
= sample
->direction
;
6219 encode_SAMPLE(const struct ofpact_sample
*sample
,
6220 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
6222 if (sample
->ofpact
.raw
== NXAST_RAW_SAMPLE3
6223 || sample
->direction
!= NX_ACTION_SAMPLE_DEFAULT
) {
6224 encode_SAMPLE2(sample
, put_NXAST_SAMPLE3(out
));
6225 } else if (sample
->ofpact
.raw
== NXAST_RAW_SAMPLE2
6226 || sample
->sampling_port
!= OFPP_NONE
) {
6227 encode_SAMPLE2(sample
, put_NXAST_SAMPLE2(out
));
6229 struct nx_action_sample
*nas
= put_NXAST_SAMPLE(out
);
6230 nas
->probability
= htons(sample
->probability
);
6231 nas
->collector_set_id
= htonl(sample
->collector_set_id
);
6232 nas
->obs_domain_id
= htonl(sample
->obs_domain_id
);
6233 nas
->obs_point_id
= htonl(sample
->obs_point_id
);
6237 /* Parses 'arg' as the argument to a "sample" action, and appends such an
6238 * action to 'pp->ofpacts'.
6240 * Returns NULL if successful, otherwise a malloc()'d string describing the
6241 * error. The caller is responsible for freeing the returned string. */
6242 static char * OVS_WARN_UNUSED_RESULT
6243 parse_SAMPLE(char *arg
, const struct ofpact_parse_params
*pp
)
6245 struct ofpact_sample
*os
= ofpact_put_SAMPLE(pp
->ofpacts
);
6246 os
->sampling_port
= OFPP_NONE
;
6247 os
->direction
= NX_ACTION_SAMPLE_DEFAULT
;
6250 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
6253 if (!strcmp(key
, "probability")) {
6254 error
= str_to_u16(value
, "probability", &os
->probability
);
6255 if (!error
&& os
->probability
== 0) {
6256 error
= xasprintf("invalid probability value \"%s\"", value
);
6258 } else if (!strcmp(key
, "collector_set_id")) {
6259 error
= str_to_u32(value
, &os
->collector_set_id
);
6260 } else if (!strcmp(key
, "obs_domain_id")) {
6261 error
= str_to_u32(value
, &os
->obs_domain_id
);
6262 } else if (!strcmp(key
, "obs_point_id")) {
6263 error
= str_to_u32(value
, &os
->obs_point_id
);
6264 } else if (!strcmp(key
, "sampling_port")) {
6265 if (!ofputil_port_from_string(value
, pp
->port_map
,
6266 &os
->sampling_port
)) {
6267 error
= xasprintf("%s: unknown port", value
);
6269 } else if (!strcmp(key
, "ingress")) {
6270 os
->direction
= NX_ACTION_SAMPLE_INGRESS
;
6271 } else if (!strcmp(key
, "egress")) {
6272 os
->direction
= NX_ACTION_SAMPLE_EGRESS
;
6274 error
= xasprintf("invalid key \"%s\" in \"sample\" argument",
6281 if (os
->probability
== 0) {
6282 return xstrdup("non-zero \"probability\" must be specified on sample");
6289 format_SAMPLE(const struct ofpact_sample
*a
,
6290 const struct ofpact_format_params
*fp
)
6292 ds_put_format(fp
->s
, "%ssample(%s%sprobability=%s%"PRIu16
6293 ",%scollector_set_id=%s%"PRIu32
6294 ",%sobs_domain_id=%s%"PRIu32
6295 ",%sobs_point_id=%s%"PRIu32
,
6296 colors
.paren
, colors
.end
,
6297 colors
.param
, colors
.end
, a
->probability
,
6298 colors
.param
, colors
.end
, a
->collector_set_id
,
6299 colors
.param
, colors
.end
, a
->obs_domain_id
,
6300 colors
.param
, colors
.end
, a
->obs_point_id
);
6301 if (a
->sampling_port
!= OFPP_NONE
) {
6302 ds_put_format(fp
->s
, ",%ssampling_port=%s", colors
.param
, colors
.end
);
6303 ofputil_format_port(a
->sampling_port
, fp
->port_map
, fp
->s
);
6305 if (a
->direction
== NX_ACTION_SAMPLE_INGRESS
) {
6306 ds_put_format(fp
->s
, ",%singress%s", colors
.param
, colors
.end
);
6307 } else if (a
->direction
== NX_ACTION_SAMPLE_EGRESS
) {
6308 ds_put_format(fp
->s
, ",%segress%s", colors
.param
, colors
.end
);
6310 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
6314 check_SAMPLE(const struct ofpact_sample
*a OVS_UNUSED
,
6315 const struct ofpact_check_params
*cp OVS_UNUSED
)
6320 /* debug instructions. */
6322 static bool enable_debug
;
6325 ofpact_dummy_enable(void)
6327 enable_debug
= true;
6331 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf
*out
)
6333 if (!enable_debug
) {
6334 return OFPERR_OFPBAC_BAD_VENDOR_TYPE
;
6337 ofpact_put_DEBUG_RECIRC(out
);
6342 encode_DEBUG_RECIRC(const struct ofpact_null
*n OVS_UNUSED
,
6343 enum ofp_version ofp_version OVS_UNUSED
,
6346 put_NXAST_DEBUG_RECIRC(out
);
6349 static char * OVS_WARN_UNUSED_RESULT
6350 parse_DEBUG_RECIRC(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
6352 ofpact_put_DEBUG_RECIRC(pp
->ofpacts
);
6357 format_DEBUG_RECIRC(const struct ofpact_null
*a OVS_UNUSED
,
6358 const struct ofpact_format_params
*fp
)
6360 ds_put_format(fp
->s
, "%sdebug_recirc%s", colors
.value
, colors
.end
);
6364 check_DEBUG_RECIRC(const struct ofpact_null
*a OVS_UNUSED
,
6365 const struct ofpact_check_params
*cp OVS_UNUSED
)
6371 decode_NXAST_RAW_DEBUG_SLOW(struct ofpbuf
*out
)
6373 if (!enable_debug
) {
6374 return OFPERR_OFPBAC_BAD_VENDOR_TYPE
;
6377 ofpact_put_DEBUG_SLOW(out
);
6382 encode_DEBUG_SLOW(const struct ofpact_null
*n OVS_UNUSED
,
6383 enum ofp_version ofp_version OVS_UNUSED
,
6386 put_NXAST_DEBUG_SLOW(out
);
6389 static char * OVS_WARN_UNUSED_RESULT
6390 parse_DEBUG_SLOW(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
6392 ofpact_put_DEBUG_SLOW(pp
->ofpacts
);
6397 format_DEBUG_SLOW(const struct ofpact_null
*a OVS_UNUSED
,
6398 const struct ofpact_format_params
*fp
)
6400 ds_put_format(fp
->s
, "%sdebug_slow%s", colors
.value
, colors
.end
);
6404 check_DEBUG_SLOW(const struct ofpact_null
*a OVS_UNUSED
,
6405 const struct ofpact_check_params
*cp OVS_UNUSED
)
6410 /* Action structure for NXAST_CT.
6412 * Pass traffic to the connection tracker.
6414 * There are two important concepts to understanding the connection tracking
6415 * interface: Packet state and Connection state. Packets may be "Untracked" or
6416 * "Tracked". Connections may be "Uncommitted" or "Committed".
6420 * Untracked packets have an unknown connection state. In most
6421 * cases, packets entering the OpenFlow pipeline will initially be
6422 * in the untracked state. Untracked packets may become tracked by
6423 * executing NXAST_CT with a "recirc_table" specified. This makes
6424 * various aspects about the connection available, in particular
6425 * the connection state.
6427 * An NXAST_CT action always puts the packet into an untracked
6428 * state for the current processing path. If "recirc_table" is
6429 * set, execution is forked and the packet passes through the
6430 * connection tracker. The specified table's processing path is
6431 * able to match on Connection state until the end of the OpenFlow
6432 * pipeline or NXAST_CT is called again.
6434 * - Connection State:
6436 * Multiple packets may be associated with a single connection. Initially,
6437 * all connections are uncommitted. The connection state corresponding to
6438 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
6440 * Uncommitted connections have no state stored about them. Uncommitted
6441 * connections may transition into the committed state by executing
6442 * NXAST_CT with the NX_CT_F_COMMIT flag.
6444 * Once a connection becomes committed, information may be gathered about
6445 * the connection by passing subsequent packets through the connection
6446 * tracker, and the state of the connection will be stored beyond the
6447 * lifetime of packet processing.
6449 * A committed connection always has the directionality of the packet that
6450 * caused the connection to be committed in the first place. This is the
6451 * "original direction" of the connection, and the opposite direction is
6452 * the "reply direction". If a connection is already committed, but it is
6453 * then decided that the original direction should be the opposite of the
6454 * existing connection, NX_CT_F_FORCE flag may be used in addition to
6455 * NX_CT_F_COMMIT flag to in effect terminate the existing connection and
6456 * start a new one in the current direction.
6458 * Connections may transition back into the uncommitted state due to
6459 * external timers, or due to the contents of packets that are sent to the
6460 * connection tracker. This behaviour is outside of the scope of the
6461 * OpenFlow interface.
6463 * The "zone" specifies a context within which the tracking is done:
6465 * The connection tracking zone is a 16-bit number. Each zone is an
6466 * independent connection tracking context. The connection state for each
6467 * connection is completely separate for each zone, so if a connection
6468 * is committed to zone A, then it will remain uncommitted in zone B.
6469 * If NXAST_CT is executed with the same zone multiple times, later
6470 * executions have no effect.
6472 * If 'zone_src' is nonzero, this specifies that the zone should be
6473 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
6474 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
6475 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
6476 * are the same as the acceptable nxm_header values for the 'src' field of
6479 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
6480 * connection tracking zone.
6482 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
6484 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
6485 * packet will be logically cloned prior to executing this action. One
6486 * copy will be sent to the connection tracker, then will be re-injected
6487 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
6488 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
6489 * fields will be populated. The original instance of the packet will
6490 * continue the current actions list. This can be thought of as similar to
6491 * the effect of the "output" action: One copy is sent out (in this case,
6492 * to the connection tracker), but the current copy continues processing.
6494 * It is strongly recommended that this table is later than the current
6495 * table, to prevent loops.
6497 * The "alg" attaches protocol-specific behaviour to this action:
6499 * The ALG is a 16-bit number which specifies that additional
6500 * processing should be applied to this traffic.
6502 * Protocol | Value | Meaning
6503 * --------------------------------------------------------------------
6504 * None | 0 | No protocol-specific behaviour.
6505 * FTP | 21 | Parse FTP control connections and observe the
6506 * | | negotiation of related data connections.
6507 * Other | Other | Unsupported protocols.
6509 * By way of example, if FTP control connections have this action applied
6510 * with the ALG set to FTP (21), then the connection tracker will observe
6511 * the negotiation of data connections. This allows the connection
6512 * tracker to identify subsequent data connections as "related" to this
6513 * existing connection. The "related" flag will be populated in the
6514 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
6517 * Zero or more actions may immediately follow this action. These actions will
6518 * be executed within the context of the connection tracker, and they require
6519 * NX_CT_F_COMMIT flag be set.
6521 struct nx_action_conntrack
{
6522 ovs_be16 type
; /* OFPAT_VENDOR. */
6523 ovs_be16 len
; /* At least 24. */
6524 ovs_be32 vendor
; /* NX_VENDOR_ID. */
6525 ovs_be16 subtype
; /* NXAST_CT. */
6526 ovs_be16 flags
; /* Zero or more NX_CT_F_* flags.
6527 * Unspecified flag bits must be zero. */
6528 ovs_be32 zone_src
; /* Connection tracking context. */
6530 ovs_be16 zone_ofs_nbits
;/* Range to use from source field. */
6531 ovs_be16 zone_imm
; /* Immediate value for zone. */
6533 uint8_t recirc_table
; /* Recirculate to a specific table, or
6534 NX_CT_RECIRC_NONE for no recirculation. */
6535 uint8_t pad
[3]; /* Zeroes */
6536 ovs_be16 alg
; /* Well-known port number for the protocol.
6537 * 0 indicates no ALG is required. */
6538 /* Followed by a sequence of zero or more OpenFlow actions. The length of
6539 * these is included in 'len'. */
6541 OFP_ASSERT(sizeof(struct nx_action_conntrack
) == 24);
6544 decode_ct_zone(const struct nx_action_conntrack
*nac
,
6545 struct ofpact_conntrack
*out
,
6546 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
)
6548 if (nac
->zone_src
) {
6551 out
->zone_src
.ofs
= nxm_decode_ofs(nac
->zone_ofs_nbits
);
6552 out
->zone_src
.n_bits
= nxm_decode_n_bits(nac
->zone_ofs_nbits
);
6553 error
= mf_vl_mff_mf_from_nxm_header(ntohl(nac
->zone_src
),
6554 vl_mff_map
, &out
->zone_src
.field
,
6560 error
= mf_check_src(&out
->zone_src
, NULL
);
6565 if (out
->zone_src
.n_bits
!= 16) {
6566 VLOG_WARN_RL(&rl
, "zone n_bits %d not within valid range [16..16]",
6567 out
->zone_src
.n_bits
);
6568 return OFPERR_OFPBAC_BAD_SET_LEN
;
6571 out
->zone_src
.field
= NULL
;
6572 out
->zone_imm
= ntohs(nac
->zone_imm
);
6579 decode_NXAST_RAW_CT(const struct nx_action_conntrack
*nac
,
6580 enum ofp_version ofp_version
,
6581 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
6584 const size_t ct_offset
= ofpacts_pull(out
);
6585 struct ofpact_conntrack
*conntrack
= ofpact_put_CT(out
);
6588 conntrack
->flags
= ntohs(nac
->flags
);
6589 if (conntrack
->flags
& NX_CT_F_FORCE
&&
6590 !(conntrack
->flags
& NX_CT_F_COMMIT
)) {
6591 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
6595 error
= decode_ct_zone(nac
, conntrack
, vl_mff_map
, tlv_bitmap
);
6599 conntrack
->recirc_table
= nac
->recirc_table
;
6600 conntrack
->alg
= ntohs(nac
->alg
);
6602 ofpbuf_pull(out
, sizeof(*conntrack
));
6604 struct ofpbuf openflow
= ofpbuf_const_initializer(
6605 nac
+ 1, ntohs(nac
->len
) - sizeof(*nac
));
6606 error
= ofpacts_pull_openflow_actions__(&openflow
, openflow
.size
,
6608 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
6609 out
, OFPACT_CT
, vl_mff_map
,
6615 conntrack
= ofpbuf_push_uninit(out
, sizeof(*conntrack
));
6616 out
->header
= &conntrack
->ofpact
;
6617 ofpact_finish_CT(out
, &conntrack
);
6619 if (conntrack
->ofpact
.len
> sizeof(*conntrack
)
6620 && !(conntrack
->flags
& NX_CT_F_COMMIT
)) {
6621 const struct ofpact
*a
;
6622 size_t ofpacts_len
= conntrack
->ofpact
.len
- sizeof(*conntrack
);
6624 OFPACT_FOR_EACH (a
, conntrack
->actions
, ofpacts_len
) {
6625 if (a
->type
!= OFPACT_NAT
|| ofpact_get_NAT(a
)->flags
6626 || ofpact_get_NAT(a
)->range_af
!= AF_UNSPEC
) {
6627 VLOG_WARN_RL(&rl
, "CT action requires commit flag if actions "
6628 "other than NAT without arguments are specified.");
6629 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
6636 ofpbuf_push_uninit(out
, ct_offset
);
6641 encode_CT(const struct ofpact_conntrack
*conntrack
,
6642 enum ofp_version ofp_version
, struct ofpbuf
*out
)
6644 struct nx_action_conntrack
*nac
;
6645 const size_t ofs
= out
->size
;
6648 nac
= put_NXAST_CT(out
);
6649 nac
->flags
= htons(conntrack
->flags
);
6650 if (conntrack
->zone_src
.field
) {
6651 nac
->zone_src
= htonl(nxm_header_from_mff(conntrack
->zone_src
.field
));
6652 nac
->zone_ofs_nbits
= nxm_encode_ofs_nbits(conntrack
->zone_src
.ofs
,
6653 conntrack
->zone_src
.n_bits
);
6655 nac
->zone_src
= htonl(0);
6656 nac
->zone_imm
= htons(conntrack
->zone_imm
);
6658 nac
->recirc_table
= conntrack
->recirc_table
;
6659 nac
->alg
= htons(conntrack
->alg
);
6661 len
= ofpacts_put_openflow_actions(conntrack
->actions
,
6662 ofpact_ct_get_action_len(conntrack
),
6664 len
+= sizeof(*nac
);
6665 nac
= ofpbuf_at(out
, ofs
, sizeof(*nac
));
6666 nac
->len
= htons(len
);
6669 static char *OVS_WARN_UNUSED_RESULT
6670 parse_NAT(char *arg
, const struct ofpact_parse_params
*pp
);
6672 /* Parses 'arg' as the argument to a "ct" action, and appends such an
6673 * action to 'pp->ofpacts'.
6675 * Returns NULL if successful, otherwise a malloc()'d string describing the
6676 * error. The caller is responsible for freeing the returned string. */
6677 static char * OVS_WARN_UNUSED_RESULT
6678 parse_CT(char *arg
, const struct ofpact_parse_params
*pp
)
6680 const size_t ct_offset
= ofpacts_pull(pp
->ofpacts
);
6681 struct ofpact_conntrack
*oc
;
6685 oc
= ofpact_put_CT(pp
->ofpacts
);
6687 oc
->recirc_table
= NX_CT_RECIRC_NONE
;
6688 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
6689 if (!strcmp(key
, "commit")) {
6690 oc
->flags
|= NX_CT_F_COMMIT
;
6691 } else if (!strcmp(key
, "force")) {
6692 oc
->flags
|= NX_CT_F_FORCE
;
6693 } else if (!strcmp(key
, "table")) {
6694 if (!ofputil_table_from_string(value
, pp
->table_map
,
6695 &oc
->recirc_table
)) {
6696 error
= xasprintf("unknown table %s", value
);
6697 } else if (oc
->recirc_table
== NX_CT_RECIRC_NONE
) {
6698 error
= xasprintf("invalid table %#"PRIx8
, oc
->recirc_table
);
6700 } else if (!strcmp(key
, "zone")) {
6701 error
= str_to_u16(value
, "zone", &oc
->zone_imm
);
6705 error
= mf_parse_subfield(&oc
->zone_src
, value
);
6710 } else if (!strcmp(key
, "alg")) {
6711 error
= str_to_connhelper(value
, &oc
->alg
);
6712 } else if (!strcmp(key
, "nat")) {
6713 const size_t nat_offset
= ofpacts_pull(pp
->ofpacts
);
6715 error
= parse_NAT(value
, pp
);
6716 /* Update CT action pointer and length. */
6717 pp
->ofpacts
->header
= ofpbuf_push_uninit(pp
->ofpacts
, nat_offset
);
6718 oc
= pp
->ofpacts
->header
;
6719 } else if (!strcmp(key
, "exec")) {
6720 /* Hide existing actions from ofpacts_parse_copy(), so the
6721 * nesting can be handled transparently. */
6722 enum ofputil_protocol usable_protocols2
;
6723 const size_t exec_offset
= ofpacts_pull(pp
->ofpacts
);
6725 /* Initializes 'usable_protocol2', fold it back to
6726 * '*usable_protocols' afterwards, so that we do not lose
6727 * restrictions already in there. */
6728 struct ofpact_parse_params pp2
= *pp
;
6729 pp2
.usable_protocols
= &usable_protocols2
;
6730 error
= ofpacts_parse_copy(value
, &pp2
, false, OFPACT_CT
);
6731 *pp
->usable_protocols
&= usable_protocols2
;
6732 pp
->ofpacts
->header
= ofpbuf_push_uninit(pp
->ofpacts
, exec_offset
);
6733 oc
= pp
->ofpacts
->header
;
6735 error
= xasprintf("invalid argument to \"ct\" action: `%s'", key
);
6741 if (!error
&& oc
->flags
& NX_CT_F_FORCE
&& !(oc
->flags
& NX_CT_F_COMMIT
)) {
6742 error
= xasprintf("\"force\" flag requires \"commit\" flag.");
6745 if (ofpbuf_oversized(pp
->ofpacts
)) {
6747 return xasprintf("input too big");
6750 ofpact_finish_CT(pp
->ofpacts
, &oc
);
6751 ofpbuf_push_uninit(pp
->ofpacts
, ct_offset
);
6756 format_alg(int port
, struct ds
*s
)
6760 ds_put_format(s
, "%salg=%sftp,", colors
.param
, colors
.end
);
6763 ds_put_format(s
, "%salg=%stftp,", colors
.param
, colors
.end
);
6769 ds_put_format(s
, "%salg=%s%d,", colors
.param
, colors
.end
, port
);
6774 static void format_NAT(const struct ofpact_nat
*,
6775 const struct ofpact_format_params
*fp
);
6778 format_CT(const struct ofpact_conntrack
*a
,
6779 const struct ofpact_format_params
*fp
)
6781 ds_put_format(fp
->s
, "%sct(%s", colors
.paren
, colors
.end
);
6782 if (a
->flags
& NX_CT_F_COMMIT
) {
6783 ds_put_format(fp
->s
, "%scommit%s,", colors
.value
, colors
.end
);
6785 if (a
->flags
& NX_CT_F_FORCE
) {
6786 ds_put_format(fp
->s
, "%sforce%s,", colors
.value
, colors
.end
);
6788 if (a
->recirc_table
!= NX_CT_RECIRC_NONE
) {
6789 ds_put_format(fp
->s
, "%stable=%s", colors
.special
, colors
.end
);
6790 ofputil_format_table(a
->recirc_table
, fp
->table_map
, fp
->s
);
6791 ds_put_char(fp
->s
, ',');
6793 if (a
->zone_src
.field
) {
6794 ds_put_format(fp
->s
, "%szone=%s", colors
.param
, colors
.end
);
6795 mf_format_subfield(&a
->zone_src
, fp
->s
);
6796 ds_put_char(fp
->s
, ',');
6797 } else if (a
->zone_imm
) {
6798 ds_put_format(fp
->s
, "%szone=%s%"PRIu16
",",
6799 colors
.param
, colors
.end
, a
->zone_imm
);
6801 /* If the first action is a NAT action, format it outside of the 'exec'
6803 const struct ofpact
*action
= a
->actions
;
6804 size_t actions_len
= ofpact_ct_get_action_len(a
);
6805 if (actions_len
&& action
->type
== OFPACT_NAT
) {
6806 format_NAT(ofpact_get_NAT(action
), fp
);
6807 ds_put_char(fp
->s
, ',');
6808 actions_len
-= OFPACT_ALIGN(action
->len
);
6809 action
= ofpact_next(action
);
6812 ds_put_format(fp
->s
, "%sexec(%s", colors
.paren
, colors
.end
);
6813 ofpacts_format(action
, actions_len
, fp
);
6814 ds_put_format(fp
->s
, "%s),%s", colors
.paren
, colors
.end
);
6816 format_alg(a
->alg
, fp
->s
);
6817 ds_chomp(fp
->s
, ',');
6818 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
6822 check_CT(struct ofpact_conntrack
*a
, struct ofpact_check_params
*cp
)
6824 struct flow
*flow
= &cp
->match
->flow
;
6826 if (!dl_type_is_ip_any(get_dl_type(flow
))
6827 || (flow
->ct_state
& CS_INVALID
&& a
->flags
& NX_CT_F_COMMIT
)
6828 || (a
->alg
== IPPORT_FTP
&& flow
->nw_proto
!= IPPROTO_TCP
)
6829 || (a
->alg
== IPPORT_TFTP
&& flow
->nw_proto
!= IPPROTO_UDP
)) {
6830 /* We can't downgrade to OF1.0 and expect inconsistent CT actions
6831 * be silently discarded. Instead, datapath flow install fails, so
6832 * it is better to flag inconsistent CT actions as hard errors. */
6833 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
6836 if (a
->zone_src
.field
) {
6837 return mf_check_src(&a
->zone_src
, cp
->match
);
6840 return check_subactions(a
->actions
, ofpact_ct_get_action_len(a
), cp
);
6843 /* ct_clear action. */
6846 decode_NXAST_RAW_CT_CLEAR(struct ofpbuf
*out
)
6848 ofpact_put_CT_CLEAR(out
);
6853 encode_CT_CLEAR(const struct ofpact_null
*null OVS_UNUSED
,
6854 enum ofp_version ofp_version OVS_UNUSED
,
6857 put_NXAST_CT_CLEAR(out
);
6860 static char * OVS_WARN_UNUSED_RESULT
6861 parse_CT_CLEAR(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
6863 ofpact_put_CT_CLEAR(pp
->ofpacts
);
6868 format_CT_CLEAR(const struct ofpact_null
*a OVS_UNUSED
,
6869 const struct ofpact_format_params
*fp
)
6871 ds_put_format(fp
->s
, "%sct_clear%s", colors
.value
, colors
.end
);
6875 check_CT_CLEAR(const struct ofpact_null
*a OVS_UNUSED
,
6876 const struct ofpact_check_params
*cp OVS_UNUSED
)
6883 /* Which optional fields are present? */
6885 NX_NAT_RANGE_IPV4_MIN
= 1 << 0, /* ovs_be32 */
6886 NX_NAT_RANGE_IPV4_MAX
= 1 << 1, /* ovs_be32 */
6887 NX_NAT_RANGE_IPV6_MIN
= 1 << 2, /* struct in6_addr */
6888 NX_NAT_RANGE_IPV6_MAX
= 1 << 3, /* struct in6_addr */
6889 NX_NAT_RANGE_PROTO_MIN
= 1 << 4, /* ovs_be16 */
6890 NX_NAT_RANGE_PROTO_MAX
= 1 << 5, /* ovs_be16 */
6893 /* Action structure for NXAST_NAT. */
6894 struct nx_action_nat
{
6895 ovs_be16 type
; /* OFPAT_VENDOR. */
6896 ovs_be16 len
; /* At least 16. */
6897 ovs_be32 vendor
; /* NX_VENDOR_ID. */
6898 ovs_be16 subtype
; /* NXAST_NAT. */
6899 uint8_t pad
[2]; /* Must be zero. */
6900 ovs_be16 flags
; /* Zero or more NX_NAT_F_* flags.
6901 * Unspecified flag bits must be zero. */
6902 ovs_be16 range_present
; /* NX_NAT_RANGE_* */
6903 /* Followed by optional parameters as specified by 'range_present' */
6905 OFP_ASSERT(sizeof(struct nx_action_nat
) == 16);
6908 encode_NAT(const struct ofpact_nat
*nat
,
6909 enum ofp_version ofp_version OVS_UNUSED
,
6912 struct nx_action_nat
*nan
;
6913 const size_t ofs
= out
->size
;
6914 uint16_t range_present
= 0;
6916 nan
= put_NXAST_NAT(out
);
6917 nan
->flags
= htons(nat
->flags
);
6918 if (nat
->range_af
== AF_INET
) {
6919 if (nat
->range
.addr
.ipv4
.min
) {
6920 ovs_be32
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
6921 *min
= nat
->range
.addr
.ipv4
.min
;
6922 range_present
|= NX_NAT_RANGE_IPV4_MIN
;
6924 if (nat
->range
.addr
.ipv4
.max
) {
6925 ovs_be32
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
6926 *max
= nat
->range
.addr
.ipv4
.max
;
6927 range_present
|= NX_NAT_RANGE_IPV4_MAX
;
6929 } else if (nat
->range_af
== AF_INET6
) {
6930 if (!ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.min
)) {
6931 struct in6_addr
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
6932 *min
= nat
->range
.addr
.ipv6
.min
;
6933 range_present
|= NX_NAT_RANGE_IPV6_MIN
;
6935 if (!ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.max
)) {
6936 struct in6_addr
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
6937 *max
= nat
->range
.addr
.ipv6
.max
;
6938 range_present
|= NX_NAT_RANGE_IPV6_MAX
;
6941 if (nat
->range_af
!= AF_UNSPEC
) {
6942 if (nat
->range
.proto
.min
) {
6943 ovs_be16
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
6944 *min
= htons(nat
->range
.proto
.min
);
6945 range_present
|= NX_NAT_RANGE_PROTO_MIN
;
6947 if (nat
->range
.proto
.max
) {
6948 ovs_be16
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
6949 *max
= htons(nat
->range
.proto
.max
);
6950 range_present
|= NX_NAT_RANGE_PROTO_MAX
;
6953 pad_ofpat(out
, ofs
);
6954 nan
= ofpbuf_at(out
, ofs
, sizeof *nan
);
6955 nan
->range_present
= htons(range_present
);
6959 decode_NXAST_RAW_NAT(const struct nx_action_nat
*nan
,
6960 enum ofp_version ofp_version OVS_UNUSED
,
6963 struct ofpact_nat
*nat
;
6964 uint16_t range_present
= ntohs(nan
->range_present
);
6965 const char *opts
= (char *)(nan
+ 1);
6966 uint16_t len
= ntohs(nan
->len
) - sizeof *nan
;
6968 nat
= ofpact_put_NAT(out
);
6969 nat
->flags
= ntohs(nan
->flags
);
6971 /* Check for unknown or mutually exclusive flags. */
6972 if ((nat
->flags
& ~NX_NAT_F_MASK
)
6973 || (nat
->flags
& NX_NAT_F_SRC
&& nat
->flags
& NX_NAT_F_DST
)
6974 || (nat
->flags
& NX_NAT_F_PROTO_HASH
6975 && nat
->flags
& NX_NAT_F_PROTO_RANDOM
)) {
6976 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6979 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
6980 (LEN >= sizeof(TYPE) \
6981 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
6982 SRC += sizeof(TYPE)) \
6985 nat
->range_af
= AF_UNSPEC
;
6986 if (range_present
& NX_NAT_RANGE_IPV4_MIN
) {
6987 if (range_present
& (NX_NAT_RANGE_IPV6_MIN
| NX_NAT_RANGE_IPV6_MAX
)) {
6988 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6991 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv4
.min
, opts
, len
, ovs_be32
)
6992 || !nat
->range
.addr
.ipv4
.min
) {
6993 return OFPERR_OFPBAC_BAD_ARGUMENT
;
6996 nat
->range_af
= AF_INET
;
6998 if (range_present
& NX_NAT_RANGE_IPV4_MAX
) {
6999 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv4
.max
, opts
, len
,
7001 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7003 if (ntohl(nat
->range
.addr
.ipv4
.max
)
7004 < ntohl(nat
->range
.addr
.ipv4
.min
)) {
7005 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7008 } else if (range_present
& NX_NAT_RANGE_IPV4_MAX
) {
7009 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7010 } else if (range_present
& NX_NAT_RANGE_IPV6_MIN
) {
7011 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv6
.min
, opts
, len
,
7013 || ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.min
)) {
7014 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7017 nat
->range_af
= AF_INET6
;
7019 if (range_present
& NX_NAT_RANGE_IPV6_MAX
) {
7020 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv6
.max
, opts
, len
,
7022 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7024 if (memcmp(&nat
->range
.addr
.ipv6
.max
, &nat
->range
.addr
.ipv6
.min
,
7025 sizeof(struct in6_addr
)) < 0) {
7026 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7029 } else if (range_present
& NX_NAT_RANGE_IPV6_MAX
) {
7030 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7033 if (range_present
& NX_NAT_RANGE_PROTO_MIN
) {
7036 if (nat
->range_af
== AF_UNSPEC
) {
7037 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7039 if (!NX_NAT_GET_OPT(&proto
, opts
, len
, ovs_be16
) || proto
== 0) {
7040 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7042 nat
->range
.proto
.min
= ntohs(proto
);
7043 if (range_present
& NX_NAT_RANGE_PROTO_MAX
) {
7044 if (!NX_NAT_GET_OPT(&proto
, opts
, len
, ovs_be16
)) {
7045 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7047 nat
->range
.proto
.max
= ntohs(proto
);
7048 if (nat
->range
.proto
.max
< nat
->range
.proto
.min
) {
7049 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7052 } else if (range_present
& NX_NAT_RANGE_PROTO_MAX
) {
7053 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7060 format_NAT(const struct ofpact_nat
*a
, const struct ofpact_format_params
*fp
)
7062 ds_put_format(fp
->s
, "%snat%s", colors
.paren
, colors
.end
);
7064 if (a
->flags
& (NX_NAT_F_SRC
| NX_NAT_F_DST
)) {
7065 ds_put_format(fp
->s
, "%s(%s", colors
.paren
, colors
.end
);
7066 ds_put_format(fp
->s
, a
->flags
& NX_NAT_F_SRC
? "%ssrc%s" : "%sdst%s",
7067 colors
.param
, colors
.end
);
7069 if (a
->range_af
!= AF_UNSPEC
) {
7070 ds_put_format(fp
->s
, "%s=%s", colors
.param
, colors
.end
);
7072 if (a
->range_af
== AF_INET
) {
7073 ds_put_format(fp
->s
, IP_FMT
, IP_ARGS(a
->range
.addr
.ipv4
.min
));
7075 if (a
->range
.addr
.ipv4
.max
7076 && a
->range
.addr
.ipv4
.max
!= a
->range
.addr
.ipv4
.min
) {
7077 ds_put_format(fp
->s
, "-"IP_FMT
,
7078 IP_ARGS(a
->range
.addr
.ipv4
.max
));
7080 } else if (a
->range_af
== AF_INET6
) {
7081 ipv6_format_addr_bracket(&a
->range
.addr
.ipv6
.min
, fp
->s
,
7082 a
->range
.proto
.min
);
7084 if (!ipv6_mask_is_any(&a
->range
.addr
.ipv6
.max
)
7085 && memcmp(&a
->range
.addr
.ipv6
.max
, &a
->range
.addr
.ipv6
.min
,
7086 sizeof(struct in6_addr
)) != 0) {
7087 ds_put_char(fp
->s
, '-');
7088 ipv6_format_addr_bracket(&a
->range
.addr
.ipv6
.max
, fp
->s
,
7089 a
->range
.proto
.min
);
7092 if (a
->range
.proto
.min
) {
7093 ds_put_char(fp
->s
, ':');
7094 ds_put_format(fp
->s
, "%"PRIu16
, a
->range
.proto
.min
);
7096 if (a
->range
.proto
.max
7097 && a
->range
.proto
.max
!= a
->range
.proto
.min
) {
7098 ds_put_format(fp
->s
, "-%"PRIu16
, a
->range
.proto
.max
);
7101 ds_put_char(fp
->s
, ',');
7103 if (a
->flags
& NX_NAT_F_PERSISTENT
) {
7104 ds_put_format(fp
->s
, "%spersistent%s,",
7105 colors
.value
, colors
.end
);
7107 if (a
->flags
& NX_NAT_F_PROTO_HASH
) {
7108 ds_put_format(fp
->s
, "%shash%s,", colors
.value
, colors
.end
);
7110 if (a
->flags
& NX_NAT_F_PROTO_RANDOM
) {
7111 ds_put_format(fp
->s
, "%srandom%s,", colors
.value
, colors
.end
);
7114 ds_chomp(fp
->s
, ',');
7115 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
7119 static char * OVS_WARN_UNUSED_RESULT
7120 str_to_nat_range(const char *s
, struct ofpact_nat
*on
)
7122 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
7125 on
->range_af
= AF_UNSPEC
;
7126 if (ovs_scan_len(s
, &n
, IP_SCAN_FMT
,
7127 IP_SCAN_ARGS(&on
->range
.addr
.ipv4
.min
))) {
7128 on
->range_af
= AF_INET
;
7132 if (!ovs_scan_len(s
, &n
, IP_SCAN_FMT
,
7133 IP_SCAN_ARGS(&on
->range
.addr
.ipv4
.max
))
7134 || (ntohl(on
->range
.addr
.ipv4
.max
)
7135 < ntohl(on
->range
.addr
.ipv4
.min
))) {
7139 } else if ((ovs_scan_len(s
, &n
, IPV6_SCAN_FMT
, ipv6_s
)
7140 || ovs_scan_len(s
, &n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
7141 && inet_pton(AF_INET6
, ipv6_s
, &on
->range
.addr
.ipv6
.min
) == 1) {
7142 on
->range_af
= AF_INET6
;
7146 if (!(ovs_scan_len(s
, &n
, IPV6_SCAN_FMT
, ipv6_s
)
7147 || ovs_scan_len(s
, &n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
7148 || inet_pton(AF_INET6
, ipv6_s
, &on
->range
.addr
.ipv6
.max
) != 1
7149 || memcmp(&on
->range
.addr
.ipv6
.max
, &on
->range
.addr
.ipv6
.min
,
7150 sizeof on
->range
.addr
.ipv6
.max
) < 0) {
7155 if (on
->range_af
!= AF_UNSPEC
&& s
[n
] == ':') {
7157 if (!ovs_scan_len(s
, &n
, "%"SCNu16
, &on
->range
.proto
.min
)) {
7162 if (!ovs_scan_len(s
, &n
, "%"SCNu16
, &on
->range
.proto
.max
)
7163 || on
->range
.proto
.max
< on
->range
.proto
.min
) {
7168 if (strlen(s
) != n
) {
7169 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
7174 return xasprintf("invalid nat range \"%s\"", s
);
7178 /* Parses 'arg' as the argument to a "nat" action, and appends such an
7179 * action to 'pp->ofpacts'.
7181 * Returns NULL if successful, otherwise a malloc()'d string describing the
7182 * error. The caller is responsible for freeing the returned string. */
7183 static char * OVS_WARN_UNUSED_RESULT
7184 parse_NAT(char *arg
, const struct ofpact_parse_params
*pp
)
7186 struct ofpact_nat
*on
= ofpact_put_NAT(pp
->ofpacts
);
7190 on
->range_af
= AF_UNSPEC
;
7192 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
7195 if (!strcmp(key
, "src")) {
7196 on
->flags
|= NX_NAT_F_SRC
;
7197 error
= str_to_nat_range(value
, on
);
7198 } else if (!strcmp(key
, "dst")) {
7199 on
->flags
|= NX_NAT_F_DST
;
7200 error
= str_to_nat_range(value
, on
);
7201 } else if (!strcmp(key
, "persistent")) {
7202 on
->flags
|= NX_NAT_F_PERSISTENT
;
7203 } else if (!strcmp(key
, "hash")) {
7204 on
->flags
|= NX_NAT_F_PROTO_HASH
;
7205 } else if (!strcmp(key
, "random")) {
7206 on
->flags
|= NX_NAT_F_PROTO_RANDOM
;
7208 error
= xasprintf("invalid key \"%s\" in \"nat\" argument",
7215 if (on
->flags
& NX_NAT_F_SRC
&& on
->flags
& NX_NAT_F_DST
) {
7216 return xasprintf("May only specify one of \"src\" or \"dst\".");
7218 if (!(on
->flags
& NX_NAT_F_SRC
|| on
->flags
& NX_NAT_F_DST
)) {
7220 return xasprintf("Flags allowed only with \"src\" or \"dst\".");
7222 if (on
->range_af
!= AF_UNSPEC
) {
7223 return xasprintf("Range allowed only with \"src\" or \"dst\".");
7226 if (on
->flags
& NX_NAT_F_PROTO_HASH
&& on
->flags
& NX_NAT_F_PROTO_RANDOM
) {
7227 return xasprintf("Both \"hash\" and \"random\" are not allowed.");
7234 check_NAT(const struct ofpact_nat
*a
, const struct ofpact_check_params
*cp
)
7236 ovs_be16 dl_type
= get_dl_type(&cp
->match
->flow
);
7237 if (!dl_type_is_ip_any(dl_type
) ||
7238 (a
->range_af
== AF_INET
&& dl_type
!= htons(ETH_TYPE_IP
)) ||
7239 (a
->range_af
== AF_INET6
&& dl_type
!= htons(ETH_TYPE_IPV6
))) {
7240 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
7245 /* Truncate output action. */
7246 struct nx_action_output_trunc
{
7247 ovs_be16 type
; /* OFPAT_VENDOR. */
7248 ovs_be16 len
; /* At least 16. */
7249 ovs_be32 vendor
; /* NX_VENDOR_ID. */
7250 ovs_be16 subtype
; /* NXAST_OUTPUT_TRUNC. */
7251 ovs_be16 port
; /* Output port */
7252 ovs_be32 max_len
; /* Truncate packet to size bytes */
7254 OFP_ASSERT(sizeof(struct nx_action_output_trunc
) == 16);
7257 decode_NXAST_RAW_OUTPUT_TRUNC(const struct nx_action_output_trunc
*natrc
,
7258 enum ofp_version ofp_version OVS_UNUSED
,
7261 struct ofpact_output_trunc
*output_trunc
;
7263 output_trunc
= ofpact_put_OUTPUT_TRUNC(out
);
7264 output_trunc
->max_len
= ntohl(natrc
->max_len
);
7265 output_trunc
->port
= u16_to_ofp(ntohs(natrc
->port
));
7267 if (output_trunc
->max_len
< ETH_HEADER_LEN
) {
7268 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7274 encode_OUTPUT_TRUNC(const struct ofpact_output_trunc
*output_trunc
,
7275 enum ofp_version ofp_version OVS_UNUSED
,
7278 struct nx_action_output_trunc
*natrc
= put_NXAST_OUTPUT_TRUNC(out
);
7280 natrc
->max_len
= htonl(output_trunc
->max_len
);
7281 natrc
->port
= htons(ofp_to_u16(output_trunc
->port
));
7284 static char * OVS_WARN_UNUSED_RESULT
7285 parse_OUTPUT_TRUNC(const char *arg
,
7286 const struct ofpact_parse_params
*pp OVS_UNUSED
)
7288 /* Disable output_trunc parsing. Expose as output(port=N,max_len=M) and
7289 * reuse parse_OUTPUT to parse output_trunc action. */
7290 return xasprintf("unknown action %s", arg
);
7294 format_OUTPUT_TRUNC(const struct ofpact_output_trunc
*a
,
7295 const struct ofpact_format_params
*fp
)
7297 ds_put_format(fp
->s
, "%soutput%s(port=", colors
.special
, colors
.end
);
7298 ofputil_format_port(a
->port
, fp
->port_map
, fp
->s
);
7299 ds_put_format(fp
->s
, ",max_len=%"PRIu32
")", a
->max_len
);
7303 check_OUTPUT_TRUNC(const struct ofpact_output_trunc
*a
,
7304 const struct ofpact_check_params
*cp
)
7306 return ofpact_check_output_port(a
->port
, cp
->max_ports
);
7311 * In OpenFlow 1.3 and 1.4, "meter" is an instruction.
7312 * In OpenFlow 1.5 and later, "meter" is an action.
7317 decode_OFPAT_RAW15_METER(uint32_t meter_id
,
7318 enum ofp_version ofp_version OVS_UNUSED
,
7321 struct ofpact_meter
*om
= ofpact_put_METER(out
);
7322 om
->meter_id
= meter_id
;
7323 om
->provider_meter_id
= UINT32_MAX
; /* No provider meter ID. */
7328 encode_METER(const struct ofpact_meter
*meter
,
7329 enum ofp_version ofp_version
, struct ofpbuf
*out
)
7331 if (ofp_version
== OFP13_VERSION
|| ofp_version
== OFP14_VERSION
) {
7332 instruction_put_OFPIT13_METER(out
)->meter_id
= htonl(meter
->meter_id
);
7333 } else if (ofp_version
>= OFP15_VERSION
) {
7334 put_OFPAT15_METER(out
, meter
->meter_id
);
7338 static char * OVS_WARN_UNUSED_RESULT
7339 parse_METER(char *arg
, const struct ofpact_parse_params
*pp
)
7341 *pp
->usable_protocols
&= OFPUTIL_P_OF13_UP
;
7342 return str_to_u32(arg
, &ofpact_put_METER(pp
->ofpacts
)->meter_id
);
7346 format_METER(const struct ofpact_meter
*a
,
7347 const struct ofpact_format_params
*fp
)
7349 ds_put_format(fp
->s
, "%smeter:%s%"PRIu32
,
7350 colors
.param
, colors
.end
, a
->meter_id
);
7354 check_METER(const struct ofpact_meter
*a
,
7355 const struct ofpact_check_params
*cp OVS_UNUSED
)
7357 uint32_t mid
= a
->meter_id
;
7358 return mid
== 0 || mid
> OFPM13_MAX
? OFPERR_OFPMMFC_INVALID_METER
: 0;
7361 /* Clear-Actions instruction. */
7364 encode_CLEAR_ACTIONS(const struct ofpact_null
*null OVS_UNUSED
,
7365 enum ofp_version ofp_version OVS_UNUSED
,
7366 struct ofpbuf
*out OVS_UNUSED
)
7368 if (ofp_version
> OFP10_VERSION
) {
7369 instruction_put_OFPIT11_CLEAR_ACTIONS(out
);
7373 static char * OVS_WARN_UNUSED_RESULT
7374 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED
, const struct ofpact_parse_params
*pp
)
7376 ofpact_put_CLEAR_ACTIONS(pp
->ofpacts
);
7381 format_CLEAR_ACTIONS(const struct ofpact_null
*a OVS_UNUSED
,
7382 const struct ofpact_format_params
*fp
)
7384 ds_put_format(fp
->s
, "%sclear_actions%s", colors
.value
, colors
.end
);
7388 check_CLEAR_ACTIONS(const struct ofpact_null
*a OVS_UNUSED
,
7389 const struct ofpact_check_params
*cp OVS_UNUSED
)
7394 /* Write-Actions instruction. */
7397 encode_WRITE_ACTIONS(const struct ofpact_nest
*actions
,
7398 enum ofp_version ofp_version
, struct ofpbuf
*out
)
7400 if (ofp_version
> OFP10_VERSION
) {
7401 const size_t ofs
= out
->size
;
7403 instruction_put_OFPIT11_WRITE_ACTIONS(out
);
7404 ofpacts_put_openflow_actions(actions
->actions
,
7405 ofpact_nest_get_action_len(actions
),
7407 ofpacts_update_instruction_actions(out
, ofs
);
7411 static char * OVS_WARN_UNUSED_RESULT
7412 parse_WRITE_ACTIONS(char *arg
, const struct ofpact_parse_params
*pp
)
7414 size_t ofs
= ofpacts_pull(pp
->ofpacts
);
7415 struct ofpact_nest
*on
;
7418 /* Add a Write-Actions instruction and then pull it off. */
7419 ofpact_put(pp
->ofpacts
, OFPACT_WRITE_ACTIONS
, sizeof *on
);
7420 ofpbuf_pull(pp
->ofpacts
, sizeof *on
);
7422 /* Parse nested actions.
7424 * We pulled off "write-actions" and the previous actions because the
7425 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
7426 * that it doesn't actually include the nested actions. That means that
7427 * ofpacts_parse() would reject them as being part of an Apply-Actions that
7428 * follows a Write-Actions, which is an invalid order. */
7429 error
= ofpacts_parse(arg
, pp
, false, OFPACT_WRITE_ACTIONS
);
7431 /* Put the Write-Actions back on and update its length. */
7432 on
= ofpbuf_push_uninit(pp
->ofpacts
, sizeof *on
);
7433 on
->ofpact
.len
= pp
->ofpacts
->size
;
7435 /* Put any previous actions or instructions back on. */
7436 ofpbuf_push_uninit(pp
->ofpacts
, ofs
);
7442 format_WRITE_ACTIONS(const struct ofpact_nest
*a
,
7443 const struct ofpact_format_params
*fp
)
7445 ds_put_format(fp
->s
, "%swrite_actions(%s", colors
.paren
, colors
.end
);
7446 ofpacts_format(a
->actions
, ofpact_nest_get_action_len(a
), fp
);
7447 ds_put_format(fp
->s
, "%s)%s", colors
.paren
, colors
.end
);
7451 check_WRITE_ACTIONS(struct ofpact_nest
*a
,
7452 const struct ofpact_check_params
*cp
)
7454 /* Use a temporary copy of 'cp' to avoid updating 'cp->usable_protocols',
7455 * since we can't check consistency of an action set. */
7456 struct ofpact_check_params tmp
= *cp
;
7457 return ofpacts_check(a
->actions
, ofpact_nest_get_action_len(a
), &tmp
);
7460 /* Action structure for NXAST_WRITE_METADATA.
7462 * Modifies the 'mask' bits of the metadata value. */
7463 struct nx_action_write_metadata
{
7464 ovs_be16 type
; /* OFPAT_VENDOR. */
7465 ovs_be16 len
; /* Length is 32. */
7466 ovs_be32 vendor
; /* NX_VENDOR_ID. */
7467 ovs_be16 subtype
; /* NXAST_WRITE_METADATA. */
7468 uint8_t zeros
[6]; /* Must be zero. */
7469 ovs_be64 metadata
; /* Metadata register. */
7470 ovs_be64 mask
; /* Metadata mask. */
7472 OFP_ASSERT(sizeof(struct nx_action_write_metadata
) == 32);
7475 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata
*nawm
,
7476 enum ofp_version ofp_version OVS_UNUSED
,
7479 struct ofpact_metadata
*om
;
7481 if (!is_all_zeros(nawm
->zeros
, sizeof nawm
->zeros
)) {
7482 return OFPERR_NXBRC_MUST_BE_ZERO
;
7485 om
= ofpact_put_WRITE_METADATA(out
);
7486 om
->metadata
= nawm
->metadata
;
7487 om
->mask
= nawm
->mask
;
7493 encode_WRITE_METADATA(const struct ofpact_metadata
*metadata
,
7494 enum ofp_version ofp_version
, struct ofpbuf
*out
)
7496 if (ofp_version
== OFP10_VERSION
) {
7497 struct nx_action_write_metadata
*nawm
;
7499 nawm
= put_NXAST_WRITE_METADATA(out
);
7500 nawm
->metadata
= metadata
->metadata
;
7501 nawm
->mask
= metadata
->mask
;
7503 struct ofp11_instruction_write_metadata
*oiwm
;
7505 oiwm
= instruction_put_OFPIT11_WRITE_METADATA(out
);
7506 oiwm
->metadata
= metadata
->metadata
;
7507 oiwm
->metadata_mask
= metadata
->mask
;
7511 static char * OVS_WARN_UNUSED_RESULT
7512 parse_WRITE_METADATA(char *arg
, const struct ofpact_parse_params
*pp
)
7514 struct ofpact_metadata
*om
;
7515 char *mask
= strchr(arg
, '/');
7517 *pp
->usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
7519 om
= ofpact_put_WRITE_METADATA(pp
->ofpacts
);
7524 error
= str_to_be64(mask
+ 1, &om
->mask
);
7529 om
->mask
= OVS_BE64_MAX
;
7532 return str_to_be64(arg
, &om
->metadata
);
7536 format_WRITE_METADATA(const struct ofpact_metadata
*a
,
7537 const struct ofpact_format_params
*fp
)
7539 ds_put_format(fp
->s
, "%swrite_metadata:%s%#"PRIx64
,
7540 colors
.param
, colors
.end
, ntohll(a
->metadata
));
7541 if (a
->mask
!= OVS_BE64_MAX
) {
7542 ds_put_format(fp
->s
, "/%#"PRIx64
, ntohll(a
->mask
));
7547 check_WRITE_METADATA(const struct ofpact_metadata
*a OVS_UNUSED
,
7548 const struct ofpact_check_params
*cp OVS_UNUSED
)
7553 /* Check packet length action. */
7555 struct nx_action_check_pkt_larger
{
7556 ovs_be16 type
; /* OFPAT_VENDOR. */
7557 ovs_be16 len
; /* 24. */
7558 ovs_be32 vendor
; /* NX_VENDOR_ID. */
7559 ovs_be16 subtype
; /* NXAST_OUTPUT_REG. */
7560 ovs_be16 pkt_len
; /* Length of the packet to check. */
7561 ovs_be16 offset
; /* Result bit offset in destination. */
7563 * - 'dst', as an OXM/NXM header (either 4 or 8 bytes).
7564 * - Enough 0-bytes to pad the action out to 24 bytes. */
7568 OFP_ASSERT(sizeof(struct nx_action_check_pkt_larger
) == 24);
7571 decode_NXAST_RAW_CHECK_PKT_LARGER(
7572 const struct nx_action_check_pkt_larger
*ncpl
,
7573 enum ofp_version ofp_version OVS_UNUSED
,
7574 const struct vl_mff_map
*vl_mff_map
, uint64_t *tlv_bitmap
,
7577 struct ofpact_check_pkt_larger
*check_pkt_larger
;
7580 check_pkt_larger
= ofpact_put_CHECK_PKT_LARGER(out
);
7581 check_pkt_larger
->pkt_len
= ntohs(ncpl
->pkt_len
);
7582 check_pkt_larger
->dst
.ofs
= ntohs(ncpl
->offset
);
7583 check_pkt_larger
->dst
.n_bits
= 1;
7585 struct ofpbuf b
= ofpbuf_const_initializer(ncpl
, ntohs(ncpl
->len
));
7586 ofpbuf_pull(&b
, OBJECT_OFFSETOF(ncpl
, pad
));
7588 error
= mf_vl_mff_nx_pull_header(&b
, vl_mff_map
,
7589 &check_pkt_larger
->dst
.field
,
7595 if (!is_all_zeros(b
.data
, b
.size
)) {
7596 return OFPERR_NXBRC_MUST_BE_ZERO
;
7599 return mf_check_dst(&check_pkt_larger
->dst
, NULL
);
7603 encode_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger
*check_pkt_larger
,
7604 enum ofp_version ofp_version OVS_UNUSED
,
7607 struct nx_action_check_pkt_larger
*ncpl
= put_NXAST_CHECK_PKT_LARGER(out
);
7608 ncpl
->pkt_len
= htons(check_pkt_larger
->pkt_len
);
7609 ncpl
->offset
= htons(check_pkt_larger
->dst
.ofs
);
7611 if (check_pkt_larger
->dst
.field
) {
7612 size_t size
= out
->size
;
7613 out
->size
= size
- sizeof ncpl
->pad
;
7614 nx_put_mff_header(out
, check_pkt_larger
->dst
.field
, 0, false);
7619 static char * OVS_WARN_UNUSED_RESULT
7620 parse_CHECK_PKT_LARGER(char *arg
, const struct ofpact_parse_params
*pp
)
7625 char *error
= set_field_split_str(arg
, &key
, &value
, &delim
);
7631 if (value
[strlen(value
) - 1] == ')') {
7632 value
[strlen(value
) - 1] = '\0';
7634 struct mf_subfield dst
;
7635 error
= mf_parse_subfield(&dst
, key
);
7640 if (dst
.n_bits
!= 1) {
7641 return xstrdup("Only 1-bit destination field is allowed");
7644 struct ofpact_check_pkt_larger
*check_pkt_larger
=
7645 ofpact_put_CHECK_PKT_LARGER(pp
->ofpacts
);
7646 error
= str_to_u16(value
, NULL
, &check_pkt_larger
->pkt_len
);
7650 check_pkt_larger
->dst
= dst
;
7655 format_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger
*a
,
7656 const struct ofpact_format_params
*fp
)
7658 ds_put_format(fp
->s
, "%scheck_pkt_larger(%s%"PRIu32
")->",
7659 colors
.param
, colors
.end
, a
->pkt_len
);
7660 mf_format_subfield(&a
->dst
, fp
->s
);
7664 check_CHECK_PKT_LARGER(const struct ofpact_check_pkt_larger
*a OVS_UNUSED
,
7665 const struct ofpact_check_params
*cp OVS_UNUSED
)
7671 /* Goto-Table instruction. */
7674 encode_GOTO_TABLE(const struct ofpact_goto_table
*goto_table
,
7675 enum ofp_version ofp_version
, struct ofpbuf
*out
)
7677 if (ofp_version
== OFP10_VERSION
) {
7678 struct nx_action_resubmit
*nar
;
7680 nar
= put_NXAST_RESUBMIT_TABLE(out
);
7681 nar
->table
= goto_table
->table_id
;
7682 nar
->in_port
= htons(ofp_to_u16(OFPP_IN_PORT
));
7684 struct ofp11_instruction_goto_table
*oigt
;
7686 oigt
= instruction_put_OFPIT11_GOTO_TABLE(out
);
7687 oigt
->table_id
= goto_table
->table_id
;
7688 memset(oigt
->pad
, 0, sizeof oigt
->pad
);
7692 static char * OVS_WARN_UNUSED_RESULT
7693 parse_GOTO_TABLE(char *arg
, const struct ofpact_parse_params
*pp
)
7695 struct ofpact_goto_table
*ogt
= ofpact_put_GOTO_TABLE(pp
->ofpacts
);
7696 if (!ofputil_table_from_string(arg
, pp
->table_map
, &ogt
->table_id
)) {
7697 return xasprintf("unknown table \"%s\"", arg
);
7703 format_GOTO_TABLE(const struct ofpact_goto_table
*a
,
7704 const struct ofpact_format_params
*fp
)
7706 ds_put_format(fp
->s
, "%sgoto_table:%s", colors
.param
, colors
.end
);
7707 ofputil_format_table(a
->table_id
, fp
->table_map
, fp
->s
);
7711 check_GOTO_TABLE(const struct ofpact_goto_table
*a
,
7712 const struct ofpact_check_params
*cp
)
7714 if ((cp
->table_id
!= 255 && a
->table_id
<= cp
->table_id
)
7715 || (cp
->n_tables
!= 255 && a
->table_id
>= cp
->n_tables
)) {
7716 return OFPERR_OFPBIC_BAD_TABLE_ID
;
7722 log_bad_action(const struct ofp_action_header
*actions
, size_t actions_len
,
7723 const struct ofp_action_header
*bad_action
, enum ofperr error
)
7725 if (!VLOG_DROP_WARN(&rl
)) {
7729 ds_put_hex_dump(&s
, actions
, actions_len
, 0, false);
7730 VLOG_WARN("bad action at offset %#"PRIxPTR
" (%s):\n%s",
7731 (char *)bad_action
- (char *)actions
,
7732 ofperr_get_name(error
), ds_cstr(&s
));
7738 ofpacts_decode(const void *actions
, size_t actions_len
,
7739 enum ofp_version ofp_version
,
7740 const struct vl_mff_map
*vl_mff_map
,
7741 uint64_t *ofpacts_tlv_bitmap
, struct ofpbuf
*ofpacts
)
7743 struct ofpbuf openflow
= ofpbuf_const_initializer(actions
, actions_len
);
7744 while (openflow
.size
) {
7745 const struct ofp_action_header
*action
= openflow
.data
;
7746 enum ofp_raw_action_type raw
;
7750 error
= ofpact_pull_raw(&openflow
, ofp_version
, &raw
, &arg
);
7752 error
= ofpact_decode(action
, raw
, ofp_version
, arg
, vl_mff_map
,
7753 ofpacts_tlv_bitmap
, ofpacts
);
7757 log_bad_action(actions
, actions_len
, action
, error
);
7765 ofpacts_pull_openflow_actions__(struct ofpbuf
*openflow
,
7766 unsigned int actions_len
,
7767 enum ofp_version version
,
7768 uint32_t allowed_ovsinsts
,
7769 struct ofpbuf
*ofpacts
,
7770 enum ofpact_type outer_action
,
7771 const struct vl_mff_map
*vl_mff_map
,
7772 uint64_t *ofpacts_tlv_bitmap
)
7774 const struct ofp_action_header
*actions
;
7777 if (actions_len
% OFP_ACTION_ALIGN
!= 0) {
7778 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u is not a "
7779 "multiple of %d", actions_len
, OFP_ACTION_ALIGN
);
7780 return OFPERR_OFPBRC_BAD_LEN
;
7783 actions
= ofpbuf_try_pull(openflow
, actions_len
);
7784 if (actions
== NULL
) {
7785 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u exceeds "
7786 "remaining message length (%"PRIu32
")",
7787 actions_len
, openflow
->size
);
7788 return OFPERR_OFPBRC_BAD_LEN
;
7791 error
= ofpacts_decode(actions
, actions_len
, version
, vl_mff_map
,
7792 ofpacts_tlv_bitmap
, ofpacts
);
7794 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
, version
,
7795 allowed_ovsinsts
, outer_action
, NULL
);
7798 ofpbuf_clear(ofpacts
);
7803 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
7804 * of 'openflow' into ofpacts. On success, appends the converted actions to
7805 * 'ofpacts'; on failure, clears 'ofpacts'. Returns 0 if successful, otherwise
7806 * an OpenFlow error.
7808 * Actions are processed according to their OpenFlow version which
7809 * is provided in the 'version' parameter.
7811 * In most places in OpenFlow, actions appear encapsulated in instructions, so
7812 * you should call ofpacts_pull_openflow_instructions() instead of this
7815 * 'vl_mff_map' and 'ofpacts_tlv_bitmap' are optional. If 'vl_mff_map' is
7816 * provided, it is used to get variable length mf_fields with configured
7817 * length in the actions. If an action uses a variable length mf_field,
7818 * 'ofpacts_tlv_bitmap' is updated accordingly for ref counting. If
7819 * 'vl_mff_map' is not provided, the default mf_fields with maximum length
7822 * The parsed actions are valid generically, but they may not be valid in a
7823 * specific context. For example, port numbers up to OFPP_MAX are valid
7824 * generically, but specific datapaths may only support port numbers in a
7825 * smaller range. Use ofpacts_check() to additional check whether actions are
7826 * valid in a specific context. */
7828 ofpacts_pull_openflow_actions(struct ofpbuf
*openflow
,
7829 unsigned int actions_len
,
7830 enum ofp_version version
,
7831 const struct vl_mff_map
*vl_mff_map
,
7832 uint64_t *ofpacts_tlv_bitmap
,
7833 struct ofpbuf
*ofpacts
)
7835 return ofpacts_pull_openflow_actions__(
7836 openflow
, actions_len
, version
,
7837 (1u << OVSINST_OFPIT11_APPLY_ACTIONS
) | (1u << OVSINST_OFPIT13_METER
),
7838 ofpacts
, 0, vl_mff_map
, ofpacts_tlv_bitmap
);
7841 /* OpenFlow 1.1 action sets. */
7843 /* Append ofpact 'a' onto the tail of 'out' */
7845 ofpact_copy(struct ofpbuf
*out
, const struct ofpact
*a
)
7847 ofpbuf_put(out
, a
, OFPACT_ALIGN(a
->len
));
7850 /* The order in which actions in an action set get executed. This is only for
7851 * the actions where only the last instance added is used. */
7852 #define ACTION_SET_ORDER \
7853 SLOT(OFPACT_STRIP_VLAN) \
7854 SLOT(OFPACT_POP_MPLS) \
7855 SLOT(OFPACT_DECAP) \
7856 SLOT(OFPACT_ENCAP) \
7857 SLOT(OFPACT_PUSH_MPLS) \
7858 SLOT(OFPACT_PUSH_VLAN) \
7859 SLOT(OFPACT_DEC_TTL) \
7860 SLOT(OFPACT_DEC_MPLS_TTL) \
7861 SLOT(OFPACT_DEC_NSH_TTL)
7863 /* Priority for "final actions" in an action set. An action set only gets
7864 * executed at all if at least one of these actions is present. If more than
7865 * one is present, then only the one later in this list is executed (and if
7866 * more than one of a given type, the one later in the action set). */
7867 #define ACTION_SET_FINAL_PRIORITY \
7869 FINAL(OFPACT_CT_CLEAR) \
7870 FINAL(OFPACT_RESUBMIT) \
7871 FINAL(OFPACT_OUTPUT) \
7874 enum action_set_class
{
7875 /* Actions that individually can usefully appear only once in an action
7876 * set. If they do appear more than once, then only the last instance is
7878 #define SLOT(OFPACT) ACTION_SLOT_##OFPACT,
7882 /* Final actions. */
7883 #define FINAL(OFPACT) ACTION_SLOT_##OFPACT,
7884 ACTION_SET_FINAL_PRIORITY
7887 /* Actions that can appear in an action set more than once and are executed
7889 ACTION_SLOT_SET_OR_MOVE
,
7891 /* Actions that shouldn't appear in the action set at all. */
7895 /* Count the action set slots. */
7896 #define SLOT(OFPACT) +1
7897 enum { N_ACTION_SLOTS
= ACTION_SET_ORDER
};
7900 static enum action_set_class
7901 action_set_classify(const struct ofpact
*a
)
7904 #define SLOT(OFPACT) case OFPACT: return ACTION_SLOT_##OFPACT;
7908 #define FINAL(OFPACT) case OFPACT: return ACTION_SLOT_##OFPACT;
7909 ACTION_SET_FINAL_PRIORITY
7912 case OFPACT_SET_FIELD
:
7913 case OFPACT_REG_MOVE
:
7914 case OFPACT_SET_ETH_DST
:
7915 case OFPACT_SET_ETH_SRC
:
7916 case OFPACT_SET_IP_DSCP
:
7917 case OFPACT_SET_IP_ECN
:
7918 case OFPACT_SET_IP_TTL
:
7919 case OFPACT_SET_IPV4_DST
:
7920 case OFPACT_SET_IPV4_SRC
:
7921 case OFPACT_SET_L4_DST_PORT
:
7922 case OFPACT_SET_L4_SRC_PORT
:
7923 case OFPACT_SET_MPLS_LABEL
:
7924 case OFPACT_SET_MPLS_TC
:
7925 case OFPACT_SET_MPLS_TTL
:
7926 case OFPACT_SET_QUEUE
:
7927 case OFPACT_SET_TUNNEL
:
7928 case OFPACT_SET_VLAN_PCP
:
7929 case OFPACT_SET_VLAN_VID
:
7930 return ACTION_SLOT_SET_OR_MOVE
;
7933 case OFPACT_CLEAR_ACTIONS
:
7936 case OFPACT_CONTROLLER
:
7937 case OFPACT_ENQUEUE
:
7939 case OFPACT_UNROLL_XLATE
:
7940 case OFPACT_FIN_TIMEOUT
:
7941 case OFPACT_GOTO_TABLE
:
7943 case OFPACT_CONJUNCTION
:
7945 case OFPACT_MULTIPATH
:
7947 case OFPACT_OUTPUT_REG
:
7948 case OFPACT_OUTPUT_TRUNC
:
7949 case OFPACT_POP_QUEUE
:
7951 case OFPACT_STACK_POP
:
7952 case OFPACT_STACK_PUSH
:
7953 case OFPACT_WRITE_ACTIONS
:
7954 case OFPACT_WRITE_METADATA
:
7955 case OFPACT_DEBUG_RECIRC
:
7956 case OFPACT_DEBUG_SLOW
:
7957 case OFPACT_CHECK_PKT_LARGER
:
7958 case OFPACT_DELETE_FIELD
:
7959 return ACTION_SLOT_INVALID
;
7966 /* True if an action is allowed in the action set.
7967 * False otherwise. */
7969 ofpact_is_allowed_in_actions_set(const struct ofpact
*a
)
7971 return action_set_classify(a
) != ACTION_SLOT_INVALID
;
7974 /* Reads 'action_set', which contains ofpacts accumulated by
7975 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
7976 * executed directly into 'action_list'. (These names correspond to the
7977 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
7979 * In general this involves appending the last instance of each action that is
7980 * admissible in the action set in the order described in the OpenFlow
7984 * + output action is only appended if no group action was present in 'in'.
7985 * + As a simplification all set actions are copied in the order the are
7986 * provided in 'in' as many set actions applied to a field has the same
7987 * affect as only applying the last action that sets a field and
7988 * duplicates are removed by do_xlate_actions().
7989 * This has an unwanted side-effect of compsoting multiple
7990 * LOAD_REG actions that touch different regions of the same field. */
7992 ofpacts_execute_action_set(struct ofpbuf
*action_list
,
7993 const struct ofpbuf
*action_set
)
7995 const struct ofpact
*slots
[N_ACTION_SLOTS
] = {NULL
, };
7997 struct ofpbuf set_or_move
;
7998 ofpbuf_init(&set_or_move
, 0);
8000 const struct ofpact
*final_action
= NULL
;
8001 enum action_set_class final_class
= 0;
8003 const struct ofpact
*cursor
;
8004 OFPACT_FOR_EACH (cursor
, action_set
->data
, action_set
->size
) {
8005 int class = action_set_classify(cursor
);
8006 if (class < N_ACTION_SLOTS
) {
8007 slots
[class] = cursor
;
8008 } else if (class < ACTION_SLOT_SET_OR_MOVE
) {
8009 if (class >= final_class
) {
8010 final_action
= cursor
;
8011 final_class
= class;
8013 } else if (class == ACTION_SLOT_SET_OR_MOVE
) {
8014 ofpact_copy(&set_or_move
, cursor
);
8016 ovs_assert(class == ACTION_SLOT_INVALID
);
8021 for (int i
= 0; i
< N_ACTION_SLOTS
; i
++) {
8023 ofpact_copy(action_list
, slots
[i
]);
8026 ofpbuf_put(action_list
, set_or_move
.data
, set_or_move
.size
);
8027 ofpact_copy(action_list
, final_action
);
8029 ofpbuf_uninit(&set_or_move
);
8034 ofpacts_decode_for_action_set(const struct ofp_action_header
*in
,
8035 size_t n_in
, enum ofp_version version
,
8036 const struct vl_mff_map
*vl_mff_map
,
8037 uint64_t *ofpacts_tlv_bitmap
,
8042 size_t start
= out
->size
;
8044 error
= ofpacts_decode(in
, n_in
, version
, vl_mff_map
, ofpacts_tlv_bitmap
,
8051 OFPACT_FOR_EACH (a
, ofpact_end(out
->data
, start
), out
->size
- start
) {
8052 if (!ofpact_is_allowed_in_actions_set(a
)) {
8053 VLOG_WARN_RL(&rl
, "disallowed action in action set");
8054 return OFPERR_OFPBAC_BAD_TYPE
;
8061 /* OpenFlow 1.1 instructions. */
8063 struct instruction_type_info
{
8064 enum ovs_instruction_type type
;
8068 static const struct instruction_type_info inst_info
[] = {
8069 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
8075 ovs_instruction_name_from_type(enum ovs_instruction_type type
)
8077 return type
< ARRAY_SIZE(inst_info
) ? inst_info
[type
].name
: NULL
;
8081 ovs_instruction_type_from_name(const char *name
)
8083 const struct instruction_type_info
*p
;
8084 for (p
= inst_info
; p
< &inst_info
[ARRAY_SIZE(inst_info
)]; p
++) {
8085 if (!strcasecmp(name
, p
->name
)) {
8092 enum ovs_instruction_type
8093 ovs_instruction_type_from_ofpact_type(enum ofpact_type type
,
8094 enum ofp_version version
)
8098 return (version
>= OFP15_VERSION
8099 ? OVSINST_OFPIT11_APPLY_ACTIONS
8100 : OVSINST_OFPIT13_METER
);
8101 case OFPACT_CLEAR_ACTIONS
:
8102 return OVSINST_OFPIT11_CLEAR_ACTIONS
;
8103 case OFPACT_WRITE_ACTIONS
:
8104 return OVSINST_OFPIT11_WRITE_ACTIONS
;
8105 case OFPACT_WRITE_METADATA
:
8106 return OVSINST_OFPIT11_WRITE_METADATA
;
8107 case OFPACT_GOTO_TABLE
:
8108 return OVSINST_OFPIT11_GOTO_TABLE
;
8112 case OFPACT_CONTROLLER
:
8113 case OFPACT_ENQUEUE
:
8114 case OFPACT_OUTPUT_REG
:
8115 case OFPACT_OUTPUT_TRUNC
:
8117 case OFPACT_SET_VLAN_VID
:
8118 case OFPACT_SET_VLAN_PCP
:
8119 case OFPACT_STRIP_VLAN
:
8120 case OFPACT_PUSH_VLAN
:
8121 case OFPACT_SET_ETH_SRC
:
8122 case OFPACT_SET_ETH_DST
:
8123 case OFPACT_SET_IPV4_SRC
:
8124 case OFPACT_SET_IPV4_DST
:
8125 case OFPACT_SET_IP_DSCP
:
8126 case OFPACT_SET_IP_ECN
:
8127 case OFPACT_SET_IP_TTL
:
8128 case OFPACT_SET_L4_SRC_PORT
:
8129 case OFPACT_SET_L4_DST_PORT
:
8130 case OFPACT_REG_MOVE
:
8131 case OFPACT_SET_FIELD
:
8132 case OFPACT_STACK_PUSH
:
8133 case OFPACT_STACK_POP
:
8134 case OFPACT_DEC_TTL
:
8135 case OFPACT_SET_MPLS_LABEL
:
8136 case OFPACT_SET_MPLS_TC
:
8137 case OFPACT_SET_MPLS_TTL
:
8138 case OFPACT_DEC_MPLS_TTL
:
8139 case OFPACT_PUSH_MPLS
:
8140 case OFPACT_POP_MPLS
:
8141 case OFPACT_SET_TUNNEL
:
8142 case OFPACT_SET_QUEUE
:
8143 case OFPACT_POP_QUEUE
:
8144 case OFPACT_FIN_TIMEOUT
:
8145 case OFPACT_RESUBMIT
:
8147 case OFPACT_CONJUNCTION
:
8148 case OFPACT_MULTIPATH
:
8151 case OFPACT_UNROLL_XLATE
:
8153 case OFPACT_DEBUG_RECIRC
:
8154 case OFPACT_DEBUG_SLOW
:
8156 case OFPACT_CT_CLEAR
:
8160 case OFPACT_DEC_NSH_TTL
:
8161 case OFPACT_CHECK_PKT_LARGER
:
8162 case OFPACT_DELETE_FIELD
:
8164 return OVSINST_OFPIT11_APPLY_ACTIONS
;
8169 ovs_instruction_type_from_inst_type(enum ovs_instruction_type
*instruction_type
,
8170 const uint16_t inst_type
)
8172 switch (inst_type
) {
8174 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
8176 *instruction_type = OVSINST_##ENUM; \
8182 return OFPERR_OFPBIC_UNKNOWN_INST
;
8186 /* Two-way translation between OVS's internal "OVSINST_*" representation of
8187 * instructions and the "OFPIT_*" representation used in OpenFlow. */
8188 struct ovsinst_map
{
8189 enum ovs_instruction_type ovsinst
; /* Internal name for instruction. */
8190 int ofpit
; /* OFPIT_* number from OpenFlow spec. */
8193 static const struct ovsinst_map
*
8194 get_ovsinst_map(enum ofp_version version
)
8196 /* OpenFlow 1.1, 1.2, and 1.5 instructions. */
8197 static const struct ovsinst_map of11
[] = {
8198 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
8199 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
8200 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
8201 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
8202 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
8206 /* OpenFlow 1.3 and 1.4 instructions. */
8207 static const struct ovsinst_map of13
[] = {
8208 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
8209 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
8210 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
8211 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
8212 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
8213 { OVSINST_OFPIT13_METER
, 6 },
8217 return version
== OFP13_VERSION
|| version
== OFP14_VERSION
? of13
: of11
;
8220 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
8221 * values, into a bitmap of instructions suitable for OpenFlow 'version'
8222 * (OFP11_VERSION or later), and returns the result. */
8224 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap
, enum ofp_version version
)
8226 uint32_t ofpit_bitmap
= 0;
8227 const struct ovsinst_map
*x
;
8229 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
8230 if (ovsinst_bitmap
& (1u << x
->ovsinst
)) {
8231 ofpit_bitmap
|= 1u << x
->ofpit
;
8234 return htonl(ofpit_bitmap
);
8237 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
8238 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
8239 * correspond to OVSINST_* values, and returns the result. */
8241 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap
, enum ofp_version version
)
8243 uint32_t ovsinst_bitmap
= 0;
8244 const struct ovsinst_map
*x
;
8246 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
8247 if (ofpit_bitmap
& htonl(1u << x
->ofpit
)) {
8248 ovsinst_bitmap
|= 1u << x
->ovsinst
;
8251 return ovsinst_bitmap
;
8254 static inline struct ofp11_instruction
*
8255 instruction_next(const struct ofp11_instruction
*inst
)
8257 return ((struct ofp11_instruction
*) (void *)
8258 ((uint8_t *) inst
+ ntohs(inst
->len
)));
8262 instruction_is_valid(const struct ofp11_instruction
*inst
,
8263 size_t n_instructions
)
8265 uint16_t len
= ntohs(inst
->len
);
8266 return (!(len
% OFP11_INSTRUCTION_ALIGN
)
8267 && len
>= sizeof *inst
8268 && len
/ sizeof *inst
<= n_instructions
);
8271 /* This macro is careful to check for instructions with bad lengths. */
8272 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
8273 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
8274 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
8275 ((LEFT) -= (ntohs((ITER)->len) \
8276 / sizeof(struct ofp11_instruction)), \
8277 (ITER) = instruction_next(ITER)))
8280 decode_openflow11_instruction(const struct ofp11_instruction
*inst
,
8281 enum ovs_instruction_type
*type
)
8283 uint16_t len
= ntohs(inst
->len
);
8285 switch (inst
->type
) {
8286 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER
):
8287 return OFPERR_OFPBIC_BAD_EXPERIMENTER
;
8289 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
8290 case CONSTANT_HTONS(ENUM): \
8292 ? len >= sizeof(struct STRUCT) \
8293 : len == sizeof(struct STRUCT)) { \
8294 *type = OVSINST_##ENUM; \
8297 return OFPERR_OFPBIC_BAD_LEN; \
8303 return OFPERR_OFPBIC_UNKNOWN_INST
;
8308 decode_openflow11_instructions(const struct ofp11_instruction insts
[],
8309 size_t n_insts
, enum ofp_version version
,
8310 const struct ofp11_instruction
*out
[])
8312 const struct ofp11_instruction
*inst
;
8315 memset(out
, 0, N_OVS_INSTRUCTIONS
* sizeof *out
);
8316 INSTRUCTION_FOR_EACH (inst
, left
, insts
, n_insts
) {
8317 enum ovs_instruction_type type
;
8320 error
= decode_openflow11_instruction(inst
, &type
);
8325 if (type
== OVSINST_OFPIT13_METER
&& version
>= OFP15_VERSION
) {
8326 /* "meter" is an action, not an instruction, in OpenFlow 1.5. */
8327 return OFPERR_OFPBIC_UNKNOWN_INST
;
8331 return OFPERR_OFPBIC_DUP_INST
;
8337 VLOG_WARN_RL(&rl
, "bad instruction format at offset %"PRIuSIZE
,
8338 (n_insts
- left
) * sizeof *inst
);
8339 return OFPERR_OFPBIC_BAD_LEN
;
8345 get_actions_from_instruction(const struct ofp11_instruction
*inst
,
8346 const struct ofp_action_header
**actions
,
8347 size_t *actions_len
)
8349 *actions
= ALIGNED_CAST(const struct ofp_action_header
*, inst
+ 1);
8350 *actions_len
= ntohs(inst
->len
) - sizeof *inst
;
8354 ofpacts_pull_openflow_instructions(struct ofpbuf
*openflow
,
8355 unsigned int instructions_len
,
8356 enum ofp_version version
,
8357 const struct vl_mff_map
*vl_mff_map
,
8358 uint64_t *ofpacts_tlv_bitmap
,
8359 struct ofpbuf
*ofpacts
)
8361 const struct ofp11_instruction
*instructions
;
8362 const struct ofp11_instruction
*insts
[N_OVS_INSTRUCTIONS
];
8365 ofpbuf_clear(ofpacts
);
8366 if (version
== OFP10_VERSION
) {
8367 return ofpacts_pull_openflow_actions__(openflow
, instructions_len
,
8369 (1u << N_OVS_INSTRUCTIONS
) - 1,
8370 ofpacts
, 0, vl_mff_map
,
8371 ofpacts_tlv_bitmap
);
8374 if (instructions_len
% OFP11_INSTRUCTION_ALIGN
!= 0) {
8375 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u is not a "
8377 instructions_len
, OFP11_INSTRUCTION_ALIGN
);
8378 error
= OFPERR_OFPBIC_BAD_LEN
;
8382 instructions
= ofpbuf_try_pull(openflow
, instructions_len
);
8383 if (instructions
== NULL
) {
8384 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u exceeds "
8385 "remaining message length (%"PRIu32
")",
8386 instructions_len
, openflow
->size
);
8387 error
= OFPERR_OFPBIC_BAD_LEN
;
8391 error
= decode_openflow11_instructions(
8392 instructions
, instructions_len
/ OFP11_INSTRUCTION_ALIGN
, version
,
8398 if (insts
[OVSINST_OFPIT13_METER
]) {
8399 const struct ofp13_instruction_meter
*oim
;
8400 struct ofpact_meter
*om
;
8402 oim
= ALIGNED_CAST(const struct ofp13_instruction_meter
*,
8403 insts
[OVSINST_OFPIT13_METER
]);
8405 om
= ofpact_put_METER(ofpacts
);
8406 om
->meter_id
= ntohl(oim
->meter_id
);
8407 om
->provider_meter_id
= UINT32_MAX
; /* No provider meter ID. */
8409 if (insts
[OVSINST_OFPIT11_APPLY_ACTIONS
]) {
8410 const struct ofp_action_header
*actions
;
8413 get_actions_from_instruction(insts
[OVSINST_OFPIT11_APPLY_ACTIONS
],
8414 &actions
, &actions_len
);
8415 error
= ofpacts_decode(actions
, actions_len
, version
, vl_mff_map
,
8416 ofpacts_tlv_bitmap
, ofpacts
);
8421 if (insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]) {
8422 instruction_get_OFPIT11_CLEAR_ACTIONS(
8423 insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]);
8424 ofpact_put_CLEAR_ACTIONS(ofpacts
);
8426 if (insts
[OVSINST_OFPIT11_WRITE_ACTIONS
]) {
8427 struct ofpact_nest
*on
;
8428 const struct ofp_action_header
*actions
;
8430 size_t start
= ofpacts
->size
;
8431 ofpact_put(ofpacts
, OFPACT_WRITE_ACTIONS
,
8432 offsetof(struct ofpact_nest
, actions
));
8433 get_actions_from_instruction(insts
[OVSINST_OFPIT11_WRITE_ACTIONS
],
8434 &actions
, &actions_len
);
8435 error
= ofpacts_decode_for_action_set(actions
, actions_len
,
8436 version
, vl_mff_map
,
8437 ofpacts_tlv_bitmap
, ofpacts
);
8441 on
= ofpbuf_at_assert(ofpacts
, start
, sizeof *on
);
8442 on
->ofpact
.len
= ofpacts
->size
- start
;
8444 if (insts
[OVSINST_OFPIT11_WRITE_METADATA
]) {
8445 const struct ofp11_instruction_write_metadata
*oiwm
;
8446 struct ofpact_metadata
*om
;
8448 oiwm
= ALIGNED_CAST(const struct ofp11_instruction_write_metadata
*,
8449 insts
[OVSINST_OFPIT11_WRITE_METADATA
]);
8451 om
= ofpact_put_WRITE_METADATA(ofpacts
);
8452 om
->metadata
= oiwm
->metadata
;
8453 om
->mask
= oiwm
->metadata_mask
;
8455 if (insts
[OVSINST_OFPIT11_GOTO_TABLE
]) {
8456 const struct ofp11_instruction_goto_table
*oigt
;
8457 struct ofpact_goto_table
*ogt
;
8459 oigt
= instruction_get_OFPIT11_GOTO_TABLE(
8460 insts
[OVSINST_OFPIT11_GOTO_TABLE
]);
8461 ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
8462 ogt
->table_id
= oigt
->table_id
;
8465 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
, version
,
8466 (1u << N_OVS_INSTRUCTIONS
) - 1, 0, NULL
);
8469 ofpbuf_clear(ofpacts
);
8474 /* Update the length of the instruction that begins at offset 'ofs' within
8475 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
8476 * If the instruction contains no nested actions, deletes it entirely. */
8478 ofpacts_update_instruction_actions(struct ofpbuf
*openflow
, size_t ofs
)
8480 struct ofp11_instruction_actions
*oia
;
8482 oia
= ofpbuf_at_assert(openflow
, ofs
, sizeof *oia
);
8483 if (openflow
->size
> ofs
+ sizeof *oia
) {
8484 oia
->len
= htons(openflow
->size
- ofs
);
8486 openflow
->size
= ofs
;
8490 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
8491 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
8492 * valid, otherwise an OpenFlow error code. */
8494 ofpact_check_output_port(ofp_port_t port
, ofp_port_t max_ports
)
8502 case OFPP_CONTROLLER
:
8507 return OFPERR_OFPBAC_BAD_OUT_PORT
;
8510 if (ofp_to_u16(port
) < ofp_to_u16(max_ports
)) {
8513 return OFPERR_OFPBAC_BAD_OUT_PORT
;
8517 /* Removes the protocols that require consistency between match and actions
8518 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
8520 * (An example of an inconsistency between match and actions is a flow that
8521 * does not match on an MPLS Ethertype but has an action that pops an MPLS
8524 inconsistent_match(enum ofputil_protocol
*usable_protocols
)
8526 *usable_protocols
&= OFPUTIL_P_OF10_ANY
;
8529 /* May modify flow->packet_type, flow->dl_type, flow->nw_proto and
8530 * flow->vlan_tci, caller must restore them.
8532 * Modifies some actions, filling in fields that could not be properly set
8533 * without context. */
8535 ofpact_check__(struct ofpact
*a
, struct ofpact_check_params
*cp
)
8538 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
8539 case OFPACT_##ENUM: \
8540 return check_##ENUM(ofpact_get_##ENUM(a), cp);
8548 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
8549 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
8550 * switch with no more than 'max_ports' ports.
8552 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
8553 * '*usable_protocols' the protocols that forbid the inconsistency. (An
8554 * example of an inconsistency between match and actions is a flow that does
8555 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
8557 * May annotate ofpacts with information gathered from the 'match'.
8559 * May temporarily modify 'match', but restores the changes before
8562 ofpacts_check(struct ofpact ofpacts
[], size_t ofpacts_len
,
8563 struct ofpact_check_params
*cp
)
8565 /* Save fields that might temporarily be modified. */
8566 struct flow
*flow
= &cp
->match
->flow
;
8567 ovs_be32 packet_type
= flow
->packet_type
;
8568 ovs_be16 dl_type
= flow
->dl_type
;
8569 uint8_t nw_proto
= flow
->nw_proto
;
8570 union flow_vlan_hdr vlans
[FLOW_MAX_VLAN_HEADERS
];
8571 memcpy(vlans
, flow
->vlans
, sizeof vlans
);
8573 /* Check all the actions. */
8574 cp
->usable_protocols
= OFPUTIL_P_ANY
;
8575 enum ofperr error
= 0;
8577 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8578 error
= ofpact_check__(a
, cp
);
8584 /* Restore fields that may have been modified. */
8585 flow
->packet_type
= packet_type
;
8586 flow
->dl_type
= dl_type
;
8587 memcpy(flow
->vlans
, vlans
, sizeof vlans
);
8588 flow
->nw_proto
= nw_proto
;
8593 /* Like ofpacts_check(), but reports inconsistencies as
8594 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
8596 ofpacts_check_consistency(struct ofpact ofpacts
[], size_t ofpacts_len
,
8597 enum ofputil_protocol needed_protocols
,
8598 struct ofpact_check_params
*cp
)
8600 enum ofperr error
= ofpacts_check(ofpacts
, ofpacts_len
, cp
);
8601 if (!error
&& needed_protocols
& ~cp
->usable_protocols
) {
8602 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
8607 /* Returns the destination field that 'ofpact' would write to, or NULL
8608 * if the action would not write to an mf_field. */
8609 const struct mf_field
*
8610 ofpact_get_mf_dst(const struct ofpact
*ofpact
)
8612 if (ofpact
->type
== OFPACT_SET_FIELD
) {
8613 const struct ofpact_set_field
*orl
;
8615 orl
= CONTAINER_OF(ofpact
, struct ofpact_set_field
, ofpact
);
8617 } else if (ofpact
->type
== OFPACT_REG_MOVE
) {
8618 const struct ofpact_reg_move
*orm
;
8620 orm
= CONTAINER_OF(ofpact
, struct ofpact_reg_move
, ofpact
);
8621 return orm
->dst
.field
;
8627 static void OVS_PRINTF_FORMAT(2, 3)
8628 verify_error(char **errorp
, const char *format
, ...)
8631 va_start(args
, format
);
8632 char *error
= xvasprintf(format
, args
);
8638 VLOG_WARN("%s", error
);
8644 unsupported_nesting(enum ofpact_type action
, enum ofpact_type outer_action
,
8647 verify_error(errorp
, "%s action doesn't support nested action %s",
8648 ofpact_name(outer_action
), ofpact_name(action
));
8649 return OFPERR_OFPBAC_BAD_ARGUMENT
;
8653 field_requires_ct(enum mf_field_id field
)
8655 return field
== MFF_CT_MARK
|| field
== MFF_CT_LABEL
;
8658 /* Apply nesting constraints for actions */
8660 ofpacts_verify_nested(const struct ofpact
*a
, enum ofpact_type outer_action
,
8663 const struct mf_field
*field
= ofpact_get_mf_dst(a
);
8665 if (field
&& field_requires_ct(field
->id
) && outer_action
!= OFPACT_CT
) {
8666 verify_error(errorp
, "cannot set CT fields outside of ct action");
8667 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
8669 if (a
->type
== OFPACT_NAT
) {
8670 if (outer_action
!= OFPACT_CT
) {
8671 verify_error(errorp
,
8672 "Cannot have NAT action outside of \"ct\" action");
8673 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
8679 ovs_assert(outer_action
== OFPACT_WRITE_ACTIONS
8680 || outer_action
== OFPACT_CT
8681 || outer_action
== OFPACT_CLONE
);
8683 if (outer_action
== OFPACT_CT
) {
8685 return unsupported_nesting(a
->type
, outer_action
, errorp
);
8686 } else if (!field_requires_ct(field
->id
)) {
8687 verify_error(errorp
,
8688 "%s action doesn't support nested modification "
8689 "of %s", ofpact_name(outer_action
), field
->name
);
8690 return OFPERR_OFPBAC_BAD_ARGUMENT
;
8694 if (a
->type
== OFPACT_METER
) {
8695 return unsupported_nesting(a
->type
, outer_action
, errorp
);
8702 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
8703 * appropriate order as defined by the OpenFlow spec and as required by Open
8706 * The 'version' is relevant only for error reporting: Open vSwitch enforces
8707 * the same rules for every version of OpenFlow, but different versions require
8708 * different error codes.
8710 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
8711 * instructions that are allowed within 'ofpacts[]'.
8713 * If 'outer_action' is not zero, it specifies that the actions are nested
8714 * within another action of type 'outer_action'. */
8716 ofpacts_verify(const struct ofpact ofpacts
[], size_t ofpacts_len
,
8717 enum ofp_version version
, uint32_t allowed_ovsinsts
,
8718 enum ofpact_type outer_action
, char **errorp
)
8720 const struct ofpact
*a
;
8721 enum ovs_instruction_type inst
;
8723 inst
= OVSINST_OFPIT13_METER
;
8724 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8725 enum ovs_instruction_type next
;
8728 if (a
->type
== OFPACT_CONJUNCTION
) {
8729 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8730 if (a
->type
!= OFPACT_CONJUNCTION
&& a
->type
!= OFPACT_NOTE
) {
8731 verify_error(errorp
, "\"conjunction\" actions may be used "
8732 "along with \"note\" but not any other kind "
8733 "of action (such as the \"%s\" action used "
8734 "here)", ofpact_name(a
->type
));
8735 return OFPERR_NXBAC_BAD_CONJUNCTION
;
8741 error
= ofpacts_verify_nested(a
, outer_action
, errorp
);
8746 next
= ovs_instruction_type_from_ofpact_type(a
->type
, version
);
8748 && (inst
== OVSINST_OFPIT11_APPLY_ACTIONS
8751 const char *name
= ovs_instruction_name_from_type(inst
);
8752 const char *next_name
= ovs_instruction_name_from_type(next
);
8755 verify_error(errorp
, "duplicate %s instruction not allowed, "
8756 "for OpenFlow 1.1+ compatibility", name
);
8758 verify_error(errorp
, "invalid instruction ordering: "
8759 "%s must appear before %s, "
8760 "for OpenFlow 1.1+ compatibility",
8763 return OFPERR_OFPBAC_UNSUPPORTED_ORDER
;
8765 if (!((1u << next
) & allowed_ovsinsts
)) {
8766 const char *name
= ovs_instruction_name_from_type(next
);
8768 if (next
== OVSINST_OFPIT13_METER
&& version
>= OFP15_VERSION
) {
8769 verify_error(errorp
, "%s action not allowed here", name
);
8770 return OFPERR_OFPBAC_BAD_TYPE
;
8772 verify_error(errorp
, "%s instruction not allowed here", name
);
8773 return OFPERR_OFPBIC_UNSUP_INST
;
8783 /* Converting ofpacts to OpenFlow. */
8786 encode_ofpact(const struct ofpact
*a
, enum ofp_version ofp_version
,
8790 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
8791 case OFPACT_##ENUM: \
8792 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
8801 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
8802 * actions in 'openflow', appending the actions to any existing data in
8805 ofpacts_put_openflow_actions(const struct ofpact ofpacts
[], size_t ofpacts_len
,
8806 struct ofpbuf
*openflow
,
8807 enum ofp_version ofp_version
)
8809 const struct ofpact
*a
;
8810 size_t start_size
= openflow
->size
;
8812 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
8813 encode_ofpact(a
, ofp_version
, openflow
);
8815 return openflow
->size
- start_size
;
8818 static enum ovs_instruction_type
8819 ofpact_is_apply_actions(const struct ofpact
*a
, enum ofp_version version
)
8821 return (ovs_instruction_type_from_ofpact_type(a
->type
, version
)
8822 == OVSINST_OFPIT11_APPLY_ACTIONS
);
8826 ofpacts_put_openflow_instructions(const struct ofpact ofpacts
[],
8828 struct ofpbuf
*openflow
,
8829 enum ofp_version ofp_version
)
8831 const struct ofpact
*end
= ofpact_end(ofpacts
, ofpacts_len
);
8832 const struct ofpact
*a
;
8834 if (ofp_version
== OFP10_VERSION
) {
8835 ofpacts_put_openflow_actions(ofpacts
, ofpacts_len
, openflow
,
8842 if (ofpact_is_apply_actions(a
, ofp_version
)) {
8843 size_t ofs
= openflow
->size
;
8845 instruction_put_OFPIT11_APPLY_ACTIONS(openflow
);
8847 encode_ofpact(a
, ofp_version
, openflow
);
8849 } while (a
< end
&& ofpact_is_apply_actions(a
, ofp_version
));
8850 ofpacts_update_instruction_actions(openflow
, ofs
);
8852 encode_ofpact(a
, ofp_version
, openflow
);
8858 /* Sets of supported actions. */
8860 /* Two-way translation between OVS's internal "OFPACT_*" representation of
8861 * actions and the "OFPAT_*" representation used in some OpenFlow version.
8862 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
8863 * instance is specific to one OpenFlow version.) */
8865 enum ofpact_type ofpact
; /* Internal name for action type. */
8866 int ofpat
; /* OFPAT_* number from OpenFlow spec. */
8869 static const struct ofpact_map
*
8870 get_ofpact_map(enum ofp_version version
)
8872 /* OpenFlow 1.0 actions. */
8873 static const struct ofpact_map of10
[] = {
8874 { OFPACT_OUTPUT
, 0 },
8875 { OFPACT_SET_VLAN_VID
, 1 },
8876 { OFPACT_SET_VLAN_PCP
, 2 },
8877 { OFPACT_STRIP_VLAN
, 3 },
8878 { OFPACT_SET_ETH_SRC
, 4 },
8879 { OFPACT_SET_ETH_DST
, 5 },
8880 { OFPACT_SET_IPV4_SRC
, 6 },
8881 { OFPACT_SET_IPV4_DST
, 7 },
8882 { OFPACT_SET_IP_DSCP
, 8 },
8883 { OFPACT_SET_L4_SRC_PORT
, 9 },
8884 { OFPACT_SET_L4_DST_PORT
, 10 },
8885 { OFPACT_ENQUEUE
, 11 },
8889 /* OpenFlow 1.1 actions. */
8890 static const struct ofpact_map of11
[] = {
8891 { OFPACT_OUTPUT
, 0 },
8892 { OFPACT_SET_VLAN_VID
, 1 },
8893 { OFPACT_SET_VLAN_PCP
, 2 },
8894 { OFPACT_SET_ETH_SRC
, 3 },
8895 { OFPACT_SET_ETH_DST
, 4 },
8896 { OFPACT_SET_IPV4_SRC
, 5 },
8897 { OFPACT_SET_IPV4_DST
, 6 },
8898 { OFPACT_SET_IP_DSCP
, 7 },
8899 { OFPACT_SET_IP_ECN
, 8 },
8900 { OFPACT_SET_L4_SRC_PORT
, 9 },
8901 { OFPACT_SET_L4_DST_PORT
, 10 },
8902 /* OFPAT_COPY_TTL_OUT (11) not supported. */
8903 /* OFPAT_COPY_TTL_IN (12) not supported. */
8904 { OFPACT_SET_MPLS_LABEL
, 13 },
8905 { OFPACT_SET_MPLS_TC
, 14 },
8906 { OFPACT_SET_MPLS_TTL
, 15 },
8907 { OFPACT_DEC_MPLS_TTL
, 16 },
8908 { OFPACT_PUSH_VLAN
, 17 },
8909 { OFPACT_STRIP_VLAN
, 18 },
8910 { OFPACT_PUSH_MPLS
, 19 },
8911 { OFPACT_POP_MPLS
, 20 },
8912 { OFPACT_SET_QUEUE
, 21 },
8913 { OFPACT_GROUP
, 22 },
8914 { OFPACT_SET_IP_TTL
, 23 },
8915 { OFPACT_DEC_TTL
, 24 },
8919 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
8920 static const struct ofpact_map of12
[] = {
8921 { OFPACT_OUTPUT
, 0 },
8922 /* OFPAT_COPY_TTL_OUT (11) not supported. */
8923 /* OFPAT_COPY_TTL_IN (12) not supported. */
8924 { OFPACT_SET_MPLS_TTL
, 15 },
8925 { OFPACT_DEC_MPLS_TTL
, 16 },
8926 { OFPACT_PUSH_VLAN
, 17 },
8927 { OFPACT_STRIP_VLAN
, 18 },
8928 { OFPACT_PUSH_MPLS
, 19 },
8929 { OFPACT_POP_MPLS
, 20 },
8930 { OFPACT_SET_QUEUE
, 21 },
8931 { OFPACT_GROUP
, 22 },
8932 { OFPACT_SET_IP_TTL
, 23 },
8933 { OFPACT_DEC_TTL
, 24 },
8934 { OFPACT_SET_FIELD
, 25 },
8935 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
8936 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
8956 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
8957 * values, into a bitmap of actions suitable for OpenFlow 'version', and
8958 * returns the result. */
8960 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap
, enum ofp_version version
)
8962 uint32_t openflow_bitmap
= 0;
8963 const struct ofpact_map
*x
;
8965 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
8966 if (ofpacts_bitmap
& (UINT64_C(1) << x
->ofpact
)) {
8967 openflow_bitmap
|= 1u << x
->ofpat
;
8970 return htonl(openflow_bitmap
);
8973 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
8974 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
8975 * and returns the result. */
8977 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap
, enum ofp_version version
)
8979 uint64_t ofpact_bitmap
= 0;
8980 const struct ofpact_map
*x
;
8982 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
8983 if (ofpat_bitmap
& htonl(1u << x
->ofpat
)) {
8984 ofpact_bitmap
|= UINT64_C(1) << x
->ofpact
;
8987 return ofpact_bitmap
;
8990 /* Appends to 's' a string representation of the set of OFPACT_* represented
8991 * by 'ofpacts_bitmap'. */
8993 ofpact_bitmap_format(uint64_t ofpacts_bitmap
, struct ds
*s
)
8995 if (!ofpacts_bitmap
) {
8996 ds_put_cstr(s
, "<none>");
8998 while (ofpacts_bitmap
) {
8999 ds_put_format(s
, "%s ",
9000 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap
)));
9001 ofpacts_bitmap
= zero_rightmost_1bit(ofpacts_bitmap
);
9007 /* Returns true if 'action' outputs to 'port', false otherwise. */
9009 ofpact_outputs_to_port(const struct ofpact
*ofpact
, ofp_port_t port
)
9011 switch (ofpact
->type
) {
9013 return ofpact_get_OUTPUT(ofpact
)->port
== port
;
9014 case OFPACT_ENQUEUE
:
9015 return ofpact_get_ENQUEUE(ofpact
)->port
== port
;
9016 case OFPACT_CONTROLLER
:
9017 return port
== OFPP_CONTROLLER
;
9019 case OFPACT_OUTPUT_REG
:
9020 case OFPACT_OUTPUT_TRUNC
:
9022 case OFPACT_SET_VLAN_VID
:
9023 case OFPACT_SET_VLAN_PCP
:
9024 case OFPACT_STRIP_VLAN
:
9025 case OFPACT_PUSH_VLAN
:
9026 case OFPACT_SET_ETH_SRC
:
9027 case OFPACT_SET_ETH_DST
:
9028 case OFPACT_SET_IPV4_SRC
:
9029 case OFPACT_SET_IPV4_DST
:
9030 case OFPACT_SET_IP_DSCP
:
9031 case OFPACT_SET_IP_ECN
:
9032 case OFPACT_SET_IP_TTL
:
9033 case OFPACT_SET_L4_SRC_PORT
:
9034 case OFPACT_SET_L4_DST_PORT
:
9035 case OFPACT_REG_MOVE
:
9036 case OFPACT_SET_FIELD
:
9037 case OFPACT_STACK_PUSH
:
9038 case OFPACT_STACK_POP
:
9039 case OFPACT_DEC_TTL
:
9040 case OFPACT_SET_MPLS_LABEL
:
9041 case OFPACT_SET_MPLS_TC
:
9042 case OFPACT_SET_MPLS_TTL
:
9043 case OFPACT_DEC_MPLS_TTL
:
9044 case OFPACT_SET_TUNNEL
:
9045 case OFPACT_WRITE_METADATA
:
9046 case OFPACT_SET_QUEUE
:
9047 case OFPACT_POP_QUEUE
:
9048 case OFPACT_FIN_TIMEOUT
:
9049 case OFPACT_RESUBMIT
:
9051 case OFPACT_CONJUNCTION
:
9052 case OFPACT_MULTIPATH
:
9055 case OFPACT_UNROLL_XLATE
:
9056 case OFPACT_PUSH_MPLS
:
9057 case OFPACT_POP_MPLS
:
9059 case OFPACT_CLEAR_ACTIONS
:
9061 case OFPACT_WRITE_ACTIONS
:
9062 case OFPACT_GOTO_TABLE
:
9065 case OFPACT_DEBUG_RECIRC
:
9066 case OFPACT_DEBUG_SLOW
:
9068 case OFPACT_CT_CLEAR
:
9072 case OFPACT_DEC_NSH_TTL
:
9073 case OFPACT_CHECK_PKT_LARGER
:
9074 case OFPACT_DELETE_FIELD
:
9080 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
9081 * to 'port', false otherwise. */
9083 ofpacts_output_to_port(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
9086 const struct ofpact
*a
;
9088 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
9089 if (ofpact_outputs_to_port(a
, port
)) {
9097 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
9098 * to 'group', false otherwise. */
9100 ofpacts_output_to_group(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
9103 const struct ofpact
*a
;
9105 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
9106 if (a
->type
== OFPACT_GROUP
9107 && ofpact_get_GROUP(a
)->group_id
== group_id
) {
9115 /* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of
9116 * actions in 'b' are bytewise identical. */
9118 ofpacts_equal(const struct ofpact
*a
, size_t a_len
,
9119 const struct ofpact
*b
, size_t b_len
)
9121 return a_len
== b_len
&& !memcmp(a
, b
, a_len
);
9124 /* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of
9125 * actions in 'b' are identical when formatted as strings. (Converting actions
9126 * to string form suppresses some rarely meaningful differences, such as the
9127 * 'compat' member of actions.) */
9129 ofpacts_equal_stringwise(const struct ofpact
*a
, size_t a_len
,
9130 const struct ofpact
*b
, size_t b_len
)
9132 struct ds a_s
= DS_EMPTY_INITIALIZER
;
9133 struct ofpact_format_params a_fp
= { .s
= &a_s
};
9134 ofpacts_format(a
, a_len
, &a_fp
);
9136 struct ds b_s
= DS_EMPTY_INITIALIZER
;
9137 struct ofpact_format_params b_fp
= { .s
= &b_s
};
9138 ofpacts_format(b
, b_len
, &b_fp
);
9140 bool equal
= !strcmp(ds_cstr(&a_s
), ds_cstr(&b_s
));
9148 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
9149 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
9151 * This function relies on the order of 'ofpacts' being correct (as checked by
9152 * ofpacts_verify()). */
9154 ofpacts_get_meter(const struct ofpact ofpacts
[], size_t ofpacts_len
)
9156 const struct ofpact
*a
;
9158 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
9159 if (a
->type
== OFPACT_METER
) {
9160 return ofpact_get_METER(a
)->meter_id
;
9163 enum ovs_instruction_type inst
9164 = ovs_instruction_type_from_ofpact_type(a
->type
, 0);
9165 if (inst
> OVSINST_OFPIT13_METER
) {
9173 /* Formatting ofpacts. */
9176 ofpact_format(const struct ofpact
*a
,
9177 const struct ofpact_format_params
*fp
)
9180 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
9181 case OFPACT_##ENUM: \
9182 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), fp); \
9191 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
9192 * 'ofpacts' to 'fp->s'. If 'port_map' is nonnull, uses it to translate port
9193 * numbers to names in output. */
9195 ofpacts_format(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
9196 const struct ofpact_format_params
*fp
)
9199 ds_put_format(fp
->s
, "%sdrop%s", colors
.drop
, colors
.end
);
9201 const struct ofpact
*a
;
9203 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
9205 ds_put_char(fp
->s
, ',');
9208 ofpact_format(a
, fp
);
9213 /* Internal use by helpers. */
9215 /* Implementation of ofpact_put_<ENUM>(). */
9217 ofpact_put(struct ofpbuf
*ofpacts
, enum ofpact_type type
, size_t len
)
9219 struct ofpact
*ofpact
;
9221 ofpacts
->header
= ofpbuf_put_uninit(ofpacts
, len
);
9222 ofpact
= ofpacts
->header
;
9223 ofpact_init(ofpact
, type
, len
);
9227 /* Implementation of ofpact_init_<ENUM>(). */
9229 ofpact_init(struct ofpact
*ofpact
, enum ofpact_type type
, size_t len
)
9231 memset(ofpact
, 0, len
);
9232 ofpact
->type
= type
;
9237 /* Implementation of ofpact_finish_<ENUM>().
9239 * Finishes composing a variable-length action (begun using
9240 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
9241 * bytes and updating its embedded length field. See the large comment near
9242 * the end of ofp-actions.h for more information.
9244 * May reallocate 'ofpacts'. Callers should consider updating their 'ofpact'
9245 * pointer to the return value of this function. */
9247 ofpact_finish(struct ofpbuf
*ofpacts
, struct ofpact
*ofpact
)
9251 ovs_assert(ofpact
== ofpacts
->header
);
9252 len
= (char *) ofpbuf_tail(ofpacts
) - (char *) ofpact
;
9253 ovs_assert(len
> 0 && len
<= UINT16_MAX
);
9255 ofpbuf_padto(ofpacts
, OFPACT_ALIGN(ofpacts
->size
));
9257 return ofpacts
->header
;
9260 static char * OVS_WARN_UNUSED_RESULT
9261 ofpact_parse(enum ofpact_type type
, char *value
,
9262 const struct ofpact_parse_params
*pp
)
9265 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
9266 case OFPACT_##ENUM: \
9267 return parse_##ENUM(value, pp);
9276 ofpact_type_from_name(const char *name
, enum ofpact_type
*type
)
9278 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
9279 if (!strcasecmp(name, NAME)) { \
9280 *type = OFPACT_##ENUM; \
9289 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
9291 * Returns NULL if successful, otherwise a malloc()'d string describing the
9292 * error. The caller is responsible for freeing the returned string.
9294 * If 'outer_action' is specified, indicates that the actions being parsed
9295 * are nested within another action of the type specified in 'outer_action'. */
9296 static char * OVS_WARN_UNUSED_RESULT
9297 ofpacts_parse__(char *str
, const struct ofpact_parse_params
*pp
,
9298 bool allow_instructions
, enum ofpact_type outer_action
)
9305 while (ofputil_parse_key_value(&pos
, &key
, &value
)) {
9306 enum ofpact_type type
;
9309 if (ofpact_type_from_name(key
, &type
)) {
9310 error
= ofpact_parse(type
, value
, pp
);
9312 if (type
== OFPACT_METER
&& !allow_instructions
) {
9313 /* Meter is an action in OF1.5 and it's being used in a
9314 * context where instructions aren't allowed. Therefore,
9315 * this must be OF1.5+. */
9316 *pp
->usable_protocols
&= OFPUTIL_P_OF15_UP
;
9318 } else if (!strcasecmp(key
, "mod_vlan_vid")) {
9319 error
= parse_set_vlan_vid(value
, true, pp
);
9320 } else if (!strcasecmp(key
, "mod_vlan_pcp")) {
9321 error
= parse_set_vlan_pcp(value
, true, pp
);
9322 } else if (!strcasecmp(key
, "set_nw_ttl")) {
9323 error
= parse_SET_IP_TTL(value
, pp
);
9324 } else if (!strcasecmp(key
, "pop_vlan")) {
9325 error
= parse_pop_vlan(pp
);
9326 } else if (!strcasecmp(key
, "set_tunnel64")) {
9327 error
= parse_set_tunnel(value
, NXAST_RAW_SET_TUNNEL64
, pp
);
9328 } else if (!strcasecmp(key
, "load")) {
9329 error
= parse_reg_load(value
, pp
);
9330 } else if (!strcasecmp(key
, "bundle_load")) {
9331 error
= parse_bundle_load(value
, pp
);
9332 } else if (!strcasecmp(key
, "drop")) {
9334 } else if (!strcasecmp(key
, "apply_actions")) {
9335 return xstrdup("apply_actions is the default instruction");
9336 } else if (ofputil_port_from_string(key
, pp
->port_map
, &port
)) {
9337 ofpact_put_OUTPUT(pp
->ofpacts
)->port
= port
;
9339 return xasprintf("unknown action %s", key
);
9346 if (drop
&& pp
->ofpacts
->size
) {
9347 return xstrdup("\"drop\" must not be accompanied by any other action "
9352 ofpacts_verify(pp
->ofpacts
->data
, pp
->ofpacts
->size
, OFP11_VERSION
,
9354 ? (1u << N_OVS_INSTRUCTIONS
) - 1
9355 : ((1u << OVSINST_OFPIT11_APPLY_ACTIONS
)
9356 | (1u << OVSINST_OFPIT13_METER
))),
9357 outer_action
, &error
);
9365 static char * OVS_WARN_UNUSED_RESULT
9366 ofpacts_parse(char *str
, const struct ofpact_parse_params
*pp
,
9367 bool allow_instructions
, enum ofpact_type outer_action
)
9369 if (pp
->depth
>= MAX_OFPACT_PARSE_DEPTH
) {
9370 return xstrdup("Action nested too deeply");
9372 CONST_CAST(struct ofpact_parse_params
*, pp
)->depth
++;
9373 uint32_t orig_size
= pp
->ofpacts
->size
;
9374 char *error
= ofpacts_parse__(str
, pp
, allow_instructions
, outer_action
);
9376 pp
->ofpacts
->size
= orig_size
;
9378 CONST_CAST(struct ofpact_parse_params
*, pp
)->depth
--;
9382 static char * OVS_WARN_UNUSED_RESULT
9383 ofpacts_parse_copy(const char *s_
, const struct ofpact_parse_params
*pp
,
9384 bool allow_instructions
, enum ofpact_type outer_action
)
9388 *pp
->usable_protocols
= OFPUTIL_P_ANY
;
9391 error
= ofpacts_parse(s
, pp
, allow_instructions
, outer_action
);
9397 /* Parses 's' as a set of OpenFlow actions and appends the actions to
9398 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
9399 * that are nested within the action of type 'outer_action'.
9401 * Returns NULL if successful, otherwise a malloc()'d string describing the
9402 * error. The caller is responsible for freeing the returned string. */
9403 char * OVS_WARN_UNUSED_RESULT
9404 ofpacts_parse_actions(const char *s
, const struct ofpact_parse_params
*pp
)
9406 return ofpacts_parse_copy(s
, pp
, false, 0);
9409 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
9412 * Returns NULL if successful, otherwise a malloc()'d string describing the
9413 * error. The caller is responsible for freeing the returned string. */
9414 char * OVS_WARN_UNUSED_RESULT
9415 ofpacts_parse_instructions(const char *s
, const struct ofpact_parse_params
*pp
)
9417 return ofpacts_parse_copy(s
, pp
, true, 0);
9421 ofpact_name(enum ofpact_type type
)
9424 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
9431 /* Low-level action decoding and encoding functions. */
9433 /* Everything needed to identify a particular OpenFlow action. */
9434 struct ofpact_hdrs
{
9435 uint32_t vendor
; /* 0 if standard, otherwise a vendor code. */
9436 uint16_t type
; /* Type if standard, otherwise subtype. */
9437 uint8_t ofp_version
; /* From ofp_header. */
9440 /* Information about a particular OpenFlow action. */
9441 struct ofpact_raw_instance
{
9442 /* The action's identity. */
9443 struct ofpact_hdrs hdrs
;
9444 enum ofp_raw_action_type raw
;
9446 /* Looking up the action. */
9447 struct hmap_node decode_node
; /* Based on 'hdrs'. */
9448 struct hmap_node encode_node
; /* Based on 'raw' + 'hdrs.ofp_version'. */
9450 /* The action's encoded size.
9452 * If this action is fixed-length, 'min_length' == 'max_length'.
9453 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
9454 * OFP_ACTION_ALIGN) == 65528. */
9455 unsigned short int min_length
;
9456 unsigned short int max_length
;
9458 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
9459 * offset of that argument from the beginning of the action and 'arg_len'
9460 * its length, both in bytes.
9462 * For actions that take other forms, these are both zero. */
9463 unsigned short int arg_ofs
;
9464 unsigned short int arg_len
;
9466 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
9469 /* If this action is deprecated, a human-readable string with a brief
9471 const char *deprecation
;
9474 /* Action header. */
9475 struct ofp_action_header
{
9476 /* The meaning of other values of 'type' generally depends on the OpenFlow
9477 * version (see enum ofp_raw_action_type).
9479 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
9480 * designates an OpenFlow vendor ID and that the remainder of the action
9481 * structure has a vendor-defined meaning.
9483 #define OFPAT_VENDOR 0xffff
9486 /* Always a multiple of 8. */
9489 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
9490 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
9493 OFP_ASSERT(sizeof(struct ofp_action_header
) == 8);
9496 ofpact_hdrs_equal(const struct ofpact_hdrs
*a
,
9497 const struct ofpact_hdrs
*b
)
9499 return (a
->vendor
== b
->vendor
9500 && a
->type
== b
->type
9501 && a
->ofp_version
== b
->ofp_version
);
9505 ofpact_hdrs_hash(const struct ofpact_hdrs
*hdrs
)
9507 return hash_2words(hdrs
->vendor
,
9508 ((uint32_t) hdrs
->type
<< 16) | hdrs
->ofp_version
);
9511 #include "ofp-actions.inc2"
9513 static struct hmap
*
9514 ofpact_decode_hmap(void)
9516 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
9517 static struct hmap hmap
;
9519 if (ovsthread_once_start(&once
)) {
9520 struct ofpact_raw_instance
*inst
;
9523 for (inst
= all_raw_instances
;
9524 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
9526 hmap_insert(&hmap
, &inst
->decode_node
,
9527 ofpact_hdrs_hash(&inst
->hdrs
));
9529 ovsthread_once_done(&once
);
9534 static struct hmap
*
9535 ofpact_encode_hmap(void)
9537 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
9538 static struct hmap hmap
;
9540 if (ovsthread_once_start(&once
)) {
9541 struct ofpact_raw_instance
*inst
;
9544 for (inst
= all_raw_instances
;
9545 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
9547 hmap_insert(&hmap
, &inst
->encode_node
,
9548 hash_2words(inst
->raw
, inst
->hdrs
.ofp_version
));
9550 ovsthread_once_done(&once
);
9556 ofpact_decode_raw(enum ofp_version ofp_version
,
9557 const struct ofp_action_header
*oah
, size_t length
,
9558 const struct ofpact_raw_instance
**instp
)
9560 const struct ofpact_raw_instance
*inst
;
9561 struct ofpact_hdrs hdrs
;
9564 if (length
< sizeof *oah
) {
9565 return OFPERR_OFPBAC_BAD_LEN
;
9568 /* Get base action type. */
9569 if (oah
->type
== htons(OFPAT_VENDOR
)) {
9571 hdrs
.vendor
= ntohl(oah
->vendor
);
9572 if (hdrs
.vendor
== NX_VENDOR_ID
|| hdrs
.vendor
== ONF_VENDOR_ID
) {
9573 /* Get extension subtype. */
9574 const struct ext_action_header
*nah
;
9576 nah
= ALIGNED_CAST(const struct ext_action_header
*, oah
);
9577 if (length
< sizeof *nah
) {
9578 return OFPERR_OFPBAC_BAD_LEN
;
9580 hdrs
.type
= ntohs(nah
->subtype
);
9582 VLOG_WARN_RL(&rl
, "OpenFlow action has unknown vendor %#"PRIx32
,
9584 return OFPERR_OFPBAC_BAD_VENDOR
;
9588 hdrs
.type
= ntohs(oah
->type
);
9591 hdrs
.ofp_version
= ofp_version
;
9592 HMAP_FOR_EACH_WITH_HASH (inst
, decode_node
, ofpact_hdrs_hash(&hdrs
),
9593 ofpact_decode_hmap()) {
9594 if (ofpact_hdrs_equal(&hdrs
, &inst
->hdrs
)) {
9600 VLOG_WARN_RL(&rl
, "unknown %s action for vendor %#"PRIx32
" and "
9601 "type %"PRIu16
, ofputil_version_to_string(ofp_version
),
9602 hdrs
.vendor
, hdrs
.type
);
9604 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
9605 : OFPERR_OFPBAC_BAD_TYPE
);
9609 ofpact_pull_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
9610 enum ofp_raw_action_type
*raw
, uint64_t *arg
)
9612 const struct ofp_action_header
*oah
= buf
->data
;
9613 const struct ofpact_raw_instance
*action
;
9614 unsigned int length
;
9618 error
= ofpact_decode_raw(ofp_version
, oah
, buf
->size
, &action
);
9623 if (action
->deprecation
) {
9624 VLOG_INFO_RL(&rl
, "%s is deprecated in %s (%s)",
9625 action
->name
, ofputil_version_to_string(ofp_version
),
9626 action
->deprecation
);
9629 length
= ntohs(oah
->len
);
9630 if (length
> buf
->size
) {
9631 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u exceeds action buffer "
9632 "length %"PRIu32
, action
->name
, length
, buf
->size
);
9633 return OFPERR_OFPBAC_BAD_LEN
;
9635 if (length
< action
->min_length
|| length
> action
->max_length
) {
9636 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u not in valid range "
9637 "[%hu,%hu]", action
->name
, length
,
9638 action
->min_length
, action
->max_length
);
9639 return OFPERR_OFPBAC_BAD_LEN
;
9642 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u is not a multiple "
9643 "of 8", action
->name
, length
);
9644 return OFPERR_OFPBAC_BAD_LEN
;
9649 if (action
->arg_len
) {
9653 p
= ofpbuf_at_assert(buf
, action
->arg_ofs
, action
->arg_len
);
9654 for (i
= 0; i
< action
->arg_len
; i
++) {
9655 *arg
= (*arg
<< 8) | p
[i
];
9659 ofpbuf_pull(buf
, length
);
9664 static const struct ofpact_raw_instance
*
9665 ofpact_raw_lookup(enum ofp_version ofp_version
, enum ofp_raw_action_type raw
)
9667 const struct ofpact_raw_instance
*inst
;
9669 HMAP_FOR_EACH_WITH_HASH (inst
, encode_node
, hash_2words(raw
, ofp_version
),
9670 ofpact_encode_hmap()) {
9671 if (inst
->raw
== raw
&& inst
->hdrs
.ofp_version
== ofp_version
) {
9679 ofpact_put_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
9680 enum ofp_raw_action_type raw
, uint64_t arg
)
9682 const struct ofpact_raw_instance
*inst
;
9683 struct ofp_action_header
*oah
;
9684 const struct ofpact_hdrs
*hdrs
;
9686 inst
= ofpact_raw_lookup(ofp_version
, raw
);
9689 oah
= ofpbuf_put_zeros(buf
, inst
->min_length
);
9690 oah
->type
= htons(hdrs
->vendor
? OFPAT_VENDOR
: hdrs
->type
);
9691 oah
->len
= htons(inst
->min_length
);
9692 oah
->vendor
= htonl(hdrs
->vendor
);
9694 switch (hdrs
->vendor
) {
9699 case ONF_VENDOR_ID
: {
9700 struct ext_action_header
*nah
= (struct ext_action_header
*) oah
;
9701 nah
->subtype
= htons(hdrs
->type
);
9709 if (inst
->arg_len
) {
9710 uint8_t *p
= (uint8_t *) oah
+ inst
->arg_ofs
+ inst
->arg_len
;
9713 for (i
= 0; i
< inst
->arg_len
; i
++) {
9725 pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
)
9727 struct ofp_action_header
*oah
;
9729 ofpbuf_put_zeros(openflow
, PAD_SIZE(openflow
->size
- start_ofs
,
9732 oah
= ofpbuf_at_assert(openflow
, start_ofs
, sizeof *oah
);
9733 oah
->len
= htons(openflow
->size
- start_ofs
);