2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-actions.h"
20 #include "byte-order.h"
23 #include "dynamic-string.h"
26 #include "meta-flow.h"
27 #include "multipath.h"
29 #include "ofp-parse.h"
32 #include "unaligned.h"
34 #include "openvswitch/vlog.h"
36 VLOG_DEFINE_THIS_MODULE(ofp_actions);
38 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
40 struct ofp_action_header;
42 /* Raw identifiers for OpenFlow actions.
44 * Decoding and encoding OpenFlow actions across multiple versions is difficult
45 * to do in a clean, consistent way. This enumeration lays out all of the
46 * forms of actions that Open vSwitch supports.
48 * The comments here must follow a stylized form because the
49 * "extract-ofp-actions" program parses them at build time to generate data
52 * - The first part of each comment specifies the vendor, OpenFlow versions,
53 * and type for each protocol that supports the action:
55 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
56 * extension actions. (Support for other vendors can be added, but
57 * it can't be done just based on a vendor ID definition alone
58 * because OpenFlow doesn't define a standard way to specify a
59 * subtype for vendor actions, so other vendors might do it different
62 * # The version can specify a specific OpenFlow version, a version
63 * range delimited by "-", or an open-ended range with "+".
65 * # The type, in parentheses, is the action type number (for standard
66 * OpenFlow actions) or subtype (for vendor extension actions).
68 * # Optionally one may add "is deprecated" followed by a
69 * human-readable reason in parentheses (which will be used in log
70 * messages), if a particular action should no longer be used.
72 * Multiple such specifications may be separated by commas.
74 * - The second part describes the action's wire format. It may be:
76 * # "struct <name>": The struct fully specifies the wire format. The
77 * action is exactly the size of the struct. (Thus, the struct must
78 * be an exact multiple of 8 bytes in size.)
80 * # "struct <name>, ...": The struct specifies the beginning of the
81 * wire format. An instance of the action is either the struct's
82 * exact size, or a multiple of 8 bytes longer.
84 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
85 * vendor extension) header, followed by 0 or more pad bytes to align
86 * to a multiple of <N> bits, followed by an argument of the given
87 * type, followed by 0 or more pad bytes to bring the total action up
88 * to a multiple of 8 bytes.
90 * # "void": The action is just a (standard or vendor extension)
93 * - Optional additional text enclosed in square brackets is commentary for
96 enum ofp_raw_action_type {
97 /* ## ----------------- ## */
98 /* ## Standard actions. ## */
99 /* ## ----------------- ## */
101 /* OF1.0(0): struct ofp10_action_output. */
103 /* OF1.1+(0): struct ofp11_action_output. */
106 /* OF1.0(1): uint16_t. */
107 OFPAT_RAW10_SET_VLAN_VID,
108 /* OF1.0(2): uint8_t. */
109 OFPAT_RAW10_SET_VLAN_PCP,
111 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
113 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
114 * modification actions: the 1.0 versions push a VLAN header if none is
115 * present, but the 1.1 versions do not. That is the only reason that we
116 * distinguish their raw action types.] */
117 OFPAT_RAW11_SET_VLAN_VID,
118 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
119 OFPAT_RAW11_SET_VLAN_PCP,
121 /* OF1.1+(17): ovs_be16.
123 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
125 OFPAT_RAW11_PUSH_VLAN,
127 /* OF1.0(3): void. */
128 OFPAT_RAW10_STRIP_VLAN,
129 /* OF1.1+(18): void. */
130 OFPAT_RAW11_POP_VLAN,
132 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
133 * ofp_action_dl_addr. */
134 OFPAT_RAW_SET_DL_SRC,
136 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
137 * ofp_action_dl_addr. */
138 OFPAT_RAW_SET_DL_DST,
140 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
142 OFPAT_RAW_SET_NW_SRC,
144 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
146 OFPAT_RAW_SET_NW_DST,
148 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
149 OFPAT_RAW_SET_NW_TOS,
151 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
152 OFPAT_RAW11_SET_NW_ECN,
154 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
156 OFPAT_RAW_SET_TP_SRC,
158 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
160 OFPAT_RAW_SET_TP_DST,
162 /* OF1.0(11): struct ofp10_action_enqueue. */
165 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
167 OFPAT_RAW_SET_MPLS_LABEL,
169 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
171 OFPAT_RAW_SET_MPLS_TC,
173 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
175 OFPAT_RAW_SET_MPLS_TTL,
177 /* NX1.0(26), OF1.1+(16): void. */
178 OFPAT_RAW_DEC_MPLS_TTL,
180 /* NX1.0(23), OF1.1+(19): ovs_be16.
182 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
185 /* NX1.0(24), OF1.1+(20): ovs_be16.
187 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
188 * ETH_TYPE_MPLS otherwise, not the label.] */
191 /* NX1.0(4), OF1.1+(21): uint32_t. */
194 /* OF1.1+(22): uint32_t. */
197 /* OF1.1+(23): uint8_t. */
198 OFPAT_RAW11_SET_NW_TTL,
200 /* NX1.0(18), OF1.1+(24): void. */
201 OFPAT_RAW_DEC_NW_TTL,
202 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
203 NXAST_RAW_DEC_TTL_CNT_IDS,
205 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
206 OFPAT_RAW12_SET_FIELD,
207 /* OF1.5+(25): struct ofp12_action_set_field, ... */
208 OFPAT_RAW15_SET_FIELD,
209 /* NX1.0-1.4(7): struct nx_action_reg_load.
211 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
212 * we drop reg_load.] */
214 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
216 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
217 * we drop reg_load2.] */
220 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
221 OFPAT_RAW15_COPY_FIELD,
222 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
223 ONFACT_RAW13_COPY_FIELD,
224 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
227 /* ## ------------------------- ## */
228 /* ## Nicira extension actions. ## */
229 /* ## ------------------------- ## */
231 /* Actions similar to standard actions are listed with the standard actions. */
233 /* NX1.0+(1): uint16_t. */
235 /* NX1.0+(14): struct nx_action_resubmit. */
236 NXAST_RAW_RESUBMIT_TABLE,
238 /* NX1.0+(2): uint32_t. */
239 NXAST_RAW_SET_TUNNEL,
240 /* NX1.0+(9): uint64_t. */
241 NXAST_RAW_SET_TUNNEL64,
243 /* NX1.0+(5): void. */
246 /* NX1.0+(8): struct nx_action_note, ... */
249 /* NX1.0+(10): struct nx_action_multipath. */
252 /* NX1.0+(12): struct nx_action_bundle, ... */
254 /* NX1.0+(13): struct nx_action_bundle, ... */
255 NXAST_RAW_BUNDLE_LOAD,
257 /* NX1.0+(15): struct nx_action_output_reg. */
258 NXAST_RAW_OUTPUT_REG,
259 /* NX1.0+(32): struct nx_action_output_reg2. */
260 NXAST_RAW_OUTPUT_REG2,
262 /* NX1.0+(16): struct nx_action_learn, ... */
265 /* NX1.0+(17): void. */
268 /* NX1.0+(19): struct nx_action_fin_timeout. */
269 NXAST_RAW_FIN_TIMEOUT,
271 /* NX1.0+(20): struct nx_action_controller. */
272 NXAST_RAW_CONTROLLER,
274 /* NX1.0+(22): struct nx_action_write_metadata. */
275 NXAST_RAW_WRITE_METADATA,
277 /* NX1.0+(27): struct nx_action_stack. */
278 NXAST_RAW_STACK_PUSH,
280 /* NX1.0+(28): struct nx_action_stack. */
283 /* NX1.0+(29): struct nx_action_sample. */
286 /* NX1.0+(34): struct nx_action_conjunction. */
287 NXAST_RAW_CONJUNCTION,
289 /* ## ------------------ ## */
290 /* ## Debugging actions. ## */
291 /* ## ------------------ ## */
293 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
294 /* accepts them only if started with --enable-dummy. */
296 /* NX1.0+(255): void. */
297 NXAST_RAW_DEBUG_RECIRC,
300 /* OpenFlow actions are always a multiple of 8 bytes in length. */
301 #define OFP_ACTION_ALIGN 8
303 /* Define a few functions for working with instructions. */
304 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
305 static inline const struct STRUCT * OVS_UNUSED \
306 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
308 ovs_assert(inst->type == htons(ENUM)); \
309 return ALIGNED_CAST(struct STRUCT *, inst); \
312 static inline void OVS_UNUSED \
313 instruction_init_##ENUM(struct STRUCT *s) \
315 memset(s, 0, sizeof *s); \
316 s->type = htons(ENUM); \
317 s->len = htons(sizeof *s); \
320 static inline struct STRUCT * OVS_UNUSED \
321 instruction_put_##ENUM(struct ofpbuf *buf) \
323 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
324 instruction_init_##ENUM(s); \
330 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
332 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
334 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
335 uint32_t allowed_ovsinsts,
336 enum ofpact_type outer_action);
338 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
339 enum mf_field_id, uint64_t value);
341 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
342 enum ofp_raw_action_type *, uint64_t *arg);
343 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
344 enum ofp_raw_action_type, uint64_t arg);
346 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
347 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
348 bool allow_instructions, enum ofpact_type outer_action);
350 /* Pull off existing actions or instructions. Used by nesting actions to keep
351 * ofpacts_parse() oblivious of actions nesting.
353 * Push the actions back on after nested parsing, e.g.:
355 * size_t ofs = ofpacts_pull(ofpacts);
356 * ...nested parsing...
357 * ofpbuf_push_uninit(ofpacts, ofs);
360 ofpacts_pull(struct ofpbuf *ofpacts)
366 ofpbuf_pull(ofpacts, ofs);
371 #include "ofp-actions.inc1"
373 /* Output actions. */
375 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
376 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
377 * number of bytes to send. A 'max_len' of zero means no bytes of the
378 * packet should be sent. */
379 struct ofp10_action_output {
380 ovs_be16 type; /* OFPAT10_OUTPUT. */
381 ovs_be16 len; /* Length is 8. */
382 ovs_be16 port; /* Output port. */
383 ovs_be16 max_len; /* Max length to send to controller. */
385 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
387 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
388 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
389 * number of bytes to send. A 'max_len' of zero means no bytes of the
390 * packet should be sent.*/
391 struct ofp11_action_output {
392 ovs_be16 type; /* OFPAT11_OUTPUT. */
393 ovs_be16 len; /* Length is 16. */
394 ovs_be32 port; /* Output port. */
395 ovs_be16 max_len; /* Max length to send to controller. */
396 uint8_t pad[6]; /* Pad to 64 bits. */
398 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
401 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
404 struct ofpact_output *output;
406 output = ofpact_put_OUTPUT(out);
407 output->port = u16_to_ofp(ntohs(oao->port));
408 output->max_len = ntohs(oao->max_len);
410 return ofpact_check_output_port(output->port, OFPP_MAX);
414 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
417 struct ofpact_output *output;
420 output = ofpact_put_OUTPUT(out);
421 output->max_len = ntohs(oao->max_len);
423 error = ofputil_port_from_ofp11(oao->port, &output->port);
428 return ofpact_check_output_port(output->port, OFPP_MAX);
432 encode_OUTPUT(const struct ofpact_output *output,
433 enum ofp_version ofp_version, struct ofpbuf *out)
435 if (ofp_version == OFP10_VERSION) {
436 struct ofp10_action_output *oao;
438 oao = put_OFPAT10_OUTPUT(out);
439 oao->port = htons(ofp_to_u16(output->port));
440 oao->max_len = htons(output->max_len);
442 struct ofp11_action_output *oao;
444 oao = put_OFPAT11_OUTPUT(out);
445 oao->port = ofputil_port_to_ofp11(output->port);
446 oao->max_len = htons(output->max_len);
450 static char * OVS_WARN_UNUSED_RESULT
451 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
452 enum ofputil_protocol *usable_protocols OVS_UNUSED)
454 if (strchr(arg, '[')) {
455 struct ofpact_output_reg *output_reg;
457 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
458 output_reg->max_len = UINT16_MAX;
459 return mf_parse_subfield(&output_reg->src, arg);
461 struct ofpact_output *output;
463 output = ofpact_put_OUTPUT(ofpacts);
464 if (!ofputil_port_from_string(arg, &output->port)) {
465 return xasprintf("%s: output to unknown port", arg);
467 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
473 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
475 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
476 ds_put_format(s, "output:%"PRIu16, a->port);
478 ofputil_format_port(a->port, s);
479 if (a->port == OFPP_CONTROLLER) {
480 ds_put_format(s, ":%"PRIu16, a->max_len);
488 decode_OFPAT_RAW11_GROUP(uint32_t group_id, struct ofpbuf *out)
490 ofpact_put_GROUP(out)->group_id = group_id;
495 encode_GROUP(const struct ofpact_group *group,
496 enum ofp_version ofp_version, struct ofpbuf *out)
498 if (ofp_version == OFP10_VERSION) {
501 put_OFPAT11_GROUP(out, group->group_id);
505 static char * OVS_WARN_UNUSED_RESULT
506 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
507 enum ofputil_protocol *usable_protocols OVS_UNUSED)
509 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
513 format_GROUP(const struct ofpact_group *a, struct ds *s)
515 ds_put_format(s, "group:%"PRIu32, a->group_id);
518 /* Action structure for NXAST_CONTROLLER.
520 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
521 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
524 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
526 * - 'controller_id': The ID of the controller connection to which the
527 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
528 * sent only to controllers that have the specified controller connection
529 * ID. See "struct nx_controller_id" for more information. */
530 struct nx_action_controller {
531 ovs_be16 type; /* OFPAT_VENDOR. */
532 ovs_be16 len; /* Length is 16. */
533 ovs_be32 vendor; /* NX_VENDOR_ID. */
534 ovs_be16 subtype; /* NXAST_CONTROLLER. */
535 ovs_be16 max_len; /* Maximum length to send to controller. */
536 ovs_be16 controller_id; /* Controller ID to send packet-in. */
537 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
538 uint8_t zero; /* Must be zero. */
540 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
543 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
546 struct ofpact_controller *oc;
548 oc = ofpact_put_CONTROLLER(out);
549 oc->max_len = ntohs(nac->max_len);
550 oc->controller_id = ntohs(nac->controller_id);
551 oc->reason = nac->reason;
556 encode_CONTROLLER(const struct ofpact_controller *controller,
557 enum ofp_version ofp_version OVS_UNUSED,
560 struct nx_action_controller *nac;
562 nac = put_NXAST_CONTROLLER(out);
563 nac->max_len = htons(controller->max_len);
564 nac->controller_id = htons(controller->controller_id);
565 nac->reason = controller->reason;
568 static char * OVS_WARN_UNUSED_RESULT
569 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
570 enum ofputil_protocol *usable_protocols OVS_UNUSED)
572 enum ofp_packet_in_reason reason = OFPR_ACTION;
573 uint16_t controller_id = 0;
574 uint16_t max_len = UINT16_MAX;
578 } else if (strspn(arg, "0123456789") == strlen(arg)) {
579 char *error = str_to_u16(arg, "max_len", &max_len);
586 while (ofputil_parse_key_value(&arg, &name, &value)) {
587 if (!strcmp(name, "reason")) {
588 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
589 return xasprintf("unknown reason \"%s\"", value);
591 } else if (!strcmp(name, "max_len")) {
592 char *error = str_to_u16(value, "max_len", &max_len);
596 } else if (!strcmp(name, "id")) {
597 char *error = str_to_u16(value, "id", &controller_id);
602 return xasprintf("unknown key \"%s\" parsing controller "
608 if (reason == OFPR_ACTION && controller_id == 0) {
609 struct ofpact_output *output;
611 output = ofpact_put_OUTPUT(ofpacts);
612 output->port = OFPP_CONTROLLER;
613 output->max_len = max_len;
615 struct ofpact_controller *controller;
617 controller = ofpact_put_CONTROLLER(ofpacts);
618 controller->max_len = max_len;
619 controller->reason = reason;
620 controller->controller_id = controller_id;
627 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
629 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
630 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
632 enum ofp_packet_in_reason reason = a->reason;
634 ds_put_cstr(s, "controller(");
635 if (reason != OFPR_ACTION) {
636 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
638 ds_put_format(s, "reason=%s,",
639 ofputil_packet_in_reason_to_string(
640 reason, reasonbuf, sizeof reasonbuf));
642 if (a->max_len != UINT16_MAX) {
643 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
645 if (a->controller_id != 0) {
646 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
653 /* Enqueue action. */
654 struct ofp10_action_enqueue {
655 ovs_be16 type; /* OFPAT10_ENQUEUE. */
656 ovs_be16 len; /* Len is 16. */
657 ovs_be16 port; /* Port that queue belongs. Should
658 refer to a valid physical port
659 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
660 uint8_t pad[6]; /* Pad for 64-bit alignment. */
661 ovs_be32 queue_id; /* Where to enqueue the packets. */
663 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
666 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
669 struct ofpact_enqueue *enqueue;
671 enqueue = ofpact_put_ENQUEUE(out);
672 enqueue->port = u16_to_ofp(ntohs(oae->port));
673 enqueue->queue = ntohl(oae->queue_id);
674 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
675 && enqueue->port != OFPP_IN_PORT
676 && enqueue->port != OFPP_LOCAL) {
677 return OFPERR_OFPBAC_BAD_OUT_PORT;
683 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
684 enum ofp_version ofp_version, struct ofpbuf *out)
686 if (ofp_version == OFP10_VERSION) {
687 struct ofp10_action_enqueue *oae;
689 oae = put_OFPAT10_ENQUEUE(out);
690 oae->port = htons(ofp_to_u16(enqueue->port));
691 oae->queue_id = htonl(enqueue->queue);
697 static char * OVS_WARN_UNUSED_RESULT
698 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
699 enum ofputil_protocol *usable_protocols OVS_UNUSED)
702 char *port = strtok_r(arg, ":q,", &sp);
703 char *queue = strtok_r(NULL, "", &sp);
704 struct ofpact_enqueue *enqueue;
706 if (port == NULL || queue == NULL) {
707 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
708 "\"enqueue(PORT,QUEUE)\"");
711 enqueue = ofpact_put_ENQUEUE(ofpacts);
712 if (!ofputil_port_from_string(port, &enqueue->port)) {
713 return xasprintf("%s: enqueue to unknown port", port);
715 return str_to_u32(queue, &enqueue->queue);
719 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
721 ds_put_format(s, "enqueue:");
722 ofputil_format_port(a->port, s);
723 ds_put_format(s, ":%"PRIu32, a->queue);
726 /* Action structure for NXAST_OUTPUT_REG.
728 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
730 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
731 * the NXAST_REG_LOAD action.
733 * The acceptable nxm_header values for 'src' are the same as the acceptable
734 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
736 * The 'max_len' field indicates the number of bytes to send when the chosen
737 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
738 * field of OFPAT_OUTPUT.
740 * The 'zero' field is required to be zeroed for forward compatibility. */
741 struct nx_action_output_reg {
742 ovs_be16 type; /* OFPAT_VENDOR. */
743 ovs_be16 len; /* 24. */
744 ovs_be32 vendor; /* NX_VENDOR_ID. */
745 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
747 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
748 ovs_be32 src; /* Source. */
750 ovs_be16 max_len; /* Max length to send to controller. */
752 uint8_t zero[6]; /* Reserved, must be zero. */
754 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
756 /* Action structure for NXAST_OUTPUT_REG2.
758 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
759 * experimenter OXM as 'src'.
761 struct nx_action_output_reg2 {
762 ovs_be16 type; /* OFPAT_VENDOR. */
763 ovs_be16 len; /* 24. */
764 ovs_be32 vendor; /* NX_VENDOR_ID. */
765 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
767 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
768 ovs_be16 max_len; /* Max length to send to controller. */
771 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
772 * - Enough 0-bytes to pad the action out to 24 bytes. */
775 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
778 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
781 struct ofpact_output_reg *output_reg;
783 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
784 return OFPERR_OFPBAC_BAD_ARGUMENT;
787 output_reg = ofpact_put_OUTPUT_REG(out);
788 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
789 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
790 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
791 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
792 output_reg->max_len = ntohs(naor->max_len);
794 return mf_check_src(&output_reg->src, NULL);
798 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
801 struct ofpact_output_reg *output_reg;
805 output_reg = ofpact_put_OUTPUT_REG(out);
806 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
807 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
808 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
809 output_reg->max_len = ntohs(naor->max_len);
811 ofpbuf_use_const(&b, naor, ntohs(naor->len));
812 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
813 error = nx_pull_header(&b, &output_reg->src.field, NULL);
817 if (!is_all_zeros(b.data, b.size)) {
818 return OFPERR_NXBRC_MUST_BE_ZERO;
821 return mf_check_src(&output_reg->src, NULL);
825 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
826 enum ofp_version ofp_version OVS_UNUSED,
829 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
830 * cannot be encoded in the older form, encode it as
831 * NXAST_RAW_OUTPUT_REG2. */
832 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
833 || !mf_nxm_header(output_reg->src.field->id)) {
834 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
835 size_t size = out->size;
837 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
838 output_reg->src.n_bits);
839 naor->max_len = htons(output_reg->max_len);
841 out->size = size - sizeof naor->pad;
842 nx_put_header(out, output_reg->src.field->id, 0, false);
845 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
847 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
848 output_reg->src.n_bits);
849 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
850 naor->max_len = htons(output_reg->max_len);
854 static char * OVS_WARN_UNUSED_RESULT
855 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
856 enum ofputil_protocol *usable_protocols OVS_UNUSED)
858 return parse_OUTPUT(arg, ofpacts, usable_protocols);
862 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
864 ds_put_cstr(s, "output:");
865 mf_format_subfield(&a->src, s);
868 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
870 * The bundle actions choose a slave from a supplied list of options.
871 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
872 * selection to a register.
874 * The list of possible slaves follows the nx_action_bundle structure. The size
875 * of each slave is governed by its type as indicated by the 'slave_type'
876 * parameter. The list of slaves should be padded at its end with zeros to make
877 * the total length of the action a multiple of 8.
879 * Switches infer from the 'slave_type' parameter the size of each slave. All
880 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
881 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
882 * 2 byte width. Switches should reject actions which indicate unknown or
883 * unsupported slave types.
885 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
886 * slave. If the switch does not support the specified 'algorithm' parameter,
887 * it should reject the action.
889 * Several algorithms take into account liveness when selecting slaves. The
890 * liveness of a slave is implementation defined (with one exception), but will
891 * generally take into account things like its carrier status and the results
892 * of any link monitoring protocols which happen to be running on it. In order
893 * to give controllers a place-holder value, the OFPP_NONE port is always
896 * Some slave selection strategies require the use of a hash function, in which
897 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
898 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
899 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
900 * 'basis' parameter is used as a universal hash parameter. Different values
901 * of 'basis' yield different hash results.
903 * The 'zero' parameter at the end of the action structure is reserved for
904 * future use. Switches are required to reject actions which have nonzero
905 * bytes in the 'zero' field.
907 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
908 * should reject actions which have nonzero bytes in either of these fields.
910 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
911 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
912 * similar to those for the NXAST_REG_LOAD action. */
913 struct nx_action_bundle {
914 ovs_be16 type; /* OFPAT_VENDOR. */
915 ovs_be16 len; /* Length including slaves. */
916 ovs_be32 vendor; /* NX_VENDOR_ID. */
917 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
919 /* Slave choice algorithm to apply to hash value. */
920 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
922 /* What fields to hash and how. */
923 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
924 ovs_be16 basis; /* Universal hash parameter. */
926 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
927 ovs_be16 n_slaves; /* Number of slaves. */
929 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
930 ovs_be32 dst; /* Destination. */
932 uint8_t zero[4]; /* Reserved. Must be zero. */
934 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
937 decode_bundle(bool load, const struct nx_action_bundle *nab,
938 struct ofpbuf *ofpacts)
940 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
941 struct ofpact_bundle *bundle;
943 size_t slaves_size, i;
946 bundle = ofpact_put_BUNDLE(ofpacts);
948 bundle->n_slaves = ntohs(nab->n_slaves);
949 bundle->basis = ntohs(nab->basis);
950 bundle->fields = ntohs(nab->fields);
951 bundle->algorithm = ntohs(nab->algorithm);
952 slave_type = ntohl(nab->slave_type);
953 slaves_size = ntohs(nab->len) - sizeof *nab;
955 error = OFPERR_OFPBAC_BAD_ARGUMENT;
956 if (!flow_hash_fields_valid(bundle->fields)) {
957 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
958 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
959 VLOG_WARN_RL(&rl, "too many slaves");
960 } else if (bundle->algorithm != NX_BD_ALG_HRW
961 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
962 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
963 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
964 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
969 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
970 VLOG_WARN_RL(&rl, "reserved field is nonzero");
971 error = OFPERR_OFPBAC_BAD_ARGUMENT;
975 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
976 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
977 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
979 if (bundle->dst.n_bits < 16) {
980 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
982 error = OFPERR_OFPBAC_BAD_ARGUMENT;
985 if (nab->ofs_nbits || nab->dst) {
986 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
987 error = OFPERR_OFPBAC_BAD_ARGUMENT;
991 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
992 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
993 "allocated for slaves. %"PRIuSIZE" bytes are required "
994 "for %"PRIu16" slaves.",
995 load ? "bundle_load" : "bundle", slaves_size,
996 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
997 error = OFPERR_OFPBAC_BAD_LEN;
1000 for (i = 0; i < bundle->n_slaves; i++) {
1001 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1002 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1005 bundle = ofpacts->header;
1006 ofpact_update_len(ofpacts, &bundle->ofpact);
1009 error = bundle_check(bundle, OFPP_MAX, NULL);
1015 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab, struct ofpbuf *out)
1017 return decode_bundle(false, nab, out);
1021 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1024 return decode_bundle(true, nab, out);
1028 encode_BUNDLE(const struct ofpact_bundle *bundle,
1029 enum ofp_version ofp_version OVS_UNUSED,
1032 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1033 struct nx_action_bundle *nab;
1037 nab = (bundle->dst.field
1038 ? put_NXAST_BUNDLE_LOAD(out)
1039 : put_NXAST_BUNDLE(out));
1040 nab->len = htons(ntohs(nab->len) + slaves_len);
1041 nab->algorithm = htons(bundle->algorithm);
1042 nab->fields = htons(bundle->fields);
1043 nab->basis = htons(bundle->basis);
1044 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1045 nab->n_slaves = htons(bundle->n_slaves);
1046 if (bundle->dst.field) {
1047 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1048 bundle->dst.n_bits);
1049 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1052 slaves = ofpbuf_put_zeros(out, slaves_len);
1053 for (i = 0; i < bundle->n_slaves; i++) {
1054 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1058 static char * OVS_WARN_UNUSED_RESULT
1059 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1060 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1062 return bundle_parse(arg, ofpacts);
1065 static char * OVS_WARN_UNUSED_RESULT
1066 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1068 return bundle_parse_load(arg, ofpacts);
1072 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1074 bundle_format(a, s);
1077 /* Set VLAN actions. */
1080 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1083 return OFPERR_OFPBAC_BAD_ARGUMENT;
1085 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1086 vlan_vid->vlan_vid = vid;
1087 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1093 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid, struct ofpbuf *out)
1095 return decode_set_vlan_vid(vid, true, out);
1099 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid, struct ofpbuf *out)
1101 return decode_set_vlan_vid(vid, false, out);
1105 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1106 enum ofp_version ofp_version, struct ofpbuf *out)
1108 uint16_t vid = vlan_vid->vlan_vid;
1110 /* Push a VLAN tag, if none is present and this form of the action calls
1111 * for such a feature. */
1112 if (ofp_version > OFP10_VERSION
1113 && vlan_vid->push_vlan_if_needed
1114 && !vlan_vid->flow_has_vlan) {
1115 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1118 if (ofp_version == OFP10_VERSION) {
1119 put_OFPAT10_SET_VLAN_VID(out, vid);
1120 } else if (ofp_version == OFP11_VERSION) {
1121 put_OFPAT11_SET_VLAN_VID(out, vid);
1123 ofpact_put_set_field(out, ofp_version,
1124 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1128 static char * OVS_WARN_UNUSED_RESULT
1129 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1131 struct ofpact_vlan_vid *vlan_vid;
1135 error = str_to_u16(arg, "VLAN VID", &vid);
1140 if (vid & ~VLAN_VID_MASK) {
1141 return xasprintf("%s: not a valid VLAN VID", arg);
1143 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1144 vlan_vid->vlan_vid = vid;
1145 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1149 static char * OVS_WARN_UNUSED_RESULT
1150 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1151 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1153 return parse_set_vlan_vid(arg, ofpacts, false);
1157 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1159 ds_put_format(s, "%s:%"PRIu16,
1160 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1164 /* Set PCP actions. */
1167 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1170 return OFPERR_OFPBAC_BAD_ARGUMENT;
1172 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1173 vlan_pcp->vlan_pcp = pcp;
1174 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1180 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp, struct ofpbuf *out)
1182 return decode_set_vlan_pcp(pcp, true, out);
1186 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp, struct ofpbuf *out)
1188 return decode_set_vlan_pcp(pcp, false, out);
1192 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1193 enum ofp_version ofp_version, struct ofpbuf *out)
1195 uint8_t pcp = vlan_pcp->vlan_pcp;
1197 /* Push a VLAN tag, if none is present and this form of the action calls
1198 * for such a feature. */
1199 if (ofp_version > OFP10_VERSION
1200 && vlan_pcp->push_vlan_if_needed
1201 && !vlan_pcp->flow_has_vlan) {
1202 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1205 if (ofp_version == OFP10_VERSION) {
1206 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1207 } else if (ofp_version == OFP11_VERSION) {
1208 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1210 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1214 static char * OVS_WARN_UNUSED_RESULT
1215 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1217 struct ofpact_vlan_pcp *vlan_pcp;
1221 error = str_to_u8(arg, "VLAN PCP", &pcp);
1227 return xasprintf("%s: not a valid VLAN PCP", arg);
1229 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1230 vlan_pcp->vlan_pcp = pcp;
1231 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1235 static char * OVS_WARN_UNUSED_RESULT
1236 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1237 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1239 return parse_set_vlan_pcp(arg, ofpacts, false);
1243 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1245 ds_put_format(s, "%s:%"PRIu8,
1246 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1250 /* Strip VLAN actions. */
1253 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1255 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1260 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1262 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1267 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1268 enum ofp_version ofp_version, struct ofpbuf *out)
1270 if (ofp_version == OFP10_VERSION) {
1271 put_OFPAT10_STRIP_VLAN(out);
1273 put_OFPAT11_POP_VLAN(out);
1277 static char * OVS_WARN_UNUSED_RESULT
1278 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1279 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1281 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1285 static char * OVS_WARN_UNUSED_RESULT
1286 parse_pop_vlan(struct ofpbuf *ofpacts)
1288 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1293 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1295 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1300 /* Push VLAN action. */
1303 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type, struct ofpbuf *out)
1305 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1306 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1307 return OFPERR_OFPBAC_BAD_ARGUMENT;
1309 ofpact_put_PUSH_VLAN(out);
1314 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1315 enum ofp_version ofp_version, struct ofpbuf *out)
1317 if (ofp_version == OFP10_VERSION) {
1318 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1319 * follow this action. */
1321 /* XXX ETH_TYPE_VLAN_8021AD case */
1322 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1326 static char * OVS_WARN_UNUSED_RESULT
1327 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1328 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1333 *usable_protocols &= OFPUTIL_P_OF11_UP;
1334 error = str_to_u16(arg, "ethertype", ðertype);
1339 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1340 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1341 return xasprintf("%s: not a valid VLAN ethertype", arg);
1344 ofpact_put_PUSH_VLAN(ofpacts);
1349 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1351 /* XXX 802.1AD case*/
1352 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1355 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1356 struct ofp_action_dl_addr {
1357 ovs_be16 type; /* Type. */
1358 ovs_be16 len; /* Length is 16. */
1359 struct eth_addr dl_addr; /* Ethernet address. */
1362 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1365 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1368 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1373 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1376 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1381 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1382 enum ofp_raw_action_type raw, enum mf_field_id field,
1385 if (ofp_version < OFP12_VERSION) {
1386 struct ofp_action_dl_addr *oada;
1388 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1389 oada->dl_addr = mac->mac;
1391 ofpact_put_set_field(out, ofp_version, field,
1392 eth_addr_to_uint64(mac->mac));
1397 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1400 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1406 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1407 enum ofp_version ofp_version,
1410 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1415 static char * OVS_WARN_UNUSED_RESULT
1416 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1417 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1419 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1422 static char * OVS_WARN_UNUSED_RESULT
1423 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1424 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1426 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1430 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1432 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1436 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1438 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1441 /* Set IPv4 address actions. */
1444 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4, struct ofpbuf *out)
1446 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1451 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4, struct ofpbuf *out)
1453 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1458 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1459 enum ofp_version ofp_version,
1460 enum ofp_raw_action_type raw, enum mf_field_id field,
1463 ovs_be32 addr = ipv4->ipv4;
1464 if (ofp_version < OFP12_VERSION) {
1465 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1467 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1472 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1473 enum ofp_version ofp_version, struct ofpbuf *out)
1475 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1480 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1481 enum ofp_version ofp_version, struct ofpbuf *out)
1483 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1487 static char * OVS_WARN_UNUSED_RESULT
1488 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1489 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1491 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1494 static char * OVS_WARN_UNUSED_RESULT
1495 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1496 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1498 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1502 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1504 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1508 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1510 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1513 /* Set IPv4/v6 TOS actions. */
1516 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp, struct ofpbuf *out)
1518 if (dscp & ~IP_DSCP_MASK) {
1519 return OFPERR_OFPBAC_BAD_ARGUMENT;
1521 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1527 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1528 enum ofp_version ofp_version, struct ofpbuf *out)
1530 if (ofp_version < OFP12_VERSION) {
1531 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1533 ofpact_put_set_field(out, ofp_version,
1534 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1538 static char * OVS_WARN_UNUSED_RESULT
1539 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1540 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1545 error = str_to_u8(arg, "TOS", &tos);
1550 if (tos & ~IP_DSCP_MASK) {
1551 return xasprintf("%s: not a valid TOS", arg);
1553 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1558 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1560 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1563 /* Set IPv4/v6 ECN actions. */
1566 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn, struct ofpbuf *out)
1568 if (ecn & ~IP_ECN_MASK) {
1569 return OFPERR_OFPBAC_BAD_ARGUMENT;
1571 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1577 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1578 enum ofp_version ofp_version, struct ofpbuf *out)
1580 uint8_t ecn = ip_ecn->ecn;
1581 if (ofp_version == OFP10_VERSION) {
1583 } else if (ofp_version == OFP11_VERSION) {
1584 put_OFPAT11_SET_NW_ECN(out, ecn);
1586 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1590 static char * OVS_WARN_UNUSED_RESULT
1591 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1592 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1597 error = str_to_u8(arg, "ECN", &ecn);
1602 if (ecn & ~IP_ECN_MASK) {
1603 return xasprintf("%s: not a valid ECN", arg);
1605 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1610 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1612 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1615 /* Set IPv4/v6 TTL actions. */
1618 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl, struct ofpbuf *out)
1620 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1625 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1626 enum ofp_version ofp_version, struct ofpbuf *out)
1628 if (ofp_version >= OFP11_VERSION) {
1629 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1635 static char * OVS_WARN_UNUSED_RESULT
1636 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1637 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1642 error = str_to_u8(arg, "TTL", &ttl);
1647 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1652 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1654 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1657 /* Set TCP/UDP/SCTP port actions. */
1660 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port, struct ofpbuf *out)
1662 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1667 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port, struct ofpbuf *out)
1669 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1674 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1675 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1676 enum mf_field_id field, struct ofpbuf *out)
1678 uint16_t port = l4_port->port;
1680 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1681 ofpact_put_set_field(out, ofp_version, field, port);
1683 ofpact_put_raw(out, ofp_version, raw, port);
1688 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1689 enum ofp_version ofp_version, struct ofpbuf *out)
1691 uint8_t proto = l4_port->flow_ip_proto;
1692 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1693 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1694 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1697 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1701 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1702 enum ofp_version ofp_version,
1705 uint8_t proto = l4_port->flow_ip_proto;
1706 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1707 : proto == IPPROTO_UDP ? MFF_UDP_DST
1708 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1711 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1714 static char * OVS_WARN_UNUSED_RESULT
1715 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1716 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1718 return str_to_u16(arg, "source port",
1719 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1722 static char * OVS_WARN_UNUSED_RESULT
1723 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1724 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1726 return str_to_u16(arg, "destination port",
1727 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1731 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1733 ds_put_format(s, "mod_tp_src:%d", a->port);
1737 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1739 ds_put_format(s, "mod_tp_dst:%d", a->port);
1742 /* Action structure for OFPAT_COPY_FIELD. */
1743 struct ofp15_action_copy_field {
1744 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1745 ovs_be16 len; /* Length is padded to 64 bits. */
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. */
1751 * - OXM header for source field.
1752 * - OXM header for destination field.
1753 * - Padding with 0-bytes to a multiple of 8 bytes.
1754 * The "pad2" member is the beginning of the above. */
1757 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1759 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1760 struct onf_action_copy_field {
1761 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
1762 ovs_be16 len; /* Length is padded to 64 bits. */
1763 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
1764 ovs_be16 exp_type; /* 3200. */
1765 uint8_t pad[2]; /* Not used. */
1766 ovs_be16 n_bits; /* Number of bits to copy. */
1767 ovs_be16 src_offset; /* Starting bit offset in source. */
1768 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1769 uint8_t pad2[2]; /* Not used. */
1771 * - OXM header for source field.
1772 * - OXM header for destination field.
1773 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1774 * The "pad3" member is the beginning of the above. */
1775 uint8_t pad3[4]; /* Not used. */
1777 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1779 /* Action structure for NXAST_REG_MOVE.
1781 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1782 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1783 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1784 * the next most significant bit, and so on.
1786 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1787 * sense to use nxm_hasmask=1 because the action does not do any kind of
1788 * matching; it uses the actual value of a field.)
1790 * The following nxm_header values are potentially acceptable as 'src':
1805 * - NXM_OF_ICMP_TYPE
1806 * - NXM_OF_ICMP_CODE
1813 * - NXM_NX_ICMPV6_TYPE
1814 * - NXM_NX_ICMPV6_CODE
1817 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1819 * - NXM_NX_TUN_IPV4_SRC
1820 * - NXM_NX_TUN_IPV4_DST
1822 * The following nxm_header values are potentially acceptable as 'dst':
1838 * Modifying any of the above fields changes the corresponding packet
1843 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1847 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1848 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1849 * header (if any), ignoring the other bits. Setting a value with CFI=1
1850 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1851 * to the field's new value (with the CFI bit masked out).
1853 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1854 * any of these values modifies the corresponding tunnel header field used
1855 * for the packet's next tunnel encapsulation, if allowed by the
1856 * configuration of the output tunnel port.
1858 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1859 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1860 * used only if the flow's nx_match includes an nxm_entry that specifies
1861 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1863 * The switch will reject actions for which src_ofs+n_bits is greater than the
1864 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1865 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1867 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1868 * behaves as if 'src' were copied out to a temporary buffer, then the
1869 * temporary buffer copied to 'dst'.
1871 struct nx_action_reg_move {
1872 ovs_be16 type; /* OFPAT_VENDOR. */
1873 ovs_be16 len; /* Length is 24. */
1874 ovs_be32 vendor; /* NX_VENDOR_ID. */
1875 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1876 ovs_be16 n_bits; /* Number of bits. */
1877 ovs_be16 src_ofs; /* Starting bit offset in source. */
1878 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1880 * - OXM/NXM header for source field (4 or 8 bytes).
1881 * - OXM/NXM header for destination field (4 or 8 bytes).
1882 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
1884 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
1887 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
1888 const void *action, ovs_be16 action_len, size_t oxm_offset,
1889 struct ofpbuf *ofpacts)
1891 struct ofpact_reg_move *move;
1895 move = ofpact_put_REG_MOVE(ofpacts);
1896 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
1897 move->src.ofs = ntohs(src_offset);
1898 move->src.n_bits = ntohs(n_bits);
1899 move->dst.ofs = ntohs(dst_offset);
1900 move->dst.n_bits = ntohs(n_bits);
1902 ofpbuf_use_const(&b, action, ntohs(action_len));
1903 ofpbuf_pull(&b, oxm_offset);
1904 error = nx_pull_header(&b, &move->src.field, NULL);
1908 error = nx_pull_header(&b, &move->dst.field, NULL);
1913 if (!is_all_zeros(b.data, b.size)) {
1914 return OFPERR_NXBRC_MUST_BE_ZERO;
1917 return nxm_reg_move_check(move, NULL);
1921 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
1922 struct ofpbuf *ofpacts)
1924 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1925 oacf->n_bits, oacf, oacf->len,
1926 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
1930 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
1931 struct ofpbuf *ofpacts)
1933 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1934 oacf->n_bits, oacf, oacf->len,
1935 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
1939 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
1940 struct ofpbuf *ofpacts)
1942 struct ofpact_reg_move *move;
1946 move = ofpact_put_REG_MOVE(ofpacts);
1947 move->ofpact.raw = NXAST_RAW_REG_MOVE;
1948 move->src.ofs = ntohs(narm->src_ofs);
1949 move->src.n_bits = ntohs(narm->n_bits);
1950 move->dst.ofs = ntohs(narm->dst_ofs);
1951 move->dst.n_bits = ntohs(narm->n_bits);
1953 ofpbuf_use_const(&b, narm, ntohs(narm->len));
1954 ofpbuf_pull(&b, sizeof *narm);
1955 error = nx_pull_header(&b, &move->src.field, NULL);
1959 error = nx_pull_header(&b, &move->dst.field, NULL);
1963 if (!is_all_zeros(b.data, b.size)) {
1964 return OFPERR_NXBRC_MUST_BE_ZERO;
1967 return nxm_reg_move_check(move, NULL);
1971 encode_REG_MOVE(const struct ofpact_reg_move *move,
1972 enum ofp_version ofp_version, struct ofpbuf *out)
1974 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
1975 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
1976 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
1977 * probe for support. Until we have that ability, we currently prefer
1978 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
1980 size_t start_ofs = out->size;
1981 if (ofp_version >= OFP15_VERSION) {
1982 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
1983 copy->n_bits = htons(move->dst.n_bits);
1984 copy->src_offset = htons(move->src.ofs);
1985 copy->dst_offset = htons(move->dst.ofs);
1986 out->size = out->size - sizeof copy->pad2;
1987 nx_put_header(out, move->src.field->id, ofp_version, false);
1988 nx_put_header(out, move->dst.field->id, ofp_version, false);
1989 } else if (ofp_version == OFP13_VERSION
1990 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
1991 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
1992 copy->n_bits = htons(move->dst.n_bits);
1993 copy->src_offset = htons(move->src.ofs);
1994 copy->dst_offset = htons(move->dst.ofs);
1995 out->size = out->size - sizeof copy->pad3;
1996 nx_put_header(out, move->src.field->id, ofp_version, false);
1997 nx_put_header(out, move->dst.field->id, ofp_version, false);
1999 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2000 narm->n_bits = htons(move->dst.n_bits);
2001 narm->src_ofs = htons(move->src.ofs);
2002 narm->dst_ofs = htons(move->dst.ofs);
2003 nx_put_header(out, move->src.field->id, 0, false);
2004 nx_put_header(out, move->dst.field->id, 0, false);
2006 pad_ofpat(out, start_ofs);
2009 static char * OVS_WARN_UNUSED_RESULT
2010 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2011 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2013 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2014 const char *full_arg = arg;
2017 error = mf_parse_subfield__(&move->src, &arg);
2021 if (strncmp(arg, "->", 2)) {
2022 return xasprintf("%s: missing `->' following source", full_arg);
2025 error = mf_parse_subfield(&move->dst, arg);
2030 if (move->src.n_bits != move->dst.n_bits) {
2031 return xasprintf("%s: source field is %d bits wide but destination is "
2032 "%d bits wide", full_arg,
2033 move->src.n_bits, move->dst.n_bits);
2039 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2041 nxm_format_reg_move(a, s);
2044 /* Action structure for OFPAT12_SET_FIELD. */
2045 struct ofp12_action_set_field {
2046 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2047 ovs_be16 len; /* Length is padded to 64 bits. */
2050 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2051 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2053 * The "pad" member is the beginning of the above. */
2056 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2058 /* Action structure for NXAST_REG_LOAD.
2060 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2061 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2062 * starts at 0 for the least-significant bit, 1 for the next most significant
2065 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2066 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2069 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2070 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2071 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2072 * also stored as one less than its true value:
2075 * +------------------------------+------------------+
2076 * | ofs | n_bits - 1 |
2077 * +------------------------------+------------------+
2079 * The switch will reject actions for which ofs+n_bits is greater than the
2080 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2081 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2082 * OFPBAC_BAD_ARGUMENT.
2084 struct nx_action_reg_load {
2085 ovs_be16 type; /* OFPAT_VENDOR. */
2086 ovs_be16 len; /* Length is 24. */
2087 ovs_be32 vendor; /* NX_VENDOR_ID. */
2088 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2089 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2090 ovs_be32 dst; /* Destination register. */
2091 ovs_be64 value; /* Immediate value. */
2093 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2095 /* Action structure for NXAST_REG_LOAD2.
2097 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2098 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2099 * OXM experimenter fields. */
2100 struct nx_action_reg_load2 {
2101 ovs_be16 type; /* OFPAT_VENDOR. */
2102 ovs_be16 len; /* At least 16. */
2103 ovs_be32 vendor; /* NX_VENDOR_ID. */
2104 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2107 * - An NXM/OXM header, value, and optionally a mask.
2108 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2110 * The "pad" member is the beginning of the above. */
2113 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2116 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2117 bool may_mask, struct ofpbuf *ofpacts)
2119 struct ofpact_set_field *sf;
2123 sf = ofpact_put_SET_FIELD(ofpacts);
2125 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2126 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2127 error = nx_pull_entry(&b, &sf->field, &sf->value,
2128 may_mask ? &sf->mask : NULL);
2130 return (error == OFPERR_OFPBMC_BAD_MASK
2131 ? OFPERR_OFPBAC_BAD_SET_MASK
2135 memset(&sf->mask, 0xff, sf->field->n_bytes);
2138 if (!is_all_zeros(b.data, b.size)) {
2139 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2142 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2144 if (sf->field->id == MFF_IN_PORT_OXM) {
2145 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2148 /* oxm_length is now validated to be compatible with mf_value. */
2149 if (!sf->field->writable) {
2150 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2152 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2155 /* The value must be valid for match. OpenFlow 1.5 also says,
2156 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2157 * a 1-bit in oxm_value and in oxm_mask." */
2158 if (!mf_is_value_valid(sf->field, &sf->value)
2159 || (sf->field->id == MFF_VLAN_VID
2160 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2161 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2162 struct ds ds = DS_EMPTY_INITIALIZER;
2163 mf_format(sf->field, &sf->value, NULL, &ds);
2164 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2165 sf->field->name, ds_cstr(&ds));
2168 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2174 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2175 struct ofpbuf *ofpacts)
2177 return decode_ofpat_set_field(oasf, false, ofpacts);
2181 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2182 struct ofpbuf *ofpacts)
2184 return decode_ofpat_set_field(oasf, true, ofpacts);
2188 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2191 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2192 struct mf_subfield dst;
2195 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2197 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2198 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2199 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2200 error = mf_check_dst(&dst, NULL);
2205 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2207 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2208 return OFPERR_OFPBAC_BAD_ARGUMENT;
2211 sf->field = dst.field;
2212 bitwise_put(ntohll(narl->value),
2213 &sf->value, dst.field->n_bytes, dst.ofs,
2215 bitwise_put(UINT64_MAX,
2216 &sf->mask, dst.field->n_bytes, dst.ofs,
2223 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2226 struct ofpact_set_field *sf;
2230 sf = ofpact_put_SET_FIELD(out);
2231 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2233 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2234 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2235 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2239 if (!is_all_zeros(b.data, b.size)) {
2240 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2243 if (!sf->field->writable) {
2244 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2246 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2252 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2253 enum mf_field_id field, uint64_t value_)
2255 struct ofp12_action_set_field *oasf OVS_UNUSED;
2256 int n_bytes = mf_from_id(field)->n_bytes;
2257 size_t start_ofs = openflow->size;
2258 union mf_value value;
2260 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2262 oasf = put_OFPAT12_SET_FIELD(openflow);
2263 openflow->size = openflow->size - sizeof oasf->pad;
2264 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2265 pad_ofpat(openflow, start_ofs);
2269 next_load_segment(const struct ofpact_set_field *sf,
2270 struct mf_subfield *dst, uint64_t *value)
2272 int n_bits = sf->field->n_bits;
2273 int n_bytes = sf->field->n_bytes;
2274 int start = dst->ofs + dst->n_bits;
2276 if (start < n_bits) {
2277 dst->field = sf->field;
2278 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2279 if (dst->ofs < n_bits) {
2280 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2281 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2282 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2289 /* Convert 'sf' to a series of REG_LOADs. */
2291 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2293 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2294 * experimenter OXM or is variable length (or if it came in as
2295 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2296 * NXAST_REG_LOAD, which is backward compatible. */
2297 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2298 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2299 struct nx_action_reg_load2 *narl OVS_UNUSED;
2300 size_t start_ofs = openflow->size;
2302 narl = put_NXAST_REG_LOAD2(openflow);
2303 openflow->size = openflow->size - sizeof narl->pad;
2304 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2305 pad_ofpat(openflow, start_ofs);
2307 struct mf_subfield dst;
2310 dst.ofs = dst.n_bits = 0;
2311 while (next_load_segment(sf, &dst, &value)) {
2312 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2313 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2314 narl->dst = htonl(mf_nxm_header(dst.field->id));
2315 narl->value = htonll(value);
2320 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2321 * actions, if we can, falling back to Nicira extensions if we must.
2323 * We check only meta-flow types that can appear within set field actions and
2324 * that have a mapping to compatible action types. These struct mf_field
2325 * definitions have a defined OXM or NXM header value and specify the field as
2328 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2329 enum ofp_version ofp_version,
2332 switch ((int) sf->field->id) {
2333 case MFF_VLAN_TCI: {
2334 ovs_be16 tci = sf->value.be16;
2335 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2336 uint16_t vid = vlan_tci_to_vid(tci);
2337 uint8_t pcp = vlan_tci_to_pcp(tci);
2339 if (ofp_version < OFP11_VERSION) {
2340 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2342 * If CFI=1, Add or modify VLAN VID & PCP.
2343 * If CFI=0, strip VLAN header, if any.
2346 put_OFPAT10_SET_VLAN_VID(out, vid);
2347 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2349 put_OFPAT10_STRIP_VLAN(out);
2352 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2354 * If CFI=1, Add or modify VLAN VID & PCP.
2355 * OpenFlow 1.1 set actions only apply if the packet
2356 * already has VLAN tags. To be sure that is the case
2357 * we have to push a VLAN header. As we do not support
2358 * multiple layers of VLANs, this is a no-op, if a VLAN
2359 * header already exists. This may backfire, however,
2360 * when we start supporting multiple layers of VLANs.
2361 * If CFI=0, strip VLAN header, if any.
2364 /* Push a VLAN tag, if one was not seen at action validation
2366 if (!sf->flow_has_vlan) {
2367 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2369 put_OFPAT11_SET_VLAN_VID(out, vid);
2370 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2372 /* If the flow did not match on vlan, we have no way of
2373 * knowing if the vlan tag exists, so we must POP just to be
2375 put_OFPAT11_POP_VLAN(out);
2381 case MFF_VLAN_VID: {
2382 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2383 if (ofp_version == OFP10_VERSION) {
2384 put_OFPAT10_SET_VLAN_VID(out, vid);
2386 put_OFPAT11_SET_VLAN_VID(out, vid);
2392 if (ofp_version == OFP10_VERSION) {
2393 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2395 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2400 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2404 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2408 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2412 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2416 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2419 case MFF_IP_DSCP_SHIFTED:
2420 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2425 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2430 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2434 set_field_to_nxast(sf, out);
2440 set_field_to_set_field(const struct ofpact_set_field *sf,
2441 enum ofp_version ofp_version, struct ofpbuf *out)
2443 struct ofp12_action_set_field *oasf OVS_UNUSED;
2444 size_t start_ofs = out->size;
2446 oasf = put_OFPAT12_SET_FIELD(out);
2447 out->size = out->size - sizeof oasf->pad;
2448 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2449 pad_ofpat(out, start_ofs);
2453 encode_SET_FIELD(const struct ofpact_set_field *sf,
2454 enum ofp_version ofp_version, struct ofpbuf *out)
2456 if (ofp_version >= OFP15_VERSION) {
2457 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2459 set_field_to_set_field(sf, ofp_version, out);
2460 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2461 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2462 /* It came in as reg_load, send it out the same way. */
2463 set_field_to_nxast(sf, out);
2464 } else if (ofp_version < OFP12_VERSION) {
2465 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2466 set_field_to_legacy_openflow(sf, ofp_version, out);
2467 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2468 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2469 * entire field, so encode it as OFPAT_SET_FIELD. */
2470 set_field_to_set_field(sf, ofp_version, out);
2472 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2473 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2474 * so encode it as reg_load. */
2475 set_field_to_nxast(sf, out);
2479 /* Parses the input argument 'arg' into the key, value, and delimiter
2480 * components that are common across the reg_load and set_field action format.
2482 * With an argument like "1->metadata", sets the following pointers to
2483 * point within 'arg':
2488 * Returns NULL if successful, otherwise a malloc()'d string describing the
2489 * error. The caller is responsible for freeing the returned string. */
2490 static char * OVS_WARN_UNUSED_RESULT
2491 set_field_split_str(char *arg, char **key, char **value, char **delim)
2496 value_end = strstr(arg, "->");
2497 *key = value_end + strlen("->");
2503 return xasprintf("%s: missing `->'", arg);
2505 if (strlen(value_end) <= strlen("->")) {
2506 return xasprintf("%s: missing field name following `->'", arg);
2512 /* Parses a "set_field" action with argument 'arg', appending the parsed
2513 * action to 'ofpacts'.
2515 * Returns NULL if successful, otherwise a malloc()'d string describing the
2516 * error. The caller is responsible for freeing the returned string. */
2517 static char * OVS_WARN_UNUSED_RESULT
2518 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2519 enum ofputil_protocol *usable_protocols)
2521 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2525 const struct mf_field *mf;
2528 error = set_field_split_str(arg, &key, &value, &delim);
2533 mf = mf_from_name(key);
2535 return xasprintf("%s is not a valid OXM field name", key);
2537 if (!mf->writable) {
2538 return xasprintf("%s is read-only", key);
2542 error = mf_parse(mf, value, &sf->value, &sf->mask);
2547 if (!mf_is_value_valid(mf, &sf->value)) {
2548 return xasprintf("%s is not a valid value for field %s", value, key);
2551 *usable_protocols &= mf->usable_protocols_exact;
2555 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2556 * action to 'ofpacts'.
2558 * Returns NULL if successful, otherwise a malloc()'d string describing the
2559 * error. The caller is responsible for freeing the returned string. */
2560 static char * OVS_WARN_UNUSED_RESULT
2561 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2562 enum ofputil_protocol *usable_protocols)
2564 char *copy = xstrdup(arg);
2565 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2570 static char * OVS_WARN_UNUSED_RESULT
2571 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2573 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2574 struct mf_subfield dst;
2575 char *key, *value_str;
2576 union mf_value value;
2579 error = set_field_split_str(arg, &key, &value_str, NULL);
2584 error = mf_parse_subfield(&dst, key);
2589 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2591 return xasprintf("%s: cannot parse integer value", arg);
2594 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2595 dst.field->n_bytes * 8 - dst.n_bits)) {
2599 mf_format(dst.field, &value, NULL, &ds);
2600 error = xasprintf("%s: value %s does not fit into %d bits",
2601 arg, ds_cstr(&ds), dst.n_bits);
2606 sf->field = dst.field;
2607 memset(&sf->value, 0, sizeof sf->value);
2608 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2609 dst.field->n_bytes, dst.ofs, dst.n_bits);
2610 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2616 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2618 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2619 struct mf_subfield dst;
2622 dst.ofs = dst.n_bits = 0;
2623 while (next_load_segment(a, &dst, &value)) {
2624 ds_put_format(s, "load:%#"PRIx64"->", value);
2625 mf_format_subfield(&dst, s);
2626 ds_put_char(s, ',');
2630 ds_put_cstr(s, "set_field:");
2631 mf_format(a->field, &a->value, &a->mask, s);
2632 ds_put_format(s, "->%s", a->field->name);
2636 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2637 * is marked such that, if possible, it will be translated to OpenFlow as
2638 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2639 * that was the way that the action was expressed when it came into OVS or for
2640 * backward compatibility. */
2641 struct ofpact_set_field *
2642 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2644 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2645 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2649 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2651 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2654 struct nx_action_stack {
2655 ovs_be16 type; /* OFPAT_VENDOR. */
2656 ovs_be16 len; /* Length is 16. */
2657 ovs_be32 vendor; /* NX_VENDOR_ID. */
2658 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2659 ovs_be16 offset; /* Bit offset into the field. */
2661 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2662 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2663 * - Enough 0-bytes to pad out the action to 24 bytes. */
2664 uint8_t pad[12]; /* See above. */
2666 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2669 decode_stack_action(const struct nx_action_stack *nasp,
2670 struct ofpact_stack *stack_action)
2675 stack_action->subfield.ofs = ntohs(nasp->offset);
2677 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2678 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2679 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2683 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2685 if (!is_all_zeros(b.data, b.size)) {
2686 return OFPERR_NXBRC_MUST_BE_ZERO;
2693 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2694 struct ofpbuf *ofpacts)
2696 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2697 enum ofperr error = decode_stack_action(nasp, push);
2698 return error ? error : nxm_stack_push_check(push, NULL);
2702 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2703 struct ofpbuf *ofpacts)
2705 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2706 enum ofperr error = decode_stack_action(nasp, pop);
2707 return error ? error : nxm_stack_pop_check(pop, NULL);
2711 encode_STACK_op(const struct ofpact_stack *stack_action,
2712 struct nx_action_stack *nasp)
2717 nasp->offset = htons(stack_action->subfield.ofs);
2719 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2720 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2721 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2722 n_bits = htons(stack_action->subfield.n_bits);
2723 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2727 encode_STACK_PUSH(const struct ofpact_stack *stack,
2728 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2730 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2734 encode_STACK_POP(const struct ofpact_stack *stack,
2735 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2737 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2740 static char * OVS_WARN_UNUSED_RESULT
2741 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2742 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2744 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2747 static char * OVS_WARN_UNUSED_RESULT
2748 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2749 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2751 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2755 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2757 nxm_format_stack_push(a, s);
2761 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2763 nxm_format_stack_pop(a, s);
2766 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2768 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2769 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2770 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2771 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2773 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2774 * sent only to controllers with id 0.)
2776 struct nx_action_cnt_ids {
2777 ovs_be16 type; /* OFPAT_VENDOR. */
2778 ovs_be16 len; /* Length including slaves. */
2779 ovs_be32 vendor; /* NX_VENDOR_ID. */
2780 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2782 ovs_be16 n_controllers; /* Number of controllers. */
2783 uint8_t zeros[4]; /* Must be zero. */
2785 /* Followed by 1 or more controller ids.
2787 * uint16_t cnt_ids[]; // Controller ids.
2788 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2791 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2794 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2797 struct ofpact_cnt_ids *ids;
2798 enum ofperr error = 0;
2800 ids = ofpact_put_DEC_TTL(out);
2801 ids->n_controllers = 1;
2802 ofpbuf_put(out, &id, sizeof id);
2804 ofpact_update_len(out, &ids->ofpact);
2809 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2812 struct ofpact_cnt_ids *ids;
2816 ids = ofpact_put_DEC_TTL(out);
2817 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2818 ids->n_controllers = ntohs(nac_ids->n_controllers);
2819 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2821 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2822 return OFPERR_NXBRC_MUST_BE_ZERO;
2825 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2826 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2827 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2828 "are required for %"PRIu16" controllers.",
2829 ids_size, ids->n_controllers * sizeof(ovs_be16),
2830 ids->n_controllers);
2831 return OFPERR_OFPBAC_BAD_LEN;
2834 for (i = 0; i < ids->n_controllers; i++) {
2835 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2836 ofpbuf_put(out, &id, sizeof id);
2840 ofpact_update_len(out, &ids->ofpact);
2846 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2847 enum ofp_version ofp_version, struct ofpbuf *out)
2849 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2850 || dec_ttl->n_controllers != 1
2851 || dec_ttl->cnt_ids[0] != 0) {
2852 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2853 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2857 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2858 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2860 ids = ofpbuf_put_zeros(out, ids_len);
2861 for (i = 0; i < dec_ttl->n_controllers; i++) {
2862 ids[i] = htons(dec_ttl->cnt_ids[i]);
2865 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2870 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2872 struct ofpact_cnt_ids *ids;
2875 ofpact_put_DEC_TTL(ofpacts);
2876 ofpbuf_put(ofpacts, &id, sizeof id);
2877 ids = ofpacts->header;
2878 ids->n_controllers++;
2879 ofpact_update_len(ofpacts, &ids->ofpact);
2882 static char * OVS_WARN_UNUSED_RESULT
2883 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2884 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2887 parse_noargs_dec_ttl(ofpacts);
2889 struct ofpact_cnt_ids *ids;
2892 ids = ofpact_put_DEC_TTL(ofpacts);
2893 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2894 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2895 cntr = strtok_r(NULL, ", ", &arg)) {
2896 uint16_t id = atoi(cntr);
2898 ofpbuf_put(ofpacts, &id, sizeof id);
2899 ids = ofpacts->header;
2900 ids->n_controllers++;
2902 if (!ids->n_controllers) {
2903 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2906 ofpact_update_len(ofpacts, &ids->ofpact);
2912 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2916 ds_put_cstr(s, "dec_ttl");
2917 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2918 ds_put_cstr(s, "(");
2919 for (i = 0; i < a->n_controllers; i++) {
2921 ds_put_cstr(s, ",");
2923 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2925 ds_put_cstr(s, ")");
2929 /* Set MPLS label actions. */
2932 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label, struct ofpbuf *out)
2934 ofpact_put_SET_MPLS_LABEL(out)->label = label;
2939 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
2940 enum ofp_version ofp_version,
2943 if (ofp_version < OFP12_VERSION) {
2944 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
2946 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
2947 ntohl(label->label));
2951 static char * OVS_WARN_UNUSED_RESULT
2952 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
2953 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2955 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
2957 return xstrdup("set_mpls_label: expected label.");
2960 mpls_label->label = htonl(atoi(arg));
2965 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
2967 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
2970 /* Set MPLS TC actions. */
2973 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc, struct ofpbuf *out)
2975 ofpact_put_SET_MPLS_TC(out)->tc = tc;
2980 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
2981 enum ofp_version ofp_version, struct ofpbuf *out)
2983 if (ofp_version < OFP12_VERSION) {
2984 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
2986 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
2990 static char * OVS_WARN_UNUSED_RESULT
2991 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
2992 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2994 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
2997 return xstrdup("set_mpls_tc: expected tc.");
3000 mpls_tc->tc = atoi(arg);
3005 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3007 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
3010 /* Set MPLS TTL actions. */
3013 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl, struct ofpbuf *out)
3015 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3020 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3021 enum ofp_version ofp_version, struct ofpbuf *out)
3023 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3026 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3027 * action to 'ofpacts'.
3029 * Returns NULL if successful, otherwise a malloc()'d string describing the
3030 * error. The caller is responsible for freeing the returned string. */
3031 static char * OVS_WARN_UNUSED_RESULT
3032 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3033 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3035 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3038 return xstrdup("set_mpls_ttl: expected ttl.");
3041 mpls_ttl->ttl = atoi(arg);
3046 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3048 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3051 /* Decrement MPLS TTL actions. */
3054 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3056 ofpact_put_DEC_MPLS_TTL(out);
3061 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3062 enum ofp_version ofp_version, struct ofpbuf *out)
3064 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3067 static char * OVS_WARN_UNUSED_RESULT
3068 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3069 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3071 ofpact_put_DEC_MPLS_TTL(ofpacts);
3076 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3078 ds_put_cstr(s, "dec_mpls_ttl");
3081 /* Push MPLS label action. */
3084 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
3086 struct ofpact_push_mpls *oam;
3088 if (!eth_type_mpls(ethertype)) {
3089 return OFPERR_OFPBAC_BAD_ARGUMENT;
3091 oam = ofpact_put_PUSH_MPLS(out);
3092 oam->ethertype = ethertype;
3098 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3099 enum ofp_version ofp_version, struct ofpbuf *out)
3101 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3104 static char * OVS_WARN_UNUSED_RESULT
3105 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3106 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3111 error = str_to_u16(arg, "push_mpls", ðertype);
3113 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3119 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3121 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3124 /* Pop MPLS label action. */
3127 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
3129 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3134 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3135 enum ofp_version ofp_version, struct ofpbuf *out)
3137 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3140 static char * OVS_WARN_UNUSED_RESULT
3141 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3142 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3147 error = str_to_u16(arg, "pop_mpls", ðertype);
3149 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3155 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3157 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3160 /* Set tunnel ID actions. */
3163 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id, struct ofpbuf *out)
3165 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3166 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3167 tunnel->tun_id = tun_id;
3172 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id, struct ofpbuf *out)
3174 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3175 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3176 tunnel->tun_id = tun_id;
3181 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3182 enum ofp_version ofp_version, struct ofpbuf *out)
3184 uint64_t tun_id = tunnel->tun_id;
3186 if (ofp_version < OFP12_VERSION) {
3187 if (tun_id <= UINT32_MAX
3188 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3189 put_NXAST_SET_TUNNEL(out, tun_id);
3191 put_NXAST_SET_TUNNEL64(out, tun_id);
3194 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3198 static char * OVS_WARN_UNUSED_RESULT
3199 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3200 enum ofp_raw_action_type raw)
3202 struct ofpact_tunnel *tunnel;
3204 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3205 tunnel->ofpact.raw = raw;
3206 return str_to_u64(arg, &tunnel->tun_id);
3209 static char * OVS_WARN_UNUSED_RESULT
3210 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3211 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3213 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3217 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3219 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3220 (a->tun_id > UINT32_MAX
3221 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3225 /* Set queue action. */
3228 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id, struct ofpbuf *out)
3230 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3235 encode_SET_QUEUE(const struct ofpact_queue *queue,
3236 enum ofp_version ofp_version, struct ofpbuf *out)
3238 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3241 static char * OVS_WARN_UNUSED_RESULT
3242 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3243 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3245 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3249 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3251 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3254 /* Pop queue action. */
3257 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3259 ofpact_put_POP_QUEUE(out);
3264 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3265 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3267 put_NXAST_POP_QUEUE(out);
3270 static char * OVS_WARN_UNUSED_RESULT
3271 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3272 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3274 ofpact_put_POP_QUEUE(ofpacts);
3279 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3281 ds_put_cstr(s, "pop_queue");
3284 /* Action structure for NXAST_FIN_TIMEOUT.
3286 * This action changes the idle timeout or hard timeout, or both, of this
3287 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3288 * When such a packet is observed, the action reduces the rule's idle timeout
3289 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3290 * action has no effect on an existing timeout that is already shorter than the
3291 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3292 * of zero has no effect on the respective timeout.
3294 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3295 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3296 * receipt of the FIN or RST.
3298 * This is useful for quickly discarding learned TCP flows that otherwise will
3299 * take a long time to expire.
3301 * This action is intended for use with an OpenFlow rule that matches only a
3302 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3303 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3304 * RST in any of those flows will cause the entire OpenFlow rule to expire
3305 * early, which is not normally desirable.
3307 struct nx_action_fin_timeout {
3308 ovs_be16 type; /* OFPAT_VENDOR. */
3309 ovs_be16 len; /* 16. */
3310 ovs_be32 vendor; /* NX_VENDOR_ID. */
3311 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3312 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3313 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3314 ovs_be16 pad; /* Must be zero. */
3316 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3319 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3322 struct ofpact_fin_timeout *oft;
3324 oft = ofpact_put_FIN_TIMEOUT(out);
3325 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3326 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3331 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3332 enum ofp_version ofp_version OVS_UNUSED,
3335 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3336 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3337 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3340 static char * OVS_WARN_UNUSED_RESULT
3341 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3342 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3344 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3347 while (ofputil_parse_key_value(&arg, &key, &value)) {
3350 if (!strcmp(key, "idle_timeout")) {
3351 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3352 } else if (!strcmp(key, "hard_timeout")) {
3353 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3355 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3367 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3369 ds_put_cstr(s, "fin_timeout(");
3370 if (a->fin_idle_timeout) {
3371 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3373 if (a->fin_hard_timeout) {
3374 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3377 ds_put_char(s, ')');
3380 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3382 * These actions search one of the switch's flow tables:
3384 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3385 * it specifies the table to search.
3387 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3388 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3389 * table, that is, the OpenFlow flow table that contains the flow from
3390 * which this action was obtained. If this action did not come from a
3391 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3392 * is the current table.
3394 * The flow table lookup uses a flow that may be slightly modified from the
3397 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3398 * is used as the flow's in_port.
3400 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3401 * then its value is used as the flow's in_port. Otherwise, the original
3404 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3405 * resubmit action, then the flow is updated with the new values.
3407 * Following the lookup, the original in_port is restored.
3409 * If the modified flow matched in the flow table, then the corresponding
3410 * actions are executed. Afterward, actions following the resubmit in the
3411 * original set of actions, if any, are executed; any changes made to the
3412 * packet (e.g. changes to VLAN) by secondary actions persist when those
3413 * actions are executed, although the original in_port is restored.
3415 * Resubmit actions may be used any number of times within a set of actions.
3417 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3418 * implementation-defined depth, further resubmit actions are simply ignored.
3420 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3421 * 'pad' to be all-bits-zero.
3423 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3424 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3426 struct nx_action_resubmit {
3427 ovs_be16 type; /* OFPAT_VENDOR. */
3428 ovs_be16 len; /* Length is 16. */
3429 ovs_be32 vendor; /* NX_VENDOR_ID. */
3430 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3431 ovs_be16 in_port; /* New in_port for checking flow table. */
3432 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3435 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3438 decode_NXAST_RAW_RESUBMIT(uint16_t port, struct ofpbuf *out)
3440 struct ofpact_resubmit *resubmit;
3442 resubmit = ofpact_put_RESUBMIT(out);
3443 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3444 resubmit->in_port = u16_to_ofp(port);
3445 resubmit->table_id = 0xff;
3450 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3453 struct ofpact_resubmit *resubmit;
3455 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3456 return OFPERR_OFPBAC_BAD_ARGUMENT;
3459 resubmit = ofpact_put_RESUBMIT(out);
3460 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3461 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3462 resubmit->table_id = nar->table;
3467 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3468 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3470 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3472 if (resubmit->table_id == 0xff
3473 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3474 put_NXAST_RESUBMIT(out, in_port);
3476 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3477 nar->table = resubmit->table_id;
3478 nar->in_port = htons(in_port);
3482 static char * OVS_WARN_UNUSED_RESULT
3483 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3484 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3486 struct ofpact_resubmit *resubmit;
3487 char *in_port_s, *table_s;
3489 resubmit = ofpact_put_RESUBMIT(ofpacts);
3491 in_port_s = strsep(&arg, ",");
3492 if (in_port_s && in_port_s[0]) {
3493 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3494 return xasprintf("%s: resubmit to unknown port", in_port_s);
3497 resubmit->in_port = OFPP_IN_PORT;
3500 table_s = strsep(&arg, ",");
3501 if (table_s && table_s[0]) {
3502 uint32_t table_id = 0;
3505 error = str_to_u32(table_s, &table_id);
3509 resubmit->table_id = table_id;
3511 resubmit->table_id = 255;
3514 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3515 return xstrdup("at least one \"in_port\" or \"table\" must be "
3516 "specified on resubmit");
3522 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3524 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3525 ds_put_cstr(s, "resubmit:");
3526 ofputil_format_port(a->in_port, s);
3528 ds_put_format(s, "resubmit(");
3529 if (a->in_port != OFPP_IN_PORT) {
3530 ofputil_format_port(a->in_port, s);
3532 ds_put_char(s, ',');
3533 if (a->table_id != 255) {
3534 ds_put_format(s, "%"PRIu8, a->table_id);
3536 ds_put_char(s, ')');
3540 /* Action structure for NXAST_LEARN.
3542 * This action adds or modifies a flow in an OpenFlow table, similar to
3543 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3544 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3545 * flow's match criteria and actions are built by applying each of the series
3546 * of flow_mod_spec elements included as part of the action.
3548 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3549 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3550 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3551 * bits from a source to a destination. In this case, the header contains
3554 * 15 14 13 12 11 10 0
3555 * +------+---+------+---------------------------------+
3556 * | 0 |src| dst | n_bits |
3557 * +------+---+------+---------------------------------+
3559 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3560 * following table summarizes the meaning of each possible combination.
3561 * Details follow the table:
3564 * --- --- ----------------------------------------------------------
3565 * 0 0 Add match criteria based on value in a field.
3566 * 1 0 Add match criteria based on an immediate value.
3567 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3568 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3569 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3570 * All other combinations are undefined and not allowed.
3572 * The flow_mod_spec header is followed by a source specification and a
3573 * destination specification. The format and meaning of the source
3574 * specification depends on 'src':
3576 * - If 'src' is 0, the source bits are taken from a field in the flow to
3577 * which this action is attached. (This should be a wildcarded field. If
3578 * its value is fully specified then the source bits being copied have
3581 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3582 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3583 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3584 * 'field' and 'ofs' are subject to the same restrictions as the source
3585 * field in NXAST_REG_MOVE.
3587 * - If 'src' is 1, the source bits are a constant value. The source
3588 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3589 * number in network order, the source bits are the 'n_bits'
3590 * least-significant bits. The switch will report an error if other bits
3591 * in the constant are nonzero.
3593 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3594 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3595 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3596 * meaning of the flow_mod_spec depends on 'dst':
3598 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3599 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3600 * packet equal the source bits. 'field' may be any nxm_header with
3601 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3603 * Order is significant. Earlier flow_mod_specs must satisfy any
3604 * prerequisites for matching fields specified later, by copying constant
3605 * values into prerequisite fields.
3607 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3610 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3611 * the new flow. The new flow copies the source bits into
3612 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3615 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3616 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3618 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3619 * empty. It has the following meaning:
3621 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3622 * The new flow outputs to the OpenFlow port specified by the source field.
3623 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3624 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3627 * Resource Management
3628 * -------------------
3630 * A switch has a finite amount of flow table space available for learning.
3631 * When this space is exhausted, no new learning table entries will be learned
3632 * until some existing flow table entries expire. The controller should be
3633 * prepared to handle this by flooding (which can be implemented as a
3634 * low-priority flow).
3636 * If a learned flow matches a single TCP stream with a relatively long
3637 * timeout, one may make the best of resource constraints by setting
3638 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3639 * both, to shorter timeouts. When either of these is specified as a nonzero
3640 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3646 * The following examples give a prose description of the flow_mod_specs along
3647 * with informal notation for how those would be represented and a hex dump of
3648 * the bytes that would be required.
3650 * These examples could work with various nx_action_learn parameters. Typical
3651 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3652 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3654 * 1. Learn input port based on the source MAC, with lookup into
3655 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3657 * Match on in_port=99:
3658 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3659 * ovs_be16(99), 00 63
3660 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3662 * Match Ethernet destination on Ethernet source from packet:
3663 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3664 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3665 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3667 * Set NXM_NX_REG1[16:31] to the packet's input port:
3668 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3669 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3670 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3672 * Given a packet that arrived on port A with Ethernet source address B,
3673 * this would set up the flow "in_port=99, dl_dst=B,
3674 * actions=load:A->NXM_NX_REG1[16..31]".
3676 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3677 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3678 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3680 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3681 * into NXM_NX_REG1[16:31]:
3683 * Match on same VLAN ID as packet:
3684 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3685 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3686 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3688 * Match Ethernet destination on Ethernet source from packet:
3689 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3690 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3691 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3693 * Output to the packet's input port:
3694 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3695 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3697 * Given a packet that arrived on port A with Ethernet source address B in
3698 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3699 * actions=output:A".
3701 * In syntax accepted by ovs-ofctl, this action is:
3702 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3703 * output:NXM_OF_IN_PORT[])
3705 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3706 * 10-second MAC expiration time to make it easier to see what's going on
3708 * ovs-vsctl del-controller br0
3709 * ovs-ofctl del-flows br0
3710 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3711 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3712 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3713 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3714 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3716 * You can then dump the MAC learning table with:
3718 * ovs-ofctl dump-flows br0 table=1
3723 * For best performance, segregate learned flows into a table that is not used
3724 * for any other flows except possibly for a lowest-priority "catch-all" flow
3725 * (a flow with no match criteria). If different learning actions specify
3726 * different match criteria, use different tables for the learned flows.
3728 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3729 * These timeouts apply to the flow that is added, which means that a flow with
3730 * an idle timeout will expire when no traffic has been sent *to* the learned
3731 * address. This is not usually the intent in MAC learning; instead, we want
3732 * the MAC learn entry to expire when no traffic has been sent *from* the
3733 * learned address. Use a hard timeout for that.
3736 * Visibility of Changes
3737 * ---------------------
3739 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3740 * flow translation are visible to flow table lookups made later in the flow
3741 * translation. This means that, in the example above, a MAC learned by the
3742 * learn action in table 0 would be found in table 1 (if the packet being
3743 * processed had the same source and destination MAC address).
3745 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3746 * modify a flow) by a "learn" action are visible only for later flow
3747 * translations, not for later lookups within the same flow translation. In
3748 * the MAC learning example, a MAC learned by the learn action in table 0 would
3749 * not be found in table 1 if the flow translation would resubmit to table 1
3750 * after the processing of the learn action, meaning that if this MAC had not
3751 * been learned before then the packet would be flooded. */
3752 struct nx_action_learn {
3753 ovs_be16 type; /* OFPAT_VENDOR. */
3754 ovs_be16 len; /* At least 24. */
3755 ovs_be32 vendor; /* NX_VENDOR_ID. */
3756 ovs_be16 subtype; /* NXAST_LEARN. */
3757 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3758 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3759 ovs_be16 priority; /* Priority level of flow entry. */
3760 ovs_be64 cookie; /* Cookie for new flow. */
3761 ovs_be16 flags; /* NX_LEARN_F_*. */
3762 uint8_t table_id; /* Table to insert flow entry. */
3763 uint8_t pad; /* Must be zero. */
3764 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3765 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3766 /* Followed by a sequence of flow_mod_spec elements, as described above,
3767 * until the end of the action is reached. */
3769 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3772 get_be16(const void **pp)
3774 const ovs_be16 *p = *pp;
3775 ovs_be16 value = *p;
3781 get_be32(const void **pp)
3783 const ovs_be32 *p = *pp;
3784 ovs_be32 value = get_unaligned_be32(p);
3790 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3792 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3793 sf->ofs = ntohs(get_be16(p));
3794 sf->n_bits = n_bits;
3798 learn_min_len(uint16_t header)
3800 int n_bits = header & NX_LEARN_N_BITS_MASK;
3801 int src_type = header & NX_LEARN_SRC_MASK;
3802 int dst_type = header & NX_LEARN_DST_MASK;
3803 unsigned int min_len;
3806 if (src_type == NX_LEARN_SRC_FIELD) {
3807 min_len += sizeof(ovs_be32); /* src_field */
3808 min_len += sizeof(ovs_be16); /* src_ofs */
3810 min_len += DIV_ROUND_UP(n_bits, 16);
3812 if (dst_type == NX_LEARN_DST_MATCH ||
3813 dst_type == NX_LEARN_DST_LOAD) {
3814 min_len += sizeof(ovs_be32); /* dst_field */
3815 min_len += sizeof(ovs_be16); /* dst_ofs */
3820 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3821 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3823 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3824 struct ofpbuf *ofpacts)
3826 struct ofpact_learn *learn;
3827 const void *p, *end;
3830 return OFPERR_OFPBAC_BAD_ARGUMENT;
3833 learn = ofpact_put_LEARN(ofpacts);
3835 learn->idle_timeout = ntohs(nal->idle_timeout);
3836 learn->hard_timeout = ntohs(nal->hard_timeout);
3837 learn->priority = ntohs(nal->priority);
3838 learn->cookie = nal->cookie;
3839 learn->table_id = nal->table_id;
3840 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3841 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3843 learn->flags = ntohs(nal->flags);
3844 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3845 NX_LEARN_F_DELETE_LEARNED)) {
3846 return OFPERR_OFPBAC_BAD_ARGUMENT;
3849 if (learn->table_id == 0xff) {
3850 return OFPERR_OFPBAC_BAD_ARGUMENT;
3853 end = (char *) nal + ntohs(nal->len);
3854 for (p = nal + 1; p != end; ) {
3855 struct ofpact_learn_spec *spec;
3856 uint16_t header = ntohs(get_be16(&p));
3862 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3863 learn = ofpacts->header;
3866 spec->src_type = header & NX_LEARN_SRC_MASK;
3867 spec->dst_type = header & NX_LEARN_DST_MASK;
3868 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3870 /* Check for valid src and dst type combination. */
3871 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3872 spec->dst_type == NX_LEARN_DST_LOAD ||
3873 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3874 spec->src_type == NX_LEARN_SRC_FIELD)) {
3877 return OFPERR_OFPBAC_BAD_ARGUMENT;
3880 /* Check that the arguments don't overrun the end of the action. */
3881 if ((char *) end - (char *) p < learn_min_len(header)) {
3882 return OFPERR_OFPBAC_BAD_LEN;
3885 /* Get the source. */
3886 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3887 get_subfield(spec->n_bits, &p, &spec->src);
3889 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3891 bitwise_copy(p, p_bytes, 0,
3892 &spec->src_imm, sizeof spec->src_imm, 0,
3894 p = (const uint8_t *) p + p_bytes;
3897 /* Get the destination. */
3898 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3899 spec->dst_type == NX_LEARN_DST_LOAD) {
3900 get_subfield(spec->n_bits, &p, &spec->dst);
3903 ofpact_update_len(ofpacts, &learn->ofpact);
3905 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3906 return OFPERR_OFPBAC_BAD_ARGUMENT;
3913 put_be16(struct ofpbuf *b, ovs_be16 x)
3915 ofpbuf_put(b, &x, sizeof x);
3919 put_be32(struct ofpbuf *b, ovs_be32 x)
3921 ofpbuf_put(b, &x, sizeof x);
3925 put_u16(struct ofpbuf *b, uint16_t x)
3927 put_be16(b, htons(x));
3931 put_u32(struct ofpbuf *b, uint32_t x)
3933 put_be32(b, htonl(x));
3937 encode_LEARN(const struct ofpact_learn *learn,
3938 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3940 const struct ofpact_learn_spec *spec;
3941 struct nx_action_learn *nal;
3944 start_ofs = out->size;
3945 nal = put_NXAST_LEARN(out);
3946 nal->idle_timeout = htons(learn->idle_timeout);
3947 nal->hard_timeout = htons(learn->hard_timeout);
3948 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
3949 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
3950 nal->priority = htons(learn->priority);
3951 nal->cookie = learn->cookie;
3952 nal->flags = htons(learn->flags);
3953 nal->table_id = learn->table_id;
3955 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
3956 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
3958 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3959 put_u32(out, mf_nxm_header(spec->src.field->id));
3960 put_u16(out, spec->src.ofs);
3962 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3963 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
3964 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
3965 bits, n_dst_bytes, 0,
3969 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3970 spec->dst_type == NX_LEARN_DST_LOAD) {
3971 put_u32(out, mf_nxm_header(spec->dst.field->id));
3972 put_u16(out, spec->dst.ofs);
3976 pad_ofpat(out, start_ofs);
3979 static char * OVS_WARN_UNUSED_RESULT
3980 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
3981 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3983 return learn_parse(arg, ofpacts);
3987 format_LEARN(const struct ofpact_learn *a, struct ds *s)
3992 /* Action structure for NXAST_CONJUNCTION. */
3993 struct nx_action_conjunction {
3994 ovs_be16 type; /* OFPAT_VENDOR. */
3995 ovs_be16 len; /* At least 16. */
3996 ovs_be32 vendor; /* NX_VENDOR_ID. */
3997 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4002 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4005 add_conjunction(struct ofpbuf *out,
4006 uint32_t id, uint8_t clause, uint8_t n_clauses)
4008 struct ofpact_conjunction *oc;
4010 oc = ofpact_put_CONJUNCTION(out);
4012 oc->clause = clause;
4013 oc->n_clauses = n_clauses;
4017 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4020 if (nac->n_clauses < 2 || nac->n_clauses > 64
4021 || nac->clause >= nac->n_clauses) {
4022 return OFPERR_NXBAC_BAD_CONJUNCTION;
4024 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4030 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4031 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4033 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4034 nac->clause = oc->clause;
4035 nac->n_clauses = oc->n_clauses;
4036 nac->id = htonl(oc->id);
4040 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4042 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4043 oc->id, oc->clause + 1, oc->n_clauses);
4046 static char * OVS_WARN_UNUSED_RESULT
4047 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4048 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4055 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4056 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4057 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4060 if (n_clauses < 2) {
4061 return xstrdup("conjunction must have at least 2 clauses");
4062 } else if (n_clauses > 64) {
4063 return xstrdup("conjunction must have at most 64 clauses");
4064 } else if (clause < 1) {
4065 return xstrdup("clause index must be positive");
4066 } else if (clause > n_clauses) {
4067 return xstrdup("clause index must be less than or equal to "
4068 "number of clauses");
4071 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4075 /* Action structure for NXAST_MULTIPATH.
4077 * This action performs the following steps in sequence:
4079 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4080 * Refer to the definition of "enum nx_mp_fields" for details.
4082 * The 'basis' value is used as a universal hash parameter, that is,
4083 * different values of 'basis' yield different hash functions. The
4084 * particular universal hash function used is implementation-defined.
4086 * The hashed fields' values are drawn from the current state of the
4087 * flow, including all modifications that have been made by actions up to
4090 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4091 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4094 * The output of the algorithm is 'link', an unsigned integer less than
4095 * or equal to 'max_link'.
4097 * Some algorithms use 'arg' as an additional argument.
4099 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4100 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4103 * The switch will reject actions that have an unknown 'fields', or an unknown
4104 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4105 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4106 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4108 struct nx_action_multipath {
4109 ovs_be16 type; /* OFPAT_VENDOR. */
4110 ovs_be16 len; /* Length is 32. */
4111 ovs_be32 vendor; /* NX_VENDOR_ID. */
4112 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4114 /* What fields to hash and how. */
4115 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4116 ovs_be16 basis; /* Universal hash parameter. */
4119 /* Multipath link choice algorithm to apply to hash value. */
4120 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4121 ovs_be16 max_link; /* Number of output links, minus 1. */
4122 ovs_be32 arg; /* Algorithm-specific argument. */
4125 /* Where to store the result. */
4126 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4127 ovs_be32 dst; /* Destination. */
4129 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4132 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4135 uint32_t n_links = ntohs(nam->max_link) + 1;
4136 size_t min_n_bits = log_2_ceil(n_links);
4137 struct ofpact_multipath *mp;
4139 mp = ofpact_put_MULTIPATH(out);
4140 mp->fields = ntohs(nam->fields);
4141 mp->basis = ntohs(nam->basis);
4142 mp->algorithm = ntohs(nam->algorithm);
4143 mp->max_link = ntohs(nam->max_link);
4144 mp->arg = ntohl(nam->arg);
4145 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4146 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4147 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4149 if (!flow_hash_fields_valid(mp->fields)) {
4150 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4151 return OFPERR_OFPBAC_BAD_ARGUMENT;
4152 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4153 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4154 && mp->algorithm != NX_MP_ALG_HRW
4155 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4156 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4157 return OFPERR_OFPBAC_BAD_ARGUMENT;
4158 } else if (mp->dst.n_bits < min_n_bits) {
4159 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4160 "%"PRIu32" links", min_n_bits, n_links);
4161 return OFPERR_OFPBAC_BAD_ARGUMENT;
4164 return multipath_check(mp, NULL);
4168 encode_MULTIPATH(const struct ofpact_multipath *mp,
4169 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4171 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4173 nam->fields = htons(mp->fields);
4174 nam->basis = htons(mp->basis);
4175 nam->algorithm = htons(mp->algorithm);
4176 nam->max_link = htons(mp->max_link);
4177 nam->arg = htonl(mp->arg);
4178 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4179 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4182 static char * OVS_WARN_UNUSED_RESULT
4183 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4184 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4186 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4190 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4192 multipath_format(a, s);
4195 /* Action structure for NXAST_NOTE.
4197 * This action has no effect. It is variable length. The switch does not
4198 * attempt to interpret the user-defined 'note' data in any way. A controller
4199 * can use this action to attach arbitrary metadata to a flow.
4201 * This action might go away in the future.
4203 struct nx_action_note {
4204 ovs_be16 type; /* OFPAT_VENDOR. */
4205 ovs_be16 len; /* A multiple of 8, but at least 16. */
4206 ovs_be32 vendor; /* NX_VENDOR_ID. */
4207 ovs_be16 subtype; /* NXAST_NOTE. */
4208 uint8_t note[6]; /* Start of user-defined data. */
4209 /* Possibly followed by additional user-defined data. */
4211 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4214 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan, struct ofpbuf *out)
4216 struct ofpact_note *note;
4217 unsigned int length;
4219 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4220 note = ofpact_put(out, OFPACT_NOTE,
4221 offsetof(struct ofpact_note, data) + length);
4222 note->length = length;
4223 memcpy(note->data, nan->note, length);
4229 encode_NOTE(const struct ofpact_note *note,
4230 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4232 size_t start_ofs = out->size;
4233 struct nx_action_note *nan;
4234 unsigned int remainder;
4237 put_NXAST_NOTE(out);
4238 out->size = out->size - sizeof nan->note;
4240 ofpbuf_put(out, note->data, note->length);
4242 len = out->size - start_ofs;
4243 remainder = len % OFP_ACTION_ALIGN;
4245 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4247 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4248 nan->len = htons(out->size - start_ofs);
4251 static char * OVS_WARN_UNUSED_RESULT
4252 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4253 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4255 struct ofpact_note *note;
4257 note = ofpact_put_NOTE(ofpacts);
4258 while (*arg != '\0') {
4269 byte = hexits_value(arg, 2, &ok);
4271 return xstrdup("bad hex digit in `note' argument");
4273 ofpbuf_put(ofpacts, &byte, 1);
4275 note = ofpacts->header;
4280 ofpact_update_len(ofpacts, ¬e->ofpact);
4285 format_NOTE(const struct ofpact_note *a, struct ds *s)
4289 ds_put_cstr(s, "note:");
4290 for (i = 0; i < a->length; i++) {
4292 ds_put_char(s, '.');
4294 ds_put_format(s, "%02"PRIx8, a->data[i]);
4301 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4303 ofpact_put_EXIT(out);
4308 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4309 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4311 put_NXAST_EXIT(out);
4314 static char * OVS_WARN_UNUSED_RESULT
4315 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4316 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4318 ofpact_put_EXIT(ofpacts);
4323 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4325 ds_put_cstr(s, "exit");
4328 /* Unroll xlate action. */
4331 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4332 enum ofp_version ofp_version OVS_UNUSED,
4333 struct ofpbuf *out OVS_UNUSED)
4338 static char * OVS_WARN_UNUSED_RESULT
4339 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4340 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4347 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4350 ds_put_cstr(s, "unroll_xlate");
4353 /* Action structure for NXAST_SAMPLE.
4355 * Samples matching packets with the given probability and sends them
4356 * each to the set of collectors identified with the given ID. The
4357 * probability is expressed as a number of packets to be sampled out
4358 * of USHRT_MAX packets, and must be >0.
4360 * When sending packet samples to IPFIX collectors, the IPFIX flow
4361 * record sent for each sampled packet is associated with the given
4362 * observation domain ID and observation point ID. Each IPFIX flow
4363 * record contain the sampled packet's headers when executing this
4364 * rule. If a sampled packet's headers are modified by previous
4365 * actions in the flow, those modified headers are sent. */
4366 struct nx_action_sample {
4367 ovs_be16 type; /* OFPAT_VENDOR. */
4368 ovs_be16 len; /* Length is 24. */
4369 ovs_be32 vendor; /* NX_VENDOR_ID. */
4370 ovs_be16 subtype; /* NXAST_SAMPLE. */
4371 ovs_be16 probability; /* Fraction of packets to sample. */
4372 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4373 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4374 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4376 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4379 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas, struct ofpbuf *out)
4381 struct ofpact_sample *sample;
4383 sample = ofpact_put_SAMPLE(out);
4384 sample->probability = ntohs(nas->probability);
4385 sample->collector_set_id = ntohl(nas->collector_set_id);
4386 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4387 sample->obs_point_id = ntohl(nas->obs_point_id);
4389 if (sample->probability == 0) {
4390 return OFPERR_OFPBAC_BAD_ARGUMENT;
4397 encode_SAMPLE(const struct ofpact_sample *sample,
4398 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4400 struct nx_action_sample *nas;
4402 nas = put_NXAST_SAMPLE(out);
4403 nas->probability = htons(sample->probability);
4404 nas->collector_set_id = htonl(sample->collector_set_id);
4405 nas->obs_domain_id = htonl(sample->obs_domain_id);
4406 nas->obs_point_id = htonl(sample->obs_point_id);
4409 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4410 * action to 'ofpacts'.
4412 * Returns NULL if successful, otherwise a malloc()'d string describing the
4413 * error. The caller is responsible for freeing the returned string. */
4414 static char * OVS_WARN_UNUSED_RESULT
4415 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4416 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4418 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4421 while (ofputil_parse_key_value(&arg, &key, &value)) {
4424 if (!strcmp(key, "probability")) {
4425 error = str_to_u16(value, "probability", &os->probability);
4426 if (!error && os->probability == 0) {
4427 error = xasprintf("invalid probability value \"%s\"", value);
4429 } else if (!strcmp(key, "collector_set_id")) {
4430 error = str_to_u32(value, &os->collector_set_id);
4431 } else if (!strcmp(key, "obs_domain_id")) {
4432 error = str_to_u32(value, &os->obs_domain_id);
4433 } else if (!strcmp(key, "obs_point_id")) {
4434 error = str_to_u32(value, &os->obs_point_id);
4436 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4443 if (os->probability == 0) {
4444 return xstrdup("non-zero \"probability\" must be specified on sample");
4450 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4452 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4453 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4454 a->probability, a->collector_set_id,
4455 a->obs_domain_id, a->obs_point_id);
4458 /* debug_recirc instruction. */
4460 static bool enable_debug;
4463 ofpact_dummy_enable(void)
4465 enable_debug = true;
4469 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4471 if (!enable_debug) {
4472 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4475 ofpact_put_DEBUG_RECIRC(out);
4480 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4481 enum ofp_version ofp_version OVS_UNUSED,
4484 put_NXAST_DEBUG_RECIRC(out);
4487 static char * OVS_WARN_UNUSED_RESULT
4488 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4489 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4491 ofpact_put_DEBUG_RECIRC(ofpacts);
4496 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4498 ds_put_cstr(s, "debug_recirc");
4501 /* Meter instruction. */
4504 encode_METER(const struct ofpact_meter *meter,
4505 enum ofp_version ofp_version, struct ofpbuf *out)
4507 if (ofp_version >= OFP13_VERSION) {
4508 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4512 static char * OVS_WARN_UNUSED_RESULT
4513 parse_METER(char *arg, struct ofpbuf *ofpacts,
4514 enum ofputil_protocol *usable_protocols)
4516 *usable_protocols &= OFPUTIL_P_OF13_UP;
4517 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
4521 format_METER(const struct ofpact_meter *a, struct ds *s)
4523 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
4526 /* Clear-Actions instruction. */
4529 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
4530 enum ofp_version ofp_version OVS_UNUSED,
4531 struct ofpbuf *out OVS_UNUSED)
4533 if (ofp_version > OFP10_VERSION) {
4534 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
4538 static char * OVS_WARN_UNUSED_RESULT
4539 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4540 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4542 ofpact_put_CLEAR_ACTIONS(ofpacts);
4547 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4549 ds_put_cstr(s, "clear_actions");
4552 /* Write-Actions instruction. */
4555 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
4556 enum ofp_version ofp_version, struct ofpbuf *out)
4558 if (ofp_version > OFP10_VERSION) {
4559 const size_t ofs = out->size;
4561 instruction_put_OFPIT11_WRITE_ACTIONS(out);
4562 ofpacts_put_openflow_actions(actions->actions,
4563 ofpact_nest_get_action_len(actions),
4565 ofpacts_update_instruction_actions(out, ofs);
4569 static char * OVS_WARN_UNUSED_RESULT
4570 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
4571 enum ofputil_protocol *usable_protocols)
4573 size_t ofs = ofpacts_pull(ofpacts);
4574 struct ofpact_nest *on;
4577 /* Add a Write-Actions instruction and then pull it off. */
4578 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
4579 ofpbuf_pull(ofpacts, sizeof *on);
4581 /* Parse nested actions.
4583 * We pulled off "write-actions" and the previous actions because the
4584 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4585 * that it doesn't actually include the nested actions. That means that
4586 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4587 * follows a Write-Actions, which is an invalid order. */
4588 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
4589 OFPACT_WRITE_ACTIONS);
4591 /* Put the Write-Actions back on and update its length. */
4592 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
4593 on->ofpact.len = ofpacts->size;
4595 /* Put any previous actions or instructions back on. */
4596 ofpbuf_push_uninit(ofpacts, ofs);
4602 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
4604 ds_put_cstr(s, "write_actions(");
4605 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
4606 ds_put_char(s, ')');
4609 /* Action structure for NXAST_WRITE_METADATA.
4611 * Modifies the 'mask' bits of the metadata value. */
4612 struct nx_action_write_metadata {
4613 ovs_be16 type; /* OFPAT_VENDOR. */
4614 ovs_be16 len; /* Length is 32. */
4615 ovs_be32 vendor; /* NX_VENDOR_ID. */
4616 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
4617 uint8_t zeros[6]; /* Must be zero. */
4618 ovs_be64 metadata; /* Metadata register. */
4619 ovs_be64 mask; /* Metadata mask. */
4621 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
4624 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
4627 struct ofpact_metadata *om;
4629 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
4630 return OFPERR_NXBRC_MUST_BE_ZERO;
4633 om = ofpact_put_WRITE_METADATA(out);
4634 om->metadata = nawm->metadata;
4635 om->mask = nawm->mask;
4641 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
4642 enum ofp_version ofp_version, struct ofpbuf *out)
4644 if (ofp_version == OFP10_VERSION) {
4645 struct nx_action_write_metadata *nawm;
4647 nawm = put_NXAST_WRITE_METADATA(out);
4648 nawm->metadata = metadata->metadata;
4649 nawm->mask = metadata->mask;
4651 struct ofp11_instruction_write_metadata *oiwm;
4653 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
4654 oiwm->metadata = metadata->metadata;
4655 oiwm->metadata_mask = metadata->mask;
4659 static char * OVS_WARN_UNUSED_RESULT
4660 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
4661 enum ofputil_protocol *usable_protocols)
4663 struct ofpact_metadata *om;
4664 char *mask = strchr(arg, '/');
4666 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
4668 om = ofpact_put_WRITE_METADATA(ofpacts);
4673 error = str_to_be64(mask + 1, &om->mask);
4678 om->mask = OVS_BE64_MAX;
4681 return str_to_be64(arg, &om->metadata);
4685 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
4687 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
4688 if (a->mask != OVS_BE64_MAX) {
4689 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
4693 /* Goto-Table instruction. */
4696 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
4697 enum ofp_version ofp_version, struct ofpbuf *out)
4699 if (ofp_version == OFP10_VERSION) {
4700 struct nx_action_resubmit *nar;
4702 nar = put_NXAST_RESUBMIT_TABLE(out);
4703 nar->table = goto_table->table_id;
4704 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
4706 struct ofp11_instruction_goto_table *oigt;
4708 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
4709 oigt->table_id = goto_table->table_id;
4710 memset(oigt->pad, 0, sizeof oigt->pad);
4714 static char * OVS_WARN_UNUSED_RESULT
4715 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
4716 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4718 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
4719 char *table_s = strsep(&arg, ",");
4720 if (!table_s || !table_s[0]) {
4721 return xstrdup("instruction goto-table needs table id");
4723 return str_to_u8(table_s, "table", &ogt->table_id);
4727 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
4729 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
4733 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
4734 const struct ofp_action_header *bad_action, enum ofperr error)
4736 if (!VLOG_DROP_WARN(&rl)) {
4740 ds_put_hex_dump(&s, actions, actions_len, 0, false);
4741 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
4742 (char *)bad_action - (char *)actions,
4743 ofperr_get_name(error), ds_cstr(&s));
4749 ofpacts_decode(const void *actions, size_t actions_len,
4750 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
4752 struct ofpbuf openflow;
4754 ofpbuf_use_const(&openflow, actions, actions_len);
4755 while (openflow.size) {
4756 const struct ofp_action_header *action = openflow.data;
4757 enum ofp_raw_action_type raw;
4761 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
4763 error = ofpact_decode(action, raw, arg, ofpacts);
4767 log_bad_action(actions, actions_len, action, error);
4772 ofpact_pad(ofpacts);
4777 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
4778 unsigned int actions_len,
4779 enum ofp_version version,
4780 uint32_t allowed_ovsinsts,
4781 struct ofpbuf *ofpacts,
4782 enum ofpact_type outer_action)
4784 const struct ofp_action_header *actions;
4787 if (!outer_action) {
4788 ofpbuf_clear(ofpacts);
4791 if (actions_len % OFP_ACTION_ALIGN != 0) {
4792 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
4793 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
4794 return OFPERR_OFPBRC_BAD_LEN;
4797 actions = ofpbuf_try_pull(openflow, actions_len);
4798 if (actions == NULL) {
4799 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
4800 "remaining message length (%"PRIu32")",
4801 actions_len, openflow->size);
4802 return OFPERR_OFPBRC_BAD_LEN;
4805 error = ofpacts_decode(actions, actions_len, version, ofpacts);
4807 ofpbuf_clear(ofpacts);
4811 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
4814 ofpbuf_clear(ofpacts);
4819 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
4820 * front of 'openflow' into ofpacts. On success, replaces any existing content
4821 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
4822 * Returns 0 if successful, otherwise an OpenFlow error.
4824 * Actions are processed according to their OpenFlow version which
4825 * is provided in the 'version' parameter.
4827 * In most places in OpenFlow, actions appear encapsulated in instructions, so
4828 * you should call ofpacts_pull_openflow_instructions() instead of this
4831 * The parsed actions are valid generically, but they may not be valid in a
4832 * specific context. For example, port numbers up to OFPP_MAX are valid
4833 * generically, but specific datapaths may only support port numbers in a
4834 * smaller range. Use ofpacts_check() to additional check whether actions are
4835 * valid in a specific context. */
4837 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
4838 unsigned int actions_len,
4839 enum ofp_version version,
4840 struct ofpbuf *ofpacts)
4842 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
4843 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4847 /* OpenFlow 1.1 actions. */
4850 /* True if an action sets the value of a field
4851 * in a way that is compatibile with the action set.
4852 * The field can be set via either a set or a move action.
4853 * False otherwise. */
4855 ofpact_is_set_or_move_action(const struct ofpact *a)
4858 case OFPACT_SET_FIELD:
4859 case OFPACT_REG_MOVE:
4860 case OFPACT_SET_ETH_DST:
4861 case OFPACT_SET_ETH_SRC:
4862 case OFPACT_SET_IP_DSCP:
4863 case OFPACT_SET_IP_ECN:
4864 case OFPACT_SET_IP_TTL:
4865 case OFPACT_SET_IPV4_DST:
4866 case OFPACT_SET_IPV4_SRC:
4867 case OFPACT_SET_L4_DST_PORT:
4868 case OFPACT_SET_L4_SRC_PORT:
4869 case OFPACT_SET_MPLS_LABEL:
4870 case OFPACT_SET_MPLS_TC:
4871 case OFPACT_SET_MPLS_TTL:
4872 case OFPACT_SET_QUEUE:
4873 case OFPACT_SET_TUNNEL:
4874 case OFPACT_SET_VLAN_PCP:
4875 case OFPACT_SET_VLAN_VID:
4878 case OFPACT_CLEAR_ACTIONS:
4879 case OFPACT_CONTROLLER:
4880 case OFPACT_DEC_MPLS_TTL:
4881 case OFPACT_DEC_TTL:
4882 case OFPACT_ENQUEUE:
4884 case OFPACT_UNROLL_XLATE:
4885 case OFPACT_FIN_TIMEOUT:
4886 case OFPACT_GOTO_TABLE:
4889 case OFPACT_CONJUNCTION:
4891 case OFPACT_MULTIPATH:
4894 case OFPACT_OUTPUT_REG:
4895 case OFPACT_POP_MPLS:
4896 case OFPACT_POP_QUEUE:
4897 case OFPACT_PUSH_MPLS:
4898 case OFPACT_PUSH_VLAN:
4899 case OFPACT_RESUBMIT:
4901 case OFPACT_STACK_POP:
4902 case OFPACT_STACK_PUSH:
4903 case OFPACT_STRIP_VLAN:
4904 case OFPACT_WRITE_ACTIONS:
4905 case OFPACT_WRITE_METADATA:
4906 case OFPACT_DEBUG_RECIRC:
4913 /* True if an action is allowed in the action set.
4914 * False otherwise. */
4916 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
4919 case OFPACT_DEC_MPLS_TTL:
4920 case OFPACT_DEC_TTL:
4923 case OFPACT_POP_MPLS:
4924 case OFPACT_PUSH_MPLS:
4925 case OFPACT_PUSH_VLAN:
4926 case OFPACT_REG_MOVE:
4927 case OFPACT_SET_FIELD:
4928 case OFPACT_SET_ETH_DST:
4929 case OFPACT_SET_ETH_SRC:
4930 case OFPACT_SET_IP_DSCP:
4931 case OFPACT_SET_IP_ECN:
4932 case OFPACT_SET_IP_TTL:
4933 case OFPACT_SET_IPV4_DST:
4934 case OFPACT_SET_IPV4_SRC:
4935 case OFPACT_SET_L4_DST_PORT:
4936 case OFPACT_SET_L4_SRC_PORT:
4937 case OFPACT_SET_MPLS_LABEL:
4938 case OFPACT_SET_MPLS_TC:
4939 case OFPACT_SET_MPLS_TTL:
4940 case OFPACT_SET_QUEUE:
4941 case OFPACT_SET_TUNNEL:
4942 case OFPACT_SET_VLAN_PCP:
4943 case OFPACT_SET_VLAN_VID:
4944 case OFPACT_STRIP_VLAN:
4947 /* In general these actions are excluded because they are not part of
4948 * the OpenFlow specification nor map to actions that are defined in
4949 * the specification. Thus the order in which they should be applied
4950 * in the action set is undefined. */
4952 case OFPACT_CONTROLLER:
4953 case OFPACT_ENQUEUE:
4955 case OFPACT_UNROLL_XLATE:
4956 case OFPACT_FIN_TIMEOUT:
4958 case OFPACT_CONJUNCTION:
4959 case OFPACT_MULTIPATH:
4961 case OFPACT_OUTPUT_REG:
4962 case OFPACT_POP_QUEUE:
4963 case OFPACT_RESUBMIT:
4965 case OFPACT_STACK_POP:
4966 case OFPACT_STACK_PUSH:
4967 case OFPACT_DEBUG_RECIRC:
4969 /* The action set may only include actions and thus
4970 * may not include any instructions */
4971 case OFPACT_CLEAR_ACTIONS:
4972 case OFPACT_GOTO_TABLE:
4974 case OFPACT_WRITE_ACTIONS:
4975 case OFPACT_WRITE_METADATA:
4982 /* Append ofpact 'a' onto the tail of 'out' */
4984 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
4986 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
4989 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
4991 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
4992 enum ofpact_type filter)
4994 const struct ofpact *target;
4995 const struct ofpact *a;
4998 OFPACT_FOR_EACH (a, in->data, in->size) {
4999 if (a->type == filter) {
5004 ofpact_copy(out, target);
5006 return target != NULL;
5009 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5010 * The order of appended ofpacts is preserved between 'in' and 'out' */
5012 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5013 bool (*filter)(const struct ofpact *))
5015 const struct ofpact *a;
5017 OFPACT_FOR_EACH (a, in->data, in->size) {
5019 ofpact_copy(out, a);
5024 /* Reads 'action_set', which contains ofpacts accumulated by
5025 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5026 * executed directly into 'action_list'. (These names correspond to the
5027 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5029 * In general this involves appending the last instance of each action that is
5030 * admissible in the action set in the order described in the OpenFlow
5034 * + output action is only appended if no group action was present in 'in'.
5035 * + As a simplification all set actions are copied in the order the are
5036 * provided in 'in' as many set actions applied to a field has the same
5037 * affect as only applying the last action that sets a field and
5038 * duplicates are removed by do_xlate_actions().
5039 * This has an unwanted side-effect of compsoting multiple
5040 * LOAD_REG actions that touch different regions of the same field. */
5042 ofpacts_execute_action_set(struct ofpbuf *action_list,
5043 const struct ofpbuf *action_set)
5045 /* The OpenFlow spec "Action Set" section specifies this order. */
5046 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5047 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5048 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5049 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5050 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5051 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5052 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5053 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5055 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5056 * we should execute only OFPACT_GROUP.
5058 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5059 * all the actions because there's no point in modifying a packet that will
5060 * not be sent anywhere. */
5061 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5062 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5063 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5064 ofpbuf_clear(action_list);
5070 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5071 size_t n_in, enum ofp_version version,
5076 size_t start = out->size;
5078 error = ofpacts_decode(in, n_in, version, out);
5084 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5085 if (!ofpact_is_allowed_in_actions_set(a)) {
5086 VLOG_WARN_RL(&rl, "disallowed action in action set");
5087 return OFPERR_OFPBAC_BAD_TYPE;
5094 /* OpenFlow 1.1 instructions. */
5096 struct instruction_type_info {
5097 enum ovs_instruction_type type;
5101 static const struct instruction_type_info inst_info[] = {
5102 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5108 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5110 return inst_info[type].name;
5114 ovs_instruction_type_from_name(const char *name)
5116 const struct instruction_type_info *p;
5117 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5118 if (!strcasecmp(name, p->name)) {
5125 enum ovs_instruction_type
5126 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5130 return OVSINST_OFPIT13_METER;
5131 case OFPACT_CLEAR_ACTIONS:
5132 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5133 case OFPACT_WRITE_ACTIONS:
5134 return OVSINST_OFPIT11_WRITE_ACTIONS;
5135 case OFPACT_WRITE_METADATA:
5136 return OVSINST_OFPIT11_WRITE_METADATA;
5137 case OFPACT_GOTO_TABLE:
5138 return OVSINST_OFPIT11_GOTO_TABLE;
5141 case OFPACT_CONTROLLER:
5142 case OFPACT_ENQUEUE:
5143 case OFPACT_OUTPUT_REG:
5145 case OFPACT_SET_VLAN_VID:
5146 case OFPACT_SET_VLAN_PCP:
5147 case OFPACT_STRIP_VLAN:
5148 case OFPACT_PUSH_VLAN:
5149 case OFPACT_SET_ETH_SRC:
5150 case OFPACT_SET_ETH_DST:
5151 case OFPACT_SET_IPV4_SRC:
5152 case OFPACT_SET_IPV4_DST:
5153 case OFPACT_SET_IP_DSCP:
5154 case OFPACT_SET_IP_ECN:
5155 case OFPACT_SET_IP_TTL:
5156 case OFPACT_SET_L4_SRC_PORT:
5157 case OFPACT_SET_L4_DST_PORT:
5158 case OFPACT_REG_MOVE:
5159 case OFPACT_SET_FIELD:
5160 case OFPACT_STACK_PUSH:
5161 case OFPACT_STACK_POP:
5162 case OFPACT_DEC_TTL:
5163 case OFPACT_SET_MPLS_LABEL:
5164 case OFPACT_SET_MPLS_TC:
5165 case OFPACT_SET_MPLS_TTL:
5166 case OFPACT_DEC_MPLS_TTL:
5167 case OFPACT_PUSH_MPLS:
5168 case OFPACT_POP_MPLS:
5169 case OFPACT_SET_TUNNEL:
5170 case OFPACT_SET_QUEUE:
5171 case OFPACT_POP_QUEUE:
5172 case OFPACT_FIN_TIMEOUT:
5173 case OFPACT_RESUBMIT:
5175 case OFPACT_CONJUNCTION:
5176 case OFPACT_MULTIPATH:
5179 case OFPACT_UNROLL_XLATE:
5181 case OFPACT_DEBUG_RECIRC:
5183 return OVSINST_OFPIT11_APPLY_ACTIONS;
5188 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
5189 const uint16_t inst_type)
5191 switch (inst_type) {
5193 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5195 *instruction_type = OVSINST_##ENUM; \
5201 return OFPERR_OFPBIC_UNKNOWN_INST;
5205 /* Two-way translation between OVS's internal "OVSINST_*" representation of
5206 * instructions and the "OFPIT_*" representation used in OpenFlow. */
5207 struct ovsinst_map {
5208 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
5209 int ofpit; /* OFPIT_* number from OpenFlow spec. */
5212 static const struct ovsinst_map *
5213 get_ovsinst_map(enum ofp_version version)
5215 /* OpenFlow 1.1 and 1.2 instructions. */
5216 static const struct ovsinst_map of11[] = {
5217 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5218 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5219 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5220 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5221 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5225 /* OpenFlow 1.3+ instructions. */
5226 static const struct ovsinst_map of13[] = {
5227 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5228 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5229 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5230 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5231 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5232 { OVSINST_OFPIT13_METER, 6 },
5236 return version < OFP13_VERSION ? of11 : of13;
5239 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
5240 * values, into a bitmap of instructions suitable for OpenFlow 'version'
5241 * (OFP11_VERSION or later), and returns the result. */
5243 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
5245 uint32_t ofpit_bitmap = 0;
5246 const struct ovsinst_map *x;
5248 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5249 if (ovsinst_bitmap & (1u << x->ovsinst)) {
5250 ofpit_bitmap |= 1u << x->ofpit;
5253 return htonl(ofpit_bitmap);
5256 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5257 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5258 * correspond to OVSINST_* values, and returns the result. */
5260 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
5262 uint32_t ovsinst_bitmap = 0;
5263 const struct ovsinst_map *x;
5265 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5266 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
5267 ovsinst_bitmap |= 1u << x->ovsinst;
5270 return ovsinst_bitmap;
5273 static inline struct ofp11_instruction *
5274 instruction_next(const struct ofp11_instruction *inst)
5276 return ((struct ofp11_instruction *) (void *)
5277 ((uint8_t *) inst + ntohs(inst->len)));
5281 instruction_is_valid(const struct ofp11_instruction *inst,
5282 size_t n_instructions)
5284 uint16_t len = ntohs(inst->len);
5285 return (!(len % OFP11_INSTRUCTION_ALIGN)
5286 && len >= sizeof *inst
5287 && len / sizeof *inst <= n_instructions);
5290 /* This macro is careful to check for instructions with bad lengths. */
5291 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
5292 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
5293 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
5294 ((LEFT) -= (ntohs((ITER)->len) \
5295 / sizeof(struct ofp11_instruction)), \
5296 (ITER) = instruction_next(ITER)))
5299 decode_openflow11_instruction(const struct ofp11_instruction *inst,
5300 enum ovs_instruction_type *type)
5302 uint16_t len = ntohs(inst->len);
5304 switch (inst->type) {
5305 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5306 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5308 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5309 case CONSTANT_HTONS(ENUM): \
5311 ? len >= sizeof(struct STRUCT) \
5312 : len == sizeof(struct STRUCT)) { \
5313 *type = OVSINST_##ENUM; \
5316 return OFPERR_OFPBIC_BAD_LEN; \
5322 return OFPERR_OFPBIC_UNKNOWN_INST;
5327 decode_openflow11_instructions(const struct ofp11_instruction insts[],
5329 const struct ofp11_instruction *out[])
5331 const struct ofp11_instruction *inst;
5334 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5335 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5336 enum ovs_instruction_type type;
5339 error = decode_openflow11_instruction(inst, &type);
5345 return OFPERR_OFPBIC_DUP_INST;
5351 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5352 (n_insts - left) * sizeof *inst);
5353 return OFPERR_OFPBIC_BAD_LEN;
5359 get_actions_from_instruction(const struct ofp11_instruction *inst,
5360 const struct ofp_action_header **actions,
5361 size_t *actions_len)
5363 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5364 *actions_len = ntohs(inst->len) - sizeof *inst;
5368 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5369 unsigned int instructions_len,
5370 enum ofp_version version,
5371 struct ofpbuf *ofpacts)
5373 const struct ofp11_instruction *instructions;
5374 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5377 if (version == OFP10_VERSION) {
5378 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5380 (1u << N_OVS_INSTRUCTIONS) - 1,
5384 ofpbuf_clear(ofpacts);
5386 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5387 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5389 instructions_len, OFP11_INSTRUCTION_ALIGN);
5390 error = OFPERR_OFPBIC_BAD_LEN;
5394 instructions = ofpbuf_try_pull(openflow, instructions_len);
5395 if (instructions == NULL) {
5396 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5397 "remaining message length (%"PRIu32")",
5398 instructions_len, openflow->size);
5399 error = OFPERR_OFPBIC_BAD_LEN;
5403 error = decode_openflow11_instructions(
5404 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5410 if (insts[OVSINST_OFPIT13_METER]) {
5411 const struct ofp13_instruction_meter *oim;
5412 struct ofpact_meter *om;
5414 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5415 insts[OVSINST_OFPIT13_METER]);
5417 om = ofpact_put_METER(ofpacts);
5418 om->meter_id = ntohl(oim->meter_id);
5420 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5421 const struct ofp_action_header *actions;
5424 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5425 &actions, &actions_len);
5426 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5431 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5432 instruction_get_OFPIT11_CLEAR_ACTIONS(
5433 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5434 ofpact_put_CLEAR_ACTIONS(ofpacts);
5436 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5437 struct ofpact_nest *on;
5438 const struct ofp_action_header *actions;
5442 ofpact_pad(ofpacts);
5443 start = ofpacts->size;
5444 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5445 offsetof(struct ofpact_nest, actions));
5446 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5447 &actions, &actions_len);
5448 error = ofpacts_decode_for_action_set(actions, actions_len,
5453 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5454 on->ofpact.len = ofpacts->size - start;
5456 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5457 const struct ofp11_instruction_write_metadata *oiwm;
5458 struct ofpact_metadata *om;
5460 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5461 insts[OVSINST_OFPIT11_WRITE_METADATA]);
5463 om = ofpact_put_WRITE_METADATA(ofpacts);
5464 om->metadata = oiwm->metadata;
5465 om->mask = oiwm->metadata_mask;
5467 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5468 const struct ofp11_instruction_goto_table *oigt;
5469 struct ofpact_goto_table *ogt;
5471 oigt = instruction_get_OFPIT11_GOTO_TABLE(
5472 insts[OVSINST_OFPIT11_GOTO_TABLE]);
5473 ogt = ofpact_put_GOTO_TABLE(ofpacts);
5474 ogt->table_id = oigt->table_id;
5477 error = ofpacts_verify(ofpacts->data, ofpacts->size,
5478 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
5481 ofpbuf_clear(ofpacts);
5486 /* Update the length of the instruction that begins at offset 'ofs' within
5487 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5488 * If the instruction contains no nested actions, deletes it entirely. */
5490 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5492 struct ofp11_instruction_actions *oia;
5494 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5495 if (openflow->size > ofs + sizeof *oia) {
5496 oia->len = htons(openflow->size - ofs);
5498 openflow->size = ofs;
5502 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5503 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5504 * valid, otherwise an OpenFlow error code. */
5506 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5514 case OFPP_CONTROLLER:
5519 return OFPERR_OFPBAC_BAD_OUT_PORT;
5522 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
5525 return OFPERR_OFPBAC_BAD_OUT_PORT;
5529 /* Removes the protocols that require consistency between match and actions
5530 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5532 * (An example of an inconsistency between match and actions is a flow that
5533 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5536 inconsistent_match(enum ofputil_protocol *usable_protocols)
5538 *usable_protocols &= OFPUTIL_P_OF10_ANY;
5541 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5542 * caller must restore them.
5544 * Modifies some actions, filling in fields that could not be properly set
5545 * without context. */
5547 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
5548 struct flow *flow, ofp_port_t max_ports,
5549 uint8_t table_id, uint8_t n_tables)
5551 const struct ofpact_enqueue *enqueue;
5552 const struct mf_field *mf;
5556 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
5559 case OFPACT_CONTROLLER:
5562 case OFPACT_ENQUEUE:
5563 enqueue = ofpact_get_ENQUEUE(a);
5564 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
5565 && enqueue->port != OFPP_IN_PORT
5566 && enqueue->port != OFPP_LOCAL) {
5567 return OFPERR_OFPBAC_BAD_OUT_PORT;
5571 case OFPACT_OUTPUT_REG:
5572 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
5575 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
5577 case OFPACT_SET_VLAN_VID:
5578 /* Remember if we saw a vlan tag in the flow to aid translating to
5579 * OpenFlow 1.1+ if need be. */
5580 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
5581 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5582 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5583 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5584 inconsistent_match(usable_protocols);
5586 /* Temporary mark that we have a vlan tag. */
5587 flow->vlan_tci |= htons(VLAN_CFI);
5590 case OFPACT_SET_VLAN_PCP:
5591 /* Remember if we saw a vlan tag in the flow to aid translating to
5592 * OpenFlow 1.1+ if need be. */
5593 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
5594 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5595 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5596 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5597 inconsistent_match(usable_protocols);
5599 /* Temporary mark that we have a vlan tag. */
5600 flow->vlan_tci |= htons(VLAN_CFI);
5603 case OFPACT_STRIP_VLAN:
5604 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5605 inconsistent_match(usable_protocols);
5607 /* Temporary mark that we have no vlan tag. */
5608 flow->vlan_tci = htons(0);
5611 case OFPACT_PUSH_VLAN:
5612 if (flow->vlan_tci & htons(VLAN_CFI)) {
5613 /* Multiple VLAN headers not supported. */
5614 return OFPERR_OFPBAC_BAD_TAG;
5616 /* Temporary mark that we have a vlan tag. */
5617 flow->vlan_tci |= htons(VLAN_CFI);
5620 case OFPACT_SET_ETH_SRC:
5621 case OFPACT_SET_ETH_DST:
5624 case OFPACT_SET_IPV4_SRC:
5625 case OFPACT_SET_IPV4_DST:
5626 if (flow->dl_type != htons(ETH_TYPE_IP)) {
5627 inconsistent_match(usable_protocols);
5631 case OFPACT_SET_IP_DSCP:
5632 case OFPACT_SET_IP_ECN:
5633 case OFPACT_SET_IP_TTL:
5634 case OFPACT_DEC_TTL:
5635 if (!is_ip_any(flow)) {
5636 inconsistent_match(usable_protocols);
5640 case OFPACT_SET_L4_SRC_PORT:
5641 case OFPACT_SET_L4_DST_PORT:
5642 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
5643 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5644 && flow->nw_proto != IPPROTO_SCTP)) {
5645 inconsistent_match(usable_protocols);
5647 /* Note on which transport protocol the port numbers are set.
5648 * This allows this set action to be converted to an OF1.2 set field
5650 if (a->type == OFPACT_SET_L4_SRC_PORT) {
5651 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
5653 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
5657 case OFPACT_REG_MOVE:
5658 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
5660 case OFPACT_SET_FIELD:
5661 mf = ofpact_get_SET_FIELD(a)->field;
5662 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
5663 if (!mf_are_prereqs_ok(mf, flow) ||
5664 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
5665 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
5667 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5669 /* Remember if we saw a vlan tag in the flow to aid translating to
5670 * OpenFlow 1.1 if need be. */
5671 ofpact_get_SET_FIELD(a)->flow_has_vlan =
5672 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5673 if (mf->id == MFF_VLAN_TCI) {
5674 /* The set field may add or remove the vlan tag,
5675 * Mark the status temporarily. */
5676 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
5680 case OFPACT_STACK_PUSH:
5681 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
5683 case OFPACT_STACK_POP:
5684 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
5686 case OFPACT_SET_MPLS_LABEL:
5687 case OFPACT_SET_MPLS_TC:
5688 case OFPACT_SET_MPLS_TTL:
5689 case OFPACT_DEC_MPLS_TTL:
5690 if (!eth_type_mpls(flow->dl_type)) {
5691 inconsistent_match(usable_protocols);
5695 case OFPACT_SET_TUNNEL:
5696 case OFPACT_SET_QUEUE:
5697 case OFPACT_POP_QUEUE:
5698 case OFPACT_RESUBMIT:
5701 case OFPACT_FIN_TIMEOUT:
5702 if (flow->nw_proto != IPPROTO_TCP) {
5703 inconsistent_match(usable_protocols);
5708 return learn_check(ofpact_get_LEARN(a), flow);
5710 case OFPACT_CONJUNCTION:
5713 case OFPACT_MULTIPATH:
5714 return multipath_check(ofpact_get_MULTIPATH(a), flow);
5720 case OFPACT_PUSH_MPLS:
5721 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
5722 /* The packet is now MPLS and the MPLS payload is opaque.
5723 * Thus nothing can be assumed about the network protocol.
5724 * Temporarily mark that we have no nw_proto. */
5728 case OFPACT_POP_MPLS:
5729 if (!eth_type_mpls(flow->dl_type)) {
5730 inconsistent_match(usable_protocols);
5732 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
5738 case OFPACT_CLEAR_ACTIONS:
5741 case OFPACT_WRITE_ACTIONS: {
5742 /* Use a temporary copy of 'usable_protocols' because we can't check
5743 * consistency of an action set. */
5744 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
5745 enum ofputil_protocol p = *usable_protocols;
5746 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
5747 flow, max_ports, table_id, n_tables, &p);
5750 case OFPACT_WRITE_METADATA:
5753 case OFPACT_METER: {
5754 uint32_t mid = ofpact_get_METER(a)->meter_id;
5755 if (mid == 0 || mid > OFPM13_MAX) {
5756 return OFPERR_OFPMMFC_INVALID_METER;
5761 case OFPACT_GOTO_TABLE: {
5762 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
5763 if ((table_id != 255 && goto_table <= table_id)
5764 || (n_tables != 255 && goto_table >= n_tables)) {
5765 return OFPERR_OFPBIC_BAD_TABLE_ID;
5773 case OFPACT_UNROLL_XLATE:
5774 /* UNROLL is an internal action that should never be seen via
5776 return OFPERR_OFPBAC_BAD_TYPE;
5778 case OFPACT_DEBUG_RECIRC:
5786 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5787 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
5788 * switch with no more than 'max_ports' ports.
5790 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
5791 * '*usable_protocols' the protocols that forbid the inconsistency. (An
5792 * example of an inconsistency between match and actions is a flow that does
5793 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
5795 * May annotate ofpacts with information gathered from the 'flow'.
5797 * May temporarily modify 'flow', but restores the changes before returning. */
5799 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
5800 struct flow *flow, ofp_port_t max_ports,
5801 uint8_t table_id, uint8_t n_tables,
5802 enum ofputil_protocol *usable_protocols)
5805 ovs_be16 dl_type = flow->dl_type;
5806 ovs_be16 vlan_tci = flow->vlan_tci;
5807 uint8_t nw_proto = flow->nw_proto;
5808 enum ofperr error = 0;
5810 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5811 error = ofpact_check__(usable_protocols, a, flow,
5812 max_ports, table_id, n_tables);
5817 /* Restore fields that may have been modified. */
5818 flow->dl_type = dl_type;
5819 flow->vlan_tci = vlan_tci;
5820 flow->nw_proto = nw_proto;
5824 /* Like ofpacts_check(), but reports inconsistencies as
5825 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
5827 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
5828 struct flow *flow, ofp_port_t max_ports,
5829 uint8_t table_id, uint8_t n_tables,
5830 enum ofputil_protocol usable_protocols)
5832 enum ofputil_protocol p = usable_protocols;
5835 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
5836 table_id, n_tables, &p);
5837 return (error ? error
5838 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
5843 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
5845 if (outer_action != OFPACT_WRITE_ACTIONS) {
5846 VLOG_WARN("\"%s\" action doesn't support nested action \"%s\"",
5847 ofpact_name(outer_action), ofpact_name(a->type));
5848 return OFPERR_OFPBAC_BAD_ARGUMENT;
5854 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
5855 * appropriate order as defined by the OpenFlow spec and as required by Open
5858 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
5859 * instructions that are allowed within 'ofpacts[]'.
5861 * If 'outer_action' is not zero, it specifies that the actions are nested
5862 * within another action of type 'outer_action'. */
5864 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
5865 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
5867 const struct ofpact *a;
5868 enum ovs_instruction_type inst;
5870 inst = OVSINST_OFPIT13_METER;
5871 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5872 enum ovs_instruction_type next;
5874 if (a->type == OFPACT_CONJUNCTION) {
5875 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5876 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
5877 VLOG_WARN("\"conjunction\" actions may be used along with "
5878 "\"note\" but not any other kind of action "
5879 "(such as the \"%s\" action used here)",
5880 ofpact_name(a->type));
5881 return OFPERR_NXBAC_BAD_CONJUNCTION;
5888 enum ofperr error = ofpacts_verify_nested(a, outer_action);
5895 next = ovs_instruction_type_from_ofpact_type(a->type);
5897 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
5900 const char *name = ovs_instruction_name_from_type(inst);
5901 const char *next_name = ovs_instruction_name_from_type(next);
5904 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
5905 "1.1+ compatibility", name);
5907 VLOG_WARN("invalid instruction ordering: %s must appear "
5908 "before %s, for OpenFlow 1.1+ compatibility",
5911 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
5913 if (!((1u << next) & allowed_ovsinsts)) {
5914 const char *name = ovs_instruction_name_from_type(next);
5916 VLOG_WARN("%s instruction not allowed here", name);
5917 return OFPERR_OFPBIC_UNSUP_INST;
5926 /* Converting ofpacts to OpenFlow. */
5929 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
5933 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5934 case OFPACT_##ENUM: \
5935 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
5944 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
5945 * actions in 'openflow', appending the actions to any existing data in
5948 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
5949 struct ofpbuf *openflow,
5950 enum ofp_version ofp_version)
5952 const struct ofpact *a;
5953 size_t start_size = openflow->size;
5955 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5956 encode_ofpact(a, ofp_version, openflow);
5958 return openflow->size - start_size;
5961 static enum ovs_instruction_type
5962 ofpact_is_apply_actions(const struct ofpact *a)
5964 return (ovs_instruction_type_from_ofpact_type(a->type)
5965 == OVSINST_OFPIT11_APPLY_ACTIONS);
5969 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
5971 struct ofpbuf *openflow,
5972 enum ofp_version ofp_version)
5974 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
5975 const struct ofpact *a;
5977 if (ofp_version == OFP10_VERSION) {
5978 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
5985 if (ofpact_is_apply_actions(a)) {
5986 size_t ofs = openflow->size;
5988 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
5990 encode_ofpact(a, ofp_version, openflow);
5992 } while (a < end && ofpact_is_apply_actions(a));
5993 ofpacts_update_instruction_actions(openflow, ofs);
5995 encode_ofpact(a, ofp_version, openflow);
6001 /* Sets of supported actions. */
6003 /* Two-way translation between OVS's internal "OFPACT_*" representation of
6004 * actions and the "OFPAT_*" representation used in some OpenFlow version.
6005 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
6006 * instance is specific to one OpenFlow version.) */
6008 enum ofpact_type ofpact; /* Internal name for action type. */
6009 int ofpat; /* OFPAT_* number from OpenFlow spec. */
6012 static const struct ofpact_map *
6013 get_ofpact_map(enum ofp_version version)
6015 /* OpenFlow 1.0 actions. */
6016 static const struct ofpact_map of10[] = {
6017 { OFPACT_OUTPUT, 0 },
6018 { OFPACT_SET_VLAN_VID, 1 },
6019 { OFPACT_SET_VLAN_PCP, 2 },
6020 { OFPACT_STRIP_VLAN, 3 },
6021 { OFPACT_SET_ETH_SRC, 4 },
6022 { OFPACT_SET_ETH_DST, 5 },
6023 { OFPACT_SET_IPV4_SRC, 6 },
6024 { OFPACT_SET_IPV4_DST, 7 },
6025 { OFPACT_SET_IP_DSCP, 8 },
6026 { OFPACT_SET_L4_SRC_PORT, 9 },
6027 { OFPACT_SET_L4_DST_PORT, 10 },
6028 { OFPACT_ENQUEUE, 11 },
6032 /* OpenFlow 1.1 actions. */
6033 static const struct ofpact_map of11[] = {
6034 { OFPACT_OUTPUT, 0 },
6035 { OFPACT_SET_VLAN_VID, 1 },
6036 { OFPACT_SET_VLAN_PCP, 2 },
6037 { OFPACT_SET_ETH_SRC, 3 },
6038 { OFPACT_SET_ETH_DST, 4 },
6039 { OFPACT_SET_IPV4_SRC, 5 },
6040 { OFPACT_SET_IPV4_DST, 6 },
6041 { OFPACT_SET_IP_DSCP, 7 },
6042 { OFPACT_SET_IP_ECN, 8 },
6043 { OFPACT_SET_L4_SRC_PORT, 9 },
6044 { OFPACT_SET_L4_DST_PORT, 10 },
6045 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6046 /* OFPAT_COPY_TTL_IN (12) not supported. */
6047 { OFPACT_SET_MPLS_LABEL, 13 },
6048 { OFPACT_SET_MPLS_TC, 14 },
6049 { OFPACT_SET_MPLS_TTL, 15 },
6050 { OFPACT_DEC_MPLS_TTL, 16 },
6051 { OFPACT_PUSH_VLAN, 17 },
6052 { OFPACT_STRIP_VLAN, 18 },
6053 { OFPACT_PUSH_MPLS, 19 },
6054 { OFPACT_POP_MPLS, 20 },
6055 { OFPACT_SET_QUEUE, 21 },
6056 { OFPACT_GROUP, 22 },
6057 { OFPACT_SET_IP_TTL, 23 },
6058 { OFPACT_DEC_TTL, 24 },
6062 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6063 static const struct ofpact_map of12[] = {
6064 { OFPACT_OUTPUT, 0 },
6065 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6066 /* OFPAT_COPY_TTL_IN (12) not supported. */
6067 { OFPACT_SET_MPLS_TTL, 15 },
6068 { OFPACT_DEC_MPLS_TTL, 16 },
6069 { OFPACT_PUSH_VLAN, 17 },
6070 { OFPACT_STRIP_VLAN, 18 },
6071 { OFPACT_PUSH_MPLS, 19 },
6072 { OFPACT_POP_MPLS, 20 },
6073 { OFPACT_SET_QUEUE, 21 },
6074 { OFPACT_GROUP, 22 },
6075 { OFPACT_SET_IP_TTL, 23 },
6076 { OFPACT_DEC_TTL, 24 },
6077 { OFPACT_SET_FIELD, 25 },
6078 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
6079 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
6099 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
6100 * values, into a bitmap of actions suitable for OpenFlow 'version', and
6101 * returns the result. */
6103 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
6105 uint32_t openflow_bitmap = 0;
6106 const struct ofpact_map *x;
6108 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6109 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
6110 openflow_bitmap |= 1u << x->ofpat;
6113 return htonl(openflow_bitmap);
6116 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
6117 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
6118 * and returns the result. */
6120 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
6122 uint64_t ofpact_bitmap = 0;
6123 const struct ofpact_map *x;
6125 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6126 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
6127 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
6130 return ofpact_bitmap;
6133 /* Appends to 's' a string representation of the set of OFPACT_* represented
6134 * by 'ofpacts_bitmap'. */
6136 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
6138 if (!ofpacts_bitmap) {
6139 ds_put_cstr(s, "<none>");
6141 while (ofpacts_bitmap) {
6142 ds_put_format(s, "%s ",
6143 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
6144 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
6150 /* Returns true if 'action' outputs to 'port', false otherwise. */
6152 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
6154 switch (ofpact->type) {
6156 return ofpact_get_OUTPUT(ofpact)->port == port;
6157 case OFPACT_ENQUEUE:
6158 return ofpact_get_ENQUEUE(ofpact)->port == port;
6159 case OFPACT_CONTROLLER:
6160 return port == OFPP_CONTROLLER;
6162 case OFPACT_OUTPUT_REG:
6164 case OFPACT_SET_VLAN_VID:
6165 case OFPACT_SET_VLAN_PCP:
6166 case OFPACT_STRIP_VLAN:
6167 case OFPACT_PUSH_VLAN:
6168 case OFPACT_SET_ETH_SRC:
6169 case OFPACT_SET_ETH_DST:
6170 case OFPACT_SET_IPV4_SRC:
6171 case OFPACT_SET_IPV4_DST:
6172 case OFPACT_SET_IP_DSCP:
6173 case OFPACT_SET_IP_ECN:
6174 case OFPACT_SET_IP_TTL:
6175 case OFPACT_SET_L4_SRC_PORT:
6176 case OFPACT_SET_L4_DST_PORT:
6177 case OFPACT_REG_MOVE:
6178 case OFPACT_SET_FIELD:
6179 case OFPACT_STACK_PUSH:
6180 case OFPACT_STACK_POP:
6181 case OFPACT_DEC_TTL:
6182 case OFPACT_SET_MPLS_LABEL:
6183 case OFPACT_SET_MPLS_TC:
6184 case OFPACT_SET_MPLS_TTL:
6185 case OFPACT_DEC_MPLS_TTL:
6186 case OFPACT_SET_TUNNEL:
6187 case OFPACT_WRITE_METADATA:
6188 case OFPACT_SET_QUEUE:
6189 case OFPACT_POP_QUEUE:
6190 case OFPACT_FIN_TIMEOUT:
6191 case OFPACT_RESUBMIT:
6193 case OFPACT_CONJUNCTION:
6194 case OFPACT_MULTIPATH:
6197 case OFPACT_UNROLL_XLATE:
6198 case OFPACT_PUSH_MPLS:
6199 case OFPACT_POP_MPLS:
6201 case OFPACT_CLEAR_ACTIONS:
6202 case OFPACT_WRITE_ACTIONS:
6203 case OFPACT_GOTO_TABLE:
6206 case OFPACT_DEBUG_RECIRC:
6212 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6213 * to 'port', false otherwise. */
6215 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
6218 const struct ofpact *a;
6220 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6221 if (ofpact_outputs_to_port(a, port)) {
6229 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6230 * to 'group', false otherwise. */
6232 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
6235 const struct ofpact *a;
6237 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6238 if (a->type == OFPACT_GROUP
6239 && ofpact_get_GROUP(a)->group_id == group_id) {
6248 ofpacts_equal(const struct ofpact *a, size_t a_len,
6249 const struct ofpact *b, size_t b_len)
6251 return a_len == b_len && !memcmp(a, b, a_len);
6254 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
6255 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
6257 * This function relies on the order of 'ofpacts' being correct (as checked by
6258 * ofpacts_verify()). */
6260 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
6262 const struct ofpact *a;
6264 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6265 enum ovs_instruction_type inst;
6267 inst = ovs_instruction_type_from_ofpact_type(a->type);
6268 if (a->type == OFPACT_METER) {
6269 return ofpact_get_METER(a)->meter_id;
6270 } else if (inst > OVSINST_OFPIT13_METER) {
6278 /* Formatting ofpacts. */
6281 ofpact_format(const struct ofpact *a, struct ds *s)
6284 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6285 case OFPACT_##ENUM: \
6286 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
6295 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
6296 * 'ofpacts' to 'string'. */
6298 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
6302 ds_put_cstr(string, "drop");
6304 const struct ofpact *a;
6306 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6308 ds_put_cstr(string, ",");
6311 /* XXX write-actions */
6312 ofpact_format(a, string);
6317 /* Internal use by helpers. */
6320 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
6322 struct ofpact *ofpact;
6324 ofpact_pad(ofpacts);
6325 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
6326 ofpact = ofpacts->header;
6327 ofpact_init(ofpact, type, len);
6332 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
6334 memset(ofpact, 0, len);
6335 ofpact->type = type;
6340 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6341 * starting at 'ofpact'.
6343 * This is the correct way to update a variable-length ofpact's length after
6344 * adding the variable-length part of the payload. (See the large comment
6345 * near the end of ofp-actions.h for more information.) */
6347 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
6349 ovs_assert(ofpact == ofpacts->header);
6350 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6353 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6354 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6355 * client must call this itself after adding the final ofpact to an array of
6358 * (The consequences of failing to call this function are probably not dire.
6359 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6360 * not dereference it. That's undefined behavior, technically, but it will not
6361 * cause a real problem on common systems. Still, it seems better to call
6364 ofpact_pad(struct ofpbuf *ofpacts)
6366 unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
6368 ofpbuf_put_zeros(ofpacts, pad);
6375 static char * OVS_WARN_UNUSED_RESULT
6376 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6377 enum ofputil_protocol *usable_protocols)
6380 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6381 case OFPACT_##ENUM: \
6382 return parse_##ENUM(value, ofpacts, usable_protocols);
6391 ofpact_type_from_name(const char *name, enum ofpact_type *type)
6393 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6394 if (!strcasecmp(name, NAME)) { \
6395 *type = OFPACT_##ENUM; \
6404 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6406 * Returns NULL if successful, otherwise a malloc()'d string describing the
6407 * error. The caller is responsible for freeing the returned string.
6409 * If 'outer_action' is specified, indicates that the actions being parsed
6410 * are nested within another action of the type specified in 'outer_action'. */
6411 static char * OVS_WARN_UNUSED_RESULT
6412 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6413 enum ofputil_protocol *usable_protocols,
6414 bool allow_instructions, enum ofpact_type outer_action)
6423 while (ofputil_parse_key_value(&pos, &key, &value)) {
6424 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6425 enum ofpact_type type;
6429 if (ofpact_type_from_name(key, &type)) {
6430 error = ofpact_parse(type, value, ofpacts, usable_protocols);
6431 inst = ovs_instruction_type_from_ofpact_type(type);
6432 } else if (!strcasecmp(key, "mod_vlan_vid")) {
6433 error = parse_set_vlan_vid(value, ofpacts, true);
6434 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6435 error = parse_set_vlan_pcp(value, ofpacts, true);
6436 } else if (!strcasecmp(key, "set_nw_ttl")) {
6437 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6438 } else if (!strcasecmp(key, "pop_vlan")) {
6439 error = parse_pop_vlan(ofpacts);
6440 } else if (!strcasecmp(key, "set_tunnel64")) {
6441 error = parse_set_tunnel(value, ofpacts,
6442 NXAST_RAW_SET_TUNNEL64);
6443 } else if (!strcasecmp(key, "load")) {
6444 error = parse_reg_load(value, ofpacts);
6445 } else if (!strcasecmp(key, "bundle_load")) {
6446 error = parse_bundle_load(value, ofpacts);
6447 } else if (!strcasecmp(key, "drop")) {
6449 } else if (!strcasecmp(key, "apply_actions")) {
6450 return xstrdup("apply_actions is the default instruction");
6451 } else if (ofputil_port_from_string(key, &port)) {
6452 ofpact_put_OUTPUT(ofpacts)->port = port;
6454 return xasprintf("unknown action %s", key);
6460 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
6461 if (!allow_instructions) {
6462 return xasprintf("only actions are allowed here (not "
6464 ovs_instruction_name_from_type(inst));
6466 if (inst == prev_inst) {
6467 return xasprintf("instruction %s may be specified only once",
6468 ovs_instruction_name_from_type(inst));
6471 if (prev_inst != -1 && inst < prev_inst) {
6472 return xasprintf("instruction %s must be specified before %s",
6473 ovs_instruction_name_from_type(inst),
6474 ovs_instruction_name_from_type(prev_inst));
6478 ofpact_pad(ofpacts);
6480 if (drop && ofpacts->size) {
6481 return xstrdup("\"drop\" must not be accompanied by any other action "
6485 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
6487 ? (1u << N_OVS_INSTRUCTIONS) - 1
6488 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
6491 return xstrdup("Incorrect instruction ordering");
6497 static char * OVS_WARN_UNUSED_RESULT
6498 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
6499 enum ofputil_protocol *usable_protocols, bool allow_instructions,
6500 enum ofpact_type outer_action)
6502 uint32_t orig_size = ofpacts->size;
6503 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
6504 allow_instructions, outer_action);
6506 ofpacts->size = orig_size;
6511 static char * OVS_WARN_UNUSED_RESULT
6512 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
6513 enum ofputil_protocol *usable_protocols,
6514 bool allow_instructions, enum ofpact_type outer_action)
6518 *usable_protocols = OFPUTIL_P_ANY;
6521 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
6528 /* Parses 's' as a set of OpenFlow actions and appends the actions to
6529 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
6530 * that are nested within the action of type 'outer_action'.
6532 * Returns NULL if successful, otherwise a malloc()'d string describing the
6533 * error. The caller is responsible for freeing the returned string. */
6534 char * OVS_WARN_UNUSED_RESULT
6535 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
6536 enum ofputil_protocol *usable_protocols)
6538 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
6541 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
6544 * Returns NULL if successful, otherwise a malloc()'d string describing the
6545 * error. The caller is responsible for freeing the returned string. */
6546 char * OVS_WARN_UNUSED_RESULT
6547 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
6548 enum ofputil_protocol *usable_protocols)
6550 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
6554 ofpact_name(enum ofpact_type type)
6557 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
6564 /* Low-level action decoding and encoding functions. */
6566 /* Everything needed to identify a particular OpenFlow action. */
6567 struct ofpact_hdrs {
6568 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
6569 uint16_t type; /* Type if standard, otherwise subtype. */
6570 uint8_t ofp_version; /* From ofp_header. */
6573 /* Information about a particular OpenFlow action. */
6574 struct ofpact_raw_instance {
6575 /* The action's identity. */
6576 struct ofpact_hdrs hdrs;
6577 enum ofp_raw_action_type raw;
6579 /* Looking up the action. */
6580 struct hmap_node decode_node; /* Based on 'hdrs'. */
6581 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
6583 /* The action's encoded size.
6585 * If this action is fixed-length, 'min_length' == 'max_length'.
6586 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
6587 * OFP_ACTION_ALIGN) == 65528. */
6588 unsigned short int min_length;
6589 unsigned short int max_length;
6591 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
6592 * offset of that argument from the beginning of the action and 'arg_len'
6593 * its length, both in bytes.
6595 * For actions that take other forms, these are both zero. */
6596 unsigned short int arg_ofs;
6597 unsigned short int arg_len;
6599 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
6602 /* If this action is deprecated, a human-readable string with a brief
6604 const char *deprecation;
6607 /* Action header. */
6608 struct ofp_action_header {
6609 /* The meaning of other values of 'type' generally depends on the OpenFlow
6610 * version (see enum ofp_raw_action_type).
6612 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
6613 * designates an OpenFlow vendor ID and that the remainder of the action
6614 * structure has a vendor-defined meaning.
6616 #define OFPAT_VENDOR 0xffff
6619 /* Always a multiple of 8. */
6622 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
6623 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
6626 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
6628 /* Header for Nicira-defined actions and for ONF vendor extensions.
6630 * This cannot be used as an entirely generic vendor extension action header,
6631 * because OpenFlow does not specify the location or size of the action
6632 * subtype; it just happens that ONF extensions and Nicira extensions share
6634 struct ext_action_header {
6635 ovs_be16 type; /* OFPAT_VENDOR. */
6636 ovs_be16 len; /* At least 16. */
6637 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
6638 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
6641 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
6644 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
6645 const struct ofpact_hdrs *b)
6647 return (a->vendor == b->vendor
6648 && a->type == b->type
6649 && a->ofp_version == b->ofp_version);
6653 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
6655 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
6658 #include "ofp-actions.inc2"
6660 static struct hmap *
6661 ofpact_decode_hmap(void)
6663 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6664 static struct hmap hmap;
6666 if (ovsthread_once_start(&once)) {
6667 struct ofpact_raw_instance *inst;
6670 for (inst = all_raw_instances;
6671 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6673 hmap_insert(&hmap, &inst->decode_node,
6674 ofpact_hdrs_hash(&inst->hdrs));
6676 ovsthread_once_done(&once);
6681 static struct hmap *
6682 ofpact_encode_hmap(void)
6684 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6685 static struct hmap hmap;
6687 if (ovsthread_once_start(&once)) {
6688 struct ofpact_raw_instance *inst;
6691 for (inst = all_raw_instances;
6692 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6694 hmap_insert(&hmap, &inst->encode_node,
6695 hash_2words(inst->raw, inst->hdrs.ofp_version));
6697 ovsthread_once_done(&once);
6703 ofpact_decode_raw(enum ofp_version ofp_version,
6704 const struct ofp_action_header *oah, size_t length,
6705 const struct ofpact_raw_instance **instp)
6707 const struct ofpact_raw_instance *inst;
6708 struct ofpact_hdrs hdrs;
6711 if (length < sizeof *oah) {
6712 return OFPERR_OFPBAC_BAD_LEN;
6715 /* Get base action type. */
6716 if (oah->type == htons(OFPAT_VENDOR)) {
6718 hdrs.vendor = ntohl(oah->vendor);
6719 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
6720 /* Get extension subtype. */
6721 const struct ext_action_header *nah;
6723 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
6724 if (length < sizeof *nah) {
6725 return OFPERR_OFPBAC_BAD_LEN;
6727 hdrs.type = ntohs(nah->subtype);
6729 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
6731 return OFPERR_OFPBAC_BAD_VENDOR;
6735 hdrs.type = ntohs(oah->type);
6738 hdrs.ofp_version = ofp_version;
6739 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
6740 ofpact_decode_hmap()) {
6741 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
6748 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
6749 : OFPERR_OFPBAC_BAD_TYPE);
6753 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6754 enum ofp_raw_action_type *raw, uint64_t *arg)
6756 const struct ofp_action_header *oah = buf->data;
6757 const struct ofpact_raw_instance *action;
6758 unsigned int length;
6762 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
6767 if (action->deprecation) {
6768 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
6769 action->name, ofputil_version_to_string(ofp_version),
6770 action->deprecation);
6773 length = ntohs(oah->len);
6774 if (length > buf->size) {
6775 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
6776 "length %"PRIu32, action->name, length, buf->size);
6777 return OFPERR_OFPBAC_BAD_LEN;
6779 if (length < action->min_length || length > action->max_length) {
6780 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
6781 "[%hu,%hu]", action->name, length,
6782 action->min_length, action->max_length);
6783 return OFPERR_OFPBAC_BAD_LEN;
6786 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
6787 "of 8", action->name, length);
6788 return OFPERR_OFPBAC_BAD_LEN;
6793 if (action->arg_len) {
6797 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
6798 for (i = 0; i < action->arg_len; i++) {
6799 *arg = (*arg << 8) | p[i];
6803 ofpbuf_pull(buf, length);
6808 static const struct ofpact_raw_instance *
6809 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
6811 const struct ofpact_raw_instance *inst;
6813 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
6814 ofpact_encode_hmap()) {
6815 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
6823 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6824 enum ofp_raw_action_type raw, uint64_t arg)
6826 const struct ofpact_raw_instance *inst;
6827 struct ofp_action_header *oah;
6828 const struct ofpact_hdrs *hdrs;
6830 inst = ofpact_raw_lookup(ofp_version, raw);
6833 oah = ofpbuf_put_zeros(buf, inst->min_length);
6834 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
6835 oah->len = htons(inst->min_length);
6836 oah->vendor = htonl(hdrs->vendor);
6838 switch (hdrs->vendor) {
6843 case ONF_VENDOR_ID: {
6844 struct ext_action_header *nah = (struct ext_action_header *) oah;
6845 nah->subtype = htons(hdrs->type);
6853 if (inst->arg_len) {
6854 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
6857 for (i = 0; i < inst->arg_len; i++) {
6869 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
6871 struct ofp_action_header *oah;
6873 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs, 8));
6875 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
6876 oah->len = htons(openflow->size - start_ofs);