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 <netinet/in.h>
20 #include "ofp-actions.h"
22 #include "byte-order.h"
25 #include "dynamic-string.h"
28 #include "meta-flow.h"
29 #include "multipath.h"
31 #include "odp-netlink.h"
32 #include "ofp-parse.h"
35 #include "unaligned.h"
37 #include "openvswitch/vlog.h"
39 VLOG_DEFINE_THIS_MODULE(ofp_actions);
41 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
43 struct ofp_action_header;
45 /* Raw identifiers for OpenFlow actions.
47 * Decoding and encoding OpenFlow actions across multiple versions is difficult
48 * to do in a clean, consistent way. This enumeration lays out all of the
49 * forms of actions that Open vSwitch supports.
51 * The comments here must follow a stylized form because the
52 * "extract-ofp-actions" program parses them at build time to generate data
55 * - The first part of each comment specifies the vendor, OpenFlow versions,
56 * and type for each protocol that supports the action:
58 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
59 * extension actions. (Support for other vendors can be added, but
60 * it can't be done just based on a vendor ID definition alone
61 * because OpenFlow doesn't define a standard way to specify a
62 * subtype for vendor actions, so other vendors might do it different
65 * # The version can specify a specific OpenFlow version, a version
66 * range delimited by "-", or an open-ended range with "+".
68 * # The type, in parentheses, is the action type number (for standard
69 * OpenFlow actions) or subtype (for vendor extension actions).
71 * # Optionally one may add "is deprecated" followed by a
72 * human-readable reason in parentheses (which will be used in log
73 * messages), if a particular action should no longer be used.
75 * Multiple such specifications may be separated by commas.
77 * - The second part describes the action's wire format. It may be:
79 * # "struct <name>": The struct fully specifies the wire format. The
80 * action is exactly the size of the struct. (Thus, the struct must
81 * be an exact multiple of 8 bytes in size.)
83 * # "struct <name>, ...": The struct specifies the beginning of the
84 * wire format. An instance of the action is either the struct's
85 * exact size, or a multiple of 8 bytes longer.
87 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
88 * vendor extension) header, followed by 0 or more pad bytes to align
89 * to a multiple of <N> bits, followed by an argument of the given
90 * type, followed by 0 or more pad bytes to bring the total action up
91 * to a multiple of 8 bytes.
93 * # "void": The action is just a (standard or vendor extension)
96 * - Optional additional text enclosed in square brackets is commentary for
99 enum ofp_raw_action_type {
100 /* ## ----------------- ## */
101 /* ## Standard actions. ## */
102 /* ## ----------------- ## */
104 /* OF1.0(0): struct ofp10_action_output. */
106 /* OF1.1+(0): struct ofp11_action_output. */
109 /* OF1.0(1): uint16_t. */
110 OFPAT_RAW10_SET_VLAN_VID,
111 /* OF1.0(2): uint8_t. */
112 OFPAT_RAW10_SET_VLAN_PCP,
114 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
116 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
117 * modification actions: the 1.0 versions push a VLAN header if none is
118 * present, but the 1.1 versions do not. That is the only reason that we
119 * distinguish their raw action types.] */
120 OFPAT_RAW11_SET_VLAN_VID,
121 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
122 OFPAT_RAW11_SET_VLAN_PCP,
124 /* OF1.1+(17): ovs_be16.
126 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
128 OFPAT_RAW11_PUSH_VLAN,
130 /* OF1.0(3): void. */
131 OFPAT_RAW10_STRIP_VLAN,
132 /* OF1.1+(18): void. */
133 OFPAT_RAW11_POP_VLAN,
135 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
136 * ofp_action_dl_addr. */
137 OFPAT_RAW_SET_DL_SRC,
139 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
140 * ofp_action_dl_addr. */
141 OFPAT_RAW_SET_DL_DST,
143 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
145 OFPAT_RAW_SET_NW_SRC,
147 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
149 OFPAT_RAW_SET_NW_DST,
151 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
152 OFPAT_RAW_SET_NW_TOS,
154 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
155 OFPAT_RAW11_SET_NW_ECN,
157 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
159 OFPAT_RAW_SET_TP_SRC,
161 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
163 OFPAT_RAW_SET_TP_DST,
165 /* OF1.0(11): struct ofp10_action_enqueue. */
168 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
170 OFPAT_RAW_SET_MPLS_LABEL,
172 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
174 OFPAT_RAW_SET_MPLS_TC,
176 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
178 OFPAT_RAW_SET_MPLS_TTL,
180 /* NX1.0(26), OF1.1+(16): void. */
181 OFPAT_RAW_DEC_MPLS_TTL,
183 /* NX1.0(23), OF1.1+(19): ovs_be16.
185 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
188 /* NX1.0(24), OF1.1+(20): ovs_be16.
190 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
191 * ETH_TYPE_MPLS otherwise, not the label.] */
194 /* NX1.0(4), OF1.1+(21): uint32_t. */
197 /* OF1.1+(22): uint32_t. */
200 /* OF1.1+(23): uint8_t. */
201 OFPAT_RAW11_SET_NW_TTL,
203 /* NX1.0(18), OF1.1+(24): void. */
204 OFPAT_RAW_DEC_NW_TTL,
205 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
206 NXAST_RAW_DEC_TTL_CNT_IDS,
208 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
209 OFPAT_RAW12_SET_FIELD,
210 /* OF1.5+(25): struct ofp12_action_set_field, ... */
211 OFPAT_RAW15_SET_FIELD,
212 /* NX1.0-1.4(7): struct nx_action_reg_load.
214 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
215 * we drop reg_load.] */
217 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
219 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
220 * we drop reg_load2.] */
223 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
224 OFPAT_RAW15_COPY_FIELD,
225 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
226 ONFACT_RAW13_COPY_FIELD,
227 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
230 /* ## ------------------------- ## */
231 /* ## Nicira extension actions. ## */
232 /* ## ------------------------- ## */
234 /* Actions similar to standard actions are listed with the standard actions. */
236 /* NX1.0+(1): uint16_t. */
238 /* NX1.0+(14): struct nx_action_resubmit. */
239 NXAST_RAW_RESUBMIT_TABLE,
241 /* NX1.0+(2): uint32_t. */
242 NXAST_RAW_SET_TUNNEL,
243 /* NX1.0+(9): uint64_t. */
244 NXAST_RAW_SET_TUNNEL64,
246 /* NX1.0+(5): void. */
249 /* NX1.0+(8): struct nx_action_note, ... */
252 /* NX1.0+(10): struct nx_action_multipath. */
255 /* NX1.0+(12): struct nx_action_bundle, ... */
257 /* NX1.0+(13): struct nx_action_bundle, ... */
258 NXAST_RAW_BUNDLE_LOAD,
260 /* NX1.0+(15): struct nx_action_output_reg. */
261 NXAST_RAW_OUTPUT_REG,
262 /* NX1.0+(32): struct nx_action_output_reg2. */
263 NXAST_RAW_OUTPUT_REG2,
265 /* NX1.0+(16): struct nx_action_learn, ... */
268 /* NX1.0+(17): void. */
271 /* NX1.0+(19): struct nx_action_fin_timeout. */
272 NXAST_RAW_FIN_TIMEOUT,
274 /* NX1.0+(20): struct nx_action_controller. */
275 NXAST_RAW_CONTROLLER,
277 /* NX1.0+(22): struct nx_action_write_metadata. */
278 NXAST_RAW_WRITE_METADATA,
280 /* NX1.0+(27): struct nx_action_stack. */
281 NXAST_RAW_STACK_PUSH,
283 /* NX1.0+(28): struct nx_action_stack. */
286 /* NX1.0+(29): struct nx_action_sample. */
289 /* NX1.0+(34): struct nx_action_conjunction. */
290 NXAST_RAW_CONJUNCTION,
292 /* NX1.0+(35): struct nx_action_conntrack, ... */
295 /* NX1.0+(36): struct nx_action_nat, ... */
298 /* ## ------------------ ## */
299 /* ## Debugging actions. ## */
300 /* ## ------------------ ## */
302 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
303 /* accepts them only if started with --enable-dummy. */
305 /* NX1.0+(255): void. */
306 NXAST_RAW_DEBUG_RECIRC,
309 /* OpenFlow actions are always a multiple of 8 bytes in length. */
310 #define OFP_ACTION_ALIGN 8
312 /* Define a few functions for working with instructions. */
313 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
314 static inline const struct STRUCT * OVS_UNUSED \
315 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
317 ovs_assert(inst->type == htons(ENUM)); \
318 return ALIGNED_CAST(struct STRUCT *, inst); \
321 static inline void OVS_UNUSED \
322 instruction_init_##ENUM(struct STRUCT *s) \
324 memset(s, 0, sizeof *s); \
325 s->type = htons(ENUM); \
326 s->len = htons(sizeof *s); \
329 static inline struct STRUCT * OVS_UNUSED \
330 instruction_put_##ENUM(struct ofpbuf *buf) \
332 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
333 instruction_init_##ENUM(s); \
339 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
341 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
343 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
344 uint32_t allowed_ovsinsts,
345 enum ofpact_type outer_action);
347 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
348 enum mf_field_id, uint64_t value);
350 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
351 enum ofp_raw_action_type *, uint64_t *arg);
352 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
353 enum ofp_raw_action_type, uint64_t arg);
355 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
356 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
357 bool allow_instructions, enum ofpact_type outer_action);
358 static enum ofperr ofpacts_pull_openflow_actions__(
359 struct ofpbuf *openflow, unsigned int actions_len,
360 enum ofp_version version, uint32_t allowed_ovsinsts,
361 struct ofpbuf *ofpacts, enum ofpact_type outer_action);
362 static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
363 const char *s_, struct ofpbuf *ofpacts,
364 enum ofputil_protocol *usable_protocols,
365 bool allow_instructions, enum ofpact_type outer_action);
367 /* Pull off existing actions or instructions. Used by nesting actions to keep
368 * ofpacts_parse() oblivious of actions nesting.
370 * Push the actions back on after nested parsing, e.g.:
372 * size_t ofs = ofpacts_pull(ofpacts);
373 * ...nested parsing...
374 * ofpbuf_push_uninit(ofpacts, ofs);
377 ofpacts_pull(struct ofpbuf *ofpacts)
383 ofpbuf_pull(ofpacts, ofs);
388 #include "ofp-actions.inc1"
390 /* Output actions. */
392 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
393 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
394 * number of bytes to send. A 'max_len' of zero means no bytes of the
395 * packet should be sent. */
396 struct ofp10_action_output {
397 ovs_be16 type; /* OFPAT10_OUTPUT. */
398 ovs_be16 len; /* Length is 8. */
399 ovs_be16 port; /* Output port. */
400 ovs_be16 max_len; /* Max length to send to controller. */
402 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
404 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
405 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
406 * number of bytes to send. A 'max_len' of zero means no bytes of the
407 * packet should be sent.*/
408 struct ofp11_action_output {
409 ovs_be16 type; /* OFPAT11_OUTPUT. */
410 ovs_be16 len; /* Length is 16. */
411 ovs_be32 port; /* Output port. */
412 ovs_be16 max_len; /* Max length to send to controller. */
413 uint8_t pad[6]; /* Pad to 64 bits. */
415 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
418 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
419 enum ofp_version ofp_version OVS_UNUSED,
422 struct ofpact_output *output;
424 output = ofpact_put_OUTPUT(out);
425 output->port = u16_to_ofp(ntohs(oao->port));
426 output->max_len = ntohs(oao->max_len);
428 return ofpact_check_output_port(output->port, OFPP_MAX);
432 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
433 enum ofp_version ofp_version OVS_UNUSED,
436 struct ofpact_output *output;
439 output = ofpact_put_OUTPUT(out);
440 output->max_len = ntohs(oao->max_len);
442 error = ofputil_port_from_ofp11(oao->port, &output->port);
447 return ofpact_check_output_port(output->port, OFPP_MAX);
451 encode_OUTPUT(const struct ofpact_output *output,
452 enum ofp_version ofp_version, struct ofpbuf *out)
454 if (ofp_version == OFP10_VERSION) {
455 struct ofp10_action_output *oao;
457 oao = put_OFPAT10_OUTPUT(out);
458 oao->port = htons(ofp_to_u16(output->port));
459 oao->max_len = htons(output->max_len);
461 struct ofp11_action_output *oao;
463 oao = put_OFPAT11_OUTPUT(out);
464 oao->port = ofputil_port_to_ofp11(output->port);
465 oao->max_len = htons(output->max_len);
469 static char * OVS_WARN_UNUSED_RESULT
470 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
471 enum ofputil_protocol *usable_protocols OVS_UNUSED)
473 if (strchr(arg, '[')) {
474 struct ofpact_output_reg *output_reg;
476 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
477 output_reg->max_len = UINT16_MAX;
478 return mf_parse_subfield(&output_reg->src, arg);
480 struct ofpact_output *output;
482 output = ofpact_put_OUTPUT(ofpacts);
483 if (!ofputil_port_from_string(arg, &output->port)) {
484 return xasprintf("%s: output to unknown port", arg);
486 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
492 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
494 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
495 ds_put_format(s, "output:%"PRIu16, a->port);
497 ofputil_format_port(a->port, s);
498 if (a->port == OFPP_CONTROLLER) {
499 ds_put_format(s, ":%"PRIu16, a->max_len);
507 decode_OFPAT_RAW11_GROUP(uint32_t group_id,
508 enum ofp_version ofp_version OVS_UNUSED,
511 ofpact_put_GROUP(out)->group_id = group_id;
516 encode_GROUP(const struct ofpact_group *group,
517 enum ofp_version ofp_version, struct ofpbuf *out)
519 if (ofp_version == OFP10_VERSION) {
522 put_OFPAT11_GROUP(out, group->group_id);
526 static char * OVS_WARN_UNUSED_RESULT
527 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
528 enum ofputil_protocol *usable_protocols OVS_UNUSED)
530 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
534 format_GROUP(const struct ofpact_group *a, struct ds *s)
536 ds_put_format(s, "group:%"PRIu32, a->group_id);
539 /* Action structure for NXAST_CONTROLLER.
541 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
542 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
545 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
547 * - 'controller_id': The ID of the controller connection to which the
548 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
549 * sent only to controllers that have the specified controller connection
550 * ID. See "struct nx_controller_id" for more information. */
551 struct nx_action_controller {
552 ovs_be16 type; /* OFPAT_VENDOR. */
553 ovs_be16 len; /* Length is 16. */
554 ovs_be32 vendor; /* NX_VENDOR_ID. */
555 ovs_be16 subtype; /* NXAST_CONTROLLER. */
556 ovs_be16 max_len; /* Maximum length to send to controller. */
557 ovs_be16 controller_id; /* Controller ID to send packet-in. */
558 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
559 uint8_t zero; /* Must be zero. */
561 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
564 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
565 enum ofp_version ofp_version OVS_UNUSED,
568 struct ofpact_controller *oc;
570 oc = ofpact_put_CONTROLLER(out);
571 oc->max_len = ntohs(nac->max_len);
572 oc->controller_id = ntohs(nac->controller_id);
573 oc->reason = nac->reason;
578 encode_CONTROLLER(const struct ofpact_controller *controller,
579 enum ofp_version ofp_version OVS_UNUSED,
582 struct nx_action_controller *nac;
584 nac = put_NXAST_CONTROLLER(out);
585 nac->max_len = htons(controller->max_len);
586 nac->controller_id = htons(controller->controller_id);
587 nac->reason = controller->reason;
590 static char * OVS_WARN_UNUSED_RESULT
591 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
592 enum ofputil_protocol *usable_protocols OVS_UNUSED)
594 enum ofp_packet_in_reason reason = OFPR_ACTION;
595 uint16_t controller_id = 0;
596 uint16_t max_len = UINT16_MAX;
600 } else if (strspn(arg, "0123456789") == strlen(arg)) {
601 char *error = str_to_u16(arg, "max_len", &max_len);
608 while (ofputil_parse_key_value(&arg, &name, &value)) {
609 if (!strcmp(name, "reason")) {
610 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
611 return xasprintf("unknown reason \"%s\"", value);
613 } else if (!strcmp(name, "max_len")) {
614 char *error = str_to_u16(value, "max_len", &max_len);
618 } else if (!strcmp(name, "id")) {
619 char *error = str_to_u16(value, "id", &controller_id);
624 return xasprintf("unknown key \"%s\" parsing controller "
630 if (reason == OFPR_ACTION && controller_id == 0) {
631 struct ofpact_output *output;
633 output = ofpact_put_OUTPUT(ofpacts);
634 output->port = OFPP_CONTROLLER;
635 output->max_len = max_len;
637 struct ofpact_controller *controller;
639 controller = ofpact_put_CONTROLLER(ofpacts);
640 controller->max_len = max_len;
641 controller->reason = reason;
642 controller->controller_id = controller_id;
649 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
651 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
652 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
654 enum ofp_packet_in_reason reason = a->reason;
656 ds_put_cstr(s, "controller(");
657 if (reason != OFPR_ACTION) {
658 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
660 ds_put_format(s, "reason=%s,",
661 ofputil_packet_in_reason_to_string(
662 reason, reasonbuf, sizeof reasonbuf));
664 if (a->max_len != UINT16_MAX) {
665 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
667 if (a->controller_id != 0) {
668 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
675 /* Enqueue action. */
676 struct ofp10_action_enqueue {
677 ovs_be16 type; /* OFPAT10_ENQUEUE. */
678 ovs_be16 len; /* Len is 16. */
679 ovs_be16 port; /* Port that queue belongs. Should
680 refer to a valid physical port
681 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
682 uint8_t pad[6]; /* Pad for 64-bit alignment. */
683 ovs_be32 queue_id; /* Where to enqueue the packets. */
685 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
688 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
689 enum ofp_version ofp_version OVS_UNUSED,
692 struct ofpact_enqueue *enqueue;
694 enqueue = ofpact_put_ENQUEUE(out);
695 enqueue->port = u16_to_ofp(ntohs(oae->port));
696 enqueue->queue = ntohl(oae->queue_id);
697 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
698 && enqueue->port != OFPP_IN_PORT
699 && enqueue->port != OFPP_LOCAL) {
700 return OFPERR_OFPBAC_BAD_OUT_PORT;
706 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
707 enum ofp_version ofp_version, struct ofpbuf *out)
709 if (ofp_version == OFP10_VERSION) {
710 struct ofp10_action_enqueue *oae;
712 oae = put_OFPAT10_ENQUEUE(out);
713 oae->port = htons(ofp_to_u16(enqueue->port));
714 oae->queue_id = htonl(enqueue->queue);
720 static char * OVS_WARN_UNUSED_RESULT
721 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
722 enum ofputil_protocol *usable_protocols OVS_UNUSED)
725 char *port = strtok_r(arg, ":q,", &sp);
726 char *queue = strtok_r(NULL, "", &sp);
727 struct ofpact_enqueue *enqueue;
729 if (port == NULL || queue == NULL) {
730 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
731 "\"enqueue(PORT,QUEUE)\"");
734 enqueue = ofpact_put_ENQUEUE(ofpacts);
735 if (!ofputil_port_from_string(port, &enqueue->port)) {
736 return xasprintf("%s: enqueue to unknown port", port);
738 return str_to_u32(queue, &enqueue->queue);
742 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
744 ds_put_format(s, "enqueue:");
745 ofputil_format_port(a->port, s);
746 ds_put_format(s, ":%"PRIu32, a->queue);
749 /* Action structure for NXAST_OUTPUT_REG.
751 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
753 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
754 * the NXAST_REG_LOAD action.
756 * The acceptable nxm_header values for 'src' are the same as the acceptable
757 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
759 * The 'max_len' field indicates the number of bytes to send when the chosen
760 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
761 * field of OFPAT_OUTPUT.
763 * The 'zero' field is required to be zeroed for forward compatibility. */
764 struct nx_action_output_reg {
765 ovs_be16 type; /* OFPAT_VENDOR. */
766 ovs_be16 len; /* 24. */
767 ovs_be32 vendor; /* NX_VENDOR_ID. */
768 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
770 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
771 ovs_be32 src; /* Source. */
773 ovs_be16 max_len; /* Max length to send to controller. */
775 uint8_t zero[6]; /* Reserved, must be zero. */
777 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
779 /* Action structure for NXAST_OUTPUT_REG2.
781 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
782 * experimenter OXM as 'src'.
784 struct nx_action_output_reg2 {
785 ovs_be16 type; /* OFPAT_VENDOR. */
786 ovs_be16 len; /* 24. */
787 ovs_be32 vendor; /* NX_VENDOR_ID. */
788 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
790 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
791 ovs_be16 max_len; /* Max length to send to controller. */
794 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
795 * - Enough 0-bytes to pad the action out to 24 bytes. */
798 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
801 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
802 enum ofp_version ofp_version OVS_UNUSED,
805 struct ofpact_output_reg *output_reg;
807 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
808 return OFPERR_OFPBAC_BAD_ARGUMENT;
811 output_reg = ofpact_put_OUTPUT_REG(out);
812 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
813 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
814 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
815 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
816 output_reg->max_len = ntohs(naor->max_len);
818 return mf_check_src(&output_reg->src, NULL);
822 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
823 enum ofp_version ofp_version OVS_UNUSED,
826 struct ofpact_output_reg *output_reg;
830 output_reg = ofpact_put_OUTPUT_REG(out);
831 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
832 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
833 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
834 output_reg->max_len = ntohs(naor->max_len);
836 ofpbuf_use_const(&b, naor, ntohs(naor->len));
837 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
838 error = nx_pull_header(&b, &output_reg->src.field, NULL);
842 if (!is_all_zeros(b.data, b.size)) {
843 return OFPERR_NXBRC_MUST_BE_ZERO;
846 return mf_check_src(&output_reg->src, NULL);
850 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
851 enum ofp_version ofp_version OVS_UNUSED,
854 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
855 * cannot be encoded in the older form, encode it as
856 * NXAST_RAW_OUTPUT_REG2. */
857 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
858 || !mf_nxm_header(output_reg->src.field->id)) {
859 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
860 size_t size = out->size;
862 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
863 output_reg->src.n_bits);
864 naor->max_len = htons(output_reg->max_len);
866 out->size = size - sizeof naor->pad;
867 nx_put_header(out, output_reg->src.field->id, 0, false);
870 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
872 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
873 output_reg->src.n_bits);
874 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
875 naor->max_len = htons(output_reg->max_len);
879 static char * OVS_WARN_UNUSED_RESULT
880 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
881 enum ofputil_protocol *usable_protocols OVS_UNUSED)
883 return parse_OUTPUT(arg, ofpacts, usable_protocols);
887 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
889 ds_put_cstr(s, "output:");
890 mf_format_subfield(&a->src, s);
893 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
895 * The bundle actions choose a slave from a supplied list of options.
896 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
897 * selection to a register.
899 * The list of possible slaves follows the nx_action_bundle structure. The size
900 * of each slave is governed by its type as indicated by the 'slave_type'
901 * parameter. The list of slaves should be padded at its end with zeros to make
902 * the total length of the action a multiple of 8.
904 * Switches infer from the 'slave_type' parameter the size of each slave. All
905 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
906 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
907 * 2 byte width. Switches should reject actions which indicate unknown or
908 * unsupported slave types.
910 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
911 * slave. If the switch does not support the specified 'algorithm' parameter,
912 * it should reject the action.
914 * Several algorithms take into account liveness when selecting slaves. The
915 * liveness of a slave is implementation defined (with one exception), but will
916 * generally take into account things like its carrier status and the results
917 * of any link monitoring protocols which happen to be running on it. In order
918 * to give controllers a place-holder value, the OFPP_NONE port is always
921 * Some slave selection strategies require the use of a hash function, in which
922 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
923 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
924 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
925 * 'basis' parameter is used as a universal hash parameter. Different values
926 * of 'basis' yield different hash results.
928 * The 'zero' parameter at the end of the action structure is reserved for
929 * future use. Switches are required to reject actions which have nonzero
930 * bytes in the 'zero' field.
932 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
933 * should reject actions which have nonzero bytes in either of these fields.
935 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
936 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
937 * similar to those for the NXAST_REG_LOAD action. */
938 struct nx_action_bundle {
939 ovs_be16 type; /* OFPAT_VENDOR. */
940 ovs_be16 len; /* Length including slaves. */
941 ovs_be32 vendor; /* NX_VENDOR_ID. */
942 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
944 /* Slave choice algorithm to apply to hash value. */
945 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
947 /* What fields to hash and how. */
948 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
949 ovs_be16 basis; /* Universal hash parameter. */
951 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
952 ovs_be16 n_slaves; /* Number of slaves. */
954 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
955 ovs_be32 dst; /* Destination. */
957 uint8_t zero[4]; /* Reserved. Must be zero. */
959 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
962 decode_bundle(bool load, const struct nx_action_bundle *nab,
963 struct ofpbuf *ofpacts)
965 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
966 struct ofpact_bundle *bundle;
968 size_t slaves_size, i;
971 bundle = ofpact_put_BUNDLE(ofpacts);
973 bundle->n_slaves = ntohs(nab->n_slaves);
974 bundle->basis = ntohs(nab->basis);
975 bundle->fields = ntohs(nab->fields);
976 bundle->algorithm = ntohs(nab->algorithm);
977 slave_type = ntohl(nab->slave_type);
978 slaves_size = ntohs(nab->len) - sizeof *nab;
980 error = OFPERR_OFPBAC_BAD_ARGUMENT;
981 if (!flow_hash_fields_valid(bundle->fields)) {
982 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
983 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
984 VLOG_WARN_RL(&rl, "too many slaves");
985 } else if (bundle->algorithm != NX_BD_ALG_HRW
986 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
987 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
988 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
989 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
994 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
995 VLOG_WARN_RL(&rl, "reserved field is nonzero");
996 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1000 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
1001 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
1002 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
1004 if (bundle->dst.n_bits < 16) {
1005 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
1007 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1010 if (nab->ofs_nbits || nab->dst) {
1011 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
1012 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1016 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1017 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1018 "allocated for slaves. %"PRIuSIZE" bytes are required "
1019 "for %"PRIu16" slaves.",
1020 load ? "bundle_load" : "bundle", slaves_size,
1021 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1022 error = OFPERR_OFPBAC_BAD_LEN;
1025 for (i = 0; i < bundle->n_slaves; i++) {
1026 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1027 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1030 bundle = ofpacts->header;
1031 ofpact_update_len(ofpacts, &bundle->ofpact);
1034 error = bundle_check(bundle, OFPP_MAX, NULL);
1040 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1041 enum ofp_version ofp_version OVS_UNUSED,
1044 return decode_bundle(false, nab, out);
1048 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1049 enum ofp_version ofp_version OVS_UNUSED,
1052 return decode_bundle(true, nab, out);
1056 encode_BUNDLE(const struct ofpact_bundle *bundle,
1057 enum ofp_version ofp_version OVS_UNUSED,
1060 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1061 struct nx_action_bundle *nab;
1065 nab = (bundle->dst.field
1066 ? put_NXAST_BUNDLE_LOAD(out)
1067 : put_NXAST_BUNDLE(out));
1068 nab->len = htons(ntohs(nab->len) + slaves_len);
1069 nab->algorithm = htons(bundle->algorithm);
1070 nab->fields = htons(bundle->fields);
1071 nab->basis = htons(bundle->basis);
1072 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1073 nab->n_slaves = htons(bundle->n_slaves);
1074 if (bundle->dst.field) {
1075 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1076 bundle->dst.n_bits);
1077 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1080 slaves = ofpbuf_put_zeros(out, slaves_len);
1081 for (i = 0; i < bundle->n_slaves; i++) {
1082 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1086 static char * OVS_WARN_UNUSED_RESULT
1087 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1088 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1090 return bundle_parse(arg, ofpacts);
1093 static char * OVS_WARN_UNUSED_RESULT
1094 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1096 return bundle_parse_load(arg, ofpacts);
1100 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1102 bundle_format(a, s);
1105 /* Set VLAN actions. */
1108 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1111 return OFPERR_OFPBAC_BAD_ARGUMENT;
1113 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1114 vlan_vid->vlan_vid = vid;
1115 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1121 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1122 enum ofp_version ofp_version OVS_UNUSED,
1125 return decode_set_vlan_vid(vid, true, out);
1129 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1130 enum ofp_version ofp_version OVS_UNUSED,
1133 return decode_set_vlan_vid(vid, false, out);
1137 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1138 enum ofp_version ofp_version, struct ofpbuf *out)
1140 uint16_t vid = vlan_vid->vlan_vid;
1142 /* Push a VLAN tag, if none is present and this form of the action calls
1143 * for such a feature. */
1144 if (ofp_version > OFP10_VERSION
1145 && vlan_vid->push_vlan_if_needed
1146 && !vlan_vid->flow_has_vlan) {
1147 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1150 if (ofp_version == OFP10_VERSION) {
1151 put_OFPAT10_SET_VLAN_VID(out, vid);
1152 } else if (ofp_version == OFP11_VERSION) {
1153 put_OFPAT11_SET_VLAN_VID(out, vid);
1155 ofpact_put_set_field(out, ofp_version,
1156 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1160 static char * OVS_WARN_UNUSED_RESULT
1161 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1163 struct ofpact_vlan_vid *vlan_vid;
1167 error = str_to_u16(arg, "VLAN VID", &vid);
1172 if (vid & ~VLAN_VID_MASK) {
1173 return xasprintf("%s: not a valid VLAN VID", arg);
1175 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1176 vlan_vid->vlan_vid = vid;
1177 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1181 static char * OVS_WARN_UNUSED_RESULT
1182 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1183 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1185 return parse_set_vlan_vid(arg, ofpacts, false);
1189 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1191 ds_put_format(s, "%s:%"PRIu16,
1192 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1196 /* Set PCP actions. */
1199 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1202 return OFPERR_OFPBAC_BAD_ARGUMENT;
1204 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1205 vlan_pcp->vlan_pcp = pcp;
1206 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1212 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1213 enum ofp_version ofp_version OVS_UNUSED,
1216 return decode_set_vlan_pcp(pcp, true, out);
1220 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1221 enum ofp_version ofp_version OVS_UNUSED,
1224 return decode_set_vlan_pcp(pcp, false, out);
1228 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1229 enum ofp_version ofp_version, struct ofpbuf *out)
1231 uint8_t pcp = vlan_pcp->vlan_pcp;
1233 /* Push a VLAN tag, if none is present and this form of the action calls
1234 * for such a feature. */
1235 if (ofp_version > OFP10_VERSION
1236 && vlan_pcp->push_vlan_if_needed
1237 && !vlan_pcp->flow_has_vlan) {
1238 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1241 if (ofp_version == OFP10_VERSION) {
1242 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1243 } else if (ofp_version == OFP11_VERSION) {
1244 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1246 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1250 static char * OVS_WARN_UNUSED_RESULT
1251 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1253 struct ofpact_vlan_pcp *vlan_pcp;
1257 error = str_to_u8(arg, "VLAN PCP", &pcp);
1263 return xasprintf("%s: not a valid VLAN PCP", arg);
1265 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1266 vlan_pcp->vlan_pcp = pcp;
1267 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1271 static char * OVS_WARN_UNUSED_RESULT
1272 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1273 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1275 return parse_set_vlan_pcp(arg, ofpacts, false);
1279 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1281 ds_put_format(s, "%s:%"PRIu8,
1282 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1286 /* Strip VLAN actions. */
1289 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1291 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1296 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1298 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1303 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1304 enum ofp_version ofp_version, struct ofpbuf *out)
1306 if (ofp_version == OFP10_VERSION) {
1307 put_OFPAT10_STRIP_VLAN(out);
1309 put_OFPAT11_POP_VLAN(out);
1313 static char * OVS_WARN_UNUSED_RESULT
1314 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1315 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1317 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1321 static char * OVS_WARN_UNUSED_RESULT
1322 parse_pop_vlan(struct ofpbuf *ofpacts)
1324 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1329 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1331 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1336 /* Push VLAN action. */
1339 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1340 enum ofp_version ofp_version OVS_UNUSED,
1343 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1344 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1345 return OFPERR_OFPBAC_BAD_ARGUMENT;
1347 ofpact_put_PUSH_VLAN(out);
1352 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1353 enum ofp_version ofp_version, struct ofpbuf *out)
1355 if (ofp_version == OFP10_VERSION) {
1356 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1357 * follow this action. */
1359 /* XXX ETH_TYPE_VLAN_8021AD case */
1360 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1364 static char * OVS_WARN_UNUSED_RESULT
1365 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1366 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1371 *usable_protocols &= OFPUTIL_P_OF11_UP;
1372 error = str_to_u16(arg, "ethertype", ðertype);
1377 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1378 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1379 return xasprintf("%s: not a valid VLAN ethertype", arg);
1382 ofpact_put_PUSH_VLAN(ofpacts);
1387 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1389 /* XXX 802.1AD case*/
1390 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1393 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1394 struct ofp_action_dl_addr {
1395 ovs_be16 type; /* Type. */
1396 ovs_be16 len; /* Length is 16. */
1397 struct eth_addr dl_addr; /* Ethernet address. */
1400 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1403 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1404 enum ofp_version ofp_version OVS_UNUSED,
1407 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1412 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1413 enum ofp_version ofp_version OVS_UNUSED,
1416 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1421 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1422 enum ofp_raw_action_type raw, enum mf_field_id field,
1425 if (ofp_version < OFP12_VERSION) {
1426 struct ofp_action_dl_addr *oada;
1428 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1429 oada->dl_addr = mac->mac;
1431 ofpact_put_set_field(out, ofp_version, field,
1432 eth_addr_to_uint64(mac->mac));
1437 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1440 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1446 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1447 enum ofp_version ofp_version,
1450 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1455 static char * OVS_WARN_UNUSED_RESULT
1456 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1457 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1459 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1462 static char * OVS_WARN_UNUSED_RESULT
1463 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1464 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1466 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1470 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1472 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1476 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1478 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1481 /* Set IPv4 address actions. */
1484 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1485 enum ofp_version ofp_version OVS_UNUSED,
1488 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1493 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1494 enum ofp_version ofp_version OVS_UNUSED,
1497 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1502 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1503 enum ofp_version ofp_version,
1504 enum ofp_raw_action_type raw, enum mf_field_id field,
1507 ovs_be32 addr = ipv4->ipv4;
1508 if (ofp_version < OFP12_VERSION) {
1509 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1511 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1516 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1517 enum ofp_version ofp_version, struct ofpbuf *out)
1519 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1524 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1525 enum ofp_version ofp_version, struct ofpbuf *out)
1527 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1531 static char * OVS_WARN_UNUSED_RESULT
1532 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1533 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1535 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1538 static char * OVS_WARN_UNUSED_RESULT
1539 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1540 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1542 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1546 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1548 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1552 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1554 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1557 /* Set IPv4/v6 TOS actions. */
1560 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1561 enum ofp_version ofp_version OVS_UNUSED,
1564 if (dscp & ~IP_DSCP_MASK) {
1565 return OFPERR_OFPBAC_BAD_ARGUMENT;
1567 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1573 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1574 enum ofp_version ofp_version, struct ofpbuf *out)
1576 if (ofp_version < OFP12_VERSION) {
1577 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1579 ofpact_put_set_field(out, ofp_version,
1580 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1584 static char * OVS_WARN_UNUSED_RESULT
1585 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1586 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1591 error = str_to_u8(arg, "TOS", &tos);
1596 if (tos & ~IP_DSCP_MASK) {
1597 return xasprintf("%s: not a valid TOS", arg);
1599 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1604 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1606 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1609 /* Set IPv4/v6 ECN actions. */
1612 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1613 enum ofp_version ofp_version OVS_UNUSED,
1616 if (ecn & ~IP_ECN_MASK) {
1617 return OFPERR_OFPBAC_BAD_ARGUMENT;
1619 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1625 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1626 enum ofp_version ofp_version, struct ofpbuf *out)
1628 uint8_t ecn = ip_ecn->ecn;
1629 if (ofp_version == OFP10_VERSION) {
1631 } else if (ofp_version == OFP11_VERSION) {
1632 put_OFPAT11_SET_NW_ECN(out, ecn);
1634 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1638 static char * OVS_WARN_UNUSED_RESULT
1639 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1640 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1645 error = str_to_u8(arg, "ECN", &ecn);
1650 if (ecn & ~IP_ECN_MASK) {
1651 return xasprintf("%s: not a valid ECN", arg);
1653 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1658 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1660 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1663 /* Set IPv4/v6 TTL actions. */
1666 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1667 enum ofp_version ofp_version OVS_UNUSED,
1670 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1675 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1676 enum ofp_version ofp_version, struct ofpbuf *out)
1678 if (ofp_version >= OFP11_VERSION) {
1679 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1685 static char * OVS_WARN_UNUSED_RESULT
1686 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1687 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1692 error = str_to_u8(arg, "TTL", &ttl);
1697 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1702 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1704 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1707 /* Set TCP/UDP/SCTP port actions. */
1710 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1711 enum ofp_version ofp_version OVS_UNUSED,
1714 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1719 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1720 enum ofp_version ofp_version OVS_UNUSED,
1723 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1728 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1729 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1730 enum mf_field_id field, struct ofpbuf *out)
1732 uint16_t port = l4_port->port;
1734 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1735 ofpact_put_set_field(out, ofp_version, field, port);
1737 ofpact_put_raw(out, ofp_version, raw, port);
1742 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1743 enum ofp_version ofp_version, struct ofpbuf *out)
1745 uint8_t proto = l4_port->flow_ip_proto;
1746 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1747 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1748 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1751 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1755 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1756 enum ofp_version ofp_version,
1759 uint8_t proto = l4_port->flow_ip_proto;
1760 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1761 : proto == IPPROTO_UDP ? MFF_UDP_DST
1762 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1765 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1768 static char * OVS_WARN_UNUSED_RESULT
1769 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1770 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1772 return str_to_u16(arg, "source port",
1773 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1776 static char * OVS_WARN_UNUSED_RESULT
1777 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1778 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1780 return str_to_u16(arg, "destination port",
1781 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1785 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1787 ds_put_format(s, "mod_tp_src:%d", a->port);
1791 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1793 ds_put_format(s, "mod_tp_dst:%d", a->port);
1796 /* Action structure for OFPAT_COPY_FIELD. */
1797 struct ofp15_action_copy_field {
1798 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1799 ovs_be16 len; /* Length is padded to 64 bits. */
1800 ovs_be16 n_bits; /* Number of bits to copy. */
1801 ovs_be16 src_offset; /* Starting bit offset in source. */
1802 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1805 * - OXM header for source field.
1806 * - OXM header for destination field.
1807 * - Padding with 0-bytes to a multiple of 8 bytes.
1808 * The "pad2" member is the beginning of the above. */
1811 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1813 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1814 struct onf_action_copy_field {
1815 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
1816 ovs_be16 len; /* Length is padded to 64 bits. */
1817 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
1818 ovs_be16 exp_type; /* 3200. */
1819 uint8_t pad[2]; /* Not used. */
1820 ovs_be16 n_bits; /* Number of bits to copy. */
1821 ovs_be16 src_offset; /* Starting bit offset in source. */
1822 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1823 uint8_t pad2[2]; /* Not used. */
1825 * - OXM header for source field.
1826 * - OXM header for destination field.
1827 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1828 * The "pad3" member is the beginning of the above. */
1829 uint8_t pad3[4]; /* Not used. */
1831 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1833 /* Action structure for NXAST_REG_MOVE.
1835 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1836 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1837 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1838 * the next most significant bit, and so on.
1840 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1841 * sense to use nxm_hasmask=1 because the action does not do any kind of
1842 * matching; it uses the actual value of a field.)
1844 * The following nxm_header values are potentially acceptable as 'src':
1859 * - NXM_OF_ICMP_TYPE
1860 * - NXM_OF_ICMP_CODE
1867 * - NXM_NX_ICMPV6_TYPE
1868 * - NXM_NX_ICMPV6_CODE
1871 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1873 * - NXM_NX_TUN_IPV4_SRC
1874 * - NXM_NX_TUN_IPV4_DST
1876 * The following nxm_header values are potentially acceptable as 'dst':
1887 * - NXM_OF_ICMP_TYPE
1888 * - NXM_OF_ICMP_CODE
1889 * - NXM_NX_ICMPV6_TYPE
1890 * - NXM_NX_ICMPV6_CODE
1896 * Modifying any of the above fields changes the corresponding packet
1901 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1905 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1906 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1907 * header (if any), ignoring the other bits. Setting a value with CFI=1
1908 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1909 * to the field's new value (with the CFI bit masked out).
1911 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1912 * any of these values modifies the corresponding tunnel header field used
1913 * for the packet's next tunnel encapsulation, if allowed by the
1914 * configuration of the output tunnel port.
1916 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1917 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1918 * used only if the flow's nx_match includes an nxm_entry that specifies
1919 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1921 * The switch will reject actions for which src_ofs+n_bits is greater than the
1922 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1923 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1925 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1926 * behaves as if 'src' were copied out to a temporary buffer, then the
1927 * temporary buffer copied to 'dst'.
1929 struct nx_action_reg_move {
1930 ovs_be16 type; /* OFPAT_VENDOR. */
1931 ovs_be16 len; /* Length is 24. */
1932 ovs_be32 vendor; /* NX_VENDOR_ID. */
1933 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1934 ovs_be16 n_bits; /* Number of bits. */
1935 ovs_be16 src_ofs; /* Starting bit offset in source. */
1936 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1938 * - OXM/NXM header for source field (4 or 8 bytes).
1939 * - OXM/NXM header for destination field (4 or 8 bytes).
1940 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
1942 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
1945 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
1946 const void *action, ovs_be16 action_len, size_t oxm_offset,
1947 struct ofpbuf *ofpacts)
1949 struct ofpact_reg_move *move;
1953 move = ofpact_put_REG_MOVE(ofpacts);
1954 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
1955 move->src.ofs = ntohs(src_offset);
1956 move->src.n_bits = ntohs(n_bits);
1957 move->dst.ofs = ntohs(dst_offset);
1958 move->dst.n_bits = ntohs(n_bits);
1960 ofpbuf_use_const(&b, action, ntohs(action_len));
1961 ofpbuf_pull(&b, oxm_offset);
1962 error = nx_pull_header(&b, &move->src.field, NULL);
1966 error = nx_pull_header(&b, &move->dst.field, NULL);
1971 if (!is_all_zeros(b.data, b.size)) {
1972 return OFPERR_NXBRC_MUST_BE_ZERO;
1975 return nxm_reg_move_check(move, NULL);
1979 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
1980 enum ofp_version ofp_version OVS_UNUSED,
1981 struct ofpbuf *ofpacts)
1983 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1984 oacf->n_bits, oacf, oacf->len,
1985 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
1989 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
1990 enum ofp_version ofp_version OVS_UNUSED,
1991 struct ofpbuf *ofpacts)
1993 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1994 oacf->n_bits, oacf, oacf->len,
1995 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
1999 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
2000 enum ofp_version ofp_version OVS_UNUSED,
2001 struct ofpbuf *ofpacts)
2003 struct ofpact_reg_move *move;
2007 move = ofpact_put_REG_MOVE(ofpacts);
2008 move->ofpact.raw = NXAST_RAW_REG_MOVE;
2009 move->src.ofs = ntohs(narm->src_ofs);
2010 move->src.n_bits = ntohs(narm->n_bits);
2011 move->dst.ofs = ntohs(narm->dst_ofs);
2012 move->dst.n_bits = ntohs(narm->n_bits);
2014 ofpbuf_use_const(&b, narm, ntohs(narm->len));
2015 ofpbuf_pull(&b, sizeof *narm);
2016 error = nx_pull_header(&b, &move->src.field, NULL);
2020 error = nx_pull_header(&b, &move->dst.field, NULL);
2024 if (!is_all_zeros(b.data, b.size)) {
2025 return OFPERR_NXBRC_MUST_BE_ZERO;
2028 return nxm_reg_move_check(move, NULL);
2032 encode_REG_MOVE(const struct ofpact_reg_move *move,
2033 enum ofp_version ofp_version, struct ofpbuf *out)
2035 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2036 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2037 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2038 * probe for support. Until we have that ability, we currently prefer
2039 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2041 size_t start_ofs = out->size;
2042 if (ofp_version >= OFP15_VERSION) {
2043 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2044 copy->n_bits = htons(move->dst.n_bits);
2045 copy->src_offset = htons(move->src.ofs);
2046 copy->dst_offset = htons(move->dst.ofs);
2047 out->size = out->size - sizeof copy->pad2;
2048 nx_put_header(out, move->src.field->id, ofp_version, false);
2049 nx_put_header(out, move->dst.field->id, ofp_version, false);
2050 } else if (ofp_version == OFP13_VERSION
2051 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2052 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2053 copy->n_bits = htons(move->dst.n_bits);
2054 copy->src_offset = htons(move->src.ofs);
2055 copy->dst_offset = htons(move->dst.ofs);
2056 out->size = out->size - sizeof copy->pad3;
2057 nx_put_header(out, move->src.field->id, ofp_version, false);
2058 nx_put_header(out, move->dst.field->id, ofp_version, false);
2060 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2061 narm->n_bits = htons(move->dst.n_bits);
2062 narm->src_ofs = htons(move->src.ofs);
2063 narm->dst_ofs = htons(move->dst.ofs);
2064 nx_put_header(out, move->src.field->id, 0, false);
2065 nx_put_header(out, move->dst.field->id, 0, false);
2067 pad_ofpat(out, start_ofs);
2070 static char * OVS_WARN_UNUSED_RESULT
2071 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2072 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2074 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2075 const char *full_arg = arg;
2078 error = mf_parse_subfield__(&move->src, &arg);
2082 if (strncmp(arg, "->", 2)) {
2083 return xasprintf("%s: missing `->' following source", full_arg);
2086 error = mf_parse_subfield(&move->dst, arg);
2091 if (move->src.n_bits != move->dst.n_bits) {
2092 return xasprintf("%s: source field is %d bits wide but destination is "
2093 "%d bits wide", full_arg,
2094 move->src.n_bits, move->dst.n_bits);
2100 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2102 nxm_format_reg_move(a, s);
2105 /* Action structure for OFPAT12_SET_FIELD. */
2106 struct ofp12_action_set_field {
2107 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2108 ovs_be16 len; /* Length is padded to 64 bits. */
2111 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2112 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2114 * The "pad" member is the beginning of the above. */
2117 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2119 /* Action structure for NXAST_REG_LOAD.
2121 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2122 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2123 * starts at 0 for the least-significant bit, 1 for the next most significant
2126 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2127 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2130 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2131 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2132 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2133 * also stored as one less than its true value:
2136 * +------------------------------+------------------+
2137 * | ofs | n_bits - 1 |
2138 * +------------------------------+------------------+
2140 * The switch will reject actions for which ofs+n_bits is greater than the
2141 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2142 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2143 * OFPBAC_BAD_ARGUMENT.
2145 struct nx_action_reg_load {
2146 ovs_be16 type; /* OFPAT_VENDOR. */
2147 ovs_be16 len; /* Length is 24. */
2148 ovs_be32 vendor; /* NX_VENDOR_ID. */
2149 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2150 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2151 ovs_be32 dst; /* Destination register. */
2152 ovs_be64 value; /* Immediate value. */
2154 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2156 /* Action structure for NXAST_REG_LOAD2.
2158 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2159 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2160 * OXM experimenter fields. */
2161 struct nx_action_reg_load2 {
2162 ovs_be16 type; /* OFPAT_VENDOR. */
2163 ovs_be16 len; /* At least 16. */
2164 ovs_be32 vendor; /* NX_VENDOR_ID. */
2165 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2168 * - An NXM/OXM header, value, and optionally a mask.
2169 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2171 * The "pad" member is the beginning of the above. */
2174 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2177 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2178 bool may_mask, struct ofpbuf *ofpacts)
2180 struct ofpact_set_field *sf;
2184 sf = ofpact_put_SET_FIELD(ofpacts);
2186 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2187 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2188 error = nx_pull_entry(&b, &sf->field, &sf->value,
2189 may_mask ? &sf->mask : NULL);
2191 return (error == OFPERR_OFPBMC_BAD_MASK
2192 ? OFPERR_OFPBAC_BAD_SET_MASK
2196 memset(&sf->mask, 0xff, sf->field->n_bytes);
2199 if (!is_all_zeros(b.data, b.size)) {
2200 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2203 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2205 if (sf->field->id == MFF_IN_PORT_OXM) {
2206 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2209 /* oxm_length is now validated to be compatible with mf_value. */
2210 if (!sf->field->writable) {
2211 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2213 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2216 /* The value must be valid for match. OpenFlow 1.5 also says,
2217 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2218 * a 1-bit in oxm_value and in oxm_mask." */
2219 if (!mf_is_value_valid(sf->field, &sf->value)
2220 || (sf->field->id == MFF_VLAN_VID
2221 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2222 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2223 struct ds ds = DS_EMPTY_INITIALIZER;
2224 mf_format(sf->field, &sf->value, NULL, &ds);
2225 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2226 sf->field->name, ds_cstr(&ds));
2229 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2235 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2236 enum ofp_version ofp_version OVS_UNUSED,
2237 struct ofpbuf *ofpacts)
2239 return decode_ofpat_set_field(oasf, false, ofpacts);
2243 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2244 enum ofp_version ofp_version OVS_UNUSED,
2245 struct ofpbuf *ofpacts)
2247 return decode_ofpat_set_field(oasf, true, ofpacts);
2251 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2252 enum ofp_version ofp_version OVS_UNUSED,
2255 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2256 struct mf_subfield dst;
2259 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2261 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2262 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2263 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2264 error = mf_check_dst(&dst, NULL);
2269 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2271 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2272 return OFPERR_OFPBAC_BAD_ARGUMENT;
2275 sf->field = dst.field;
2276 bitwise_put(ntohll(narl->value),
2277 &sf->value, dst.field->n_bytes, dst.ofs,
2279 bitwise_put(UINT64_MAX,
2280 &sf->mask, dst.field->n_bytes, dst.ofs,
2287 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2288 enum ofp_version ofp_version OVS_UNUSED,
2291 struct ofpact_set_field *sf;
2295 sf = ofpact_put_SET_FIELD(out);
2296 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2298 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2299 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2300 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2304 if (!is_all_zeros(b.data, b.size)) {
2305 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2308 if (!sf->field->writable) {
2309 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2311 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2317 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2318 enum mf_field_id field, uint64_t value_)
2320 struct ofp12_action_set_field *oasf OVS_UNUSED;
2321 int n_bytes = mf_from_id(field)->n_bytes;
2322 size_t start_ofs = openflow->size;
2323 union mf_value value;
2325 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2327 oasf = put_OFPAT12_SET_FIELD(openflow);
2328 openflow->size = openflow->size - sizeof oasf->pad;
2329 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2330 pad_ofpat(openflow, start_ofs);
2334 next_load_segment(const struct ofpact_set_field *sf,
2335 struct mf_subfield *dst, uint64_t *value)
2337 int n_bits = sf->field->n_bits;
2338 int n_bytes = sf->field->n_bytes;
2339 int start = dst->ofs + dst->n_bits;
2341 if (start < n_bits) {
2342 dst->field = sf->field;
2343 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2344 if (dst->ofs < n_bits) {
2345 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2346 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2347 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2354 /* Convert 'sf' to a series of REG_LOADs. */
2356 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2358 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2359 * experimenter OXM or is variable length (or if it came in as
2360 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2361 * NXAST_REG_LOAD, which is backward compatible. */
2362 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2363 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2364 struct nx_action_reg_load2 *narl OVS_UNUSED;
2365 size_t start_ofs = openflow->size;
2367 narl = put_NXAST_REG_LOAD2(openflow);
2368 openflow->size = openflow->size - sizeof narl->pad;
2369 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2370 pad_ofpat(openflow, start_ofs);
2372 struct mf_subfield dst;
2375 dst.ofs = dst.n_bits = 0;
2376 while (next_load_segment(sf, &dst, &value)) {
2377 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2378 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2379 narl->dst = htonl(mf_nxm_header(dst.field->id));
2380 narl->value = htonll(value);
2385 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2386 * actions, if we can, falling back to Nicira extensions if we must.
2388 * We check only meta-flow types that can appear within set field actions and
2389 * that have a mapping to compatible action types. These struct mf_field
2390 * definitions have a defined OXM or NXM header value and specify the field as
2393 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2394 enum ofp_version ofp_version,
2397 switch ((int) sf->field->id) {
2398 case MFF_VLAN_TCI: {
2399 ovs_be16 tci = sf->value.be16;
2400 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2401 uint16_t vid = vlan_tci_to_vid(tci);
2402 uint8_t pcp = vlan_tci_to_pcp(tci);
2404 if (ofp_version < OFP11_VERSION) {
2405 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2407 * If CFI=1, Add or modify VLAN VID & PCP.
2408 * If CFI=0, strip VLAN header, if any.
2411 put_OFPAT10_SET_VLAN_VID(out, vid);
2412 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2414 put_OFPAT10_STRIP_VLAN(out);
2417 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2419 * If CFI=1, Add or modify VLAN VID & PCP.
2420 * OpenFlow 1.1 set actions only apply if the packet
2421 * already has VLAN tags. To be sure that is the case
2422 * we have to push a VLAN header. As we do not support
2423 * multiple layers of VLANs, this is a no-op, if a VLAN
2424 * header already exists. This may backfire, however,
2425 * when we start supporting multiple layers of VLANs.
2426 * If CFI=0, strip VLAN header, if any.
2429 /* Push a VLAN tag, if one was not seen at action validation
2431 if (!sf->flow_has_vlan) {
2432 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2434 put_OFPAT11_SET_VLAN_VID(out, vid);
2435 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2437 /* If the flow did not match on vlan, we have no way of
2438 * knowing if the vlan tag exists, so we must POP just to be
2440 put_OFPAT11_POP_VLAN(out);
2446 case MFF_VLAN_VID: {
2447 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2448 if (ofp_version == OFP10_VERSION) {
2449 put_OFPAT10_SET_VLAN_VID(out, vid);
2451 put_OFPAT11_SET_VLAN_VID(out, vid);
2457 if (ofp_version == OFP10_VERSION) {
2458 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2460 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2465 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2469 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2473 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2477 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2481 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2484 case MFF_IP_DSCP_SHIFTED:
2485 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2490 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2495 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2499 set_field_to_nxast(sf, out);
2505 set_field_to_set_field(const struct ofpact_set_field *sf,
2506 enum ofp_version ofp_version, struct ofpbuf *out)
2508 struct ofp12_action_set_field *oasf OVS_UNUSED;
2509 size_t start_ofs = out->size;
2511 oasf = put_OFPAT12_SET_FIELD(out);
2512 out->size = out->size - sizeof oasf->pad;
2513 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2514 pad_ofpat(out, start_ofs);
2518 encode_SET_FIELD(const struct ofpact_set_field *sf,
2519 enum ofp_version ofp_version, struct ofpbuf *out)
2521 if (ofp_version >= OFP15_VERSION) {
2522 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2524 set_field_to_set_field(sf, ofp_version, out);
2525 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2526 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2527 /* It came in as reg_load, send it out the same way. */
2528 set_field_to_nxast(sf, out);
2529 } else if (ofp_version < OFP12_VERSION) {
2530 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2531 set_field_to_legacy_openflow(sf, ofp_version, out);
2532 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2533 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2534 * entire field, so encode it as OFPAT_SET_FIELD. */
2535 set_field_to_set_field(sf, ofp_version, out);
2537 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2538 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2539 * so encode it as reg_load. */
2540 set_field_to_nxast(sf, out);
2544 /* Parses the input argument 'arg' into the key, value, and delimiter
2545 * components that are common across the reg_load and set_field action format.
2547 * With an argument like "1->metadata", sets the following pointers to
2548 * point within 'arg':
2553 * Returns NULL if successful, otherwise a malloc()'d string describing the
2554 * error. The caller is responsible for freeing the returned string. */
2555 static char * OVS_WARN_UNUSED_RESULT
2556 set_field_split_str(char *arg, char **key, char **value, char **delim)
2561 value_end = strstr(arg, "->");
2562 *key = value_end + strlen("->");
2568 return xasprintf("%s: missing `->'", arg);
2570 if (strlen(value_end) <= strlen("->")) {
2571 return xasprintf("%s: missing field name following `->'", arg);
2577 /* Parses a "set_field" action with argument 'arg', appending the parsed
2578 * action to 'ofpacts'.
2580 * Returns NULL if successful, otherwise a malloc()'d string describing the
2581 * error. The caller is responsible for freeing the returned string. */
2582 static char * OVS_WARN_UNUSED_RESULT
2583 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2584 enum ofputil_protocol *usable_protocols)
2586 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2590 const struct mf_field *mf;
2593 error = set_field_split_str(arg, &key, &value, &delim);
2598 mf = mf_from_name(key);
2600 return xasprintf("%s is not a valid OXM field name", key);
2602 if (!mf->writable) {
2603 return xasprintf("%s is read-only", key);
2607 error = mf_parse(mf, value, &sf->value, &sf->mask);
2612 if (!mf_is_value_valid(mf, &sf->value)) {
2613 return xasprintf("%s is not a valid value for field %s", value, key);
2616 *usable_protocols &= mf->usable_protocols_exact;
2620 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2621 * action to 'ofpacts'.
2623 * Returns NULL if successful, otherwise a malloc()'d string describing the
2624 * error. The caller is responsible for freeing the returned string. */
2625 static char * OVS_WARN_UNUSED_RESULT
2626 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2627 enum ofputil_protocol *usable_protocols)
2629 char *copy = xstrdup(arg);
2630 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2635 static char * OVS_WARN_UNUSED_RESULT
2636 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2638 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2639 struct mf_subfield dst;
2640 char *key, *value_str;
2641 union mf_value value;
2644 error = set_field_split_str(arg, &key, &value_str, NULL);
2649 error = mf_parse_subfield(&dst, key);
2654 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2656 return xasprintf("%s: cannot parse integer value", arg);
2659 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2660 dst.field->n_bytes * 8 - dst.n_bits)) {
2664 mf_format(dst.field, &value, NULL, &ds);
2665 error = xasprintf("%s: value %s does not fit into %d bits",
2666 arg, ds_cstr(&ds), dst.n_bits);
2671 sf->field = dst.field;
2672 memset(&sf->value, 0, sizeof sf->value);
2673 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2674 dst.field->n_bytes, dst.ofs, dst.n_bits);
2675 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2681 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2683 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2684 struct mf_subfield dst;
2687 dst.ofs = dst.n_bits = 0;
2688 while (next_load_segment(a, &dst, &value)) {
2689 ds_put_format(s, "load:%#"PRIx64"->", value);
2690 mf_format_subfield(&dst, s);
2691 ds_put_char(s, ',');
2695 ds_put_cstr(s, "set_field:");
2696 mf_format(a->field, &a->value, &a->mask, s);
2697 ds_put_format(s, "->%s", a->field->name);
2701 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2702 * is marked such that, if possible, it will be translated to OpenFlow as
2703 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2704 * that was the way that the action was expressed when it came into OVS or for
2705 * backward compatibility. */
2706 struct ofpact_set_field *
2707 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2709 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2710 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2714 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2716 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2719 struct nx_action_stack {
2720 ovs_be16 type; /* OFPAT_VENDOR. */
2721 ovs_be16 len; /* Length is 16. */
2722 ovs_be32 vendor; /* NX_VENDOR_ID. */
2723 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2724 ovs_be16 offset; /* Bit offset into the field. */
2726 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2727 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2728 * - Enough 0-bytes to pad out the action to 24 bytes. */
2729 uint8_t pad[12]; /* See above. */
2731 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2734 decode_stack_action(const struct nx_action_stack *nasp,
2735 struct ofpact_stack *stack_action)
2740 stack_action->subfield.ofs = ntohs(nasp->offset);
2742 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2743 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2744 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2748 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2750 if (!is_all_zeros(b.data, b.size)) {
2751 return OFPERR_NXBRC_MUST_BE_ZERO;
2758 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2759 enum ofp_version ofp_version OVS_UNUSED,
2760 struct ofpbuf *ofpacts)
2762 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2763 enum ofperr error = decode_stack_action(nasp, push);
2764 return error ? error : nxm_stack_push_check(push, NULL);
2768 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2769 enum ofp_version ofp_version OVS_UNUSED,
2770 struct ofpbuf *ofpacts)
2772 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2773 enum ofperr error = decode_stack_action(nasp, pop);
2774 return error ? error : nxm_stack_pop_check(pop, NULL);
2778 encode_STACK_op(const struct ofpact_stack *stack_action,
2779 struct nx_action_stack *nasp)
2784 nasp->offset = htons(stack_action->subfield.ofs);
2786 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2787 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2788 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2789 n_bits = htons(stack_action->subfield.n_bits);
2790 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2794 encode_STACK_PUSH(const struct ofpact_stack *stack,
2795 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2797 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2801 encode_STACK_POP(const struct ofpact_stack *stack,
2802 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2804 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2807 static char * OVS_WARN_UNUSED_RESULT
2808 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2809 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2811 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2814 static char * OVS_WARN_UNUSED_RESULT
2815 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2816 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2818 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2822 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2824 nxm_format_stack_push(a, s);
2828 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2830 nxm_format_stack_pop(a, s);
2833 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2835 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2836 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2837 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2838 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2840 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2841 * sent only to controllers with id 0.)
2843 struct nx_action_cnt_ids {
2844 ovs_be16 type; /* OFPAT_VENDOR. */
2845 ovs_be16 len; /* Length including slaves. */
2846 ovs_be32 vendor; /* NX_VENDOR_ID. */
2847 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2849 ovs_be16 n_controllers; /* Number of controllers. */
2850 uint8_t zeros[4]; /* Must be zero. */
2852 /* Followed by 1 or more controller ids.
2854 * uint16_t cnt_ids[]; // Controller ids.
2855 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2858 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2861 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2864 struct ofpact_cnt_ids *ids;
2865 enum ofperr error = 0;
2867 ids = ofpact_put_DEC_TTL(out);
2868 ids->n_controllers = 1;
2869 ofpbuf_put(out, &id, sizeof id);
2871 ofpact_update_len(out, &ids->ofpact);
2876 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2877 enum ofp_version ofp_version OVS_UNUSED,
2880 struct ofpact_cnt_ids *ids;
2884 ids = ofpact_put_DEC_TTL(out);
2885 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2886 ids->n_controllers = ntohs(nac_ids->n_controllers);
2887 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2889 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2890 return OFPERR_NXBRC_MUST_BE_ZERO;
2893 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2894 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2895 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2896 "are required for %"PRIu16" controllers.",
2897 ids_size, ids->n_controllers * sizeof(ovs_be16),
2898 ids->n_controllers);
2899 return OFPERR_OFPBAC_BAD_LEN;
2902 for (i = 0; i < ids->n_controllers; i++) {
2903 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2904 ofpbuf_put(out, &id, sizeof id);
2908 ofpact_update_len(out, &ids->ofpact);
2914 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2915 enum ofp_version ofp_version, struct ofpbuf *out)
2917 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2918 || dec_ttl->n_controllers != 1
2919 || dec_ttl->cnt_ids[0] != 0) {
2920 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2921 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2925 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2926 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2928 ids = ofpbuf_put_zeros(out, ids_len);
2929 for (i = 0; i < dec_ttl->n_controllers; i++) {
2930 ids[i] = htons(dec_ttl->cnt_ids[i]);
2933 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2938 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2940 struct ofpact_cnt_ids *ids;
2943 ofpact_put_DEC_TTL(ofpacts);
2944 ofpbuf_put(ofpacts, &id, sizeof id);
2945 ids = ofpacts->header;
2946 ids->n_controllers++;
2947 ofpact_update_len(ofpacts, &ids->ofpact);
2950 static char * OVS_WARN_UNUSED_RESULT
2951 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2952 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2955 parse_noargs_dec_ttl(ofpacts);
2957 struct ofpact_cnt_ids *ids;
2960 ids = ofpact_put_DEC_TTL(ofpacts);
2961 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2962 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2963 cntr = strtok_r(NULL, ", ", &arg)) {
2964 uint16_t id = atoi(cntr);
2966 ofpbuf_put(ofpacts, &id, sizeof id);
2967 ids = ofpacts->header;
2968 ids->n_controllers++;
2970 if (!ids->n_controllers) {
2971 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2974 ofpact_update_len(ofpacts, &ids->ofpact);
2980 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2984 ds_put_cstr(s, "dec_ttl");
2985 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2986 ds_put_cstr(s, "(");
2987 for (i = 0; i < a->n_controllers; i++) {
2989 ds_put_cstr(s, ",");
2991 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2993 ds_put_cstr(s, ")");
2997 /* Set MPLS label actions. */
3000 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
3001 enum ofp_version ofp_version OVS_UNUSED,
3004 ofpact_put_SET_MPLS_LABEL(out)->label = label;
3009 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3010 enum ofp_version ofp_version,
3013 if (ofp_version < OFP12_VERSION) {
3014 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3016 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
3017 ntohl(label->label));
3021 static char * OVS_WARN_UNUSED_RESULT
3022 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3023 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3025 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3027 return xstrdup("set_mpls_label: expected label.");
3030 mpls_label->label = htonl(atoi(arg));
3035 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3037 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
3040 /* Set MPLS TC actions. */
3043 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3044 enum ofp_version ofp_version OVS_UNUSED,
3047 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3052 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3053 enum ofp_version ofp_version, struct ofpbuf *out)
3055 if (ofp_version < OFP12_VERSION) {
3056 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3058 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3062 static char * OVS_WARN_UNUSED_RESULT
3063 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3064 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3066 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3069 return xstrdup("set_mpls_tc: expected tc.");
3072 mpls_tc->tc = atoi(arg);
3077 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3079 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
3082 /* Set MPLS TTL actions. */
3085 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3086 enum ofp_version ofp_version OVS_UNUSED,
3089 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3094 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3095 enum ofp_version ofp_version, struct ofpbuf *out)
3097 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3100 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3101 * action to 'ofpacts'.
3103 * Returns NULL if successful, otherwise a malloc()'d string describing the
3104 * error. The caller is responsible for freeing the returned string. */
3105 static char * OVS_WARN_UNUSED_RESULT
3106 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3107 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3109 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3112 return xstrdup("set_mpls_ttl: expected ttl.");
3115 mpls_ttl->ttl = atoi(arg);
3120 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3122 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3125 /* Decrement MPLS TTL actions. */
3128 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3130 ofpact_put_DEC_MPLS_TTL(out);
3135 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3136 enum ofp_version ofp_version, struct ofpbuf *out)
3138 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3141 static char * OVS_WARN_UNUSED_RESULT
3142 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3143 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3145 ofpact_put_DEC_MPLS_TTL(ofpacts);
3150 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3152 ds_put_cstr(s, "dec_mpls_ttl");
3155 /* Push MPLS label action. */
3158 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3159 enum ofp_version ofp_version OVS_UNUSED,
3162 struct ofpact_push_mpls *oam;
3164 if (!eth_type_mpls(ethertype)) {
3165 return OFPERR_OFPBAC_BAD_ARGUMENT;
3167 oam = ofpact_put_PUSH_MPLS(out);
3168 oam->ethertype = ethertype;
3174 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3175 enum ofp_version ofp_version, struct ofpbuf *out)
3177 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3180 static char * OVS_WARN_UNUSED_RESULT
3181 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3182 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3187 error = str_to_u16(arg, "push_mpls", ðertype);
3189 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3195 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3197 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3200 /* Pop MPLS label action. */
3203 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3204 enum ofp_version ofp_version OVS_UNUSED,
3207 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3212 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3213 enum ofp_version ofp_version, struct ofpbuf *out)
3215 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3218 static char * OVS_WARN_UNUSED_RESULT
3219 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3220 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3225 error = str_to_u16(arg, "pop_mpls", ðertype);
3227 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3233 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3235 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3238 /* Set tunnel ID actions. */
3241 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3242 enum ofp_version ofp_version OVS_UNUSED,
3245 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3246 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3247 tunnel->tun_id = tun_id;
3252 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3253 enum ofp_version ofp_version OVS_UNUSED,
3256 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3257 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3258 tunnel->tun_id = tun_id;
3263 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3264 enum ofp_version ofp_version, struct ofpbuf *out)
3266 uint64_t tun_id = tunnel->tun_id;
3268 if (ofp_version < OFP12_VERSION) {
3269 if (tun_id <= UINT32_MAX
3270 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3271 put_NXAST_SET_TUNNEL(out, tun_id);
3273 put_NXAST_SET_TUNNEL64(out, tun_id);
3276 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3280 static char * OVS_WARN_UNUSED_RESULT
3281 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3282 enum ofp_raw_action_type raw)
3284 struct ofpact_tunnel *tunnel;
3286 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3287 tunnel->ofpact.raw = raw;
3288 return str_to_u64(arg, &tunnel->tun_id);
3291 static char * OVS_WARN_UNUSED_RESULT
3292 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3293 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3295 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3299 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3301 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3302 (a->tun_id > UINT32_MAX
3303 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3307 /* Set queue action. */
3310 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3311 enum ofp_version ofp_version OVS_UNUSED,
3314 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3319 encode_SET_QUEUE(const struct ofpact_queue *queue,
3320 enum ofp_version ofp_version, struct ofpbuf *out)
3322 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3325 static char * OVS_WARN_UNUSED_RESULT
3326 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3327 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3329 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3333 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3335 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3338 /* Pop queue action. */
3341 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3343 ofpact_put_POP_QUEUE(out);
3348 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3349 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3351 put_NXAST_POP_QUEUE(out);
3354 static char * OVS_WARN_UNUSED_RESULT
3355 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3356 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3358 ofpact_put_POP_QUEUE(ofpacts);
3363 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3365 ds_put_cstr(s, "pop_queue");
3368 /* Action structure for NXAST_FIN_TIMEOUT.
3370 * This action changes the idle timeout or hard timeout, or both, of this
3371 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3372 * When such a packet is observed, the action reduces the rule's idle timeout
3373 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3374 * action has no effect on an existing timeout that is already shorter than the
3375 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3376 * of zero has no effect on the respective timeout.
3378 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3379 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3380 * receipt of the FIN or RST.
3382 * This is useful for quickly discarding learned TCP flows that otherwise will
3383 * take a long time to expire.
3385 * This action is intended for use with an OpenFlow rule that matches only a
3386 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3387 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3388 * RST in any of those flows will cause the entire OpenFlow rule to expire
3389 * early, which is not normally desirable.
3391 struct nx_action_fin_timeout {
3392 ovs_be16 type; /* OFPAT_VENDOR. */
3393 ovs_be16 len; /* 16. */
3394 ovs_be32 vendor; /* NX_VENDOR_ID. */
3395 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3396 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3397 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3398 ovs_be16 pad; /* Must be zero. */
3400 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3403 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3404 enum ofp_version ofp_version OVS_UNUSED,
3407 struct ofpact_fin_timeout *oft;
3409 oft = ofpact_put_FIN_TIMEOUT(out);
3410 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3411 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3416 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3417 enum ofp_version ofp_version OVS_UNUSED,
3420 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3421 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3422 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3425 static char * OVS_WARN_UNUSED_RESULT
3426 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3427 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3429 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3432 while (ofputil_parse_key_value(&arg, &key, &value)) {
3435 if (!strcmp(key, "idle_timeout")) {
3436 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3437 } else if (!strcmp(key, "hard_timeout")) {
3438 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3440 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3452 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3454 ds_put_cstr(s, "fin_timeout(");
3455 if (a->fin_idle_timeout) {
3456 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3458 if (a->fin_hard_timeout) {
3459 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3462 ds_put_char(s, ')');
3465 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3467 * These actions search one of the switch's flow tables:
3469 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3470 * it specifies the table to search.
3472 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3473 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3474 * table, that is, the OpenFlow flow table that contains the flow from
3475 * which this action was obtained. If this action did not come from a
3476 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3477 * is the current table.
3479 * The flow table lookup uses a flow that may be slightly modified from the
3482 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3483 * is used as the flow's in_port.
3485 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3486 * then its value is used as the flow's in_port. Otherwise, the original
3489 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3490 * resubmit action, then the flow is updated with the new values.
3492 * Following the lookup, the original in_port is restored.
3494 * If the modified flow matched in the flow table, then the corresponding
3495 * actions are executed. Afterward, actions following the resubmit in the
3496 * original set of actions, if any, are executed; any changes made to the
3497 * packet (e.g. changes to VLAN) by secondary actions persist when those
3498 * actions are executed, although the original in_port is restored.
3500 * Resubmit actions may be used any number of times within a set of actions.
3502 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3503 * implementation-defined depth, further resubmit actions are simply ignored.
3505 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3506 * 'pad' to be all-bits-zero.
3508 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3509 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3511 struct nx_action_resubmit {
3512 ovs_be16 type; /* OFPAT_VENDOR. */
3513 ovs_be16 len; /* Length is 16. */
3514 ovs_be32 vendor; /* NX_VENDOR_ID. */
3515 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3516 ovs_be16 in_port; /* New in_port for checking flow table. */
3517 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3520 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3523 decode_NXAST_RAW_RESUBMIT(uint16_t port,
3524 enum ofp_version ofp_version OVS_UNUSED,
3527 struct ofpact_resubmit *resubmit;
3529 resubmit = ofpact_put_RESUBMIT(out);
3530 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3531 resubmit->in_port = u16_to_ofp(port);
3532 resubmit->table_id = 0xff;
3537 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3538 enum ofp_version ofp_version OVS_UNUSED,
3541 struct ofpact_resubmit *resubmit;
3543 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3544 return OFPERR_OFPBAC_BAD_ARGUMENT;
3547 resubmit = ofpact_put_RESUBMIT(out);
3548 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3549 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3550 resubmit->table_id = nar->table;
3555 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3556 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3558 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3560 if (resubmit->table_id == 0xff
3561 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3562 put_NXAST_RESUBMIT(out, in_port);
3564 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3565 nar->table = resubmit->table_id;
3566 nar->in_port = htons(in_port);
3570 static char * OVS_WARN_UNUSED_RESULT
3571 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3572 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3574 struct ofpact_resubmit *resubmit;
3575 char *in_port_s, *table_s;
3577 resubmit = ofpact_put_RESUBMIT(ofpacts);
3579 in_port_s = strsep(&arg, ",");
3580 if (in_port_s && in_port_s[0]) {
3581 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3582 return xasprintf("%s: resubmit to unknown port", in_port_s);
3585 resubmit->in_port = OFPP_IN_PORT;
3588 table_s = strsep(&arg, ",");
3589 if (table_s && table_s[0]) {
3590 uint32_t table_id = 0;
3593 error = str_to_u32(table_s, &table_id);
3597 resubmit->table_id = table_id;
3599 resubmit->table_id = 255;
3602 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3603 return xstrdup("at least one \"in_port\" or \"table\" must be "
3604 "specified on resubmit");
3610 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3612 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3613 ds_put_cstr(s, "resubmit:");
3614 ofputil_format_port(a->in_port, s);
3616 ds_put_format(s, "resubmit(");
3617 if (a->in_port != OFPP_IN_PORT) {
3618 ofputil_format_port(a->in_port, s);
3620 ds_put_char(s, ',');
3621 if (a->table_id != 255) {
3622 ds_put_format(s, "%"PRIu8, a->table_id);
3624 ds_put_char(s, ')');
3628 /* Action structure for NXAST_LEARN.
3630 * This action adds or modifies a flow in an OpenFlow table, similar to
3631 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3632 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3633 * flow's match criteria and actions are built by applying each of the series
3634 * of flow_mod_spec elements included as part of the action.
3636 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3637 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3638 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3639 * bits from a source to a destination. In this case, the header contains
3642 * 15 14 13 12 11 10 0
3643 * +------+---+------+---------------------------------+
3644 * | 0 |src| dst | n_bits |
3645 * +------+---+------+---------------------------------+
3647 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3648 * following table summarizes the meaning of each possible combination.
3649 * Details follow the table:
3652 * --- --- ----------------------------------------------------------
3653 * 0 0 Add match criteria based on value in a field.
3654 * 1 0 Add match criteria based on an immediate value.
3655 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3656 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3657 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3658 * All other combinations are undefined and not allowed.
3660 * The flow_mod_spec header is followed by a source specification and a
3661 * destination specification. The format and meaning of the source
3662 * specification depends on 'src':
3664 * - If 'src' is 0, the source bits are taken from a field in the flow to
3665 * which this action is attached. (This should be a wildcarded field. If
3666 * its value is fully specified then the source bits being copied have
3669 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3670 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3671 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3672 * 'field' and 'ofs' are subject to the same restrictions as the source
3673 * field in NXAST_REG_MOVE.
3675 * - If 'src' is 1, the source bits are a constant value. The source
3676 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3677 * number in network order, the source bits are the 'n_bits'
3678 * least-significant bits. The switch will report an error if other bits
3679 * in the constant are nonzero.
3681 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3682 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3683 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3684 * meaning of the flow_mod_spec depends on 'dst':
3686 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3687 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3688 * packet equal the source bits. 'field' may be any nxm_header with
3689 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3691 * Order is significant. Earlier flow_mod_specs must satisfy any
3692 * prerequisites for matching fields specified later, by copying constant
3693 * values into prerequisite fields.
3695 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3698 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3699 * the new flow. The new flow copies the source bits into
3700 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3703 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3704 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3706 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3707 * empty. It has the following meaning:
3709 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3710 * The new flow outputs to the OpenFlow port specified by the source field.
3711 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3712 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3715 * Resource Management
3716 * -------------------
3718 * A switch has a finite amount of flow table space available for learning.
3719 * When this space is exhausted, no new learning table entries will be learned
3720 * until some existing flow table entries expire. The controller should be
3721 * prepared to handle this by flooding (which can be implemented as a
3722 * low-priority flow).
3724 * If a learned flow matches a single TCP stream with a relatively long
3725 * timeout, one may make the best of resource constraints by setting
3726 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3727 * both, to shorter timeouts. When either of these is specified as a nonzero
3728 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3734 * The following examples give a prose description of the flow_mod_specs along
3735 * with informal notation for how those would be represented and a hex dump of
3736 * the bytes that would be required.
3738 * These examples could work with various nx_action_learn parameters. Typical
3739 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3740 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3742 * 1. Learn input port based on the source MAC, with lookup into
3743 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3745 * Match on in_port=99:
3746 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3747 * ovs_be16(99), 00 63
3748 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3750 * Match Ethernet destination on Ethernet source from packet:
3751 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3752 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3753 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3755 * Set NXM_NX_REG1[16:31] to the packet's input port:
3756 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3757 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3758 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3760 * Given a packet that arrived on port A with Ethernet source address B,
3761 * this would set up the flow "in_port=99, dl_dst=B,
3762 * actions=load:A->NXM_NX_REG1[16..31]".
3764 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3765 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3766 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3768 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3769 * into NXM_NX_REG1[16:31]:
3771 * Match on same VLAN ID as packet:
3772 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3773 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3774 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3776 * Match Ethernet destination on Ethernet source from packet:
3777 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3778 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3779 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3781 * Output to the packet's input port:
3782 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3783 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3785 * Given a packet that arrived on port A with Ethernet source address B in
3786 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3787 * actions=output:A".
3789 * In syntax accepted by ovs-ofctl, this action is:
3790 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3791 * output:NXM_OF_IN_PORT[])
3793 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3794 * 10-second MAC expiration time to make it easier to see what's going on
3796 * ovs-vsctl del-controller br0
3797 * ovs-ofctl del-flows br0
3798 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3799 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3800 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3801 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3802 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3804 * You can then dump the MAC learning table with:
3806 * ovs-ofctl dump-flows br0 table=1
3811 * For best performance, segregate learned flows into a table that is not used
3812 * for any other flows except possibly for a lowest-priority "catch-all" flow
3813 * (a flow with no match criteria). If different learning actions specify
3814 * different match criteria, use different tables for the learned flows.
3816 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3817 * These timeouts apply to the flow that is added, which means that a flow with
3818 * an idle timeout will expire when no traffic has been sent *to* the learned
3819 * address. This is not usually the intent in MAC learning; instead, we want
3820 * the MAC learn entry to expire when no traffic has been sent *from* the
3821 * learned address. Use a hard timeout for that.
3824 * Visibility of Changes
3825 * ---------------------
3827 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3828 * flow translation are visible to flow table lookups made later in the flow
3829 * translation. This means that, in the example above, a MAC learned by the
3830 * learn action in table 0 would be found in table 1 (if the packet being
3831 * processed had the same source and destination MAC address).
3833 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3834 * modify a flow) by a "learn" action are visible only for later flow
3835 * translations, not for later lookups within the same flow translation. In
3836 * the MAC learning example, a MAC learned by the learn action in table 0 would
3837 * not be found in table 1 if the flow translation would resubmit to table 1
3838 * after the processing of the learn action, meaning that if this MAC had not
3839 * been learned before then the packet would be flooded. */
3840 struct nx_action_learn {
3841 ovs_be16 type; /* OFPAT_VENDOR. */
3842 ovs_be16 len; /* At least 24. */
3843 ovs_be32 vendor; /* NX_VENDOR_ID. */
3844 ovs_be16 subtype; /* NXAST_LEARN. */
3845 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3846 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3847 ovs_be16 priority; /* Priority level of flow entry. */
3848 ovs_be64 cookie; /* Cookie for new flow. */
3849 ovs_be16 flags; /* NX_LEARN_F_*. */
3850 uint8_t table_id; /* Table to insert flow entry. */
3851 uint8_t pad; /* Must be zero. */
3852 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3853 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3854 /* Followed by a sequence of flow_mod_spec elements, as described above,
3855 * until the end of the action is reached. */
3857 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3860 get_be16(const void **pp)
3862 const ovs_be16 *p = *pp;
3863 ovs_be16 value = *p;
3869 get_be32(const void **pp)
3871 const ovs_be32 *p = *pp;
3872 ovs_be32 value = get_unaligned_be32(p);
3878 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3880 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3881 sf->ofs = ntohs(get_be16(p));
3882 sf->n_bits = n_bits;
3886 learn_min_len(uint16_t header)
3888 int n_bits = header & NX_LEARN_N_BITS_MASK;
3889 int src_type = header & NX_LEARN_SRC_MASK;
3890 int dst_type = header & NX_LEARN_DST_MASK;
3891 unsigned int min_len;
3894 if (src_type == NX_LEARN_SRC_FIELD) {
3895 min_len += sizeof(ovs_be32); /* src_field */
3896 min_len += sizeof(ovs_be16); /* src_ofs */
3898 min_len += DIV_ROUND_UP(n_bits, 16);
3900 if (dst_type == NX_LEARN_DST_MATCH ||
3901 dst_type == NX_LEARN_DST_LOAD) {
3902 min_len += sizeof(ovs_be32); /* dst_field */
3903 min_len += sizeof(ovs_be16); /* dst_ofs */
3908 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3909 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3911 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3912 enum ofp_version ofp_version OVS_UNUSED,
3913 struct ofpbuf *ofpacts)
3915 struct ofpact_learn *learn;
3916 const void *p, *end;
3919 return OFPERR_OFPBAC_BAD_ARGUMENT;
3922 learn = ofpact_put_LEARN(ofpacts);
3924 learn->idle_timeout = ntohs(nal->idle_timeout);
3925 learn->hard_timeout = ntohs(nal->hard_timeout);
3926 learn->priority = ntohs(nal->priority);
3927 learn->cookie = nal->cookie;
3928 learn->table_id = nal->table_id;
3929 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3930 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3932 learn->flags = ntohs(nal->flags);
3933 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3934 NX_LEARN_F_DELETE_LEARNED)) {
3935 return OFPERR_OFPBAC_BAD_ARGUMENT;
3938 if (learn->table_id == 0xff) {
3939 return OFPERR_OFPBAC_BAD_ARGUMENT;
3942 end = (char *) nal + ntohs(nal->len);
3943 for (p = nal + 1; p != end; ) {
3944 struct ofpact_learn_spec *spec;
3945 uint16_t header = ntohs(get_be16(&p));
3951 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3952 learn = ofpacts->header;
3955 spec->src_type = header & NX_LEARN_SRC_MASK;
3956 spec->dst_type = header & NX_LEARN_DST_MASK;
3957 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3959 /* Check for valid src and dst type combination. */
3960 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3961 spec->dst_type == NX_LEARN_DST_LOAD ||
3962 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3963 spec->src_type == NX_LEARN_SRC_FIELD)) {
3966 return OFPERR_OFPBAC_BAD_ARGUMENT;
3969 /* Check that the arguments don't overrun the end of the action. */
3970 if ((char *) end - (char *) p < learn_min_len(header)) {
3971 return OFPERR_OFPBAC_BAD_LEN;
3974 /* Get the source. */
3975 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3976 get_subfield(spec->n_bits, &p, &spec->src);
3978 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3980 bitwise_copy(p, p_bytes, 0,
3981 &spec->src_imm, sizeof spec->src_imm, 0,
3983 p = (const uint8_t *) p + p_bytes;
3986 /* Get the destination. */
3987 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3988 spec->dst_type == NX_LEARN_DST_LOAD) {
3989 get_subfield(spec->n_bits, &p, &spec->dst);
3992 ofpact_update_len(ofpacts, &learn->ofpact);
3994 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3995 return OFPERR_OFPBAC_BAD_ARGUMENT;
4002 put_be16(struct ofpbuf *b, ovs_be16 x)
4004 ofpbuf_put(b, &x, sizeof x);
4008 put_be32(struct ofpbuf *b, ovs_be32 x)
4010 ofpbuf_put(b, &x, sizeof x);
4014 put_u16(struct ofpbuf *b, uint16_t x)
4016 put_be16(b, htons(x));
4020 put_u32(struct ofpbuf *b, uint32_t x)
4022 put_be32(b, htonl(x));
4026 encode_LEARN(const struct ofpact_learn *learn,
4027 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4029 const struct ofpact_learn_spec *spec;
4030 struct nx_action_learn *nal;
4033 start_ofs = out->size;
4034 nal = put_NXAST_LEARN(out);
4035 nal->idle_timeout = htons(learn->idle_timeout);
4036 nal->hard_timeout = htons(learn->hard_timeout);
4037 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4038 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4039 nal->priority = htons(learn->priority);
4040 nal->cookie = learn->cookie;
4041 nal->flags = htons(learn->flags);
4042 nal->table_id = learn->table_id;
4044 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4045 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4047 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4048 put_u32(out, mf_nxm_header(spec->src.field->id));
4049 put_u16(out, spec->src.ofs);
4051 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4052 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4053 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4054 bits, n_dst_bytes, 0,
4058 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4059 spec->dst_type == NX_LEARN_DST_LOAD) {
4060 put_u32(out, mf_nxm_header(spec->dst.field->id));
4061 put_u16(out, spec->dst.ofs);
4065 pad_ofpat(out, start_ofs);
4068 static char * OVS_WARN_UNUSED_RESULT
4069 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4070 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4072 return learn_parse(arg, ofpacts);
4076 format_LEARN(const struct ofpact_learn *a, struct ds *s)
4081 /* Action structure for NXAST_CONJUNCTION. */
4082 struct nx_action_conjunction {
4083 ovs_be16 type; /* OFPAT_VENDOR. */
4084 ovs_be16 len; /* At least 16. */
4085 ovs_be32 vendor; /* NX_VENDOR_ID. */
4086 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4091 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4094 add_conjunction(struct ofpbuf *out,
4095 uint32_t id, uint8_t clause, uint8_t n_clauses)
4097 struct ofpact_conjunction *oc;
4099 oc = ofpact_put_CONJUNCTION(out);
4101 oc->clause = clause;
4102 oc->n_clauses = n_clauses;
4106 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4107 enum ofp_version ofp_version OVS_UNUSED,
4110 if (nac->n_clauses < 2 || nac->n_clauses > 64
4111 || nac->clause >= nac->n_clauses) {
4112 return OFPERR_NXBAC_BAD_CONJUNCTION;
4114 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4120 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4121 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4123 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4124 nac->clause = oc->clause;
4125 nac->n_clauses = oc->n_clauses;
4126 nac->id = htonl(oc->id);
4130 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4132 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4133 oc->id, oc->clause + 1, oc->n_clauses);
4136 static char * OVS_WARN_UNUSED_RESULT
4137 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4138 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4145 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4146 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4147 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4150 if (n_clauses < 2) {
4151 return xstrdup("conjunction must have at least 2 clauses");
4152 } else if (n_clauses > 64) {
4153 return xstrdup("conjunction must have at most 64 clauses");
4154 } else if (clause < 1) {
4155 return xstrdup("clause index must be positive");
4156 } else if (clause > n_clauses) {
4157 return xstrdup("clause index must be less than or equal to "
4158 "number of clauses");
4161 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4165 /* Action structure for NXAST_MULTIPATH.
4167 * This action performs the following steps in sequence:
4169 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4170 * Refer to the definition of "enum nx_mp_fields" for details.
4172 * The 'basis' value is used as a universal hash parameter, that is,
4173 * different values of 'basis' yield different hash functions. The
4174 * particular universal hash function used is implementation-defined.
4176 * The hashed fields' values are drawn from the current state of the
4177 * flow, including all modifications that have been made by actions up to
4180 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4181 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4184 * The output of the algorithm is 'link', an unsigned integer less than
4185 * or equal to 'max_link'.
4187 * Some algorithms use 'arg' as an additional argument.
4189 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4190 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4193 * The switch will reject actions that have an unknown 'fields', or an unknown
4194 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4195 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4196 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4198 struct nx_action_multipath {
4199 ovs_be16 type; /* OFPAT_VENDOR. */
4200 ovs_be16 len; /* Length is 32. */
4201 ovs_be32 vendor; /* NX_VENDOR_ID. */
4202 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4204 /* What fields to hash and how. */
4205 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4206 ovs_be16 basis; /* Universal hash parameter. */
4209 /* Multipath link choice algorithm to apply to hash value. */
4210 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4211 ovs_be16 max_link; /* Number of output links, minus 1. */
4212 ovs_be32 arg; /* Algorithm-specific argument. */
4215 /* Where to store the result. */
4216 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4217 ovs_be32 dst; /* Destination. */
4219 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4222 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4223 enum ofp_version ofp_version OVS_UNUSED,
4226 uint32_t n_links = ntohs(nam->max_link) + 1;
4227 size_t min_n_bits = log_2_ceil(n_links);
4228 struct ofpact_multipath *mp;
4230 mp = ofpact_put_MULTIPATH(out);
4231 mp->fields = ntohs(nam->fields);
4232 mp->basis = ntohs(nam->basis);
4233 mp->algorithm = ntohs(nam->algorithm);
4234 mp->max_link = ntohs(nam->max_link);
4235 mp->arg = ntohl(nam->arg);
4236 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4237 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4238 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4240 if (!flow_hash_fields_valid(mp->fields)) {
4241 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4242 return OFPERR_OFPBAC_BAD_ARGUMENT;
4243 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4244 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4245 && mp->algorithm != NX_MP_ALG_HRW
4246 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4247 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4248 return OFPERR_OFPBAC_BAD_ARGUMENT;
4249 } else if (mp->dst.n_bits < min_n_bits) {
4250 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4251 "%"PRIu32" links", min_n_bits, n_links);
4252 return OFPERR_OFPBAC_BAD_ARGUMENT;
4255 return multipath_check(mp, NULL);
4259 encode_MULTIPATH(const struct ofpact_multipath *mp,
4260 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4262 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4264 nam->fields = htons(mp->fields);
4265 nam->basis = htons(mp->basis);
4266 nam->algorithm = htons(mp->algorithm);
4267 nam->max_link = htons(mp->max_link);
4268 nam->arg = htonl(mp->arg);
4269 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4270 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4273 static char * OVS_WARN_UNUSED_RESULT
4274 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4275 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4277 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4281 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4283 multipath_format(a, s);
4286 /* Action structure for NXAST_NOTE.
4288 * This action has no effect. It is variable length. The switch does not
4289 * attempt to interpret the user-defined 'note' data in any way. A controller
4290 * can use this action to attach arbitrary metadata to a flow.
4292 * This action might go away in the future.
4294 struct nx_action_note {
4295 ovs_be16 type; /* OFPAT_VENDOR. */
4296 ovs_be16 len; /* A multiple of 8, but at least 16. */
4297 ovs_be32 vendor; /* NX_VENDOR_ID. */
4298 ovs_be16 subtype; /* NXAST_NOTE. */
4299 uint8_t note[6]; /* Start of user-defined data. */
4300 /* Possibly followed by additional user-defined data. */
4302 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4305 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4306 enum ofp_version ofp_version OVS_UNUSED,
4309 struct ofpact_note *note;
4310 unsigned int length;
4312 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4313 note = ofpact_put(out, OFPACT_NOTE,
4314 offsetof(struct ofpact_note, data) + length);
4315 note->length = length;
4316 memcpy(note->data, nan->note, length);
4322 encode_NOTE(const struct ofpact_note *note,
4323 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4325 size_t start_ofs = out->size;
4326 struct nx_action_note *nan;
4328 put_NXAST_NOTE(out);
4329 out->size = out->size - sizeof nan->note;
4331 ofpbuf_put(out, note->data, note->length);
4332 pad_ofpat(out, start_ofs);
4335 static char * OVS_WARN_UNUSED_RESULT
4336 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4337 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4339 struct ofpact_note *note;
4341 note = ofpact_put_NOTE(ofpacts);
4342 while (*arg != '\0') {
4353 byte = hexits_value(arg, 2, &ok);
4355 return xstrdup("bad hex digit in `note' argument");
4357 ofpbuf_put(ofpacts, &byte, 1);
4359 note = ofpacts->header;
4364 ofpact_update_len(ofpacts, ¬e->ofpact);
4369 format_NOTE(const struct ofpact_note *a, struct ds *s)
4373 ds_put_cstr(s, "note:");
4374 for (i = 0; i < a->length; i++) {
4376 ds_put_char(s, '.');
4378 ds_put_format(s, "%02"PRIx8, a->data[i]);
4385 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4387 ofpact_put_EXIT(out);
4392 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4393 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4395 put_NXAST_EXIT(out);
4398 static char * OVS_WARN_UNUSED_RESULT
4399 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4400 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4402 ofpact_put_EXIT(ofpacts);
4407 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4409 ds_put_cstr(s, "exit");
4412 /* Unroll xlate action. */
4415 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4416 enum ofp_version ofp_version OVS_UNUSED,
4417 struct ofpbuf *out OVS_UNUSED)
4422 static char * OVS_WARN_UNUSED_RESULT
4423 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4424 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4431 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4434 ds_put_cstr(s, "unroll_xlate");
4437 /* Action structure for NXAST_SAMPLE.
4439 * Samples matching packets with the given probability and sends them
4440 * each to the set of collectors identified with the given ID. The
4441 * probability is expressed as a number of packets to be sampled out
4442 * of USHRT_MAX packets, and must be >0.
4444 * When sending packet samples to IPFIX collectors, the IPFIX flow
4445 * record sent for each sampled packet is associated with the given
4446 * observation domain ID and observation point ID. Each IPFIX flow
4447 * record contain the sampled packet's headers when executing this
4448 * rule. If a sampled packet's headers are modified by previous
4449 * actions in the flow, those modified headers are sent. */
4450 struct nx_action_sample {
4451 ovs_be16 type; /* OFPAT_VENDOR. */
4452 ovs_be16 len; /* Length is 24. */
4453 ovs_be32 vendor; /* NX_VENDOR_ID. */
4454 ovs_be16 subtype; /* NXAST_SAMPLE. */
4455 ovs_be16 probability; /* Fraction of packets to sample. */
4456 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4457 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4458 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4460 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4463 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4464 enum ofp_version ofp_version OVS_UNUSED,
4467 struct ofpact_sample *sample;
4469 sample = ofpact_put_SAMPLE(out);
4470 sample->probability = ntohs(nas->probability);
4471 sample->collector_set_id = ntohl(nas->collector_set_id);
4472 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4473 sample->obs_point_id = ntohl(nas->obs_point_id);
4475 if (sample->probability == 0) {
4476 return OFPERR_OFPBAC_BAD_ARGUMENT;
4483 encode_SAMPLE(const struct ofpact_sample *sample,
4484 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4486 struct nx_action_sample *nas;
4488 nas = put_NXAST_SAMPLE(out);
4489 nas->probability = htons(sample->probability);
4490 nas->collector_set_id = htonl(sample->collector_set_id);
4491 nas->obs_domain_id = htonl(sample->obs_domain_id);
4492 nas->obs_point_id = htonl(sample->obs_point_id);
4495 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4496 * action to 'ofpacts'.
4498 * Returns NULL if successful, otherwise a malloc()'d string describing the
4499 * error. The caller is responsible for freeing the returned string. */
4500 static char * OVS_WARN_UNUSED_RESULT
4501 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4502 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4504 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4507 while (ofputil_parse_key_value(&arg, &key, &value)) {
4510 if (!strcmp(key, "probability")) {
4511 error = str_to_u16(value, "probability", &os->probability);
4512 if (!error && os->probability == 0) {
4513 error = xasprintf("invalid probability value \"%s\"", value);
4515 } else if (!strcmp(key, "collector_set_id")) {
4516 error = str_to_u32(value, &os->collector_set_id);
4517 } else if (!strcmp(key, "obs_domain_id")) {
4518 error = str_to_u32(value, &os->obs_domain_id);
4519 } else if (!strcmp(key, "obs_point_id")) {
4520 error = str_to_u32(value, &os->obs_point_id);
4522 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4529 if (os->probability == 0) {
4530 return xstrdup("non-zero \"probability\" must be specified on sample");
4536 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4538 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4539 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4540 a->probability, a->collector_set_id,
4541 a->obs_domain_id, a->obs_point_id);
4544 /* debug_recirc instruction. */
4546 static bool enable_debug;
4549 ofpact_dummy_enable(void)
4551 enable_debug = true;
4555 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4557 if (!enable_debug) {
4558 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4561 ofpact_put_DEBUG_RECIRC(out);
4566 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4567 enum ofp_version ofp_version OVS_UNUSED,
4570 put_NXAST_DEBUG_RECIRC(out);
4573 static char * OVS_WARN_UNUSED_RESULT
4574 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4575 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4577 ofpact_put_DEBUG_RECIRC(ofpacts);
4582 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4584 ds_put_cstr(s, "debug_recirc");
4587 /* Action structure for NXAST_CT.
4589 * Pass traffic to the connection tracker.
4591 * There are two important concepts to understanding the connection tracking
4592 * interface: Packet state and Connection state. Packets may be "Untracked" or
4593 * "Tracked". Connections may be "Uncommitted" or "Committed".
4597 * Untracked packets have not yet passed through the connection tracker,
4598 * and the connection state for such packets is unknown. In most cases,
4599 * packets entering the OpenFlow pipeline will initially be in the
4600 * untracked state. Untracked packets may become tracked by executing
4601 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4602 * about the connection available, in particular the connection state.
4604 * Tracked packets have previously passed through the connection tracker.
4605 * These packets will remain tracked through until the end of the OpenFlow
4606 * pipeline. Tracked packets which have NXAST_CT executed with a
4607 * "recirc_table" specified will return to the tracked state.
4609 * The packet state is only significant for the duration of packet
4610 * processing within the OpenFlow pipeline.
4612 * - Connection State:
4614 * Multiple packets may be associated with a single connection. Initially,
4615 * all connections are uncommitted. The connection state corresponding to
4616 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4618 * Uncommitted connections have no state stored about them. Uncommitted
4619 * connections may transition into the committed state by executing
4620 * NXAST_CT with the NX_CT_F_COMMIT flag.
4622 * Once a connection becomes committed, information may be gathered about
4623 * the connection by passing subsequent packets through the connection
4624 * tracker, and the state of the connection will be stored beyond the
4625 * lifetime of packet processing.
4627 * Connections may transition back into the uncommitted state due to
4628 * external timers, or due to the contents of packets that are sent to the
4629 * connection tracker. This behaviour is outside of the scope of the
4630 * OpenFlow interface.
4632 * The "zone" specifies a context within which the tracking is done:
4634 * The connection tracking zone is a 16-bit number. Each zone is an
4635 * independent connection tracking context. The connection state for each
4636 * connection is completely separate for each zone, so if a connection
4637 * is committed to zone A, then it will remain uncommitted in zone B.
4638 * If NXAST_CT is executed with the same zone multiple times, later
4639 * executions have no effect.
4641 * If 'zone_src' is nonzero, this specifies that the zone should be
4642 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4643 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4644 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4645 * are the same as the acceptable nxm_header values for the 'src' field of
4648 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4649 * connection tracking zone.
4651 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4653 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4654 * packet will be logically cloned prior to executing this action. One
4655 * copy will be sent to the connection tracker, then will be re-injected
4656 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
4657 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4658 * fields will be populated. The original instance of the packet will
4659 * continue the current actions list. This can be thought of as similar to
4660 * the effect of the "output" action: One copy is sent out (in this case,
4661 * to the connection tracker), but the current copy continues processing.
4663 * It is strongly recommended that this table is later than the current
4664 * table, to prevent loops.
4666 * The "alg" attaches protocol-specific behaviour to this action:
4668 * The ALG is a 16-bit number which specifies that additional
4669 * processing should be applied to this traffic.
4671 * Protocol | Value | Meaning
4672 * --------------------------------------------------------------------
4673 * None | 0 | No protocol-specific behaviour.
4674 * FTP | 21 | Parse FTP control connections and observe the
4675 * | | negotiation of related data connections.
4676 * Other | Other | Unsupported protocols.
4678 * By way of example, if FTP control connections have this action applied
4679 * with the ALG set to FTP (21), then the connection tracker will observe
4680 * the negotiation of data connections. This allows the connection
4681 * tracker to identify subsequent data connections as "related" to this
4682 * existing connection. The "related" flag will be populated in the
4683 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
4686 * Zero or more actions may immediately follow this action. These actions will
4687 * be executed within the context of the connection tracker, and they require
4688 * the NX_CT_F_COMMIT flag to be set.
4690 struct nx_action_conntrack {
4691 ovs_be16 type; /* OFPAT_VENDOR. */
4692 ovs_be16 len; /* At least 24. */
4693 ovs_be32 vendor; /* NX_VENDOR_ID. */
4694 ovs_be16 subtype; /* NXAST_CT. */
4695 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
4696 * Unspecified flag bits must be zero. */
4697 ovs_be32 zone_src; /* Connection tracking context. */
4699 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
4700 ovs_be16 zone_imm; /* Immediate value for zone. */
4702 uint8_t recirc_table; /* Recirculate to a specific table, or
4703 NX_CT_RECIRC_NONE for no recirculation. */
4704 uint8_t pad[3]; /* Zeroes */
4705 ovs_be16 alg; /* Well-known port number for the protocol.
4706 * 0 indicates no ALG is required. */
4707 /* Followed by a sequence of zero or more OpenFlow actions. The length of
4708 * these is included in 'len'. */
4710 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
4713 decode_ct_zone(const struct nx_action_conntrack *nac,
4714 struct ofpact_conntrack *out)
4716 if (nac->zone_src) {
4719 out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
4720 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
4721 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
4722 error = mf_check_src(&out->zone_src, NULL);
4727 if (out->zone_src.n_bits != 16) {
4728 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
4729 out->zone_src.n_bits);
4730 return OFPERR_OFPBAC_BAD_SET_LEN;
4733 out->zone_src.field = NULL;
4734 out->zone_imm = ntohs(nac->zone_imm);
4741 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
4742 enum ofp_version ofp_version, struct ofpbuf *out)
4744 const size_t ct_offset = ofpacts_pull(out);
4745 struct ofpact_conntrack *conntrack;
4746 struct ofpbuf openflow;
4749 conntrack = ofpact_put_CT(out);
4750 conntrack->flags = ntohs(nac->flags);
4751 error = decode_ct_zone(nac, conntrack);
4755 conntrack->recirc_table = nac->recirc_table;
4756 conntrack->alg = ntohs(nac->alg);
4758 ofpbuf_pull(out, sizeof(*conntrack));
4760 ofpbuf_use_const(&openflow, nac + 1, ntohs(nac->len) - sizeof(*nac));
4761 error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
4763 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4769 conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
4770 out->header = &conntrack->ofpact;
4771 ofpact_update_len(out, &conntrack->ofpact);
4773 if (conntrack->ofpact.len > sizeof(*conntrack)
4774 && !(conntrack->flags & NX_CT_F_COMMIT)) {
4775 const struct ofpact *a;
4776 size_t ofpacts_len = conntrack->ofpact.len - sizeof(*conntrack);
4778 OFPACT_FOR_EACH (a, conntrack->actions, ofpacts_len) {
4779 if (a->type != OFPACT_NAT || ofpact_get_NAT(a)->flags
4780 || ofpact_get_NAT(a)->range_af != AF_UNSPEC) {
4781 VLOG_WARN_RL(&rl, "CT action requires commit flag if actions "
4782 "other than NAT without arguments are specified.");
4783 error = OFPERR_OFPBAC_BAD_ARGUMENT;
4790 ofpbuf_push_uninit(out, ct_offset);
4795 encode_CT(const struct ofpact_conntrack *conntrack,
4796 enum ofp_version ofp_version, struct ofpbuf *out)
4798 struct nx_action_conntrack *nac;
4799 const size_t ofs = out->size;
4802 nac = put_NXAST_CT(out);
4803 nac->flags = htons(conntrack->flags);
4804 if (conntrack->zone_src.field) {
4805 nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
4806 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
4807 conntrack->zone_src.n_bits);
4809 nac->zone_src = htonl(0);
4810 nac->zone_imm = htons(conntrack->zone_imm);
4812 nac->recirc_table = conntrack->recirc_table;
4813 nac->alg = htons(conntrack->alg);
4815 len = ofpacts_put_openflow_actions(conntrack->actions,
4816 ofpact_ct_get_action_len(conntrack),
4818 len += sizeof(*nac);
4819 nac = ofpbuf_at(out, ofs, sizeof(*nac));
4820 nac->len = htons(len);
4823 static char * OVS_WARN_UNUSED_RESULT parse_NAT(char *arg, struct ofpbuf *,
4824 enum ofputil_protocol * OVS_UNUSED);
4826 /* Parses 'arg' as the argument to a "ct" action, and appends such an
4827 * action to 'ofpacts'.
4829 * Returns NULL if successful, otherwise a malloc()'d string describing the
4830 * error. The caller is responsible for freeing the returned string. */
4831 static char * OVS_WARN_UNUSED_RESULT
4832 parse_CT(char *arg, struct ofpbuf *ofpacts,
4833 enum ofputil_protocol *usable_protocols)
4835 const size_t ct_offset = ofpacts_pull(ofpacts);
4836 struct ofpact_conntrack *oc;
4840 oc = ofpact_put_CT(ofpacts);
4842 oc->recirc_table = NX_CT_RECIRC_NONE;
4843 while (ofputil_parse_key_value(&arg, &key, &value)) {
4844 if (!strcmp(key, "commit")) {
4845 oc->flags |= NX_CT_F_COMMIT;
4846 } else if (!strcmp(key, "table")) {
4847 error = str_to_u8(value, "recirc_table", &oc->recirc_table);
4848 if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
4849 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
4851 } else if (!strcmp(key, "zone")) {
4852 error = str_to_u16(value, "zone", &oc->zone_imm);
4856 error = mf_parse_subfield(&oc->zone_src, value);
4861 } else if (!strcmp(key, "alg")) {
4862 error = str_to_connhelper(value, &oc->alg);
4863 } else if (!strcmp(key, "nat")) {
4864 const size_t nat_offset = ofpacts_pull(ofpacts);
4866 error = parse_NAT(value, ofpacts, usable_protocols);
4867 ofpact_pad(ofpacts);
4868 /* Update CT action pointer and length. */
4869 ofpacts->header = ofpbuf_push_uninit(ofpacts, nat_offset);
4870 oc = ofpacts->header;
4871 } else if (!strcmp(key, "exec")) {
4872 /* Hide existing actions from ofpacts_parse_copy(), so the
4873 * nesting can be handled transparently. */
4874 enum ofputil_protocol usable_protocols2;
4875 const size_t exec_offset = ofpacts_pull(ofpacts);
4877 /* Initializes 'usable_protocol2', fold it back to
4878 * '*usable_protocols' afterwards, so that we do not lose
4879 * restrictions already in there. */
4880 error = ofpacts_parse_copy(value, ofpacts, &usable_protocols2,
4882 *usable_protocols &= usable_protocols2;
4883 ofpact_pad(ofpacts);
4884 ofpacts->header = ofpbuf_push_uninit(ofpacts, exec_offset);
4885 oc = ofpacts->header;
4887 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
4894 ofpact_update_len(ofpacts, &oc->ofpact);
4895 ofpbuf_push_uninit(ofpacts, ct_offset);
4900 format_alg(int port, struct ds *s)
4902 if (port == IPPORT_FTP) {
4903 ds_put_format(s, "alg=ftp,");
4905 ds_put_format(s, "alg=%d,", port);
4909 static void format_NAT(const struct ofpact_nat *a, struct ds *ds);
4912 format_CT(const struct ofpact_conntrack *a, struct ds *s)
4914 ds_put_cstr(s, "ct(");
4915 if (a->flags & NX_CT_F_COMMIT) {
4916 ds_put_cstr(s, "commit,");
4918 if (a->recirc_table != NX_CT_RECIRC_NONE) {
4919 ds_put_format(s, "table=%"PRIu8",", a->recirc_table);
4921 if (a->zone_src.field) {
4922 ds_put_format(s, "zone=");
4923 mf_format_subfield(&a->zone_src, s);
4924 ds_put_char(s, ',');
4925 } else if (a->zone_imm) {
4926 ds_put_format(s, "zone=%"PRIu16",", a->zone_imm);
4928 /* If the first action is a NAT action, format it outside of the 'exec'
4930 const struct ofpact *action = a->actions;
4931 size_t actions_len = ofpact_ct_get_action_len(a);
4932 if (actions_len && action->type == OFPACT_NAT) {
4933 format_NAT(ofpact_get_NAT(action), s);
4934 ds_put_char(s, ',');
4935 actions_len -= OFPACT_ALIGN(action->len);
4936 action = ofpact_next(action);
4939 ds_put_cstr(s, "exec(");
4940 ofpacts_format(action, actions_len, s);
4941 ds_put_cstr(s, "),");
4943 format_alg(a->alg, s);
4945 ds_put_char(s, ')');
4950 /* Which optional fields are present? */
4952 NX_NAT_RANGE_IPV4_MIN = 1 << 0, /* ovs_be32 */
4953 NX_NAT_RANGE_IPV4_MAX = 1 << 1, /* ovs_be32 */
4954 NX_NAT_RANGE_IPV6_MIN = 1 << 2, /* struct in6_addr */
4955 NX_NAT_RANGE_IPV6_MAX = 1 << 3, /* struct in6_addr */
4956 NX_NAT_RANGE_PROTO_MIN = 1 << 4, /* ovs_be16 */
4957 NX_NAT_RANGE_PROTO_MAX = 1 << 5, /* ovs_be16 */
4960 /* Action structure for NXAST_NAT. */
4961 struct nx_action_nat {
4962 ovs_be16 type; /* OFPAT_VENDOR. */
4963 ovs_be16 len; /* At least 16. */
4964 ovs_be32 vendor; /* NX_VENDOR_ID. */
4965 ovs_be16 subtype; /* NXAST_NAT. */
4966 uint8_t pad[2]; /* Must be zero. */
4967 ovs_be16 flags; /* Zero or more NX_NAT_F_* flags.
4968 * Unspecified flag bits must be zero. */
4969 ovs_be16 range_present; /* NX_NAT_RANGE_* */
4970 /* Followed by optional parameters as specified by 'range_present' */
4972 OFP_ASSERT(sizeof(struct nx_action_nat) == 16);
4975 encode_NAT(const struct ofpact_nat *nat,
4976 enum ofp_version ofp_version OVS_UNUSED,
4979 struct nx_action_nat *nan;
4980 const size_t ofs = out->size;
4981 uint16_t range_present = 0;
4983 nan = put_NXAST_NAT(out);
4984 nan->flags = htons(nat->flags);
4985 if (nat->range_af == AF_INET) {
4986 if (nat->range.addr.ipv4.min) {
4987 ovs_be32 *min = ofpbuf_put_uninit(out, sizeof *min);
4988 *min = nat->range.addr.ipv4.min;
4989 range_present |= NX_NAT_RANGE_IPV4_MIN;
4991 if (nat->range.addr.ipv4.max) {
4992 ovs_be32 *max = ofpbuf_put_uninit(out, sizeof *max);
4993 *max = nat->range.addr.ipv4.max;
4994 range_present |= NX_NAT_RANGE_IPV4_MAX;
4996 } else if (nat->range_af == AF_INET6) {
4997 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
4998 struct in6_addr *min = ofpbuf_put_uninit(out, sizeof *min);
4999 *min = nat->range.addr.ipv6.min;
5000 range_present |= NX_NAT_RANGE_IPV6_MIN;
5002 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.max)) {
5003 struct in6_addr *max = ofpbuf_put_uninit(out, sizeof *max);
5004 *max = nat->range.addr.ipv6.max;
5005 range_present |= NX_NAT_RANGE_IPV6_MAX;
5008 if (nat->range_af != AF_UNSPEC) {
5009 if (nat->range.proto.min) {
5010 ovs_be16 *min = ofpbuf_put_uninit(out, sizeof *min);
5011 *min = htons(nat->range.proto.min);
5012 range_present |= NX_NAT_RANGE_PROTO_MIN;
5014 if (nat->range.proto.max) {
5015 ovs_be16 *max = ofpbuf_put_uninit(out, sizeof *max);
5016 *max = htons(nat->range.proto.max);
5017 range_present |= NX_NAT_RANGE_PROTO_MAX;
5020 pad_ofpat(out, ofs);
5021 nan = ofpbuf_at(out, ofs, sizeof *nan);
5022 nan->range_present = htons(range_present);
5026 decode_NXAST_RAW_NAT(const struct nx_action_nat *nan,
5027 enum ofp_version ofp_version OVS_UNUSED,
5030 struct ofpact_nat *nat;
5031 uint16_t range_present = ntohs(nan->range_present);
5032 const char *opts = (char *)(nan + 1);
5033 uint16_t len = ntohs(nan->len) - sizeof *nan;
5035 nat = ofpact_put_NAT(out);
5036 nat->flags = ntohs(nan->flags);
5038 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
5039 (LEN >= sizeof(TYPE) \
5040 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
5041 SRC += sizeof(TYPE)) \
5044 nat->range_af = AF_UNSPEC;
5045 if (range_present & NX_NAT_RANGE_IPV4_MIN) {
5046 if (range_present & (NX_NAT_RANGE_IPV6_MIN | NX_NAT_RANGE_IPV6_MAX)) {
5047 return OFPERR_OFPBAC_BAD_ARGUMENT;
5050 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.min, opts, len, ovs_be32)
5051 || !nat->range.addr.ipv4.min) {
5052 return OFPERR_OFPBAC_BAD_ARGUMENT;
5055 nat->range_af = AF_INET;
5057 if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5058 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.max, opts, len,
5060 return OFPERR_OFPBAC_BAD_ARGUMENT;
5062 if (ntohl(nat->range.addr.ipv4.max)
5063 < ntohl(nat->range.addr.ipv4.min)) {
5064 return OFPERR_OFPBAC_BAD_ARGUMENT;
5067 } else if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5068 return OFPERR_OFPBAC_BAD_ARGUMENT;
5069 } else if (range_present & NX_NAT_RANGE_IPV6_MIN) {
5070 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.min, opts, len,
5072 || ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5073 return OFPERR_OFPBAC_BAD_ARGUMENT;
5076 nat->range_af = AF_INET6;
5078 if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5079 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.max, opts, len,
5081 return OFPERR_OFPBAC_BAD_ARGUMENT;
5083 if (memcmp(&nat->range.addr.ipv6.max, &nat->range.addr.ipv6.min,
5084 sizeof(struct in6_addr)) < 0) {
5085 return OFPERR_OFPBAC_BAD_ARGUMENT;
5088 } else if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5089 return OFPERR_OFPBAC_BAD_ARGUMENT;
5092 if (range_present & NX_NAT_RANGE_PROTO_MIN) {
5095 if (nat->range_af == AF_UNSPEC) {
5096 return OFPERR_OFPBAC_BAD_ARGUMENT;
5098 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16) || proto == 0) {
5099 return OFPERR_OFPBAC_BAD_ARGUMENT;
5101 nat->range.proto.min = ntohs(proto);
5102 if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5103 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16)) {
5104 return OFPERR_OFPBAC_BAD_ARGUMENT;
5106 nat->range.proto.max = ntohs(proto);
5107 if (nat->range.proto.max < nat->range.proto.min) {
5108 return OFPERR_OFPBAC_BAD_ARGUMENT;
5111 } else if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5112 return OFPERR_OFPBAC_BAD_ARGUMENT;
5119 format_NAT(const struct ofpact_nat *a, struct ds *ds)
5121 ds_put_cstr(ds, "nat");
5123 if (a->flags & (NX_NAT_F_SRC | NX_NAT_F_DST)) {
5124 ds_put_char(ds, '(');
5125 ds_put_cstr(ds, a->flags & NX_NAT_F_SRC ? "src" : "dst");
5127 if (a->range_af != AF_UNSPEC) {
5128 ds_put_cstr(ds, "=");
5130 if (a->range_af == AF_INET) {
5131 ds_put_format(ds, IP_FMT, IP_ARGS(a->range.addr.ipv4.min));
5133 if (a->range.addr.ipv4.max
5134 && a->range.addr.ipv4.max != a->range.addr.ipv4.min) {
5135 ds_put_format(ds, "-"IP_FMT,
5136 IP_ARGS(a->range.addr.ipv4.max));
5138 } else if (a->range_af == AF_INET6) {
5139 ipv6_format_addr_bracket(&a->range.addr.ipv6.min, ds,
5140 a->range.proto.min);
5142 if (!ipv6_mask_is_any(&a->range.addr.ipv6.max)
5143 && memcmp(&a->range.addr.ipv6.max, &a->range.addr.ipv6.min,
5144 sizeof(struct in6_addr)) != 0) {
5145 ds_put_char(ds, '-');
5146 ipv6_format_addr_bracket(&a->range.addr.ipv6.max, ds,
5147 a->range.proto.min);
5150 if (a->range.proto.min) {
5151 ds_put_char(ds, ':');
5152 ds_put_format(ds, "%"PRIu16, a->range.proto.min);
5154 if (a->range.proto.max
5155 && a->range.proto.max != a->range.proto.min) {
5156 ds_put_format(ds, "-%"PRIu16, a->range.proto.max);
5159 ds_put_char(ds, ',');
5161 if (a->flags & NX_NAT_F_PERSISTENT) {
5162 ds_put_cstr(ds, "persistent,");
5164 if (a->flags & NX_NAT_F_PROTO_HASH) {
5165 ds_put_cstr(ds, "hash,");
5167 if (a->flags & NX_NAT_F_PROTO_RANDOM) {
5168 ds_put_cstr(ds, "random,");
5172 ds_put_char(ds, ')');
5176 static char * OVS_WARN_UNUSED_RESULT
5177 str_to_nat_range(const char *s, struct ofpact_nat *on)
5179 char ipv6_s[IPV6_SCAN_LEN + 1];
5182 on->range_af = AF_UNSPEC;
5183 if (ovs_scan_len(s, &n, IP_SCAN_FMT,
5184 IP_SCAN_ARGS(&on->range.addr.ipv4.min))) {
5185 on->range_af = AF_INET;
5189 if (!ovs_scan_len(s, &n, IP_SCAN_FMT,
5190 IP_SCAN_ARGS(&on->range.addr.ipv4.max))
5191 || (ntohl(on->range.addr.ipv4.max)
5192 < ntohl(on->range.addr.ipv4.min))) {
5196 } else if ((ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5197 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5198 && inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.min) == 1) {
5199 on->range_af = AF_INET6;
5203 if (!(ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5204 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5205 || inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.max) != 1
5206 || memcmp(&on->range.addr.ipv6.max, &on->range.addr.ipv6.min,
5207 sizeof on->range.addr.ipv6.max) < 0) {
5212 if (on->range_af != AF_UNSPEC && s[n] == ':') {
5214 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.min)) {
5219 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.max)
5220 || on->range.proto.max < on->range.proto.min) {
5225 if (strlen(s) != n) {
5226 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
5231 return xasprintf("invalid nat range \"%s\"", s);
5235 /* Parses 'arg' as the argument to a "nat" action, and appends such an
5236 * action to 'ofpacts'.
5238 * Returns NULL if successful, otherwise a malloc()'d string describing the
5239 * error. The caller is responsible for freeing the returned string. */
5240 static char * OVS_WARN_UNUSED_RESULT
5241 parse_NAT(char *arg, struct ofpbuf *ofpacts,
5242 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5244 struct ofpact_nat *on = ofpact_put_NAT(ofpacts);
5248 on->range_af = AF_UNSPEC;
5250 while (ofputil_parse_key_value(&arg, &key, &value)) {
5253 if (!strcmp(key, "src")) {
5254 on->flags |= NX_NAT_F_SRC;
5255 error = str_to_nat_range(value, on);
5256 } else if (!strcmp(key, "dst")) {
5257 on->flags |= NX_NAT_F_DST;
5258 error = str_to_nat_range(value, on);
5259 } else if (!strcmp(key, "persistent")) {
5260 on->flags |= NX_NAT_F_PERSISTENT;
5261 } else if (!strcmp(key, "hash")) {
5262 on->flags |= NX_NAT_F_PROTO_HASH;
5263 } else if (!strcmp(key, "random")) {
5264 on->flags |= NX_NAT_F_PROTO_RANDOM;
5266 error = xasprintf("invalid key \"%s\" in \"nat\" argument",
5273 if (on->flags & NX_NAT_F_SRC && on->flags & NX_NAT_F_DST) {
5274 return xasprintf("May only specify one of \"snat\" or \"dnat\".");
5276 if (!(on->flags & NX_NAT_F_SRC || on->flags & NX_NAT_F_DST)) {
5278 return xasprintf("Flags allowed only with \"snat\" or \"dnat\".");
5280 if (on->range_af != AF_UNSPEC) {
5281 return xasprintf("Range allowed only with \"snat\" or \"dnat\".");
5288 /* Meter instruction. */
5291 encode_METER(const struct ofpact_meter *meter,
5292 enum ofp_version ofp_version, struct ofpbuf *out)
5294 if (ofp_version >= OFP13_VERSION) {
5295 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
5299 static char * OVS_WARN_UNUSED_RESULT
5300 parse_METER(char *arg, struct ofpbuf *ofpacts,
5301 enum ofputil_protocol *usable_protocols)
5303 *usable_protocols &= OFPUTIL_P_OF13_UP;
5304 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
5308 format_METER(const struct ofpact_meter *a, struct ds *s)
5310 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
5313 /* Clear-Actions instruction. */
5316 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
5317 enum ofp_version ofp_version OVS_UNUSED,
5318 struct ofpbuf *out OVS_UNUSED)
5320 if (ofp_version > OFP10_VERSION) {
5321 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
5325 static char * OVS_WARN_UNUSED_RESULT
5326 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
5327 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5329 ofpact_put_CLEAR_ACTIONS(ofpacts);
5334 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
5336 ds_put_cstr(s, "clear_actions");
5339 /* Write-Actions instruction. */
5342 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
5343 enum ofp_version ofp_version, struct ofpbuf *out)
5345 if (ofp_version > OFP10_VERSION) {
5346 const size_t ofs = out->size;
5348 instruction_put_OFPIT11_WRITE_ACTIONS(out);
5349 ofpacts_put_openflow_actions(actions->actions,
5350 ofpact_nest_get_action_len(actions),
5352 ofpacts_update_instruction_actions(out, ofs);
5356 static char * OVS_WARN_UNUSED_RESULT
5357 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
5358 enum ofputil_protocol *usable_protocols)
5360 size_t ofs = ofpacts_pull(ofpacts);
5361 struct ofpact_nest *on;
5364 /* Add a Write-Actions instruction and then pull it off. */
5365 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
5366 ofpbuf_pull(ofpacts, sizeof *on);
5368 /* Parse nested actions.
5370 * We pulled off "write-actions" and the previous actions because the
5371 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
5372 * that it doesn't actually include the nested actions. That means that
5373 * ofpacts_parse() would reject them as being part of an Apply-Actions that
5374 * follows a Write-Actions, which is an invalid order. */
5375 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
5376 OFPACT_WRITE_ACTIONS);
5378 /* Put the Write-Actions back on and update its length. */
5379 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
5380 on->ofpact.len = ofpacts->size;
5382 /* Put any previous actions or instructions back on. */
5383 ofpbuf_push_uninit(ofpacts, ofs);
5389 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
5391 ds_put_cstr(s, "write_actions(");
5392 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
5393 ds_put_char(s, ')');
5396 /* Action structure for NXAST_WRITE_METADATA.
5398 * Modifies the 'mask' bits of the metadata value. */
5399 struct nx_action_write_metadata {
5400 ovs_be16 type; /* OFPAT_VENDOR. */
5401 ovs_be16 len; /* Length is 32. */
5402 ovs_be32 vendor; /* NX_VENDOR_ID. */
5403 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
5404 uint8_t zeros[6]; /* Must be zero. */
5405 ovs_be64 metadata; /* Metadata register. */
5406 ovs_be64 mask; /* Metadata mask. */
5408 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
5411 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
5412 enum ofp_version ofp_version OVS_UNUSED,
5415 struct ofpact_metadata *om;
5417 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
5418 return OFPERR_NXBRC_MUST_BE_ZERO;
5421 om = ofpact_put_WRITE_METADATA(out);
5422 om->metadata = nawm->metadata;
5423 om->mask = nawm->mask;
5429 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
5430 enum ofp_version ofp_version, struct ofpbuf *out)
5432 if (ofp_version == OFP10_VERSION) {
5433 struct nx_action_write_metadata *nawm;
5435 nawm = put_NXAST_WRITE_METADATA(out);
5436 nawm->metadata = metadata->metadata;
5437 nawm->mask = metadata->mask;
5439 struct ofp11_instruction_write_metadata *oiwm;
5441 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
5442 oiwm->metadata = metadata->metadata;
5443 oiwm->metadata_mask = metadata->mask;
5447 static char * OVS_WARN_UNUSED_RESULT
5448 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
5449 enum ofputil_protocol *usable_protocols)
5451 struct ofpact_metadata *om;
5452 char *mask = strchr(arg, '/');
5454 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
5456 om = ofpact_put_WRITE_METADATA(ofpacts);
5461 error = str_to_be64(mask + 1, &om->mask);
5466 om->mask = OVS_BE64_MAX;
5469 return str_to_be64(arg, &om->metadata);
5473 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5475 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
5476 if (a->mask != OVS_BE64_MAX) {
5477 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5481 /* Goto-Table instruction. */
5484 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5485 enum ofp_version ofp_version, struct ofpbuf *out)
5487 if (ofp_version == OFP10_VERSION) {
5488 struct nx_action_resubmit *nar;
5490 nar = put_NXAST_RESUBMIT_TABLE(out);
5491 nar->table = goto_table->table_id;
5492 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5494 struct ofp11_instruction_goto_table *oigt;
5496 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5497 oigt->table_id = goto_table->table_id;
5498 memset(oigt->pad, 0, sizeof oigt->pad);
5502 static char * OVS_WARN_UNUSED_RESULT
5503 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5504 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5506 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5507 char *table_s = strsep(&arg, ",");
5508 if (!table_s || !table_s[0]) {
5509 return xstrdup("instruction goto-table needs table id");
5511 return str_to_u8(table_s, "table", &ogt->table_id);
5515 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5517 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
5521 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
5522 const struct ofp_action_header *bad_action, enum ofperr error)
5524 if (!VLOG_DROP_WARN(&rl)) {
5528 ds_put_hex_dump(&s, actions, actions_len, 0, false);
5529 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5530 (char *)bad_action - (char *)actions,
5531 ofperr_get_name(error), ds_cstr(&s));
5537 ofpacts_decode(const void *actions, size_t actions_len,
5538 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5540 struct ofpbuf openflow;
5542 ofpbuf_use_const(&openflow, actions, actions_len);
5543 while (openflow.size) {
5544 const struct ofp_action_header *action = openflow.data;
5545 enum ofp_raw_action_type raw;
5549 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5551 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
5555 log_bad_action(actions, actions_len, action, error);
5560 ofpact_pad(ofpacts);
5565 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5566 unsigned int actions_len,
5567 enum ofp_version version,
5568 uint32_t allowed_ovsinsts,
5569 struct ofpbuf *ofpacts,
5570 enum ofpact_type outer_action)
5572 const struct ofp_action_header *actions;
5575 if (!outer_action) {
5576 ofpbuf_clear(ofpacts);
5579 if (actions_len % OFP_ACTION_ALIGN != 0) {
5580 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5581 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5582 return OFPERR_OFPBRC_BAD_LEN;
5585 actions = ofpbuf_try_pull(openflow, actions_len);
5586 if (actions == NULL) {
5587 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
5588 "remaining message length (%"PRIu32")",
5589 actions_len, openflow->size);
5590 return OFPERR_OFPBRC_BAD_LEN;
5593 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5595 ofpbuf_clear(ofpacts);
5599 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
5602 ofpbuf_clear(ofpacts);
5607 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
5608 * front of 'openflow' into ofpacts. On success, replaces any existing content
5609 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
5610 * Returns 0 if successful, otherwise an OpenFlow error.
5612 * Actions are processed according to their OpenFlow version which
5613 * is provided in the 'version' parameter.
5615 * In most places in OpenFlow, actions appear encapsulated in instructions, so
5616 * you should call ofpacts_pull_openflow_instructions() instead of this
5619 * The parsed actions are valid generically, but they may not be valid in a
5620 * specific context. For example, port numbers up to OFPP_MAX are valid
5621 * generically, but specific datapaths may only support port numbers in a
5622 * smaller range. Use ofpacts_check() to additional check whether actions are
5623 * valid in a specific context. */
5625 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
5626 unsigned int actions_len,
5627 enum ofp_version version,
5628 struct ofpbuf *ofpacts)
5630 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
5631 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
5635 /* OpenFlow 1.1 actions. */
5638 /* True if an action sets the value of a field
5639 * in a way that is compatibile with the action set.
5640 * The field can be set via either a set or a move action.
5641 * False otherwise. */
5643 ofpact_is_set_or_move_action(const struct ofpact *a)
5646 case OFPACT_SET_FIELD:
5647 case OFPACT_REG_MOVE:
5648 case OFPACT_SET_ETH_DST:
5649 case OFPACT_SET_ETH_SRC:
5650 case OFPACT_SET_IP_DSCP:
5651 case OFPACT_SET_IP_ECN:
5652 case OFPACT_SET_IP_TTL:
5653 case OFPACT_SET_IPV4_DST:
5654 case OFPACT_SET_IPV4_SRC:
5655 case OFPACT_SET_L4_DST_PORT:
5656 case OFPACT_SET_L4_SRC_PORT:
5657 case OFPACT_SET_MPLS_LABEL:
5658 case OFPACT_SET_MPLS_TC:
5659 case OFPACT_SET_MPLS_TTL:
5660 case OFPACT_SET_QUEUE:
5661 case OFPACT_SET_TUNNEL:
5662 case OFPACT_SET_VLAN_PCP:
5663 case OFPACT_SET_VLAN_VID:
5666 case OFPACT_CLEAR_ACTIONS:
5669 case OFPACT_CONTROLLER:
5670 case OFPACT_DEC_MPLS_TTL:
5671 case OFPACT_DEC_TTL:
5672 case OFPACT_ENQUEUE:
5674 case OFPACT_UNROLL_XLATE:
5675 case OFPACT_FIN_TIMEOUT:
5676 case OFPACT_GOTO_TABLE:
5679 case OFPACT_CONJUNCTION:
5681 case OFPACT_MULTIPATH:
5684 case OFPACT_OUTPUT_REG:
5685 case OFPACT_POP_MPLS:
5686 case OFPACT_POP_QUEUE:
5687 case OFPACT_PUSH_MPLS:
5688 case OFPACT_PUSH_VLAN:
5689 case OFPACT_RESUBMIT:
5691 case OFPACT_STACK_POP:
5692 case OFPACT_STACK_PUSH:
5693 case OFPACT_STRIP_VLAN:
5694 case OFPACT_WRITE_ACTIONS:
5695 case OFPACT_WRITE_METADATA:
5696 case OFPACT_DEBUG_RECIRC:
5703 /* True if an action is allowed in the action set.
5704 * False otherwise. */
5706 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
5709 case OFPACT_DEC_MPLS_TTL:
5710 case OFPACT_DEC_TTL:
5713 case OFPACT_POP_MPLS:
5714 case OFPACT_PUSH_MPLS:
5715 case OFPACT_PUSH_VLAN:
5716 case OFPACT_REG_MOVE:
5717 case OFPACT_SET_FIELD:
5718 case OFPACT_SET_ETH_DST:
5719 case OFPACT_SET_ETH_SRC:
5720 case OFPACT_SET_IP_DSCP:
5721 case OFPACT_SET_IP_ECN:
5722 case OFPACT_SET_IP_TTL:
5723 case OFPACT_SET_IPV4_DST:
5724 case OFPACT_SET_IPV4_SRC:
5725 case OFPACT_SET_L4_DST_PORT:
5726 case OFPACT_SET_L4_SRC_PORT:
5727 case OFPACT_SET_MPLS_LABEL:
5728 case OFPACT_SET_MPLS_TC:
5729 case OFPACT_SET_MPLS_TTL:
5730 case OFPACT_SET_QUEUE:
5731 case OFPACT_SET_TUNNEL:
5732 case OFPACT_SET_VLAN_PCP:
5733 case OFPACT_SET_VLAN_VID:
5734 case OFPACT_STRIP_VLAN:
5737 /* In general these actions are excluded because they are not part of
5738 * the OpenFlow specification nor map to actions that are defined in
5739 * the specification. Thus the order in which they should be applied
5740 * in the action set is undefined. */
5742 case OFPACT_CONTROLLER:
5745 case OFPACT_ENQUEUE:
5747 case OFPACT_UNROLL_XLATE:
5748 case OFPACT_FIN_TIMEOUT:
5750 case OFPACT_CONJUNCTION:
5751 case OFPACT_MULTIPATH:
5753 case OFPACT_OUTPUT_REG:
5754 case OFPACT_POP_QUEUE:
5755 case OFPACT_RESUBMIT:
5757 case OFPACT_STACK_POP:
5758 case OFPACT_STACK_PUSH:
5759 case OFPACT_DEBUG_RECIRC:
5761 /* The action set may only include actions and thus
5762 * may not include any instructions */
5763 case OFPACT_CLEAR_ACTIONS:
5764 case OFPACT_GOTO_TABLE:
5766 case OFPACT_WRITE_ACTIONS:
5767 case OFPACT_WRITE_METADATA:
5774 /* Append ofpact 'a' onto the tail of 'out' */
5776 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
5778 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
5781 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
5783 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
5784 enum ofpact_type filter)
5786 const struct ofpact *target;
5787 const struct ofpact *a;
5790 OFPACT_FOR_EACH (a, in->data, in->size) {
5791 if (a->type == filter) {
5796 ofpact_copy(out, target);
5798 return target != NULL;
5801 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5802 * The order of appended ofpacts is preserved between 'in' and 'out' */
5804 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5805 bool (*filter)(const struct ofpact *))
5807 const struct ofpact *a;
5809 OFPACT_FOR_EACH (a, in->data, in->size) {
5811 ofpact_copy(out, a);
5816 /* Reads 'action_set', which contains ofpacts accumulated by
5817 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5818 * executed directly into 'action_list'. (These names correspond to the
5819 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5821 * In general this involves appending the last instance of each action that is
5822 * admissible in the action set in the order described in the OpenFlow
5826 * + output action is only appended if no group action was present in 'in'.
5827 * + As a simplification all set actions are copied in the order the are
5828 * provided in 'in' as many set actions applied to a field has the same
5829 * affect as only applying the last action that sets a field and
5830 * duplicates are removed by do_xlate_actions().
5831 * This has an unwanted side-effect of compsoting multiple
5832 * LOAD_REG actions that touch different regions of the same field. */
5834 ofpacts_execute_action_set(struct ofpbuf *action_list,
5835 const struct ofpbuf *action_set)
5837 /* The OpenFlow spec "Action Set" section specifies this order. */
5838 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5839 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5840 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5841 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5842 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5843 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5844 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5845 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5847 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5848 * we should execute only OFPACT_GROUP.
5850 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5851 * all the actions because there's no point in modifying a packet that will
5852 * not be sent anywhere. */
5853 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5854 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5855 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5856 ofpbuf_clear(action_list);
5862 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5863 size_t n_in, enum ofp_version version,
5868 size_t start = out->size;
5870 error = ofpacts_decode(in, n_in, version, out);
5876 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5877 if (!ofpact_is_allowed_in_actions_set(a)) {
5878 VLOG_WARN_RL(&rl, "disallowed action in action set");
5879 return OFPERR_OFPBAC_BAD_TYPE;
5886 /* OpenFlow 1.1 instructions. */
5888 struct instruction_type_info {
5889 enum ovs_instruction_type type;
5893 static const struct instruction_type_info inst_info[] = {
5894 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5900 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5902 return inst_info[type].name;
5906 ovs_instruction_type_from_name(const char *name)
5908 const struct instruction_type_info *p;
5909 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5910 if (!strcasecmp(name, p->name)) {
5917 enum ovs_instruction_type
5918 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5922 return OVSINST_OFPIT13_METER;
5923 case OFPACT_CLEAR_ACTIONS:
5924 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5925 case OFPACT_WRITE_ACTIONS:
5926 return OVSINST_OFPIT11_WRITE_ACTIONS;
5927 case OFPACT_WRITE_METADATA:
5928 return OVSINST_OFPIT11_WRITE_METADATA;
5929 case OFPACT_GOTO_TABLE:
5930 return OVSINST_OFPIT11_GOTO_TABLE;
5933 case OFPACT_CONTROLLER:
5934 case OFPACT_ENQUEUE:
5935 case OFPACT_OUTPUT_REG:
5937 case OFPACT_SET_VLAN_VID:
5938 case OFPACT_SET_VLAN_PCP:
5939 case OFPACT_STRIP_VLAN:
5940 case OFPACT_PUSH_VLAN:
5941 case OFPACT_SET_ETH_SRC:
5942 case OFPACT_SET_ETH_DST:
5943 case OFPACT_SET_IPV4_SRC:
5944 case OFPACT_SET_IPV4_DST:
5945 case OFPACT_SET_IP_DSCP:
5946 case OFPACT_SET_IP_ECN:
5947 case OFPACT_SET_IP_TTL:
5948 case OFPACT_SET_L4_SRC_PORT:
5949 case OFPACT_SET_L4_DST_PORT:
5950 case OFPACT_REG_MOVE:
5951 case OFPACT_SET_FIELD:
5952 case OFPACT_STACK_PUSH:
5953 case OFPACT_STACK_POP:
5954 case OFPACT_DEC_TTL:
5955 case OFPACT_SET_MPLS_LABEL:
5956 case OFPACT_SET_MPLS_TC:
5957 case OFPACT_SET_MPLS_TTL:
5958 case OFPACT_DEC_MPLS_TTL:
5959 case OFPACT_PUSH_MPLS:
5960 case OFPACT_POP_MPLS:
5961 case OFPACT_SET_TUNNEL:
5962 case OFPACT_SET_QUEUE:
5963 case OFPACT_POP_QUEUE:
5964 case OFPACT_FIN_TIMEOUT:
5965 case OFPACT_RESUBMIT:
5967 case OFPACT_CONJUNCTION:
5968 case OFPACT_MULTIPATH:
5971 case OFPACT_UNROLL_XLATE:
5973 case OFPACT_DEBUG_RECIRC:
5977 return OVSINST_OFPIT11_APPLY_ACTIONS;
5982 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
5983 const uint16_t inst_type)
5985 switch (inst_type) {
5987 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5989 *instruction_type = OVSINST_##ENUM; \
5995 return OFPERR_OFPBIC_UNKNOWN_INST;
5999 /* Two-way translation between OVS's internal "OVSINST_*" representation of
6000 * instructions and the "OFPIT_*" representation used in OpenFlow. */
6001 struct ovsinst_map {
6002 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
6003 int ofpit; /* OFPIT_* number from OpenFlow spec. */
6006 static const struct ovsinst_map *
6007 get_ovsinst_map(enum ofp_version version)
6009 /* OpenFlow 1.1 and 1.2 instructions. */
6010 static const struct ovsinst_map of11[] = {
6011 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6012 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6013 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6014 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6015 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6019 /* OpenFlow 1.3+ instructions. */
6020 static const struct ovsinst_map of13[] = {
6021 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6022 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6023 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6024 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6025 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6026 { OVSINST_OFPIT13_METER, 6 },
6030 return version < OFP13_VERSION ? of11 : of13;
6033 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
6034 * values, into a bitmap of instructions suitable for OpenFlow 'version'
6035 * (OFP11_VERSION or later), and returns the result. */
6037 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
6039 uint32_t ofpit_bitmap = 0;
6040 const struct ovsinst_map *x;
6042 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6043 if (ovsinst_bitmap & (1u << x->ovsinst)) {
6044 ofpit_bitmap |= 1u << x->ofpit;
6047 return htonl(ofpit_bitmap);
6050 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
6051 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
6052 * correspond to OVSINST_* values, and returns the result. */
6054 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
6056 uint32_t ovsinst_bitmap = 0;
6057 const struct ovsinst_map *x;
6059 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6060 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
6061 ovsinst_bitmap |= 1u << x->ovsinst;
6064 return ovsinst_bitmap;
6067 static inline struct ofp11_instruction *
6068 instruction_next(const struct ofp11_instruction *inst)
6070 return ((struct ofp11_instruction *) (void *)
6071 ((uint8_t *) inst + ntohs(inst->len)));
6075 instruction_is_valid(const struct ofp11_instruction *inst,
6076 size_t n_instructions)
6078 uint16_t len = ntohs(inst->len);
6079 return (!(len % OFP11_INSTRUCTION_ALIGN)
6080 && len >= sizeof *inst
6081 && len / sizeof *inst <= n_instructions);
6084 /* This macro is careful to check for instructions with bad lengths. */
6085 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
6086 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
6087 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
6088 ((LEFT) -= (ntohs((ITER)->len) \
6089 / sizeof(struct ofp11_instruction)), \
6090 (ITER) = instruction_next(ITER)))
6093 decode_openflow11_instruction(const struct ofp11_instruction *inst,
6094 enum ovs_instruction_type *type)
6096 uint16_t len = ntohs(inst->len);
6098 switch (inst->type) {
6099 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
6100 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
6102 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6103 case CONSTANT_HTONS(ENUM): \
6105 ? len >= sizeof(struct STRUCT) \
6106 : len == sizeof(struct STRUCT)) { \
6107 *type = OVSINST_##ENUM; \
6110 return OFPERR_OFPBIC_BAD_LEN; \
6116 return OFPERR_OFPBIC_UNKNOWN_INST;
6121 decode_openflow11_instructions(const struct ofp11_instruction insts[],
6123 const struct ofp11_instruction *out[])
6125 const struct ofp11_instruction *inst;
6128 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
6129 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
6130 enum ovs_instruction_type type;
6133 error = decode_openflow11_instruction(inst, &type);
6139 return OFPERR_OFPBIC_DUP_INST;
6145 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
6146 (n_insts - left) * sizeof *inst);
6147 return OFPERR_OFPBIC_BAD_LEN;
6153 get_actions_from_instruction(const struct ofp11_instruction *inst,
6154 const struct ofp_action_header **actions,
6155 size_t *actions_len)
6157 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
6158 *actions_len = ntohs(inst->len) - sizeof *inst;
6162 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
6163 unsigned int instructions_len,
6164 enum ofp_version version,
6165 struct ofpbuf *ofpacts)
6167 const struct ofp11_instruction *instructions;
6168 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
6171 if (version == OFP10_VERSION) {
6172 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
6174 (1u << N_OVS_INSTRUCTIONS) - 1,
6178 ofpbuf_clear(ofpacts);
6180 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
6181 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
6183 instructions_len, OFP11_INSTRUCTION_ALIGN);
6184 error = OFPERR_OFPBIC_BAD_LEN;
6188 instructions = ofpbuf_try_pull(openflow, instructions_len);
6189 if (instructions == NULL) {
6190 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
6191 "remaining message length (%"PRIu32")",
6192 instructions_len, openflow->size);
6193 error = OFPERR_OFPBIC_BAD_LEN;
6197 error = decode_openflow11_instructions(
6198 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
6204 if (insts[OVSINST_OFPIT13_METER]) {
6205 const struct ofp13_instruction_meter *oim;
6206 struct ofpact_meter *om;
6208 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
6209 insts[OVSINST_OFPIT13_METER]);
6211 om = ofpact_put_METER(ofpacts);
6212 om->meter_id = ntohl(oim->meter_id);
6214 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
6215 const struct ofp_action_header *actions;
6218 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
6219 &actions, &actions_len);
6220 error = ofpacts_decode(actions, actions_len, version, ofpacts);
6225 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
6226 instruction_get_OFPIT11_CLEAR_ACTIONS(
6227 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
6228 ofpact_put_CLEAR_ACTIONS(ofpacts);
6230 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
6231 struct ofpact_nest *on;
6232 const struct ofp_action_header *actions;
6236 ofpact_pad(ofpacts);
6237 start = ofpacts->size;
6238 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
6239 offsetof(struct ofpact_nest, actions));
6240 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
6241 &actions, &actions_len);
6242 error = ofpacts_decode_for_action_set(actions, actions_len,
6247 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
6248 on->ofpact.len = ofpacts->size - start;
6250 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
6251 const struct ofp11_instruction_write_metadata *oiwm;
6252 struct ofpact_metadata *om;
6254 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
6255 insts[OVSINST_OFPIT11_WRITE_METADATA]);
6257 om = ofpact_put_WRITE_METADATA(ofpacts);
6258 om->metadata = oiwm->metadata;
6259 om->mask = oiwm->metadata_mask;
6261 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
6262 const struct ofp11_instruction_goto_table *oigt;
6263 struct ofpact_goto_table *ogt;
6265 oigt = instruction_get_OFPIT11_GOTO_TABLE(
6266 insts[OVSINST_OFPIT11_GOTO_TABLE]);
6267 ogt = ofpact_put_GOTO_TABLE(ofpacts);
6268 ogt->table_id = oigt->table_id;
6271 error = ofpacts_verify(ofpacts->data, ofpacts->size,
6272 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
6275 ofpbuf_clear(ofpacts);
6280 /* Update the length of the instruction that begins at offset 'ofs' within
6281 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
6282 * If the instruction contains no nested actions, deletes it entirely. */
6284 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
6286 struct ofp11_instruction_actions *oia;
6288 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
6289 if (openflow->size > ofs + sizeof *oia) {
6290 oia->len = htons(openflow->size - ofs);
6292 openflow->size = ofs;
6296 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
6297 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
6298 * valid, otherwise an OpenFlow error code. */
6300 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
6308 case OFPP_CONTROLLER:
6313 return OFPERR_OFPBAC_BAD_OUT_PORT;
6316 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
6319 return OFPERR_OFPBAC_BAD_OUT_PORT;
6323 /* Removes the protocols that require consistency between match and actions
6324 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
6326 * (An example of an inconsistency between match and actions is a flow that
6327 * does not match on an MPLS Ethertype but has an action that pops an MPLS
6330 inconsistent_match(enum ofputil_protocol *usable_protocols)
6332 *usable_protocols &= OFPUTIL_P_OF10_ANY;
6335 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
6336 * caller must restore them.
6338 * Modifies some actions, filling in fields that could not be properly set
6339 * without context. */
6341 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
6342 struct flow *flow, ofp_port_t max_ports,
6343 uint8_t table_id, uint8_t n_tables)
6345 const struct ofpact_enqueue *enqueue;
6346 const struct mf_field *mf;
6350 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
6353 case OFPACT_CONTROLLER:
6356 case OFPACT_ENQUEUE:
6357 enqueue = ofpact_get_ENQUEUE(a);
6358 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
6359 && enqueue->port != OFPP_IN_PORT
6360 && enqueue->port != OFPP_LOCAL) {
6361 return OFPERR_OFPBAC_BAD_OUT_PORT;
6365 case OFPACT_OUTPUT_REG:
6366 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
6369 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
6371 case OFPACT_SET_VLAN_VID:
6372 /* Remember if we saw a vlan tag in the flow to aid translating to
6373 * OpenFlow 1.1+ if need be. */
6374 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
6375 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6376 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6377 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
6378 inconsistent_match(usable_protocols);
6380 /* Temporary mark that we have a vlan tag. */
6381 flow->vlan_tci |= htons(VLAN_CFI);
6384 case OFPACT_SET_VLAN_PCP:
6385 /* Remember if we saw a vlan tag in the flow to aid translating to
6386 * OpenFlow 1.1+ if need be. */
6387 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
6388 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6389 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6390 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
6391 inconsistent_match(usable_protocols);
6393 /* Temporary mark that we have a vlan tag. */
6394 flow->vlan_tci |= htons(VLAN_CFI);
6397 case OFPACT_STRIP_VLAN:
6398 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
6399 inconsistent_match(usable_protocols);
6401 /* Temporary mark that we have no vlan tag. */
6402 flow->vlan_tci = htons(0);
6405 case OFPACT_PUSH_VLAN:
6406 if (flow->vlan_tci & htons(VLAN_CFI)) {
6407 /* Multiple VLAN headers not supported. */
6408 return OFPERR_OFPBAC_BAD_TAG;
6410 /* Temporary mark that we have a vlan tag. */
6411 flow->vlan_tci |= htons(VLAN_CFI);
6414 case OFPACT_SET_ETH_SRC:
6415 case OFPACT_SET_ETH_DST:
6418 case OFPACT_SET_IPV4_SRC:
6419 case OFPACT_SET_IPV4_DST:
6420 if (flow->dl_type != htons(ETH_TYPE_IP)) {
6421 inconsistent_match(usable_protocols);
6425 case OFPACT_SET_IP_DSCP:
6426 case OFPACT_SET_IP_ECN:
6427 case OFPACT_SET_IP_TTL:
6428 case OFPACT_DEC_TTL:
6429 if (!is_ip_any(flow)) {
6430 inconsistent_match(usable_protocols);
6434 case OFPACT_SET_L4_SRC_PORT:
6435 case OFPACT_SET_L4_DST_PORT:
6436 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
6437 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
6438 && flow->nw_proto != IPPROTO_SCTP)) {
6439 inconsistent_match(usable_protocols);
6441 /* Note on which transport protocol the port numbers are set.
6442 * This allows this set action to be converted to an OF1.2 set field
6444 if (a->type == OFPACT_SET_L4_SRC_PORT) {
6445 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
6447 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
6451 case OFPACT_REG_MOVE:
6452 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
6454 case OFPACT_SET_FIELD:
6455 mf = ofpact_get_SET_FIELD(a)->field;
6456 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6457 if (!mf_are_prereqs_ok(mf, flow) ||
6458 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
6459 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
6461 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6463 /* Remember if we saw a vlan tag in the flow to aid translating to
6464 * OpenFlow 1.1 if need be. */
6465 ofpact_get_SET_FIELD(a)->flow_has_vlan =
6466 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6467 if (mf->id == MFF_VLAN_TCI) {
6468 /* The set field may add or remove the vlan tag,
6469 * Mark the status temporarily. */
6470 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
6474 case OFPACT_STACK_PUSH:
6475 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
6477 case OFPACT_STACK_POP:
6478 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6480 case OFPACT_SET_MPLS_LABEL:
6481 case OFPACT_SET_MPLS_TC:
6482 case OFPACT_SET_MPLS_TTL:
6483 case OFPACT_DEC_MPLS_TTL:
6484 if (!eth_type_mpls(flow->dl_type)) {
6485 inconsistent_match(usable_protocols);
6489 case OFPACT_SET_TUNNEL:
6490 case OFPACT_SET_QUEUE:
6491 case OFPACT_POP_QUEUE:
6492 case OFPACT_RESUBMIT:
6495 case OFPACT_FIN_TIMEOUT:
6496 if (flow->nw_proto != IPPROTO_TCP) {
6497 inconsistent_match(usable_protocols);
6502 return learn_check(ofpact_get_LEARN(a), flow);
6504 case OFPACT_CONJUNCTION:
6507 case OFPACT_MULTIPATH:
6508 return multipath_check(ofpact_get_MULTIPATH(a), flow);
6514 case OFPACT_PUSH_MPLS:
6515 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6516 /* The packet is now MPLS and the MPLS payload is opaque.
6517 * Thus nothing can be assumed about the network protocol.
6518 * Temporarily mark that we have no nw_proto. */
6522 case OFPACT_POP_MPLS:
6523 if (!eth_type_mpls(flow->dl_type)) {
6524 inconsistent_match(usable_protocols);
6526 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6533 struct ofpact_conntrack *oc = ofpact_get_CT(a);
6536 if (!dl_type_is_ip_any(flow->dl_type)
6537 || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6538 inconsistent_match(usable_protocols);
6541 if (oc->zone_src.field) {
6542 return mf_check_src(&oc->zone_src, flow);
6545 err = ofpacts_check(oc->actions, ofpact_ct_get_action_len(oc),
6546 flow, max_ports, table_id, n_tables,
6552 struct ofpact_nat *on = ofpact_get_NAT(a);
6554 if (!dl_type_is_ip_any(flow->dl_type) ||
6555 (on->range_af == AF_INET && flow->dl_type != htons(ETH_TYPE_IP)) ||
6556 (on->range_af == AF_INET6
6557 && flow->dl_type != htons(ETH_TYPE_IPV6))) {
6558 inconsistent_match(usable_protocols);
6563 case OFPACT_CLEAR_ACTIONS:
6566 case OFPACT_WRITE_ACTIONS: {
6567 /* Use a temporary copy of 'usable_protocols' because we can't check
6568 * consistency of an action set. */
6569 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6570 enum ofputil_protocol p = *usable_protocols;
6571 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6572 flow, max_ports, table_id, n_tables, &p);
6575 case OFPACT_WRITE_METADATA:
6578 case OFPACT_METER: {
6579 uint32_t mid = ofpact_get_METER(a)->meter_id;
6580 if (mid == 0 || mid > OFPM13_MAX) {
6581 return OFPERR_OFPMMFC_INVALID_METER;
6586 case OFPACT_GOTO_TABLE: {
6587 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
6588 if ((table_id != 255 && goto_table <= table_id)
6589 || (n_tables != 255 && goto_table >= n_tables)) {
6590 return OFPERR_OFPBIC_BAD_TABLE_ID;
6598 case OFPACT_UNROLL_XLATE:
6599 /* UNROLL is an internal action that should never be seen via
6601 return OFPERR_OFPBAC_BAD_TYPE;
6603 case OFPACT_DEBUG_RECIRC:
6611 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6612 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6613 * switch with no more than 'max_ports' ports.
6615 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6616 * '*usable_protocols' the protocols that forbid the inconsistency. (An
6617 * example of an inconsistency between match and actions is a flow that does
6618 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6620 * May annotate ofpacts with information gathered from the 'flow'.
6622 * May temporarily modify 'flow', but restores the changes before returning. */
6624 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
6625 struct flow *flow, ofp_port_t max_ports,
6626 uint8_t table_id, uint8_t n_tables,
6627 enum ofputil_protocol *usable_protocols)
6630 ovs_be16 dl_type = flow->dl_type;
6631 ovs_be16 vlan_tci = flow->vlan_tci;
6632 uint8_t nw_proto = flow->nw_proto;
6633 enum ofperr error = 0;
6635 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6636 error = ofpact_check__(usable_protocols, a, flow,
6637 max_ports, table_id, n_tables);
6642 /* Restore fields that may have been modified. */
6643 flow->dl_type = dl_type;
6644 flow->vlan_tci = vlan_tci;
6645 flow->nw_proto = nw_proto;
6649 /* Like ofpacts_check(), but reports inconsistencies as
6650 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6652 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
6653 struct flow *flow, ofp_port_t max_ports,
6654 uint8_t table_id, uint8_t n_tables,
6655 enum ofputil_protocol usable_protocols)
6657 enum ofputil_protocol p = usable_protocols;
6660 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
6661 table_id, n_tables, &p);
6662 return (error ? error
6663 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
6667 static const struct mf_field *
6668 ofpact_get_mf_field(enum ofpact_type type, const void *ofpact)
6670 if (type == OFPACT_SET_FIELD) {
6671 const struct ofpact_set_field *orl = ofpact;
6674 } else if (type == OFPACT_REG_MOVE) {
6675 const struct ofpact_reg_move *orm = ofpact;
6677 return orm->dst.field;
6684 unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action)
6686 VLOG_WARN("%s action doesn't support nested action %s",
6687 ofpact_name(outer_action), ofpact_name(action));
6688 return OFPERR_OFPBAC_BAD_ARGUMENT;
6692 field_requires_ct(enum mf_field_id field)
6694 return field == MFF_CT_MARK || field == MFF_CT_LABEL;
6697 /* Apply nesting constraints for actions */
6699 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
6701 const struct mf_field *field = ofpact_get_mf_field(a->type, a);
6703 if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
6704 VLOG_WARN("cannot set CT fields outside of ct action");
6705 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6707 if (a->type == OFPACT_NAT) {
6708 if (outer_action != OFPACT_CT) {
6709 VLOG_WARN("Cannot have NAT action outside of \"ct\" action");
6710 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6716 ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
6717 || outer_action == OFPACT_CT);
6719 if (outer_action == OFPACT_CT) {
6721 return unsupported_nesting(a->type, outer_action);
6722 } else if (!field_requires_ct(field->id)) {
6723 VLOG_WARN("%s action doesn't support nested modification "
6724 "of %s", ofpact_name(outer_action), field->name);
6725 return OFPERR_OFPBAC_BAD_ARGUMENT;
6733 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
6734 * appropriate order as defined by the OpenFlow spec and as required by Open
6737 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
6738 * instructions that are allowed within 'ofpacts[]'.
6740 * If 'outer_action' is not zero, it specifies that the actions are nested
6741 * within another action of type 'outer_action'. */
6743 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
6744 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
6746 const struct ofpact *a;
6747 enum ovs_instruction_type inst;
6749 inst = OVSINST_OFPIT13_METER;
6750 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6751 enum ovs_instruction_type next;
6754 if (a->type == OFPACT_CONJUNCTION) {
6755 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6756 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
6757 VLOG_WARN("\"conjunction\" actions may be used along with "
6758 "\"note\" but not any other kind of action "
6759 "(such as the \"%s\" action used here)",
6760 ofpact_name(a->type));
6761 return OFPERR_NXBAC_BAD_CONJUNCTION;
6767 error = ofpacts_verify_nested(a, outer_action);
6772 next = ovs_instruction_type_from_ofpact_type(a->type);
6774 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
6777 const char *name = ovs_instruction_name_from_type(inst);
6778 const char *next_name = ovs_instruction_name_from_type(next);
6781 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
6782 "1.1+ compatibility", name);
6784 VLOG_WARN("invalid instruction ordering: %s must appear "
6785 "before %s, for OpenFlow 1.1+ compatibility",
6788 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
6790 if (!((1u << next) & allowed_ovsinsts)) {
6791 const char *name = ovs_instruction_name_from_type(next);
6793 VLOG_WARN("%s instruction not allowed here", name);
6794 return OFPERR_OFPBIC_UNSUP_INST;
6803 /* Converting ofpacts to OpenFlow. */
6806 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
6810 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6811 case OFPACT_##ENUM: \
6812 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
6821 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
6822 * actions in 'openflow', appending the actions to any existing data in
6825 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
6826 struct ofpbuf *openflow,
6827 enum ofp_version ofp_version)
6829 const struct ofpact *a;
6830 size_t start_size = openflow->size;
6832 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6833 encode_ofpact(a, ofp_version, openflow);
6835 return openflow->size - start_size;
6838 static enum ovs_instruction_type
6839 ofpact_is_apply_actions(const struct ofpact *a)
6841 return (ovs_instruction_type_from_ofpact_type(a->type)
6842 == OVSINST_OFPIT11_APPLY_ACTIONS);
6846 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
6848 struct ofpbuf *openflow,
6849 enum ofp_version ofp_version)
6851 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
6852 const struct ofpact *a;
6854 if (ofp_version == OFP10_VERSION) {
6855 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
6862 if (ofpact_is_apply_actions(a)) {
6863 size_t ofs = openflow->size;
6865 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
6867 encode_ofpact(a, ofp_version, openflow);
6869 } while (a < end && ofpact_is_apply_actions(a));
6870 ofpacts_update_instruction_actions(openflow, ofs);
6872 encode_ofpact(a, ofp_version, openflow);
6878 /* Sets of supported actions. */
6880 /* Two-way translation between OVS's internal "OFPACT_*" representation of
6881 * actions and the "OFPAT_*" representation used in some OpenFlow version.
6882 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
6883 * instance is specific to one OpenFlow version.) */
6885 enum ofpact_type ofpact; /* Internal name for action type. */
6886 int ofpat; /* OFPAT_* number from OpenFlow spec. */
6889 static const struct ofpact_map *
6890 get_ofpact_map(enum ofp_version version)
6892 /* OpenFlow 1.0 actions. */
6893 static const struct ofpact_map of10[] = {
6894 { OFPACT_OUTPUT, 0 },
6895 { OFPACT_SET_VLAN_VID, 1 },
6896 { OFPACT_SET_VLAN_PCP, 2 },
6897 { OFPACT_STRIP_VLAN, 3 },
6898 { OFPACT_SET_ETH_SRC, 4 },
6899 { OFPACT_SET_ETH_DST, 5 },
6900 { OFPACT_SET_IPV4_SRC, 6 },
6901 { OFPACT_SET_IPV4_DST, 7 },
6902 { OFPACT_SET_IP_DSCP, 8 },
6903 { OFPACT_SET_L4_SRC_PORT, 9 },
6904 { OFPACT_SET_L4_DST_PORT, 10 },
6905 { OFPACT_ENQUEUE, 11 },
6909 /* OpenFlow 1.1 actions. */
6910 static const struct ofpact_map of11[] = {
6911 { OFPACT_OUTPUT, 0 },
6912 { OFPACT_SET_VLAN_VID, 1 },
6913 { OFPACT_SET_VLAN_PCP, 2 },
6914 { OFPACT_SET_ETH_SRC, 3 },
6915 { OFPACT_SET_ETH_DST, 4 },
6916 { OFPACT_SET_IPV4_SRC, 5 },
6917 { OFPACT_SET_IPV4_DST, 6 },
6918 { OFPACT_SET_IP_DSCP, 7 },
6919 { OFPACT_SET_IP_ECN, 8 },
6920 { OFPACT_SET_L4_SRC_PORT, 9 },
6921 { OFPACT_SET_L4_DST_PORT, 10 },
6922 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6923 /* OFPAT_COPY_TTL_IN (12) not supported. */
6924 { OFPACT_SET_MPLS_LABEL, 13 },
6925 { OFPACT_SET_MPLS_TC, 14 },
6926 { OFPACT_SET_MPLS_TTL, 15 },
6927 { OFPACT_DEC_MPLS_TTL, 16 },
6928 { OFPACT_PUSH_VLAN, 17 },
6929 { OFPACT_STRIP_VLAN, 18 },
6930 { OFPACT_PUSH_MPLS, 19 },
6931 { OFPACT_POP_MPLS, 20 },
6932 { OFPACT_SET_QUEUE, 21 },
6933 { OFPACT_GROUP, 22 },
6934 { OFPACT_SET_IP_TTL, 23 },
6935 { OFPACT_DEC_TTL, 24 },
6939 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6940 static const struct ofpact_map of12[] = {
6941 { OFPACT_OUTPUT, 0 },
6942 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6943 /* OFPAT_COPY_TTL_IN (12) not supported. */
6944 { OFPACT_SET_MPLS_TTL, 15 },
6945 { OFPACT_DEC_MPLS_TTL, 16 },
6946 { OFPACT_PUSH_VLAN, 17 },
6947 { OFPACT_STRIP_VLAN, 18 },
6948 { OFPACT_PUSH_MPLS, 19 },
6949 { OFPACT_POP_MPLS, 20 },
6950 { OFPACT_SET_QUEUE, 21 },
6951 { OFPACT_GROUP, 22 },
6952 { OFPACT_SET_IP_TTL, 23 },
6953 { OFPACT_DEC_TTL, 24 },
6954 { OFPACT_SET_FIELD, 25 },
6955 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
6956 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
6976 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
6977 * values, into a bitmap of actions suitable for OpenFlow 'version', and
6978 * returns the result. */
6980 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
6982 uint32_t openflow_bitmap = 0;
6983 const struct ofpact_map *x;
6985 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6986 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
6987 openflow_bitmap |= 1u << x->ofpat;
6990 return htonl(openflow_bitmap);
6993 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
6994 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
6995 * and returns the result. */
6997 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
6999 uint64_t ofpact_bitmap = 0;
7000 const struct ofpact_map *x;
7002 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7003 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
7004 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
7007 return ofpact_bitmap;
7010 /* Appends to 's' a string representation of the set of OFPACT_* represented
7011 * by 'ofpacts_bitmap'. */
7013 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
7015 if (!ofpacts_bitmap) {
7016 ds_put_cstr(s, "<none>");
7018 while (ofpacts_bitmap) {
7019 ds_put_format(s, "%s ",
7020 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
7021 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
7027 /* Returns true if 'action' outputs to 'port', false otherwise. */
7029 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
7031 switch (ofpact->type) {
7033 return ofpact_get_OUTPUT(ofpact)->port == port;
7034 case OFPACT_ENQUEUE:
7035 return ofpact_get_ENQUEUE(ofpact)->port == port;
7036 case OFPACT_CONTROLLER:
7037 return port == OFPP_CONTROLLER;
7039 case OFPACT_OUTPUT_REG:
7041 case OFPACT_SET_VLAN_VID:
7042 case OFPACT_SET_VLAN_PCP:
7043 case OFPACT_STRIP_VLAN:
7044 case OFPACT_PUSH_VLAN:
7045 case OFPACT_SET_ETH_SRC:
7046 case OFPACT_SET_ETH_DST:
7047 case OFPACT_SET_IPV4_SRC:
7048 case OFPACT_SET_IPV4_DST:
7049 case OFPACT_SET_IP_DSCP:
7050 case OFPACT_SET_IP_ECN:
7051 case OFPACT_SET_IP_TTL:
7052 case OFPACT_SET_L4_SRC_PORT:
7053 case OFPACT_SET_L4_DST_PORT:
7054 case OFPACT_REG_MOVE:
7055 case OFPACT_SET_FIELD:
7056 case OFPACT_STACK_PUSH:
7057 case OFPACT_STACK_POP:
7058 case OFPACT_DEC_TTL:
7059 case OFPACT_SET_MPLS_LABEL:
7060 case OFPACT_SET_MPLS_TC:
7061 case OFPACT_SET_MPLS_TTL:
7062 case OFPACT_DEC_MPLS_TTL:
7063 case OFPACT_SET_TUNNEL:
7064 case OFPACT_WRITE_METADATA:
7065 case OFPACT_SET_QUEUE:
7066 case OFPACT_POP_QUEUE:
7067 case OFPACT_FIN_TIMEOUT:
7068 case OFPACT_RESUBMIT:
7070 case OFPACT_CONJUNCTION:
7071 case OFPACT_MULTIPATH:
7074 case OFPACT_UNROLL_XLATE:
7075 case OFPACT_PUSH_MPLS:
7076 case OFPACT_POP_MPLS:
7078 case OFPACT_CLEAR_ACTIONS:
7079 case OFPACT_WRITE_ACTIONS:
7080 case OFPACT_GOTO_TABLE:
7083 case OFPACT_DEBUG_RECIRC:
7091 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7092 * to 'port', false otherwise. */
7094 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
7097 const struct ofpact *a;
7099 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7100 if (ofpact_outputs_to_port(a, port)) {
7108 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7109 * to 'group', false otherwise. */
7111 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
7114 const struct ofpact *a;
7116 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7117 if (a->type == OFPACT_GROUP
7118 && ofpact_get_GROUP(a)->group_id == group_id) {
7127 ofpacts_equal(const struct ofpact *a, size_t a_len,
7128 const struct ofpact *b, size_t b_len)
7130 return a_len == b_len && !memcmp(a, b, a_len);
7133 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
7134 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
7136 * This function relies on the order of 'ofpacts' being correct (as checked by
7137 * ofpacts_verify()). */
7139 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
7141 const struct ofpact *a;
7143 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7144 enum ovs_instruction_type inst;
7146 inst = ovs_instruction_type_from_ofpact_type(a->type);
7147 if (a->type == OFPACT_METER) {
7148 return ofpact_get_METER(a)->meter_id;
7149 } else if (inst > OVSINST_OFPIT13_METER) {
7157 /* Formatting ofpacts. */
7160 ofpact_format(const struct ofpact *a, struct ds *s)
7163 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7164 case OFPACT_##ENUM: \
7165 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
7174 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
7175 * 'ofpacts' to 'string'. */
7177 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
7181 ds_put_cstr(string, "drop");
7183 const struct ofpact *a;
7185 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7187 ds_put_cstr(string, ",");
7190 /* XXX write-actions */
7191 ofpact_format(a, string);
7196 /* Internal use by helpers. */
7199 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
7201 struct ofpact *ofpact;
7203 ofpact_pad(ofpacts);
7204 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
7205 ofpact = ofpacts->header;
7206 ofpact_init(ofpact, type, len);
7211 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
7213 memset(ofpact, 0, len);
7214 ofpact->type = type;
7219 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
7220 * starting at 'ofpact'.
7222 * This is the correct way to update a variable-length ofpact's length after
7223 * adding the variable-length part of the payload. (See the large comment
7224 * near the end of ofp-actions.h for more information.) */
7226 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
7228 ovs_assert(ofpact == ofpacts->header);
7229 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
7232 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
7233 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
7234 * client must call this itself after adding the final ofpact to an array of
7237 * (The consequences of failing to call this function are probably not dire.
7238 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
7239 * not dereference it. That's undefined behavior, technically, but it will not
7240 * cause a real problem on common systems. Still, it seems better to call
7243 ofpact_pad(struct ofpbuf *ofpacts)
7245 unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
7247 ofpbuf_put_zeros(ofpacts, pad);
7254 static char * OVS_WARN_UNUSED_RESULT
7255 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
7256 enum ofputil_protocol *usable_protocols)
7259 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7260 case OFPACT_##ENUM: \
7261 return parse_##ENUM(value, ofpacts, usable_protocols);
7270 ofpact_type_from_name(const char *name, enum ofpact_type *type)
7272 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7273 if (!strcasecmp(name, NAME)) { \
7274 *type = OFPACT_##ENUM; \
7283 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
7285 * Returns NULL if successful, otherwise a malloc()'d string describing the
7286 * error. The caller is responsible for freeing the returned string.
7288 * If 'outer_action' is specified, indicates that the actions being parsed
7289 * are nested within another action of the type specified in 'outer_action'. */
7290 static char * OVS_WARN_UNUSED_RESULT
7291 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
7292 enum ofputil_protocol *usable_protocols,
7293 bool allow_instructions, enum ofpact_type outer_action)
7302 while (ofputil_parse_key_value(&pos, &key, &value)) {
7303 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
7304 enum ofpact_type type;
7308 if (ofpact_type_from_name(key, &type)) {
7309 error = ofpact_parse(type, value, ofpacts, usable_protocols);
7310 inst = ovs_instruction_type_from_ofpact_type(type);
7311 } else if (!strcasecmp(key, "mod_vlan_vid")) {
7312 error = parse_set_vlan_vid(value, ofpacts, true);
7313 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
7314 error = parse_set_vlan_pcp(value, ofpacts, true);
7315 } else if (!strcasecmp(key, "set_nw_ttl")) {
7316 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
7317 } else if (!strcasecmp(key, "pop_vlan")) {
7318 error = parse_pop_vlan(ofpacts);
7319 } else if (!strcasecmp(key, "set_tunnel64")) {
7320 error = parse_set_tunnel(value, ofpacts,
7321 NXAST_RAW_SET_TUNNEL64);
7322 } else if (!strcasecmp(key, "load")) {
7323 error = parse_reg_load(value, ofpacts);
7324 } else if (!strcasecmp(key, "bundle_load")) {
7325 error = parse_bundle_load(value, ofpacts);
7326 } else if (!strcasecmp(key, "drop")) {
7328 } else if (!strcasecmp(key, "apply_actions")) {
7329 return xstrdup("apply_actions is the default instruction");
7330 } else if (ofputil_port_from_string(key, &port)) {
7331 ofpact_put_OUTPUT(ofpacts)->port = port;
7333 return xasprintf("unknown action %s", key);
7339 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
7340 if (!allow_instructions) {
7341 return xasprintf("only actions are allowed here (not "
7343 ovs_instruction_name_from_type(inst));
7345 if (inst == prev_inst) {
7346 return xasprintf("instruction %s may be specified only once",
7347 ovs_instruction_name_from_type(inst));
7350 if (prev_inst != -1 && inst < prev_inst) {
7351 return xasprintf("instruction %s must be specified before %s",
7352 ovs_instruction_name_from_type(inst),
7353 ovs_instruction_name_from_type(prev_inst));
7357 ofpact_pad(ofpacts);
7359 if (drop && ofpacts->size) {
7360 return xstrdup("\"drop\" must not be accompanied by any other action "
7364 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
7366 ? (1u << N_OVS_INSTRUCTIONS) - 1
7367 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
7370 return xstrdup("Incorrect instruction ordering");
7376 static char * OVS_WARN_UNUSED_RESULT
7377 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
7378 enum ofputil_protocol *usable_protocols, bool allow_instructions,
7379 enum ofpact_type outer_action)
7381 uint32_t orig_size = ofpacts->size;
7382 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
7383 allow_instructions, outer_action);
7385 ofpacts->size = orig_size;
7390 static char * OVS_WARN_UNUSED_RESULT
7391 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
7392 enum ofputil_protocol *usable_protocols,
7393 bool allow_instructions, enum ofpact_type outer_action)
7397 *usable_protocols = OFPUTIL_P_ANY;
7400 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
7407 /* Parses 's' as a set of OpenFlow actions and appends the actions to
7408 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
7409 * that are nested within the action of type 'outer_action'.
7411 * Returns NULL if successful, otherwise a malloc()'d string describing the
7412 * error. The caller is responsible for freeing the returned string. */
7413 char * OVS_WARN_UNUSED_RESULT
7414 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
7415 enum ofputil_protocol *usable_protocols)
7417 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
7420 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
7423 * Returns NULL if successful, otherwise a malloc()'d string describing the
7424 * error. The caller is responsible for freeing the returned string. */
7425 char * OVS_WARN_UNUSED_RESULT
7426 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
7427 enum ofputil_protocol *usable_protocols)
7429 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
7433 ofpact_name(enum ofpact_type type)
7436 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7443 /* Low-level action decoding and encoding functions. */
7445 /* Everything needed to identify a particular OpenFlow action. */
7446 struct ofpact_hdrs {
7447 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
7448 uint16_t type; /* Type if standard, otherwise subtype. */
7449 uint8_t ofp_version; /* From ofp_header. */
7452 /* Information about a particular OpenFlow action. */
7453 struct ofpact_raw_instance {
7454 /* The action's identity. */
7455 struct ofpact_hdrs hdrs;
7456 enum ofp_raw_action_type raw;
7458 /* Looking up the action. */
7459 struct hmap_node decode_node; /* Based on 'hdrs'. */
7460 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
7462 /* The action's encoded size.
7464 * If this action is fixed-length, 'min_length' == 'max_length'.
7465 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7466 * OFP_ACTION_ALIGN) == 65528. */
7467 unsigned short int min_length;
7468 unsigned short int max_length;
7470 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7471 * offset of that argument from the beginning of the action and 'arg_len'
7472 * its length, both in bytes.
7474 * For actions that take other forms, these are both zero. */
7475 unsigned short int arg_ofs;
7476 unsigned short int arg_len;
7478 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7481 /* If this action is deprecated, a human-readable string with a brief
7483 const char *deprecation;
7486 /* Action header. */
7487 struct ofp_action_header {
7488 /* The meaning of other values of 'type' generally depends on the OpenFlow
7489 * version (see enum ofp_raw_action_type).
7491 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7492 * designates an OpenFlow vendor ID and that the remainder of the action
7493 * structure has a vendor-defined meaning.
7495 #define OFPAT_VENDOR 0xffff
7498 /* Always a multiple of 8. */
7501 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7502 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
7505 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
7507 /* Header for Nicira-defined actions and for ONF vendor extensions.
7509 * This cannot be used as an entirely generic vendor extension action header,
7510 * because OpenFlow does not specify the location or size of the action
7511 * subtype; it just happens that ONF extensions and Nicira extensions share
7513 struct ext_action_header {
7514 ovs_be16 type; /* OFPAT_VENDOR. */
7515 ovs_be16 len; /* At least 16. */
7516 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
7517 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
7520 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
7523 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
7524 const struct ofpact_hdrs *b)
7526 return (a->vendor == b->vendor
7527 && a->type == b->type
7528 && a->ofp_version == b->ofp_version);
7532 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
7534 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
7537 #include "ofp-actions.inc2"
7539 static struct hmap *
7540 ofpact_decode_hmap(void)
7542 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7543 static struct hmap hmap;
7545 if (ovsthread_once_start(&once)) {
7546 struct ofpact_raw_instance *inst;
7549 for (inst = all_raw_instances;
7550 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7552 hmap_insert(&hmap, &inst->decode_node,
7553 ofpact_hdrs_hash(&inst->hdrs));
7555 ovsthread_once_done(&once);
7560 static struct hmap *
7561 ofpact_encode_hmap(void)
7563 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7564 static struct hmap hmap;
7566 if (ovsthread_once_start(&once)) {
7567 struct ofpact_raw_instance *inst;
7570 for (inst = all_raw_instances;
7571 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7573 hmap_insert(&hmap, &inst->encode_node,
7574 hash_2words(inst->raw, inst->hdrs.ofp_version));
7576 ovsthread_once_done(&once);
7582 ofpact_decode_raw(enum ofp_version ofp_version,
7583 const struct ofp_action_header *oah, size_t length,
7584 const struct ofpact_raw_instance **instp)
7586 const struct ofpact_raw_instance *inst;
7587 struct ofpact_hdrs hdrs;
7590 if (length < sizeof *oah) {
7591 return OFPERR_OFPBAC_BAD_LEN;
7594 /* Get base action type. */
7595 if (oah->type == htons(OFPAT_VENDOR)) {
7597 hdrs.vendor = ntohl(oah->vendor);
7598 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
7599 /* Get extension subtype. */
7600 const struct ext_action_header *nah;
7602 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
7603 if (length < sizeof *nah) {
7604 return OFPERR_OFPBAC_BAD_LEN;
7606 hdrs.type = ntohs(nah->subtype);
7608 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
7610 return OFPERR_OFPBAC_BAD_VENDOR;
7614 hdrs.type = ntohs(oah->type);
7617 hdrs.ofp_version = ofp_version;
7618 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
7619 ofpact_decode_hmap()) {
7620 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
7627 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7628 : OFPERR_OFPBAC_BAD_TYPE);
7632 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7633 enum ofp_raw_action_type *raw, uint64_t *arg)
7635 const struct ofp_action_header *oah = buf->data;
7636 const struct ofpact_raw_instance *action;
7637 unsigned int length;
7641 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
7646 if (action->deprecation) {
7647 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
7648 action->name, ofputil_version_to_string(ofp_version),
7649 action->deprecation);
7652 length = ntohs(oah->len);
7653 if (length > buf->size) {
7654 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
7655 "length %"PRIu32, action->name, length, buf->size);
7656 return OFPERR_OFPBAC_BAD_LEN;
7658 if (length < action->min_length || length > action->max_length) {
7659 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
7660 "[%hu,%hu]", action->name, length,
7661 action->min_length, action->max_length);
7662 return OFPERR_OFPBAC_BAD_LEN;
7665 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
7666 "of 8", action->name, length);
7667 return OFPERR_OFPBAC_BAD_LEN;
7672 if (action->arg_len) {
7676 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
7677 for (i = 0; i < action->arg_len; i++) {
7678 *arg = (*arg << 8) | p[i];
7682 ofpbuf_pull(buf, length);
7687 static const struct ofpact_raw_instance *
7688 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
7690 const struct ofpact_raw_instance *inst;
7692 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
7693 ofpact_encode_hmap()) {
7694 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
7702 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7703 enum ofp_raw_action_type raw, uint64_t arg)
7705 const struct ofpact_raw_instance *inst;
7706 struct ofp_action_header *oah;
7707 const struct ofpact_hdrs *hdrs;
7709 inst = ofpact_raw_lookup(ofp_version, raw);
7712 oah = ofpbuf_put_zeros(buf, inst->min_length);
7713 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
7714 oah->len = htons(inst->min_length);
7715 oah->vendor = htonl(hdrs->vendor);
7717 switch (hdrs->vendor) {
7722 case ONF_VENDOR_ID: {
7723 struct ext_action_header *nah = (struct ext_action_header *) oah;
7724 nah->subtype = htons(hdrs->type);
7732 if (inst->arg_len) {
7733 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
7736 for (i = 0; i < inst->arg_len; i++) {
7748 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
7750 struct ofp_action_header *oah;
7752 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs,
7755 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
7756 oah->len = htons(openflow->size - start_ofs);