2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 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>
20 #include "ofp-actions.h"
22 #include "byte-order.h"
25 #include "dynamic-string.h"
28 #include "meta-flow.h"
29 #include "multipath.h"
31 #include "odp-netlink.h"
32 #include "ofp-parse.h"
36 #include "unaligned.h"
38 #include "openvswitch/vlog.h"
40 VLOG_DEFINE_THIS_MODULE(ofp_actions);
42 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
44 struct ofp_action_header;
46 /* Raw identifiers for OpenFlow actions.
48 * Decoding and encoding OpenFlow actions across multiple versions is difficult
49 * to do in a clean, consistent way. This enumeration lays out all of the
50 * forms of actions that Open vSwitch supports.
52 * The comments here must follow a stylized form because the
53 * "extract-ofp-actions" program parses them at build time to generate data
56 * - The first part of each comment specifies the vendor, OpenFlow versions,
57 * and type for each protocol that supports the action:
59 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
60 * extension actions. (Support for other vendors can be added, but
61 * it can't be done just based on a vendor ID definition alone
62 * because OpenFlow doesn't define a standard way to specify a
63 * subtype for vendor actions, so other vendors might do it different
66 * # The version can specify a specific OpenFlow version, a version
67 * range delimited by "-", or an open-ended range with "+".
69 * # The type, in parentheses, is the action type number (for standard
70 * OpenFlow actions) or subtype (for vendor extension actions).
72 * # Optionally one may add "is deprecated" followed by a
73 * human-readable reason in parentheses (which will be used in log
74 * messages), if a particular action should no longer be used.
76 * Multiple such specifications may be separated by commas.
78 * - The second part describes the action's wire format. It may be:
80 * # "struct <name>": The struct fully specifies the wire format. The
81 * action is exactly the size of the struct. (Thus, the struct must
82 * be an exact multiple of 8 bytes in size.)
84 * # "struct <name>, ...": The struct specifies the beginning of the
85 * wire format. An instance of the action is either the struct's
86 * exact size, or a multiple of 8 bytes longer.
88 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
89 * vendor extension) header, followed by 0 or more pad bytes to align
90 * to a multiple of <N> bits, followed by an argument of the given
91 * type, followed by 0 or more pad bytes to bring the total action up
92 * to a multiple of 8 bytes.
94 * # "void": The action is just a (standard or vendor extension)
97 * - Optional additional text enclosed in square brackets is commentary for
100 enum ofp_raw_action_type {
101 /* ## ----------------- ## */
102 /* ## Standard actions. ## */
103 /* ## ----------------- ## */
105 /* OF1.0(0): struct ofp10_action_output. */
107 /* OF1.1+(0): struct ofp11_action_output. */
110 /* OF1.0(1): uint16_t. */
111 OFPAT_RAW10_SET_VLAN_VID,
112 /* OF1.0(2): uint8_t. */
113 OFPAT_RAW10_SET_VLAN_PCP,
115 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
117 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
118 * modification actions: the 1.0 versions push a VLAN header if none is
119 * present, but the 1.1 versions do not. That is the only reason that we
120 * distinguish their raw action types.] */
121 OFPAT_RAW11_SET_VLAN_VID,
122 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
123 OFPAT_RAW11_SET_VLAN_PCP,
125 /* OF1.1+(17): ovs_be16.
127 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
129 OFPAT_RAW11_PUSH_VLAN,
131 /* OF1.0(3): void. */
132 OFPAT_RAW10_STRIP_VLAN,
133 /* OF1.1+(18): void. */
134 OFPAT_RAW11_POP_VLAN,
136 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
137 * ofp_action_dl_addr. */
138 OFPAT_RAW_SET_DL_SRC,
140 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
141 * ofp_action_dl_addr. */
142 OFPAT_RAW_SET_DL_DST,
144 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
146 OFPAT_RAW_SET_NW_SRC,
148 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
150 OFPAT_RAW_SET_NW_DST,
152 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
153 OFPAT_RAW_SET_NW_TOS,
155 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
156 OFPAT_RAW11_SET_NW_ECN,
158 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
160 OFPAT_RAW_SET_TP_SRC,
162 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
164 OFPAT_RAW_SET_TP_DST,
166 /* OF1.0(11): struct ofp10_action_enqueue. */
169 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
171 OFPAT_RAW_SET_MPLS_LABEL,
173 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
175 OFPAT_RAW_SET_MPLS_TC,
177 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
179 OFPAT_RAW_SET_MPLS_TTL,
181 /* NX1.0(26), OF1.1+(16): void. */
182 OFPAT_RAW_DEC_MPLS_TTL,
184 /* NX1.0(23), OF1.1+(19): ovs_be16.
186 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
189 /* NX1.0(24), OF1.1+(20): ovs_be16.
191 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
192 * ETH_TYPE_MPLS otherwise, not the label.] */
195 /* NX1.0(4), OF1.1+(21): uint32_t. */
198 /* OF1.1+(22): uint32_t. */
201 /* OF1.1+(23): uint8_t. */
202 OFPAT_RAW11_SET_NW_TTL,
204 /* NX1.0(18), OF1.1+(24): void. */
205 OFPAT_RAW_DEC_NW_TTL,
206 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
207 NXAST_RAW_DEC_TTL_CNT_IDS,
209 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
210 OFPAT_RAW12_SET_FIELD,
211 /* OF1.5+(25): struct ofp12_action_set_field, ... */
212 OFPAT_RAW15_SET_FIELD,
213 /* NX1.0-1.4(7): struct nx_action_reg_load.
215 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
216 * we drop reg_load.] */
218 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
220 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
221 * we drop reg_load2.] */
224 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
225 OFPAT_RAW15_COPY_FIELD,
226 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
227 ONFACT_RAW13_COPY_FIELD,
228 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
231 /* ## ------------------------- ## */
232 /* ## Nicira extension actions. ## */
233 /* ## ------------------------- ## */
235 /* Actions similar to standard actions are listed with the standard actions. */
237 /* NX1.0+(1): uint16_t. */
239 /* NX1.0+(14): struct nx_action_resubmit. */
240 NXAST_RAW_RESUBMIT_TABLE,
242 /* NX1.0+(2): uint32_t. */
243 NXAST_RAW_SET_TUNNEL,
244 /* NX1.0+(9): uint64_t. */
245 NXAST_RAW_SET_TUNNEL64,
247 /* NX1.0+(5): void. */
250 /* NX1.0+(8): struct nx_action_note, ... */
253 /* NX1.0+(10): struct nx_action_multipath. */
256 /* NX1.0+(12): struct nx_action_bundle, ... */
258 /* NX1.0+(13): struct nx_action_bundle, ... */
259 NXAST_RAW_BUNDLE_LOAD,
261 /* NX1.0+(15): struct nx_action_output_reg. */
262 NXAST_RAW_OUTPUT_REG,
263 /* NX1.0+(32): struct nx_action_output_reg2. */
264 NXAST_RAW_OUTPUT_REG2,
266 /* NX1.0+(16): struct nx_action_learn, ... */
269 /* NX1.0+(17): void. */
272 /* NX1.0+(19): struct nx_action_fin_timeout. */
273 NXAST_RAW_FIN_TIMEOUT,
275 /* NX1.0+(20): struct nx_action_controller. */
276 NXAST_RAW_CONTROLLER,
277 /* NX1.0+(37): struct nx_action_controller2, ... */
278 NXAST_RAW_CONTROLLER2,
280 /* NX1.0+(22): struct nx_action_write_metadata. */
281 NXAST_RAW_WRITE_METADATA,
283 /* NX1.0+(27): struct nx_action_stack. */
284 NXAST_RAW_STACK_PUSH,
286 /* NX1.0+(28): struct nx_action_stack. */
289 /* NX1.0+(29): struct nx_action_sample. */
292 /* NX1.0+(34): struct nx_action_conjunction. */
293 NXAST_RAW_CONJUNCTION,
295 /* NX1.0+(35): struct nx_action_conntrack, ... */
298 /* NX1.0+(36): struct nx_action_nat, ... */
301 /* ## ------------------ ## */
302 /* ## Debugging actions. ## */
303 /* ## ------------------ ## */
305 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
306 /* accepts them only if started with --enable-dummy. */
308 /* NX1.0+(255): void. */
309 NXAST_RAW_DEBUG_RECIRC,
312 /* OpenFlow actions are always a multiple of 8 bytes in length. */
313 #define OFP_ACTION_ALIGN 8
315 /* Define a few functions for working with instructions. */
316 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
317 static inline const struct STRUCT * OVS_UNUSED \
318 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
320 ovs_assert(inst->type == htons(ENUM)); \
321 return ALIGNED_CAST(struct STRUCT *, inst); \
324 static inline void OVS_UNUSED \
325 instruction_init_##ENUM(struct STRUCT *s) \
327 memset(s, 0, sizeof *s); \
328 s->type = htons(ENUM); \
329 s->len = htons(sizeof *s); \
332 static inline struct STRUCT * OVS_UNUSED \
333 instruction_put_##ENUM(struct ofpbuf *buf) \
335 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
336 instruction_init_##ENUM(s); \
342 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
344 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
346 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
347 uint32_t allowed_ovsinsts,
348 enum ofpact_type outer_action);
350 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
351 enum mf_field_id, uint64_t value);
353 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
354 enum ofp_raw_action_type *, uint64_t *arg);
355 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
356 enum ofp_raw_action_type, uint64_t arg);
358 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
359 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
360 bool allow_instructions, enum ofpact_type outer_action);
361 static enum ofperr ofpacts_pull_openflow_actions__(
362 struct ofpbuf *openflow, unsigned int actions_len,
363 enum ofp_version version, uint32_t allowed_ovsinsts,
364 struct ofpbuf *ofpacts, enum ofpact_type outer_action);
365 static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
366 const char *s_, struct ofpbuf *ofpacts,
367 enum ofputil_protocol *usable_protocols,
368 bool allow_instructions, enum ofpact_type outer_action);
370 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
371 * nested ofpacts it returns the first one. */
373 ofpact_next_flattened(const struct ofpact *ofpact)
375 switch (ofpact->type) {
378 case OFPACT_CONTROLLER:
380 case OFPACT_OUTPUT_REG:
382 case OFPACT_SET_FIELD:
383 case OFPACT_SET_VLAN_VID:
384 case OFPACT_SET_VLAN_PCP:
385 case OFPACT_STRIP_VLAN:
386 case OFPACT_PUSH_VLAN:
387 case OFPACT_SET_ETH_SRC:
388 case OFPACT_SET_ETH_DST:
389 case OFPACT_SET_IPV4_SRC:
390 case OFPACT_SET_IPV4_DST:
391 case OFPACT_SET_IP_DSCP:
392 case OFPACT_SET_IP_ECN:
393 case OFPACT_SET_IP_TTL:
394 case OFPACT_SET_L4_SRC_PORT:
395 case OFPACT_SET_L4_DST_PORT:
396 case OFPACT_REG_MOVE:
397 case OFPACT_STACK_PUSH:
398 case OFPACT_STACK_POP:
400 case OFPACT_SET_MPLS_LABEL:
401 case OFPACT_SET_MPLS_TC:
402 case OFPACT_SET_MPLS_TTL:
403 case OFPACT_DEC_MPLS_TTL:
404 case OFPACT_PUSH_MPLS:
405 case OFPACT_POP_MPLS:
406 case OFPACT_SET_TUNNEL:
407 case OFPACT_SET_QUEUE:
408 case OFPACT_POP_QUEUE:
409 case OFPACT_FIN_TIMEOUT:
410 case OFPACT_RESUBMIT:
412 case OFPACT_CONJUNCTION:
413 case OFPACT_MULTIPATH:
417 case OFPACT_UNROLL_XLATE:
418 case OFPACT_DEBUG_RECIRC:
420 case OFPACT_CLEAR_ACTIONS:
421 case OFPACT_WRITE_METADATA:
422 case OFPACT_GOTO_TABLE:
424 return ofpact_next(ofpact);
427 return ofpact_get_CT(ofpact)->actions;
429 case OFPACT_WRITE_ACTIONS:
430 return ofpact_get_WRITE_ACTIONS(ofpact)->actions;
436 /* Pull off existing actions or instructions. Used by nesting actions to keep
437 * ofpacts_parse() oblivious of actions nesting.
439 * Push the actions back on after nested parsing, e.g.:
441 * size_t ofs = ofpacts_pull(ofpacts);
442 * ...nested parsing...
443 * ofpbuf_push_uninit(ofpacts, ofs);
446 ofpacts_pull(struct ofpbuf *ofpacts)
451 ofpbuf_pull(ofpacts, ofs);
456 #include "ofp-actions.inc1"
458 /* Output actions. */
460 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
461 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
462 * number of bytes to send. A 'max_len' of zero means no bytes of the
463 * packet should be sent. */
464 struct ofp10_action_output {
465 ovs_be16 type; /* OFPAT10_OUTPUT. */
466 ovs_be16 len; /* Length is 8. */
467 ovs_be16 port; /* Output port. */
468 ovs_be16 max_len; /* Max length to send to controller. */
470 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
472 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
473 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
474 * number of bytes to send. A 'max_len' of zero means no bytes of the
475 * packet should be sent.*/
476 struct ofp11_action_output {
477 ovs_be16 type; /* OFPAT11_OUTPUT. */
478 ovs_be16 len; /* Length is 16. */
479 ovs_be32 port; /* Output port. */
480 ovs_be16 max_len; /* Max length to send to controller. */
481 uint8_t pad[6]; /* Pad to 64 bits. */
483 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
486 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
487 enum ofp_version ofp_version OVS_UNUSED,
490 struct ofpact_output *output;
492 output = ofpact_put_OUTPUT(out);
493 output->port = u16_to_ofp(ntohs(oao->port));
494 output->max_len = ntohs(oao->max_len);
496 return ofpact_check_output_port(output->port, OFPP_MAX);
500 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
501 enum ofp_version ofp_version OVS_UNUSED,
504 struct ofpact_output *output;
507 output = ofpact_put_OUTPUT(out);
508 output->max_len = ntohs(oao->max_len);
510 error = ofputil_port_from_ofp11(oao->port, &output->port);
515 return ofpact_check_output_port(output->port, OFPP_MAX);
519 encode_OUTPUT(const struct ofpact_output *output,
520 enum ofp_version ofp_version, struct ofpbuf *out)
522 if (ofp_version == OFP10_VERSION) {
523 struct ofp10_action_output *oao;
525 oao = put_OFPAT10_OUTPUT(out);
526 oao->port = htons(ofp_to_u16(output->port));
527 oao->max_len = htons(output->max_len);
529 struct ofp11_action_output *oao;
531 oao = put_OFPAT11_OUTPUT(out);
532 oao->port = ofputil_port_to_ofp11(output->port);
533 oao->max_len = htons(output->max_len);
537 static char * OVS_WARN_UNUSED_RESULT
538 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
539 enum ofputil_protocol *usable_protocols OVS_UNUSED)
541 if (strchr(arg, '[')) {
542 struct ofpact_output_reg *output_reg;
544 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
545 output_reg->max_len = UINT16_MAX;
546 return mf_parse_subfield(&output_reg->src, arg);
548 struct ofpact_output *output;
550 output = ofpact_put_OUTPUT(ofpacts);
551 if (!ofputil_port_from_string(arg, &output->port)) {
552 return xasprintf("%s: output to unknown port", arg);
554 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
560 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
562 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
563 ds_put_format(s, "output:%"PRIu16, a->port);
565 ofputil_format_port(a->port, s);
566 if (a->port == OFPP_CONTROLLER) {
567 ds_put_format(s, ":%"PRIu16, a->max_len);
575 decode_OFPAT_RAW11_GROUP(uint32_t group_id,
576 enum ofp_version ofp_version OVS_UNUSED,
579 ofpact_put_GROUP(out)->group_id = group_id;
584 encode_GROUP(const struct ofpact_group *group,
585 enum ofp_version ofp_version, struct ofpbuf *out)
587 if (ofp_version == OFP10_VERSION) {
590 put_OFPAT11_GROUP(out, group->group_id);
594 static char * OVS_WARN_UNUSED_RESULT
595 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
596 enum ofputil_protocol *usable_protocols OVS_UNUSED)
598 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
602 format_GROUP(const struct ofpact_group *a, struct ds *s)
604 ds_put_format(s, "group:%"PRIu32, a->group_id);
607 /* Action structure for NXAST_CONTROLLER.
609 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
610 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
613 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
615 * - 'controller_id': The ID of the controller connection to which the
616 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
617 * sent only to controllers that have the specified controller connection
618 * ID. See "struct nx_controller_id" for more information. */
619 struct nx_action_controller {
620 ovs_be16 type; /* OFPAT_VENDOR. */
621 ovs_be16 len; /* Length is 16. */
622 ovs_be32 vendor; /* NX_VENDOR_ID. */
623 ovs_be16 subtype; /* NXAST_CONTROLLER. */
624 ovs_be16 max_len; /* Maximum length to send to controller. */
625 ovs_be16 controller_id; /* Controller ID to send packet-in. */
626 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
627 uint8_t zero; /* Must be zero. */
629 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
631 /* Properties for NXAST_CONTROLLER2. */
632 enum nx_action_controller2_prop_type {
633 NXAC2PT_MAX_LEN, /* ovs_be16 max bytes to send (default all). */
634 NXAC2PT_CONTROLLER_ID, /* ovs_be16 dest controller ID (default 0). */
635 NXAC2PT_REASON, /* uint8_t reason (OFPR_*), default 0. */
636 NXAC2PT_USERDATA, /* Data to copy into NXPINT_USERDATA. */
639 /* Action structure for NXAST_CONTROLLER2.
641 * This replacement for NXAST_CONTROLLER makes it extensible via properties. */
642 struct nx_action_controller2 {
643 ovs_be16 type; /* OFPAT_VENDOR. */
644 ovs_be16 len; /* Length is 16 or more. */
645 ovs_be32 vendor; /* NX_VENDOR_ID. */
646 ovs_be16 subtype; /* NXAST_CONTROLLER2. */
647 uint8_t zeros[6]; /* Must be zero. */
648 /* Followed by NXAC2PT_* properties. */
650 OFP_ASSERT(sizeof(struct nx_action_controller2) == 16);
653 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
654 enum ofp_version ofp_version OVS_UNUSED,
657 struct ofpact_controller *oc;
659 oc = ofpact_put_CONTROLLER(out);
660 oc->ofpact.raw = NXAST_RAW_CONTROLLER;
661 oc->max_len = ntohs(nac->max_len);
662 oc->controller_id = ntohs(nac->controller_id);
663 oc->reason = nac->reason;
664 ofpact_finish(out, &oc->ofpact);
670 decode_NXAST_RAW_CONTROLLER2(const struct nx_action_controller2 *nac2,
671 enum ofp_version ofp_version OVS_UNUSED,
674 if (!is_all_zeros(nac2->zeros, sizeof nac2->zeros)) {
675 return OFPERR_NXBRC_MUST_BE_ZERO;
678 size_t start_ofs = out->size;
679 struct ofpact_controller *oc = ofpact_put_CONTROLLER(out);
680 oc->ofpact.raw = NXAST_RAW_CONTROLLER2;
681 oc->max_len = UINT16_MAX;
682 oc->reason = OFPR_ACTION;
684 struct ofpbuf properties;
685 ofpbuf_use_const(&properties, nac2, ntohs(nac2->len));
686 ofpbuf_pull(&properties, sizeof *nac2);
688 while (properties.size > 0) {
689 struct ofpbuf payload;
692 enum ofperr error = ofpprop_pull(&properties, &payload, &type);
698 case NXAC2PT_MAX_LEN:
699 error = ofpprop_parse_u16(&payload, &oc->max_len);
702 case NXAC2PT_CONTROLLER_ID:
703 error = ofpprop_parse_u16(&payload, &oc->controller_id);
706 case NXAC2PT_REASON: {
708 error = ofpprop_parse_u8(&payload, &u8);
713 case NXAC2PT_USERDATA:
714 out->size = start_ofs + OFPACT_CONTROLLER_SIZE;
715 ofpbuf_put(out, payload.msg, ofpbuf_msgsize(&payload));
716 oc = ofpbuf_at_assert(out, start_ofs, sizeof *oc);
717 oc->userdata_len = ofpbuf_msgsize(&payload);
721 error = OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type);
729 ofpact_finish(out, &oc->ofpact);
735 encode_CONTROLLER(const struct ofpact_controller *controller,
736 enum ofp_version ofp_version OVS_UNUSED,
739 if (controller->userdata_len
740 || controller->ofpact.raw == NXAST_RAW_CONTROLLER2) {
741 size_t start_ofs = out->size;
742 put_NXAST_CONTROLLER2(out);
743 if (controller->max_len != UINT16_MAX) {
744 ofpprop_put_u16(out, NXAC2PT_MAX_LEN, controller->max_len);
746 if (controller->controller_id != 0) {
747 ofpprop_put_u16(out, NXAC2PT_CONTROLLER_ID,
748 controller->controller_id);
750 if (controller->reason != OFPR_ACTION) {
751 ofpprop_put_u8(out, NXAC2PT_REASON, controller->reason);
753 if (controller->userdata_len != 0) {
754 ofpprop_put(out, NXAC2PT_USERDATA, controller->userdata,
755 controller->userdata_len);
757 pad_ofpat(out, start_ofs);
759 struct nx_action_controller *nac;
761 nac = put_NXAST_CONTROLLER(out);
762 nac->max_len = htons(controller->max_len);
763 nac->controller_id = htons(controller->controller_id);
764 nac->reason = controller->reason;
768 static char * OVS_WARN_UNUSED_RESULT
769 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
770 enum ofputil_protocol *usable_protocols OVS_UNUSED)
772 enum ofp_packet_in_reason reason = OFPR_ACTION;
773 uint16_t controller_id = 0;
774 uint16_t max_len = UINT16_MAX;
775 const char *userdata = NULL;
779 } else if (strspn(arg, "0123456789") == strlen(arg)) {
780 char *error = str_to_u16(arg, "max_len", &max_len);
787 while (ofputil_parse_key_value(&arg, &name, &value)) {
788 if (!strcmp(name, "reason")) {
789 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
790 return xasprintf("unknown reason \"%s\"", value);
792 } else if (!strcmp(name, "max_len")) {
793 char *error = str_to_u16(value, "max_len", &max_len);
797 } else if (!strcmp(name, "id")) {
798 char *error = str_to_u16(value, "id", &controller_id);
802 } else if (!strcmp(name, "userdata")) {
805 return xasprintf("unknown key \"%s\" parsing controller "
811 if (reason == OFPR_ACTION && controller_id == 0 && !userdata) {
812 struct ofpact_output *output;
814 output = ofpact_put_OUTPUT(ofpacts);
815 output->port = OFPP_CONTROLLER;
816 output->max_len = max_len;
818 struct ofpact_controller *controller;
820 controller = ofpact_put_CONTROLLER(ofpacts);
821 controller->max_len = max_len;
822 controller->reason = reason;
823 controller->controller_id = controller_id;
826 size_t start_ofs = ofpacts->size;
827 const char *end = ofpbuf_put_hex(ofpacts, userdata, NULL);
829 return xstrdup("bad hex digit in `controller' "
830 "action `userdata'");
832 size_t userdata_len = ofpacts->size - start_ofs;
833 controller = ofpacts->header;
834 controller->userdata_len = userdata_len;
836 ofpact_finish(ofpacts, &controller->ofpact);
843 format_hex_arg(struct ds *s, const uint8_t *data, size_t len)
845 for (size_t i = 0; i < len; i++) {
849 ds_put_format(s, "%02"PRIx8, data[i]);
854 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
856 if (a->reason == OFPR_ACTION && !a->controller_id && !a->userdata_len) {
857 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
859 enum ofp_packet_in_reason reason = a->reason;
861 ds_put_cstr(s, "controller(");
862 if (reason != OFPR_ACTION) {
863 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
865 ds_put_format(s, "reason=%s,",
866 ofputil_packet_in_reason_to_string(
867 reason, reasonbuf, sizeof reasonbuf));
869 if (a->max_len != UINT16_MAX) {
870 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
872 if (a->controller_id != 0) {
873 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
875 if (a->userdata_len) {
876 ds_put_cstr(s, "userdata=");
877 format_hex_arg(s, a->userdata, a->userdata_len);
885 /* Enqueue action. */
886 struct ofp10_action_enqueue {
887 ovs_be16 type; /* OFPAT10_ENQUEUE. */
888 ovs_be16 len; /* Len is 16. */
889 ovs_be16 port; /* Port that queue belongs. Should
890 refer to a valid physical port
891 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
892 uint8_t pad[6]; /* Pad for 64-bit alignment. */
893 ovs_be32 queue_id; /* Where to enqueue the packets. */
895 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
898 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
899 enum ofp_version ofp_version OVS_UNUSED,
902 struct ofpact_enqueue *enqueue;
904 enqueue = ofpact_put_ENQUEUE(out);
905 enqueue->port = u16_to_ofp(ntohs(oae->port));
906 enqueue->queue = ntohl(oae->queue_id);
907 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
908 && enqueue->port != OFPP_IN_PORT
909 && enqueue->port != OFPP_LOCAL) {
910 return OFPERR_OFPBAC_BAD_OUT_PORT;
916 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
917 enum ofp_version ofp_version, struct ofpbuf *out)
919 if (ofp_version == OFP10_VERSION) {
920 struct ofp10_action_enqueue *oae;
922 oae = put_OFPAT10_ENQUEUE(out);
923 oae->port = htons(ofp_to_u16(enqueue->port));
924 oae->queue_id = htonl(enqueue->queue);
930 static char * OVS_WARN_UNUSED_RESULT
931 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
932 enum ofputil_protocol *usable_protocols OVS_UNUSED)
935 char *port = strtok_r(arg, ":q,", &sp);
936 char *queue = strtok_r(NULL, "", &sp);
937 struct ofpact_enqueue *enqueue;
939 if (port == NULL || queue == NULL) {
940 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
941 "\"enqueue(PORT,QUEUE)\"");
944 enqueue = ofpact_put_ENQUEUE(ofpacts);
945 if (!ofputil_port_from_string(port, &enqueue->port)) {
946 return xasprintf("%s: enqueue to unknown port", port);
948 return str_to_u32(queue, &enqueue->queue);
952 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
954 ds_put_format(s, "enqueue:");
955 ofputil_format_port(a->port, s);
956 ds_put_format(s, ":%"PRIu32, a->queue);
959 /* Action structure for NXAST_OUTPUT_REG.
961 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
963 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
964 * the NXAST_REG_LOAD action.
966 * The acceptable nxm_header values for 'src' are the same as the acceptable
967 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
969 * The 'max_len' field indicates the number of bytes to send when the chosen
970 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
971 * field of OFPAT_OUTPUT.
973 * The 'zero' field is required to be zeroed for forward compatibility. */
974 struct nx_action_output_reg {
975 ovs_be16 type; /* OFPAT_VENDOR. */
976 ovs_be16 len; /* 24. */
977 ovs_be32 vendor; /* NX_VENDOR_ID. */
978 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
980 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
981 ovs_be32 src; /* Source. */
983 ovs_be16 max_len; /* Max length to send to controller. */
985 uint8_t zero[6]; /* Reserved, must be zero. */
987 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
989 /* Action structure for NXAST_OUTPUT_REG2.
991 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
992 * experimenter OXM as 'src'.
994 struct nx_action_output_reg2 {
995 ovs_be16 type; /* OFPAT_VENDOR. */
996 ovs_be16 len; /* 24. */
997 ovs_be32 vendor; /* NX_VENDOR_ID. */
998 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
1000 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1001 ovs_be16 max_len; /* Max length to send to controller. */
1004 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1005 * - Enough 0-bytes to pad the action out to 24 bytes. */
1008 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
1011 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
1012 enum ofp_version ofp_version OVS_UNUSED,
1015 struct ofpact_output_reg *output_reg;
1017 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
1018 return OFPERR_OFPBAC_BAD_ARGUMENT;
1021 output_reg = ofpact_put_OUTPUT_REG(out);
1022 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
1023 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
1024 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1025 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1026 output_reg->max_len = ntohs(naor->max_len);
1028 return mf_check_src(&output_reg->src, NULL);
1032 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
1033 enum ofp_version ofp_version OVS_UNUSED,
1036 struct ofpact_output_reg *output_reg;
1037 output_reg = ofpact_put_OUTPUT_REG(out);
1038 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
1039 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1040 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1041 output_reg->max_len = ntohs(naor->max_len);
1043 struct ofpbuf b = ofpbuf_const_initializer(naor, ntohs(naor->len));
1044 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
1046 enum ofperr error = nx_pull_header(&b, &output_reg->src.field, NULL);
1050 if (!is_all_zeros(b.data, b.size)) {
1051 return OFPERR_NXBRC_MUST_BE_ZERO;
1054 return mf_check_src(&output_reg->src, NULL);
1058 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
1059 enum ofp_version ofp_version OVS_UNUSED,
1062 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1063 * cannot be encoded in the older form, encode it as
1064 * NXAST_RAW_OUTPUT_REG2. */
1065 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
1066 || !mf_nxm_header(output_reg->src.field->id)) {
1067 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
1068 size_t size = out->size;
1070 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1071 output_reg->src.n_bits);
1072 naor->max_len = htons(output_reg->max_len);
1074 out->size = size - sizeof naor->pad;
1075 nx_put_header(out, output_reg->src.field->id, 0, false);
1078 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
1080 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1081 output_reg->src.n_bits);
1082 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
1083 naor->max_len = htons(output_reg->max_len);
1087 static char * OVS_WARN_UNUSED_RESULT
1088 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
1089 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1091 return parse_OUTPUT(arg, ofpacts, usable_protocols);
1095 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
1097 ds_put_cstr(s, "output:");
1098 mf_format_subfield(&a->src, s);
1101 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1103 * The bundle actions choose a slave from a supplied list of options.
1104 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
1105 * selection to a register.
1107 * The list of possible slaves follows the nx_action_bundle structure. The size
1108 * of each slave is governed by its type as indicated by the 'slave_type'
1109 * parameter. The list of slaves should be padded at its end with zeros to make
1110 * the total length of the action a multiple of 8.
1112 * Switches infer from the 'slave_type' parameter the size of each slave. All
1113 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
1114 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
1115 * 2 byte width. Switches should reject actions which indicate unknown or
1116 * unsupported slave types.
1118 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1119 * slave. If the switch does not support the specified 'algorithm' parameter,
1120 * it should reject the action.
1122 * Several algorithms take into account liveness when selecting slaves. The
1123 * liveness of a slave is implementation defined (with one exception), but will
1124 * generally take into account things like its carrier status and the results
1125 * of any link monitoring protocols which happen to be running on it. In order
1126 * to give controllers a place-holder value, the OFPP_NONE port is always
1129 * Some slave selection strategies require the use of a hash function, in which
1130 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
1131 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
1132 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
1133 * 'basis' parameter is used as a universal hash parameter. Different values
1134 * of 'basis' yield different hash results.
1136 * The 'zero' parameter at the end of the action structure is reserved for
1137 * future use. Switches are required to reject actions which have nonzero
1138 * bytes in the 'zero' field.
1140 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
1141 * should reject actions which have nonzero bytes in either of these fields.
1143 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
1144 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
1145 * similar to those for the NXAST_REG_LOAD action. */
1146 struct nx_action_bundle {
1147 ovs_be16 type; /* OFPAT_VENDOR. */
1148 ovs_be16 len; /* Length including slaves. */
1149 ovs_be32 vendor; /* NX_VENDOR_ID. */
1150 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1152 /* Slave choice algorithm to apply to hash value. */
1153 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
1155 /* What fields to hash and how. */
1156 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
1157 ovs_be16 basis; /* Universal hash parameter. */
1159 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
1160 ovs_be16 n_slaves; /* Number of slaves. */
1162 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1163 ovs_be32 dst; /* Destination. */
1165 uint8_t zero[4]; /* Reserved. Must be zero. */
1167 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
1170 decode_bundle(bool load, const struct nx_action_bundle *nab,
1171 struct ofpbuf *ofpacts)
1173 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1174 struct ofpact_bundle *bundle;
1175 uint32_t slave_type;
1176 size_t slaves_size, i;
1179 bundle = ofpact_put_BUNDLE(ofpacts);
1181 bundle->n_slaves = ntohs(nab->n_slaves);
1182 bundle->basis = ntohs(nab->basis);
1183 bundle->fields = ntohs(nab->fields);
1184 bundle->algorithm = ntohs(nab->algorithm);
1185 slave_type = ntohl(nab->slave_type);
1186 slaves_size = ntohs(nab->len) - sizeof *nab;
1188 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1189 if (!flow_hash_fields_valid(bundle->fields)) {
1190 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
1191 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
1192 VLOG_WARN_RL(&rl, "too many slaves");
1193 } else if (bundle->algorithm != NX_BD_ALG_HRW
1194 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
1195 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
1196 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
1197 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
1202 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
1203 VLOG_WARN_RL(&rl, "reserved field is nonzero");
1204 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1208 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
1209 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
1210 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
1212 if (bundle->dst.n_bits < 16) {
1213 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
1215 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1218 if (nab->ofs_nbits || nab->dst) {
1219 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
1220 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1224 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1225 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1226 "allocated for slaves. %"PRIuSIZE" bytes are required "
1227 "for %"PRIu16" slaves.",
1228 load ? "bundle_load" : "bundle", slaves_size,
1229 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1230 error = OFPERR_OFPBAC_BAD_LEN;
1233 for (i = 0; i < bundle->n_slaves; i++) {
1234 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1235 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1238 bundle = ofpacts->header;
1239 ofpact_finish(ofpacts, &bundle->ofpact);
1242 error = bundle_check(bundle, OFPP_MAX, NULL);
1248 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1249 enum ofp_version ofp_version OVS_UNUSED,
1252 return decode_bundle(false, nab, out);
1256 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1257 enum ofp_version ofp_version OVS_UNUSED,
1260 return decode_bundle(true, nab, out);
1264 encode_BUNDLE(const struct ofpact_bundle *bundle,
1265 enum ofp_version ofp_version OVS_UNUSED,
1268 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1269 struct nx_action_bundle *nab;
1273 nab = (bundle->dst.field
1274 ? put_NXAST_BUNDLE_LOAD(out)
1275 : put_NXAST_BUNDLE(out));
1276 nab->len = htons(ntohs(nab->len) + slaves_len);
1277 nab->algorithm = htons(bundle->algorithm);
1278 nab->fields = htons(bundle->fields);
1279 nab->basis = htons(bundle->basis);
1280 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1281 nab->n_slaves = htons(bundle->n_slaves);
1282 if (bundle->dst.field) {
1283 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1284 bundle->dst.n_bits);
1285 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1288 slaves = ofpbuf_put_zeros(out, slaves_len);
1289 for (i = 0; i < bundle->n_slaves; i++) {
1290 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1294 static char * OVS_WARN_UNUSED_RESULT
1295 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1296 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1298 return bundle_parse(arg, ofpacts);
1301 static char * OVS_WARN_UNUSED_RESULT
1302 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1304 return bundle_parse_load(arg, ofpacts);
1308 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1310 bundle_format(a, s);
1313 /* Set VLAN actions. */
1316 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1319 return OFPERR_OFPBAC_BAD_ARGUMENT;
1321 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1322 vlan_vid->vlan_vid = vid;
1323 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1329 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1330 enum ofp_version ofp_version OVS_UNUSED,
1333 return decode_set_vlan_vid(vid, true, out);
1337 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1338 enum ofp_version ofp_version OVS_UNUSED,
1341 return decode_set_vlan_vid(vid, false, out);
1345 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1346 enum ofp_version ofp_version, struct ofpbuf *out)
1348 uint16_t vid = vlan_vid->vlan_vid;
1350 /* Push a VLAN tag, if none is present and this form of the action calls
1351 * for such a feature. */
1352 if (ofp_version > OFP10_VERSION
1353 && vlan_vid->push_vlan_if_needed
1354 && !vlan_vid->flow_has_vlan) {
1355 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1358 if (ofp_version == OFP10_VERSION) {
1359 put_OFPAT10_SET_VLAN_VID(out, vid);
1360 } else if (ofp_version == OFP11_VERSION) {
1361 put_OFPAT11_SET_VLAN_VID(out, vid);
1363 ofpact_put_set_field(out, ofp_version,
1364 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1368 static char * OVS_WARN_UNUSED_RESULT
1369 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1371 struct ofpact_vlan_vid *vlan_vid;
1375 error = str_to_u16(arg, "VLAN VID", &vid);
1380 if (vid & ~VLAN_VID_MASK) {
1381 return xasprintf("%s: not a valid VLAN VID", arg);
1383 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1384 vlan_vid->vlan_vid = vid;
1385 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1389 static char * OVS_WARN_UNUSED_RESULT
1390 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1391 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1393 return parse_set_vlan_vid(arg, ofpacts, false);
1397 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1399 ds_put_format(s, "%s:%"PRIu16,
1400 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1404 /* Set PCP actions. */
1407 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1410 return OFPERR_OFPBAC_BAD_ARGUMENT;
1412 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1413 vlan_pcp->vlan_pcp = pcp;
1414 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1420 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1421 enum ofp_version ofp_version OVS_UNUSED,
1424 return decode_set_vlan_pcp(pcp, true, out);
1428 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1429 enum ofp_version ofp_version OVS_UNUSED,
1432 return decode_set_vlan_pcp(pcp, false, out);
1436 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1437 enum ofp_version ofp_version, struct ofpbuf *out)
1439 uint8_t pcp = vlan_pcp->vlan_pcp;
1441 /* Push a VLAN tag, if none is present and this form of the action calls
1442 * for such a feature. */
1443 if (ofp_version > OFP10_VERSION
1444 && vlan_pcp->push_vlan_if_needed
1445 && !vlan_pcp->flow_has_vlan) {
1446 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1449 if (ofp_version == OFP10_VERSION) {
1450 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1451 } else if (ofp_version == OFP11_VERSION) {
1452 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1454 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1458 static char * OVS_WARN_UNUSED_RESULT
1459 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1461 struct ofpact_vlan_pcp *vlan_pcp;
1465 error = str_to_u8(arg, "VLAN PCP", &pcp);
1471 return xasprintf("%s: not a valid VLAN PCP", arg);
1473 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1474 vlan_pcp->vlan_pcp = pcp;
1475 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1479 static char * OVS_WARN_UNUSED_RESULT
1480 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1481 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1483 return parse_set_vlan_pcp(arg, ofpacts, false);
1487 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1489 ds_put_format(s, "%s:%"PRIu8,
1490 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1494 /* Strip VLAN actions. */
1497 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1499 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1504 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1506 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1511 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1512 enum ofp_version ofp_version, struct ofpbuf *out)
1514 if (ofp_version == OFP10_VERSION) {
1515 put_OFPAT10_STRIP_VLAN(out);
1517 put_OFPAT11_POP_VLAN(out);
1521 static char * OVS_WARN_UNUSED_RESULT
1522 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1523 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1525 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1529 static char * OVS_WARN_UNUSED_RESULT
1530 parse_pop_vlan(struct ofpbuf *ofpacts)
1532 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1537 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1539 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1544 /* Push VLAN action. */
1547 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1548 enum ofp_version ofp_version OVS_UNUSED,
1551 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1552 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1553 return OFPERR_OFPBAC_BAD_ARGUMENT;
1555 ofpact_put_PUSH_VLAN(out);
1560 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1561 enum ofp_version ofp_version, struct ofpbuf *out)
1563 if (ofp_version == OFP10_VERSION) {
1564 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1565 * follow this action. */
1567 /* XXX ETH_TYPE_VLAN_8021AD case */
1568 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1572 static char * OVS_WARN_UNUSED_RESULT
1573 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1574 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1579 *usable_protocols &= OFPUTIL_P_OF11_UP;
1580 error = str_to_u16(arg, "ethertype", ðertype);
1585 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1586 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1587 return xasprintf("%s: not a valid VLAN ethertype", arg);
1590 ofpact_put_PUSH_VLAN(ofpacts);
1595 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1597 /* XXX 802.1AD case*/
1598 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1601 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1602 struct ofp_action_dl_addr {
1603 ovs_be16 type; /* Type. */
1604 ovs_be16 len; /* Length is 16. */
1605 struct eth_addr dl_addr; /* Ethernet address. */
1608 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1611 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1612 enum ofp_version ofp_version OVS_UNUSED,
1615 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1620 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1621 enum ofp_version ofp_version OVS_UNUSED,
1624 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1629 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1630 enum ofp_raw_action_type raw, enum mf_field_id field,
1633 if (ofp_version < OFP12_VERSION) {
1634 struct ofp_action_dl_addr *oada;
1636 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1637 oada->dl_addr = mac->mac;
1639 ofpact_put_set_field(out, ofp_version, field,
1640 eth_addr_to_uint64(mac->mac));
1645 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1648 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1654 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1655 enum ofp_version ofp_version,
1658 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1663 static char * OVS_WARN_UNUSED_RESULT
1664 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1665 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1667 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1670 static char * OVS_WARN_UNUSED_RESULT
1671 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1672 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1674 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1678 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1680 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1684 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1686 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1689 /* Set IPv4 address actions. */
1692 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1693 enum ofp_version ofp_version OVS_UNUSED,
1696 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1701 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1702 enum ofp_version ofp_version OVS_UNUSED,
1705 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1710 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1711 enum ofp_version ofp_version,
1712 enum ofp_raw_action_type raw, enum mf_field_id field,
1715 ovs_be32 addr = ipv4->ipv4;
1716 if (ofp_version < OFP12_VERSION) {
1717 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1719 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1724 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1725 enum ofp_version ofp_version, struct ofpbuf *out)
1727 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1732 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1733 enum ofp_version ofp_version, struct ofpbuf *out)
1735 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1739 static char * OVS_WARN_UNUSED_RESULT
1740 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1741 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1743 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1746 static char * OVS_WARN_UNUSED_RESULT
1747 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1748 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1750 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1754 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1756 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1760 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1762 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1765 /* Set IPv4/v6 TOS actions. */
1768 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1769 enum ofp_version ofp_version OVS_UNUSED,
1772 if (dscp & ~IP_DSCP_MASK) {
1773 return OFPERR_OFPBAC_BAD_ARGUMENT;
1775 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1781 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1782 enum ofp_version ofp_version, struct ofpbuf *out)
1784 if (ofp_version < OFP12_VERSION) {
1785 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1787 ofpact_put_set_field(out, ofp_version,
1788 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1792 static char * OVS_WARN_UNUSED_RESULT
1793 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1794 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1799 error = str_to_u8(arg, "TOS", &tos);
1804 if (tos & ~IP_DSCP_MASK) {
1805 return xasprintf("%s: not a valid TOS", arg);
1807 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1812 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1814 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1817 /* Set IPv4/v6 ECN actions. */
1820 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1821 enum ofp_version ofp_version OVS_UNUSED,
1824 if (ecn & ~IP_ECN_MASK) {
1825 return OFPERR_OFPBAC_BAD_ARGUMENT;
1827 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1833 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1834 enum ofp_version ofp_version, struct ofpbuf *out)
1836 uint8_t ecn = ip_ecn->ecn;
1837 if (ofp_version == OFP10_VERSION) {
1839 } else if (ofp_version == OFP11_VERSION) {
1840 put_OFPAT11_SET_NW_ECN(out, ecn);
1842 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1846 static char * OVS_WARN_UNUSED_RESULT
1847 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1848 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1853 error = str_to_u8(arg, "ECN", &ecn);
1858 if (ecn & ~IP_ECN_MASK) {
1859 return xasprintf("%s: not a valid ECN", arg);
1861 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1866 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1868 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1871 /* Set IPv4/v6 TTL actions. */
1874 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1875 enum ofp_version ofp_version OVS_UNUSED,
1878 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1883 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1884 enum ofp_version ofp_version, struct ofpbuf *out)
1886 if (ofp_version >= OFP11_VERSION) {
1887 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1893 static char * OVS_WARN_UNUSED_RESULT
1894 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1895 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1900 error = str_to_u8(arg, "TTL", &ttl);
1905 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1910 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1912 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1915 /* Set TCP/UDP/SCTP port actions. */
1918 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1919 enum ofp_version ofp_version OVS_UNUSED,
1922 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1927 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1928 enum ofp_version ofp_version OVS_UNUSED,
1931 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1936 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1937 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1938 enum mf_field_id field, struct ofpbuf *out)
1940 uint16_t port = l4_port->port;
1942 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1943 ofpact_put_set_field(out, ofp_version, field, port);
1945 ofpact_put_raw(out, ofp_version, raw, port);
1950 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1951 enum ofp_version ofp_version, struct ofpbuf *out)
1953 uint8_t proto = l4_port->flow_ip_proto;
1954 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1955 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1956 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1959 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1963 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1964 enum ofp_version ofp_version,
1967 uint8_t proto = l4_port->flow_ip_proto;
1968 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1969 : proto == IPPROTO_UDP ? MFF_UDP_DST
1970 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1973 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1976 static char * OVS_WARN_UNUSED_RESULT
1977 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1978 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1980 return str_to_u16(arg, "source port",
1981 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1984 static char * OVS_WARN_UNUSED_RESULT
1985 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1986 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1988 return str_to_u16(arg, "destination port",
1989 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1993 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1995 ds_put_format(s, "mod_tp_src:%d", a->port);
1999 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
2001 ds_put_format(s, "mod_tp_dst:%d", a->port);
2004 /* Action structure for OFPAT_COPY_FIELD. */
2005 struct ofp15_action_copy_field {
2006 ovs_be16 type; /* OFPAT_COPY_FIELD. */
2007 ovs_be16 len; /* Length is padded to 64 bits. */
2008 ovs_be16 n_bits; /* Number of bits to copy. */
2009 ovs_be16 src_offset; /* Starting bit offset in source. */
2010 ovs_be16 dst_offset; /* Starting bit offset in destination. */
2013 * - OXM header for source field.
2014 * - OXM header for destination field.
2015 * - Padding with 0-bytes to a multiple of 8 bytes.
2016 * The "pad2" member is the beginning of the above. */
2019 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
2021 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
2022 struct onf_action_copy_field {
2023 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
2024 ovs_be16 len; /* Length is padded to 64 bits. */
2025 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
2026 ovs_be16 exp_type; /* 3200. */
2027 uint8_t pad[2]; /* Not used. */
2028 ovs_be16 n_bits; /* Number of bits to copy. */
2029 ovs_be16 src_offset; /* Starting bit offset in source. */
2030 ovs_be16 dst_offset; /* Starting bit offset in destination. */
2031 uint8_t pad2[2]; /* Not used. */
2033 * - OXM header for source field.
2034 * - OXM header for destination field.
2035 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2036 * The "pad3" member is the beginning of the above. */
2037 uint8_t pad3[4]; /* Not used. */
2039 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
2041 /* Action structure for NXAST_REG_MOVE.
2043 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2044 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2045 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
2046 * the next most significant bit, and so on.
2048 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
2049 * sense to use nxm_hasmask=1 because the action does not do any kind of
2050 * matching; it uses the actual value of a field.)
2052 * The following nxm_header values are potentially acceptable as 'src':
2067 * - NXM_OF_ICMP_TYPE
2068 * - NXM_OF_ICMP_CODE
2075 * - NXM_NX_ICMPV6_TYPE
2076 * - NXM_NX_ICMPV6_CODE
2079 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2081 * - NXM_NX_TUN_IPV4_SRC
2082 * - NXM_NX_TUN_IPV4_DST
2084 * The following nxm_header values are potentially acceptable as 'dst':
2095 * - NXM_OF_ICMP_TYPE
2096 * - NXM_OF_ICMP_CODE
2097 * - NXM_NX_ICMPV6_TYPE
2098 * - NXM_NX_ICMPV6_CODE
2104 * Modifying any of the above fields changes the corresponding packet
2109 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2113 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
2114 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
2115 * header (if any), ignoring the other bits. Setting a value with CFI=1
2116 * adds or modifies the 802.1Q header appropriately, setting the TCI field
2117 * to the field's new value (with the CFI bit masked out).
2119 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
2120 * any of these values modifies the corresponding tunnel header field used
2121 * for the packet's next tunnel encapsulation, if allowed by the
2122 * configuration of the output tunnel port.
2124 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2125 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
2126 * used only if the flow's nx_match includes an nxm_entry that specifies
2127 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2129 * The switch will reject actions for which src_ofs+n_bits is greater than the
2130 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2131 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2133 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2134 * behaves as if 'src' were copied out to a temporary buffer, then the
2135 * temporary buffer copied to 'dst'.
2137 struct nx_action_reg_move {
2138 ovs_be16 type; /* OFPAT_VENDOR. */
2139 ovs_be16 len; /* Length is 24. */
2140 ovs_be32 vendor; /* NX_VENDOR_ID. */
2141 ovs_be16 subtype; /* NXAST_REG_MOVE. */
2142 ovs_be16 n_bits; /* Number of bits. */
2143 ovs_be16 src_ofs; /* Starting bit offset in source. */
2144 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
2146 * - OXM/NXM header for source field (4 or 8 bytes).
2147 * - OXM/NXM header for destination field (4 or 8 bytes).
2148 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2150 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
2153 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
2154 const void *action, ovs_be16 action_len, size_t oxm_offset,
2155 struct ofpbuf *ofpacts)
2157 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2158 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
2159 move->src.ofs = ntohs(src_offset);
2160 move->src.n_bits = ntohs(n_bits);
2161 move->dst.ofs = ntohs(dst_offset);
2162 move->dst.n_bits = ntohs(n_bits);
2164 struct ofpbuf b = ofpbuf_const_initializer(action, ntohs(action_len));
2165 ofpbuf_pull(&b, oxm_offset);
2167 enum ofperr error = nx_pull_header(&b, &move->src.field, NULL);
2171 error = nx_pull_header(&b, &move->dst.field, NULL);
2176 if (!is_all_zeros(b.data, b.size)) {
2177 return OFPERR_NXBRC_MUST_BE_ZERO;
2180 return nxm_reg_move_check(move, NULL);
2184 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
2185 enum ofp_version ofp_version OVS_UNUSED,
2186 struct ofpbuf *ofpacts)
2188 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2189 oacf->n_bits, oacf, oacf->len,
2190 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
2194 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
2195 enum ofp_version ofp_version OVS_UNUSED,
2196 struct ofpbuf *ofpacts)
2198 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2199 oacf->n_bits, oacf, oacf->len,
2200 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
2204 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
2205 enum ofp_version ofp_version OVS_UNUSED,
2206 struct ofpbuf *ofpacts)
2208 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2209 move->ofpact.raw = NXAST_RAW_REG_MOVE;
2210 move->src.ofs = ntohs(narm->src_ofs);
2211 move->src.n_bits = ntohs(narm->n_bits);
2212 move->dst.ofs = ntohs(narm->dst_ofs);
2213 move->dst.n_bits = ntohs(narm->n_bits);
2215 struct ofpbuf b = ofpbuf_const_initializer(narm, ntohs(narm->len));
2216 ofpbuf_pull(&b, sizeof *narm);
2218 enum ofperr error = nx_pull_header(&b, &move->src.field, NULL);
2222 error = nx_pull_header(&b, &move->dst.field, NULL);
2226 if (!is_all_zeros(b.data, b.size)) {
2227 return OFPERR_NXBRC_MUST_BE_ZERO;
2230 return nxm_reg_move_check(move, NULL);
2234 encode_REG_MOVE(const struct ofpact_reg_move *move,
2235 enum ofp_version ofp_version, struct ofpbuf *out)
2237 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2238 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2239 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2240 * probe for support. Until we have that ability, we currently prefer
2241 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2243 size_t start_ofs = out->size;
2244 if (ofp_version >= OFP15_VERSION) {
2245 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2246 copy->n_bits = htons(move->dst.n_bits);
2247 copy->src_offset = htons(move->src.ofs);
2248 copy->dst_offset = htons(move->dst.ofs);
2249 out->size = out->size - sizeof copy->pad2;
2250 nx_put_header(out, move->src.field->id, ofp_version, false);
2251 nx_put_header(out, move->dst.field->id, ofp_version, false);
2252 } else if (ofp_version == OFP13_VERSION
2253 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2254 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2255 copy->n_bits = htons(move->dst.n_bits);
2256 copy->src_offset = htons(move->src.ofs);
2257 copy->dst_offset = htons(move->dst.ofs);
2258 out->size = out->size - sizeof copy->pad3;
2259 nx_put_header(out, move->src.field->id, ofp_version, false);
2260 nx_put_header(out, move->dst.field->id, ofp_version, false);
2262 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2263 narm->n_bits = htons(move->dst.n_bits);
2264 narm->src_ofs = htons(move->src.ofs);
2265 narm->dst_ofs = htons(move->dst.ofs);
2266 nx_put_header(out, move->src.field->id, 0, false);
2267 nx_put_header(out, move->dst.field->id, 0, false);
2269 pad_ofpat(out, start_ofs);
2272 static char * OVS_WARN_UNUSED_RESULT
2273 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2274 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2276 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2277 const char *full_arg = arg;
2280 error = mf_parse_subfield__(&move->src, &arg);
2284 if (strncmp(arg, "->", 2)) {
2285 return xasprintf("%s: missing `->' following source", full_arg);
2288 error = mf_parse_subfield(&move->dst, arg);
2293 if (move->src.n_bits != move->dst.n_bits) {
2294 return xasprintf("%s: source field is %d bits wide but destination is "
2295 "%d bits wide", full_arg,
2296 move->src.n_bits, move->dst.n_bits);
2302 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2304 nxm_format_reg_move(a, s);
2307 /* Action structure for OFPAT12_SET_FIELD. */
2308 struct ofp12_action_set_field {
2309 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2310 ovs_be16 len; /* Length is padded to 64 bits. */
2313 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2314 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2316 * The "pad" member is the beginning of the above. */
2319 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2321 /* Action structure for NXAST_REG_LOAD.
2323 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2324 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2325 * starts at 0 for the least-significant bit, 1 for the next most significant
2328 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2329 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2332 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2333 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2334 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2335 * also stored as one less than its true value:
2338 * +------------------------------+------------------+
2339 * | ofs | n_bits - 1 |
2340 * +------------------------------+------------------+
2342 * The switch will reject actions for which ofs+n_bits is greater than the
2343 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2344 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2345 * OFPBAC_BAD_ARGUMENT.
2347 struct nx_action_reg_load {
2348 ovs_be16 type; /* OFPAT_VENDOR. */
2349 ovs_be16 len; /* Length is 24. */
2350 ovs_be32 vendor; /* NX_VENDOR_ID. */
2351 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2352 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2353 ovs_be32 dst; /* Destination register. */
2354 ovs_be64 value; /* Immediate value. */
2356 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2358 /* Action structure for NXAST_REG_LOAD2.
2360 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2361 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2362 * OXM experimenter fields. */
2363 struct nx_action_reg_load2 {
2364 ovs_be16 type; /* OFPAT_VENDOR. */
2365 ovs_be16 len; /* At least 16. */
2366 ovs_be32 vendor; /* NX_VENDOR_ID. */
2367 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2370 * - An NXM/OXM header, value, and optionally a mask.
2371 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2373 * The "pad" member is the beginning of the above. */
2376 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2379 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2380 bool may_mask, struct ofpbuf *ofpacts)
2382 struct ofpbuf b = ofpbuf_const_initializer(oasf, ntohs(oasf->len));
2383 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2385 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2386 enum ofperr error = nx_pull_entry(&b, &sf->field, &sf->value,
2387 may_mask ? &sf->mask : NULL);
2389 return (error == OFPERR_OFPBMC_BAD_MASK
2390 ? OFPERR_OFPBAC_BAD_SET_MASK
2394 memset(&sf->mask, 0xff, sf->field->n_bytes);
2397 if (!is_all_zeros(b.data, b.size)) {
2398 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2401 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2403 if (sf->field->id == MFF_IN_PORT_OXM) {
2404 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2407 /* oxm_length is now validated to be compatible with mf_value. */
2408 if (!sf->field->writable) {
2409 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2411 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2414 /* The value must be valid for match. OpenFlow 1.5 also says,
2415 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2416 * a 1-bit in oxm_value and in oxm_mask." */
2417 if (!mf_is_value_valid(sf->field, &sf->value)
2418 || (sf->field->id == MFF_VLAN_VID
2419 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2420 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2421 struct ds ds = DS_EMPTY_INITIALIZER;
2422 mf_format(sf->field, &sf->value, NULL, &ds);
2423 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2424 sf->field->name, ds_cstr(&ds));
2427 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2433 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2434 enum ofp_version ofp_version OVS_UNUSED,
2435 struct ofpbuf *ofpacts)
2437 return decode_ofpat_set_field(oasf, false, ofpacts);
2441 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2442 enum ofp_version ofp_version OVS_UNUSED,
2443 struct ofpbuf *ofpacts)
2445 return decode_ofpat_set_field(oasf, true, ofpacts);
2449 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2450 enum ofp_version ofp_version OVS_UNUSED,
2453 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2454 struct mf_subfield dst;
2457 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2459 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2460 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2461 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2462 error = mf_check_dst(&dst, NULL);
2467 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2469 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2470 return OFPERR_OFPBAC_BAD_ARGUMENT;
2473 sf->field = dst.field;
2474 bitwise_put(ntohll(narl->value),
2475 &sf->value, dst.field->n_bytes, dst.ofs,
2477 bitwise_put(UINT64_MAX,
2478 &sf->mask, dst.field->n_bytes, dst.ofs,
2485 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2486 enum ofp_version ofp_version OVS_UNUSED,
2489 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(out);
2490 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2492 struct ofpbuf b = ofpbuf_const_initializer(narl, ntohs(narl->len));
2493 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2495 enum ofperr error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2499 if (!is_all_zeros(b.data, b.size)) {
2500 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2503 if (!sf->field->writable) {
2504 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2506 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2512 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2513 enum mf_field_id field, uint64_t value_)
2515 struct ofp12_action_set_field *oasf OVS_UNUSED;
2516 int n_bytes = mf_from_id(field)->n_bytes;
2517 size_t start_ofs = openflow->size;
2518 union mf_value value;
2520 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2522 oasf = put_OFPAT12_SET_FIELD(openflow);
2523 openflow->size = openflow->size - sizeof oasf->pad;
2524 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2525 pad_ofpat(openflow, start_ofs);
2529 next_load_segment(const struct ofpact_set_field *sf,
2530 struct mf_subfield *dst, uint64_t *value)
2532 int n_bits = sf->field->n_bits;
2533 int n_bytes = sf->field->n_bytes;
2534 int start = dst->ofs + dst->n_bits;
2536 if (start < n_bits) {
2537 dst->field = sf->field;
2538 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2539 if (dst->ofs < n_bits) {
2540 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2541 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2542 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2549 /* Convert 'sf' to a series of REG_LOADs. */
2551 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2553 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2554 * experimenter OXM or is variable length (or if it came in as
2555 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2556 * NXAST_REG_LOAD, which is backward compatible. */
2557 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2558 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2559 struct nx_action_reg_load2 *narl OVS_UNUSED;
2560 size_t start_ofs = openflow->size;
2562 narl = put_NXAST_REG_LOAD2(openflow);
2563 openflow->size = openflow->size - sizeof narl->pad;
2564 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2565 pad_ofpat(openflow, start_ofs);
2567 struct mf_subfield dst;
2570 dst.ofs = dst.n_bits = 0;
2571 while (next_load_segment(sf, &dst, &value)) {
2572 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2573 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2574 narl->dst = htonl(mf_nxm_header(dst.field->id));
2575 narl->value = htonll(value);
2580 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2581 * actions, if we can, falling back to Nicira extensions if we must.
2583 * We check only meta-flow types that can appear within set field actions and
2584 * that have a mapping to compatible action types. These struct mf_field
2585 * definitions have a defined OXM or NXM header value and specify the field as
2588 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2589 enum ofp_version ofp_version,
2592 switch ((int) sf->field->id) {
2593 case MFF_VLAN_TCI: {
2594 ovs_be16 tci = sf->value.be16;
2595 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2596 uint16_t vid = vlan_tci_to_vid(tci);
2597 uint8_t pcp = vlan_tci_to_pcp(tci);
2599 if (ofp_version < OFP11_VERSION) {
2600 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2602 * If CFI=1, Add or modify VLAN VID & PCP.
2603 * If CFI=0, strip VLAN header, if any.
2606 put_OFPAT10_SET_VLAN_VID(out, vid);
2607 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2609 put_OFPAT10_STRIP_VLAN(out);
2612 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2614 * If CFI=1, Add or modify VLAN VID & PCP.
2615 * OpenFlow 1.1 set actions only apply if the packet
2616 * already has VLAN tags. To be sure that is the case
2617 * we have to push a VLAN header. As we do not support
2618 * multiple layers of VLANs, this is a no-op, if a VLAN
2619 * header already exists. This may backfire, however,
2620 * when we start supporting multiple layers of VLANs.
2621 * If CFI=0, strip VLAN header, if any.
2624 /* Push a VLAN tag, if one was not seen at action validation
2626 if (!sf->flow_has_vlan) {
2627 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2629 put_OFPAT11_SET_VLAN_VID(out, vid);
2630 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2632 /* If the flow did not match on vlan, we have no way of
2633 * knowing if the vlan tag exists, so we must POP just to be
2635 put_OFPAT11_POP_VLAN(out);
2641 case MFF_VLAN_VID: {
2642 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2643 if (ofp_version == OFP10_VERSION) {
2644 put_OFPAT10_SET_VLAN_VID(out, vid);
2646 put_OFPAT11_SET_VLAN_VID(out, vid);
2652 if (ofp_version == OFP10_VERSION) {
2653 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2655 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2660 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2664 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2668 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2672 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2676 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2679 case MFF_IP_DSCP_SHIFTED:
2680 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2685 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2690 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2694 set_field_to_nxast(sf, out);
2700 set_field_to_set_field(const struct ofpact_set_field *sf,
2701 enum ofp_version ofp_version, struct ofpbuf *out)
2703 struct ofp12_action_set_field *oasf OVS_UNUSED;
2704 size_t start_ofs = out->size;
2706 oasf = put_OFPAT12_SET_FIELD(out);
2707 out->size = out->size - sizeof oasf->pad;
2708 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2709 pad_ofpat(out, start_ofs);
2713 encode_SET_FIELD(const struct ofpact_set_field *sf,
2714 enum ofp_version ofp_version, struct ofpbuf *out)
2716 if (ofp_version >= OFP15_VERSION) {
2717 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2719 set_field_to_set_field(sf, ofp_version, out);
2720 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2721 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2722 /* It came in as reg_load, send it out the same way. */
2723 set_field_to_nxast(sf, out);
2724 } else if (ofp_version < OFP12_VERSION) {
2725 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2726 set_field_to_legacy_openflow(sf, ofp_version, out);
2727 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2728 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2729 * entire field, so encode it as OFPAT_SET_FIELD. */
2730 set_field_to_set_field(sf, ofp_version, out);
2732 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2733 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2734 * so encode it as reg_load. */
2735 set_field_to_nxast(sf, out);
2739 /* Parses the input argument 'arg' into the key, value, and delimiter
2740 * components that are common across the reg_load and set_field action format.
2742 * With an argument like "1->metadata", sets the following pointers to
2743 * point within 'arg':
2748 * Returns NULL if successful, otherwise a malloc()'d string describing the
2749 * error. The caller is responsible for freeing the returned string. */
2750 static char * OVS_WARN_UNUSED_RESULT
2751 set_field_split_str(char *arg, char **key, char **value, char **delim)
2756 value_end = strstr(arg, "->");
2757 *key = value_end + strlen("->");
2763 return xasprintf("%s: missing `->'", arg);
2765 if (strlen(value_end) <= strlen("->")) {
2766 return xasprintf("%s: missing field name following `->'", arg);
2772 /* Parses a "set_field" action with argument 'arg', appending the parsed
2773 * action to 'ofpacts'.
2775 * Returns NULL if successful, otherwise a malloc()'d string describing the
2776 * error. The caller is responsible for freeing the returned string. */
2777 static char * OVS_WARN_UNUSED_RESULT
2778 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2779 enum ofputil_protocol *usable_protocols)
2781 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2785 const struct mf_field *mf;
2788 error = set_field_split_str(arg, &key, &value, &delim);
2793 mf = mf_from_name(key);
2795 return xasprintf("%s is not a valid OXM field name", key);
2797 if (!mf->writable) {
2798 return xasprintf("%s is read-only", key);
2802 error = mf_parse(mf, value, &sf->value, &sf->mask);
2807 if (!mf_is_value_valid(mf, &sf->value)) {
2808 return xasprintf("%s is not a valid value for field %s", value, key);
2811 *usable_protocols &= mf->usable_protocols_exact;
2815 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2816 * action to 'ofpacts'.
2818 * Returns NULL if successful, otherwise a malloc()'d string describing the
2819 * error. The caller is responsible for freeing the returned string. */
2820 static char * OVS_WARN_UNUSED_RESULT
2821 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2822 enum ofputil_protocol *usable_protocols)
2824 char *copy = xstrdup(arg);
2825 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2830 static char * OVS_WARN_UNUSED_RESULT
2831 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2833 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2834 struct mf_subfield dst;
2835 char *key, *value_str;
2836 union mf_value value;
2839 error = set_field_split_str(arg, &key, &value_str, NULL);
2844 error = mf_parse_subfield(&dst, key);
2849 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2851 return xasprintf("%s: cannot parse integer value", arg);
2854 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2855 dst.field->n_bytes * 8 - dst.n_bits)) {
2859 mf_format(dst.field, &value, NULL, &ds);
2860 error = xasprintf("%s: value %s does not fit into %d bits",
2861 arg, ds_cstr(&ds), dst.n_bits);
2866 sf->field = dst.field;
2867 memset(&sf->value, 0, sizeof sf->value);
2868 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2869 dst.field->n_bytes, dst.ofs, dst.n_bits);
2870 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2876 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2878 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2879 struct mf_subfield dst;
2882 dst.ofs = dst.n_bits = 0;
2883 while (next_load_segment(a, &dst, &value)) {
2884 ds_put_format(s, "load:%#"PRIx64"->", value);
2885 mf_format_subfield(&dst, s);
2886 ds_put_char(s, ',');
2890 ds_put_cstr(s, "set_field:");
2891 mf_format(a->field, &a->value, &a->mask, s);
2892 ds_put_format(s, "->%s", a->field->name);
2896 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2897 * is marked such that, if possible, it will be translated to OpenFlow as
2898 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2899 * that was the way that the action was expressed when it came into OVS or for
2900 * backward compatibility. */
2901 struct ofpact_set_field *
2902 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2904 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2905 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2909 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2911 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2914 struct nx_action_stack {
2915 ovs_be16 type; /* OFPAT_VENDOR. */
2916 ovs_be16 len; /* Length is 16. */
2917 ovs_be32 vendor; /* NX_VENDOR_ID. */
2918 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2919 ovs_be16 offset; /* Bit offset into the field. */
2921 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2922 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2923 * - Enough 0-bytes to pad out the action to 24 bytes. */
2924 uint8_t pad[12]; /* See above. */
2926 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2929 decode_stack_action(const struct nx_action_stack *nasp,
2930 struct ofpact_stack *stack_action)
2932 stack_action->subfield.ofs = ntohs(nasp->offset);
2934 struct ofpbuf b = ofpbuf_const_initializer(nasp, sizeof *nasp);
2935 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2936 enum ofperr error = nx_pull_header(&b, &stack_action->subfield.field,
2941 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2943 if (!is_all_zeros(b.data, b.size)) {
2944 return OFPERR_NXBRC_MUST_BE_ZERO;
2951 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2952 enum ofp_version ofp_version OVS_UNUSED,
2953 struct ofpbuf *ofpacts)
2955 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2956 enum ofperr error = decode_stack_action(nasp, push);
2957 return error ? error : nxm_stack_push_check(push, NULL);
2961 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2962 enum ofp_version ofp_version OVS_UNUSED,
2963 struct ofpbuf *ofpacts)
2965 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2966 enum ofperr error = decode_stack_action(nasp, pop);
2967 return error ? error : nxm_stack_pop_check(pop, NULL);
2971 encode_STACK_op(const struct ofpact_stack *stack_action,
2972 struct nx_action_stack *nasp)
2977 nasp->offset = htons(stack_action->subfield.ofs);
2979 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2980 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2981 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2982 n_bits = htons(stack_action->subfield.n_bits);
2983 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2987 encode_STACK_PUSH(const struct ofpact_stack *stack,
2988 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2990 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2994 encode_STACK_POP(const struct ofpact_stack *stack,
2995 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2997 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
3000 static char * OVS_WARN_UNUSED_RESULT
3001 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
3002 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3004 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
3007 static char * OVS_WARN_UNUSED_RESULT
3008 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
3009 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3011 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
3015 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
3017 nxm_format_stack_push(a, s);
3021 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
3023 nxm_format_stack_pop(a, s);
3026 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
3028 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
3029 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
3030 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3031 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3033 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3034 * sent only to controllers with id 0.)
3036 struct nx_action_cnt_ids {
3037 ovs_be16 type; /* OFPAT_VENDOR. */
3038 ovs_be16 len; /* Length including slaves. */
3039 ovs_be32 vendor; /* NX_VENDOR_ID. */
3040 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
3042 ovs_be16 n_controllers; /* Number of controllers. */
3043 uint8_t zeros[4]; /* Must be zero. */
3045 /* Followed by 1 or more controller ids.
3047 * uint16_t cnt_ids[]; // Controller ids.
3048 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
3051 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
3054 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
3057 struct ofpact_cnt_ids *ids;
3058 enum ofperr error = 0;
3060 ids = ofpact_put_DEC_TTL(out);
3061 ids->n_controllers = 1;
3062 ofpbuf_put(out, &id, sizeof id);
3064 ofpact_finish(out, &ids->ofpact);
3069 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
3070 enum ofp_version ofp_version OVS_UNUSED,
3073 struct ofpact_cnt_ids *ids;
3077 ids = ofpact_put_DEC_TTL(out);
3078 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3079 ids->n_controllers = ntohs(nac_ids->n_controllers);
3080 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
3082 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
3083 return OFPERR_NXBRC_MUST_BE_ZERO;
3086 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
3087 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
3088 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
3089 "are required for %"PRIu16" controllers.",
3090 ids_size, ids->n_controllers * sizeof(ovs_be16),
3091 ids->n_controllers);
3092 return OFPERR_OFPBAC_BAD_LEN;
3095 for (i = 0; i < ids->n_controllers; i++) {
3096 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
3097 ofpbuf_put(out, &id, sizeof id);
3101 ofpact_finish(out, &ids->ofpact);
3107 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
3108 enum ofp_version ofp_version, struct ofpbuf *out)
3110 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
3111 || dec_ttl->n_controllers != 1
3112 || dec_ttl->cnt_ids[0] != 0) {
3113 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
3114 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
3118 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
3119 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
3121 ids = ofpbuf_put_zeros(out, ids_len);
3122 for (i = 0; i < dec_ttl->n_controllers; i++) {
3123 ids[i] = htons(dec_ttl->cnt_ids[i]);
3126 put_OFPAT_DEC_NW_TTL(out, ofp_version);
3131 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
3133 struct ofpact_cnt_ids *ids;
3136 ofpact_put_DEC_TTL(ofpacts);
3137 ofpbuf_put(ofpacts, &id, sizeof id);
3138 ids = ofpacts->header;
3139 ids->n_controllers++;
3140 ofpact_finish(ofpacts, &ids->ofpact);
3143 static char * OVS_WARN_UNUSED_RESULT
3144 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
3145 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3148 parse_noargs_dec_ttl(ofpacts);
3150 struct ofpact_cnt_ids *ids;
3153 ids = ofpact_put_DEC_TTL(ofpacts);
3154 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3155 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
3156 cntr = strtok_r(NULL, ", ", &arg)) {
3157 uint16_t id = atoi(cntr);
3159 ofpbuf_put(ofpacts, &id, sizeof id);
3160 ids = ofpacts->header;
3161 ids->n_controllers++;
3163 if (!ids->n_controllers) {
3164 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3167 ofpact_finish(ofpacts, &ids->ofpact);
3173 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
3177 ds_put_cstr(s, "dec_ttl");
3178 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
3179 ds_put_cstr(s, "(");
3180 for (i = 0; i < a->n_controllers; i++) {
3182 ds_put_cstr(s, ",");
3184 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
3186 ds_put_cstr(s, ")");
3190 /* Set MPLS label actions. */
3193 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
3194 enum ofp_version ofp_version OVS_UNUSED,
3197 ofpact_put_SET_MPLS_LABEL(out)->label = label;
3202 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3203 enum ofp_version ofp_version,
3206 if (ofp_version < OFP12_VERSION) {
3207 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3209 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
3210 ntohl(label->label));
3214 static char * OVS_WARN_UNUSED_RESULT
3215 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3216 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3218 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3220 return xstrdup("set_mpls_label: expected label.");
3223 mpls_label->label = htonl(atoi(arg));
3228 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3230 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
3233 /* Set MPLS TC actions. */
3236 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3237 enum ofp_version ofp_version OVS_UNUSED,
3240 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3245 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3246 enum ofp_version ofp_version, struct ofpbuf *out)
3248 if (ofp_version < OFP12_VERSION) {
3249 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3251 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3255 static char * OVS_WARN_UNUSED_RESULT
3256 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3257 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3259 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3262 return xstrdup("set_mpls_tc: expected tc.");
3265 mpls_tc->tc = atoi(arg);
3270 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3272 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
3275 /* Set MPLS TTL actions. */
3278 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3279 enum ofp_version ofp_version OVS_UNUSED,
3282 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3287 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3288 enum ofp_version ofp_version, struct ofpbuf *out)
3290 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3293 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3294 * action to 'ofpacts'.
3296 * Returns NULL if successful, otherwise a malloc()'d string describing the
3297 * error. The caller is responsible for freeing the returned string. */
3298 static char * OVS_WARN_UNUSED_RESULT
3299 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3300 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3302 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3305 return xstrdup("set_mpls_ttl: expected ttl.");
3308 mpls_ttl->ttl = atoi(arg);
3313 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3315 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3318 /* Decrement MPLS TTL actions. */
3321 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3323 ofpact_put_DEC_MPLS_TTL(out);
3328 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3329 enum ofp_version ofp_version, struct ofpbuf *out)
3331 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3334 static char * OVS_WARN_UNUSED_RESULT
3335 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3336 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3338 ofpact_put_DEC_MPLS_TTL(ofpacts);
3343 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3345 ds_put_cstr(s, "dec_mpls_ttl");
3348 /* Push MPLS label action. */
3351 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3352 enum ofp_version ofp_version OVS_UNUSED,
3355 struct ofpact_push_mpls *oam;
3357 if (!eth_type_mpls(ethertype)) {
3358 return OFPERR_OFPBAC_BAD_ARGUMENT;
3360 oam = ofpact_put_PUSH_MPLS(out);
3361 oam->ethertype = ethertype;
3367 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3368 enum ofp_version ofp_version, struct ofpbuf *out)
3370 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3373 static char * OVS_WARN_UNUSED_RESULT
3374 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3375 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3380 error = str_to_u16(arg, "push_mpls", ðertype);
3382 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3388 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3390 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3393 /* Pop MPLS label action. */
3396 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3397 enum ofp_version ofp_version OVS_UNUSED,
3400 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3405 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3406 enum ofp_version ofp_version, struct ofpbuf *out)
3408 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3411 static char * OVS_WARN_UNUSED_RESULT
3412 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3413 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3418 error = str_to_u16(arg, "pop_mpls", ðertype);
3420 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3426 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3428 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3431 /* Set tunnel ID actions. */
3434 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3435 enum ofp_version ofp_version OVS_UNUSED,
3438 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3439 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3440 tunnel->tun_id = tun_id;
3445 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3446 enum ofp_version ofp_version OVS_UNUSED,
3449 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3450 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3451 tunnel->tun_id = tun_id;
3456 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3457 enum ofp_version ofp_version, struct ofpbuf *out)
3459 uint64_t tun_id = tunnel->tun_id;
3461 if (ofp_version < OFP12_VERSION) {
3462 if (tun_id <= UINT32_MAX
3463 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3464 put_NXAST_SET_TUNNEL(out, tun_id);
3466 put_NXAST_SET_TUNNEL64(out, tun_id);
3469 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3473 static char * OVS_WARN_UNUSED_RESULT
3474 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3475 enum ofp_raw_action_type raw)
3477 struct ofpact_tunnel *tunnel;
3479 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3480 tunnel->ofpact.raw = raw;
3481 return str_to_u64(arg, &tunnel->tun_id);
3484 static char * OVS_WARN_UNUSED_RESULT
3485 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3486 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3488 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3492 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3494 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3495 (a->tun_id > UINT32_MAX
3496 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3500 /* Set queue action. */
3503 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3504 enum ofp_version ofp_version OVS_UNUSED,
3507 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3512 encode_SET_QUEUE(const struct ofpact_queue *queue,
3513 enum ofp_version ofp_version, struct ofpbuf *out)
3515 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3518 static char * OVS_WARN_UNUSED_RESULT
3519 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3520 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3522 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3526 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3528 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3531 /* Pop queue action. */
3534 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3536 ofpact_put_POP_QUEUE(out);
3541 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3542 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3544 put_NXAST_POP_QUEUE(out);
3547 static char * OVS_WARN_UNUSED_RESULT
3548 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3549 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3551 ofpact_put_POP_QUEUE(ofpacts);
3556 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3558 ds_put_cstr(s, "pop_queue");
3561 /* Action structure for NXAST_FIN_TIMEOUT.
3563 * This action changes the idle timeout or hard timeout, or both, of this
3564 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3565 * When such a packet is observed, the action reduces the rule's idle timeout
3566 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3567 * action has no effect on an existing timeout that is already shorter than the
3568 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3569 * of zero has no effect on the respective timeout.
3571 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3572 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3573 * receipt of the FIN or RST.
3575 * This is useful for quickly discarding learned TCP flows that otherwise will
3576 * take a long time to expire.
3578 * This action is intended for use with an OpenFlow rule that matches only a
3579 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3580 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3581 * RST in any of those flows will cause the entire OpenFlow rule to expire
3582 * early, which is not normally desirable.
3584 struct nx_action_fin_timeout {
3585 ovs_be16 type; /* OFPAT_VENDOR. */
3586 ovs_be16 len; /* 16. */
3587 ovs_be32 vendor; /* NX_VENDOR_ID. */
3588 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3589 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3590 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3591 ovs_be16 pad; /* Must be zero. */
3593 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3596 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3597 enum ofp_version ofp_version OVS_UNUSED,
3600 struct ofpact_fin_timeout *oft;
3602 oft = ofpact_put_FIN_TIMEOUT(out);
3603 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3604 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3609 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3610 enum ofp_version ofp_version OVS_UNUSED,
3613 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3614 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3615 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3618 static char * OVS_WARN_UNUSED_RESULT
3619 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3620 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3622 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3625 while (ofputil_parse_key_value(&arg, &key, &value)) {
3628 if (!strcmp(key, "idle_timeout")) {
3629 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3630 } else if (!strcmp(key, "hard_timeout")) {
3631 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3633 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3645 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3647 ds_put_cstr(s, "fin_timeout(");
3648 if (a->fin_idle_timeout) {
3649 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3651 if (a->fin_hard_timeout) {
3652 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3655 ds_put_char(s, ')');
3658 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3660 * These actions search one of the switch's flow tables:
3662 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3663 * it specifies the table to search.
3665 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3666 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3667 * table, that is, the OpenFlow flow table that contains the flow from
3668 * which this action was obtained. If this action did not come from a
3669 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3670 * is the current table.
3672 * The flow table lookup uses a flow that may be slightly modified from the
3675 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3676 * is used as the flow's in_port.
3678 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3679 * then its value is used as the flow's in_port. Otherwise, the original
3682 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3683 * resubmit action, then the flow is updated with the new values.
3685 * Following the lookup, the original in_port is restored.
3687 * If the modified flow matched in the flow table, then the corresponding
3688 * actions are executed. Afterward, actions following the resubmit in the
3689 * original set of actions, if any, are executed; any changes made to the
3690 * packet (e.g. changes to VLAN) by secondary actions persist when those
3691 * actions are executed, although the original in_port is restored.
3693 * Resubmit actions may be used any number of times within a set of actions.
3695 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3696 * implementation-defined depth, further resubmit actions are simply ignored.
3698 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3699 * 'pad' to be all-bits-zero.
3701 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3702 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3704 struct nx_action_resubmit {
3705 ovs_be16 type; /* OFPAT_VENDOR. */
3706 ovs_be16 len; /* Length is 16. */
3707 ovs_be32 vendor; /* NX_VENDOR_ID. */
3708 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3709 ovs_be16 in_port; /* New in_port for checking flow table. */
3710 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3713 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3716 decode_NXAST_RAW_RESUBMIT(uint16_t port,
3717 enum ofp_version ofp_version OVS_UNUSED,
3720 struct ofpact_resubmit *resubmit;
3722 resubmit = ofpact_put_RESUBMIT(out);
3723 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3724 resubmit->in_port = u16_to_ofp(port);
3725 resubmit->table_id = 0xff;
3730 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3731 enum ofp_version ofp_version OVS_UNUSED,
3734 struct ofpact_resubmit *resubmit;
3736 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3737 return OFPERR_OFPBAC_BAD_ARGUMENT;
3740 resubmit = ofpact_put_RESUBMIT(out);
3741 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3742 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3743 resubmit->table_id = nar->table;
3748 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3749 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3751 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3753 if (resubmit->table_id == 0xff
3754 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3755 put_NXAST_RESUBMIT(out, in_port);
3757 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3758 nar->table = resubmit->table_id;
3759 nar->in_port = htons(in_port);
3763 static char * OVS_WARN_UNUSED_RESULT
3764 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3765 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3767 struct ofpact_resubmit *resubmit;
3768 char *in_port_s, *table_s;
3770 resubmit = ofpact_put_RESUBMIT(ofpacts);
3772 in_port_s = strsep(&arg, ",");
3773 if (in_port_s && in_port_s[0]) {
3774 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3775 return xasprintf("%s: resubmit to unknown port", in_port_s);
3778 resubmit->in_port = OFPP_IN_PORT;
3781 table_s = strsep(&arg, ",");
3782 if (table_s && table_s[0]) {
3783 uint32_t table_id = 0;
3786 error = str_to_u32(table_s, &table_id);
3790 resubmit->table_id = table_id;
3792 resubmit->table_id = 255;
3795 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3796 return xstrdup("at least one \"in_port\" or \"table\" must be "
3797 "specified on resubmit");
3803 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3805 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3806 ds_put_cstr(s, "resubmit:");
3807 ofputil_format_port(a->in_port, s);
3809 ds_put_format(s, "resubmit(");
3810 if (a->in_port != OFPP_IN_PORT) {
3811 ofputil_format_port(a->in_port, s);
3813 ds_put_char(s, ',');
3814 if (a->table_id != 255) {
3815 ds_put_format(s, "%"PRIu8, a->table_id);
3817 ds_put_char(s, ')');
3821 /* Action structure for NXAST_LEARN.
3823 * This action adds or modifies a flow in an OpenFlow table, similar to
3824 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3825 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3826 * flow's match criteria and actions are built by applying each of the series
3827 * of flow_mod_spec elements included as part of the action.
3829 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3830 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3831 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3832 * bits from a source to a destination. In this case, the header contains
3835 * 15 14 13 12 11 10 0
3836 * +------+---+------+---------------------------------+
3837 * | 0 |src| dst | n_bits |
3838 * +------+---+------+---------------------------------+
3840 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3841 * following table summarizes the meaning of each possible combination.
3842 * Details follow the table:
3845 * --- --- ----------------------------------------------------------
3846 * 0 0 Add match criteria based on value in a field.
3847 * 1 0 Add match criteria based on an immediate value.
3848 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3849 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3850 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3851 * All other combinations are undefined and not allowed.
3853 * The flow_mod_spec header is followed by a source specification and a
3854 * destination specification. The format and meaning of the source
3855 * specification depends on 'src':
3857 * - If 'src' is 0, the source bits are taken from a field in the flow to
3858 * which this action is attached. (This should be a wildcarded field. If
3859 * its value is fully specified then the source bits being copied have
3862 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3863 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3864 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3865 * 'field' and 'ofs' are subject to the same restrictions as the source
3866 * field in NXAST_REG_MOVE.
3868 * - If 'src' is 1, the source bits are a constant value. The source
3869 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3870 * number in network order, the source bits are the 'n_bits'
3871 * least-significant bits. The switch will report an error if other bits
3872 * in the constant are nonzero.
3874 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3875 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3876 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3877 * meaning of the flow_mod_spec depends on 'dst':
3879 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3880 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3881 * packet equal the source bits. 'field' may be any nxm_header with
3882 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3884 * Order is significant. Earlier flow_mod_specs must satisfy any
3885 * prerequisites for matching fields specified later, by copying constant
3886 * values into prerequisite fields.
3888 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3891 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3892 * the new flow. The new flow copies the source bits into
3893 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3896 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3897 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3899 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3900 * empty. It has the following meaning:
3902 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3903 * The new flow outputs to the OpenFlow port specified by the source field.
3904 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3905 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3908 * Resource Management
3909 * -------------------
3911 * A switch has a finite amount of flow table space available for learning.
3912 * When this space is exhausted, no new learning table entries will be learned
3913 * until some existing flow table entries expire. The controller should be
3914 * prepared to handle this by flooding (which can be implemented as a
3915 * low-priority flow).
3917 * If a learned flow matches a single TCP stream with a relatively long
3918 * timeout, one may make the best of resource constraints by setting
3919 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3920 * both, to shorter timeouts. When either of these is specified as a nonzero
3921 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3927 * The following examples give a prose description of the flow_mod_specs along
3928 * with informal notation for how those would be represented and a hex dump of
3929 * the bytes that would be required.
3931 * These examples could work with various nx_action_learn parameters. Typical
3932 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3933 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3935 * 1. Learn input port based on the source MAC, with lookup into
3936 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3938 * Match on in_port=99:
3939 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3940 * ovs_be16(99), 00 63
3941 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3943 * Match Ethernet destination on Ethernet source from packet:
3944 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3945 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3946 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3948 * Set NXM_NX_REG1[16:31] to the packet's input port:
3949 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3950 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3951 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3953 * Given a packet that arrived on port A with Ethernet source address B,
3954 * this would set up the flow "in_port=99, dl_dst=B,
3955 * actions=load:A->NXM_NX_REG1[16..31]".
3957 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3958 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3959 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3961 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3962 * into NXM_NX_REG1[16:31]:
3964 * Match on same VLAN ID as packet:
3965 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3966 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3967 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3969 * Match Ethernet destination on Ethernet source from packet:
3970 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3971 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3972 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3974 * Output to the packet's input port:
3975 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3976 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3978 * Given a packet that arrived on port A with Ethernet source address B in
3979 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3980 * actions=output:A".
3982 * In syntax accepted by ovs-ofctl, this action is:
3983 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3984 * output:NXM_OF_IN_PORT[])
3986 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3987 * 10-second MAC expiration time to make it easier to see what's going on
3989 * ovs-vsctl del-controller br0
3990 * ovs-ofctl del-flows br0
3991 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3992 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3993 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3994 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3995 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3997 * You can then dump the MAC learning table with:
3999 * ovs-ofctl dump-flows br0 table=1
4004 * For best performance, segregate learned flows into a table that is not used
4005 * for any other flows except possibly for a lowest-priority "catch-all" flow
4006 * (a flow with no match criteria). If different learning actions specify
4007 * different match criteria, use different tables for the learned flows.
4009 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
4010 * These timeouts apply to the flow that is added, which means that a flow with
4011 * an idle timeout will expire when no traffic has been sent *to* the learned
4012 * address. This is not usually the intent in MAC learning; instead, we want
4013 * the MAC learn entry to expire when no traffic has been sent *from* the
4014 * learned address. Use a hard timeout for that.
4017 * Visibility of Changes
4018 * ---------------------
4020 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
4021 * flow translation are visible to flow table lookups made later in the flow
4022 * translation. This means that, in the example above, a MAC learned by the
4023 * learn action in table 0 would be found in table 1 (if the packet being
4024 * processed had the same source and destination MAC address).
4026 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
4027 * modify a flow) by a "learn" action are visible only for later flow
4028 * translations, not for later lookups within the same flow translation. In
4029 * the MAC learning example, a MAC learned by the learn action in table 0 would
4030 * not be found in table 1 if the flow translation would resubmit to table 1
4031 * after the processing of the learn action, meaning that if this MAC had not
4032 * been learned before then the packet would be flooded. */
4033 struct nx_action_learn {
4034 ovs_be16 type; /* OFPAT_VENDOR. */
4035 ovs_be16 len; /* At least 24. */
4036 ovs_be32 vendor; /* NX_VENDOR_ID. */
4037 ovs_be16 subtype; /* NXAST_LEARN. */
4038 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
4039 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
4040 ovs_be16 priority; /* Priority level of flow entry. */
4041 ovs_be64 cookie; /* Cookie for new flow. */
4042 ovs_be16 flags; /* NX_LEARN_F_*. */
4043 uint8_t table_id; /* Table to insert flow entry. */
4044 uint8_t pad; /* Must be zero. */
4045 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
4046 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
4047 /* Followed by a sequence of flow_mod_spec elements, as described above,
4048 * until the end of the action is reached. */
4050 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
4053 get_be16(const void **pp)
4055 const ovs_be16 *p = *pp;
4056 ovs_be16 value = *p;
4062 get_be32(const void **pp)
4064 const ovs_be32 *p = *pp;
4065 ovs_be32 value = get_unaligned_be32(p);
4071 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
4073 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
4074 sf->ofs = ntohs(get_be16(p));
4075 sf->n_bits = n_bits;
4079 learn_min_len(uint16_t header)
4081 int n_bits = header & NX_LEARN_N_BITS_MASK;
4082 int src_type = header & NX_LEARN_SRC_MASK;
4083 int dst_type = header & NX_LEARN_DST_MASK;
4084 unsigned int min_len;
4087 if (src_type == NX_LEARN_SRC_FIELD) {
4088 min_len += sizeof(ovs_be32); /* src_field */
4089 min_len += sizeof(ovs_be16); /* src_ofs */
4091 min_len += DIV_ROUND_UP(n_bits, 16);
4093 if (dst_type == NX_LEARN_DST_MATCH ||
4094 dst_type == NX_LEARN_DST_LOAD) {
4095 min_len += sizeof(ovs_be32); /* dst_field */
4096 min_len += sizeof(ovs_be16); /* dst_ofs */
4101 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
4102 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
4104 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
4105 enum ofp_version ofp_version OVS_UNUSED,
4106 struct ofpbuf *ofpacts)
4108 struct ofpact_learn *learn;
4109 const void *p, *end;
4112 return OFPERR_OFPBAC_BAD_ARGUMENT;
4115 learn = ofpact_put_LEARN(ofpacts);
4117 learn->idle_timeout = ntohs(nal->idle_timeout);
4118 learn->hard_timeout = ntohs(nal->hard_timeout);
4119 learn->priority = ntohs(nal->priority);
4120 learn->cookie = nal->cookie;
4121 learn->table_id = nal->table_id;
4122 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
4123 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
4125 learn->flags = ntohs(nal->flags);
4126 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
4127 NX_LEARN_F_DELETE_LEARNED)) {
4128 return OFPERR_OFPBAC_BAD_ARGUMENT;
4131 if (learn->table_id == 0xff) {
4132 return OFPERR_OFPBAC_BAD_ARGUMENT;
4135 end = (char *) nal + ntohs(nal->len);
4136 for (p = nal + 1; p != end; ) {
4137 struct ofpact_learn_spec *spec;
4138 uint16_t header = ntohs(get_be16(&p));
4144 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
4145 learn = ofpacts->header;
4148 spec->src_type = header & NX_LEARN_SRC_MASK;
4149 spec->dst_type = header & NX_LEARN_DST_MASK;
4150 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
4152 /* Check for valid src and dst type combination. */
4153 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4154 spec->dst_type == NX_LEARN_DST_LOAD ||
4155 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
4156 spec->src_type == NX_LEARN_SRC_FIELD)) {
4159 return OFPERR_OFPBAC_BAD_ARGUMENT;
4162 /* Check that the arguments don't overrun the end of the action. */
4163 if ((char *) end - (char *) p < learn_min_len(header)) {
4164 return OFPERR_OFPBAC_BAD_LEN;
4167 /* Get the source. */
4168 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4169 get_subfield(spec->n_bits, &p, &spec->src);
4171 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4173 bitwise_copy(p, p_bytes, 0,
4174 &spec->src_imm, sizeof spec->src_imm, 0,
4176 p = (const uint8_t *) p + p_bytes;
4179 /* Get the destination. */
4180 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4181 spec->dst_type == NX_LEARN_DST_LOAD) {
4182 get_subfield(spec->n_bits, &p, &spec->dst);
4185 ofpact_finish(ofpacts, &learn->ofpact);
4187 if (!is_all_zeros(p, (char *) end - (char *) p)) {
4188 return OFPERR_OFPBAC_BAD_ARGUMENT;
4195 put_be16(struct ofpbuf *b, ovs_be16 x)
4197 ofpbuf_put(b, &x, sizeof x);
4201 put_be32(struct ofpbuf *b, ovs_be32 x)
4203 ofpbuf_put(b, &x, sizeof x);
4207 put_u16(struct ofpbuf *b, uint16_t x)
4209 put_be16(b, htons(x));
4213 put_u32(struct ofpbuf *b, uint32_t x)
4215 put_be32(b, htonl(x));
4219 encode_LEARN(const struct ofpact_learn *learn,
4220 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4222 const struct ofpact_learn_spec *spec;
4223 struct nx_action_learn *nal;
4226 start_ofs = out->size;
4227 nal = put_NXAST_LEARN(out);
4228 nal->idle_timeout = htons(learn->idle_timeout);
4229 nal->hard_timeout = htons(learn->hard_timeout);
4230 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4231 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4232 nal->priority = htons(learn->priority);
4233 nal->cookie = learn->cookie;
4234 nal->flags = htons(learn->flags);
4235 nal->table_id = learn->table_id;
4237 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4238 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4240 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4241 put_u32(out, mf_nxm_header(spec->src.field->id));
4242 put_u16(out, spec->src.ofs);
4244 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4245 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4246 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4247 bits, n_dst_bytes, 0,
4251 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4252 spec->dst_type == NX_LEARN_DST_LOAD) {
4253 put_u32(out, mf_nxm_header(spec->dst.field->id));
4254 put_u16(out, spec->dst.ofs);
4258 pad_ofpat(out, start_ofs);
4261 static char * OVS_WARN_UNUSED_RESULT
4262 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4263 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4265 return learn_parse(arg, ofpacts);
4269 format_LEARN(const struct ofpact_learn *a, struct ds *s)
4274 /* Action structure for NXAST_CONJUNCTION. */
4275 struct nx_action_conjunction {
4276 ovs_be16 type; /* OFPAT_VENDOR. */
4277 ovs_be16 len; /* At least 16. */
4278 ovs_be32 vendor; /* NX_VENDOR_ID. */
4279 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4284 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4287 add_conjunction(struct ofpbuf *out,
4288 uint32_t id, uint8_t clause, uint8_t n_clauses)
4290 struct ofpact_conjunction *oc;
4292 oc = ofpact_put_CONJUNCTION(out);
4294 oc->clause = clause;
4295 oc->n_clauses = n_clauses;
4299 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4300 enum ofp_version ofp_version OVS_UNUSED,
4303 if (nac->n_clauses < 2 || nac->n_clauses > 64
4304 || nac->clause >= nac->n_clauses) {
4305 return OFPERR_NXBAC_BAD_CONJUNCTION;
4307 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4313 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4314 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4316 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4317 nac->clause = oc->clause;
4318 nac->n_clauses = oc->n_clauses;
4319 nac->id = htonl(oc->id);
4323 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4325 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4326 oc->id, oc->clause + 1, oc->n_clauses);
4329 static char * OVS_WARN_UNUSED_RESULT
4330 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4331 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4338 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4339 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4340 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4343 if (n_clauses < 2) {
4344 return xstrdup("conjunction must have at least 2 clauses");
4345 } else if (n_clauses > 64) {
4346 return xstrdup("conjunction must have at most 64 clauses");
4347 } else if (clause < 1) {
4348 return xstrdup("clause index must be positive");
4349 } else if (clause > n_clauses) {
4350 return xstrdup("clause index must be less than or equal to "
4351 "number of clauses");
4354 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4358 /* Action structure for NXAST_MULTIPATH.
4360 * This action performs the following steps in sequence:
4362 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4363 * Refer to the definition of "enum nx_mp_fields" for details.
4365 * The 'basis' value is used as a universal hash parameter, that is,
4366 * different values of 'basis' yield different hash functions. The
4367 * particular universal hash function used is implementation-defined.
4369 * The hashed fields' values are drawn from the current state of the
4370 * flow, including all modifications that have been made by actions up to
4373 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4374 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4377 * The output of the algorithm is 'link', an unsigned integer less than
4378 * or equal to 'max_link'.
4380 * Some algorithms use 'arg' as an additional argument.
4382 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4383 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4386 * The switch will reject actions that have an unknown 'fields', or an unknown
4387 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4388 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4389 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4391 struct nx_action_multipath {
4392 ovs_be16 type; /* OFPAT_VENDOR. */
4393 ovs_be16 len; /* Length is 32. */
4394 ovs_be32 vendor; /* NX_VENDOR_ID. */
4395 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4397 /* What fields to hash and how. */
4398 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4399 ovs_be16 basis; /* Universal hash parameter. */
4402 /* Multipath link choice algorithm to apply to hash value. */
4403 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4404 ovs_be16 max_link; /* Number of output links, minus 1. */
4405 ovs_be32 arg; /* Algorithm-specific argument. */
4408 /* Where to store the result. */
4409 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4410 ovs_be32 dst; /* Destination. */
4412 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4415 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4416 enum ofp_version ofp_version OVS_UNUSED,
4419 uint32_t n_links = ntohs(nam->max_link) + 1;
4420 size_t min_n_bits = log_2_ceil(n_links);
4421 struct ofpact_multipath *mp;
4423 mp = ofpact_put_MULTIPATH(out);
4424 mp->fields = ntohs(nam->fields);
4425 mp->basis = ntohs(nam->basis);
4426 mp->algorithm = ntohs(nam->algorithm);
4427 mp->max_link = ntohs(nam->max_link);
4428 mp->arg = ntohl(nam->arg);
4429 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4430 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4431 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4433 if (!flow_hash_fields_valid(mp->fields)) {
4434 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4435 return OFPERR_OFPBAC_BAD_ARGUMENT;
4436 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4437 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4438 && mp->algorithm != NX_MP_ALG_HRW
4439 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4440 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4441 return OFPERR_OFPBAC_BAD_ARGUMENT;
4442 } else if (mp->dst.n_bits < min_n_bits) {
4443 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4444 "%"PRIu32" links", min_n_bits, n_links);
4445 return OFPERR_OFPBAC_BAD_ARGUMENT;
4448 return multipath_check(mp, NULL);
4452 encode_MULTIPATH(const struct ofpact_multipath *mp,
4453 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4455 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4457 nam->fields = htons(mp->fields);
4458 nam->basis = htons(mp->basis);
4459 nam->algorithm = htons(mp->algorithm);
4460 nam->max_link = htons(mp->max_link);
4461 nam->arg = htonl(mp->arg);
4462 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4463 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4466 static char * OVS_WARN_UNUSED_RESULT
4467 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4468 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4470 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4474 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4476 multipath_format(a, s);
4479 /* Action structure for NXAST_NOTE.
4481 * This action has no effect. It is variable length. The switch does not
4482 * attempt to interpret the user-defined 'note' data in any way. A controller
4483 * can use this action to attach arbitrary metadata to a flow.
4485 * This action might go away in the future.
4487 struct nx_action_note {
4488 ovs_be16 type; /* OFPAT_VENDOR. */
4489 ovs_be16 len; /* A multiple of 8, but at least 16. */
4490 ovs_be32 vendor; /* NX_VENDOR_ID. */
4491 ovs_be16 subtype; /* NXAST_NOTE. */
4492 uint8_t note[6]; /* Start of user-defined data. */
4493 /* Possibly followed by additional user-defined data. */
4495 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4498 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4499 enum ofp_version ofp_version OVS_UNUSED,
4502 struct ofpact_note *note;
4503 unsigned int length;
4505 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4506 note = ofpact_put_NOTE(out);
4507 note->length = length;
4508 ofpbuf_put(out, nan->note, length);
4509 ofpact_finish(out, out->header);
4515 encode_NOTE(const struct ofpact_note *note,
4516 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4518 size_t start_ofs = out->size;
4519 struct nx_action_note *nan;
4521 put_NXAST_NOTE(out);
4522 out->size = out->size - sizeof nan->note;
4524 ofpbuf_put(out, note->data, note->length);
4525 pad_ofpat(out, start_ofs);
4528 static char * OVS_WARN_UNUSED_RESULT
4529 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4530 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4532 size_t start_ofs = ofpacts->size;
4533 ofpact_put_NOTE(ofpacts);
4534 arg = ofpbuf_put_hex(ofpacts, arg, NULL);
4536 return xstrdup("bad hex digit in `note' argument");
4538 struct ofpact_note *note = ofpbuf_at_assert(ofpacts, start_ofs,
4540 note->length = ofpacts->size - (start_ofs + sizeof *note);
4541 ofpact_finish(ofpacts, ¬e->ofpact);
4546 format_NOTE(const struct ofpact_note *a, struct ds *s)
4548 ds_put_cstr(s, "note:");
4549 format_hex_arg(s, a->data, a->length);
4555 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4557 ofpact_put_EXIT(out);
4562 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4563 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4565 put_NXAST_EXIT(out);
4568 static char * OVS_WARN_UNUSED_RESULT
4569 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4570 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4572 ofpact_put_EXIT(ofpacts);
4577 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4579 ds_put_cstr(s, "exit");
4582 /* Unroll xlate action. */
4585 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4586 enum ofp_version ofp_version OVS_UNUSED,
4587 struct ofpbuf *out OVS_UNUSED)
4592 static char * OVS_WARN_UNUSED_RESULT
4593 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4594 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4601 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a, struct ds *s)
4603 ds_put_format(s, "unroll_xlate(table=%"PRIu8", cookie=%"PRIu64")",
4604 a->rule_table_id, ntohll(a->rule_cookie));
4607 /* Action structure for NXAST_SAMPLE.
4609 * Samples matching packets with the given probability and sends them
4610 * each to the set of collectors identified with the given ID. The
4611 * probability is expressed as a number of packets to be sampled out
4612 * of USHRT_MAX packets, and must be >0.
4614 * When sending packet samples to IPFIX collectors, the IPFIX flow
4615 * record sent for each sampled packet is associated with the given
4616 * observation domain ID and observation point ID. Each IPFIX flow
4617 * record contain the sampled packet's headers when executing this
4618 * rule. If a sampled packet's headers are modified by previous
4619 * actions in the flow, those modified headers are sent. */
4620 struct nx_action_sample {
4621 ovs_be16 type; /* OFPAT_VENDOR. */
4622 ovs_be16 len; /* Length is 24. */
4623 ovs_be32 vendor; /* NX_VENDOR_ID. */
4624 ovs_be16 subtype; /* NXAST_SAMPLE. */
4625 ovs_be16 probability; /* Fraction of packets to sample. */
4626 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4627 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4628 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4630 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4633 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4634 enum ofp_version ofp_version OVS_UNUSED,
4637 struct ofpact_sample *sample;
4639 sample = ofpact_put_SAMPLE(out);
4640 sample->probability = ntohs(nas->probability);
4641 sample->collector_set_id = ntohl(nas->collector_set_id);
4642 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4643 sample->obs_point_id = ntohl(nas->obs_point_id);
4645 if (sample->probability == 0) {
4646 return OFPERR_OFPBAC_BAD_ARGUMENT;
4653 encode_SAMPLE(const struct ofpact_sample *sample,
4654 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4656 struct nx_action_sample *nas;
4658 nas = put_NXAST_SAMPLE(out);
4659 nas->probability = htons(sample->probability);
4660 nas->collector_set_id = htonl(sample->collector_set_id);
4661 nas->obs_domain_id = htonl(sample->obs_domain_id);
4662 nas->obs_point_id = htonl(sample->obs_point_id);
4665 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4666 * action to 'ofpacts'.
4668 * Returns NULL if successful, otherwise a malloc()'d string describing the
4669 * error. The caller is responsible for freeing the returned string. */
4670 static char * OVS_WARN_UNUSED_RESULT
4671 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4672 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4674 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4677 while (ofputil_parse_key_value(&arg, &key, &value)) {
4680 if (!strcmp(key, "probability")) {
4681 error = str_to_u16(value, "probability", &os->probability);
4682 if (!error && os->probability == 0) {
4683 error = xasprintf("invalid probability value \"%s\"", value);
4685 } else if (!strcmp(key, "collector_set_id")) {
4686 error = str_to_u32(value, &os->collector_set_id);
4687 } else if (!strcmp(key, "obs_domain_id")) {
4688 error = str_to_u32(value, &os->obs_domain_id);
4689 } else if (!strcmp(key, "obs_point_id")) {
4690 error = str_to_u32(value, &os->obs_point_id);
4692 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4699 if (os->probability == 0) {
4700 return xstrdup("non-zero \"probability\" must be specified on sample");
4706 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4708 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4709 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4710 a->probability, a->collector_set_id,
4711 a->obs_domain_id, a->obs_point_id);
4714 /* debug_recirc instruction. */
4716 static bool enable_debug;
4719 ofpact_dummy_enable(void)
4721 enable_debug = true;
4725 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4727 if (!enable_debug) {
4728 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4731 ofpact_put_DEBUG_RECIRC(out);
4736 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4737 enum ofp_version ofp_version OVS_UNUSED,
4740 put_NXAST_DEBUG_RECIRC(out);
4743 static char * OVS_WARN_UNUSED_RESULT
4744 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4745 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4747 ofpact_put_DEBUG_RECIRC(ofpacts);
4752 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4754 ds_put_cstr(s, "debug_recirc");
4757 /* Action structure for NXAST_CT.
4759 * Pass traffic to the connection tracker.
4761 * There are two important concepts to understanding the connection tracking
4762 * interface: Packet state and Connection state. Packets may be "Untracked" or
4763 * "Tracked". Connections may be "Uncommitted" or "Committed".
4767 * Untracked packets have not yet passed through the connection tracker,
4768 * and the connection state for such packets is unknown. In most cases,
4769 * packets entering the OpenFlow pipeline will initially be in the
4770 * untracked state. Untracked packets may become tracked by executing
4771 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4772 * about the connection available, in particular the connection state.
4774 * Tracked packets have previously passed through the connection tracker.
4775 * These packets will remain tracked through until the end of the OpenFlow
4776 * pipeline. Tracked packets which have NXAST_CT executed with a
4777 * "recirc_table" specified will return to the tracked state.
4779 * The packet state is only significant for the duration of packet
4780 * processing within the OpenFlow pipeline.
4782 * - Connection State:
4784 * Multiple packets may be associated with a single connection. Initially,
4785 * all connections are uncommitted. The connection state corresponding to
4786 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4788 * Uncommitted connections have no state stored about them. Uncommitted
4789 * connections may transition into the committed state by executing
4790 * NXAST_CT with the NX_CT_F_COMMIT flag.
4792 * Once a connection becomes committed, information may be gathered about
4793 * the connection by passing subsequent packets through the connection
4794 * tracker, and the state of the connection will be stored beyond the
4795 * lifetime of packet processing.
4797 * Connections may transition back into the uncommitted state due to
4798 * external timers, or due to the contents of packets that are sent to the
4799 * connection tracker. This behaviour is outside of the scope of the
4800 * OpenFlow interface.
4802 * The "zone" specifies a context within which the tracking is done:
4804 * The connection tracking zone is a 16-bit number. Each zone is an
4805 * independent connection tracking context. The connection state for each
4806 * connection is completely separate for each zone, so if a connection
4807 * is committed to zone A, then it will remain uncommitted in zone B.
4808 * If NXAST_CT is executed with the same zone multiple times, later
4809 * executions have no effect.
4811 * If 'zone_src' is nonzero, this specifies that the zone should be
4812 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4813 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4814 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4815 * are the same as the acceptable nxm_header values for the 'src' field of
4818 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4819 * connection tracking zone.
4821 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4823 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4824 * packet will be logically cloned prior to executing this action. One
4825 * copy will be sent to the connection tracker, then will be re-injected
4826 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
4827 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4828 * fields will be populated. The original instance of the packet will
4829 * continue the current actions list. This can be thought of as similar to
4830 * the effect of the "output" action: One copy is sent out (in this case,
4831 * to the connection tracker), but the current copy continues processing.
4833 * It is strongly recommended that this table is later than the current
4834 * table, to prevent loops.
4836 * The "alg" attaches protocol-specific behaviour to this action:
4838 * The ALG is a 16-bit number which specifies that additional
4839 * processing should be applied to this traffic.
4841 * Protocol | Value | Meaning
4842 * --------------------------------------------------------------------
4843 * None | 0 | No protocol-specific behaviour.
4844 * FTP | 21 | Parse FTP control connections and observe the
4845 * | | negotiation of related data connections.
4846 * Other | Other | Unsupported protocols.
4848 * By way of example, if FTP control connections have this action applied
4849 * with the ALG set to FTP (21), then the connection tracker will observe
4850 * the negotiation of data connections. This allows the connection
4851 * tracker to identify subsequent data connections as "related" to this
4852 * existing connection. The "related" flag will be populated in the
4853 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
4856 * Zero or more actions may immediately follow this action. These actions will
4857 * be executed within the context of the connection tracker, and they require
4858 * the NX_CT_F_COMMIT flag to be set.
4860 struct nx_action_conntrack {
4861 ovs_be16 type; /* OFPAT_VENDOR. */
4862 ovs_be16 len; /* At least 24. */
4863 ovs_be32 vendor; /* NX_VENDOR_ID. */
4864 ovs_be16 subtype; /* NXAST_CT. */
4865 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
4866 * Unspecified flag bits must be zero. */
4867 ovs_be32 zone_src; /* Connection tracking context. */
4869 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
4870 ovs_be16 zone_imm; /* Immediate value for zone. */
4872 uint8_t recirc_table; /* Recirculate to a specific table, or
4873 NX_CT_RECIRC_NONE for no recirculation. */
4874 uint8_t pad[3]; /* Zeroes */
4875 ovs_be16 alg; /* Well-known port number for the protocol.
4876 * 0 indicates no ALG is required. */
4877 /* Followed by a sequence of zero or more OpenFlow actions. The length of
4878 * these is included in 'len'. */
4880 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
4883 decode_ct_zone(const struct nx_action_conntrack *nac,
4884 struct ofpact_conntrack *out)
4886 if (nac->zone_src) {
4889 out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
4890 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
4891 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
4892 error = mf_check_src(&out->zone_src, NULL);
4897 if (out->zone_src.n_bits != 16) {
4898 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
4899 out->zone_src.n_bits);
4900 return OFPERR_OFPBAC_BAD_SET_LEN;
4903 out->zone_src.field = NULL;
4904 out->zone_imm = ntohs(nac->zone_imm);
4911 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
4912 enum ofp_version ofp_version, struct ofpbuf *out)
4914 const size_t ct_offset = ofpacts_pull(out);
4915 struct ofpact_conntrack *conntrack = ofpact_put_CT(out);
4916 conntrack->flags = ntohs(nac->flags);
4918 int error = decode_ct_zone(nac, conntrack);
4922 conntrack->recirc_table = nac->recirc_table;
4923 conntrack->alg = ntohs(nac->alg);
4925 ofpbuf_pull(out, sizeof(*conntrack));
4927 struct ofpbuf openflow = ofpbuf_const_initializer(
4928 nac + 1, ntohs(nac->len) - sizeof(*nac));
4929 error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
4931 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4937 conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
4938 out->header = &conntrack->ofpact;
4939 ofpact_finish(out, &conntrack->ofpact);
4941 if (conntrack->ofpact.len > sizeof(*conntrack)
4942 && !(conntrack->flags & NX_CT_F_COMMIT)) {
4943 const struct ofpact *a;
4944 size_t ofpacts_len = conntrack->ofpact.len - sizeof(*conntrack);
4946 OFPACT_FOR_EACH (a, conntrack->actions, ofpacts_len) {
4947 if (a->type != OFPACT_NAT || ofpact_get_NAT(a)->flags
4948 || ofpact_get_NAT(a)->range_af != AF_UNSPEC) {
4949 VLOG_WARN_RL(&rl, "CT action requires commit flag if actions "
4950 "other than NAT without arguments are specified.");
4951 error = OFPERR_OFPBAC_BAD_ARGUMENT;
4958 ofpbuf_push_uninit(out, ct_offset);
4963 encode_CT(const struct ofpact_conntrack *conntrack,
4964 enum ofp_version ofp_version, struct ofpbuf *out)
4966 struct nx_action_conntrack *nac;
4967 const size_t ofs = out->size;
4970 nac = put_NXAST_CT(out);
4971 nac->flags = htons(conntrack->flags);
4972 if (conntrack->zone_src.field) {
4973 nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
4974 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
4975 conntrack->zone_src.n_bits);
4977 nac->zone_src = htonl(0);
4978 nac->zone_imm = htons(conntrack->zone_imm);
4980 nac->recirc_table = conntrack->recirc_table;
4981 nac->alg = htons(conntrack->alg);
4983 len = ofpacts_put_openflow_actions(conntrack->actions,
4984 ofpact_ct_get_action_len(conntrack),
4986 len += sizeof(*nac);
4987 nac = ofpbuf_at(out, ofs, sizeof(*nac));
4988 nac->len = htons(len);
4991 static char * OVS_WARN_UNUSED_RESULT parse_NAT(char *arg, struct ofpbuf *,
4992 enum ofputil_protocol * OVS_UNUSED);
4994 /* Parses 'arg' as the argument to a "ct" action, and appends such an
4995 * action to 'ofpacts'.
4997 * Returns NULL if successful, otherwise a malloc()'d string describing the
4998 * error. The caller is responsible for freeing the returned string. */
4999 static char * OVS_WARN_UNUSED_RESULT
5000 parse_CT(char *arg, struct ofpbuf *ofpacts,
5001 enum ofputil_protocol *usable_protocols)
5003 const size_t ct_offset = ofpacts_pull(ofpacts);
5004 struct ofpact_conntrack *oc;
5008 oc = ofpact_put_CT(ofpacts);
5010 oc->recirc_table = NX_CT_RECIRC_NONE;
5011 while (ofputil_parse_key_value(&arg, &key, &value)) {
5012 if (!strcmp(key, "commit")) {
5013 oc->flags |= NX_CT_F_COMMIT;
5014 } else if (!strcmp(key, "table")) {
5015 error = str_to_u8(value, "recirc_table", &oc->recirc_table);
5016 if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
5017 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
5019 } else if (!strcmp(key, "zone")) {
5020 error = str_to_u16(value, "zone", &oc->zone_imm);
5024 error = mf_parse_subfield(&oc->zone_src, value);
5029 } else if (!strcmp(key, "alg")) {
5030 error = str_to_connhelper(value, &oc->alg);
5031 } else if (!strcmp(key, "nat")) {
5032 const size_t nat_offset = ofpacts_pull(ofpacts);
5034 error = parse_NAT(value, ofpacts, usable_protocols);
5035 /* Update CT action pointer and length. */
5036 ofpacts->header = ofpbuf_push_uninit(ofpacts, nat_offset);
5037 oc = ofpacts->header;
5038 } else if (!strcmp(key, "exec")) {
5039 /* Hide existing actions from ofpacts_parse_copy(), so the
5040 * nesting can be handled transparently. */
5041 enum ofputil_protocol usable_protocols2;
5042 const size_t exec_offset = ofpacts_pull(ofpacts);
5044 /* Initializes 'usable_protocol2', fold it back to
5045 * '*usable_protocols' afterwards, so that we do not lose
5046 * restrictions already in there. */
5047 error = ofpacts_parse_copy(value, ofpacts, &usable_protocols2,
5049 *usable_protocols &= usable_protocols2;
5050 ofpacts->header = ofpbuf_push_uninit(ofpacts, exec_offset);
5051 oc = ofpacts->header;
5053 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
5060 ofpact_finish(ofpacts, &oc->ofpact);
5061 ofpbuf_push_uninit(ofpacts, ct_offset);
5066 format_alg(int port, struct ds *s)
5068 if (port == IPPORT_FTP) {
5069 ds_put_format(s, "alg=ftp,");
5071 ds_put_format(s, "alg=%d,", port);
5075 static void format_NAT(const struct ofpact_nat *a, struct ds *ds);
5078 format_CT(const struct ofpact_conntrack *a, struct ds *s)
5080 ds_put_cstr(s, "ct(");
5081 if (a->flags & NX_CT_F_COMMIT) {
5082 ds_put_cstr(s, "commit,");
5084 if (a->recirc_table != NX_CT_RECIRC_NONE) {
5085 ds_put_format(s, "table=%"PRIu8",", a->recirc_table);
5087 if (a->zone_src.field) {
5088 ds_put_format(s, "zone=");
5089 mf_format_subfield(&a->zone_src, s);
5090 ds_put_char(s, ',');
5091 } else if (a->zone_imm) {
5092 ds_put_format(s, "zone=%"PRIu16",", a->zone_imm);
5094 /* If the first action is a NAT action, format it outside of the 'exec'
5096 const struct ofpact *action = a->actions;
5097 size_t actions_len = ofpact_ct_get_action_len(a);
5098 if (actions_len && action->type == OFPACT_NAT) {
5099 format_NAT(ofpact_get_NAT(action), s);
5100 ds_put_char(s, ',');
5101 actions_len -= OFPACT_ALIGN(action->len);
5102 action = ofpact_next(action);
5105 ds_put_cstr(s, "exec(");
5106 ofpacts_format(action, actions_len, s);
5107 ds_put_cstr(s, "),");
5109 format_alg(a->alg, s);
5111 ds_put_char(s, ')');
5116 /* Which optional fields are present? */
5118 NX_NAT_RANGE_IPV4_MIN = 1 << 0, /* ovs_be32 */
5119 NX_NAT_RANGE_IPV4_MAX = 1 << 1, /* ovs_be32 */
5120 NX_NAT_RANGE_IPV6_MIN = 1 << 2, /* struct in6_addr */
5121 NX_NAT_RANGE_IPV6_MAX = 1 << 3, /* struct in6_addr */
5122 NX_NAT_RANGE_PROTO_MIN = 1 << 4, /* ovs_be16 */
5123 NX_NAT_RANGE_PROTO_MAX = 1 << 5, /* ovs_be16 */
5126 /* Action structure for NXAST_NAT. */
5127 struct nx_action_nat {
5128 ovs_be16 type; /* OFPAT_VENDOR. */
5129 ovs_be16 len; /* At least 16. */
5130 ovs_be32 vendor; /* NX_VENDOR_ID. */
5131 ovs_be16 subtype; /* NXAST_NAT. */
5132 uint8_t pad[2]; /* Must be zero. */
5133 ovs_be16 flags; /* Zero or more NX_NAT_F_* flags.
5134 * Unspecified flag bits must be zero. */
5135 ovs_be16 range_present; /* NX_NAT_RANGE_* */
5136 /* Followed by optional parameters as specified by 'range_present' */
5138 OFP_ASSERT(sizeof(struct nx_action_nat) == 16);
5141 encode_NAT(const struct ofpact_nat *nat,
5142 enum ofp_version ofp_version OVS_UNUSED,
5145 struct nx_action_nat *nan;
5146 const size_t ofs = out->size;
5147 uint16_t range_present = 0;
5149 nan = put_NXAST_NAT(out);
5150 nan->flags = htons(nat->flags);
5151 if (nat->range_af == AF_INET) {
5152 if (nat->range.addr.ipv4.min) {
5153 ovs_be32 *min = ofpbuf_put_uninit(out, sizeof *min);
5154 *min = nat->range.addr.ipv4.min;
5155 range_present |= NX_NAT_RANGE_IPV4_MIN;
5157 if (nat->range.addr.ipv4.max) {
5158 ovs_be32 *max = ofpbuf_put_uninit(out, sizeof *max);
5159 *max = nat->range.addr.ipv4.max;
5160 range_present |= NX_NAT_RANGE_IPV4_MAX;
5162 } else if (nat->range_af == AF_INET6) {
5163 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5164 struct in6_addr *min = ofpbuf_put_uninit(out, sizeof *min);
5165 *min = nat->range.addr.ipv6.min;
5166 range_present |= NX_NAT_RANGE_IPV6_MIN;
5168 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.max)) {
5169 struct in6_addr *max = ofpbuf_put_uninit(out, sizeof *max);
5170 *max = nat->range.addr.ipv6.max;
5171 range_present |= NX_NAT_RANGE_IPV6_MAX;
5174 if (nat->range_af != AF_UNSPEC) {
5175 if (nat->range.proto.min) {
5176 ovs_be16 *min = ofpbuf_put_uninit(out, sizeof *min);
5177 *min = htons(nat->range.proto.min);
5178 range_present |= NX_NAT_RANGE_PROTO_MIN;
5180 if (nat->range.proto.max) {
5181 ovs_be16 *max = ofpbuf_put_uninit(out, sizeof *max);
5182 *max = htons(nat->range.proto.max);
5183 range_present |= NX_NAT_RANGE_PROTO_MAX;
5186 pad_ofpat(out, ofs);
5187 nan = ofpbuf_at(out, ofs, sizeof *nan);
5188 nan->range_present = htons(range_present);
5192 decode_NXAST_RAW_NAT(const struct nx_action_nat *nan,
5193 enum ofp_version ofp_version OVS_UNUSED,
5196 struct ofpact_nat *nat;
5197 uint16_t range_present = ntohs(nan->range_present);
5198 const char *opts = (char *)(nan + 1);
5199 uint16_t len = ntohs(nan->len) - sizeof *nan;
5201 nat = ofpact_put_NAT(out);
5202 nat->flags = ntohs(nan->flags);
5204 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
5205 (LEN >= sizeof(TYPE) \
5206 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
5207 SRC += sizeof(TYPE)) \
5210 nat->range_af = AF_UNSPEC;
5211 if (range_present & NX_NAT_RANGE_IPV4_MIN) {
5212 if (range_present & (NX_NAT_RANGE_IPV6_MIN | NX_NAT_RANGE_IPV6_MAX)) {
5213 return OFPERR_OFPBAC_BAD_ARGUMENT;
5216 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.min, opts, len, ovs_be32)
5217 || !nat->range.addr.ipv4.min) {
5218 return OFPERR_OFPBAC_BAD_ARGUMENT;
5221 nat->range_af = AF_INET;
5223 if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5224 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.max, opts, len,
5226 return OFPERR_OFPBAC_BAD_ARGUMENT;
5228 if (ntohl(nat->range.addr.ipv4.max)
5229 < ntohl(nat->range.addr.ipv4.min)) {
5230 return OFPERR_OFPBAC_BAD_ARGUMENT;
5233 } else if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5234 return OFPERR_OFPBAC_BAD_ARGUMENT;
5235 } else if (range_present & NX_NAT_RANGE_IPV6_MIN) {
5236 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.min, opts, len,
5238 || ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5239 return OFPERR_OFPBAC_BAD_ARGUMENT;
5242 nat->range_af = AF_INET6;
5244 if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5245 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.max, opts, len,
5247 return OFPERR_OFPBAC_BAD_ARGUMENT;
5249 if (memcmp(&nat->range.addr.ipv6.max, &nat->range.addr.ipv6.min,
5250 sizeof(struct in6_addr)) < 0) {
5251 return OFPERR_OFPBAC_BAD_ARGUMENT;
5254 } else if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5255 return OFPERR_OFPBAC_BAD_ARGUMENT;
5258 if (range_present & NX_NAT_RANGE_PROTO_MIN) {
5261 if (nat->range_af == AF_UNSPEC) {
5262 return OFPERR_OFPBAC_BAD_ARGUMENT;
5264 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16) || proto == 0) {
5265 return OFPERR_OFPBAC_BAD_ARGUMENT;
5267 nat->range.proto.min = ntohs(proto);
5268 if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5269 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16)) {
5270 return OFPERR_OFPBAC_BAD_ARGUMENT;
5272 nat->range.proto.max = ntohs(proto);
5273 if (nat->range.proto.max < nat->range.proto.min) {
5274 return OFPERR_OFPBAC_BAD_ARGUMENT;
5277 } else if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5278 return OFPERR_OFPBAC_BAD_ARGUMENT;
5285 format_NAT(const struct ofpact_nat *a, struct ds *ds)
5287 ds_put_cstr(ds, "nat");
5289 if (a->flags & (NX_NAT_F_SRC | NX_NAT_F_DST)) {
5290 ds_put_char(ds, '(');
5291 ds_put_cstr(ds, a->flags & NX_NAT_F_SRC ? "src" : "dst");
5293 if (a->range_af != AF_UNSPEC) {
5294 ds_put_cstr(ds, "=");
5296 if (a->range_af == AF_INET) {
5297 ds_put_format(ds, IP_FMT, IP_ARGS(a->range.addr.ipv4.min));
5299 if (a->range.addr.ipv4.max
5300 && a->range.addr.ipv4.max != a->range.addr.ipv4.min) {
5301 ds_put_format(ds, "-"IP_FMT,
5302 IP_ARGS(a->range.addr.ipv4.max));
5304 } else if (a->range_af == AF_INET6) {
5305 ipv6_format_addr_bracket(&a->range.addr.ipv6.min, ds,
5306 a->range.proto.min);
5308 if (!ipv6_mask_is_any(&a->range.addr.ipv6.max)
5309 && memcmp(&a->range.addr.ipv6.max, &a->range.addr.ipv6.min,
5310 sizeof(struct in6_addr)) != 0) {
5311 ds_put_char(ds, '-');
5312 ipv6_format_addr_bracket(&a->range.addr.ipv6.max, ds,
5313 a->range.proto.min);
5316 if (a->range.proto.min) {
5317 ds_put_char(ds, ':');
5318 ds_put_format(ds, "%"PRIu16, a->range.proto.min);
5320 if (a->range.proto.max
5321 && a->range.proto.max != a->range.proto.min) {
5322 ds_put_format(ds, "-%"PRIu16, a->range.proto.max);
5325 ds_put_char(ds, ',');
5327 if (a->flags & NX_NAT_F_PERSISTENT) {
5328 ds_put_cstr(ds, "persistent,");
5330 if (a->flags & NX_NAT_F_PROTO_HASH) {
5331 ds_put_cstr(ds, "hash,");
5333 if (a->flags & NX_NAT_F_PROTO_RANDOM) {
5334 ds_put_cstr(ds, "random,");
5338 ds_put_char(ds, ')');
5342 static char * OVS_WARN_UNUSED_RESULT
5343 str_to_nat_range(const char *s, struct ofpact_nat *on)
5345 char ipv6_s[IPV6_SCAN_LEN + 1];
5348 on->range_af = AF_UNSPEC;
5349 if (ovs_scan_len(s, &n, IP_SCAN_FMT,
5350 IP_SCAN_ARGS(&on->range.addr.ipv4.min))) {
5351 on->range_af = AF_INET;
5355 if (!ovs_scan_len(s, &n, IP_SCAN_FMT,
5356 IP_SCAN_ARGS(&on->range.addr.ipv4.max))
5357 || (ntohl(on->range.addr.ipv4.max)
5358 < ntohl(on->range.addr.ipv4.min))) {
5362 } else if ((ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5363 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5364 && inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.min) == 1) {
5365 on->range_af = AF_INET6;
5369 if (!(ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5370 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5371 || inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.max) != 1
5372 || memcmp(&on->range.addr.ipv6.max, &on->range.addr.ipv6.min,
5373 sizeof on->range.addr.ipv6.max) < 0) {
5378 if (on->range_af != AF_UNSPEC && s[n] == ':') {
5380 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.min)) {
5385 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.max)
5386 || on->range.proto.max < on->range.proto.min) {
5391 if (strlen(s) != n) {
5392 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
5397 return xasprintf("invalid nat range \"%s\"", s);
5401 /* Parses 'arg' as the argument to a "nat" action, and appends such an
5402 * action to 'ofpacts'.
5404 * Returns NULL if successful, otherwise a malloc()'d string describing the
5405 * error. The caller is responsible for freeing the returned string. */
5406 static char * OVS_WARN_UNUSED_RESULT
5407 parse_NAT(char *arg, struct ofpbuf *ofpacts,
5408 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5410 struct ofpact_nat *on = ofpact_put_NAT(ofpacts);
5414 on->range_af = AF_UNSPEC;
5416 while (ofputil_parse_key_value(&arg, &key, &value)) {
5419 if (!strcmp(key, "src")) {
5420 on->flags |= NX_NAT_F_SRC;
5421 error = str_to_nat_range(value, on);
5422 } else if (!strcmp(key, "dst")) {
5423 on->flags |= NX_NAT_F_DST;
5424 error = str_to_nat_range(value, on);
5425 } else if (!strcmp(key, "persistent")) {
5426 on->flags |= NX_NAT_F_PERSISTENT;
5427 } else if (!strcmp(key, "hash")) {
5428 on->flags |= NX_NAT_F_PROTO_HASH;
5429 } else if (!strcmp(key, "random")) {
5430 on->flags |= NX_NAT_F_PROTO_RANDOM;
5432 error = xasprintf("invalid key \"%s\" in \"nat\" argument",
5439 if (on->flags & NX_NAT_F_SRC && on->flags & NX_NAT_F_DST) {
5440 return xasprintf("May only specify one of \"snat\" or \"dnat\".");
5442 if (!(on->flags & NX_NAT_F_SRC || on->flags & NX_NAT_F_DST)) {
5444 return xasprintf("Flags allowed only with \"snat\" or \"dnat\".");
5446 if (on->range_af != AF_UNSPEC) {
5447 return xasprintf("Range allowed only with \"snat\" or \"dnat\".");
5454 /* Meter instruction. */
5457 encode_METER(const struct ofpact_meter *meter,
5458 enum ofp_version ofp_version, struct ofpbuf *out)
5460 if (ofp_version >= OFP13_VERSION) {
5461 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
5465 static char * OVS_WARN_UNUSED_RESULT
5466 parse_METER(char *arg, struct ofpbuf *ofpacts,
5467 enum ofputil_protocol *usable_protocols)
5469 *usable_protocols &= OFPUTIL_P_OF13_UP;
5470 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
5474 format_METER(const struct ofpact_meter *a, struct ds *s)
5476 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
5479 /* Clear-Actions instruction. */
5482 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
5483 enum ofp_version ofp_version OVS_UNUSED,
5484 struct ofpbuf *out OVS_UNUSED)
5486 if (ofp_version > OFP10_VERSION) {
5487 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
5491 static char * OVS_WARN_UNUSED_RESULT
5492 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
5493 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5495 ofpact_put_CLEAR_ACTIONS(ofpacts);
5500 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
5502 ds_put_cstr(s, "clear_actions");
5505 /* Write-Actions instruction. */
5508 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
5509 enum ofp_version ofp_version, struct ofpbuf *out)
5511 if (ofp_version > OFP10_VERSION) {
5512 const size_t ofs = out->size;
5514 instruction_put_OFPIT11_WRITE_ACTIONS(out);
5515 ofpacts_put_openflow_actions(actions->actions,
5516 ofpact_nest_get_action_len(actions),
5518 ofpacts_update_instruction_actions(out, ofs);
5522 static char * OVS_WARN_UNUSED_RESULT
5523 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
5524 enum ofputil_protocol *usable_protocols)
5526 size_t ofs = ofpacts_pull(ofpacts);
5527 struct ofpact_nest *on;
5530 /* Add a Write-Actions instruction and then pull it off. */
5531 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
5532 ofpbuf_pull(ofpacts, sizeof *on);
5534 /* Parse nested actions.
5536 * We pulled off "write-actions" and the previous actions because the
5537 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
5538 * that it doesn't actually include the nested actions. That means that
5539 * ofpacts_parse() would reject them as being part of an Apply-Actions that
5540 * follows a Write-Actions, which is an invalid order. */
5541 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
5542 OFPACT_WRITE_ACTIONS);
5544 /* Put the Write-Actions back on and update its length. */
5545 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
5546 on->ofpact.len = ofpacts->size;
5548 /* Put any previous actions or instructions back on. */
5549 ofpbuf_push_uninit(ofpacts, ofs);
5555 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
5557 ds_put_cstr(s, "write_actions(");
5558 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
5559 ds_put_char(s, ')');
5562 /* Action structure for NXAST_WRITE_METADATA.
5564 * Modifies the 'mask' bits of the metadata value. */
5565 struct nx_action_write_metadata {
5566 ovs_be16 type; /* OFPAT_VENDOR. */
5567 ovs_be16 len; /* Length is 32. */
5568 ovs_be32 vendor; /* NX_VENDOR_ID. */
5569 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
5570 uint8_t zeros[6]; /* Must be zero. */
5571 ovs_be64 metadata; /* Metadata register. */
5572 ovs_be64 mask; /* Metadata mask. */
5574 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
5577 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
5578 enum ofp_version ofp_version OVS_UNUSED,
5581 struct ofpact_metadata *om;
5583 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
5584 return OFPERR_NXBRC_MUST_BE_ZERO;
5587 om = ofpact_put_WRITE_METADATA(out);
5588 om->metadata = nawm->metadata;
5589 om->mask = nawm->mask;
5595 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
5596 enum ofp_version ofp_version, struct ofpbuf *out)
5598 if (ofp_version == OFP10_VERSION) {
5599 struct nx_action_write_metadata *nawm;
5601 nawm = put_NXAST_WRITE_METADATA(out);
5602 nawm->metadata = metadata->metadata;
5603 nawm->mask = metadata->mask;
5605 struct ofp11_instruction_write_metadata *oiwm;
5607 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
5608 oiwm->metadata = metadata->metadata;
5609 oiwm->metadata_mask = metadata->mask;
5613 static char * OVS_WARN_UNUSED_RESULT
5614 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
5615 enum ofputil_protocol *usable_protocols)
5617 struct ofpact_metadata *om;
5618 char *mask = strchr(arg, '/');
5620 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
5622 om = ofpact_put_WRITE_METADATA(ofpacts);
5627 error = str_to_be64(mask + 1, &om->mask);
5632 om->mask = OVS_BE64_MAX;
5635 return str_to_be64(arg, &om->metadata);
5639 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5641 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
5642 if (a->mask != OVS_BE64_MAX) {
5643 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5647 /* Goto-Table instruction. */
5650 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5651 enum ofp_version ofp_version, struct ofpbuf *out)
5653 if (ofp_version == OFP10_VERSION) {
5654 struct nx_action_resubmit *nar;
5656 nar = put_NXAST_RESUBMIT_TABLE(out);
5657 nar->table = goto_table->table_id;
5658 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5660 struct ofp11_instruction_goto_table *oigt;
5662 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5663 oigt->table_id = goto_table->table_id;
5664 memset(oigt->pad, 0, sizeof oigt->pad);
5668 static char * OVS_WARN_UNUSED_RESULT
5669 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5670 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5672 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5673 char *table_s = strsep(&arg, ",");
5674 if (!table_s || !table_s[0]) {
5675 return xstrdup("instruction goto-table needs table id");
5677 return str_to_u8(table_s, "table", &ogt->table_id);
5681 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5683 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
5687 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
5688 const struct ofp_action_header *bad_action, enum ofperr error)
5690 if (!VLOG_DROP_WARN(&rl)) {
5694 ds_put_hex_dump(&s, actions, actions_len, 0, false);
5695 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5696 (char *)bad_action - (char *)actions,
5697 ofperr_get_name(error), ds_cstr(&s));
5703 ofpacts_decode(const void *actions, size_t actions_len,
5704 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5706 struct ofpbuf openflow = ofpbuf_const_initializer(actions, actions_len);
5707 while (openflow.size) {
5708 const struct ofp_action_header *action = openflow.data;
5709 enum ofp_raw_action_type raw;
5713 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5715 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
5719 log_bad_action(actions, actions_len, action, error);
5727 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5728 unsigned int actions_len,
5729 enum ofp_version version,
5730 uint32_t allowed_ovsinsts,
5731 struct ofpbuf *ofpacts,
5732 enum ofpact_type outer_action)
5734 const struct ofp_action_header *actions;
5735 size_t orig_size = ofpacts->size;
5738 if (actions_len % OFP_ACTION_ALIGN != 0) {
5739 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5740 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5741 return OFPERR_OFPBRC_BAD_LEN;
5744 actions = ofpbuf_try_pull(openflow, actions_len);
5745 if (actions == NULL) {
5746 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
5747 "remaining message length (%"PRIu32")",
5748 actions_len, openflow->size);
5749 return OFPERR_OFPBRC_BAD_LEN;
5752 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5754 ofpacts->size = orig_size;
5758 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
5761 ofpacts->size = orig_size;
5766 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
5767 * of 'openflow' into ofpacts. On success, appends the converted actions to
5768 * 'ofpacts'; on failure, 'ofpacts' is unchanged (but might be reallocated) .
5769 * Returns 0 if successful, otherwise an OpenFlow error.
5771 * Actions are processed according to their OpenFlow version which
5772 * is provided in the 'version' parameter.
5774 * In most places in OpenFlow, actions appear encapsulated in instructions, so
5775 * you should call ofpacts_pull_openflow_instructions() instead of this
5778 * The parsed actions are valid generically, but they may not be valid in a
5779 * specific context. For example, port numbers up to OFPP_MAX are valid
5780 * generically, but specific datapaths may only support port numbers in a
5781 * smaller range. Use ofpacts_check() to additional check whether actions are
5782 * valid in a specific context. */
5784 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
5785 unsigned int actions_len,
5786 enum ofp_version version,
5787 struct ofpbuf *ofpacts)
5789 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
5790 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
5794 /* OpenFlow 1.1 actions. */
5797 /* True if an action sets the value of a field
5798 * in a way that is compatibile with the action set.
5799 * The field can be set via either a set or a move action.
5800 * False otherwise. */
5802 ofpact_is_set_or_move_action(const struct ofpact *a)
5805 case OFPACT_SET_FIELD:
5806 case OFPACT_REG_MOVE:
5807 case OFPACT_SET_ETH_DST:
5808 case OFPACT_SET_ETH_SRC:
5809 case OFPACT_SET_IP_DSCP:
5810 case OFPACT_SET_IP_ECN:
5811 case OFPACT_SET_IP_TTL:
5812 case OFPACT_SET_IPV4_DST:
5813 case OFPACT_SET_IPV4_SRC:
5814 case OFPACT_SET_L4_DST_PORT:
5815 case OFPACT_SET_L4_SRC_PORT:
5816 case OFPACT_SET_MPLS_LABEL:
5817 case OFPACT_SET_MPLS_TC:
5818 case OFPACT_SET_MPLS_TTL:
5819 case OFPACT_SET_QUEUE:
5820 case OFPACT_SET_TUNNEL:
5821 case OFPACT_SET_VLAN_PCP:
5822 case OFPACT_SET_VLAN_VID:
5825 case OFPACT_CLEAR_ACTIONS:
5828 case OFPACT_CONTROLLER:
5829 case OFPACT_DEC_MPLS_TTL:
5830 case OFPACT_DEC_TTL:
5831 case OFPACT_ENQUEUE:
5833 case OFPACT_UNROLL_XLATE:
5834 case OFPACT_FIN_TIMEOUT:
5835 case OFPACT_GOTO_TABLE:
5838 case OFPACT_CONJUNCTION:
5840 case OFPACT_MULTIPATH:
5843 case OFPACT_OUTPUT_REG:
5844 case OFPACT_POP_MPLS:
5845 case OFPACT_POP_QUEUE:
5846 case OFPACT_PUSH_MPLS:
5847 case OFPACT_PUSH_VLAN:
5848 case OFPACT_RESUBMIT:
5850 case OFPACT_STACK_POP:
5851 case OFPACT_STACK_PUSH:
5852 case OFPACT_STRIP_VLAN:
5853 case OFPACT_WRITE_ACTIONS:
5854 case OFPACT_WRITE_METADATA:
5855 case OFPACT_DEBUG_RECIRC:
5862 /* True if an action is allowed in the action set.
5863 * False otherwise. */
5865 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
5868 case OFPACT_DEC_MPLS_TTL:
5869 case OFPACT_DEC_TTL:
5872 case OFPACT_POP_MPLS:
5873 case OFPACT_PUSH_MPLS:
5874 case OFPACT_PUSH_VLAN:
5875 case OFPACT_REG_MOVE:
5876 case OFPACT_SET_FIELD:
5877 case OFPACT_SET_ETH_DST:
5878 case OFPACT_SET_ETH_SRC:
5879 case OFPACT_SET_IP_DSCP:
5880 case OFPACT_SET_IP_ECN:
5881 case OFPACT_SET_IP_TTL:
5882 case OFPACT_SET_IPV4_DST:
5883 case OFPACT_SET_IPV4_SRC:
5884 case OFPACT_SET_L4_DST_PORT:
5885 case OFPACT_SET_L4_SRC_PORT:
5886 case OFPACT_SET_MPLS_LABEL:
5887 case OFPACT_SET_MPLS_TC:
5888 case OFPACT_SET_MPLS_TTL:
5889 case OFPACT_SET_QUEUE:
5890 case OFPACT_SET_TUNNEL:
5891 case OFPACT_SET_VLAN_PCP:
5892 case OFPACT_SET_VLAN_VID:
5893 case OFPACT_STRIP_VLAN:
5896 /* In general these actions are excluded because they are not part of
5897 * the OpenFlow specification nor map to actions that are defined in
5898 * the specification. Thus the order in which they should be applied
5899 * in the action set is undefined. */
5901 case OFPACT_CONTROLLER:
5904 case OFPACT_ENQUEUE:
5906 case OFPACT_UNROLL_XLATE:
5907 case OFPACT_FIN_TIMEOUT:
5909 case OFPACT_CONJUNCTION:
5910 case OFPACT_MULTIPATH:
5912 case OFPACT_OUTPUT_REG:
5913 case OFPACT_POP_QUEUE:
5914 case OFPACT_RESUBMIT:
5916 case OFPACT_STACK_POP:
5917 case OFPACT_STACK_PUSH:
5918 case OFPACT_DEBUG_RECIRC:
5920 /* The action set may only include actions and thus
5921 * may not include any instructions */
5922 case OFPACT_CLEAR_ACTIONS:
5923 case OFPACT_GOTO_TABLE:
5925 case OFPACT_WRITE_ACTIONS:
5926 case OFPACT_WRITE_METADATA:
5933 /* Append ofpact 'a' onto the tail of 'out' */
5935 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
5937 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
5940 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
5942 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
5943 enum ofpact_type filter)
5945 const struct ofpact *target;
5946 const struct ofpact *a;
5949 OFPACT_FOR_EACH (a, in->data, in->size) {
5950 if (a->type == filter) {
5955 ofpact_copy(out, target);
5957 return target != NULL;
5960 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5961 * The order of appended ofpacts is preserved between 'in' and 'out' */
5963 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5964 bool (*filter)(const struct ofpact *))
5966 const struct ofpact *a;
5968 OFPACT_FOR_EACH (a, in->data, in->size) {
5970 ofpact_copy(out, a);
5975 /* Reads 'action_set', which contains ofpacts accumulated by
5976 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5977 * executed directly into 'action_list'. (These names correspond to the
5978 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5980 * In general this involves appending the last instance of each action that is
5981 * admissible in the action set in the order described in the OpenFlow
5985 * + output action is only appended if no group action was present in 'in'.
5986 * + As a simplification all set actions are copied in the order the are
5987 * provided in 'in' as many set actions applied to a field has the same
5988 * affect as only applying the last action that sets a field and
5989 * duplicates are removed by do_xlate_actions().
5990 * This has an unwanted side-effect of compsoting multiple
5991 * LOAD_REG actions that touch different regions of the same field. */
5993 ofpacts_execute_action_set(struct ofpbuf *action_list,
5994 const struct ofpbuf *action_set)
5996 /* The OpenFlow spec "Action Set" section specifies this order. */
5997 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5998 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5999 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
6000 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
6001 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
6002 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
6003 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
6004 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
6006 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
6007 * we should execute only OFPACT_GROUP.
6009 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
6010 * all the actions because there's no point in modifying a packet that will
6011 * not be sent anywhere. */
6012 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
6013 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
6014 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
6015 ofpbuf_clear(action_list);
6021 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
6022 size_t n_in, enum ofp_version version,
6027 size_t start = out->size;
6029 error = ofpacts_decode(in, n_in, version, out);
6035 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
6036 if (!ofpact_is_allowed_in_actions_set(a)) {
6037 VLOG_WARN_RL(&rl, "disallowed action in action set");
6038 return OFPERR_OFPBAC_BAD_TYPE;
6045 /* OpenFlow 1.1 instructions. */
6047 struct instruction_type_info {
6048 enum ovs_instruction_type type;
6052 static const struct instruction_type_info inst_info[] = {
6053 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
6059 ovs_instruction_name_from_type(enum ovs_instruction_type type)
6061 return inst_info[type].name;
6065 ovs_instruction_type_from_name(const char *name)
6067 const struct instruction_type_info *p;
6068 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
6069 if (!strcasecmp(name, p->name)) {
6076 enum ovs_instruction_type
6077 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
6081 return OVSINST_OFPIT13_METER;
6082 case OFPACT_CLEAR_ACTIONS:
6083 return OVSINST_OFPIT11_CLEAR_ACTIONS;
6084 case OFPACT_WRITE_ACTIONS:
6085 return OVSINST_OFPIT11_WRITE_ACTIONS;
6086 case OFPACT_WRITE_METADATA:
6087 return OVSINST_OFPIT11_WRITE_METADATA;
6088 case OFPACT_GOTO_TABLE:
6089 return OVSINST_OFPIT11_GOTO_TABLE;
6092 case OFPACT_CONTROLLER:
6093 case OFPACT_ENQUEUE:
6094 case OFPACT_OUTPUT_REG:
6096 case OFPACT_SET_VLAN_VID:
6097 case OFPACT_SET_VLAN_PCP:
6098 case OFPACT_STRIP_VLAN:
6099 case OFPACT_PUSH_VLAN:
6100 case OFPACT_SET_ETH_SRC:
6101 case OFPACT_SET_ETH_DST:
6102 case OFPACT_SET_IPV4_SRC:
6103 case OFPACT_SET_IPV4_DST:
6104 case OFPACT_SET_IP_DSCP:
6105 case OFPACT_SET_IP_ECN:
6106 case OFPACT_SET_IP_TTL:
6107 case OFPACT_SET_L4_SRC_PORT:
6108 case OFPACT_SET_L4_DST_PORT:
6109 case OFPACT_REG_MOVE:
6110 case OFPACT_SET_FIELD:
6111 case OFPACT_STACK_PUSH:
6112 case OFPACT_STACK_POP:
6113 case OFPACT_DEC_TTL:
6114 case OFPACT_SET_MPLS_LABEL:
6115 case OFPACT_SET_MPLS_TC:
6116 case OFPACT_SET_MPLS_TTL:
6117 case OFPACT_DEC_MPLS_TTL:
6118 case OFPACT_PUSH_MPLS:
6119 case OFPACT_POP_MPLS:
6120 case OFPACT_SET_TUNNEL:
6121 case OFPACT_SET_QUEUE:
6122 case OFPACT_POP_QUEUE:
6123 case OFPACT_FIN_TIMEOUT:
6124 case OFPACT_RESUBMIT:
6126 case OFPACT_CONJUNCTION:
6127 case OFPACT_MULTIPATH:
6130 case OFPACT_UNROLL_XLATE:
6132 case OFPACT_DEBUG_RECIRC:
6136 return OVSINST_OFPIT11_APPLY_ACTIONS;
6141 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
6142 const uint16_t inst_type)
6144 switch (inst_type) {
6146 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6148 *instruction_type = OVSINST_##ENUM; \
6154 return OFPERR_OFPBIC_UNKNOWN_INST;
6158 /* Two-way translation between OVS's internal "OVSINST_*" representation of
6159 * instructions and the "OFPIT_*" representation used in OpenFlow. */
6160 struct ovsinst_map {
6161 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
6162 int ofpit; /* OFPIT_* number from OpenFlow spec. */
6165 static const struct ovsinst_map *
6166 get_ovsinst_map(enum ofp_version version)
6168 /* OpenFlow 1.1 and 1.2 instructions. */
6169 static const struct ovsinst_map of11[] = {
6170 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6171 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6172 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6173 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6174 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6178 /* OpenFlow 1.3+ instructions. */
6179 static const struct ovsinst_map of13[] = {
6180 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6181 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6182 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6183 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6184 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6185 { OVSINST_OFPIT13_METER, 6 },
6189 return version < OFP13_VERSION ? of11 : of13;
6192 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
6193 * values, into a bitmap of instructions suitable for OpenFlow 'version'
6194 * (OFP11_VERSION or later), and returns the result. */
6196 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
6198 uint32_t ofpit_bitmap = 0;
6199 const struct ovsinst_map *x;
6201 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6202 if (ovsinst_bitmap & (1u << x->ovsinst)) {
6203 ofpit_bitmap |= 1u << x->ofpit;
6206 return htonl(ofpit_bitmap);
6209 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
6210 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
6211 * correspond to OVSINST_* values, and returns the result. */
6213 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
6215 uint32_t ovsinst_bitmap = 0;
6216 const struct ovsinst_map *x;
6218 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6219 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
6220 ovsinst_bitmap |= 1u << x->ovsinst;
6223 return ovsinst_bitmap;
6226 static inline struct ofp11_instruction *
6227 instruction_next(const struct ofp11_instruction *inst)
6229 return ((struct ofp11_instruction *) (void *)
6230 ((uint8_t *) inst + ntohs(inst->len)));
6234 instruction_is_valid(const struct ofp11_instruction *inst,
6235 size_t n_instructions)
6237 uint16_t len = ntohs(inst->len);
6238 return (!(len % OFP11_INSTRUCTION_ALIGN)
6239 && len >= sizeof *inst
6240 && len / sizeof *inst <= n_instructions);
6243 /* This macro is careful to check for instructions with bad lengths. */
6244 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
6245 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
6246 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
6247 ((LEFT) -= (ntohs((ITER)->len) \
6248 / sizeof(struct ofp11_instruction)), \
6249 (ITER) = instruction_next(ITER)))
6252 decode_openflow11_instruction(const struct ofp11_instruction *inst,
6253 enum ovs_instruction_type *type)
6255 uint16_t len = ntohs(inst->len);
6257 switch (inst->type) {
6258 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
6259 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
6261 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6262 case CONSTANT_HTONS(ENUM): \
6264 ? len >= sizeof(struct STRUCT) \
6265 : len == sizeof(struct STRUCT)) { \
6266 *type = OVSINST_##ENUM; \
6269 return OFPERR_OFPBIC_BAD_LEN; \
6275 return OFPERR_OFPBIC_UNKNOWN_INST;
6280 decode_openflow11_instructions(const struct ofp11_instruction insts[],
6282 const struct ofp11_instruction *out[])
6284 const struct ofp11_instruction *inst;
6287 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
6288 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
6289 enum ovs_instruction_type type;
6292 error = decode_openflow11_instruction(inst, &type);
6298 return OFPERR_OFPBIC_DUP_INST;
6304 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
6305 (n_insts - left) * sizeof *inst);
6306 return OFPERR_OFPBIC_BAD_LEN;
6312 get_actions_from_instruction(const struct ofp11_instruction *inst,
6313 const struct ofp_action_header **actions,
6314 size_t *actions_len)
6316 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
6317 *actions_len = ntohs(inst->len) - sizeof *inst;
6321 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
6322 unsigned int instructions_len,
6323 enum ofp_version version,
6324 struct ofpbuf *ofpacts)
6326 const struct ofp11_instruction *instructions;
6327 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
6330 ofpbuf_clear(ofpacts);
6331 if (version == OFP10_VERSION) {
6332 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
6334 (1u << N_OVS_INSTRUCTIONS) - 1,
6338 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
6339 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
6341 instructions_len, OFP11_INSTRUCTION_ALIGN);
6342 error = OFPERR_OFPBIC_BAD_LEN;
6346 instructions = ofpbuf_try_pull(openflow, instructions_len);
6347 if (instructions == NULL) {
6348 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
6349 "remaining message length (%"PRIu32")",
6350 instructions_len, openflow->size);
6351 error = OFPERR_OFPBIC_BAD_LEN;
6355 error = decode_openflow11_instructions(
6356 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
6362 if (insts[OVSINST_OFPIT13_METER]) {
6363 const struct ofp13_instruction_meter *oim;
6364 struct ofpact_meter *om;
6366 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
6367 insts[OVSINST_OFPIT13_METER]);
6369 om = ofpact_put_METER(ofpacts);
6370 om->meter_id = ntohl(oim->meter_id);
6372 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
6373 const struct ofp_action_header *actions;
6376 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
6377 &actions, &actions_len);
6378 error = ofpacts_decode(actions, actions_len, version, ofpacts);
6383 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
6384 instruction_get_OFPIT11_CLEAR_ACTIONS(
6385 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
6386 ofpact_put_CLEAR_ACTIONS(ofpacts);
6388 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
6389 struct ofpact_nest *on;
6390 const struct ofp_action_header *actions;
6392 size_t start = ofpacts->size;
6393 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
6394 offsetof(struct ofpact_nest, actions));
6395 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
6396 &actions, &actions_len);
6397 error = ofpacts_decode_for_action_set(actions, actions_len,
6402 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
6403 on->ofpact.len = ofpacts->size - start;
6405 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
6406 const struct ofp11_instruction_write_metadata *oiwm;
6407 struct ofpact_metadata *om;
6409 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
6410 insts[OVSINST_OFPIT11_WRITE_METADATA]);
6412 om = ofpact_put_WRITE_METADATA(ofpacts);
6413 om->metadata = oiwm->metadata;
6414 om->mask = oiwm->metadata_mask;
6416 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
6417 const struct ofp11_instruction_goto_table *oigt;
6418 struct ofpact_goto_table *ogt;
6420 oigt = instruction_get_OFPIT11_GOTO_TABLE(
6421 insts[OVSINST_OFPIT11_GOTO_TABLE]);
6422 ogt = ofpact_put_GOTO_TABLE(ofpacts);
6423 ogt->table_id = oigt->table_id;
6426 error = ofpacts_verify(ofpacts->data, ofpacts->size,
6427 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
6430 ofpbuf_clear(ofpacts);
6435 /* Update the length of the instruction that begins at offset 'ofs' within
6436 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
6437 * If the instruction contains no nested actions, deletes it entirely. */
6439 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
6441 struct ofp11_instruction_actions *oia;
6443 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
6444 if (openflow->size > ofs + sizeof *oia) {
6445 oia->len = htons(openflow->size - ofs);
6447 openflow->size = ofs;
6451 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
6452 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
6453 * valid, otherwise an OpenFlow error code. */
6455 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
6463 case OFPP_CONTROLLER:
6468 return OFPERR_OFPBAC_BAD_OUT_PORT;
6471 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
6474 return OFPERR_OFPBAC_BAD_OUT_PORT;
6478 /* Removes the protocols that require consistency between match and actions
6479 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
6481 * (An example of an inconsistency between match and actions is a flow that
6482 * does not match on an MPLS Ethertype but has an action that pops an MPLS
6485 inconsistent_match(enum ofputil_protocol *usable_protocols)
6487 *usable_protocols &= OFPUTIL_P_OF10_ANY;
6490 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
6491 * caller must restore them.
6493 * Modifies some actions, filling in fields that could not be properly set
6494 * without context. */
6496 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
6497 struct flow *flow, ofp_port_t max_ports,
6498 uint8_t table_id, uint8_t n_tables)
6500 const struct ofpact_enqueue *enqueue;
6501 const struct mf_field *mf;
6505 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
6508 case OFPACT_CONTROLLER:
6511 case OFPACT_ENQUEUE:
6512 enqueue = ofpact_get_ENQUEUE(a);
6513 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
6514 && enqueue->port != OFPP_IN_PORT
6515 && enqueue->port != OFPP_LOCAL) {
6516 return OFPERR_OFPBAC_BAD_OUT_PORT;
6520 case OFPACT_OUTPUT_REG:
6521 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
6524 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
6526 case OFPACT_SET_VLAN_VID:
6527 /* Remember if we saw a vlan tag in the flow to aid translating to
6528 * OpenFlow 1.1+ if need be. */
6529 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
6530 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6531 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6532 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
6533 inconsistent_match(usable_protocols);
6535 /* Temporary mark that we have a vlan tag. */
6536 flow->vlan_tci |= htons(VLAN_CFI);
6539 case OFPACT_SET_VLAN_PCP:
6540 /* Remember if we saw a vlan tag in the flow to aid translating to
6541 * OpenFlow 1.1+ if need be. */
6542 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
6543 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6544 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6545 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
6546 inconsistent_match(usable_protocols);
6548 /* Temporary mark that we have a vlan tag. */
6549 flow->vlan_tci |= htons(VLAN_CFI);
6552 case OFPACT_STRIP_VLAN:
6553 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
6554 inconsistent_match(usable_protocols);
6556 /* Temporary mark that we have no vlan tag. */
6557 flow->vlan_tci = htons(0);
6560 case OFPACT_PUSH_VLAN:
6561 if (flow->vlan_tci & htons(VLAN_CFI)) {
6562 /* Multiple VLAN headers not supported. */
6563 return OFPERR_OFPBAC_BAD_TAG;
6565 /* Temporary mark that we have a vlan tag. */
6566 flow->vlan_tci |= htons(VLAN_CFI);
6569 case OFPACT_SET_ETH_SRC:
6570 case OFPACT_SET_ETH_DST:
6573 case OFPACT_SET_IPV4_SRC:
6574 case OFPACT_SET_IPV4_DST:
6575 if (flow->dl_type != htons(ETH_TYPE_IP)) {
6576 inconsistent_match(usable_protocols);
6580 case OFPACT_SET_IP_DSCP:
6581 case OFPACT_SET_IP_ECN:
6582 case OFPACT_SET_IP_TTL:
6583 case OFPACT_DEC_TTL:
6584 if (!is_ip_any(flow)) {
6585 inconsistent_match(usable_protocols);
6589 case OFPACT_SET_L4_SRC_PORT:
6590 case OFPACT_SET_L4_DST_PORT:
6591 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
6592 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
6593 && flow->nw_proto != IPPROTO_SCTP)) {
6594 inconsistent_match(usable_protocols);
6596 /* Note on which transport protocol the port numbers are set.
6597 * This allows this set action to be converted to an OF1.2 set field
6599 if (a->type == OFPACT_SET_L4_SRC_PORT) {
6600 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
6602 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
6606 case OFPACT_REG_MOVE:
6607 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
6609 case OFPACT_SET_FIELD:
6610 mf = ofpact_get_SET_FIELD(a)->field;
6611 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6612 if (!mf_are_prereqs_ok(mf, flow) ||
6613 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
6614 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
6616 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6618 /* Remember if we saw a vlan tag in the flow to aid translating to
6619 * OpenFlow 1.1 if need be. */
6620 ofpact_get_SET_FIELD(a)->flow_has_vlan =
6621 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6622 if (mf->id == MFF_VLAN_TCI) {
6623 /* The set field may add or remove the vlan tag,
6624 * Mark the status temporarily. */
6625 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
6629 case OFPACT_STACK_PUSH:
6630 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
6632 case OFPACT_STACK_POP:
6633 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6635 case OFPACT_SET_MPLS_LABEL:
6636 case OFPACT_SET_MPLS_TC:
6637 case OFPACT_SET_MPLS_TTL:
6638 case OFPACT_DEC_MPLS_TTL:
6639 if (!eth_type_mpls(flow->dl_type)) {
6640 inconsistent_match(usable_protocols);
6644 case OFPACT_SET_TUNNEL:
6645 case OFPACT_SET_QUEUE:
6646 case OFPACT_POP_QUEUE:
6647 case OFPACT_RESUBMIT:
6650 case OFPACT_FIN_TIMEOUT:
6651 if (flow->nw_proto != IPPROTO_TCP) {
6652 inconsistent_match(usable_protocols);
6657 return learn_check(ofpact_get_LEARN(a), flow);
6659 case OFPACT_CONJUNCTION:
6662 case OFPACT_MULTIPATH:
6663 return multipath_check(ofpact_get_MULTIPATH(a), flow);
6669 case OFPACT_PUSH_MPLS:
6670 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6671 /* The packet is now MPLS and the MPLS payload is opaque.
6672 * Thus nothing can be assumed about the network protocol.
6673 * Temporarily mark that we have no nw_proto. */
6677 case OFPACT_POP_MPLS:
6678 if (!eth_type_mpls(flow->dl_type)) {
6679 inconsistent_match(usable_protocols);
6681 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6688 struct ofpact_conntrack *oc = ofpact_get_CT(a);
6691 if (!dl_type_is_ip_any(flow->dl_type)
6692 || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6693 inconsistent_match(usable_protocols);
6696 if (oc->zone_src.field) {
6697 return mf_check_src(&oc->zone_src, flow);
6700 err = ofpacts_check(oc->actions, ofpact_ct_get_action_len(oc),
6701 flow, max_ports, table_id, n_tables,
6707 struct ofpact_nat *on = ofpact_get_NAT(a);
6709 if (!dl_type_is_ip_any(flow->dl_type) ||
6710 (on->range_af == AF_INET && flow->dl_type != htons(ETH_TYPE_IP)) ||
6711 (on->range_af == AF_INET6
6712 && flow->dl_type != htons(ETH_TYPE_IPV6))) {
6713 inconsistent_match(usable_protocols);
6718 case OFPACT_CLEAR_ACTIONS:
6721 case OFPACT_WRITE_ACTIONS: {
6722 /* Use a temporary copy of 'usable_protocols' because we can't check
6723 * consistency of an action set. */
6724 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6725 enum ofputil_protocol p = *usable_protocols;
6726 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6727 flow, max_ports, table_id, n_tables, &p);
6730 case OFPACT_WRITE_METADATA:
6733 case OFPACT_METER: {
6734 uint32_t mid = ofpact_get_METER(a)->meter_id;
6735 if (mid == 0 || mid > OFPM13_MAX) {
6736 return OFPERR_OFPMMFC_INVALID_METER;
6741 case OFPACT_GOTO_TABLE: {
6742 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
6743 if ((table_id != 255 && goto_table <= table_id)
6744 || (n_tables != 255 && goto_table >= n_tables)) {
6745 return OFPERR_OFPBIC_BAD_TABLE_ID;
6753 case OFPACT_UNROLL_XLATE:
6754 /* UNROLL is an internal action that should never be seen via
6756 return OFPERR_OFPBAC_BAD_TYPE;
6758 case OFPACT_DEBUG_RECIRC:
6766 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6767 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6768 * switch with no more than 'max_ports' ports.
6770 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6771 * '*usable_protocols' the protocols that forbid the inconsistency. (An
6772 * example of an inconsistency between match and actions is a flow that does
6773 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6775 * May annotate ofpacts with information gathered from the 'flow'.
6777 * May temporarily modify 'flow', but restores the changes before returning. */
6779 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
6780 struct flow *flow, ofp_port_t max_ports,
6781 uint8_t table_id, uint8_t n_tables,
6782 enum ofputil_protocol *usable_protocols)
6785 ovs_be16 dl_type = flow->dl_type;
6786 ovs_be16 vlan_tci = flow->vlan_tci;
6787 uint8_t nw_proto = flow->nw_proto;
6788 enum ofperr error = 0;
6790 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6791 error = ofpact_check__(usable_protocols, a, flow,
6792 max_ports, table_id, n_tables);
6797 /* Restore fields that may have been modified. */
6798 flow->dl_type = dl_type;
6799 flow->vlan_tci = vlan_tci;
6800 flow->nw_proto = nw_proto;
6804 /* Like ofpacts_check(), but reports inconsistencies as
6805 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6807 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
6808 struct flow *flow, ofp_port_t max_ports,
6809 uint8_t table_id, uint8_t n_tables,
6810 enum ofputil_protocol usable_protocols)
6812 enum ofputil_protocol p = usable_protocols;
6815 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
6816 table_id, n_tables, &p);
6817 return (error ? error
6818 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
6822 /* Returns the destination field that 'ofpact' would write to, or NULL
6823 * if the action would not write to an mf_field. */
6824 const struct mf_field *
6825 ofpact_get_mf_dst(const struct ofpact *ofpact)
6827 if (ofpact->type == OFPACT_SET_FIELD) {
6828 const struct ofpact_set_field *orl;
6830 orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
6832 } else if (ofpact->type == OFPACT_REG_MOVE) {
6833 const struct ofpact_reg_move *orm;
6835 orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
6836 return orm->dst.field;
6843 unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action)
6845 VLOG_WARN("%s action doesn't support nested action %s",
6846 ofpact_name(outer_action), ofpact_name(action));
6847 return OFPERR_OFPBAC_BAD_ARGUMENT;
6851 field_requires_ct(enum mf_field_id field)
6853 return field == MFF_CT_MARK || field == MFF_CT_LABEL;
6856 /* Apply nesting constraints for actions */
6858 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
6860 const struct mf_field *field = ofpact_get_mf_dst(a);
6862 if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
6863 VLOG_WARN("cannot set CT fields outside of ct action");
6864 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6866 if (a->type == OFPACT_NAT) {
6867 if (outer_action != OFPACT_CT) {
6868 VLOG_WARN("Cannot have NAT action outside of \"ct\" action");
6869 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6875 ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
6876 || outer_action == OFPACT_CT);
6878 if (outer_action == OFPACT_CT) {
6880 return unsupported_nesting(a->type, outer_action);
6881 } else if (!field_requires_ct(field->id)) {
6882 VLOG_WARN("%s action doesn't support nested modification "
6883 "of %s", ofpact_name(outer_action), field->name);
6884 return OFPERR_OFPBAC_BAD_ARGUMENT;
6892 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
6893 * appropriate order as defined by the OpenFlow spec and as required by Open
6896 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
6897 * instructions that are allowed within 'ofpacts[]'.
6899 * If 'outer_action' is not zero, it specifies that the actions are nested
6900 * within another action of type 'outer_action'. */
6902 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
6903 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
6905 const struct ofpact *a;
6906 enum ovs_instruction_type inst;
6908 inst = OVSINST_OFPIT13_METER;
6909 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6910 enum ovs_instruction_type next;
6913 if (a->type == OFPACT_CONJUNCTION) {
6914 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6915 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
6916 VLOG_WARN("\"conjunction\" actions may be used along with "
6917 "\"note\" but not any other kind of action "
6918 "(such as the \"%s\" action used here)",
6919 ofpact_name(a->type));
6920 return OFPERR_NXBAC_BAD_CONJUNCTION;
6926 error = ofpacts_verify_nested(a, outer_action);
6931 next = ovs_instruction_type_from_ofpact_type(a->type);
6933 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
6936 const char *name = ovs_instruction_name_from_type(inst);
6937 const char *next_name = ovs_instruction_name_from_type(next);
6940 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
6941 "1.1+ compatibility", name);
6943 VLOG_WARN("invalid instruction ordering: %s must appear "
6944 "before %s, for OpenFlow 1.1+ compatibility",
6947 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
6949 if (!((1u << next) & allowed_ovsinsts)) {
6950 const char *name = ovs_instruction_name_from_type(next);
6952 VLOG_WARN("%s instruction not allowed here", name);
6953 return OFPERR_OFPBIC_UNSUP_INST;
6962 /* Converting ofpacts to OpenFlow. */
6965 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
6969 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6970 case OFPACT_##ENUM: \
6971 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
6980 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
6981 * actions in 'openflow', appending the actions to any existing data in
6984 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
6985 struct ofpbuf *openflow,
6986 enum ofp_version ofp_version)
6988 const struct ofpact *a;
6989 size_t start_size = openflow->size;
6991 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6992 encode_ofpact(a, ofp_version, openflow);
6994 return openflow->size - start_size;
6997 static enum ovs_instruction_type
6998 ofpact_is_apply_actions(const struct ofpact *a)
7000 return (ovs_instruction_type_from_ofpact_type(a->type)
7001 == OVSINST_OFPIT11_APPLY_ACTIONS);
7005 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
7007 struct ofpbuf *openflow,
7008 enum ofp_version ofp_version)
7010 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
7011 const struct ofpact *a;
7013 if (ofp_version == OFP10_VERSION) {
7014 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
7021 if (ofpact_is_apply_actions(a)) {
7022 size_t ofs = openflow->size;
7024 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
7026 encode_ofpact(a, ofp_version, openflow);
7028 } while (a < end && ofpact_is_apply_actions(a));
7029 ofpacts_update_instruction_actions(openflow, ofs);
7031 encode_ofpact(a, ofp_version, openflow);
7037 /* Sets of supported actions. */
7039 /* Two-way translation between OVS's internal "OFPACT_*" representation of
7040 * actions and the "OFPAT_*" representation used in some OpenFlow version.
7041 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
7042 * instance is specific to one OpenFlow version.) */
7044 enum ofpact_type ofpact; /* Internal name for action type. */
7045 int ofpat; /* OFPAT_* number from OpenFlow spec. */
7048 static const struct ofpact_map *
7049 get_ofpact_map(enum ofp_version version)
7051 /* OpenFlow 1.0 actions. */
7052 static const struct ofpact_map of10[] = {
7053 { OFPACT_OUTPUT, 0 },
7054 { OFPACT_SET_VLAN_VID, 1 },
7055 { OFPACT_SET_VLAN_PCP, 2 },
7056 { OFPACT_STRIP_VLAN, 3 },
7057 { OFPACT_SET_ETH_SRC, 4 },
7058 { OFPACT_SET_ETH_DST, 5 },
7059 { OFPACT_SET_IPV4_SRC, 6 },
7060 { OFPACT_SET_IPV4_DST, 7 },
7061 { OFPACT_SET_IP_DSCP, 8 },
7062 { OFPACT_SET_L4_SRC_PORT, 9 },
7063 { OFPACT_SET_L4_DST_PORT, 10 },
7064 { OFPACT_ENQUEUE, 11 },
7068 /* OpenFlow 1.1 actions. */
7069 static const struct ofpact_map of11[] = {
7070 { OFPACT_OUTPUT, 0 },
7071 { OFPACT_SET_VLAN_VID, 1 },
7072 { OFPACT_SET_VLAN_PCP, 2 },
7073 { OFPACT_SET_ETH_SRC, 3 },
7074 { OFPACT_SET_ETH_DST, 4 },
7075 { OFPACT_SET_IPV4_SRC, 5 },
7076 { OFPACT_SET_IPV4_DST, 6 },
7077 { OFPACT_SET_IP_DSCP, 7 },
7078 { OFPACT_SET_IP_ECN, 8 },
7079 { OFPACT_SET_L4_SRC_PORT, 9 },
7080 { OFPACT_SET_L4_DST_PORT, 10 },
7081 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7082 /* OFPAT_COPY_TTL_IN (12) not supported. */
7083 { OFPACT_SET_MPLS_LABEL, 13 },
7084 { OFPACT_SET_MPLS_TC, 14 },
7085 { OFPACT_SET_MPLS_TTL, 15 },
7086 { OFPACT_DEC_MPLS_TTL, 16 },
7087 { OFPACT_PUSH_VLAN, 17 },
7088 { OFPACT_STRIP_VLAN, 18 },
7089 { OFPACT_PUSH_MPLS, 19 },
7090 { OFPACT_POP_MPLS, 20 },
7091 { OFPACT_SET_QUEUE, 21 },
7092 { OFPACT_GROUP, 22 },
7093 { OFPACT_SET_IP_TTL, 23 },
7094 { OFPACT_DEC_TTL, 24 },
7098 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
7099 static const struct ofpact_map of12[] = {
7100 { OFPACT_OUTPUT, 0 },
7101 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7102 /* OFPAT_COPY_TTL_IN (12) not supported. */
7103 { OFPACT_SET_MPLS_TTL, 15 },
7104 { OFPACT_DEC_MPLS_TTL, 16 },
7105 { OFPACT_PUSH_VLAN, 17 },
7106 { OFPACT_STRIP_VLAN, 18 },
7107 { OFPACT_PUSH_MPLS, 19 },
7108 { OFPACT_POP_MPLS, 20 },
7109 { OFPACT_SET_QUEUE, 21 },
7110 { OFPACT_GROUP, 22 },
7111 { OFPACT_SET_IP_TTL, 23 },
7112 { OFPACT_DEC_TTL, 24 },
7113 { OFPACT_SET_FIELD, 25 },
7114 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
7115 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
7135 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
7136 * values, into a bitmap of actions suitable for OpenFlow 'version', and
7137 * returns the result. */
7139 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
7141 uint32_t openflow_bitmap = 0;
7142 const struct ofpact_map *x;
7144 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7145 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
7146 openflow_bitmap |= 1u << x->ofpat;
7149 return htonl(openflow_bitmap);
7152 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
7153 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
7154 * and returns the result. */
7156 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
7158 uint64_t ofpact_bitmap = 0;
7159 const struct ofpact_map *x;
7161 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7162 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
7163 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
7166 return ofpact_bitmap;
7169 /* Appends to 's' a string representation of the set of OFPACT_* represented
7170 * by 'ofpacts_bitmap'. */
7172 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
7174 if (!ofpacts_bitmap) {
7175 ds_put_cstr(s, "<none>");
7177 while (ofpacts_bitmap) {
7178 ds_put_format(s, "%s ",
7179 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
7180 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
7186 /* Returns true if 'action' outputs to 'port', false otherwise. */
7188 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
7190 switch (ofpact->type) {
7192 return ofpact_get_OUTPUT(ofpact)->port == port;
7193 case OFPACT_ENQUEUE:
7194 return ofpact_get_ENQUEUE(ofpact)->port == port;
7195 case OFPACT_CONTROLLER:
7196 return port == OFPP_CONTROLLER;
7198 case OFPACT_OUTPUT_REG:
7200 case OFPACT_SET_VLAN_VID:
7201 case OFPACT_SET_VLAN_PCP:
7202 case OFPACT_STRIP_VLAN:
7203 case OFPACT_PUSH_VLAN:
7204 case OFPACT_SET_ETH_SRC:
7205 case OFPACT_SET_ETH_DST:
7206 case OFPACT_SET_IPV4_SRC:
7207 case OFPACT_SET_IPV4_DST:
7208 case OFPACT_SET_IP_DSCP:
7209 case OFPACT_SET_IP_ECN:
7210 case OFPACT_SET_IP_TTL:
7211 case OFPACT_SET_L4_SRC_PORT:
7212 case OFPACT_SET_L4_DST_PORT:
7213 case OFPACT_REG_MOVE:
7214 case OFPACT_SET_FIELD:
7215 case OFPACT_STACK_PUSH:
7216 case OFPACT_STACK_POP:
7217 case OFPACT_DEC_TTL:
7218 case OFPACT_SET_MPLS_LABEL:
7219 case OFPACT_SET_MPLS_TC:
7220 case OFPACT_SET_MPLS_TTL:
7221 case OFPACT_DEC_MPLS_TTL:
7222 case OFPACT_SET_TUNNEL:
7223 case OFPACT_WRITE_METADATA:
7224 case OFPACT_SET_QUEUE:
7225 case OFPACT_POP_QUEUE:
7226 case OFPACT_FIN_TIMEOUT:
7227 case OFPACT_RESUBMIT:
7229 case OFPACT_CONJUNCTION:
7230 case OFPACT_MULTIPATH:
7233 case OFPACT_UNROLL_XLATE:
7234 case OFPACT_PUSH_MPLS:
7235 case OFPACT_POP_MPLS:
7237 case OFPACT_CLEAR_ACTIONS:
7238 case OFPACT_WRITE_ACTIONS:
7239 case OFPACT_GOTO_TABLE:
7242 case OFPACT_DEBUG_RECIRC:
7250 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7251 * to 'port', false otherwise. */
7253 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
7256 const struct ofpact *a;
7258 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
7259 if (ofpact_outputs_to_port(a, port)) {
7267 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7268 * to 'group', false otherwise. */
7270 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
7273 const struct ofpact *a;
7275 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
7276 if (a->type == OFPACT_GROUP
7277 && ofpact_get_GROUP(a)->group_id == group_id) {
7286 ofpacts_equal(const struct ofpact *a, size_t a_len,
7287 const struct ofpact *b, size_t b_len)
7289 return a_len == b_len && !memcmp(a, b, a_len);
7292 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
7293 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
7295 * This function relies on the order of 'ofpacts' being correct (as checked by
7296 * ofpacts_verify()). */
7298 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
7300 const struct ofpact *a;
7302 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7303 enum ovs_instruction_type inst;
7305 inst = ovs_instruction_type_from_ofpact_type(a->type);
7306 if (a->type == OFPACT_METER) {
7307 return ofpact_get_METER(a)->meter_id;
7308 } else if (inst > OVSINST_OFPIT13_METER) {
7316 /* Formatting ofpacts. */
7319 ofpact_format(const struct ofpact *a, struct ds *s)
7322 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7323 case OFPACT_##ENUM: \
7324 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
7333 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
7334 * 'ofpacts' to 'string'. */
7336 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
7340 ds_put_cstr(string, "drop");
7342 const struct ofpact *a;
7344 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7346 ds_put_cstr(string, ",");
7349 /* XXX write-actions */
7350 ofpact_format(a, string);
7355 /* Internal use by helpers. */
7358 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
7360 struct ofpact *ofpact;
7362 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
7363 ofpact = ofpacts->header;
7364 ofpact_init(ofpact, type, len);
7369 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
7371 memset(ofpact, 0, len);
7372 ofpact->type = type;
7377 /* Finishes composing a variable-length action (begun using
7378 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
7379 * bytes and updating its embedded length field. See the large comment near
7380 * the end of ofp-actions.h for more information. */
7382 ofpact_finish(struct ofpbuf *ofpacts, struct ofpact *ofpact)
7384 ovs_assert(ofpact == ofpacts->header);
7385 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
7386 ofpbuf_padto(ofpacts, OFPACT_ALIGN(ofpacts->size));
7389 static char * OVS_WARN_UNUSED_RESULT
7390 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
7391 enum ofputil_protocol *usable_protocols)
7394 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7395 case OFPACT_##ENUM: \
7396 return parse_##ENUM(value, ofpacts, usable_protocols);
7405 ofpact_type_from_name(const char *name, enum ofpact_type *type)
7407 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7408 if (!strcasecmp(name, NAME)) { \
7409 *type = OFPACT_##ENUM; \
7418 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
7420 * Returns NULL if successful, otherwise a malloc()'d string describing the
7421 * error. The caller is responsible for freeing the returned string.
7423 * If 'outer_action' is specified, indicates that the actions being parsed
7424 * are nested within another action of the type specified in 'outer_action'. */
7425 static char * OVS_WARN_UNUSED_RESULT
7426 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
7427 enum ofputil_protocol *usable_protocols,
7428 bool allow_instructions, enum ofpact_type outer_action)
7437 while (ofputil_parse_key_value(&pos, &key, &value)) {
7438 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
7439 enum ofpact_type type;
7443 if (ofpact_type_from_name(key, &type)) {
7444 error = ofpact_parse(type, value, ofpacts, usable_protocols);
7445 inst = ovs_instruction_type_from_ofpact_type(type);
7446 } else if (!strcasecmp(key, "mod_vlan_vid")) {
7447 error = parse_set_vlan_vid(value, ofpacts, true);
7448 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
7449 error = parse_set_vlan_pcp(value, ofpacts, true);
7450 } else if (!strcasecmp(key, "set_nw_ttl")) {
7451 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
7452 } else if (!strcasecmp(key, "pop_vlan")) {
7453 error = parse_pop_vlan(ofpacts);
7454 } else if (!strcasecmp(key, "set_tunnel64")) {
7455 error = parse_set_tunnel(value, ofpacts,
7456 NXAST_RAW_SET_TUNNEL64);
7457 } else if (!strcasecmp(key, "load")) {
7458 error = parse_reg_load(value, ofpacts);
7459 } else if (!strcasecmp(key, "bundle_load")) {
7460 error = parse_bundle_load(value, ofpacts);
7461 } else if (!strcasecmp(key, "drop")) {
7463 } else if (!strcasecmp(key, "apply_actions")) {
7464 return xstrdup("apply_actions is the default instruction");
7465 } else if (ofputil_port_from_string(key, &port)) {
7466 ofpact_put_OUTPUT(ofpacts)->port = port;
7468 return xasprintf("unknown action %s", key);
7474 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
7475 if (!allow_instructions) {
7476 return xasprintf("only actions are allowed here (not "
7478 ovs_instruction_name_from_type(inst));
7480 if (inst == prev_inst) {
7481 return xasprintf("instruction %s may be specified only once",
7482 ovs_instruction_name_from_type(inst));
7485 if (prev_inst != -1 && inst < prev_inst) {
7486 return xasprintf("instruction %s must be specified before %s",
7487 ovs_instruction_name_from_type(inst),
7488 ovs_instruction_name_from_type(prev_inst));
7493 if (drop && ofpacts->size) {
7494 return xstrdup("\"drop\" must not be accompanied by any other action "
7498 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
7500 ? (1u << N_OVS_INSTRUCTIONS) - 1
7501 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
7504 return xstrdup("Incorrect instruction ordering");
7510 static char * OVS_WARN_UNUSED_RESULT
7511 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
7512 enum ofputil_protocol *usable_protocols, bool allow_instructions,
7513 enum ofpact_type outer_action)
7515 uint32_t orig_size = ofpacts->size;
7516 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
7517 allow_instructions, outer_action);
7519 ofpacts->size = orig_size;
7524 static char * OVS_WARN_UNUSED_RESULT
7525 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
7526 enum ofputil_protocol *usable_protocols,
7527 bool allow_instructions, enum ofpact_type outer_action)
7531 *usable_protocols = OFPUTIL_P_ANY;
7534 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
7541 /* Parses 's' as a set of OpenFlow actions and appends the actions to
7542 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
7543 * that are nested within the action of type 'outer_action'.
7545 * Returns NULL if successful, otherwise a malloc()'d string describing the
7546 * error. The caller is responsible for freeing the returned string. */
7547 char * OVS_WARN_UNUSED_RESULT
7548 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
7549 enum ofputil_protocol *usable_protocols)
7551 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
7554 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
7557 * Returns NULL if successful, otherwise a malloc()'d string describing the
7558 * error. The caller is responsible for freeing the returned string. */
7559 char * OVS_WARN_UNUSED_RESULT
7560 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
7561 enum ofputil_protocol *usable_protocols)
7563 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
7567 ofpact_name(enum ofpact_type type)
7570 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7577 /* Low-level action decoding and encoding functions. */
7579 /* Everything needed to identify a particular OpenFlow action. */
7580 struct ofpact_hdrs {
7581 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
7582 uint16_t type; /* Type if standard, otherwise subtype. */
7583 uint8_t ofp_version; /* From ofp_header. */
7586 /* Information about a particular OpenFlow action. */
7587 struct ofpact_raw_instance {
7588 /* The action's identity. */
7589 struct ofpact_hdrs hdrs;
7590 enum ofp_raw_action_type raw;
7592 /* Looking up the action. */
7593 struct hmap_node decode_node; /* Based on 'hdrs'. */
7594 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
7596 /* The action's encoded size.
7598 * If this action is fixed-length, 'min_length' == 'max_length'.
7599 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7600 * OFP_ACTION_ALIGN) == 65528. */
7601 unsigned short int min_length;
7602 unsigned short int max_length;
7604 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7605 * offset of that argument from the beginning of the action and 'arg_len'
7606 * its length, both in bytes.
7608 * For actions that take other forms, these are both zero. */
7609 unsigned short int arg_ofs;
7610 unsigned short int arg_len;
7612 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7615 /* If this action is deprecated, a human-readable string with a brief
7617 const char *deprecation;
7620 /* Action header. */
7621 struct ofp_action_header {
7622 /* The meaning of other values of 'type' generally depends on the OpenFlow
7623 * version (see enum ofp_raw_action_type).
7625 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7626 * designates an OpenFlow vendor ID and that the remainder of the action
7627 * structure has a vendor-defined meaning.
7629 #define OFPAT_VENDOR 0xffff
7632 /* Always a multiple of 8. */
7635 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7636 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
7639 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
7641 /* Header for Nicira-defined actions and for ONF vendor extensions.
7643 * This cannot be used as an entirely generic vendor extension action header,
7644 * because OpenFlow does not specify the location or size of the action
7645 * subtype; it just happens that ONF extensions and Nicira extensions share
7647 struct ext_action_header {
7648 ovs_be16 type; /* OFPAT_VENDOR. */
7649 ovs_be16 len; /* At least 16. */
7650 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
7651 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
7654 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
7657 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
7658 const struct ofpact_hdrs *b)
7660 return (a->vendor == b->vendor
7661 && a->type == b->type
7662 && a->ofp_version == b->ofp_version);
7666 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
7668 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
7671 #include "ofp-actions.inc2"
7673 static struct hmap *
7674 ofpact_decode_hmap(void)
7676 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7677 static struct hmap hmap;
7679 if (ovsthread_once_start(&once)) {
7680 struct ofpact_raw_instance *inst;
7683 for (inst = all_raw_instances;
7684 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7686 hmap_insert(&hmap, &inst->decode_node,
7687 ofpact_hdrs_hash(&inst->hdrs));
7689 ovsthread_once_done(&once);
7694 static struct hmap *
7695 ofpact_encode_hmap(void)
7697 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7698 static struct hmap hmap;
7700 if (ovsthread_once_start(&once)) {
7701 struct ofpact_raw_instance *inst;
7704 for (inst = all_raw_instances;
7705 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7707 hmap_insert(&hmap, &inst->encode_node,
7708 hash_2words(inst->raw, inst->hdrs.ofp_version));
7710 ovsthread_once_done(&once);
7716 ofpact_decode_raw(enum ofp_version ofp_version,
7717 const struct ofp_action_header *oah, size_t length,
7718 const struct ofpact_raw_instance **instp)
7720 const struct ofpact_raw_instance *inst;
7721 struct ofpact_hdrs hdrs;
7724 if (length < sizeof *oah) {
7725 return OFPERR_OFPBAC_BAD_LEN;
7728 /* Get base action type. */
7729 if (oah->type == htons(OFPAT_VENDOR)) {
7731 hdrs.vendor = ntohl(oah->vendor);
7732 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
7733 /* Get extension subtype. */
7734 const struct ext_action_header *nah;
7736 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
7737 if (length < sizeof *nah) {
7738 return OFPERR_OFPBAC_BAD_LEN;
7740 hdrs.type = ntohs(nah->subtype);
7742 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
7744 return OFPERR_OFPBAC_BAD_VENDOR;
7748 hdrs.type = ntohs(oah->type);
7751 hdrs.ofp_version = ofp_version;
7752 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
7753 ofpact_decode_hmap()) {
7754 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
7761 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7762 : OFPERR_OFPBAC_BAD_TYPE);
7766 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7767 enum ofp_raw_action_type *raw, uint64_t *arg)
7769 const struct ofp_action_header *oah = buf->data;
7770 const struct ofpact_raw_instance *action;
7771 unsigned int length;
7775 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
7780 if (action->deprecation) {
7781 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
7782 action->name, ofputil_version_to_string(ofp_version),
7783 action->deprecation);
7786 length = ntohs(oah->len);
7787 if (length > buf->size) {
7788 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
7789 "length %"PRIu32, action->name, length, buf->size);
7790 return OFPERR_OFPBAC_BAD_LEN;
7792 if (length < action->min_length || length > action->max_length) {
7793 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
7794 "[%hu,%hu]", action->name, length,
7795 action->min_length, action->max_length);
7796 return OFPERR_OFPBAC_BAD_LEN;
7799 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
7800 "of 8", action->name, length);
7801 return OFPERR_OFPBAC_BAD_LEN;
7806 if (action->arg_len) {
7810 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
7811 for (i = 0; i < action->arg_len; i++) {
7812 *arg = (*arg << 8) | p[i];
7816 ofpbuf_pull(buf, length);
7821 static const struct ofpact_raw_instance *
7822 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
7824 const struct ofpact_raw_instance *inst;
7826 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
7827 ofpact_encode_hmap()) {
7828 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
7836 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7837 enum ofp_raw_action_type raw, uint64_t arg)
7839 const struct ofpact_raw_instance *inst;
7840 struct ofp_action_header *oah;
7841 const struct ofpact_hdrs *hdrs;
7843 inst = ofpact_raw_lookup(ofp_version, raw);
7846 oah = ofpbuf_put_zeros(buf, inst->min_length);
7847 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
7848 oah->len = htons(inst->min_length);
7849 oah->vendor = htonl(hdrs->vendor);
7851 switch (hdrs->vendor) {
7856 case ONF_VENDOR_ID: {
7857 struct ext_action_header *nah = (struct ext_action_header *) oah;
7858 nah->subtype = htons(hdrs->type);
7866 if (inst->arg_len) {
7867 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
7870 for (i = 0; i < inst->arg_len; i++) {
7882 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
7884 struct ofp_action_header *oah;
7886 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs,
7889 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
7890 oah->len = htons(openflow->size - start_ofs);