2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-actions.h"
20 #include "byte-order.h"
23 #include "dynamic-string.h"
26 #include "meta-flow.h"
27 #include "multipath.h"
29 #include "ofp-parse.h"
32 #include "unaligned.h"
34 #include "openvswitch/vlog.h"
36 VLOG_DEFINE_THIS_MODULE(ofp_actions);
38 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
40 struct ofp_action_header;
42 /* Raw identifiers for OpenFlow actions.
44 * Decoding and encoding OpenFlow actions across multiple versions is difficult
45 * to do in a clean, consistent way. This enumeration lays out all of the
46 * forms of actions that Open vSwitch supports.
48 * The comments here must follow a stylized form because the
49 * "extract-ofp-actions" program parses them at build time to generate data
52 * - The first part of each comment specifies the vendor, OpenFlow versions,
53 * and type for each protocol that supports the action:
55 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
56 * extension actions. (Support for other vendors can be added, but
57 * it can't be done just based on a vendor ID definition alone
58 * because OpenFlow doesn't define a standard way to specify a
59 * subtype for vendor actions, so other vendors might do it different
62 * # The version can specify a specific OpenFlow version, a version
63 * range delimited by "-", or an open-ended range with "+".
65 * # The type, in parentheses, is the action type number (for standard
66 * OpenFlow actions) or subtype (for vendor extension actions).
68 * # Optionally one may add "is deprecated" followed by a
69 * human-readable reason in parentheses (which will be used in log
70 * messages), if a particular action should no longer be used.
72 * Multiple such specifications may be separated by commas.
74 * - The second part describes the action's wire format. It may be:
76 * # "struct <name>": The struct fully specifies the wire format. The
77 * action is exactly the size of the struct. (Thus, the struct must
78 * be an exact multiple of 8 bytes in size.)
80 * # "struct <name>, ...": The struct specifies the beginning of the
81 * wire format. An instance of the action is either the struct's
82 * exact size, or a multiple of 8 bytes longer.
84 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
85 * vendor extension) header, followed by 0 or more pad bytes to align
86 * to a multiple of <N> bits, followed by an argument of the given
87 * type, followed by 0 or more pad bytes to bring the total action up
88 * to a multiple of 8 bytes.
90 * # "void": The action is just a (standard or vendor extension)
93 * - Optional additional text enclosed in square brackets is commentary for
96 enum ofp_raw_action_type {
97 /* ## ----------------- ## */
98 /* ## Standard actions. ## */
99 /* ## ----------------- ## */
101 /* OF1.0(0): struct ofp10_action_output. */
103 /* OF1.1+(0): struct ofp11_action_output. */
106 /* OF1.0(1): uint16_t. */
107 OFPAT_RAW10_SET_VLAN_VID,
108 /* OF1.0(2): uint8_t. */
109 OFPAT_RAW10_SET_VLAN_PCP,
111 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
113 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
114 * modification actions: the 1.0 versions push a VLAN header if none is
115 * present, but the 1.1 versions do not. That is the only reason that we
116 * distinguish their raw action types.] */
117 OFPAT_RAW11_SET_VLAN_VID,
118 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
119 OFPAT_RAW11_SET_VLAN_PCP,
121 /* OF1.1+(17): ovs_be16.
123 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
125 OFPAT_RAW11_PUSH_VLAN,
127 /* OF1.0(3): void. */
128 OFPAT_RAW10_STRIP_VLAN,
129 /* OF1.1+(18): void. */
130 OFPAT_RAW11_POP_VLAN,
132 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
133 * ofp_action_dl_addr. */
134 OFPAT_RAW_SET_DL_SRC,
136 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
137 * ofp_action_dl_addr. */
138 OFPAT_RAW_SET_DL_DST,
140 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
142 OFPAT_RAW_SET_NW_SRC,
144 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
146 OFPAT_RAW_SET_NW_DST,
148 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
149 OFPAT_RAW_SET_NW_TOS,
151 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
152 OFPAT_RAW11_SET_NW_ECN,
154 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
156 OFPAT_RAW_SET_TP_SRC,
158 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
160 OFPAT_RAW_SET_TP_DST,
162 /* OF1.0(11): struct ofp10_action_enqueue. */
165 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
167 OFPAT_RAW_SET_MPLS_LABEL,
169 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
171 OFPAT_RAW_SET_MPLS_TC,
173 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
175 OFPAT_RAW_SET_MPLS_TTL,
177 /* NX1.0(26), OF1.1+(16): void. */
178 OFPAT_RAW_DEC_MPLS_TTL,
180 /* NX1.0(23), OF1.1+(19): ovs_be16.
182 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
185 /* NX1.0(24), OF1.1+(20): ovs_be16.
187 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
188 * ETH_TYPE_MPLS otherwise, not the label.] */
191 /* NX1.0(4), OF1.1+(21): uint32_t. */
194 /* OF1.1+(22): uint32_t. */
197 /* OF1.1+(23): uint8_t. */
198 OFPAT_RAW11_SET_NW_TTL,
200 /* NX1.0(18), OF1.1+(24): void. */
201 OFPAT_RAW_DEC_NW_TTL,
202 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
203 NXAST_RAW_DEC_TTL_CNT_IDS,
205 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
206 OFPAT_RAW12_SET_FIELD,
207 /* OF1.5+(25): struct ofp12_action_set_field, ... */
208 OFPAT_RAW15_SET_FIELD,
209 /* NX1.0-1.4(7): struct nx_action_reg_load.
211 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
212 * we drop reg_load.] */
214 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
216 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
217 * we drop reg_load2.] */
220 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
221 OFPAT_RAW15_COPY_FIELD,
222 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
223 ONFACT_RAW13_COPY_FIELD,
224 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
227 /* ## ------------------------- ## */
228 /* ## Nicira extension actions. ## */
229 /* ## ------------------------- ## */
231 /* Actions similar to standard actions are listed with the standard actions. */
233 /* NX1.0+(1): uint16_t. */
235 /* NX1.0+(14): struct nx_action_resubmit. */
236 NXAST_RAW_RESUBMIT_TABLE,
238 /* NX1.0+(2): uint32_t. */
239 NXAST_RAW_SET_TUNNEL,
240 /* NX1.0+(9): uint64_t. */
241 NXAST_RAW_SET_TUNNEL64,
243 /* NX1.0+(5): void. */
246 /* NX1.0+(8): struct nx_action_note, ... */
249 /* NX1.0+(10): struct nx_action_multipath. */
252 /* NX1.0+(12): struct nx_action_bundle, ... */
254 /* NX1.0+(13): struct nx_action_bundle, ... */
255 NXAST_RAW_BUNDLE_LOAD,
257 /* NX1.0+(15): struct nx_action_output_reg. */
258 NXAST_RAW_OUTPUT_REG,
259 /* NX1.0+(32): struct nx_action_output_reg2. */
260 NXAST_RAW_OUTPUT_REG2,
262 /* NX1.0+(16): struct nx_action_learn, ... */
265 /* NX1.0+(17): void. */
268 /* NX1.0+(19): struct nx_action_fin_timeout. */
269 NXAST_RAW_FIN_TIMEOUT,
271 /* NX1.0+(20): struct nx_action_controller. */
272 NXAST_RAW_CONTROLLER,
274 /* NX1.0+(22): struct nx_action_write_metadata. */
275 NXAST_RAW_WRITE_METADATA,
277 /* NX1.0+(27): struct nx_action_stack. */
278 NXAST_RAW_STACK_PUSH,
280 /* NX1.0+(28): struct nx_action_stack. */
283 /* NX1.0+(29): struct nx_action_sample. */
286 /* NX1.0+(34): struct nx_action_conjunction. */
287 NXAST_RAW_CONJUNCTION,
289 /* ## ------------------ ## */
290 /* ## Debugging actions. ## */
291 /* ## ------------------ ## */
293 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
294 /* accepts them only if started with --enable-dummy. */
296 /* NX1.0+(255): void. */
297 NXAST_RAW_DEBUG_RECIRC,
300 /* OpenFlow actions are always a multiple of 8 bytes in length. */
301 #define OFP_ACTION_ALIGN 8
303 /* Define a few functions for working with instructions. */
304 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
305 static inline const struct STRUCT * OVS_UNUSED \
306 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
308 ovs_assert(inst->type == htons(ENUM)); \
309 return ALIGNED_CAST(struct STRUCT *, inst); \
312 static inline void OVS_UNUSED \
313 instruction_init_##ENUM(struct STRUCT *s) \
315 memset(s, 0, sizeof *s); \
316 s->type = htons(ENUM); \
317 s->len = htons(sizeof *s); \
320 static inline struct STRUCT * OVS_UNUSED \
321 instruction_put_##ENUM(struct ofpbuf *buf) \
323 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
324 instruction_init_##ENUM(s); \
330 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
332 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
334 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
335 uint32_t allowed_ovsinsts);
337 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
338 enum mf_field_id, uint64_t value);
340 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
341 enum ofp_raw_action_type *, uint64_t *arg);
342 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
343 enum ofp_raw_action_type, uint64_t arg);
345 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
346 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
347 bool allow_instructions);
349 #include "ofp-actions.inc1"
351 /* Output actions. */
353 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
354 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
355 * number of bytes to send. A 'max_len' of zero means no bytes of the
356 * packet should be sent. */
357 struct ofp10_action_output {
358 ovs_be16 type; /* OFPAT10_OUTPUT. */
359 ovs_be16 len; /* Length is 8. */
360 ovs_be16 port; /* Output port. */
361 ovs_be16 max_len; /* Max length to send to controller. */
363 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
365 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
366 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
367 * number of bytes to send. A 'max_len' of zero means no bytes of the
368 * packet should be sent.*/
369 struct ofp11_action_output {
370 ovs_be16 type; /* OFPAT11_OUTPUT. */
371 ovs_be16 len; /* Length is 16. */
372 ovs_be32 port; /* Output port. */
373 ovs_be16 max_len; /* Max length to send to controller. */
374 uint8_t pad[6]; /* Pad to 64 bits. */
376 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
379 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
382 struct ofpact_output *output;
384 output = ofpact_put_OUTPUT(out);
385 output->port = u16_to_ofp(ntohs(oao->port));
386 output->max_len = ntohs(oao->max_len);
388 return ofpact_check_output_port(output->port, OFPP_MAX);
392 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
395 struct ofpact_output *output;
398 output = ofpact_put_OUTPUT(out);
399 output->max_len = ntohs(oao->max_len);
401 error = ofputil_port_from_ofp11(oao->port, &output->port);
406 return ofpact_check_output_port(output->port, OFPP_MAX);
410 encode_OUTPUT(const struct ofpact_output *output,
411 enum ofp_version ofp_version, struct ofpbuf *out)
413 if (ofp_version == OFP10_VERSION) {
414 struct ofp10_action_output *oao;
416 oao = put_OFPAT10_OUTPUT(out);
417 oao->port = htons(ofp_to_u16(output->port));
418 oao->max_len = htons(output->max_len);
420 struct ofp11_action_output *oao;
422 oao = put_OFPAT11_OUTPUT(out);
423 oao->port = ofputil_port_to_ofp11(output->port);
424 oao->max_len = htons(output->max_len);
428 static char * OVS_WARN_UNUSED_RESULT
429 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
430 enum ofputil_protocol *usable_protocols OVS_UNUSED)
432 if (strchr(arg, '[')) {
433 struct ofpact_output_reg *output_reg;
435 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
436 output_reg->max_len = UINT16_MAX;
437 return mf_parse_subfield(&output_reg->src, arg);
439 struct ofpact_output *output;
441 output = ofpact_put_OUTPUT(ofpacts);
442 if (!ofputil_port_from_string(arg, &output->port)) {
443 return xasprintf("%s: output to unknown port", arg);
445 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
451 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
453 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
454 ds_put_format(s, "output:%"PRIu16, a->port);
456 ofputil_format_port(a->port, s);
457 if (a->port == OFPP_CONTROLLER) {
458 ds_put_format(s, ":%"PRIu16, a->max_len);
466 decode_OFPAT_RAW11_GROUP(uint32_t group_id, struct ofpbuf *out)
468 ofpact_put_GROUP(out)->group_id = group_id;
473 encode_GROUP(const struct ofpact_group *group,
474 enum ofp_version ofp_version, struct ofpbuf *out)
476 if (ofp_version == OFP10_VERSION) {
479 put_OFPAT11_GROUP(out, group->group_id);
483 static char * OVS_WARN_UNUSED_RESULT
484 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
485 enum ofputil_protocol *usable_protocols OVS_UNUSED)
487 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
491 format_GROUP(const struct ofpact_group *a, struct ds *s)
493 ds_put_format(s, "group:%"PRIu32, a->group_id);
496 /* Action structure for NXAST_CONTROLLER.
498 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
499 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
502 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
504 * - 'controller_id': The ID of the controller connection to which the
505 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
506 * sent only to controllers that have the specified controller connection
507 * ID. See "struct nx_controller_id" for more information. */
508 struct nx_action_controller {
509 ovs_be16 type; /* OFPAT_VENDOR. */
510 ovs_be16 len; /* Length is 16. */
511 ovs_be32 vendor; /* NX_VENDOR_ID. */
512 ovs_be16 subtype; /* NXAST_CONTROLLER. */
513 ovs_be16 max_len; /* Maximum length to send to controller. */
514 ovs_be16 controller_id; /* Controller ID to send packet-in. */
515 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
516 uint8_t zero; /* Must be zero. */
518 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
521 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
524 struct ofpact_controller *oc;
526 oc = ofpact_put_CONTROLLER(out);
527 oc->max_len = ntohs(nac->max_len);
528 oc->controller_id = ntohs(nac->controller_id);
529 oc->reason = nac->reason;
534 encode_CONTROLLER(const struct ofpact_controller *controller,
535 enum ofp_version ofp_version OVS_UNUSED,
538 struct nx_action_controller *nac;
540 nac = put_NXAST_CONTROLLER(out);
541 nac->max_len = htons(controller->max_len);
542 nac->controller_id = htons(controller->controller_id);
543 nac->reason = controller->reason;
546 static char * OVS_WARN_UNUSED_RESULT
547 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
548 enum ofputil_protocol *usable_protocols OVS_UNUSED)
550 enum ofp_packet_in_reason reason = OFPR_ACTION;
551 uint16_t controller_id = 0;
552 uint16_t max_len = UINT16_MAX;
556 } else if (strspn(arg, "0123456789") == strlen(arg)) {
557 char *error = str_to_u16(arg, "max_len", &max_len);
564 while (ofputil_parse_key_value(&arg, &name, &value)) {
565 if (!strcmp(name, "reason")) {
566 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
567 return xasprintf("unknown reason \"%s\"", value);
569 } else if (!strcmp(name, "max_len")) {
570 char *error = str_to_u16(value, "max_len", &max_len);
574 } else if (!strcmp(name, "id")) {
575 char *error = str_to_u16(value, "id", &controller_id);
580 return xasprintf("unknown key \"%s\" parsing controller "
586 if (reason == OFPR_ACTION && controller_id == 0) {
587 struct ofpact_output *output;
589 output = ofpact_put_OUTPUT(ofpacts);
590 output->port = OFPP_CONTROLLER;
591 output->max_len = max_len;
593 struct ofpact_controller *controller;
595 controller = ofpact_put_CONTROLLER(ofpacts);
596 controller->max_len = max_len;
597 controller->reason = reason;
598 controller->controller_id = controller_id;
605 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
607 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
608 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
610 enum ofp_packet_in_reason reason = a->reason;
612 ds_put_cstr(s, "controller(");
613 if (reason != OFPR_ACTION) {
614 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
616 ds_put_format(s, "reason=%s,",
617 ofputil_packet_in_reason_to_string(
618 reason, reasonbuf, sizeof reasonbuf));
620 if (a->max_len != UINT16_MAX) {
621 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
623 if (a->controller_id != 0) {
624 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
631 /* Enqueue action. */
632 struct ofp10_action_enqueue {
633 ovs_be16 type; /* OFPAT10_ENQUEUE. */
634 ovs_be16 len; /* Len is 16. */
635 ovs_be16 port; /* Port that queue belongs. Should
636 refer to a valid physical port
637 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
638 uint8_t pad[6]; /* Pad for 64-bit alignment. */
639 ovs_be32 queue_id; /* Where to enqueue the packets. */
641 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
644 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
647 struct ofpact_enqueue *enqueue;
649 enqueue = ofpact_put_ENQUEUE(out);
650 enqueue->port = u16_to_ofp(ntohs(oae->port));
651 enqueue->queue = ntohl(oae->queue_id);
652 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
653 && enqueue->port != OFPP_IN_PORT
654 && enqueue->port != OFPP_LOCAL) {
655 return OFPERR_OFPBAC_BAD_OUT_PORT;
661 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
662 enum ofp_version ofp_version, struct ofpbuf *out)
664 if (ofp_version == OFP10_VERSION) {
665 struct ofp10_action_enqueue *oae;
667 oae = put_OFPAT10_ENQUEUE(out);
668 oae->port = htons(ofp_to_u16(enqueue->port));
669 oae->queue_id = htonl(enqueue->queue);
675 static char * OVS_WARN_UNUSED_RESULT
676 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
677 enum ofputil_protocol *usable_protocols OVS_UNUSED)
680 char *port = strtok_r(arg, ":q,", &sp);
681 char *queue = strtok_r(NULL, "", &sp);
682 struct ofpact_enqueue *enqueue;
684 if (port == NULL || queue == NULL) {
685 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
686 "\"enqueue(PORT,QUEUE)\"");
689 enqueue = ofpact_put_ENQUEUE(ofpacts);
690 if (!ofputil_port_from_string(port, &enqueue->port)) {
691 return xasprintf("%s: enqueue to unknown port", port);
693 return str_to_u32(queue, &enqueue->queue);
697 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
699 ds_put_format(s, "enqueue:");
700 ofputil_format_port(a->port, s);
701 ds_put_format(s, ":%"PRIu32, a->queue);
704 /* Action structure for NXAST_OUTPUT_REG.
706 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
708 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
709 * the NXAST_REG_LOAD action.
711 * The acceptable nxm_header values for 'src' are the same as the acceptable
712 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
714 * The 'max_len' field indicates the number of bytes to send when the chosen
715 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
716 * field of OFPAT_OUTPUT.
718 * The 'zero' field is required to be zeroed for forward compatibility. */
719 struct nx_action_output_reg {
720 ovs_be16 type; /* OFPAT_VENDOR. */
721 ovs_be16 len; /* 24. */
722 ovs_be32 vendor; /* NX_VENDOR_ID. */
723 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
725 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
726 ovs_be32 src; /* Source. */
728 ovs_be16 max_len; /* Max length to send to controller. */
730 uint8_t zero[6]; /* Reserved, must be zero. */
732 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
734 /* Action structure for NXAST_OUTPUT_REG2.
736 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
737 * experimenter OXM as 'src'.
739 struct nx_action_output_reg2 {
740 ovs_be16 type; /* OFPAT_VENDOR. */
741 ovs_be16 len; /* 24. */
742 ovs_be32 vendor; /* NX_VENDOR_ID. */
743 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
745 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
746 ovs_be16 max_len; /* Max length to send to controller. */
749 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
750 * - Enough 0-bytes to pad the action out to 24 bytes. */
753 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
756 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
759 struct ofpact_output_reg *output_reg;
761 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
762 return OFPERR_OFPBAC_BAD_ARGUMENT;
765 output_reg = ofpact_put_OUTPUT_REG(out);
766 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
767 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
768 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
769 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
770 output_reg->max_len = ntohs(naor->max_len);
772 return mf_check_src(&output_reg->src, NULL);
776 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
779 struct ofpact_output_reg *output_reg;
783 output_reg = ofpact_put_OUTPUT_REG(out);
784 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
785 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
786 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
787 output_reg->max_len = ntohs(naor->max_len);
789 ofpbuf_use_const(&b, naor, ntohs(naor->len));
790 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
791 error = nx_pull_header(&b, &output_reg->src.field, NULL);
795 if (!is_all_zeros(b.data, b.size)) {
796 return OFPERR_NXBRC_MUST_BE_ZERO;
799 return mf_check_src(&output_reg->src, NULL);
803 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
804 enum ofp_version ofp_version OVS_UNUSED,
807 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
808 * cannot be encoded in the older form, encode it as
809 * NXAST_RAW_OUTPUT_REG2. */
810 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
811 || !mf_nxm_header(output_reg->src.field->id)) {
812 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
813 size_t size = out->size;
815 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
816 output_reg->src.n_bits);
817 naor->max_len = htons(output_reg->max_len);
819 out->size = size - sizeof naor->pad;
820 nx_put_header(out, output_reg->src.field->id, 0, false);
823 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
825 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
826 output_reg->src.n_bits);
827 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
828 naor->max_len = htons(output_reg->max_len);
832 static char * OVS_WARN_UNUSED_RESULT
833 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
834 enum ofputil_protocol *usable_protocols OVS_UNUSED)
836 return parse_OUTPUT(arg, ofpacts, usable_protocols);
840 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
842 ds_put_cstr(s, "output:");
843 mf_format_subfield(&a->src, s);
846 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
848 * The bundle actions choose a slave from a supplied list of options.
849 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
850 * selection to a register.
852 * The list of possible slaves follows the nx_action_bundle structure. The size
853 * of each slave is governed by its type as indicated by the 'slave_type'
854 * parameter. The list of slaves should be padded at its end with zeros to make
855 * the total length of the action a multiple of 8.
857 * Switches infer from the 'slave_type' parameter the size of each slave. All
858 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
859 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
860 * 2 byte width. Switches should reject actions which indicate unknown or
861 * unsupported slave types.
863 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
864 * slave. If the switch does not support the specified 'algorithm' parameter,
865 * it should reject the action.
867 * Several algorithms take into account liveness when selecting slaves. The
868 * liveness of a slave is implementation defined (with one exception), but will
869 * generally take into account things like its carrier status and the results
870 * of any link monitoring protocols which happen to be running on it. In order
871 * to give controllers a place-holder value, the OFPP_NONE port is always
874 * Some slave selection strategies require the use of a hash function, in which
875 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
876 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
877 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
878 * 'basis' parameter is used as a universal hash parameter. Different values
879 * of 'basis' yield different hash results.
881 * The 'zero' parameter at the end of the action structure is reserved for
882 * future use. Switches are required to reject actions which have nonzero
883 * bytes in the 'zero' field.
885 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
886 * should reject actions which have nonzero bytes in either of these fields.
888 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
889 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
890 * similar to those for the NXAST_REG_LOAD action. */
891 struct nx_action_bundle {
892 ovs_be16 type; /* OFPAT_VENDOR. */
893 ovs_be16 len; /* Length including slaves. */
894 ovs_be32 vendor; /* NX_VENDOR_ID. */
895 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
897 /* Slave choice algorithm to apply to hash value. */
898 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
900 /* What fields to hash and how. */
901 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
902 ovs_be16 basis; /* Universal hash parameter. */
904 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
905 ovs_be16 n_slaves; /* Number of slaves. */
907 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
908 ovs_be32 dst; /* Destination. */
910 uint8_t zero[4]; /* Reserved. Must be zero. */
912 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
915 decode_bundle(bool load, const struct nx_action_bundle *nab,
916 struct ofpbuf *ofpacts)
918 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
919 struct ofpact_bundle *bundle;
921 size_t slaves_size, i;
924 bundle = ofpact_put_BUNDLE(ofpacts);
926 bundle->n_slaves = ntohs(nab->n_slaves);
927 bundle->basis = ntohs(nab->basis);
928 bundle->fields = ntohs(nab->fields);
929 bundle->algorithm = ntohs(nab->algorithm);
930 slave_type = ntohl(nab->slave_type);
931 slaves_size = ntohs(nab->len) - sizeof *nab;
933 error = OFPERR_OFPBAC_BAD_ARGUMENT;
934 if (!flow_hash_fields_valid(bundle->fields)) {
935 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
936 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
937 VLOG_WARN_RL(&rl, "too many slaves");
938 } else if (bundle->algorithm != NX_BD_ALG_HRW
939 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
940 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
941 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
942 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
947 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
948 VLOG_WARN_RL(&rl, "reserved field is nonzero");
949 error = OFPERR_OFPBAC_BAD_ARGUMENT;
953 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
954 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
955 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
957 if (bundle->dst.n_bits < 16) {
958 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
960 error = OFPERR_OFPBAC_BAD_ARGUMENT;
963 if (nab->ofs_nbits || nab->dst) {
964 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
965 error = OFPERR_OFPBAC_BAD_ARGUMENT;
969 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
970 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
971 "allocated for slaves. %"PRIuSIZE" bytes are required "
972 "for %"PRIu16" slaves.",
973 load ? "bundle_load" : "bundle", slaves_size,
974 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
975 error = OFPERR_OFPBAC_BAD_LEN;
978 for (i = 0; i < bundle->n_slaves; i++) {
979 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
980 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
983 bundle = ofpacts->header;
984 ofpact_update_len(ofpacts, &bundle->ofpact);
987 error = bundle_check(bundle, OFPP_MAX, NULL);
993 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab, struct ofpbuf *out)
995 return decode_bundle(false, nab, out);
999 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1002 return decode_bundle(true, nab, out);
1006 encode_BUNDLE(const struct ofpact_bundle *bundle,
1007 enum ofp_version ofp_version OVS_UNUSED,
1010 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1011 struct nx_action_bundle *nab;
1015 nab = (bundle->dst.field
1016 ? put_NXAST_BUNDLE_LOAD(out)
1017 : put_NXAST_BUNDLE(out));
1018 nab->len = htons(ntohs(nab->len) + slaves_len);
1019 nab->algorithm = htons(bundle->algorithm);
1020 nab->fields = htons(bundle->fields);
1021 nab->basis = htons(bundle->basis);
1022 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1023 nab->n_slaves = htons(bundle->n_slaves);
1024 if (bundle->dst.field) {
1025 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1026 bundle->dst.n_bits);
1027 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1030 slaves = ofpbuf_put_zeros(out, slaves_len);
1031 for (i = 0; i < bundle->n_slaves; i++) {
1032 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1036 static char * OVS_WARN_UNUSED_RESULT
1037 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1038 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1040 return bundle_parse(arg, ofpacts);
1043 static char * OVS_WARN_UNUSED_RESULT
1044 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1046 return bundle_parse_load(arg, ofpacts);
1050 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1052 bundle_format(a, s);
1055 /* Set VLAN actions. */
1058 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1061 return OFPERR_OFPBAC_BAD_ARGUMENT;
1063 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1064 vlan_vid->vlan_vid = vid;
1065 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1071 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid, struct ofpbuf *out)
1073 return decode_set_vlan_vid(vid, true, out);
1077 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid, struct ofpbuf *out)
1079 return decode_set_vlan_vid(vid, false, out);
1083 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1084 enum ofp_version ofp_version, struct ofpbuf *out)
1086 uint16_t vid = vlan_vid->vlan_vid;
1088 /* Push a VLAN tag, if none is present and this form of the action calls
1089 * for such a feature. */
1090 if (ofp_version > OFP10_VERSION
1091 && vlan_vid->push_vlan_if_needed
1092 && !vlan_vid->flow_has_vlan) {
1093 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1096 if (ofp_version == OFP10_VERSION) {
1097 put_OFPAT10_SET_VLAN_VID(out, vid);
1098 } else if (ofp_version == OFP11_VERSION) {
1099 put_OFPAT11_SET_VLAN_VID(out, vid);
1101 ofpact_put_set_field(out, ofp_version,
1102 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1106 static char * OVS_WARN_UNUSED_RESULT
1107 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1109 struct ofpact_vlan_vid *vlan_vid;
1113 error = str_to_u16(arg, "VLAN VID", &vid);
1118 if (vid & ~VLAN_VID_MASK) {
1119 return xasprintf("%s: not a valid VLAN VID", arg);
1121 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1122 vlan_vid->vlan_vid = vid;
1123 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1127 static char * OVS_WARN_UNUSED_RESULT
1128 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1129 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1131 return parse_set_vlan_vid(arg, ofpacts, false);
1135 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1137 ds_put_format(s, "%s:%"PRIu16,
1138 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1142 /* Set PCP actions. */
1145 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1148 return OFPERR_OFPBAC_BAD_ARGUMENT;
1150 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1151 vlan_pcp->vlan_pcp = pcp;
1152 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1158 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp, struct ofpbuf *out)
1160 return decode_set_vlan_pcp(pcp, true, out);
1164 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp, struct ofpbuf *out)
1166 return decode_set_vlan_pcp(pcp, false, out);
1170 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1171 enum ofp_version ofp_version, struct ofpbuf *out)
1173 uint8_t pcp = vlan_pcp->vlan_pcp;
1175 /* Push a VLAN tag, if none is present and this form of the action calls
1176 * for such a feature. */
1177 if (ofp_version > OFP10_VERSION
1178 && vlan_pcp->push_vlan_if_needed
1179 && !vlan_pcp->flow_has_vlan) {
1180 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1183 if (ofp_version == OFP10_VERSION) {
1184 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1185 } else if (ofp_version == OFP11_VERSION) {
1186 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1188 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1192 static char * OVS_WARN_UNUSED_RESULT
1193 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1195 struct ofpact_vlan_pcp *vlan_pcp;
1199 error = str_to_u8(arg, "VLAN PCP", &pcp);
1205 return xasprintf("%s: not a valid VLAN PCP", arg);
1207 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1208 vlan_pcp->vlan_pcp = pcp;
1209 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1213 static char * OVS_WARN_UNUSED_RESULT
1214 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1215 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1217 return parse_set_vlan_pcp(arg, ofpacts, false);
1221 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1223 ds_put_format(s, "%s:%"PRIu8,
1224 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1228 /* Strip VLAN actions. */
1231 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1233 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1238 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1240 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1245 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1246 enum ofp_version ofp_version, struct ofpbuf *out)
1248 if (ofp_version == OFP10_VERSION) {
1249 put_OFPAT10_STRIP_VLAN(out);
1251 put_OFPAT11_POP_VLAN(out);
1255 static char * OVS_WARN_UNUSED_RESULT
1256 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1257 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1259 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1263 static char * OVS_WARN_UNUSED_RESULT
1264 parse_pop_vlan(struct ofpbuf *ofpacts)
1266 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1271 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1273 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1278 /* Push VLAN action. */
1281 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type, struct ofpbuf *out)
1283 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1284 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1285 return OFPERR_OFPBAC_BAD_ARGUMENT;
1287 ofpact_put_PUSH_VLAN(out);
1292 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1293 enum ofp_version ofp_version, struct ofpbuf *out)
1295 if (ofp_version == OFP10_VERSION) {
1296 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1297 * follow this action. */
1299 /* XXX ETH_TYPE_VLAN_8021AD case */
1300 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1304 static char * OVS_WARN_UNUSED_RESULT
1305 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1306 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1311 *usable_protocols &= OFPUTIL_P_OF11_UP;
1312 error = str_to_u16(arg, "ethertype", ðertype);
1317 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1318 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1319 return xasprintf("%s: not a valid VLAN ethertype", arg);
1322 ofpact_put_PUSH_VLAN(ofpacts);
1327 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1329 /* XXX 802.1AD case*/
1330 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1333 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1334 struct ofp_action_dl_addr {
1335 ovs_be16 type; /* Type. */
1336 ovs_be16 len; /* Length is 16. */
1337 uint8_t dl_addr[OFP_ETH_ALEN]; /* Ethernet address. */
1340 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1343 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1346 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr, ETH_ADDR_LEN);
1351 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1354 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr, ETH_ADDR_LEN);
1359 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1360 enum ofp_raw_action_type raw, enum mf_field_id field,
1363 const uint8_t *addr = mac->mac;
1365 if (ofp_version < OFP12_VERSION) {
1366 struct ofp_action_dl_addr *oada;
1368 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1369 memcpy(oada->dl_addr, addr, ETH_ADDR_LEN);
1371 ofpact_put_set_field(out, ofp_version, field,
1372 eth_addr_to_uint64(addr));
1377 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1380 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1386 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1387 enum ofp_version ofp_version,
1390 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1395 static char * OVS_WARN_UNUSED_RESULT
1396 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1397 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1399 return str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1402 static char * OVS_WARN_UNUSED_RESULT
1403 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1404 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1406 return str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
1410 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1412 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1416 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1418 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1421 /* Set IPv4 address actions. */
1424 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4, struct ofpbuf *out)
1426 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1431 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4, struct ofpbuf *out)
1433 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1438 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1439 enum ofp_version ofp_version,
1440 enum ofp_raw_action_type raw, enum mf_field_id field,
1443 ovs_be32 addr = ipv4->ipv4;
1444 if (ofp_version < OFP12_VERSION) {
1445 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1447 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1452 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1453 enum ofp_version ofp_version, struct ofpbuf *out)
1455 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1460 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1461 enum ofp_version ofp_version, struct ofpbuf *out)
1463 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1467 static char * OVS_WARN_UNUSED_RESULT
1468 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1469 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1471 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1474 static char * OVS_WARN_UNUSED_RESULT
1475 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1476 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1478 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1482 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1484 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1488 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1490 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1493 /* Set IPv4/v6 TOS actions. */
1496 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp, struct ofpbuf *out)
1498 if (dscp & ~IP_DSCP_MASK) {
1499 return OFPERR_OFPBAC_BAD_ARGUMENT;
1501 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1507 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1508 enum ofp_version ofp_version, struct ofpbuf *out)
1510 if (ofp_version < OFP12_VERSION) {
1511 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1513 ofpact_put_set_field(out, ofp_version,
1514 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1518 static char * OVS_WARN_UNUSED_RESULT
1519 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1520 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1525 error = str_to_u8(arg, "TOS", &tos);
1530 if (tos & ~IP_DSCP_MASK) {
1531 return xasprintf("%s: not a valid TOS", arg);
1533 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1538 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1540 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1543 /* Set IPv4/v6 ECN actions. */
1546 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn, struct ofpbuf *out)
1548 if (ecn & ~IP_ECN_MASK) {
1549 return OFPERR_OFPBAC_BAD_ARGUMENT;
1551 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1557 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1558 enum ofp_version ofp_version, struct ofpbuf *out)
1560 uint8_t ecn = ip_ecn->ecn;
1561 if (ofp_version == OFP10_VERSION) {
1563 } else if (ofp_version == OFP11_VERSION) {
1564 put_OFPAT11_SET_NW_ECN(out, ecn);
1566 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1570 static char * OVS_WARN_UNUSED_RESULT
1571 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1572 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1577 error = str_to_u8(arg, "ECN", &ecn);
1582 if (ecn & ~IP_ECN_MASK) {
1583 return xasprintf("%s: not a valid ECN", arg);
1585 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1590 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1592 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1595 /* Set IPv4/v6 TTL actions. */
1598 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl, struct ofpbuf *out)
1600 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1605 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1606 enum ofp_version ofp_version, struct ofpbuf *out)
1608 if (ofp_version >= OFP11_VERSION) {
1609 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1615 static char * OVS_WARN_UNUSED_RESULT
1616 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1617 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1622 error = str_to_u8(arg, "TTL", &ttl);
1627 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1632 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1634 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1637 /* Set TCP/UDP/SCTP port actions. */
1640 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port, struct ofpbuf *out)
1642 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1647 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port, struct ofpbuf *out)
1649 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1654 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1655 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1656 enum mf_field_id field, struct ofpbuf *out)
1658 uint16_t port = l4_port->port;
1660 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1661 ofpact_put_set_field(out, ofp_version, field, port);
1663 ofpact_put_raw(out, ofp_version, raw, port);
1668 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1669 enum ofp_version ofp_version, struct ofpbuf *out)
1671 uint8_t proto = l4_port->flow_ip_proto;
1672 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1673 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1674 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1677 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1681 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1682 enum ofp_version ofp_version,
1685 uint8_t proto = l4_port->flow_ip_proto;
1686 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1687 : proto == IPPROTO_UDP ? MFF_UDP_DST
1688 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1691 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1694 static char * OVS_WARN_UNUSED_RESULT
1695 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1696 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1698 return str_to_u16(arg, "source port",
1699 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1702 static char * OVS_WARN_UNUSED_RESULT
1703 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1704 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1706 return str_to_u16(arg, "destination port",
1707 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1711 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1713 ds_put_format(s, "mod_tp_src:%d", a->port);
1717 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1719 ds_put_format(s, "mod_tp_dst:%d", a->port);
1722 /* Action structure for OFPAT_COPY_FIELD. */
1723 struct ofp15_action_copy_field {
1724 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1725 ovs_be16 len; /* Length is padded to 64 bits. */
1726 ovs_be16 n_bits; /* Number of bits to copy. */
1727 ovs_be16 src_offset; /* Starting bit offset in source. */
1728 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1731 * - OXM header for source field.
1732 * - OXM header for destination field.
1733 * - Padding with 0-bytes to a multiple of 8 bytes.
1734 * The "pad2" member is the beginning of the above. */
1737 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1739 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1740 struct onf_action_copy_field {
1741 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
1742 ovs_be16 len; /* Length is padded to 64 bits. */
1743 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
1744 ovs_be16 exp_type; /* 3200. */
1745 uint8_t pad[2]; /* Not used. */
1746 ovs_be16 n_bits; /* Number of bits to copy. */
1747 ovs_be16 src_offset; /* Starting bit offset in source. */
1748 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1749 uint8_t pad2[2]; /* Not used. */
1751 * - OXM header for source field.
1752 * - OXM header for destination field.
1753 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1754 * The "pad3" member is the beginning of the above. */
1755 uint8_t pad3[4]; /* Not used. */
1757 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1759 /* Action structure for NXAST_REG_MOVE.
1761 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1762 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1763 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1764 * the next most significant bit, and so on.
1766 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1767 * sense to use nxm_hasmask=1 because the action does not do any kind of
1768 * matching; it uses the actual value of a field.)
1770 * The following nxm_header values are potentially acceptable as 'src':
1785 * - NXM_OF_ICMP_TYPE
1786 * - NXM_OF_ICMP_CODE
1793 * - NXM_NX_ICMPV6_TYPE
1794 * - NXM_NX_ICMPV6_CODE
1797 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1799 * - NXM_NX_TUN_IPV4_SRC
1800 * - NXM_NX_TUN_IPV4_DST
1802 * The following nxm_header values are potentially acceptable as 'dst':
1818 * Modifying any of the above fields changes the corresponding packet
1823 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1827 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1828 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1829 * header (if any), ignoring the other bits. Setting a value with CFI=1
1830 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1831 * to the field's new value (with the CFI bit masked out).
1833 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1834 * any of these values modifies the corresponding tunnel header field used
1835 * for the packet's next tunnel encapsulation, if allowed by the
1836 * configuration of the output tunnel port.
1838 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1839 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1840 * used only if the flow's nx_match includes an nxm_entry that specifies
1841 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1843 * The switch will reject actions for which src_ofs+n_bits is greater than the
1844 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1845 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1847 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1848 * behaves as if 'src' were copied out to a temporary buffer, then the
1849 * temporary buffer copied to 'dst'.
1851 struct nx_action_reg_move {
1852 ovs_be16 type; /* OFPAT_VENDOR. */
1853 ovs_be16 len; /* Length is 24. */
1854 ovs_be32 vendor; /* NX_VENDOR_ID. */
1855 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1856 ovs_be16 n_bits; /* Number of bits. */
1857 ovs_be16 src_ofs; /* Starting bit offset in source. */
1858 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1860 * - OXM/NXM header for source field (4 or 8 bytes).
1861 * - OXM/NXM header for destination field (4 or 8 bytes).
1862 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
1864 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
1867 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
1868 const void *action, ovs_be16 action_len, size_t oxm_offset,
1869 struct ofpbuf *ofpacts)
1871 struct ofpact_reg_move *move;
1875 move = ofpact_put_REG_MOVE(ofpacts);
1876 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
1877 move->src.ofs = ntohs(src_offset);
1878 move->src.n_bits = ntohs(n_bits);
1879 move->dst.ofs = ntohs(dst_offset);
1880 move->dst.n_bits = ntohs(n_bits);
1882 ofpbuf_use_const(&b, action, ntohs(action_len));
1883 ofpbuf_pull(&b, oxm_offset);
1884 error = nx_pull_header(&b, &move->src.field, NULL);
1888 error = nx_pull_header(&b, &move->dst.field, NULL);
1893 if (!is_all_zeros(b.data, b.size)) {
1894 return OFPERR_NXBRC_MUST_BE_ZERO;
1897 return nxm_reg_move_check(move, NULL);
1901 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
1902 struct ofpbuf *ofpacts)
1904 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1905 oacf->n_bits, oacf, oacf->len,
1906 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
1910 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
1911 struct ofpbuf *ofpacts)
1913 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1914 oacf->n_bits, oacf, oacf->len,
1915 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
1919 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
1920 struct ofpbuf *ofpacts)
1922 struct ofpact_reg_move *move;
1926 move = ofpact_put_REG_MOVE(ofpacts);
1927 move->ofpact.raw = NXAST_RAW_REG_MOVE;
1928 move->src.ofs = ntohs(narm->src_ofs);
1929 move->src.n_bits = ntohs(narm->n_bits);
1930 move->dst.ofs = ntohs(narm->dst_ofs);
1931 move->dst.n_bits = ntohs(narm->n_bits);
1933 ofpbuf_use_const(&b, narm, ntohs(narm->len));
1934 ofpbuf_pull(&b, sizeof *narm);
1935 error = nx_pull_header(&b, &move->src.field, NULL);
1939 error = nx_pull_header(&b, &move->dst.field, NULL);
1943 if (!is_all_zeros(b.data, b.size)) {
1944 return OFPERR_NXBRC_MUST_BE_ZERO;
1947 return nxm_reg_move_check(move, NULL);
1951 encode_REG_MOVE(const struct ofpact_reg_move *move,
1952 enum ofp_version ofp_version, struct ofpbuf *out)
1954 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
1955 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
1956 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
1957 * probe for support. Until we have that ability, we currently prefer
1958 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
1960 size_t start_ofs = out->size;
1961 if (ofp_version >= OFP15_VERSION) {
1962 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
1963 copy->n_bits = htons(move->dst.n_bits);
1964 copy->src_offset = htons(move->src.ofs);
1965 copy->dst_offset = htons(move->dst.ofs);
1966 out->size = out->size - sizeof copy->pad2;
1967 nx_put_header(out, move->src.field->id, ofp_version, false);
1968 nx_put_header(out, move->dst.field->id, ofp_version, false);
1969 } else if (ofp_version == OFP13_VERSION
1970 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
1971 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
1972 copy->n_bits = htons(move->dst.n_bits);
1973 copy->src_offset = htons(move->src.ofs);
1974 copy->dst_offset = htons(move->dst.ofs);
1975 out->size = out->size - sizeof copy->pad3;
1976 nx_put_header(out, move->src.field->id, ofp_version, false);
1977 nx_put_header(out, move->dst.field->id, ofp_version, false);
1979 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
1980 narm->n_bits = htons(move->dst.n_bits);
1981 narm->src_ofs = htons(move->src.ofs);
1982 narm->dst_ofs = htons(move->dst.ofs);
1983 nx_put_header(out, move->src.field->id, 0, false);
1984 nx_put_header(out, move->dst.field->id, 0, false);
1986 pad_ofpat(out, start_ofs);
1989 static char * OVS_WARN_UNUSED_RESULT
1990 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
1991 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1993 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
1994 const char *full_arg = arg;
1997 error = mf_parse_subfield__(&move->src, &arg);
2001 if (strncmp(arg, "->", 2)) {
2002 return xasprintf("%s: missing `->' following source", full_arg);
2005 error = mf_parse_subfield(&move->dst, arg);
2010 if (move->src.n_bits != move->dst.n_bits) {
2011 return xasprintf("%s: source field is %d bits wide but destination is "
2012 "%d bits wide", full_arg,
2013 move->src.n_bits, move->dst.n_bits);
2019 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2021 nxm_format_reg_move(a, s);
2024 /* Action structure for OFPAT12_SET_FIELD. */
2025 struct ofp12_action_set_field {
2026 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2027 ovs_be16 len; /* Length is padded to 64 bits. */
2030 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2031 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2033 * The "pad" member is the beginning of the above. */
2036 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2038 /* Action structure for NXAST_REG_LOAD.
2040 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2041 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2042 * starts at 0 for the least-significant bit, 1 for the next most significant
2045 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2046 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2049 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2050 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2051 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2052 * also stored as one less than its true value:
2055 * +------------------------------+------------------+
2056 * | ofs | n_bits - 1 |
2057 * +------------------------------+------------------+
2059 * The switch will reject actions for which ofs+n_bits is greater than the
2060 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2061 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2062 * OFPBAC_BAD_ARGUMENT.
2064 struct nx_action_reg_load {
2065 ovs_be16 type; /* OFPAT_VENDOR. */
2066 ovs_be16 len; /* Length is 24. */
2067 ovs_be32 vendor; /* NX_VENDOR_ID. */
2068 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2069 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2070 ovs_be32 dst; /* Destination register. */
2071 ovs_be64 value; /* Immediate value. */
2073 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2075 /* Action structure for NXAST_REG_LOAD2.
2077 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2078 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2079 * OXM experimenter fields. */
2080 struct nx_action_reg_load2 {
2081 ovs_be16 type; /* OFPAT_VENDOR. */
2082 ovs_be16 len; /* At least 16. */
2083 ovs_be32 vendor; /* NX_VENDOR_ID. */
2084 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2087 * - An NXM/OXM header, value, and optionally a mask.
2088 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2090 * The "pad" member is the beginning of the above. */
2093 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2096 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2097 bool may_mask, struct ofpbuf *ofpacts)
2099 struct ofpact_set_field *sf;
2103 sf = ofpact_put_SET_FIELD(ofpacts);
2105 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2106 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2107 error = nx_pull_entry(&b, &sf->field, &sf->value,
2108 may_mask ? &sf->mask : NULL);
2110 return (error == OFPERR_OFPBMC_BAD_MASK
2111 ? OFPERR_OFPBAC_BAD_SET_MASK
2115 memset(&sf->mask, 0xff, sf->field->n_bytes);
2118 if (!is_all_zeros(b.data, b.size)) {
2119 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2122 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2124 if (sf->field->id == MFF_IN_PORT_OXM) {
2125 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2128 /* oxm_length is now validated to be compatible with mf_value. */
2129 if (!sf->field->writable) {
2130 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2132 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2135 /* The value must be valid for match. OpenFlow 1.5 also says,
2136 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2137 * a 1-bit in oxm_value and in oxm_mask." */
2138 if (!mf_is_value_valid(sf->field, &sf->value)
2139 || (sf->field->id == MFF_VLAN_VID
2140 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2141 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2142 struct ds ds = DS_EMPTY_INITIALIZER;
2143 mf_format(sf->field, &sf->value, NULL, &ds);
2144 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2145 sf->field->name, ds_cstr(&ds));
2148 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2154 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2155 struct ofpbuf *ofpacts)
2157 return decode_ofpat_set_field(oasf, false, ofpacts);
2161 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2162 struct ofpbuf *ofpacts)
2164 return decode_ofpat_set_field(oasf, true, ofpacts);
2168 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2171 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2172 struct mf_subfield dst;
2175 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2177 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2178 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2179 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2180 error = mf_check_dst(&dst, NULL);
2185 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2187 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2188 return OFPERR_OFPBAC_BAD_ARGUMENT;
2191 sf->field = dst.field;
2192 bitwise_put(ntohll(narl->value),
2193 &sf->value, dst.field->n_bytes, dst.ofs,
2195 bitwise_put(UINT64_MAX,
2196 &sf->mask, dst.field->n_bytes, dst.ofs,
2203 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2206 struct ofpact_set_field *sf;
2210 sf = ofpact_put_SET_FIELD(out);
2211 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2213 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2214 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2215 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2219 if (!is_all_zeros(b.data, b.size)) {
2220 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2223 if (!sf->field->writable) {
2224 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2226 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2232 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2233 enum mf_field_id field, uint64_t value_)
2235 struct ofp12_action_set_field *oasf OVS_UNUSED;
2236 int n_bytes = mf_from_id(field)->n_bytes;
2237 size_t start_ofs = openflow->size;
2238 union mf_value value;
2240 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2242 oasf = put_OFPAT12_SET_FIELD(openflow);
2243 openflow->size = openflow->size - sizeof oasf->pad;
2244 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2245 pad_ofpat(openflow, start_ofs);
2249 next_load_segment(const struct ofpact_set_field *sf,
2250 struct mf_subfield *dst, uint64_t *value)
2252 int n_bits = sf->field->n_bits;
2253 int n_bytes = sf->field->n_bytes;
2254 int start = dst->ofs + dst->n_bits;
2256 if (start < n_bits) {
2257 dst->field = sf->field;
2258 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2259 if (dst->ofs < n_bits) {
2260 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2261 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2262 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2269 /* Convert 'sf' to a series of REG_LOADs. */
2271 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2273 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2274 * experimenter OXM (or if it came in as NXAST_REG_LOAD2), encode as
2275 * NXAST_REG_LOAD2. Otherwise use NXAST_REG_LOAD, which is backward
2277 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2278 || !mf_nxm_header(sf->field->id)) {
2279 struct nx_action_reg_load2 *narl OVS_UNUSED;
2280 size_t start_ofs = openflow->size;
2282 narl = put_NXAST_REG_LOAD2(openflow);
2283 openflow->size = openflow->size - sizeof narl->pad;
2284 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2285 pad_ofpat(openflow, start_ofs);
2287 struct mf_subfield dst;
2290 dst.ofs = dst.n_bits = 0;
2291 while (next_load_segment(sf, &dst, &value)) {
2292 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2293 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2294 narl->dst = htonl(mf_nxm_header(dst.field->id));
2295 narl->value = htonll(value);
2300 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2301 * actions, if we can, falling back to Nicira extensions if we must.
2303 * We check only meta-flow types that can appear within set field actions and
2304 * that have a mapping to compatible action types. These struct mf_field
2305 * definitions have a defined OXM or NXM header value and specify the field as
2308 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2309 enum ofp_version ofp_version,
2312 switch ((int) sf->field->id) {
2313 case MFF_VLAN_TCI: {
2314 ovs_be16 tci = sf->value.be16;
2315 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2316 uint16_t vid = vlan_tci_to_vid(tci);
2317 uint8_t pcp = vlan_tci_to_pcp(tci);
2319 if (ofp_version < OFP11_VERSION) {
2320 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2322 * If CFI=1, Add or modify VLAN VID & PCP.
2323 * If CFI=0, strip VLAN header, if any.
2326 put_OFPAT10_SET_VLAN_VID(out, vid);
2327 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2329 put_OFPAT10_STRIP_VLAN(out);
2332 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2334 * If CFI=1, Add or modify VLAN VID & PCP.
2335 * OpenFlow 1.1 set actions only apply if the packet
2336 * already has VLAN tags. To be sure that is the case
2337 * we have to push a VLAN header. As we do not support
2338 * multiple layers of VLANs, this is a no-op, if a VLAN
2339 * header already exists. This may backfire, however,
2340 * when we start supporting multiple layers of VLANs.
2341 * If CFI=0, strip VLAN header, if any.
2344 /* Push a VLAN tag, if one was not seen at action validation
2346 if (!sf->flow_has_vlan) {
2347 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2349 put_OFPAT11_SET_VLAN_VID(out, vid);
2350 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2352 /* If the flow did not match on vlan, we have no way of
2353 * knowing if the vlan tag exists, so we must POP just to be
2355 put_OFPAT11_POP_VLAN(out);
2361 case MFF_VLAN_VID: {
2362 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2363 if (ofp_version == OFP10_VERSION) {
2364 put_OFPAT10_SET_VLAN_VID(out, vid);
2366 put_OFPAT11_SET_VLAN_VID(out, vid);
2372 if (ofp_version == OFP10_VERSION) {
2373 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2375 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2380 memcpy(put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr,
2381 sf->value.mac, ETH_ADDR_LEN);
2385 memcpy(put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr,
2386 sf->value.mac, ETH_ADDR_LEN);
2390 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2394 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2398 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2401 case MFF_IP_DSCP_SHIFTED:
2402 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2407 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2412 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2416 set_field_to_nxast(sf, out);
2422 set_field_to_set_field(const struct ofpact_set_field *sf,
2423 enum ofp_version ofp_version, struct ofpbuf *out)
2425 struct ofp12_action_set_field *oasf OVS_UNUSED;
2426 size_t start_ofs = out->size;
2428 oasf = put_OFPAT12_SET_FIELD(out);
2429 out->size = out->size - sizeof oasf->pad;
2430 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2431 pad_ofpat(out, start_ofs);
2435 encode_SET_FIELD(const struct ofpact_set_field *sf,
2436 enum ofp_version ofp_version, struct ofpbuf *out)
2438 if (ofp_version >= OFP15_VERSION) {
2439 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2441 set_field_to_set_field(sf, ofp_version, out);
2442 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2443 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2444 /* It came in as reg_load, send it out the same way. */
2445 set_field_to_nxast(sf, out);
2446 } else if (ofp_version < OFP12_VERSION) {
2447 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2448 set_field_to_legacy_openflow(sf, ofp_version, out);
2449 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2450 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2451 * entire field, so encode it as OFPAT_SET_FIELD. */
2452 set_field_to_set_field(sf, ofp_version, out);
2454 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2455 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2456 * so encode it as reg_load. */
2457 set_field_to_nxast(sf, out);
2461 /* Parses a "set_field" action with argument 'arg', appending the parsed
2462 * action to 'ofpacts'.
2464 * Returns NULL if successful, otherwise a malloc()'d string describing the
2465 * error. The caller is responsible for freeing the returned string. */
2466 static char * OVS_WARN_UNUSED_RESULT
2467 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2468 enum ofputil_protocol *usable_protocols)
2470 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2474 const struct mf_field *mf;
2478 delim = strstr(arg, "->");
2480 return xasprintf("%s: missing `->'", arg);
2482 if (strlen(delim) <= strlen("->")) {
2483 return xasprintf("%s: missing field name following `->'", arg);
2486 key = delim + strlen("->");
2487 mf = mf_from_name(key);
2489 return xasprintf("%s is not a valid OXM field name", key);
2491 if (!mf->writable) {
2492 return xasprintf("%s is read-only", key);
2496 error = mf_parse(mf, value, &sf->value, &sf->mask);
2501 if (!mf_is_value_valid(mf, &sf->value)) {
2502 return xasprintf("%s is not a valid value for field %s", value, key);
2505 *usable_protocols &= mf->usable_protocols_exact;
2509 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2510 * action to 'ofpacts'.
2512 * Returns NULL if successful, otherwise a malloc()'d string describing the
2513 * error. The caller is responsible for freeing the returned string. */
2514 static char * OVS_WARN_UNUSED_RESULT
2515 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2516 enum ofputil_protocol *usable_protocols)
2518 char *copy = xstrdup(arg);
2519 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2524 static char * OVS_WARN_UNUSED_RESULT
2525 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2527 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2528 const char *full_arg = arg;
2529 uint64_t value = strtoull(arg, (char **) &arg, 0);
2530 struct mf_subfield dst;
2533 if (strncmp(arg, "->", 2)) {
2534 return xasprintf("%s: missing `->' following value", full_arg);
2537 error = mf_parse_subfield(&dst, arg);
2542 if (dst.n_bits < 64 && (value >> dst.n_bits) != 0) {
2543 return xasprintf("%s: value %"PRIu64" does not fit into %d bits",
2544 full_arg, value, dst.n_bits);
2547 sf->field = dst.field;
2548 memset(&sf->value, 0, sizeof sf->value);
2549 bitwise_put(value, &sf->value, dst.field->n_bytes, dst.ofs, dst.n_bits);
2550 bitwise_put(UINT64_MAX, &sf->mask,
2551 dst.field->n_bytes, dst.ofs, dst.n_bits);
2556 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2558 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2559 struct mf_subfield dst;
2562 dst.ofs = dst.n_bits = 0;
2563 while (next_load_segment(a, &dst, &value)) {
2564 ds_put_format(s, "load:%#"PRIx64"->", value);
2565 mf_format_subfield(&dst, s);
2566 ds_put_char(s, ',');
2570 ds_put_cstr(s, "set_field:");
2571 mf_format(a->field, &a->value, &a->mask, s);
2572 ds_put_format(s, "->%s", a->field->name);
2576 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2577 * is marked such that, if possible, it will be translated to OpenFlow as
2578 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2579 * that was the way that the action was expressed when it came into OVS or for
2580 * backward compatibility. */
2581 struct ofpact_set_field *
2582 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2584 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2585 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2589 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2591 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2594 struct nx_action_stack {
2595 ovs_be16 type; /* OFPAT_VENDOR. */
2596 ovs_be16 len; /* Length is 16. */
2597 ovs_be32 vendor; /* NX_VENDOR_ID. */
2598 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2599 ovs_be16 offset; /* Bit offset into the field. */
2601 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2602 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2603 * - Enough 0-bytes to pad out the action to 24 bytes. */
2604 uint8_t pad[12]; /* See above. */
2606 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2609 decode_stack_action(const struct nx_action_stack *nasp,
2610 struct ofpact_stack *stack_action)
2615 stack_action->subfield.ofs = ntohs(nasp->offset);
2617 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2618 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2619 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2623 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2625 if (!is_all_zeros(b.data, b.size)) {
2626 return OFPERR_NXBRC_MUST_BE_ZERO;
2633 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2634 struct ofpbuf *ofpacts)
2636 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2637 enum ofperr error = decode_stack_action(nasp, push);
2638 return error ? error : nxm_stack_push_check(push, NULL);
2642 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2643 struct ofpbuf *ofpacts)
2645 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2646 enum ofperr error = decode_stack_action(nasp, pop);
2647 return error ? error : nxm_stack_pop_check(pop, NULL);
2651 encode_STACK_op(const struct ofpact_stack *stack_action,
2652 struct nx_action_stack *nasp)
2657 nasp->offset = htons(stack_action->subfield.ofs);
2659 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2660 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2661 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2662 n_bits = htons(stack_action->subfield.n_bits);
2663 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2667 encode_STACK_PUSH(const struct ofpact_stack *stack,
2668 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2670 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2674 encode_STACK_POP(const struct ofpact_stack *stack,
2675 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2677 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2680 static char * OVS_WARN_UNUSED_RESULT
2681 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2682 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2684 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2687 static char * OVS_WARN_UNUSED_RESULT
2688 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2689 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2691 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2695 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2697 nxm_format_stack_push(a, s);
2701 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2703 nxm_format_stack_pop(a, s);
2706 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2708 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2709 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2710 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2711 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2713 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2714 * sent only to controllers with id 0.)
2716 struct nx_action_cnt_ids {
2717 ovs_be16 type; /* OFPAT_VENDOR. */
2718 ovs_be16 len; /* Length including slaves. */
2719 ovs_be32 vendor; /* NX_VENDOR_ID. */
2720 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2722 ovs_be16 n_controllers; /* Number of controllers. */
2723 uint8_t zeros[4]; /* Must be zero. */
2725 /* Followed by 1 or more controller ids.
2727 * uint16_t cnt_ids[]; // Controller ids.
2728 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2731 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2734 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2737 struct ofpact_cnt_ids *ids;
2738 enum ofperr error = 0;
2740 ids = ofpact_put_DEC_TTL(out);
2741 ids->n_controllers = 1;
2742 ofpbuf_put(out, &id, sizeof id);
2744 ofpact_update_len(out, &ids->ofpact);
2749 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2752 struct ofpact_cnt_ids *ids;
2756 ids = ofpact_put_DEC_TTL(out);
2757 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2758 ids->n_controllers = ntohs(nac_ids->n_controllers);
2759 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2761 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2762 return OFPERR_NXBRC_MUST_BE_ZERO;
2765 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2766 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2767 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2768 "are required for %"PRIu16" controllers.",
2769 ids_size, ids->n_controllers * sizeof(ovs_be16),
2770 ids->n_controllers);
2771 return OFPERR_OFPBAC_BAD_LEN;
2774 for (i = 0; i < ids->n_controllers; i++) {
2775 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2776 ofpbuf_put(out, &id, sizeof id);
2780 ofpact_update_len(out, &ids->ofpact);
2786 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2787 enum ofp_version ofp_version, struct ofpbuf *out)
2789 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2790 || dec_ttl->n_controllers != 1
2791 || dec_ttl->cnt_ids[0] != 0) {
2792 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2793 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2797 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2798 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2800 ids = ofpbuf_put_zeros(out, ids_len);
2801 for (i = 0; i < dec_ttl->n_controllers; i++) {
2802 ids[i] = htons(dec_ttl->cnt_ids[i]);
2805 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2810 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2812 struct ofpact_cnt_ids *ids;
2815 ofpact_put_DEC_TTL(ofpacts);
2816 ofpbuf_put(ofpacts, &id, sizeof id);
2817 ids = ofpacts->header;
2818 ids->n_controllers++;
2819 ofpact_update_len(ofpacts, &ids->ofpact);
2822 static char * OVS_WARN_UNUSED_RESULT
2823 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2824 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2827 parse_noargs_dec_ttl(ofpacts);
2829 struct ofpact_cnt_ids *ids;
2832 ids = ofpact_put_DEC_TTL(ofpacts);
2833 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2834 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2835 cntr = strtok_r(NULL, ", ", &arg)) {
2836 uint16_t id = atoi(cntr);
2838 ofpbuf_put(ofpacts, &id, sizeof id);
2839 ids = ofpacts->header;
2840 ids->n_controllers++;
2842 if (!ids->n_controllers) {
2843 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2846 ofpact_update_len(ofpacts, &ids->ofpact);
2852 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2856 ds_put_cstr(s, "dec_ttl");
2857 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2858 ds_put_cstr(s, "(");
2859 for (i = 0; i < a->n_controllers; i++) {
2861 ds_put_cstr(s, ",");
2863 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2865 ds_put_cstr(s, ")");
2869 /* Set MPLS label actions. */
2872 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label, struct ofpbuf *out)
2874 ofpact_put_SET_MPLS_LABEL(out)->label = label;
2879 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
2880 enum ofp_version ofp_version,
2883 if (ofp_version < OFP12_VERSION) {
2884 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
2886 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
2887 ntohl(label->label));
2891 static char * OVS_WARN_UNUSED_RESULT
2892 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
2893 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2895 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
2897 return xstrdup("set_mpls_label: expected label.");
2900 mpls_label->label = htonl(atoi(arg));
2905 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
2907 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
2910 /* Set MPLS TC actions. */
2913 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc, struct ofpbuf *out)
2915 ofpact_put_SET_MPLS_TC(out)->tc = tc;
2920 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
2921 enum ofp_version ofp_version, struct ofpbuf *out)
2923 if (ofp_version < OFP12_VERSION) {
2924 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
2926 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
2930 static char * OVS_WARN_UNUSED_RESULT
2931 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
2932 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2934 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
2937 return xstrdup("set_mpls_tc: expected tc.");
2940 mpls_tc->tc = atoi(arg);
2945 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
2947 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
2950 /* Set MPLS TTL actions. */
2953 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl, struct ofpbuf *out)
2955 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
2960 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
2961 enum ofp_version ofp_version, struct ofpbuf *out)
2963 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
2966 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
2967 * action to 'ofpacts'.
2969 * Returns NULL if successful, otherwise a malloc()'d string describing the
2970 * error. The caller is responsible for freeing the returned string. */
2971 static char * OVS_WARN_UNUSED_RESULT
2972 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
2973 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2975 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
2978 return xstrdup("set_mpls_ttl: expected ttl.");
2981 mpls_ttl->ttl = atoi(arg);
2986 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
2988 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
2991 /* Decrement MPLS TTL actions. */
2994 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
2996 ofpact_put_DEC_MPLS_TTL(out);
3001 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3002 enum ofp_version ofp_version, struct ofpbuf *out)
3004 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3007 static char * OVS_WARN_UNUSED_RESULT
3008 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3009 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3011 ofpact_put_DEC_MPLS_TTL(ofpacts);
3016 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3018 ds_put_cstr(s, "dec_mpls_ttl");
3021 /* Push MPLS label action. */
3024 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
3026 struct ofpact_push_mpls *oam;
3028 if (!eth_type_mpls(ethertype)) {
3029 return OFPERR_OFPBAC_BAD_ARGUMENT;
3031 oam = ofpact_put_PUSH_MPLS(out);
3032 oam->ethertype = ethertype;
3038 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3039 enum ofp_version ofp_version, struct ofpbuf *out)
3041 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3044 static char * OVS_WARN_UNUSED_RESULT
3045 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3046 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3051 error = str_to_u16(arg, "push_mpls", ðertype);
3053 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3059 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3061 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3064 /* Pop MPLS label action. */
3067 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
3069 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3074 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3075 enum ofp_version ofp_version, struct ofpbuf *out)
3077 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3080 static char * OVS_WARN_UNUSED_RESULT
3081 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3082 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3087 error = str_to_u16(arg, "pop_mpls", ðertype);
3089 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3095 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3097 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3100 /* Set tunnel ID actions. */
3103 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id, struct ofpbuf *out)
3105 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3106 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3107 tunnel->tun_id = tun_id;
3112 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id, struct ofpbuf *out)
3114 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3115 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3116 tunnel->tun_id = tun_id;
3121 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3122 enum ofp_version ofp_version, struct ofpbuf *out)
3124 uint64_t tun_id = tunnel->tun_id;
3126 if (ofp_version < OFP12_VERSION) {
3127 if (tun_id <= UINT32_MAX
3128 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3129 put_NXAST_SET_TUNNEL(out, tun_id);
3131 put_NXAST_SET_TUNNEL64(out, tun_id);
3134 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3138 static char * OVS_WARN_UNUSED_RESULT
3139 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3140 enum ofp_raw_action_type raw)
3142 struct ofpact_tunnel *tunnel;
3144 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3145 tunnel->ofpact.raw = raw;
3146 return str_to_u64(arg, &tunnel->tun_id);
3149 static char * OVS_WARN_UNUSED_RESULT
3150 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3151 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3153 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3157 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3159 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3160 (a->tun_id > UINT32_MAX
3161 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3165 /* Set queue action. */
3168 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id, struct ofpbuf *out)
3170 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3175 encode_SET_QUEUE(const struct ofpact_queue *queue,
3176 enum ofp_version ofp_version, struct ofpbuf *out)
3178 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3181 static char * OVS_WARN_UNUSED_RESULT
3182 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3183 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3185 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3189 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3191 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3194 /* Pop queue action. */
3197 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3199 ofpact_put_POP_QUEUE(out);
3204 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3205 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3207 put_NXAST_POP_QUEUE(out);
3210 static char * OVS_WARN_UNUSED_RESULT
3211 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3212 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3214 ofpact_put_POP_QUEUE(ofpacts);
3219 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3221 ds_put_cstr(s, "pop_queue");
3224 /* Action structure for NXAST_FIN_TIMEOUT.
3226 * This action changes the idle timeout or hard timeout, or both, of this
3227 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3228 * When such a packet is observed, the action reduces the rule's idle timeout
3229 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3230 * action has no effect on an existing timeout that is already shorter than the
3231 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3232 * of zero has no effect on the respective timeout.
3234 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3235 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3236 * receipt of the FIN or RST.
3238 * This is useful for quickly discarding learned TCP flows that otherwise will
3239 * take a long time to expire.
3241 * This action is intended for use with an OpenFlow rule that matches only a
3242 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3243 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3244 * RST in any of those flows will cause the entire OpenFlow rule to expire
3245 * early, which is not normally desirable.
3247 struct nx_action_fin_timeout {
3248 ovs_be16 type; /* OFPAT_VENDOR. */
3249 ovs_be16 len; /* 16. */
3250 ovs_be32 vendor; /* NX_VENDOR_ID. */
3251 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3252 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3253 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3254 ovs_be16 pad; /* Must be zero. */
3256 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3259 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3262 struct ofpact_fin_timeout *oft;
3264 oft = ofpact_put_FIN_TIMEOUT(out);
3265 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3266 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3271 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3272 enum ofp_version ofp_version OVS_UNUSED,
3275 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3276 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3277 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3280 static char * OVS_WARN_UNUSED_RESULT
3281 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3282 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3284 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3287 while (ofputil_parse_key_value(&arg, &key, &value)) {
3290 if (!strcmp(key, "idle_timeout")) {
3291 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3292 } else if (!strcmp(key, "hard_timeout")) {
3293 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3295 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3307 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3309 ds_put_cstr(s, "fin_timeout(");
3310 if (a->fin_idle_timeout) {
3311 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3313 if (a->fin_hard_timeout) {
3314 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3317 ds_put_char(s, ')');
3320 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3322 * These actions search one of the switch's flow tables:
3324 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3325 * it specifies the table to search.
3327 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3328 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3329 * table, that is, the OpenFlow flow table that contains the flow from
3330 * which this action was obtained. If this action did not come from a
3331 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3332 * is the current table.
3334 * The flow table lookup uses a flow that may be slightly modified from the
3337 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3338 * is used as the flow's in_port.
3340 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3341 * then its value is used as the flow's in_port. Otherwise, the original
3344 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3345 * resubmit action, then the flow is updated with the new values.
3347 * Following the lookup, the original in_port is restored.
3349 * If the modified flow matched in the flow table, then the corresponding
3350 * actions are executed. Afterward, actions following the resubmit in the
3351 * original set of actions, if any, are executed; any changes made to the
3352 * packet (e.g. changes to VLAN) by secondary actions persist when those
3353 * actions are executed, although the original in_port is restored.
3355 * Resubmit actions may be used any number of times within a set of actions.
3357 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3358 * implementation-defined depth, further resubmit actions are simply ignored.
3360 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3361 * 'pad' to be all-bits-zero.
3363 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3364 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3366 struct nx_action_resubmit {
3367 ovs_be16 type; /* OFPAT_VENDOR. */
3368 ovs_be16 len; /* Length is 16. */
3369 ovs_be32 vendor; /* NX_VENDOR_ID. */
3370 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3371 ovs_be16 in_port; /* New in_port for checking flow table. */
3372 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3375 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3378 decode_NXAST_RAW_RESUBMIT(uint16_t port, struct ofpbuf *out)
3380 struct ofpact_resubmit *resubmit;
3382 resubmit = ofpact_put_RESUBMIT(out);
3383 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3384 resubmit->in_port = u16_to_ofp(port);
3385 resubmit->table_id = 0xff;
3390 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3393 struct ofpact_resubmit *resubmit;
3395 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3396 return OFPERR_OFPBAC_BAD_ARGUMENT;
3399 resubmit = ofpact_put_RESUBMIT(out);
3400 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3401 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3402 resubmit->table_id = nar->table;
3407 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3408 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3410 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3412 if (resubmit->table_id == 0xff
3413 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3414 put_NXAST_RESUBMIT(out, in_port);
3416 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3417 nar->table = resubmit->table_id;
3418 nar->in_port = htons(in_port);
3422 static char * OVS_WARN_UNUSED_RESULT
3423 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3424 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3426 struct ofpact_resubmit *resubmit;
3427 char *in_port_s, *table_s;
3429 resubmit = ofpact_put_RESUBMIT(ofpacts);
3431 in_port_s = strsep(&arg, ",");
3432 if (in_port_s && in_port_s[0]) {
3433 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3434 return xasprintf("%s: resubmit to unknown port", in_port_s);
3437 resubmit->in_port = OFPP_IN_PORT;
3440 table_s = strsep(&arg, ",");
3441 if (table_s && table_s[0]) {
3442 uint32_t table_id = 0;
3445 error = str_to_u32(table_s, &table_id);
3449 resubmit->table_id = table_id;
3451 resubmit->table_id = 255;
3454 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3455 return xstrdup("at least one \"in_port\" or \"table\" must be "
3456 "specified on resubmit");
3462 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3464 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3465 ds_put_cstr(s, "resubmit:");
3466 ofputil_format_port(a->in_port, s);
3468 ds_put_format(s, "resubmit(");
3469 if (a->in_port != OFPP_IN_PORT) {
3470 ofputil_format_port(a->in_port, s);
3472 ds_put_char(s, ',');
3473 if (a->table_id != 255) {
3474 ds_put_format(s, "%"PRIu8, a->table_id);
3476 ds_put_char(s, ')');
3480 /* Action structure for NXAST_LEARN.
3482 * This action adds or modifies a flow in an OpenFlow table, similar to
3483 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3484 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3485 * flow's match criteria and actions are built by applying each of the series
3486 * of flow_mod_spec elements included as part of the action.
3488 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3489 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3490 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3491 * bits from a source to a destination. In this case, the header contains
3494 * 15 14 13 12 11 10 0
3495 * +------+---+------+---------------------------------+
3496 * | 0 |src| dst | n_bits |
3497 * +------+---+------+---------------------------------+
3499 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3500 * following table summarizes the meaning of each possible combination.
3501 * Details follow the table:
3504 * --- --- ----------------------------------------------------------
3505 * 0 0 Add match criteria based on value in a field.
3506 * 1 0 Add match criteria based on an immediate value.
3507 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3508 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3509 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3510 * All other combinations are undefined and not allowed.
3512 * The flow_mod_spec header is followed by a source specification and a
3513 * destination specification. The format and meaning of the source
3514 * specification depends on 'src':
3516 * - If 'src' is 0, the source bits are taken from a field in the flow to
3517 * which this action is attached. (This should be a wildcarded field. If
3518 * its value is fully specified then the source bits being copied have
3521 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3522 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3523 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3524 * 'field' and 'ofs' are subject to the same restrictions as the source
3525 * field in NXAST_REG_MOVE.
3527 * - If 'src' is 1, the source bits are a constant value. The source
3528 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3529 * number in network order, the source bits are the 'n_bits'
3530 * least-significant bits. The switch will report an error if other bits
3531 * in the constant are nonzero.
3533 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3534 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3535 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3536 * meaning of the flow_mod_spec depends on 'dst':
3538 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3539 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3540 * packet equal the source bits. 'field' may be any nxm_header with
3541 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3543 * Order is significant. Earlier flow_mod_specs must satisfy any
3544 * prerequisites for matching fields specified later, by copying constant
3545 * values into prerequisite fields.
3547 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3550 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3551 * the new flow. The new flow copies the source bits into
3552 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3555 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3556 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3558 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3559 * empty. It has the following meaning:
3561 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3562 * The new flow outputs to the OpenFlow port specified by the source field.
3563 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3564 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3567 * Resource Management
3568 * -------------------
3570 * A switch has a finite amount of flow table space available for learning.
3571 * When this space is exhausted, no new learning table entries will be learned
3572 * until some existing flow table entries expire. The controller should be
3573 * prepared to handle this by flooding (which can be implemented as a
3574 * low-priority flow).
3576 * If a learned flow matches a single TCP stream with a relatively long
3577 * timeout, one may make the best of resource constraints by setting
3578 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3579 * both, to shorter timeouts. When either of these is specified as a nonzero
3580 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3586 * The following examples give a prose description of the flow_mod_specs along
3587 * with informal notation for how those would be represented and a hex dump of
3588 * the bytes that would be required.
3590 * These examples could work with various nx_action_learn parameters. Typical
3591 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3592 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3594 * 1. Learn input port based on the source MAC, with lookup into
3595 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3597 * Match on in_port=99:
3598 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3599 * ovs_be16(99), 00 63
3600 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3602 * Match Ethernet destination on Ethernet source from packet:
3603 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3604 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3605 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3607 * Set NXM_NX_REG1[16:31] to the packet's input port:
3608 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3609 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3610 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3612 * Given a packet that arrived on port A with Ethernet source address B,
3613 * this would set up the flow "in_port=99, dl_dst=B,
3614 * actions=load:A->NXM_NX_REG1[16..31]".
3616 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3617 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3618 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3620 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3621 * into NXM_NX_REG1[16:31]:
3623 * Match on same VLAN ID as packet:
3624 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3625 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3626 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3628 * Match Ethernet destination on Ethernet source from packet:
3629 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3630 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3631 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3633 * Output to the packet's input port:
3634 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3635 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3637 * Given a packet that arrived on port A with Ethernet source address B in
3638 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3639 * actions=output:A".
3641 * In syntax accepted by ovs-ofctl, this action is:
3642 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3643 * output:NXM_OF_IN_PORT[])
3645 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3646 * 10-second MAC expiration time to make it easier to see what's going on
3648 * ovs-vsctl del-controller br0
3649 * ovs-ofctl del-flows br0
3650 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3651 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3652 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3653 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3654 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3656 * You can then dump the MAC learning table with:
3658 * ovs-ofctl dump-flows br0 table=1
3663 * For best performance, segregate learned flows into a table that is not used
3664 * for any other flows except possibly for a lowest-priority "catch-all" flow
3665 * (a flow with no match criteria). If different learning actions specify
3666 * different match criteria, use different tables for the learned flows.
3668 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3669 * These timeouts apply to the flow that is added, which means that a flow with
3670 * an idle timeout will expire when no traffic has been sent *to* the learned
3671 * address. This is not usually the intent in MAC learning; instead, we want
3672 * the MAC learn entry to expire when no traffic has been sent *from* the
3673 * learned address. Use a hard timeout for that.
3676 * Visibility of Changes
3677 * ---------------------
3679 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3680 * flow translation are visible to flow table lookups made later in the flow
3681 * translation. This means that, in the example above, a MAC learned by the
3682 * learn action in table 0 would be found in table 1 (if the packet being
3683 * processed had the same source and destination MAC address).
3685 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3686 * modify a flow) by a "learn" action are visible only for later flow
3687 * translations, not for later lookups within the same flow translation. In
3688 * the MAC learning example, a MAC learned by the learn action in table 0 would
3689 * not be found in table 1 if the flow translation would resubmit to table 1
3690 * after the processing of the learn action, meaning that if this MAC had not
3691 * been learned before then the packet would be flooded. */
3692 struct nx_action_learn {
3693 ovs_be16 type; /* OFPAT_VENDOR. */
3694 ovs_be16 len; /* At least 24. */
3695 ovs_be32 vendor; /* NX_VENDOR_ID. */
3696 ovs_be16 subtype; /* NXAST_LEARN. */
3697 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3698 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3699 ovs_be16 priority; /* Priority level of flow entry. */
3700 ovs_be64 cookie; /* Cookie for new flow. */
3701 ovs_be16 flags; /* NX_LEARN_F_*. */
3702 uint8_t table_id; /* Table to insert flow entry. */
3703 uint8_t pad; /* Must be zero. */
3704 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3705 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3706 /* Followed by a sequence of flow_mod_spec elements, as described above,
3707 * until the end of the action is reached. */
3709 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3712 get_be16(const void **pp)
3714 const ovs_be16 *p = *pp;
3715 ovs_be16 value = *p;
3721 get_be32(const void **pp)
3723 const ovs_be32 *p = *pp;
3724 ovs_be32 value = get_unaligned_be32(p);
3730 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3732 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3733 sf->ofs = ntohs(get_be16(p));
3734 sf->n_bits = n_bits;
3738 learn_min_len(uint16_t header)
3740 int n_bits = header & NX_LEARN_N_BITS_MASK;
3741 int src_type = header & NX_LEARN_SRC_MASK;
3742 int dst_type = header & NX_LEARN_DST_MASK;
3743 unsigned int min_len;
3746 if (src_type == NX_LEARN_SRC_FIELD) {
3747 min_len += sizeof(ovs_be32); /* src_field */
3748 min_len += sizeof(ovs_be16); /* src_ofs */
3750 min_len += DIV_ROUND_UP(n_bits, 16);
3752 if (dst_type == NX_LEARN_DST_MATCH ||
3753 dst_type == NX_LEARN_DST_LOAD) {
3754 min_len += sizeof(ovs_be32); /* dst_field */
3755 min_len += sizeof(ovs_be16); /* dst_ofs */
3760 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3761 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3763 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3764 struct ofpbuf *ofpacts)
3766 struct ofpact_learn *learn;
3767 const void *p, *end;
3770 return OFPERR_OFPBAC_BAD_ARGUMENT;
3773 learn = ofpact_put_LEARN(ofpacts);
3775 learn->idle_timeout = ntohs(nal->idle_timeout);
3776 learn->hard_timeout = ntohs(nal->hard_timeout);
3777 learn->priority = ntohs(nal->priority);
3778 learn->cookie = nal->cookie;
3779 learn->table_id = nal->table_id;
3780 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3781 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3783 learn->flags = ntohs(nal->flags);
3784 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3785 NX_LEARN_F_DELETE_LEARNED)) {
3786 return OFPERR_OFPBAC_BAD_ARGUMENT;
3789 if (learn->table_id == 0xff) {
3790 return OFPERR_OFPBAC_BAD_ARGUMENT;
3793 end = (char *) nal + ntohs(nal->len);
3794 for (p = nal + 1; p != end; ) {
3795 struct ofpact_learn_spec *spec;
3796 uint16_t header = ntohs(get_be16(&p));
3802 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3803 learn = ofpacts->header;
3806 spec->src_type = header & NX_LEARN_SRC_MASK;
3807 spec->dst_type = header & NX_LEARN_DST_MASK;
3808 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3810 /* Check for valid src and dst type combination. */
3811 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3812 spec->dst_type == NX_LEARN_DST_LOAD ||
3813 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3814 spec->src_type == NX_LEARN_SRC_FIELD)) {
3817 return OFPERR_OFPBAC_BAD_ARGUMENT;
3820 /* Check that the arguments don't overrun the end of the action. */
3821 if ((char *) end - (char *) p < learn_min_len(header)) {
3822 return OFPERR_OFPBAC_BAD_LEN;
3825 /* Get the source. */
3826 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3827 get_subfield(spec->n_bits, &p, &spec->src);
3829 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3831 bitwise_copy(p, p_bytes, 0,
3832 &spec->src_imm, sizeof spec->src_imm, 0,
3834 p = (const uint8_t *) p + p_bytes;
3837 /* Get the destination. */
3838 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3839 spec->dst_type == NX_LEARN_DST_LOAD) {
3840 get_subfield(spec->n_bits, &p, &spec->dst);
3843 ofpact_update_len(ofpacts, &learn->ofpact);
3845 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3846 return OFPERR_OFPBAC_BAD_ARGUMENT;
3853 put_be16(struct ofpbuf *b, ovs_be16 x)
3855 ofpbuf_put(b, &x, sizeof x);
3859 put_be32(struct ofpbuf *b, ovs_be32 x)
3861 ofpbuf_put(b, &x, sizeof x);
3865 put_u16(struct ofpbuf *b, uint16_t x)
3867 put_be16(b, htons(x));
3871 put_u32(struct ofpbuf *b, uint32_t x)
3873 put_be32(b, htonl(x));
3877 encode_LEARN(const struct ofpact_learn *learn,
3878 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3880 const struct ofpact_learn_spec *spec;
3881 struct nx_action_learn *nal;
3884 start_ofs = out->size;
3885 nal = put_NXAST_LEARN(out);
3886 nal->idle_timeout = htons(learn->idle_timeout);
3887 nal->hard_timeout = htons(learn->hard_timeout);
3888 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
3889 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
3890 nal->priority = htons(learn->priority);
3891 nal->cookie = learn->cookie;
3892 nal->flags = htons(learn->flags);
3893 nal->table_id = learn->table_id;
3895 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
3896 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
3898 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3899 put_u32(out, mf_nxm_header(spec->src.field->id));
3900 put_u16(out, spec->src.ofs);
3902 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3903 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
3904 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
3905 bits, n_dst_bytes, 0,
3909 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3910 spec->dst_type == NX_LEARN_DST_LOAD) {
3911 put_u32(out, mf_nxm_header(spec->dst.field->id));
3912 put_u16(out, spec->dst.ofs);
3916 pad_ofpat(out, start_ofs);
3919 static char * OVS_WARN_UNUSED_RESULT
3920 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
3921 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3923 return learn_parse(arg, ofpacts);
3927 format_LEARN(const struct ofpact_learn *a, struct ds *s)
3932 /* Action structure for NXAST_CONJUNCTION. */
3933 struct nx_action_conjunction {
3934 ovs_be16 type; /* OFPAT_VENDOR. */
3935 ovs_be16 len; /* At least 16. */
3936 ovs_be32 vendor; /* NX_VENDOR_ID. */
3937 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
3942 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
3945 add_conjunction(struct ofpbuf *out,
3946 uint32_t id, uint8_t clause, uint8_t n_clauses)
3948 struct ofpact_conjunction *oc;
3950 oc = ofpact_put_CONJUNCTION(out);
3952 oc->clause = clause;
3953 oc->n_clauses = n_clauses;
3957 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
3960 if (nac->n_clauses < 2 || nac->n_clauses > 64
3961 || nac->clause >= nac->n_clauses) {
3962 return OFPERR_NXBAC_BAD_CONJUNCTION;
3964 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
3970 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
3971 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3973 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
3974 nac->clause = oc->clause;
3975 nac->n_clauses = oc->n_clauses;
3976 nac->id = htonl(oc->id);
3980 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
3982 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
3983 oc->id, oc->clause + 1, oc->n_clauses);
3986 static char * OVS_WARN_UNUSED_RESULT
3987 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
3988 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3995 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
3996 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
3997 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4000 if (n_clauses < 2) {
4001 return xstrdup("conjunction must have at least 2 clauses");
4002 } else if (n_clauses > 64) {
4003 return xstrdup("conjunction must have at most 64 clauses");
4004 } else if (clause < 1) {
4005 return xstrdup("clause index must be positive");
4006 } else if (clause > n_clauses) {
4007 return xstrdup("clause index must be less than or equal to "
4008 "number of clauses");
4011 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4015 /* Action structure for NXAST_MULTIPATH.
4017 * This action performs the following steps in sequence:
4019 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4020 * Refer to the definition of "enum nx_mp_fields" for details.
4022 * The 'basis' value is used as a universal hash parameter, that is,
4023 * different values of 'basis' yield different hash functions. The
4024 * particular universal hash function used is implementation-defined.
4026 * The hashed fields' values are drawn from the current state of the
4027 * flow, including all modifications that have been made by actions up to
4030 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4031 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4034 * The output of the algorithm is 'link', an unsigned integer less than
4035 * or equal to 'max_link'.
4037 * Some algorithms use 'arg' as an additional argument.
4039 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4040 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4043 * The switch will reject actions that have an unknown 'fields', or an unknown
4044 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4045 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4046 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4048 struct nx_action_multipath {
4049 ovs_be16 type; /* OFPAT_VENDOR. */
4050 ovs_be16 len; /* Length is 32. */
4051 ovs_be32 vendor; /* NX_VENDOR_ID. */
4052 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4054 /* What fields to hash and how. */
4055 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4056 ovs_be16 basis; /* Universal hash parameter. */
4059 /* Multipath link choice algorithm to apply to hash value. */
4060 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4061 ovs_be16 max_link; /* Number of output links, minus 1. */
4062 ovs_be32 arg; /* Algorithm-specific argument. */
4065 /* Where to store the result. */
4066 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4067 ovs_be32 dst; /* Destination. */
4069 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4072 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4075 uint32_t n_links = ntohs(nam->max_link) + 1;
4076 size_t min_n_bits = log_2_ceil(n_links);
4077 struct ofpact_multipath *mp;
4079 mp = ofpact_put_MULTIPATH(out);
4080 mp->fields = ntohs(nam->fields);
4081 mp->basis = ntohs(nam->basis);
4082 mp->algorithm = ntohs(nam->algorithm);
4083 mp->max_link = ntohs(nam->max_link);
4084 mp->arg = ntohl(nam->arg);
4085 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4086 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4087 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4089 if (!flow_hash_fields_valid(mp->fields)) {
4090 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4091 return OFPERR_OFPBAC_BAD_ARGUMENT;
4092 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4093 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4094 && mp->algorithm != NX_MP_ALG_HRW
4095 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4096 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4097 return OFPERR_OFPBAC_BAD_ARGUMENT;
4098 } else if (mp->dst.n_bits < min_n_bits) {
4099 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4100 "%"PRIu32" links", min_n_bits, n_links);
4101 return OFPERR_OFPBAC_BAD_ARGUMENT;
4104 return multipath_check(mp, NULL);
4108 encode_MULTIPATH(const struct ofpact_multipath *mp,
4109 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4111 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4113 nam->fields = htons(mp->fields);
4114 nam->basis = htons(mp->basis);
4115 nam->algorithm = htons(mp->algorithm);
4116 nam->max_link = htons(mp->max_link);
4117 nam->arg = htonl(mp->arg);
4118 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4119 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4122 static char * OVS_WARN_UNUSED_RESULT
4123 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4124 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4126 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4130 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4132 multipath_format(a, s);
4135 /* Action structure for NXAST_NOTE.
4137 * This action has no effect. It is variable length. The switch does not
4138 * attempt to interpret the user-defined 'note' data in any way. A controller
4139 * can use this action to attach arbitrary metadata to a flow.
4141 * This action might go away in the future.
4143 struct nx_action_note {
4144 ovs_be16 type; /* OFPAT_VENDOR. */
4145 ovs_be16 len; /* A multiple of 8, but at least 16. */
4146 ovs_be32 vendor; /* NX_VENDOR_ID. */
4147 ovs_be16 subtype; /* NXAST_NOTE. */
4148 uint8_t note[6]; /* Start of user-defined data. */
4149 /* Possibly followed by additional user-defined data. */
4151 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4154 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan, struct ofpbuf *out)
4156 struct ofpact_note *note;
4157 unsigned int length;
4159 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4160 note = ofpact_put(out, OFPACT_NOTE,
4161 offsetof(struct ofpact_note, data) + length);
4162 note->length = length;
4163 memcpy(note->data, nan->note, length);
4169 encode_NOTE(const struct ofpact_note *note,
4170 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4172 size_t start_ofs = out->size;
4173 struct nx_action_note *nan;
4174 unsigned int remainder;
4177 put_NXAST_NOTE(out);
4178 out->size = out->size - sizeof nan->note;
4180 ofpbuf_put(out, note->data, note->length);
4182 len = out->size - start_ofs;
4183 remainder = len % OFP_ACTION_ALIGN;
4185 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4187 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4188 nan->len = htons(out->size - start_ofs);
4191 static char * OVS_WARN_UNUSED_RESULT
4192 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4193 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4195 struct ofpact_note *note;
4197 note = ofpact_put_NOTE(ofpacts);
4198 while (*arg != '\0') {
4209 byte = hexits_value(arg, 2, &ok);
4211 return xstrdup("bad hex digit in `note' argument");
4213 ofpbuf_put(ofpacts, &byte, 1);
4215 note = ofpacts->header;
4220 ofpact_update_len(ofpacts, ¬e->ofpact);
4225 format_NOTE(const struct ofpact_note *a, struct ds *s)
4229 ds_put_cstr(s, "note:");
4230 for (i = 0; i < a->length; i++) {
4232 ds_put_char(s, '.');
4234 ds_put_format(s, "%02"PRIx8, a->data[i]);
4241 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4243 ofpact_put_EXIT(out);
4248 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4249 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4251 put_NXAST_EXIT(out);
4254 static char * OVS_WARN_UNUSED_RESULT
4255 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4256 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4258 ofpact_put_EXIT(ofpacts);
4263 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4265 ds_put_cstr(s, "exit");
4268 /* Unroll xlate action. */
4271 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4272 enum ofp_version ofp_version OVS_UNUSED,
4273 struct ofpbuf *out OVS_UNUSED)
4278 static char * OVS_WARN_UNUSED_RESULT
4279 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4280 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4287 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4290 ds_put_cstr(s, "unroll_xlate");
4293 /* Action structure for NXAST_SAMPLE.
4295 * Samples matching packets with the given probability and sends them
4296 * each to the set of collectors identified with the given ID. The
4297 * probability is expressed as a number of packets to be sampled out
4298 * of USHRT_MAX packets, and must be >0.
4300 * When sending packet samples to IPFIX collectors, the IPFIX flow
4301 * record sent for each sampled packet is associated with the given
4302 * observation domain ID and observation point ID. Each IPFIX flow
4303 * record contain the sampled packet's headers when executing this
4304 * rule. If a sampled packet's headers are modified by previous
4305 * actions in the flow, those modified headers are sent. */
4306 struct nx_action_sample {
4307 ovs_be16 type; /* OFPAT_VENDOR. */
4308 ovs_be16 len; /* Length is 24. */
4309 ovs_be32 vendor; /* NX_VENDOR_ID. */
4310 ovs_be16 subtype; /* NXAST_SAMPLE. */
4311 ovs_be16 probability; /* Fraction of packets to sample. */
4312 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4313 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4314 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4316 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4319 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas, struct ofpbuf *out)
4321 struct ofpact_sample *sample;
4323 sample = ofpact_put_SAMPLE(out);
4324 sample->probability = ntohs(nas->probability);
4325 sample->collector_set_id = ntohl(nas->collector_set_id);
4326 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4327 sample->obs_point_id = ntohl(nas->obs_point_id);
4329 if (sample->probability == 0) {
4330 return OFPERR_OFPBAC_BAD_ARGUMENT;
4337 encode_SAMPLE(const struct ofpact_sample *sample,
4338 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4340 struct nx_action_sample *nas;
4342 nas = put_NXAST_SAMPLE(out);
4343 nas->probability = htons(sample->probability);
4344 nas->collector_set_id = htonl(sample->collector_set_id);
4345 nas->obs_domain_id = htonl(sample->obs_domain_id);
4346 nas->obs_point_id = htonl(sample->obs_point_id);
4349 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4350 * action to 'ofpacts'.
4352 * Returns NULL if successful, otherwise a malloc()'d string describing the
4353 * error. The caller is responsible for freeing the returned string. */
4354 static char * OVS_WARN_UNUSED_RESULT
4355 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4356 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4358 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4361 while (ofputil_parse_key_value(&arg, &key, &value)) {
4364 if (!strcmp(key, "probability")) {
4365 error = str_to_u16(value, "probability", &os->probability);
4366 if (!error && os->probability == 0) {
4367 error = xasprintf("invalid probability value \"%s\"", value);
4369 } else if (!strcmp(key, "collector_set_id")) {
4370 error = str_to_u32(value, &os->collector_set_id);
4371 } else if (!strcmp(key, "obs_domain_id")) {
4372 error = str_to_u32(value, &os->obs_domain_id);
4373 } else if (!strcmp(key, "obs_point_id")) {
4374 error = str_to_u32(value, &os->obs_point_id);
4376 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4383 if (os->probability == 0) {
4384 return xstrdup("non-zero \"probability\" must be specified on sample");
4390 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4392 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4393 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4394 a->probability, a->collector_set_id,
4395 a->obs_domain_id, a->obs_point_id);
4398 /* debug_recirc instruction. */
4400 static bool enable_debug;
4403 ofpact_dummy_enable(void)
4405 enable_debug = true;
4409 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4411 if (!enable_debug) {
4412 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4415 ofpact_put_DEBUG_RECIRC(out);
4420 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4421 enum ofp_version ofp_version OVS_UNUSED,
4424 put_NXAST_DEBUG_RECIRC(out);
4427 static char * OVS_WARN_UNUSED_RESULT
4428 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4429 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4431 ofpact_put_DEBUG_RECIRC(ofpacts);
4436 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4438 ds_put_cstr(s, "debug_recirc");
4441 /* Meter instruction. */
4444 encode_METER(const struct ofpact_meter *meter,
4445 enum ofp_version ofp_version, struct ofpbuf *out)
4447 if (ofp_version >= OFP13_VERSION) {
4448 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4452 static char * OVS_WARN_UNUSED_RESULT
4453 parse_METER(char *arg, struct ofpbuf *ofpacts,
4454 enum ofputil_protocol *usable_protocols)
4456 *usable_protocols &= OFPUTIL_P_OF13_UP;
4457 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
4461 format_METER(const struct ofpact_meter *a, struct ds *s)
4463 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
4466 /* Clear-Actions instruction. */
4469 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
4470 enum ofp_version ofp_version OVS_UNUSED,
4471 struct ofpbuf *out OVS_UNUSED)
4473 if (ofp_version > OFP10_VERSION) {
4474 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
4478 static char * OVS_WARN_UNUSED_RESULT
4479 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4480 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4482 ofpact_put_CLEAR_ACTIONS(ofpacts);
4487 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4489 ds_put_cstr(s, "clear_actions");
4492 /* Write-Actions instruction. */
4495 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
4496 enum ofp_version ofp_version, struct ofpbuf *out)
4498 if (ofp_version > OFP10_VERSION) {
4499 const size_t ofs = out->size;
4501 instruction_put_OFPIT11_WRITE_ACTIONS(out);
4502 ofpacts_put_openflow_actions(actions->actions,
4503 ofpact_nest_get_action_len(actions),
4505 ofpacts_update_instruction_actions(out, ofs);
4509 static char * OVS_WARN_UNUSED_RESULT
4510 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
4511 enum ofputil_protocol *usable_protocols)
4513 struct ofpact_nest *on;
4517 /* Pull off existing actions or instructions. */
4518 ofpact_pad(ofpacts);
4519 ofs = ofpacts->size;
4520 ofpbuf_pull(ofpacts, ofs);
4522 /* Add a Write-Actions instruction and then pull it off. */
4523 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
4524 ofpbuf_pull(ofpacts, sizeof *on);
4526 /* Parse nested actions.
4528 * We pulled off "write-actions" and the previous actions because the
4529 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4530 * that it doesn't actually include the nested actions. That means that
4531 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4532 * follows a Write-Actions, which is an invalid order. */
4533 error = ofpacts_parse(arg, ofpacts, usable_protocols, false);
4535 /* Put the Write-Actions back on and update its length. */
4536 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
4537 on->ofpact.len = ofpacts->size;
4539 /* Put any previous actions or instructions back on. */
4540 ofpbuf_push_uninit(ofpacts, ofs);
4546 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
4548 ds_put_cstr(s, "write_actions(");
4549 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
4550 ds_put_char(s, ')');
4553 /* Action structure for NXAST_WRITE_METADATA.
4555 * Modifies the 'mask' bits of the metadata value. */
4556 struct nx_action_write_metadata {
4557 ovs_be16 type; /* OFPAT_VENDOR. */
4558 ovs_be16 len; /* Length is 32. */
4559 ovs_be32 vendor; /* NX_VENDOR_ID. */
4560 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
4561 uint8_t zeros[6]; /* Must be zero. */
4562 ovs_be64 metadata; /* Metadata register. */
4563 ovs_be64 mask; /* Metadata mask. */
4565 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
4568 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
4571 struct ofpact_metadata *om;
4573 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
4574 return OFPERR_NXBRC_MUST_BE_ZERO;
4577 om = ofpact_put_WRITE_METADATA(out);
4578 om->metadata = nawm->metadata;
4579 om->mask = nawm->mask;
4585 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
4586 enum ofp_version ofp_version, struct ofpbuf *out)
4588 if (ofp_version == OFP10_VERSION) {
4589 struct nx_action_write_metadata *nawm;
4591 nawm = put_NXAST_WRITE_METADATA(out);
4592 nawm->metadata = metadata->metadata;
4593 nawm->mask = metadata->mask;
4595 struct ofp11_instruction_write_metadata *oiwm;
4597 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
4598 oiwm->metadata = metadata->metadata;
4599 oiwm->metadata_mask = metadata->mask;
4603 static char * OVS_WARN_UNUSED_RESULT
4604 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
4605 enum ofputil_protocol *usable_protocols)
4607 struct ofpact_metadata *om;
4608 char *mask = strchr(arg, '/');
4610 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
4612 om = ofpact_put_WRITE_METADATA(ofpacts);
4617 error = str_to_be64(mask + 1, &om->mask);
4622 om->mask = OVS_BE64_MAX;
4625 return str_to_be64(arg, &om->metadata);
4629 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
4631 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
4632 if (a->mask != OVS_BE64_MAX) {
4633 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
4637 /* Goto-Table instruction. */
4640 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
4641 enum ofp_version ofp_version, struct ofpbuf *out)
4643 if (ofp_version == OFP10_VERSION) {
4644 struct nx_action_resubmit *nar;
4646 nar = put_NXAST_RESUBMIT_TABLE(out);
4647 nar->table = goto_table->table_id;
4648 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
4650 struct ofp11_instruction_goto_table *oigt;
4652 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
4653 oigt->table_id = goto_table->table_id;
4654 memset(oigt->pad, 0, sizeof oigt->pad);
4658 static char * OVS_WARN_UNUSED_RESULT
4659 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
4660 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4662 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
4663 char *table_s = strsep(&arg, ",");
4664 if (!table_s || !table_s[0]) {
4665 return xstrdup("instruction goto-table needs table id");
4667 return str_to_u8(table_s, "table", &ogt->table_id);
4671 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
4673 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
4677 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
4678 const struct ofp_action_header *bad_action, enum ofperr error)
4680 if (!VLOG_DROP_WARN(&rl)) {
4684 ds_put_hex_dump(&s, actions, actions_len, 0, false);
4685 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
4686 (char *)bad_action - (char *)actions,
4687 ofperr_get_name(error), ds_cstr(&s));
4693 ofpacts_decode(const void *actions, size_t actions_len,
4694 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
4696 struct ofpbuf openflow;
4698 ofpbuf_use_const(&openflow, actions, actions_len);
4699 while (openflow.size) {
4700 const struct ofp_action_header *action = openflow.data;
4701 enum ofp_raw_action_type raw;
4705 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
4707 error = ofpact_decode(action, raw, arg, ofpacts);
4711 log_bad_action(actions, actions_len, action, error);
4716 ofpact_pad(ofpacts);
4721 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
4722 unsigned int actions_len,
4723 enum ofp_version version,
4724 uint32_t allowed_ovsinsts,
4725 struct ofpbuf *ofpacts)
4727 const struct ofp_action_header *actions;
4730 ofpbuf_clear(ofpacts);
4732 if (actions_len % OFP_ACTION_ALIGN != 0) {
4733 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
4734 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
4735 return OFPERR_OFPBRC_BAD_LEN;
4738 actions = ofpbuf_try_pull(openflow, actions_len);
4739 if (actions == NULL) {
4740 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
4741 "remaining message length (%"PRIu32")",
4742 actions_len, openflow->size);
4743 return OFPERR_OFPBRC_BAD_LEN;
4746 error = ofpacts_decode(actions, actions_len, version, ofpacts);
4748 ofpbuf_clear(ofpacts);
4752 error = ofpacts_verify(ofpacts->data, ofpacts->size,
4755 ofpbuf_clear(ofpacts);
4760 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
4761 * front of 'openflow' into ofpacts. On success, replaces any existing content
4762 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
4763 * Returns 0 if successful, otherwise an OpenFlow error.
4765 * Actions are processed according to their OpenFlow version which
4766 * is provided in the 'version' parameter.
4768 * In most places in OpenFlow, actions appear encapsulated in instructions, so
4769 * you should call ofpacts_pull_openflow_instructions() instead of this
4772 * The parsed actions are valid generically, but they may not be valid in a
4773 * specific context. For example, port numbers up to OFPP_MAX are valid
4774 * generically, but specific datapaths may only support port numbers in a
4775 * smaller range. Use ofpacts_check() to additional check whether actions are
4776 * valid in a specific context. */
4778 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
4779 unsigned int actions_len,
4780 enum ofp_version version,
4781 struct ofpbuf *ofpacts)
4783 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
4784 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4788 /* OpenFlow 1.1 actions. */
4791 /* True if an action sets the value of a field
4792 * in a way that is compatibile with the action set.
4793 * The field can be set via either a set or a move action.
4794 * False otherwise. */
4796 ofpact_is_set_or_move_action(const struct ofpact *a)
4799 case OFPACT_SET_FIELD:
4800 case OFPACT_REG_MOVE:
4801 case OFPACT_SET_ETH_DST:
4802 case OFPACT_SET_ETH_SRC:
4803 case OFPACT_SET_IP_DSCP:
4804 case OFPACT_SET_IP_ECN:
4805 case OFPACT_SET_IP_TTL:
4806 case OFPACT_SET_IPV4_DST:
4807 case OFPACT_SET_IPV4_SRC:
4808 case OFPACT_SET_L4_DST_PORT:
4809 case OFPACT_SET_L4_SRC_PORT:
4810 case OFPACT_SET_MPLS_LABEL:
4811 case OFPACT_SET_MPLS_TC:
4812 case OFPACT_SET_MPLS_TTL:
4813 case OFPACT_SET_QUEUE:
4814 case OFPACT_SET_TUNNEL:
4815 case OFPACT_SET_VLAN_PCP:
4816 case OFPACT_SET_VLAN_VID:
4819 case OFPACT_CLEAR_ACTIONS:
4820 case OFPACT_CONTROLLER:
4821 case OFPACT_DEC_MPLS_TTL:
4822 case OFPACT_DEC_TTL:
4823 case OFPACT_ENQUEUE:
4825 case OFPACT_UNROLL_XLATE:
4826 case OFPACT_FIN_TIMEOUT:
4827 case OFPACT_GOTO_TABLE:
4830 case OFPACT_CONJUNCTION:
4832 case OFPACT_MULTIPATH:
4835 case OFPACT_OUTPUT_REG:
4836 case OFPACT_POP_MPLS:
4837 case OFPACT_POP_QUEUE:
4838 case OFPACT_PUSH_MPLS:
4839 case OFPACT_PUSH_VLAN:
4840 case OFPACT_RESUBMIT:
4842 case OFPACT_STACK_POP:
4843 case OFPACT_STACK_PUSH:
4844 case OFPACT_STRIP_VLAN:
4845 case OFPACT_WRITE_ACTIONS:
4846 case OFPACT_WRITE_METADATA:
4847 case OFPACT_DEBUG_RECIRC:
4854 /* True if an action is allowed in the action set.
4855 * False otherwise. */
4857 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
4860 case OFPACT_DEC_MPLS_TTL:
4861 case OFPACT_DEC_TTL:
4864 case OFPACT_POP_MPLS:
4865 case OFPACT_PUSH_MPLS:
4866 case OFPACT_PUSH_VLAN:
4867 case OFPACT_REG_MOVE:
4868 case OFPACT_SET_FIELD:
4869 case OFPACT_SET_ETH_DST:
4870 case OFPACT_SET_ETH_SRC:
4871 case OFPACT_SET_IP_DSCP:
4872 case OFPACT_SET_IP_ECN:
4873 case OFPACT_SET_IP_TTL:
4874 case OFPACT_SET_IPV4_DST:
4875 case OFPACT_SET_IPV4_SRC:
4876 case OFPACT_SET_L4_DST_PORT:
4877 case OFPACT_SET_L4_SRC_PORT:
4878 case OFPACT_SET_MPLS_LABEL:
4879 case OFPACT_SET_MPLS_TC:
4880 case OFPACT_SET_MPLS_TTL:
4881 case OFPACT_SET_QUEUE:
4882 case OFPACT_SET_TUNNEL:
4883 case OFPACT_SET_VLAN_PCP:
4884 case OFPACT_SET_VLAN_VID:
4885 case OFPACT_STRIP_VLAN:
4888 /* In general these actions are excluded because they are not part of
4889 * the OpenFlow specification nor map to actions that are defined in
4890 * the specification. Thus the order in which they should be applied
4891 * in the action set is undefined. */
4893 case OFPACT_CONTROLLER:
4894 case OFPACT_ENQUEUE:
4896 case OFPACT_UNROLL_XLATE:
4897 case OFPACT_FIN_TIMEOUT:
4899 case OFPACT_CONJUNCTION:
4900 case OFPACT_MULTIPATH:
4902 case OFPACT_OUTPUT_REG:
4903 case OFPACT_POP_QUEUE:
4904 case OFPACT_RESUBMIT:
4906 case OFPACT_STACK_POP:
4907 case OFPACT_STACK_PUSH:
4908 case OFPACT_DEBUG_RECIRC:
4910 /* The action set may only include actions and thus
4911 * may not include any instructions */
4912 case OFPACT_CLEAR_ACTIONS:
4913 case OFPACT_GOTO_TABLE:
4915 case OFPACT_WRITE_ACTIONS:
4916 case OFPACT_WRITE_METADATA:
4923 /* Append ofpact 'a' onto the tail of 'out' */
4925 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
4927 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
4930 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
4932 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
4933 enum ofpact_type filter)
4935 const struct ofpact *target;
4936 const struct ofpact *a;
4939 OFPACT_FOR_EACH (a, in->data, in->size) {
4940 if (a->type == filter) {
4945 ofpact_copy(out, target);
4947 return target != NULL;
4950 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
4951 * The order of appended ofpacts is preserved between 'in' and 'out' */
4953 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
4954 bool (*filter)(const struct ofpact *))
4956 const struct ofpact *a;
4958 OFPACT_FOR_EACH (a, in->data, in->size) {
4960 ofpact_copy(out, a);
4965 /* Reads 'action_set', which contains ofpacts accumulated by
4966 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
4967 * executed directly into 'action_list'. (These names correspond to the
4968 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
4970 * In general this involves appending the last instance of each action that is
4971 * admissible in the action set in the order described in the OpenFlow
4975 * + output action is only appended if no group action was present in 'in'.
4976 * + As a simplification all set actions are copied in the order the are
4977 * provided in 'in' as many set actions applied to a field has the same
4978 * affect as only applying the last action that sets a field and
4979 * duplicates are removed by do_xlate_actions().
4980 * This has an unwanted side-effect of compsoting multiple
4981 * LOAD_REG actions that touch different regions of the same field. */
4983 ofpacts_execute_action_set(struct ofpbuf *action_list,
4984 const struct ofpbuf *action_set)
4986 /* The OpenFlow spec "Action Set" section specifies this order. */
4987 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
4988 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
4989 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
4990 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
4991 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
4992 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
4993 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
4994 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
4996 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
4997 * we should execute only OFPACT_GROUP.
4999 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5000 * all the actions because there's no point in modifying a packet that will
5001 * not be sent anywhere. */
5002 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5003 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5004 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5005 ofpbuf_clear(action_list);
5011 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5012 size_t n_in, enum ofp_version version,
5017 size_t start = out->size;
5019 error = ofpacts_decode(in, n_in, version, out);
5025 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5026 if (!ofpact_is_allowed_in_actions_set(a)) {
5027 VLOG_WARN_RL(&rl, "disallowed action in action set");
5028 return OFPERR_OFPBAC_BAD_TYPE;
5035 /* OpenFlow 1.1 instructions. */
5037 struct instruction_type_info {
5038 enum ovs_instruction_type type;
5042 static const struct instruction_type_info inst_info[] = {
5043 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5049 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5051 return inst_info[type].name;
5055 ovs_instruction_type_from_name(const char *name)
5057 const struct instruction_type_info *p;
5058 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5059 if (!strcasecmp(name, p->name)) {
5066 enum ovs_instruction_type
5067 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5071 return OVSINST_OFPIT13_METER;
5072 case OFPACT_CLEAR_ACTIONS:
5073 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5074 case OFPACT_WRITE_ACTIONS:
5075 return OVSINST_OFPIT11_WRITE_ACTIONS;
5076 case OFPACT_WRITE_METADATA:
5077 return OVSINST_OFPIT11_WRITE_METADATA;
5078 case OFPACT_GOTO_TABLE:
5079 return OVSINST_OFPIT11_GOTO_TABLE;
5082 case OFPACT_CONTROLLER:
5083 case OFPACT_ENQUEUE:
5084 case OFPACT_OUTPUT_REG:
5086 case OFPACT_SET_VLAN_VID:
5087 case OFPACT_SET_VLAN_PCP:
5088 case OFPACT_STRIP_VLAN:
5089 case OFPACT_PUSH_VLAN:
5090 case OFPACT_SET_ETH_SRC:
5091 case OFPACT_SET_ETH_DST:
5092 case OFPACT_SET_IPV4_SRC:
5093 case OFPACT_SET_IPV4_DST:
5094 case OFPACT_SET_IP_DSCP:
5095 case OFPACT_SET_IP_ECN:
5096 case OFPACT_SET_IP_TTL:
5097 case OFPACT_SET_L4_SRC_PORT:
5098 case OFPACT_SET_L4_DST_PORT:
5099 case OFPACT_REG_MOVE:
5100 case OFPACT_SET_FIELD:
5101 case OFPACT_STACK_PUSH:
5102 case OFPACT_STACK_POP:
5103 case OFPACT_DEC_TTL:
5104 case OFPACT_SET_MPLS_LABEL:
5105 case OFPACT_SET_MPLS_TC:
5106 case OFPACT_SET_MPLS_TTL:
5107 case OFPACT_DEC_MPLS_TTL:
5108 case OFPACT_PUSH_MPLS:
5109 case OFPACT_POP_MPLS:
5110 case OFPACT_SET_TUNNEL:
5111 case OFPACT_SET_QUEUE:
5112 case OFPACT_POP_QUEUE:
5113 case OFPACT_FIN_TIMEOUT:
5114 case OFPACT_RESUBMIT:
5116 case OFPACT_CONJUNCTION:
5117 case OFPACT_MULTIPATH:
5120 case OFPACT_UNROLL_XLATE:
5122 case OFPACT_DEBUG_RECIRC:
5124 return OVSINST_OFPIT11_APPLY_ACTIONS;
5129 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
5130 const uint16_t inst_type)
5132 switch (inst_type) {
5134 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5136 *instruction_type = OVSINST_##ENUM; \
5142 return OFPERR_OFPBIC_UNKNOWN_INST;
5146 /* Two-way translation between OVS's internal "OVSINST_*" representation of
5147 * instructions and the "OFPIT_*" representation used in OpenFlow. */
5148 struct ovsinst_map {
5149 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
5150 int ofpit; /* OFPIT_* number from OpenFlow spec. */
5153 static const struct ovsinst_map *
5154 get_ovsinst_map(enum ofp_version version)
5156 /* OpenFlow 1.1 and 1.2 instructions. */
5157 static const struct ovsinst_map of11[] = {
5158 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5159 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5160 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5161 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5162 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5166 /* OpenFlow 1.3+ instructions. */
5167 static const struct ovsinst_map of13[] = {
5168 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5169 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5170 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5171 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5172 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5173 { OVSINST_OFPIT13_METER, 6 },
5177 return version < OFP13_VERSION ? of11 : of13;
5180 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
5181 * values, into a bitmap of instructions suitable for OpenFlow 'version'
5182 * (OFP11_VERSION or later), and returns the result. */
5184 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
5186 uint32_t ofpit_bitmap = 0;
5187 const struct ovsinst_map *x;
5189 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5190 if (ovsinst_bitmap & (1u << x->ovsinst)) {
5191 ofpit_bitmap |= 1u << x->ofpit;
5194 return htonl(ofpit_bitmap);
5197 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5198 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5199 * correspond to OVSINST_* values, and returns the result. */
5201 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
5203 uint32_t ovsinst_bitmap = 0;
5204 const struct ovsinst_map *x;
5206 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5207 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
5208 ovsinst_bitmap |= 1u << x->ovsinst;
5211 return ovsinst_bitmap;
5214 static inline struct ofp11_instruction *
5215 instruction_next(const struct ofp11_instruction *inst)
5217 return ((struct ofp11_instruction *) (void *)
5218 ((uint8_t *) inst + ntohs(inst->len)));
5222 instruction_is_valid(const struct ofp11_instruction *inst,
5223 size_t n_instructions)
5225 uint16_t len = ntohs(inst->len);
5226 return (!(len % OFP11_INSTRUCTION_ALIGN)
5227 && len >= sizeof *inst
5228 && len / sizeof *inst <= n_instructions);
5231 /* This macro is careful to check for instructions with bad lengths. */
5232 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
5233 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
5234 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
5235 ((LEFT) -= (ntohs((ITER)->len) \
5236 / sizeof(struct ofp11_instruction)), \
5237 (ITER) = instruction_next(ITER)))
5240 decode_openflow11_instruction(const struct ofp11_instruction *inst,
5241 enum ovs_instruction_type *type)
5243 uint16_t len = ntohs(inst->len);
5245 switch (inst->type) {
5246 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5247 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5249 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5250 case CONSTANT_HTONS(ENUM): \
5252 ? len >= sizeof(struct STRUCT) \
5253 : len == sizeof(struct STRUCT)) { \
5254 *type = OVSINST_##ENUM; \
5257 return OFPERR_OFPBIC_BAD_LEN; \
5263 return OFPERR_OFPBIC_UNKNOWN_INST;
5268 decode_openflow11_instructions(const struct ofp11_instruction insts[],
5270 const struct ofp11_instruction *out[])
5272 const struct ofp11_instruction *inst;
5275 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5276 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5277 enum ovs_instruction_type type;
5280 error = decode_openflow11_instruction(inst, &type);
5286 return OFPERR_OFPBIC_DUP_INST;
5292 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5293 (n_insts - left) * sizeof *inst);
5294 return OFPERR_OFPBIC_BAD_LEN;
5300 get_actions_from_instruction(const struct ofp11_instruction *inst,
5301 const struct ofp_action_header **actions,
5302 size_t *actions_len)
5304 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5305 *actions_len = ntohs(inst->len) - sizeof *inst;
5309 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5310 unsigned int instructions_len,
5311 enum ofp_version version,
5312 struct ofpbuf *ofpacts)
5314 const struct ofp11_instruction *instructions;
5315 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5318 if (version == OFP10_VERSION) {
5319 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5321 (1u << N_OVS_INSTRUCTIONS) - 1,
5325 ofpbuf_clear(ofpacts);
5327 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5328 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5330 instructions_len, OFP11_INSTRUCTION_ALIGN);
5331 error = OFPERR_OFPBIC_BAD_LEN;
5335 instructions = ofpbuf_try_pull(openflow, instructions_len);
5336 if (instructions == NULL) {
5337 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5338 "remaining message length (%"PRIu32")",
5339 instructions_len, openflow->size);
5340 error = OFPERR_OFPBIC_BAD_LEN;
5344 error = decode_openflow11_instructions(
5345 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5351 if (insts[OVSINST_OFPIT13_METER]) {
5352 const struct ofp13_instruction_meter *oim;
5353 struct ofpact_meter *om;
5355 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5356 insts[OVSINST_OFPIT13_METER]);
5358 om = ofpact_put_METER(ofpacts);
5359 om->meter_id = ntohl(oim->meter_id);
5361 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5362 const struct ofp_action_header *actions;
5365 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5366 &actions, &actions_len);
5367 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5372 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5373 instruction_get_OFPIT11_CLEAR_ACTIONS(
5374 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5375 ofpact_put_CLEAR_ACTIONS(ofpacts);
5377 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5378 struct ofpact_nest *on;
5379 const struct ofp_action_header *actions;
5383 ofpact_pad(ofpacts);
5384 start = ofpacts->size;
5385 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5386 offsetof(struct ofpact_nest, actions));
5387 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5388 &actions, &actions_len);
5389 error = ofpacts_decode_for_action_set(actions, actions_len,
5394 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5395 on->ofpact.len = ofpacts->size - start;
5397 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5398 const struct ofp11_instruction_write_metadata *oiwm;
5399 struct ofpact_metadata *om;
5401 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5402 insts[OVSINST_OFPIT11_WRITE_METADATA]);
5404 om = ofpact_put_WRITE_METADATA(ofpacts);
5405 om->metadata = oiwm->metadata;
5406 om->mask = oiwm->metadata_mask;
5408 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5409 const struct ofp11_instruction_goto_table *oigt;
5410 struct ofpact_goto_table *ogt;
5412 oigt = instruction_get_OFPIT11_GOTO_TABLE(
5413 insts[OVSINST_OFPIT11_GOTO_TABLE]);
5414 ogt = ofpact_put_GOTO_TABLE(ofpacts);
5415 ogt->table_id = oigt->table_id;
5418 error = ofpacts_verify(ofpacts->data, ofpacts->size,
5419 (1u << N_OVS_INSTRUCTIONS) - 1);
5422 ofpbuf_clear(ofpacts);
5427 /* Update the length of the instruction that begins at offset 'ofs' within
5428 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5429 * If the instruction contains no nested actions, deletes it entirely. */
5431 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5433 struct ofp11_instruction_actions *oia;
5435 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5436 if (openflow->size > ofs + sizeof *oia) {
5437 oia->len = htons(openflow->size - ofs);
5439 openflow->size = ofs;
5443 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5444 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5445 * valid, otherwise an OpenFlow error code. */
5447 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5455 case OFPP_CONTROLLER:
5460 return OFPERR_OFPBAC_BAD_OUT_PORT;
5463 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
5466 return OFPERR_OFPBAC_BAD_OUT_PORT;
5470 /* Removes the protocols that require consistency between match and actions
5471 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5473 * (An example of an inconsistency between match and actions is a flow that
5474 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5477 inconsistent_match(enum ofputil_protocol *usable_protocols)
5479 *usable_protocols &= OFPUTIL_P_OF10_ANY;
5482 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5483 * caller must restore them.
5485 * Modifies some actions, filling in fields that could not be properly set
5486 * without context. */
5488 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
5489 struct flow *flow, ofp_port_t max_ports,
5490 uint8_t table_id, uint8_t n_tables)
5492 const struct ofpact_enqueue *enqueue;
5493 const struct mf_field *mf;
5497 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
5500 case OFPACT_CONTROLLER:
5503 case OFPACT_ENQUEUE:
5504 enqueue = ofpact_get_ENQUEUE(a);
5505 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
5506 && enqueue->port != OFPP_IN_PORT
5507 && enqueue->port != OFPP_LOCAL) {
5508 return OFPERR_OFPBAC_BAD_OUT_PORT;
5512 case OFPACT_OUTPUT_REG:
5513 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
5516 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
5518 case OFPACT_SET_VLAN_VID:
5519 /* Remember if we saw a vlan tag in the flow to aid translating to
5520 * OpenFlow 1.1+ if need be. */
5521 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
5522 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5523 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5524 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5525 inconsistent_match(usable_protocols);
5527 /* Temporary mark that we have a vlan tag. */
5528 flow->vlan_tci |= htons(VLAN_CFI);
5531 case OFPACT_SET_VLAN_PCP:
5532 /* Remember if we saw a vlan tag in the flow to aid translating to
5533 * OpenFlow 1.1+ if need be. */
5534 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
5535 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5536 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5537 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5538 inconsistent_match(usable_protocols);
5540 /* Temporary mark that we have a vlan tag. */
5541 flow->vlan_tci |= htons(VLAN_CFI);
5544 case OFPACT_STRIP_VLAN:
5545 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5546 inconsistent_match(usable_protocols);
5548 /* Temporary mark that we have no vlan tag. */
5549 flow->vlan_tci = htons(0);
5552 case OFPACT_PUSH_VLAN:
5553 if (flow->vlan_tci & htons(VLAN_CFI)) {
5554 /* Multiple VLAN headers not supported. */
5555 return OFPERR_OFPBAC_BAD_TAG;
5557 /* Temporary mark that we have a vlan tag. */
5558 flow->vlan_tci |= htons(VLAN_CFI);
5561 case OFPACT_SET_ETH_SRC:
5562 case OFPACT_SET_ETH_DST:
5565 case OFPACT_SET_IPV4_SRC:
5566 case OFPACT_SET_IPV4_DST:
5567 if (flow->dl_type != htons(ETH_TYPE_IP)) {
5568 inconsistent_match(usable_protocols);
5572 case OFPACT_SET_IP_DSCP:
5573 case OFPACT_SET_IP_ECN:
5574 case OFPACT_SET_IP_TTL:
5575 case OFPACT_DEC_TTL:
5576 if (!is_ip_any(flow)) {
5577 inconsistent_match(usable_protocols);
5581 case OFPACT_SET_L4_SRC_PORT:
5582 case OFPACT_SET_L4_DST_PORT:
5583 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
5584 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5585 && flow->nw_proto != IPPROTO_SCTP)) {
5586 inconsistent_match(usable_protocols);
5588 /* Note on which transport protocol the port numbers are set.
5589 * This allows this set action to be converted to an OF1.2 set field
5591 if (a->type == OFPACT_SET_L4_SRC_PORT) {
5592 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
5594 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
5598 case OFPACT_REG_MOVE:
5599 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
5601 case OFPACT_SET_FIELD:
5602 mf = ofpact_get_SET_FIELD(a)->field;
5603 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
5604 if (!mf_are_prereqs_ok(mf, flow) ||
5605 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
5606 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
5608 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5610 /* Remember if we saw a vlan tag in the flow to aid translating to
5611 * OpenFlow 1.1 if need be. */
5612 ofpact_get_SET_FIELD(a)->flow_has_vlan =
5613 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5614 if (mf->id == MFF_VLAN_TCI) {
5615 /* The set field may add or remove the vlan tag,
5616 * Mark the status temporarily. */
5617 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
5621 case OFPACT_STACK_PUSH:
5622 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
5624 case OFPACT_STACK_POP:
5625 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
5627 case OFPACT_SET_MPLS_LABEL:
5628 case OFPACT_SET_MPLS_TC:
5629 case OFPACT_SET_MPLS_TTL:
5630 case OFPACT_DEC_MPLS_TTL:
5631 if (!eth_type_mpls(flow->dl_type)) {
5632 inconsistent_match(usable_protocols);
5636 case OFPACT_SET_TUNNEL:
5637 case OFPACT_SET_QUEUE:
5638 case OFPACT_POP_QUEUE:
5639 case OFPACT_RESUBMIT:
5642 case OFPACT_FIN_TIMEOUT:
5643 if (flow->nw_proto != IPPROTO_TCP) {
5644 inconsistent_match(usable_protocols);
5649 return learn_check(ofpact_get_LEARN(a), flow);
5651 case OFPACT_CONJUNCTION:
5654 case OFPACT_MULTIPATH:
5655 return multipath_check(ofpact_get_MULTIPATH(a), flow);
5661 case OFPACT_PUSH_MPLS:
5662 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
5663 /* The packet is now MPLS and the MPLS payload is opaque.
5664 * Thus nothing can be assumed about the network protocol.
5665 * Temporarily mark that we have no nw_proto. */
5669 case OFPACT_POP_MPLS:
5670 if (!eth_type_mpls(flow->dl_type)) {
5671 inconsistent_match(usable_protocols);
5673 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
5679 case OFPACT_CLEAR_ACTIONS:
5682 case OFPACT_WRITE_ACTIONS: {
5683 /* Use a temporary copy of 'usable_protocols' because we can't check
5684 * consistency of an action set. */
5685 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
5686 enum ofputil_protocol p = *usable_protocols;
5687 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
5688 flow, max_ports, table_id, n_tables, &p);
5691 case OFPACT_WRITE_METADATA:
5694 case OFPACT_METER: {
5695 uint32_t mid = ofpact_get_METER(a)->meter_id;
5696 if (mid == 0 || mid > OFPM13_MAX) {
5697 return OFPERR_OFPMMFC_INVALID_METER;
5702 case OFPACT_GOTO_TABLE: {
5703 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
5704 if ((table_id != 255 && goto_table <= table_id)
5705 || (n_tables != 255 && goto_table >= n_tables)) {
5706 return OFPERR_OFPBIC_BAD_TABLE_ID;
5714 case OFPACT_UNROLL_XLATE:
5715 /* UNROLL is an internal action that should never be seen via
5717 return OFPERR_OFPBAC_BAD_TYPE;
5719 case OFPACT_DEBUG_RECIRC:
5727 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5728 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
5729 * switch with no more than 'max_ports' ports.
5731 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
5732 * '*usable_protocols' the protocols that forbid the inconsistency. (An
5733 * example of an inconsistency between match and actions is a flow that does
5734 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
5736 * May annotate ofpacts with information gathered from the 'flow'.
5738 * May temporarily modify 'flow', but restores the changes before returning. */
5740 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
5741 struct flow *flow, ofp_port_t max_ports,
5742 uint8_t table_id, uint8_t n_tables,
5743 enum ofputil_protocol *usable_protocols)
5746 ovs_be16 dl_type = flow->dl_type;
5747 ovs_be16 vlan_tci = flow->vlan_tci;
5748 uint8_t nw_proto = flow->nw_proto;
5749 enum ofperr error = 0;
5751 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5752 error = ofpact_check__(usable_protocols, a, flow,
5753 max_ports, table_id, n_tables);
5758 /* Restore fields that may have been modified. */
5759 flow->dl_type = dl_type;
5760 flow->vlan_tci = vlan_tci;
5761 flow->nw_proto = nw_proto;
5765 /* Like ofpacts_check(), but reports inconsistencies as
5766 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
5768 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
5769 struct flow *flow, ofp_port_t max_ports,
5770 uint8_t table_id, uint8_t n_tables,
5771 enum ofputil_protocol usable_protocols)
5773 enum ofputil_protocol p = usable_protocols;
5776 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
5777 table_id, n_tables, &p);
5778 return (error ? error
5779 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
5783 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
5784 * appropriate order as defined by the OpenFlow spec and as required by Open
5787 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
5788 * instructions that are allowed within 'ofpacts[]'. */
5790 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
5791 uint32_t allowed_ovsinsts)
5793 const struct ofpact *a;
5794 enum ovs_instruction_type inst;
5796 inst = OVSINST_OFPIT13_METER;
5797 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5798 enum ovs_instruction_type next;
5800 if (a->type == OFPACT_CONJUNCTION) {
5801 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5802 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
5803 VLOG_WARN("\"conjunction\" actions may be used along with "
5804 "\"note\" but not any other kind of action "
5805 "(such as the \"%s\" action used here)",
5806 ofpact_name(a->type));
5807 return OFPERR_NXBAC_BAD_CONJUNCTION;
5813 next = ovs_instruction_type_from_ofpact_type(a->type);
5815 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
5818 const char *name = ovs_instruction_name_from_type(inst);
5819 const char *next_name = ovs_instruction_name_from_type(next);
5822 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
5823 "1.1+ compatibility", name);
5825 VLOG_WARN("invalid instruction ordering: %s must appear "
5826 "before %s, for OpenFlow 1.1+ compatibility",
5829 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
5831 if (!((1u << next) & allowed_ovsinsts)) {
5832 const char *name = ovs_instruction_name_from_type(next);
5834 VLOG_WARN("%s instruction not allowed here", name);
5835 return OFPERR_OFPBIC_UNSUP_INST;
5844 /* Converting ofpacts to OpenFlow. */
5847 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
5851 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5852 case OFPACT_##ENUM: \
5853 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
5862 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
5863 * actions in 'openflow', appending the actions to any existing data in
5866 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
5867 struct ofpbuf *openflow,
5868 enum ofp_version ofp_version)
5870 const struct ofpact *a;
5871 size_t start_size = openflow->size;
5873 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5874 encode_ofpact(a, ofp_version, openflow);
5876 return openflow->size - start_size;
5879 static enum ovs_instruction_type
5880 ofpact_is_apply_actions(const struct ofpact *a)
5882 return (ovs_instruction_type_from_ofpact_type(a->type)
5883 == OVSINST_OFPIT11_APPLY_ACTIONS);
5887 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
5889 struct ofpbuf *openflow,
5890 enum ofp_version ofp_version)
5892 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
5893 const struct ofpact *a;
5895 if (ofp_version == OFP10_VERSION) {
5896 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
5903 if (ofpact_is_apply_actions(a)) {
5904 size_t ofs = openflow->size;
5906 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
5908 encode_ofpact(a, ofp_version, openflow);
5910 } while (a < end && ofpact_is_apply_actions(a));
5911 ofpacts_update_instruction_actions(openflow, ofs);
5913 encode_ofpact(a, ofp_version, openflow);
5919 /* Sets of supported actions. */
5921 /* Two-way translation between OVS's internal "OFPACT_*" representation of
5922 * actions and the "OFPAT_*" representation used in some OpenFlow version.
5923 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
5924 * instance is specific to one OpenFlow version.) */
5926 enum ofpact_type ofpact; /* Internal name for action type. */
5927 int ofpat; /* OFPAT_* number from OpenFlow spec. */
5930 static const struct ofpact_map *
5931 get_ofpact_map(enum ofp_version version)
5933 /* OpenFlow 1.0 actions. */
5934 static const struct ofpact_map of10[] = {
5935 { OFPACT_OUTPUT, 0 },
5936 { OFPACT_SET_VLAN_VID, 1 },
5937 { OFPACT_SET_VLAN_PCP, 2 },
5938 { OFPACT_STRIP_VLAN, 3 },
5939 { OFPACT_SET_ETH_SRC, 4 },
5940 { OFPACT_SET_ETH_DST, 5 },
5941 { OFPACT_SET_IPV4_SRC, 6 },
5942 { OFPACT_SET_IPV4_DST, 7 },
5943 { OFPACT_SET_IP_DSCP, 8 },
5944 { OFPACT_SET_L4_SRC_PORT, 9 },
5945 { OFPACT_SET_L4_DST_PORT, 10 },
5946 { OFPACT_ENQUEUE, 11 },
5950 /* OpenFlow 1.1 actions. */
5951 static const struct ofpact_map of11[] = {
5952 { OFPACT_OUTPUT, 0 },
5953 { OFPACT_SET_VLAN_VID, 1 },
5954 { OFPACT_SET_VLAN_PCP, 2 },
5955 { OFPACT_SET_ETH_SRC, 3 },
5956 { OFPACT_SET_ETH_DST, 4 },
5957 { OFPACT_SET_IPV4_SRC, 5 },
5958 { OFPACT_SET_IPV4_DST, 6 },
5959 { OFPACT_SET_IP_DSCP, 7 },
5960 { OFPACT_SET_IP_ECN, 8 },
5961 { OFPACT_SET_L4_SRC_PORT, 9 },
5962 { OFPACT_SET_L4_DST_PORT, 10 },
5963 /* OFPAT_COPY_TTL_OUT (11) not supported. */
5964 /* OFPAT_COPY_TTL_IN (12) not supported. */
5965 { OFPACT_SET_MPLS_LABEL, 13 },
5966 { OFPACT_SET_MPLS_TC, 14 },
5967 { OFPACT_SET_MPLS_TTL, 15 },
5968 { OFPACT_DEC_MPLS_TTL, 16 },
5969 { OFPACT_PUSH_VLAN, 17 },
5970 { OFPACT_STRIP_VLAN, 18 },
5971 { OFPACT_PUSH_MPLS, 19 },
5972 { OFPACT_POP_MPLS, 20 },
5973 { OFPACT_SET_QUEUE, 21 },
5974 { OFPACT_GROUP, 22 },
5975 { OFPACT_SET_IP_TTL, 23 },
5976 { OFPACT_DEC_TTL, 24 },
5980 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
5981 static const struct ofpact_map of12[] = {
5982 { OFPACT_OUTPUT, 0 },
5983 /* OFPAT_COPY_TTL_OUT (11) not supported. */
5984 /* OFPAT_COPY_TTL_IN (12) not supported. */
5985 { OFPACT_SET_MPLS_TTL, 15 },
5986 { OFPACT_DEC_MPLS_TTL, 16 },
5987 { OFPACT_PUSH_VLAN, 17 },
5988 { OFPACT_STRIP_VLAN, 18 },
5989 { OFPACT_PUSH_MPLS, 19 },
5990 { OFPACT_POP_MPLS, 20 },
5991 { OFPACT_SET_QUEUE, 21 },
5992 { OFPACT_GROUP, 22 },
5993 { OFPACT_SET_IP_TTL, 23 },
5994 { OFPACT_DEC_TTL, 24 },
5995 { OFPACT_SET_FIELD, 25 },
5996 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
5997 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
6017 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
6018 * values, into a bitmap of actions suitable for OpenFlow 'version', and
6019 * returns the result. */
6021 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
6023 uint32_t openflow_bitmap = 0;
6024 const struct ofpact_map *x;
6026 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6027 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
6028 openflow_bitmap |= 1u << x->ofpat;
6031 return htonl(openflow_bitmap);
6034 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
6035 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
6036 * and returns the result. */
6038 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
6040 uint64_t ofpact_bitmap = 0;
6041 const struct ofpact_map *x;
6043 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6044 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
6045 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
6048 return ofpact_bitmap;
6051 /* Appends to 's' a string representation of the set of OFPACT_* represented
6052 * by 'ofpacts_bitmap'. */
6054 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
6056 if (!ofpacts_bitmap) {
6057 ds_put_cstr(s, "<none>");
6059 while (ofpacts_bitmap) {
6060 ds_put_format(s, "%s ",
6061 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
6062 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
6068 /* Returns true if 'action' outputs to 'port', false otherwise. */
6070 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
6072 switch (ofpact->type) {
6074 return ofpact_get_OUTPUT(ofpact)->port == port;
6075 case OFPACT_ENQUEUE:
6076 return ofpact_get_ENQUEUE(ofpact)->port == port;
6077 case OFPACT_CONTROLLER:
6078 return port == OFPP_CONTROLLER;
6080 case OFPACT_OUTPUT_REG:
6082 case OFPACT_SET_VLAN_VID:
6083 case OFPACT_SET_VLAN_PCP:
6084 case OFPACT_STRIP_VLAN:
6085 case OFPACT_PUSH_VLAN:
6086 case OFPACT_SET_ETH_SRC:
6087 case OFPACT_SET_ETH_DST:
6088 case OFPACT_SET_IPV4_SRC:
6089 case OFPACT_SET_IPV4_DST:
6090 case OFPACT_SET_IP_DSCP:
6091 case OFPACT_SET_IP_ECN:
6092 case OFPACT_SET_IP_TTL:
6093 case OFPACT_SET_L4_SRC_PORT:
6094 case OFPACT_SET_L4_DST_PORT:
6095 case OFPACT_REG_MOVE:
6096 case OFPACT_SET_FIELD:
6097 case OFPACT_STACK_PUSH:
6098 case OFPACT_STACK_POP:
6099 case OFPACT_DEC_TTL:
6100 case OFPACT_SET_MPLS_LABEL:
6101 case OFPACT_SET_MPLS_TC:
6102 case OFPACT_SET_MPLS_TTL:
6103 case OFPACT_DEC_MPLS_TTL:
6104 case OFPACT_SET_TUNNEL:
6105 case OFPACT_WRITE_METADATA:
6106 case OFPACT_SET_QUEUE:
6107 case OFPACT_POP_QUEUE:
6108 case OFPACT_FIN_TIMEOUT:
6109 case OFPACT_RESUBMIT:
6111 case OFPACT_CONJUNCTION:
6112 case OFPACT_MULTIPATH:
6115 case OFPACT_UNROLL_XLATE:
6116 case OFPACT_PUSH_MPLS:
6117 case OFPACT_POP_MPLS:
6119 case OFPACT_CLEAR_ACTIONS:
6120 case OFPACT_WRITE_ACTIONS:
6121 case OFPACT_GOTO_TABLE:
6124 case OFPACT_DEBUG_RECIRC:
6130 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6131 * to 'port', false otherwise. */
6133 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
6136 const struct ofpact *a;
6138 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6139 if (ofpact_outputs_to_port(a, port)) {
6147 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6148 * to 'group', false otherwise. */
6150 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
6153 const struct ofpact *a;
6155 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6156 if (a->type == OFPACT_GROUP
6157 && ofpact_get_GROUP(a)->group_id == group_id) {
6166 ofpacts_equal(const struct ofpact *a, size_t a_len,
6167 const struct ofpact *b, size_t b_len)
6169 return a_len == b_len && !memcmp(a, b, a_len);
6172 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
6173 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
6175 * This function relies on the order of 'ofpacts' being correct (as checked by
6176 * ofpacts_verify()). */
6178 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
6180 const struct ofpact *a;
6182 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6183 enum ovs_instruction_type inst;
6185 inst = ovs_instruction_type_from_ofpact_type(a->type);
6186 if (a->type == OFPACT_METER) {
6187 return ofpact_get_METER(a)->meter_id;
6188 } else if (inst > OVSINST_OFPIT13_METER) {
6196 /* Formatting ofpacts. */
6199 ofpact_format(const struct ofpact *a, struct ds *s)
6202 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6203 case OFPACT_##ENUM: \
6204 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
6213 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
6214 * 'ofpacts' to 'string'. */
6216 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
6220 ds_put_cstr(string, "drop");
6222 const struct ofpact *a;
6224 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6226 ds_put_cstr(string, ",");
6229 /* XXX write-actions */
6230 ofpact_format(a, string);
6235 /* Internal use by helpers. */
6238 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
6240 struct ofpact *ofpact;
6242 ofpact_pad(ofpacts);
6243 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
6244 ofpact = ofpacts->header;
6245 ofpact_init(ofpact, type, len);
6250 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
6252 memset(ofpact, 0, len);
6253 ofpact->type = type;
6258 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6259 * starting at 'ofpact'.
6261 * This is the correct way to update a variable-length ofpact's length after
6262 * adding the variable-length part of the payload. (See the large comment
6263 * near the end of ofp-actions.h for more information.) */
6265 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
6267 ovs_assert(ofpact == ofpacts->header);
6268 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6271 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6272 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6273 * client must call this itself after adding the final ofpact to an array of
6276 * (The consequences of failing to call this function are probably not dire.
6277 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6278 * not dereference it. That's undefined behavior, technically, but it will not
6279 * cause a real problem on common systems. Still, it seems better to call
6282 ofpact_pad(struct ofpbuf *ofpacts)
6284 unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
6286 ofpbuf_put_zeros(ofpacts, pad);
6293 static char * OVS_WARN_UNUSED_RESULT
6294 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6295 enum ofputil_protocol *usable_protocols)
6298 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6299 case OFPACT_##ENUM: \
6300 return parse_##ENUM(value, ofpacts, usable_protocols);
6309 ofpact_type_from_name(const char *name, enum ofpact_type *type)
6311 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6312 if (!strcasecmp(name, NAME)) { \
6313 *type = OFPACT_##ENUM; \
6322 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6324 * Returns NULL if successful, otherwise a malloc()'d string describing the
6325 * error. The caller is responsible for freeing the returned string. */
6326 static char * OVS_WARN_UNUSED_RESULT
6327 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6328 enum ofputil_protocol *usable_protocols,
6329 bool allow_instructions)
6338 while (ofputil_parse_key_value(&pos, &key, &value)) {
6339 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6340 enum ofpact_type type;
6344 if (ofpact_type_from_name(key, &type)) {
6345 error = ofpact_parse(type, value, ofpacts, usable_protocols);
6346 inst = ovs_instruction_type_from_ofpact_type(type);
6347 } else if (!strcasecmp(key, "mod_vlan_vid")) {
6348 error = parse_set_vlan_vid(value, ofpacts, true);
6349 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6350 error = parse_set_vlan_pcp(value, ofpacts, true);
6351 } else if (!strcasecmp(key, "set_nw_ttl")) {
6352 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6353 } else if (!strcasecmp(key, "pop_vlan")) {
6354 error = parse_pop_vlan(ofpacts);
6355 } else if (!strcasecmp(key, "set_tunnel64")) {
6356 error = parse_set_tunnel(value, ofpacts,
6357 NXAST_RAW_SET_TUNNEL64);
6358 } else if (!strcasecmp(key, "load")) {
6359 error = parse_reg_load(value, ofpacts);
6360 } else if (!strcasecmp(key, "bundle_load")) {
6361 error = parse_bundle_load(value, ofpacts);
6362 } else if (!strcasecmp(key, "drop")) {
6364 } else if (!strcasecmp(key, "apply_actions")) {
6365 return xstrdup("apply_actions is the default instruction");
6366 } else if (ofputil_port_from_string(key, &port)) {
6367 ofpact_put_OUTPUT(ofpacts)->port = port;
6369 return xasprintf("unknown action %s", key);
6375 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
6376 if (!allow_instructions) {
6377 return xasprintf("only actions are allowed here (not "
6379 ovs_instruction_name_from_type(inst));
6381 if (inst == prev_inst) {
6382 return xasprintf("instruction %s may be specified only once",
6383 ovs_instruction_name_from_type(inst));
6386 if (prev_inst != -1 && inst < prev_inst) {
6387 return xasprintf("instruction %s must be specified before %s",
6388 ovs_instruction_name_from_type(inst),
6389 ovs_instruction_name_from_type(prev_inst));
6393 ofpact_pad(ofpacts);
6395 if (drop && ofpacts->size) {
6396 return xstrdup("\"drop\" must not be accompanied by any other action "
6400 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
6402 ? (1u << N_OVS_INSTRUCTIONS) - 1
6403 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS));
6405 return xstrdup("Incorrect instruction ordering");
6411 static char * OVS_WARN_UNUSED_RESULT
6412 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
6413 enum ofputil_protocol *usable_protocols, bool allow_instructions)
6415 uint32_t orig_size = ofpacts->size;
6416 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
6417 allow_instructions);
6419 ofpacts->size = orig_size;
6424 static char * OVS_WARN_UNUSED_RESULT
6425 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
6426 enum ofputil_protocol *usable_protocols,
6427 bool allow_instructions)
6431 *usable_protocols = OFPUTIL_P_ANY;
6434 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions);
6440 /* Parses 's' as a set of OpenFlow actions and appends the actions to
6443 * Returns NULL if successful, otherwise a malloc()'d string describing the
6444 * error. The caller is responsible for freeing the returned string. */
6445 char * OVS_WARN_UNUSED_RESULT
6446 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
6447 enum ofputil_protocol *usable_protocols)
6449 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false);
6452 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
6455 * Returns NULL if successful, otherwise a malloc()'d string describing the
6456 * error. The caller is responsible for freeing the returned string. */
6457 char * OVS_WARN_UNUSED_RESULT
6458 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
6459 enum ofputil_protocol *usable_protocols)
6461 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true);
6465 ofpact_name(enum ofpact_type type)
6468 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
6475 /* Low-level action decoding and encoding functions. */
6477 /* Everything needed to identify a particular OpenFlow action. */
6478 struct ofpact_hdrs {
6479 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
6480 uint16_t type; /* Type if standard, otherwise subtype. */
6481 uint8_t ofp_version; /* From ofp_header. */
6484 /* Information about a particular OpenFlow action. */
6485 struct ofpact_raw_instance {
6486 /* The action's identity. */
6487 struct ofpact_hdrs hdrs;
6488 enum ofp_raw_action_type raw;
6490 /* Looking up the action. */
6491 struct hmap_node decode_node; /* Based on 'hdrs'. */
6492 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
6494 /* The action's encoded size.
6496 * If this action is fixed-length, 'min_length' == 'max_length'.
6497 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
6498 * OFP_ACTION_ALIGN) == 65528. */
6499 unsigned short int min_length;
6500 unsigned short int max_length;
6502 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
6503 * offset of that argument from the beginning of the action and 'arg_len'
6504 * its length, both in bytes.
6506 * For actions that take other forms, these are both zero. */
6507 unsigned short int arg_ofs;
6508 unsigned short int arg_len;
6510 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
6513 /* If this action is deprecated, a human-readable string with a brief
6515 const char *deprecation;
6518 /* Action header. */
6519 struct ofp_action_header {
6520 /* The meaning of other values of 'type' generally depends on the OpenFlow
6521 * version (see enum ofp_raw_action_type).
6523 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
6524 * designates an OpenFlow vendor ID and that the remainder of the action
6525 * structure has a vendor-defined meaning.
6527 #define OFPAT_VENDOR 0xffff
6530 /* Always a multiple of 8. */
6533 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
6534 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
6537 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
6539 /* Header for Nicira-defined actions and for ONF vendor extensions.
6541 * This cannot be used as an entirely generic vendor extension action header,
6542 * because OpenFlow does not specify the location or size of the action
6543 * subtype; it just happens that ONF extensions and Nicira extensions share
6545 struct ext_action_header {
6546 ovs_be16 type; /* OFPAT_VENDOR. */
6547 ovs_be16 len; /* At least 16. */
6548 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
6549 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
6552 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
6555 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
6556 const struct ofpact_hdrs *b)
6558 return (a->vendor == b->vendor
6559 && a->type == b->type
6560 && a->ofp_version == b->ofp_version);
6564 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
6566 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
6569 #include "ofp-actions.inc2"
6571 static struct hmap *
6572 ofpact_decode_hmap(void)
6574 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6575 static struct hmap hmap;
6577 if (ovsthread_once_start(&once)) {
6578 struct ofpact_raw_instance *inst;
6581 for (inst = all_raw_instances;
6582 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6584 hmap_insert(&hmap, &inst->decode_node,
6585 ofpact_hdrs_hash(&inst->hdrs));
6587 ovsthread_once_done(&once);
6592 static struct hmap *
6593 ofpact_encode_hmap(void)
6595 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6596 static struct hmap hmap;
6598 if (ovsthread_once_start(&once)) {
6599 struct ofpact_raw_instance *inst;
6602 for (inst = all_raw_instances;
6603 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6605 hmap_insert(&hmap, &inst->encode_node,
6606 hash_2words(inst->raw, inst->hdrs.ofp_version));
6608 ovsthread_once_done(&once);
6614 ofpact_decode_raw(enum ofp_version ofp_version,
6615 const struct ofp_action_header *oah, size_t length,
6616 const struct ofpact_raw_instance **instp)
6618 const struct ofpact_raw_instance *inst;
6619 struct ofpact_hdrs hdrs;
6622 if (length < sizeof *oah) {
6623 return OFPERR_OFPBAC_BAD_LEN;
6626 /* Get base action type. */
6627 if (oah->type == htons(OFPAT_VENDOR)) {
6629 hdrs.vendor = ntohl(oah->vendor);
6630 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
6631 /* Get extension subtype. */
6632 const struct ext_action_header *nah;
6634 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
6635 if (length < sizeof *nah) {
6636 return OFPERR_OFPBAC_BAD_LEN;
6638 hdrs.type = ntohs(nah->subtype);
6640 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
6642 return OFPERR_OFPBAC_BAD_VENDOR;
6646 hdrs.type = ntohs(oah->type);
6649 hdrs.ofp_version = ofp_version;
6650 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
6651 ofpact_decode_hmap()) {
6652 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
6659 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
6660 : OFPERR_OFPBAC_BAD_TYPE);
6664 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6665 enum ofp_raw_action_type *raw, uint64_t *arg)
6667 const struct ofp_action_header *oah = buf->data;
6668 const struct ofpact_raw_instance *action;
6669 unsigned int length;
6673 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
6678 if (action->deprecation) {
6679 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
6680 action->name, ofputil_version_to_string(ofp_version),
6681 action->deprecation);
6684 length = ntohs(oah->len);
6685 if (length > buf->size) {
6686 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
6687 "length %"PRIu32, action->name, length, buf->size);
6688 return OFPERR_OFPBAC_BAD_LEN;
6690 if (length < action->min_length || length > action->max_length) {
6691 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
6692 "[%hu,%hu]", action->name, length,
6693 action->min_length, action->max_length);
6694 return OFPERR_OFPBAC_BAD_LEN;
6697 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
6698 "of 8", action->name, length);
6699 return OFPERR_OFPBAC_BAD_LEN;
6704 if (action->arg_len) {
6708 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
6709 for (i = 0; i < action->arg_len; i++) {
6710 *arg = (*arg << 8) | p[i];
6714 ofpbuf_pull(buf, length);
6719 static const struct ofpact_raw_instance *
6720 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
6722 const struct ofpact_raw_instance *inst;
6724 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
6725 ofpact_encode_hmap()) {
6726 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
6734 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6735 enum ofp_raw_action_type raw, uint64_t arg)
6737 const struct ofpact_raw_instance *inst;
6738 struct ofp_action_header *oah;
6739 const struct ofpact_hdrs *hdrs;
6741 inst = ofpact_raw_lookup(ofp_version, raw);
6744 oah = ofpbuf_put_zeros(buf, inst->min_length);
6745 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
6746 oah->len = htons(inst->min_length);
6747 oah->vendor = htonl(hdrs->vendor);
6749 switch (hdrs->vendor) {
6754 case ONF_VENDOR_ID: {
6755 struct ext_action_header *nah = (struct ext_action_header *) oah;
6756 nah->subtype = htons(hdrs->type);
6764 if (inst->arg_len) {
6765 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
6768 for (i = 0; i < inst->arg_len; i++) {
6780 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
6782 struct ofp_action_header *oah;
6784 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs, 8));
6786 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
6787 oah->len = htons(openflow->size - start_ofs);