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 /* NX1.0+(39): struct nx_action_output_trunc. */
305 NXAST_RAW_OUTPUT_TRUNC,
307 /* ## ------------------ ## */
308 /* ## Debugging actions. ## */
309 /* ## ------------------ ## */
311 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
312 /* accepts them only if started with --enable-dummy. */
314 /* NX1.0+(255): void. */
315 NXAST_RAW_DEBUG_RECIRC,
318 /* OpenFlow actions are always a multiple of 8 bytes in length. */
319 #define OFP_ACTION_ALIGN 8
321 /* Define a few functions for working with instructions. */
322 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
323 static inline const struct STRUCT * OVS_UNUSED \
324 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
326 ovs_assert(inst->type == htons(ENUM)); \
327 return ALIGNED_CAST(struct STRUCT *, inst); \
330 static inline void OVS_UNUSED \
331 instruction_init_##ENUM(struct STRUCT *s) \
333 memset(s, 0, sizeof *s); \
334 s->type = htons(ENUM); \
335 s->len = htons(sizeof *s); \
338 static inline struct STRUCT * OVS_UNUSED \
339 instruction_put_##ENUM(struct ofpbuf *buf) \
341 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
342 instruction_init_##ENUM(s); \
348 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
350 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
352 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
353 uint32_t allowed_ovsinsts,
354 enum ofpact_type outer_action);
356 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
357 enum mf_field_id, uint64_t value);
359 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
360 enum ofp_raw_action_type *, uint64_t *arg);
361 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
362 enum ofp_raw_action_type, uint64_t arg);
364 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
365 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
366 bool allow_instructions, enum ofpact_type outer_action);
367 static enum ofperr ofpacts_pull_openflow_actions__(
368 struct ofpbuf *openflow, unsigned int actions_len,
369 enum ofp_version version, uint32_t allowed_ovsinsts,
370 struct ofpbuf *ofpacts, enum ofpact_type outer_action);
371 static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
372 const char *s_, struct ofpbuf *ofpacts,
373 enum ofputil_protocol *usable_protocols,
374 bool allow_instructions, enum ofpact_type outer_action);
376 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
377 * nested ofpacts it returns the first one. */
379 ofpact_next_flattened(const struct ofpact *ofpact)
381 switch (ofpact->type) {
384 case OFPACT_CONTROLLER:
386 case OFPACT_OUTPUT_REG:
387 case OFPACT_OUTPUT_TRUNC:
389 case OFPACT_SET_FIELD:
390 case OFPACT_SET_VLAN_VID:
391 case OFPACT_SET_VLAN_PCP:
392 case OFPACT_STRIP_VLAN:
393 case OFPACT_PUSH_VLAN:
394 case OFPACT_SET_ETH_SRC:
395 case OFPACT_SET_ETH_DST:
396 case OFPACT_SET_IPV4_SRC:
397 case OFPACT_SET_IPV4_DST:
398 case OFPACT_SET_IP_DSCP:
399 case OFPACT_SET_IP_ECN:
400 case OFPACT_SET_IP_TTL:
401 case OFPACT_SET_L4_SRC_PORT:
402 case OFPACT_SET_L4_DST_PORT:
403 case OFPACT_REG_MOVE:
404 case OFPACT_STACK_PUSH:
405 case OFPACT_STACK_POP:
407 case OFPACT_SET_MPLS_LABEL:
408 case OFPACT_SET_MPLS_TC:
409 case OFPACT_SET_MPLS_TTL:
410 case OFPACT_DEC_MPLS_TTL:
411 case OFPACT_PUSH_MPLS:
412 case OFPACT_POP_MPLS:
413 case OFPACT_SET_TUNNEL:
414 case OFPACT_SET_QUEUE:
415 case OFPACT_POP_QUEUE:
416 case OFPACT_FIN_TIMEOUT:
417 case OFPACT_RESUBMIT:
419 case OFPACT_CONJUNCTION:
420 case OFPACT_MULTIPATH:
424 case OFPACT_UNROLL_XLATE:
425 case OFPACT_DEBUG_RECIRC:
427 case OFPACT_CLEAR_ACTIONS:
428 case OFPACT_WRITE_METADATA:
429 case OFPACT_GOTO_TABLE:
431 return ofpact_next(ofpact);
434 return ofpact_get_CT(ofpact)->actions;
436 case OFPACT_WRITE_ACTIONS:
437 return ofpact_get_WRITE_ACTIONS(ofpact)->actions;
443 /* Pull off existing actions or instructions. Used by nesting actions to keep
444 * ofpacts_parse() oblivious of actions nesting.
446 * Push the actions back on after nested parsing, e.g.:
448 * size_t ofs = ofpacts_pull(ofpacts);
449 * ...nested parsing...
450 * ofpbuf_push_uninit(ofpacts, ofs);
453 ofpacts_pull(struct ofpbuf *ofpacts)
458 ofpbuf_pull(ofpacts, ofs);
463 #include "ofp-actions.inc1"
465 /* Output actions. */
467 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
468 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
469 * number of bytes to send. A 'max_len' of zero means no bytes of the
470 * packet should be sent. */
471 struct ofp10_action_output {
472 ovs_be16 type; /* OFPAT10_OUTPUT. */
473 ovs_be16 len; /* Length is 8. */
474 ovs_be16 port; /* Output port. */
475 ovs_be16 max_len; /* Max length to send to controller. */
477 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
479 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
480 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
481 * number of bytes to send. A 'max_len' of zero means no bytes of the
482 * packet should be sent.*/
483 struct ofp11_action_output {
484 ovs_be16 type; /* OFPAT11_OUTPUT. */
485 ovs_be16 len; /* Length is 16. */
486 ovs_be32 port; /* Output port. */
487 ovs_be16 max_len; /* Max length to send to controller. */
488 uint8_t pad[6]; /* Pad to 64 bits. */
490 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
493 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
494 enum ofp_version ofp_version OVS_UNUSED,
497 struct ofpact_output *output;
499 output = ofpact_put_OUTPUT(out);
500 output->port = u16_to_ofp(ntohs(oao->port));
501 output->max_len = ntohs(oao->max_len);
503 return ofpact_check_output_port(output->port, OFPP_MAX);
507 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
508 enum ofp_version ofp_version OVS_UNUSED,
511 struct ofpact_output *output;
514 output = ofpact_put_OUTPUT(out);
515 output->max_len = ntohs(oao->max_len);
517 error = ofputil_port_from_ofp11(oao->port, &output->port);
522 return ofpact_check_output_port(output->port, OFPP_MAX);
526 encode_OUTPUT(const struct ofpact_output *output,
527 enum ofp_version ofp_version, struct ofpbuf *out)
529 if (ofp_version == OFP10_VERSION) {
530 struct ofp10_action_output *oao;
532 oao = put_OFPAT10_OUTPUT(out);
533 oao->port = htons(ofp_to_u16(output->port));
534 oao->max_len = htons(output->max_len);
536 struct ofp11_action_output *oao;
538 oao = put_OFPAT11_OUTPUT(out);
539 oao->port = ofputil_port_to_ofp11(output->port);
540 oao->max_len = htons(output->max_len);
544 static char * OVS_WARN_UNUSED_RESULT
545 parse_truncate_subfield(struct ofpact_output_trunc *output_trunc,
549 char *arg = CONST_CAST(char *, arg_);
551 while (ofputil_parse_key_value(&arg, &key, &value)) {
552 if (!strcmp(key, "port")) {
553 if (!ofputil_port_from_string(value, &output_trunc->port)) {
554 return xasprintf("output to unknown truncate port: %s",
557 if (ofp_to_u16(output_trunc->port) > ofp_to_u16(OFPP_MAX)) {
558 if (output_trunc->port != OFPP_LOCAL &&
559 output_trunc->port != OFPP_IN_PORT)
560 return xasprintf("output to unsupported truncate port: %s",
563 } else if (!strcmp(key, "max_len")) {
566 err = str_to_u32(value, &output_trunc->max_len);
571 return xasprintf("invalid key '%s' in output_trunc argument",
578 static char * OVS_WARN_UNUSED_RESULT
579 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
580 enum ofputil_protocol *usable_protocols OVS_UNUSED)
582 if (strchr(arg, '[')) {
583 struct ofpact_output_reg *output_reg;
585 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
586 output_reg->max_len = UINT16_MAX;
587 return mf_parse_subfield(&output_reg->src, arg);
588 } else if (strstr(arg, "port") && strstr(arg, "max_len")) {
589 struct ofpact_output_trunc *output_trunc;
591 output_trunc = ofpact_put_OUTPUT_TRUNC(ofpacts);
592 return parse_truncate_subfield(output_trunc, arg);
594 struct ofpact_output *output;
596 output = ofpact_put_OUTPUT(ofpacts);
597 if (!ofputil_port_from_string(arg, &output->port)) {
598 return xasprintf("%s: output to unknown port", arg);
600 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
606 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
608 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
609 ds_put_format(s, "%soutput:%s%"PRIu16,
610 colors.special, colors.end, a->port);
612 ofputil_format_port(a->port, s);
613 if (a->port == OFPP_CONTROLLER) {
614 ds_put_format(s, ":%"PRIu16, a->max_len);
622 decode_OFPAT_RAW11_GROUP(uint32_t group_id,
623 enum ofp_version ofp_version OVS_UNUSED,
626 ofpact_put_GROUP(out)->group_id = group_id;
631 encode_GROUP(const struct ofpact_group *group,
632 enum ofp_version ofp_version, struct ofpbuf *out)
634 if (ofp_version == OFP10_VERSION) {
637 put_OFPAT11_GROUP(out, group->group_id);
641 static char * OVS_WARN_UNUSED_RESULT
642 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
643 enum ofputil_protocol *usable_protocols OVS_UNUSED)
645 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
649 format_GROUP(const struct ofpact_group *a, struct ds *s)
651 ds_put_format(s, "%sgroup:%s%"PRIu32,
652 colors.special, colors.end, a->group_id);
655 /* Action structure for NXAST_CONTROLLER.
657 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
658 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
661 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
663 * - 'controller_id': The ID of the controller connection to which the
664 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
665 * sent only to controllers that have the specified controller connection
666 * ID. See "struct nx_controller_id" for more information. */
667 struct nx_action_controller {
668 ovs_be16 type; /* OFPAT_VENDOR. */
669 ovs_be16 len; /* Length is 16. */
670 ovs_be32 vendor; /* NX_VENDOR_ID. */
671 ovs_be16 subtype; /* NXAST_CONTROLLER. */
672 ovs_be16 max_len; /* Maximum length to send to controller. */
673 ovs_be16 controller_id; /* Controller ID to send packet-in. */
674 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
675 uint8_t zero; /* Must be zero. */
677 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
679 /* Properties for NXAST_CONTROLLER2.
681 * For more information on the effect of NXAC2PT_PAUSE, see the large comment
682 * on NXT_PACKET_IN2 in nicira-ext.h */
683 enum nx_action_controller2_prop_type {
684 NXAC2PT_MAX_LEN, /* ovs_be16 max bytes to send (default all). */
685 NXAC2PT_CONTROLLER_ID, /* ovs_be16 dest controller ID (default 0). */
686 NXAC2PT_REASON, /* uint8_t reason (OFPR_*), default 0. */
687 NXAC2PT_USERDATA, /* Data to copy into NXPINT_USERDATA. */
688 NXAC2PT_PAUSE, /* Flag to pause pipeline to resume later. */
691 /* Action structure for NXAST_CONTROLLER2.
693 * This replacement for NXAST_CONTROLLER makes it extensible via properties. */
694 struct nx_action_controller2 {
695 ovs_be16 type; /* OFPAT_VENDOR. */
696 ovs_be16 len; /* Length is 16 or more. */
697 ovs_be32 vendor; /* NX_VENDOR_ID. */
698 ovs_be16 subtype; /* NXAST_CONTROLLER2. */
699 uint8_t zeros[6]; /* Must be zero. */
700 /* Followed by NXAC2PT_* properties. */
702 OFP_ASSERT(sizeof(struct nx_action_controller2) == 16);
705 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
706 enum ofp_version ofp_version OVS_UNUSED,
709 struct ofpact_controller *oc;
711 oc = ofpact_put_CONTROLLER(out);
712 oc->ofpact.raw = NXAST_RAW_CONTROLLER;
713 oc->max_len = ntohs(nac->max_len);
714 oc->controller_id = ntohs(nac->controller_id);
715 oc->reason = nac->reason;
716 ofpact_finish_CONTROLLER(out, &oc);
722 decode_NXAST_RAW_CONTROLLER2(const struct nx_action_controller2 *nac2,
723 enum ofp_version ofp_version OVS_UNUSED,
726 if (!is_all_zeros(nac2->zeros, sizeof nac2->zeros)) {
727 return OFPERR_NXBRC_MUST_BE_ZERO;
730 size_t start_ofs = out->size;
731 struct ofpact_controller *oc = ofpact_put_CONTROLLER(out);
732 oc->ofpact.raw = NXAST_RAW_CONTROLLER2;
733 oc->max_len = UINT16_MAX;
734 oc->reason = OFPR_ACTION;
736 struct ofpbuf properties;
737 ofpbuf_use_const(&properties, nac2, ntohs(nac2->len));
738 ofpbuf_pull(&properties, sizeof *nac2);
740 while (properties.size > 0) {
741 struct ofpbuf payload;
744 enum ofperr error = ofpprop_pull(&properties, &payload, &type);
750 case NXAC2PT_MAX_LEN:
751 error = ofpprop_parse_u16(&payload, &oc->max_len);
754 case NXAC2PT_CONTROLLER_ID:
755 error = ofpprop_parse_u16(&payload, &oc->controller_id);
758 case NXAC2PT_REASON: {
760 error = ofpprop_parse_u8(&payload, &u8);
765 case NXAC2PT_USERDATA:
766 out->size = start_ofs + OFPACT_CONTROLLER_SIZE;
767 ofpbuf_put(out, payload.msg, ofpbuf_msgsize(&payload));
768 oc = ofpbuf_at_assert(out, start_ofs, sizeof *oc);
769 oc->userdata_len = ofpbuf_msgsize(&payload);
777 error = OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type);
785 ofpact_finish_CONTROLLER(out, &oc);
791 encode_CONTROLLER(const struct ofpact_controller *controller,
792 enum ofp_version ofp_version OVS_UNUSED,
795 if (controller->userdata_len
797 || controller->ofpact.raw == NXAST_RAW_CONTROLLER2) {
798 size_t start_ofs = out->size;
799 put_NXAST_CONTROLLER2(out);
800 if (controller->max_len != UINT16_MAX) {
801 ofpprop_put_u16(out, NXAC2PT_MAX_LEN, controller->max_len);
803 if (controller->controller_id != 0) {
804 ofpprop_put_u16(out, NXAC2PT_CONTROLLER_ID,
805 controller->controller_id);
807 if (controller->reason != OFPR_ACTION) {
808 ofpprop_put_u8(out, NXAC2PT_REASON, controller->reason);
810 if (controller->userdata_len != 0) {
811 ofpprop_put(out, NXAC2PT_USERDATA, controller->userdata,
812 controller->userdata_len);
814 if (controller->pause) {
815 ofpprop_put_flag(out, NXAC2PT_PAUSE);
817 pad_ofpat(out, start_ofs);
819 struct nx_action_controller *nac;
821 nac = put_NXAST_CONTROLLER(out);
822 nac->max_len = htons(controller->max_len);
823 nac->controller_id = htons(controller->controller_id);
824 nac->reason = controller->reason;
828 static char * OVS_WARN_UNUSED_RESULT
829 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
830 enum ofputil_protocol *usable_protocols OVS_UNUSED)
832 enum ofp_packet_in_reason reason = OFPR_ACTION;
833 uint16_t controller_id = 0;
834 uint16_t max_len = UINT16_MAX;
835 const char *userdata = NULL;
840 } else if (strspn(arg, "0123456789") == strlen(arg)) {
841 char *error = str_to_u16(arg, "max_len", &max_len);
848 while (ofputil_parse_key_value(&arg, &name, &value)) {
849 if (!strcmp(name, "reason")) {
850 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
851 return xasprintf("unknown reason \"%s\"", value);
853 } else if (!strcmp(name, "max_len")) {
854 char *error = str_to_u16(value, "max_len", &max_len);
858 } else if (!strcmp(name, "id")) {
859 char *error = str_to_u16(value, "id", &controller_id);
863 } else if (!strcmp(name, "userdata")) {
865 } else if (!strcmp(name, "pause")) {
868 return xasprintf("unknown key \"%s\" parsing controller "
874 if (reason == OFPR_ACTION && controller_id == 0 && !userdata && !pause) {
875 struct ofpact_output *output;
877 output = ofpact_put_OUTPUT(ofpacts);
878 output->port = OFPP_CONTROLLER;
879 output->max_len = max_len;
881 struct ofpact_controller *controller;
883 controller = ofpact_put_CONTROLLER(ofpacts);
884 controller->max_len = max_len;
885 controller->reason = reason;
886 controller->controller_id = controller_id;
887 controller->pause = pause;
890 size_t start_ofs = ofpacts->size;
891 const char *end = ofpbuf_put_hex(ofpacts, userdata, NULL);
893 return xstrdup("bad hex digit in `controller' "
894 "action `userdata'");
896 size_t userdata_len = ofpacts->size - start_ofs;
897 controller = ofpacts->header;
898 controller->userdata_len = userdata_len;
900 ofpact_finish_CONTROLLER(ofpacts, &controller);
907 format_hex_arg(struct ds *s, const uint8_t *data, size_t len)
909 for (size_t i = 0; i < len; i++) {
913 ds_put_format(s, "%02"PRIx8, data[i]);
918 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
920 if (a->reason == OFPR_ACTION && !a->controller_id && !a->userdata_len
922 ds_put_format(s, "%sCONTROLLER:%s%"PRIu16,
923 colors.special, colors.end, a->max_len);
925 enum ofp_packet_in_reason reason = a->reason;
927 ds_put_format(s, "%scontroller(%s", colors.paren, colors.end);
928 if (reason != OFPR_ACTION) {
929 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
931 ds_put_format(s, "%sreason=%s%s,", colors.param, colors.end,
932 ofputil_packet_in_reason_to_string(
933 reason, reasonbuf, sizeof reasonbuf));
935 if (a->max_len != UINT16_MAX) {
936 ds_put_format(s, "%smax_len=%s%"PRIu16",",
937 colors.param, colors.end, a->max_len);
939 if (a->controller_id != 0) {
940 ds_put_format(s, "%sid=%s%"PRIu16",",
941 colors.param, colors.end, a->controller_id);
943 if (a->userdata_len) {
944 ds_put_format(s, "%suserdata=%s", colors.param, colors.end);
945 format_hex_arg(s, a->userdata, a->userdata_len);
949 ds_put_format(s, "%spause%s,", colors.value, colors.end);
952 ds_put_format(s, "%s)%s", colors.paren, colors.end);
956 /* Enqueue action. */
957 struct ofp10_action_enqueue {
958 ovs_be16 type; /* OFPAT10_ENQUEUE. */
959 ovs_be16 len; /* Len is 16. */
960 ovs_be16 port; /* Port that queue belongs. Should
961 refer to a valid physical port
962 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
963 uint8_t pad[6]; /* Pad for 64-bit alignment. */
964 ovs_be32 queue_id; /* Where to enqueue the packets. */
966 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
969 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
970 enum ofp_version ofp_version OVS_UNUSED,
973 struct ofpact_enqueue *enqueue;
975 enqueue = ofpact_put_ENQUEUE(out);
976 enqueue->port = u16_to_ofp(ntohs(oae->port));
977 enqueue->queue = ntohl(oae->queue_id);
978 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
979 && enqueue->port != OFPP_IN_PORT
980 && enqueue->port != OFPP_LOCAL) {
981 return OFPERR_OFPBAC_BAD_OUT_PORT;
987 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
988 enum ofp_version ofp_version, struct ofpbuf *out)
990 if (ofp_version == OFP10_VERSION) {
991 struct ofp10_action_enqueue *oae;
993 oae = put_OFPAT10_ENQUEUE(out);
994 oae->port = htons(ofp_to_u16(enqueue->port));
995 oae->queue_id = htonl(enqueue->queue);
1001 static char * OVS_WARN_UNUSED_RESULT
1002 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
1003 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1006 char *port = strtok_r(arg, ":q,", &sp);
1007 char *queue = strtok_r(NULL, "", &sp);
1008 struct ofpact_enqueue *enqueue;
1010 if (port == NULL || queue == NULL) {
1011 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
1012 "\"enqueue(PORT,QUEUE)\"");
1015 enqueue = ofpact_put_ENQUEUE(ofpacts);
1016 if (!ofputil_port_from_string(port, &enqueue->port)) {
1017 return xasprintf("%s: enqueue to unknown port", port);
1019 return str_to_u32(queue, &enqueue->queue);
1023 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
1025 ds_put_format(s, "%senqueue:%s", colors.param, colors.end);
1026 ofputil_format_port(a->port, s);
1027 ds_put_format(s, ":%"PRIu32, a->queue);
1030 /* Action structure for NXAST_OUTPUT_REG.
1032 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
1034 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
1035 * the NXAST_REG_LOAD action.
1037 * The acceptable nxm_header values for 'src' are the same as the acceptable
1038 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
1040 * The 'max_len' field indicates the number of bytes to send when the chosen
1041 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
1042 * field of OFPAT_OUTPUT.
1044 * The 'zero' field is required to be zeroed for forward compatibility. */
1045 struct nx_action_output_reg {
1046 ovs_be16 type; /* OFPAT_VENDOR. */
1047 ovs_be16 len; /* 24. */
1048 ovs_be32 vendor; /* NX_VENDOR_ID. */
1049 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
1051 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1052 ovs_be32 src; /* Source. */
1054 ovs_be16 max_len; /* Max length to send to controller. */
1056 uint8_t zero[6]; /* Reserved, must be zero. */
1058 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
1060 /* Action structure for NXAST_OUTPUT_REG2.
1062 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
1063 * experimenter OXM as 'src'.
1065 struct nx_action_output_reg2 {
1066 ovs_be16 type; /* OFPAT_VENDOR. */
1067 ovs_be16 len; /* 24. */
1068 ovs_be32 vendor; /* NX_VENDOR_ID. */
1069 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
1071 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1072 ovs_be16 max_len; /* Max length to send to controller. */
1075 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1076 * - Enough 0-bytes to pad the action out to 24 bytes. */
1079 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
1082 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
1083 enum ofp_version ofp_version OVS_UNUSED,
1086 struct ofpact_output_reg *output_reg;
1088 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
1089 return OFPERR_OFPBAC_BAD_ARGUMENT;
1092 output_reg = ofpact_put_OUTPUT_REG(out);
1093 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
1094 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
1095 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1096 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1097 output_reg->max_len = ntohs(naor->max_len);
1099 return mf_check_src(&output_reg->src, NULL);
1103 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
1104 enum ofp_version ofp_version OVS_UNUSED,
1107 struct ofpact_output_reg *output_reg;
1108 output_reg = ofpact_put_OUTPUT_REG(out);
1109 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
1110 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1111 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1112 output_reg->max_len = ntohs(naor->max_len);
1114 struct ofpbuf b = ofpbuf_const_initializer(naor, ntohs(naor->len));
1115 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
1117 enum ofperr error = nx_pull_header(&b, &output_reg->src.field, NULL);
1121 if (!is_all_zeros(b.data, b.size)) {
1122 return OFPERR_NXBRC_MUST_BE_ZERO;
1125 return mf_check_src(&output_reg->src, NULL);
1129 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
1130 enum ofp_version ofp_version OVS_UNUSED,
1133 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1134 * cannot be encoded in the older form, encode it as
1135 * NXAST_RAW_OUTPUT_REG2. */
1136 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
1137 || !mf_nxm_header(output_reg->src.field->id)) {
1138 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
1139 size_t size = out->size;
1141 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1142 output_reg->src.n_bits);
1143 naor->max_len = htons(output_reg->max_len);
1145 out->size = size - sizeof naor->pad;
1146 nx_put_header(out, output_reg->src.field->id, 0, false);
1149 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
1151 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1152 output_reg->src.n_bits);
1153 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
1154 naor->max_len = htons(output_reg->max_len);
1158 static char * OVS_WARN_UNUSED_RESULT
1159 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
1160 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1162 return parse_OUTPUT(arg, ofpacts, usable_protocols);
1166 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
1168 ds_put_format(s, "%soutput:%s", colors.special, colors.end);
1169 mf_format_subfield(&a->src, s);
1172 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1174 * The bundle actions choose a slave from a supplied list of options.
1175 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
1176 * selection to a register.
1178 * The list of possible slaves follows the nx_action_bundle structure. The size
1179 * of each slave is governed by its type as indicated by the 'slave_type'
1180 * parameter. The list of slaves should be padded at its end with zeros to make
1181 * the total length of the action a multiple of 8.
1183 * Switches infer from the 'slave_type' parameter the size of each slave. All
1184 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
1185 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
1186 * 2 byte width. Switches should reject actions which indicate unknown or
1187 * unsupported slave types.
1189 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1190 * slave. If the switch does not support the specified 'algorithm' parameter,
1191 * it should reject the action.
1193 * Several algorithms take into account liveness when selecting slaves. The
1194 * liveness of a slave is implementation defined (with one exception), but will
1195 * generally take into account things like its carrier status and the results
1196 * of any link monitoring protocols which happen to be running on it. In order
1197 * to give controllers a place-holder value, the OFPP_NONE port is always
1200 * Some slave selection strategies require the use of a hash function, in which
1201 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
1202 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
1203 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
1204 * 'basis' parameter is used as a universal hash parameter. Different values
1205 * of 'basis' yield different hash results.
1207 * The 'zero' parameter at the end of the action structure is reserved for
1208 * future use. Switches are required to reject actions which have nonzero
1209 * bytes in the 'zero' field.
1211 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
1212 * should reject actions which have nonzero bytes in either of these fields.
1214 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
1215 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
1216 * similar to those for the NXAST_REG_LOAD action. */
1217 struct nx_action_bundle {
1218 ovs_be16 type; /* OFPAT_VENDOR. */
1219 ovs_be16 len; /* Length including slaves. */
1220 ovs_be32 vendor; /* NX_VENDOR_ID. */
1221 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1223 /* Slave choice algorithm to apply to hash value. */
1224 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
1226 /* What fields to hash and how. */
1227 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
1228 ovs_be16 basis; /* Universal hash parameter. */
1230 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
1231 ovs_be16 n_slaves; /* Number of slaves. */
1233 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1234 ovs_be32 dst; /* Destination. */
1236 uint8_t zero[4]; /* Reserved. Must be zero. */
1238 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
1241 decode_bundle(bool load, const struct nx_action_bundle *nab,
1242 struct ofpbuf *ofpacts)
1244 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1245 struct ofpact_bundle *bundle;
1246 uint32_t slave_type;
1247 size_t slaves_size, i;
1250 bundle = ofpact_put_BUNDLE(ofpacts);
1252 bundle->n_slaves = ntohs(nab->n_slaves);
1253 bundle->basis = ntohs(nab->basis);
1254 bundle->fields = ntohs(nab->fields);
1255 bundle->algorithm = ntohs(nab->algorithm);
1256 slave_type = ntohl(nab->slave_type);
1257 slaves_size = ntohs(nab->len) - sizeof *nab;
1259 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1260 if (!flow_hash_fields_valid(bundle->fields)) {
1261 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
1262 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
1263 VLOG_WARN_RL(&rl, "too many slaves");
1264 } else if (bundle->algorithm != NX_BD_ALG_HRW
1265 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
1266 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
1267 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
1268 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
1273 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
1274 VLOG_WARN_RL(&rl, "reserved field is nonzero");
1275 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1279 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
1280 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
1281 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
1283 if (bundle->dst.n_bits < 16) {
1284 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
1286 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1289 if (nab->ofs_nbits || nab->dst) {
1290 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
1291 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1295 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1296 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1297 "allocated for slaves. %"PRIuSIZE" bytes are required "
1298 "for %"PRIu16" slaves.",
1299 load ? "bundle_load" : "bundle", slaves_size,
1300 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1301 error = OFPERR_OFPBAC_BAD_LEN;
1304 for (i = 0; i < bundle->n_slaves; i++) {
1305 ofp_port_t ofp_port = u16_to_ofp(ntohs(((ovs_be16 *)(nab + 1))[i]));
1306 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1307 bundle = ofpacts->header;
1310 ofpact_finish_BUNDLE(ofpacts, &bundle);
1312 error = bundle_check(bundle, OFPP_MAX, NULL);
1318 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1319 enum ofp_version ofp_version OVS_UNUSED,
1322 return decode_bundle(false, nab, out);
1326 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1327 enum ofp_version ofp_version OVS_UNUSED,
1330 return decode_bundle(true, nab, out);
1334 encode_BUNDLE(const struct ofpact_bundle *bundle,
1335 enum ofp_version ofp_version OVS_UNUSED,
1338 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1339 struct nx_action_bundle *nab;
1343 nab = (bundle->dst.field
1344 ? put_NXAST_BUNDLE_LOAD(out)
1345 : put_NXAST_BUNDLE(out));
1346 nab->len = htons(ntohs(nab->len) + slaves_len);
1347 nab->algorithm = htons(bundle->algorithm);
1348 nab->fields = htons(bundle->fields);
1349 nab->basis = htons(bundle->basis);
1350 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1351 nab->n_slaves = htons(bundle->n_slaves);
1352 if (bundle->dst.field) {
1353 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1354 bundle->dst.n_bits);
1355 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1358 slaves = ofpbuf_put_zeros(out, slaves_len);
1359 for (i = 0; i < bundle->n_slaves; i++) {
1360 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1364 static char * OVS_WARN_UNUSED_RESULT
1365 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1366 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1368 return bundle_parse(arg, ofpacts);
1371 static char * OVS_WARN_UNUSED_RESULT
1372 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1374 return bundle_parse_load(arg, ofpacts);
1378 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1380 bundle_format(a, s);
1383 /* Set VLAN actions. */
1386 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1389 return OFPERR_OFPBAC_BAD_ARGUMENT;
1391 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1392 vlan_vid->vlan_vid = vid;
1393 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1399 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1400 enum ofp_version ofp_version OVS_UNUSED,
1403 return decode_set_vlan_vid(vid, true, out);
1407 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1408 enum ofp_version ofp_version OVS_UNUSED,
1411 return decode_set_vlan_vid(vid, false, out);
1415 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1416 enum ofp_version ofp_version, struct ofpbuf *out)
1418 uint16_t vid = vlan_vid->vlan_vid;
1420 /* Push a VLAN tag, if none is present and this form of the action calls
1421 * for such a feature. */
1422 if (ofp_version > OFP10_VERSION
1423 && vlan_vid->push_vlan_if_needed
1424 && !vlan_vid->flow_has_vlan) {
1425 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1428 if (ofp_version == OFP10_VERSION) {
1429 put_OFPAT10_SET_VLAN_VID(out, vid);
1430 } else if (ofp_version == OFP11_VERSION) {
1431 put_OFPAT11_SET_VLAN_VID(out, vid);
1433 ofpact_put_set_field(out, ofp_version,
1434 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1438 static char * OVS_WARN_UNUSED_RESULT
1439 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1441 struct ofpact_vlan_vid *vlan_vid;
1445 error = str_to_u16(arg, "VLAN VID", &vid);
1450 if (vid & ~VLAN_VID_MASK) {
1451 return xasprintf("%s: not a valid VLAN VID", arg);
1453 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1454 vlan_vid->vlan_vid = vid;
1455 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1459 static char * OVS_WARN_UNUSED_RESULT
1460 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1461 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1463 return parse_set_vlan_vid(arg, ofpacts, false);
1467 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1469 ds_put_format(s, "%s%s:%s%"PRIu16, colors.param,
1470 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1471 colors.end, a->vlan_vid);
1474 /* Set PCP actions. */
1477 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1480 return OFPERR_OFPBAC_BAD_ARGUMENT;
1482 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1483 vlan_pcp->vlan_pcp = pcp;
1484 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1490 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1491 enum ofp_version ofp_version OVS_UNUSED,
1494 return decode_set_vlan_pcp(pcp, true, out);
1498 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1499 enum ofp_version ofp_version OVS_UNUSED,
1502 return decode_set_vlan_pcp(pcp, false, out);
1506 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1507 enum ofp_version ofp_version, struct ofpbuf *out)
1509 uint8_t pcp = vlan_pcp->vlan_pcp;
1511 /* Push a VLAN tag, if none is present and this form of the action calls
1512 * for such a feature. */
1513 if (ofp_version > OFP10_VERSION
1514 && vlan_pcp->push_vlan_if_needed
1515 && !vlan_pcp->flow_has_vlan) {
1516 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1519 if (ofp_version == OFP10_VERSION) {
1520 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1521 } else if (ofp_version == OFP11_VERSION) {
1522 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1524 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1528 static char * OVS_WARN_UNUSED_RESULT
1529 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1531 struct ofpact_vlan_pcp *vlan_pcp;
1535 error = str_to_u8(arg, "VLAN PCP", &pcp);
1541 return xasprintf("%s: not a valid VLAN PCP", arg);
1543 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1544 vlan_pcp->vlan_pcp = pcp;
1545 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1549 static char * OVS_WARN_UNUSED_RESULT
1550 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1551 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1553 return parse_set_vlan_pcp(arg, ofpacts, false);
1557 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1559 ds_put_format(s, "%s%s:%s%"PRIu8, colors.param,
1560 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1561 colors.end, a->vlan_pcp);
1564 /* Strip VLAN actions. */
1567 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1569 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1574 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1576 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1581 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1582 enum ofp_version ofp_version, struct ofpbuf *out)
1584 if (ofp_version == OFP10_VERSION) {
1585 put_OFPAT10_STRIP_VLAN(out);
1587 put_OFPAT11_POP_VLAN(out);
1591 static char * OVS_WARN_UNUSED_RESULT
1592 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1593 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1595 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1599 static char * OVS_WARN_UNUSED_RESULT
1600 parse_pop_vlan(struct ofpbuf *ofpacts)
1602 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1607 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1609 ds_put_format(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1611 : "%sstrip_vlan%s"),
1612 colors.value, colors.end);
1615 /* Push VLAN action. */
1618 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1619 enum ofp_version ofp_version OVS_UNUSED,
1622 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1623 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1624 return OFPERR_OFPBAC_BAD_ARGUMENT;
1626 ofpact_put_PUSH_VLAN(out);
1631 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1632 enum ofp_version ofp_version, struct ofpbuf *out)
1634 if (ofp_version == OFP10_VERSION) {
1635 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1636 * follow this action. */
1638 /* XXX ETH_TYPE_VLAN_8021AD case */
1639 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1643 static char * OVS_WARN_UNUSED_RESULT
1644 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1645 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1650 *usable_protocols &= OFPUTIL_P_OF11_UP;
1651 error = str_to_u16(arg, "ethertype", ðertype);
1656 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1657 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1658 return xasprintf("%s: not a valid VLAN ethertype", arg);
1661 ofpact_put_PUSH_VLAN(ofpacts);
1666 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1668 /* XXX 802.1AD case*/
1669 ds_put_format(s, "%spush_vlan:%s%#"PRIx16,
1670 colors.param, colors.end, ETH_TYPE_VLAN_8021Q);
1673 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1674 struct ofp_action_dl_addr {
1675 ovs_be16 type; /* Type. */
1676 ovs_be16 len; /* Length is 16. */
1677 struct eth_addr dl_addr; /* Ethernet address. */
1680 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1683 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1684 enum ofp_version ofp_version OVS_UNUSED,
1687 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1692 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1693 enum ofp_version ofp_version OVS_UNUSED,
1696 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1701 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1702 enum ofp_raw_action_type raw, enum mf_field_id field,
1705 if (ofp_version < OFP12_VERSION) {
1706 struct ofp_action_dl_addr *oada;
1708 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1709 oada->dl_addr = mac->mac;
1711 ofpact_put_set_field(out, ofp_version, field,
1712 eth_addr_to_uint64(mac->mac));
1717 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1720 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1726 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1727 enum ofp_version ofp_version,
1730 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1735 static char * OVS_WARN_UNUSED_RESULT
1736 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1737 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1739 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1742 static char * OVS_WARN_UNUSED_RESULT
1743 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1744 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1746 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1750 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1752 ds_put_format(s, "%smod_dl_src:%s"ETH_ADDR_FMT,
1753 colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
1757 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1759 ds_put_format(s, "%smod_dl_dst:%s"ETH_ADDR_FMT,
1760 colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
1763 /* Set IPv4 address actions. */
1766 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1767 enum ofp_version ofp_version OVS_UNUSED,
1770 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1775 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1776 enum ofp_version ofp_version OVS_UNUSED,
1779 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1784 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1785 enum ofp_version ofp_version,
1786 enum ofp_raw_action_type raw, enum mf_field_id field,
1789 ovs_be32 addr = ipv4->ipv4;
1790 if (ofp_version < OFP12_VERSION) {
1791 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1793 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1798 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1799 enum ofp_version ofp_version, struct ofpbuf *out)
1801 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1806 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1807 enum ofp_version ofp_version, struct ofpbuf *out)
1809 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1813 static char * OVS_WARN_UNUSED_RESULT
1814 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1815 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1817 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1820 static char * OVS_WARN_UNUSED_RESULT
1821 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1822 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1824 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1828 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1830 ds_put_format(s, "%smod_nw_src:%s"IP_FMT,
1831 colors.param, colors.end, IP_ARGS(a->ipv4));
1835 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1837 ds_put_format(s, "%smod_nw_dst:%s"IP_FMT,
1838 colors.param, colors.end, IP_ARGS(a->ipv4));
1841 /* Set IPv4/v6 TOS actions. */
1844 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1845 enum ofp_version ofp_version OVS_UNUSED,
1848 if (dscp & ~IP_DSCP_MASK) {
1849 return OFPERR_OFPBAC_BAD_ARGUMENT;
1851 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1857 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1858 enum ofp_version ofp_version, struct ofpbuf *out)
1860 if (ofp_version < OFP12_VERSION) {
1861 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1863 ofpact_put_set_field(out, ofp_version,
1864 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1868 static char * OVS_WARN_UNUSED_RESULT
1869 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1870 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1875 error = str_to_u8(arg, "TOS", &tos);
1880 if (tos & ~IP_DSCP_MASK) {
1881 return xasprintf("%s: not a valid TOS", arg);
1883 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1888 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1890 ds_put_format(s, "%smod_nw_tos:%s%d", colors.param, colors.end, a->dscp);
1893 /* Set IPv4/v6 ECN actions. */
1896 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1897 enum ofp_version ofp_version OVS_UNUSED,
1900 if (ecn & ~IP_ECN_MASK) {
1901 return OFPERR_OFPBAC_BAD_ARGUMENT;
1903 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1909 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1910 enum ofp_version ofp_version, struct ofpbuf *out)
1912 uint8_t ecn = ip_ecn->ecn;
1913 if (ofp_version == OFP10_VERSION) {
1915 } else if (ofp_version == OFP11_VERSION) {
1916 put_OFPAT11_SET_NW_ECN(out, ecn);
1918 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1922 static char * OVS_WARN_UNUSED_RESULT
1923 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1924 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1929 error = str_to_u8(arg, "ECN", &ecn);
1934 if (ecn & ~IP_ECN_MASK) {
1935 return xasprintf("%s: not a valid ECN", arg);
1937 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1942 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1944 ds_put_format(s, "%smod_nw_ecn:%s%d",
1945 colors.param, colors.end, a->ecn);
1948 /* Set IPv4/v6 TTL actions. */
1951 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1952 enum ofp_version ofp_version OVS_UNUSED,
1955 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1960 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1961 enum ofp_version ofp_version, struct ofpbuf *out)
1963 if (ofp_version >= OFP11_VERSION) {
1964 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1970 static char * OVS_WARN_UNUSED_RESULT
1971 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1972 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1977 error = str_to_u8(arg, "TTL", &ttl);
1982 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1987 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1989 ds_put_format(s, "%smod_nw_ttl:%s%d", colors.param, colors.end, a->ttl);
1992 /* Set TCP/UDP/SCTP port actions. */
1995 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1996 enum ofp_version ofp_version OVS_UNUSED,
1999 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
2004 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
2005 enum ofp_version ofp_version OVS_UNUSED,
2008 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
2013 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
2014 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
2015 enum mf_field_id field, struct ofpbuf *out)
2017 uint16_t port = l4_port->port;
2019 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
2020 ofpact_put_set_field(out, ofp_version, field, port);
2022 ofpact_put_raw(out, ofp_version, raw, port);
2027 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
2028 enum ofp_version ofp_version, struct ofpbuf *out)
2030 uint8_t proto = l4_port->flow_ip_proto;
2031 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
2032 : proto == IPPROTO_UDP ? MFF_UDP_SRC
2033 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
2036 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
2040 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
2041 enum ofp_version ofp_version,
2044 uint8_t proto = l4_port->flow_ip_proto;
2045 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
2046 : proto == IPPROTO_UDP ? MFF_UDP_DST
2047 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
2050 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
2053 static char * OVS_WARN_UNUSED_RESULT
2054 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
2055 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2057 return str_to_u16(arg, "source port",
2058 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
2061 static char * OVS_WARN_UNUSED_RESULT
2062 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
2063 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2065 return str_to_u16(arg, "destination port",
2066 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
2070 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
2072 ds_put_format(s, "%smod_tp_src:%s%d", colors.param, colors.end, a->port);
2076 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
2078 ds_put_format(s, "%smod_tp_dst:%s%d", colors.param, colors.end, a->port);
2081 /* Action structure for OFPAT_COPY_FIELD. */
2082 struct ofp15_action_copy_field {
2083 ovs_be16 type; /* OFPAT_COPY_FIELD. */
2084 ovs_be16 len; /* Length is padded to 64 bits. */
2085 ovs_be16 n_bits; /* Number of bits to copy. */
2086 ovs_be16 src_offset; /* Starting bit offset in source. */
2087 ovs_be16 dst_offset; /* Starting bit offset in destination. */
2090 * - OXM header for source field.
2091 * - OXM header for destination field.
2092 * - Padding with 0-bytes to a multiple of 8 bytes.
2093 * The "pad2" member is the beginning of the above. */
2096 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
2098 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
2099 struct onf_action_copy_field {
2100 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
2101 ovs_be16 len; /* Length is padded to 64 bits. */
2102 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
2103 ovs_be16 exp_type; /* 3200. */
2104 uint8_t pad[2]; /* Not used. */
2105 ovs_be16 n_bits; /* Number of bits to copy. */
2106 ovs_be16 src_offset; /* Starting bit offset in source. */
2107 ovs_be16 dst_offset; /* Starting bit offset in destination. */
2108 uint8_t pad2[2]; /* Not used. */
2110 * - OXM header for source field.
2111 * - OXM header for destination field.
2112 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2113 * The "pad3" member is the beginning of the above. */
2114 uint8_t pad3[4]; /* Not used. */
2116 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
2118 /* Action structure for NXAST_REG_MOVE.
2120 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2121 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2122 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
2123 * the next most significant bit, and so on.
2125 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
2126 * sense to use nxm_hasmask=1 because the action does not do any kind of
2127 * matching; it uses the actual value of a field.)
2129 * The following nxm_header values are potentially acceptable as 'src':
2144 * - NXM_OF_ICMP_TYPE
2145 * - NXM_OF_ICMP_CODE
2152 * - NXM_NX_ICMPV6_TYPE
2153 * - NXM_NX_ICMPV6_CODE
2156 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2158 * - NXM_NX_TUN_IPV4_SRC
2159 * - NXM_NX_TUN_IPV4_DST
2161 * The following nxm_header values are potentially acceptable as 'dst':
2172 * - NXM_OF_ICMP_TYPE
2173 * - NXM_OF_ICMP_CODE
2174 * - NXM_NX_ICMPV6_TYPE
2175 * - NXM_NX_ICMPV6_CODE
2181 * Modifying any of the above fields changes the corresponding packet
2186 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2190 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
2191 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
2192 * header (if any), ignoring the other bits. Setting a value with CFI=1
2193 * adds or modifies the 802.1Q header appropriately, setting the TCI field
2194 * to the field's new value (with the CFI bit masked out).
2196 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
2197 * any of these values modifies the corresponding tunnel header field used
2198 * for the packet's next tunnel encapsulation, if allowed by the
2199 * configuration of the output tunnel port.
2201 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2202 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
2203 * used only if the flow's nx_match includes an nxm_entry that specifies
2204 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2206 * The switch will reject actions for which src_ofs+n_bits is greater than the
2207 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2208 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2210 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2211 * behaves as if 'src' were copied out to a temporary buffer, then the
2212 * temporary buffer copied to 'dst'.
2214 struct nx_action_reg_move {
2215 ovs_be16 type; /* OFPAT_VENDOR. */
2216 ovs_be16 len; /* Length is 24. */
2217 ovs_be32 vendor; /* NX_VENDOR_ID. */
2218 ovs_be16 subtype; /* NXAST_REG_MOVE. */
2219 ovs_be16 n_bits; /* Number of bits. */
2220 ovs_be16 src_ofs; /* Starting bit offset in source. */
2221 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
2223 * - OXM/NXM header for source field (4 or 8 bytes).
2224 * - OXM/NXM header for destination field (4 or 8 bytes).
2225 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2227 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
2230 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
2231 const void *action, ovs_be16 action_len, size_t oxm_offset,
2232 struct ofpbuf *ofpacts)
2234 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2235 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
2236 move->src.ofs = ntohs(src_offset);
2237 move->src.n_bits = ntohs(n_bits);
2238 move->dst.ofs = ntohs(dst_offset);
2239 move->dst.n_bits = ntohs(n_bits);
2241 struct ofpbuf b = ofpbuf_const_initializer(action, ntohs(action_len));
2242 ofpbuf_pull(&b, oxm_offset);
2244 enum ofperr error = nx_pull_header(&b, &move->src.field, NULL);
2248 error = nx_pull_header(&b, &move->dst.field, NULL);
2253 if (!is_all_zeros(b.data, b.size)) {
2254 return OFPERR_NXBRC_MUST_BE_ZERO;
2257 return nxm_reg_move_check(move, NULL);
2261 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
2262 enum ofp_version ofp_version OVS_UNUSED,
2263 struct ofpbuf *ofpacts)
2265 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2266 oacf->n_bits, oacf, oacf->len,
2267 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
2271 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
2272 enum ofp_version ofp_version OVS_UNUSED,
2273 struct ofpbuf *ofpacts)
2275 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2276 oacf->n_bits, oacf, oacf->len,
2277 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
2281 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
2282 enum ofp_version ofp_version OVS_UNUSED,
2283 struct ofpbuf *ofpacts)
2285 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2286 move->ofpact.raw = NXAST_RAW_REG_MOVE;
2287 move->src.ofs = ntohs(narm->src_ofs);
2288 move->src.n_bits = ntohs(narm->n_bits);
2289 move->dst.ofs = ntohs(narm->dst_ofs);
2290 move->dst.n_bits = ntohs(narm->n_bits);
2292 struct ofpbuf b = ofpbuf_const_initializer(narm, ntohs(narm->len));
2293 ofpbuf_pull(&b, sizeof *narm);
2295 enum ofperr error = nx_pull_header(&b, &move->src.field, NULL);
2299 error = nx_pull_header(&b, &move->dst.field, NULL);
2303 if (!is_all_zeros(b.data, b.size)) {
2304 return OFPERR_NXBRC_MUST_BE_ZERO;
2307 return nxm_reg_move_check(move, NULL);
2311 encode_REG_MOVE(const struct ofpact_reg_move *move,
2312 enum ofp_version ofp_version, struct ofpbuf *out)
2314 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2315 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2316 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2317 * probe for support. Until we have that ability, we currently prefer
2318 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2320 size_t start_ofs = out->size;
2321 if (ofp_version >= OFP15_VERSION) {
2322 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2323 copy->n_bits = htons(move->dst.n_bits);
2324 copy->src_offset = htons(move->src.ofs);
2325 copy->dst_offset = htons(move->dst.ofs);
2326 out->size = out->size - sizeof copy->pad2;
2327 nx_put_header(out, move->src.field->id, ofp_version, false);
2328 nx_put_header(out, move->dst.field->id, ofp_version, false);
2329 } else if (ofp_version == OFP13_VERSION
2330 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2331 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2332 copy->n_bits = htons(move->dst.n_bits);
2333 copy->src_offset = htons(move->src.ofs);
2334 copy->dst_offset = htons(move->dst.ofs);
2335 out->size = out->size - sizeof copy->pad3;
2336 nx_put_header(out, move->src.field->id, ofp_version, false);
2337 nx_put_header(out, move->dst.field->id, ofp_version, false);
2339 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2340 narm->n_bits = htons(move->dst.n_bits);
2341 narm->src_ofs = htons(move->src.ofs);
2342 narm->dst_ofs = htons(move->dst.ofs);
2343 nx_put_header(out, move->src.field->id, 0, false);
2344 nx_put_header(out, move->dst.field->id, 0, false);
2346 pad_ofpat(out, start_ofs);
2349 static char * OVS_WARN_UNUSED_RESULT
2350 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2351 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2353 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2354 const char *full_arg = arg;
2357 error = mf_parse_subfield__(&move->src, &arg);
2361 if (strncmp(arg, "->", 2)) {
2362 return xasprintf("%s: missing `->' following source", full_arg);
2365 error = mf_parse_subfield(&move->dst, arg);
2370 if (move->src.n_bits != move->dst.n_bits) {
2371 return xasprintf("%s: source field is %d bits wide but destination is "
2372 "%d bits wide", full_arg,
2373 move->src.n_bits, move->dst.n_bits);
2379 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2381 nxm_format_reg_move(a, s);
2384 /* Action structure for OFPAT12_SET_FIELD. */
2385 struct ofp12_action_set_field {
2386 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2387 ovs_be16 len; /* Length is padded to 64 bits. */
2390 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2391 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2393 * The "pad" member is the beginning of the above. */
2396 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2398 /* Action structure for NXAST_REG_LOAD.
2400 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2401 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2402 * starts at 0 for the least-significant bit, 1 for the next most significant
2405 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2406 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2409 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2410 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2411 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2412 * also stored as one less than its true value:
2415 * +------------------------------+------------------+
2416 * | ofs | n_bits - 1 |
2417 * +------------------------------+------------------+
2419 * The switch will reject actions for which ofs+n_bits is greater than the
2420 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2421 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2422 * OFPBAC_BAD_ARGUMENT.
2424 struct nx_action_reg_load {
2425 ovs_be16 type; /* OFPAT_VENDOR. */
2426 ovs_be16 len; /* Length is 24. */
2427 ovs_be32 vendor; /* NX_VENDOR_ID. */
2428 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2429 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2430 ovs_be32 dst; /* Destination register. */
2431 ovs_be64 value; /* Immediate value. */
2433 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2435 /* Action structure for NXAST_REG_LOAD2.
2437 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2438 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2439 * OXM experimenter fields. */
2440 struct nx_action_reg_load2 {
2441 ovs_be16 type; /* OFPAT_VENDOR. */
2442 ovs_be16 len; /* At least 16. */
2443 ovs_be32 vendor; /* NX_VENDOR_ID. */
2444 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2447 * - An NXM/OXM header, value, and optionally a mask.
2448 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2450 * The "pad" member is the beginning of the above. */
2453 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2456 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2457 bool may_mask, struct ofpbuf *ofpacts)
2459 struct ofpbuf b = ofpbuf_const_initializer(oasf, ntohs(oasf->len));
2460 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2462 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2463 enum ofperr error = nx_pull_entry(&b, &sf->field, &sf->value,
2464 may_mask ? &sf->mask : NULL);
2466 return (error == OFPERR_OFPBMC_BAD_MASK
2467 ? OFPERR_OFPBAC_BAD_SET_MASK
2471 memset(&sf->mask, 0xff, sf->field->n_bytes);
2474 if (!is_all_zeros(b.data, b.size)) {
2475 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2478 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2480 if (sf->field->id == MFF_IN_PORT_OXM) {
2481 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2484 /* oxm_length is now validated to be compatible with mf_value. */
2485 if (!sf->field->writable) {
2486 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2488 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2491 /* The value must be valid for match. OpenFlow 1.5 also says,
2492 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2493 * a 1-bit in oxm_value and in oxm_mask." */
2494 if (!mf_is_value_valid(sf->field, &sf->value)
2495 || (sf->field->id == MFF_VLAN_VID
2496 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2497 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2498 struct ds ds = DS_EMPTY_INITIALIZER;
2499 mf_format(sf->field, &sf->value, NULL, &ds);
2500 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2501 sf->field->name, ds_cstr(&ds));
2504 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2510 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2511 enum ofp_version ofp_version OVS_UNUSED,
2512 struct ofpbuf *ofpacts)
2514 return decode_ofpat_set_field(oasf, false, ofpacts);
2518 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2519 enum ofp_version ofp_version OVS_UNUSED,
2520 struct ofpbuf *ofpacts)
2522 return decode_ofpat_set_field(oasf, true, ofpacts);
2526 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2527 enum ofp_version ofp_version OVS_UNUSED,
2530 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2531 struct mf_subfield dst;
2534 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2536 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2537 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2538 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2539 error = mf_check_dst(&dst, NULL);
2544 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2546 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2547 return OFPERR_OFPBAC_BAD_ARGUMENT;
2550 sf->field = dst.field;
2551 bitwise_put(ntohll(narl->value),
2552 &sf->value, dst.field->n_bytes, dst.ofs,
2554 bitwise_put(UINT64_MAX,
2555 &sf->mask, dst.field->n_bytes, dst.ofs,
2562 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2563 enum ofp_version ofp_version OVS_UNUSED,
2566 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(out);
2567 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2569 struct ofpbuf b = ofpbuf_const_initializer(narl, ntohs(narl->len));
2570 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2572 enum ofperr error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2576 if (!is_all_zeros(b.data, b.size)) {
2577 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2580 if (!sf->field->writable) {
2581 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2583 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2589 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2590 enum mf_field_id field, uint64_t value_)
2592 struct ofp12_action_set_field *oasf OVS_UNUSED;
2593 int n_bytes = mf_from_id(field)->n_bytes;
2594 size_t start_ofs = openflow->size;
2595 union mf_value value;
2597 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2599 oasf = put_OFPAT12_SET_FIELD(openflow);
2600 openflow->size = openflow->size - sizeof oasf->pad;
2601 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2602 pad_ofpat(openflow, start_ofs);
2606 next_load_segment(const struct ofpact_set_field *sf,
2607 struct mf_subfield *dst, uint64_t *value)
2609 int n_bits = sf->field->n_bits;
2610 int n_bytes = sf->field->n_bytes;
2611 int start = dst->ofs + dst->n_bits;
2613 if (start < n_bits) {
2614 dst->field = sf->field;
2615 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2616 if (dst->ofs < n_bits) {
2617 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2618 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2619 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2626 /* Convert 'sf' to a series of REG_LOADs. */
2628 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2630 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2631 * experimenter OXM or is variable length (or if it came in as
2632 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2633 * NXAST_REG_LOAD, which is backward compatible. */
2634 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2635 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2636 struct nx_action_reg_load2 *narl OVS_UNUSED;
2637 size_t start_ofs = openflow->size;
2639 narl = put_NXAST_REG_LOAD2(openflow);
2640 openflow->size = openflow->size - sizeof narl->pad;
2641 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2642 pad_ofpat(openflow, start_ofs);
2644 struct mf_subfield dst;
2647 dst.ofs = dst.n_bits = 0;
2648 while (next_load_segment(sf, &dst, &value)) {
2649 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2650 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2651 narl->dst = htonl(mf_nxm_header(dst.field->id));
2652 narl->value = htonll(value);
2657 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2658 * actions, if we can, falling back to Nicira extensions if we must.
2660 * We check only meta-flow types that can appear within set field actions and
2661 * that have a mapping to compatible action types. These struct mf_field
2662 * definitions have a defined OXM or NXM header value and specify the field as
2665 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2666 enum ofp_version ofp_version,
2669 switch ((int) sf->field->id) {
2670 case MFF_VLAN_TCI: {
2671 ovs_be16 tci = sf->value.be16;
2672 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2673 uint16_t vid = vlan_tci_to_vid(tci);
2674 uint8_t pcp = vlan_tci_to_pcp(tci);
2676 if (ofp_version < OFP11_VERSION) {
2677 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2679 * If CFI=1, Add or modify VLAN VID & PCP.
2680 * If CFI=0, strip VLAN header, if any.
2683 put_OFPAT10_SET_VLAN_VID(out, vid);
2684 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2686 put_OFPAT10_STRIP_VLAN(out);
2689 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2691 * If CFI=1, Add or modify VLAN VID & PCP.
2692 * OpenFlow 1.1 set actions only apply if the packet
2693 * already has VLAN tags. To be sure that is the case
2694 * we have to push a VLAN header. As we do not support
2695 * multiple layers of VLANs, this is a no-op, if a VLAN
2696 * header already exists. This may backfire, however,
2697 * when we start supporting multiple layers of VLANs.
2698 * If CFI=0, strip VLAN header, if any.
2701 /* Push a VLAN tag, if one was not seen at action validation
2703 if (!sf->flow_has_vlan) {
2704 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2706 put_OFPAT11_SET_VLAN_VID(out, vid);
2707 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2709 /* If the flow did not match on vlan, we have no way of
2710 * knowing if the vlan tag exists, so we must POP just to be
2712 put_OFPAT11_POP_VLAN(out);
2718 case MFF_VLAN_VID: {
2719 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2720 if (ofp_version == OFP10_VERSION) {
2721 put_OFPAT10_SET_VLAN_VID(out, vid);
2723 put_OFPAT11_SET_VLAN_VID(out, vid);
2729 if (ofp_version == OFP10_VERSION) {
2730 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2732 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2737 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2741 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2745 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2749 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2753 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2756 case MFF_IP_DSCP_SHIFTED:
2757 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2762 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2767 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2771 set_field_to_nxast(sf, out);
2777 set_field_to_set_field(const struct ofpact_set_field *sf,
2778 enum ofp_version ofp_version, struct ofpbuf *out)
2780 struct ofp12_action_set_field *oasf OVS_UNUSED;
2781 size_t start_ofs = out->size;
2783 oasf = put_OFPAT12_SET_FIELD(out);
2784 out->size = out->size - sizeof oasf->pad;
2785 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2786 pad_ofpat(out, start_ofs);
2790 encode_SET_FIELD(const struct ofpact_set_field *sf,
2791 enum ofp_version ofp_version, struct ofpbuf *out)
2793 if (ofp_version >= OFP15_VERSION) {
2794 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2796 set_field_to_set_field(sf, ofp_version, out);
2797 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2798 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2799 /* It came in as reg_load, send it out the same way. */
2800 set_field_to_nxast(sf, out);
2801 } else if (ofp_version < OFP12_VERSION) {
2802 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2803 set_field_to_legacy_openflow(sf, ofp_version, out);
2804 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2805 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2806 * entire field, so encode it as OFPAT_SET_FIELD. */
2807 set_field_to_set_field(sf, ofp_version, out);
2809 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2810 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2811 * so encode it as reg_load. */
2812 set_field_to_nxast(sf, out);
2816 /* Parses the input argument 'arg' into the key, value, and delimiter
2817 * components that are common across the reg_load and set_field action format.
2819 * With an argument like "1->metadata", sets the following pointers to
2820 * point within 'arg':
2825 * Returns NULL if successful, otherwise a malloc()'d string describing the
2826 * error. The caller is responsible for freeing the returned string. */
2827 static char * OVS_WARN_UNUSED_RESULT
2828 set_field_split_str(char *arg, char **key, char **value, char **delim)
2833 value_end = strstr(arg, "->");
2834 *key = value_end + strlen("->");
2840 return xasprintf("%s: missing `->'", arg);
2842 if (strlen(value_end) <= strlen("->")) {
2843 return xasprintf("%s: missing field name following `->'", arg);
2849 /* Parses a "set_field" action with argument 'arg', appending the parsed
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 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2856 enum ofputil_protocol *usable_protocols)
2858 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2862 const struct mf_field *mf;
2865 error = set_field_split_str(arg, &key, &value, &delim);
2870 mf = mf_from_name(key);
2872 return xasprintf("%s is not a valid OXM field name", key);
2874 if (!mf->writable) {
2875 return xasprintf("%s is read-only", key);
2879 error = mf_parse(mf, value, &sf->value, &sf->mask);
2884 if (!mf_is_value_valid(mf, &sf->value)) {
2885 return xasprintf("%s is not a valid value for field %s", value, key);
2888 *usable_protocols &= mf->usable_protocols_exact;
2892 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2893 * action to 'ofpacts'.
2895 * Returns NULL if successful, otherwise a malloc()'d string describing the
2896 * error. The caller is responsible for freeing the returned string. */
2897 static char * OVS_WARN_UNUSED_RESULT
2898 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2899 enum ofputil_protocol *usable_protocols)
2901 char *copy = xstrdup(arg);
2902 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2907 static char * OVS_WARN_UNUSED_RESULT
2908 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2910 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2911 struct mf_subfield dst;
2912 char *key, *value_str;
2913 union mf_value value;
2916 error = set_field_split_str(arg, &key, &value_str, NULL);
2921 error = mf_parse_subfield(&dst, key);
2926 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2928 return xasprintf("%s: cannot parse integer value", arg);
2931 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2932 dst.field->n_bytes * 8 - dst.n_bits)) {
2936 mf_format(dst.field, &value, NULL, &ds);
2937 error = xasprintf("%s: value %s does not fit into %d bits",
2938 arg, ds_cstr(&ds), dst.n_bits);
2943 sf->field = dst.field;
2944 memset(&sf->value, 0, sizeof sf->value);
2945 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2946 dst.field->n_bytes, dst.ofs, dst.n_bits);
2947 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2953 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2955 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2956 struct mf_subfield dst;
2959 dst.ofs = dst.n_bits = 0;
2960 while (next_load_segment(a, &dst, &value)) {
2961 ds_put_format(s, "%sload:%s%#"PRIx64"%s->%s",
2962 colors.special, colors.end, value,
2963 colors.special, colors.end);
2964 mf_format_subfield(&dst, s);
2965 ds_put_char(s, ',');
2969 ds_put_format(s, "%sset_field:%s", colors.special, colors.end);
2970 mf_format(a->field, &a->value, &a->mask, s);
2971 ds_put_format(s, "%s->%s%s",
2972 colors.special, colors.end, a->field->name);
2976 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2977 * is marked such that, if possible, it will be translated to OpenFlow as
2978 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2979 * that was the way that the action was expressed when it came into OVS or for
2980 * backward compatibility. */
2981 struct ofpact_set_field *
2982 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2984 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2985 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2989 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2991 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2994 struct nx_action_stack {
2995 ovs_be16 type; /* OFPAT_VENDOR. */
2996 ovs_be16 len; /* Length is 16. */
2997 ovs_be32 vendor; /* NX_VENDOR_ID. */
2998 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2999 ovs_be16 offset; /* Bit offset into the field. */
3001 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
3002 * - ovs_be16 'n_bits', the number of bits to extract from the field.
3003 * - Enough 0-bytes to pad out the action to 24 bytes. */
3004 uint8_t pad[12]; /* See above. */
3006 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
3009 decode_stack_action(const struct nx_action_stack *nasp,
3010 struct ofpact_stack *stack_action)
3012 stack_action->subfield.ofs = ntohs(nasp->offset);
3014 struct ofpbuf b = ofpbuf_const_initializer(nasp, sizeof *nasp);
3015 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
3016 enum ofperr error = nx_pull_header(&b, &stack_action->subfield.field,
3021 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
3023 if (!is_all_zeros(b.data, b.size)) {
3024 return OFPERR_NXBRC_MUST_BE_ZERO;
3031 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
3032 enum ofp_version ofp_version OVS_UNUSED,
3033 struct ofpbuf *ofpacts)
3035 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
3036 enum ofperr error = decode_stack_action(nasp, push);
3037 return error ? error : nxm_stack_push_check(push, NULL);
3041 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
3042 enum ofp_version ofp_version OVS_UNUSED,
3043 struct ofpbuf *ofpacts)
3045 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
3046 enum ofperr error = decode_stack_action(nasp, pop);
3047 return error ? error : nxm_stack_pop_check(pop, NULL);
3051 encode_STACK_op(const struct ofpact_stack *stack_action,
3052 struct nx_action_stack *nasp)
3057 nasp->offset = htons(stack_action->subfield.ofs);
3059 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
3060 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
3061 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
3062 n_bits = htons(stack_action->subfield.n_bits);
3063 ofpbuf_put(&b, &n_bits, sizeof n_bits);
3067 encode_STACK_PUSH(const struct ofpact_stack *stack,
3068 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3070 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
3074 encode_STACK_POP(const struct ofpact_stack *stack,
3075 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3077 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
3080 static char * OVS_WARN_UNUSED_RESULT
3081 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
3082 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3084 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
3087 static char * OVS_WARN_UNUSED_RESULT
3088 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
3089 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3091 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
3095 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
3097 nxm_format_stack_push(a, s);
3101 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
3103 nxm_format_stack_pop(a, s);
3106 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
3108 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
3109 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
3110 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3111 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3113 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3114 * sent only to controllers with id 0.)
3116 struct nx_action_cnt_ids {
3117 ovs_be16 type; /* OFPAT_VENDOR. */
3118 ovs_be16 len; /* Length including slaves. */
3119 ovs_be32 vendor; /* NX_VENDOR_ID. */
3120 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
3122 ovs_be16 n_controllers; /* Number of controllers. */
3123 uint8_t zeros[4]; /* Must be zero. */
3125 /* Followed by 1 or more controller ids.
3127 * uint16_t cnt_ids[]; // Controller ids.
3128 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
3131 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
3134 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
3137 struct ofpact_cnt_ids *ids;
3138 enum ofperr error = 0;
3140 ids = ofpact_put_DEC_TTL(out);
3141 ids->n_controllers = 1;
3142 ofpbuf_put(out, &id, sizeof id);
3144 ofpact_finish_DEC_TTL(out, &ids);
3149 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
3150 enum ofp_version ofp_version OVS_UNUSED,
3153 struct ofpact_cnt_ids *ids;
3157 ids = ofpact_put_DEC_TTL(out);
3158 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3159 ids->n_controllers = ntohs(nac_ids->n_controllers);
3160 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
3162 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
3163 return OFPERR_NXBRC_MUST_BE_ZERO;
3166 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
3167 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
3168 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
3169 "are required for %"PRIu16" controllers.",
3170 ids_size, ids->n_controllers * sizeof(ovs_be16),
3171 ids->n_controllers);
3172 return OFPERR_OFPBAC_BAD_LEN;
3175 for (i = 0; i < ids->n_controllers; i++) {
3176 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
3177 ofpbuf_put(out, &id, sizeof id);
3181 ofpact_finish_DEC_TTL(out, &ids);
3187 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
3188 enum ofp_version ofp_version, struct ofpbuf *out)
3190 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
3191 || dec_ttl->n_controllers != 1
3192 || dec_ttl->cnt_ids[0] != 0) {
3193 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
3194 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
3198 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
3199 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
3201 ids = ofpbuf_put_zeros(out, ids_len);
3202 for (i = 0; i < dec_ttl->n_controllers; i++) {
3203 ids[i] = htons(dec_ttl->cnt_ids[i]);
3206 put_OFPAT_DEC_NW_TTL(out, ofp_version);
3211 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
3213 struct ofpact_cnt_ids *ids;
3216 ofpact_put_DEC_TTL(ofpacts);
3217 ofpbuf_put(ofpacts, &id, sizeof id);
3218 ids = ofpacts->header;
3219 ids->n_controllers++;
3220 ofpact_finish_DEC_TTL(ofpacts, &ids);
3223 static char * OVS_WARN_UNUSED_RESULT
3224 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
3225 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3228 parse_noargs_dec_ttl(ofpacts);
3230 struct ofpact_cnt_ids *ids;
3233 ids = ofpact_put_DEC_TTL(ofpacts);
3234 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3235 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
3236 cntr = strtok_r(NULL, ", ", &arg)) {
3237 uint16_t id = atoi(cntr);
3239 ofpbuf_put(ofpacts, &id, sizeof id);
3240 ids = ofpacts->header;
3241 ids->n_controllers++;
3243 if (!ids->n_controllers) {
3244 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3247 ofpact_finish_DEC_TTL(ofpacts, &ids);
3253 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
3257 ds_put_format(s, "%sdec_ttl%s", colors.paren, colors.end);
3258 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
3259 ds_put_format(s, "%s(%s", colors.paren, colors.end);
3260 for (i = 0; i < a->n_controllers; i++) {
3262 ds_put_cstr(s, ",");
3264 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
3266 ds_put_format(s, "%s)%s", colors.paren, colors.end);
3270 /* Set MPLS label actions. */
3273 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
3274 enum ofp_version ofp_version OVS_UNUSED,
3277 ofpact_put_SET_MPLS_LABEL(out)->label = label;
3282 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3283 enum ofp_version ofp_version,
3286 if (ofp_version < OFP12_VERSION) {
3287 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3289 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
3290 ntohl(label->label));
3294 static char * OVS_WARN_UNUSED_RESULT
3295 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3296 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3298 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3300 return xstrdup("set_mpls_label: expected label.");
3303 mpls_label->label = htonl(atoi(arg));
3308 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3310 ds_put_format(s, "%sset_mpls_label(%s%"PRIu32"%s)%s",
3311 colors.paren, colors.end, ntohl(a->label),
3312 colors.paren, colors.end);
3315 /* Set MPLS TC actions. */
3318 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3319 enum ofp_version ofp_version OVS_UNUSED,
3322 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3327 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3328 enum ofp_version ofp_version, struct ofpbuf *out)
3330 if (ofp_version < OFP12_VERSION) {
3331 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3333 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3337 static char * OVS_WARN_UNUSED_RESULT
3338 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3339 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3341 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3344 return xstrdup("set_mpls_tc: expected tc.");
3347 mpls_tc->tc = atoi(arg);
3352 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3354 ds_put_format(s, "%sset_mpls_ttl(%s%"PRIu8"%s)%s",
3355 colors.paren, colors.end, a->tc,
3356 colors.paren, colors.end);
3359 /* Set MPLS TTL actions. */
3362 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3363 enum ofp_version ofp_version OVS_UNUSED,
3366 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3371 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3372 enum ofp_version ofp_version, struct ofpbuf *out)
3374 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3377 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3378 * action to 'ofpacts'.
3380 * Returns NULL if successful, otherwise a malloc()'d string describing the
3381 * error. The caller is responsible for freeing the returned string. */
3382 static char * OVS_WARN_UNUSED_RESULT
3383 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3384 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3386 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3389 return xstrdup("set_mpls_ttl: expected ttl.");
3392 mpls_ttl->ttl = atoi(arg);
3397 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3399 ds_put_format(s, "%sset_mpls_ttl(%s%"PRIu8"%s)%s",
3400 colors.paren, colors.end, a->ttl,
3401 colors.paren, colors.end);
3404 /* Decrement MPLS TTL actions. */
3407 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3409 ofpact_put_DEC_MPLS_TTL(out);
3414 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3415 enum ofp_version ofp_version, struct ofpbuf *out)
3417 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3420 static char * OVS_WARN_UNUSED_RESULT
3421 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3422 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3424 ofpact_put_DEC_MPLS_TTL(ofpacts);
3429 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3431 ds_put_format(s, "%sdec_mpls_ttl%s", colors.value, colors.end);
3434 /* Push MPLS label action. */
3437 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3438 enum ofp_version ofp_version OVS_UNUSED,
3441 struct ofpact_push_mpls *oam;
3443 if (!eth_type_mpls(ethertype)) {
3444 return OFPERR_OFPBAC_BAD_ARGUMENT;
3446 oam = ofpact_put_PUSH_MPLS(out);
3447 oam->ethertype = ethertype;
3453 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3454 enum ofp_version ofp_version, struct ofpbuf *out)
3456 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3459 static char * OVS_WARN_UNUSED_RESULT
3460 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3461 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3466 error = str_to_u16(arg, "push_mpls", ðertype);
3468 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3474 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3476 ds_put_format(s, "%spush_mpls:%s0x%04"PRIx16,
3477 colors.param, colors.end, ntohs(a->ethertype));
3480 /* Pop MPLS label action. */
3483 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3484 enum ofp_version ofp_version OVS_UNUSED,
3487 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3492 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3493 enum ofp_version ofp_version, struct ofpbuf *out)
3495 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3498 static char * OVS_WARN_UNUSED_RESULT
3499 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3500 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3505 error = str_to_u16(arg, "pop_mpls", ðertype);
3507 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3513 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3515 ds_put_format(s, "%spop_mpls:%s0x%04"PRIx16,
3516 colors.param, colors.end, ntohs(a->ethertype));
3519 /* Set tunnel ID actions. */
3522 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3523 enum ofp_version ofp_version OVS_UNUSED,
3526 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3527 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3528 tunnel->tun_id = tun_id;
3533 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3534 enum ofp_version ofp_version OVS_UNUSED,
3537 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3538 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3539 tunnel->tun_id = tun_id;
3544 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3545 enum ofp_version ofp_version, struct ofpbuf *out)
3547 uint64_t tun_id = tunnel->tun_id;
3549 if (ofp_version < OFP12_VERSION) {
3550 if (tun_id <= UINT32_MAX
3551 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3552 put_NXAST_SET_TUNNEL(out, tun_id);
3554 put_NXAST_SET_TUNNEL64(out, tun_id);
3557 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3561 static char * OVS_WARN_UNUSED_RESULT
3562 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3563 enum ofp_raw_action_type raw)
3565 struct ofpact_tunnel *tunnel;
3567 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3568 tunnel->ofpact.raw = raw;
3569 return str_to_u64(arg, &tunnel->tun_id);
3572 static char * OVS_WARN_UNUSED_RESULT
3573 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3574 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3576 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3580 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3582 ds_put_format(s, "%sset_tunnel%s:%s%#"PRIx64, colors.param,
3583 (a->tun_id > UINT32_MAX
3584 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3585 colors.end, a->tun_id);
3588 /* Set queue action. */
3591 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3592 enum ofp_version ofp_version OVS_UNUSED,
3595 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3600 encode_SET_QUEUE(const struct ofpact_queue *queue,
3601 enum ofp_version ofp_version, struct ofpbuf *out)
3603 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3606 static char * OVS_WARN_UNUSED_RESULT
3607 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3608 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3610 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3614 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3616 ds_put_format(s, "%sset_queue:%s%"PRIu32,
3617 colors.param, colors.end, a->queue_id);
3620 /* Pop queue action. */
3623 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3625 ofpact_put_POP_QUEUE(out);
3630 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3631 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3633 put_NXAST_POP_QUEUE(out);
3636 static char * OVS_WARN_UNUSED_RESULT
3637 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3638 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3640 ofpact_put_POP_QUEUE(ofpacts);
3645 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3647 ds_put_format(s, "%spop_queue%s", colors.value, colors.end);
3650 /* Action structure for NXAST_FIN_TIMEOUT.
3652 * This action changes the idle timeout or hard timeout, or both, of this
3653 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3654 * When such a packet is observed, the action reduces the rule's idle timeout
3655 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3656 * action has no effect on an existing timeout that is already shorter than the
3657 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3658 * of zero has no effect on the respective timeout.
3660 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3661 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3662 * receipt of the FIN or RST.
3664 * This is useful for quickly discarding learned TCP flows that otherwise will
3665 * take a long time to expire.
3667 * This action is intended for use with an OpenFlow rule that matches only a
3668 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3669 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3670 * RST in any of those flows will cause the entire OpenFlow rule to expire
3671 * early, which is not normally desirable.
3673 struct nx_action_fin_timeout {
3674 ovs_be16 type; /* OFPAT_VENDOR. */
3675 ovs_be16 len; /* 16. */
3676 ovs_be32 vendor; /* NX_VENDOR_ID. */
3677 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3678 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3679 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3680 ovs_be16 pad; /* Must be zero. */
3682 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3685 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3686 enum ofp_version ofp_version OVS_UNUSED,
3689 struct ofpact_fin_timeout *oft;
3691 oft = ofpact_put_FIN_TIMEOUT(out);
3692 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3693 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3698 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3699 enum ofp_version ofp_version OVS_UNUSED,
3702 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3703 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3704 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3707 static char * OVS_WARN_UNUSED_RESULT
3708 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3709 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3711 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3714 while (ofputil_parse_key_value(&arg, &key, &value)) {
3717 if (!strcmp(key, "idle_timeout")) {
3718 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3719 } else if (!strcmp(key, "hard_timeout")) {
3720 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3722 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3734 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3736 ds_put_format(s, "%sfin_timeout(%s", colors.paren, colors.end);
3737 if (a->fin_idle_timeout) {
3738 ds_put_format(s, "%sidle_timeout=%s%"PRIu16",",
3739 colors.param, colors.end, a->fin_idle_timeout);
3741 if (a->fin_hard_timeout) {
3742 ds_put_format(s, "%shard_timeout=%s%"PRIu16",",
3743 colors.param, colors.end, a->fin_hard_timeout);
3746 ds_put_format(s, "%s)%s", colors.paren, colors.end);
3749 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3751 * These actions search one of the switch's flow tables:
3753 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3754 * it specifies the table to search.
3756 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3757 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3758 * table, that is, the OpenFlow flow table that contains the flow from
3759 * which this action was obtained. If this action did not come from a
3760 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3761 * is the current table.
3763 * The flow table lookup uses a flow that may be slightly modified from the
3766 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3767 * is used as the flow's in_port.
3769 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3770 * then its value is used as the flow's in_port. Otherwise, the original
3773 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3774 * resubmit action, then the flow is updated with the new values.
3776 * Following the lookup, the original in_port is restored.
3778 * If the modified flow matched in the flow table, then the corresponding
3779 * actions are executed. Afterward, actions following the resubmit in the
3780 * original set of actions, if any, are executed; any changes made to the
3781 * packet (e.g. changes to VLAN) by secondary actions persist when those
3782 * actions are executed, although the original in_port is restored.
3784 * Resubmit actions may be used any number of times within a set of actions.
3786 * Resubmit actions may nest. To prevent infinite loops and excessive resource
3787 * use, the implementation may limit nesting depth and the total number of
3790 * - Open vSwitch 1.0.1 and earlier did not support recursion.
3792 * - Open vSwitch 1.0.2 and 1.0.3 limited recursion to 8 levels.
3794 * - Open vSwitch 1.1 and 1.2 limited recursion to 16 levels.
3796 * - Open vSwitch 1.2 through 1.8 limited recursion to 32 levels.
3798 * - Open vSwitch 1.9 through 2.0 limited recursion to 64 levels.
3800 * - Open vSwitch 2.1 through 2.5 limited recursion to 64 levels and impose
3801 * a total limit of 4,096 resubmits per flow translation (earlier versions
3802 * did not impose any total limit).
3804 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3805 * 'pad' to be all-bits-zero.
3807 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3808 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3810 struct nx_action_resubmit {
3811 ovs_be16 type; /* OFPAT_VENDOR. */
3812 ovs_be16 len; /* Length is 16. */
3813 ovs_be32 vendor; /* NX_VENDOR_ID. */
3814 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3815 ovs_be16 in_port; /* New in_port for checking flow table. */
3816 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3819 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3822 decode_NXAST_RAW_RESUBMIT(uint16_t port,
3823 enum ofp_version ofp_version OVS_UNUSED,
3826 struct ofpact_resubmit *resubmit;
3828 resubmit = ofpact_put_RESUBMIT(out);
3829 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3830 resubmit->in_port = u16_to_ofp(port);
3831 resubmit->table_id = 0xff;
3836 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3837 enum ofp_version ofp_version OVS_UNUSED,
3840 struct ofpact_resubmit *resubmit;
3842 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3843 return OFPERR_OFPBAC_BAD_ARGUMENT;
3846 resubmit = ofpact_put_RESUBMIT(out);
3847 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3848 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3849 resubmit->table_id = nar->table;
3854 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3855 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3857 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3859 if (resubmit->table_id == 0xff
3860 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3861 put_NXAST_RESUBMIT(out, in_port);
3863 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3864 nar->table = resubmit->table_id;
3865 nar->in_port = htons(in_port);
3869 static char * OVS_WARN_UNUSED_RESULT
3870 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3871 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3873 struct ofpact_resubmit *resubmit;
3874 char *in_port_s, *table_s;
3876 resubmit = ofpact_put_RESUBMIT(ofpacts);
3878 in_port_s = strsep(&arg, ",");
3879 if (in_port_s && in_port_s[0]) {
3880 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3881 return xasprintf("%s: resubmit to unknown port", in_port_s);
3884 resubmit->in_port = OFPP_IN_PORT;
3887 table_s = strsep(&arg, ",");
3888 if (table_s && table_s[0]) {
3889 uint32_t table_id = 0;
3892 error = str_to_u32(table_s, &table_id);
3896 resubmit->table_id = table_id;
3898 resubmit->table_id = 255;
3901 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3902 return xstrdup("at least one \"in_port\" or \"table\" must be "
3903 "specified on resubmit");
3909 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3911 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3912 ds_put_format(s, "%sresubmit:%s", colors.special, colors.end);
3913 ofputil_format_port(a->in_port, s);
3915 ds_put_format(s, "%sresubmit(%s", colors.paren, colors.end);
3916 if (a->in_port != OFPP_IN_PORT) {
3917 ofputil_format_port(a->in_port, s);
3919 ds_put_char(s, ',');
3920 if (a->table_id != 255) {
3921 ds_put_format(s, "%"PRIu8, a->table_id);
3923 ds_put_format(s, "%s)%s", colors.paren, colors.end);
3927 /* Action structure for NXAST_LEARN.
3929 * This action adds or modifies a flow in an OpenFlow table, similar to
3930 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3931 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3932 * flow's match criteria and actions are built by applying each of the series
3933 * of flow_mod_spec elements included as part of the action.
3935 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3936 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3937 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3938 * bits from a source to a destination. In this case, the header contains
3941 * 15 14 13 12 11 10 0
3942 * +------+---+------+---------------------------------+
3943 * | 0 |src| dst | n_bits |
3944 * +------+---+------+---------------------------------+
3946 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3947 * following table summarizes the meaning of each possible combination.
3948 * Details follow the table:
3951 * --- --- ----------------------------------------------------------
3952 * 0 0 Add match criteria based on value in a field.
3953 * 1 0 Add match criteria based on an immediate value.
3954 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3955 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3956 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3957 * All other combinations are undefined and not allowed.
3959 * The flow_mod_spec header is followed by a source specification and a
3960 * destination specification. The format and meaning of the source
3961 * specification depends on 'src':
3963 * - If 'src' is 0, the source bits are taken from a field in the flow to
3964 * which this action is attached. (This should be a wildcarded field. If
3965 * its value is fully specified then the source bits being copied have
3968 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3969 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3970 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3971 * 'field' and 'ofs' are subject to the same restrictions as the source
3972 * field in NXAST_REG_MOVE.
3974 * - If 'src' is 1, the source bits are a constant value. The source
3975 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3976 * number in network order, the source bits are the 'n_bits'
3977 * least-significant bits. The switch will report an error if other bits
3978 * in the constant are nonzero.
3980 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3981 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3982 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3983 * meaning of the flow_mod_spec depends on 'dst':
3985 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3986 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3987 * packet equal the source bits. 'field' may be any nxm_header with
3988 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3990 * Order is significant. Earlier flow_mod_specs must satisfy any
3991 * prerequisites for matching fields specified later, by copying constant
3992 * values into prerequisite fields.
3994 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3997 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3998 * the new flow. The new flow copies the source bits into
3999 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
4002 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
4003 * greater than 64 yields multiple NXAST_REG_LOAD actions.
4005 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
4006 * empty. It has the following meaning:
4008 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
4009 * The new flow outputs to the OpenFlow port specified by the source field.
4010 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
4011 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
4014 * Resource Management
4015 * -------------------
4017 * A switch has a finite amount of flow table space available for learning.
4018 * When this space is exhausted, no new learning table entries will be learned
4019 * until some existing flow table entries expire. The controller should be
4020 * prepared to handle this by flooding (which can be implemented as a
4021 * low-priority flow).
4023 * If a learned flow matches a single TCP stream with a relatively long
4024 * timeout, one may make the best of resource constraints by setting
4025 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
4026 * both, to shorter timeouts. When either of these is specified as a nonzero
4027 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
4033 * The following examples give a prose description of the flow_mod_specs along
4034 * with informal notation for how those would be represented and a hex dump of
4035 * the bytes that would be required.
4037 * These examples could work with various nx_action_learn parameters. Typical
4038 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
4039 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
4041 * 1. Learn input port based on the source MAC, with lookup into
4042 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
4044 * Match on in_port=99:
4045 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
4046 * ovs_be16(99), 00 63
4047 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4049 * Match Ethernet destination on Ethernet source from packet:
4050 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4051 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4052 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4054 * Set NXM_NX_REG1[16:31] to the packet's input port:
4055 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
4056 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4057 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
4059 * Given a packet that arrived on port A with Ethernet source address B,
4060 * this would set up the flow "in_port=99, dl_dst=B,
4061 * actions=load:A->NXM_NX_REG1[16..31]".
4063 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
4064 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4065 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
4067 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
4068 * into NXM_NX_REG1[16:31]:
4070 * Match on same VLAN ID as packet:
4071 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
4072 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4073 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4075 * Match Ethernet destination on Ethernet source from packet:
4076 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4077 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4078 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4080 * Output to the packet's input port:
4081 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
4082 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4084 * Given a packet that arrived on port A with Ethernet source address B in
4085 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
4086 * actions=output:A".
4088 * In syntax accepted by ovs-ofctl, this action is:
4089 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4090 * output:NXM_OF_IN_PORT[])
4092 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
4093 * 10-second MAC expiration time to make it easier to see what's going on
4095 * ovs-vsctl del-controller br0
4096 * ovs-ofctl del-flows br0
4097 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
4098 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
4099 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
4100 output:NXM_OF_IN_PORT[]), resubmit(,1)"
4101 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
4103 * You can then dump the MAC learning table with:
4105 * ovs-ofctl dump-flows br0 table=1
4110 * For best performance, segregate learned flows into a table that is not used
4111 * for any other flows except possibly for a lowest-priority "catch-all" flow
4112 * (a flow with no match criteria). If different learning actions specify
4113 * different match criteria, use different tables for the learned flows.
4115 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
4116 * These timeouts apply to the flow that is added, which means that a flow with
4117 * an idle timeout will expire when no traffic has been sent *to* the learned
4118 * address. This is not usually the intent in MAC learning; instead, we want
4119 * the MAC learn entry to expire when no traffic has been sent *from* the
4120 * learned address. Use a hard timeout for that.
4123 * Visibility of Changes
4124 * ---------------------
4126 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
4127 * flow translation are visible to flow table lookups made later in the flow
4128 * translation. This means that, in the example above, a MAC learned by the
4129 * learn action in table 0 would be found in table 1 (if the packet being
4130 * processed had the same source and destination MAC address).
4132 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
4133 * modify a flow) by a "learn" action are visible only for later flow
4134 * translations, not for later lookups within the same flow translation. In
4135 * the MAC learning example, a MAC learned by the learn action in table 0 would
4136 * not be found in table 1 if the flow translation would resubmit to table 1
4137 * after the processing of the learn action, meaning that if this MAC had not
4138 * been learned before then the packet would be flooded. */
4139 struct nx_action_learn {
4140 ovs_be16 type; /* OFPAT_VENDOR. */
4141 ovs_be16 len; /* At least 24. */
4142 ovs_be32 vendor; /* NX_VENDOR_ID. */
4143 ovs_be16 subtype; /* NXAST_LEARN. */
4144 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
4145 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
4146 ovs_be16 priority; /* Priority level of flow entry. */
4147 ovs_be64 cookie; /* Cookie for new flow. */
4148 ovs_be16 flags; /* NX_LEARN_F_*. */
4149 uint8_t table_id; /* Table to insert flow entry. */
4150 uint8_t pad; /* Must be zero. */
4151 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
4152 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
4153 /* Followed by a sequence of flow_mod_spec elements, as described above,
4154 * until the end of the action is reached. */
4156 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
4159 get_be16(const void **pp)
4161 const ovs_be16 *p = *pp;
4162 ovs_be16 value = *p;
4168 get_be32(const void **pp)
4170 const ovs_be32 *p = *pp;
4171 ovs_be32 value = get_unaligned_be32(p);
4177 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
4179 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
4180 sf->ofs = ntohs(get_be16(p));
4181 sf->n_bits = n_bits;
4185 learn_min_len(uint16_t header)
4187 int n_bits = header & NX_LEARN_N_BITS_MASK;
4188 int src_type = header & NX_LEARN_SRC_MASK;
4189 int dst_type = header & NX_LEARN_DST_MASK;
4190 unsigned int min_len;
4193 if (src_type == NX_LEARN_SRC_FIELD) {
4194 min_len += sizeof(ovs_be32); /* src_field */
4195 min_len += sizeof(ovs_be16); /* src_ofs */
4197 min_len += DIV_ROUND_UP(n_bits, 16);
4199 if (dst_type == NX_LEARN_DST_MATCH ||
4200 dst_type == NX_LEARN_DST_LOAD) {
4201 min_len += sizeof(ovs_be32); /* dst_field */
4202 min_len += sizeof(ovs_be16); /* dst_ofs */
4207 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
4208 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
4210 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
4211 enum ofp_version ofp_version OVS_UNUSED,
4212 struct ofpbuf *ofpacts)
4214 struct ofpact_learn *learn;
4215 const void *p, *end;
4218 return OFPERR_OFPBAC_BAD_ARGUMENT;
4221 learn = ofpact_put_LEARN(ofpacts);
4223 learn->idle_timeout = ntohs(nal->idle_timeout);
4224 learn->hard_timeout = ntohs(nal->hard_timeout);
4225 learn->priority = ntohs(nal->priority);
4226 learn->cookie = nal->cookie;
4227 learn->table_id = nal->table_id;
4228 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
4229 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
4231 learn->flags = ntohs(nal->flags);
4232 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
4233 NX_LEARN_F_DELETE_LEARNED)) {
4234 return OFPERR_OFPBAC_BAD_ARGUMENT;
4237 if (learn->table_id == 0xff) {
4238 return OFPERR_OFPBAC_BAD_ARGUMENT;
4241 end = (char *) nal + ntohs(nal->len);
4242 for (p = nal + 1; p != end; ) {
4243 struct ofpact_learn_spec *spec;
4244 uint16_t header = ntohs(get_be16(&p));
4250 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
4251 learn = ofpacts->header;
4254 spec->src_type = header & NX_LEARN_SRC_MASK;
4255 spec->dst_type = header & NX_LEARN_DST_MASK;
4256 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
4258 /* Check for valid src and dst type combination. */
4259 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4260 spec->dst_type == NX_LEARN_DST_LOAD ||
4261 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
4262 spec->src_type == NX_LEARN_SRC_FIELD)) {
4265 return OFPERR_OFPBAC_BAD_ARGUMENT;
4268 /* Check that the arguments don't overrun the end of the action. */
4269 if ((char *) end - (char *) p < learn_min_len(header)) {
4270 return OFPERR_OFPBAC_BAD_LEN;
4273 /* Get the source. */
4274 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4275 get_subfield(spec->n_bits, &p, &spec->src);
4277 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4279 bitwise_copy(p, p_bytes, 0,
4280 &spec->src_imm, sizeof spec->src_imm, 0,
4282 p = (const uint8_t *) p + p_bytes;
4285 /* Get the destination. */
4286 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4287 spec->dst_type == NX_LEARN_DST_LOAD) {
4288 get_subfield(spec->n_bits, &p, &spec->dst);
4291 ofpact_finish_LEARN(ofpacts, &learn);
4293 if (!is_all_zeros(p, (char *) end - (char *) p)) {
4294 return OFPERR_OFPBAC_BAD_ARGUMENT;
4301 put_be16(struct ofpbuf *b, ovs_be16 x)
4303 ofpbuf_put(b, &x, sizeof x);
4307 put_be32(struct ofpbuf *b, ovs_be32 x)
4309 ofpbuf_put(b, &x, sizeof x);
4313 put_u16(struct ofpbuf *b, uint16_t x)
4315 put_be16(b, htons(x));
4319 put_u32(struct ofpbuf *b, uint32_t x)
4321 put_be32(b, htonl(x));
4325 encode_LEARN(const struct ofpact_learn *learn,
4326 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4328 const struct ofpact_learn_spec *spec;
4329 struct nx_action_learn *nal;
4332 start_ofs = out->size;
4333 nal = put_NXAST_LEARN(out);
4334 nal->idle_timeout = htons(learn->idle_timeout);
4335 nal->hard_timeout = htons(learn->hard_timeout);
4336 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4337 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4338 nal->priority = htons(learn->priority);
4339 nal->cookie = learn->cookie;
4340 nal->flags = htons(learn->flags);
4341 nal->table_id = learn->table_id;
4343 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4344 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4346 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4347 put_u32(out, mf_nxm_header(spec->src.field->id));
4348 put_u16(out, spec->src.ofs);
4350 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4351 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4352 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4353 bits, n_dst_bytes, 0,
4357 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4358 spec->dst_type == NX_LEARN_DST_LOAD) {
4359 put_u32(out, mf_nxm_header(spec->dst.field->id));
4360 put_u16(out, spec->dst.ofs);
4364 pad_ofpat(out, start_ofs);
4367 static char * OVS_WARN_UNUSED_RESULT
4368 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4369 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4371 return learn_parse(arg, ofpacts);
4375 format_LEARN(const struct ofpact_learn *a, struct ds *s)
4380 /* Action structure for NXAST_CONJUNCTION. */
4381 struct nx_action_conjunction {
4382 ovs_be16 type; /* OFPAT_VENDOR. */
4383 ovs_be16 len; /* At least 16. */
4384 ovs_be32 vendor; /* NX_VENDOR_ID. */
4385 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4390 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4393 add_conjunction(struct ofpbuf *out,
4394 uint32_t id, uint8_t clause, uint8_t n_clauses)
4396 struct ofpact_conjunction *oc;
4398 oc = ofpact_put_CONJUNCTION(out);
4400 oc->clause = clause;
4401 oc->n_clauses = n_clauses;
4405 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4406 enum ofp_version ofp_version OVS_UNUSED,
4409 if (nac->n_clauses < 2 || nac->n_clauses > 64
4410 || nac->clause >= nac->n_clauses) {
4411 return OFPERR_NXBAC_BAD_CONJUNCTION;
4413 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4419 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4420 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4422 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4423 nac->clause = oc->clause;
4424 nac->n_clauses = oc->n_clauses;
4425 nac->id = htonl(oc->id);
4429 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4431 ds_put_format(s, "%sconjunction(%s%"PRIu32",%"PRIu8"/%"PRIu8"%s)%s",
4432 colors.paren, colors.end,
4433 oc->id, oc->clause + 1, oc->n_clauses,
4434 colors.paren, colors.end);
4437 static char * OVS_WARN_UNUSED_RESULT
4438 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4439 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4446 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4447 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4448 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4451 if (n_clauses < 2) {
4452 return xstrdup("conjunction must have at least 2 clauses");
4453 } else if (n_clauses > 64) {
4454 return xstrdup("conjunction must have at most 64 clauses");
4455 } else if (clause < 1) {
4456 return xstrdup("clause index must be positive");
4457 } else if (clause > n_clauses) {
4458 return xstrdup("clause index must be less than or equal to "
4459 "number of clauses");
4462 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4466 /* Action structure for NXAST_MULTIPATH.
4468 * This action performs the following steps in sequence:
4470 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4471 * Refer to the definition of "enum nx_mp_fields" for details.
4473 * The 'basis' value is used as a universal hash parameter, that is,
4474 * different values of 'basis' yield different hash functions. The
4475 * particular universal hash function used is implementation-defined.
4477 * The hashed fields' values are drawn from the current state of the
4478 * flow, including all modifications that have been made by actions up to
4481 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4482 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4485 * The output of the algorithm is 'link', an unsigned integer less than
4486 * or equal to 'max_link'.
4488 * Some algorithms use 'arg' as an additional argument.
4490 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4491 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4494 * The switch will reject actions that have an unknown 'fields', or an unknown
4495 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4496 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4497 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4499 struct nx_action_multipath {
4500 ovs_be16 type; /* OFPAT_VENDOR. */
4501 ovs_be16 len; /* Length is 32. */
4502 ovs_be32 vendor; /* NX_VENDOR_ID. */
4503 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4505 /* What fields to hash and how. */
4506 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4507 ovs_be16 basis; /* Universal hash parameter. */
4510 /* Multipath link choice algorithm to apply to hash value. */
4511 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4512 ovs_be16 max_link; /* Number of output links, minus 1. */
4513 ovs_be32 arg; /* Algorithm-specific argument. */
4516 /* Where to store the result. */
4517 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4518 ovs_be32 dst; /* Destination. */
4520 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4523 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4524 enum ofp_version ofp_version OVS_UNUSED,
4527 uint32_t n_links = ntohs(nam->max_link) + 1;
4528 size_t min_n_bits = log_2_ceil(n_links);
4529 struct ofpact_multipath *mp;
4531 mp = ofpact_put_MULTIPATH(out);
4532 mp->fields = ntohs(nam->fields);
4533 mp->basis = ntohs(nam->basis);
4534 mp->algorithm = ntohs(nam->algorithm);
4535 mp->max_link = ntohs(nam->max_link);
4536 mp->arg = ntohl(nam->arg);
4537 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4538 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4539 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4541 if (!flow_hash_fields_valid(mp->fields)) {
4542 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4543 return OFPERR_OFPBAC_BAD_ARGUMENT;
4544 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4545 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4546 && mp->algorithm != NX_MP_ALG_HRW
4547 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4548 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4549 return OFPERR_OFPBAC_BAD_ARGUMENT;
4550 } else if (mp->dst.n_bits < min_n_bits) {
4551 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4552 "%"PRIu32" links", min_n_bits, n_links);
4553 return OFPERR_OFPBAC_BAD_ARGUMENT;
4556 return multipath_check(mp, NULL);
4560 encode_MULTIPATH(const struct ofpact_multipath *mp,
4561 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4563 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4565 nam->fields = htons(mp->fields);
4566 nam->basis = htons(mp->basis);
4567 nam->algorithm = htons(mp->algorithm);
4568 nam->max_link = htons(mp->max_link);
4569 nam->arg = htonl(mp->arg);
4570 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4571 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4574 static char * OVS_WARN_UNUSED_RESULT
4575 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4576 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4578 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4582 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4584 multipath_format(a, s);
4587 /* Action structure for NXAST_NOTE.
4589 * This action has no effect. It is variable length. The switch does not
4590 * attempt to interpret the user-defined 'note' data in any way. A controller
4591 * can use this action to attach arbitrary metadata to a flow.
4593 * This action might go away in the future.
4595 struct nx_action_note {
4596 ovs_be16 type; /* OFPAT_VENDOR. */
4597 ovs_be16 len; /* A multiple of 8, but at least 16. */
4598 ovs_be32 vendor; /* NX_VENDOR_ID. */
4599 ovs_be16 subtype; /* NXAST_NOTE. */
4600 uint8_t note[6]; /* Start of user-defined data. */
4601 /* Possibly followed by additional user-defined data. */
4603 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4606 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4607 enum ofp_version ofp_version OVS_UNUSED,
4610 struct ofpact_note *note;
4611 unsigned int length;
4613 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4614 note = ofpact_put_NOTE(out);
4615 note->length = length;
4616 ofpbuf_put(out, nan->note, length);
4618 ofpact_finish_NOTE(out, ¬e);
4624 encode_NOTE(const struct ofpact_note *note,
4625 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4627 size_t start_ofs = out->size;
4628 struct nx_action_note *nan;
4630 put_NXAST_NOTE(out);
4631 out->size = out->size - sizeof nan->note;
4633 ofpbuf_put(out, note->data, note->length);
4634 pad_ofpat(out, start_ofs);
4637 static char * OVS_WARN_UNUSED_RESULT
4638 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4639 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4641 size_t start_ofs = ofpacts->size;
4642 ofpact_put_NOTE(ofpacts);
4643 arg = ofpbuf_put_hex(ofpacts, arg, NULL);
4645 return xstrdup("bad hex digit in `note' argument");
4647 struct ofpact_note *note = ofpbuf_at_assert(ofpacts, start_ofs,
4649 note->length = ofpacts->size - (start_ofs + sizeof *note);
4650 ofpact_finish_NOTE(ofpacts, ¬e);
4655 format_NOTE(const struct ofpact_note *a, struct ds *s)
4657 ds_put_format(s, "%snote:%s", colors.param, colors.end);
4658 format_hex_arg(s, a->data, a->length);
4664 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4666 ofpact_put_EXIT(out);
4671 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4672 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4674 put_NXAST_EXIT(out);
4677 static char * OVS_WARN_UNUSED_RESULT
4678 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4679 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4681 ofpact_put_EXIT(ofpacts);
4686 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4688 ds_put_format(s, "%sexit%s", colors.special, colors.end);
4691 /* Unroll xlate action. */
4694 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4695 enum ofp_version ofp_version OVS_UNUSED,
4696 struct ofpbuf *out OVS_UNUSED)
4701 static char * OVS_WARN_UNUSED_RESULT
4702 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4703 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4710 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a, struct ds *s)
4712 ds_put_format(s, "%sunroll_xlate(%s%stable=%s%"PRIu8
4713 ", %scookie=%s%"PRIu64"%s)%s",
4714 colors.paren, colors.end,
4715 colors.special, colors.end, a->rule_table_id,
4716 colors.param, colors.end, ntohll(a->rule_cookie),
4717 colors.paren, colors.end);
4720 /* Action structure for NXAST_SAMPLE.
4722 * Samples matching packets with the given probability and sends them
4723 * each to the set of collectors identified with the given ID. The
4724 * probability is expressed as a number of packets to be sampled out
4725 * of USHRT_MAX packets, and must be >0.
4727 * When sending packet samples to IPFIX collectors, the IPFIX flow
4728 * record sent for each sampled packet is associated with the given
4729 * observation domain ID and observation point ID. Each IPFIX flow
4730 * record contain the sampled packet's headers when executing this
4731 * rule. If a sampled packet's headers are modified by previous
4732 * actions in the flow, those modified headers are sent. */
4733 struct nx_action_sample {
4734 ovs_be16 type; /* OFPAT_VENDOR. */
4735 ovs_be16 len; /* Length is 24. */
4736 ovs_be32 vendor; /* NX_VENDOR_ID. */
4737 ovs_be16 subtype; /* NXAST_SAMPLE. */
4738 ovs_be16 probability; /* Fraction of packets to sample. */
4739 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4740 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4741 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4743 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4745 /* Action structure for NXAST_SAMPLE2.
4747 * This replacement for NXAST_SAMPLE makes it support exporting
4748 * egress tunnel information. */
4749 struct nx_action_sample2 {
4750 ovs_be16 type; /* OFPAT_VENDOR. */
4751 ovs_be16 len; /* Length is 32. */
4752 ovs_be32 vendor; /* NX_VENDOR_ID. */
4753 ovs_be16 subtype; /* NXAST_SAMPLE. */
4754 ovs_be16 probability; /* Fraction of packets to sample. */
4755 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4756 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4757 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4758 ovs_be16 sampling_port; /* Sampling port. */
4759 uint8_t pad[6]; /* Pad to a multiple of 8 bytes */
4761 OFP_ASSERT(sizeof(struct nx_action_sample2) == 32);
4764 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4765 enum ofp_version ofp_version OVS_UNUSED,
4768 struct ofpact_sample *sample;
4770 sample = ofpact_put_SAMPLE(out);
4771 sample->ofpact.raw = NXAST_RAW_SAMPLE;
4772 sample->probability = ntohs(nas->probability);
4773 sample->collector_set_id = ntohl(nas->collector_set_id);
4774 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4775 sample->obs_point_id = ntohl(nas->obs_point_id);
4776 /* Default value for sampling port is OFPP_NONE */
4777 sample->sampling_port = OFPP_NONE;
4779 if (sample->probability == 0) {
4780 return OFPERR_OFPBAC_BAD_ARGUMENT;
4787 decode_NXAST_RAW_SAMPLE2(const struct nx_action_sample2 *nas,
4788 enum ofp_version ofp_version OVS_UNUSED,
4791 struct ofpact_sample *sample;
4793 sample = ofpact_put_SAMPLE(out);
4794 sample->ofpact.raw = NXAST_RAW_SAMPLE2;
4795 sample->probability = ntohs(nas->probability);
4796 sample->collector_set_id = ntohl(nas->collector_set_id);
4797 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4798 sample->obs_point_id = ntohl(nas->obs_point_id);
4799 sample->sampling_port = u16_to_ofp(ntohs(nas->sampling_port));
4801 if (sample->probability == 0) {
4802 return OFPERR_OFPBAC_BAD_ARGUMENT;
4809 encode_SAMPLE(const struct ofpact_sample *sample,
4810 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4812 if (sample->ofpact.raw == NXAST_RAW_SAMPLE2
4813 || sample->sampling_port != OFPP_NONE) {
4814 struct nx_action_sample2 *nas = put_NXAST_SAMPLE2(out);
4815 nas->probability = htons(sample->probability);
4816 nas->collector_set_id = htonl(sample->collector_set_id);
4817 nas->obs_domain_id = htonl(sample->obs_domain_id);
4818 nas->obs_point_id = htonl(sample->obs_point_id);
4819 nas->sampling_port = htons(ofp_to_u16(sample->sampling_port));
4821 struct nx_action_sample *nas = put_NXAST_SAMPLE(out);
4822 nas->probability = htons(sample->probability);
4823 nas->collector_set_id = htonl(sample->collector_set_id);
4824 nas->obs_domain_id = htonl(sample->obs_domain_id);
4825 nas->obs_point_id = htonl(sample->obs_point_id);
4829 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4830 * action to 'ofpacts'.
4832 * Returns NULL if successful, otherwise a malloc()'d string describing the
4833 * error. The caller is responsible for freeing the returned string. */
4834 static char * OVS_WARN_UNUSED_RESULT
4835 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4836 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4838 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4839 os->sampling_port = OFPP_NONE;
4842 while (ofputil_parse_key_value(&arg, &key, &value)) {
4845 if (!strcmp(key, "probability")) {
4846 error = str_to_u16(value, "probability", &os->probability);
4847 if (!error && os->probability == 0) {
4848 error = xasprintf("invalid probability value \"%s\"", value);
4850 } else if (!strcmp(key, "collector_set_id")) {
4851 error = str_to_u32(value, &os->collector_set_id);
4852 } else if (!strcmp(key, "obs_domain_id")) {
4853 error = str_to_u32(value, &os->obs_domain_id);
4854 } else if (!strcmp(key, "obs_point_id")) {
4855 error = str_to_u32(value, &os->obs_point_id);
4856 } else if (!strcmp(key, "sampling_port")) {
4857 if (!ofputil_port_from_string(value, &os->sampling_port)) {
4858 error = xasprintf("%s: unknown port", value);
4861 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4868 if (os->probability == 0) {
4869 return xstrdup("non-zero \"probability\" must be specified on sample");
4876 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4878 ds_put_format(s, "%ssample(%s%sprobability=%s%"PRIu16
4879 ",%scollector_set_id=%s%"PRIu32
4880 ",%sobs_domain_id=%s%"PRIu32
4881 ",%sobs_point_id=%s%"PRIu32,
4882 colors.paren, colors.end,
4883 colors.param, colors.end, a->probability,
4884 colors.param, colors.end, a->collector_set_id,
4885 colors.param, colors.end, a->obs_domain_id,
4886 colors.param, colors.end, a->obs_point_id);
4887 if (a->sampling_port != OFPP_NONE) {
4888 ds_put_format(s, ",%ssampling_port=%s%"PRIu16,
4889 colors.param, colors.end, a->sampling_port);
4891 ds_put_format(s, "%s)%s", colors.paren, colors.end);
4894 /* debug_recirc instruction. */
4896 static bool enable_debug;
4899 ofpact_dummy_enable(void)
4901 enable_debug = true;
4905 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4907 if (!enable_debug) {
4908 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4911 ofpact_put_DEBUG_RECIRC(out);
4916 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4917 enum ofp_version ofp_version OVS_UNUSED,
4920 put_NXAST_DEBUG_RECIRC(out);
4923 static char * OVS_WARN_UNUSED_RESULT
4924 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4925 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4927 ofpact_put_DEBUG_RECIRC(ofpacts);
4932 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4934 ds_put_format(s, "%sdebug_recirc%s", colors.value, colors.end);
4937 /* Action structure for NXAST_CT.
4939 * Pass traffic to the connection tracker.
4941 * There are two important concepts to understanding the connection tracking
4942 * interface: Packet state and Connection state. Packets may be "Untracked" or
4943 * "Tracked". Connections may be "Uncommitted" or "Committed".
4947 * Untracked packets have not yet passed through the connection tracker,
4948 * and the connection state for such packets is unknown. In most cases,
4949 * packets entering the OpenFlow pipeline will initially be in the
4950 * untracked state. Untracked packets may become tracked by executing
4951 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4952 * about the connection available, in particular the connection state.
4954 * Tracked packets have previously passed through the connection tracker.
4955 * These packets will remain tracked through until the end of the OpenFlow
4956 * pipeline. Tracked packets which have NXAST_CT executed with a
4957 * "recirc_table" specified will return to the tracked state.
4959 * The packet state is only significant for the duration of packet
4960 * processing within the OpenFlow pipeline.
4962 * - Connection State:
4964 * Multiple packets may be associated with a single connection. Initially,
4965 * all connections are uncommitted. The connection state corresponding to
4966 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4968 * Uncommitted connections have no state stored about them. Uncommitted
4969 * connections may transition into the committed state by executing
4970 * NXAST_CT with the NX_CT_F_COMMIT flag.
4972 * Once a connection becomes committed, information may be gathered about
4973 * the connection by passing subsequent packets through the connection
4974 * tracker, and the state of the connection will be stored beyond the
4975 * lifetime of packet processing.
4977 * Connections may transition back into the uncommitted state due to
4978 * external timers, or due to the contents of packets that are sent to the
4979 * connection tracker. This behaviour is outside of the scope of the
4980 * OpenFlow interface.
4982 * The "zone" specifies a context within which the tracking is done:
4984 * The connection tracking zone is a 16-bit number. Each zone is an
4985 * independent connection tracking context. The connection state for each
4986 * connection is completely separate for each zone, so if a connection
4987 * is committed to zone A, then it will remain uncommitted in zone B.
4988 * If NXAST_CT is executed with the same zone multiple times, later
4989 * executions have no effect.
4991 * If 'zone_src' is nonzero, this specifies that the zone should be
4992 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4993 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4994 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4995 * are the same as the acceptable nxm_header values for the 'src' field of
4998 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4999 * connection tracking zone.
5001 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
5003 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
5004 * packet will be logically cloned prior to executing this action. One
5005 * copy will be sent to the connection tracker, then will be re-injected
5006 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
5007 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
5008 * fields will be populated. The original instance of the packet will
5009 * continue the current actions list. This can be thought of as similar to
5010 * the effect of the "output" action: One copy is sent out (in this case,
5011 * to the connection tracker), but the current copy continues processing.
5013 * It is strongly recommended that this table is later than the current
5014 * table, to prevent loops.
5016 * The "alg" attaches protocol-specific behaviour to this action:
5018 * The ALG is a 16-bit number which specifies that additional
5019 * processing should be applied to this traffic.
5021 * Protocol | Value | Meaning
5022 * --------------------------------------------------------------------
5023 * None | 0 | No protocol-specific behaviour.
5024 * FTP | 21 | Parse FTP control connections and observe the
5025 * | | negotiation of related data connections.
5026 * Other | Other | Unsupported protocols.
5028 * By way of example, if FTP control connections have this action applied
5029 * with the ALG set to FTP (21), then the connection tracker will observe
5030 * the negotiation of data connections. This allows the connection
5031 * tracker to identify subsequent data connections as "related" to this
5032 * existing connection. The "related" flag will be populated in the
5033 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
5036 * Zero or more actions may immediately follow this action. These actions will
5037 * be executed within the context of the connection tracker, and they require
5038 * the NX_CT_F_COMMIT flag to be set.
5040 struct nx_action_conntrack {
5041 ovs_be16 type; /* OFPAT_VENDOR. */
5042 ovs_be16 len; /* At least 24. */
5043 ovs_be32 vendor; /* NX_VENDOR_ID. */
5044 ovs_be16 subtype; /* NXAST_CT. */
5045 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
5046 * Unspecified flag bits must be zero. */
5047 ovs_be32 zone_src; /* Connection tracking context. */
5049 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
5050 ovs_be16 zone_imm; /* Immediate value for zone. */
5052 uint8_t recirc_table; /* Recirculate to a specific table, or
5053 NX_CT_RECIRC_NONE for no recirculation. */
5054 uint8_t pad[3]; /* Zeroes */
5055 ovs_be16 alg; /* Well-known port number for the protocol.
5056 * 0 indicates no ALG is required. */
5057 /* Followed by a sequence of zero or more OpenFlow actions. The length of
5058 * these is included in 'len'. */
5060 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
5063 decode_ct_zone(const struct nx_action_conntrack *nac,
5064 struct ofpact_conntrack *out)
5066 if (nac->zone_src) {
5069 out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
5070 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
5071 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
5072 error = mf_check_src(&out->zone_src, NULL);
5077 if (out->zone_src.n_bits != 16) {
5078 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
5079 out->zone_src.n_bits);
5080 return OFPERR_OFPBAC_BAD_SET_LEN;
5083 out->zone_src.field = NULL;
5084 out->zone_imm = ntohs(nac->zone_imm);
5091 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
5092 enum ofp_version ofp_version, struct ofpbuf *out)
5094 const size_t ct_offset = ofpacts_pull(out);
5095 struct ofpact_conntrack *conntrack = ofpact_put_CT(out);
5096 conntrack->flags = ntohs(nac->flags);
5098 int error = decode_ct_zone(nac, conntrack);
5102 conntrack->recirc_table = nac->recirc_table;
5103 conntrack->alg = ntohs(nac->alg);
5105 ofpbuf_pull(out, sizeof(*conntrack));
5107 struct ofpbuf openflow = ofpbuf_const_initializer(
5108 nac + 1, ntohs(nac->len) - sizeof(*nac));
5109 error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
5111 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
5117 conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
5118 out->header = &conntrack->ofpact;
5119 ofpact_finish_CT(out, &conntrack);
5121 if (conntrack->ofpact.len > sizeof(*conntrack)
5122 && !(conntrack->flags & NX_CT_F_COMMIT)) {
5123 const struct ofpact *a;
5124 size_t ofpacts_len = conntrack->ofpact.len - sizeof(*conntrack);
5126 OFPACT_FOR_EACH (a, conntrack->actions, ofpacts_len) {
5127 if (a->type != OFPACT_NAT || ofpact_get_NAT(a)->flags
5128 || ofpact_get_NAT(a)->range_af != AF_UNSPEC) {
5129 VLOG_WARN_RL(&rl, "CT action requires commit flag if actions "
5130 "other than NAT without arguments are specified.");
5131 error = OFPERR_OFPBAC_BAD_ARGUMENT;
5138 ofpbuf_push_uninit(out, ct_offset);
5143 encode_CT(const struct ofpact_conntrack *conntrack,
5144 enum ofp_version ofp_version, struct ofpbuf *out)
5146 struct nx_action_conntrack *nac;
5147 const size_t ofs = out->size;
5150 nac = put_NXAST_CT(out);
5151 nac->flags = htons(conntrack->flags);
5152 if (conntrack->zone_src.field) {
5153 nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
5154 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
5155 conntrack->zone_src.n_bits);
5157 nac->zone_src = htonl(0);
5158 nac->zone_imm = htons(conntrack->zone_imm);
5160 nac->recirc_table = conntrack->recirc_table;
5161 nac->alg = htons(conntrack->alg);
5163 len = ofpacts_put_openflow_actions(conntrack->actions,
5164 ofpact_ct_get_action_len(conntrack),
5166 len += sizeof(*nac);
5167 nac = ofpbuf_at(out, ofs, sizeof(*nac));
5168 nac->len = htons(len);
5171 static char * OVS_WARN_UNUSED_RESULT parse_NAT(char *arg, struct ofpbuf *,
5172 enum ofputil_protocol * OVS_UNUSED);
5174 /* Parses 'arg' as the argument to a "ct" action, and appends such an
5175 * action to 'ofpacts'.
5177 * Returns NULL if successful, otherwise a malloc()'d string describing the
5178 * error. The caller is responsible for freeing the returned string. */
5179 static char * OVS_WARN_UNUSED_RESULT
5180 parse_CT(char *arg, struct ofpbuf *ofpacts,
5181 enum ofputil_protocol *usable_protocols)
5183 const size_t ct_offset = ofpacts_pull(ofpacts);
5184 struct ofpact_conntrack *oc;
5188 oc = ofpact_put_CT(ofpacts);
5190 oc->recirc_table = NX_CT_RECIRC_NONE;
5191 while (ofputil_parse_key_value(&arg, &key, &value)) {
5192 if (!strcmp(key, "commit")) {
5193 oc->flags |= NX_CT_F_COMMIT;
5194 } else if (!strcmp(key, "table")) {
5195 error = str_to_u8(value, "recirc_table", &oc->recirc_table);
5196 if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
5197 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
5199 } else if (!strcmp(key, "zone")) {
5200 error = str_to_u16(value, "zone", &oc->zone_imm);
5204 error = mf_parse_subfield(&oc->zone_src, value);
5209 } else if (!strcmp(key, "alg")) {
5210 error = str_to_connhelper(value, &oc->alg);
5211 } else if (!strcmp(key, "nat")) {
5212 const size_t nat_offset = ofpacts_pull(ofpacts);
5214 error = parse_NAT(value, ofpacts, usable_protocols);
5215 /* Update CT action pointer and length. */
5216 ofpacts->header = ofpbuf_push_uninit(ofpacts, nat_offset);
5217 oc = ofpacts->header;
5218 } else if (!strcmp(key, "exec")) {
5219 /* Hide existing actions from ofpacts_parse_copy(), so the
5220 * nesting can be handled transparently. */
5221 enum ofputil_protocol usable_protocols2;
5222 const size_t exec_offset = ofpacts_pull(ofpacts);
5224 /* Initializes 'usable_protocol2', fold it back to
5225 * '*usable_protocols' afterwards, so that we do not lose
5226 * restrictions already in there. */
5227 error = ofpacts_parse_copy(value, ofpacts, &usable_protocols2,
5229 *usable_protocols &= usable_protocols2;
5230 ofpacts->header = ofpbuf_push_uninit(ofpacts, exec_offset);
5231 oc = ofpacts->header;
5233 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
5240 ofpact_finish_CT(ofpacts, &oc);
5241 ofpbuf_push_uninit(ofpacts, ct_offset);
5246 format_alg(int port, struct ds *s)
5248 if (port == IPPORT_FTP) {
5249 ds_put_format(s, "%salg=%sftp,", colors.param, colors.end);
5251 ds_put_format(s, "%salg=%s%d,", colors.param, colors.end, port);
5255 static void format_NAT(const struct ofpact_nat *a, struct ds *ds);
5258 format_CT(const struct ofpact_conntrack *a, struct ds *s)
5260 ds_put_format(s, "%sct(%s", colors.paren, colors.end);
5261 if (a->flags & NX_CT_F_COMMIT) {
5262 ds_put_format(s, "%scommit%s,", colors.value, colors.end);
5264 if (a->recirc_table != NX_CT_RECIRC_NONE) {
5265 ds_put_format(s, "%stable=%s%"PRIu8",",
5266 colors.special, colors.end, a->recirc_table);
5268 if (a->zone_src.field) {
5269 ds_put_format(s, "%szone=%s", colors.param, colors.end);
5270 mf_format_subfield(&a->zone_src, s);
5271 ds_put_char(s, ',');
5272 } else if (a->zone_imm) {
5273 ds_put_format(s, "%szone=%s%"PRIu16",",
5274 colors.param, colors.end, a->zone_imm);
5276 /* If the first action is a NAT action, format it outside of the 'exec'
5278 const struct ofpact *action = a->actions;
5279 size_t actions_len = ofpact_ct_get_action_len(a);
5280 if (actions_len && action->type == OFPACT_NAT) {
5281 format_NAT(ofpact_get_NAT(action), s);
5282 ds_put_char(s, ',');
5283 actions_len -= OFPACT_ALIGN(action->len);
5284 action = ofpact_next(action);
5287 ds_put_format(s, "%sexec(%s", colors.paren, colors.end);
5288 ofpacts_format(action, actions_len, s);
5289 ds_put_format(s, "%s),%s", colors.paren, colors.end);
5291 format_alg(a->alg, s);
5293 ds_put_format(s, "%s)%s", colors.paren, colors.end);
5298 /* Which optional fields are present? */
5300 NX_NAT_RANGE_IPV4_MIN = 1 << 0, /* ovs_be32 */
5301 NX_NAT_RANGE_IPV4_MAX = 1 << 1, /* ovs_be32 */
5302 NX_NAT_RANGE_IPV6_MIN = 1 << 2, /* struct in6_addr */
5303 NX_NAT_RANGE_IPV6_MAX = 1 << 3, /* struct in6_addr */
5304 NX_NAT_RANGE_PROTO_MIN = 1 << 4, /* ovs_be16 */
5305 NX_NAT_RANGE_PROTO_MAX = 1 << 5, /* ovs_be16 */
5308 /* Action structure for NXAST_NAT. */
5309 struct nx_action_nat {
5310 ovs_be16 type; /* OFPAT_VENDOR. */
5311 ovs_be16 len; /* At least 16. */
5312 ovs_be32 vendor; /* NX_VENDOR_ID. */
5313 ovs_be16 subtype; /* NXAST_NAT. */
5314 uint8_t pad[2]; /* Must be zero. */
5315 ovs_be16 flags; /* Zero or more NX_NAT_F_* flags.
5316 * Unspecified flag bits must be zero. */
5317 ovs_be16 range_present; /* NX_NAT_RANGE_* */
5318 /* Followed by optional parameters as specified by 'range_present' */
5320 OFP_ASSERT(sizeof(struct nx_action_nat) == 16);
5323 encode_NAT(const struct ofpact_nat *nat,
5324 enum ofp_version ofp_version OVS_UNUSED,
5327 struct nx_action_nat *nan;
5328 const size_t ofs = out->size;
5329 uint16_t range_present = 0;
5331 nan = put_NXAST_NAT(out);
5332 nan->flags = htons(nat->flags);
5333 if (nat->range_af == AF_INET) {
5334 if (nat->range.addr.ipv4.min) {
5335 ovs_be32 *min = ofpbuf_put_uninit(out, sizeof *min);
5336 *min = nat->range.addr.ipv4.min;
5337 range_present |= NX_NAT_RANGE_IPV4_MIN;
5339 if (nat->range.addr.ipv4.max) {
5340 ovs_be32 *max = ofpbuf_put_uninit(out, sizeof *max);
5341 *max = nat->range.addr.ipv4.max;
5342 range_present |= NX_NAT_RANGE_IPV4_MAX;
5344 } else if (nat->range_af == AF_INET6) {
5345 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5346 struct in6_addr *min = ofpbuf_put_uninit(out, sizeof *min);
5347 *min = nat->range.addr.ipv6.min;
5348 range_present |= NX_NAT_RANGE_IPV6_MIN;
5350 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.max)) {
5351 struct in6_addr *max = ofpbuf_put_uninit(out, sizeof *max);
5352 *max = nat->range.addr.ipv6.max;
5353 range_present |= NX_NAT_RANGE_IPV6_MAX;
5356 if (nat->range_af != AF_UNSPEC) {
5357 if (nat->range.proto.min) {
5358 ovs_be16 *min = ofpbuf_put_uninit(out, sizeof *min);
5359 *min = htons(nat->range.proto.min);
5360 range_present |= NX_NAT_RANGE_PROTO_MIN;
5362 if (nat->range.proto.max) {
5363 ovs_be16 *max = ofpbuf_put_uninit(out, sizeof *max);
5364 *max = htons(nat->range.proto.max);
5365 range_present |= NX_NAT_RANGE_PROTO_MAX;
5368 pad_ofpat(out, ofs);
5369 nan = ofpbuf_at(out, ofs, sizeof *nan);
5370 nan->range_present = htons(range_present);
5374 decode_NXAST_RAW_NAT(const struct nx_action_nat *nan,
5375 enum ofp_version ofp_version OVS_UNUSED,
5378 struct ofpact_nat *nat;
5379 uint16_t range_present = ntohs(nan->range_present);
5380 const char *opts = (char *)(nan + 1);
5381 uint16_t len = ntohs(nan->len) - sizeof *nan;
5383 nat = ofpact_put_NAT(out);
5384 nat->flags = ntohs(nan->flags);
5386 /* Check for unknown or mutually exclusive flags. */
5387 if ((nat->flags & ~NX_NAT_F_MASK)
5388 || (nat->flags & NX_NAT_F_SRC && nat->flags & NX_NAT_F_DST)
5389 || (nat->flags & NX_NAT_F_PROTO_HASH
5390 && nat->flags & NX_NAT_F_PROTO_RANDOM)) {
5391 return OFPERR_OFPBAC_BAD_ARGUMENT;
5394 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
5395 (LEN >= sizeof(TYPE) \
5396 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
5397 SRC += sizeof(TYPE)) \
5400 nat->range_af = AF_UNSPEC;
5401 if (range_present & NX_NAT_RANGE_IPV4_MIN) {
5402 if (range_present & (NX_NAT_RANGE_IPV6_MIN | NX_NAT_RANGE_IPV6_MAX)) {
5403 return OFPERR_OFPBAC_BAD_ARGUMENT;
5406 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.min, opts, len, ovs_be32)
5407 || !nat->range.addr.ipv4.min) {
5408 return OFPERR_OFPBAC_BAD_ARGUMENT;
5411 nat->range_af = AF_INET;
5413 if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5414 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.max, opts, len,
5416 return OFPERR_OFPBAC_BAD_ARGUMENT;
5418 if (ntohl(nat->range.addr.ipv4.max)
5419 < ntohl(nat->range.addr.ipv4.min)) {
5420 return OFPERR_OFPBAC_BAD_ARGUMENT;
5423 } else if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5424 return OFPERR_OFPBAC_BAD_ARGUMENT;
5425 } else if (range_present & NX_NAT_RANGE_IPV6_MIN) {
5426 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.min, opts, len,
5428 || ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5429 return OFPERR_OFPBAC_BAD_ARGUMENT;
5432 nat->range_af = AF_INET6;
5434 if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5435 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.max, opts, len,
5437 return OFPERR_OFPBAC_BAD_ARGUMENT;
5439 if (memcmp(&nat->range.addr.ipv6.max, &nat->range.addr.ipv6.min,
5440 sizeof(struct in6_addr)) < 0) {
5441 return OFPERR_OFPBAC_BAD_ARGUMENT;
5444 } else if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5445 return OFPERR_OFPBAC_BAD_ARGUMENT;
5448 if (range_present & NX_NAT_RANGE_PROTO_MIN) {
5451 if (nat->range_af == AF_UNSPEC) {
5452 return OFPERR_OFPBAC_BAD_ARGUMENT;
5454 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16) || proto == 0) {
5455 return OFPERR_OFPBAC_BAD_ARGUMENT;
5457 nat->range.proto.min = ntohs(proto);
5458 if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5459 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16)) {
5460 return OFPERR_OFPBAC_BAD_ARGUMENT;
5462 nat->range.proto.max = ntohs(proto);
5463 if (nat->range.proto.max < nat->range.proto.min) {
5464 return OFPERR_OFPBAC_BAD_ARGUMENT;
5467 } else if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5468 return OFPERR_OFPBAC_BAD_ARGUMENT;
5475 format_NAT(const struct ofpact_nat *a, struct ds *ds)
5477 ds_put_format(ds, "%snat%s", colors.paren, colors.end);
5479 if (a->flags & (NX_NAT_F_SRC | NX_NAT_F_DST)) {
5480 ds_put_format(ds, "%s(%s", colors.paren, colors.end);
5481 ds_put_format(ds, a->flags & NX_NAT_F_SRC ? "%ssrc%s" : "%sdst%s",
5482 colors.param, colors.end);
5484 if (a->range_af != AF_UNSPEC) {
5485 ds_put_format(ds, "%s=%s", colors.param, colors.end);
5487 if (a->range_af == AF_INET) {
5488 ds_put_format(ds, IP_FMT, IP_ARGS(a->range.addr.ipv4.min));
5490 if (a->range.addr.ipv4.max
5491 && a->range.addr.ipv4.max != a->range.addr.ipv4.min) {
5492 ds_put_format(ds, "-"IP_FMT,
5493 IP_ARGS(a->range.addr.ipv4.max));
5495 } else if (a->range_af == AF_INET6) {
5496 ipv6_format_addr_bracket(&a->range.addr.ipv6.min, ds,
5497 a->range.proto.min);
5499 if (!ipv6_mask_is_any(&a->range.addr.ipv6.max)
5500 && memcmp(&a->range.addr.ipv6.max, &a->range.addr.ipv6.min,
5501 sizeof(struct in6_addr)) != 0) {
5502 ds_put_char(ds, '-');
5503 ipv6_format_addr_bracket(&a->range.addr.ipv6.max, ds,
5504 a->range.proto.min);
5507 if (a->range.proto.min) {
5508 ds_put_char(ds, ':');
5509 ds_put_format(ds, "%"PRIu16, a->range.proto.min);
5511 if (a->range.proto.max
5512 && a->range.proto.max != a->range.proto.min) {
5513 ds_put_format(ds, "-%"PRIu16, a->range.proto.max);
5516 ds_put_char(ds, ',');
5518 if (a->flags & NX_NAT_F_PERSISTENT) {
5519 ds_put_format(ds, "%spersistent%s,",
5520 colors.value, colors.end);
5522 if (a->flags & NX_NAT_F_PROTO_HASH) {
5523 ds_put_format(ds, "%shash%s,", colors.value, colors.end);
5525 if (a->flags & NX_NAT_F_PROTO_RANDOM) {
5526 ds_put_format(ds, "%srandom%s,", colors.value, colors.end);
5530 ds_put_format(ds, "%s)%s", colors.paren, colors.end);
5534 static char * OVS_WARN_UNUSED_RESULT
5535 str_to_nat_range(const char *s, struct ofpact_nat *on)
5537 char ipv6_s[IPV6_SCAN_LEN + 1];
5540 on->range_af = AF_UNSPEC;
5541 if (ovs_scan_len(s, &n, IP_SCAN_FMT,
5542 IP_SCAN_ARGS(&on->range.addr.ipv4.min))) {
5543 on->range_af = AF_INET;
5547 if (!ovs_scan_len(s, &n, IP_SCAN_FMT,
5548 IP_SCAN_ARGS(&on->range.addr.ipv4.max))
5549 || (ntohl(on->range.addr.ipv4.max)
5550 < ntohl(on->range.addr.ipv4.min))) {
5554 } else if ((ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5555 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5556 && inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.min) == 1) {
5557 on->range_af = AF_INET6;
5561 if (!(ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5562 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5563 || inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.max) != 1
5564 || memcmp(&on->range.addr.ipv6.max, &on->range.addr.ipv6.min,
5565 sizeof on->range.addr.ipv6.max) < 0) {
5570 if (on->range_af != AF_UNSPEC && s[n] == ':') {
5572 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.min)) {
5577 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.max)
5578 || on->range.proto.max < on->range.proto.min) {
5583 if (strlen(s) != n) {
5584 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
5589 return xasprintf("invalid nat range \"%s\"", s);
5593 /* Parses 'arg' as the argument to a "nat" action, and appends such an
5594 * action to 'ofpacts'.
5596 * Returns NULL if successful, otherwise a malloc()'d string describing the
5597 * error. The caller is responsible for freeing the returned string. */
5598 static char * OVS_WARN_UNUSED_RESULT
5599 parse_NAT(char *arg, struct ofpbuf *ofpacts,
5600 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5602 struct ofpact_nat *on = ofpact_put_NAT(ofpacts);
5606 on->range_af = AF_UNSPEC;
5608 while (ofputil_parse_key_value(&arg, &key, &value)) {
5611 if (!strcmp(key, "src")) {
5612 on->flags |= NX_NAT_F_SRC;
5613 error = str_to_nat_range(value, on);
5614 } else if (!strcmp(key, "dst")) {
5615 on->flags |= NX_NAT_F_DST;
5616 error = str_to_nat_range(value, on);
5617 } else if (!strcmp(key, "persistent")) {
5618 on->flags |= NX_NAT_F_PERSISTENT;
5619 } else if (!strcmp(key, "hash")) {
5620 on->flags |= NX_NAT_F_PROTO_HASH;
5621 } else if (!strcmp(key, "random")) {
5622 on->flags |= NX_NAT_F_PROTO_RANDOM;
5624 error = xasprintf("invalid key \"%s\" in \"nat\" argument",
5631 if (on->flags & NX_NAT_F_SRC && on->flags & NX_NAT_F_DST) {
5632 return xasprintf("May only specify one of \"src\" or \"dst\".");
5634 if (!(on->flags & NX_NAT_F_SRC || on->flags & NX_NAT_F_DST)) {
5636 return xasprintf("Flags allowed only with \"src\" or \"dst\".");
5638 if (on->range_af != AF_UNSPEC) {
5639 return xasprintf("Range allowed only with \"src\" or \"dst\".");
5642 if (on->flags & NX_NAT_F_PROTO_HASH && on->flags & NX_NAT_F_PROTO_RANDOM) {
5643 return xasprintf("Both \"hash\" and \"random\" are not allowed.");
5649 /* Truncate output action. */
5650 struct nx_action_output_trunc {
5651 ovs_be16 type; /* OFPAT_VENDOR. */
5652 ovs_be16 len; /* At least 16. */
5653 ovs_be32 vendor; /* NX_VENDOR_ID. */
5654 ovs_be16 subtype; /* NXAST_OUTPUT_TRUNC. */
5655 ovs_be16 port; /* Output port */
5656 ovs_be32 max_len; /* Truncate packet to size bytes */
5658 OFP_ASSERT(sizeof(struct nx_action_output_trunc) == 16);
5661 decode_NXAST_RAW_OUTPUT_TRUNC(const struct nx_action_output_trunc *natrc,
5662 enum ofp_version ofp_version OVS_UNUSED,
5665 struct ofpact_output_trunc *output_trunc;
5667 output_trunc = ofpact_put_OUTPUT_TRUNC(out);
5668 output_trunc->max_len = ntohl(natrc->max_len);
5669 output_trunc->port = u16_to_ofp(ntohs(natrc->port));
5671 if (output_trunc->max_len < ETH_HEADER_LEN) {
5672 return OFPERR_OFPBAC_BAD_ARGUMENT;
5678 encode_OUTPUT_TRUNC(const struct ofpact_output_trunc *output_trunc,
5679 enum ofp_version ofp_version OVS_UNUSED,
5682 struct nx_action_output_trunc *natrc = put_NXAST_OUTPUT_TRUNC(out);
5684 natrc->max_len = htonl(output_trunc->max_len);
5685 natrc->port = htons(ofp_to_u16(output_trunc->port));
5688 static char * OVS_WARN_UNUSED_RESULT
5689 parse_OUTPUT_TRUNC(const char *arg, struct ofpbuf *ofpacts OVS_UNUSED,
5690 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5692 /* Disable output_trunc parsing. Expose as output(port=N,max_len=M) and
5693 * reuse parse_OUTPUT to parse output_trunc action. */
5694 return xasprintf("unknown action %s", arg);
5698 format_OUTPUT_TRUNC(const struct ofpact_output_trunc *a, struct ds *s)
5700 ds_put_format(s, "%soutput%s(port=%"PRIu16",max_len=%"PRIu32")",
5701 colors.special, colors.end, a->port, a->max_len);
5705 /* Meter instruction. */
5708 encode_METER(const struct ofpact_meter *meter,
5709 enum ofp_version ofp_version, struct ofpbuf *out)
5711 if (ofp_version >= OFP13_VERSION) {
5712 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
5716 static char * OVS_WARN_UNUSED_RESULT
5717 parse_METER(char *arg, struct ofpbuf *ofpacts,
5718 enum ofputil_protocol *usable_protocols)
5720 *usable_protocols &= OFPUTIL_P_OF13_UP;
5721 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
5725 format_METER(const struct ofpact_meter *a, struct ds *s)
5727 ds_put_format(s, "%smeter:%s%"PRIu32,
5728 colors.param, colors.end, a->meter_id);
5731 /* Clear-Actions instruction. */
5734 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
5735 enum ofp_version ofp_version OVS_UNUSED,
5736 struct ofpbuf *out OVS_UNUSED)
5738 if (ofp_version > OFP10_VERSION) {
5739 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
5743 static char * OVS_WARN_UNUSED_RESULT
5744 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
5745 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5747 ofpact_put_CLEAR_ACTIONS(ofpacts);
5752 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
5754 ds_put_format(s, "%sclear_actions%s", colors.value, colors.end);
5757 /* Write-Actions instruction. */
5760 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
5761 enum ofp_version ofp_version, struct ofpbuf *out)
5763 if (ofp_version > OFP10_VERSION) {
5764 const size_t ofs = out->size;
5766 instruction_put_OFPIT11_WRITE_ACTIONS(out);
5767 ofpacts_put_openflow_actions(actions->actions,
5768 ofpact_nest_get_action_len(actions),
5770 ofpacts_update_instruction_actions(out, ofs);
5774 static char * OVS_WARN_UNUSED_RESULT
5775 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
5776 enum ofputil_protocol *usable_protocols)
5778 size_t ofs = ofpacts_pull(ofpacts);
5779 struct ofpact_nest *on;
5782 /* Add a Write-Actions instruction and then pull it off. */
5783 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
5784 ofpbuf_pull(ofpacts, sizeof *on);
5786 /* Parse nested actions.
5788 * We pulled off "write-actions" and the previous actions because the
5789 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
5790 * that it doesn't actually include the nested actions. That means that
5791 * ofpacts_parse() would reject them as being part of an Apply-Actions that
5792 * follows a Write-Actions, which is an invalid order. */
5793 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
5794 OFPACT_WRITE_ACTIONS);
5796 /* Put the Write-Actions back on and update its length. */
5797 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
5798 on->ofpact.len = ofpacts->size;
5800 /* Put any previous actions or instructions back on. */
5801 ofpbuf_push_uninit(ofpacts, ofs);
5807 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
5809 ds_put_format(s, "%swrite_actions(%s", colors.paren, colors.end);
5810 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
5811 ds_put_format(s, "%s)%s", colors.paren, colors.end);
5814 /* Action structure for NXAST_WRITE_METADATA.
5816 * Modifies the 'mask' bits of the metadata value. */
5817 struct nx_action_write_metadata {
5818 ovs_be16 type; /* OFPAT_VENDOR. */
5819 ovs_be16 len; /* Length is 32. */
5820 ovs_be32 vendor; /* NX_VENDOR_ID. */
5821 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
5822 uint8_t zeros[6]; /* Must be zero. */
5823 ovs_be64 metadata; /* Metadata register. */
5824 ovs_be64 mask; /* Metadata mask. */
5826 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
5829 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
5830 enum ofp_version ofp_version OVS_UNUSED,
5833 struct ofpact_metadata *om;
5835 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
5836 return OFPERR_NXBRC_MUST_BE_ZERO;
5839 om = ofpact_put_WRITE_METADATA(out);
5840 om->metadata = nawm->metadata;
5841 om->mask = nawm->mask;
5847 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
5848 enum ofp_version ofp_version, struct ofpbuf *out)
5850 if (ofp_version == OFP10_VERSION) {
5851 struct nx_action_write_metadata *nawm;
5853 nawm = put_NXAST_WRITE_METADATA(out);
5854 nawm->metadata = metadata->metadata;
5855 nawm->mask = metadata->mask;
5857 struct ofp11_instruction_write_metadata *oiwm;
5859 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
5860 oiwm->metadata = metadata->metadata;
5861 oiwm->metadata_mask = metadata->mask;
5865 static char * OVS_WARN_UNUSED_RESULT
5866 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
5867 enum ofputil_protocol *usable_protocols)
5869 struct ofpact_metadata *om;
5870 char *mask = strchr(arg, '/');
5872 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
5874 om = ofpact_put_WRITE_METADATA(ofpacts);
5879 error = str_to_be64(mask + 1, &om->mask);
5884 om->mask = OVS_BE64_MAX;
5887 return str_to_be64(arg, &om->metadata);
5891 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5893 ds_put_format(s, "%swrite_metadata:%s%#"PRIx64,
5894 colors.param, colors.end, ntohll(a->metadata));
5895 if (a->mask != OVS_BE64_MAX) {
5896 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5900 /* Goto-Table instruction. */
5903 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5904 enum ofp_version ofp_version, struct ofpbuf *out)
5906 if (ofp_version == OFP10_VERSION) {
5907 struct nx_action_resubmit *nar;
5909 nar = put_NXAST_RESUBMIT_TABLE(out);
5910 nar->table = goto_table->table_id;
5911 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5913 struct ofp11_instruction_goto_table *oigt;
5915 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5916 oigt->table_id = goto_table->table_id;
5917 memset(oigt->pad, 0, sizeof oigt->pad);
5921 static char * OVS_WARN_UNUSED_RESULT
5922 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5923 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5925 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5926 char *table_s = strsep(&arg, ",");
5927 if (!table_s || !table_s[0]) {
5928 return xstrdup("instruction goto-table needs table id");
5930 return str_to_u8(table_s, "table", &ogt->table_id);
5934 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5936 ds_put_format(s, "%sgoto_table:%s%"PRIu8,
5937 colors.param, colors.end, a->table_id);
5941 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
5942 const struct ofp_action_header *bad_action, enum ofperr error)
5944 if (!VLOG_DROP_WARN(&rl)) {
5948 ds_put_hex_dump(&s, actions, actions_len, 0, false);
5949 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5950 (char *)bad_action - (char *)actions,
5951 ofperr_get_name(error), ds_cstr(&s));
5957 ofpacts_decode(const void *actions, size_t actions_len,
5958 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5960 struct ofpbuf openflow = ofpbuf_const_initializer(actions, actions_len);
5961 while (openflow.size) {
5962 const struct ofp_action_header *action = openflow.data;
5963 enum ofp_raw_action_type raw;
5967 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5969 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
5973 log_bad_action(actions, actions_len, action, error);
5981 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5982 unsigned int actions_len,
5983 enum ofp_version version,
5984 uint32_t allowed_ovsinsts,
5985 struct ofpbuf *ofpacts,
5986 enum ofpact_type outer_action)
5988 const struct ofp_action_header *actions;
5989 size_t orig_size = ofpacts->size;
5992 if (actions_len % OFP_ACTION_ALIGN != 0) {
5993 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5994 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5995 return OFPERR_OFPBRC_BAD_LEN;
5998 actions = ofpbuf_try_pull(openflow, actions_len);
5999 if (actions == NULL) {
6000 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
6001 "remaining message length (%"PRIu32")",
6002 actions_len, openflow->size);
6003 return OFPERR_OFPBRC_BAD_LEN;
6006 error = ofpacts_decode(actions, actions_len, version, ofpacts);
6008 ofpacts->size = orig_size;
6012 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
6015 ofpacts->size = orig_size;
6020 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
6021 * of 'openflow' into ofpacts. On success, appends the converted actions to
6022 * 'ofpacts'; on failure, 'ofpacts' is unchanged (but might be reallocated) .
6023 * Returns 0 if successful, otherwise an OpenFlow error.
6025 * Actions are processed according to their OpenFlow version which
6026 * is provided in the 'version' parameter.
6028 * In most places in OpenFlow, actions appear encapsulated in instructions, so
6029 * you should call ofpacts_pull_openflow_instructions() instead of this
6032 * The parsed actions are valid generically, but they may not be valid in a
6033 * specific context. For example, port numbers up to OFPP_MAX are valid
6034 * generically, but specific datapaths may only support port numbers in a
6035 * smaller range. Use ofpacts_check() to additional check whether actions are
6036 * valid in a specific context. */
6038 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
6039 unsigned int actions_len,
6040 enum ofp_version version,
6041 struct ofpbuf *ofpacts)
6043 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
6044 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
6048 /* OpenFlow 1.1 actions. */
6051 /* True if an action sets the value of a field
6052 * in a way that is compatibile with the action set.
6053 * The field can be set via either a set or a move action.
6054 * False otherwise. */
6056 ofpact_is_set_or_move_action(const struct ofpact *a)
6059 case OFPACT_SET_FIELD:
6060 case OFPACT_REG_MOVE:
6061 case OFPACT_SET_ETH_DST:
6062 case OFPACT_SET_ETH_SRC:
6063 case OFPACT_SET_IP_DSCP:
6064 case OFPACT_SET_IP_ECN:
6065 case OFPACT_SET_IP_TTL:
6066 case OFPACT_SET_IPV4_DST:
6067 case OFPACT_SET_IPV4_SRC:
6068 case OFPACT_SET_L4_DST_PORT:
6069 case OFPACT_SET_L4_SRC_PORT:
6070 case OFPACT_SET_MPLS_LABEL:
6071 case OFPACT_SET_MPLS_TC:
6072 case OFPACT_SET_MPLS_TTL:
6073 case OFPACT_SET_QUEUE:
6074 case OFPACT_SET_TUNNEL:
6075 case OFPACT_SET_VLAN_PCP:
6076 case OFPACT_SET_VLAN_VID:
6079 case OFPACT_CLEAR_ACTIONS:
6082 case OFPACT_CONTROLLER:
6083 case OFPACT_DEC_MPLS_TTL:
6084 case OFPACT_DEC_TTL:
6085 case OFPACT_ENQUEUE:
6087 case OFPACT_UNROLL_XLATE:
6088 case OFPACT_FIN_TIMEOUT:
6089 case OFPACT_GOTO_TABLE:
6092 case OFPACT_CONJUNCTION:
6094 case OFPACT_MULTIPATH:
6097 case OFPACT_OUTPUT_REG:
6098 case OFPACT_OUTPUT_TRUNC:
6099 case OFPACT_POP_MPLS:
6100 case OFPACT_POP_QUEUE:
6101 case OFPACT_PUSH_MPLS:
6102 case OFPACT_PUSH_VLAN:
6103 case OFPACT_RESUBMIT:
6105 case OFPACT_STACK_POP:
6106 case OFPACT_STACK_PUSH:
6107 case OFPACT_STRIP_VLAN:
6108 case OFPACT_WRITE_ACTIONS:
6109 case OFPACT_WRITE_METADATA:
6110 case OFPACT_DEBUG_RECIRC:
6117 /* True if an action is allowed in the action set.
6118 * False otherwise. */
6120 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
6123 case OFPACT_DEC_MPLS_TTL:
6124 case OFPACT_DEC_TTL:
6127 case OFPACT_OUTPUT_TRUNC:
6128 case OFPACT_POP_MPLS:
6129 case OFPACT_PUSH_MPLS:
6130 case OFPACT_PUSH_VLAN:
6131 case OFPACT_REG_MOVE:
6132 case OFPACT_SET_FIELD:
6133 case OFPACT_SET_ETH_DST:
6134 case OFPACT_SET_ETH_SRC:
6135 case OFPACT_SET_IP_DSCP:
6136 case OFPACT_SET_IP_ECN:
6137 case OFPACT_SET_IP_TTL:
6138 case OFPACT_SET_IPV4_DST:
6139 case OFPACT_SET_IPV4_SRC:
6140 case OFPACT_SET_L4_DST_PORT:
6141 case OFPACT_SET_L4_SRC_PORT:
6142 case OFPACT_SET_MPLS_LABEL:
6143 case OFPACT_SET_MPLS_TC:
6144 case OFPACT_SET_MPLS_TTL:
6145 case OFPACT_SET_QUEUE:
6146 case OFPACT_SET_TUNNEL:
6147 case OFPACT_SET_VLAN_PCP:
6148 case OFPACT_SET_VLAN_VID:
6149 case OFPACT_STRIP_VLAN:
6152 /* In general these actions are excluded because they are not part of
6153 * the OpenFlow specification nor map to actions that are defined in
6154 * the specification. Thus the order in which they should be applied
6155 * in the action set is undefined. */
6157 case OFPACT_CONTROLLER:
6160 case OFPACT_ENQUEUE:
6162 case OFPACT_UNROLL_XLATE:
6163 case OFPACT_FIN_TIMEOUT:
6165 case OFPACT_CONJUNCTION:
6166 case OFPACT_MULTIPATH:
6168 case OFPACT_OUTPUT_REG:
6169 case OFPACT_POP_QUEUE:
6170 case OFPACT_RESUBMIT:
6172 case OFPACT_STACK_POP:
6173 case OFPACT_STACK_PUSH:
6174 case OFPACT_DEBUG_RECIRC:
6176 /* The action set may only include actions and thus
6177 * may not include any instructions */
6178 case OFPACT_CLEAR_ACTIONS:
6179 case OFPACT_GOTO_TABLE:
6181 case OFPACT_WRITE_ACTIONS:
6182 case OFPACT_WRITE_METADATA:
6189 /* Append ofpact 'a' onto the tail of 'out' */
6191 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
6193 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
6196 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
6198 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
6199 enum ofpact_type filter)
6201 const struct ofpact *target;
6202 const struct ofpact *a;
6205 OFPACT_FOR_EACH (a, in->data, in->size) {
6206 if (a->type == filter) {
6211 ofpact_copy(out, target);
6213 return target != NULL;
6216 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
6217 * The order of appended ofpacts is preserved between 'in' and 'out' */
6219 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
6220 bool (*filter)(const struct ofpact *))
6222 const struct ofpact *a;
6224 OFPACT_FOR_EACH (a, in->data, in->size) {
6226 ofpact_copy(out, a);
6231 /* Reads 'action_set', which contains ofpacts accumulated by
6232 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
6233 * executed directly into 'action_list'. (These names correspond to the
6234 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
6236 * In general this involves appending the last instance of each action that is
6237 * admissible in the action set in the order described in the OpenFlow
6241 * + output action is only appended if no group action was present in 'in'.
6242 * + As a simplification all set actions are copied in the order the are
6243 * provided in 'in' as many set actions applied to a field has the same
6244 * affect as only applying the last action that sets a field and
6245 * duplicates are removed by do_xlate_actions().
6246 * This has an unwanted side-effect of compsoting multiple
6247 * LOAD_REG actions that touch different regions of the same field. */
6249 ofpacts_execute_action_set(struct ofpbuf *action_list,
6250 const struct ofpbuf *action_set)
6252 /* The OpenFlow spec "Action Set" section specifies this order. */
6253 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
6254 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
6255 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
6256 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
6257 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
6258 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
6259 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
6260 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
6262 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
6263 * we should execute only OFPACT_GROUP.
6265 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
6266 * all the actions because there's no point in modifying a packet that will
6267 * not be sent anywhere. */
6268 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
6269 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
6270 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT) &&
6271 !ofpacts_copy_last(action_list, action_set, OFPACT_CT)) {
6272 ofpbuf_clear(action_list);
6278 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
6279 size_t n_in, enum ofp_version version,
6284 size_t start = out->size;
6286 error = ofpacts_decode(in, n_in, version, out);
6292 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
6293 if (!ofpact_is_allowed_in_actions_set(a)) {
6294 VLOG_WARN_RL(&rl, "disallowed action in action set");
6295 return OFPERR_OFPBAC_BAD_TYPE;
6302 /* OpenFlow 1.1 instructions. */
6304 struct instruction_type_info {
6305 enum ovs_instruction_type type;
6309 static const struct instruction_type_info inst_info[] = {
6310 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
6316 ovs_instruction_name_from_type(enum ovs_instruction_type type)
6318 return inst_info[type].name;
6322 ovs_instruction_type_from_name(const char *name)
6324 const struct instruction_type_info *p;
6325 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
6326 if (!strcasecmp(name, p->name)) {
6333 enum ovs_instruction_type
6334 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
6338 return OVSINST_OFPIT13_METER;
6339 case OFPACT_CLEAR_ACTIONS:
6340 return OVSINST_OFPIT11_CLEAR_ACTIONS;
6341 case OFPACT_WRITE_ACTIONS:
6342 return OVSINST_OFPIT11_WRITE_ACTIONS;
6343 case OFPACT_WRITE_METADATA:
6344 return OVSINST_OFPIT11_WRITE_METADATA;
6345 case OFPACT_GOTO_TABLE:
6346 return OVSINST_OFPIT11_GOTO_TABLE;
6349 case OFPACT_CONTROLLER:
6350 case OFPACT_ENQUEUE:
6351 case OFPACT_OUTPUT_REG:
6352 case OFPACT_OUTPUT_TRUNC:
6354 case OFPACT_SET_VLAN_VID:
6355 case OFPACT_SET_VLAN_PCP:
6356 case OFPACT_STRIP_VLAN:
6357 case OFPACT_PUSH_VLAN:
6358 case OFPACT_SET_ETH_SRC:
6359 case OFPACT_SET_ETH_DST:
6360 case OFPACT_SET_IPV4_SRC:
6361 case OFPACT_SET_IPV4_DST:
6362 case OFPACT_SET_IP_DSCP:
6363 case OFPACT_SET_IP_ECN:
6364 case OFPACT_SET_IP_TTL:
6365 case OFPACT_SET_L4_SRC_PORT:
6366 case OFPACT_SET_L4_DST_PORT:
6367 case OFPACT_REG_MOVE:
6368 case OFPACT_SET_FIELD:
6369 case OFPACT_STACK_PUSH:
6370 case OFPACT_STACK_POP:
6371 case OFPACT_DEC_TTL:
6372 case OFPACT_SET_MPLS_LABEL:
6373 case OFPACT_SET_MPLS_TC:
6374 case OFPACT_SET_MPLS_TTL:
6375 case OFPACT_DEC_MPLS_TTL:
6376 case OFPACT_PUSH_MPLS:
6377 case OFPACT_POP_MPLS:
6378 case OFPACT_SET_TUNNEL:
6379 case OFPACT_SET_QUEUE:
6380 case OFPACT_POP_QUEUE:
6381 case OFPACT_FIN_TIMEOUT:
6382 case OFPACT_RESUBMIT:
6384 case OFPACT_CONJUNCTION:
6385 case OFPACT_MULTIPATH:
6388 case OFPACT_UNROLL_XLATE:
6390 case OFPACT_DEBUG_RECIRC:
6394 return OVSINST_OFPIT11_APPLY_ACTIONS;
6399 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
6400 const uint16_t inst_type)
6402 switch (inst_type) {
6404 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6406 *instruction_type = OVSINST_##ENUM; \
6412 return OFPERR_OFPBIC_UNKNOWN_INST;
6416 /* Two-way translation between OVS's internal "OVSINST_*" representation of
6417 * instructions and the "OFPIT_*" representation used in OpenFlow. */
6418 struct ovsinst_map {
6419 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
6420 int ofpit; /* OFPIT_* number from OpenFlow spec. */
6423 static const struct ovsinst_map *
6424 get_ovsinst_map(enum ofp_version version)
6426 /* OpenFlow 1.1 and 1.2 instructions. */
6427 static const struct ovsinst_map of11[] = {
6428 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6429 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6430 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6431 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6432 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6436 /* OpenFlow 1.3+ instructions. */
6437 static const struct ovsinst_map of13[] = {
6438 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6439 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6440 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6441 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6442 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6443 { OVSINST_OFPIT13_METER, 6 },
6447 return version < OFP13_VERSION ? of11 : of13;
6450 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
6451 * values, into a bitmap of instructions suitable for OpenFlow 'version'
6452 * (OFP11_VERSION or later), and returns the result. */
6454 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
6456 uint32_t ofpit_bitmap = 0;
6457 const struct ovsinst_map *x;
6459 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6460 if (ovsinst_bitmap & (1u << x->ovsinst)) {
6461 ofpit_bitmap |= 1u << x->ofpit;
6464 return htonl(ofpit_bitmap);
6467 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
6468 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
6469 * correspond to OVSINST_* values, and returns the result. */
6471 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
6473 uint32_t ovsinst_bitmap = 0;
6474 const struct ovsinst_map *x;
6476 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6477 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
6478 ovsinst_bitmap |= 1u << x->ovsinst;
6481 return ovsinst_bitmap;
6484 static inline struct ofp11_instruction *
6485 instruction_next(const struct ofp11_instruction *inst)
6487 return ((struct ofp11_instruction *) (void *)
6488 ((uint8_t *) inst + ntohs(inst->len)));
6492 instruction_is_valid(const struct ofp11_instruction *inst,
6493 size_t n_instructions)
6495 uint16_t len = ntohs(inst->len);
6496 return (!(len % OFP11_INSTRUCTION_ALIGN)
6497 && len >= sizeof *inst
6498 && len / sizeof *inst <= n_instructions);
6501 /* This macro is careful to check for instructions with bad lengths. */
6502 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
6503 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
6504 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
6505 ((LEFT) -= (ntohs((ITER)->len) \
6506 / sizeof(struct ofp11_instruction)), \
6507 (ITER) = instruction_next(ITER)))
6510 decode_openflow11_instruction(const struct ofp11_instruction *inst,
6511 enum ovs_instruction_type *type)
6513 uint16_t len = ntohs(inst->len);
6515 switch (inst->type) {
6516 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
6517 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
6519 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6520 case CONSTANT_HTONS(ENUM): \
6522 ? len >= sizeof(struct STRUCT) \
6523 : len == sizeof(struct STRUCT)) { \
6524 *type = OVSINST_##ENUM; \
6527 return OFPERR_OFPBIC_BAD_LEN; \
6533 return OFPERR_OFPBIC_UNKNOWN_INST;
6538 decode_openflow11_instructions(const struct ofp11_instruction insts[],
6540 const struct ofp11_instruction *out[])
6542 const struct ofp11_instruction *inst;
6545 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
6546 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
6547 enum ovs_instruction_type type;
6550 error = decode_openflow11_instruction(inst, &type);
6556 return OFPERR_OFPBIC_DUP_INST;
6562 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
6563 (n_insts - left) * sizeof *inst);
6564 return OFPERR_OFPBIC_BAD_LEN;
6570 get_actions_from_instruction(const struct ofp11_instruction *inst,
6571 const struct ofp_action_header **actions,
6572 size_t *actions_len)
6574 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
6575 *actions_len = ntohs(inst->len) - sizeof *inst;
6579 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
6580 unsigned int instructions_len,
6581 enum ofp_version version,
6582 struct ofpbuf *ofpacts)
6584 const struct ofp11_instruction *instructions;
6585 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
6588 ofpbuf_clear(ofpacts);
6589 if (version == OFP10_VERSION) {
6590 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
6592 (1u << N_OVS_INSTRUCTIONS) - 1,
6596 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
6597 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
6599 instructions_len, OFP11_INSTRUCTION_ALIGN);
6600 error = OFPERR_OFPBIC_BAD_LEN;
6604 instructions = ofpbuf_try_pull(openflow, instructions_len);
6605 if (instructions == NULL) {
6606 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
6607 "remaining message length (%"PRIu32")",
6608 instructions_len, openflow->size);
6609 error = OFPERR_OFPBIC_BAD_LEN;
6613 error = decode_openflow11_instructions(
6614 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
6620 if (insts[OVSINST_OFPIT13_METER]) {
6621 const struct ofp13_instruction_meter *oim;
6622 struct ofpact_meter *om;
6624 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
6625 insts[OVSINST_OFPIT13_METER]);
6627 om = ofpact_put_METER(ofpacts);
6628 om->meter_id = ntohl(oim->meter_id);
6630 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
6631 const struct ofp_action_header *actions;
6634 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
6635 &actions, &actions_len);
6636 error = ofpacts_decode(actions, actions_len, version, ofpacts);
6641 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
6642 instruction_get_OFPIT11_CLEAR_ACTIONS(
6643 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
6644 ofpact_put_CLEAR_ACTIONS(ofpacts);
6646 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
6647 struct ofpact_nest *on;
6648 const struct ofp_action_header *actions;
6650 size_t start = ofpacts->size;
6651 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
6652 offsetof(struct ofpact_nest, actions));
6653 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
6654 &actions, &actions_len);
6655 error = ofpacts_decode_for_action_set(actions, actions_len,
6660 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
6661 on->ofpact.len = ofpacts->size - start;
6663 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
6664 const struct ofp11_instruction_write_metadata *oiwm;
6665 struct ofpact_metadata *om;
6667 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
6668 insts[OVSINST_OFPIT11_WRITE_METADATA]);
6670 om = ofpact_put_WRITE_METADATA(ofpacts);
6671 om->metadata = oiwm->metadata;
6672 om->mask = oiwm->metadata_mask;
6674 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
6675 const struct ofp11_instruction_goto_table *oigt;
6676 struct ofpact_goto_table *ogt;
6678 oigt = instruction_get_OFPIT11_GOTO_TABLE(
6679 insts[OVSINST_OFPIT11_GOTO_TABLE]);
6680 ogt = ofpact_put_GOTO_TABLE(ofpacts);
6681 ogt->table_id = oigt->table_id;
6684 error = ofpacts_verify(ofpacts->data, ofpacts->size,
6685 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
6688 ofpbuf_clear(ofpacts);
6693 /* Update the length of the instruction that begins at offset 'ofs' within
6694 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
6695 * If the instruction contains no nested actions, deletes it entirely. */
6697 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
6699 struct ofp11_instruction_actions *oia;
6701 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
6702 if (openflow->size > ofs + sizeof *oia) {
6703 oia->len = htons(openflow->size - ofs);
6705 openflow->size = ofs;
6709 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
6710 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
6711 * valid, otherwise an OpenFlow error code. */
6713 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
6721 case OFPP_CONTROLLER:
6726 return OFPERR_OFPBAC_BAD_OUT_PORT;
6729 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
6732 return OFPERR_OFPBAC_BAD_OUT_PORT;
6736 /* Removes the protocols that require consistency between match and actions
6737 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
6739 * (An example of an inconsistency between match and actions is a flow that
6740 * does not match on an MPLS Ethertype but has an action that pops an MPLS
6743 inconsistent_match(enum ofputil_protocol *usable_protocols)
6745 *usable_protocols &= OFPUTIL_P_OF10_ANY;
6748 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
6749 * caller must restore them.
6751 * Modifies some actions, filling in fields that could not be properly set
6752 * without context. */
6754 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
6755 struct flow *flow, ofp_port_t max_ports,
6756 uint8_t table_id, uint8_t n_tables)
6758 const struct ofpact_enqueue *enqueue;
6759 const struct mf_field *mf;
6763 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
6766 case OFPACT_CONTROLLER:
6769 case OFPACT_ENQUEUE:
6770 enqueue = ofpact_get_ENQUEUE(a);
6771 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
6772 && enqueue->port != OFPP_IN_PORT
6773 && enqueue->port != OFPP_LOCAL) {
6774 return OFPERR_OFPBAC_BAD_OUT_PORT;
6778 case OFPACT_OUTPUT_REG:
6779 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
6781 case OFPACT_OUTPUT_TRUNC:
6782 return ofpact_check_output_port(ofpact_get_OUTPUT_TRUNC(a)->port,
6786 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
6788 case OFPACT_SET_VLAN_VID:
6789 /* Remember if we saw a vlan tag in the flow to aid translating to
6790 * OpenFlow 1.1+ if need be. */
6791 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
6792 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6793 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6794 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
6795 inconsistent_match(usable_protocols);
6797 /* Temporary mark that we have a vlan tag. */
6798 flow->vlan_tci |= htons(VLAN_CFI);
6801 case OFPACT_SET_VLAN_PCP:
6802 /* Remember if we saw a vlan tag in the flow to aid translating to
6803 * OpenFlow 1.1+ if need be. */
6804 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
6805 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6806 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6807 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
6808 inconsistent_match(usable_protocols);
6810 /* Temporary mark that we have a vlan tag. */
6811 flow->vlan_tci |= htons(VLAN_CFI);
6814 case OFPACT_STRIP_VLAN:
6815 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
6816 inconsistent_match(usable_protocols);
6818 /* Temporary mark that we have no vlan tag. */
6819 flow->vlan_tci = htons(0);
6822 case OFPACT_PUSH_VLAN:
6823 if (flow->vlan_tci & htons(VLAN_CFI)) {
6824 /* Multiple VLAN headers not supported. */
6825 return OFPERR_OFPBAC_BAD_TAG;
6827 /* Temporary mark that we have a vlan tag. */
6828 flow->vlan_tci |= htons(VLAN_CFI);
6831 case OFPACT_SET_ETH_SRC:
6832 case OFPACT_SET_ETH_DST:
6835 case OFPACT_SET_IPV4_SRC:
6836 case OFPACT_SET_IPV4_DST:
6837 if (flow->dl_type != htons(ETH_TYPE_IP)) {
6838 inconsistent_match(usable_protocols);
6842 case OFPACT_SET_IP_DSCP:
6843 case OFPACT_SET_IP_ECN:
6844 case OFPACT_SET_IP_TTL:
6845 case OFPACT_DEC_TTL:
6846 if (!is_ip_any(flow)) {
6847 inconsistent_match(usable_protocols);
6851 case OFPACT_SET_L4_SRC_PORT:
6852 case OFPACT_SET_L4_DST_PORT:
6853 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
6854 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
6855 && flow->nw_proto != IPPROTO_SCTP)) {
6856 inconsistent_match(usable_protocols);
6858 /* Note on which transport protocol the port numbers are set.
6859 * This allows this set action to be converted to an OF1.2 set field
6861 if (a->type == OFPACT_SET_L4_SRC_PORT) {
6862 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
6864 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
6868 case OFPACT_REG_MOVE:
6869 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
6871 case OFPACT_SET_FIELD:
6872 mf = ofpact_get_SET_FIELD(a)->field;
6873 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6874 if (!mf_are_prereqs_ok(mf, flow) ||
6875 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
6876 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
6878 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6880 /* Remember if we saw a vlan tag in the flow to aid translating to
6881 * OpenFlow 1.1 if need be. */
6882 ofpact_get_SET_FIELD(a)->flow_has_vlan =
6883 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6884 if (mf->id == MFF_VLAN_TCI) {
6885 /* The set field may add or remove the vlan tag,
6886 * Mark the status temporarily. */
6887 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
6891 case OFPACT_STACK_PUSH:
6892 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
6894 case OFPACT_STACK_POP:
6895 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6897 case OFPACT_SET_MPLS_LABEL:
6898 case OFPACT_SET_MPLS_TC:
6899 case OFPACT_SET_MPLS_TTL:
6900 case OFPACT_DEC_MPLS_TTL:
6901 if (!eth_type_mpls(flow->dl_type)) {
6902 inconsistent_match(usable_protocols);
6906 case OFPACT_SET_TUNNEL:
6907 case OFPACT_SET_QUEUE:
6908 case OFPACT_POP_QUEUE:
6909 case OFPACT_RESUBMIT:
6912 case OFPACT_FIN_TIMEOUT:
6913 if (flow->nw_proto != IPPROTO_TCP) {
6914 inconsistent_match(usable_protocols);
6919 return learn_check(ofpact_get_LEARN(a), flow);
6921 case OFPACT_CONJUNCTION:
6924 case OFPACT_MULTIPATH:
6925 return multipath_check(ofpact_get_MULTIPATH(a), flow);
6931 case OFPACT_PUSH_MPLS:
6932 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6933 /* The packet is now MPLS and the MPLS payload is opaque.
6934 * Thus nothing can be assumed about the network protocol.
6935 * Temporarily mark that we have no nw_proto. */
6939 case OFPACT_POP_MPLS:
6940 if (!eth_type_mpls(flow->dl_type)) {
6941 inconsistent_match(usable_protocols);
6943 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6950 struct ofpact_conntrack *oc = ofpact_get_CT(a);
6953 if (!dl_type_is_ip_any(flow->dl_type)
6954 || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6955 inconsistent_match(usable_protocols);
6958 if (oc->zone_src.field) {
6959 return mf_check_src(&oc->zone_src, flow);
6962 err = ofpacts_check(oc->actions, ofpact_ct_get_action_len(oc),
6963 flow, max_ports, table_id, n_tables,
6969 struct ofpact_nat *on = ofpact_get_NAT(a);
6971 if (!dl_type_is_ip_any(flow->dl_type) ||
6972 (on->range_af == AF_INET && flow->dl_type != htons(ETH_TYPE_IP)) ||
6973 (on->range_af == AF_INET6
6974 && flow->dl_type != htons(ETH_TYPE_IPV6))) {
6975 inconsistent_match(usable_protocols);
6980 case OFPACT_CLEAR_ACTIONS:
6983 case OFPACT_WRITE_ACTIONS: {
6984 /* Use a temporary copy of 'usable_protocols' because we can't check
6985 * consistency of an action set. */
6986 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6987 enum ofputil_protocol p = *usable_protocols;
6988 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6989 flow, max_ports, table_id, n_tables, &p);
6992 case OFPACT_WRITE_METADATA:
6995 case OFPACT_METER: {
6996 uint32_t mid = ofpact_get_METER(a)->meter_id;
6997 if (mid == 0 || mid > OFPM13_MAX) {
6998 return OFPERR_OFPMMFC_INVALID_METER;
7003 case OFPACT_GOTO_TABLE: {
7004 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
7005 if ((table_id != 255 && goto_table <= table_id)
7006 || (n_tables != 255 && goto_table >= n_tables)) {
7007 return OFPERR_OFPBIC_BAD_TABLE_ID;
7015 case OFPACT_UNROLL_XLATE:
7016 /* UNROLL is an internal action that should never be seen via
7018 return OFPERR_OFPBAC_BAD_TYPE;
7020 case OFPACT_DEBUG_RECIRC:
7028 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
7029 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
7030 * switch with no more than 'max_ports' ports.
7032 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
7033 * '*usable_protocols' the protocols that forbid the inconsistency. (An
7034 * example of an inconsistency between match and actions is a flow that does
7035 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
7037 * May annotate ofpacts with information gathered from the 'flow'.
7039 * May temporarily modify 'flow', but restores the changes before returning. */
7041 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
7042 struct flow *flow, ofp_port_t max_ports,
7043 uint8_t table_id, uint8_t n_tables,
7044 enum ofputil_protocol *usable_protocols)
7047 ovs_be16 dl_type = flow->dl_type;
7048 ovs_be16 vlan_tci = flow->vlan_tci;
7049 uint8_t nw_proto = flow->nw_proto;
7050 enum ofperr error = 0;
7052 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7053 error = ofpact_check__(usable_protocols, a, flow,
7054 max_ports, table_id, n_tables);
7059 /* Restore fields that may have been modified. */
7060 flow->dl_type = dl_type;
7061 flow->vlan_tci = vlan_tci;
7062 flow->nw_proto = nw_proto;
7066 /* Like ofpacts_check(), but reports inconsistencies as
7067 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
7069 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
7070 struct flow *flow, ofp_port_t max_ports,
7071 uint8_t table_id, uint8_t n_tables,
7072 enum ofputil_protocol usable_protocols)
7074 enum ofputil_protocol p = usable_protocols;
7077 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
7078 table_id, n_tables, &p);
7079 return (error ? error
7080 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
7084 /* Returns the destination field that 'ofpact' would write to, or NULL
7085 * if the action would not write to an mf_field. */
7086 const struct mf_field *
7087 ofpact_get_mf_dst(const struct ofpact *ofpact)
7089 if (ofpact->type == OFPACT_SET_FIELD) {
7090 const struct ofpact_set_field *orl;
7092 orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
7094 } else if (ofpact->type == OFPACT_REG_MOVE) {
7095 const struct ofpact_reg_move *orm;
7097 orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
7098 return orm->dst.field;
7105 unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action)
7107 VLOG_WARN("%s action doesn't support nested action %s",
7108 ofpact_name(outer_action), ofpact_name(action));
7109 return OFPERR_OFPBAC_BAD_ARGUMENT;
7113 field_requires_ct(enum mf_field_id field)
7115 return field == MFF_CT_MARK || field == MFF_CT_LABEL;
7118 /* Apply nesting constraints for actions */
7120 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
7122 const struct mf_field *field = ofpact_get_mf_dst(a);
7124 if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
7125 VLOG_WARN("cannot set CT fields outside of ct action");
7126 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
7128 if (a->type == OFPACT_NAT) {
7129 if (outer_action != OFPACT_CT) {
7130 VLOG_WARN("Cannot have NAT action outside of \"ct\" action");
7131 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
7137 ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
7138 || outer_action == OFPACT_CT);
7140 if (outer_action == OFPACT_CT) {
7142 return unsupported_nesting(a->type, outer_action);
7143 } else if (!field_requires_ct(field->id)) {
7144 VLOG_WARN("%s action doesn't support nested modification "
7145 "of %s", ofpact_name(outer_action), field->name);
7146 return OFPERR_OFPBAC_BAD_ARGUMENT;
7154 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
7155 * appropriate order as defined by the OpenFlow spec and as required by Open
7158 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
7159 * instructions that are allowed within 'ofpacts[]'.
7161 * If 'outer_action' is not zero, it specifies that the actions are nested
7162 * within another action of type 'outer_action'. */
7164 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
7165 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
7167 const struct ofpact *a;
7168 enum ovs_instruction_type inst;
7170 inst = OVSINST_OFPIT13_METER;
7171 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7172 enum ovs_instruction_type next;
7175 if (a->type == OFPACT_CONJUNCTION) {
7176 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7177 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
7178 VLOG_WARN("\"conjunction\" actions may be used along with "
7179 "\"note\" but not any other kind of action "
7180 "(such as the \"%s\" action used here)",
7181 ofpact_name(a->type));
7182 return OFPERR_NXBAC_BAD_CONJUNCTION;
7188 error = ofpacts_verify_nested(a, outer_action);
7193 next = ovs_instruction_type_from_ofpact_type(a->type);
7195 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
7198 const char *name = ovs_instruction_name_from_type(inst);
7199 const char *next_name = ovs_instruction_name_from_type(next);
7202 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
7203 "1.1+ compatibility", name);
7205 VLOG_WARN("invalid instruction ordering: %s must appear "
7206 "before %s, for OpenFlow 1.1+ compatibility",
7209 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
7211 if (!((1u << next) & allowed_ovsinsts)) {
7212 const char *name = ovs_instruction_name_from_type(next);
7214 VLOG_WARN("%s instruction not allowed here", name);
7215 return OFPERR_OFPBIC_UNSUP_INST;
7224 /* Converting ofpacts to OpenFlow. */
7227 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
7231 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7232 case OFPACT_##ENUM: \
7233 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
7242 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
7243 * actions in 'openflow', appending the actions to any existing data in
7246 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
7247 struct ofpbuf *openflow,
7248 enum ofp_version ofp_version)
7250 const struct ofpact *a;
7251 size_t start_size = openflow->size;
7253 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7254 encode_ofpact(a, ofp_version, openflow);
7256 return openflow->size - start_size;
7259 static enum ovs_instruction_type
7260 ofpact_is_apply_actions(const struct ofpact *a)
7262 return (ovs_instruction_type_from_ofpact_type(a->type)
7263 == OVSINST_OFPIT11_APPLY_ACTIONS);
7267 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
7269 struct ofpbuf *openflow,
7270 enum ofp_version ofp_version)
7272 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
7273 const struct ofpact *a;
7275 if (ofp_version == OFP10_VERSION) {
7276 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
7283 if (ofpact_is_apply_actions(a)) {
7284 size_t ofs = openflow->size;
7286 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
7288 encode_ofpact(a, ofp_version, openflow);
7290 } while (a < end && ofpact_is_apply_actions(a));
7291 ofpacts_update_instruction_actions(openflow, ofs);
7293 encode_ofpact(a, ofp_version, openflow);
7299 /* Sets of supported actions. */
7301 /* Two-way translation between OVS's internal "OFPACT_*" representation of
7302 * actions and the "OFPAT_*" representation used in some OpenFlow version.
7303 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
7304 * instance is specific to one OpenFlow version.) */
7306 enum ofpact_type ofpact; /* Internal name for action type. */
7307 int ofpat; /* OFPAT_* number from OpenFlow spec. */
7310 static const struct ofpact_map *
7311 get_ofpact_map(enum ofp_version version)
7313 /* OpenFlow 1.0 actions. */
7314 static const struct ofpact_map of10[] = {
7315 { OFPACT_OUTPUT, 0 },
7316 { OFPACT_SET_VLAN_VID, 1 },
7317 { OFPACT_SET_VLAN_PCP, 2 },
7318 { OFPACT_STRIP_VLAN, 3 },
7319 { OFPACT_SET_ETH_SRC, 4 },
7320 { OFPACT_SET_ETH_DST, 5 },
7321 { OFPACT_SET_IPV4_SRC, 6 },
7322 { OFPACT_SET_IPV4_DST, 7 },
7323 { OFPACT_SET_IP_DSCP, 8 },
7324 { OFPACT_SET_L4_SRC_PORT, 9 },
7325 { OFPACT_SET_L4_DST_PORT, 10 },
7326 { OFPACT_ENQUEUE, 11 },
7330 /* OpenFlow 1.1 actions. */
7331 static const struct ofpact_map of11[] = {
7332 { OFPACT_OUTPUT, 0 },
7333 { OFPACT_SET_VLAN_VID, 1 },
7334 { OFPACT_SET_VLAN_PCP, 2 },
7335 { OFPACT_SET_ETH_SRC, 3 },
7336 { OFPACT_SET_ETH_DST, 4 },
7337 { OFPACT_SET_IPV4_SRC, 5 },
7338 { OFPACT_SET_IPV4_DST, 6 },
7339 { OFPACT_SET_IP_DSCP, 7 },
7340 { OFPACT_SET_IP_ECN, 8 },
7341 { OFPACT_SET_L4_SRC_PORT, 9 },
7342 { OFPACT_SET_L4_DST_PORT, 10 },
7343 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7344 /* OFPAT_COPY_TTL_IN (12) not supported. */
7345 { OFPACT_SET_MPLS_LABEL, 13 },
7346 { OFPACT_SET_MPLS_TC, 14 },
7347 { OFPACT_SET_MPLS_TTL, 15 },
7348 { OFPACT_DEC_MPLS_TTL, 16 },
7349 { OFPACT_PUSH_VLAN, 17 },
7350 { OFPACT_STRIP_VLAN, 18 },
7351 { OFPACT_PUSH_MPLS, 19 },
7352 { OFPACT_POP_MPLS, 20 },
7353 { OFPACT_SET_QUEUE, 21 },
7354 { OFPACT_GROUP, 22 },
7355 { OFPACT_SET_IP_TTL, 23 },
7356 { OFPACT_DEC_TTL, 24 },
7360 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
7361 static const struct ofpact_map of12[] = {
7362 { OFPACT_OUTPUT, 0 },
7363 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7364 /* OFPAT_COPY_TTL_IN (12) not supported. */
7365 { OFPACT_SET_MPLS_TTL, 15 },
7366 { OFPACT_DEC_MPLS_TTL, 16 },
7367 { OFPACT_PUSH_VLAN, 17 },
7368 { OFPACT_STRIP_VLAN, 18 },
7369 { OFPACT_PUSH_MPLS, 19 },
7370 { OFPACT_POP_MPLS, 20 },
7371 { OFPACT_SET_QUEUE, 21 },
7372 { OFPACT_GROUP, 22 },
7373 { OFPACT_SET_IP_TTL, 23 },
7374 { OFPACT_DEC_TTL, 24 },
7375 { OFPACT_SET_FIELD, 25 },
7376 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
7377 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
7398 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
7399 * values, into a bitmap of actions suitable for OpenFlow 'version', and
7400 * returns the result. */
7402 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
7404 uint32_t openflow_bitmap = 0;
7405 const struct ofpact_map *x;
7407 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7408 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
7409 openflow_bitmap |= 1u << x->ofpat;
7412 return htonl(openflow_bitmap);
7415 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
7416 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
7417 * and returns the result. */
7419 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
7421 uint64_t ofpact_bitmap = 0;
7422 const struct ofpact_map *x;
7424 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7425 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
7426 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
7429 return ofpact_bitmap;
7432 /* Appends to 's' a string representation of the set of OFPACT_* represented
7433 * by 'ofpacts_bitmap'. */
7435 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
7437 if (!ofpacts_bitmap) {
7438 ds_put_cstr(s, "<none>");
7440 while (ofpacts_bitmap) {
7441 ds_put_format(s, "%s ",
7442 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
7443 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
7449 /* Returns true if 'action' outputs to 'port', false otherwise. */
7451 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
7453 switch (ofpact->type) {
7455 return ofpact_get_OUTPUT(ofpact)->port == port;
7456 case OFPACT_ENQUEUE:
7457 return ofpact_get_ENQUEUE(ofpact)->port == port;
7458 case OFPACT_CONTROLLER:
7459 return port == OFPP_CONTROLLER;
7461 case OFPACT_OUTPUT_REG:
7462 case OFPACT_OUTPUT_TRUNC:
7464 case OFPACT_SET_VLAN_VID:
7465 case OFPACT_SET_VLAN_PCP:
7466 case OFPACT_STRIP_VLAN:
7467 case OFPACT_PUSH_VLAN:
7468 case OFPACT_SET_ETH_SRC:
7469 case OFPACT_SET_ETH_DST:
7470 case OFPACT_SET_IPV4_SRC:
7471 case OFPACT_SET_IPV4_DST:
7472 case OFPACT_SET_IP_DSCP:
7473 case OFPACT_SET_IP_ECN:
7474 case OFPACT_SET_IP_TTL:
7475 case OFPACT_SET_L4_SRC_PORT:
7476 case OFPACT_SET_L4_DST_PORT:
7477 case OFPACT_REG_MOVE:
7478 case OFPACT_SET_FIELD:
7479 case OFPACT_STACK_PUSH:
7480 case OFPACT_STACK_POP:
7481 case OFPACT_DEC_TTL:
7482 case OFPACT_SET_MPLS_LABEL:
7483 case OFPACT_SET_MPLS_TC:
7484 case OFPACT_SET_MPLS_TTL:
7485 case OFPACT_DEC_MPLS_TTL:
7486 case OFPACT_SET_TUNNEL:
7487 case OFPACT_WRITE_METADATA:
7488 case OFPACT_SET_QUEUE:
7489 case OFPACT_POP_QUEUE:
7490 case OFPACT_FIN_TIMEOUT:
7491 case OFPACT_RESUBMIT:
7493 case OFPACT_CONJUNCTION:
7494 case OFPACT_MULTIPATH:
7497 case OFPACT_UNROLL_XLATE:
7498 case OFPACT_PUSH_MPLS:
7499 case OFPACT_POP_MPLS:
7501 case OFPACT_CLEAR_ACTIONS:
7502 case OFPACT_WRITE_ACTIONS:
7503 case OFPACT_GOTO_TABLE:
7506 case OFPACT_DEBUG_RECIRC:
7514 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7515 * to 'port', false otherwise. */
7517 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
7520 const struct ofpact *a;
7522 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
7523 if (ofpact_outputs_to_port(a, port)) {
7531 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7532 * to 'group', false otherwise. */
7534 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
7537 const struct ofpact *a;
7539 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
7540 if (a->type == OFPACT_GROUP
7541 && ofpact_get_GROUP(a)->group_id == group_id) {
7550 ofpacts_equal(const struct ofpact *a, size_t a_len,
7551 const struct ofpact *b, size_t b_len)
7553 return a_len == b_len && !memcmp(a, b, a_len);
7556 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
7557 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
7559 * This function relies on the order of 'ofpacts' being correct (as checked by
7560 * ofpacts_verify()). */
7562 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
7564 const struct ofpact *a;
7566 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7567 enum ovs_instruction_type inst;
7569 inst = ovs_instruction_type_from_ofpact_type(a->type);
7570 if (a->type == OFPACT_METER) {
7571 return ofpact_get_METER(a)->meter_id;
7572 } else if (inst > OVSINST_OFPIT13_METER) {
7580 /* Formatting ofpacts. */
7583 ofpact_format(const struct ofpact *a, struct ds *s)
7586 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7587 case OFPACT_##ENUM: \
7588 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
7597 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
7598 * 'ofpacts' to 'string'. */
7600 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
7604 ds_put_format(string, "%sdrop%s", colors.drop, colors.end);
7606 const struct ofpact *a;
7608 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7610 ds_put_char(string, ',');
7613 /* XXX write-actions */
7614 ofpact_format(a, string);
7619 /* Internal use by helpers. */
7621 /* Implementation of ofpact_put_<ENUM>(). */
7623 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
7625 struct ofpact *ofpact;
7627 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
7628 ofpact = ofpacts->header;
7629 ofpact_init(ofpact, type, len);
7633 /* Implementation of ofpact_init_<ENUM>(). */
7635 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
7637 memset(ofpact, 0, len);
7638 ofpact->type = type;
7643 /* Implementation of ofpact_finish_<ENUM>().
7645 * Finishes composing a variable-length action (begun using
7646 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
7647 * bytes and updating its embedded length field. See the large comment near
7648 * the end of ofp-actions.h for more information.
7650 * May reallocate 'ofpacts'. Callers should consider updating their 'ofpact'
7651 * pointer to the return value of this function. */
7653 ofpact_finish(struct ofpbuf *ofpacts, struct ofpact *ofpact)
7657 ovs_assert(ofpact == ofpacts->header);
7658 len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
7659 ovs_assert(len > 0 && len <= UINT16_MAX);
7661 ofpbuf_padto(ofpacts, OFPACT_ALIGN(ofpacts->size));
7663 return ofpacts->header;
7666 static char * OVS_WARN_UNUSED_RESULT
7667 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
7668 enum ofputil_protocol *usable_protocols)
7671 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7672 case OFPACT_##ENUM: \
7673 return parse_##ENUM(value, ofpacts, usable_protocols);
7682 ofpact_type_from_name(const char *name, enum ofpact_type *type)
7684 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7685 if (!strcasecmp(name, NAME)) { \
7686 *type = OFPACT_##ENUM; \
7695 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
7697 * Returns NULL if successful, otherwise a malloc()'d string describing the
7698 * error. The caller is responsible for freeing the returned string.
7700 * If 'outer_action' is specified, indicates that the actions being parsed
7701 * are nested within another action of the type specified in 'outer_action'. */
7702 static char * OVS_WARN_UNUSED_RESULT
7703 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
7704 enum ofputil_protocol *usable_protocols,
7705 bool allow_instructions, enum ofpact_type outer_action)
7714 while (ofputil_parse_key_value(&pos, &key, &value)) {
7715 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
7716 enum ofpact_type type;
7720 if (ofpact_type_from_name(key, &type)) {
7721 error = ofpact_parse(type, value, ofpacts, usable_protocols);
7722 inst = ovs_instruction_type_from_ofpact_type(type);
7723 } else if (!strcasecmp(key, "mod_vlan_vid")) {
7724 error = parse_set_vlan_vid(value, ofpacts, true);
7725 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
7726 error = parse_set_vlan_pcp(value, ofpacts, true);
7727 } else if (!strcasecmp(key, "set_nw_ttl")) {
7728 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
7729 } else if (!strcasecmp(key, "pop_vlan")) {
7730 error = parse_pop_vlan(ofpacts);
7731 } else if (!strcasecmp(key, "set_tunnel64")) {
7732 error = parse_set_tunnel(value, ofpacts,
7733 NXAST_RAW_SET_TUNNEL64);
7734 } else if (!strcasecmp(key, "load")) {
7735 error = parse_reg_load(value, ofpacts);
7736 } else if (!strcasecmp(key, "bundle_load")) {
7737 error = parse_bundle_load(value, ofpacts);
7738 } else if (!strcasecmp(key, "drop")) {
7740 } else if (!strcasecmp(key, "apply_actions")) {
7741 return xstrdup("apply_actions is the default instruction");
7742 } else if (ofputil_port_from_string(key, &port)) {
7743 ofpact_put_OUTPUT(ofpacts)->port = port;
7745 return xasprintf("unknown action %s", key);
7751 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
7752 if (!allow_instructions) {
7753 return xasprintf("only actions are allowed here (not "
7755 ovs_instruction_name_from_type(inst));
7757 if (inst == prev_inst) {
7758 return xasprintf("instruction %s may be specified only once",
7759 ovs_instruction_name_from_type(inst));
7762 if (prev_inst != -1 && inst < prev_inst) {
7763 return xasprintf("instruction %s must be specified before %s",
7764 ovs_instruction_name_from_type(inst),
7765 ovs_instruction_name_from_type(prev_inst));
7770 if (drop && ofpacts->size) {
7771 return xstrdup("\"drop\" must not be accompanied by any other action "
7775 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
7777 ? (1u << N_OVS_INSTRUCTIONS) - 1
7778 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
7781 return xstrdup("Incorrect instruction ordering");
7787 static char * OVS_WARN_UNUSED_RESULT
7788 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
7789 enum ofputil_protocol *usable_protocols, bool allow_instructions,
7790 enum ofpact_type outer_action)
7792 uint32_t orig_size = ofpacts->size;
7793 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
7794 allow_instructions, outer_action);
7796 ofpacts->size = orig_size;
7801 static char * OVS_WARN_UNUSED_RESULT
7802 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
7803 enum ofputil_protocol *usable_protocols,
7804 bool allow_instructions, enum ofpact_type outer_action)
7808 *usable_protocols = OFPUTIL_P_ANY;
7811 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
7818 /* Parses 's' as a set of OpenFlow actions and appends the actions to
7819 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
7820 * that are nested within the action of type 'outer_action'.
7822 * Returns NULL if successful, otherwise a malloc()'d string describing the
7823 * error. The caller is responsible for freeing the returned string. */
7824 char * OVS_WARN_UNUSED_RESULT
7825 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
7826 enum ofputil_protocol *usable_protocols)
7828 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
7831 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
7834 * Returns NULL if successful, otherwise a malloc()'d string describing the
7835 * error. The caller is responsible for freeing the returned string. */
7836 char * OVS_WARN_UNUSED_RESULT
7837 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
7838 enum ofputil_protocol *usable_protocols)
7840 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
7844 ofpact_name(enum ofpact_type type)
7847 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7854 /* Low-level action decoding and encoding functions. */
7856 /* Everything needed to identify a particular OpenFlow action. */
7857 struct ofpact_hdrs {
7858 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
7859 uint16_t type; /* Type if standard, otherwise subtype. */
7860 uint8_t ofp_version; /* From ofp_header. */
7863 /* Information about a particular OpenFlow action. */
7864 struct ofpact_raw_instance {
7865 /* The action's identity. */
7866 struct ofpact_hdrs hdrs;
7867 enum ofp_raw_action_type raw;
7869 /* Looking up the action. */
7870 struct hmap_node decode_node; /* Based on 'hdrs'. */
7871 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
7873 /* The action's encoded size.
7875 * If this action is fixed-length, 'min_length' == 'max_length'.
7876 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7877 * OFP_ACTION_ALIGN) == 65528. */
7878 unsigned short int min_length;
7879 unsigned short int max_length;
7881 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7882 * offset of that argument from the beginning of the action and 'arg_len'
7883 * its length, both in bytes.
7885 * For actions that take other forms, these are both zero. */
7886 unsigned short int arg_ofs;
7887 unsigned short int arg_len;
7889 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7892 /* If this action is deprecated, a human-readable string with a brief
7894 const char *deprecation;
7897 /* Action header. */
7898 struct ofp_action_header {
7899 /* The meaning of other values of 'type' generally depends on the OpenFlow
7900 * version (see enum ofp_raw_action_type).
7902 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7903 * designates an OpenFlow vendor ID and that the remainder of the action
7904 * structure has a vendor-defined meaning.
7906 #define OFPAT_VENDOR 0xffff
7909 /* Always a multiple of 8. */
7912 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7913 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
7916 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
7918 /* Header for Nicira-defined actions and for ONF vendor extensions.
7920 * This cannot be used as an entirely generic vendor extension action header,
7921 * because OpenFlow does not specify the location or size of the action
7922 * subtype; it just happens that ONF extensions and Nicira extensions share
7924 struct ext_action_header {
7925 ovs_be16 type; /* OFPAT_VENDOR. */
7926 ovs_be16 len; /* At least 16. */
7927 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
7928 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
7931 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
7934 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
7935 const struct ofpact_hdrs *b)
7937 return (a->vendor == b->vendor
7938 && a->type == b->type
7939 && a->ofp_version == b->ofp_version);
7943 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
7945 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
7948 #include "ofp-actions.inc2"
7950 static struct hmap *
7951 ofpact_decode_hmap(void)
7953 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7954 static struct hmap hmap;
7956 if (ovsthread_once_start(&once)) {
7957 struct ofpact_raw_instance *inst;
7960 for (inst = all_raw_instances;
7961 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7963 hmap_insert(&hmap, &inst->decode_node,
7964 ofpact_hdrs_hash(&inst->hdrs));
7966 ovsthread_once_done(&once);
7971 static struct hmap *
7972 ofpact_encode_hmap(void)
7974 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7975 static struct hmap hmap;
7977 if (ovsthread_once_start(&once)) {
7978 struct ofpact_raw_instance *inst;
7981 for (inst = all_raw_instances;
7982 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7984 hmap_insert(&hmap, &inst->encode_node,
7985 hash_2words(inst->raw, inst->hdrs.ofp_version));
7987 ovsthread_once_done(&once);
7993 ofpact_decode_raw(enum ofp_version ofp_version,
7994 const struct ofp_action_header *oah, size_t length,
7995 const struct ofpact_raw_instance **instp)
7997 const struct ofpact_raw_instance *inst;
7998 struct ofpact_hdrs hdrs;
8001 if (length < sizeof *oah) {
8002 return OFPERR_OFPBAC_BAD_LEN;
8005 /* Get base action type. */
8006 if (oah->type == htons(OFPAT_VENDOR)) {
8008 hdrs.vendor = ntohl(oah->vendor);
8009 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
8010 /* Get extension subtype. */
8011 const struct ext_action_header *nah;
8013 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
8014 if (length < sizeof *nah) {
8015 return OFPERR_OFPBAC_BAD_LEN;
8017 hdrs.type = ntohs(nah->subtype);
8019 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
8021 return OFPERR_OFPBAC_BAD_VENDOR;
8025 hdrs.type = ntohs(oah->type);
8028 hdrs.ofp_version = ofp_version;
8029 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
8030 ofpact_decode_hmap()) {
8031 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
8038 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
8039 : OFPERR_OFPBAC_BAD_TYPE);
8043 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
8044 enum ofp_raw_action_type *raw, uint64_t *arg)
8046 const struct ofp_action_header *oah = buf->data;
8047 const struct ofpact_raw_instance *action;
8048 unsigned int length;
8052 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
8057 if (action->deprecation) {
8058 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
8059 action->name, ofputil_version_to_string(ofp_version),
8060 action->deprecation);
8063 length = ntohs(oah->len);
8064 if (length > buf->size) {
8065 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
8066 "length %"PRIu32, action->name, length, buf->size);
8067 return OFPERR_OFPBAC_BAD_LEN;
8069 if (length < action->min_length || length > action->max_length) {
8070 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
8071 "[%hu,%hu]", action->name, length,
8072 action->min_length, action->max_length);
8073 return OFPERR_OFPBAC_BAD_LEN;
8076 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
8077 "of 8", action->name, length);
8078 return OFPERR_OFPBAC_BAD_LEN;
8083 if (action->arg_len) {
8087 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
8088 for (i = 0; i < action->arg_len; i++) {
8089 *arg = (*arg << 8) | p[i];
8093 ofpbuf_pull(buf, length);
8098 static const struct ofpact_raw_instance *
8099 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
8101 const struct ofpact_raw_instance *inst;
8103 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
8104 ofpact_encode_hmap()) {
8105 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
8113 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
8114 enum ofp_raw_action_type raw, uint64_t arg)
8116 const struct ofpact_raw_instance *inst;
8117 struct ofp_action_header *oah;
8118 const struct ofpact_hdrs *hdrs;
8120 inst = ofpact_raw_lookup(ofp_version, raw);
8123 oah = ofpbuf_put_zeros(buf, inst->min_length);
8124 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
8125 oah->len = htons(inst->min_length);
8126 oah->vendor = htonl(hdrs->vendor);
8128 switch (hdrs->vendor) {
8133 case ONF_VENDOR_ID: {
8134 struct ext_action_header *nah = (struct ext_action_header *) oah;
8135 nah->subtype = htons(hdrs->type);
8143 if (inst->arg_len) {
8144 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
8147 for (i = 0; i < inst->arg_len; i++) {
8159 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
8161 struct ofp_action_header *oah;
8163 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs,
8166 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
8167 oah->len = htons(openflow->size - start_ofs);