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.] */
214 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
215 OFPAT_RAW15_COPY_FIELD,
216 /* NX1.0-1.4(6): struct nx_action_reg_move. */
219 /* ## ------------------------- ## */
220 /* ## Nicira extension actions. ## */
221 /* ## ------------------------- ## */
223 /* Actions similar to standard actions are listed with the standard actions. */
225 /* NX1.0+(1): uint16_t. */
227 /* NX1.0+(14): struct nx_action_resubmit. */
228 NXAST_RAW_RESUBMIT_TABLE,
230 /* NX1.0+(2): uint32_t. */
231 NXAST_RAW_SET_TUNNEL,
232 /* NX1.0+(9): uint64_t. */
233 NXAST_RAW_SET_TUNNEL64,
235 /* NX1.0+(5): void. */
238 /* NX1.0+(8): struct nx_action_note, ... */
241 /* NX1.0+(10): struct nx_action_multipath. */
244 /* NX1.0+(12): struct nx_action_bundle, ... */
246 /* NX1.0+(13): struct nx_action_bundle, ... */
247 NXAST_RAW_BUNDLE_LOAD,
249 /* NX1.0+(15): struct nx_action_output_reg. */
250 NXAST_RAW_OUTPUT_REG,
252 /* NX1.0+(16): struct nx_action_learn, ... */
255 /* NX1.0+(17): void. */
258 /* NX1.0+(19): struct nx_action_fin_timeout. */
259 NXAST_RAW_FIN_TIMEOUT,
261 /* NX1.0+(20): struct nx_action_controller. */
262 NXAST_RAW_CONTROLLER,
264 /* NX1.0+(22): struct nx_action_write_metadata. */
265 NXAST_RAW_WRITE_METADATA,
267 /* NX1.0+(27): struct nx_action_stack. */
268 NXAST_RAW_STACK_PUSH,
270 /* NX1.0+(28): struct nx_action_stack. */
273 /* NX1.0+(29): struct nx_action_sample. */
277 /* OpenFlow actions are always a multiple of 8 bytes in length. */
278 #define OFP_ACTION_ALIGN 8
280 /* Define a few functions for working with instructions. */
281 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
282 static inline const struct STRUCT * OVS_UNUSED \
283 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
285 ovs_assert(inst->type == htons(ENUM)); \
286 return ALIGNED_CAST(struct STRUCT *, inst); \
289 static inline void OVS_UNUSED \
290 instruction_init_##ENUM(struct STRUCT *s) \
292 memset(s, 0, sizeof *s); \
293 s->type = htons(ENUM); \
294 s->len = htons(sizeof *s); \
297 static inline struct STRUCT * OVS_UNUSED \
298 instruction_put_##ENUM(struct ofpbuf *buf) \
300 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
301 instruction_init_##ENUM(s); \
307 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
309 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
311 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
312 uint32_t allowed_ovsinsts);
314 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
315 enum mf_field_id, uint64_t value);
317 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
318 enum ofp_raw_action_type *, uint64_t *arg);
319 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
320 enum ofp_raw_action_type, uint64_t arg);
322 static char *WARN_UNUSED_RESULT ofpacts_parse(
323 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
324 bool allow_instructions);
326 #include "ofp-actions.inc1"
328 /* Output actions. */
330 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
331 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
332 * number of bytes to send. A 'max_len' of zero means no bytes of the
333 * packet should be sent. */
334 struct ofp10_action_output {
335 ovs_be16 type; /* OFPAT10_OUTPUT. */
336 ovs_be16 len; /* Length is 8. */
337 ovs_be16 port; /* Output port. */
338 ovs_be16 max_len; /* Max length to send to controller. */
340 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
342 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
343 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
344 * number of bytes to send. A 'max_len' of zero means no bytes of the
345 * packet should be sent.*/
346 struct ofp11_action_output {
347 ovs_be16 type; /* OFPAT11_OUTPUT. */
348 ovs_be16 len; /* Length is 16. */
349 ovs_be32 port; /* Output port. */
350 ovs_be16 max_len; /* Max length to send to controller. */
351 uint8_t pad[6]; /* Pad to 64 bits. */
353 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
356 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
359 struct ofpact_output *output;
361 output = ofpact_put_OUTPUT(out);
362 output->port = u16_to_ofp(ntohs(oao->port));
363 output->max_len = ntohs(oao->max_len);
365 return ofpact_check_output_port(output->port, OFPP_MAX);
369 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
372 struct ofpact_output *output;
375 output = ofpact_put_OUTPUT(out);
376 output->max_len = ntohs(oao->max_len);
378 error = ofputil_port_from_ofp11(oao->port, &output->port);
383 return ofpact_check_output_port(output->port, OFPP_MAX);
387 encode_OUTPUT(const struct ofpact_output *output,
388 enum ofp_version ofp_version, struct ofpbuf *out)
390 if (ofp_version == OFP10_VERSION) {
391 struct ofp10_action_output *oao;
393 oao = put_OFPAT10_OUTPUT(out);
394 oao->port = htons(ofp_to_u16(output->port));
395 oao->max_len = htons(output->max_len);
397 struct ofp11_action_output *oao;
399 oao = put_OFPAT11_OUTPUT(out);
400 oao->port = ofputil_port_to_ofp11(output->port);
401 oao->max_len = htons(output->max_len);
405 static char * WARN_UNUSED_RESULT
406 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
407 enum ofputil_protocol *usable_protocols OVS_UNUSED)
409 if (strchr(arg, '[')) {
410 struct ofpact_output_reg *output_reg;
412 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
413 output_reg->max_len = UINT16_MAX;
414 return mf_parse_subfield(&output_reg->src, arg);
416 struct ofpact_output *output;
418 output = ofpact_put_OUTPUT(ofpacts);
419 if (!ofputil_port_from_string(arg, &output->port)) {
420 return xasprintf("%s: output to unknown port", arg);
422 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
428 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
430 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
431 ds_put_format(s, "output:%"PRIu16, a->port);
433 ofputil_format_port(a->port, s);
434 if (a->port == OFPP_CONTROLLER) {
435 ds_put_format(s, ":%"PRIu16, a->max_len);
443 decode_OFPAT_RAW11_GROUP(uint32_t group_id, struct ofpbuf *out)
445 ofpact_put_GROUP(out)->group_id = group_id;
450 encode_GROUP(const struct ofpact_group *group,
451 enum ofp_version ofp_version, struct ofpbuf *out)
453 if (ofp_version == OFP10_VERSION) {
456 put_OFPAT11_GROUP(out, group->group_id);
460 static char * WARN_UNUSED_RESULT
461 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
462 enum ofputil_protocol *usable_protocols OVS_UNUSED)
464 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
468 format_GROUP(const struct ofpact_group *a, struct ds *s)
470 ds_put_format(s, "group:%"PRIu32, a->group_id);
473 /* Action structure for NXAST_CONTROLLER.
475 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
476 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
479 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
481 * - 'controller_id': The ID of the controller connection to which the
482 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
483 * sent only to controllers that have the specified controller connection
484 * ID. See "struct nx_controller_id" for more information. */
485 struct nx_action_controller {
486 ovs_be16 type; /* OFPAT_VENDOR. */
487 ovs_be16 len; /* Length is 16. */
488 ovs_be32 vendor; /* NX_VENDOR_ID. */
489 ovs_be16 subtype; /* NXAST_CONTROLLER. */
490 ovs_be16 max_len; /* Maximum length to send to controller. */
491 ovs_be16 controller_id; /* Controller ID to send packet-in. */
492 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
493 uint8_t zero; /* Must be zero. */
495 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
498 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
501 struct ofpact_controller *oc;
503 oc = ofpact_put_CONTROLLER(out);
504 oc->max_len = ntohs(nac->max_len);
505 oc->controller_id = ntohs(nac->controller_id);
506 oc->reason = nac->reason;
511 encode_CONTROLLER(const struct ofpact_controller *controller,
512 enum ofp_version ofp_version OVS_UNUSED,
515 struct nx_action_controller *nac;
517 nac = put_NXAST_CONTROLLER(out);
518 nac->max_len = htons(controller->max_len);
519 nac->controller_id = htons(controller->controller_id);
520 nac->reason = controller->reason;
523 static char * WARN_UNUSED_RESULT
524 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
525 enum ofputil_protocol *usable_protocols OVS_UNUSED)
527 enum ofp_packet_in_reason reason = OFPR_ACTION;
528 uint16_t controller_id = 0;
529 uint16_t max_len = UINT16_MAX;
533 } else if (strspn(arg, "0123456789") == strlen(arg)) {
534 char *error = str_to_u16(arg, "max_len", &max_len);
541 while (ofputil_parse_key_value(&arg, &name, &value)) {
542 if (!strcmp(name, "reason")) {
543 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
544 return xasprintf("unknown reason \"%s\"", value);
546 } else if (!strcmp(name, "max_len")) {
547 char *error = str_to_u16(value, "max_len", &max_len);
551 } else if (!strcmp(name, "id")) {
552 char *error = str_to_u16(value, "id", &controller_id);
557 return xasprintf("unknown key \"%s\" parsing controller "
563 if (reason == OFPR_ACTION && controller_id == 0) {
564 struct ofpact_output *output;
566 output = ofpact_put_OUTPUT(ofpacts);
567 output->port = OFPP_CONTROLLER;
568 output->max_len = max_len;
570 struct ofpact_controller *controller;
572 controller = ofpact_put_CONTROLLER(ofpacts);
573 controller->max_len = max_len;
574 controller->reason = reason;
575 controller->controller_id = controller_id;
582 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
584 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
585 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
587 enum ofp_packet_in_reason reason = a->reason;
589 ds_put_cstr(s, "controller(");
590 if (reason != OFPR_ACTION) {
591 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
593 ds_put_format(s, "reason=%s,",
594 ofputil_packet_in_reason_to_string(
595 reason, reasonbuf, sizeof reasonbuf));
597 if (a->max_len != UINT16_MAX) {
598 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
600 if (a->controller_id != 0) {
601 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
608 /* Enqueue action. */
609 struct ofp10_action_enqueue {
610 ovs_be16 type; /* OFPAT10_ENQUEUE. */
611 ovs_be16 len; /* Len is 16. */
612 ovs_be16 port; /* Port that queue belongs. Should
613 refer to a valid physical port
614 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
615 uint8_t pad[6]; /* Pad for 64-bit alignment. */
616 ovs_be32 queue_id; /* Where to enqueue the packets. */
618 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
621 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
624 struct ofpact_enqueue *enqueue;
626 enqueue = ofpact_put_ENQUEUE(out);
627 enqueue->port = u16_to_ofp(ntohs(oae->port));
628 enqueue->queue = ntohl(oae->queue_id);
629 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
630 && enqueue->port != OFPP_IN_PORT
631 && enqueue->port != OFPP_LOCAL) {
632 return OFPERR_OFPBAC_BAD_OUT_PORT;
638 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
639 enum ofp_version ofp_version, struct ofpbuf *out)
641 if (ofp_version == OFP10_VERSION) {
642 struct ofp10_action_enqueue *oae;
644 oae = put_OFPAT10_ENQUEUE(out);
645 oae->port = htons(ofp_to_u16(enqueue->port));
646 oae->queue_id = htonl(enqueue->queue);
652 static char * WARN_UNUSED_RESULT
653 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
654 enum ofputil_protocol *usable_protocols OVS_UNUSED)
657 char *port = strtok_r(arg, ":q,", &sp);
658 char *queue = strtok_r(NULL, "", &sp);
659 struct ofpact_enqueue *enqueue;
661 if (port == NULL || queue == NULL) {
662 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
663 "\"enqueue(PORT,QUEUE)\"");
666 enqueue = ofpact_put_ENQUEUE(ofpacts);
667 if (!ofputil_port_from_string(port, &enqueue->port)) {
668 return xasprintf("%s: enqueue to unknown port", port);
670 return str_to_u32(queue, &enqueue->queue);
674 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
676 ds_put_format(s, "enqueue:");
677 ofputil_format_port(a->port, s);
678 ds_put_format(s, ":%"PRIu32, a->queue);
681 /* Action structure for NXAST_OUTPUT_REG.
683 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
685 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
686 * the NXAST_REG_LOAD action.
688 * The acceptable nxm_header values for 'src' are the same as the acceptable
689 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
691 * The 'max_len' field indicates the number of bytes to send when the chosen
692 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
693 * field of OFPAT_OUTPUT.
695 * The 'zero' field is required to be zeroed for forward compatibility. */
696 struct nx_action_output_reg {
697 ovs_be16 type; /* OFPAT_VENDOR. */
698 ovs_be16 len; /* 24. */
699 ovs_be32 vendor; /* NX_VENDOR_ID. */
700 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
702 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
703 ovs_be32 src; /* Source. */
705 ovs_be16 max_len; /* Max length to send to controller. */
707 uint8_t zero[6]; /* Reserved, must be zero. */
709 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
712 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
715 struct ofpact_output_reg *output_reg;
717 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
718 return OFPERR_OFPBAC_BAD_ARGUMENT;
721 output_reg = ofpact_put_OUTPUT_REG(out);
722 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
723 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
724 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
725 output_reg->max_len = ntohs(naor->max_len);
727 return mf_check_src(&output_reg->src, NULL);
731 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
732 enum ofp_version ofp_version OVS_UNUSED,
735 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
737 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
738 output_reg->src.n_bits);
739 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
740 naor->max_len = htons(output_reg->max_len);
743 static char * WARN_UNUSED_RESULT
744 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
745 enum ofputil_protocol *usable_protocols OVS_UNUSED)
747 return parse_OUTPUT(arg, ofpacts, usable_protocols);
751 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
753 ds_put_cstr(s, "output:");
754 mf_format_subfield(&a->src, s);
757 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
759 * The bundle actions choose a slave from a supplied list of options.
760 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
761 * selection to a register.
763 * The list of possible slaves follows the nx_action_bundle structure. The size
764 * of each slave is governed by its type as indicated by the 'slave_type'
765 * parameter. The list of slaves should be padded at its end with zeros to make
766 * the total length of the action a multiple of 8.
768 * Switches infer from the 'slave_type' parameter the size of each slave. All
769 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
770 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
771 * 2 byte width. Switches should reject actions which indicate unknown or
772 * unsupported slave types.
774 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
775 * slave. If the switch does not support the specified 'algorithm' parameter,
776 * it should reject the action.
778 * Several algorithms take into account liveness when selecting slaves. The
779 * liveness of a slave is implementation defined (with one exception), but will
780 * generally take into account things like its carrier status and the results
781 * of any link monitoring protocols which happen to be running on it. In order
782 * to give controllers a place-holder value, the OFPP_NONE port is always
785 * Some slave selection strategies require the use of a hash function, in which
786 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
787 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
788 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
789 * 'basis' parameter is used as a universal hash parameter. Different values
790 * of 'basis' yield different hash results.
792 * The 'zero' parameter at the end of the action structure is reserved for
793 * future use. Switches are required to reject actions which have nonzero
794 * bytes in the 'zero' field.
796 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
797 * should reject actions which have nonzero bytes in either of these fields.
799 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
800 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
801 * similar to those for the NXAST_REG_LOAD action. */
802 struct nx_action_bundle {
803 ovs_be16 type; /* OFPAT_VENDOR. */
804 ovs_be16 len; /* Length including slaves. */
805 ovs_be32 vendor; /* NX_VENDOR_ID. */
806 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
808 /* Slave choice algorithm to apply to hash value. */
809 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
811 /* What fields to hash and how. */
812 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
813 ovs_be16 basis; /* Universal hash parameter. */
815 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
816 ovs_be16 n_slaves; /* Number of slaves. */
818 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
819 ovs_be32 dst; /* Destination. */
821 uint8_t zero[4]; /* Reserved. Must be zero. */
823 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
826 decode_bundle(bool load, const struct nx_action_bundle *nab,
827 struct ofpbuf *ofpacts)
829 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
830 struct ofpact_bundle *bundle;
832 size_t slaves_size, i;
835 bundle = ofpact_put_BUNDLE(ofpacts);
837 bundle->n_slaves = ntohs(nab->n_slaves);
838 bundle->basis = ntohs(nab->basis);
839 bundle->fields = ntohs(nab->fields);
840 bundle->algorithm = ntohs(nab->algorithm);
841 slave_type = ntohl(nab->slave_type);
842 slaves_size = ntohs(nab->len) - sizeof *nab;
844 error = OFPERR_OFPBAC_BAD_ARGUMENT;
845 if (!flow_hash_fields_valid(bundle->fields)) {
846 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
847 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
848 VLOG_WARN_RL(&rl, "too many slaves");
849 } else if (bundle->algorithm != NX_BD_ALG_HRW
850 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
851 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
852 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
853 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
858 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
859 VLOG_WARN_RL(&rl, "reserved field is nonzero");
860 error = OFPERR_OFPBAC_BAD_ARGUMENT;
864 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
865 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
866 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
868 if (bundle->dst.n_bits < 16) {
869 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
871 error = OFPERR_OFPBAC_BAD_ARGUMENT;
874 if (nab->ofs_nbits || nab->dst) {
875 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
876 error = OFPERR_OFPBAC_BAD_ARGUMENT;
880 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
881 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
882 "allocated for slaves. %"PRIuSIZE" bytes are required "
883 "for %"PRIu16" slaves.",
884 load ? "bundle_load" : "bundle", slaves_size,
885 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
886 error = OFPERR_OFPBAC_BAD_LEN;
889 for (i = 0; i < bundle->n_slaves; i++) {
890 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
891 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
894 bundle = ofpacts->frame;
895 ofpact_update_len(ofpacts, &bundle->ofpact);
898 error = bundle_check(bundle, OFPP_MAX, NULL);
904 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab, struct ofpbuf *out)
906 return decode_bundle(false, nab, out);
910 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
913 return decode_bundle(true, nab, out);
917 encode_BUNDLE(const struct ofpact_bundle *bundle,
918 enum ofp_version ofp_version OVS_UNUSED,
921 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
922 struct nx_action_bundle *nab;
926 nab = (bundle->dst.field
927 ? put_NXAST_BUNDLE_LOAD(out)
928 : put_NXAST_BUNDLE(out));
929 nab->len = htons(ntohs(nab->len) + slaves_len);
930 nab->algorithm = htons(bundle->algorithm);
931 nab->fields = htons(bundle->fields);
932 nab->basis = htons(bundle->basis);
933 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
934 nab->n_slaves = htons(bundle->n_slaves);
935 if (bundle->dst.field) {
936 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
938 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
941 slaves = ofpbuf_put_zeros(out, slaves_len);
942 for (i = 0; i < bundle->n_slaves; i++) {
943 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
947 static char * WARN_UNUSED_RESULT
948 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
949 enum ofputil_protocol *usable_protocols OVS_UNUSED)
951 return bundle_parse(arg, ofpacts);
954 static char * WARN_UNUSED_RESULT
955 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
957 return bundle_parse_load(arg, ofpacts);
961 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
966 /* Set VLAN actions. */
969 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
972 return OFPERR_OFPBAC_BAD_ARGUMENT;
974 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
975 vlan_vid->vlan_vid = vid;
976 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
982 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid, struct ofpbuf *out)
984 return decode_set_vlan_vid(vid, true, out);
988 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid, struct ofpbuf *out)
990 return decode_set_vlan_vid(vid, false, out);
994 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
995 enum ofp_version ofp_version, struct ofpbuf *out)
997 uint16_t vid = vlan_vid->vlan_vid;
999 /* Push a VLAN tag, if none is present and this form of the action calls
1000 * for such a feature. */
1001 if (ofp_version > OFP10_VERSION
1002 && vlan_vid->push_vlan_if_needed
1003 && !vlan_vid->flow_has_vlan) {
1004 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1007 if (ofp_version == OFP10_VERSION) {
1008 put_OFPAT10_SET_VLAN_VID(out, vid);
1009 } else if (ofp_version == OFP11_VERSION) {
1010 put_OFPAT11_SET_VLAN_VID(out, vid);
1012 ofpact_put_set_field(out, ofp_version,
1013 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1017 static char * WARN_UNUSED_RESULT
1018 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1020 struct ofpact_vlan_vid *vlan_vid;
1024 error = str_to_u16(arg, "VLAN VID", &vid);
1029 if (vid & ~VLAN_VID_MASK) {
1030 return xasprintf("%s: not a valid VLAN VID", arg);
1032 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1033 vlan_vid->vlan_vid = vid;
1034 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1038 static char * WARN_UNUSED_RESULT
1039 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1040 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1042 return parse_set_vlan_vid(arg, ofpacts, false);
1046 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1048 ds_put_format(s, "%s:%"PRIu16,
1049 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1053 /* Set PCP actions. */
1056 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1059 return OFPERR_OFPBAC_BAD_ARGUMENT;
1061 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1062 vlan_pcp->vlan_pcp = pcp;
1063 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1069 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp, struct ofpbuf *out)
1071 return decode_set_vlan_pcp(pcp, true, out);
1075 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp, struct ofpbuf *out)
1077 return decode_set_vlan_pcp(pcp, false, out);
1081 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1082 enum ofp_version ofp_version, struct ofpbuf *out)
1084 uint8_t pcp = vlan_pcp->vlan_pcp;
1086 /* Push a VLAN tag, if none is present and this form of the action calls
1087 * for such a feature. */
1088 if (ofp_version > OFP10_VERSION
1089 && vlan_pcp->push_vlan_if_needed
1090 && !vlan_pcp->flow_has_vlan) {
1091 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1094 if (ofp_version == OFP10_VERSION) {
1095 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1096 } else if (ofp_version == OFP11_VERSION) {
1097 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1099 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1103 static char * WARN_UNUSED_RESULT
1104 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1106 struct ofpact_vlan_pcp *vlan_pcp;
1110 error = str_to_u8(arg, "VLAN PCP", &pcp);
1116 return xasprintf("%s: not a valid VLAN PCP", arg);
1118 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1119 vlan_pcp->vlan_pcp = pcp;
1120 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1124 static char * WARN_UNUSED_RESULT
1125 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1126 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1128 return parse_set_vlan_pcp(arg, ofpacts, false);
1132 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1134 ds_put_format(s, "%s:%"PRIu8,
1135 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1139 /* Strip VLAN actions. */
1142 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1144 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1149 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1151 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1156 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1157 enum ofp_version ofp_version, struct ofpbuf *out)
1159 if (ofp_version == OFP10_VERSION) {
1160 put_OFPAT10_STRIP_VLAN(out);
1162 put_OFPAT11_POP_VLAN(out);
1166 static char * WARN_UNUSED_RESULT
1167 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1168 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1170 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1174 static char * WARN_UNUSED_RESULT
1175 parse_pop_vlan(struct ofpbuf *ofpacts)
1177 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1182 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1184 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1189 /* Push VLAN action. */
1192 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type, struct ofpbuf *out)
1194 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1195 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1196 return OFPERR_OFPBAC_BAD_ARGUMENT;
1198 ofpact_put_PUSH_VLAN(out);
1203 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1204 enum ofp_version ofp_version, struct ofpbuf *out)
1206 if (ofp_version == OFP10_VERSION) {
1207 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1208 * follow this action. */
1210 /* XXX ETH_TYPE_VLAN_8021AD case */
1211 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1215 static char * WARN_UNUSED_RESULT
1216 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1217 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1222 *usable_protocols &= OFPUTIL_P_OF11_UP;
1223 error = str_to_u16(arg, "ethertype", ðertype);
1228 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1229 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1230 return xasprintf("%s: not a valid VLAN ethertype", arg);
1233 ofpact_put_PUSH_VLAN(ofpacts);
1238 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1240 /* XXX 802.1AD case*/
1241 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1244 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1245 struct ofp_action_dl_addr {
1246 ovs_be16 type; /* Type. */
1247 ovs_be16 len; /* Length is 16. */
1248 uint8_t dl_addr[OFP_ETH_ALEN]; /* Ethernet address. */
1251 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1254 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1257 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr, ETH_ADDR_LEN);
1262 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1265 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr, ETH_ADDR_LEN);
1270 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1271 enum ofp_raw_action_type raw, enum mf_field_id field,
1274 const uint8_t *addr = mac->mac;
1276 if (ofp_version < OFP12_VERSION) {
1277 struct ofp_action_dl_addr *oada;
1279 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1280 memcpy(oada->dl_addr, addr, ETH_ADDR_LEN);
1282 ofpact_put_set_field(out, ofp_version, field,
1283 eth_addr_to_uint64(addr));
1288 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1291 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1297 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1298 enum ofp_version ofp_version,
1301 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1306 static char * WARN_UNUSED_RESULT
1307 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1308 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1310 return str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1313 static char * WARN_UNUSED_RESULT
1314 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1315 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1317 return str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
1321 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1323 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1327 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1329 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1332 /* Set IPv4 address actions. */
1335 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4, struct ofpbuf *out)
1337 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1342 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4, struct ofpbuf *out)
1344 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1349 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1350 enum ofp_version ofp_version,
1351 enum ofp_raw_action_type raw, enum mf_field_id field,
1354 ovs_be32 addr = ipv4->ipv4;
1355 if (ofp_version < OFP12_VERSION) {
1356 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1358 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1363 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1364 enum ofp_version ofp_version, struct ofpbuf *out)
1366 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1371 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1372 enum ofp_version ofp_version, struct ofpbuf *out)
1374 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1378 static char * WARN_UNUSED_RESULT
1379 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1380 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1382 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1385 static char * WARN_UNUSED_RESULT
1386 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1387 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1389 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1393 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1395 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1399 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1401 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1404 /* Set IPv4/v6 TOS actions. */
1407 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp, struct ofpbuf *out)
1409 if (dscp & ~IP_DSCP_MASK) {
1410 return OFPERR_OFPBAC_BAD_ARGUMENT;
1412 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1418 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1419 enum ofp_version ofp_version, struct ofpbuf *out)
1421 if (ofp_version < OFP12_VERSION) {
1422 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1424 ofpact_put_set_field(out, ofp_version,
1425 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1429 static char * WARN_UNUSED_RESULT
1430 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1431 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1436 error = str_to_u8(arg, "TOS", &tos);
1441 if (tos & ~IP_DSCP_MASK) {
1442 return xasprintf("%s: not a valid TOS", arg);
1444 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1449 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1451 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1454 /* Set IPv4/v6 ECN actions. */
1457 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn, struct ofpbuf *out)
1459 if (ecn & ~IP_ECN_MASK) {
1460 return OFPERR_OFPBAC_BAD_ARGUMENT;
1462 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1468 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1469 enum ofp_version ofp_version, struct ofpbuf *out)
1471 uint8_t ecn = ip_ecn->ecn;
1472 if (ofp_version == OFP10_VERSION) {
1474 } else if (ofp_version == OFP11_VERSION) {
1475 put_OFPAT11_SET_NW_ECN(out, ecn);
1477 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1481 static char * WARN_UNUSED_RESULT
1482 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1483 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1488 error = str_to_u8(arg, "ECN", &ecn);
1493 if (ecn & ~IP_ECN_MASK) {
1494 return xasprintf("%s: not a valid ECN", arg);
1496 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1501 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1503 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1506 /* Set IPv4/v6 TTL actions. */
1509 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl, struct ofpbuf *out)
1511 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1516 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1517 enum ofp_version ofp_version, struct ofpbuf *out)
1519 if (ofp_version >= OFP11_VERSION) {
1520 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1526 static char * WARN_UNUSED_RESULT
1527 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1528 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1533 error = str_to_u8(arg, "TTL", &ttl);
1538 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1543 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1545 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1548 /* Set TCP/UDP/SCTP port actions. */
1551 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port, struct ofpbuf *out)
1553 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1558 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port, struct ofpbuf *out)
1560 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1565 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1566 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1567 enum mf_field_id field, struct ofpbuf *out)
1569 uint16_t port = l4_port->port;
1571 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1572 ofpact_put_set_field(out, ofp_version, field, port);
1574 ofpact_put_raw(out, ofp_version, raw, port);
1579 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1580 enum ofp_version ofp_version, struct ofpbuf *out)
1582 uint8_t proto = l4_port->flow_ip_proto;
1583 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1584 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1585 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1588 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1592 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1593 enum ofp_version ofp_version,
1596 uint8_t proto = l4_port->flow_ip_proto;
1597 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1598 : proto == IPPROTO_UDP ? MFF_UDP_DST
1599 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1602 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1605 static char * WARN_UNUSED_RESULT
1606 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1607 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1609 return str_to_u16(arg, "source port",
1610 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1613 static char * WARN_UNUSED_RESULT
1614 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1615 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1617 return str_to_u16(arg, "destination port",
1618 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1622 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1624 ds_put_format(s, "mod_tp_src:%d", a->port);
1628 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1630 ds_put_format(s, "mod_tp_dst:%d", a->port);
1633 /* Action structure for OFPAT_COPY_FIELD. */
1634 struct ofp15_action_copy_field {
1635 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1636 ovs_be16 len; /* Length is padded to 64 bits. */
1637 ovs_be16 n_bits; /* Number of bits to copy. */
1638 ovs_be16 src_offset; /* Starting bit offset in source. */
1639 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1640 ovs_be16 oxm_id_len; /* Length of oxm_ids. */
1642 * - OXM header for source field.
1643 * - OXM header for destination field.
1644 * - Padding with 0-bytes to a multiple of 8 bytes.
1645 * The "pad" member is the beginning of the above. */
1648 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1650 /* Action structure for NXAST_REG_MOVE.
1652 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1653 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1654 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1655 * the next most significant bit, and so on.
1657 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1658 * sense to use nxm_hasmask=1 because the action does not do any kind of
1659 * matching; it uses the actual value of a field.)
1661 * The following nxm_header values are potentially acceptable as 'src':
1676 * - NXM_OF_ICMP_TYPE
1677 * - NXM_OF_ICMP_CODE
1684 * - NXM_NX_ICMPV6_TYPE
1685 * - NXM_NX_ICMPV6_CODE
1688 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1690 * - NXM_NX_TUN_IPV4_SRC
1691 * - NXM_NX_TUN_IPV4_DST
1693 * The following nxm_header values are potentially acceptable as 'dst':
1709 * Modifying any of the above fields changes the corresponding packet
1714 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1718 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1719 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1720 * header (if any), ignoring the other bits. Setting a value with CFI=1
1721 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1722 * to the field's new value (with the CFI bit masked out).
1724 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1725 * any of these values modifies the corresponding tunnel header field used
1726 * for the packet's next tunnel encapsulation, if allowed by the
1727 * configuration of the output tunnel port.
1729 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1730 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1731 * used only if the flow's nx_match includes an nxm_entry that specifies
1732 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1734 * The switch will reject actions for which src_ofs+n_bits is greater than the
1735 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1736 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1738 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1739 * behaves as if 'src' were copied out to a temporary buffer, then the
1740 * temporary buffer copied to 'dst'.
1742 struct nx_action_reg_move {
1743 ovs_be16 type; /* OFPAT_VENDOR. */
1744 ovs_be16 len; /* Length is 24. */
1745 ovs_be32 vendor; /* NX_VENDOR_ID. */
1746 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1747 ovs_be16 n_bits; /* Number of bits. */
1748 ovs_be16 src_ofs; /* Starting bit offset in source. */
1749 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1750 ovs_be32 src; /* Source register. */
1751 ovs_be32 dst; /* Destination register. */
1753 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 24);
1756 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
1757 struct ofpbuf *ofpacts)
1759 struct ofpact_reg_move *move;
1764 move = ofpact_put_REG_MOVE(ofpacts);
1765 move->src.ofs = ntohs(oacf->src_offset);
1766 move->src.n_bits = ntohs(oacf->n_bits);
1767 move->dst.ofs = ntohs(oacf->dst_offset);
1768 move->dst.n_bits = ntohs(oacf->n_bits);
1770 ofpbuf_use_const(&b, oacf, ntohs(oacf->len));
1771 ofpbuf_pull(&b, offsetof(struct ofp15_action_copy_field, pad));
1772 orig_size = ofpbuf_size(&b);
1773 error = nx_pull_header(&b, &move->src.field, NULL);
1777 error = nx_pull_header(&b, &move->dst.field, NULL);
1781 if (orig_size - ofpbuf_size(&b) != ntohs(oacf->oxm_id_len)) {
1782 return OFPERR_OFPBAC_BAD_LEN;
1785 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
1786 return OFPERR_NXBRC_MUST_BE_ZERO;
1789 return nxm_reg_move_check(move, NULL);
1793 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
1794 struct ofpbuf *ofpacts)
1796 struct ofpact_reg_move *move;
1798 move = ofpact_put_REG_MOVE(ofpacts);
1799 move->src.field = mf_from_nxm_header(ntohl(narm->src));
1800 move->src.ofs = ntohs(narm->src_ofs);
1801 move->src.n_bits = ntohs(narm->n_bits);
1802 move->dst.field = mf_from_nxm_header(ntohl(narm->dst));
1803 move->dst.ofs = ntohs(narm->dst_ofs);
1804 move->dst.n_bits = ntohs(narm->n_bits);
1806 return nxm_reg_move_check(move, NULL);
1810 encode_REG_MOVE(const struct ofpact_reg_move *move,
1811 enum ofp_version ofp_version, struct ofpbuf *out)
1813 if (ofp_version >= OFP15_VERSION) {
1814 struct ofp15_action_copy_field *copy;
1815 size_t start_ofs = ofpbuf_size(out);
1817 copy = put_OFPAT15_COPY_FIELD(out);
1818 copy->n_bits = htons(move->dst.n_bits);
1819 copy->src_offset = htons(move->src.ofs);
1820 copy->dst_offset = htons(move->dst.ofs);
1821 copy->oxm_id_len = htons(8);
1822 ofpbuf_set_size(out, ofpbuf_size(out) - sizeof copy->pad);
1823 nx_put_header(out, move->src.field->id, ofp_version, false);
1824 nx_put_header(out, move->dst.field->id, ofp_version, false);
1825 pad_ofpat(out, start_ofs);
1827 struct nx_action_reg_move *narm;
1829 narm = put_NXAST_REG_MOVE(out);
1830 narm->n_bits = htons(move->dst.n_bits);
1831 narm->src_ofs = htons(move->src.ofs);
1832 narm->dst_ofs = htons(move->dst.ofs);
1833 narm->src = htonl(mf_nxm_header(move->src.field->id));
1834 narm->dst = htonl(mf_nxm_header(move->dst.field->id));
1838 static char * WARN_UNUSED_RESULT
1839 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
1840 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1842 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
1843 const char *full_arg = arg;
1846 error = mf_parse_subfield__(&move->src, &arg);
1850 if (strncmp(arg, "->", 2)) {
1851 return xasprintf("%s: missing `->' following source", full_arg);
1854 error = mf_parse_subfield(&move->dst, arg);
1859 if (move->src.n_bits != move->dst.n_bits) {
1860 return xasprintf("%s: source field is %d bits wide but destination is "
1861 "%d bits wide", full_arg,
1862 move->src.n_bits, move->dst.n_bits);
1868 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
1870 nxm_format_reg_move(a, s);
1873 /* Action structure for OFPAT12_SET_FIELD. */
1874 struct ofp12_action_set_field {
1875 ovs_be16 type; /* OFPAT12_SET_FIELD. */
1876 ovs_be16 len; /* Length is padded to 64 bits. */
1879 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
1880 * - Enough 0-bytes to pad out to a multiple of 64 bits.
1882 * The "pad" member is the beginning of the above. */
1885 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
1887 /* Action structure for NXAST_REG_LOAD.
1889 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
1890 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
1891 * starts at 0 for the least-significant bit, 1 for the next most significant
1894 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
1895 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
1898 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
1899 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
1900 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
1901 * also stored as one less than its true value:
1904 * +------------------------------+------------------+
1905 * | ofs | n_bits - 1 |
1906 * +------------------------------+------------------+
1908 * The switch will reject actions for which ofs+n_bits is greater than the
1909 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
1910 * greater are set to 1, with error type OFPET_BAD_ACTION, code
1911 * OFPBAC_BAD_ARGUMENT.
1913 struct nx_action_reg_load {
1914 ovs_be16 type; /* OFPAT_VENDOR. */
1915 ovs_be16 len; /* Length is 24. */
1916 ovs_be32 vendor; /* NX_VENDOR_ID. */
1917 ovs_be16 subtype; /* NXAST_REG_LOAD. */
1918 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1919 ovs_be32 dst; /* Destination register. */
1920 ovs_be64 value; /* Immediate value. */
1922 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
1925 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
1926 bool may_mask, struct ofpbuf *ofpacts)
1928 struct ofpact_set_field *sf;
1932 sf = ofpact_put_SET_FIELD(ofpacts);
1934 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
1935 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
1936 error = nx_pull_entry(&b, &sf->field, &sf->value,
1937 may_mask ? &sf->mask : NULL);
1939 return (error == OFPERR_OFPBMC_BAD_MASK
1940 ? OFPERR_OFPBAC_BAD_SET_MASK
1944 memset(&sf->mask, 0xff, sf->field->n_bytes);
1947 if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
1948 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1951 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
1953 if (sf->field->id == MFF_IN_PORT_OXM) {
1954 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1957 /* oxm_length is now validated to be compatible with mf_value. */
1958 if (!sf->field->writable) {
1959 VLOG_WARN_RL(&rl, "destination field %s is not writable",
1961 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1964 /* The value must be valid for match. The OpenFlow 1.5 draft also says,
1965 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
1966 * a 1-bit in oxm_value and in oxm_mask." */
1967 if (!mf_is_value_valid(sf->field, &sf->value)
1968 || (sf->field->id == MFF_VLAN_VID
1969 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT)
1970 || !(sf->value.be16 & htons(OFPVID12_PRESENT)))))) {
1971 struct ds ds = DS_EMPTY_INITIALIZER;
1972 mf_format(sf->field, &sf->value, NULL, &ds);
1973 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
1974 sf->field->name, ds_cstr(&ds));
1977 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
1983 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
1984 struct ofpbuf *ofpacts)
1986 return decode_ofpat_set_field(oasf, false, ofpacts);
1990 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
1991 struct ofpbuf *ofpacts)
1993 return decode_ofpat_set_field(oasf, true, ofpacts);
1997 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2000 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2001 struct mf_subfield dst;
2004 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2006 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2007 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2008 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2009 error = mf_check_dst(&dst, NULL);
2014 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2016 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2017 return OFPERR_OFPBAC_BAD_ARGUMENT;
2020 sf->field = dst.field;
2021 bitwise_put(ntohll(narl->value),
2022 &sf->value, dst.field->n_bytes, dst.ofs,
2024 bitwise_put(UINT64_MAX,
2025 &sf->mask, dst.field->n_bytes, dst.ofs,
2032 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2033 enum mf_field_id field, uint64_t value_)
2035 struct ofp12_action_set_field *oasf OVS_UNUSED;
2036 int n_bytes = mf_from_id(field)->n_bytes;
2037 size_t start_ofs = ofpbuf_size(openflow);
2038 union mf_value value;
2040 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2042 oasf = put_OFPAT12_SET_FIELD(openflow);
2043 ofpbuf_set_size(openflow, ofpbuf_size(openflow) - sizeof oasf->pad);
2044 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2045 pad_ofpat(openflow, start_ofs);
2049 next_load_segment(const struct ofpact_set_field *sf,
2050 struct mf_subfield *dst, uint64_t *value)
2052 int w = sf->field->n_bytes;
2053 int start = dst->ofs + dst->n_bits;
2055 if (start < 8 * w) {
2056 dst->field = sf->field;
2057 dst->ofs = bitwise_scan(&sf->mask, w, 1, start, 8 * w);
2058 if (dst->ofs < 8 * w) {
2059 dst->n_bits = bitwise_scan(&sf->mask, w, 0, dst->ofs + 1,
2060 MIN(dst->ofs + 64, 8 * w)) - dst->ofs;
2061 *value = bitwise_get(&sf->value, w, dst->ofs, dst->n_bits);
2068 /* Convert 'sf' to a series of REG_LOADs. */
2070 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2072 struct mf_subfield dst;
2075 dst.ofs = dst.n_bits = 0;
2076 while (next_load_segment(sf, &dst, &value)) {
2077 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2078 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2079 narl->dst = htonl(mf_nxm_header(dst.field->id));
2080 narl->value = htonll(value);
2084 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2085 * actions, if we can, falling back to Nicira extensions if we must.
2087 * We check only meta-flow types that can appear within set field actions and
2088 * that have a mapping to compatible action types. These struct mf_field
2089 * definitions have a defined OXM or NXM header value and specify the field as
2092 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2093 enum ofp_version ofp_version,
2096 switch ((int) sf->field->id) {
2097 case MFF_VLAN_TCI: {
2098 ovs_be16 tci = sf->value.be16;
2099 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2100 uint16_t vid = vlan_tci_to_vid(tci);
2101 uint8_t pcp = vlan_tci_to_pcp(tci);
2103 if (ofp_version < OFP11_VERSION) {
2104 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2106 * If CFI=1, Add or modify VLAN VID & PCP.
2107 * If CFI=0, strip VLAN header, if any.
2110 put_OFPAT10_SET_VLAN_VID(out, vid);
2111 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2113 put_OFPAT10_STRIP_VLAN(out);
2116 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2118 * If CFI=1, Add or modify VLAN VID & PCP.
2119 * OpenFlow 1.1 set actions only apply if the packet
2120 * already has VLAN tags. To be sure that is the case
2121 * we have to push a VLAN header. As we do not support
2122 * multiple layers of VLANs, this is a no-op, if a VLAN
2123 * header already exists. This may backfire, however,
2124 * when we start supporting multiple layers of VLANs.
2125 * If CFI=0, strip VLAN header, if any.
2128 /* Push a VLAN tag, if one was not seen at action validation
2130 if (!sf->flow_has_vlan) {
2131 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2133 put_OFPAT11_SET_VLAN_VID(out, vid);
2134 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2136 /* If the flow did not match on vlan, we have no way of
2137 * knowing if the vlan tag exists, so we must POP just to be
2139 put_OFPAT11_POP_VLAN(out);
2145 case MFF_VLAN_VID: {
2146 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2147 if (ofp_version == OFP10_VERSION) {
2148 put_OFPAT10_SET_VLAN_VID(out, vid);
2150 put_OFPAT11_SET_VLAN_VID(out, vid);
2156 if (ofp_version == OFP10_VERSION) {
2157 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2159 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2164 memcpy(put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr,
2165 sf->value.mac, ETH_ADDR_LEN);
2169 memcpy(put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr,
2170 sf->value.mac, ETH_ADDR_LEN);
2174 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2178 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2182 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2185 case MFF_IP_DSCP_SHIFTED:
2186 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2191 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2196 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2200 set_field_to_nxast(sf, out);
2206 set_field_to_set_field(const struct ofpact_set_field *sf,
2207 enum ofp_version ofp_version, struct ofpbuf *out)
2209 struct ofp12_action_set_field *oasf OVS_UNUSED;
2210 size_t start_ofs = ofpbuf_size(out);
2212 oasf = put_OFPAT12_SET_FIELD(out);
2213 ofpbuf_set_size(out, ofpbuf_size(out) - sizeof oasf->pad);
2214 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2215 pad_ofpat(out, start_ofs);
2219 encode_SET_FIELD(const struct ofpact_set_field *sf,
2220 enum ofp_version ofp_version, struct ofpbuf *out)
2222 if (ofp_version >= OFP15_VERSION) {
2223 /* OF1.5+ only has Set-Field (we drop NXAST_REG_LOAD entirely). */
2224 set_field_to_set_field(sf, ofp_version, out);
2225 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD) {
2226 /* It came in as NXAST_REG_LOAD, send it out the same way. */
2227 set_field_to_nxast(sf, out);
2228 } else if (ofp_version < OFP12_VERSION) {
2229 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2230 set_field_to_legacy_openflow(sf, ofp_version, out);
2231 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2232 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2233 * entire field, so encode it as OFPAT_SET_FIELD. */
2234 set_field_to_set_field(sf, ofp_version, out);
2236 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2237 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2238 * so encode it as reg_load. */
2239 set_field_to_nxast(sf, out);
2243 /* Parses a "set_field" action with argument 'arg', appending the parsed
2244 * action to 'ofpacts'.
2246 * Returns NULL if successful, otherwise a malloc()'d string describing the
2247 * error. The caller is responsible for freeing the returned string. */
2248 static char * WARN_UNUSED_RESULT
2249 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2250 enum ofputil_protocol *usable_protocols)
2252 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2256 const struct mf_field *mf;
2260 delim = strstr(arg, "->");
2262 return xasprintf("%s: missing `->'", arg);
2264 if (strlen(delim) <= strlen("->")) {
2265 return xasprintf("%s: missing field name following `->'", arg);
2268 key = delim + strlen("->");
2269 mf = mf_from_name(key);
2271 return xasprintf("%s is not a valid OXM field name", key);
2273 if (!mf->writable) {
2274 return xasprintf("%s is read-only", key);
2278 error = mf_parse(mf, value, &sf->value, &sf->mask);
2283 if (!mf_is_value_valid(mf, &sf->value)) {
2284 return xasprintf("%s is not a valid value for field %s", value, key);
2287 *usable_protocols &= mf->usable_protocols_exact;
2291 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2292 * action to 'ofpacts'.
2294 * Returns NULL if successful, otherwise a malloc()'d string describing the
2295 * error. The caller is responsible for freeing the returned string. */
2296 static char * WARN_UNUSED_RESULT
2297 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2298 enum ofputil_protocol *usable_protocols)
2300 char *copy = xstrdup(arg);
2301 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2306 static char * WARN_UNUSED_RESULT
2307 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2309 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2310 const char *full_arg = arg;
2311 uint64_t value = strtoull(arg, (char **) &arg, 0);
2312 struct mf_subfield dst;
2315 if (strncmp(arg, "->", 2)) {
2316 return xasprintf("%s: missing `->' following value", full_arg);
2319 error = mf_parse_subfield(&dst, arg);
2324 if (dst.n_bits < 64 && (value >> dst.n_bits) != 0) {
2325 return xasprintf("%s: value %"PRIu64" does not fit into %d bits",
2326 full_arg, value, dst.n_bits);
2329 sf->field = dst.field;
2330 memset(&sf->value, 0, sizeof sf->value);
2331 bitwise_put(value, &sf->value, dst.field->n_bytes, dst.ofs, dst.n_bits);
2332 bitwise_put(UINT64_MAX, &sf->mask,
2333 dst.field->n_bytes, dst.ofs, dst.n_bits);
2338 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2340 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2341 struct mf_subfield dst;
2344 dst.ofs = dst.n_bits = 0;
2345 while (next_load_segment(a, &dst, &value)) {
2346 ds_put_format(s, "load:%#"PRIx64"->", value);
2347 mf_format_subfield(&dst, s);
2348 ds_put_char(s, ',');
2352 ds_put_cstr(s, "set_field:");
2353 mf_format(a->field, &a->value, &a->mask, s);
2354 ds_put_format(s, "->%s", a->field->name);
2358 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2359 * is marked such that, if possible, it will be translated to OpenFlow as
2360 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2361 * that was the way that the action was expressed when it came into OVS or for
2362 * backward compatibility. */
2363 struct ofpact_set_field *
2364 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2366 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2367 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2371 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2373 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2376 struct nx_action_stack {
2377 ovs_be16 type; /* OFPAT_VENDOR. */
2378 ovs_be16 len; /* Length is 16. */
2379 ovs_be32 vendor; /* NX_VENDOR_ID. */
2380 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2381 ovs_be16 offset; /* Bit offset into the field. */
2382 ovs_be32 field; /* The field used for push or pop. */
2383 ovs_be16 n_bits; /* (n_bits + 1) bits of the field. */
2384 uint8_t zero[6]; /* Reserved, must be zero. */
2386 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2389 decode_stack_action(const struct nx_action_stack *nasp,
2390 struct ofpact_stack *stack_action)
2392 stack_action->subfield.field = mf_from_nxm_header(ntohl(nasp->field));
2393 stack_action->subfield.ofs = ntohs(nasp->offset);
2394 stack_action->subfield.n_bits = ntohs(nasp->n_bits);
2398 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2399 struct ofpbuf *ofpacts)
2401 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2402 decode_stack_action(nasp, push);
2403 return nxm_stack_push_check(push, NULL);
2407 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2408 struct ofpbuf *ofpacts)
2410 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2411 decode_stack_action(nasp, pop);
2412 return nxm_stack_pop_check(pop, NULL);
2416 encode_STACK_op(const struct ofpact_stack *stack_action,
2417 struct nx_action_stack *nasp)
2419 nasp->offset = htons(stack_action->subfield.ofs);
2420 nasp->n_bits = htons(stack_action->subfield.n_bits);
2421 nasp->field = htonl(mf_nxm_header(stack_action->subfield.field->id));
2425 encode_STACK_PUSH(const struct ofpact_stack *stack,
2426 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2428 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2432 encode_STACK_POP(const struct ofpact_stack *stack,
2433 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2435 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2438 static char * WARN_UNUSED_RESULT
2439 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2440 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2442 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2445 static char * WARN_UNUSED_RESULT
2446 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2447 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2449 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2453 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2455 nxm_format_stack_push(a, s);
2459 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2461 nxm_format_stack_pop(a, s);
2464 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2466 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2467 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2468 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2469 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2471 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2472 * sent only to controllers with id 0.)
2474 struct nx_action_cnt_ids {
2475 ovs_be16 type; /* OFPAT_VENDOR. */
2476 ovs_be16 len; /* Length including slaves. */
2477 ovs_be32 vendor; /* NX_VENDOR_ID. */
2478 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2480 ovs_be16 n_controllers; /* Number of controllers. */
2481 uint8_t zeros[4]; /* Must be zero. */
2483 /* Followed by 1 or more controller ids.
2485 * uint16_t cnt_ids[]; // Controller ids.
2486 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2489 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2492 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2495 struct ofpact_cnt_ids *ids;
2496 enum ofperr error = 0;
2498 ids = ofpact_put_DEC_TTL(out);
2499 ids->n_controllers = 1;
2500 ofpbuf_put(out, &id, sizeof id);
2502 ofpact_update_len(out, &ids->ofpact);
2507 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2510 struct ofpact_cnt_ids *ids;
2514 ids = ofpact_put_DEC_TTL(out);
2515 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2516 ids->n_controllers = ntohs(nac_ids->n_controllers);
2517 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2519 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2520 return OFPERR_NXBRC_MUST_BE_ZERO;
2523 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2524 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2525 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2526 "are required for %"PRIu16" controllers.",
2527 ids_size, ids->n_controllers * sizeof(ovs_be16),
2528 ids->n_controllers);
2529 return OFPERR_OFPBAC_BAD_LEN;
2532 for (i = 0; i < ids->n_controllers; i++) {
2533 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2534 ofpbuf_put(out, &id, sizeof id);
2538 ofpact_update_len(out, &ids->ofpact);
2544 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2545 enum ofp_version ofp_version, struct ofpbuf *out)
2547 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2548 || dec_ttl->n_controllers != 1
2549 || dec_ttl->cnt_ids[0] != 0) {
2550 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2551 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2555 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2556 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2558 ids = ofpbuf_put_zeros(out, ids_len);
2559 for (i = 0; i < dec_ttl->n_controllers; i++) {
2560 ids[i] = htons(dec_ttl->cnt_ids[i]);
2563 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2568 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
2570 struct ofpact_cnt_ids *ids;
2573 ofpact_put_DEC_TTL(ofpacts);
2574 ofpbuf_put(ofpacts, &id, sizeof id);
2575 ids = ofpacts->frame;
2576 ids->n_controllers++;
2577 ofpact_update_len(ofpacts, &ids->ofpact);
2580 static char * WARN_UNUSED_RESULT
2581 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
2582 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2585 parse_noargs_dec_ttl(ofpacts);
2587 struct ofpact_cnt_ids *ids;
2590 ids = ofpact_put_DEC_TTL(ofpacts);
2591 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2592 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
2593 cntr = strtok_r(NULL, ", ", &arg)) {
2594 uint16_t id = atoi(cntr);
2596 ofpbuf_put(ofpacts, &id, sizeof id);
2597 ids = ofpacts->frame;
2598 ids->n_controllers++;
2600 if (!ids->n_controllers) {
2601 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
2604 ofpact_update_len(ofpacts, &ids->ofpact);
2610 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
2614 ds_put_cstr(s, "dec_ttl");
2615 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
2616 ds_put_cstr(s, "(");
2617 for (i = 0; i < a->n_controllers; i++) {
2619 ds_put_cstr(s, ",");
2621 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
2623 ds_put_cstr(s, ")");
2627 /* Set MPLS label actions. */
2630 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label, struct ofpbuf *out)
2632 ofpact_put_SET_MPLS_LABEL(out)->label = label;
2637 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
2638 enum ofp_version ofp_version,
2641 if (ofp_version < OFP12_VERSION) {
2642 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
2644 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
2645 ntohl(label->label));
2649 static char * WARN_UNUSED_RESULT
2650 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
2651 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2653 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
2655 return xstrdup("set_mpls_label: expected label.");
2658 mpls_label->label = htonl(atoi(arg));
2663 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
2665 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
2668 /* Set MPLS TC actions. */
2671 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc, struct ofpbuf *out)
2673 ofpact_put_SET_MPLS_TC(out)->tc = tc;
2678 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
2679 enum ofp_version ofp_version, struct ofpbuf *out)
2681 if (ofp_version < OFP12_VERSION) {
2682 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
2684 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
2688 static char * WARN_UNUSED_RESULT
2689 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
2690 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2692 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
2695 return xstrdup("set_mpls_tc: expected tc.");
2698 mpls_tc->tc = atoi(arg);
2703 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
2705 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
2708 /* Set MPLS TTL actions. */
2711 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl, struct ofpbuf *out)
2713 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
2718 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
2719 enum ofp_version ofp_version, struct ofpbuf *out)
2721 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
2724 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
2725 * action to 'ofpacts'.
2727 * Returns NULL if successful, otherwise a malloc()'d string describing the
2728 * error. The caller is responsible for freeing the returned string. */
2729 static char * WARN_UNUSED_RESULT
2730 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
2731 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2733 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
2736 return xstrdup("set_mpls_ttl: expected ttl.");
2739 mpls_ttl->ttl = atoi(arg);
2744 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
2746 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
2749 /* Decrement MPLS TTL actions. */
2752 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
2754 ofpact_put_DEC_MPLS_TTL(out);
2759 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
2760 enum ofp_version ofp_version, struct ofpbuf *out)
2762 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
2765 static char * WARN_UNUSED_RESULT
2766 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
2767 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2769 ofpact_put_DEC_MPLS_TTL(ofpacts);
2774 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
2776 ds_put_cstr(s, "dec_mpls_ttl");
2779 /* Push MPLS label action. */
2782 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
2784 struct ofpact_push_mpls *oam;
2786 if (!eth_type_mpls(ethertype)) {
2787 return OFPERR_OFPBAC_BAD_ARGUMENT;
2789 oam = ofpact_put_PUSH_MPLS(out);
2790 oam->ethertype = ethertype;
2796 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
2797 enum ofp_version ofp_version, struct ofpbuf *out)
2799 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
2802 static char * WARN_UNUSED_RESULT
2803 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
2804 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2809 error = str_to_u16(arg, "push_mpls", ðertype);
2811 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
2817 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
2819 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
2822 /* Pop MPLS label action. */
2825 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype, struct ofpbuf *out)
2827 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
2832 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
2833 enum ofp_version ofp_version, struct ofpbuf *out)
2835 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
2838 static char * WARN_UNUSED_RESULT
2839 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
2840 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2845 error = str_to_u16(arg, "pop_mpls", ðertype);
2847 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
2853 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
2855 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
2858 /* Set tunnel ID actions. */
2861 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id, struct ofpbuf *out)
2863 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
2864 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
2865 tunnel->tun_id = tun_id;
2870 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id, struct ofpbuf *out)
2872 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
2873 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
2874 tunnel->tun_id = tun_id;
2879 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
2880 enum ofp_version ofp_version, struct ofpbuf *out)
2882 uint64_t tun_id = tunnel->tun_id;
2884 if (ofp_version < OFP12_VERSION) {
2885 if (tun_id <= UINT32_MAX
2886 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
2887 put_NXAST_SET_TUNNEL(out, tun_id);
2889 put_NXAST_SET_TUNNEL64(out, tun_id);
2892 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
2896 static char * WARN_UNUSED_RESULT
2897 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
2898 enum ofp_raw_action_type raw)
2900 struct ofpact_tunnel *tunnel;
2902 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
2903 tunnel->ofpact.raw = raw;
2904 return str_to_u64(arg, &tunnel->tun_id);
2907 static char * WARN_UNUSED_RESULT
2908 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
2909 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2911 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
2915 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
2917 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2918 (a->tun_id > UINT32_MAX
2919 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
2923 /* Set queue action. */
2926 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id, struct ofpbuf *out)
2928 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
2933 encode_SET_QUEUE(const struct ofpact_queue *queue,
2934 enum ofp_version ofp_version, struct ofpbuf *out)
2936 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
2939 static char * WARN_UNUSED_RESULT
2940 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
2941 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2943 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
2947 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
2949 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
2952 /* Pop queue action. */
2955 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
2957 ofpact_put_POP_QUEUE(out);
2962 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
2963 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2965 put_NXAST_POP_QUEUE(out);
2968 static char * WARN_UNUSED_RESULT
2969 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
2970 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2972 ofpact_put_POP_QUEUE(ofpacts);
2977 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
2979 ds_put_cstr(s, "pop_queue");
2982 /* Action structure for NXAST_FIN_TIMEOUT.
2984 * This action changes the idle timeout or hard timeout, or both, of this
2985 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
2986 * When such a packet is observed, the action reduces the rule's idle timeout
2987 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
2988 * action has no effect on an existing timeout that is already shorter than the
2989 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
2990 * of zero has no effect on the respective timeout.
2992 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
2993 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
2994 * receipt of the FIN or RST.
2996 * This is useful for quickly discarding learned TCP flows that otherwise will
2997 * take a long time to expire.
2999 * This action is intended for use with an OpenFlow rule that matches only a
3000 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3001 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3002 * RST in any of those flows will cause the entire OpenFlow rule to expire
3003 * early, which is not normally desirable.
3005 struct nx_action_fin_timeout {
3006 ovs_be16 type; /* OFPAT_VENDOR. */
3007 ovs_be16 len; /* 16. */
3008 ovs_be32 vendor; /* NX_VENDOR_ID. */
3009 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3010 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3011 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3012 ovs_be16 pad; /* Must be zero. */
3014 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3017 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3020 struct ofpact_fin_timeout *oft;
3022 oft = ofpact_put_FIN_TIMEOUT(out);
3023 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3024 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3029 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3030 enum ofp_version ofp_version OVS_UNUSED,
3033 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3034 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3035 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3038 static char * WARN_UNUSED_RESULT
3039 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3040 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3042 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3045 while (ofputil_parse_key_value(&arg, &key, &value)) {
3048 if (!strcmp(key, "idle_timeout")) {
3049 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3050 } else if (!strcmp(key, "hard_timeout")) {
3051 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3053 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3065 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3067 ds_put_cstr(s, "fin_timeout(");
3068 if (a->fin_idle_timeout) {
3069 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3071 if (a->fin_hard_timeout) {
3072 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3075 ds_put_char(s, ')');
3078 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3080 * These actions search one of the switch's flow tables:
3082 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3083 * it specifies the table to search.
3085 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3086 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3087 * table, that is, the OpenFlow flow table that contains the flow from
3088 * which this action was obtained. If this action did not come from a
3089 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3090 * is the current table.
3092 * The flow table lookup uses a flow that may be slightly modified from the
3095 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3096 * is used as the flow's in_port.
3098 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3099 * then its value is used as the flow's in_port. Otherwise, the original
3102 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3103 * resubmit action, then the flow is updated with the new values.
3105 * Following the lookup, the original in_port is restored.
3107 * If the modified flow matched in the flow table, then the corresponding
3108 * actions are executed. Afterward, actions following the resubmit in the
3109 * original set of actions, if any, are executed; any changes made to the
3110 * packet (e.g. changes to VLAN) by secondary actions persist when those
3111 * actions are executed, although the original in_port is restored.
3113 * Resubmit actions may be used any number of times within a set of actions.
3115 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3116 * implementation-defined depth, further resubmit actions are simply ignored.
3118 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3119 * 'pad' to be all-bits-zero.
3121 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3122 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3124 struct nx_action_resubmit {
3125 ovs_be16 type; /* OFPAT_VENDOR. */
3126 ovs_be16 len; /* Length is 16. */
3127 ovs_be32 vendor; /* NX_VENDOR_ID. */
3128 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3129 ovs_be16 in_port; /* New in_port for checking flow table. */
3130 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3133 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3136 decode_NXAST_RAW_RESUBMIT(uint16_t port, struct ofpbuf *out)
3138 struct ofpact_resubmit *resubmit;
3140 resubmit = ofpact_put_RESUBMIT(out);
3141 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3142 resubmit->in_port = u16_to_ofp(port);
3143 resubmit->table_id = 0xff;
3148 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3151 struct ofpact_resubmit *resubmit;
3153 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3154 return OFPERR_OFPBAC_BAD_ARGUMENT;
3157 resubmit = ofpact_put_RESUBMIT(out);
3158 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3159 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3160 resubmit->table_id = nar->table;
3165 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3166 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3168 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3170 if (resubmit->table_id == 0xff
3171 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3172 put_NXAST_RESUBMIT(out, in_port);
3174 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3175 nar->table = resubmit->table_id;
3176 nar->in_port = htons(in_port);
3180 static char * WARN_UNUSED_RESULT
3181 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3182 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3184 struct ofpact_resubmit *resubmit;
3185 char *in_port_s, *table_s;
3187 resubmit = ofpact_put_RESUBMIT(ofpacts);
3189 in_port_s = strsep(&arg, ",");
3190 if (in_port_s && in_port_s[0]) {
3191 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3192 return xasprintf("%s: resubmit to unknown port", in_port_s);
3195 resubmit->in_port = OFPP_IN_PORT;
3198 table_s = strsep(&arg, ",");
3199 if (table_s && table_s[0]) {
3200 uint32_t table_id = 0;
3203 error = str_to_u32(table_s, &table_id);
3207 resubmit->table_id = table_id;
3209 resubmit->table_id = 255;
3212 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3213 return xstrdup("at least one \"in_port\" or \"table\" must be "
3214 "specified on resubmit");
3220 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3222 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3223 ds_put_cstr(s, "resubmit:");
3224 ofputil_format_port(a->in_port, s);
3226 ds_put_format(s, "resubmit(");
3227 if (a->in_port != OFPP_IN_PORT) {
3228 ofputil_format_port(a->in_port, s);
3230 ds_put_char(s, ',');
3231 if (a->table_id != 255) {
3232 ds_put_format(s, "%"PRIu8, a->table_id);
3234 ds_put_char(s, ')');
3238 /* Action structure for NXAST_LEARN.
3240 * This action adds or modifies a flow in an OpenFlow table, similar to
3241 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3242 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3243 * flow's match criteria and actions are built by applying each of the series
3244 * of flow_mod_spec elements included as part of the action.
3246 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3247 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3248 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3249 * bits from a source to a destination. In this case, the header contains
3252 * 15 14 13 12 11 10 0
3253 * +------+---+------+---------------------------------+
3254 * | 0 |src| dst | n_bits |
3255 * +------+---+------+---------------------------------+
3257 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3258 * following table summarizes the meaning of each possible combination.
3259 * Details follow the table:
3262 * --- --- ----------------------------------------------------------
3263 * 0 0 Add match criteria based on value in a field.
3264 * 1 0 Add match criteria based on an immediate value.
3265 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3266 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3267 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3268 * All other combinations are undefined and not allowed.
3270 * The flow_mod_spec header is followed by a source specification and a
3271 * destination specification. The format and meaning of the source
3272 * specification depends on 'src':
3274 * - If 'src' is 0, the source bits are taken from a field in the flow to
3275 * which this action is attached. (This should be a wildcarded field. If
3276 * its value is fully specified then the source bits being copied have
3279 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3280 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3281 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3282 * 'field' and 'ofs' are subject to the same restrictions as the source
3283 * field in NXAST_REG_MOVE.
3285 * - If 'src' is 1, the source bits are a constant value. The source
3286 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3287 * number in network order, the source bits are the 'n_bits'
3288 * least-significant bits. The switch will report an error if other bits
3289 * in the constant are nonzero.
3291 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3292 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3293 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3294 * meaning of the flow_mod_spec depends on 'dst':
3296 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3297 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3298 * packet equal the source bits. 'field' may be any nxm_header with
3299 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3301 * Order is significant. Earlier flow_mod_specs must satisfy any
3302 * prerequisites for matching fields specified later, by copying constant
3303 * values into prerequisite fields.
3305 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3308 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3309 * the new flow. The new flow copies the source bits into
3310 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3313 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3314 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3316 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3317 * empty. It has the following meaning:
3319 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3320 * The new flow outputs to the OpenFlow port specified by the source field.
3321 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3322 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3325 * Resource Management
3326 * -------------------
3328 * A switch has a finite amount of flow table space available for learning.
3329 * When this space is exhausted, no new learning table entries will be learned
3330 * until some existing flow table entries expire. The controller should be
3331 * prepared to handle this by flooding (which can be implemented as a
3332 * low-priority flow).
3334 * If a learned flow matches a single TCP stream with a relatively long
3335 * timeout, one may make the best of resource constraints by setting
3336 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3337 * both, to shorter timeouts. When either of these is specified as a nonzero
3338 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3344 * The following examples give a prose description of the flow_mod_specs along
3345 * with informal notation for how those would be represented and a hex dump of
3346 * the bytes that would be required.
3348 * These examples could work with various nx_action_learn parameters. Typical
3349 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3350 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3352 * 1. Learn input port based on the source MAC, with lookup into
3353 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3355 * Match on in_port=99:
3356 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3357 * ovs_be16(99), 00 63
3358 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3360 * Match Ethernet destination on Ethernet source from packet:
3361 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3362 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3363 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3365 * Set NXM_NX_REG1[16:31] to the packet's input port:
3366 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3367 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3368 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3370 * Given a packet that arrived on port A with Ethernet source address B,
3371 * this would set up the flow "in_port=99, dl_dst=B,
3372 * actions=load:A->NXM_NX_REG1[16..31]".
3374 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3375 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3376 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3378 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3379 * into NXM_NX_REG1[16:31]:
3381 * Match on same VLAN ID as packet:
3382 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3383 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3384 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3386 * Match Ethernet destination on Ethernet source from packet:
3387 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3388 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3389 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3391 * Output to the packet's input port:
3392 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3393 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3395 * Given a packet that arrived on port A with Ethernet source address B in
3396 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3397 * actions=output:A".
3399 * In syntax accepted by ovs-ofctl, this action is:
3400 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3401 * output:NXM_OF_IN_PORT[])
3403 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3404 * 10-second MAC expiration time to make it easier to see what's going on
3406 * ovs-vsctl del-controller br0
3407 * ovs-ofctl del-flows br0
3408 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3409 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3410 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3411 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3412 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3414 * You can then dump the MAC learning table with:
3416 * ovs-ofctl dump-flows br0 table=1
3421 * For best performance, segregate learned flows into a table that is not used
3422 * for any other flows except possibly for a lowest-priority "catch-all" flow
3423 * (a flow with no match criteria). If different learning actions specify
3424 * different match criteria, use different tables for the learned flows.
3426 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3427 * These timeouts apply to the flow that is added, which means that a flow with
3428 * an idle timeout will expire when no traffic has been sent *to* the learned
3429 * address. This is not usually the intent in MAC learning; instead, we want
3430 * the MAC learn entry to expire when no traffic has been sent *from* the
3431 * learned address. Use a hard timeout for that.
3433 struct nx_action_learn {
3434 ovs_be16 type; /* OFPAT_VENDOR. */
3435 ovs_be16 len; /* At least 24. */
3436 ovs_be32 vendor; /* NX_VENDOR_ID. */
3437 ovs_be16 subtype; /* NXAST_LEARN. */
3438 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3439 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3440 ovs_be16 priority; /* Priority level of flow entry. */
3441 ovs_be64 cookie; /* Cookie for new flow. */
3442 ovs_be16 flags; /* NX_LEARN_F_*. */
3443 uint8_t table_id; /* Table to insert flow entry. */
3444 uint8_t pad; /* Must be zero. */
3445 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3446 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3447 /* Followed by a sequence of flow_mod_spec elements, as described above,
3448 * until the end of the action is reached. */
3450 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3453 get_be16(const void **pp)
3455 const ovs_be16 *p = *pp;
3456 ovs_be16 value = *p;
3462 get_be32(const void **pp)
3464 const ovs_be32 *p = *pp;
3465 ovs_be32 value = get_unaligned_be32(p);
3471 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3473 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3474 sf->ofs = ntohs(get_be16(p));
3475 sf->n_bits = n_bits;
3479 learn_min_len(uint16_t header)
3481 int n_bits = header & NX_LEARN_N_BITS_MASK;
3482 int src_type = header & NX_LEARN_SRC_MASK;
3483 int dst_type = header & NX_LEARN_DST_MASK;
3484 unsigned int min_len;
3487 if (src_type == NX_LEARN_SRC_FIELD) {
3488 min_len += sizeof(ovs_be32); /* src_field */
3489 min_len += sizeof(ovs_be16); /* src_ofs */
3491 min_len += DIV_ROUND_UP(n_bits, 16);
3493 if (dst_type == NX_LEARN_DST_MATCH ||
3494 dst_type == NX_LEARN_DST_LOAD) {
3495 min_len += sizeof(ovs_be32); /* dst_field */
3496 min_len += sizeof(ovs_be16); /* dst_ofs */
3501 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3502 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3504 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3505 struct ofpbuf *ofpacts)
3507 struct ofpact_learn *learn;
3508 const void *p, *end;
3511 return OFPERR_OFPBAC_BAD_ARGUMENT;
3514 learn = ofpact_put_LEARN(ofpacts);
3516 learn->idle_timeout = ntohs(nal->idle_timeout);
3517 learn->hard_timeout = ntohs(nal->hard_timeout);
3518 learn->priority = ntohs(nal->priority);
3519 learn->cookie = nal->cookie;
3520 learn->table_id = nal->table_id;
3521 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3522 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3524 learn->flags = ntohs(nal->flags);
3525 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3526 NX_LEARN_F_DELETE_LEARNED)) {
3527 return OFPERR_OFPBAC_BAD_ARGUMENT;
3530 if (learn->table_id == 0xff) {
3531 return OFPERR_OFPBAC_BAD_ARGUMENT;
3534 end = (char *) nal + ntohs(nal->len);
3535 for (p = nal + 1; p != end; ) {
3536 struct ofpact_learn_spec *spec;
3537 uint16_t header = ntohs(get_be16(&p));
3543 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
3544 learn = ofpacts->frame;
3547 spec->src_type = header & NX_LEARN_SRC_MASK;
3548 spec->dst_type = header & NX_LEARN_DST_MASK;
3549 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
3551 /* Check for valid src and dst type combination. */
3552 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3553 spec->dst_type == NX_LEARN_DST_LOAD ||
3554 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
3555 spec->src_type == NX_LEARN_SRC_FIELD)) {
3558 return OFPERR_OFPBAC_BAD_ARGUMENT;
3561 /* Check that the arguments don't overrun the end of the action. */
3562 if ((char *) end - (char *) p < learn_min_len(header)) {
3563 return OFPERR_OFPBAC_BAD_LEN;
3566 /* Get the source. */
3567 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3568 get_subfield(spec->n_bits, &p, &spec->src);
3570 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3572 bitwise_copy(p, p_bytes, 0,
3573 &spec->src_imm, sizeof spec->src_imm, 0,
3575 p = (const uint8_t *) p + p_bytes;
3578 /* Get the destination. */
3579 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3580 spec->dst_type == NX_LEARN_DST_LOAD) {
3581 get_subfield(spec->n_bits, &p, &spec->dst);
3584 ofpact_update_len(ofpacts, &learn->ofpact);
3586 if (!is_all_zeros(p, (char *) end - (char *) p)) {
3587 return OFPERR_OFPBAC_BAD_ARGUMENT;
3594 put_be16(struct ofpbuf *b, ovs_be16 x)
3596 ofpbuf_put(b, &x, sizeof x);
3600 put_be32(struct ofpbuf *b, ovs_be32 x)
3602 ofpbuf_put(b, &x, sizeof x);
3606 put_u16(struct ofpbuf *b, uint16_t x)
3608 put_be16(b, htons(x));
3612 put_u32(struct ofpbuf *b, uint32_t x)
3614 put_be32(b, htonl(x));
3618 encode_LEARN(const struct ofpact_learn *learn,
3619 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3621 const struct ofpact_learn_spec *spec;
3622 struct nx_action_learn *nal;
3625 start_ofs = ofpbuf_size(out);
3626 nal = put_NXAST_LEARN(out);
3627 nal->idle_timeout = htons(learn->idle_timeout);
3628 nal->hard_timeout = htons(learn->hard_timeout);
3629 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
3630 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
3631 nal->priority = htons(learn->priority);
3632 nal->cookie = learn->cookie;
3633 nal->flags = htons(learn->flags);
3634 nal->table_id = learn->table_id;
3636 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
3637 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
3639 if (spec->src_type == NX_LEARN_SRC_FIELD) {
3640 put_u32(out, mf_nxm_header(spec->src.field->id));
3641 put_u16(out, spec->src.ofs);
3643 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
3644 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
3645 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
3646 bits, n_dst_bytes, 0,
3650 if (spec->dst_type == NX_LEARN_DST_MATCH ||
3651 spec->dst_type == NX_LEARN_DST_LOAD) {
3652 put_u32(out, mf_nxm_header(spec->dst.field->id));
3653 put_u16(out, spec->dst.ofs);
3657 pad_ofpat(out, start_ofs);
3660 static char * WARN_UNUSED_RESULT
3661 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
3662 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3664 return learn_parse(arg, ofpacts);
3668 format_LEARN(const struct ofpact_learn *a, struct ds *s)
3673 /* Action structure for NXAST_MULTIPATH.
3675 * This action performs the following steps in sequence:
3677 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
3678 * Refer to the definition of "enum nx_mp_fields" for details.
3680 * The 'basis' value is used as a universal hash parameter, that is,
3681 * different values of 'basis' yield different hash functions. The
3682 * particular universal hash function used is implementation-defined.
3684 * The hashed fields' values are drawn from the current state of the
3685 * flow, including all modifications that have been made by actions up to
3688 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
3689 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
3692 * The output of the algorithm is 'link', an unsigned integer less than
3693 * or equal to 'max_link'.
3695 * Some algorithms use 'arg' as an additional argument.
3697 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
3698 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
3701 * The switch will reject actions that have an unknown 'fields', or an unknown
3702 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
3703 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
3704 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
3706 struct nx_action_multipath {
3707 ovs_be16 type; /* OFPAT_VENDOR. */
3708 ovs_be16 len; /* Length is 32. */
3709 ovs_be32 vendor; /* NX_VENDOR_ID. */
3710 ovs_be16 subtype; /* NXAST_MULTIPATH. */
3712 /* What fields to hash and how. */
3713 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
3714 ovs_be16 basis; /* Universal hash parameter. */
3717 /* Multipath link choice algorithm to apply to hash value. */
3718 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
3719 ovs_be16 max_link; /* Number of output links, minus 1. */
3720 ovs_be32 arg; /* Algorithm-specific argument. */
3723 /* Where to store the result. */
3724 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
3725 ovs_be32 dst; /* Destination. */
3727 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
3730 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
3733 uint32_t n_links = ntohs(nam->max_link) + 1;
3734 size_t min_n_bits = log_2_ceil(n_links);
3735 struct ofpact_multipath *mp;
3737 mp = ofpact_put_MULTIPATH(out);
3738 mp->fields = ntohs(nam->fields);
3739 mp->basis = ntohs(nam->basis);
3740 mp->algorithm = ntohs(nam->algorithm);
3741 mp->max_link = ntohs(nam->max_link);
3742 mp->arg = ntohl(nam->arg);
3743 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
3744 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
3745 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
3747 if (!flow_hash_fields_valid(mp->fields)) {
3748 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
3749 return OFPERR_OFPBAC_BAD_ARGUMENT;
3750 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
3751 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
3752 && mp->algorithm != NX_MP_ALG_HRW
3753 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
3754 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
3755 return OFPERR_OFPBAC_BAD_ARGUMENT;
3756 } else if (mp->dst.n_bits < min_n_bits) {
3757 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
3758 "%"PRIu32" links", min_n_bits, n_links);
3759 return OFPERR_OFPBAC_BAD_ARGUMENT;
3762 return multipath_check(mp, NULL);
3766 encode_MULTIPATH(const struct ofpact_multipath *mp,
3767 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3769 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
3771 nam->fields = htons(mp->fields);
3772 nam->basis = htons(mp->basis);
3773 nam->algorithm = htons(mp->algorithm);
3774 nam->max_link = htons(mp->max_link);
3775 nam->arg = htonl(mp->arg);
3776 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
3777 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
3780 static char * WARN_UNUSED_RESULT
3781 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
3782 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3784 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
3788 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
3790 multipath_format(a, s);
3793 /* Action structure for NXAST_NOTE.
3795 * This action has no effect. It is variable length. The switch does not
3796 * attempt to interpret the user-defined 'note' data in any way. A controller
3797 * can use this action to attach arbitrary metadata to a flow.
3799 * This action might go away in the future.
3801 struct nx_action_note {
3802 ovs_be16 type; /* OFPAT_VENDOR. */
3803 ovs_be16 len; /* A multiple of 8, but at least 16. */
3804 ovs_be32 vendor; /* NX_VENDOR_ID. */
3805 ovs_be16 subtype; /* NXAST_NOTE. */
3806 uint8_t note[6]; /* Start of user-defined data. */
3807 /* Possibly followed by additional user-defined data. */
3809 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
3812 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan, struct ofpbuf *out)
3814 struct ofpact_note *note;
3815 unsigned int length;
3817 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
3818 note = ofpact_put(out, OFPACT_NOTE,
3819 offsetof(struct ofpact_note, data) + length);
3820 note->length = length;
3821 memcpy(note->data, nan->note, length);
3827 encode_NOTE(const struct ofpact_note *note,
3828 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3830 size_t start_ofs = ofpbuf_size(out);
3831 struct nx_action_note *nan;
3832 unsigned int remainder;
3835 put_NXAST_NOTE(out);
3836 ofpbuf_set_size(out, ofpbuf_size(out) - sizeof nan->note);
3838 ofpbuf_put(out, note->data, note->length);
3840 len = ofpbuf_size(out) - start_ofs;
3841 remainder = len % OFP_ACTION_ALIGN;
3843 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
3845 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
3846 nan->len = htons(ofpbuf_size(out) - start_ofs);
3849 static char * WARN_UNUSED_RESULT
3850 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
3851 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3853 struct ofpact_note *note;
3855 note = ofpact_put_NOTE(ofpacts);
3856 while (*arg != '\0') {
3867 byte = hexits_value(arg, 2, &ok);
3869 return xstrdup("bad hex digit in `note' argument");
3871 ofpbuf_put(ofpacts, &byte, 1);
3873 note = ofpacts->frame;
3878 ofpact_update_len(ofpacts, ¬e->ofpact);
3883 format_NOTE(const struct ofpact_note *a, struct ds *s)
3887 ds_put_cstr(s, "note:");
3888 for (i = 0; i < a->length; i++) {
3890 ds_put_char(s, '.');
3892 ds_put_format(s, "%02"PRIx8, a->data[i]);
3899 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
3901 ofpact_put_EXIT(out);
3906 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
3907 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3909 put_NXAST_EXIT(out);
3912 static char * WARN_UNUSED_RESULT
3913 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3914 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3916 ofpact_put_EXIT(ofpacts);
3921 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3923 ds_put_cstr(s, "exit");
3926 /* Action structure for NXAST_SAMPLE.
3928 * Samples matching packets with the given probability and sends them
3929 * each to the set of collectors identified with the given ID. The
3930 * probability is expressed as a number of packets to be sampled out
3931 * of USHRT_MAX packets, and must be >0.
3933 * When sending packet samples to IPFIX collectors, the IPFIX flow
3934 * record sent for each sampled packet is associated with the given
3935 * observation domain ID and observation point ID. Each IPFIX flow
3936 * record contain the sampled packet's headers when executing this
3937 * rule. If a sampled packet's headers are modified by previous
3938 * actions in the flow, those modified headers are sent. */
3939 struct nx_action_sample {
3940 ovs_be16 type; /* OFPAT_VENDOR. */
3941 ovs_be16 len; /* Length is 24. */
3942 ovs_be32 vendor; /* NX_VENDOR_ID. */
3943 ovs_be16 subtype; /* NXAST_SAMPLE. */
3944 ovs_be16 probability; /* Fraction of packets to sample. */
3945 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
3946 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
3947 ovs_be32 obs_point_id; /* ID of sampling observation point. */
3949 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
3952 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas, struct ofpbuf *out)
3954 struct ofpact_sample *sample;
3956 sample = ofpact_put_SAMPLE(out);
3957 sample->probability = ntohs(nas->probability);
3958 sample->collector_set_id = ntohl(nas->collector_set_id);
3959 sample->obs_domain_id = ntohl(nas->obs_domain_id);
3960 sample->obs_point_id = ntohl(nas->obs_point_id);
3962 if (sample->probability == 0) {
3963 return OFPERR_OFPBAC_BAD_ARGUMENT;
3970 encode_SAMPLE(const struct ofpact_sample *sample,
3971 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3973 struct nx_action_sample *nas;
3975 nas = put_NXAST_SAMPLE(out);
3976 nas->probability = htons(sample->probability);
3977 nas->collector_set_id = htonl(sample->collector_set_id);
3978 nas->obs_domain_id = htonl(sample->obs_domain_id);
3979 nas->obs_point_id = htonl(sample->obs_point_id);
3982 /* Parses 'arg' as the argument to a "sample" action, and appends such an
3983 * action to 'ofpacts'.
3985 * Returns NULL if successful, otherwise a malloc()'d string describing the
3986 * error. The caller is responsible for freeing the returned string. */
3987 static char * WARN_UNUSED_RESULT
3988 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
3989 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3991 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
3994 while (ofputil_parse_key_value(&arg, &key, &value)) {
3997 if (!strcmp(key, "probability")) {
3998 error = str_to_u16(value, "probability", &os->probability);
3999 if (!error && os->probability == 0) {
4000 error = xasprintf("invalid probability value \"%s\"", value);
4002 } else if (!strcmp(key, "collector_set_id")) {
4003 error = str_to_u32(value, &os->collector_set_id);
4004 } else if (!strcmp(key, "obs_domain_id")) {
4005 error = str_to_u32(value, &os->obs_domain_id);
4006 } else if (!strcmp(key, "obs_point_id")) {
4007 error = str_to_u32(value, &os->obs_point_id);
4009 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4016 if (os->probability == 0) {
4017 return xstrdup("non-zero \"probability\" must be specified on sample");
4023 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4025 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4026 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4027 a->probability, a->collector_set_id,
4028 a->obs_domain_id, a->obs_point_id);
4031 /* Meter instruction. */
4034 encode_METER(const struct ofpact_meter *meter,
4035 enum ofp_version ofp_version, struct ofpbuf *out)
4037 if (ofp_version >= OFP13_VERSION) {
4038 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4042 static char * WARN_UNUSED_RESULT
4043 parse_METER(char *arg, struct ofpbuf *ofpacts,
4044 enum ofputil_protocol *usable_protocols)
4046 *usable_protocols &= OFPUTIL_P_OF13_UP;
4047 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
4051 format_METER(const struct ofpact_meter *a, struct ds *s)
4053 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
4056 /* Clear-Actions instruction. */
4059 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
4060 enum ofp_version ofp_version OVS_UNUSED,
4061 struct ofpbuf *out OVS_UNUSED)
4063 if (ofp_version > OFP10_VERSION) {
4064 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
4068 static char * WARN_UNUSED_RESULT
4069 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4070 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4072 ofpact_put_CLEAR_ACTIONS(ofpacts);
4077 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4079 ds_put_cstr(s, "clear_actions");
4082 /* Write-Actions instruction. */
4085 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
4086 enum ofp_version ofp_version, struct ofpbuf *out)
4088 if (ofp_version > OFP10_VERSION) {
4089 const size_t ofs = ofpbuf_size(out);
4091 instruction_put_OFPIT11_WRITE_ACTIONS(out);
4092 ofpacts_put_openflow_actions(actions->actions,
4093 ofpact_nest_get_action_len(actions),
4095 ofpacts_update_instruction_actions(out, ofs);
4099 static char * WARN_UNUSED_RESULT
4100 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
4101 enum ofputil_protocol *usable_protocols)
4103 struct ofpact_nest *on;
4107 /* Pull off existing actions or instructions. */
4108 ofpact_pad(ofpacts);
4109 ofs = ofpbuf_size(ofpacts);
4110 ofpbuf_pull(ofpacts, ofs);
4112 /* Add a Write-Actions instruction and then pull it off. */
4113 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
4114 ofpbuf_pull(ofpacts, sizeof *on);
4116 /* Parse nested actions.
4118 * We pulled off "write-actions" and the previous actions because the
4119 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
4120 * that it doesn't actually include the nested actions. That means that
4121 * ofpacts_parse() would reject them as being part of an Apply-Actions that
4122 * follows a Write-Actions, which is an invalid order. */
4123 error = ofpacts_parse(arg, ofpacts, usable_protocols, false);
4125 /* Put the Write-Actions back on and update its length. */
4126 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
4127 on->ofpact.len = ofpbuf_size(ofpacts);
4129 /* Put any previous actions or instructions back on. */
4130 ofpbuf_push_uninit(ofpacts, ofs);
4136 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
4138 ds_put_cstr(s, "write_actions(");
4139 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
4140 ds_put_char(s, ')');
4143 /* Action structure for NXAST_WRITE_METADATA.
4145 * Modifies the 'mask' bits of the metadata value. */
4146 struct nx_action_write_metadata {
4147 ovs_be16 type; /* OFPAT_VENDOR. */
4148 ovs_be16 len; /* Length is 32. */
4149 ovs_be32 vendor; /* NX_VENDOR_ID. */
4150 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
4151 uint8_t zeros[6]; /* Must be zero. */
4152 ovs_be64 metadata; /* Metadata register. */
4153 ovs_be64 mask; /* Metadata mask. */
4155 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
4158 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
4161 struct ofpact_metadata *om;
4163 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
4164 return OFPERR_NXBRC_MUST_BE_ZERO;
4167 om = ofpact_put_WRITE_METADATA(out);
4168 om->metadata = nawm->metadata;
4169 om->mask = nawm->mask;
4175 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
4176 enum ofp_version ofp_version, struct ofpbuf *out)
4178 if (ofp_version == OFP10_VERSION) {
4179 struct nx_action_write_metadata *nawm;
4181 nawm = put_NXAST_WRITE_METADATA(out);
4182 nawm->metadata = metadata->metadata;
4183 nawm->mask = metadata->mask;
4185 struct ofp11_instruction_write_metadata *oiwm;
4187 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
4188 oiwm->metadata = metadata->metadata;
4189 oiwm->metadata_mask = metadata->mask;
4193 static char * WARN_UNUSED_RESULT
4194 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
4195 enum ofputil_protocol *usable_protocols)
4197 struct ofpact_metadata *om;
4198 char *mask = strchr(arg, '/');
4200 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
4202 om = ofpact_put_WRITE_METADATA(ofpacts);
4207 error = str_to_be64(mask + 1, &om->mask);
4212 om->mask = OVS_BE64_MAX;
4215 return str_to_be64(arg, &om->metadata);
4219 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
4221 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
4222 if (a->mask != OVS_BE64_MAX) {
4223 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
4227 /* Goto-Table instruction. */
4230 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
4231 enum ofp_version ofp_version, struct ofpbuf *out)
4233 if (ofp_version == OFP10_VERSION) {
4234 struct nx_action_resubmit *nar;
4236 nar = put_NXAST_RESUBMIT_TABLE(out);
4237 nar->table = goto_table->table_id;
4238 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
4240 struct ofp11_instruction_goto_table *oigt;
4242 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
4243 oigt->table_id = goto_table->table_id;
4244 memset(oigt->pad, 0, sizeof oigt->pad);
4248 static char * WARN_UNUSED_RESULT
4249 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
4250 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4252 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
4253 char *table_s = strsep(&arg, ",");
4254 if (!table_s || !table_s[0]) {
4255 return xstrdup("instruction goto-table needs table id");
4257 return str_to_u8(table_s, "table", &ogt->table_id);
4261 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
4263 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
4267 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
4268 const struct ofp_action_header *bad_action, enum ofperr error)
4270 if (!VLOG_DROP_WARN(&rl)) {
4274 ds_put_hex_dump(&s, actions, actions_len, 0, false);
4275 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
4276 (char *)bad_action - (char *)actions,
4277 ofperr_get_name(error), ds_cstr(&s));
4283 ofpacts_decode(const void *actions, size_t actions_len,
4284 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
4286 struct ofpbuf openflow;
4288 ofpbuf_use_const(&openflow, actions, actions_len);
4289 while (ofpbuf_size(&openflow)) {
4290 const struct ofp_action_header *action = ofpbuf_data(&openflow);
4291 enum ofp_raw_action_type raw;
4295 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
4297 error = ofpact_decode(action, raw, arg, ofpacts);
4301 log_bad_action(actions, actions_len, action, error);
4306 ofpact_pad(ofpacts);
4311 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
4312 unsigned int actions_len,
4313 enum ofp_version version,
4314 uint32_t allowed_ovsinsts,
4315 struct ofpbuf *ofpacts)
4317 const struct ofp_action_header *actions;
4320 ofpbuf_clear(ofpacts);
4322 if (actions_len % OFP_ACTION_ALIGN != 0) {
4323 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
4324 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
4325 return OFPERR_OFPBRC_BAD_LEN;
4328 actions = ofpbuf_try_pull(openflow, actions_len);
4329 if (actions == NULL) {
4330 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
4331 "remaining message length (%"PRIu32")",
4332 actions_len, ofpbuf_size(openflow));
4333 return OFPERR_OFPBRC_BAD_LEN;
4336 error = ofpacts_decode(actions, actions_len, version, ofpacts);
4338 ofpbuf_clear(ofpacts);
4342 error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
4345 ofpbuf_clear(ofpacts);
4350 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
4351 * front of 'openflow' into ofpacts. On success, replaces any existing content
4352 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
4353 * Returns 0 if successful, otherwise an OpenFlow error.
4355 * Actions are processed according to their OpenFlow version which
4356 * is provided in the 'version' parameter.
4358 * In most places in OpenFlow, actions appear encapsulated in instructions, so
4359 * you should call ofpacts_pull_openflow_instructions() instead of this
4362 * The parsed actions are valid generically, but they may not be valid in a
4363 * specific context. For example, port numbers up to OFPP_MAX are valid
4364 * generically, but specific datapaths may only support port numbers in a
4365 * smaller range. Use ofpacts_check() to additional check whether actions are
4366 * valid in a specific context. */
4368 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
4369 unsigned int actions_len,
4370 enum ofp_version version,
4371 struct ofpbuf *ofpacts)
4373 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
4374 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4378 /* OpenFlow 1.1 actions. */
4381 /* True if an action sets the value of a field
4382 * in a way that is compatibile with the action set.
4383 * The field can be set via either a set or a move action.
4384 * False otherwise. */
4386 ofpact_is_set_or_move_action(const struct ofpact *a)
4389 case OFPACT_SET_FIELD:
4390 case OFPACT_REG_MOVE:
4391 case OFPACT_SET_ETH_DST:
4392 case OFPACT_SET_ETH_SRC:
4393 case OFPACT_SET_IP_DSCP:
4394 case OFPACT_SET_IP_ECN:
4395 case OFPACT_SET_IP_TTL:
4396 case OFPACT_SET_IPV4_DST:
4397 case OFPACT_SET_IPV4_SRC:
4398 case OFPACT_SET_L4_DST_PORT:
4399 case OFPACT_SET_L4_SRC_PORT:
4400 case OFPACT_SET_MPLS_LABEL:
4401 case OFPACT_SET_MPLS_TC:
4402 case OFPACT_SET_MPLS_TTL:
4403 case OFPACT_SET_QUEUE:
4404 case OFPACT_SET_TUNNEL:
4405 case OFPACT_SET_VLAN_PCP:
4406 case OFPACT_SET_VLAN_VID:
4409 case OFPACT_CLEAR_ACTIONS:
4410 case OFPACT_CONTROLLER:
4411 case OFPACT_DEC_MPLS_TTL:
4412 case OFPACT_DEC_TTL:
4413 case OFPACT_ENQUEUE:
4415 case OFPACT_FIN_TIMEOUT:
4416 case OFPACT_GOTO_TABLE:
4420 case OFPACT_MULTIPATH:
4423 case OFPACT_OUTPUT_REG:
4424 case OFPACT_POP_MPLS:
4425 case OFPACT_POP_QUEUE:
4426 case OFPACT_PUSH_MPLS:
4427 case OFPACT_PUSH_VLAN:
4428 case OFPACT_RESUBMIT:
4430 case OFPACT_STACK_POP:
4431 case OFPACT_STACK_PUSH:
4432 case OFPACT_STRIP_VLAN:
4433 case OFPACT_WRITE_ACTIONS:
4434 case OFPACT_WRITE_METADATA:
4441 /* True if an action is allowed in the action set.
4442 * False otherwise. */
4444 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
4447 case OFPACT_DEC_MPLS_TTL:
4448 case OFPACT_DEC_TTL:
4451 case OFPACT_POP_MPLS:
4452 case OFPACT_PUSH_MPLS:
4453 case OFPACT_PUSH_VLAN:
4454 case OFPACT_REG_MOVE:
4455 case OFPACT_SET_FIELD:
4456 case OFPACT_SET_ETH_DST:
4457 case OFPACT_SET_ETH_SRC:
4458 case OFPACT_SET_IP_DSCP:
4459 case OFPACT_SET_IP_ECN:
4460 case OFPACT_SET_IP_TTL:
4461 case OFPACT_SET_IPV4_DST:
4462 case OFPACT_SET_IPV4_SRC:
4463 case OFPACT_SET_L4_DST_PORT:
4464 case OFPACT_SET_L4_SRC_PORT:
4465 case OFPACT_SET_MPLS_LABEL:
4466 case OFPACT_SET_MPLS_TC:
4467 case OFPACT_SET_MPLS_TTL:
4468 case OFPACT_SET_QUEUE:
4469 case OFPACT_SET_TUNNEL:
4470 case OFPACT_SET_VLAN_PCP:
4471 case OFPACT_SET_VLAN_VID:
4472 case OFPACT_STRIP_VLAN:
4475 /* In general these actions are excluded because they are not part of
4476 * the OpenFlow specification nor map to actions that are defined in
4477 * the specification. Thus the order in which they should be applied
4478 * in the action set is undefined. */
4480 case OFPACT_CONTROLLER:
4481 case OFPACT_ENQUEUE:
4483 case OFPACT_FIN_TIMEOUT:
4485 case OFPACT_MULTIPATH:
4487 case OFPACT_OUTPUT_REG:
4488 case OFPACT_POP_QUEUE:
4489 case OFPACT_RESUBMIT:
4491 case OFPACT_STACK_POP:
4492 case OFPACT_STACK_PUSH:
4494 /* The action set may only include actions and thus
4495 * may not include any instructions */
4496 case OFPACT_CLEAR_ACTIONS:
4497 case OFPACT_GOTO_TABLE:
4499 case OFPACT_WRITE_ACTIONS:
4500 case OFPACT_WRITE_METADATA:
4507 /* Append ofpact 'a' onto the tail of 'out' */
4509 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
4511 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
4514 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
4516 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
4517 enum ofpact_type filter)
4519 const struct ofpact *target;
4520 const struct ofpact *a;
4523 OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
4524 if (a->type == filter) {
4529 ofpact_copy(out, target);
4531 return target != NULL;
4534 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
4535 * The order of appended ofpacts is preserved between 'in' and 'out' */
4537 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
4538 bool (*filter)(const struct ofpact *))
4540 const struct ofpact *a;
4542 OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
4544 ofpact_copy(out, a);
4549 /* Reads 'action_set', which contains ofpacts accumulated by
4550 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
4551 * executed directly into 'action_list'. (These names correspond to the
4552 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
4554 * In general this involves appending the last instance of each action that is
4555 * adimissible in the action set in the order described in the OpenFlow
4559 * + output action is only appended if no group action was present in 'in'.
4560 * + As a simplification all set actions are copied in the order the are
4561 * provided in 'in' as many set actions applied to a field has the same
4562 * affect as only applying the last action that sets a field and
4563 * duplicates are removed by do_xlate_actions().
4564 * This has an unwanted side-effect of compsoting multiple
4565 * LOAD_REG actions that touch different regions of the same field. */
4567 ofpacts_execute_action_set(struct ofpbuf *action_list,
4568 const struct ofpbuf *action_set)
4570 /* The OpenFlow spec "Action Set" section specifies this order. */
4571 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
4572 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
4573 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
4574 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
4575 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
4576 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
4577 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
4578 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
4580 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
4581 * we should execute only OFPACT_GROUP.
4583 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
4584 * all the actions because there's no point in modifying a packet that will
4585 * not be sent anywhere. */
4586 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
4587 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
4588 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
4589 ofpbuf_clear(action_list);
4595 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
4596 size_t n_in, enum ofp_version version,
4601 size_t start = ofpbuf_size(out);
4603 error = ofpacts_decode(in, n_in, version, out);
4609 OFPACT_FOR_EACH (a, ofpact_end(ofpbuf_data(out), start), ofpbuf_size(out) - start) {
4610 if (!ofpact_is_allowed_in_actions_set(a)) {
4611 VLOG_WARN_RL(&rl, "disallowed action in action set");
4612 return OFPERR_OFPBAC_BAD_TYPE;
4619 /* OpenFlow 1.1 instructions. */
4621 struct instruction_type_info {
4622 enum ovs_instruction_type type;
4626 static const struct instruction_type_info inst_info[] = {
4627 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
4633 ovs_instruction_name_from_type(enum ovs_instruction_type type)
4635 return inst_info[type].name;
4639 ovs_instruction_type_from_name(const char *name)
4641 const struct instruction_type_info *p;
4642 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
4643 if (!strcasecmp(name, p->name)) {
4650 enum ovs_instruction_type
4651 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
4655 return OVSINST_OFPIT13_METER;
4656 case OFPACT_CLEAR_ACTIONS:
4657 return OVSINST_OFPIT11_CLEAR_ACTIONS;
4658 case OFPACT_WRITE_ACTIONS:
4659 return OVSINST_OFPIT11_WRITE_ACTIONS;
4660 case OFPACT_WRITE_METADATA:
4661 return OVSINST_OFPIT11_WRITE_METADATA;
4662 case OFPACT_GOTO_TABLE:
4663 return OVSINST_OFPIT11_GOTO_TABLE;
4666 case OFPACT_CONTROLLER:
4667 case OFPACT_ENQUEUE:
4668 case OFPACT_OUTPUT_REG:
4670 case OFPACT_SET_VLAN_VID:
4671 case OFPACT_SET_VLAN_PCP:
4672 case OFPACT_STRIP_VLAN:
4673 case OFPACT_PUSH_VLAN:
4674 case OFPACT_SET_ETH_SRC:
4675 case OFPACT_SET_ETH_DST:
4676 case OFPACT_SET_IPV4_SRC:
4677 case OFPACT_SET_IPV4_DST:
4678 case OFPACT_SET_IP_DSCP:
4679 case OFPACT_SET_IP_ECN:
4680 case OFPACT_SET_IP_TTL:
4681 case OFPACT_SET_L4_SRC_PORT:
4682 case OFPACT_SET_L4_DST_PORT:
4683 case OFPACT_REG_MOVE:
4684 case OFPACT_SET_FIELD:
4685 case OFPACT_STACK_PUSH:
4686 case OFPACT_STACK_POP:
4687 case OFPACT_DEC_TTL:
4688 case OFPACT_SET_MPLS_LABEL:
4689 case OFPACT_SET_MPLS_TC:
4690 case OFPACT_SET_MPLS_TTL:
4691 case OFPACT_DEC_MPLS_TTL:
4692 case OFPACT_PUSH_MPLS:
4693 case OFPACT_POP_MPLS:
4694 case OFPACT_SET_TUNNEL:
4695 case OFPACT_SET_QUEUE:
4696 case OFPACT_POP_QUEUE:
4697 case OFPACT_FIN_TIMEOUT:
4698 case OFPACT_RESUBMIT:
4700 case OFPACT_MULTIPATH:
4705 return OVSINST_OFPIT11_APPLY_ACTIONS;
4710 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
4711 const uint16_t inst_type)
4713 switch (inst_type) {
4715 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
4717 *instruction_type = OVSINST_##ENUM; \
4723 return OFPERR_OFPBIC_UNKNOWN_INST;
4727 /* Two-way translation between OVS's internal "OVSINST_*" representation of
4728 * instructions and the "OFPIT_*" representation used in OpenFlow. */
4729 struct ovsinst_map {
4730 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
4731 int ofpit; /* OFPIT_* number from OpenFlow spec. */
4734 static const struct ovsinst_map *
4735 get_ovsinst_map(enum ofp_version version)
4737 /* OpenFlow 1.1 and 1.2 instructions. */
4738 static const struct ovsinst_map of11[] = {
4739 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
4740 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
4741 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
4742 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
4743 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
4747 /* OpenFlow 1.3+ instructions. */
4748 static const struct ovsinst_map of13[] = {
4749 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
4750 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
4751 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
4752 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
4753 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
4754 { OVSINST_OFPIT13_METER, 6 },
4758 return version < OFP13_VERSION ? of11 : of13;
4761 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
4762 * values, into a bitmap of instructions suitable for OpenFlow 'version'
4763 * (OFP11_VERSION or later), and returns the result. */
4765 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
4767 uint32_t ofpit_bitmap = 0;
4768 const struct ovsinst_map *x;
4770 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
4771 if (ovsinst_bitmap & (1u << x->ovsinst)) {
4772 ofpit_bitmap |= 1u << x->ofpit;
4775 return htonl(ofpit_bitmap);
4778 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
4779 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
4780 * correspond to OVSINST_* values, and returns the result. */
4782 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
4784 uint32_t ovsinst_bitmap = 0;
4785 const struct ovsinst_map *x;
4787 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
4788 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
4789 ovsinst_bitmap |= 1u << x->ovsinst;
4792 return ovsinst_bitmap;
4795 static inline struct ofp11_instruction *
4796 instruction_next(const struct ofp11_instruction *inst)
4798 return ((struct ofp11_instruction *) (void *)
4799 ((uint8_t *) inst + ntohs(inst->len)));
4803 instruction_is_valid(const struct ofp11_instruction *inst,
4804 size_t n_instructions)
4806 uint16_t len = ntohs(inst->len);
4807 return (!(len % OFP11_INSTRUCTION_ALIGN)
4808 && len >= sizeof *inst
4809 && len / sizeof *inst <= n_instructions);
4812 /* This macro is careful to check for instructions with bad lengths. */
4813 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
4814 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
4815 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
4816 ((LEFT) -= (ntohs((ITER)->len) \
4817 / sizeof(struct ofp11_instruction)), \
4818 (ITER) = instruction_next(ITER)))
4821 decode_openflow11_instruction(const struct ofp11_instruction *inst,
4822 enum ovs_instruction_type *type)
4824 uint16_t len = ntohs(inst->len);
4826 switch (inst->type) {
4827 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
4828 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
4830 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
4831 case CONSTANT_HTONS(ENUM): \
4833 ? len >= sizeof(struct STRUCT) \
4834 : len == sizeof(struct STRUCT)) { \
4835 *type = OVSINST_##ENUM; \
4838 return OFPERR_OFPBIC_BAD_LEN; \
4844 return OFPERR_OFPBIC_UNKNOWN_INST;
4849 decode_openflow11_instructions(const struct ofp11_instruction insts[],
4851 const struct ofp11_instruction *out[])
4853 const struct ofp11_instruction *inst;
4856 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
4857 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
4858 enum ovs_instruction_type type;
4861 error = decode_openflow11_instruction(inst, &type);
4867 return OFPERR_OFPBIC_DUP_INST;
4873 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
4874 (n_insts - left) * sizeof *inst);
4875 return OFPERR_OFPBIC_BAD_LEN;
4881 get_actions_from_instruction(const struct ofp11_instruction *inst,
4882 const struct ofp_action_header **actions,
4883 size_t *actions_len)
4885 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
4886 *actions_len = ntohs(inst->len) - sizeof *inst;
4890 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
4891 unsigned int instructions_len,
4892 enum ofp_version version,
4893 struct ofpbuf *ofpacts)
4895 const struct ofp11_instruction *instructions;
4896 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
4899 if (version == OFP10_VERSION) {
4900 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
4902 (1u << N_OVS_INSTRUCTIONS) - 1,
4906 ofpbuf_clear(ofpacts);
4908 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
4909 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
4911 instructions_len, OFP11_INSTRUCTION_ALIGN);
4912 error = OFPERR_OFPBIC_BAD_LEN;
4916 instructions = ofpbuf_try_pull(openflow, instructions_len);
4917 if (instructions == NULL) {
4918 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
4919 "remaining message length (%"PRIu32")",
4920 instructions_len, ofpbuf_size(openflow));
4921 error = OFPERR_OFPBIC_BAD_LEN;
4925 error = decode_openflow11_instructions(
4926 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
4932 if (insts[OVSINST_OFPIT13_METER]) {
4933 const struct ofp13_instruction_meter *oim;
4934 struct ofpact_meter *om;
4936 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
4937 insts[OVSINST_OFPIT13_METER]);
4939 om = ofpact_put_METER(ofpacts);
4940 om->meter_id = ntohl(oim->meter_id);
4942 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
4943 const struct ofp_action_header *actions;
4946 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
4947 &actions, &actions_len);
4948 error = ofpacts_decode(actions, actions_len, version, ofpacts);
4953 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
4954 instruction_get_OFPIT11_CLEAR_ACTIONS(
4955 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
4956 ofpact_put_CLEAR_ACTIONS(ofpacts);
4958 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
4959 struct ofpact_nest *on;
4960 const struct ofp_action_header *actions;
4964 ofpact_pad(ofpacts);
4965 start = ofpbuf_size(ofpacts);
4966 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
4967 offsetof(struct ofpact_nest, actions));
4968 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
4969 &actions, &actions_len);
4970 error = ofpacts_decode_for_action_set(actions, actions_len,
4975 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
4976 on->ofpact.len = ofpbuf_size(ofpacts) - start;
4978 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
4979 const struct ofp11_instruction_write_metadata *oiwm;
4980 struct ofpact_metadata *om;
4982 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
4983 insts[OVSINST_OFPIT11_WRITE_METADATA]);
4985 om = ofpact_put_WRITE_METADATA(ofpacts);
4986 om->metadata = oiwm->metadata;
4987 om->mask = oiwm->metadata_mask;
4989 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
4990 const struct ofp11_instruction_goto_table *oigt;
4991 struct ofpact_goto_table *ogt;
4993 oigt = instruction_get_OFPIT11_GOTO_TABLE(
4994 insts[OVSINST_OFPIT11_GOTO_TABLE]);
4995 ogt = ofpact_put_GOTO_TABLE(ofpacts);
4996 ogt->table_id = oigt->table_id;
4999 error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
5000 (1u << N_OVS_INSTRUCTIONS) - 1);
5003 ofpbuf_clear(ofpacts);
5008 /* Update the length of the instruction that begins at offset 'ofs' within
5009 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5010 * If the instruction contains no nested actions, deletes it entirely. */
5012 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5014 struct ofp11_instruction_actions *oia;
5016 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5017 if (ofpbuf_size(openflow) > ofs + sizeof *oia) {
5018 oia->len = htons(ofpbuf_size(openflow) - ofs);
5020 ofpbuf_set_size(openflow, ofs);
5024 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5025 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5026 * valid, otherwise an OpenFlow error code. */
5028 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
5036 case OFPP_CONTROLLER:
5042 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
5045 return OFPERR_OFPBAC_BAD_OUT_PORT;
5049 /* Removes the protocols that require consistency between match and actions
5050 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
5052 * (An example of an inconsistency between match and actions is a flow that
5053 * does not match on an MPLS Ethertype but has an action that pops an MPLS
5056 inconsistent_match(enum ofputil_protocol *usable_protocols)
5058 *usable_protocols &= OFPUTIL_P_OF10_ANY;
5061 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
5062 * caller must restore them.
5064 * Modifies some actions, filling in fields that could not be properly set
5065 * without context. */
5067 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
5068 struct flow *flow, ofp_port_t max_ports,
5069 uint8_t table_id, uint8_t n_tables)
5071 const struct ofpact_enqueue *enqueue;
5072 const struct mf_field *mf;
5076 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
5079 case OFPACT_CONTROLLER:
5082 case OFPACT_ENQUEUE:
5083 enqueue = ofpact_get_ENQUEUE(a);
5084 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
5085 && enqueue->port != OFPP_IN_PORT
5086 && enqueue->port != OFPP_LOCAL) {
5087 return OFPERR_OFPBAC_BAD_OUT_PORT;
5091 case OFPACT_OUTPUT_REG:
5092 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
5095 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
5097 case OFPACT_SET_VLAN_VID:
5098 /* Remember if we saw a vlan tag in the flow to aid translating to
5099 * OpenFlow 1.1+ if need be. */
5100 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
5101 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5102 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5103 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
5104 inconsistent_match(usable_protocols);
5106 /* Temporary mark that we have a vlan tag. */
5107 flow->vlan_tci |= htons(VLAN_CFI);
5110 case OFPACT_SET_VLAN_PCP:
5111 /* Remember if we saw a vlan tag in the flow to aid translating to
5112 * OpenFlow 1.1+ if need be. */
5113 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
5114 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5115 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
5116 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
5117 inconsistent_match(usable_protocols);
5119 /* Temporary mark that we have a vlan tag. */
5120 flow->vlan_tci |= htons(VLAN_CFI);
5123 case OFPACT_STRIP_VLAN:
5124 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
5125 inconsistent_match(usable_protocols);
5127 /* Temporary mark that we have no vlan tag. */
5128 flow->vlan_tci = htons(0);
5131 case OFPACT_PUSH_VLAN:
5132 if (flow->vlan_tci & htons(VLAN_CFI)) {
5133 /* Multiple VLAN headers not supported. */
5134 return OFPERR_OFPBAC_BAD_TAG;
5136 /* Temporary mark that we have a vlan tag. */
5137 flow->vlan_tci |= htons(VLAN_CFI);
5140 case OFPACT_SET_ETH_SRC:
5141 case OFPACT_SET_ETH_DST:
5144 case OFPACT_SET_IPV4_SRC:
5145 case OFPACT_SET_IPV4_DST:
5146 if (flow->dl_type != htons(ETH_TYPE_IP)) {
5147 inconsistent_match(usable_protocols);
5151 case OFPACT_SET_IP_DSCP:
5152 case OFPACT_SET_IP_ECN:
5153 case OFPACT_SET_IP_TTL:
5154 case OFPACT_DEC_TTL:
5155 if (!is_ip_any(flow)) {
5156 inconsistent_match(usable_protocols);
5160 case OFPACT_SET_L4_SRC_PORT:
5161 if (!is_ip_any(flow) ||
5162 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5163 && flow->nw_proto != IPPROTO_SCTP)) {
5164 inconsistent_match(usable_protocols);
5166 /* Note on which transport protocol the port numbers are set.
5167 * This allows this set action to be converted to an OF1.2 set field
5169 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
5172 case OFPACT_SET_L4_DST_PORT:
5173 if (!is_ip_any(flow) ||
5174 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
5175 && flow->nw_proto != IPPROTO_SCTP)) {
5176 inconsistent_match(usable_protocols);
5178 /* Note on which transport protocol the port numbers are set.
5179 * This allows this set action to be converted to an OF1.2 set field
5181 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
5184 case OFPACT_REG_MOVE:
5185 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
5187 case OFPACT_SET_FIELD:
5188 mf = ofpact_get_SET_FIELD(a)->field;
5189 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
5190 if (!mf_are_prereqs_ok(mf, flow) ||
5191 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
5192 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
5194 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
5196 /* Remember if we saw a vlan tag in the flow to aid translating to
5197 * OpenFlow 1.1 if need be. */
5198 ofpact_get_SET_FIELD(a)->flow_has_vlan =
5199 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
5200 if (mf->id == MFF_VLAN_TCI) {
5201 /* The set field may add or remove the vlan tag,
5202 * Mark the status temporarily. */
5203 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
5207 case OFPACT_STACK_PUSH:
5208 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
5210 case OFPACT_STACK_POP:
5211 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
5213 case OFPACT_SET_MPLS_LABEL:
5214 case OFPACT_SET_MPLS_TC:
5215 case OFPACT_SET_MPLS_TTL:
5216 case OFPACT_DEC_MPLS_TTL:
5217 if (!eth_type_mpls(flow->dl_type)) {
5218 inconsistent_match(usable_protocols);
5222 case OFPACT_SET_TUNNEL:
5223 case OFPACT_SET_QUEUE:
5224 case OFPACT_POP_QUEUE:
5225 case OFPACT_RESUBMIT:
5228 case OFPACT_FIN_TIMEOUT:
5229 if (flow->nw_proto != IPPROTO_TCP) {
5230 inconsistent_match(usable_protocols);
5235 return learn_check(ofpact_get_LEARN(a), flow);
5237 case OFPACT_MULTIPATH:
5238 return multipath_check(ofpact_get_MULTIPATH(a), flow);
5244 case OFPACT_PUSH_MPLS:
5245 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
5246 /* The packet is now MPLS and the MPLS payload is opaque.
5247 * Thus nothing can be assumed about the network protocol.
5248 * Temporarily mark that we have no nw_proto. */
5252 case OFPACT_POP_MPLS:
5253 if (!eth_type_mpls(flow->dl_type)) {
5254 inconsistent_match(usable_protocols);
5256 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
5262 case OFPACT_CLEAR_ACTIONS:
5265 case OFPACT_WRITE_ACTIONS: {
5266 /* Use a temporary copy of 'usable_protocols' because we can't check
5267 * consistency of an action set. */
5268 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
5269 enum ofputil_protocol p = *usable_protocols;
5270 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
5271 flow, max_ports, table_id, n_tables, &p);
5274 case OFPACT_WRITE_METADATA:
5277 case OFPACT_METER: {
5278 uint32_t mid = ofpact_get_METER(a)->meter_id;
5279 if (mid == 0 || mid > OFPM13_MAX) {
5280 return OFPERR_OFPMMFC_INVALID_METER;
5285 case OFPACT_GOTO_TABLE: {
5286 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
5287 if ((table_id != 255 && goto_table <= table_id)
5288 || (n_tables != 255 && goto_table >= n_tables)) {
5289 return OFPERR_OFPBIC_BAD_TABLE_ID;
5302 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5303 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
5304 * switch with no more than 'max_ports' ports.
5306 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
5307 * '*usable_protocols' the protocols that forbid the inconsistency. (An
5308 * example of an inconsistency between match and actions is a flow that does
5309 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
5311 * May annotate ofpacts with information gathered from the 'flow'.
5313 * May temporarily modify 'flow', but restores the changes before returning. */
5315 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
5316 struct flow *flow, ofp_port_t max_ports,
5317 uint8_t table_id, uint8_t n_tables,
5318 enum ofputil_protocol *usable_protocols)
5321 ovs_be16 dl_type = flow->dl_type;
5322 ovs_be16 vlan_tci = flow->vlan_tci;
5323 uint8_t nw_proto = flow->nw_proto;
5324 enum ofperr error = 0;
5326 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5327 error = ofpact_check__(usable_protocols, a, flow,
5328 max_ports, table_id, n_tables);
5333 /* Restore fields that may have been modified. */
5334 flow->dl_type = dl_type;
5335 flow->vlan_tci = vlan_tci;
5336 flow->nw_proto = nw_proto;
5340 /* Like ofpacts_check(), but reports inconsistencies as
5341 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
5343 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
5344 struct flow *flow, ofp_port_t max_ports,
5345 uint8_t table_id, uint8_t n_tables,
5346 enum ofputil_protocol usable_protocols)
5348 enum ofputil_protocol p = usable_protocols;
5351 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
5352 table_id, n_tables, &p);
5353 return (error ? error
5354 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
5358 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
5359 * in the appropriate order as defined by the OpenFlow spec. */
5361 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
5362 uint32_t allowed_ovsinsts)
5364 const struct ofpact *a;
5365 enum ovs_instruction_type inst;
5367 inst = OVSINST_OFPIT13_METER;
5368 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5369 enum ovs_instruction_type next;
5371 next = ovs_instruction_type_from_ofpact_type(a->type);
5373 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
5376 const char *name = ovs_instruction_name_from_type(inst);
5377 const char *next_name = ovs_instruction_name_from_type(next);
5380 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
5381 "1.1+ compatibility", name);
5383 VLOG_WARN("invalid instruction ordering: %s must appear "
5384 "before %s, for OpenFlow 1.1+ compatibility",
5387 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
5389 if (!((1u << next) & allowed_ovsinsts)) {
5390 const char *name = ovs_instruction_name_from_type(next);
5392 VLOG_WARN("%s instruction not allowed here", name);
5393 return OFPERR_OFPBIC_UNSUP_INST;
5402 /* Converting ofpacts to OpenFlow. */
5405 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
5409 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5410 case OFPACT_##ENUM: \
5411 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
5420 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
5421 * actions in 'openflow', appending the actions to any existing data in
5424 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
5425 struct ofpbuf *openflow,
5426 enum ofp_version ofp_version)
5428 const struct ofpact *a;
5429 size_t start_size = ofpbuf_size(openflow);
5431 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5432 encode_ofpact(a, ofp_version, openflow);
5434 return ofpbuf_size(openflow) - start_size;
5437 static enum ovs_instruction_type
5438 ofpact_is_apply_actions(const struct ofpact *a)
5440 return (ovs_instruction_type_from_ofpact_type(a->type)
5441 == OVSINST_OFPIT11_APPLY_ACTIONS);
5445 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
5447 struct ofpbuf *openflow,
5448 enum ofp_version ofp_version)
5450 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
5451 const struct ofpact *a;
5453 if (ofp_version == OFP10_VERSION) {
5454 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
5461 if (ofpact_is_apply_actions(a)) {
5462 size_t ofs = ofpbuf_size(openflow);
5464 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
5466 encode_ofpact(a, ofp_version, openflow);
5468 } while (a < end && ofpact_is_apply_actions(a));
5469 ofpacts_update_instruction_actions(openflow, ofs);
5471 encode_ofpact(a, ofp_version, openflow);
5477 /* Sets of supported actions. */
5479 /* Two-way translation between OVS's internal "OFPACT_*" representation of
5480 * actions and the "OFPAT_*" representation used in some OpenFlow version.
5481 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
5482 * instance is specific to one OpenFlow version.) */
5484 enum ofpact_type ofpact; /* Internal name for action type. */
5485 int ofpat; /* OFPAT_* number from OpenFlow spec. */
5488 static const struct ofpact_map *
5489 get_ofpact_map(enum ofp_version version)
5491 /* OpenFlow 1.0 actions. */
5492 static const struct ofpact_map of10[] = {
5493 { OFPACT_OUTPUT, 0 },
5494 { OFPACT_SET_VLAN_VID, 1 },
5495 { OFPACT_SET_VLAN_PCP, 2 },
5496 { OFPACT_STRIP_VLAN, 3 },
5497 { OFPACT_SET_ETH_SRC, 4 },
5498 { OFPACT_SET_ETH_DST, 5 },
5499 { OFPACT_SET_IPV4_SRC, 6 },
5500 { OFPACT_SET_IPV4_DST, 7 },
5501 { OFPACT_SET_IP_DSCP, 8 },
5502 { OFPACT_SET_L4_SRC_PORT, 9 },
5503 { OFPACT_SET_L4_DST_PORT, 10 },
5504 { OFPACT_ENQUEUE, 11 },
5508 /* OpenFlow 1.1 actions. */
5509 static const struct ofpact_map of11[] = {
5510 { OFPACT_OUTPUT, 0 },
5511 { OFPACT_SET_VLAN_VID, 1 },
5512 { OFPACT_SET_VLAN_PCP, 2 },
5513 { OFPACT_SET_ETH_SRC, 3 },
5514 { OFPACT_SET_ETH_DST, 4 },
5515 { OFPACT_SET_IPV4_SRC, 5 },
5516 { OFPACT_SET_IPV4_DST, 6 },
5517 { OFPACT_SET_IP_DSCP, 7 },
5518 { OFPACT_SET_IP_ECN, 8 },
5519 { OFPACT_SET_L4_SRC_PORT, 9 },
5520 { OFPACT_SET_L4_DST_PORT, 10 },
5521 /* OFPAT_COPY_TTL_OUT (11) not supported. */
5522 /* OFPAT_COPY_TTL_IN (12) not supported. */
5523 { OFPACT_SET_MPLS_LABEL, 13 },
5524 { OFPACT_SET_MPLS_TC, 14 },
5525 { OFPACT_SET_MPLS_TTL, 15 },
5526 { OFPACT_DEC_MPLS_TTL, 16 },
5527 { OFPACT_PUSH_VLAN, 17 },
5528 { OFPACT_STRIP_VLAN, 18 },
5529 { OFPACT_PUSH_MPLS, 19 },
5530 { OFPACT_POP_MPLS, 20 },
5531 { OFPACT_SET_QUEUE, 21 },
5532 { OFPACT_GROUP, 22 },
5533 { OFPACT_SET_IP_TTL, 23 },
5534 { OFPACT_DEC_TTL, 24 },
5538 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
5539 static const struct ofpact_map of12[] = {
5540 { OFPACT_OUTPUT, 0 },
5541 /* OFPAT_COPY_TTL_OUT (11) not supported. */
5542 /* OFPAT_COPY_TTL_IN (12) not supported. */
5543 { OFPACT_SET_MPLS_TTL, 15 },
5544 { OFPACT_DEC_MPLS_TTL, 16 },
5545 { OFPACT_PUSH_VLAN, 17 },
5546 { OFPACT_STRIP_VLAN, 18 },
5547 { OFPACT_PUSH_MPLS, 19 },
5548 { OFPACT_POP_MPLS, 20 },
5549 { OFPACT_SET_QUEUE, 21 },
5550 { OFPACT_GROUP, 22 },
5551 { OFPACT_SET_IP_TTL, 23 },
5552 { OFPACT_DEC_TTL, 24 },
5553 { OFPACT_SET_FIELD, 25 },
5554 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
5555 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
5575 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
5576 * values, into a bitmap of actions suitable for OpenFlow 'version', and
5577 * returns the result. */
5579 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
5581 uint32_t openflow_bitmap = 0;
5582 const struct ofpact_map *x;
5584 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
5585 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
5586 openflow_bitmap |= 1u << x->ofpat;
5589 return htonl(openflow_bitmap);
5592 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
5593 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
5594 * and returns the result. */
5596 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
5598 uint64_t ofpact_bitmap = 0;
5599 const struct ofpact_map *x;
5601 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
5602 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
5603 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
5606 return ofpact_bitmap;
5609 /* Appends to 's' a string representation of the set of OFPACT_* represented
5610 * by 'ofpacts_bitmap'. */
5612 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
5614 if (!ofpacts_bitmap) {
5615 ds_put_cstr(s, "<none>");
5617 while (ofpacts_bitmap) {
5618 ds_put_format(s, "%s ",
5619 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
5620 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
5626 /* Returns true if 'action' outputs to 'port', false otherwise. */
5628 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
5630 switch (ofpact->type) {
5632 return ofpact_get_OUTPUT(ofpact)->port == port;
5633 case OFPACT_ENQUEUE:
5634 return ofpact_get_ENQUEUE(ofpact)->port == port;
5635 case OFPACT_CONTROLLER:
5636 return port == OFPP_CONTROLLER;
5638 case OFPACT_OUTPUT_REG:
5640 case OFPACT_SET_VLAN_VID:
5641 case OFPACT_SET_VLAN_PCP:
5642 case OFPACT_STRIP_VLAN:
5643 case OFPACT_PUSH_VLAN:
5644 case OFPACT_SET_ETH_SRC:
5645 case OFPACT_SET_ETH_DST:
5646 case OFPACT_SET_IPV4_SRC:
5647 case OFPACT_SET_IPV4_DST:
5648 case OFPACT_SET_IP_DSCP:
5649 case OFPACT_SET_IP_ECN:
5650 case OFPACT_SET_IP_TTL:
5651 case OFPACT_SET_L4_SRC_PORT:
5652 case OFPACT_SET_L4_DST_PORT:
5653 case OFPACT_REG_MOVE:
5654 case OFPACT_SET_FIELD:
5655 case OFPACT_STACK_PUSH:
5656 case OFPACT_STACK_POP:
5657 case OFPACT_DEC_TTL:
5658 case OFPACT_SET_MPLS_LABEL:
5659 case OFPACT_SET_MPLS_TC:
5660 case OFPACT_SET_MPLS_TTL:
5661 case OFPACT_DEC_MPLS_TTL:
5662 case OFPACT_SET_TUNNEL:
5663 case OFPACT_WRITE_METADATA:
5664 case OFPACT_SET_QUEUE:
5665 case OFPACT_POP_QUEUE:
5666 case OFPACT_FIN_TIMEOUT:
5667 case OFPACT_RESUBMIT:
5669 case OFPACT_MULTIPATH:
5672 case OFPACT_PUSH_MPLS:
5673 case OFPACT_POP_MPLS:
5675 case OFPACT_CLEAR_ACTIONS:
5676 case OFPACT_WRITE_ACTIONS:
5677 case OFPACT_GOTO_TABLE:
5685 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
5686 * to 'port', false otherwise. */
5688 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
5691 const struct ofpact *a;
5693 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5694 if (ofpact_outputs_to_port(a, port)) {
5702 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
5703 * to 'group', false otherwise. */
5705 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
5708 const struct ofpact *a;
5710 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5711 if (a->type == OFPACT_GROUP
5712 && ofpact_get_GROUP(a)->group_id == group_id) {
5721 ofpacts_equal(const struct ofpact *a, size_t a_len,
5722 const struct ofpact *b, size_t b_len)
5724 return a_len == b_len && !memcmp(a, b, a_len);
5727 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
5728 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
5730 * This function relies on the order of 'ofpacts' being correct (as checked by
5731 * ofpacts_verify()). */
5733 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
5735 const struct ofpact *a;
5737 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5738 enum ovs_instruction_type inst;
5740 inst = ovs_instruction_type_from_ofpact_type(a->type);
5741 if (a->type == OFPACT_METER) {
5742 return ofpact_get_METER(a)->meter_id;
5743 } else if (inst > OVSINST_OFPIT13_METER) {
5751 /* Formatting ofpacts. */
5754 ofpact_format(const struct ofpact *a, struct ds *s)
5757 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5758 case OFPACT_##ENUM: \
5759 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
5768 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
5769 * 'ofpacts' to 'string'. */
5771 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
5775 ds_put_cstr(string, "drop");
5777 const struct ofpact *a;
5779 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5781 ds_put_cstr(string, ",");
5784 /* XXX write-actions */
5785 ofpact_format(a, string);
5790 /* Internal use by helpers. */
5793 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
5795 struct ofpact *ofpact;
5797 ofpact_pad(ofpacts);
5798 ofpact = ofpacts->frame = ofpbuf_put_uninit(ofpacts, len);
5799 ofpact_init(ofpact, type, len);
5804 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
5806 memset(ofpact, 0, len);
5807 ofpact->type = type;
5812 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
5813 * starting at 'ofpact'.
5815 * This is the correct way to update a variable-length ofpact's length after
5816 * adding the variable-length part of the payload. (See the large comment
5817 * near the end of ofp-actions.h for more information.) */
5819 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
5821 ovs_assert(ofpact == ofpacts->frame);
5822 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
5825 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
5826 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
5827 * client must call this itself after adding the final ofpact to an array of
5830 * (The consequences of failing to call this function are probably not dire.
5831 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
5832 * not dereference it. That's undefined behavior, technically, but it will not
5833 * cause a real problem on common systems. Still, it seems better to call
5836 ofpact_pad(struct ofpbuf *ofpacts)
5838 unsigned int pad = PAD_SIZE(ofpbuf_size(ofpacts), OFPACT_ALIGNTO);
5840 ofpbuf_put_zeros(ofpacts, pad);
5847 static char * WARN_UNUSED_RESULT
5848 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
5849 enum ofputil_protocol *usable_protocols)
5852 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5853 case OFPACT_##ENUM: \
5854 return parse_##ENUM(value, ofpacts, usable_protocols);
5863 ofpact_type_from_name(const char *name, enum ofpact_type *type)
5865 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
5866 if (!strcasecmp(name, NAME)) { \
5867 *type = OFPACT_##ENUM; \
5876 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
5878 * Returns NULL if successful, otherwise a malloc()'d string describing the
5879 * error. The caller is responsible for freeing the returned string. */
5880 static char * WARN_UNUSED_RESULT
5881 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
5882 enum ofputil_protocol *usable_protocols,
5883 bool allow_instructions)
5892 while (ofputil_parse_key_value(&pos, &key, &value)) {
5893 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
5894 enum ofpact_type type;
5898 if (ofpact_type_from_name(key, &type)) {
5899 error = ofpact_parse(type, value, ofpacts, usable_protocols);
5900 inst = ovs_instruction_type_from_ofpact_type(type);
5901 } else if (!strcasecmp(key, "mod_vlan_vid")) {
5902 error = parse_set_vlan_vid(value, ofpacts, true);
5903 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
5904 error = parse_set_vlan_pcp(value, ofpacts, true);
5905 } else if (!strcasecmp(key, "set_nw_ttl")) {
5906 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
5907 } else if (!strcasecmp(key, "pop_vlan")) {
5908 error = parse_pop_vlan(ofpacts);
5909 } else if (!strcasecmp(key, "set_tunnel64")) {
5910 error = parse_set_tunnel(value, ofpacts,
5911 NXAST_RAW_SET_TUNNEL64);
5912 } else if (!strcasecmp(key, "load")) {
5913 error = parse_reg_load(value, ofpacts);
5914 } else if (!strcasecmp(key, "bundle_load")) {
5915 error = parse_bundle_load(value, ofpacts);
5916 } else if (!strcasecmp(key, "drop")) {
5918 } else if (!strcasecmp(key, "apply_actions")) {
5919 return xstrdup("apply_actions is the default instruction");
5920 } else if (ofputil_port_from_string(key, &port)) {
5921 ofpact_put_OUTPUT(ofpacts)->port = port;
5923 return xasprintf("unknown action %s", key);
5929 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
5930 if (!allow_instructions) {
5931 return xasprintf("only actions are allowed here (not "
5933 ovs_instruction_name_from_type(inst));
5935 if (inst == prev_inst) {
5936 return xasprintf("instruction %s may be specified only once",
5937 ovs_instruction_name_from_type(inst));
5940 if (prev_inst != -1 && inst < prev_inst) {
5941 return xasprintf("instruction %s must be specified before %s",
5942 ovs_instruction_name_from_type(inst),
5943 ovs_instruction_name_from_type(prev_inst));
5947 ofpact_pad(ofpacts);
5949 if (drop && ofpbuf_size(ofpacts)) {
5950 return xstrdup("\"drop\" must not be accompanied by any other action "
5954 retval = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
5956 ? (1u << N_OVS_INSTRUCTIONS) - 1
5957 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS));
5959 return xstrdup("Incorrect instruction ordering");
5965 static char * WARN_UNUSED_RESULT
5966 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
5967 enum ofputil_protocol *usable_protocols, bool allow_instructions)
5969 uint32_t orig_size = ofpbuf_size(ofpacts);
5970 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
5971 allow_instructions);
5973 ofpbuf_set_size(ofpacts, orig_size);
5978 static char * WARN_UNUSED_RESULT
5979 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
5980 enum ofputil_protocol *usable_protocols,
5981 bool allow_instructions)
5985 *usable_protocols = OFPUTIL_P_ANY;
5988 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions);
5994 /* Parses 's' as a set of OpenFlow actions and appends the actions to
5997 * Returns NULL if successful, otherwise a malloc()'d string describing the
5998 * error. The caller is responsible for freeing the returned string. */
5999 char * WARN_UNUSED_RESULT
6000 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
6001 enum ofputil_protocol *usable_protocols)
6003 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false);
6006 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
6009 * Returns NULL if successful, otherwise a malloc()'d string describing the
6010 * error. The caller is responsible for freeing the returned string. */
6011 char * WARN_UNUSED_RESULT
6012 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
6013 enum ofputil_protocol *usable_protocols)
6015 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true);
6019 ofpact_name(enum ofpact_type type)
6022 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
6029 /* Low-level action decoding and encoding functions. */
6031 /* Everything needed to identify a particular OpenFlow action. */
6032 struct ofpact_hdrs {
6033 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
6034 uint16_t type; /* Type if standard, otherwise subtype. */
6035 uint8_t ofp_version; /* From ofp_header. */
6038 /* Information about a particular OpenFlow action. */
6039 struct ofpact_raw_instance {
6040 /* The action's identity. */
6041 struct ofpact_hdrs hdrs;
6042 enum ofp_raw_action_type raw;
6044 /* Looking up the action. */
6045 struct hmap_node decode_node; /* Based on 'hdrs'. */
6046 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
6048 /* The action's encoded size.
6050 * If this action is fixed-length, 'min_length' == 'max_length'.
6051 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
6052 * OFP_ACTION_ALIGN) == 65528. */
6053 unsigned short int min_length;
6054 unsigned short int max_length;
6056 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
6057 * offset of that argument from the beginning of the action and 'arg_len'
6058 * its length, both in bytes.
6060 * For actions that take other forms, these are both zero. */
6061 unsigned short int arg_ofs;
6062 unsigned short int arg_len;
6064 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
6067 /* If this action is deprecated, a human-readable string with a brief
6069 const char *deprecation;
6072 /* Action header. */
6073 struct ofp_action_header {
6074 /* The meaning of other values of 'type' generally depends on the OpenFlow
6075 * version (see enum ofp_raw_action_type).
6077 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
6078 * designates an OpenFlow vendor ID and that the remainder of the action
6079 * structure has a vendor-defined meaning.
6081 #define OFPAT_VENDOR 0xffff
6084 /* Always a multiple of 8. */
6087 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
6088 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
6091 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
6093 /* Header for Nicira-defined actions. */
6094 struct nx_action_header {
6095 ovs_be16 type; /* OFPAT_VENDOR. */
6096 ovs_be16 len; /* At least 16. */
6097 ovs_be32 vendor; /* NX_VENDOR_ID. */
6098 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
6101 OFP_ASSERT(sizeof(struct nx_action_header) == 16);
6104 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
6105 const struct ofpact_hdrs *b)
6107 return (a->vendor == b->vendor
6108 && a->type == b->type
6109 && a->ofp_version == b->ofp_version);
6113 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
6115 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
6118 #include "ofp-actions.inc2"
6120 static struct hmap *
6121 ofpact_decode_hmap(void)
6123 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6124 static struct hmap hmap;
6126 if (ovsthread_once_start(&once)) {
6127 struct ofpact_raw_instance *inst;
6130 for (inst = all_raw_instances;
6131 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6133 hmap_insert(&hmap, &inst->decode_node,
6134 ofpact_hdrs_hash(&inst->hdrs));
6136 ovsthread_once_done(&once);
6141 static struct hmap *
6142 ofpact_encode_hmap(void)
6144 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6145 static struct hmap hmap;
6147 if (ovsthread_once_start(&once)) {
6148 struct ofpact_raw_instance *inst;
6151 for (inst = all_raw_instances;
6152 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
6154 hmap_insert(&hmap, &inst->encode_node,
6155 hash_2words(inst->raw, inst->hdrs.ofp_version));
6157 ovsthread_once_done(&once);
6163 ofpact_decode_raw(enum ofp_version ofp_version,
6164 const struct ofp_action_header *oah, size_t length,
6165 const struct ofpact_raw_instance **instp)
6167 const struct ofpact_raw_instance *inst;
6168 struct ofpact_hdrs hdrs;
6171 if (length < sizeof *oah) {
6172 return OFPERR_OFPBAC_BAD_LEN;
6175 /* Get base action type. */
6176 if (oah->type == htons(OFPAT_VENDOR)) {
6178 hdrs.vendor = ntohl(oah->vendor);
6179 if (hdrs.vendor == NX_VENDOR_ID) {
6180 /* Get Nicira action type. */
6181 const struct nx_action_header *nah;
6183 nah = ALIGNED_CAST(const struct nx_action_header *, oah);
6184 if (length < sizeof *nah) {
6185 return OFPERR_OFPBAC_BAD_LEN;
6187 hdrs.type = ntohs(nah->subtype);
6189 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
6191 return OFPERR_OFPBAC_BAD_VENDOR;
6195 hdrs.type = ntohs(oah->type);
6198 hdrs.ofp_version = ofp_version;
6199 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
6200 ofpact_decode_hmap()) {
6201 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
6208 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
6209 : OFPERR_OFPBAC_BAD_TYPE);
6213 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6214 enum ofp_raw_action_type *raw, uint64_t *arg)
6216 const struct ofp_action_header *oah = ofpbuf_data(buf);
6217 const struct ofpact_raw_instance *action;
6218 unsigned int length;
6222 error = ofpact_decode_raw(ofp_version, oah, ofpbuf_size(buf), &action);
6227 if (action->deprecation) {
6228 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
6229 action->name, ofputil_version_to_string(ofp_version),
6230 action->deprecation);
6233 length = ntohs(oah->len);
6234 if (length < action->min_length || length > action->max_length) {
6235 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
6236 "[%hu,%hu]", action->name, length,
6237 action->min_length, action->max_length);
6238 return OFPERR_OFPBAC_BAD_LEN;
6241 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
6242 "of 8", action->name, length);
6243 return OFPERR_OFPBAC_BAD_LEN;
6248 if (action->arg_len) {
6252 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
6253 for (i = 0; i < action->arg_len; i++) {
6254 *arg = (*arg << 8) | p[i];
6258 ofpbuf_pull(buf, length);
6263 static const struct ofpact_raw_instance *
6264 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
6266 const struct ofpact_raw_instance *inst;
6268 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
6269 ofpact_encode_hmap()) {
6270 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
6278 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
6279 enum ofp_raw_action_type raw, uint64_t arg)
6281 const struct ofpact_raw_instance *inst;
6282 struct ofp_action_header *oah;
6283 const struct ofpact_hdrs *hdrs;
6285 inst = ofpact_raw_lookup(ofp_version, raw);
6288 oah = ofpbuf_put_zeros(buf, inst->min_length);
6289 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
6290 oah->len = htons(inst->min_length);
6291 oah->vendor = htonl(hdrs->vendor);
6293 switch (hdrs->vendor) {
6297 case NX_VENDOR_ID: {
6298 struct nx_action_header *nah = (struct nx_action_header *) oah;
6299 nah->subtype = htons(hdrs->type);
6307 if (inst->arg_len) {
6308 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
6311 for (i = 0; i < inst->arg_len; i++) {
6323 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
6325 struct ofp_action_header *oah;
6327 ofpbuf_put_zeros(openflow, PAD_SIZE(ofpbuf_size(openflow) - start_ofs, 8));
6329 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
6330 oah->len = htons(ofpbuf_size(openflow) - start_ofs);