2 * Copyright (c) 2008-2016 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <netinet/in.h>
21 #include "byte-order.h"
27 #include "multipath.h"
29 #include "odp-netlink.h"
30 #include "openvswitch/dynamic-string.h"
31 #include "openvswitch/meta-flow.h"
32 #include "openvswitch/ofp-actions.h"
33 #include "openvswitch/ofp-util.h"
34 #include "openvswitch/ofp-parse.h"
35 #include "openvswitch/ofp-prop.h"
36 #include "openvswitch/ofpbuf.h"
37 #include "openvswitch/vlog.h"
38 #include "unaligned.h"
41 VLOG_DEFINE_THIS_MODULE(ofp_actions
);
43 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
45 struct ofp_action_header
;
47 /* Raw identifiers for OpenFlow actions.
49 * Decoding and encoding OpenFlow actions across multiple versions is difficult
50 * to do in a clean, consistent way. This enumeration lays out all of the
51 * forms of actions that Open vSwitch supports.
53 * The comments here must follow a stylized form because the
54 * "extract-ofp-actions" program parses them at build time to generate data
57 * - The first part of each comment specifies the vendor, OpenFlow versions,
58 * and type for each protocol that supports the action:
60 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
61 * extension actions. (Support for other vendors can be added, but
62 * it can't be done just based on a vendor ID definition alone
63 * because OpenFlow doesn't define a standard way to specify a
64 * subtype for vendor actions, so other vendors might do it different
67 * # The version can specify a specific OpenFlow version, a version
68 * range delimited by "-", or an open-ended range with "+".
70 * # The type, in parentheses, is the action type number (for standard
71 * OpenFlow actions) or subtype (for vendor extension actions).
73 * # Optionally one may add "is deprecated" followed by a
74 * human-readable reason in parentheses (which will be used in log
75 * messages), if a particular action should no longer be used.
77 * Multiple such specifications may be separated by commas.
79 * - The second part describes the action's wire format. It may be:
81 * # "struct <name>": The struct fully specifies the wire format. The
82 * action is exactly the size of the struct. (Thus, the struct must
83 * be an exact multiple of 8 bytes in size.)
85 * # "struct <name>, ...": The struct specifies the beginning of the
86 * wire format. An instance of the action is either the struct's
87 * exact size, or a multiple of 8 bytes longer.
89 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
90 * vendor extension) header, followed by 0 or more pad bytes to align
91 * to a multiple of <N> bits, followed by an argument of the given
92 * type, followed by 0 or more pad bytes to bring the total action up
93 * to a multiple of 8 bytes.
95 * # "void": The action is just a (standard or vendor extension)
98 * - Optional additional text enclosed in square brackets is commentary for
101 enum ofp_raw_action_type
{
102 /* ## ----------------- ## */
103 /* ## Standard actions. ## */
104 /* ## ----------------- ## */
106 /* OF1.0(0): struct ofp10_action_output. */
108 /* OF1.1+(0): struct ofp11_action_output. */
111 /* OF1.0(1): uint16_t. */
112 OFPAT_RAW10_SET_VLAN_VID
,
113 /* OF1.0(2): uint8_t. */
114 OFPAT_RAW10_SET_VLAN_PCP
,
116 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
118 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
119 * modification actions: the 1.0 versions push a VLAN header if none is
120 * present, but the 1.1 versions do not. That is the only reason that we
121 * distinguish their raw action types.] */
122 OFPAT_RAW11_SET_VLAN_VID
,
123 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
124 OFPAT_RAW11_SET_VLAN_PCP
,
126 /* OF1.1+(17): ovs_be16.
128 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
130 OFPAT_RAW11_PUSH_VLAN
,
132 /* OF1.0(3): void. */
133 OFPAT_RAW10_STRIP_VLAN
,
134 /* OF1.1+(18): void. */
135 OFPAT_RAW11_POP_VLAN
,
137 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
138 * ofp_action_dl_addr. */
139 OFPAT_RAW_SET_DL_SRC
,
141 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
142 * ofp_action_dl_addr. */
143 OFPAT_RAW_SET_DL_DST
,
145 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
147 OFPAT_RAW_SET_NW_SRC
,
149 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
151 OFPAT_RAW_SET_NW_DST
,
153 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
154 OFPAT_RAW_SET_NW_TOS
,
156 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
157 OFPAT_RAW11_SET_NW_ECN
,
159 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
161 OFPAT_RAW_SET_TP_SRC
,
163 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
165 OFPAT_RAW_SET_TP_DST
,
167 /* OF1.0(11): struct ofp10_action_enqueue. */
170 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
172 OFPAT_RAW_SET_MPLS_LABEL
,
174 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
176 OFPAT_RAW_SET_MPLS_TC
,
178 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
180 OFPAT_RAW_SET_MPLS_TTL
,
182 /* NX1.0(26), OF1.1+(16): void. */
183 OFPAT_RAW_DEC_MPLS_TTL
,
185 /* NX1.0(23), OF1.1+(19): ovs_be16.
187 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
190 /* NX1.0(24), OF1.1+(20): ovs_be16.
192 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
193 * ETH_TYPE_MPLS otherwise, not the label.] */
196 /* NX1.0(4), OF1.1+(21): uint32_t. */
199 /* NX1.0(40), OF1.1+(22): uint32_t. */
202 /* OF1.1+(23): uint8_t. */
203 OFPAT_RAW11_SET_NW_TTL
,
205 /* NX1.0(18), OF1.1+(24): void. */
206 OFPAT_RAW_DEC_NW_TTL
,
207 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
208 NXAST_RAW_DEC_TTL_CNT_IDS
,
210 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
211 OFPAT_RAW12_SET_FIELD
,
212 /* OF1.5+(25): struct ofp12_action_set_field, ... */
213 OFPAT_RAW15_SET_FIELD
,
214 /* NX1.0-1.4(7): struct nx_action_reg_load.
216 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
217 * we drop reg_load.] */
219 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
221 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
222 * we drop reg_load2.] */
225 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
226 OFPAT_RAW15_COPY_FIELD
,
227 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
228 ONFACT_RAW13_COPY_FIELD
,
229 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
232 /* ## ------------------------- ## */
233 /* ## Nicira extension actions. ## */
234 /* ## ------------------------- ## */
236 /* Actions similar to standard actions are listed with the standard actions. */
238 /* NX1.0+(1): uint16_t. */
240 /* NX1.0+(14): struct nx_action_resubmit. */
241 NXAST_RAW_RESUBMIT_TABLE
,
243 /* NX1.0+(2): uint32_t. */
244 NXAST_RAW_SET_TUNNEL
,
245 /* NX1.0+(9): uint64_t. */
246 NXAST_RAW_SET_TUNNEL64
,
248 /* NX1.0+(5): void. */
251 /* NX1.0+(8): struct nx_action_note, ... */
254 /* NX1.0+(10): struct nx_action_multipath. */
257 /* NX1.0+(12): struct nx_action_bundle, ... */
259 /* NX1.0+(13): struct nx_action_bundle, ... */
260 NXAST_RAW_BUNDLE_LOAD
,
262 /* NX1.0+(15): struct nx_action_output_reg. */
263 NXAST_RAW_OUTPUT_REG
,
264 /* NX1.0+(32): struct nx_action_output_reg2. */
265 NXAST_RAW_OUTPUT_REG2
,
267 /* NX1.0+(16): struct nx_action_learn, ... */
270 /* NX1.0+(17): void. */
273 /* NX1.0+(19): struct nx_action_fin_timeout. */
274 NXAST_RAW_FIN_TIMEOUT
,
276 /* NX1.0+(20): struct nx_action_controller. */
277 NXAST_RAW_CONTROLLER
,
278 /* NX1.0+(37): struct nx_action_controller2, ... */
279 NXAST_RAW_CONTROLLER2
,
281 /* NX1.0+(22): struct nx_action_write_metadata. */
282 NXAST_RAW_WRITE_METADATA
,
284 /* NX1.0+(27): struct nx_action_stack. */
285 NXAST_RAW_STACK_PUSH
,
287 /* NX1.0+(28): struct nx_action_stack. */
290 /* NX1.0+(29): struct nx_action_sample. */
292 /* NX1.0+(38): struct nx_action_sample2. */
295 /* NX1.0+(34): struct nx_action_conjunction. */
296 NXAST_RAW_CONJUNCTION
,
298 /* NX1.0+(35): struct nx_action_conntrack, ... */
301 /* NX1.0+(36): struct nx_action_nat, ... */
304 /* NX1.0+(39): struct nx_action_output_trunc. */
305 NXAST_RAW_OUTPUT_TRUNC
,
307 /* ## ------------------ ## */
308 /* ## Debugging actions. ## */
309 /* ## ------------------ ## */
311 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
312 /* accepts them only if started with --enable-dummy. */
314 /* NX1.0+(255): void. */
315 NXAST_RAW_DEBUG_RECIRC
,
318 /* OpenFlow actions are always a multiple of 8 bytes in length. */
319 #define OFP_ACTION_ALIGN 8
321 /* Define a few functions for working with instructions. */
322 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
323 static inline const struct STRUCT * OVS_UNUSED \
324 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
326 ovs_assert(inst->type == htons(ENUM)); \
327 return ALIGNED_CAST(struct STRUCT *, inst); \
330 static inline void OVS_UNUSED \
331 instruction_init_##ENUM(struct STRUCT *s) \
333 memset(s, 0, sizeof *s); \
334 s->type = htons(ENUM); \
335 s->len = htons(sizeof *s); \
338 static inline struct STRUCT * OVS_UNUSED \
339 instruction_put_##ENUM(struct ofpbuf *buf) \
341 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
342 instruction_init_##ENUM(s); \
348 static void ofpacts_update_instruction_actions(struct ofpbuf
*openflow
,
350 static void pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
);
352 static enum ofperr
ofpacts_verify(const struct ofpact
[], size_t ofpacts_len
,
353 uint32_t allowed_ovsinsts
,
354 enum ofpact_type outer_action
);
356 static void ofpact_put_set_field(struct ofpbuf
*openflow
, enum ofp_version
,
357 enum mf_field_id
, uint64_t value
);
359 static enum ofperr
ofpact_pull_raw(struct ofpbuf
*, enum ofp_version
,
360 enum ofp_raw_action_type
*, uint64_t *arg
);
361 static void *ofpact_put_raw(struct ofpbuf
*, enum ofp_version
,
362 enum ofp_raw_action_type
, uint64_t arg
);
364 static char *OVS_WARN_UNUSED_RESULT
ofpacts_parse(
365 char *str
, struct ofpbuf
*ofpacts
, enum ofputil_protocol
*usable_protocols
,
366 bool allow_instructions
, enum ofpact_type outer_action
);
367 static enum ofperr
ofpacts_pull_openflow_actions__(
368 struct ofpbuf
*openflow
, unsigned int actions_len
,
369 enum ofp_version version
, uint32_t allowed_ovsinsts
,
370 struct ofpbuf
*ofpacts
, enum ofpact_type outer_action
);
371 static char * OVS_WARN_UNUSED_RESULT
ofpacts_parse_copy(
372 const char *s_
, struct ofpbuf
*ofpacts
,
373 enum ofputil_protocol
*usable_protocols
,
374 bool allow_instructions
, enum ofpact_type outer_action
);
376 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
377 * nested ofpacts it returns the first one. */
379 ofpact_next_flattened(const struct ofpact
*ofpact
)
381 switch (ofpact
->type
) {
384 case OFPACT_CONTROLLER
:
386 case OFPACT_OUTPUT_REG
:
387 case OFPACT_OUTPUT_TRUNC
:
389 case OFPACT_SET_FIELD
:
390 case OFPACT_SET_VLAN_VID
:
391 case OFPACT_SET_VLAN_PCP
:
392 case OFPACT_STRIP_VLAN
:
393 case OFPACT_PUSH_VLAN
:
394 case OFPACT_SET_ETH_SRC
:
395 case OFPACT_SET_ETH_DST
:
396 case OFPACT_SET_IPV4_SRC
:
397 case OFPACT_SET_IPV4_DST
:
398 case OFPACT_SET_IP_DSCP
:
399 case OFPACT_SET_IP_ECN
:
400 case OFPACT_SET_IP_TTL
:
401 case OFPACT_SET_L4_SRC_PORT
:
402 case OFPACT_SET_L4_DST_PORT
:
403 case OFPACT_REG_MOVE
:
404 case OFPACT_STACK_PUSH
:
405 case OFPACT_STACK_POP
:
407 case OFPACT_SET_MPLS_LABEL
:
408 case OFPACT_SET_MPLS_TC
:
409 case OFPACT_SET_MPLS_TTL
:
410 case OFPACT_DEC_MPLS_TTL
:
411 case OFPACT_PUSH_MPLS
:
412 case OFPACT_POP_MPLS
:
413 case OFPACT_SET_TUNNEL
:
414 case OFPACT_SET_QUEUE
:
415 case OFPACT_POP_QUEUE
:
416 case OFPACT_FIN_TIMEOUT
:
417 case OFPACT_RESUBMIT
:
419 case OFPACT_CONJUNCTION
:
420 case OFPACT_MULTIPATH
:
424 case OFPACT_UNROLL_XLATE
:
425 case OFPACT_DEBUG_RECIRC
:
427 case OFPACT_CLEAR_ACTIONS
:
428 case OFPACT_WRITE_METADATA
:
429 case OFPACT_GOTO_TABLE
:
431 return ofpact_next(ofpact
);
434 return ofpact_get_CT(ofpact
)->actions
;
436 case OFPACT_WRITE_ACTIONS
:
437 return ofpact_get_WRITE_ACTIONS(ofpact
)->actions
;
443 /* Pull off existing actions or instructions. Used by nesting actions to keep
444 * ofpacts_parse() oblivious of actions nesting.
446 * Push the actions back on after nested parsing, e.g.:
448 * size_t ofs = ofpacts_pull(ofpacts);
449 * ...nested parsing...
450 * ofpbuf_push_uninit(ofpacts, ofs);
453 ofpacts_pull(struct ofpbuf
*ofpacts
)
458 ofpbuf_pull(ofpacts
, ofs
);
463 #include "ofp-actions.inc1"
465 /* Output actions. */
467 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
468 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
469 * number of bytes to send. A 'max_len' of zero means no bytes of the
470 * packet should be sent. */
471 struct ofp10_action_output
{
472 ovs_be16 type
; /* OFPAT10_OUTPUT. */
473 ovs_be16 len
; /* Length is 8. */
474 ovs_be16 port
; /* Output port. */
475 ovs_be16 max_len
; /* Max length to send to controller. */
477 OFP_ASSERT(sizeof(struct ofp10_action_output
) == 8);
479 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
480 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
481 * number of bytes to send. A 'max_len' of zero means no bytes of the
482 * packet should be sent.*/
483 struct ofp11_action_output
{
484 ovs_be16 type
; /* OFPAT11_OUTPUT. */
485 ovs_be16 len
; /* Length is 16. */
486 ovs_be32 port
; /* Output port. */
487 ovs_be16 max_len
; /* Max length to send to controller. */
488 uint8_t pad
[6]; /* Pad to 64 bits. */
490 OFP_ASSERT(sizeof(struct ofp11_action_output
) == 16);
493 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output
*oao
,
494 enum ofp_version ofp_version OVS_UNUSED
,
497 struct ofpact_output
*output
;
499 output
= ofpact_put_OUTPUT(out
);
500 output
->port
= u16_to_ofp(ntohs(oao
->port
));
501 output
->max_len
= ntohs(oao
->max_len
);
503 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
507 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output
*oao
,
508 enum ofp_version ofp_version OVS_UNUSED
,
511 struct ofpact_output
*output
;
514 output
= ofpact_put_OUTPUT(out
);
515 output
->max_len
= ntohs(oao
->max_len
);
517 error
= ofputil_port_from_ofp11(oao
->port
, &output
->port
);
522 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
526 encode_OUTPUT(const struct ofpact_output
*output
,
527 enum ofp_version ofp_version
, struct ofpbuf
*out
)
529 if (ofp_version
== OFP10_VERSION
) {
530 struct ofp10_action_output
*oao
;
532 oao
= put_OFPAT10_OUTPUT(out
);
533 oao
->port
= htons(ofp_to_u16(output
->port
));
534 oao
->max_len
= htons(output
->max_len
);
536 struct ofp11_action_output
*oao
;
538 oao
= put_OFPAT11_OUTPUT(out
);
539 oao
->port
= ofputil_port_to_ofp11(output
->port
);
540 oao
->max_len
= htons(output
->max_len
);
544 static char * OVS_WARN_UNUSED_RESULT
545 parse_truncate_subfield(struct ofpact_output_trunc
*output_trunc
,
549 char *arg
= CONST_CAST(char *, arg_
);
551 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
552 if (!strcmp(key
, "port")) {
553 if (!ofputil_port_from_string(value
, &output_trunc
->port
)) {
554 return xasprintf("output to unknown truncate port: %s",
557 if (ofp_to_u16(output_trunc
->port
) > ofp_to_u16(OFPP_MAX
)) {
558 if (output_trunc
->port
!= OFPP_LOCAL
&&
559 output_trunc
->port
!= OFPP_IN_PORT
)
560 return xasprintf("output to unsupported truncate port: %s",
563 } else if (!strcmp(key
, "max_len")) {
566 err
= str_to_u32(value
, &output_trunc
->max_len
);
571 return xasprintf("invalid key '%s' in output_trunc argument",
578 static char * OVS_WARN_UNUSED_RESULT
579 parse_OUTPUT(const char *arg
, struct ofpbuf
*ofpacts
,
580 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
582 if (strchr(arg
, '[')) {
583 struct ofpact_output_reg
*output_reg
;
585 output_reg
= ofpact_put_OUTPUT_REG(ofpacts
);
586 output_reg
->max_len
= UINT16_MAX
;
587 return mf_parse_subfield(&output_reg
->src
, arg
);
588 } else if (strstr(arg
, "port") && strstr(arg
, "max_len")) {
589 struct ofpact_output_trunc
*output_trunc
;
591 output_trunc
= ofpact_put_OUTPUT_TRUNC(ofpacts
);
592 return parse_truncate_subfield(output_trunc
, arg
);
594 struct ofpact_output
*output
;
596 output
= ofpact_put_OUTPUT(ofpacts
);
597 if (!ofputil_port_from_string(arg
, &output
->port
)) {
598 return xasprintf("%s: output to unknown port", arg
);
600 output
->max_len
= output
->port
== OFPP_CONTROLLER
? UINT16_MAX
: 0;
606 format_OUTPUT(const struct ofpact_output
*a
, struct ds
*s
)
608 if (ofp_to_u16(a
->port
) < ofp_to_u16(OFPP_MAX
)) {
609 ds_put_format(s
, "%soutput:%s%"PRIu16
,
610 colors
.special
, colors
.end
, a
->port
);
612 ofputil_format_port(a
->port
, s
);
613 if (a
->port
== OFPP_CONTROLLER
) {
614 ds_put_format(s
, ":%"PRIu16
, a
->max_len
);
622 decode_OFPAT_RAW_GROUP(uint32_t group_id
,
623 enum ofp_version ofp_version OVS_UNUSED
,
626 ofpact_put_GROUP(out
)->group_id
= group_id
;
631 encode_GROUP(const struct ofpact_group
*group
,
632 enum ofp_version ofp_version
, struct ofpbuf
*out
)
634 put_OFPAT_GROUP(out
, ofp_version
, group
->group_id
);
637 static char * OVS_WARN_UNUSED_RESULT
638 parse_GROUP(char *arg
, struct ofpbuf
*ofpacts
,
639 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
641 return str_to_u32(arg
, &ofpact_put_GROUP(ofpacts
)->group_id
);
645 format_GROUP(const struct ofpact_group
*a
, struct ds
*s
)
647 ds_put_format(s
, "%sgroup:%s%"PRIu32
,
648 colors
.special
, colors
.end
, a
->group_id
);
651 /* Action structure for NXAST_CONTROLLER.
653 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
654 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
657 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
659 * - 'controller_id': The ID of the controller connection to which the
660 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
661 * sent only to controllers that have the specified controller connection
662 * ID. See "struct nx_controller_id" for more information. */
663 struct nx_action_controller
{
664 ovs_be16 type
; /* OFPAT_VENDOR. */
665 ovs_be16 len
; /* Length is 16. */
666 ovs_be32 vendor
; /* NX_VENDOR_ID. */
667 ovs_be16 subtype
; /* NXAST_CONTROLLER. */
668 ovs_be16 max_len
; /* Maximum length to send to controller. */
669 ovs_be16 controller_id
; /* Controller ID to send packet-in. */
670 uint8_t reason
; /* enum ofp_packet_in_reason (OFPR_*). */
671 uint8_t zero
; /* Must be zero. */
673 OFP_ASSERT(sizeof(struct nx_action_controller
) == 16);
675 /* Properties for NXAST_CONTROLLER2.
677 * For more information on the effect of NXAC2PT_PAUSE, see the large comment
678 * on NXT_PACKET_IN2 in nicira-ext.h */
679 enum nx_action_controller2_prop_type
{
680 NXAC2PT_MAX_LEN
, /* ovs_be16 max bytes to send (default all). */
681 NXAC2PT_CONTROLLER_ID
, /* ovs_be16 dest controller ID (default 0). */
682 NXAC2PT_REASON
, /* uint8_t reason (OFPR_*), default 0. */
683 NXAC2PT_USERDATA
, /* Data to copy into NXPINT_USERDATA. */
684 NXAC2PT_PAUSE
, /* Flag to pause pipeline to resume later. */
687 /* Action structure for NXAST_CONTROLLER2.
689 * This replacement for NXAST_CONTROLLER makes it extensible via properties. */
690 struct nx_action_controller2
{
691 ovs_be16 type
; /* OFPAT_VENDOR. */
692 ovs_be16 len
; /* Length is 16 or more. */
693 ovs_be32 vendor
; /* NX_VENDOR_ID. */
694 ovs_be16 subtype
; /* NXAST_CONTROLLER2. */
695 uint8_t zeros
[6]; /* Must be zero. */
696 /* Followed by NXAC2PT_* properties. */
698 OFP_ASSERT(sizeof(struct nx_action_controller2
) == 16);
701 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller
*nac
,
702 enum ofp_version ofp_version OVS_UNUSED
,
705 struct ofpact_controller
*oc
;
707 oc
= ofpact_put_CONTROLLER(out
);
708 oc
->ofpact
.raw
= NXAST_RAW_CONTROLLER
;
709 oc
->max_len
= ntohs(nac
->max_len
);
710 oc
->controller_id
= ntohs(nac
->controller_id
);
711 oc
->reason
= nac
->reason
;
712 ofpact_finish_CONTROLLER(out
, &oc
);
718 decode_NXAST_RAW_CONTROLLER2(const struct nx_action_controller2
*nac2
,
719 enum ofp_version ofp_version OVS_UNUSED
,
722 if (!is_all_zeros(nac2
->zeros
, sizeof nac2
->zeros
)) {
723 return OFPERR_NXBRC_MUST_BE_ZERO
;
726 size_t start_ofs
= out
->size
;
727 struct ofpact_controller
*oc
= ofpact_put_CONTROLLER(out
);
728 oc
->ofpact
.raw
= NXAST_RAW_CONTROLLER2
;
729 oc
->max_len
= UINT16_MAX
;
730 oc
->reason
= OFPR_ACTION
;
732 struct ofpbuf properties
;
733 ofpbuf_use_const(&properties
, nac2
, ntohs(nac2
->len
));
734 ofpbuf_pull(&properties
, sizeof *nac2
);
736 while (properties
.size
> 0) {
737 struct ofpbuf payload
;
740 enum ofperr error
= ofpprop_pull(&properties
, &payload
, &type
);
746 case NXAC2PT_MAX_LEN
:
747 error
= ofpprop_parse_u16(&payload
, &oc
->max_len
);
750 case NXAC2PT_CONTROLLER_ID
:
751 error
= ofpprop_parse_u16(&payload
, &oc
->controller_id
);
754 case NXAC2PT_REASON
: {
756 error
= ofpprop_parse_u8(&payload
, &u8
);
761 case NXAC2PT_USERDATA
:
762 out
->size
= start_ofs
+ OFPACT_CONTROLLER_SIZE
;
763 ofpbuf_put(out
, payload
.msg
, ofpbuf_msgsize(&payload
));
764 oc
= ofpbuf_at_assert(out
, start_ofs
, sizeof *oc
);
765 oc
->userdata_len
= ofpbuf_msgsize(&payload
);
773 error
= OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type
);
781 ofpact_finish_CONTROLLER(out
, &oc
);
787 encode_CONTROLLER(const struct ofpact_controller
*controller
,
788 enum ofp_version ofp_version OVS_UNUSED
,
791 if (controller
->userdata_len
793 || controller
->ofpact
.raw
== NXAST_RAW_CONTROLLER2
) {
794 size_t start_ofs
= out
->size
;
795 put_NXAST_CONTROLLER2(out
);
796 if (controller
->max_len
!= UINT16_MAX
) {
797 ofpprop_put_u16(out
, NXAC2PT_MAX_LEN
, controller
->max_len
);
799 if (controller
->controller_id
!= 0) {
800 ofpprop_put_u16(out
, NXAC2PT_CONTROLLER_ID
,
801 controller
->controller_id
);
803 if (controller
->reason
!= OFPR_ACTION
) {
804 ofpprop_put_u8(out
, NXAC2PT_REASON
, controller
->reason
);
806 if (controller
->userdata_len
!= 0) {
807 ofpprop_put(out
, NXAC2PT_USERDATA
, controller
->userdata
,
808 controller
->userdata_len
);
810 if (controller
->pause
) {
811 ofpprop_put_flag(out
, NXAC2PT_PAUSE
);
813 pad_ofpat(out
, start_ofs
);
815 struct nx_action_controller
*nac
;
817 nac
= put_NXAST_CONTROLLER(out
);
818 nac
->max_len
= htons(controller
->max_len
);
819 nac
->controller_id
= htons(controller
->controller_id
);
820 nac
->reason
= controller
->reason
;
824 static char * OVS_WARN_UNUSED_RESULT
825 parse_CONTROLLER(char *arg
, struct ofpbuf
*ofpacts
,
826 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
828 enum ofp_packet_in_reason reason
= OFPR_ACTION
;
829 uint16_t controller_id
= 0;
830 uint16_t max_len
= UINT16_MAX
;
831 const char *userdata
= NULL
;
836 } else if (strspn(arg
, "0123456789") == strlen(arg
)) {
837 char *error
= str_to_u16(arg
, "max_len", &max_len
);
844 while (ofputil_parse_key_value(&arg
, &name
, &value
)) {
845 if (!strcmp(name
, "reason")) {
846 if (!ofputil_packet_in_reason_from_string(value
, &reason
)) {
847 return xasprintf("unknown reason \"%s\"", value
);
849 } else if (!strcmp(name
, "max_len")) {
850 char *error
= str_to_u16(value
, "max_len", &max_len
);
854 } else if (!strcmp(name
, "id")) {
855 char *error
= str_to_u16(value
, "id", &controller_id
);
859 } else if (!strcmp(name
, "userdata")) {
861 } else if (!strcmp(name
, "pause")) {
864 return xasprintf("unknown key \"%s\" parsing controller "
870 if (reason
== OFPR_ACTION
&& controller_id
== 0 && !userdata
&& !pause
) {
871 struct ofpact_output
*output
;
873 output
= ofpact_put_OUTPUT(ofpacts
);
874 output
->port
= OFPP_CONTROLLER
;
875 output
->max_len
= max_len
;
877 struct ofpact_controller
*controller
;
879 controller
= ofpact_put_CONTROLLER(ofpacts
);
880 controller
->max_len
= max_len
;
881 controller
->reason
= reason
;
882 controller
->controller_id
= controller_id
;
883 controller
->pause
= pause
;
886 size_t start_ofs
= ofpacts
->size
;
887 const char *end
= ofpbuf_put_hex(ofpacts
, userdata
, NULL
);
889 return xstrdup("bad hex digit in `controller' "
890 "action `userdata'");
892 size_t userdata_len
= ofpacts
->size
- start_ofs
;
893 controller
= ofpacts
->header
;
894 controller
->userdata_len
= userdata_len
;
896 ofpact_finish_CONTROLLER(ofpacts
, &controller
);
903 format_hex_arg(struct ds
*s
, const uint8_t *data
, size_t len
)
905 for (size_t i
= 0; i
< len
; i
++) {
909 ds_put_format(s
, "%02"PRIx8
, data
[i
]);
914 format_CONTROLLER(const struct ofpact_controller
*a
, struct ds
*s
)
916 if (a
->reason
== OFPR_ACTION
&& !a
->controller_id
&& !a
->userdata_len
918 ds_put_format(s
, "%sCONTROLLER:%s%"PRIu16
,
919 colors
.special
, colors
.end
, a
->max_len
);
921 enum ofp_packet_in_reason reason
= a
->reason
;
923 ds_put_format(s
, "%scontroller(%s", colors
.paren
, colors
.end
);
924 if (reason
!= OFPR_ACTION
) {
925 char reasonbuf
[OFPUTIL_PACKET_IN_REASON_BUFSIZE
];
927 ds_put_format(s
, "%sreason=%s%s,", colors
.param
, colors
.end
,
928 ofputil_packet_in_reason_to_string(
929 reason
, reasonbuf
, sizeof reasonbuf
));
931 if (a
->max_len
!= UINT16_MAX
) {
932 ds_put_format(s
, "%smax_len=%s%"PRIu16
",",
933 colors
.param
, colors
.end
, a
->max_len
);
935 if (a
->controller_id
!= 0) {
936 ds_put_format(s
, "%sid=%s%"PRIu16
",",
937 colors
.param
, colors
.end
, a
->controller_id
);
939 if (a
->userdata_len
) {
940 ds_put_format(s
, "%suserdata=%s", colors
.param
, colors
.end
);
941 format_hex_arg(s
, a
->userdata
, a
->userdata_len
);
945 ds_put_format(s
, "%spause%s,", colors
.value
, colors
.end
);
948 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
952 /* Enqueue action. */
953 struct ofp10_action_enqueue
{
954 ovs_be16 type
; /* OFPAT10_ENQUEUE. */
955 ovs_be16 len
; /* Len is 16. */
956 ovs_be16 port
; /* Port that queue belongs. Should
957 refer to a valid physical port
958 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
959 uint8_t pad
[6]; /* Pad for 64-bit alignment. */
960 ovs_be32 queue_id
; /* Where to enqueue the packets. */
962 OFP_ASSERT(sizeof(struct ofp10_action_enqueue
) == 16);
965 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue
*oae
,
966 enum ofp_version ofp_version OVS_UNUSED
,
969 struct ofpact_enqueue
*enqueue
;
971 enqueue
= ofpact_put_ENQUEUE(out
);
972 enqueue
->port
= u16_to_ofp(ntohs(oae
->port
));
973 enqueue
->queue
= ntohl(oae
->queue_id
);
974 if (ofp_to_u16(enqueue
->port
) >= ofp_to_u16(OFPP_MAX
)
975 && enqueue
->port
!= OFPP_IN_PORT
976 && enqueue
->port
!= OFPP_LOCAL
) {
977 return OFPERR_OFPBAC_BAD_OUT_PORT
;
983 encode_ENQUEUE(const struct ofpact_enqueue
*enqueue
,
984 enum ofp_version ofp_version
, struct ofpbuf
*out
)
986 if (ofp_version
== OFP10_VERSION
) {
987 struct ofp10_action_enqueue
*oae
;
989 oae
= put_OFPAT10_ENQUEUE(out
);
990 oae
->port
= htons(ofp_to_u16(enqueue
->port
));
991 oae
->queue_id
= htonl(enqueue
->queue
);
997 static char * OVS_WARN_UNUSED_RESULT
998 parse_ENQUEUE(char *arg
, struct ofpbuf
*ofpacts
,
999 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1002 char *port
= strtok_r(arg
, ":q,", &sp
);
1003 char *queue
= strtok_r(NULL
, "", &sp
);
1004 struct ofpact_enqueue
*enqueue
;
1006 if (port
== NULL
|| queue
== NULL
) {
1007 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
1008 "\"enqueue(PORT,QUEUE)\"");
1011 enqueue
= ofpact_put_ENQUEUE(ofpacts
);
1012 if (!ofputil_port_from_string(port
, &enqueue
->port
)) {
1013 return xasprintf("%s: enqueue to unknown port", port
);
1015 return str_to_u32(queue
, &enqueue
->queue
);
1019 format_ENQUEUE(const struct ofpact_enqueue
*a
, struct ds
*s
)
1021 ds_put_format(s
, "%senqueue:%s", colors
.param
, colors
.end
);
1022 ofputil_format_port(a
->port
, s
);
1023 ds_put_format(s
, ":%"PRIu32
, a
->queue
);
1026 /* Action structure for NXAST_OUTPUT_REG.
1028 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
1030 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
1031 * the NXAST_REG_LOAD action.
1033 * The acceptable nxm_header values for 'src' are the same as the acceptable
1034 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
1036 * The 'max_len' field indicates the number of bytes to send when the chosen
1037 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
1038 * field of OFPAT_OUTPUT.
1040 * The 'zero' field is required to be zeroed for forward compatibility. */
1041 struct nx_action_output_reg
{
1042 ovs_be16 type
; /* OFPAT_VENDOR. */
1043 ovs_be16 len
; /* 24. */
1044 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1045 ovs_be16 subtype
; /* NXAST_OUTPUT_REG. */
1047 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1048 ovs_be32 src
; /* Source. */
1050 ovs_be16 max_len
; /* Max length to send to controller. */
1052 uint8_t zero
[6]; /* Reserved, must be zero. */
1054 OFP_ASSERT(sizeof(struct nx_action_output_reg
) == 24);
1056 /* Action structure for NXAST_OUTPUT_REG2.
1058 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
1059 * experimenter OXM as 'src'.
1061 struct nx_action_output_reg2
{
1062 ovs_be16 type
; /* OFPAT_VENDOR. */
1063 ovs_be16 len
; /* 24. */
1064 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1065 ovs_be16 subtype
; /* NXAST_OUTPUT_REG2. */
1067 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1068 ovs_be16 max_len
; /* Max length to send to controller. */
1071 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1072 * - Enough 0-bytes to pad the action out to 24 bytes. */
1075 OFP_ASSERT(sizeof(struct nx_action_output_reg2
) == 24);
1078 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg
*naor
,
1079 enum ofp_version ofp_version OVS_UNUSED
,
1082 struct ofpact_output_reg
*output_reg
;
1084 if (!is_all_zeros(naor
->zero
, sizeof naor
->zero
)) {
1085 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1088 output_reg
= ofpact_put_OUTPUT_REG(out
);
1089 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG
;
1090 output_reg
->src
.field
= mf_from_nxm_header(ntohl(naor
->src
));
1091 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
1092 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
1093 output_reg
->max_len
= ntohs(naor
->max_len
);
1095 return mf_check_src(&output_reg
->src
, NULL
);
1099 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2
*naor
,
1100 enum ofp_version ofp_version OVS_UNUSED
,
1103 struct ofpact_output_reg
*output_reg
;
1104 output_reg
= ofpact_put_OUTPUT_REG(out
);
1105 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG2
;
1106 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
1107 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
1108 output_reg
->max_len
= ntohs(naor
->max_len
);
1110 struct ofpbuf b
= ofpbuf_const_initializer(naor
, ntohs(naor
->len
));
1111 ofpbuf_pull(&b
, OBJECT_OFFSETOF(naor
, pad
));
1113 enum ofperr error
= nx_pull_header(&b
, &output_reg
->src
.field
, NULL
);
1117 if (!is_all_zeros(b
.data
, b
.size
)) {
1118 return OFPERR_NXBRC_MUST_BE_ZERO
;
1121 return mf_check_src(&output_reg
->src
, NULL
);
1125 encode_OUTPUT_REG(const struct ofpact_output_reg
*output_reg
,
1126 enum ofp_version ofp_version OVS_UNUSED
,
1129 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1130 * cannot be encoded in the older form, encode it as
1131 * NXAST_RAW_OUTPUT_REG2. */
1132 if (output_reg
->ofpact
.raw
== NXAST_RAW_OUTPUT_REG2
1133 || !mf_nxm_header(output_reg
->src
.field
->id
)) {
1134 struct nx_action_output_reg2
*naor
= put_NXAST_OUTPUT_REG2(out
);
1135 size_t size
= out
->size
;
1137 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
1138 output_reg
->src
.n_bits
);
1139 naor
->max_len
= htons(output_reg
->max_len
);
1141 out
->size
= size
- sizeof naor
->pad
;
1142 nx_put_header(out
, output_reg
->src
.field
->id
, 0, false);
1145 struct nx_action_output_reg
*naor
= put_NXAST_OUTPUT_REG(out
);
1147 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
1148 output_reg
->src
.n_bits
);
1149 naor
->src
= htonl(mf_nxm_header(output_reg
->src
.field
->id
));
1150 naor
->max_len
= htons(output_reg
->max_len
);
1154 static char * OVS_WARN_UNUSED_RESULT
1155 parse_OUTPUT_REG(const char *arg
, struct ofpbuf
*ofpacts
,
1156 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1158 return parse_OUTPUT(arg
, ofpacts
, usable_protocols
);
1162 format_OUTPUT_REG(const struct ofpact_output_reg
*a
, struct ds
*s
)
1164 ds_put_format(s
, "%soutput:%s", colors
.special
, colors
.end
);
1165 mf_format_subfield(&a
->src
, s
);
1168 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1170 * The bundle actions choose a slave from a supplied list of options.
1171 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
1172 * selection to a register.
1174 * The list of possible slaves follows the nx_action_bundle structure. The size
1175 * of each slave is governed by its type as indicated by the 'slave_type'
1176 * parameter. The list of slaves should be padded at its end with zeros to make
1177 * the total length of the action a multiple of 8.
1179 * Switches infer from the 'slave_type' parameter the size of each slave. All
1180 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
1181 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
1182 * 2 byte width. Switches should reject actions which indicate unknown or
1183 * unsupported slave types.
1185 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1186 * slave. If the switch does not support the specified 'algorithm' parameter,
1187 * it should reject the action.
1189 * Several algorithms take into account liveness when selecting slaves. The
1190 * liveness of a slave is implementation defined (with one exception), but will
1191 * generally take into account things like its carrier status and the results
1192 * of any link monitoring protocols which happen to be running on it. In order
1193 * to give controllers a place-holder value, the OFPP_NONE port is always
1196 * Some slave selection strategies require the use of a hash function, in which
1197 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
1198 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
1199 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
1200 * 'basis' parameter is used as a universal hash parameter. Different values
1201 * of 'basis' yield different hash results.
1203 * The 'zero' parameter at the end of the action structure is reserved for
1204 * future use. Switches are required to reject actions which have nonzero
1205 * bytes in the 'zero' field.
1207 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
1208 * should reject actions which have nonzero bytes in either of these fields.
1210 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
1211 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
1212 * similar to those for the NXAST_REG_LOAD action. */
1213 struct nx_action_bundle
{
1214 ovs_be16 type
; /* OFPAT_VENDOR. */
1215 ovs_be16 len
; /* Length including slaves. */
1216 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1217 ovs_be16 subtype
; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1219 /* Slave choice algorithm to apply to hash value. */
1220 ovs_be16 algorithm
; /* One of NX_BD_ALG_*. */
1222 /* What fields to hash and how. */
1223 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
1224 ovs_be16 basis
; /* Universal hash parameter. */
1226 ovs_be32 slave_type
; /* NXM_OF_IN_PORT. */
1227 ovs_be16 n_slaves
; /* Number of slaves. */
1229 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1230 ovs_be32 dst
; /* Destination. */
1232 uint8_t zero
[4]; /* Reserved. Must be zero. */
1234 OFP_ASSERT(sizeof(struct nx_action_bundle
) == 32);
1237 decode_bundle(bool load
, const struct nx_action_bundle
*nab
,
1238 struct ofpbuf
*ofpacts
)
1240 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1241 struct ofpact_bundle
*bundle
;
1242 uint32_t slave_type
;
1243 size_t slaves_size
, i
;
1246 bundle
= ofpact_put_BUNDLE(ofpacts
);
1248 bundle
->n_slaves
= ntohs(nab
->n_slaves
);
1249 bundle
->basis
= ntohs(nab
->basis
);
1250 bundle
->fields
= ntohs(nab
->fields
);
1251 bundle
->algorithm
= ntohs(nab
->algorithm
);
1252 slave_type
= ntohl(nab
->slave_type
);
1253 slaves_size
= ntohs(nab
->len
) - sizeof *nab
;
1255 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1256 if (!flow_hash_fields_valid(bundle
->fields
)) {
1257 VLOG_WARN_RL(&rl
, "unsupported fields %d", (int) bundle
->fields
);
1258 } else if (bundle
->n_slaves
> BUNDLE_MAX_SLAVES
) {
1259 VLOG_WARN_RL(&rl
, "too many slaves");
1260 } else if (bundle
->algorithm
!= NX_BD_ALG_HRW
1261 && bundle
->algorithm
!= NX_BD_ALG_ACTIVE_BACKUP
) {
1262 VLOG_WARN_RL(&rl
, "unsupported algorithm %d", (int) bundle
->algorithm
);
1263 } else if (slave_type
!= mf_nxm_header(MFF_IN_PORT
)) {
1264 VLOG_WARN_RL(&rl
, "unsupported slave type %"PRIu16
, slave_type
);
1269 if (!is_all_zeros(nab
->zero
, sizeof nab
->zero
)) {
1270 VLOG_WARN_RL(&rl
, "reserved field is nonzero");
1271 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1275 bundle
->dst
.field
= mf_from_nxm_header(ntohl(nab
->dst
));
1276 bundle
->dst
.ofs
= nxm_decode_ofs(nab
->ofs_nbits
);
1277 bundle
->dst
.n_bits
= nxm_decode_n_bits(nab
->ofs_nbits
);
1279 if (bundle
->dst
.n_bits
< 16) {
1280 VLOG_WARN_RL(&rl
, "bundle_load action requires at least 16 bit "
1282 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1285 if (nab
->ofs_nbits
|| nab
->dst
) {
1286 VLOG_WARN_RL(&rl
, "bundle action has nonzero reserved fields");
1287 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1291 if (slaves_size
< bundle
->n_slaves
* sizeof(ovs_be16
)) {
1292 VLOG_WARN_RL(&rl
, "Nicira action %s only has %"PRIuSIZE
" bytes "
1293 "allocated for slaves. %"PRIuSIZE
" bytes are required "
1294 "for %"PRIu16
" slaves.",
1295 load
? "bundle_load" : "bundle", slaves_size
,
1296 bundle
->n_slaves
* sizeof(ovs_be16
), bundle
->n_slaves
);
1297 error
= OFPERR_OFPBAC_BAD_LEN
;
1300 for (i
= 0; i
< bundle
->n_slaves
; i
++) {
1301 ofp_port_t ofp_port
= u16_to_ofp(ntohs(((ovs_be16
*)(nab
+ 1))[i
]));
1302 ofpbuf_put(ofpacts
, &ofp_port
, sizeof ofp_port
);
1303 bundle
= ofpacts
->header
;
1306 ofpact_finish_BUNDLE(ofpacts
, &bundle
);
1308 error
= bundle_check(bundle
, OFPP_MAX
, NULL
);
1314 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle
*nab
,
1315 enum ofp_version ofp_version OVS_UNUSED
,
1318 return decode_bundle(false, nab
, out
);
1322 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle
*nab
,
1323 enum ofp_version ofp_version OVS_UNUSED
,
1326 return decode_bundle(true, nab
, out
);
1330 encode_BUNDLE(const struct ofpact_bundle
*bundle
,
1331 enum ofp_version ofp_version OVS_UNUSED
,
1334 int slaves_len
= ROUND_UP(2 * bundle
->n_slaves
, OFP_ACTION_ALIGN
);
1335 struct nx_action_bundle
*nab
;
1339 nab
= (bundle
->dst
.field
1340 ? put_NXAST_BUNDLE_LOAD(out
)
1341 : put_NXAST_BUNDLE(out
));
1342 nab
->len
= htons(ntohs(nab
->len
) + slaves_len
);
1343 nab
->algorithm
= htons(bundle
->algorithm
);
1344 nab
->fields
= htons(bundle
->fields
);
1345 nab
->basis
= htons(bundle
->basis
);
1346 nab
->slave_type
= htonl(mf_nxm_header(MFF_IN_PORT
));
1347 nab
->n_slaves
= htons(bundle
->n_slaves
);
1348 if (bundle
->dst
.field
) {
1349 nab
->ofs_nbits
= nxm_encode_ofs_nbits(bundle
->dst
.ofs
,
1350 bundle
->dst
.n_bits
);
1351 nab
->dst
= htonl(mf_nxm_header(bundle
->dst
.field
->id
));
1354 slaves
= ofpbuf_put_zeros(out
, slaves_len
);
1355 for (i
= 0; i
< bundle
->n_slaves
; i
++) {
1356 slaves
[i
] = htons(ofp_to_u16(bundle
->slaves
[i
]));
1360 static char * OVS_WARN_UNUSED_RESULT
1361 parse_BUNDLE(const char *arg
, struct ofpbuf
*ofpacts
,
1362 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1364 return bundle_parse(arg
, ofpacts
);
1367 static char * OVS_WARN_UNUSED_RESULT
1368 parse_bundle_load(const char *arg
, struct ofpbuf
*ofpacts
)
1370 return bundle_parse_load(arg
, ofpacts
);
1374 format_BUNDLE(const struct ofpact_bundle
*a
, struct ds
*s
)
1376 bundle_format(a
, s
);
1379 /* Set VLAN actions. */
1382 decode_set_vlan_vid(uint16_t vid
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1385 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1387 struct ofpact_vlan_vid
*vlan_vid
= ofpact_put_SET_VLAN_VID(out
);
1388 vlan_vid
->vlan_vid
= vid
;
1389 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1395 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid
,
1396 enum ofp_version ofp_version OVS_UNUSED
,
1399 return decode_set_vlan_vid(vid
, true, out
);
1403 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid
,
1404 enum ofp_version ofp_version OVS_UNUSED
,
1407 return decode_set_vlan_vid(vid
, false, out
);
1411 encode_SET_VLAN_VID(const struct ofpact_vlan_vid
*vlan_vid
,
1412 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1414 uint16_t vid
= vlan_vid
->vlan_vid
;
1416 /* Push a VLAN tag, if none is present and this form of the action calls
1417 * for such a feature. */
1418 if (ofp_version
> OFP10_VERSION
1419 && vlan_vid
->push_vlan_if_needed
1420 && !vlan_vid
->flow_has_vlan
) {
1421 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1424 if (ofp_version
== OFP10_VERSION
) {
1425 put_OFPAT10_SET_VLAN_VID(out
, vid
);
1426 } else if (ofp_version
== OFP11_VERSION
) {
1427 put_OFPAT11_SET_VLAN_VID(out
, vid
);
1429 ofpact_put_set_field(out
, ofp_version
,
1430 MFF_VLAN_VID
, vid
| OFPVID12_PRESENT
);
1434 static char * OVS_WARN_UNUSED_RESULT
1435 parse_set_vlan_vid(char *arg
, struct ofpbuf
*ofpacts
, bool push_vlan_if_needed
)
1437 struct ofpact_vlan_vid
*vlan_vid
;
1441 error
= str_to_u16(arg
, "VLAN VID", &vid
);
1446 if (vid
& ~VLAN_VID_MASK
) {
1447 return xasprintf("%s: not a valid VLAN VID", arg
);
1449 vlan_vid
= ofpact_put_SET_VLAN_VID(ofpacts
);
1450 vlan_vid
->vlan_vid
= vid
;
1451 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1455 static char * OVS_WARN_UNUSED_RESULT
1456 parse_SET_VLAN_VID(char *arg
, struct ofpbuf
*ofpacts
,
1457 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1459 return parse_set_vlan_vid(arg
, ofpacts
, false);
1463 format_SET_VLAN_VID(const struct ofpact_vlan_vid
*a
, struct ds
*s
)
1465 ds_put_format(s
, "%s%s:%s%"PRIu16
, colors
.param
,
1466 a
->push_vlan_if_needed
? "mod_vlan_vid" : "set_vlan_vid",
1467 colors
.end
, a
->vlan_vid
);
1470 /* Set PCP actions. */
1473 decode_set_vlan_pcp(uint8_t pcp
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1476 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1478 struct ofpact_vlan_pcp
*vlan_pcp
= ofpact_put_SET_VLAN_PCP(out
);
1479 vlan_pcp
->vlan_pcp
= pcp
;
1480 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1486 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp
,
1487 enum ofp_version ofp_version OVS_UNUSED
,
1490 return decode_set_vlan_pcp(pcp
, true, out
);
1494 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp
,
1495 enum ofp_version ofp_version OVS_UNUSED
,
1498 return decode_set_vlan_pcp(pcp
, false, out
);
1502 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*vlan_pcp
,
1503 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1505 uint8_t pcp
= vlan_pcp
->vlan_pcp
;
1507 /* Push a VLAN tag, if none is present and this form of the action calls
1508 * for such a feature. */
1509 if (ofp_version
> OFP10_VERSION
1510 && vlan_pcp
->push_vlan_if_needed
1511 && !vlan_pcp
->flow_has_vlan
) {
1512 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1515 if (ofp_version
== OFP10_VERSION
) {
1516 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
1517 } else if (ofp_version
== OFP11_VERSION
) {
1518 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
1520 ofpact_put_set_field(out
, ofp_version
, MFF_VLAN_PCP
, pcp
);
1524 static char * OVS_WARN_UNUSED_RESULT
1525 parse_set_vlan_pcp(char *arg
, struct ofpbuf
*ofpacts
, bool push_vlan_if_needed
)
1527 struct ofpact_vlan_pcp
*vlan_pcp
;
1531 error
= str_to_u8(arg
, "VLAN PCP", &pcp
);
1537 return xasprintf("%s: not a valid VLAN PCP", arg
);
1539 vlan_pcp
= ofpact_put_SET_VLAN_PCP(ofpacts
);
1540 vlan_pcp
->vlan_pcp
= pcp
;
1541 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1545 static char * OVS_WARN_UNUSED_RESULT
1546 parse_SET_VLAN_PCP(char *arg
, struct ofpbuf
*ofpacts
,
1547 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1549 return parse_set_vlan_pcp(arg
, ofpacts
, false);
1553 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*a
, struct ds
*s
)
1555 ds_put_format(s
, "%s%s:%s%"PRIu8
, colors
.param
,
1556 a
->push_vlan_if_needed
? "mod_vlan_pcp" : "set_vlan_pcp",
1557 colors
.end
, a
->vlan_pcp
);
1560 /* Strip VLAN actions. */
1563 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf
*out
)
1565 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1570 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf
*out
)
1572 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1577 encode_STRIP_VLAN(const struct ofpact_null
*null OVS_UNUSED
,
1578 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1580 if (ofp_version
== OFP10_VERSION
) {
1581 put_OFPAT10_STRIP_VLAN(out
);
1583 put_OFPAT11_POP_VLAN(out
);
1587 static char * OVS_WARN_UNUSED_RESULT
1588 parse_STRIP_VLAN(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
1589 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1591 ofpact_put_STRIP_VLAN(ofpacts
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1595 static char * OVS_WARN_UNUSED_RESULT
1596 parse_pop_vlan(struct ofpbuf
*ofpacts
)
1598 ofpact_put_STRIP_VLAN(ofpacts
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1603 format_STRIP_VLAN(const struct ofpact_null
*a
, struct ds
*s
)
1605 ds_put_format(s
, (a
->ofpact
.raw
== OFPAT_RAW11_POP_VLAN
1607 : "%sstrip_vlan%s"),
1608 colors
.value
, colors
.end
);
1611 /* Push VLAN action. */
1614 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type
,
1615 enum ofp_version ofp_version OVS_UNUSED
,
1618 if (eth_type
!= htons(ETH_TYPE_VLAN_8021Q
)) {
1619 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1620 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1622 ofpact_put_PUSH_VLAN(out
);
1627 encode_PUSH_VLAN(const struct ofpact_null
*null OVS_UNUSED
,
1628 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1630 if (ofp_version
== OFP10_VERSION
) {
1631 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1632 * follow this action. */
1634 /* XXX ETH_TYPE_VLAN_8021AD case */
1635 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1639 static char * OVS_WARN_UNUSED_RESULT
1640 parse_PUSH_VLAN(char *arg
, struct ofpbuf
*ofpacts
,
1641 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1646 *usable_protocols
&= OFPUTIL_P_OF11_UP
;
1647 error
= str_to_u16(arg
, "ethertype", ðertype
);
1652 if (ethertype
!= ETH_TYPE_VLAN_8021Q
) {
1653 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1654 return xasprintf("%s: not a valid VLAN ethertype", arg
);
1657 ofpact_put_PUSH_VLAN(ofpacts
);
1662 format_PUSH_VLAN(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
1664 /* XXX 802.1AD case*/
1665 ds_put_format(s
, "%spush_vlan:%s%#"PRIx16
,
1666 colors
.param
, colors
.end
, ETH_TYPE_VLAN_8021Q
);
1669 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1670 struct ofp_action_dl_addr
{
1671 ovs_be16 type
; /* Type. */
1672 ovs_be16 len
; /* Length is 16. */
1673 struct eth_addr dl_addr
; /* Ethernet address. */
1676 OFP_ASSERT(sizeof(struct ofp_action_dl_addr
) == 16);
1679 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr
*a
,
1680 enum ofp_version ofp_version OVS_UNUSED
,
1683 ofpact_put_SET_ETH_SRC(out
)->mac
= a
->dl_addr
;
1688 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr
*a
,
1689 enum ofp_version ofp_version OVS_UNUSED
,
1692 ofpact_put_SET_ETH_DST(out
)->mac
= a
->dl_addr
;
1697 encode_SET_ETH_addr(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1698 enum ofp_raw_action_type raw
, enum mf_field_id field
,
1701 if (ofp_version
< OFP12_VERSION
) {
1702 struct ofp_action_dl_addr
*oada
;
1704 oada
= ofpact_put_raw(out
, ofp_version
, raw
, 0);
1705 oada
->dl_addr
= mac
->mac
;
1707 ofpact_put_set_field(out
, ofp_version
, field
,
1708 eth_addr_to_uint64(mac
->mac
));
1713 encode_SET_ETH_SRC(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1716 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_SRC
, MFF_ETH_SRC
,
1722 encode_SET_ETH_DST(const struct ofpact_mac
*mac
,
1723 enum ofp_version ofp_version
,
1726 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_DST
, MFF_ETH_DST
,
1731 static char * OVS_WARN_UNUSED_RESULT
1732 parse_SET_ETH_SRC(char *arg
, struct ofpbuf
*ofpacts
,
1733 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1735 return str_to_mac(arg
, &ofpact_put_SET_ETH_SRC(ofpacts
)->mac
);
1738 static char * OVS_WARN_UNUSED_RESULT
1739 parse_SET_ETH_DST(char *arg
, struct ofpbuf
*ofpacts
,
1740 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1742 return str_to_mac(arg
, &ofpact_put_SET_ETH_DST(ofpacts
)->mac
);
1746 format_SET_ETH_SRC(const struct ofpact_mac
*a
, struct ds
*s
)
1748 ds_put_format(s
, "%smod_dl_src:%s"ETH_ADDR_FMT
,
1749 colors
.param
, colors
.end
, ETH_ADDR_ARGS(a
->mac
));
1753 format_SET_ETH_DST(const struct ofpact_mac
*a
, struct ds
*s
)
1755 ds_put_format(s
, "%smod_dl_dst:%s"ETH_ADDR_FMT
,
1756 colors
.param
, colors
.end
, ETH_ADDR_ARGS(a
->mac
));
1759 /* Set IPv4 address actions. */
1762 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4
,
1763 enum ofp_version ofp_version OVS_UNUSED
,
1766 ofpact_put_SET_IPV4_SRC(out
)->ipv4
= ipv4
;
1771 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4
,
1772 enum ofp_version ofp_version OVS_UNUSED
,
1775 ofpact_put_SET_IPV4_DST(out
)->ipv4
= ipv4
;
1780 encode_SET_IPV4_addr(const struct ofpact_ipv4
*ipv4
,
1781 enum ofp_version ofp_version
,
1782 enum ofp_raw_action_type raw
, enum mf_field_id field
,
1785 ovs_be32 addr
= ipv4
->ipv4
;
1786 if (ofp_version
< OFP12_VERSION
) {
1787 ofpact_put_raw(out
, ofp_version
, raw
, ntohl(addr
));
1789 ofpact_put_set_field(out
, ofp_version
, field
, ntohl(addr
));
1794 encode_SET_IPV4_SRC(const struct ofpact_ipv4
*ipv4
,
1795 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1797 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_SRC
, MFF_IPV4_SRC
,
1802 encode_SET_IPV4_DST(const struct ofpact_ipv4
*ipv4
,
1803 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1805 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_DST
, MFF_IPV4_DST
,
1809 static char * OVS_WARN_UNUSED_RESULT
1810 parse_SET_IPV4_SRC(char *arg
, struct ofpbuf
*ofpacts
,
1811 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1813 return str_to_ip(arg
, &ofpact_put_SET_IPV4_SRC(ofpacts
)->ipv4
);
1816 static char * OVS_WARN_UNUSED_RESULT
1817 parse_SET_IPV4_DST(char *arg
, struct ofpbuf
*ofpacts
,
1818 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1820 return str_to_ip(arg
, &ofpact_put_SET_IPV4_DST(ofpacts
)->ipv4
);
1824 format_SET_IPV4_SRC(const struct ofpact_ipv4
*a
, struct ds
*s
)
1826 ds_put_format(s
, "%smod_nw_src:%s"IP_FMT
,
1827 colors
.param
, colors
.end
, IP_ARGS(a
->ipv4
));
1831 format_SET_IPV4_DST(const struct ofpact_ipv4
*a
, struct ds
*s
)
1833 ds_put_format(s
, "%smod_nw_dst:%s"IP_FMT
,
1834 colors
.param
, colors
.end
, IP_ARGS(a
->ipv4
));
1837 /* Set IPv4/v6 TOS actions. */
1840 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp
,
1841 enum ofp_version ofp_version OVS_UNUSED
,
1844 if (dscp
& ~IP_DSCP_MASK
) {
1845 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1847 ofpact_put_SET_IP_DSCP(out
)->dscp
= dscp
;
1853 encode_SET_IP_DSCP(const struct ofpact_dscp
*dscp
,
1854 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1856 if (ofp_version
< OFP12_VERSION
) {
1857 put_OFPAT_SET_NW_TOS(out
, ofp_version
, dscp
->dscp
);
1859 ofpact_put_set_field(out
, ofp_version
,
1860 MFF_IP_DSCP_SHIFTED
, dscp
->dscp
>> 2);
1864 static char * OVS_WARN_UNUSED_RESULT
1865 parse_SET_IP_DSCP(char *arg
, struct ofpbuf
*ofpacts
,
1866 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1871 error
= str_to_u8(arg
, "TOS", &tos
);
1876 if (tos
& ~IP_DSCP_MASK
) {
1877 return xasprintf("%s: not a valid TOS", arg
);
1879 ofpact_put_SET_IP_DSCP(ofpacts
)->dscp
= tos
;
1884 format_SET_IP_DSCP(const struct ofpact_dscp
*a
, struct ds
*s
)
1886 ds_put_format(s
, "%smod_nw_tos:%s%d", colors
.param
, colors
.end
, a
->dscp
);
1889 /* Set IPv4/v6 ECN actions. */
1892 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn
,
1893 enum ofp_version ofp_version OVS_UNUSED
,
1896 if (ecn
& ~IP_ECN_MASK
) {
1897 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1899 ofpact_put_SET_IP_ECN(out
)->ecn
= ecn
;
1905 encode_SET_IP_ECN(const struct ofpact_ecn
*ip_ecn
,
1906 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1908 uint8_t ecn
= ip_ecn
->ecn
;
1909 if (ofp_version
== OFP10_VERSION
) {
1911 } else if (ofp_version
== OFP11_VERSION
) {
1912 put_OFPAT11_SET_NW_ECN(out
, ecn
);
1914 ofpact_put_set_field(out
, ofp_version
, MFF_IP_ECN
, ecn
);
1918 static char * OVS_WARN_UNUSED_RESULT
1919 parse_SET_IP_ECN(char *arg
, struct ofpbuf
*ofpacts
,
1920 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1925 error
= str_to_u8(arg
, "ECN", &ecn
);
1930 if (ecn
& ~IP_ECN_MASK
) {
1931 return xasprintf("%s: not a valid ECN", arg
);
1933 ofpact_put_SET_IP_ECN(ofpacts
)->ecn
= ecn
;
1938 format_SET_IP_ECN(const struct ofpact_ecn
*a
, struct ds
*s
)
1940 ds_put_format(s
, "%smod_nw_ecn:%s%d",
1941 colors
.param
, colors
.end
, a
->ecn
);
1944 /* Set IPv4/v6 TTL actions. */
1947 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl
,
1948 enum ofp_version ofp_version OVS_UNUSED
,
1951 ofpact_put_SET_IP_TTL(out
)->ttl
= ttl
;
1956 encode_SET_IP_TTL(const struct ofpact_ip_ttl
*ttl
,
1957 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1959 if (ofp_version
>= OFP11_VERSION
) {
1960 put_OFPAT11_SET_NW_TTL(out
, ttl
->ttl
);
1966 static char * OVS_WARN_UNUSED_RESULT
1967 parse_SET_IP_TTL(char *arg
, struct ofpbuf
*ofpacts
,
1968 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1973 error
= str_to_u8(arg
, "TTL", &ttl
);
1978 ofpact_put_SET_IP_TTL(ofpacts
)->ttl
= ttl
;
1983 format_SET_IP_TTL(const struct ofpact_ip_ttl
*a
, struct ds
*s
)
1985 ds_put_format(s
, "%smod_nw_ttl:%s%d", colors
.param
, colors
.end
, a
->ttl
);
1988 /* Set TCP/UDP/SCTP port actions. */
1991 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port
,
1992 enum ofp_version ofp_version OVS_UNUSED
,
1995 ofpact_put_SET_L4_SRC_PORT(out
)->port
= ntohs(port
);
2000 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port
,
2001 enum ofp_version ofp_version OVS_UNUSED
,
2004 ofpact_put_SET_L4_DST_PORT(out
)->port
= ntohs(port
);
2009 encode_SET_L4_port(const struct ofpact_l4_port
*l4_port
,
2010 enum ofp_version ofp_version
, enum ofp_raw_action_type raw
,
2011 enum mf_field_id field
, struct ofpbuf
*out
)
2013 uint16_t port
= l4_port
->port
;
2015 if (ofp_version
>= OFP12_VERSION
&& field
!= MFF_N_IDS
) {
2016 ofpact_put_set_field(out
, ofp_version
, field
, port
);
2018 ofpact_put_raw(out
, ofp_version
, raw
, port
);
2023 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port
*l4_port
,
2024 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2026 uint8_t proto
= l4_port
->flow_ip_proto
;
2027 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_SRC
2028 : proto
== IPPROTO_UDP
? MFF_UDP_SRC
2029 : proto
== IPPROTO_SCTP
? MFF_SCTP_SRC
2032 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_SRC
, field
, out
);
2036 encode_SET_L4_DST_PORT(const struct ofpact_l4_port
*l4_port
,
2037 enum ofp_version ofp_version
,
2040 uint8_t proto
= l4_port
->flow_ip_proto
;
2041 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_DST
2042 : proto
== IPPROTO_UDP
? MFF_UDP_DST
2043 : proto
== IPPROTO_SCTP
? MFF_SCTP_DST
2046 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_DST
, field
, out
);
2049 static char * OVS_WARN_UNUSED_RESULT
2050 parse_SET_L4_SRC_PORT(char *arg
, struct ofpbuf
*ofpacts
,
2051 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2053 return str_to_u16(arg
, "source port",
2054 &ofpact_put_SET_L4_SRC_PORT(ofpacts
)->port
);
2057 static char * OVS_WARN_UNUSED_RESULT
2058 parse_SET_L4_DST_PORT(char *arg
, struct ofpbuf
*ofpacts
,
2059 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2061 return str_to_u16(arg
, "destination port",
2062 &ofpact_put_SET_L4_DST_PORT(ofpacts
)->port
);
2066 format_SET_L4_SRC_PORT(const struct ofpact_l4_port
*a
, struct ds
*s
)
2068 ds_put_format(s
, "%smod_tp_src:%s%d", colors
.param
, colors
.end
, a
->port
);
2072 format_SET_L4_DST_PORT(const struct ofpact_l4_port
*a
, struct ds
*s
)
2074 ds_put_format(s
, "%smod_tp_dst:%s%d", colors
.param
, colors
.end
, a
->port
);
2077 /* Action structure for OFPAT_COPY_FIELD. */
2078 struct ofp15_action_copy_field
{
2079 ovs_be16 type
; /* OFPAT_COPY_FIELD. */
2080 ovs_be16 len
; /* Length is padded to 64 bits. */
2081 ovs_be16 n_bits
; /* Number of bits to copy. */
2082 ovs_be16 src_offset
; /* Starting bit offset in source. */
2083 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
2086 * - OXM header for source field.
2087 * - OXM header for destination field.
2088 * - Padding with 0-bytes to a multiple of 8 bytes.
2089 * The "pad2" member is the beginning of the above. */
2092 OFP_ASSERT(sizeof(struct ofp15_action_copy_field
) == 16);
2094 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
2095 struct onf_action_copy_field
{
2096 ovs_be16 type
; /* OFPAT_EXPERIMENTER. */
2097 ovs_be16 len
; /* Length is padded to 64 bits. */
2098 ovs_be32 experimenter
; /* ONF_VENDOR_ID. */
2099 ovs_be16 exp_type
; /* 3200. */
2100 uint8_t pad
[2]; /* Not used. */
2101 ovs_be16 n_bits
; /* Number of bits to copy. */
2102 ovs_be16 src_offset
; /* Starting bit offset in source. */
2103 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
2104 uint8_t pad2
[2]; /* Not used. */
2106 * - OXM header for source field.
2107 * - OXM header for destination field.
2108 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2109 * The "pad3" member is the beginning of the above. */
2110 uint8_t pad3
[4]; /* Not used. */
2112 OFP_ASSERT(sizeof(struct onf_action_copy_field
) == 24);
2114 /* Action structure for NXAST_REG_MOVE.
2116 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2117 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2118 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
2119 * the next most significant bit, and so on.
2121 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
2122 * sense to use nxm_hasmask=1 because the action does not do any kind of
2123 * matching; it uses the actual value of a field.)
2125 * The following nxm_header values are potentially acceptable as 'src':
2140 * - NXM_OF_ICMP_TYPE
2141 * - NXM_OF_ICMP_CODE
2148 * - NXM_NX_ICMPV6_TYPE
2149 * - NXM_NX_ICMPV6_CODE
2152 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2154 * - NXM_NX_TUN_IPV4_SRC
2155 * - NXM_NX_TUN_IPV4_DST
2157 * The following nxm_header values are potentially acceptable as 'dst':
2168 * - NXM_OF_ICMP_TYPE
2169 * - NXM_OF_ICMP_CODE
2170 * - NXM_NX_ICMPV6_TYPE
2171 * - NXM_NX_ICMPV6_CODE
2177 * Modifying any of the above fields changes the corresponding packet
2182 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2186 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
2187 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
2188 * header (if any), ignoring the other bits. Setting a value with CFI=1
2189 * adds or modifies the 802.1Q header appropriately, setting the TCI field
2190 * to the field's new value (with the CFI bit masked out).
2192 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
2193 * any of these values modifies the corresponding tunnel header field used
2194 * for the packet's next tunnel encapsulation, if allowed by the
2195 * configuration of the output tunnel port.
2197 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2198 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
2199 * used only if the flow's nx_match includes an nxm_entry that specifies
2200 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2202 * The switch will reject actions for which src_ofs+n_bits is greater than the
2203 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2204 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2206 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2207 * behaves as if 'src' were copied out to a temporary buffer, then the
2208 * temporary buffer copied to 'dst'.
2210 struct nx_action_reg_move
{
2211 ovs_be16 type
; /* OFPAT_VENDOR. */
2212 ovs_be16 len
; /* Length is 24. */
2213 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2214 ovs_be16 subtype
; /* NXAST_REG_MOVE. */
2215 ovs_be16 n_bits
; /* Number of bits. */
2216 ovs_be16 src_ofs
; /* Starting bit offset in source. */
2217 ovs_be16 dst_ofs
; /* Starting bit offset in destination. */
2219 * - OXM/NXM header for source field (4 or 8 bytes).
2220 * - OXM/NXM header for destination field (4 or 8 bytes).
2221 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2223 OFP_ASSERT(sizeof(struct nx_action_reg_move
) == 16);
2226 decode_copy_field__(ovs_be16 src_offset
, ovs_be16 dst_offset
, ovs_be16 n_bits
,
2227 const void *action
, ovs_be16 action_len
, size_t oxm_offset
,
2228 struct ofpbuf
*ofpacts
)
2230 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2231 move
->ofpact
.raw
= ONFACT_RAW13_COPY_FIELD
;
2232 move
->src
.ofs
= ntohs(src_offset
);
2233 move
->src
.n_bits
= ntohs(n_bits
);
2234 move
->dst
.ofs
= ntohs(dst_offset
);
2235 move
->dst
.n_bits
= ntohs(n_bits
);
2237 struct ofpbuf b
= ofpbuf_const_initializer(action
, ntohs(action_len
));
2238 ofpbuf_pull(&b
, oxm_offset
);
2240 enum ofperr error
= nx_pull_header(&b
, &move
->src
.field
, NULL
);
2244 error
= nx_pull_header(&b
, &move
->dst
.field
, NULL
);
2249 if (!is_all_zeros(b
.data
, b
.size
)) {
2250 return OFPERR_NXBRC_MUST_BE_ZERO
;
2253 return nxm_reg_move_check(move
, NULL
);
2257 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field
*oacf
,
2258 enum ofp_version ofp_version OVS_UNUSED
,
2259 struct ofpbuf
*ofpacts
)
2261 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
2262 oacf
->n_bits
, oacf
, oacf
->len
,
2263 OBJECT_OFFSETOF(oacf
, pad2
), ofpacts
);
2267 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field
*oacf
,
2268 enum ofp_version ofp_version OVS_UNUSED
,
2269 struct ofpbuf
*ofpacts
)
2271 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
2272 oacf
->n_bits
, oacf
, oacf
->len
,
2273 OBJECT_OFFSETOF(oacf
, pad3
), ofpacts
);
2277 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move
*narm
,
2278 enum ofp_version ofp_version OVS_UNUSED
,
2279 struct ofpbuf
*ofpacts
)
2281 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2282 move
->ofpact
.raw
= NXAST_RAW_REG_MOVE
;
2283 move
->src
.ofs
= ntohs(narm
->src_ofs
);
2284 move
->src
.n_bits
= ntohs(narm
->n_bits
);
2285 move
->dst
.ofs
= ntohs(narm
->dst_ofs
);
2286 move
->dst
.n_bits
= ntohs(narm
->n_bits
);
2288 struct ofpbuf b
= ofpbuf_const_initializer(narm
, ntohs(narm
->len
));
2289 ofpbuf_pull(&b
, sizeof *narm
);
2291 enum ofperr error
= nx_pull_header(&b
, &move
->src
.field
, NULL
);
2295 error
= nx_pull_header(&b
, &move
->dst
.field
, NULL
);
2299 if (!is_all_zeros(b
.data
, b
.size
)) {
2300 return OFPERR_NXBRC_MUST_BE_ZERO
;
2303 return nxm_reg_move_check(move
, NULL
);
2307 encode_REG_MOVE(const struct ofpact_reg_move
*move
,
2308 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2310 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2311 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2312 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2313 * probe for support. Until we have that ability, we currently prefer
2314 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2316 size_t start_ofs
= out
->size
;
2317 if (ofp_version
>= OFP15_VERSION
) {
2318 struct ofp15_action_copy_field
*copy
= put_OFPAT15_COPY_FIELD(out
);
2319 copy
->n_bits
= htons(move
->dst
.n_bits
);
2320 copy
->src_offset
= htons(move
->src
.ofs
);
2321 copy
->dst_offset
= htons(move
->dst
.ofs
);
2322 out
->size
= out
->size
- sizeof copy
->pad2
;
2323 nx_put_header(out
, move
->src
.field
->id
, ofp_version
, false);
2324 nx_put_header(out
, move
->dst
.field
->id
, ofp_version
, false);
2325 } else if (ofp_version
== OFP13_VERSION
2326 && move
->ofpact
.raw
== ONFACT_RAW13_COPY_FIELD
) {
2327 struct onf_action_copy_field
*copy
= put_ONFACT13_COPY_FIELD(out
);
2328 copy
->n_bits
= htons(move
->dst
.n_bits
);
2329 copy
->src_offset
= htons(move
->src
.ofs
);
2330 copy
->dst_offset
= htons(move
->dst
.ofs
);
2331 out
->size
= out
->size
- sizeof copy
->pad3
;
2332 nx_put_header(out
, move
->src
.field
->id
, ofp_version
, false);
2333 nx_put_header(out
, move
->dst
.field
->id
, ofp_version
, false);
2335 struct nx_action_reg_move
*narm
= put_NXAST_REG_MOVE(out
);
2336 narm
->n_bits
= htons(move
->dst
.n_bits
);
2337 narm
->src_ofs
= htons(move
->src
.ofs
);
2338 narm
->dst_ofs
= htons(move
->dst
.ofs
);
2339 nx_put_header(out
, move
->src
.field
->id
, 0, false);
2340 nx_put_header(out
, move
->dst
.field
->id
, 0, false);
2342 pad_ofpat(out
, start_ofs
);
2345 static char * OVS_WARN_UNUSED_RESULT
2346 parse_REG_MOVE(const char *arg
, struct ofpbuf
*ofpacts
,
2347 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2349 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2350 const char *full_arg
= arg
;
2353 error
= mf_parse_subfield__(&move
->src
, &arg
);
2357 if (strncmp(arg
, "->", 2)) {
2358 return xasprintf("%s: missing `->' following source", full_arg
);
2361 error
= mf_parse_subfield(&move
->dst
, arg
);
2366 if (move
->src
.n_bits
!= move
->dst
.n_bits
) {
2367 return xasprintf("%s: source field is %d bits wide but destination is "
2368 "%d bits wide", full_arg
,
2369 move
->src
.n_bits
, move
->dst
.n_bits
);
2375 format_REG_MOVE(const struct ofpact_reg_move
*a
, struct ds
*s
)
2377 nxm_format_reg_move(a
, s
);
2380 /* Action structure for OFPAT12_SET_FIELD. */
2381 struct ofp12_action_set_field
{
2382 ovs_be16 type
; /* OFPAT12_SET_FIELD. */
2383 ovs_be16 len
; /* Length is padded to 64 bits. */
2386 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2387 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2389 * The "pad" member is the beginning of the above. */
2392 OFP_ASSERT(sizeof(struct ofp12_action_set_field
) == 8);
2394 /* Action structure for NXAST_REG_LOAD.
2396 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2397 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2398 * starts at 0 for the least-significant bit, 1 for the next most significant
2401 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2402 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2405 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2406 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2407 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2408 * also stored as one less than its true value:
2411 * +------------------------------+------------------+
2412 * | ofs | n_bits - 1 |
2413 * +------------------------------+------------------+
2415 * The switch will reject actions for which ofs+n_bits is greater than the
2416 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2417 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2418 * OFPBAC_BAD_ARGUMENT.
2420 struct nx_action_reg_load
{
2421 ovs_be16 type
; /* OFPAT_VENDOR. */
2422 ovs_be16 len
; /* Length is 24. */
2423 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2424 ovs_be16 subtype
; /* NXAST_REG_LOAD. */
2425 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
2426 ovs_be32 dst
; /* Destination register. */
2427 ovs_be64 value
; /* Immediate value. */
2429 OFP_ASSERT(sizeof(struct nx_action_reg_load
) == 24);
2431 /* Action structure for NXAST_REG_LOAD2.
2433 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2434 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2435 * OXM experimenter fields. */
2436 struct nx_action_reg_load2
{
2437 ovs_be16 type
; /* OFPAT_VENDOR. */
2438 ovs_be16 len
; /* At least 16. */
2439 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2440 ovs_be16 subtype
; /* NXAST_SET_FIELD. */
2443 * - An NXM/OXM header, value, and optionally a mask.
2444 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2446 * The "pad" member is the beginning of the above. */
2449 OFP_ASSERT(sizeof(struct nx_action_reg_load2
) == 16);
2452 decode_ofpat_set_field(const struct ofp12_action_set_field
*oasf
,
2453 bool may_mask
, struct ofpbuf
*ofpacts
)
2455 struct ofpbuf b
= ofpbuf_const_initializer(oasf
, ntohs(oasf
->len
));
2456 ofpbuf_pull(&b
, OBJECT_OFFSETOF(oasf
, pad
));
2458 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(ofpacts
);
2459 enum ofperr error
= nx_pull_entry(&b
, &sf
->field
, &sf
->value
,
2460 may_mask
? &sf
->mask
: NULL
);
2462 return (error
== OFPERR_OFPBMC_BAD_MASK
2463 ? OFPERR_OFPBAC_BAD_SET_MASK
2467 memset(&sf
->mask
, 0xff, sf
->field
->n_bytes
);
2470 if (!is_all_zeros(b
.data
, b
.size
)) {
2471 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2474 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2476 if (sf
->field
->id
== MFF_IN_PORT_OXM
) {
2477 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2480 /* oxm_length is now validated to be compatible with mf_value. */
2481 if (!sf
->field
->writable
) {
2482 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2484 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2487 /* The value must be valid for match. OpenFlow 1.5 also says,
2488 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2489 * a 1-bit in oxm_value and in oxm_mask." */
2490 if (!mf_is_value_valid(sf
->field
, &sf
->value
)
2491 || (sf
->field
->id
== MFF_VLAN_VID
2492 && (!(sf
->mask
.be16
& htons(OFPVID12_PRESENT
))
2493 || !(sf
->value
.be16
& htons(OFPVID12_PRESENT
))))) {
2494 struct ds ds
= DS_EMPTY_INITIALIZER
;
2495 mf_format(sf
->field
, &sf
->value
, NULL
, &ds
);
2496 VLOG_WARN_RL(&rl
, "Invalid value for set field %s: %s",
2497 sf
->field
->name
, ds_cstr(&ds
));
2500 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2506 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2507 enum ofp_version ofp_version OVS_UNUSED
,
2508 struct ofpbuf
*ofpacts
)
2510 return decode_ofpat_set_field(oasf
, false, ofpacts
);
2514 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2515 enum ofp_version ofp_version OVS_UNUSED
,
2516 struct ofpbuf
*ofpacts
)
2518 return decode_ofpat_set_field(oasf
, true, ofpacts
);
2522 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load
*narl
,
2523 enum ofp_version ofp_version OVS_UNUSED
,
2526 struct ofpact_set_field
*sf
= ofpact_put_reg_load(out
);
2527 struct mf_subfield dst
;
2530 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD
;
2532 dst
.field
= mf_from_nxm_header(ntohl(narl
->dst
));
2533 dst
.ofs
= nxm_decode_ofs(narl
->ofs_nbits
);
2534 dst
.n_bits
= nxm_decode_n_bits(narl
->ofs_nbits
);
2535 error
= mf_check_dst(&dst
, NULL
);
2540 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2542 if (dst
.n_bits
< 64 && ntohll(narl
->value
) >> dst
.n_bits
) {
2543 return OFPERR_OFPBAC_BAD_ARGUMENT
;
2546 sf
->field
= dst
.field
;
2547 bitwise_put(ntohll(narl
->value
),
2548 &sf
->value
, dst
.field
->n_bytes
, dst
.ofs
,
2550 bitwise_put(UINT64_MAX
,
2551 &sf
->mask
, dst
.field
->n_bytes
, dst
.ofs
,
2558 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2
*narl
,
2559 enum ofp_version ofp_version OVS_UNUSED
,
2562 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(out
);
2563 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD2
;
2565 struct ofpbuf b
= ofpbuf_const_initializer(narl
, ntohs(narl
->len
));
2566 ofpbuf_pull(&b
, OBJECT_OFFSETOF(narl
, pad
));
2568 enum ofperr error
= nx_pull_entry(&b
, &sf
->field
, &sf
->value
, &sf
->mask
);
2572 if (!is_all_zeros(b
.data
, b
.size
)) {
2573 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2576 if (!sf
->field
->writable
) {
2577 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2579 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2585 ofpact_put_set_field(struct ofpbuf
*openflow
, enum ofp_version ofp_version
,
2586 enum mf_field_id field
, uint64_t value_
)
2588 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
2589 int n_bytes
= mf_from_id(field
)->n_bytes
;
2590 size_t start_ofs
= openflow
->size
;
2591 union mf_value value
;
2593 value
.be64
= htonll(value_
<< (8 * (8 - n_bytes
)));
2595 oasf
= put_OFPAT12_SET_FIELD(openflow
);
2596 openflow
->size
= openflow
->size
- sizeof oasf
->pad
;
2597 nx_put_entry(openflow
, field
, ofp_version
, &value
, NULL
);
2598 pad_ofpat(openflow
, start_ofs
);
2602 next_load_segment(const struct ofpact_set_field
*sf
,
2603 struct mf_subfield
*dst
, uint64_t *value
)
2605 int n_bits
= sf
->field
->n_bits
;
2606 int n_bytes
= sf
->field
->n_bytes
;
2607 int start
= dst
->ofs
+ dst
->n_bits
;
2609 if (start
< n_bits
) {
2610 dst
->field
= sf
->field
;
2611 dst
->ofs
= bitwise_scan(&sf
->mask
, n_bytes
, 1, start
, n_bits
);
2612 if (dst
->ofs
< n_bits
) {
2613 dst
->n_bits
= bitwise_scan(&sf
->mask
, n_bytes
, 0, dst
->ofs
+ 1,
2614 MIN(dst
->ofs
+ 64, n_bits
)) - dst
->ofs
;
2615 *value
= bitwise_get(&sf
->value
, n_bytes
, dst
->ofs
, dst
->n_bits
);
2622 /* Convert 'sf' to a series of REG_LOADs. */
2624 set_field_to_nxast(const struct ofpact_set_field
*sf
, struct ofpbuf
*openflow
)
2626 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2627 * experimenter OXM or is variable length (or if it came in as
2628 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2629 * NXAST_REG_LOAD, which is backward compatible. */
2630 if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
2631 || !mf_nxm_header(sf
->field
->id
) || sf
->field
->variable_len
) {
2632 struct nx_action_reg_load2
*narl OVS_UNUSED
;
2633 size_t start_ofs
= openflow
->size
;
2635 narl
= put_NXAST_REG_LOAD2(openflow
);
2636 openflow
->size
= openflow
->size
- sizeof narl
->pad
;
2637 nx_put_entry(openflow
, sf
->field
->id
, 0, &sf
->value
, &sf
->mask
);
2638 pad_ofpat(openflow
, start_ofs
);
2640 struct mf_subfield dst
;
2643 dst
.ofs
= dst
.n_bits
= 0;
2644 while (next_load_segment(sf
, &dst
, &value
)) {
2645 struct nx_action_reg_load
*narl
= put_NXAST_REG_LOAD(openflow
);
2646 narl
->ofs_nbits
= nxm_encode_ofs_nbits(dst
.ofs
, dst
.n_bits
);
2647 narl
->dst
= htonl(mf_nxm_header(dst
.field
->id
));
2648 narl
->value
= htonll(value
);
2653 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2654 * actions, if we can, falling back to Nicira extensions if we must.
2656 * We check only meta-flow types that can appear within set field actions and
2657 * that have a mapping to compatible action types. These struct mf_field
2658 * definitions have a defined OXM or NXM header value and specify the field as
2661 set_field_to_legacy_openflow(const struct ofpact_set_field
*sf
,
2662 enum ofp_version ofp_version
,
2665 switch ((int) sf
->field
->id
) {
2666 case MFF_VLAN_TCI
: {
2667 ovs_be16 tci
= sf
->value
.be16
;
2668 bool cfi
= (tci
& htons(VLAN_CFI
)) != 0;
2669 uint16_t vid
= vlan_tci_to_vid(tci
);
2670 uint8_t pcp
= vlan_tci_to_pcp(tci
);
2672 if (ofp_version
< OFP11_VERSION
) {
2673 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2675 * If CFI=1, Add or modify VLAN VID & PCP.
2676 * If CFI=0, strip VLAN header, if any.
2679 put_OFPAT10_SET_VLAN_VID(out
, vid
);
2680 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
2682 put_OFPAT10_STRIP_VLAN(out
);
2685 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2687 * If CFI=1, Add or modify VLAN VID & PCP.
2688 * OpenFlow 1.1 set actions only apply if the packet
2689 * already has VLAN tags. To be sure that is the case
2690 * we have to push a VLAN header. As we do not support
2691 * multiple layers of VLANs, this is a no-op, if a VLAN
2692 * header already exists. This may backfire, however,
2693 * when we start supporting multiple layers of VLANs.
2694 * If CFI=0, strip VLAN header, if any.
2697 /* Push a VLAN tag, if one was not seen at action validation
2699 if (!sf
->flow_has_vlan
) {
2700 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
2702 put_OFPAT11_SET_VLAN_VID(out
, vid
);
2703 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
2705 /* If the flow did not match on vlan, we have no way of
2706 * knowing if the vlan tag exists, so we must POP just to be
2708 put_OFPAT11_POP_VLAN(out
);
2714 case MFF_VLAN_VID
: {
2715 uint16_t vid
= ntohs(sf
->value
.be16
) & VLAN_VID_MASK
;
2716 if (ofp_version
== OFP10_VERSION
) {
2717 put_OFPAT10_SET_VLAN_VID(out
, vid
);
2719 put_OFPAT11_SET_VLAN_VID(out
, vid
);
2725 if (ofp_version
== OFP10_VERSION
) {
2726 put_OFPAT10_SET_VLAN_PCP(out
, sf
->value
.u8
);
2728 put_OFPAT11_SET_VLAN_PCP(out
, sf
->value
.u8
);
2733 put_OFPAT_SET_DL_SRC(out
, ofp_version
)->dl_addr
= sf
->value
.mac
;
2737 put_OFPAT_SET_DL_DST(out
, ofp_version
)->dl_addr
= sf
->value
.mac
;
2741 put_OFPAT_SET_NW_SRC(out
, ofp_version
, sf
->value
.be32
);
2745 put_OFPAT_SET_NW_DST(out
, ofp_version
, sf
->value
.be32
);
2749 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
.u8
);
2752 case MFF_IP_DSCP_SHIFTED
:
2753 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
.u8
<< 2);
2758 put_OFPAT_SET_TP_SRC(out
, sf
->value
.be16
);
2763 put_OFPAT_SET_TP_DST(out
, sf
->value
.be16
);
2767 set_field_to_nxast(sf
, out
);
2773 set_field_to_set_field(const struct ofpact_set_field
*sf
,
2774 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2776 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
2777 size_t start_ofs
= out
->size
;
2779 oasf
= put_OFPAT12_SET_FIELD(out
);
2780 out
->size
= out
->size
- sizeof oasf
->pad
;
2781 nx_put_entry(out
, sf
->field
->id
, ofp_version
, &sf
->value
, &sf
->mask
);
2782 pad_ofpat(out
, start_ofs
);
2786 encode_SET_FIELD(const struct ofpact_set_field
*sf
,
2787 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2789 if (ofp_version
>= OFP15_VERSION
) {
2790 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2792 set_field_to_set_field(sf
, ofp_version
, out
);
2793 } else if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD
||
2794 sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
) {
2795 /* It came in as reg_load, send it out the same way. */
2796 set_field_to_nxast(sf
, out
);
2797 } else if (ofp_version
< OFP12_VERSION
) {
2798 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2799 set_field_to_legacy_openflow(sf
, ofp_version
, out
);
2800 } else if (is_all_ones((const uint8_t *) &sf
->mask
, sf
->field
->n_bytes
)) {
2801 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2802 * entire field, so encode it as OFPAT_SET_FIELD. */
2803 set_field_to_set_field(sf
, ofp_version
, out
);
2805 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2806 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2807 * so encode it as reg_load. */
2808 set_field_to_nxast(sf
, out
);
2812 /* Parses the input argument 'arg' into the key, value, and delimiter
2813 * components that are common across the reg_load and set_field action format.
2815 * With an argument like "1->metadata", sets the following pointers to
2816 * point within 'arg':
2821 * Returns NULL if successful, otherwise a malloc()'d string describing the
2822 * error. The caller is responsible for freeing the returned string. */
2823 static char * OVS_WARN_UNUSED_RESULT
2824 set_field_split_str(char *arg
, char **key
, char **value
, char **delim
)
2829 value_end
= strstr(arg
, "->");
2830 *key
= value_end
+ strlen("->");
2836 return xasprintf("%s: missing `->'", arg
);
2838 if (strlen(value_end
) <= strlen("->")) {
2839 return xasprintf("%s: missing field name following `->'", arg
);
2845 /* Parses a "set_field" action with argument 'arg', appending the parsed
2846 * action to 'ofpacts'.
2848 * Returns NULL if successful, otherwise a malloc()'d string describing the
2849 * error. The caller is responsible for freeing the returned string. */
2850 static char * OVS_WARN_UNUSED_RESULT
2851 set_field_parse__(char *arg
, struct ofpbuf
*ofpacts
,
2852 enum ofputil_protocol
*usable_protocols
)
2854 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(ofpacts
);
2858 const struct mf_field
*mf
;
2861 error
= set_field_split_str(arg
, &key
, &value
, &delim
);
2866 mf
= mf_from_name(key
);
2868 return xasprintf("%s is not a valid OXM field name", key
);
2870 if (!mf
->writable
) {
2871 return xasprintf("%s is read-only", key
);
2875 error
= mf_parse(mf
, value
, &sf
->value
, &sf
->mask
);
2880 if (!mf_is_value_valid(mf
, &sf
->value
)) {
2881 return xasprintf("%s is not a valid value for field %s", value
, key
);
2884 *usable_protocols
&= mf
->usable_protocols_exact
;
2888 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2889 * action to 'ofpacts'.
2891 * Returns NULL if successful, otherwise a malloc()'d string describing the
2892 * error. The caller is responsible for freeing the returned string. */
2893 static char * OVS_WARN_UNUSED_RESULT
2894 parse_SET_FIELD(const char *arg
, struct ofpbuf
*ofpacts
,
2895 enum ofputil_protocol
*usable_protocols
)
2897 char *copy
= xstrdup(arg
);
2898 char *error
= set_field_parse__(copy
, ofpacts
, usable_protocols
);
2903 static char * OVS_WARN_UNUSED_RESULT
2904 parse_reg_load(char *arg
, struct ofpbuf
*ofpacts
)
2906 struct ofpact_set_field
*sf
= ofpact_put_reg_load(ofpacts
);
2907 struct mf_subfield dst
;
2908 char *key
, *value_str
;
2909 union mf_value value
;
2912 error
= set_field_split_str(arg
, &key
, &value_str
, NULL
);
2917 error
= mf_parse_subfield(&dst
, key
);
2922 if (parse_int_string(value_str
, (uint8_t *)&value
, dst
.field
->n_bytes
,
2924 return xasprintf("%s: cannot parse integer value", arg
);
2927 if (!bitwise_is_all_zeros(&value
, dst
.field
->n_bytes
, dst
.n_bits
,
2928 dst
.field
->n_bytes
* 8 - dst
.n_bits
)) {
2932 mf_format(dst
.field
, &value
, NULL
, &ds
);
2933 error
= xasprintf("%s: value %s does not fit into %d bits",
2934 arg
, ds_cstr(&ds
), dst
.n_bits
);
2939 sf
->field
= dst
.field
;
2940 memset(&sf
->value
, 0, sizeof sf
->value
);
2941 bitwise_copy(&value
, dst
.field
->n_bytes
, 0, &sf
->value
,
2942 dst
.field
->n_bytes
, dst
.ofs
, dst
.n_bits
);
2943 bitwise_one(&sf
->mask
, dst
.field
->n_bytes
, dst
.ofs
, dst
.n_bits
);
2949 format_SET_FIELD(const struct ofpact_set_field
*a
, struct ds
*s
)
2951 if (a
->ofpact
.raw
== NXAST_RAW_REG_LOAD
) {
2952 struct mf_subfield dst
;
2955 dst
.ofs
= dst
.n_bits
= 0;
2956 while (next_load_segment(a
, &dst
, &value
)) {
2957 ds_put_format(s
, "%sload:%s%#"PRIx64
"%s->%s",
2958 colors
.special
, colors
.end
, value
,
2959 colors
.special
, colors
.end
);
2960 mf_format_subfield(&dst
, s
);
2961 ds_put_char(s
, ',');
2965 ds_put_format(s
, "%sset_field:%s", colors
.special
, colors
.end
);
2966 mf_format(a
->field
, &a
->value
, &a
->mask
, s
);
2967 ds_put_format(s
, "%s->%s%s",
2968 colors
.special
, colors
.end
, a
->field
->name
);
2972 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2973 * is marked such that, if possible, it will be translated to OpenFlow as
2974 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2975 * that was the way that the action was expressed when it came into OVS or for
2976 * backward compatibility. */
2977 struct ofpact_set_field
*
2978 ofpact_put_reg_load(struct ofpbuf
*ofpacts
)
2980 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(ofpacts
);
2981 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD
;
2985 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2987 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2990 struct nx_action_stack
{
2991 ovs_be16 type
; /* OFPAT_VENDOR. */
2992 ovs_be16 len
; /* Length is 16. */
2993 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2994 ovs_be16 subtype
; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2995 ovs_be16 offset
; /* Bit offset into the field. */
2997 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2998 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2999 * - Enough 0-bytes to pad out the action to 24 bytes. */
3000 uint8_t pad
[12]; /* See above. */
3002 OFP_ASSERT(sizeof(struct nx_action_stack
) == 24);
3005 decode_stack_action(const struct nx_action_stack
*nasp
,
3006 struct ofpact_stack
*stack_action
)
3008 stack_action
->subfield
.ofs
= ntohs(nasp
->offset
);
3010 struct ofpbuf b
= ofpbuf_const_initializer(nasp
, sizeof *nasp
);
3011 ofpbuf_pull(&b
, OBJECT_OFFSETOF(nasp
, pad
));
3012 enum ofperr error
= nx_pull_header(&b
, &stack_action
->subfield
.field
,
3017 stack_action
->subfield
.n_bits
= ntohs(*(const ovs_be16
*) b
.data
);
3019 if (!is_all_zeros(b
.data
, b
.size
)) {
3020 return OFPERR_NXBRC_MUST_BE_ZERO
;
3027 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack
*nasp
,
3028 enum ofp_version ofp_version OVS_UNUSED
,
3029 struct ofpbuf
*ofpacts
)
3031 struct ofpact_stack
*push
= ofpact_put_STACK_PUSH(ofpacts
);
3032 enum ofperr error
= decode_stack_action(nasp
, push
);
3033 return error
? error
: nxm_stack_push_check(push
, NULL
);
3037 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack
*nasp
,
3038 enum ofp_version ofp_version OVS_UNUSED
,
3039 struct ofpbuf
*ofpacts
)
3041 struct ofpact_stack
*pop
= ofpact_put_STACK_POP(ofpacts
);
3042 enum ofperr error
= decode_stack_action(nasp
, pop
);
3043 return error
? error
: nxm_stack_pop_check(pop
, NULL
);
3047 encode_STACK_op(const struct ofpact_stack
*stack_action
,
3048 struct nx_action_stack
*nasp
)
3053 nasp
->offset
= htons(stack_action
->subfield
.ofs
);
3055 ofpbuf_use_stack(&b
, nasp
, ntohs(nasp
->len
));
3056 ofpbuf_put_uninit(&b
, OBJECT_OFFSETOF(nasp
, pad
));
3057 nx_put_header(&b
, stack_action
->subfield
.field
->id
, 0, false);
3058 n_bits
= htons(stack_action
->subfield
.n_bits
);
3059 ofpbuf_put(&b
, &n_bits
, sizeof n_bits
);
3063 encode_STACK_PUSH(const struct ofpact_stack
*stack
,
3064 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3066 encode_STACK_op(stack
, put_NXAST_STACK_PUSH(out
));
3070 encode_STACK_POP(const struct ofpact_stack
*stack
,
3071 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3073 encode_STACK_op(stack
, put_NXAST_STACK_POP(out
));
3076 static char * OVS_WARN_UNUSED_RESULT
3077 parse_STACK_PUSH(char *arg
, struct ofpbuf
*ofpacts
,
3078 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3080 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts
), arg
);
3083 static char * OVS_WARN_UNUSED_RESULT
3084 parse_STACK_POP(char *arg
, struct ofpbuf
*ofpacts
,
3085 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3087 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts
), arg
);
3091 format_STACK_PUSH(const struct ofpact_stack
*a
, struct ds
*s
)
3093 nxm_format_stack_push(a
, s
);
3097 format_STACK_POP(const struct ofpact_stack
*a
, struct ds
*s
)
3099 nxm_format_stack_pop(a
, s
);
3102 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
3104 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
3105 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
3106 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3107 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3109 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3110 * sent only to controllers with id 0.)
3112 struct nx_action_cnt_ids
{
3113 ovs_be16 type
; /* OFPAT_VENDOR. */
3114 ovs_be16 len
; /* Length including slaves. */
3115 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3116 ovs_be16 subtype
; /* NXAST_DEC_TTL_CNT_IDS. */
3118 ovs_be16 n_controllers
; /* Number of controllers. */
3119 uint8_t zeros
[4]; /* Must be zero. */
3121 /* Followed by 1 or more controller ids.
3123 * uint16_t cnt_ids[]; // Controller ids.
3124 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
3127 OFP_ASSERT(sizeof(struct nx_action_cnt_ids
) == 16);
3130 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf
*out
)
3133 struct ofpact_cnt_ids
*ids
;
3134 enum ofperr error
= 0;
3136 ids
= ofpact_put_DEC_TTL(out
);
3137 ids
->n_controllers
= 1;
3138 ofpbuf_put(out
, &id
, sizeof id
);
3140 ofpact_finish_DEC_TTL(out
, &ids
);
3145 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids
*nac_ids
,
3146 enum ofp_version ofp_version OVS_UNUSED
,
3149 struct ofpact_cnt_ids
*ids
;
3153 ids
= ofpact_put_DEC_TTL(out
);
3154 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
3155 ids
->n_controllers
= ntohs(nac_ids
->n_controllers
);
3156 ids_size
= ntohs(nac_ids
->len
) - sizeof *nac_ids
;
3158 if (!is_all_zeros(nac_ids
->zeros
, sizeof nac_ids
->zeros
)) {
3159 return OFPERR_NXBRC_MUST_BE_ZERO
;
3162 if (ids_size
< ids
->n_controllers
* sizeof(ovs_be16
)) {
3163 VLOG_WARN_RL(&rl
, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE
" "
3164 "bytes allocated for controller ids. %"PRIuSIZE
" bytes "
3165 "are required for %"PRIu16
" controllers.",
3166 ids_size
, ids
->n_controllers
* sizeof(ovs_be16
),
3167 ids
->n_controllers
);
3168 return OFPERR_OFPBAC_BAD_LEN
;
3171 for (i
= 0; i
< ids
->n_controllers
; i
++) {
3172 uint16_t id
= ntohs(((ovs_be16
*)(nac_ids
+ 1))[i
]);
3173 ofpbuf_put(out
, &id
, sizeof id
);
3177 ofpact_finish_DEC_TTL(out
, &ids
);
3183 encode_DEC_TTL(const struct ofpact_cnt_ids
*dec_ttl
,
3184 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3186 if (dec_ttl
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
3187 || dec_ttl
->n_controllers
!= 1
3188 || dec_ttl
->cnt_ids
[0] != 0) {
3189 struct nx_action_cnt_ids
*nac_ids
= put_NXAST_DEC_TTL_CNT_IDS(out
);
3190 int ids_len
= ROUND_UP(2 * dec_ttl
->n_controllers
, OFP_ACTION_ALIGN
);
3194 nac_ids
->len
= htons(ntohs(nac_ids
->len
) + ids_len
);
3195 nac_ids
->n_controllers
= htons(dec_ttl
->n_controllers
);
3197 ids
= ofpbuf_put_zeros(out
, ids_len
);
3198 for (i
= 0; i
< dec_ttl
->n_controllers
; i
++) {
3199 ids
[i
] = htons(dec_ttl
->cnt_ids
[i
]);
3202 put_OFPAT_DEC_NW_TTL(out
, ofp_version
);
3207 parse_noargs_dec_ttl(struct ofpbuf
*ofpacts
)
3209 struct ofpact_cnt_ids
*ids
;
3212 ofpact_put_DEC_TTL(ofpacts
);
3213 ofpbuf_put(ofpacts
, &id
, sizeof id
);
3214 ids
= ofpacts
->header
;
3215 ids
->n_controllers
++;
3216 ofpact_finish_DEC_TTL(ofpacts
, &ids
);
3219 static char * OVS_WARN_UNUSED_RESULT
3220 parse_DEC_TTL(char *arg
, struct ofpbuf
*ofpacts
,
3221 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3224 parse_noargs_dec_ttl(ofpacts
);
3226 struct ofpact_cnt_ids
*ids
;
3229 ids
= ofpact_put_DEC_TTL(ofpacts
);
3230 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
3231 for (cntr
= strtok_r(arg
, ", ", &arg
); cntr
!= NULL
;
3232 cntr
= strtok_r(NULL
, ", ", &arg
)) {
3233 uint16_t id
= atoi(cntr
);
3235 ofpbuf_put(ofpacts
, &id
, sizeof id
);
3236 ids
= ofpacts
->header
;
3237 ids
->n_controllers
++;
3239 if (!ids
->n_controllers
) {
3240 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3243 ofpact_finish_DEC_TTL(ofpacts
, &ids
);
3249 format_DEC_TTL(const struct ofpact_cnt_ids
*a
, struct ds
*s
)
3253 ds_put_format(s
, "%sdec_ttl%s", colors
.paren
, colors
.end
);
3254 if (a
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
) {
3255 ds_put_format(s
, "%s(%s", colors
.paren
, colors
.end
);
3256 for (i
= 0; i
< a
->n_controllers
; i
++) {
3258 ds_put_cstr(s
, ",");
3260 ds_put_format(s
, "%"PRIu16
, a
->cnt_ids
[i
]);
3262 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
3266 /* Set MPLS label actions. */
3269 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label
,
3270 enum ofp_version ofp_version OVS_UNUSED
,
3273 ofpact_put_SET_MPLS_LABEL(out
)->label
= label
;
3278 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label
*label
,
3279 enum ofp_version ofp_version
,
3282 if (ofp_version
< OFP12_VERSION
) {
3283 put_OFPAT_SET_MPLS_LABEL(out
, ofp_version
, label
->label
);
3285 ofpact_put_set_field(out
, ofp_version
, MFF_MPLS_LABEL
,
3286 ntohl(label
->label
));
3290 static char * OVS_WARN_UNUSED_RESULT
3291 parse_SET_MPLS_LABEL(char *arg
, struct ofpbuf
*ofpacts
,
3292 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3294 struct ofpact_mpls_label
*mpls_label
= ofpact_put_SET_MPLS_LABEL(ofpacts
);
3296 return xstrdup("set_mpls_label: expected label.");
3299 mpls_label
->label
= htonl(atoi(arg
));
3304 format_SET_MPLS_LABEL(const struct ofpact_mpls_label
*a
, struct ds
*s
)
3306 ds_put_format(s
, "%sset_mpls_label(%s%"PRIu32
"%s)%s",
3307 colors
.paren
, colors
.end
, ntohl(a
->label
),
3308 colors
.paren
, colors
.end
);
3311 /* Set MPLS TC actions. */
3314 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc
,
3315 enum ofp_version ofp_version OVS_UNUSED
,
3318 ofpact_put_SET_MPLS_TC(out
)->tc
= tc
;
3323 encode_SET_MPLS_TC(const struct ofpact_mpls_tc
*tc
,
3324 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3326 if (ofp_version
< OFP12_VERSION
) {
3327 put_OFPAT_SET_MPLS_TC(out
, ofp_version
, tc
->tc
);
3329 ofpact_put_set_field(out
, ofp_version
, MFF_MPLS_TC
, tc
->tc
);
3333 static char * OVS_WARN_UNUSED_RESULT
3334 parse_SET_MPLS_TC(char *arg
, struct ofpbuf
*ofpacts
,
3335 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3337 struct ofpact_mpls_tc
*mpls_tc
= ofpact_put_SET_MPLS_TC(ofpacts
);
3340 return xstrdup("set_mpls_tc: expected tc.");
3343 mpls_tc
->tc
= atoi(arg
);
3348 format_SET_MPLS_TC(const struct ofpact_mpls_tc
*a
, struct ds
*s
)
3350 ds_put_format(s
, "%sset_mpls_ttl(%s%"PRIu8
"%s)%s",
3351 colors
.paren
, colors
.end
, a
->tc
,
3352 colors
.paren
, colors
.end
);
3355 /* Set MPLS TTL actions. */
3358 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl
,
3359 enum ofp_version ofp_version OVS_UNUSED
,
3362 ofpact_put_SET_MPLS_TTL(out
)->ttl
= ttl
;
3367 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*ttl
,
3368 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3370 put_OFPAT_SET_MPLS_TTL(out
, ofp_version
, ttl
->ttl
);
3373 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3374 * action to 'ofpacts'.
3376 * Returns NULL if successful, otherwise a malloc()'d string describing the
3377 * error. The caller is responsible for freeing the returned string. */
3378 static char * OVS_WARN_UNUSED_RESULT
3379 parse_SET_MPLS_TTL(char *arg
, struct ofpbuf
*ofpacts
,
3380 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3382 struct ofpact_mpls_ttl
*mpls_ttl
= ofpact_put_SET_MPLS_TTL(ofpacts
);
3385 return xstrdup("set_mpls_ttl: expected ttl.");
3388 mpls_ttl
->ttl
= atoi(arg
);
3393 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*a
, struct ds
*s
)
3395 ds_put_format(s
, "%sset_mpls_ttl(%s%"PRIu8
"%s)%s",
3396 colors
.paren
, colors
.end
, a
->ttl
,
3397 colors
.paren
, colors
.end
);
3400 /* Decrement MPLS TTL actions. */
3403 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf
*out
)
3405 ofpact_put_DEC_MPLS_TTL(out
);
3410 encode_DEC_MPLS_TTL(const struct ofpact_null
*null OVS_UNUSED
,
3411 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3413 put_OFPAT_DEC_MPLS_TTL(out
, ofp_version
);
3416 static char * OVS_WARN_UNUSED_RESULT
3417 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
3418 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3420 ofpact_put_DEC_MPLS_TTL(ofpacts
);
3425 format_DEC_MPLS_TTL(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
3427 ds_put_format(s
, "%sdec_mpls_ttl%s", colors
.value
, colors
.end
);
3430 /* Push MPLS label action. */
3433 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype
,
3434 enum ofp_version ofp_version OVS_UNUSED
,
3437 struct ofpact_push_mpls
*oam
;
3439 if (!eth_type_mpls(ethertype
)) {
3440 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3442 oam
= ofpact_put_PUSH_MPLS(out
);
3443 oam
->ethertype
= ethertype
;
3449 encode_PUSH_MPLS(const struct ofpact_push_mpls
*push_mpls
,
3450 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3452 put_OFPAT_PUSH_MPLS(out
, ofp_version
, push_mpls
->ethertype
);
3455 static char * OVS_WARN_UNUSED_RESULT
3456 parse_PUSH_MPLS(char *arg
, struct ofpbuf
*ofpacts
,
3457 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3462 error
= str_to_u16(arg
, "push_mpls", ðertype
);
3464 ofpact_put_PUSH_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
3470 format_PUSH_MPLS(const struct ofpact_push_mpls
*a
, struct ds
*s
)
3472 ds_put_format(s
, "%spush_mpls:%s0x%04"PRIx16
,
3473 colors
.param
, colors
.end
, ntohs(a
->ethertype
));
3476 /* Pop MPLS label action. */
3479 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype
,
3480 enum ofp_version ofp_version OVS_UNUSED
,
3483 ofpact_put_POP_MPLS(out
)->ethertype
= ethertype
;
3488 encode_POP_MPLS(const struct ofpact_pop_mpls
*pop_mpls
,
3489 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3491 put_OFPAT_POP_MPLS(out
, ofp_version
, pop_mpls
->ethertype
);
3494 static char * OVS_WARN_UNUSED_RESULT
3495 parse_POP_MPLS(char *arg
, struct ofpbuf
*ofpacts
,
3496 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3501 error
= str_to_u16(arg
, "pop_mpls", ðertype
);
3503 ofpact_put_POP_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
3509 format_POP_MPLS(const struct ofpact_pop_mpls
*a
, struct ds
*s
)
3511 ds_put_format(s
, "%spop_mpls:%s0x%04"PRIx16
,
3512 colors
.param
, colors
.end
, ntohs(a
->ethertype
));
3515 /* Set tunnel ID actions. */
3518 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id
,
3519 enum ofp_version ofp_version OVS_UNUSED
,
3522 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
3523 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL
;
3524 tunnel
->tun_id
= tun_id
;
3529 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id
,
3530 enum ofp_version ofp_version OVS_UNUSED
,
3533 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
3534 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL64
;
3535 tunnel
->tun_id
= tun_id
;
3540 encode_SET_TUNNEL(const struct ofpact_tunnel
*tunnel
,
3541 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3543 uint64_t tun_id
= tunnel
->tun_id
;
3545 if (ofp_version
< OFP12_VERSION
) {
3546 if (tun_id
<= UINT32_MAX
3547 && tunnel
->ofpact
.raw
!= NXAST_RAW_SET_TUNNEL64
) {
3548 put_NXAST_SET_TUNNEL(out
, tun_id
);
3550 put_NXAST_SET_TUNNEL64(out
, tun_id
);
3553 ofpact_put_set_field(out
, ofp_version
, MFF_TUN_ID
, tun_id
);
3557 static char * OVS_WARN_UNUSED_RESULT
3558 parse_set_tunnel(char *arg
, struct ofpbuf
*ofpacts
,
3559 enum ofp_raw_action_type raw
)
3561 struct ofpact_tunnel
*tunnel
;
3563 tunnel
= ofpact_put_SET_TUNNEL(ofpacts
);
3564 tunnel
->ofpact
.raw
= raw
;
3565 return str_to_u64(arg
, &tunnel
->tun_id
);
3568 static char * OVS_WARN_UNUSED_RESULT
3569 parse_SET_TUNNEL(char *arg
, struct ofpbuf
*ofpacts
,
3570 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3572 return parse_set_tunnel(arg
, ofpacts
, NXAST_RAW_SET_TUNNEL
);
3576 format_SET_TUNNEL(const struct ofpact_tunnel
*a
, struct ds
*s
)
3578 ds_put_format(s
, "%sset_tunnel%s:%s%#"PRIx64
, colors
.param
,
3579 (a
->tun_id
> UINT32_MAX
3580 || a
->ofpact
.raw
== NXAST_RAW_SET_TUNNEL64
? "64" : ""),
3581 colors
.end
, a
->tun_id
);
3584 /* Set queue action. */
3587 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id
,
3588 enum ofp_version ofp_version OVS_UNUSED
,
3591 ofpact_put_SET_QUEUE(out
)->queue_id
= queue_id
;
3596 encode_SET_QUEUE(const struct ofpact_queue
*queue
,
3597 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3599 put_OFPAT_SET_QUEUE(out
, ofp_version
, queue
->queue_id
);
3602 static char * OVS_WARN_UNUSED_RESULT
3603 parse_SET_QUEUE(char *arg
, struct ofpbuf
*ofpacts
,
3604 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3606 return str_to_u32(arg
, &ofpact_put_SET_QUEUE(ofpacts
)->queue_id
);
3610 format_SET_QUEUE(const struct ofpact_queue
*a
, struct ds
*s
)
3612 ds_put_format(s
, "%sset_queue:%s%"PRIu32
,
3613 colors
.param
, colors
.end
, a
->queue_id
);
3616 /* Pop queue action. */
3619 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf
*out
)
3621 ofpact_put_POP_QUEUE(out
);
3626 encode_POP_QUEUE(const struct ofpact_null
*null OVS_UNUSED
,
3627 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3629 put_NXAST_POP_QUEUE(out
);
3632 static char * OVS_WARN_UNUSED_RESULT
3633 parse_POP_QUEUE(const char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
3634 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3636 ofpact_put_POP_QUEUE(ofpacts
);
3641 format_POP_QUEUE(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
3643 ds_put_format(s
, "%spop_queue%s", colors
.value
, colors
.end
);
3646 /* Action structure for NXAST_FIN_TIMEOUT.
3648 * This action changes the idle timeout or hard timeout, or both, of this
3649 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3650 * When such a packet is observed, the action reduces the rule's idle timeout
3651 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3652 * action has no effect on an existing timeout that is already shorter than the
3653 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3654 * of zero has no effect on the respective timeout.
3656 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3657 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3658 * receipt of the FIN or RST.
3660 * This is useful for quickly discarding learned TCP flows that otherwise will
3661 * take a long time to expire.
3663 * This action is intended for use with an OpenFlow rule that matches only a
3664 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3665 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3666 * RST in any of those flows will cause the entire OpenFlow rule to expire
3667 * early, which is not normally desirable.
3669 struct nx_action_fin_timeout
{
3670 ovs_be16 type
; /* OFPAT_VENDOR. */
3671 ovs_be16 len
; /* 16. */
3672 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3673 ovs_be16 subtype
; /* NXAST_FIN_TIMEOUT. */
3674 ovs_be16 fin_idle_timeout
; /* New idle timeout, if nonzero. */
3675 ovs_be16 fin_hard_timeout
; /* New hard timeout, if nonzero. */
3676 ovs_be16 pad
; /* Must be zero. */
3678 OFP_ASSERT(sizeof(struct nx_action_fin_timeout
) == 16);
3681 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout
*naft
,
3682 enum ofp_version ofp_version OVS_UNUSED
,
3685 struct ofpact_fin_timeout
*oft
;
3687 oft
= ofpact_put_FIN_TIMEOUT(out
);
3688 oft
->fin_idle_timeout
= ntohs(naft
->fin_idle_timeout
);
3689 oft
->fin_hard_timeout
= ntohs(naft
->fin_hard_timeout
);
3694 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout
*fin_timeout
,
3695 enum ofp_version ofp_version OVS_UNUSED
,
3698 struct nx_action_fin_timeout
*naft
= put_NXAST_FIN_TIMEOUT(out
);
3699 naft
->fin_idle_timeout
= htons(fin_timeout
->fin_idle_timeout
);
3700 naft
->fin_hard_timeout
= htons(fin_timeout
->fin_hard_timeout
);
3703 static char * OVS_WARN_UNUSED_RESULT
3704 parse_FIN_TIMEOUT(char *arg
, struct ofpbuf
*ofpacts
,
3705 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3707 struct ofpact_fin_timeout
*oft
= ofpact_put_FIN_TIMEOUT(ofpacts
);
3710 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
3713 if (!strcmp(key
, "idle_timeout")) {
3714 error
= str_to_u16(value
, key
, &oft
->fin_idle_timeout
);
3715 } else if (!strcmp(key
, "hard_timeout")) {
3716 error
= str_to_u16(value
, key
, &oft
->fin_hard_timeout
);
3718 error
= xasprintf("invalid key '%s' in 'fin_timeout' argument",
3730 format_FIN_TIMEOUT(const struct ofpact_fin_timeout
*a
, struct ds
*s
)
3732 ds_put_format(s
, "%sfin_timeout(%s", colors
.paren
, colors
.end
);
3733 if (a
->fin_idle_timeout
) {
3734 ds_put_format(s
, "%sidle_timeout=%s%"PRIu16
",",
3735 colors
.param
, colors
.end
, a
->fin_idle_timeout
);
3737 if (a
->fin_hard_timeout
) {
3738 ds_put_format(s
, "%shard_timeout=%s%"PRIu16
",",
3739 colors
.param
, colors
.end
, a
->fin_hard_timeout
);
3742 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
3745 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3747 * These actions search one of the switch's flow tables:
3749 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3750 * it specifies the table to search.
3752 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3753 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3754 * table, that is, the OpenFlow flow table that contains the flow from
3755 * which this action was obtained. If this action did not come from a
3756 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3757 * is the current table.
3759 * The flow table lookup uses a flow that may be slightly modified from the
3762 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3763 * is used as the flow's in_port.
3765 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3766 * then its value is used as the flow's in_port. Otherwise, the original
3769 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3770 * resubmit action, then the flow is updated with the new values.
3772 * Following the lookup, the original in_port is restored.
3774 * If the modified flow matched in the flow table, then the corresponding
3775 * actions are executed. Afterward, actions following the resubmit in the
3776 * original set of actions, if any, are executed; any changes made to the
3777 * packet (e.g. changes to VLAN) by secondary actions persist when those
3778 * actions are executed, although the original in_port is restored.
3780 * Resubmit actions may be used any number of times within a set of actions.
3782 * Resubmit actions may nest. To prevent infinite loops and excessive resource
3783 * use, the implementation may limit nesting depth and the total number of
3786 * - Open vSwitch 1.0.1 and earlier did not support recursion.
3788 * - Open vSwitch 1.0.2 and 1.0.3 limited recursion to 8 levels.
3790 * - Open vSwitch 1.1 and 1.2 limited recursion to 16 levels.
3792 * - Open vSwitch 1.2 through 1.8 limited recursion to 32 levels.
3794 * - Open vSwitch 1.9 through 2.0 limited recursion to 64 levels.
3796 * - Open vSwitch 2.1 through 2.5 limited recursion to 64 levels and impose
3797 * a total limit of 4,096 resubmits per flow translation (earlier versions
3798 * did not impose any total limit).
3800 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3801 * 'pad' to be all-bits-zero.
3803 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3804 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3806 struct nx_action_resubmit
{
3807 ovs_be16 type
; /* OFPAT_VENDOR. */
3808 ovs_be16 len
; /* Length is 16. */
3809 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3810 ovs_be16 subtype
; /* NXAST_RESUBMIT. */
3811 ovs_be16 in_port
; /* New in_port for checking flow table. */
3812 uint8_t table
; /* NXAST_RESUBMIT_TABLE: table to use. */
3815 OFP_ASSERT(sizeof(struct nx_action_resubmit
) == 16);
3818 decode_NXAST_RAW_RESUBMIT(uint16_t port
,
3819 enum ofp_version ofp_version OVS_UNUSED
,
3822 struct ofpact_resubmit
*resubmit
;
3824 resubmit
= ofpact_put_RESUBMIT(out
);
3825 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT
;
3826 resubmit
->in_port
= u16_to_ofp(port
);
3827 resubmit
->table_id
= 0xff;
3832 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit
*nar
,
3833 enum ofp_version ofp_version OVS_UNUSED
,
3836 struct ofpact_resubmit
*resubmit
;
3838 if (nar
->pad
[0] || nar
->pad
[1] || nar
->pad
[2]) {
3839 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3842 resubmit
= ofpact_put_RESUBMIT(out
);
3843 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT_TABLE
;
3844 resubmit
->in_port
= u16_to_ofp(ntohs(nar
->in_port
));
3845 resubmit
->table_id
= nar
->table
;
3850 encode_RESUBMIT(const struct ofpact_resubmit
*resubmit
,
3851 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3853 uint16_t in_port
= ofp_to_u16(resubmit
->in_port
);
3855 if (resubmit
->table_id
== 0xff
3856 && resubmit
->ofpact
.raw
!= NXAST_RAW_RESUBMIT_TABLE
) {
3857 put_NXAST_RESUBMIT(out
, in_port
);
3859 struct nx_action_resubmit
*nar
= put_NXAST_RESUBMIT_TABLE(out
);
3860 nar
->table
= resubmit
->table_id
;
3861 nar
->in_port
= htons(in_port
);
3865 static char * OVS_WARN_UNUSED_RESULT
3866 parse_RESUBMIT(char *arg
, struct ofpbuf
*ofpacts
,
3867 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3869 struct ofpact_resubmit
*resubmit
;
3870 char *in_port_s
, *table_s
;
3872 resubmit
= ofpact_put_RESUBMIT(ofpacts
);
3874 in_port_s
= strsep(&arg
, ",");
3875 if (in_port_s
&& in_port_s
[0]) {
3876 if (!ofputil_port_from_string(in_port_s
, &resubmit
->in_port
)) {
3877 return xasprintf("%s: resubmit to unknown port", in_port_s
);
3880 resubmit
->in_port
= OFPP_IN_PORT
;
3883 table_s
= strsep(&arg
, ",");
3884 if (table_s
&& table_s
[0]) {
3885 uint32_t table_id
= 0;
3888 error
= str_to_u32(table_s
, &table_id
);
3892 resubmit
->table_id
= table_id
;
3894 resubmit
->table_id
= 255;
3897 if (resubmit
->in_port
== OFPP_IN_PORT
&& resubmit
->table_id
== 255) {
3898 return xstrdup("at least one \"in_port\" or \"table\" must be "
3899 "specified on resubmit");
3905 format_RESUBMIT(const struct ofpact_resubmit
*a
, struct ds
*s
)
3907 if (a
->in_port
!= OFPP_IN_PORT
&& a
->table_id
== 255) {
3908 ds_put_format(s
, "%sresubmit:%s", colors
.special
, colors
.end
);
3909 ofputil_format_port(a
->in_port
, s
);
3911 ds_put_format(s
, "%sresubmit(%s", colors
.paren
, colors
.end
);
3912 if (a
->in_port
!= OFPP_IN_PORT
) {
3913 ofputil_format_port(a
->in_port
, s
);
3915 ds_put_char(s
, ',');
3916 if (a
->table_id
!= 255) {
3917 ds_put_format(s
, "%"PRIu8
, a
->table_id
);
3919 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
3923 /* Action structure for NXAST_LEARN.
3925 * This action adds or modifies a flow in an OpenFlow table, similar to
3926 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3927 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3928 * flow's match criteria and actions are built by applying each of the series
3929 * of flow_mod_spec elements included as part of the action.
3931 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3932 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3933 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3934 * bits from a source to a destination. In this case, the header contains
3937 * 15 14 13 12 11 10 0
3938 * +------+---+------+---------------------------------+
3939 * | 0 |src| dst | n_bits |
3940 * +------+---+------+---------------------------------+
3942 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3943 * following table summarizes the meaning of each possible combination.
3944 * Details follow the table:
3947 * --- --- ----------------------------------------------------------
3948 * 0 0 Add match criteria based on value in a field.
3949 * 1 0 Add match criteria based on an immediate value.
3950 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3951 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3952 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3953 * All other combinations are undefined and not allowed.
3955 * The flow_mod_spec header is followed by a source specification and a
3956 * destination specification. The format and meaning of the source
3957 * specification depends on 'src':
3959 * - If 'src' is 0, the source bits are taken from a field in the flow to
3960 * which this action is attached. (This should be a wildcarded field. If
3961 * its value is fully specified then the source bits being copied have
3964 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3965 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3966 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3967 * 'field' and 'ofs' are subject to the same restrictions as the source
3968 * field in NXAST_REG_MOVE.
3970 * - If 'src' is 1, the source bits are a constant value. The source
3971 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3972 * number in network order, the source bits are the 'n_bits'
3973 * least-significant bits. The switch will report an error if other bits
3974 * in the constant are nonzero.
3976 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3977 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3978 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3979 * meaning of the flow_mod_spec depends on 'dst':
3981 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3982 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3983 * packet equal the source bits. 'field' may be any nxm_header with
3984 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3986 * Order is significant. Earlier flow_mod_specs must satisfy any
3987 * prerequisites for matching fields specified later, by copying constant
3988 * values into prerequisite fields.
3990 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3993 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3994 * the new flow. The new flow copies the source bits into
3995 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3998 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3999 * greater than 64 yields multiple NXAST_REG_LOAD actions.
4001 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
4002 * empty. It has the following meaning:
4004 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
4005 * The new flow outputs to the OpenFlow port specified by the source field.
4006 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
4007 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
4010 * Resource Management
4011 * -------------------
4013 * A switch has a finite amount of flow table space available for learning.
4014 * When this space is exhausted, no new learning table entries will be learned
4015 * until some existing flow table entries expire. The controller should be
4016 * prepared to handle this by flooding (which can be implemented as a
4017 * low-priority flow).
4019 * If a learned flow matches a single TCP stream with a relatively long
4020 * timeout, one may make the best of resource constraints by setting
4021 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
4022 * both, to shorter timeouts. When either of these is specified as a nonzero
4023 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
4029 * The following examples give a prose description of the flow_mod_specs along
4030 * with informal notation for how those would be represented and a hex dump of
4031 * the bytes that would be required.
4033 * These examples could work with various nx_action_learn parameters. Typical
4034 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
4035 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
4037 * 1. Learn input port based on the source MAC, with lookup into
4038 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
4040 * Match on in_port=99:
4041 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
4042 * ovs_be16(99), 00 63
4043 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4045 * Match Ethernet destination on Ethernet source from packet:
4046 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4047 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4048 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4050 * Set NXM_NX_REG1[16:31] to the packet's input port:
4051 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
4052 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4053 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
4055 * Given a packet that arrived on port A with Ethernet source address B,
4056 * this would set up the flow "in_port=99, dl_dst=B,
4057 * actions=load:A->NXM_NX_REG1[16..31]".
4059 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
4060 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4061 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
4063 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
4064 * into NXM_NX_REG1[16:31]:
4066 * Match on same VLAN ID as packet:
4067 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
4068 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4069 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4071 * Match Ethernet destination on Ethernet source from packet:
4072 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4073 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4074 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4076 * Output to the packet's input port:
4077 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
4078 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4080 * Given a packet that arrived on port A with Ethernet source address B in
4081 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
4082 * actions=output:A".
4084 * In syntax accepted by ovs-ofctl, this action is:
4085 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4086 * output:NXM_OF_IN_PORT[])
4088 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
4089 * 10-second MAC expiration time to make it easier to see what's going on
4091 * ovs-vsctl del-controller br0
4092 * ovs-ofctl del-flows br0
4093 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
4094 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
4095 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
4096 output:NXM_OF_IN_PORT[]), resubmit(,1)"
4097 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
4099 * You can then dump the MAC learning table with:
4101 * ovs-ofctl dump-flows br0 table=1
4106 * For best performance, segregate learned flows into a table that is not used
4107 * for any other flows except possibly for a lowest-priority "catch-all" flow
4108 * (a flow with no match criteria). If different learning actions specify
4109 * different match criteria, use different tables for the learned flows.
4111 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
4112 * These timeouts apply to the flow that is added, which means that a flow with
4113 * an idle timeout will expire when no traffic has been sent *to* the learned
4114 * address. This is not usually the intent in MAC learning; instead, we want
4115 * the MAC learn entry to expire when no traffic has been sent *from* the
4116 * learned address. Use a hard timeout for that.
4119 * Visibility of Changes
4120 * ---------------------
4122 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
4123 * flow translation are visible to flow table lookups made later in the flow
4124 * translation. This means that, in the example above, a MAC learned by the
4125 * learn action in table 0 would be found in table 1 (if the packet being
4126 * processed had the same source and destination MAC address).
4128 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
4129 * modify a flow) by a "learn" action are visible only for later flow
4130 * translations, not for later lookups within the same flow translation. In
4131 * the MAC learning example, a MAC learned by the learn action in table 0 would
4132 * not be found in table 1 if the flow translation would resubmit to table 1
4133 * after the processing of the learn action, meaning that if this MAC had not
4134 * been learned before then the packet would be flooded. */
4135 struct nx_action_learn
{
4136 ovs_be16 type
; /* OFPAT_VENDOR. */
4137 ovs_be16 len
; /* At least 24. */
4138 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4139 ovs_be16 subtype
; /* NXAST_LEARN. */
4140 ovs_be16 idle_timeout
; /* Idle time before discarding (seconds). */
4141 ovs_be16 hard_timeout
; /* Max time before discarding (seconds). */
4142 ovs_be16 priority
; /* Priority level of flow entry. */
4143 ovs_be64 cookie
; /* Cookie for new flow. */
4144 ovs_be16 flags
; /* NX_LEARN_F_*. */
4145 uint8_t table_id
; /* Table to insert flow entry. */
4146 uint8_t pad
; /* Must be zero. */
4147 ovs_be16 fin_idle_timeout
; /* Idle timeout after FIN, if nonzero. */
4148 ovs_be16 fin_hard_timeout
; /* Hard timeout after FIN, if nonzero. */
4149 /* Followed by a sequence of flow_mod_spec elements, as described above,
4150 * until the end of the action is reached. */
4152 OFP_ASSERT(sizeof(struct nx_action_learn
) == 32);
4155 get_be16(const void **pp
)
4157 const ovs_be16
*p
= *pp
;
4158 ovs_be16 value
= *p
;
4164 get_be32(const void **pp
)
4166 const ovs_be32
*p
= *pp
;
4167 ovs_be32 value
= get_unaligned_be32(p
);
4173 get_subfield(int n_bits
, const void **p
, struct mf_subfield
*sf
)
4175 sf
->field
= mf_from_nxm_header(ntohl(get_be32(p
)));
4176 sf
->ofs
= ntohs(get_be16(p
));
4177 sf
->n_bits
= n_bits
;
4181 learn_min_len(uint16_t header
)
4183 int n_bits
= header
& NX_LEARN_N_BITS_MASK
;
4184 int src_type
= header
& NX_LEARN_SRC_MASK
;
4185 int dst_type
= header
& NX_LEARN_DST_MASK
;
4186 unsigned int min_len
;
4189 if (src_type
== NX_LEARN_SRC_FIELD
) {
4190 min_len
+= sizeof(ovs_be32
); /* src_field */
4191 min_len
+= sizeof(ovs_be16
); /* src_ofs */
4193 min_len
+= DIV_ROUND_UP(n_bits
, 16);
4195 if (dst_type
== NX_LEARN_DST_MATCH
||
4196 dst_type
== NX_LEARN_DST_LOAD
) {
4197 min_len
+= sizeof(ovs_be32
); /* dst_field */
4198 min_len
+= sizeof(ovs_be16
); /* dst_ofs */
4203 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
4204 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
4206 decode_NXAST_RAW_LEARN(const struct nx_action_learn
*nal
,
4207 enum ofp_version ofp_version OVS_UNUSED
,
4208 struct ofpbuf
*ofpacts
)
4210 struct ofpact_learn
*learn
;
4211 const void *p
, *end
;
4214 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4217 learn
= ofpact_put_LEARN(ofpacts
);
4219 learn
->idle_timeout
= ntohs(nal
->idle_timeout
);
4220 learn
->hard_timeout
= ntohs(nal
->hard_timeout
);
4221 learn
->priority
= ntohs(nal
->priority
);
4222 learn
->cookie
= nal
->cookie
;
4223 learn
->table_id
= nal
->table_id
;
4224 learn
->fin_idle_timeout
= ntohs(nal
->fin_idle_timeout
);
4225 learn
->fin_hard_timeout
= ntohs(nal
->fin_hard_timeout
);
4227 learn
->flags
= ntohs(nal
->flags
);
4228 if (learn
->flags
& ~(NX_LEARN_F_SEND_FLOW_REM
|
4229 NX_LEARN_F_DELETE_LEARNED
)) {
4230 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4233 if (learn
->table_id
== 0xff) {
4234 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4237 end
= (char *) nal
+ ntohs(nal
->len
);
4238 for (p
= nal
+ 1; p
!= end
; ) {
4239 struct ofpact_learn_spec
*spec
;
4240 uint16_t header
= ntohs(get_be16(&p
));
4246 spec
= ofpbuf_put_zeros(ofpacts
, sizeof *spec
);
4247 learn
= ofpacts
->header
;
4250 spec
->src_type
= header
& NX_LEARN_SRC_MASK
;
4251 spec
->dst_type
= header
& NX_LEARN_DST_MASK
;
4252 spec
->n_bits
= header
& NX_LEARN_N_BITS_MASK
;
4254 /* Check for valid src and dst type combination. */
4255 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
4256 spec
->dst_type
== NX_LEARN_DST_LOAD
||
4257 (spec
->dst_type
== NX_LEARN_DST_OUTPUT
&&
4258 spec
->src_type
== NX_LEARN_SRC_FIELD
)) {
4261 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4264 /* Check that the arguments don't overrun the end of the action. */
4265 if ((char *) end
- (char *) p
< learn_min_len(header
)) {
4266 return OFPERR_OFPBAC_BAD_LEN
;
4269 /* Get the source. */
4270 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
4271 get_subfield(spec
->n_bits
, &p
, &spec
->src
);
4273 int p_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
4275 bitwise_copy(p
, p_bytes
, 0,
4276 &spec
->src_imm
, sizeof spec
->src_imm
, 0,
4278 p
= (const uint8_t *) p
+ p_bytes
;
4281 /* Get the destination. */
4282 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
4283 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
4284 get_subfield(spec
->n_bits
, &p
, &spec
->dst
);
4287 ofpact_finish_LEARN(ofpacts
, &learn
);
4289 if (!is_all_zeros(p
, (char *) end
- (char *) p
)) {
4290 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4297 put_be16(struct ofpbuf
*b
, ovs_be16 x
)
4299 ofpbuf_put(b
, &x
, sizeof x
);
4303 put_be32(struct ofpbuf
*b
, ovs_be32 x
)
4305 ofpbuf_put(b
, &x
, sizeof x
);
4309 put_u16(struct ofpbuf
*b
, uint16_t x
)
4311 put_be16(b
, htons(x
));
4315 put_u32(struct ofpbuf
*b
, uint32_t x
)
4317 put_be32(b
, htonl(x
));
4321 encode_LEARN(const struct ofpact_learn
*learn
,
4322 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4324 const struct ofpact_learn_spec
*spec
;
4325 struct nx_action_learn
*nal
;
4328 start_ofs
= out
->size
;
4329 nal
= put_NXAST_LEARN(out
);
4330 nal
->idle_timeout
= htons(learn
->idle_timeout
);
4331 nal
->hard_timeout
= htons(learn
->hard_timeout
);
4332 nal
->fin_idle_timeout
= htons(learn
->fin_idle_timeout
);
4333 nal
->fin_hard_timeout
= htons(learn
->fin_hard_timeout
);
4334 nal
->priority
= htons(learn
->priority
);
4335 nal
->cookie
= learn
->cookie
;
4336 nal
->flags
= htons(learn
->flags
);
4337 nal
->table_id
= learn
->table_id
;
4339 for (spec
= learn
->specs
; spec
< &learn
->specs
[learn
->n_specs
]; spec
++) {
4340 put_u16(out
, spec
->n_bits
| spec
->dst_type
| spec
->src_type
);
4342 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
4343 put_u32(out
, mf_nxm_header(spec
->src
.field
->id
));
4344 put_u16(out
, spec
->src
.ofs
);
4346 size_t n_dst_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
4347 uint8_t *bits
= ofpbuf_put_zeros(out
, n_dst_bytes
);
4348 bitwise_copy(&spec
->src_imm
, sizeof spec
->src_imm
, 0,
4349 bits
, n_dst_bytes
, 0,
4353 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
4354 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
4355 put_u32(out
, mf_nxm_header(spec
->dst
.field
->id
));
4356 put_u16(out
, spec
->dst
.ofs
);
4360 pad_ofpat(out
, start_ofs
);
4363 static char * OVS_WARN_UNUSED_RESULT
4364 parse_LEARN(char *arg
, struct ofpbuf
*ofpacts
,
4365 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4367 return learn_parse(arg
, ofpacts
);
4371 format_LEARN(const struct ofpact_learn
*a
, struct ds
*s
)
4376 /* Action structure for NXAST_CONJUNCTION. */
4377 struct nx_action_conjunction
{
4378 ovs_be16 type
; /* OFPAT_VENDOR. */
4379 ovs_be16 len
; /* At least 16. */
4380 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4381 ovs_be16 subtype
; /* See enum ofp_raw_action_type. */
4386 OFP_ASSERT(sizeof(struct nx_action_conjunction
) == 16);
4389 add_conjunction(struct ofpbuf
*out
,
4390 uint32_t id
, uint8_t clause
, uint8_t n_clauses
)
4392 struct ofpact_conjunction
*oc
;
4394 oc
= ofpact_put_CONJUNCTION(out
);
4396 oc
->clause
= clause
;
4397 oc
->n_clauses
= n_clauses
;
4401 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction
*nac
,
4402 enum ofp_version ofp_version OVS_UNUSED
,
4405 if (nac
->n_clauses
< 2 || nac
->n_clauses
> 64
4406 || nac
->clause
>= nac
->n_clauses
) {
4407 return OFPERR_NXBAC_BAD_CONJUNCTION
;
4409 add_conjunction(out
, ntohl(nac
->id
), nac
->clause
, nac
->n_clauses
);
4415 encode_CONJUNCTION(const struct ofpact_conjunction
*oc
,
4416 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4418 struct nx_action_conjunction
*nac
= put_NXAST_CONJUNCTION(out
);
4419 nac
->clause
= oc
->clause
;
4420 nac
->n_clauses
= oc
->n_clauses
;
4421 nac
->id
= htonl(oc
->id
);
4425 format_CONJUNCTION(const struct ofpact_conjunction
*oc
, struct ds
*s
)
4427 ds_put_format(s
, "%sconjunction(%s%"PRIu32
",%"PRIu8
"/%"PRIu8
"%s)%s",
4428 colors
.paren
, colors
.end
,
4429 oc
->id
, oc
->clause
+ 1, oc
->n_clauses
,
4430 colors
.paren
, colors
.end
);
4433 static char * OVS_WARN_UNUSED_RESULT
4434 parse_CONJUNCTION(const char *arg
, struct ofpbuf
*ofpacts
,
4435 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4442 if (!ovs_scan(arg
, "%"SCNi32
" , %"SCNu8
" / %"SCNu8
" %n",
4443 &id
, &clause
, &n_clauses
, &n
) || n
!= strlen(arg
)) {
4444 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4447 if (n_clauses
< 2) {
4448 return xstrdup("conjunction must have at least 2 clauses");
4449 } else if (n_clauses
> 64) {
4450 return xstrdup("conjunction must have at most 64 clauses");
4451 } else if (clause
< 1) {
4452 return xstrdup("clause index must be positive");
4453 } else if (clause
> n_clauses
) {
4454 return xstrdup("clause index must be less than or equal to "
4455 "number of clauses");
4458 add_conjunction(ofpacts
, id
, clause
- 1, n_clauses
);
4462 /* Action structure for NXAST_MULTIPATH.
4464 * This action performs the following steps in sequence:
4466 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4467 * Refer to the definition of "enum nx_mp_fields" for details.
4469 * The 'basis' value is used as a universal hash parameter, that is,
4470 * different values of 'basis' yield different hash functions. The
4471 * particular universal hash function used is implementation-defined.
4473 * The hashed fields' values are drawn from the current state of the
4474 * flow, including all modifications that have been made by actions up to
4477 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4478 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4481 * The output of the algorithm is 'link', an unsigned integer less than
4482 * or equal to 'max_link'.
4484 * Some algorithms use 'arg' as an additional argument.
4486 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4487 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4490 * The switch will reject actions that have an unknown 'fields', or an unknown
4491 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4492 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4493 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4495 struct nx_action_multipath
{
4496 ovs_be16 type
; /* OFPAT_VENDOR. */
4497 ovs_be16 len
; /* Length is 32. */
4498 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4499 ovs_be16 subtype
; /* NXAST_MULTIPATH. */
4501 /* What fields to hash and how. */
4502 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
4503 ovs_be16 basis
; /* Universal hash parameter. */
4506 /* Multipath link choice algorithm to apply to hash value. */
4507 ovs_be16 algorithm
; /* One of NX_MP_ALG_*. */
4508 ovs_be16 max_link
; /* Number of output links, minus 1. */
4509 ovs_be32 arg
; /* Algorithm-specific argument. */
4512 /* Where to store the result. */
4513 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
4514 ovs_be32 dst
; /* Destination. */
4516 OFP_ASSERT(sizeof(struct nx_action_multipath
) == 32);
4519 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath
*nam
,
4520 enum ofp_version ofp_version OVS_UNUSED
,
4523 uint32_t n_links
= ntohs(nam
->max_link
) + 1;
4524 size_t min_n_bits
= log_2_ceil(n_links
);
4525 struct ofpact_multipath
*mp
;
4527 mp
= ofpact_put_MULTIPATH(out
);
4528 mp
->fields
= ntohs(nam
->fields
);
4529 mp
->basis
= ntohs(nam
->basis
);
4530 mp
->algorithm
= ntohs(nam
->algorithm
);
4531 mp
->max_link
= ntohs(nam
->max_link
);
4532 mp
->arg
= ntohl(nam
->arg
);
4533 mp
->dst
.field
= mf_from_nxm_header(ntohl(nam
->dst
));
4534 mp
->dst
.ofs
= nxm_decode_ofs(nam
->ofs_nbits
);
4535 mp
->dst
.n_bits
= nxm_decode_n_bits(nam
->ofs_nbits
);
4537 if (!flow_hash_fields_valid(mp
->fields
)) {
4538 VLOG_WARN_RL(&rl
, "unsupported fields %d", (int) mp
->fields
);
4539 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4540 } else if (mp
->algorithm
!= NX_MP_ALG_MODULO_N
4541 && mp
->algorithm
!= NX_MP_ALG_HASH_THRESHOLD
4542 && mp
->algorithm
!= NX_MP_ALG_HRW
4543 && mp
->algorithm
!= NX_MP_ALG_ITER_HASH
) {
4544 VLOG_WARN_RL(&rl
, "unsupported algorithm %d", (int) mp
->algorithm
);
4545 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4546 } else if (mp
->dst
.n_bits
< min_n_bits
) {
4547 VLOG_WARN_RL(&rl
, "multipath action requires at least %"PRIuSIZE
" bits for "
4548 "%"PRIu32
" links", min_n_bits
, n_links
);
4549 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4552 return multipath_check(mp
, NULL
);
4556 encode_MULTIPATH(const struct ofpact_multipath
*mp
,
4557 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4559 struct nx_action_multipath
*nam
= put_NXAST_MULTIPATH(out
);
4561 nam
->fields
= htons(mp
->fields
);
4562 nam
->basis
= htons(mp
->basis
);
4563 nam
->algorithm
= htons(mp
->algorithm
);
4564 nam
->max_link
= htons(mp
->max_link
);
4565 nam
->arg
= htonl(mp
->arg
);
4566 nam
->ofs_nbits
= nxm_encode_ofs_nbits(mp
->dst
.ofs
, mp
->dst
.n_bits
);
4567 nam
->dst
= htonl(mf_nxm_header(mp
->dst
.field
->id
));
4570 static char * OVS_WARN_UNUSED_RESULT
4571 parse_MULTIPATH(const char *arg
, struct ofpbuf
*ofpacts
,
4572 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4574 return multipath_parse(ofpact_put_MULTIPATH(ofpacts
), arg
);
4578 format_MULTIPATH(const struct ofpact_multipath
*a
, struct ds
*s
)
4580 multipath_format(a
, s
);
4583 /* Action structure for NXAST_NOTE.
4585 * This action has no effect. It is variable length. The switch does not
4586 * attempt to interpret the user-defined 'note' data in any way. A controller
4587 * can use this action to attach arbitrary metadata to a flow.
4589 * This action might go away in the future.
4591 struct nx_action_note
{
4592 ovs_be16 type
; /* OFPAT_VENDOR. */
4593 ovs_be16 len
; /* A multiple of 8, but at least 16. */
4594 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4595 ovs_be16 subtype
; /* NXAST_NOTE. */
4596 uint8_t note
[6]; /* Start of user-defined data. */
4597 /* Possibly followed by additional user-defined data. */
4599 OFP_ASSERT(sizeof(struct nx_action_note
) == 16);
4602 decode_NXAST_RAW_NOTE(const struct nx_action_note
*nan
,
4603 enum ofp_version ofp_version OVS_UNUSED
,
4606 struct ofpact_note
*note
;
4607 unsigned int length
;
4609 length
= ntohs(nan
->len
) - offsetof(struct nx_action_note
, note
);
4610 note
= ofpact_put_NOTE(out
);
4611 note
->length
= length
;
4612 ofpbuf_put(out
, nan
->note
, length
);
4614 ofpact_finish_NOTE(out
, ¬e
);
4620 encode_NOTE(const struct ofpact_note
*note
,
4621 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4623 size_t start_ofs
= out
->size
;
4624 struct nx_action_note
*nan
;
4626 put_NXAST_NOTE(out
);
4627 out
->size
= out
->size
- sizeof nan
->note
;
4629 ofpbuf_put(out
, note
->data
, note
->length
);
4630 pad_ofpat(out
, start_ofs
);
4633 static char * OVS_WARN_UNUSED_RESULT
4634 parse_NOTE(const char *arg
, struct ofpbuf
*ofpacts
,
4635 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4637 size_t start_ofs
= ofpacts
->size
;
4638 ofpact_put_NOTE(ofpacts
);
4639 arg
= ofpbuf_put_hex(ofpacts
, arg
, NULL
);
4641 return xstrdup("bad hex digit in `note' argument");
4643 struct ofpact_note
*note
= ofpbuf_at_assert(ofpacts
, start_ofs
,
4645 note
->length
= ofpacts
->size
- (start_ofs
+ sizeof *note
);
4646 ofpact_finish_NOTE(ofpacts
, ¬e
);
4651 format_NOTE(const struct ofpact_note
*a
, struct ds
*s
)
4653 ds_put_format(s
, "%snote:%s", colors
.param
, colors
.end
);
4654 format_hex_arg(s
, a
->data
, a
->length
);
4660 decode_NXAST_RAW_EXIT(struct ofpbuf
*out
)
4662 ofpact_put_EXIT(out
);
4667 encode_EXIT(const struct ofpact_null
*null OVS_UNUSED
,
4668 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4670 put_NXAST_EXIT(out
);
4673 static char * OVS_WARN_UNUSED_RESULT
4674 parse_EXIT(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
4675 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4677 ofpact_put_EXIT(ofpacts
);
4682 format_EXIT(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
4684 ds_put_format(s
, "%sexit%s", colors
.special
, colors
.end
);
4687 /* Unroll xlate action. */
4690 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate
*unroll OVS_UNUSED
,
4691 enum ofp_version ofp_version OVS_UNUSED
,
4692 struct ofpbuf
*out OVS_UNUSED
)
4697 static char * OVS_WARN_UNUSED_RESULT
4698 parse_UNROLL_XLATE(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts OVS_UNUSED
,
4699 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4706 format_UNROLL_XLATE(const struct ofpact_unroll_xlate
*a
, struct ds
*s
)
4708 ds_put_format(s
, "%sunroll_xlate(%s%stable=%s%"PRIu8
4709 ", %scookie=%s%"PRIu64
"%s)%s",
4710 colors
.paren
, colors
.end
,
4711 colors
.special
, colors
.end
, a
->rule_table_id
,
4712 colors
.param
, colors
.end
, ntohll(a
->rule_cookie
),
4713 colors
.paren
, colors
.end
);
4716 /* Action structure for NXAST_SAMPLE.
4718 * Samples matching packets with the given probability and sends them
4719 * each to the set of collectors identified with the given ID. The
4720 * probability is expressed as a number of packets to be sampled out
4721 * of USHRT_MAX packets, and must be >0.
4723 * When sending packet samples to IPFIX collectors, the IPFIX flow
4724 * record sent for each sampled packet is associated with the given
4725 * observation domain ID and observation point ID. Each IPFIX flow
4726 * record contain the sampled packet's headers when executing this
4727 * rule. If a sampled packet's headers are modified by previous
4728 * actions in the flow, those modified headers are sent. */
4729 struct nx_action_sample
{
4730 ovs_be16 type
; /* OFPAT_VENDOR. */
4731 ovs_be16 len
; /* Length is 24. */
4732 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4733 ovs_be16 subtype
; /* NXAST_SAMPLE. */
4734 ovs_be16 probability
; /* Fraction of packets to sample. */
4735 ovs_be32 collector_set_id
; /* ID of collector set in OVSDB. */
4736 ovs_be32 obs_domain_id
; /* ID of sampling observation domain. */
4737 ovs_be32 obs_point_id
; /* ID of sampling observation point. */
4739 OFP_ASSERT(sizeof(struct nx_action_sample
) == 24);
4741 /* Action structure for NXAST_SAMPLE2.
4743 * This replacement for NXAST_SAMPLE makes it support exporting
4744 * egress tunnel information. */
4745 struct nx_action_sample2
{
4746 ovs_be16 type
; /* OFPAT_VENDOR. */
4747 ovs_be16 len
; /* Length is 32. */
4748 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4749 ovs_be16 subtype
; /* NXAST_SAMPLE. */
4750 ovs_be16 probability
; /* Fraction of packets to sample. */
4751 ovs_be32 collector_set_id
; /* ID of collector set in OVSDB. */
4752 ovs_be32 obs_domain_id
; /* ID of sampling observation domain. */
4753 ovs_be32 obs_point_id
; /* ID of sampling observation point. */
4754 ovs_be16 sampling_port
; /* Sampling port. */
4755 uint8_t pad
[6]; /* Pad to a multiple of 8 bytes */
4757 OFP_ASSERT(sizeof(struct nx_action_sample2
) == 32);
4760 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample
*nas
,
4761 enum ofp_version ofp_version OVS_UNUSED
,
4764 struct ofpact_sample
*sample
;
4766 sample
= ofpact_put_SAMPLE(out
);
4767 sample
->ofpact
.raw
= NXAST_RAW_SAMPLE
;
4768 sample
->probability
= ntohs(nas
->probability
);
4769 sample
->collector_set_id
= ntohl(nas
->collector_set_id
);
4770 sample
->obs_domain_id
= ntohl(nas
->obs_domain_id
);
4771 sample
->obs_point_id
= ntohl(nas
->obs_point_id
);
4772 /* Default value for sampling port is OFPP_NONE */
4773 sample
->sampling_port
= OFPP_NONE
;
4775 if (sample
->probability
== 0) {
4776 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4783 decode_NXAST_RAW_SAMPLE2(const struct nx_action_sample2
*nas
,
4784 enum ofp_version ofp_version OVS_UNUSED
,
4787 struct ofpact_sample
*sample
;
4789 sample
= ofpact_put_SAMPLE(out
);
4790 sample
->ofpact
.raw
= NXAST_RAW_SAMPLE2
;
4791 sample
->probability
= ntohs(nas
->probability
);
4792 sample
->collector_set_id
= ntohl(nas
->collector_set_id
);
4793 sample
->obs_domain_id
= ntohl(nas
->obs_domain_id
);
4794 sample
->obs_point_id
= ntohl(nas
->obs_point_id
);
4795 sample
->sampling_port
= u16_to_ofp(ntohs(nas
->sampling_port
));
4797 if (sample
->probability
== 0) {
4798 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4805 encode_SAMPLE(const struct ofpact_sample
*sample
,
4806 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4808 if (sample
->ofpact
.raw
== NXAST_RAW_SAMPLE2
4809 || sample
->sampling_port
!= OFPP_NONE
) {
4810 struct nx_action_sample2
*nas
= put_NXAST_SAMPLE2(out
);
4811 nas
->probability
= htons(sample
->probability
);
4812 nas
->collector_set_id
= htonl(sample
->collector_set_id
);
4813 nas
->obs_domain_id
= htonl(sample
->obs_domain_id
);
4814 nas
->obs_point_id
= htonl(sample
->obs_point_id
);
4815 nas
->sampling_port
= htons(ofp_to_u16(sample
->sampling_port
));
4817 struct nx_action_sample
*nas
= put_NXAST_SAMPLE(out
);
4818 nas
->probability
= htons(sample
->probability
);
4819 nas
->collector_set_id
= htonl(sample
->collector_set_id
);
4820 nas
->obs_domain_id
= htonl(sample
->obs_domain_id
);
4821 nas
->obs_point_id
= htonl(sample
->obs_point_id
);
4825 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4826 * action to 'ofpacts'.
4828 * Returns NULL if successful, otherwise a malloc()'d string describing the
4829 * error. The caller is responsible for freeing the returned string. */
4830 static char * OVS_WARN_UNUSED_RESULT
4831 parse_SAMPLE(char *arg
, struct ofpbuf
*ofpacts
,
4832 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4834 struct ofpact_sample
*os
= ofpact_put_SAMPLE(ofpacts
);
4835 os
->sampling_port
= OFPP_NONE
;
4838 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
4841 if (!strcmp(key
, "probability")) {
4842 error
= str_to_u16(value
, "probability", &os
->probability
);
4843 if (!error
&& os
->probability
== 0) {
4844 error
= xasprintf("invalid probability value \"%s\"", value
);
4846 } else if (!strcmp(key
, "collector_set_id")) {
4847 error
= str_to_u32(value
, &os
->collector_set_id
);
4848 } else if (!strcmp(key
, "obs_domain_id")) {
4849 error
= str_to_u32(value
, &os
->obs_domain_id
);
4850 } else if (!strcmp(key
, "obs_point_id")) {
4851 error
= str_to_u32(value
, &os
->obs_point_id
);
4852 } else if (!strcmp(key
, "sampling_port")) {
4853 if (!ofputil_port_from_string(value
, &os
->sampling_port
)) {
4854 error
= xasprintf("%s: unknown port", value
);
4857 error
= xasprintf("invalid key \"%s\" in \"sample\" argument",
4864 if (os
->probability
== 0) {
4865 return xstrdup("non-zero \"probability\" must be specified on sample");
4872 format_SAMPLE(const struct ofpact_sample
*a
, struct ds
*s
)
4874 ds_put_format(s
, "%ssample(%s%sprobability=%s%"PRIu16
4875 ",%scollector_set_id=%s%"PRIu32
4876 ",%sobs_domain_id=%s%"PRIu32
4877 ",%sobs_point_id=%s%"PRIu32
,
4878 colors
.paren
, colors
.end
,
4879 colors
.param
, colors
.end
, a
->probability
,
4880 colors
.param
, colors
.end
, a
->collector_set_id
,
4881 colors
.param
, colors
.end
, a
->obs_domain_id
,
4882 colors
.param
, colors
.end
, a
->obs_point_id
);
4883 if (a
->sampling_port
!= OFPP_NONE
) {
4884 ds_put_format(s
, ",%ssampling_port=%s%"PRIu16
,
4885 colors
.param
, colors
.end
, a
->sampling_port
);
4887 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
4890 /* debug_recirc instruction. */
4892 static bool enable_debug
;
4895 ofpact_dummy_enable(void)
4897 enable_debug
= true;
4901 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf
*out
)
4903 if (!enable_debug
) {
4904 return OFPERR_OFPBAC_BAD_VENDOR_TYPE
;
4907 ofpact_put_DEBUG_RECIRC(out
);
4912 encode_DEBUG_RECIRC(const struct ofpact_null
*n OVS_UNUSED
,
4913 enum ofp_version ofp_version OVS_UNUSED
,
4916 put_NXAST_DEBUG_RECIRC(out
);
4919 static char * OVS_WARN_UNUSED_RESULT
4920 parse_DEBUG_RECIRC(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
4921 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4923 ofpact_put_DEBUG_RECIRC(ofpacts
);
4928 format_DEBUG_RECIRC(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
4930 ds_put_format(s
, "%sdebug_recirc%s", colors
.value
, colors
.end
);
4933 /* Action structure for NXAST_CT.
4935 * Pass traffic to the connection tracker.
4937 * There are two important concepts to understanding the connection tracking
4938 * interface: Packet state and Connection state. Packets may be "Untracked" or
4939 * "Tracked". Connections may be "Uncommitted" or "Committed".
4943 * Untracked packets have not yet passed through the connection tracker,
4944 * and the connection state for such packets is unknown. In most cases,
4945 * packets entering the OpenFlow pipeline will initially be in the
4946 * untracked state. Untracked packets may become tracked by executing
4947 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4948 * about the connection available, in particular the connection state.
4950 * Tracked packets have previously passed through the connection tracker.
4951 * These packets will remain tracked through until the end of the OpenFlow
4952 * pipeline. Tracked packets which have NXAST_CT executed with a
4953 * "recirc_table" specified will return to the tracked state.
4955 * The packet state is only significant for the duration of packet
4956 * processing within the OpenFlow pipeline.
4958 * - Connection State:
4960 * Multiple packets may be associated with a single connection. Initially,
4961 * all connections are uncommitted. The connection state corresponding to
4962 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4964 * Uncommitted connections have no state stored about them. Uncommitted
4965 * connections may transition into the committed state by executing
4966 * NXAST_CT with the NX_CT_F_COMMIT flag.
4968 * Once a connection becomes committed, information may be gathered about
4969 * the connection by passing subsequent packets through the connection
4970 * tracker, and the state of the connection will be stored beyond the
4971 * lifetime of packet processing.
4973 * Connections may transition back into the uncommitted state due to
4974 * external timers, or due to the contents of packets that are sent to the
4975 * connection tracker. This behaviour is outside of the scope of the
4976 * OpenFlow interface.
4978 * The "zone" specifies a context within which the tracking is done:
4980 * The connection tracking zone is a 16-bit number. Each zone is an
4981 * independent connection tracking context. The connection state for each
4982 * connection is completely separate for each zone, so if a connection
4983 * is committed to zone A, then it will remain uncommitted in zone B.
4984 * If NXAST_CT is executed with the same zone multiple times, later
4985 * executions have no effect.
4987 * If 'zone_src' is nonzero, this specifies that the zone should be
4988 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4989 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4990 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4991 * are the same as the acceptable nxm_header values for the 'src' field of
4994 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4995 * connection tracking zone.
4997 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4999 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
5000 * packet will be logically cloned prior to executing this action. One
5001 * copy will be sent to the connection tracker, then will be re-injected
5002 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
5003 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
5004 * fields will be populated. The original instance of the packet will
5005 * continue the current actions list. This can be thought of as similar to
5006 * the effect of the "output" action: One copy is sent out (in this case,
5007 * to the connection tracker), but the current copy continues processing.
5009 * It is strongly recommended that this table is later than the current
5010 * table, to prevent loops.
5012 * The "alg" attaches protocol-specific behaviour to this action:
5014 * The ALG is a 16-bit number which specifies that additional
5015 * processing should be applied to this traffic.
5017 * Protocol | Value | Meaning
5018 * --------------------------------------------------------------------
5019 * None | 0 | No protocol-specific behaviour.
5020 * FTP | 21 | Parse FTP control connections and observe the
5021 * | | negotiation of related data connections.
5022 * Other | Other | Unsupported protocols.
5024 * By way of example, if FTP control connections have this action applied
5025 * with the ALG set to FTP (21), then the connection tracker will observe
5026 * the negotiation of data connections. This allows the connection
5027 * tracker to identify subsequent data connections as "related" to this
5028 * existing connection. The "related" flag will be populated in the
5029 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
5032 * Zero or more actions may immediately follow this action. These actions will
5033 * be executed within the context of the connection tracker, and they require
5034 * the NX_CT_F_COMMIT flag to be set.
5036 struct nx_action_conntrack
{
5037 ovs_be16 type
; /* OFPAT_VENDOR. */
5038 ovs_be16 len
; /* At least 24. */
5039 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5040 ovs_be16 subtype
; /* NXAST_CT. */
5041 ovs_be16 flags
; /* Zero or more NX_CT_F_* flags.
5042 * Unspecified flag bits must be zero. */
5043 ovs_be32 zone_src
; /* Connection tracking context. */
5045 ovs_be16 zone_ofs_nbits
;/* Range to use from source field. */
5046 ovs_be16 zone_imm
; /* Immediate value for zone. */
5048 uint8_t recirc_table
; /* Recirculate to a specific table, or
5049 NX_CT_RECIRC_NONE for no recirculation. */
5050 uint8_t pad
[3]; /* Zeroes */
5051 ovs_be16 alg
; /* Well-known port number for the protocol.
5052 * 0 indicates no ALG is required. */
5053 /* Followed by a sequence of zero or more OpenFlow actions. The length of
5054 * these is included in 'len'. */
5056 OFP_ASSERT(sizeof(struct nx_action_conntrack
) == 24);
5059 decode_ct_zone(const struct nx_action_conntrack
*nac
,
5060 struct ofpact_conntrack
*out
)
5062 if (nac
->zone_src
) {
5065 out
->zone_src
.field
= mf_from_nxm_header(ntohl(nac
->zone_src
));
5066 out
->zone_src
.ofs
= nxm_decode_ofs(nac
->zone_ofs_nbits
);
5067 out
->zone_src
.n_bits
= nxm_decode_n_bits(nac
->zone_ofs_nbits
);
5068 error
= mf_check_src(&out
->zone_src
, NULL
);
5073 if (out
->zone_src
.n_bits
!= 16) {
5074 VLOG_WARN_RL(&rl
, "zone n_bits %d not within valid range [16..16]",
5075 out
->zone_src
.n_bits
);
5076 return OFPERR_OFPBAC_BAD_SET_LEN
;
5079 out
->zone_src
.field
= NULL
;
5080 out
->zone_imm
= ntohs(nac
->zone_imm
);
5087 decode_NXAST_RAW_CT(const struct nx_action_conntrack
*nac
,
5088 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5090 const size_t ct_offset
= ofpacts_pull(out
);
5091 struct ofpact_conntrack
*conntrack
= ofpact_put_CT(out
);
5092 conntrack
->flags
= ntohs(nac
->flags
);
5094 int error
= decode_ct_zone(nac
, conntrack
);
5098 conntrack
->recirc_table
= nac
->recirc_table
;
5099 conntrack
->alg
= ntohs(nac
->alg
);
5101 ofpbuf_pull(out
, sizeof(*conntrack
));
5103 struct ofpbuf openflow
= ofpbuf_const_initializer(
5104 nac
+ 1, ntohs(nac
->len
) - sizeof(*nac
));
5105 error
= ofpacts_pull_openflow_actions__(&openflow
, openflow
.size
,
5107 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
5113 conntrack
= ofpbuf_push_uninit(out
, sizeof(*conntrack
));
5114 out
->header
= &conntrack
->ofpact
;
5115 ofpact_finish_CT(out
, &conntrack
);
5117 if (conntrack
->ofpact
.len
> sizeof(*conntrack
)
5118 && !(conntrack
->flags
& NX_CT_F_COMMIT
)) {
5119 const struct ofpact
*a
;
5120 size_t ofpacts_len
= conntrack
->ofpact
.len
- sizeof(*conntrack
);
5122 OFPACT_FOR_EACH (a
, conntrack
->actions
, ofpacts_len
) {
5123 if (a
->type
!= OFPACT_NAT
|| ofpact_get_NAT(a
)->flags
5124 || ofpact_get_NAT(a
)->range_af
!= AF_UNSPEC
) {
5125 VLOG_WARN_RL(&rl
, "CT action requires commit flag if actions "
5126 "other than NAT without arguments are specified.");
5127 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
5134 ofpbuf_push_uninit(out
, ct_offset
);
5139 encode_CT(const struct ofpact_conntrack
*conntrack
,
5140 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5142 struct nx_action_conntrack
*nac
;
5143 const size_t ofs
= out
->size
;
5146 nac
= put_NXAST_CT(out
);
5147 nac
->flags
= htons(conntrack
->flags
);
5148 if (conntrack
->zone_src
.field
) {
5149 nac
->zone_src
= htonl(mf_nxm_header(conntrack
->zone_src
.field
->id
));
5150 nac
->zone_ofs_nbits
= nxm_encode_ofs_nbits(conntrack
->zone_src
.ofs
,
5151 conntrack
->zone_src
.n_bits
);
5153 nac
->zone_src
= htonl(0);
5154 nac
->zone_imm
= htons(conntrack
->zone_imm
);
5156 nac
->recirc_table
= conntrack
->recirc_table
;
5157 nac
->alg
= htons(conntrack
->alg
);
5159 len
= ofpacts_put_openflow_actions(conntrack
->actions
,
5160 ofpact_ct_get_action_len(conntrack
),
5162 len
+= sizeof(*nac
);
5163 nac
= ofpbuf_at(out
, ofs
, sizeof(*nac
));
5164 nac
->len
= htons(len
);
5167 static char * OVS_WARN_UNUSED_RESULT
parse_NAT(char *arg
, struct ofpbuf
*,
5168 enum ofputil_protocol
* OVS_UNUSED
);
5170 /* Parses 'arg' as the argument to a "ct" action, and appends such an
5171 * action to 'ofpacts'.
5173 * Returns NULL if successful, otherwise a malloc()'d string describing the
5174 * error. The caller is responsible for freeing the returned string. */
5175 static char * OVS_WARN_UNUSED_RESULT
5176 parse_CT(char *arg
, struct ofpbuf
*ofpacts
,
5177 enum ofputil_protocol
*usable_protocols
)
5179 const size_t ct_offset
= ofpacts_pull(ofpacts
);
5180 struct ofpact_conntrack
*oc
;
5184 oc
= ofpact_put_CT(ofpacts
);
5186 oc
->recirc_table
= NX_CT_RECIRC_NONE
;
5187 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
5188 if (!strcmp(key
, "commit")) {
5189 oc
->flags
|= NX_CT_F_COMMIT
;
5190 } else if (!strcmp(key
, "table")) {
5191 error
= str_to_u8(value
, "recirc_table", &oc
->recirc_table
);
5192 if (!error
&& oc
->recirc_table
== NX_CT_RECIRC_NONE
) {
5193 error
= xasprintf("invalid table %#"PRIx16
, oc
->recirc_table
);
5195 } else if (!strcmp(key
, "zone")) {
5196 error
= str_to_u16(value
, "zone", &oc
->zone_imm
);
5200 error
= mf_parse_subfield(&oc
->zone_src
, value
);
5205 } else if (!strcmp(key
, "alg")) {
5206 error
= str_to_connhelper(value
, &oc
->alg
);
5207 } else if (!strcmp(key
, "nat")) {
5208 const size_t nat_offset
= ofpacts_pull(ofpacts
);
5210 error
= parse_NAT(value
, ofpacts
, usable_protocols
);
5211 /* Update CT action pointer and length. */
5212 ofpacts
->header
= ofpbuf_push_uninit(ofpacts
, nat_offset
);
5213 oc
= ofpacts
->header
;
5214 } else if (!strcmp(key
, "exec")) {
5215 /* Hide existing actions from ofpacts_parse_copy(), so the
5216 * nesting can be handled transparently. */
5217 enum ofputil_protocol usable_protocols2
;
5218 const size_t exec_offset
= ofpacts_pull(ofpacts
);
5220 /* Initializes 'usable_protocol2', fold it back to
5221 * '*usable_protocols' afterwards, so that we do not lose
5222 * restrictions already in there. */
5223 error
= ofpacts_parse_copy(value
, ofpacts
, &usable_protocols2
,
5225 *usable_protocols
&= usable_protocols2
;
5226 ofpacts
->header
= ofpbuf_push_uninit(ofpacts
, exec_offset
);
5227 oc
= ofpacts
->header
;
5229 error
= xasprintf("invalid argument to \"ct\" action: `%s'", key
);
5236 ofpact_finish_CT(ofpacts
, &oc
);
5237 ofpbuf_push_uninit(ofpacts
, ct_offset
);
5242 format_alg(int port
, struct ds
*s
)
5244 if (port
== IPPORT_FTP
) {
5245 ds_put_format(s
, "%salg=%sftp,", colors
.param
, colors
.end
);
5247 ds_put_format(s
, "%salg=%s%d,", colors
.param
, colors
.end
, port
);
5251 static void format_NAT(const struct ofpact_nat
*a
, struct ds
*ds
);
5254 format_CT(const struct ofpact_conntrack
*a
, struct ds
*s
)
5256 ds_put_format(s
, "%sct(%s", colors
.paren
, colors
.end
);
5257 if (a
->flags
& NX_CT_F_COMMIT
) {
5258 ds_put_format(s
, "%scommit%s,", colors
.value
, colors
.end
);
5260 if (a
->recirc_table
!= NX_CT_RECIRC_NONE
) {
5261 ds_put_format(s
, "%stable=%s%"PRIu8
",",
5262 colors
.special
, colors
.end
, a
->recirc_table
);
5264 if (a
->zone_src
.field
) {
5265 ds_put_format(s
, "%szone=%s", colors
.param
, colors
.end
);
5266 mf_format_subfield(&a
->zone_src
, s
);
5267 ds_put_char(s
, ',');
5268 } else if (a
->zone_imm
) {
5269 ds_put_format(s
, "%szone=%s%"PRIu16
",",
5270 colors
.param
, colors
.end
, a
->zone_imm
);
5272 /* If the first action is a NAT action, format it outside of the 'exec'
5274 const struct ofpact
*action
= a
->actions
;
5275 size_t actions_len
= ofpact_ct_get_action_len(a
);
5276 if (actions_len
&& action
->type
== OFPACT_NAT
) {
5277 format_NAT(ofpact_get_NAT(action
), s
);
5278 ds_put_char(s
, ',');
5279 actions_len
-= OFPACT_ALIGN(action
->len
);
5280 action
= ofpact_next(action
);
5283 ds_put_format(s
, "%sexec(%s", colors
.paren
, colors
.end
);
5284 ofpacts_format(action
, actions_len
, s
);
5285 ds_put_format(s
, "%s),%s", colors
.paren
, colors
.end
);
5287 format_alg(a
->alg
, s
);
5289 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
5294 /* Which optional fields are present? */
5296 NX_NAT_RANGE_IPV4_MIN
= 1 << 0, /* ovs_be32 */
5297 NX_NAT_RANGE_IPV4_MAX
= 1 << 1, /* ovs_be32 */
5298 NX_NAT_RANGE_IPV6_MIN
= 1 << 2, /* struct in6_addr */
5299 NX_NAT_RANGE_IPV6_MAX
= 1 << 3, /* struct in6_addr */
5300 NX_NAT_RANGE_PROTO_MIN
= 1 << 4, /* ovs_be16 */
5301 NX_NAT_RANGE_PROTO_MAX
= 1 << 5, /* ovs_be16 */
5304 /* Action structure for NXAST_NAT. */
5305 struct nx_action_nat
{
5306 ovs_be16 type
; /* OFPAT_VENDOR. */
5307 ovs_be16 len
; /* At least 16. */
5308 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5309 ovs_be16 subtype
; /* NXAST_NAT. */
5310 uint8_t pad
[2]; /* Must be zero. */
5311 ovs_be16 flags
; /* Zero or more NX_NAT_F_* flags.
5312 * Unspecified flag bits must be zero. */
5313 ovs_be16 range_present
; /* NX_NAT_RANGE_* */
5314 /* Followed by optional parameters as specified by 'range_present' */
5316 OFP_ASSERT(sizeof(struct nx_action_nat
) == 16);
5319 encode_NAT(const struct ofpact_nat
*nat
,
5320 enum ofp_version ofp_version OVS_UNUSED
,
5323 struct nx_action_nat
*nan
;
5324 const size_t ofs
= out
->size
;
5325 uint16_t range_present
= 0;
5327 nan
= put_NXAST_NAT(out
);
5328 nan
->flags
= htons(nat
->flags
);
5329 if (nat
->range_af
== AF_INET
) {
5330 if (nat
->range
.addr
.ipv4
.min
) {
5331 ovs_be32
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
5332 *min
= nat
->range
.addr
.ipv4
.min
;
5333 range_present
|= NX_NAT_RANGE_IPV4_MIN
;
5335 if (nat
->range
.addr
.ipv4
.max
) {
5336 ovs_be32
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
5337 *max
= nat
->range
.addr
.ipv4
.max
;
5338 range_present
|= NX_NAT_RANGE_IPV4_MAX
;
5340 } else if (nat
->range_af
== AF_INET6
) {
5341 if (!ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.min
)) {
5342 struct in6_addr
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
5343 *min
= nat
->range
.addr
.ipv6
.min
;
5344 range_present
|= NX_NAT_RANGE_IPV6_MIN
;
5346 if (!ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.max
)) {
5347 struct in6_addr
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
5348 *max
= nat
->range
.addr
.ipv6
.max
;
5349 range_present
|= NX_NAT_RANGE_IPV6_MAX
;
5352 if (nat
->range_af
!= AF_UNSPEC
) {
5353 if (nat
->range
.proto
.min
) {
5354 ovs_be16
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
5355 *min
= htons(nat
->range
.proto
.min
);
5356 range_present
|= NX_NAT_RANGE_PROTO_MIN
;
5358 if (nat
->range
.proto
.max
) {
5359 ovs_be16
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
5360 *max
= htons(nat
->range
.proto
.max
);
5361 range_present
|= NX_NAT_RANGE_PROTO_MAX
;
5364 pad_ofpat(out
, ofs
);
5365 nan
= ofpbuf_at(out
, ofs
, sizeof *nan
);
5366 nan
->range_present
= htons(range_present
);
5370 decode_NXAST_RAW_NAT(const struct nx_action_nat
*nan
,
5371 enum ofp_version ofp_version OVS_UNUSED
,
5374 struct ofpact_nat
*nat
;
5375 uint16_t range_present
= ntohs(nan
->range_present
);
5376 const char *opts
= (char *)(nan
+ 1);
5377 uint16_t len
= ntohs(nan
->len
) - sizeof *nan
;
5379 nat
= ofpact_put_NAT(out
);
5380 nat
->flags
= ntohs(nan
->flags
);
5382 /* Check for unknown or mutually exclusive flags. */
5383 if ((nat
->flags
& ~NX_NAT_F_MASK
)
5384 || (nat
->flags
& NX_NAT_F_SRC
&& nat
->flags
& NX_NAT_F_DST
)
5385 || (nat
->flags
& NX_NAT_F_PROTO_HASH
5386 && nat
->flags
& NX_NAT_F_PROTO_RANDOM
)) {
5387 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5390 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
5391 (LEN >= sizeof(TYPE) \
5392 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
5393 SRC += sizeof(TYPE)) \
5396 nat
->range_af
= AF_UNSPEC
;
5397 if (range_present
& NX_NAT_RANGE_IPV4_MIN
) {
5398 if (range_present
& (NX_NAT_RANGE_IPV6_MIN
| NX_NAT_RANGE_IPV6_MAX
)) {
5399 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5402 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv4
.min
, opts
, len
, ovs_be32
)
5403 || !nat
->range
.addr
.ipv4
.min
) {
5404 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5407 nat
->range_af
= AF_INET
;
5409 if (range_present
& NX_NAT_RANGE_IPV4_MAX
) {
5410 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv4
.max
, opts
, len
,
5412 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5414 if (ntohl(nat
->range
.addr
.ipv4
.max
)
5415 < ntohl(nat
->range
.addr
.ipv4
.min
)) {
5416 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5419 } else if (range_present
& NX_NAT_RANGE_IPV4_MAX
) {
5420 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5421 } else if (range_present
& NX_NAT_RANGE_IPV6_MIN
) {
5422 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv6
.min
, opts
, len
,
5424 || ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.min
)) {
5425 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5428 nat
->range_af
= AF_INET6
;
5430 if (range_present
& NX_NAT_RANGE_IPV6_MAX
) {
5431 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv6
.max
, opts
, len
,
5433 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5435 if (memcmp(&nat
->range
.addr
.ipv6
.max
, &nat
->range
.addr
.ipv6
.min
,
5436 sizeof(struct in6_addr
)) < 0) {
5437 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5440 } else if (range_present
& NX_NAT_RANGE_IPV6_MAX
) {
5441 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5444 if (range_present
& NX_NAT_RANGE_PROTO_MIN
) {
5447 if (nat
->range_af
== AF_UNSPEC
) {
5448 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5450 if (!NX_NAT_GET_OPT(&proto
, opts
, len
, ovs_be16
) || proto
== 0) {
5451 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5453 nat
->range
.proto
.min
= ntohs(proto
);
5454 if (range_present
& NX_NAT_RANGE_PROTO_MAX
) {
5455 if (!NX_NAT_GET_OPT(&proto
, opts
, len
, ovs_be16
)) {
5456 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5458 nat
->range
.proto
.max
= ntohs(proto
);
5459 if (nat
->range
.proto
.max
< nat
->range
.proto
.min
) {
5460 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5463 } else if (range_present
& NX_NAT_RANGE_PROTO_MAX
) {
5464 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5471 format_NAT(const struct ofpact_nat
*a
, struct ds
*ds
)
5473 ds_put_format(ds
, "%snat%s", colors
.paren
, colors
.end
);
5475 if (a
->flags
& (NX_NAT_F_SRC
| NX_NAT_F_DST
)) {
5476 ds_put_format(ds
, "%s(%s", colors
.paren
, colors
.end
);
5477 ds_put_format(ds
, a
->flags
& NX_NAT_F_SRC
? "%ssrc%s" : "%sdst%s",
5478 colors
.param
, colors
.end
);
5480 if (a
->range_af
!= AF_UNSPEC
) {
5481 ds_put_format(ds
, "%s=%s", colors
.param
, colors
.end
);
5483 if (a
->range_af
== AF_INET
) {
5484 ds_put_format(ds
, IP_FMT
, IP_ARGS(a
->range
.addr
.ipv4
.min
));
5486 if (a
->range
.addr
.ipv4
.max
5487 && a
->range
.addr
.ipv4
.max
!= a
->range
.addr
.ipv4
.min
) {
5488 ds_put_format(ds
, "-"IP_FMT
,
5489 IP_ARGS(a
->range
.addr
.ipv4
.max
));
5491 } else if (a
->range_af
== AF_INET6
) {
5492 ipv6_format_addr_bracket(&a
->range
.addr
.ipv6
.min
, ds
,
5493 a
->range
.proto
.min
);
5495 if (!ipv6_mask_is_any(&a
->range
.addr
.ipv6
.max
)
5496 && memcmp(&a
->range
.addr
.ipv6
.max
, &a
->range
.addr
.ipv6
.min
,
5497 sizeof(struct in6_addr
)) != 0) {
5498 ds_put_char(ds
, '-');
5499 ipv6_format_addr_bracket(&a
->range
.addr
.ipv6
.max
, ds
,
5500 a
->range
.proto
.min
);
5503 if (a
->range
.proto
.min
) {
5504 ds_put_char(ds
, ':');
5505 ds_put_format(ds
, "%"PRIu16
, a
->range
.proto
.min
);
5507 if (a
->range
.proto
.max
5508 && a
->range
.proto
.max
!= a
->range
.proto
.min
) {
5509 ds_put_format(ds
, "-%"PRIu16
, a
->range
.proto
.max
);
5512 ds_put_char(ds
, ',');
5514 if (a
->flags
& NX_NAT_F_PERSISTENT
) {
5515 ds_put_format(ds
, "%spersistent%s,",
5516 colors
.value
, colors
.end
);
5518 if (a
->flags
& NX_NAT_F_PROTO_HASH
) {
5519 ds_put_format(ds
, "%shash%s,", colors
.value
, colors
.end
);
5521 if (a
->flags
& NX_NAT_F_PROTO_RANDOM
) {
5522 ds_put_format(ds
, "%srandom%s,", colors
.value
, colors
.end
);
5526 ds_put_format(ds
, "%s)%s", colors
.paren
, colors
.end
);
5530 static char * OVS_WARN_UNUSED_RESULT
5531 str_to_nat_range(const char *s
, struct ofpact_nat
*on
)
5533 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
5536 on
->range_af
= AF_UNSPEC
;
5537 if (ovs_scan_len(s
, &n
, IP_SCAN_FMT
,
5538 IP_SCAN_ARGS(&on
->range
.addr
.ipv4
.min
))) {
5539 on
->range_af
= AF_INET
;
5543 if (!ovs_scan_len(s
, &n
, IP_SCAN_FMT
,
5544 IP_SCAN_ARGS(&on
->range
.addr
.ipv4
.max
))
5545 || (ntohl(on
->range
.addr
.ipv4
.max
)
5546 < ntohl(on
->range
.addr
.ipv4
.min
))) {
5550 } else if ((ovs_scan_len(s
, &n
, IPV6_SCAN_FMT
, ipv6_s
)
5551 || ovs_scan_len(s
, &n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
5552 && inet_pton(AF_INET6
, ipv6_s
, &on
->range
.addr
.ipv6
.min
) == 1) {
5553 on
->range_af
= AF_INET6
;
5557 if (!(ovs_scan_len(s
, &n
, IPV6_SCAN_FMT
, ipv6_s
)
5558 || ovs_scan_len(s
, &n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
5559 || inet_pton(AF_INET6
, ipv6_s
, &on
->range
.addr
.ipv6
.max
) != 1
5560 || memcmp(&on
->range
.addr
.ipv6
.max
, &on
->range
.addr
.ipv6
.min
,
5561 sizeof on
->range
.addr
.ipv6
.max
) < 0) {
5566 if (on
->range_af
!= AF_UNSPEC
&& s
[n
] == ':') {
5568 if (!ovs_scan_len(s
, &n
, "%"SCNu16
, &on
->range
.proto
.min
)) {
5573 if (!ovs_scan_len(s
, &n
, "%"SCNu16
, &on
->range
.proto
.max
)
5574 || on
->range
.proto
.max
< on
->range
.proto
.min
) {
5579 if (strlen(s
) != n
) {
5580 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
5585 return xasprintf("invalid nat range \"%s\"", s
);
5589 /* Parses 'arg' as the argument to a "nat" action, and appends such an
5590 * action to 'ofpacts'.
5592 * Returns NULL if successful, otherwise a malloc()'d string describing the
5593 * error. The caller is responsible for freeing the returned string. */
5594 static char * OVS_WARN_UNUSED_RESULT
5595 parse_NAT(char *arg
, struct ofpbuf
*ofpacts
,
5596 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
5598 struct ofpact_nat
*on
= ofpact_put_NAT(ofpacts
);
5602 on
->range_af
= AF_UNSPEC
;
5604 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
5607 if (!strcmp(key
, "src")) {
5608 on
->flags
|= NX_NAT_F_SRC
;
5609 error
= str_to_nat_range(value
, on
);
5610 } else if (!strcmp(key
, "dst")) {
5611 on
->flags
|= NX_NAT_F_DST
;
5612 error
= str_to_nat_range(value
, on
);
5613 } else if (!strcmp(key
, "persistent")) {
5614 on
->flags
|= NX_NAT_F_PERSISTENT
;
5615 } else if (!strcmp(key
, "hash")) {
5616 on
->flags
|= NX_NAT_F_PROTO_HASH
;
5617 } else if (!strcmp(key
, "random")) {
5618 on
->flags
|= NX_NAT_F_PROTO_RANDOM
;
5620 error
= xasprintf("invalid key \"%s\" in \"nat\" argument",
5627 if (on
->flags
& NX_NAT_F_SRC
&& on
->flags
& NX_NAT_F_DST
) {
5628 return xasprintf("May only specify one of \"src\" or \"dst\".");
5630 if (!(on
->flags
& NX_NAT_F_SRC
|| on
->flags
& NX_NAT_F_DST
)) {
5632 return xasprintf("Flags allowed only with \"src\" or \"dst\".");
5634 if (on
->range_af
!= AF_UNSPEC
) {
5635 return xasprintf("Range allowed only with \"src\" or \"dst\".");
5638 if (on
->flags
& NX_NAT_F_PROTO_HASH
&& on
->flags
& NX_NAT_F_PROTO_RANDOM
) {
5639 return xasprintf("Both \"hash\" and \"random\" are not allowed.");
5645 /* Truncate output action. */
5646 struct nx_action_output_trunc
{
5647 ovs_be16 type
; /* OFPAT_VENDOR. */
5648 ovs_be16 len
; /* At least 16. */
5649 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5650 ovs_be16 subtype
; /* NXAST_OUTPUT_TRUNC. */
5651 ovs_be16 port
; /* Output port */
5652 ovs_be32 max_len
; /* Truncate packet to size bytes */
5654 OFP_ASSERT(sizeof(struct nx_action_output_trunc
) == 16);
5657 decode_NXAST_RAW_OUTPUT_TRUNC(const struct nx_action_output_trunc
*natrc
,
5658 enum ofp_version ofp_version OVS_UNUSED
,
5661 struct ofpact_output_trunc
*output_trunc
;
5663 output_trunc
= ofpact_put_OUTPUT_TRUNC(out
);
5664 output_trunc
->max_len
= ntohl(natrc
->max_len
);
5665 output_trunc
->port
= u16_to_ofp(ntohs(natrc
->port
));
5667 if (output_trunc
->max_len
< ETH_HEADER_LEN
) {
5668 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5674 encode_OUTPUT_TRUNC(const struct ofpact_output_trunc
*output_trunc
,
5675 enum ofp_version ofp_version OVS_UNUSED
,
5678 struct nx_action_output_trunc
*natrc
= put_NXAST_OUTPUT_TRUNC(out
);
5680 natrc
->max_len
= htonl(output_trunc
->max_len
);
5681 natrc
->port
= htons(ofp_to_u16(output_trunc
->port
));
5684 static char * OVS_WARN_UNUSED_RESULT
5685 parse_OUTPUT_TRUNC(const char *arg
, struct ofpbuf
*ofpacts OVS_UNUSED
,
5686 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
5688 /* Disable output_trunc parsing. Expose as output(port=N,max_len=M) and
5689 * reuse parse_OUTPUT to parse output_trunc action. */
5690 return xasprintf("unknown action %s", arg
);
5694 format_OUTPUT_TRUNC(const struct ofpact_output_trunc
*a
, struct ds
*s
)
5696 ds_put_format(s
, "%soutput%s(port=%"PRIu16
",max_len=%"PRIu32
")",
5697 colors
.special
, colors
.end
, a
->port
, a
->max_len
);
5701 /* Meter instruction. */
5704 encode_METER(const struct ofpact_meter
*meter
,
5705 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5707 if (ofp_version
>= OFP13_VERSION
) {
5708 instruction_put_OFPIT13_METER(out
)->meter_id
= htonl(meter
->meter_id
);
5712 static char * OVS_WARN_UNUSED_RESULT
5713 parse_METER(char *arg
, struct ofpbuf
*ofpacts
,
5714 enum ofputil_protocol
*usable_protocols
)
5716 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
5717 return str_to_u32(arg
, &ofpact_put_METER(ofpacts
)->meter_id
);
5721 format_METER(const struct ofpact_meter
*a
, struct ds
*s
)
5723 ds_put_format(s
, "%smeter:%s%"PRIu32
,
5724 colors
.param
, colors
.end
, a
->meter_id
);
5727 /* Clear-Actions instruction. */
5730 encode_CLEAR_ACTIONS(const struct ofpact_null
*null OVS_UNUSED
,
5731 enum ofp_version ofp_version OVS_UNUSED
,
5732 struct ofpbuf
*out OVS_UNUSED
)
5734 if (ofp_version
> OFP10_VERSION
) {
5735 instruction_put_OFPIT11_CLEAR_ACTIONS(out
);
5739 static char * OVS_WARN_UNUSED_RESULT
5740 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
5741 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
5743 ofpact_put_CLEAR_ACTIONS(ofpacts
);
5748 format_CLEAR_ACTIONS(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
5750 ds_put_format(s
, "%sclear_actions%s", colors
.value
, colors
.end
);
5753 /* Write-Actions instruction. */
5756 encode_WRITE_ACTIONS(const struct ofpact_nest
*actions
,
5757 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5759 if (ofp_version
> OFP10_VERSION
) {
5760 const size_t ofs
= out
->size
;
5762 instruction_put_OFPIT11_WRITE_ACTIONS(out
);
5763 ofpacts_put_openflow_actions(actions
->actions
,
5764 ofpact_nest_get_action_len(actions
),
5766 ofpacts_update_instruction_actions(out
, ofs
);
5770 static char * OVS_WARN_UNUSED_RESULT
5771 parse_WRITE_ACTIONS(char *arg
, struct ofpbuf
*ofpacts
,
5772 enum ofputil_protocol
*usable_protocols
)
5774 size_t ofs
= ofpacts_pull(ofpacts
);
5775 struct ofpact_nest
*on
;
5778 /* Add a Write-Actions instruction and then pull it off. */
5779 ofpact_put(ofpacts
, OFPACT_WRITE_ACTIONS
, sizeof *on
);
5780 ofpbuf_pull(ofpacts
, sizeof *on
);
5782 /* Parse nested actions.
5784 * We pulled off "write-actions" and the previous actions because the
5785 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
5786 * that it doesn't actually include the nested actions. That means that
5787 * ofpacts_parse() would reject them as being part of an Apply-Actions that
5788 * follows a Write-Actions, which is an invalid order. */
5789 error
= ofpacts_parse(arg
, ofpacts
, usable_protocols
, false,
5790 OFPACT_WRITE_ACTIONS
);
5792 /* Put the Write-Actions back on and update its length. */
5793 on
= ofpbuf_push_uninit(ofpacts
, sizeof *on
);
5794 on
->ofpact
.len
= ofpacts
->size
;
5796 /* Put any previous actions or instructions back on. */
5797 ofpbuf_push_uninit(ofpacts
, ofs
);
5803 format_WRITE_ACTIONS(const struct ofpact_nest
*a
, struct ds
*s
)
5805 ds_put_format(s
, "%swrite_actions(%s", colors
.paren
, colors
.end
);
5806 ofpacts_format(a
->actions
, ofpact_nest_get_action_len(a
), s
);
5807 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
5810 /* Action structure for NXAST_WRITE_METADATA.
5812 * Modifies the 'mask' bits of the metadata value. */
5813 struct nx_action_write_metadata
{
5814 ovs_be16 type
; /* OFPAT_VENDOR. */
5815 ovs_be16 len
; /* Length is 32. */
5816 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5817 ovs_be16 subtype
; /* NXAST_WRITE_METADATA. */
5818 uint8_t zeros
[6]; /* Must be zero. */
5819 ovs_be64 metadata
; /* Metadata register. */
5820 ovs_be64 mask
; /* Metadata mask. */
5822 OFP_ASSERT(sizeof(struct nx_action_write_metadata
) == 32);
5825 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata
*nawm
,
5826 enum ofp_version ofp_version OVS_UNUSED
,
5829 struct ofpact_metadata
*om
;
5831 if (!is_all_zeros(nawm
->zeros
, sizeof nawm
->zeros
)) {
5832 return OFPERR_NXBRC_MUST_BE_ZERO
;
5835 om
= ofpact_put_WRITE_METADATA(out
);
5836 om
->metadata
= nawm
->metadata
;
5837 om
->mask
= nawm
->mask
;
5843 encode_WRITE_METADATA(const struct ofpact_metadata
*metadata
,
5844 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5846 if (ofp_version
== OFP10_VERSION
) {
5847 struct nx_action_write_metadata
*nawm
;
5849 nawm
= put_NXAST_WRITE_METADATA(out
);
5850 nawm
->metadata
= metadata
->metadata
;
5851 nawm
->mask
= metadata
->mask
;
5853 struct ofp11_instruction_write_metadata
*oiwm
;
5855 oiwm
= instruction_put_OFPIT11_WRITE_METADATA(out
);
5856 oiwm
->metadata
= metadata
->metadata
;
5857 oiwm
->metadata_mask
= metadata
->mask
;
5861 static char * OVS_WARN_UNUSED_RESULT
5862 parse_WRITE_METADATA(char *arg
, struct ofpbuf
*ofpacts
,
5863 enum ofputil_protocol
*usable_protocols
)
5865 struct ofpact_metadata
*om
;
5866 char *mask
= strchr(arg
, '/');
5868 *usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
5870 om
= ofpact_put_WRITE_METADATA(ofpacts
);
5875 error
= str_to_be64(mask
+ 1, &om
->mask
);
5880 om
->mask
= OVS_BE64_MAX
;
5883 return str_to_be64(arg
, &om
->metadata
);
5887 format_WRITE_METADATA(const struct ofpact_metadata
*a
, struct ds
*s
)
5889 ds_put_format(s
, "%swrite_metadata:%s%#"PRIx64
,
5890 colors
.param
, colors
.end
, ntohll(a
->metadata
));
5891 if (a
->mask
!= OVS_BE64_MAX
) {
5892 ds_put_format(s
, "/%#"PRIx64
, ntohll(a
->mask
));
5896 /* Goto-Table instruction. */
5899 encode_GOTO_TABLE(const struct ofpact_goto_table
*goto_table
,
5900 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5902 if (ofp_version
== OFP10_VERSION
) {
5903 struct nx_action_resubmit
*nar
;
5905 nar
= put_NXAST_RESUBMIT_TABLE(out
);
5906 nar
->table
= goto_table
->table_id
;
5907 nar
->in_port
= htons(ofp_to_u16(OFPP_IN_PORT
));
5909 struct ofp11_instruction_goto_table
*oigt
;
5911 oigt
= instruction_put_OFPIT11_GOTO_TABLE(out
);
5912 oigt
->table_id
= goto_table
->table_id
;
5913 memset(oigt
->pad
, 0, sizeof oigt
->pad
);
5917 static char * OVS_WARN_UNUSED_RESULT
5918 parse_GOTO_TABLE(char *arg
, struct ofpbuf
*ofpacts
,
5919 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
5921 struct ofpact_goto_table
*ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
5922 char *table_s
= strsep(&arg
, ",");
5923 if (!table_s
|| !table_s
[0]) {
5924 return xstrdup("instruction goto-table needs table id");
5926 return str_to_u8(table_s
, "table", &ogt
->table_id
);
5930 format_GOTO_TABLE(const struct ofpact_goto_table
*a
, struct ds
*s
)
5932 ds_put_format(s
, "%sgoto_table:%s%"PRIu8
,
5933 colors
.param
, colors
.end
, a
->table_id
);
5937 log_bad_action(const struct ofp_action_header
*actions
, size_t actions_len
,
5938 const struct ofp_action_header
*bad_action
, enum ofperr error
)
5940 if (!VLOG_DROP_WARN(&rl
)) {
5944 ds_put_hex_dump(&s
, actions
, actions_len
, 0, false);
5945 VLOG_WARN("bad action at offset %#"PRIxPTR
" (%s):\n%s",
5946 (char *)bad_action
- (char *)actions
,
5947 ofperr_get_name(error
), ds_cstr(&s
));
5953 ofpacts_decode(const void *actions
, size_t actions_len
,
5954 enum ofp_version ofp_version
, struct ofpbuf
*ofpacts
)
5956 struct ofpbuf openflow
= ofpbuf_const_initializer(actions
, actions_len
);
5957 while (openflow
.size
) {
5958 const struct ofp_action_header
*action
= openflow
.data
;
5959 enum ofp_raw_action_type raw
;
5963 error
= ofpact_pull_raw(&openflow
, ofp_version
, &raw
, &arg
);
5965 error
= ofpact_decode(action
, raw
, ofp_version
, arg
, ofpacts
);
5969 log_bad_action(actions
, actions_len
, action
, error
);
5977 ofpacts_pull_openflow_actions__(struct ofpbuf
*openflow
,
5978 unsigned int actions_len
,
5979 enum ofp_version version
,
5980 uint32_t allowed_ovsinsts
,
5981 struct ofpbuf
*ofpacts
,
5982 enum ofpact_type outer_action
)
5984 const struct ofp_action_header
*actions
;
5985 size_t orig_size
= ofpacts
->size
;
5988 if (actions_len
% OFP_ACTION_ALIGN
!= 0) {
5989 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u is not a "
5990 "multiple of %d", actions_len
, OFP_ACTION_ALIGN
);
5991 return OFPERR_OFPBRC_BAD_LEN
;
5994 actions
= ofpbuf_try_pull(openflow
, actions_len
);
5995 if (actions
== NULL
) {
5996 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u exceeds "
5997 "remaining message length (%"PRIu32
")",
5998 actions_len
, openflow
->size
);
5999 return OFPERR_OFPBRC_BAD_LEN
;
6002 error
= ofpacts_decode(actions
, actions_len
, version
, ofpacts
);
6004 ofpacts
->size
= orig_size
;
6008 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
, allowed_ovsinsts
,
6011 ofpacts
->size
= orig_size
;
6016 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
6017 * of 'openflow' into ofpacts. On success, appends the converted actions to
6018 * 'ofpacts'; on failure, 'ofpacts' is unchanged (but might be reallocated) .
6019 * Returns 0 if successful, otherwise an OpenFlow error.
6021 * Actions are processed according to their OpenFlow version which
6022 * is provided in the 'version' parameter.
6024 * In most places in OpenFlow, actions appear encapsulated in instructions, so
6025 * you should call ofpacts_pull_openflow_instructions() instead of this
6028 * The parsed actions are valid generically, but they may not be valid in a
6029 * specific context. For example, port numbers up to OFPP_MAX are valid
6030 * generically, but specific datapaths may only support port numbers in a
6031 * smaller range. Use ofpacts_check() to additional check whether actions are
6032 * valid in a specific context. */
6034 ofpacts_pull_openflow_actions(struct ofpbuf
*openflow
,
6035 unsigned int actions_len
,
6036 enum ofp_version version
,
6037 struct ofpbuf
*ofpacts
)
6039 return ofpacts_pull_openflow_actions__(openflow
, actions_len
, version
,
6040 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
6044 /* OpenFlow 1.1 actions. */
6047 /* True if an action sets the value of a field
6048 * in a way that is compatibile with the action set.
6049 * The field can be set via either a set or a move action.
6050 * False otherwise. */
6052 ofpact_is_set_or_move_action(const struct ofpact
*a
)
6055 case OFPACT_SET_FIELD
:
6056 case OFPACT_REG_MOVE
:
6057 case OFPACT_SET_ETH_DST
:
6058 case OFPACT_SET_ETH_SRC
:
6059 case OFPACT_SET_IP_DSCP
:
6060 case OFPACT_SET_IP_ECN
:
6061 case OFPACT_SET_IP_TTL
:
6062 case OFPACT_SET_IPV4_DST
:
6063 case OFPACT_SET_IPV4_SRC
:
6064 case OFPACT_SET_L4_DST_PORT
:
6065 case OFPACT_SET_L4_SRC_PORT
:
6066 case OFPACT_SET_MPLS_LABEL
:
6067 case OFPACT_SET_MPLS_TC
:
6068 case OFPACT_SET_MPLS_TTL
:
6069 case OFPACT_SET_QUEUE
:
6070 case OFPACT_SET_TUNNEL
:
6071 case OFPACT_SET_VLAN_PCP
:
6072 case OFPACT_SET_VLAN_VID
:
6075 case OFPACT_CLEAR_ACTIONS
:
6078 case OFPACT_CONTROLLER
:
6079 case OFPACT_DEC_MPLS_TTL
:
6080 case OFPACT_DEC_TTL
:
6081 case OFPACT_ENQUEUE
:
6083 case OFPACT_UNROLL_XLATE
:
6084 case OFPACT_FIN_TIMEOUT
:
6085 case OFPACT_GOTO_TABLE
:
6088 case OFPACT_CONJUNCTION
:
6090 case OFPACT_MULTIPATH
:
6093 case OFPACT_OUTPUT_REG
:
6094 case OFPACT_OUTPUT_TRUNC
:
6095 case OFPACT_POP_MPLS
:
6096 case OFPACT_POP_QUEUE
:
6097 case OFPACT_PUSH_MPLS
:
6098 case OFPACT_PUSH_VLAN
:
6099 case OFPACT_RESUBMIT
:
6101 case OFPACT_STACK_POP
:
6102 case OFPACT_STACK_PUSH
:
6103 case OFPACT_STRIP_VLAN
:
6104 case OFPACT_WRITE_ACTIONS
:
6105 case OFPACT_WRITE_METADATA
:
6106 case OFPACT_DEBUG_RECIRC
:
6113 /* True if an action is allowed in the action set.
6114 * False otherwise. */
6116 ofpact_is_allowed_in_actions_set(const struct ofpact
*a
)
6119 case OFPACT_DEC_MPLS_TTL
:
6120 case OFPACT_DEC_TTL
:
6123 case OFPACT_OUTPUT_TRUNC
:
6124 case OFPACT_POP_MPLS
:
6125 case OFPACT_PUSH_MPLS
:
6126 case OFPACT_PUSH_VLAN
:
6127 case OFPACT_REG_MOVE
:
6128 case OFPACT_SET_FIELD
:
6129 case OFPACT_SET_ETH_DST
:
6130 case OFPACT_SET_ETH_SRC
:
6131 case OFPACT_SET_IP_DSCP
:
6132 case OFPACT_SET_IP_ECN
:
6133 case OFPACT_SET_IP_TTL
:
6134 case OFPACT_SET_IPV4_DST
:
6135 case OFPACT_SET_IPV4_SRC
:
6136 case OFPACT_SET_L4_DST_PORT
:
6137 case OFPACT_SET_L4_SRC_PORT
:
6138 case OFPACT_SET_MPLS_LABEL
:
6139 case OFPACT_SET_MPLS_TC
:
6140 case OFPACT_SET_MPLS_TTL
:
6141 case OFPACT_SET_QUEUE
:
6142 case OFPACT_SET_TUNNEL
:
6143 case OFPACT_SET_VLAN_PCP
:
6144 case OFPACT_SET_VLAN_VID
:
6145 case OFPACT_STRIP_VLAN
:
6148 /* In general these actions are excluded because they are not part of
6149 * the OpenFlow specification nor map to actions that are defined in
6150 * the specification. Thus the order in which they should be applied
6151 * in the action set is undefined. */
6153 case OFPACT_CONTROLLER
:
6156 case OFPACT_ENQUEUE
:
6158 case OFPACT_UNROLL_XLATE
:
6159 case OFPACT_FIN_TIMEOUT
:
6161 case OFPACT_CONJUNCTION
:
6162 case OFPACT_MULTIPATH
:
6164 case OFPACT_OUTPUT_REG
:
6165 case OFPACT_POP_QUEUE
:
6166 case OFPACT_RESUBMIT
:
6168 case OFPACT_STACK_POP
:
6169 case OFPACT_STACK_PUSH
:
6170 case OFPACT_DEBUG_RECIRC
:
6172 /* The action set may only include actions and thus
6173 * may not include any instructions */
6174 case OFPACT_CLEAR_ACTIONS
:
6175 case OFPACT_GOTO_TABLE
:
6177 case OFPACT_WRITE_ACTIONS
:
6178 case OFPACT_WRITE_METADATA
:
6185 /* Append ofpact 'a' onto the tail of 'out' */
6187 ofpact_copy(struct ofpbuf
*out
, const struct ofpact
*a
)
6189 ofpbuf_put(out
, a
, OFPACT_ALIGN(a
->len
));
6192 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
6194 ofpacts_copy_last(struct ofpbuf
*out
, const struct ofpbuf
*in
,
6195 enum ofpact_type filter
)
6197 const struct ofpact
*target
;
6198 const struct ofpact
*a
;
6201 OFPACT_FOR_EACH (a
, in
->data
, in
->size
) {
6202 if (a
->type
== filter
) {
6207 ofpact_copy(out
, target
);
6209 return target
!= NULL
;
6212 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
6213 * The order of appended ofpacts is preserved between 'in' and 'out' */
6215 ofpacts_copy_all(struct ofpbuf
*out
, const struct ofpbuf
*in
,
6216 bool (*filter
)(const struct ofpact
*))
6218 const struct ofpact
*a
;
6220 OFPACT_FOR_EACH (a
, in
->data
, in
->size
) {
6222 ofpact_copy(out
, a
);
6227 /* Reads 'action_set', which contains ofpacts accumulated by
6228 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
6229 * executed directly into 'action_list'. (These names correspond to the
6230 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
6232 * In general this involves appending the last instance of each action that is
6233 * admissible in the action set in the order described in the OpenFlow
6237 * + output action is only appended if no group action was present in 'in'.
6238 * + As a simplification all set actions are copied in the order the are
6239 * provided in 'in' as many set actions applied to a field has the same
6240 * affect as only applying the last action that sets a field and
6241 * duplicates are removed by do_xlate_actions().
6242 * This has an unwanted side-effect of compsoting multiple
6243 * LOAD_REG actions that touch different regions of the same field. */
6245 ofpacts_execute_action_set(struct ofpbuf
*action_list
,
6246 const struct ofpbuf
*action_set
)
6248 /* The OpenFlow spec "Action Set" section specifies this order. */
6249 ofpacts_copy_last(action_list
, action_set
, OFPACT_STRIP_VLAN
);
6250 ofpacts_copy_last(action_list
, action_set
, OFPACT_POP_MPLS
);
6251 ofpacts_copy_last(action_list
, action_set
, OFPACT_PUSH_MPLS
);
6252 ofpacts_copy_last(action_list
, action_set
, OFPACT_PUSH_VLAN
);
6253 ofpacts_copy_last(action_list
, action_set
, OFPACT_DEC_TTL
);
6254 ofpacts_copy_last(action_list
, action_set
, OFPACT_DEC_MPLS_TTL
);
6255 ofpacts_copy_all(action_list
, action_set
, ofpact_is_set_or_move_action
);
6256 ofpacts_copy_last(action_list
, action_set
, OFPACT_SET_QUEUE
);
6258 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
6259 * we should execute only OFPACT_GROUP.
6261 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
6262 * all the actions because there's no point in modifying a packet that will
6263 * not be sent anywhere. */
6264 if (!ofpacts_copy_last(action_list
, action_set
, OFPACT_GROUP
) &&
6265 !ofpacts_copy_last(action_list
, action_set
, OFPACT_OUTPUT
) &&
6266 !ofpacts_copy_last(action_list
, action_set
, OFPACT_RESUBMIT
) &&
6267 !ofpacts_copy_last(action_list
, action_set
, OFPACT_CT
)) {
6268 ofpbuf_clear(action_list
);
6274 ofpacts_decode_for_action_set(const struct ofp_action_header
*in
,
6275 size_t n_in
, enum ofp_version version
,
6280 size_t start
= out
->size
;
6282 error
= ofpacts_decode(in
, n_in
, version
, out
);
6288 OFPACT_FOR_EACH (a
, ofpact_end(out
->data
, start
), out
->size
- start
) {
6289 if (!ofpact_is_allowed_in_actions_set(a
)) {
6290 VLOG_WARN_RL(&rl
, "disallowed action in action set");
6291 return OFPERR_OFPBAC_BAD_TYPE
;
6298 /* OpenFlow 1.1 instructions. */
6300 struct instruction_type_info
{
6301 enum ovs_instruction_type type
;
6305 static const struct instruction_type_info inst_info
[] = {
6306 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
6312 ovs_instruction_name_from_type(enum ovs_instruction_type type
)
6314 return inst_info
[type
].name
;
6318 ovs_instruction_type_from_name(const char *name
)
6320 const struct instruction_type_info
*p
;
6321 for (p
= inst_info
; p
< &inst_info
[ARRAY_SIZE(inst_info
)]; p
++) {
6322 if (!strcasecmp(name
, p
->name
)) {
6329 enum ovs_instruction_type
6330 ovs_instruction_type_from_ofpact_type(enum ofpact_type type
)
6334 return OVSINST_OFPIT13_METER
;
6335 case OFPACT_CLEAR_ACTIONS
:
6336 return OVSINST_OFPIT11_CLEAR_ACTIONS
;
6337 case OFPACT_WRITE_ACTIONS
:
6338 return OVSINST_OFPIT11_WRITE_ACTIONS
;
6339 case OFPACT_WRITE_METADATA
:
6340 return OVSINST_OFPIT11_WRITE_METADATA
;
6341 case OFPACT_GOTO_TABLE
:
6342 return OVSINST_OFPIT11_GOTO_TABLE
;
6345 case OFPACT_CONTROLLER
:
6346 case OFPACT_ENQUEUE
:
6347 case OFPACT_OUTPUT_REG
:
6348 case OFPACT_OUTPUT_TRUNC
:
6350 case OFPACT_SET_VLAN_VID
:
6351 case OFPACT_SET_VLAN_PCP
:
6352 case OFPACT_STRIP_VLAN
:
6353 case OFPACT_PUSH_VLAN
:
6354 case OFPACT_SET_ETH_SRC
:
6355 case OFPACT_SET_ETH_DST
:
6356 case OFPACT_SET_IPV4_SRC
:
6357 case OFPACT_SET_IPV4_DST
:
6358 case OFPACT_SET_IP_DSCP
:
6359 case OFPACT_SET_IP_ECN
:
6360 case OFPACT_SET_IP_TTL
:
6361 case OFPACT_SET_L4_SRC_PORT
:
6362 case OFPACT_SET_L4_DST_PORT
:
6363 case OFPACT_REG_MOVE
:
6364 case OFPACT_SET_FIELD
:
6365 case OFPACT_STACK_PUSH
:
6366 case OFPACT_STACK_POP
:
6367 case OFPACT_DEC_TTL
:
6368 case OFPACT_SET_MPLS_LABEL
:
6369 case OFPACT_SET_MPLS_TC
:
6370 case OFPACT_SET_MPLS_TTL
:
6371 case OFPACT_DEC_MPLS_TTL
:
6372 case OFPACT_PUSH_MPLS
:
6373 case OFPACT_POP_MPLS
:
6374 case OFPACT_SET_TUNNEL
:
6375 case OFPACT_SET_QUEUE
:
6376 case OFPACT_POP_QUEUE
:
6377 case OFPACT_FIN_TIMEOUT
:
6378 case OFPACT_RESUBMIT
:
6380 case OFPACT_CONJUNCTION
:
6381 case OFPACT_MULTIPATH
:
6384 case OFPACT_UNROLL_XLATE
:
6386 case OFPACT_DEBUG_RECIRC
:
6390 return OVSINST_OFPIT11_APPLY_ACTIONS
;
6395 ovs_instruction_type_from_inst_type(enum ovs_instruction_type
*instruction_type
,
6396 const uint16_t inst_type
)
6398 switch (inst_type
) {
6400 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6402 *instruction_type = OVSINST_##ENUM; \
6408 return OFPERR_OFPBIC_UNKNOWN_INST
;
6412 /* Two-way translation between OVS's internal "OVSINST_*" representation of
6413 * instructions and the "OFPIT_*" representation used in OpenFlow. */
6414 struct ovsinst_map
{
6415 enum ovs_instruction_type ovsinst
; /* Internal name for instruction. */
6416 int ofpit
; /* OFPIT_* number from OpenFlow spec. */
6419 static const struct ovsinst_map
*
6420 get_ovsinst_map(enum ofp_version version
)
6422 /* OpenFlow 1.1 and 1.2 instructions. */
6423 static const struct ovsinst_map of11
[] = {
6424 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
6425 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
6426 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
6427 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
6428 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
6432 /* OpenFlow 1.3+ instructions. */
6433 static const struct ovsinst_map of13
[] = {
6434 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
6435 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
6436 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
6437 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
6438 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
6439 { OVSINST_OFPIT13_METER
, 6 },
6443 return version
< OFP13_VERSION
? of11
: of13
;
6446 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
6447 * values, into a bitmap of instructions suitable for OpenFlow 'version'
6448 * (OFP11_VERSION or later), and returns the result. */
6450 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap
, enum ofp_version version
)
6452 uint32_t ofpit_bitmap
= 0;
6453 const struct ovsinst_map
*x
;
6455 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
6456 if (ovsinst_bitmap
& (1u << x
->ovsinst
)) {
6457 ofpit_bitmap
|= 1u << x
->ofpit
;
6460 return htonl(ofpit_bitmap
);
6463 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
6464 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
6465 * correspond to OVSINST_* values, and returns the result. */
6467 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap
, enum ofp_version version
)
6469 uint32_t ovsinst_bitmap
= 0;
6470 const struct ovsinst_map
*x
;
6472 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
6473 if (ofpit_bitmap
& htonl(1u << x
->ofpit
)) {
6474 ovsinst_bitmap
|= 1u << x
->ovsinst
;
6477 return ovsinst_bitmap
;
6480 static inline struct ofp11_instruction
*
6481 instruction_next(const struct ofp11_instruction
*inst
)
6483 return ((struct ofp11_instruction
*) (void *)
6484 ((uint8_t *) inst
+ ntohs(inst
->len
)));
6488 instruction_is_valid(const struct ofp11_instruction
*inst
,
6489 size_t n_instructions
)
6491 uint16_t len
= ntohs(inst
->len
);
6492 return (!(len
% OFP11_INSTRUCTION_ALIGN
)
6493 && len
>= sizeof *inst
6494 && len
/ sizeof *inst
<= n_instructions
);
6497 /* This macro is careful to check for instructions with bad lengths. */
6498 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
6499 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
6500 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
6501 ((LEFT) -= (ntohs((ITER)->len) \
6502 / sizeof(struct ofp11_instruction)), \
6503 (ITER) = instruction_next(ITER)))
6506 decode_openflow11_instruction(const struct ofp11_instruction
*inst
,
6507 enum ovs_instruction_type
*type
)
6509 uint16_t len
= ntohs(inst
->len
);
6511 switch (inst
->type
) {
6512 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER
):
6513 return OFPERR_OFPBIC_BAD_EXPERIMENTER
;
6515 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6516 case CONSTANT_HTONS(ENUM): \
6518 ? len >= sizeof(struct STRUCT) \
6519 : len == sizeof(struct STRUCT)) { \
6520 *type = OVSINST_##ENUM; \
6523 return OFPERR_OFPBIC_BAD_LEN; \
6529 return OFPERR_OFPBIC_UNKNOWN_INST
;
6534 decode_openflow11_instructions(const struct ofp11_instruction insts
[],
6536 const struct ofp11_instruction
*out
[])
6538 const struct ofp11_instruction
*inst
;
6541 memset(out
, 0, N_OVS_INSTRUCTIONS
* sizeof *out
);
6542 INSTRUCTION_FOR_EACH (inst
, left
, insts
, n_insts
) {
6543 enum ovs_instruction_type type
;
6546 error
= decode_openflow11_instruction(inst
, &type
);
6552 return OFPERR_OFPBIC_DUP_INST
;
6558 VLOG_WARN_RL(&rl
, "bad instruction format at offset %"PRIuSIZE
,
6559 (n_insts
- left
) * sizeof *inst
);
6560 return OFPERR_OFPBIC_BAD_LEN
;
6566 get_actions_from_instruction(const struct ofp11_instruction
*inst
,
6567 const struct ofp_action_header
**actions
,
6568 size_t *actions_len
)
6570 *actions
= ALIGNED_CAST(const struct ofp_action_header
*, inst
+ 1);
6571 *actions_len
= ntohs(inst
->len
) - sizeof *inst
;
6575 ofpacts_pull_openflow_instructions(struct ofpbuf
*openflow
,
6576 unsigned int instructions_len
,
6577 enum ofp_version version
,
6578 struct ofpbuf
*ofpacts
)
6580 const struct ofp11_instruction
*instructions
;
6581 const struct ofp11_instruction
*insts
[N_OVS_INSTRUCTIONS
];
6584 ofpbuf_clear(ofpacts
);
6585 if (version
== OFP10_VERSION
) {
6586 return ofpacts_pull_openflow_actions__(openflow
, instructions_len
,
6588 (1u << N_OVS_INSTRUCTIONS
) - 1,
6592 if (instructions_len
% OFP11_INSTRUCTION_ALIGN
!= 0) {
6593 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u is not a "
6595 instructions_len
, OFP11_INSTRUCTION_ALIGN
);
6596 error
= OFPERR_OFPBIC_BAD_LEN
;
6600 instructions
= ofpbuf_try_pull(openflow
, instructions_len
);
6601 if (instructions
== NULL
) {
6602 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u exceeds "
6603 "remaining message length (%"PRIu32
")",
6604 instructions_len
, openflow
->size
);
6605 error
= OFPERR_OFPBIC_BAD_LEN
;
6609 error
= decode_openflow11_instructions(
6610 instructions
, instructions_len
/ OFP11_INSTRUCTION_ALIGN
,
6616 if (insts
[OVSINST_OFPIT13_METER
]) {
6617 const struct ofp13_instruction_meter
*oim
;
6618 struct ofpact_meter
*om
;
6620 oim
= ALIGNED_CAST(const struct ofp13_instruction_meter
*,
6621 insts
[OVSINST_OFPIT13_METER
]);
6623 om
= ofpact_put_METER(ofpacts
);
6624 om
->meter_id
= ntohl(oim
->meter_id
);
6626 if (insts
[OVSINST_OFPIT11_APPLY_ACTIONS
]) {
6627 const struct ofp_action_header
*actions
;
6630 get_actions_from_instruction(insts
[OVSINST_OFPIT11_APPLY_ACTIONS
],
6631 &actions
, &actions_len
);
6632 error
= ofpacts_decode(actions
, actions_len
, version
, ofpacts
);
6637 if (insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]) {
6638 instruction_get_OFPIT11_CLEAR_ACTIONS(
6639 insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]);
6640 ofpact_put_CLEAR_ACTIONS(ofpacts
);
6642 if (insts
[OVSINST_OFPIT11_WRITE_ACTIONS
]) {
6643 struct ofpact_nest
*on
;
6644 const struct ofp_action_header
*actions
;
6646 size_t start
= ofpacts
->size
;
6647 ofpact_put(ofpacts
, OFPACT_WRITE_ACTIONS
,
6648 offsetof(struct ofpact_nest
, actions
));
6649 get_actions_from_instruction(insts
[OVSINST_OFPIT11_WRITE_ACTIONS
],
6650 &actions
, &actions_len
);
6651 error
= ofpacts_decode_for_action_set(actions
, actions_len
,
6656 on
= ofpbuf_at_assert(ofpacts
, start
, sizeof *on
);
6657 on
->ofpact
.len
= ofpacts
->size
- start
;
6659 if (insts
[OVSINST_OFPIT11_WRITE_METADATA
]) {
6660 const struct ofp11_instruction_write_metadata
*oiwm
;
6661 struct ofpact_metadata
*om
;
6663 oiwm
= ALIGNED_CAST(const struct ofp11_instruction_write_metadata
*,
6664 insts
[OVSINST_OFPIT11_WRITE_METADATA
]);
6666 om
= ofpact_put_WRITE_METADATA(ofpacts
);
6667 om
->metadata
= oiwm
->metadata
;
6668 om
->mask
= oiwm
->metadata_mask
;
6670 if (insts
[OVSINST_OFPIT11_GOTO_TABLE
]) {
6671 const struct ofp11_instruction_goto_table
*oigt
;
6672 struct ofpact_goto_table
*ogt
;
6674 oigt
= instruction_get_OFPIT11_GOTO_TABLE(
6675 insts
[OVSINST_OFPIT11_GOTO_TABLE
]);
6676 ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
6677 ogt
->table_id
= oigt
->table_id
;
6680 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
,
6681 (1u << N_OVS_INSTRUCTIONS
) - 1, 0);
6684 ofpbuf_clear(ofpacts
);
6689 /* Update the length of the instruction that begins at offset 'ofs' within
6690 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
6691 * If the instruction contains no nested actions, deletes it entirely. */
6693 ofpacts_update_instruction_actions(struct ofpbuf
*openflow
, size_t ofs
)
6695 struct ofp11_instruction_actions
*oia
;
6697 oia
= ofpbuf_at_assert(openflow
, ofs
, sizeof *oia
);
6698 if (openflow
->size
> ofs
+ sizeof *oia
) {
6699 oia
->len
= htons(openflow
->size
- ofs
);
6701 openflow
->size
= ofs
;
6705 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
6706 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
6707 * valid, otherwise an OpenFlow error code. */
6709 ofpact_check_output_port(ofp_port_t port
, ofp_port_t max_ports
)
6717 case OFPP_CONTROLLER
:
6722 return OFPERR_OFPBAC_BAD_OUT_PORT
;
6725 if (ofp_to_u16(port
) < ofp_to_u16(max_ports
)) {
6728 return OFPERR_OFPBAC_BAD_OUT_PORT
;
6732 /* Removes the protocols that require consistency between match and actions
6733 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
6735 * (An example of an inconsistency between match and actions is a flow that
6736 * does not match on an MPLS Ethertype but has an action that pops an MPLS
6739 inconsistent_match(enum ofputil_protocol
*usable_protocols
)
6741 *usable_protocols
&= OFPUTIL_P_OF10_ANY
;
6744 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
6745 * caller must restore them.
6747 * Modifies some actions, filling in fields that could not be properly set
6748 * without context. */
6750 ofpact_check__(enum ofputil_protocol
*usable_protocols
, struct ofpact
*a
,
6751 struct flow
*flow
, ofp_port_t max_ports
,
6752 uint8_t table_id
, uint8_t n_tables
)
6754 const struct ofpact_enqueue
*enqueue
;
6755 const struct mf_field
*mf
;
6759 return ofpact_check_output_port(ofpact_get_OUTPUT(a
)->port
,
6762 case OFPACT_CONTROLLER
:
6765 case OFPACT_ENQUEUE
:
6766 enqueue
= ofpact_get_ENQUEUE(a
);
6767 if (ofp_to_u16(enqueue
->port
) >= ofp_to_u16(max_ports
)
6768 && enqueue
->port
!= OFPP_IN_PORT
6769 && enqueue
->port
!= OFPP_LOCAL
) {
6770 return OFPERR_OFPBAC_BAD_OUT_PORT
;
6774 case OFPACT_OUTPUT_REG
:
6775 return mf_check_src(&ofpact_get_OUTPUT_REG(a
)->src
, flow
);
6777 case OFPACT_OUTPUT_TRUNC
:
6778 return ofpact_check_output_port(ofpact_get_OUTPUT_TRUNC(a
)->port
,
6782 return bundle_check(ofpact_get_BUNDLE(a
), max_ports
, flow
);
6784 case OFPACT_SET_VLAN_VID
:
6785 /* Remember if we saw a vlan tag in the flow to aid translating to
6786 * OpenFlow 1.1+ if need be. */
6787 ofpact_get_SET_VLAN_VID(a
)->flow_has_vlan
=
6788 (flow
->vlan_tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
6789 if (!(flow
->vlan_tci
& htons(VLAN_CFI
)) &&
6790 !ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
6791 inconsistent_match(usable_protocols
);
6793 /* Temporary mark that we have a vlan tag. */
6794 flow
->vlan_tci
|= htons(VLAN_CFI
);
6797 case OFPACT_SET_VLAN_PCP
:
6798 /* Remember if we saw a vlan tag in the flow to aid translating to
6799 * OpenFlow 1.1+ if need be. */
6800 ofpact_get_SET_VLAN_PCP(a
)->flow_has_vlan
=
6801 (flow
->vlan_tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
6802 if (!(flow
->vlan_tci
& htons(VLAN_CFI
)) &&
6803 !ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
6804 inconsistent_match(usable_protocols
);
6806 /* Temporary mark that we have a vlan tag. */
6807 flow
->vlan_tci
|= htons(VLAN_CFI
);
6810 case OFPACT_STRIP_VLAN
:
6811 if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
6812 inconsistent_match(usable_protocols
);
6814 /* Temporary mark that we have no vlan tag. */
6815 flow
->vlan_tci
= htons(0);
6818 case OFPACT_PUSH_VLAN
:
6819 if (flow
->vlan_tci
& htons(VLAN_CFI
)) {
6820 /* Multiple VLAN headers not supported. */
6821 return OFPERR_OFPBAC_BAD_TAG
;
6823 /* Temporary mark that we have a vlan tag. */
6824 flow
->vlan_tci
|= htons(VLAN_CFI
);
6827 case OFPACT_SET_ETH_SRC
:
6828 case OFPACT_SET_ETH_DST
:
6831 case OFPACT_SET_IPV4_SRC
:
6832 case OFPACT_SET_IPV4_DST
:
6833 if (flow
->dl_type
!= htons(ETH_TYPE_IP
)) {
6834 inconsistent_match(usable_protocols
);
6838 case OFPACT_SET_IP_DSCP
:
6839 case OFPACT_SET_IP_ECN
:
6840 case OFPACT_SET_IP_TTL
:
6841 case OFPACT_DEC_TTL
:
6842 if (!is_ip_any(flow
)) {
6843 inconsistent_match(usable_protocols
);
6847 case OFPACT_SET_L4_SRC_PORT
:
6848 case OFPACT_SET_L4_DST_PORT
:
6849 if (!is_ip_any(flow
) || (flow
->nw_frag
& FLOW_NW_FRAG_LATER
) ||
6850 (flow
->nw_proto
!= IPPROTO_TCP
&& flow
->nw_proto
!= IPPROTO_UDP
6851 && flow
->nw_proto
!= IPPROTO_SCTP
)) {
6852 inconsistent_match(usable_protocols
);
6854 /* Note on which transport protocol the port numbers are set.
6855 * This allows this set action to be converted to an OF1.2 set field
6857 if (a
->type
== OFPACT_SET_L4_SRC_PORT
) {
6858 ofpact_get_SET_L4_SRC_PORT(a
)->flow_ip_proto
= flow
->nw_proto
;
6860 ofpact_get_SET_L4_DST_PORT(a
)->flow_ip_proto
= flow
->nw_proto
;
6864 case OFPACT_REG_MOVE
:
6865 return nxm_reg_move_check(ofpact_get_REG_MOVE(a
), flow
);
6867 case OFPACT_SET_FIELD
:
6868 mf
= ofpact_get_SET_FIELD(a
)->field
;
6869 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6870 if (!mf_are_prereqs_ok(mf
, flow
) ||
6871 (mf
->id
== MFF_VLAN_VID
&& !(flow
->vlan_tci
& htons(VLAN_CFI
)))) {
6872 VLOG_WARN_RL(&rl
, "set_field %s lacks correct prerequisities",
6874 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
6876 /* Remember if we saw a vlan tag in the flow to aid translating to
6877 * OpenFlow 1.1 if need be. */
6878 ofpact_get_SET_FIELD(a
)->flow_has_vlan
=
6879 (flow
->vlan_tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
6880 if (mf
->id
== MFF_VLAN_TCI
) {
6881 /* The set field may add or remove the vlan tag,
6882 * Mark the status temporarily. */
6883 flow
->vlan_tci
= ofpact_get_SET_FIELD(a
)->value
.be16
;
6887 case OFPACT_STACK_PUSH
:
6888 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a
), flow
);
6890 case OFPACT_STACK_POP
:
6891 return nxm_stack_pop_check(ofpact_get_STACK_POP(a
), flow
);
6893 case OFPACT_SET_MPLS_LABEL
:
6894 case OFPACT_SET_MPLS_TC
:
6895 case OFPACT_SET_MPLS_TTL
:
6896 case OFPACT_DEC_MPLS_TTL
:
6897 if (!eth_type_mpls(flow
->dl_type
)) {
6898 inconsistent_match(usable_protocols
);
6902 case OFPACT_SET_TUNNEL
:
6903 case OFPACT_SET_QUEUE
:
6904 case OFPACT_POP_QUEUE
:
6905 case OFPACT_RESUBMIT
:
6908 case OFPACT_FIN_TIMEOUT
:
6909 if (flow
->nw_proto
!= IPPROTO_TCP
) {
6910 inconsistent_match(usable_protocols
);
6915 return learn_check(ofpact_get_LEARN(a
), flow
);
6917 case OFPACT_CONJUNCTION
:
6920 case OFPACT_MULTIPATH
:
6921 return multipath_check(ofpact_get_MULTIPATH(a
), flow
);
6927 case OFPACT_PUSH_MPLS
:
6928 flow
->dl_type
= ofpact_get_PUSH_MPLS(a
)->ethertype
;
6929 /* The packet is now MPLS and the MPLS payload is opaque.
6930 * Thus nothing can be assumed about the network protocol.
6931 * Temporarily mark that we have no nw_proto. */
6935 case OFPACT_POP_MPLS
:
6936 if (!eth_type_mpls(flow
->dl_type
)) {
6937 inconsistent_match(usable_protocols
);
6939 flow
->dl_type
= ofpact_get_POP_MPLS(a
)->ethertype
;
6946 struct ofpact_conntrack
*oc
= ofpact_get_CT(a
);
6949 if (!dl_type_is_ip_any(flow
->dl_type
)
6950 || (flow
->ct_state
& CS_INVALID
&& oc
->flags
& NX_CT_F_COMMIT
)) {
6951 inconsistent_match(usable_protocols
);
6954 if (oc
->zone_src
.field
) {
6955 return mf_check_src(&oc
->zone_src
, flow
);
6958 err
= ofpacts_check(oc
->actions
, ofpact_ct_get_action_len(oc
),
6959 flow
, max_ports
, table_id
, n_tables
,
6965 struct ofpact_nat
*on
= ofpact_get_NAT(a
);
6967 if (!dl_type_is_ip_any(flow
->dl_type
) ||
6968 (on
->range_af
== AF_INET
&& flow
->dl_type
!= htons(ETH_TYPE_IP
)) ||
6969 (on
->range_af
== AF_INET6
6970 && flow
->dl_type
!= htons(ETH_TYPE_IPV6
))) {
6971 inconsistent_match(usable_protocols
);
6976 case OFPACT_CLEAR_ACTIONS
:
6979 case OFPACT_WRITE_ACTIONS
: {
6980 /* Use a temporary copy of 'usable_protocols' because we can't check
6981 * consistency of an action set. */
6982 struct ofpact_nest
*on
= ofpact_get_WRITE_ACTIONS(a
);
6983 enum ofputil_protocol p
= *usable_protocols
;
6984 return ofpacts_check(on
->actions
, ofpact_nest_get_action_len(on
),
6985 flow
, max_ports
, table_id
, n_tables
, &p
);
6988 case OFPACT_WRITE_METADATA
:
6991 case OFPACT_METER
: {
6992 uint32_t mid
= ofpact_get_METER(a
)->meter_id
;
6993 if (mid
== 0 || mid
> OFPM13_MAX
) {
6994 return OFPERR_OFPMMFC_INVALID_METER
;
6999 case OFPACT_GOTO_TABLE
: {
7000 uint8_t goto_table
= ofpact_get_GOTO_TABLE(a
)->table_id
;
7001 if ((table_id
!= 255 && goto_table
<= table_id
)
7002 || (n_tables
!= 255 && goto_table
>= n_tables
)) {
7003 return OFPERR_OFPBIC_BAD_TABLE_ID
;
7011 case OFPACT_UNROLL_XLATE
:
7012 /* UNROLL is an internal action that should never be seen via
7014 return OFPERR_OFPBAC_BAD_TYPE
;
7016 case OFPACT_DEBUG_RECIRC
:
7024 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
7025 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
7026 * switch with no more than 'max_ports' ports.
7028 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
7029 * '*usable_protocols' the protocols that forbid the inconsistency. (An
7030 * example of an inconsistency between match and actions is a flow that does
7031 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
7033 * May annotate ofpacts with information gathered from the 'flow'.
7035 * May temporarily modify 'flow', but restores the changes before returning. */
7037 ofpacts_check(struct ofpact ofpacts
[], size_t ofpacts_len
,
7038 struct flow
*flow
, ofp_port_t max_ports
,
7039 uint8_t table_id
, uint8_t n_tables
,
7040 enum ofputil_protocol
*usable_protocols
)
7043 ovs_be16 dl_type
= flow
->dl_type
;
7044 ovs_be16 vlan_tci
= flow
->vlan_tci
;
7045 uint8_t nw_proto
= flow
->nw_proto
;
7046 enum ofperr error
= 0;
7048 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7049 error
= ofpact_check__(usable_protocols
, a
, flow
,
7050 max_ports
, table_id
, n_tables
);
7055 /* Restore fields that may have been modified. */
7056 flow
->dl_type
= dl_type
;
7057 flow
->vlan_tci
= vlan_tci
;
7058 flow
->nw_proto
= nw_proto
;
7062 /* Like ofpacts_check(), but reports inconsistencies as
7063 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
7065 ofpacts_check_consistency(struct ofpact ofpacts
[], size_t ofpacts_len
,
7066 struct flow
*flow
, ofp_port_t max_ports
,
7067 uint8_t table_id
, uint8_t n_tables
,
7068 enum ofputil_protocol usable_protocols
)
7070 enum ofputil_protocol p
= usable_protocols
;
7073 error
= ofpacts_check(ofpacts
, ofpacts_len
, flow
, max_ports
,
7074 table_id
, n_tables
, &p
);
7075 return (error
? error
7076 : p
!= usable_protocols
? OFPERR_OFPBAC_MATCH_INCONSISTENT
7080 /* Returns the destination field that 'ofpact' would write to, or NULL
7081 * if the action would not write to an mf_field. */
7082 const struct mf_field
*
7083 ofpact_get_mf_dst(const struct ofpact
*ofpact
)
7085 if (ofpact
->type
== OFPACT_SET_FIELD
) {
7086 const struct ofpact_set_field
*orl
;
7088 orl
= CONTAINER_OF(ofpact
, struct ofpact_set_field
, ofpact
);
7090 } else if (ofpact
->type
== OFPACT_REG_MOVE
) {
7091 const struct ofpact_reg_move
*orm
;
7093 orm
= CONTAINER_OF(ofpact
, struct ofpact_reg_move
, ofpact
);
7094 return orm
->dst
.field
;
7101 unsupported_nesting(enum ofpact_type action
, enum ofpact_type outer_action
)
7103 VLOG_WARN("%s action doesn't support nested action %s",
7104 ofpact_name(outer_action
), ofpact_name(action
));
7105 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7109 field_requires_ct(enum mf_field_id field
)
7111 return field
== MFF_CT_MARK
|| field
== MFF_CT_LABEL
;
7114 /* Apply nesting constraints for actions */
7116 ofpacts_verify_nested(const struct ofpact
*a
, enum ofpact_type outer_action
)
7118 const struct mf_field
*field
= ofpact_get_mf_dst(a
);
7120 if (field
&& field_requires_ct(field
->id
) && outer_action
!= OFPACT_CT
) {
7121 VLOG_WARN("cannot set CT fields outside of ct action");
7122 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
7124 if (a
->type
== OFPACT_NAT
) {
7125 if (outer_action
!= OFPACT_CT
) {
7126 VLOG_WARN("Cannot have NAT action outside of \"ct\" action");
7127 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
7133 ovs_assert(outer_action
== OFPACT_WRITE_ACTIONS
7134 || outer_action
== OFPACT_CT
);
7136 if (outer_action
== OFPACT_CT
) {
7138 return unsupported_nesting(a
->type
, outer_action
);
7139 } else if (!field_requires_ct(field
->id
)) {
7140 VLOG_WARN("%s action doesn't support nested modification "
7141 "of %s", ofpact_name(outer_action
), field
->name
);
7142 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7150 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
7151 * appropriate order as defined by the OpenFlow spec and as required by Open
7154 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
7155 * instructions that are allowed within 'ofpacts[]'.
7157 * If 'outer_action' is not zero, it specifies that the actions are nested
7158 * within another action of type 'outer_action'. */
7160 ofpacts_verify(const struct ofpact ofpacts
[], size_t ofpacts_len
,
7161 uint32_t allowed_ovsinsts
, enum ofpact_type outer_action
)
7163 const struct ofpact
*a
;
7164 enum ovs_instruction_type inst
;
7166 inst
= OVSINST_OFPIT13_METER
;
7167 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7168 enum ovs_instruction_type next
;
7171 if (a
->type
== OFPACT_CONJUNCTION
) {
7172 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7173 if (a
->type
!= OFPACT_CONJUNCTION
&& a
->type
!= OFPACT_NOTE
) {
7174 VLOG_WARN("\"conjunction\" actions may be used along with "
7175 "\"note\" but not any other kind of action "
7176 "(such as the \"%s\" action used here)",
7177 ofpact_name(a
->type
));
7178 return OFPERR_NXBAC_BAD_CONJUNCTION
;
7184 error
= ofpacts_verify_nested(a
, outer_action
);
7189 next
= ovs_instruction_type_from_ofpact_type(a
->type
);
7191 && (inst
== OVSINST_OFPIT11_APPLY_ACTIONS
7194 const char *name
= ovs_instruction_name_from_type(inst
);
7195 const char *next_name
= ovs_instruction_name_from_type(next
);
7198 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
7199 "1.1+ compatibility", name
);
7201 VLOG_WARN("invalid instruction ordering: %s must appear "
7202 "before %s, for OpenFlow 1.1+ compatibility",
7205 return OFPERR_OFPBAC_UNSUPPORTED_ORDER
;
7207 if (!((1u << next
) & allowed_ovsinsts
)) {
7208 const char *name
= ovs_instruction_name_from_type(next
);
7210 VLOG_WARN("%s instruction not allowed here", name
);
7211 return OFPERR_OFPBIC_UNSUP_INST
;
7220 /* Converting ofpacts to OpenFlow. */
7223 encode_ofpact(const struct ofpact
*a
, enum ofp_version ofp_version
,
7227 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7228 case OFPACT_##ENUM: \
7229 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
7238 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
7239 * actions in 'openflow', appending the actions to any existing data in
7242 ofpacts_put_openflow_actions(const struct ofpact ofpacts
[], size_t ofpacts_len
,
7243 struct ofpbuf
*openflow
,
7244 enum ofp_version ofp_version
)
7246 const struct ofpact
*a
;
7247 size_t start_size
= openflow
->size
;
7249 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7250 encode_ofpact(a
, ofp_version
, openflow
);
7252 return openflow
->size
- start_size
;
7255 static enum ovs_instruction_type
7256 ofpact_is_apply_actions(const struct ofpact
*a
)
7258 return (ovs_instruction_type_from_ofpact_type(a
->type
)
7259 == OVSINST_OFPIT11_APPLY_ACTIONS
);
7263 ofpacts_put_openflow_instructions(const struct ofpact ofpacts
[],
7265 struct ofpbuf
*openflow
,
7266 enum ofp_version ofp_version
)
7268 const struct ofpact
*end
= ofpact_end(ofpacts
, ofpacts_len
);
7269 const struct ofpact
*a
;
7271 if (ofp_version
== OFP10_VERSION
) {
7272 ofpacts_put_openflow_actions(ofpacts
, ofpacts_len
, openflow
,
7279 if (ofpact_is_apply_actions(a
)) {
7280 size_t ofs
= openflow
->size
;
7282 instruction_put_OFPIT11_APPLY_ACTIONS(openflow
);
7284 encode_ofpact(a
, ofp_version
, openflow
);
7286 } while (a
< end
&& ofpact_is_apply_actions(a
));
7287 ofpacts_update_instruction_actions(openflow
, ofs
);
7289 encode_ofpact(a
, ofp_version
, openflow
);
7295 /* Sets of supported actions. */
7297 /* Two-way translation between OVS's internal "OFPACT_*" representation of
7298 * actions and the "OFPAT_*" representation used in some OpenFlow version.
7299 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
7300 * instance is specific to one OpenFlow version.) */
7302 enum ofpact_type ofpact
; /* Internal name for action type. */
7303 int ofpat
; /* OFPAT_* number from OpenFlow spec. */
7306 static const struct ofpact_map
*
7307 get_ofpact_map(enum ofp_version version
)
7309 /* OpenFlow 1.0 actions. */
7310 static const struct ofpact_map of10
[] = {
7311 { OFPACT_OUTPUT
, 0 },
7312 { OFPACT_SET_VLAN_VID
, 1 },
7313 { OFPACT_SET_VLAN_PCP
, 2 },
7314 { OFPACT_STRIP_VLAN
, 3 },
7315 { OFPACT_SET_ETH_SRC
, 4 },
7316 { OFPACT_SET_ETH_DST
, 5 },
7317 { OFPACT_SET_IPV4_SRC
, 6 },
7318 { OFPACT_SET_IPV4_DST
, 7 },
7319 { OFPACT_SET_IP_DSCP
, 8 },
7320 { OFPACT_SET_L4_SRC_PORT
, 9 },
7321 { OFPACT_SET_L4_DST_PORT
, 10 },
7322 { OFPACT_ENQUEUE
, 11 },
7326 /* OpenFlow 1.1 actions. */
7327 static const struct ofpact_map of11
[] = {
7328 { OFPACT_OUTPUT
, 0 },
7329 { OFPACT_SET_VLAN_VID
, 1 },
7330 { OFPACT_SET_VLAN_PCP
, 2 },
7331 { OFPACT_SET_ETH_SRC
, 3 },
7332 { OFPACT_SET_ETH_DST
, 4 },
7333 { OFPACT_SET_IPV4_SRC
, 5 },
7334 { OFPACT_SET_IPV4_DST
, 6 },
7335 { OFPACT_SET_IP_DSCP
, 7 },
7336 { OFPACT_SET_IP_ECN
, 8 },
7337 { OFPACT_SET_L4_SRC_PORT
, 9 },
7338 { OFPACT_SET_L4_DST_PORT
, 10 },
7339 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7340 /* OFPAT_COPY_TTL_IN (12) not supported. */
7341 { OFPACT_SET_MPLS_LABEL
, 13 },
7342 { OFPACT_SET_MPLS_TC
, 14 },
7343 { OFPACT_SET_MPLS_TTL
, 15 },
7344 { OFPACT_DEC_MPLS_TTL
, 16 },
7345 { OFPACT_PUSH_VLAN
, 17 },
7346 { OFPACT_STRIP_VLAN
, 18 },
7347 { OFPACT_PUSH_MPLS
, 19 },
7348 { OFPACT_POP_MPLS
, 20 },
7349 { OFPACT_SET_QUEUE
, 21 },
7350 { OFPACT_GROUP
, 22 },
7351 { OFPACT_SET_IP_TTL
, 23 },
7352 { OFPACT_DEC_TTL
, 24 },
7356 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
7357 static const struct ofpact_map of12
[] = {
7358 { OFPACT_OUTPUT
, 0 },
7359 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7360 /* OFPAT_COPY_TTL_IN (12) not supported. */
7361 { OFPACT_SET_MPLS_TTL
, 15 },
7362 { OFPACT_DEC_MPLS_TTL
, 16 },
7363 { OFPACT_PUSH_VLAN
, 17 },
7364 { OFPACT_STRIP_VLAN
, 18 },
7365 { OFPACT_PUSH_MPLS
, 19 },
7366 { OFPACT_POP_MPLS
, 20 },
7367 { OFPACT_SET_QUEUE
, 21 },
7368 { OFPACT_GROUP
, 22 },
7369 { OFPACT_SET_IP_TTL
, 23 },
7370 { OFPACT_DEC_TTL
, 24 },
7371 { OFPACT_SET_FIELD
, 25 },
7372 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
7373 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
7394 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
7395 * values, into a bitmap of actions suitable for OpenFlow 'version', and
7396 * returns the result. */
7398 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap
, enum ofp_version version
)
7400 uint32_t openflow_bitmap
= 0;
7401 const struct ofpact_map
*x
;
7403 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
7404 if (ofpacts_bitmap
& (UINT64_C(1) << x
->ofpact
)) {
7405 openflow_bitmap
|= 1u << x
->ofpat
;
7408 return htonl(openflow_bitmap
);
7411 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
7412 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
7413 * and returns the result. */
7415 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap
, enum ofp_version version
)
7417 uint64_t ofpact_bitmap
= 0;
7418 const struct ofpact_map
*x
;
7420 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
7421 if (ofpat_bitmap
& htonl(1u << x
->ofpat
)) {
7422 ofpact_bitmap
|= UINT64_C(1) << x
->ofpact
;
7425 return ofpact_bitmap
;
7428 /* Appends to 's' a string representation of the set of OFPACT_* represented
7429 * by 'ofpacts_bitmap'. */
7431 ofpact_bitmap_format(uint64_t ofpacts_bitmap
, struct ds
*s
)
7433 if (!ofpacts_bitmap
) {
7434 ds_put_cstr(s
, "<none>");
7436 while (ofpacts_bitmap
) {
7437 ds_put_format(s
, "%s ",
7438 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap
)));
7439 ofpacts_bitmap
= zero_rightmost_1bit(ofpacts_bitmap
);
7445 /* Returns true if 'action' outputs to 'port', false otherwise. */
7447 ofpact_outputs_to_port(const struct ofpact
*ofpact
, ofp_port_t port
)
7449 switch (ofpact
->type
) {
7451 return ofpact_get_OUTPUT(ofpact
)->port
== port
;
7452 case OFPACT_ENQUEUE
:
7453 return ofpact_get_ENQUEUE(ofpact
)->port
== port
;
7454 case OFPACT_CONTROLLER
:
7455 return port
== OFPP_CONTROLLER
;
7457 case OFPACT_OUTPUT_REG
:
7458 case OFPACT_OUTPUT_TRUNC
:
7460 case OFPACT_SET_VLAN_VID
:
7461 case OFPACT_SET_VLAN_PCP
:
7462 case OFPACT_STRIP_VLAN
:
7463 case OFPACT_PUSH_VLAN
:
7464 case OFPACT_SET_ETH_SRC
:
7465 case OFPACT_SET_ETH_DST
:
7466 case OFPACT_SET_IPV4_SRC
:
7467 case OFPACT_SET_IPV4_DST
:
7468 case OFPACT_SET_IP_DSCP
:
7469 case OFPACT_SET_IP_ECN
:
7470 case OFPACT_SET_IP_TTL
:
7471 case OFPACT_SET_L4_SRC_PORT
:
7472 case OFPACT_SET_L4_DST_PORT
:
7473 case OFPACT_REG_MOVE
:
7474 case OFPACT_SET_FIELD
:
7475 case OFPACT_STACK_PUSH
:
7476 case OFPACT_STACK_POP
:
7477 case OFPACT_DEC_TTL
:
7478 case OFPACT_SET_MPLS_LABEL
:
7479 case OFPACT_SET_MPLS_TC
:
7480 case OFPACT_SET_MPLS_TTL
:
7481 case OFPACT_DEC_MPLS_TTL
:
7482 case OFPACT_SET_TUNNEL
:
7483 case OFPACT_WRITE_METADATA
:
7484 case OFPACT_SET_QUEUE
:
7485 case OFPACT_POP_QUEUE
:
7486 case OFPACT_FIN_TIMEOUT
:
7487 case OFPACT_RESUBMIT
:
7489 case OFPACT_CONJUNCTION
:
7490 case OFPACT_MULTIPATH
:
7493 case OFPACT_UNROLL_XLATE
:
7494 case OFPACT_PUSH_MPLS
:
7495 case OFPACT_POP_MPLS
:
7497 case OFPACT_CLEAR_ACTIONS
:
7498 case OFPACT_WRITE_ACTIONS
:
7499 case OFPACT_GOTO_TABLE
:
7502 case OFPACT_DEBUG_RECIRC
:
7510 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7511 * to 'port', false otherwise. */
7513 ofpacts_output_to_port(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
7516 const struct ofpact
*a
;
7518 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
7519 if (ofpact_outputs_to_port(a
, port
)) {
7527 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7528 * to 'group', false otherwise. */
7530 ofpacts_output_to_group(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
7533 const struct ofpact
*a
;
7535 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
7536 if (a
->type
== OFPACT_GROUP
7537 && ofpact_get_GROUP(a
)->group_id
== group_id
) {
7546 ofpacts_equal(const struct ofpact
*a
, size_t a_len
,
7547 const struct ofpact
*b
, size_t b_len
)
7549 return a_len
== b_len
&& !memcmp(a
, b
, a_len
);
7552 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
7553 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
7555 * This function relies on the order of 'ofpacts' being correct (as checked by
7556 * ofpacts_verify()). */
7558 ofpacts_get_meter(const struct ofpact ofpacts
[], size_t ofpacts_len
)
7560 const struct ofpact
*a
;
7562 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7563 enum ovs_instruction_type inst
;
7565 inst
= ovs_instruction_type_from_ofpact_type(a
->type
);
7566 if (a
->type
== OFPACT_METER
) {
7567 return ofpact_get_METER(a
)->meter_id
;
7568 } else if (inst
> OVSINST_OFPIT13_METER
) {
7576 /* Formatting ofpacts. */
7579 ofpact_format(const struct ofpact
*a
, struct ds
*s
)
7582 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7583 case OFPACT_##ENUM: \
7584 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
7593 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
7594 * 'ofpacts' to 'string'. */
7596 ofpacts_format(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
7600 ds_put_format(string
, "%sdrop%s", colors
.drop
, colors
.end
);
7602 const struct ofpact
*a
;
7604 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7606 ds_put_char(string
, ',');
7609 /* XXX write-actions */
7610 ofpact_format(a
, string
);
7615 /* Internal use by helpers. */
7617 /* Implementation of ofpact_put_<ENUM>(). */
7619 ofpact_put(struct ofpbuf
*ofpacts
, enum ofpact_type type
, size_t len
)
7621 struct ofpact
*ofpact
;
7623 ofpacts
->header
= ofpbuf_put_uninit(ofpacts
, len
);
7624 ofpact
= ofpacts
->header
;
7625 ofpact_init(ofpact
, type
, len
);
7629 /* Implementation of ofpact_init_<ENUM>(). */
7631 ofpact_init(struct ofpact
*ofpact
, enum ofpact_type type
, size_t len
)
7633 memset(ofpact
, 0, len
);
7634 ofpact
->type
= type
;
7639 /* Implementation of ofpact_finish_<ENUM>().
7641 * Finishes composing a variable-length action (begun using
7642 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
7643 * bytes and updating its embedded length field. See the large comment near
7644 * the end of ofp-actions.h for more information.
7646 * May reallocate 'ofpacts'. Callers should consider updating their 'ofpact'
7647 * pointer to the return value of this function. */
7649 ofpact_finish(struct ofpbuf
*ofpacts
, struct ofpact
*ofpact
)
7653 ovs_assert(ofpact
== ofpacts
->header
);
7654 len
= (char *) ofpbuf_tail(ofpacts
) - (char *) ofpact
;
7655 ovs_assert(len
> 0 && len
<= UINT16_MAX
);
7657 ofpbuf_padto(ofpacts
, OFPACT_ALIGN(ofpacts
->size
));
7659 return ofpacts
->header
;
7662 static char * OVS_WARN_UNUSED_RESULT
7663 ofpact_parse(enum ofpact_type type
, char *value
, struct ofpbuf
*ofpacts
,
7664 enum ofputil_protocol
*usable_protocols
)
7667 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7668 case OFPACT_##ENUM: \
7669 return parse_##ENUM(value, ofpacts, usable_protocols);
7678 ofpact_type_from_name(const char *name
, enum ofpact_type
*type
)
7680 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7681 if (!strcasecmp(name, NAME)) { \
7682 *type = OFPACT_##ENUM; \
7691 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
7693 * Returns NULL if successful, otherwise a malloc()'d string describing the
7694 * error. The caller is responsible for freeing the returned string.
7696 * If 'outer_action' is specified, indicates that the actions being parsed
7697 * are nested within another action of the type specified in 'outer_action'. */
7698 static char * OVS_WARN_UNUSED_RESULT
7699 ofpacts_parse__(char *str
, struct ofpbuf
*ofpacts
,
7700 enum ofputil_protocol
*usable_protocols
,
7701 bool allow_instructions
, enum ofpact_type outer_action
)
7710 while (ofputil_parse_key_value(&pos
, &key
, &value
)) {
7711 enum ovs_instruction_type inst
= OVSINST_OFPIT11_APPLY_ACTIONS
;
7712 enum ofpact_type type
;
7716 if (ofpact_type_from_name(key
, &type
)) {
7717 error
= ofpact_parse(type
, value
, ofpacts
, usable_protocols
);
7718 inst
= ovs_instruction_type_from_ofpact_type(type
);
7719 } else if (!strcasecmp(key
, "mod_vlan_vid")) {
7720 error
= parse_set_vlan_vid(value
, ofpacts
, true);
7721 } else if (!strcasecmp(key
, "mod_vlan_pcp")) {
7722 error
= parse_set_vlan_pcp(value
, ofpacts
, true);
7723 } else if (!strcasecmp(key
, "set_nw_ttl")) {
7724 error
= parse_SET_IP_TTL(value
, ofpacts
, usable_protocols
);
7725 } else if (!strcasecmp(key
, "pop_vlan")) {
7726 error
= parse_pop_vlan(ofpacts
);
7727 } else if (!strcasecmp(key
, "set_tunnel64")) {
7728 error
= parse_set_tunnel(value
, ofpacts
,
7729 NXAST_RAW_SET_TUNNEL64
);
7730 } else if (!strcasecmp(key
, "load")) {
7731 error
= parse_reg_load(value
, ofpacts
);
7732 } else if (!strcasecmp(key
, "bundle_load")) {
7733 error
= parse_bundle_load(value
, ofpacts
);
7734 } else if (!strcasecmp(key
, "drop")) {
7736 } else if (!strcasecmp(key
, "apply_actions")) {
7737 return xstrdup("apply_actions is the default instruction");
7738 } else if (ofputil_port_from_string(key
, &port
)) {
7739 ofpact_put_OUTPUT(ofpacts
)->port
= port
;
7741 return xasprintf("unknown action %s", key
);
7747 if (inst
!= OVSINST_OFPIT11_APPLY_ACTIONS
) {
7748 if (!allow_instructions
) {
7749 return xasprintf("only actions are allowed here (not "
7751 ovs_instruction_name_from_type(inst
));
7753 if (inst
== prev_inst
) {
7754 return xasprintf("instruction %s may be specified only once",
7755 ovs_instruction_name_from_type(inst
));
7758 if (prev_inst
!= -1 && inst
< prev_inst
) {
7759 return xasprintf("instruction %s must be specified before %s",
7760 ovs_instruction_name_from_type(inst
),
7761 ovs_instruction_name_from_type(prev_inst
));
7766 if (drop
&& ofpacts
->size
) {
7767 return xstrdup("\"drop\" must not be accompanied by any other action "
7771 retval
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
,
7773 ? (1u << N_OVS_INSTRUCTIONS
) - 1
7774 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS
),
7777 return xstrdup("Incorrect instruction ordering");
7783 static char * OVS_WARN_UNUSED_RESULT
7784 ofpacts_parse(char *str
, struct ofpbuf
*ofpacts
,
7785 enum ofputil_protocol
*usable_protocols
, bool allow_instructions
,
7786 enum ofpact_type outer_action
)
7788 uint32_t orig_size
= ofpacts
->size
;
7789 char *error
= ofpacts_parse__(str
, ofpacts
, usable_protocols
,
7790 allow_instructions
, outer_action
);
7792 ofpacts
->size
= orig_size
;
7797 static char * OVS_WARN_UNUSED_RESULT
7798 ofpacts_parse_copy(const char *s_
, struct ofpbuf
*ofpacts
,
7799 enum ofputil_protocol
*usable_protocols
,
7800 bool allow_instructions
, enum ofpact_type outer_action
)
7804 *usable_protocols
= OFPUTIL_P_ANY
;
7807 error
= ofpacts_parse(s
, ofpacts
, usable_protocols
, allow_instructions
,
7814 /* Parses 's' as a set of OpenFlow actions and appends the actions to
7815 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
7816 * that are nested within the action of type 'outer_action'.
7818 * Returns NULL if successful, otherwise a malloc()'d string describing the
7819 * error. The caller is responsible for freeing the returned string. */
7820 char * OVS_WARN_UNUSED_RESULT
7821 ofpacts_parse_actions(const char *s
, struct ofpbuf
*ofpacts
,
7822 enum ofputil_protocol
*usable_protocols
)
7824 return ofpacts_parse_copy(s
, ofpacts
, usable_protocols
, false, 0);
7827 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
7830 * Returns NULL if successful, otherwise a malloc()'d string describing the
7831 * error. The caller is responsible for freeing the returned string. */
7832 char * OVS_WARN_UNUSED_RESULT
7833 ofpacts_parse_instructions(const char *s
, struct ofpbuf
*ofpacts
,
7834 enum ofputil_protocol
*usable_protocols
)
7836 return ofpacts_parse_copy(s
, ofpacts
, usable_protocols
, true, 0);
7840 ofpact_name(enum ofpact_type type
)
7843 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7850 /* Low-level action decoding and encoding functions. */
7852 /* Everything needed to identify a particular OpenFlow action. */
7853 struct ofpact_hdrs
{
7854 uint32_t vendor
; /* 0 if standard, otherwise a vendor code. */
7855 uint16_t type
; /* Type if standard, otherwise subtype. */
7856 uint8_t ofp_version
; /* From ofp_header. */
7859 /* Information about a particular OpenFlow action. */
7860 struct ofpact_raw_instance
{
7861 /* The action's identity. */
7862 struct ofpact_hdrs hdrs
;
7863 enum ofp_raw_action_type raw
;
7865 /* Looking up the action. */
7866 struct hmap_node decode_node
; /* Based on 'hdrs'. */
7867 struct hmap_node encode_node
; /* Based on 'raw' + 'hdrs.ofp_version'. */
7869 /* The action's encoded size.
7871 * If this action is fixed-length, 'min_length' == 'max_length'.
7872 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7873 * OFP_ACTION_ALIGN) == 65528. */
7874 unsigned short int min_length
;
7875 unsigned short int max_length
;
7877 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7878 * offset of that argument from the beginning of the action and 'arg_len'
7879 * its length, both in bytes.
7881 * For actions that take other forms, these are both zero. */
7882 unsigned short int arg_ofs
;
7883 unsigned short int arg_len
;
7885 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7888 /* If this action is deprecated, a human-readable string with a brief
7890 const char *deprecation
;
7893 /* Action header. */
7894 struct ofp_action_header
{
7895 /* The meaning of other values of 'type' generally depends on the OpenFlow
7896 * version (see enum ofp_raw_action_type).
7898 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7899 * designates an OpenFlow vendor ID and that the remainder of the action
7900 * structure has a vendor-defined meaning.
7902 #define OFPAT_VENDOR 0xffff
7905 /* Always a multiple of 8. */
7908 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7909 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
7912 OFP_ASSERT(sizeof(struct ofp_action_header
) == 8);
7914 /* Header for Nicira-defined actions and for ONF vendor extensions.
7916 * This cannot be used as an entirely generic vendor extension action header,
7917 * because OpenFlow does not specify the location or size of the action
7918 * subtype; it just happens that ONF extensions and Nicira extensions share
7920 struct ext_action_header
{
7921 ovs_be16 type
; /* OFPAT_VENDOR. */
7922 ovs_be16 len
; /* At least 16. */
7923 ovs_be32 vendor
; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
7924 ovs_be16 subtype
; /* See enum ofp_raw_action_type. */
7927 OFP_ASSERT(sizeof(struct ext_action_header
) == 16);
7930 ofpact_hdrs_equal(const struct ofpact_hdrs
*a
,
7931 const struct ofpact_hdrs
*b
)
7933 return (a
->vendor
== b
->vendor
7934 && a
->type
== b
->type
7935 && a
->ofp_version
== b
->ofp_version
);
7939 ofpact_hdrs_hash(const struct ofpact_hdrs
*hdrs
)
7941 return hash_2words(hdrs
->vendor
, (hdrs
->type
<< 16) | hdrs
->ofp_version
);
7944 #include "ofp-actions.inc2"
7946 static struct hmap
*
7947 ofpact_decode_hmap(void)
7949 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
7950 static struct hmap hmap
;
7952 if (ovsthread_once_start(&once
)) {
7953 struct ofpact_raw_instance
*inst
;
7956 for (inst
= all_raw_instances
;
7957 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
7959 hmap_insert(&hmap
, &inst
->decode_node
,
7960 ofpact_hdrs_hash(&inst
->hdrs
));
7962 ovsthread_once_done(&once
);
7967 static struct hmap
*
7968 ofpact_encode_hmap(void)
7970 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
7971 static struct hmap hmap
;
7973 if (ovsthread_once_start(&once
)) {
7974 struct ofpact_raw_instance
*inst
;
7977 for (inst
= all_raw_instances
;
7978 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
7980 hmap_insert(&hmap
, &inst
->encode_node
,
7981 hash_2words(inst
->raw
, inst
->hdrs
.ofp_version
));
7983 ovsthread_once_done(&once
);
7989 ofpact_decode_raw(enum ofp_version ofp_version
,
7990 const struct ofp_action_header
*oah
, size_t length
,
7991 const struct ofpact_raw_instance
**instp
)
7993 const struct ofpact_raw_instance
*inst
;
7994 struct ofpact_hdrs hdrs
;
7997 if (length
< sizeof *oah
) {
7998 return OFPERR_OFPBAC_BAD_LEN
;
8001 /* Get base action type. */
8002 if (oah
->type
== htons(OFPAT_VENDOR
)) {
8004 hdrs
.vendor
= ntohl(oah
->vendor
);
8005 if (hdrs
.vendor
== NX_VENDOR_ID
|| hdrs
.vendor
== ONF_VENDOR_ID
) {
8006 /* Get extension subtype. */
8007 const struct ext_action_header
*nah
;
8009 nah
= ALIGNED_CAST(const struct ext_action_header
*, oah
);
8010 if (length
< sizeof *nah
) {
8011 return OFPERR_OFPBAC_BAD_LEN
;
8013 hdrs
.type
= ntohs(nah
->subtype
);
8015 VLOG_WARN_RL(&rl
, "OpenFlow action has unknown vendor %#"PRIx32
,
8017 return OFPERR_OFPBAC_BAD_VENDOR
;
8021 hdrs
.type
= ntohs(oah
->type
);
8024 hdrs
.ofp_version
= ofp_version
;
8025 HMAP_FOR_EACH_WITH_HASH (inst
, decode_node
, ofpact_hdrs_hash(&hdrs
),
8026 ofpact_decode_hmap()) {
8027 if (ofpact_hdrs_equal(&hdrs
, &inst
->hdrs
)) {
8034 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
8035 : OFPERR_OFPBAC_BAD_TYPE
);
8039 ofpact_pull_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
8040 enum ofp_raw_action_type
*raw
, uint64_t *arg
)
8042 const struct ofp_action_header
*oah
= buf
->data
;
8043 const struct ofpact_raw_instance
*action
;
8044 unsigned int length
;
8048 error
= ofpact_decode_raw(ofp_version
, oah
, buf
->size
, &action
);
8053 if (action
->deprecation
) {
8054 VLOG_INFO_RL(&rl
, "%s is deprecated in %s (%s)",
8055 action
->name
, ofputil_version_to_string(ofp_version
),
8056 action
->deprecation
);
8059 length
= ntohs(oah
->len
);
8060 if (length
> buf
->size
) {
8061 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u exceeds action buffer "
8062 "length %"PRIu32
, action
->name
, length
, buf
->size
);
8063 return OFPERR_OFPBAC_BAD_LEN
;
8065 if (length
< action
->min_length
|| length
> action
->max_length
) {
8066 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u not in valid range "
8067 "[%hu,%hu]", action
->name
, length
,
8068 action
->min_length
, action
->max_length
);
8069 return OFPERR_OFPBAC_BAD_LEN
;
8072 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u is not a multiple "
8073 "of 8", action
->name
, length
);
8074 return OFPERR_OFPBAC_BAD_LEN
;
8079 if (action
->arg_len
) {
8083 p
= ofpbuf_at_assert(buf
, action
->arg_ofs
, action
->arg_len
);
8084 for (i
= 0; i
< action
->arg_len
; i
++) {
8085 *arg
= (*arg
<< 8) | p
[i
];
8089 ofpbuf_pull(buf
, length
);
8094 static const struct ofpact_raw_instance
*
8095 ofpact_raw_lookup(enum ofp_version ofp_version
, enum ofp_raw_action_type raw
)
8097 const struct ofpact_raw_instance
*inst
;
8099 HMAP_FOR_EACH_WITH_HASH (inst
, encode_node
, hash_2words(raw
, ofp_version
),
8100 ofpact_encode_hmap()) {
8101 if (inst
->raw
== raw
&& inst
->hdrs
.ofp_version
== ofp_version
) {
8109 ofpact_put_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
8110 enum ofp_raw_action_type raw
, uint64_t arg
)
8112 const struct ofpact_raw_instance
*inst
;
8113 struct ofp_action_header
*oah
;
8114 const struct ofpact_hdrs
*hdrs
;
8116 inst
= ofpact_raw_lookup(ofp_version
, raw
);
8119 oah
= ofpbuf_put_zeros(buf
, inst
->min_length
);
8120 oah
->type
= htons(hdrs
->vendor
? OFPAT_VENDOR
: hdrs
->type
);
8121 oah
->len
= htons(inst
->min_length
);
8122 oah
->vendor
= htonl(hdrs
->vendor
);
8124 switch (hdrs
->vendor
) {
8129 case ONF_VENDOR_ID
: {
8130 struct ext_action_header
*nah
= (struct ext_action_header
*) oah
;
8131 nah
->subtype
= htons(hdrs
->type
);
8139 if (inst
->arg_len
) {
8140 uint8_t *p
= (uint8_t *) oah
+ inst
->arg_ofs
+ inst
->arg_len
;
8143 for (i
= 0; i
< inst
->arg_len
; i
++) {
8155 pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
)
8157 struct ofp_action_header
*oah
;
8159 ofpbuf_put_zeros(openflow
, PAD_SIZE(openflow
->size
- start_ofs
,
8162 oah
= ofpbuf_at_assert(openflow
, start_ofs
, sizeof *oah
);
8163 oah
->len
= htons(openflow
->size
- start_ofs
);