2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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"
22 #include "dynamic-string.h"
25 #include "meta-flow.h"
26 #include "multipath.h"
28 #include "ofp-parse.h"
31 #include "unaligned.h"
35 VLOG_DEFINE_THIS_MODULE(ofp_actions);
37 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
39 struct ofp_action_header;
41 /* Raw identifiers for OpenFlow actions.
43 * Decoding and encoding OpenFlow actions across multiple versions is difficult
44 * to do in a clean, consistent way. This enumeration lays out all of the
45 * forms of actions that Open vSwitch supports.
47 * The comments here must follow a stylized form because the
48 * "extract-ofp-actions" program parses them at build time to generate data
51 * - The first part of each comment specifies the vendor, OpenFlow versions,
52 * and type for each protocol that supports the action:
54 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
55 * extension actions. (Support for other vendors can be added, but
56 * it can't be done just based on a vendor ID definition alone
57 * because OpenFlow doesn't define a standard way to specify a
58 * subtype for vendor actions, so other vendors might do it different
61 * # The version can specify a specific OpenFlow version, a version
62 * range delimited by "-", or an open-ended range with "+".
64 * # The type, in parentheses, is the action type number (for standard
65 * OpenFlow actions) or subtype (for vendor extension actions).
67 * # Optionally one may add "is deprecated" followed by a
68 * human-readable reason in parentheses (which will be used in log
69 * messages), if a particular action should no longer be used.
71 * Multiple such specifications may be separated by commas.
73 * - The second part describes the action's wire format. It may be:
75 * # "struct <name>": The struct fully specifies the wire format. The
76 * action is exactly the size of the struct. (Thus, the struct must
77 * be an exact multiple of 8 bytes in size.)
79 * # "struct <name>, ...": The struct specifies the beginning of the
80 * wire format. An instance of the action is either the struct's
81 * exact size, or a multiple of 8 bytes longer.
83 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
84 * vendor extension) header, followed by 0 or more pad bytes to align
85 * to a multiple of <N> bits, followed by an argument of the given
86 * type, followed by 0 or more pad bytes to bring the total action up
87 * to a multiple of 8 bytes.
89 * # "void": The action is just a (standard or vendor extension)
92 * - Optional additional text enclosed in square brackets is commentary for
95 enum ofp_raw_action_type {
96 /* ## ----------------- ## */
97 /* ## Standard actions. ## */
98 /* ## ----------------- ## */
100 /* OF1.0(0): struct ofp10_action_output. */
102 /* OF1.1+(0): struct ofp11_action_output. */
105 /* OF1.0(1): uint16_t. */
106 OFPAT_RAW10_SET_VLAN_VID,
107 /* OF1.0(2): uint8_t. */
108 OFPAT_RAW10_SET_VLAN_PCP,
110 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
112 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
113 * modification actions: the 1.0 versions push a VLAN header if none is
114 * present, but the 1.1 versions do not. That is the only reason that we
115 * distinguish their raw action types.] */
116 OFPAT_RAW11_SET_VLAN_VID,
117 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
118 OFPAT_RAW11_SET_VLAN_PCP,
120 /* OF1.1+(17): ovs_be16.
122 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
124 OFPAT_RAW11_PUSH_VLAN,
126 /* OF1.0(3): void. */
127 OFPAT_RAW10_STRIP_VLAN,
128 /* OF1.1+(18): void. */
129 OFPAT_RAW11_POP_VLAN,
131 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
132 * ofp_action_dl_addr. */
133 OFPAT_RAW_SET_DL_SRC,
135 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
136 * ofp_action_dl_addr. */
137 OFPAT_RAW_SET_DL_DST,
139 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
141 OFPAT_RAW_SET_NW_SRC,
143 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
145 OFPAT_RAW_SET_NW_DST,
147 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
148 OFPAT_RAW_SET_NW_TOS,
150 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
151 OFPAT_RAW11_SET_NW_ECN,
153 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
155 OFPAT_RAW_SET_TP_SRC,
157 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
159 OFPAT_RAW_SET_TP_DST,
161 /* OF1.0(11): struct ofp10_action_enqueue. */
164 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
166 OFPAT_RAW_SET_MPLS_LABEL,
168 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
170 OFPAT_RAW_SET_MPLS_TC,
172 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
174 OFPAT_RAW_SET_MPLS_TTL,
176 /* NX1.0(26), OF1.1+(16): void. */
177 OFPAT_RAW_DEC_MPLS_TTL,
179 /* NX1.0(23), OF1.1+(19): ovs_be16.
181 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
184 /* NX1.0(24), OF1.1+(20): ovs_be16.
186 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
187 * ETH_TYPE_MPLS otherwise, not the label.] */
190 /* NX1.0(4), OF1.1+(21): uint32_t. */
193 /* OF1.1+(22): uint32_t. */
196 /* OF1.1+(23): uint8_t. */
197 OFPAT_RAW11_SET_NW_TTL,
199 /* NX1.0(18), OF1.1+(24): void. */
200 OFPAT_RAW_DEC_NW_TTL,
201 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
202 NXAST_RAW_DEC_TTL_CNT_IDS,
204 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
205 OFPAT_RAW12_SET_FIELD,
206 /* OF1.5+(25): struct ofp12_action_set_field, ... */
207 OFPAT_RAW15_SET_FIELD,
208 /* NX1.0-1.4(7): struct nx_action_reg_load.
210 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
211 * we drop reg_load.] */
213 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
215 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
216 * we drop reg_load2.] */
219 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
220 OFPAT_RAW15_COPY_FIELD,
221 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
224 /* ## ------------------------- ## */
225 /* ## Nicira extension actions. ## */
226 /* ## ------------------------- ## */
228 /* Actions similar to standard actions are listed with the standard actions. */
230 /* NX1.0+(1): uint16_t. */
232 /* NX1.0+(14): struct nx_action_resubmit. */
233 NXAST_RAW_RESUBMIT_TABLE,
235 /* NX1.0+(2): uint32_t. */
236 NXAST_RAW_SET_TUNNEL,
237 /* NX1.0+(9): uint64_t. */
238 NXAST_RAW_SET_TUNNEL64,
240 /* NX1.0+(5): void. */
243 /* NX1.0+(8): struct nx_action_note, ... */
246 /* NX1.0+(10): struct nx_action_multipath. */
249 /* NX1.0+(12): struct nx_action_bundle, ... */
251 /* NX1.0+(13): struct nx_action_bundle, ... */
252 NXAST_RAW_BUNDLE_LOAD,
254 /* NX1.0+(15): struct nx_action_output_reg. */
255 NXAST_RAW_OUTPUT_REG,
256 /* NX1.0+(32): struct nx_action_output_reg2. */
257 NXAST_RAW_OUTPUT_REG2,
259 /* NX1.0+(16): struct nx_action_learn, ... */
262 /* NX1.0+(17): void. */
265 /* NX1.0+(19): struct nx_action_fin_timeout. */
266 NXAST_RAW_FIN_TIMEOUT,
268 /* NX1.0+(20): struct nx_action_controller. */
269 NXAST_RAW_CONTROLLER,
271 /* NX1.0+(22): struct nx_action_write_metadata. */
272 NXAST_RAW_WRITE_METADATA,
274 /* NX1.0+(27): struct nx_action_stack. */
275 NXAST_RAW_STACK_PUSH,
277 /* NX1.0+(28): struct nx_action_stack. */
280 /* NX1.0+(29): struct nx_action_sample. */
284 /* OpenFlow actions are always a multiple of 8 bytes in length. */
285 #define OFP_ACTION_ALIGN 8
287 /* Define a few functions for working with instructions. */
288 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
289 static inline const struct STRUCT * OVS_UNUSED \
290 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
292 ovs_assert(inst->type == htons(ENUM)); \
293 return ALIGNED_CAST(struct STRUCT *, inst); \
296 static inline void OVS_UNUSED \
297 instruction_init_##ENUM(struct STRUCT *s) \
299 memset(s, 0, sizeof *s); \
300 s->type = htons(ENUM); \
301 s->len = htons(sizeof *s); \
304 static inline struct STRUCT * OVS_UNUSED \
305 instruction_put_##ENUM(struct ofpbuf *buf) \
307 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
308 instruction_init_##ENUM(s); \
314 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
316 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
318 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
319 uint32_t allowed_ovsinsts);
321 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
322 enum mf_field_id, uint64_t value);
324 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
325 enum ofp_raw_action_type *, uint64_t *arg);
326 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
327 enum ofp_raw_action_type, uint64_t arg);
329 static char *WARN_UNUSED_RESULT ofpacts_parse(
330 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
331 bool allow_instructions);
333 #include "ofp-actions.inc1"
335 /* Output actions. */
337 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
338 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
339 * number of bytes to send. A 'max_len' of zero means no bytes of the
340 * packet should be sent. */
341 struct ofp10_action_output {
342 ovs_be16 type; /* OFPAT10_OUTPUT. */
343 ovs_be16 len; /* Length is 8. */
344 ovs_be16 port; /* Output port. */
345 ovs_be16 max_len; /* Max length to send to controller. */
347 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
349 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
350 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
351 * number of bytes to send. A 'max_len' of zero means no bytes of the
352 * packet should be sent.*/
353 struct ofp11_action_output {
354 ovs_be16 type; /* OFPAT11_OUTPUT. */
355 ovs_be16 len; /* Length is 16. */
356 ovs_be32 port; /* Output port. */
357 ovs_be16 max_len; /* Max length to send to controller. */
358 uint8_t pad[6]; /* Pad to 64 bits. */
360 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
363 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
366 struct ofpact_output *output;
368 output = ofpact_put_OUTPUT(out);
369 output->port = u16_to_ofp(ntohs(oao->port));
370 output->max_len = ntohs(oao->max_len);
372 return ofpact_check_output_port(output->port, OFPP_MAX);
376 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
379 struct ofpact_output *output;
382 output = ofpact_put_OUTPUT(out);
383 output->max_len = ntohs(oao->max_len);
385 error = ofputil_port_from_ofp11(oao->port, &output->port);
390 return ofpact_check_output_port(output->port, OFPP_MAX);
394 encode_OUTPUT(const struct ofpact_output *output,
395 enum ofp_version ofp_version, struct ofpbuf *out)
397 if (ofp_version == OFP10_VERSION) {
398 struct ofp10_action_output *oao;
400 oao = put_OFPAT10_OUTPUT(out);
401 oao->port = htons(ofp_to_u16(output->port));
402 oao->max_len = htons(output->max_len);
404 struct ofp11_action_output *oao;
406 oao = put_OFPAT11_OUTPUT(out);
407 oao->port = ofputil_port_to_ofp11(output->port);
408 oao->max_len = htons(output->max_len);
412 static char * WARN_UNUSED_RESULT
413 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
414 enum ofputil_protocol *usable_protocols OVS_UNUSED)
416 if (strchr(arg, '[')) {
417 struct ofpact_output_reg *output_reg;
419 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
420 output_reg->max_len = UINT16_MAX;
421 return mf_parse_subfield(&output_reg->src, arg);
423 struct ofpact_output *output;
425 output = ofpact_put_OUTPUT(ofpacts);
426 if (!ofputil_port_from_string(arg, &output->port)) {
427 return xasprintf("%s: output to unknown port", arg);
429 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
435 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
437 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
438 ds_put_format(s, "output:%"PRIu16, a->port);
440 ofputil_format_port(a->port, s);
441 if (a->port == OFPP_CONTROLLER) {
442 ds_put_format(s, ":%"PRIu16, a->max_len);
450 decode_OFPAT_RAW11_GROUP(uint32_t group_id, struct ofpbuf *out)
452 ofpact_put_GROUP(out)->group_id = group_id;
457 encode_GROUP(const struct ofpact_group *group,
458 enum ofp_version ofp_version, struct ofpbuf *out)
460 if (ofp_version == OFP10_VERSION) {
463 put_OFPAT11_GROUP(out, group->group_id);
467 static char * WARN_UNUSED_RESULT
468 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
469 enum ofputil_protocol *usable_protocols OVS_UNUSED)
471 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
475 format_GROUP(const struct ofpact_group *a, struct ds *s)
477 ds_put_format(s, "group:%"PRIu32, a->group_id);
480 /* Action structure for NXAST_CONTROLLER.
482 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
483 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
486 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
488 * - 'controller_id': The ID of the controller connection to which the
489 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
490 * sent only to controllers that have the specified controller connection
491 * ID. See "struct nx_controller_id" for more information. */
492 struct nx_action_controller {
493 ovs_be16 type; /* OFPAT_VENDOR. */
494 ovs_be16 len; /* Length is 16. */
495 ovs_be32 vendor; /* NX_VENDOR_ID. */
496 ovs_be16 subtype; /* NXAST_CONTROLLER. */
497 ovs_be16 max_len; /* Maximum length to send to controller. */
498 ovs_be16 controller_id; /* Controller ID to send packet-in. */
499 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
500 uint8_t zero; /* Must be zero. */
502 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
505 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
508 struct ofpact_controller *oc;
510 oc = ofpact_put_CONTROLLER(out);
511 oc->max_len = ntohs(nac->max_len);
512 oc->controller_id = ntohs(nac->controller_id);
513 oc->reason = nac->reason;
518 encode_CONTROLLER(const struct ofpact_controller *controller,
519 enum ofp_version ofp_version OVS_UNUSED,
522 struct nx_action_controller *nac;
524 nac = put_NXAST_CONTROLLER(out);
525 nac->max_len = htons(controller->max_len);
526 nac->controller_id = htons(controller->controller_id);
527 nac->reason = controller->reason;
530 static char * WARN_UNUSED_RESULT
531 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
532 enum ofputil_protocol *usable_protocols OVS_UNUSED)
534 enum ofp_packet_in_reason reason = OFPR_ACTION;
535 uint16_t controller_id = 0;
536 uint16_t max_len = UINT16_MAX;
540 } else if (strspn(arg, "0123456789") == strlen(arg)) {
541 char *error = str_to_u16(arg, "max_len", &max_len);
548 while (ofputil_parse_key_value(&arg, &name, &value)) {
549 if (!strcmp(name, "reason")) {
550 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
551 return xasprintf("unknown reason \"%s\"", value);
553 } else if (!strcmp(name, "max_len")) {
554 char *error = str_to_u16(value, "max_len", &max_len);
558 } else if (!strcmp(name, "id")) {
559 char *error = str_to_u16(value, "id", &controller_id);
564 return xasprintf("unknown key \"%s\" parsing controller "
570 if (reason == OFPR_ACTION && controller_id == 0) {
571 struct ofpact_output *output;
573 output = ofpact_put_OUTPUT(ofpacts);
574 output->port = OFPP_CONTROLLER;
575 output->max_len = max_len;
577 struct ofpact_controller *controller;
579 controller = ofpact_put_CONTROLLER(ofpacts);
580 controller->max_len = max_len;
581 controller->reason = reason;
582 controller->controller_id = controller_id;
589 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
591 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
592 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
594 enum ofp_packet_in_reason reason = a->reason;
596 ds_put_cstr(s, "controller(");
597 if (reason != OFPR_ACTION) {
598 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
600 ds_put_format(s, "reason=%s,",
601 ofputil_packet_in_reason_to_string(
602 reason, reasonbuf, sizeof reasonbuf));
604 if (a->max_len != UINT16_MAX) {
605 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
607 if (a->controller_id != 0) {
608 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
615 /* Enqueue action. */
616 struct ofp10_action_enqueue {
617 ovs_be16 type; /* OFPAT10_ENQUEUE. */
618 ovs_be16 len; /* Len is 16. */
619 ovs_be16 port; /* Port that queue belongs. Should
620 refer to a valid physical port
621 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
622 uint8_t pad[6]; /* Pad for 64-bit alignment. */
623 ovs_be32 queue_id; /* Where to enqueue the packets. */
625 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
628 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
631 struct ofpact_enqueue *enqueue;
633 enqueue = ofpact_put_ENQUEUE(out);
634 enqueue->port = u16_to_ofp(ntohs(oae->port));
635 enqueue->queue = ntohl(oae->queue_id);
636 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
637 && enqueue->port != OFPP_IN_PORT
638 && enqueue->port != OFPP_LOCAL) {
639 return OFPERR_OFPBAC_BAD_OUT_PORT;
645 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
646 enum ofp_version ofp_version, struct ofpbuf *out)
648 if (ofp_version == OFP10_VERSION) {
649 struct ofp10_action_enqueue *oae;
651 oae = put_OFPAT10_ENQUEUE(out);
652 oae->port = htons(ofp_to_u16(enqueue->port));
653 oae->queue_id = htonl(enqueue->queue);
659 static char * WARN_UNUSED_RESULT
660 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
661 enum ofputil_protocol *usable_protocols OVS_UNUSED)
664 char *port = strtok_r(arg, ":q,", &sp);
665 char *queue = strtok_r(NULL, "", &sp);
666 struct ofpact_enqueue *enqueue;
668 if (port == NULL || queue == NULL) {
669 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
670 "\"enqueue(PORT,QUEUE)\"");
673 enqueue = ofpact_put_ENQUEUE(ofpacts);
674 if (!ofputil_port_from_string(port, &enqueue->port)) {
675 return xasprintf("%s: enqueue to unknown port", port);
677 return str_to_u32(queue, &enqueue->queue);
681 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
683 ds_put_format(s, "enqueue:");
684 ofputil_format_port(a->port, s);
685 ds_put_format(s, ":%"PRIu32, a->queue);
688 /* Action structure for NXAST_OUTPUT_REG.
690 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
692 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
693 * the NXAST_REG_LOAD action.
695 * The acceptable nxm_header values for 'src' are the same as the acceptable
696 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
698 * The 'max_len' field indicates the number of bytes to send when the chosen
699 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
700 * field of OFPAT_OUTPUT.
702 * The 'zero' field is required to be zeroed for forward compatibility. */
703 struct nx_action_output_reg {
704 ovs_be16 type; /* OFPAT_VENDOR. */
705 ovs_be16 len; /* 24. */
706 ovs_be32 vendor; /* NX_VENDOR_ID. */
707 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
709 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
710 ovs_be32 src; /* Source. */
712 ovs_be16 max_len; /* Max length to send to controller. */
714 uint8_t zero[6]; /* Reserved, must be zero. */
716 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
718 /* Action structure for NXAST_OUTPUT_REG2.
720 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
721 * experimenter OXM as 'src'.
723 struct nx_action_output_reg2 {
724 ovs_be16 type; /* OFPAT_VENDOR. */
725 ovs_be16 len; /* 24. */
726 ovs_be32 vendor; /* NX_VENDOR_ID. */
727 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
729 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
730 ovs_be16 max_len; /* Max length to send to controller. */
733 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
734 * - Enough 0-bytes to pad the action out to 24 bytes. */
737 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
740 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
743 struct ofpact_output_reg *output_reg;
745 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
746 return OFPERR_OFPBAC_BAD_ARGUMENT;
749 output_reg = ofpact_put_OUTPUT_REG(out);
750 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
751 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
752 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
753 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
754 output_reg->max_len = ntohs(naor->max_len);
756 return mf_check_src(&output_reg->src, NULL);
760 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
763 struct ofpact_output_reg *output_reg;
767 output_reg = ofpact_put_OUTPUT_REG(out);
768 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
769 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
770 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
771 output_reg->max_len = ntohs(naor->max_len);
773 ofpbuf_use_const(&b, naor, ntohs(naor->len));
774 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
775 error = nx_pull_header(&b, &output_reg->src.field, NULL);
779 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
780 return OFPERR_NXBRC_MUST_BE_ZERO;
783 return mf_check_src(&output_reg->src, NULL);
787 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
788 enum ofp_version ofp_version OVS_UNUSED,
791 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
792 * cannot be encoded in the older form, encode it as
793 * NXAST_RAW_OUTPUT_REG2. */
794 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
795 || !mf_nxm_header(output_reg->src.field->id)) {
796 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
797 size_t size = ofpbuf_size(out);
799 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
800 output_reg->src.n_bits);
801 naor->max_len = htons(output_reg->max_len);
803 ofpbuf_set_size(out, size - sizeof naor->pad);
804 nx_put_header(out, output_reg->src.field->id, 0, false);
805 ofpbuf_set_size(out, size);
807 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
809 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
810 output_reg->src.n_bits);
811 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
812 naor->max_len = htons(output_reg->max_len);
816 static char * WARN_UNUSED_RESULT
817 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
818 enum ofputil_protocol *usable_protocols OVS_UNUSED)
820 return parse_OUTPUT(arg, ofpacts, usable_protocols);
824 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
826 ds_put_cstr(s, "output:");
827 mf_format_subfield(&a->src, s);
830 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
832 * The bundle actions choose a slave from a supplied list of options.
833 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
834 * selection to a register.
836 * The list of possible slaves follows the nx_action_bundle structure. The size
837 * of each slave is governed by its type as indicated by the 'slave_type'
838 * parameter. The list of slaves should be padded at its end with zeros to make
839 * the total length of the action a multiple of 8.
841 * Switches infer from the 'slave_type' parameter the size of each slave. All
842 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
843 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
844 * 2 byte width. Switches should reject actions which indicate unknown or
845 * unsupported slave types.
847 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
848 * slave. If the switch does not support the specified 'algorithm' parameter,
849 * it should reject the action.
851 * Several algorithms take into account liveness when selecting slaves. The
852 * liveness of a slave is implementation defined (with one exception), but will
853 * generally take into account things like its carrier status and the results
854 * of any link monitoring protocols which happen to be running on it. In order
855 * to give controllers a place-holder value, the OFPP_NONE port is always
858 * Some slave selection strategies require the use of a hash function, in which
859 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
860 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
861 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
862 * 'basis' parameter is used as a universal hash parameter. Different values
863 * of 'basis' yield different hash results.
865 * The 'zero' parameter at the end of the action structure is reserved for
866 * future use. Switches are required to reject actions which have nonzero
867 * bytes in the 'zero' field.
869 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
870 * should reject actions which have nonzero bytes in either of these fields.
872 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
873 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
874 * similar to those for the NXAST_REG_LOAD action. */
875 struct nx_action_bundle {
876 ovs_be16 type; /* OFPAT_VENDOR. */
877 ovs_be16 len; /* Length including slaves. */
878 ovs_be32 vendor; /* NX_VENDOR_ID. */
879 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
881 /* Slave choice algorithm to apply to hash value. */
882 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
884 /* What fields to hash and how. */
885 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
886 ovs_be16 basis; /* Universal hash parameter. */
888 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
889 ovs_be16 n_slaves; /* Number of slaves. */
891 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
892 ovs_be32 dst; /* Destination. */
894 uint8_t zero[4]; /* Reserved. Must be zero. */
896 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
899 decode_bundle(bool load, const struct nx_action_bundle *nab,
900 struct ofpbuf *ofpacts)
902 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
903 struct ofpact_bundle *bundle;
905 size_t slaves_size, i;
908 bundle = ofpact_put_BUNDLE(ofpacts);
910 bundle->n_slaves = ntohs(nab->n_slaves);
911 bundle->basis = ntohs(nab->basis);
912 bundle->fields = ntohs(nab->fields);
913 bundle->algorithm = ntohs(nab->algorithm);
914 slave_type = ntohl(nab->slave_type);
915 slaves_size = ntohs(nab->len) - sizeof *nab;
917 error = OFPERR_OFPBAC_BAD_ARGUMENT;
918 if (!flow_hash_fields_valid(bundle->fields)) {
919 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
920 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
921 VLOG_WARN_RL(&rl, "too many slaves");
922 } else if (bundle->algorithm != NX_BD_ALG_HRW
923 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
924 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
925 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
926 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
931 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
932 VLOG_WARN_RL(&rl, "reserved field is nonzero");
933 error = OFPERR_OFPBAC_BAD_ARGUMENT;
937 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
938 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
939 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
941 if (bundle->dst.n_bits < 16) {
942 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
944 error = OFPERR_OFPBAC_BAD_ARGUMENT;
947 if (nab->ofs_nbits || nab->dst) {
948 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
949 error = OFPERR_OFPBAC_BAD_ARGUMENT;
953 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
954 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
955 "allocated for slaves. %"PRIuSIZE" bytes are required "
956 "for %"PRIu16" slaves.",
957 load ? "bundle_load" : "bundle", slaves_size,
958 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
959 error = OFPERR_OFPBAC_BAD_LEN;
962 for (i = 0; i < bundle->n_slaves; i++) {
963 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
964 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
967 bundle = ofpacts->frame;
968 ofpact_update_len(ofpacts, &bundle->ofpact);
971 error = bundle_check(bundle, OFPP_MAX, NULL);
977 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab, struct ofpbuf *out)
979 return decode_bundle(false, nab, out);
983 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
986 return decode_bundle(true, nab, out);
990 encode_BUNDLE(const struct ofpact_bundle *bundle,
991 enum ofp_version ofp_version OVS_UNUSED,
994 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
995 struct nx_action_bundle *nab;
999 nab = (bundle->dst.field
1000 ? put_NXAST_BUNDLE_LOAD(out)
1001 : put_NXAST_BUNDLE(out));
1002 nab->len = htons(ntohs(nab->len) + slaves_len);
1003 nab->algorithm = htons(bundle->algorithm);
1004 nab->fields = htons(bundle->fields);
1005 nab->basis = htons(bundle->basis);
1006 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1007 nab->n_slaves = htons(bundle->n_slaves);
1008 if (bundle->dst.field) {
1009 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1010 bundle->dst.n_bits);
1011 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1014 slaves = ofpbuf_put_zeros(out, slaves_len);
1015 for (i = 0; i < bundle->n_slaves; i++) {
1016 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1020 static char * WARN_UNUSED_RESULT
1021 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1022 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1024 return bundle_parse(arg, ofpacts);
1027 static char * WARN_UNUSED_RESULT
1028 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1030 return bundle_parse_load(arg, ofpacts);
1034 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1036 bundle_format(a, s);
1039 /* Set VLAN actions. */
1042 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1045 return OFPERR_OFPBAC_BAD_ARGUMENT;
1047 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1048 vlan_vid->vlan_vid = vid;
1049 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1055 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid, struct ofpbuf *out)
1057 return decode_set_vlan_vid(vid, true, out);
1061 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid, struct ofpbuf *out)
1063 return decode_set_vlan_vid(vid, false, out);
1067 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1068 enum ofp_version ofp_version, struct ofpbuf *out)
1070 uint16_t vid = vlan_vid->vlan_vid;
1072 /* Push a VLAN tag, if none is present and this form of the action calls
1073 * for such a feature. */
1074 if (ofp_version > OFP10_VERSION
1075 && vlan_vid->push_vlan_if_needed
1076 && !vlan_vid->flow_has_vlan) {
1077 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1080 if (ofp_version == OFP10_VERSION) {
1081 put_OFPAT10_SET_VLAN_VID(out, vid);
1082 } else if (ofp_version == OFP11_VERSION) {
1083 put_OFPAT11_SET_VLAN_VID(out, vid);
1085 ofpact_put_set_field(out, ofp_version,
1086 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1090 static char * WARN_UNUSED_RESULT
1091 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1093 struct ofpact_vlan_vid *vlan_vid;
1097 error = str_to_u16(arg, "VLAN VID", &vid);
1102 if (vid & ~VLAN_VID_MASK) {
1103 return xasprintf("%s: not a valid VLAN VID", arg);
1105 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1106 vlan_vid->vlan_vid = vid;
1107 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1111 static char * WARN_UNUSED_RESULT
1112 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1113 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1115 return parse_set_vlan_vid(arg, ofpacts, false);
1119 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1121 ds_put_format(s, "%s:%"PRIu16,
1122 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1126 /* Set PCP actions. */
1129 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1132 return OFPERR_OFPBAC_BAD_ARGUMENT;
1134 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1135 vlan_pcp->vlan_pcp = pcp;
1136 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1142 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp, struct ofpbuf *out)
1144 return decode_set_vlan_pcp(pcp, true, out);
1148 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp, struct ofpbuf *out)
1150 return decode_set_vlan_pcp(pcp, false, out);
1154 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1155 enum ofp_version ofp_version, struct ofpbuf *out)
1157 uint8_t pcp = vlan_pcp->vlan_pcp;
1159 /* Push a VLAN tag, if none is present and this form of the action calls
1160 * for such a feature. */
1161 if (ofp_version > OFP10_VERSION
1162 && vlan_pcp->push_vlan_if_needed
1163 && !vlan_pcp->flow_has_vlan) {
1164 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1167 if (ofp_version == OFP10_VERSION) {
1168 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1169 } else if (ofp_version == OFP11_VERSION) {
1170 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1172 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1176 static char * WARN_UNUSED_RESULT
1177 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1179 struct ofpact_vlan_pcp *vlan_pcp;
1183 error = str_to_u8(arg, "VLAN PCP", &pcp);
1189 return xasprintf("%s: not a valid VLAN PCP", arg);
1191 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1192 vlan_pcp->vlan_pcp = pcp;
1193 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1197 static char * WARN_UNUSED_RESULT
1198 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1199 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1201 return parse_set_vlan_pcp(arg, ofpacts, false);
1205 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1207 ds_put_format(s, "%s:%"PRIu8,
1208 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1212 /* Strip VLAN actions. */
1215 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1217 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1222 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1224 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1229 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1230 enum ofp_version ofp_version, struct ofpbuf *out)
1232 if (ofp_version == OFP10_VERSION) {
1233 put_OFPAT10_STRIP_VLAN(out);
1235 put_OFPAT11_POP_VLAN(out);
1239 static char * WARN_UNUSED_RESULT
1240 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1241 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1243 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1247 static char * WARN_UNUSED_RESULT
1248 parse_pop_vlan(struct ofpbuf *ofpacts)
1250 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1255 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1257 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1262 /* Push VLAN action. */
1265 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type, struct ofpbuf *out)
1267 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1268 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1269 return OFPERR_OFPBAC_BAD_ARGUMENT;
1271 ofpact_put_PUSH_VLAN(out);
1276 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1277 enum ofp_version ofp_version, struct ofpbuf *out)
1279 if (ofp_version == OFP10_VERSION) {
1280 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1281 * follow this action. */
1283 /* XXX ETH_TYPE_VLAN_8021AD case */
1284 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1288 static char * WARN_UNUSED_RESULT
1289 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1290 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1295 *usable_protocols &= OFPUTIL_P_OF11_UP;
1296 error = str_to_u16(arg, "ethertype", ðertype);
1301 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1302 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1303 return xasprintf("%s: not a valid VLAN ethertype", arg);
1306 ofpact_put_PUSH_VLAN(ofpacts);
1311 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1313 /* XXX 802.1AD case*/
1314 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1317 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1318 struct ofp_action_dl_addr {
1319 ovs_be16 type; /* Type. */
1320 ovs_be16 len; /* Length is 16. */
1321 uint8_t dl_addr[OFP_ETH_ALEN]; /* Ethernet address. */
1324 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1327 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1330 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr, ETH_ADDR_LEN);
1335 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1338 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr, ETH_ADDR_LEN);
1343 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1344 enum ofp_raw_action_type raw, enum mf_field_id field,
1347 const uint8_t *addr = mac->mac;
1349 if (ofp_version < OFP12_VERSION) {
1350 struct ofp_action_dl_addr *oada;
1352 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1353 memcpy(oada->dl_addr, addr, ETH_ADDR_LEN);
1355 ofpact_put_set_field(out, ofp_version, field,
1356 eth_addr_to_uint64(addr));
1361 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1364 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1370 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1371 enum ofp_version ofp_version,
1374 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1379 static char * WARN_UNUSED_RESULT
1380 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1381 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1383 return str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1386 static char * WARN_UNUSED_RESULT
1387 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1388 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1390 return str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
1394 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1396 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1400 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1402 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1405 /* Set IPv4 address actions. */
1408 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4, struct ofpbuf *out)
1410 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1415 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4, struct ofpbuf *out)
1417 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1422 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1423 enum ofp_version ofp_version,
1424 enum ofp_raw_action_type raw, enum mf_field_id field,
1427 ovs_be32 addr = ipv4->ipv4;
1428 if (ofp_version < OFP12_VERSION) {
1429 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1431 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1436 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1437 enum ofp_version ofp_version, struct ofpbuf *out)
1439 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1444 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1445 enum ofp_version ofp_version, struct ofpbuf *out)
1447 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1451 static char * WARN_UNUSED_RESULT
1452 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1453 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1455 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1458 static char * WARN_UNUSED_RESULT
1459 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1460 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1462 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1466 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1468 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1472 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1474 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1477 /* Set IPv4/v6 TOS actions. */
1480 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp, struct ofpbuf *out)
1482 if (dscp & ~IP_DSCP_MASK) {
1483 return OFPERR_OFPBAC_BAD_ARGUMENT;
1485 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1491 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1492 enum ofp_version ofp_version, struct ofpbuf *out)
1494 if (ofp_version < OFP12_VERSION) {
1495 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1497 ofpact_put_set_field(out, ofp_version,
1498 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1502 static char * WARN_UNUSED_RESULT
1503 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1504 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1509 error = str_to_u8(arg, "TOS", &tos);
1514 if (tos & ~IP_DSCP_MASK) {
1515 return xasprintf("%s: not a valid TOS", arg);
1517 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1522 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1524 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1527 /* Set IPv4/v6 ECN actions. */
1530 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn, struct ofpbuf *out)
1532 if (ecn & ~IP_ECN_MASK) {
1533 return OFPERR_OFPBAC_BAD_ARGUMENT;
1535 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1541 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1542 enum ofp_version ofp_version, struct ofpbuf *out)
1544 uint8_t ecn = ip_ecn->ecn;
1545 if (ofp_version == OFP10_VERSION) {
1547 } else if (ofp_version == OFP11_VERSION) {
1548 put_OFPAT11_SET_NW_ECN(out, ecn);
1550 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1554 static char * WARN_UNUSED_RESULT
1555 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1556 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1561 error = str_to_u8(arg, "ECN", &ecn);
1566 if (ecn & ~IP_ECN_MASK) {
1567 return xasprintf("%s: not a valid ECN", arg);
1569 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1574 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1576 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1579 /* Set IPv4/v6 TTL actions. */
1582 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl, struct ofpbuf *out)
1584 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1589 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1590 enum ofp_version ofp_version, struct ofpbuf *out)
1592 if (ofp_version >= OFP11_VERSION) {
1593 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1599 static char * WARN_UNUSED_RESULT
1600 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1601 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1606 error = str_to_u8(arg, "TTL", &ttl);
1611 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1616 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1618 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1621 /* Set TCP/UDP/SCTP port actions. */
1624 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port, struct ofpbuf *out)
1626 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1631 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port, struct ofpbuf *out)
1633 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1638 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1639 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1640 enum mf_field_id field, struct ofpbuf *out)
1642 uint16_t port = l4_port->port;
1644 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1645 ofpact_put_set_field(out, ofp_version, field, port);
1647 ofpact_put_raw(out, ofp_version, raw, port);
1652 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1653 enum ofp_version ofp_version, struct ofpbuf *out)
1655 uint8_t proto = l4_port->flow_ip_proto;
1656 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1657 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1658 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1661 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1665 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1666 enum ofp_version ofp_version,
1669 uint8_t proto = l4_port->flow_ip_proto;
1670 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1671 : proto == IPPROTO_UDP ? MFF_UDP_DST
1672 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1675 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1678 static char * WARN_UNUSED_RESULT
1679 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1680 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1682 return str_to_u16(arg, "source port",
1683 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1686 static char * WARN_UNUSED_RESULT
1687 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1688 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1690 return str_to_u16(arg, "destination port",
1691 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1695 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1697 ds_put_format(s, "mod_tp_src:%d", a->port);
1701 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1703 ds_put_format(s, "mod_tp_dst:%d", a->port);
1706 /* Action structure for OFPAT_COPY_FIELD. */
1707 struct ofp15_action_copy_field {
1708 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1709 ovs_be16 len; /* Length is padded to 64 bits. */
1710 ovs_be16 n_bits; /* Number of bits to copy. */
1711 ovs_be16 src_offset; /* Starting bit offset in source. */
1712 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1713 ovs_be16 oxm_id_len; /* Length of oxm_ids. */
1715 * - OXM header for source field.
1716 * - OXM header for destination field.
1717 * - Padding with 0-bytes to a multiple of 8 bytes.
1718 * The "pad" member is the beginning of the above. */
1721 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1723 /* Action structure for NXAST_REG_MOVE.
1725 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1726 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1727 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1728 * the next most significant bit, and so on.
1730 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1731 * sense to use nxm_hasmask=1 because the action does not do any kind of
1732 * matching; it uses the actual value of a field.)
1734 * The following nxm_header values are potentially acceptable as 'src':
1749 * - NXM_OF_ICMP_TYPE
1750 * - NXM_OF_ICMP_CODE
1757 * - NXM_NX_ICMPV6_TYPE
1758 * - NXM_NX_ICMPV6_CODE
1761 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1763 * - NXM_NX_TUN_IPV4_SRC
1764 * - NXM_NX_TUN_IPV4_DST
1766 * The following nxm_header values are potentially acceptable as 'dst':
1782 * Modifying any of the above fields changes the corresponding packet
1787 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1791 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1792 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1793 * header (if any), ignoring the other bits. Setting a value with CFI=1
1794 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1795 * to the field's new value (with the CFI bit masked out).
1797 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1798 * any of these values modifies the corresponding tunnel header field used
1799 * for the packet's next tunnel encapsulation, if allowed by the
1800 * configuration of the output tunnel port.
1802 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1803 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1804 * used only if the flow's nx_match includes an nxm_entry that specifies
1805 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1807 * The switch will reject actions for which src_ofs+n_bits is greater than the
1808 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1809 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1811 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1812 * behaves as if 'src' were copied out to a temporary buffer, then the
1813 * temporary buffer copied to 'dst'.
1815 struct nx_action_reg_move {
1816 ovs_be16 type; /* OFPAT_VENDOR. */
1817 ovs_be16 len; /* Length is 24. */
1818 ovs_be32 vendor; /* NX_VENDOR_ID. */
1819 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1820 ovs_be16 n_bits; /* Number of bits. */
1821 ovs_be16 src_ofs; /* Starting bit offset in source. */
1822 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1824 * - OXM/NXM header for source field (4 or 8 bytes).
1825 * - OXM/NXM header for destination field (4 or 8 bytes).
1826 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
1828 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
1831 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
1832 struct ofpbuf *ofpacts)
1834 struct ofpact_reg_move *move;
1839 move = ofpact_put_REG_MOVE(ofpacts);
1840 move->src.ofs = ntohs(oacf->src_offset);
1841 move->src.n_bits = ntohs(oacf->n_bits);
1842 move->dst.ofs = ntohs(oacf->dst_offset);
1843 move->dst.n_bits = ntohs(oacf->n_bits);
1845 ofpbuf_use_const(&b, oacf, ntohs(oacf->len));
1846 ofpbuf_pull(&b, offsetof(struct ofp15_action_copy_field, pad));
1847 orig_size = ofpbuf_size(&b);
1848 error = nx_pull_header(&b, &move->src.field, NULL);
1852 error = nx_pull_header(&b, &move->dst.field, NULL);
1856 if (orig_size - ofpbuf_size(&b) != ntohs(oacf->oxm_id_len)) {
1857 return OFPERR_OFPBAC_BAD_LEN;
1860 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
1861 return OFPERR_NXBRC_MUST_BE_ZERO;
1864 return nxm_reg_move_check(move, NULL);
1868 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
1869 struct ofpbuf *ofpacts)
1871 struct ofpact_reg_move *move;
1875 move = ofpact_put_REG_MOVE(ofpacts);
1876 move->src.ofs = ntohs(narm->src_ofs);
1877 move->src.n_bits = ntohs(narm->n_bits);
1878 move->dst.ofs = ntohs(narm->dst_ofs);
1879 move->dst.n_bits = ntohs(narm->n_bits);
1881 ofpbuf_use_const(&b, narm, ntohs(narm->len));
1882 ofpbuf_pull(&b, sizeof *narm);
1883 error = nx_pull_header(&b, &move->src.field, NULL);
1887 error = nx_pull_header(&b, &move->dst.field, NULL);
1891 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
1892 return OFPERR_NXBRC_MUST_BE_ZERO;
1895 return nxm_reg_move_check(move, NULL);
1899 encode_REG_MOVE(const struct ofpact_reg_move *move,
1900 enum ofp_version ofp_version, struct ofpbuf *out)
1902 size_t start_ofs = ofpbuf_size(out);
1903 if (ofp_version >= OFP15_VERSION) {
1904 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
1905 copy->n_bits = htons(move->dst.n_bits);
1906 copy->src_offset = htons(move->src.ofs);
1907 copy->dst_offset = htons(move->dst.ofs);
1908 copy->oxm_id_len = htons(8);
1909 ofpbuf_set_size(out, ofpbuf_size(out) - sizeof copy->pad);
1910 nx_put_header(out, move->src.field->id, ofp_version, false);
1911 nx_put_header(out, move->dst.field->id, ofp_version, false);
1913 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
1914 narm->n_bits = htons(move->dst.n_bits);
1915 narm->src_ofs = htons(move->src.ofs);
1916 narm->dst_ofs = htons(move->dst.ofs);
1917 nx_put_header(out, move->src.field->id, 0, false);
1918 nx_put_header(out, move->dst.field->id, 0, false);
1920 pad_ofpat(out, start_ofs);
1923 static char * WARN_UNUSED_RESULT
1924 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
1925 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1927 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
1928 const char *full_arg = arg;
1931 error = mf_parse_subfield__(&move->src, &arg);
1935 if (strncmp(arg, "->", 2)) {
1936 return xasprintf("%s: missing `->' following source", full_arg);
1939 error = mf_parse_subfield(&move->dst, arg);
1944 if (move->src.n_bits != move->dst.n_bits) {
1945 return xasprintf("%s: source field is %d bits wide but destination is "
1946 "%d bits wide", full_arg,
1947 move->src.n_bits, move->dst.n_bits);
1953 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
1955 nxm_format_reg_move(a, s);
1958 /* Action structure for OFPAT12_SET_FIELD. */
1959 struct ofp12_action_set_field {
1960 ovs_be16 type; /* OFPAT12_SET_FIELD. */
1961 ovs_be16 len; /* Length is padded to 64 bits. */
1964 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
1965 * - Enough 0-bytes to pad out to a multiple of 64 bits.
1967 * The "pad" member is the beginning of the above. */
1970 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
1972 /* Action structure for NXAST_REG_LOAD.
1974 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
1975 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
1976 * starts at 0 for the least-significant bit, 1 for the next most significant
1979 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
1980 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
1983 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
1984 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
1985 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
1986 * also stored as one less than its true value:
1989 * +------------------------------+------------------+
1990 * | ofs | n_bits - 1 |
1991 * +------------------------------+------------------+
1993 * The switch will reject actions for which ofs+n_bits is greater than the
1994 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
1995 * greater are set to 1, with error type OFPET_BAD_ACTION, code
1996 * OFPBAC_BAD_ARGUMENT.
1998 struct nx_action_reg_load {
1999 ovs_be16 type; /* OFPAT_VENDOR. */
2000 ovs_be16 len; /* Length is 24. */
2001 ovs_be32 vendor; /* NX_VENDOR_ID. */
2002 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2003 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2004 ovs_be32 dst; /* Destination register. */
2005 ovs_be64 value; /* Immediate value. */
2007 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2009 /* Action structure for NXAST_REG_LOAD2.
2011 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2012 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2013 * OXM experimenter fields. */
2014 struct nx_action_reg_load2 {
2015 ovs_be16 type; /* OFPAT_VENDOR. */
2016 ovs_be16 len; /* At least 16. */
2017 ovs_be32 vendor; /* NX_VENDOR_ID. */
2018 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2021 * - An NXM/OXM header, value, and optionally a mask.
2022 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2024 * The "pad" member is the beginning of the above. */
2027 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2030 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2031 bool may_mask, struct ofpbuf *ofpacts)
2033 struct ofpact_set_field *sf;
2037 sf = ofpact_put_SET_FIELD(ofpacts);
2039 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2040 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2041 error = nx_pull_entry(&b, &sf->field, &sf->value,
2042 may_mask ? &sf->mask : NULL);
2044 return (error == OFPERR_OFPBMC_BAD_MASK
2045 ? OFPERR_OFPBAC_BAD_SET_MASK
2049 memset(&sf->mask, 0xff, sf->field->n_bytes);
2052 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
2053 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2056 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2058 if (sf->field->id == MFF_IN_PORT_OXM) {
2059 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2062 /* oxm_length is now validated to be compatible with mf_value. */
2063 if (!sf->field->writable) {
2064 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2066 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2069 /* The value must be valid for match. The OpenFlow 1.5 draft also says,
2070 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2071 * a 1-bit in oxm_value and in oxm_mask." */
2072 if (!mf_is_value_valid(sf->field, &sf->value)
2073 || (sf->field->id == MFF_VLAN_VID
2074 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2075 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2076 struct ds ds = DS_EMPTY_INITIALIZER;
2077 mf_format(sf->field, &sf->value, NULL, &ds);
2078 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2079 sf->field->name, ds_cstr(&ds));
2082 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2088 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2089 struct ofpbuf *ofpacts)
2091 return decode_ofpat_set_field(oasf, false, ofpacts);
2095 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2096 struct ofpbuf *ofpacts)
2098 return decode_ofpat_set_field(oasf, true, ofpacts);
2102 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2105 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2106 struct mf_subfield dst;
2109 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2111 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2112 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2113 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2114 error = mf_check_dst(&dst, NULL);
2119 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2121 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2122 return OFPERR_OFPBAC_BAD_ARGUMENT;
2125 sf->field = dst.field;
2126 bitwise_put(ntohll(narl->value),
2127 &sf->value, dst.field->n_bytes, dst.ofs,
2129 bitwise_put(UINT64_MAX,
2130 &sf->mask, dst.field->n_bytes, dst.ofs,
2137 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2140 struct ofpact_set_field *sf;
2144 sf = ofpact_put_SET_FIELD(out);
2145 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2147 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2148 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2149 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2153 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
2154 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2157 if (!sf->field->writable) {
2158 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2160 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2166 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2167 enum mf_field_id field, uint64_t value_)
2169 struct ofp12_action_set_field *oasf OVS_UNUSED;
2170 int n_bytes = mf_from_id(field)->n_bytes;
2171 size_t start_ofs = ofpbuf_size(openflow);
2172 union mf_value value;
2174 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2176 oasf = put_OFPAT12_SET_FIELD(openflow);
2177 ofpbuf_set_size(openflow, ofpbuf_size(openflow) - sizeof oasf->pad);
2178 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2179 pad_ofpat(openflow, start_ofs);
2183 next_load_segment(const struct ofpact_set_field *sf,
2184 struct mf_subfield *dst, uint64_t *value)
2186 int w = sf->field->n_bytes;
2187 int start = dst->ofs + dst->n_bits;
2189 if (start < 8 * w) {
2190 dst->field = sf->field;
2191 dst->ofs = bitwise_scan(&sf->mask, w, 1, start, 8 * w);
2192 if (dst->ofs < 8 * w) {
2193 dst->n_bits = bitwise_scan(&sf->mask, w, 0, dst->ofs + 1,
2194 MIN(dst->ofs + 64, 8 * w)) - dst->ofs;
2195 *value = bitwise_get(&sf->value, w, dst->ofs, dst->n_bits);
2202 /* Convert 'sf' to a series of REG_LOADs. */
2204 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2206 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2207 * experimenter OXM (or if it came in as NXAST_REG_LOAD2), encode as
2208 * NXAST_REG_LOAD2. Otherwise use NXAST_REG_LOAD, which is backward
2210 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2211 || !mf_nxm_header(sf->field->id)) {
2212 struct nx_action_reg_load2 *narl OVS_UNUSED;
2213 size_t start_ofs = ofpbuf_size(openflow);
2215 narl = put_NXAST_REG_LOAD2(openflow);
2216 ofpbuf_set_size(openflow, ofpbuf_size(openflow) - sizeof narl->pad);
2217 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2218 pad_ofpat(openflow, start_ofs);
2220 struct mf_subfield dst;
2223 dst.ofs = dst.n_bits = 0;
2224 while (next_load_segment(sf, &dst, &value)) {
2225 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2226 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2227 narl->dst = htonl(mf_nxm_header(dst.field->id));
2228 narl->value = htonll(value);
2233 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2234 * actions, if we can, falling back to Nicira extensions if we must.
2236 * We check only meta-flow types that can appear within set field actions and
2237 * that have a mapping to compatible action types. These struct mf_field
2238 * definitions have a defined OXM or NXM header value and specify the field as
2241 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2242 enum ofp_version ofp_version,
2245 switch ((int) sf->field->id) {
2246 case MFF_VLAN_TCI: {
2247 ovs_be16 tci = sf->value.be16;
2248 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2249 uint16_t vid = vlan_tci_to_vid(tci);
2250 uint8_t pcp = vlan_tci_to_pcp(tci);
2252 if (ofp_version < OFP11_VERSION) {
2253 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2255 * If CFI=1, Add or modify VLAN VID & PCP.
2256 * If CFI=0, strip VLAN header, if any.
2259 put_OFPAT10_SET_VLAN_VID(out, vid);
2260 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2262 put_OFPAT10_STRIP_VLAN(out);
2265 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2267 * If CFI=1, Add or modify VLAN VID & PCP.
2268 * OpenFlow 1.1 set actions only apply if the packet
2269 * already has VLAN tags. To be sure that is the case
2270 * we have to push a VLAN header. As we do not support
2271 * multiple layers of VLANs, this is a no-op, if a VLAN
2272 * header already exists. This may backfire, however,
2273 * when we start supporting multiple layers of VLANs.
2274 * If CFI=0, strip VLAN header, if any.
2277 /* Push a VLAN tag, if one was not seen at action validation
2279 if (!sf->flow_has_vlan) {
2280 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2282 put_OFPAT11_SET_VLAN_VID(out, vid);
2283 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2285 /* If the flow did not match on vlan, we have no way of
2286 * knowing if the vlan tag exists, so we must POP just to be
2288 put_OFPAT11_POP_VLAN(out);
2294 case MFF_VLAN_VID: {
2295 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2296 if (ofp_version == OFP10_VERSION) {
2297 put_OFPAT10_SET_VLAN_VID(out, vid);
2299 put_OFPAT11_SET_VLAN_VID(out, vid);
2305 if (ofp_version == OFP10_VERSION) {
2306 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2308 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2313 memcpy(put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr,
2314 sf->value.mac, ETH_ADDR_LEN);
2318 memcpy(put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr,
2319 sf->value.mac, ETH_ADDR_LEN);
2323 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2327 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2331 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2334 case MFF_IP_DSCP_SHIFTED:
2335 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2340 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2345 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2349 set_field_to_nxast(sf, out);
2355 set_field_to_set_field(const struct ofpact_set_field *sf,
2356 enum ofp_version ofp_version, struct ofpbuf *out)
2358 struct ofp12_action_set_field *oasf OVS_UNUSED;
2359 size_t start_ofs = ofpbuf_size(out);
2361 oasf = put_OFPAT12_SET_FIELD(out);
2362 ofpbuf_set_size(out, ofpbuf_size(out) - sizeof oasf->pad);
2363 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2364 pad_ofpat(out, start_ofs);
2368 encode_SET_FIELD(const struct ofpact_set_field *sf,
2369 enum ofp_version ofp_version, struct ofpbuf *out)
2371 if (ofp_version >= OFP15_VERSION) {
2372 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2374 set_field_to_set_field(sf, ofp_version, out);
2375 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2376 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2377 /* It came in as reg_load, send it out the same way. */
2378 set_field_to_nxast(sf, out);
2379 } else if (ofp_version < OFP12_VERSION) {
2380 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2381 set_field_to_legacy_openflow(sf, ofp_version, out);
2382 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2383 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2384 * entire field, so encode it as OFPAT_SET_FIELD. */
2385 set_field_to_set_field(sf, ofp_version, out);
2387 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2388 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2389 * so encode it as reg_load. */
2390 set_field_to_nxast(sf, out);
2394 /* Parses a "set_field" action with argument 'arg', appending the parsed
2395 * action to 'ofpacts'.
2397 * Returns NULL if successful, otherwise a malloc()'d string describing the
2398 * error. The caller is responsible for freeing the returned string. */
2399 static char * WARN_UNUSED_RESULT
2400 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2401 enum ofputil_protocol *usable_protocols)
2403 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2407 const struct mf_field *mf;
2411 delim = strstr(arg, "->");
2413 return xasprintf("%s: missing `->'", arg);
2415 if (strlen(delim) <= strlen("->")) {
2416 return xasprintf("%s: missing field name following `->'", arg);
2419 key = delim + strlen("->");
2420 mf = mf_from_name(key);
2422 return xasprintf("%s is not a valid OXM field name", key);
2424 if (!mf->writable) {
2425 return xasprintf("%s is read-only", key);
2429 error = mf_parse(mf, value, &sf->value, &sf->mask);
2434 if (!mf_is_value_valid(mf, &sf->value)) {
2435 return xasprintf("%s is not a valid value for field %s", value, key);
2438 *usable_protocols &= mf->usable_protocols_exact;
2442 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2443 * action to 'ofpacts'.
2445 * Returns NULL if successful, otherwise a malloc()'d string describing the
2446 * error. The caller is responsible for freeing the returned string. */
2447 static char * WARN_UNUSED_RESULT
2448 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2449 enum ofputil_protocol *usable_protocols)
2451 char *copy = xstrdup(arg);
2452 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2457 static char * WARN_UNUSED_RESULT
2458 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2460 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2461 const char *full_arg = arg;
2462 uint64_t value = strtoull(arg, (char **) &arg, 0);
2463 struct mf_subfield dst;
2466 if (strncmp(arg, "->", 2)) {
2467 return xasprintf("%s: missing `->' following value", full_arg);
2470 error = mf_parse_subfield(&dst, arg);
2475 if (dst.n_bits < 64 && (value >> dst.n_bits) != 0) {
2476 return xasprintf("%s: value %"PRIu64" does not fit into %d bits",
2477 full_arg, value, dst.n_bits);
2480 sf->field = dst.field;
2481 memset(&sf->value, 0, sizeof sf->value);
2482 bitwise_put(value, &sf->value, dst.field->n_bytes, dst.ofs, dst.n_bits);
2483 bitwise_put(UINT64_MAX, &sf->mask,
2484 dst.field->n_bytes, dst.ofs, dst.n_bits);
2489 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2491 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2492 struct mf_subfield dst;
2495 dst.ofs = dst.n_bits = 0;
2496 while (next_load_segment(a, &dst, &value)) {
2497 ds_put_format(s, "load:%#"PRIx64"->", value);
2498 mf_format_subfield(&dst, s);
2499 ds_put_char(s, ',');
2503 ds_put_cstr(s, "set_field:");
2504 mf_format(a->field, &a->value, &a->mask, s);
2505 ds_put_format(s, "->%s", a->field->name);
2509 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2510 * is marked such that, if possible, it will be translated to OpenFlow as
2511 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2512 * that was the way that the action was expressed when it came into OVS or for
2513 * backward compatibility. */
2514 struct ofpact_set_field *
2515 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2517 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2518 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2522 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2524 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2527 struct nx_action_stack {
2528 ovs_be16 type; /* OFPAT_VENDOR. */
2529 ovs_be16 len; /* Length is 16. */
2530 ovs_be32 vendor; /* NX_VENDOR_ID. */
2531 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2532 ovs_be16 offset; /* Bit offset into the field. */
2534 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2535 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2536 * - Enough 0-bytes to pad out the action to 24 bytes. */
2537 uint8_t pad[12]; /* See above. */
2539 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2542 decode_stack_action(const struct nx_action_stack *nasp,
2543 struct ofpact_stack *stack_action)
2548 stack_action->subfield.ofs = ntohs(nasp->offset);
2550 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2551 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2552 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2556 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) ofpbuf_data(&b));
2558 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
2559 return OFPERR_NXBRC_MUST_BE_ZERO;
2566 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2567 struct ofpbuf *ofpacts)
2569 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2570 enum ofperr error = decode_stack_action(nasp, push);
2571 return error ? error : nxm_stack_push_check(push, NULL);
2575 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2576 struct ofpbuf *ofpacts)
2578 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2579 enum ofperr error = decode_stack_action(nasp, pop);
2580 return error ? error : nxm_stack_pop_check(pop, NULL);
2584 encode_STACK_op(const struct ofpact_stack *stack_action,
2585 struct nx_action_stack *nasp)
2590 nasp->offset = htons(stack_action->subfield.ofs);
2592 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2593 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2594 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2595 n_bits = htons(stack_action->subfield.n_bits);
2596 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2600 encode_STACK_PUSH(const struct ofpact_stack *stack,
2601 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2603 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2607 encode_STACK_POP(const struct ofpact_stack *stack,
2608 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2610 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2613 static char * WARN_UNUSED_RESULT
2614 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2615 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2617 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2620 static char * WARN_UNUSED_RESULT
2621 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2622 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2624 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2628 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2630 nxm_format_stack_push(a, s);
2634 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2636 nxm_format_stack_pop(a, s);
2639 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2641 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2642 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2643 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2644 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2646 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2647 * sent only to controllers with id 0.)
2649 struct nx_action_cnt_ids {
2650 ovs_be16 type; /* OFPAT_VENDOR. */
2651 ovs_be16 len; /* Length including slaves. */
2652 ovs_be32 vendor; /* NX_VENDOR_ID. */
2653 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2655 ovs_be16 n_controllers; /* Number of controllers. */
2656 uint8_t zeros[4]; /* Must be zero. */
2658 /* Followed by 1 or more controller ids.
2660 * uint16_t cnt_ids[]; // Controller ids.
2661 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2664 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2667 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2670 struct ofpact_cnt_ids *ids;
2671 enum ofperr error = 0;
2673 ids = ofpact_put_DEC_TTL(out);
2674 ids->n_controllers = 1;
2675 ofpbuf_put(out, &id, sizeof id);
2677 ofpact_update_len(out, &ids->ofpact);
2682 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2685 struct ofpact_cnt_ids *ids;
2689 ids = ofpact_put_DEC_TTL(out);
2690 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2691 ids->n_controllers = ntohs(nac_ids->n_controllers);
2692 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2694 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2695 return OFPERR_NXBRC_MUST_BE_ZERO;
2698 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2699 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2700 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2701 "are required for %"PRIu16" controllers.",
2702 ids_size, ids->n_controllers * sizeof(ovs_be16),
2703 ids->n_controllers);
2704 return OFPERR_OFPBAC_BAD_LEN;
2707 for (i = 0; i < ids->n_controllers; i++) {
2708 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2709 ofpbuf_put(out, &id, sizeof id);
2713 ofpact_update_len(out, &ids->ofpact);
2719 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2720 enum ofp_version ofp_version, struct ofpbuf *out)
2722 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2723 || dec_ttl->n_controllers != 1
2724 || dec_ttl->cnt_ids[0] != 0) {
2725 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2726 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2730 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2731 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2733 ids = ofpbuf_put_zeros(out, ids_len);
2734 for (i = 0; i < dec_ttl->n_controllers; i++) {
2735 ids[i] = htons(dec_ttl->cnt_ids[i]);
2738 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2743 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2745 struct ofpact_cnt_ids *ids;
2748 ofpact_put_DEC_TTL(ofpacts);
2749 ofpbuf_put(ofpacts, &id, sizeof id);
2750 ids = ofpacts->frame;
2751 ids->n_controllers++;
2752 ofpact_update_len(ofpacts, &ids->ofpact);
2755 static char * WARN_UNUSED_RESULT
2756 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2757 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2760 parse_noargs_dec_ttl(ofpacts);
2762 struct ofpact_cnt_ids *ids;
2765 ids = ofpact_put_DEC_TTL(ofpacts);
2766 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2767 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2768 cntr = strtok_r(NULL, ", ", &arg)) {
2769 uint16_t id = atoi(cntr);
2771 ofpbuf_put(ofpacts, &id, sizeof id);
2772 ids = ofpacts->frame;
2773 ids->n_controllers++;
2775 if (!ids->n_controllers) {
2776 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2779 ofpact_update_len(ofpacts, &ids->ofpact);
2785 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2789 ds_put_cstr(s, "dec_ttl");
2790 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2791 ds_put_cstr(s, "(");
2792 for (i = 0; i < a->n_controllers; i++) {
2794 ds_put_cstr(s, ",");
2796 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2798 ds_put_cstr(s, ")");
2802 /* Set MPLS label actions. */
2805 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label, struct ofpbuf *out)
2807 ofpact_put_SET_MPLS_LABEL(out)->label = label;
2812 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
2813 enum ofp_version ofp_version,
2816 if (ofp_version < OFP12_VERSION) {
2817 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
2819 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
2820 ntohl(label->label));
2824 static char * WARN_UNUSED_RESULT
2825 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
2826 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2828 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
2830 return xstrdup("set_mpls_label: expected label.");
2833 mpls_label->label = htonl(atoi(arg));
2838 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
2840 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
2843 /* Set MPLS TC actions. */
2846 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc, struct ofpbuf *out)
2848 ofpact_put_SET_MPLS_TC(out)->tc = tc;
2853 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
2854 enum ofp_version ofp_version, struct ofpbuf *out)
2856 if (ofp_version < OFP12_VERSION) {
2857 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
2859 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
2863 static char * WARN_UNUSED_RESULT
2864 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
2865 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2867 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
2870 return xstrdup("set_mpls_tc: expected tc.");
2873 mpls_tc->tc = atoi(arg);
2878 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
2880 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
2883 /* Set MPLS TTL actions. */
2886 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl, struct ofpbuf *out)
2888 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
2893 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
2894 enum ofp_version ofp_version, struct ofpbuf *out)
2896 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
2899 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
2900 * action to 'ofpacts'.
2902 * Returns NULL if successful, otherwise a malloc()'d string describing the
2903 * error. The caller is responsible for freeing the returned string. */
2904 static char * WARN_UNUSED_RESULT
2905 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
2906 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2908 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
2911 return xstrdup("set_mpls_ttl: expected ttl.");
2914 mpls_ttl->ttl = atoi(arg);
2919 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
2921 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
2924 /* Decrement MPLS TTL actions. */
2927 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
2929 ofpact_put_DEC_MPLS_TTL(out);
2934 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
2935 enum ofp_version ofp_version, struct ofpbuf *out)
2937 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
2940 static char * WARN_UNUSED_RESULT
2941 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
2942 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2944 ofpact_put_DEC_MPLS_TTL(ofpacts);
2949 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
2951 ds_put_cstr(s, "dec_mpls_ttl");
2954 /* Push MPLS label action. */
2957 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
2959 struct ofpact_push_mpls *oam;
2961 if (!eth_type_mpls(ethertype)) {
2962 return OFPERR_OFPBAC_BAD_ARGUMENT;
2964 oam = ofpact_put_PUSH_MPLS(out);
2965 oam->ethertype = ethertype;
2971 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
2972 enum ofp_version ofp_version, struct ofpbuf *out)
2974 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
2977 static char * WARN_UNUSED_RESULT
2978 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
2979 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2984 error = str_to_u16(arg, "push_mpls", ðertype);
2986 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
2992 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
2994 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
2997 /* Pop MPLS label action. */
3000 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
3002 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3007 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3008 enum ofp_version ofp_version, struct ofpbuf *out)
3010 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3013 static char * WARN_UNUSED_RESULT
3014 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3015 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3020 error = str_to_u16(arg, "pop_mpls", ðertype);
3022 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3028 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3030 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3033 /* Set tunnel ID actions. */
3036 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id, struct ofpbuf *out)
3038 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3039 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3040 tunnel->tun_id = tun_id;
3045 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id, struct ofpbuf *out)
3047 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3048 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3049 tunnel->tun_id = tun_id;
3054 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3055 enum ofp_version ofp_version, struct ofpbuf *out)
3057 uint64_t tun_id = tunnel->tun_id;
3059 if (ofp_version < OFP12_VERSION) {
3060 if (tun_id <= UINT32_MAX
3061 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3062 put_NXAST_SET_TUNNEL(out, tun_id);
3064 put_NXAST_SET_TUNNEL64(out, tun_id);
3067 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3071 static char * WARN_UNUSED_RESULT
3072 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3073 enum ofp_raw_action_type raw)
3075 struct ofpact_tunnel *tunnel;
3077 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3078 tunnel->ofpact.raw = raw;
3079 return str_to_u64(arg, &tunnel->tun_id);
3082 static char * WARN_UNUSED_RESULT
3083 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3084 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3086 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3090 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3092 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3093 (a->tun_id > UINT32_MAX
3094 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3098 /* Set queue action. */
3101 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id, struct ofpbuf *out)
3103 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3108 encode_SET_QUEUE(const struct ofpact_queue *queue,
3109 enum ofp_version ofp_version, struct ofpbuf *out)
3111 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3114 static char * WARN_UNUSED_RESULT
3115 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3116 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3118 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3122 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3124 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3127 /* Pop queue action. */
3130 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3132 ofpact_put_POP_QUEUE(out);
3137 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3138 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3140 put_NXAST_POP_QUEUE(out);
3143 static char * WARN_UNUSED_RESULT
3144 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3145 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3147 ofpact_put_POP_QUEUE(ofpacts);
3152 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3154 ds_put_cstr(s, "pop_queue");
3157 /* Action structure for NXAST_FIN_TIMEOUT.
3159 * This action changes the idle timeout or hard timeout, or both, of this
3160 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3161 * When such a packet is observed, the action reduces the rule's idle timeout
3162 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3163 * action has no effect on an existing timeout that is already shorter than the
3164 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3165 * of zero has no effect on the respective timeout.
3167 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3168 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3169 * receipt of the FIN or RST.
3171 * This is useful for quickly discarding learned TCP flows that otherwise will
3172 * take a long time to expire.
3174 * This action is intended for use with an OpenFlow rule that matches only a
3175 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3176 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3177 * RST in any of those flows will cause the entire OpenFlow rule to expire
3178 * early, which is not normally desirable.
3180 struct nx_action_fin_timeout {
3181 ovs_be16 type; /* OFPAT_VENDOR. */
3182 ovs_be16 len; /* 16. */
3183 ovs_be32 vendor; /* NX_VENDOR_ID. */
3184 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3185 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3186 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3187 ovs_be16 pad; /* Must be zero. */
3189 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3192 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3195 struct ofpact_fin_timeout *oft;
3197 oft = ofpact_put_FIN_TIMEOUT(out);
3198 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3199 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3204 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3205 enum ofp_version ofp_version OVS_UNUSED,
3208 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3209 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3210 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3213 static char * WARN_UNUSED_RESULT
3214 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3215 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3217 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3220 while (ofputil_parse_key_value(&arg, &key, &value)) {
3223 if (!strcmp(key, "idle_timeout")) {
3224 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3225 } else if (!strcmp(key, "hard_timeout")) {
3226 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3228 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3240 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3242 ds_put_cstr(s, "fin_timeout(");
3243 if (a->fin_idle_timeout) {
3244 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3246 if (a->fin_hard_timeout) {
3247 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3250 ds_put_char(s, ')');
3253 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3255 * These actions search one of the switch's flow tables:
3257 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3258 * it specifies the table to search.
3260 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3261 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3262 * table, that is, the OpenFlow flow table that contains the flow from
3263 * which this action was obtained. If this action did not come from a
3264 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3265 * is the current table.
3267 * The flow table lookup uses a flow that may be slightly modified from the
3270 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3271 * is used as the flow's in_port.
3273 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3274 * then its value is used as the flow's in_port. Otherwise, the original
3277 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3278 * resubmit action, then the flow is updated with the new values.
3280 * Following the lookup, the original in_port is restored.
3282 * If the modified flow matched in the flow table, then the corresponding
3283 * actions are executed. Afterward, actions following the resubmit in the
3284 * original set of actions, if any, are executed; any changes made to the
3285 * packet (e.g. changes to VLAN) by secondary actions persist when those
3286 * actions are executed, although the original in_port is restored.
3288 * Resubmit actions may be used any number of times within a set of actions.
3290 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3291 * implementation-defined depth, further resubmit actions are simply ignored.
3293 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3294 * 'pad' to be all-bits-zero.
3296 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3297 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3299 struct nx_action_resubmit {
3300 ovs_be16 type; /* OFPAT_VENDOR. */
3301 ovs_be16 len; /* Length is 16. */
3302 ovs_be32 vendor; /* NX_VENDOR_ID. */
3303 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3304 ovs_be16 in_port; /* New in_port for checking flow table. */
3305 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3308 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3311 decode_NXAST_RAW_RESUBMIT(uint16_t port, struct ofpbuf *out)
3313 struct ofpact_resubmit *resubmit;
3315 resubmit = ofpact_put_RESUBMIT(out);
3316 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3317 resubmit->in_port = u16_to_ofp(port);
3318 resubmit->table_id = 0xff;
3323 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3326 struct ofpact_resubmit *resubmit;
3328 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3329 return OFPERR_OFPBAC_BAD_ARGUMENT;
3332 resubmit = ofpact_put_RESUBMIT(out);
3333 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3334 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3335 resubmit->table_id = nar->table;
3340 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3341 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3343 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3345 if (resubmit->table_id == 0xff
3346 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3347 put_NXAST_RESUBMIT(out, in_port);
3349 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3350 nar->table = resubmit->table_id;
3351 nar->in_port = htons(in_port);
3355 static char * WARN_UNUSED_RESULT
3356 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3357 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3359 struct ofpact_resubmit *resubmit;
3360 char *in_port_s, *table_s;
3362 resubmit = ofpact_put_RESUBMIT(ofpacts);
3364 in_port_s = strsep(&arg, ",");
3365 if (in_port_s && in_port_s[0]) {
3366 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3367 return xasprintf("%s: resubmit to unknown port", in_port_s);
3370 resubmit->in_port = OFPP_IN_PORT;
3373 table_s = strsep(&arg, ",");
3374 if (table_s && table_s[0]) {
3375 uint32_t table_id = 0;
3378 error = str_to_u32(table_s, &table_id);
3382 resubmit->table_id = table_id;
3384 resubmit->table_id = 255;
3387 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3388 return xstrdup("at least one \"in_port\" or \"table\" must be "
3389 "specified on resubmit");
3395 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3397 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3398 ds_put_cstr(s, "resubmit:");
3399 ofputil_format_port(a->in_port, s);
3401 ds_put_format(s, "resubmit(");
3402 if (a->in_port != OFPP_IN_PORT) {
3403 ofputil_format_port(a->in_port, s);
3405 ds_put_char(s, ',');
3406 if (a->table_id != 255) {
3407 ds_put_format(s, "%"PRIu8, a->table_id);
3409 ds_put_char(s, ')');
3413 /* Action structure for NXAST_LEARN.
3415 * This action adds or modifies a flow in an OpenFlow table, similar to
3416 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3417 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3418 * flow's match criteria and actions are built by applying each of the series
3419 * of flow_mod_spec elements included as part of the action.
3421 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3422 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3423 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3424 * bits from a source to a destination. In this case, the header contains
3427 * 15 14 13 12 11 10 0
3428 * +------+---+------+---------------------------------+
3429 * | 0 |src| dst | n_bits |
3430 * +------+---+------+---------------------------------+
3432 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3433 * following table summarizes the meaning of each possible combination.
3434 * Details follow the table:
3437 * --- --- ----------------------------------------------------------
3438 * 0 0 Add match criteria based on value in a field.
3439 * 1 0 Add match criteria based on an immediate value.
3440 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3441 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3442 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3443 * All other combinations are undefined and not allowed.
3445 * The flow_mod_spec header is followed by a source specification and a
3446 * destination specification. The format and meaning of the source
3447 * specification depends on 'src':
3449 * - If 'src' is 0, the source bits are taken from a field in the flow to
3450 * which this action is attached. (This should be a wildcarded field. If
3451 * its value is fully specified then the source bits being copied have
3454 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3455 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3456 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3457 * 'field' and 'ofs' are subject to the same restrictions as the source
3458 * field in NXAST_REG_MOVE.
3460 * - If 'src' is 1, the source bits are a constant value. The source
3461 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3462 * number in network order, the source bits are the 'n_bits'
3463 * least-significant bits. The switch will report an error if other bits
3464 * in the constant are nonzero.
3466 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3467 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3468 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3469 * meaning of the flow_mod_spec depends on 'dst':
3471 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3472 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3473 * packet equal the source bits. 'field' may be any nxm_header with
3474 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3476 * Order is significant. Earlier flow_mod_specs must satisfy any
3477 * prerequisites for matching fields specified later, by copying constant
3478 * values into prerequisite fields.
3480 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3483 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3484 * the new flow. The new flow copies the source bits into
3485 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3488 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3489 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3491 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3492 * empty. It has the following meaning:
3494 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3495 * The new flow outputs to the OpenFlow port specified by the source field.
3496 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3497 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3500 * Resource Management
3501 * -------------------
3503 * A switch has a finite amount of flow table space available for learning.
3504 * When this space is exhausted, no new learning table entries will be learned
3505 * until some existing flow table entries expire. The controller should be
3506 * prepared to handle this by flooding (which can be implemented as a
3507 * low-priority flow).
3509 * If a learned flow matches a single TCP stream with a relatively long
3510 * timeout, one may make the best of resource constraints by setting
3511 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3512 * both, to shorter timeouts. When either of these is specified as a nonzero
3513 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3519 * The following examples give a prose description of the flow_mod_specs along
3520 * with informal notation for how those would be represented and a hex dump of
3521 * the bytes that would be required.
3523 * These examples could work with various nx_action_learn parameters. Typical
3524 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3525 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3527 * 1. Learn input port based on the source MAC, with lookup into
3528 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3530 * Match on in_port=99:
3531 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3532 * ovs_be16(99), 00 63
3533 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3535 * Match Ethernet destination on Ethernet source from packet:
3536 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3537 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3538 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3540 * Set NXM_NX_REG1[16:31] to the packet's input port:
3541 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3542 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3543 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3545 * Given a packet that arrived on port A with Ethernet source address B,
3546 * this would set up the flow "in_port=99, dl_dst=B,
3547 * actions=load:A->NXM_NX_REG1[16..31]".
3549 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3550 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3551 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3553 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3554 * into NXM_NX_REG1[16:31]:
3556 * Match on same VLAN ID as packet:
3557 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3558 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3559 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3561 * Match Ethernet destination on Ethernet source from packet:
3562 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3563 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3564 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3566 * Output to the packet's input port:
3567 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3568 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3570 * Given a packet that arrived on port A with Ethernet source address B in
3571 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3572 * actions=output:A".
3574 * In syntax accepted by ovs-ofctl, this action is:
3575 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3576 * output:NXM_OF_IN_PORT[])
3578 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3579 * 10-second MAC expiration time to make it easier to see what's going on
3581 * ovs-vsctl del-controller br0
3582 * ovs-ofctl del-flows br0
3583 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3584 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3585 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3586 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3587 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3589 * You can then dump the MAC learning table with:
3591 * ovs-ofctl dump-flows br0 table=1
3596 * For best performance, segregate learned flows into a table that is not used
3597 * for any other flows except possibly for a lowest-priority "catch-all" flow
3598 * (a flow with no match criteria). If different learning actions specify
3599 * different match criteria, use different tables for the learned flows.
3601 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3602 * These timeouts apply to the flow that is added, which means that a flow with
3603 * an idle timeout will expire when no traffic has been sent *to* the learned
3604 * address. This is not usually the intent in MAC learning; instead, we want
3605 * the MAC learn entry to expire when no traffic has been sent *from* the
3606 * learned address. Use a hard timeout for that.
3608 struct nx_action_learn {
3609 ovs_be16 type; /* OFPAT_VENDOR. */
3610 ovs_be16 len; /* At least 24. */
3611 ovs_be32 vendor; /* NX_VENDOR_ID. */
3612 ovs_be16 subtype; /* NXAST_LEARN. */
3613 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3614 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3615 ovs_be16 priority; /* Priority level of flow entry. */
3616 ovs_be64 cookie; /* Cookie for new flow. */
3617 ovs_be16 flags; /* NX_LEARN_F_*. */
3618 uint8_t table_id; /* Table to insert flow entry. */
3619 uint8_t pad; /* Must be zero. */
3620 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3621 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3622 /* Followed by a sequence of flow_mod_spec elements, as described above,
3623 * until the end of the action is reached. */
3625 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3628 get_be16(const void **pp)
3630 const ovs_be16 *p = *pp;
3631 ovs_be16 value = *p;
3637 get_be32(const void **pp)
3639 const ovs_be32 *p = *pp;
3640 ovs_be32 value = get_unaligned_be32(p);
3646 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3648 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3649 sf->ofs = ntohs(get_be16(p));
3650 sf->n_bits = n_bits;
3654 learn_min_len(uint16_t header)
3656 int n_bits = header & NX_LEARN_N_BITS_MASK;
3657 int src_type = header & NX_LEARN_SRC_MASK;
3658 int dst_type = header & NX_LEARN_DST_MASK;
3659 unsigned int min_len;
3662 if (src_type == NX_LEARN_SRC_FIELD) {
3663 min_len += sizeof(ovs_be32); /* src_field */
3664 min_len += sizeof(ovs_be16); /* src_ofs */
3666 min_len += DIV_ROUND_UP(n_bits, 16);
3668 if (dst_type == NX_LEARN_DST_MATCH ||
3669 dst_type == NX_LEARN_DST_LOAD) {
3670 min_len += sizeof(ovs_be32); /* dst_field */
3671 min_len += sizeof(ovs_be16); /* dst_ofs */
3676 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3677 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3679 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3680 struct ofpbuf *ofpacts)
3682 struct ofpact_learn *learn;
3683 const void *p, *end;
3686 return OFPERR_OFPBAC_BAD_ARGUMENT;
3689 learn = ofpact_put_LEARN(ofpacts);
3691 learn->idle_timeout = ntohs(nal->idle_timeout);
3692 learn->hard_timeout = ntohs(nal->hard_timeout);
3693 learn->priority = ntohs(nal->priority);
3694 learn->cookie = nal->cookie;
3695 learn->table_id = nal->table_id;
3696 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3697 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3699 learn->flags = ntohs(nal->flags);
3700 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3701 NX_LEARN_F_DELETE_LEARNED)) {
3702 return OFPERR_OFPBAC_BAD_ARGUMENT;
3705 if (learn->table_id == 0xff) {
3706 return OFPERR_OFPBAC_BAD_ARGUMENT;
3709 end = (char *) nal + ntohs(nal->len);
3710 for (p = nal + 1; p != end; ) {
3711 struct ofpact_learn_spec *spec;
3712 uint16_t header = ntohs(get_be16(&p));
3718 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3719 learn = ofpacts->frame;
3722 spec->src_type = header & NX_LEARN_SRC_MASK;
3723 spec->dst_type = header & NX_LEARN_DST_MASK;
3724 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3726 /* Check for valid src and dst type combination. */
3727 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3728 spec->dst_type == NX_LEARN_DST_LOAD ||
3729 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3730 spec->src_type == NX_LEARN_SRC_FIELD)) {
3733 return OFPERR_OFPBAC_BAD_ARGUMENT;
3736 /* Check that the arguments don't overrun the end of the action. */
3737 if ((char *) end - (char *) p < learn_min_len(header)) {
3738 return OFPERR_OFPBAC_BAD_LEN;
3741 /* Get the source. */
3742 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3743 get_subfield(spec->n_bits, &p, &spec->src);
3745 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3747 bitwise_copy(p, p_bytes, 0,
3748 &spec->src_imm, sizeof spec->src_imm, 0,
3750 p = (const uint8_t *) p + p_bytes;
3753 /* Get the destination. */
3754 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3755 spec->dst_type == NX_LEARN_DST_LOAD) {
3756 get_subfield(spec->n_bits, &p, &spec->dst);
3759 ofpact_update_len(ofpacts, &learn->ofpact);
3761 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3762 return OFPERR_OFPBAC_BAD_ARGUMENT;
3769 put_be16(struct ofpbuf *b, ovs_be16 x)
3771 ofpbuf_put(b, &x, sizeof x);
3775 put_be32(struct ofpbuf *b, ovs_be32 x)
3777 ofpbuf_put(b, &x, sizeof x);
3781 put_u16(struct ofpbuf *b, uint16_t x)
3783 put_be16(b, htons(x));
3787 put_u32(struct ofpbuf *b, uint32_t x)
3789 put_be32(b, htonl(x));
3793 encode_LEARN(const struct ofpact_learn *learn,
3794 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3796 const struct ofpact_learn_spec *spec;
3797 struct nx_action_learn *nal;
3800 start_ofs = ofpbuf_size(out);
3801 nal = put_NXAST_LEARN(out);
3802 nal->idle_timeout = htons(learn->idle_timeout);
3803 nal->hard_timeout = htons(learn->hard_timeout);
3804 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
3805 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
3806 nal->priority = htons(learn->priority);
3807 nal->cookie = learn->cookie;
3808 nal->flags = htons(learn->flags);
3809 nal->table_id = learn->table_id;
3811 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
3812 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
3814 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3815 put_u32(out, mf_nxm_header(spec->src.field->id));
3816 put_u16(out, spec->src.ofs);
3818 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3819 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
3820 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
3821 bits, n_dst_bytes, 0,
3825 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3826 spec->dst_type == NX_LEARN_DST_LOAD) {
3827 put_u32(out, mf_nxm_header(spec->dst.field->id));
3828 put_u16(out, spec->dst.ofs);
3832 pad_ofpat(out, start_ofs);
3835 static char * WARN_UNUSED_RESULT
3836 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
3837 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3839 return learn_parse(arg, ofpacts);
3843 format_LEARN(const struct ofpact_learn *a, struct ds *s)
3848 /* Action structure for NXAST_MULTIPATH.
3850 * This action performs the following steps in sequence:
3852 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
3853 * Refer to the definition of "enum nx_mp_fields" for details.
3855 * The 'basis' value is used as a universal hash parameter, that is,
3856 * different values of 'basis' yield different hash functions. The
3857 * particular universal hash function used is implementation-defined.
3859 * The hashed fields' values are drawn from the current state of the
3860 * flow, including all modifications that have been made by actions up to
3863 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
3864 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
3867 * The output of the algorithm is 'link', an unsigned integer less than
3868 * or equal to 'max_link'.
3870 * Some algorithms use 'arg' as an additional argument.
3872 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
3873 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
3876 * The switch will reject actions that have an unknown 'fields', or an unknown
3877 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
3878 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
3879 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
3881 struct nx_action_multipath {
3882 ovs_be16 type; /* OFPAT_VENDOR. */
3883 ovs_be16 len; /* Length is 32. */
3884 ovs_be32 vendor; /* NX_VENDOR_ID. */
3885 ovs_be16 subtype; /* NXAST_MULTIPATH. */
3887 /* What fields to hash and how. */
3888 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
3889 ovs_be16 basis; /* Universal hash parameter. */
3892 /* Multipath link choice algorithm to apply to hash value. */
3893 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
3894 ovs_be16 max_link; /* Number of output links, minus 1. */
3895 ovs_be32 arg; /* Algorithm-specific argument. */
3898 /* Where to store the result. */
3899 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
3900 ovs_be32 dst; /* Destination. */
3902 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
3905 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
3908 uint32_t n_links = ntohs(nam->max_link) + 1;
3909 size_t min_n_bits = log_2_ceil(n_links);
3910 struct ofpact_multipath *mp;
3912 mp = ofpact_put_MULTIPATH(out);
3913 mp->fields = ntohs(nam->fields);
3914 mp->basis = ntohs(nam->basis);
3915 mp->algorithm = ntohs(nam->algorithm);
3916 mp->max_link = ntohs(nam->max_link);
3917 mp->arg = ntohl(nam->arg);
3918 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
3919 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
3920 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
3922 if (!flow_hash_fields_valid(mp->fields)) {
3923 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
3924 return OFPERR_OFPBAC_BAD_ARGUMENT;
3925 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
3926 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
3927 && mp->algorithm != NX_MP_ALG_HRW
3928 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
3929 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
3930 return OFPERR_OFPBAC_BAD_ARGUMENT;
3931 } else if (mp->dst.n_bits < min_n_bits) {
3932 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
3933 "%"PRIu32" links", min_n_bits, n_links);
3934 return OFPERR_OFPBAC_BAD_ARGUMENT;
3937 return multipath_check(mp, NULL);
3941 encode_MULTIPATH(const struct ofpact_multipath *mp,
3942 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3944 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
3946 nam->fields = htons(mp->fields);
3947 nam->basis = htons(mp->basis);
3948 nam->algorithm = htons(mp->algorithm);
3949 nam->max_link = htons(mp->max_link);
3950 nam->arg = htonl(mp->arg);
3951 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
3952 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
3955 static char * WARN_UNUSED_RESULT
3956 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
3957 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3959 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
3963 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
3965 multipath_format(a, s);
3968 /* Action structure for NXAST_NOTE.
3970 * This action has no effect. It is variable length. The switch does not
3971 * attempt to interpret the user-defined 'note' data in any way. A controller
3972 * can use this action to attach arbitrary metadata to a flow.
3974 * This action might go away in the future.
3976 struct nx_action_note {
3977 ovs_be16 type; /* OFPAT_VENDOR. */
3978 ovs_be16 len; /* A multiple of 8, but at least 16. */
3979 ovs_be32 vendor; /* NX_VENDOR_ID. */
3980 ovs_be16 subtype; /* NXAST_NOTE. */
3981 uint8_t note[6]; /* Start of user-defined data. */
3982 /* Possibly followed by additional user-defined data. */
3984 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
3987 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan, struct ofpbuf *out)
3989 struct ofpact_note *note;
3990 unsigned int length;
3992 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
3993 note = ofpact_put(out, OFPACT_NOTE,
3994 offsetof(struct ofpact_note, data) + length);
3995 note->length = length;
3996 memcpy(note->data, nan->note, length);
4002 encode_NOTE(const struct ofpact_note *note,
4003 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4005 size_t start_ofs = ofpbuf_size(out);
4006 struct nx_action_note *nan;
4007 unsigned int remainder;
4010 put_NXAST_NOTE(out);
4011 ofpbuf_set_size(out, ofpbuf_size(out) - sizeof nan->note);
4013 ofpbuf_put(out, note->data, note->length);
4015 len = ofpbuf_size(out) - start_ofs;
4016 remainder = len % OFP_ACTION_ALIGN;
4018 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4020 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4021 nan->len = htons(ofpbuf_size(out) - start_ofs);
4024 static char * WARN_UNUSED_RESULT
4025 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4026 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4028 struct ofpact_note *note;
4030 note = ofpact_put_NOTE(ofpacts);
4031 while (*arg != '\0') {
4042 byte = hexits_value(arg, 2, &ok);
4044 return xstrdup("bad hex digit in `note' argument");
4046 ofpbuf_put(ofpacts, &byte, 1);
4048 note = ofpacts->frame;
4053 ofpact_update_len(ofpacts, ¬e->ofpact);
4058 format_NOTE(const struct ofpact_note *a, struct ds *s)
4062 ds_put_cstr(s, "note:");
4063 for (i = 0; i < a->length; i++) {
4065 ds_put_char(s, '.');
4067 ds_put_format(s, "%02"PRIx8, a->data[i]);
4074 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4076 ofpact_put_EXIT(out);
4081 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4082 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4084 put_NXAST_EXIT(out);
4087 static char * WARN_UNUSED_RESULT
4088 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4089 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4091 ofpact_put_EXIT(ofpacts);
4096 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4098 ds_put_cstr(s, "exit");
4101 /* Action structure for NXAST_SAMPLE.
4103 * Samples matching packets with the given probability and sends them
4104 * each to the set of collectors identified with the given ID. The
4105 * probability is expressed as a number of packets to be sampled out
4106 * of USHRT_MAX packets, and must be >0.
4108 * When sending packet samples to IPFIX collectors, the IPFIX flow
4109 * record sent for each sampled packet is associated with the given
4110 * observation domain ID and observation point ID. Each IPFIX flow
4111 * record contain the sampled packet's headers when executing this
4112 * rule. If a sampled packet's headers are modified by previous
4113 * actions in the flow, those modified headers are sent. */
4114 struct nx_action_sample {
4115 ovs_be16 type; /* OFPAT_VENDOR. */
4116 ovs_be16 len; /* Length is 24. */
4117 ovs_be32 vendor; /* NX_VENDOR_ID. */
4118 ovs_be16 subtype; /* NXAST_SAMPLE. */
4119 ovs_be16 probability; /* Fraction of packets to sample. */
4120 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4121 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4122 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4124 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4127 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas, struct ofpbuf *out)
4129 struct ofpact_sample *sample;
4131 sample = ofpact_put_SAMPLE(out);
4132 sample->probability = ntohs(nas->probability);
4133 sample->collector_set_id = ntohl(nas->collector_set_id);
4134 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4135 sample->obs_point_id = ntohl(nas->obs_point_id);
4137 if (sample->probability == 0) {
4138 return OFPERR_OFPBAC_BAD_ARGUMENT;
4145 encode_SAMPLE(const struct ofpact_sample *sample,
4146 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4148 struct nx_action_sample *nas;
4150 nas = put_NXAST_SAMPLE(out);
4151 nas->probability = htons(sample->probability);
4152 nas->collector_set_id = htonl(sample->collector_set_id);
4153 nas->obs_domain_id = htonl(sample->obs_domain_id);
4154 nas->obs_point_id = htonl(sample->obs_point_id);
4157 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4158 * action to 'ofpacts'.
4160 * Returns NULL if successful, otherwise a malloc()'d string describing the
4161 * error. The caller is responsible for freeing the returned string. */
4162 static char * WARN_UNUSED_RESULT
4163 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4164 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4166 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4169 while (ofputil_parse_key_value(&arg, &key, &value)) {
4172 if (!strcmp(key, "probability")) {
4173 error = str_to_u16(value, "probability", &os->probability);
4174 if (!error && os->probability == 0) {
4175 error = xasprintf("invalid probability value \"%s\"", value);
4177 } else if (!strcmp(key, "collector_set_id")) {
4178 error = str_to_u32(value, &os->collector_set_id);
4179 } else if (!strcmp(key, "obs_domain_id")) {
4180 error = str_to_u32(value, &os->obs_domain_id);
4181 } else if (!strcmp(key, "obs_point_id")) {
4182 error = str_to_u32(value, &os->obs_point_id);
4184 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4191 if (os->probability == 0) {
4192 return xstrdup("non-zero \"probability\" must be specified on sample");
4198 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4200 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4201 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4202 a->probability, a->collector_set_id,
4203 a->obs_domain_id, a->obs_point_id);
4206 /* Meter instruction. */
4209 encode_METER(const struct ofpact_meter *meter,
4210 enum ofp_version ofp_version, struct ofpbuf *out)
4212 if (ofp_version >= OFP13_VERSION) {
4213 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4217 static char * WARN_UNUSED_RESULT
4218 parse_METER(char *arg, struct ofpbuf *ofpacts,
4219 enum ofputil_protocol *usable_protocols)
4221 *usable_protocols &= OFPUTIL_P_OF13_UP;
4222 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
4226 format_METER(const struct ofpact_meter *a, struct ds *s)
4228 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
4231 /* Clear-Actions instruction. */
4234 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
4235 enum ofp_version ofp_version OVS_UNUSED,
4236 struct ofpbuf *out OVS_UNUSED)
4238 if (ofp_version > OFP10_VERSION) {
4239 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
4243 static char * WARN_UNUSED_RESULT
4244 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4245 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4247 ofpact_put_CLEAR_ACTIONS(ofpacts);
4252 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4254 ds_put_cstr(s, "clear_actions");
4257 /* Write-Actions instruction. */
4260 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
4261 enum ofp_version ofp_version, struct ofpbuf *out)
4263 if (ofp_version > OFP10_VERSION) {
4264 const size_t ofs = ofpbuf_size(out);
4266 instruction_put_OFPIT11_WRITE_ACTIONS(out);
4267 ofpacts_put_openflow_actions(actions->actions,
4268 ofpact_nest_get_action_len(actions),
4270 ofpacts_update_instruction_actions(out, ofs);
4274 static char * WARN_UNUSED_RESULT
4275 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
4276 enum ofputil_protocol *usable_protocols)
4278 struct ofpact_nest *on;
4282 /* Pull off existing actions or instructions. */
4283 ofpact_pad(ofpacts);
4284 ofs = ofpbuf_size(ofpacts);
4285 ofpbuf_pull(ofpacts, ofs);
4287 /* Add a Write-Actions instruction and then pull it off. */
4288 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
4289 ofpbuf_pull(ofpacts, sizeof *on);
4291 /* Parse nested actions.
4293 * We pulled off "write-actions" and the previous actions because the
4294 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4295 * that it doesn't actually include the nested actions. That means that
4296 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4297 * follows a Write-Actions, which is an invalid order. */
4298 error = ofpacts_parse(arg, ofpacts, usable_protocols, false);
4300 /* Put the Write-Actions back on and update its length. */
4301 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
4302 on->ofpact.len = ofpbuf_size(ofpacts);
4304 /* Put any previous actions or instructions back on. */
4305 ofpbuf_push_uninit(ofpacts, ofs);
4311 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
4313 ds_put_cstr(s, "write_actions(");
4314 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
4315 ds_put_char(s, ')');
4318 /* Action structure for NXAST_WRITE_METADATA.
4320 * Modifies the 'mask' bits of the metadata value. */
4321 struct nx_action_write_metadata {
4322 ovs_be16 type; /* OFPAT_VENDOR. */
4323 ovs_be16 len; /* Length is 32. */
4324 ovs_be32 vendor; /* NX_VENDOR_ID. */
4325 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
4326 uint8_t zeros[6]; /* Must be zero. */
4327 ovs_be64 metadata; /* Metadata register. */
4328 ovs_be64 mask; /* Metadata mask. */
4330 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
4333 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
4336 struct ofpact_metadata *om;
4338 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
4339 return OFPERR_NXBRC_MUST_BE_ZERO;
4342 om = ofpact_put_WRITE_METADATA(out);
4343 om->metadata = nawm->metadata;
4344 om->mask = nawm->mask;
4350 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
4351 enum ofp_version ofp_version, struct ofpbuf *out)
4353 if (ofp_version == OFP10_VERSION) {
4354 struct nx_action_write_metadata *nawm;
4356 nawm = put_NXAST_WRITE_METADATA(out);
4357 nawm->metadata = metadata->metadata;
4358 nawm->mask = metadata->mask;
4360 struct ofp11_instruction_write_metadata *oiwm;
4362 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
4363 oiwm->metadata = metadata->metadata;
4364 oiwm->metadata_mask = metadata->mask;
4368 static char * WARN_UNUSED_RESULT
4369 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
4370 enum ofputil_protocol *usable_protocols)
4372 struct ofpact_metadata *om;
4373 char *mask = strchr(arg, '/');
4375 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
4377 om = ofpact_put_WRITE_METADATA(ofpacts);
4382 error = str_to_be64(mask + 1, &om->mask);
4387 om->mask = OVS_BE64_MAX;
4390 return str_to_be64(arg, &om->metadata);
4394 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
4396 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
4397 if (a->mask != OVS_BE64_MAX) {
4398 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
4402 /* Goto-Table instruction. */
4405 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
4406 enum ofp_version ofp_version, struct ofpbuf *out)
4408 if (ofp_version == OFP10_VERSION) {
4409 struct nx_action_resubmit *nar;
4411 nar = put_NXAST_RESUBMIT_TABLE(out);
4412 nar->table = goto_table->table_id;
4413 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
4415 struct ofp11_instruction_goto_table *oigt;
4417 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
4418 oigt->table_id = goto_table->table_id;
4419 memset(oigt->pad, 0, sizeof oigt->pad);
4423 static char * WARN_UNUSED_RESULT
4424 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
4425 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4427 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
4428 char *table_s = strsep(&arg, ",");
4429 if (!table_s || !table_s[0]) {
4430 return xstrdup("instruction goto-table needs table id");
4432 return str_to_u8(table_s, "table", &ogt->table_id);
4436 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
4438 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
4442 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
4443 const struct ofp_action_header *bad_action, enum ofperr error)
4445 if (!VLOG_DROP_WARN(&rl)) {
4449 ds_put_hex_dump(&s, actions, actions_len, 0, false);
4450 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
4451 (char *)bad_action - (char *)actions,
4452 ofperr_get_name(error), ds_cstr(&s));
4458 ofpacts_decode(const void *actions, size_t actions_len,
4459 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
4461 struct ofpbuf openflow;
4463 ofpbuf_use_const(&openflow, actions, actions_len);
4464 while (ofpbuf_size(&openflow)) {
4465 const struct ofp_action_header *action = ofpbuf_data(&openflow);
4466 enum ofp_raw_action_type raw;
4470 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
4472 error = ofpact_decode(action, raw, arg, ofpacts);
4476 log_bad_action(actions, actions_len, action, error);
4481 ofpact_pad(ofpacts);
4486 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
4487 unsigned int actions_len,
4488 enum ofp_version version,
4489 uint32_t allowed_ovsinsts,
4490 struct ofpbuf *ofpacts)
4492 const struct ofp_action_header *actions;
4495 ofpbuf_clear(ofpacts);
4497 if (actions_len % OFP_ACTION_ALIGN != 0) {
4498 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
4499 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
4500 return OFPERR_OFPBRC_BAD_LEN;
4503 actions = ofpbuf_try_pull(openflow, actions_len);
4504 if (actions == NULL) {
4505 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
4506 "remaining message length (%"PRIu32")",
4507 actions_len, ofpbuf_size(openflow));
4508 return OFPERR_OFPBRC_BAD_LEN;
4511 error = ofpacts_decode(actions, actions_len, version, ofpacts);
4513 ofpbuf_clear(ofpacts);
4517 error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
4520 ofpbuf_clear(ofpacts);
4525 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
4526 * front of 'openflow' into ofpacts. On success, replaces any existing content
4527 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
4528 * Returns 0 if successful, otherwise an OpenFlow error.
4530 * Actions are processed according to their OpenFlow version which
4531 * is provided in the 'version' parameter.
4533 * In most places in OpenFlow, actions appear encapsulated in instructions, so
4534 * you should call ofpacts_pull_openflow_instructions() instead of this
4537 * The parsed actions are valid generically, but they may not be valid in a
4538 * specific context. For example, port numbers up to OFPP_MAX are valid
4539 * generically, but specific datapaths may only support port numbers in a
4540 * smaller range. Use ofpacts_check() to additional check whether actions are
4541 * valid in a specific context. */
4543 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
4544 unsigned int actions_len,
4545 enum ofp_version version,
4546 struct ofpbuf *ofpacts)
4548 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
4549 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4553 /* OpenFlow 1.1 actions. */
4556 /* True if an action sets the value of a field
4557 * in a way that is compatibile with the action set.
4558 * The field can be set via either a set or a move action.
4559 * False otherwise. */
4561 ofpact_is_set_or_move_action(const struct ofpact *a)
4564 case OFPACT_SET_FIELD:
4565 case OFPACT_REG_MOVE:
4566 case OFPACT_SET_ETH_DST:
4567 case OFPACT_SET_ETH_SRC:
4568 case OFPACT_SET_IP_DSCP:
4569 case OFPACT_SET_IP_ECN:
4570 case OFPACT_SET_IP_TTL:
4571 case OFPACT_SET_IPV4_DST:
4572 case OFPACT_SET_IPV4_SRC:
4573 case OFPACT_SET_L4_DST_PORT:
4574 case OFPACT_SET_L4_SRC_PORT:
4575 case OFPACT_SET_MPLS_LABEL:
4576 case OFPACT_SET_MPLS_TC:
4577 case OFPACT_SET_MPLS_TTL:
4578 case OFPACT_SET_QUEUE:
4579 case OFPACT_SET_TUNNEL:
4580 case OFPACT_SET_VLAN_PCP:
4581 case OFPACT_SET_VLAN_VID:
4584 case OFPACT_CLEAR_ACTIONS:
4585 case OFPACT_CONTROLLER:
4586 case OFPACT_DEC_MPLS_TTL:
4587 case OFPACT_DEC_TTL:
4588 case OFPACT_ENQUEUE:
4590 case OFPACT_FIN_TIMEOUT:
4591 case OFPACT_GOTO_TABLE:
4595 case OFPACT_MULTIPATH:
4598 case OFPACT_OUTPUT_REG:
4599 case OFPACT_POP_MPLS:
4600 case OFPACT_POP_QUEUE:
4601 case OFPACT_PUSH_MPLS:
4602 case OFPACT_PUSH_VLAN:
4603 case OFPACT_RESUBMIT:
4605 case OFPACT_STACK_POP:
4606 case OFPACT_STACK_PUSH:
4607 case OFPACT_STRIP_VLAN:
4608 case OFPACT_WRITE_ACTIONS:
4609 case OFPACT_WRITE_METADATA:
4616 /* True if an action is allowed in the action set.
4617 * False otherwise. */
4619 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
4622 case OFPACT_DEC_MPLS_TTL:
4623 case OFPACT_DEC_TTL:
4626 case OFPACT_POP_MPLS:
4627 case OFPACT_PUSH_MPLS:
4628 case OFPACT_PUSH_VLAN:
4629 case OFPACT_REG_MOVE:
4630 case OFPACT_SET_FIELD:
4631 case OFPACT_SET_ETH_DST:
4632 case OFPACT_SET_ETH_SRC:
4633 case OFPACT_SET_IP_DSCP:
4634 case OFPACT_SET_IP_ECN:
4635 case OFPACT_SET_IP_TTL:
4636 case OFPACT_SET_IPV4_DST:
4637 case OFPACT_SET_IPV4_SRC:
4638 case OFPACT_SET_L4_DST_PORT:
4639 case OFPACT_SET_L4_SRC_PORT:
4640 case OFPACT_SET_MPLS_LABEL:
4641 case OFPACT_SET_MPLS_TC:
4642 case OFPACT_SET_MPLS_TTL:
4643 case OFPACT_SET_QUEUE:
4644 case OFPACT_SET_TUNNEL:
4645 case OFPACT_SET_VLAN_PCP:
4646 case OFPACT_SET_VLAN_VID:
4647 case OFPACT_STRIP_VLAN:
4650 /* In general these actions are excluded because they are not part of
4651 * the OpenFlow specification nor map to actions that are defined in
4652 * the specification. Thus the order in which they should be applied
4653 * in the action set is undefined. */
4655 case OFPACT_CONTROLLER:
4656 case OFPACT_ENQUEUE:
4658 case OFPACT_FIN_TIMEOUT:
4660 case OFPACT_MULTIPATH:
4662 case OFPACT_OUTPUT_REG:
4663 case OFPACT_POP_QUEUE:
4664 case OFPACT_RESUBMIT:
4666 case OFPACT_STACK_POP:
4667 case OFPACT_STACK_PUSH:
4669 /* The action set may only include actions and thus
4670 * may not include any instructions */
4671 case OFPACT_CLEAR_ACTIONS:
4672 case OFPACT_GOTO_TABLE:
4674 case OFPACT_WRITE_ACTIONS:
4675 case OFPACT_WRITE_METADATA:
4682 /* Append ofpact 'a' onto the tail of 'out' */
4684 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
4686 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
4689 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
4691 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
4692 enum ofpact_type filter)
4694 const struct ofpact *target;
4695 const struct ofpact *a;
4698 OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
4699 if (a->type == filter) {
4704 ofpact_copy(out, target);
4706 return target != NULL;
4709 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
4710 * The order of appended ofpacts is preserved between 'in' and 'out' */
4712 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
4713 bool (*filter)(const struct ofpact *))
4715 const struct ofpact *a;
4717 OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
4719 ofpact_copy(out, a);
4724 /* Reads 'action_set', which contains ofpacts accumulated by
4725 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
4726 * executed directly into 'action_list'. (These names correspond to the
4727 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
4729 * In general this involves appending the last instance of each action that is
4730 * adimissible in the action set in the order described in the OpenFlow
4734 * + output action is only appended if no group action was present in 'in'.
4735 * + As a simplification all set actions are copied in the order the are
4736 * provided in 'in' as many set actions applied to a field has the same
4737 * affect as only applying the last action that sets a field and
4738 * duplicates are removed by do_xlate_actions().
4739 * This has an unwanted side-effect of compsoting multiple
4740 * LOAD_REG actions that touch different regions of the same field. */
4742 ofpacts_execute_action_set(struct ofpbuf *action_list,
4743 const struct ofpbuf *action_set)
4745 /* The OpenFlow spec "Action Set" section specifies this order. */
4746 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
4747 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
4748 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
4749 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
4750 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
4751 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
4752 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
4753 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
4755 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
4756 * we should execute only OFPACT_GROUP.
4758 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
4759 * all the actions because there's no point in modifying a packet that will
4760 * not be sent anywhere. */
4761 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
4762 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
4763 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
4764 ofpbuf_clear(action_list);
4770 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
4771 size_t n_in, enum ofp_version version,
4776 size_t start = ofpbuf_size(out);
4778 error = ofpacts_decode(in, n_in, version, out);
4784 OFPACT_FOR_EACH (a, ofpact_end(ofpbuf_data(out), start), ofpbuf_size(out) - start) {
4785 if (!ofpact_is_allowed_in_actions_set(a)) {
4786 VLOG_WARN_RL(&rl, "disallowed action in action set");
4787 return OFPERR_OFPBAC_BAD_TYPE;
4794 /* OpenFlow 1.1 instructions. */
4796 struct instruction_type_info {
4797 enum ovs_instruction_type type;
4801 static const struct instruction_type_info inst_info[] = {
4802 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
4808 ovs_instruction_name_from_type(enum ovs_instruction_type type)
4810 return inst_info[type].name;
4814 ovs_instruction_type_from_name(const char *name)
4816 const struct instruction_type_info *p;
4817 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
4818 if (!strcasecmp(name, p->name)) {
4825 enum ovs_instruction_type
4826 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
4830 return OVSINST_OFPIT13_METER;
4831 case OFPACT_CLEAR_ACTIONS:
4832 return OVSINST_OFPIT11_CLEAR_ACTIONS;
4833 case OFPACT_WRITE_ACTIONS:
4834 return OVSINST_OFPIT11_WRITE_ACTIONS;
4835 case OFPACT_WRITE_METADATA:
4836 return OVSINST_OFPIT11_WRITE_METADATA;
4837 case OFPACT_GOTO_TABLE:
4838 return OVSINST_OFPIT11_GOTO_TABLE;
4841 case OFPACT_CONTROLLER:
4842 case OFPACT_ENQUEUE:
4843 case OFPACT_OUTPUT_REG:
4845 case OFPACT_SET_VLAN_VID:
4846 case OFPACT_SET_VLAN_PCP:
4847 case OFPACT_STRIP_VLAN:
4848 case OFPACT_PUSH_VLAN:
4849 case OFPACT_SET_ETH_SRC:
4850 case OFPACT_SET_ETH_DST:
4851 case OFPACT_SET_IPV4_SRC:
4852 case OFPACT_SET_IPV4_DST:
4853 case OFPACT_SET_IP_DSCP:
4854 case OFPACT_SET_IP_ECN:
4855 case OFPACT_SET_IP_TTL:
4856 case OFPACT_SET_L4_SRC_PORT:
4857 case OFPACT_SET_L4_DST_PORT:
4858 case OFPACT_REG_MOVE:
4859 case OFPACT_SET_FIELD:
4860 case OFPACT_STACK_PUSH:
4861 case OFPACT_STACK_POP:
4862 case OFPACT_DEC_TTL:
4863 case OFPACT_SET_MPLS_LABEL:
4864 case OFPACT_SET_MPLS_TC:
4865 case OFPACT_SET_MPLS_TTL:
4866 case OFPACT_DEC_MPLS_TTL:
4867 case OFPACT_PUSH_MPLS:
4868 case OFPACT_POP_MPLS:
4869 case OFPACT_SET_TUNNEL:
4870 case OFPACT_SET_QUEUE:
4871 case OFPACT_POP_QUEUE:
4872 case OFPACT_FIN_TIMEOUT:
4873 case OFPACT_RESUBMIT:
4875 case OFPACT_MULTIPATH:
4880 return OVSINST_OFPIT11_APPLY_ACTIONS;
4885 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
4886 const uint16_t inst_type)
4888 switch (inst_type) {
4890 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
4892 *instruction_type = OVSINST_##ENUM; \
4898 return OFPERR_OFPBIC_UNKNOWN_INST;
4902 /* Two-way translation between OVS's internal "OVSINST_*" representation of
4903 * instructions and the "OFPIT_*" representation used in OpenFlow. */
4904 struct ovsinst_map {
4905 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
4906 int ofpit; /* OFPIT_* number from OpenFlow spec. */
4909 static const struct ovsinst_map *
4910 get_ovsinst_map(enum ofp_version version)
4912 /* OpenFlow 1.1 and 1.2 instructions. */
4913 static const struct ovsinst_map of11[] = {
4914 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
4915 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
4916 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
4917 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
4918 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
4922 /* OpenFlow 1.3+ instructions. */
4923 static const struct ovsinst_map of13[] = {
4924 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
4925 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
4926 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
4927 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
4928 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
4929 { OVSINST_OFPIT13_METER, 6 },
4933 return version < OFP13_VERSION ? of11 : of13;
4936 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
4937 * values, into a bitmap of instructions suitable for OpenFlow 'version'
4938 * (OFP11_VERSION or later), and returns the result. */
4940 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
4942 uint32_t ofpit_bitmap = 0;
4943 const struct ovsinst_map *x;
4945 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
4946 if (ovsinst_bitmap & (1u << x->ovsinst)) {
4947 ofpit_bitmap |= 1u << x->ofpit;
4950 return htonl(ofpit_bitmap);
4953 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
4954 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
4955 * correspond to OVSINST_* values, and returns the result. */
4957 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
4959 uint32_t ovsinst_bitmap = 0;
4960 const struct ovsinst_map *x;
4962 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
4963 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
4964 ovsinst_bitmap |= 1u << x->ovsinst;
4967 return ovsinst_bitmap;
4970 static inline struct ofp11_instruction *
4971 instruction_next(const struct ofp11_instruction *inst)
4973 return ((struct ofp11_instruction *) (void *)
4974 ((uint8_t *) inst + ntohs(inst->len)));
4978 instruction_is_valid(const struct ofp11_instruction *inst,
4979 size_t n_instructions)
4981 uint16_t len = ntohs(inst->len);
4982 return (!(len % OFP11_INSTRUCTION_ALIGN)
4983 && len >= sizeof *inst
4984 && len / sizeof *inst <= n_instructions);
4987 /* This macro is careful to check for instructions with bad lengths. */
4988 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
4989 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
4990 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
4991 ((LEFT) -= (ntohs((ITER)->len) \
4992 / sizeof(struct ofp11_instruction)), \
4993 (ITER) = instruction_next(ITER)))
4996 decode_openflow11_instruction(const struct ofp11_instruction *inst,
4997 enum ovs_instruction_type *type)
4999 uint16_t len = ntohs(inst->len);
5001 switch (inst->type) {
5002 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5003 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5005 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5006 case CONSTANT_HTONS(ENUM): \
5008 ? len >= sizeof(struct STRUCT) \
5009 : len == sizeof(struct STRUCT)) { \
5010 *type = OVSINST_##ENUM; \
5013 return OFPERR_OFPBIC_BAD_LEN; \
5019 return OFPERR_OFPBIC_UNKNOWN_INST;
5024 decode_openflow11_instructions(const struct ofp11_instruction insts[],
5026 const struct ofp11_instruction *out[])
5028 const struct ofp11_instruction *inst;
5031 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5032 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5033 enum ovs_instruction_type type;
5036 error = decode_openflow11_instruction(inst, &type);
5042 return OFPERR_OFPBIC_DUP_INST;
5048 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5049 (n_insts - left) * sizeof *inst);
5050 return OFPERR_OFPBIC_BAD_LEN;
5056 get_actions_from_instruction(const struct ofp11_instruction *inst,
5057 const struct ofp_action_header **actions,
5058 size_t *actions_len)
5060 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5061 *actions_len = ntohs(inst->len) - sizeof *inst;
5065 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5066 unsigned int instructions_len,
5067 enum ofp_version version,
5068 struct ofpbuf *ofpacts)
5070 const struct ofp11_instruction *instructions;
5071 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5074 if (version == OFP10_VERSION) {
5075 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5077 (1u << N_OVS_INSTRUCTIONS) - 1,
5081 ofpbuf_clear(ofpacts);
5083 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5084 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5086 instructions_len, OFP11_INSTRUCTION_ALIGN);
5087 error = OFPERR_OFPBIC_BAD_LEN;
5091 instructions = ofpbuf_try_pull(openflow, instructions_len);
5092 if (instructions == NULL) {
5093 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5094 "remaining message length (%"PRIu32")",
5095 instructions_len, ofpbuf_size(openflow));
5096 error = OFPERR_OFPBIC_BAD_LEN;
5100 error = decode_openflow11_instructions(
5101 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5107 if (insts[OVSINST_OFPIT13_METER]) {
5108 const struct ofp13_instruction_meter *oim;
5109 struct ofpact_meter *om;
5111 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5112 insts[OVSINST_OFPIT13_METER]);
5114 om = ofpact_put_METER(ofpacts);
5115 om->meter_id = ntohl(oim->meter_id);
5117 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5118 const struct ofp_action_header *actions;
5121 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5122 &actions, &actions_len);
5123 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5128 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5129 instruction_get_OFPIT11_CLEAR_ACTIONS(
5130 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5131 ofpact_put_CLEAR_ACTIONS(ofpacts);
5133 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5134 struct ofpact_nest *on;
5135 const struct ofp_action_header *actions;
5139 ofpact_pad(ofpacts);
5140 start = ofpbuf_size(ofpacts);
5141 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5142 offsetof(struct ofpact_nest, actions));
5143 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5144 &actions, &actions_len);
5145 error = ofpacts_decode_for_action_set(actions, actions_len,
5150 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5151 on->ofpact.len = ofpbuf_size(ofpacts) - start;
5153 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5154 const struct ofp11_instruction_write_metadata *oiwm;
5155 struct ofpact_metadata *om;
5157 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5158 insts[OVSINST_OFPIT11_WRITE_METADATA]);
5160 om = ofpact_put_WRITE_METADATA(ofpacts);
5161 om->metadata = oiwm->metadata;
5162 om->mask = oiwm->metadata_mask;
5164 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5165 const struct ofp11_instruction_goto_table *oigt;
5166 struct ofpact_goto_table *ogt;
5168 oigt = instruction_get_OFPIT11_GOTO_TABLE(
5169 insts[OVSINST_OFPIT11_GOTO_TABLE]);
5170 ogt = ofpact_put_GOTO_TABLE(ofpacts);
5171 ogt->table_id = oigt->table_id;
5174 error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
5175 (1u << N_OVS_INSTRUCTIONS) - 1);
5178 ofpbuf_clear(ofpacts);
5183 /* Update the length of the instruction that begins at offset 'ofs' within
5184 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5185 * If the instruction contains no nested actions, deletes it entirely. */
5187 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5189 struct ofp11_instruction_actions *oia;
5191 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5192 if (ofpbuf_size(openflow) > ofs + sizeof *oia) {
5193 oia->len = htons(ofpbuf_size(openflow) - ofs);
5195 ofpbuf_set_size(openflow, ofs);
5199 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5200 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5201 * valid, otherwise an OpenFlow error code. */
5203 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5211 case OFPP_CONTROLLER:
5217 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
5220 return OFPERR_OFPBAC_BAD_OUT_PORT;
5224 /* Removes the protocols that require consistency between match and actions
5225 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5227 * (An example of an inconsistency between match and actions is a flow that
5228 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5231 inconsistent_match(enum ofputil_protocol *usable_protocols)
5233 *usable_protocols &= OFPUTIL_P_OF10_ANY;
5236 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5237 * caller must restore them.
5239 * Modifies some actions, filling in fields that could not be properly set
5240 * without context. */
5242 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
5243 struct flow *flow, ofp_port_t max_ports,
5244 uint8_t table_id, uint8_t n_tables)
5246 const struct ofpact_enqueue *enqueue;
5247 const struct mf_field *mf;
5251 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
5254 case OFPACT_CONTROLLER:
5257 case OFPACT_ENQUEUE:
5258 enqueue = ofpact_get_ENQUEUE(a);
5259 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
5260 && enqueue->port != OFPP_IN_PORT
5261 && enqueue->port != OFPP_LOCAL) {
5262 return OFPERR_OFPBAC_BAD_OUT_PORT;
5266 case OFPACT_OUTPUT_REG:
5267 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
5270 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
5272 case OFPACT_SET_VLAN_VID:
5273 /* Remember if we saw a vlan tag in the flow to aid translating to
5274 * OpenFlow 1.1+ if need be. */
5275 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
5276 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5277 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5278 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5279 inconsistent_match(usable_protocols);
5281 /* Temporary mark that we have a vlan tag. */
5282 flow->vlan_tci |= htons(VLAN_CFI);
5285 case OFPACT_SET_VLAN_PCP:
5286 /* Remember if we saw a vlan tag in the flow to aid translating to
5287 * OpenFlow 1.1+ if need be. */
5288 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
5289 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5290 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5291 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5292 inconsistent_match(usable_protocols);
5294 /* Temporary mark that we have a vlan tag. */
5295 flow->vlan_tci |= htons(VLAN_CFI);
5298 case OFPACT_STRIP_VLAN:
5299 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5300 inconsistent_match(usable_protocols);
5302 /* Temporary mark that we have no vlan tag. */
5303 flow->vlan_tci = htons(0);
5306 case OFPACT_PUSH_VLAN:
5307 if (flow->vlan_tci & htons(VLAN_CFI)) {
5308 /* Multiple VLAN headers not supported. */
5309 return OFPERR_OFPBAC_BAD_TAG;
5311 /* Temporary mark that we have a vlan tag. */
5312 flow->vlan_tci |= htons(VLAN_CFI);
5315 case OFPACT_SET_ETH_SRC:
5316 case OFPACT_SET_ETH_DST:
5319 case OFPACT_SET_IPV4_SRC:
5320 case OFPACT_SET_IPV4_DST:
5321 if (flow->dl_type != htons(ETH_TYPE_IP)) {
5322 inconsistent_match(usable_protocols);
5326 case OFPACT_SET_IP_DSCP:
5327 case OFPACT_SET_IP_ECN:
5328 case OFPACT_SET_IP_TTL:
5329 case OFPACT_DEC_TTL:
5330 if (!is_ip_any(flow)) {
5331 inconsistent_match(usable_protocols);
5335 case OFPACT_SET_L4_SRC_PORT:
5336 if (!is_ip_any(flow) ||
5337 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5338 && flow->nw_proto != IPPROTO_SCTP)) {
5339 inconsistent_match(usable_protocols);
5341 /* Note on which transport protocol the port numbers are set.
5342 * This allows this set action to be converted to an OF1.2 set field
5344 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
5347 case OFPACT_SET_L4_DST_PORT:
5348 if (!is_ip_any(flow) ||
5349 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5350 && flow->nw_proto != IPPROTO_SCTP)) {
5351 inconsistent_match(usable_protocols);
5353 /* Note on which transport protocol the port numbers are set.
5354 * This allows this set action to be converted to an OF1.2 set field
5356 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
5359 case OFPACT_REG_MOVE:
5360 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
5362 case OFPACT_SET_FIELD:
5363 mf = ofpact_get_SET_FIELD(a)->field;
5364 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
5365 if (!mf_are_prereqs_ok(mf, flow) ||
5366 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
5367 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
5369 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5371 /* Remember if we saw a vlan tag in the flow to aid translating to
5372 * OpenFlow 1.1 if need be. */
5373 ofpact_get_SET_FIELD(a)->flow_has_vlan =
5374 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5375 if (mf->id == MFF_VLAN_TCI) {
5376 /* The set field may add or remove the vlan tag,
5377 * Mark the status temporarily. */
5378 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
5382 case OFPACT_STACK_PUSH:
5383 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
5385 case OFPACT_STACK_POP:
5386 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
5388 case OFPACT_SET_MPLS_LABEL:
5389 case OFPACT_SET_MPLS_TC:
5390 case OFPACT_SET_MPLS_TTL:
5391 case OFPACT_DEC_MPLS_TTL:
5392 if (!eth_type_mpls(flow->dl_type)) {
5393 inconsistent_match(usable_protocols);
5397 case OFPACT_SET_TUNNEL:
5398 case OFPACT_SET_QUEUE:
5399 case OFPACT_POP_QUEUE:
5400 case OFPACT_RESUBMIT:
5403 case OFPACT_FIN_TIMEOUT:
5404 if (flow->nw_proto != IPPROTO_TCP) {
5405 inconsistent_match(usable_protocols);
5410 return learn_check(ofpact_get_LEARN(a), flow);
5412 case OFPACT_MULTIPATH:
5413 return multipath_check(ofpact_get_MULTIPATH(a), flow);
5419 case OFPACT_PUSH_MPLS:
5420 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
5421 /* The packet is now MPLS and the MPLS payload is opaque.
5422 * Thus nothing can be assumed about the network protocol.
5423 * Temporarily mark that we have no nw_proto. */
5427 case OFPACT_POP_MPLS:
5428 if (!eth_type_mpls(flow->dl_type)) {
5429 inconsistent_match(usable_protocols);
5431 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
5437 case OFPACT_CLEAR_ACTIONS:
5440 case OFPACT_WRITE_ACTIONS: {
5441 /* Use a temporary copy of 'usable_protocols' because we can't check
5442 * consistency of an action set. */
5443 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
5444 enum ofputil_protocol p = *usable_protocols;
5445 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
5446 flow, max_ports, table_id, n_tables, &p);
5449 case OFPACT_WRITE_METADATA:
5452 case OFPACT_METER: {
5453 uint32_t mid = ofpact_get_METER(a)->meter_id;
5454 if (mid == 0 || mid > OFPM13_MAX) {
5455 return OFPERR_OFPMMFC_INVALID_METER;
5460 case OFPACT_GOTO_TABLE: {
5461 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
5462 if ((table_id != 255 && goto_table <= table_id)
5463 || (n_tables != 255 && goto_table >= n_tables)) {
5464 return OFPERR_OFPBIC_BAD_TABLE_ID;
5477 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5478 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
5479 * switch with no more than 'max_ports' ports.
5481 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
5482 * '*usable_protocols' the protocols that forbid the inconsistency. (An
5483 * example of an inconsistency between match and actions is a flow that does
5484 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
5486 * May annotate ofpacts with information gathered from the 'flow'.
5488 * May temporarily modify 'flow', but restores the changes before returning. */
5490 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
5491 struct flow *flow, ofp_port_t max_ports,
5492 uint8_t table_id, uint8_t n_tables,
5493 enum ofputil_protocol *usable_protocols)
5496 ovs_be16 dl_type = flow->dl_type;
5497 ovs_be16 vlan_tci = flow->vlan_tci;
5498 uint8_t nw_proto = flow->nw_proto;
5499 enum ofperr error = 0;
5501 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5502 error = ofpact_check__(usable_protocols, a, flow,
5503 max_ports, table_id, n_tables);
5508 /* Restore fields that may have been modified. */
5509 flow->dl_type = dl_type;
5510 flow->vlan_tci = vlan_tci;
5511 flow->nw_proto = nw_proto;
5515 /* Like ofpacts_check(), but reports inconsistencies as
5516 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
5518 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
5519 struct flow *flow, ofp_port_t max_ports,
5520 uint8_t table_id, uint8_t n_tables,
5521 enum ofputil_protocol usable_protocols)
5523 enum ofputil_protocol p = usable_protocols;
5526 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
5527 table_id, n_tables, &p);
5528 return (error ? error
5529 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
5533 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5534 * in the appropriate order as defined by the OpenFlow spec. */
5536 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
5537 uint32_t allowed_ovsinsts)
5539 const struct ofpact *a;
5540 enum ovs_instruction_type inst;
5542 inst = OVSINST_OFPIT13_METER;
5543 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5544 enum ovs_instruction_type next;
5546 next = ovs_instruction_type_from_ofpact_type(a->type);
5548 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
5551 const char *name = ovs_instruction_name_from_type(inst);
5552 const char *next_name = ovs_instruction_name_from_type(next);
5555 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
5556 "1.1+ compatibility", name);
5558 VLOG_WARN("invalid instruction ordering: %s must appear "
5559 "before %s, for OpenFlow 1.1+ compatibility",
5562 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
5564 if (!((1u << next) & allowed_ovsinsts)) {
5565 const char *name = ovs_instruction_name_from_type(next);
5567 VLOG_WARN("%s instruction not allowed here", name);
5568 return OFPERR_OFPBIC_UNSUP_INST;
5577 /* Converting ofpacts to OpenFlow. */
5580 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
5584 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5585 case OFPACT_##ENUM: \
5586 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
5595 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
5596 * actions in 'openflow', appending the actions to any existing data in
5599 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
5600 struct ofpbuf *openflow,
5601 enum ofp_version ofp_version)
5603 const struct ofpact *a;
5604 size_t start_size = ofpbuf_size(openflow);
5606 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5607 encode_ofpact(a, ofp_version, openflow);
5609 return ofpbuf_size(openflow) - start_size;
5612 static enum ovs_instruction_type
5613 ofpact_is_apply_actions(const struct ofpact *a)
5615 return (ovs_instruction_type_from_ofpact_type(a->type)
5616 == OVSINST_OFPIT11_APPLY_ACTIONS);
5620 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
5622 struct ofpbuf *openflow,
5623 enum ofp_version ofp_version)
5625 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
5626 const struct ofpact *a;
5628 if (ofp_version == OFP10_VERSION) {
5629 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
5636 if (ofpact_is_apply_actions(a)) {
5637 size_t ofs = ofpbuf_size(openflow);
5639 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
5641 encode_ofpact(a, ofp_version, openflow);
5643 } while (a < end && ofpact_is_apply_actions(a));
5644 ofpacts_update_instruction_actions(openflow, ofs);
5646 encode_ofpact(a, ofp_version, openflow);
5652 /* Sets of supported actions. */
5654 /* Two-way translation between OVS's internal "OFPACT_*" representation of
5655 * actions and the "OFPAT_*" representation used in some OpenFlow version.
5656 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
5657 * instance is specific to one OpenFlow version.) */
5659 enum ofpact_type ofpact; /* Internal name for action type. */
5660 int ofpat; /* OFPAT_* number from OpenFlow spec. */
5663 static const struct ofpact_map *
5664 get_ofpact_map(enum ofp_version version)
5666 /* OpenFlow 1.0 actions. */
5667 static const struct ofpact_map of10[] = {
5668 { OFPACT_OUTPUT, 0 },
5669 { OFPACT_SET_VLAN_VID, 1 },
5670 { OFPACT_SET_VLAN_PCP, 2 },
5671 { OFPACT_STRIP_VLAN, 3 },
5672 { OFPACT_SET_ETH_SRC, 4 },
5673 { OFPACT_SET_ETH_DST, 5 },
5674 { OFPACT_SET_IPV4_SRC, 6 },
5675 { OFPACT_SET_IPV4_DST, 7 },
5676 { OFPACT_SET_IP_DSCP, 8 },
5677 { OFPACT_SET_L4_SRC_PORT, 9 },
5678 { OFPACT_SET_L4_DST_PORT, 10 },
5679 { OFPACT_ENQUEUE, 11 },
5683 /* OpenFlow 1.1 actions. */
5684 static const struct ofpact_map of11[] = {
5685 { OFPACT_OUTPUT, 0 },
5686 { OFPACT_SET_VLAN_VID, 1 },
5687 { OFPACT_SET_VLAN_PCP, 2 },
5688 { OFPACT_SET_ETH_SRC, 3 },
5689 { OFPACT_SET_ETH_DST, 4 },
5690 { OFPACT_SET_IPV4_SRC, 5 },
5691 { OFPACT_SET_IPV4_DST, 6 },
5692 { OFPACT_SET_IP_DSCP, 7 },
5693 { OFPACT_SET_IP_ECN, 8 },
5694 { OFPACT_SET_L4_SRC_PORT, 9 },
5695 { OFPACT_SET_L4_DST_PORT, 10 },
5696 /* OFPAT_COPY_TTL_OUT (11) not supported. */
5697 /* OFPAT_COPY_TTL_IN (12) not supported. */
5698 { OFPACT_SET_MPLS_LABEL, 13 },
5699 { OFPACT_SET_MPLS_TC, 14 },
5700 { OFPACT_SET_MPLS_TTL, 15 },
5701 { OFPACT_DEC_MPLS_TTL, 16 },
5702 { OFPACT_PUSH_VLAN, 17 },
5703 { OFPACT_STRIP_VLAN, 18 },
5704 { OFPACT_PUSH_MPLS, 19 },
5705 { OFPACT_POP_MPLS, 20 },
5706 { OFPACT_SET_QUEUE, 21 },
5707 { OFPACT_GROUP, 22 },
5708 { OFPACT_SET_IP_TTL, 23 },
5709 { OFPACT_DEC_TTL, 24 },
5713 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
5714 static const struct ofpact_map of12[] = {
5715 { OFPACT_OUTPUT, 0 },
5716 /* OFPAT_COPY_TTL_OUT (11) not supported. */
5717 /* OFPAT_COPY_TTL_IN (12) not supported. */
5718 { OFPACT_SET_MPLS_TTL, 15 },
5719 { OFPACT_DEC_MPLS_TTL, 16 },
5720 { OFPACT_PUSH_VLAN, 17 },
5721 { OFPACT_STRIP_VLAN, 18 },
5722 { OFPACT_PUSH_MPLS, 19 },
5723 { OFPACT_POP_MPLS, 20 },
5724 { OFPACT_SET_QUEUE, 21 },
5725 { OFPACT_GROUP, 22 },
5726 { OFPACT_SET_IP_TTL, 23 },
5727 { OFPACT_DEC_TTL, 24 },
5728 { OFPACT_SET_FIELD, 25 },
5729 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
5730 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
5750 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
5751 * values, into a bitmap of actions suitable for OpenFlow 'version', and
5752 * returns the result. */
5754 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
5756 uint32_t openflow_bitmap = 0;
5757 const struct ofpact_map *x;
5759 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
5760 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
5761 openflow_bitmap |= 1u << x->ofpat;
5764 return htonl(openflow_bitmap);
5767 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
5768 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
5769 * and returns the result. */
5771 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
5773 uint64_t ofpact_bitmap = 0;
5774 const struct ofpact_map *x;
5776 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
5777 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
5778 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
5781 return ofpact_bitmap;
5784 /* Appends to 's' a string representation of the set of OFPACT_* represented
5785 * by 'ofpacts_bitmap'. */
5787 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
5789 if (!ofpacts_bitmap) {
5790 ds_put_cstr(s, "<none>");
5792 while (ofpacts_bitmap) {
5793 ds_put_format(s, "%s ",
5794 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
5795 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
5801 /* Returns true if 'action' outputs to 'port', false otherwise. */
5803 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
5805 switch (ofpact->type) {
5807 return ofpact_get_OUTPUT(ofpact)->port == port;
5808 case OFPACT_ENQUEUE:
5809 return ofpact_get_ENQUEUE(ofpact)->port == port;
5810 case OFPACT_CONTROLLER:
5811 return port == OFPP_CONTROLLER;
5813 case OFPACT_OUTPUT_REG:
5815 case OFPACT_SET_VLAN_VID:
5816 case OFPACT_SET_VLAN_PCP:
5817 case OFPACT_STRIP_VLAN:
5818 case OFPACT_PUSH_VLAN:
5819 case OFPACT_SET_ETH_SRC:
5820 case OFPACT_SET_ETH_DST:
5821 case OFPACT_SET_IPV4_SRC:
5822 case OFPACT_SET_IPV4_DST:
5823 case OFPACT_SET_IP_DSCP:
5824 case OFPACT_SET_IP_ECN:
5825 case OFPACT_SET_IP_TTL:
5826 case OFPACT_SET_L4_SRC_PORT:
5827 case OFPACT_SET_L4_DST_PORT:
5828 case OFPACT_REG_MOVE:
5829 case OFPACT_SET_FIELD:
5830 case OFPACT_STACK_PUSH:
5831 case OFPACT_STACK_POP:
5832 case OFPACT_DEC_TTL:
5833 case OFPACT_SET_MPLS_LABEL:
5834 case OFPACT_SET_MPLS_TC:
5835 case OFPACT_SET_MPLS_TTL:
5836 case OFPACT_DEC_MPLS_TTL:
5837 case OFPACT_SET_TUNNEL:
5838 case OFPACT_WRITE_METADATA:
5839 case OFPACT_SET_QUEUE:
5840 case OFPACT_POP_QUEUE:
5841 case OFPACT_FIN_TIMEOUT:
5842 case OFPACT_RESUBMIT:
5844 case OFPACT_MULTIPATH:
5847 case OFPACT_PUSH_MPLS:
5848 case OFPACT_POP_MPLS:
5850 case OFPACT_CLEAR_ACTIONS:
5851 case OFPACT_WRITE_ACTIONS:
5852 case OFPACT_GOTO_TABLE:
5860 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
5861 * to 'port', false otherwise. */
5863 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
5866 const struct ofpact *a;
5868 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5869 if (ofpact_outputs_to_port(a, port)) {
5877 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
5878 * to 'group', false otherwise. */
5880 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
5883 const struct ofpact *a;
5885 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5886 if (a->type == OFPACT_GROUP
5887 && ofpact_get_GROUP(a)->group_id == group_id) {
5896 ofpacts_equal(const struct ofpact *a, size_t a_len,
5897 const struct ofpact *b, size_t b_len)
5899 return a_len == b_len && !memcmp(a, b, a_len);
5902 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
5903 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
5905 * This function relies on the order of 'ofpacts' being correct (as checked by
5906 * ofpacts_verify()). */
5908 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
5910 const struct ofpact *a;
5912 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5913 enum ovs_instruction_type inst;
5915 inst = ovs_instruction_type_from_ofpact_type(a->type);
5916 if (a->type == OFPACT_METER) {
5917 return ofpact_get_METER(a)->meter_id;
5918 } else if (inst > OVSINST_OFPIT13_METER) {
5926 /* Formatting ofpacts. */
5929 ofpact_format(const struct ofpact *a, struct ds *s)
5932 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5933 case OFPACT_##ENUM: \
5934 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
5943 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
5944 * 'ofpacts' to 'string'. */
5946 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
5950 ds_put_cstr(string, "drop");
5952 const struct ofpact *a;
5954 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5956 ds_put_cstr(string, ",");
5959 /* XXX write-actions */
5960 ofpact_format(a, string);
5965 /* Internal use by helpers. */
5968 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
5970 struct ofpact *ofpact;
5972 ofpact_pad(ofpacts);
5973 ofpact = ofpacts->frame = ofpbuf_put_uninit(ofpacts, len);
5974 ofpact_init(ofpact, type, len);
5979 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
5981 memset(ofpact, 0, len);
5982 ofpact->type = type;
5987 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
5988 * starting at 'ofpact'.
5990 * This is the correct way to update a variable-length ofpact's length after
5991 * adding the variable-length part of the payload. (See the large comment
5992 * near the end of ofp-actions.h for more information.) */
5994 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
5996 ovs_assert(ofpact == ofpacts->frame);
5997 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6000 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6001 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6002 * client must call this itself after adding the final ofpact to an array of
6005 * (The consequences of failing to call this function are probably not dire.
6006 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6007 * not dereference it. That's undefined behavior, technically, but it will not
6008 * cause a real problem on common systems. Still, it seems better to call
6011 ofpact_pad(struct ofpbuf *ofpacts)
6013 unsigned int pad = PAD_SIZE(ofpbuf_size(ofpacts), OFPACT_ALIGNTO);
6015 ofpbuf_put_zeros(ofpacts, pad);
6022 static char * WARN_UNUSED_RESULT
6023 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6024 enum ofputil_protocol *usable_protocols)
6027 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6028 case OFPACT_##ENUM: \
6029 return parse_##ENUM(value, ofpacts, usable_protocols);
6038 ofpact_type_from_name(const char *name, enum ofpact_type *type)
6040 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6041 if (!strcasecmp(name, NAME)) { \
6042 *type = OFPACT_##ENUM; \
6051 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6053 * Returns NULL if successful, otherwise a malloc()'d string describing the
6054 * error. The caller is responsible for freeing the returned string. */
6055 static char * WARN_UNUSED_RESULT
6056 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6057 enum ofputil_protocol *usable_protocols,
6058 bool allow_instructions)
6067 while (ofputil_parse_key_value(&pos, &key, &value)) {
6068 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6069 enum ofpact_type type;
6073 if (ofpact_type_from_name(key, &type)) {
6074 error = ofpact_parse(type, value, ofpacts, usable_protocols);
6075 inst = ovs_instruction_type_from_ofpact_type(type);
6076 } else if (!strcasecmp(key, "mod_vlan_vid")) {
6077 error = parse_set_vlan_vid(value, ofpacts, true);
6078 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6079 error = parse_set_vlan_pcp(value, ofpacts, true);
6080 } else if (!strcasecmp(key, "set_nw_ttl")) {
6081 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6082 } else if (!strcasecmp(key, "pop_vlan")) {
6083 error = parse_pop_vlan(ofpacts);
6084 } else if (!strcasecmp(key, "set_tunnel64")) {
6085 error = parse_set_tunnel(value, ofpacts,
6086 NXAST_RAW_SET_TUNNEL64);
6087 } else if (!strcasecmp(key, "load")) {
6088 error = parse_reg_load(value, ofpacts);
6089 } else if (!strcasecmp(key, "bundle_load")) {
6090 error = parse_bundle_load(value, ofpacts);
6091 } else if (!strcasecmp(key, "drop")) {
6093 } else if (!strcasecmp(key, "apply_actions")) {
6094 return xstrdup("apply_actions is the default instruction");
6095 } else if (ofputil_port_from_string(key, &port)) {
6096 ofpact_put_OUTPUT(ofpacts)->port = port;
6098 return xasprintf("unknown action %s", key);
6104 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
6105 if (!allow_instructions) {
6106 return xasprintf("only actions are allowed here (not "
6108 ovs_instruction_name_from_type(inst));
6110 if (inst == prev_inst) {
6111 return xasprintf("instruction %s may be specified only once",
6112 ovs_instruction_name_from_type(inst));
6115 if (prev_inst != -1 && inst < prev_inst) {
6116 return xasprintf("instruction %s must be specified before %s",
6117 ovs_instruction_name_from_type(inst),
6118 ovs_instruction_name_from_type(prev_inst));
6122 ofpact_pad(ofpacts);
6124 if (drop && ofpbuf_size(ofpacts)) {
6125 return xstrdup("\"drop\" must not be accompanied by any other action "
6129 retval = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
6131 ? (1u << N_OVS_INSTRUCTIONS) - 1
6132 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS));
6134 return xstrdup("Incorrect instruction ordering");
6140 static char * WARN_UNUSED_RESULT
6141 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
6142 enum ofputil_protocol *usable_protocols, bool allow_instructions)
6144 uint32_t orig_size = ofpbuf_size(ofpacts);
6145 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
6146 allow_instructions);
6148 ofpbuf_set_size(ofpacts, orig_size);
6153 static char * WARN_UNUSED_RESULT
6154 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
6155 enum ofputil_protocol *usable_protocols,
6156 bool allow_instructions)
6160 *usable_protocols = OFPUTIL_P_ANY;
6163 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions);
6169 /* Parses 's' as a set of OpenFlow actions and appends the actions to
6172 * Returns NULL if successful, otherwise a malloc()'d string describing the
6173 * error. The caller is responsible for freeing the returned string. */
6174 char * WARN_UNUSED_RESULT
6175 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
6176 enum ofputil_protocol *usable_protocols)
6178 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false);
6181 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
6184 * Returns NULL if successful, otherwise a malloc()'d string describing the
6185 * error. The caller is responsible for freeing the returned string. */
6186 char * WARN_UNUSED_RESULT
6187 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
6188 enum ofputil_protocol *usable_protocols)
6190 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true);
6194 ofpact_name(enum ofpact_type type)
6197 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
6204 /* Low-level action decoding and encoding functions. */
6206 /* Everything needed to identify a particular OpenFlow action. */
6207 struct ofpact_hdrs {
6208 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
6209 uint16_t type; /* Type if standard, otherwise subtype. */
6210 uint8_t ofp_version; /* From ofp_header. */
6213 /* Information about a particular OpenFlow action. */
6214 struct ofpact_raw_instance {
6215 /* The action's identity. */
6216 struct ofpact_hdrs hdrs;
6217 enum ofp_raw_action_type raw;
6219 /* Looking up the action. */
6220 struct hmap_node decode_node; /* Based on 'hdrs'. */
6221 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
6223 /* The action's encoded size.
6225 * If this action is fixed-length, 'min_length' == 'max_length'.
6226 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
6227 * OFP_ACTION_ALIGN) == 65528. */
6228 unsigned short int min_length;
6229 unsigned short int max_length;
6231 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
6232 * offset of that argument from the beginning of the action and 'arg_len'
6233 * its length, both in bytes.
6235 * For actions that take other forms, these are both zero. */
6236 unsigned short int arg_ofs;
6237 unsigned short int arg_len;
6239 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
6242 /* If this action is deprecated, a human-readable string with a brief
6244 const char *deprecation;
6247 /* Action header. */
6248 struct ofp_action_header {
6249 /* The meaning of other values of 'type' generally depends on the OpenFlow
6250 * version (see enum ofp_raw_action_type).
6252 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
6253 * designates an OpenFlow vendor ID and that the remainder of the action
6254 * structure has a vendor-defined meaning.
6256 #define OFPAT_VENDOR 0xffff
6259 /* Always a multiple of 8. */
6262 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
6263 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
6266 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
6268 /* Header for Nicira-defined actions. */
6269 struct nx_action_header {
6270 ovs_be16 type; /* OFPAT_VENDOR. */
6271 ovs_be16 len; /* At least 16. */
6272 ovs_be32 vendor; /* NX_VENDOR_ID. */
6273 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
6276 OFP_ASSERT(sizeof(struct nx_action_header) == 16);
6279 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
6280 const struct ofpact_hdrs *b)
6282 return (a->vendor == b->vendor
6283 && a->type == b->type
6284 && a->ofp_version == b->ofp_version);
6288 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
6290 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
6293 #include "ofp-actions.inc2"
6295 static struct hmap *
6296 ofpact_decode_hmap(void)
6298 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6299 static struct hmap hmap;
6301 if (ovsthread_once_start(&once)) {
6302 struct ofpact_raw_instance *inst;
6305 for (inst = all_raw_instances;
6306 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6308 hmap_insert(&hmap, &inst->decode_node,
6309 ofpact_hdrs_hash(&inst->hdrs));
6311 ovsthread_once_done(&once);
6316 static struct hmap *
6317 ofpact_encode_hmap(void)
6319 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6320 static struct hmap hmap;
6322 if (ovsthread_once_start(&once)) {
6323 struct ofpact_raw_instance *inst;
6326 for (inst = all_raw_instances;
6327 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6329 hmap_insert(&hmap, &inst->encode_node,
6330 hash_2words(inst->raw, inst->hdrs.ofp_version));
6332 ovsthread_once_done(&once);
6338 ofpact_decode_raw(enum ofp_version ofp_version,
6339 const struct ofp_action_header *oah, size_t length,
6340 const struct ofpact_raw_instance **instp)
6342 const struct ofpact_raw_instance *inst;
6343 struct ofpact_hdrs hdrs;
6346 if (length < sizeof *oah) {
6347 return OFPERR_OFPBAC_BAD_LEN;
6350 /* Get base action type. */
6351 if (oah->type == htons(OFPAT_VENDOR)) {
6353 hdrs.vendor = ntohl(oah->vendor);
6354 if (hdrs.vendor == NX_VENDOR_ID) {
6355 /* Get Nicira action type. */
6356 const struct nx_action_header *nah;
6358 nah = ALIGNED_CAST(const struct nx_action_header *, oah);
6359 if (length < sizeof *nah) {
6360 return OFPERR_OFPBAC_BAD_LEN;
6362 hdrs.type = ntohs(nah->subtype);
6364 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
6366 return OFPERR_OFPBAC_BAD_VENDOR;
6370 hdrs.type = ntohs(oah->type);
6373 hdrs.ofp_version = ofp_version;
6374 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
6375 ofpact_decode_hmap()) {
6376 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
6383 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
6384 : OFPERR_OFPBAC_BAD_TYPE);
6388 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6389 enum ofp_raw_action_type *raw, uint64_t *arg)
6391 const struct ofp_action_header *oah = ofpbuf_data(buf);
6392 const struct ofpact_raw_instance *action;
6393 unsigned int length;
6397 error = ofpact_decode_raw(ofp_version, oah, ofpbuf_size(buf), &action);
6402 if (action->deprecation) {
6403 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
6404 action->name, ofputil_version_to_string(ofp_version),
6405 action->deprecation);
6408 length = ntohs(oah->len);
6409 if (length < action->min_length || length > action->max_length) {
6410 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
6411 "[%hu,%hu]", action->name, length,
6412 action->min_length, action->max_length);
6413 return OFPERR_OFPBAC_BAD_LEN;
6416 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
6417 "of 8", action->name, length);
6418 return OFPERR_OFPBAC_BAD_LEN;
6423 if (action->arg_len) {
6427 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
6428 for (i = 0; i < action->arg_len; i++) {
6429 *arg = (*arg << 8) | p[i];
6433 ofpbuf_pull(buf, length);
6438 static const struct ofpact_raw_instance *
6439 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
6441 const struct ofpact_raw_instance *inst;
6443 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
6444 ofpact_encode_hmap()) {
6445 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
6453 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6454 enum ofp_raw_action_type raw, uint64_t arg)
6456 const struct ofpact_raw_instance *inst;
6457 struct ofp_action_header *oah;
6458 const struct ofpact_hdrs *hdrs;
6460 inst = ofpact_raw_lookup(ofp_version, raw);
6463 oah = ofpbuf_put_zeros(buf, inst->min_length);
6464 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
6465 oah->len = htons(inst->min_length);
6466 oah->vendor = htonl(hdrs->vendor);
6468 switch (hdrs->vendor) {
6472 case NX_VENDOR_ID: {
6473 struct nx_action_header *nah = (struct nx_action_header *) oah;
6474 nah->subtype = htons(hdrs->type);
6482 if (inst->arg_len) {
6483 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
6486 for (i = 0; i < inst->arg_len; i++) {
6498 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
6500 struct ofp_action_header *oah;
6502 ofpbuf_put_zeros(openflow, PAD_SIZE(ofpbuf_size(openflow) - start_ofs, 8));
6504 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
6505 oah->len = htons(ofpbuf_size(openflow) - start_ofs);