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 const char *full_arg = arg;
2575 uint64_t value = strtoull(arg, (char **) &arg, 0);
2576 struct mf_subfield dst;
2577 char *key, *value_str;
2580 error = set_field_split_str(arg, &key, &value_str, NULL);
2585 error = mf_parse_subfield(&dst, key);
2590 if (dst.n_bits < 64 && (value >> dst.n_bits) != 0) {
2591 return xasprintf("%s: value %"PRIu64" does not fit into %d bits",
2592 full_arg, value, dst.n_bits);
2595 sf->field = dst.field;
2596 memset(&sf->value, 0, sizeof sf->value);
2597 bitwise_put(value, &sf->value, dst.field->n_bytes, dst.ofs, dst.n_bits);
2598 bitwise_put(UINT64_MAX, &sf->mask,
2599 dst.field->n_bytes, dst.ofs, dst.n_bits);
2604 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2606 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2607 struct mf_subfield dst;
2610 dst.ofs = dst.n_bits = 0;
2611 while (next_load_segment(a, &dst, &value)) {
2612 ds_put_format(s, "load:%#"PRIx64"->", value);
2613 mf_format_subfield(&dst, s);
2614 ds_put_char(s, ',');
2618 ds_put_cstr(s, "set_field:");
2619 mf_format(a->field, &a->value, &a->mask, s);
2620 ds_put_format(s, "->%s", a->field->name);
2624 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2625 * is marked such that, if possible, it will be translated to OpenFlow as
2626 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2627 * that was the way that the action was expressed when it came into OVS or for
2628 * backward compatibility. */
2629 struct ofpact_set_field *
2630 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2632 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2633 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2637 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2639 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2642 struct nx_action_stack {
2643 ovs_be16 type; /* OFPAT_VENDOR. */
2644 ovs_be16 len; /* Length is 16. */
2645 ovs_be32 vendor; /* NX_VENDOR_ID. */
2646 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2647 ovs_be16 offset; /* Bit offset into the field. */
2649 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2650 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2651 * - Enough 0-bytes to pad out the action to 24 bytes. */
2652 uint8_t pad[12]; /* See above. */
2654 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2657 decode_stack_action(const struct nx_action_stack *nasp,
2658 struct ofpact_stack *stack_action)
2663 stack_action->subfield.ofs = ntohs(nasp->offset);
2665 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2666 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2667 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2671 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2673 if (!is_all_zeros(b.data, b.size)) {
2674 return OFPERR_NXBRC_MUST_BE_ZERO;
2681 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2682 struct ofpbuf *ofpacts)
2684 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2685 enum ofperr error = decode_stack_action(nasp, push);
2686 return error ? error : nxm_stack_push_check(push, NULL);
2690 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2691 struct ofpbuf *ofpacts)
2693 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2694 enum ofperr error = decode_stack_action(nasp, pop);
2695 return error ? error : nxm_stack_pop_check(pop, NULL);
2699 encode_STACK_op(const struct ofpact_stack *stack_action,
2700 struct nx_action_stack *nasp)
2705 nasp->offset = htons(stack_action->subfield.ofs);
2707 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2708 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2709 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2710 n_bits = htons(stack_action->subfield.n_bits);
2711 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2715 encode_STACK_PUSH(const struct ofpact_stack *stack,
2716 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2718 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2722 encode_STACK_POP(const struct ofpact_stack *stack,
2723 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2725 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2728 static char * OVS_WARN_UNUSED_RESULT
2729 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2730 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2732 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2735 static char * OVS_WARN_UNUSED_RESULT
2736 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2737 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2739 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2743 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2745 nxm_format_stack_push(a, s);
2749 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2751 nxm_format_stack_pop(a, s);
2754 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2756 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2757 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2758 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2759 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2761 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2762 * sent only to controllers with id 0.)
2764 struct nx_action_cnt_ids {
2765 ovs_be16 type; /* OFPAT_VENDOR. */
2766 ovs_be16 len; /* Length including slaves. */
2767 ovs_be32 vendor; /* NX_VENDOR_ID. */
2768 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2770 ovs_be16 n_controllers; /* Number of controllers. */
2771 uint8_t zeros[4]; /* Must be zero. */
2773 /* Followed by 1 or more controller ids.
2775 * uint16_t cnt_ids[]; // Controller ids.
2776 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2779 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2782 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2785 struct ofpact_cnt_ids *ids;
2786 enum ofperr error = 0;
2788 ids = ofpact_put_DEC_TTL(out);
2789 ids->n_controllers = 1;
2790 ofpbuf_put(out, &id, sizeof id);
2792 ofpact_update_len(out, &ids->ofpact);
2797 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2800 struct ofpact_cnt_ids *ids;
2804 ids = ofpact_put_DEC_TTL(out);
2805 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2806 ids->n_controllers = ntohs(nac_ids->n_controllers);
2807 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2809 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2810 return OFPERR_NXBRC_MUST_BE_ZERO;
2813 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2814 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2815 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2816 "are required for %"PRIu16" controllers.",
2817 ids_size, ids->n_controllers * sizeof(ovs_be16),
2818 ids->n_controllers);
2819 return OFPERR_OFPBAC_BAD_LEN;
2822 for (i = 0; i < ids->n_controllers; i++) {
2823 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2824 ofpbuf_put(out, &id, sizeof id);
2828 ofpact_update_len(out, &ids->ofpact);
2834 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2835 enum ofp_version ofp_version, struct ofpbuf *out)
2837 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2838 || dec_ttl->n_controllers != 1
2839 || dec_ttl->cnt_ids[0] != 0) {
2840 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2841 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2845 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2846 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2848 ids = ofpbuf_put_zeros(out, ids_len);
2849 for (i = 0; i < dec_ttl->n_controllers; i++) {
2850 ids[i] = htons(dec_ttl->cnt_ids[i]);
2853 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2858 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2860 struct ofpact_cnt_ids *ids;
2863 ofpact_put_DEC_TTL(ofpacts);
2864 ofpbuf_put(ofpacts, &id, sizeof id);
2865 ids = ofpacts->header;
2866 ids->n_controllers++;
2867 ofpact_update_len(ofpacts, &ids->ofpact);
2870 static char * OVS_WARN_UNUSED_RESULT
2871 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2872 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2875 parse_noargs_dec_ttl(ofpacts);
2877 struct ofpact_cnt_ids *ids;
2880 ids = ofpact_put_DEC_TTL(ofpacts);
2881 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2882 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2883 cntr = strtok_r(NULL, ", ", &arg)) {
2884 uint16_t id = atoi(cntr);
2886 ofpbuf_put(ofpacts, &id, sizeof id);
2887 ids = ofpacts->header;
2888 ids->n_controllers++;
2890 if (!ids->n_controllers) {
2891 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2894 ofpact_update_len(ofpacts, &ids->ofpact);
2900 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2904 ds_put_cstr(s, "dec_ttl");
2905 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2906 ds_put_cstr(s, "(");
2907 for (i = 0; i < a->n_controllers; i++) {
2909 ds_put_cstr(s, ",");
2911 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2913 ds_put_cstr(s, ")");
2917 /* Set MPLS label actions. */
2920 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label, struct ofpbuf *out)
2922 ofpact_put_SET_MPLS_LABEL(out)->label = label;
2927 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
2928 enum ofp_version ofp_version,
2931 if (ofp_version < OFP12_VERSION) {
2932 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
2934 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
2935 ntohl(label->label));
2939 static char * OVS_WARN_UNUSED_RESULT
2940 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
2941 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2943 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
2945 return xstrdup("set_mpls_label: expected label.");
2948 mpls_label->label = htonl(atoi(arg));
2953 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
2955 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
2958 /* Set MPLS TC actions. */
2961 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc, struct ofpbuf *out)
2963 ofpact_put_SET_MPLS_TC(out)->tc = tc;
2968 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
2969 enum ofp_version ofp_version, struct ofpbuf *out)
2971 if (ofp_version < OFP12_VERSION) {
2972 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
2974 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
2978 static char * OVS_WARN_UNUSED_RESULT
2979 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
2980 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2982 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
2985 return xstrdup("set_mpls_tc: expected tc.");
2988 mpls_tc->tc = atoi(arg);
2993 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
2995 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
2998 /* Set MPLS TTL actions. */
3001 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl, struct ofpbuf *out)
3003 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3008 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3009 enum ofp_version ofp_version, struct ofpbuf *out)
3011 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3014 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3015 * action to 'ofpacts'.
3017 * Returns NULL if successful, otherwise a malloc()'d string describing the
3018 * error. The caller is responsible for freeing the returned string. */
3019 static char * OVS_WARN_UNUSED_RESULT
3020 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3021 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3023 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3026 return xstrdup("set_mpls_ttl: expected ttl.");
3029 mpls_ttl->ttl = atoi(arg);
3034 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3036 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3039 /* Decrement MPLS TTL actions. */
3042 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3044 ofpact_put_DEC_MPLS_TTL(out);
3049 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3050 enum ofp_version ofp_version, struct ofpbuf *out)
3052 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3055 static char * OVS_WARN_UNUSED_RESULT
3056 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3057 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3059 ofpact_put_DEC_MPLS_TTL(ofpacts);
3064 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3066 ds_put_cstr(s, "dec_mpls_ttl");
3069 /* Push MPLS label action. */
3072 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
3074 struct ofpact_push_mpls *oam;
3076 if (!eth_type_mpls(ethertype)) {
3077 return OFPERR_OFPBAC_BAD_ARGUMENT;
3079 oam = ofpact_put_PUSH_MPLS(out);
3080 oam->ethertype = ethertype;
3086 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3087 enum ofp_version ofp_version, struct ofpbuf *out)
3089 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3092 static char * OVS_WARN_UNUSED_RESULT
3093 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3094 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3099 error = str_to_u16(arg, "push_mpls", ðertype);
3101 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3107 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3109 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3112 /* Pop MPLS label action. */
3115 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
3117 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3122 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3123 enum ofp_version ofp_version, struct ofpbuf *out)
3125 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3128 static char * OVS_WARN_UNUSED_RESULT
3129 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3130 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3135 error = str_to_u16(arg, "pop_mpls", ðertype);
3137 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3143 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3145 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3148 /* Set tunnel ID actions. */
3151 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id, struct ofpbuf *out)
3153 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3154 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3155 tunnel->tun_id = tun_id;
3160 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id, struct ofpbuf *out)
3162 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3163 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3164 tunnel->tun_id = tun_id;
3169 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3170 enum ofp_version ofp_version, struct ofpbuf *out)
3172 uint64_t tun_id = tunnel->tun_id;
3174 if (ofp_version < OFP12_VERSION) {
3175 if (tun_id <= UINT32_MAX
3176 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3177 put_NXAST_SET_TUNNEL(out, tun_id);
3179 put_NXAST_SET_TUNNEL64(out, tun_id);
3182 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3186 static char * OVS_WARN_UNUSED_RESULT
3187 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3188 enum ofp_raw_action_type raw)
3190 struct ofpact_tunnel *tunnel;
3192 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3193 tunnel->ofpact.raw = raw;
3194 return str_to_u64(arg, &tunnel->tun_id);
3197 static char * OVS_WARN_UNUSED_RESULT
3198 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3199 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3201 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3205 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3207 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3208 (a->tun_id > UINT32_MAX
3209 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3213 /* Set queue action. */
3216 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id, struct ofpbuf *out)
3218 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3223 encode_SET_QUEUE(const struct ofpact_queue *queue,
3224 enum ofp_version ofp_version, struct ofpbuf *out)
3226 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3229 static char * OVS_WARN_UNUSED_RESULT
3230 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3231 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3233 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3237 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3239 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3242 /* Pop queue action. */
3245 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3247 ofpact_put_POP_QUEUE(out);
3252 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3253 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3255 put_NXAST_POP_QUEUE(out);
3258 static char * OVS_WARN_UNUSED_RESULT
3259 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3260 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3262 ofpact_put_POP_QUEUE(ofpacts);
3267 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3269 ds_put_cstr(s, "pop_queue");
3272 /* Action structure for NXAST_FIN_TIMEOUT.
3274 * This action changes the idle timeout or hard timeout, or both, of this
3275 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3276 * When such a packet is observed, the action reduces the rule's idle timeout
3277 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3278 * action has no effect on an existing timeout that is already shorter than the
3279 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3280 * of zero has no effect on the respective timeout.
3282 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3283 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3284 * receipt of the FIN or RST.
3286 * This is useful for quickly discarding learned TCP flows that otherwise will
3287 * take a long time to expire.
3289 * This action is intended for use with an OpenFlow rule that matches only a
3290 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3291 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3292 * RST in any of those flows will cause the entire OpenFlow rule to expire
3293 * early, which is not normally desirable.
3295 struct nx_action_fin_timeout {
3296 ovs_be16 type; /* OFPAT_VENDOR. */
3297 ovs_be16 len; /* 16. */
3298 ovs_be32 vendor; /* NX_VENDOR_ID. */
3299 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3300 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3301 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3302 ovs_be16 pad; /* Must be zero. */
3304 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3307 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3310 struct ofpact_fin_timeout *oft;
3312 oft = ofpact_put_FIN_TIMEOUT(out);
3313 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3314 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3319 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3320 enum ofp_version ofp_version OVS_UNUSED,
3323 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3324 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3325 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3328 static char * OVS_WARN_UNUSED_RESULT
3329 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3330 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3332 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3335 while (ofputil_parse_key_value(&arg, &key, &value)) {
3338 if (!strcmp(key, "idle_timeout")) {
3339 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3340 } else if (!strcmp(key, "hard_timeout")) {
3341 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3343 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3355 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3357 ds_put_cstr(s, "fin_timeout(");
3358 if (a->fin_idle_timeout) {
3359 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3361 if (a->fin_hard_timeout) {
3362 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3365 ds_put_char(s, ')');
3368 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3370 * These actions search one of the switch's flow tables:
3372 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3373 * it specifies the table to search.
3375 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3376 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3377 * table, that is, the OpenFlow flow table that contains the flow from
3378 * which this action was obtained. If this action did not come from a
3379 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3380 * is the current table.
3382 * The flow table lookup uses a flow that may be slightly modified from the
3385 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3386 * is used as the flow's in_port.
3388 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3389 * then its value is used as the flow's in_port. Otherwise, the original
3392 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3393 * resubmit action, then the flow is updated with the new values.
3395 * Following the lookup, the original in_port is restored.
3397 * If the modified flow matched in the flow table, then the corresponding
3398 * actions are executed. Afterward, actions following the resubmit in the
3399 * original set of actions, if any, are executed; any changes made to the
3400 * packet (e.g. changes to VLAN) by secondary actions persist when those
3401 * actions are executed, although the original in_port is restored.
3403 * Resubmit actions may be used any number of times within a set of actions.
3405 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3406 * implementation-defined depth, further resubmit actions are simply ignored.
3408 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3409 * 'pad' to be all-bits-zero.
3411 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3412 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3414 struct nx_action_resubmit {
3415 ovs_be16 type; /* OFPAT_VENDOR. */
3416 ovs_be16 len; /* Length is 16. */
3417 ovs_be32 vendor; /* NX_VENDOR_ID. */
3418 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3419 ovs_be16 in_port; /* New in_port for checking flow table. */
3420 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3423 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3426 decode_NXAST_RAW_RESUBMIT(uint16_t port, struct ofpbuf *out)
3428 struct ofpact_resubmit *resubmit;
3430 resubmit = ofpact_put_RESUBMIT(out);
3431 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3432 resubmit->in_port = u16_to_ofp(port);
3433 resubmit->table_id = 0xff;
3438 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3441 struct ofpact_resubmit *resubmit;
3443 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3444 return OFPERR_OFPBAC_BAD_ARGUMENT;
3447 resubmit = ofpact_put_RESUBMIT(out);
3448 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3449 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3450 resubmit->table_id = nar->table;
3455 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3456 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3458 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3460 if (resubmit->table_id == 0xff
3461 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3462 put_NXAST_RESUBMIT(out, in_port);
3464 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3465 nar->table = resubmit->table_id;
3466 nar->in_port = htons(in_port);
3470 static char * OVS_WARN_UNUSED_RESULT
3471 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3472 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3474 struct ofpact_resubmit *resubmit;
3475 char *in_port_s, *table_s;
3477 resubmit = ofpact_put_RESUBMIT(ofpacts);
3479 in_port_s = strsep(&arg, ",");
3480 if (in_port_s && in_port_s[0]) {
3481 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3482 return xasprintf("%s: resubmit to unknown port", in_port_s);
3485 resubmit->in_port = OFPP_IN_PORT;
3488 table_s = strsep(&arg, ",");
3489 if (table_s && table_s[0]) {
3490 uint32_t table_id = 0;
3493 error = str_to_u32(table_s, &table_id);
3497 resubmit->table_id = table_id;
3499 resubmit->table_id = 255;
3502 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3503 return xstrdup("at least one \"in_port\" or \"table\" must be "
3504 "specified on resubmit");
3510 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3512 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3513 ds_put_cstr(s, "resubmit:");
3514 ofputil_format_port(a->in_port, s);
3516 ds_put_format(s, "resubmit(");
3517 if (a->in_port != OFPP_IN_PORT) {
3518 ofputil_format_port(a->in_port, s);
3520 ds_put_char(s, ',');
3521 if (a->table_id != 255) {
3522 ds_put_format(s, "%"PRIu8, a->table_id);
3524 ds_put_char(s, ')');
3528 /* Action structure for NXAST_LEARN.
3530 * This action adds or modifies a flow in an OpenFlow table, similar to
3531 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3532 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3533 * flow's match criteria and actions are built by applying each of the series
3534 * of flow_mod_spec elements included as part of the action.
3536 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3537 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3538 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3539 * bits from a source to a destination. In this case, the header contains
3542 * 15 14 13 12 11 10 0
3543 * +------+---+------+---------------------------------+
3544 * | 0 |src| dst | n_bits |
3545 * +------+---+------+---------------------------------+
3547 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3548 * following table summarizes the meaning of each possible combination.
3549 * Details follow the table:
3552 * --- --- ----------------------------------------------------------
3553 * 0 0 Add match criteria based on value in a field.
3554 * 1 0 Add match criteria based on an immediate value.
3555 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3556 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3557 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3558 * All other combinations are undefined and not allowed.
3560 * The flow_mod_spec header is followed by a source specification and a
3561 * destination specification. The format and meaning of the source
3562 * specification depends on 'src':
3564 * - If 'src' is 0, the source bits are taken from a field in the flow to
3565 * which this action is attached. (This should be a wildcarded field. If
3566 * its value is fully specified then the source bits being copied have
3569 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3570 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3571 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3572 * 'field' and 'ofs' are subject to the same restrictions as the source
3573 * field in NXAST_REG_MOVE.
3575 * - If 'src' is 1, the source bits are a constant value. The source
3576 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3577 * number in network order, the source bits are the 'n_bits'
3578 * least-significant bits. The switch will report an error if other bits
3579 * in the constant are nonzero.
3581 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3582 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3583 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3584 * meaning of the flow_mod_spec depends on 'dst':
3586 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3587 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3588 * packet equal the source bits. 'field' may be any nxm_header with
3589 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3591 * Order is significant. Earlier flow_mod_specs must satisfy any
3592 * prerequisites for matching fields specified later, by copying constant
3593 * values into prerequisite fields.
3595 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3598 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3599 * the new flow. The new flow copies the source bits into
3600 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3603 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3604 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3606 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3607 * empty. It has the following meaning:
3609 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3610 * The new flow outputs to the OpenFlow port specified by the source field.
3611 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3612 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3615 * Resource Management
3616 * -------------------
3618 * A switch has a finite amount of flow table space available for learning.
3619 * When this space is exhausted, no new learning table entries will be learned
3620 * until some existing flow table entries expire. The controller should be
3621 * prepared to handle this by flooding (which can be implemented as a
3622 * low-priority flow).
3624 * If a learned flow matches a single TCP stream with a relatively long
3625 * timeout, one may make the best of resource constraints by setting
3626 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3627 * both, to shorter timeouts. When either of these is specified as a nonzero
3628 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3634 * The following examples give a prose description of the flow_mod_specs along
3635 * with informal notation for how those would be represented and a hex dump of
3636 * the bytes that would be required.
3638 * These examples could work with various nx_action_learn parameters. Typical
3639 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3640 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3642 * 1. Learn input port based on the source MAC, with lookup into
3643 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3645 * Match on in_port=99:
3646 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3647 * ovs_be16(99), 00 63
3648 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3650 * Match Ethernet destination on Ethernet source from packet:
3651 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3652 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3653 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3655 * Set NXM_NX_REG1[16:31] to the packet's input port:
3656 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3657 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3658 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3660 * Given a packet that arrived on port A with Ethernet source address B,
3661 * this would set up the flow "in_port=99, dl_dst=B,
3662 * actions=load:A->NXM_NX_REG1[16..31]".
3664 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3665 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3666 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3668 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3669 * into NXM_NX_REG1[16:31]:
3671 * Match on same VLAN ID as packet:
3672 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3673 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3674 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3676 * Match Ethernet destination on Ethernet source from packet:
3677 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3678 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3679 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3681 * Output to the packet's input port:
3682 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3683 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3685 * Given a packet that arrived on port A with Ethernet source address B in
3686 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3687 * actions=output:A".
3689 * In syntax accepted by ovs-ofctl, this action is:
3690 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3691 * output:NXM_OF_IN_PORT[])
3693 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3694 * 10-second MAC expiration time to make it easier to see what's going on
3696 * ovs-vsctl del-controller br0
3697 * ovs-ofctl del-flows br0
3698 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3699 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3700 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3701 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3702 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3704 * You can then dump the MAC learning table with:
3706 * ovs-ofctl dump-flows br0 table=1
3711 * For best performance, segregate learned flows into a table that is not used
3712 * for any other flows except possibly for a lowest-priority "catch-all" flow
3713 * (a flow with no match criteria). If different learning actions specify
3714 * different match criteria, use different tables for the learned flows.
3716 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3717 * These timeouts apply to the flow that is added, which means that a flow with
3718 * an idle timeout will expire when no traffic has been sent *to* the learned
3719 * address. This is not usually the intent in MAC learning; instead, we want
3720 * the MAC learn entry to expire when no traffic has been sent *from* the
3721 * learned address. Use a hard timeout for that.
3724 * Visibility of Changes
3725 * ---------------------
3727 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3728 * flow translation are visible to flow table lookups made later in the flow
3729 * translation. This means that, in the example above, a MAC learned by the
3730 * learn action in table 0 would be found in table 1 (if the packet being
3731 * processed had the same source and destination MAC address).
3733 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3734 * modify a flow) by a "learn" action are visible only for later flow
3735 * translations, not for later lookups within the same flow translation. In
3736 * the MAC learning example, a MAC learned by the learn action in table 0 would
3737 * not be found in table 1 if the flow translation would resubmit to table 1
3738 * after the processing of the learn action, meaning that if this MAC had not
3739 * been learned before then the packet would be flooded. */
3740 struct nx_action_learn {
3741 ovs_be16 type; /* OFPAT_VENDOR. */
3742 ovs_be16 len; /* At least 24. */
3743 ovs_be32 vendor; /* NX_VENDOR_ID. */
3744 ovs_be16 subtype; /* NXAST_LEARN. */
3745 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3746 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3747 ovs_be16 priority; /* Priority level of flow entry. */
3748 ovs_be64 cookie; /* Cookie for new flow. */
3749 ovs_be16 flags; /* NX_LEARN_F_*. */
3750 uint8_t table_id; /* Table to insert flow entry. */
3751 uint8_t pad; /* Must be zero. */
3752 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3753 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3754 /* Followed by a sequence of flow_mod_spec elements, as described above,
3755 * until the end of the action is reached. */
3757 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3760 get_be16(const void **pp)
3762 const ovs_be16 *p = *pp;
3763 ovs_be16 value = *p;
3769 get_be32(const void **pp)
3771 const ovs_be32 *p = *pp;
3772 ovs_be32 value = get_unaligned_be32(p);
3778 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3780 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3781 sf->ofs = ntohs(get_be16(p));
3782 sf->n_bits = n_bits;
3786 learn_min_len(uint16_t header)
3788 int n_bits = header & NX_LEARN_N_BITS_MASK;
3789 int src_type = header & NX_LEARN_SRC_MASK;
3790 int dst_type = header & NX_LEARN_DST_MASK;
3791 unsigned int min_len;
3794 if (src_type == NX_LEARN_SRC_FIELD) {
3795 min_len += sizeof(ovs_be32); /* src_field */
3796 min_len += sizeof(ovs_be16); /* src_ofs */
3798 min_len += DIV_ROUND_UP(n_bits, 16);
3800 if (dst_type == NX_LEARN_DST_MATCH ||
3801 dst_type == NX_LEARN_DST_LOAD) {
3802 min_len += sizeof(ovs_be32); /* dst_field */
3803 min_len += sizeof(ovs_be16); /* dst_ofs */
3808 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3809 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3811 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3812 struct ofpbuf *ofpacts)
3814 struct ofpact_learn *learn;
3815 const void *p, *end;
3818 return OFPERR_OFPBAC_BAD_ARGUMENT;
3821 learn = ofpact_put_LEARN(ofpacts);
3823 learn->idle_timeout = ntohs(nal->idle_timeout);
3824 learn->hard_timeout = ntohs(nal->hard_timeout);
3825 learn->priority = ntohs(nal->priority);
3826 learn->cookie = nal->cookie;
3827 learn->table_id = nal->table_id;
3828 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3829 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3831 learn->flags = ntohs(nal->flags);
3832 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3833 NX_LEARN_F_DELETE_LEARNED)) {
3834 return OFPERR_OFPBAC_BAD_ARGUMENT;
3837 if (learn->table_id == 0xff) {
3838 return OFPERR_OFPBAC_BAD_ARGUMENT;
3841 end = (char *) nal + ntohs(nal->len);
3842 for (p = nal + 1; p != end; ) {
3843 struct ofpact_learn_spec *spec;
3844 uint16_t header = ntohs(get_be16(&p));
3850 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3851 learn = ofpacts->header;
3854 spec->src_type = header & NX_LEARN_SRC_MASK;
3855 spec->dst_type = header & NX_LEARN_DST_MASK;
3856 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3858 /* Check for valid src and dst type combination. */
3859 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3860 spec->dst_type == NX_LEARN_DST_LOAD ||
3861 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3862 spec->src_type == NX_LEARN_SRC_FIELD)) {
3865 return OFPERR_OFPBAC_BAD_ARGUMENT;
3868 /* Check that the arguments don't overrun the end of the action. */
3869 if ((char *) end - (char *) p < learn_min_len(header)) {
3870 return OFPERR_OFPBAC_BAD_LEN;
3873 /* Get the source. */
3874 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3875 get_subfield(spec->n_bits, &p, &spec->src);
3877 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3879 bitwise_copy(p, p_bytes, 0,
3880 &spec->src_imm, sizeof spec->src_imm, 0,
3882 p = (const uint8_t *) p + p_bytes;
3885 /* Get the destination. */
3886 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3887 spec->dst_type == NX_LEARN_DST_LOAD) {
3888 get_subfield(spec->n_bits, &p, &spec->dst);
3891 ofpact_update_len(ofpacts, &learn->ofpact);
3893 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3894 return OFPERR_OFPBAC_BAD_ARGUMENT;
3901 put_be16(struct ofpbuf *b, ovs_be16 x)
3903 ofpbuf_put(b, &x, sizeof x);
3907 put_be32(struct ofpbuf *b, ovs_be32 x)
3909 ofpbuf_put(b, &x, sizeof x);
3913 put_u16(struct ofpbuf *b, uint16_t x)
3915 put_be16(b, htons(x));
3919 put_u32(struct ofpbuf *b, uint32_t x)
3921 put_be32(b, htonl(x));
3925 encode_LEARN(const struct ofpact_learn *learn,
3926 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3928 const struct ofpact_learn_spec *spec;
3929 struct nx_action_learn *nal;
3932 start_ofs = out->size;
3933 nal = put_NXAST_LEARN(out);
3934 nal->idle_timeout = htons(learn->idle_timeout);
3935 nal->hard_timeout = htons(learn->hard_timeout);
3936 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
3937 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
3938 nal->priority = htons(learn->priority);
3939 nal->cookie = learn->cookie;
3940 nal->flags = htons(learn->flags);
3941 nal->table_id = learn->table_id;
3943 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
3944 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
3946 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3947 put_u32(out, mf_nxm_header(spec->src.field->id));
3948 put_u16(out, spec->src.ofs);
3950 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3951 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
3952 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
3953 bits, n_dst_bytes, 0,
3957 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3958 spec->dst_type == NX_LEARN_DST_LOAD) {
3959 put_u32(out, mf_nxm_header(spec->dst.field->id));
3960 put_u16(out, spec->dst.ofs);
3964 pad_ofpat(out, start_ofs);
3967 static char * OVS_WARN_UNUSED_RESULT
3968 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
3969 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3971 return learn_parse(arg, ofpacts);
3975 format_LEARN(const struct ofpact_learn *a, struct ds *s)
3980 /* Action structure for NXAST_CONJUNCTION. */
3981 struct nx_action_conjunction {
3982 ovs_be16 type; /* OFPAT_VENDOR. */
3983 ovs_be16 len; /* At least 16. */
3984 ovs_be32 vendor; /* NX_VENDOR_ID. */
3985 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
3990 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
3993 add_conjunction(struct ofpbuf *out,
3994 uint32_t id, uint8_t clause, uint8_t n_clauses)
3996 struct ofpact_conjunction *oc;
3998 oc = ofpact_put_CONJUNCTION(out);
4000 oc->clause = clause;
4001 oc->n_clauses = n_clauses;
4005 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4008 if (nac->n_clauses < 2 || nac->n_clauses > 64
4009 || nac->clause >= nac->n_clauses) {
4010 return OFPERR_NXBAC_BAD_CONJUNCTION;
4012 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4018 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4019 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4021 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4022 nac->clause = oc->clause;
4023 nac->n_clauses = oc->n_clauses;
4024 nac->id = htonl(oc->id);
4028 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4030 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4031 oc->id, oc->clause + 1, oc->n_clauses);
4034 static char * OVS_WARN_UNUSED_RESULT
4035 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4036 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4043 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4044 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4045 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4048 if (n_clauses < 2) {
4049 return xstrdup("conjunction must have at least 2 clauses");
4050 } else if (n_clauses > 64) {
4051 return xstrdup("conjunction must have at most 64 clauses");
4052 } else if (clause < 1) {
4053 return xstrdup("clause index must be positive");
4054 } else if (clause > n_clauses) {
4055 return xstrdup("clause index must be less than or equal to "
4056 "number of clauses");
4059 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4063 /* Action structure for NXAST_MULTIPATH.
4065 * This action performs the following steps in sequence:
4067 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4068 * Refer to the definition of "enum nx_mp_fields" for details.
4070 * The 'basis' value is used as a universal hash parameter, that is,
4071 * different values of 'basis' yield different hash functions. The
4072 * particular universal hash function used is implementation-defined.
4074 * The hashed fields' values are drawn from the current state of the
4075 * flow, including all modifications that have been made by actions up to
4078 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4079 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4082 * The output of the algorithm is 'link', an unsigned integer less than
4083 * or equal to 'max_link'.
4085 * Some algorithms use 'arg' as an additional argument.
4087 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4088 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4091 * The switch will reject actions that have an unknown 'fields', or an unknown
4092 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4093 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4094 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4096 struct nx_action_multipath {
4097 ovs_be16 type; /* OFPAT_VENDOR. */
4098 ovs_be16 len; /* Length is 32. */
4099 ovs_be32 vendor; /* NX_VENDOR_ID. */
4100 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4102 /* What fields to hash and how. */
4103 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4104 ovs_be16 basis; /* Universal hash parameter. */
4107 /* Multipath link choice algorithm to apply to hash value. */
4108 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4109 ovs_be16 max_link; /* Number of output links, minus 1. */
4110 ovs_be32 arg; /* Algorithm-specific argument. */
4113 /* Where to store the result. */
4114 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4115 ovs_be32 dst; /* Destination. */
4117 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4120 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4123 uint32_t n_links = ntohs(nam->max_link) + 1;
4124 size_t min_n_bits = log_2_ceil(n_links);
4125 struct ofpact_multipath *mp;
4127 mp = ofpact_put_MULTIPATH(out);
4128 mp->fields = ntohs(nam->fields);
4129 mp->basis = ntohs(nam->basis);
4130 mp->algorithm = ntohs(nam->algorithm);
4131 mp->max_link = ntohs(nam->max_link);
4132 mp->arg = ntohl(nam->arg);
4133 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4134 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4135 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4137 if (!flow_hash_fields_valid(mp->fields)) {
4138 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4139 return OFPERR_OFPBAC_BAD_ARGUMENT;
4140 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4141 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4142 && mp->algorithm != NX_MP_ALG_HRW
4143 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4144 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4145 return OFPERR_OFPBAC_BAD_ARGUMENT;
4146 } else if (mp->dst.n_bits < min_n_bits) {
4147 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4148 "%"PRIu32" links", min_n_bits, n_links);
4149 return OFPERR_OFPBAC_BAD_ARGUMENT;
4152 return multipath_check(mp, NULL);
4156 encode_MULTIPATH(const struct ofpact_multipath *mp,
4157 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4159 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4161 nam->fields = htons(mp->fields);
4162 nam->basis = htons(mp->basis);
4163 nam->algorithm = htons(mp->algorithm);
4164 nam->max_link = htons(mp->max_link);
4165 nam->arg = htonl(mp->arg);
4166 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4167 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4170 static char * OVS_WARN_UNUSED_RESULT
4171 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4172 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4174 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4178 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4180 multipath_format(a, s);
4183 /* Action structure for NXAST_NOTE.
4185 * This action has no effect. It is variable length. The switch does not
4186 * attempt to interpret the user-defined 'note' data in any way. A controller
4187 * can use this action to attach arbitrary metadata to a flow.
4189 * This action might go away in the future.
4191 struct nx_action_note {
4192 ovs_be16 type; /* OFPAT_VENDOR. */
4193 ovs_be16 len; /* A multiple of 8, but at least 16. */
4194 ovs_be32 vendor; /* NX_VENDOR_ID. */
4195 ovs_be16 subtype; /* NXAST_NOTE. */
4196 uint8_t note[6]; /* Start of user-defined data. */
4197 /* Possibly followed by additional user-defined data. */
4199 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4202 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan, struct ofpbuf *out)
4204 struct ofpact_note *note;
4205 unsigned int length;
4207 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4208 note = ofpact_put(out, OFPACT_NOTE,
4209 offsetof(struct ofpact_note, data) + length);
4210 note->length = length;
4211 memcpy(note->data, nan->note, length);
4217 encode_NOTE(const struct ofpact_note *note,
4218 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4220 size_t start_ofs = out->size;
4221 struct nx_action_note *nan;
4222 unsigned int remainder;
4225 put_NXAST_NOTE(out);
4226 out->size = out->size - sizeof nan->note;
4228 ofpbuf_put(out, note->data, note->length);
4230 len = out->size - start_ofs;
4231 remainder = len % OFP_ACTION_ALIGN;
4233 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4235 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4236 nan->len = htons(out->size - start_ofs);
4239 static char * OVS_WARN_UNUSED_RESULT
4240 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4241 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4243 struct ofpact_note *note;
4245 note = ofpact_put_NOTE(ofpacts);
4246 while (*arg != '\0') {
4257 byte = hexits_value(arg, 2, &ok);
4259 return xstrdup("bad hex digit in `note' argument");
4261 ofpbuf_put(ofpacts, &byte, 1);
4263 note = ofpacts->header;
4268 ofpact_update_len(ofpacts, ¬e->ofpact);
4273 format_NOTE(const struct ofpact_note *a, struct ds *s)
4277 ds_put_cstr(s, "note:");
4278 for (i = 0; i < a->length; i++) {
4280 ds_put_char(s, '.');
4282 ds_put_format(s, "%02"PRIx8, a->data[i]);
4289 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4291 ofpact_put_EXIT(out);
4296 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4297 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4299 put_NXAST_EXIT(out);
4302 static char * OVS_WARN_UNUSED_RESULT
4303 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4304 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4306 ofpact_put_EXIT(ofpacts);
4311 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4313 ds_put_cstr(s, "exit");
4316 /* Unroll xlate action. */
4319 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4320 enum ofp_version ofp_version OVS_UNUSED,
4321 struct ofpbuf *out OVS_UNUSED)
4326 static char * OVS_WARN_UNUSED_RESULT
4327 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4328 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4335 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4338 ds_put_cstr(s, "unroll_xlate");
4341 /* Action structure for NXAST_SAMPLE.
4343 * Samples matching packets with the given probability and sends them
4344 * each to the set of collectors identified with the given ID. The
4345 * probability is expressed as a number of packets to be sampled out
4346 * of USHRT_MAX packets, and must be >0.
4348 * When sending packet samples to IPFIX collectors, the IPFIX flow
4349 * record sent for each sampled packet is associated with the given
4350 * observation domain ID and observation point ID. Each IPFIX flow
4351 * record contain the sampled packet's headers when executing this
4352 * rule. If a sampled packet's headers are modified by previous
4353 * actions in the flow, those modified headers are sent. */
4354 struct nx_action_sample {
4355 ovs_be16 type; /* OFPAT_VENDOR. */
4356 ovs_be16 len; /* Length is 24. */
4357 ovs_be32 vendor; /* NX_VENDOR_ID. */
4358 ovs_be16 subtype; /* NXAST_SAMPLE. */
4359 ovs_be16 probability; /* Fraction of packets to sample. */
4360 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4361 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4362 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4364 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4367 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas, struct ofpbuf *out)
4369 struct ofpact_sample *sample;
4371 sample = ofpact_put_SAMPLE(out);
4372 sample->probability = ntohs(nas->probability);
4373 sample->collector_set_id = ntohl(nas->collector_set_id);
4374 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4375 sample->obs_point_id = ntohl(nas->obs_point_id);
4377 if (sample->probability == 0) {
4378 return OFPERR_OFPBAC_BAD_ARGUMENT;
4385 encode_SAMPLE(const struct ofpact_sample *sample,
4386 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4388 struct nx_action_sample *nas;
4390 nas = put_NXAST_SAMPLE(out);
4391 nas->probability = htons(sample->probability);
4392 nas->collector_set_id = htonl(sample->collector_set_id);
4393 nas->obs_domain_id = htonl(sample->obs_domain_id);
4394 nas->obs_point_id = htonl(sample->obs_point_id);
4397 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4398 * action to 'ofpacts'.
4400 * Returns NULL if successful, otherwise a malloc()'d string describing the
4401 * error. The caller is responsible for freeing the returned string. */
4402 static char * OVS_WARN_UNUSED_RESULT
4403 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4404 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4406 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4409 while (ofputil_parse_key_value(&arg, &key, &value)) {
4412 if (!strcmp(key, "probability")) {
4413 error = str_to_u16(value, "probability", &os->probability);
4414 if (!error && os->probability == 0) {
4415 error = xasprintf("invalid probability value \"%s\"", value);
4417 } else if (!strcmp(key, "collector_set_id")) {
4418 error = str_to_u32(value, &os->collector_set_id);
4419 } else if (!strcmp(key, "obs_domain_id")) {
4420 error = str_to_u32(value, &os->obs_domain_id);
4421 } else if (!strcmp(key, "obs_point_id")) {
4422 error = str_to_u32(value, &os->obs_point_id);
4424 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4431 if (os->probability == 0) {
4432 return xstrdup("non-zero \"probability\" must be specified on sample");
4438 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4440 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4441 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4442 a->probability, a->collector_set_id,
4443 a->obs_domain_id, a->obs_point_id);
4446 /* debug_recirc instruction. */
4448 static bool enable_debug;
4451 ofpact_dummy_enable(void)
4453 enable_debug = true;
4457 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4459 if (!enable_debug) {
4460 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4463 ofpact_put_DEBUG_RECIRC(out);
4468 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4469 enum ofp_version ofp_version OVS_UNUSED,
4472 put_NXAST_DEBUG_RECIRC(out);
4475 static char * OVS_WARN_UNUSED_RESULT
4476 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4477 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4479 ofpact_put_DEBUG_RECIRC(ofpacts);
4484 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4486 ds_put_cstr(s, "debug_recirc");
4489 /* Meter instruction. */
4492 encode_METER(const struct ofpact_meter *meter,
4493 enum ofp_version ofp_version, struct ofpbuf *out)
4495 if (ofp_version >= OFP13_VERSION) {
4496 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4500 static char * OVS_WARN_UNUSED_RESULT
4501 parse_METER(char *arg, struct ofpbuf *ofpacts,
4502 enum ofputil_protocol *usable_protocols)
4504 *usable_protocols &= OFPUTIL_P_OF13_UP;
4505 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
4509 format_METER(const struct ofpact_meter *a, struct ds *s)
4511 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
4514 /* Clear-Actions instruction. */
4517 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
4518 enum ofp_version ofp_version OVS_UNUSED,
4519 struct ofpbuf *out OVS_UNUSED)
4521 if (ofp_version > OFP10_VERSION) {
4522 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
4526 static char * OVS_WARN_UNUSED_RESULT
4527 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4528 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4530 ofpact_put_CLEAR_ACTIONS(ofpacts);
4535 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4537 ds_put_cstr(s, "clear_actions");
4540 /* Write-Actions instruction. */
4543 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
4544 enum ofp_version ofp_version, struct ofpbuf *out)
4546 if (ofp_version > OFP10_VERSION) {
4547 const size_t ofs = out->size;
4549 instruction_put_OFPIT11_WRITE_ACTIONS(out);
4550 ofpacts_put_openflow_actions(actions->actions,
4551 ofpact_nest_get_action_len(actions),
4553 ofpacts_update_instruction_actions(out, ofs);
4557 static char * OVS_WARN_UNUSED_RESULT
4558 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
4559 enum ofputil_protocol *usable_protocols)
4561 size_t ofs = ofpacts_pull(ofpacts);
4562 struct ofpact_nest *on;
4565 /* Add a Write-Actions instruction and then pull it off. */
4566 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
4567 ofpbuf_pull(ofpacts, sizeof *on);
4569 /* Parse nested actions.
4571 * We pulled off "write-actions" and the previous actions because the
4572 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4573 * that it doesn't actually include the nested actions. That means that
4574 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4575 * follows a Write-Actions, which is an invalid order. */
4576 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
4577 OFPACT_WRITE_ACTIONS);
4579 /* Put the Write-Actions back on and update its length. */
4580 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
4581 on->ofpact.len = ofpacts->size;
4583 /* Put any previous actions or instructions back on. */
4584 ofpbuf_push_uninit(ofpacts, ofs);
4590 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
4592 ds_put_cstr(s, "write_actions(");
4593 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
4594 ds_put_char(s, ')');
4597 /* Action structure for NXAST_WRITE_METADATA.
4599 * Modifies the 'mask' bits of the metadata value. */
4600 struct nx_action_write_metadata {
4601 ovs_be16 type; /* OFPAT_VENDOR. */
4602 ovs_be16 len; /* Length is 32. */
4603 ovs_be32 vendor; /* NX_VENDOR_ID. */
4604 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
4605 uint8_t zeros[6]; /* Must be zero. */
4606 ovs_be64 metadata; /* Metadata register. */
4607 ovs_be64 mask; /* Metadata mask. */
4609 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
4612 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
4615 struct ofpact_metadata *om;
4617 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
4618 return OFPERR_NXBRC_MUST_BE_ZERO;
4621 om = ofpact_put_WRITE_METADATA(out);
4622 om->metadata = nawm->metadata;
4623 om->mask = nawm->mask;
4629 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
4630 enum ofp_version ofp_version, struct ofpbuf *out)
4632 if (ofp_version == OFP10_VERSION) {
4633 struct nx_action_write_metadata *nawm;
4635 nawm = put_NXAST_WRITE_METADATA(out);
4636 nawm->metadata = metadata->metadata;
4637 nawm->mask = metadata->mask;
4639 struct ofp11_instruction_write_metadata *oiwm;
4641 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
4642 oiwm->metadata = metadata->metadata;
4643 oiwm->metadata_mask = metadata->mask;
4647 static char * OVS_WARN_UNUSED_RESULT
4648 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
4649 enum ofputil_protocol *usable_protocols)
4651 struct ofpact_metadata *om;
4652 char *mask = strchr(arg, '/');
4654 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
4656 om = ofpact_put_WRITE_METADATA(ofpacts);
4661 error = str_to_be64(mask + 1, &om->mask);
4666 om->mask = OVS_BE64_MAX;
4669 return str_to_be64(arg, &om->metadata);
4673 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
4675 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
4676 if (a->mask != OVS_BE64_MAX) {
4677 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
4681 /* Goto-Table instruction. */
4684 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
4685 enum ofp_version ofp_version, struct ofpbuf *out)
4687 if (ofp_version == OFP10_VERSION) {
4688 struct nx_action_resubmit *nar;
4690 nar = put_NXAST_RESUBMIT_TABLE(out);
4691 nar->table = goto_table->table_id;
4692 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
4694 struct ofp11_instruction_goto_table *oigt;
4696 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
4697 oigt->table_id = goto_table->table_id;
4698 memset(oigt->pad, 0, sizeof oigt->pad);
4702 static char * OVS_WARN_UNUSED_RESULT
4703 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
4704 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4706 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
4707 char *table_s = strsep(&arg, ",");
4708 if (!table_s || !table_s[0]) {
4709 return xstrdup("instruction goto-table needs table id");
4711 return str_to_u8(table_s, "table", &ogt->table_id);
4715 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
4717 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
4721 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
4722 const struct ofp_action_header *bad_action, enum ofperr error)
4724 if (!VLOG_DROP_WARN(&rl)) {
4728 ds_put_hex_dump(&s, actions, actions_len, 0, false);
4729 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
4730 (char *)bad_action - (char *)actions,
4731 ofperr_get_name(error), ds_cstr(&s));
4737 ofpacts_decode(const void *actions, size_t actions_len,
4738 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
4740 struct ofpbuf openflow;
4742 ofpbuf_use_const(&openflow, actions, actions_len);
4743 while (openflow.size) {
4744 const struct ofp_action_header *action = openflow.data;
4745 enum ofp_raw_action_type raw;
4749 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
4751 error = ofpact_decode(action, raw, arg, ofpacts);
4755 log_bad_action(actions, actions_len, action, error);
4760 ofpact_pad(ofpacts);
4765 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
4766 unsigned int actions_len,
4767 enum ofp_version version,
4768 uint32_t allowed_ovsinsts,
4769 struct ofpbuf *ofpacts,
4770 enum ofpact_type outer_action)
4772 const struct ofp_action_header *actions;
4775 if (!outer_action) {
4776 ofpbuf_clear(ofpacts);
4779 if (actions_len % OFP_ACTION_ALIGN != 0) {
4780 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
4781 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
4782 return OFPERR_OFPBRC_BAD_LEN;
4785 actions = ofpbuf_try_pull(openflow, actions_len);
4786 if (actions == NULL) {
4787 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
4788 "remaining message length (%"PRIu32")",
4789 actions_len, openflow->size);
4790 return OFPERR_OFPBRC_BAD_LEN;
4793 error = ofpacts_decode(actions, actions_len, version, ofpacts);
4795 ofpbuf_clear(ofpacts);
4799 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
4802 ofpbuf_clear(ofpacts);
4807 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
4808 * front of 'openflow' into ofpacts. On success, replaces any existing content
4809 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
4810 * Returns 0 if successful, otherwise an OpenFlow error.
4812 * Actions are processed according to their OpenFlow version which
4813 * is provided in the 'version' parameter.
4815 * In most places in OpenFlow, actions appear encapsulated in instructions, so
4816 * you should call ofpacts_pull_openflow_instructions() instead of this
4819 * The parsed actions are valid generically, but they may not be valid in a
4820 * specific context. For example, port numbers up to OFPP_MAX are valid
4821 * generically, but specific datapaths may only support port numbers in a
4822 * smaller range. Use ofpacts_check() to additional check whether actions are
4823 * valid in a specific context. */
4825 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
4826 unsigned int actions_len,
4827 enum ofp_version version,
4828 struct ofpbuf *ofpacts)
4830 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
4831 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4835 /* OpenFlow 1.1 actions. */
4838 /* True if an action sets the value of a field
4839 * in a way that is compatibile with the action set.
4840 * The field can be set via either a set or a move action.
4841 * False otherwise. */
4843 ofpact_is_set_or_move_action(const struct ofpact *a)
4846 case OFPACT_SET_FIELD:
4847 case OFPACT_REG_MOVE:
4848 case OFPACT_SET_ETH_DST:
4849 case OFPACT_SET_ETH_SRC:
4850 case OFPACT_SET_IP_DSCP:
4851 case OFPACT_SET_IP_ECN:
4852 case OFPACT_SET_IP_TTL:
4853 case OFPACT_SET_IPV4_DST:
4854 case OFPACT_SET_IPV4_SRC:
4855 case OFPACT_SET_L4_DST_PORT:
4856 case OFPACT_SET_L4_SRC_PORT:
4857 case OFPACT_SET_MPLS_LABEL:
4858 case OFPACT_SET_MPLS_TC:
4859 case OFPACT_SET_MPLS_TTL:
4860 case OFPACT_SET_QUEUE:
4861 case OFPACT_SET_TUNNEL:
4862 case OFPACT_SET_VLAN_PCP:
4863 case OFPACT_SET_VLAN_VID:
4866 case OFPACT_CLEAR_ACTIONS:
4867 case OFPACT_CONTROLLER:
4868 case OFPACT_DEC_MPLS_TTL:
4869 case OFPACT_DEC_TTL:
4870 case OFPACT_ENQUEUE:
4872 case OFPACT_UNROLL_XLATE:
4873 case OFPACT_FIN_TIMEOUT:
4874 case OFPACT_GOTO_TABLE:
4877 case OFPACT_CONJUNCTION:
4879 case OFPACT_MULTIPATH:
4882 case OFPACT_OUTPUT_REG:
4883 case OFPACT_POP_MPLS:
4884 case OFPACT_POP_QUEUE:
4885 case OFPACT_PUSH_MPLS:
4886 case OFPACT_PUSH_VLAN:
4887 case OFPACT_RESUBMIT:
4889 case OFPACT_STACK_POP:
4890 case OFPACT_STACK_PUSH:
4891 case OFPACT_STRIP_VLAN:
4892 case OFPACT_WRITE_ACTIONS:
4893 case OFPACT_WRITE_METADATA:
4894 case OFPACT_DEBUG_RECIRC:
4901 /* True if an action is allowed in the action set.
4902 * False otherwise. */
4904 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
4907 case OFPACT_DEC_MPLS_TTL:
4908 case OFPACT_DEC_TTL:
4911 case OFPACT_POP_MPLS:
4912 case OFPACT_PUSH_MPLS:
4913 case OFPACT_PUSH_VLAN:
4914 case OFPACT_REG_MOVE:
4915 case OFPACT_SET_FIELD:
4916 case OFPACT_SET_ETH_DST:
4917 case OFPACT_SET_ETH_SRC:
4918 case OFPACT_SET_IP_DSCP:
4919 case OFPACT_SET_IP_ECN:
4920 case OFPACT_SET_IP_TTL:
4921 case OFPACT_SET_IPV4_DST:
4922 case OFPACT_SET_IPV4_SRC:
4923 case OFPACT_SET_L4_DST_PORT:
4924 case OFPACT_SET_L4_SRC_PORT:
4925 case OFPACT_SET_MPLS_LABEL:
4926 case OFPACT_SET_MPLS_TC:
4927 case OFPACT_SET_MPLS_TTL:
4928 case OFPACT_SET_QUEUE:
4929 case OFPACT_SET_TUNNEL:
4930 case OFPACT_SET_VLAN_PCP:
4931 case OFPACT_SET_VLAN_VID:
4932 case OFPACT_STRIP_VLAN:
4935 /* In general these actions are excluded because they are not part of
4936 * the OpenFlow specification nor map to actions that are defined in
4937 * the specification. Thus the order in which they should be applied
4938 * in the action set is undefined. */
4940 case OFPACT_CONTROLLER:
4941 case OFPACT_ENQUEUE:
4943 case OFPACT_UNROLL_XLATE:
4944 case OFPACT_FIN_TIMEOUT:
4946 case OFPACT_CONJUNCTION:
4947 case OFPACT_MULTIPATH:
4949 case OFPACT_OUTPUT_REG:
4950 case OFPACT_POP_QUEUE:
4951 case OFPACT_RESUBMIT:
4953 case OFPACT_STACK_POP:
4954 case OFPACT_STACK_PUSH:
4955 case OFPACT_DEBUG_RECIRC:
4957 /* The action set may only include actions and thus
4958 * may not include any instructions */
4959 case OFPACT_CLEAR_ACTIONS:
4960 case OFPACT_GOTO_TABLE:
4962 case OFPACT_WRITE_ACTIONS:
4963 case OFPACT_WRITE_METADATA:
4970 /* Append ofpact 'a' onto the tail of 'out' */
4972 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
4974 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
4977 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
4979 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
4980 enum ofpact_type filter)
4982 const struct ofpact *target;
4983 const struct ofpact *a;
4986 OFPACT_FOR_EACH (a, in->data, in->size) {
4987 if (a->type == filter) {
4992 ofpact_copy(out, target);
4994 return target != NULL;
4997 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
4998 * The order of appended ofpacts is preserved between 'in' and 'out' */
5000 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5001 bool (*filter)(const struct ofpact *))
5003 const struct ofpact *a;
5005 OFPACT_FOR_EACH (a, in->data, in->size) {
5007 ofpact_copy(out, a);
5012 /* Reads 'action_set', which contains ofpacts accumulated by
5013 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5014 * executed directly into 'action_list'. (These names correspond to the
5015 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5017 * In general this involves appending the last instance of each action that is
5018 * admissible in the action set in the order described in the OpenFlow
5022 * + output action is only appended if no group action was present in 'in'.
5023 * + As a simplification all set actions are copied in the order the are
5024 * provided in 'in' as many set actions applied to a field has the same
5025 * affect as only applying the last action that sets a field and
5026 * duplicates are removed by do_xlate_actions().
5027 * This has an unwanted side-effect of compsoting multiple
5028 * LOAD_REG actions that touch different regions of the same field. */
5030 ofpacts_execute_action_set(struct ofpbuf *action_list,
5031 const struct ofpbuf *action_set)
5033 /* The OpenFlow spec "Action Set" section specifies this order. */
5034 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5035 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5036 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5037 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5038 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5039 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5040 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5041 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5043 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5044 * we should execute only OFPACT_GROUP.
5046 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5047 * all the actions because there's no point in modifying a packet that will
5048 * not be sent anywhere. */
5049 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5050 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5051 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5052 ofpbuf_clear(action_list);
5058 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5059 size_t n_in, enum ofp_version version,
5064 size_t start = out->size;
5066 error = ofpacts_decode(in, n_in, version, out);
5072 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5073 if (!ofpact_is_allowed_in_actions_set(a)) {
5074 VLOG_WARN_RL(&rl, "disallowed action in action set");
5075 return OFPERR_OFPBAC_BAD_TYPE;
5082 /* OpenFlow 1.1 instructions. */
5084 struct instruction_type_info {
5085 enum ovs_instruction_type type;
5089 static const struct instruction_type_info inst_info[] = {
5090 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5096 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5098 return inst_info[type].name;
5102 ovs_instruction_type_from_name(const char *name)
5104 const struct instruction_type_info *p;
5105 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5106 if (!strcasecmp(name, p->name)) {
5113 enum ovs_instruction_type
5114 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5118 return OVSINST_OFPIT13_METER;
5119 case OFPACT_CLEAR_ACTIONS:
5120 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5121 case OFPACT_WRITE_ACTIONS:
5122 return OVSINST_OFPIT11_WRITE_ACTIONS;
5123 case OFPACT_WRITE_METADATA:
5124 return OVSINST_OFPIT11_WRITE_METADATA;
5125 case OFPACT_GOTO_TABLE:
5126 return OVSINST_OFPIT11_GOTO_TABLE;
5129 case OFPACT_CONTROLLER:
5130 case OFPACT_ENQUEUE:
5131 case OFPACT_OUTPUT_REG:
5133 case OFPACT_SET_VLAN_VID:
5134 case OFPACT_SET_VLAN_PCP:
5135 case OFPACT_STRIP_VLAN:
5136 case OFPACT_PUSH_VLAN:
5137 case OFPACT_SET_ETH_SRC:
5138 case OFPACT_SET_ETH_DST:
5139 case OFPACT_SET_IPV4_SRC:
5140 case OFPACT_SET_IPV4_DST:
5141 case OFPACT_SET_IP_DSCP:
5142 case OFPACT_SET_IP_ECN:
5143 case OFPACT_SET_IP_TTL:
5144 case OFPACT_SET_L4_SRC_PORT:
5145 case OFPACT_SET_L4_DST_PORT:
5146 case OFPACT_REG_MOVE:
5147 case OFPACT_SET_FIELD:
5148 case OFPACT_STACK_PUSH:
5149 case OFPACT_STACK_POP:
5150 case OFPACT_DEC_TTL:
5151 case OFPACT_SET_MPLS_LABEL:
5152 case OFPACT_SET_MPLS_TC:
5153 case OFPACT_SET_MPLS_TTL:
5154 case OFPACT_DEC_MPLS_TTL:
5155 case OFPACT_PUSH_MPLS:
5156 case OFPACT_POP_MPLS:
5157 case OFPACT_SET_TUNNEL:
5158 case OFPACT_SET_QUEUE:
5159 case OFPACT_POP_QUEUE:
5160 case OFPACT_FIN_TIMEOUT:
5161 case OFPACT_RESUBMIT:
5163 case OFPACT_CONJUNCTION:
5164 case OFPACT_MULTIPATH:
5167 case OFPACT_UNROLL_XLATE:
5169 case OFPACT_DEBUG_RECIRC:
5171 return OVSINST_OFPIT11_APPLY_ACTIONS;
5176 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
5177 const uint16_t inst_type)
5179 switch (inst_type) {
5181 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5183 *instruction_type = OVSINST_##ENUM; \
5189 return OFPERR_OFPBIC_UNKNOWN_INST;
5193 /* Two-way translation between OVS's internal "OVSINST_*" representation of
5194 * instructions and the "OFPIT_*" representation used in OpenFlow. */
5195 struct ovsinst_map {
5196 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
5197 int ofpit; /* OFPIT_* number from OpenFlow spec. */
5200 static const struct ovsinst_map *
5201 get_ovsinst_map(enum ofp_version version)
5203 /* OpenFlow 1.1 and 1.2 instructions. */
5204 static const struct ovsinst_map of11[] = {
5205 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5206 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5207 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5208 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5209 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5213 /* OpenFlow 1.3+ instructions. */
5214 static const struct ovsinst_map of13[] = {
5215 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5216 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5217 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5218 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5219 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5220 { OVSINST_OFPIT13_METER, 6 },
5224 return version < OFP13_VERSION ? of11 : of13;
5227 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
5228 * values, into a bitmap of instructions suitable for OpenFlow 'version'
5229 * (OFP11_VERSION or later), and returns the result. */
5231 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
5233 uint32_t ofpit_bitmap = 0;
5234 const struct ovsinst_map *x;
5236 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5237 if (ovsinst_bitmap & (1u << x->ovsinst)) {
5238 ofpit_bitmap |= 1u << x->ofpit;
5241 return htonl(ofpit_bitmap);
5244 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5245 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5246 * correspond to OVSINST_* values, and returns the result. */
5248 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
5250 uint32_t ovsinst_bitmap = 0;
5251 const struct ovsinst_map *x;
5253 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5254 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
5255 ovsinst_bitmap |= 1u << x->ovsinst;
5258 return ovsinst_bitmap;
5261 static inline struct ofp11_instruction *
5262 instruction_next(const struct ofp11_instruction *inst)
5264 return ((struct ofp11_instruction *) (void *)
5265 ((uint8_t *) inst + ntohs(inst->len)));
5269 instruction_is_valid(const struct ofp11_instruction *inst,
5270 size_t n_instructions)
5272 uint16_t len = ntohs(inst->len);
5273 return (!(len % OFP11_INSTRUCTION_ALIGN)
5274 && len >= sizeof *inst
5275 && len / sizeof *inst <= n_instructions);
5278 /* This macro is careful to check for instructions with bad lengths. */
5279 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
5280 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
5281 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
5282 ((LEFT) -= (ntohs((ITER)->len) \
5283 / sizeof(struct ofp11_instruction)), \
5284 (ITER) = instruction_next(ITER)))
5287 decode_openflow11_instruction(const struct ofp11_instruction *inst,
5288 enum ovs_instruction_type *type)
5290 uint16_t len = ntohs(inst->len);
5292 switch (inst->type) {
5293 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5294 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5296 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5297 case CONSTANT_HTONS(ENUM): \
5299 ? len >= sizeof(struct STRUCT) \
5300 : len == sizeof(struct STRUCT)) { \
5301 *type = OVSINST_##ENUM; \
5304 return OFPERR_OFPBIC_BAD_LEN; \
5310 return OFPERR_OFPBIC_UNKNOWN_INST;
5315 decode_openflow11_instructions(const struct ofp11_instruction insts[],
5317 const struct ofp11_instruction *out[])
5319 const struct ofp11_instruction *inst;
5322 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5323 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5324 enum ovs_instruction_type type;
5327 error = decode_openflow11_instruction(inst, &type);
5333 return OFPERR_OFPBIC_DUP_INST;
5339 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5340 (n_insts - left) * sizeof *inst);
5341 return OFPERR_OFPBIC_BAD_LEN;
5347 get_actions_from_instruction(const struct ofp11_instruction *inst,
5348 const struct ofp_action_header **actions,
5349 size_t *actions_len)
5351 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5352 *actions_len = ntohs(inst->len) - sizeof *inst;
5356 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5357 unsigned int instructions_len,
5358 enum ofp_version version,
5359 struct ofpbuf *ofpacts)
5361 const struct ofp11_instruction *instructions;
5362 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5365 if (version == OFP10_VERSION) {
5366 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5368 (1u << N_OVS_INSTRUCTIONS) - 1,
5372 ofpbuf_clear(ofpacts);
5374 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5375 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5377 instructions_len, OFP11_INSTRUCTION_ALIGN);
5378 error = OFPERR_OFPBIC_BAD_LEN;
5382 instructions = ofpbuf_try_pull(openflow, instructions_len);
5383 if (instructions == NULL) {
5384 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5385 "remaining message length (%"PRIu32")",
5386 instructions_len, openflow->size);
5387 error = OFPERR_OFPBIC_BAD_LEN;
5391 error = decode_openflow11_instructions(
5392 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5398 if (insts[OVSINST_OFPIT13_METER]) {
5399 const struct ofp13_instruction_meter *oim;
5400 struct ofpact_meter *om;
5402 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5403 insts[OVSINST_OFPIT13_METER]);
5405 om = ofpact_put_METER(ofpacts);
5406 om->meter_id = ntohl(oim->meter_id);
5408 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5409 const struct ofp_action_header *actions;
5412 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5413 &actions, &actions_len);
5414 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5419 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5420 instruction_get_OFPIT11_CLEAR_ACTIONS(
5421 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5422 ofpact_put_CLEAR_ACTIONS(ofpacts);
5424 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5425 struct ofpact_nest *on;
5426 const struct ofp_action_header *actions;
5430 ofpact_pad(ofpacts);
5431 start = ofpacts->size;
5432 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5433 offsetof(struct ofpact_nest, actions));
5434 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5435 &actions, &actions_len);
5436 error = ofpacts_decode_for_action_set(actions, actions_len,
5441 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5442 on->ofpact.len = ofpacts->size - start;
5444 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5445 const struct ofp11_instruction_write_metadata *oiwm;
5446 struct ofpact_metadata *om;
5448 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5449 insts[OVSINST_OFPIT11_WRITE_METADATA]);
5451 om = ofpact_put_WRITE_METADATA(ofpacts);
5452 om->metadata = oiwm->metadata;
5453 om->mask = oiwm->metadata_mask;
5455 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5456 const struct ofp11_instruction_goto_table *oigt;
5457 struct ofpact_goto_table *ogt;
5459 oigt = instruction_get_OFPIT11_GOTO_TABLE(
5460 insts[OVSINST_OFPIT11_GOTO_TABLE]);
5461 ogt = ofpact_put_GOTO_TABLE(ofpacts);
5462 ogt->table_id = oigt->table_id;
5465 error = ofpacts_verify(ofpacts->data, ofpacts->size,
5466 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
5469 ofpbuf_clear(ofpacts);
5474 /* Update the length of the instruction that begins at offset 'ofs' within
5475 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5476 * If the instruction contains no nested actions, deletes it entirely. */
5478 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5480 struct ofp11_instruction_actions *oia;
5482 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5483 if (openflow->size > ofs + sizeof *oia) {
5484 oia->len = htons(openflow->size - ofs);
5486 openflow->size = ofs;
5490 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5491 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5492 * valid, otherwise an OpenFlow error code. */
5494 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5502 case OFPP_CONTROLLER:
5507 return OFPERR_OFPBAC_BAD_OUT_PORT;
5510 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
5513 return OFPERR_OFPBAC_BAD_OUT_PORT;
5517 /* Removes the protocols that require consistency between match and actions
5518 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5520 * (An example of an inconsistency between match and actions is a flow that
5521 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5524 inconsistent_match(enum ofputil_protocol *usable_protocols)
5526 *usable_protocols &= OFPUTIL_P_OF10_ANY;
5529 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5530 * caller must restore them.
5532 * Modifies some actions, filling in fields that could not be properly set
5533 * without context. */
5535 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
5536 struct flow *flow, ofp_port_t max_ports,
5537 uint8_t table_id, uint8_t n_tables)
5539 const struct ofpact_enqueue *enqueue;
5540 const struct mf_field *mf;
5544 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
5547 case OFPACT_CONTROLLER:
5550 case OFPACT_ENQUEUE:
5551 enqueue = ofpact_get_ENQUEUE(a);
5552 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
5553 && enqueue->port != OFPP_IN_PORT
5554 && enqueue->port != OFPP_LOCAL) {
5555 return OFPERR_OFPBAC_BAD_OUT_PORT;
5559 case OFPACT_OUTPUT_REG:
5560 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
5563 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
5565 case OFPACT_SET_VLAN_VID:
5566 /* Remember if we saw a vlan tag in the flow to aid translating to
5567 * OpenFlow 1.1+ if need be. */
5568 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
5569 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5570 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5571 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5572 inconsistent_match(usable_protocols);
5574 /* Temporary mark that we have a vlan tag. */
5575 flow->vlan_tci |= htons(VLAN_CFI);
5578 case OFPACT_SET_VLAN_PCP:
5579 /* Remember if we saw a vlan tag in the flow to aid translating to
5580 * OpenFlow 1.1+ if need be. */
5581 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
5582 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5583 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5584 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5585 inconsistent_match(usable_protocols);
5587 /* Temporary mark that we have a vlan tag. */
5588 flow->vlan_tci |= htons(VLAN_CFI);
5591 case OFPACT_STRIP_VLAN:
5592 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5593 inconsistent_match(usable_protocols);
5595 /* Temporary mark that we have no vlan tag. */
5596 flow->vlan_tci = htons(0);
5599 case OFPACT_PUSH_VLAN:
5600 if (flow->vlan_tci & htons(VLAN_CFI)) {
5601 /* Multiple VLAN headers not supported. */
5602 return OFPERR_OFPBAC_BAD_TAG;
5604 /* Temporary mark that we have a vlan tag. */
5605 flow->vlan_tci |= htons(VLAN_CFI);
5608 case OFPACT_SET_ETH_SRC:
5609 case OFPACT_SET_ETH_DST:
5612 case OFPACT_SET_IPV4_SRC:
5613 case OFPACT_SET_IPV4_DST:
5614 if (flow->dl_type != htons(ETH_TYPE_IP)) {
5615 inconsistent_match(usable_protocols);
5619 case OFPACT_SET_IP_DSCP:
5620 case OFPACT_SET_IP_ECN:
5621 case OFPACT_SET_IP_TTL:
5622 case OFPACT_DEC_TTL:
5623 if (!is_ip_any(flow)) {
5624 inconsistent_match(usable_protocols);
5628 case OFPACT_SET_L4_SRC_PORT:
5629 case OFPACT_SET_L4_DST_PORT:
5630 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
5631 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5632 && flow->nw_proto != IPPROTO_SCTP)) {
5633 inconsistent_match(usable_protocols);
5635 /* Note on which transport protocol the port numbers are set.
5636 * This allows this set action to be converted to an OF1.2 set field
5638 if (a->type == OFPACT_SET_L4_SRC_PORT) {
5639 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
5641 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
5645 case OFPACT_REG_MOVE:
5646 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
5648 case OFPACT_SET_FIELD:
5649 mf = ofpact_get_SET_FIELD(a)->field;
5650 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
5651 if (!mf_are_prereqs_ok(mf, flow) ||
5652 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
5653 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
5655 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5657 /* Remember if we saw a vlan tag in the flow to aid translating to
5658 * OpenFlow 1.1 if need be. */
5659 ofpact_get_SET_FIELD(a)->flow_has_vlan =
5660 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5661 if (mf->id == MFF_VLAN_TCI) {
5662 /* The set field may add or remove the vlan tag,
5663 * Mark the status temporarily. */
5664 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
5668 case OFPACT_STACK_PUSH:
5669 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
5671 case OFPACT_STACK_POP:
5672 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
5674 case OFPACT_SET_MPLS_LABEL:
5675 case OFPACT_SET_MPLS_TC:
5676 case OFPACT_SET_MPLS_TTL:
5677 case OFPACT_DEC_MPLS_TTL:
5678 if (!eth_type_mpls(flow->dl_type)) {
5679 inconsistent_match(usable_protocols);
5683 case OFPACT_SET_TUNNEL:
5684 case OFPACT_SET_QUEUE:
5685 case OFPACT_POP_QUEUE:
5686 case OFPACT_RESUBMIT:
5689 case OFPACT_FIN_TIMEOUT:
5690 if (flow->nw_proto != IPPROTO_TCP) {
5691 inconsistent_match(usable_protocols);
5696 return learn_check(ofpact_get_LEARN(a), flow);
5698 case OFPACT_CONJUNCTION:
5701 case OFPACT_MULTIPATH:
5702 return multipath_check(ofpact_get_MULTIPATH(a), flow);
5708 case OFPACT_PUSH_MPLS:
5709 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
5710 /* The packet is now MPLS and the MPLS payload is opaque.
5711 * Thus nothing can be assumed about the network protocol.
5712 * Temporarily mark that we have no nw_proto. */
5716 case OFPACT_POP_MPLS:
5717 if (!eth_type_mpls(flow->dl_type)) {
5718 inconsistent_match(usable_protocols);
5720 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
5726 case OFPACT_CLEAR_ACTIONS:
5729 case OFPACT_WRITE_ACTIONS: {
5730 /* Use a temporary copy of 'usable_protocols' because we can't check
5731 * consistency of an action set. */
5732 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
5733 enum ofputil_protocol p = *usable_protocols;
5734 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
5735 flow, max_ports, table_id, n_tables, &p);
5738 case OFPACT_WRITE_METADATA:
5741 case OFPACT_METER: {
5742 uint32_t mid = ofpact_get_METER(a)->meter_id;
5743 if (mid == 0 || mid > OFPM13_MAX) {
5744 return OFPERR_OFPMMFC_INVALID_METER;
5749 case OFPACT_GOTO_TABLE: {
5750 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
5751 if ((table_id != 255 && goto_table <= table_id)
5752 || (n_tables != 255 && goto_table >= n_tables)) {
5753 return OFPERR_OFPBIC_BAD_TABLE_ID;
5761 case OFPACT_UNROLL_XLATE:
5762 /* UNROLL is an internal action that should never be seen via
5764 return OFPERR_OFPBAC_BAD_TYPE;
5766 case OFPACT_DEBUG_RECIRC:
5774 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5775 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
5776 * switch with no more than 'max_ports' ports.
5778 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
5779 * '*usable_protocols' the protocols that forbid the inconsistency. (An
5780 * example of an inconsistency between match and actions is a flow that does
5781 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
5783 * May annotate ofpacts with information gathered from the 'flow'.
5785 * May temporarily modify 'flow', but restores the changes before returning. */
5787 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
5788 struct flow *flow, ofp_port_t max_ports,
5789 uint8_t table_id, uint8_t n_tables,
5790 enum ofputil_protocol *usable_protocols)
5793 ovs_be16 dl_type = flow->dl_type;
5794 ovs_be16 vlan_tci = flow->vlan_tci;
5795 uint8_t nw_proto = flow->nw_proto;
5796 enum ofperr error = 0;
5798 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5799 error = ofpact_check__(usable_protocols, a, flow,
5800 max_ports, table_id, n_tables);
5805 /* Restore fields that may have been modified. */
5806 flow->dl_type = dl_type;
5807 flow->vlan_tci = vlan_tci;
5808 flow->nw_proto = nw_proto;
5812 /* Like ofpacts_check(), but reports inconsistencies as
5813 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
5815 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
5816 struct flow *flow, ofp_port_t max_ports,
5817 uint8_t table_id, uint8_t n_tables,
5818 enum ofputil_protocol usable_protocols)
5820 enum ofputil_protocol p = usable_protocols;
5823 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
5824 table_id, n_tables, &p);
5825 return (error ? error
5826 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
5831 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
5833 if (outer_action != OFPACT_WRITE_ACTIONS) {
5834 VLOG_WARN("\"%s\" action doesn't support nested action \"%s\"",
5835 ofpact_name(outer_action), ofpact_name(a->type));
5836 return OFPERR_OFPBAC_BAD_ARGUMENT;
5842 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
5843 * appropriate order as defined by the OpenFlow spec and as required by Open
5846 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
5847 * instructions that are allowed within 'ofpacts[]'.
5849 * If 'outer_action' is not zero, it specifies that the actions are nested
5850 * within another action of type 'outer_action'. */
5852 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
5853 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
5855 const struct ofpact *a;
5856 enum ovs_instruction_type inst;
5858 inst = OVSINST_OFPIT13_METER;
5859 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5860 enum ovs_instruction_type next;
5862 if (a->type == OFPACT_CONJUNCTION) {
5863 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5864 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
5865 VLOG_WARN("\"conjunction\" actions may be used along with "
5866 "\"note\" but not any other kind of action "
5867 "(such as the \"%s\" action used here)",
5868 ofpact_name(a->type));
5869 return OFPERR_NXBAC_BAD_CONJUNCTION;
5876 enum ofperr error = ofpacts_verify_nested(a, outer_action);
5883 next = ovs_instruction_type_from_ofpact_type(a->type);
5885 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
5888 const char *name = ovs_instruction_name_from_type(inst);
5889 const char *next_name = ovs_instruction_name_from_type(next);
5892 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
5893 "1.1+ compatibility", name);
5895 VLOG_WARN("invalid instruction ordering: %s must appear "
5896 "before %s, for OpenFlow 1.1+ compatibility",
5899 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
5901 if (!((1u << next) & allowed_ovsinsts)) {
5902 const char *name = ovs_instruction_name_from_type(next);
5904 VLOG_WARN("%s instruction not allowed here", name);
5905 return OFPERR_OFPBIC_UNSUP_INST;
5914 /* Converting ofpacts to OpenFlow. */
5917 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
5921 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5922 case OFPACT_##ENUM: \
5923 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
5932 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
5933 * actions in 'openflow', appending the actions to any existing data in
5936 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
5937 struct ofpbuf *openflow,
5938 enum ofp_version ofp_version)
5940 const struct ofpact *a;
5941 size_t start_size = openflow->size;
5943 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5944 encode_ofpact(a, ofp_version, openflow);
5946 return openflow->size - start_size;
5949 static enum ovs_instruction_type
5950 ofpact_is_apply_actions(const struct ofpact *a)
5952 return (ovs_instruction_type_from_ofpact_type(a->type)
5953 == OVSINST_OFPIT11_APPLY_ACTIONS);
5957 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
5959 struct ofpbuf *openflow,
5960 enum ofp_version ofp_version)
5962 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
5963 const struct ofpact *a;
5965 if (ofp_version == OFP10_VERSION) {
5966 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
5973 if (ofpact_is_apply_actions(a)) {
5974 size_t ofs = openflow->size;
5976 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
5978 encode_ofpact(a, ofp_version, openflow);
5980 } while (a < end && ofpact_is_apply_actions(a));
5981 ofpacts_update_instruction_actions(openflow, ofs);
5983 encode_ofpact(a, ofp_version, openflow);
5989 /* Sets of supported actions. */
5991 /* Two-way translation between OVS's internal "OFPACT_*" representation of
5992 * actions and the "OFPAT_*" representation used in some OpenFlow version.
5993 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
5994 * instance is specific to one OpenFlow version.) */
5996 enum ofpact_type ofpact; /* Internal name for action type. */
5997 int ofpat; /* OFPAT_* number from OpenFlow spec. */
6000 static const struct ofpact_map *
6001 get_ofpact_map(enum ofp_version version)
6003 /* OpenFlow 1.0 actions. */
6004 static const struct ofpact_map of10[] = {
6005 { OFPACT_OUTPUT, 0 },
6006 { OFPACT_SET_VLAN_VID, 1 },
6007 { OFPACT_SET_VLAN_PCP, 2 },
6008 { OFPACT_STRIP_VLAN, 3 },
6009 { OFPACT_SET_ETH_SRC, 4 },
6010 { OFPACT_SET_ETH_DST, 5 },
6011 { OFPACT_SET_IPV4_SRC, 6 },
6012 { OFPACT_SET_IPV4_DST, 7 },
6013 { OFPACT_SET_IP_DSCP, 8 },
6014 { OFPACT_SET_L4_SRC_PORT, 9 },
6015 { OFPACT_SET_L4_DST_PORT, 10 },
6016 { OFPACT_ENQUEUE, 11 },
6020 /* OpenFlow 1.1 actions. */
6021 static const struct ofpact_map of11[] = {
6022 { OFPACT_OUTPUT, 0 },
6023 { OFPACT_SET_VLAN_VID, 1 },
6024 { OFPACT_SET_VLAN_PCP, 2 },
6025 { OFPACT_SET_ETH_SRC, 3 },
6026 { OFPACT_SET_ETH_DST, 4 },
6027 { OFPACT_SET_IPV4_SRC, 5 },
6028 { OFPACT_SET_IPV4_DST, 6 },
6029 { OFPACT_SET_IP_DSCP, 7 },
6030 { OFPACT_SET_IP_ECN, 8 },
6031 { OFPACT_SET_L4_SRC_PORT, 9 },
6032 { OFPACT_SET_L4_DST_PORT, 10 },
6033 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6034 /* OFPAT_COPY_TTL_IN (12) not supported. */
6035 { OFPACT_SET_MPLS_LABEL, 13 },
6036 { OFPACT_SET_MPLS_TC, 14 },
6037 { OFPACT_SET_MPLS_TTL, 15 },
6038 { OFPACT_DEC_MPLS_TTL, 16 },
6039 { OFPACT_PUSH_VLAN, 17 },
6040 { OFPACT_STRIP_VLAN, 18 },
6041 { OFPACT_PUSH_MPLS, 19 },
6042 { OFPACT_POP_MPLS, 20 },
6043 { OFPACT_SET_QUEUE, 21 },
6044 { OFPACT_GROUP, 22 },
6045 { OFPACT_SET_IP_TTL, 23 },
6046 { OFPACT_DEC_TTL, 24 },
6050 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6051 static const struct ofpact_map of12[] = {
6052 { OFPACT_OUTPUT, 0 },
6053 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6054 /* OFPAT_COPY_TTL_IN (12) not supported. */
6055 { OFPACT_SET_MPLS_TTL, 15 },
6056 { OFPACT_DEC_MPLS_TTL, 16 },
6057 { OFPACT_PUSH_VLAN, 17 },
6058 { OFPACT_STRIP_VLAN, 18 },
6059 { OFPACT_PUSH_MPLS, 19 },
6060 { OFPACT_POP_MPLS, 20 },
6061 { OFPACT_SET_QUEUE, 21 },
6062 { OFPACT_GROUP, 22 },
6063 { OFPACT_SET_IP_TTL, 23 },
6064 { OFPACT_DEC_TTL, 24 },
6065 { OFPACT_SET_FIELD, 25 },
6066 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
6067 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
6087 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
6088 * values, into a bitmap of actions suitable for OpenFlow 'version', and
6089 * returns the result. */
6091 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
6093 uint32_t openflow_bitmap = 0;
6094 const struct ofpact_map *x;
6096 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6097 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
6098 openflow_bitmap |= 1u << x->ofpat;
6101 return htonl(openflow_bitmap);
6104 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
6105 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
6106 * and returns the result. */
6108 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
6110 uint64_t ofpact_bitmap = 0;
6111 const struct ofpact_map *x;
6113 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6114 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
6115 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
6118 return ofpact_bitmap;
6121 /* Appends to 's' a string representation of the set of OFPACT_* represented
6122 * by 'ofpacts_bitmap'. */
6124 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
6126 if (!ofpacts_bitmap) {
6127 ds_put_cstr(s, "<none>");
6129 while (ofpacts_bitmap) {
6130 ds_put_format(s, "%s ",
6131 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
6132 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
6138 /* Returns true if 'action' outputs to 'port', false otherwise. */
6140 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
6142 switch (ofpact->type) {
6144 return ofpact_get_OUTPUT(ofpact)->port == port;
6145 case OFPACT_ENQUEUE:
6146 return ofpact_get_ENQUEUE(ofpact)->port == port;
6147 case OFPACT_CONTROLLER:
6148 return port == OFPP_CONTROLLER;
6150 case OFPACT_OUTPUT_REG:
6152 case OFPACT_SET_VLAN_VID:
6153 case OFPACT_SET_VLAN_PCP:
6154 case OFPACT_STRIP_VLAN:
6155 case OFPACT_PUSH_VLAN:
6156 case OFPACT_SET_ETH_SRC:
6157 case OFPACT_SET_ETH_DST:
6158 case OFPACT_SET_IPV4_SRC:
6159 case OFPACT_SET_IPV4_DST:
6160 case OFPACT_SET_IP_DSCP:
6161 case OFPACT_SET_IP_ECN:
6162 case OFPACT_SET_IP_TTL:
6163 case OFPACT_SET_L4_SRC_PORT:
6164 case OFPACT_SET_L4_DST_PORT:
6165 case OFPACT_REG_MOVE:
6166 case OFPACT_SET_FIELD:
6167 case OFPACT_STACK_PUSH:
6168 case OFPACT_STACK_POP:
6169 case OFPACT_DEC_TTL:
6170 case OFPACT_SET_MPLS_LABEL:
6171 case OFPACT_SET_MPLS_TC:
6172 case OFPACT_SET_MPLS_TTL:
6173 case OFPACT_DEC_MPLS_TTL:
6174 case OFPACT_SET_TUNNEL:
6175 case OFPACT_WRITE_METADATA:
6176 case OFPACT_SET_QUEUE:
6177 case OFPACT_POP_QUEUE:
6178 case OFPACT_FIN_TIMEOUT:
6179 case OFPACT_RESUBMIT:
6181 case OFPACT_CONJUNCTION:
6182 case OFPACT_MULTIPATH:
6185 case OFPACT_UNROLL_XLATE:
6186 case OFPACT_PUSH_MPLS:
6187 case OFPACT_POP_MPLS:
6189 case OFPACT_CLEAR_ACTIONS:
6190 case OFPACT_WRITE_ACTIONS:
6191 case OFPACT_GOTO_TABLE:
6194 case OFPACT_DEBUG_RECIRC:
6200 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6201 * to 'port', false otherwise. */
6203 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
6206 const struct ofpact *a;
6208 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6209 if (ofpact_outputs_to_port(a, port)) {
6217 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6218 * to 'group', false otherwise. */
6220 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
6223 const struct ofpact *a;
6225 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6226 if (a->type == OFPACT_GROUP
6227 && ofpact_get_GROUP(a)->group_id == group_id) {
6236 ofpacts_equal(const struct ofpact *a, size_t a_len,
6237 const struct ofpact *b, size_t b_len)
6239 return a_len == b_len && !memcmp(a, b, a_len);
6242 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
6243 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
6245 * This function relies on the order of 'ofpacts' being correct (as checked by
6246 * ofpacts_verify()). */
6248 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
6250 const struct ofpact *a;
6252 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6253 enum ovs_instruction_type inst;
6255 inst = ovs_instruction_type_from_ofpact_type(a->type);
6256 if (a->type == OFPACT_METER) {
6257 return ofpact_get_METER(a)->meter_id;
6258 } else if (inst > OVSINST_OFPIT13_METER) {
6266 /* Formatting ofpacts. */
6269 ofpact_format(const struct ofpact *a, struct ds *s)
6272 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6273 case OFPACT_##ENUM: \
6274 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
6283 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
6284 * 'ofpacts' to 'string'. */
6286 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
6290 ds_put_cstr(string, "drop");
6292 const struct ofpact *a;
6294 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6296 ds_put_cstr(string, ",");
6299 /* XXX write-actions */
6300 ofpact_format(a, string);
6305 /* Internal use by helpers. */
6308 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
6310 struct ofpact *ofpact;
6312 ofpact_pad(ofpacts);
6313 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
6314 ofpact = ofpacts->header;
6315 ofpact_init(ofpact, type, len);
6320 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
6322 memset(ofpact, 0, len);
6323 ofpact->type = type;
6328 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6329 * starting at 'ofpact'.
6331 * This is the correct way to update a variable-length ofpact's length after
6332 * adding the variable-length part of the payload. (See the large comment
6333 * near the end of ofp-actions.h for more information.) */
6335 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
6337 ovs_assert(ofpact == ofpacts->header);
6338 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6341 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6342 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6343 * client must call this itself after adding the final ofpact to an array of
6346 * (The consequences of failing to call this function are probably not dire.
6347 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6348 * not dereference it. That's undefined behavior, technically, but it will not
6349 * cause a real problem on common systems. Still, it seems better to call
6352 ofpact_pad(struct ofpbuf *ofpacts)
6354 unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
6356 ofpbuf_put_zeros(ofpacts, pad);
6363 static char * OVS_WARN_UNUSED_RESULT
6364 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6365 enum ofputil_protocol *usable_protocols)
6368 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6369 case OFPACT_##ENUM: \
6370 return parse_##ENUM(value, ofpacts, usable_protocols);
6379 ofpact_type_from_name(const char *name, enum ofpact_type *type)
6381 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6382 if (!strcasecmp(name, NAME)) { \
6383 *type = OFPACT_##ENUM; \
6392 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6394 * Returns NULL if successful, otherwise a malloc()'d string describing the
6395 * error. The caller is responsible for freeing the returned string.
6397 * If 'outer_action' is specified, indicates that the actions being parsed
6398 * are nested within another action of the type specified in 'outer_action'. */
6399 static char * OVS_WARN_UNUSED_RESULT
6400 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6401 enum ofputil_protocol *usable_protocols,
6402 bool allow_instructions, enum ofpact_type outer_action)
6411 while (ofputil_parse_key_value(&pos, &key, &value)) {
6412 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6413 enum ofpact_type type;
6417 if (ofpact_type_from_name(key, &type)) {
6418 error = ofpact_parse(type, value, ofpacts, usable_protocols);
6419 inst = ovs_instruction_type_from_ofpact_type(type);
6420 } else if (!strcasecmp(key, "mod_vlan_vid")) {
6421 error = parse_set_vlan_vid(value, ofpacts, true);
6422 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6423 error = parse_set_vlan_pcp(value, ofpacts, true);
6424 } else if (!strcasecmp(key, "set_nw_ttl")) {
6425 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6426 } else if (!strcasecmp(key, "pop_vlan")) {
6427 error = parse_pop_vlan(ofpacts);
6428 } else if (!strcasecmp(key, "set_tunnel64")) {
6429 error = parse_set_tunnel(value, ofpacts,
6430 NXAST_RAW_SET_TUNNEL64);
6431 } else if (!strcasecmp(key, "load")) {
6432 error = parse_reg_load(value, ofpacts);
6433 } else if (!strcasecmp(key, "bundle_load")) {
6434 error = parse_bundle_load(value, ofpacts);
6435 } else if (!strcasecmp(key, "drop")) {
6437 } else if (!strcasecmp(key, "apply_actions")) {
6438 return xstrdup("apply_actions is the default instruction");
6439 } else if (ofputil_port_from_string(key, &port)) {
6440 ofpact_put_OUTPUT(ofpacts)->port = port;
6442 return xasprintf("unknown action %s", key);
6448 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
6449 if (!allow_instructions) {
6450 return xasprintf("only actions are allowed here (not "
6452 ovs_instruction_name_from_type(inst));
6454 if (inst == prev_inst) {
6455 return xasprintf("instruction %s may be specified only once",
6456 ovs_instruction_name_from_type(inst));
6459 if (prev_inst != -1 && inst < prev_inst) {
6460 return xasprintf("instruction %s must be specified before %s",
6461 ovs_instruction_name_from_type(inst),
6462 ovs_instruction_name_from_type(prev_inst));
6466 ofpact_pad(ofpacts);
6468 if (drop && ofpacts->size) {
6469 return xstrdup("\"drop\" must not be accompanied by any other action "
6473 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
6475 ? (1u << N_OVS_INSTRUCTIONS) - 1
6476 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
6479 return xstrdup("Incorrect instruction ordering");
6485 static char * OVS_WARN_UNUSED_RESULT
6486 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
6487 enum ofputil_protocol *usable_protocols, bool allow_instructions,
6488 enum ofpact_type outer_action)
6490 uint32_t orig_size = ofpacts->size;
6491 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
6492 allow_instructions, outer_action);
6494 ofpacts->size = orig_size;
6499 static char * OVS_WARN_UNUSED_RESULT
6500 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
6501 enum ofputil_protocol *usable_protocols,
6502 bool allow_instructions, enum ofpact_type outer_action)
6506 *usable_protocols = OFPUTIL_P_ANY;
6509 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
6516 /* Parses 's' as a set of OpenFlow actions and appends the actions to
6517 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
6518 * that are nested within the action of type 'outer_action'.
6520 * Returns NULL if successful, otherwise a malloc()'d string describing the
6521 * error. The caller is responsible for freeing the returned string. */
6522 char * OVS_WARN_UNUSED_RESULT
6523 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
6524 enum ofputil_protocol *usable_protocols)
6526 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
6529 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
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_instructions(const char *s, struct ofpbuf *ofpacts,
6536 enum ofputil_protocol *usable_protocols)
6538 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
6542 ofpact_name(enum ofpact_type type)
6545 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
6552 /* Low-level action decoding and encoding functions. */
6554 /* Everything needed to identify a particular OpenFlow action. */
6555 struct ofpact_hdrs {
6556 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
6557 uint16_t type; /* Type if standard, otherwise subtype. */
6558 uint8_t ofp_version; /* From ofp_header. */
6561 /* Information about a particular OpenFlow action. */
6562 struct ofpact_raw_instance {
6563 /* The action's identity. */
6564 struct ofpact_hdrs hdrs;
6565 enum ofp_raw_action_type raw;
6567 /* Looking up the action. */
6568 struct hmap_node decode_node; /* Based on 'hdrs'. */
6569 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
6571 /* The action's encoded size.
6573 * If this action is fixed-length, 'min_length' == 'max_length'.
6574 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
6575 * OFP_ACTION_ALIGN) == 65528. */
6576 unsigned short int min_length;
6577 unsigned short int max_length;
6579 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
6580 * offset of that argument from the beginning of the action and 'arg_len'
6581 * its length, both in bytes.
6583 * For actions that take other forms, these are both zero. */
6584 unsigned short int arg_ofs;
6585 unsigned short int arg_len;
6587 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
6590 /* If this action is deprecated, a human-readable string with a brief
6592 const char *deprecation;
6595 /* Action header. */
6596 struct ofp_action_header {
6597 /* The meaning of other values of 'type' generally depends on the OpenFlow
6598 * version (see enum ofp_raw_action_type).
6600 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
6601 * designates an OpenFlow vendor ID and that the remainder of the action
6602 * structure has a vendor-defined meaning.
6604 #define OFPAT_VENDOR 0xffff
6607 /* Always a multiple of 8. */
6610 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
6611 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
6614 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
6616 /* Header for Nicira-defined actions and for ONF vendor extensions.
6618 * This cannot be used as an entirely generic vendor extension action header,
6619 * because OpenFlow does not specify the location or size of the action
6620 * subtype; it just happens that ONF extensions and Nicira extensions share
6622 struct ext_action_header {
6623 ovs_be16 type; /* OFPAT_VENDOR. */
6624 ovs_be16 len; /* At least 16. */
6625 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
6626 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
6629 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
6632 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
6633 const struct ofpact_hdrs *b)
6635 return (a->vendor == b->vendor
6636 && a->type == b->type
6637 && a->ofp_version == b->ofp_version);
6641 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
6643 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
6646 #include "ofp-actions.inc2"
6648 static struct hmap *
6649 ofpact_decode_hmap(void)
6651 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6652 static struct hmap hmap;
6654 if (ovsthread_once_start(&once)) {
6655 struct ofpact_raw_instance *inst;
6658 for (inst = all_raw_instances;
6659 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6661 hmap_insert(&hmap, &inst->decode_node,
6662 ofpact_hdrs_hash(&inst->hdrs));
6664 ovsthread_once_done(&once);
6669 static struct hmap *
6670 ofpact_encode_hmap(void)
6672 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6673 static struct hmap hmap;
6675 if (ovsthread_once_start(&once)) {
6676 struct ofpact_raw_instance *inst;
6679 for (inst = all_raw_instances;
6680 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6682 hmap_insert(&hmap, &inst->encode_node,
6683 hash_2words(inst->raw, inst->hdrs.ofp_version));
6685 ovsthread_once_done(&once);
6691 ofpact_decode_raw(enum ofp_version ofp_version,
6692 const struct ofp_action_header *oah, size_t length,
6693 const struct ofpact_raw_instance **instp)
6695 const struct ofpact_raw_instance *inst;
6696 struct ofpact_hdrs hdrs;
6699 if (length < sizeof *oah) {
6700 return OFPERR_OFPBAC_BAD_LEN;
6703 /* Get base action type. */
6704 if (oah->type == htons(OFPAT_VENDOR)) {
6706 hdrs.vendor = ntohl(oah->vendor);
6707 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
6708 /* Get extension subtype. */
6709 const struct ext_action_header *nah;
6711 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
6712 if (length < sizeof *nah) {
6713 return OFPERR_OFPBAC_BAD_LEN;
6715 hdrs.type = ntohs(nah->subtype);
6717 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
6719 return OFPERR_OFPBAC_BAD_VENDOR;
6723 hdrs.type = ntohs(oah->type);
6726 hdrs.ofp_version = ofp_version;
6727 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
6728 ofpact_decode_hmap()) {
6729 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
6736 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
6737 : OFPERR_OFPBAC_BAD_TYPE);
6741 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6742 enum ofp_raw_action_type *raw, uint64_t *arg)
6744 const struct ofp_action_header *oah = buf->data;
6745 const struct ofpact_raw_instance *action;
6746 unsigned int length;
6750 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
6755 if (action->deprecation) {
6756 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
6757 action->name, ofputil_version_to_string(ofp_version),
6758 action->deprecation);
6761 length = ntohs(oah->len);
6762 if (length > buf->size) {
6763 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
6764 "length %"PRIu32, action->name, length, buf->size);
6765 return OFPERR_OFPBAC_BAD_LEN;
6767 if (length < action->min_length || length > action->max_length) {
6768 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
6769 "[%hu,%hu]", action->name, length,
6770 action->min_length, action->max_length);
6771 return OFPERR_OFPBAC_BAD_LEN;
6774 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
6775 "of 8", action->name, length);
6776 return OFPERR_OFPBAC_BAD_LEN;
6781 if (action->arg_len) {
6785 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
6786 for (i = 0; i < action->arg_len; i++) {
6787 *arg = (*arg << 8) | p[i];
6791 ofpbuf_pull(buf, length);
6796 static const struct ofpact_raw_instance *
6797 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
6799 const struct ofpact_raw_instance *inst;
6801 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
6802 ofpact_encode_hmap()) {
6803 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
6811 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6812 enum ofp_raw_action_type raw, uint64_t arg)
6814 const struct ofpact_raw_instance *inst;
6815 struct ofp_action_header *oah;
6816 const struct ofpact_hdrs *hdrs;
6818 inst = ofpact_raw_lookup(ofp_version, raw);
6821 oah = ofpbuf_put_zeros(buf, inst->min_length);
6822 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
6823 oah->len = htons(inst->min_length);
6824 oah->vendor = htonl(hdrs->vendor);
6826 switch (hdrs->vendor) {
6831 case ONF_VENDOR_ID: {
6832 struct ext_action_header *nah = (struct ext_action_header *) oah;
6833 nah->subtype = htons(hdrs->type);
6841 if (inst->arg_len) {
6842 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
6845 for (i = 0; i < inst->arg_len; i++) {
6857 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
6859 struct ofp_action_header *oah;
6861 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs, 8));
6863 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
6864 oah->len = htons(openflow->size - start_ofs);