2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-actions.h"
20 #include "byte-order.h"
22 #include "dynamic-string.h"
25 #include "meta-flow.h"
26 #include "multipath.h"
28 #include "ofp-parse.h"
31 #include "unaligned.h"
35 VLOG_DEFINE_THIS_MODULE(ofp_actions);
37 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
39 struct ofp_action_header;
41 /* Raw identifiers for OpenFlow actions.
43 * Decoding and encoding OpenFlow actions across multiple versions is difficult
44 * to do in a clean, consistent way. This enumeration lays out all of the
45 * forms of actions that Open vSwitch supports.
47 * The comments here must follow a stylized form because the
48 * "extract-ofp-actions" program parses them at build time to generate data
51 * - The first part of each comment specifies the vendor, OpenFlow versions,
52 * and type for each protocol that supports the action:
54 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
55 * extension actions. (Support for other vendors can be added, but
56 * it can't be done just based on a vendor ID definition alone
57 * because OpenFlow doesn't define a standard way to specify a
58 * subtype for vendor actions, so other vendors might do it different
61 * # The version can specify a specific OpenFlow version, a version
62 * range delimited by "-", or an open-ended range with "+".
64 * # The type, in parentheses, is the action type number (for standard
65 * OpenFlow actions) or subtype (for vendor extension actions).
67 * # Optionally one may add "is deprecated" followed by a
68 * human-readable reason in parentheses (which will be used in log
69 * messages), if a particular action should no longer be used.
71 * Multiple such specifications may be separated by commas.
73 * - The second part describes the action's wire format. It may be:
75 * # "struct <name>": The struct fully specifies the wire format. The
76 * action is exactly the size of the struct. (Thus, the struct must
77 * be an exact multiple of 8 bytes in size.)
79 * # "struct <name>, ...": The struct specifies the beginning of the
80 * wire format. An instance of the action is either the struct's
81 * exact size, or a multiple of 8 bytes longer.
83 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
84 * vendor extension) header, followed by 0 or more pad bytes to align
85 * to a multiple of <N> bits, followed by an argument of the given
86 * type, followed by 0 or more pad bytes to bring the total action up
87 * to a multiple of 8 bytes.
89 * # "void": The action is just a (standard or vendor extension)
92 * - Optional additional text enclosed in square brackets is commentary for
95 enum ofp_raw_action_type {
96 /* ## ----------------- ## */
97 /* ## Standard actions. ## */
98 /* ## ----------------- ## */
100 /* OF1.0(0): struct ofp10_action_output. */
102 /* OF1.1+(0): struct ofp11_action_output. */
105 /* OF1.0(1): uint16_t. */
106 OFPAT_RAW10_SET_VLAN_VID,
107 /* OF1.0(2): uint8_t. */
108 OFPAT_RAW10_SET_VLAN_PCP,
110 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
112 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
113 * modification actions: the 1.0 versions push a VLAN header if none is
114 * present, but the 1.1 versions do not. That is the only reason that we
115 * distinguish their raw action types.] */
116 OFPAT_RAW11_SET_VLAN_VID,
117 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
118 OFPAT_RAW11_SET_VLAN_PCP,
120 /* OF1.1+(17): ovs_be16.
122 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
124 OFPAT_RAW11_PUSH_VLAN,
126 /* OF1.0(3): void. */
127 OFPAT_RAW10_STRIP_VLAN,
128 /* OF1.1+(18): void. */
129 OFPAT_RAW11_POP_VLAN,
131 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
132 * ofp_action_dl_addr. */
133 OFPAT_RAW_SET_DL_SRC,
135 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
136 * ofp_action_dl_addr. */
137 OFPAT_RAW_SET_DL_DST,
139 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
141 OFPAT_RAW_SET_NW_SRC,
143 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
145 OFPAT_RAW_SET_NW_DST,
147 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
148 OFPAT_RAW_SET_NW_TOS,
150 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
151 OFPAT_RAW11_SET_NW_ECN,
153 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
155 OFPAT_RAW_SET_TP_SRC,
157 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
159 OFPAT_RAW_SET_TP_DST,
161 /* OF1.0(11): struct ofp10_action_enqueue. */
164 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
166 OFPAT_RAW_SET_MPLS_LABEL,
168 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
170 OFPAT_RAW_SET_MPLS_TC,
172 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
174 OFPAT_RAW_SET_MPLS_TTL,
176 /* NX1.0(26), OF1.1+(16): void. */
177 OFPAT_RAW_DEC_MPLS_TTL,
179 /* NX1.0(23), OF1.1+(19): ovs_be16.
181 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
184 /* NX1.0(24), OF1.1+(20): ovs_be16.
186 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
187 * ETH_TYPE_MPLS otherwise, not the label.] */
190 /* NX1.0(4), OF1.1+(21): uint32_t. */
193 /* OF1.1+(22): uint32_t. */
196 /* OF1.1+(23): uint8_t. */
197 OFPAT_RAW11_SET_NW_TTL,
199 /* NX1.0(18), OF1.1+(24): void. */
200 OFPAT_RAW_DEC_NW_TTL,
201 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
202 NXAST_RAW_DEC_TTL_CNT_IDS,
204 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
205 OFPAT_RAW12_SET_FIELD,
206 /* OF1.5+(25): struct ofp12_action_set_field, ... */
207 OFPAT_RAW15_SET_FIELD,
208 /* NX1.0-1.4(7): struct nx_action_reg_load.
210 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
211 * we drop reg_load.] */
213 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
215 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
216 * we drop reg_load2.] */
219 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
220 OFPAT_RAW15_COPY_FIELD,
221 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
222 ONFACT_RAW13_COPY_FIELD,
223 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
226 /* ## ------------------------- ## */
227 /* ## Nicira extension actions. ## */
228 /* ## ------------------------- ## */
230 /* Actions similar to standard actions are listed with the standard actions. */
232 /* NX1.0+(1): uint16_t. */
234 /* NX1.0+(14): struct nx_action_resubmit. */
235 NXAST_RAW_RESUBMIT_TABLE,
237 /* NX1.0+(2): uint32_t. */
238 NXAST_RAW_SET_TUNNEL,
239 /* NX1.0+(9): uint64_t. */
240 NXAST_RAW_SET_TUNNEL64,
242 /* NX1.0+(5): void. */
245 /* NX1.0+(8): struct nx_action_note, ... */
248 /* NX1.0+(10): struct nx_action_multipath. */
251 /* NX1.0+(12): struct nx_action_bundle, ... */
253 /* NX1.0+(13): struct nx_action_bundle, ... */
254 NXAST_RAW_BUNDLE_LOAD,
256 /* NX1.0+(15): struct nx_action_output_reg. */
257 NXAST_RAW_OUTPUT_REG,
258 /* NX1.0+(32): struct nx_action_output_reg2. */
259 NXAST_RAW_OUTPUT_REG2,
261 /* NX1.0+(16): struct nx_action_learn, ... */
264 /* NX1.0+(17): void. */
267 /* NX1.0+(19): struct nx_action_fin_timeout. */
268 NXAST_RAW_FIN_TIMEOUT,
270 /* NX1.0+(20): struct nx_action_controller. */
271 NXAST_RAW_CONTROLLER,
273 /* NX1.0+(22): struct nx_action_write_metadata. */
274 NXAST_RAW_WRITE_METADATA,
276 /* NX1.0+(27): struct nx_action_stack. */
277 NXAST_RAW_STACK_PUSH,
279 /* NX1.0+(28): struct nx_action_stack. */
282 /* NX1.0+(29): struct nx_action_sample. */
286 /* OpenFlow actions are always a multiple of 8 bytes in length. */
287 #define OFP_ACTION_ALIGN 8
289 /* Define a few functions for working with instructions. */
290 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
291 static inline const struct STRUCT * OVS_UNUSED \
292 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
294 ovs_assert(inst->type == htons(ENUM)); \
295 return ALIGNED_CAST(struct STRUCT *, inst); \
298 static inline void OVS_UNUSED \
299 instruction_init_##ENUM(struct STRUCT *s) \
301 memset(s, 0, sizeof *s); \
302 s->type = htons(ENUM); \
303 s->len = htons(sizeof *s); \
306 static inline struct STRUCT * OVS_UNUSED \
307 instruction_put_##ENUM(struct ofpbuf *buf) \
309 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
310 instruction_init_##ENUM(s); \
316 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
318 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
320 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
321 uint32_t allowed_ovsinsts);
323 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
324 enum mf_field_id, uint64_t value);
326 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
327 enum ofp_raw_action_type *, uint64_t *arg);
328 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
329 enum ofp_raw_action_type, uint64_t arg);
331 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
332 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
333 bool allow_instructions);
335 #include "ofp-actions.inc1"
337 /* Output actions. */
339 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
340 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
341 * number of bytes to send. A 'max_len' of zero means no bytes of the
342 * packet should be sent. */
343 struct ofp10_action_output {
344 ovs_be16 type; /* OFPAT10_OUTPUT. */
345 ovs_be16 len; /* Length is 8. */
346 ovs_be16 port; /* Output port. */
347 ovs_be16 max_len; /* Max length to send to controller. */
349 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
351 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
352 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
353 * number of bytes to send. A 'max_len' of zero means no bytes of the
354 * packet should be sent.*/
355 struct ofp11_action_output {
356 ovs_be16 type; /* OFPAT11_OUTPUT. */
357 ovs_be16 len; /* Length is 16. */
358 ovs_be32 port; /* Output port. */
359 ovs_be16 max_len; /* Max length to send to controller. */
360 uint8_t pad[6]; /* Pad to 64 bits. */
362 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
365 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
368 struct ofpact_output *output;
370 output = ofpact_put_OUTPUT(out);
371 output->port = u16_to_ofp(ntohs(oao->port));
372 output->max_len = ntohs(oao->max_len);
374 return ofpact_check_output_port(output->port, OFPP_MAX);
378 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
381 struct ofpact_output *output;
384 output = ofpact_put_OUTPUT(out);
385 output->max_len = ntohs(oao->max_len);
387 error = ofputil_port_from_ofp11(oao->port, &output->port);
392 return ofpact_check_output_port(output->port, OFPP_MAX);
396 encode_OUTPUT(const struct ofpact_output *output,
397 enum ofp_version ofp_version, struct ofpbuf *out)
399 if (ofp_version == OFP10_VERSION) {
400 struct ofp10_action_output *oao;
402 oao = put_OFPAT10_OUTPUT(out);
403 oao->port = htons(ofp_to_u16(output->port));
404 oao->max_len = htons(output->max_len);
406 struct ofp11_action_output *oao;
408 oao = put_OFPAT11_OUTPUT(out);
409 oao->port = ofputil_port_to_ofp11(output->port);
410 oao->max_len = htons(output->max_len);
414 static char * OVS_WARN_UNUSED_RESULT
415 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
416 enum ofputil_protocol *usable_protocols OVS_UNUSED)
418 if (strchr(arg, '[')) {
419 struct ofpact_output_reg *output_reg;
421 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
422 output_reg->max_len = UINT16_MAX;
423 return mf_parse_subfield(&output_reg->src, arg);
425 struct ofpact_output *output;
427 output = ofpact_put_OUTPUT(ofpacts);
428 if (!ofputil_port_from_string(arg, &output->port)) {
429 return xasprintf("%s: output to unknown port", arg);
431 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
437 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
439 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
440 ds_put_format(s, "output:%"PRIu16, a->port);
442 ofputil_format_port(a->port, s);
443 if (a->port == OFPP_CONTROLLER) {
444 ds_put_format(s, ":%"PRIu16, a->max_len);
452 decode_OFPAT_RAW11_GROUP(uint32_t group_id, struct ofpbuf *out)
454 ofpact_put_GROUP(out)->group_id = group_id;
459 encode_GROUP(const struct ofpact_group *group,
460 enum ofp_version ofp_version, struct ofpbuf *out)
462 if (ofp_version == OFP10_VERSION) {
465 put_OFPAT11_GROUP(out, group->group_id);
469 static char * OVS_WARN_UNUSED_RESULT
470 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
471 enum ofputil_protocol *usable_protocols OVS_UNUSED)
473 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
477 format_GROUP(const struct ofpact_group *a, struct ds *s)
479 ds_put_format(s, "group:%"PRIu32, a->group_id);
482 /* Action structure for NXAST_CONTROLLER.
484 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
485 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
488 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
490 * - 'controller_id': The ID of the controller connection to which the
491 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
492 * sent only to controllers that have the specified controller connection
493 * ID. See "struct nx_controller_id" for more information. */
494 struct nx_action_controller {
495 ovs_be16 type; /* OFPAT_VENDOR. */
496 ovs_be16 len; /* Length is 16. */
497 ovs_be32 vendor; /* NX_VENDOR_ID. */
498 ovs_be16 subtype; /* NXAST_CONTROLLER. */
499 ovs_be16 max_len; /* Maximum length to send to controller. */
500 ovs_be16 controller_id; /* Controller ID to send packet-in. */
501 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
502 uint8_t zero; /* Must be zero. */
504 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
507 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
510 struct ofpact_controller *oc;
512 oc = ofpact_put_CONTROLLER(out);
513 oc->max_len = ntohs(nac->max_len);
514 oc->controller_id = ntohs(nac->controller_id);
515 oc->reason = nac->reason;
520 encode_CONTROLLER(const struct ofpact_controller *controller,
521 enum ofp_version ofp_version OVS_UNUSED,
524 struct nx_action_controller *nac;
526 nac = put_NXAST_CONTROLLER(out);
527 nac->max_len = htons(controller->max_len);
528 nac->controller_id = htons(controller->controller_id);
529 nac->reason = controller->reason;
532 static char * OVS_WARN_UNUSED_RESULT
533 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
534 enum ofputil_protocol *usable_protocols OVS_UNUSED)
536 enum ofp_packet_in_reason reason = OFPR_ACTION;
537 uint16_t controller_id = 0;
538 uint16_t max_len = UINT16_MAX;
542 } else if (strspn(arg, "0123456789") == strlen(arg)) {
543 char *error = str_to_u16(arg, "max_len", &max_len);
550 while (ofputil_parse_key_value(&arg, &name, &value)) {
551 if (!strcmp(name, "reason")) {
552 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
553 return xasprintf("unknown reason \"%s\"", value);
555 } else if (!strcmp(name, "max_len")) {
556 char *error = str_to_u16(value, "max_len", &max_len);
560 } else if (!strcmp(name, "id")) {
561 char *error = str_to_u16(value, "id", &controller_id);
566 return xasprintf("unknown key \"%s\" parsing controller "
572 if (reason == OFPR_ACTION && controller_id == 0) {
573 struct ofpact_output *output;
575 output = ofpact_put_OUTPUT(ofpacts);
576 output->port = OFPP_CONTROLLER;
577 output->max_len = max_len;
579 struct ofpact_controller *controller;
581 controller = ofpact_put_CONTROLLER(ofpacts);
582 controller->max_len = max_len;
583 controller->reason = reason;
584 controller->controller_id = controller_id;
591 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
593 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
594 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
596 enum ofp_packet_in_reason reason = a->reason;
598 ds_put_cstr(s, "controller(");
599 if (reason != OFPR_ACTION) {
600 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
602 ds_put_format(s, "reason=%s,",
603 ofputil_packet_in_reason_to_string(
604 reason, reasonbuf, sizeof reasonbuf));
606 if (a->max_len != UINT16_MAX) {
607 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
609 if (a->controller_id != 0) {
610 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
617 /* Enqueue action. */
618 struct ofp10_action_enqueue {
619 ovs_be16 type; /* OFPAT10_ENQUEUE. */
620 ovs_be16 len; /* Len is 16. */
621 ovs_be16 port; /* Port that queue belongs. Should
622 refer to a valid physical port
623 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
624 uint8_t pad[6]; /* Pad for 64-bit alignment. */
625 ovs_be32 queue_id; /* Where to enqueue the packets. */
627 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
630 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
633 struct ofpact_enqueue *enqueue;
635 enqueue = ofpact_put_ENQUEUE(out);
636 enqueue->port = u16_to_ofp(ntohs(oae->port));
637 enqueue->queue = ntohl(oae->queue_id);
638 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
639 && enqueue->port != OFPP_IN_PORT
640 && enqueue->port != OFPP_LOCAL) {
641 return OFPERR_OFPBAC_BAD_OUT_PORT;
647 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
648 enum ofp_version ofp_version, struct ofpbuf *out)
650 if (ofp_version == OFP10_VERSION) {
651 struct ofp10_action_enqueue *oae;
653 oae = put_OFPAT10_ENQUEUE(out);
654 oae->port = htons(ofp_to_u16(enqueue->port));
655 oae->queue_id = htonl(enqueue->queue);
661 static char * OVS_WARN_UNUSED_RESULT
662 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
663 enum ofputil_protocol *usable_protocols OVS_UNUSED)
666 char *port = strtok_r(arg, ":q,", &sp);
667 char *queue = strtok_r(NULL, "", &sp);
668 struct ofpact_enqueue *enqueue;
670 if (port == NULL || queue == NULL) {
671 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
672 "\"enqueue(PORT,QUEUE)\"");
675 enqueue = ofpact_put_ENQUEUE(ofpacts);
676 if (!ofputil_port_from_string(port, &enqueue->port)) {
677 return xasprintf("%s: enqueue to unknown port", port);
679 return str_to_u32(queue, &enqueue->queue);
683 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
685 ds_put_format(s, "enqueue:");
686 ofputil_format_port(a->port, s);
687 ds_put_format(s, ":%"PRIu32, a->queue);
690 /* Action structure for NXAST_OUTPUT_REG.
692 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
694 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
695 * the NXAST_REG_LOAD action.
697 * The acceptable nxm_header values for 'src' are the same as the acceptable
698 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
700 * The 'max_len' field indicates the number of bytes to send when the chosen
701 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
702 * field of OFPAT_OUTPUT.
704 * The 'zero' field is required to be zeroed for forward compatibility. */
705 struct nx_action_output_reg {
706 ovs_be16 type; /* OFPAT_VENDOR. */
707 ovs_be16 len; /* 24. */
708 ovs_be32 vendor; /* NX_VENDOR_ID. */
709 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
711 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
712 ovs_be32 src; /* Source. */
714 ovs_be16 max_len; /* Max length to send to controller. */
716 uint8_t zero[6]; /* Reserved, must be zero. */
718 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
720 /* Action structure for NXAST_OUTPUT_REG2.
722 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
723 * experimenter OXM as 'src'.
725 struct nx_action_output_reg2 {
726 ovs_be16 type; /* OFPAT_VENDOR. */
727 ovs_be16 len; /* 24. */
728 ovs_be32 vendor; /* NX_VENDOR_ID. */
729 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
731 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
732 ovs_be16 max_len; /* Max length to send to controller. */
735 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
736 * - Enough 0-bytes to pad the action out to 24 bytes. */
739 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
742 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
745 struct ofpact_output_reg *output_reg;
747 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
748 return OFPERR_OFPBAC_BAD_ARGUMENT;
751 output_reg = ofpact_put_OUTPUT_REG(out);
752 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
753 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
754 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
755 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
756 output_reg->max_len = ntohs(naor->max_len);
758 return mf_check_src(&output_reg->src, NULL);
762 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
765 struct ofpact_output_reg *output_reg;
769 output_reg = ofpact_put_OUTPUT_REG(out);
770 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
771 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
772 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
773 output_reg->max_len = ntohs(naor->max_len);
775 ofpbuf_use_const(&b, naor, ntohs(naor->len));
776 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
777 error = nx_pull_header(&b, &output_reg->src.field, NULL);
781 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
782 return OFPERR_NXBRC_MUST_BE_ZERO;
785 return mf_check_src(&output_reg->src, NULL);
789 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
790 enum ofp_version ofp_version OVS_UNUSED,
793 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
794 * cannot be encoded in the older form, encode it as
795 * NXAST_RAW_OUTPUT_REG2. */
796 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
797 || !mf_nxm_header(output_reg->src.field->id)) {
798 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
799 size_t size = ofpbuf_size(out);
801 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
802 output_reg->src.n_bits);
803 naor->max_len = htons(output_reg->max_len);
805 ofpbuf_set_size(out, size - sizeof naor->pad);
806 nx_put_header(out, output_reg->src.field->id, 0, false);
807 ofpbuf_set_size(out, size);
809 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
811 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
812 output_reg->src.n_bits);
813 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
814 naor->max_len = htons(output_reg->max_len);
818 static char * OVS_WARN_UNUSED_RESULT
819 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
820 enum ofputil_protocol *usable_protocols OVS_UNUSED)
822 return parse_OUTPUT(arg, ofpacts, usable_protocols);
826 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
828 ds_put_cstr(s, "output:");
829 mf_format_subfield(&a->src, s);
832 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
834 * The bundle actions choose a slave from a supplied list of options.
835 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
836 * selection to a register.
838 * The list of possible slaves follows the nx_action_bundle structure. The size
839 * of each slave is governed by its type as indicated by the 'slave_type'
840 * parameter. The list of slaves should be padded at its end with zeros to make
841 * the total length of the action a multiple of 8.
843 * Switches infer from the 'slave_type' parameter the size of each slave. All
844 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
845 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
846 * 2 byte width. Switches should reject actions which indicate unknown or
847 * unsupported slave types.
849 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
850 * slave. If the switch does not support the specified 'algorithm' parameter,
851 * it should reject the action.
853 * Several algorithms take into account liveness when selecting slaves. The
854 * liveness of a slave is implementation defined (with one exception), but will
855 * generally take into account things like its carrier status and the results
856 * of any link monitoring protocols which happen to be running on it. In order
857 * to give controllers a place-holder value, the OFPP_NONE port is always
860 * Some slave selection strategies require the use of a hash function, in which
861 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
862 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
863 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
864 * 'basis' parameter is used as a universal hash parameter. Different values
865 * of 'basis' yield different hash results.
867 * The 'zero' parameter at the end of the action structure is reserved for
868 * future use. Switches are required to reject actions which have nonzero
869 * bytes in the 'zero' field.
871 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
872 * should reject actions which have nonzero bytes in either of these fields.
874 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
875 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
876 * similar to those for the NXAST_REG_LOAD action. */
877 struct nx_action_bundle {
878 ovs_be16 type; /* OFPAT_VENDOR. */
879 ovs_be16 len; /* Length including slaves. */
880 ovs_be32 vendor; /* NX_VENDOR_ID. */
881 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
883 /* Slave choice algorithm to apply to hash value. */
884 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
886 /* What fields to hash and how. */
887 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
888 ovs_be16 basis; /* Universal hash parameter. */
890 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
891 ovs_be16 n_slaves; /* Number of slaves. */
893 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
894 ovs_be32 dst; /* Destination. */
896 uint8_t zero[4]; /* Reserved. Must be zero. */
898 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
901 decode_bundle(bool load, const struct nx_action_bundle *nab,
902 struct ofpbuf *ofpacts)
904 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
905 struct ofpact_bundle *bundle;
907 size_t slaves_size, i;
910 bundle = ofpact_put_BUNDLE(ofpacts);
912 bundle->n_slaves = ntohs(nab->n_slaves);
913 bundle->basis = ntohs(nab->basis);
914 bundle->fields = ntohs(nab->fields);
915 bundle->algorithm = ntohs(nab->algorithm);
916 slave_type = ntohl(nab->slave_type);
917 slaves_size = ntohs(nab->len) - sizeof *nab;
919 error = OFPERR_OFPBAC_BAD_ARGUMENT;
920 if (!flow_hash_fields_valid(bundle->fields)) {
921 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
922 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
923 VLOG_WARN_RL(&rl, "too many slaves");
924 } else if (bundle->algorithm != NX_BD_ALG_HRW
925 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
926 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
927 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
928 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
933 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
934 VLOG_WARN_RL(&rl, "reserved field is nonzero");
935 error = OFPERR_OFPBAC_BAD_ARGUMENT;
939 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
940 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
941 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
943 if (bundle->dst.n_bits < 16) {
944 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
946 error = OFPERR_OFPBAC_BAD_ARGUMENT;
949 if (nab->ofs_nbits || nab->dst) {
950 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
951 error = OFPERR_OFPBAC_BAD_ARGUMENT;
955 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
956 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
957 "allocated for slaves. %"PRIuSIZE" bytes are required "
958 "for %"PRIu16" slaves.",
959 load ? "bundle_load" : "bundle", slaves_size,
960 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
961 error = OFPERR_OFPBAC_BAD_LEN;
964 for (i = 0; i < bundle->n_slaves; i++) {
965 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
966 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
969 bundle = ofpacts->frame;
970 ofpact_update_len(ofpacts, &bundle->ofpact);
973 error = bundle_check(bundle, OFPP_MAX, NULL);
979 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab, struct ofpbuf *out)
981 return decode_bundle(false, nab, out);
985 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
988 return decode_bundle(true, nab, out);
992 encode_BUNDLE(const struct ofpact_bundle *bundle,
993 enum ofp_version ofp_version OVS_UNUSED,
996 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
997 struct nx_action_bundle *nab;
1001 nab = (bundle->dst.field
1002 ? put_NXAST_BUNDLE_LOAD(out)
1003 : put_NXAST_BUNDLE(out));
1004 nab->len = htons(ntohs(nab->len) + slaves_len);
1005 nab->algorithm = htons(bundle->algorithm);
1006 nab->fields = htons(bundle->fields);
1007 nab->basis = htons(bundle->basis);
1008 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1009 nab->n_slaves = htons(bundle->n_slaves);
1010 if (bundle->dst.field) {
1011 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1012 bundle->dst.n_bits);
1013 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1016 slaves = ofpbuf_put_zeros(out, slaves_len);
1017 for (i = 0; i < bundle->n_slaves; i++) {
1018 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1022 static char * OVS_WARN_UNUSED_RESULT
1023 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1024 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1026 return bundle_parse(arg, ofpacts);
1029 static char * OVS_WARN_UNUSED_RESULT
1030 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1032 return bundle_parse_load(arg, ofpacts);
1036 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1038 bundle_format(a, s);
1041 /* Set VLAN actions. */
1044 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1047 return OFPERR_OFPBAC_BAD_ARGUMENT;
1049 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1050 vlan_vid->vlan_vid = vid;
1051 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1057 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid, struct ofpbuf *out)
1059 return decode_set_vlan_vid(vid, true, out);
1063 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid, struct ofpbuf *out)
1065 return decode_set_vlan_vid(vid, false, out);
1069 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1070 enum ofp_version ofp_version, struct ofpbuf *out)
1072 uint16_t vid = vlan_vid->vlan_vid;
1074 /* Push a VLAN tag, if none is present and this form of the action calls
1075 * for such a feature. */
1076 if (ofp_version > OFP10_VERSION
1077 && vlan_vid->push_vlan_if_needed
1078 && !vlan_vid->flow_has_vlan) {
1079 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1082 if (ofp_version == OFP10_VERSION) {
1083 put_OFPAT10_SET_VLAN_VID(out, vid);
1084 } else if (ofp_version == OFP11_VERSION) {
1085 put_OFPAT11_SET_VLAN_VID(out, vid);
1087 ofpact_put_set_field(out, ofp_version,
1088 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1092 static char * OVS_WARN_UNUSED_RESULT
1093 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1095 struct ofpact_vlan_vid *vlan_vid;
1099 error = str_to_u16(arg, "VLAN VID", &vid);
1104 if (vid & ~VLAN_VID_MASK) {
1105 return xasprintf("%s: not a valid VLAN VID", arg);
1107 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1108 vlan_vid->vlan_vid = vid;
1109 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1113 static char * OVS_WARN_UNUSED_RESULT
1114 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1115 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1117 return parse_set_vlan_vid(arg, ofpacts, false);
1121 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1123 ds_put_format(s, "%s:%"PRIu16,
1124 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1128 /* Set PCP actions. */
1131 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1134 return OFPERR_OFPBAC_BAD_ARGUMENT;
1136 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1137 vlan_pcp->vlan_pcp = pcp;
1138 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1144 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp, struct ofpbuf *out)
1146 return decode_set_vlan_pcp(pcp, true, out);
1150 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp, struct ofpbuf *out)
1152 return decode_set_vlan_pcp(pcp, false, out);
1156 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1157 enum ofp_version ofp_version, struct ofpbuf *out)
1159 uint8_t pcp = vlan_pcp->vlan_pcp;
1161 /* Push a VLAN tag, if none is present and this form of the action calls
1162 * for such a feature. */
1163 if (ofp_version > OFP10_VERSION
1164 && vlan_pcp->push_vlan_if_needed
1165 && !vlan_pcp->flow_has_vlan) {
1166 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1169 if (ofp_version == OFP10_VERSION) {
1170 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1171 } else if (ofp_version == OFP11_VERSION) {
1172 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1174 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1178 static char * OVS_WARN_UNUSED_RESULT
1179 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1181 struct ofpact_vlan_pcp *vlan_pcp;
1185 error = str_to_u8(arg, "VLAN PCP", &pcp);
1191 return xasprintf("%s: not a valid VLAN PCP", arg);
1193 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1194 vlan_pcp->vlan_pcp = pcp;
1195 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1199 static char * OVS_WARN_UNUSED_RESULT
1200 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1201 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1203 return parse_set_vlan_pcp(arg, ofpacts, false);
1207 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1209 ds_put_format(s, "%s:%"PRIu8,
1210 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1214 /* Strip VLAN actions. */
1217 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1219 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1224 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1226 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1231 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1232 enum ofp_version ofp_version, struct ofpbuf *out)
1234 if (ofp_version == OFP10_VERSION) {
1235 put_OFPAT10_STRIP_VLAN(out);
1237 put_OFPAT11_POP_VLAN(out);
1241 static char * OVS_WARN_UNUSED_RESULT
1242 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1243 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1245 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1249 static char * OVS_WARN_UNUSED_RESULT
1250 parse_pop_vlan(struct ofpbuf *ofpacts)
1252 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1257 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1259 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1264 /* Push VLAN action. */
1267 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type, struct ofpbuf *out)
1269 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1270 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1271 return OFPERR_OFPBAC_BAD_ARGUMENT;
1273 ofpact_put_PUSH_VLAN(out);
1278 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1279 enum ofp_version ofp_version, struct ofpbuf *out)
1281 if (ofp_version == OFP10_VERSION) {
1282 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1283 * follow this action. */
1285 /* XXX ETH_TYPE_VLAN_8021AD case */
1286 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1290 static char * OVS_WARN_UNUSED_RESULT
1291 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1292 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1297 *usable_protocols &= OFPUTIL_P_OF11_UP;
1298 error = str_to_u16(arg, "ethertype", ðertype);
1303 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1304 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1305 return xasprintf("%s: not a valid VLAN ethertype", arg);
1308 ofpact_put_PUSH_VLAN(ofpacts);
1313 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1315 /* XXX 802.1AD case*/
1316 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1319 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1320 struct ofp_action_dl_addr {
1321 ovs_be16 type; /* Type. */
1322 ovs_be16 len; /* Length is 16. */
1323 uint8_t dl_addr[OFP_ETH_ALEN]; /* Ethernet address. */
1326 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1329 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1332 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr, ETH_ADDR_LEN);
1337 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1340 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr, ETH_ADDR_LEN);
1345 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1346 enum ofp_raw_action_type raw, enum mf_field_id field,
1349 const uint8_t *addr = mac->mac;
1351 if (ofp_version < OFP12_VERSION) {
1352 struct ofp_action_dl_addr *oada;
1354 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1355 memcpy(oada->dl_addr, addr, ETH_ADDR_LEN);
1357 ofpact_put_set_field(out, ofp_version, field,
1358 eth_addr_to_uint64(addr));
1363 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1366 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1372 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1373 enum ofp_version ofp_version,
1376 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1381 static char * OVS_WARN_UNUSED_RESULT
1382 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1383 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1385 return str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1388 static char * OVS_WARN_UNUSED_RESULT
1389 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1390 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1392 return str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
1396 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1398 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1402 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1404 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1407 /* Set IPv4 address actions. */
1410 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4, struct ofpbuf *out)
1412 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1417 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4, struct ofpbuf *out)
1419 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1424 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1425 enum ofp_version ofp_version,
1426 enum ofp_raw_action_type raw, enum mf_field_id field,
1429 ovs_be32 addr = ipv4->ipv4;
1430 if (ofp_version < OFP12_VERSION) {
1431 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1433 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1438 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1439 enum ofp_version ofp_version, struct ofpbuf *out)
1441 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1446 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1447 enum ofp_version ofp_version, struct ofpbuf *out)
1449 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1453 static char * OVS_WARN_UNUSED_RESULT
1454 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1455 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1457 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1460 static char * OVS_WARN_UNUSED_RESULT
1461 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1462 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1464 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1468 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1470 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1474 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1476 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1479 /* Set IPv4/v6 TOS actions. */
1482 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp, struct ofpbuf *out)
1484 if (dscp & ~IP_DSCP_MASK) {
1485 return OFPERR_OFPBAC_BAD_ARGUMENT;
1487 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1493 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1494 enum ofp_version ofp_version, struct ofpbuf *out)
1496 if (ofp_version < OFP12_VERSION) {
1497 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1499 ofpact_put_set_field(out, ofp_version,
1500 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1504 static char * OVS_WARN_UNUSED_RESULT
1505 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1506 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1511 error = str_to_u8(arg, "TOS", &tos);
1516 if (tos & ~IP_DSCP_MASK) {
1517 return xasprintf("%s: not a valid TOS", arg);
1519 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1524 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1526 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1529 /* Set IPv4/v6 ECN actions. */
1532 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn, struct ofpbuf *out)
1534 if (ecn & ~IP_ECN_MASK) {
1535 return OFPERR_OFPBAC_BAD_ARGUMENT;
1537 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1543 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1544 enum ofp_version ofp_version, struct ofpbuf *out)
1546 uint8_t ecn = ip_ecn->ecn;
1547 if (ofp_version == OFP10_VERSION) {
1549 } else if (ofp_version == OFP11_VERSION) {
1550 put_OFPAT11_SET_NW_ECN(out, ecn);
1552 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1556 static char * OVS_WARN_UNUSED_RESULT
1557 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1558 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1563 error = str_to_u8(arg, "ECN", &ecn);
1568 if (ecn & ~IP_ECN_MASK) {
1569 return xasprintf("%s: not a valid ECN", arg);
1571 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1576 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1578 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1581 /* Set IPv4/v6 TTL actions. */
1584 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl, struct ofpbuf *out)
1586 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1591 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1592 enum ofp_version ofp_version, struct ofpbuf *out)
1594 if (ofp_version >= OFP11_VERSION) {
1595 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1601 static char * OVS_WARN_UNUSED_RESULT
1602 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1603 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1608 error = str_to_u8(arg, "TTL", &ttl);
1613 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1618 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1620 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1623 /* Set TCP/UDP/SCTP port actions. */
1626 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port, struct ofpbuf *out)
1628 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1633 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port, struct ofpbuf *out)
1635 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1640 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1641 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1642 enum mf_field_id field, struct ofpbuf *out)
1644 uint16_t port = l4_port->port;
1646 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1647 ofpact_put_set_field(out, ofp_version, field, port);
1649 ofpact_put_raw(out, ofp_version, raw, port);
1654 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1655 enum ofp_version ofp_version, struct ofpbuf *out)
1657 uint8_t proto = l4_port->flow_ip_proto;
1658 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1659 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1660 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1663 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1667 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1668 enum ofp_version ofp_version,
1671 uint8_t proto = l4_port->flow_ip_proto;
1672 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1673 : proto == IPPROTO_UDP ? MFF_UDP_DST
1674 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1677 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1680 static char * OVS_WARN_UNUSED_RESULT
1681 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1682 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1684 return str_to_u16(arg, "source port",
1685 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1688 static char * OVS_WARN_UNUSED_RESULT
1689 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1690 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1692 return str_to_u16(arg, "destination port",
1693 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1697 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1699 ds_put_format(s, "mod_tp_src:%d", a->port);
1703 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1705 ds_put_format(s, "mod_tp_dst:%d", a->port);
1708 /* Action structure for OFPAT_COPY_FIELD. */
1709 struct ofp15_action_copy_field {
1710 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1711 ovs_be16 len; /* Length is padded to 64 bits. */
1712 ovs_be16 n_bits; /* Number of bits to copy. */
1713 ovs_be16 src_offset; /* Starting bit offset in source. */
1714 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1717 * - OXM header for source field.
1718 * - OXM header for destination field.
1719 * - Padding with 0-bytes to a multiple of 8 bytes.
1720 * The "pad2" member is the beginning of the above. */
1723 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1725 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1726 struct onf_action_copy_field {
1727 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
1728 ovs_be16 len; /* Length is padded to 64 bits. */
1729 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
1730 ovs_be16 exp_type; /* 3200. */
1731 uint8_t pad[2]; /* Not used. */
1732 ovs_be16 n_bits; /* Number of bits to copy. */
1733 ovs_be16 src_offset; /* Starting bit offset in source. */
1734 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1735 uint8_t pad2[2]; /* Not used. */
1737 * - OXM header for source field.
1738 * - OXM header for destination field.
1739 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1740 * The "pad3" member is the beginning of the above. */
1741 uint8_t pad3[4]; /* Not used. */
1743 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1745 /* Action structure for NXAST_REG_MOVE.
1747 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1748 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1749 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1750 * the next most significant bit, and so on.
1752 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1753 * sense to use nxm_hasmask=1 because the action does not do any kind of
1754 * matching; it uses the actual value of a field.)
1756 * The following nxm_header values are potentially acceptable as 'src':
1771 * - NXM_OF_ICMP_TYPE
1772 * - NXM_OF_ICMP_CODE
1779 * - NXM_NX_ICMPV6_TYPE
1780 * - NXM_NX_ICMPV6_CODE
1783 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1785 * - NXM_NX_TUN_IPV4_SRC
1786 * - NXM_NX_TUN_IPV4_DST
1788 * The following nxm_header values are potentially acceptable as 'dst':
1804 * Modifying any of the above fields changes the corresponding packet
1809 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1813 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1814 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1815 * header (if any), ignoring the other bits. Setting a value with CFI=1
1816 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1817 * to the field's new value (with the CFI bit masked out).
1819 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1820 * any of these values modifies the corresponding tunnel header field used
1821 * for the packet's next tunnel encapsulation, if allowed by the
1822 * configuration of the output tunnel port.
1824 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1825 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1826 * used only if the flow's nx_match includes an nxm_entry that specifies
1827 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1829 * The switch will reject actions for which src_ofs+n_bits is greater than the
1830 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1831 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1833 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1834 * behaves as if 'src' were copied out to a temporary buffer, then the
1835 * temporary buffer copied to 'dst'.
1837 struct nx_action_reg_move {
1838 ovs_be16 type; /* OFPAT_VENDOR. */
1839 ovs_be16 len; /* Length is 24. */
1840 ovs_be32 vendor; /* NX_VENDOR_ID. */
1841 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1842 ovs_be16 n_bits; /* Number of bits. */
1843 ovs_be16 src_ofs; /* Starting bit offset in source. */
1844 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1846 * - OXM/NXM header for source field (4 or 8 bytes).
1847 * - OXM/NXM header for destination field (4 or 8 bytes).
1848 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
1850 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
1853 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
1854 const void *action, ovs_be16 action_len, size_t oxm_offset,
1855 struct ofpbuf *ofpacts)
1857 struct ofpact_reg_move *move;
1861 move = ofpact_put_REG_MOVE(ofpacts);
1862 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
1863 move->src.ofs = ntohs(src_offset);
1864 move->src.n_bits = ntohs(n_bits);
1865 move->dst.ofs = ntohs(dst_offset);
1866 move->dst.n_bits = ntohs(n_bits);
1868 ofpbuf_use_const(&b, action, ntohs(action_len));
1869 ofpbuf_pull(&b, oxm_offset);
1870 error = nx_pull_header(&b, &move->src.field, NULL);
1874 error = nx_pull_header(&b, &move->dst.field, NULL);
1879 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
1880 return OFPERR_NXBRC_MUST_BE_ZERO;
1883 return nxm_reg_move_check(move, NULL);
1887 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
1888 struct ofpbuf *ofpacts)
1890 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1891 oacf->n_bits, oacf, oacf->len,
1892 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
1896 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
1897 struct ofpbuf *ofpacts)
1899 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
1900 oacf->n_bits, oacf, oacf->len,
1901 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
1905 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
1906 struct ofpbuf *ofpacts)
1908 struct ofpact_reg_move *move;
1912 move = ofpact_put_REG_MOVE(ofpacts);
1913 move->ofpact.raw = NXAST_RAW_REG_MOVE;
1914 move->src.ofs = ntohs(narm->src_ofs);
1915 move->src.n_bits = ntohs(narm->n_bits);
1916 move->dst.ofs = ntohs(narm->dst_ofs);
1917 move->dst.n_bits = ntohs(narm->n_bits);
1919 ofpbuf_use_const(&b, narm, ntohs(narm->len));
1920 ofpbuf_pull(&b, sizeof *narm);
1921 error = nx_pull_header(&b, &move->src.field, NULL);
1925 error = nx_pull_header(&b, &move->dst.field, NULL);
1929 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
1930 return OFPERR_NXBRC_MUST_BE_ZERO;
1933 return nxm_reg_move_check(move, NULL);
1937 encode_REG_MOVE(const struct ofpact_reg_move *move,
1938 enum ofp_version ofp_version, struct ofpbuf *out)
1940 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
1941 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
1942 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
1943 * probe for support. Until we have that ability, we currently prefer
1944 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
1946 size_t start_ofs = ofpbuf_size(out);
1947 if (ofp_version >= OFP15_VERSION) {
1948 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
1949 copy->n_bits = htons(move->dst.n_bits);
1950 copy->src_offset = htons(move->src.ofs);
1951 copy->dst_offset = htons(move->dst.ofs);
1952 ofpbuf_set_size(out, ofpbuf_size(out) - sizeof copy->pad2);
1953 nx_put_header(out, move->src.field->id, ofp_version, false);
1954 nx_put_header(out, move->dst.field->id, ofp_version, false);
1955 } else if (ofp_version == OFP13_VERSION
1956 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
1957 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
1958 copy->n_bits = htons(move->dst.n_bits);
1959 copy->src_offset = htons(move->src.ofs);
1960 copy->dst_offset = htons(move->dst.ofs);
1961 ofpbuf_set_size(out, ofpbuf_size(out) - sizeof copy->pad3);
1962 nx_put_header(out, move->src.field->id, ofp_version, false);
1963 nx_put_header(out, move->dst.field->id, ofp_version, false);
1965 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
1966 narm->n_bits = htons(move->dst.n_bits);
1967 narm->src_ofs = htons(move->src.ofs);
1968 narm->dst_ofs = htons(move->dst.ofs);
1969 nx_put_header(out, move->src.field->id, 0, false);
1970 nx_put_header(out, move->dst.field->id, 0, false);
1972 pad_ofpat(out, start_ofs);
1975 static char * OVS_WARN_UNUSED_RESULT
1976 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
1977 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1979 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
1980 const char *full_arg = arg;
1983 error = mf_parse_subfield__(&move->src, &arg);
1987 if (strncmp(arg, "->", 2)) {
1988 return xasprintf("%s: missing `->' following source", full_arg);
1991 error = mf_parse_subfield(&move->dst, arg);
1996 if (move->src.n_bits != move->dst.n_bits) {
1997 return xasprintf("%s: source field is %d bits wide but destination is "
1998 "%d bits wide", full_arg,
1999 move->src.n_bits, move->dst.n_bits);
2005 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2007 nxm_format_reg_move(a, s);
2010 /* Action structure for OFPAT12_SET_FIELD. */
2011 struct ofp12_action_set_field {
2012 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2013 ovs_be16 len; /* Length is padded to 64 bits. */
2016 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2017 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2019 * The "pad" member is the beginning of the above. */
2022 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2024 /* Action structure for NXAST_REG_LOAD.
2026 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2027 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2028 * starts at 0 for the least-significant bit, 1 for the next most significant
2031 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2032 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2035 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2036 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2037 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2038 * also stored as one less than its true value:
2041 * +------------------------------+------------------+
2042 * | ofs | n_bits - 1 |
2043 * +------------------------------+------------------+
2045 * The switch will reject actions for which ofs+n_bits is greater than the
2046 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2047 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2048 * OFPBAC_BAD_ARGUMENT.
2050 struct nx_action_reg_load {
2051 ovs_be16 type; /* OFPAT_VENDOR. */
2052 ovs_be16 len; /* Length is 24. */
2053 ovs_be32 vendor; /* NX_VENDOR_ID. */
2054 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2055 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2056 ovs_be32 dst; /* Destination register. */
2057 ovs_be64 value; /* Immediate value. */
2059 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2061 /* Action structure for NXAST_REG_LOAD2.
2063 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2064 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2065 * OXM experimenter fields. */
2066 struct nx_action_reg_load2 {
2067 ovs_be16 type; /* OFPAT_VENDOR. */
2068 ovs_be16 len; /* At least 16. */
2069 ovs_be32 vendor; /* NX_VENDOR_ID. */
2070 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2073 * - An NXM/OXM header, value, and optionally a mask.
2074 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2076 * The "pad" member is the beginning of the above. */
2079 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2082 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2083 bool may_mask, struct ofpbuf *ofpacts)
2085 struct ofpact_set_field *sf;
2089 sf = ofpact_put_SET_FIELD(ofpacts);
2091 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2092 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2093 error = nx_pull_entry(&b, &sf->field, &sf->value,
2094 may_mask ? &sf->mask : NULL);
2096 return (error == OFPERR_OFPBMC_BAD_MASK
2097 ? OFPERR_OFPBAC_BAD_SET_MASK
2101 memset(&sf->mask, 0xff, sf->field->n_bytes);
2104 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
2105 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2108 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2110 if (sf->field->id == MFF_IN_PORT_OXM) {
2111 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2114 /* oxm_length is now validated to be compatible with mf_value. */
2115 if (!sf->field->writable) {
2116 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2118 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2121 /* The value must be valid for match. The OpenFlow 1.5 draft also says,
2122 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2123 * a 1-bit in oxm_value and in oxm_mask." */
2124 if (!mf_is_value_valid(sf->field, &sf->value)
2125 || (sf->field->id == MFF_VLAN_VID
2126 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2127 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2128 struct ds ds = DS_EMPTY_INITIALIZER;
2129 mf_format(sf->field, &sf->value, NULL, &ds);
2130 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2131 sf->field->name, ds_cstr(&ds));
2134 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2140 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2141 struct ofpbuf *ofpacts)
2143 return decode_ofpat_set_field(oasf, false, ofpacts);
2147 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2148 struct ofpbuf *ofpacts)
2150 return decode_ofpat_set_field(oasf, true, ofpacts);
2154 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2157 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2158 struct mf_subfield dst;
2161 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2163 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2164 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2165 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2166 error = mf_check_dst(&dst, NULL);
2171 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2173 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2174 return OFPERR_OFPBAC_BAD_ARGUMENT;
2177 sf->field = dst.field;
2178 bitwise_put(ntohll(narl->value),
2179 &sf->value, dst.field->n_bytes, dst.ofs,
2181 bitwise_put(UINT64_MAX,
2182 &sf->mask, dst.field->n_bytes, dst.ofs,
2189 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2192 struct ofpact_set_field *sf;
2196 sf = ofpact_put_SET_FIELD(out);
2197 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2199 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2200 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2201 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2205 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
2206 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2209 if (!sf->field->writable) {
2210 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2212 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2218 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2219 enum mf_field_id field, uint64_t value_)
2221 struct ofp12_action_set_field *oasf OVS_UNUSED;
2222 int n_bytes = mf_from_id(field)->n_bytes;
2223 size_t start_ofs = ofpbuf_size(openflow);
2224 union mf_value value;
2226 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2228 oasf = put_OFPAT12_SET_FIELD(openflow);
2229 ofpbuf_set_size(openflow, ofpbuf_size(openflow) - sizeof oasf->pad);
2230 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2231 pad_ofpat(openflow, start_ofs);
2235 next_load_segment(const struct ofpact_set_field *sf,
2236 struct mf_subfield *dst, uint64_t *value)
2238 int n_bits = sf->field->n_bits;
2239 int n_bytes = sf->field->n_bytes;
2240 int start = dst->ofs + dst->n_bits;
2242 if (start < n_bits) {
2243 dst->field = sf->field;
2244 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2245 if (dst->ofs < n_bits) {
2246 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2247 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2248 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2255 /* Convert 'sf' to a series of REG_LOADs. */
2257 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2259 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2260 * experimenter OXM (or if it came in as NXAST_REG_LOAD2), encode as
2261 * NXAST_REG_LOAD2. Otherwise use NXAST_REG_LOAD, which is backward
2263 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2264 || !mf_nxm_header(sf->field->id)) {
2265 struct nx_action_reg_load2 *narl OVS_UNUSED;
2266 size_t start_ofs = ofpbuf_size(openflow);
2268 narl = put_NXAST_REG_LOAD2(openflow);
2269 ofpbuf_set_size(openflow, ofpbuf_size(openflow) - sizeof narl->pad);
2270 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2271 pad_ofpat(openflow, start_ofs);
2273 struct mf_subfield dst;
2276 dst.ofs = dst.n_bits = 0;
2277 while (next_load_segment(sf, &dst, &value)) {
2278 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2279 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2280 narl->dst = htonl(mf_nxm_header(dst.field->id));
2281 narl->value = htonll(value);
2286 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2287 * actions, if we can, falling back to Nicira extensions if we must.
2289 * We check only meta-flow types that can appear within set field actions and
2290 * that have a mapping to compatible action types. These struct mf_field
2291 * definitions have a defined OXM or NXM header value and specify the field as
2294 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2295 enum ofp_version ofp_version,
2298 switch ((int) sf->field->id) {
2299 case MFF_VLAN_TCI: {
2300 ovs_be16 tci = sf->value.be16;
2301 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2302 uint16_t vid = vlan_tci_to_vid(tci);
2303 uint8_t pcp = vlan_tci_to_pcp(tci);
2305 if (ofp_version < OFP11_VERSION) {
2306 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2308 * If CFI=1, Add or modify VLAN VID & PCP.
2309 * If CFI=0, strip VLAN header, if any.
2312 put_OFPAT10_SET_VLAN_VID(out, vid);
2313 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2315 put_OFPAT10_STRIP_VLAN(out);
2318 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2320 * If CFI=1, Add or modify VLAN VID & PCP.
2321 * OpenFlow 1.1 set actions only apply if the packet
2322 * already has VLAN tags. To be sure that is the case
2323 * we have to push a VLAN header. As we do not support
2324 * multiple layers of VLANs, this is a no-op, if a VLAN
2325 * header already exists. This may backfire, however,
2326 * when we start supporting multiple layers of VLANs.
2327 * If CFI=0, strip VLAN header, if any.
2330 /* Push a VLAN tag, if one was not seen at action validation
2332 if (!sf->flow_has_vlan) {
2333 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2335 put_OFPAT11_SET_VLAN_VID(out, vid);
2336 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2338 /* If the flow did not match on vlan, we have no way of
2339 * knowing if the vlan tag exists, so we must POP just to be
2341 put_OFPAT11_POP_VLAN(out);
2347 case MFF_VLAN_VID: {
2348 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2349 if (ofp_version == OFP10_VERSION) {
2350 put_OFPAT10_SET_VLAN_VID(out, vid);
2352 put_OFPAT11_SET_VLAN_VID(out, vid);
2358 if (ofp_version == OFP10_VERSION) {
2359 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2361 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2366 memcpy(put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr,
2367 sf->value.mac, ETH_ADDR_LEN);
2371 memcpy(put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr,
2372 sf->value.mac, ETH_ADDR_LEN);
2376 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2380 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2384 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2387 case MFF_IP_DSCP_SHIFTED:
2388 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2393 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2398 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2402 set_field_to_nxast(sf, out);
2408 set_field_to_set_field(const struct ofpact_set_field *sf,
2409 enum ofp_version ofp_version, struct ofpbuf *out)
2411 struct ofp12_action_set_field *oasf OVS_UNUSED;
2412 size_t start_ofs = ofpbuf_size(out);
2414 oasf = put_OFPAT12_SET_FIELD(out);
2415 ofpbuf_set_size(out, ofpbuf_size(out) - sizeof oasf->pad);
2416 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2417 pad_ofpat(out, start_ofs);
2421 encode_SET_FIELD(const struct ofpact_set_field *sf,
2422 enum ofp_version ofp_version, struct ofpbuf *out)
2424 if (ofp_version >= OFP15_VERSION) {
2425 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2427 set_field_to_set_field(sf, ofp_version, out);
2428 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2429 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2430 /* It came in as reg_load, send it out the same way. */
2431 set_field_to_nxast(sf, out);
2432 } else if (ofp_version < OFP12_VERSION) {
2433 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2434 set_field_to_legacy_openflow(sf, ofp_version, out);
2435 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2436 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2437 * entire field, so encode it as OFPAT_SET_FIELD. */
2438 set_field_to_set_field(sf, ofp_version, out);
2440 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2441 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2442 * so encode it as reg_load. */
2443 set_field_to_nxast(sf, out);
2447 /* Parses a "set_field" action with argument 'arg', appending the parsed
2448 * action to 'ofpacts'.
2450 * Returns NULL if successful, otherwise a malloc()'d string describing the
2451 * error. The caller is responsible for freeing the returned string. */
2452 static char * OVS_WARN_UNUSED_RESULT
2453 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2454 enum ofputil_protocol *usable_protocols)
2456 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2460 const struct mf_field *mf;
2464 delim = strstr(arg, "->");
2466 return xasprintf("%s: missing `->'", arg);
2468 if (strlen(delim) <= strlen("->")) {
2469 return xasprintf("%s: missing field name following `->'", arg);
2472 key = delim + strlen("->");
2473 mf = mf_from_name(key);
2475 return xasprintf("%s is not a valid OXM field name", key);
2477 if (!mf->writable) {
2478 return xasprintf("%s is read-only", key);
2482 error = mf_parse(mf, value, &sf->value, &sf->mask);
2487 if (!mf_is_value_valid(mf, &sf->value)) {
2488 return xasprintf("%s is not a valid value for field %s", value, key);
2491 *usable_protocols &= mf->usable_protocols_exact;
2495 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2496 * action to 'ofpacts'.
2498 * Returns NULL if successful, otherwise a malloc()'d string describing the
2499 * error. The caller is responsible for freeing the returned string. */
2500 static char * OVS_WARN_UNUSED_RESULT
2501 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2502 enum ofputil_protocol *usable_protocols)
2504 char *copy = xstrdup(arg);
2505 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2510 static char * OVS_WARN_UNUSED_RESULT
2511 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2513 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2514 const char *full_arg = arg;
2515 uint64_t value = strtoull(arg, (char **) &arg, 0);
2516 struct mf_subfield dst;
2519 if (strncmp(arg, "->", 2)) {
2520 return xasprintf("%s: missing `->' following value", full_arg);
2523 error = mf_parse_subfield(&dst, arg);
2528 if (dst.n_bits < 64 && (value >> dst.n_bits) != 0) {
2529 return xasprintf("%s: value %"PRIu64" does not fit into %d bits",
2530 full_arg, value, dst.n_bits);
2533 sf->field = dst.field;
2534 memset(&sf->value, 0, sizeof sf->value);
2535 bitwise_put(value, &sf->value, dst.field->n_bytes, dst.ofs, dst.n_bits);
2536 bitwise_put(UINT64_MAX, &sf->mask,
2537 dst.field->n_bytes, dst.ofs, dst.n_bits);
2542 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2544 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2545 struct mf_subfield dst;
2548 dst.ofs = dst.n_bits = 0;
2549 while (next_load_segment(a, &dst, &value)) {
2550 ds_put_format(s, "load:%#"PRIx64"->", value);
2551 mf_format_subfield(&dst, s);
2552 ds_put_char(s, ',');
2556 ds_put_cstr(s, "set_field:");
2557 mf_format(a->field, &a->value, &a->mask, s);
2558 ds_put_format(s, "->%s", a->field->name);
2562 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2563 * is marked such that, if possible, it will be translated to OpenFlow as
2564 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2565 * that was the way that the action was expressed when it came into OVS or for
2566 * backward compatibility. */
2567 struct ofpact_set_field *
2568 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2570 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2571 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2575 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2577 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2580 struct nx_action_stack {
2581 ovs_be16 type; /* OFPAT_VENDOR. */
2582 ovs_be16 len; /* Length is 16. */
2583 ovs_be32 vendor; /* NX_VENDOR_ID. */
2584 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2585 ovs_be16 offset; /* Bit offset into the field. */
2587 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2588 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2589 * - Enough 0-bytes to pad out the action to 24 bytes. */
2590 uint8_t pad[12]; /* See above. */
2592 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2595 decode_stack_action(const struct nx_action_stack *nasp,
2596 struct ofpact_stack *stack_action)
2601 stack_action->subfield.ofs = ntohs(nasp->offset);
2603 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2604 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2605 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2609 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) ofpbuf_data(&b));
2611 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
2612 return OFPERR_NXBRC_MUST_BE_ZERO;
2619 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2620 struct ofpbuf *ofpacts)
2622 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2623 enum ofperr error = decode_stack_action(nasp, push);
2624 return error ? error : nxm_stack_push_check(push, NULL);
2628 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2629 struct ofpbuf *ofpacts)
2631 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2632 enum ofperr error = decode_stack_action(nasp, pop);
2633 return error ? error : nxm_stack_pop_check(pop, NULL);
2637 encode_STACK_op(const struct ofpact_stack *stack_action,
2638 struct nx_action_stack *nasp)
2643 nasp->offset = htons(stack_action->subfield.ofs);
2645 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2646 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2647 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2648 n_bits = htons(stack_action->subfield.n_bits);
2649 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2653 encode_STACK_PUSH(const struct ofpact_stack *stack,
2654 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2656 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2660 encode_STACK_POP(const struct ofpact_stack *stack,
2661 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2663 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2666 static char * OVS_WARN_UNUSED_RESULT
2667 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2668 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2670 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2673 static char * OVS_WARN_UNUSED_RESULT
2674 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2675 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2677 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2681 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2683 nxm_format_stack_push(a, s);
2687 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2689 nxm_format_stack_pop(a, s);
2692 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2694 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2695 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2696 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2697 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2699 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2700 * sent only to controllers with id 0.)
2702 struct nx_action_cnt_ids {
2703 ovs_be16 type; /* OFPAT_VENDOR. */
2704 ovs_be16 len; /* Length including slaves. */
2705 ovs_be32 vendor; /* NX_VENDOR_ID. */
2706 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2708 ovs_be16 n_controllers; /* Number of controllers. */
2709 uint8_t zeros[4]; /* Must be zero. */
2711 /* Followed by 1 or more controller ids.
2713 * uint16_t cnt_ids[]; // Controller ids.
2714 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2717 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2720 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2723 struct ofpact_cnt_ids *ids;
2724 enum ofperr error = 0;
2726 ids = ofpact_put_DEC_TTL(out);
2727 ids->n_controllers = 1;
2728 ofpbuf_put(out, &id, sizeof id);
2730 ofpact_update_len(out, &ids->ofpact);
2735 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2738 struct ofpact_cnt_ids *ids;
2742 ids = ofpact_put_DEC_TTL(out);
2743 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2744 ids->n_controllers = ntohs(nac_ids->n_controllers);
2745 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2747 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2748 return OFPERR_NXBRC_MUST_BE_ZERO;
2751 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2752 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2753 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2754 "are required for %"PRIu16" controllers.",
2755 ids_size, ids->n_controllers * sizeof(ovs_be16),
2756 ids->n_controllers);
2757 return OFPERR_OFPBAC_BAD_LEN;
2760 for (i = 0; i < ids->n_controllers; i++) {
2761 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2762 ofpbuf_put(out, &id, sizeof id);
2766 ofpact_update_len(out, &ids->ofpact);
2772 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2773 enum ofp_version ofp_version, struct ofpbuf *out)
2775 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2776 || dec_ttl->n_controllers != 1
2777 || dec_ttl->cnt_ids[0] != 0) {
2778 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2779 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2783 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2784 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2786 ids = ofpbuf_put_zeros(out, ids_len);
2787 for (i = 0; i < dec_ttl->n_controllers; i++) {
2788 ids[i] = htons(dec_ttl->cnt_ids[i]);
2791 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2796 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2798 struct ofpact_cnt_ids *ids;
2801 ofpact_put_DEC_TTL(ofpacts);
2802 ofpbuf_put(ofpacts, &id, sizeof id);
2803 ids = ofpacts->frame;
2804 ids->n_controllers++;
2805 ofpact_update_len(ofpacts, &ids->ofpact);
2808 static char * OVS_WARN_UNUSED_RESULT
2809 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2810 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2813 parse_noargs_dec_ttl(ofpacts);
2815 struct ofpact_cnt_ids *ids;
2818 ids = ofpact_put_DEC_TTL(ofpacts);
2819 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2820 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2821 cntr = strtok_r(NULL, ", ", &arg)) {
2822 uint16_t id = atoi(cntr);
2824 ofpbuf_put(ofpacts, &id, sizeof id);
2825 ids = ofpacts->frame;
2826 ids->n_controllers++;
2828 if (!ids->n_controllers) {
2829 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2832 ofpact_update_len(ofpacts, &ids->ofpact);
2838 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2842 ds_put_cstr(s, "dec_ttl");
2843 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2844 ds_put_cstr(s, "(");
2845 for (i = 0; i < a->n_controllers; i++) {
2847 ds_put_cstr(s, ",");
2849 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2851 ds_put_cstr(s, ")");
2855 /* Set MPLS label actions. */
2858 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label, struct ofpbuf *out)
2860 ofpact_put_SET_MPLS_LABEL(out)->label = label;
2865 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
2866 enum ofp_version ofp_version,
2869 if (ofp_version < OFP12_VERSION) {
2870 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
2872 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
2873 ntohl(label->label));
2877 static char * OVS_WARN_UNUSED_RESULT
2878 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
2879 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2881 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
2883 return xstrdup("set_mpls_label: expected label.");
2886 mpls_label->label = htonl(atoi(arg));
2891 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
2893 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
2896 /* Set MPLS TC actions. */
2899 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc, struct ofpbuf *out)
2901 ofpact_put_SET_MPLS_TC(out)->tc = tc;
2906 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
2907 enum ofp_version ofp_version, struct ofpbuf *out)
2909 if (ofp_version < OFP12_VERSION) {
2910 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
2912 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
2916 static char * OVS_WARN_UNUSED_RESULT
2917 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
2918 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2920 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
2923 return xstrdup("set_mpls_tc: expected tc.");
2926 mpls_tc->tc = atoi(arg);
2931 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
2933 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
2936 /* Set MPLS TTL actions. */
2939 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl, struct ofpbuf *out)
2941 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
2946 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
2947 enum ofp_version ofp_version, struct ofpbuf *out)
2949 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
2952 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
2953 * action to 'ofpacts'.
2955 * Returns NULL if successful, otherwise a malloc()'d string describing the
2956 * error. The caller is responsible for freeing the returned string. */
2957 static char * OVS_WARN_UNUSED_RESULT
2958 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
2959 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2961 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
2964 return xstrdup("set_mpls_ttl: expected ttl.");
2967 mpls_ttl->ttl = atoi(arg);
2972 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
2974 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
2977 /* Decrement MPLS TTL actions. */
2980 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
2982 ofpact_put_DEC_MPLS_TTL(out);
2987 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
2988 enum ofp_version ofp_version, struct ofpbuf *out)
2990 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
2993 static char * OVS_WARN_UNUSED_RESULT
2994 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
2995 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2997 ofpact_put_DEC_MPLS_TTL(ofpacts);
3002 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3004 ds_put_cstr(s, "dec_mpls_ttl");
3007 /* Push MPLS label action. */
3010 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
3012 struct ofpact_push_mpls *oam;
3014 if (!eth_type_mpls(ethertype)) {
3015 return OFPERR_OFPBAC_BAD_ARGUMENT;
3017 oam = ofpact_put_PUSH_MPLS(out);
3018 oam->ethertype = ethertype;
3024 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3025 enum ofp_version ofp_version, struct ofpbuf *out)
3027 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3030 static char * OVS_WARN_UNUSED_RESULT
3031 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3032 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3037 error = str_to_u16(arg, "push_mpls", ðertype);
3039 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3045 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3047 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3050 /* Pop MPLS label action. */
3053 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
3055 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3060 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3061 enum ofp_version ofp_version, struct ofpbuf *out)
3063 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3066 static char * OVS_WARN_UNUSED_RESULT
3067 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3068 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3073 error = str_to_u16(arg, "pop_mpls", ðertype);
3075 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3081 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3083 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3086 /* Set tunnel ID actions. */
3089 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id, struct ofpbuf *out)
3091 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3092 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3093 tunnel->tun_id = tun_id;
3098 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id, struct ofpbuf *out)
3100 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3101 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3102 tunnel->tun_id = tun_id;
3107 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3108 enum ofp_version ofp_version, struct ofpbuf *out)
3110 uint64_t tun_id = tunnel->tun_id;
3112 if (ofp_version < OFP12_VERSION) {
3113 if (tun_id <= UINT32_MAX
3114 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3115 put_NXAST_SET_TUNNEL(out, tun_id);
3117 put_NXAST_SET_TUNNEL64(out, tun_id);
3120 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3124 static char * OVS_WARN_UNUSED_RESULT
3125 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3126 enum ofp_raw_action_type raw)
3128 struct ofpact_tunnel *tunnel;
3130 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3131 tunnel->ofpact.raw = raw;
3132 return str_to_u64(arg, &tunnel->tun_id);
3135 static char * OVS_WARN_UNUSED_RESULT
3136 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3137 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3139 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3143 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3145 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3146 (a->tun_id > UINT32_MAX
3147 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3151 /* Set queue action. */
3154 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id, struct ofpbuf *out)
3156 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3161 encode_SET_QUEUE(const struct ofpact_queue *queue,
3162 enum ofp_version ofp_version, struct ofpbuf *out)
3164 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3167 static char * OVS_WARN_UNUSED_RESULT
3168 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3169 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3171 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3175 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3177 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3180 /* Pop queue action. */
3183 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3185 ofpact_put_POP_QUEUE(out);
3190 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3191 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3193 put_NXAST_POP_QUEUE(out);
3196 static char * OVS_WARN_UNUSED_RESULT
3197 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3198 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3200 ofpact_put_POP_QUEUE(ofpacts);
3205 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3207 ds_put_cstr(s, "pop_queue");
3210 /* Action structure for NXAST_FIN_TIMEOUT.
3212 * This action changes the idle timeout or hard timeout, or both, of this
3213 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3214 * When such a packet is observed, the action reduces the rule's idle timeout
3215 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3216 * action has no effect on an existing timeout that is already shorter than the
3217 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3218 * of zero has no effect on the respective timeout.
3220 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3221 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3222 * receipt of the FIN or RST.
3224 * This is useful for quickly discarding learned TCP flows that otherwise will
3225 * take a long time to expire.
3227 * This action is intended for use with an OpenFlow rule that matches only a
3228 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3229 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3230 * RST in any of those flows will cause the entire OpenFlow rule to expire
3231 * early, which is not normally desirable.
3233 struct nx_action_fin_timeout {
3234 ovs_be16 type; /* OFPAT_VENDOR. */
3235 ovs_be16 len; /* 16. */
3236 ovs_be32 vendor; /* NX_VENDOR_ID. */
3237 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3238 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3239 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3240 ovs_be16 pad; /* Must be zero. */
3242 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3245 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3248 struct ofpact_fin_timeout *oft;
3250 oft = ofpact_put_FIN_TIMEOUT(out);
3251 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3252 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3257 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3258 enum ofp_version ofp_version OVS_UNUSED,
3261 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3262 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3263 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3266 static char * OVS_WARN_UNUSED_RESULT
3267 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3268 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3270 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3273 while (ofputil_parse_key_value(&arg, &key, &value)) {
3276 if (!strcmp(key, "idle_timeout")) {
3277 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3278 } else if (!strcmp(key, "hard_timeout")) {
3279 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3281 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3293 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3295 ds_put_cstr(s, "fin_timeout(");
3296 if (a->fin_idle_timeout) {
3297 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3299 if (a->fin_hard_timeout) {
3300 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3303 ds_put_char(s, ')');
3306 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3308 * These actions search one of the switch's flow tables:
3310 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3311 * it specifies the table to search.
3313 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3314 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3315 * table, that is, the OpenFlow flow table that contains the flow from
3316 * which this action was obtained. If this action did not come from a
3317 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3318 * is the current table.
3320 * The flow table lookup uses a flow that may be slightly modified from the
3323 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3324 * is used as the flow's in_port.
3326 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3327 * then its value is used as the flow's in_port. Otherwise, the original
3330 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3331 * resubmit action, then the flow is updated with the new values.
3333 * Following the lookup, the original in_port is restored.
3335 * If the modified flow matched in the flow table, then the corresponding
3336 * actions are executed. Afterward, actions following the resubmit in the
3337 * original set of actions, if any, are executed; any changes made to the
3338 * packet (e.g. changes to VLAN) by secondary actions persist when those
3339 * actions are executed, although the original in_port is restored.
3341 * Resubmit actions may be used any number of times within a set of actions.
3343 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3344 * implementation-defined depth, further resubmit actions are simply ignored.
3346 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3347 * 'pad' to be all-bits-zero.
3349 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3350 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3352 struct nx_action_resubmit {
3353 ovs_be16 type; /* OFPAT_VENDOR. */
3354 ovs_be16 len; /* Length is 16. */
3355 ovs_be32 vendor; /* NX_VENDOR_ID. */
3356 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3357 ovs_be16 in_port; /* New in_port for checking flow table. */
3358 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3361 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3364 decode_NXAST_RAW_RESUBMIT(uint16_t port, struct ofpbuf *out)
3366 struct ofpact_resubmit *resubmit;
3368 resubmit = ofpact_put_RESUBMIT(out);
3369 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3370 resubmit->in_port = u16_to_ofp(port);
3371 resubmit->table_id = 0xff;
3376 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3379 struct ofpact_resubmit *resubmit;
3381 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3382 return OFPERR_OFPBAC_BAD_ARGUMENT;
3385 resubmit = ofpact_put_RESUBMIT(out);
3386 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3387 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3388 resubmit->table_id = nar->table;
3393 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3394 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3396 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3398 if (resubmit->table_id == 0xff
3399 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3400 put_NXAST_RESUBMIT(out, in_port);
3402 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3403 nar->table = resubmit->table_id;
3404 nar->in_port = htons(in_port);
3408 static char * OVS_WARN_UNUSED_RESULT
3409 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3410 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3412 struct ofpact_resubmit *resubmit;
3413 char *in_port_s, *table_s;
3415 resubmit = ofpact_put_RESUBMIT(ofpacts);
3417 in_port_s = strsep(&arg, ",");
3418 if (in_port_s && in_port_s[0]) {
3419 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3420 return xasprintf("%s: resubmit to unknown port", in_port_s);
3423 resubmit->in_port = OFPP_IN_PORT;
3426 table_s = strsep(&arg, ",");
3427 if (table_s && table_s[0]) {
3428 uint32_t table_id = 0;
3431 error = str_to_u32(table_s, &table_id);
3435 resubmit->table_id = table_id;
3437 resubmit->table_id = 255;
3440 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3441 return xstrdup("at least one \"in_port\" or \"table\" must be "
3442 "specified on resubmit");
3448 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3450 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3451 ds_put_cstr(s, "resubmit:");
3452 ofputil_format_port(a->in_port, s);
3454 ds_put_format(s, "resubmit(");
3455 if (a->in_port != OFPP_IN_PORT) {
3456 ofputil_format_port(a->in_port, s);
3458 ds_put_char(s, ',');
3459 if (a->table_id != 255) {
3460 ds_put_format(s, "%"PRIu8, a->table_id);
3462 ds_put_char(s, ')');
3466 /* Action structure for NXAST_LEARN.
3468 * This action adds or modifies a flow in an OpenFlow table, similar to
3469 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3470 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3471 * flow's match criteria and actions are built by applying each of the series
3472 * of flow_mod_spec elements included as part of the action.
3474 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3475 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3476 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3477 * bits from a source to a destination. In this case, the header contains
3480 * 15 14 13 12 11 10 0
3481 * +------+---+------+---------------------------------+
3482 * | 0 |src| dst | n_bits |
3483 * +------+---+------+---------------------------------+
3485 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3486 * following table summarizes the meaning of each possible combination.
3487 * Details follow the table:
3490 * --- --- ----------------------------------------------------------
3491 * 0 0 Add match criteria based on value in a field.
3492 * 1 0 Add match criteria based on an immediate value.
3493 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3494 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3495 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3496 * All other combinations are undefined and not allowed.
3498 * The flow_mod_spec header is followed by a source specification and a
3499 * destination specification. The format and meaning of the source
3500 * specification depends on 'src':
3502 * - If 'src' is 0, the source bits are taken from a field in the flow to
3503 * which this action is attached. (This should be a wildcarded field. If
3504 * its value is fully specified then the source bits being copied have
3507 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3508 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3509 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3510 * 'field' and 'ofs' are subject to the same restrictions as the source
3511 * field in NXAST_REG_MOVE.
3513 * - If 'src' is 1, the source bits are a constant value. The source
3514 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3515 * number in network order, the source bits are the 'n_bits'
3516 * least-significant bits. The switch will report an error if other bits
3517 * in the constant are nonzero.
3519 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3520 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3521 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3522 * meaning of the flow_mod_spec depends on 'dst':
3524 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3525 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3526 * packet equal the source bits. 'field' may be any nxm_header with
3527 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3529 * Order is significant. Earlier flow_mod_specs must satisfy any
3530 * prerequisites for matching fields specified later, by copying constant
3531 * values into prerequisite fields.
3533 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3536 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3537 * the new flow. The new flow copies the source bits into
3538 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3541 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3542 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3544 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3545 * empty. It has the following meaning:
3547 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3548 * The new flow outputs to the OpenFlow port specified by the source field.
3549 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3550 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3553 * Resource Management
3554 * -------------------
3556 * A switch has a finite amount of flow table space available for learning.
3557 * When this space is exhausted, no new learning table entries will be learned
3558 * until some existing flow table entries expire. The controller should be
3559 * prepared to handle this by flooding (which can be implemented as a
3560 * low-priority flow).
3562 * If a learned flow matches a single TCP stream with a relatively long
3563 * timeout, one may make the best of resource constraints by setting
3564 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3565 * both, to shorter timeouts. When either of these is specified as a nonzero
3566 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3572 * The following examples give a prose description of the flow_mod_specs along
3573 * with informal notation for how those would be represented and a hex dump of
3574 * the bytes that would be required.
3576 * These examples could work with various nx_action_learn parameters. Typical
3577 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3578 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3580 * 1. Learn input port based on the source MAC, with lookup into
3581 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3583 * Match on in_port=99:
3584 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3585 * ovs_be16(99), 00 63
3586 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3588 * Match Ethernet destination on Ethernet source from packet:
3589 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3590 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3591 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3593 * Set NXM_NX_REG1[16:31] to the packet's input port:
3594 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3595 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3596 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3598 * Given a packet that arrived on port A with Ethernet source address B,
3599 * this would set up the flow "in_port=99, dl_dst=B,
3600 * actions=load:A->NXM_NX_REG1[16..31]".
3602 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3603 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3604 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3606 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3607 * into NXM_NX_REG1[16:31]:
3609 * Match on same VLAN ID as packet:
3610 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3611 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3612 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3614 * Match Ethernet destination on Ethernet source from packet:
3615 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3616 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3617 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3619 * Output to the packet's input port:
3620 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3621 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3623 * Given a packet that arrived on port A with Ethernet source address B in
3624 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3625 * actions=output:A".
3627 * In syntax accepted by ovs-ofctl, this action is:
3628 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3629 * output:NXM_OF_IN_PORT[])
3631 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3632 * 10-second MAC expiration time to make it easier to see what's going on
3634 * ovs-vsctl del-controller br0
3635 * ovs-ofctl del-flows br0
3636 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3637 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3638 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3639 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3640 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3642 * You can then dump the MAC learning table with:
3644 * ovs-ofctl dump-flows br0 table=1
3649 * For best performance, segregate learned flows into a table that is not used
3650 * for any other flows except possibly for a lowest-priority "catch-all" flow
3651 * (a flow with no match criteria). If different learning actions specify
3652 * different match criteria, use different tables for the learned flows.
3654 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3655 * These timeouts apply to the flow that is added, which means that a flow with
3656 * an idle timeout will expire when no traffic has been sent *to* the learned
3657 * address. This is not usually the intent in MAC learning; instead, we want
3658 * the MAC learn entry to expire when no traffic has been sent *from* the
3659 * learned address. Use a hard timeout for that.
3661 struct nx_action_learn {
3662 ovs_be16 type; /* OFPAT_VENDOR. */
3663 ovs_be16 len; /* At least 24. */
3664 ovs_be32 vendor; /* NX_VENDOR_ID. */
3665 ovs_be16 subtype; /* NXAST_LEARN. */
3666 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3667 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3668 ovs_be16 priority; /* Priority level of flow entry. */
3669 ovs_be64 cookie; /* Cookie for new flow. */
3670 ovs_be16 flags; /* NX_LEARN_F_*. */
3671 uint8_t table_id; /* Table to insert flow entry. */
3672 uint8_t pad; /* Must be zero. */
3673 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3674 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3675 /* Followed by a sequence of flow_mod_spec elements, as described above,
3676 * until the end of the action is reached. */
3678 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3681 get_be16(const void **pp)
3683 const ovs_be16 *p = *pp;
3684 ovs_be16 value = *p;
3690 get_be32(const void **pp)
3692 const ovs_be32 *p = *pp;
3693 ovs_be32 value = get_unaligned_be32(p);
3699 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3701 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3702 sf->ofs = ntohs(get_be16(p));
3703 sf->n_bits = n_bits;
3707 learn_min_len(uint16_t header)
3709 int n_bits = header & NX_LEARN_N_BITS_MASK;
3710 int src_type = header & NX_LEARN_SRC_MASK;
3711 int dst_type = header & NX_LEARN_DST_MASK;
3712 unsigned int min_len;
3715 if (src_type == NX_LEARN_SRC_FIELD) {
3716 min_len += sizeof(ovs_be32); /* src_field */
3717 min_len += sizeof(ovs_be16); /* src_ofs */
3719 min_len += DIV_ROUND_UP(n_bits, 16);
3721 if (dst_type == NX_LEARN_DST_MATCH ||
3722 dst_type == NX_LEARN_DST_LOAD) {
3723 min_len += sizeof(ovs_be32); /* dst_field */
3724 min_len += sizeof(ovs_be16); /* dst_ofs */
3729 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3730 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3732 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3733 struct ofpbuf *ofpacts)
3735 struct ofpact_learn *learn;
3736 const void *p, *end;
3739 return OFPERR_OFPBAC_BAD_ARGUMENT;
3742 learn = ofpact_put_LEARN(ofpacts);
3744 learn->idle_timeout = ntohs(nal->idle_timeout);
3745 learn->hard_timeout = ntohs(nal->hard_timeout);
3746 learn->priority = ntohs(nal->priority);
3747 learn->cookie = nal->cookie;
3748 learn->table_id = nal->table_id;
3749 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3750 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3752 learn->flags = ntohs(nal->flags);
3753 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3754 NX_LEARN_F_DELETE_LEARNED)) {
3755 return OFPERR_OFPBAC_BAD_ARGUMENT;
3758 if (learn->table_id == 0xff) {
3759 return OFPERR_OFPBAC_BAD_ARGUMENT;
3762 end = (char *) nal + ntohs(nal->len);
3763 for (p = nal + 1; p != end; ) {
3764 struct ofpact_learn_spec *spec;
3765 uint16_t header = ntohs(get_be16(&p));
3771 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3772 learn = ofpacts->frame;
3775 spec->src_type = header & NX_LEARN_SRC_MASK;
3776 spec->dst_type = header & NX_LEARN_DST_MASK;
3777 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3779 /* Check for valid src and dst type combination. */
3780 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3781 spec->dst_type == NX_LEARN_DST_LOAD ||
3782 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3783 spec->src_type == NX_LEARN_SRC_FIELD)) {
3786 return OFPERR_OFPBAC_BAD_ARGUMENT;
3789 /* Check that the arguments don't overrun the end of the action. */
3790 if ((char *) end - (char *) p < learn_min_len(header)) {
3791 return OFPERR_OFPBAC_BAD_LEN;
3794 /* Get the source. */
3795 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3796 get_subfield(spec->n_bits, &p, &spec->src);
3798 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3800 bitwise_copy(p, p_bytes, 0,
3801 &spec->src_imm, sizeof spec->src_imm, 0,
3803 p = (const uint8_t *) p + p_bytes;
3806 /* Get the destination. */
3807 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3808 spec->dst_type == NX_LEARN_DST_LOAD) {
3809 get_subfield(spec->n_bits, &p, &spec->dst);
3812 ofpact_update_len(ofpacts, &learn->ofpact);
3814 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3815 return OFPERR_OFPBAC_BAD_ARGUMENT;
3822 put_be16(struct ofpbuf *b, ovs_be16 x)
3824 ofpbuf_put(b, &x, sizeof x);
3828 put_be32(struct ofpbuf *b, ovs_be32 x)
3830 ofpbuf_put(b, &x, sizeof x);
3834 put_u16(struct ofpbuf *b, uint16_t x)
3836 put_be16(b, htons(x));
3840 put_u32(struct ofpbuf *b, uint32_t x)
3842 put_be32(b, htonl(x));
3846 encode_LEARN(const struct ofpact_learn *learn,
3847 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3849 const struct ofpact_learn_spec *spec;
3850 struct nx_action_learn *nal;
3853 start_ofs = ofpbuf_size(out);
3854 nal = put_NXAST_LEARN(out);
3855 nal->idle_timeout = htons(learn->idle_timeout);
3856 nal->hard_timeout = htons(learn->hard_timeout);
3857 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
3858 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
3859 nal->priority = htons(learn->priority);
3860 nal->cookie = learn->cookie;
3861 nal->flags = htons(learn->flags);
3862 nal->table_id = learn->table_id;
3864 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
3865 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
3867 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3868 put_u32(out, mf_nxm_header(spec->src.field->id));
3869 put_u16(out, spec->src.ofs);
3871 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3872 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
3873 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
3874 bits, n_dst_bytes, 0,
3878 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3879 spec->dst_type == NX_LEARN_DST_LOAD) {
3880 put_u32(out, mf_nxm_header(spec->dst.field->id));
3881 put_u16(out, spec->dst.ofs);
3885 pad_ofpat(out, start_ofs);
3888 static char * OVS_WARN_UNUSED_RESULT
3889 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
3890 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3892 return learn_parse(arg, ofpacts);
3896 format_LEARN(const struct ofpact_learn *a, struct ds *s)
3901 /* Action structure for NXAST_MULTIPATH.
3903 * This action performs the following steps in sequence:
3905 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
3906 * Refer to the definition of "enum nx_mp_fields" for details.
3908 * The 'basis' value is used as a universal hash parameter, that is,
3909 * different values of 'basis' yield different hash functions. The
3910 * particular universal hash function used is implementation-defined.
3912 * The hashed fields' values are drawn from the current state of the
3913 * flow, including all modifications that have been made by actions up to
3916 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
3917 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
3920 * The output of the algorithm is 'link', an unsigned integer less than
3921 * or equal to 'max_link'.
3923 * Some algorithms use 'arg' as an additional argument.
3925 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
3926 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
3929 * The switch will reject actions that have an unknown 'fields', or an unknown
3930 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
3931 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
3932 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
3934 struct nx_action_multipath {
3935 ovs_be16 type; /* OFPAT_VENDOR. */
3936 ovs_be16 len; /* Length is 32. */
3937 ovs_be32 vendor; /* NX_VENDOR_ID. */
3938 ovs_be16 subtype; /* NXAST_MULTIPATH. */
3940 /* What fields to hash and how. */
3941 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
3942 ovs_be16 basis; /* Universal hash parameter. */
3945 /* Multipath link choice algorithm to apply to hash value. */
3946 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
3947 ovs_be16 max_link; /* Number of output links, minus 1. */
3948 ovs_be32 arg; /* Algorithm-specific argument. */
3951 /* Where to store the result. */
3952 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
3953 ovs_be32 dst; /* Destination. */
3955 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
3958 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
3961 uint32_t n_links = ntohs(nam->max_link) + 1;
3962 size_t min_n_bits = log_2_ceil(n_links);
3963 struct ofpact_multipath *mp;
3965 mp = ofpact_put_MULTIPATH(out);
3966 mp->fields = ntohs(nam->fields);
3967 mp->basis = ntohs(nam->basis);
3968 mp->algorithm = ntohs(nam->algorithm);
3969 mp->max_link = ntohs(nam->max_link);
3970 mp->arg = ntohl(nam->arg);
3971 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
3972 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
3973 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
3975 if (!flow_hash_fields_valid(mp->fields)) {
3976 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
3977 return OFPERR_OFPBAC_BAD_ARGUMENT;
3978 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
3979 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
3980 && mp->algorithm != NX_MP_ALG_HRW
3981 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
3982 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
3983 return OFPERR_OFPBAC_BAD_ARGUMENT;
3984 } else if (mp->dst.n_bits < min_n_bits) {
3985 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
3986 "%"PRIu32" links", min_n_bits, n_links);
3987 return OFPERR_OFPBAC_BAD_ARGUMENT;
3990 return multipath_check(mp, NULL);
3994 encode_MULTIPATH(const struct ofpact_multipath *mp,
3995 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3997 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
3999 nam->fields = htons(mp->fields);
4000 nam->basis = htons(mp->basis);
4001 nam->algorithm = htons(mp->algorithm);
4002 nam->max_link = htons(mp->max_link);
4003 nam->arg = htonl(mp->arg);
4004 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4005 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4008 static char * OVS_WARN_UNUSED_RESULT
4009 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4010 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4012 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4016 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4018 multipath_format(a, s);
4021 /* Action structure for NXAST_NOTE.
4023 * This action has no effect. It is variable length. The switch does not
4024 * attempt to interpret the user-defined 'note' data in any way. A controller
4025 * can use this action to attach arbitrary metadata to a flow.
4027 * This action might go away in the future.
4029 struct nx_action_note {
4030 ovs_be16 type; /* OFPAT_VENDOR. */
4031 ovs_be16 len; /* A multiple of 8, but at least 16. */
4032 ovs_be32 vendor; /* NX_VENDOR_ID. */
4033 ovs_be16 subtype; /* NXAST_NOTE. */
4034 uint8_t note[6]; /* Start of user-defined data. */
4035 /* Possibly followed by additional user-defined data. */
4037 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4040 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan, struct ofpbuf *out)
4042 struct ofpact_note *note;
4043 unsigned int length;
4045 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4046 note = ofpact_put(out, OFPACT_NOTE,
4047 offsetof(struct ofpact_note, data) + length);
4048 note->length = length;
4049 memcpy(note->data, nan->note, length);
4055 encode_NOTE(const struct ofpact_note *note,
4056 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4058 size_t start_ofs = ofpbuf_size(out);
4059 struct nx_action_note *nan;
4060 unsigned int remainder;
4063 put_NXAST_NOTE(out);
4064 ofpbuf_set_size(out, ofpbuf_size(out) - sizeof nan->note);
4066 ofpbuf_put(out, note->data, note->length);
4068 len = ofpbuf_size(out) - start_ofs;
4069 remainder = len % OFP_ACTION_ALIGN;
4071 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4073 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4074 nan->len = htons(ofpbuf_size(out) - start_ofs);
4077 static char * OVS_WARN_UNUSED_RESULT
4078 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4079 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4081 struct ofpact_note *note;
4083 note = ofpact_put_NOTE(ofpacts);
4084 while (*arg != '\0') {
4095 byte = hexits_value(arg, 2, &ok);
4097 return xstrdup("bad hex digit in `note' argument");
4099 ofpbuf_put(ofpacts, &byte, 1);
4101 note = ofpacts->frame;
4106 ofpact_update_len(ofpacts, ¬e->ofpact);
4111 format_NOTE(const struct ofpact_note *a, struct ds *s)
4115 ds_put_cstr(s, "note:");
4116 for (i = 0; i < a->length; i++) {
4118 ds_put_char(s, '.');
4120 ds_put_format(s, "%02"PRIx8, a->data[i]);
4127 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4129 ofpact_put_EXIT(out);
4134 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4135 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4137 put_NXAST_EXIT(out);
4140 static char * OVS_WARN_UNUSED_RESULT
4141 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4142 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4144 ofpact_put_EXIT(ofpacts);
4149 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4151 ds_put_cstr(s, "exit");
4154 /* Action structure for NXAST_SAMPLE.
4156 * Samples matching packets with the given probability and sends them
4157 * each to the set of collectors identified with the given ID. The
4158 * probability is expressed as a number of packets to be sampled out
4159 * of USHRT_MAX packets, and must be >0.
4161 * When sending packet samples to IPFIX collectors, the IPFIX flow
4162 * record sent for each sampled packet is associated with the given
4163 * observation domain ID and observation point ID. Each IPFIX flow
4164 * record contain the sampled packet's headers when executing this
4165 * rule. If a sampled packet's headers are modified by previous
4166 * actions in the flow, those modified headers are sent. */
4167 struct nx_action_sample {
4168 ovs_be16 type; /* OFPAT_VENDOR. */
4169 ovs_be16 len; /* Length is 24. */
4170 ovs_be32 vendor; /* NX_VENDOR_ID. */
4171 ovs_be16 subtype; /* NXAST_SAMPLE. */
4172 ovs_be16 probability; /* Fraction of packets to sample. */
4173 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4174 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4175 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4177 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4180 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas, struct ofpbuf *out)
4182 struct ofpact_sample *sample;
4184 sample = ofpact_put_SAMPLE(out);
4185 sample->probability = ntohs(nas->probability);
4186 sample->collector_set_id = ntohl(nas->collector_set_id);
4187 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4188 sample->obs_point_id = ntohl(nas->obs_point_id);
4190 if (sample->probability == 0) {
4191 return OFPERR_OFPBAC_BAD_ARGUMENT;
4198 encode_SAMPLE(const struct ofpact_sample *sample,
4199 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4201 struct nx_action_sample *nas;
4203 nas = put_NXAST_SAMPLE(out);
4204 nas->probability = htons(sample->probability);
4205 nas->collector_set_id = htonl(sample->collector_set_id);
4206 nas->obs_domain_id = htonl(sample->obs_domain_id);
4207 nas->obs_point_id = htonl(sample->obs_point_id);
4210 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4211 * action to 'ofpacts'.
4213 * Returns NULL if successful, otherwise a malloc()'d string describing the
4214 * error. The caller is responsible for freeing the returned string. */
4215 static char * OVS_WARN_UNUSED_RESULT
4216 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4217 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4219 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4222 while (ofputil_parse_key_value(&arg, &key, &value)) {
4225 if (!strcmp(key, "probability")) {
4226 error = str_to_u16(value, "probability", &os->probability);
4227 if (!error && os->probability == 0) {
4228 error = xasprintf("invalid probability value \"%s\"", value);
4230 } else if (!strcmp(key, "collector_set_id")) {
4231 error = str_to_u32(value, &os->collector_set_id);
4232 } else if (!strcmp(key, "obs_domain_id")) {
4233 error = str_to_u32(value, &os->obs_domain_id);
4234 } else if (!strcmp(key, "obs_point_id")) {
4235 error = str_to_u32(value, &os->obs_point_id);
4237 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4244 if (os->probability == 0) {
4245 return xstrdup("non-zero \"probability\" must be specified on sample");
4251 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4253 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4254 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4255 a->probability, a->collector_set_id,
4256 a->obs_domain_id, a->obs_point_id);
4259 /* Meter instruction. */
4262 encode_METER(const struct ofpact_meter *meter,
4263 enum ofp_version ofp_version, struct ofpbuf *out)
4265 if (ofp_version >= OFP13_VERSION) {
4266 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4270 static char * OVS_WARN_UNUSED_RESULT
4271 parse_METER(char *arg, struct ofpbuf *ofpacts,
4272 enum ofputil_protocol *usable_protocols)
4274 *usable_protocols &= OFPUTIL_P_OF13_UP;
4275 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
4279 format_METER(const struct ofpact_meter *a, struct ds *s)
4281 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
4284 /* Clear-Actions instruction. */
4287 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
4288 enum ofp_version ofp_version OVS_UNUSED,
4289 struct ofpbuf *out OVS_UNUSED)
4291 if (ofp_version > OFP10_VERSION) {
4292 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
4296 static char * OVS_WARN_UNUSED_RESULT
4297 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4298 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4300 ofpact_put_CLEAR_ACTIONS(ofpacts);
4305 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4307 ds_put_cstr(s, "clear_actions");
4310 /* Write-Actions instruction. */
4313 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
4314 enum ofp_version ofp_version, struct ofpbuf *out)
4316 if (ofp_version > OFP10_VERSION) {
4317 const size_t ofs = ofpbuf_size(out);
4319 instruction_put_OFPIT11_WRITE_ACTIONS(out);
4320 ofpacts_put_openflow_actions(actions->actions,
4321 ofpact_nest_get_action_len(actions),
4323 ofpacts_update_instruction_actions(out, ofs);
4327 static char * OVS_WARN_UNUSED_RESULT
4328 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
4329 enum ofputil_protocol *usable_protocols)
4331 struct ofpact_nest *on;
4335 /* Pull off existing actions or instructions. */
4336 ofpact_pad(ofpacts);
4337 ofs = ofpbuf_size(ofpacts);
4338 ofpbuf_pull(ofpacts, ofs);
4340 /* Add a Write-Actions instruction and then pull it off. */
4341 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
4342 ofpbuf_pull(ofpacts, sizeof *on);
4344 /* Parse nested actions.
4346 * We pulled off "write-actions" and the previous actions because the
4347 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4348 * that it doesn't actually include the nested actions. That means that
4349 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4350 * follows a Write-Actions, which is an invalid order. */
4351 error = ofpacts_parse(arg, ofpacts, usable_protocols, false);
4353 /* Put the Write-Actions back on and update its length. */
4354 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
4355 on->ofpact.len = ofpbuf_size(ofpacts);
4357 /* Put any previous actions or instructions back on. */
4358 ofpbuf_push_uninit(ofpacts, ofs);
4364 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
4366 ds_put_cstr(s, "write_actions(");
4367 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
4368 ds_put_char(s, ')');
4371 /* Action structure for NXAST_WRITE_METADATA.
4373 * Modifies the 'mask' bits of the metadata value. */
4374 struct nx_action_write_metadata {
4375 ovs_be16 type; /* OFPAT_VENDOR. */
4376 ovs_be16 len; /* Length is 32. */
4377 ovs_be32 vendor; /* NX_VENDOR_ID. */
4378 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
4379 uint8_t zeros[6]; /* Must be zero. */
4380 ovs_be64 metadata; /* Metadata register. */
4381 ovs_be64 mask; /* Metadata mask. */
4383 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
4386 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
4389 struct ofpact_metadata *om;
4391 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
4392 return OFPERR_NXBRC_MUST_BE_ZERO;
4395 om = ofpact_put_WRITE_METADATA(out);
4396 om->metadata = nawm->metadata;
4397 om->mask = nawm->mask;
4403 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
4404 enum ofp_version ofp_version, struct ofpbuf *out)
4406 if (ofp_version == OFP10_VERSION) {
4407 struct nx_action_write_metadata *nawm;
4409 nawm = put_NXAST_WRITE_METADATA(out);
4410 nawm->metadata = metadata->metadata;
4411 nawm->mask = metadata->mask;
4413 struct ofp11_instruction_write_metadata *oiwm;
4415 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
4416 oiwm->metadata = metadata->metadata;
4417 oiwm->metadata_mask = metadata->mask;
4421 static char * OVS_WARN_UNUSED_RESULT
4422 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
4423 enum ofputil_protocol *usable_protocols)
4425 struct ofpact_metadata *om;
4426 char *mask = strchr(arg, '/');
4428 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
4430 om = ofpact_put_WRITE_METADATA(ofpacts);
4435 error = str_to_be64(mask + 1, &om->mask);
4440 om->mask = OVS_BE64_MAX;
4443 return str_to_be64(arg, &om->metadata);
4447 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
4449 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
4450 if (a->mask != OVS_BE64_MAX) {
4451 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
4455 /* Goto-Table instruction. */
4458 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
4459 enum ofp_version ofp_version, struct ofpbuf *out)
4461 if (ofp_version == OFP10_VERSION) {
4462 struct nx_action_resubmit *nar;
4464 nar = put_NXAST_RESUBMIT_TABLE(out);
4465 nar->table = goto_table->table_id;
4466 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
4468 struct ofp11_instruction_goto_table *oigt;
4470 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
4471 oigt->table_id = goto_table->table_id;
4472 memset(oigt->pad, 0, sizeof oigt->pad);
4476 static char * OVS_WARN_UNUSED_RESULT
4477 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
4478 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4480 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
4481 char *table_s = strsep(&arg, ",");
4482 if (!table_s || !table_s[0]) {
4483 return xstrdup("instruction goto-table needs table id");
4485 return str_to_u8(table_s, "table", &ogt->table_id);
4489 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
4491 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
4495 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
4496 const struct ofp_action_header *bad_action, enum ofperr error)
4498 if (!VLOG_DROP_WARN(&rl)) {
4502 ds_put_hex_dump(&s, actions, actions_len, 0, false);
4503 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
4504 (char *)bad_action - (char *)actions,
4505 ofperr_get_name(error), ds_cstr(&s));
4511 ofpacts_decode(const void *actions, size_t actions_len,
4512 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
4514 struct ofpbuf openflow;
4516 ofpbuf_use_const(&openflow, actions, actions_len);
4517 while (ofpbuf_size(&openflow)) {
4518 const struct ofp_action_header *action = ofpbuf_data(&openflow);
4519 enum ofp_raw_action_type raw;
4523 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
4525 error = ofpact_decode(action, raw, arg, ofpacts);
4529 log_bad_action(actions, actions_len, action, error);
4534 ofpact_pad(ofpacts);
4539 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
4540 unsigned int actions_len,
4541 enum ofp_version version,
4542 uint32_t allowed_ovsinsts,
4543 struct ofpbuf *ofpacts)
4545 const struct ofp_action_header *actions;
4548 ofpbuf_clear(ofpacts);
4550 if (actions_len % OFP_ACTION_ALIGN != 0) {
4551 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
4552 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
4553 return OFPERR_OFPBRC_BAD_LEN;
4556 actions = ofpbuf_try_pull(openflow, actions_len);
4557 if (actions == NULL) {
4558 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
4559 "remaining message length (%"PRIu32")",
4560 actions_len, ofpbuf_size(openflow));
4561 return OFPERR_OFPBRC_BAD_LEN;
4564 error = ofpacts_decode(actions, actions_len, version, ofpacts);
4566 ofpbuf_clear(ofpacts);
4570 error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
4573 ofpbuf_clear(ofpacts);
4578 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
4579 * front of 'openflow' into ofpacts. On success, replaces any existing content
4580 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
4581 * Returns 0 if successful, otherwise an OpenFlow error.
4583 * Actions are processed according to their OpenFlow version which
4584 * is provided in the 'version' parameter.
4586 * In most places in OpenFlow, actions appear encapsulated in instructions, so
4587 * you should call ofpacts_pull_openflow_instructions() instead of this
4590 * The parsed actions are valid generically, but they may not be valid in a
4591 * specific context. For example, port numbers up to OFPP_MAX are valid
4592 * generically, but specific datapaths may only support port numbers in a
4593 * smaller range. Use ofpacts_check() to additional check whether actions are
4594 * valid in a specific context. */
4596 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
4597 unsigned int actions_len,
4598 enum ofp_version version,
4599 struct ofpbuf *ofpacts)
4601 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
4602 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4606 /* OpenFlow 1.1 actions. */
4609 /* True if an action sets the value of a field
4610 * in a way that is compatibile with the action set.
4611 * The field can be set via either a set or a move action.
4612 * False otherwise. */
4614 ofpact_is_set_or_move_action(const struct ofpact *a)
4617 case OFPACT_SET_FIELD:
4618 case OFPACT_REG_MOVE:
4619 case OFPACT_SET_ETH_DST:
4620 case OFPACT_SET_ETH_SRC:
4621 case OFPACT_SET_IP_DSCP:
4622 case OFPACT_SET_IP_ECN:
4623 case OFPACT_SET_IP_TTL:
4624 case OFPACT_SET_IPV4_DST:
4625 case OFPACT_SET_IPV4_SRC:
4626 case OFPACT_SET_L4_DST_PORT:
4627 case OFPACT_SET_L4_SRC_PORT:
4628 case OFPACT_SET_MPLS_LABEL:
4629 case OFPACT_SET_MPLS_TC:
4630 case OFPACT_SET_MPLS_TTL:
4631 case OFPACT_SET_QUEUE:
4632 case OFPACT_SET_TUNNEL:
4633 case OFPACT_SET_VLAN_PCP:
4634 case OFPACT_SET_VLAN_VID:
4637 case OFPACT_CLEAR_ACTIONS:
4638 case OFPACT_CONTROLLER:
4639 case OFPACT_DEC_MPLS_TTL:
4640 case OFPACT_DEC_TTL:
4641 case OFPACT_ENQUEUE:
4643 case OFPACT_FIN_TIMEOUT:
4644 case OFPACT_GOTO_TABLE:
4648 case OFPACT_MULTIPATH:
4651 case OFPACT_OUTPUT_REG:
4652 case OFPACT_POP_MPLS:
4653 case OFPACT_POP_QUEUE:
4654 case OFPACT_PUSH_MPLS:
4655 case OFPACT_PUSH_VLAN:
4656 case OFPACT_RESUBMIT:
4658 case OFPACT_STACK_POP:
4659 case OFPACT_STACK_PUSH:
4660 case OFPACT_STRIP_VLAN:
4661 case OFPACT_WRITE_ACTIONS:
4662 case OFPACT_WRITE_METADATA:
4669 /* True if an action is allowed in the action set.
4670 * False otherwise. */
4672 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
4675 case OFPACT_DEC_MPLS_TTL:
4676 case OFPACT_DEC_TTL:
4679 case OFPACT_POP_MPLS:
4680 case OFPACT_PUSH_MPLS:
4681 case OFPACT_PUSH_VLAN:
4682 case OFPACT_REG_MOVE:
4683 case OFPACT_SET_FIELD:
4684 case OFPACT_SET_ETH_DST:
4685 case OFPACT_SET_ETH_SRC:
4686 case OFPACT_SET_IP_DSCP:
4687 case OFPACT_SET_IP_ECN:
4688 case OFPACT_SET_IP_TTL:
4689 case OFPACT_SET_IPV4_DST:
4690 case OFPACT_SET_IPV4_SRC:
4691 case OFPACT_SET_L4_DST_PORT:
4692 case OFPACT_SET_L4_SRC_PORT:
4693 case OFPACT_SET_MPLS_LABEL:
4694 case OFPACT_SET_MPLS_TC:
4695 case OFPACT_SET_MPLS_TTL:
4696 case OFPACT_SET_QUEUE:
4697 case OFPACT_SET_TUNNEL:
4698 case OFPACT_SET_VLAN_PCP:
4699 case OFPACT_SET_VLAN_VID:
4700 case OFPACT_STRIP_VLAN:
4703 /* In general these actions are excluded because they are not part of
4704 * the OpenFlow specification nor map to actions that are defined in
4705 * the specification. Thus the order in which they should be applied
4706 * in the action set is undefined. */
4708 case OFPACT_CONTROLLER:
4709 case OFPACT_ENQUEUE:
4711 case OFPACT_FIN_TIMEOUT:
4713 case OFPACT_MULTIPATH:
4715 case OFPACT_OUTPUT_REG:
4716 case OFPACT_POP_QUEUE:
4717 case OFPACT_RESUBMIT:
4719 case OFPACT_STACK_POP:
4720 case OFPACT_STACK_PUSH:
4722 /* The action set may only include actions and thus
4723 * may not include any instructions */
4724 case OFPACT_CLEAR_ACTIONS:
4725 case OFPACT_GOTO_TABLE:
4727 case OFPACT_WRITE_ACTIONS:
4728 case OFPACT_WRITE_METADATA:
4735 /* Append ofpact 'a' onto the tail of 'out' */
4737 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
4739 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
4742 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
4744 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
4745 enum ofpact_type filter)
4747 const struct ofpact *target;
4748 const struct ofpact *a;
4751 OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
4752 if (a->type == filter) {
4757 ofpact_copy(out, target);
4759 return target != NULL;
4762 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
4763 * The order of appended ofpacts is preserved between 'in' and 'out' */
4765 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
4766 bool (*filter)(const struct ofpact *))
4768 const struct ofpact *a;
4770 OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
4772 ofpact_copy(out, a);
4777 /* Reads 'action_set', which contains ofpacts accumulated by
4778 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
4779 * executed directly into 'action_list'. (These names correspond to the
4780 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
4782 * In general this involves appending the last instance of each action that is
4783 * adimissible in the action set in the order described in the OpenFlow
4787 * + output action is only appended if no group action was present in 'in'.
4788 * + As a simplification all set actions are copied in the order the are
4789 * provided in 'in' as many set actions applied to a field has the same
4790 * affect as only applying the last action that sets a field and
4791 * duplicates are removed by do_xlate_actions().
4792 * This has an unwanted side-effect of compsoting multiple
4793 * LOAD_REG actions that touch different regions of the same field. */
4795 ofpacts_execute_action_set(struct ofpbuf *action_list,
4796 const struct ofpbuf *action_set)
4798 /* The OpenFlow spec "Action Set" section specifies this order. */
4799 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
4800 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
4801 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
4802 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
4803 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
4804 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
4805 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
4806 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
4808 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
4809 * we should execute only OFPACT_GROUP.
4811 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
4812 * all the actions because there's no point in modifying a packet that will
4813 * not be sent anywhere. */
4814 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
4815 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
4816 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
4817 ofpbuf_clear(action_list);
4823 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
4824 size_t n_in, enum ofp_version version,
4829 size_t start = ofpbuf_size(out);
4831 error = ofpacts_decode(in, n_in, version, out);
4837 OFPACT_FOR_EACH (a, ofpact_end(ofpbuf_data(out), start), ofpbuf_size(out) - start) {
4838 if (!ofpact_is_allowed_in_actions_set(a)) {
4839 VLOG_WARN_RL(&rl, "disallowed action in action set");
4840 return OFPERR_OFPBAC_BAD_TYPE;
4847 /* OpenFlow 1.1 instructions. */
4849 struct instruction_type_info {
4850 enum ovs_instruction_type type;
4854 static const struct instruction_type_info inst_info[] = {
4855 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
4861 ovs_instruction_name_from_type(enum ovs_instruction_type type)
4863 return inst_info[type].name;
4867 ovs_instruction_type_from_name(const char *name)
4869 const struct instruction_type_info *p;
4870 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
4871 if (!strcasecmp(name, p->name)) {
4878 enum ovs_instruction_type
4879 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
4883 return OVSINST_OFPIT13_METER;
4884 case OFPACT_CLEAR_ACTIONS:
4885 return OVSINST_OFPIT11_CLEAR_ACTIONS;
4886 case OFPACT_WRITE_ACTIONS:
4887 return OVSINST_OFPIT11_WRITE_ACTIONS;
4888 case OFPACT_WRITE_METADATA:
4889 return OVSINST_OFPIT11_WRITE_METADATA;
4890 case OFPACT_GOTO_TABLE:
4891 return OVSINST_OFPIT11_GOTO_TABLE;
4894 case OFPACT_CONTROLLER:
4895 case OFPACT_ENQUEUE:
4896 case OFPACT_OUTPUT_REG:
4898 case OFPACT_SET_VLAN_VID:
4899 case OFPACT_SET_VLAN_PCP:
4900 case OFPACT_STRIP_VLAN:
4901 case OFPACT_PUSH_VLAN:
4902 case OFPACT_SET_ETH_SRC:
4903 case OFPACT_SET_ETH_DST:
4904 case OFPACT_SET_IPV4_SRC:
4905 case OFPACT_SET_IPV4_DST:
4906 case OFPACT_SET_IP_DSCP:
4907 case OFPACT_SET_IP_ECN:
4908 case OFPACT_SET_IP_TTL:
4909 case OFPACT_SET_L4_SRC_PORT:
4910 case OFPACT_SET_L4_DST_PORT:
4911 case OFPACT_REG_MOVE:
4912 case OFPACT_SET_FIELD:
4913 case OFPACT_STACK_PUSH:
4914 case OFPACT_STACK_POP:
4915 case OFPACT_DEC_TTL:
4916 case OFPACT_SET_MPLS_LABEL:
4917 case OFPACT_SET_MPLS_TC:
4918 case OFPACT_SET_MPLS_TTL:
4919 case OFPACT_DEC_MPLS_TTL:
4920 case OFPACT_PUSH_MPLS:
4921 case OFPACT_POP_MPLS:
4922 case OFPACT_SET_TUNNEL:
4923 case OFPACT_SET_QUEUE:
4924 case OFPACT_POP_QUEUE:
4925 case OFPACT_FIN_TIMEOUT:
4926 case OFPACT_RESUBMIT:
4928 case OFPACT_MULTIPATH:
4933 return OVSINST_OFPIT11_APPLY_ACTIONS;
4938 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
4939 const uint16_t inst_type)
4941 switch (inst_type) {
4943 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
4945 *instruction_type = OVSINST_##ENUM; \
4951 return OFPERR_OFPBIC_UNKNOWN_INST;
4955 /* Two-way translation between OVS's internal "OVSINST_*" representation of
4956 * instructions and the "OFPIT_*" representation used in OpenFlow. */
4957 struct ovsinst_map {
4958 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
4959 int ofpit; /* OFPIT_* number from OpenFlow spec. */
4962 static const struct ovsinst_map *
4963 get_ovsinst_map(enum ofp_version version)
4965 /* OpenFlow 1.1 and 1.2 instructions. */
4966 static const struct ovsinst_map of11[] = {
4967 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
4968 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
4969 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
4970 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
4971 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
4975 /* OpenFlow 1.3+ instructions. */
4976 static const struct ovsinst_map of13[] = {
4977 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
4978 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
4979 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
4980 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
4981 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
4982 { OVSINST_OFPIT13_METER, 6 },
4986 return version < OFP13_VERSION ? of11 : of13;
4989 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
4990 * values, into a bitmap of instructions suitable for OpenFlow 'version'
4991 * (OFP11_VERSION or later), and returns the result. */
4993 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
4995 uint32_t ofpit_bitmap = 0;
4996 const struct ovsinst_map *x;
4998 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
4999 if (ovsinst_bitmap & (1u << x->ovsinst)) {
5000 ofpit_bitmap |= 1u << x->ofpit;
5003 return htonl(ofpit_bitmap);
5006 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5007 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5008 * correspond to OVSINST_* values, and returns the result. */
5010 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
5012 uint32_t ovsinst_bitmap = 0;
5013 const struct ovsinst_map *x;
5015 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5016 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
5017 ovsinst_bitmap |= 1u << x->ovsinst;
5020 return ovsinst_bitmap;
5023 static inline struct ofp11_instruction *
5024 instruction_next(const struct ofp11_instruction *inst)
5026 return ((struct ofp11_instruction *) (void *)
5027 ((uint8_t *) inst + ntohs(inst->len)));
5031 instruction_is_valid(const struct ofp11_instruction *inst,
5032 size_t n_instructions)
5034 uint16_t len = ntohs(inst->len);
5035 return (!(len % OFP11_INSTRUCTION_ALIGN)
5036 && len >= sizeof *inst
5037 && len / sizeof *inst <= n_instructions);
5040 /* This macro is careful to check for instructions with bad lengths. */
5041 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
5042 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
5043 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
5044 ((LEFT) -= (ntohs((ITER)->len) \
5045 / sizeof(struct ofp11_instruction)), \
5046 (ITER) = instruction_next(ITER)))
5049 decode_openflow11_instruction(const struct ofp11_instruction *inst,
5050 enum ovs_instruction_type *type)
5052 uint16_t len = ntohs(inst->len);
5054 switch (inst->type) {
5055 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5056 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5058 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5059 case CONSTANT_HTONS(ENUM): \
5061 ? len >= sizeof(struct STRUCT) \
5062 : len == sizeof(struct STRUCT)) { \
5063 *type = OVSINST_##ENUM; \
5066 return OFPERR_OFPBIC_BAD_LEN; \
5072 return OFPERR_OFPBIC_UNKNOWN_INST;
5077 decode_openflow11_instructions(const struct ofp11_instruction insts[],
5079 const struct ofp11_instruction *out[])
5081 const struct ofp11_instruction *inst;
5084 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5085 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5086 enum ovs_instruction_type type;
5089 error = decode_openflow11_instruction(inst, &type);
5095 return OFPERR_OFPBIC_DUP_INST;
5101 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5102 (n_insts - left) * sizeof *inst);
5103 return OFPERR_OFPBIC_BAD_LEN;
5109 get_actions_from_instruction(const struct ofp11_instruction *inst,
5110 const struct ofp_action_header **actions,
5111 size_t *actions_len)
5113 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5114 *actions_len = ntohs(inst->len) - sizeof *inst;
5118 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5119 unsigned int instructions_len,
5120 enum ofp_version version,
5121 struct ofpbuf *ofpacts)
5123 const struct ofp11_instruction *instructions;
5124 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5127 if (version == OFP10_VERSION) {
5128 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5130 (1u << N_OVS_INSTRUCTIONS) - 1,
5134 ofpbuf_clear(ofpacts);
5136 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5137 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5139 instructions_len, OFP11_INSTRUCTION_ALIGN);
5140 error = OFPERR_OFPBIC_BAD_LEN;
5144 instructions = ofpbuf_try_pull(openflow, instructions_len);
5145 if (instructions == NULL) {
5146 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5147 "remaining message length (%"PRIu32")",
5148 instructions_len, ofpbuf_size(openflow));
5149 error = OFPERR_OFPBIC_BAD_LEN;
5153 error = decode_openflow11_instructions(
5154 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5160 if (insts[OVSINST_OFPIT13_METER]) {
5161 const struct ofp13_instruction_meter *oim;
5162 struct ofpact_meter *om;
5164 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5165 insts[OVSINST_OFPIT13_METER]);
5167 om = ofpact_put_METER(ofpacts);
5168 om->meter_id = ntohl(oim->meter_id);
5170 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5171 const struct ofp_action_header *actions;
5174 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5175 &actions, &actions_len);
5176 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5181 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5182 instruction_get_OFPIT11_CLEAR_ACTIONS(
5183 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5184 ofpact_put_CLEAR_ACTIONS(ofpacts);
5186 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5187 struct ofpact_nest *on;
5188 const struct ofp_action_header *actions;
5192 ofpact_pad(ofpacts);
5193 start = ofpbuf_size(ofpacts);
5194 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5195 offsetof(struct ofpact_nest, actions));
5196 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5197 &actions, &actions_len);
5198 error = ofpacts_decode_for_action_set(actions, actions_len,
5203 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5204 on->ofpact.len = ofpbuf_size(ofpacts) - start;
5206 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5207 const struct ofp11_instruction_write_metadata *oiwm;
5208 struct ofpact_metadata *om;
5210 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5211 insts[OVSINST_OFPIT11_WRITE_METADATA]);
5213 om = ofpact_put_WRITE_METADATA(ofpacts);
5214 om->metadata = oiwm->metadata;
5215 om->mask = oiwm->metadata_mask;
5217 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5218 const struct ofp11_instruction_goto_table *oigt;
5219 struct ofpact_goto_table *ogt;
5221 oigt = instruction_get_OFPIT11_GOTO_TABLE(
5222 insts[OVSINST_OFPIT11_GOTO_TABLE]);
5223 ogt = ofpact_put_GOTO_TABLE(ofpacts);
5224 ogt->table_id = oigt->table_id;
5227 error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
5228 (1u << N_OVS_INSTRUCTIONS) - 1);
5231 ofpbuf_clear(ofpacts);
5236 /* Update the length of the instruction that begins at offset 'ofs' within
5237 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5238 * If the instruction contains no nested actions, deletes it entirely. */
5240 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5242 struct ofp11_instruction_actions *oia;
5244 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5245 if (ofpbuf_size(openflow) > ofs + sizeof *oia) {
5246 oia->len = htons(ofpbuf_size(openflow) - ofs);
5248 ofpbuf_set_size(openflow, ofs);
5252 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5253 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5254 * valid, otherwise an OpenFlow error code. */
5256 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5264 case OFPP_CONTROLLER:
5270 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
5273 return OFPERR_OFPBAC_BAD_OUT_PORT;
5277 /* Removes the protocols that require consistency between match and actions
5278 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5280 * (An example of an inconsistency between match and actions is a flow that
5281 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5284 inconsistent_match(enum ofputil_protocol *usable_protocols)
5286 *usable_protocols &= OFPUTIL_P_OF10_ANY;
5289 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5290 * caller must restore them.
5292 * Modifies some actions, filling in fields that could not be properly set
5293 * without context. */
5295 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
5296 struct flow *flow, ofp_port_t max_ports,
5297 uint8_t table_id, uint8_t n_tables)
5299 const struct ofpact_enqueue *enqueue;
5300 const struct mf_field *mf;
5304 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
5307 case OFPACT_CONTROLLER:
5310 case OFPACT_ENQUEUE:
5311 enqueue = ofpact_get_ENQUEUE(a);
5312 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
5313 && enqueue->port != OFPP_IN_PORT
5314 && enqueue->port != OFPP_LOCAL) {
5315 return OFPERR_OFPBAC_BAD_OUT_PORT;
5319 case OFPACT_OUTPUT_REG:
5320 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
5323 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
5325 case OFPACT_SET_VLAN_VID:
5326 /* Remember if we saw a vlan tag in the flow to aid translating to
5327 * OpenFlow 1.1+ if need be. */
5328 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
5329 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5330 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5331 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5332 inconsistent_match(usable_protocols);
5334 /* Temporary mark that we have a vlan tag. */
5335 flow->vlan_tci |= htons(VLAN_CFI);
5338 case OFPACT_SET_VLAN_PCP:
5339 /* Remember if we saw a vlan tag in the flow to aid translating to
5340 * OpenFlow 1.1+ if need be. */
5341 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
5342 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5343 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5344 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5345 inconsistent_match(usable_protocols);
5347 /* Temporary mark that we have a vlan tag. */
5348 flow->vlan_tci |= htons(VLAN_CFI);
5351 case OFPACT_STRIP_VLAN:
5352 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5353 inconsistent_match(usable_protocols);
5355 /* Temporary mark that we have no vlan tag. */
5356 flow->vlan_tci = htons(0);
5359 case OFPACT_PUSH_VLAN:
5360 if (flow->vlan_tci & htons(VLAN_CFI)) {
5361 /* Multiple VLAN headers not supported. */
5362 return OFPERR_OFPBAC_BAD_TAG;
5364 /* Temporary mark that we have a vlan tag. */
5365 flow->vlan_tci |= htons(VLAN_CFI);
5368 case OFPACT_SET_ETH_SRC:
5369 case OFPACT_SET_ETH_DST:
5372 case OFPACT_SET_IPV4_SRC:
5373 case OFPACT_SET_IPV4_DST:
5374 if (flow->dl_type != htons(ETH_TYPE_IP)) {
5375 inconsistent_match(usable_protocols);
5379 case OFPACT_SET_IP_DSCP:
5380 case OFPACT_SET_IP_ECN:
5381 case OFPACT_SET_IP_TTL:
5382 case OFPACT_DEC_TTL:
5383 if (!is_ip_any(flow)) {
5384 inconsistent_match(usable_protocols);
5388 case OFPACT_SET_L4_SRC_PORT:
5389 case OFPACT_SET_L4_DST_PORT:
5390 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
5391 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5392 && flow->nw_proto != IPPROTO_SCTP)) {
5393 inconsistent_match(usable_protocols);
5395 /* Note on which transport protocol the port numbers are set.
5396 * This allows this set action to be converted to an OF1.2 set field
5398 if (a->type == OFPACT_SET_L4_SRC_PORT) {
5399 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
5401 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
5405 case OFPACT_REG_MOVE:
5406 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
5408 case OFPACT_SET_FIELD:
5409 mf = ofpact_get_SET_FIELD(a)->field;
5410 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
5411 if (!mf_are_prereqs_ok(mf, flow) ||
5412 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
5413 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
5415 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5417 /* Remember if we saw a vlan tag in the flow to aid translating to
5418 * OpenFlow 1.1 if need be. */
5419 ofpact_get_SET_FIELD(a)->flow_has_vlan =
5420 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5421 if (mf->id == MFF_VLAN_TCI) {
5422 /* The set field may add or remove the vlan tag,
5423 * Mark the status temporarily. */
5424 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
5428 case OFPACT_STACK_PUSH:
5429 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
5431 case OFPACT_STACK_POP:
5432 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
5434 case OFPACT_SET_MPLS_LABEL:
5435 case OFPACT_SET_MPLS_TC:
5436 case OFPACT_SET_MPLS_TTL:
5437 case OFPACT_DEC_MPLS_TTL:
5438 if (!eth_type_mpls(flow->dl_type)) {
5439 inconsistent_match(usable_protocols);
5443 case OFPACT_SET_TUNNEL:
5444 case OFPACT_SET_QUEUE:
5445 case OFPACT_POP_QUEUE:
5446 case OFPACT_RESUBMIT:
5449 case OFPACT_FIN_TIMEOUT:
5450 if (flow->nw_proto != IPPROTO_TCP) {
5451 inconsistent_match(usable_protocols);
5456 return learn_check(ofpact_get_LEARN(a), flow);
5458 case OFPACT_MULTIPATH:
5459 return multipath_check(ofpact_get_MULTIPATH(a), flow);
5465 case OFPACT_PUSH_MPLS:
5466 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
5467 /* The packet is now MPLS and the MPLS payload is opaque.
5468 * Thus nothing can be assumed about the network protocol.
5469 * Temporarily mark that we have no nw_proto. */
5473 case OFPACT_POP_MPLS:
5474 if (!eth_type_mpls(flow->dl_type)) {
5475 inconsistent_match(usable_protocols);
5477 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
5483 case OFPACT_CLEAR_ACTIONS:
5486 case OFPACT_WRITE_ACTIONS: {
5487 /* Use a temporary copy of 'usable_protocols' because we can't check
5488 * consistency of an action set. */
5489 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
5490 enum ofputil_protocol p = *usable_protocols;
5491 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
5492 flow, max_ports, table_id, n_tables, &p);
5495 case OFPACT_WRITE_METADATA:
5498 case OFPACT_METER: {
5499 uint32_t mid = ofpact_get_METER(a)->meter_id;
5500 if (mid == 0 || mid > OFPM13_MAX) {
5501 return OFPERR_OFPMMFC_INVALID_METER;
5506 case OFPACT_GOTO_TABLE: {
5507 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
5508 if ((table_id != 255 && goto_table <= table_id)
5509 || (n_tables != 255 && goto_table >= n_tables)) {
5510 return OFPERR_OFPBIC_BAD_TABLE_ID;
5523 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5524 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
5525 * switch with no more than 'max_ports' ports.
5527 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
5528 * '*usable_protocols' the protocols that forbid the inconsistency. (An
5529 * example of an inconsistency between match and actions is a flow that does
5530 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
5532 * May annotate ofpacts with information gathered from the 'flow'.
5534 * May temporarily modify 'flow', but restores the changes before returning. */
5536 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
5537 struct flow *flow, ofp_port_t max_ports,
5538 uint8_t table_id, uint8_t n_tables,
5539 enum ofputil_protocol *usable_protocols)
5542 ovs_be16 dl_type = flow->dl_type;
5543 ovs_be16 vlan_tci = flow->vlan_tci;
5544 uint8_t nw_proto = flow->nw_proto;
5545 enum ofperr error = 0;
5547 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5548 error = ofpact_check__(usable_protocols, a, flow,
5549 max_ports, table_id, n_tables);
5554 /* Restore fields that may have been modified. */
5555 flow->dl_type = dl_type;
5556 flow->vlan_tci = vlan_tci;
5557 flow->nw_proto = nw_proto;
5561 /* Like ofpacts_check(), but reports inconsistencies as
5562 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
5564 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
5565 struct flow *flow, ofp_port_t max_ports,
5566 uint8_t table_id, uint8_t n_tables,
5567 enum ofputil_protocol usable_protocols)
5569 enum ofputil_protocol p = usable_protocols;
5572 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
5573 table_id, n_tables, &p);
5574 return (error ? error
5575 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
5579 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5580 * in the appropriate order as defined by the OpenFlow spec. */
5582 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
5583 uint32_t allowed_ovsinsts)
5585 const struct ofpact *a;
5586 enum ovs_instruction_type inst;
5588 inst = OVSINST_OFPIT13_METER;
5589 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5590 enum ovs_instruction_type next;
5592 next = ovs_instruction_type_from_ofpact_type(a->type);
5594 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
5597 const char *name = ovs_instruction_name_from_type(inst);
5598 const char *next_name = ovs_instruction_name_from_type(next);
5601 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
5602 "1.1+ compatibility", name);
5604 VLOG_WARN("invalid instruction ordering: %s must appear "
5605 "before %s, for OpenFlow 1.1+ compatibility",
5608 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
5610 if (!((1u << next) & allowed_ovsinsts)) {
5611 const char *name = ovs_instruction_name_from_type(next);
5613 VLOG_WARN("%s instruction not allowed here", name);
5614 return OFPERR_OFPBIC_UNSUP_INST;
5623 /* Converting ofpacts to OpenFlow. */
5626 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
5630 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5631 case OFPACT_##ENUM: \
5632 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
5641 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
5642 * actions in 'openflow', appending the actions to any existing data in
5645 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
5646 struct ofpbuf *openflow,
5647 enum ofp_version ofp_version)
5649 const struct ofpact *a;
5650 size_t start_size = ofpbuf_size(openflow);
5652 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5653 encode_ofpact(a, ofp_version, openflow);
5655 return ofpbuf_size(openflow) - start_size;
5658 static enum ovs_instruction_type
5659 ofpact_is_apply_actions(const struct ofpact *a)
5661 return (ovs_instruction_type_from_ofpact_type(a->type)
5662 == OVSINST_OFPIT11_APPLY_ACTIONS);
5666 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
5668 struct ofpbuf *openflow,
5669 enum ofp_version ofp_version)
5671 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
5672 const struct ofpact *a;
5674 if (ofp_version == OFP10_VERSION) {
5675 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
5682 if (ofpact_is_apply_actions(a)) {
5683 size_t ofs = ofpbuf_size(openflow);
5685 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
5687 encode_ofpact(a, ofp_version, openflow);
5689 } while (a < end && ofpact_is_apply_actions(a));
5690 ofpacts_update_instruction_actions(openflow, ofs);
5692 encode_ofpact(a, ofp_version, openflow);
5698 /* Sets of supported actions. */
5700 /* Two-way translation between OVS's internal "OFPACT_*" representation of
5701 * actions and the "OFPAT_*" representation used in some OpenFlow version.
5702 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
5703 * instance is specific to one OpenFlow version.) */
5705 enum ofpact_type ofpact; /* Internal name for action type. */
5706 int ofpat; /* OFPAT_* number from OpenFlow spec. */
5709 static const struct ofpact_map *
5710 get_ofpact_map(enum ofp_version version)
5712 /* OpenFlow 1.0 actions. */
5713 static const struct ofpact_map of10[] = {
5714 { OFPACT_OUTPUT, 0 },
5715 { OFPACT_SET_VLAN_VID, 1 },
5716 { OFPACT_SET_VLAN_PCP, 2 },
5717 { OFPACT_STRIP_VLAN, 3 },
5718 { OFPACT_SET_ETH_SRC, 4 },
5719 { OFPACT_SET_ETH_DST, 5 },
5720 { OFPACT_SET_IPV4_SRC, 6 },
5721 { OFPACT_SET_IPV4_DST, 7 },
5722 { OFPACT_SET_IP_DSCP, 8 },
5723 { OFPACT_SET_L4_SRC_PORT, 9 },
5724 { OFPACT_SET_L4_DST_PORT, 10 },
5725 { OFPACT_ENQUEUE, 11 },
5729 /* OpenFlow 1.1 actions. */
5730 static const struct ofpact_map of11[] = {
5731 { OFPACT_OUTPUT, 0 },
5732 { OFPACT_SET_VLAN_VID, 1 },
5733 { OFPACT_SET_VLAN_PCP, 2 },
5734 { OFPACT_SET_ETH_SRC, 3 },
5735 { OFPACT_SET_ETH_DST, 4 },
5736 { OFPACT_SET_IPV4_SRC, 5 },
5737 { OFPACT_SET_IPV4_DST, 6 },
5738 { OFPACT_SET_IP_DSCP, 7 },
5739 { OFPACT_SET_IP_ECN, 8 },
5740 { OFPACT_SET_L4_SRC_PORT, 9 },
5741 { OFPACT_SET_L4_DST_PORT, 10 },
5742 /* OFPAT_COPY_TTL_OUT (11) not supported. */
5743 /* OFPAT_COPY_TTL_IN (12) not supported. */
5744 { OFPACT_SET_MPLS_LABEL, 13 },
5745 { OFPACT_SET_MPLS_TC, 14 },
5746 { OFPACT_SET_MPLS_TTL, 15 },
5747 { OFPACT_DEC_MPLS_TTL, 16 },
5748 { OFPACT_PUSH_VLAN, 17 },
5749 { OFPACT_STRIP_VLAN, 18 },
5750 { OFPACT_PUSH_MPLS, 19 },
5751 { OFPACT_POP_MPLS, 20 },
5752 { OFPACT_SET_QUEUE, 21 },
5753 { OFPACT_GROUP, 22 },
5754 { OFPACT_SET_IP_TTL, 23 },
5755 { OFPACT_DEC_TTL, 24 },
5759 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
5760 static const struct ofpact_map of12[] = {
5761 { OFPACT_OUTPUT, 0 },
5762 /* OFPAT_COPY_TTL_OUT (11) not supported. */
5763 /* OFPAT_COPY_TTL_IN (12) not supported. */
5764 { OFPACT_SET_MPLS_TTL, 15 },
5765 { OFPACT_DEC_MPLS_TTL, 16 },
5766 { OFPACT_PUSH_VLAN, 17 },
5767 { OFPACT_STRIP_VLAN, 18 },
5768 { OFPACT_PUSH_MPLS, 19 },
5769 { OFPACT_POP_MPLS, 20 },
5770 { OFPACT_SET_QUEUE, 21 },
5771 { OFPACT_GROUP, 22 },
5772 { OFPACT_SET_IP_TTL, 23 },
5773 { OFPACT_DEC_TTL, 24 },
5774 { OFPACT_SET_FIELD, 25 },
5775 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
5776 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
5796 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
5797 * values, into a bitmap of actions suitable for OpenFlow 'version', and
5798 * returns the result. */
5800 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
5802 uint32_t openflow_bitmap = 0;
5803 const struct ofpact_map *x;
5805 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
5806 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
5807 openflow_bitmap |= 1u << x->ofpat;
5810 return htonl(openflow_bitmap);
5813 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
5814 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
5815 * and returns the result. */
5817 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
5819 uint64_t ofpact_bitmap = 0;
5820 const struct ofpact_map *x;
5822 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
5823 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
5824 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
5827 return ofpact_bitmap;
5830 /* Appends to 's' a string representation of the set of OFPACT_* represented
5831 * by 'ofpacts_bitmap'. */
5833 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
5835 if (!ofpacts_bitmap) {
5836 ds_put_cstr(s, "<none>");
5838 while (ofpacts_bitmap) {
5839 ds_put_format(s, "%s ",
5840 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
5841 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
5847 /* Returns true if 'action' outputs to 'port', false otherwise. */
5849 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
5851 switch (ofpact->type) {
5853 return ofpact_get_OUTPUT(ofpact)->port == port;
5854 case OFPACT_ENQUEUE:
5855 return ofpact_get_ENQUEUE(ofpact)->port == port;
5856 case OFPACT_CONTROLLER:
5857 return port == OFPP_CONTROLLER;
5859 case OFPACT_OUTPUT_REG:
5861 case OFPACT_SET_VLAN_VID:
5862 case OFPACT_SET_VLAN_PCP:
5863 case OFPACT_STRIP_VLAN:
5864 case OFPACT_PUSH_VLAN:
5865 case OFPACT_SET_ETH_SRC:
5866 case OFPACT_SET_ETH_DST:
5867 case OFPACT_SET_IPV4_SRC:
5868 case OFPACT_SET_IPV4_DST:
5869 case OFPACT_SET_IP_DSCP:
5870 case OFPACT_SET_IP_ECN:
5871 case OFPACT_SET_IP_TTL:
5872 case OFPACT_SET_L4_SRC_PORT:
5873 case OFPACT_SET_L4_DST_PORT:
5874 case OFPACT_REG_MOVE:
5875 case OFPACT_SET_FIELD:
5876 case OFPACT_STACK_PUSH:
5877 case OFPACT_STACK_POP:
5878 case OFPACT_DEC_TTL:
5879 case OFPACT_SET_MPLS_LABEL:
5880 case OFPACT_SET_MPLS_TC:
5881 case OFPACT_SET_MPLS_TTL:
5882 case OFPACT_DEC_MPLS_TTL:
5883 case OFPACT_SET_TUNNEL:
5884 case OFPACT_WRITE_METADATA:
5885 case OFPACT_SET_QUEUE:
5886 case OFPACT_POP_QUEUE:
5887 case OFPACT_FIN_TIMEOUT:
5888 case OFPACT_RESUBMIT:
5890 case OFPACT_MULTIPATH:
5893 case OFPACT_PUSH_MPLS:
5894 case OFPACT_POP_MPLS:
5896 case OFPACT_CLEAR_ACTIONS:
5897 case OFPACT_WRITE_ACTIONS:
5898 case OFPACT_GOTO_TABLE:
5906 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
5907 * to 'port', false otherwise. */
5909 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
5912 const struct ofpact *a;
5914 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5915 if (ofpact_outputs_to_port(a, port)) {
5923 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
5924 * to 'group', false otherwise. */
5926 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
5929 const struct ofpact *a;
5931 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5932 if (a->type == OFPACT_GROUP
5933 && ofpact_get_GROUP(a)->group_id == group_id) {
5942 ofpacts_equal(const struct ofpact *a, size_t a_len,
5943 const struct ofpact *b, size_t b_len)
5945 return a_len == b_len && !memcmp(a, b, a_len);
5948 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
5949 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
5951 * This function relies on the order of 'ofpacts' being correct (as checked by
5952 * ofpacts_verify()). */
5954 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
5956 const struct ofpact *a;
5958 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5959 enum ovs_instruction_type inst;
5961 inst = ovs_instruction_type_from_ofpact_type(a->type);
5962 if (a->type == OFPACT_METER) {
5963 return ofpact_get_METER(a)->meter_id;
5964 } else if (inst > OVSINST_OFPIT13_METER) {
5972 /* Formatting ofpacts. */
5975 ofpact_format(const struct ofpact *a, struct ds *s)
5978 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5979 case OFPACT_##ENUM: \
5980 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
5989 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
5990 * 'ofpacts' to 'string'. */
5992 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
5996 ds_put_cstr(string, "drop");
5998 const struct ofpact *a;
6000 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6002 ds_put_cstr(string, ",");
6005 /* XXX write-actions */
6006 ofpact_format(a, string);
6011 /* Internal use by helpers. */
6014 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
6016 struct ofpact *ofpact;
6018 ofpact_pad(ofpacts);
6019 ofpact = ofpacts->frame = ofpbuf_put_uninit(ofpacts, len);
6020 ofpact_init(ofpact, type, len);
6025 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
6027 memset(ofpact, 0, len);
6028 ofpact->type = type;
6033 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6034 * starting at 'ofpact'.
6036 * This is the correct way to update a variable-length ofpact's length after
6037 * adding the variable-length part of the payload. (See the large comment
6038 * near the end of ofp-actions.h for more information.) */
6040 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
6042 ovs_assert(ofpact == ofpacts->frame);
6043 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6046 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6047 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6048 * client must call this itself after adding the final ofpact to an array of
6051 * (The consequences of failing to call this function are probably not dire.
6052 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6053 * not dereference it. That's undefined behavior, technically, but it will not
6054 * cause a real problem on common systems. Still, it seems better to call
6057 ofpact_pad(struct ofpbuf *ofpacts)
6059 unsigned int pad = PAD_SIZE(ofpbuf_size(ofpacts), OFPACT_ALIGNTO);
6061 ofpbuf_put_zeros(ofpacts, pad);
6068 static char * OVS_WARN_UNUSED_RESULT
6069 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6070 enum ofputil_protocol *usable_protocols)
6073 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6074 case OFPACT_##ENUM: \
6075 return parse_##ENUM(value, ofpacts, usable_protocols);
6084 ofpact_type_from_name(const char *name, enum ofpact_type *type)
6086 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6087 if (!strcasecmp(name, NAME)) { \
6088 *type = OFPACT_##ENUM; \
6097 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6099 * Returns NULL if successful, otherwise a malloc()'d string describing the
6100 * error. The caller is responsible for freeing the returned string. */
6101 static char * OVS_WARN_UNUSED_RESULT
6102 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6103 enum ofputil_protocol *usable_protocols,
6104 bool allow_instructions)
6113 while (ofputil_parse_key_value(&pos, &key, &value)) {
6114 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6115 enum ofpact_type type;
6119 if (ofpact_type_from_name(key, &type)) {
6120 error = ofpact_parse(type, value, ofpacts, usable_protocols);
6121 inst = ovs_instruction_type_from_ofpact_type(type);
6122 } else if (!strcasecmp(key, "mod_vlan_vid")) {
6123 error = parse_set_vlan_vid(value, ofpacts, true);
6124 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6125 error = parse_set_vlan_pcp(value, ofpacts, true);
6126 } else if (!strcasecmp(key, "set_nw_ttl")) {
6127 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6128 } else if (!strcasecmp(key, "pop_vlan")) {
6129 error = parse_pop_vlan(ofpacts);
6130 } else if (!strcasecmp(key, "set_tunnel64")) {
6131 error = parse_set_tunnel(value, ofpacts,
6132 NXAST_RAW_SET_TUNNEL64);
6133 } else if (!strcasecmp(key, "load")) {
6134 error = parse_reg_load(value, ofpacts);
6135 } else if (!strcasecmp(key, "bundle_load")) {
6136 error = parse_bundle_load(value, ofpacts);
6137 } else if (!strcasecmp(key, "drop")) {
6139 } else if (!strcasecmp(key, "apply_actions")) {
6140 return xstrdup("apply_actions is the default instruction");
6141 } else if (ofputil_port_from_string(key, &port)) {
6142 ofpact_put_OUTPUT(ofpacts)->port = port;
6144 return xasprintf("unknown action %s", key);
6150 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
6151 if (!allow_instructions) {
6152 return xasprintf("only actions are allowed here (not "
6154 ovs_instruction_name_from_type(inst));
6156 if (inst == prev_inst) {
6157 return xasprintf("instruction %s may be specified only once",
6158 ovs_instruction_name_from_type(inst));
6161 if (prev_inst != -1 && inst < prev_inst) {
6162 return xasprintf("instruction %s must be specified before %s",
6163 ovs_instruction_name_from_type(inst),
6164 ovs_instruction_name_from_type(prev_inst));
6168 ofpact_pad(ofpacts);
6170 if (drop && ofpbuf_size(ofpacts)) {
6171 return xstrdup("\"drop\" must not be accompanied by any other action "
6175 retval = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
6177 ? (1u << N_OVS_INSTRUCTIONS) - 1
6178 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS));
6180 return xstrdup("Incorrect instruction ordering");
6186 static char * OVS_WARN_UNUSED_RESULT
6187 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
6188 enum ofputil_protocol *usable_protocols, bool allow_instructions)
6190 uint32_t orig_size = ofpbuf_size(ofpacts);
6191 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
6192 allow_instructions);
6194 ofpbuf_set_size(ofpacts, orig_size);
6199 static char * OVS_WARN_UNUSED_RESULT
6200 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
6201 enum ofputil_protocol *usable_protocols,
6202 bool allow_instructions)
6206 *usable_protocols = OFPUTIL_P_ANY;
6209 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions);
6215 /* Parses 's' as a set of OpenFlow actions and appends the actions to
6218 * Returns NULL if successful, otherwise a malloc()'d string describing the
6219 * error. The caller is responsible for freeing the returned string. */
6220 char * OVS_WARN_UNUSED_RESULT
6221 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
6222 enum ofputil_protocol *usable_protocols)
6224 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false);
6227 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
6230 * Returns NULL if successful, otherwise a malloc()'d string describing the
6231 * error. The caller is responsible for freeing the returned string. */
6232 char * OVS_WARN_UNUSED_RESULT
6233 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
6234 enum ofputil_protocol *usable_protocols)
6236 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true);
6240 ofpact_name(enum ofpact_type type)
6243 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
6250 /* Low-level action decoding and encoding functions. */
6252 /* Everything needed to identify a particular OpenFlow action. */
6253 struct ofpact_hdrs {
6254 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
6255 uint16_t type; /* Type if standard, otherwise subtype. */
6256 uint8_t ofp_version; /* From ofp_header. */
6259 /* Information about a particular OpenFlow action. */
6260 struct ofpact_raw_instance {
6261 /* The action's identity. */
6262 struct ofpact_hdrs hdrs;
6263 enum ofp_raw_action_type raw;
6265 /* Looking up the action. */
6266 struct hmap_node decode_node; /* Based on 'hdrs'. */
6267 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
6269 /* The action's encoded size.
6271 * If this action is fixed-length, 'min_length' == 'max_length'.
6272 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
6273 * OFP_ACTION_ALIGN) == 65528. */
6274 unsigned short int min_length;
6275 unsigned short int max_length;
6277 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
6278 * offset of that argument from the beginning of the action and 'arg_len'
6279 * its length, both in bytes.
6281 * For actions that take other forms, these are both zero. */
6282 unsigned short int arg_ofs;
6283 unsigned short int arg_len;
6285 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
6288 /* If this action is deprecated, a human-readable string with a brief
6290 const char *deprecation;
6293 /* Action header. */
6294 struct ofp_action_header {
6295 /* The meaning of other values of 'type' generally depends on the OpenFlow
6296 * version (see enum ofp_raw_action_type).
6298 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
6299 * designates an OpenFlow vendor ID and that the remainder of the action
6300 * structure has a vendor-defined meaning.
6302 #define OFPAT_VENDOR 0xffff
6305 /* Always a multiple of 8. */
6308 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
6309 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
6312 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
6314 /* Header for Nicira-defined actions and for ONF vendor extensions.
6316 * This cannot be used as an entirely generic vendor extension action header,
6317 * because OpenFlow does not specify the location or size of the action
6318 * subtype; it just happens that ONF extensions and Nicira extensions share
6320 struct ext_action_header {
6321 ovs_be16 type; /* OFPAT_VENDOR. */
6322 ovs_be16 len; /* At least 16. */
6323 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
6324 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
6327 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
6330 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
6331 const struct ofpact_hdrs *b)
6333 return (a->vendor == b->vendor
6334 && a->type == b->type
6335 && a->ofp_version == b->ofp_version);
6339 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
6341 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
6344 #include "ofp-actions.inc2"
6346 static struct hmap *
6347 ofpact_decode_hmap(void)
6349 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6350 static struct hmap hmap;
6352 if (ovsthread_once_start(&once)) {
6353 struct ofpact_raw_instance *inst;
6356 for (inst = all_raw_instances;
6357 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6359 hmap_insert(&hmap, &inst->decode_node,
6360 ofpact_hdrs_hash(&inst->hdrs));
6362 ovsthread_once_done(&once);
6367 static struct hmap *
6368 ofpact_encode_hmap(void)
6370 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6371 static struct hmap hmap;
6373 if (ovsthread_once_start(&once)) {
6374 struct ofpact_raw_instance *inst;
6377 for (inst = all_raw_instances;
6378 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6380 hmap_insert(&hmap, &inst->encode_node,
6381 hash_2words(inst->raw, inst->hdrs.ofp_version));
6383 ovsthread_once_done(&once);
6389 ofpact_decode_raw(enum ofp_version ofp_version,
6390 const struct ofp_action_header *oah, size_t length,
6391 const struct ofpact_raw_instance **instp)
6393 const struct ofpact_raw_instance *inst;
6394 struct ofpact_hdrs hdrs;
6397 if (length < sizeof *oah) {
6398 return OFPERR_OFPBAC_BAD_LEN;
6401 /* Get base action type. */
6402 if (oah->type == htons(OFPAT_VENDOR)) {
6404 hdrs.vendor = ntohl(oah->vendor);
6405 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
6406 /* Get extension subtype. */
6407 const struct ext_action_header *nah;
6409 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
6410 if (length < sizeof *nah) {
6411 return OFPERR_OFPBAC_BAD_LEN;
6413 hdrs.type = ntohs(nah->subtype);
6415 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
6417 return OFPERR_OFPBAC_BAD_VENDOR;
6421 hdrs.type = ntohs(oah->type);
6424 hdrs.ofp_version = ofp_version;
6425 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
6426 ofpact_decode_hmap()) {
6427 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
6434 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
6435 : OFPERR_OFPBAC_BAD_TYPE);
6439 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6440 enum ofp_raw_action_type *raw, uint64_t *arg)
6442 const struct ofp_action_header *oah = ofpbuf_data(buf);
6443 const struct ofpact_raw_instance *action;
6444 unsigned int length;
6448 error = ofpact_decode_raw(ofp_version, oah, ofpbuf_size(buf), &action);
6453 if (action->deprecation) {
6454 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
6455 action->name, ofputil_version_to_string(ofp_version),
6456 action->deprecation);
6459 length = ntohs(oah->len);
6460 if (length > ofpbuf_size(buf)) {
6461 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
6462 "length %"PRIu32, action->name, length, ofpbuf_size(buf));
6463 return OFPERR_OFPBAC_BAD_LEN;
6465 if (length < action->min_length || length > action->max_length) {
6466 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
6467 "[%hu,%hu]", action->name, length,
6468 action->min_length, action->max_length);
6469 return OFPERR_OFPBAC_BAD_LEN;
6472 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
6473 "of 8", action->name, length);
6474 return OFPERR_OFPBAC_BAD_LEN;
6479 if (action->arg_len) {
6483 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
6484 for (i = 0; i < action->arg_len; i++) {
6485 *arg = (*arg << 8) | p[i];
6489 ofpbuf_pull(buf, length);
6494 static const struct ofpact_raw_instance *
6495 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
6497 const struct ofpact_raw_instance *inst;
6499 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
6500 ofpact_encode_hmap()) {
6501 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
6509 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6510 enum ofp_raw_action_type raw, uint64_t arg)
6512 const struct ofpact_raw_instance *inst;
6513 struct ofp_action_header *oah;
6514 const struct ofpact_hdrs *hdrs;
6516 inst = ofpact_raw_lookup(ofp_version, raw);
6519 oah = ofpbuf_put_zeros(buf, inst->min_length);
6520 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
6521 oah->len = htons(inst->min_length);
6522 oah->vendor = htonl(hdrs->vendor);
6524 switch (hdrs->vendor) {
6529 case ONF_VENDOR_ID: {
6530 struct ext_action_header *nah = (struct ext_action_header *) oah;
6531 nah->subtype = htons(hdrs->type);
6539 if (inst->arg_len) {
6540 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
6543 for (i = 0; i < inst->arg_len; i++) {
6555 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
6557 struct ofp_action_header *oah;
6559 ofpbuf_put_zeros(openflow, PAD_SIZE(ofpbuf_size(openflow) - start_ofs, 8));
6561 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
6562 oah->len = htons(ofpbuf_size(openflow) - start_ofs);