2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-actions.h"
20 #include "byte-order.h"
22 #include "dynamic-string.h"
25 #include "meta-flow.h"
26 #include "multipath.h"
28 #include "ofp-parse.h"
31 #include "unaligned.h"
33 #include "openvswitch/vlog.h"
35 VLOG_DEFINE_THIS_MODULE(ofp_actions
);
37 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
39 struct ofp_action_header
;
41 /* Raw identifiers for OpenFlow actions.
43 * Decoding and encoding OpenFlow actions across multiple versions is difficult
44 * to do in a clean, consistent way. This enumeration lays out all of the
45 * forms of actions that Open vSwitch supports.
47 * The comments here must follow a stylized form because the
48 * "extract-ofp-actions" program parses them at build time to generate data
51 * - The first part of each comment specifies the vendor, OpenFlow versions,
52 * and type for each protocol that supports the action:
54 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
55 * extension actions. (Support for other vendors can be added, but
56 * it can't be done just based on a vendor ID definition alone
57 * because OpenFlow doesn't define a standard way to specify a
58 * subtype for vendor actions, so other vendors might do it different
61 * # The version can specify a specific OpenFlow version, a version
62 * range delimited by "-", or an open-ended range with "+".
64 * # The type, in parentheses, is the action type number (for standard
65 * OpenFlow actions) or subtype (for vendor extension actions).
67 * # Optionally one may add "is deprecated" followed by a
68 * human-readable reason in parentheses (which will be used in log
69 * messages), if a particular action should no longer be used.
71 * Multiple such specifications may be separated by commas.
73 * - The second part describes the action's wire format. It may be:
75 * # "struct <name>": The struct fully specifies the wire format. The
76 * action is exactly the size of the struct. (Thus, the struct must
77 * be an exact multiple of 8 bytes in size.)
79 * # "struct <name>, ...": The struct specifies the beginning of the
80 * wire format. An instance of the action is either the struct's
81 * exact size, or a multiple of 8 bytes longer.
83 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
84 * vendor extension) header, followed by 0 or more pad bytes to align
85 * to a multiple of <N> bits, followed by an argument of the given
86 * type, followed by 0 or more pad bytes to bring the total action up
87 * to a multiple of 8 bytes.
89 * # "void": The action is just a (standard or vendor extension)
92 * - Optional additional text enclosed in square brackets is commentary for
95 enum ofp_raw_action_type
{
96 /* ## ----------------- ## */
97 /* ## Standard actions. ## */
98 /* ## ----------------- ## */
100 /* OF1.0(0): struct ofp10_action_output. */
102 /* OF1.1+(0): struct ofp11_action_output. */
105 /* OF1.0(1): uint16_t. */
106 OFPAT_RAW10_SET_VLAN_VID
,
107 /* OF1.0(2): uint8_t. */
108 OFPAT_RAW10_SET_VLAN_PCP
,
110 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
112 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
113 * modification actions: the 1.0 versions push a VLAN header if none is
114 * present, but the 1.1 versions do not. That is the only reason that we
115 * distinguish their raw action types.] */
116 OFPAT_RAW11_SET_VLAN_VID
,
117 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
118 OFPAT_RAW11_SET_VLAN_PCP
,
120 /* OF1.1+(17): ovs_be16.
122 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
124 OFPAT_RAW11_PUSH_VLAN
,
126 /* OF1.0(3): void. */
127 OFPAT_RAW10_STRIP_VLAN
,
128 /* OF1.1+(18): void. */
129 OFPAT_RAW11_POP_VLAN
,
131 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
132 * ofp_action_dl_addr. */
133 OFPAT_RAW_SET_DL_SRC
,
135 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
136 * ofp_action_dl_addr. */
137 OFPAT_RAW_SET_DL_DST
,
139 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
141 OFPAT_RAW_SET_NW_SRC
,
143 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
145 OFPAT_RAW_SET_NW_DST
,
147 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
148 OFPAT_RAW_SET_NW_TOS
,
150 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
151 OFPAT_RAW11_SET_NW_ECN
,
153 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
155 OFPAT_RAW_SET_TP_SRC
,
157 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
159 OFPAT_RAW_SET_TP_DST
,
161 /* OF1.0(11): struct ofp10_action_enqueue. */
164 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
166 OFPAT_RAW_SET_MPLS_LABEL
,
168 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
170 OFPAT_RAW_SET_MPLS_TC
,
172 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
174 OFPAT_RAW_SET_MPLS_TTL
,
176 /* NX1.0(26), OF1.1+(16): void. */
177 OFPAT_RAW_DEC_MPLS_TTL
,
179 /* NX1.0(23), OF1.1+(19): ovs_be16.
181 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
184 /* NX1.0(24), OF1.1+(20): ovs_be16.
186 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
187 * ETH_TYPE_MPLS otherwise, not the label.] */
190 /* NX1.0(4), OF1.1+(21): uint32_t. */
193 /* OF1.1+(22): uint32_t. */
196 /* OF1.1+(23): uint8_t. */
197 OFPAT_RAW11_SET_NW_TTL
,
199 /* NX1.0(18), OF1.1+(24): void. */
200 OFPAT_RAW_DEC_NW_TTL
,
201 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
202 NXAST_RAW_DEC_TTL_CNT_IDS
,
204 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
205 OFPAT_RAW12_SET_FIELD
,
206 /* OF1.5+(25): struct ofp12_action_set_field, ... */
207 OFPAT_RAW15_SET_FIELD
,
208 /* NX1.0-1.4(7): struct nx_action_reg_load.
210 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
211 * we drop reg_load.] */
213 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
215 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
216 * we drop reg_load2.] */
219 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
220 OFPAT_RAW15_COPY_FIELD
,
221 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
222 ONFACT_RAW13_COPY_FIELD
,
223 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
226 /* ## ------------------------- ## */
227 /* ## Nicira extension actions. ## */
228 /* ## ------------------------- ## */
230 /* Actions similar to standard actions are listed with the standard actions. */
232 /* NX1.0+(1): uint16_t. */
234 /* NX1.0+(14): struct nx_action_resubmit. */
235 NXAST_RAW_RESUBMIT_TABLE
,
237 /* NX1.0+(2): uint32_t. */
238 NXAST_RAW_SET_TUNNEL
,
239 /* NX1.0+(9): uint64_t. */
240 NXAST_RAW_SET_TUNNEL64
,
242 /* NX1.0+(5): void. */
245 /* NX1.0+(8): struct nx_action_note, ... */
248 /* NX1.0+(10): struct nx_action_multipath. */
251 /* NX1.0+(12): struct nx_action_bundle, ... */
253 /* NX1.0+(13): struct nx_action_bundle, ... */
254 NXAST_RAW_BUNDLE_LOAD
,
256 /* NX1.0+(15): struct nx_action_output_reg. */
257 NXAST_RAW_OUTPUT_REG
,
258 /* NX1.0+(32): struct nx_action_output_reg2. */
259 NXAST_RAW_OUTPUT_REG2
,
261 /* NX1.0+(16): struct nx_action_learn, ... */
264 /* NX1.0+(17): void. */
267 /* NX1.0+(19): struct nx_action_fin_timeout. */
268 NXAST_RAW_FIN_TIMEOUT
,
270 /* NX1.0+(20): struct nx_action_controller. */
271 NXAST_RAW_CONTROLLER
,
273 /* NX1.0+(22): struct nx_action_write_metadata. */
274 NXAST_RAW_WRITE_METADATA
,
276 /* NX1.0+(27): struct nx_action_stack. */
277 NXAST_RAW_STACK_PUSH
,
279 /* NX1.0+(28): struct nx_action_stack. */
282 /* NX1.0+(29): struct nx_action_sample. */
285 /* NX1.0+(34): struct nx_action_conjunction. */
286 NXAST_RAW_CONJUNCTION
,
289 /* OpenFlow actions are always a multiple of 8 bytes in length. */
290 #define OFP_ACTION_ALIGN 8
292 /* Define a few functions for working with instructions. */
293 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
294 static inline const struct STRUCT * OVS_UNUSED \
295 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
297 ovs_assert(inst->type == htons(ENUM)); \
298 return ALIGNED_CAST(struct STRUCT *, inst); \
301 static inline void OVS_UNUSED \
302 instruction_init_##ENUM(struct STRUCT *s) \
304 memset(s, 0, sizeof *s); \
305 s->type = htons(ENUM); \
306 s->len = htons(sizeof *s); \
309 static inline struct STRUCT * OVS_UNUSED \
310 instruction_put_##ENUM(struct ofpbuf *buf) \
312 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
313 instruction_init_##ENUM(s); \
319 static void ofpacts_update_instruction_actions(struct ofpbuf
*openflow
,
321 static void pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
);
323 static enum ofperr
ofpacts_verify(const struct ofpact
[], size_t ofpacts_len
,
324 uint32_t allowed_ovsinsts
);
326 static void ofpact_put_set_field(struct ofpbuf
*openflow
, enum ofp_version
,
327 enum mf_field_id
, uint64_t value
);
329 static enum ofperr
ofpact_pull_raw(struct ofpbuf
*, enum ofp_version
,
330 enum ofp_raw_action_type
*, uint64_t *arg
);
331 static void *ofpact_put_raw(struct ofpbuf
*, enum ofp_version
,
332 enum ofp_raw_action_type
, uint64_t arg
);
334 static char *OVS_WARN_UNUSED_RESULT
ofpacts_parse(
335 char *str
, struct ofpbuf
*ofpacts
, enum ofputil_protocol
*usable_protocols
,
336 bool allow_instructions
);
338 #include "ofp-actions.inc1"
340 /* Output actions. */
342 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
343 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
344 * number of bytes to send. A 'max_len' of zero means no bytes of the
345 * packet should be sent. */
346 struct ofp10_action_output
{
347 ovs_be16 type
; /* OFPAT10_OUTPUT. */
348 ovs_be16 len
; /* Length is 8. */
349 ovs_be16 port
; /* Output port. */
350 ovs_be16 max_len
; /* Max length to send to controller. */
352 OFP_ASSERT(sizeof(struct ofp10_action_output
) == 8);
354 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
355 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
356 * number of bytes to send. A 'max_len' of zero means no bytes of the
357 * packet should be sent.*/
358 struct ofp11_action_output
{
359 ovs_be16 type
; /* OFPAT11_OUTPUT. */
360 ovs_be16 len
; /* Length is 16. */
361 ovs_be32 port
; /* Output port. */
362 ovs_be16 max_len
; /* Max length to send to controller. */
363 uint8_t pad
[6]; /* Pad to 64 bits. */
365 OFP_ASSERT(sizeof(struct ofp11_action_output
) == 16);
368 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output
*oao
,
371 struct ofpact_output
*output
;
373 output
= ofpact_put_OUTPUT(out
);
374 output
->port
= u16_to_ofp(ntohs(oao
->port
));
375 output
->max_len
= ntohs(oao
->max_len
);
377 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
381 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output
*oao
,
384 struct ofpact_output
*output
;
387 output
= ofpact_put_OUTPUT(out
);
388 output
->max_len
= ntohs(oao
->max_len
);
390 error
= ofputil_port_from_ofp11(oao
->port
, &output
->port
);
395 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
399 encode_OUTPUT(const struct ofpact_output
*output
,
400 enum ofp_version ofp_version
, struct ofpbuf
*out
)
402 if (ofp_version
== OFP10_VERSION
) {
403 struct ofp10_action_output
*oao
;
405 oao
= put_OFPAT10_OUTPUT(out
);
406 oao
->port
= htons(ofp_to_u16(output
->port
));
407 oao
->max_len
= htons(output
->max_len
);
409 struct ofp11_action_output
*oao
;
411 oao
= put_OFPAT11_OUTPUT(out
);
412 oao
->port
= ofputil_port_to_ofp11(output
->port
);
413 oao
->max_len
= htons(output
->max_len
);
417 static char * OVS_WARN_UNUSED_RESULT
418 parse_OUTPUT(const char *arg
, struct ofpbuf
*ofpacts
,
419 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
421 if (strchr(arg
, '[')) {
422 struct ofpact_output_reg
*output_reg
;
424 output_reg
= ofpact_put_OUTPUT_REG(ofpacts
);
425 output_reg
->max_len
= UINT16_MAX
;
426 return mf_parse_subfield(&output_reg
->src
, arg
);
428 struct ofpact_output
*output
;
430 output
= ofpact_put_OUTPUT(ofpacts
);
431 if (!ofputil_port_from_string(arg
, &output
->port
)) {
432 return xasprintf("%s: output to unknown port", arg
);
434 output
->max_len
= output
->port
== OFPP_CONTROLLER
? UINT16_MAX
: 0;
440 format_OUTPUT(const struct ofpact_output
*a
, struct ds
*s
)
442 if (ofp_to_u16(a
->port
) < ofp_to_u16(OFPP_MAX
)) {
443 ds_put_format(s
, "output:%"PRIu16
, a
->port
);
445 ofputil_format_port(a
->port
, s
);
446 if (a
->port
== OFPP_CONTROLLER
) {
447 ds_put_format(s
, ":%"PRIu16
, a
->max_len
);
455 decode_OFPAT_RAW11_GROUP(uint32_t group_id
, struct ofpbuf
*out
)
457 ofpact_put_GROUP(out
)->group_id
= group_id
;
462 encode_GROUP(const struct ofpact_group
*group
,
463 enum ofp_version ofp_version
, struct ofpbuf
*out
)
465 if (ofp_version
== OFP10_VERSION
) {
468 put_OFPAT11_GROUP(out
, group
->group_id
);
472 static char * OVS_WARN_UNUSED_RESULT
473 parse_GROUP(char *arg
, struct ofpbuf
*ofpacts
,
474 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
476 return str_to_u32(arg
, &ofpact_put_GROUP(ofpacts
)->group_id
);
480 format_GROUP(const struct ofpact_group
*a
, struct ds
*s
)
482 ds_put_format(s
, "group:%"PRIu32
, a
->group_id
);
485 /* Action structure for NXAST_CONTROLLER.
487 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
488 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
491 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
493 * - 'controller_id': The ID of the controller connection to which the
494 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
495 * sent only to controllers that have the specified controller connection
496 * ID. See "struct nx_controller_id" for more information. */
497 struct nx_action_controller
{
498 ovs_be16 type
; /* OFPAT_VENDOR. */
499 ovs_be16 len
; /* Length is 16. */
500 ovs_be32 vendor
; /* NX_VENDOR_ID. */
501 ovs_be16 subtype
; /* NXAST_CONTROLLER. */
502 ovs_be16 max_len
; /* Maximum length to send to controller. */
503 ovs_be16 controller_id
; /* Controller ID to send packet-in. */
504 uint8_t reason
; /* enum ofp_packet_in_reason (OFPR_*). */
505 uint8_t zero
; /* Must be zero. */
507 OFP_ASSERT(sizeof(struct nx_action_controller
) == 16);
510 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller
*nac
,
513 struct ofpact_controller
*oc
;
515 oc
= ofpact_put_CONTROLLER(out
);
516 oc
->max_len
= ntohs(nac
->max_len
);
517 oc
->controller_id
= ntohs(nac
->controller_id
);
518 oc
->reason
= nac
->reason
;
523 encode_CONTROLLER(const struct ofpact_controller
*controller
,
524 enum ofp_version ofp_version OVS_UNUSED
,
527 struct nx_action_controller
*nac
;
529 nac
= put_NXAST_CONTROLLER(out
);
530 nac
->max_len
= htons(controller
->max_len
);
531 nac
->controller_id
= htons(controller
->controller_id
);
532 nac
->reason
= controller
->reason
;
535 static char * OVS_WARN_UNUSED_RESULT
536 parse_CONTROLLER(char *arg
, struct ofpbuf
*ofpacts
,
537 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
539 enum ofp_packet_in_reason reason
= OFPR_ACTION
;
540 uint16_t controller_id
= 0;
541 uint16_t max_len
= UINT16_MAX
;
545 } else if (strspn(arg
, "0123456789") == strlen(arg
)) {
546 char *error
= str_to_u16(arg
, "max_len", &max_len
);
553 while (ofputil_parse_key_value(&arg
, &name
, &value
)) {
554 if (!strcmp(name
, "reason")) {
555 if (!ofputil_packet_in_reason_from_string(value
, &reason
)) {
556 return xasprintf("unknown reason \"%s\"", value
);
558 } else if (!strcmp(name
, "max_len")) {
559 char *error
= str_to_u16(value
, "max_len", &max_len
);
563 } else if (!strcmp(name
, "id")) {
564 char *error
= str_to_u16(value
, "id", &controller_id
);
569 return xasprintf("unknown key \"%s\" parsing controller "
575 if (reason
== OFPR_ACTION
&& controller_id
== 0) {
576 struct ofpact_output
*output
;
578 output
= ofpact_put_OUTPUT(ofpacts
);
579 output
->port
= OFPP_CONTROLLER
;
580 output
->max_len
= max_len
;
582 struct ofpact_controller
*controller
;
584 controller
= ofpact_put_CONTROLLER(ofpacts
);
585 controller
->max_len
= max_len
;
586 controller
->reason
= reason
;
587 controller
->controller_id
= controller_id
;
594 format_CONTROLLER(const struct ofpact_controller
*a
, struct ds
*s
)
596 if (a
->reason
== OFPR_ACTION
&& a
->controller_id
== 0) {
597 ds_put_format(s
, "CONTROLLER:%"PRIu16
, a
->max_len
);
599 enum ofp_packet_in_reason reason
= a
->reason
;
601 ds_put_cstr(s
, "controller(");
602 if (reason
!= OFPR_ACTION
) {
603 char reasonbuf
[OFPUTIL_PACKET_IN_REASON_BUFSIZE
];
605 ds_put_format(s
, "reason=%s,",
606 ofputil_packet_in_reason_to_string(
607 reason
, reasonbuf
, sizeof reasonbuf
));
609 if (a
->max_len
!= UINT16_MAX
) {
610 ds_put_format(s
, "max_len=%"PRIu16
",", a
->max_len
);
612 if (a
->controller_id
!= 0) {
613 ds_put_format(s
, "id=%"PRIu16
",", a
->controller_id
);
620 /* Enqueue action. */
621 struct ofp10_action_enqueue
{
622 ovs_be16 type
; /* OFPAT10_ENQUEUE. */
623 ovs_be16 len
; /* Len is 16. */
624 ovs_be16 port
; /* Port that queue belongs. Should
625 refer to a valid physical port
626 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
627 uint8_t pad
[6]; /* Pad for 64-bit alignment. */
628 ovs_be32 queue_id
; /* Where to enqueue the packets. */
630 OFP_ASSERT(sizeof(struct ofp10_action_enqueue
) == 16);
633 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue
*oae
,
636 struct ofpact_enqueue
*enqueue
;
638 enqueue
= ofpact_put_ENQUEUE(out
);
639 enqueue
->port
= u16_to_ofp(ntohs(oae
->port
));
640 enqueue
->queue
= ntohl(oae
->queue_id
);
641 if (ofp_to_u16(enqueue
->port
) >= ofp_to_u16(OFPP_MAX
)
642 && enqueue
->port
!= OFPP_IN_PORT
643 && enqueue
->port
!= OFPP_LOCAL
) {
644 return OFPERR_OFPBAC_BAD_OUT_PORT
;
650 encode_ENQUEUE(const struct ofpact_enqueue
*enqueue
,
651 enum ofp_version ofp_version
, struct ofpbuf
*out
)
653 if (ofp_version
== OFP10_VERSION
) {
654 struct ofp10_action_enqueue
*oae
;
656 oae
= put_OFPAT10_ENQUEUE(out
);
657 oae
->port
= htons(ofp_to_u16(enqueue
->port
));
658 oae
->queue_id
= htonl(enqueue
->queue
);
664 static char * OVS_WARN_UNUSED_RESULT
665 parse_ENQUEUE(char *arg
, struct ofpbuf
*ofpacts
,
666 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
669 char *port
= strtok_r(arg
, ":q,", &sp
);
670 char *queue
= strtok_r(NULL
, "", &sp
);
671 struct ofpact_enqueue
*enqueue
;
673 if (port
== NULL
|| queue
== NULL
) {
674 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
675 "\"enqueue(PORT,QUEUE)\"");
678 enqueue
= ofpact_put_ENQUEUE(ofpacts
);
679 if (!ofputil_port_from_string(port
, &enqueue
->port
)) {
680 return xasprintf("%s: enqueue to unknown port", port
);
682 return str_to_u32(queue
, &enqueue
->queue
);
686 format_ENQUEUE(const struct ofpact_enqueue
*a
, struct ds
*s
)
688 ds_put_format(s
, "enqueue:");
689 ofputil_format_port(a
->port
, s
);
690 ds_put_format(s
, ":%"PRIu32
, a
->queue
);
693 /* Action structure for NXAST_OUTPUT_REG.
695 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
697 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
698 * the NXAST_REG_LOAD action.
700 * The acceptable nxm_header values for 'src' are the same as the acceptable
701 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
703 * The 'max_len' field indicates the number of bytes to send when the chosen
704 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
705 * field of OFPAT_OUTPUT.
707 * The 'zero' field is required to be zeroed for forward compatibility. */
708 struct nx_action_output_reg
{
709 ovs_be16 type
; /* OFPAT_VENDOR. */
710 ovs_be16 len
; /* 24. */
711 ovs_be32 vendor
; /* NX_VENDOR_ID. */
712 ovs_be16 subtype
; /* NXAST_OUTPUT_REG. */
714 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
715 ovs_be32 src
; /* Source. */
717 ovs_be16 max_len
; /* Max length to send to controller. */
719 uint8_t zero
[6]; /* Reserved, must be zero. */
721 OFP_ASSERT(sizeof(struct nx_action_output_reg
) == 24);
723 /* Action structure for NXAST_OUTPUT_REG2.
725 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
726 * experimenter OXM as 'src'.
728 struct nx_action_output_reg2
{
729 ovs_be16 type
; /* OFPAT_VENDOR. */
730 ovs_be16 len
; /* 24. */
731 ovs_be32 vendor
; /* NX_VENDOR_ID. */
732 ovs_be16 subtype
; /* NXAST_OUTPUT_REG2. */
734 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
735 ovs_be16 max_len
; /* Max length to send to controller. */
738 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
739 * - Enough 0-bytes to pad the action out to 24 bytes. */
742 OFP_ASSERT(sizeof(struct nx_action_output_reg2
) == 24);
745 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg
*naor
,
748 struct ofpact_output_reg
*output_reg
;
750 if (!is_all_zeros(naor
->zero
, sizeof naor
->zero
)) {
751 return OFPERR_OFPBAC_BAD_ARGUMENT
;
754 output_reg
= ofpact_put_OUTPUT_REG(out
);
755 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG
;
756 output_reg
->src
.field
= mf_from_nxm_header(ntohl(naor
->src
));
757 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
758 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
759 output_reg
->max_len
= ntohs(naor
->max_len
);
761 return mf_check_src(&output_reg
->src
, NULL
);
765 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2
*naor
,
768 struct ofpact_output_reg
*output_reg
;
772 output_reg
= ofpact_put_OUTPUT_REG(out
);
773 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG2
;
774 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
775 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
776 output_reg
->max_len
= ntohs(naor
->max_len
);
778 ofpbuf_use_const(&b
, naor
, ntohs(naor
->len
));
779 ofpbuf_pull(&b
, OBJECT_OFFSETOF(naor
, pad
));
780 error
= nx_pull_header(&b
, &output_reg
->src
.field
, NULL
);
784 if (!is_all_zeros(b
.data
, b
.size
)) {
785 return OFPERR_NXBRC_MUST_BE_ZERO
;
788 return mf_check_src(&output_reg
->src
, NULL
);
792 encode_OUTPUT_REG(const struct ofpact_output_reg
*output_reg
,
793 enum ofp_version ofp_version OVS_UNUSED
,
796 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
797 * cannot be encoded in the older form, encode it as
798 * NXAST_RAW_OUTPUT_REG2. */
799 if (output_reg
->ofpact
.raw
== NXAST_RAW_OUTPUT_REG2
800 || !mf_nxm_header(output_reg
->src
.field
->id
)) {
801 struct nx_action_output_reg2
*naor
= put_NXAST_OUTPUT_REG2(out
);
802 size_t size
= out
->size
;
804 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
805 output_reg
->src
.n_bits
);
806 naor
->max_len
= htons(output_reg
->max_len
);
808 out
->size
= size
- sizeof naor
->pad
;
809 nx_put_header(out
, output_reg
->src
.field
->id
, 0, false);
812 struct nx_action_output_reg
*naor
= put_NXAST_OUTPUT_REG(out
);
814 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
815 output_reg
->src
.n_bits
);
816 naor
->src
= htonl(mf_nxm_header(output_reg
->src
.field
->id
));
817 naor
->max_len
= htons(output_reg
->max_len
);
821 static char * OVS_WARN_UNUSED_RESULT
822 parse_OUTPUT_REG(const char *arg
, struct ofpbuf
*ofpacts
,
823 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
825 return parse_OUTPUT(arg
, ofpacts
, usable_protocols
);
829 format_OUTPUT_REG(const struct ofpact_output_reg
*a
, struct ds
*s
)
831 ds_put_cstr(s
, "output:");
832 mf_format_subfield(&a
->src
, s
);
835 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
837 * The bundle actions choose a slave from a supplied list of options.
838 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
839 * selection to a register.
841 * The list of possible slaves follows the nx_action_bundle structure. The size
842 * of each slave is governed by its type as indicated by the 'slave_type'
843 * parameter. The list of slaves should be padded at its end with zeros to make
844 * the total length of the action a multiple of 8.
846 * Switches infer from the 'slave_type' parameter the size of each slave. All
847 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
848 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
849 * 2 byte width. Switches should reject actions which indicate unknown or
850 * unsupported slave types.
852 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
853 * slave. If the switch does not support the specified 'algorithm' parameter,
854 * it should reject the action.
856 * Several algorithms take into account liveness when selecting slaves. The
857 * liveness of a slave is implementation defined (with one exception), but will
858 * generally take into account things like its carrier status and the results
859 * of any link monitoring protocols which happen to be running on it. In order
860 * to give controllers a place-holder value, the OFPP_NONE port is always
863 * Some slave selection strategies require the use of a hash function, in which
864 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
865 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
866 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
867 * 'basis' parameter is used as a universal hash parameter. Different values
868 * of 'basis' yield different hash results.
870 * The 'zero' parameter at the end of the action structure is reserved for
871 * future use. Switches are required to reject actions which have nonzero
872 * bytes in the 'zero' field.
874 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
875 * should reject actions which have nonzero bytes in either of these fields.
877 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
878 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
879 * similar to those for the NXAST_REG_LOAD action. */
880 struct nx_action_bundle
{
881 ovs_be16 type
; /* OFPAT_VENDOR. */
882 ovs_be16 len
; /* Length including slaves. */
883 ovs_be32 vendor
; /* NX_VENDOR_ID. */
884 ovs_be16 subtype
; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
886 /* Slave choice algorithm to apply to hash value. */
887 ovs_be16 algorithm
; /* One of NX_BD_ALG_*. */
889 /* What fields to hash and how. */
890 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
891 ovs_be16 basis
; /* Universal hash parameter. */
893 ovs_be32 slave_type
; /* NXM_OF_IN_PORT. */
894 ovs_be16 n_slaves
; /* Number of slaves. */
896 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
897 ovs_be32 dst
; /* Destination. */
899 uint8_t zero
[4]; /* Reserved. Must be zero. */
901 OFP_ASSERT(sizeof(struct nx_action_bundle
) == 32);
904 decode_bundle(bool load
, const struct nx_action_bundle
*nab
,
905 struct ofpbuf
*ofpacts
)
907 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
908 struct ofpact_bundle
*bundle
;
910 size_t slaves_size
, i
;
913 bundle
= ofpact_put_BUNDLE(ofpacts
);
915 bundle
->n_slaves
= ntohs(nab
->n_slaves
);
916 bundle
->basis
= ntohs(nab
->basis
);
917 bundle
->fields
= ntohs(nab
->fields
);
918 bundle
->algorithm
= ntohs(nab
->algorithm
);
919 slave_type
= ntohl(nab
->slave_type
);
920 slaves_size
= ntohs(nab
->len
) - sizeof *nab
;
922 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
923 if (!flow_hash_fields_valid(bundle
->fields
)) {
924 VLOG_WARN_RL(&rl
, "unsupported fields %d", (int) bundle
->fields
);
925 } else if (bundle
->n_slaves
> BUNDLE_MAX_SLAVES
) {
926 VLOG_WARN_RL(&rl
, "too many slaves");
927 } else if (bundle
->algorithm
!= NX_BD_ALG_HRW
928 && bundle
->algorithm
!= NX_BD_ALG_ACTIVE_BACKUP
) {
929 VLOG_WARN_RL(&rl
, "unsupported algorithm %d", (int) bundle
->algorithm
);
930 } else if (slave_type
!= mf_nxm_header(MFF_IN_PORT
)) {
931 VLOG_WARN_RL(&rl
, "unsupported slave type %"PRIu16
, slave_type
);
936 if (!is_all_zeros(nab
->zero
, sizeof nab
->zero
)) {
937 VLOG_WARN_RL(&rl
, "reserved field is nonzero");
938 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
942 bundle
->dst
.field
= mf_from_nxm_header(ntohl(nab
->dst
));
943 bundle
->dst
.ofs
= nxm_decode_ofs(nab
->ofs_nbits
);
944 bundle
->dst
.n_bits
= nxm_decode_n_bits(nab
->ofs_nbits
);
946 if (bundle
->dst
.n_bits
< 16) {
947 VLOG_WARN_RL(&rl
, "bundle_load action requires at least 16 bit "
949 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
952 if (nab
->ofs_nbits
|| nab
->dst
) {
953 VLOG_WARN_RL(&rl
, "bundle action has nonzero reserved fields");
954 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
958 if (slaves_size
< bundle
->n_slaves
* sizeof(ovs_be16
)) {
959 VLOG_WARN_RL(&rl
, "Nicira action %s only has %"PRIuSIZE
" bytes "
960 "allocated for slaves. %"PRIuSIZE
" bytes are required "
961 "for %"PRIu16
" slaves.",
962 load
? "bundle_load" : "bundle", slaves_size
,
963 bundle
->n_slaves
* sizeof(ovs_be16
), bundle
->n_slaves
);
964 error
= OFPERR_OFPBAC_BAD_LEN
;
967 for (i
= 0; i
< bundle
->n_slaves
; i
++) {
968 uint16_t ofp_port
= ntohs(((ovs_be16
*)(nab
+ 1))[i
]);
969 ofpbuf_put(ofpacts
, &ofp_port
, sizeof ofp_port
);
972 bundle
= ofpacts
->header
;
973 ofpact_update_len(ofpacts
, &bundle
->ofpact
);
976 error
= bundle_check(bundle
, OFPP_MAX
, NULL
);
982 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle
*nab
, struct ofpbuf
*out
)
984 return decode_bundle(false, nab
, out
);
988 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle
*nab
,
991 return decode_bundle(true, nab
, out
);
995 encode_BUNDLE(const struct ofpact_bundle
*bundle
,
996 enum ofp_version ofp_version OVS_UNUSED
,
999 int slaves_len
= ROUND_UP(2 * bundle
->n_slaves
, OFP_ACTION_ALIGN
);
1000 struct nx_action_bundle
*nab
;
1004 nab
= (bundle
->dst
.field
1005 ? put_NXAST_BUNDLE_LOAD(out
)
1006 : put_NXAST_BUNDLE(out
));
1007 nab
->len
= htons(ntohs(nab
->len
) + slaves_len
);
1008 nab
->algorithm
= htons(bundle
->algorithm
);
1009 nab
->fields
= htons(bundle
->fields
);
1010 nab
->basis
= htons(bundle
->basis
);
1011 nab
->slave_type
= htonl(mf_nxm_header(MFF_IN_PORT
));
1012 nab
->n_slaves
= htons(bundle
->n_slaves
);
1013 if (bundle
->dst
.field
) {
1014 nab
->ofs_nbits
= nxm_encode_ofs_nbits(bundle
->dst
.ofs
,
1015 bundle
->dst
.n_bits
);
1016 nab
->dst
= htonl(mf_nxm_header(bundle
->dst
.field
->id
));
1019 slaves
= ofpbuf_put_zeros(out
, slaves_len
);
1020 for (i
= 0; i
< bundle
->n_slaves
; i
++) {
1021 slaves
[i
] = htons(ofp_to_u16(bundle
->slaves
[i
]));
1025 static char * OVS_WARN_UNUSED_RESULT
1026 parse_BUNDLE(const char *arg
, struct ofpbuf
*ofpacts
,
1027 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1029 return bundle_parse(arg
, ofpacts
);
1032 static char * OVS_WARN_UNUSED_RESULT
1033 parse_bundle_load(const char *arg
, struct ofpbuf
*ofpacts
)
1035 return bundle_parse_load(arg
, ofpacts
);
1039 format_BUNDLE(const struct ofpact_bundle
*a
, struct ds
*s
)
1041 bundle_format(a
, s
);
1044 /* Set VLAN actions. */
1047 decode_set_vlan_vid(uint16_t vid
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1050 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1052 struct ofpact_vlan_vid
*vlan_vid
= ofpact_put_SET_VLAN_VID(out
);
1053 vlan_vid
->vlan_vid
= vid
;
1054 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1060 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid
, struct ofpbuf
*out
)
1062 return decode_set_vlan_vid(vid
, true, out
);
1066 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid
, struct ofpbuf
*out
)
1068 return decode_set_vlan_vid(vid
, false, out
);
1072 encode_SET_VLAN_VID(const struct ofpact_vlan_vid
*vlan_vid
,
1073 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1075 uint16_t vid
= vlan_vid
->vlan_vid
;
1077 /* Push a VLAN tag, if none is present and this form of the action calls
1078 * for such a feature. */
1079 if (ofp_version
> OFP10_VERSION
1080 && vlan_vid
->push_vlan_if_needed
1081 && !vlan_vid
->flow_has_vlan
) {
1082 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1085 if (ofp_version
== OFP10_VERSION
) {
1086 put_OFPAT10_SET_VLAN_VID(out
, vid
);
1087 } else if (ofp_version
== OFP11_VERSION
) {
1088 put_OFPAT11_SET_VLAN_VID(out
, vid
);
1090 ofpact_put_set_field(out
, ofp_version
,
1091 MFF_VLAN_VID
, vid
| OFPVID12_PRESENT
);
1095 static char * OVS_WARN_UNUSED_RESULT
1096 parse_set_vlan_vid(char *arg
, struct ofpbuf
*ofpacts
, bool push_vlan_if_needed
)
1098 struct ofpact_vlan_vid
*vlan_vid
;
1102 error
= str_to_u16(arg
, "VLAN VID", &vid
);
1107 if (vid
& ~VLAN_VID_MASK
) {
1108 return xasprintf("%s: not a valid VLAN VID", arg
);
1110 vlan_vid
= ofpact_put_SET_VLAN_VID(ofpacts
);
1111 vlan_vid
->vlan_vid
= vid
;
1112 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1116 static char * OVS_WARN_UNUSED_RESULT
1117 parse_SET_VLAN_VID(char *arg
, struct ofpbuf
*ofpacts
,
1118 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1120 return parse_set_vlan_vid(arg
, ofpacts
, false);
1124 format_SET_VLAN_VID(const struct ofpact_vlan_vid
*a
, struct ds
*s
)
1126 ds_put_format(s
, "%s:%"PRIu16
,
1127 a
->push_vlan_if_needed
? "mod_vlan_vid" : "set_vlan_vid",
1131 /* Set PCP actions. */
1134 decode_set_vlan_pcp(uint8_t pcp
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1137 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1139 struct ofpact_vlan_pcp
*vlan_pcp
= ofpact_put_SET_VLAN_PCP(out
);
1140 vlan_pcp
->vlan_pcp
= pcp
;
1141 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1147 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp
, struct ofpbuf
*out
)
1149 return decode_set_vlan_pcp(pcp
, true, out
);
1153 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp
, struct ofpbuf
*out
)
1155 return decode_set_vlan_pcp(pcp
, false, out
);
1159 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*vlan_pcp
,
1160 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1162 uint8_t pcp
= vlan_pcp
->vlan_pcp
;
1164 /* Push a VLAN tag, if none is present and this form of the action calls
1165 * for such a feature. */
1166 if (ofp_version
> OFP10_VERSION
1167 && vlan_pcp
->push_vlan_if_needed
1168 && !vlan_pcp
->flow_has_vlan
) {
1169 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1172 if (ofp_version
== OFP10_VERSION
) {
1173 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
1174 } else if (ofp_version
== OFP11_VERSION
) {
1175 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
1177 ofpact_put_set_field(out
, ofp_version
, MFF_VLAN_PCP
, pcp
);
1181 static char * OVS_WARN_UNUSED_RESULT
1182 parse_set_vlan_pcp(char *arg
, struct ofpbuf
*ofpacts
, bool push_vlan_if_needed
)
1184 struct ofpact_vlan_pcp
*vlan_pcp
;
1188 error
= str_to_u8(arg
, "VLAN PCP", &pcp
);
1194 return xasprintf("%s: not a valid VLAN PCP", arg
);
1196 vlan_pcp
= ofpact_put_SET_VLAN_PCP(ofpacts
);
1197 vlan_pcp
->vlan_pcp
= pcp
;
1198 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1202 static char * OVS_WARN_UNUSED_RESULT
1203 parse_SET_VLAN_PCP(char *arg
, struct ofpbuf
*ofpacts
,
1204 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1206 return parse_set_vlan_pcp(arg
, ofpacts
, false);
1210 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*a
, struct ds
*s
)
1212 ds_put_format(s
, "%s:%"PRIu8
,
1213 a
->push_vlan_if_needed
? "mod_vlan_pcp" : "set_vlan_pcp",
1217 /* Strip VLAN actions. */
1220 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf
*out
)
1222 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1227 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf
*out
)
1229 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1234 encode_STRIP_VLAN(const struct ofpact_null
*null OVS_UNUSED
,
1235 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1237 if (ofp_version
== OFP10_VERSION
) {
1238 put_OFPAT10_STRIP_VLAN(out
);
1240 put_OFPAT11_POP_VLAN(out
);
1244 static char * OVS_WARN_UNUSED_RESULT
1245 parse_STRIP_VLAN(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
1246 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1248 ofpact_put_STRIP_VLAN(ofpacts
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1252 static char * OVS_WARN_UNUSED_RESULT
1253 parse_pop_vlan(struct ofpbuf
*ofpacts
)
1255 ofpact_put_STRIP_VLAN(ofpacts
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1260 format_STRIP_VLAN(const struct ofpact_null
*a
, struct ds
*s
)
1262 ds_put_cstr(s
, (a
->ofpact
.raw
== OFPAT_RAW11_POP_VLAN
1267 /* Push VLAN action. */
1270 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type
, struct ofpbuf
*out
)
1272 if (eth_type
!= htons(ETH_TYPE_VLAN_8021Q
)) {
1273 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1274 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1276 ofpact_put_PUSH_VLAN(out
);
1281 encode_PUSH_VLAN(const struct ofpact_null
*null OVS_UNUSED
,
1282 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1284 if (ofp_version
== OFP10_VERSION
) {
1285 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1286 * follow this action. */
1288 /* XXX ETH_TYPE_VLAN_8021AD case */
1289 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1293 static char * OVS_WARN_UNUSED_RESULT
1294 parse_PUSH_VLAN(char *arg
, struct ofpbuf
*ofpacts
,
1295 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1300 *usable_protocols
&= OFPUTIL_P_OF11_UP
;
1301 error
= str_to_u16(arg
, "ethertype", ðertype
);
1306 if (ethertype
!= ETH_TYPE_VLAN_8021Q
) {
1307 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1308 return xasprintf("%s: not a valid VLAN ethertype", arg
);
1311 ofpact_put_PUSH_VLAN(ofpacts
);
1316 format_PUSH_VLAN(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
1318 /* XXX 802.1AD case*/
1319 ds_put_format(s
, "push_vlan:%#"PRIx16
, ETH_TYPE_VLAN_8021Q
);
1322 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1323 struct ofp_action_dl_addr
{
1324 ovs_be16 type
; /* Type. */
1325 ovs_be16 len
; /* Length is 16. */
1326 uint8_t dl_addr
[OFP_ETH_ALEN
]; /* Ethernet address. */
1329 OFP_ASSERT(sizeof(struct ofp_action_dl_addr
) == 16);
1332 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr
*a
,
1335 memcpy(ofpact_put_SET_ETH_SRC(out
)->mac
, a
->dl_addr
, ETH_ADDR_LEN
);
1340 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr
*a
,
1343 memcpy(ofpact_put_SET_ETH_DST(out
)->mac
, a
->dl_addr
, ETH_ADDR_LEN
);
1348 encode_SET_ETH_addr(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1349 enum ofp_raw_action_type raw
, enum mf_field_id field
,
1352 const uint8_t *addr
= mac
->mac
;
1354 if (ofp_version
< OFP12_VERSION
) {
1355 struct ofp_action_dl_addr
*oada
;
1357 oada
= ofpact_put_raw(out
, ofp_version
, raw
, 0);
1358 memcpy(oada
->dl_addr
, addr
, ETH_ADDR_LEN
);
1360 ofpact_put_set_field(out
, ofp_version
, field
,
1361 eth_addr_to_uint64(addr
));
1366 encode_SET_ETH_SRC(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1369 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_SRC
, MFF_ETH_SRC
,
1375 encode_SET_ETH_DST(const struct ofpact_mac
*mac
,
1376 enum ofp_version ofp_version
,
1379 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_DST
, MFF_ETH_DST
,
1384 static char * OVS_WARN_UNUSED_RESULT
1385 parse_SET_ETH_SRC(char *arg
, struct ofpbuf
*ofpacts
,
1386 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1388 return str_to_mac(arg
, ofpact_put_SET_ETH_SRC(ofpacts
)->mac
);
1391 static char * OVS_WARN_UNUSED_RESULT
1392 parse_SET_ETH_DST(char *arg
, struct ofpbuf
*ofpacts
,
1393 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1395 return str_to_mac(arg
, ofpact_put_SET_ETH_DST(ofpacts
)->mac
);
1399 format_SET_ETH_SRC(const struct ofpact_mac
*a
, struct ds
*s
)
1401 ds_put_format(s
, "mod_dl_src:"ETH_ADDR_FMT
, ETH_ADDR_ARGS(a
->mac
));
1405 format_SET_ETH_DST(const struct ofpact_mac
*a
, struct ds
*s
)
1407 ds_put_format(s
, "mod_dl_dst:"ETH_ADDR_FMT
, ETH_ADDR_ARGS(a
->mac
));
1410 /* Set IPv4 address actions. */
1413 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4
, struct ofpbuf
*out
)
1415 ofpact_put_SET_IPV4_SRC(out
)->ipv4
= ipv4
;
1420 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4
, struct ofpbuf
*out
)
1422 ofpact_put_SET_IPV4_DST(out
)->ipv4
= ipv4
;
1427 encode_SET_IPV4_addr(const struct ofpact_ipv4
*ipv4
,
1428 enum ofp_version ofp_version
,
1429 enum ofp_raw_action_type raw
, enum mf_field_id field
,
1432 ovs_be32 addr
= ipv4
->ipv4
;
1433 if (ofp_version
< OFP12_VERSION
) {
1434 ofpact_put_raw(out
, ofp_version
, raw
, ntohl(addr
));
1436 ofpact_put_set_field(out
, ofp_version
, field
, ntohl(addr
));
1441 encode_SET_IPV4_SRC(const struct ofpact_ipv4
*ipv4
,
1442 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1444 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_SRC
, MFF_IPV4_SRC
,
1449 encode_SET_IPV4_DST(const struct ofpact_ipv4
*ipv4
,
1450 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1452 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_DST
, MFF_IPV4_DST
,
1456 static char * OVS_WARN_UNUSED_RESULT
1457 parse_SET_IPV4_SRC(char *arg
, struct ofpbuf
*ofpacts
,
1458 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1460 return str_to_ip(arg
, &ofpact_put_SET_IPV4_SRC(ofpacts
)->ipv4
);
1463 static char * OVS_WARN_UNUSED_RESULT
1464 parse_SET_IPV4_DST(char *arg
, struct ofpbuf
*ofpacts
,
1465 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1467 return str_to_ip(arg
, &ofpact_put_SET_IPV4_DST(ofpacts
)->ipv4
);
1471 format_SET_IPV4_SRC(const struct ofpact_ipv4
*a
, struct ds
*s
)
1473 ds_put_format(s
, "mod_nw_src:"IP_FMT
, IP_ARGS(a
->ipv4
));
1477 format_SET_IPV4_DST(const struct ofpact_ipv4
*a
, struct ds
*s
)
1479 ds_put_format(s
, "mod_nw_dst:"IP_FMT
, IP_ARGS(a
->ipv4
));
1482 /* Set IPv4/v6 TOS actions. */
1485 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp
, struct ofpbuf
*out
)
1487 if (dscp
& ~IP_DSCP_MASK
) {
1488 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1490 ofpact_put_SET_IP_DSCP(out
)->dscp
= dscp
;
1496 encode_SET_IP_DSCP(const struct ofpact_dscp
*dscp
,
1497 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1499 if (ofp_version
< OFP12_VERSION
) {
1500 put_OFPAT_SET_NW_TOS(out
, ofp_version
, dscp
->dscp
);
1502 ofpact_put_set_field(out
, ofp_version
,
1503 MFF_IP_DSCP_SHIFTED
, dscp
->dscp
>> 2);
1507 static char * OVS_WARN_UNUSED_RESULT
1508 parse_SET_IP_DSCP(char *arg
, struct ofpbuf
*ofpacts
,
1509 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1514 error
= str_to_u8(arg
, "TOS", &tos
);
1519 if (tos
& ~IP_DSCP_MASK
) {
1520 return xasprintf("%s: not a valid TOS", arg
);
1522 ofpact_put_SET_IP_DSCP(ofpacts
)->dscp
= tos
;
1527 format_SET_IP_DSCP(const struct ofpact_dscp
*a
, struct ds
*s
)
1529 ds_put_format(s
, "mod_nw_tos:%d", a
->dscp
);
1532 /* Set IPv4/v6 ECN actions. */
1535 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn
, struct ofpbuf
*out
)
1537 if (ecn
& ~IP_ECN_MASK
) {
1538 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1540 ofpact_put_SET_IP_ECN(out
)->ecn
= ecn
;
1546 encode_SET_IP_ECN(const struct ofpact_ecn
*ip_ecn
,
1547 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1549 uint8_t ecn
= ip_ecn
->ecn
;
1550 if (ofp_version
== OFP10_VERSION
) {
1552 } else if (ofp_version
== OFP11_VERSION
) {
1553 put_OFPAT11_SET_NW_ECN(out
, ecn
);
1555 ofpact_put_set_field(out
, ofp_version
, MFF_IP_ECN
, ecn
);
1559 static char * OVS_WARN_UNUSED_RESULT
1560 parse_SET_IP_ECN(char *arg
, struct ofpbuf
*ofpacts
,
1561 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1566 error
= str_to_u8(arg
, "ECN", &ecn
);
1571 if (ecn
& ~IP_ECN_MASK
) {
1572 return xasprintf("%s: not a valid ECN", arg
);
1574 ofpact_put_SET_IP_ECN(ofpacts
)->ecn
= ecn
;
1579 format_SET_IP_ECN(const struct ofpact_ecn
*a
, struct ds
*s
)
1581 ds_put_format(s
, "mod_nw_ecn:%d", a
->ecn
);
1584 /* Set IPv4/v6 TTL actions. */
1587 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl
, struct ofpbuf
*out
)
1589 ofpact_put_SET_IP_TTL(out
)->ttl
= ttl
;
1594 encode_SET_IP_TTL(const struct ofpact_ip_ttl
*ttl
,
1595 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1597 if (ofp_version
>= OFP11_VERSION
) {
1598 put_OFPAT11_SET_NW_TTL(out
, ttl
->ttl
);
1604 static char * OVS_WARN_UNUSED_RESULT
1605 parse_SET_IP_TTL(char *arg
, struct ofpbuf
*ofpacts
,
1606 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1611 error
= str_to_u8(arg
, "TTL", &ttl
);
1616 ofpact_put_SET_IP_TTL(ofpacts
)->ttl
= ttl
;
1621 format_SET_IP_TTL(const struct ofpact_ip_ttl
*a
, struct ds
*s
)
1623 ds_put_format(s
, "mod_nw_ttl:%d", a
->ttl
);
1626 /* Set TCP/UDP/SCTP port actions. */
1629 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port
, struct ofpbuf
*out
)
1631 ofpact_put_SET_L4_SRC_PORT(out
)->port
= ntohs(port
);
1636 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port
, struct ofpbuf
*out
)
1638 ofpact_put_SET_L4_DST_PORT(out
)->port
= ntohs(port
);
1643 encode_SET_L4_port(const struct ofpact_l4_port
*l4_port
,
1644 enum ofp_version ofp_version
, enum ofp_raw_action_type raw
,
1645 enum mf_field_id field
, struct ofpbuf
*out
)
1647 uint16_t port
= l4_port
->port
;
1649 if (ofp_version
>= OFP12_VERSION
&& field
!= MFF_N_IDS
) {
1650 ofpact_put_set_field(out
, ofp_version
, field
, port
);
1652 ofpact_put_raw(out
, ofp_version
, raw
, port
);
1657 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port
*l4_port
,
1658 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1660 uint8_t proto
= l4_port
->flow_ip_proto
;
1661 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_SRC
1662 : proto
== IPPROTO_UDP
? MFF_UDP_SRC
1663 : proto
== IPPROTO_SCTP
? MFF_SCTP_SRC
1666 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_SRC
, field
, out
);
1670 encode_SET_L4_DST_PORT(const struct ofpact_l4_port
*l4_port
,
1671 enum ofp_version ofp_version
,
1674 uint8_t proto
= l4_port
->flow_ip_proto
;
1675 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_DST
1676 : proto
== IPPROTO_UDP
? MFF_UDP_DST
1677 : proto
== IPPROTO_SCTP
? MFF_SCTP_DST
1680 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_DST
, field
, out
);
1683 static char * OVS_WARN_UNUSED_RESULT
1684 parse_SET_L4_SRC_PORT(char *arg
, struct ofpbuf
*ofpacts
,
1685 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1687 return str_to_u16(arg
, "source port",
1688 &ofpact_put_SET_L4_SRC_PORT(ofpacts
)->port
);
1691 static char * OVS_WARN_UNUSED_RESULT
1692 parse_SET_L4_DST_PORT(char *arg
, struct ofpbuf
*ofpacts
,
1693 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1695 return str_to_u16(arg
, "destination port",
1696 &ofpact_put_SET_L4_DST_PORT(ofpacts
)->port
);
1700 format_SET_L4_SRC_PORT(const struct ofpact_l4_port
*a
, struct ds
*s
)
1702 ds_put_format(s
, "mod_tp_src:%d", a
->port
);
1706 format_SET_L4_DST_PORT(const struct ofpact_l4_port
*a
, struct ds
*s
)
1708 ds_put_format(s
, "mod_tp_dst:%d", a
->port
);
1711 /* Action structure for OFPAT_COPY_FIELD. */
1712 struct ofp15_action_copy_field
{
1713 ovs_be16 type
; /* OFPAT_COPY_FIELD. */
1714 ovs_be16 len
; /* Length is padded to 64 bits. */
1715 ovs_be16 n_bits
; /* Number of bits to copy. */
1716 ovs_be16 src_offset
; /* Starting bit offset in source. */
1717 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
1720 * - OXM header for source field.
1721 * - OXM header for destination field.
1722 * - Padding with 0-bytes to a multiple of 8 bytes.
1723 * The "pad2" member is the beginning of the above. */
1726 OFP_ASSERT(sizeof(struct ofp15_action_copy_field
) == 16);
1728 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1729 struct onf_action_copy_field
{
1730 ovs_be16 type
; /* OFPAT_EXPERIMENTER. */
1731 ovs_be16 len
; /* Length is padded to 64 bits. */
1732 ovs_be32 experimenter
; /* ONF_VENDOR_ID. */
1733 ovs_be16 exp_type
; /* 3200. */
1734 uint8_t pad
[2]; /* Not used. */
1735 ovs_be16 n_bits
; /* Number of bits to copy. */
1736 ovs_be16 src_offset
; /* Starting bit offset in source. */
1737 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
1738 uint8_t pad2
[2]; /* Not used. */
1740 * - OXM header for source field.
1741 * - OXM header for destination field.
1742 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1743 * The "pad3" member is the beginning of the above. */
1744 uint8_t pad3
[4]; /* Not used. */
1746 OFP_ASSERT(sizeof(struct onf_action_copy_field
) == 24);
1748 /* Action structure for NXAST_REG_MOVE.
1750 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1751 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1752 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1753 * the next most significant bit, and so on.
1755 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1756 * sense to use nxm_hasmask=1 because the action does not do any kind of
1757 * matching; it uses the actual value of a field.)
1759 * The following nxm_header values are potentially acceptable as 'src':
1774 * - NXM_OF_ICMP_TYPE
1775 * - NXM_OF_ICMP_CODE
1782 * - NXM_NX_ICMPV6_TYPE
1783 * - NXM_NX_ICMPV6_CODE
1786 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1788 * - NXM_NX_TUN_IPV4_SRC
1789 * - NXM_NX_TUN_IPV4_DST
1791 * The following nxm_header values are potentially acceptable as 'dst':
1807 * Modifying any of the above fields changes the corresponding packet
1812 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1816 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1817 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1818 * header (if any), ignoring the other bits. Setting a value with CFI=1
1819 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1820 * to the field's new value (with the CFI bit masked out).
1822 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1823 * any of these values modifies the corresponding tunnel header field used
1824 * for the packet's next tunnel encapsulation, if allowed by the
1825 * configuration of the output tunnel port.
1827 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1828 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1829 * used only if the flow's nx_match includes an nxm_entry that specifies
1830 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1832 * The switch will reject actions for which src_ofs+n_bits is greater than the
1833 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1834 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1836 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1837 * behaves as if 'src' were copied out to a temporary buffer, then the
1838 * temporary buffer copied to 'dst'.
1840 struct nx_action_reg_move
{
1841 ovs_be16 type
; /* OFPAT_VENDOR. */
1842 ovs_be16 len
; /* Length is 24. */
1843 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1844 ovs_be16 subtype
; /* NXAST_REG_MOVE. */
1845 ovs_be16 n_bits
; /* Number of bits. */
1846 ovs_be16 src_ofs
; /* Starting bit offset in source. */
1847 ovs_be16 dst_ofs
; /* Starting bit offset in destination. */
1849 * - OXM/NXM header for source field (4 or 8 bytes).
1850 * - OXM/NXM header for destination field (4 or 8 bytes).
1851 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
1853 OFP_ASSERT(sizeof(struct nx_action_reg_move
) == 16);
1856 decode_copy_field__(ovs_be16 src_offset
, ovs_be16 dst_offset
, ovs_be16 n_bits
,
1857 const void *action
, ovs_be16 action_len
, size_t oxm_offset
,
1858 struct ofpbuf
*ofpacts
)
1860 struct ofpact_reg_move
*move
;
1864 move
= ofpact_put_REG_MOVE(ofpacts
);
1865 move
->ofpact
.raw
= ONFACT_RAW13_COPY_FIELD
;
1866 move
->src
.ofs
= ntohs(src_offset
);
1867 move
->src
.n_bits
= ntohs(n_bits
);
1868 move
->dst
.ofs
= ntohs(dst_offset
);
1869 move
->dst
.n_bits
= ntohs(n_bits
);
1871 ofpbuf_use_const(&b
, action
, ntohs(action_len
));
1872 ofpbuf_pull(&b
, oxm_offset
);
1873 error
= nx_pull_header(&b
, &move
->src
.field
, NULL
);
1877 error
= nx_pull_header(&b
, &move
->dst
.field
, NULL
);
1882 if (!is_all_zeros(b
.data
, b
.size
)) {
1883 return OFPERR_NXBRC_MUST_BE_ZERO
;
1886 return nxm_reg_move_check(move
, NULL
);
1890 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field
*oacf
,
1891 struct ofpbuf
*ofpacts
)
1893 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
1894 oacf
->n_bits
, oacf
, oacf
->len
,
1895 OBJECT_OFFSETOF(oacf
, pad2
), ofpacts
);
1899 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field
*oacf
,
1900 struct ofpbuf
*ofpacts
)
1902 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
1903 oacf
->n_bits
, oacf
, oacf
->len
,
1904 OBJECT_OFFSETOF(oacf
, pad3
), ofpacts
);
1908 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move
*narm
,
1909 struct ofpbuf
*ofpacts
)
1911 struct ofpact_reg_move
*move
;
1915 move
= ofpact_put_REG_MOVE(ofpacts
);
1916 move
->ofpact
.raw
= NXAST_RAW_REG_MOVE
;
1917 move
->src
.ofs
= ntohs(narm
->src_ofs
);
1918 move
->src
.n_bits
= ntohs(narm
->n_bits
);
1919 move
->dst
.ofs
= ntohs(narm
->dst_ofs
);
1920 move
->dst
.n_bits
= ntohs(narm
->n_bits
);
1922 ofpbuf_use_const(&b
, narm
, ntohs(narm
->len
));
1923 ofpbuf_pull(&b
, sizeof *narm
);
1924 error
= nx_pull_header(&b
, &move
->src
.field
, NULL
);
1928 error
= nx_pull_header(&b
, &move
->dst
.field
, NULL
);
1932 if (!is_all_zeros(b
.data
, b
.size
)) {
1933 return OFPERR_NXBRC_MUST_BE_ZERO
;
1936 return nxm_reg_move_check(move
, NULL
);
1940 encode_REG_MOVE(const struct ofpact_reg_move
*move
,
1941 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1943 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
1944 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
1945 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
1946 * probe for support. Until we have that ability, we currently prefer
1947 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
1949 size_t start_ofs
= out
->size
;
1950 if (ofp_version
>= OFP15_VERSION
) {
1951 struct ofp15_action_copy_field
*copy
= put_OFPAT15_COPY_FIELD(out
);
1952 copy
->n_bits
= htons(move
->dst
.n_bits
);
1953 copy
->src_offset
= htons(move
->src
.ofs
);
1954 copy
->dst_offset
= htons(move
->dst
.ofs
);
1955 out
->size
= out
->size
- sizeof copy
->pad2
;
1956 nx_put_header(out
, move
->src
.field
->id
, ofp_version
, false);
1957 nx_put_header(out
, move
->dst
.field
->id
, ofp_version
, false);
1958 } else if (ofp_version
== OFP13_VERSION
1959 && move
->ofpact
.raw
== ONFACT_RAW13_COPY_FIELD
) {
1960 struct onf_action_copy_field
*copy
= put_ONFACT13_COPY_FIELD(out
);
1961 copy
->n_bits
= htons(move
->dst
.n_bits
);
1962 copy
->src_offset
= htons(move
->src
.ofs
);
1963 copy
->dst_offset
= htons(move
->dst
.ofs
);
1964 out
->size
= out
->size
- sizeof copy
->pad3
;
1965 nx_put_header(out
, move
->src
.field
->id
, ofp_version
, false);
1966 nx_put_header(out
, move
->dst
.field
->id
, ofp_version
, false);
1968 struct nx_action_reg_move
*narm
= put_NXAST_REG_MOVE(out
);
1969 narm
->n_bits
= htons(move
->dst
.n_bits
);
1970 narm
->src_ofs
= htons(move
->src
.ofs
);
1971 narm
->dst_ofs
= htons(move
->dst
.ofs
);
1972 nx_put_header(out
, move
->src
.field
->id
, 0, false);
1973 nx_put_header(out
, move
->dst
.field
->id
, 0, false);
1975 pad_ofpat(out
, start_ofs
);
1978 static char * OVS_WARN_UNUSED_RESULT
1979 parse_REG_MOVE(const char *arg
, struct ofpbuf
*ofpacts
,
1980 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1982 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
1983 const char *full_arg
= arg
;
1986 error
= mf_parse_subfield__(&move
->src
, &arg
);
1990 if (strncmp(arg
, "->", 2)) {
1991 return xasprintf("%s: missing `->' following source", full_arg
);
1994 error
= mf_parse_subfield(&move
->dst
, arg
);
1999 if (move
->src
.n_bits
!= move
->dst
.n_bits
) {
2000 return xasprintf("%s: source field is %d bits wide but destination is "
2001 "%d bits wide", full_arg
,
2002 move
->src
.n_bits
, move
->dst
.n_bits
);
2008 format_REG_MOVE(const struct ofpact_reg_move
*a
, struct ds
*s
)
2010 nxm_format_reg_move(a
, s
);
2013 /* Action structure for OFPAT12_SET_FIELD. */
2014 struct ofp12_action_set_field
{
2015 ovs_be16 type
; /* OFPAT12_SET_FIELD. */
2016 ovs_be16 len
; /* Length is padded to 64 bits. */
2019 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2020 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2022 * The "pad" member is the beginning of the above. */
2025 OFP_ASSERT(sizeof(struct ofp12_action_set_field
) == 8);
2027 /* Action structure for NXAST_REG_LOAD.
2029 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2030 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2031 * starts at 0 for the least-significant bit, 1 for the next most significant
2034 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2035 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2038 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2039 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2040 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2041 * also stored as one less than its true value:
2044 * +------------------------------+------------------+
2045 * | ofs | n_bits - 1 |
2046 * +------------------------------+------------------+
2048 * The switch will reject actions for which ofs+n_bits is greater than the
2049 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2050 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2051 * OFPBAC_BAD_ARGUMENT.
2053 struct nx_action_reg_load
{
2054 ovs_be16 type
; /* OFPAT_VENDOR. */
2055 ovs_be16 len
; /* Length is 24. */
2056 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2057 ovs_be16 subtype
; /* NXAST_REG_LOAD. */
2058 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
2059 ovs_be32 dst
; /* Destination register. */
2060 ovs_be64 value
; /* Immediate value. */
2062 OFP_ASSERT(sizeof(struct nx_action_reg_load
) == 24);
2064 /* Action structure for NXAST_REG_LOAD2.
2066 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2067 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2068 * OXM experimenter fields. */
2069 struct nx_action_reg_load2
{
2070 ovs_be16 type
; /* OFPAT_VENDOR. */
2071 ovs_be16 len
; /* At least 16. */
2072 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2073 ovs_be16 subtype
; /* NXAST_SET_FIELD. */
2076 * - An NXM/OXM header, value, and optionally a mask.
2077 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2079 * The "pad" member is the beginning of the above. */
2082 OFP_ASSERT(sizeof(struct nx_action_reg_load2
) == 16);
2085 decode_ofpat_set_field(const struct ofp12_action_set_field
*oasf
,
2086 bool may_mask
, struct ofpbuf
*ofpacts
)
2088 struct ofpact_set_field
*sf
;
2092 sf
= ofpact_put_SET_FIELD(ofpacts
);
2094 ofpbuf_use_const(&b
, oasf
, ntohs(oasf
->len
));
2095 ofpbuf_pull(&b
, OBJECT_OFFSETOF(oasf
, pad
));
2096 error
= nx_pull_entry(&b
, &sf
->field
, &sf
->value
,
2097 may_mask
? &sf
->mask
: NULL
);
2099 return (error
== OFPERR_OFPBMC_BAD_MASK
2100 ? OFPERR_OFPBAC_BAD_SET_MASK
2104 memset(&sf
->mask
, 0xff, sf
->field
->n_bytes
);
2107 if (!is_all_zeros(b
.data
, b
.size
)) {
2108 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2111 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2113 if (sf
->field
->id
== MFF_IN_PORT_OXM
) {
2114 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2117 /* oxm_length is now validated to be compatible with mf_value. */
2118 if (!sf
->field
->writable
) {
2119 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2121 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2124 /* The value must be valid for match. OpenFlow 1.5 also says,
2125 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2126 * a 1-bit in oxm_value and in oxm_mask." */
2127 if (!mf_is_value_valid(sf
->field
, &sf
->value
)
2128 || (sf
->field
->id
== MFF_VLAN_VID
2129 && (!(sf
->mask
.be16
& htons(OFPVID12_PRESENT
))
2130 || !(sf
->value
.be16
& htons(OFPVID12_PRESENT
))))) {
2131 struct ds ds
= DS_EMPTY_INITIALIZER
;
2132 mf_format(sf
->field
, &sf
->value
, NULL
, &ds
);
2133 VLOG_WARN_RL(&rl
, "Invalid value for set field %s: %s",
2134 sf
->field
->name
, ds_cstr(&ds
));
2137 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2143 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2144 struct ofpbuf
*ofpacts
)
2146 return decode_ofpat_set_field(oasf
, false, ofpacts
);
2150 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2151 struct ofpbuf
*ofpacts
)
2153 return decode_ofpat_set_field(oasf
, true, ofpacts
);
2157 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load
*narl
,
2160 struct ofpact_set_field
*sf
= ofpact_put_reg_load(out
);
2161 struct mf_subfield dst
;
2164 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD
;
2166 dst
.field
= mf_from_nxm_header(ntohl(narl
->dst
));
2167 dst
.ofs
= nxm_decode_ofs(narl
->ofs_nbits
);
2168 dst
.n_bits
= nxm_decode_n_bits(narl
->ofs_nbits
);
2169 error
= mf_check_dst(&dst
, NULL
);
2174 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2176 if (dst
.n_bits
< 64 && ntohll(narl
->value
) >> dst
.n_bits
) {
2177 return OFPERR_OFPBAC_BAD_ARGUMENT
;
2180 sf
->field
= dst
.field
;
2181 bitwise_put(ntohll(narl
->value
),
2182 &sf
->value
, dst
.field
->n_bytes
, dst
.ofs
,
2184 bitwise_put(UINT64_MAX
,
2185 &sf
->mask
, dst
.field
->n_bytes
, dst
.ofs
,
2192 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2
*narl
,
2195 struct ofpact_set_field
*sf
;
2199 sf
= ofpact_put_SET_FIELD(out
);
2200 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD2
;
2202 ofpbuf_use_const(&b
, narl
, ntohs(narl
->len
));
2203 ofpbuf_pull(&b
, OBJECT_OFFSETOF(narl
, pad
));
2204 error
= nx_pull_entry(&b
, &sf
->field
, &sf
->value
, &sf
->mask
);
2208 if (!is_all_zeros(b
.data
, b
.size
)) {
2209 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2212 if (!sf
->field
->writable
) {
2213 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2215 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2221 ofpact_put_set_field(struct ofpbuf
*openflow
, enum ofp_version ofp_version
,
2222 enum mf_field_id field
, uint64_t value_
)
2224 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
2225 int n_bytes
= mf_from_id(field
)->n_bytes
;
2226 size_t start_ofs
= openflow
->size
;
2227 union mf_value value
;
2229 value
.be64
= htonll(value_
<< (8 * (8 - n_bytes
)));
2231 oasf
= put_OFPAT12_SET_FIELD(openflow
);
2232 openflow
->size
= openflow
->size
- sizeof oasf
->pad
;
2233 nx_put_entry(openflow
, field
, ofp_version
, &value
, NULL
);
2234 pad_ofpat(openflow
, start_ofs
);
2238 next_load_segment(const struct ofpact_set_field
*sf
,
2239 struct mf_subfield
*dst
, uint64_t *value
)
2241 int n_bits
= sf
->field
->n_bits
;
2242 int n_bytes
= sf
->field
->n_bytes
;
2243 int start
= dst
->ofs
+ dst
->n_bits
;
2245 if (start
< n_bits
) {
2246 dst
->field
= sf
->field
;
2247 dst
->ofs
= bitwise_scan(&sf
->mask
, n_bytes
, 1, start
, n_bits
);
2248 if (dst
->ofs
< n_bits
) {
2249 dst
->n_bits
= bitwise_scan(&sf
->mask
, n_bytes
, 0, dst
->ofs
+ 1,
2250 MIN(dst
->ofs
+ 64, n_bits
)) - dst
->ofs
;
2251 *value
= bitwise_get(&sf
->value
, n_bytes
, dst
->ofs
, dst
->n_bits
);
2258 /* Convert 'sf' to a series of REG_LOADs. */
2260 set_field_to_nxast(const struct ofpact_set_field
*sf
, struct ofpbuf
*openflow
)
2262 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2263 * experimenter OXM (or if it came in as NXAST_REG_LOAD2), encode as
2264 * NXAST_REG_LOAD2. Otherwise use NXAST_REG_LOAD, which is backward
2266 if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
2267 || !mf_nxm_header(sf
->field
->id
)) {
2268 struct nx_action_reg_load2
*narl OVS_UNUSED
;
2269 size_t start_ofs
= openflow
->size
;
2271 narl
= put_NXAST_REG_LOAD2(openflow
);
2272 openflow
->size
= openflow
->size
- sizeof narl
->pad
;
2273 nx_put_entry(openflow
, sf
->field
->id
, 0, &sf
->value
, &sf
->mask
);
2274 pad_ofpat(openflow
, start_ofs
);
2276 struct mf_subfield dst
;
2279 dst
.ofs
= dst
.n_bits
= 0;
2280 while (next_load_segment(sf
, &dst
, &value
)) {
2281 struct nx_action_reg_load
*narl
= put_NXAST_REG_LOAD(openflow
);
2282 narl
->ofs_nbits
= nxm_encode_ofs_nbits(dst
.ofs
, dst
.n_bits
);
2283 narl
->dst
= htonl(mf_nxm_header(dst
.field
->id
));
2284 narl
->value
= htonll(value
);
2289 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2290 * actions, if we can, falling back to Nicira extensions if we must.
2292 * We check only meta-flow types that can appear within set field actions and
2293 * that have a mapping to compatible action types. These struct mf_field
2294 * definitions have a defined OXM or NXM header value and specify the field as
2297 set_field_to_legacy_openflow(const struct ofpact_set_field
*sf
,
2298 enum ofp_version ofp_version
,
2301 switch ((int) sf
->field
->id
) {
2302 case MFF_VLAN_TCI
: {
2303 ovs_be16 tci
= sf
->value
.be16
;
2304 bool cfi
= (tci
& htons(VLAN_CFI
)) != 0;
2305 uint16_t vid
= vlan_tci_to_vid(tci
);
2306 uint8_t pcp
= vlan_tci_to_pcp(tci
);
2308 if (ofp_version
< OFP11_VERSION
) {
2309 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2311 * If CFI=1, Add or modify VLAN VID & PCP.
2312 * If CFI=0, strip VLAN header, if any.
2315 put_OFPAT10_SET_VLAN_VID(out
, vid
);
2316 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
2318 put_OFPAT10_STRIP_VLAN(out
);
2321 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2323 * If CFI=1, Add or modify VLAN VID & PCP.
2324 * OpenFlow 1.1 set actions only apply if the packet
2325 * already has VLAN tags. To be sure that is the case
2326 * we have to push a VLAN header. As we do not support
2327 * multiple layers of VLANs, this is a no-op, if a VLAN
2328 * header already exists. This may backfire, however,
2329 * when we start supporting multiple layers of VLANs.
2330 * If CFI=0, strip VLAN header, if any.
2333 /* Push a VLAN tag, if one was not seen at action validation
2335 if (!sf
->flow_has_vlan
) {
2336 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
2338 put_OFPAT11_SET_VLAN_VID(out
, vid
);
2339 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
2341 /* If the flow did not match on vlan, we have no way of
2342 * knowing if the vlan tag exists, so we must POP just to be
2344 put_OFPAT11_POP_VLAN(out
);
2350 case MFF_VLAN_VID
: {
2351 uint16_t vid
= ntohs(sf
->value
.be16
) & VLAN_VID_MASK
;
2352 if (ofp_version
== OFP10_VERSION
) {
2353 put_OFPAT10_SET_VLAN_VID(out
, vid
);
2355 put_OFPAT11_SET_VLAN_VID(out
, vid
);
2361 if (ofp_version
== OFP10_VERSION
) {
2362 put_OFPAT10_SET_VLAN_PCP(out
, sf
->value
.u8
);
2364 put_OFPAT11_SET_VLAN_PCP(out
, sf
->value
.u8
);
2369 memcpy(put_OFPAT_SET_DL_SRC(out
, ofp_version
)->dl_addr
,
2370 sf
->value
.mac
, ETH_ADDR_LEN
);
2374 memcpy(put_OFPAT_SET_DL_DST(out
, ofp_version
)->dl_addr
,
2375 sf
->value
.mac
, ETH_ADDR_LEN
);
2379 put_OFPAT_SET_NW_SRC(out
, ofp_version
, sf
->value
.be32
);
2383 put_OFPAT_SET_NW_DST(out
, ofp_version
, sf
->value
.be32
);
2387 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
.u8
);
2390 case MFF_IP_DSCP_SHIFTED
:
2391 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
.u8
<< 2);
2396 put_OFPAT_SET_TP_SRC(out
, sf
->value
.be16
);
2401 put_OFPAT_SET_TP_DST(out
, sf
->value
.be16
);
2405 set_field_to_nxast(sf
, out
);
2411 set_field_to_set_field(const struct ofpact_set_field
*sf
,
2412 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2414 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
2415 size_t start_ofs
= out
->size
;
2417 oasf
= put_OFPAT12_SET_FIELD(out
);
2418 out
->size
= out
->size
- sizeof oasf
->pad
;
2419 nx_put_entry(out
, sf
->field
->id
, ofp_version
, &sf
->value
, &sf
->mask
);
2420 pad_ofpat(out
, start_ofs
);
2424 encode_SET_FIELD(const struct ofpact_set_field
*sf
,
2425 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2427 if (ofp_version
>= OFP15_VERSION
) {
2428 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2430 set_field_to_set_field(sf
, ofp_version
, out
);
2431 } else if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD
||
2432 sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
) {
2433 /* It came in as reg_load, send it out the same way. */
2434 set_field_to_nxast(sf
, out
);
2435 } else if (ofp_version
< OFP12_VERSION
) {
2436 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2437 set_field_to_legacy_openflow(sf
, ofp_version
, out
);
2438 } else if (is_all_ones((const uint8_t *) &sf
->mask
, sf
->field
->n_bytes
)) {
2439 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2440 * entire field, so encode it as OFPAT_SET_FIELD. */
2441 set_field_to_set_field(sf
, ofp_version
, out
);
2443 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2444 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2445 * so encode it as reg_load. */
2446 set_field_to_nxast(sf
, out
);
2450 /* Parses a "set_field" action with argument 'arg', appending the parsed
2451 * action to 'ofpacts'.
2453 * Returns NULL if successful, otherwise a malloc()'d string describing the
2454 * error. The caller is responsible for freeing the returned string. */
2455 static char * OVS_WARN_UNUSED_RESULT
2456 set_field_parse__(char *arg
, struct ofpbuf
*ofpacts
,
2457 enum ofputil_protocol
*usable_protocols
)
2459 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(ofpacts
);
2463 const struct mf_field
*mf
;
2467 delim
= strstr(arg
, "->");
2469 return xasprintf("%s: missing `->'", arg
);
2471 if (strlen(delim
) <= strlen("->")) {
2472 return xasprintf("%s: missing field name following `->'", arg
);
2475 key
= delim
+ strlen("->");
2476 mf
= mf_from_name(key
);
2478 return xasprintf("%s is not a valid OXM field name", key
);
2480 if (!mf
->writable
) {
2481 return xasprintf("%s is read-only", key
);
2485 error
= mf_parse(mf
, value
, &sf
->value
, &sf
->mask
);
2490 if (!mf_is_value_valid(mf
, &sf
->value
)) {
2491 return xasprintf("%s is not a valid value for field %s", value
, key
);
2494 *usable_protocols
&= mf
->usable_protocols_exact
;
2498 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2499 * action to 'ofpacts'.
2501 * Returns NULL if successful, otherwise a malloc()'d string describing the
2502 * error. The caller is responsible for freeing the returned string. */
2503 static char * OVS_WARN_UNUSED_RESULT
2504 parse_SET_FIELD(const char *arg
, struct ofpbuf
*ofpacts
,
2505 enum ofputil_protocol
*usable_protocols
)
2507 char *copy
= xstrdup(arg
);
2508 char *error
= set_field_parse__(copy
, ofpacts
, usable_protocols
);
2513 static char * OVS_WARN_UNUSED_RESULT
2514 parse_reg_load(char *arg
, struct ofpbuf
*ofpacts
)
2516 struct ofpact_set_field
*sf
= ofpact_put_reg_load(ofpacts
);
2517 const char *full_arg
= arg
;
2518 uint64_t value
= strtoull(arg
, (char **) &arg
, 0);
2519 struct mf_subfield dst
;
2522 if (strncmp(arg
, "->", 2)) {
2523 return xasprintf("%s: missing `->' following value", full_arg
);
2526 error
= mf_parse_subfield(&dst
, arg
);
2531 if (dst
.n_bits
< 64 && (value
>> dst
.n_bits
) != 0) {
2532 return xasprintf("%s: value %"PRIu64
" does not fit into %d bits",
2533 full_arg
, value
, dst
.n_bits
);
2536 sf
->field
= dst
.field
;
2537 memset(&sf
->value
, 0, sizeof sf
->value
);
2538 bitwise_put(value
, &sf
->value
, dst
.field
->n_bytes
, dst
.ofs
, dst
.n_bits
);
2539 bitwise_put(UINT64_MAX
, &sf
->mask
,
2540 dst
.field
->n_bytes
, dst
.ofs
, dst
.n_bits
);
2545 format_SET_FIELD(const struct ofpact_set_field
*a
, struct ds
*s
)
2547 if (a
->ofpact
.raw
== NXAST_RAW_REG_LOAD
) {
2548 struct mf_subfield dst
;
2551 dst
.ofs
= dst
.n_bits
= 0;
2552 while (next_load_segment(a
, &dst
, &value
)) {
2553 ds_put_format(s
, "load:%#"PRIx64
"->", value
);
2554 mf_format_subfield(&dst
, s
);
2555 ds_put_char(s
, ',');
2559 ds_put_cstr(s
, "set_field:");
2560 mf_format(a
->field
, &a
->value
, &a
->mask
, s
);
2561 ds_put_format(s
, "->%s", a
->field
->name
);
2565 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2566 * is marked such that, if possible, it will be translated to OpenFlow as
2567 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2568 * that was the way that the action was expressed when it came into OVS or for
2569 * backward compatibility. */
2570 struct ofpact_set_field
*
2571 ofpact_put_reg_load(struct ofpbuf
*ofpacts
)
2573 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(ofpacts
);
2574 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD
;
2578 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2580 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2583 struct nx_action_stack
{
2584 ovs_be16 type
; /* OFPAT_VENDOR. */
2585 ovs_be16 len
; /* Length is 16. */
2586 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2587 ovs_be16 subtype
; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2588 ovs_be16 offset
; /* Bit offset into the field. */
2590 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2591 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2592 * - Enough 0-bytes to pad out the action to 24 bytes. */
2593 uint8_t pad
[12]; /* See above. */
2595 OFP_ASSERT(sizeof(struct nx_action_stack
) == 24);
2598 decode_stack_action(const struct nx_action_stack
*nasp
,
2599 struct ofpact_stack
*stack_action
)
2604 stack_action
->subfield
.ofs
= ntohs(nasp
->offset
);
2606 ofpbuf_use_const(&b
, nasp
, sizeof *nasp
);
2607 ofpbuf_pull(&b
, OBJECT_OFFSETOF(nasp
, pad
));
2608 error
= nx_pull_header(&b
, &stack_action
->subfield
.field
, NULL
);
2612 stack_action
->subfield
.n_bits
= ntohs(*(const ovs_be16
*) b
.data
);
2614 if (!is_all_zeros(b
.data
, b
.size
)) {
2615 return OFPERR_NXBRC_MUST_BE_ZERO
;
2622 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack
*nasp
,
2623 struct ofpbuf
*ofpacts
)
2625 struct ofpact_stack
*push
= ofpact_put_STACK_PUSH(ofpacts
);
2626 enum ofperr error
= decode_stack_action(nasp
, push
);
2627 return error
? error
: nxm_stack_push_check(push
, NULL
);
2631 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack
*nasp
,
2632 struct ofpbuf
*ofpacts
)
2634 struct ofpact_stack
*pop
= ofpact_put_STACK_POP(ofpacts
);
2635 enum ofperr error
= decode_stack_action(nasp
, pop
);
2636 return error
? error
: nxm_stack_pop_check(pop
, NULL
);
2640 encode_STACK_op(const struct ofpact_stack
*stack_action
,
2641 struct nx_action_stack
*nasp
)
2646 nasp
->offset
= htons(stack_action
->subfield
.ofs
);
2648 ofpbuf_use_stack(&b
, nasp
, ntohs(nasp
->len
));
2649 ofpbuf_put_uninit(&b
, OBJECT_OFFSETOF(nasp
, pad
));
2650 nx_put_header(&b
, stack_action
->subfield
.field
->id
, 0, false);
2651 n_bits
= htons(stack_action
->subfield
.n_bits
);
2652 ofpbuf_put(&b
, &n_bits
, sizeof n_bits
);
2656 encode_STACK_PUSH(const struct ofpact_stack
*stack
,
2657 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
2659 encode_STACK_op(stack
, put_NXAST_STACK_PUSH(out
));
2663 encode_STACK_POP(const struct ofpact_stack
*stack
,
2664 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
2666 encode_STACK_op(stack
, put_NXAST_STACK_POP(out
));
2669 static char * OVS_WARN_UNUSED_RESULT
2670 parse_STACK_PUSH(char *arg
, struct ofpbuf
*ofpacts
,
2671 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2673 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts
), arg
);
2676 static char * OVS_WARN_UNUSED_RESULT
2677 parse_STACK_POP(char *arg
, struct ofpbuf
*ofpacts
,
2678 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2680 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts
), arg
);
2684 format_STACK_PUSH(const struct ofpact_stack
*a
, struct ds
*s
)
2686 nxm_format_stack_push(a
, s
);
2690 format_STACK_POP(const struct ofpact_stack
*a
, struct ds
*s
)
2692 nxm_format_stack_pop(a
, s
);
2695 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2697 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2698 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2699 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2700 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2702 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2703 * sent only to controllers with id 0.)
2705 struct nx_action_cnt_ids
{
2706 ovs_be16 type
; /* OFPAT_VENDOR. */
2707 ovs_be16 len
; /* Length including slaves. */
2708 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2709 ovs_be16 subtype
; /* NXAST_DEC_TTL_CNT_IDS. */
2711 ovs_be16 n_controllers
; /* Number of controllers. */
2712 uint8_t zeros
[4]; /* Must be zero. */
2714 /* Followed by 1 or more controller ids.
2716 * uint16_t cnt_ids[]; // Controller ids.
2717 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2720 OFP_ASSERT(sizeof(struct nx_action_cnt_ids
) == 16);
2723 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf
*out
)
2726 struct ofpact_cnt_ids
*ids
;
2727 enum ofperr error
= 0;
2729 ids
= ofpact_put_DEC_TTL(out
);
2730 ids
->n_controllers
= 1;
2731 ofpbuf_put(out
, &id
, sizeof id
);
2733 ofpact_update_len(out
, &ids
->ofpact
);
2738 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids
*nac_ids
,
2741 struct ofpact_cnt_ids
*ids
;
2745 ids
= ofpact_put_DEC_TTL(out
);
2746 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
2747 ids
->n_controllers
= ntohs(nac_ids
->n_controllers
);
2748 ids_size
= ntohs(nac_ids
->len
) - sizeof *nac_ids
;
2750 if (!is_all_zeros(nac_ids
->zeros
, sizeof nac_ids
->zeros
)) {
2751 return OFPERR_NXBRC_MUST_BE_ZERO
;
2754 if (ids_size
< ids
->n_controllers
* sizeof(ovs_be16
)) {
2755 VLOG_WARN_RL(&rl
, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE
" "
2756 "bytes allocated for controller ids. %"PRIuSIZE
" bytes "
2757 "are required for %"PRIu16
" controllers.",
2758 ids_size
, ids
->n_controllers
* sizeof(ovs_be16
),
2759 ids
->n_controllers
);
2760 return OFPERR_OFPBAC_BAD_LEN
;
2763 for (i
= 0; i
< ids
->n_controllers
; i
++) {
2764 uint16_t id
= ntohs(((ovs_be16
*)(nac_ids
+ 1))[i
]);
2765 ofpbuf_put(out
, &id
, sizeof id
);
2769 ofpact_update_len(out
, &ids
->ofpact
);
2775 encode_DEC_TTL(const struct ofpact_cnt_ids
*dec_ttl
,
2776 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2778 if (dec_ttl
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
2779 || dec_ttl
->n_controllers
!= 1
2780 || dec_ttl
->cnt_ids
[0] != 0) {
2781 struct nx_action_cnt_ids
*nac_ids
= put_NXAST_DEC_TTL_CNT_IDS(out
);
2782 int ids_len
= ROUND_UP(2 * dec_ttl
->n_controllers
, OFP_ACTION_ALIGN
);
2786 nac_ids
->len
= htons(ntohs(nac_ids
->len
) + ids_len
);
2787 nac_ids
->n_controllers
= htons(dec_ttl
->n_controllers
);
2789 ids
= ofpbuf_put_zeros(out
, ids_len
);
2790 for (i
= 0; i
< dec_ttl
->n_controllers
; i
++) {
2791 ids
[i
] = htons(dec_ttl
->cnt_ids
[i
]);
2794 put_OFPAT_DEC_NW_TTL(out
, ofp_version
);
2799 parse_noargs_dec_ttl(struct ofpbuf
*ofpacts
)
2801 struct ofpact_cnt_ids
*ids
;
2804 ofpact_put_DEC_TTL(ofpacts
);
2805 ofpbuf_put(ofpacts
, &id
, sizeof id
);
2806 ids
= ofpacts
->header
;
2807 ids
->n_controllers
++;
2808 ofpact_update_len(ofpacts
, &ids
->ofpact
);
2811 static char * OVS_WARN_UNUSED_RESULT
2812 parse_DEC_TTL(char *arg
, struct ofpbuf
*ofpacts
,
2813 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2816 parse_noargs_dec_ttl(ofpacts
);
2818 struct ofpact_cnt_ids
*ids
;
2821 ids
= ofpact_put_DEC_TTL(ofpacts
);
2822 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
2823 for (cntr
= strtok_r(arg
, ", ", &arg
); cntr
!= NULL
;
2824 cntr
= strtok_r(NULL
, ", ", &arg
)) {
2825 uint16_t id
= atoi(cntr
);
2827 ofpbuf_put(ofpacts
, &id
, sizeof id
);
2828 ids
= ofpacts
->header
;
2829 ids
->n_controllers
++;
2831 if (!ids
->n_controllers
) {
2832 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2835 ofpact_update_len(ofpacts
, &ids
->ofpact
);
2841 format_DEC_TTL(const struct ofpact_cnt_ids
*a
, struct ds
*s
)
2845 ds_put_cstr(s
, "dec_ttl");
2846 if (a
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
) {
2847 ds_put_cstr(s
, "(");
2848 for (i
= 0; i
< a
->n_controllers
; i
++) {
2850 ds_put_cstr(s
, ",");
2852 ds_put_format(s
, "%"PRIu16
, a
->cnt_ids
[i
]);
2854 ds_put_cstr(s
, ")");
2858 /* Set MPLS label actions. */
2861 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label
, struct ofpbuf
*out
)
2863 ofpact_put_SET_MPLS_LABEL(out
)->label
= label
;
2868 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label
*label
,
2869 enum ofp_version ofp_version
,
2872 if (ofp_version
< OFP12_VERSION
) {
2873 put_OFPAT_SET_MPLS_LABEL(out
, ofp_version
, label
->label
);
2875 ofpact_put_set_field(out
, ofp_version
, MFF_MPLS_LABEL
,
2876 ntohl(label
->label
));
2880 static char * OVS_WARN_UNUSED_RESULT
2881 parse_SET_MPLS_LABEL(char *arg
, struct ofpbuf
*ofpacts
,
2882 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2884 struct ofpact_mpls_label
*mpls_label
= ofpact_put_SET_MPLS_LABEL(ofpacts
);
2886 return xstrdup("set_mpls_label: expected label.");
2889 mpls_label
->label
= htonl(atoi(arg
));
2894 format_SET_MPLS_LABEL(const struct ofpact_mpls_label
*a
, struct ds
*s
)
2896 ds_put_format(s
, "set_mpls_label(%"PRIu32
")", ntohl(a
->label
));
2899 /* Set MPLS TC actions. */
2902 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc
, struct ofpbuf
*out
)
2904 ofpact_put_SET_MPLS_TC(out
)->tc
= tc
;
2909 encode_SET_MPLS_TC(const struct ofpact_mpls_tc
*tc
,
2910 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2912 if (ofp_version
< OFP12_VERSION
) {
2913 put_OFPAT_SET_MPLS_TC(out
, ofp_version
, tc
->tc
);
2915 ofpact_put_set_field(out
, ofp_version
, MFF_MPLS_TC
, tc
->tc
);
2919 static char * OVS_WARN_UNUSED_RESULT
2920 parse_SET_MPLS_TC(char *arg
, struct ofpbuf
*ofpacts
,
2921 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2923 struct ofpact_mpls_tc
*mpls_tc
= ofpact_put_SET_MPLS_TC(ofpacts
);
2926 return xstrdup("set_mpls_tc: expected tc.");
2929 mpls_tc
->tc
= atoi(arg
);
2934 format_SET_MPLS_TC(const struct ofpact_mpls_tc
*a
, struct ds
*s
)
2936 ds_put_format(s
, "set_mpls_ttl(%"PRIu8
")", a
->tc
);
2939 /* Set MPLS TTL actions. */
2942 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl
, struct ofpbuf
*out
)
2944 ofpact_put_SET_MPLS_TTL(out
)->ttl
= ttl
;
2949 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*ttl
,
2950 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2952 put_OFPAT_SET_MPLS_TTL(out
, ofp_version
, ttl
->ttl
);
2955 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
2956 * action to 'ofpacts'.
2958 * Returns NULL if successful, otherwise a malloc()'d string describing the
2959 * error. The caller is responsible for freeing the returned string. */
2960 static char * OVS_WARN_UNUSED_RESULT
2961 parse_SET_MPLS_TTL(char *arg
, struct ofpbuf
*ofpacts
,
2962 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2964 struct ofpact_mpls_ttl
*mpls_ttl
= ofpact_put_SET_MPLS_TTL(ofpacts
);
2967 return xstrdup("set_mpls_ttl: expected ttl.");
2970 mpls_ttl
->ttl
= atoi(arg
);
2975 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*a
, struct ds
*s
)
2977 ds_put_format(s
, "set_mpls_ttl(%"PRIu8
")", a
->ttl
);
2980 /* Decrement MPLS TTL actions. */
2983 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf
*out
)
2985 ofpact_put_DEC_MPLS_TTL(out
);
2990 encode_DEC_MPLS_TTL(const struct ofpact_null
*null OVS_UNUSED
,
2991 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2993 put_OFPAT_DEC_MPLS_TTL(out
, ofp_version
);
2996 static char * OVS_WARN_UNUSED_RESULT
2997 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
2998 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3000 ofpact_put_DEC_MPLS_TTL(ofpacts
);
3005 format_DEC_MPLS_TTL(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
3007 ds_put_cstr(s
, "dec_mpls_ttl");
3010 /* Push MPLS label action. */
3013 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype
, struct ofpbuf
*out
)
3015 struct ofpact_push_mpls
*oam
;
3017 if (!eth_type_mpls(ethertype
)) {
3018 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3020 oam
= ofpact_put_PUSH_MPLS(out
);
3021 oam
->ethertype
= ethertype
;
3027 encode_PUSH_MPLS(const struct ofpact_push_mpls
*push_mpls
,
3028 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3030 put_OFPAT_PUSH_MPLS(out
, ofp_version
, push_mpls
->ethertype
);
3033 static char * OVS_WARN_UNUSED_RESULT
3034 parse_PUSH_MPLS(char *arg
, struct ofpbuf
*ofpacts
,
3035 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3040 error
= str_to_u16(arg
, "push_mpls", ðertype
);
3042 ofpact_put_PUSH_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
3048 format_PUSH_MPLS(const struct ofpact_push_mpls
*a
, struct ds
*s
)
3050 ds_put_format(s
, "push_mpls:0x%04"PRIx16
, ntohs(a
->ethertype
));
3053 /* Pop MPLS label action. */
3056 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype
, struct ofpbuf
*out
)
3058 ofpact_put_POP_MPLS(out
)->ethertype
= ethertype
;
3063 encode_POP_MPLS(const struct ofpact_pop_mpls
*pop_mpls
,
3064 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3066 put_OFPAT_POP_MPLS(out
, ofp_version
, pop_mpls
->ethertype
);
3069 static char * OVS_WARN_UNUSED_RESULT
3070 parse_POP_MPLS(char *arg
, struct ofpbuf
*ofpacts
,
3071 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3076 error
= str_to_u16(arg
, "pop_mpls", ðertype
);
3078 ofpact_put_POP_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
3084 format_POP_MPLS(const struct ofpact_pop_mpls
*a
, struct ds
*s
)
3086 ds_put_format(s
, "pop_mpls:0x%04"PRIx16
, ntohs(a
->ethertype
));
3089 /* Set tunnel ID actions. */
3092 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id
, struct ofpbuf
*out
)
3094 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
3095 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL
;
3096 tunnel
->tun_id
= tun_id
;
3101 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id
, struct ofpbuf
*out
)
3103 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
3104 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL64
;
3105 tunnel
->tun_id
= tun_id
;
3110 encode_SET_TUNNEL(const struct ofpact_tunnel
*tunnel
,
3111 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3113 uint64_t tun_id
= tunnel
->tun_id
;
3115 if (ofp_version
< OFP12_VERSION
) {
3116 if (tun_id
<= UINT32_MAX
3117 && tunnel
->ofpact
.raw
!= NXAST_RAW_SET_TUNNEL64
) {
3118 put_NXAST_SET_TUNNEL(out
, tun_id
);
3120 put_NXAST_SET_TUNNEL64(out
, tun_id
);
3123 ofpact_put_set_field(out
, ofp_version
, MFF_TUN_ID
, tun_id
);
3127 static char * OVS_WARN_UNUSED_RESULT
3128 parse_set_tunnel(char *arg
, struct ofpbuf
*ofpacts
,
3129 enum ofp_raw_action_type raw
)
3131 struct ofpact_tunnel
*tunnel
;
3133 tunnel
= ofpact_put_SET_TUNNEL(ofpacts
);
3134 tunnel
->ofpact
.raw
= raw
;
3135 return str_to_u64(arg
, &tunnel
->tun_id
);
3138 static char * OVS_WARN_UNUSED_RESULT
3139 parse_SET_TUNNEL(char *arg
, struct ofpbuf
*ofpacts
,
3140 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3142 return parse_set_tunnel(arg
, ofpacts
, NXAST_RAW_SET_TUNNEL
);
3146 format_SET_TUNNEL(const struct ofpact_tunnel
*a
, struct ds
*s
)
3148 ds_put_format(s
, "set_tunnel%s:%#"PRIx64
,
3149 (a
->tun_id
> UINT32_MAX
3150 || a
->ofpact
.raw
== NXAST_RAW_SET_TUNNEL64
? "64" : ""),
3154 /* Set queue action. */
3157 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id
, struct ofpbuf
*out
)
3159 ofpact_put_SET_QUEUE(out
)->queue_id
= queue_id
;
3164 encode_SET_QUEUE(const struct ofpact_queue
*queue
,
3165 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3167 put_OFPAT_SET_QUEUE(out
, ofp_version
, queue
->queue_id
);
3170 static char * OVS_WARN_UNUSED_RESULT
3171 parse_SET_QUEUE(char *arg
, struct ofpbuf
*ofpacts
,
3172 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3174 return str_to_u32(arg
, &ofpact_put_SET_QUEUE(ofpacts
)->queue_id
);
3178 format_SET_QUEUE(const struct ofpact_queue
*a
, struct ds
*s
)
3180 ds_put_format(s
, "set_queue:%"PRIu32
, a
->queue_id
);
3183 /* Pop queue action. */
3186 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf
*out
)
3188 ofpact_put_POP_QUEUE(out
);
3193 encode_POP_QUEUE(const struct ofpact_null
*null OVS_UNUSED
,
3194 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3196 put_NXAST_POP_QUEUE(out
);
3199 static char * OVS_WARN_UNUSED_RESULT
3200 parse_POP_QUEUE(const char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
3201 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3203 ofpact_put_POP_QUEUE(ofpacts
);
3208 format_POP_QUEUE(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
3210 ds_put_cstr(s
, "pop_queue");
3213 /* Action structure for NXAST_FIN_TIMEOUT.
3215 * This action changes the idle timeout or hard timeout, or both, of this
3216 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3217 * When such a packet is observed, the action reduces the rule's idle timeout
3218 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3219 * action has no effect on an existing timeout that is already shorter than the
3220 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3221 * of zero has no effect on the respective timeout.
3223 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3224 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3225 * receipt of the FIN or RST.
3227 * This is useful for quickly discarding learned TCP flows that otherwise will
3228 * take a long time to expire.
3230 * This action is intended for use with an OpenFlow rule that matches only a
3231 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3232 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3233 * RST in any of those flows will cause the entire OpenFlow rule to expire
3234 * early, which is not normally desirable.
3236 struct nx_action_fin_timeout
{
3237 ovs_be16 type
; /* OFPAT_VENDOR. */
3238 ovs_be16 len
; /* 16. */
3239 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3240 ovs_be16 subtype
; /* NXAST_FIN_TIMEOUT. */
3241 ovs_be16 fin_idle_timeout
; /* New idle timeout, if nonzero. */
3242 ovs_be16 fin_hard_timeout
; /* New hard timeout, if nonzero. */
3243 ovs_be16 pad
; /* Must be zero. */
3245 OFP_ASSERT(sizeof(struct nx_action_fin_timeout
) == 16);
3248 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout
*naft
,
3251 struct ofpact_fin_timeout
*oft
;
3253 oft
= ofpact_put_FIN_TIMEOUT(out
);
3254 oft
->fin_idle_timeout
= ntohs(naft
->fin_idle_timeout
);
3255 oft
->fin_hard_timeout
= ntohs(naft
->fin_hard_timeout
);
3260 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout
*fin_timeout
,
3261 enum ofp_version ofp_version OVS_UNUSED
,
3264 struct nx_action_fin_timeout
*naft
= put_NXAST_FIN_TIMEOUT(out
);
3265 naft
->fin_idle_timeout
= htons(fin_timeout
->fin_idle_timeout
);
3266 naft
->fin_hard_timeout
= htons(fin_timeout
->fin_hard_timeout
);
3269 static char * OVS_WARN_UNUSED_RESULT
3270 parse_FIN_TIMEOUT(char *arg
, struct ofpbuf
*ofpacts
,
3271 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3273 struct ofpact_fin_timeout
*oft
= ofpact_put_FIN_TIMEOUT(ofpacts
);
3276 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
3279 if (!strcmp(key
, "idle_timeout")) {
3280 error
= str_to_u16(value
, key
, &oft
->fin_idle_timeout
);
3281 } else if (!strcmp(key
, "hard_timeout")) {
3282 error
= str_to_u16(value
, key
, &oft
->fin_hard_timeout
);
3284 error
= xasprintf("invalid key '%s' in 'fin_timeout' argument",
3296 format_FIN_TIMEOUT(const struct ofpact_fin_timeout
*a
, struct ds
*s
)
3298 ds_put_cstr(s
, "fin_timeout(");
3299 if (a
->fin_idle_timeout
) {
3300 ds_put_format(s
, "idle_timeout=%"PRIu16
",", a
->fin_idle_timeout
);
3302 if (a
->fin_hard_timeout
) {
3303 ds_put_format(s
, "hard_timeout=%"PRIu16
",", a
->fin_hard_timeout
);
3306 ds_put_char(s
, ')');
3309 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3311 * These actions search one of the switch's flow tables:
3313 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3314 * it specifies the table to search.
3316 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3317 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3318 * table, that is, the OpenFlow flow table that contains the flow from
3319 * which this action was obtained. If this action did not come from a
3320 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3321 * is the current table.
3323 * The flow table lookup uses a flow that may be slightly modified from the
3326 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3327 * is used as the flow's in_port.
3329 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3330 * then its value is used as the flow's in_port. Otherwise, the original
3333 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3334 * resubmit action, then the flow is updated with the new values.
3336 * Following the lookup, the original in_port is restored.
3338 * If the modified flow matched in the flow table, then the corresponding
3339 * actions are executed. Afterward, actions following the resubmit in the
3340 * original set of actions, if any, are executed; any changes made to the
3341 * packet (e.g. changes to VLAN) by secondary actions persist when those
3342 * actions are executed, although the original in_port is restored.
3344 * Resubmit actions may be used any number of times within a set of actions.
3346 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3347 * implementation-defined depth, further resubmit actions are simply ignored.
3349 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3350 * 'pad' to be all-bits-zero.
3352 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3353 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3355 struct nx_action_resubmit
{
3356 ovs_be16 type
; /* OFPAT_VENDOR. */
3357 ovs_be16 len
; /* Length is 16. */
3358 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3359 ovs_be16 subtype
; /* NXAST_RESUBMIT. */
3360 ovs_be16 in_port
; /* New in_port for checking flow table. */
3361 uint8_t table
; /* NXAST_RESUBMIT_TABLE: table to use. */
3364 OFP_ASSERT(sizeof(struct nx_action_resubmit
) == 16);
3367 decode_NXAST_RAW_RESUBMIT(uint16_t port
, struct ofpbuf
*out
)
3369 struct ofpact_resubmit
*resubmit
;
3371 resubmit
= ofpact_put_RESUBMIT(out
);
3372 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT
;
3373 resubmit
->in_port
= u16_to_ofp(port
);
3374 resubmit
->table_id
= 0xff;
3379 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit
*nar
,
3382 struct ofpact_resubmit
*resubmit
;
3384 if (nar
->pad
[0] || nar
->pad
[1] || nar
->pad
[2]) {
3385 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3388 resubmit
= ofpact_put_RESUBMIT(out
);
3389 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT_TABLE
;
3390 resubmit
->in_port
= u16_to_ofp(ntohs(nar
->in_port
));
3391 resubmit
->table_id
= nar
->table
;
3396 encode_RESUBMIT(const struct ofpact_resubmit
*resubmit
,
3397 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3399 uint16_t in_port
= ofp_to_u16(resubmit
->in_port
);
3401 if (resubmit
->table_id
== 0xff
3402 && resubmit
->ofpact
.raw
!= NXAST_RAW_RESUBMIT_TABLE
) {
3403 put_NXAST_RESUBMIT(out
, in_port
);
3405 struct nx_action_resubmit
*nar
= put_NXAST_RESUBMIT_TABLE(out
);
3406 nar
->table
= resubmit
->table_id
;
3407 nar
->in_port
= htons(in_port
);
3411 static char * OVS_WARN_UNUSED_RESULT
3412 parse_RESUBMIT(char *arg
, struct ofpbuf
*ofpacts
,
3413 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3415 struct ofpact_resubmit
*resubmit
;
3416 char *in_port_s
, *table_s
;
3418 resubmit
= ofpact_put_RESUBMIT(ofpacts
);
3420 in_port_s
= strsep(&arg
, ",");
3421 if (in_port_s
&& in_port_s
[0]) {
3422 if (!ofputil_port_from_string(in_port_s
, &resubmit
->in_port
)) {
3423 return xasprintf("%s: resubmit to unknown port", in_port_s
);
3426 resubmit
->in_port
= OFPP_IN_PORT
;
3429 table_s
= strsep(&arg
, ",");
3430 if (table_s
&& table_s
[0]) {
3431 uint32_t table_id
= 0;
3434 error
= str_to_u32(table_s
, &table_id
);
3438 resubmit
->table_id
= table_id
;
3440 resubmit
->table_id
= 255;
3443 if (resubmit
->in_port
== OFPP_IN_PORT
&& resubmit
->table_id
== 255) {
3444 return xstrdup("at least one \"in_port\" or \"table\" must be "
3445 "specified on resubmit");
3451 format_RESUBMIT(const struct ofpact_resubmit
*a
, struct ds
*s
)
3453 if (a
->in_port
!= OFPP_IN_PORT
&& a
->table_id
== 255) {
3454 ds_put_cstr(s
, "resubmit:");
3455 ofputil_format_port(a
->in_port
, s
);
3457 ds_put_format(s
, "resubmit(");
3458 if (a
->in_port
!= OFPP_IN_PORT
) {
3459 ofputil_format_port(a
->in_port
, s
);
3461 ds_put_char(s
, ',');
3462 if (a
->table_id
!= 255) {
3463 ds_put_format(s
, "%"PRIu8
, a
->table_id
);
3465 ds_put_char(s
, ')');
3469 /* Action structure for NXAST_LEARN.
3471 * This action adds or modifies a flow in an OpenFlow table, similar to
3472 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3473 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3474 * flow's match criteria and actions are built by applying each of the series
3475 * of flow_mod_spec elements included as part of the action.
3477 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3478 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3479 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3480 * bits from a source to a destination. In this case, the header contains
3483 * 15 14 13 12 11 10 0
3484 * +------+---+------+---------------------------------+
3485 * | 0 |src| dst | n_bits |
3486 * +------+---+------+---------------------------------+
3488 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3489 * following table summarizes the meaning of each possible combination.
3490 * Details follow the table:
3493 * --- --- ----------------------------------------------------------
3494 * 0 0 Add match criteria based on value in a field.
3495 * 1 0 Add match criteria based on an immediate value.
3496 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3497 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3498 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3499 * All other combinations are undefined and not allowed.
3501 * The flow_mod_spec header is followed by a source specification and a
3502 * destination specification. The format and meaning of the source
3503 * specification depends on 'src':
3505 * - If 'src' is 0, the source bits are taken from a field in the flow to
3506 * which this action is attached. (This should be a wildcarded field. If
3507 * its value is fully specified then the source bits being copied have
3510 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3511 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3512 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3513 * 'field' and 'ofs' are subject to the same restrictions as the source
3514 * field in NXAST_REG_MOVE.
3516 * - If 'src' is 1, the source bits are a constant value. The source
3517 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3518 * number in network order, the source bits are the 'n_bits'
3519 * least-significant bits. The switch will report an error if other bits
3520 * in the constant are nonzero.
3522 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3523 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3524 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3525 * meaning of the flow_mod_spec depends on 'dst':
3527 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3528 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3529 * packet equal the source bits. 'field' may be any nxm_header with
3530 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3532 * Order is significant. Earlier flow_mod_specs must satisfy any
3533 * prerequisites for matching fields specified later, by copying constant
3534 * values into prerequisite fields.
3536 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3539 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3540 * the new flow. The new flow copies the source bits into
3541 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3544 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3545 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3547 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3548 * empty. It has the following meaning:
3550 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3551 * The new flow outputs to the OpenFlow port specified by the source field.
3552 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3553 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3556 * Resource Management
3557 * -------------------
3559 * A switch has a finite amount of flow table space available for learning.
3560 * When this space is exhausted, no new learning table entries will be learned
3561 * until some existing flow table entries expire. The controller should be
3562 * prepared to handle this by flooding (which can be implemented as a
3563 * low-priority flow).
3565 * If a learned flow matches a single TCP stream with a relatively long
3566 * timeout, one may make the best of resource constraints by setting
3567 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3568 * both, to shorter timeouts. When either of these is specified as a nonzero
3569 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3575 * The following examples give a prose description of the flow_mod_specs along
3576 * with informal notation for how those would be represented and a hex dump of
3577 * the bytes that would be required.
3579 * These examples could work with various nx_action_learn parameters. Typical
3580 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3581 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3583 * 1. Learn input port based on the source MAC, with lookup into
3584 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3586 * Match on in_port=99:
3587 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3588 * ovs_be16(99), 00 63
3589 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3591 * Match Ethernet destination on Ethernet source from packet:
3592 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3593 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3594 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3596 * Set NXM_NX_REG1[16:31] to the packet's input port:
3597 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3598 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3599 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3601 * Given a packet that arrived on port A with Ethernet source address B,
3602 * this would set up the flow "in_port=99, dl_dst=B,
3603 * actions=load:A->NXM_NX_REG1[16..31]".
3605 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3606 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3607 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3609 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3610 * into NXM_NX_REG1[16:31]:
3612 * Match on same VLAN ID as packet:
3613 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3614 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3615 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3617 * Match Ethernet destination on Ethernet source from packet:
3618 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3619 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3620 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3622 * Output to the packet's input port:
3623 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3624 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3626 * Given a packet that arrived on port A with Ethernet source address B in
3627 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3628 * actions=output:A".
3630 * In syntax accepted by ovs-ofctl, this action is:
3631 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3632 * output:NXM_OF_IN_PORT[])
3634 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3635 * 10-second MAC expiration time to make it easier to see what's going on
3637 * ovs-vsctl del-controller br0
3638 * ovs-ofctl del-flows br0
3639 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3640 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3641 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3642 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3643 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3645 * You can then dump the MAC learning table with:
3647 * ovs-ofctl dump-flows br0 table=1
3652 * For best performance, segregate learned flows into a table that is not used
3653 * for any other flows except possibly for a lowest-priority "catch-all" flow
3654 * (a flow with no match criteria). If different learning actions specify
3655 * different match criteria, use different tables for the learned flows.
3657 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3658 * These timeouts apply to the flow that is added, which means that a flow with
3659 * an idle timeout will expire when no traffic has been sent *to* the learned
3660 * address. This is not usually the intent in MAC learning; instead, we want
3661 * the MAC learn entry to expire when no traffic has been sent *from* the
3662 * learned address. Use a hard timeout for that.
3665 * Visibility of Changes
3666 * ---------------------
3668 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3669 * flow translation are visible to flow table lookups made later in the flow
3670 * translation. This means that, in the example above, a MAC learned by the
3671 * learn action in table 0 would be found in table 1 (if the packet being
3672 * processed had the same source and destination MAC address).
3674 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3675 * modify a flow) by a "learn" action are visible only for later flow
3676 * translations, not for later lookups within the same flow translation. In
3677 * the MAC learning example, a MAC learned by the learn action in table 0 would
3678 * not be found in table 1 if the flow translation would resubmit to table 1
3679 * after the processing of the learn action, meaning that if this MAC had not
3680 * been learned before then the packet would be flooded. */
3681 struct nx_action_learn
{
3682 ovs_be16 type
; /* OFPAT_VENDOR. */
3683 ovs_be16 len
; /* At least 24. */
3684 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3685 ovs_be16 subtype
; /* NXAST_LEARN. */
3686 ovs_be16 idle_timeout
; /* Idle time before discarding (seconds). */
3687 ovs_be16 hard_timeout
; /* Max time before discarding (seconds). */
3688 ovs_be16 priority
; /* Priority level of flow entry. */
3689 ovs_be64 cookie
; /* Cookie for new flow. */
3690 ovs_be16 flags
; /* NX_LEARN_F_*. */
3691 uint8_t table_id
; /* Table to insert flow entry. */
3692 uint8_t pad
; /* Must be zero. */
3693 ovs_be16 fin_idle_timeout
; /* Idle timeout after FIN, if nonzero. */
3694 ovs_be16 fin_hard_timeout
; /* Hard timeout after FIN, if nonzero. */
3695 /* Followed by a sequence of flow_mod_spec elements, as described above,
3696 * until the end of the action is reached. */
3698 OFP_ASSERT(sizeof(struct nx_action_learn
) == 32);
3701 get_be16(const void **pp
)
3703 const ovs_be16
*p
= *pp
;
3704 ovs_be16 value
= *p
;
3710 get_be32(const void **pp
)
3712 const ovs_be32
*p
= *pp
;
3713 ovs_be32 value
= get_unaligned_be32(p
);
3719 get_subfield(int n_bits
, const void **p
, struct mf_subfield
*sf
)
3721 sf
->field
= mf_from_nxm_header(ntohl(get_be32(p
)));
3722 sf
->ofs
= ntohs(get_be16(p
));
3723 sf
->n_bits
= n_bits
;
3727 learn_min_len(uint16_t header
)
3729 int n_bits
= header
& NX_LEARN_N_BITS_MASK
;
3730 int src_type
= header
& NX_LEARN_SRC_MASK
;
3731 int dst_type
= header
& NX_LEARN_DST_MASK
;
3732 unsigned int min_len
;
3735 if (src_type
== NX_LEARN_SRC_FIELD
) {
3736 min_len
+= sizeof(ovs_be32
); /* src_field */
3737 min_len
+= sizeof(ovs_be16
); /* src_ofs */
3739 min_len
+= DIV_ROUND_UP(n_bits
, 16);
3741 if (dst_type
== NX_LEARN_DST_MATCH
||
3742 dst_type
== NX_LEARN_DST_LOAD
) {
3743 min_len
+= sizeof(ovs_be32
); /* dst_field */
3744 min_len
+= sizeof(ovs_be16
); /* dst_ofs */
3749 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3750 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3752 decode_NXAST_RAW_LEARN(const struct nx_action_learn
*nal
,
3753 struct ofpbuf
*ofpacts
)
3755 struct ofpact_learn
*learn
;
3756 const void *p
, *end
;
3759 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3762 learn
= ofpact_put_LEARN(ofpacts
);
3764 learn
->idle_timeout
= ntohs(nal
->idle_timeout
);
3765 learn
->hard_timeout
= ntohs(nal
->hard_timeout
);
3766 learn
->priority
= ntohs(nal
->priority
);
3767 learn
->cookie
= nal
->cookie
;
3768 learn
->table_id
= nal
->table_id
;
3769 learn
->fin_idle_timeout
= ntohs(nal
->fin_idle_timeout
);
3770 learn
->fin_hard_timeout
= ntohs(nal
->fin_hard_timeout
);
3772 learn
->flags
= ntohs(nal
->flags
);
3773 if (learn
->flags
& ~(NX_LEARN_F_SEND_FLOW_REM
|
3774 NX_LEARN_F_DELETE_LEARNED
)) {
3775 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3778 if (learn
->table_id
== 0xff) {
3779 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3782 end
= (char *) nal
+ ntohs(nal
->len
);
3783 for (p
= nal
+ 1; p
!= end
; ) {
3784 struct ofpact_learn_spec
*spec
;
3785 uint16_t header
= ntohs(get_be16(&p
));
3791 spec
= ofpbuf_put_zeros(ofpacts
, sizeof *spec
);
3792 learn
= ofpacts
->header
;
3795 spec
->src_type
= header
& NX_LEARN_SRC_MASK
;
3796 spec
->dst_type
= header
& NX_LEARN_DST_MASK
;
3797 spec
->n_bits
= header
& NX_LEARN_N_BITS_MASK
;
3799 /* Check for valid src and dst type combination. */
3800 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
3801 spec
->dst_type
== NX_LEARN_DST_LOAD
||
3802 (spec
->dst_type
== NX_LEARN_DST_OUTPUT
&&
3803 spec
->src_type
== NX_LEARN_SRC_FIELD
)) {
3806 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3809 /* Check that the arguments don't overrun the end of the action. */
3810 if ((char *) end
- (char *) p
< learn_min_len(header
)) {
3811 return OFPERR_OFPBAC_BAD_LEN
;
3814 /* Get the source. */
3815 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
3816 get_subfield(spec
->n_bits
, &p
, &spec
->src
);
3818 int p_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
3820 bitwise_copy(p
, p_bytes
, 0,
3821 &spec
->src_imm
, sizeof spec
->src_imm
, 0,
3823 p
= (const uint8_t *) p
+ p_bytes
;
3826 /* Get the destination. */
3827 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
3828 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
3829 get_subfield(spec
->n_bits
, &p
, &spec
->dst
);
3832 ofpact_update_len(ofpacts
, &learn
->ofpact
);
3834 if (!is_all_zeros(p
, (char *) end
- (char *) p
)) {
3835 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3842 put_be16(struct ofpbuf
*b
, ovs_be16 x
)
3844 ofpbuf_put(b
, &x
, sizeof x
);
3848 put_be32(struct ofpbuf
*b
, ovs_be32 x
)
3850 ofpbuf_put(b
, &x
, sizeof x
);
3854 put_u16(struct ofpbuf
*b
, uint16_t x
)
3856 put_be16(b
, htons(x
));
3860 put_u32(struct ofpbuf
*b
, uint32_t x
)
3862 put_be32(b
, htonl(x
));
3866 encode_LEARN(const struct ofpact_learn
*learn
,
3867 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3869 const struct ofpact_learn_spec
*spec
;
3870 struct nx_action_learn
*nal
;
3873 start_ofs
= out
->size
;
3874 nal
= put_NXAST_LEARN(out
);
3875 nal
->idle_timeout
= htons(learn
->idle_timeout
);
3876 nal
->hard_timeout
= htons(learn
->hard_timeout
);
3877 nal
->fin_idle_timeout
= htons(learn
->fin_idle_timeout
);
3878 nal
->fin_hard_timeout
= htons(learn
->fin_hard_timeout
);
3879 nal
->priority
= htons(learn
->priority
);
3880 nal
->cookie
= learn
->cookie
;
3881 nal
->flags
= htons(learn
->flags
);
3882 nal
->table_id
= learn
->table_id
;
3884 for (spec
= learn
->specs
; spec
< &learn
->specs
[learn
->n_specs
]; spec
++) {
3885 put_u16(out
, spec
->n_bits
| spec
->dst_type
| spec
->src_type
);
3887 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
3888 put_u32(out
, mf_nxm_header(spec
->src
.field
->id
));
3889 put_u16(out
, spec
->src
.ofs
);
3891 size_t n_dst_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
3892 uint8_t *bits
= ofpbuf_put_zeros(out
, n_dst_bytes
);
3893 bitwise_copy(&spec
->src_imm
, sizeof spec
->src_imm
, 0,
3894 bits
, n_dst_bytes
, 0,
3898 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
3899 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
3900 put_u32(out
, mf_nxm_header(spec
->dst
.field
->id
));
3901 put_u16(out
, spec
->dst
.ofs
);
3905 pad_ofpat(out
, start_ofs
);
3908 static char * OVS_WARN_UNUSED_RESULT
3909 parse_LEARN(char *arg
, struct ofpbuf
*ofpacts
,
3910 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3912 return learn_parse(arg
, ofpacts
);
3916 format_LEARN(const struct ofpact_learn
*a
, struct ds
*s
)
3921 /* Action structure for NXAST_CONJUNCTION. */
3922 struct nx_action_conjunction
{
3923 ovs_be16 type
; /* OFPAT_VENDOR. */
3924 ovs_be16 len
; /* At least 16. */
3925 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3926 ovs_be16 subtype
; /* See enum ofp_raw_action_type. */
3931 OFP_ASSERT(sizeof(struct nx_action_conjunction
) == 16);
3934 add_conjunction(struct ofpbuf
*out
,
3935 uint32_t id
, uint8_t clause
, uint8_t n_clauses
)
3937 struct ofpact_conjunction
*oc
;
3939 oc
= ofpact_put_CONJUNCTION(out
);
3941 oc
->clause
= clause
;
3942 oc
->n_clauses
= n_clauses
;
3946 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction
*nac
,
3949 if (nac
->n_clauses
< 2 || nac
->n_clauses
> 64
3950 || nac
->clause
>= nac
->n_clauses
) {
3951 return OFPERR_NXBAC_BAD_CONJUNCTION
;
3953 add_conjunction(out
, ntohl(nac
->id
), nac
->clause
, nac
->n_clauses
);
3959 encode_CONJUNCTION(const struct ofpact_conjunction
*oc
,
3960 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3962 struct nx_action_conjunction
*nac
= put_NXAST_CONJUNCTION(out
);
3963 nac
->clause
= oc
->clause
;
3964 nac
->n_clauses
= oc
->n_clauses
;
3965 nac
->id
= htonl(oc
->id
);
3969 format_CONJUNCTION(const struct ofpact_conjunction
*oc
, struct ds
*s
)
3971 ds_put_format(s
, "conjunction(%"PRIu32
",%"PRIu8
"/%"PRIu8
")",
3972 oc
->id
, oc
->clause
+ 1, oc
->n_clauses
);
3975 static char * OVS_WARN_UNUSED_RESULT
3976 parse_CONJUNCTION(const char *arg
, struct ofpbuf
*ofpacts
,
3977 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3984 if (!ovs_scan(arg
, "%"SCNi32
" , %"SCNu8
" / %"SCNu8
" %n",
3985 &id
, &clause
, &n_clauses
, &n
) || n
!= strlen(arg
)) {
3986 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
3989 if (n_clauses
< 2) {
3990 return xstrdup("conjunction must have at least 2 clauses");
3991 } else if (n_clauses
> 64) {
3992 return xstrdup("conjunction must have at most 64 clauses");
3993 } else if (clause
< 1) {
3994 return xstrdup("clause index must be positive");
3995 } else if (clause
> n_clauses
) {
3996 return xstrdup("clause index must be less than or equal to "
3997 "number of clauses");
4000 add_conjunction(ofpacts
, id
, clause
- 1, n_clauses
);
4004 /* Action structure for NXAST_MULTIPATH.
4006 * This action performs the following steps in sequence:
4008 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4009 * Refer to the definition of "enum nx_mp_fields" for details.
4011 * The 'basis' value is used as a universal hash parameter, that is,
4012 * different values of 'basis' yield different hash functions. The
4013 * particular universal hash function used is implementation-defined.
4015 * The hashed fields' values are drawn from the current state of the
4016 * flow, including all modifications that have been made by actions up to
4019 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4020 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4023 * The output of the algorithm is 'link', an unsigned integer less than
4024 * or equal to 'max_link'.
4026 * Some algorithms use 'arg' as an additional argument.
4028 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4029 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4032 * The switch will reject actions that have an unknown 'fields', or an unknown
4033 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4034 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4035 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4037 struct nx_action_multipath
{
4038 ovs_be16 type
; /* OFPAT_VENDOR. */
4039 ovs_be16 len
; /* Length is 32. */
4040 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4041 ovs_be16 subtype
; /* NXAST_MULTIPATH. */
4043 /* What fields to hash and how. */
4044 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
4045 ovs_be16 basis
; /* Universal hash parameter. */
4048 /* Multipath link choice algorithm to apply to hash value. */
4049 ovs_be16 algorithm
; /* One of NX_MP_ALG_*. */
4050 ovs_be16 max_link
; /* Number of output links, minus 1. */
4051 ovs_be32 arg
; /* Algorithm-specific argument. */
4054 /* Where to store the result. */
4055 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
4056 ovs_be32 dst
; /* Destination. */
4058 OFP_ASSERT(sizeof(struct nx_action_multipath
) == 32);
4061 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath
*nam
,
4064 uint32_t n_links
= ntohs(nam
->max_link
) + 1;
4065 size_t min_n_bits
= log_2_ceil(n_links
);
4066 struct ofpact_multipath
*mp
;
4068 mp
= ofpact_put_MULTIPATH(out
);
4069 mp
->fields
= ntohs(nam
->fields
);
4070 mp
->basis
= ntohs(nam
->basis
);
4071 mp
->algorithm
= ntohs(nam
->algorithm
);
4072 mp
->max_link
= ntohs(nam
->max_link
);
4073 mp
->arg
= ntohl(nam
->arg
);
4074 mp
->dst
.field
= mf_from_nxm_header(ntohl(nam
->dst
));
4075 mp
->dst
.ofs
= nxm_decode_ofs(nam
->ofs_nbits
);
4076 mp
->dst
.n_bits
= nxm_decode_n_bits(nam
->ofs_nbits
);
4078 if (!flow_hash_fields_valid(mp
->fields
)) {
4079 VLOG_WARN_RL(&rl
, "unsupported fields %d", (int) mp
->fields
);
4080 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4081 } else if (mp
->algorithm
!= NX_MP_ALG_MODULO_N
4082 && mp
->algorithm
!= NX_MP_ALG_HASH_THRESHOLD
4083 && mp
->algorithm
!= NX_MP_ALG_HRW
4084 && mp
->algorithm
!= NX_MP_ALG_ITER_HASH
) {
4085 VLOG_WARN_RL(&rl
, "unsupported algorithm %d", (int) mp
->algorithm
);
4086 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4087 } else if (mp
->dst
.n_bits
< min_n_bits
) {
4088 VLOG_WARN_RL(&rl
, "multipath action requires at least %"PRIuSIZE
" bits for "
4089 "%"PRIu32
" links", min_n_bits
, n_links
);
4090 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4093 return multipath_check(mp
, NULL
);
4097 encode_MULTIPATH(const struct ofpact_multipath
*mp
,
4098 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4100 struct nx_action_multipath
*nam
= put_NXAST_MULTIPATH(out
);
4102 nam
->fields
= htons(mp
->fields
);
4103 nam
->basis
= htons(mp
->basis
);
4104 nam
->algorithm
= htons(mp
->algorithm
);
4105 nam
->max_link
= htons(mp
->max_link
);
4106 nam
->arg
= htonl(mp
->arg
);
4107 nam
->ofs_nbits
= nxm_encode_ofs_nbits(mp
->dst
.ofs
, mp
->dst
.n_bits
);
4108 nam
->dst
= htonl(mf_nxm_header(mp
->dst
.field
->id
));
4111 static char * OVS_WARN_UNUSED_RESULT
4112 parse_MULTIPATH(const char *arg
, struct ofpbuf
*ofpacts
,
4113 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4115 return multipath_parse(ofpact_put_MULTIPATH(ofpacts
), arg
);
4119 format_MULTIPATH(const struct ofpact_multipath
*a
, struct ds
*s
)
4121 multipath_format(a
, s
);
4124 /* Action structure for NXAST_NOTE.
4126 * This action has no effect. It is variable length. The switch does not
4127 * attempt to interpret the user-defined 'note' data in any way. A controller
4128 * can use this action to attach arbitrary metadata to a flow.
4130 * This action might go away in the future.
4132 struct nx_action_note
{
4133 ovs_be16 type
; /* OFPAT_VENDOR. */
4134 ovs_be16 len
; /* A multiple of 8, but at least 16. */
4135 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4136 ovs_be16 subtype
; /* NXAST_NOTE. */
4137 uint8_t note
[6]; /* Start of user-defined data. */
4138 /* Possibly followed by additional user-defined data. */
4140 OFP_ASSERT(sizeof(struct nx_action_note
) == 16);
4143 decode_NXAST_RAW_NOTE(const struct nx_action_note
*nan
, struct ofpbuf
*out
)
4145 struct ofpact_note
*note
;
4146 unsigned int length
;
4148 length
= ntohs(nan
->len
) - offsetof(struct nx_action_note
, note
);
4149 note
= ofpact_put(out
, OFPACT_NOTE
,
4150 offsetof(struct ofpact_note
, data
) + length
);
4151 note
->length
= length
;
4152 memcpy(note
->data
, nan
->note
, length
);
4158 encode_NOTE(const struct ofpact_note
*note
,
4159 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4161 size_t start_ofs
= out
->size
;
4162 struct nx_action_note
*nan
;
4163 unsigned int remainder
;
4166 put_NXAST_NOTE(out
);
4167 out
->size
= out
->size
- sizeof nan
->note
;
4169 ofpbuf_put(out
, note
->data
, note
->length
);
4171 len
= out
->size
- start_ofs
;
4172 remainder
= len
% OFP_ACTION_ALIGN
;
4174 ofpbuf_put_zeros(out
, OFP_ACTION_ALIGN
- remainder
);
4176 nan
= ofpbuf_at(out
, start_ofs
, sizeof *nan
);
4177 nan
->len
= htons(out
->size
- start_ofs
);
4180 static char * OVS_WARN_UNUSED_RESULT
4181 parse_NOTE(const char *arg
, struct ofpbuf
*ofpacts
,
4182 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4184 struct ofpact_note
*note
;
4186 note
= ofpact_put_NOTE(ofpacts
);
4187 while (*arg
!= '\0') {
4198 byte
= hexits_value(arg
, 2, &ok
);
4200 return xstrdup("bad hex digit in `note' argument");
4202 ofpbuf_put(ofpacts
, &byte
, 1);
4204 note
= ofpacts
->header
;
4209 ofpact_update_len(ofpacts
, ¬e
->ofpact
);
4214 format_NOTE(const struct ofpact_note
*a
, struct ds
*s
)
4218 ds_put_cstr(s
, "note:");
4219 for (i
= 0; i
< a
->length
; i
++) {
4221 ds_put_char(s
, '.');
4223 ds_put_format(s
, "%02"PRIx8
, a
->data
[i
]);
4230 decode_NXAST_RAW_EXIT(struct ofpbuf
*out
)
4232 ofpact_put_EXIT(out
);
4237 encode_EXIT(const struct ofpact_null
*null OVS_UNUSED
,
4238 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4240 put_NXAST_EXIT(out
);
4243 static char * OVS_WARN_UNUSED_RESULT
4244 parse_EXIT(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
4245 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4247 ofpact_put_EXIT(ofpacts
);
4252 format_EXIT(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
4254 ds_put_cstr(s
, "exit");
4257 /* Unroll xlate action. */
4260 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate
*unroll OVS_UNUSED
,
4261 enum ofp_version ofp_version OVS_UNUSED
,
4262 struct ofpbuf
*out OVS_UNUSED
)
4267 static char * OVS_WARN_UNUSED_RESULT
4268 parse_UNROLL_XLATE(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts OVS_UNUSED
,
4269 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4276 format_UNROLL_XLATE(const struct ofpact_unroll_xlate
*a OVS_UNUSED
,
4279 ds_put_cstr(s
, "unroll_xlate");
4282 /* Action structure for NXAST_SAMPLE.
4284 * Samples matching packets with the given probability and sends them
4285 * each to the set of collectors identified with the given ID. The
4286 * probability is expressed as a number of packets to be sampled out
4287 * of USHRT_MAX packets, and must be >0.
4289 * When sending packet samples to IPFIX collectors, the IPFIX flow
4290 * record sent for each sampled packet is associated with the given
4291 * observation domain ID and observation point ID. Each IPFIX flow
4292 * record contain the sampled packet's headers when executing this
4293 * rule. If a sampled packet's headers are modified by previous
4294 * actions in the flow, those modified headers are sent. */
4295 struct nx_action_sample
{
4296 ovs_be16 type
; /* OFPAT_VENDOR. */
4297 ovs_be16 len
; /* Length is 24. */
4298 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4299 ovs_be16 subtype
; /* NXAST_SAMPLE. */
4300 ovs_be16 probability
; /* Fraction of packets to sample. */
4301 ovs_be32 collector_set_id
; /* ID of collector set in OVSDB. */
4302 ovs_be32 obs_domain_id
; /* ID of sampling observation domain. */
4303 ovs_be32 obs_point_id
; /* ID of sampling observation point. */
4305 OFP_ASSERT(sizeof(struct nx_action_sample
) == 24);
4308 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample
*nas
, struct ofpbuf
*out
)
4310 struct ofpact_sample
*sample
;
4312 sample
= ofpact_put_SAMPLE(out
);
4313 sample
->probability
= ntohs(nas
->probability
);
4314 sample
->collector_set_id
= ntohl(nas
->collector_set_id
);
4315 sample
->obs_domain_id
= ntohl(nas
->obs_domain_id
);
4316 sample
->obs_point_id
= ntohl(nas
->obs_point_id
);
4318 if (sample
->probability
== 0) {
4319 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4326 encode_SAMPLE(const struct ofpact_sample
*sample
,
4327 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4329 struct nx_action_sample
*nas
;
4331 nas
= put_NXAST_SAMPLE(out
);
4332 nas
->probability
= htons(sample
->probability
);
4333 nas
->collector_set_id
= htonl(sample
->collector_set_id
);
4334 nas
->obs_domain_id
= htonl(sample
->obs_domain_id
);
4335 nas
->obs_point_id
= htonl(sample
->obs_point_id
);
4338 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4339 * action to 'ofpacts'.
4341 * Returns NULL if successful, otherwise a malloc()'d string describing the
4342 * error. The caller is responsible for freeing the returned string. */
4343 static char * OVS_WARN_UNUSED_RESULT
4344 parse_SAMPLE(char *arg
, struct ofpbuf
*ofpacts
,
4345 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4347 struct ofpact_sample
*os
= ofpact_put_SAMPLE(ofpacts
);
4350 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
4353 if (!strcmp(key
, "probability")) {
4354 error
= str_to_u16(value
, "probability", &os
->probability
);
4355 if (!error
&& os
->probability
== 0) {
4356 error
= xasprintf("invalid probability value \"%s\"", value
);
4358 } else if (!strcmp(key
, "collector_set_id")) {
4359 error
= str_to_u32(value
, &os
->collector_set_id
);
4360 } else if (!strcmp(key
, "obs_domain_id")) {
4361 error
= str_to_u32(value
, &os
->obs_domain_id
);
4362 } else if (!strcmp(key
, "obs_point_id")) {
4363 error
= str_to_u32(value
, &os
->obs_point_id
);
4365 error
= xasprintf("invalid key \"%s\" in \"sample\" argument",
4372 if (os
->probability
== 0) {
4373 return xstrdup("non-zero \"probability\" must be specified on sample");
4379 format_SAMPLE(const struct ofpact_sample
*a
, struct ds
*s
)
4381 ds_put_format(s
, "sample(probability=%"PRIu16
",collector_set_id=%"PRIu32
4382 ",obs_domain_id=%"PRIu32
",obs_point_id=%"PRIu32
")",
4383 a
->probability
, a
->collector_set_id
,
4384 a
->obs_domain_id
, a
->obs_point_id
);
4387 /* Meter instruction. */
4390 encode_METER(const struct ofpact_meter
*meter
,
4391 enum ofp_version ofp_version
, struct ofpbuf
*out
)
4393 if (ofp_version
>= OFP13_VERSION
) {
4394 instruction_put_OFPIT13_METER(out
)->meter_id
= htonl(meter
->meter_id
);
4398 static char * OVS_WARN_UNUSED_RESULT
4399 parse_METER(char *arg
, struct ofpbuf
*ofpacts
,
4400 enum ofputil_protocol
*usable_protocols
)
4402 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
4403 return str_to_u32(arg
, &ofpact_put_METER(ofpacts
)->meter_id
);
4407 format_METER(const struct ofpact_meter
*a
, struct ds
*s
)
4409 ds_put_format(s
, "meter:%"PRIu32
, a
->meter_id
);
4412 /* Clear-Actions instruction. */
4415 encode_CLEAR_ACTIONS(const struct ofpact_null
*null OVS_UNUSED
,
4416 enum ofp_version ofp_version OVS_UNUSED
,
4417 struct ofpbuf
*out OVS_UNUSED
)
4419 if (ofp_version
> OFP10_VERSION
) {
4420 instruction_put_OFPIT11_CLEAR_ACTIONS(out
);
4424 static char * OVS_WARN_UNUSED_RESULT
4425 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
4426 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4428 ofpact_put_CLEAR_ACTIONS(ofpacts
);
4433 format_CLEAR_ACTIONS(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
4435 ds_put_cstr(s
, "clear_actions");
4438 /* Write-Actions instruction. */
4441 encode_WRITE_ACTIONS(const struct ofpact_nest
*actions
,
4442 enum ofp_version ofp_version
, struct ofpbuf
*out
)
4444 if (ofp_version
> OFP10_VERSION
) {
4445 const size_t ofs
= out
->size
;
4447 instruction_put_OFPIT11_WRITE_ACTIONS(out
);
4448 ofpacts_put_openflow_actions(actions
->actions
,
4449 ofpact_nest_get_action_len(actions
),
4451 ofpacts_update_instruction_actions(out
, ofs
);
4455 static char * OVS_WARN_UNUSED_RESULT
4456 parse_WRITE_ACTIONS(char *arg
, struct ofpbuf
*ofpacts
,
4457 enum ofputil_protocol
*usable_protocols
)
4459 struct ofpact_nest
*on
;
4463 /* Pull off existing actions or instructions. */
4464 ofpact_pad(ofpacts
);
4465 ofs
= ofpacts
->size
;
4466 ofpbuf_pull(ofpacts
, ofs
);
4468 /* Add a Write-Actions instruction and then pull it off. */
4469 ofpact_put(ofpacts
, OFPACT_WRITE_ACTIONS
, sizeof *on
);
4470 ofpbuf_pull(ofpacts
, sizeof *on
);
4472 /* Parse nested actions.
4474 * We pulled off "write-actions" and the previous actions because the
4475 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4476 * that it doesn't actually include the nested actions. That means that
4477 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4478 * follows a Write-Actions, which is an invalid order. */
4479 error
= ofpacts_parse(arg
, ofpacts
, usable_protocols
, false);
4481 /* Put the Write-Actions back on and update its length. */
4482 on
= ofpbuf_push_uninit(ofpacts
, sizeof *on
);
4483 on
->ofpact
.len
= ofpacts
->size
;
4485 /* Put any previous actions or instructions back on. */
4486 ofpbuf_push_uninit(ofpacts
, ofs
);
4492 format_WRITE_ACTIONS(const struct ofpact_nest
*a
, struct ds
*s
)
4494 ds_put_cstr(s
, "write_actions(");
4495 ofpacts_format(a
->actions
, ofpact_nest_get_action_len(a
), s
);
4496 ds_put_char(s
, ')');
4499 /* Action structure for NXAST_WRITE_METADATA.
4501 * Modifies the 'mask' bits of the metadata value. */
4502 struct nx_action_write_metadata
{
4503 ovs_be16 type
; /* OFPAT_VENDOR. */
4504 ovs_be16 len
; /* Length is 32. */
4505 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4506 ovs_be16 subtype
; /* NXAST_WRITE_METADATA. */
4507 uint8_t zeros
[6]; /* Must be zero. */
4508 ovs_be64 metadata
; /* Metadata register. */
4509 ovs_be64 mask
; /* Metadata mask. */
4511 OFP_ASSERT(sizeof(struct nx_action_write_metadata
) == 32);
4514 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata
*nawm
,
4517 struct ofpact_metadata
*om
;
4519 if (!is_all_zeros(nawm
->zeros
, sizeof nawm
->zeros
)) {
4520 return OFPERR_NXBRC_MUST_BE_ZERO
;
4523 om
= ofpact_put_WRITE_METADATA(out
);
4524 om
->metadata
= nawm
->metadata
;
4525 om
->mask
= nawm
->mask
;
4531 encode_WRITE_METADATA(const struct ofpact_metadata
*metadata
,
4532 enum ofp_version ofp_version
, struct ofpbuf
*out
)
4534 if (ofp_version
== OFP10_VERSION
) {
4535 struct nx_action_write_metadata
*nawm
;
4537 nawm
= put_NXAST_WRITE_METADATA(out
);
4538 nawm
->metadata
= metadata
->metadata
;
4539 nawm
->mask
= metadata
->mask
;
4541 struct ofp11_instruction_write_metadata
*oiwm
;
4543 oiwm
= instruction_put_OFPIT11_WRITE_METADATA(out
);
4544 oiwm
->metadata
= metadata
->metadata
;
4545 oiwm
->metadata_mask
= metadata
->mask
;
4549 static char * OVS_WARN_UNUSED_RESULT
4550 parse_WRITE_METADATA(char *arg
, struct ofpbuf
*ofpacts
,
4551 enum ofputil_protocol
*usable_protocols
)
4553 struct ofpact_metadata
*om
;
4554 char *mask
= strchr(arg
, '/');
4556 *usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
4558 om
= ofpact_put_WRITE_METADATA(ofpacts
);
4563 error
= str_to_be64(mask
+ 1, &om
->mask
);
4568 om
->mask
= OVS_BE64_MAX
;
4571 return str_to_be64(arg
, &om
->metadata
);
4575 format_WRITE_METADATA(const struct ofpact_metadata
*a
, struct ds
*s
)
4577 ds_put_format(s
, "write_metadata:%#"PRIx64
, ntohll(a
->metadata
));
4578 if (a
->mask
!= OVS_BE64_MAX
) {
4579 ds_put_format(s
, "/%#"PRIx64
, ntohll(a
->mask
));
4583 /* Goto-Table instruction. */
4586 encode_GOTO_TABLE(const struct ofpact_goto_table
*goto_table
,
4587 enum ofp_version ofp_version
, struct ofpbuf
*out
)
4589 if (ofp_version
== OFP10_VERSION
) {
4590 struct nx_action_resubmit
*nar
;
4592 nar
= put_NXAST_RESUBMIT_TABLE(out
);
4593 nar
->table
= goto_table
->table_id
;
4594 nar
->in_port
= htons(ofp_to_u16(OFPP_IN_PORT
));
4596 struct ofp11_instruction_goto_table
*oigt
;
4598 oigt
= instruction_put_OFPIT11_GOTO_TABLE(out
);
4599 oigt
->table_id
= goto_table
->table_id
;
4600 memset(oigt
->pad
, 0, sizeof oigt
->pad
);
4604 static char * OVS_WARN_UNUSED_RESULT
4605 parse_GOTO_TABLE(char *arg
, struct ofpbuf
*ofpacts
,
4606 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4608 struct ofpact_goto_table
*ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
4609 char *table_s
= strsep(&arg
, ",");
4610 if (!table_s
|| !table_s
[0]) {
4611 return xstrdup("instruction goto-table needs table id");
4613 return str_to_u8(table_s
, "table", &ogt
->table_id
);
4617 format_GOTO_TABLE(const struct ofpact_goto_table
*a
, struct ds
*s
)
4619 ds_put_format(s
, "goto_table:%"PRIu8
, a
->table_id
);
4623 log_bad_action(const struct ofp_action_header
*actions
, size_t actions_len
,
4624 const struct ofp_action_header
*bad_action
, enum ofperr error
)
4626 if (!VLOG_DROP_WARN(&rl
)) {
4630 ds_put_hex_dump(&s
, actions
, actions_len
, 0, false);
4631 VLOG_WARN("bad action at offset %#"PRIxPTR
" (%s):\n%s",
4632 (char *)bad_action
- (char *)actions
,
4633 ofperr_get_name(error
), ds_cstr(&s
));
4639 ofpacts_decode(const void *actions
, size_t actions_len
,
4640 enum ofp_version ofp_version
, struct ofpbuf
*ofpacts
)
4642 struct ofpbuf openflow
;
4644 ofpbuf_use_const(&openflow
, actions
, actions_len
);
4645 while (openflow
.size
) {
4646 const struct ofp_action_header
*action
= openflow
.data
;
4647 enum ofp_raw_action_type raw
;
4651 error
= ofpact_pull_raw(&openflow
, ofp_version
, &raw
, &arg
);
4653 error
= ofpact_decode(action
, raw
, arg
, ofpacts
);
4657 log_bad_action(actions
, actions_len
, action
, error
);
4662 ofpact_pad(ofpacts
);
4667 ofpacts_pull_openflow_actions__(struct ofpbuf
*openflow
,
4668 unsigned int actions_len
,
4669 enum ofp_version version
,
4670 uint32_t allowed_ovsinsts
,
4671 struct ofpbuf
*ofpacts
)
4673 const struct ofp_action_header
*actions
;
4676 ofpbuf_clear(ofpacts
);
4678 if (actions_len
% OFP_ACTION_ALIGN
!= 0) {
4679 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u is not a "
4680 "multiple of %d", actions_len
, OFP_ACTION_ALIGN
);
4681 return OFPERR_OFPBRC_BAD_LEN
;
4684 actions
= ofpbuf_try_pull(openflow
, actions_len
);
4685 if (actions
== NULL
) {
4686 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u exceeds "
4687 "remaining message length (%"PRIu32
")",
4688 actions_len
, openflow
->size
);
4689 return OFPERR_OFPBRC_BAD_LEN
;
4692 error
= ofpacts_decode(actions
, actions_len
, version
, ofpacts
);
4694 ofpbuf_clear(ofpacts
);
4698 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
,
4701 ofpbuf_clear(ofpacts
);
4706 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
4707 * front of 'openflow' into ofpacts. On success, replaces any existing content
4708 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
4709 * Returns 0 if successful, otherwise an OpenFlow error.
4711 * Actions are processed according to their OpenFlow version which
4712 * is provided in the 'version' parameter.
4714 * In most places in OpenFlow, actions appear encapsulated in instructions, so
4715 * you should call ofpacts_pull_openflow_instructions() instead of this
4718 * The parsed actions are valid generically, but they may not be valid in a
4719 * specific context. For example, port numbers up to OFPP_MAX are valid
4720 * generically, but specific datapaths may only support port numbers in a
4721 * smaller range. Use ofpacts_check() to additional check whether actions are
4722 * valid in a specific context. */
4724 ofpacts_pull_openflow_actions(struct ofpbuf
*openflow
,
4725 unsigned int actions_len
,
4726 enum ofp_version version
,
4727 struct ofpbuf
*ofpacts
)
4729 return ofpacts_pull_openflow_actions__(openflow
, actions_len
, version
,
4730 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
4734 /* OpenFlow 1.1 actions. */
4737 /* True if an action sets the value of a field
4738 * in a way that is compatibile with the action set.
4739 * The field can be set via either a set or a move action.
4740 * False otherwise. */
4742 ofpact_is_set_or_move_action(const struct ofpact
*a
)
4745 case OFPACT_SET_FIELD
:
4746 case OFPACT_REG_MOVE
:
4747 case OFPACT_SET_ETH_DST
:
4748 case OFPACT_SET_ETH_SRC
:
4749 case OFPACT_SET_IP_DSCP
:
4750 case OFPACT_SET_IP_ECN
:
4751 case OFPACT_SET_IP_TTL
:
4752 case OFPACT_SET_IPV4_DST
:
4753 case OFPACT_SET_IPV4_SRC
:
4754 case OFPACT_SET_L4_DST_PORT
:
4755 case OFPACT_SET_L4_SRC_PORT
:
4756 case OFPACT_SET_MPLS_LABEL
:
4757 case OFPACT_SET_MPLS_TC
:
4758 case OFPACT_SET_MPLS_TTL
:
4759 case OFPACT_SET_QUEUE
:
4760 case OFPACT_SET_TUNNEL
:
4761 case OFPACT_SET_VLAN_PCP
:
4762 case OFPACT_SET_VLAN_VID
:
4765 case OFPACT_CLEAR_ACTIONS
:
4766 case OFPACT_CONTROLLER
:
4767 case OFPACT_DEC_MPLS_TTL
:
4768 case OFPACT_DEC_TTL
:
4769 case OFPACT_ENQUEUE
:
4771 case OFPACT_UNROLL_XLATE
:
4772 case OFPACT_FIN_TIMEOUT
:
4773 case OFPACT_GOTO_TABLE
:
4776 case OFPACT_CONJUNCTION
:
4778 case OFPACT_MULTIPATH
:
4781 case OFPACT_OUTPUT_REG
:
4782 case OFPACT_POP_MPLS
:
4783 case OFPACT_POP_QUEUE
:
4784 case OFPACT_PUSH_MPLS
:
4785 case OFPACT_PUSH_VLAN
:
4786 case OFPACT_RESUBMIT
:
4788 case OFPACT_STACK_POP
:
4789 case OFPACT_STACK_PUSH
:
4790 case OFPACT_STRIP_VLAN
:
4791 case OFPACT_WRITE_ACTIONS
:
4792 case OFPACT_WRITE_METADATA
:
4799 /* True if an action is allowed in the action set.
4800 * False otherwise. */
4802 ofpact_is_allowed_in_actions_set(const struct ofpact
*a
)
4805 case OFPACT_DEC_MPLS_TTL
:
4806 case OFPACT_DEC_TTL
:
4809 case OFPACT_POP_MPLS
:
4810 case OFPACT_PUSH_MPLS
:
4811 case OFPACT_PUSH_VLAN
:
4812 case OFPACT_REG_MOVE
:
4813 case OFPACT_SET_FIELD
:
4814 case OFPACT_SET_ETH_DST
:
4815 case OFPACT_SET_ETH_SRC
:
4816 case OFPACT_SET_IP_DSCP
:
4817 case OFPACT_SET_IP_ECN
:
4818 case OFPACT_SET_IP_TTL
:
4819 case OFPACT_SET_IPV4_DST
:
4820 case OFPACT_SET_IPV4_SRC
:
4821 case OFPACT_SET_L4_DST_PORT
:
4822 case OFPACT_SET_L4_SRC_PORT
:
4823 case OFPACT_SET_MPLS_LABEL
:
4824 case OFPACT_SET_MPLS_TC
:
4825 case OFPACT_SET_MPLS_TTL
:
4826 case OFPACT_SET_QUEUE
:
4827 case OFPACT_SET_TUNNEL
:
4828 case OFPACT_SET_VLAN_PCP
:
4829 case OFPACT_SET_VLAN_VID
:
4830 case OFPACT_STRIP_VLAN
:
4833 /* In general these actions are excluded because they are not part of
4834 * the OpenFlow specification nor map to actions that are defined in
4835 * the specification. Thus the order in which they should be applied
4836 * in the action set is undefined. */
4838 case OFPACT_CONTROLLER
:
4839 case OFPACT_ENQUEUE
:
4841 case OFPACT_UNROLL_XLATE
:
4842 case OFPACT_FIN_TIMEOUT
:
4844 case OFPACT_CONJUNCTION
:
4845 case OFPACT_MULTIPATH
:
4847 case OFPACT_OUTPUT_REG
:
4848 case OFPACT_POP_QUEUE
:
4849 case OFPACT_RESUBMIT
:
4851 case OFPACT_STACK_POP
:
4852 case OFPACT_STACK_PUSH
:
4854 /* The action set may only include actions and thus
4855 * may not include any instructions */
4856 case OFPACT_CLEAR_ACTIONS
:
4857 case OFPACT_GOTO_TABLE
:
4859 case OFPACT_WRITE_ACTIONS
:
4860 case OFPACT_WRITE_METADATA
:
4867 /* Append ofpact 'a' onto the tail of 'out' */
4869 ofpact_copy(struct ofpbuf
*out
, const struct ofpact
*a
)
4871 ofpbuf_put(out
, a
, OFPACT_ALIGN(a
->len
));
4874 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
4876 ofpacts_copy_last(struct ofpbuf
*out
, const struct ofpbuf
*in
,
4877 enum ofpact_type filter
)
4879 const struct ofpact
*target
;
4880 const struct ofpact
*a
;
4883 OFPACT_FOR_EACH (a
, in
->data
, in
->size
) {
4884 if (a
->type
== filter
) {
4889 ofpact_copy(out
, target
);
4891 return target
!= NULL
;
4894 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
4895 * The order of appended ofpacts is preserved between 'in' and 'out' */
4897 ofpacts_copy_all(struct ofpbuf
*out
, const struct ofpbuf
*in
,
4898 bool (*filter
)(const struct ofpact
*))
4900 const struct ofpact
*a
;
4902 OFPACT_FOR_EACH (a
, in
->data
, in
->size
) {
4904 ofpact_copy(out
, a
);
4909 /* Reads 'action_set', which contains ofpacts accumulated by
4910 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
4911 * executed directly into 'action_list'. (These names correspond to the
4912 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
4914 * In general this involves appending the last instance of each action that is
4915 * admissible in the action set in the order described in the OpenFlow
4919 * + output action is only appended if no group action was present in 'in'.
4920 * + As a simplification all set actions are copied in the order the are
4921 * provided in 'in' as many set actions applied to a field has the same
4922 * affect as only applying the last action that sets a field and
4923 * duplicates are removed by do_xlate_actions().
4924 * This has an unwanted side-effect of compsoting multiple
4925 * LOAD_REG actions that touch different regions of the same field. */
4927 ofpacts_execute_action_set(struct ofpbuf
*action_list
,
4928 const struct ofpbuf
*action_set
)
4930 /* The OpenFlow spec "Action Set" section specifies this order. */
4931 ofpacts_copy_last(action_list
, action_set
, OFPACT_STRIP_VLAN
);
4932 ofpacts_copy_last(action_list
, action_set
, OFPACT_POP_MPLS
);
4933 ofpacts_copy_last(action_list
, action_set
, OFPACT_PUSH_MPLS
);
4934 ofpacts_copy_last(action_list
, action_set
, OFPACT_PUSH_VLAN
);
4935 ofpacts_copy_last(action_list
, action_set
, OFPACT_DEC_TTL
);
4936 ofpacts_copy_last(action_list
, action_set
, OFPACT_DEC_MPLS_TTL
);
4937 ofpacts_copy_all(action_list
, action_set
, ofpact_is_set_or_move_action
);
4938 ofpacts_copy_last(action_list
, action_set
, OFPACT_SET_QUEUE
);
4940 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
4941 * we should execute only OFPACT_GROUP.
4943 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
4944 * all the actions because there's no point in modifying a packet that will
4945 * not be sent anywhere. */
4946 if (!ofpacts_copy_last(action_list
, action_set
, OFPACT_GROUP
) &&
4947 !ofpacts_copy_last(action_list
, action_set
, OFPACT_OUTPUT
) &&
4948 !ofpacts_copy_last(action_list
, action_set
, OFPACT_RESUBMIT
)) {
4949 ofpbuf_clear(action_list
);
4955 ofpacts_decode_for_action_set(const struct ofp_action_header
*in
,
4956 size_t n_in
, enum ofp_version version
,
4961 size_t start
= out
->size
;
4963 error
= ofpacts_decode(in
, n_in
, version
, out
);
4969 OFPACT_FOR_EACH (a
, ofpact_end(out
->data
, start
), out
->size
- start
) {
4970 if (!ofpact_is_allowed_in_actions_set(a
)) {
4971 VLOG_WARN_RL(&rl
, "disallowed action in action set");
4972 return OFPERR_OFPBAC_BAD_TYPE
;
4979 /* OpenFlow 1.1 instructions. */
4981 struct instruction_type_info
{
4982 enum ovs_instruction_type type
;
4986 static const struct instruction_type_info inst_info
[] = {
4987 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
4993 ovs_instruction_name_from_type(enum ovs_instruction_type type
)
4995 return inst_info
[type
].name
;
4999 ovs_instruction_type_from_name(const char *name
)
5001 const struct instruction_type_info
*p
;
5002 for (p
= inst_info
; p
< &inst_info
[ARRAY_SIZE(inst_info
)]; p
++) {
5003 if (!strcasecmp(name
, p
->name
)) {
5010 enum ovs_instruction_type
5011 ovs_instruction_type_from_ofpact_type(enum ofpact_type type
)
5015 return OVSINST_OFPIT13_METER
;
5016 case OFPACT_CLEAR_ACTIONS
:
5017 return OVSINST_OFPIT11_CLEAR_ACTIONS
;
5018 case OFPACT_WRITE_ACTIONS
:
5019 return OVSINST_OFPIT11_WRITE_ACTIONS
;
5020 case OFPACT_WRITE_METADATA
:
5021 return OVSINST_OFPIT11_WRITE_METADATA
;
5022 case OFPACT_GOTO_TABLE
:
5023 return OVSINST_OFPIT11_GOTO_TABLE
;
5026 case OFPACT_CONTROLLER
:
5027 case OFPACT_ENQUEUE
:
5028 case OFPACT_OUTPUT_REG
:
5030 case OFPACT_SET_VLAN_VID
:
5031 case OFPACT_SET_VLAN_PCP
:
5032 case OFPACT_STRIP_VLAN
:
5033 case OFPACT_PUSH_VLAN
:
5034 case OFPACT_SET_ETH_SRC
:
5035 case OFPACT_SET_ETH_DST
:
5036 case OFPACT_SET_IPV4_SRC
:
5037 case OFPACT_SET_IPV4_DST
:
5038 case OFPACT_SET_IP_DSCP
:
5039 case OFPACT_SET_IP_ECN
:
5040 case OFPACT_SET_IP_TTL
:
5041 case OFPACT_SET_L4_SRC_PORT
:
5042 case OFPACT_SET_L4_DST_PORT
:
5043 case OFPACT_REG_MOVE
:
5044 case OFPACT_SET_FIELD
:
5045 case OFPACT_STACK_PUSH
:
5046 case OFPACT_STACK_POP
:
5047 case OFPACT_DEC_TTL
:
5048 case OFPACT_SET_MPLS_LABEL
:
5049 case OFPACT_SET_MPLS_TC
:
5050 case OFPACT_SET_MPLS_TTL
:
5051 case OFPACT_DEC_MPLS_TTL
:
5052 case OFPACT_PUSH_MPLS
:
5053 case OFPACT_POP_MPLS
:
5054 case OFPACT_SET_TUNNEL
:
5055 case OFPACT_SET_QUEUE
:
5056 case OFPACT_POP_QUEUE
:
5057 case OFPACT_FIN_TIMEOUT
:
5058 case OFPACT_RESUBMIT
:
5060 case OFPACT_CONJUNCTION
:
5061 case OFPACT_MULTIPATH
:
5064 case OFPACT_UNROLL_XLATE
:
5067 return OVSINST_OFPIT11_APPLY_ACTIONS
;
5072 ovs_instruction_type_from_inst_type(enum ovs_instruction_type
*instruction_type
,
5073 const uint16_t inst_type
)
5075 switch (inst_type
) {
5077 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5079 *instruction_type = OVSINST_##ENUM; \
5085 return OFPERR_OFPBIC_UNKNOWN_INST
;
5089 /* Two-way translation between OVS's internal "OVSINST_*" representation of
5090 * instructions and the "OFPIT_*" representation used in OpenFlow. */
5091 struct ovsinst_map
{
5092 enum ovs_instruction_type ovsinst
; /* Internal name for instruction. */
5093 int ofpit
; /* OFPIT_* number from OpenFlow spec. */
5096 static const struct ovsinst_map
*
5097 get_ovsinst_map(enum ofp_version version
)
5099 /* OpenFlow 1.1 and 1.2 instructions. */
5100 static const struct ovsinst_map of11
[] = {
5101 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
5102 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
5103 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
5104 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
5105 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
5109 /* OpenFlow 1.3+ instructions. */
5110 static const struct ovsinst_map of13
[] = {
5111 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
5112 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
5113 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
5114 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
5115 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
5116 { OVSINST_OFPIT13_METER
, 6 },
5120 return version
< OFP13_VERSION
? of11
: of13
;
5123 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
5124 * values, into a bitmap of instructions suitable for OpenFlow 'version'
5125 * (OFP11_VERSION or later), and returns the result. */
5127 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap
, enum ofp_version version
)
5129 uint32_t ofpit_bitmap
= 0;
5130 const struct ovsinst_map
*x
;
5132 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
5133 if (ovsinst_bitmap
& (1u << x
->ovsinst
)) {
5134 ofpit_bitmap
|= 1u << x
->ofpit
;
5137 return htonl(ofpit_bitmap
);
5140 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5141 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5142 * correspond to OVSINST_* values, and returns the result. */
5144 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap
, enum ofp_version version
)
5146 uint32_t ovsinst_bitmap
= 0;
5147 const struct ovsinst_map
*x
;
5149 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
5150 if (ofpit_bitmap
& htonl(1u << x
->ofpit
)) {
5151 ovsinst_bitmap
|= 1u << x
->ovsinst
;
5154 return ovsinst_bitmap
;
5157 static inline struct ofp11_instruction
*
5158 instruction_next(const struct ofp11_instruction
*inst
)
5160 return ((struct ofp11_instruction
*) (void *)
5161 ((uint8_t *) inst
+ ntohs(inst
->len
)));
5165 instruction_is_valid(const struct ofp11_instruction
*inst
,
5166 size_t n_instructions
)
5168 uint16_t len
= ntohs(inst
->len
);
5169 return (!(len
% OFP11_INSTRUCTION_ALIGN
)
5170 && len
>= sizeof *inst
5171 && len
/ sizeof *inst
<= n_instructions
);
5174 /* This macro is careful to check for instructions with bad lengths. */
5175 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
5176 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
5177 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
5178 ((LEFT) -= (ntohs((ITER)->len) \
5179 / sizeof(struct ofp11_instruction)), \
5180 (ITER) = instruction_next(ITER)))
5183 decode_openflow11_instruction(const struct ofp11_instruction
*inst
,
5184 enum ovs_instruction_type
*type
)
5186 uint16_t len
= ntohs(inst
->len
);
5188 switch (inst
->type
) {
5189 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER
):
5190 return OFPERR_OFPBIC_BAD_EXPERIMENTER
;
5192 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5193 case CONSTANT_HTONS(ENUM): \
5195 ? len >= sizeof(struct STRUCT) \
5196 : len == sizeof(struct STRUCT)) { \
5197 *type = OVSINST_##ENUM; \
5200 return OFPERR_OFPBIC_BAD_LEN; \
5206 return OFPERR_OFPBIC_UNKNOWN_INST
;
5211 decode_openflow11_instructions(const struct ofp11_instruction insts
[],
5213 const struct ofp11_instruction
*out
[])
5215 const struct ofp11_instruction
*inst
;
5218 memset(out
, 0, N_OVS_INSTRUCTIONS
* sizeof *out
);
5219 INSTRUCTION_FOR_EACH (inst
, left
, insts
, n_insts
) {
5220 enum ovs_instruction_type type
;
5223 error
= decode_openflow11_instruction(inst
, &type
);
5229 return OFPERR_OFPBIC_DUP_INST
;
5235 VLOG_WARN_RL(&rl
, "bad instruction format at offset %"PRIuSIZE
,
5236 (n_insts
- left
) * sizeof *inst
);
5237 return OFPERR_OFPBIC_BAD_LEN
;
5243 get_actions_from_instruction(const struct ofp11_instruction
*inst
,
5244 const struct ofp_action_header
**actions
,
5245 size_t *actions_len
)
5247 *actions
= ALIGNED_CAST(const struct ofp_action_header
*, inst
+ 1);
5248 *actions_len
= ntohs(inst
->len
) - sizeof *inst
;
5252 ofpacts_pull_openflow_instructions(struct ofpbuf
*openflow
,
5253 unsigned int instructions_len
,
5254 enum ofp_version version
,
5255 struct ofpbuf
*ofpacts
)
5257 const struct ofp11_instruction
*instructions
;
5258 const struct ofp11_instruction
*insts
[N_OVS_INSTRUCTIONS
];
5261 if (version
== OFP10_VERSION
) {
5262 return ofpacts_pull_openflow_actions__(openflow
, instructions_len
,
5264 (1u << N_OVS_INSTRUCTIONS
) - 1,
5268 ofpbuf_clear(ofpacts
);
5270 if (instructions_len
% OFP11_INSTRUCTION_ALIGN
!= 0) {
5271 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u is not a "
5273 instructions_len
, OFP11_INSTRUCTION_ALIGN
);
5274 error
= OFPERR_OFPBIC_BAD_LEN
;
5278 instructions
= ofpbuf_try_pull(openflow
, instructions_len
);
5279 if (instructions
== NULL
) {
5280 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u exceeds "
5281 "remaining message length (%"PRIu32
")",
5282 instructions_len
, openflow
->size
);
5283 error
= OFPERR_OFPBIC_BAD_LEN
;
5287 error
= decode_openflow11_instructions(
5288 instructions
, instructions_len
/ OFP11_INSTRUCTION_ALIGN
,
5294 if (insts
[OVSINST_OFPIT13_METER
]) {
5295 const struct ofp13_instruction_meter
*oim
;
5296 struct ofpact_meter
*om
;
5298 oim
= ALIGNED_CAST(const struct ofp13_instruction_meter
*,
5299 insts
[OVSINST_OFPIT13_METER
]);
5301 om
= ofpact_put_METER(ofpacts
);
5302 om
->meter_id
= ntohl(oim
->meter_id
);
5304 if (insts
[OVSINST_OFPIT11_APPLY_ACTIONS
]) {
5305 const struct ofp_action_header
*actions
;
5308 get_actions_from_instruction(insts
[OVSINST_OFPIT11_APPLY_ACTIONS
],
5309 &actions
, &actions_len
);
5310 error
= ofpacts_decode(actions
, actions_len
, version
, ofpacts
);
5315 if (insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]) {
5316 instruction_get_OFPIT11_CLEAR_ACTIONS(
5317 insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]);
5318 ofpact_put_CLEAR_ACTIONS(ofpacts
);
5320 if (insts
[OVSINST_OFPIT11_WRITE_ACTIONS
]) {
5321 struct ofpact_nest
*on
;
5322 const struct ofp_action_header
*actions
;
5326 ofpact_pad(ofpacts
);
5327 start
= ofpacts
->size
;
5328 ofpact_put(ofpacts
, OFPACT_WRITE_ACTIONS
,
5329 offsetof(struct ofpact_nest
, actions
));
5330 get_actions_from_instruction(insts
[OVSINST_OFPIT11_WRITE_ACTIONS
],
5331 &actions
, &actions_len
);
5332 error
= ofpacts_decode_for_action_set(actions
, actions_len
,
5337 on
= ofpbuf_at_assert(ofpacts
, start
, sizeof *on
);
5338 on
->ofpact
.len
= ofpacts
->size
- start
;
5340 if (insts
[OVSINST_OFPIT11_WRITE_METADATA
]) {
5341 const struct ofp11_instruction_write_metadata
*oiwm
;
5342 struct ofpact_metadata
*om
;
5344 oiwm
= ALIGNED_CAST(const struct ofp11_instruction_write_metadata
*,
5345 insts
[OVSINST_OFPIT11_WRITE_METADATA
]);
5347 om
= ofpact_put_WRITE_METADATA(ofpacts
);
5348 om
->metadata
= oiwm
->metadata
;
5349 om
->mask
= oiwm
->metadata_mask
;
5351 if (insts
[OVSINST_OFPIT11_GOTO_TABLE
]) {
5352 const struct ofp11_instruction_goto_table
*oigt
;
5353 struct ofpact_goto_table
*ogt
;
5355 oigt
= instruction_get_OFPIT11_GOTO_TABLE(
5356 insts
[OVSINST_OFPIT11_GOTO_TABLE
]);
5357 ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
5358 ogt
->table_id
= oigt
->table_id
;
5361 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
,
5362 (1u << N_OVS_INSTRUCTIONS
) - 1);
5365 ofpbuf_clear(ofpacts
);
5370 /* Update the length of the instruction that begins at offset 'ofs' within
5371 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5372 * If the instruction contains no nested actions, deletes it entirely. */
5374 ofpacts_update_instruction_actions(struct ofpbuf
*openflow
, size_t ofs
)
5376 struct ofp11_instruction_actions
*oia
;
5378 oia
= ofpbuf_at_assert(openflow
, ofs
, sizeof *oia
);
5379 if (openflow
->size
> ofs
+ sizeof *oia
) {
5380 oia
->len
= htons(openflow
->size
- ofs
);
5382 openflow
->size
= ofs
;
5386 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5387 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5388 * valid, otherwise an OpenFlow error code. */
5390 ofpact_check_output_port(ofp_port_t port
, ofp_port_t max_ports
)
5398 case OFPP_CONTROLLER
:
5404 if (ofp_to_u16(port
) < ofp_to_u16(max_ports
)) {
5407 return OFPERR_OFPBAC_BAD_OUT_PORT
;
5411 /* Removes the protocols that require consistency between match and actions
5412 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5414 * (An example of an inconsistency between match and actions is a flow that
5415 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5418 inconsistent_match(enum ofputil_protocol
*usable_protocols
)
5420 *usable_protocols
&= OFPUTIL_P_OF10_ANY
;
5423 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5424 * caller must restore them.
5426 * Modifies some actions, filling in fields that could not be properly set
5427 * without context. */
5429 ofpact_check__(enum ofputil_protocol
*usable_protocols
, struct ofpact
*a
,
5430 struct flow
*flow
, ofp_port_t max_ports
,
5431 uint8_t table_id
, uint8_t n_tables
)
5433 const struct ofpact_enqueue
*enqueue
;
5434 const struct mf_field
*mf
;
5438 return ofpact_check_output_port(ofpact_get_OUTPUT(a
)->port
,
5441 case OFPACT_CONTROLLER
:
5444 case OFPACT_ENQUEUE
:
5445 enqueue
= ofpact_get_ENQUEUE(a
);
5446 if (ofp_to_u16(enqueue
->port
) >= ofp_to_u16(max_ports
)
5447 && enqueue
->port
!= OFPP_IN_PORT
5448 && enqueue
->port
!= OFPP_LOCAL
) {
5449 return OFPERR_OFPBAC_BAD_OUT_PORT
;
5453 case OFPACT_OUTPUT_REG
:
5454 return mf_check_src(&ofpact_get_OUTPUT_REG(a
)->src
, flow
);
5457 return bundle_check(ofpact_get_BUNDLE(a
), max_ports
, flow
);
5459 case OFPACT_SET_VLAN_VID
:
5460 /* Remember if we saw a vlan tag in the flow to aid translating to
5461 * OpenFlow 1.1+ if need be. */
5462 ofpact_get_SET_VLAN_VID(a
)->flow_has_vlan
=
5463 (flow
->vlan_tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
5464 if (!(flow
->vlan_tci
& htons(VLAN_CFI
)) &&
5465 !ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
5466 inconsistent_match(usable_protocols
);
5468 /* Temporary mark that we have a vlan tag. */
5469 flow
->vlan_tci
|= htons(VLAN_CFI
);
5472 case OFPACT_SET_VLAN_PCP
:
5473 /* Remember if we saw a vlan tag in the flow to aid translating to
5474 * OpenFlow 1.1+ if need be. */
5475 ofpact_get_SET_VLAN_PCP(a
)->flow_has_vlan
=
5476 (flow
->vlan_tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
5477 if (!(flow
->vlan_tci
& htons(VLAN_CFI
)) &&
5478 !ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
5479 inconsistent_match(usable_protocols
);
5481 /* Temporary mark that we have a vlan tag. */
5482 flow
->vlan_tci
|= htons(VLAN_CFI
);
5485 case OFPACT_STRIP_VLAN
:
5486 if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
5487 inconsistent_match(usable_protocols
);
5489 /* Temporary mark that we have no vlan tag. */
5490 flow
->vlan_tci
= htons(0);
5493 case OFPACT_PUSH_VLAN
:
5494 if (flow
->vlan_tci
& htons(VLAN_CFI
)) {
5495 /* Multiple VLAN headers not supported. */
5496 return OFPERR_OFPBAC_BAD_TAG
;
5498 /* Temporary mark that we have a vlan tag. */
5499 flow
->vlan_tci
|= htons(VLAN_CFI
);
5502 case OFPACT_SET_ETH_SRC
:
5503 case OFPACT_SET_ETH_DST
:
5506 case OFPACT_SET_IPV4_SRC
:
5507 case OFPACT_SET_IPV4_DST
:
5508 if (flow
->dl_type
!= htons(ETH_TYPE_IP
)) {
5509 inconsistent_match(usable_protocols
);
5513 case OFPACT_SET_IP_DSCP
:
5514 case OFPACT_SET_IP_ECN
:
5515 case OFPACT_SET_IP_TTL
:
5516 case OFPACT_DEC_TTL
:
5517 if (!is_ip_any(flow
)) {
5518 inconsistent_match(usable_protocols
);
5522 case OFPACT_SET_L4_SRC_PORT
:
5523 case OFPACT_SET_L4_DST_PORT
:
5524 if (!is_ip_any(flow
) || (flow
->nw_frag
& FLOW_NW_FRAG_LATER
) ||
5525 (flow
->nw_proto
!= IPPROTO_TCP
&& flow
->nw_proto
!= IPPROTO_UDP
5526 && flow
->nw_proto
!= IPPROTO_SCTP
)) {
5527 inconsistent_match(usable_protocols
);
5529 /* Note on which transport protocol the port numbers are set.
5530 * This allows this set action to be converted to an OF1.2 set field
5532 if (a
->type
== OFPACT_SET_L4_SRC_PORT
) {
5533 ofpact_get_SET_L4_SRC_PORT(a
)->flow_ip_proto
= flow
->nw_proto
;
5535 ofpact_get_SET_L4_DST_PORT(a
)->flow_ip_proto
= flow
->nw_proto
;
5539 case OFPACT_REG_MOVE
:
5540 return nxm_reg_move_check(ofpact_get_REG_MOVE(a
), flow
);
5542 case OFPACT_SET_FIELD
:
5543 mf
= ofpact_get_SET_FIELD(a
)->field
;
5544 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
5545 if (!mf_are_prereqs_ok(mf
, flow
) ||
5546 (mf
->id
== MFF_VLAN_VID
&& !(flow
->vlan_tci
& htons(VLAN_CFI
)))) {
5547 VLOG_WARN_RL(&rl
, "set_field %s lacks correct prerequisities",
5549 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
5551 /* Remember if we saw a vlan tag in the flow to aid translating to
5552 * OpenFlow 1.1 if need be. */
5553 ofpact_get_SET_FIELD(a
)->flow_has_vlan
=
5554 (flow
->vlan_tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
5555 if (mf
->id
== MFF_VLAN_TCI
) {
5556 /* The set field may add or remove the vlan tag,
5557 * Mark the status temporarily. */
5558 flow
->vlan_tci
= ofpact_get_SET_FIELD(a
)->value
.be16
;
5562 case OFPACT_STACK_PUSH
:
5563 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a
), flow
);
5565 case OFPACT_STACK_POP
:
5566 return nxm_stack_pop_check(ofpact_get_STACK_POP(a
), flow
);
5568 case OFPACT_SET_MPLS_LABEL
:
5569 case OFPACT_SET_MPLS_TC
:
5570 case OFPACT_SET_MPLS_TTL
:
5571 case OFPACT_DEC_MPLS_TTL
:
5572 if (!eth_type_mpls(flow
->dl_type
)) {
5573 inconsistent_match(usable_protocols
);
5577 case OFPACT_SET_TUNNEL
:
5578 case OFPACT_SET_QUEUE
:
5579 case OFPACT_POP_QUEUE
:
5580 case OFPACT_RESUBMIT
:
5583 case OFPACT_FIN_TIMEOUT
:
5584 if (flow
->nw_proto
!= IPPROTO_TCP
) {
5585 inconsistent_match(usable_protocols
);
5590 return learn_check(ofpact_get_LEARN(a
), flow
);
5592 case OFPACT_CONJUNCTION
:
5595 case OFPACT_MULTIPATH
:
5596 return multipath_check(ofpact_get_MULTIPATH(a
), flow
);
5602 case OFPACT_PUSH_MPLS
:
5603 flow
->dl_type
= ofpact_get_PUSH_MPLS(a
)->ethertype
;
5604 /* The packet is now MPLS and the MPLS payload is opaque.
5605 * Thus nothing can be assumed about the network protocol.
5606 * Temporarily mark that we have no nw_proto. */
5610 case OFPACT_POP_MPLS
:
5611 if (!eth_type_mpls(flow
->dl_type
)) {
5612 inconsistent_match(usable_protocols
);
5614 flow
->dl_type
= ofpact_get_POP_MPLS(a
)->ethertype
;
5620 case OFPACT_CLEAR_ACTIONS
:
5623 case OFPACT_WRITE_ACTIONS
: {
5624 /* Use a temporary copy of 'usable_protocols' because we can't check
5625 * consistency of an action set. */
5626 struct ofpact_nest
*on
= ofpact_get_WRITE_ACTIONS(a
);
5627 enum ofputil_protocol p
= *usable_protocols
;
5628 return ofpacts_check(on
->actions
, ofpact_nest_get_action_len(on
),
5629 flow
, max_ports
, table_id
, n_tables
, &p
);
5632 case OFPACT_WRITE_METADATA
:
5635 case OFPACT_METER
: {
5636 uint32_t mid
= ofpact_get_METER(a
)->meter_id
;
5637 if (mid
== 0 || mid
> OFPM13_MAX
) {
5638 return OFPERR_OFPMMFC_INVALID_METER
;
5643 case OFPACT_GOTO_TABLE
: {
5644 uint8_t goto_table
= ofpact_get_GOTO_TABLE(a
)->table_id
;
5645 if ((table_id
!= 255 && goto_table
<= table_id
)
5646 || (n_tables
!= 255 && goto_table
>= n_tables
)) {
5647 return OFPERR_OFPBIC_BAD_TABLE_ID
;
5655 case OFPACT_UNROLL_XLATE
:
5656 /* UNROLL is an internal action that should never be seen via
5658 return OFPERR_OFPBAC_BAD_TYPE
;
5665 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5666 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
5667 * switch with no more than 'max_ports' ports.
5669 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
5670 * '*usable_protocols' the protocols that forbid the inconsistency. (An
5671 * example of an inconsistency between match and actions is a flow that does
5672 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
5674 * May annotate ofpacts with information gathered from the 'flow'.
5676 * May temporarily modify 'flow', but restores the changes before returning. */
5678 ofpacts_check(struct ofpact ofpacts
[], size_t ofpacts_len
,
5679 struct flow
*flow
, ofp_port_t max_ports
,
5680 uint8_t table_id
, uint8_t n_tables
,
5681 enum ofputil_protocol
*usable_protocols
)
5684 ovs_be16 dl_type
= flow
->dl_type
;
5685 ovs_be16 vlan_tci
= flow
->vlan_tci
;
5686 uint8_t nw_proto
= flow
->nw_proto
;
5687 enum ofperr error
= 0;
5689 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
5690 error
= ofpact_check__(usable_protocols
, a
, flow
,
5691 max_ports
, table_id
, n_tables
);
5696 /* Restore fields that may have been modified. */
5697 flow
->dl_type
= dl_type
;
5698 flow
->vlan_tci
= vlan_tci
;
5699 flow
->nw_proto
= nw_proto
;
5703 /* Like ofpacts_check(), but reports inconsistencies as
5704 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
5706 ofpacts_check_consistency(struct ofpact ofpacts
[], size_t ofpacts_len
,
5707 struct flow
*flow
, ofp_port_t max_ports
,
5708 uint8_t table_id
, uint8_t n_tables
,
5709 enum ofputil_protocol usable_protocols
)
5711 enum ofputil_protocol p
= usable_protocols
;
5714 error
= ofpacts_check(ofpacts
, ofpacts_len
, flow
, max_ports
,
5715 table_id
, n_tables
, &p
);
5716 return (error
? error
5717 : p
!= usable_protocols
? OFPERR_OFPBAC_MATCH_INCONSISTENT
5721 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
5722 * appropriate order as defined by the OpenFlow spec and as required by Open
5725 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
5726 * instructions that are allowed within 'ofpacts[]'. */
5728 ofpacts_verify(const struct ofpact ofpacts
[], size_t ofpacts_len
,
5729 uint32_t allowed_ovsinsts
)
5731 const struct ofpact
*a
;
5732 enum ovs_instruction_type inst
;
5734 inst
= OVSINST_OFPIT13_METER
;
5735 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
5736 enum ovs_instruction_type next
;
5738 if (a
->type
== OFPACT_CONJUNCTION
) {
5739 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
5740 if (a
->type
!= OFPACT_CONJUNCTION
) {
5741 VLOG_WARN("when conjunction action is present, it must be "
5742 "the only kind of action used (saw '%s' action)",
5743 ofpact_name(a
->type
));
5744 return OFPERR_NXBAC_BAD_CONJUNCTION
;
5750 next
= ovs_instruction_type_from_ofpact_type(a
->type
);
5752 && (inst
== OVSINST_OFPIT11_APPLY_ACTIONS
5755 const char *name
= ovs_instruction_name_from_type(inst
);
5756 const char *next_name
= ovs_instruction_name_from_type(next
);
5759 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
5760 "1.1+ compatibility", name
);
5762 VLOG_WARN("invalid instruction ordering: %s must appear "
5763 "before %s, for OpenFlow 1.1+ compatibility",
5766 return OFPERR_OFPBAC_UNSUPPORTED_ORDER
;
5768 if (!((1u << next
) & allowed_ovsinsts
)) {
5769 const char *name
= ovs_instruction_name_from_type(next
);
5771 VLOG_WARN("%s instruction not allowed here", name
);
5772 return OFPERR_OFPBIC_UNSUP_INST
;
5781 /* Converting ofpacts to OpenFlow. */
5784 encode_ofpact(const struct ofpact
*a
, enum ofp_version ofp_version
,
5788 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5789 case OFPACT_##ENUM: \
5790 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
5799 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
5800 * actions in 'openflow', appending the actions to any existing data in
5803 ofpacts_put_openflow_actions(const struct ofpact ofpacts
[], size_t ofpacts_len
,
5804 struct ofpbuf
*openflow
,
5805 enum ofp_version ofp_version
)
5807 const struct ofpact
*a
;
5808 size_t start_size
= openflow
->size
;
5810 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
5811 encode_ofpact(a
, ofp_version
, openflow
);
5813 return openflow
->size
- start_size
;
5816 static enum ovs_instruction_type
5817 ofpact_is_apply_actions(const struct ofpact
*a
)
5819 return (ovs_instruction_type_from_ofpact_type(a
->type
)
5820 == OVSINST_OFPIT11_APPLY_ACTIONS
);
5824 ofpacts_put_openflow_instructions(const struct ofpact ofpacts
[],
5826 struct ofpbuf
*openflow
,
5827 enum ofp_version ofp_version
)
5829 const struct ofpact
*end
= ofpact_end(ofpacts
, ofpacts_len
);
5830 const struct ofpact
*a
;
5832 if (ofp_version
== OFP10_VERSION
) {
5833 ofpacts_put_openflow_actions(ofpacts
, ofpacts_len
, openflow
,
5840 if (ofpact_is_apply_actions(a
)) {
5841 size_t ofs
= openflow
->size
;
5843 instruction_put_OFPIT11_APPLY_ACTIONS(openflow
);
5845 encode_ofpact(a
, ofp_version
, openflow
);
5847 } while (a
< end
&& ofpact_is_apply_actions(a
));
5848 ofpacts_update_instruction_actions(openflow
, ofs
);
5850 encode_ofpact(a
, ofp_version
, openflow
);
5856 /* Sets of supported actions. */
5858 /* Two-way translation between OVS's internal "OFPACT_*" representation of
5859 * actions and the "OFPAT_*" representation used in some OpenFlow version.
5860 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
5861 * instance is specific to one OpenFlow version.) */
5863 enum ofpact_type ofpact
; /* Internal name for action type. */
5864 int ofpat
; /* OFPAT_* number from OpenFlow spec. */
5867 static const struct ofpact_map
*
5868 get_ofpact_map(enum ofp_version version
)
5870 /* OpenFlow 1.0 actions. */
5871 static const struct ofpact_map of10
[] = {
5872 { OFPACT_OUTPUT
, 0 },
5873 { OFPACT_SET_VLAN_VID
, 1 },
5874 { OFPACT_SET_VLAN_PCP
, 2 },
5875 { OFPACT_STRIP_VLAN
, 3 },
5876 { OFPACT_SET_ETH_SRC
, 4 },
5877 { OFPACT_SET_ETH_DST
, 5 },
5878 { OFPACT_SET_IPV4_SRC
, 6 },
5879 { OFPACT_SET_IPV4_DST
, 7 },
5880 { OFPACT_SET_IP_DSCP
, 8 },
5881 { OFPACT_SET_L4_SRC_PORT
, 9 },
5882 { OFPACT_SET_L4_DST_PORT
, 10 },
5883 { OFPACT_ENQUEUE
, 11 },
5887 /* OpenFlow 1.1 actions. */
5888 static const struct ofpact_map of11
[] = {
5889 { OFPACT_OUTPUT
, 0 },
5890 { OFPACT_SET_VLAN_VID
, 1 },
5891 { OFPACT_SET_VLAN_PCP
, 2 },
5892 { OFPACT_SET_ETH_SRC
, 3 },
5893 { OFPACT_SET_ETH_DST
, 4 },
5894 { OFPACT_SET_IPV4_SRC
, 5 },
5895 { OFPACT_SET_IPV4_DST
, 6 },
5896 { OFPACT_SET_IP_DSCP
, 7 },
5897 { OFPACT_SET_IP_ECN
, 8 },
5898 { OFPACT_SET_L4_SRC_PORT
, 9 },
5899 { OFPACT_SET_L4_DST_PORT
, 10 },
5900 /* OFPAT_COPY_TTL_OUT (11) not supported. */
5901 /* OFPAT_COPY_TTL_IN (12) not supported. */
5902 { OFPACT_SET_MPLS_LABEL
, 13 },
5903 { OFPACT_SET_MPLS_TC
, 14 },
5904 { OFPACT_SET_MPLS_TTL
, 15 },
5905 { OFPACT_DEC_MPLS_TTL
, 16 },
5906 { OFPACT_PUSH_VLAN
, 17 },
5907 { OFPACT_STRIP_VLAN
, 18 },
5908 { OFPACT_PUSH_MPLS
, 19 },
5909 { OFPACT_POP_MPLS
, 20 },
5910 { OFPACT_SET_QUEUE
, 21 },
5911 { OFPACT_GROUP
, 22 },
5912 { OFPACT_SET_IP_TTL
, 23 },
5913 { OFPACT_DEC_TTL
, 24 },
5917 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
5918 static const struct ofpact_map of12
[] = {
5919 { OFPACT_OUTPUT
, 0 },
5920 /* OFPAT_COPY_TTL_OUT (11) not supported. */
5921 /* OFPAT_COPY_TTL_IN (12) not supported. */
5922 { OFPACT_SET_MPLS_TTL
, 15 },
5923 { OFPACT_DEC_MPLS_TTL
, 16 },
5924 { OFPACT_PUSH_VLAN
, 17 },
5925 { OFPACT_STRIP_VLAN
, 18 },
5926 { OFPACT_PUSH_MPLS
, 19 },
5927 { OFPACT_POP_MPLS
, 20 },
5928 { OFPACT_SET_QUEUE
, 21 },
5929 { OFPACT_GROUP
, 22 },
5930 { OFPACT_SET_IP_TTL
, 23 },
5931 { OFPACT_DEC_TTL
, 24 },
5932 { OFPACT_SET_FIELD
, 25 },
5933 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
5934 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
5954 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
5955 * values, into a bitmap of actions suitable for OpenFlow 'version', and
5956 * returns the result. */
5958 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap
, enum ofp_version version
)
5960 uint32_t openflow_bitmap
= 0;
5961 const struct ofpact_map
*x
;
5963 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
5964 if (ofpacts_bitmap
& (UINT64_C(1) << x
->ofpact
)) {
5965 openflow_bitmap
|= 1u << x
->ofpat
;
5968 return htonl(openflow_bitmap
);
5971 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
5972 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
5973 * and returns the result. */
5975 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap
, enum ofp_version version
)
5977 uint64_t ofpact_bitmap
= 0;
5978 const struct ofpact_map
*x
;
5980 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
5981 if (ofpat_bitmap
& htonl(1u << x
->ofpat
)) {
5982 ofpact_bitmap
|= UINT64_C(1) << x
->ofpact
;
5985 return ofpact_bitmap
;
5988 /* Appends to 's' a string representation of the set of OFPACT_* represented
5989 * by 'ofpacts_bitmap'. */
5991 ofpact_bitmap_format(uint64_t ofpacts_bitmap
, struct ds
*s
)
5993 if (!ofpacts_bitmap
) {
5994 ds_put_cstr(s
, "<none>");
5996 while (ofpacts_bitmap
) {
5997 ds_put_format(s
, "%s ",
5998 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap
)));
5999 ofpacts_bitmap
= zero_rightmost_1bit(ofpacts_bitmap
);
6005 /* Returns true if 'action' outputs to 'port', false otherwise. */
6007 ofpact_outputs_to_port(const struct ofpact
*ofpact
, ofp_port_t port
)
6009 switch (ofpact
->type
) {
6011 return ofpact_get_OUTPUT(ofpact
)->port
== port
;
6012 case OFPACT_ENQUEUE
:
6013 return ofpact_get_ENQUEUE(ofpact
)->port
== port
;
6014 case OFPACT_CONTROLLER
:
6015 return port
== OFPP_CONTROLLER
;
6017 case OFPACT_OUTPUT_REG
:
6019 case OFPACT_SET_VLAN_VID
:
6020 case OFPACT_SET_VLAN_PCP
:
6021 case OFPACT_STRIP_VLAN
:
6022 case OFPACT_PUSH_VLAN
:
6023 case OFPACT_SET_ETH_SRC
:
6024 case OFPACT_SET_ETH_DST
:
6025 case OFPACT_SET_IPV4_SRC
:
6026 case OFPACT_SET_IPV4_DST
:
6027 case OFPACT_SET_IP_DSCP
:
6028 case OFPACT_SET_IP_ECN
:
6029 case OFPACT_SET_IP_TTL
:
6030 case OFPACT_SET_L4_SRC_PORT
:
6031 case OFPACT_SET_L4_DST_PORT
:
6032 case OFPACT_REG_MOVE
:
6033 case OFPACT_SET_FIELD
:
6034 case OFPACT_STACK_PUSH
:
6035 case OFPACT_STACK_POP
:
6036 case OFPACT_DEC_TTL
:
6037 case OFPACT_SET_MPLS_LABEL
:
6038 case OFPACT_SET_MPLS_TC
:
6039 case OFPACT_SET_MPLS_TTL
:
6040 case OFPACT_DEC_MPLS_TTL
:
6041 case OFPACT_SET_TUNNEL
:
6042 case OFPACT_WRITE_METADATA
:
6043 case OFPACT_SET_QUEUE
:
6044 case OFPACT_POP_QUEUE
:
6045 case OFPACT_FIN_TIMEOUT
:
6046 case OFPACT_RESUBMIT
:
6048 case OFPACT_CONJUNCTION
:
6049 case OFPACT_MULTIPATH
:
6052 case OFPACT_UNROLL_XLATE
:
6053 case OFPACT_PUSH_MPLS
:
6054 case OFPACT_POP_MPLS
:
6056 case OFPACT_CLEAR_ACTIONS
:
6057 case OFPACT_WRITE_ACTIONS
:
6058 case OFPACT_GOTO_TABLE
:
6066 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6067 * to 'port', false otherwise. */
6069 ofpacts_output_to_port(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
6072 const struct ofpact
*a
;
6074 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
6075 if (ofpact_outputs_to_port(a
, port
)) {
6083 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6084 * to 'group', false otherwise. */
6086 ofpacts_output_to_group(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
6089 const struct ofpact
*a
;
6091 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
6092 if (a
->type
== OFPACT_GROUP
6093 && ofpact_get_GROUP(a
)->group_id
== group_id
) {
6102 ofpacts_equal(const struct ofpact
*a
, size_t a_len
,
6103 const struct ofpact
*b
, size_t b_len
)
6105 return a_len
== b_len
&& !memcmp(a
, b
, a_len
);
6108 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
6109 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
6111 * This function relies on the order of 'ofpacts' being correct (as checked by
6112 * ofpacts_verify()). */
6114 ofpacts_get_meter(const struct ofpact ofpacts
[], size_t ofpacts_len
)
6116 const struct ofpact
*a
;
6118 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
6119 enum ovs_instruction_type inst
;
6121 inst
= ovs_instruction_type_from_ofpact_type(a
->type
);
6122 if (a
->type
== OFPACT_METER
) {
6123 return ofpact_get_METER(a
)->meter_id
;
6124 } else if (inst
> OVSINST_OFPIT13_METER
) {
6132 /* Formatting ofpacts. */
6135 ofpact_format(const struct ofpact
*a
, struct ds
*s
)
6138 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6139 case OFPACT_##ENUM: \
6140 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
6149 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
6150 * 'ofpacts' to 'string'. */
6152 ofpacts_format(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
6156 ds_put_cstr(string
, "drop");
6158 const struct ofpact
*a
;
6160 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
6162 ds_put_cstr(string
, ",");
6165 /* XXX write-actions */
6166 ofpact_format(a
, string
);
6171 /* Internal use by helpers. */
6174 ofpact_put(struct ofpbuf
*ofpacts
, enum ofpact_type type
, size_t len
)
6176 struct ofpact
*ofpact
;
6178 ofpact_pad(ofpacts
);
6179 ofpacts
->header
= ofpbuf_put_uninit(ofpacts
, len
);
6180 ofpact
= ofpacts
->header
;
6181 ofpact_init(ofpact
, type
, len
);
6186 ofpact_init(struct ofpact
*ofpact
, enum ofpact_type type
, size_t len
)
6188 memset(ofpact
, 0, len
);
6189 ofpact
->type
= type
;
6194 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6195 * starting at 'ofpact'.
6197 * This is the correct way to update a variable-length ofpact's length after
6198 * adding the variable-length part of the payload. (See the large comment
6199 * near the end of ofp-actions.h for more information.) */
6201 ofpact_update_len(struct ofpbuf
*ofpacts
, struct ofpact
*ofpact
)
6203 ovs_assert(ofpact
== ofpacts
->header
);
6204 ofpact
->len
= (char *) ofpbuf_tail(ofpacts
) - (char *) ofpact
;
6207 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6208 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6209 * client must call this itself after adding the final ofpact to an array of
6212 * (The consequences of failing to call this function are probably not dire.
6213 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6214 * not dereference it. That's undefined behavior, technically, but it will not
6215 * cause a real problem on common systems. Still, it seems better to call
6218 ofpact_pad(struct ofpbuf
*ofpacts
)
6220 unsigned int pad
= PAD_SIZE(ofpacts
->size
, OFPACT_ALIGNTO
);
6222 ofpbuf_put_zeros(ofpacts
, pad
);
6229 static char * OVS_WARN_UNUSED_RESULT
6230 ofpact_parse(enum ofpact_type type
, char *value
, struct ofpbuf
*ofpacts
,
6231 enum ofputil_protocol
*usable_protocols
)
6234 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6235 case OFPACT_##ENUM: \
6236 return parse_##ENUM(value, ofpacts, usable_protocols);
6245 ofpact_type_from_name(const char *name
, enum ofpact_type
*type
)
6247 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6248 if (!strcasecmp(name, NAME)) { \
6249 *type = OFPACT_##ENUM; \
6258 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6260 * Returns NULL if successful, otherwise a malloc()'d string describing the
6261 * error. The caller is responsible for freeing the returned string. */
6262 static char * OVS_WARN_UNUSED_RESULT
6263 ofpacts_parse__(char *str
, struct ofpbuf
*ofpacts
,
6264 enum ofputil_protocol
*usable_protocols
,
6265 bool allow_instructions
)
6274 while (ofputil_parse_key_value(&pos
, &key
, &value
)) {
6275 enum ovs_instruction_type inst
= OVSINST_OFPIT11_APPLY_ACTIONS
;
6276 enum ofpact_type type
;
6280 if (ofpact_type_from_name(key
, &type
)) {
6281 error
= ofpact_parse(type
, value
, ofpacts
, usable_protocols
);
6282 inst
= ovs_instruction_type_from_ofpact_type(type
);
6283 } else if (!strcasecmp(key
, "mod_vlan_vid")) {
6284 error
= parse_set_vlan_vid(value
, ofpacts
, true);
6285 } else if (!strcasecmp(key
, "mod_vlan_pcp")) {
6286 error
= parse_set_vlan_pcp(value
, ofpacts
, true);
6287 } else if (!strcasecmp(key
, "set_nw_ttl")) {
6288 error
= parse_SET_IP_TTL(value
, ofpacts
, usable_protocols
);
6289 } else if (!strcasecmp(key
, "pop_vlan")) {
6290 error
= parse_pop_vlan(ofpacts
);
6291 } else if (!strcasecmp(key
, "set_tunnel64")) {
6292 error
= parse_set_tunnel(value
, ofpacts
,
6293 NXAST_RAW_SET_TUNNEL64
);
6294 } else if (!strcasecmp(key
, "load")) {
6295 error
= parse_reg_load(value
, ofpacts
);
6296 } else if (!strcasecmp(key
, "bundle_load")) {
6297 error
= parse_bundle_load(value
, ofpacts
);
6298 } else if (!strcasecmp(key
, "drop")) {
6300 } else if (!strcasecmp(key
, "apply_actions")) {
6301 return xstrdup("apply_actions is the default instruction");
6302 } else if (ofputil_port_from_string(key
, &port
)) {
6303 ofpact_put_OUTPUT(ofpacts
)->port
= port
;
6305 return xasprintf("unknown action %s", key
);
6311 if (inst
!= OVSINST_OFPIT11_APPLY_ACTIONS
) {
6312 if (!allow_instructions
) {
6313 return xasprintf("only actions are allowed here (not "
6315 ovs_instruction_name_from_type(inst
));
6317 if (inst
== prev_inst
) {
6318 return xasprintf("instruction %s may be specified only once",
6319 ovs_instruction_name_from_type(inst
));
6322 if (prev_inst
!= -1 && inst
< prev_inst
) {
6323 return xasprintf("instruction %s must be specified before %s",
6324 ovs_instruction_name_from_type(inst
),
6325 ovs_instruction_name_from_type(prev_inst
));
6329 ofpact_pad(ofpacts
);
6331 if (drop
&& ofpacts
->size
) {
6332 return xstrdup("\"drop\" must not be accompanied by any other action "
6336 retval
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
,
6338 ? (1u << N_OVS_INSTRUCTIONS
) - 1
6339 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS
));
6341 return xstrdup("Incorrect instruction ordering");
6347 static char * OVS_WARN_UNUSED_RESULT
6348 ofpacts_parse(char *str
, struct ofpbuf
*ofpacts
,
6349 enum ofputil_protocol
*usable_protocols
, bool allow_instructions
)
6351 uint32_t orig_size
= ofpacts
->size
;
6352 char *error
= ofpacts_parse__(str
, ofpacts
, usable_protocols
,
6353 allow_instructions
);
6355 ofpacts
->size
= orig_size
;
6360 static char * OVS_WARN_UNUSED_RESULT
6361 ofpacts_parse_copy(const char *s_
, struct ofpbuf
*ofpacts
,
6362 enum ofputil_protocol
*usable_protocols
,
6363 bool allow_instructions
)
6367 *usable_protocols
= OFPUTIL_P_ANY
;
6370 error
= ofpacts_parse(s
, ofpacts
, usable_protocols
, allow_instructions
);
6376 /* Parses 's' as a set of OpenFlow actions and appends the actions to
6379 * Returns NULL if successful, otherwise a malloc()'d string describing the
6380 * error. The caller is responsible for freeing the returned string. */
6381 char * OVS_WARN_UNUSED_RESULT
6382 ofpacts_parse_actions(const char *s
, struct ofpbuf
*ofpacts
,
6383 enum ofputil_protocol
*usable_protocols
)
6385 return ofpacts_parse_copy(s
, ofpacts
, usable_protocols
, false);
6388 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
6391 * Returns NULL if successful, otherwise a malloc()'d string describing the
6392 * error. The caller is responsible for freeing the returned string. */
6393 char * OVS_WARN_UNUSED_RESULT
6394 ofpacts_parse_instructions(const char *s
, struct ofpbuf
*ofpacts
,
6395 enum ofputil_protocol
*usable_protocols
)
6397 return ofpacts_parse_copy(s
, ofpacts
, usable_protocols
, true);
6401 ofpact_name(enum ofpact_type type
)
6404 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
6411 /* Low-level action decoding and encoding functions. */
6413 /* Everything needed to identify a particular OpenFlow action. */
6414 struct ofpact_hdrs
{
6415 uint32_t vendor
; /* 0 if standard, otherwise a vendor code. */
6416 uint16_t type
; /* Type if standard, otherwise subtype. */
6417 uint8_t ofp_version
; /* From ofp_header. */
6420 /* Information about a particular OpenFlow action. */
6421 struct ofpact_raw_instance
{
6422 /* The action's identity. */
6423 struct ofpact_hdrs hdrs
;
6424 enum ofp_raw_action_type raw
;
6426 /* Looking up the action. */
6427 struct hmap_node decode_node
; /* Based on 'hdrs'. */
6428 struct hmap_node encode_node
; /* Based on 'raw' + 'hdrs.ofp_version'. */
6430 /* The action's encoded size.
6432 * If this action is fixed-length, 'min_length' == 'max_length'.
6433 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
6434 * OFP_ACTION_ALIGN) == 65528. */
6435 unsigned short int min_length
;
6436 unsigned short int max_length
;
6438 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
6439 * offset of that argument from the beginning of the action and 'arg_len'
6440 * its length, both in bytes.
6442 * For actions that take other forms, these are both zero. */
6443 unsigned short int arg_ofs
;
6444 unsigned short int arg_len
;
6446 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
6449 /* If this action is deprecated, a human-readable string with a brief
6451 const char *deprecation
;
6454 /* Action header. */
6455 struct ofp_action_header
{
6456 /* The meaning of other values of 'type' generally depends on the OpenFlow
6457 * version (see enum ofp_raw_action_type).
6459 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
6460 * designates an OpenFlow vendor ID and that the remainder of the action
6461 * structure has a vendor-defined meaning.
6463 #define OFPAT_VENDOR 0xffff
6466 /* Always a multiple of 8. */
6469 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
6470 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
6473 OFP_ASSERT(sizeof(struct ofp_action_header
) == 8);
6475 /* Header for Nicira-defined actions and for ONF vendor extensions.
6477 * This cannot be used as an entirely generic vendor extension action header,
6478 * because OpenFlow does not specify the location or size of the action
6479 * subtype; it just happens that ONF extensions and Nicira extensions share
6481 struct ext_action_header
{
6482 ovs_be16 type
; /* OFPAT_VENDOR. */
6483 ovs_be16 len
; /* At least 16. */
6484 ovs_be32 vendor
; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
6485 ovs_be16 subtype
; /* See enum ofp_raw_action_type. */
6488 OFP_ASSERT(sizeof(struct ext_action_header
) == 16);
6491 ofpact_hdrs_equal(const struct ofpact_hdrs
*a
,
6492 const struct ofpact_hdrs
*b
)
6494 return (a
->vendor
== b
->vendor
6495 && a
->type
== b
->type
6496 && a
->ofp_version
== b
->ofp_version
);
6500 ofpact_hdrs_hash(const struct ofpact_hdrs
*hdrs
)
6502 return hash_2words(hdrs
->vendor
, (hdrs
->type
<< 16) | hdrs
->ofp_version
);
6505 #include "ofp-actions.inc2"
6507 static struct hmap
*
6508 ofpact_decode_hmap(void)
6510 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
6511 static struct hmap hmap
;
6513 if (ovsthread_once_start(&once
)) {
6514 struct ofpact_raw_instance
*inst
;
6517 for (inst
= all_raw_instances
;
6518 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
6520 hmap_insert(&hmap
, &inst
->decode_node
,
6521 ofpact_hdrs_hash(&inst
->hdrs
));
6523 ovsthread_once_done(&once
);
6528 static struct hmap
*
6529 ofpact_encode_hmap(void)
6531 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
6532 static struct hmap hmap
;
6534 if (ovsthread_once_start(&once
)) {
6535 struct ofpact_raw_instance
*inst
;
6538 for (inst
= all_raw_instances
;
6539 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
6541 hmap_insert(&hmap
, &inst
->encode_node
,
6542 hash_2words(inst
->raw
, inst
->hdrs
.ofp_version
));
6544 ovsthread_once_done(&once
);
6550 ofpact_decode_raw(enum ofp_version ofp_version
,
6551 const struct ofp_action_header
*oah
, size_t length
,
6552 const struct ofpact_raw_instance
**instp
)
6554 const struct ofpact_raw_instance
*inst
;
6555 struct ofpact_hdrs hdrs
;
6558 if (length
< sizeof *oah
) {
6559 return OFPERR_OFPBAC_BAD_LEN
;
6562 /* Get base action type. */
6563 if (oah
->type
== htons(OFPAT_VENDOR
)) {
6565 hdrs
.vendor
= ntohl(oah
->vendor
);
6566 if (hdrs
.vendor
== NX_VENDOR_ID
|| hdrs
.vendor
== ONF_VENDOR_ID
) {
6567 /* Get extension subtype. */
6568 const struct ext_action_header
*nah
;
6570 nah
= ALIGNED_CAST(const struct ext_action_header
*, oah
);
6571 if (length
< sizeof *nah
) {
6572 return OFPERR_OFPBAC_BAD_LEN
;
6574 hdrs
.type
= ntohs(nah
->subtype
);
6576 VLOG_WARN_RL(&rl
, "OpenFlow action has unknown vendor %#"PRIx32
,
6578 return OFPERR_OFPBAC_BAD_VENDOR
;
6582 hdrs
.type
= ntohs(oah
->type
);
6585 hdrs
.ofp_version
= ofp_version
;
6586 HMAP_FOR_EACH_WITH_HASH (inst
, decode_node
, ofpact_hdrs_hash(&hdrs
),
6587 ofpact_decode_hmap()) {
6588 if (ofpact_hdrs_equal(&hdrs
, &inst
->hdrs
)) {
6595 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
6596 : OFPERR_OFPBAC_BAD_TYPE
);
6600 ofpact_pull_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
6601 enum ofp_raw_action_type
*raw
, uint64_t *arg
)
6603 const struct ofp_action_header
*oah
= buf
->data
;
6604 const struct ofpact_raw_instance
*action
;
6605 unsigned int length
;
6609 error
= ofpact_decode_raw(ofp_version
, oah
, buf
->size
, &action
);
6614 if (action
->deprecation
) {
6615 VLOG_INFO_RL(&rl
, "%s is deprecated in %s (%s)",
6616 action
->name
, ofputil_version_to_string(ofp_version
),
6617 action
->deprecation
);
6620 length
= ntohs(oah
->len
);
6621 if (length
> buf
->size
) {
6622 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u exceeds action buffer "
6623 "length %"PRIu32
, action
->name
, length
, buf
->size
);
6624 return OFPERR_OFPBAC_BAD_LEN
;
6626 if (length
< action
->min_length
|| length
> action
->max_length
) {
6627 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u not in valid range "
6628 "[%hu,%hu]", action
->name
, length
,
6629 action
->min_length
, action
->max_length
);
6630 return OFPERR_OFPBAC_BAD_LEN
;
6633 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u is not a multiple "
6634 "of 8", action
->name
, length
);
6635 return OFPERR_OFPBAC_BAD_LEN
;
6640 if (action
->arg_len
) {
6644 p
= ofpbuf_at_assert(buf
, action
->arg_ofs
, action
->arg_len
);
6645 for (i
= 0; i
< action
->arg_len
; i
++) {
6646 *arg
= (*arg
<< 8) | p
[i
];
6650 ofpbuf_pull(buf
, length
);
6655 static const struct ofpact_raw_instance
*
6656 ofpact_raw_lookup(enum ofp_version ofp_version
, enum ofp_raw_action_type raw
)
6658 const struct ofpact_raw_instance
*inst
;
6660 HMAP_FOR_EACH_WITH_HASH (inst
, encode_node
, hash_2words(raw
, ofp_version
),
6661 ofpact_encode_hmap()) {
6662 if (inst
->raw
== raw
&& inst
->hdrs
.ofp_version
== ofp_version
) {
6670 ofpact_put_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
6671 enum ofp_raw_action_type raw
, uint64_t arg
)
6673 const struct ofpact_raw_instance
*inst
;
6674 struct ofp_action_header
*oah
;
6675 const struct ofpact_hdrs
*hdrs
;
6677 inst
= ofpact_raw_lookup(ofp_version
, raw
);
6680 oah
= ofpbuf_put_zeros(buf
, inst
->min_length
);
6681 oah
->type
= htons(hdrs
->vendor
? OFPAT_VENDOR
: hdrs
->type
);
6682 oah
->len
= htons(inst
->min_length
);
6683 oah
->vendor
= htonl(hdrs
->vendor
);
6685 switch (hdrs
->vendor
) {
6690 case ONF_VENDOR_ID
: {
6691 struct ext_action_header
*nah
= (struct ext_action_header
*) oah
;
6692 nah
->subtype
= htons(hdrs
->type
);
6700 if (inst
->arg_len
) {
6701 uint8_t *p
= (uint8_t *) oah
+ inst
->arg_ofs
+ inst
->arg_len
;
6704 for (i
= 0; i
< inst
->arg_len
; i
++) {
6716 pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
)
6718 struct ofp_action_header
*oah
;
6720 ofpbuf_put_zeros(openflow
, PAD_SIZE(openflow
->size
- start_ofs
, 8));
6722 oah
= ofpbuf_at_assert(openflow
, start_ofs
, sizeof *oah
);
6723 oah
->len
= htons(openflow
->size
- start_ofs
);