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);
357 /* Pull off existing actions or instructions. Used by nesting actions to keep
358 * ofpacts_parse() oblivious of actions nesting.
360 * Push the actions back on after nested parsing, e.g.:
362 * size_t ofs = ofpacts_pull(ofpacts);
363 * ...nested parsing...
364 * ofpbuf_push_uninit(ofpacts, ofs);
367 ofpacts_pull(struct ofpbuf *ofpacts)
373 ofpbuf_pull(ofpacts, ofs);
378 #include "ofp-actions.inc1"
380 /* Output actions. */
382 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
383 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
384 * number of bytes to send. A 'max_len' of zero means no bytes of the
385 * packet should be sent. */
386 struct ofp10_action_output {
387 ovs_be16 type; /* OFPAT10_OUTPUT. */
388 ovs_be16 len; /* Length is 8. */
389 ovs_be16 port; /* Output port. */
390 ovs_be16 max_len; /* Max length to send to controller. */
392 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
394 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
395 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
396 * number of bytes to send. A 'max_len' of zero means no bytes of the
397 * packet should be sent.*/
398 struct ofp11_action_output {
399 ovs_be16 type; /* OFPAT11_OUTPUT. */
400 ovs_be16 len; /* Length is 16. */
401 ovs_be32 port; /* Output port. */
402 ovs_be16 max_len; /* Max length to send to controller. */
403 uint8_t pad[6]; /* Pad to 64 bits. */
405 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
408 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
409 enum ofp_version ofp_version OVS_UNUSED,
412 struct ofpact_output *output;
414 output = ofpact_put_OUTPUT(out);
415 output->port = u16_to_ofp(ntohs(oao->port));
416 output->max_len = ntohs(oao->max_len);
418 return ofpact_check_output_port(output->port, OFPP_MAX);
422 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
423 enum ofp_version ofp_version OVS_UNUSED,
426 struct ofpact_output *output;
429 output = ofpact_put_OUTPUT(out);
430 output->max_len = ntohs(oao->max_len);
432 error = ofputil_port_from_ofp11(oao->port, &output->port);
437 return ofpact_check_output_port(output->port, OFPP_MAX);
441 encode_OUTPUT(const struct ofpact_output *output,
442 enum ofp_version ofp_version, struct ofpbuf *out)
444 if (ofp_version == OFP10_VERSION) {
445 struct ofp10_action_output *oao;
447 oao = put_OFPAT10_OUTPUT(out);
448 oao->port = htons(ofp_to_u16(output->port));
449 oao->max_len = htons(output->max_len);
451 struct ofp11_action_output *oao;
453 oao = put_OFPAT11_OUTPUT(out);
454 oao->port = ofputil_port_to_ofp11(output->port);
455 oao->max_len = htons(output->max_len);
459 static char * OVS_WARN_UNUSED_RESULT
460 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
461 enum ofputil_protocol *usable_protocols OVS_UNUSED)
463 if (strchr(arg, '[')) {
464 struct ofpact_output_reg *output_reg;
466 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
467 output_reg->max_len = UINT16_MAX;
468 return mf_parse_subfield(&output_reg->src, arg);
470 struct ofpact_output *output;
472 output = ofpact_put_OUTPUT(ofpacts);
473 if (!ofputil_port_from_string(arg, &output->port)) {
474 return xasprintf("%s: output to unknown port", arg);
476 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
482 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
484 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
485 ds_put_format(s, "output:%"PRIu16, a->port);
487 ofputil_format_port(a->port, s);
488 if (a->port == OFPP_CONTROLLER) {
489 ds_put_format(s, ":%"PRIu16, a->max_len);
497 decode_OFPAT_RAW11_GROUP(uint32_t group_id,
498 enum ofp_version ofp_version OVS_UNUSED,
501 ofpact_put_GROUP(out)->group_id = group_id;
506 encode_GROUP(const struct ofpact_group *group,
507 enum ofp_version ofp_version, struct ofpbuf *out)
509 if (ofp_version == OFP10_VERSION) {
512 put_OFPAT11_GROUP(out, group->group_id);
516 static char * OVS_WARN_UNUSED_RESULT
517 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
518 enum ofputil_protocol *usable_protocols OVS_UNUSED)
520 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
524 format_GROUP(const struct ofpact_group *a, struct ds *s)
526 ds_put_format(s, "group:%"PRIu32, a->group_id);
529 /* Action structure for NXAST_CONTROLLER.
531 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
532 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
535 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
537 * - 'controller_id': The ID of the controller connection to which the
538 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
539 * sent only to controllers that have the specified controller connection
540 * ID. See "struct nx_controller_id" for more information. */
541 struct nx_action_controller {
542 ovs_be16 type; /* OFPAT_VENDOR. */
543 ovs_be16 len; /* Length is 16. */
544 ovs_be32 vendor; /* NX_VENDOR_ID. */
545 ovs_be16 subtype; /* NXAST_CONTROLLER. */
546 ovs_be16 max_len; /* Maximum length to send to controller. */
547 ovs_be16 controller_id; /* Controller ID to send packet-in. */
548 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
549 uint8_t zero; /* Must be zero. */
551 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
554 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
555 enum ofp_version ofp_version OVS_UNUSED,
558 struct ofpact_controller *oc;
560 oc = ofpact_put_CONTROLLER(out);
561 oc->max_len = ntohs(nac->max_len);
562 oc->controller_id = ntohs(nac->controller_id);
563 oc->reason = nac->reason;
568 encode_CONTROLLER(const struct ofpact_controller *controller,
569 enum ofp_version ofp_version OVS_UNUSED,
572 struct nx_action_controller *nac;
574 nac = put_NXAST_CONTROLLER(out);
575 nac->max_len = htons(controller->max_len);
576 nac->controller_id = htons(controller->controller_id);
577 nac->reason = controller->reason;
580 static char * OVS_WARN_UNUSED_RESULT
581 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
582 enum ofputil_protocol *usable_protocols OVS_UNUSED)
584 enum ofp_packet_in_reason reason = OFPR_ACTION;
585 uint16_t controller_id = 0;
586 uint16_t max_len = UINT16_MAX;
590 } else if (strspn(arg, "0123456789") == strlen(arg)) {
591 char *error = str_to_u16(arg, "max_len", &max_len);
598 while (ofputil_parse_key_value(&arg, &name, &value)) {
599 if (!strcmp(name, "reason")) {
600 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
601 return xasprintf("unknown reason \"%s\"", value);
603 } else if (!strcmp(name, "max_len")) {
604 char *error = str_to_u16(value, "max_len", &max_len);
608 } else if (!strcmp(name, "id")) {
609 char *error = str_to_u16(value, "id", &controller_id);
614 return xasprintf("unknown key \"%s\" parsing controller "
620 if (reason == OFPR_ACTION && controller_id == 0) {
621 struct ofpact_output *output;
623 output = ofpact_put_OUTPUT(ofpacts);
624 output->port = OFPP_CONTROLLER;
625 output->max_len = max_len;
627 struct ofpact_controller *controller;
629 controller = ofpact_put_CONTROLLER(ofpacts);
630 controller->max_len = max_len;
631 controller->reason = reason;
632 controller->controller_id = controller_id;
639 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
641 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
642 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
644 enum ofp_packet_in_reason reason = a->reason;
646 ds_put_cstr(s, "controller(");
647 if (reason != OFPR_ACTION) {
648 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
650 ds_put_format(s, "reason=%s,",
651 ofputil_packet_in_reason_to_string(
652 reason, reasonbuf, sizeof reasonbuf));
654 if (a->max_len != UINT16_MAX) {
655 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
657 if (a->controller_id != 0) {
658 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
665 /* Enqueue action. */
666 struct ofp10_action_enqueue {
667 ovs_be16 type; /* OFPAT10_ENQUEUE. */
668 ovs_be16 len; /* Len is 16. */
669 ovs_be16 port; /* Port that queue belongs. Should
670 refer to a valid physical port
671 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
672 uint8_t pad[6]; /* Pad for 64-bit alignment. */
673 ovs_be32 queue_id; /* Where to enqueue the packets. */
675 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
678 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
679 enum ofp_version ofp_version OVS_UNUSED,
682 struct ofpact_enqueue *enqueue;
684 enqueue = ofpact_put_ENQUEUE(out);
685 enqueue->port = u16_to_ofp(ntohs(oae->port));
686 enqueue->queue = ntohl(oae->queue_id);
687 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
688 && enqueue->port != OFPP_IN_PORT
689 && enqueue->port != OFPP_LOCAL) {
690 return OFPERR_OFPBAC_BAD_OUT_PORT;
696 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
697 enum ofp_version ofp_version, struct ofpbuf *out)
699 if (ofp_version == OFP10_VERSION) {
700 struct ofp10_action_enqueue *oae;
702 oae = put_OFPAT10_ENQUEUE(out);
703 oae->port = htons(ofp_to_u16(enqueue->port));
704 oae->queue_id = htonl(enqueue->queue);
710 static char * OVS_WARN_UNUSED_RESULT
711 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
712 enum ofputil_protocol *usable_protocols OVS_UNUSED)
715 char *port = strtok_r(arg, ":q,", &sp);
716 char *queue = strtok_r(NULL, "", &sp);
717 struct ofpact_enqueue *enqueue;
719 if (port == NULL || queue == NULL) {
720 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
721 "\"enqueue(PORT,QUEUE)\"");
724 enqueue = ofpact_put_ENQUEUE(ofpacts);
725 if (!ofputil_port_from_string(port, &enqueue->port)) {
726 return xasprintf("%s: enqueue to unknown port", port);
728 return str_to_u32(queue, &enqueue->queue);
732 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
734 ds_put_format(s, "enqueue:");
735 ofputil_format_port(a->port, s);
736 ds_put_format(s, ":%"PRIu32, a->queue);
739 /* Action structure for NXAST_OUTPUT_REG.
741 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
743 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
744 * the NXAST_REG_LOAD action.
746 * The acceptable nxm_header values for 'src' are the same as the acceptable
747 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
749 * The 'max_len' field indicates the number of bytes to send when the chosen
750 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
751 * field of OFPAT_OUTPUT.
753 * The 'zero' field is required to be zeroed for forward compatibility. */
754 struct nx_action_output_reg {
755 ovs_be16 type; /* OFPAT_VENDOR. */
756 ovs_be16 len; /* 24. */
757 ovs_be32 vendor; /* NX_VENDOR_ID. */
758 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
760 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
761 ovs_be32 src; /* Source. */
763 ovs_be16 max_len; /* Max length to send to controller. */
765 uint8_t zero[6]; /* Reserved, must be zero. */
767 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
769 /* Action structure for NXAST_OUTPUT_REG2.
771 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
772 * experimenter OXM as 'src'.
774 struct nx_action_output_reg2 {
775 ovs_be16 type; /* OFPAT_VENDOR. */
776 ovs_be16 len; /* 24. */
777 ovs_be32 vendor; /* NX_VENDOR_ID. */
778 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
780 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
781 ovs_be16 max_len; /* Max length to send to controller. */
784 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
785 * - Enough 0-bytes to pad the action out to 24 bytes. */
788 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
791 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
792 enum ofp_version ofp_version OVS_UNUSED,
795 struct ofpact_output_reg *output_reg;
797 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
798 return OFPERR_OFPBAC_BAD_ARGUMENT;
801 output_reg = ofpact_put_OUTPUT_REG(out);
802 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
803 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
804 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
805 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
806 output_reg->max_len = ntohs(naor->max_len);
808 return mf_check_src(&output_reg->src, NULL);
812 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
813 enum ofp_version ofp_version OVS_UNUSED,
816 struct ofpact_output_reg *output_reg;
820 output_reg = ofpact_put_OUTPUT_REG(out);
821 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
822 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
823 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
824 output_reg->max_len = ntohs(naor->max_len);
826 ofpbuf_use_const(&b, naor, ntohs(naor->len));
827 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
828 error = nx_pull_header(&b, &output_reg->src.field, NULL);
832 if (!is_all_zeros(b.data, b.size)) {
833 return OFPERR_NXBRC_MUST_BE_ZERO;
836 return mf_check_src(&output_reg->src, NULL);
840 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
841 enum ofp_version ofp_version OVS_UNUSED,
844 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
845 * cannot be encoded in the older form, encode it as
846 * NXAST_RAW_OUTPUT_REG2. */
847 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
848 || !mf_nxm_header(output_reg->src.field->id)) {
849 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
850 size_t size = out->size;
852 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
853 output_reg->src.n_bits);
854 naor->max_len = htons(output_reg->max_len);
856 out->size = size - sizeof naor->pad;
857 nx_put_header(out, output_reg->src.field->id, 0, false);
860 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
862 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
863 output_reg->src.n_bits);
864 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
865 naor->max_len = htons(output_reg->max_len);
869 static char * OVS_WARN_UNUSED_RESULT
870 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
871 enum ofputil_protocol *usable_protocols OVS_UNUSED)
873 return parse_OUTPUT(arg, ofpacts, usable_protocols);
877 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
879 ds_put_cstr(s, "output:");
880 mf_format_subfield(&a->src, s);
883 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
885 * The bundle actions choose a slave from a supplied list of options.
886 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
887 * selection to a register.
889 * The list of possible slaves follows the nx_action_bundle structure. The size
890 * of each slave is governed by its type as indicated by the 'slave_type'
891 * parameter. The list of slaves should be padded at its end with zeros to make
892 * the total length of the action a multiple of 8.
894 * Switches infer from the 'slave_type' parameter the size of each slave. All
895 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
896 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
897 * 2 byte width. Switches should reject actions which indicate unknown or
898 * unsupported slave types.
900 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
901 * slave. If the switch does not support the specified 'algorithm' parameter,
902 * it should reject the action.
904 * Several algorithms take into account liveness when selecting slaves. The
905 * liveness of a slave is implementation defined (with one exception), but will
906 * generally take into account things like its carrier status and the results
907 * of any link monitoring protocols which happen to be running on it. In order
908 * to give controllers a place-holder value, the OFPP_NONE port is always
911 * Some slave selection strategies require the use of a hash function, in which
912 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
913 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
914 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
915 * 'basis' parameter is used as a universal hash parameter. Different values
916 * of 'basis' yield different hash results.
918 * The 'zero' parameter at the end of the action structure is reserved for
919 * future use. Switches are required to reject actions which have nonzero
920 * bytes in the 'zero' field.
922 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
923 * should reject actions which have nonzero bytes in either of these fields.
925 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
926 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
927 * similar to those for the NXAST_REG_LOAD action. */
928 struct nx_action_bundle {
929 ovs_be16 type; /* OFPAT_VENDOR. */
930 ovs_be16 len; /* Length including slaves. */
931 ovs_be32 vendor; /* NX_VENDOR_ID. */
932 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
934 /* Slave choice algorithm to apply to hash value. */
935 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
937 /* What fields to hash and how. */
938 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
939 ovs_be16 basis; /* Universal hash parameter. */
941 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
942 ovs_be16 n_slaves; /* Number of slaves. */
944 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
945 ovs_be32 dst; /* Destination. */
947 uint8_t zero[4]; /* Reserved. Must be zero. */
949 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
952 decode_bundle(bool load, const struct nx_action_bundle *nab,
953 struct ofpbuf *ofpacts)
955 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
956 struct ofpact_bundle *bundle;
958 size_t slaves_size, i;
961 bundle = ofpact_put_BUNDLE(ofpacts);
963 bundle->n_slaves = ntohs(nab->n_slaves);
964 bundle->basis = ntohs(nab->basis);
965 bundle->fields = ntohs(nab->fields);
966 bundle->algorithm = ntohs(nab->algorithm);
967 slave_type = ntohl(nab->slave_type);
968 slaves_size = ntohs(nab->len) - sizeof *nab;
970 error = OFPERR_OFPBAC_BAD_ARGUMENT;
971 if (!flow_hash_fields_valid(bundle->fields)) {
972 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
973 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
974 VLOG_WARN_RL(&rl, "too many slaves");
975 } else if (bundle->algorithm != NX_BD_ALG_HRW
976 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
977 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
978 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
979 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
984 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
985 VLOG_WARN_RL(&rl, "reserved field is nonzero");
986 error = OFPERR_OFPBAC_BAD_ARGUMENT;
990 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
991 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
992 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
994 if (bundle->dst.n_bits < 16) {
995 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
997 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1000 if (nab->ofs_nbits || nab->dst) {
1001 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
1002 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1006 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1007 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1008 "allocated for slaves. %"PRIuSIZE" bytes are required "
1009 "for %"PRIu16" slaves.",
1010 load ? "bundle_load" : "bundle", slaves_size,
1011 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1012 error = OFPERR_OFPBAC_BAD_LEN;
1015 for (i = 0; i < bundle->n_slaves; i++) {
1016 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1017 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1020 bundle = ofpacts->header;
1021 ofpact_update_len(ofpacts, &bundle->ofpact);
1024 error = bundle_check(bundle, OFPP_MAX, NULL);
1030 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1031 enum ofp_version ofp_version OVS_UNUSED,
1034 return decode_bundle(false, nab, out);
1038 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1039 enum ofp_version ofp_version OVS_UNUSED,
1042 return decode_bundle(true, nab, out);
1046 encode_BUNDLE(const struct ofpact_bundle *bundle,
1047 enum ofp_version ofp_version OVS_UNUSED,
1050 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1051 struct nx_action_bundle *nab;
1055 nab = (bundle->dst.field
1056 ? put_NXAST_BUNDLE_LOAD(out)
1057 : put_NXAST_BUNDLE(out));
1058 nab->len = htons(ntohs(nab->len) + slaves_len);
1059 nab->algorithm = htons(bundle->algorithm);
1060 nab->fields = htons(bundle->fields);
1061 nab->basis = htons(bundle->basis);
1062 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1063 nab->n_slaves = htons(bundle->n_slaves);
1064 if (bundle->dst.field) {
1065 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1066 bundle->dst.n_bits);
1067 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1070 slaves = ofpbuf_put_zeros(out, slaves_len);
1071 for (i = 0; i < bundle->n_slaves; i++) {
1072 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1076 static char * OVS_WARN_UNUSED_RESULT
1077 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1078 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1080 return bundle_parse(arg, ofpacts);
1083 static char * OVS_WARN_UNUSED_RESULT
1084 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1086 return bundle_parse_load(arg, ofpacts);
1090 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1092 bundle_format(a, s);
1095 /* Set VLAN actions. */
1098 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1101 return OFPERR_OFPBAC_BAD_ARGUMENT;
1103 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1104 vlan_vid->vlan_vid = vid;
1105 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1111 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1112 enum ofp_version ofp_version OVS_UNUSED,
1115 return decode_set_vlan_vid(vid, true, out);
1119 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1120 enum ofp_version ofp_version OVS_UNUSED,
1123 return decode_set_vlan_vid(vid, false, out);
1127 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1128 enum ofp_version ofp_version, struct ofpbuf *out)
1130 uint16_t vid = vlan_vid->vlan_vid;
1132 /* Push a VLAN tag, if none is present and this form of the action calls
1133 * for such a feature. */
1134 if (ofp_version > OFP10_VERSION
1135 && vlan_vid->push_vlan_if_needed
1136 && !vlan_vid->flow_has_vlan) {
1137 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1140 if (ofp_version == OFP10_VERSION) {
1141 put_OFPAT10_SET_VLAN_VID(out, vid);
1142 } else if (ofp_version == OFP11_VERSION) {
1143 put_OFPAT11_SET_VLAN_VID(out, vid);
1145 ofpact_put_set_field(out, ofp_version,
1146 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1150 static char * OVS_WARN_UNUSED_RESULT
1151 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1153 struct ofpact_vlan_vid *vlan_vid;
1157 error = str_to_u16(arg, "VLAN VID", &vid);
1162 if (vid & ~VLAN_VID_MASK) {
1163 return xasprintf("%s: not a valid VLAN VID", arg);
1165 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1166 vlan_vid->vlan_vid = vid;
1167 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1171 static char * OVS_WARN_UNUSED_RESULT
1172 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1173 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1175 return parse_set_vlan_vid(arg, ofpacts, false);
1179 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1181 ds_put_format(s, "%s:%"PRIu16,
1182 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1186 /* Set PCP actions. */
1189 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1192 return OFPERR_OFPBAC_BAD_ARGUMENT;
1194 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1195 vlan_pcp->vlan_pcp = pcp;
1196 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1202 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1203 enum ofp_version ofp_version OVS_UNUSED,
1206 return decode_set_vlan_pcp(pcp, true, out);
1210 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1211 enum ofp_version ofp_version OVS_UNUSED,
1214 return decode_set_vlan_pcp(pcp, false, out);
1218 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1219 enum ofp_version ofp_version, struct ofpbuf *out)
1221 uint8_t pcp = vlan_pcp->vlan_pcp;
1223 /* Push a VLAN tag, if none is present and this form of the action calls
1224 * for such a feature. */
1225 if (ofp_version > OFP10_VERSION
1226 && vlan_pcp->push_vlan_if_needed
1227 && !vlan_pcp->flow_has_vlan) {
1228 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1231 if (ofp_version == OFP10_VERSION) {
1232 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1233 } else if (ofp_version == OFP11_VERSION) {
1234 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1236 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1240 static char * OVS_WARN_UNUSED_RESULT
1241 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1243 struct ofpact_vlan_pcp *vlan_pcp;
1247 error = str_to_u8(arg, "VLAN PCP", &pcp);
1253 return xasprintf("%s: not a valid VLAN PCP", arg);
1255 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1256 vlan_pcp->vlan_pcp = pcp;
1257 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1261 static char * OVS_WARN_UNUSED_RESULT
1262 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1263 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1265 return parse_set_vlan_pcp(arg, ofpacts, false);
1269 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1271 ds_put_format(s, "%s:%"PRIu8,
1272 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1276 /* Strip VLAN actions. */
1279 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1281 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1286 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1288 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1293 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1294 enum ofp_version ofp_version, struct ofpbuf *out)
1296 if (ofp_version == OFP10_VERSION) {
1297 put_OFPAT10_STRIP_VLAN(out);
1299 put_OFPAT11_POP_VLAN(out);
1303 static char * OVS_WARN_UNUSED_RESULT
1304 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1305 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1307 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1311 static char * OVS_WARN_UNUSED_RESULT
1312 parse_pop_vlan(struct ofpbuf *ofpacts)
1314 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1319 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1321 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1326 /* Push VLAN action. */
1329 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1330 enum ofp_version ofp_version OVS_UNUSED,
1333 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1334 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1335 return OFPERR_OFPBAC_BAD_ARGUMENT;
1337 ofpact_put_PUSH_VLAN(out);
1342 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1343 enum ofp_version ofp_version, struct ofpbuf *out)
1345 if (ofp_version == OFP10_VERSION) {
1346 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1347 * follow this action. */
1349 /* XXX ETH_TYPE_VLAN_8021AD case */
1350 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1354 static char * OVS_WARN_UNUSED_RESULT
1355 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1356 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1361 *usable_protocols &= OFPUTIL_P_OF11_UP;
1362 error = str_to_u16(arg, "ethertype", ðertype);
1367 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1368 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1369 return xasprintf("%s: not a valid VLAN ethertype", arg);
1372 ofpact_put_PUSH_VLAN(ofpacts);
1377 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1379 /* XXX 802.1AD case*/
1380 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1383 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1384 struct ofp_action_dl_addr {
1385 ovs_be16 type; /* Type. */
1386 ovs_be16 len; /* Length is 16. */
1387 struct eth_addr dl_addr; /* Ethernet address. */
1390 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1393 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1394 enum ofp_version ofp_version OVS_UNUSED,
1397 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1402 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1403 enum ofp_version ofp_version OVS_UNUSED,
1406 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1411 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1412 enum ofp_raw_action_type raw, enum mf_field_id field,
1415 if (ofp_version < OFP12_VERSION) {
1416 struct ofp_action_dl_addr *oada;
1418 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1419 oada->dl_addr = mac->mac;
1421 ofpact_put_set_field(out, ofp_version, field,
1422 eth_addr_to_uint64(mac->mac));
1427 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1430 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1436 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1437 enum ofp_version ofp_version,
1440 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1445 static char * OVS_WARN_UNUSED_RESULT
1446 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1447 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1449 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1452 static char * OVS_WARN_UNUSED_RESULT
1453 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1454 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1456 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1460 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1462 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1466 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1468 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1471 /* Set IPv4 address actions. */
1474 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1475 enum ofp_version ofp_version OVS_UNUSED,
1478 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1483 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1484 enum ofp_version ofp_version OVS_UNUSED,
1487 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1492 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1493 enum ofp_version ofp_version,
1494 enum ofp_raw_action_type raw, enum mf_field_id field,
1497 ovs_be32 addr = ipv4->ipv4;
1498 if (ofp_version < OFP12_VERSION) {
1499 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1501 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1506 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1507 enum ofp_version ofp_version, struct ofpbuf *out)
1509 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1514 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1515 enum ofp_version ofp_version, struct ofpbuf *out)
1517 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1521 static char * OVS_WARN_UNUSED_RESULT
1522 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1523 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1525 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1528 static char * OVS_WARN_UNUSED_RESULT
1529 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1530 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1532 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1536 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1538 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1542 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1544 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1547 /* Set IPv4/v6 TOS actions. */
1550 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1551 enum ofp_version ofp_version OVS_UNUSED,
1554 if (dscp & ~IP_DSCP_MASK) {
1555 return OFPERR_OFPBAC_BAD_ARGUMENT;
1557 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1563 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1564 enum ofp_version ofp_version, struct ofpbuf *out)
1566 if (ofp_version < OFP12_VERSION) {
1567 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1569 ofpact_put_set_field(out, ofp_version,
1570 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1574 static char * OVS_WARN_UNUSED_RESULT
1575 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1576 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1581 error = str_to_u8(arg, "TOS", &tos);
1586 if (tos & ~IP_DSCP_MASK) {
1587 return xasprintf("%s: not a valid TOS", arg);
1589 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1594 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1596 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1599 /* Set IPv4/v6 ECN actions. */
1602 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1603 enum ofp_version ofp_version OVS_UNUSED,
1606 if (ecn & ~IP_ECN_MASK) {
1607 return OFPERR_OFPBAC_BAD_ARGUMENT;
1609 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1615 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1616 enum ofp_version ofp_version, struct ofpbuf *out)
1618 uint8_t ecn = ip_ecn->ecn;
1619 if (ofp_version == OFP10_VERSION) {
1621 } else if (ofp_version == OFP11_VERSION) {
1622 put_OFPAT11_SET_NW_ECN(out, ecn);
1624 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1628 static char * OVS_WARN_UNUSED_RESULT
1629 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1630 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1635 error = str_to_u8(arg, "ECN", &ecn);
1640 if (ecn & ~IP_ECN_MASK) {
1641 return xasprintf("%s: not a valid ECN", arg);
1643 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1648 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1650 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1653 /* Set IPv4/v6 TTL actions. */
1656 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1657 enum ofp_version ofp_version OVS_UNUSED,
1660 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1665 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1666 enum ofp_version ofp_version, struct ofpbuf *out)
1668 if (ofp_version >= OFP11_VERSION) {
1669 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1675 static char * OVS_WARN_UNUSED_RESULT
1676 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1677 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1682 error = str_to_u8(arg, "TTL", &ttl);
1687 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1692 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1694 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1697 /* Set TCP/UDP/SCTP port actions. */
1700 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1701 enum ofp_version ofp_version OVS_UNUSED,
1704 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1709 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1710 enum ofp_version ofp_version OVS_UNUSED,
1713 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1718 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1719 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1720 enum mf_field_id field, struct ofpbuf *out)
1722 uint16_t port = l4_port->port;
1724 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1725 ofpact_put_set_field(out, ofp_version, field, port);
1727 ofpact_put_raw(out, ofp_version, raw, port);
1732 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1733 enum ofp_version ofp_version, struct ofpbuf *out)
1735 uint8_t proto = l4_port->flow_ip_proto;
1736 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1737 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1738 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1741 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1745 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1746 enum ofp_version ofp_version,
1749 uint8_t proto = l4_port->flow_ip_proto;
1750 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1751 : proto == IPPROTO_UDP ? MFF_UDP_DST
1752 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1755 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1758 static char * OVS_WARN_UNUSED_RESULT
1759 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1760 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1762 return str_to_u16(arg, "source port",
1763 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1766 static char * OVS_WARN_UNUSED_RESULT
1767 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1768 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1770 return str_to_u16(arg, "destination port",
1771 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1775 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1777 ds_put_format(s, "mod_tp_src:%d", a->port);
1781 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1783 ds_put_format(s, "mod_tp_dst:%d", a->port);
1786 /* Action structure for OFPAT_COPY_FIELD. */
1787 struct ofp15_action_copy_field {
1788 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1789 ovs_be16 len; /* Length is padded to 64 bits. */
1790 ovs_be16 n_bits; /* Number of bits to copy. */
1791 ovs_be16 src_offset; /* Starting bit offset in source. */
1792 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1795 * - OXM header for source field.
1796 * - OXM header for destination field.
1797 * - Padding with 0-bytes to a multiple of 8 bytes.
1798 * The "pad2" member is the beginning of the above. */
1801 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1803 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1804 struct onf_action_copy_field {
1805 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
1806 ovs_be16 len; /* Length is padded to 64 bits. */
1807 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
1808 ovs_be16 exp_type; /* 3200. */
1809 uint8_t pad[2]; /* Not used. */
1810 ovs_be16 n_bits; /* Number of bits to copy. */
1811 ovs_be16 src_offset; /* Starting bit offset in source. */
1812 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1813 uint8_t pad2[2]; /* Not used. */
1815 * - OXM header for source field.
1816 * - OXM header for destination field.
1817 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1818 * The "pad3" member is the beginning of the above. */
1819 uint8_t pad3[4]; /* Not used. */
1821 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1823 /* Action structure for NXAST_REG_MOVE.
1825 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1826 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1827 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1828 * the next most significant bit, and so on.
1830 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1831 * sense to use nxm_hasmask=1 because the action does not do any kind of
1832 * matching; it uses the actual value of a field.)
1834 * The following nxm_header values are potentially acceptable as 'src':
1849 * - NXM_OF_ICMP_TYPE
1850 * - NXM_OF_ICMP_CODE
1857 * - NXM_NX_ICMPV6_TYPE
1858 * - NXM_NX_ICMPV6_CODE
1861 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1863 * - NXM_NX_TUN_IPV4_SRC
1864 * - NXM_NX_TUN_IPV4_DST
1866 * The following nxm_header values are potentially acceptable as 'dst':
1882 * Modifying any of the above fields changes the corresponding packet
1887 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1891 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1892 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1893 * header (if any), ignoring the other bits. Setting a value with CFI=1
1894 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1895 * to the field's new value (with the CFI bit masked out).
1897 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1898 * any of these values modifies the corresponding tunnel header field used
1899 * for the packet's next tunnel encapsulation, if allowed by the
1900 * configuration of the output tunnel port.
1902 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1903 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1904 * used only if the flow's nx_match includes an nxm_entry that specifies
1905 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1907 * The switch will reject actions for which src_ofs+n_bits is greater than the
1908 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1909 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1911 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1912 * behaves as if 'src' were copied out to a temporary buffer, then the
1913 * temporary buffer copied to 'dst'.
1915 struct nx_action_reg_move {
1916 ovs_be16 type; /* OFPAT_VENDOR. */
1917 ovs_be16 len; /* Length is 24. */
1918 ovs_be32 vendor; /* NX_VENDOR_ID. */
1919 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1920 ovs_be16 n_bits; /* Number of bits. */
1921 ovs_be16 src_ofs; /* Starting bit offset in source. */
1922 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1924 * - OXM/NXM header for source field (4 or 8 bytes).
1925 * - OXM/NXM header for destination field (4 or 8 bytes).
1926 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
1928 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
1931 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
1932 const void *action, ovs_be16 action_len, size_t oxm_offset,
1933 struct ofpbuf *ofpacts)
1935 struct ofpact_reg_move *move;
1939 move = ofpact_put_REG_MOVE(ofpacts);
1940 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
1941 move->src.ofs = ntohs(src_offset);
1942 move->src.n_bits = ntohs(n_bits);
1943 move->dst.ofs = ntohs(dst_offset);
1944 move->dst.n_bits = ntohs(n_bits);
1946 ofpbuf_use_const(&b, action, ntohs(action_len));
1947 ofpbuf_pull(&b, oxm_offset);
1948 error = nx_pull_header(&b, &move->src.field, NULL);
1952 error = nx_pull_header(&b, &move->dst.field, NULL);
1957 if (!is_all_zeros(b.data, b.size)) {
1958 return OFPERR_NXBRC_MUST_BE_ZERO;
1961 return nxm_reg_move_check(move, NULL);
1965 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
1966 enum ofp_version ofp_version OVS_UNUSED,
1967 struct ofpbuf *ofpacts)
1969 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1970 oacf->n_bits, oacf, oacf->len,
1971 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
1975 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
1976 enum ofp_version ofp_version OVS_UNUSED,
1977 struct ofpbuf *ofpacts)
1979 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1980 oacf->n_bits, oacf, oacf->len,
1981 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
1985 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
1986 enum ofp_version ofp_version OVS_UNUSED,
1987 struct ofpbuf *ofpacts)
1989 struct ofpact_reg_move *move;
1993 move = ofpact_put_REG_MOVE(ofpacts);
1994 move->ofpact.raw = NXAST_RAW_REG_MOVE;
1995 move->src.ofs = ntohs(narm->src_ofs);
1996 move->src.n_bits = ntohs(narm->n_bits);
1997 move->dst.ofs = ntohs(narm->dst_ofs);
1998 move->dst.n_bits = ntohs(narm->n_bits);
2000 ofpbuf_use_const(&b, narm, ntohs(narm->len));
2001 ofpbuf_pull(&b, sizeof *narm);
2002 error = nx_pull_header(&b, &move->src.field, NULL);
2006 error = nx_pull_header(&b, &move->dst.field, NULL);
2010 if (!is_all_zeros(b.data, b.size)) {
2011 return OFPERR_NXBRC_MUST_BE_ZERO;
2014 return nxm_reg_move_check(move, NULL);
2018 encode_REG_MOVE(const struct ofpact_reg_move *move,
2019 enum ofp_version ofp_version, struct ofpbuf *out)
2021 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2022 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2023 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2024 * probe for support. Until we have that ability, we currently prefer
2025 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2027 size_t start_ofs = out->size;
2028 if (ofp_version >= OFP15_VERSION) {
2029 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2030 copy->n_bits = htons(move->dst.n_bits);
2031 copy->src_offset = htons(move->src.ofs);
2032 copy->dst_offset = htons(move->dst.ofs);
2033 out->size = out->size - sizeof copy->pad2;
2034 nx_put_header(out, move->src.field->id, ofp_version, false);
2035 nx_put_header(out, move->dst.field->id, ofp_version, false);
2036 } else if (ofp_version == OFP13_VERSION
2037 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2038 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2039 copy->n_bits = htons(move->dst.n_bits);
2040 copy->src_offset = htons(move->src.ofs);
2041 copy->dst_offset = htons(move->dst.ofs);
2042 out->size = out->size - sizeof copy->pad3;
2043 nx_put_header(out, move->src.field->id, ofp_version, false);
2044 nx_put_header(out, move->dst.field->id, ofp_version, false);
2046 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2047 narm->n_bits = htons(move->dst.n_bits);
2048 narm->src_ofs = htons(move->src.ofs);
2049 narm->dst_ofs = htons(move->dst.ofs);
2050 nx_put_header(out, move->src.field->id, 0, false);
2051 nx_put_header(out, move->dst.field->id, 0, false);
2053 pad_ofpat(out, start_ofs);
2056 static char * OVS_WARN_UNUSED_RESULT
2057 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2058 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2060 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2061 const char *full_arg = arg;
2064 error = mf_parse_subfield__(&move->src, &arg);
2068 if (strncmp(arg, "->", 2)) {
2069 return xasprintf("%s: missing `->' following source", full_arg);
2072 error = mf_parse_subfield(&move->dst, arg);
2077 if (move->src.n_bits != move->dst.n_bits) {
2078 return xasprintf("%s: source field is %d bits wide but destination is "
2079 "%d bits wide", full_arg,
2080 move->src.n_bits, move->dst.n_bits);
2086 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2088 nxm_format_reg_move(a, s);
2091 /* Action structure for OFPAT12_SET_FIELD. */
2092 struct ofp12_action_set_field {
2093 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2094 ovs_be16 len; /* Length is padded to 64 bits. */
2097 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2098 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2100 * The "pad" member is the beginning of the above. */
2103 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2105 /* Action structure for NXAST_REG_LOAD.
2107 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2108 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2109 * starts at 0 for the least-significant bit, 1 for the next most significant
2112 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2113 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2116 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2117 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2118 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2119 * also stored as one less than its true value:
2122 * +------------------------------+------------------+
2123 * | ofs | n_bits - 1 |
2124 * +------------------------------+------------------+
2126 * The switch will reject actions for which ofs+n_bits is greater than the
2127 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2128 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2129 * OFPBAC_BAD_ARGUMENT.
2131 struct nx_action_reg_load {
2132 ovs_be16 type; /* OFPAT_VENDOR. */
2133 ovs_be16 len; /* Length is 24. */
2134 ovs_be32 vendor; /* NX_VENDOR_ID. */
2135 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2136 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2137 ovs_be32 dst; /* Destination register. */
2138 ovs_be64 value; /* Immediate value. */
2140 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2142 /* Action structure for NXAST_REG_LOAD2.
2144 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2145 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2146 * OXM experimenter fields. */
2147 struct nx_action_reg_load2 {
2148 ovs_be16 type; /* OFPAT_VENDOR. */
2149 ovs_be16 len; /* At least 16. */
2150 ovs_be32 vendor; /* NX_VENDOR_ID. */
2151 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2154 * - An NXM/OXM header, value, and optionally a mask.
2155 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2157 * The "pad" member is the beginning of the above. */
2160 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2163 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2164 bool may_mask, struct ofpbuf *ofpacts)
2166 struct ofpact_set_field *sf;
2170 sf = ofpact_put_SET_FIELD(ofpacts);
2172 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2173 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2174 error = nx_pull_entry(&b, &sf->field, &sf->value,
2175 may_mask ? &sf->mask : NULL);
2177 return (error == OFPERR_OFPBMC_BAD_MASK
2178 ? OFPERR_OFPBAC_BAD_SET_MASK
2182 memset(&sf->mask, 0xff, sf->field->n_bytes);
2185 if (!is_all_zeros(b.data, b.size)) {
2186 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2189 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2191 if (sf->field->id == MFF_IN_PORT_OXM) {
2192 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2195 /* oxm_length is now validated to be compatible with mf_value. */
2196 if (!sf->field->writable) {
2197 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2199 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2202 /* The value must be valid for match. OpenFlow 1.5 also says,
2203 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2204 * a 1-bit in oxm_value and in oxm_mask." */
2205 if (!mf_is_value_valid(sf->field, &sf->value)
2206 || (sf->field->id == MFF_VLAN_VID
2207 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2208 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2209 struct ds ds = DS_EMPTY_INITIALIZER;
2210 mf_format(sf->field, &sf->value, NULL, &ds);
2211 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2212 sf->field->name, ds_cstr(&ds));
2215 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2221 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2222 enum ofp_version ofp_version OVS_UNUSED,
2223 struct ofpbuf *ofpacts)
2225 return decode_ofpat_set_field(oasf, false, ofpacts);
2229 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2230 enum ofp_version ofp_version OVS_UNUSED,
2231 struct ofpbuf *ofpacts)
2233 return decode_ofpat_set_field(oasf, true, ofpacts);
2237 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2238 enum ofp_version ofp_version OVS_UNUSED,
2241 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2242 struct mf_subfield dst;
2245 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2247 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2248 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2249 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2250 error = mf_check_dst(&dst, NULL);
2255 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2257 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2258 return OFPERR_OFPBAC_BAD_ARGUMENT;
2261 sf->field = dst.field;
2262 bitwise_put(ntohll(narl->value),
2263 &sf->value, dst.field->n_bytes, dst.ofs,
2265 bitwise_put(UINT64_MAX,
2266 &sf->mask, dst.field->n_bytes, dst.ofs,
2273 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2274 enum ofp_version ofp_version OVS_UNUSED,
2277 struct ofpact_set_field *sf;
2281 sf = ofpact_put_SET_FIELD(out);
2282 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2284 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2285 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2286 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2290 if (!is_all_zeros(b.data, b.size)) {
2291 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2294 if (!sf->field->writable) {
2295 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2297 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2303 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2304 enum mf_field_id field, uint64_t value_)
2306 struct ofp12_action_set_field *oasf OVS_UNUSED;
2307 int n_bytes = mf_from_id(field)->n_bytes;
2308 size_t start_ofs = openflow->size;
2309 union mf_value value;
2311 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2313 oasf = put_OFPAT12_SET_FIELD(openflow);
2314 openflow->size = openflow->size - sizeof oasf->pad;
2315 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2316 pad_ofpat(openflow, start_ofs);
2320 next_load_segment(const struct ofpact_set_field *sf,
2321 struct mf_subfield *dst, uint64_t *value)
2323 int n_bits = sf->field->n_bits;
2324 int n_bytes = sf->field->n_bytes;
2325 int start = dst->ofs + dst->n_bits;
2327 if (start < n_bits) {
2328 dst->field = sf->field;
2329 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2330 if (dst->ofs < n_bits) {
2331 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2332 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2333 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2340 /* Convert 'sf' to a series of REG_LOADs. */
2342 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2344 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2345 * experimenter OXM or is variable length (or if it came in as
2346 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2347 * NXAST_REG_LOAD, which is backward compatible. */
2348 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2349 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2350 struct nx_action_reg_load2 *narl OVS_UNUSED;
2351 size_t start_ofs = openflow->size;
2353 narl = put_NXAST_REG_LOAD2(openflow);
2354 openflow->size = openflow->size - sizeof narl->pad;
2355 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2356 pad_ofpat(openflow, start_ofs);
2358 struct mf_subfield dst;
2361 dst.ofs = dst.n_bits = 0;
2362 while (next_load_segment(sf, &dst, &value)) {
2363 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2364 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2365 narl->dst = htonl(mf_nxm_header(dst.field->id));
2366 narl->value = htonll(value);
2371 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2372 * actions, if we can, falling back to Nicira extensions if we must.
2374 * We check only meta-flow types that can appear within set field actions and
2375 * that have a mapping to compatible action types. These struct mf_field
2376 * definitions have a defined OXM or NXM header value and specify the field as
2379 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2380 enum ofp_version ofp_version,
2383 switch ((int) sf->field->id) {
2384 case MFF_VLAN_TCI: {
2385 ovs_be16 tci = sf->value.be16;
2386 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2387 uint16_t vid = vlan_tci_to_vid(tci);
2388 uint8_t pcp = vlan_tci_to_pcp(tci);
2390 if (ofp_version < OFP11_VERSION) {
2391 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2393 * If CFI=1, Add or modify VLAN VID & PCP.
2394 * If CFI=0, strip VLAN header, if any.
2397 put_OFPAT10_SET_VLAN_VID(out, vid);
2398 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2400 put_OFPAT10_STRIP_VLAN(out);
2403 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2405 * If CFI=1, Add or modify VLAN VID & PCP.
2406 * OpenFlow 1.1 set actions only apply if the packet
2407 * already has VLAN tags. To be sure that is the case
2408 * we have to push a VLAN header. As we do not support
2409 * multiple layers of VLANs, this is a no-op, if a VLAN
2410 * header already exists. This may backfire, however,
2411 * when we start supporting multiple layers of VLANs.
2412 * If CFI=0, strip VLAN header, if any.
2415 /* Push a VLAN tag, if one was not seen at action validation
2417 if (!sf->flow_has_vlan) {
2418 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2420 put_OFPAT11_SET_VLAN_VID(out, vid);
2421 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2423 /* If the flow did not match on vlan, we have no way of
2424 * knowing if the vlan tag exists, so we must POP just to be
2426 put_OFPAT11_POP_VLAN(out);
2432 case MFF_VLAN_VID: {
2433 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2434 if (ofp_version == OFP10_VERSION) {
2435 put_OFPAT10_SET_VLAN_VID(out, vid);
2437 put_OFPAT11_SET_VLAN_VID(out, vid);
2443 if (ofp_version == OFP10_VERSION) {
2444 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2446 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2451 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2455 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2459 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2463 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2467 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2470 case MFF_IP_DSCP_SHIFTED:
2471 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2476 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2481 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2485 set_field_to_nxast(sf, out);
2491 set_field_to_set_field(const struct ofpact_set_field *sf,
2492 enum ofp_version ofp_version, struct ofpbuf *out)
2494 struct ofp12_action_set_field *oasf OVS_UNUSED;
2495 size_t start_ofs = out->size;
2497 oasf = put_OFPAT12_SET_FIELD(out);
2498 out->size = out->size - sizeof oasf->pad;
2499 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2500 pad_ofpat(out, start_ofs);
2504 encode_SET_FIELD(const struct ofpact_set_field *sf,
2505 enum ofp_version ofp_version, struct ofpbuf *out)
2507 if (ofp_version >= OFP15_VERSION) {
2508 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2510 set_field_to_set_field(sf, ofp_version, out);
2511 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2512 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2513 /* It came in as reg_load, send it out the same way. */
2514 set_field_to_nxast(sf, out);
2515 } else if (ofp_version < OFP12_VERSION) {
2516 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2517 set_field_to_legacy_openflow(sf, ofp_version, out);
2518 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2519 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2520 * entire field, so encode it as OFPAT_SET_FIELD. */
2521 set_field_to_set_field(sf, ofp_version, out);
2523 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2524 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2525 * so encode it as reg_load. */
2526 set_field_to_nxast(sf, out);
2530 /* Parses the input argument 'arg' into the key, value, and delimiter
2531 * components that are common across the reg_load and set_field action format.
2533 * With an argument like "1->metadata", sets the following pointers to
2534 * point within 'arg':
2539 * Returns NULL if successful, otherwise a malloc()'d string describing the
2540 * error. The caller is responsible for freeing the returned string. */
2541 static char * OVS_WARN_UNUSED_RESULT
2542 set_field_split_str(char *arg, char **key, char **value, char **delim)
2547 value_end = strstr(arg, "->");
2548 *key = value_end + strlen("->");
2554 return xasprintf("%s: missing `->'", arg);
2556 if (strlen(value_end) <= strlen("->")) {
2557 return xasprintf("%s: missing field name following `->'", arg);
2563 /* Parses a "set_field" action with argument 'arg', appending the parsed
2564 * action to 'ofpacts'.
2566 * Returns NULL if successful, otherwise a malloc()'d string describing the
2567 * error. The caller is responsible for freeing the returned string. */
2568 static char * OVS_WARN_UNUSED_RESULT
2569 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2570 enum ofputil_protocol *usable_protocols)
2572 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2576 const struct mf_field *mf;
2579 error = set_field_split_str(arg, &key, &value, &delim);
2584 mf = mf_from_name(key);
2586 return xasprintf("%s is not a valid OXM field name", key);
2588 if (!mf->writable) {
2589 return xasprintf("%s is read-only", key);
2593 error = mf_parse(mf, value, &sf->value, &sf->mask);
2598 if (!mf_is_value_valid(mf, &sf->value)) {
2599 return xasprintf("%s is not a valid value for field %s", value, key);
2602 *usable_protocols &= mf->usable_protocols_exact;
2606 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2607 * action to 'ofpacts'.
2609 * Returns NULL if successful, otherwise a malloc()'d string describing the
2610 * error. The caller is responsible for freeing the returned string. */
2611 static char * OVS_WARN_UNUSED_RESULT
2612 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2613 enum ofputil_protocol *usable_protocols)
2615 char *copy = xstrdup(arg);
2616 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2621 static char * OVS_WARN_UNUSED_RESULT
2622 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2624 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2625 struct mf_subfield dst;
2626 char *key, *value_str;
2627 union mf_value value;
2630 error = set_field_split_str(arg, &key, &value_str, NULL);
2635 error = mf_parse_subfield(&dst, key);
2640 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2642 return xasprintf("%s: cannot parse integer value", arg);
2645 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2646 dst.field->n_bytes * 8 - dst.n_bits)) {
2650 mf_format(dst.field, &value, NULL, &ds);
2651 error = xasprintf("%s: value %s does not fit into %d bits",
2652 arg, ds_cstr(&ds), dst.n_bits);
2657 sf->field = dst.field;
2658 memset(&sf->value, 0, sizeof sf->value);
2659 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2660 dst.field->n_bytes, dst.ofs, dst.n_bits);
2661 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2667 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2669 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2670 struct mf_subfield dst;
2673 dst.ofs = dst.n_bits = 0;
2674 while (next_load_segment(a, &dst, &value)) {
2675 ds_put_format(s, "load:%#"PRIx64"->", value);
2676 mf_format_subfield(&dst, s);
2677 ds_put_char(s, ',');
2681 ds_put_cstr(s, "set_field:");
2682 mf_format(a->field, &a->value, &a->mask, s);
2683 ds_put_format(s, "->%s", a->field->name);
2687 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2688 * is marked such that, if possible, it will be translated to OpenFlow as
2689 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2690 * that was the way that the action was expressed when it came into OVS or for
2691 * backward compatibility. */
2692 struct ofpact_set_field *
2693 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2695 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2696 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2700 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2702 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2705 struct nx_action_stack {
2706 ovs_be16 type; /* OFPAT_VENDOR. */
2707 ovs_be16 len; /* Length is 16. */
2708 ovs_be32 vendor; /* NX_VENDOR_ID. */
2709 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2710 ovs_be16 offset; /* Bit offset into the field. */
2712 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2713 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2714 * - Enough 0-bytes to pad out the action to 24 bytes. */
2715 uint8_t pad[12]; /* See above. */
2717 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2720 decode_stack_action(const struct nx_action_stack *nasp,
2721 struct ofpact_stack *stack_action)
2726 stack_action->subfield.ofs = ntohs(nasp->offset);
2728 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2729 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2730 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2734 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2736 if (!is_all_zeros(b.data, b.size)) {
2737 return OFPERR_NXBRC_MUST_BE_ZERO;
2744 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2745 enum ofp_version ofp_version OVS_UNUSED,
2746 struct ofpbuf *ofpacts)
2748 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2749 enum ofperr error = decode_stack_action(nasp, push);
2750 return error ? error : nxm_stack_push_check(push, NULL);
2754 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2755 enum ofp_version ofp_version OVS_UNUSED,
2756 struct ofpbuf *ofpacts)
2758 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2759 enum ofperr error = decode_stack_action(nasp, pop);
2760 return error ? error : nxm_stack_pop_check(pop, NULL);
2764 encode_STACK_op(const struct ofpact_stack *stack_action,
2765 struct nx_action_stack *nasp)
2770 nasp->offset = htons(stack_action->subfield.ofs);
2772 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2773 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2774 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2775 n_bits = htons(stack_action->subfield.n_bits);
2776 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2780 encode_STACK_PUSH(const struct ofpact_stack *stack,
2781 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2783 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2787 encode_STACK_POP(const struct ofpact_stack *stack,
2788 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2790 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2793 static char * OVS_WARN_UNUSED_RESULT
2794 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2795 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2797 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2800 static char * OVS_WARN_UNUSED_RESULT
2801 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2802 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2804 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2808 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2810 nxm_format_stack_push(a, s);
2814 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2816 nxm_format_stack_pop(a, s);
2819 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2821 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2822 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2823 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2824 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2826 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2827 * sent only to controllers with id 0.)
2829 struct nx_action_cnt_ids {
2830 ovs_be16 type; /* OFPAT_VENDOR. */
2831 ovs_be16 len; /* Length including slaves. */
2832 ovs_be32 vendor; /* NX_VENDOR_ID. */
2833 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2835 ovs_be16 n_controllers; /* Number of controllers. */
2836 uint8_t zeros[4]; /* Must be zero. */
2838 /* Followed by 1 or more controller ids.
2840 * uint16_t cnt_ids[]; // Controller ids.
2841 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2844 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2847 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2850 struct ofpact_cnt_ids *ids;
2851 enum ofperr error = 0;
2853 ids = ofpact_put_DEC_TTL(out);
2854 ids->n_controllers = 1;
2855 ofpbuf_put(out, &id, sizeof id);
2857 ofpact_update_len(out, &ids->ofpact);
2862 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2863 enum ofp_version ofp_version OVS_UNUSED,
2866 struct ofpact_cnt_ids *ids;
2870 ids = ofpact_put_DEC_TTL(out);
2871 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2872 ids->n_controllers = ntohs(nac_ids->n_controllers);
2873 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2875 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2876 return OFPERR_NXBRC_MUST_BE_ZERO;
2879 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2880 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2881 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2882 "are required for %"PRIu16" controllers.",
2883 ids_size, ids->n_controllers * sizeof(ovs_be16),
2884 ids->n_controllers);
2885 return OFPERR_OFPBAC_BAD_LEN;
2888 for (i = 0; i < ids->n_controllers; i++) {
2889 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2890 ofpbuf_put(out, &id, sizeof id);
2894 ofpact_update_len(out, &ids->ofpact);
2900 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2901 enum ofp_version ofp_version, struct ofpbuf *out)
2903 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2904 || dec_ttl->n_controllers != 1
2905 || dec_ttl->cnt_ids[0] != 0) {
2906 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2907 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2911 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2912 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2914 ids = ofpbuf_put_zeros(out, ids_len);
2915 for (i = 0; i < dec_ttl->n_controllers; i++) {
2916 ids[i] = htons(dec_ttl->cnt_ids[i]);
2919 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2924 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2926 struct ofpact_cnt_ids *ids;
2929 ofpact_put_DEC_TTL(ofpacts);
2930 ofpbuf_put(ofpacts, &id, sizeof id);
2931 ids = ofpacts->header;
2932 ids->n_controllers++;
2933 ofpact_update_len(ofpacts, &ids->ofpact);
2936 static char * OVS_WARN_UNUSED_RESULT
2937 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2938 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2941 parse_noargs_dec_ttl(ofpacts);
2943 struct ofpact_cnt_ids *ids;
2946 ids = ofpact_put_DEC_TTL(ofpacts);
2947 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2948 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2949 cntr = strtok_r(NULL, ", ", &arg)) {
2950 uint16_t id = atoi(cntr);
2952 ofpbuf_put(ofpacts, &id, sizeof id);
2953 ids = ofpacts->header;
2954 ids->n_controllers++;
2956 if (!ids->n_controllers) {
2957 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2960 ofpact_update_len(ofpacts, &ids->ofpact);
2966 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2970 ds_put_cstr(s, "dec_ttl");
2971 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2972 ds_put_cstr(s, "(");
2973 for (i = 0; i < a->n_controllers; i++) {
2975 ds_put_cstr(s, ",");
2977 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2979 ds_put_cstr(s, ")");
2983 /* Set MPLS label actions. */
2986 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
2987 enum ofp_version ofp_version OVS_UNUSED,
2990 ofpact_put_SET_MPLS_LABEL(out)->label = label;
2995 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
2996 enum ofp_version ofp_version,
2999 if (ofp_version < OFP12_VERSION) {
3000 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3002 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
3003 ntohl(label->label));
3007 static char * OVS_WARN_UNUSED_RESULT
3008 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3009 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3011 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3013 return xstrdup("set_mpls_label: expected label.");
3016 mpls_label->label = htonl(atoi(arg));
3021 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3023 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
3026 /* Set MPLS TC actions. */
3029 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3030 enum ofp_version ofp_version OVS_UNUSED,
3033 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3038 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3039 enum ofp_version ofp_version, struct ofpbuf *out)
3041 if (ofp_version < OFP12_VERSION) {
3042 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3044 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3048 static char * OVS_WARN_UNUSED_RESULT
3049 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3050 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3052 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3055 return xstrdup("set_mpls_tc: expected tc.");
3058 mpls_tc->tc = atoi(arg);
3063 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3065 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
3068 /* Set MPLS TTL actions. */
3071 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3072 enum ofp_version ofp_version OVS_UNUSED,
3075 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3080 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3081 enum ofp_version ofp_version, struct ofpbuf *out)
3083 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3086 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3087 * action to 'ofpacts'.
3089 * Returns NULL if successful, otherwise a malloc()'d string describing the
3090 * error. The caller is responsible for freeing the returned string. */
3091 static char * OVS_WARN_UNUSED_RESULT
3092 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3093 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3095 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3098 return xstrdup("set_mpls_ttl: expected ttl.");
3101 mpls_ttl->ttl = atoi(arg);
3106 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3108 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3111 /* Decrement MPLS TTL actions. */
3114 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3116 ofpact_put_DEC_MPLS_TTL(out);
3121 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3122 enum ofp_version ofp_version, struct ofpbuf *out)
3124 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3127 static char * OVS_WARN_UNUSED_RESULT
3128 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3129 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3131 ofpact_put_DEC_MPLS_TTL(ofpacts);
3136 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3138 ds_put_cstr(s, "dec_mpls_ttl");
3141 /* Push MPLS label action. */
3144 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3145 enum ofp_version ofp_version OVS_UNUSED,
3148 struct ofpact_push_mpls *oam;
3150 if (!eth_type_mpls(ethertype)) {
3151 return OFPERR_OFPBAC_BAD_ARGUMENT;
3153 oam = ofpact_put_PUSH_MPLS(out);
3154 oam->ethertype = ethertype;
3160 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3161 enum ofp_version ofp_version, struct ofpbuf *out)
3163 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3166 static char * OVS_WARN_UNUSED_RESULT
3167 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3168 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3173 error = str_to_u16(arg, "push_mpls", ðertype);
3175 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3181 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3183 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3186 /* Pop MPLS label action. */
3189 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3190 enum ofp_version ofp_version OVS_UNUSED,
3193 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3198 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3199 enum ofp_version ofp_version, struct ofpbuf *out)
3201 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3204 static char * OVS_WARN_UNUSED_RESULT
3205 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3206 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3211 error = str_to_u16(arg, "pop_mpls", ðertype);
3213 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3219 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3221 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3224 /* Set tunnel ID actions. */
3227 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3228 enum ofp_version ofp_version OVS_UNUSED,
3231 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3232 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3233 tunnel->tun_id = tun_id;
3238 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3239 enum ofp_version ofp_version OVS_UNUSED,
3242 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3243 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3244 tunnel->tun_id = tun_id;
3249 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3250 enum ofp_version ofp_version, struct ofpbuf *out)
3252 uint64_t tun_id = tunnel->tun_id;
3254 if (ofp_version < OFP12_VERSION) {
3255 if (tun_id <= UINT32_MAX
3256 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3257 put_NXAST_SET_TUNNEL(out, tun_id);
3259 put_NXAST_SET_TUNNEL64(out, tun_id);
3262 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3266 static char * OVS_WARN_UNUSED_RESULT
3267 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3268 enum ofp_raw_action_type raw)
3270 struct ofpact_tunnel *tunnel;
3272 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3273 tunnel->ofpact.raw = raw;
3274 return str_to_u64(arg, &tunnel->tun_id);
3277 static char * OVS_WARN_UNUSED_RESULT
3278 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3279 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3281 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3285 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3287 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3288 (a->tun_id > UINT32_MAX
3289 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3293 /* Set queue action. */
3296 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3297 enum ofp_version ofp_version OVS_UNUSED,
3300 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3305 encode_SET_QUEUE(const struct ofpact_queue *queue,
3306 enum ofp_version ofp_version, struct ofpbuf *out)
3308 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3311 static char * OVS_WARN_UNUSED_RESULT
3312 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3313 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3315 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3319 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3321 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3324 /* Pop queue action. */
3327 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3329 ofpact_put_POP_QUEUE(out);
3334 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3335 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3337 put_NXAST_POP_QUEUE(out);
3340 static char * OVS_WARN_UNUSED_RESULT
3341 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3342 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3344 ofpact_put_POP_QUEUE(ofpacts);
3349 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3351 ds_put_cstr(s, "pop_queue");
3354 /* Action structure for NXAST_FIN_TIMEOUT.
3356 * This action changes the idle timeout or hard timeout, or both, of this
3357 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3358 * When such a packet is observed, the action reduces the rule's idle timeout
3359 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3360 * action has no effect on an existing timeout that is already shorter than the
3361 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3362 * of zero has no effect on the respective timeout.
3364 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3365 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3366 * receipt of the FIN or RST.
3368 * This is useful for quickly discarding learned TCP flows that otherwise will
3369 * take a long time to expire.
3371 * This action is intended for use with an OpenFlow rule that matches only a
3372 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3373 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3374 * RST in any of those flows will cause the entire OpenFlow rule to expire
3375 * early, which is not normally desirable.
3377 struct nx_action_fin_timeout {
3378 ovs_be16 type; /* OFPAT_VENDOR. */
3379 ovs_be16 len; /* 16. */
3380 ovs_be32 vendor; /* NX_VENDOR_ID. */
3381 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3382 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3383 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3384 ovs_be16 pad; /* Must be zero. */
3386 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3389 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3390 enum ofp_version ofp_version OVS_UNUSED,
3393 struct ofpact_fin_timeout *oft;
3395 oft = ofpact_put_FIN_TIMEOUT(out);
3396 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3397 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3402 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3403 enum ofp_version ofp_version OVS_UNUSED,
3406 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3407 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3408 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3411 static char * OVS_WARN_UNUSED_RESULT
3412 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3413 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3415 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3418 while (ofputil_parse_key_value(&arg, &key, &value)) {
3421 if (!strcmp(key, "idle_timeout")) {
3422 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3423 } else if (!strcmp(key, "hard_timeout")) {
3424 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3426 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3438 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3440 ds_put_cstr(s, "fin_timeout(");
3441 if (a->fin_idle_timeout) {
3442 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3444 if (a->fin_hard_timeout) {
3445 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3448 ds_put_char(s, ')');
3451 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3453 * These actions search one of the switch's flow tables:
3455 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3456 * it specifies the table to search.
3458 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3459 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3460 * table, that is, the OpenFlow flow table that contains the flow from
3461 * which this action was obtained. If this action did not come from a
3462 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3463 * is the current table.
3465 * The flow table lookup uses a flow that may be slightly modified from the
3468 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3469 * is used as the flow's in_port.
3471 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3472 * then its value is used as the flow's in_port. Otherwise, the original
3475 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3476 * resubmit action, then the flow is updated with the new values.
3478 * Following the lookup, the original in_port is restored.
3480 * If the modified flow matched in the flow table, then the corresponding
3481 * actions are executed. Afterward, actions following the resubmit in the
3482 * original set of actions, if any, are executed; any changes made to the
3483 * packet (e.g. changes to VLAN) by secondary actions persist when those
3484 * actions are executed, although the original in_port is restored.
3486 * Resubmit actions may be used any number of times within a set of actions.
3488 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3489 * implementation-defined depth, further resubmit actions are simply ignored.
3491 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3492 * 'pad' to be all-bits-zero.
3494 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3495 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3497 struct nx_action_resubmit {
3498 ovs_be16 type; /* OFPAT_VENDOR. */
3499 ovs_be16 len; /* Length is 16. */
3500 ovs_be32 vendor; /* NX_VENDOR_ID. */
3501 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3502 ovs_be16 in_port; /* New in_port for checking flow table. */
3503 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3506 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3509 decode_NXAST_RAW_RESUBMIT(uint16_t port,
3510 enum ofp_version ofp_version OVS_UNUSED,
3513 struct ofpact_resubmit *resubmit;
3515 resubmit = ofpact_put_RESUBMIT(out);
3516 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3517 resubmit->in_port = u16_to_ofp(port);
3518 resubmit->table_id = 0xff;
3523 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3524 enum ofp_version ofp_version OVS_UNUSED,
3527 struct ofpact_resubmit *resubmit;
3529 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3530 return OFPERR_OFPBAC_BAD_ARGUMENT;
3533 resubmit = ofpact_put_RESUBMIT(out);
3534 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3535 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3536 resubmit->table_id = nar->table;
3541 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3542 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3544 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3546 if (resubmit->table_id == 0xff
3547 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3548 put_NXAST_RESUBMIT(out, in_port);
3550 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3551 nar->table = resubmit->table_id;
3552 nar->in_port = htons(in_port);
3556 static char * OVS_WARN_UNUSED_RESULT
3557 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3558 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3560 struct ofpact_resubmit *resubmit;
3561 char *in_port_s, *table_s;
3563 resubmit = ofpact_put_RESUBMIT(ofpacts);
3565 in_port_s = strsep(&arg, ",");
3566 if (in_port_s && in_port_s[0]) {
3567 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3568 return xasprintf("%s: resubmit to unknown port", in_port_s);
3571 resubmit->in_port = OFPP_IN_PORT;
3574 table_s = strsep(&arg, ",");
3575 if (table_s && table_s[0]) {
3576 uint32_t table_id = 0;
3579 error = str_to_u32(table_s, &table_id);
3583 resubmit->table_id = table_id;
3585 resubmit->table_id = 255;
3588 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3589 return xstrdup("at least one \"in_port\" or \"table\" must be "
3590 "specified on resubmit");
3596 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3598 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3599 ds_put_cstr(s, "resubmit:");
3600 ofputil_format_port(a->in_port, s);
3602 ds_put_format(s, "resubmit(");
3603 if (a->in_port != OFPP_IN_PORT) {
3604 ofputil_format_port(a->in_port, s);
3606 ds_put_char(s, ',');
3607 if (a->table_id != 255) {
3608 ds_put_format(s, "%"PRIu8, a->table_id);
3610 ds_put_char(s, ')');
3614 /* Action structure for NXAST_LEARN.
3616 * This action adds or modifies a flow in an OpenFlow table, similar to
3617 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3618 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3619 * flow's match criteria and actions are built by applying each of the series
3620 * of flow_mod_spec elements included as part of the action.
3622 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3623 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3624 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3625 * bits from a source to a destination. In this case, the header contains
3628 * 15 14 13 12 11 10 0
3629 * +------+---+------+---------------------------------+
3630 * | 0 |src| dst | n_bits |
3631 * +------+---+------+---------------------------------+
3633 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3634 * following table summarizes the meaning of each possible combination.
3635 * Details follow the table:
3638 * --- --- ----------------------------------------------------------
3639 * 0 0 Add match criteria based on value in a field.
3640 * 1 0 Add match criteria based on an immediate value.
3641 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3642 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3643 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3644 * All other combinations are undefined and not allowed.
3646 * The flow_mod_spec header is followed by a source specification and a
3647 * destination specification. The format and meaning of the source
3648 * specification depends on 'src':
3650 * - If 'src' is 0, the source bits are taken from a field in the flow to
3651 * which this action is attached. (This should be a wildcarded field. If
3652 * its value is fully specified then the source bits being copied have
3655 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3656 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3657 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3658 * 'field' and 'ofs' are subject to the same restrictions as the source
3659 * field in NXAST_REG_MOVE.
3661 * - If 'src' is 1, the source bits are a constant value. The source
3662 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3663 * number in network order, the source bits are the 'n_bits'
3664 * least-significant bits. The switch will report an error if other bits
3665 * in the constant are nonzero.
3667 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3668 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3669 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3670 * meaning of the flow_mod_spec depends on 'dst':
3672 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3673 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3674 * packet equal the source bits. 'field' may be any nxm_header with
3675 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3677 * Order is significant. Earlier flow_mod_specs must satisfy any
3678 * prerequisites for matching fields specified later, by copying constant
3679 * values into prerequisite fields.
3681 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3684 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3685 * the new flow. The new flow copies the source bits into
3686 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3689 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3690 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3692 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3693 * empty. It has the following meaning:
3695 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3696 * The new flow outputs to the OpenFlow port specified by the source field.
3697 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3698 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3701 * Resource Management
3702 * -------------------
3704 * A switch has a finite amount of flow table space available for learning.
3705 * When this space is exhausted, no new learning table entries will be learned
3706 * until some existing flow table entries expire. The controller should be
3707 * prepared to handle this by flooding (which can be implemented as a
3708 * low-priority flow).
3710 * If a learned flow matches a single TCP stream with a relatively long
3711 * timeout, one may make the best of resource constraints by setting
3712 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3713 * both, to shorter timeouts. When either of these is specified as a nonzero
3714 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3720 * The following examples give a prose description of the flow_mod_specs along
3721 * with informal notation for how those would be represented and a hex dump of
3722 * the bytes that would be required.
3724 * These examples could work with various nx_action_learn parameters. Typical
3725 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3726 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3728 * 1. Learn input port based on the source MAC, with lookup into
3729 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3731 * Match on in_port=99:
3732 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3733 * ovs_be16(99), 00 63
3734 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3736 * Match Ethernet destination on Ethernet source from packet:
3737 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3738 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3739 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3741 * Set NXM_NX_REG1[16:31] to the packet's input port:
3742 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3743 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3744 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3746 * Given a packet that arrived on port A with Ethernet source address B,
3747 * this would set up the flow "in_port=99, dl_dst=B,
3748 * actions=load:A->NXM_NX_REG1[16..31]".
3750 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3751 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3752 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3754 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3755 * into NXM_NX_REG1[16:31]:
3757 * Match on same VLAN ID as packet:
3758 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3759 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3760 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3762 * Match Ethernet destination on Ethernet source from packet:
3763 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3764 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3765 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3767 * Output to the packet's input port:
3768 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3769 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3771 * Given a packet that arrived on port A with Ethernet source address B in
3772 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3773 * actions=output:A".
3775 * In syntax accepted by ovs-ofctl, this action is:
3776 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3777 * output:NXM_OF_IN_PORT[])
3779 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3780 * 10-second MAC expiration time to make it easier to see what's going on
3782 * ovs-vsctl del-controller br0
3783 * ovs-ofctl del-flows br0
3784 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3785 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3786 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3787 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3788 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3790 * You can then dump the MAC learning table with:
3792 * ovs-ofctl dump-flows br0 table=1
3797 * For best performance, segregate learned flows into a table that is not used
3798 * for any other flows except possibly for a lowest-priority "catch-all" flow
3799 * (a flow with no match criteria). If different learning actions specify
3800 * different match criteria, use different tables for the learned flows.
3802 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3803 * These timeouts apply to the flow that is added, which means that a flow with
3804 * an idle timeout will expire when no traffic has been sent *to* the learned
3805 * address. This is not usually the intent in MAC learning; instead, we want
3806 * the MAC learn entry to expire when no traffic has been sent *from* the
3807 * learned address. Use a hard timeout for that.
3810 * Visibility of Changes
3811 * ---------------------
3813 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3814 * flow translation are visible to flow table lookups made later in the flow
3815 * translation. This means that, in the example above, a MAC learned by the
3816 * learn action in table 0 would be found in table 1 (if the packet being
3817 * processed had the same source and destination MAC address).
3819 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3820 * modify a flow) by a "learn" action are visible only for later flow
3821 * translations, not for later lookups within the same flow translation. In
3822 * the MAC learning example, a MAC learned by the learn action in table 0 would
3823 * not be found in table 1 if the flow translation would resubmit to table 1
3824 * after the processing of the learn action, meaning that if this MAC had not
3825 * been learned before then the packet would be flooded. */
3826 struct nx_action_learn {
3827 ovs_be16 type; /* OFPAT_VENDOR. */
3828 ovs_be16 len; /* At least 24. */
3829 ovs_be32 vendor; /* NX_VENDOR_ID. */
3830 ovs_be16 subtype; /* NXAST_LEARN. */
3831 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3832 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3833 ovs_be16 priority; /* Priority level of flow entry. */
3834 ovs_be64 cookie; /* Cookie for new flow. */
3835 ovs_be16 flags; /* NX_LEARN_F_*. */
3836 uint8_t table_id; /* Table to insert flow entry. */
3837 uint8_t pad; /* Must be zero. */
3838 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3839 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3840 /* Followed by a sequence of flow_mod_spec elements, as described above,
3841 * until the end of the action is reached. */
3843 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3846 get_be16(const void **pp)
3848 const ovs_be16 *p = *pp;
3849 ovs_be16 value = *p;
3855 get_be32(const void **pp)
3857 const ovs_be32 *p = *pp;
3858 ovs_be32 value = get_unaligned_be32(p);
3864 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3866 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3867 sf->ofs = ntohs(get_be16(p));
3868 sf->n_bits = n_bits;
3872 learn_min_len(uint16_t header)
3874 int n_bits = header & NX_LEARN_N_BITS_MASK;
3875 int src_type = header & NX_LEARN_SRC_MASK;
3876 int dst_type = header & NX_LEARN_DST_MASK;
3877 unsigned int min_len;
3880 if (src_type == NX_LEARN_SRC_FIELD) {
3881 min_len += sizeof(ovs_be32); /* src_field */
3882 min_len += sizeof(ovs_be16); /* src_ofs */
3884 min_len += DIV_ROUND_UP(n_bits, 16);
3886 if (dst_type == NX_LEARN_DST_MATCH ||
3887 dst_type == NX_LEARN_DST_LOAD) {
3888 min_len += sizeof(ovs_be32); /* dst_field */
3889 min_len += sizeof(ovs_be16); /* dst_ofs */
3894 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3895 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3897 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3898 enum ofp_version ofp_version OVS_UNUSED,
3899 struct ofpbuf *ofpacts)
3901 struct ofpact_learn *learn;
3902 const void *p, *end;
3905 return OFPERR_OFPBAC_BAD_ARGUMENT;
3908 learn = ofpact_put_LEARN(ofpacts);
3910 learn->idle_timeout = ntohs(nal->idle_timeout);
3911 learn->hard_timeout = ntohs(nal->hard_timeout);
3912 learn->priority = ntohs(nal->priority);
3913 learn->cookie = nal->cookie;
3914 learn->table_id = nal->table_id;
3915 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3916 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3918 learn->flags = ntohs(nal->flags);
3919 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3920 NX_LEARN_F_DELETE_LEARNED)) {
3921 return OFPERR_OFPBAC_BAD_ARGUMENT;
3924 if (learn->table_id == 0xff) {
3925 return OFPERR_OFPBAC_BAD_ARGUMENT;
3928 end = (char *) nal + ntohs(nal->len);
3929 for (p = nal + 1; p != end; ) {
3930 struct ofpact_learn_spec *spec;
3931 uint16_t header = ntohs(get_be16(&p));
3937 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3938 learn = ofpacts->header;
3941 spec->src_type = header & NX_LEARN_SRC_MASK;
3942 spec->dst_type = header & NX_LEARN_DST_MASK;
3943 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3945 /* Check for valid src and dst type combination. */
3946 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3947 spec->dst_type == NX_LEARN_DST_LOAD ||
3948 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3949 spec->src_type == NX_LEARN_SRC_FIELD)) {
3952 return OFPERR_OFPBAC_BAD_ARGUMENT;
3955 /* Check that the arguments don't overrun the end of the action. */
3956 if ((char *) end - (char *) p < learn_min_len(header)) {
3957 return OFPERR_OFPBAC_BAD_LEN;
3960 /* Get the source. */
3961 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3962 get_subfield(spec->n_bits, &p, &spec->src);
3964 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3966 bitwise_copy(p, p_bytes, 0,
3967 &spec->src_imm, sizeof spec->src_imm, 0,
3969 p = (const uint8_t *) p + p_bytes;
3972 /* Get the destination. */
3973 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3974 spec->dst_type == NX_LEARN_DST_LOAD) {
3975 get_subfield(spec->n_bits, &p, &spec->dst);
3978 ofpact_update_len(ofpacts, &learn->ofpact);
3980 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3981 return OFPERR_OFPBAC_BAD_ARGUMENT;
3988 put_be16(struct ofpbuf *b, ovs_be16 x)
3990 ofpbuf_put(b, &x, sizeof x);
3994 put_be32(struct ofpbuf *b, ovs_be32 x)
3996 ofpbuf_put(b, &x, sizeof x);
4000 put_u16(struct ofpbuf *b, uint16_t x)
4002 put_be16(b, htons(x));
4006 put_u32(struct ofpbuf *b, uint32_t x)
4008 put_be32(b, htonl(x));
4012 encode_LEARN(const struct ofpact_learn *learn,
4013 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4015 const struct ofpact_learn_spec *spec;
4016 struct nx_action_learn *nal;
4019 start_ofs = out->size;
4020 nal = put_NXAST_LEARN(out);
4021 nal->idle_timeout = htons(learn->idle_timeout);
4022 nal->hard_timeout = htons(learn->hard_timeout);
4023 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4024 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4025 nal->priority = htons(learn->priority);
4026 nal->cookie = learn->cookie;
4027 nal->flags = htons(learn->flags);
4028 nal->table_id = learn->table_id;
4030 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4031 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4033 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4034 put_u32(out, mf_nxm_header(spec->src.field->id));
4035 put_u16(out, spec->src.ofs);
4037 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4038 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4039 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4040 bits, n_dst_bytes, 0,
4044 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4045 spec->dst_type == NX_LEARN_DST_LOAD) {
4046 put_u32(out, mf_nxm_header(spec->dst.field->id));
4047 put_u16(out, spec->dst.ofs);
4051 pad_ofpat(out, start_ofs);
4054 static char * OVS_WARN_UNUSED_RESULT
4055 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4056 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4058 return learn_parse(arg, ofpacts);
4062 format_LEARN(const struct ofpact_learn *a, struct ds *s)
4067 /* Action structure for NXAST_CONJUNCTION. */
4068 struct nx_action_conjunction {
4069 ovs_be16 type; /* OFPAT_VENDOR. */
4070 ovs_be16 len; /* At least 16. */
4071 ovs_be32 vendor; /* NX_VENDOR_ID. */
4072 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4077 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4080 add_conjunction(struct ofpbuf *out,
4081 uint32_t id, uint8_t clause, uint8_t n_clauses)
4083 struct ofpact_conjunction *oc;
4085 oc = ofpact_put_CONJUNCTION(out);
4087 oc->clause = clause;
4088 oc->n_clauses = n_clauses;
4092 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4093 enum ofp_version ofp_version OVS_UNUSED,
4096 if (nac->n_clauses < 2 || nac->n_clauses > 64
4097 || nac->clause >= nac->n_clauses) {
4098 return OFPERR_NXBAC_BAD_CONJUNCTION;
4100 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4106 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4107 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4109 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4110 nac->clause = oc->clause;
4111 nac->n_clauses = oc->n_clauses;
4112 nac->id = htonl(oc->id);
4116 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4118 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4119 oc->id, oc->clause + 1, oc->n_clauses);
4122 static char * OVS_WARN_UNUSED_RESULT
4123 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4124 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4131 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4132 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4133 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4136 if (n_clauses < 2) {
4137 return xstrdup("conjunction must have at least 2 clauses");
4138 } else if (n_clauses > 64) {
4139 return xstrdup("conjunction must have at most 64 clauses");
4140 } else if (clause < 1) {
4141 return xstrdup("clause index must be positive");
4142 } else if (clause > n_clauses) {
4143 return xstrdup("clause index must be less than or equal to "
4144 "number of clauses");
4147 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4151 /* Action structure for NXAST_MULTIPATH.
4153 * This action performs the following steps in sequence:
4155 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4156 * Refer to the definition of "enum nx_mp_fields" for details.
4158 * The 'basis' value is used as a universal hash parameter, that is,
4159 * different values of 'basis' yield different hash functions. The
4160 * particular universal hash function used is implementation-defined.
4162 * The hashed fields' values are drawn from the current state of the
4163 * flow, including all modifications that have been made by actions up to
4166 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4167 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4170 * The output of the algorithm is 'link', an unsigned integer less than
4171 * or equal to 'max_link'.
4173 * Some algorithms use 'arg' as an additional argument.
4175 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4176 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4179 * The switch will reject actions that have an unknown 'fields', or an unknown
4180 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4181 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4182 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4184 struct nx_action_multipath {
4185 ovs_be16 type; /* OFPAT_VENDOR. */
4186 ovs_be16 len; /* Length is 32. */
4187 ovs_be32 vendor; /* NX_VENDOR_ID. */
4188 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4190 /* What fields to hash and how. */
4191 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4192 ovs_be16 basis; /* Universal hash parameter. */
4195 /* Multipath link choice algorithm to apply to hash value. */
4196 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4197 ovs_be16 max_link; /* Number of output links, minus 1. */
4198 ovs_be32 arg; /* Algorithm-specific argument. */
4201 /* Where to store the result. */
4202 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4203 ovs_be32 dst; /* Destination. */
4205 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4208 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4209 enum ofp_version ofp_version OVS_UNUSED,
4212 uint32_t n_links = ntohs(nam->max_link) + 1;
4213 size_t min_n_bits = log_2_ceil(n_links);
4214 struct ofpact_multipath *mp;
4216 mp = ofpact_put_MULTIPATH(out);
4217 mp->fields = ntohs(nam->fields);
4218 mp->basis = ntohs(nam->basis);
4219 mp->algorithm = ntohs(nam->algorithm);
4220 mp->max_link = ntohs(nam->max_link);
4221 mp->arg = ntohl(nam->arg);
4222 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4223 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4224 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4226 if (!flow_hash_fields_valid(mp->fields)) {
4227 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4228 return OFPERR_OFPBAC_BAD_ARGUMENT;
4229 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4230 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4231 && mp->algorithm != NX_MP_ALG_HRW
4232 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4233 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4234 return OFPERR_OFPBAC_BAD_ARGUMENT;
4235 } else if (mp->dst.n_bits < min_n_bits) {
4236 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4237 "%"PRIu32" links", min_n_bits, n_links);
4238 return OFPERR_OFPBAC_BAD_ARGUMENT;
4241 return multipath_check(mp, NULL);
4245 encode_MULTIPATH(const struct ofpact_multipath *mp,
4246 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4248 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4250 nam->fields = htons(mp->fields);
4251 nam->basis = htons(mp->basis);
4252 nam->algorithm = htons(mp->algorithm);
4253 nam->max_link = htons(mp->max_link);
4254 nam->arg = htonl(mp->arg);
4255 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4256 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4259 static char * OVS_WARN_UNUSED_RESULT
4260 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4261 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4263 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4267 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4269 multipath_format(a, s);
4272 /* Action structure for NXAST_NOTE.
4274 * This action has no effect. It is variable length. The switch does not
4275 * attempt to interpret the user-defined 'note' data in any way. A controller
4276 * can use this action to attach arbitrary metadata to a flow.
4278 * This action might go away in the future.
4280 struct nx_action_note {
4281 ovs_be16 type; /* OFPAT_VENDOR. */
4282 ovs_be16 len; /* A multiple of 8, but at least 16. */
4283 ovs_be32 vendor; /* NX_VENDOR_ID. */
4284 ovs_be16 subtype; /* NXAST_NOTE. */
4285 uint8_t note[6]; /* Start of user-defined data. */
4286 /* Possibly followed by additional user-defined data. */
4288 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4291 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4292 enum ofp_version ofp_version OVS_UNUSED,
4295 struct ofpact_note *note;
4296 unsigned int length;
4298 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4299 note = ofpact_put(out, OFPACT_NOTE,
4300 offsetof(struct ofpact_note, data) + length);
4301 note->length = length;
4302 memcpy(note->data, nan->note, length);
4308 encode_NOTE(const struct ofpact_note *note,
4309 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4311 size_t start_ofs = out->size;
4312 struct nx_action_note *nan;
4313 unsigned int remainder;
4316 put_NXAST_NOTE(out);
4317 out->size = out->size - sizeof nan->note;
4319 ofpbuf_put(out, note->data, note->length);
4321 len = out->size - start_ofs;
4322 remainder = len % OFP_ACTION_ALIGN;
4324 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4326 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4327 nan->len = htons(out->size - start_ofs);
4330 static char * OVS_WARN_UNUSED_RESULT
4331 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4332 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4334 struct ofpact_note *note;
4336 note = ofpact_put_NOTE(ofpacts);
4337 while (*arg != '\0') {
4348 byte = hexits_value(arg, 2, &ok);
4350 return xstrdup("bad hex digit in `note' argument");
4352 ofpbuf_put(ofpacts, &byte, 1);
4354 note = ofpacts->header;
4359 ofpact_update_len(ofpacts, ¬e->ofpact);
4364 format_NOTE(const struct ofpact_note *a, struct ds *s)
4368 ds_put_cstr(s, "note:");
4369 for (i = 0; i < a->length; i++) {
4371 ds_put_char(s, '.');
4373 ds_put_format(s, "%02"PRIx8, a->data[i]);
4380 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4382 ofpact_put_EXIT(out);
4387 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4388 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4390 put_NXAST_EXIT(out);
4393 static char * OVS_WARN_UNUSED_RESULT
4394 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4395 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4397 ofpact_put_EXIT(ofpacts);
4402 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4404 ds_put_cstr(s, "exit");
4407 /* Unroll xlate action. */
4410 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4411 enum ofp_version ofp_version OVS_UNUSED,
4412 struct ofpbuf *out OVS_UNUSED)
4417 static char * OVS_WARN_UNUSED_RESULT
4418 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4419 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4426 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4429 ds_put_cstr(s, "unroll_xlate");
4432 /* Action structure for NXAST_SAMPLE.
4434 * Samples matching packets with the given probability and sends them
4435 * each to the set of collectors identified with the given ID. The
4436 * probability is expressed as a number of packets to be sampled out
4437 * of USHRT_MAX packets, and must be >0.
4439 * When sending packet samples to IPFIX collectors, the IPFIX flow
4440 * record sent for each sampled packet is associated with the given
4441 * observation domain ID and observation point ID. Each IPFIX flow
4442 * record contain the sampled packet's headers when executing this
4443 * rule. If a sampled packet's headers are modified by previous
4444 * actions in the flow, those modified headers are sent. */
4445 struct nx_action_sample {
4446 ovs_be16 type; /* OFPAT_VENDOR. */
4447 ovs_be16 len; /* Length is 24. */
4448 ovs_be32 vendor; /* NX_VENDOR_ID. */
4449 ovs_be16 subtype; /* NXAST_SAMPLE. */
4450 ovs_be16 probability; /* Fraction of packets to sample. */
4451 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4452 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4453 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4455 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4458 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4459 enum ofp_version ofp_version OVS_UNUSED,
4462 struct ofpact_sample *sample;
4464 sample = ofpact_put_SAMPLE(out);
4465 sample->probability = ntohs(nas->probability);
4466 sample->collector_set_id = ntohl(nas->collector_set_id);
4467 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4468 sample->obs_point_id = ntohl(nas->obs_point_id);
4470 if (sample->probability == 0) {
4471 return OFPERR_OFPBAC_BAD_ARGUMENT;
4478 encode_SAMPLE(const struct ofpact_sample *sample,
4479 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4481 struct nx_action_sample *nas;
4483 nas = put_NXAST_SAMPLE(out);
4484 nas->probability = htons(sample->probability);
4485 nas->collector_set_id = htonl(sample->collector_set_id);
4486 nas->obs_domain_id = htonl(sample->obs_domain_id);
4487 nas->obs_point_id = htonl(sample->obs_point_id);
4490 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4491 * action to 'ofpacts'.
4493 * Returns NULL if successful, otherwise a malloc()'d string describing the
4494 * error. The caller is responsible for freeing the returned string. */
4495 static char * OVS_WARN_UNUSED_RESULT
4496 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4497 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4499 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4502 while (ofputil_parse_key_value(&arg, &key, &value)) {
4505 if (!strcmp(key, "probability")) {
4506 error = str_to_u16(value, "probability", &os->probability);
4507 if (!error && os->probability == 0) {
4508 error = xasprintf("invalid probability value \"%s\"", value);
4510 } else if (!strcmp(key, "collector_set_id")) {
4511 error = str_to_u32(value, &os->collector_set_id);
4512 } else if (!strcmp(key, "obs_domain_id")) {
4513 error = str_to_u32(value, &os->obs_domain_id);
4514 } else if (!strcmp(key, "obs_point_id")) {
4515 error = str_to_u32(value, &os->obs_point_id);
4517 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4524 if (os->probability == 0) {
4525 return xstrdup("non-zero \"probability\" must be specified on sample");
4531 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4533 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4534 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4535 a->probability, a->collector_set_id,
4536 a->obs_domain_id, a->obs_point_id);
4539 /* debug_recirc instruction. */
4541 static bool enable_debug;
4544 ofpact_dummy_enable(void)
4546 enable_debug = true;
4550 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4552 if (!enable_debug) {
4553 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4556 ofpact_put_DEBUG_RECIRC(out);
4561 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4562 enum ofp_version ofp_version OVS_UNUSED,
4565 put_NXAST_DEBUG_RECIRC(out);
4568 static char * OVS_WARN_UNUSED_RESULT
4569 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4570 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4572 ofpact_put_DEBUG_RECIRC(ofpacts);
4577 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4579 ds_put_cstr(s, "debug_recirc");
4582 /* Action structure for NXAST_CT.
4584 * Pass traffic to the connection tracker.
4586 * There are two important concepts to understanding the connection tracking
4587 * interface: Packet state and Connection state. Packets may be "Untracked" or
4588 * "Tracked". Connections may be "Uncommitted" or "Committed".
4592 * Untracked packets have not yet passed through the connection tracker,
4593 * and the connection state for such packets is unknown. In most cases,
4594 * packets entering the OpenFlow pipeline will initially be in the
4595 * untracked state. Untracked packets may become tracked by executing
4596 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4597 * about the connection available, in particular the connection state.
4599 * Tracked packets have previously passed through the connection tracker.
4600 * These packets will remain tracked through until the end of the OpenFlow
4601 * pipeline. Tracked packets which have NXAST_CT executed with a
4602 * "recirc_table" specified will return to the tracked state.
4604 * The packet state is only significant for the duration of packet
4605 * processing within the OpenFlow pipeline.
4607 * - Connection State:
4609 * Multiple packets may be associated with a single connection. Initially,
4610 * all connections are uncommitted. The connection state corresponding to
4611 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4613 * Uncommitted connections have no state stored about them. Uncommitted
4614 * connections may transition into the committed state by executing
4615 * NXAST_CT with the NX_CT_F_COMMIT flag.
4617 * Once a connection becomes committed, information may be gathered about
4618 * the connection by passing subsequent packets through the connection
4619 * tracker, and the state of the connection will be stored beyond the
4620 * lifetime of packet processing.
4622 * Connections may transition back into the uncommitted state due to
4623 * external timers, or due to the contents of packets that are sent to the
4624 * connection tracker. This behaviour is outside of the scope of the
4625 * OpenFlow interface.
4627 * The "zone" specifies a context within which the tracking is done:
4629 * The connection tracking zone is a 16-bit number. Each zone is an
4630 * independent connection tracking context. The connection state for each
4631 * connection is completely separate for each zone, so if a connection
4632 * is committed to zone A, then it will remain uncommitted in zone B.
4633 * If NXAST_CT is executed with the same zone multiple times, later
4634 * executions have no effect.
4636 * If 'zone_src' is nonzero, this specifies that the zone should be
4637 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4638 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4639 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4640 * are the same as the acceptable nxm_header values for the 'src' field of
4643 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4644 * connection tracking zone.
4646 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4648 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4649 * packet will be logically cloned prior to executing this action. One
4650 * copy will be sent to the connection tracker, then will be re-injected
4651 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
4652 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4653 * fields will be populated. The original instance of the packet will
4654 * continue the current actions list. This can be thought of as similar to
4655 * the effect of the "output" action: One copy is sent out (in this case,
4656 * to the connection tracker), but the current copy continues processing.
4658 * It is strongly recommended that this table is later than the current
4659 * table, to prevent loops.
4661 struct nx_action_conntrack {
4662 ovs_be16 type; /* OFPAT_VENDOR. */
4663 ovs_be16 len; /* At least 24. */
4664 ovs_be32 vendor; /* NX_VENDOR_ID. */
4665 ovs_be16 subtype; /* NXAST_CT. */
4666 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
4667 * Unspecified flag bits must be zero. */
4668 ovs_be32 zone_src; /* Connection tracking context. */
4670 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
4671 ovs_be16 zone_imm; /* Immediate value for zone. */
4673 uint8_t recirc_table; /* Recirculate to a specific table, or
4674 NX_CT_RECIRC_NONE for no recirculation. */
4675 uint8_t pad[5]; /* Zeroes */
4676 /* Followed by a sequence of zero or more OpenFlow actions. The length of
4677 * these is included in 'len'. */
4679 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
4682 decode_ct_zone(const struct nx_action_conntrack *nac,
4683 struct ofpact_conntrack *out)
4685 if (nac->zone_src) {
4688 out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
4689 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
4690 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
4691 error = mf_check_src(&out->zone_src, NULL);
4696 if (out->zone_src.n_bits != 16) {
4697 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
4698 out->zone_src.n_bits);
4699 return OFPERR_OFPBAC_BAD_SET_LEN;
4702 out->zone_src.field = NULL;
4703 out->zone_imm = ntohs(nac->zone_imm);
4710 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
4711 enum ofp_version ofp_version OVS_UNUSED,
4714 struct ofpact_conntrack *conntrack;
4717 conntrack = ofpact_put_CT(out);
4718 conntrack->flags = ntohs(nac->flags);
4719 error = decode_ct_zone(nac, conntrack);
4723 conntrack->recirc_table = nac->recirc_table;
4730 encode_CT(const struct ofpact_conntrack *conntrack,
4731 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4733 struct nx_action_conntrack *nac;
4735 nac = put_NXAST_CT(out);
4736 nac->flags = htons(conntrack->flags);
4737 if (conntrack->zone_src.field) {
4738 nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
4739 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
4740 conntrack->zone_src.n_bits);
4742 nac->zone_src = htonl(0);
4743 nac->zone_imm = htons(conntrack->zone_imm);
4745 nac->recirc_table = conntrack->recirc_table;
4748 /* Parses 'arg' as the argument to a "ct" action, and appends such an
4749 * action to 'ofpacts'.
4751 * Returns NULL if successful, otherwise a malloc()'d string describing the
4752 * error. The caller is responsible for freeing the returned string. */
4753 static char * OVS_WARN_UNUSED_RESULT
4754 parse_CT(char *arg, struct ofpbuf *ofpacts,
4755 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4757 struct ofpact_conntrack *oc;
4761 oc = ofpact_put_CT(ofpacts);
4763 oc->recirc_table = NX_CT_RECIRC_NONE;
4764 while (ofputil_parse_key_value(&arg, &key, &value)) {
4765 if (!strcmp(key, "commit")) {
4766 oc->flags |= NX_CT_F_COMMIT;
4767 } else if (!strcmp(key, "table")) {
4768 error = str_to_u8(value, "recirc_table", &oc->recirc_table);
4769 if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
4770 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
4772 } else if (!strcmp(key, "zone")) {
4773 error = str_to_u16(value, "zone", &oc->zone_imm);
4777 error = mf_parse_subfield(&oc->zone_src, value);
4783 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
4794 format_CT(const struct ofpact_conntrack *a, struct ds *s)
4796 ds_put_cstr(s, "ct(");
4797 if (a->flags & NX_CT_F_COMMIT) {
4798 ds_put_cstr(s, "commit,");
4800 if (a->recirc_table != NX_CT_RECIRC_NONE) {
4801 ds_put_format(s, "table=%"PRIu8",", a->recirc_table);
4803 if (a->zone_src.field) {
4804 ds_put_format(s, "zone=");
4805 mf_format_subfield(&a->zone_src, s);
4806 ds_put_char(s, ',');
4807 } else if (a->zone_imm) {
4808 ds_put_format(s, "zone=%"PRIu16",", a->zone_imm);
4811 ds_put_char(s, ')');
4814 /* Meter instruction. */
4817 encode_METER(const struct ofpact_meter *meter,
4818 enum ofp_version ofp_version, struct ofpbuf *out)
4820 if (ofp_version >= OFP13_VERSION) {
4821 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4825 static char * OVS_WARN_UNUSED_RESULT
4826 parse_METER(char *arg, struct ofpbuf *ofpacts,
4827 enum ofputil_protocol *usable_protocols)
4829 *usable_protocols &= OFPUTIL_P_OF13_UP;
4830 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
4834 format_METER(const struct ofpact_meter *a, struct ds *s)
4836 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
4839 /* Clear-Actions instruction. */
4842 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
4843 enum ofp_version ofp_version OVS_UNUSED,
4844 struct ofpbuf *out OVS_UNUSED)
4846 if (ofp_version > OFP10_VERSION) {
4847 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
4851 static char * OVS_WARN_UNUSED_RESULT
4852 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4853 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4855 ofpact_put_CLEAR_ACTIONS(ofpacts);
4860 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4862 ds_put_cstr(s, "clear_actions");
4865 /* Write-Actions instruction. */
4868 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
4869 enum ofp_version ofp_version, struct ofpbuf *out)
4871 if (ofp_version > OFP10_VERSION) {
4872 const size_t ofs = out->size;
4874 instruction_put_OFPIT11_WRITE_ACTIONS(out);
4875 ofpacts_put_openflow_actions(actions->actions,
4876 ofpact_nest_get_action_len(actions),
4878 ofpacts_update_instruction_actions(out, ofs);
4882 static char * OVS_WARN_UNUSED_RESULT
4883 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
4884 enum ofputil_protocol *usable_protocols)
4886 size_t ofs = ofpacts_pull(ofpacts);
4887 struct ofpact_nest *on;
4890 /* Add a Write-Actions instruction and then pull it off. */
4891 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
4892 ofpbuf_pull(ofpacts, sizeof *on);
4894 /* Parse nested actions.
4896 * We pulled off "write-actions" and the previous actions because the
4897 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4898 * that it doesn't actually include the nested actions. That means that
4899 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4900 * follows a Write-Actions, which is an invalid order. */
4901 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
4902 OFPACT_WRITE_ACTIONS);
4904 /* Put the Write-Actions back on and update its length. */
4905 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
4906 on->ofpact.len = ofpacts->size;
4908 /* Put any previous actions or instructions back on. */
4909 ofpbuf_push_uninit(ofpacts, ofs);
4915 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
4917 ds_put_cstr(s, "write_actions(");
4918 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
4919 ds_put_char(s, ')');
4922 /* Action structure for NXAST_WRITE_METADATA.
4924 * Modifies the 'mask' bits of the metadata value. */
4925 struct nx_action_write_metadata {
4926 ovs_be16 type; /* OFPAT_VENDOR. */
4927 ovs_be16 len; /* Length is 32. */
4928 ovs_be32 vendor; /* NX_VENDOR_ID. */
4929 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
4930 uint8_t zeros[6]; /* Must be zero. */
4931 ovs_be64 metadata; /* Metadata register. */
4932 ovs_be64 mask; /* Metadata mask. */
4934 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
4937 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
4938 enum ofp_version ofp_version OVS_UNUSED,
4941 struct ofpact_metadata *om;
4943 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
4944 return OFPERR_NXBRC_MUST_BE_ZERO;
4947 om = ofpact_put_WRITE_METADATA(out);
4948 om->metadata = nawm->metadata;
4949 om->mask = nawm->mask;
4955 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
4956 enum ofp_version ofp_version, struct ofpbuf *out)
4958 if (ofp_version == OFP10_VERSION) {
4959 struct nx_action_write_metadata *nawm;
4961 nawm = put_NXAST_WRITE_METADATA(out);
4962 nawm->metadata = metadata->metadata;
4963 nawm->mask = metadata->mask;
4965 struct ofp11_instruction_write_metadata *oiwm;
4967 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
4968 oiwm->metadata = metadata->metadata;
4969 oiwm->metadata_mask = metadata->mask;
4973 static char * OVS_WARN_UNUSED_RESULT
4974 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
4975 enum ofputil_protocol *usable_protocols)
4977 struct ofpact_metadata *om;
4978 char *mask = strchr(arg, '/');
4980 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
4982 om = ofpact_put_WRITE_METADATA(ofpacts);
4987 error = str_to_be64(mask + 1, &om->mask);
4992 om->mask = OVS_BE64_MAX;
4995 return str_to_be64(arg, &om->metadata);
4999 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5001 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
5002 if (a->mask != OVS_BE64_MAX) {
5003 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5007 /* Goto-Table instruction. */
5010 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5011 enum ofp_version ofp_version, struct ofpbuf *out)
5013 if (ofp_version == OFP10_VERSION) {
5014 struct nx_action_resubmit *nar;
5016 nar = put_NXAST_RESUBMIT_TABLE(out);
5017 nar->table = goto_table->table_id;
5018 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5020 struct ofp11_instruction_goto_table *oigt;
5022 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5023 oigt->table_id = goto_table->table_id;
5024 memset(oigt->pad, 0, sizeof oigt->pad);
5028 static char * OVS_WARN_UNUSED_RESULT
5029 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5030 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5032 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5033 char *table_s = strsep(&arg, ",");
5034 if (!table_s || !table_s[0]) {
5035 return xstrdup("instruction goto-table needs table id");
5037 return str_to_u8(table_s, "table", &ogt->table_id);
5041 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5043 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
5047 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
5048 const struct ofp_action_header *bad_action, enum ofperr error)
5050 if (!VLOG_DROP_WARN(&rl)) {
5054 ds_put_hex_dump(&s, actions, actions_len, 0, false);
5055 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5056 (char *)bad_action - (char *)actions,
5057 ofperr_get_name(error), ds_cstr(&s));
5063 ofpacts_decode(const void *actions, size_t actions_len,
5064 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5066 struct ofpbuf openflow;
5068 ofpbuf_use_const(&openflow, actions, actions_len);
5069 while (openflow.size) {
5070 const struct ofp_action_header *action = openflow.data;
5071 enum ofp_raw_action_type raw;
5075 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5077 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
5081 log_bad_action(actions, actions_len, action, error);
5086 ofpact_pad(ofpacts);
5091 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5092 unsigned int actions_len,
5093 enum ofp_version version,
5094 uint32_t allowed_ovsinsts,
5095 struct ofpbuf *ofpacts,
5096 enum ofpact_type outer_action)
5098 const struct ofp_action_header *actions;
5101 if (!outer_action) {
5102 ofpbuf_clear(ofpacts);
5105 if (actions_len % OFP_ACTION_ALIGN != 0) {
5106 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5107 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5108 return OFPERR_OFPBRC_BAD_LEN;
5111 actions = ofpbuf_try_pull(openflow, actions_len);
5112 if (actions == NULL) {
5113 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
5114 "remaining message length (%"PRIu32")",
5115 actions_len, openflow->size);
5116 return OFPERR_OFPBRC_BAD_LEN;
5119 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5121 ofpbuf_clear(ofpacts);
5125 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
5128 ofpbuf_clear(ofpacts);
5133 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
5134 * front of 'openflow' into ofpacts. On success, replaces any existing content
5135 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
5136 * Returns 0 if successful, otherwise an OpenFlow error.
5138 * Actions are processed according to their OpenFlow version which
5139 * is provided in the 'version' parameter.
5141 * In most places in OpenFlow, actions appear encapsulated in instructions, so
5142 * you should call ofpacts_pull_openflow_instructions() instead of this
5145 * The parsed actions are valid generically, but they may not be valid in a
5146 * specific context. For example, port numbers up to OFPP_MAX are valid
5147 * generically, but specific datapaths may only support port numbers in a
5148 * smaller range. Use ofpacts_check() to additional check whether actions are
5149 * valid in a specific context. */
5151 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
5152 unsigned int actions_len,
5153 enum ofp_version version,
5154 struct ofpbuf *ofpacts)
5156 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
5157 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
5161 /* OpenFlow 1.1 actions. */
5164 /* True if an action sets the value of a field
5165 * in a way that is compatibile with the action set.
5166 * The field can be set via either a set or a move action.
5167 * False otherwise. */
5169 ofpact_is_set_or_move_action(const struct ofpact *a)
5172 case OFPACT_SET_FIELD:
5173 case OFPACT_REG_MOVE:
5174 case OFPACT_SET_ETH_DST:
5175 case OFPACT_SET_ETH_SRC:
5176 case OFPACT_SET_IP_DSCP:
5177 case OFPACT_SET_IP_ECN:
5178 case OFPACT_SET_IP_TTL:
5179 case OFPACT_SET_IPV4_DST:
5180 case OFPACT_SET_IPV4_SRC:
5181 case OFPACT_SET_L4_DST_PORT:
5182 case OFPACT_SET_L4_SRC_PORT:
5183 case OFPACT_SET_MPLS_LABEL:
5184 case OFPACT_SET_MPLS_TC:
5185 case OFPACT_SET_MPLS_TTL:
5186 case OFPACT_SET_QUEUE:
5187 case OFPACT_SET_TUNNEL:
5188 case OFPACT_SET_VLAN_PCP:
5189 case OFPACT_SET_VLAN_VID:
5192 case OFPACT_CLEAR_ACTIONS:
5194 case OFPACT_CONTROLLER:
5195 case OFPACT_DEC_MPLS_TTL:
5196 case OFPACT_DEC_TTL:
5197 case OFPACT_ENQUEUE:
5199 case OFPACT_UNROLL_XLATE:
5200 case OFPACT_FIN_TIMEOUT:
5201 case OFPACT_GOTO_TABLE:
5204 case OFPACT_CONJUNCTION:
5206 case OFPACT_MULTIPATH:
5209 case OFPACT_OUTPUT_REG:
5210 case OFPACT_POP_MPLS:
5211 case OFPACT_POP_QUEUE:
5212 case OFPACT_PUSH_MPLS:
5213 case OFPACT_PUSH_VLAN:
5214 case OFPACT_RESUBMIT:
5216 case OFPACT_STACK_POP:
5217 case OFPACT_STACK_PUSH:
5218 case OFPACT_STRIP_VLAN:
5219 case OFPACT_WRITE_ACTIONS:
5220 case OFPACT_WRITE_METADATA:
5221 case OFPACT_DEBUG_RECIRC:
5228 /* True if an action is allowed in the action set.
5229 * False otherwise. */
5231 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
5234 case OFPACT_DEC_MPLS_TTL:
5235 case OFPACT_DEC_TTL:
5238 case OFPACT_POP_MPLS:
5239 case OFPACT_PUSH_MPLS:
5240 case OFPACT_PUSH_VLAN:
5241 case OFPACT_REG_MOVE:
5242 case OFPACT_SET_FIELD:
5243 case OFPACT_SET_ETH_DST:
5244 case OFPACT_SET_ETH_SRC:
5245 case OFPACT_SET_IP_DSCP:
5246 case OFPACT_SET_IP_ECN:
5247 case OFPACT_SET_IP_TTL:
5248 case OFPACT_SET_IPV4_DST:
5249 case OFPACT_SET_IPV4_SRC:
5250 case OFPACT_SET_L4_DST_PORT:
5251 case OFPACT_SET_L4_SRC_PORT:
5252 case OFPACT_SET_MPLS_LABEL:
5253 case OFPACT_SET_MPLS_TC:
5254 case OFPACT_SET_MPLS_TTL:
5255 case OFPACT_SET_QUEUE:
5256 case OFPACT_SET_TUNNEL:
5257 case OFPACT_SET_VLAN_PCP:
5258 case OFPACT_SET_VLAN_VID:
5259 case OFPACT_STRIP_VLAN:
5262 /* In general these actions are excluded because they are not part of
5263 * the OpenFlow specification nor map to actions that are defined in
5264 * the specification. Thus the order in which they should be applied
5265 * in the action set is undefined. */
5267 case OFPACT_CONTROLLER:
5269 case OFPACT_ENQUEUE:
5271 case OFPACT_UNROLL_XLATE:
5272 case OFPACT_FIN_TIMEOUT:
5274 case OFPACT_CONJUNCTION:
5275 case OFPACT_MULTIPATH:
5277 case OFPACT_OUTPUT_REG:
5278 case OFPACT_POP_QUEUE:
5279 case OFPACT_RESUBMIT:
5281 case OFPACT_STACK_POP:
5282 case OFPACT_STACK_PUSH:
5283 case OFPACT_DEBUG_RECIRC:
5285 /* The action set may only include actions and thus
5286 * may not include any instructions */
5287 case OFPACT_CLEAR_ACTIONS:
5288 case OFPACT_GOTO_TABLE:
5290 case OFPACT_WRITE_ACTIONS:
5291 case OFPACT_WRITE_METADATA:
5298 /* Append ofpact 'a' onto the tail of 'out' */
5300 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
5302 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
5305 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
5307 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
5308 enum ofpact_type filter)
5310 const struct ofpact *target;
5311 const struct ofpact *a;
5314 OFPACT_FOR_EACH (a, in->data, in->size) {
5315 if (a->type == filter) {
5320 ofpact_copy(out, target);
5322 return target != NULL;
5325 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5326 * The order of appended ofpacts is preserved between 'in' and 'out' */
5328 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5329 bool (*filter)(const struct ofpact *))
5331 const struct ofpact *a;
5333 OFPACT_FOR_EACH (a, in->data, in->size) {
5335 ofpact_copy(out, a);
5340 /* Reads 'action_set', which contains ofpacts accumulated by
5341 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5342 * executed directly into 'action_list'. (These names correspond to the
5343 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5345 * In general this involves appending the last instance of each action that is
5346 * admissible in the action set in the order described in the OpenFlow
5350 * + output action is only appended if no group action was present in 'in'.
5351 * + As a simplification all set actions are copied in the order the are
5352 * provided in 'in' as many set actions applied to a field has the same
5353 * affect as only applying the last action that sets a field and
5354 * duplicates are removed by do_xlate_actions().
5355 * This has an unwanted side-effect of compsoting multiple
5356 * LOAD_REG actions that touch different regions of the same field. */
5358 ofpacts_execute_action_set(struct ofpbuf *action_list,
5359 const struct ofpbuf *action_set)
5361 /* The OpenFlow spec "Action Set" section specifies this order. */
5362 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5363 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5364 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5365 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5366 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5367 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5368 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5369 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5371 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5372 * we should execute only OFPACT_GROUP.
5374 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5375 * all the actions because there's no point in modifying a packet that will
5376 * not be sent anywhere. */
5377 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5378 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5379 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5380 ofpbuf_clear(action_list);
5386 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5387 size_t n_in, enum ofp_version version,
5392 size_t start = out->size;
5394 error = ofpacts_decode(in, n_in, version, out);
5400 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5401 if (!ofpact_is_allowed_in_actions_set(a)) {
5402 VLOG_WARN_RL(&rl, "disallowed action in action set");
5403 return OFPERR_OFPBAC_BAD_TYPE;
5410 /* OpenFlow 1.1 instructions. */
5412 struct instruction_type_info {
5413 enum ovs_instruction_type type;
5417 static const struct instruction_type_info inst_info[] = {
5418 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5424 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5426 return inst_info[type].name;
5430 ovs_instruction_type_from_name(const char *name)
5432 const struct instruction_type_info *p;
5433 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5434 if (!strcasecmp(name, p->name)) {
5441 enum ovs_instruction_type
5442 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5446 return OVSINST_OFPIT13_METER;
5447 case OFPACT_CLEAR_ACTIONS:
5448 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5449 case OFPACT_WRITE_ACTIONS:
5450 return OVSINST_OFPIT11_WRITE_ACTIONS;
5451 case OFPACT_WRITE_METADATA:
5452 return OVSINST_OFPIT11_WRITE_METADATA;
5453 case OFPACT_GOTO_TABLE:
5454 return OVSINST_OFPIT11_GOTO_TABLE;
5457 case OFPACT_CONTROLLER:
5458 case OFPACT_ENQUEUE:
5459 case OFPACT_OUTPUT_REG:
5461 case OFPACT_SET_VLAN_VID:
5462 case OFPACT_SET_VLAN_PCP:
5463 case OFPACT_STRIP_VLAN:
5464 case OFPACT_PUSH_VLAN:
5465 case OFPACT_SET_ETH_SRC:
5466 case OFPACT_SET_ETH_DST:
5467 case OFPACT_SET_IPV4_SRC:
5468 case OFPACT_SET_IPV4_DST:
5469 case OFPACT_SET_IP_DSCP:
5470 case OFPACT_SET_IP_ECN:
5471 case OFPACT_SET_IP_TTL:
5472 case OFPACT_SET_L4_SRC_PORT:
5473 case OFPACT_SET_L4_DST_PORT:
5474 case OFPACT_REG_MOVE:
5475 case OFPACT_SET_FIELD:
5476 case OFPACT_STACK_PUSH:
5477 case OFPACT_STACK_POP:
5478 case OFPACT_DEC_TTL:
5479 case OFPACT_SET_MPLS_LABEL:
5480 case OFPACT_SET_MPLS_TC:
5481 case OFPACT_SET_MPLS_TTL:
5482 case OFPACT_DEC_MPLS_TTL:
5483 case OFPACT_PUSH_MPLS:
5484 case OFPACT_POP_MPLS:
5485 case OFPACT_SET_TUNNEL:
5486 case OFPACT_SET_QUEUE:
5487 case OFPACT_POP_QUEUE:
5488 case OFPACT_FIN_TIMEOUT:
5489 case OFPACT_RESUBMIT:
5491 case OFPACT_CONJUNCTION:
5492 case OFPACT_MULTIPATH:
5495 case OFPACT_UNROLL_XLATE:
5497 case OFPACT_DEBUG_RECIRC:
5500 return OVSINST_OFPIT11_APPLY_ACTIONS;
5505 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
5506 const uint16_t inst_type)
5508 switch (inst_type) {
5510 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5512 *instruction_type = OVSINST_##ENUM; \
5518 return OFPERR_OFPBIC_UNKNOWN_INST;
5522 /* Two-way translation between OVS's internal "OVSINST_*" representation of
5523 * instructions and the "OFPIT_*" representation used in OpenFlow. */
5524 struct ovsinst_map {
5525 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
5526 int ofpit; /* OFPIT_* number from OpenFlow spec. */
5529 static const struct ovsinst_map *
5530 get_ovsinst_map(enum ofp_version version)
5532 /* OpenFlow 1.1 and 1.2 instructions. */
5533 static const struct ovsinst_map of11[] = {
5534 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5535 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5536 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5537 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5538 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5542 /* OpenFlow 1.3+ instructions. */
5543 static const struct ovsinst_map of13[] = {
5544 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5545 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5546 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5547 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5548 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5549 { OVSINST_OFPIT13_METER, 6 },
5553 return version < OFP13_VERSION ? of11 : of13;
5556 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
5557 * values, into a bitmap of instructions suitable for OpenFlow 'version'
5558 * (OFP11_VERSION or later), and returns the result. */
5560 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
5562 uint32_t ofpit_bitmap = 0;
5563 const struct ovsinst_map *x;
5565 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5566 if (ovsinst_bitmap & (1u << x->ovsinst)) {
5567 ofpit_bitmap |= 1u << x->ofpit;
5570 return htonl(ofpit_bitmap);
5573 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5574 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5575 * correspond to OVSINST_* values, and returns the result. */
5577 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
5579 uint32_t ovsinst_bitmap = 0;
5580 const struct ovsinst_map *x;
5582 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5583 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
5584 ovsinst_bitmap |= 1u << x->ovsinst;
5587 return ovsinst_bitmap;
5590 static inline struct ofp11_instruction *
5591 instruction_next(const struct ofp11_instruction *inst)
5593 return ((struct ofp11_instruction *) (void *)
5594 ((uint8_t *) inst + ntohs(inst->len)));
5598 instruction_is_valid(const struct ofp11_instruction *inst,
5599 size_t n_instructions)
5601 uint16_t len = ntohs(inst->len);
5602 return (!(len % OFP11_INSTRUCTION_ALIGN)
5603 && len >= sizeof *inst
5604 && len / sizeof *inst <= n_instructions);
5607 /* This macro is careful to check for instructions with bad lengths. */
5608 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
5609 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
5610 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
5611 ((LEFT) -= (ntohs((ITER)->len) \
5612 / sizeof(struct ofp11_instruction)), \
5613 (ITER) = instruction_next(ITER)))
5616 decode_openflow11_instruction(const struct ofp11_instruction *inst,
5617 enum ovs_instruction_type *type)
5619 uint16_t len = ntohs(inst->len);
5621 switch (inst->type) {
5622 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5623 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5625 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5626 case CONSTANT_HTONS(ENUM): \
5628 ? len >= sizeof(struct STRUCT) \
5629 : len == sizeof(struct STRUCT)) { \
5630 *type = OVSINST_##ENUM; \
5633 return OFPERR_OFPBIC_BAD_LEN; \
5639 return OFPERR_OFPBIC_UNKNOWN_INST;
5644 decode_openflow11_instructions(const struct ofp11_instruction insts[],
5646 const struct ofp11_instruction *out[])
5648 const struct ofp11_instruction *inst;
5651 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5652 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5653 enum ovs_instruction_type type;
5656 error = decode_openflow11_instruction(inst, &type);
5662 return OFPERR_OFPBIC_DUP_INST;
5668 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5669 (n_insts - left) * sizeof *inst);
5670 return OFPERR_OFPBIC_BAD_LEN;
5676 get_actions_from_instruction(const struct ofp11_instruction *inst,
5677 const struct ofp_action_header **actions,
5678 size_t *actions_len)
5680 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5681 *actions_len = ntohs(inst->len) - sizeof *inst;
5685 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5686 unsigned int instructions_len,
5687 enum ofp_version version,
5688 struct ofpbuf *ofpacts)
5690 const struct ofp11_instruction *instructions;
5691 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5694 if (version == OFP10_VERSION) {
5695 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5697 (1u << N_OVS_INSTRUCTIONS) - 1,
5701 ofpbuf_clear(ofpacts);
5703 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5704 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5706 instructions_len, OFP11_INSTRUCTION_ALIGN);
5707 error = OFPERR_OFPBIC_BAD_LEN;
5711 instructions = ofpbuf_try_pull(openflow, instructions_len);
5712 if (instructions == NULL) {
5713 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5714 "remaining message length (%"PRIu32")",
5715 instructions_len, openflow->size);
5716 error = OFPERR_OFPBIC_BAD_LEN;
5720 error = decode_openflow11_instructions(
5721 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5727 if (insts[OVSINST_OFPIT13_METER]) {
5728 const struct ofp13_instruction_meter *oim;
5729 struct ofpact_meter *om;
5731 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5732 insts[OVSINST_OFPIT13_METER]);
5734 om = ofpact_put_METER(ofpacts);
5735 om->meter_id = ntohl(oim->meter_id);
5737 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5738 const struct ofp_action_header *actions;
5741 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5742 &actions, &actions_len);
5743 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5748 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5749 instruction_get_OFPIT11_CLEAR_ACTIONS(
5750 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5751 ofpact_put_CLEAR_ACTIONS(ofpacts);
5753 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5754 struct ofpact_nest *on;
5755 const struct ofp_action_header *actions;
5759 ofpact_pad(ofpacts);
5760 start = ofpacts->size;
5761 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5762 offsetof(struct ofpact_nest, actions));
5763 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5764 &actions, &actions_len);
5765 error = ofpacts_decode_for_action_set(actions, actions_len,
5770 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5771 on->ofpact.len = ofpacts->size - start;
5773 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5774 const struct ofp11_instruction_write_metadata *oiwm;
5775 struct ofpact_metadata *om;
5777 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5778 insts[OVSINST_OFPIT11_WRITE_METADATA]);
5780 om = ofpact_put_WRITE_METADATA(ofpacts);
5781 om->metadata = oiwm->metadata;
5782 om->mask = oiwm->metadata_mask;
5784 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5785 const struct ofp11_instruction_goto_table *oigt;
5786 struct ofpact_goto_table *ogt;
5788 oigt = instruction_get_OFPIT11_GOTO_TABLE(
5789 insts[OVSINST_OFPIT11_GOTO_TABLE]);
5790 ogt = ofpact_put_GOTO_TABLE(ofpacts);
5791 ogt->table_id = oigt->table_id;
5794 error = ofpacts_verify(ofpacts->data, ofpacts->size,
5795 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
5798 ofpbuf_clear(ofpacts);
5803 /* Update the length of the instruction that begins at offset 'ofs' within
5804 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5805 * If the instruction contains no nested actions, deletes it entirely. */
5807 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5809 struct ofp11_instruction_actions *oia;
5811 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5812 if (openflow->size > ofs + sizeof *oia) {
5813 oia->len = htons(openflow->size - ofs);
5815 openflow->size = ofs;
5819 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5820 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5821 * valid, otherwise an OpenFlow error code. */
5823 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5831 case OFPP_CONTROLLER:
5836 return OFPERR_OFPBAC_BAD_OUT_PORT;
5839 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
5842 return OFPERR_OFPBAC_BAD_OUT_PORT;
5846 /* Removes the protocols that require consistency between match and actions
5847 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5849 * (An example of an inconsistency between match and actions is a flow that
5850 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5853 inconsistent_match(enum ofputil_protocol *usable_protocols)
5855 *usable_protocols &= OFPUTIL_P_OF10_ANY;
5858 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5859 * caller must restore them.
5861 * Modifies some actions, filling in fields that could not be properly set
5862 * without context. */
5864 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
5865 struct flow *flow, ofp_port_t max_ports,
5866 uint8_t table_id, uint8_t n_tables)
5868 const struct ofpact_enqueue *enqueue;
5869 const struct mf_field *mf;
5873 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
5876 case OFPACT_CONTROLLER:
5879 case OFPACT_ENQUEUE:
5880 enqueue = ofpact_get_ENQUEUE(a);
5881 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
5882 && enqueue->port != OFPP_IN_PORT
5883 && enqueue->port != OFPP_LOCAL) {
5884 return OFPERR_OFPBAC_BAD_OUT_PORT;
5888 case OFPACT_OUTPUT_REG:
5889 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
5892 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
5894 case OFPACT_SET_VLAN_VID:
5895 /* Remember if we saw a vlan tag in the flow to aid translating to
5896 * OpenFlow 1.1+ if need be. */
5897 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
5898 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5899 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5900 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5901 inconsistent_match(usable_protocols);
5903 /* Temporary mark that we have a vlan tag. */
5904 flow->vlan_tci |= htons(VLAN_CFI);
5907 case OFPACT_SET_VLAN_PCP:
5908 /* Remember if we saw a vlan tag in the flow to aid translating to
5909 * OpenFlow 1.1+ if need be. */
5910 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
5911 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5912 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5913 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5914 inconsistent_match(usable_protocols);
5916 /* Temporary mark that we have a vlan tag. */
5917 flow->vlan_tci |= htons(VLAN_CFI);
5920 case OFPACT_STRIP_VLAN:
5921 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5922 inconsistent_match(usable_protocols);
5924 /* Temporary mark that we have no vlan tag. */
5925 flow->vlan_tci = htons(0);
5928 case OFPACT_PUSH_VLAN:
5929 if (flow->vlan_tci & htons(VLAN_CFI)) {
5930 /* Multiple VLAN headers not supported. */
5931 return OFPERR_OFPBAC_BAD_TAG;
5933 /* Temporary mark that we have a vlan tag. */
5934 flow->vlan_tci |= htons(VLAN_CFI);
5937 case OFPACT_SET_ETH_SRC:
5938 case OFPACT_SET_ETH_DST:
5941 case OFPACT_SET_IPV4_SRC:
5942 case OFPACT_SET_IPV4_DST:
5943 if (flow->dl_type != htons(ETH_TYPE_IP)) {
5944 inconsistent_match(usable_protocols);
5948 case OFPACT_SET_IP_DSCP:
5949 case OFPACT_SET_IP_ECN:
5950 case OFPACT_SET_IP_TTL:
5951 case OFPACT_DEC_TTL:
5952 if (!is_ip_any(flow)) {
5953 inconsistent_match(usable_protocols);
5957 case OFPACT_SET_L4_SRC_PORT:
5958 case OFPACT_SET_L4_DST_PORT:
5959 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
5960 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5961 && flow->nw_proto != IPPROTO_SCTP)) {
5962 inconsistent_match(usable_protocols);
5964 /* Note on which transport protocol the port numbers are set.
5965 * This allows this set action to be converted to an OF1.2 set field
5967 if (a->type == OFPACT_SET_L4_SRC_PORT) {
5968 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
5970 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
5974 case OFPACT_REG_MOVE:
5975 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
5977 case OFPACT_SET_FIELD:
5978 mf = ofpact_get_SET_FIELD(a)->field;
5979 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
5980 if (!mf_are_prereqs_ok(mf, flow) ||
5981 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
5982 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
5984 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5986 /* Remember if we saw a vlan tag in the flow to aid translating to
5987 * OpenFlow 1.1 if need be. */
5988 ofpact_get_SET_FIELD(a)->flow_has_vlan =
5989 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5990 if (mf->id == MFF_VLAN_TCI) {
5991 /* The set field may add or remove the vlan tag,
5992 * Mark the status temporarily. */
5993 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
5997 case OFPACT_STACK_PUSH:
5998 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
6000 case OFPACT_STACK_POP:
6001 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6003 case OFPACT_SET_MPLS_LABEL:
6004 case OFPACT_SET_MPLS_TC:
6005 case OFPACT_SET_MPLS_TTL:
6006 case OFPACT_DEC_MPLS_TTL:
6007 if (!eth_type_mpls(flow->dl_type)) {
6008 inconsistent_match(usable_protocols);
6012 case OFPACT_SET_TUNNEL:
6013 case OFPACT_SET_QUEUE:
6014 case OFPACT_POP_QUEUE:
6015 case OFPACT_RESUBMIT:
6018 case OFPACT_FIN_TIMEOUT:
6019 if (flow->nw_proto != IPPROTO_TCP) {
6020 inconsistent_match(usable_protocols);
6025 return learn_check(ofpact_get_LEARN(a), flow);
6027 case OFPACT_CONJUNCTION:
6030 case OFPACT_MULTIPATH:
6031 return multipath_check(ofpact_get_MULTIPATH(a), flow);
6037 case OFPACT_PUSH_MPLS:
6038 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6039 /* The packet is now MPLS and the MPLS payload is opaque.
6040 * Thus nothing can be assumed about the network protocol.
6041 * Temporarily mark that we have no nw_proto. */
6045 case OFPACT_POP_MPLS:
6046 if (!eth_type_mpls(flow->dl_type)) {
6047 inconsistent_match(usable_protocols);
6049 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6056 struct ofpact_conntrack *oc = ofpact_get_CT(a);
6058 if (!dl_type_is_ip_any(flow->dl_type)
6059 || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6060 inconsistent_match(usable_protocols);
6063 if (oc->zone_src.field) {
6064 return mf_check_src(&oc->zone_src, flow);
6069 case OFPACT_CLEAR_ACTIONS:
6072 case OFPACT_WRITE_ACTIONS: {
6073 /* Use a temporary copy of 'usable_protocols' because we can't check
6074 * consistency of an action set. */
6075 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6076 enum ofputil_protocol p = *usable_protocols;
6077 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6078 flow, max_ports, table_id, n_tables, &p);
6081 case OFPACT_WRITE_METADATA:
6084 case OFPACT_METER: {
6085 uint32_t mid = ofpact_get_METER(a)->meter_id;
6086 if (mid == 0 || mid > OFPM13_MAX) {
6087 return OFPERR_OFPMMFC_INVALID_METER;
6092 case OFPACT_GOTO_TABLE: {
6093 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
6094 if ((table_id != 255 && goto_table <= table_id)
6095 || (n_tables != 255 && goto_table >= n_tables)) {
6096 return OFPERR_OFPBIC_BAD_TABLE_ID;
6104 case OFPACT_UNROLL_XLATE:
6105 /* UNROLL is an internal action that should never be seen via
6107 return OFPERR_OFPBAC_BAD_TYPE;
6109 case OFPACT_DEBUG_RECIRC:
6117 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6118 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6119 * switch with no more than 'max_ports' ports.
6121 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6122 * '*usable_protocols' the protocols that forbid the inconsistency. (An
6123 * example of an inconsistency between match and actions is a flow that does
6124 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6126 * May annotate ofpacts with information gathered from the 'flow'.
6128 * May temporarily modify 'flow', but restores the changes before returning. */
6130 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
6131 struct flow *flow, ofp_port_t max_ports,
6132 uint8_t table_id, uint8_t n_tables,
6133 enum ofputil_protocol *usable_protocols)
6136 ovs_be16 dl_type = flow->dl_type;
6137 ovs_be16 vlan_tci = flow->vlan_tci;
6138 uint8_t nw_proto = flow->nw_proto;
6139 enum ofperr error = 0;
6141 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6142 error = ofpact_check__(usable_protocols, a, flow,
6143 max_ports, table_id, n_tables);
6148 /* Restore fields that may have been modified. */
6149 flow->dl_type = dl_type;
6150 flow->vlan_tci = vlan_tci;
6151 flow->nw_proto = nw_proto;
6155 /* Like ofpacts_check(), but reports inconsistencies as
6156 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6158 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
6159 struct flow *flow, ofp_port_t max_ports,
6160 uint8_t table_id, uint8_t n_tables,
6161 enum ofputil_protocol usable_protocols)
6163 enum ofputil_protocol p = usable_protocols;
6166 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
6167 table_id, n_tables, &p);
6168 return (error ? error
6169 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
6174 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
6176 if (outer_action != OFPACT_WRITE_ACTIONS) {
6177 VLOG_WARN("\"%s\" action doesn't support nested action \"%s\"",
6178 ofpact_name(outer_action), ofpact_name(a->type));
6179 return OFPERR_OFPBAC_BAD_ARGUMENT;
6185 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
6186 * appropriate order as defined by the OpenFlow spec and as required by Open
6189 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
6190 * instructions that are allowed within 'ofpacts[]'.
6192 * If 'outer_action' is not zero, it specifies that the actions are nested
6193 * within another action of type 'outer_action'. */
6195 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
6196 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
6198 const struct ofpact *a;
6199 enum ovs_instruction_type inst;
6201 inst = OVSINST_OFPIT13_METER;
6202 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6203 enum ovs_instruction_type next;
6205 if (a->type == OFPACT_CONJUNCTION) {
6206 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6207 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
6208 VLOG_WARN("\"conjunction\" actions may be used along with "
6209 "\"note\" but not any other kind of action "
6210 "(such as the \"%s\" action used here)",
6211 ofpact_name(a->type));
6212 return OFPERR_NXBAC_BAD_CONJUNCTION;
6219 enum ofperr error = ofpacts_verify_nested(a, outer_action);
6226 next = ovs_instruction_type_from_ofpact_type(a->type);
6228 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
6231 const char *name = ovs_instruction_name_from_type(inst);
6232 const char *next_name = ovs_instruction_name_from_type(next);
6235 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
6236 "1.1+ compatibility", name);
6238 VLOG_WARN("invalid instruction ordering: %s must appear "
6239 "before %s, for OpenFlow 1.1+ compatibility",
6242 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
6244 if (!((1u << next) & allowed_ovsinsts)) {
6245 const char *name = ovs_instruction_name_from_type(next);
6247 VLOG_WARN("%s instruction not allowed here", name);
6248 return OFPERR_OFPBIC_UNSUP_INST;
6257 /* Converting ofpacts to OpenFlow. */
6260 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
6264 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6265 case OFPACT_##ENUM: \
6266 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
6275 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
6276 * actions in 'openflow', appending the actions to any existing data in
6279 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
6280 struct ofpbuf *openflow,
6281 enum ofp_version ofp_version)
6283 const struct ofpact *a;
6284 size_t start_size = openflow->size;
6286 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6287 encode_ofpact(a, ofp_version, openflow);
6289 return openflow->size - start_size;
6292 static enum ovs_instruction_type
6293 ofpact_is_apply_actions(const struct ofpact *a)
6295 return (ovs_instruction_type_from_ofpact_type(a->type)
6296 == OVSINST_OFPIT11_APPLY_ACTIONS);
6300 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
6302 struct ofpbuf *openflow,
6303 enum ofp_version ofp_version)
6305 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
6306 const struct ofpact *a;
6308 if (ofp_version == OFP10_VERSION) {
6309 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
6316 if (ofpact_is_apply_actions(a)) {
6317 size_t ofs = openflow->size;
6319 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
6321 encode_ofpact(a, ofp_version, openflow);
6323 } while (a < end && ofpact_is_apply_actions(a));
6324 ofpacts_update_instruction_actions(openflow, ofs);
6326 encode_ofpact(a, ofp_version, openflow);
6332 /* Sets of supported actions. */
6334 /* Two-way translation between OVS's internal "OFPACT_*" representation of
6335 * actions and the "OFPAT_*" representation used in some OpenFlow version.
6336 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
6337 * instance is specific to one OpenFlow version.) */
6339 enum ofpact_type ofpact; /* Internal name for action type. */
6340 int ofpat; /* OFPAT_* number from OpenFlow spec. */
6343 static const struct ofpact_map *
6344 get_ofpact_map(enum ofp_version version)
6346 /* OpenFlow 1.0 actions. */
6347 static const struct ofpact_map of10[] = {
6348 { OFPACT_OUTPUT, 0 },
6349 { OFPACT_SET_VLAN_VID, 1 },
6350 { OFPACT_SET_VLAN_PCP, 2 },
6351 { OFPACT_STRIP_VLAN, 3 },
6352 { OFPACT_SET_ETH_SRC, 4 },
6353 { OFPACT_SET_ETH_DST, 5 },
6354 { OFPACT_SET_IPV4_SRC, 6 },
6355 { OFPACT_SET_IPV4_DST, 7 },
6356 { OFPACT_SET_IP_DSCP, 8 },
6357 { OFPACT_SET_L4_SRC_PORT, 9 },
6358 { OFPACT_SET_L4_DST_PORT, 10 },
6359 { OFPACT_ENQUEUE, 11 },
6363 /* OpenFlow 1.1 actions. */
6364 static const struct ofpact_map of11[] = {
6365 { OFPACT_OUTPUT, 0 },
6366 { OFPACT_SET_VLAN_VID, 1 },
6367 { OFPACT_SET_VLAN_PCP, 2 },
6368 { OFPACT_SET_ETH_SRC, 3 },
6369 { OFPACT_SET_ETH_DST, 4 },
6370 { OFPACT_SET_IPV4_SRC, 5 },
6371 { OFPACT_SET_IPV4_DST, 6 },
6372 { OFPACT_SET_IP_DSCP, 7 },
6373 { OFPACT_SET_IP_ECN, 8 },
6374 { OFPACT_SET_L4_SRC_PORT, 9 },
6375 { OFPACT_SET_L4_DST_PORT, 10 },
6376 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6377 /* OFPAT_COPY_TTL_IN (12) not supported. */
6378 { OFPACT_SET_MPLS_LABEL, 13 },
6379 { OFPACT_SET_MPLS_TC, 14 },
6380 { OFPACT_SET_MPLS_TTL, 15 },
6381 { OFPACT_DEC_MPLS_TTL, 16 },
6382 { OFPACT_PUSH_VLAN, 17 },
6383 { OFPACT_STRIP_VLAN, 18 },
6384 { OFPACT_PUSH_MPLS, 19 },
6385 { OFPACT_POP_MPLS, 20 },
6386 { OFPACT_SET_QUEUE, 21 },
6387 { OFPACT_GROUP, 22 },
6388 { OFPACT_SET_IP_TTL, 23 },
6389 { OFPACT_DEC_TTL, 24 },
6393 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6394 static const struct ofpact_map of12[] = {
6395 { OFPACT_OUTPUT, 0 },
6396 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6397 /* OFPAT_COPY_TTL_IN (12) not supported. */
6398 { OFPACT_SET_MPLS_TTL, 15 },
6399 { OFPACT_DEC_MPLS_TTL, 16 },
6400 { OFPACT_PUSH_VLAN, 17 },
6401 { OFPACT_STRIP_VLAN, 18 },
6402 { OFPACT_PUSH_MPLS, 19 },
6403 { OFPACT_POP_MPLS, 20 },
6404 { OFPACT_SET_QUEUE, 21 },
6405 { OFPACT_GROUP, 22 },
6406 { OFPACT_SET_IP_TTL, 23 },
6407 { OFPACT_DEC_TTL, 24 },
6408 { OFPACT_SET_FIELD, 25 },
6409 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
6410 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
6430 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
6431 * values, into a bitmap of actions suitable for OpenFlow 'version', and
6432 * returns the result. */
6434 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
6436 uint32_t openflow_bitmap = 0;
6437 const struct ofpact_map *x;
6439 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6440 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
6441 openflow_bitmap |= 1u << x->ofpat;
6444 return htonl(openflow_bitmap);
6447 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
6448 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
6449 * and returns the result. */
6451 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
6453 uint64_t ofpact_bitmap = 0;
6454 const struct ofpact_map *x;
6456 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6457 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
6458 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
6461 return ofpact_bitmap;
6464 /* Appends to 's' a string representation of the set of OFPACT_* represented
6465 * by 'ofpacts_bitmap'. */
6467 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
6469 if (!ofpacts_bitmap) {
6470 ds_put_cstr(s, "<none>");
6472 while (ofpacts_bitmap) {
6473 ds_put_format(s, "%s ",
6474 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
6475 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
6481 /* Returns true if 'action' outputs to 'port', false otherwise. */
6483 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
6485 switch (ofpact->type) {
6487 return ofpact_get_OUTPUT(ofpact)->port == port;
6488 case OFPACT_ENQUEUE:
6489 return ofpact_get_ENQUEUE(ofpact)->port == port;
6490 case OFPACT_CONTROLLER:
6491 return port == OFPP_CONTROLLER;
6493 case OFPACT_OUTPUT_REG:
6495 case OFPACT_SET_VLAN_VID:
6496 case OFPACT_SET_VLAN_PCP:
6497 case OFPACT_STRIP_VLAN:
6498 case OFPACT_PUSH_VLAN:
6499 case OFPACT_SET_ETH_SRC:
6500 case OFPACT_SET_ETH_DST:
6501 case OFPACT_SET_IPV4_SRC:
6502 case OFPACT_SET_IPV4_DST:
6503 case OFPACT_SET_IP_DSCP:
6504 case OFPACT_SET_IP_ECN:
6505 case OFPACT_SET_IP_TTL:
6506 case OFPACT_SET_L4_SRC_PORT:
6507 case OFPACT_SET_L4_DST_PORT:
6508 case OFPACT_REG_MOVE:
6509 case OFPACT_SET_FIELD:
6510 case OFPACT_STACK_PUSH:
6511 case OFPACT_STACK_POP:
6512 case OFPACT_DEC_TTL:
6513 case OFPACT_SET_MPLS_LABEL:
6514 case OFPACT_SET_MPLS_TC:
6515 case OFPACT_SET_MPLS_TTL:
6516 case OFPACT_DEC_MPLS_TTL:
6517 case OFPACT_SET_TUNNEL:
6518 case OFPACT_WRITE_METADATA:
6519 case OFPACT_SET_QUEUE:
6520 case OFPACT_POP_QUEUE:
6521 case OFPACT_FIN_TIMEOUT:
6522 case OFPACT_RESUBMIT:
6524 case OFPACT_CONJUNCTION:
6525 case OFPACT_MULTIPATH:
6528 case OFPACT_UNROLL_XLATE:
6529 case OFPACT_PUSH_MPLS:
6530 case OFPACT_POP_MPLS:
6532 case OFPACT_CLEAR_ACTIONS:
6533 case OFPACT_WRITE_ACTIONS:
6534 case OFPACT_GOTO_TABLE:
6537 case OFPACT_DEBUG_RECIRC:
6544 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6545 * to 'port', false otherwise. */
6547 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
6550 const struct ofpact *a;
6552 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6553 if (ofpact_outputs_to_port(a, port)) {
6561 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6562 * to 'group', false otherwise. */
6564 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
6567 const struct ofpact *a;
6569 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6570 if (a->type == OFPACT_GROUP
6571 && ofpact_get_GROUP(a)->group_id == group_id) {
6580 ofpacts_equal(const struct ofpact *a, size_t a_len,
6581 const struct ofpact *b, size_t b_len)
6583 return a_len == b_len && !memcmp(a, b, a_len);
6586 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
6587 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
6589 * This function relies on the order of 'ofpacts' being correct (as checked by
6590 * ofpacts_verify()). */
6592 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
6594 const struct ofpact *a;
6596 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6597 enum ovs_instruction_type inst;
6599 inst = ovs_instruction_type_from_ofpact_type(a->type);
6600 if (a->type == OFPACT_METER) {
6601 return ofpact_get_METER(a)->meter_id;
6602 } else if (inst > OVSINST_OFPIT13_METER) {
6610 /* Formatting ofpacts. */
6613 ofpact_format(const struct ofpact *a, struct ds *s)
6616 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6617 case OFPACT_##ENUM: \
6618 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
6627 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
6628 * 'ofpacts' to 'string'. */
6630 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
6634 ds_put_cstr(string, "drop");
6636 const struct ofpact *a;
6638 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6640 ds_put_cstr(string, ",");
6643 /* XXX write-actions */
6644 ofpact_format(a, string);
6649 /* Internal use by helpers. */
6652 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
6654 struct ofpact *ofpact;
6656 ofpact_pad(ofpacts);
6657 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
6658 ofpact = ofpacts->header;
6659 ofpact_init(ofpact, type, len);
6664 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
6666 memset(ofpact, 0, len);
6667 ofpact->type = type;
6672 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6673 * starting at 'ofpact'.
6675 * This is the correct way to update a variable-length ofpact's length after
6676 * adding the variable-length part of the payload. (See the large comment
6677 * near the end of ofp-actions.h for more information.) */
6679 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
6681 ovs_assert(ofpact == ofpacts->header);
6682 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6685 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6686 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6687 * client must call this itself after adding the final ofpact to an array of
6690 * (The consequences of failing to call this function are probably not dire.
6691 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6692 * not dereference it. That's undefined behavior, technically, but it will not
6693 * cause a real problem on common systems. Still, it seems better to call
6696 ofpact_pad(struct ofpbuf *ofpacts)
6698 unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
6700 ofpbuf_put_zeros(ofpacts, pad);
6707 static char * OVS_WARN_UNUSED_RESULT
6708 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6709 enum ofputil_protocol *usable_protocols)
6712 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6713 case OFPACT_##ENUM: \
6714 return parse_##ENUM(value, ofpacts, usable_protocols);
6723 ofpact_type_from_name(const char *name, enum ofpact_type *type)
6725 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6726 if (!strcasecmp(name, NAME)) { \
6727 *type = OFPACT_##ENUM; \
6736 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6738 * Returns NULL if successful, otherwise a malloc()'d string describing the
6739 * error. The caller is responsible for freeing the returned string.
6741 * If 'outer_action' is specified, indicates that the actions being parsed
6742 * are nested within another action of the type specified in 'outer_action'. */
6743 static char * OVS_WARN_UNUSED_RESULT
6744 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6745 enum ofputil_protocol *usable_protocols,
6746 bool allow_instructions, enum ofpact_type outer_action)
6755 while (ofputil_parse_key_value(&pos, &key, &value)) {
6756 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6757 enum ofpact_type type;
6761 if (ofpact_type_from_name(key, &type)) {
6762 error = ofpact_parse(type, value, ofpacts, usable_protocols);
6763 inst = ovs_instruction_type_from_ofpact_type(type);
6764 } else if (!strcasecmp(key, "mod_vlan_vid")) {
6765 error = parse_set_vlan_vid(value, ofpacts, true);
6766 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6767 error = parse_set_vlan_pcp(value, ofpacts, true);
6768 } else if (!strcasecmp(key, "set_nw_ttl")) {
6769 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6770 } else if (!strcasecmp(key, "pop_vlan")) {
6771 error = parse_pop_vlan(ofpacts);
6772 } else if (!strcasecmp(key, "set_tunnel64")) {
6773 error = parse_set_tunnel(value, ofpacts,
6774 NXAST_RAW_SET_TUNNEL64);
6775 } else if (!strcasecmp(key, "load")) {
6776 error = parse_reg_load(value, ofpacts);
6777 } else if (!strcasecmp(key, "bundle_load")) {
6778 error = parse_bundle_load(value, ofpacts);
6779 } else if (!strcasecmp(key, "drop")) {
6781 } else if (!strcasecmp(key, "apply_actions")) {
6782 return xstrdup("apply_actions is the default instruction");
6783 } else if (ofputil_port_from_string(key, &port)) {
6784 ofpact_put_OUTPUT(ofpacts)->port = port;
6786 return xasprintf("unknown action %s", key);
6792 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
6793 if (!allow_instructions) {
6794 return xasprintf("only actions are allowed here (not "
6796 ovs_instruction_name_from_type(inst));
6798 if (inst == prev_inst) {
6799 return xasprintf("instruction %s may be specified only once",
6800 ovs_instruction_name_from_type(inst));
6803 if (prev_inst != -1 && inst < prev_inst) {
6804 return xasprintf("instruction %s must be specified before %s",
6805 ovs_instruction_name_from_type(inst),
6806 ovs_instruction_name_from_type(prev_inst));
6810 ofpact_pad(ofpacts);
6812 if (drop && ofpacts->size) {
6813 return xstrdup("\"drop\" must not be accompanied by any other action "
6817 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
6819 ? (1u << N_OVS_INSTRUCTIONS) - 1
6820 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
6823 return xstrdup("Incorrect instruction ordering");
6829 static char * OVS_WARN_UNUSED_RESULT
6830 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
6831 enum ofputil_protocol *usable_protocols, bool allow_instructions,
6832 enum ofpact_type outer_action)
6834 uint32_t orig_size = ofpacts->size;
6835 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
6836 allow_instructions, outer_action);
6838 ofpacts->size = orig_size;
6843 static char * OVS_WARN_UNUSED_RESULT
6844 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
6845 enum ofputil_protocol *usable_protocols,
6846 bool allow_instructions, enum ofpact_type outer_action)
6850 *usable_protocols = OFPUTIL_P_ANY;
6853 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
6860 /* Parses 's' as a set of OpenFlow actions and appends the actions to
6861 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
6862 * that are nested within the action of type 'outer_action'.
6864 * Returns NULL if successful, otherwise a malloc()'d string describing the
6865 * error. The caller is responsible for freeing the returned string. */
6866 char * OVS_WARN_UNUSED_RESULT
6867 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
6868 enum ofputil_protocol *usable_protocols)
6870 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
6873 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
6876 * Returns NULL if successful, otherwise a malloc()'d string describing the
6877 * error. The caller is responsible for freeing the returned string. */
6878 char * OVS_WARN_UNUSED_RESULT
6879 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
6880 enum ofputil_protocol *usable_protocols)
6882 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
6886 ofpact_name(enum ofpact_type type)
6889 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
6896 /* Low-level action decoding and encoding functions. */
6898 /* Everything needed to identify a particular OpenFlow action. */
6899 struct ofpact_hdrs {
6900 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
6901 uint16_t type; /* Type if standard, otherwise subtype. */
6902 uint8_t ofp_version; /* From ofp_header. */
6905 /* Information about a particular OpenFlow action. */
6906 struct ofpact_raw_instance {
6907 /* The action's identity. */
6908 struct ofpact_hdrs hdrs;
6909 enum ofp_raw_action_type raw;
6911 /* Looking up the action. */
6912 struct hmap_node decode_node; /* Based on 'hdrs'. */
6913 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
6915 /* The action's encoded size.
6917 * If this action is fixed-length, 'min_length' == 'max_length'.
6918 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
6919 * OFP_ACTION_ALIGN) == 65528. */
6920 unsigned short int min_length;
6921 unsigned short int max_length;
6923 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
6924 * offset of that argument from the beginning of the action and 'arg_len'
6925 * its length, both in bytes.
6927 * For actions that take other forms, these are both zero. */
6928 unsigned short int arg_ofs;
6929 unsigned short int arg_len;
6931 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
6934 /* If this action is deprecated, a human-readable string with a brief
6936 const char *deprecation;
6939 /* Action header. */
6940 struct ofp_action_header {
6941 /* The meaning of other values of 'type' generally depends on the OpenFlow
6942 * version (see enum ofp_raw_action_type).
6944 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
6945 * designates an OpenFlow vendor ID and that the remainder of the action
6946 * structure has a vendor-defined meaning.
6948 #define OFPAT_VENDOR 0xffff
6951 /* Always a multiple of 8. */
6954 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
6955 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
6958 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
6960 /* Header for Nicira-defined actions and for ONF vendor extensions.
6962 * This cannot be used as an entirely generic vendor extension action header,
6963 * because OpenFlow does not specify the location or size of the action
6964 * subtype; it just happens that ONF extensions and Nicira extensions share
6966 struct ext_action_header {
6967 ovs_be16 type; /* OFPAT_VENDOR. */
6968 ovs_be16 len; /* At least 16. */
6969 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
6970 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
6973 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
6976 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
6977 const struct ofpact_hdrs *b)
6979 return (a->vendor == b->vendor
6980 && a->type == b->type
6981 && a->ofp_version == b->ofp_version);
6985 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
6987 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
6990 #include "ofp-actions.inc2"
6992 static struct hmap *
6993 ofpact_decode_hmap(void)
6995 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6996 static struct hmap hmap;
6998 if (ovsthread_once_start(&once)) {
6999 struct ofpact_raw_instance *inst;
7002 for (inst = all_raw_instances;
7003 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7005 hmap_insert(&hmap, &inst->decode_node,
7006 ofpact_hdrs_hash(&inst->hdrs));
7008 ovsthread_once_done(&once);
7013 static struct hmap *
7014 ofpact_encode_hmap(void)
7016 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7017 static struct hmap hmap;
7019 if (ovsthread_once_start(&once)) {
7020 struct ofpact_raw_instance *inst;
7023 for (inst = all_raw_instances;
7024 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7026 hmap_insert(&hmap, &inst->encode_node,
7027 hash_2words(inst->raw, inst->hdrs.ofp_version));
7029 ovsthread_once_done(&once);
7035 ofpact_decode_raw(enum ofp_version ofp_version,
7036 const struct ofp_action_header *oah, size_t length,
7037 const struct ofpact_raw_instance **instp)
7039 const struct ofpact_raw_instance *inst;
7040 struct ofpact_hdrs hdrs;
7043 if (length < sizeof *oah) {
7044 return OFPERR_OFPBAC_BAD_LEN;
7047 /* Get base action type. */
7048 if (oah->type == htons(OFPAT_VENDOR)) {
7050 hdrs.vendor = ntohl(oah->vendor);
7051 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
7052 /* Get extension subtype. */
7053 const struct ext_action_header *nah;
7055 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
7056 if (length < sizeof *nah) {
7057 return OFPERR_OFPBAC_BAD_LEN;
7059 hdrs.type = ntohs(nah->subtype);
7061 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
7063 return OFPERR_OFPBAC_BAD_VENDOR;
7067 hdrs.type = ntohs(oah->type);
7070 hdrs.ofp_version = ofp_version;
7071 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
7072 ofpact_decode_hmap()) {
7073 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
7080 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7081 : OFPERR_OFPBAC_BAD_TYPE);
7085 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7086 enum ofp_raw_action_type *raw, uint64_t *arg)
7088 const struct ofp_action_header *oah = buf->data;
7089 const struct ofpact_raw_instance *action;
7090 unsigned int length;
7094 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
7099 if (action->deprecation) {
7100 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
7101 action->name, ofputil_version_to_string(ofp_version),
7102 action->deprecation);
7105 length = ntohs(oah->len);
7106 if (length > buf->size) {
7107 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
7108 "length %"PRIu32, action->name, length, buf->size);
7109 return OFPERR_OFPBAC_BAD_LEN;
7111 if (length < action->min_length || length > action->max_length) {
7112 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
7113 "[%hu,%hu]", action->name, length,
7114 action->min_length, action->max_length);
7115 return OFPERR_OFPBAC_BAD_LEN;
7118 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
7119 "of 8", action->name, length);
7120 return OFPERR_OFPBAC_BAD_LEN;
7125 if (action->arg_len) {
7129 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
7130 for (i = 0; i < action->arg_len; i++) {
7131 *arg = (*arg << 8) | p[i];
7135 ofpbuf_pull(buf, length);
7140 static const struct ofpact_raw_instance *
7141 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
7143 const struct ofpact_raw_instance *inst;
7145 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
7146 ofpact_encode_hmap()) {
7147 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
7155 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7156 enum ofp_raw_action_type raw, uint64_t arg)
7158 const struct ofpact_raw_instance *inst;
7159 struct ofp_action_header *oah;
7160 const struct ofpact_hdrs *hdrs;
7162 inst = ofpact_raw_lookup(ofp_version, raw);
7165 oah = ofpbuf_put_zeros(buf, inst->min_length);
7166 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
7167 oah->len = htons(inst->min_length);
7168 oah->vendor = htonl(hdrs->vendor);
7170 switch (hdrs->vendor) {
7175 case ONF_VENDOR_ID: {
7176 struct ext_action_header *nah = (struct ext_action_header *) oah;
7177 nah->subtype = htons(hdrs->type);
7185 if (inst->arg_len) {
7186 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
7189 for (i = 0; i < inst->arg_len; i++) {
7201 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
7203 struct ofp_action_header *oah;
7205 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs, 8));
7207 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
7208 oah->len = htons(openflow->size - start_ofs);