2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-actions.h"
20 #include "byte-order.h"
23 #include "dynamic-string.h"
26 #include "meta-flow.h"
27 #include "multipath.h"
29 #include "ofp-parse.h"
32 #include "unaligned.h"
34 #include "openvswitch/vlog.h"
36 VLOG_DEFINE_THIS_MODULE(ofp_actions);
38 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
40 struct ofp_action_header;
42 /* Raw identifiers for OpenFlow actions.
44 * Decoding and encoding OpenFlow actions across multiple versions is difficult
45 * to do in a clean, consistent way. This enumeration lays out all of the
46 * forms of actions that Open vSwitch supports.
48 * The comments here must follow a stylized form because the
49 * "extract-ofp-actions" program parses them at build time to generate data
52 * - The first part of each comment specifies the vendor, OpenFlow versions,
53 * and type for each protocol that supports the action:
55 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
56 * extension actions. (Support for other vendors can be added, but
57 * it can't be done just based on a vendor ID definition alone
58 * because OpenFlow doesn't define a standard way to specify a
59 * subtype for vendor actions, so other vendors might do it different
62 * # The version can specify a specific OpenFlow version, a version
63 * range delimited by "-", or an open-ended range with "+".
65 * # The type, in parentheses, is the action type number (for standard
66 * OpenFlow actions) or subtype (for vendor extension actions).
68 * # Optionally one may add "is deprecated" followed by a
69 * human-readable reason in parentheses (which will be used in log
70 * messages), if a particular action should no longer be used.
72 * Multiple such specifications may be separated by commas.
74 * - The second part describes the action's wire format. It may be:
76 * # "struct <name>": The struct fully specifies the wire format. The
77 * action is exactly the size of the struct. (Thus, the struct must
78 * be an exact multiple of 8 bytes in size.)
80 * # "struct <name>, ...": The struct specifies the beginning of the
81 * wire format. An instance of the action is either the struct's
82 * exact size, or a multiple of 8 bytes longer.
84 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
85 * vendor extension) header, followed by 0 or more pad bytes to align
86 * to a multiple of <N> bits, followed by an argument of the given
87 * type, followed by 0 or more pad bytes to bring the total action up
88 * to a multiple of 8 bytes.
90 * # "void": The action is just a (standard or vendor extension)
93 * - Optional additional text enclosed in square brackets is commentary for
96 enum ofp_raw_action_type {
97 /* ## ----------------- ## */
98 /* ## Standard actions. ## */
99 /* ## ----------------- ## */
101 /* OF1.0(0): struct ofp10_action_output. */
103 /* OF1.1+(0): struct ofp11_action_output. */
106 /* OF1.0(1): uint16_t. */
107 OFPAT_RAW10_SET_VLAN_VID,
108 /* OF1.0(2): uint8_t. */
109 OFPAT_RAW10_SET_VLAN_PCP,
111 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
113 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
114 * modification actions: the 1.0 versions push a VLAN header if none is
115 * present, but the 1.1 versions do not. That is the only reason that we
116 * distinguish their raw action types.] */
117 OFPAT_RAW11_SET_VLAN_VID,
118 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
119 OFPAT_RAW11_SET_VLAN_PCP,
121 /* OF1.1+(17): ovs_be16.
123 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
125 OFPAT_RAW11_PUSH_VLAN,
127 /* OF1.0(3): void. */
128 OFPAT_RAW10_STRIP_VLAN,
129 /* OF1.1+(18): void. */
130 OFPAT_RAW11_POP_VLAN,
132 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
133 * ofp_action_dl_addr. */
134 OFPAT_RAW_SET_DL_SRC,
136 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
137 * ofp_action_dl_addr. */
138 OFPAT_RAW_SET_DL_DST,
140 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
142 OFPAT_RAW_SET_NW_SRC,
144 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
146 OFPAT_RAW_SET_NW_DST,
148 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
149 OFPAT_RAW_SET_NW_TOS,
151 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
152 OFPAT_RAW11_SET_NW_ECN,
154 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
156 OFPAT_RAW_SET_TP_SRC,
158 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
160 OFPAT_RAW_SET_TP_DST,
162 /* OF1.0(11): struct ofp10_action_enqueue. */
165 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
167 OFPAT_RAW_SET_MPLS_LABEL,
169 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
171 OFPAT_RAW_SET_MPLS_TC,
173 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
175 OFPAT_RAW_SET_MPLS_TTL,
177 /* NX1.0(26), OF1.1+(16): void. */
178 OFPAT_RAW_DEC_MPLS_TTL,
180 /* NX1.0(23), OF1.1+(19): ovs_be16.
182 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
185 /* NX1.0(24), OF1.1+(20): ovs_be16.
187 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
188 * ETH_TYPE_MPLS otherwise, not the label.] */
191 /* NX1.0(4), OF1.1+(21): uint32_t. */
194 /* OF1.1+(22): uint32_t. */
197 /* OF1.1+(23): uint8_t. */
198 OFPAT_RAW11_SET_NW_TTL,
200 /* NX1.0(18), OF1.1+(24): void. */
201 OFPAT_RAW_DEC_NW_TTL,
202 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
203 NXAST_RAW_DEC_TTL_CNT_IDS,
205 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
206 OFPAT_RAW12_SET_FIELD,
207 /* OF1.5+(25): struct ofp12_action_set_field, ... */
208 OFPAT_RAW15_SET_FIELD,
209 /* NX1.0-1.4(7): struct nx_action_reg_load.
211 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
212 * we drop reg_load.] */
214 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
216 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
217 * we drop reg_load2.] */
220 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
221 OFPAT_RAW15_COPY_FIELD,
222 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
223 ONFACT_RAW13_COPY_FIELD,
224 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
227 /* ## ------------------------- ## */
228 /* ## Nicira extension actions. ## */
229 /* ## ------------------------- ## */
231 /* Actions similar to standard actions are listed with the standard actions. */
233 /* NX1.0+(1): uint16_t. */
235 /* NX1.0+(14): struct nx_action_resubmit. */
236 NXAST_RAW_RESUBMIT_TABLE,
238 /* NX1.0+(2): uint32_t. */
239 NXAST_RAW_SET_TUNNEL,
240 /* NX1.0+(9): uint64_t. */
241 NXAST_RAW_SET_TUNNEL64,
243 /* NX1.0+(5): void. */
246 /* NX1.0+(8): struct nx_action_note, ... */
249 /* NX1.0+(10): struct nx_action_multipath. */
252 /* NX1.0+(12): struct nx_action_bundle, ... */
254 /* NX1.0+(13): struct nx_action_bundle, ... */
255 NXAST_RAW_BUNDLE_LOAD,
257 /* NX1.0+(15): struct nx_action_output_reg. */
258 NXAST_RAW_OUTPUT_REG,
259 /* NX1.0+(32): struct nx_action_output_reg2. */
260 NXAST_RAW_OUTPUT_REG2,
262 /* NX1.0+(16): struct nx_action_learn, ... */
265 /* NX1.0+(17): void. */
268 /* NX1.0+(19): struct nx_action_fin_timeout. */
269 NXAST_RAW_FIN_TIMEOUT,
271 /* NX1.0+(20): struct nx_action_controller. */
272 NXAST_RAW_CONTROLLER,
274 /* NX1.0+(22): struct nx_action_write_metadata. */
275 NXAST_RAW_WRITE_METADATA,
277 /* NX1.0+(27): struct nx_action_stack. */
278 NXAST_RAW_STACK_PUSH,
280 /* NX1.0+(28): struct nx_action_stack. */
283 /* NX1.0+(29): struct nx_action_sample. */
286 /* NX1.0+(34): struct nx_action_conjunction. */
287 NXAST_RAW_CONJUNCTION,
289 /* NX1.0+(35): struct nx_action_conntrack, ... */
292 /* ## ------------------ ## */
293 /* ## Debugging actions. ## */
294 /* ## ------------------ ## */
296 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
297 /* accepts them only if started with --enable-dummy. */
299 /* NX1.0+(255): void. */
300 NXAST_RAW_DEBUG_RECIRC,
303 /* OpenFlow actions are always a multiple of 8 bytes in length. */
304 #define OFP_ACTION_ALIGN 8
306 /* Define a few functions for working with instructions. */
307 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
308 static inline const struct STRUCT * OVS_UNUSED \
309 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
311 ovs_assert(inst->type == htons(ENUM)); \
312 return ALIGNED_CAST(struct STRUCT *, inst); \
315 static inline void OVS_UNUSED \
316 instruction_init_##ENUM(struct STRUCT *s) \
318 memset(s, 0, sizeof *s); \
319 s->type = htons(ENUM); \
320 s->len = htons(sizeof *s); \
323 static inline struct STRUCT * OVS_UNUSED \
324 instruction_put_##ENUM(struct ofpbuf *buf) \
326 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
327 instruction_init_##ENUM(s); \
333 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
335 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
337 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
338 uint32_t allowed_ovsinsts,
339 enum ofpact_type outer_action);
341 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
342 enum mf_field_id, uint64_t value);
344 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
345 enum ofp_raw_action_type *, uint64_t *arg);
346 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
347 enum ofp_raw_action_type, uint64_t arg);
349 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
350 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
351 bool allow_instructions, enum ofpact_type outer_action);
352 static enum ofperr ofpacts_pull_openflow_actions__(
353 struct ofpbuf *openflow, unsigned int actions_len,
354 enum ofp_version version, uint32_t allowed_ovsinsts,
355 struct ofpbuf *ofpacts, enum ofpact_type outer_action);
356 static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
357 const char *s_, struct ofpbuf *ofpacts,
358 enum ofputil_protocol *usable_protocols,
359 bool allow_instructions, enum ofpact_type outer_action);
361 /* Pull off existing actions or instructions. Used by nesting actions to keep
362 * ofpacts_parse() oblivious of actions nesting.
364 * Push the actions back on after nested parsing, e.g.:
366 * size_t ofs = ofpacts_pull(ofpacts);
367 * ...nested parsing...
368 * ofpbuf_push_uninit(ofpacts, ofs);
371 ofpacts_pull(struct ofpbuf *ofpacts)
377 ofpbuf_pull(ofpacts, ofs);
382 #include "ofp-actions.inc1"
384 /* Output actions. */
386 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
387 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
388 * number of bytes to send. A 'max_len' of zero means no bytes of the
389 * packet should be sent. */
390 struct ofp10_action_output {
391 ovs_be16 type; /* OFPAT10_OUTPUT. */
392 ovs_be16 len; /* Length is 8. */
393 ovs_be16 port; /* Output port. */
394 ovs_be16 max_len; /* Max length to send to controller. */
396 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
398 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
399 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
400 * number of bytes to send. A 'max_len' of zero means no bytes of the
401 * packet should be sent.*/
402 struct ofp11_action_output {
403 ovs_be16 type; /* OFPAT11_OUTPUT. */
404 ovs_be16 len; /* Length is 16. */
405 ovs_be32 port; /* Output port. */
406 ovs_be16 max_len; /* Max length to send to controller. */
407 uint8_t pad[6]; /* Pad to 64 bits. */
409 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
412 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
413 enum ofp_version ofp_version OVS_UNUSED,
416 struct ofpact_output *output;
418 output = ofpact_put_OUTPUT(out);
419 output->port = u16_to_ofp(ntohs(oao->port));
420 output->max_len = ntohs(oao->max_len);
422 return ofpact_check_output_port(output->port, OFPP_MAX);
426 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
427 enum ofp_version ofp_version OVS_UNUSED,
430 struct ofpact_output *output;
433 output = ofpact_put_OUTPUT(out);
434 output->max_len = ntohs(oao->max_len);
436 error = ofputil_port_from_ofp11(oao->port, &output->port);
441 return ofpact_check_output_port(output->port, OFPP_MAX);
445 encode_OUTPUT(const struct ofpact_output *output,
446 enum ofp_version ofp_version, struct ofpbuf *out)
448 if (ofp_version == OFP10_VERSION) {
449 struct ofp10_action_output *oao;
451 oao = put_OFPAT10_OUTPUT(out);
452 oao->port = htons(ofp_to_u16(output->port));
453 oao->max_len = htons(output->max_len);
455 struct ofp11_action_output *oao;
457 oao = put_OFPAT11_OUTPUT(out);
458 oao->port = ofputil_port_to_ofp11(output->port);
459 oao->max_len = htons(output->max_len);
463 static char * OVS_WARN_UNUSED_RESULT
464 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
465 enum ofputil_protocol *usable_protocols OVS_UNUSED)
467 if (strchr(arg, '[')) {
468 struct ofpact_output_reg *output_reg;
470 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
471 output_reg->max_len = UINT16_MAX;
472 return mf_parse_subfield(&output_reg->src, arg);
474 struct ofpact_output *output;
476 output = ofpact_put_OUTPUT(ofpacts);
477 if (!ofputil_port_from_string(arg, &output->port)) {
478 return xasprintf("%s: output to unknown port", arg);
480 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
486 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
488 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
489 ds_put_format(s, "output:%"PRIu16, a->port);
491 ofputil_format_port(a->port, s);
492 if (a->port == OFPP_CONTROLLER) {
493 ds_put_format(s, ":%"PRIu16, a->max_len);
501 decode_OFPAT_RAW11_GROUP(uint32_t group_id,
502 enum ofp_version ofp_version OVS_UNUSED,
505 ofpact_put_GROUP(out)->group_id = group_id;
510 encode_GROUP(const struct ofpact_group *group,
511 enum ofp_version ofp_version, struct ofpbuf *out)
513 if (ofp_version == OFP10_VERSION) {
516 put_OFPAT11_GROUP(out, group->group_id);
520 static char * OVS_WARN_UNUSED_RESULT
521 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
522 enum ofputil_protocol *usable_protocols OVS_UNUSED)
524 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
528 format_GROUP(const struct ofpact_group *a, struct ds *s)
530 ds_put_format(s, "group:%"PRIu32, a->group_id);
533 /* Action structure for NXAST_CONTROLLER.
535 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
536 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
539 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
541 * - 'controller_id': The ID of the controller connection to which the
542 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
543 * sent only to controllers that have the specified controller connection
544 * ID. See "struct nx_controller_id" for more information. */
545 struct nx_action_controller {
546 ovs_be16 type; /* OFPAT_VENDOR. */
547 ovs_be16 len; /* Length is 16. */
548 ovs_be32 vendor; /* NX_VENDOR_ID. */
549 ovs_be16 subtype; /* NXAST_CONTROLLER. */
550 ovs_be16 max_len; /* Maximum length to send to controller. */
551 ovs_be16 controller_id; /* Controller ID to send packet-in. */
552 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
553 uint8_t zero; /* Must be zero. */
555 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
558 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
559 enum ofp_version ofp_version OVS_UNUSED,
562 struct ofpact_controller *oc;
564 oc = ofpact_put_CONTROLLER(out);
565 oc->max_len = ntohs(nac->max_len);
566 oc->controller_id = ntohs(nac->controller_id);
567 oc->reason = nac->reason;
572 encode_CONTROLLER(const struct ofpact_controller *controller,
573 enum ofp_version ofp_version OVS_UNUSED,
576 struct nx_action_controller *nac;
578 nac = put_NXAST_CONTROLLER(out);
579 nac->max_len = htons(controller->max_len);
580 nac->controller_id = htons(controller->controller_id);
581 nac->reason = controller->reason;
584 static char * OVS_WARN_UNUSED_RESULT
585 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
586 enum ofputil_protocol *usable_protocols OVS_UNUSED)
588 enum ofp_packet_in_reason reason = OFPR_ACTION;
589 uint16_t controller_id = 0;
590 uint16_t max_len = UINT16_MAX;
594 } else if (strspn(arg, "0123456789") == strlen(arg)) {
595 char *error = str_to_u16(arg, "max_len", &max_len);
602 while (ofputil_parse_key_value(&arg, &name, &value)) {
603 if (!strcmp(name, "reason")) {
604 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
605 return xasprintf("unknown reason \"%s\"", value);
607 } else if (!strcmp(name, "max_len")) {
608 char *error = str_to_u16(value, "max_len", &max_len);
612 } else if (!strcmp(name, "id")) {
613 char *error = str_to_u16(value, "id", &controller_id);
618 return xasprintf("unknown key \"%s\" parsing controller "
624 if (reason == OFPR_ACTION && controller_id == 0) {
625 struct ofpact_output *output;
627 output = ofpact_put_OUTPUT(ofpacts);
628 output->port = OFPP_CONTROLLER;
629 output->max_len = max_len;
631 struct ofpact_controller *controller;
633 controller = ofpact_put_CONTROLLER(ofpacts);
634 controller->max_len = max_len;
635 controller->reason = reason;
636 controller->controller_id = controller_id;
643 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
645 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
646 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
648 enum ofp_packet_in_reason reason = a->reason;
650 ds_put_cstr(s, "controller(");
651 if (reason != OFPR_ACTION) {
652 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
654 ds_put_format(s, "reason=%s,",
655 ofputil_packet_in_reason_to_string(
656 reason, reasonbuf, sizeof reasonbuf));
658 if (a->max_len != UINT16_MAX) {
659 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
661 if (a->controller_id != 0) {
662 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
669 /* Enqueue action. */
670 struct ofp10_action_enqueue {
671 ovs_be16 type; /* OFPAT10_ENQUEUE. */
672 ovs_be16 len; /* Len is 16. */
673 ovs_be16 port; /* Port that queue belongs. Should
674 refer to a valid physical port
675 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
676 uint8_t pad[6]; /* Pad for 64-bit alignment. */
677 ovs_be32 queue_id; /* Where to enqueue the packets. */
679 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
682 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
683 enum ofp_version ofp_version OVS_UNUSED,
686 struct ofpact_enqueue *enqueue;
688 enqueue = ofpact_put_ENQUEUE(out);
689 enqueue->port = u16_to_ofp(ntohs(oae->port));
690 enqueue->queue = ntohl(oae->queue_id);
691 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
692 && enqueue->port != OFPP_IN_PORT
693 && enqueue->port != OFPP_LOCAL) {
694 return OFPERR_OFPBAC_BAD_OUT_PORT;
700 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
701 enum ofp_version ofp_version, struct ofpbuf *out)
703 if (ofp_version == OFP10_VERSION) {
704 struct ofp10_action_enqueue *oae;
706 oae = put_OFPAT10_ENQUEUE(out);
707 oae->port = htons(ofp_to_u16(enqueue->port));
708 oae->queue_id = htonl(enqueue->queue);
714 static char * OVS_WARN_UNUSED_RESULT
715 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
716 enum ofputil_protocol *usable_protocols OVS_UNUSED)
719 char *port = strtok_r(arg, ":q,", &sp);
720 char *queue = strtok_r(NULL, "", &sp);
721 struct ofpact_enqueue *enqueue;
723 if (port == NULL || queue == NULL) {
724 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
725 "\"enqueue(PORT,QUEUE)\"");
728 enqueue = ofpact_put_ENQUEUE(ofpacts);
729 if (!ofputil_port_from_string(port, &enqueue->port)) {
730 return xasprintf("%s: enqueue to unknown port", port);
732 return str_to_u32(queue, &enqueue->queue);
736 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
738 ds_put_format(s, "enqueue:");
739 ofputil_format_port(a->port, s);
740 ds_put_format(s, ":%"PRIu32, a->queue);
743 /* Action structure for NXAST_OUTPUT_REG.
745 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
747 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
748 * the NXAST_REG_LOAD action.
750 * The acceptable nxm_header values for 'src' are the same as the acceptable
751 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
753 * The 'max_len' field indicates the number of bytes to send when the chosen
754 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
755 * field of OFPAT_OUTPUT.
757 * The 'zero' field is required to be zeroed for forward compatibility. */
758 struct nx_action_output_reg {
759 ovs_be16 type; /* OFPAT_VENDOR. */
760 ovs_be16 len; /* 24. */
761 ovs_be32 vendor; /* NX_VENDOR_ID. */
762 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
764 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
765 ovs_be32 src; /* Source. */
767 ovs_be16 max_len; /* Max length to send to controller. */
769 uint8_t zero[6]; /* Reserved, must be zero. */
771 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
773 /* Action structure for NXAST_OUTPUT_REG2.
775 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
776 * experimenter OXM as 'src'.
778 struct nx_action_output_reg2 {
779 ovs_be16 type; /* OFPAT_VENDOR. */
780 ovs_be16 len; /* 24. */
781 ovs_be32 vendor; /* NX_VENDOR_ID. */
782 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
784 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
785 ovs_be16 max_len; /* Max length to send to controller. */
788 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
789 * - Enough 0-bytes to pad the action out to 24 bytes. */
792 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
795 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
796 enum ofp_version ofp_version OVS_UNUSED,
799 struct ofpact_output_reg *output_reg;
801 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
802 return OFPERR_OFPBAC_BAD_ARGUMENT;
805 output_reg = ofpact_put_OUTPUT_REG(out);
806 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
807 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
808 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
809 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
810 output_reg->max_len = ntohs(naor->max_len);
812 return mf_check_src(&output_reg->src, NULL);
816 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
817 enum ofp_version ofp_version OVS_UNUSED,
820 struct ofpact_output_reg *output_reg;
824 output_reg = ofpact_put_OUTPUT_REG(out);
825 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
826 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
827 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
828 output_reg->max_len = ntohs(naor->max_len);
830 ofpbuf_use_const(&b, naor, ntohs(naor->len));
831 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
832 error = nx_pull_header(&b, &output_reg->src.field, NULL);
836 if (!is_all_zeros(b.data, b.size)) {
837 return OFPERR_NXBRC_MUST_BE_ZERO;
840 return mf_check_src(&output_reg->src, NULL);
844 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
845 enum ofp_version ofp_version OVS_UNUSED,
848 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
849 * cannot be encoded in the older form, encode it as
850 * NXAST_RAW_OUTPUT_REG2. */
851 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
852 || !mf_nxm_header(output_reg->src.field->id)) {
853 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
854 size_t size = out->size;
856 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
857 output_reg->src.n_bits);
858 naor->max_len = htons(output_reg->max_len);
860 out->size = size - sizeof naor->pad;
861 nx_put_header(out, output_reg->src.field->id, 0, false);
864 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
866 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
867 output_reg->src.n_bits);
868 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
869 naor->max_len = htons(output_reg->max_len);
873 static char * OVS_WARN_UNUSED_RESULT
874 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
875 enum ofputil_protocol *usable_protocols OVS_UNUSED)
877 return parse_OUTPUT(arg, ofpacts, usable_protocols);
881 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
883 ds_put_cstr(s, "output:");
884 mf_format_subfield(&a->src, s);
887 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
889 * The bundle actions choose a slave from a supplied list of options.
890 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
891 * selection to a register.
893 * The list of possible slaves follows the nx_action_bundle structure. The size
894 * of each slave is governed by its type as indicated by the 'slave_type'
895 * parameter. The list of slaves should be padded at its end with zeros to make
896 * the total length of the action a multiple of 8.
898 * Switches infer from the 'slave_type' parameter the size of each slave. All
899 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
900 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
901 * 2 byte width. Switches should reject actions which indicate unknown or
902 * unsupported slave types.
904 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
905 * slave. If the switch does not support the specified 'algorithm' parameter,
906 * it should reject the action.
908 * Several algorithms take into account liveness when selecting slaves. The
909 * liveness of a slave is implementation defined (with one exception), but will
910 * generally take into account things like its carrier status and the results
911 * of any link monitoring protocols which happen to be running on it. In order
912 * to give controllers a place-holder value, the OFPP_NONE port is always
915 * Some slave selection strategies require the use of a hash function, in which
916 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
917 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
918 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
919 * 'basis' parameter is used as a universal hash parameter. Different values
920 * of 'basis' yield different hash results.
922 * The 'zero' parameter at the end of the action structure is reserved for
923 * future use. Switches are required to reject actions which have nonzero
924 * bytes in the 'zero' field.
926 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
927 * should reject actions which have nonzero bytes in either of these fields.
929 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
930 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
931 * similar to those for the NXAST_REG_LOAD action. */
932 struct nx_action_bundle {
933 ovs_be16 type; /* OFPAT_VENDOR. */
934 ovs_be16 len; /* Length including slaves. */
935 ovs_be32 vendor; /* NX_VENDOR_ID. */
936 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
938 /* Slave choice algorithm to apply to hash value. */
939 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
941 /* What fields to hash and how. */
942 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
943 ovs_be16 basis; /* Universal hash parameter. */
945 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
946 ovs_be16 n_slaves; /* Number of slaves. */
948 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
949 ovs_be32 dst; /* Destination. */
951 uint8_t zero[4]; /* Reserved. Must be zero. */
953 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
956 decode_bundle(bool load, const struct nx_action_bundle *nab,
957 struct ofpbuf *ofpacts)
959 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
960 struct ofpact_bundle *bundle;
962 size_t slaves_size, i;
965 bundle = ofpact_put_BUNDLE(ofpacts);
967 bundle->n_slaves = ntohs(nab->n_slaves);
968 bundle->basis = ntohs(nab->basis);
969 bundle->fields = ntohs(nab->fields);
970 bundle->algorithm = ntohs(nab->algorithm);
971 slave_type = ntohl(nab->slave_type);
972 slaves_size = ntohs(nab->len) - sizeof *nab;
974 error = OFPERR_OFPBAC_BAD_ARGUMENT;
975 if (!flow_hash_fields_valid(bundle->fields)) {
976 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
977 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
978 VLOG_WARN_RL(&rl, "too many slaves");
979 } else if (bundle->algorithm != NX_BD_ALG_HRW
980 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
981 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
982 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
983 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
988 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
989 VLOG_WARN_RL(&rl, "reserved field is nonzero");
990 error = OFPERR_OFPBAC_BAD_ARGUMENT;
994 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
995 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
996 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
998 if (bundle->dst.n_bits < 16) {
999 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
1001 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1004 if (nab->ofs_nbits || nab->dst) {
1005 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
1006 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1010 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1011 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1012 "allocated for slaves. %"PRIuSIZE" bytes are required "
1013 "for %"PRIu16" slaves.",
1014 load ? "bundle_load" : "bundle", slaves_size,
1015 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1016 error = OFPERR_OFPBAC_BAD_LEN;
1019 for (i = 0; i < bundle->n_slaves; i++) {
1020 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1021 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1024 bundle = ofpacts->header;
1025 ofpact_update_len(ofpacts, &bundle->ofpact);
1028 error = bundle_check(bundle, OFPP_MAX, NULL);
1034 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1035 enum ofp_version ofp_version OVS_UNUSED,
1038 return decode_bundle(false, nab, out);
1042 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1043 enum ofp_version ofp_version OVS_UNUSED,
1046 return decode_bundle(true, nab, out);
1050 encode_BUNDLE(const struct ofpact_bundle *bundle,
1051 enum ofp_version ofp_version OVS_UNUSED,
1054 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1055 struct nx_action_bundle *nab;
1059 nab = (bundle->dst.field
1060 ? put_NXAST_BUNDLE_LOAD(out)
1061 : put_NXAST_BUNDLE(out));
1062 nab->len = htons(ntohs(nab->len) + slaves_len);
1063 nab->algorithm = htons(bundle->algorithm);
1064 nab->fields = htons(bundle->fields);
1065 nab->basis = htons(bundle->basis);
1066 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1067 nab->n_slaves = htons(bundle->n_slaves);
1068 if (bundle->dst.field) {
1069 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1070 bundle->dst.n_bits);
1071 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1074 slaves = ofpbuf_put_zeros(out, slaves_len);
1075 for (i = 0; i < bundle->n_slaves; i++) {
1076 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1080 static char * OVS_WARN_UNUSED_RESULT
1081 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1082 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1084 return bundle_parse(arg, ofpacts);
1087 static char * OVS_WARN_UNUSED_RESULT
1088 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1090 return bundle_parse_load(arg, ofpacts);
1094 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1096 bundle_format(a, s);
1099 /* Set VLAN actions. */
1102 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1105 return OFPERR_OFPBAC_BAD_ARGUMENT;
1107 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1108 vlan_vid->vlan_vid = vid;
1109 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1115 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1116 enum ofp_version ofp_version OVS_UNUSED,
1119 return decode_set_vlan_vid(vid, true, out);
1123 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1124 enum ofp_version ofp_version OVS_UNUSED,
1127 return decode_set_vlan_vid(vid, false, out);
1131 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1132 enum ofp_version ofp_version, struct ofpbuf *out)
1134 uint16_t vid = vlan_vid->vlan_vid;
1136 /* Push a VLAN tag, if none is present and this form of the action calls
1137 * for such a feature. */
1138 if (ofp_version > OFP10_VERSION
1139 && vlan_vid->push_vlan_if_needed
1140 && !vlan_vid->flow_has_vlan) {
1141 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1144 if (ofp_version == OFP10_VERSION) {
1145 put_OFPAT10_SET_VLAN_VID(out, vid);
1146 } else if (ofp_version == OFP11_VERSION) {
1147 put_OFPAT11_SET_VLAN_VID(out, vid);
1149 ofpact_put_set_field(out, ofp_version,
1150 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1154 static char * OVS_WARN_UNUSED_RESULT
1155 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1157 struct ofpact_vlan_vid *vlan_vid;
1161 error = str_to_u16(arg, "VLAN VID", &vid);
1166 if (vid & ~VLAN_VID_MASK) {
1167 return xasprintf("%s: not a valid VLAN VID", arg);
1169 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1170 vlan_vid->vlan_vid = vid;
1171 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1175 static char * OVS_WARN_UNUSED_RESULT
1176 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1177 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1179 return parse_set_vlan_vid(arg, ofpacts, false);
1183 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1185 ds_put_format(s, "%s:%"PRIu16,
1186 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1190 /* Set PCP actions. */
1193 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1196 return OFPERR_OFPBAC_BAD_ARGUMENT;
1198 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1199 vlan_pcp->vlan_pcp = pcp;
1200 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1206 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1207 enum ofp_version ofp_version OVS_UNUSED,
1210 return decode_set_vlan_pcp(pcp, true, out);
1214 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1215 enum ofp_version ofp_version OVS_UNUSED,
1218 return decode_set_vlan_pcp(pcp, false, out);
1222 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1223 enum ofp_version ofp_version, struct ofpbuf *out)
1225 uint8_t pcp = vlan_pcp->vlan_pcp;
1227 /* Push a VLAN tag, if none is present and this form of the action calls
1228 * for such a feature. */
1229 if (ofp_version > OFP10_VERSION
1230 && vlan_pcp->push_vlan_if_needed
1231 && !vlan_pcp->flow_has_vlan) {
1232 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1235 if (ofp_version == OFP10_VERSION) {
1236 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1237 } else if (ofp_version == OFP11_VERSION) {
1238 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1240 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1244 static char * OVS_WARN_UNUSED_RESULT
1245 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1247 struct ofpact_vlan_pcp *vlan_pcp;
1251 error = str_to_u8(arg, "VLAN PCP", &pcp);
1257 return xasprintf("%s: not a valid VLAN PCP", arg);
1259 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1260 vlan_pcp->vlan_pcp = pcp;
1261 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1265 static char * OVS_WARN_UNUSED_RESULT
1266 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1267 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1269 return parse_set_vlan_pcp(arg, ofpacts, false);
1273 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1275 ds_put_format(s, "%s:%"PRIu8,
1276 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1280 /* Strip VLAN actions. */
1283 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1285 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1290 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1292 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1297 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1298 enum ofp_version ofp_version, struct ofpbuf *out)
1300 if (ofp_version == OFP10_VERSION) {
1301 put_OFPAT10_STRIP_VLAN(out);
1303 put_OFPAT11_POP_VLAN(out);
1307 static char * OVS_WARN_UNUSED_RESULT
1308 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1309 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1311 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1315 static char * OVS_WARN_UNUSED_RESULT
1316 parse_pop_vlan(struct ofpbuf *ofpacts)
1318 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1323 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1325 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1330 /* Push VLAN action. */
1333 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1334 enum ofp_version ofp_version OVS_UNUSED,
1337 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1338 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1339 return OFPERR_OFPBAC_BAD_ARGUMENT;
1341 ofpact_put_PUSH_VLAN(out);
1346 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1347 enum ofp_version ofp_version, struct ofpbuf *out)
1349 if (ofp_version == OFP10_VERSION) {
1350 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1351 * follow this action. */
1353 /* XXX ETH_TYPE_VLAN_8021AD case */
1354 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1358 static char * OVS_WARN_UNUSED_RESULT
1359 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1360 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1365 *usable_protocols &= OFPUTIL_P_OF11_UP;
1366 error = str_to_u16(arg, "ethertype", ðertype);
1371 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1372 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1373 return xasprintf("%s: not a valid VLAN ethertype", arg);
1376 ofpact_put_PUSH_VLAN(ofpacts);
1381 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1383 /* XXX 802.1AD case*/
1384 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1387 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1388 struct ofp_action_dl_addr {
1389 ovs_be16 type; /* Type. */
1390 ovs_be16 len; /* Length is 16. */
1391 struct eth_addr dl_addr; /* Ethernet address. */
1394 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1397 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1398 enum ofp_version ofp_version OVS_UNUSED,
1401 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1406 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1407 enum ofp_version ofp_version OVS_UNUSED,
1410 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1415 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1416 enum ofp_raw_action_type raw, enum mf_field_id field,
1419 if (ofp_version < OFP12_VERSION) {
1420 struct ofp_action_dl_addr *oada;
1422 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1423 oada->dl_addr = mac->mac;
1425 ofpact_put_set_field(out, ofp_version, field,
1426 eth_addr_to_uint64(mac->mac));
1431 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1434 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1440 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1441 enum ofp_version ofp_version,
1444 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1449 static char * OVS_WARN_UNUSED_RESULT
1450 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1451 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1453 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1456 static char * OVS_WARN_UNUSED_RESULT
1457 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1458 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1460 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1464 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1466 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1470 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1472 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1475 /* Set IPv4 address actions. */
1478 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1479 enum ofp_version ofp_version OVS_UNUSED,
1482 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1487 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1488 enum ofp_version ofp_version OVS_UNUSED,
1491 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1496 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1497 enum ofp_version ofp_version,
1498 enum ofp_raw_action_type raw, enum mf_field_id field,
1501 ovs_be32 addr = ipv4->ipv4;
1502 if (ofp_version < OFP12_VERSION) {
1503 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1505 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1510 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1511 enum ofp_version ofp_version, struct ofpbuf *out)
1513 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1518 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1519 enum ofp_version ofp_version, struct ofpbuf *out)
1521 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1525 static char * OVS_WARN_UNUSED_RESULT
1526 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1527 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1529 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1532 static char * OVS_WARN_UNUSED_RESULT
1533 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1534 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1536 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1540 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1542 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1546 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1548 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1551 /* Set IPv4/v6 TOS actions. */
1554 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1555 enum ofp_version ofp_version OVS_UNUSED,
1558 if (dscp & ~IP_DSCP_MASK) {
1559 return OFPERR_OFPBAC_BAD_ARGUMENT;
1561 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1567 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1568 enum ofp_version ofp_version, struct ofpbuf *out)
1570 if (ofp_version < OFP12_VERSION) {
1571 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1573 ofpact_put_set_field(out, ofp_version,
1574 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1578 static char * OVS_WARN_UNUSED_RESULT
1579 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1580 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1585 error = str_to_u8(arg, "TOS", &tos);
1590 if (tos & ~IP_DSCP_MASK) {
1591 return xasprintf("%s: not a valid TOS", arg);
1593 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1598 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1600 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1603 /* Set IPv4/v6 ECN actions. */
1606 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1607 enum ofp_version ofp_version OVS_UNUSED,
1610 if (ecn & ~IP_ECN_MASK) {
1611 return OFPERR_OFPBAC_BAD_ARGUMENT;
1613 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1619 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1620 enum ofp_version ofp_version, struct ofpbuf *out)
1622 uint8_t ecn = ip_ecn->ecn;
1623 if (ofp_version == OFP10_VERSION) {
1625 } else if (ofp_version == OFP11_VERSION) {
1626 put_OFPAT11_SET_NW_ECN(out, ecn);
1628 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1632 static char * OVS_WARN_UNUSED_RESULT
1633 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1634 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1639 error = str_to_u8(arg, "ECN", &ecn);
1644 if (ecn & ~IP_ECN_MASK) {
1645 return xasprintf("%s: not a valid ECN", arg);
1647 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1652 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1654 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1657 /* Set IPv4/v6 TTL actions. */
1660 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1661 enum ofp_version ofp_version OVS_UNUSED,
1664 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1669 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1670 enum ofp_version ofp_version, struct ofpbuf *out)
1672 if (ofp_version >= OFP11_VERSION) {
1673 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1679 static char * OVS_WARN_UNUSED_RESULT
1680 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1681 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1686 error = str_to_u8(arg, "TTL", &ttl);
1691 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1696 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1698 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1701 /* Set TCP/UDP/SCTP port actions. */
1704 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1705 enum ofp_version ofp_version OVS_UNUSED,
1708 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1713 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1714 enum ofp_version ofp_version OVS_UNUSED,
1717 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1722 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1723 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1724 enum mf_field_id field, struct ofpbuf *out)
1726 uint16_t port = l4_port->port;
1728 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1729 ofpact_put_set_field(out, ofp_version, field, port);
1731 ofpact_put_raw(out, ofp_version, raw, port);
1736 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1737 enum ofp_version ofp_version, struct ofpbuf *out)
1739 uint8_t proto = l4_port->flow_ip_proto;
1740 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1741 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1742 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1745 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1749 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1750 enum ofp_version ofp_version,
1753 uint8_t proto = l4_port->flow_ip_proto;
1754 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1755 : proto == IPPROTO_UDP ? MFF_UDP_DST
1756 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1759 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1762 static char * OVS_WARN_UNUSED_RESULT
1763 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1764 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1766 return str_to_u16(arg, "source port",
1767 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1770 static char * OVS_WARN_UNUSED_RESULT
1771 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1772 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1774 return str_to_u16(arg, "destination port",
1775 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1779 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1781 ds_put_format(s, "mod_tp_src:%d", a->port);
1785 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1787 ds_put_format(s, "mod_tp_dst:%d", a->port);
1790 /* Action structure for OFPAT_COPY_FIELD. */
1791 struct ofp15_action_copy_field {
1792 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1793 ovs_be16 len; /* Length is padded to 64 bits. */
1794 ovs_be16 n_bits; /* Number of bits to copy. */
1795 ovs_be16 src_offset; /* Starting bit offset in source. */
1796 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1799 * - OXM header for source field.
1800 * - OXM header for destination field.
1801 * - Padding with 0-bytes to a multiple of 8 bytes.
1802 * The "pad2" member is the beginning of the above. */
1805 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1807 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1808 struct onf_action_copy_field {
1809 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
1810 ovs_be16 len; /* Length is padded to 64 bits. */
1811 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
1812 ovs_be16 exp_type; /* 3200. */
1813 uint8_t pad[2]; /* Not used. */
1814 ovs_be16 n_bits; /* Number of bits to copy. */
1815 ovs_be16 src_offset; /* Starting bit offset in source. */
1816 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1817 uint8_t pad2[2]; /* Not used. */
1819 * - OXM header for source field.
1820 * - OXM header for destination field.
1821 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1822 * The "pad3" member is the beginning of the above. */
1823 uint8_t pad3[4]; /* Not used. */
1825 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1827 /* Action structure for NXAST_REG_MOVE.
1829 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1830 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1831 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1832 * the next most significant bit, and so on.
1834 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1835 * sense to use nxm_hasmask=1 because the action does not do any kind of
1836 * matching; it uses the actual value of a field.)
1838 * The following nxm_header values are potentially acceptable as 'src':
1853 * - NXM_OF_ICMP_TYPE
1854 * - NXM_OF_ICMP_CODE
1861 * - NXM_NX_ICMPV6_TYPE
1862 * - NXM_NX_ICMPV6_CODE
1865 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1867 * - NXM_NX_TUN_IPV4_SRC
1868 * - NXM_NX_TUN_IPV4_DST
1870 * The following nxm_header values are potentially acceptable as 'dst':
1886 * Modifying any of the above fields changes the corresponding packet
1891 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1895 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1896 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1897 * header (if any), ignoring the other bits. Setting a value with CFI=1
1898 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1899 * to the field's new value (with the CFI bit masked out).
1901 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1902 * any of these values modifies the corresponding tunnel header field used
1903 * for the packet's next tunnel encapsulation, if allowed by the
1904 * configuration of the output tunnel port.
1906 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1907 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1908 * used only if the flow's nx_match includes an nxm_entry that specifies
1909 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1911 * The switch will reject actions for which src_ofs+n_bits is greater than the
1912 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1913 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1915 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1916 * behaves as if 'src' were copied out to a temporary buffer, then the
1917 * temporary buffer copied to 'dst'.
1919 struct nx_action_reg_move {
1920 ovs_be16 type; /* OFPAT_VENDOR. */
1921 ovs_be16 len; /* Length is 24. */
1922 ovs_be32 vendor; /* NX_VENDOR_ID. */
1923 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1924 ovs_be16 n_bits; /* Number of bits. */
1925 ovs_be16 src_ofs; /* Starting bit offset in source. */
1926 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1928 * - OXM/NXM header for source field (4 or 8 bytes).
1929 * - OXM/NXM header for destination field (4 or 8 bytes).
1930 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
1932 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
1935 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
1936 const void *action, ovs_be16 action_len, size_t oxm_offset,
1937 struct ofpbuf *ofpacts)
1939 struct ofpact_reg_move *move;
1943 move = ofpact_put_REG_MOVE(ofpacts);
1944 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
1945 move->src.ofs = ntohs(src_offset);
1946 move->src.n_bits = ntohs(n_bits);
1947 move->dst.ofs = ntohs(dst_offset);
1948 move->dst.n_bits = ntohs(n_bits);
1950 ofpbuf_use_const(&b, action, ntohs(action_len));
1951 ofpbuf_pull(&b, oxm_offset);
1952 error = nx_pull_header(&b, &move->src.field, NULL);
1956 error = nx_pull_header(&b, &move->dst.field, NULL);
1961 if (!is_all_zeros(b.data, b.size)) {
1962 return OFPERR_NXBRC_MUST_BE_ZERO;
1965 return nxm_reg_move_check(move, NULL);
1969 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
1970 enum ofp_version ofp_version OVS_UNUSED,
1971 struct ofpbuf *ofpacts)
1973 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1974 oacf->n_bits, oacf, oacf->len,
1975 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
1979 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
1980 enum ofp_version ofp_version OVS_UNUSED,
1981 struct ofpbuf *ofpacts)
1983 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1984 oacf->n_bits, oacf, oacf->len,
1985 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
1989 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
1990 enum ofp_version ofp_version OVS_UNUSED,
1991 struct ofpbuf *ofpacts)
1993 struct ofpact_reg_move *move;
1997 move = ofpact_put_REG_MOVE(ofpacts);
1998 move->ofpact.raw = NXAST_RAW_REG_MOVE;
1999 move->src.ofs = ntohs(narm->src_ofs);
2000 move->src.n_bits = ntohs(narm->n_bits);
2001 move->dst.ofs = ntohs(narm->dst_ofs);
2002 move->dst.n_bits = ntohs(narm->n_bits);
2004 ofpbuf_use_const(&b, narm, ntohs(narm->len));
2005 ofpbuf_pull(&b, sizeof *narm);
2006 error = nx_pull_header(&b, &move->src.field, NULL);
2010 error = nx_pull_header(&b, &move->dst.field, NULL);
2014 if (!is_all_zeros(b.data, b.size)) {
2015 return OFPERR_NXBRC_MUST_BE_ZERO;
2018 return nxm_reg_move_check(move, NULL);
2022 encode_REG_MOVE(const struct ofpact_reg_move *move,
2023 enum ofp_version ofp_version, struct ofpbuf *out)
2025 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2026 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2027 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2028 * probe for support. Until we have that ability, we currently prefer
2029 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2031 size_t start_ofs = out->size;
2032 if (ofp_version >= OFP15_VERSION) {
2033 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2034 copy->n_bits = htons(move->dst.n_bits);
2035 copy->src_offset = htons(move->src.ofs);
2036 copy->dst_offset = htons(move->dst.ofs);
2037 out->size = out->size - sizeof copy->pad2;
2038 nx_put_header(out, move->src.field->id, ofp_version, false);
2039 nx_put_header(out, move->dst.field->id, ofp_version, false);
2040 } else if (ofp_version == OFP13_VERSION
2041 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2042 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2043 copy->n_bits = htons(move->dst.n_bits);
2044 copy->src_offset = htons(move->src.ofs);
2045 copy->dst_offset = htons(move->dst.ofs);
2046 out->size = out->size - sizeof copy->pad3;
2047 nx_put_header(out, move->src.field->id, ofp_version, false);
2048 nx_put_header(out, move->dst.field->id, ofp_version, false);
2050 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2051 narm->n_bits = htons(move->dst.n_bits);
2052 narm->src_ofs = htons(move->src.ofs);
2053 narm->dst_ofs = htons(move->dst.ofs);
2054 nx_put_header(out, move->src.field->id, 0, false);
2055 nx_put_header(out, move->dst.field->id, 0, false);
2057 pad_ofpat(out, start_ofs);
2060 static char * OVS_WARN_UNUSED_RESULT
2061 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2062 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2064 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2065 const char *full_arg = arg;
2068 error = mf_parse_subfield__(&move->src, &arg);
2072 if (strncmp(arg, "->", 2)) {
2073 return xasprintf("%s: missing `->' following source", full_arg);
2076 error = mf_parse_subfield(&move->dst, arg);
2081 if (move->src.n_bits != move->dst.n_bits) {
2082 return xasprintf("%s: source field is %d bits wide but destination is "
2083 "%d bits wide", full_arg,
2084 move->src.n_bits, move->dst.n_bits);
2090 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2092 nxm_format_reg_move(a, s);
2095 /* Action structure for OFPAT12_SET_FIELD. */
2096 struct ofp12_action_set_field {
2097 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2098 ovs_be16 len; /* Length is padded to 64 bits. */
2101 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2102 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2104 * The "pad" member is the beginning of the above. */
2107 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2109 /* Action structure for NXAST_REG_LOAD.
2111 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2112 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2113 * starts at 0 for the least-significant bit, 1 for the next most significant
2116 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2117 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2120 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2121 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2122 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2123 * also stored as one less than its true value:
2126 * +------------------------------+------------------+
2127 * | ofs | n_bits - 1 |
2128 * +------------------------------+------------------+
2130 * The switch will reject actions for which ofs+n_bits is greater than the
2131 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2132 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2133 * OFPBAC_BAD_ARGUMENT.
2135 struct nx_action_reg_load {
2136 ovs_be16 type; /* OFPAT_VENDOR. */
2137 ovs_be16 len; /* Length is 24. */
2138 ovs_be32 vendor; /* NX_VENDOR_ID. */
2139 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2140 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2141 ovs_be32 dst; /* Destination register. */
2142 ovs_be64 value; /* Immediate value. */
2144 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2146 /* Action structure for NXAST_REG_LOAD2.
2148 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2149 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2150 * OXM experimenter fields. */
2151 struct nx_action_reg_load2 {
2152 ovs_be16 type; /* OFPAT_VENDOR. */
2153 ovs_be16 len; /* At least 16. */
2154 ovs_be32 vendor; /* NX_VENDOR_ID. */
2155 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2158 * - An NXM/OXM header, value, and optionally a mask.
2159 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2161 * The "pad" member is the beginning of the above. */
2164 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2167 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2168 bool may_mask, struct ofpbuf *ofpacts)
2170 struct ofpact_set_field *sf;
2174 sf = ofpact_put_SET_FIELD(ofpacts);
2176 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2177 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2178 error = nx_pull_entry(&b, &sf->field, &sf->value,
2179 may_mask ? &sf->mask : NULL);
2181 return (error == OFPERR_OFPBMC_BAD_MASK
2182 ? OFPERR_OFPBAC_BAD_SET_MASK
2186 memset(&sf->mask, 0xff, sf->field->n_bytes);
2189 if (!is_all_zeros(b.data, b.size)) {
2190 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2193 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2195 if (sf->field->id == MFF_IN_PORT_OXM) {
2196 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2199 /* oxm_length is now validated to be compatible with mf_value. */
2200 if (!sf->field->writable) {
2201 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2203 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2206 /* The value must be valid for match. OpenFlow 1.5 also says,
2207 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2208 * a 1-bit in oxm_value and in oxm_mask." */
2209 if (!mf_is_value_valid(sf->field, &sf->value)
2210 || (sf->field->id == MFF_VLAN_VID
2211 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2212 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2213 struct ds ds = DS_EMPTY_INITIALIZER;
2214 mf_format(sf->field, &sf->value, NULL, &ds);
2215 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2216 sf->field->name, ds_cstr(&ds));
2219 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2225 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2226 enum ofp_version ofp_version OVS_UNUSED,
2227 struct ofpbuf *ofpacts)
2229 return decode_ofpat_set_field(oasf, false, ofpacts);
2233 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2234 enum ofp_version ofp_version OVS_UNUSED,
2235 struct ofpbuf *ofpacts)
2237 return decode_ofpat_set_field(oasf, true, ofpacts);
2241 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2242 enum ofp_version ofp_version OVS_UNUSED,
2245 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2246 struct mf_subfield dst;
2249 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2251 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2252 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2253 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2254 error = mf_check_dst(&dst, NULL);
2259 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2261 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2262 return OFPERR_OFPBAC_BAD_ARGUMENT;
2265 sf->field = dst.field;
2266 bitwise_put(ntohll(narl->value),
2267 &sf->value, dst.field->n_bytes, dst.ofs,
2269 bitwise_put(UINT64_MAX,
2270 &sf->mask, dst.field->n_bytes, dst.ofs,
2277 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2278 enum ofp_version ofp_version OVS_UNUSED,
2281 struct ofpact_set_field *sf;
2285 sf = ofpact_put_SET_FIELD(out);
2286 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2288 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2289 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2290 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2294 if (!is_all_zeros(b.data, b.size)) {
2295 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2298 if (!sf->field->writable) {
2299 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2301 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2307 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2308 enum mf_field_id field, uint64_t value_)
2310 struct ofp12_action_set_field *oasf OVS_UNUSED;
2311 int n_bytes = mf_from_id(field)->n_bytes;
2312 size_t start_ofs = openflow->size;
2313 union mf_value value;
2315 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2317 oasf = put_OFPAT12_SET_FIELD(openflow);
2318 openflow->size = openflow->size - sizeof oasf->pad;
2319 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2320 pad_ofpat(openflow, start_ofs);
2324 next_load_segment(const struct ofpact_set_field *sf,
2325 struct mf_subfield *dst, uint64_t *value)
2327 int n_bits = sf->field->n_bits;
2328 int n_bytes = sf->field->n_bytes;
2329 int start = dst->ofs + dst->n_bits;
2331 if (start < n_bits) {
2332 dst->field = sf->field;
2333 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2334 if (dst->ofs < n_bits) {
2335 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2336 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2337 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2344 /* Convert 'sf' to a series of REG_LOADs. */
2346 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2348 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2349 * experimenter OXM or is variable length (or if it came in as
2350 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2351 * NXAST_REG_LOAD, which is backward compatible. */
2352 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2353 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2354 struct nx_action_reg_load2 *narl OVS_UNUSED;
2355 size_t start_ofs = openflow->size;
2357 narl = put_NXAST_REG_LOAD2(openflow);
2358 openflow->size = openflow->size - sizeof narl->pad;
2359 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2360 pad_ofpat(openflow, start_ofs);
2362 struct mf_subfield dst;
2365 dst.ofs = dst.n_bits = 0;
2366 while (next_load_segment(sf, &dst, &value)) {
2367 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2368 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2369 narl->dst = htonl(mf_nxm_header(dst.field->id));
2370 narl->value = htonll(value);
2375 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2376 * actions, if we can, falling back to Nicira extensions if we must.
2378 * We check only meta-flow types that can appear within set field actions and
2379 * that have a mapping to compatible action types. These struct mf_field
2380 * definitions have a defined OXM or NXM header value and specify the field as
2383 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2384 enum ofp_version ofp_version,
2387 switch ((int) sf->field->id) {
2388 case MFF_VLAN_TCI: {
2389 ovs_be16 tci = sf->value.be16;
2390 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2391 uint16_t vid = vlan_tci_to_vid(tci);
2392 uint8_t pcp = vlan_tci_to_pcp(tci);
2394 if (ofp_version < OFP11_VERSION) {
2395 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2397 * If CFI=1, Add or modify VLAN VID & PCP.
2398 * If CFI=0, strip VLAN header, if any.
2401 put_OFPAT10_SET_VLAN_VID(out, vid);
2402 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2404 put_OFPAT10_STRIP_VLAN(out);
2407 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2409 * If CFI=1, Add or modify VLAN VID & PCP.
2410 * OpenFlow 1.1 set actions only apply if the packet
2411 * already has VLAN tags. To be sure that is the case
2412 * we have to push a VLAN header. As we do not support
2413 * multiple layers of VLANs, this is a no-op, if a VLAN
2414 * header already exists. This may backfire, however,
2415 * when we start supporting multiple layers of VLANs.
2416 * If CFI=0, strip VLAN header, if any.
2419 /* Push a VLAN tag, if one was not seen at action validation
2421 if (!sf->flow_has_vlan) {
2422 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2424 put_OFPAT11_SET_VLAN_VID(out, vid);
2425 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2427 /* If the flow did not match on vlan, we have no way of
2428 * knowing if the vlan tag exists, so we must POP just to be
2430 put_OFPAT11_POP_VLAN(out);
2436 case MFF_VLAN_VID: {
2437 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2438 if (ofp_version == OFP10_VERSION) {
2439 put_OFPAT10_SET_VLAN_VID(out, vid);
2441 put_OFPAT11_SET_VLAN_VID(out, vid);
2447 if (ofp_version == OFP10_VERSION) {
2448 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2450 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2455 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2459 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2463 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2467 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2471 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2474 case MFF_IP_DSCP_SHIFTED:
2475 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2480 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2485 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2489 set_field_to_nxast(sf, out);
2495 set_field_to_set_field(const struct ofpact_set_field *sf,
2496 enum ofp_version ofp_version, struct ofpbuf *out)
2498 struct ofp12_action_set_field *oasf OVS_UNUSED;
2499 size_t start_ofs = out->size;
2501 oasf = put_OFPAT12_SET_FIELD(out);
2502 out->size = out->size - sizeof oasf->pad;
2503 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2504 pad_ofpat(out, start_ofs);
2508 encode_SET_FIELD(const struct ofpact_set_field *sf,
2509 enum ofp_version ofp_version, struct ofpbuf *out)
2511 if (ofp_version >= OFP15_VERSION) {
2512 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2514 set_field_to_set_field(sf, ofp_version, out);
2515 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2516 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2517 /* It came in as reg_load, send it out the same way. */
2518 set_field_to_nxast(sf, out);
2519 } else if (ofp_version < OFP12_VERSION) {
2520 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2521 set_field_to_legacy_openflow(sf, ofp_version, out);
2522 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2523 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2524 * entire field, so encode it as OFPAT_SET_FIELD. */
2525 set_field_to_set_field(sf, ofp_version, out);
2527 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2528 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2529 * so encode it as reg_load. */
2530 set_field_to_nxast(sf, out);
2534 /* Parses the input argument 'arg' into the key, value, and delimiter
2535 * components that are common across the reg_load and set_field action format.
2537 * With an argument like "1->metadata", sets the following pointers to
2538 * point within 'arg':
2543 * Returns NULL if successful, otherwise a malloc()'d string describing the
2544 * error. The caller is responsible for freeing the returned string. */
2545 static char * OVS_WARN_UNUSED_RESULT
2546 set_field_split_str(char *arg, char **key, char **value, char **delim)
2551 value_end = strstr(arg, "->");
2552 *key = value_end + strlen("->");
2558 return xasprintf("%s: missing `->'", arg);
2560 if (strlen(value_end) <= strlen("->")) {
2561 return xasprintf("%s: missing field name following `->'", arg);
2567 /* Parses a "set_field" action with argument 'arg', appending the parsed
2568 * action to 'ofpacts'.
2570 * Returns NULL if successful, otherwise a malloc()'d string describing the
2571 * error. The caller is responsible for freeing the returned string. */
2572 static char * OVS_WARN_UNUSED_RESULT
2573 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2574 enum ofputil_protocol *usable_protocols)
2576 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2580 const struct mf_field *mf;
2583 error = set_field_split_str(arg, &key, &value, &delim);
2588 mf = mf_from_name(key);
2590 return xasprintf("%s is not a valid OXM field name", key);
2592 if (!mf->writable) {
2593 return xasprintf("%s is read-only", key);
2597 error = mf_parse(mf, value, &sf->value, &sf->mask);
2602 if (!mf_is_value_valid(mf, &sf->value)) {
2603 return xasprintf("%s is not a valid value for field %s", value, key);
2606 *usable_protocols &= mf->usable_protocols_exact;
2610 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2611 * action to 'ofpacts'.
2613 * Returns NULL if successful, otherwise a malloc()'d string describing the
2614 * error. The caller is responsible for freeing the returned string. */
2615 static char * OVS_WARN_UNUSED_RESULT
2616 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2617 enum ofputil_protocol *usable_protocols)
2619 char *copy = xstrdup(arg);
2620 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2625 static char * OVS_WARN_UNUSED_RESULT
2626 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2628 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2629 struct mf_subfield dst;
2630 char *key, *value_str;
2631 union mf_value value;
2634 error = set_field_split_str(arg, &key, &value_str, NULL);
2639 error = mf_parse_subfield(&dst, key);
2644 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2646 return xasprintf("%s: cannot parse integer value", arg);
2649 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2650 dst.field->n_bytes * 8 - dst.n_bits)) {
2654 mf_format(dst.field, &value, NULL, &ds);
2655 error = xasprintf("%s: value %s does not fit into %d bits",
2656 arg, ds_cstr(&ds), dst.n_bits);
2661 sf->field = dst.field;
2662 memset(&sf->value, 0, sizeof sf->value);
2663 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2664 dst.field->n_bytes, dst.ofs, dst.n_bits);
2665 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2671 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2673 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2674 struct mf_subfield dst;
2677 dst.ofs = dst.n_bits = 0;
2678 while (next_load_segment(a, &dst, &value)) {
2679 ds_put_format(s, "load:%#"PRIx64"->", value);
2680 mf_format_subfield(&dst, s);
2681 ds_put_char(s, ',');
2685 ds_put_cstr(s, "set_field:");
2686 mf_format(a->field, &a->value, &a->mask, s);
2687 ds_put_format(s, "->%s", a->field->name);
2691 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2692 * is marked such that, if possible, it will be translated to OpenFlow as
2693 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2694 * that was the way that the action was expressed when it came into OVS or for
2695 * backward compatibility. */
2696 struct ofpact_set_field *
2697 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2699 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2700 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2704 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2706 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2709 struct nx_action_stack {
2710 ovs_be16 type; /* OFPAT_VENDOR. */
2711 ovs_be16 len; /* Length is 16. */
2712 ovs_be32 vendor; /* NX_VENDOR_ID. */
2713 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2714 ovs_be16 offset; /* Bit offset into the field. */
2716 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2717 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2718 * - Enough 0-bytes to pad out the action to 24 bytes. */
2719 uint8_t pad[12]; /* See above. */
2721 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2724 decode_stack_action(const struct nx_action_stack *nasp,
2725 struct ofpact_stack *stack_action)
2730 stack_action->subfield.ofs = ntohs(nasp->offset);
2732 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2733 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2734 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2738 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2740 if (!is_all_zeros(b.data, b.size)) {
2741 return OFPERR_NXBRC_MUST_BE_ZERO;
2748 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2749 enum ofp_version ofp_version OVS_UNUSED,
2750 struct ofpbuf *ofpacts)
2752 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2753 enum ofperr error = decode_stack_action(nasp, push);
2754 return error ? error : nxm_stack_push_check(push, NULL);
2758 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2759 enum ofp_version ofp_version OVS_UNUSED,
2760 struct ofpbuf *ofpacts)
2762 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2763 enum ofperr error = decode_stack_action(nasp, pop);
2764 return error ? error : nxm_stack_pop_check(pop, NULL);
2768 encode_STACK_op(const struct ofpact_stack *stack_action,
2769 struct nx_action_stack *nasp)
2774 nasp->offset = htons(stack_action->subfield.ofs);
2776 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2777 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2778 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2779 n_bits = htons(stack_action->subfield.n_bits);
2780 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2784 encode_STACK_PUSH(const struct ofpact_stack *stack,
2785 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2787 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2791 encode_STACK_POP(const struct ofpact_stack *stack,
2792 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2794 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2797 static char * OVS_WARN_UNUSED_RESULT
2798 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2799 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2801 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2804 static char * OVS_WARN_UNUSED_RESULT
2805 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2806 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2808 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2812 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2814 nxm_format_stack_push(a, s);
2818 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2820 nxm_format_stack_pop(a, s);
2823 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2825 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2826 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2827 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2828 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2830 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2831 * sent only to controllers with id 0.)
2833 struct nx_action_cnt_ids {
2834 ovs_be16 type; /* OFPAT_VENDOR. */
2835 ovs_be16 len; /* Length including slaves. */
2836 ovs_be32 vendor; /* NX_VENDOR_ID. */
2837 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2839 ovs_be16 n_controllers; /* Number of controllers. */
2840 uint8_t zeros[4]; /* Must be zero. */
2842 /* Followed by 1 or more controller ids.
2844 * uint16_t cnt_ids[]; // Controller ids.
2845 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2848 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2851 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2854 struct ofpact_cnt_ids *ids;
2855 enum ofperr error = 0;
2857 ids = ofpact_put_DEC_TTL(out);
2858 ids->n_controllers = 1;
2859 ofpbuf_put(out, &id, sizeof id);
2861 ofpact_update_len(out, &ids->ofpact);
2866 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2867 enum ofp_version ofp_version OVS_UNUSED,
2870 struct ofpact_cnt_ids *ids;
2874 ids = ofpact_put_DEC_TTL(out);
2875 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2876 ids->n_controllers = ntohs(nac_ids->n_controllers);
2877 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2879 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2880 return OFPERR_NXBRC_MUST_BE_ZERO;
2883 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2884 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2885 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2886 "are required for %"PRIu16" controllers.",
2887 ids_size, ids->n_controllers * sizeof(ovs_be16),
2888 ids->n_controllers);
2889 return OFPERR_OFPBAC_BAD_LEN;
2892 for (i = 0; i < ids->n_controllers; i++) {
2893 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2894 ofpbuf_put(out, &id, sizeof id);
2898 ofpact_update_len(out, &ids->ofpact);
2904 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2905 enum ofp_version ofp_version, struct ofpbuf *out)
2907 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2908 || dec_ttl->n_controllers != 1
2909 || dec_ttl->cnt_ids[0] != 0) {
2910 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2911 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2915 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2916 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2918 ids = ofpbuf_put_zeros(out, ids_len);
2919 for (i = 0; i < dec_ttl->n_controllers; i++) {
2920 ids[i] = htons(dec_ttl->cnt_ids[i]);
2923 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2928 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2930 struct ofpact_cnt_ids *ids;
2933 ofpact_put_DEC_TTL(ofpacts);
2934 ofpbuf_put(ofpacts, &id, sizeof id);
2935 ids = ofpacts->header;
2936 ids->n_controllers++;
2937 ofpact_update_len(ofpacts, &ids->ofpact);
2940 static char * OVS_WARN_UNUSED_RESULT
2941 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2942 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2945 parse_noargs_dec_ttl(ofpacts);
2947 struct ofpact_cnt_ids *ids;
2950 ids = ofpact_put_DEC_TTL(ofpacts);
2951 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2952 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2953 cntr = strtok_r(NULL, ", ", &arg)) {
2954 uint16_t id = atoi(cntr);
2956 ofpbuf_put(ofpacts, &id, sizeof id);
2957 ids = ofpacts->header;
2958 ids->n_controllers++;
2960 if (!ids->n_controllers) {
2961 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2964 ofpact_update_len(ofpacts, &ids->ofpact);
2970 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2974 ds_put_cstr(s, "dec_ttl");
2975 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2976 ds_put_cstr(s, "(");
2977 for (i = 0; i < a->n_controllers; i++) {
2979 ds_put_cstr(s, ",");
2981 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2983 ds_put_cstr(s, ")");
2987 /* Set MPLS label actions. */
2990 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
2991 enum ofp_version ofp_version OVS_UNUSED,
2994 ofpact_put_SET_MPLS_LABEL(out)->label = label;
2999 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3000 enum ofp_version ofp_version,
3003 if (ofp_version < OFP12_VERSION) {
3004 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3006 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
3007 ntohl(label->label));
3011 static char * OVS_WARN_UNUSED_RESULT
3012 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3013 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3015 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3017 return xstrdup("set_mpls_label: expected label.");
3020 mpls_label->label = htonl(atoi(arg));
3025 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3027 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
3030 /* Set MPLS TC actions. */
3033 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3034 enum ofp_version ofp_version OVS_UNUSED,
3037 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3042 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3043 enum ofp_version ofp_version, struct ofpbuf *out)
3045 if (ofp_version < OFP12_VERSION) {
3046 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3048 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3052 static char * OVS_WARN_UNUSED_RESULT
3053 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3054 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3056 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3059 return xstrdup("set_mpls_tc: expected tc.");
3062 mpls_tc->tc = atoi(arg);
3067 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3069 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
3072 /* Set MPLS TTL actions. */
3075 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3076 enum ofp_version ofp_version OVS_UNUSED,
3079 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3084 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3085 enum ofp_version ofp_version, struct ofpbuf *out)
3087 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3090 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3091 * action to 'ofpacts'.
3093 * Returns NULL if successful, otherwise a malloc()'d string describing the
3094 * error. The caller is responsible for freeing the returned string. */
3095 static char * OVS_WARN_UNUSED_RESULT
3096 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3097 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3099 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3102 return xstrdup("set_mpls_ttl: expected ttl.");
3105 mpls_ttl->ttl = atoi(arg);
3110 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3112 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3115 /* Decrement MPLS TTL actions. */
3118 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3120 ofpact_put_DEC_MPLS_TTL(out);
3125 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3126 enum ofp_version ofp_version, struct ofpbuf *out)
3128 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3131 static char * OVS_WARN_UNUSED_RESULT
3132 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3133 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3135 ofpact_put_DEC_MPLS_TTL(ofpacts);
3140 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3142 ds_put_cstr(s, "dec_mpls_ttl");
3145 /* Push MPLS label action. */
3148 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3149 enum ofp_version ofp_version OVS_UNUSED,
3152 struct ofpact_push_mpls *oam;
3154 if (!eth_type_mpls(ethertype)) {
3155 return OFPERR_OFPBAC_BAD_ARGUMENT;
3157 oam = ofpact_put_PUSH_MPLS(out);
3158 oam->ethertype = ethertype;
3164 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3165 enum ofp_version ofp_version, struct ofpbuf *out)
3167 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3170 static char * OVS_WARN_UNUSED_RESULT
3171 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3172 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3177 error = str_to_u16(arg, "push_mpls", ðertype);
3179 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3185 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3187 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3190 /* Pop MPLS label action. */
3193 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3194 enum ofp_version ofp_version OVS_UNUSED,
3197 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3202 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3203 enum ofp_version ofp_version, struct ofpbuf *out)
3205 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3208 static char * OVS_WARN_UNUSED_RESULT
3209 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3210 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3215 error = str_to_u16(arg, "pop_mpls", ðertype);
3217 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3223 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3225 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3228 /* Set tunnel ID actions. */
3231 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3232 enum ofp_version ofp_version OVS_UNUSED,
3235 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3236 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3237 tunnel->tun_id = tun_id;
3242 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3243 enum ofp_version ofp_version OVS_UNUSED,
3246 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3247 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3248 tunnel->tun_id = tun_id;
3253 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3254 enum ofp_version ofp_version, struct ofpbuf *out)
3256 uint64_t tun_id = tunnel->tun_id;
3258 if (ofp_version < OFP12_VERSION) {
3259 if (tun_id <= UINT32_MAX
3260 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3261 put_NXAST_SET_TUNNEL(out, tun_id);
3263 put_NXAST_SET_TUNNEL64(out, tun_id);
3266 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3270 static char * OVS_WARN_UNUSED_RESULT
3271 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3272 enum ofp_raw_action_type raw)
3274 struct ofpact_tunnel *tunnel;
3276 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3277 tunnel->ofpact.raw = raw;
3278 return str_to_u64(arg, &tunnel->tun_id);
3281 static char * OVS_WARN_UNUSED_RESULT
3282 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3283 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3285 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3289 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3291 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3292 (a->tun_id > UINT32_MAX
3293 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3297 /* Set queue action. */
3300 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3301 enum ofp_version ofp_version OVS_UNUSED,
3304 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3309 encode_SET_QUEUE(const struct ofpact_queue *queue,
3310 enum ofp_version ofp_version, struct ofpbuf *out)
3312 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3315 static char * OVS_WARN_UNUSED_RESULT
3316 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3317 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3319 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3323 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3325 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3328 /* Pop queue action. */
3331 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3333 ofpact_put_POP_QUEUE(out);
3338 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3339 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3341 put_NXAST_POP_QUEUE(out);
3344 static char * OVS_WARN_UNUSED_RESULT
3345 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3346 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3348 ofpact_put_POP_QUEUE(ofpacts);
3353 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3355 ds_put_cstr(s, "pop_queue");
3358 /* Action structure for NXAST_FIN_TIMEOUT.
3360 * This action changes the idle timeout or hard timeout, or both, of this
3361 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3362 * When such a packet is observed, the action reduces the rule's idle timeout
3363 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3364 * action has no effect on an existing timeout that is already shorter than the
3365 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3366 * of zero has no effect on the respective timeout.
3368 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3369 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3370 * receipt of the FIN or RST.
3372 * This is useful for quickly discarding learned TCP flows that otherwise will
3373 * take a long time to expire.
3375 * This action is intended for use with an OpenFlow rule that matches only a
3376 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3377 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3378 * RST in any of those flows will cause the entire OpenFlow rule to expire
3379 * early, which is not normally desirable.
3381 struct nx_action_fin_timeout {
3382 ovs_be16 type; /* OFPAT_VENDOR. */
3383 ovs_be16 len; /* 16. */
3384 ovs_be32 vendor; /* NX_VENDOR_ID. */
3385 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3386 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3387 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3388 ovs_be16 pad; /* Must be zero. */
3390 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3393 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3394 enum ofp_version ofp_version OVS_UNUSED,
3397 struct ofpact_fin_timeout *oft;
3399 oft = ofpact_put_FIN_TIMEOUT(out);
3400 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3401 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3406 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3407 enum ofp_version ofp_version OVS_UNUSED,
3410 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3411 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3412 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3415 static char * OVS_WARN_UNUSED_RESULT
3416 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3417 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3419 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3422 while (ofputil_parse_key_value(&arg, &key, &value)) {
3425 if (!strcmp(key, "idle_timeout")) {
3426 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3427 } else if (!strcmp(key, "hard_timeout")) {
3428 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3430 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3442 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3444 ds_put_cstr(s, "fin_timeout(");
3445 if (a->fin_idle_timeout) {
3446 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3448 if (a->fin_hard_timeout) {
3449 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3452 ds_put_char(s, ')');
3455 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3457 * These actions search one of the switch's flow tables:
3459 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3460 * it specifies the table to search.
3462 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3463 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3464 * table, that is, the OpenFlow flow table that contains the flow from
3465 * which this action was obtained. If this action did not come from a
3466 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3467 * is the current table.
3469 * The flow table lookup uses a flow that may be slightly modified from the
3472 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3473 * is used as the flow's in_port.
3475 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3476 * then its value is used as the flow's in_port. Otherwise, the original
3479 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3480 * resubmit action, then the flow is updated with the new values.
3482 * Following the lookup, the original in_port is restored.
3484 * If the modified flow matched in the flow table, then the corresponding
3485 * actions are executed. Afterward, actions following the resubmit in the
3486 * original set of actions, if any, are executed; any changes made to the
3487 * packet (e.g. changes to VLAN) by secondary actions persist when those
3488 * actions are executed, although the original in_port is restored.
3490 * Resubmit actions may be used any number of times within a set of actions.
3492 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3493 * implementation-defined depth, further resubmit actions are simply ignored.
3495 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3496 * 'pad' to be all-bits-zero.
3498 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3499 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3501 struct nx_action_resubmit {
3502 ovs_be16 type; /* OFPAT_VENDOR. */
3503 ovs_be16 len; /* Length is 16. */
3504 ovs_be32 vendor; /* NX_VENDOR_ID. */
3505 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3506 ovs_be16 in_port; /* New in_port for checking flow table. */
3507 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3510 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3513 decode_NXAST_RAW_RESUBMIT(uint16_t port,
3514 enum ofp_version ofp_version OVS_UNUSED,
3517 struct ofpact_resubmit *resubmit;
3519 resubmit = ofpact_put_RESUBMIT(out);
3520 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3521 resubmit->in_port = u16_to_ofp(port);
3522 resubmit->table_id = 0xff;
3527 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3528 enum ofp_version ofp_version OVS_UNUSED,
3531 struct ofpact_resubmit *resubmit;
3533 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3534 return OFPERR_OFPBAC_BAD_ARGUMENT;
3537 resubmit = ofpact_put_RESUBMIT(out);
3538 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3539 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3540 resubmit->table_id = nar->table;
3545 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3546 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3548 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3550 if (resubmit->table_id == 0xff
3551 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3552 put_NXAST_RESUBMIT(out, in_port);
3554 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3555 nar->table = resubmit->table_id;
3556 nar->in_port = htons(in_port);
3560 static char * OVS_WARN_UNUSED_RESULT
3561 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3562 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3564 struct ofpact_resubmit *resubmit;
3565 char *in_port_s, *table_s;
3567 resubmit = ofpact_put_RESUBMIT(ofpacts);
3569 in_port_s = strsep(&arg, ",");
3570 if (in_port_s && in_port_s[0]) {
3571 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3572 return xasprintf("%s: resubmit to unknown port", in_port_s);
3575 resubmit->in_port = OFPP_IN_PORT;
3578 table_s = strsep(&arg, ",");
3579 if (table_s && table_s[0]) {
3580 uint32_t table_id = 0;
3583 error = str_to_u32(table_s, &table_id);
3587 resubmit->table_id = table_id;
3589 resubmit->table_id = 255;
3592 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3593 return xstrdup("at least one \"in_port\" or \"table\" must be "
3594 "specified on resubmit");
3600 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3602 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3603 ds_put_cstr(s, "resubmit:");
3604 ofputil_format_port(a->in_port, s);
3606 ds_put_format(s, "resubmit(");
3607 if (a->in_port != OFPP_IN_PORT) {
3608 ofputil_format_port(a->in_port, s);
3610 ds_put_char(s, ',');
3611 if (a->table_id != 255) {
3612 ds_put_format(s, "%"PRIu8, a->table_id);
3614 ds_put_char(s, ')');
3618 /* Action structure for NXAST_LEARN.
3620 * This action adds or modifies a flow in an OpenFlow table, similar to
3621 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3622 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3623 * flow's match criteria and actions are built by applying each of the series
3624 * of flow_mod_spec elements included as part of the action.
3626 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3627 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3628 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3629 * bits from a source to a destination. In this case, the header contains
3632 * 15 14 13 12 11 10 0
3633 * +------+---+------+---------------------------------+
3634 * | 0 |src| dst | n_bits |
3635 * +------+---+------+---------------------------------+
3637 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3638 * following table summarizes the meaning of each possible combination.
3639 * Details follow the table:
3642 * --- --- ----------------------------------------------------------
3643 * 0 0 Add match criteria based on value in a field.
3644 * 1 0 Add match criteria based on an immediate value.
3645 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3646 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3647 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3648 * All other combinations are undefined and not allowed.
3650 * The flow_mod_spec header is followed by a source specification and a
3651 * destination specification. The format and meaning of the source
3652 * specification depends on 'src':
3654 * - If 'src' is 0, the source bits are taken from a field in the flow to
3655 * which this action is attached. (This should be a wildcarded field. If
3656 * its value is fully specified then the source bits being copied have
3659 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3660 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3661 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3662 * 'field' and 'ofs' are subject to the same restrictions as the source
3663 * field in NXAST_REG_MOVE.
3665 * - If 'src' is 1, the source bits are a constant value. The source
3666 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3667 * number in network order, the source bits are the 'n_bits'
3668 * least-significant bits. The switch will report an error if other bits
3669 * in the constant are nonzero.
3671 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3672 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3673 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3674 * meaning of the flow_mod_spec depends on 'dst':
3676 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3677 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3678 * packet equal the source bits. 'field' may be any nxm_header with
3679 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3681 * Order is significant. Earlier flow_mod_specs must satisfy any
3682 * prerequisites for matching fields specified later, by copying constant
3683 * values into prerequisite fields.
3685 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3688 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3689 * the new flow. The new flow copies the source bits into
3690 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3693 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3694 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3696 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3697 * empty. It has the following meaning:
3699 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3700 * The new flow outputs to the OpenFlow port specified by the source field.
3701 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3702 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3705 * Resource Management
3706 * -------------------
3708 * A switch has a finite amount of flow table space available for learning.
3709 * When this space is exhausted, no new learning table entries will be learned
3710 * until some existing flow table entries expire. The controller should be
3711 * prepared to handle this by flooding (which can be implemented as a
3712 * low-priority flow).
3714 * If a learned flow matches a single TCP stream with a relatively long
3715 * timeout, one may make the best of resource constraints by setting
3716 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3717 * both, to shorter timeouts. When either of these is specified as a nonzero
3718 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3724 * The following examples give a prose description of the flow_mod_specs along
3725 * with informal notation for how those would be represented and a hex dump of
3726 * the bytes that would be required.
3728 * These examples could work with various nx_action_learn parameters. Typical
3729 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3730 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3732 * 1. Learn input port based on the source MAC, with lookup into
3733 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3735 * Match on in_port=99:
3736 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3737 * ovs_be16(99), 00 63
3738 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3740 * Match Ethernet destination on Ethernet source from packet:
3741 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3742 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3743 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3745 * Set NXM_NX_REG1[16:31] to the packet's input port:
3746 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3747 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3748 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3750 * Given a packet that arrived on port A with Ethernet source address B,
3751 * this would set up the flow "in_port=99, dl_dst=B,
3752 * actions=load:A->NXM_NX_REG1[16..31]".
3754 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3755 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3756 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3758 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3759 * into NXM_NX_REG1[16:31]:
3761 * Match on same VLAN ID as packet:
3762 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3763 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3764 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3766 * Match Ethernet destination on Ethernet source from packet:
3767 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3768 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3769 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3771 * Output to the packet's input port:
3772 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3773 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3775 * Given a packet that arrived on port A with Ethernet source address B in
3776 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3777 * actions=output:A".
3779 * In syntax accepted by ovs-ofctl, this action is:
3780 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3781 * output:NXM_OF_IN_PORT[])
3783 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3784 * 10-second MAC expiration time to make it easier to see what's going on
3786 * ovs-vsctl del-controller br0
3787 * ovs-ofctl del-flows br0
3788 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3789 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3790 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3791 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3792 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3794 * You can then dump the MAC learning table with:
3796 * ovs-ofctl dump-flows br0 table=1
3801 * For best performance, segregate learned flows into a table that is not used
3802 * for any other flows except possibly for a lowest-priority "catch-all" flow
3803 * (a flow with no match criteria). If different learning actions specify
3804 * different match criteria, use different tables for the learned flows.
3806 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3807 * These timeouts apply to the flow that is added, which means that a flow with
3808 * an idle timeout will expire when no traffic has been sent *to* the learned
3809 * address. This is not usually the intent in MAC learning; instead, we want
3810 * the MAC learn entry to expire when no traffic has been sent *from* the
3811 * learned address. Use a hard timeout for that.
3814 * Visibility of Changes
3815 * ---------------------
3817 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3818 * flow translation are visible to flow table lookups made later in the flow
3819 * translation. This means that, in the example above, a MAC learned by the
3820 * learn action in table 0 would be found in table 1 (if the packet being
3821 * processed had the same source and destination MAC address).
3823 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3824 * modify a flow) by a "learn" action are visible only for later flow
3825 * translations, not for later lookups within the same flow translation. In
3826 * the MAC learning example, a MAC learned by the learn action in table 0 would
3827 * not be found in table 1 if the flow translation would resubmit to table 1
3828 * after the processing of the learn action, meaning that if this MAC had not
3829 * been learned before then the packet would be flooded. */
3830 struct nx_action_learn {
3831 ovs_be16 type; /* OFPAT_VENDOR. */
3832 ovs_be16 len; /* At least 24. */
3833 ovs_be32 vendor; /* NX_VENDOR_ID. */
3834 ovs_be16 subtype; /* NXAST_LEARN. */
3835 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3836 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3837 ovs_be16 priority; /* Priority level of flow entry. */
3838 ovs_be64 cookie; /* Cookie for new flow. */
3839 ovs_be16 flags; /* NX_LEARN_F_*. */
3840 uint8_t table_id; /* Table to insert flow entry. */
3841 uint8_t pad; /* Must be zero. */
3842 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3843 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3844 /* Followed by a sequence of flow_mod_spec elements, as described above,
3845 * until the end of the action is reached. */
3847 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3850 get_be16(const void **pp)
3852 const ovs_be16 *p = *pp;
3853 ovs_be16 value = *p;
3859 get_be32(const void **pp)
3861 const ovs_be32 *p = *pp;
3862 ovs_be32 value = get_unaligned_be32(p);
3868 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3870 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3871 sf->ofs = ntohs(get_be16(p));
3872 sf->n_bits = n_bits;
3876 learn_min_len(uint16_t header)
3878 int n_bits = header & NX_LEARN_N_BITS_MASK;
3879 int src_type = header & NX_LEARN_SRC_MASK;
3880 int dst_type = header & NX_LEARN_DST_MASK;
3881 unsigned int min_len;
3884 if (src_type == NX_LEARN_SRC_FIELD) {
3885 min_len += sizeof(ovs_be32); /* src_field */
3886 min_len += sizeof(ovs_be16); /* src_ofs */
3888 min_len += DIV_ROUND_UP(n_bits, 16);
3890 if (dst_type == NX_LEARN_DST_MATCH ||
3891 dst_type == NX_LEARN_DST_LOAD) {
3892 min_len += sizeof(ovs_be32); /* dst_field */
3893 min_len += sizeof(ovs_be16); /* dst_ofs */
3898 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3899 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3901 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3902 enum ofp_version ofp_version OVS_UNUSED,
3903 struct ofpbuf *ofpacts)
3905 struct ofpact_learn *learn;
3906 const void *p, *end;
3909 return OFPERR_OFPBAC_BAD_ARGUMENT;
3912 learn = ofpact_put_LEARN(ofpacts);
3914 learn->idle_timeout = ntohs(nal->idle_timeout);
3915 learn->hard_timeout = ntohs(nal->hard_timeout);
3916 learn->priority = ntohs(nal->priority);
3917 learn->cookie = nal->cookie;
3918 learn->table_id = nal->table_id;
3919 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3920 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3922 learn->flags = ntohs(nal->flags);
3923 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3924 NX_LEARN_F_DELETE_LEARNED)) {
3925 return OFPERR_OFPBAC_BAD_ARGUMENT;
3928 if (learn->table_id == 0xff) {
3929 return OFPERR_OFPBAC_BAD_ARGUMENT;
3932 end = (char *) nal + ntohs(nal->len);
3933 for (p = nal + 1; p != end; ) {
3934 struct ofpact_learn_spec *spec;
3935 uint16_t header = ntohs(get_be16(&p));
3941 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3942 learn = ofpacts->header;
3945 spec->src_type = header & NX_LEARN_SRC_MASK;
3946 spec->dst_type = header & NX_LEARN_DST_MASK;
3947 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3949 /* Check for valid src and dst type combination. */
3950 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3951 spec->dst_type == NX_LEARN_DST_LOAD ||
3952 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3953 spec->src_type == NX_LEARN_SRC_FIELD)) {
3956 return OFPERR_OFPBAC_BAD_ARGUMENT;
3959 /* Check that the arguments don't overrun the end of the action. */
3960 if ((char *) end - (char *) p < learn_min_len(header)) {
3961 return OFPERR_OFPBAC_BAD_LEN;
3964 /* Get the source. */
3965 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3966 get_subfield(spec->n_bits, &p, &spec->src);
3968 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3970 bitwise_copy(p, p_bytes, 0,
3971 &spec->src_imm, sizeof spec->src_imm, 0,
3973 p = (const uint8_t *) p + p_bytes;
3976 /* Get the destination. */
3977 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3978 spec->dst_type == NX_LEARN_DST_LOAD) {
3979 get_subfield(spec->n_bits, &p, &spec->dst);
3982 ofpact_update_len(ofpacts, &learn->ofpact);
3984 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3985 return OFPERR_OFPBAC_BAD_ARGUMENT;
3992 put_be16(struct ofpbuf *b, ovs_be16 x)
3994 ofpbuf_put(b, &x, sizeof x);
3998 put_be32(struct ofpbuf *b, ovs_be32 x)
4000 ofpbuf_put(b, &x, sizeof x);
4004 put_u16(struct ofpbuf *b, uint16_t x)
4006 put_be16(b, htons(x));
4010 put_u32(struct ofpbuf *b, uint32_t x)
4012 put_be32(b, htonl(x));
4016 encode_LEARN(const struct ofpact_learn *learn,
4017 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4019 const struct ofpact_learn_spec *spec;
4020 struct nx_action_learn *nal;
4023 start_ofs = out->size;
4024 nal = put_NXAST_LEARN(out);
4025 nal->idle_timeout = htons(learn->idle_timeout);
4026 nal->hard_timeout = htons(learn->hard_timeout);
4027 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4028 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4029 nal->priority = htons(learn->priority);
4030 nal->cookie = learn->cookie;
4031 nal->flags = htons(learn->flags);
4032 nal->table_id = learn->table_id;
4034 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4035 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4037 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4038 put_u32(out, mf_nxm_header(spec->src.field->id));
4039 put_u16(out, spec->src.ofs);
4041 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4042 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4043 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4044 bits, n_dst_bytes, 0,
4048 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4049 spec->dst_type == NX_LEARN_DST_LOAD) {
4050 put_u32(out, mf_nxm_header(spec->dst.field->id));
4051 put_u16(out, spec->dst.ofs);
4055 pad_ofpat(out, start_ofs);
4058 static char * OVS_WARN_UNUSED_RESULT
4059 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4060 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4062 return learn_parse(arg, ofpacts);
4066 format_LEARN(const struct ofpact_learn *a, struct ds *s)
4071 /* Action structure for NXAST_CONJUNCTION. */
4072 struct nx_action_conjunction {
4073 ovs_be16 type; /* OFPAT_VENDOR. */
4074 ovs_be16 len; /* At least 16. */
4075 ovs_be32 vendor; /* NX_VENDOR_ID. */
4076 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4081 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4084 add_conjunction(struct ofpbuf *out,
4085 uint32_t id, uint8_t clause, uint8_t n_clauses)
4087 struct ofpact_conjunction *oc;
4089 oc = ofpact_put_CONJUNCTION(out);
4091 oc->clause = clause;
4092 oc->n_clauses = n_clauses;
4096 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4097 enum ofp_version ofp_version OVS_UNUSED,
4100 if (nac->n_clauses < 2 || nac->n_clauses > 64
4101 || nac->clause >= nac->n_clauses) {
4102 return OFPERR_NXBAC_BAD_CONJUNCTION;
4104 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4110 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4111 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4113 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4114 nac->clause = oc->clause;
4115 nac->n_clauses = oc->n_clauses;
4116 nac->id = htonl(oc->id);
4120 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4122 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4123 oc->id, oc->clause + 1, oc->n_clauses);
4126 static char * OVS_WARN_UNUSED_RESULT
4127 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4128 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4135 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4136 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4137 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4140 if (n_clauses < 2) {
4141 return xstrdup("conjunction must have at least 2 clauses");
4142 } else if (n_clauses > 64) {
4143 return xstrdup("conjunction must have at most 64 clauses");
4144 } else if (clause < 1) {
4145 return xstrdup("clause index must be positive");
4146 } else if (clause > n_clauses) {
4147 return xstrdup("clause index must be less than or equal to "
4148 "number of clauses");
4151 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4155 /* Action structure for NXAST_MULTIPATH.
4157 * This action performs the following steps in sequence:
4159 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4160 * Refer to the definition of "enum nx_mp_fields" for details.
4162 * The 'basis' value is used as a universal hash parameter, that is,
4163 * different values of 'basis' yield different hash functions. The
4164 * particular universal hash function used is implementation-defined.
4166 * The hashed fields' values are drawn from the current state of the
4167 * flow, including all modifications that have been made by actions up to
4170 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4171 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4174 * The output of the algorithm is 'link', an unsigned integer less than
4175 * or equal to 'max_link'.
4177 * Some algorithms use 'arg' as an additional argument.
4179 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4180 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4183 * The switch will reject actions that have an unknown 'fields', or an unknown
4184 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4185 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4186 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4188 struct nx_action_multipath {
4189 ovs_be16 type; /* OFPAT_VENDOR. */
4190 ovs_be16 len; /* Length is 32. */
4191 ovs_be32 vendor; /* NX_VENDOR_ID. */
4192 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4194 /* What fields to hash and how. */
4195 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4196 ovs_be16 basis; /* Universal hash parameter. */
4199 /* Multipath link choice algorithm to apply to hash value. */
4200 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4201 ovs_be16 max_link; /* Number of output links, minus 1. */
4202 ovs_be32 arg; /* Algorithm-specific argument. */
4205 /* Where to store the result. */
4206 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4207 ovs_be32 dst; /* Destination. */
4209 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4212 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4213 enum ofp_version ofp_version OVS_UNUSED,
4216 uint32_t n_links = ntohs(nam->max_link) + 1;
4217 size_t min_n_bits = log_2_ceil(n_links);
4218 struct ofpact_multipath *mp;
4220 mp = ofpact_put_MULTIPATH(out);
4221 mp->fields = ntohs(nam->fields);
4222 mp->basis = ntohs(nam->basis);
4223 mp->algorithm = ntohs(nam->algorithm);
4224 mp->max_link = ntohs(nam->max_link);
4225 mp->arg = ntohl(nam->arg);
4226 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4227 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4228 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4230 if (!flow_hash_fields_valid(mp->fields)) {
4231 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4232 return OFPERR_OFPBAC_BAD_ARGUMENT;
4233 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4234 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4235 && mp->algorithm != NX_MP_ALG_HRW
4236 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4237 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4238 return OFPERR_OFPBAC_BAD_ARGUMENT;
4239 } else if (mp->dst.n_bits < min_n_bits) {
4240 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4241 "%"PRIu32" links", min_n_bits, n_links);
4242 return OFPERR_OFPBAC_BAD_ARGUMENT;
4245 return multipath_check(mp, NULL);
4249 encode_MULTIPATH(const struct ofpact_multipath *mp,
4250 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4252 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4254 nam->fields = htons(mp->fields);
4255 nam->basis = htons(mp->basis);
4256 nam->algorithm = htons(mp->algorithm);
4257 nam->max_link = htons(mp->max_link);
4258 nam->arg = htonl(mp->arg);
4259 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4260 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4263 static char * OVS_WARN_UNUSED_RESULT
4264 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4265 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4267 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4271 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4273 multipath_format(a, s);
4276 /* Action structure for NXAST_NOTE.
4278 * This action has no effect. It is variable length. The switch does not
4279 * attempt to interpret the user-defined 'note' data in any way. A controller
4280 * can use this action to attach arbitrary metadata to a flow.
4282 * This action might go away in the future.
4284 struct nx_action_note {
4285 ovs_be16 type; /* OFPAT_VENDOR. */
4286 ovs_be16 len; /* A multiple of 8, but at least 16. */
4287 ovs_be32 vendor; /* NX_VENDOR_ID. */
4288 ovs_be16 subtype; /* NXAST_NOTE. */
4289 uint8_t note[6]; /* Start of user-defined data. */
4290 /* Possibly followed by additional user-defined data. */
4292 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4295 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4296 enum ofp_version ofp_version OVS_UNUSED,
4299 struct ofpact_note *note;
4300 unsigned int length;
4302 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4303 note = ofpact_put(out, OFPACT_NOTE,
4304 offsetof(struct ofpact_note, data) + length);
4305 note->length = length;
4306 memcpy(note->data, nan->note, length);
4312 encode_NOTE(const struct ofpact_note *note,
4313 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4315 size_t start_ofs = out->size;
4316 struct nx_action_note *nan;
4317 unsigned int remainder;
4320 put_NXAST_NOTE(out);
4321 out->size = out->size - sizeof nan->note;
4323 ofpbuf_put(out, note->data, note->length);
4325 len = out->size - start_ofs;
4326 remainder = len % OFP_ACTION_ALIGN;
4328 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4330 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4331 nan->len = htons(out->size - start_ofs);
4334 static char * OVS_WARN_UNUSED_RESULT
4335 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4336 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4338 struct ofpact_note *note;
4340 note = ofpact_put_NOTE(ofpacts);
4341 while (*arg != '\0') {
4352 byte = hexits_value(arg, 2, &ok);
4354 return xstrdup("bad hex digit in `note' argument");
4356 ofpbuf_put(ofpacts, &byte, 1);
4358 note = ofpacts->header;
4363 ofpact_update_len(ofpacts, ¬e->ofpact);
4368 format_NOTE(const struct ofpact_note *a, struct ds *s)
4372 ds_put_cstr(s, "note:");
4373 for (i = 0; i < a->length; i++) {
4375 ds_put_char(s, '.');
4377 ds_put_format(s, "%02"PRIx8, a->data[i]);
4384 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4386 ofpact_put_EXIT(out);
4391 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4392 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4394 put_NXAST_EXIT(out);
4397 static char * OVS_WARN_UNUSED_RESULT
4398 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4399 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4401 ofpact_put_EXIT(ofpacts);
4406 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4408 ds_put_cstr(s, "exit");
4411 /* Unroll xlate action. */
4414 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4415 enum ofp_version ofp_version OVS_UNUSED,
4416 struct ofpbuf *out OVS_UNUSED)
4421 static char * OVS_WARN_UNUSED_RESULT
4422 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4423 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4430 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4433 ds_put_cstr(s, "unroll_xlate");
4436 /* Action structure for NXAST_SAMPLE.
4438 * Samples matching packets with the given probability and sends them
4439 * each to the set of collectors identified with the given ID. The
4440 * probability is expressed as a number of packets to be sampled out
4441 * of USHRT_MAX packets, and must be >0.
4443 * When sending packet samples to IPFIX collectors, the IPFIX flow
4444 * record sent for each sampled packet is associated with the given
4445 * observation domain ID and observation point ID. Each IPFIX flow
4446 * record contain the sampled packet's headers when executing this
4447 * rule. If a sampled packet's headers are modified by previous
4448 * actions in the flow, those modified headers are sent. */
4449 struct nx_action_sample {
4450 ovs_be16 type; /* OFPAT_VENDOR. */
4451 ovs_be16 len; /* Length is 24. */
4452 ovs_be32 vendor; /* NX_VENDOR_ID. */
4453 ovs_be16 subtype; /* NXAST_SAMPLE. */
4454 ovs_be16 probability; /* Fraction of packets to sample. */
4455 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4456 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4457 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4459 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4462 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4463 enum ofp_version ofp_version OVS_UNUSED,
4466 struct ofpact_sample *sample;
4468 sample = ofpact_put_SAMPLE(out);
4469 sample->probability = ntohs(nas->probability);
4470 sample->collector_set_id = ntohl(nas->collector_set_id);
4471 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4472 sample->obs_point_id = ntohl(nas->obs_point_id);
4474 if (sample->probability == 0) {
4475 return OFPERR_OFPBAC_BAD_ARGUMENT;
4482 encode_SAMPLE(const struct ofpact_sample *sample,
4483 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4485 struct nx_action_sample *nas;
4487 nas = put_NXAST_SAMPLE(out);
4488 nas->probability = htons(sample->probability);
4489 nas->collector_set_id = htonl(sample->collector_set_id);
4490 nas->obs_domain_id = htonl(sample->obs_domain_id);
4491 nas->obs_point_id = htonl(sample->obs_point_id);
4494 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4495 * action to 'ofpacts'.
4497 * Returns NULL if successful, otherwise a malloc()'d string describing the
4498 * error. The caller is responsible for freeing the returned string. */
4499 static char * OVS_WARN_UNUSED_RESULT
4500 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4501 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4503 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4506 while (ofputil_parse_key_value(&arg, &key, &value)) {
4509 if (!strcmp(key, "probability")) {
4510 error = str_to_u16(value, "probability", &os->probability);
4511 if (!error && os->probability == 0) {
4512 error = xasprintf("invalid probability value \"%s\"", value);
4514 } else if (!strcmp(key, "collector_set_id")) {
4515 error = str_to_u32(value, &os->collector_set_id);
4516 } else if (!strcmp(key, "obs_domain_id")) {
4517 error = str_to_u32(value, &os->obs_domain_id);
4518 } else if (!strcmp(key, "obs_point_id")) {
4519 error = str_to_u32(value, &os->obs_point_id);
4521 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4528 if (os->probability == 0) {
4529 return xstrdup("non-zero \"probability\" must be specified on sample");
4535 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4537 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4538 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4539 a->probability, a->collector_set_id,
4540 a->obs_domain_id, a->obs_point_id);
4543 /* debug_recirc instruction. */
4545 static bool enable_debug;
4548 ofpact_dummy_enable(void)
4550 enable_debug = true;
4554 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4556 if (!enable_debug) {
4557 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4560 ofpact_put_DEBUG_RECIRC(out);
4565 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4566 enum ofp_version ofp_version OVS_UNUSED,
4569 put_NXAST_DEBUG_RECIRC(out);
4572 static char * OVS_WARN_UNUSED_RESULT
4573 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4574 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4576 ofpact_put_DEBUG_RECIRC(ofpacts);
4581 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4583 ds_put_cstr(s, "debug_recirc");
4586 /* Action structure for NXAST_CT.
4588 * Pass traffic to the connection tracker.
4590 * There are two important concepts to understanding the connection tracking
4591 * interface: Packet state and Connection state. Packets may be "Untracked" or
4592 * "Tracked". Connections may be "Uncommitted" or "Committed".
4596 * Untracked packets have not yet passed through the connection tracker,
4597 * and the connection state for such packets is unknown. In most cases,
4598 * packets entering the OpenFlow pipeline will initially be in the
4599 * untracked state. Untracked packets may become tracked by executing
4600 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4601 * about the connection available, in particular the connection state.
4603 * Tracked packets have previously passed through the connection tracker.
4604 * These packets will remain tracked through until the end of the OpenFlow
4605 * pipeline. Tracked packets which have NXAST_CT executed with a
4606 * "recirc_table" specified will return to the tracked state.
4608 * The packet state is only significant for the duration of packet
4609 * processing within the OpenFlow pipeline.
4611 * - Connection State:
4613 * Multiple packets may be associated with a single connection. Initially,
4614 * all connections are uncommitted. The connection state corresponding to
4615 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4617 * Uncommitted connections have no state stored about them. Uncommitted
4618 * connections may transition into the committed state by executing
4619 * NXAST_CT with the NX_CT_F_COMMIT flag.
4621 * Once a connection becomes committed, information may be gathered about
4622 * the connection by passing subsequent packets through the connection
4623 * tracker, and the state of the connection will be stored beyond the
4624 * lifetime of packet processing.
4626 * Connections may transition back into the uncommitted state due to
4627 * external timers, or due to the contents of packets that are sent to the
4628 * connection tracker. This behaviour is outside of the scope of the
4629 * OpenFlow interface.
4631 * The "zone" specifies a context within which the tracking is done:
4633 * The connection tracking zone is a 16-bit number. Each zone is an
4634 * independent connection tracking context. The connection state for each
4635 * connection is completely separate for each zone, so if a connection
4636 * is committed to zone A, then it will remain uncommitted in zone B.
4637 * If NXAST_CT is executed with the same zone multiple times, later
4638 * executions have no effect.
4640 * If 'zone_src' is nonzero, this specifies that the zone should be
4641 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4642 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4643 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4644 * are the same as the acceptable nxm_header values for the 'src' field of
4647 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4648 * connection tracking zone.
4650 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4652 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4653 * packet will be logically cloned prior to executing this action. One
4654 * copy will be sent to the connection tracker, then will be re-injected
4655 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
4656 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4657 * fields will be populated. The original instance of the packet will
4658 * continue the current actions list. This can be thought of as similar to
4659 * the effect of the "output" action: One copy is sent out (in this case,
4660 * to the connection tracker), but the current copy continues processing.
4662 * It is strongly recommended that this table is later than the current
4663 * table, to prevent loops.
4665 * Zero or more actions may immediately follow this action. These actions will
4666 * be executed within the context of the connection tracker, and they require
4667 * the NX_CT_F_COMMIT flag to be set.
4669 struct nx_action_conntrack {
4670 ovs_be16 type; /* OFPAT_VENDOR. */
4671 ovs_be16 len; /* At least 24. */
4672 ovs_be32 vendor; /* NX_VENDOR_ID. */
4673 ovs_be16 subtype; /* NXAST_CT. */
4674 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
4675 * Unspecified flag bits must be zero. */
4676 ovs_be32 zone_src; /* Connection tracking context. */
4678 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
4679 ovs_be16 zone_imm; /* Immediate value for zone. */
4681 uint8_t recirc_table; /* Recirculate to a specific table, or
4682 NX_CT_RECIRC_NONE for no recirculation. */
4683 uint8_t pad[5]; /* Zeroes */
4684 /* Followed by a sequence of zero or more OpenFlow actions. The length of
4685 * these is included in 'len'. */
4687 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
4690 decode_ct_zone(const struct nx_action_conntrack *nac,
4691 struct ofpact_conntrack *out)
4693 if (nac->zone_src) {
4696 out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
4697 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
4698 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
4699 error = mf_check_src(&out->zone_src, NULL);
4704 if (out->zone_src.n_bits != 16) {
4705 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
4706 out->zone_src.n_bits);
4707 return OFPERR_OFPBAC_BAD_SET_LEN;
4710 out->zone_src.field = NULL;
4711 out->zone_imm = ntohs(nac->zone_imm);
4718 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
4719 enum ofp_version ofp_version, struct ofpbuf *out)
4721 const size_t ct_offset = ofpacts_pull(out);
4722 struct ofpact_conntrack *conntrack;
4723 struct ofpbuf openflow;
4726 conntrack = ofpact_put_CT(out);
4727 conntrack->flags = ntohs(nac->flags);
4728 error = decode_ct_zone(nac, conntrack);
4732 conntrack->recirc_table = nac->recirc_table;
4734 ofpbuf_pull(out, sizeof(*conntrack));
4736 ofpbuf_use_const(&openflow, nac + 1, ntohs(nac->len) - sizeof(*nac));
4737 error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
4739 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4745 conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
4746 out->header = &conntrack->ofpact;
4747 ofpact_update_len(out, &conntrack->ofpact);
4749 if (conntrack->ofpact.len > sizeof(*conntrack)
4750 && !(conntrack->flags & NX_CT_F_COMMIT)) {
4751 VLOG_WARN_RL(&rl, "CT action requires commit flag if actions are "
4753 error = OFPERR_OFPBAC_BAD_ARGUMENT;
4757 ofpbuf_push_uninit(out, ct_offset);
4762 encode_CT(const struct ofpact_conntrack *conntrack,
4763 enum ofp_version ofp_version, struct ofpbuf *out)
4765 struct nx_action_conntrack *nac;
4766 const size_t ofs = out->size;
4769 nac = put_NXAST_CT(out);
4770 nac->flags = htons(conntrack->flags);
4771 if (conntrack->zone_src.field) {
4772 nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
4773 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
4774 conntrack->zone_src.n_bits);
4776 nac->zone_src = htonl(0);
4777 nac->zone_imm = htons(conntrack->zone_imm);
4779 nac->recirc_table = conntrack->recirc_table;
4781 len = ofpacts_put_openflow_actions(conntrack->actions,
4782 ofpact_ct_get_action_len(conntrack),
4784 len += sizeof(*nac);
4785 nac = ofpbuf_at(out, ofs, sizeof(*nac));
4786 nac->len = htons(len);
4789 /* Parses 'arg' as the argument to a "ct" action, and appends such an
4790 * action to 'ofpacts'.
4792 * Returns NULL if successful, otherwise a malloc()'d string describing the
4793 * error. The caller is responsible for freeing the returned string. */
4794 static char * OVS_WARN_UNUSED_RESULT
4795 parse_CT(char *arg, struct ofpbuf *ofpacts,
4796 enum ofputil_protocol *usable_protocols)
4798 const size_t ct_offset = ofpacts_pull(ofpacts);
4799 struct ofpact_conntrack *oc;
4803 oc = ofpact_put_CT(ofpacts);
4805 oc->recirc_table = NX_CT_RECIRC_NONE;
4806 while (ofputil_parse_key_value(&arg, &key, &value)) {
4807 if (!strcmp(key, "commit")) {
4808 oc->flags |= NX_CT_F_COMMIT;
4809 } else if (!strcmp(key, "table")) {
4810 error = str_to_u8(value, "recirc_table", &oc->recirc_table);
4811 if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
4812 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
4814 } else if (!strcmp(key, "zone")) {
4815 error = str_to_u16(value, "zone", &oc->zone_imm);
4819 error = mf_parse_subfield(&oc->zone_src, value);
4824 } else if (!strcmp(key, "exec")) {
4825 /* Hide existing actions from ofpacts_parse_copy(), so the
4826 * nesting can be handled transparently. */
4827 ofpbuf_pull(ofpacts, sizeof(*oc));
4828 error = ofpacts_parse_copy(value, ofpacts, usable_protocols, false,
4830 ofpact_pad(ofpacts);
4831 ofpacts->header = ofpbuf_push_uninit(ofpacts, sizeof(*oc));
4832 oc = ofpacts->header;
4834 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
4841 ofpact_update_len(ofpacts, &oc->ofpact);
4842 ofpbuf_push_uninit(ofpacts, ct_offset);
4847 format_CT(const struct ofpact_conntrack *a, struct ds *s)
4849 ds_put_cstr(s, "ct(");
4850 if (a->flags & NX_CT_F_COMMIT) {
4851 ds_put_cstr(s, "commit,");
4853 if (a->recirc_table != NX_CT_RECIRC_NONE) {
4854 ds_put_format(s, "table=%"PRIu8",", a->recirc_table);
4856 if (a->zone_src.field) {
4857 ds_put_format(s, "zone=");
4858 mf_format_subfield(&a->zone_src, s);
4859 ds_put_char(s, ',');
4860 } else if (a->zone_imm) {
4861 ds_put_format(s, "zone=%"PRIu16",", a->zone_imm);
4863 if (ofpact_ct_get_action_len(a)) {
4864 ds_put_cstr(s, "exec(");
4865 ofpacts_format(a->actions, ofpact_ct_get_action_len(a), s);
4866 ds_put_format(s, "),");
4869 ds_put_char(s, ')');
4872 /* Meter instruction. */
4875 encode_METER(const struct ofpact_meter *meter,
4876 enum ofp_version ofp_version, struct ofpbuf *out)
4878 if (ofp_version >= OFP13_VERSION) {
4879 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4883 static char * OVS_WARN_UNUSED_RESULT
4884 parse_METER(char *arg, struct ofpbuf *ofpacts,
4885 enum ofputil_protocol *usable_protocols)
4887 *usable_protocols &= OFPUTIL_P_OF13_UP;
4888 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
4892 format_METER(const struct ofpact_meter *a, struct ds *s)
4894 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
4897 /* Clear-Actions instruction. */
4900 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
4901 enum ofp_version ofp_version OVS_UNUSED,
4902 struct ofpbuf *out OVS_UNUSED)
4904 if (ofp_version > OFP10_VERSION) {
4905 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
4909 static char * OVS_WARN_UNUSED_RESULT
4910 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4911 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4913 ofpact_put_CLEAR_ACTIONS(ofpacts);
4918 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4920 ds_put_cstr(s, "clear_actions");
4923 /* Write-Actions instruction. */
4926 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
4927 enum ofp_version ofp_version, struct ofpbuf *out)
4929 if (ofp_version > OFP10_VERSION) {
4930 const size_t ofs = out->size;
4932 instruction_put_OFPIT11_WRITE_ACTIONS(out);
4933 ofpacts_put_openflow_actions(actions->actions,
4934 ofpact_nest_get_action_len(actions),
4936 ofpacts_update_instruction_actions(out, ofs);
4940 static char * OVS_WARN_UNUSED_RESULT
4941 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
4942 enum ofputil_protocol *usable_protocols)
4944 size_t ofs = ofpacts_pull(ofpacts);
4945 struct ofpact_nest *on;
4948 /* Add a Write-Actions instruction and then pull it off. */
4949 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
4950 ofpbuf_pull(ofpacts, sizeof *on);
4952 /* Parse nested actions.
4954 * We pulled off "write-actions" and the previous actions because the
4955 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4956 * that it doesn't actually include the nested actions. That means that
4957 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4958 * follows a Write-Actions, which is an invalid order. */
4959 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
4960 OFPACT_WRITE_ACTIONS);
4962 /* Put the Write-Actions back on and update its length. */
4963 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
4964 on->ofpact.len = ofpacts->size;
4966 /* Put any previous actions or instructions back on. */
4967 ofpbuf_push_uninit(ofpacts, ofs);
4973 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
4975 ds_put_cstr(s, "write_actions(");
4976 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
4977 ds_put_char(s, ')');
4980 /* Action structure for NXAST_WRITE_METADATA.
4982 * Modifies the 'mask' bits of the metadata value. */
4983 struct nx_action_write_metadata {
4984 ovs_be16 type; /* OFPAT_VENDOR. */
4985 ovs_be16 len; /* Length is 32. */
4986 ovs_be32 vendor; /* NX_VENDOR_ID. */
4987 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
4988 uint8_t zeros[6]; /* Must be zero. */
4989 ovs_be64 metadata; /* Metadata register. */
4990 ovs_be64 mask; /* Metadata mask. */
4992 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
4995 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
4996 enum ofp_version ofp_version OVS_UNUSED,
4999 struct ofpact_metadata *om;
5001 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
5002 return OFPERR_NXBRC_MUST_BE_ZERO;
5005 om = ofpact_put_WRITE_METADATA(out);
5006 om->metadata = nawm->metadata;
5007 om->mask = nawm->mask;
5013 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
5014 enum ofp_version ofp_version, struct ofpbuf *out)
5016 if (ofp_version == OFP10_VERSION) {
5017 struct nx_action_write_metadata *nawm;
5019 nawm = put_NXAST_WRITE_METADATA(out);
5020 nawm->metadata = metadata->metadata;
5021 nawm->mask = metadata->mask;
5023 struct ofp11_instruction_write_metadata *oiwm;
5025 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
5026 oiwm->metadata = metadata->metadata;
5027 oiwm->metadata_mask = metadata->mask;
5031 static char * OVS_WARN_UNUSED_RESULT
5032 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
5033 enum ofputil_protocol *usable_protocols)
5035 struct ofpact_metadata *om;
5036 char *mask = strchr(arg, '/');
5038 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
5040 om = ofpact_put_WRITE_METADATA(ofpacts);
5045 error = str_to_be64(mask + 1, &om->mask);
5050 om->mask = OVS_BE64_MAX;
5053 return str_to_be64(arg, &om->metadata);
5057 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5059 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
5060 if (a->mask != OVS_BE64_MAX) {
5061 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5065 /* Goto-Table instruction. */
5068 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5069 enum ofp_version ofp_version, struct ofpbuf *out)
5071 if (ofp_version == OFP10_VERSION) {
5072 struct nx_action_resubmit *nar;
5074 nar = put_NXAST_RESUBMIT_TABLE(out);
5075 nar->table = goto_table->table_id;
5076 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5078 struct ofp11_instruction_goto_table *oigt;
5080 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5081 oigt->table_id = goto_table->table_id;
5082 memset(oigt->pad, 0, sizeof oigt->pad);
5086 static char * OVS_WARN_UNUSED_RESULT
5087 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5088 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5090 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5091 char *table_s = strsep(&arg, ",");
5092 if (!table_s || !table_s[0]) {
5093 return xstrdup("instruction goto-table needs table id");
5095 return str_to_u8(table_s, "table", &ogt->table_id);
5099 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5101 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
5105 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
5106 const struct ofp_action_header *bad_action, enum ofperr error)
5108 if (!VLOG_DROP_WARN(&rl)) {
5112 ds_put_hex_dump(&s, actions, actions_len, 0, false);
5113 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5114 (char *)bad_action - (char *)actions,
5115 ofperr_get_name(error), ds_cstr(&s));
5121 ofpacts_decode(const void *actions, size_t actions_len,
5122 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5124 struct ofpbuf openflow;
5126 ofpbuf_use_const(&openflow, actions, actions_len);
5127 while (openflow.size) {
5128 const struct ofp_action_header *action = openflow.data;
5129 enum ofp_raw_action_type raw;
5133 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5135 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
5139 log_bad_action(actions, actions_len, action, error);
5144 ofpact_pad(ofpacts);
5149 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5150 unsigned int actions_len,
5151 enum ofp_version version,
5152 uint32_t allowed_ovsinsts,
5153 struct ofpbuf *ofpacts,
5154 enum ofpact_type outer_action)
5156 const struct ofp_action_header *actions;
5159 if (!outer_action) {
5160 ofpbuf_clear(ofpacts);
5163 if (actions_len % OFP_ACTION_ALIGN != 0) {
5164 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5165 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5166 return OFPERR_OFPBRC_BAD_LEN;
5169 actions = ofpbuf_try_pull(openflow, actions_len);
5170 if (actions == NULL) {
5171 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
5172 "remaining message length (%"PRIu32")",
5173 actions_len, openflow->size);
5174 return OFPERR_OFPBRC_BAD_LEN;
5177 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5179 ofpbuf_clear(ofpacts);
5183 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
5186 ofpbuf_clear(ofpacts);
5191 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
5192 * front of 'openflow' into ofpacts. On success, replaces any existing content
5193 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
5194 * Returns 0 if successful, otherwise an OpenFlow error.
5196 * Actions are processed according to their OpenFlow version which
5197 * is provided in the 'version' parameter.
5199 * In most places in OpenFlow, actions appear encapsulated in instructions, so
5200 * you should call ofpacts_pull_openflow_instructions() instead of this
5203 * The parsed actions are valid generically, but they may not be valid in a
5204 * specific context. For example, port numbers up to OFPP_MAX are valid
5205 * generically, but specific datapaths may only support port numbers in a
5206 * smaller range. Use ofpacts_check() to additional check whether actions are
5207 * valid in a specific context. */
5209 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
5210 unsigned int actions_len,
5211 enum ofp_version version,
5212 struct ofpbuf *ofpacts)
5214 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
5215 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
5219 /* OpenFlow 1.1 actions. */
5222 /* True if an action sets the value of a field
5223 * in a way that is compatibile with the action set.
5224 * The field can be set via either a set or a move action.
5225 * False otherwise. */
5227 ofpact_is_set_or_move_action(const struct ofpact *a)
5230 case OFPACT_SET_FIELD:
5231 case OFPACT_REG_MOVE:
5232 case OFPACT_SET_ETH_DST:
5233 case OFPACT_SET_ETH_SRC:
5234 case OFPACT_SET_IP_DSCP:
5235 case OFPACT_SET_IP_ECN:
5236 case OFPACT_SET_IP_TTL:
5237 case OFPACT_SET_IPV4_DST:
5238 case OFPACT_SET_IPV4_SRC:
5239 case OFPACT_SET_L4_DST_PORT:
5240 case OFPACT_SET_L4_SRC_PORT:
5241 case OFPACT_SET_MPLS_LABEL:
5242 case OFPACT_SET_MPLS_TC:
5243 case OFPACT_SET_MPLS_TTL:
5244 case OFPACT_SET_QUEUE:
5245 case OFPACT_SET_TUNNEL:
5246 case OFPACT_SET_VLAN_PCP:
5247 case OFPACT_SET_VLAN_VID:
5250 case OFPACT_CLEAR_ACTIONS:
5252 case OFPACT_CONTROLLER:
5253 case OFPACT_DEC_MPLS_TTL:
5254 case OFPACT_DEC_TTL:
5255 case OFPACT_ENQUEUE:
5257 case OFPACT_UNROLL_XLATE:
5258 case OFPACT_FIN_TIMEOUT:
5259 case OFPACT_GOTO_TABLE:
5262 case OFPACT_CONJUNCTION:
5264 case OFPACT_MULTIPATH:
5267 case OFPACT_OUTPUT_REG:
5268 case OFPACT_POP_MPLS:
5269 case OFPACT_POP_QUEUE:
5270 case OFPACT_PUSH_MPLS:
5271 case OFPACT_PUSH_VLAN:
5272 case OFPACT_RESUBMIT:
5274 case OFPACT_STACK_POP:
5275 case OFPACT_STACK_PUSH:
5276 case OFPACT_STRIP_VLAN:
5277 case OFPACT_WRITE_ACTIONS:
5278 case OFPACT_WRITE_METADATA:
5279 case OFPACT_DEBUG_RECIRC:
5286 /* True if an action is allowed in the action set.
5287 * False otherwise. */
5289 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
5292 case OFPACT_DEC_MPLS_TTL:
5293 case OFPACT_DEC_TTL:
5296 case OFPACT_POP_MPLS:
5297 case OFPACT_PUSH_MPLS:
5298 case OFPACT_PUSH_VLAN:
5299 case OFPACT_REG_MOVE:
5300 case OFPACT_SET_FIELD:
5301 case OFPACT_SET_ETH_DST:
5302 case OFPACT_SET_ETH_SRC:
5303 case OFPACT_SET_IP_DSCP:
5304 case OFPACT_SET_IP_ECN:
5305 case OFPACT_SET_IP_TTL:
5306 case OFPACT_SET_IPV4_DST:
5307 case OFPACT_SET_IPV4_SRC:
5308 case OFPACT_SET_L4_DST_PORT:
5309 case OFPACT_SET_L4_SRC_PORT:
5310 case OFPACT_SET_MPLS_LABEL:
5311 case OFPACT_SET_MPLS_TC:
5312 case OFPACT_SET_MPLS_TTL:
5313 case OFPACT_SET_QUEUE:
5314 case OFPACT_SET_TUNNEL:
5315 case OFPACT_SET_VLAN_PCP:
5316 case OFPACT_SET_VLAN_VID:
5317 case OFPACT_STRIP_VLAN:
5320 /* In general these actions are excluded because they are not part of
5321 * the OpenFlow specification nor map to actions that are defined in
5322 * the specification. Thus the order in which they should be applied
5323 * in the action set is undefined. */
5325 case OFPACT_CONTROLLER:
5327 case OFPACT_ENQUEUE:
5329 case OFPACT_UNROLL_XLATE:
5330 case OFPACT_FIN_TIMEOUT:
5332 case OFPACT_CONJUNCTION:
5333 case OFPACT_MULTIPATH:
5335 case OFPACT_OUTPUT_REG:
5336 case OFPACT_POP_QUEUE:
5337 case OFPACT_RESUBMIT:
5339 case OFPACT_STACK_POP:
5340 case OFPACT_STACK_PUSH:
5341 case OFPACT_DEBUG_RECIRC:
5343 /* The action set may only include actions and thus
5344 * may not include any instructions */
5345 case OFPACT_CLEAR_ACTIONS:
5346 case OFPACT_GOTO_TABLE:
5348 case OFPACT_WRITE_ACTIONS:
5349 case OFPACT_WRITE_METADATA:
5356 /* Append ofpact 'a' onto the tail of 'out' */
5358 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
5360 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
5363 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
5365 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
5366 enum ofpact_type filter)
5368 const struct ofpact *target;
5369 const struct ofpact *a;
5372 OFPACT_FOR_EACH (a, in->data, in->size) {
5373 if (a->type == filter) {
5378 ofpact_copy(out, target);
5380 return target != NULL;
5383 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5384 * The order of appended ofpacts is preserved between 'in' and 'out' */
5386 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5387 bool (*filter)(const struct ofpact *))
5389 const struct ofpact *a;
5391 OFPACT_FOR_EACH (a, in->data, in->size) {
5393 ofpact_copy(out, a);
5398 /* Reads 'action_set', which contains ofpacts accumulated by
5399 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5400 * executed directly into 'action_list'. (These names correspond to the
5401 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5403 * In general this involves appending the last instance of each action that is
5404 * admissible in the action set in the order described in the OpenFlow
5408 * + output action is only appended if no group action was present in 'in'.
5409 * + As a simplification all set actions are copied in the order the are
5410 * provided in 'in' as many set actions applied to a field has the same
5411 * affect as only applying the last action that sets a field and
5412 * duplicates are removed by do_xlate_actions().
5413 * This has an unwanted side-effect of compsoting multiple
5414 * LOAD_REG actions that touch different regions of the same field. */
5416 ofpacts_execute_action_set(struct ofpbuf *action_list,
5417 const struct ofpbuf *action_set)
5419 /* The OpenFlow spec "Action Set" section specifies this order. */
5420 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5421 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5422 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5423 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5424 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5425 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5426 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5427 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5429 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5430 * we should execute only OFPACT_GROUP.
5432 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5433 * all the actions because there's no point in modifying a packet that will
5434 * not be sent anywhere. */
5435 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5436 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5437 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5438 ofpbuf_clear(action_list);
5444 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5445 size_t n_in, enum ofp_version version,
5450 size_t start = out->size;
5452 error = ofpacts_decode(in, n_in, version, out);
5458 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5459 if (!ofpact_is_allowed_in_actions_set(a)) {
5460 VLOG_WARN_RL(&rl, "disallowed action in action set");
5461 return OFPERR_OFPBAC_BAD_TYPE;
5468 /* OpenFlow 1.1 instructions. */
5470 struct instruction_type_info {
5471 enum ovs_instruction_type type;
5475 static const struct instruction_type_info inst_info[] = {
5476 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5482 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5484 return inst_info[type].name;
5488 ovs_instruction_type_from_name(const char *name)
5490 const struct instruction_type_info *p;
5491 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5492 if (!strcasecmp(name, p->name)) {
5499 enum ovs_instruction_type
5500 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5504 return OVSINST_OFPIT13_METER;
5505 case OFPACT_CLEAR_ACTIONS:
5506 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5507 case OFPACT_WRITE_ACTIONS:
5508 return OVSINST_OFPIT11_WRITE_ACTIONS;
5509 case OFPACT_WRITE_METADATA:
5510 return OVSINST_OFPIT11_WRITE_METADATA;
5511 case OFPACT_GOTO_TABLE:
5512 return OVSINST_OFPIT11_GOTO_TABLE;
5515 case OFPACT_CONTROLLER:
5516 case OFPACT_ENQUEUE:
5517 case OFPACT_OUTPUT_REG:
5519 case OFPACT_SET_VLAN_VID:
5520 case OFPACT_SET_VLAN_PCP:
5521 case OFPACT_STRIP_VLAN:
5522 case OFPACT_PUSH_VLAN:
5523 case OFPACT_SET_ETH_SRC:
5524 case OFPACT_SET_ETH_DST:
5525 case OFPACT_SET_IPV4_SRC:
5526 case OFPACT_SET_IPV4_DST:
5527 case OFPACT_SET_IP_DSCP:
5528 case OFPACT_SET_IP_ECN:
5529 case OFPACT_SET_IP_TTL:
5530 case OFPACT_SET_L4_SRC_PORT:
5531 case OFPACT_SET_L4_DST_PORT:
5532 case OFPACT_REG_MOVE:
5533 case OFPACT_SET_FIELD:
5534 case OFPACT_STACK_PUSH:
5535 case OFPACT_STACK_POP:
5536 case OFPACT_DEC_TTL:
5537 case OFPACT_SET_MPLS_LABEL:
5538 case OFPACT_SET_MPLS_TC:
5539 case OFPACT_SET_MPLS_TTL:
5540 case OFPACT_DEC_MPLS_TTL:
5541 case OFPACT_PUSH_MPLS:
5542 case OFPACT_POP_MPLS:
5543 case OFPACT_SET_TUNNEL:
5544 case OFPACT_SET_QUEUE:
5545 case OFPACT_POP_QUEUE:
5546 case OFPACT_FIN_TIMEOUT:
5547 case OFPACT_RESUBMIT:
5549 case OFPACT_CONJUNCTION:
5550 case OFPACT_MULTIPATH:
5553 case OFPACT_UNROLL_XLATE:
5555 case OFPACT_DEBUG_RECIRC:
5558 return OVSINST_OFPIT11_APPLY_ACTIONS;
5563 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
5564 const uint16_t inst_type)
5566 switch (inst_type) {
5568 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5570 *instruction_type = OVSINST_##ENUM; \
5576 return OFPERR_OFPBIC_UNKNOWN_INST;
5580 /* Two-way translation between OVS's internal "OVSINST_*" representation of
5581 * instructions and the "OFPIT_*" representation used in OpenFlow. */
5582 struct ovsinst_map {
5583 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
5584 int ofpit; /* OFPIT_* number from OpenFlow spec. */
5587 static const struct ovsinst_map *
5588 get_ovsinst_map(enum ofp_version version)
5590 /* OpenFlow 1.1 and 1.2 instructions. */
5591 static const struct ovsinst_map of11[] = {
5592 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5593 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5594 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5595 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5596 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5600 /* OpenFlow 1.3+ instructions. */
5601 static const struct ovsinst_map of13[] = {
5602 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5603 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5604 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5605 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5606 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5607 { OVSINST_OFPIT13_METER, 6 },
5611 return version < OFP13_VERSION ? of11 : of13;
5614 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
5615 * values, into a bitmap of instructions suitable for OpenFlow 'version'
5616 * (OFP11_VERSION or later), and returns the result. */
5618 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
5620 uint32_t ofpit_bitmap = 0;
5621 const struct ovsinst_map *x;
5623 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5624 if (ovsinst_bitmap & (1u << x->ovsinst)) {
5625 ofpit_bitmap |= 1u << x->ofpit;
5628 return htonl(ofpit_bitmap);
5631 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5632 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5633 * correspond to OVSINST_* values, and returns the result. */
5635 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
5637 uint32_t ovsinst_bitmap = 0;
5638 const struct ovsinst_map *x;
5640 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5641 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
5642 ovsinst_bitmap |= 1u << x->ovsinst;
5645 return ovsinst_bitmap;
5648 static inline struct ofp11_instruction *
5649 instruction_next(const struct ofp11_instruction *inst)
5651 return ((struct ofp11_instruction *) (void *)
5652 ((uint8_t *) inst + ntohs(inst->len)));
5656 instruction_is_valid(const struct ofp11_instruction *inst,
5657 size_t n_instructions)
5659 uint16_t len = ntohs(inst->len);
5660 return (!(len % OFP11_INSTRUCTION_ALIGN)
5661 && len >= sizeof *inst
5662 && len / sizeof *inst <= n_instructions);
5665 /* This macro is careful to check for instructions with bad lengths. */
5666 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
5667 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
5668 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
5669 ((LEFT) -= (ntohs((ITER)->len) \
5670 / sizeof(struct ofp11_instruction)), \
5671 (ITER) = instruction_next(ITER)))
5674 decode_openflow11_instruction(const struct ofp11_instruction *inst,
5675 enum ovs_instruction_type *type)
5677 uint16_t len = ntohs(inst->len);
5679 switch (inst->type) {
5680 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5681 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5683 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5684 case CONSTANT_HTONS(ENUM): \
5686 ? len >= sizeof(struct STRUCT) \
5687 : len == sizeof(struct STRUCT)) { \
5688 *type = OVSINST_##ENUM; \
5691 return OFPERR_OFPBIC_BAD_LEN; \
5697 return OFPERR_OFPBIC_UNKNOWN_INST;
5702 decode_openflow11_instructions(const struct ofp11_instruction insts[],
5704 const struct ofp11_instruction *out[])
5706 const struct ofp11_instruction *inst;
5709 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5710 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5711 enum ovs_instruction_type type;
5714 error = decode_openflow11_instruction(inst, &type);
5720 return OFPERR_OFPBIC_DUP_INST;
5726 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5727 (n_insts - left) * sizeof *inst);
5728 return OFPERR_OFPBIC_BAD_LEN;
5734 get_actions_from_instruction(const struct ofp11_instruction *inst,
5735 const struct ofp_action_header **actions,
5736 size_t *actions_len)
5738 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5739 *actions_len = ntohs(inst->len) - sizeof *inst;
5743 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5744 unsigned int instructions_len,
5745 enum ofp_version version,
5746 struct ofpbuf *ofpacts)
5748 const struct ofp11_instruction *instructions;
5749 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5752 if (version == OFP10_VERSION) {
5753 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5755 (1u << N_OVS_INSTRUCTIONS) - 1,
5759 ofpbuf_clear(ofpacts);
5761 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5762 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5764 instructions_len, OFP11_INSTRUCTION_ALIGN);
5765 error = OFPERR_OFPBIC_BAD_LEN;
5769 instructions = ofpbuf_try_pull(openflow, instructions_len);
5770 if (instructions == NULL) {
5771 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5772 "remaining message length (%"PRIu32")",
5773 instructions_len, openflow->size);
5774 error = OFPERR_OFPBIC_BAD_LEN;
5778 error = decode_openflow11_instructions(
5779 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5785 if (insts[OVSINST_OFPIT13_METER]) {
5786 const struct ofp13_instruction_meter *oim;
5787 struct ofpact_meter *om;
5789 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5790 insts[OVSINST_OFPIT13_METER]);
5792 om = ofpact_put_METER(ofpacts);
5793 om->meter_id = ntohl(oim->meter_id);
5795 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5796 const struct ofp_action_header *actions;
5799 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5800 &actions, &actions_len);
5801 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5806 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5807 instruction_get_OFPIT11_CLEAR_ACTIONS(
5808 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5809 ofpact_put_CLEAR_ACTIONS(ofpacts);
5811 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5812 struct ofpact_nest *on;
5813 const struct ofp_action_header *actions;
5817 ofpact_pad(ofpacts);
5818 start = ofpacts->size;
5819 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5820 offsetof(struct ofpact_nest, actions));
5821 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5822 &actions, &actions_len);
5823 error = ofpacts_decode_for_action_set(actions, actions_len,
5828 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5829 on->ofpact.len = ofpacts->size - start;
5831 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5832 const struct ofp11_instruction_write_metadata *oiwm;
5833 struct ofpact_metadata *om;
5835 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5836 insts[OVSINST_OFPIT11_WRITE_METADATA]);
5838 om = ofpact_put_WRITE_METADATA(ofpacts);
5839 om->metadata = oiwm->metadata;
5840 om->mask = oiwm->metadata_mask;
5842 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5843 const struct ofp11_instruction_goto_table *oigt;
5844 struct ofpact_goto_table *ogt;
5846 oigt = instruction_get_OFPIT11_GOTO_TABLE(
5847 insts[OVSINST_OFPIT11_GOTO_TABLE]);
5848 ogt = ofpact_put_GOTO_TABLE(ofpacts);
5849 ogt->table_id = oigt->table_id;
5852 error = ofpacts_verify(ofpacts->data, ofpacts->size,
5853 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
5856 ofpbuf_clear(ofpacts);
5861 /* Update the length of the instruction that begins at offset 'ofs' within
5862 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5863 * If the instruction contains no nested actions, deletes it entirely. */
5865 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5867 struct ofp11_instruction_actions *oia;
5869 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5870 if (openflow->size > ofs + sizeof *oia) {
5871 oia->len = htons(openflow->size - ofs);
5873 openflow->size = ofs;
5877 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5878 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5879 * valid, otherwise an OpenFlow error code. */
5881 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5889 case OFPP_CONTROLLER:
5894 return OFPERR_OFPBAC_BAD_OUT_PORT;
5897 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
5900 return OFPERR_OFPBAC_BAD_OUT_PORT;
5904 /* Removes the protocols that require consistency between match and actions
5905 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5907 * (An example of an inconsistency between match and actions is a flow that
5908 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5911 inconsistent_match(enum ofputil_protocol *usable_protocols)
5913 *usable_protocols &= OFPUTIL_P_OF10_ANY;
5916 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5917 * caller must restore them.
5919 * Modifies some actions, filling in fields that could not be properly set
5920 * without context. */
5922 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
5923 struct flow *flow, ofp_port_t max_ports,
5924 uint8_t table_id, uint8_t n_tables)
5926 const struct ofpact_enqueue *enqueue;
5927 const struct mf_field *mf;
5931 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
5934 case OFPACT_CONTROLLER:
5937 case OFPACT_ENQUEUE:
5938 enqueue = ofpact_get_ENQUEUE(a);
5939 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
5940 && enqueue->port != OFPP_IN_PORT
5941 && enqueue->port != OFPP_LOCAL) {
5942 return OFPERR_OFPBAC_BAD_OUT_PORT;
5946 case OFPACT_OUTPUT_REG:
5947 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
5950 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
5952 case OFPACT_SET_VLAN_VID:
5953 /* Remember if we saw a vlan tag in the flow to aid translating to
5954 * OpenFlow 1.1+ if need be. */
5955 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
5956 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5957 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5958 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5959 inconsistent_match(usable_protocols);
5961 /* Temporary mark that we have a vlan tag. */
5962 flow->vlan_tci |= htons(VLAN_CFI);
5965 case OFPACT_SET_VLAN_PCP:
5966 /* Remember if we saw a vlan tag in the flow to aid translating to
5967 * OpenFlow 1.1+ if need be. */
5968 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
5969 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5970 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5971 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5972 inconsistent_match(usable_protocols);
5974 /* Temporary mark that we have a vlan tag. */
5975 flow->vlan_tci |= htons(VLAN_CFI);
5978 case OFPACT_STRIP_VLAN:
5979 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5980 inconsistent_match(usable_protocols);
5982 /* Temporary mark that we have no vlan tag. */
5983 flow->vlan_tci = htons(0);
5986 case OFPACT_PUSH_VLAN:
5987 if (flow->vlan_tci & htons(VLAN_CFI)) {
5988 /* Multiple VLAN headers not supported. */
5989 return OFPERR_OFPBAC_BAD_TAG;
5991 /* Temporary mark that we have a vlan tag. */
5992 flow->vlan_tci |= htons(VLAN_CFI);
5995 case OFPACT_SET_ETH_SRC:
5996 case OFPACT_SET_ETH_DST:
5999 case OFPACT_SET_IPV4_SRC:
6000 case OFPACT_SET_IPV4_DST:
6001 if (flow->dl_type != htons(ETH_TYPE_IP)) {
6002 inconsistent_match(usable_protocols);
6006 case OFPACT_SET_IP_DSCP:
6007 case OFPACT_SET_IP_ECN:
6008 case OFPACT_SET_IP_TTL:
6009 case OFPACT_DEC_TTL:
6010 if (!is_ip_any(flow)) {
6011 inconsistent_match(usable_protocols);
6015 case OFPACT_SET_L4_SRC_PORT:
6016 case OFPACT_SET_L4_DST_PORT:
6017 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
6018 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
6019 && flow->nw_proto != IPPROTO_SCTP)) {
6020 inconsistent_match(usable_protocols);
6022 /* Note on which transport protocol the port numbers are set.
6023 * This allows this set action to be converted to an OF1.2 set field
6025 if (a->type == OFPACT_SET_L4_SRC_PORT) {
6026 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
6028 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
6032 case OFPACT_REG_MOVE:
6033 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
6035 case OFPACT_SET_FIELD:
6036 mf = ofpact_get_SET_FIELD(a)->field;
6037 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6038 if (!mf_are_prereqs_ok(mf, flow) ||
6039 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
6040 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
6042 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6044 /* Remember if we saw a vlan tag in the flow to aid translating to
6045 * OpenFlow 1.1 if need be. */
6046 ofpact_get_SET_FIELD(a)->flow_has_vlan =
6047 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6048 if (mf->id == MFF_VLAN_TCI) {
6049 /* The set field may add or remove the vlan tag,
6050 * Mark the status temporarily. */
6051 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
6055 case OFPACT_STACK_PUSH:
6056 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
6058 case OFPACT_STACK_POP:
6059 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6061 case OFPACT_SET_MPLS_LABEL:
6062 case OFPACT_SET_MPLS_TC:
6063 case OFPACT_SET_MPLS_TTL:
6064 case OFPACT_DEC_MPLS_TTL:
6065 if (!eth_type_mpls(flow->dl_type)) {
6066 inconsistent_match(usable_protocols);
6070 case OFPACT_SET_TUNNEL:
6071 case OFPACT_SET_QUEUE:
6072 case OFPACT_POP_QUEUE:
6073 case OFPACT_RESUBMIT:
6076 case OFPACT_FIN_TIMEOUT:
6077 if (flow->nw_proto != IPPROTO_TCP) {
6078 inconsistent_match(usable_protocols);
6083 return learn_check(ofpact_get_LEARN(a), flow);
6085 case OFPACT_CONJUNCTION:
6088 case OFPACT_MULTIPATH:
6089 return multipath_check(ofpact_get_MULTIPATH(a), flow);
6095 case OFPACT_PUSH_MPLS:
6096 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6097 /* The packet is now MPLS and the MPLS payload is opaque.
6098 * Thus nothing can be assumed about the network protocol.
6099 * Temporarily mark that we have no nw_proto. */
6103 case OFPACT_POP_MPLS:
6104 if (!eth_type_mpls(flow->dl_type)) {
6105 inconsistent_match(usable_protocols);
6107 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6114 struct ofpact_conntrack *oc = ofpact_get_CT(a);
6115 enum ofputil_protocol p = *usable_protocols;
6117 if (!dl_type_is_ip_any(flow->dl_type)
6118 || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6119 inconsistent_match(usable_protocols);
6122 if (oc->zone_src.field) {
6123 return mf_check_src(&oc->zone_src, flow);
6126 return ofpacts_check(oc->actions, ofpact_ct_get_action_len(oc),
6127 flow, max_ports, table_id, n_tables, &p);
6130 case OFPACT_CLEAR_ACTIONS:
6133 case OFPACT_WRITE_ACTIONS: {
6134 /* Use a temporary copy of 'usable_protocols' because we can't check
6135 * consistency of an action set. */
6136 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6137 enum ofputil_protocol p = *usable_protocols;
6138 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6139 flow, max_ports, table_id, n_tables, &p);
6142 case OFPACT_WRITE_METADATA:
6145 case OFPACT_METER: {
6146 uint32_t mid = ofpact_get_METER(a)->meter_id;
6147 if (mid == 0 || mid > OFPM13_MAX) {
6148 return OFPERR_OFPMMFC_INVALID_METER;
6153 case OFPACT_GOTO_TABLE: {
6154 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
6155 if ((table_id != 255 && goto_table <= table_id)
6156 || (n_tables != 255 && goto_table >= n_tables)) {
6157 return OFPERR_OFPBIC_BAD_TABLE_ID;
6165 case OFPACT_UNROLL_XLATE:
6166 /* UNROLL is an internal action that should never be seen via
6168 return OFPERR_OFPBAC_BAD_TYPE;
6170 case OFPACT_DEBUG_RECIRC:
6178 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6179 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6180 * switch with no more than 'max_ports' ports.
6182 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6183 * '*usable_protocols' the protocols that forbid the inconsistency. (An
6184 * example of an inconsistency between match and actions is a flow that does
6185 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6187 * May annotate ofpacts with information gathered from the 'flow'.
6189 * May temporarily modify 'flow', but restores the changes before returning. */
6191 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
6192 struct flow *flow, ofp_port_t max_ports,
6193 uint8_t table_id, uint8_t n_tables,
6194 enum ofputil_protocol *usable_protocols)
6197 ovs_be16 dl_type = flow->dl_type;
6198 ovs_be16 vlan_tci = flow->vlan_tci;
6199 uint8_t nw_proto = flow->nw_proto;
6200 enum ofperr error = 0;
6202 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6203 error = ofpact_check__(usable_protocols, a, flow,
6204 max_ports, table_id, n_tables);
6209 /* Restore fields that may have been modified. */
6210 flow->dl_type = dl_type;
6211 flow->vlan_tci = vlan_tci;
6212 flow->nw_proto = nw_proto;
6216 /* Like ofpacts_check(), but reports inconsistencies as
6217 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6219 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
6220 struct flow *flow, ofp_port_t max_ports,
6221 uint8_t table_id, uint8_t n_tables,
6222 enum ofputil_protocol usable_protocols)
6224 enum ofputil_protocol p = usable_protocols;
6227 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
6228 table_id, n_tables, &p);
6229 return (error ? error
6230 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
6234 static const struct mf_field *
6235 ofpact_get_mf_field(enum ofpact_type type, const void *ofpact)
6237 if (type == OFPACT_SET_FIELD) {
6238 const struct ofpact_set_field *orl = ofpact;
6241 } else if (type == OFPACT_REG_MOVE) {
6242 const struct ofpact_reg_move *orm = ofpact;
6244 return orm->dst.field;
6251 unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action)
6253 VLOG_WARN("%s action doesn't support nested action %s",
6254 ofpact_name(outer_action), ofpact_name(action));
6255 return OFPERR_OFPBAC_BAD_ARGUMENT;
6259 field_requires_ct(enum mf_field_id field)
6261 return field == MFF_CT_MARK || field == MFF_CT_LABEL;
6264 /* Apply nesting constraints for actions */
6266 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
6268 const struct mf_field *field = ofpact_get_mf_field(a->type, a);
6270 if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
6271 VLOG_WARN("cannot set CT fields outside of ct action");
6272 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6276 ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
6277 || outer_action == OFPACT_CT);
6279 if (outer_action == OFPACT_CT) {
6281 return unsupported_nesting(a->type, outer_action);
6282 } else if (!field_requires_ct(field->id)) {
6283 VLOG_WARN("%s action doesn't support nested modification "
6284 "of %s", ofpact_name(outer_action), field->name);
6285 return OFPERR_OFPBAC_BAD_ARGUMENT;
6293 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
6294 * appropriate order as defined by the OpenFlow spec and as required by Open
6297 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
6298 * instructions that are allowed within 'ofpacts[]'.
6300 * If 'outer_action' is not zero, it specifies that the actions are nested
6301 * within another action of type 'outer_action'. */
6303 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
6304 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
6306 const struct ofpact *a;
6307 enum ovs_instruction_type inst;
6309 inst = OVSINST_OFPIT13_METER;
6310 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6311 enum ovs_instruction_type next;
6314 if (a->type == OFPACT_CONJUNCTION) {
6315 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6316 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
6317 VLOG_WARN("\"conjunction\" actions may be used along with "
6318 "\"note\" but not any other kind of action "
6319 "(such as the \"%s\" action used here)",
6320 ofpact_name(a->type));
6321 return OFPERR_NXBAC_BAD_CONJUNCTION;
6327 error = ofpacts_verify_nested(a, outer_action);
6332 next = ovs_instruction_type_from_ofpact_type(a->type);
6334 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
6337 const char *name = ovs_instruction_name_from_type(inst);
6338 const char *next_name = ovs_instruction_name_from_type(next);
6341 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
6342 "1.1+ compatibility", name);
6344 VLOG_WARN("invalid instruction ordering: %s must appear "
6345 "before %s, for OpenFlow 1.1+ compatibility",
6348 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
6350 if (!((1u << next) & allowed_ovsinsts)) {
6351 const char *name = ovs_instruction_name_from_type(next);
6353 VLOG_WARN("%s instruction not allowed here", name);
6354 return OFPERR_OFPBIC_UNSUP_INST;
6363 /* Converting ofpacts to OpenFlow. */
6366 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
6370 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6371 case OFPACT_##ENUM: \
6372 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
6381 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
6382 * actions in 'openflow', appending the actions to any existing data in
6385 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
6386 struct ofpbuf *openflow,
6387 enum ofp_version ofp_version)
6389 const struct ofpact *a;
6390 size_t start_size = openflow->size;
6392 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6393 encode_ofpact(a, ofp_version, openflow);
6395 return openflow->size - start_size;
6398 static enum ovs_instruction_type
6399 ofpact_is_apply_actions(const struct ofpact *a)
6401 return (ovs_instruction_type_from_ofpact_type(a->type)
6402 == OVSINST_OFPIT11_APPLY_ACTIONS);
6406 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
6408 struct ofpbuf *openflow,
6409 enum ofp_version ofp_version)
6411 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
6412 const struct ofpact *a;
6414 if (ofp_version == OFP10_VERSION) {
6415 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
6422 if (ofpact_is_apply_actions(a)) {
6423 size_t ofs = openflow->size;
6425 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
6427 encode_ofpact(a, ofp_version, openflow);
6429 } while (a < end && ofpact_is_apply_actions(a));
6430 ofpacts_update_instruction_actions(openflow, ofs);
6432 encode_ofpact(a, ofp_version, openflow);
6438 /* Sets of supported actions. */
6440 /* Two-way translation between OVS's internal "OFPACT_*" representation of
6441 * actions and the "OFPAT_*" representation used in some OpenFlow version.
6442 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
6443 * instance is specific to one OpenFlow version.) */
6445 enum ofpact_type ofpact; /* Internal name for action type. */
6446 int ofpat; /* OFPAT_* number from OpenFlow spec. */
6449 static const struct ofpact_map *
6450 get_ofpact_map(enum ofp_version version)
6452 /* OpenFlow 1.0 actions. */
6453 static const struct ofpact_map of10[] = {
6454 { OFPACT_OUTPUT, 0 },
6455 { OFPACT_SET_VLAN_VID, 1 },
6456 { OFPACT_SET_VLAN_PCP, 2 },
6457 { OFPACT_STRIP_VLAN, 3 },
6458 { OFPACT_SET_ETH_SRC, 4 },
6459 { OFPACT_SET_ETH_DST, 5 },
6460 { OFPACT_SET_IPV4_SRC, 6 },
6461 { OFPACT_SET_IPV4_DST, 7 },
6462 { OFPACT_SET_IP_DSCP, 8 },
6463 { OFPACT_SET_L4_SRC_PORT, 9 },
6464 { OFPACT_SET_L4_DST_PORT, 10 },
6465 { OFPACT_ENQUEUE, 11 },
6469 /* OpenFlow 1.1 actions. */
6470 static const struct ofpact_map of11[] = {
6471 { OFPACT_OUTPUT, 0 },
6472 { OFPACT_SET_VLAN_VID, 1 },
6473 { OFPACT_SET_VLAN_PCP, 2 },
6474 { OFPACT_SET_ETH_SRC, 3 },
6475 { OFPACT_SET_ETH_DST, 4 },
6476 { OFPACT_SET_IPV4_SRC, 5 },
6477 { OFPACT_SET_IPV4_DST, 6 },
6478 { OFPACT_SET_IP_DSCP, 7 },
6479 { OFPACT_SET_IP_ECN, 8 },
6480 { OFPACT_SET_L4_SRC_PORT, 9 },
6481 { OFPACT_SET_L4_DST_PORT, 10 },
6482 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6483 /* OFPAT_COPY_TTL_IN (12) not supported. */
6484 { OFPACT_SET_MPLS_LABEL, 13 },
6485 { OFPACT_SET_MPLS_TC, 14 },
6486 { OFPACT_SET_MPLS_TTL, 15 },
6487 { OFPACT_DEC_MPLS_TTL, 16 },
6488 { OFPACT_PUSH_VLAN, 17 },
6489 { OFPACT_STRIP_VLAN, 18 },
6490 { OFPACT_PUSH_MPLS, 19 },
6491 { OFPACT_POP_MPLS, 20 },
6492 { OFPACT_SET_QUEUE, 21 },
6493 { OFPACT_GROUP, 22 },
6494 { OFPACT_SET_IP_TTL, 23 },
6495 { OFPACT_DEC_TTL, 24 },
6499 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6500 static const struct ofpact_map of12[] = {
6501 { OFPACT_OUTPUT, 0 },
6502 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6503 /* OFPAT_COPY_TTL_IN (12) not supported. */
6504 { OFPACT_SET_MPLS_TTL, 15 },
6505 { OFPACT_DEC_MPLS_TTL, 16 },
6506 { OFPACT_PUSH_VLAN, 17 },
6507 { OFPACT_STRIP_VLAN, 18 },
6508 { OFPACT_PUSH_MPLS, 19 },
6509 { OFPACT_POP_MPLS, 20 },
6510 { OFPACT_SET_QUEUE, 21 },
6511 { OFPACT_GROUP, 22 },
6512 { OFPACT_SET_IP_TTL, 23 },
6513 { OFPACT_DEC_TTL, 24 },
6514 { OFPACT_SET_FIELD, 25 },
6515 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
6516 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
6536 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
6537 * values, into a bitmap of actions suitable for OpenFlow 'version', and
6538 * returns the result. */
6540 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
6542 uint32_t openflow_bitmap = 0;
6543 const struct ofpact_map *x;
6545 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6546 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
6547 openflow_bitmap |= 1u << x->ofpat;
6550 return htonl(openflow_bitmap);
6553 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
6554 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
6555 * and returns the result. */
6557 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
6559 uint64_t ofpact_bitmap = 0;
6560 const struct ofpact_map *x;
6562 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6563 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
6564 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
6567 return ofpact_bitmap;
6570 /* Appends to 's' a string representation of the set of OFPACT_* represented
6571 * by 'ofpacts_bitmap'. */
6573 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
6575 if (!ofpacts_bitmap) {
6576 ds_put_cstr(s, "<none>");
6578 while (ofpacts_bitmap) {
6579 ds_put_format(s, "%s ",
6580 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
6581 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
6587 /* Returns true if 'action' outputs to 'port', false otherwise. */
6589 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
6591 switch (ofpact->type) {
6593 return ofpact_get_OUTPUT(ofpact)->port == port;
6594 case OFPACT_ENQUEUE:
6595 return ofpact_get_ENQUEUE(ofpact)->port == port;
6596 case OFPACT_CONTROLLER:
6597 return port == OFPP_CONTROLLER;
6599 case OFPACT_OUTPUT_REG:
6601 case OFPACT_SET_VLAN_VID:
6602 case OFPACT_SET_VLAN_PCP:
6603 case OFPACT_STRIP_VLAN:
6604 case OFPACT_PUSH_VLAN:
6605 case OFPACT_SET_ETH_SRC:
6606 case OFPACT_SET_ETH_DST:
6607 case OFPACT_SET_IPV4_SRC:
6608 case OFPACT_SET_IPV4_DST:
6609 case OFPACT_SET_IP_DSCP:
6610 case OFPACT_SET_IP_ECN:
6611 case OFPACT_SET_IP_TTL:
6612 case OFPACT_SET_L4_SRC_PORT:
6613 case OFPACT_SET_L4_DST_PORT:
6614 case OFPACT_REG_MOVE:
6615 case OFPACT_SET_FIELD:
6616 case OFPACT_STACK_PUSH:
6617 case OFPACT_STACK_POP:
6618 case OFPACT_DEC_TTL:
6619 case OFPACT_SET_MPLS_LABEL:
6620 case OFPACT_SET_MPLS_TC:
6621 case OFPACT_SET_MPLS_TTL:
6622 case OFPACT_DEC_MPLS_TTL:
6623 case OFPACT_SET_TUNNEL:
6624 case OFPACT_WRITE_METADATA:
6625 case OFPACT_SET_QUEUE:
6626 case OFPACT_POP_QUEUE:
6627 case OFPACT_FIN_TIMEOUT:
6628 case OFPACT_RESUBMIT:
6630 case OFPACT_CONJUNCTION:
6631 case OFPACT_MULTIPATH:
6634 case OFPACT_UNROLL_XLATE:
6635 case OFPACT_PUSH_MPLS:
6636 case OFPACT_POP_MPLS:
6638 case OFPACT_CLEAR_ACTIONS:
6639 case OFPACT_WRITE_ACTIONS:
6640 case OFPACT_GOTO_TABLE:
6643 case OFPACT_DEBUG_RECIRC:
6650 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6651 * to 'port', false otherwise. */
6653 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
6656 const struct ofpact *a;
6658 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6659 if (ofpact_outputs_to_port(a, port)) {
6667 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6668 * to 'group', false otherwise. */
6670 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
6673 const struct ofpact *a;
6675 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6676 if (a->type == OFPACT_GROUP
6677 && ofpact_get_GROUP(a)->group_id == group_id) {
6686 ofpacts_equal(const struct ofpact *a, size_t a_len,
6687 const struct ofpact *b, size_t b_len)
6689 return a_len == b_len && !memcmp(a, b, a_len);
6692 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
6693 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
6695 * This function relies on the order of 'ofpacts' being correct (as checked by
6696 * ofpacts_verify()). */
6698 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
6700 const struct ofpact *a;
6702 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6703 enum ovs_instruction_type inst;
6705 inst = ovs_instruction_type_from_ofpact_type(a->type);
6706 if (a->type == OFPACT_METER) {
6707 return ofpact_get_METER(a)->meter_id;
6708 } else if (inst > OVSINST_OFPIT13_METER) {
6716 /* Formatting ofpacts. */
6719 ofpact_format(const struct ofpact *a, struct ds *s)
6722 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6723 case OFPACT_##ENUM: \
6724 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
6733 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
6734 * 'ofpacts' to 'string'. */
6736 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
6740 ds_put_cstr(string, "drop");
6742 const struct ofpact *a;
6744 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6746 ds_put_cstr(string, ",");
6749 /* XXX write-actions */
6750 ofpact_format(a, string);
6755 /* Internal use by helpers. */
6758 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
6760 struct ofpact *ofpact;
6762 ofpact_pad(ofpacts);
6763 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
6764 ofpact = ofpacts->header;
6765 ofpact_init(ofpact, type, len);
6770 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
6772 memset(ofpact, 0, len);
6773 ofpact->type = type;
6778 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6779 * starting at 'ofpact'.
6781 * This is the correct way to update a variable-length ofpact's length after
6782 * adding the variable-length part of the payload. (See the large comment
6783 * near the end of ofp-actions.h for more information.) */
6785 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
6787 ovs_assert(ofpact == ofpacts->header);
6788 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6791 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6792 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6793 * client must call this itself after adding the final ofpact to an array of
6796 * (The consequences of failing to call this function are probably not dire.
6797 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6798 * not dereference it. That's undefined behavior, technically, but it will not
6799 * cause a real problem on common systems. Still, it seems better to call
6802 ofpact_pad(struct ofpbuf *ofpacts)
6804 unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
6806 ofpbuf_put_zeros(ofpacts, pad);
6813 static char * OVS_WARN_UNUSED_RESULT
6814 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6815 enum ofputil_protocol *usable_protocols)
6818 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6819 case OFPACT_##ENUM: \
6820 return parse_##ENUM(value, ofpacts, usable_protocols);
6829 ofpact_type_from_name(const char *name, enum ofpact_type *type)
6831 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6832 if (!strcasecmp(name, NAME)) { \
6833 *type = OFPACT_##ENUM; \
6842 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6844 * Returns NULL if successful, otherwise a malloc()'d string describing the
6845 * error. The caller is responsible for freeing the returned string.
6847 * If 'outer_action' is specified, indicates that the actions being parsed
6848 * are nested within another action of the type specified in 'outer_action'. */
6849 static char * OVS_WARN_UNUSED_RESULT
6850 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6851 enum ofputil_protocol *usable_protocols,
6852 bool allow_instructions, enum ofpact_type outer_action)
6861 while (ofputil_parse_key_value(&pos, &key, &value)) {
6862 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6863 enum ofpact_type type;
6867 if (ofpact_type_from_name(key, &type)) {
6868 error = ofpact_parse(type, value, ofpacts, usable_protocols);
6869 inst = ovs_instruction_type_from_ofpact_type(type);
6870 } else if (!strcasecmp(key, "mod_vlan_vid")) {
6871 error = parse_set_vlan_vid(value, ofpacts, true);
6872 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6873 error = parse_set_vlan_pcp(value, ofpacts, true);
6874 } else if (!strcasecmp(key, "set_nw_ttl")) {
6875 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6876 } else if (!strcasecmp(key, "pop_vlan")) {
6877 error = parse_pop_vlan(ofpacts);
6878 } else if (!strcasecmp(key, "set_tunnel64")) {
6879 error = parse_set_tunnel(value, ofpacts,
6880 NXAST_RAW_SET_TUNNEL64);
6881 } else if (!strcasecmp(key, "load")) {
6882 error = parse_reg_load(value, ofpacts);
6883 } else if (!strcasecmp(key, "bundle_load")) {
6884 error = parse_bundle_load(value, ofpacts);
6885 } else if (!strcasecmp(key, "drop")) {
6887 } else if (!strcasecmp(key, "apply_actions")) {
6888 return xstrdup("apply_actions is the default instruction");
6889 } else if (ofputil_port_from_string(key, &port)) {
6890 ofpact_put_OUTPUT(ofpacts)->port = port;
6892 return xasprintf("unknown action %s", key);
6898 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
6899 if (!allow_instructions) {
6900 return xasprintf("only actions are allowed here (not "
6902 ovs_instruction_name_from_type(inst));
6904 if (inst == prev_inst) {
6905 return xasprintf("instruction %s may be specified only once",
6906 ovs_instruction_name_from_type(inst));
6909 if (prev_inst != -1 && inst < prev_inst) {
6910 return xasprintf("instruction %s must be specified before %s",
6911 ovs_instruction_name_from_type(inst),
6912 ovs_instruction_name_from_type(prev_inst));
6916 ofpact_pad(ofpacts);
6918 if (drop && ofpacts->size) {
6919 return xstrdup("\"drop\" must not be accompanied by any other action "
6923 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
6925 ? (1u << N_OVS_INSTRUCTIONS) - 1
6926 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
6929 return xstrdup("Incorrect instruction ordering");
6935 static char * OVS_WARN_UNUSED_RESULT
6936 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
6937 enum ofputil_protocol *usable_protocols, bool allow_instructions,
6938 enum ofpact_type outer_action)
6940 uint32_t orig_size = ofpacts->size;
6941 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
6942 allow_instructions, outer_action);
6944 ofpacts->size = orig_size;
6949 static char * OVS_WARN_UNUSED_RESULT
6950 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
6951 enum ofputil_protocol *usable_protocols,
6952 bool allow_instructions, enum ofpact_type outer_action)
6956 *usable_protocols = OFPUTIL_P_ANY;
6959 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
6966 /* Parses 's' as a set of OpenFlow actions and appends the actions to
6967 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
6968 * that are nested within the action of type 'outer_action'.
6970 * Returns NULL if successful, otherwise a malloc()'d string describing the
6971 * error. The caller is responsible for freeing the returned string. */
6972 char * OVS_WARN_UNUSED_RESULT
6973 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
6974 enum ofputil_protocol *usable_protocols)
6976 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
6979 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
6982 * Returns NULL if successful, otherwise a malloc()'d string describing the
6983 * error. The caller is responsible for freeing the returned string. */
6984 char * OVS_WARN_UNUSED_RESULT
6985 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
6986 enum ofputil_protocol *usable_protocols)
6988 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
6992 ofpact_name(enum ofpact_type type)
6995 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7002 /* Low-level action decoding and encoding functions. */
7004 /* Everything needed to identify a particular OpenFlow action. */
7005 struct ofpact_hdrs {
7006 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
7007 uint16_t type; /* Type if standard, otherwise subtype. */
7008 uint8_t ofp_version; /* From ofp_header. */
7011 /* Information about a particular OpenFlow action. */
7012 struct ofpact_raw_instance {
7013 /* The action's identity. */
7014 struct ofpact_hdrs hdrs;
7015 enum ofp_raw_action_type raw;
7017 /* Looking up the action. */
7018 struct hmap_node decode_node; /* Based on 'hdrs'. */
7019 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
7021 /* The action's encoded size.
7023 * If this action is fixed-length, 'min_length' == 'max_length'.
7024 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7025 * OFP_ACTION_ALIGN) == 65528. */
7026 unsigned short int min_length;
7027 unsigned short int max_length;
7029 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7030 * offset of that argument from the beginning of the action and 'arg_len'
7031 * its length, both in bytes.
7033 * For actions that take other forms, these are both zero. */
7034 unsigned short int arg_ofs;
7035 unsigned short int arg_len;
7037 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7040 /* If this action is deprecated, a human-readable string with a brief
7042 const char *deprecation;
7045 /* Action header. */
7046 struct ofp_action_header {
7047 /* The meaning of other values of 'type' generally depends on the OpenFlow
7048 * version (see enum ofp_raw_action_type).
7050 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7051 * designates an OpenFlow vendor ID and that the remainder of the action
7052 * structure has a vendor-defined meaning.
7054 #define OFPAT_VENDOR 0xffff
7057 /* Always a multiple of 8. */
7060 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7061 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
7064 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
7066 /* Header for Nicira-defined actions and for ONF vendor extensions.
7068 * This cannot be used as an entirely generic vendor extension action header,
7069 * because OpenFlow does not specify the location or size of the action
7070 * subtype; it just happens that ONF extensions and Nicira extensions share
7072 struct ext_action_header {
7073 ovs_be16 type; /* OFPAT_VENDOR. */
7074 ovs_be16 len; /* At least 16. */
7075 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
7076 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
7079 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
7082 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
7083 const struct ofpact_hdrs *b)
7085 return (a->vendor == b->vendor
7086 && a->type == b->type
7087 && a->ofp_version == b->ofp_version);
7091 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
7093 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
7096 #include "ofp-actions.inc2"
7098 static struct hmap *
7099 ofpact_decode_hmap(void)
7101 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7102 static struct hmap hmap;
7104 if (ovsthread_once_start(&once)) {
7105 struct ofpact_raw_instance *inst;
7108 for (inst = all_raw_instances;
7109 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7111 hmap_insert(&hmap, &inst->decode_node,
7112 ofpact_hdrs_hash(&inst->hdrs));
7114 ovsthread_once_done(&once);
7119 static struct hmap *
7120 ofpact_encode_hmap(void)
7122 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7123 static struct hmap hmap;
7125 if (ovsthread_once_start(&once)) {
7126 struct ofpact_raw_instance *inst;
7129 for (inst = all_raw_instances;
7130 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7132 hmap_insert(&hmap, &inst->encode_node,
7133 hash_2words(inst->raw, inst->hdrs.ofp_version));
7135 ovsthread_once_done(&once);
7141 ofpact_decode_raw(enum ofp_version ofp_version,
7142 const struct ofp_action_header *oah, size_t length,
7143 const struct ofpact_raw_instance **instp)
7145 const struct ofpact_raw_instance *inst;
7146 struct ofpact_hdrs hdrs;
7149 if (length < sizeof *oah) {
7150 return OFPERR_OFPBAC_BAD_LEN;
7153 /* Get base action type. */
7154 if (oah->type == htons(OFPAT_VENDOR)) {
7156 hdrs.vendor = ntohl(oah->vendor);
7157 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
7158 /* Get extension subtype. */
7159 const struct ext_action_header *nah;
7161 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
7162 if (length < sizeof *nah) {
7163 return OFPERR_OFPBAC_BAD_LEN;
7165 hdrs.type = ntohs(nah->subtype);
7167 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
7169 return OFPERR_OFPBAC_BAD_VENDOR;
7173 hdrs.type = ntohs(oah->type);
7176 hdrs.ofp_version = ofp_version;
7177 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
7178 ofpact_decode_hmap()) {
7179 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
7186 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7187 : OFPERR_OFPBAC_BAD_TYPE);
7191 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7192 enum ofp_raw_action_type *raw, uint64_t *arg)
7194 const struct ofp_action_header *oah = buf->data;
7195 const struct ofpact_raw_instance *action;
7196 unsigned int length;
7200 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
7205 if (action->deprecation) {
7206 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
7207 action->name, ofputil_version_to_string(ofp_version),
7208 action->deprecation);
7211 length = ntohs(oah->len);
7212 if (length > buf->size) {
7213 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
7214 "length %"PRIu32, action->name, length, buf->size);
7215 return OFPERR_OFPBAC_BAD_LEN;
7217 if (length < action->min_length || length > action->max_length) {
7218 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
7219 "[%hu,%hu]", action->name, length,
7220 action->min_length, action->max_length);
7221 return OFPERR_OFPBAC_BAD_LEN;
7224 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
7225 "of 8", action->name, length);
7226 return OFPERR_OFPBAC_BAD_LEN;
7231 if (action->arg_len) {
7235 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
7236 for (i = 0; i < action->arg_len; i++) {
7237 *arg = (*arg << 8) | p[i];
7241 ofpbuf_pull(buf, length);
7246 static const struct ofpact_raw_instance *
7247 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
7249 const struct ofpact_raw_instance *inst;
7251 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
7252 ofpact_encode_hmap()) {
7253 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
7261 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7262 enum ofp_raw_action_type raw, uint64_t arg)
7264 const struct ofpact_raw_instance *inst;
7265 struct ofp_action_header *oah;
7266 const struct ofpact_hdrs *hdrs;
7268 inst = ofpact_raw_lookup(ofp_version, raw);
7271 oah = ofpbuf_put_zeros(buf, inst->min_length);
7272 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
7273 oah->len = htons(inst->min_length);
7274 oah->vendor = htonl(hdrs->vendor);
7276 switch (hdrs->vendor) {
7281 case ONF_VENDOR_ID: {
7282 struct ext_action_header *nah = (struct ext_action_header *) oah;
7283 nah->subtype = htons(hdrs->type);
7291 if (inst->arg_len) {
7292 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
7295 for (i = 0; i < inst->arg_len; i++) {
7307 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
7309 struct ofp_action_header *oah;
7311 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs, 8));
7313 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
7314 oah->len = htons(openflow->size - start_ofs);