2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-actions.h"
20 #include "byte-order.h"
23 #include "dynamic-string.h"
26 #include "meta-flow.h"
27 #include "multipath.h"
29 #include "ofp-parse.h"
32 #include "unaligned.h"
34 #include "openvswitch/vlog.h"
36 VLOG_DEFINE_THIS_MODULE(ofp_actions);
38 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
40 struct ofp_action_header;
42 /* Raw identifiers for OpenFlow actions.
44 * Decoding and encoding OpenFlow actions across multiple versions is difficult
45 * to do in a clean, consistent way. This enumeration lays out all of the
46 * forms of actions that Open vSwitch supports.
48 * The comments here must follow a stylized form because the
49 * "extract-ofp-actions" program parses them at build time to generate data
52 * - The first part of each comment specifies the vendor, OpenFlow versions,
53 * and type for each protocol that supports the action:
55 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
56 * extension actions. (Support for other vendors can be added, but
57 * it can't be done just based on a vendor ID definition alone
58 * because OpenFlow doesn't define a standard way to specify a
59 * subtype for vendor actions, so other vendors might do it different
62 * # The version can specify a specific OpenFlow version, a version
63 * range delimited by "-", or an open-ended range with "+".
65 * # The type, in parentheses, is the action type number (for standard
66 * OpenFlow actions) or subtype (for vendor extension actions).
68 * # Optionally one may add "is deprecated" followed by a
69 * human-readable reason in parentheses (which will be used in log
70 * messages), if a particular action should no longer be used.
72 * Multiple such specifications may be separated by commas.
74 * - The second part describes the action's wire format. It may be:
76 * # "struct <name>": The struct fully specifies the wire format. The
77 * action is exactly the size of the struct. (Thus, the struct must
78 * be an exact multiple of 8 bytes in size.)
80 * # "struct <name>, ...": The struct specifies the beginning of the
81 * wire format. An instance of the action is either the struct's
82 * exact size, or a multiple of 8 bytes longer.
84 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
85 * vendor extension) header, followed by 0 or more pad bytes to align
86 * to a multiple of <N> bits, followed by an argument of the given
87 * type, followed by 0 or more pad bytes to bring the total action up
88 * to a multiple of 8 bytes.
90 * # "void": The action is just a (standard or vendor extension)
93 * - Optional additional text enclosed in square brackets is commentary for
96 enum ofp_raw_action_type {
97 /* ## ----------------- ## */
98 /* ## Standard actions. ## */
99 /* ## ----------------- ## */
101 /* OF1.0(0): struct ofp10_action_output. */
103 /* OF1.1+(0): struct ofp11_action_output. */
106 /* OF1.0(1): uint16_t. */
107 OFPAT_RAW10_SET_VLAN_VID,
108 /* OF1.0(2): uint8_t. */
109 OFPAT_RAW10_SET_VLAN_PCP,
111 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
113 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
114 * modification actions: the 1.0 versions push a VLAN header if none is
115 * present, but the 1.1 versions do not. That is the only reason that we
116 * distinguish their raw action types.] */
117 OFPAT_RAW11_SET_VLAN_VID,
118 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
119 OFPAT_RAW11_SET_VLAN_PCP,
121 /* OF1.1+(17): ovs_be16.
123 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
125 OFPAT_RAW11_PUSH_VLAN,
127 /* OF1.0(3): void. */
128 OFPAT_RAW10_STRIP_VLAN,
129 /* OF1.1+(18): void. */
130 OFPAT_RAW11_POP_VLAN,
132 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
133 * ofp_action_dl_addr. */
134 OFPAT_RAW_SET_DL_SRC,
136 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
137 * ofp_action_dl_addr. */
138 OFPAT_RAW_SET_DL_DST,
140 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
142 OFPAT_RAW_SET_NW_SRC,
144 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
146 OFPAT_RAW_SET_NW_DST,
148 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
149 OFPAT_RAW_SET_NW_TOS,
151 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
152 OFPAT_RAW11_SET_NW_ECN,
154 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
156 OFPAT_RAW_SET_TP_SRC,
158 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
160 OFPAT_RAW_SET_TP_DST,
162 /* OF1.0(11): struct ofp10_action_enqueue. */
165 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
167 OFPAT_RAW_SET_MPLS_LABEL,
169 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
171 OFPAT_RAW_SET_MPLS_TC,
173 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
175 OFPAT_RAW_SET_MPLS_TTL,
177 /* NX1.0(26), OF1.1+(16): void. */
178 OFPAT_RAW_DEC_MPLS_TTL,
180 /* NX1.0(23), OF1.1+(19): ovs_be16.
182 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
185 /* NX1.0(24), OF1.1+(20): ovs_be16.
187 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
188 * ETH_TYPE_MPLS otherwise, not the label.] */
191 /* NX1.0(4), OF1.1+(21): uint32_t. */
194 /* OF1.1+(22): uint32_t. */
197 /* OF1.1+(23): uint8_t. */
198 OFPAT_RAW11_SET_NW_TTL,
200 /* NX1.0(18), OF1.1+(24): void. */
201 OFPAT_RAW_DEC_NW_TTL,
202 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
203 NXAST_RAW_DEC_TTL_CNT_IDS,
205 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
206 OFPAT_RAW12_SET_FIELD,
207 /* OF1.5+(25): struct ofp12_action_set_field, ... */
208 OFPAT_RAW15_SET_FIELD,
209 /* NX1.0-1.4(7): struct nx_action_reg_load.
211 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
212 * we drop reg_load.] */
214 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
216 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
217 * we drop reg_load2.] */
220 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
221 OFPAT_RAW15_COPY_FIELD,
222 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
223 ONFACT_RAW13_COPY_FIELD,
224 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
227 /* ## ------------------------- ## */
228 /* ## Nicira extension actions. ## */
229 /* ## ------------------------- ## */
231 /* Actions similar to standard actions are listed with the standard actions. */
233 /* NX1.0+(1): uint16_t. */
235 /* NX1.0+(14): struct nx_action_resubmit. */
236 NXAST_RAW_RESUBMIT_TABLE,
238 /* NX1.0+(2): uint32_t. */
239 NXAST_RAW_SET_TUNNEL,
240 /* NX1.0+(9): uint64_t. */
241 NXAST_RAW_SET_TUNNEL64,
243 /* NX1.0+(5): void. */
246 /* NX1.0+(8): struct nx_action_note, ... */
249 /* NX1.0+(10): struct nx_action_multipath. */
252 /* NX1.0+(12): struct nx_action_bundle, ... */
254 /* NX1.0+(13): struct nx_action_bundle, ... */
255 NXAST_RAW_BUNDLE_LOAD,
257 /* NX1.0+(15): struct nx_action_output_reg. */
258 NXAST_RAW_OUTPUT_REG,
259 /* NX1.0+(32): struct nx_action_output_reg2. */
260 NXAST_RAW_OUTPUT_REG2,
262 /* NX1.0+(16): struct nx_action_learn, ... */
265 /* NX1.0+(17): void. */
268 /* NX1.0+(19): struct nx_action_fin_timeout. */
269 NXAST_RAW_FIN_TIMEOUT,
271 /* NX1.0+(20): struct nx_action_controller. */
272 NXAST_RAW_CONTROLLER,
274 /* NX1.0+(22): struct nx_action_write_metadata. */
275 NXAST_RAW_WRITE_METADATA,
277 /* NX1.0+(27): struct nx_action_stack. */
278 NXAST_RAW_STACK_PUSH,
280 /* NX1.0+(28): struct nx_action_stack. */
283 /* NX1.0+(29): struct nx_action_sample. */
286 /* NX1.0+(34): struct nx_action_conjunction. */
287 NXAST_RAW_CONJUNCTION,
289 /* ## ------------------ ## */
290 /* ## Debugging actions. ## */
291 /* ## ------------------ ## */
293 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
294 /* accepts them only if started with --enable-dummy. */
296 /* NX1.0+(255): void. */
297 NXAST_RAW_DEBUG_RECIRC,
300 /* OpenFlow actions are always a multiple of 8 bytes in length. */
301 #define OFP_ACTION_ALIGN 8
303 /* Define a few functions for working with instructions. */
304 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
305 static inline const struct STRUCT * OVS_UNUSED \
306 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
308 ovs_assert(inst->type == htons(ENUM)); \
309 return ALIGNED_CAST(struct STRUCT *, inst); \
312 static inline void OVS_UNUSED \
313 instruction_init_##ENUM(struct STRUCT *s) \
315 memset(s, 0, sizeof *s); \
316 s->type = htons(ENUM); \
317 s->len = htons(sizeof *s); \
320 static inline struct STRUCT * OVS_UNUSED \
321 instruction_put_##ENUM(struct ofpbuf *buf) \
323 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
324 instruction_init_##ENUM(s); \
330 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
332 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
334 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
335 uint32_t allowed_ovsinsts,
336 enum ofpact_type outer_action);
338 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
339 enum mf_field_id, uint64_t value);
341 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
342 enum ofp_raw_action_type *, uint64_t *arg);
343 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
344 enum ofp_raw_action_type, uint64_t arg);
346 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
347 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
348 bool allow_instructions, enum ofpact_type outer_action);
350 /* Pull off existing actions or instructions. Used by nesting actions to keep
351 * ofpacts_parse() oblivious of actions nesting.
353 * Push the actions back on after nested parsing, e.g.:
355 * size_t ofs = ofpacts_pull(ofpacts);
356 * ...nested parsing...
357 * ofpbuf_push_uninit(ofpacts, ofs);
360 ofpacts_pull(struct ofpbuf *ofpacts)
366 ofpbuf_pull(ofpacts, ofs);
371 #include "ofp-actions.inc1"
373 /* Output actions. */
375 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
376 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
377 * number of bytes to send. A 'max_len' of zero means no bytes of the
378 * packet should be sent. */
379 struct ofp10_action_output {
380 ovs_be16 type; /* OFPAT10_OUTPUT. */
381 ovs_be16 len; /* Length is 8. */
382 ovs_be16 port; /* Output port. */
383 ovs_be16 max_len; /* Max length to send to controller. */
385 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
387 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
388 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
389 * number of bytes to send. A 'max_len' of zero means no bytes of the
390 * packet should be sent.*/
391 struct ofp11_action_output {
392 ovs_be16 type; /* OFPAT11_OUTPUT. */
393 ovs_be16 len; /* Length is 16. */
394 ovs_be32 port; /* Output port. */
395 ovs_be16 max_len; /* Max length to send to controller. */
396 uint8_t pad[6]; /* Pad to 64 bits. */
398 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
401 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
402 enum ofp_version ofp_version OVS_UNUSED,
405 struct ofpact_output *output;
407 output = ofpact_put_OUTPUT(out);
408 output->port = u16_to_ofp(ntohs(oao->port));
409 output->max_len = ntohs(oao->max_len);
411 return ofpact_check_output_port(output->port, OFPP_MAX);
415 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
416 enum ofp_version ofp_version OVS_UNUSED,
419 struct ofpact_output *output;
422 output = ofpact_put_OUTPUT(out);
423 output->max_len = ntohs(oao->max_len);
425 error = ofputil_port_from_ofp11(oao->port, &output->port);
430 return ofpact_check_output_port(output->port, OFPP_MAX);
434 encode_OUTPUT(const struct ofpact_output *output,
435 enum ofp_version ofp_version, struct ofpbuf *out)
437 if (ofp_version == OFP10_VERSION) {
438 struct ofp10_action_output *oao;
440 oao = put_OFPAT10_OUTPUT(out);
441 oao->port = htons(ofp_to_u16(output->port));
442 oao->max_len = htons(output->max_len);
444 struct ofp11_action_output *oao;
446 oao = put_OFPAT11_OUTPUT(out);
447 oao->port = ofputil_port_to_ofp11(output->port);
448 oao->max_len = htons(output->max_len);
452 static char * OVS_WARN_UNUSED_RESULT
453 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
454 enum ofputil_protocol *usable_protocols OVS_UNUSED)
456 if (strchr(arg, '[')) {
457 struct ofpact_output_reg *output_reg;
459 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
460 output_reg->max_len = UINT16_MAX;
461 return mf_parse_subfield(&output_reg->src, arg);
463 struct ofpact_output *output;
465 output = ofpact_put_OUTPUT(ofpacts);
466 if (!ofputil_port_from_string(arg, &output->port)) {
467 return xasprintf("%s: output to unknown port", arg);
469 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
475 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
477 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
478 ds_put_format(s, "output:%"PRIu16, a->port);
480 ofputil_format_port(a->port, s);
481 if (a->port == OFPP_CONTROLLER) {
482 ds_put_format(s, ":%"PRIu16, a->max_len);
490 decode_OFPAT_RAW11_GROUP(uint32_t group_id,
491 enum ofp_version ofp_version OVS_UNUSED,
494 ofpact_put_GROUP(out)->group_id = group_id;
499 encode_GROUP(const struct ofpact_group *group,
500 enum ofp_version ofp_version, struct ofpbuf *out)
502 if (ofp_version == OFP10_VERSION) {
505 put_OFPAT11_GROUP(out, group->group_id);
509 static char * OVS_WARN_UNUSED_RESULT
510 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
511 enum ofputil_protocol *usable_protocols OVS_UNUSED)
513 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
517 format_GROUP(const struct ofpact_group *a, struct ds *s)
519 ds_put_format(s, "group:%"PRIu32, a->group_id);
522 /* Action structure for NXAST_CONTROLLER.
524 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
525 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
528 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
530 * - 'controller_id': The ID of the controller connection to which the
531 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
532 * sent only to controllers that have the specified controller connection
533 * ID. See "struct nx_controller_id" for more information. */
534 struct nx_action_controller {
535 ovs_be16 type; /* OFPAT_VENDOR. */
536 ovs_be16 len; /* Length is 16. */
537 ovs_be32 vendor; /* NX_VENDOR_ID. */
538 ovs_be16 subtype; /* NXAST_CONTROLLER. */
539 ovs_be16 max_len; /* Maximum length to send to controller. */
540 ovs_be16 controller_id; /* Controller ID to send packet-in. */
541 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
542 uint8_t zero; /* Must be zero. */
544 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
547 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
548 enum ofp_version ofp_version OVS_UNUSED,
551 struct ofpact_controller *oc;
553 oc = ofpact_put_CONTROLLER(out);
554 oc->max_len = ntohs(nac->max_len);
555 oc->controller_id = ntohs(nac->controller_id);
556 oc->reason = nac->reason;
561 encode_CONTROLLER(const struct ofpact_controller *controller,
562 enum ofp_version ofp_version OVS_UNUSED,
565 struct nx_action_controller *nac;
567 nac = put_NXAST_CONTROLLER(out);
568 nac->max_len = htons(controller->max_len);
569 nac->controller_id = htons(controller->controller_id);
570 nac->reason = controller->reason;
573 static char * OVS_WARN_UNUSED_RESULT
574 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
575 enum ofputil_protocol *usable_protocols OVS_UNUSED)
577 enum ofp_packet_in_reason reason = OFPR_ACTION;
578 uint16_t controller_id = 0;
579 uint16_t max_len = UINT16_MAX;
583 } else if (strspn(arg, "0123456789") == strlen(arg)) {
584 char *error = str_to_u16(arg, "max_len", &max_len);
591 while (ofputil_parse_key_value(&arg, &name, &value)) {
592 if (!strcmp(name, "reason")) {
593 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
594 return xasprintf("unknown reason \"%s\"", value);
596 } else if (!strcmp(name, "max_len")) {
597 char *error = str_to_u16(value, "max_len", &max_len);
601 } else if (!strcmp(name, "id")) {
602 char *error = str_to_u16(value, "id", &controller_id);
607 return xasprintf("unknown key \"%s\" parsing controller "
613 if (reason == OFPR_ACTION && controller_id == 0) {
614 struct ofpact_output *output;
616 output = ofpact_put_OUTPUT(ofpacts);
617 output->port = OFPP_CONTROLLER;
618 output->max_len = max_len;
620 struct ofpact_controller *controller;
622 controller = ofpact_put_CONTROLLER(ofpacts);
623 controller->max_len = max_len;
624 controller->reason = reason;
625 controller->controller_id = controller_id;
632 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
634 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
635 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
637 enum ofp_packet_in_reason reason = a->reason;
639 ds_put_cstr(s, "controller(");
640 if (reason != OFPR_ACTION) {
641 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
643 ds_put_format(s, "reason=%s,",
644 ofputil_packet_in_reason_to_string(
645 reason, reasonbuf, sizeof reasonbuf));
647 if (a->max_len != UINT16_MAX) {
648 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
650 if (a->controller_id != 0) {
651 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
658 /* Enqueue action. */
659 struct ofp10_action_enqueue {
660 ovs_be16 type; /* OFPAT10_ENQUEUE. */
661 ovs_be16 len; /* Len is 16. */
662 ovs_be16 port; /* Port that queue belongs. Should
663 refer to a valid physical port
664 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
665 uint8_t pad[6]; /* Pad for 64-bit alignment. */
666 ovs_be32 queue_id; /* Where to enqueue the packets. */
668 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
671 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
672 enum ofp_version ofp_version OVS_UNUSED,
675 struct ofpact_enqueue *enqueue;
677 enqueue = ofpact_put_ENQUEUE(out);
678 enqueue->port = u16_to_ofp(ntohs(oae->port));
679 enqueue->queue = ntohl(oae->queue_id);
680 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
681 && enqueue->port != OFPP_IN_PORT
682 && enqueue->port != OFPP_LOCAL) {
683 return OFPERR_OFPBAC_BAD_OUT_PORT;
689 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
690 enum ofp_version ofp_version, struct ofpbuf *out)
692 if (ofp_version == OFP10_VERSION) {
693 struct ofp10_action_enqueue *oae;
695 oae = put_OFPAT10_ENQUEUE(out);
696 oae->port = htons(ofp_to_u16(enqueue->port));
697 oae->queue_id = htonl(enqueue->queue);
703 static char * OVS_WARN_UNUSED_RESULT
704 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
705 enum ofputil_protocol *usable_protocols OVS_UNUSED)
708 char *port = strtok_r(arg, ":q,", &sp);
709 char *queue = strtok_r(NULL, "", &sp);
710 struct ofpact_enqueue *enqueue;
712 if (port == NULL || queue == NULL) {
713 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
714 "\"enqueue(PORT,QUEUE)\"");
717 enqueue = ofpact_put_ENQUEUE(ofpacts);
718 if (!ofputil_port_from_string(port, &enqueue->port)) {
719 return xasprintf("%s: enqueue to unknown port", port);
721 return str_to_u32(queue, &enqueue->queue);
725 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
727 ds_put_format(s, "enqueue:");
728 ofputil_format_port(a->port, s);
729 ds_put_format(s, ":%"PRIu32, a->queue);
732 /* Action structure for NXAST_OUTPUT_REG.
734 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
736 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
737 * the NXAST_REG_LOAD action.
739 * The acceptable nxm_header values for 'src' are the same as the acceptable
740 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
742 * The 'max_len' field indicates the number of bytes to send when the chosen
743 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
744 * field of OFPAT_OUTPUT.
746 * The 'zero' field is required to be zeroed for forward compatibility. */
747 struct nx_action_output_reg {
748 ovs_be16 type; /* OFPAT_VENDOR. */
749 ovs_be16 len; /* 24. */
750 ovs_be32 vendor; /* NX_VENDOR_ID. */
751 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
753 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
754 ovs_be32 src; /* Source. */
756 ovs_be16 max_len; /* Max length to send to controller. */
758 uint8_t zero[6]; /* Reserved, must be zero. */
760 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
762 /* Action structure for NXAST_OUTPUT_REG2.
764 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
765 * experimenter OXM as 'src'.
767 struct nx_action_output_reg2 {
768 ovs_be16 type; /* OFPAT_VENDOR. */
769 ovs_be16 len; /* 24. */
770 ovs_be32 vendor; /* NX_VENDOR_ID. */
771 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
773 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
774 ovs_be16 max_len; /* Max length to send to controller. */
777 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
778 * - Enough 0-bytes to pad the action out to 24 bytes. */
781 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
784 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
785 enum ofp_version ofp_version OVS_UNUSED,
788 struct ofpact_output_reg *output_reg;
790 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
791 return OFPERR_OFPBAC_BAD_ARGUMENT;
794 output_reg = ofpact_put_OUTPUT_REG(out);
795 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
796 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
797 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
798 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
799 output_reg->max_len = ntohs(naor->max_len);
801 return mf_check_src(&output_reg->src, NULL);
805 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
806 enum ofp_version ofp_version OVS_UNUSED,
809 struct ofpact_output_reg *output_reg;
813 output_reg = ofpact_put_OUTPUT_REG(out);
814 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
815 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
816 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
817 output_reg->max_len = ntohs(naor->max_len);
819 ofpbuf_use_const(&b, naor, ntohs(naor->len));
820 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
821 error = nx_pull_header(&b, &output_reg->src.field, NULL);
825 if (!is_all_zeros(b.data, b.size)) {
826 return OFPERR_NXBRC_MUST_BE_ZERO;
829 return mf_check_src(&output_reg->src, NULL);
833 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
834 enum ofp_version ofp_version OVS_UNUSED,
837 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
838 * cannot be encoded in the older form, encode it as
839 * NXAST_RAW_OUTPUT_REG2. */
840 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
841 || !mf_nxm_header(output_reg->src.field->id)) {
842 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
843 size_t size = out->size;
845 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
846 output_reg->src.n_bits);
847 naor->max_len = htons(output_reg->max_len);
849 out->size = size - sizeof naor->pad;
850 nx_put_header(out, output_reg->src.field->id, 0, false);
853 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
855 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
856 output_reg->src.n_bits);
857 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
858 naor->max_len = htons(output_reg->max_len);
862 static char * OVS_WARN_UNUSED_RESULT
863 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
864 enum ofputil_protocol *usable_protocols OVS_UNUSED)
866 return parse_OUTPUT(arg, ofpacts, usable_protocols);
870 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
872 ds_put_cstr(s, "output:");
873 mf_format_subfield(&a->src, s);
876 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
878 * The bundle actions choose a slave from a supplied list of options.
879 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
880 * selection to a register.
882 * The list of possible slaves follows the nx_action_bundle structure. The size
883 * of each slave is governed by its type as indicated by the 'slave_type'
884 * parameter. The list of slaves should be padded at its end with zeros to make
885 * the total length of the action a multiple of 8.
887 * Switches infer from the 'slave_type' parameter the size of each slave. All
888 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
889 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
890 * 2 byte width. Switches should reject actions which indicate unknown or
891 * unsupported slave types.
893 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
894 * slave. If the switch does not support the specified 'algorithm' parameter,
895 * it should reject the action.
897 * Several algorithms take into account liveness when selecting slaves. The
898 * liveness of a slave is implementation defined (with one exception), but will
899 * generally take into account things like its carrier status and the results
900 * of any link monitoring protocols which happen to be running on it. In order
901 * to give controllers a place-holder value, the OFPP_NONE port is always
904 * Some slave selection strategies require the use of a hash function, in which
905 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
906 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
907 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
908 * 'basis' parameter is used as a universal hash parameter. Different values
909 * of 'basis' yield different hash results.
911 * The 'zero' parameter at the end of the action structure is reserved for
912 * future use. Switches are required to reject actions which have nonzero
913 * bytes in the 'zero' field.
915 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
916 * should reject actions which have nonzero bytes in either of these fields.
918 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
919 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
920 * similar to those for the NXAST_REG_LOAD action. */
921 struct nx_action_bundle {
922 ovs_be16 type; /* OFPAT_VENDOR. */
923 ovs_be16 len; /* Length including slaves. */
924 ovs_be32 vendor; /* NX_VENDOR_ID. */
925 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
927 /* Slave choice algorithm to apply to hash value. */
928 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
930 /* What fields to hash and how. */
931 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
932 ovs_be16 basis; /* Universal hash parameter. */
934 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
935 ovs_be16 n_slaves; /* Number of slaves. */
937 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
938 ovs_be32 dst; /* Destination. */
940 uint8_t zero[4]; /* Reserved. Must be zero. */
942 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
945 decode_bundle(bool load, const struct nx_action_bundle *nab,
946 struct ofpbuf *ofpacts)
948 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
949 struct ofpact_bundle *bundle;
951 size_t slaves_size, i;
954 bundle = ofpact_put_BUNDLE(ofpacts);
956 bundle->n_slaves = ntohs(nab->n_slaves);
957 bundle->basis = ntohs(nab->basis);
958 bundle->fields = ntohs(nab->fields);
959 bundle->algorithm = ntohs(nab->algorithm);
960 slave_type = ntohl(nab->slave_type);
961 slaves_size = ntohs(nab->len) - sizeof *nab;
963 error = OFPERR_OFPBAC_BAD_ARGUMENT;
964 if (!flow_hash_fields_valid(bundle->fields)) {
965 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
966 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
967 VLOG_WARN_RL(&rl, "too many slaves");
968 } else if (bundle->algorithm != NX_BD_ALG_HRW
969 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
970 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
971 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
972 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
977 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
978 VLOG_WARN_RL(&rl, "reserved field is nonzero");
979 error = OFPERR_OFPBAC_BAD_ARGUMENT;
983 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
984 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
985 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
987 if (bundle->dst.n_bits < 16) {
988 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
990 error = OFPERR_OFPBAC_BAD_ARGUMENT;
993 if (nab->ofs_nbits || nab->dst) {
994 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
995 error = OFPERR_OFPBAC_BAD_ARGUMENT;
999 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1000 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1001 "allocated for slaves. %"PRIuSIZE" bytes are required "
1002 "for %"PRIu16" slaves.",
1003 load ? "bundle_load" : "bundle", slaves_size,
1004 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1005 error = OFPERR_OFPBAC_BAD_LEN;
1008 for (i = 0; i < bundle->n_slaves; i++) {
1009 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1010 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1013 bundle = ofpacts->header;
1014 ofpact_update_len(ofpacts, &bundle->ofpact);
1017 error = bundle_check(bundle, OFPP_MAX, NULL);
1023 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1024 enum ofp_version ofp_version OVS_UNUSED,
1027 return decode_bundle(false, nab, out);
1031 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1032 enum ofp_version ofp_version OVS_UNUSED,
1035 return decode_bundle(true, nab, out);
1039 encode_BUNDLE(const struct ofpact_bundle *bundle,
1040 enum ofp_version ofp_version OVS_UNUSED,
1043 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1044 struct nx_action_bundle *nab;
1048 nab = (bundle->dst.field
1049 ? put_NXAST_BUNDLE_LOAD(out)
1050 : put_NXAST_BUNDLE(out));
1051 nab->len = htons(ntohs(nab->len) + slaves_len);
1052 nab->algorithm = htons(bundle->algorithm);
1053 nab->fields = htons(bundle->fields);
1054 nab->basis = htons(bundle->basis);
1055 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1056 nab->n_slaves = htons(bundle->n_slaves);
1057 if (bundle->dst.field) {
1058 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1059 bundle->dst.n_bits);
1060 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1063 slaves = ofpbuf_put_zeros(out, slaves_len);
1064 for (i = 0; i < bundle->n_slaves; i++) {
1065 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1069 static char * OVS_WARN_UNUSED_RESULT
1070 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1071 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1073 return bundle_parse(arg, ofpacts);
1076 static char * OVS_WARN_UNUSED_RESULT
1077 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1079 return bundle_parse_load(arg, ofpacts);
1083 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1085 bundle_format(a, s);
1088 /* Set VLAN actions. */
1091 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1094 return OFPERR_OFPBAC_BAD_ARGUMENT;
1096 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1097 vlan_vid->vlan_vid = vid;
1098 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1104 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1105 enum ofp_version ofp_version OVS_UNUSED,
1108 return decode_set_vlan_vid(vid, true, out);
1112 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1113 enum ofp_version ofp_version OVS_UNUSED,
1116 return decode_set_vlan_vid(vid, false, out);
1120 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1121 enum ofp_version ofp_version, struct ofpbuf *out)
1123 uint16_t vid = vlan_vid->vlan_vid;
1125 /* Push a VLAN tag, if none is present and this form of the action calls
1126 * for such a feature. */
1127 if (ofp_version > OFP10_VERSION
1128 && vlan_vid->push_vlan_if_needed
1129 && !vlan_vid->flow_has_vlan) {
1130 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1133 if (ofp_version == OFP10_VERSION) {
1134 put_OFPAT10_SET_VLAN_VID(out, vid);
1135 } else if (ofp_version == OFP11_VERSION) {
1136 put_OFPAT11_SET_VLAN_VID(out, vid);
1138 ofpact_put_set_field(out, ofp_version,
1139 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1143 static char * OVS_WARN_UNUSED_RESULT
1144 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1146 struct ofpact_vlan_vid *vlan_vid;
1150 error = str_to_u16(arg, "VLAN VID", &vid);
1155 if (vid & ~VLAN_VID_MASK) {
1156 return xasprintf("%s: not a valid VLAN VID", arg);
1158 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1159 vlan_vid->vlan_vid = vid;
1160 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1164 static char * OVS_WARN_UNUSED_RESULT
1165 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1166 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1168 return parse_set_vlan_vid(arg, ofpacts, false);
1172 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1174 ds_put_format(s, "%s:%"PRIu16,
1175 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1179 /* Set PCP actions. */
1182 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1185 return OFPERR_OFPBAC_BAD_ARGUMENT;
1187 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1188 vlan_pcp->vlan_pcp = pcp;
1189 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1195 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1196 enum ofp_version ofp_version OVS_UNUSED,
1199 return decode_set_vlan_pcp(pcp, true, out);
1203 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1204 enum ofp_version ofp_version OVS_UNUSED,
1207 return decode_set_vlan_pcp(pcp, false, out);
1211 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1212 enum ofp_version ofp_version, struct ofpbuf *out)
1214 uint8_t pcp = vlan_pcp->vlan_pcp;
1216 /* Push a VLAN tag, if none is present and this form of the action calls
1217 * for such a feature. */
1218 if (ofp_version > OFP10_VERSION
1219 && vlan_pcp->push_vlan_if_needed
1220 && !vlan_pcp->flow_has_vlan) {
1221 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1224 if (ofp_version == OFP10_VERSION) {
1225 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1226 } else if (ofp_version == OFP11_VERSION) {
1227 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1229 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1233 static char * OVS_WARN_UNUSED_RESULT
1234 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1236 struct ofpact_vlan_pcp *vlan_pcp;
1240 error = str_to_u8(arg, "VLAN PCP", &pcp);
1246 return xasprintf("%s: not a valid VLAN PCP", arg);
1248 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1249 vlan_pcp->vlan_pcp = pcp;
1250 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1254 static char * OVS_WARN_UNUSED_RESULT
1255 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1256 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1258 return parse_set_vlan_pcp(arg, ofpacts, false);
1262 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1264 ds_put_format(s, "%s:%"PRIu8,
1265 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1269 /* Strip VLAN actions. */
1272 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1274 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1279 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1281 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1286 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1287 enum ofp_version ofp_version, struct ofpbuf *out)
1289 if (ofp_version == OFP10_VERSION) {
1290 put_OFPAT10_STRIP_VLAN(out);
1292 put_OFPAT11_POP_VLAN(out);
1296 static char * OVS_WARN_UNUSED_RESULT
1297 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1298 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1300 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1304 static char * OVS_WARN_UNUSED_RESULT
1305 parse_pop_vlan(struct ofpbuf *ofpacts)
1307 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1312 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1314 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1319 /* Push VLAN action. */
1322 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1323 enum ofp_version ofp_version OVS_UNUSED,
1326 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1327 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1328 return OFPERR_OFPBAC_BAD_ARGUMENT;
1330 ofpact_put_PUSH_VLAN(out);
1335 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1336 enum ofp_version ofp_version, struct ofpbuf *out)
1338 if (ofp_version == OFP10_VERSION) {
1339 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1340 * follow this action. */
1342 /* XXX ETH_TYPE_VLAN_8021AD case */
1343 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1347 static char * OVS_WARN_UNUSED_RESULT
1348 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1349 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1354 *usable_protocols &= OFPUTIL_P_OF11_UP;
1355 error = str_to_u16(arg, "ethertype", ðertype);
1360 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1361 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1362 return xasprintf("%s: not a valid VLAN ethertype", arg);
1365 ofpact_put_PUSH_VLAN(ofpacts);
1370 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1372 /* XXX 802.1AD case*/
1373 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1376 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1377 struct ofp_action_dl_addr {
1378 ovs_be16 type; /* Type. */
1379 ovs_be16 len; /* Length is 16. */
1380 struct eth_addr dl_addr; /* Ethernet address. */
1383 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1386 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1387 enum ofp_version ofp_version OVS_UNUSED,
1390 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1395 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1396 enum ofp_version ofp_version OVS_UNUSED,
1399 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1404 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1405 enum ofp_raw_action_type raw, enum mf_field_id field,
1408 if (ofp_version < OFP12_VERSION) {
1409 struct ofp_action_dl_addr *oada;
1411 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1412 oada->dl_addr = mac->mac;
1414 ofpact_put_set_field(out, ofp_version, field,
1415 eth_addr_to_uint64(mac->mac));
1420 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1423 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1429 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1430 enum ofp_version ofp_version,
1433 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1438 static char * OVS_WARN_UNUSED_RESULT
1439 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1440 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1442 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1445 static char * OVS_WARN_UNUSED_RESULT
1446 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1447 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1449 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1453 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1455 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1459 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1461 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1464 /* Set IPv4 address actions. */
1467 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1468 enum ofp_version ofp_version OVS_UNUSED,
1471 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1476 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1477 enum ofp_version ofp_version OVS_UNUSED,
1480 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1485 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1486 enum ofp_version ofp_version,
1487 enum ofp_raw_action_type raw, enum mf_field_id field,
1490 ovs_be32 addr = ipv4->ipv4;
1491 if (ofp_version < OFP12_VERSION) {
1492 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1494 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1499 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1500 enum ofp_version ofp_version, struct ofpbuf *out)
1502 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1507 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1508 enum ofp_version ofp_version, struct ofpbuf *out)
1510 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1514 static char * OVS_WARN_UNUSED_RESULT
1515 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1516 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1518 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1521 static char * OVS_WARN_UNUSED_RESULT
1522 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1523 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1525 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1529 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1531 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1535 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1537 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1540 /* Set IPv4/v6 TOS actions. */
1543 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1544 enum ofp_version ofp_version OVS_UNUSED,
1547 if (dscp & ~IP_DSCP_MASK) {
1548 return OFPERR_OFPBAC_BAD_ARGUMENT;
1550 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1556 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1557 enum ofp_version ofp_version, struct ofpbuf *out)
1559 if (ofp_version < OFP12_VERSION) {
1560 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1562 ofpact_put_set_field(out, ofp_version,
1563 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1567 static char * OVS_WARN_UNUSED_RESULT
1568 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1569 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1574 error = str_to_u8(arg, "TOS", &tos);
1579 if (tos & ~IP_DSCP_MASK) {
1580 return xasprintf("%s: not a valid TOS", arg);
1582 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1587 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1589 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1592 /* Set IPv4/v6 ECN actions. */
1595 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1596 enum ofp_version ofp_version OVS_UNUSED,
1599 if (ecn & ~IP_ECN_MASK) {
1600 return OFPERR_OFPBAC_BAD_ARGUMENT;
1602 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1608 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1609 enum ofp_version ofp_version, struct ofpbuf *out)
1611 uint8_t ecn = ip_ecn->ecn;
1612 if (ofp_version == OFP10_VERSION) {
1614 } else if (ofp_version == OFP11_VERSION) {
1615 put_OFPAT11_SET_NW_ECN(out, ecn);
1617 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1621 static char * OVS_WARN_UNUSED_RESULT
1622 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1623 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1628 error = str_to_u8(arg, "ECN", &ecn);
1633 if (ecn & ~IP_ECN_MASK) {
1634 return xasprintf("%s: not a valid ECN", arg);
1636 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1641 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1643 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1646 /* Set IPv4/v6 TTL actions. */
1649 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1650 enum ofp_version ofp_version OVS_UNUSED,
1653 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1658 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1659 enum ofp_version ofp_version, struct ofpbuf *out)
1661 if (ofp_version >= OFP11_VERSION) {
1662 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1668 static char * OVS_WARN_UNUSED_RESULT
1669 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1670 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1675 error = str_to_u8(arg, "TTL", &ttl);
1680 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1685 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1687 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1690 /* Set TCP/UDP/SCTP port actions. */
1693 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1694 enum ofp_version ofp_version OVS_UNUSED,
1697 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1702 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1703 enum ofp_version ofp_version OVS_UNUSED,
1706 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1711 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1712 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1713 enum mf_field_id field, struct ofpbuf *out)
1715 uint16_t port = l4_port->port;
1717 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1718 ofpact_put_set_field(out, ofp_version, field, port);
1720 ofpact_put_raw(out, ofp_version, raw, port);
1725 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1726 enum ofp_version ofp_version, struct ofpbuf *out)
1728 uint8_t proto = l4_port->flow_ip_proto;
1729 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1730 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1731 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1734 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1738 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1739 enum ofp_version ofp_version,
1742 uint8_t proto = l4_port->flow_ip_proto;
1743 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1744 : proto == IPPROTO_UDP ? MFF_UDP_DST
1745 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1748 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1751 static char * OVS_WARN_UNUSED_RESULT
1752 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1753 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1755 return str_to_u16(arg, "source port",
1756 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1759 static char * OVS_WARN_UNUSED_RESULT
1760 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1761 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1763 return str_to_u16(arg, "destination port",
1764 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1768 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1770 ds_put_format(s, "mod_tp_src:%d", a->port);
1774 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1776 ds_put_format(s, "mod_tp_dst:%d", a->port);
1779 /* Action structure for OFPAT_COPY_FIELD. */
1780 struct ofp15_action_copy_field {
1781 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1782 ovs_be16 len; /* Length is padded to 64 bits. */
1783 ovs_be16 n_bits; /* Number of bits to copy. */
1784 ovs_be16 src_offset; /* Starting bit offset in source. */
1785 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1788 * - OXM header for source field.
1789 * - OXM header for destination field.
1790 * - Padding with 0-bytes to a multiple of 8 bytes.
1791 * The "pad2" member is the beginning of the above. */
1794 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1796 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1797 struct onf_action_copy_field {
1798 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
1799 ovs_be16 len; /* Length is padded to 64 bits. */
1800 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
1801 ovs_be16 exp_type; /* 3200. */
1802 uint8_t pad[2]; /* Not used. */
1803 ovs_be16 n_bits; /* Number of bits to copy. */
1804 ovs_be16 src_offset; /* Starting bit offset in source. */
1805 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1806 uint8_t pad2[2]; /* Not used. */
1808 * - OXM header for source field.
1809 * - OXM header for destination field.
1810 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1811 * The "pad3" member is the beginning of the above. */
1812 uint8_t pad3[4]; /* Not used. */
1814 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1816 /* Action structure for NXAST_REG_MOVE.
1818 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1819 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1820 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1821 * the next most significant bit, and so on.
1823 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1824 * sense to use nxm_hasmask=1 because the action does not do any kind of
1825 * matching; it uses the actual value of a field.)
1827 * The following nxm_header values are potentially acceptable as 'src':
1842 * - NXM_OF_ICMP_TYPE
1843 * - NXM_OF_ICMP_CODE
1850 * - NXM_NX_ICMPV6_TYPE
1851 * - NXM_NX_ICMPV6_CODE
1854 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1856 * - NXM_NX_TUN_IPV4_SRC
1857 * - NXM_NX_TUN_IPV4_DST
1859 * The following nxm_header values are potentially acceptable as 'dst':
1875 * Modifying any of the above fields changes the corresponding packet
1880 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1884 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1885 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1886 * header (if any), ignoring the other bits. Setting a value with CFI=1
1887 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1888 * to the field's new value (with the CFI bit masked out).
1890 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1891 * any of these values modifies the corresponding tunnel header field used
1892 * for the packet's next tunnel encapsulation, if allowed by the
1893 * configuration of the output tunnel port.
1895 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1896 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1897 * used only if the flow's nx_match includes an nxm_entry that specifies
1898 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1900 * The switch will reject actions for which src_ofs+n_bits is greater than the
1901 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1902 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1904 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1905 * behaves as if 'src' were copied out to a temporary buffer, then the
1906 * temporary buffer copied to 'dst'.
1908 struct nx_action_reg_move {
1909 ovs_be16 type; /* OFPAT_VENDOR. */
1910 ovs_be16 len; /* Length is 24. */
1911 ovs_be32 vendor; /* NX_VENDOR_ID. */
1912 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1913 ovs_be16 n_bits; /* Number of bits. */
1914 ovs_be16 src_ofs; /* Starting bit offset in source. */
1915 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1917 * - OXM/NXM header for source field (4 or 8 bytes).
1918 * - OXM/NXM header for destination field (4 or 8 bytes).
1919 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
1921 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
1924 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
1925 const void *action, ovs_be16 action_len, size_t oxm_offset,
1926 struct ofpbuf *ofpacts)
1928 struct ofpact_reg_move *move;
1932 move = ofpact_put_REG_MOVE(ofpacts);
1933 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
1934 move->src.ofs = ntohs(src_offset);
1935 move->src.n_bits = ntohs(n_bits);
1936 move->dst.ofs = ntohs(dst_offset);
1937 move->dst.n_bits = ntohs(n_bits);
1939 ofpbuf_use_const(&b, action, ntohs(action_len));
1940 ofpbuf_pull(&b, oxm_offset);
1941 error = nx_pull_header(&b, &move->src.field, NULL);
1945 error = nx_pull_header(&b, &move->dst.field, NULL);
1950 if (!is_all_zeros(b.data, b.size)) {
1951 return OFPERR_NXBRC_MUST_BE_ZERO;
1954 return nxm_reg_move_check(move, NULL);
1958 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
1959 enum ofp_version ofp_version OVS_UNUSED,
1960 struct ofpbuf *ofpacts)
1962 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1963 oacf->n_bits, oacf, oacf->len,
1964 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
1968 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
1969 enum ofp_version ofp_version OVS_UNUSED,
1970 struct ofpbuf *ofpacts)
1972 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1973 oacf->n_bits, oacf, oacf->len,
1974 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
1978 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
1979 enum ofp_version ofp_version OVS_UNUSED,
1980 struct ofpbuf *ofpacts)
1982 struct ofpact_reg_move *move;
1986 move = ofpact_put_REG_MOVE(ofpacts);
1987 move->ofpact.raw = NXAST_RAW_REG_MOVE;
1988 move->src.ofs = ntohs(narm->src_ofs);
1989 move->src.n_bits = ntohs(narm->n_bits);
1990 move->dst.ofs = ntohs(narm->dst_ofs);
1991 move->dst.n_bits = ntohs(narm->n_bits);
1993 ofpbuf_use_const(&b, narm, ntohs(narm->len));
1994 ofpbuf_pull(&b, sizeof *narm);
1995 error = nx_pull_header(&b, &move->src.field, NULL);
1999 error = nx_pull_header(&b, &move->dst.field, NULL);
2003 if (!is_all_zeros(b.data, b.size)) {
2004 return OFPERR_NXBRC_MUST_BE_ZERO;
2007 return nxm_reg_move_check(move, NULL);
2011 encode_REG_MOVE(const struct ofpact_reg_move *move,
2012 enum ofp_version ofp_version, struct ofpbuf *out)
2014 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2015 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2016 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2017 * probe for support. Until we have that ability, we currently prefer
2018 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2020 size_t start_ofs = out->size;
2021 if (ofp_version >= OFP15_VERSION) {
2022 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2023 copy->n_bits = htons(move->dst.n_bits);
2024 copy->src_offset = htons(move->src.ofs);
2025 copy->dst_offset = htons(move->dst.ofs);
2026 out->size = out->size - sizeof copy->pad2;
2027 nx_put_header(out, move->src.field->id, ofp_version, false);
2028 nx_put_header(out, move->dst.field->id, ofp_version, false);
2029 } else if (ofp_version == OFP13_VERSION
2030 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2031 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2032 copy->n_bits = htons(move->dst.n_bits);
2033 copy->src_offset = htons(move->src.ofs);
2034 copy->dst_offset = htons(move->dst.ofs);
2035 out->size = out->size - sizeof copy->pad3;
2036 nx_put_header(out, move->src.field->id, ofp_version, false);
2037 nx_put_header(out, move->dst.field->id, ofp_version, false);
2039 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2040 narm->n_bits = htons(move->dst.n_bits);
2041 narm->src_ofs = htons(move->src.ofs);
2042 narm->dst_ofs = htons(move->dst.ofs);
2043 nx_put_header(out, move->src.field->id, 0, false);
2044 nx_put_header(out, move->dst.field->id, 0, false);
2046 pad_ofpat(out, start_ofs);
2049 static char * OVS_WARN_UNUSED_RESULT
2050 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2051 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2053 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2054 const char *full_arg = arg;
2057 error = mf_parse_subfield__(&move->src, &arg);
2061 if (strncmp(arg, "->", 2)) {
2062 return xasprintf("%s: missing `->' following source", full_arg);
2065 error = mf_parse_subfield(&move->dst, arg);
2070 if (move->src.n_bits != move->dst.n_bits) {
2071 return xasprintf("%s: source field is %d bits wide but destination is "
2072 "%d bits wide", full_arg,
2073 move->src.n_bits, move->dst.n_bits);
2079 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2081 nxm_format_reg_move(a, s);
2084 /* Action structure for OFPAT12_SET_FIELD. */
2085 struct ofp12_action_set_field {
2086 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2087 ovs_be16 len; /* Length is padded to 64 bits. */
2090 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2091 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2093 * The "pad" member is the beginning of the above. */
2096 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2098 /* Action structure for NXAST_REG_LOAD.
2100 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2101 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2102 * starts at 0 for the least-significant bit, 1 for the next most significant
2105 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2106 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2109 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2110 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2111 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2112 * also stored as one less than its true value:
2115 * +------------------------------+------------------+
2116 * | ofs | n_bits - 1 |
2117 * +------------------------------+------------------+
2119 * The switch will reject actions for which ofs+n_bits is greater than the
2120 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2121 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2122 * OFPBAC_BAD_ARGUMENT.
2124 struct nx_action_reg_load {
2125 ovs_be16 type; /* OFPAT_VENDOR. */
2126 ovs_be16 len; /* Length is 24. */
2127 ovs_be32 vendor; /* NX_VENDOR_ID. */
2128 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2129 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2130 ovs_be32 dst; /* Destination register. */
2131 ovs_be64 value; /* Immediate value. */
2133 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2135 /* Action structure for NXAST_REG_LOAD2.
2137 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2138 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2139 * OXM experimenter fields. */
2140 struct nx_action_reg_load2 {
2141 ovs_be16 type; /* OFPAT_VENDOR. */
2142 ovs_be16 len; /* At least 16. */
2143 ovs_be32 vendor; /* NX_VENDOR_ID. */
2144 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2147 * - An NXM/OXM header, value, and optionally a mask.
2148 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2150 * The "pad" member is the beginning of the above. */
2153 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2156 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2157 bool may_mask, struct ofpbuf *ofpacts)
2159 struct ofpact_set_field *sf;
2163 sf = ofpact_put_SET_FIELD(ofpacts);
2165 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2166 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2167 error = nx_pull_entry(&b, &sf->field, &sf->value,
2168 may_mask ? &sf->mask : NULL);
2170 return (error == OFPERR_OFPBMC_BAD_MASK
2171 ? OFPERR_OFPBAC_BAD_SET_MASK
2175 memset(&sf->mask, 0xff, sf->field->n_bytes);
2178 if (!is_all_zeros(b.data, b.size)) {
2179 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2182 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2184 if (sf->field->id == MFF_IN_PORT_OXM) {
2185 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2188 /* oxm_length is now validated to be compatible with mf_value. */
2189 if (!sf->field->writable) {
2190 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2192 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2195 /* The value must be valid for match. OpenFlow 1.5 also says,
2196 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2197 * a 1-bit in oxm_value and in oxm_mask." */
2198 if (!mf_is_value_valid(sf->field, &sf->value)
2199 || (sf->field->id == MFF_VLAN_VID
2200 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2201 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2202 struct ds ds = DS_EMPTY_INITIALIZER;
2203 mf_format(sf->field, &sf->value, NULL, &ds);
2204 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2205 sf->field->name, ds_cstr(&ds));
2208 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2214 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2215 enum ofp_version ofp_version OVS_UNUSED,
2216 struct ofpbuf *ofpacts)
2218 return decode_ofpat_set_field(oasf, false, ofpacts);
2222 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2223 enum ofp_version ofp_version OVS_UNUSED,
2224 struct ofpbuf *ofpacts)
2226 return decode_ofpat_set_field(oasf, true, ofpacts);
2230 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2231 enum ofp_version ofp_version OVS_UNUSED,
2234 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2235 struct mf_subfield dst;
2238 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2240 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2241 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2242 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2243 error = mf_check_dst(&dst, NULL);
2248 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2250 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2251 return OFPERR_OFPBAC_BAD_ARGUMENT;
2254 sf->field = dst.field;
2255 bitwise_put(ntohll(narl->value),
2256 &sf->value, dst.field->n_bytes, dst.ofs,
2258 bitwise_put(UINT64_MAX,
2259 &sf->mask, dst.field->n_bytes, dst.ofs,
2266 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2267 enum ofp_version ofp_version OVS_UNUSED,
2270 struct ofpact_set_field *sf;
2274 sf = ofpact_put_SET_FIELD(out);
2275 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2277 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2278 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2279 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2283 if (!is_all_zeros(b.data, b.size)) {
2284 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2287 if (!sf->field->writable) {
2288 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2290 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2296 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2297 enum mf_field_id field, uint64_t value_)
2299 struct ofp12_action_set_field *oasf OVS_UNUSED;
2300 int n_bytes = mf_from_id(field)->n_bytes;
2301 size_t start_ofs = openflow->size;
2302 union mf_value value;
2304 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2306 oasf = put_OFPAT12_SET_FIELD(openflow);
2307 openflow->size = openflow->size - sizeof oasf->pad;
2308 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2309 pad_ofpat(openflow, start_ofs);
2313 next_load_segment(const struct ofpact_set_field *sf,
2314 struct mf_subfield *dst, uint64_t *value)
2316 int n_bits = sf->field->n_bits;
2317 int n_bytes = sf->field->n_bytes;
2318 int start = dst->ofs + dst->n_bits;
2320 if (start < n_bits) {
2321 dst->field = sf->field;
2322 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2323 if (dst->ofs < n_bits) {
2324 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2325 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2326 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2333 /* Convert 'sf' to a series of REG_LOADs. */
2335 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2337 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2338 * experimenter OXM or is variable length (or if it came in as
2339 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2340 * NXAST_REG_LOAD, which is backward compatible. */
2341 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2342 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2343 struct nx_action_reg_load2 *narl OVS_UNUSED;
2344 size_t start_ofs = openflow->size;
2346 narl = put_NXAST_REG_LOAD2(openflow);
2347 openflow->size = openflow->size - sizeof narl->pad;
2348 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2349 pad_ofpat(openflow, start_ofs);
2351 struct mf_subfield dst;
2354 dst.ofs = dst.n_bits = 0;
2355 while (next_load_segment(sf, &dst, &value)) {
2356 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2357 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2358 narl->dst = htonl(mf_nxm_header(dst.field->id));
2359 narl->value = htonll(value);
2364 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2365 * actions, if we can, falling back to Nicira extensions if we must.
2367 * We check only meta-flow types that can appear within set field actions and
2368 * that have a mapping to compatible action types. These struct mf_field
2369 * definitions have a defined OXM or NXM header value and specify the field as
2372 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2373 enum ofp_version ofp_version,
2376 switch ((int) sf->field->id) {
2377 case MFF_VLAN_TCI: {
2378 ovs_be16 tci = sf->value.be16;
2379 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2380 uint16_t vid = vlan_tci_to_vid(tci);
2381 uint8_t pcp = vlan_tci_to_pcp(tci);
2383 if (ofp_version < OFP11_VERSION) {
2384 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2386 * If CFI=1, Add or modify VLAN VID & PCP.
2387 * If CFI=0, strip VLAN header, if any.
2390 put_OFPAT10_SET_VLAN_VID(out, vid);
2391 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2393 put_OFPAT10_STRIP_VLAN(out);
2396 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2398 * If CFI=1, Add or modify VLAN VID & PCP.
2399 * OpenFlow 1.1 set actions only apply if the packet
2400 * already has VLAN tags. To be sure that is the case
2401 * we have to push a VLAN header. As we do not support
2402 * multiple layers of VLANs, this is a no-op, if a VLAN
2403 * header already exists. This may backfire, however,
2404 * when we start supporting multiple layers of VLANs.
2405 * If CFI=0, strip VLAN header, if any.
2408 /* Push a VLAN tag, if one was not seen at action validation
2410 if (!sf->flow_has_vlan) {
2411 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2413 put_OFPAT11_SET_VLAN_VID(out, vid);
2414 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2416 /* If the flow did not match on vlan, we have no way of
2417 * knowing if the vlan tag exists, so we must POP just to be
2419 put_OFPAT11_POP_VLAN(out);
2425 case MFF_VLAN_VID: {
2426 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2427 if (ofp_version == OFP10_VERSION) {
2428 put_OFPAT10_SET_VLAN_VID(out, vid);
2430 put_OFPAT11_SET_VLAN_VID(out, vid);
2436 if (ofp_version == OFP10_VERSION) {
2437 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2439 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2444 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2448 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2452 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2456 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2460 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2463 case MFF_IP_DSCP_SHIFTED:
2464 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2469 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2474 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2478 set_field_to_nxast(sf, out);
2484 set_field_to_set_field(const struct ofpact_set_field *sf,
2485 enum ofp_version ofp_version, struct ofpbuf *out)
2487 struct ofp12_action_set_field *oasf OVS_UNUSED;
2488 size_t start_ofs = out->size;
2490 oasf = put_OFPAT12_SET_FIELD(out);
2491 out->size = out->size - sizeof oasf->pad;
2492 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2493 pad_ofpat(out, start_ofs);
2497 encode_SET_FIELD(const struct ofpact_set_field *sf,
2498 enum ofp_version ofp_version, struct ofpbuf *out)
2500 if (ofp_version >= OFP15_VERSION) {
2501 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2503 set_field_to_set_field(sf, ofp_version, out);
2504 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2505 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2506 /* It came in as reg_load, send it out the same way. */
2507 set_field_to_nxast(sf, out);
2508 } else if (ofp_version < OFP12_VERSION) {
2509 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2510 set_field_to_legacy_openflow(sf, ofp_version, out);
2511 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2512 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2513 * entire field, so encode it as OFPAT_SET_FIELD. */
2514 set_field_to_set_field(sf, ofp_version, out);
2516 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2517 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2518 * so encode it as reg_load. */
2519 set_field_to_nxast(sf, out);
2523 /* Parses the input argument 'arg' into the key, value, and delimiter
2524 * components that are common across the reg_load and set_field action format.
2526 * With an argument like "1->metadata", sets the following pointers to
2527 * point within 'arg':
2532 * Returns NULL if successful, otherwise a malloc()'d string describing the
2533 * error. The caller is responsible for freeing the returned string. */
2534 static char * OVS_WARN_UNUSED_RESULT
2535 set_field_split_str(char *arg, char **key, char **value, char **delim)
2540 value_end = strstr(arg, "->");
2541 *key = value_end + strlen("->");
2547 return xasprintf("%s: missing `->'", arg);
2549 if (strlen(value_end) <= strlen("->")) {
2550 return xasprintf("%s: missing field name following `->'", arg);
2556 /* Parses a "set_field" action with argument 'arg', appending the parsed
2557 * action to 'ofpacts'.
2559 * Returns NULL if successful, otherwise a malloc()'d string describing the
2560 * error. The caller is responsible for freeing the returned string. */
2561 static char * OVS_WARN_UNUSED_RESULT
2562 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2563 enum ofputil_protocol *usable_protocols)
2565 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2569 const struct mf_field *mf;
2572 error = set_field_split_str(arg, &key, &value, &delim);
2577 mf = mf_from_name(key);
2579 return xasprintf("%s is not a valid OXM field name", key);
2581 if (!mf->writable) {
2582 return xasprintf("%s is read-only", key);
2586 error = mf_parse(mf, value, &sf->value, &sf->mask);
2591 if (!mf_is_value_valid(mf, &sf->value)) {
2592 return xasprintf("%s is not a valid value for field %s", value, key);
2595 *usable_protocols &= mf->usable_protocols_exact;
2599 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2600 * action to 'ofpacts'.
2602 * Returns NULL if successful, otherwise a malloc()'d string describing the
2603 * error. The caller is responsible for freeing the returned string. */
2604 static char * OVS_WARN_UNUSED_RESULT
2605 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2606 enum ofputil_protocol *usable_protocols)
2608 char *copy = xstrdup(arg);
2609 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2614 static char * OVS_WARN_UNUSED_RESULT
2615 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2617 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2618 struct mf_subfield dst;
2619 char *key, *value_str;
2620 union mf_value value;
2623 error = set_field_split_str(arg, &key, &value_str, NULL);
2628 error = mf_parse_subfield(&dst, key);
2633 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2635 return xasprintf("%s: cannot parse integer value", arg);
2638 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2639 dst.field->n_bytes * 8 - dst.n_bits)) {
2643 mf_format(dst.field, &value, NULL, &ds);
2644 error = xasprintf("%s: value %s does not fit into %d bits",
2645 arg, ds_cstr(&ds), dst.n_bits);
2650 sf->field = dst.field;
2651 memset(&sf->value, 0, sizeof sf->value);
2652 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2653 dst.field->n_bytes, dst.ofs, dst.n_bits);
2654 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2660 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2662 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2663 struct mf_subfield dst;
2666 dst.ofs = dst.n_bits = 0;
2667 while (next_load_segment(a, &dst, &value)) {
2668 ds_put_format(s, "load:%#"PRIx64"->", value);
2669 mf_format_subfield(&dst, s);
2670 ds_put_char(s, ',');
2674 ds_put_cstr(s, "set_field:");
2675 mf_format(a->field, &a->value, &a->mask, s);
2676 ds_put_format(s, "->%s", a->field->name);
2680 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2681 * is marked such that, if possible, it will be translated to OpenFlow as
2682 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2683 * that was the way that the action was expressed when it came into OVS or for
2684 * backward compatibility. */
2685 struct ofpact_set_field *
2686 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2688 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2689 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2693 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2695 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2698 struct nx_action_stack {
2699 ovs_be16 type; /* OFPAT_VENDOR. */
2700 ovs_be16 len; /* Length is 16. */
2701 ovs_be32 vendor; /* NX_VENDOR_ID. */
2702 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2703 ovs_be16 offset; /* Bit offset into the field. */
2705 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2706 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2707 * - Enough 0-bytes to pad out the action to 24 bytes. */
2708 uint8_t pad[12]; /* See above. */
2710 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2713 decode_stack_action(const struct nx_action_stack *nasp,
2714 struct ofpact_stack *stack_action)
2719 stack_action->subfield.ofs = ntohs(nasp->offset);
2721 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2722 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2723 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2727 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2729 if (!is_all_zeros(b.data, b.size)) {
2730 return OFPERR_NXBRC_MUST_BE_ZERO;
2737 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2738 enum ofp_version ofp_version OVS_UNUSED,
2739 struct ofpbuf *ofpacts)
2741 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2742 enum ofperr error = decode_stack_action(nasp, push);
2743 return error ? error : nxm_stack_push_check(push, NULL);
2747 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2748 enum ofp_version ofp_version OVS_UNUSED,
2749 struct ofpbuf *ofpacts)
2751 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2752 enum ofperr error = decode_stack_action(nasp, pop);
2753 return error ? error : nxm_stack_pop_check(pop, NULL);
2757 encode_STACK_op(const struct ofpact_stack *stack_action,
2758 struct nx_action_stack *nasp)
2763 nasp->offset = htons(stack_action->subfield.ofs);
2765 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2766 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2767 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2768 n_bits = htons(stack_action->subfield.n_bits);
2769 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2773 encode_STACK_PUSH(const struct ofpact_stack *stack,
2774 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2776 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2780 encode_STACK_POP(const struct ofpact_stack *stack,
2781 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2783 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2786 static char * OVS_WARN_UNUSED_RESULT
2787 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2788 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2790 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2793 static char * OVS_WARN_UNUSED_RESULT
2794 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2795 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2797 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2801 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2803 nxm_format_stack_push(a, s);
2807 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2809 nxm_format_stack_pop(a, s);
2812 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2814 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2815 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2816 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2817 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2819 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2820 * sent only to controllers with id 0.)
2822 struct nx_action_cnt_ids {
2823 ovs_be16 type; /* OFPAT_VENDOR. */
2824 ovs_be16 len; /* Length including slaves. */
2825 ovs_be32 vendor; /* NX_VENDOR_ID. */
2826 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2828 ovs_be16 n_controllers; /* Number of controllers. */
2829 uint8_t zeros[4]; /* Must be zero. */
2831 /* Followed by 1 or more controller ids.
2833 * uint16_t cnt_ids[]; // Controller ids.
2834 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2837 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2840 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2843 struct ofpact_cnt_ids *ids;
2844 enum ofperr error = 0;
2846 ids = ofpact_put_DEC_TTL(out);
2847 ids->n_controllers = 1;
2848 ofpbuf_put(out, &id, sizeof id);
2850 ofpact_update_len(out, &ids->ofpact);
2855 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2856 enum ofp_version ofp_version OVS_UNUSED,
2859 struct ofpact_cnt_ids *ids;
2863 ids = ofpact_put_DEC_TTL(out);
2864 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2865 ids->n_controllers = ntohs(nac_ids->n_controllers);
2866 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2868 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2869 return OFPERR_NXBRC_MUST_BE_ZERO;
2872 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2873 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2874 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2875 "are required for %"PRIu16" controllers.",
2876 ids_size, ids->n_controllers * sizeof(ovs_be16),
2877 ids->n_controllers);
2878 return OFPERR_OFPBAC_BAD_LEN;
2881 for (i = 0; i < ids->n_controllers; i++) {
2882 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2883 ofpbuf_put(out, &id, sizeof id);
2887 ofpact_update_len(out, &ids->ofpact);
2893 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2894 enum ofp_version ofp_version, struct ofpbuf *out)
2896 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2897 || dec_ttl->n_controllers != 1
2898 || dec_ttl->cnt_ids[0] != 0) {
2899 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2900 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2904 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2905 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2907 ids = ofpbuf_put_zeros(out, ids_len);
2908 for (i = 0; i < dec_ttl->n_controllers; i++) {
2909 ids[i] = htons(dec_ttl->cnt_ids[i]);
2912 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2917 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2919 struct ofpact_cnt_ids *ids;
2922 ofpact_put_DEC_TTL(ofpacts);
2923 ofpbuf_put(ofpacts, &id, sizeof id);
2924 ids = ofpacts->header;
2925 ids->n_controllers++;
2926 ofpact_update_len(ofpacts, &ids->ofpact);
2929 static char * OVS_WARN_UNUSED_RESULT
2930 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2931 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2934 parse_noargs_dec_ttl(ofpacts);
2936 struct ofpact_cnt_ids *ids;
2939 ids = ofpact_put_DEC_TTL(ofpacts);
2940 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2941 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2942 cntr = strtok_r(NULL, ", ", &arg)) {
2943 uint16_t id = atoi(cntr);
2945 ofpbuf_put(ofpacts, &id, sizeof id);
2946 ids = ofpacts->header;
2947 ids->n_controllers++;
2949 if (!ids->n_controllers) {
2950 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2953 ofpact_update_len(ofpacts, &ids->ofpact);
2959 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2963 ds_put_cstr(s, "dec_ttl");
2964 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2965 ds_put_cstr(s, "(");
2966 for (i = 0; i < a->n_controllers; i++) {
2968 ds_put_cstr(s, ",");
2970 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2972 ds_put_cstr(s, ")");
2976 /* Set MPLS label actions. */
2979 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
2980 enum ofp_version ofp_version OVS_UNUSED,
2983 ofpact_put_SET_MPLS_LABEL(out)->label = label;
2988 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
2989 enum ofp_version ofp_version,
2992 if (ofp_version < OFP12_VERSION) {
2993 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
2995 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
2996 ntohl(label->label));
3000 static char * OVS_WARN_UNUSED_RESULT
3001 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3002 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3004 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3006 return xstrdup("set_mpls_label: expected label.");
3009 mpls_label->label = htonl(atoi(arg));
3014 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3016 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
3019 /* Set MPLS TC actions. */
3022 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3023 enum ofp_version ofp_version OVS_UNUSED,
3026 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3031 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3032 enum ofp_version ofp_version, struct ofpbuf *out)
3034 if (ofp_version < OFP12_VERSION) {
3035 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3037 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3041 static char * OVS_WARN_UNUSED_RESULT
3042 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3043 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3045 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3048 return xstrdup("set_mpls_tc: expected tc.");
3051 mpls_tc->tc = atoi(arg);
3056 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3058 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
3061 /* Set MPLS TTL actions. */
3064 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3065 enum ofp_version ofp_version OVS_UNUSED,
3068 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3073 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3074 enum ofp_version ofp_version, struct ofpbuf *out)
3076 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3079 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3080 * action to 'ofpacts'.
3082 * Returns NULL if successful, otherwise a malloc()'d string describing the
3083 * error. The caller is responsible for freeing the returned string. */
3084 static char * OVS_WARN_UNUSED_RESULT
3085 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3086 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3088 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3091 return xstrdup("set_mpls_ttl: expected ttl.");
3094 mpls_ttl->ttl = atoi(arg);
3099 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3101 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3104 /* Decrement MPLS TTL actions. */
3107 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3109 ofpact_put_DEC_MPLS_TTL(out);
3114 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3115 enum ofp_version ofp_version, struct ofpbuf *out)
3117 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3120 static char * OVS_WARN_UNUSED_RESULT
3121 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3122 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3124 ofpact_put_DEC_MPLS_TTL(ofpacts);
3129 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3131 ds_put_cstr(s, "dec_mpls_ttl");
3134 /* Push MPLS label action. */
3137 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3138 enum ofp_version ofp_version OVS_UNUSED,
3141 struct ofpact_push_mpls *oam;
3143 if (!eth_type_mpls(ethertype)) {
3144 return OFPERR_OFPBAC_BAD_ARGUMENT;
3146 oam = ofpact_put_PUSH_MPLS(out);
3147 oam->ethertype = ethertype;
3153 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3154 enum ofp_version ofp_version, struct ofpbuf *out)
3156 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3159 static char * OVS_WARN_UNUSED_RESULT
3160 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3161 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3166 error = str_to_u16(arg, "push_mpls", ðertype);
3168 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3174 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3176 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3179 /* Pop MPLS label action. */
3182 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3183 enum ofp_version ofp_version OVS_UNUSED,
3186 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3191 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3192 enum ofp_version ofp_version, struct ofpbuf *out)
3194 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3197 static char * OVS_WARN_UNUSED_RESULT
3198 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3199 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3204 error = str_to_u16(arg, "pop_mpls", ðertype);
3206 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3212 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3214 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3217 /* Set tunnel ID actions. */
3220 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3221 enum ofp_version ofp_version OVS_UNUSED,
3224 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3225 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3226 tunnel->tun_id = tun_id;
3231 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3232 enum ofp_version ofp_version OVS_UNUSED,
3235 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3236 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3237 tunnel->tun_id = tun_id;
3242 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3243 enum ofp_version ofp_version, struct ofpbuf *out)
3245 uint64_t tun_id = tunnel->tun_id;
3247 if (ofp_version < OFP12_VERSION) {
3248 if (tun_id <= UINT32_MAX
3249 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3250 put_NXAST_SET_TUNNEL(out, tun_id);
3252 put_NXAST_SET_TUNNEL64(out, tun_id);
3255 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3259 static char * OVS_WARN_UNUSED_RESULT
3260 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3261 enum ofp_raw_action_type raw)
3263 struct ofpact_tunnel *tunnel;
3265 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3266 tunnel->ofpact.raw = raw;
3267 return str_to_u64(arg, &tunnel->tun_id);
3270 static char * OVS_WARN_UNUSED_RESULT
3271 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3272 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3274 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3278 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3280 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3281 (a->tun_id > UINT32_MAX
3282 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3286 /* Set queue action. */
3289 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3290 enum ofp_version ofp_version OVS_UNUSED,
3293 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3298 encode_SET_QUEUE(const struct ofpact_queue *queue,
3299 enum ofp_version ofp_version, struct ofpbuf *out)
3301 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3304 static char * OVS_WARN_UNUSED_RESULT
3305 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3306 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3308 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3312 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3314 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3317 /* Pop queue action. */
3320 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3322 ofpact_put_POP_QUEUE(out);
3327 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3328 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3330 put_NXAST_POP_QUEUE(out);
3333 static char * OVS_WARN_UNUSED_RESULT
3334 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3335 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3337 ofpact_put_POP_QUEUE(ofpacts);
3342 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3344 ds_put_cstr(s, "pop_queue");
3347 /* Action structure for NXAST_FIN_TIMEOUT.
3349 * This action changes the idle timeout or hard timeout, or both, of this
3350 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3351 * When such a packet is observed, the action reduces the rule's idle timeout
3352 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3353 * action has no effect on an existing timeout that is already shorter than the
3354 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3355 * of zero has no effect on the respective timeout.
3357 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3358 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3359 * receipt of the FIN or RST.
3361 * This is useful for quickly discarding learned TCP flows that otherwise will
3362 * take a long time to expire.
3364 * This action is intended for use with an OpenFlow rule that matches only a
3365 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3366 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3367 * RST in any of those flows will cause the entire OpenFlow rule to expire
3368 * early, which is not normally desirable.
3370 struct nx_action_fin_timeout {
3371 ovs_be16 type; /* OFPAT_VENDOR. */
3372 ovs_be16 len; /* 16. */
3373 ovs_be32 vendor; /* NX_VENDOR_ID. */
3374 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3375 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3376 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3377 ovs_be16 pad; /* Must be zero. */
3379 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3382 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3383 enum ofp_version ofp_version OVS_UNUSED,
3386 struct ofpact_fin_timeout *oft;
3388 oft = ofpact_put_FIN_TIMEOUT(out);
3389 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3390 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3395 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3396 enum ofp_version ofp_version OVS_UNUSED,
3399 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3400 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3401 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3404 static char * OVS_WARN_UNUSED_RESULT
3405 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3406 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3408 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3411 while (ofputil_parse_key_value(&arg, &key, &value)) {
3414 if (!strcmp(key, "idle_timeout")) {
3415 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3416 } else if (!strcmp(key, "hard_timeout")) {
3417 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3419 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3431 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3433 ds_put_cstr(s, "fin_timeout(");
3434 if (a->fin_idle_timeout) {
3435 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3437 if (a->fin_hard_timeout) {
3438 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3441 ds_put_char(s, ')');
3444 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3446 * These actions search one of the switch's flow tables:
3448 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3449 * it specifies the table to search.
3451 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3452 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3453 * table, that is, the OpenFlow flow table that contains the flow from
3454 * which this action was obtained. If this action did not come from a
3455 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3456 * is the current table.
3458 * The flow table lookup uses a flow that may be slightly modified from the
3461 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3462 * is used as the flow's in_port.
3464 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3465 * then its value is used as the flow's in_port. Otherwise, the original
3468 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3469 * resubmit action, then the flow is updated with the new values.
3471 * Following the lookup, the original in_port is restored.
3473 * If the modified flow matched in the flow table, then the corresponding
3474 * actions are executed. Afterward, actions following the resubmit in the
3475 * original set of actions, if any, are executed; any changes made to the
3476 * packet (e.g. changes to VLAN) by secondary actions persist when those
3477 * actions are executed, although the original in_port is restored.
3479 * Resubmit actions may be used any number of times within a set of actions.
3481 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3482 * implementation-defined depth, further resubmit actions are simply ignored.
3484 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3485 * 'pad' to be all-bits-zero.
3487 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3488 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3490 struct nx_action_resubmit {
3491 ovs_be16 type; /* OFPAT_VENDOR. */
3492 ovs_be16 len; /* Length is 16. */
3493 ovs_be32 vendor; /* NX_VENDOR_ID. */
3494 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3495 ovs_be16 in_port; /* New in_port for checking flow table. */
3496 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3499 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3502 decode_NXAST_RAW_RESUBMIT(uint16_t port,
3503 enum ofp_version ofp_version OVS_UNUSED,
3506 struct ofpact_resubmit *resubmit;
3508 resubmit = ofpact_put_RESUBMIT(out);
3509 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3510 resubmit->in_port = u16_to_ofp(port);
3511 resubmit->table_id = 0xff;
3516 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3517 enum ofp_version ofp_version OVS_UNUSED,
3520 struct ofpact_resubmit *resubmit;
3522 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3523 return OFPERR_OFPBAC_BAD_ARGUMENT;
3526 resubmit = ofpact_put_RESUBMIT(out);
3527 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3528 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3529 resubmit->table_id = nar->table;
3534 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3535 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3537 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3539 if (resubmit->table_id == 0xff
3540 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3541 put_NXAST_RESUBMIT(out, in_port);
3543 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3544 nar->table = resubmit->table_id;
3545 nar->in_port = htons(in_port);
3549 static char * OVS_WARN_UNUSED_RESULT
3550 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3551 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3553 struct ofpact_resubmit *resubmit;
3554 char *in_port_s, *table_s;
3556 resubmit = ofpact_put_RESUBMIT(ofpacts);
3558 in_port_s = strsep(&arg, ",");
3559 if (in_port_s && in_port_s[0]) {
3560 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3561 return xasprintf("%s: resubmit to unknown port", in_port_s);
3564 resubmit->in_port = OFPP_IN_PORT;
3567 table_s = strsep(&arg, ",");
3568 if (table_s && table_s[0]) {
3569 uint32_t table_id = 0;
3572 error = str_to_u32(table_s, &table_id);
3576 resubmit->table_id = table_id;
3578 resubmit->table_id = 255;
3581 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3582 return xstrdup("at least one \"in_port\" or \"table\" must be "
3583 "specified on resubmit");
3589 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3591 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3592 ds_put_cstr(s, "resubmit:");
3593 ofputil_format_port(a->in_port, s);
3595 ds_put_format(s, "resubmit(");
3596 if (a->in_port != OFPP_IN_PORT) {
3597 ofputil_format_port(a->in_port, s);
3599 ds_put_char(s, ',');
3600 if (a->table_id != 255) {
3601 ds_put_format(s, "%"PRIu8, a->table_id);
3603 ds_put_char(s, ')');
3607 /* Action structure for NXAST_LEARN.
3609 * This action adds or modifies a flow in an OpenFlow table, similar to
3610 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3611 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3612 * flow's match criteria and actions are built by applying each of the series
3613 * of flow_mod_spec elements included as part of the action.
3615 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3616 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3617 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3618 * bits from a source to a destination. In this case, the header contains
3621 * 15 14 13 12 11 10 0
3622 * +------+---+------+---------------------------------+
3623 * | 0 |src| dst | n_bits |
3624 * +------+---+------+---------------------------------+
3626 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3627 * following table summarizes the meaning of each possible combination.
3628 * Details follow the table:
3631 * --- --- ----------------------------------------------------------
3632 * 0 0 Add match criteria based on value in a field.
3633 * 1 0 Add match criteria based on an immediate value.
3634 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3635 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3636 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3637 * All other combinations are undefined and not allowed.
3639 * The flow_mod_spec header is followed by a source specification and a
3640 * destination specification. The format and meaning of the source
3641 * specification depends on 'src':
3643 * - If 'src' is 0, the source bits are taken from a field in the flow to
3644 * which this action is attached. (This should be a wildcarded field. If
3645 * its value is fully specified then the source bits being copied have
3648 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3649 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3650 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3651 * 'field' and 'ofs' are subject to the same restrictions as the source
3652 * field in NXAST_REG_MOVE.
3654 * - If 'src' is 1, the source bits are a constant value. The source
3655 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3656 * number in network order, the source bits are the 'n_bits'
3657 * least-significant bits. The switch will report an error if other bits
3658 * in the constant are nonzero.
3660 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3661 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3662 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3663 * meaning of the flow_mod_spec depends on 'dst':
3665 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3666 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3667 * packet equal the source bits. 'field' may be any nxm_header with
3668 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3670 * Order is significant. Earlier flow_mod_specs must satisfy any
3671 * prerequisites for matching fields specified later, by copying constant
3672 * values into prerequisite fields.
3674 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3677 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3678 * the new flow. The new flow copies the source bits into
3679 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3682 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3683 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3685 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3686 * empty. It has the following meaning:
3688 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3689 * The new flow outputs to the OpenFlow port specified by the source field.
3690 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3691 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3694 * Resource Management
3695 * -------------------
3697 * A switch has a finite amount of flow table space available for learning.
3698 * When this space is exhausted, no new learning table entries will be learned
3699 * until some existing flow table entries expire. The controller should be
3700 * prepared to handle this by flooding (which can be implemented as a
3701 * low-priority flow).
3703 * If a learned flow matches a single TCP stream with a relatively long
3704 * timeout, one may make the best of resource constraints by setting
3705 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3706 * both, to shorter timeouts. When either of these is specified as a nonzero
3707 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3713 * The following examples give a prose description of the flow_mod_specs along
3714 * with informal notation for how those would be represented and a hex dump of
3715 * the bytes that would be required.
3717 * These examples could work with various nx_action_learn parameters. Typical
3718 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3719 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3721 * 1. Learn input port based on the source MAC, with lookup into
3722 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3724 * Match on in_port=99:
3725 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3726 * ovs_be16(99), 00 63
3727 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3729 * Match Ethernet destination on Ethernet source from packet:
3730 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3731 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3732 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3734 * Set NXM_NX_REG1[16:31] to the packet's input port:
3735 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3736 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3737 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3739 * Given a packet that arrived on port A with Ethernet source address B,
3740 * this would set up the flow "in_port=99, dl_dst=B,
3741 * actions=load:A->NXM_NX_REG1[16..31]".
3743 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3744 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3745 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3747 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3748 * into NXM_NX_REG1[16:31]:
3750 * Match on same VLAN ID as packet:
3751 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3752 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3753 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3755 * Match Ethernet destination on Ethernet source from packet:
3756 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3757 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3758 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3760 * Output to the packet's input port:
3761 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3762 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3764 * Given a packet that arrived on port A with Ethernet source address B in
3765 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3766 * actions=output:A".
3768 * In syntax accepted by ovs-ofctl, this action is:
3769 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3770 * output:NXM_OF_IN_PORT[])
3772 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3773 * 10-second MAC expiration time to make it easier to see what's going on
3775 * ovs-vsctl del-controller br0
3776 * ovs-ofctl del-flows br0
3777 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3778 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3779 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3780 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3781 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3783 * You can then dump the MAC learning table with:
3785 * ovs-ofctl dump-flows br0 table=1
3790 * For best performance, segregate learned flows into a table that is not used
3791 * for any other flows except possibly for a lowest-priority "catch-all" flow
3792 * (a flow with no match criteria). If different learning actions specify
3793 * different match criteria, use different tables for the learned flows.
3795 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3796 * These timeouts apply to the flow that is added, which means that a flow with
3797 * an idle timeout will expire when no traffic has been sent *to* the learned
3798 * address. This is not usually the intent in MAC learning; instead, we want
3799 * the MAC learn entry to expire when no traffic has been sent *from* the
3800 * learned address. Use a hard timeout for that.
3803 * Visibility of Changes
3804 * ---------------------
3806 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3807 * flow translation are visible to flow table lookups made later in the flow
3808 * translation. This means that, in the example above, a MAC learned by the
3809 * learn action in table 0 would be found in table 1 (if the packet being
3810 * processed had the same source and destination MAC address).
3812 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3813 * modify a flow) by a "learn" action are visible only for later flow
3814 * translations, not for later lookups within the same flow translation. In
3815 * the MAC learning example, a MAC learned by the learn action in table 0 would
3816 * not be found in table 1 if the flow translation would resubmit to table 1
3817 * after the processing of the learn action, meaning that if this MAC had not
3818 * been learned before then the packet would be flooded. */
3819 struct nx_action_learn {
3820 ovs_be16 type; /* OFPAT_VENDOR. */
3821 ovs_be16 len; /* At least 24. */
3822 ovs_be32 vendor; /* NX_VENDOR_ID. */
3823 ovs_be16 subtype; /* NXAST_LEARN. */
3824 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3825 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3826 ovs_be16 priority; /* Priority level of flow entry. */
3827 ovs_be64 cookie; /* Cookie for new flow. */
3828 ovs_be16 flags; /* NX_LEARN_F_*. */
3829 uint8_t table_id; /* Table to insert flow entry. */
3830 uint8_t pad; /* Must be zero. */
3831 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3832 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3833 /* Followed by a sequence of flow_mod_spec elements, as described above,
3834 * until the end of the action is reached. */
3836 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3839 get_be16(const void **pp)
3841 const ovs_be16 *p = *pp;
3842 ovs_be16 value = *p;
3848 get_be32(const void **pp)
3850 const ovs_be32 *p = *pp;
3851 ovs_be32 value = get_unaligned_be32(p);
3857 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3859 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3860 sf->ofs = ntohs(get_be16(p));
3861 sf->n_bits = n_bits;
3865 learn_min_len(uint16_t header)
3867 int n_bits = header & NX_LEARN_N_BITS_MASK;
3868 int src_type = header & NX_LEARN_SRC_MASK;
3869 int dst_type = header & NX_LEARN_DST_MASK;
3870 unsigned int min_len;
3873 if (src_type == NX_LEARN_SRC_FIELD) {
3874 min_len += sizeof(ovs_be32); /* src_field */
3875 min_len += sizeof(ovs_be16); /* src_ofs */
3877 min_len += DIV_ROUND_UP(n_bits, 16);
3879 if (dst_type == NX_LEARN_DST_MATCH ||
3880 dst_type == NX_LEARN_DST_LOAD) {
3881 min_len += sizeof(ovs_be32); /* dst_field */
3882 min_len += sizeof(ovs_be16); /* dst_ofs */
3887 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3888 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3890 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3891 enum ofp_version ofp_version OVS_UNUSED,
3892 struct ofpbuf *ofpacts)
3894 struct ofpact_learn *learn;
3895 const void *p, *end;
3898 return OFPERR_OFPBAC_BAD_ARGUMENT;
3901 learn = ofpact_put_LEARN(ofpacts);
3903 learn->idle_timeout = ntohs(nal->idle_timeout);
3904 learn->hard_timeout = ntohs(nal->hard_timeout);
3905 learn->priority = ntohs(nal->priority);
3906 learn->cookie = nal->cookie;
3907 learn->table_id = nal->table_id;
3908 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3909 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3911 learn->flags = ntohs(nal->flags);
3912 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3913 NX_LEARN_F_DELETE_LEARNED)) {
3914 return OFPERR_OFPBAC_BAD_ARGUMENT;
3917 if (learn->table_id == 0xff) {
3918 return OFPERR_OFPBAC_BAD_ARGUMENT;
3921 end = (char *) nal + ntohs(nal->len);
3922 for (p = nal + 1; p != end; ) {
3923 struct ofpact_learn_spec *spec;
3924 uint16_t header = ntohs(get_be16(&p));
3930 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3931 learn = ofpacts->header;
3934 spec->src_type = header & NX_LEARN_SRC_MASK;
3935 spec->dst_type = header & NX_LEARN_DST_MASK;
3936 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3938 /* Check for valid src and dst type combination. */
3939 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3940 spec->dst_type == NX_LEARN_DST_LOAD ||
3941 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3942 spec->src_type == NX_LEARN_SRC_FIELD)) {
3945 return OFPERR_OFPBAC_BAD_ARGUMENT;
3948 /* Check that the arguments don't overrun the end of the action. */
3949 if ((char *) end - (char *) p < learn_min_len(header)) {
3950 return OFPERR_OFPBAC_BAD_LEN;
3953 /* Get the source. */
3954 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3955 get_subfield(spec->n_bits, &p, &spec->src);
3957 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3959 bitwise_copy(p, p_bytes, 0,
3960 &spec->src_imm, sizeof spec->src_imm, 0,
3962 p = (const uint8_t *) p + p_bytes;
3965 /* Get the destination. */
3966 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3967 spec->dst_type == NX_LEARN_DST_LOAD) {
3968 get_subfield(spec->n_bits, &p, &spec->dst);
3971 ofpact_update_len(ofpacts, &learn->ofpact);
3973 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3974 return OFPERR_OFPBAC_BAD_ARGUMENT;
3981 put_be16(struct ofpbuf *b, ovs_be16 x)
3983 ofpbuf_put(b, &x, sizeof x);
3987 put_be32(struct ofpbuf *b, ovs_be32 x)
3989 ofpbuf_put(b, &x, sizeof x);
3993 put_u16(struct ofpbuf *b, uint16_t x)
3995 put_be16(b, htons(x));
3999 put_u32(struct ofpbuf *b, uint32_t x)
4001 put_be32(b, htonl(x));
4005 encode_LEARN(const struct ofpact_learn *learn,
4006 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4008 const struct ofpact_learn_spec *spec;
4009 struct nx_action_learn *nal;
4012 start_ofs = out->size;
4013 nal = put_NXAST_LEARN(out);
4014 nal->idle_timeout = htons(learn->idle_timeout);
4015 nal->hard_timeout = htons(learn->hard_timeout);
4016 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4017 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4018 nal->priority = htons(learn->priority);
4019 nal->cookie = learn->cookie;
4020 nal->flags = htons(learn->flags);
4021 nal->table_id = learn->table_id;
4023 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4024 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4026 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4027 put_u32(out, mf_nxm_header(spec->src.field->id));
4028 put_u16(out, spec->src.ofs);
4030 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4031 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4032 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4033 bits, n_dst_bytes, 0,
4037 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4038 spec->dst_type == NX_LEARN_DST_LOAD) {
4039 put_u32(out, mf_nxm_header(spec->dst.field->id));
4040 put_u16(out, spec->dst.ofs);
4044 pad_ofpat(out, start_ofs);
4047 static char * OVS_WARN_UNUSED_RESULT
4048 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4049 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4051 return learn_parse(arg, ofpacts);
4055 format_LEARN(const struct ofpact_learn *a, struct ds *s)
4060 /* Action structure for NXAST_CONJUNCTION. */
4061 struct nx_action_conjunction {
4062 ovs_be16 type; /* OFPAT_VENDOR. */
4063 ovs_be16 len; /* At least 16. */
4064 ovs_be32 vendor; /* NX_VENDOR_ID. */
4065 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4070 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4073 add_conjunction(struct ofpbuf *out,
4074 uint32_t id, uint8_t clause, uint8_t n_clauses)
4076 struct ofpact_conjunction *oc;
4078 oc = ofpact_put_CONJUNCTION(out);
4080 oc->clause = clause;
4081 oc->n_clauses = n_clauses;
4085 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4086 enum ofp_version ofp_version OVS_UNUSED,
4089 if (nac->n_clauses < 2 || nac->n_clauses > 64
4090 || nac->clause >= nac->n_clauses) {
4091 return OFPERR_NXBAC_BAD_CONJUNCTION;
4093 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4099 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4100 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4102 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4103 nac->clause = oc->clause;
4104 nac->n_clauses = oc->n_clauses;
4105 nac->id = htonl(oc->id);
4109 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4111 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4112 oc->id, oc->clause + 1, oc->n_clauses);
4115 static char * OVS_WARN_UNUSED_RESULT
4116 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4117 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4124 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4125 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4126 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4129 if (n_clauses < 2) {
4130 return xstrdup("conjunction must have at least 2 clauses");
4131 } else if (n_clauses > 64) {
4132 return xstrdup("conjunction must have at most 64 clauses");
4133 } else if (clause < 1) {
4134 return xstrdup("clause index must be positive");
4135 } else if (clause > n_clauses) {
4136 return xstrdup("clause index must be less than or equal to "
4137 "number of clauses");
4140 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4144 /* Action structure for NXAST_MULTIPATH.
4146 * This action performs the following steps in sequence:
4148 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4149 * Refer to the definition of "enum nx_mp_fields" for details.
4151 * The 'basis' value is used as a universal hash parameter, that is,
4152 * different values of 'basis' yield different hash functions. The
4153 * particular universal hash function used is implementation-defined.
4155 * The hashed fields' values are drawn from the current state of the
4156 * flow, including all modifications that have been made by actions up to
4159 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4160 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4163 * The output of the algorithm is 'link', an unsigned integer less than
4164 * or equal to 'max_link'.
4166 * Some algorithms use 'arg' as an additional argument.
4168 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4169 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4172 * The switch will reject actions that have an unknown 'fields', or an unknown
4173 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4174 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4175 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4177 struct nx_action_multipath {
4178 ovs_be16 type; /* OFPAT_VENDOR. */
4179 ovs_be16 len; /* Length is 32. */
4180 ovs_be32 vendor; /* NX_VENDOR_ID. */
4181 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4183 /* What fields to hash and how. */
4184 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4185 ovs_be16 basis; /* Universal hash parameter. */
4188 /* Multipath link choice algorithm to apply to hash value. */
4189 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4190 ovs_be16 max_link; /* Number of output links, minus 1. */
4191 ovs_be32 arg; /* Algorithm-specific argument. */
4194 /* Where to store the result. */
4195 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4196 ovs_be32 dst; /* Destination. */
4198 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4201 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4202 enum ofp_version ofp_version OVS_UNUSED,
4205 uint32_t n_links = ntohs(nam->max_link) + 1;
4206 size_t min_n_bits = log_2_ceil(n_links);
4207 struct ofpact_multipath *mp;
4209 mp = ofpact_put_MULTIPATH(out);
4210 mp->fields = ntohs(nam->fields);
4211 mp->basis = ntohs(nam->basis);
4212 mp->algorithm = ntohs(nam->algorithm);
4213 mp->max_link = ntohs(nam->max_link);
4214 mp->arg = ntohl(nam->arg);
4215 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4216 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4217 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4219 if (!flow_hash_fields_valid(mp->fields)) {
4220 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4221 return OFPERR_OFPBAC_BAD_ARGUMENT;
4222 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4223 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4224 && mp->algorithm != NX_MP_ALG_HRW
4225 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4226 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4227 return OFPERR_OFPBAC_BAD_ARGUMENT;
4228 } else if (mp->dst.n_bits < min_n_bits) {
4229 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4230 "%"PRIu32" links", min_n_bits, n_links);
4231 return OFPERR_OFPBAC_BAD_ARGUMENT;
4234 return multipath_check(mp, NULL);
4238 encode_MULTIPATH(const struct ofpact_multipath *mp,
4239 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4241 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4243 nam->fields = htons(mp->fields);
4244 nam->basis = htons(mp->basis);
4245 nam->algorithm = htons(mp->algorithm);
4246 nam->max_link = htons(mp->max_link);
4247 nam->arg = htonl(mp->arg);
4248 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4249 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4252 static char * OVS_WARN_UNUSED_RESULT
4253 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4254 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4256 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4260 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4262 multipath_format(a, s);
4265 /* Action structure for NXAST_NOTE.
4267 * This action has no effect. It is variable length. The switch does not
4268 * attempt to interpret the user-defined 'note' data in any way. A controller
4269 * can use this action to attach arbitrary metadata to a flow.
4271 * This action might go away in the future.
4273 struct nx_action_note {
4274 ovs_be16 type; /* OFPAT_VENDOR. */
4275 ovs_be16 len; /* A multiple of 8, but at least 16. */
4276 ovs_be32 vendor; /* NX_VENDOR_ID. */
4277 ovs_be16 subtype; /* NXAST_NOTE. */
4278 uint8_t note[6]; /* Start of user-defined data. */
4279 /* Possibly followed by additional user-defined data. */
4281 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4284 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4285 enum ofp_version ofp_version OVS_UNUSED,
4288 struct ofpact_note *note;
4289 unsigned int length;
4291 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4292 note = ofpact_put(out, OFPACT_NOTE,
4293 offsetof(struct ofpact_note, data) + length);
4294 note->length = length;
4295 memcpy(note->data, nan->note, length);
4301 encode_NOTE(const struct ofpact_note *note,
4302 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4304 size_t start_ofs = out->size;
4305 struct nx_action_note *nan;
4306 unsigned int remainder;
4309 put_NXAST_NOTE(out);
4310 out->size = out->size - sizeof nan->note;
4312 ofpbuf_put(out, note->data, note->length);
4314 len = out->size - start_ofs;
4315 remainder = len % OFP_ACTION_ALIGN;
4317 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4319 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4320 nan->len = htons(out->size - start_ofs);
4323 static char * OVS_WARN_UNUSED_RESULT
4324 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4325 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4327 struct ofpact_note *note;
4329 note = ofpact_put_NOTE(ofpacts);
4330 while (*arg != '\0') {
4341 byte = hexits_value(arg, 2, &ok);
4343 return xstrdup("bad hex digit in `note' argument");
4345 ofpbuf_put(ofpacts, &byte, 1);
4347 note = ofpacts->header;
4352 ofpact_update_len(ofpacts, ¬e->ofpact);
4357 format_NOTE(const struct ofpact_note *a, struct ds *s)
4361 ds_put_cstr(s, "note:");
4362 for (i = 0; i < a->length; i++) {
4364 ds_put_char(s, '.');
4366 ds_put_format(s, "%02"PRIx8, a->data[i]);
4373 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4375 ofpact_put_EXIT(out);
4380 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4381 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4383 put_NXAST_EXIT(out);
4386 static char * OVS_WARN_UNUSED_RESULT
4387 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4388 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4390 ofpact_put_EXIT(ofpacts);
4395 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4397 ds_put_cstr(s, "exit");
4400 /* Unroll xlate action. */
4403 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4404 enum ofp_version ofp_version OVS_UNUSED,
4405 struct ofpbuf *out OVS_UNUSED)
4410 static char * OVS_WARN_UNUSED_RESULT
4411 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4412 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4419 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4422 ds_put_cstr(s, "unroll_xlate");
4425 /* Action structure for NXAST_SAMPLE.
4427 * Samples matching packets with the given probability and sends them
4428 * each to the set of collectors identified with the given ID. The
4429 * probability is expressed as a number of packets to be sampled out
4430 * of USHRT_MAX packets, and must be >0.
4432 * When sending packet samples to IPFIX collectors, the IPFIX flow
4433 * record sent for each sampled packet is associated with the given
4434 * observation domain ID and observation point ID. Each IPFIX flow
4435 * record contain the sampled packet's headers when executing this
4436 * rule. If a sampled packet's headers are modified by previous
4437 * actions in the flow, those modified headers are sent. */
4438 struct nx_action_sample {
4439 ovs_be16 type; /* OFPAT_VENDOR. */
4440 ovs_be16 len; /* Length is 24. */
4441 ovs_be32 vendor; /* NX_VENDOR_ID. */
4442 ovs_be16 subtype; /* NXAST_SAMPLE. */
4443 ovs_be16 probability; /* Fraction of packets to sample. */
4444 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4445 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4446 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4448 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4451 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4452 enum ofp_version ofp_version OVS_UNUSED,
4455 struct ofpact_sample *sample;
4457 sample = ofpact_put_SAMPLE(out);
4458 sample->probability = ntohs(nas->probability);
4459 sample->collector_set_id = ntohl(nas->collector_set_id);
4460 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4461 sample->obs_point_id = ntohl(nas->obs_point_id);
4463 if (sample->probability == 0) {
4464 return OFPERR_OFPBAC_BAD_ARGUMENT;
4471 encode_SAMPLE(const struct ofpact_sample *sample,
4472 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4474 struct nx_action_sample *nas;
4476 nas = put_NXAST_SAMPLE(out);
4477 nas->probability = htons(sample->probability);
4478 nas->collector_set_id = htonl(sample->collector_set_id);
4479 nas->obs_domain_id = htonl(sample->obs_domain_id);
4480 nas->obs_point_id = htonl(sample->obs_point_id);
4483 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4484 * action to 'ofpacts'.
4486 * Returns NULL if successful, otherwise a malloc()'d string describing the
4487 * error. The caller is responsible for freeing the returned string. */
4488 static char * OVS_WARN_UNUSED_RESULT
4489 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4490 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4492 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4495 while (ofputil_parse_key_value(&arg, &key, &value)) {
4498 if (!strcmp(key, "probability")) {
4499 error = str_to_u16(value, "probability", &os->probability);
4500 if (!error && os->probability == 0) {
4501 error = xasprintf("invalid probability value \"%s\"", value);
4503 } else if (!strcmp(key, "collector_set_id")) {
4504 error = str_to_u32(value, &os->collector_set_id);
4505 } else if (!strcmp(key, "obs_domain_id")) {
4506 error = str_to_u32(value, &os->obs_domain_id);
4507 } else if (!strcmp(key, "obs_point_id")) {
4508 error = str_to_u32(value, &os->obs_point_id);
4510 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4517 if (os->probability == 0) {
4518 return xstrdup("non-zero \"probability\" must be specified on sample");
4524 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4526 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4527 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4528 a->probability, a->collector_set_id,
4529 a->obs_domain_id, a->obs_point_id);
4532 /* debug_recirc instruction. */
4534 static bool enable_debug;
4537 ofpact_dummy_enable(void)
4539 enable_debug = true;
4543 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4545 if (!enable_debug) {
4546 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4549 ofpact_put_DEBUG_RECIRC(out);
4554 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4555 enum ofp_version ofp_version OVS_UNUSED,
4558 put_NXAST_DEBUG_RECIRC(out);
4561 static char * OVS_WARN_UNUSED_RESULT
4562 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4563 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4565 ofpact_put_DEBUG_RECIRC(ofpacts);
4570 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4572 ds_put_cstr(s, "debug_recirc");
4575 /* Meter instruction. */
4578 encode_METER(const struct ofpact_meter *meter,
4579 enum ofp_version ofp_version, struct ofpbuf *out)
4581 if (ofp_version >= OFP13_VERSION) {
4582 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4586 static char * OVS_WARN_UNUSED_RESULT
4587 parse_METER(char *arg, struct ofpbuf *ofpacts,
4588 enum ofputil_protocol *usable_protocols)
4590 *usable_protocols &= OFPUTIL_P_OF13_UP;
4591 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
4595 format_METER(const struct ofpact_meter *a, struct ds *s)
4597 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
4600 /* Clear-Actions instruction. */
4603 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
4604 enum ofp_version ofp_version OVS_UNUSED,
4605 struct ofpbuf *out OVS_UNUSED)
4607 if (ofp_version > OFP10_VERSION) {
4608 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
4612 static char * OVS_WARN_UNUSED_RESULT
4613 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4614 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4616 ofpact_put_CLEAR_ACTIONS(ofpacts);
4621 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4623 ds_put_cstr(s, "clear_actions");
4626 /* Write-Actions instruction. */
4629 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
4630 enum ofp_version ofp_version, struct ofpbuf *out)
4632 if (ofp_version > OFP10_VERSION) {
4633 const size_t ofs = out->size;
4635 instruction_put_OFPIT11_WRITE_ACTIONS(out);
4636 ofpacts_put_openflow_actions(actions->actions,
4637 ofpact_nest_get_action_len(actions),
4639 ofpacts_update_instruction_actions(out, ofs);
4643 static char * OVS_WARN_UNUSED_RESULT
4644 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
4645 enum ofputil_protocol *usable_protocols)
4647 size_t ofs = ofpacts_pull(ofpacts);
4648 struct ofpact_nest *on;
4651 /* Add a Write-Actions instruction and then pull it off. */
4652 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
4653 ofpbuf_pull(ofpacts, sizeof *on);
4655 /* Parse nested actions.
4657 * We pulled off "write-actions" and the previous actions because the
4658 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4659 * that it doesn't actually include the nested actions. That means that
4660 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4661 * follows a Write-Actions, which is an invalid order. */
4662 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
4663 OFPACT_WRITE_ACTIONS);
4665 /* Put the Write-Actions back on and update its length. */
4666 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
4667 on->ofpact.len = ofpacts->size;
4669 /* Put any previous actions or instructions back on. */
4670 ofpbuf_push_uninit(ofpacts, ofs);
4676 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
4678 ds_put_cstr(s, "write_actions(");
4679 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
4680 ds_put_char(s, ')');
4683 /* Action structure for NXAST_WRITE_METADATA.
4685 * Modifies the 'mask' bits of the metadata value. */
4686 struct nx_action_write_metadata {
4687 ovs_be16 type; /* OFPAT_VENDOR. */
4688 ovs_be16 len; /* Length is 32. */
4689 ovs_be32 vendor; /* NX_VENDOR_ID. */
4690 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
4691 uint8_t zeros[6]; /* Must be zero. */
4692 ovs_be64 metadata; /* Metadata register. */
4693 ovs_be64 mask; /* Metadata mask. */
4695 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
4698 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
4699 enum ofp_version ofp_version OVS_UNUSED,
4702 struct ofpact_metadata *om;
4704 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
4705 return OFPERR_NXBRC_MUST_BE_ZERO;
4708 om = ofpact_put_WRITE_METADATA(out);
4709 om->metadata = nawm->metadata;
4710 om->mask = nawm->mask;
4716 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
4717 enum ofp_version ofp_version, struct ofpbuf *out)
4719 if (ofp_version == OFP10_VERSION) {
4720 struct nx_action_write_metadata *nawm;
4722 nawm = put_NXAST_WRITE_METADATA(out);
4723 nawm->metadata = metadata->metadata;
4724 nawm->mask = metadata->mask;
4726 struct ofp11_instruction_write_metadata *oiwm;
4728 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
4729 oiwm->metadata = metadata->metadata;
4730 oiwm->metadata_mask = metadata->mask;
4734 static char * OVS_WARN_UNUSED_RESULT
4735 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
4736 enum ofputil_protocol *usable_protocols)
4738 struct ofpact_metadata *om;
4739 char *mask = strchr(arg, '/');
4741 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
4743 om = ofpact_put_WRITE_METADATA(ofpacts);
4748 error = str_to_be64(mask + 1, &om->mask);
4753 om->mask = OVS_BE64_MAX;
4756 return str_to_be64(arg, &om->metadata);
4760 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
4762 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
4763 if (a->mask != OVS_BE64_MAX) {
4764 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
4768 /* Goto-Table instruction. */
4771 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
4772 enum ofp_version ofp_version, struct ofpbuf *out)
4774 if (ofp_version == OFP10_VERSION) {
4775 struct nx_action_resubmit *nar;
4777 nar = put_NXAST_RESUBMIT_TABLE(out);
4778 nar->table = goto_table->table_id;
4779 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
4781 struct ofp11_instruction_goto_table *oigt;
4783 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
4784 oigt->table_id = goto_table->table_id;
4785 memset(oigt->pad, 0, sizeof oigt->pad);
4789 static char * OVS_WARN_UNUSED_RESULT
4790 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
4791 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4793 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
4794 char *table_s = strsep(&arg, ",");
4795 if (!table_s || !table_s[0]) {
4796 return xstrdup("instruction goto-table needs table id");
4798 return str_to_u8(table_s, "table", &ogt->table_id);
4802 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
4804 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
4808 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
4809 const struct ofp_action_header *bad_action, enum ofperr error)
4811 if (!VLOG_DROP_WARN(&rl)) {
4815 ds_put_hex_dump(&s, actions, actions_len, 0, false);
4816 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
4817 (char *)bad_action - (char *)actions,
4818 ofperr_get_name(error), ds_cstr(&s));
4824 ofpacts_decode(const void *actions, size_t actions_len,
4825 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
4827 struct ofpbuf openflow;
4829 ofpbuf_use_const(&openflow, actions, actions_len);
4830 while (openflow.size) {
4831 const struct ofp_action_header *action = openflow.data;
4832 enum ofp_raw_action_type raw;
4836 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
4838 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
4842 log_bad_action(actions, actions_len, action, error);
4847 ofpact_pad(ofpacts);
4852 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
4853 unsigned int actions_len,
4854 enum ofp_version version,
4855 uint32_t allowed_ovsinsts,
4856 struct ofpbuf *ofpacts,
4857 enum ofpact_type outer_action)
4859 const struct ofp_action_header *actions;
4862 if (!outer_action) {
4863 ofpbuf_clear(ofpacts);
4866 if (actions_len % OFP_ACTION_ALIGN != 0) {
4867 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
4868 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
4869 return OFPERR_OFPBRC_BAD_LEN;
4872 actions = ofpbuf_try_pull(openflow, actions_len);
4873 if (actions == NULL) {
4874 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
4875 "remaining message length (%"PRIu32")",
4876 actions_len, openflow->size);
4877 return OFPERR_OFPBRC_BAD_LEN;
4880 error = ofpacts_decode(actions, actions_len, version, ofpacts);
4882 ofpbuf_clear(ofpacts);
4886 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
4889 ofpbuf_clear(ofpacts);
4894 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
4895 * front of 'openflow' into ofpacts. On success, replaces any existing content
4896 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
4897 * Returns 0 if successful, otherwise an OpenFlow error.
4899 * Actions are processed according to their OpenFlow version which
4900 * is provided in the 'version' parameter.
4902 * In most places in OpenFlow, actions appear encapsulated in instructions, so
4903 * you should call ofpacts_pull_openflow_instructions() instead of this
4906 * The parsed actions are valid generically, but they may not be valid in a
4907 * specific context. For example, port numbers up to OFPP_MAX are valid
4908 * generically, but specific datapaths may only support port numbers in a
4909 * smaller range. Use ofpacts_check() to additional check whether actions are
4910 * valid in a specific context. */
4912 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
4913 unsigned int actions_len,
4914 enum ofp_version version,
4915 struct ofpbuf *ofpacts)
4917 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
4918 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4922 /* OpenFlow 1.1 actions. */
4925 /* True if an action sets the value of a field
4926 * in a way that is compatibile with the action set.
4927 * The field can be set via either a set or a move action.
4928 * False otherwise. */
4930 ofpact_is_set_or_move_action(const struct ofpact *a)
4933 case OFPACT_SET_FIELD:
4934 case OFPACT_REG_MOVE:
4935 case OFPACT_SET_ETH_DST:
4936 case OFPACT_SET_ETH_SRC:
4937 case OFPACT_SET_IP_DSCP:
4938 case OFPACT_SET_IP_ECN:
4939 case OFPACT_SET_IP_TTL:
4940 case OFPACT_SET_IPV4_DST:
4941 case OFPACT_SET_IPV4_SRC:
4942 case OFPACT_SET_L4_DST_PORT:
4943 case OFPACT_SET_L4_SRC_PORT:
4944 case OFPACT_SET_MPLS_LABEL:
4945 case OFPACT_SET_MPLS_TC:
4946 case OFPACT_SET_MPLS_TTL:
4947 case OFPACT_SET_QUEUE:
4948 case OFPACT_SET_TUNNEL:
4949 case OFPACT_SET_VLAN_PCP:
4950 case OFPACT_SET_VLAN_VID:
4953 case OFPACT_CLEAR_ACTIONS:
4954 case OFPACT_CONTROLLER:
4955 case OFPACT_DEC_MPLS_TTL:
4956 case OFPACT_DEC_TTL:
4957 case OFPACT_ENQUEUE:
4959 case OFPACT_UNROLL_XLATE:
4960 case OFPACT_FIN_TIMEOUT:
4961 case OFPACT_GOTO_TABLE:
4964 case OFPACT_CONJUNCTION:
4966 case OFPACT_MULTIPATH:
4969 case OFPACT_OUTPUT_REG:
4970 case OFPACT_POP_MPLS:
4971 case OFPACT_POP_QUEUE:
4972 case OFPACT_PUSH_MPLS:
4973 case OFPACT_PUSH_VLAN:
4974 case OFPACT_RESUBMIT:
4976 case OFPACT_STACK_POP:
4977 case OFPACT_STACK_PUSH:
4978 case OFPACT_STRIP_VLAN:
4979 case OFPACT_WRITE_ACTIONS:
4980 case OFPACT_WRITE_METADATA:
4981 case OFPACT_DEBUG_RECIRC:
4988 /* True if an action is allowed in the action set.
4989 * False otherwise. */
4991 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
4994 case OFPACT_DEC_MPLS_TTL:
4995 case OFPACT_DEC_TTL:
4998 case OFPACT_POP_MPLS:
4999 case OFPACT_PUSH_MPLS:
5000 case OFPACT_PUSH_VLAN:
5001 case OFPACT_REG_MOVE:
5002 case OFPACT_SET_FIELD:
5003 case OFPACT_SET_ETH_DST:
5004 case OFPACT_SET_ETH_SRC:
5005 case OFPACT_SET_IP_DSCP:
5006 case OFPACT_SET_IP_ECN:
5007 case OFPACT_SET_IP_TTL:
5008 case OFPACT_SET_IPV4_DST:
5009 case OFPACT_SET_IPV4_SRC:
5010 case OFPACT_SET_L4_DST_PORT:
5011 case OFPACT_SET_L4_SRC_PORT:
5012 case OFPACT_SET_MPLS_LABEL:
5013 case OFPACT_SET_MPLS_TC:
5014 case OFPACT_SET_MPLS_TTL:
5015 case OFPACT_SET_QUEUE:
5016 case OFPACT_SET_TUNNEL:
5017 case OFPACT_SET_VLAN_PCP:
5018 case OFPACT_SET_VLAN_VID:
5019 case OFPACT_STRIP_VLAN:
5022 /* In general these actions are excluded because they are not part of
5023 * the OpenFlow specification nor map to actions that are defined in
5024 * the specification. Thus the order in which they should be applied
5025 * in the action set is undefined. */
5027 case OFPACT_CONTROLLER:
5028 case OFPACT_ENQUEUE:
5030 case OFPACT_UNROLL_XLATE:
5031 case OFPACT_FIN_TIMEOUT:
5033 case OFPACT_CONJUNCTION:
5034 case OFPACT_MULTIPATH:
5036 case OFPACT_OUTPUT_REG:
5037 case OFPACT_POP_QUEUE:
5038 case OFPACT_RESUBMIT:
5040 case OFPACT_STACK_POP:
5041 case OFPACT_STACK_PUSH:
5042 case OFPACT_DEBUG_RECIRC:
5044 /* The action set may only include actions and thus
5045 * may not include any instructions */
5046 case OFPACT_CLEAR_ACTIONS:
5047 case OFPACT_GOTO_TABLE:
5049 case OFPACT_WRITE_ACTIONS:
5050 case OFPACT_WRITE_METADATA:
5057 /* Append ofpact 'a' onto the tail of 'out' */
5059 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
5061 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
5064 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
5066 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
5067 enum ofpact_type filter)
5069 const struct ofpact *target;
5070 const struct ofpact *a;
5073 OFPACT_FOR_EACH (a, in->data, in->size) {
5074 if (a->type == filter) {
5079 ofpact_copy(out, target);
5081 return target != NULL;
5084 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5085 * The order of appended ofpacts is preserved between 'in' and 'out' */
5087 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5088 bool (*filter)(const struct ofpact *))
5090 const struct ofpact *a;
5092 OFPACT_FOR_EACH (a, in->data, in->size) {
5094 ofpact_copy(out, a);
5099 /* Reads 'action_set', which contains ofpacts accumulated by
5100 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5101 * executed directly into 'action_list'. (These names correspond to the
5102 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5104 * In general this involves appending the last instance of each action that is
5105 * admissible in the action set in the order described in the OpenFlow
5109 * + output action is only appended if no group action was present in 'in'.
5110 * + As a simplification all set actions are copied in the order the are
5111 * provided in 'in' as many set actions applied to a field has the same
5112 * affect as only applying the last action that sets a field and
5113 * duplicates are removed by do_xlate_actions().
5114 * This has an unwanted side-effect of compsoting multiple
5115 * LOAD_REG actions that touch different regions of the same field. */
5117 ofpacts_execute_action_set(struct ofpbuf *action_list,
5118 const struct ofpbuf *action_set)
5120 /* The OpenFlow spec "Action Set" section specifies this order. */
5121 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5122 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5123 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5124 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5125 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5126 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5127 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5128 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5130 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5131 * we should execute only OFPACT_GROUP.
5133 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5134 * all the actions because there's no point in modifying a packet that will
5135 * not be sent anywhere. */
5136 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5137 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5138 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5139 ofpbuf_clear(action_list);
5145 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5146 size_t n_in, enum ofp_version version,
5151 size_t start = out->size;
5153 error = ofpacts_decode(in, n_in, version, out);
5159 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5160 if (!ofpact_is_allowed_in_actions_set(a)) {
5161 VLOG_WARN_RL(&rl, "disallowed action in action set");
5162 return OFPERR_OFPBAC_BAD_TYPE;
5169 /* OpenFlow 1.1 instructions. */
5171 struct instruction_type_info {
5172 enum ovs_instruction_type type;
5176 static const struct instruction_type_info inst_info[] = {
5177 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5183 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5185 return inst_info[type].name;
5189 ovs_instruction_type_from_name(const char *name)
5191 const struct instruction_type_info *p;
5192 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5193 if (!strcasecmp(name, p->name)) {
5200 enum ovs_instruction_type
5201 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5205 return OVSINST_OFPIT13_METER;
5206 case OFPACT_CLEAR_ACTIONS:
5207 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5208 case OFPACT_WRITE_ACTIONS:
5209 return OVSINST_OFPIT11_WRITE_ACTIONS;
5210 case OFPACT_WRITE_METADATA:
5211 return OVSINST_OFPIT11_WRITE_METADATA;
5212 case OFPACT_GOTO_TABLE:
5213 return OVSINST_OFPIT11_GOTO_TABLE;
5216 case OFPACT_CONTROLLER:
5217 case OFPACT_ENQUEUE:
5218 case OFPACT_OUTPUT_REG:
5220 case OFPACT_SET_VLAN_VID:
5221 case OFPACT_SET_VLAN_PCP:
5222 case OFPACT_STRIP_VLAN:
5223 case OFPACT_PUSH_VLAN:
5224 case OFPACT_SET_ETH_SRC:
5225 case OFPACT_SET_ETH_DST:
5226 case OFPACT_SET_IPV4_SRC:
5227 case OFPACT_SET_IPV4_DST:
5228 case OFPACT_SET_IP_DSCP:
5229 case OFPACT_SET_IP_ECN:
5230 case OFPACT_SET_IP_TTL:
5231 case OFPACT_SET_L4_SRC_PORT:
5232 case OFPACT_SET_L4_DST_PORT:
5233 case OFPACT_REG_MOVE:
5234 case OFPACT_SET_FIELD:
5235 case OFPACT_STACK_PUSH:
5236 case OFPACT_STACK_POP:
5237 case OFPACT_DEC_TTL:
5238 case OFPACT_SET_MPLS_LABEL:
5239 case OFPACT_SET_MPLS_TC:
5240 case OFPACT_SET_MPLS_TTL:
5241 case OFPACT_DEC_MPLS_TTL:
5242 case OFPACT_PUSH_MPLS:
5243 case OFPACT_POP_MPLS:
5244 case OFPACT_SET_TUNNEL:
5245 case OFPACT_SET_QUEUE:
5246 case OFPACT_POP_QUEUE:
5247 case OFPACT_FIN_TIMEOUT:
5248 case OFPACT_RESUBMIT:
5250 case OFPACT_CONJUNCTION:
5251 case OFPACT_MULTIPATH:
5254 case OFPACT_UNROLL_XLATE:
5256 case OFPACT_DEBUG_RECIRC:
5258 return OVSINST_OFPIT11_APPLY_ACTIONS;
5263 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
5264 const uint16_t inst_type)
5266 switch (inst_type) {
5268 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5270 *instruction_type = OVSINST_##ENUM; \
5276 return OFPERR_OFPBIC_UNKNOWN_INST;
5280 /* Two-way translation between OVS's internal "OVSINST_*" representation of
5281 * instructions and the "OFPIT_*" representation used in OpenFlow. */
5282 struct ovsinst_map {
5283 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
5284 int ofpit; /* OFPIT_* number from OpenFlow spec. */
5287 static const struct ovsinst_map *
5288 get_ovsinst_map(enum ofp_version version)
5290 /* OpenFlow 1.1 and 1.2 instructions. */
5291 static const struct ovsinst_map of11[] = {
5292 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5293 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5294 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5295 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5296 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5300 /* OpenFlow 1.3+ instructions. */
5301 static const struct ovsinst_map of13[] = {
5302 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5303 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5304 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5305 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5306 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5307 { OVSINST_OFPIT13_METER, 6 },
5311 return version < OFP13_VERSION ? of11 : of13;
5314 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
5315 * values, into a bitmap of instructions suitable for OpenFlow 'version'
5316 * (OFP11_VERSION or later), and returns the result. */
5318 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
5320 uint32_t ofpit_bitmap = 0;
5321 const struct ovsinst_map *x;
5323 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5324 if (ovsinst_bitmap & (1u << x->ovsinst)) {
5325 ofpit_bitmap |= 1u << x->ofpit;
5328 return htonl(ofpit_bitmap);
5331 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5332 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5333 * correspond to OVSINST_* values, and returns the result. */
5335 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
5337 uint32_t ovsinst_bitmap = 0;
5338 const struct ovsinst_map *x;
5340 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5341 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
5342 ovsinst_bitmap |= 1u << x->ovsinst;
5345 return ovsinst_bitmap;
5348 static inline struct ofp11_instruction *
5349 instruction_next(const struct ofp11_instruction *inst)
5351 return ((struct ofp11_instruction *) (void *)
5352 ((uint8_t *) inst + ntohs(inst->len)));
5356 instruction_is_valid(const struct ofp11_instruction *inst,
5357 size_t n_instructions)
5359 uint16_t len = ntohs(inst->len);
5360 return (!(len % OFP11_INSTRUCTION_ALIGN)
5361 && len >= sizeof *inst
5362 && len / sizeof *inst <= n_instructions);
5365 /* This macro is careful to check for instructions with bad lengths. */
5366 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
5367 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
5368 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
5369 ((LEFT) -= (ntohs((ITER)->len) \
5370 / sizeof(struct ofp11_instruction)), \
5371 (ITER) = instruction_next(ITER)))
5374 decode_openflow11_instruction(const struct ofp11_instruction *inst,
5375 enum ovs_instruction_type *type)
5377 uint16_t len = ntohs(inst->len);
5379 switch (inst->type) {
5380 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5381 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5383 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5384 case CONSTANT_HTONS(ENUM): \
5386 ? len >= sizeof(struct STRUCT) \
5387 : len == sizeof(struct STRUCT)) { \
5388 *type = OVSINST_##ENUM; \
5391 return OFPERR_OFPBIC_BAD_LEN; \
5397 return OFPERR_OFPBIC_UNKNOWN_INST;
5402 decode_openflow11_instructions(const struct ofp11_instruction insts[],
5404 const struct ofp11_instruction *out[])
5406 const struct ofp11_instruction *inst;
5409 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5410 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5411 enum ovs_instruction_type type;
5414 error = decode_openflow11_instruction(inst, &type);
5420 return OFPERR_OFPBIC_DUP_INST;
5426 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5427 (n_insts - left) * sizeof *inst);
5428 return OFPERR_OFPBIC_BAD_LEN;
5434 get_actions_from_instruction(const struct ofp11_instruction *inst,
5435 const struct ofp_action_header **actions,
5436 size_t *actions_len)
5438 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5439 *actions_len = ntohs(inst->len) - sizeof *inst;
5443 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5444 unsigned int instructions_len,
5445 enum ofp_version version,
5446 struct ofpbuf *ofpacts)
5448 const struct ofp11_instruction *instructions;
5449 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5452 if (version == OFP10_VERSION) {
5453 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5455 (1u << N_OVS_INSTRUCTIONS) - 1,
5459 ofpbuf_clear(ofpacts);
5461 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5462 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5464 instructions_len, OFP11_INSTRUCTION_ALIGN);
5465 error = OFPERR_OFPBIC_BAD_LEN;
5469 instructions = ofpbuf_try_pull(openflow, instructions_len);
5470 if (instructions == NULL) {
5471 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5472 "remaining message length (%"PRIu32")",
5473 instructions_len, openflow->size);
5474 error = OFPERR_OFPBIC_BAD_LEN;
5478 error = decode_openflow11_instructions(
5479 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5485 if (insts[OVSINST_OFPIT13_METER]) {
5486 const struct ofp13_instruction_meter *oim;
5487 struct ofpact_meter *om;
5489 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5490 insts[OVSINST_OFPIT13_METER]);
5492 om = ofpact_put_METER(ofpacts);
5493 om->meter_id = ntohl(oim->meter_id);
5495 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5496 const struct ofp_action_header *actions;
5499 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5500 &actions, &actions_len);
5501 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5506 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5507 instruction_get_OFPIT11_CLEAR_ACTIONS(
5508 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5509 ofpact_put_CLEAR_ACTIONS(ofpacts);
5511 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5512 struct ofpact_nest *on;
5513 const struct ofp_action_header *actions;
5517 ofpact_pad(ofpacts);
5518 start = ofpacts->size;
5519 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5520 offsetof(struct ofpact_nest, actions));
5521 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5522 &actions, &actions_len);
5523 error = ofpacts_decode_for_action_set(actions, actions_len,
5528 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5529 on->ofpact.len = ofpacts->size - start;
5531 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5532 const struct ofp11_instruction_write_metadata *oiwm;
5533 struct ofpact_metadata *om;
5535 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5536 insts[OVSINST_OFPIT11_WRITE_METADATA]);
5538 om = ofpact_put_WRITE_METADATA(ofpacts);
5539 om->metadata = oiwm->metadata;
5540 om->mask = oiwm->metadata_mask;
5542 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5543 const struct ofp11_instruction_goto_table *oigt;
5544 struct ofpact_goto_table *ogt;
5546 oigt = instruction_get_OFPIT11_GOTO_TABLE(
5547 insts[OVSINST_OFPIT11_GOTO_TABLE]);
5548 ogt = ofpact_put_GOTO_TABLE(ofpacts);
5549 ogt->table_id = oigt->table_id;
5552 error = ofpacts_verify(ofpacts->data, ofpacts->size,
5553 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
5556 ofpbuf_clear(ofpacts);
5561 /* Update the length of the instruction that begins at offset 'ofs' within
5562 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5563 * If the instruction contains no nested actions, deletes it entirely. */
5565 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5567 struct ofp11_instruction_actions *oia;
5569 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5570 if (openflow->size > ofs + sizeof *oia) {
5571 oia->len = htons(openflow->size - ofs);
5573 openflow->size = ofs;
5577 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5578 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5579 * valid, otherwise an OpenFlow error code. */
5581 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5589 case OFPP_CONTROLLER:
5594 return OFPERR_OFPBAC_BAD_OUT_PORT;
5597 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
5600 return OFPERR_OFPBAC_BAD_OUT_PORT;
5604 /* Removes the protocols that require consistency between match and actions
5605 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5607 * (An example of an inconsistency between match and actions is a flow that
5608 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5611 inconsistent_match(enum ofputil_protocol *usable_protocols)
5613 *usable_protocols &= OFPUTIL_P_OF10_ANY;
5616 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5617 * caller must restore them.
5619 * Modifies some actions, filling in fields that could not be properly set
5620 * without context. */
5622 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
5623 struct flow *flow, ofp_port_t max_ports,
5624 uint8_t table_id, uint8_t n_tables)
5626 const struct ofpact_enqueue *enqueue;
5627 const struct mf_field *mf;
5631 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
5634 case OFPACT_CONTROLLER:
5637 case OFPACT_ENQUEUE:
5638 enqueue = ofpact_get_ENQUEUE(a);
5639 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
5640 && enqueue->port != OFPP_IN_PORT
5641 && enqueue->port != OFPP_LOCAL) {
5642 return OFPERR_OFPBAC_BAD_OUT_PORT;
5646 case OFPACT_OUTPUT_REG:
5647 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
5650 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
5652 case OFPACT_SET_VLAN_VID:
5653 /* Remember if we saw a vlan tag in the flow to aid translating to
5654 * OpenFlow 1.1+ if need be. */
5655 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
5656 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5657 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5658 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5659 inconsistent_match(usable_protocols);
5661 /* Temporary mark that we have a vlan tag. */
5662 flow->vlan_tci |= htons(VLAN_CFI);
5665 case OFPACT_SET_VLAN_PCP:
5666 /* Remember if we saw a vlan tag in the flow to aid translating to
5667 * OpenFlow 1.1+ if need be. */
5668 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
5669 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5670 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5671 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5672 inconsistent_match(usable_protocols);
5674 /* Temporary mark that we have a vlan tag. */
5675 flow->vlan_tci |= htons(VLAN_CFI);
5678 case OFPACT_STRIP_VLAN:
5679 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5680 inconsistent_match(usable_protocols);
5682 /* Temporary mark that we have no vlan tag. */
5683 flow->vlan_tci = htons(0);
5686 case OFPACT_PUSH_VLAN:
5687 if (flow->vlan_tci & htons(VLAN_CFI)) {
5688 /* Multiple VLAN headers not supported. */
5689 return OFPERR_OFPBAC_BAD_TAG;
5691 /* Temporary mark that we have a vlan tag. */
5692 flow->vlan_tci |= htons(VLAN_CFI);
5695 case OFPACT_SET_ETH_SRC:
5696 case OFPACT_SET_ETH_DST:
5699 case OFPACT_SET_IPV4_SRC:
5700 case OFPACT_SET_IPV4_DST:
5701 if (flow->dl_type != htons(ETH_TYPE_IP)) {
5702 inconsistent_match(usable_protocols);
5706 case OFPACT_SET_IP_DSCP:
5707 case OFPACT_SET_IP_ECN:
5708 case OFPACT_SET_IP_TTL:
5709 case OFPACT_DEC_TTL:
5710 if (!is_ip_any(flow)) {
5711 inconsistent_match(usable_protocols);
5715 case OFPACT_SET_L4_SRC_PORT:
5716 case OFPACT_SET_L4_DST_PORT:
5717 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
5718 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5719 && flow->nw_proto != IPPROTO_SCTP)) {
5720 inconsistent_match(usable_protocols);
5722 /* Note on which transport protocol the port numbers are set.
5723 * This allows this set action to be converted to an OF1.2 set field
5725 if (a->type == OFPACT_SET_L4_SRC_PORT) {
5726 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
5728 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
5732 case OFPACT_REG_MOVE:
5733 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
5735 case OFPACT_SET_FIELD:
5736 mf = ofpact_get_SET_FIELD(a)->field;
5737 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
5738 if (!mf_are_prereqs_ok(mf, flow) ||
5739 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
5740 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
5742 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5744 /* Remember if we saw a vlan tag in the flow to aid translating to
5745 * OpenFlow 1.1 if need be. */
5746 ofpact_get_SET_FIELD(a)->flow_has_vlan =
5747 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5748 if (mf->id == MFF_VLAN_TCI) {
5749 /* The set field may add or remove the vlan tag,
5750 * Mark the status temporarily. */
5751 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
5755 case OFPACT_STACK_PUSH:
5756 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
5758 case OFPACT_STACK_POP:
5759 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
5761 case OFPACT_SET_MPLS_LABEL:
5762 case OFPACT_SET_MPLS_TC:
5763 case OFPACT_SET_MPLS_TTL:
5764 case OFPACT_DEC_MPLS_TTL:
5765 if (!eth_type_mpls(flow->dl_type)) {
5766 inconsistent_match(usable_protocols);
5770 case OFPACT_SET_TUNNEL:
5771 case OFPACT_SET_QUEUE:
5772 case OFPACT_POP_QUEUE:
5773 case OFPACT_RESUBMIT:
5776 case OFPACT_FIN_TIMEOUT:
5777 if (flow->nw_proto != IPPROTO_TCP) {
5778 inconsistent_match(usable_protocols);
5783 return learn_check(ofpact_get_LEARN(a), flow);
5785 case OFPACT_CONJUNCTION:
5788 case OFPACT_MULTIPATH:
5789 return multipath_check(ofpact_get_MULTIPATH(a), flow);
5795 case OFPACT_PUSH_MPLS:
5796 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
5797 /* The packet is now MPLS and the MPLS payload is opaque.
5798 * Thus nothing can be assumed about the network protocol.
5799 * Temporarily mark that we have no nw_proto. */
5803 case OFPACT_POP_MPLS:
5804 if (!eth_type_mpls(flow->dl_type)) {
5805 inconsistent_match(usable_protocols);
5807 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
5813 case OFPACT_CLEAR_ACTIONS:
5816 case OFPACT_WRITE_ACTIONS: {
5817 /* Use a temporary copy of 'usable_protocols' because we can't check
5818 * consistency of an action set. */
5819 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
5820 enum ofputil_protocol p = *usable_protocols;
5821 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
5822 flow, max_ports, table_id, n_tables, &p);
5825 case OFPACT_WRITE_METADATA:
5828 case OFPACT_METER: {
5829 uint32_t mid = ofpact_get_METER(a)->meter_id;
5830 if (mid == 0 || mid > OFPM13_MAX) {
5831 return OFPERR_OFPMMFC_INVALID_METER;
5836 case OFPACT_GOTO_TABLE: {
5837 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
5838 if ((table_id != 255 && goto_table <= table_id)
5839 || (n_tables != 255 && goto_table >= n_tables)) {
5840 return OFPERR_OFPBIC_BAD_TABLE_ID;
5848 case OFPACT_UNROLL_XLATE:
5849 /* UNROLL is an internal action that should never be seen via
5851 return OFPERR_OFPBAC_BAD_TYPE;
5853 case OFPACT_DEBUG_RECIRC:
5861 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5862 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
5863 * switch with no more than 'max_ports' ports.
5865 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
5866 * '*usable_protocols' the protocols that forbid the inconsistency. (An
5867 * example of an inconsistency between match and actions is a flow that does
5868 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
5870 * May annotate ofpacts with information gathered from the 'flow'.
5872 * May temporarily modify 'flow', but restores the changes before returning. */
5874 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
5875 struct flow *flow, ofp_port_t max_ports,
5876 uint8_t table_id, uint8_t n_tables,
5877 enum ofputil_protocol *usable_protocols)
5880 ovs_be16 dl_type = flow->dl_type;
5881 ovs_be16 vlan_tci = flow->vlan_tci;
5882 uint8_t nw_proto = flow->nw_proto;
5883 enum ofperr error = 0;
5885 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5886 error = ofpact_check__(usable_protocols, a, flow,
5887 max_ports, table_id, n_tables);
5892 /* Restore fields that may have been modified. */
5893 flow->dl_type = dl_type;
5894 flow->vlan_tci = vlan_tci;
5895 flow->nw_proto = nw_proto;
5899 /* Like ofpacts_check(), but reports inconsistencies as
5900 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
5902 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
5903 struct flow *flow, ofp_port_t max_ports,
5904 uint8_t table_id, uint8_t n_tables,
5905 enum ofputil_protocol usable_protocols)
5907 enum ofputil_protocol p = usable_protocols;
5910 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
5911 table_id, n_tables, &p);
5912 return (error ? error
5913 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
5918 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
5920 if (outer_action != OFPACT_WRITE_ACTIONS) {
5921 VLOG_WARN("\"%s\" action doesn't support nested action \"%s\"",
5922 ofpact_name(outer_action), ofpact_name(a->type));
5923 return OFPERR_OFPBAC_BAD_ARGUMENT;
5929 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
5930 * appropriate order as defined by the OpenFlow spec and as required by Open
5933 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
5934 * instructions that are allowed within 'ofpacts[]'.
5936 * If 'outer_action' is not zero, it specifies that the actions are nested
5937 * within another action of type 'outer_action'. */
5939 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
5940 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
5942 const struct ofpact *a;
5943 enum ovs_instruction_type inst;
5945 inst = OVSINST_OFPIT13_METER;
5946 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5947 enum ovs_instruction_type next;
5949 if (a->type == OFPACT_CONJUNCTION) {
5950 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5951 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
5952 VLOG_WARN("\"conjunction\" actions may be used along with "
5953 "\"note\" but not any other kind of action "
5954 "(such as the \"%s\" action used here)",
5955 ofpact_name(a->type));
5956 return OFPERR_NXBAC_BAD_CONJUNCTION;
5963 enum ofperr error = ofpacts_verify_nested(a, outer_action);
5970 next = ovs_instruction_type_from_ofpact_type(a->type);
5972 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
5975 const char *name = ovs_instruction_name_from_type(inst);
5976 const char *next_name = ovs_instruction_name_from_type(next);
5979 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
5980 "1.1+ compatibility", name);
5982 VLOG_WARN("invalid instruction ordering: %s must appear "
5983 "before %s, for OpenFlow 1.1+ compatibility",
5986 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
5988 if (!((1u << next) & allowed_ovsinsts)) {
5989 const char *name = ovs_instruction_name_from_type(next);
5991 VLOG_WARN("%s instruction not allowed here", name);
5992 return OFPERR_OFPBIC_UNSUP_INST;
6001 /* Converting ofpacts to OpenFlow. */
6004 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
6008 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6009 case OFPACT_##ENUM: \
6010 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
6019 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
6020 * actions in 'openflow', appending the actions to any existing data in
6023 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
6024 struct ofpbuf *openflow,
6025 enum ofp_version ofp_version)
6027 const struct ofpact *a;
6028 size_t start_size = openflow->size;
6030 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6031 encode_ofpact(a, ofp_version, openflow);
6033 return openflow->size - start_size;
6036 static enum ovs_instruction_type
6037 ofpact_is_apply_actions(const struct ofpact *a)
6039 return (ovs_instruction_type_from_ofpact_type(a->type)
6040 == OVSINST_OFPIT11_APPLY_ACTIONS);
6044 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
6046 struct ofpbuf *openflow,
6047 enum ofp_version ofp_version)
6049 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
6050 const struct ofpact *a;
6052 if (ofp_version == OFP10_VERSION) {
6053 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
6060 if (ofpact_is_apply_actions(a)) {
6061 size_t ofs = openflow->size;
6063 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
6065 encode_ofpact(a, ofp_version, openflow);
6067 } while (a < end && ofpact_is_apply_actions(a));
6068 ofpacts_update_instruction_actions(openflow, ofs);
6070 encode_ofpact(a, ofp_version, openflow);
6076 /* Sets of supported actions. */
6078 /* Two-way translation between OVS's internal "OFPACT_*" representation of
6079 * actions and the "OFPAT_*" representation used in some OpenFlow version.
6080 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
6081 * instance is specific to one OpenFlow version.) */
6083 enum ofpact_type ofpact; /* Internal name for action type. */
6084 int ofpat; /* OFPAT_* number from OpenFlow spec. */
6087 static const struct ofpact_map *
6088 get_ofpact_map(enum ofp_version version)
6090 /* OpenFlow 1.0 actions. */
6091 static const struct ofpact_map of10[] = {
6092 { OFPACT_OUTPUT, 0 },
6093 { OFPACT_SET_VLAN_VID, 1 },
6094 { OFPACT_SET_VLAN_PCP, 2 },
6095 { OFPACT_STRIP_VLAN, 3 },
6096 { OFPACT_SET_ETH_SRC, 4 },
6097 { OFPACT_SET_ETH_DST, 5 },
6098 { OFPACT_SET_IPV4_SRC, 6 },
6099 { OFPACT_SET_IPV4_DST, 7 },
6100 { OFPACT_SET_IP_DSCP, 8 },
6101 { OFPACT_SET_L4_SRC_PORT, 9 },
6102 { OFPACT_SET_L4_DST_PORT, 10 },
6103 { OFPACT_ENQUEUE, 11 },
6107 /* OpenFlow 1.1 actions. */
6108 static const struct ofpact_map of11[] = {
6109 { OFPACT_OUTPUT, 0 },
6110 { OFPACT_SET_VLAN_VID, 1 },
6111 { OFPACT_SET_VLAN_PCP, 2 },
6112 { OFPACT_SET_ETH_SRC, 3 },
6113 { OFPACT_SET_ETH_DST, 4 },
6114 { OFPACT_SET_IPV4_SRC, 5 },
6115 { OFPACT_SET_IPV4_DST, 6 },
6116 { OFPACT_SET_IP_DSCP, 7 },
6117 { OFPACT_SET_IP_ECN, 8 },
6118 { OFPACT_SET_L4_SRC_PORT, 9 },
6119 { OFPACT_SET_L4_DST_PORT, 10 },
6120 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6121 /* OFPAT_COPY_TTL_IN (12) not supported. */
6122 { OFPACT_SET_MPLS_LABEL, 13 },
6123 { OFPACT_SET_MPLS_TC, 14 },
6124 { OFPACT_SET_MPLS_TTL, 15 },
6125 { OFPACT_DEC_MPLS_TTL, 16 },
6126 { OFPACT_PUSH_VLAN, 17 },
6127 { OFPACT_STRIP_VLAN, 18 },
6128 { OFPACT_PUSH_MPLS, 19 },
6129 { OFPACT_POP_MPLS, 20 },
6130 { OFPACT_SET_QUEUE, 21 },
6131 { OFPACT_GROUP, 22 },
6132 { OFPACT_SET_IP_TTL, 23 },
6133 { OFPACT_DEC_TTL, 24 },
6137 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6138 static const struct ofpact_map of12[] = {
6139 { OFPACT_OUTPUT, 0 },
6140 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6141 /* OFPAT_COPY_TTL_IN (12) not supported. */
6142 { OFPACT_SET_MPLS_TTL, 15 },
6143 { OFPACT_DEC_MPLS_TTL, 16 },
6144 { OFPACT_PUSH_VLAN, 17 },
6145 { OFPACT_STRIP_VLAN, 18 },
6146 { OFPACT_PUSH_MPLS, 19 },
6147 { OFPACT_POP_MPLS, 20 },
6148 { OFPACT_SET_QUEUE, 21 },
6149 { OFPACT_GROUP, 22 },
6150 { OFPACT_SET_IP_TTL, 23 },
6151 { OFPACT_DEC_TTL, 24 },
6152 { OFPACT_SET_FIELD, 25 },
6153 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
6154 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
6174 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
6175 * values, into a bitmap of actions suitable for OpenFlow 'version', and
6176 * returns the result. */
6178 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
6180 uint32_t openflow_bitmap = 0;
6181 const struct ofpact_map *x;
6183 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6184 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
6185 openflow_bitmap |= 1u << x->ofpat;
6188 return htonl(openflow_bitmap);
6191 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
6192 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
6193 * and returns the result. */
6195 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
6197 uint64_t ofpact_bitmap = 0;
6198 const struct ofpact_map *x;
6200 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6201 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
6202 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
6205 return ofpact_bitmap;
6208 /* Appends to 's' a string representation of the set of OFPACT_* represented
6209 * by 'ofpacts_bitmap'. */
6211 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
6213 if (!ofpacts_bitmap) {
6214 ds_put_cstr(s, "<none>");
6216 while (ofpacts_bitmap) {
6217 ds_put_format(s, "%s ",
6218 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
6219 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
6225 /* Returns true if 'action' outputs to 'port', false otherwise. */
6227 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
6229 switch (ofpact->type) {
6231 return ofpact_get_OUTPUT(ofpact)->port == port;
6232 case OFPACT_ENQUEUE:
6233 return ofpact_get_ENQUEUE(ofpact)->port == port;
6234 case OFPACT_CONTROLLER:
6235 return port == OFPP_CONTROLLER;
6237 case OFPACT_OUTPUT_REG:
6239 case OFPACT_SET_VLAN_VID:
6240 case OFPACT_SET_VLAN_PCP:
6241 case OFPACT_STRIP_VLAN:
6242 case OFPACT_PUSH_VLAN:
6243 case OFPACT_SET_ETH_SRC:
6244 case OFPACT_SET_ETH_DST:
6245 case OFPACT_SET_IPV4_SRC:
6246 case OFPACT_SET_IPV4_DST:
6247 case OFPACT_SET_IP_DSCP:
6248 case OFPACT_SET_IP_ECN:
6249 case OFPACT_SET_IP_TTL:
6250 case OFPACT_SET_L4_SRC_PORT:
6251 case OFPACT_SET_L4_DST_PORT:
6252 case OFPACT_REG_MOVE:
6253 case OFPACT_SET_FIELD:
6254 case OFPACT_STACK_PUSH:
6255 case OFPACT_STACK_POP:
6256 case OFPACT_DEC_TTL:
6257 case OFPACT_SET_MPLS_LABEL:
6258 case OFPACT_SET_MPLS_TC:
6259 case OFPACT_SET_MPLS_TTL:
6260 case OFPACT_DEC_MPLS_TTL:
6261 case OFPACT_SET_TUNNEL:
6262 case OFPACT_WRITE_METADATA:
6263 case OFPACT_SET_QUEUE:
6264 case OFPACT_POP_QUEUE:
6265 case OFPACT_FIN_TIMEOUT:
6266 case OFPACT_RESUBMIT:
6268 case OFPACT_CONJUNCTION:
6269 case OFPACT_MULTIPATH:
6272 case OFPACT_UNROLL_XLATE:
6273 case OFPACT_PUSH_MPLS:
6274 case OFPACT_POP_MPLS:
6276 case OFPACT_CLEAR_ACTIONS:
6277 case OFPACT_WRITE_ACTIONS:
6278 case OFPACT_GOTO_TABLE:
6281 case OFPACT_DEBUG_RECIRC:
6287 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6288 * to 'port', false otherwise. */
6290 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
6293 const struct ofpact *a;
6295 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6296 if (ofpact_outputs_to_port(a, port)) {
6304 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6305 * to 'group', false otherwise. */
6307 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
6310 const struct ofpact *a;
6312 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6313 if (a->type == OFPACT_GROUP
6314 && ofpact_get_GROUP(a)->group_id == group_id) {
6323 ofpacts_equal(const struct ofpact *a, size_t a_len,
6324 const struct ofpact *b, size_t b_len)
6326 return a_len == b_len && !memcmp(a, b, a_len);
6329 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
6330 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
6332 * This function relies on the order of 'ofpacts' being correct (as checked by
6333 * ofpacts_verify()). */
6335 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
6337 const struct ofpact *a;
6339 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6340 enum ovs_instruction_type inst;
6342 inst = ovs_instruction_type_from_ofpact_type(a->type);
6343 if (a->type == OFPACT_METER) {
6344 return ofpact_get_METER(a)->meter_id;
6345 } else if (inst > OVSINST_OFPIT13_METER) {
6353 /* Formatting ofpacts. */
6356 ofpact_format(const struct ofpact *a, struct ds *s)
6359 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6360 case OFPACT_##ENUM: \
6361 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
6370 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
6371 * 'ofpacts' to 'string'. */
6373 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
6377 ds_put_cstr(string, "drop");
6379 const struct ofpact *a;
6381 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6383 ds_put_cstr(string, ",");
6386 /* XXX write-actions */
6387 ofpact_format(a, string);
6392 /* Internal use by helpers. */
6395 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
6397 struct ofpact *ofpact;
6399 ofpact_pad(ofpacts);
6400 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
6401 ofpact = ofpacts->header;
6402 ofpact_init(ofpact, type, len);
6407 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
6409 memset(ofpact, 0, len);
6410 ofpact->type = type;
6415 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6416 * starting at 'ofpact'.
6418 * This is the correct way to update a variable-length ofpact's length after
6419 * adding the variable-length part of the payload. (See the large comment
6420 * near the end of ofp-actions.h for more information.) */
6422 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
6424 ovs_assert(ofpact == ofpacts->header);
6425 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6428 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6429 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6430 * client must call this itself after adding the final ofpact to an array of
6433 * (The consequences of failing to call this function are probably not dire.
6434 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6435 * not dereference it. That's undefined behavior, technically, but it will not
6436 * cause a real problem on common systems. Still, it seems better to call
6439 ofpact_pad(struct ofpbuf *ofpacts)
6441 unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
6443 ofpbuf_put_zeros(ofpacts, pad);
6450 static char * OVS_WARN_UNUSED_RESULT
6451 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6452 enum ofputil_protocol *usable_protocols)
6455 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6456 case OFPACT_##ENUM: \
6457 return parse_##ENUM(value, ofpacts, usable_protocols);
6466 ofpact_type_from_name(const char *name, enum ofpact_type *type)
6468 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6469 if (!strcasecmp(name, NAME)) { \
6470 *type = OFPACT_##ENUM; \
6479 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6481 * Returns NULL if successful, otherwise a malloc()'d string describing the
6482 * error. The caller is responsible for freeing the returned string.
6484 * If 'outer_action' is specified, indicates that the actions being parsed
6485 * are nested within another action of the type specified in 'outer_action'. */
6486 static char * OVS_WARN_UNUSED_RESULT
6487 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6488 enum ofputil_protocol *usable_protocols,
6489 bool allow_instructions, enum ofpact_type outer_action)
6498 while (ofputil_parse_key_value(&pos, &key, &value)) {
6499 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6500 enum ofpact_type type;
6504 if (ofpact_type_from_name(key, &type)) {
6505 error = ofpact_parse(type, value, ofpacts, usable_protocols);
6506 inst = ovs_instruction_type_from_ofpact_type(type);
6507 } else if (!strcasecmp(key, "mod_vlan_vid")) {
6508 error = parse_set_vlan_vid(value, ofpacts, true);
6509 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6510 error = parse_set_vlan_pcp(value, ofpacts, true);
6511 } else if (!strcasecmp(key, "set_nw_ttl")) {
6512 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6513 } else if (!strcasecmp(key, "pop_vlan")) {
6514 error = parse_pop_vlan(ofpacts);
6515 } else if (!strcasecmp(key, "set_tunnel64")) {
6516 error = parse_set_tunnel(value, ofpacts,
6517 NXAST_RAW_SET_TUNNEL64);
6518 } else if (!strcasecmp(key, "load")) {
6519 error = parse_reg_load(value, ofpacts);
6520 } else if (!strcasecmp(key, "bundle_load")) {
6521 error = parse_bundle_load(value, ofpacts);
6522 } else if (!strcasecmp(key, "drop")) {
6524 } else if (!strcasecmp(key, "apply_actions")) {
6525 return xstrdup("apply_actions is the default instruction");
6526 } else if (ofputil_port_from_string(key, &port)) {
6527 ofpact_put_OUTPUT(ofpacts)->port = port;
6529 return xasprintf("unknown action %s", key);
6535 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
6536 if (!allow_instructions) {
6537 return xasprintf("only actions are allowed here (not "
6539 ovs_instruction_name_from_type(inst));
6541 if (inst == prev_inst) {
6542 return xasprintf("instruction %s may be specified only once",
6543 ovs_instruction_name_from_type(inst));
6546 if (prev_inst != -1 && inst < prev_inst) {
6547 return xasprintf("instruction %s must be specified before %s",
6548 ovs_instruction_name_from_type(inst),
6549 ovs_instruction_name_from_type(prev_inst));
6553 ofpact_pad(ofpacts);
6555 if (drop && ofpacts->size) {
6556 return xstrdup("\"drop\" must not be accompanied by any other action "
6560 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
6562 ? (1u << N_OVS_INSTRUCTIONS) - 1
6563 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
6566 return xstrdup("Incorrect instruction ordering");
6572 static char * OVS_WARN_UNUSED_RESULT
6573 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
6574 enum ofputil_protocol *usable_protocols, bool allow_instructions,
6575 enum ofpact_type outer_action)
6577 uint32_t orig_size = ofpacts->size;
6578 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
6579 allow_instructions, outer_action);
6581 ofpacts->size = orig_size;
6586 static char * OVS_WARN_UNUSED_RESULT
6587 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
6588 enum ofputil_protocol *usable_protocols,
6589 bool allow_instructions, enum ofpact_type outer_action)
6593 *usable_protocols = OFPUTIL_P_ANY;
6596 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
6603 /* Parses 's' as a set of OpenFlow actions and appends the actions to
6604 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
6605 * that are nested within the action of type 'outer_action'.
6607 * Returns NULL if successful, otherwise a malloc()'d string describing the
6608 * error. The caller is responsible for freeing the returned string. */
6609 char * OVS_WARN_UNUSED_RESULT
6610 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
6611 enum ofputil_protocol *usable_protocols)
6613 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
6616 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
6619 * Returns NULL if successful, otherwise a malloc()'d string describing the
6620 * error. The caller is responsible for freeing the returned string. */
6621 char * OVS_WARN_UNUSED_RESULT
6622 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
6623 enum ofputil_protocol *usable_protocols)
6625 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
6629 ofpact_name(enum ofpact_type type)
6632 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
6639 /* Low-level action decoding and encoding functions. */
6641 /* Everything needed to identify a particular OpenFlow action. */
6642 struct ofpact_hdrs {
6643 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
6644 uint16_t type; /* Type if standard, otherwise subtype. */
6645 uint8_t ofp_version; /* From ofp_header. */
6648 /* Information about a particular OpenFlow action. */
6649 struct ofpact_raw_instance {
6650 /* The action's identity. */
6651 struct ofpact_hdrs hdrs;
6652 enum ofp_raw_action_type raw;
6654 /* Looking up the action. */
6655 struct hmap_node decode_node; /* Based on 'hdrs'. */
6656 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
6658 /* The action's encoded size.
6660 * If this action is fixed-length, 'min_length' == 'max_length'.
6661 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
6662 * OFP_ACTION_ALIGN) == 65528. */
6663 unsigned short int min_length;
6664 unsigned short int max_length;
6666 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
6667 * offset of that argument from the beginning of the action and 'arg_len'
6668 * its length, both in bytes.
6670 * For actions that take other forms, these are both zero. */
6671 unsigned short int arg_ofs;
6672 unsigned short int arg_len;
6674 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
6677 /* If this action is deprecated, a human-readable string with a brief
6679 const char *deprecation;
6682 /* Action header. */
6683 struct ofp_action_header {
6684 /* The meaning of other values of 'type' generally depends on the OpenFlow
6685 * version (see enum ofp_raw_action_type).
6687 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
6688 * designates an OpenFlow vendor ID and that the remainder of the action
6689 * structure has a vendor-defined meaning.
6691 #define OFPAT_VENDOR 0xffff
6694 /* Always a multiple of 8. */
6697 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
6698 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
6701 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
6703 /* Header for Nicira-defined actions and for ONF vendor extensions.
6705 * This cannot be used as an entirely generic vendor extension action header,
6706 * because OpenFlow does not specify the location or size of the action
6707 * subtype; it just happens that ONF extensions and Nicira extensions share
6709 struct ext_action_header {
6710 ovs_be16 type; /* OFPAT_VENDOR. */
6711 ovs_be16 len; /* At least 16. */
6712 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
6713 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
6716 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
6719 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
6720 const struct ofpact_hdrs *b)
6722 return (a->vendor == b->vendor
6723 && a->type == b->type
6724 && a->ofp_version == b->ofp_version);
6728 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
6730 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
6733 #include "ofp-actions.inc2"
6735 static struct hmap *
6736 ofpact_decode_hmap(void)
6738 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6739 static struct hmap hmap;
6741 if (ovsthread_once_start(&once)) {
6742 struct ofpact_raw_instance *inst;
6745 for (inst = all_raw_instances;
6746 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6748 hmap_insert(&hmap, &inst->decode_node,
6749 ofpact_hdrs_hash(&inst->hdrs));
6751 ovsthread_once_done(&once);
6756 static struct hmap *
6757 ofpact_encode_hmap(void)
6759 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6760 static struct hmap hmap;
6762 if (ovsthread_once_start(&once)) {
6763 struct ofpact_raw_instance *inst;
6766 for (inst = all_raw_instances;
6767 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6769 hmap_insert(&hmap, &inst->encode_node,
6770 hash_2words(inst->raw, inst->hdrs.ofp_version));
6772 ovsthread_once_done(&once);
6778 ofpact_decode_raw(enum ofp_version ofp_version,
6779 const struct ofp_action_header *oah, size_t length,
6780 const struct ofpact_raw_instance **instp)
6782 const struct ofpact_raw_instance *inst;
6783 struct ofpact_hdrs hdrs;
6786 if (length < sizeof *oah) {
6787 return OFPERR_OFPBAC_BAD_LEN;
6790 /* Get base action type. */
6791 if (oah->type == htons(OFPAT_VENDOR)) {
6793 hdrs.vendor = ntohl(oah->vendor);
6794 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
6795 /* Get extension subtype. */
6796 const struct ext_action_header *nah;
6798 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
6799 if (length < sizeof *nah) {
6800 return OFPERR_OFPBAC_BAD_LEN;
6802 hdrs.type = ntohs(nah->subtype);
6804 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
6806 return OFPERR_OFPBAC_BAD_VENDOR;
6810 hdrs.type = ntohs(oah->type);
6813 hdrs.ofp_version = ofp_version;
6814 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
6815 ofpact_decode_hmap()) {
6816 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
6823 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
6824 : OFPERR_OFPBAC_BAD_TYPE);
6828 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6829 enum ofp_raw_action_type *raw, uint64_t *arg)
6831 const struct ofp_action_header *oah = buf->data;
6832 const struct ofpact_raw_instance *action;
6833 unsigned int length;
6837 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
6842 if (action->deprecation) {
6843 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
6844 action->name, ofputil_version_to_string(ofp_version),
6845 action->deprecation);
6848 length = ntohs(oah->len);
6849 if (length > buf->size) {
6850 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
6851 "length %"PRIu32, action->name, length, buf->size);
6852 return OFPERR_OFPBAC_BAD_LEN;
6854 if (length < action->min_length || length > action->max_length) {
6855 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
6856 "[%hu,%hu]", action->name, length,
6857 action->min_length, action->max_length);
6858 return OFPERR_OFPBAC_BAD_LEN;
6861 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
6862 "of 8", action->name, length);
6863 return OFPERR_OFPBAC_BAD_LEN;
6868 if (action->arg_len) {
6872 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
6873 for (i = 0; i < action->arg_len; i++) {
6874 *arg = (*arg << 8) | p[i];
6878 ofpbuf_pull(buf, length);
6883 static const struct ofpact_raw_instance *
6884 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
6886 const struct ofpact_raw_instance *inst;
6888 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
6889 ofpact_encode_hmap()) {
6890 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
6898 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6899 enum ofp_raw_action_type raw, uint64_t arg)
6901 const struct ofpact_raw_instance *inst;
6902 struct ofp_action_header *oah;
6903 const struct ofpact_hdrs *hdrs;
6905 inst = ofpact_raw_lookup(ofp_version, raw);
6908 oah = ofpbuf_put_zeros(buf, inst->min_length);
6909 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
6910 oah->len = htons(inst->min_length);
6911 oah->vendor = htonl(hdrs->vendor);
6913 switch (hdrs->vendor) {
6918 case ONF_VENDOR_ID: {
6919 struct ext_action_header *nah = (struct ext_action_header *) oah;
6920 nah->subtype = htons(hdrs->type);
6928 if (inst->arg_len) {
6929 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
6932 for (i = 0; i < inst->arg_len; i++) {
6944 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
6946 struct ofp_action_header *oah;
6948 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs, 8));
6950 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
6951 oah->len = htons(openflow->size - start_ofs);