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 /* 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 /* ## ------------------ ## */
305 /* ## Debugging actions. ## */
306 /* ## ------------------ ## */
308 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
309 /* accepts them only if started with --enable-dummy. */
311 /* NX1.0+(255): void. */
312 NXAST_RAW_DEBUG_RECIRC
,
315 /* OpenFlow actions are always a multiple of 8 bytes in length. */
316 #define OFP_ACTION_ALIGN 8
318 /* Define a few functions for working with instructions. */
319 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
320 static inline const struct STRUCT * OVS_UNUSED \
321 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
323 ovs_assert(inst->type == htons(ENUM)); \
324 return ALIGNED_CAST(struct STRUCT *, inst); \
327 static inline void OVS_UNUSED \
328 instruction_init_##ENUM(struct STRUCT *s) \
330 memset(s, 0, sizeof *s); \
331 s->type = htons(ENUM); \
332 s->len = htons(sizeof *s); \
335 static inline struct STRUCT * OVS_UNUSED \
336 instruction_put_##ENUM(struct ofpbuf *buf) \
338 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
339 instruction_init_##ENUM(s); \
345 static void ofpacts_update_instruction_actions(struct ofpbuf
*openflow
,
347 static void pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
);
349 static enum ofperr
ofpacts_verify(const struct ofpact
[], size_t ofpacts_len
,
350 uint32_t allowed_ovsinsts
,
351 enum ofpact_type outer_action
);
353 static void ofpact_put_set_field(struct ofpbuf
*openflow
, enum ofp_version
,
354 enum mf_field_id
, uint64_t value
);
356 static enum ofperr
ofpact_pull_raw(struct ofpbuf
*, enum ofp_version
,
357 enum ofp_raw_action_type
*, uint64_t *arg
);
358 static void *ofpact_put_raw(struct ofpbuf
*, enum ofp_version
,
359 enum ofp_raw_action_type
, uint64_t arg
);
361 static char *OVS_WARN_UNUSED_RESULT
ofpacts_parse(
362 char *str
, struct ofpbuf
*ofpacts
, enum ofputil_protocol
*usable_protocols
,
363 bool allow_instructions
, enum ofpact_type outer_action
);
364 static enum ofperr
ofpacts_pull_openflow_actions__(
365 struct ofpbuf
*openflow
, unsigned int actions_len
,
366 enum ofp_version version
, uint32_t allowed_ovsinsts
,
367 struct ofpbuf
*ofpacts
, enum ofpact_type outer_action
);
368 static char * OVS_WARN_UNUSED_RESULT
ofpacts_parse_copy(
369 const char *s_
, struct ofpbuf
*ofpacts
,
370 enum ofputil_protocol
*usable_protocols
,
371 bool allow_instructions
, enum ofpact_type outer_action
);
373 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
374 * nested ofpacts it returns the first one. */
376 ofpact_next_flattened(const struct ofpact
*ofpact
)
378 switch (ofpact
->type
) {
381 case OFPACT_CONTROLLER
:
383 case OFPACT_OUTPUT_REG
:
385 case OFPACT_SET_FIELD
:
386 case OFPACT_SET_VLAN_VID
:
387 case OFPACT_SET_VLAN_PCP
:
388 case OFPACT_STRIP_VLAN
:
389 case OFPACT_PUSH_VLAN
:
390 case OFPACT_SET_ETH_SRC
:
391 case OFPACT_SET_ETH_DST
:
392 case OFPACT_SET_IPV4_SRC
:
393 case OFPACT_SET_IPV4_DST
:
394 case OFPACT_SET_IP_DSCP
:
395 case OFPACT_SET_IP_ECN
:
396 case OFPACT_SET_IP_TTL
:
397 case OFPACT_SET_L4_SRC_PORT
:
398 case OFPACT_SET_L4_DST_PORT
:
399 case OFPACT_REG_MOVE
:
400 case OFPACT_STACK_PUSH
:
401 case OFPACT_STACK_POP
:
403 case OFPACT_SET_MPLS_LABEL
:
404 case OFPACT_SET_MPLS_TC
:
405 case OFPACT_SET_MPLS_TTL
:
406 case OFPACT_DEC_MPLS_TTL
:
407 case OFPACT_PUSH_MPLS
:
408 case OFPACT_POP_MPLS
:
409 case OFPACT_SET_TUNNEL
:
410 case OFPACT_SET_QUEUE
:
411 case OFPACT_POP_QUEUE
:
412 case OFPACT_FIN_TIMEOUT
:
413 case OFPACT_RESUBMIT
:
415 case OFPACT_CONJUNCTION
:
416 case OFPACT_MULTIPATH
:
420 case OFPACT_UNROLL_XLATE
:
421 case OFPACT_DEBUG_RECIRC
:
423 case OFPACT_CLEAR_ACTIONS
:
424 case OFPACT_WRITE_METADATA
:
425 case OFPACT_GOTO_TABLE
:
427 return ofpact_next(ofpact
);
430 return ofpact_get_CT(ofpact
)->actions
;
432 case OFPACT_WRITE_ACTIONS
:
433 return ofpact_get_WRITE_ACTIONS(ofpact
)->actions
;
439 /* Pull off existing actions or instructions. Used by nesting actions to keep
440 * ofpacts_parse() oblivious of actions nesting.
442 * Push the actions back on after nested parsing, e.g.:
444 * size_t ofs = ofpacts_pull(ofpacts);
445 * ...nested parsing...
446 * ofpbuf_push_uninit(ofpacts, ofs);
449 ofpacts_pull(struct ofpbuf
*ofpacts
)
454 ofpbuf_pull(ofpacts
, ofs
);
459 #include "ofp-actions.inc1"
461 /* Output actions. */
463 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
464 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
465 * number of bytes to send. A 'max_len' of zero means no bytes of the
466 * packet should be sent. */
467 struct ofp10_action_output
{
468 ovs_be16 type
; /* OFPAT10_OUTPUT. */
469 ovs_be16 len
; /* Length is 8. */
470 ovs_be16 port
; /* Output port. */
471 ovs_be16 max_len
; /* Max length to send to controller. */
473 OFP_ASSERT(sizeof(struct ofp10_action_output
) == 8);
475 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
476 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
477 * number of bytes to send. A 'max_len' of zero means no bytes of the
478 * packet should be sent.*/
479 struct ofp11_action_output
{
480 ovs_be16 type
; /* OFPAT11_OUTPUT. */
481 ovs_be16 len
; /* Length is 16. */
482 ovs_be32 port
; /* Output port. */
483 ovs_be16 max_len
; /* Max length to send to controller. */
484 uint8_t pad
[6]; /* Pad to 64 bits. */
486 OFP_ASSERT(sizeof(struct ofp11_action_output
) == 16);
489 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output
*oao
,
490 enum ofp_version ofp_version OVS_UNUSED
,
493 struct ofpact_output
*output
;
495 output
= ofpact_put_OUTPUT(out
);
496 output
->port
= u16_to_ofp(ntohs(oao
->port
));
497 output
->max_len
= ntohs(oao
->max_len
);
499 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
503 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output
*oao
,
504 enum ofp_version ofp_version OVS_UNUSED
,
507 struct ofpact_output
*output
;
510 output
= ofpact_put_OUTPUT(out
);
511 output
->max_len
= ntohs(oao
->max_len
);
513 error
= ofputil_port_from_ofp11(oao
->port
, &output
->port
);
518 return ofpact_check_output_port(output
->port
, OFPP_MAX
);
522 encode_OUTPUT(const struct ofpact_output
*output
,
523 enum ofp_version ofp_version
, struct ofpbuf
*out
)
525 if (ofp_version
== OFP10_VERSION
) {
526 struct ofp10_action_output
*oao
;
528 oao
= put_OFPAT10_OUTPUT(out
);
529 oao
->port
= htons(ofp_to_u16(output
->port
));
530 oao
->max_len
= htons(output
->max_len
);
532 struct ofp11_action_output
*oao
;
534 oao
= put_OFPAT11_OUTPUT(out
);
535 oao
->port
= ofputil_port_to_ofp11(output
->port
);
536 oao
->max_len
= htons(output
->max_len
);
540 static char * OVS_WARN_UNUSED_RESULT
541 parse_OUTPUT(const char *arg
, struct ofpbuf
*ofpacts
,
542 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
544 if (strchr(arg
, '[')) {
545 struct ofpact_output_reg
*output_reg
;
547 output_reg
= ofpact_put_OUTPUT_REG(ofpacts
);
548 output_reg
->max_len
= UINT16_MAX
;
549 return mf_parse_subfield(&output_reg
->src
, arg
);
551 struct ofpact_output
*output
;
553 output
= ofpact_put_OUTPUT(ofpacts
);
554 if (!ofputil_port_from_string(arg
, &output
->port
)) {
555 return xasprintf("%s: output to unknown port", arg
);
557 output
->max_len
= output
->port
== OFPP_CONTROLLER
? UINT16_MAX
: 0;
563 format_OUTPUT(const struct ofpact_output
*a
, struct ds
*s
)
565 if (ofp_to_u16(a
->port
) < ofp_to_u16(OFPP_MAX
)) {
566 ds_put_format(s
, "%soutput:%s%"PRIu16
,
567 colors
.special
, colors
.end
, a
->port
);
569 ofputil_format_port(a
->port
, s
);
570 if (a
->port
== OFPP_CONTROLLER
) {
571 ds_put_format(s
, ":%"PRIu16
, a
->max_len
);
579 decode_OFPAT_RAW11_GROUP(uint32_t group_id
,
580 enum ofp_version ofp_version OVS_UNUSED
,
583 ofpact_put_GROUP(out
)->group_id
= group_id
;
588 encode_GROUP(const struct ofpact_group
*group
,
589 enum ofp_version ofp_version
, struct ofpbuf
*out
)
591 if (ofp_version
== OFP10_VERSION
) {
594 put_OFPAT11_GROUP(out
, group
->group_id
);
598 static char * OVS_WARN_UNUSED_RESULT
599 parse_GROUP(char *arg
, struct ofpbuf
*ofpacts
,
600 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
602 return str_to_u32(arg
, &ofpact_put_GROUP(ofpacts
)->group_id
);
606 format_GROUP(const struct ofpact_group
*a
, struct ds
*s
)
608 ds_put_format(s
, "%sgroup:%s%"PRIu32
,
609 colors
.special
, colors
.end
, a
->group_id
);
612 /* Action structure for NXAST_CONTROLLER.
614 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
615 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
618 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
620 * - 'controller_id': The ID of the controller connection to which the
621 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
622 * sent only to controllers that have the specified controller connection
623 * ID. See "struct nx_controller_id" for more information. */
624 struct nx_action_controller
{
625 ovs_be16 type
; /* OFPAT_VENDOR. */
626 ovs_be16 len
; /* Length is 16. */
627 ovs_be32 vendor
; /* NX_VENDOR_ID. */
628 ovs_be16 subtype
; /* NXAST_CONTROLLER. */
629 ovs_be16 max_len
; /* Maximum length to send to controller. */
630 ovs_be16 controller_id
; /* Controller ID to send packet-in. */
631 uint8_t reason
; /* enum ofp_packet_in_reason (OFPR_*). */
632 uint8_t zero
; /* Must be zero. */
634 OFP_ASSERT(sizeof(struct nx_action_controller
) == 16);
636 /* Properties for NXAST_CONTROLLER2.
638 * For more information on the effect of NXAC2PT_PAUSE, see the large comment
639 * on NXT_PACKET_IN2 in nicira-ext.h */
640 enum nx_action_controller2_prop_type
{
641 NXAC2PT_MAX_LEN
, /* ovs_be16 max bytes to send (default all). */
642 NXAC2PT_CONTROLLER_ID
, /* ovs_be16 dest controller ID (default 0). */
643 NXAC2PT_REASON
, /* uint8_t reason (OFPR_*), default 0. */
644 NXAC2PT_USERDATA
, /* Data to copy into NXPINT_USERDATA. */
645 NXAC2PT_PAUSE
, /* Flag to pause pipeline to resume later. */
648 /* Action structure for NXAST_CONTROLLER2.
650 * This replacement for NXAST_CONTROLLER makes it extensible via properties. */
651 struct nx_action_controller2
{
652 ovs_be16 type
; /* OFPAT_VENDOR. */
653 ovs_be16 len
; /* Length is 16 or more. */
654 ovs_be32 vendor
; /* NX_VENDOR_ID. */
655 ovs_be16 subtype
; /* NXAST_CONTROLLER2. */
656 uint8_t zeros
[6]; /* Must be zero. */
657 /* Followed by NXAC2PT_* properties. */
659 OFP_ASSERT(sizeof(struct nx_action_controller2
) == 16);
662 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller
*nac
,
663 enum ofp_version ofp_version OVS_UNUSED
,
666 struct ofpact_controller
*oc
;
668 oc
= ofpact_put_CONTROLLER(out
);
669 oc
->ofpact
.raw
= NXAST_RAW_CONTROLLER
;
670 oc
->max_len
= ntohs(nac
->max_len
);
671 oc
->controller_id
= ntohs(nac
->controller_id
);
672 oc
->reason
= nac
->reason
;
673 ofpact_finish_CONTROLLER(out
, &oc
);
679 decode_NXAST_RAW_CONTROLLER2(const struct nx_action_controller2
*nac2
,
680 enum ofp_version ofp_version OVS_UNUSED
,
683 if (!is_all_zeros(nac2
->zeros
, sizeof nac2
->zeros
)) {
684 return OFPERR_NXBRC_MUST_BE_ZERO
;
687 size_t start_ofs
= out
->size
;
688 struct ofpact_controller
*oc
= ofpact_put_CONTROLLER(out
);
689 oc
->ofpact
.raw
= NXAST_RAW_CONTROLLER2
;
690 oc
->max_len
= UINT16_MAX
;
691 oc
->reason
= OFPR_ACTION
;
693 struct ofpbuf properties
;
694 ofpbuf_use_const(&properties
, nac2
, ntohs(nac2
->len
));
695 ofpbuf_pull(&properties
, sizeof *nac2
);
697 while (properties
.size
> 0) {
698 struct ofpbuf payload
;
701 enum ofperr error
= ofpprop_pull(&properties
, &payload
, &type
);
707 case NXAC2PT_MAX_LEN
:
708 error
= ofpprop_parse_u16(&payload
, &oc
->max_len
);
711 case NXAC2PT_CONTROLLER_ID
:
712 error
= ofpprop_parse_u16(&payload
, &oc
->controller_id
);
715 case NXAC2PT_REASON
: {
717 error
= ofpprop_parse_u8(&payload
, &u8
);
722 case NXAC2PT_USERDATA
:
723 out
->size
= start_ofs
+ OFPACT_CONTROLLER_SIZE
;
724 ofpbuf_put(out
, payload
.msg
, ofpbuf_msgsize(&payload
));
725 oc
= ofpbuf_at_assert(out
, start_ofs
, sizeof *oc
);
726 oc
->userdata_len
= ofpbuf_msgsize(&payload
);
734 error
= OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type
);
742 ofpact_finish_CONTROLLER(out
, &oc
);
748 encode_CONTROLLER(const struct ofpact_controller
*controller
,
749 enum ofp_version ofp_version OVS_UNUSED
,
752 if (controller
->userdata_len
754 || controller
->ofpact
.raw
== NXAST_RAW_CONTROLLER2
) {
755 size_t start_ofs
= out
->size
;
756 put_NXAST_CONTROLLER2(out
);
757 if (controller
->max_len
!= UINT16_MAX
) {
758 ofpprop_put_u16(out
, NXAC2PT_MAX_LEN
, controller
->max_len
);
760 if (controller
->controller_id
!= 0) {
761 ofpprop_put_u16(out
, NXAC2PT_CONTROLLER_ID
,
762 controller
->controller_id
);
764 if (controller
->reason
!= OFPR_ACTION
) {
765 ofpprop_put_u8(out
, NXAC2PT_REASON
, controller
->reason
);
767 if (controller
->userdata_len
!= 0) {
768 ofpprop_put(out
, NXAC2PT_USERDATA
, controller
->userdata
,
769 controller
->userdata_len
);
771 if (controller
->pause
) {
772 ofpprop_put_flag(out
, NXAC2PT_PAUSE
);
774 pad_ofpat(out
, start_ofs
);
776 struct nx_action_controller
*nac
;
778 nac
= put_NXAST_CONTROLLER(out
);
779 nac
->max_len
= htons(controller
->max_len
);
780 nac
->controller_id
= htons(controller
->controller_id
);
781 nac
->reason
= controller
->reason
;
785 static char * OVS_WARN_UNUSED_RESULT
786 parse_CONTROLLER(char *arg
, struct ofpbuf
*ofpacts
,
787 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
789 enum ofp_packet_in_reason reason
= OFPR_ACTION
;
790 uint16_t controller_id
= 0;
791 uint16_t max_len
= UINT16_MAX
;
792 const char *userdata
= NULL
;
797 } else if (strspn(arg
, "0123456789") == strlen(arg
)) {
798 char *error
= str_to_u16(arg
, "max_len", &max_len
);
805 while (ofputil_parse_key_value(&arg
, &name
, &value
)) {
806 if (!strcmp(name
, "reason")) {
807 if (!ofputil_packet_in_reason_from_string(value
, &reason
)) {
808 return xasprintf("unknown reason \"%s\"", value
);
810 } else if (!strcmp(name
, "max_len")) {
811 char *error
= str_to_u16(value
, "max_len", &max_len
);
815 } else if (!strcmp(name
, "id")) {
816 char *error
= str_to_u16(value
, "id", &controller_id
);
820 } else if (!strcmp(name
, "userdata")) {
822 } else if (!strcmp(name
, "pause")) {
825 return xasprintf("unknown key \"%s\" parsing controller "
831 if (reason
== OFPR_ACTION
&& controller_id
== 0 && !userdata
&& !pause
) {
832 struct ofpact_output
*output
;
834 output
= ofpact_put_OUTPUT(ofpacts
);
835 output
->port
= OFPP_CONTROLLER
;
836 output
->max_len
= max_len
;
838 struct ofpact_controller
*controller
;
840 controller
= ofpact_put_CONTROLLER(ofpacts
);
841 controller
->max_len
= max_len
;
842 controller
->reason
= reason
;
843 controller
->controller_id
= controller_id
;
844 controller
->pause
= pause
;
847 size_t start_ofs
= ofpacts
->size
;
848 const char *end
= ofpbuf_put_hex(ofpacts
, userdata
, NULL
);
850 return xstrdup("bad hex digit in `controller' "
851 "action `userdata'");
853 size_t userdata_len
= ofpacts
->size
- start_ofs
;
854 controller
= ofpacts
->header
;
855 controller
->userdata_len
= userdata_len
;
857 ofpact_finish_CONTROLLER(ofpacts
, &controller
);
864 format_hex_arg(struct ds
*s
, const uint8_t *data
, size_t len
)
866 for (size_t i
= 0; i
< len
; i
++) {
870 ds_put_format(s
, "%02"PRIx8
, data
[i
]);
875 format_CONTROLLER(const struct ofpact_controller
*a
, struct ds
*s
)
877 if (a
->reason
== OFPR_ACTION
&& !a
->controller_id
&& !a
->userdata_len
879 ds_put_format(s
, "%sCONTROLLER:%s%"PRIu16
,
880 colors
.special
, colors
.end
, a
->max_len
);
882 enum ofp_packet_in_reason reason
= a
->reason
;
884 ds_put_format(s
, "%scontroller(%s", colors
.paren
, colors
.end
);
885 if (reason
!= OFPR_ACTION
) {
886 char reasonbuf
[OFPUTIL_PACKET_IN_REASON_BUFSIZE
];
888 ds_put_format(s
, "%sreason=%s%s,", colors
.param
, colors
.end
,
889 ofputil_packet_in_reason_to_string(
890 reason
, reasonbuf
, sizeof reasonbuf
));
892 if (a
->max_len
!= UINT16_MAX
) {
893 ds_put_format(s
, "%smax_len=%s%"PRIu16
",",
894 colors
.param
, colors
.end
, a
->max_len
);
896 if (a
->controller_id
!= 0) {
897 ds_put_format(s
, "%sid=%s%"PRIu16
",",
898 colors
.param
, colors
.end
, a
->controller_id
);
900 if (a
->userdata_len
) {
901 ds_put_format(s
, "%suserdata=%s", colors
.param
, colors
.end
);
902 format_hex_arg(s
, a
->userdata
, a
->userdata_len
);
906 ds_put_format(s
, "%spause%s,", colors
.value
, colors
.end
);
909 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
913 /* Enqueue action. */
914 struct ofp10_action_enqueue
{
915 ovs_be16 type
; /* OFPAT10_ENQUEUE. */
916 ovs_be16 len
; /* Len is 16. */
917 ovs_be16 port
; /* Port that queue belongs. Should
918 refer to a valid physical port
919 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
920 uint8_t pad
[6]; /* Pad for 64-bit alignment. */
921 ovs_be32 queue_id
; /* Where to enqueue the packets. */
923 OFP_ASSERT(sizeof(struct ofp10_action_enqueue
) == 16);
926 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue
*oae
,
927 enum ofp_version ofp_version OVS_UNUSED
,
930 struct ofpact_enqueue
*enqueue
;
932 enqueue
= ofpact_put_ENQUEUE(out
);
933 enqueue
->port
= u16_to_ofp(ntohs(oae
->port
));
934 enqueue
->queue
= ntohl(oae
->queue_id
);
935 if (ofp_to_u16(enqueue
->port
) >= ofp_to_u16(OFPP_MAX
)
936 && enqueue
->port
!= OFPP_IN_PORT
937 && enqueue
->port
!= OFPP_LOCAL
) {
938 return OFPERR_OFPBAC_BAD_OUT_PORT
;
944 encode_ENQUEUE(const struct ofpact_enqueue
*enqueue
,
945 enum ofp_version ofp_version
, struct ofpbuf
*out
)
947 if (ofp_version
== OFP10_VERSION
) {
948 struct ofp10_action_enqueue
*oae
;
950 oae
= put_OFPAT10_ENQUEUE(out
);
951 oae
->port
= htons(ofp_to_u16(enqueue
->port
));
952 oae
->queue_id
= htonl(enqueue
->queue
);
958 static char * OVS_WARN_UNUSED_RESULT
959 parse_ENQUEUE(char *arg
, struct ofpbuf
*ofpacts
,
960 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
963 char *port
= strtok_r(arg
, ":q,", &sp
);
964 char *queue
= strtok_r(NULL
, "", &sp
);
965 struct ofpact_enqueue
*enqueue
;
967 if (port
== NULL
|| queue
== NULL
) {
968 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
969 "\"enqueue(PORT,QUEUE)\"");
972 enqueue
= ofpact_put_ENQUEUE(ofpacts
);
973 if (!ofputil_port_from_string(port
, &enqueue
->port
)) {
974 return xasprintf("%s: enqueue to unknown port", port
);
976 return str_to_u32(queue
, &enqueue
->queue
);
980 format_ENQUEUE(const struct ofpact_enqueue
*a
, struct ds
*s
)
982 ds_put_format(s
, "%senqueue:%s", colors
.param
, colors
.end
);
983 ofputil_format_port(a
->port
, s
);
984 ds_put_format(s
, ":%"PRIu32
, a
->queue
);
987 /* Action structure for NXAST_OUTPUT_REG.
989 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
991 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
992 * the NXAST_REG_LOAD action.
994 * The acceptable nxm_header values for 'src' are the same as the acceptable
995 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
997 * The 'max_len' field indicates the number of bytes to send when the chosen
998 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
999 * field of OFPAT_OUTPUT.
1001 * The 'zero' field is required to be zeroed for forward compatibility. */
1002 struct nx_action_output_reg
{
1003 ovs_be16 type
; /* OFPAT_VENDOR. */
1004 ovs_be16 len
; /* 24. */
1005 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1006 ovs_be16 subtype
; /* NXAST_OUTPUT_REG. */
1008 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1009 ovs_be32 src
; /* Source. */
1011 ovs_be16 max_len
; /* Max length to send to controller. */
1013 uint8_t zero
[6]; /* Reserved, must be zero. */
1015 OFP_ASSERT(sizeof(struct nx_action_output_reg
) == 24);
1017 /* Action structure for NXAST_OUTPUT_REG2.
1019 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
1020 * experimenter OXM as 'src'.
1022 struct nx_action_output_reg2
{
1023 ovs_be16 type
; /* OFPAT_VENDOR. */
1024 ovs_be16 len
; /* 24. */
1025 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1026 ovs_be16 subtype
; /* NXAST_OUTPUT_REG2. */
1028 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1029 ovs_be16 max_len
; /* Max length to send to controller. */
1032 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1033 * - Enough 0-bytes to pad the action out to 24 bytes. */
1036 OFP_ASSERT(sizeof(struct nx_action_output_reg2
) == 24);
1039 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg
*naor
,
1040 enum ofp_version ofp_version OVS_UNUSED
,
1043 struct ofpact_output_reg
*output_reg
;
1045 if (!is_all_zeros(naor
->zero
, sizeof naor
->zero
)) {
1046 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1049 output_reg
= ofpact_put_OUTPUT_REG(out
);
1050 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG
;
1051 output_reg
->src
.field
= mf_from_nxm_header(ntohl(naor
->src
));
1052 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
1053 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
1054 output_reg
->max_len
= ntohs(naor
->max_len
);
1056 return mf_check_src(&output_reg
->src
, NULL
);
1060 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2
*naor
,
1061 enum ofp_version ofp_version OVS_UNUSED
,
1064 struct ofpact_output_reg
*output_reg
;
1065 output_reg
= ofpact_put_OUTPUT_REG(out
);
1066 output_reg
->ofpact
.raw
= NXAST_RAW_OUTPUT_REG2
;
1067 output_reg
->src
.ofs
= nxm_decode_ofs(naor
->ofs_nbits
);
1068 output_reg
->src
.n_bits
= nxm_decode_n_bits(naor
->ofs_nbits
);
1069 output_reg
->max_len
= ntohs(naor
->max_len
);
1071 struct ofpbuf b
= ofpbuf_const_initializer(naor
, ntohs(naor
->len
));
1072 ofpbuf_pull(&b
, OBJECT_OFFSETOF(naor
, pad
));
1074 enum ofperr error
= nx_pull_header(&b
, &output_reg
->src
.field
, NULL
);
1078 if (!is_all_zeros(b
.data
, b
.size
)) {
1079 return OFPERR_NXBRC_MUST_BE_ZERO
;
1082 return mf_check_src(&output_reg
->src
, NULL
);
1086 encode_OUTPUT_REG(const struct ofpact_output_reg
*output_reg
,
1087 enum ofp_version ofp_version OVS_UNUSED
,
1090 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1091 * cannot be encoded in the older form, encode it as
1092 * NXAST_RAW_OUTPUT_REG2. */
1093 if (output_reg
->ofpact
.raw
== NXAST_RAW_OUTPUT_REG2
1094 || !mf_nxm_header(output_reg
->src
.field
->id
)) {
1095 struct nx_action_output_reg2
*naor
= put_NXAST_OUTPUT_REG2(out
);
1096 size_t size
= out
->size
;
1098 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
1099 output_reg
->src
.n_bits
);
1100 naor
->max_len
= htons(output_reg
->max_len
);
1102 out
->size
= size
- sizeof naor
->pad
;
1103 nx_put_header(out
, output_reg
->src
.field
->id
, 0, false);
1106 struct nx_action_output_reg
*naor
= put_NXAST_OUTPUT_REG(out
);
1108 naor
->ofs_nbits
= nxm_encode_ofs_nbits(output_reg
->src
.ofs
,
1109 output_reg
->src
.n_bits
);
1110 naor
->src
= htonl(mf_nxm_header(output_reg
->src
.field
->id
));
1111 naor
->max_len
= htons(output_reg
->max_len
);
1115 static char * OVS_WARN_UNUSED_RESULT
1116 parse_OUTPUT_REG(const char *arg
, struct ofpbuf
*ofpacts
,
1117 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1119 return parse_OUTPUT(arg
, ofpacts
, usable_protocols
);
1123 format_OUTPUT_REG(const struct ofpact_output_reg
*a
, struct ds
*s
)
1125 ds_put_format(s
, "%soutput:%s", colors
.special
, colors
.end
);
1126 mf_format_subfield(&a
->src
, s
);
1129 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1131 * The bundle actions choose a slave from a supplied list of options.
1132 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
1133 * selection to a register.
1135 * The list of possible slaves follows the nx_action_bundle structure. The size
1136 * of each slave is governed by its type as indicated by the 'slave_type'
1137 * parameter. The list of slaves should be padded at its end with zeros to make
1138 * the total length of the action a multiple of 8.
1140 * Switches infer from the 'slave_type' parameter the size of each slave. All
1141 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
1142 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
1143 * 2 byte width. Switches should reject actions which indicate unknown or
1144 * unsupported slave types.
1146 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1147 * slave. If the switch does not support the specified 'algorithm' parameter,
1148 * it should reject the action.
1150 * Several algorithms take into account liveness when selecting slaves. The
1151 * liveness of a slave is implementation defined (with one exception), but will
1152 * generally take into account things like its carrier status and the results
1153 * of any link monitoring protocols which happen to be running on it. In order
1154 * to give controllers a place-holder value, the OFPP_NONE port is always
1157 * Some slave selection strategies require the use of a hash function, in which
1158 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
1159 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
1160 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
1161 * 'basis' parameter is used as a universal hash parameter. Different values
1162 * of 'basis' yield different hash results.
1164 * The 'zero' parameter at the end of the action structure is reserved for
1165 * future use. Switches are required to reject actions which have nonzero
1166 * bytes in the 'zero' field.
1168 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
1169 * should reject actions which have nonzero bytes in either of these fields.
1171 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
1172 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
1173 * similar to those for the NXAST_REG_LOAD action. */
1174 struct nx_action_bundle
{
1175 ovs_be16 type
; /* OFPAT_VENDOR. */
1176 ovs_be16 len
; /* Length including slaves. */
1177 ovs_be32 vendor
; /* NX_VENDOR_ID. */
1178 ovs_be16 subtype
; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1180 /* Slave choice algorithm to apply to hash value. */
1181 ovs_be16 algorithm
; /* One of NX_BD_ALG_*. */
1183 /* What fields to hash and how. */
1184 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
1185 ovs_be16 basis
; /* Universal hash parameter. */
1187 ovs_be32 slave_type
; /* NXM_OF_IN_PORT. */
1188 ovs_be16 n_slaves
; /* Number of slaves. */
1190 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
1191 ovs_be32 dst
; /* Destination. */
1193 uint8_t zero
[4]; /* Reserved. Must be zero. */
1195 OFP_ASSERT(sizeof(struct nx_action_bundle
) == 32);
1198 decode_bundle(bool load
, const struct nx_action_bundle
*nab
,
1199 struct ofpbuf
*ofpacts
)
1201 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
1202 struct ofpact_bundle
*bundle
;
1203 uint32_t slave_type
;
1204 size_t slaves_size
, i
;
1207 bundle
= ofpact_put_BUNDLE(ofpacts
);
1209 bundle
->n_slaves
= ntohs(nab
->n_slaves
);
1210 bundle
->basis
= ntohs(nab
->basis
);
1211 bundle
->fields
= ntohs(nab
->fields
);
1212 bundle
->algorithm
= ntohs(nab
->algorithm
);
1213 slave_type
= ntohl(nab
->slave_type
);
1214 slaves_size
= ntohs(nab
->len
) - sizeof *nab
;
1216 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1217 if (!flow_hash_fields_valid(bundle
->fields
)) {
1218 VLOG_WARN_RL(&rl
, "unsupported fields %d", (int) bundle
->fields
);
1219 } else if (bundle
->n_slaves
> BUNDLE_MAX_SLAVES
) {
1220 VLOG_WARN_RL(&rl
, "too many slaves");
1221 } else if (bundle
->algorithm
!= NX_BD_ALG_HRW
1222 && bundle
->algorithm
!= NX_BD_ALG_ACTIVE_BACKUP
) {
1223 VLOG_WARN_RL(&rl
, "unsupported algorithm %d", (int) bundle
->algorithm
);
1224 } else if (slave_type
!= mf_nxm_header(MFF_IN_PORT
)) {
1225 VLOG_WARN_RL(&rl
, "unsupported slave type %"PRIu16
, slave_type
);
1230 if (!is_all_zeros(nab
->zero
, sizeof nab
->zero
)) {
1231 VLOG_WARN_RL(&rl
, "reserved field is nonzero");
1232 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1236 bundle
->dst
.field
= mf_from_nxm_header(ntohl(nab
->dst
));
1237 bundle
->dst
.ofs
= nxm_decode_ofs(nab
->ofs_nbits
);
1238 bundle
->dst
.n_bits
= nxm_decode_n_bits(nab
->ofs_nbits
);
1240 if (bundle
->dst
.n_bits
< 16) {
1241 VLOG_WARN_RL(&rl
, "bundle_load action requires at least 16 bit "
1243 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1246 if (nab
->ofs_nbits
|| nab
->dst
) {
1247 VLOG_WARN_RL(&rl
, "bundle action has nonzero reserved fields");
1248 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
1252 if (slaves_size
< bundle
->n_slaves
* sizeof(ovs_be16
)) {
1253 VLOG_WARN_RL(&rl
, "Nicira action %s only has %"PRIuSIZE
" bytes "
1254 "allocated for slaves. %"PRIuSIZE
" bytes are required "
1255 "for %"PRIu16
" slaves.",
1256 load
? "bundle_load" : "bundle", slaves_size
,
1257 bundle
->n_slaves
* sizeof(ovs_be16
), bundle
->n_slaves
);
1258 error
= OFPERR_OFPBAC_BAD_LEN
;
1261 for (i
= 0; i
< bundle
->n_slaves
; i
++) {
1262 ofp_port_t ofp_port
= u16_to_ofp(ntohs(((ovs_be16
*)(nab
+ 1))[i
]));
1263 ofpbuf_put(ofpacts
, &ofp_port
, sizeof ofp_port
);
1264 bundle
= ofpacts
->header
;
1267 ofpact_finish_BUNDLE(ofpacts
, &bundle
);
1269 error
= bundle_check(bundle
, OFPP_MAX
, NULL
);
1275 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle
*nab
,
1276 enum ofp_version ofp_version OVS_UNUSED
,
1279 return decode_bundle(false, nab
, out
);
1283 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle
*nab
,
1284 enum ofp_version ofp_version OVS_UNUSED
,
1287 return decode_bundle(true, nab
, out
);
1291 encode_BUNDLE(const struct ofpact_bundle
*bundle
,
1292 enum ofp_version ofp_version OVS_UNUSED
,
1295 int slaves_len
= ROUND_UP(2 * bundle
->n_slaves
, OFP_ACTION_ALIGN
);
1296 struct nx_action_bundle
*nab
;
1300 nab
= (bundle
->dst
.field
1301 ? put_NXAST_BUNDLE_LOAD(out
)
1302 : put_NXAST_BUNDLE(out
));
1303 nab
->len
= htons(ntohs(nab
->len
) + slaves_len
);
1304 nab
->algorithm
= htons(bundle
->algorithm
);
1305 nab
->fields
= htons(bundle
->fields
);
1306 nab
->basis
= htons(bundle
->basis
);
1307 nab
->slave_type
= htonl(mf_nxm_header(MFF_IN_PORT
));
1308 nab
->n_slaves
= htons(bundle
->n_slaves
);
1309 if (bundle
->dst
.field
) {
1310 nab
->ofs_nbits
= nxm_encode_ofs_nbits(bundle
->dst
.ofs
,
1311 bundle
->dst
.n_bits
);
1312 nab
->dst
= htonl(mf_nxm_header(bundle
->dst
.field
->id
));
1315 slaves
= ofpbuf_put_zeros(out
, slaves_len
);
1316 for (i
= 0; i
< bundle
->n_slaves
; i
++) {
1317 slaves
[i
] = htons(ofp_to_u16(bundle
->slaves
[i
]));
1321 static char * OVS_WARN_UNUSED_RESULT
1322 parse_BUNDLE(const char *arg
, struct ofpbuf
*ofpacts
,
1323 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1325 return bundle_parse(arg
, ofpacts
);
1328 static char * OVS_WARN_UNUSED_RESULT
1329 parse_bundle_load(const char *arg
, struct ofpbuf
*ofpacts
)
1331 return bundle_parse_load(arg
, ofpacts
);
1335 format_BUNDLE(const struct ofpact_bundle
*a
, struct ds
*s
)
1337 bundle_format(a
, s
);
1340 /* Set VLAN actions. */
1343 decode_set_vlan_vid(uint16_t vid
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1346 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1348 struct ofpact_vlan_vid
*vlan_vid
= ofpact_put_SET_VLAN_VID(out
);
1349 vlan_vid
->vlan_vid
= vid
;
1350 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1356 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid
,
1357 enum ofp_version ofp_version OVS_UNUSED
,
1360 return decode_set_vlan_vid(vid
, true, out
);
1364 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid
,
1365 enum ofp_version ofp_version OVS_UNUSED
,
1368 return decode_set_vlan_vid(vid
, false, out
);
1372 encode_SET_VLAN_VID(const struct ofpact_vlan_vid
*vlan_vid
,
1373 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1375 uint16_t vid
= vlan_vid
->vlan_vid
;
1377 /* Push a VLAN tag, if none is present and this form of the action calls
1378 * for such a feature. */
1379 if (ofp_version
> OFP10_VERSION
1380 && vlan_vid
->push_vlan_if_needed
1381 && !vlan_vid
->flow_has_vlan
) {
1382 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1385 if (ofp_version
== OFP10_VERSION
) {
1386 put_OFPAT10_SET_VLAN_VID(out
, vid
);
1387 } else if (ofp_version
== OFP11_VERSION
) {
1388 put_OFPAT11_SET_VLAN_VID(out
, vid
);
1390 ofpact_put_set_field(out
, ofp_version
,
1391 MFF_VLAN_VID
, vid
| OFPVID12_PRESENT
);
1395 static char * OVS_WARN_UNUSED_RESULT
1396 parse_set_vlan_vid(char *arg
, struct ofpbuf
*ofpacts
, bool push_vlan_if_needed
)
1398 struct ofpact_vlan_vid
*vlan_vid
;
1402 error
= str_to_u16(arg
, "VLAN VID", &vid
);
1407 if (vid
& ~VLAN_VID_MASK
) {
1408 return xasprintf("%s: not a valid VLAN VID", arg
);
1410 vlan_vid
= ofpact_put_SET_VLAN_VID(ofpacts
);
1411 vlan_vid
->vlan_vid
= vid
;
1412 vlan_vid
->push_vlan_if_needed
= push_vlan_if_needed
;
1416 static char * OVS_WARN_UNUSED_RESULT
1417 parse_SET_VLAN_VID(char *arg
, struct ofpbuf
*ofpacts
,
1418 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1420 return parse_set_vlan_vid(arg
, ofpacts
, false);
1424 format_SET_VLAN_VID(const struct ofpact_vlan_vid
*a
, struct ds
*s
)
1426 ds_put_format(s
, "%s%s:%s%"PRIu16
, colors
.param
,
1427 a
->push_vlan_if_needed
? "mod_vlan_vid" : "set_vlan_vid",
1428 colors
.end
, a
->vlan_vid
);
1431 /* Set PCP actions. */
1434 decode_set_vlan_pcp(uint8_t pcp
, bool push_vlan_if_needed
, struct ofpbuf
*out
)
1437 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1439 struct ofpact_vlan_pcp
*vlan_pcp
= ofpact_put_SET_VLAN_PCP(out
);
1440 vlan_pcp
->vlan_pcp
= pcp
;
1441 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1447 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp
,
1448 enum ofp_version ofp_version OVS_UNUSED
,
1451 return decode_set_vlan_pcp(pcp
, true, out
);
1455 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp
,
1456 enum ofp_version ofp_version OVS_UNUSED
,
1459 return decode_set_vlan_pcp(pcp
, false, out
);
1463 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*vlan_pcp
,
1464 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1466 uint8_t pcp
= vlan_pcp
->vlan_pcp
;
1468 /* Push a VLAN tag, if none is present and this form of the action calls
1469 * for such a feature. */
1470 if (ofp_version
> OFP10_VERSION
1471 && vlan_pcp
->push_vlan_if_needed
1472 && !vlan_pcp
->flow_has_vlan
) {
1473 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1476 if (ofp_version
== OFP10_VERSION
) {
1477 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
1478 } else if (ofp_version
== OFP11_VERSION
) {
1479 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
1481 ofpact_put_set_field(out
, ofp_version
, MFF_VLAN_PCP
, pcp
);
1485 static char * OVS_WARN_UNUSED_RESULT
1486 parse_set_vlan_pcp(char *arg
, struct ofpbuf
*ofpacts
, bool push_vlan_if_needed
)
1488 struct ofpact_vlan_pcp
*vlan_pcp
;
1492 error
= str_to_u8(arg
, "VLAN PCP", &pcp
);
1498 return xasprintf("%s: not a valid VLAN PCP", arg
);
1500 vlan_pcp
= ofpact_put_SET_VLAN_PCP(ofpacts
);
1501 vlan_pcp
->vlan_pcp
= pcp
;
1502 vlan_pcp
->push_vlan_if_needed
= push_vlan_if_needed
;
1506 static char * OVS_WARN_UNUSED_RESULT
1507 parse_SET_VLAN_PCP(char *arg
, struct ofpbuf
*ofpacts
,
1508 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1510 return parse_set_vlan_pcp(arg
, ofpacts
, false);
1514 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp
*a
, struct ds
*s
)
1516 ds_put_format(s
, "%s%s:%s%"PRIu8
, colors
.param
,
1517 a
->push_vlan_if_needed
? "mod_vlan_pcp" : "set_vlan_pcp",
1518 colors
.end
, a
->vlan_pcp
);
1521 /* Strip VLAN actions. */
1524 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf
*out
)
1526 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1531 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf
*out
)
1533 ofpact_put_STRIP_VLAN(out
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1538 encode_STRIP_VLAN(const struct ofpact_null
*null OVS_UNUSED
,
1539 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1541 if (ofp_version
== OFP10_VERSION
) {
1542 put_OFPAT10_STRIP_VLAN(out
);
1544 put_OFPAT11_POP_VLAN(out
);
1548 static char * OVS_WARN_UNUSED_RESULT
1549 parse_STRIP_VLAN(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
1550 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1552 ofpact_put_STRIP_VLAN(ofpacts
)->ofpact
.raw
= OFPAT_RAW10_STRIP_VLAN
;
1556 static char * OVS_WARN_UNUSED_RESULT
1557 parse_pop_vlan(struct ofpbuf
*ofpacts
)
1559 ofpact_put_STRIP_VLAN(ofpacts
)->ofpact
.raw
= OFPAT_RAW11_POP_VLAN
;
1564 format_STRIP_VLAN(const struct ofpact_null
*a
, struct ds
*s
)
1566 ds_put_format(s
, (a
->ofpact
.raw
== OFPAT_RAW11_POP_VLAN
1568 : "%sstrip_vlan%s"),
1569 colors
.value
, colors
.end
);
1572 /* Push VLAN action. */
1575 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type
,
1576 enum ofp_version ofp_version OVS_UNUSED
,
1579 if (eth_type
!= htons(ETH_TYPE_VLAN_8021Q
)) {
1580 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1581 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1583 ofpact_put_PUSH_VLAN(out
);
1588 encode_PUSH_VLAN(const struct ofpact_null
*null OVS_UNUSED
,
1589 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1591 if (ofp_version
== OFP10_VERSION
) {
1592 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1593 * follow this action. */
1595 /* XXX ETH_TYPE_VLAN_8021AD case */
1596 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
1600 static char * OVS_WARN_UNUSED_RESULT
1601 parse_PUSH_VLAN(char *arg
, struct ofpbuf
*ofpacts
,
1602 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1607 *usable_protocols
&= OFPUTIL_P_OF11_UP
;
1608 error
= str_to_u16(arg
, "ethertype", ðertype
);
1613 if (ethertype
!= ETH_TYPE_VLAN_8021Q
) {
1614 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1615 return xasprintf("%s: not a valid VLAN ethertype", arg
);
1618 ofpact_put_PUSH_VLAN(ofpacts
);
1623 format_PUSH_VLAN(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
1625 /* XXX 802.1AD case*/
1626 ds_put_format(s
, "%spush_vlan:%s%#"PRIx16
,
1627 colors
.param
, colors
.end
, ETH_TYPE_VLAN_8021Q
);
1630 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1631 struct ofp_action_dl_addr
{
1632 ovs_be16 type
; /* Type. */
1633 ovs_be16 len
; /* Length is 16. */
1634 struct eth_addr dl_addr
; /* Ethernet address. */
1637 OFP_ASSERT(sizeof(struct ofp_action_dl_addr
) == 16);
1640 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr
*a
,
1641 enum ofp_version ofp_version OVS_UNUSED
,
1644 ofpact_put_SET_ETH_SRC(out
)->mac
= a
->dl_addr
;
1649 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr
*a
,
1650 enum ofp_version ofp_version OVS_UNUSED
,
1653 ofpact_put_SET_ETH_DST(out
)->mac
= a
->dl_addr
;
1658 encode_SET_ETH_addr(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1659 enum ofp_raw_action_type raw
, enum mf_field_id field
,
1662 if (ofp_version
< OFP12_VERSION
) {
1663 struct ofp_action_dl_addr
*oada
;
1665 oada
= ofpact_put_raw(out
, ofp_version
, raw
, 0);
1666 oada
->dl_addr
= mac
->mac
;
1668 ofpact_put_set_field(out
, ofp_version
, field
,
1669 eth_addr_to_uint64(mac
->mac
));
1674 encode_SET_ETH_SRC(const struct ofpact_mac
*mac
, enum ofp_version ofp_version
,
1677 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_SRC
, MFF_ETH_SRC
,
1683 encode_SET_ETH_DST(const struct ofpact_mac
*mac
,
1684 enum ofp_version ofp_version
,
1687 encode_SET_ETH_addr(mac
, ofp_version
, OFPAT_RAW_SET_DL_DST
, MFF_ETH_DST
,
1692 static char * OVS_WARN_UNUSED_RESULT
1693 parse_SET_ETH_SRC(char *arg
, struct ofpbuf
*ofpacts
,
1694 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1696 return str_to_mac(arg
, &ofpact_put_SET_ETH_SRC(ofpacts
)->mac
);
1699 static char * OVS_WARN_UNUSED_RESULT
1700 parse_SET_ETH_DST(char *arg
, struct ofpbuf
*ofpacts
,
1701 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1703 return str_to_mac(arg
, &ofpact_put_SET_ETH_DST(ofpacts
)->mac
);
1707 format_SET_ETH_SRC(const struct ofpact_mac
*a
, struct ds
*s
)
1709 ds_put_format(s
, "%smod_dl_src:%s"ETH_ADDR_FMT
,
1710 colors
.param
, colors
.end
, ETH_ADDR_ARGS(a
->mac
));
1714 format_SET_ETH_DST(const struct ofpact_mac
*a
, struct ds
*s
)
1716 ds_put_format(s
, "%smod_dl_dst:%s"ETH_ADDR_FMT
,
1717 colors
.param
, colors
.end
, ETH_ADDR_ARGS(a
->mac
));
1720 /* Set IPv4 address actions. */
1723 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4
,
1724 enum ofp_version ofp_version OVS_UNUSED
,
1727 ofpact_put_SET_IPV4_SRC(out
)->ipv4
= ipv4
;
1732 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4
,
1733 enum ofp_version ofp_version OVS_UNUSED
,
1736 ofpact_put_SET_IPV4_DST(out
)->ipv4
= ipv4
;
1741 encode_SET_IPV4_addr(const struct ofpact_ipv4
*ipv4
,
1742 enum ofp_version ofp_version
,
1743 enum ofp_raw_action_type raw
, enum mf_field_id field
,
1746 ovs_be32 addr
= ipv4
->ipv4
;
1747 if (ofp_version
< OFP12_VERSION
) {
1748 ofpact_put_raw(out
, ofp_version
, raw
, ntohl(addr
));
1750 ofpact_put_set_field(out
, ofp_version
, field
, ntohl(addr
));
1755 encode_SET_IPV4_SRC(const struct ofpact_ipv4
*ipv4
,
1756 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1758 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_SRC
, MFF_IPV4_SRC
,
1763 encode_SET_IPV4_DST(const struct ofpact_ipv4
*ipv4
,
1764 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1766 encode_SET_IPV4_addr(ipv4
, ofp_version
, OFPAT_RAW_SET_NW_DST
, MFF_IPV4_DST
,
1770 static char * OVS_WARN_UNUSED_RESULT
1771 parse_SET_IPV4_SRC(char *arg
, struct ofpbuf
*ofpacts
,
1772 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1774 return str_to_ip(arg
, &ofpact_put_SET_IPV4_SRC(ofpacts
)->ipv4
);
1777 static char * OVS_WARN_UNUSED_RESULT
1778 parse_SET_IPV4_DST(char *arg
, struct ofpbuf
*ofpacts
,
1779 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1781 return str_to_ip(arg
, &ofpact_put_SET_IPV4_DST(ofpacts
)->ipv4
);
1785 format_SET_IPV4_SRC(const struct ofpact_ipv4
*a
, struct ds
*s
)
1787 ds_put_format(s
, "%smod_nw_src:%s"IP_FMT
,
1788 colors
.param
, colors
.end
, IP_ARGS(a
->ipv4
));
1792 format_SET_IPV4_DST(const struct ofpact_ipv4
*a
, struct ds
*s
)
1794 ds_put_format(s
, "%smod_nw_dst:%s"IP_FMT
,
1795 colors
.param
, colors
.end
, IP_ARGS(a
->ipv4
));
1798 /* Set IPv4/v6 TOS actions. */
1801 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp
,
1802 enum ofp_version ofp_version OVS_UNUSED
,
1805 if (dscp
& ~IP_DSCP_MASK
) {
1806 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1808 ofpact_put_SET_IP_DSCP(out
)->dscp
= dscp
;
1814 encode_SET_IP_DSCP(const struct ofpact_dscp
*dscp
,
1815 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1817 if (ofp_version
< OFP12_VERSION
) {
1818 put_OFPAT_SET_NW_TOS(out
, ofp_version
, dscp
->dscp
);
1820 ofpact_put_set_field(out
, ofp_version
,
1821 MFF_IP_DSCP_SHIFTED
, dscp
->dscp
>> 2);
1825 static char * OVS_WARN_UNUSED_RESULT
1826 parse_SET_IP_DSCP(char *arg
, struct ofpbuf
*ofpacts
,
1827 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1832 error
= str_to_u8(arg
, "TOS", &tos
);
1837 if (tos
& ~IP_DSCP_MASK
) {
1838 return xasprintf("%s: not a valid TOS", arg
);
1840 ofpact_put_SET_IP_DSCP(ofpacts
)->dscp
= tos
;
1845 format_SET_IP_DSCP(const struct ofpact_dscp
*a
, struct ds
*s
)
1847 ds_put_format(s
, "%smod_nw_tos:%s%d", colors
.param
, colors
.end
, a
->dscp
);
1850 /* Set IPv4/v6 ECN actions. */
1853 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn
,
1854 enum ofp_version ofp_version OVS_UNUSED
,
1857 if (ecn
& ~IP_ECN_MASK
) {
1858 return OFPERR_OFPBAC_BAD_ARGUMENT
;
1860 ofpact_put_SET_IP_ECN(out
)->ecn
= ecn
;
1866 encode_SET_IP_ECN(const struct ofpact_ecn
*ip_ecn
,
1867 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1869 uint8_t ecn
= ip_ecn
->ecn
;
1870 if (ofp_version
== OFP10_VERSION
) {
1872 } else if (ofp_version
== OFP11_VERSION
) {
1873 put_OFPAT11_SET_NW_ECN(out
, ecn
);
1875 ofpact_put_set_field(out
, ofp_version
, MFF_IP_ECN
, ecn
);
1879 static char * OVS_WARN_UNUSED_RESULT
1880 parse_SET_IP_ECN(char *arg
, struct ofpbuf
*ofpacts
,
1881 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1886 error
= str_to_u8(arg
, "ECN", &ecn
);
1891 if (ecn
& ~IP_ECN_MASK
) {
1892 return xasprintf("%s: not a valid ECN", arg
);
1894 ofpact_put_SET_IP_ECN(ofpacts
)->ecn
= ecn
;
1899 format_SET_IP_ECN(const struct ofpact_ecn
*a
, struct ds
*s
)
1901 ds_put_format(s
, "%smod_nw_ecn:%s%d",
1902 colors
.param
, colors
.end
, a
->ecn
);
1905 /* Set IPv4/v6 TTL actions. */
1908 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl
,
1909 enum ofp_version ofp_version OVS_UNUSED
,
1912 ofpact_put_SET_IP_TTL(out
)->ttl
= ttl
;
1917 encode_SET_IP_TTL(const struct ofpact_ip_ttl
*ttl
,
1918 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1920 if (ofp_version
>= OFP11_VERSION
) {
1921 put_OFPAT11_SET_NW_TTL(out
, ttl
->ttl
);
1927 static char * OVS_WARN_UNUSED_RESULT
1928 parse_SET_IP_TTL(char *arg
, struct ofpbuf
*ofpacts
,
1929 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
1934 error
= str_to_u8(arg
, "TTL", &ttl
);
1939 ofpact_put_SET_IP_TTL(ofpacts
)->ttl
= ttl
;
1944 format_SET_IP_TTL(const struct ofpact_ip_ttl
*a
, struct ds
*s
)
1946 ds_put_format(s
, "%smod_nw_ttl:%s%d", colors
.param
, colors
.end
, a
->ttl
);
1949 /* Set TCP/UDP/SCTP port actions. */
1952 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port
,
1953 enum ofp_version ofp_version OVS_UNUSED
,
1956 ofpact_put_SET_L4_SRC_PORT(out
)->port
= ntohs(port
);
1961 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port
,
1962 enum ofp_version ofp_version OVS_UNUSED
,
1965 ofpact_put_SET_L4_DST_PORT(out
)->port
= ntohs(port
);
1970 encode_SET_L4_port(const struct ofpact_l4_port
*l4_port
,
1971 enum ofp_version ofp_version
, enum ofp_raw_action_type raw
,
1972 enum mf_field_id field
, struct ofpbuf
*out
)
1974 uint16_t port
= l4_port
->port
;
1976 if (ofp_version
>= OFP12_VERSION
&& field
!= MFF_N_IDS
) {
1977 ofpact_put_set_field(out
, ofp_version
, field
, port
);
1979 ofpact_put_raw(out
, ofp_version
, raw
, port
);
1984 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port
*l4_port
,
1985 enum ofp_version ofp_version
, struct ofpbuf
*out
)
1987 uint8_t proto
= l4_port
->flow_ip_proto
;
1988 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_SRC
1989 : proto
== IPPROTO_UDP
? MFF_UDP_SRC
1990 : proto
== IPPROTO_SCTP
? MFF_SCTP_SRC
1993 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_SRC
, field
, out
);
1997 encode_SET_L4_DST_PORT(const struct ofpact_l4_port
*l4_port
,
1998 enum ofp_version ofp_version
,
2001 uint8_t proto
= l4_port
->flow_ip_proto
;
2002 enum mf_field_id field
= (proto
== IPPROTO_TCP
? MFF_TCP_DST
2003 : proto
== IPPROTO_UDP
? MFF_UDP_DST
2004 : proto
== IPPROTO_SCTP
? MFF_SCTP_DST
2007 encode_SET_L4_port(l4_port
, ofp_version
, OFPAT_RAW_SET_TP_DST
, field
, out
);
2010 static char * OVS_WARN_UNUSED_RESULT
2011 parse_SET_L4_SRC_PORT(char *arg
, struct ofpbuf
*ofpacts
,
2012 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2014 return str_to_u16(arg
, "source port",
2015 &ofpact_put_SET_L4_SRC_PORT(ofpacts
)->port
);
2018 static char * OVS_WARN_UNUSED_RESULT
2019 parse_SET_L4_DST_PORT(char *arg
, struct ofpbuf
*ofpacts
,
2020 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2022 return str_to_u16(arg
, "destination port",
2023 &ofpact_put_SET_L4_DST_PORT(ofpacts
)->port
);
2027 format_SET_L4_SRC_PORT(const struct ofpact_l4_port
*a
, struct ds
*s
)
2029 ds_put_format(s
, "%smod_tp_src:%s%d", colors
.param
, colors
.end
, a
->port
);
2033 format_SET_L4_DST_PORT(const struct ofpact_l4_port
*a
, struct ds
*s
)
2035 ds_put_format(s
, "%smod_tp_dst:%s%d", colors
.param
, colors
.end
, a
->port
);
2038 /* Action structure for OFPAT_COPY_FIELD. */
2039 struct ofp15_action_copy_field
{
2040 ovs_be16 type
; /* OFPAT_COPY_FIELD. */
2041 ovs_be16 len
; /* Length is padded to 64 bits. */
2042 ovs_be16 n_bits
; /* Number of bits to copy. */
2043 ovs_be16 src_offset
; /* Starting bit offset in source. */
2044 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
2047 * - OXM header for source field.
2048 * - OXM header for destination field.
2049 * - Padding with 0-bytes to a multiple of 8 bytes.
2050 * The "pad2" member is the beginning of the above. */
2053 OFP_ASSERT(sizeof(struct ofp15_action_copy_field
) == 16);
2055 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
2056 struct onf_action_copy_field
{
2057 ovs_be16 type
; /* OFPAT_EXPERIMENTER. */
2058 ovs_be16 len
; /* Length is padded to 64 bits. */
2059 ovs_be32 experimenter
; /* ONF_VENDOR_ID. */
2060 ovs_be16 exp_type
; /* 3200. */
2061 uint8_t pad
[2]; /* Not used. */
2062 ovs_be16 n_bits
; /* Number of bits to copy. */
2063 ovs_be16 src_offset
; /* Starting bit offset in source. */
2064 ovs_be16 dst_offset
; /* Starting bit offset in destination. */
2065 uint8_t pad2
[2]; /* Not used. */
2067 * - OXM header for source field.
2068 * - OXM header for destination field.
2069 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2070 * The "pad3" member is the beginning of the above. */
2071 uint8_t pad3
[4]; /* Not used. */
2073 OFP_ASSERT(sizeof(struct onf_action_copy_field
) == 24);
2075 /* Action structure for NXAST_REG_MOVE.
2077 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2078 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2079 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
2080 * the next most significant bit, and so on.
2082 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
2083 * sense to use nxm_hasmask=1 because the action does not do any kind of
2084 * matching; it uses the actual value of a field.)
2086 * The following nxm_header values are potentially acceptable as 'src':
2101 * - NXM_OF_ICMP_TYPE
2102 * - NXM_OF_ICMP_CODE
2109 * - NXM_NX_ICMPV6_TYPE
2110 * - NXM_NX_ICMPV6_CODE
2113 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2115 * - NXM_NX_TUN_IPV4_SRC
2116 * - NXM_NX_TUN_IPV4_DST
2118 * The following nxm_header values are potentially acceptable as 'dst':
2129 * - NXM_OF_ICMP_TYPE
2130 * - NXM_OF_ICMP_CODE
2131 * - NXM_NX_ICMPV6_TYPE
2132 * - NXM_NX_ICMPV6_CODE
2138 * Modifying any of the above fields changes the corresponding packet
2143 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2147 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
2148 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
2149 * header (if any), ignoring the other bits. Setting a value with CFI=1
2150 * adds or modifies the 802.1Q header appropriately, setting the TCI field
2151 * to the field's new value (with the CFI bit masked out).
2153 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
2154 * any of these values modifies the corresponding tunnel header field used
2155 * for the packet's next tunnel encapsulation, if allowed by the
2156 * configuration of the output tunnel port.
2158 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2159 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
2160 * used only if the flow's nx_match includes an nxm_entry that specifies
2161 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2163 * The switch will reject actions for which src_ofs+n_bits is greater than the
2164 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2165 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2167 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2168 * behaves as if 'src' were copied out to a temporary buffer, then the
2169 * temporary buffer copied to 'dst'.
2171 struct nx_action_reg_move
{
2172 ovs_be16 type
; /* OFPAT_VENDOR. */
2173 ovs_be16 len
; /* Length is 24. */
2174 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2175 ovs_be16 subtype
; /* NXAST_REG_MOVE. */
2176 ovs_be16 n_bits
; /* Number of bits. */
2177 ovs_be16 src_ofs
; /* Starting bit offset in source. */
2178 ovs_be16 dst_ofs
; /* Starting bit offset in destination. */
2180 * - OXM/NXM header for source field (4 or 8 bytes).
2181 * - OXM/NXM header for destination field (4 or 8 bytes).
2182 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2184 OFP_ASSERT(sizeof(struct nx_action_reg_move
) == 16);
2187 decode_copy_field__(ovs_be16 src_offset
, ovs_be16 dst_offset
, ovs_be16 n_bits
,
2188 const void *action
, ovs_be16 action_len
, size_t oxm_offset
,
2189 struct ofpbuf
*ofpacts
)
2191 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2192 move
->ofpact
.raw
= ONFACT_RAW13_COPY_FIELD
;
2193 move
->src
.ofs
= ntohs(src_offset
);
2194 move
->src
.n_bits
= ntohs(n_bits
);
2195 move
->dst
.ofs
= ntohs(dst_offset
);
2196 move
->dst
.n_bits
= ntohs(n_bits
);
2198 struct ofpbuf b
= ofpbuf_const_initializer(action
, ntohs(action_len
));
2199 ofpbuf_pull(&b
, oxm_offset
);
2201 enum ofperr error
= nx_pull_header(&b
, &move
->src
.field
, NULL
);
2205 error
= nx_pull_header(&b
, &move
->dst
.field
, NULL
);
2210 if (!is_all_zeros(b
.data
, b
.size
)) {
2211 return OFPERR_NXBRC_MUST_BE_ZERO
;
2214 return nxm_reg_move_check(move
, NULL
);
2218 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field
*oacf
,
2219 enum ofp_version ofp_version OVS_UNUSED
,
2220 struct ofpbuf
*ofpacts
)
2222 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
2223 oacf
->n_bits
, oacf
, oacf
->len
,
2224 OBJECT_OFFSETOF(oacf
, pad2
), ofpacts
);
2228 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field
*oacf
,
2229 enum ofp_version ofp_version OVS_UNUSED
,
2230 struct ofpbuf
*ofpacts
)
2232 return decode_copy_field__(oacf
->src_offset
, oacf
->dst_offset
,
2233 oacf
->n_bits
, oacf
, oacf
->len
,
2234 OBJECT_OFFSETOF(oacf
, pad3
), ofpacts
);
2238 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move
*narm
,
2239 enum ofp_version ofp_version OVS_UNUSED
,
2240 struct ofpbuf
*ofpacts
)
2242 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2243 move
->ofpact
.raw
= NXAST_RAW_REG_MOVE
;
2244 move
->src
.ofs
= ntohs(narm
->src_ofs
);
2245 move
->src
.n_bits
= ntohs(narm
->n_bits
);
2246 move
->dst
.ofs
= ntohs(narm
->dst_ofs
);
2247 move
->dst
.n_bits
= ntohs(narm
->n_bits
);
2249 struct ofpbuf b
= ofpbuf_const_initializer(narm
, ntohs(narm
->len
));
2250 ofpbuf_pull(&b
, sizeof *narm
);
2252 enum ofperr error
= nx_pull_header(&b
, &move
->src
.field
, NULL
);
2256 error
= nx_pull_header(&b
, &move
->dst
.field
, NULL
);
2260 if (!is_all_zeros(b
.data
, b
.size
)) {
2261 return OFPERR_NXBRC_MUST_BE_ZERO
;
2264 return nxm_reg_move_check(move
, NULL
);
2268 encode_REG_MOVE(const struct ofpact_reg_move
*move
,
2269 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2271 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2272 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2273 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2274 * probe for support. Until we have that ability, we currently prefer
2275 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2277 size_t start_ofs
= out
->size
;
2278 if (ofp_version
>= OFP15_VERSION
) {
2279 struct ofp15_action_copy_field
*copy
= put_OFPAT15_COPY_FIELD(out
);
2280 copy
->n_bits
= htons(move
->dst
.n_bits
);
2281 copy
->src_offset
= htons(move
->src
.ofs
);
2282 copy
->dst_offset
= htons(move
->dst
.ofs
);
2283 out
->size
= out
->size
- sizeof copy
->pad2
;
2284 nx_put_header(out
, move
->src
.field
->id
, ofp_version
, false);
2285 nx_put_header(out
, move
->dst
.field
->id
, ofp_version
, false);
2286 } else if (ofp_version
== OFP13_VERSION
2287 && move
->ofpact
.raw
== ONFACT_RAW13_COPY_FIELD
) {
2288 struct onf_action_copy_field
*copy
= put_ONFACT13_COPY_FIELD(out
);
2289 copy
->n_bits
= htons(move
->dst
.n_bits
);
2290 copy
->src_offset
= htons(move
->src
.ofs
);
2291 copy
->dst_offset
= htons(move
->dst
.ofs
);
2292 out
->size
= out
->size
- sizeof copy
->pad3
;
2293 nx_put_header(out
, move
->src
.field
->id
, ofp_version
, false);
2294 nx_put_header(out
, move
->dst
.field
->id
, ofp_version
, false);
2296 struct nx_action_reg_move
*narm
= put_NXAST_REG_MOVE(out
);
2297 narm
->n_bits
= htons(move
->dst
.n_bits
);
2298 narm
->src_ofs
= htons(move
->src
.ofs
);
2299 narm
->dst_ofs
= htons(move
->dst
.ofs
);
2300 nx_put_header(out
, move
->src
.field
->id
, 0, false);
2301 nx_put_header(out
, move
->dst
.field
->id
, 0, false);
2303 pad_ofpat(out
, start_ofs
);
2306 static char * OVS_WARN_UNUSED_RESULT
2307 parse_REG_MOVE(const char *arg
, struct ofpbuf
*ofpacts
,
2308 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
2310 struct ofpact_reg_move
*move
= ofpact_put_REG_MOVE(ofpacts
);
2311 const char *full_arg
= arg
;
2314 error
= mf_parse_subfield__(&move
->src
, &arg
);
2318 if (strncmp(arg
, "->", 2)) {
2319 return xasprintf("%s: missing `->' following source", full_arg
);
2322 error
= mf_parse_subfield(&move
->dst
, arg
);
2327 if (move
->src
.n_bits
!= move
->dst
.n_bits
) {
2328 return xasprintf("%s: source field is %d bits wide but destination is "
2329 "%d bits wide", full_arg
,
2330 move
->src
.n_bits
, move
->dst
.n_bits
);
2336 format_REG_MOVE(const struct ofpact_reg_move
*a
, struct ds
*s
)
2338 nxm_format_reg_move(a
, s
);
2341 /* Action structure for OFPAT12_SET_FIELD. */
2342 struct ofp12_action_set_field
{
2343 ovs_be16 type
; /* OFPAT12_SET_FIELD. */
2344 ovs_be16 len
; /* Length is padded to 64 bits. */
2347 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2348 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2350 * The "pad" member is the beginning of the above. */
2353 OFP_ASSERT(sizeof(struct ofp12_action_set_field
) == 8);
2355 /* Action structure for NXAST_REG_LOAD.
2357 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2358 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2359 * starts at 0 for the least-significant bit, 1 for the next most significant
2362 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2363 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2366 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2367 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2368 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2369 * also stored as one less than its true value:
2372 * +------------------------------+------------------+
2373 * | ofs | n_bits - 1 |
2374 * +------------------------------+------------------+
2376 * The switch will reject actions for which ofs+n_bits is greater than the
2377 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2378 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2379 * OFPBAC_BAD_ARGUMENT.
2381 struct nx_action_reg_load
{
2382 ovs_be16 type
; /* OFPAT_VENDOR. */
2383 ovs_be16 len
; /* Length is 24. */
2384 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2385 ovs_be16 subtype
; /* NXAST_REG_LOAD. */
2386 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
2387 ovs_be32 dst
; /* Destination register. */
2388 ovs_be64 value
; /* Immediate value. */
2390 OFP_ASSERT(sizeof(struct nx_action_reg_load
) == 24);
2392 /* Action structure for NXAST_REG_LOAD2.
2394 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2395 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2396 * OXM experimenter fields. */
2397 struct nx_action_reg_load2
{
2398 ovs_be16 type
; /* OFPAT_VENDOR. */
2399 ovs_be16 len
; /* At least 16. */
2400 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2401 ovs_be16 subtype
; /* NXAST_SET_FIELD. */
2404 * - An NXM/OXM header, value, and optionally a mask.
2405 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2407 * The "pad" member is the beginning of the above. */
2410 OFP_ASSERT(sizeof(struct nx_action_reg_load2
) == 16);
2413 decode_ofpat_set_field(const struct ofp12_action_set_field
*oasf
,
2414 bool may_mask
, struct ofpbuf
*ofpacts
)
2416 struct ofpbuf b
= ofpbuf_const_initializer(oasf
, ntohs(oasf
->len
));
2417 ofpbuf_pull(&b
, OBJECT_OFFSETOF(oasf
, pad
));
2419 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(ofpacts
);
2420 enum ofperr error
= nx_pull_entry(&b
, &sf
->field
, &sf
->value
,
2421 may_mask
? &sf
->mask
: NULL
);
2423 return (error
== OFPERR_OFPBMC_BAD_MASK
2424 ? OFPERR_OFPBAC_BAD_SET_MASK
2428 memset(&sf
->mask
, 0xff, sf
->field
->n_bytes
);
2431 if (!is_all_zeros(b
.data
, b
.size
)) {
2432 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2435 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2437 if (sf
->field
->id
== MFF_IN_PORT_OXM
) {
2438 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2441 /* oxm_length is now validated to be compatible with mf_value. */
2442 if (!sf
->field
->writable
) {
2443 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2445 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2448 /* The value must be valid for match. OpenFlow 1.5 also says,
2449 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2450 * a 1-bit in oxm_value and in oxm_mask." */
2451 if (!mf_is_value_valid(sf
->field
, &sf
->value
)
2452 || (sf
->field
->id
== MFF_VLAN_VID
2453 && (!(sf
->mask
.be16
& htons(OFPVID12_PRESENT
))
2454 || !(sf
->value
.be16
& htons(OFPVID12_PRESENT
))))) {
2455 struct ds ds
= DS_EMPTY_INITIALIZER
;
2456 mf_format(sf
->field
, &sf
->value
, NULL
, &ds
);
2457 VLOG_WARN_RL(&rl
, "Invalid value for set field %s: %s",
2458 sf
->field
->name
, ds_cstr(&ds
));
2461 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2467 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2468 enum ofp_version ofp_version OVS_UNUSED
,
2469 struct ofpbuf
*ofpacts
)
2471 return decode_ofpat_set_field(oasf
, false, ofpacts
);
2475 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field
*oasf
,
2476 enum ofp_version ofp_version OVS_UNUSED
,
2477 struct ofpbuf
*ofpacts
)
2479 return decode_ofpat_set_field(oasf
, true, ofpacts
);
2483 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load
*narl
,
2484 enum ofp_version ofp_version OVS_UNUSED
,
2487 struct ofpact_set_field
*sf
= ofpact_put_reg_load(out
);
2488 struct mf_subfield dst
;
2491 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD
;
2493 dst
.field
= mf_from_nxm_header(ntohl(narl
->dst
));
2494 dst
.ofs
= nxm_decode_ofs(narl
->ofs_nbits
);
2495 dst
.n_bits
= nxm_decode_n_bits(narl
->ofs_nbits
);
2496 error
= mf_check_dst(&dst
, NULL
);
2501 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2503 if (dst
.n_bits
< 64 && ntohll(narl
->value
) >> dst
.n_bits
) {
2504 return OFPERR_OFPBAC_BAD_ARGUMENT
;
2507 sf
->field
= dst
.field
;
2508 bitwise_put(ntohll(narl
->value
),
2509 &sf
->value
, dst
.field
->n_bytes
, dst
.ofs
,
2511 bitwise_put(UINT64_MAX
,
2512 &sf
->mask
, dst
.field
->n_bytes
, dst
.ofs
,
2519 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2
*narl
,
2520 enum ofp_version ofp_version OVS_UNUSED
,
2523 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(out
);
2524 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD2
;
2526 struct ofpbuf b
= ofpbuf_const_initializer(narl
, ntohs(narl
->len
));
2527 ofpbuf_pull(&b
, OBJECT_OFFSETOF(narl
, pad
));
2529 enum ofperr error
= nx_pull_entry(&b
, &sf
->field
, &sf
->value
, &sf
->mask
);
2533 if (!is_all_zeros(b
.data
, b
.size
)) {
2534 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2537 if (!sf
->field
->writable
) {
2538 VLOG_WARN_RL(&rl
, "destination field %s is not writable",
2540 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
2546 ofpact_put_set_field(struct ofpbuf
*openflow
, enum ofp_version ofp_version
,
2547 enum mf_field_id field
, uint64_t value_
)
2549 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
2550 int n_bytes
= mf_from_id(field
)->n_bytes
;
2551 size_t start_ofs
= openflow
->size
;
2552 union mf_value value
;
2554 value
.be64
= htonll(value_
<< (8 * (8 - n_bytes
)));
2556 oasf
= put_OFPAT12_SET_FIELD(openflow
);
2557 openflow
->size
= openflow
->size
- sizeof oasf
->pad
;
2558 nx_put_entry(openflow
, field
, ofp_version
, &value
, NULL
);
2559 pad_ofpat(openflow
, start_ofs
);
2563 next_load_segment(const struct ofpact_set_field
*sf
,
2564 struct mf_subfield
*dst
, uint64_t *value
)
2566 int n_bits
= sf
->field
->n_bits
;
2567 int n_bytes
= sf
->field
->n_bytes
;
2568 int start
= dst
->ofs
+ dst
->n_bits
;
2570 if (start
< n_bits
) {
2571 dst
->field
= sf
->field
;
2572 dst
->ofs
= bitwise_scan(&sf
->mask
, n_bytes
, 1, start
, n_bits
);
2573 if (dst
->ofs
< n_bits
) {
2574 dst
->n_bits
= bitwise_scan(&sf
->mask
, n_bytes
, 0, dst
->ofs
+ 1,
2575 MIN(dst
->ofs
+ 64, n_bits
)) - dst
->ofs
;
2576 *value
= bitwise_get(&sf
->value
, n_bytes
, dst
->ofs
, dst
->n_bits
);
2583 /* Convert 'sf' to a series of REG_LOADs. */
2585 set_field_to_nxast(const struct ofpact_set_field
*sf
, struct ofpbuf
*openflow
)
2587 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2588 * experimenter OXM or is variable length (or if it came in as
2589 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2590 * NXAST_REG_LOAD, which is backward compatible. */
2591 if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
2592 || !mf_nxm_header(sf
->field
->id
) || sf
->field
->variable_len
) {
2593 struct nx_action_reg_load2
*narl OVS_UNUSED
;
2594 size_t start_ofs
= openflow
->size
;
2596 narl
= put_NXAST_REG_LOAD2(openflow
);
2597 openflow
->size
= openflow
->size
- sizeof narl
->pad
;
2598 nx_put_entry(openflow
, sf
->field
->id
, 0, &sf
->value
, &sf
->mask
);
2599 pad_ofpat(openflow
, start_ofs
);
2601 struct mf_subfield dst
;
2604 dst
.ofs
= dst
.n_bits
= 0;
2605 while (next_load_segment(sf
, &dst
, &value
)) {
2606 struct nx_action_reg_load
*narl
= put_NXAST_REG_LOAD(openflow
);
2607 narl
->ofs_nbits
= nxm_encode_ofs_nbits(dst
.ofs
, dst
.n_bits
);
2608 narl
->dst
= htonl(mf_nxm_header(dst
.field
->id
));
2609 narl
->value
= htonll(value
);
2614 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2615 * actions, if we can, falling back to Nicira extensions if we must.
2617 * We check only meta-flow types that can appear within set field actions and
2618 * that have a mapping to compatible action types. These struct mf_field
2619 * definitions have a defined OXM or NXM header value and specify the field as
2622 set_field_to_legacy_openflow(const struct ofpact_set_field
*sf
,
2623 enum ofp_version ofp_version
,
2626 switch ((int) sf
->field
->id
) {
2627 case MFF_VLAN_TCI
: {
2628 ovs_be16 tci
= sf
->value
.be16
;
2629 bool cfi
= (tci
& htons(VLAN_CFI
)) != 0;
2630 uint16_t vid
= vlan_tci_to_vid(tci
);
2631 uint8_t pcp
= vlan_tci_to_pcp(tci
);
2633 if (ofp_version
< OFP11_VERSION
) {
2634 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2636 * If CFI=1, Add or modify VLAN VID & PCP.
2637 * If CFI=0, strip VLAN header, if any.
2640 put_OFPAT10_SET_VLAN_VID(out
, vid
);
2641 put_OFPAT10_SET_VLAN_PCP(out
, pcp
);
2643 put_OFPAT10_STRIP_VLAN(out
);
2646 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2648 * If CFI=1, Add or modify VLAN VID & PCP.
2649 * OpenFlow 1.1 set actions only apply if the packet
2650 * already has VLAN tags. To be sure that is the case
2651 * we have to push a VLAN header. As we do not support
2652 * multiple layers of VLANs, this is a no-op, if a VLAN
2653 * header already exists. This may backfire, however,
2654 * when we start supporting multiple layers of VLANs.
2655 * If CFI=0, strip VLAN header, if any.
2658 /* Push a VLAN tag, if one was not seen at action validation
2660 if (!sf
->flow_has_vlan
) {
2661 put_OFPAT11_PUSH_VLAN(out
, htons(ETH_TYPE_VLAN_8021Q
));
2663 put_OFPAT11_SET_VLAN_VID(out
, vid
);
2664 put_OFPAT11_SET_VLAN_PCP(out
, pcp
);
2666 /* If the flow did not match on vlan, we have no way of
2667 * knowing if the vlan tag exists, so we must POP just to be
2669 put_OFPAT11_POP_VLAN(out
);
2675 case MFF_VLAN_VID
: {
2676 uint16_t vid
= ntohs(sf
->value
.be16
) & VLAN_VID_MASK
;
2677 if (ofp_version
== OFP10_VERSION
) {
2678 put_OFPAT10_SET_VLAN_VID(out
, vid
);
2680 put_OFPAT11_SET_VLAN_VID(out
, vid
);
2686 if (ofp_version
== OFP10_VERSION
) {
2687 put_OFPAT10_SET_VLAN_PCP(out
, sf
->value
.u8
);
2689 put_OFPAT11_SET_VLAN_PCP(out
, sf
->value
.u8
);
2694 put_OFPAT_SET_DL_SRC(out
, ofp_version
)->dl_addr
= sf
->value
.mac
;
2698 put_OFPAT_SET_DL_DST(out
, ofp_version
)->dl_addr
= sf
->value
.mac
;
2702 put_OFPAT_SET_NW_SRC(out
, ofp_version
, sf
->value
.be32
);
2706 put_OFPAT_SET_NW_DST(out
, ofp_version
, sf
->value
.be32
);
2710 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
.u8
);
2713 case MFF_IP_DSCP_SHIFTED
:
2714 put_OFPAT_SET_NW_TOS(out
, ofp_version
, sf
->value
.u8
<< 2);
2719 put_OFPAT_SET_TP_SRC(out
, sf
->value
.be16
);
2724 put_OFPAT_SET_TP_DST(out
, sf
->value
.be16
);
2728 set_field_to_nxast(sf
, out
);
2734 set_field_to_set_field(const struct ofpact_set_field
*sf
,
2735 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2737 struct ofp12_action_set_field
*oasf OVS_UNUSED
;
2738 size_t start_ofs
= out
->size
;
2740 oasf
= put_OFPAT12_SET_FIELD(out
);
2741 out
->size
= out
->size
- sizeof oasf
->pad
;
2742 nx_put_entry(out
, sf
->field
->id
, ofp_version
, &sf
->value
, &sf
->mask
);
2743 pad_ofpat(out
, start_ofs
);
2747 encode_SET_FIELD(const struct ofpact_set_field
*sf
,
2748 enum ofp_version ofp_version
, struct ofpbuf
*out
)
2750 if (ofp_version
>= OFP15_VERSION
) {
2751 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2753 set_field_to_set_field(sf
, ofp_version
, out
);
2754 } else if (sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD
||
2755 sf
->ofpact
.raw
== NXAST_RAW_REG_LOAD2
) {
2756 /* It came in as reg_load, send it out the same way. */
2757 set_field_to_nxast(sf
, out
);
2758 } else if (ofp_version
< OFP12_VERSION
) {
2759 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2760 set_field_to_legacy_openflow(sf
, ofp_version
, out
);
2761 } else if (is_all_ones((const uint8_t *) &sf
->mask
, sf
->field
->n_bytes
)) {
2762 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2763 * entire field, so encode it as OFPAT_SET_FIELD. */
2764 set_field_to_set_field(sf
, ofp_version
, out
);
2766 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2767 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2768 * so encode it as reg_load. */
2769 set_field_to_nxast(sf
, out
);
2773 /* Parses the input argument 'arg' into the key, value, and delimiter
2774 * components that are common across the reg_load and set_field action format.
2776 * With an argument like "1->metadata", sets the following pointers to
2777 * point within 'arg':
2782 * Returns NULL if successful, otherwise a malloc()'d string describing the
2783 * error. The caller is responsible for freeing the returned string. */
2784 static char * OVS_WARN_UNUSED_RESULT
2785 set_field_split_str(char *arg
, char **key
, char **value
, char **delim
)
2790 value_end
= strstr(arg
, "->");
2791 *key
= value_end
+ strlen("->");
2797 return xasprintf("%s: missing `->'", arg
);
2799 if (strlen(value_end
) <= strlen("->")) {
2800 return xasprintf("%s: missing field name following `->'", arg
);
2806 /* Parses a "set_field" action with argument 'arg', appending the parsed
2807 * action to 'ofpacts'.
2809 * Returns NULL if successful, otherwise a malloc()'d string describing the
2810 * error. The caller is responsible for freeing the returned string. */
2811 static char * OVS_WARN_UNUSED_RESULT
2812 set_field_parse__(char *arg
, struct ofpbuf
*ofpacts
,
2813 enum ofputil_protocol
*usable_protocols
)
2815 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(ofpacts
);
2819 const struct mf_field
*mf
;
2822 error
= set_field_split_str(arg
, &key
, &value
, &delim
);
2827 mf
= mf_from_name(key
);
2829 return xasprintf("%s is not a valid OXM field name", key
);
2831 if (!mf
->writable
) {
2832 return xasprintf("%s is read-only", key
);
2836 error
= mf_parse(mf
, value
, &sf
->value
, &sf
->mask
);
2841 if (!mf_is_value_valid(mf
, &sf
->value
)) {
2842 return xasprintf("%s is not a valid value for field %s", value
, key
);
2845 *usable_protocols
&= mf
->usable_protocols_exact
;
2849 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2850 * action to 'ofpacts'.
2852 * Returns NULL if successful, otherwise a malloc()'d string describing the
2853 * error. The caller is responsible for freeing the returned string. */
2854 static char * OVS_WARN_UNUSED_RESULT
2855 parse_SET_FIELD(const char *arg
, struct ofpbuf
*ofpacts
,
2856 enum ofputil_protocol
*usable_protocols
)
2858 char *copy
= xstrdup(arg
);
2859 char *error
= set_field_parse__(copy
, ofpacts
, usable_protocols
);
2864 static char * OVS_WARN_UNUSED_RESULT
2865 parse_reg_load(char *arg
, struct ofpbuf
*ofpacts
)
2867 struct ofpact_set_field
*sf
= ofpact_put_reg_load(ofpacts
);
2868 struct mf_subfield dst
;
2869 char *key
, *value_str
;
2870 union mf_value value
;
2873 error
= set_field_split_str(arg
, &key
, &value_str
, NULL
);
2878 error
= mf_parse_subfield(&dst
, key
);
2883 if (parse_int_string(value_str
, (uint8_t *)&value
, dst
.field
->n_bytes
,
2885 return xasprintf("%s: cannot parse integer value", arg
);
2888 if (!bitwise_is_all_zeros(&value
, dst
.field
->n_bytes
, dst
.n_bits
,
2889 dst
.field
->n_bytes
* 8 - dst
.n_bits
)) {
2893 mf_format(dst
.field
, &value
, NULL
, &ds
);
2894 error
= xasprintf("%s: value %s does not fit into %d bits",
2895 arg
, ds_cstr(&ds
), dst
.n_bits
);
2900 sf
->field
= dst
.field
;
2901 memset(&sf
->value
, 0, sizeof sf
->value
);
2902 bitwise_copy(&value
, dst
.field
->n_bytes
, 0, &sf
->value
,
2903 dst
.field
->n_bytes
, dst
.ofs
, dst
.n_bits
);
2904 bitwise_one(&sf
->mask
, dst
.field
->n_bytes
, dst
.ofs
, dst
.n_bits
);
2910 format_SET_FIELD(const struct ofpact_set_field
*a
, struct ds
*s
)
2912 if (a
->ofpact
.raw
== NXAST_RAW_REG_LOAD
) {
2913 struct mf_subfield dst
;
2916 dst
.ofs
= dst
.n_bits
= 0;
2917 while (next_load_segment(a
, &dst
, &value
)) {
2918 ds_put_format(s
, "%sload:%s%#"PRIx64
"%s->%s",
2919 colors
.special
, colors
.end
, value
,
2920 colors
.special
, colors
.end
);
2921 mf_format_subfield(&dst
, s
);
2922 ds_put_char(s
, ',');
2926 ds_put_format(s
, "%sset_field:%s", colors
.special
, colors
.end
);
2927 mf_format(a
->field
, &a
->value
, &a
->mask
, s
);
2928 ds_put_format(s
, "%s->%s%s",
2929 colors
.special
, colors
.end
, a
->field
->name
);
2933 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2934 * is marked such that, if possible, it will be translated to OpenFlow as
2935 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2936 * that was the way that the action was expressed when it came into OVS or for
2937 * backward compatibility. */
2938 struct ofpact_set_field
*
2939 ofpact_put_reg_load(struct ofpbuf
*ofpacts
)
2941 struct ofpact_set_field
*sf
= ofpact_put_SET_FIELD(ofpacts
);
2942 sf
->ofpact
.raw
= NXAST_RAW_REG_LOAD
;
2946 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2948 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2951 struct nx_action_stack
{
2952 ovs_be16 type
; /* OFPAT_VENDOR. */
2953 ovs_be16 len
; /* Length is 16. */
2954 ovs_be32 vendor
; /* NX_VENDOR_ID. */
2955 ovs_be16 subtype
; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2956 ovs_be16 offset
; /* Bit offset into the field. */
2958 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2959 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2960 * - Enough 0-bytes to pad out the action to 24 bytes. */
2961 uint8_t pad
[12]; /* See above. */
2963 OFP_ASSERT(sizeof(struct nx_action_stack
) == 24);
2966 decode_stack_action(const struct nx_action_stack
*nasp
,
2967 struct ofpact_stack
*stack_action
)
2969 stack_action
->subfield
.ofs
= ntohs(nasp
->offset
);
2971 struct ofpbuf b
= ofpbuf_const_initializer(nasp
, sizeof *nasp
);
2972 ofpbuf_pull(&b
, OBJECT_OFFSETOF(nasp
, pad
));
2973 enum ofperr error
= nx_pull_header(&b
, &stack_action
->subfield
.field
,
2978 stack_action
->subfield
.n_bits
= ntohs(*(const ovs_be16
*) b
.data
);
2980 if (!is_all_zeros(b
.data
, b
.size
)) {
2981 return OFPERR_NXBRC_MUST_BE_ZERO
;
2988 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack
*nasp
,
2989 enum ofp_version ofp_version OVS_UNUSED
,
2990 struct ofpbuf
*ofpacts
)
2992 struct ofpact_stack
*push
= ofpact_put_STACK_PUSH(ofpacts
);
2993 enum ofperr error
= decode_stack_action(nasp
, push
);
2994 return error
? error
: nxm_stack_push_check(push
, NULL
);
2998 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack
*nasp
,
2999 enum ofp_version ofp_version OVS_UNUSED
,
3000 struct ofpbuf
*ofpacts
)
3002 struct ofpact_stack
*pop
= ofpact_put_STACK_POP(ofpacts
);
3003 enum ofperr error
= decode_stack_action(nasp
, pop
);
3004 return error
? error
: nxm_stack_pop_check(pop
, NULL
);
3008 encode_STACK_op(const struct ofpact_stack
*stack_action
,
3009 struct nx_action_stack
*nasp
)
3014 nasp
->offset
= htons(stack_action
->subfield
.ofs
);
3016 ofpbuf_use_stack(&b
, nasp
, ntohs(nasp
->len
));
3017 ofpbuf_put_uninit(&b
, OBJECT_OFFSETOF(nasp
, pad
));
3018 nx_put_header(&b
, stack_action
->subfield
.field
->id
, 0, false);
3019 n_bits
= htons(stack_action
->subfield
.n_bits
);
3020 ofpbuf_put(&b
, &n_bits
, sizeof n_bits
);
3024 encode_STACK_PUSH(const struct ofpact_stack
*stack
,
3025 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3027 encode_STACK_op(stack
, put_NXAST_STACK_PUSH(out
));
3031 encode_STACK_POP(const struct ofpact_stack
*stack
,
3032 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3034 encode_STACK_op(stack
, put_NXAST_STACK_POP(out
));
3037 static char * OVS_WARN_UNUSED_RESULT
3038 parse_STACK_PUSH(char *arg
, struct ofpbuf
*ofpacts
,
3039 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3041 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts
), arg
);
3044 static char * OVS_WARN_UNUSED_RESULT
3045 parse_STACK_POP(char *arg
, struct ofpbuf
*ofpacts
,
3046 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3048 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts
), arg
);
3052 format_STACK_PUSH(const struct ofpact_stack
*a
, struct ds
*s
)
3054 nxm_format_stack_push(a
, s
);
3058 format_STACK_POP(const struct ofpact_stack
*a
, struct ds
*s
)
3060 nxm_format_stack_pop(a
, s
);
3063 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
3065 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
3066 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
3067 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3068 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3070 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3071 * sent only to controllers with id 0.)
3073 struct nx_action_cnt_ids
{
3074 ovs_be16 type
; /* OFPAT_VENDOR. */
3075 ovs_be16 len
; /* Length including slaves. */
3076 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3077 ovs_be16 subtype
; /* NXAST_DEC_TTL_CNT_IDS. */
3079 ovs_be16 n_controllers
; /* Number of controllers. */
3080 uint8_t zeros
[4]; /* Must be zero. */
3082 /* Followed by 1 or more controller ids.
3084 * uint16_t cnt_ids[]; // Controller ids.
3085 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
3088 OFP_ASSERT(sizeof(struct nx_action_cnt_ids
) == 16);
3091 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf
*out
)
3094 struct ofpact_cnt_ids
*ids
;
3095 enum ofperr error
= 0;
3097 ids
= ofpact_put_DEC_TTL(out
);
3098 ids
->n_controllers
= 1;
3099 ofpbuf_put(out
, &id
, sizeof id
);
3101 ofpact_finish_DEC_TTL(out
, &ids
);
3106 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids
*nac_ids
,
3107 enum ofp_version ofp_version OVS_UNUSED
,
3110 struct ofpact_cnt_ids
*ids
;
3114 ids
= ofpact_put_DEC_TTL(out
);
3115 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
3116 ids
->n_controllers
= ntohs(nac_ids
->n_controllers
);
3117 ids_size
= ntohs(nac_ids
->len
) - sizeof *nac_ids
;
3119 if (!is_all_zeros(nac_ids
->zeros
, sizeof nac_ids
->zeros
)) {
3120 return OFPERR_NXBRC_MUST_BE_ZERO
;
3123 if (ids_size
< ids
->n_controllers
* sizeof(ovs_be16
)) {
3124 VLOG_WARN_RL(&rl
, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE
" "
3125 "bytes allocated for controller ids. %"PRIuSIZE
" bytes "
3126 "are required for %"PRIu16
" controllers.",
3127 ids_size
, ids
->n_controllers
* sizeof(ovs_be16
),
3128 ids
->n_controllers
);
3129 return OFPERR_OFPBAC_BAD_LEN
;
3132 for (i
= 0; i
< ids
->n_controllers
; i
++) {
3133 uint16_t id
= ntohs(((ovs_be16
*)(nac_ids
+ 1))[i
]);
3134 ofpbuf_put(out
, &id
, sizeof id
);
3138 ofpact_finish_DEC_TTL(out
, &ids
);
3144 encode_DEC_TTL(const struct ofpact_cnt_ids
*dec_ttl
,
3145 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3147 if (dec_ttl
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
3148 || dec_ttl
->n_controllers
!= 1
3149 || dec_ttl
->cnt_ids
[0] != 0) {
3150 struct nx_action_cnt_ids
*nac_ids
= put_NXAST_DEC_TTL_CNT_IDS(out
);
3151 int ids_len
= ROUND_UP(2 * dec_ttl
->n_controllers
, OFP_ACTION_ALIGN
);
3155 nac_ids
->len
= htons(ntohs(nac_ids
->len
) + ids_len
);
3156 nac_ids
->n_controllers
= htons(dec_ttl
->n_controllers
);
3158 ids
= ofpbuf_put_zeros(out
, ids_len
);
3159 for (i
= 0; i
< dec_ttl
->n_controllers
; i
++) {
3160 ids
[i
] = htons(dec_ttl
->cnt_ids
[i
]);
3163 put_OFPAT_DEC_NW_TTL(out
, ofp_version
);
3168 parse_noargs_dec_ttl(struct ofpbuf
*ofpacts
)
3170 struct ofpact_cnt_ids
*ids
;
3173 ofpact_put_DEC_TTL(ofpacts
);
3174 ofpbuf_put(ofpacts
, &id
, sizeof id
);
3175 ids
= ofpacts
->header
;
3176 ids
->n_controllers
++;
3177 ofpact_finish_DEC_TTL(ofpacts
, &ids
);
3180 static char * OVS_WARN_UNUSED_RESULT
3181 parse_DEC_TTL(char *arg
, struct ofpbuf
*ofpacts
,
3182 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3185 parse_noargs_dec_ttl(ofpacts
);
3187 struct ofpact_cnt_ids
*ids
;
3190 ids
= ofpact_put_DEC_TTL(ofpacts
);
3191 ids
->ofpact
.raw
= NXAST_RAW_DEC_TTL_CNT_IDS
;
3192 for (cntr
= strtok_r(arg
, ", ", &arg
); cntr
!= NULL
;
3193 cntr
= strtok_r(NULL
, ", ", &arg
)) {
3194 uint16_t id
= atoi(cntr
);
3196 ofpbuf_put(ofpacts
, &id
, sizeof id
);
3197 ids
= ofpacts
->header
;
3198 ids
->n_controllers
++;
3200 if (!ids
->n_controllers
) {
3201 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3204 ofpact_finish_DEC_TTL(ofpacts
, &ids
);
3210 format_DEC_TTL(const struct ofpact_cnt_ids
*a
, struct ds
*s
)
3214 ds_put_format(s
, "%sdec_ttl%s", colors
.paren
, colors
.end
);
3215 if (a
->ofpact
.raw
== NXAST_RAW_DEC_TTL_CNT_IDS
) {
3216 ds_put_format(s
, "%s(%s", colors
.paren
, colors
.end
);
3217 for (i
= 0; i
< a
->n_controllers
; i
++) {
3219 ds_put_cstr(s
, ",");
3221 ds_put_format(s
, "%"PRIu16
, a
->cnt_ids
[i
]);
3223 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
3227 /* Set MPLS label actions. */
3230 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label
,
3231 enum ofp_version ofp_version OVS_UNUSED
,
3234 ofpact_put_SET_MPLS_LABEL(out
)->label
= label
;
3239 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label
*label
,
3240 enum ofp_version ofp_version
,
3243 if (ofp_version
< OFP12_VERSION
) {
3244 put_OFPAT_SET_MPLS_LABEL(out
, ofp_version
, label
->label
);
3246 ofpact_put_set_field(out
, ofp_version
, MFF_MPLS_LABEL
,
3247 ntohl(label
->label
));
3251 static char * OVS_WARN_UNUSED_RESULT
3252 parse_SET_MPLS_LABEL(char *arg
, struct ofpbuf
*ofpacts
,
3253 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3255 struct ofpact_mpls_label
*mpls_label
= ofpact_put_SET_MPLS_LABEL(ofpacts
);
3257 return xstrdup("set_mpls_label: expected label.");
3260 mpls_label
->label
= htonl(atoi(arg
));
3265 format_SET_MPLS_LABEL(const struct ofpact_mpls_label
*a
, struct ds
*s
)
3267 ds_put_format(s
, "%sset_mpls_label(%s%"PRIu32
"%s)%s",
3268 colors
.paren
, colors
.end
, ntohl(a
->label
),
3269 colors
.paren
, colors
.end
);
3272 /* Set MPLS TC actions. */
3275 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc
,
3276 enum ofp_version ofp_version OVS_UNUSED
,
3279 ofpact_put_SET_MPLS_TC(out
)->tc
= tc
;
3284 encode_SET_MPLS_TC(const struct ofpact_mpls_tc
*tc
,
3285 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3287 if (ofp_version
< OFP12_VERSION
) {
3288 put_OFPAT_SET_MPLS_TC(out
, ofp_version
, tc
->tc
);
3290 ofpact_put_set_field(out
, ofp_version
, MFF_MPLS_TC
, tc
->tc
);
3294 static char * OVS_WARN_UNUSED_RESULT
3295 parse_SET_MPLS_TC(char *arg
, struct ofpbuf
*ofpacts
,
3296 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3298 struct ofpact_mpls_tc
*mpls_tc
= ofpact_put_SET_MPLS_TC(ofpacts
);
3301 return xstrdup("set_mpls_tc: expected tc.");
3304 mpls_tc
->tc
= atoi(arg
);
3309 format_SET_MPLS_TC(const struct ofpact_mpls_tc
*a
, struct ds
*s
)
3311 ds_put_format(s
, "%sset_mpls_ttl(%s%"PRIu8
"%s)%s",
3312 colors
.paren
, colors
.end
, a
->tc
,
3313 colors
.paren
, colors
.end
);
3316 /* Set MPLS TTL actions. */
3319 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl
,
3320 enum ofp_version ofp_version OVS_UNUSED
,
3323 ofpact_put_SET_MPLS_TTL(out
)->ttl
= ttl
;
3328 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*ttl
,
3329 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3331 put_OFPAT_SET_MPLS_TTL(out
, ofp_version
, ttl
->ttl
);
3334 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3335 * action to 'ofpacts'.
3337 * Returns NULL if successful, otherwise a malloc()'d string describing the
3338 * error. The caller is responsible for freeing the returned string. */
3339 static char * OVS_WARN_UNUSED_RESULT
3340 parse_SET_MPLS_TTL(char *arg
, struct ofpbuf
*ofpacts
,
3341 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3343 struct ofpact_mpls_ttl
*mpls_ttl
= ofpact_put_SET_MPLS_TTL(ofpacts
);
3346 return xstrdup("set_mpls_ttl: expected ttl.");
3349 mpls_ttl
->ttl
= atoi(arg
);
3354 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl
*a
, struct ds
*s
)
3356 ds_put_format(s
, "%sset_mpls_ttl(%s%"PRIu8
"%s)%s",
3357 colors
.paren
, colors
.end
, a
->ttl
,
3358 colors
.paren
, colors
.end
);
3361 /* Decrement MPLS TTL actions. */
3364 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf
*out
)
3366 ofpact_put_DEC_MPLS_TTL(out
);
3371 encode_DEC_MPLS_TTL(const struct ofpact_null
*null OVS_UNUSED
,
3372 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3374 put_OFPAT_DEC_MPLS_TTL(out
, ofp_version
);
3377 static char * OVS_WARN_UNUSED_RESULT
3378 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
3379 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3381 ofpact_put_DEC_MPLS_TTL(ofpacts
);
3386 format_DEC_MPLS_TTL(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
3388 ds_put_format(s
, "%sdec_mpls_ttl%s", colors
.value
, colors
.end
);
3391 /* Push MPLS label action. */
3394 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype
,
3395 enum ofp_version ofp_version OVS_UNUSED
,
3398 struct ofpact_push_mpls
*oam
;
3400 if (!eth_type_mpls(ethertype
)) {
3401 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3403 oam
= ofpact_put_PUSH_MPLS(out
);
3404 oam
->ethertype
= ethertype
;
3410 encode_PUSH_MPLS(const struct ofpact_push_mpls
*push_mpls
,
3411 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3413 put_OFPAT_PUSH_MPLS(out
, ofp_version
, push_mpls
->ethertype
);
3416 static char * OVS_WARN_UNUSED_RESULT
3417 parse_PUSH_MPLS(char *arg
, struct ofpbuf
*ofpacts
,
3418 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3423 error
= str_to_u16(arg
, "push_mpls", ðertype
);
3425 ofpact_put_PUSH_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
3431 format_PUSH_MPLS(const struct ofpact_push_mpls
*a
, struct ds
*s
)
3433 ds_put_format(s
, "%spush_mpls:%s0x%04"PRIx16
,
3434 colors
.param
, colors
.end
, ntohs(a
->ethertype
));
3437 /* Pop MPLS label action. */
3440 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype
,
3441 enum ofp_version ofp_version OVS_UNUSED
,
3444 ofpact_put_POP_MPLS(out
)->ethertype
= ethertype
;
3449 encode_POP_MPLS(const struct ofpact_pop_mpls
*pop_mpls
,
3450 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3452 put_OFPAT_POP_MPLS(out
, ofp_version
, pop_mpls
->ethertype
);
3455 static char * OVS_WARN_UNUSED_RESULT
3456 parse_POP_MPLS(char *arg
, struct ofpbuf
*ofpacts
,
3457 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3462 error
= str_to_u16(arg
, "pop_mpls", ðertype
);
3464 ofpact_put_POP_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
3470 format_POP_MPLS(const struct ofpact_pop_mpls
*a
, struct ds
*s
)
3472 ds_put_format(s
, "%spop_mpls:%s0x%04"PRIx16
,
3473 colors
.param
, colors
.end
, ntohs(a
->ethertype
));
3476 /* Set tunnel ID actions. */
3479 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id
,
3480 enum ofp_version ofp_version OVS_UNUSED
,
3483 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
3484 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL
;
3485 tunnel
->tun_id
= tun_id
;
3490 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id
,
3491 enum ofp_version ofp_version OVS_UNUSED
,
3494 struct ofpact_tunnel
*tunnel
= ofpact_put_SET_TUNNEL(out
);
3495 tunnel
->ofpact
.raw
= NXAST_RAW_SET_TUNNEL64
;
3496 tunnel
->tun_id
= tun_id
;
3501 encode_SET_TUNNEL(const struct ofpact_tunnel
*tunnel
,
3502 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3504 uint64_t tun_id
= tunnel
->tun_id
;
3506 if (ofp_version
< OFP12_VERSION
) {
3507 if (tun_id
<= UINT32_MAX
3508 && tunnel
->ofpact
.raw
!= NXAST_RAW_SET_TUNNEL64
) {
3509 put_NXAST_SET_TUNNEL(out
, tun_id
);
3511 put_NXAST_SET_TUNNEL64(out
, tun_id
);
3514 ofpact_put_set_field(out
, ofp_version
, MFF_TUN_ID
, tun_id
);
3518 static char * OVS_WARN_UNUSED_RESULT
3519 parse_set_tunnel(char *arg
, struct ofpbuf
*ofpacts
,
3520 enum ofp_raw_action_type raw
)
3522 struct ofpact_tunnel
*tunnel
;
3524 tunnel
= ofpact_put_SET_TUNNEL(ofpacts
);
3525 tunnel
->ofpact
.raw
= raw
;
3526 return str_to_u64(arg
, &tunnel
->tun_id
);
3529 static char * OVS_WARN_UNUSED_RESULT
3530 parse_SET_TUNNEL(char *arg
, struct ofpbuf
*ofpacts
,
3531 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3533 return parse_set_tunnel(arg
, ofpacts
, NXAST_RAW_SET_TUNNEL
);
3537 format_SET_TUNNEL(const struct ofpact_tunnel
*a
, struct ds
*s
)
3539 ds_put_format(s
, "%sset_tunnel%s:%s%#"PRIx64
, colors
.param
,
3540 (a
->tun_id
> UINT32_MAX
3541 || a
->ofpact
.raw
== NXAST_RAW_SET_TUNNEL64
? "64" : ""),
3542 colors
.end
, a
->tun_id
);
3545 /* Set queue action. */
3548 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id
,
3549 enum ofp_version ofp_version OVS_UNUSED
,
3552 ofpact_put_SET_QUEUE(out
)->queue_id
= queue_id
;
3557 encode_SET_QUEUE(const struct ofpact_queue
*queue
,
3558 enum ofp_version ofp_version
, struct ofpbuf
*out
)
3560 put_OFPAT_SET_QUEUE(out
, ofp_version
, queue
->queue_id
);
3563 static char * OVS_WARN_UNUSED_RESULT
3564 parse_SET_QUEUE(char *arg
, struct ofpbuf
*ofpacts
,
3565 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3567 return str_to_u32(arg
, &ofpact_put_SET_QUEUE(ofpacts
)->queue_id
);
3571 format_SET_QUEUE(const struct ofpact_queue
*a
, struct ds
*s
)
3573 ds_put_format(s
, "%sset_queue:%s%"PRIu32
,
3574 colors
.param
, colors
.end
, a
->queue_id
);
3577 /* Pop queue action. */
3580 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf
*out
)
3582 ofpact_put_POP_QUEUE(out
);
3587 encode_POP_QUEUE(const struct ofpact_null
*null OVS_UNUSED
,
3588 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3590 put_NXAST_POP_QUEUE(out
);
3593 static char * OVS_WARN_UNUSED_RESULT
3594 parse_POP_QUEUE(const char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
3595 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3597 ofpact_put_POP_QUEUE(ofpacts
);
3602 format_POP_QUEUE(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
3604 ds_put_format(s
, "%spop_queue%s", colors
.value
, colors
.end
);
3607 /* Action structure for NXAST_FIN_TIMEOUT.
3609 * This action changes the idle timeout or hard timeout, or both, of this
3610 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3611 * When such a packet is observed, the action reduces the rule's idle timeout
3612 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3613 * action has no effect on an existing timeout that is already shorter than the
3614 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3615 * of zero has no effect on the respective timeout.
3617 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3618 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3619 * receipt of the FIN or RST.
3621 * This is useful for quickly discarding learned TCP flows that otherwise will
3622 * take a long time to expire.
3624 * This action is intended for use with an OpenFlow rule that matches only a
3625 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3626 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3627 * RST in any of those flows will cause the entire OpenFlow rule to expire
3628 * early, which is not normally desirable.
3630 struct nx_action_fin_timeout
{
3631 ovs_be16 type
; /* OFPAT_VENDOR. */
3632 ovs_be16 len
; /* 16. */
3633 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3634 ovs_be16 subtype
; /* NXAST_FIN_TIMEOUT. */
3635 ovs_be16 fin_idle_timeout
; /* New idle timeout, if nonzero. */
3636 ovs_be16 fin_hard_timeout
; /* New hard timeout, if nonzero. */
3637 ovs_be16 pad
; /* Must be zero. */
3639 OFP_ASSERT(sizeof(struct nx_action_fin_timeout
) == 16);
3642 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout
*naft
,
3643 enum ofp_version ofp_version OVS_UNUSED
,
3646 struct ofpact_fin_timeout
*oft
;
3648 oft
= ofpact_put_FIN_TIMEOUT(out
);
3649 oft
->fin_idle_timeout
= ntohs(naft
->fin_idle_timeout
);
3650 oft
->fin_hard_timeout
= ntohs(naft
->fin_hard_timeout
);
3655 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout
*fin_timeout
,
3656 enum ofp_version ofp_version OVS_UNUSED
,
3659 struct nx_action_fin_timeout
*naft
= put_NXAST_FIN_TIMEOUT(out
);
3660 naft
->fin_idle_timeout
= htons(fin_timeout
->fin_idle_timeout
);
3661 naft
->fin_hard_timeout
= htons(fin_timeout
->fin_hard_timeout
);
3664 static char * OVS_WARN_UNUSED_RESULT
3665 parse_FIN_TIMEOUT(char *arg
, struct ofpbuf
*ofpacts
,
3666 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3668 struct ofpact_fin_timeout
*oft
= ofpact_put_FIN_TIMEOUT(ofpacts
);
3671 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
3674 if (!strcmp(key
, "idle_timeout")) {
3675 error
= str_to_u16(value
, key
, &oft
->fin_idle_timeout
);
3676 } else if (!strcmp(key
, "hard_timeout")) {
3677 error
= str_to_u16(value
, key
, &oft
->fin_hard_timeout
);
3679 error
= xasprintf("invalid key '%s' in 'fin_timeout' argument",
3691 format_FIN_TIMEOUT(const struct ofpact_fin_timeout
*a
, struct ds
*s
)
3693 ds_put_format(s
, "%sfin_timeout(%s", colors
.paren
, colors
.end
);
3694 if (a
->fin_idle_timeout
) {
3695 ds_put_format(s
, "%sidle_timeout=%s%"PRIu16
",",
3696 colors
.param
, colors
.end
, a
->fin_idle_timeout
);
3698 if (a
->fin_hard_timeout
) {
3699 ds_put_format(s
, "%shard_timeout=%s%"PRIu16
",",
3700 colors
.param
, colors
.end
, a
->fin_hard_timeout
);
3703 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
3706 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3708 * These actions search one of the switch's flow tables:
3710 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3711 * it specifies the table to search.
3713 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3714 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3715 * table, that is, the OpenFlow flow table that contains the flow from
3716 * which this action was obtained. If this action did not come from a
3717 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3718 * is the current table.
3720 * The flow table lookup uses a flow that may be slightly modified from the
3723 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3724 * is used as the flow's in_port.
3726 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3727 * then its value is used as the flow's in_port. Otherwise, the original
3730 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3731 * resubmit action, then the flow is updated with the new values.
3733 * Following the lookup, the original in_port is restored.
3735 * If the modified flow matched in the flow table, then the corresponding
3736 * actions are executed. Afterward, actions following the resubmit in the
3737 * original set of actions, if any, are executed; any changes made to the
3738 * packet (e.g. changes to VLAN) by secondary actions persist when those
3739 * actions are executed, although the original in_port is restored.
3741 * Resubmit actions may be used any number of times within a set of actions.
3743 * Resubmit actions may nest. To prevent infinite loops and excessive resource
3744 * use, the implementation may limit nesting depth and the total number of
3747 * - Open vSwitch 1.0.1 and earlier did not support recursion.
3749 * - Open vSwitch 1.0.2 and 1.0.3 limited recursion to 8 levels.
3751 * - Open vSwitch 1.1 and 1.2 limited recursion to 16 levels.
3753 * - Open vSwitch 1.2 through 1.8 limited recursion to 32 levels.
3755 * - Open vSwitch 1.9 through 2.0 limited recursion to 64 levels.
3757 * - Open vSwitch 2.1 through 2.5 limited recursion to 64 levels and impose
3758 * a total limit of 4,096 resubmits per flow translation (earlier versions
3759 * did not impose any total limit).
3761 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3762 * 'pad' to be all-bits-zero.
3764 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3765 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3767 struct nx_action_resubmit
{
3768 ovs_be16 type
; /* OFPAT_VENDOR. */
3769 ovs_be16 len
; /* Length is 16. */
3770 ovs_be32 vendor
; /* NX_VENDOR_ID. */
3771 ovs_be16 subtype
; /* NXAST_RESUBMIT. */
3772 ovs_be16 in_port
; /* New in_port for checking flow table. */
3773 uint8_t table
; /* NXAST_RESUBMIT_TABLE: table to use. */
3776 OFP_ASSERT(sizeof(struct nx_action_resubmit
) == 16);
3779 decode_NXAST_RAW_RESUBMIT(uint16_t port
,
3780 enum ofp_version ofp_version OVS_UNUSED
,
3783 struct ofpact_resubmit
*resubmit
;
3785 resubmit
= ofpact_put_RESUBMIT(out
);
3786 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT
;
3787 resubmit
->in_port
= u16_to_ofp(port
);
3788 resubmit
->table_id
= 0xff;
3793 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit
*nar
,
3794 enum ofp_version ofp_version OVS_UNUSED
,
3797 struct ofpact_resubmit
*resubmit
;
3799 if (nar
->pad
[0] || nar
->pad
[1] || nar
->pad
[2]) {
3800 return OFPERR_OFPBAC_BAD_ARGUMENT
;
3803 resubmit
= ofpact_put_RESUBMIT(out
);
3804 resubmit
->ofpact
.raw
= NXAST_RAW_RESUBMIT_TABLE
;
3805 resubmit
->in_port
= u16_to_ofp(ntohs(nar
->in_port
));
3806 resubmit
->table_id
= nar
->table
;
3811 encode_RESUBMIT(const struct ofpact_resubmit
*resubmit
,
3812 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
3814 uint16_t in_port
= ofp_to_u16(resubmit
->in_port
);
3816 if (resubmit
->table_id
== 0xff
3817 && resubmit
->ofpact
.raw
!= NXAST_RAW_RESUBMIT_TABLE
) {
3818 put_NXAST_RESUBMIT(out
, in_port
);
3820 struct nx_action_resubmit
*nar
= put_NXAST_RESUBMIT_TABLE(out
);
3821 nar
->table
= resubmit
->table_id
;
3822 nar
->in_port
= htons(in_port
);
3826 static char * OVS_WARN_UNUSED_RESULT
3827 parse_RESUBMIT(char *arg
, struct ofpbuf
*ofpacts
,
3828 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
3830 struct ofpact_resubmit
*resubmit
;
3831 char *in_port_s
, *table_s
;
3833 resubmit
= ofpact_put_RESUBMIT(ofpacts
);
3835 in_port_s
= strsep(&arg
, ",");
3836 if (in_port_s
&& in_port_s
[0]) {
3837 if (!ofputil_port_from_string(in_port_s
, &resubmit
->in_port
)) {
3838 return xasprintf("%s: resubmit to unknown port", in_port_s
);
3841 resubmit
->in_port
= OFPP_IN_PORT
;
3844 table_s
= strsep(&arg
, ",");
3845 if (table_s
&& table_s
[0]) {
3846 uint32_t table_id
= 0;
3849 error
= str_to_u32(table_s
, &table_id
);
3853 resubmit
->table_id
= table_id
;
3855 resubmit
->table_id
= 255;
3858 if (resubmit
->in_port
== OFPP_IN_PORT
&& resubmit
->table_id
== 255) {
3859 return xstrdup("at least one \"in_port\" or \"table\" must be "
3860 "specified on resubmit");
3866 format_RESUBMIT(const struct ofpact_resubmit
*a
, struct ds
*s
)
3868 if (a
->in_port
!= OFPP_IN_PORT
&& a
->table_id
== 255) {
3869 ds_put_format(s
, "%sresubmit:%s", colors
.special
, colors
.end
);
3870 ofputil_format_port(a
->in_port
, s
);
3872 ds_put_format(s
, "%sresubmit(%s", colors
.paren
, colors
.end
);
3873 if (a
->in_port
!= OFPP_IN_PORT
) {
3874 ofputil_format_port(a
->in_port
, s
);
3876 ds_put_char(s
, ',');
3877 if (a
->table_id
!= 255) {
3878 ds_put_format(s
, "%"PRIu8
, a
->table_id
);
3880 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
3884 /* Action structure for NXAST_LEARN.
3886 * This action adds or modifies a flow in an OpenFlow table, similar to
3887 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3888 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3889 * flow's match criteria and actions are built by applying each of the series
3890 * of flow_mod_spec elements included as part of the action.
3892 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3893 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3894 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3895 * bits from a source to a destination. In this case, the header contains
3898 * 15 14 13 12 11 10 0
3899 * +------+---+------+---------------------------------+
3900 * | 0 |src| dst | n_bits |
3901 * +------+---+------+---------------------------------+
3903 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3904 * following table summarizes the meaning of each possible combination.
3905 * Details follow the table:
3908 * --- --- ----------------------------------------------------------
3909 * 0 0 Add match criteria based on value in a field.
3910 * 1 0 Add match criteria based on an immediate value.
3911 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3912 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3913 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3914 * All other combinations are undefined and not allowed.
3916 * The flow_mod_spec header is followed by a source specification and a
3917 * destination specification. The format and meaning of the source
3918 * specification depends on 'src':
3920 * - If 'src' is 0, the source bits are taken from a field in the flow to
3921 * which this action is attached. (This should be a wildcarded field. If
3922 * its value is fully specified then the source bits being copied have
3925 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3926 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3927 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3928 * 'field' and 'ofs' are subject to the same restrictions as the source
3929 * field in NXAST_REG_MOVE.
3931 * - If 'src' is 1, the source bits are a constant value. The source
3932 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3933 * number in network order, the source bits are the 'n_bits'
3934 * least-significant bits. The switch will report an error if other bits
3935 * in the constant are nonzero.
3937 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3938 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3939 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3940 * meaning of the flow_mod_spec depends on 'dst':
3942 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3943 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3944 * packet equal the source bits. 'field' may be any nxm_header with
3945 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3947 * Order is significant. Earlier flow_mod_specs must satisfy any
3948 * prerequisites for matching fields specified later, by copying constant
3949 * values into prerequisite fields.
3951 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3954 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3955 * the new flow. The new flow copies the source bits into
3956 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3959 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3960 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3962 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3963 * empty. It has the following meaning:
3965 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3966 * The new flow outputs to the OpenFlow port specified by the source field.
3967 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3968 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3971 * Resource Management
3972 * -------------------
3974 * A switch has a finite amount of flow table space available for learning.
3975 * When this space is exhausted, no new learning table entries will be learned
3976 * until some existing flow table entries expire. The controller should be
3977 * prepared to handle this by flooding (which can be implemented as a
3978 * low-priority flow).
3980 * If a learned flow matches a single TCP stream with a relatively long
3981 * timeout, one may make the best of resource constraints by setting
3982 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3983 * both, to shorter timeouts. When either of these is specified as a nonzero
3984 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3990 * The following examples give a prose description of the flow_mod_specs along
3991 * with informal notation for how those would be represented and a hex dump of
3992 * the bytes that would be required.
3994 * These examples could work with various nx_action_learn parameters. Typical
3995 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3996 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3998 * 1. Learn input port based on the source MAC, with lookup into
3999 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
4001 * Match on in_port=99:
4002 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
4003 * ovs_be16(99), 00 63
4004 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4006 * Match Ethernet destination on Ethernet source from packet:
4007 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4008 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4009 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4011 * Set NXM_NX_REG1[16:31] to the packet's input port:
4012 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
4013 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4014 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
4016 * Given a packet that arrived on port A with Ethernet source address B,
4017 * this would set up the flow "in_port=99, dl_dst=B,
4018 * actions=load:A->NXM_NX_REG1[16..31]".
4020 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
4021 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4022 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
4024 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
4025 * into NXM_NX_REG1[16:31]:
4027 * Match on same VLAN ID as packet:
4028 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
4029 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4030 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4032 * Match Ethernet destination on Ethernet source from packet:
4033 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4034 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4035 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4037 * Output to the packet's input port:
4038 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
4039 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4041 * Given a packet that arrived on port A with Ethernet source address B in
4042 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
4043 * actions=output:A".
4045 * In syntax accepted by ovs-ofctl, this action is:
4046 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4047 * output:NXM_OF_IN_PORT[])
4049 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
4050 * 10-second MAC expiration time to make it easier to see what's going on
4052 * ovs-vsctl del-controller br0
4053 * ovs-ofctl del-flows br0
4054 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
4055 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
4056 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
4057 output:NXM_OF_IN_PORT[]), resubmit(,1)"
4058 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
4060 * You can then dump the MAC learning table with:
4062 * ovs-ofctl dump-flows br0 table=1
4067 * For best performance, segregate learned flows into a table that is not used
4068 * for any other flows except possibly for a lowest-priority "catch-all" flow
4069 * (a flow with no match criteria). If different learning actions specify
4070 * different match criteria, use different tables for the learned flows.
4072 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
4073 * These timeouts apply to the flow that is added, which means that a flow with
4074 * an idle timeout will expire when no traffic has been sent *to* the learned
4075 * address. This is not usually the intent in MAC learning; instead, we want
4076 * the MAC learn entry to expire when no traffic has been sent *from* the
4077 * learned address. Use a hard timeout for that.
4080 * Visibility of Changes
4081 * ---------------------
4083 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
4084 * flow translation are visible to flow table lookups made later in the flow
4085 * translation. This means that, in the example above, a MAC learned by the
4086 * learn action in table 0 would be found in table 1 (if the packet being
4087 * processed had the same source and destination MAC address).
4089 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
4090 * modify a flow) by a "learn" action are visible only for later flow
4091 * translations, not for later lookups within the same flow translation. In
4092 * the MAC learning example, a MAC learned by the learn action in table 0 would
4093 * not be found in table 1 if the flow translation would resubmit to table 1
4094 * after the processing of the learn action, meaning that if this MAC had not
4095 * been learned before then the packet would be flooded. */
4096 struct nx_action_learn
{
4097 ovs_be16 type
; /* OFPAT_VENDOR. */
4098 ovs_be16 len
; /* At least 24. */
4099 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4100 ovs_be16 subtype
; /* NXAST_LEARN. */
4101 ovs_be16 idle_timeout
; /* Idle time before discarding (seconds). */
4102 ovs_be16 hard_timeout
; /* Max time before discarding (seconds). */
4103 ovs_be16 priority
; /* Priority level of flow entry. */
4104 ovs_be64 cookie
; /* Cookie for new flow. */
4105 ovs_be16 flags
; /* NX_LEARN_F_*. */
4106 uint8_t table_id
; /* Table to insert flow entry. */
4107 uint8_t pad
; /* Must be zero. */
4108 ovs_be16 fin_idle_timeout
; /* Idle timeout after FIN, if nonzero. */
4109 ovs_be16 fin_hard_timeout
; /* Hard timeout after FIN, if nonzero. */
4110 /* Followed by a sequence of flow_mod_spec elements, as described above,
4111 * until the end of the action is reached. */
4113 OFP_ASSERT(sizeof(struct nx_action_learn
) == 32);
4116 get_be16(const void **pp
)
4118 const ovs_be16
*p
= *pp
;
4119 ovs_be16 value
= *p
;
4125 get_be32(const void **pp
)
4127 const ovs_be32
*p
= *pp
;
4128 ovs_be32 value
= get_unaligned_be32(p
);
4134 get_subfield(int n_bits
, const void **p
, struct mf_subfield
*sf
)
4136 sf
->field
= mf_from_nxm_header(ntohl(get_be32(p
)));
4137 sf
->ofs
= ntohs(get_be16(p
));
4138 sf
->n_bits
= n_bits
;
4142 learn_min_len(uint16_t header
)
4144 int n_bits
= header
& NX_LEARN_N_BITS_MASK
;
4145 int src_type
= header
& NX_LEARN_SRC_MASK
;
4146 int dst_type
= header
& NX_LEARN_DST_MASK
;
4147 unsigned int min_len
;
4150 if (src_type
== NX_LEARN_SRC_FIELD
) {
4151 min_len
+= sizeof(ovs_be32
); /* src_field */
4152 min_len
+= sizeof(ovs_be16
); /* src_ofs */
4154 min_len
+= DIV_ROUND_UP(n_bits
, 16);
4156 if (dst_type
== NX_LEARN_DST_MATCH
||
4157 dst_type
== NX_LEARN_DST_LOAD
) {
4158 min_len
+= sizeof(ovs_be32
); /* dst_field */
4159 min_len
+= sizeof(ovs_be16
); /* dst_ofs */
4164 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
4165 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
4167 decode_NXAST_RAW_LEARN(const struct nx_action_learn
*nal
,
4168 enum ofp_version ofp_version OVS_UNUSED
,
4169 struct ofpbuf
*ofpacts
)
4171 struct ofpact_learn
*learn
;
4172 const void *p
, *end
;
4175 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4178 learn
= ofpact_put_LEARN(ofpacts
);
4180 learn
->idle_timeout
= ntohs(nal
->idle_timeout
);
4181 learn
->hard_timeout
= ntohs(nal
->hard_timeout
);
4182 learn
->priority
= ntohs(nal
->priority
);
4183 learn
->cookie
= nal
->cookie
;
4184 learn
->table_id
= nal
->table_id
;
4185 learn
->fin_idle_timeout
= ntohs(nal
->fin_idle_timeout
);
4186 learn
->fin_hard_timeout
= ntohs(nal
->fin_hard_timeout
);
4188 learn
->flags
= ntohs(nal
->flags
);
4189 if (learn
->flags
& ~(NX_LEARN_F_SEND_FLOW_REM
|
4190 NX_LEARN_F_DELETE_LEARNED
)) {
4191 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4194 if (learn
->table_id
== 0xff) {
4195 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4198 end
= (char *) nal
+ ntohs(nal
->len
);
4199 for (p
= nal
+ 1; p
!= end
; ) {
4200 struct ofpact_learn_spec
*spec
;
4201 uint16_t header
= ntohs(get_be16(&p
));
4207 spec
= ofpbuf_put_zeros(ofpacts
, sizeof *spec
);
4208 learn
= ofpacts
->header
;
4211 spec
->src_type
= header
& NX_LEARN_SRC_MASK
;
4212 spec
->dst_type
= header
& NX_LEARN_DST_MASK
;
4213 spec
->n_bits
= header
& NX_LEARN_N_BITS_MASK
;
4215 /* Check for valid src and dst type combination. */
4216 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
4217 spec
->dst_type
== NX_LEARN_DST_LOAD
||
4218 (spec
->dst_type
== NX_LEARN_DST_OUTPUT
&&
4219 spec
->src_type
== NX_LEARN_SRC_FIELD
)) {
4222 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4225 /* Check that the arguments don't overrun the end of the action. */
4226 if ((char *) end
- (char *) p
< learn_min_len(header
)) {
4227 return OFPERR_OFPBAC_BAD_LEN
;
4230 /* Get the source. */
4231 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
4232 get_subfield(spec
->n_bits
, &p
, &spec
->src
);
4234 int p_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
4236 bitwise_copy(p
, p_bytes
, 0,
4237 &spec
->src_imm
, sizeof spec
->src_imm
, 0,
4239 p
= (const uint8_t *) p
+ p_bytes
;
4242 /* Get the destination. */
4243 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
4244 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
4245 get_subfield(spec
->n_bits
, &p
, &spec
->dst
);
4248 ofpact_finish_LEARN(ofpacts
, &learn
);
4250 if (!is_all_zeros(p
, (char *) end
- (char *) p
)) {
4251 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4258 put_be16(struct ofpbuf
*b
, ovs_be16 x
)
4260 ofpbuf_put(b
, &x
, sizeof x
);
4264 put_be32(struct ofpbuf
*b
, ovs_be32 x
)
4266 ofpbuf_put(b
, &x
, sizeof x
);
4270 put_u16(struct ofpbuf
*b
, uint16_t x
)
4272 put_be16(b
, htons(x
));
4276 put_u32(struct ofpbuf
*b
, uint32_t x
)
4278 put_be32(b
, htonl(x
));
4282 encode_LEARN(const struct ofpact_learn
*learn
,
4283 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4285 const struct ofpact_learn_spec
*spec
;
4286 struct nx_action_learn
*nal
;
4289 start_ofs
= out
->size
;
4290 nal
= put_NXAST_LEARN(out
);
4291 nal
->idle_timeout
= htons(learn
->idle_timeout
);
4292 nal
->hard_timeout
= htons(learn
->hard_timeout
);
4293 nal
->fin_idle_timeout
= htons(learn
->fin_idle_timeout
);
4294 nal
->fin_hard_timeout
= htons(learn
->fin_hard_timeout
);
4295 nal
->priority
= htons(learn
->priority
);
4296 nal
->cookie
= learn
->cookie
;
4297 nal
->flags
= htons(learn
->flags
);
4298 nal
->table_id
= learn
->table_id
;
4300 for (spec
= learn
->specs
; spec
< &learn
->specs
[learn
->n_specs
]; spec
++) {
4301 put_u16(out
, spec
->n_bits
| spec
->dst_type
| spec
->src_type
);
4303 if (spec
->src_type
== NX_LEARN_SRC_FIELD
) {
4304 put_u32(out
, mf_nxm_header(spec
->src
.field
->id
));
4305 put_u16(out
, spec
->src
.ofs
);
4307 size_t n_dst_bytes
= 2 * DIV_ROUND_UP(spec
->n_bits
, 16);
4308 uint8_t *bits
= ofpbuf_put_zeros(out
, n_dst_bytes
);
4309 bitwise_copy(&spec
->src_imm
, sizeof spec
->src_imm
, 0,
4310 bits
, n_dst_bytes
, 0,
4314 if (spec
->dst_type
== NX_LEARN_DST_MATCH
||
4315 spec
->dst_type
== NX_LEARN_DST_LOAD
) {
4316 put_u32(out
, mf_nxm_header(spec
->dst
.field
->id
));
4317 put_u16(out
, spec
->dst
.ofs
);
4321 pad_ofpat(out
, start_ofs
);
4324 static char * OVS_WARN_UNUSED_RESULT
4325 parse_LEARN(char *arg
, struct ofpbuf
*ofpacts
,
4326 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4328 return learn_parse(arg
, ofpacts
);
4332 format_LEARN(const struct ofpact_learn
*a
, struct ds
*s
)
4337 /* Action structure for NXAST_CONJUNCTION. */
4338 struct nx_action_conjunction
{
4339 ovs_be16 type
; /* OFPAT_VENDOR. */
4340 ovs_be16 len
; /* At least 16. */
4341 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4342 ovs_be16 subtype
; /* See enum ofp_raw_action_type. */
4347 OFP_ASSERT(sizeof(struct nx_action_conjunction
) == 16);
4350 add_conjunction(struct ofpbuf
*out
,
4351 uint32_t id
, uint8_t clause
, uint8_t n_clauses
)
4353 struct ofpact_conjunction
*oc
;
4355 oc
= ofpact_put_CONJUNCTION(out
);
4357 oc
->clause
= clause
;
4358 oc
->n_clauses
= n_clauses
;
4362 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction
*nac
,
4363 enum ofp_version ofp_version OVS_UNUSED
,
4366 if (nac
->n_clauses
< 2 || nac
->n_clauses
> 64
4367 || nac
->clause
>= nac
->n_clauses
) {
4368 return OFPERR_NXBAC_BAD_CONJUNCTION
;
4370 add_conjunction(out
, ntohl(nac
->id
), nac
->clause
, nac
->n_clauses
);
4376 encode_CONJUNCTION(const struct ofpact_conjunction
*oc
,
4377 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4379 struct nx_action_conjunction
*nac
= put_NXAST_CONJUNCTION(out
);
4380 nac
->clause
= oc
->clause
;
4381 nac
->n_clauses
= oc
->n_clauses
;
4382 nac
->id
= htonl(oc
->id
);
4386 format_CONJUNCTION(const struct ofpact_conjunction
*oc
, struct ds
*s
)
4388 ds_put_format(s
, "%sconjunction(%s%"PRIu32
",%"PRIu8
"/%"PRIu8
"%s)%s",
4389 colors
.paren
, colors
.end
,
4390 oc
->id
, oc
->clause
+ 1, oc
->n_clauses
,
4391 colors
.paren
, colors
.end
);
4394 static char * OVS_WARN_UNUSED_RESULT
4395 parse_CONJUNCTION(const char *arg
, struct ofpbuf
*ofpacts
,
4396 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4403 if (!ovs_scan(arg
, "%"SCNi32
" , %"SCNu8
" / %"SCNu8
" %n",
4404 &id
, &clause
, &n_clauses
, &n
) || n
!= strlen(arg
)) {
4405 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4408 if (n_clauses
< 2) {
4409 return xstrdup("conjunction must have at least 2 clauses");
4410 } else if (n_clauses
> 64) {
4411 return xstrdup("conjunction must have at most 64 clauses");
4412 } else if (clause
< 1) {
4413 return xstrdup("clause index must be positive");
4414 } else if (clause
> n_clauses
) {
4415 return xstrdup("clause index must be less than or equal to "
4416 "number of clauses");
4419 add_conjunction(ofpacts
, id
, clause
- 1, n_clauses
);
4423 /* Action structure for NXAST_MULTIPATH.
4425 * This action performs the following steps in sequence:
4427 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4428 * Refer to the definition of "enum nx_mp_fields" for details.
4430 * The 'basis' value is used as a universal hash parameter, that is,
4431 * different values of 'basis' yield different hash functions. The
4432 * particular universal hash function used is implementation-defined.
4434 * The hashed fields' values are drawn from the current state of the
4435 * flow, including all modifications that have been made by actions up to
4438 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4439 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4442 * The output of the algorithm is 'link', an unsigned integer less than
4443 * or equal to 'max_link'.
4445 * Some algorithms use 'arg' as an additional argument.
4447 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4448 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4451 * The switch will reject actions that have an unknown 'fields', or an unknown
4452 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4453 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4454 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4456 struct nx_action_multipath
{
4457 ovs_be16 type
; /* OFPAT_VENDOR. */
4458 ovs_be16 len
; /* Length is 32. */
4459 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4460 ovs_be16 subtype
; /* NXAST_MULTIPATH. */
4462 /* What fields to hash and how. */
4463 ovs_be16 fields
; /* One of NX_HASH_FIELDS_*. */
4464 ovs_be16 basis
; /* Universal hash parameter. */
4467 /* Multipath link choice algorithm to apply to hash value. */
4468 ovs_be16 algorithm
; /* One of NX_MP_ALG_*. */
4469 ovs_be16 max_link
; /* Number of output links, minus 1. */
4470 ovs_be32 arg
; /* Algorithm-specific argument. */
4473 /* Where to store the result. */
4474 ovs_be16 ofs_nbits
; /* (ofs << 6) | (n_bits - 1). */
4475 ovs_be32 dst
; /* Destination. */
4477 OFP_ASSERT(sizeof(struct nx_action_multipath
) == 32);
4480 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath
*nam
,
4481 enum ofp_version ofp_version OVS_UNUSED
,
4484 uint32_t n_links
= ntohs(nam
->max_link
) + 1;
4485 size_t min_n_bits
= log_2_ceil(n_links
);
4486 struct ofpact_multipath
*mp
;
4488 mp
= ofpact_put_MULTIPATH(out
);
4489 mp
->fields
= ntohs(nam
->fields
);
4490 mp
->basis
= ntohs(nam
->basis
);
4491 mp
->algorithm
= ntohs(nam
->algorithm
);
4492 mp
->max_link
= ntohs(nam
->max_link
);
4493 mp
->arg
= ntohl(nam
->arg
);
4494 mp
->dst
.field
= mf_from_nxm_header(ntohl(nam
->dst
));
4495 mp
->dst
.ofs
= nxm_decode_ofs(nam
->ofs_nbits
);
4496 mp
->dst
.n_bits
= nxm_decode_n_bits(nam
->ofs_nbits
);
4498 if (!flow_hash_fields_valid(mp
->fields
)) {
4499 VLOG_WARN_RL(&rl
, "unsupported fields %d", (int) mp
->fields
);
4500 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4501 } else if (mp
->algorithm
!= NX_MP_ALG_MODULO_N
4502 && mp
->algorithm
!= NX_MP_ALG_HASH_THRESHOLD
4503 && mp
->algorithm
!= NX_MP_ALG_HRW
4504 && mp
->algorithm
!= NX_MP_ALG_ITER_HASH
) {
4505 VLOG_WARN_RL(&rl
, "unsupported algorithm %d", (int) mp
->algorithm
);
4506 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4507 } else if (mp
->dst
.n_bits
< min_n_bits
) {
4508 VLOG_WARN_RL(&rl
, "multipath action requires at least %"PRIuSIZE
" bits for "
4509 "%"PRIu32
" links", min_n_bits
, n_links
);
4510 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4513 return multipath_check(mp
, NULL
);
4517 encode_MULTIPATH(const struct ofpact_multipath
*mp
,
4518 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4520 struct nx_action_multipath
*nam
= put_NXAST_MULTIPATH(out
);
4522 nam
->fields
= htons(mp
->fields
);
4523 nam
->basis
= htons(mp
->basis
);
4524 nam
->algorithm
= htons(mp
->algorithm
);
4525 nam
->max_link
= htons(mp
->max_link
);
4526 nam
->arg
= htonl(mp
->arg
);
4527 nam
->ofs_nbits
= nxm_encode_ofs_nbits(mp
->dst
.ofs
, mp
->dst
.n_bits
);
4528 nam
->dst
= htonl(mf_nxm_header(mp
->dst
.field
->id
));
4531 static char * OVS_WARN_UNUSED_RESULT
4532 parse_MULTIPATH(const char *arg
, struct ofpbuf
*ofpacts
,
4533 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4535 return multipath_parse(ofpact_put_MULTIPATH(ofpacts
), arg
);
4539 format_MULTIPATH(const struct ofpact_multipath
*a
, struct ds
*s
)
4541 multipath_format(a
, s
);
4544 /* Action structure for NXAST_NOTE.
4546 * This action has no effect. It is variable length. The switch does not
4547 * attempt to interpret the user-defined 'note' data in any way. A controller
4548 * can use this action to attach arbitrary metadata to a flow.
4550 * This action might go away in the future.
4552 struct nx_action_note
{
4553 ovs_be16 type
; /* OFPAT_VENDOR. */
4554 ovs_be16 len
; /* A multiple of 8, but at least 16. */
4555 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4556 ovs_be16 subtype
; /* NXAST_NOTE. */
4557 uint8_t note
[6]; /* Start of user-defined data. */
4558 /* Possibly followed by additional user-defined data. */
4560 OFP_ASSERT(sizeof(struct nx_action_note
) == 16);
4563 decode_NXAST_RAW_NOTE(const struct nx_action_note
*nan
,
4564 enum ofp_version ofp_version OVS_UNUSED
,
4567 struct ofpact_note
*note
;
4568 unsigned int length
;
4570 length
= ntohs(nan
->len
) - offsetof(struct nx_action_note
, note
);
4571 note
= ofpact_put_NOTE(out
);
4572 note
->length
= length
;
4573 ofpbuf_put(out
, nan
->note
, length
);
4575 ofpact_finish_NOTE(out
, ¬e
);
4581 encode_NOTE(const struct ofpact_note
*note
,
4582 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4584 size_t start_ofs
= out
->size
;
4585 struct nx_action_note
*nan
;
4587 put_NXAST_NOTE(out
);
4588 out
->size
= out
->size
- sizeof nan
->note
;
4590 ofpbuf_put(out
, note
->data
, note
->length
);
4591 pad_ofpat(out
, start_ofs
);
4594 static char * OVS_WARN_UNUSED_RESULT
4595 parse_NOTE(const char *arg
, struct ofpbuf
*ofpacts
,
4596 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4598 size_t start_ofs
= ofpacts
->size
;
4599 ofpact_put_NOTE(ofpacts
);
4600 arg
= ofpbuf_put_hex(ofpacts
, arg
, NULL
);
4602 return xstrdup("bad hex digit in `note' argument");
4604 struct ofpact_note
*note
= ofpbuf_at_assert(ofpacts
, start_ofs
,
4606 note
->length
= ofpacts
->size
- (start_ofs
+ sizeof *note
);
4607 ofpact_finish_NOTE(ofpacts
, ¬e
);
4612 format_NOTE(const struct ofpact_note
*a
, struct ds
*s
)
4614 ds_put_format(s
, "%snote:%s", colors
.param
, colors
.end
);
4615 format_hex_arg(s
, a
->data
, a
->length
);
4621 decode_NXAST_RAW_EXIT(struct ofpbuf
*out
)
4623 ofpact_put_EXIT(out
);
4628 encode_EXIT(const struct ofpact_null
*null OVS_UNUSED
,
4629 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4631 put_NXAST_EXIT(out
);
4634 static char * OVS_WARN_UNUSED_RESULT
4635 parse_EXIT(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
4636 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4638 ofpact_put_EXIT(ofpacts
);
4643 format_EXIT(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
4645 ds_put_format(s
, "%sexit%s", colors
.special
, colors
.end
);
4648 /* Unroll xlate action. */
4651 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate
*unroll OVS_UNUSED
,
4652 enum ofp_version ofp_version OVS_UNUSED
,
4653 struct ofpbuf
*out OVS_UNUSED
)
4658 static char * OVS_WARN_UNUSED_RESULT
4659 parse_UNROLL_XLATE(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts OVS_UNUSED
,
4660 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4667 format_UNROLL_XLATE(const struct ofpact_unroll_xlate
*a
, struct ds
*s
)
4669 ds_put_format(s
, "%sunroll_xlate(%s%stable=%s%"PRIu8
4670 ", %scookie=%s%"PRIu64
"%s)%s",
4671 colors
.paren
, colors
.end
,
4672 colors
.special
, colors
.end
, a
->rule_table_id
,
4673 colors
.param
, colors
.end
, ntohll(a
->rule_cookie
),
4674 colors
.paren
, colors
.end
);
4677 /* Action structure for NXAST_SAMPLE.
4679 * Samples matching packets with the given probability and sends them
4680 * each to the set of collectors identified with the given ID. The
4681 * probability is expressed as a number of packets to be sampled out
4682 * of USHRT_MAX packets, and must be >0.
4684 * When sending packet samples to IPFIX collectors, the IPFIX flow
4685 * record sent for each sampled packet is associated with the given
4686 * observation domain ID and observation point ID. Each IPFIX flow
4687 * record contain the sampled packet's headers when executing this
4688 * rule. If a sampled packet's headers are modified by previous
4689 * actions in the flow, those modified headers are sent. */
4690 struct nx_action_sample
{
4691 ovs_be16 type
; /* OFPAT_VENDOR. */
4692 ovs_be16 len
; /* Length is 24. */
4693 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4694 ovs_be16 subtype
; /* NXAST_SAMPLE. */
4695 ovs_be16 probability
; /* Fraction of packets to sample. */
4696 ovs_be32 collector_set_id
; /* ID of collector set in OVSDB. */
4697 ovs_be32 obs_domain_id
; /* ID of sampling observation domain. */
4698 ovs_be32 obs_point_id
; /* ID of sampling observation point. */
4700 OFP_ASSERT(sizeof(struct nx_action_sample
) == 24);
4702 /* Action structure for NXAST_SAMPLE2.
4704 * This replacement for NXAST_SAMPLE makes it support exporting
4705 * egress tunnel information. */
4706 struct nx_action_sample2
{
4707 ovs_be16 type
; /* OFPAT_VENDOR. */
4708 ovs_be16 len
; /* Length is 32. */
4709 ovs_be32 vendor
; /* NX_VENDOR_ID. */
4710 ovs_be16 subtype
; /* NXAST_SAMPLE. */
4711 ovs_be16 probability
; /* Fraction of packets to sample. */
4712 ovs_be32 collector_set_id
; /* ID of collector set in OVSDB. */
4713 ovs_be32 obs_domain_id
; /* ID of sampling observation domain. */
4714 ovs_be32 obs_point_id
; /* ID of sampling observation point. */
4715 ovs_be16 sampling_port
; /* Sampling port. */
4716 uint8_t pad
[6]; /* Pad to a multiple of 8 bytes */
4718 OFP_ASSERT(sizeof(struct nx_action_sample2
) == 32);
4721 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample
*nas
,
4722 enum ofp_version ofp_version OVS_UNUSED
,
4725 struct ofpact_sample
*sample
;
4727 sample
= ofpact_put_SAMPLE(out
);
4728 sample
->ofpact
.raw
= NXAST_RAW_SAMPLE
;
4729 sample
->probability
= ntohs(nas
->probability
);
4730 sample
->collector_set_id
= ntohl(nas
->collector_set_id
);
4731 sample
->obs_domain_id
= ntohl(nas
->obs_domain_id
);
4732 sample
->obs_point_id
= ntohl(nas
->obs_point_id
);
4733 /* Default value for sampling port is OFPP_NONE */
4734 sample
->sampling_port
= OFPP_NONE
;
4736 if (sample
->probability
== 0) {
4737 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4744 decode_NXAST_RAW_SAMPLE2(const struct nx_action_sample2
*nas
,
4745 enum ofp_version ofp_version OVS_UNUSED
,
4748 struct ofpact_sample
*sample
;
4750 sample
= ofpact_put_SAMPLE(out
);
4751 sample
->ofpact
.raw
= NXAST_RAW_SAMPLE2
;
4752 sample
->probability
= ntohs(nas
->probability
);
4753 sample
->collector_set_id
= ntohl(nas
->collector_set_id
);
4754 sample
->obs_domain_id
= ntohl(nas
->obs_domain_id
);
4755 sample
->obs_point_id
= ntohl(nas
->obs_point_id
);
4756 sample
->sampling_port
= u16_to_ofp(ntohs(nas
->sampling_port
));
4758 if (sample
->probability
== 0) {
4759 return OFPERR_OFPBAC_BAD_ARGUMENT
;
4766 encode_SAMPLE(const struct ofpact_sample
*sample
,
4767 enum ofp_version ofp_version OVS_UNUSED
, struct ofpbuf
*out
)
4769 if (sample
->ofpact
.raw
== NXAST_RAW_SAMPLE2
4770 || sample
->sampling_port
!= OFPP_NONE
) {
4771 struct nx_action_sample2
*nas
= put_NXAST_SAMPLE2(out
);
4772 nas
->probability
= htons(sample
->probability
);
4773 nas
->collector_set_id
= htonl(sample
->collector_set_id
);
4774 nas
->obs_domain_id
= htonl(sample
->obs_domain_id
);
4775 nas
->obs_point_id
= htonl(sample
->obs_point_id
);
4776 nas
->sampling_port
= htons(ofp_to_u16(sample
->sampling_port
));
4778 struct nx_action_sample
*nas
= put_NXAST_SAMPLE(out
);
4779 nas
->probability
= htons(sample
->probability
);
4780 nas
->collector_set_id
= htonl(sample
->collector_set_id
);
4781 nas
->obs_domain_id
= htonl(sample
->obs_domain_id
);
4782 nas
->obs_point_id
= htonl(sample
->obs_point_id
);
4786 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4787 * action to 'ofpacts'.
4789 * Returns NULL if successful, otherwise a malloc()'d string describing the
4790 * error. The caller is responsible for freeing the returned string. */
4791 static char * OVS_WARN_UNUSED_RESULT
4792 parse_SAMPLE(char *arg
, struct ofpbuf
*ofpacts
,
4793 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4795 struct ofpact_sample
*os
= ofpact_put_SAMPLE(ofpacts
);
4796 os
->sampling_port
= OFPP_NONE
;
4799 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
4802 if (!strcmp(key
, "probability")) {
4803 error
= str_to_u16(value
, "probability", &os
->probability
);
4804 if (!error
&& os
->probability
== 0) {
4805 error
= xasprintf("invalid probability value \"%s\"", value
);
4807 } else if (!strcmp(key
, "collector_set_id")) {
4808 error
= str_to_u32(value
, &os
->collector_set_id
);
4809 } else if (!strcmp(key
, "obs_domain_id")) {
4810 error
= str_to_u32(value
, &os
->obs_domain_id
);
4811 } else if (!strcmp(key
, "obs_point_id")) {
4812 error
= str_to_u32(value
, &os
->obs_point_id
);
4813 } else if (!strcmp(key
, "sampling_port")) {
4814 if (!ofputil_port_from_string(value
, &os
->sampling_port
)) {
4815 error
= xasprintf("%s: unknown port", value
);
4818 error
= xasprintf("invalid key \"%s\" in \"sample\" argument",
4825 if (os
->probability
== 0) {
4826 return xstrdup("non-zero \"probability\" must be specified on sample");
4833 format_SAMPLE(const struct ofpact_sample
*a
, struct ds
*s
)
4835 ds_put_format(s
, "%ssample(%s%sprobability=%s%"PRIu16
4836 ",%scollector_set_id=%s%"PRIu32
4837 ",%sobs_domain_id=%s%"PRIu32
4838 ",%sobs_point_id=%s%"PRIu32
,
4839 colors
.paren
, colors
.end
,
4840 colors
.param
, colors
.end
, a
->probability
,
4841 colors
.param
, colors
.end
, a
->collector_set_id
,
4842 colors
.param
, colors
.end
, a
->obs_domain_id
,
4843 colors
.param
, colors
.end
, a
->obs_point_id
);
4844 if (a
->sampling_port
!= OFPP_NONE
) {
4845 ds_put_format(s
, ",%ssampling_port=%s%"PRIu16
,
4846 colors
.param
, colors
.end
, a
->sampling_port
);
4848 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
4851 /* debug_recirc instruction. */
4853 static bool enable_debug
;
4856 ofpact_dummy_enable(void)
4858 enable_debug
= true;
4862 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf
*out
)
4864 if (!enable_debug
) {
4865 return OFPERR_OFPBAC_BAD_VENDOR_TYPE
;
4868 ofpact_put_DEBUG_RECIRC(out
);
4873 encode_DEBUG_RECIRC(const struct ofpact_null
*n OVS_UNUSED
,
4874 enum ofp_version ofp_version OVS_UNUSED
,
4877 put_NXAST_DEBUG_RECIRC(out
);
4880 static char * OVS_WARN_UNUSED_RESULT
4881 parse_DEBUG_RECIRC(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
4882 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
4884 ofpact_put_DEBUG_RECIRC(ofpacts
);
4889 format_DEBUG_RECIRC(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
4891 ds_put_format(s
, "%sdebug_recirc%s", colors
.value
, colors
.end
);
4894 /* Action structure for NXAST_CT.
4896 * Pass traffic to the connection tracker.
4898 * There are two important concepts to understanding the connection tracking
4899 * interface: Packet state and Connection state. Packets may be "Untracked" or
4900 * "Tracked". Connections may be "Uncommitted" or "Committed".
4904 * Untracked packets have not yet passed through the connection tracker,
4905 * and the connection state for such packets is unknown. In most cases,
4906 * packets entering the OpenFlow pipeline will initially be in the
4907 * untracked state. Untracked packets may become tracked by executing
4908 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4909 * about the connection available, in particular the connection state.
4911 * Tracked packets have previously passed through the connection tracker.
4912 * These packets will remain tracked through until the end of the OpenFlow
4913 * pipeline. Tracked packets which have NXAST_CT executed with a
4914 * "recirc_table" specified will return to the tracked state.
4916 * The packet state is only significant for the duration of packet
4917 * processing within the OpenFlow pipeline.
4919 * - Connection State:
4921 * Multiple packets may be associated with a single connection. Initially,
4922 * all connections are uncommitted. The connection state corresponding to
4923 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4925 * Uncommitted connections have no state stored about them. Uncommitted
4926 * connections may transition into the committed state by executing
4927 * NXAST_CT with the NX_CT_F_COMMIT flag.
4929 * Once a connection becomes committed, information may be gathered about
4930 * the connection by passing subsequent packets through the connection
4931 * tracker, and the state of the connection will be stored beyond the
4932 * lifetime of packet processing.
4934 * Connections may transition back into the uncommitted state due to
4935 * external timers, or due to the contents of packets that are sent to the
4936 * connection tracker. This behaviour is outside of the scope of the
4937 * OpenFlow interface.
4939 * The "zone" specifies a context within which the tracking is done:
4941 * The connection tracking zone is a 16-bit number. Each zone is an
4942 * independent connection tracking context. The connection state for each
4943 * connection is completely separate for each zone, so if a connection
4944 * is committed to zone A, then it will remain uncommitted in zone B.
4945 * If NXAST_CT is executed with the same zone multiple times, later
4946 * executions have no effect.
4948 * If 'zone_src' is nonzero, this specifies that the zone should be
4949 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4950 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4951 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4952 * are the same as the acceptable nxm_header values for the 'src' field of
4955 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4956 * connection tracking zone.
4958 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4960 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4961 * packet will be logically cloned prior to executing this action. One
4962 * copy will be sent to the connection tracker, then will be re-injected
4963 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
4964 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4965 * fields will be populated. The original instance of the packet will
4966 * continue the current actions list. This can be thought of as similar to
4967 * the effect of the "output" action: One copy is sent out (in this case,
4968 * to the connection tracker), but the current copy continues processing.
4970 * It is strongly recommended that this table is later than the current
4971 * table, to prevent loops.
4973 * The "alg" attaches protocol-specific behaviour to this action:
4975 * The ALG is a 16-bit number which specifies that additional
4976 * processing should be applied to this traffic.
4978 * Protocol | Value | Meaning
4979 * --------------------------------------------------------------------
4980 * None | 0 | No protocol-specific behaviour.
4981 * FTP | 21 | Parse FTP control connections and observe the
4982 * | | negotiation of related data connections.
4983 * Other | Other | Unsupported protocols.
4985 * By way of example, if FTP control connections have this action applied
4986 * with the ALG set to FTP (21), then the connection tracker will observe
4987 * the negotiation of data connections. This allows the connection
4988 * tracker to identify subsequent data connections as "related" to this
4989 * existing connection. The "related" flag will be populated in the
4990 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
4993 * Zero or more actions may immediately follow this action. These actions will
4994 * be executed within the context of the connection tracker, and they require
4995 * the NX_CT_F_COMMIT flag to be set.
4997 struct nx_action_conntrack
{
4998 ovs_be16 type
; /* OFPAT_VENDOR. */
4999 ovs_be16 len
; /* At least 24. */
5000 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5001 ovs_be16 subtype
; /* NXAST_CT. */
5002 ovs_be16 flags
; /* Zero or more NX_CT_F_* flags.
5003 * Unspecified flag bits must be zero. */
5004 ovs_be32 zone_src
; /* Connection tracking context. */
5006 ovs_be16 zone_ofs_nbits
;/* Range to use from source field. */
5007 ovs_be16 zone_imm
; /* Immediate value for zone. */
5009 uint8_t recirc_table
; /* Recirculate to a specific table, or
5010 NX_CT_RECIRC_NONE for no recirculation. */
5011 uint8_t pad
[3]; /* Zeroes */
5012 ovs_be16 alg
; /* Well-known port number for the protocol.
5013 * 0 indicates no ALG is required. */
5014 /* Followed by a sequence of zero or more OpenFlow actions. The length of
5015 * these is included in 'len'. */
5017 OFP_ASSERT(sizeof(struct nx_action_conntrack
) == 24);
5020 decode_ct_zone(const struct nx_action_conntrack
*nac
,
5021 struct ofpact_conntrack
*out
)
5023 if (nac
->zone_src
) {
5026 out
->zone_src
.field
= mf_from_nxm_header(ntohl(nac
->zone_src
));
5027 out
->zone_src
.ofs
= nxm_decode_ofs(nac
->zone_ofs_nbits
);
5028 out
->zone_src
.n_bits
= nxm_decode_n_bits(nac
->zone_ofs_nbits
);
5029 error
= mf_check_src(&out
->zone_src
, NULL
);
5034 if (out
->zone_src
.n_bits
!= 16) {
5035 VLOG_WARN_RL(&rl
, "zone n_bits %d not within valid range [16..16]",
5036 out
->zone_src
.n_bits
);
5037 return OFPERR_OFPBAC_BAD_SET_LEN
;
5040 out
->zone_src
.field
= NULL
;
5041 out
->zone_imm
= ntohs(nac
->zone_imm
);
5048 decode_NXAST_RAW_CT(const struct nx_action_conntrack
*nac
,
5049 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5051 const size_t ct_offset
= ofpacts_pull(out
);
5052 struct ofpact_conntrack
*conntrack
= ofpact_put_CT(out
);
5053 conntrack
->flags
= ntohs(nac
->flags
);
5055 int error
= decode_ct_zone(nac
, conntrack
);
5059 conntrack
->recirc_table
= nac
->recirc_table
;
5060 conntrack
->alg
= ntohs(nac
->alg
);
5062 ofpbuf_pull(out
, sizeof(*conntrack
));
5064 struct ofpbuf openflow
= ofpbuf_const_initializer(
5065 nac
+ 1, ntohs(nac
->len
) - sizeof(*nac
));
5066 error
= ofpacts_pull_openflow_actions__(&openflow
, openflow
.size
,
5068 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
5074 conntrack
= ofpbuf_push_uninit(out
, sizeof(*conntrack
));
5075 out
->header
= &conntrack
->ofpact
;
5076 ofpact_finish_CT(out
, &conntrack
);
5078 if (conntrack
->ofpact
.len
> sizeof(*conntrack
)
5079 && !(conntrack
->flags
& NX_CT_F_COMMIT
)) {
5080 const struct ofpact
*a
;
5081 size_t ofpacts_len
= conntrack
->ofpact
.len
- sizeof(*conntrack
);
5083 OFPACT_FOR_EACH (a
, conntrack
->actions
, ofpacts_len
) {
5084 if (a
->type
!= OFPACT_NAT
|| ofpact_get_NAT(a
)->flags
5085 || ofpact_get_NAT(a
)->range_af
!= AF_UNSPEC
) {
5086 VLOG_WARN_RL(&rl
, "CT action requires commit flag if actions "
5087 "other than NAT without arguments are specified.");
5088 error
= OFPERR_OFPBAC_BAD_ARGUMENT
;
5095 ofpbuf_push_uninit(out
, ct_offset
);
5100 encode_CT(const struct ofpact_conntrack
*conntrack
,
5101 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5103 struct nx_action_conntrack
*nac
;
5104 const size_t ofs
= out
->size
;
5107 nac
= put_NXAST_CT(out
);
5108 nac
->flags
= htons(conntrack
->flags
);
5109 if (conntrack
->zone_src
.field
) {
5110 nac
->zone_src
= htonl(mf_nxm_header(conntrack
->zone_src
.field
->id
));
5111 nac
->zone_ofs_nbits
= nxm_encode_ofs_nbits(conntrack
->zone_src
.ofs
,
5112 conntrack
->zone_src
.n_bits
);
5114 nac
->zone_src
= htonl(0);
5115 nac
->zone_imm
= htons(conntrack
->zone_imm
);
5117 nac
->recirc_table
= conntrack
->recirc_table
;
5118 nac
->alg
= htons(conntrack
->alg
);
5120 len
= ofpacts_put_openflow_actions(conntrack
->actions
,
5121 ofpact_ct_get_action_len(conntrack
),
5123 len
+= sizeof(*nac
);
5124 nac
= ofpbuf_at(out
, ofs
, sizeof(*nac
));
5125 nac
->len
= htons(len
);
5128 static char * OVS_WARN_UNUSED_RESULT
parse_NAT(char *arg
, struct ofpbuf
*,
5129 enum ofputil_protocol
* OVS_UNUSED
);
5131 /* Parses 'arg' as the argument to a "ct" action, and appends such an
5132 * action to 'ofpacts'.
5134 * Returns NULL if successful, otherwise a malloc()'d string describing the
5135 * error. The caller is responsible for freeing the returned string. */
5136 static char * OVS_WARN_UNUSED_RESULT
5137 parse_CT(char *arg
, struct ofpbuf
*ofpacts
,
5138 enum ofputil_protocol
*usable_protocols
)
5140 const size_t ct_offset
= ofpacts_pull(ofpacts
);
5141 struct ofpact_conntrack
*oc
;
5145 oc
= ofpact_put_CT(ofpacts
);
5147 oc
->recirc_table
= NX_CT_RECIRC_NONE
;
5148 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
5149 if (!strcmp(key
, "commit")) {
5150 oc
->flags
|= NX_CT_F_COMMIT
;
5151 } else if (!strcmp(key
, "table")) {
5152 error
= str_to_u8(value
, "recirc_table", &oc
->recirc_table
);
5153 if (!error
&& oc
->recirc_table
== NX_CT_RECIRC_NONE
) {
5154 error
= xasprintf("invalid table %#"PRIx16
, oc
->recirc_table
);
5156 } else if (!strcmp(key
, "zone")) {
5157 error
= str_to_u16(value
, "zone", &oc
->zone_imm
);
5161 error
= mf_parse_subfield(&oc
->zone_src
, value
);
5166 } else if (!strcmp(key
, "alg")) {
5167 error
= str_to_connhelper(value
, &oc
->alg
);
5168 } else if (!strcmp(key
, "nat")) {
5169 const size_t nat_offset
= ofpacts_pull(ofpacts
);
5171 error
= parse_NAT(value
, ofpacts
, usable_protocols
);
5172 /* Update CT action pointer and length. */
5173 ofpacts
->header
= ofpbuf_push_uninit(ofpacts
, nat_offset
);
5174 oc
= ofpacts
->header
;
5175 } else if (!strcmp(key
, "exec")) {
5176 /* Hide existing actions from ofpacts_parse_copy(), so the
5177 * nesting can be handled transparently. */
5178 enum ofputil_protocol usable_protocols2
;
5179 const size_t exec_offset
= ofpacts_pull(ofpacts
);
5181 /* Initializes 'usable_protocol2', fold it back to
5182 * '*usable_protocols' afterwards, so that we do not lose
5183 * restrictions already in there. */
5184 error
= ofpacts_parse_copy(value
, ofpacts
, &usable_protocols2
,
5186 *usable_protocols
&= usable_protocols2
;
5187 ofpacts
->header
= ofpbuf_push_uninit(ofpacts
, exec_offset
);
5188 oc
= ofpacts
->header
;
5190 error
= xasprintf("invalid argument to \"ct\" action: `%s'", key
);
5197 ofpact_finish_CT(ofpacts
, &oc
);
5198 ofpbuf_push_uninit(ofpacts
, ct_offset
);
5203 format_alg(int port
, struct ds
*s
)
5205 if (port
== IPPORT_FTP
) {
5206 ds_put_format(s
, "%salg=%sftp,", colors
.param
, colors
.end
);
5208 ds_put_format(s
, "%salg=%s%d,", colors
.param
, colors
.end
, port
);
5212 static void format_NAT(const struct ofpact_nat
*a
, struct ds
*ds
);
5215 format_CT(const struct ofpact_conntrack
*a
, struct ds
*s
)
5217 ds_put_format(s
, "%sct(%s", colors
.paren
, colors
.end
);
5218 if (a
->flags
& NX_CT_F_COMMIT
) {
5219 ds_put_format(s
, "%scommit%s,", colors
.value
, colors
.end
);
5221 if (a
->recirc_table
!= NX_CT_RECIRC_NONE
) {
5222 ds_put_format(s
, "%stable=%s%"PRIu8
",",
5223 colors
.special
, colors
.end
, a
->recirc_table
);
5225 if (a
->zone_src
.field
) {
5226 ds_put_format(s
, "%szone=%s", colors
.param
, colors
.end
);
5227 mf_format_subfield(&a
->zone_src
, s
);
5228 ds_put_char(s
, ',');
5229 } else if (a
->zone_imm
) {
5230 ds_put_format(s
, "%szone=%s%"PRIu16
",",
5231 colors
.param
, colors
.end
, a
->zone_imm
);
5233 /* If the first action is a NAT action, format it outside of the 'exec'
5235 const struct ofpact
*action
= a
->actions
;
5236 size_t actions_len
= ofpact_ct_get_action_len(a
);
5237 if (actions_len
&& action
->type
== OFPACT_NAT
) {
5238 format_NAT(ofpact_get_NAT(action
), s
);
5239 ds_put_char(s
, ',');
5240 actions_len
-= OFPACT_ALIGN(action
->len
);
5241 action
= ofpact_next(action
);
5244 ds_put_format(s
, "%sexec(%s", colors
.paren
, colors
.end
);
5245 ofpacts_format(action
, actions_len
, s
);
5246 ds_put_format(s
, "%s),%s", colors
.paren
, colors
.end
);
5248 format_alg(a
->alg
, s
);
5250 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
5255 /* Which optional fields are present? */
5257 NX_NAT_RANGE_IPV4_MIN
= 1 << 0, /* ovs_be32 */
5258 NX_NAT_RANGE_IPV4_MAX
= 1 << 1, /* ovs_be32 */
5259 NX_NAT_RANGE_IPV6_MIN
= 1 << 2, /* struct in6_addr */
5260 NX_NAT_RANGE_IPV6_MAX
= 1 << 3, /* struct in6_addr */
5261 NX_NAT_RANGE_PROTO_MIN
= 1 << 4, /* ovs_be16 */
5262 NX_NAT_RANGE_PROTO_MAX
= 1 << 5, /* ovs_be16 */
5265 /* Action structure for NXAST_NAT. */
5266 struct nx_action_nat
{
5267 ovs_be16 type
; /* OFPAT_VENDOR. */
5268 ovs_be16 len
; /* At least 16. */
5269 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5270 ovs_be16 subtype
; /* NXAST_NAT. */
5271 uint8_t pad
[2]; /* Must be zero. */
5272 ovs_be16 flags
; /* Zero or more NX_NAT_F_* flags.
5273 * Unspecified flag bits must be zero. */
5274 ovs_be16 range_present
; /* NX_NAT_RANGE_* */
5275 /* Followed by optional parameters as specified by 'range_present' */
5277 OFP_ASSERT(sizeof(struct nx_action_nat
) == 16);
5280 encode_NAT(const struct ofpact_nat
*nat
,
5281 enum ofp_version ofp_version OVS_UNUSED
,
5284 struct nx_action_nat
*nan
;
5285 const size_t ofs
= out
->size
;
5286 uint16_t range_present
= 0;
5288 nan
= put_NXAST_NAT(out
);
5289 nan
->flags
= htons(nat
->flags
);
5290 if (nat
->range_af
== AF_INET
) {
5291 if (nat
->range
.addr
.ipv4
.min
) {
5292 ovs_be32
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
5293 *min
= nat
->range
.addr
.ipv4
.min
;
5294 range_present
|= NX_NAT_RANGE_IPV4_MIN
;
5296 if (nat
->range
.addr
.ipv4
.max
) {
5297 ovs_be32
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
5298 *max
= nat
->range
.addr
.ipv4
.max
;
5299 range_present
|= NX_NAT_RANGE_IPV4_MAX
;
5301 } else if (nat
->range_af
== AF_INET6
) {
5302 if (!ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.min
)) {
5303 struct in6_addr
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
5304 *min
= nat
->range
.addr
.ipv6
.min
;
5305 range_present
|= NX_NAT_RANGE_IPV6_MIN
;
5307 if (!ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.max
)) {
5308 struct in6_addr
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
5309 *max
= nat
->range
.addr
.ipv6
.max
;
5310 range_present
|= NX_NAT_RANGE_IPV6_MAX
;
5313 if (nat
->range_af
!= AF_UNSPEC
) {
5314 if (nat
->range
.proto
.min
) {
5315 ovs_be16
*min
= ofpbuf_put_uninit(out
, sizeof *min
);
5316 *min
= htons(nat
->range
.proto
.min
);
5317 range_present
|= NX_NAT_RANGE_PROTO_MIN
;
5319 if (nat
->range
.proto
.max
) {
5320 ovs_be16
*max
= ofpbuf_put_uninit(out
, sizeof *max
);
5321 *max
= htons(nat
->range
.proto
.max
);
5322 range_present
|= NX_NAT_RANGE_PROTO_MAX
;
5325 pad_ofpat(out
, ofs
);
5326 nan
= ofpbuf_at(out
, ofs
, sizeof *nan
);
5327 nan
->range_present
= htons(range_present
);
5331 decode_NXAST_RAW_NAT(const struct nx_action_nat
*nan
,
5332 enum ofp_version ofp_version OVS_UNUSED
,
5335 struct ofpact_nat
*nat
;
5336 uint16_t range_present
= ntohs(nan
->range_present
);
5337 const char *opts
= (char *)(nan
+ 1);
5338 uint16_t len
= ntohs(nan
->len
) - sizeof *nan
;
5340 nat
= ofpact_put_NAT(out
);
5341 nat
->flags
= ntohs(nan
->flags
);
5343 /* Check for unknown or mutually exclusive flags. */
5344 if ((nat
->flags
& ~NX_NAT_F_MASK
)
5345 || (nat
->flags
& NX_NAT_F_SRC
&& nat
->flags
& NX_NAT_F_DST
)
5346 || (nat
->flags
& NX_NAT_F_PROTO_HASH
5347 && nat
->flags
& NX_NAT_F_PROTO_RANDOM
)) {
5348 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5351 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
5352 (LEN >= sizeof(TYPE) \
5353 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
5354 SRC += sizeof(TYPE)) \
5357 nat
->range_af
= AF_UNSPEC
;
5358 if (range_present
& NX_NAT_RANGE_IPV4_MIN
) {
5359 if (range_present
& (NX_NAT_RANGE_IPV6_MIN
| NX_NAT_RANGE_IPV6_MAX
)) {
5360 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5363 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv4
.min
, opts
, len
, ovs_be32
)
5364 || !nat
->range
.addr
.ipv4
.min
) {
5365 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5368 nat
->range_af
= AF_INET
;
5370 if (range_present
& NX_NAT_RANGE_IPV4_MAX
) {
5371 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv4
.max
, opts
, len
,
5373 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5375 if (ntohl(nat
->range
.addr
.ipv4
.max
)
5376 < ntohl(nat
->range
.addr
.ipv4
.min
)) {
5377 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5380 } else if (range_present
& NX_NAT_RANGE_IPV4_MAX
) {
5381 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5382 } else if (range_present
& NX_NAT_RANGE_IPV6_MIN
) {
5383 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv6
.min
, opts
, len
,
5385 || ipv6_mask_is_any(&nat
->range
.addr
.ipv6
.min
)) {
5386 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5389 nat
->range_af
= AF_INET6
;
5391 if (range_present
& NX_NAT_RANGE_IPV6_MAX
) {
5392 if (!NX_NAT_GET_OPT(&nat
->range
.addr
.ipv6
.max
, opts
, len
,
5394 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5396 if (memcmp(&nat
->range
.addr
.ipv6
.max
, &nat
->range
.addr
.ipv6
.min
,
5397 sizeof(struct in6_addr
)) < 0) {
5398 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5401 } else if (range_present
& NX_NAT_RANGE_IPV6_MAX
) {
5402 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5405 if (range_present
& NX_NAT_RANGE_PROTO_MIN
) {
5408 if (nat
->range_af
== AF_UNSPEC
) {
5409 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5411 if (!NX_NAT_GET_OPT(&proto
, opts
, len
, ovs_be16
) || proto
== 0) {
5412 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5414 nat
->range
.proto
.min
= ntohs(proto
);
5415 if (range_present
& NX_NAT_RANGE_PROTO_MAX
) {
5416 if (!NX_NAT_GET_OPT(&proto
, opts
, len
, ovs_be16
)) {
5417 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5419 nat
->range
.proto
.max
= ntohs(proto
);
5420 if (nat
->range
.proto
.max
< nat
->range
.proto
.min
) {
5421 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5424 } else if (range_present
& NX_NAT_RANGE_PROTO_MAX
) {
5425 return OFPERR_OFPBAC_BAD_ARGUMENT
;
5432 format_NAT(const struct ofpact_nat
*a
, struct ds
*ds
)
5434 ds_put_format(ds
, "%snat%s", colors
.paren
, colors
.end
);
5436 if (a
->flags
& (NX_NAT_F_SRC
| NX_NAT_F_DST
)) {
5437 ds_put_format(ds
, "%s(%s", colors
.paren
, colors
.end
);
5438 ds_put_format(ds
, a
->flags
& NX_NAT_F_SRC
? "%ssrc%s" : "%sdst%s",
5439 colors
.param
, colors
.end
);
5441 if (a
->range_af
!= AF_UNSPEC
) {
5442 ds_put_format(ds
, "%s=%s", colors
.param
, colors
.end
);
5444 if (a
->range_af
== AF_INET
) {
5445 ds_put_format(ds
, IP_FMT
, IP_ARGS(a
->range
.addr
.ipv4
.min
));
5447 if (a
->range
.addr
.ipv4
.max
5448 && a
->range
.addr
.ipv4
.max
!= a
->range
.addr
.ipv4
.min
) {
5449 ds_put_format(ds
, "-"IP_FMT
,
5450 IP_ARGS(a
->range
.addr
.ipv4
.max
));
5452 } else if (a
->range_af
== AF_INET6
) {
5453 ipv6_format_addr_bracket(&a
->range
.addr
.ipv6
.min
, ds
,
5454 a
->range
.proto
.min
);
5456 if (!ipv6_mask_is_any(&a
->range
.addr
.ipv6
.max
)
5457 && memcmp(&a
->range
.addr
.ipv6
.max
, &a
->range
.addr
.ipv6
.min
,
5458 sizeof(struct in6_addr
)) != 0) {
5459 ds_put_char(ds
, '-');
5460 ipv6_format_addr_bracket(&a
->range
.addr
.ipv6
.max
, ds
,
5461 a
->range
.proto
.min
);
5464 if (a
->range
.proto
.min
) {
5465 ds_put_char(ds
, ':');
5466 ds_put_format(ds
, "%"PRIu16
, a
->range
.proto
.min
);
5468 if (a
->range
.proto
.max
5469 && a
->range
.proto
.max
!= a
->range
.proto
.min
) {
5470 ds_put_format(ds
, "-%"PRIu16
, a
->range
.proto
.max
);
5473 ds_put_char(ds
, ',');
5475 if (a
->flags
& NX_NAT_F_PERSISTENT
) {
5476 ds_put_format(ds
, "%spersistent%s,",
5477 colors
.value
, colors
.end
);
5479 if (a
->flags
& NX_NAT_F_PROTO_HASH
) {
5480 ds_put_format(ds
, "%shash%s,", colors
.value
, colors
.end
);
5482 if (a
->flags
& NX_NAT_F_PROTO_RANDOM
) {
5483 ds_put_format(ds
, "%srandom%s,", colors
.value
, colors
.end
);
5487 ds_put_format(ds
, "%s)%s", colors
.paren
, colors
.end
);
5491 static char * OVS_WARN_UNUSED_RESULT
5492 str_to_nat_range(const char *s
, struct ofpact_nat
*on
)
5494 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
5497 on
->range_af
= AF_UNSPEC
;
5498 if (ovs_scan_len(s
, &n
, IP_SCAN_FMT
,
5499 IP_SCAN_ARGS(&on
->range
.addr
.ipv4
.min
))) {
5500 on
->range_af
= AF_INET
;
5504 if (!ovs_scan_len(s
, &n
, IP_SCAN_FMT
,
5505 IP_SCAN_ARGS(&on
->range
.addr
.ipv4
.max
))
5506 || (ntohl(on
->range
.addr
.ipv4
.max
)
5507 < ntohl(on
->range
.addr
.ipv4
.min
))) {
5511 } else if ((ovs_scan_len(s
, &n
, IPV6_SCAN_FMT
, ipv6_s
)
5512 || ovs_scan_len(s
, &n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
5513 && inet_pton(AF_INET6
, ipv6_s
, &on
->range
.addr
.ipv6
.min
) == 1) {
5514 on
->range_af
= AF_INET6
;
5518 if (!(ovs_scan_len(s
, &n
, IPV6_SCAN_FMT
, ipv6_s
)
5519 || ovs_scan_len(s
, &n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
5520 || inet_pton(AF_INET6
, ipv6_s
, &on
->range
.addr
.ipv6
.max
) != 1
5521 || memcmp(&on
->range
.addr
.ipv6
.max
, &on
->range
.addr
.ipv6
.min
,
5522 sizeof on
->range
.addr
.ipv6
.max
) < 0) {
5527 if (on
->range_af
!= AF_UNSPEC
&& s
[n
] == ':') {
5529 if (!ovs_scan_len(s
, &n
, "%"SCNu16
, &on
->range
.proto
.min
)) {
5534 if (!ovs_scan_len(s
, &n
, "%"SCNu16
, &on
->range
.proto
.max
)
5535 || on
->range
.proto
.max
< on
->range
.proto
.min
) {
5540 if (strlen(s
) != n
) {
5541 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
5546 return xasprintf("invalid nat range \"%s\"", s
);
5550 /* Parses 'arg' as the argument to a "nat" action, and appends such an
5551 * action to 'ofpacts'.
5553 * Returns NULL if successful, otherwise a malloc()'d string describing the
5554 * error. The caller is responsible for freeing the returned string. */
5555 static char * OVS_WARN_UNUSED_RESULT
5556 parse_NAT(char *arg
, struct ofpbuf
*ofpacts
,
5557 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
5559 struct ofpact_nat
*on
= ofpact_put_NAT(ofpacts
);
5563 on
->range_af
= AF_UNSPEC
;
5565 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
5568 if (!strcmp(key
, "src")) {
5569 on
->flags
|= NX_NAT_F_SRC
;
5570 error
= str_to_nat_range(value
, on
);
5571 } else if (!strcmp(key
, "dst")) {
5572 on
->flags
|= NX_NAT_F_DST
;
5573 error
= str_to_nat_range(value
, on
);
5574 } else if (!strcmp(key
, "persistent")) {
5575 on
->flags
|= NX_NAT_F_PERSISTENT
;
5576 } else if (!strcmp(key
, "hash")) {
5577 on
->flags
|= NX_NAT_F_PROTO_HASH
;
5578 } else if (!strcmp(key
, "random")) {
5579 on
->flags
|= NX_NAT_F_PROTO_RANDOM
;
5581 error
= xasprintf("invalid key \"%s\" in \"nat\" argument",
5588 if (on
->flags
& NX_NAT_F_SRC
&& on
->flags
& NX_NAT_F_DST
) {
5589 return xasprintf("May only specify one of \"src\" or \"dst\".");
5591 if (!(on
->flags
& NX_NAT_F_SRC
|| on
->flags
& NX_NAT_F_DST
)) {
5593 return xasprintf("Flags allowed only with \"src\" or \"dst\".");
5595 if (on
->range_af
!= AF_UNSPEC
) {
5596 return xasprintf("Range allowed only with \"src\" or \"dst\".");
5599 if (on
->flags
& NX_NAT_F_PROTO_HASH
&& on
->flags
& NX_NAT_F_PROTO_RANDOM
) {
5600 return xasprintf("Both \"hash\" and \"random\" are not allowed.");
5607 /* Meter instruction. */
5610 encode_METER(const struct ofpact_meter
*meter
,
5611 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5613 if (ofp_version
>= OFP13_VERSION
) {
5614 instruction_put_OFPIT13_METER(out
)->meter_id
= htonl(meter
->meter_id
);
5618 static char * OVS_WARN_UNUSED_RESULT
5619 parse_METER(char *arg
, struct ofpbuf
*ofpacts
,
5620 enum ofputil_protocol
*usable_protocols
)
5622 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
5623 return str_to_u32(arg
, &ofpact_put_METER(ofpacts
)->meter_id
);
5627 format_METER(const struct ofpact_meter
*a
, struct ds
*s
)
5629 ds_put_format(s
, "%smeter:%s%"PRIu32
,
5630 colors
.param
, colors
.end
, a
->meter_id
);
5633 /* Clear-Actions instruction. */
5636 encode_CLEAR_ACTIONS(const struct ofpact_null
*null OVS_UNUSED
,
5637 enum ofp_version ofp_version OVS_UNUSED
,
5638 struct ofpbuf
*out OVS_UNUSED
)
5640 if (ofp_version
> OFP10_VERSION
) {
5641 instruction_put_OFPIT11_CLEAR_ACTIONS(out
);
5645 static char * OVS_WARN_UNUSED_RESULT
5646 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED
, struct ofpbuf
*ofpacts
,
5647 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
5649 ofpact_put_CLEAR_ACTIONS(ofpacts
);
5654 format_CLEAR_ACTIONS(const struct ofpact_null
*a OVS_UNUSED
, struct ds
*s
)
5656 ds_put_format(s
, "%sclear_actions%s", colors
.value
, colors
.end
);
5659 /* Write-Actions instruction. */
5662 encode_WRITE_ACTIONS(const struct ofpact_nest
*actions
,
5663 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5665 if (ofp_version
> OFP10_VERSION
) {
5666 const size_t ofs
= out
->size
;
5668 instruction_put_OFPIT11_WRITE_ACTIONS(out
);
5669 ofpacts_put_openflow_actions(actions
->actions
,
5670 ofpact_nest_get_action_len(actions
),
5672 ofpacts_update_instruction_actions(out
, ofs
);
5676 static char * OVS_WARN_UNUSED_RESULT
5677 parse_WRITE_ACTIONS(char *arg
, struct ofpbuf
*ofpacts
,
5678 enum ofputil_protocol
*usable_protocols
)
5680 size_t ofs
= ofpacts_pull(ofpacts
);
5681 struct ofpact_nest
*on
;
5684 /* Add a Write-Actions instruction and then pull it off. */
5685 ofpact_put(ofpacts
, OFPACT_WRITE_ACTIONS
, sizeof *on
);
5686 ofpbuf_pull(ofpacts
, sizeof *on
);
5688 /* Parse nested actions.
5690 * We pulled off "write-actions" and the previous actions because the
5691 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
5692 * that it doesn't actually include the nested actions. That means that
5693 * ofpacts_parse() would reject them as being part of an Apply-Actions that
5694 * follows a Write-Actions, which is an invalid order. */
5695 error
= ofpacts_parse(arg
, ofpacts
, usable_protocols
, false,
5696 OFPACT_WRITE_ACTIONS
);
5698 /* Put the Write-Actions back on and update its length. */
5699 on
= ofpbuf_push_uninit(ofpacts
, sizeof *on
);
5700 on
->ofpact
.len
= ofpacts
->size
;
5702 /* Put any previous actions or instructions back on. */
5703 ofpbuf_push_uninit(ofpacts
, ofs
);
5709 format_WRITE_ACTIONS(const struct ofpact_nest
*a
, struct ds
*s
)
5711 ds_put_format(s
, "%swrite_actions(%s", colors
.paren
, colors
.end
);
5712 ofpacts_format(a
->actions
, ofpact_nest_get_action_len(a
), s
);
5713 ds_put_format(s
, "%s)%s", colors
.paren
, colors
.end
);
5716 /* Action structure for NXAST_WRITE_METADATA.
5718 * Modifies the 'mask' bits of the metadata value. */
5719 struct nx_action_write_metadata
{
5720 ovs_be16 type
; /* OFPAT_VENDOR. */
5721 ovs_be16 len
; /* Length is 32. */
5722 ovs_be32 vendor
; /* NX_VENDOR_ID. */
5723 ovs_be16 subtype
; /* NXAST_WRITE_METADATA. */
5724 uint8_t zeros
[6]; /* Must be zero. */
5725 ovs_be64 metadata
; /* Metadata register. */
5726 ovs_be64 mask
; /* Metadata mask. */
5728 OFP_ASSERT(sizeof(struct nx_action_write_metadata
) == 32);
5731 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata
*nawm
,
5732 enum ofp_version ofp_version OVS_UNUSED
,
5735 struct ofpact_metadata
*om
;
5737 if (!is_all_zeros(nawm
->zeros
, sizeof nawm
->zeros
)) {
5738 return OFPERR_NXBRC_MUST_BE_ZERO
;
5741 om
= ofpact_put_WRITE_METADATA(out
);
5742 om
->metadata
= nawm
->metadata
;
5743 om
->mask
= nawm
->mask
;
5749 encode_WRITE_METADATA(const struct ofpact_metadata
*metadata
,
5750 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5752 if (ofp_version
== OFP10_VERSION
) {
5753 struct nx_action_write_metadata
*nawm
;
5755 nawm
= put_NXAST_WRITE_METADATA(out
);
5756 nawm
->metadata
= metadata
->metadata
;
5757 nawm
->mask
= metadata
->mask
;
5759 struct ofp11_instruction_write_metadata
*oiwm
;
5761 oiwm
= instruction_put_OFPIT11_WRITE_METADATA(out
);
5762 oiwm
->metadata
= metadata
->metadata
;
5763 oiwm
->metadata_mask
= metadata
->mask
;
5767 static char * OVS_WARN_UNUSED_RESULT
5768 parse_WRITE_METADATA(char *arg
, struct ofpbuf
*ofpacts
,
5769 enum ofputil_protocol
*usable_protocols
)
5771 struct ofpact_metadata
*om
;
5772 char *mask
= strchr(arg
, '/');
5774 *usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
5776 om
= ofpact_put_WRITE_METADATA(ofpacts
);
5781 error
= str_to_be64(mask
+ 1, &om
->mask
);
5786 om
->mask
= OVS_BE64_MAX
;
5789 return str_to_be64(arg
, &om
->metadata
);
5793 format_WRITE_METADATA(const struct ofpact_metadata
*a
, struct ds
*s
)
5795 ds_put_format(s
, "%swrite_metadata:%s%#"PRIx64
,
5796 colors
.param
, colors
.end
, ntohll(a
->metadata
));
5797 if (a
->mask
!= OVS_BE64_MAX
) {
5798 ds_put_format(s
, "/%#"PRIx64
, ntohll(a
->mask
));
5802 /* Goto-Table instruction. */
5805 encode_GOTO_TABLE(const struct ofpact_goto_table
*goto_table
,
5806 enum ofp_version ofp_version
, struct ofpbuf
*out
)
5808 if (ofp_version
== OFP10_VERSION
) {
5809 struct nx_action_resubmit
*nar
;
5811 nar
= put_NXAST_RESUBMIT_TABLE(out
);
5812 nar
->table
= goto_table
->table_id
;
5813 nar
->in_port
= htons(ofp_to_u16(OFPP_IN_PORT
));
5815 struct ofp11_instruction_goto_table
*oigt
;
5817 oigt
= instruction_put_OFPIT11_GOTO_TABLE(out
);
5818 oigt
->table_id
= goto_table
->table_id
;
5819 memset(oigt
->pad
, 0, sizeof oigt
->pad
);
5823 static char * OVS_WARN_UNUSED_RESULT
5824 parse_GOTO_TABLE(char *arg
, struct ofpbuf
*ofpacts
,
5825 enum ofputil_protocol
*usable_protocols OVS_UNUSED
)
5827 struct ofpact_goto_table
*ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
5828 char *table_s
= strsep(&arg
, ",");
5829 if (!table_s
|| !table_s
[0]) {
5830 return xstrdup("instruction goto-table needs table id");
5832 return str_to_u8(table_s
, "table", &ogt
->table_id
);
5836 format_GOTO_TABLE(const struct ofpact_goto_table
*a
, struct ds
*s
)
5838 ds_put_format(s
, "%sgoto_table:%s%"PRIu8
,
5839 colors
.param
, colors
.end
, a
->table_id
);
5843 log_bad_action(const struct ofp_action_header
*actions
, size_t actions_len
,
5844 const struct ofp_action_header
*bad_action
, enum ofperr error
)
5846 if (!VLOG_DROP_WARN(&rl
)) {
5850 ds_put_hex_dump(&s
, actions
, actions_len
, 0, false);
5851 VLOG_WARN("bad action at offset %#"PRIxPTR
" (%s):\n%s",
5852 (char *)bad_action
- (char *)actions
,
5853 ofperr_get_name(error
), ds_cstr(&s
));
5859 ofpacts_decode(const void *actions
, size_t actions_len
,
5860 enum ofp_version ofp_version
, struct ofpbuf
*ofpacts
)
5862 struct ofpbuf openflow
= ofpbuf_const_initializer(actions
, actions_len
);
5863 while (openflow
.size
) {
5864 const struct ofp_action_header
*action
= openflow
.data
;
5865 enum ofp_raw_action_type raw
;
5869 error
= ofpact_pull_raw(&openflow
, ofp_version
, &raw
, &arg
);
5871 error
= ofpact_decode(action
, raw
, ofp_version
, arg
, ofpacts
);
5875 log_bad_action(actions
, actions_len
, action
, error
);
5883 ofpacts_pull_openflow_actions__(struct ofpbuf
*openflow
,
5884 unsigned int actions_len
,
5885 enum ofp_version version
,
5886 uint32_t allowed_ovsinsts
,
5887 struct ofpbuf
*ofpacts
,
5888 enum ofpact_type outer_action
)
5890 const struct ofp_action_header
*actions
;
5891 size_t orig_size
= ofpacts
->size
;
5894 if (actions_len
% OFP_ACTION_ALIGN
!= 0) {
5895 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u is not a "
5896 "multiple of %d", actions_len
, OFP_ACTION_ALIGN
);
5897 return OFPERR_OFPBRC_BAD_LEN
;
5900 actions
= ofpbuf_try_pull(openflow
, actions_len
);
5901 if (actions
== NULL
) {
5902 VLOG_WARN_RL(&rl
, "OpenFlow message actions length %u exceeds "
5903 "remaining message length (%"PRIu32
")",
5904 actions_len
, openflow
->size
);
5905 return OFPERR_OFPBRC_BAD_LEN
;
5908 error
= ofpacts_decode(actions
, actions_len
, version
, ofpacts
);
5910 ofpacts
->size
= orig_size
;
5914 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
, allowed_ovsinsts
,
5917 ofpacts
->size
= orig_size
;
5922 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
5923 * of 'openflow' into ofpacts. On success, appends the converted actions to
5924 * 'ofpacts'; on failure, 'ofpacts' is unchanged (but might be reallocated) .
5925 * Returns 0 if successful, otherwise an OpenFlow error.
5927 * Actions are processed according to their OpenFlow version which
5928 * is provided in the 'version' parameter.
5930 * In most places in OpenFlow, actions appear encapsulated in instructions, so
5931 * you should call ofpacts_pull_openflow_instructions() instead of this
5934 * The parsed actions are valid generically, but they may not be valid in a
5935 * specific context. For example, port numbers up to OFPP_MAX are valid
5936 * generically, but specific datapaths may only support port numbers in a
5937 * smaller range. Use ofpacts_check() to additional check whether actions are
5938 * valid in a specific context. */
5940 ofpacts_pull_openflow_actions(struct ofpbuf
*openflow
,
5941 unsigned int actions_len
,
5942 enum ofp_version version
,
5943 struct ofpbuf
*ofpacts
)
5945 return ofpacts_pull_openflow_actions__(openflow
, actions_len
, version
,
5946 1u << OVSINST_OFPIT11_APPLY_ACTIONS
,
5950 /* OpenFlow 1.1 actions. */
5953 /* True if an action sets the value of a field
5954 * in a way that is compatibile with the action set.
5955 * The field can be set via either a set or a move action.
5956 * False otherwise. */
5958 ofpact_is_set_or_move_action(const struct ofpact
*a
)
5961 case OFPACT_SET_FIELD
:
5962 case OFPACT_REG_MOVE
:
5963 case OFPACT_SET_ETH_DST
:
5964 case OFPACT_SET_ETH_SRC
:
5965 case OFPACT_SET_IP_DSCP
:
5966 case OFPACT_SET_IP_ECN
:
5967 case OFPACT_SET_IP_TTL
:
5968 case OFPACT_SET_IPV4_DST
:
5969 case OFPACT_SET_IPV4_SRC
:
5970 case OFPACT_SET_L4_DST_PORT
:
5971 case OFPACT_SET_L4_SRC_PORT
:
5972 case OFPACT_SET_MPLS_LABEL
:
5973 case OFPACT_SET_MPLS_TC
:
5974 case OFPACT_SET_MPLS_TTL
:
5975 case OFPACT_SET_QUEUE
:
5976 case OFPACT_SET_TUNNEL
:
5977 case OFPACT_SET_VLAN_PCP
:
5978 case OFPACT_SET_VLAN_VID
:
5981 case OFPACT_CLEAR_ACTIONS
:
5984 case OFPACT_CONTROLLER
:
5985 case OFPACT_DEC_MPLS_TTL
:
5986 case OFPACT_DEC_TTL
:
5987 case OFPACT_ENQUEUE
:
5989 case OFPACT_UNROLL_XLATE
:
5990 case OFPACT_FIN_TIMEOUT
:
5991 case OFPACT_GOTO_TABLE
:
5994 case OFPACT_CONJUNCTION
:
5996 case OFPACT_MULTIPATH
:
5999 case OFPACT_OUTPUT_REG
:
6000 case OFPACT_POP_MPLS
:
6001 case OFPACT_POP_QUEUE
:
6002 case OFPACT_PUSH_MPLS
:
6003 case OFPACT_PUSH_VLAN
:
6004 case OFPACT_RESUBMIT
:
6006 case OFPACT_STACK_POP
:
6007 case OFPACT_STACK_PUSH
:
6008 case OFPACT_STRIP_VLAN
:
6009 case OFPACT_WRITE_ACTIONS
:
6010 case OFPACT_WRITE_METADATA
:
6011 case OFPACT_DEBUG_RECIRC
:
6018 /* True if an action is allowed in the action set.
6019 * False otherwise. */
6021 ofpact_is_allowed_in_actions_set(const struct ofpact
*a
)
6024 case OFPACT_DEC_MPLS_TTL
:
6025 case OFPACT_DEC_TTL
:
6028 case OFPACT_POP_MPLS
:
6029 case OFPACT_PUSH_MPLS
:
6030 case OFPACT_PUSH_VLAN
:
6031 case OFPACT_REG_MOVE
:
6032 case OFPACT_SET_FIELD
:
6033 case OFPACT_SET_ETH_DST
:
6034 case OFPACT_SET_ETH_SRC
:
6035 case OFPACT_SET_IP_DSCP
:
6036 case OFPACT_SET_IP_ECN
:
6037 case OFPACT_SET_IP_TTL
:
6038 case OFPACT_SET_IPV4_DST
:
6039 case OFPACT_SET_IPV4_SRC
:
6040 case OFPACT_SET_L4_DST_PORT
:
6041 case OFPACT_SET_L4_SRC_PORT
:
6042 case OFPACT_SET_MPLS_LABEL
:
6043 case OFPACT_SET_MPLS_TC
:
6044 case OFPACT_SET_MPLS_TTL
:
6045 case OFPACT_SET_QUEUE
:
6046 case OFPACT_SET_TUNNEL
:
6047 case OFPACT_SET_VLAN_PCP
:
6048 case OFPACT_SET_VLAN_VID
:
6049 case OFPACT_STRIP_VLAN
:
6052 /* In general these actions are excluded because they are not part of
6053 * the OpenFlow specification nor map to actions that are defined in
6054 * the specification. Thus the order in which they should be applied
6055 * in the action set is undefined. */
6057 case OFPACT_CONTROLLER
:
6060 case OFPACT_ENQUEUE
:
6062 case OFPACT_UNROLL_XLATE
:
6063 case OFPACT_FIN_TIMEOUT
:
6065 case OFPACT_CONJUNCTION
:
6066 case OFPACT_MULTIPATH
:
6068 case OFPACT_OUTPUT_REG
:
6069 case OFPACT_POP_QUEUE
:
6070 case OFPACT_RESUBMIT
:
6072 case OFPACT_STACK_POP
:
6073 case OFPACT_STACK_PUSH
:
6074 case OFPACT_DEBUG_RECIRC
:
6076 /* The action set may only include actions and thus
6077 * may not include any instructions */
6078 case OFPACT_CLEAR_ACTIONS
:
6079 case OFPACT_GOTO_TABLE
:
6081 case OFPACT_WRITE_ACTIONS
:
6082 case OFPACT_WRITE_METADATA
:
6089 /* Append ofpact 'a' onto the tail of 'out' */
6091 ofpact_copy(struct ofpbuf
*out
, const struct ofpact
*a
)
6093 ofpbuf_put(out
, a
, OFPACT_ALIGN(a
->len
));
6096 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
6098 ofpacts_copy_last(struct ofpbuf
*out
, const struct ofpbuf
*in
,
6099 enum ofpact_type filter
)
6101 const struct ofpact
*target
;
6102 const struct ofpact
*a
;
6105 OFPACT_FOR_EACH (a
, in
->data
, in
->size
) {
6106 if (a
->type
== filter
) {
6111 ofpact_copy(out
, target
);
6113 return target
!= NULL
;
6116 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
6117 * The order of appended ofpacts is preserved between 'in' and 'out' */
6119 ofpacts_copy_all(struct ofpbuf
*out
, const struct ofpbuf
*in
,
6120 bool (*filter
)(const struct ofpact
*))
6122 const struct ofpact
*a
;
6124 OFPACT_FOR_EACH (a
, in
->data
, in
->size
) {
6126 ofpact_copy(out
, a
);
6131 /* Reads 'action_set', which contains ofpacts accumulated by
6132 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
6133 * executed directly into 'action_list'. (These names correspond to the
6134 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
6136 * In general this involves appending the last instance of each action that is
6137 * admissible in the action set in the order described in the OpenFlow
6141 * + output action is only appended if no group action was present in 'in'.
6142 * + As a simplification all set actions are copied in the order the are
6143 * provided in 'in' as many set actions applied to a field has the same
6144 * affect as only applying the last action that sets a field and
6145 * duplicates are removed by do_xlate_actions().
6146 * This has an unwanted side-effect of compsoting multiple
6147 * LOAD_REG actions that touch different regions of the same field. */
6149 ofpacts_execute_action_set(struct ofpbuf
*action_list
,
6150 const struct ofpbuf
*action_set
)
6152 /* The OpenFlow spec "Action Set" section specifies this order. */
6153 ofpacts_copy_last(action_list
, action_set
, OFPACT_STRIP_VLAN
);
6154 ofpacts_copy_last(action_list
, action_set
, OFPACT_POP_MPLS
);
6155 ofpacts_copy_last(action_list
, action_set
, OFPACT_PUSH_MPLS
);
6156 ofpacts_copy_last(action_list
, action_set
, OFPACT_PUSH_VLAN
);
6157 ofpacts_copy_last(action_list
, action_set
, OFPACT_DEC_TTL
);
6158 ofpacts_copy_last(action_list
, action_set
, OFPACT_DEC_MPLS_TTL
);
6159 ofpacts_copy_all(action_list
, action_set
, ofpact_is_set_or_move_action
);
6160 ofpacts_copy_last(action_list
, action_set
, OFPACT_SET_QUEUE
);
6162 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
6163 * we should execute only OFPACT_GROUP.
6165 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
6166 * all the actions because there's no point in modifying a packet that will
6167 * not be sent anywhere. */
6168 if (!ofpacts_copy_last(action_list
, action_set
, OFPACT_GROUP
) &&
6169 !ofpacts_copy_last(action_list
, action_set
, OFPACT_OUTPUT
) &&
6170 !ofpacts_copy_last(action_list
, action_set
, OFPACT_RESUBMIT
) &&
6171 !ofpacts_copy_last(action_list
, action_set
, OFPACT_CT
)) {
6172 ofpbuf_clear(action_list
);
6178 ofpacts_decode_for_action_set(const struct ofp_action_header
*in
,
6179 size_t n_in
, enum ofp_version version
,
6184 size_t start
= out
->size
;
6186 error
= ofpacts_decode(in
, n_in
, version
, out
);
6192 OFPACT_FOR_EACH (a
, ofpact_end(out
->data
, start
), out
->size
- start
) {
6193 if (!ofpact_is_allowed_in_actions_set(a
)) {
6194 VLOG_WARN_RL(&rl
, "disallowed action in action set");
6195 return OFPERR_OFPBAC_BAD_TYPE
;
6202 /* OpenFlow 1.1 instructions. */
6204 struct instruction_type_info
{
6205 enum ovs_instruction_type type
;
6209 static const struct instruction_type_info inst_info
[] = {
6210 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
6216 ovs_instruction_name_from_type(enum ovs_instruction_type type
)
6218 return inst_info
[type
].name
;
6222 ovs_instruction_type_from_name(const char *name
)
6224 const struct instruction_type_info
*p
;
6225 for (p
= inst_info
; p
< &inst_info
[ARRAY_SIZE(inst_info
)]; p
++) {
6226 if (!strcasecmp(name
, p
->name
)) {
6233 enum ovs_instruction_type
6234 ovs_instruction_type_from_ofpact_type(enum ofpact_type type
)
6238 return OVSINST_OFPIT13_METER
;
6239 case OFPACT_CLEAR_ACTIONS
:
6240 return OVSINST_OFPIT11_CLEAR_ACTIONS
;
6241 case OFPACT_WRITE_ACTIONS
:
6242 return OVSINST_OFPIT11_WRITE_ACTIONS
;
6243 case OFPACT_WRITE_METADATA
:
6244 return OVSINST_OFPIT11_WRITE_METADATA
;
6245 case OFPACT_GOTO_TABLE
:
6246 return OVSINST_OFPIT11_GOTO_TABLE
;
6249 case OFPACT_CONTROLLER
:
6250 case OFPACT_ENQUEUE
:
6251 case OFPACT_OUTPUT_REG
:
6253 case OFPACT_SET_VLAN_VID
:
6254 case OFPACT_SET_VLAN_PCP
:
6255 case OFPACT_STRIP_VLAN
:
6256 case OFPACT_PUSH_VLAN
:
6257 case OFPACT_SET_ETH_SRC
:
6258 case OFPACT_SET_ETH_DST
:
6259 case OFPACT_SET_IPV4_SRC
:
6260 case OFPACT_SET_IPV4_DST
:
6261 case OFPACT_SET_IP_DSCP
:
6262 case OFPACT_SET_IP_ECN
:
6263 case OFPACT_SET_IP_TTL
:
6264 case OFPACT_SET_L4_SRC_PORT
:
6265 case OFPACT_SET_L4_DST_PORT
:
6266 case OFPACT_REG_MOVE
:
6267 case OFPACT_SET_FIELD
:
6268 case OFPACT_STACK_PUSH
:
6269 case OFPACT_STACK_POP
:
6270 case OFPACT_DEC_TTL
:
6271 case OFPACT_SET_MPLS_LABEL
:
6272 case OFPACT_SET_MPLS_TC
:
6273 case OFPACT_SET_MPLS_TTL
:
6274 case OFPACT_DEC_MPLS_TTL
:
6275 case OFPACT_PUSH_MPLS
:
6276 case OFPACT_POP_MPLS
:
6277 case OFPACT_SET_TUNNEL
:
6278 case OFPACT_SET_QUEUE
:
6279 case OFPACT_POP_QUEUE
:
6280 case OFPACT_FIN_TIMEOUT
:
6281 case OFPACT_RESUBMIT
:
6283 case OFPACT_CONJUNCTION
:
6284 case OFPACT_MULTIPATH
:
6287 case OFPACT_UNROLL_XLATE
:
6289 case OFPACT_DEBUG_RECIRC
:
6293 return OVSINST_OFPIT11_APPLY_ACTIONS
;
6298 ovs_instruction_type_from_inst_type(enum ovs_instruction_type
*instruction_type
,
6299 const uint16_t inst_type
)
6301 switch (inst_type
) {
6303 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6305 *instruction_type = OVSINST_##ENUM; \
6311 return OFPERR_OFPBIC_UNKNOWN_INST
;
6315 /* Two-way translation between OVS's internal "OVSINST_*" representation of
6316 * instructions and the "OFPIT_*" representation used in OpenFlow. */
6317 struct ovsinst_map
{
6318 enum ovs_instruction_type ovsinst
; /* Internal name for instruction. */
6319 int ofpit
; /* OFPIT_* number from OpenFlow spec. */
6322 static const struct ovsinst_map
*
6323 get_ovsinst_map(enum ofp_version version
)
6325 /* OpenFlow 1.1 and 1.2 instructions. */
6326 static const struct ovsinst_map of11
[] = {
6327 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
6328 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
6329 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
6330 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
6331 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
6335 /* OpenFlow 1.3+ instructions. */
6336 static const struct ovsinst_map of13
[] = {
6337 { OVSINST_OFPIT11_GOTO_TABLE
, 1 },
6338 { OVSINST_OFPIT11_WRITE_METADATA
, 2 },
6339 { OVSINST_OFPIT11_WRITE_ACTIONS
, 3 },
6340 { OVSINST_OFPIT11_APPLY_ACTIONS
, 4 },
6341 { OVSINST_OFPIT11_CLEAR_ACTIONS
, 5 },
6342 { OVSINST_OFPIT13_METER
, 6 },
6346 return version
< OFP13_VERSION
? of11
: of13
;
6349 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
6350 * values, into a bitmap of instructions suitable for OpenFlow 'version'
6351 * (OFP11_VERSION or later), and returns the result. */
6353 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap
, enum ofp_version version
)
6355 uint32_t ofpit_bitmap
= 0;
6356 const struct ovsinst_map
*x
;
6358 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
6359 if (ovsinst_bitmap
& (1u << x
->ovsinst
)) {
6360 ofpit_bitmap
|= 1u << x
->ofpit
;
6363 return htonl(ofpit_bitmap
);
6366 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
6367 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
6368 * correspond to OVSINST_* values, and returns the result. */
6370 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap
, enum ofp_version version
)
6372 uint32_t ovsinst_bitmap
= 0;
6373 const struct ovsinst_map
*x
;
6375 for (x
= get_ovsinst_map(version
); x
->ofpit
>= 0; x
++) {
6376 if (ofpit_bitmap
& htonl(1u << x
->ofpit
)) {
6377 ovsinst_bitmap
|= 1u << x
->ovsinst
;
6380 return ovsinst_bitmap
;
6383 static inline struct ofp11_instruction
*
6384 instruction_next(const struct ofp11_instruction
*inst
)
6386 return ((struct ofp11_instruction
*) (void *)
6387 ((uint8_t *) inst
+ ntohs(inst
->len
)));
6391 instruction_is_valid(const struct ofp11_instruction
*inst
,
6392 size_t n_instructions
)
6394 uint16_t len
= ntohs(inst
->len
);
6395 return (!(len
% OFP11_INSTRUCTION_ALIGN
)
6396 && len
>= sizeof *inst
6397 && len
/ sizeof *inst
<= n_instructions
);
6400 /* This macro is careful to check for instructions with bad lengths. */
6401 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
6402 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
6403 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
6404 ((LEFT) -= (ntohs((ITER)->len) \
6405 / sizeof(struct ofp11_instruction)), \
6406 (ITER) = instruction_next(ITER)))
6409 decode_openflow11_instruction(const struct ofp11_instruction
*inst
,
6410 enum ovs_instruction_type
*type
)
6412 uint16_t len
= ntohs(inst
->len
);
6414 switch (inst
->type
) {
6415 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER
):
6416 return OFPERR_OFPBIC_BAD_EXPERIMENTER
;
6418 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6419 case CONSTANT_HTONS(ENUM): \
6421 ? len >= sizeof(struct STRUCT) \
6422 : len == sizeof(struct STRUCT)) { \
6423 *type = OVSINST_##ENUM; \
6426 return OFPERR_OFPBIC_BAD_LEN; \
6432 return OFPERR_OFPBIC_UNKNOWN_INST
;
6437 decode_openflow11_instructions(const struct ofp11_instruction insts
[],
6439 const struct ofp11_instruction
*out
[])
6441 const struct ofp11_instruction
*inst
;
6444 memset(out
, 0, N_OVS_INSTRUCTIONS
* sizeof *out
);
6445 INSTRUCTION_FOR_EACH (inst
, left
, insts
, n_insts
) {
6446 enum ovs_instruction_type type
;
6449 error
= decode_openflow11_instruction(inst
, &type
);
6455 return OFPERR_OFPBIC_DUP_INST
;
6461 VLOG_WARN_RL(&rl
, "bad instruction format at offset %"PRIuSIZE
,
6462 (n_insts
- left
) * sizeof *inst
);
6463 return OFPERR_OFPBIC_BAD_LEN
;
6469 get_actions_from_instruction(const struct ofp11_instruction
*inst
,
6470 const struct ofp_action_header
**actions
,
6471 size_t *actions_len
)
6473 *actions
= ALIGNED_CAST(const struct ofp_action_header
*, inst
+ 1);
6474 *actions_len
= ntohs(inst
->len
) - sizeof *inst
;
6478 ofpacts_pull_openflow_instructions(struct ofpbuf
*openflow
,
6479 unsigned int instructions_len
,
6480 enum ofp_version version
,
6481 struct ofpbuf
*ofpacts
)
6483 const struct ofp11_instruction
*instructions
;
6484 const struct ofp11_instruction
*insts
[N_OVS_INSTRUCTIONS
];
6487 ofpbuf_clear(ofpacts
);
6488 if (version
== OFP10_VERSION
) {
6489 return ofpacts_pull_openflow_actions__(openflow
, instructions_len
,
6491 (1u << N_OVS_INSTRUCTIONS
) - 1,
6495 if (instructions_len
% OFP11_INSTRUCTION_ALIGN
!= 0) {
6496 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u is not a "
6498 instructions_len
, OFP11_INSTRUCTION_ALIGN
);
6499 error
= OFPERR_OFPBIC_BAD_LEN
;
6503 instructions
= ofpbuf_try_pull(openflow
, instructions_len
);
6504 if (instructions
== NULL
) {
6505 VLOG_WARN_RL(&rl
, "OpenFlow message instructions length %u exceeds "
6506 "remaining message length (%"PRIu32
")",
6507 instructions_len
, openflow
->size
);
6508 error
= OFPERR_OFPBIC_BAD_LEN
;
6512 error
= decode_openflow11_instructions(
6513 instructions
, instructions_len
/ OFP11_INSTRUCTION_ALIGN
,
6519 if (insts
[OVSINST_OFPIT13_METER
]) {
6520 const struct ofp13_instruction_meter
*oim
;
6521 struct ofpact_meter
*om
;
6523 oim
= ALIGNED_CAST(const struct ofp13_instruction_meter
*,
6524 insts
[OVSINST_OFPIT13_METER
]);
6526 om
= ofpact_put_METER(ofpacts
);
6527 om
->meter_id
= ntohl(oim
->meter_id
);
6529 if (insts
[OVSINST_OFPIT11_APPLY_ACTIONS
]) {
6530 const struct ofp_action_header
*actions
;
6533 get_actions_from_instruction(insts
[OVSINST_OFPIT11_APPLY_ACTIONS
],
6534 &actions
, &actions_len
);
6535 error
= ofpacts_decode(actions
, actions_len
, version
, ofpacts
);
6540 if (insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]) {
6541 instruction_get_OFPIT11_CLEAR_ACTIONS(
6542 insts
[OVSINST_OFPIT11_CLEAR_ACTIONS
]);
6543 ofpact_put_CLEAR_ACTIONS(ofpacts
);
6545 if (insts
[OVSINST_OFPIT11_WRITE_ACTIONS
]) {
6546 struct ofpact_nest
*on
;
6547 const struct ofp_action_header
*actions
;
6549 size_t start
= ofpacts
->size
;
6550 ofpact_put(ofpacts
, OFPACT_WRITE_ACTIONS
,
6551 offsetof(struct ofpact_nest
, actions
));
6552 get_actions_from_instruction(insts
[OVSINST_OFPIT11_WRITE_ACTIONS
],
6553 &actions
, &actions_len
);
6554 error
= ofpacts_decode_for_action_set(actions
, actions_len
,
6559 on
= ofpbuf_at_assert(ofpacts
, start
, sizeof *on
);
6560 on
->ofpact
.len
= ofpacts
->size
- start
;
6562 if (insts
[OVSINST_OFPIT11_WRITE_METADATA
]) {
6563 const struct ofp11_instruction_write_metadata
*oiwm
;
6564 struct ofpact_metadata
*om
;
6566 oiwm
= ALIGNED_CAST(const struct ofp11_instruction_write_metadata
*,
6567 insts
[OVSINST_OFPIT11_WRITE_METADATA
]);
6569 om
= ofpact_put_WRITE_METADATA(ofpacts
);
6570 om
->metadata
= oiwm
->metadata
;
6571 om
->mask
= oiwm
->metadata_mask
;
6573 if (insts
[OVSINST_OFPIT11_GOTO_TABLE
]) {
6574 const struct ofp11_instruction_goto_table
*oigt
;
6575 struct ofpact_goto_table
*ogt
;
6577 oigt
= instruction_get_OFPIT11_GOTO_TABLE(
6578 insts
[OVSINST_OFPIT11_GOTO_TABLE
]);
6579 ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
6580 ogt
->table_id
= oigt
->table_id
;
6583 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
,
6584 (1u << N_OVS_INSTRUCTIONS
) - 1, 0);
6587 ofpbuf_clear(ofpacts
);
6592 /* Update the length of the instruction that begins at offset 'ofs' within
6593 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
6594 * If the instruction contains no nested actions, deletes it entirely. */
6596 ofpacts_update_instruction_actions(struct ofpbuf
*openflow
, size_t ofs
)
6598 struct ofp11_instruction_actions
*oia
;
6600 oia
= ofpbuf_at_assert(openflow
, ofs
, sizeof *oia
);
6601 if (openflow
->size
> ofs
+ sizeof *oia
) {
6602 oia
->len
= htons(openflow
->size
- ofs
);
6604 openflow
->size
= ofs
;
6608 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
6609 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
6610 * valid, otherwise an OpenFlow error code. */
6612 ofpact_check_output_port(ofp_port_t port
, ofp_port_t max_ports
)
6620 case OFPP_CONTROLLER
:
6625 return OFPERR_OFPBAC_BAD_OUT_PORT
;
6628 if (ofp_to_u16(port
) < ofp_to_u16(max_ports
)) {
6631 return OFPERR_OFPBAC_BAD_OUT_PORT
;
6635 /* Removes the protocols that require consistency between match and actions
6636 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
6638 * (An example of an inconsistency between match and actions is a flow that
6639 * does not match on an MPLS Ethertype but has an action that pops an MPLS
6642 inconsistent_match(enum ofputil_protocol
*usable_protocols
)
6644 *usable_protocols
&= OFPUTIL_P_OF10_ANY
;
6647 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
6648 * caller must restore them.
6650 * Modifies some actions, filling in fields that could not be properly set
6651 * without context. */
6653 ofpact_check__(enum ofputil_protocol
*usable_protocols
, struct ofpact
*a
,
6654 struct flow
*flow
, ofp_port_t max_ports
,
6655 uint8_t table_id
, uint8_t n_tables
)
6657 const struct ofpact_enqueue
*enqueue
;
6658 const struct mf_field
*mf
;
6662 return ofpact_check_output_port(ofpact_get_OUTPUT(a
)->port
,
6665 case OFPACT_CONTROLLER
:
6668 case OFPACT_ENQUEUE
:
6669 enqueue
= ofpact_get_ENQUEUE(a
);
6670 if (ofp_to_u16(enqueue
->port
) >= ofp_to_u16(max_ports
)
6671 && enqueue
->port
!= OFPP_IN_PORT
6672 && enqueue
->port
!= OFPP_LOCAL
) {
6673 return OFPERR_OFPBAC_BAD_OUT_PORT
;
6677 case OFPACT_OUTPUT_REG
:
6678 return mf_check_src(&ofpact_get_OUTPUT_REG(a
)->src
, flow
);
6681 return bundle_check(ofpact_get_BUNDLE(a
), max_ports
, flow
);
6683 case OFPACT_SET_VLAN_VID
:
6684 /* Remember if we saw a vlan tag in the flow to aid translating to
6685 * OpenFlow 1.1+ if need be. */
6686 ofpact_get_SET_VLAN_VID(a
)->flow_has_vlan
=
6687 (flow
->vlan_tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
6688 if (!(flow
->vlan_tci
& htons(VLAN_CFI
)) &&
6689 !ofpact_get_SET_VLAN_VID(a
)->push_vlan_if_needed
) {
6690 inconsistent_match(usable_protocols
);
6692 /* Temporary mark that we have a vlan tag. */
6693 flow
->vlan_tci
|= htons(VLAN_CFI
);
6696 case OFPACT_SET_VLAN_PCP
:
6697 /* Remember if we saw a vlan tag in the flow to aid translating to
6698 * OpenFlow 1.1+ if need be. */
6699 ofpact_get_SET_VLAN_PCP(a
)->flow_has_vlan
=
6700 (flow
->vlan_tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
6701 if (!(flow
->vlan_tci
& htons(VLAN_CFI
)) &&
6702 !ofpact_get_SET_VLAN_PCP(a
)->push_vlan_if_needed
) {
6703 inconsistent_match(usable_protocols
);
6705 /* Temporary mark that we have a vlan tag. */
6706 flow
->vlan_tci
|= htons(VLAN_CFI
);
6709 case OFPACT_STRIP_VLAN
:
6710 if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
6711 inconsistent_match(usable_protocols
);
6713 /* Temporary mark that we have no vlan tag. */
6714 flow
->vlan_tci
= htons(0);
6717 case OFPACT_PUSH_VLAN
:
6718 if (flow
->vlan_tci
& htons(VLAN_CFI
)) {
6719 /* Multiple VLAN headers not supported. */
6720 return OFPERR_OFPBAC_BAD_TAG
;
6722 /* Temporary mark that we have a vlan tag. */
6723 flow
->vlan_tci
|= htons(VLAN_CFI
);
6726 case OFPACT_SET_ETH_SRC
:
6727 case OFPACT_SET_ETH_DST
:
6730 case OFPACT_SET_IPV4_SRC
:
6731 case OFPACT_SET_IPV4_DST
:
6732 if (flow
->dl_type
!= htons(ETH_TYPE_IP
)) {
6733 inconsistent_match(usable_protocols
);
6737 case OFPACT_SET_IP_DSCP
:
6738 case OFPACT_SET_IP_ECN
:
6739 case OFPACT_SET_IP_TTL
:
6740 case OFPACT_DEC_TTL
:
6741 if (!is_ip_any(flow
)) {
6742 inconsistent_match(usable_protocols
);
6746 case OFPACT_SET_L4_SRC_PORT
:
6747 case OFPACT_SET_L4_DST_PORT
:
6748 if (!is_ip_any(flow
) || (flow
->nw_frag
& FLOW_NW_FRAG_LATER
) ||
6749 (flow
->nw_proto
!= IPPROTO_TCP
&& flow
->nw_proto
!= IPPROTO_UDP
6750 && flow
->nw_proto
!= IPPROTO_SCTP
)) {
6751 inconsistent_match(usable_protocols
);
6753 /* Note on which transport protocol the port numbers are set.
6754 * This allows this set action to be converted to an OF1.2 set field
6756 if (a
->type
== OFPACT_SET_L4_SRC_PORT
) {
6757 ofpact_get_SET_L4_SRC_PORT(a
)->flow_ip_proto
= flow
->nw_proto
;
6759 ofpact_get_SET_L4_DST_PORT(a
)->flow_ip_proto
= flow
->nw_proto
;
6763 case OFPACT_REG_MOVE
:
6764 return nxm_reg_move_check(ofpact_get_REG_MOVE(a
), flow
);
6766 case OFPACT_SET_FIELD
:
6767 mf
= ofpact_get_SET_FIELD(a
)->field
;
6768 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6769 if (!mf_are_prereqs_ok(mf
, flow
) ||
6770 (mf
->id
== MFF_VLAN_VID
&& !(flow
->vlan_tci
& htons(VLAN_CFI
)))) {
6771 VLOG_WARN_RL(&rl
, "set_field %s lacks correct prerequisities",
6773 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
6775 /* Remember if we saw a vlan tag in the flow to aid translating to
6776 * OpenFlow 1.1 if need be. */
6777 ofpact_get_SET_FIELD(a
)->flow_has_vlan
=
6778 (flow
->vlan_tci
& htons(VLAN_CFI
)) == htons(VLAN_CFI
);
6779 if (mf
->id
== MFF_VLAN_TCI
) {
6780 /* The set field may add or remove the vlan tag,
6781 * Mark the status temporarily. */
6782 flow
->vlan_tci
= ofpact_get_SET_FIELD(a
)->value
.be16
;
6786 case OFPACT_STACK_PUSH
:
6787 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a
), flow
);
6789 case OFPACT_STACK_POP
:
6790 return nxm_stack_pop_check(ofpact_get_STACK_POP(a
), flow
);
6792 case OFPACT_SET_MPLS_LABEL
:
6793 case OFPACT_SET_MPLS_TC
:
6794 case OFPACT_SET_MPLS_TTL
:
6795 case OFPACT_DEC_MPLS_TTL
:
6796 if (!eth_type_mpls(flow
->dl_type
)) {
6797 inconsistent_match(usable_protocols
);
6801 case OFPACT_SET_TUNNEL
:
6802 case OFPACT_SET_QUEUE
:
6803 case OFPACT_POP_QUEUE
:
6804 case OFPACT_RESUBMIT
:
6807 case OFPACT_FIN_TIMEOUT
:
6808 if (flow
->nw_proto
!= IPPROTO_TCP
) {
6809 inconsistent_match(usable_protocols
);
6814 return learn_check(ofpact_get_LEARN(a
), flow
);
6816 case OFPACT_CONJUNCTION
:
6819 case OFPACT_MULTIPATH
:
6820 return multipath_check(ofpact_get_MULTIPATH(a
), flow
);
6826 case OFPACT_PUSH_MPLS
:
6827 flow
->dl_type
= ofpact_get_PUSH_MPLS(a
)->ethertype
;
6828 /* The packet is now MPLS and the MPLS payload is opaque.
6829 * Thus nothing can be assumed about the network protocol.
6830 * Temporarily mark that we have no nw_proto. */
6834 case OFPACT_POP_MPLS
:
6835 if (!eth_type_mpls(flow
->dl_type
)) {
6836 inconsistent_match(usable_protocols
);
6838 flow
->dl_type
= ofpact_get_POP_MPLS(a
)->ethertype
;
6845 struct ofpact_conntrack
*oc
= ofpact_get_CT(a
);
6848 if (!dl_type_is_ip_any(flow
->dl_type
)
6849 || (flow
->ct_state
& CS_INVALID
&& oc
->flags
& NX_CT_F_COMMIT
)) {
6850 inconsistent_match(usable_protocols
);
6853 if (oc
->zone_src
.field
) {
6854 return mf_check_src(&oc
->zone_src
, flow
);
6857 err
= ofpacts_check(oc
->actions
, ofpact_ct_get_action_len(oc
),
6858 flow
, max_ports
, table_id
, n_tables
,
6864 struct ofpact_nat
*on
= ofpact_get_NAT(a
);
6866 if (!dl_type_is_ip_any(flow
->dl_type
) ||
6867 (on
->range_af
== AF_INET
&& flow
->dl_type
!= htons(ETH_TYPE_IP
)) ||
6868 (on
->range_af
== AF_INET6
6869 && flow
->dl_type
!= htons(ETH_TYPE_IPV6
))) {
6870 inconsistent_match(usable_protocols
);
6875 case OFPACT_CLEAR_ACTIONS
:
6878 case OFPACT_WRITE_ACTIONS
: {
6879 /* Use a temporary copy of 'usable_protocols' because we can't check
6880 * consistency of an action set. */
6881 struct ofpact_nest
*on
= ofpact_get_WRITE_ACTIONS(a
);
6882 enum ofputil_protocol p
= *usable_protocols
;
6883 return ofpacts_check(on
->actions
, ofpact_nest_get_action_len(on
),
6884 flow
, max_ports
, table_id
, n_tables
, &p
);
6887 case OFPACT_WRITE_METADATA
:
6890 case OFPACT_METER
: {
6891 uint32_t mid
= ofpact_get_METER(a
)->meter_id
;
6892 if (mid
== 0 || mid
> OFPM13_MAX
) {
6893 return OFPERR_OFPMMFC_INVALID_METER
;
6898 case OFPACT_GOTO_TABLE
: {
6899 uint8_t goto_table
= ofpact_get_GOTO_TABLE(a
)->table_id
;
6900 if ((table_id
!= 255 && goto_table
<= table_id
)
6901 || (n_tables
!= 255 && goto_table
>= n_tables
)) {
6902 return OFPERR_OFPBIC_BAD_TABLE_ID
;
6910 case OFPACT_UNROLL_XLATE
:
6911 /* UNROLL is an internal action that should never be seen via
6913 return OFPERR_OFPBAC_BAD_TYPE
;
6915 case OFPACT_DEBUG_RECIRC
:
6923 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6924 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6925 * switch with no more than 'max_ports' ports.
6927 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6928 * '*usable_protocols' the protocols that forbid the inconsistency. (An
6929 * example of an inconsistency between match and actions is a flow that does
6930 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6932 * May annotate ofpacts with information gathered from the 'flow'.
6934 * May temporarily modify 'flow', but restores the changes before returning. */
6936 ofpacts_check(struct ofpact ofpacts
[], size_t ofpacts_len
,
6937 struct flow
*flow
, ofp_port_t max_ports
,
6938 uint8_t table_id
, uint8_t n_tables
,
6939 enum ofputil_protocol
*usable_protocols
)
6942 ovs_be16 dl_type
= flow
->dl_type
;
6943 ovs_be16 vlan_tci
= flow
->vlan_tci
;
6944 uint8_t nw_proto
= flow
->nw_proto
;
6945 enum ofperr error
= 0;
6947 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
6948 error
= ofpact_check__(usable_protocols
, a
, flow
,
6949 max_ports
, table_id
, n_tables
);
6954 /* Restore fields that may have been modified. */
6955 flow
->dl_type
= dl_type
;
6956 flow
->vlan_tci
= vlan_tci
;
6957 flow
->nw_proto
= nw_proto
;
6961 /* Like ofpacts_check(), but reports inconsistencies as
6962 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6964 ofpacts_check_consistency(struct ofpact ofpacts
[], size_t ofpacts_len
,
6965 struct flow
*flow
, ofp_port_t max_ports
,
6966 uint8_t table_id
, uint8_t n_tables
,
6967 enum ofputil_protocol usable_protocols
)
6969 enum ofputil_protocol p
= usable_protocols
;
6972 error
= ofpacts_check(ofpacts
, ofpacts_len
, flow
, max_ports
,
6973 table_id
, n_tables
, &p
);
6974 return (error
? error
6975 : p
!= usable_protocols
? OFPERR_OFPBAC_MATCH_INCONSISTENT
6979 /* Returns the destination field that 'ofpact' would write to, or NULL
6980 * if the action would not write to an mf_field. */
6981 const struct mf_field
*
6982 ofpact_get_mf_dst(const struct ofpact
*ofpact
)
6984 if (ofpact
->type
== OFPACT_SET_FIELD
) {
6985 const struct ofpact_set_field
*orl
;
6987 orl
= CONTAINER_OF(ofpact
, struct ofpact_set_field
, ofpact
);
6989 } else if (ofpact
->type
== OFPACT_REG_MOVE
) {
6990 const struct ofpact_reg_move
*orm
;
6992 orm
= CONTAINER_OF(ofpact
, struct ofpact_reg_move
, ofpact
);
6993 return orm
->dst
.field
;
7000 unsupported_nesting(enum ofpact_type action
, enum ofpact_type outer_action
)
7002 VLOG_WARN("%s action doesn't support nested action %s",
7003 ofpact_name(outer_action
), ofpact_name(action
));
7004 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7008 field_requires_ct(enum mf_field_id field
)
7010 return field
== MFF_CT_MARK
|| field
== MFF_CT_LABEL
;
7013 /* Apply nesting constraints for actions */
7015 ofpacts_verify_nested(const struct ofpact
*a
, enum ofpact_type outer_action
)
7017 const struct mf_field
*field
= ofpact_get_mf_dst(a
);
7019 if (field
&& field_requires_ct(field
->id
) && outer_action
!= OFPACT_CT
) {
7020 VLOG_WARN("cannot set CT fields outside of ct action");
7021 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
7023 if (a
->type
== OFPACT_NAT
) {
7024 if (outer_action
!= OFPACT_CT
) {
7025 VLOG_WARN("Cannot have NAT action outside of \"ct\" action");
7026 return OFPERR_OFPBAC_BAD_SET_ARGUMENT
;
7032 ovs_assert(outer_action
== OFPACT_WRITE_ACTIONS
7033 || outer_action
== OFPACT_CT
);
7035 if (outer_action
== OFPACT_CT
) {
7037 return unsupported_nesting(a
->type
, outer_action
);
7038 } else if (!field_requires_ct(field
->id
)) {
7039 VLOG_WARN("%s action doesn't support nested modification "
7040 "of %s", ofpact_name(outer_action
), field
->name
);
7041 return OFPERR_OFPBAC_BAD_ARGUMENT
;
7049 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
7050 * appropriate order as defined by the OpenFlow spec and as required by Open
7053 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
7054 * instructions that are allowed within 'ofpacts[]'.
7056 * If 'outer_action' is not zero, it specifies that the actions are nested
7057 * within another action of type 'outer_action'. */
7059 ofpacts_verify(const struct ofpact ofpacts
[], size_t ofpacts_len
,
7060 uint32_t allowed_ovsinsts
, enum ofpact_type outer_action
)
7062 const struct ofpact
*a
;
7063 enum ovs_instruction_type inst
;
7065 inst
= OVSINST_OFPIT13_METER
;
7066 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7067 enum ovs_instruction_type next
;
7070 if (a
->type
== OFPACT_CONJUNCTION
) {
7071 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7072 if (a
->type
!= OFPACT_CONJUNCTION
&& a
->type
!= OFPACT_NOTE
) {
7073 VLOG_WARN("\"conjunction\" actions may be used along with "
7074 "\"note\" but not any other kind of action "
7075 "(such as the \"%s\" action used here)",
7076 ofpact_name(a
->type
));
7077 return OFPERR_NXBAC_BAD_CONJUNCTION
;
7083 error
= ofpacts_verify_nested(a
, outer_action
);
7088 next
= ovs_instruction_type_from_ofpact_type(a
->type
);
7090 && (inst
== OVSINST_OFPIT11_APPLY_ACTIONS
7093 const char *name
= ovs_instruction_name_from_type(inst
);
7094 const char *next_name
= ovs_instruction_name_from_type(next
);
7097 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
7098 "1.1+ compatibility", name
);
7100 VLOG_WARN("invalid instruction ordering: %s must appear "
7101 "before %s, for OpenFlow 1.1+ compatibility",
7104 return OFPERR_OFPBAC_UNSUPPORTED_ORDER
;
7106 if (!((1u << next
) & allowed_ovsinsts
)) {
7107 const char *name
= ovs_instruction_name_from_type(next
);
7109 VLOG_WARN("%s instruction not allowed here", name
);
7110 return OFPERR_OFPBIC_UNSUP_INST
;
7119 /* Converting ofpacts to OpenFlow. */
7122 encode_ofpact(const struct ofpact
*a
, enum ofp_version ofp_version
,
7126 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7127 case OFPACT_##ENUM: \
7128 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
7137 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
7138 * actions in 'openflow', appending the actions to any existing data in
7141 ofpacts_put_openflow_actions(const struct ofpact ofpacts
[], size_t ofpacts_len
,
7142 struct ofpbuf
*openflow
,
7143 enum ofp_version ofp_version
)
7145 const struct ofpact
*a
;
7146 size_t start_size
= openflow
->size
;
7148 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7149 encode_ofpact(a
, ofp_version
, openflow
);
7151 return openflow
->size
- start_size
;
7154 static enum ovs_instruction_type
7155 ofpact_is_apply_actions(const struct ofpact
*a
)
7157 return (ovs_instruction_type_from_ofpact_type(a
->type
)
7158 == OVSINST_OFPIT11_APPLY_ACTIONS
);
7162 ofpacts_put_openflow_instructions(const struct ofpact ofpacts
[],
7164 struct ofpbuf
*openflow
,
7165 enum ofp_version ofp_version
)
7167 const struct ofpact
*end
= ofpact_end(ofpacts
, ofpacts_len
);
7168 const struct ofpact
*a
;
7170 if (ofp_version
== OFP10_VERSION
) {
7171 ofpacts_put_openflow_actions(ofpacts
, ofpacts_len
, openflow
,
7178 if (ofpact_is_apply_actions(a
)) {
7179 size_t ofs
= openflow
->size
;
7181 instruction_put_OFPIT11_APPLY_ACTIONS(openflow
);
7183 encode_ofpact(a
, ofp_version
, openflow
);
7185 } while (a
< end
&& ofpact_is_apply_actions(a
));
7186 ofpacts_update_instruction_actions(openflow
, ofs
);
7188 encode_ofpact(a
, ofp_version
, openflow
);
7194 /* Sets of supported actions. */
7196 /* Two-way translation between OVS's internal "OFPACT_*" representation of
7197 * actions and the "OFPAT_*" representation used in some OpenFlow version.
7198 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
7199 * instance is specific to one OpenFlow version.) */
7201 enum ofpact_type ofpact
; /* Internal name for action type. */
7202 int ofpat
; /* OFPAT_* number from OpenFlow spec. */
7205 static const struct ofpact_map
*
7206 get_ofpact_map(enum ofp_version version
)
7208 /* OpenFlow 1.0 actions. */
7209 static const struct ofpact_map of10
[] = {
7210 { OFPACT_OUTPUT
, 0 },
7211 { OFPACT_SET_VLAN_VID
, 1 },
7212 { OFPACT_SET_VLAN_PCP
, 2 },
7213 { OFPACT_STRIP_VLAN
, 3 },
7214 { OFPACT_SET_ETH_SRC
, 4 },
7215 { OFPACT_SET_ETH_DST
, 5 },
7216 { OFPACT_SET_IPV4_SRC
, 6 },
7217 { OFPACT_SET_IPV4_DST
, 7 },
7218 { OFPACT_SET_IP_DSCP
, 8 },
7219 { OFPACT_SET_L4_SRC_PORT
, 9 },
7220 { OFPACT_SET_L4_DST_PORT
, 10 },
7221 { OFPACT_ENQUEUE
, 11 },
7225 /* OpenFlow 1.1 actions. */
7226 static const struct ofpact_map of11
[] = {
7227 { OFPACT_OUTPUT
, 0 },
7228 { OFPACT_SET_VLAN_VID
, 1 },
7229 { OFPACT_SET_VLAN_PCP
, 2 },
7230 { OFPACT_SET_ETH_SRC
, 3 },
7231 { OFPACT_SET_ETH_DST
, 4 },
7232 { OFPACT_SET_IPV4_SRC
, 5 },
7233 { OFPACT_SET_IPV4_DST
, 6 },
7234 { OFPACT_SET_IP_DSCP
, 7 },
7235 { OFPACT_SET_IP_ECN
, 8 },
7236 { OFPACT_SET_L4_SRC_PORT
, 9 },
7237 { OFPACT_SET_L4_DST_PORT
, 10 },
7238 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7239 /* OFPAT_COPY_TTL_IN (12) not supported. */
7240 { OFPACT_SET_MPLS_LABEL
, 13 },
7241 { OFPACT_SET_MPLS_TC
, 14 },
7242 { OFPACT_SET_MPLS_TTL
, 15 },
7243 { OFPACT_DEC_MPLS_TTL
, 16 },
7244 { OFPACT_PUSH_VLAN
, 17 },
7245 { OFPACT_STRIP_VLAN
, 18 },
7246 { OFPACT_PUSH_MPLS
, 19 },
7247 { OFPACT_POP_MPLS
, 20 },
7248 { OFPACT_SET_QUEUE
, 21 },
7249 { OFPACT_GROUP
, 22 },
7250 { OFPACT_SET_IP_TTL
, 23 },
7251 { OFPACT_DEC_TTL
, 24 },
7255 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
7256 static const struct ofpact_map of12
[] = {
7257 { OFPACT_OUTPUT
, 0 },
7258 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7259 /* OFPAT_COPY_TTL_IN (12) not supported. */
7260 { OFPACT_SET_MPLS_TTL
, 15 },
7261 { OFPACT_DEC_MPLS_TTL
, 16 },
7262 { OFPACT_PUSH_VLAN
, 17 },
7263 { OFPACT_STRIP_VLAN
, 18 },
7264 { OFPACT_PUSH_MPLS
, 19 },
7265 { OFPACT_POP_MPLS
, 20 },
7266 { OFPACT_SET_QUEUE
, 21 },
7267 { OFPACT_GROUP
, 22 },
7268 { OFPACT_SET_IP_TTL
, 23 },
7269 { OFPACT_DEC_TTL
, 24 },
7270 { OFPACT_SET_FIELD
, 25 },
7271 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
7272 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
7293 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
7294 * values, into a bitmap of actions suitable for OpenFlow 'version', and
7295 * returns the result. */
7297 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap
, enum ofp_version version
)
7299 uint32_t openflow_bitmap
= 0;
7300 const struct ofpact_map
*x
;
7302 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
7303 if (ofpacts_bitmap
& (UINT64_C(1) << x
->ofpact
)) {
7304 openflow_bitmap
|= 1u << x
->ofpat
;
7307 return htonl(openflow_bitmap
);
7310 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
7311 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
7312 * and returns the result. */
7314 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap
, enum ofp_version version
)
7316 uint64_t ofpact_bitmap
= 0;
7317 const struct ofpact_map
*x
;
7319 for (x
= get_ofpact_map(version
); x
->ofpat
>= 0; x
++) {
7320 if (ofpat_bitmap
& htonl(1u << x
->ofpat
)) {
7321 ofpact_bitmap
|= UINT64_C(1) << x
->ofpact
;
7324 return ofpact_bitmap
;
7327 /* Appends to 's' a string representation of the set of OFPACT_* represented
7328 * by 'ofpacts_bitmap'. */
7330 ofpact_bitmap_format(uint64_t ofpacts_bitmap
, struct ds
*s
)
7332 if (!ofpacts_bitmap
) {
7333 ds_put_cstr(s
, "<none>");
7335 while (ofpacts_bitmap
) {
7336 ds_put_format(s
, "%s ",
7337 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap
)));
7338 ofpacts_bitmap
= zero_rightmost_1bit(ofpacts_bitmap
);
7344 /* Returns true if 'action' outputs to 'port', false otherwise. */
7346 ofpact_outputs_to_port(const struct ofpact
*ofpact
, ofp_port_t port
)
7348 switch (ofpact
->type
) {
7350 return ofpact_get_OUTPUT(ofpact
)->port
== port
;
7351 case OFPACT_ENQUEUE
:
7352 return ofpact_get_ENQUEUE(ofpact
)->port
== port
;
7353 case OFPACT_CONTROLLER
:
7354 return port
== OFPP_CONTROLLER
;
7356 case OFPACT_OUTPUT_REG
:
7358 case OFPACT_SET_VLAN_VID
:
7359 case OFPACT_SET_VLAN_PCP
:
7360 case OFPACT_STRIP_VLAN
:
7361 case OFPACT_PUSH_VLAN
:
7362 case OFPACT_SET_ETH_SRC
:
7363 case OFPACT_SET_ETH_DST
:
7364 case OFPACT_SET_IPV4_SRC
:
7365 case OFPACT_SET_IPV4_DST
:
7366 case OFPACT_SET_IP_DSCP
:
7367 case OFPACT_SET_IP_ECN
:
7368 case OFPACT_SET_IP_TTL
:
7369 case OFPACT_SET_L4_SRC_PORT
:
7370 case OFPACT_SET_L4_DST_PORT
:
7371 case OFPACT_REG_MOVE
:
7372 case OFPACT_SET_FIELD
:
7373 case OFPACT_STACK_PUSH
:
7374 case OFPACT_STACK_POP
:
7375 case OFPACT_DEC_TTL
:
7376 case OFPACT_SET_MPLS_LABEL
:
7377 case OFPACT_SET_MPLS_TC
:
7378 case OFPACT_SET_MPLS_TTL
:
7379 case OFPACT_DEC_MPLS_TTL
:
7380 case OFPACT_SET_TUNNEL
:
7381 case OFPACT_WRITE_METADATA
:
7382 case OFPACT_SET_QUEUE
:
7383 case OFPACT_POP_QUEUE
:
7384 case OFPACT_FIN_TIMEOUT
:
7385 case OFPACT_RESUBMIT
:
7387 case OFPACT_CONJUNCTION
:
7388 case OFPACT_MULTIPATH
:
7391 case OFPACT_UNROLL_XLATE
:
7392 case OFPACT_PUSH_MPLS
:
7393 case OFPACT_POP_MPLS
:
7395 case OFPACT_CLEAR_ACTIONS
:
7396 case OFPACT_WRITE_ACTIONS
:
7397 case OFPACT_GOTO_TABLE
:
7400 case OFPACT_DEBUG_RECIRC
:
7408 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7409 * to 'port', false otherwise. */
7411 ofpacts_output_to_port(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
7414 const struct ofpact
*a
;
7416 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
7417 if (ofpact_outputs_to_port(a
, port
)) {
7425 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7426 * to 'group', false otherwise. */
7428 ofpacts_output_to_group(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
7431 const struct ofpact
*a
;
7433 OFPACT_FOR_EACH_FLATTENED (a
, ofpacts
, ofpacts_len
) {
7434 if (a
->type
== OFPACT_GROUP
7435 && ofpact_get_GROUP(a
)->group_id
== group_id
) {
7444 ofpacts_equal(const struct ofpact
*a
, size_t a_len
,
7445 const struct ofpact
*b
, size_t b_len
)
7447 return a_len
== b_len
&& !memcmp(a
, b
, a_len
);
7450 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
7451 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
7453 * This function relies on the order of 'ofpacts' being correct (as checked by
7454 * ofpacts_verify()). */
7456 ofpacts_get_meter(const struct ofpact ofpacts
[], size_t ofpacts_len
)
7458 const struct ofpact
*a
;
7460 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7461 enum ovs_instruction_type inst
;
7463 inst
= ovs_instruction_type_from_ofpact_type(a
->type
);
7464 if (a
->type
== OFPACT_METER
) {
7465 return ofpact_get_METER(a
)->meter_id
;
7466 } else if (inst
> OVSINST_OFPIT13_METER
) {
7474 /* Formatting ofpacts. */
7477 ofpact_format(const struct ofpact
*a
, struct ds
*s
)
7480 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7481 case OFPACT_##ENUM: \
7482 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
7491 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
7492 * 'ofpacts' to 'string'. */
7494 ofpacts_format(const struct ofpact
*ofpacts
, size_t ofpacts_len
,
7498 ds_put_format(string
, "%sdrop%s", colors
.drop
, colors
.end
);
7500 const struct ofpact
*a
;
7502 OFPACT_FOR_EACH (a
, ofpacts
, ofpacts_len
) {
7504 ds_put_char(string
, ',');
7507 /* XXX write-actions */
7508 ofpact_format(a
, string
);
7513 /* Internal use by helpers. */
7515 /* Implementation of ofpact_put_<ENUM>(). */
7517 ofpact_put(struct ofpbuf
*ofpacts
, enum ofpact_type type
, size_t len
)
7519 struct ofpact
*ofpact
;
7521 ofpacts
->header
= ofpbuf_put_uninit(ofpacts
, len
);
7522 ofpact
= ofpacts
->header
;
7523 ofpact_init(ofpact
, type
, len
);
7527 /* Implementation of ofpact_init_<ENUM>(). */
7529 ofpact_init(struct ofpact
*ofpact
, enum ofpact_type type
, size_t len
)
7531 memset(ofpact
, 0, len
);
7532 ofpact
->type
= type
;
7537 /* Implementation of ofpact_finish_<ENUM>().
7539 * Finishes composing a variable-length action (begun using
7540 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
7541 * bytes and updating its embedded length field. See the large comment near
7542 * the end of ofp-actions.h for more information.
7544 * May reallocate 'ofpacts'. Callers should consider updating their 'ofpact'
7545 * pointer to the return value of this function. */
7547 ofpact_finish(struct ofpbuf
*ofpacts
, struct ofpact
*ofpact
)
7551 ovs_assert(ofpact
== ofpacts
->header
);
7552 len
= (char *) ofpbuf_tail(ofpacts
) - (char *) ofpact
;
7553 ovs_assert(len
> 0 && len
<= UINT16_MAX
);
7555 ofpbuf_padto(ofpacts
, OFPACT_ALIGN(ofpacts
->size
));
7557 return ofpacts
->header
;
7560 static char * OVS_WARN_UNUSED_RESULT
7561 ofpact_parse(enum ofpact_type type
, char *value
, struct ofpbuf
*ofpacts
,
7562 enum ofputil_protocol
*usable_protocols
)
7565 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7566 case OFPACT_##ENUM: \
7567 return parse_##ENUM(value, ofpacts, usable_protocols);
7576 ofpact_type_from_name(const char *name
, enum ofpact_type
*type
)
7578 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7579 if (!strcasecmp(name, NAME)) { \
7580 *type = OFPACT_##ENUM; \
7589 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
7591 * Returns NULL if successful, otherwise a malloc()'d string describing the
7592 * error. The caller is responsible for freeing the returned string.
7594 * If 'outer_action' is specified, indicates that the actions being parsed
7595 * are nested within another action of the type specified in 'outer_action'. */
7596 static char * OVS_WARN_UNUSED_RESULT
7597 ofpacts_parse__(char *str
, struct ofpbuf
*ofpacts
,
7598 enum ofputil_protocol
*usable_protocols
,
7599 bool allow_instructions
, enum ofpact_type outer_action
)
7608 while (ofputil_parse_key_value(&pos
, &key
, &value
)) {
7609 enum ovs_instruction_type inst
= OVSINST_OFPIT11_APPLY_ACTIONS
;
7610 enum ofpact_type type
;
7614 if (ofpact_type_from_name(key
, &type
)) {
7615 error
= ofpact_parse(type
, value
, ofpacts
, usable_protocols
);
7616 inst
= ovs_instruction_type_from_ofpact_type(type
);
7617 } else if (!strcasecmp(key
, "mod_vlan_vid")) {
7618 error
= parse_set_vlan_vid(value
, ofpacts
, true);
7619 } else if (!strcasecmp(key
, "mod_vlan_pcp")) {
7620 error
= parse_set_vlan_pcp(value
, ofpacts
, true);
7621 } else if (!strcasecmp(key
, "set_nw_ttl")) {
7622 error
= parse_SET_IP_TTL(value
, ofpacts
, usable_protocols
);
7623 } else if (!strcasecmp(key
, "pop_vlan")) {
7624 error
= parse_pop_vlan(ofpacts
);
7625 } else if (!strcasecmp(key
, "set_tunnel64")) {
7626 error
= parse_set_tunnel(value
, ofpacts
,
7627 NXAST_RAW_SET_TUNNEL64
);
7628 } else if (!strcasecmp(key
, "load")) {
7629 error
= parse_reg_load(value
, ofpacts
);
7630 } else if (!strcasecmp(key
, "bundle_load")) {
7631 error
= parse_bundle_load(value
, ofpacts
);
7632 } else if (!strcasecmp(key
, "drop")) {
7634 } else if (!strcasecmp(key
, "apply_actions")) {
7635 return xstrdup("apply_actions is the default instruction");
7636 } else if (ofputil_port_from_string(key
, &port
)) {
7637 ofpact_put_OUTPUT(ofpacts
)->port
= port
;
7639 return xasprintf("unknown action %s", key
);
7645 if (inst
!= OVSINST_OFPIT11_APPLY_ACTIONS
) {
7646 if (!allow_instructions
) {
7647 return xasprintf("only actions are allowed here (not "
7649 ovs_instruction_name_from_type(inst
));
7651 if (inst
== prev_inst
) {
7652 return xasprintf("instruction %s may be specified only once",
7653 ovs_instruction_name_from_type(inst
));
7656 if (prev_inst
!= -1 && inst
< prev_inst
) {
7657 return xasprintf("instruction %s must be specified before %s",
7658 ovs_instruction_name_from_type(inst
),
7659 ovs_instruction_name_from_type(prev_inst
));
7664 if (drop
&& ofpacts
->size
) {
7665 return xstrdup("\"drop\" must not be accompanied by any other action "
7669 retval
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
,
7671 ? (1u << N_OVS_INSTRUCTIONS
) - 1
7672 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS
),
7675 return xstrdup("Incorrect instruction ordering");
7681 static char * OVS_WARN_UNUSED_RESULT
7682 ofpacts_parse(char *str
, struct ofpbuf
*ofpacts
,
7683 enum ofputil_protocol
*usable_protocols
, bool allow_instructions
,
7684 enum ofpact_type outer_action
)
7686 uint32_t orig_size
= ofpacts
->size
;
7687 char *error
= ofpacts_parse__(str
, ofpacts
, usable_protocols
,
7688 allow_instructions
, outer_action
);
7690 ofpacts
->size
= orig_size
;
7695 static char * OVS_WARN_UNUSED_RESULT
7696 ofpacts_parse_copy(const char *s_
, struct ofpbuf
*ofpacts
,
7697 enum ofputil_protocol
*usable_protocols
,
7698 bool allow_instructions
, enum ofpact_type outer_action
)
7702 *usable_protocols
= OFPUTIL_P_ANY
;
7705 error
= ofpacts_parse(s
, ofpacts
, usable_protocols
, allow_instructions
,
7712 /* Parses 's' as a set of OpenFlow actions and appends the actions to
7713 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
7714 * that are nested within the action of type 'outer_action'.
7716 * Returns NULL if successful, otherwise a malloc()'d string describing the
7717 * error. The caller is responsible for freeing the returned string. */
7718 char * OVS_WARN_UNUSED_RESULT
7719 ofpacts_parse_actions(const char *s
, struct ofpbuf
*ofpacts
,
7720 enum ofputil_protocol
*usable_protocols
)
7722 return ofpacts_parse_copy(s
, ofpacts
, usable_protocols
, false, 0);
7725 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
7728 * Returns NULL if successful, otherwise a malloc()'d string describing the
7729 * error. The caller is responsible for freeing the returned string. */
7730 char * OVS_WARN_UNUSED_RESULT
7731 ofpacts_parse_instructions(const char *s
, struct ofpbuf
*ofpacts
,
7732 enum ofputil_protocol
*usable_protocols
)
7734 return ofpacts_parse_copy(s
, ofpacts
, usable_protocols
, true, 0);
7738 ofpact_name(enum ofpact_type type
)
7741 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7748 /* Low-level action decoding and encoding functions. */
7750 /* Everything needed to identify a particular OpenFlow action. */
7751 struct ofpact_hdrs
{
7752 uint32_t vendor
; /* 0 if standard, otherwise a vendor code. */
7753 uint16_t type
; /* Type if standard, otherwise subtype. */
7754 uint8_t ofp_version
; /* From ofp_header. */
7757 /* Information about a particular OpenFlow action. */
7758 struct ofpact_raw_instance
{
7759 /* The action's identity. */
7760 struct ofpact_hdrs hdrs
;
7761 enum ofp_raw_action_type raw
;
7763 /* Looking up the action. */
7764 struct hmap_node decode_node
; /* Based on 'hdrs'. */
7765 struct hmap_node encode_node
; /* Based on 'raw' + 'hdrs.ofp_version'. */
7767 /* The action's encoded size.
7769 * If this action is fixed-length, 'min_length' == 'max_length'.
7770 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7771 * OFP_ACTION_ALIGN) == 65528. */
7772 unsigned short int min_length
;
7773 unsigned short int max_length
;
7775 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7776 * offset of that argument from the beginning of the action and 'arg_len'
7777 * its length, both in bytes.
7779 * For actions that take other forms, these are both zero. */
7780 unsigned short int arg_ofs
;
7781 unsigned short int arg_len
;
7783 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7786 /* If this action is deprecated, a human-readable string with a brief
7788 const char *deprecation
;
7791 /* Action header. */
7792 struct ofp_action_header
{
7793 /* The meaning of other values of 'type' generally depends on the OpenFlow
7794 * version (see enum ofp_raw_action_type).
7796 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7797 * designates an OpenFlow vendor ID and that the remainder of the action
7798 * structure has a vendor-defined meaning.
7800 #define OFPAT_VENDOR 0xffff
7803 /* Always a multiple of 8. */
7806 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7807 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
7810 OFP_ASSERT(sizeof(struct ofp_action_header
) == 8);
7812 /* Header for Nicira-defined actions and for ONF vendor extensions.
7814 * This cannot be used as an entirely generic vendor extension action header,
7815 * because OpenFlow does not specify the location or size of the action
7816 * subtype; it just happens that ONF extensions and Nicira extensions share
7818 struct ext_action_header
{
7819 ovs_be16 type
; /* OFPAT_VENDOR. */
7820 ovs_be16 len
; /* At least 16. */
7821 ovs_be32 vendor
; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
7822 ovs_be16 subtype
; /* See enum ofp_raw_action_type. */
7825 OFP_ASSERT(sizeof(struct ext_action_header
) == 16);
7828 ofpact_hdrs_equal(const struct ofpact_hdrs
*a
,
7829 const struct ofpact_hdrs
*b
)
7831 return (a
->vendor
== b
->vendor
7832 && a
->type
== b
->type
7833 && a
->ofp_version
== b
->ofp_version
);
7837 ofpact_hdrs_hash(const struct ofpact_hdrs
*hdrs
)
7839 return hash_2words(hdrs
->vendor
, (hdrs
->type
<< 16) | hdrs
->ofp_version
);
7842 #include "ofp-actions.inc2"
7844 static struct hmap
*
7845 ofpact_decode_hmap(void)
7847 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
7848 static struct hmap hmap
;
7850 if (ovsthread_once_start(&once
)) {
7851 struct ofpact_raw_instance
*inst
;
7854 for (inst
= all_raw_instances
;
7855 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
7857 hmap_insert(&hmap
, &inst
->decode_node
,
7858 ofpact_hdrs_hash(&inst
->hdrs
));
7860 ovsthread_once_done(&once
);
7865 static struct hmap
*
7866 ofpact_encode_hmap(void)
7868 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
7869 static struct hmap hmap
;
7871 if (ovsthread_once_start(&once
)) {
7872 struct ofpact_raw_instance
*inst
;
7875 for (inst
= all_raw_instances
;
7876 inst
< &all_raw_instances
[ARRAY_SIZE(all_raw_instances
)];
7878 hmap_insert(&hmap
, &inst
->encode_node
,
7879 hash_2words(inst
->raw
, inst
->hdrs
.ofp_version
));
7881 ovsthread_once_done(&once
);
7887 ofpact_decode_raw(enum ofp_version ofp_version
,
7888 const struct ofp_action_header
*oah
, size_t length
,
7889 const struct ofpact_raw_instance
**instp
)
7891 const struct ofpact_raw_instance
*inst
;
7892 struct ofpact_hdrs hdrs
;
7895 if (length
< sizeof *oah
) {
7896 return OFPERR_OFPBAC_BAD_LEN
;
7899 /* Get base action type. */
7900 if (oah
->type
== htons(OFPAT_VENDOR
)) {
7902 hdrs
.vendor
= ntohl(oah
->vendor
);
7903 if (hdrs
.vendor
== NX_VENDOR_ID
|| hdrs
.vendor
== ONF_VENDOR_ID
) {
7904 /* Get extension subtype. */
7905 const struct ext_action_header
*nah
;
7907 nah
= ALIGNED_CAST(const struct ext_action_header
*, oah
);
7908 if (length
< sizeof *nah
) {
7909 return OFPERR_OFPBAC_BAD_LEN
;
7911 hdrs
.type
= ntohs(nah
->subtype
);
7913 VLOG_WARN_RL(&rl
, "OpenFlow action has unknown vendor %#"PRIx32
,
7915 return OFPERR_OFPBAC_BAD_VENDOR
;
7919 hdrs
.type
= ntohs(oah
->type
);
7922 hdrs
.ofp_version
= ofp_version
;
7923 HMAP_FOR_EACH_WITH_HASH (inst
, decode_node
, ofpact_hdrs_hash(&hdrs
),
7924 ofpact_decode_hmap()) {
7925 if (ofpact_hdrs_equal(&hdrs
, &inst
->hdrs
)) {
7932 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7933 : OFPERR_OFPBAC_BAD_TYPE
);
7937 ofpact_pull_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
7938 enum ofp_raw_action_type
*raw
, uint64_t *arg
)
7940 const struct ofp_action_header
*oah
= buf
->data
;
7941 const struct ofpact_raw_instance
*action
;
7942 unsigned int length
;
7946 error
= ofpact_decode_raw(ofp_version
, oah
, buf
->size
, &action
);
7951 if (action
->deprecation
) {
7952 VLOG_INFO_RL(&rl
, "%s is deprecated in %s (%s)",
7953 action
->name
, ofputil_version_to_string(ofp_version
),
7954 action
->deprecation
);
7957 length
= ntohs(oah
->len
);
7958 if (length
> buf
->size
) {
7959 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u exceeds action buffer "
7960 "length %"PRIu32
, action
->name
, length
, buf
->size
);
7961 return OFPERR_OFPBAC_BAD_LEN
;
7963 if (length
< action
->min_length
|| length
> action
->max_length
) {
7964 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u not in valid range "
7965 "[%hu,%hu]", action
->name
, length
,
7966 action
->min_length
, action
->max_length
);
7967 return OFPERR_OFPBAC_BAD_LEN
;
7970 VLOG_WARN_RL(&rl
, "OpenFlow action %s length %u is not a multiple "
7971 "of 8", action
->name
, length
);
7972 return OFPERR_OFPBAC_BAD_LEN
;
7977 if (action
->arg_len
) {
7981 p
= ofpbuf_at_assert(buf
, action
->arg_ofs
, action
->arg_len
);
7982 for (i
= 0; i
< action
->arg_len
; i
++) {
7983 *arg
= (*arg
<< 8) | p
[i
];
7987 ofpbuf_pull(buf
, length
);
7992 static const struct ofpact_raw_instance
*
7993 ofpact_raw_lookup(enum ofp_version ofp_version
, enum ofp_raw_action_type raw
)
7995 const struct ofpact_raw_instance
*inst
;
7997 HMAP_FOR_EACH_WITH_HASH (inst
, encode_node
, hash_2words(raw
, ofp_version
),
7998 ofpact_encode_hmap()) {
7999 if (inst
->raw
== raw
&& inst
->hdrs
.ofp_version
== ofp_version
) {
8007 ofpact_put_raw(struct ofpbuf
*buf
, enum ofp_version ofp_version
,
8008 enum ofp_raw_action_type raw
, uint64_t arg
)
8010 const struct ofpact_raw_instance
*inst
;
8011 struct ofp_action_header
*oah
;
8012 const struct ofpact_hdrs
*hdrs
;
8014 inst
= ofpact_raw_lookup(ofp_version
, raw
);
8017 oah
= ofpbuf_put_zeros(buf
, inst
->min_length
);
8018 oah
->type
= htons(hdrs
->vendor
? OFPAT_VENDOR
: hdrs
->type
);
8019 oah
->len
= htons(inst
->min_length
);
8020 oah
->vendor
= htonl(hdrs
->vendor
);
8022 switch (hdrs
->vendor
) {
8027 case ONF_VENDOR_ID
: {
8028 struct ext_action_header
*nah
= (struct ext_action_header
*) oah
;
8029 nah
->subtype
= htons(hdrs
->type
);
8037 if (inst
->arg_len
) {
8038 uint8_t *p
= (uint8_t *) oah
+ inst
->arg_ofs
+ inst
->arg_len
;
8041 for (i
= 0; i
< inst
->arg_len
; i
++) {
8053 pad_ofpat(struct ofpbuf
*openflow
, size_t start_ofs
)
8055 struct ofp_action_header
*oah
;
8057 ofpbuf_put_zeros(openflow
, PAD_SIZE(openflow
->size
- start_ofs
,
8060 oah
= ofpbuf_at_assert(openflow
, start_ofs
, sizeof *oah
);
8061 oah
->len
= htons(openflow
->size
- start_ofs
);