2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <netinet/in.h>
20 #include "ofp-actions.h"
22 #include "byte-order.h"
25 #include "dynamic-string.h"
28 #include "meta-flow.h"
29 #include "multipath.h"
31 #include "ofp-parse.h"
34 #include "unaligned.h"
36 #include "openvswitch/vlog.h"
38 VLOG_DEFINE_THIS_MODULE(ofp_actions);
40 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
42 struct ofp_action_header;
44 /* Raw identifiers for OpenFlow actions.
46 * Decoding and encoding OpenFlow actions across multiple versions is difficult
47 * to do in a clean, consistent way. This enumeration lays out all of the
48 * forms of actions that Open vSwitch supports.
50 * The comments here must follow a stylized form because the
51 * "extract-ofp-actions" program parses them at build time to generate data
54 * - The first part of each comment specifies the vendor, OpenFlow versions,
55 * and type for each protocol that supports the action:
57 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
58 * extension actions. (Support for other vendors can be added, but
59 * it can't be done just based on a vendor ID definition alone
60 * because OpenFlow doesn't define a standard way to specify a
61 * subtype for vendor actions, so other vendors might do it different
64 * # The version can specify a specific OpenFlow version, a version
65 * range delimited by "-", or an open-ended range with "+".
67 * # The type, in parentheses, is the action type number (for standard
68 * OpenFlow actions) or subtype (for vendor extension actions).
70 * # Optionally one may add "is deprecated" followed by a
71 * human-readable reason in parentheses (which will be used in log
72 * messages), if a particular action should no longer be used.
74 * Multiple such specifications may be separated by commas.
76 * - The second part describes the action's wire format. It may be:
78 * # "struct <name>": The struct fully specifies the wire format. The
79 * action is exactly the size of the struct. (Thus, the struct must
80 * be an exact multiple of 8 bytes in size.)
82 * # "struct <name>, ...": The struct specifies the beginning of the
83 * wire format. An instance of the action is either the struct's
84 * exact size, or a multiple of 8 bytes longer.
86 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
87 * vendor extension) header, followed by 0 or more pad bytes to align
88 * to a multiple of <N> bits, followed by an argument of the given
89 * type, followed by 0 or more pad bytes to bring the total action up
90 * to a multiple of 8 bytes.
92 * # "void": The action is just a (standard or vendor extension)
95 * - Optional additional text enclosed in square brackets is commentary for
98 enum ofp_raw_action_type {
99 /* ## ----------------- ## */
100 /* ## Standard actions. ## */
101 /* ## ----------------- ## */
103 /* OF1.0(0): struct ofp10_action_output. */
105 /* OF1.1+(0): struct ofp11_action_output. */
108 /* OF1.0(1): uint16_t. */
109 OFPAT_RAW10_SET_VLAN_VID,
110 /* OF1.0(2): uint8_t. */
111 OFPAT_RAW10_SET_VLAN_PCP,
113 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
115 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
116 * modification actions: the 1.0 versions push a VLAN header if none is
117 * present, but the 1.1 versions do not. That is the only reason that we
118 * distinguish their raw action types.] */
119 OFPAT_RAW11_SET_VLAN_VID,
120 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
121 OFPAT_RAW11_SET_VLAN_PCP,
123 /* OF1.1+(17): ovs_be16.
125 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
127 OFPAT_RAW11_PUSH_VLAN,
129 /* OF1.0(3): void. */
130 OFPAT_RAW10_STRIP_VLAN,
131 /* OF1.1+(18): void. */
132 OFPAT_RAW11_POP_VLAN,
134 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
135 * ofp_action_dl_addr. */
136 OFPAT_RAW_SET_DL_SRC,
138 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
139 * ofp_action_dl_addr. */
140 OFPAT_RAW_SET_DL_DST,
142 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
144 OFPAT_RAW_SET_NW_SRC,
146 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
148 OFPAT_RAW_SET_NW_DST,
150 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
151 OFPAT_RAW_SET_NW_TOS,
153 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
154 OFPAT_RAW11_SET_NW_ECN,
156 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
158 OFPAT_RAW_SET_TP_SRC,
160 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
162 OFPAT_RAW_SET_TP_DST,
164 /* OF1.0(11): struct ofp10_action_enqueue. */
167 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
169 OFPAT_RAW_SET_MPLS_LABEL,
171 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
173 OFPAT_RAW_SET_MPLS_TC,
175 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
177 OFPAT_RAW_SET_MPLS_TTL,
179 /* NX1.0(26), OF1.1+(16): void. */
180 OFPAT_RAW_DEC_MPLS_TTL,
182 /* NX1.0(23), OF1.1+(19): ovs_be16.
184 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
187 /* NX1.0(24), OF1.1+(20): ovs_be16.
189 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
190 * ETH_TYPE_MPLS otherwise, not the label.] */
193 /* NX1.0(4), OF1.1+(21): uint32_t. */
196 /* OF1.1+(22): uint32_t. */
199 /* OF1.1+(23): uint8_t. */
200 OFPAT_RAW11_SET_NW_TTL,
202 /* NX1.0(18), OF1.1+(24): void. */
203 OFPAT_RAW_DEC_NW_TTL,
204 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
205 NXAST_RAW_DEC_TTL_CNT_IDS,
207 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
208 OFPAT_RAW12_SET_FIELD,
209 /* OF1.5+(25): struct ofp12_action_set_field, ... */
210 OFPAT_RAW15_SET_FIELD,
211 /* NX1.0-1.4(7): struct nx_action_reg_load.
213 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
214 * we drop reg_load.] */
216 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
218 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
219 * we drop reg_load2.] */
222 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
223 OFPAT_RAW15_COPY_FIELD,
224 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
225 ONFACT_RAW13_COPY_FIELD,
226 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
229 /* ## ------------------------- ## */
230 /* ## Nicira extension actions. ## */
231 /* ## ------------------------- ## */
233 /* Actions similar to standard actions are listed with the standard actions. */
235 /* NX1.0+(1): uint16_t. */
237 /* NX1.0+(14): struct nx_action_resubmit. */
238 NXAST_RAW_RESUBMIT_TABLE,
240 /* NX1.0+(2): uint32_t. */
241 NXAST_RAW_SET_TUNNEL,
242 /* NX1.0+(9): uint64_t. */
243 NXAST_RAW_SET_TUNNEL64,
245 /* NX1.0+(5): void. */
248 /* NX1.0+(8): struct nx_action_note, ... */
251 /* NX1.0+(10): struct nx_action_multipath. */
254 /* NX1.0+(12): struct nx_action_bundle, ... */
256 /* NX1.0+(13): struct nx_action_bundle, ... */
257 NXAST_RAW_BUNDLE_LOAD,
259 /* NX1.0+(15): struct nx_action_output_reg. */
260 NXAST_RAW_OUTPUT_REG,
261 /* NX1.0+(32): struct nx_action_output_reg2. */
262 NXAST_RAW_OUTPUT_REG2,
264 /* NX1.0+(16): struct nx_action_learn, ... */
267 /* NX1.0+(17): void. */
270 /* NX1.0+(19): struct nx_action_fin_timeout. */
271 NXAST_RAW_FIN_TIMEOUT,
273 /* NX1.0+(20): struct nx_action_controller. */
274 NXAST_RAW_CONTROLLER,
276 /* NX1.0+(22): struct nx_action_write_metadata. */
277 NXAST_RAW_WRITE_METADATA,
279 /* NX1.0+(27): struct nx_action_stack. */
280 NXAST_RAW_STACK_PUSH,
282 /* NX1.0+(28): struct nx_action_stack. */
285 /* NX1.0+(29): struct nx_action_sample. */
288 /* NX1.0+(34): struct nx_action_conjunction. */
289 NXAST_RAW_CONJUNCTION,
291 /* NX1.0+(35): struct nx_action_conntrack, ... */
294 /* ## ------------------ ## */
295 /* ## Debugging actions. ## */
296 /* ## ------------------ ## */
298 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
299 /* accepts them only if started with --enable-dummy. */
301 /* NX1.0+(255): void. */
302 NXAST_RAW_DEBUG_RECIRC,
305 /* OpenFlow actions are always a multiple of 8 bytes in length. */
306 #define OFP_ACTION_ALIGN 8
308 /* Define a few functions for working with instructions. */
309 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
310 static inline const struct STRUCT * OVS_UNUSED \
311 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
313 ovs_assert(inst->type == htons(ENUM)); \
314 return ALIGNED_CAST(struct STRUCT *, inst); \
317 static inline void OVS_UNUSED \
318 instruction_init_##ENUM(struct STRUCT *s) \
320 memset(s, 0, sizeof *s); \
321 s->type = htons(ENUM); \
322 s->len = htons(sizeof *s); \
325 static inline struct STRUCT * OVS_UNUSED \
326 instruction_put_##ENUM(struct ofpbuf *buf) \
328 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
329 instruction_init_##ENUM(s); \
335 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
337 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
339 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
340 uint32_t allowed_ovsinsts,
341 enum ofpact_type outer_action);
343 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
344 enum mf_field_id, uint64_t value);
346 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
347 enum ofp_raw_action_type *, uint64_t *arg);
348 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
349 enum ofp_raw_action_type, uint64_t arg);
351 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
352 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
353 bool allow_instructions, enum ofpact_type outer_action);
354 static enum ofperr ofpacts_pull_openflow_actions__(
355 struct ofpbuf *openflow, unsigned int actions_len,
356 enum ofp_version version, uint32_t allowed_ovsinsts,
357 struct ofpbuf *ofpacts, enum ofpact_type outer_action);
358 static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
359 const char *s_, struct ofpbuf *ofpacts,
360 enum ofputil_protocol *usable_protocols,
361 bool allow_instructions, enum ofpact_type outer_action);
363 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
364 * nested ofpacts it returns the first one. */
366 ofpact_next_flattened(const struct ofpact *ofpact)
368 switch (ofpact->type) {
371 case OFPACT_CONTROLLER:
373 case OFPACT_OUTPUT_REG:
375 case OFPACT_SET_FIELD:
376 case OFPACT_SET_VLAN_VID:
377 case OFPACT_SET_VLAN_PCP:
378 case OFPACT_STRIP_VLAN:
379 case OFPACT_PUSH_VLAN:
380 case OFPACT_SET_ETH_SRC:
381 case OFPACT_SET_ETH_DST:
382 case OFPACT_SET_IPV4_SRC:
383 case OFPACT_SET_IPV4_DST:
384 case OFPACT_SET_IP_DSCP:
385 case OFPACT_SET_IP_ECN:
386 case OFPACT_SET_IP_TTL:
387 case OFPACT_SET_L4_SRC_PORT:
388 case OFPACT_SET_L4_DST_PORT:
389 case OFPACT_REG_MOVE:
390 case OFPACT_STACK_PUSH:
391 case OFPACT_STACK_POP:
393 case OFPACT_SET_MPLS_LABEL:
394 case OFPACT_SET_MPLS_TC:
395 case OFPACT_SET_MPLS_TTL:
396 case OFPACT_DEC_MPLS_TTL:
397 case OFPACT_PUSH_MPLS:
398 case OFPACT_POP_MPLS:
399 case OFPACT_SET_TUNNEL:
400 case OFPACT_SET_QUEUE:
401 case OFPACT_POP_QUEUE:
402 case OFPACT_FIN_TIMEOUT:
403 case OFPACT_RESUBMIT:
405 case OFPACT_CONJUNCTION:
406 case OFPACT_MULTIPATH:
410 case OFPACT_UNROLL_XLATE:
411 case OFPACT_DEBUG_RECIRC:
413 case OFPACT_CLEAR_ACTIONS:
414 case OFPACT_WRITE_METADATA:
415 case OFPACT_GOTO_TABLE:
416 return ofpact_next(ofpact);
419 return ofpact_get_CT(ofpact)->actions;
421 case OFPACT_WRITE_ACTIONS:
422 return ofpact_get_WRITE_ACTIONS(ofpact)->actions;
428 /* Pull off existing actions or instructions. Used by nesting actions to keep
429 * ofpacts_parse() oblivious of actions nesting.
431 * Push the actions back on after nested parsing, e.g.:
433 * size_t ofs = ofpacts_pull(ofpacts);
434 * ...nested parsing...
435 * ofpbuf_push_uninit(ofpacts, ofs);
438 ofpacts_pull(struct ofpbuf *ofpacts)
444 ofpbuf_pull(ofpacts, ofs);
449 #include "ofp-actions.inc1"
451 /* Output actions. */
453 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
454 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
455 * number of bytes to send. A 'max_len' of zero means no bytes of the
456 * packet should be sent. */
457 struct ofp10_action_output {
458 ovs_be16 type; /* OFPAT10_OUTPUT. */
459 ovs_be16 len; /* Length is 8. */
460 ovs_be16 port; /* Output port. */
461 ovs_be16 max_len; /* Max length to send to controller. */
463 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
465 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
466 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
467 * number of bytes to send. A 'max_len' of zero means no bytes of the
468 * packet should be sent.*/
469 struct ofp11_action_output {
470 ovs_be16 type; /* OFPAT11_OUTPUT. */
471 ovs_be16 len; /* Length is 16. */
472 ovs_be32 port; /* Output port. */
473 ovs_be16 max_len; /* Max length to send to controller. */
474 uint8_t pad[6]; /* Pad to 64 bits. */
476 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
479 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
480 enum ofp_version ofp_version OVS_UNUSED,
483 struct ofpact_output *output;
485 output = ofpact_put_OUTPUT(out);
486 output->port = u16_to_ofp(ntohs(oao->port));
487 output->max_len = ntohs(oao->max_len);
489 return ofpact_check_output_port(output->port, OFPP_MAX);
493 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
494 enum ofp_version ofp_version OVS_UNUSED,
497 struct ofpact_output *output;
500 output = ofpact_put_OUTPUT(out);
501 output->max_len = ntohs(oao->max_len);
503 error = ofputil_port_from_ofp11(oao->port, &output->port);
508 return ofpact_check_output_port(output->port, OFPP_MAX);
512 encode_OUTPUT(const struct ofpact_output *output,
513 enum ofp_version ofp_version, struct ofpbuf *out)
515 if (ofp_version == OFP10_VERSION) {
516 struct ofp10_action_output *oao;
518 oao = put_OFPAT10_OUTPUT(out);
519 oao->port = htons(ofp_to_u16(output->port));
520 oao->max_len = htons(output->max_len);
522 struct ofp11_action_output *oao;
524 oao = put_OFPAT11_OUTPUT(out);
525 oao->port = ofputil_port_to_ofp11(output->port);
526 oao->max_len = htons(output->max_len);
530 static char * OVS_WARN_UNUSED_RESULT
531 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
532 enum ofputil_protocol *usable_protocols OVS_UNUSED)
534 if (strchr(arg, '[')) {
535 struct ofpact_output_reg *output_reg;
537 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
538 output_reg->max_len = UINT16_MAX;
539 return mf_parse_subfield(&output_reg->src, arg);
541 struct ofpact_output *output;
543 output = ofpact_put_OUTPUT(ofpacts);
544 if (!ofputil_port_from_string(arg, &output->port)) {
545 return xasprintf("%s: output to unknown port", arg);
547 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
553 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
555 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
556 ds_put_format(s, "output:%"PRIu16, a->port);
558 ofputil_format_port(a->port, s);
559 if (a->port == OFPP_CONTROLLER) {
560 ds_put_format(s, ":%"PRIu16, a->max_len);
568 decode_OFPAT_RAW11_GROUP(uint32_t group_id,
569 enum ofp_version ofp_version OVS_UNUSED,
572 ofpact_put_GROUP(out)->group_id = group_id;
577 encode_GROUP(const struct ofpact_group *group,
578 enum ofp_version ofp_version, struct ofpbuf *out)
580 if (ofp_version == OFP10_VERSION) {
583 put_OFPAT11_GROUP(out, group->group_id);
587 static char * OVS_WARN_UNUSED_RESULT
588 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
589 enum ofputil_protocol *usable_protocols OVS_UNUSED)
591 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
595 format_GROUP(const struct ofpact_group *a, struct ds *s)
597 ds_put_format(s, "group:%"PRIu32, a->group_id);
600 /* Action structure for NXAST_CONTROLLER.
602 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
603 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
606 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
608 * - 'controller_id': The ID of the controller connection to which the
609 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
610 * sent only to controllers that have the specified controller connection
611 * ID. See "struct nx_controller_id" for more information. */
612 struct nx_action_controller {
613 ovs_be16 type; /* OFPAT_VENDOR. */
614 ovs_be16 len; /* Length is 16. */
615 ovs_be32 vendor; /* NX_VENDOR_ID. */
616 ovs_be16 subtype; /* NXAST_CONTROLLER. */
617 ovs_be16 max_len; /* Maximum length to send to controller. */
618 ovs_be16 controller_id; /* Controller ID to send packet-in. */
619 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
620 uint8_t zero; /* Must be zero. */
622 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
625 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
626 enum ofp_version ofp_version OVS_UNUSED,
629 struct ofpact_controller *oc;
631 oc = ofpact_put_CONTROLLER(out);
632 oc->max_len = ntohs(nac->max_len);
633 oc->controller_id = ntohs(nac->controller_id);
634 oc->reason = nac->reason;
639 encode_CONTROLLER(const struct ofpact_controller *controller,
640 enum ofp_version ofp_version OVS_UNUSED,
643 struct nx_action_controller *nac;
645 nac = put_NXAST_CONTROLLER(out);
646 nac->max_len = htons(controller->max_len);
647 nac->controller_id = htons(controller->controller_id);
648 nac->reason = controller->reason;
651 static char * OVS_WARN_UNUSED_RESULT
652 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
653 enum ofputil_protocol *usable_protocols OVS_UNUSED)
655 enum ofp_packet_in_reason reason = OFPR_ACTION;
656 uint16_t controller_id = 0;
657 uint16_t max_len = UINT16_MAX;
661 } else if (strspn(arg, "0123456789") == strlen(arg)) {
662 char *error = str_to_u16(arg, "max_len", &max_len);
669 while (ofputil_parse_key_value(&arg, &name, &value)) {
670 if (!strcmp(name, "reason")) {
671 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
672 return xasprintf("unknown reason \"%s\"", value);
674 } else if (!strcmp(name, "max_len")) {
675 char *error = str_to_u16(value, "max_len", &max_len);
679 } else if (!strcmp(name, "id")) {
680 char *error = str_to_u16(value, "id", &controller_id);
685 return xasprintf("unknown key \"%s\" parsing controller "
691 if (reason == OFPR_ACTION && controller_id == 0) {
692 struct ofpact_output *output;
694 output = ofpact_put_OUTPUT(ofpacts);
695 output->port = OFPP_CONTROLLER;
696 output->max_len = max_len;
698 struct ofpact_controller *controller;
700 controller = ofpact_put_CONTROLLER(ofpacts);
701 controller->max_len = max_len;
702 controller->reason = reason;
703 controller->controller_id = controller_id;
710 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
712 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
713 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
715 enum ofp_packet_in_reason reason = a->reason;
717 ds_put_cstr(s, "controller(");
718 if (reason != OFPR_ACTION) {
719 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
721 ds_put_format(s, "reason=%s,",
722 ofputil_packet_in_reason_to_string(
723 reason, reasonbuf, sizeof reasonbuf));
725 if (a->max_len != UINT16_MAX) {
726 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
728 if (a->controller_id != 0) {
729 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
736 /* Enqueue action. */
737 struct ofp10_action_enqueue {
738 ovs_be16 type; /* OFPAT10_ENQUEUE. */
739 ovs_be16 len; /* Len is 16. */
740 ovs_be16 port; /* Port that queue belongs. Should
741 refer to a valid physical port
742 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
743 uint8_t pad[6]; /* Pad for 64-bit alignment. */
744 ovs_be32 queue_id; /* Where to enqueue the packets. */
746 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
749 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
750 enum ofp_version ofp_version OVS_UNUSED,
753 struct ofpact_enqueue *enqueue;
755 enqueue = ofpact_put_ENQUEUE(out);
756 enqueue->port = u16_to_ofp(ntohs(oae->port));
757 enqueue->queue = ntohl(oae->queue_id);
758 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
759 && enqueue->port != OFPP_IN_PORT
760 && enqueue->port != OFPP_LOCAL) {
761 return OFPERR_OFPBAC_BAD_OUT_PORT;
767 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
768 enum ofp_version ofp_version, struct ofpbuf *out)
770 if (ofp_version == OFP10_VERSION) {
771 struct ofp10_action_enqueue *oae;
773 oae = put_OFPAT10_ENQUEUE(out);
774 oae->port = htons(ofp_to_u16(enqueue->port));
775 oae->queue_id = htonl(enqueue->queue);
781 static char * OVS_WARN_UNUSED_RESULT
782 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
783 enum ofputil_protocol *usable_protocols OVS_UNUSED)
786 char *port = strtok_r(arg, ":q,", &sp);
787 char *queue = strtok_r(NULL, "", &sp);
788 struct ofpact_enqueue *enqueue;
790 if (port == NULL || queue == NULL) {
791 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
792 "\"enqueue(PORT,QUEUE)\"");
795 enqueue = ofpact_put_ENQUEUE(ofpacts);
796 if (!ofputil_port_from_string(port, &enqueue->port)) {
797 return xasprintf("%s: enqueue to unknown port", port);
799 return str_to_u32(queue, &enqueue->queue);
803 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
805 ds_put_format(s, "enqueue:");
806 ofputil_format_port(a->port, s);
807 ds_put_format(s, ":%"PRIu32, a->queue);
810 /* Action structure for NXAST_OUTPUT_REG.
812 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
814 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
815 * the NXAST_REG_LOAD action.
817 * The acceptable nxm_header values for 'src' are the same as the acceptable
818 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
820 * The 'max_len' field indicates the number of bytes to send when the chosen
821 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
822 * field of OFPAT_OUTPUT.
824 * The 'zero' field is required to be zeroed for forward compatibility. */
825 struct nx_action_output_reg {
826 ovs_be16 type; /* OFPAT_VENDOR. */
827 ovs_be16 len; /* 24. */
828 ovs_be32 vendor; /* NX_VENDOR_ID. */
829 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
831 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
832 ovs_be32 src; /* Source. */
834 ovs_be16 max_len; /* Max length to send to controller. */
836 uint8_t zero[6]; /* Reserved, must be zero. */
838 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
840 /* Action structure for NXAST_OUTPUT_REG2.
842 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
843 * experimenter OXM as 'src'.
845 struct nx_action_output_reg2 {
846 ovs_be16 type; /* OFPAT_VENDOR. */
847 ovs_be16 len; /* 24. */
848 ovs_be32 vendor; /* NX_VENDOR_ID. */
849 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
851 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
852 ovs_be16 max_len; /* Max length to send to controller. */
855 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
856 * - Enough 0-bytes to pad the action out to 24 bytes. */
859 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
862 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
863 enum ofp_version ofp_version OVS_UNUSED,
866 struct ofpact_output_reg *output_reg;
868 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
869 return OFPERR_OFPBAC_BAD_ARGUMENT;
872 output_reg = ofpact_put_OUTPUT_REG(out);
873 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
874 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
875 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
876 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
877 output_reg->max_len = ntohs(naor->max_len);
879 return mf_check_src(&output_reg->src, NULL);
883 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
884 enum ofp_version ofp_version OVS_UNUSED,
887 struct ofpact_output_reg *output_reg;
891 output_reg = ofpact_put_OUTPUT_REG(out);
892 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
893 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
894 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
895 output_reg->max_len = ntohs(naor->max_len);
897 ofpbuf_use_const(&b, naor, ntohs(naor->len));
898 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
899 error = nx_pull_header(&b, &output_reg->src.field, NULL);
903 if (!is_all_zeros(b.data, b.size)) {
904 return OFPERR_NXBRC_MUST_BE_ZERO;
907 return mf_check_src(&output_reg->src, NULL);
911 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
912 enum ofp_version ofp_version OVS_UNUSED,
915 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
916 * cannot be encoded in the older form, encode it as
917 * NXAST_RAW_OUTPUT_REG2. */
918 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
919 || !mf_nxm_header(output_reg->src.field->id)) {
920 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
921 size_t size = out->size;
923 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
924 output_reg->src.n_bits);
925 naor->max_len = htons(output_reg->max_len);
927 out->size = size - sizeof naor->pad;
928 nx_put_header(out, output_reg->src.field->id, 0, false);
931 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
933 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
934 output_reg->src.n_bits);
935 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
936 naor->max_len = htons(output_reg->max_len);
940 static char * OVS_WARN_UNUSED_RESULT
941 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
942 enum ofputil_protocol *usable_protocols OVS_UNUSED)
944 return parse_OUTPUT(arg, ofpacts, usable_protocols);
948 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
950 ds_put_cstr(s, "output:");
951 mf_format_subfield(&a->src, s);
954 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
956 * The bundle actions choose a slave from a supplied list of options.
957 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
958 * selection to a register.
960 * The list of possible slaves follows the nx_action_bundle structure. The size
961 * of each slave is governed by its type as indicated by the 'slave_type'
962 * parameter. The list of slaves should be padded at its end with zeros to make
963 * the total length of the action a multiple of 8.
965 * Switches infer from the 'slave_type' parameter the size of each slave. All
966 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
967 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
968 * 2 byte width. Switches should reject actions which indicate unknown or
969 * unsupported slave types.
971 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
972 * slave. If the switch does not support the specified 'algorithm' parameter,
973 * it should reject the action.
975 * Several algorithms take into account liveness when selecting slaves. The
976 * liveness of a slave is implementation defined (with one exception), but will
977 * generally take into account things like its carrier status and the results
978 * of any link monitoring protocols which happen to be running on it. In order
979 * to give controllers a place-holder value, the OFPP_NONE port is always
982 * Some slave selection strategies require the use of a hash function, in which
983 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
984 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
985 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
986 * 'basis' parameter is used as a universal hash parameter. Different values
987 * of 'basis' yield different hash results.
989 * The 'zero' parameter at the end of the action structure is reserved for
990 * future use. Switches are required to reject actions which have nonzero
991 * bytes in the 'zero' field.
993 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
994 * should reject actions which have nonzero bytes in either of these fields.
996 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
997 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
998 * similar to those for the NXAST_REG_LOAD action. */
999 struct nx_action_bundle {
1000 ovs_be16 type; /* OFPAT_VENDOR. */
1001 ovs_be16 len; /* Length including slaves. */
1002 ovs_be32 vendor; /* NX_VENDOR_ID. */
1003 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1005 /* Slave choice algorithm to apply to hash value. */
1006 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
1008 /* What fields to hash and how. */
1009 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
1010 ovs_be16 basis; /* Universal hash parameter. */
1012 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
1013 ovs_be16 n_slaves; /* Number of slaves. */
1015 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1016 ovs_be32 dst; /* Destination. */
1018 uint8_t zero[4]; /* Reserved. Must be zero. */
1020 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
1023 decode_bundle(bool load, const struct nx_action_bundle *nab,
1024 struct ofpbuf *ofpacts)
1026 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1027 struct ofpact_bundle *bundle;
1028 uint32_t slave_type;
1029 size_t slaves_size, i;
1032 bundle = ofpact_put_BUNDLE(ofpacts);
1034 bundle->n_slaves = ntohs(nab->n_slaves);
1035 bundle->basis = ntohs(nab->basis);
1036 bundle->fields = ntohs(nab->fields);
1037 bundle->algorithm = ntohs(nab->algorithm);
1038 slave_type = ntohl(nab->slave_type);
1039 slaves_size = ntohs(nab->len) - sizeof *nab;
1041 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1042 if (!flow_hash_fields_valid(bundle->fields)) {
1043 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
1044 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
1045 VLOG_WARN_RL(&rl, "too many slaves");
1046 } else if (bundle->algorithm != NX_BD_ALG_HRW
1047 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
1048 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
1049 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
1050 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
1055 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
1056 VLOG_WARN_RL(&rl, "reserved field is nonzero");
1057 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1061 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
1062 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
1063 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
1065 if (bundle->dst.n_bits < 16) {
1066 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
1068 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1071 if (nab->ofs_nbits || nab->dst) {
1072 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
1073 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1077 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1078 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1079 "allocated for slaves. %"PRIuSIZE" bytes are required "
1080 "for %"PRIu16" slaves.",
1081 load ? "bundle_load" : "bundle", slaves_size,
1082 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1083 error = OFPERR_OFPBAC_BAD_LEN;
1086 for (i = 0; i < bundle->n_slaves; i++) {
1087 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1088 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1091 bundle = ofpacts->header;
1092 ofpact_update_len(ofpacts, &bundle->ofpact);
1095 error = bundle_check(bundle, OFPP_MAX, NULL);
1101 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1102 enum ofp_version ofp_version OVS_UNUSED,
1105 return decode_bundle(false, nab, out);
1109 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1110 enum ofp_version ofp_version OVS_UNUSED,
1113 return decode_bundle(true, nab, out);
1117 encode_BUNDLE(const struct ofpact_bundle *bundle,
1118 enum ofp_version ofp_version OVS_UNUSED,
1121 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1122 struct nx_action_bundle *nab;
1126 nab = (bundle->dst.field
1127 ? put_NXAST_BUNDLE_LOAD(out)
1128 : put_NXAST_BUNDLE(out));
1129 nab->len = htons(ntohs(nab->len) + slaves_len);
1130 nab->algorithm = htons(bundle->algorithm);
1131 nab->fields = htons(bundle->fields);
1132 nab->basis = htons(bundle->basis);
1133 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1134 nab->n_slaves = htons(bundle->n_slaves);
1135 if (bundle->dst.field) {
1136 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1137 bundle->dst.n_bits);
1138 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1141 slaves = ofpbuf_put_zeros(out, slaves_len);
1142 for (i = 0; i < bundle->n_slaves; i++) {
1143 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1147 static char * OVS_WARN_UNUSED_RESULT
1148 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1149 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1151 return bundle_parse(arg, ofpacts);
1154 static char * OVS_WARN_UNUSED_RESULT
1155 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1157 return bundle_parse_load(arg, ofpacts);
1161 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1163 bundle_format(a, s);
1166 /* Set VLAN actions. */
1169 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1172 return OFPERR_OFPBAC_BAD_ARGUMENT;
1174 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1175 vlan_vid->vlan_vid = vid;
1176 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1182 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1183 enum ofp_version ofp_version OVS_UNUSED,
1186 return decode_set_vlan_vid(vid, true, out);
1190 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1191 enum ofp_version ofp_version OVS_UNUSED,
1194 return decode_set_vlan_vid(vid, false, out);
1198 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1199 enum ofp_version ofp_version, struct ofpbuf *out)
1201 uint16_t vid = vlan_vid->vlan_vid;
1203 /* Push a VLAN tag, if none is present and this form of the action calls
1204 * for such a feature. */
1205 if (ofp_version > OFP10_VERSION
1206 && vlan_vid->push_vlan_if_needed
1207 && !vlan_vid->flow_has_vlan) {
1208 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1211 if (ofp_version == OFP10_VERSION) {
1212 put_OFPAT10_SET_VLAN_VID(out, vid);
1213 } else if (ofp_version == OFP11_VERSION) {
1214 put_OFPAT11_SET_VLAN_VID(out, vid);
1216 ofpact_put_set_field(out, ofp_version,
1217 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1221 static char * OVS_WARN_UNUSED_RESULT
1222 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1224 struct ofpact_vlan_vid *vlan_vid;
1228 error = str_to_u16(arg, "VLAN VID", &vid);
1233 if (vid & ~VLAN_VID_MASK) {
1234 return xasprintf("%s: not a valid VLAN VID", arg);
1236 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1237 vlan_vid->vlan_vid = vid;
1238 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1242 static char * OVS_WARN_UNUSED_RESULT
1243 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1244 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1246 return parse_set_vlan_vid(arg, ofpacts, false);
1250 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1252 ds_put_format(s, "%s:%"PRIu16,
1253 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1257 /* Set PCP actions. */
1260 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1263 return OFPERR_OFPBAC_BAD_ARGUMENT;
1265 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1266 vlan_pcp->vlan_pcp = pcp;
1267 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1273 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1274 enum ofp_version ofp_version OVS_UNUSED,
1277 return decode_set_vlan_pcp(pcp, true, out);
1281 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1282 enum ofp_version ofp_version OVS_UNUSED,
1285 return decode_set_vlan_pcp(pcp, false, out);
1289 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1290 enum ofp_version ofp_version, struct ofpbuf *out)
1292 uint8_t pcp = vlan_pcp->vlan_pcp;
1294 /* Push a VLAN tag, if none is present and this form of the action calls
1295 * for such a feature. */
1296 if (ofp_version > OFP10_VERSION
1297 && vlan_pcp->push_vlan_if_needed
1298 && !vlan_pcp->flow_has_vlan) {
1299 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1302 if (ofp_version == OFP10_VERSION) {
1303 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1304 } else if (ofp_version == OFP11_VERSION) {
1305 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1307 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1311 static char * OVS_WARN_UNUSED_RESULT
1312 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1314 struct ofpact_vlan_pcp *vlan_pcp;
1318 error = str_to_u8(arg, "VLAN PCP", &pcp);
1324 return xasprintf("%s: not a valid VLAN PCP", arg);
1326 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1327 vlan_pcp->vlan_pcp = pcp;
1328 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1332 static char * OVS_WARN_UNUSED_RESULT
1333 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1334 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1336 return parse_set_vlan_pcp(arg, ofpacts, false);
1340 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1342 ds_put_format(s, "%s:%"PRIu8,
1343 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1347 /* Strip VLAN actions. */
1350 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1352 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1357 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1359 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1364 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1365 enum ofp_version ofp_version, struct ofpbuf *out)
1367 if (ofp_version == OFP10_VERSION) {
1368 put_OFPAT10_STRIP_VLAN(out);
1370 put_OFPAT11_POP_VLAN(out);
1374 static char * OVS_WARN_UNUSED_RESULT
1375 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1376 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1378 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1382 static char * OVS_WARN_UNUSED_RESULT
1383 parse_pop_vlan(struct ofpbuf *ofpacts)
1385 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1390 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1392 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1397 /* Push VLAN action. */
1400 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1401 enum ofp_version ofp_version OVS_UNUSED,
1404 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1405 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1406 return OFPERR_OFPBAC_BAD_ARGUMENT;
1408 ofpact_put_PUSH_VLAN(out);
1413 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1414 enum ofp_version ofp_version, struct ofpbuf *out)
1416 if (ofp_version == OFP10_VERSION) {
1417 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1418 * follow this action. */
1420 /* XXX ETH_TYPE_VLAN_8021AD case */
1421 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1425 static char * OVS_WARN_UNUSED_RESULT
1426 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1427 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1432 *usable_protocols &= OFPUTIL_P_OF11_UP;
1433 error = str_to_u16(arg, "ethertype", ðertype);
1438 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1439 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1440 return xasprintf("%s: not a valid VLAN ethertype", arg);
1443 ofpact_put_PUSH_VLAN(ofpacts);
1448 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1450 /* XXX 802.1AD case*/
1451 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1454 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1455 struct ofp_action_dl_addr {
1456 ovs_be16 type; /* Type. */
1457 ovs_be16 len; /* Length is 16. */
1458 struct eth_addr dl_addr; /* Ethernet address. */
1461 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1464 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1465 enum ofp_version ofp_version OVS_UNUSED,
1468 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1473 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1474 enum ofp_version ofp_version OVS_UNUSED,
1477 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1482 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1483 enum ofp_raw_action_type raw, enum mf_field_id field,
1486 if (ofp_version < OFP12_VERSION) {
1487 struct ofp_action_dl_addr *oada;
1489 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1490 oada->dl_addr = mac->mac;
1492 ofpact_put_set_field(out, ofp_version, field,
1493 eth_addr_to_uint64(mac->mac));
1498 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1501 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1507 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1508 enum ofp_version ofp_version,
1511 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1516 static char * OVS_WARN_UNUSED_RESULT
1517 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1518 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1520 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1523 static char * OVS_WARN_UNUSED_RESULT
1524 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1525 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1527 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1531 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1533 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1537 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1539 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1542 /* Set IPv4 address actions. */
1545 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1546 enum ofp_version ofp_version OVS_UNUSED,
1549 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1554 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1555 enum ofp_version ofp_version OVS_UNUSED,
1558 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1563 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1564 enum ofp_version ofp_version,
1565 enum ofp_raw_action_type raw, enum mf_field_id field,
1568 ovs_be32 addr = ipv4->ipv4;
1569 if (ofp_version < OFP12_VERSION) {
1570 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1572 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1577 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1578 enum ofp_version ofp_version, struct ofpbuf *out)
1580 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1585 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1586 enum ofp_version ofp_version, struct ofpbuf *out)
1588 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1592 static char * OVS_WARN_UNUSED_RESULT
1593 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1594 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1596 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1599 static char * OVS_WARN_UNUSED_RESULT
1600 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1601 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1603 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1607 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1609 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1613 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1615 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1618 /* Set IPv4/v6 TOS actions. */
1621 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1622 enum ofp_version ofp_version OVS_UNUSED,
1625 if (dscp & ~IP_DSCP_MASK) {
1626 return OFPERR_OFPBAC_BAD_ARGUMENT;
1628 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1634 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1635 enum ofp_version ofp_version, struct ofpbuf *out)
1637 if (ofp_version < OFP12_VERSION) {
1638 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1640 ofpact_put_set_field(out, ofp_version,
1641 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1645 static char * OVS_WARN_UNUSED_RESULT
1646 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1647 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1652 error = str_to_u8(arg, "TOS", &tos);
1657 if (tos & ~IP_DSCP_MASK) {
1658 return xasprintf("%s: not a valid TOS", arg);
1660 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1665 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1667 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1670 /* Set IPv4/v6 ECN actions. */
1673 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1674 enum ofp_version ofp_version OVS_UNUSED,
1677 if (ecn & ~IP_ECN_MASK) {
1678 return OFPERR_OFPBAC_BAD_ARGUMENT;
1680 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1686 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1687 enum ofp_version ofp_version, struct ofpbuf *out)
1689 uint8_t ecn = ip_ecn->ecn;
1690 if (ofp_version == OFP10_VERSION) {
1692 } else if (ofp_version == OFP11_VERSION) {
1693 put_OFPAT11_SET_NW_ECN(out, ecn);
1695 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1699 static char * OVS_WARN_UNUSED_RESULT
1700 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1701 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1706 error = str_to_u8(arg, "ECN", &ecn);
1711 if (ecn & ~IP_ECN_MASK) {
1712 return xasprintf("%s: not a valid ECN", arg);
1714 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1719 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1721 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1724 /* Set IPv4/v6 TTL actions. */
1727 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1728 enum ofp_version ofp_version OVS_UNUSED,
1731 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1736 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1737 enum ofp_version ofp_version, struct ofpbuf *out)
1739 if (ofp_version >= OFP11_VERSION) {
1740 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1746 static char * OVS_WARN_UNUSED_RESULT
1747 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1748 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1753 error = str_to_u8(arg, "TTL", &ttl);
1758 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1763 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1765 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1768 /* Set TCP/UDP/SCTP port actions. */
1771 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1772 enum ofp_version ofp_version OVS_UNUSED,
1775 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1780 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1781 enum ofp_version ofp_version OVS_UNUSED,
1784 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1789 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1790 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1791 enum mf_field_id field, struct ofpbuf *out)
1793 uint16_t port = l4_port->port;
1795 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1796 ofpact_put_set_field(out, ofp_version, field, port);
1798 ofpact_put_raw(out, ofp_version, raw, port);
1803 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1804 enum ofp_version ofp_version, struct ofpbuf *out)
1806 uint8_t proto = l4_port->flow_ip_proto;
1807 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1808 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1809 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1812 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1816 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1817 enum ofp_version ofp_version,
1820 uint8_t proto = l4_port->flow_ip_proto;
1821 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1822 : proto == IPPROTO_UDP ? MFF_UDP_DST
1823 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1826 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1829 static char * OVS_WARN_UNUSED_RESULT
1830 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1831 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1833 return str_to_u16(arg, "source port",
1834 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1837 static char * OVS_WARN_UNUSED_RESULT
1838 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1839 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1841 return str_to_u16(arg, "destination port",
1842 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1846 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1848 ds_put_format(s, "mod_tp_src:%d", a->port);
1852 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1854 ds_put_format(s, "mod_tp_dst:%d", a->port);
1857 /* Action structure for OFPAT_COPY_FIELD. */
1858 struct ofp15_action_copy_field {
1859 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1860 ovs_be16 len; /* Length is padded to 64 bits. */
1861 ovs_be16 n_bits; /* Number of bits to copy. */
1862 ovs_be16 src_offset; /* Starting bit offset in source. */
1863 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1866 * - OXM header for source field.
1867 * - OXM header for destination field.
1868 * - Padding with 0-bytes to a multiple of 8 bytes.
1869 * The "pad2" member is the beginning of the above. */
1872 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1874 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1875 struct onf_action_copy_field {
1876 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
1877 ovs_be16 len; /* Length is padded to 64 bits. */
1878 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
1879 ovs_be16 exp_type; /* 3200. */
1880 uint8_t pad[2]; /* Not used. */
1881 ovs_be16 n_bits; /* Number of bits to copy. */
1882 ovs_be16 src_offset; /* Starting bit offset in source. */
1883 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1884 uint8_t pad2[2]; /* Not used. */
1886 * - OXM header for source field.
1887 * - OXM header for destination field.
1888 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1889 * The "pad3" member is the beginning of the above. */
1890 uint8_t pad3[4]; /* Not used. */
1892 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1894 /* Action structure for NXAST_REG_MOVE.
1896 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1897 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1898 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1899 * the next most significant bit, and so on.
1901 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1902 * sense to use nxm_hasmask=1 because the action does not do any kind of
1903 * matching; it uses the actual value of a field.)
1905 * The following nxm_header values are potentially acceptable as 'src':
1920 * - NXM_OF_ICMP_TYPE
1921 * - NXM_OF_ICMP_CODE
1928 * - NXM_NX_ICMPV6_TYPE
1929 * - NXM_NX_ICMPV6_CODE
1932 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1934 * - NXM_NX_TUN_IPV4_SRC
1935 * - NXM_NX_TUN_IPV4_DST
1937 * The following nxm_header values are potentially acceptable as 'dst':
1948 * - NXM_OF_ICMP_TYPE
1949 * - NXM_OF_ICMP_CODE
1950 * - NXM_NX_ICMPV6_TYPE
1951 * - NXM_NX_ICMPV6_CODE
1957 * Modifying any of the above fields changes the corresponding packet
1962 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1966 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1967 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1968 * header (if any), ignoring the other bits. Setting a value with CFI=1
1969 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1970 * to the field's new value (with the CFI bit masked out).
1972 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1973 * any of these values modifies the corresponding tunnel header field used
1974 * for the packet's next tunnel encapsulation, if allowed by the
1975 * configuration of the output tunnel port.
1977 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1978 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1979 * used only if the flow's nx_match includes an nxm_entry that specifies
1980 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1982 * The switch will reject actions for which src_ofs+n_bits is greater than the
1983 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1984 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1986 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1987 * behaves as if 'src' were copied out to a temporary buffer, then the
1988 * temporary buffer copied to 'dst'.
1990 struct nx_action_reg_move {
1991 ovs_be16 type; /* OFPAT_VENDOR. */
1992 ovs_be16 len; /* Length is 24. */
1993 ovs_be32 vendor; /* NX_VENDOR_ID. */
1994 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1995 ovs_be16 n_bits; /* Number of bits. */
1996 ovs_be16 src_ofs; /* Starting bit offset in source. */
1997 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
1999 * - OXM/NXM header for source field (4 or 8 bytes).
2000 * - OXM/NXM header for destination field (4 or 8 bytes).
2001 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2003 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
2006 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
2007 const void *action, ovs_be16 action_len, size_t oxm_offset,
2008 struct ofpbuf *ofpacts)
2010 struct ofpact_reg_move *move;
2014 move = ofpact_put_REG_MOVE(ofpacts);
2015 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
2016 move->src.ofs = ntohs(src_offset);
2017 move->src.n_bits = ntohs(n_bits);
2018 move->dst.ofs = ntohs(dst_offset);
2019 move->dst.n_bits = ntohs(n_bits);
2021 ofpbuf_use_const(&b, action, ntohs(action_len));
2022 ofpbuf_pull(&b, oxm_offset);
2023 error = nx_pull_header(&b, &move->src.field, NULL);
2027 error = nx_pull_header(&b, &move->dst.field, NULL);
2032 if (!is_all_zeros(b.data, b.size)) {
2033 return OFPERR_NXBRC_MUST_BE_ZERO;
2036 return nxm_reg_move_check(move, NULL);
2040 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
2041 enum ofp_version ofp_version OVS_UNUSED,
2042 struct ofpbuf *ofpacts)
2044 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2045 oacf->n_bits, oacf, oacf->len,
2046 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
2050 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
2051 enum ofp_version ofp_version OVS_UNUSED,
2052 struct ofpbuf *ofpacts)
2054 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2055 oacf->n_bits, oacf, oacf->len,
2056 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
2060 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
2061 enum ofp_version ofp_version OVS_UNUSED,
2062 struct ofpbuf *ofpacts)
2064 struct ofpact_reg_move *move;
2068 move = ofpact_put_REG_MOVE(ofpacts);
2069 move->ofpact.raw = NXAST_RAW_REG_MOVE;
2070 move->src.ofs = ntohs(narm->src_ofs);
2071 move->src.n_bits = ntohs(narm->n_bits);
2072 move->dst.ofs = ntohs(narm->dst_ofs);
2073 move->dst.n_bits = ntohs(narm->n_bits);
2075 ofpbuf_use_const(&b, narm, ntohs(narm->len));
2076 ofpbuf_pull(&b, sizeof *narm);
2077 error = nx_pull_header(&b, &move->src.field, NULL);
2081 error = nx_pull_header(&b, &move->dst.field, NULL);
2085 if (!is_all_zeros(b.data, b.size)) {
2086 return OFPERR_NXBRC_MUST_BE_ZERO;
2089 return nxm_reg_move_check(move, NULL);
2093 encode_REG_MOVE(const struct ofpact_reg_move *move,
2094 enum ofp_version ofp_version, struct ofpbuf *out)
2096 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2097 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2098 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2099 * probe for support. Until we have that ability, we currently prefer
2100 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2102 size_t start_ofs = out->size;
2103 if (ofp_version >= OFP15_VERSION) {
2104 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2105 copy->n_bits = htons(move->dst.n_bits);
2106 copy->src_offset = htons(move->src.ofs);
2107 copy->dst_offset = htons(move->dst.ofs);
2108 out->size = out->size - sizeof copy->pad2;
2109 nx_put_header(out, move->src.field->id, ofp_version, false);
2110 nx_put_header(out, move->dst.field->id, ofp_version, false);
2111 } else if (ofp_version == OFP13_VERSION
2112 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2113 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2114 copy->n_bits = htons(move->dst.n_bits);
2115 copy->src_offset = htons(move->src.ofs);
2116 copy->dst_offset = htons(move->dst.ofs);
2117 out->size = out->size - sizeof copy->pad3;
2118 nx_put_header(out, move->src.field->id, ofp_version, false);
2119 nx_put_header(out, move->dst.field->id, ofp_version, false);
2121 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2122 narm->n_bits = htons(move->dst.n_bits);
2123 narm->src_ofs = htons(move->src.ofs);
2124 narm->dst_ofs = htons(move->dst.ofs);
2125 nx_put_header(out, move->src.field->id, 0, false);
2126 nx_put_header(out, move->dst.field->id, 0, false);
2128 pad_ofpat(out, start_ofs);
2131 static char * OVS_WARN_UNUSED_RESULT
2132 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2133 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2135 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2136 const char *full_arg = arg;
2139 error = mf_parse_subfield__(&move->src, &arg);
2143 if (strncmp(arg, "->", 2)) {
2144 return xasprintf("%s: missing `->' following source", full_arg);
2147 error = mf_parse_subfield(&move->dst, arg);
2152 if (move->src.n_bits != move->dst.n_bits) {
2153 return xasprintf("%s: source field is %d bits wide but destination is "
2154 "%d bits wide", full_arg,
2155 move->src.n_bits, move->dst.n_bits);
2161 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2163 nxm_format_reg_move(a, s);
2166 /* Action structure for OFPAT12_SET_FIELD. */
2167 struct ofp12_action_set_field {
2168 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2169 ovs_be16 len; /* Length is padded to 64 bits. */
2172 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2173 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2175 * The "pad" member is the beginning of the above. */
2178 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2180 /* Action structure for NXAST_REG_LOAD.
2182 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2183 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2184 * starts at 0 for the least-significant bit, 1 for the next most significant
2187 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2188 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2191 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2192 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2193 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2194 * also stored as one less than its true value:
2197 * +------------------------------+------------------+
2198 * | ofs | n_bits - 1 |
2199 * +------------------------------+------------------+
2201 * The switch will reject actions for which ofs+n_bits is greater than the
2202 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2203 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2204 * OFPBAC_BAD_ARGUMENT.
2206 struct nx_action_reg_load {
2207 ovs_be16 type; /* OFPAT_VENDOR. */
2208 ovs_be16 len; /* Length is 24. */
2209 ovs_be32 vendor; /* NX_VENDOR_ID. */
2210 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2211 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2212 ovs_be32 dst; /* Destination register. */
2213 ovs_be64 value; /* Immediate value. */
2215 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2217 /* Action structure for NXAST_REG_LOAD2.
2219 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2220 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2221 * OXM experimenter fields. */
2222 struct nx_action_reg_load2 {
2223 ovs_be16 type; /* OFPAT_VENDOR. */
2224 ovs_be16 len; /* At least 16. */
2225 ovs_be32 vendor; /* NX_VENDOR_ID. */
2226 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2229 * - An NXM/OXM header, value, and optionally a mask.
2230 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2232 * The "pad" member is the beginning of the above. */
2235 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2238 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2239 bool may_mask, struct ofpbuf *ofpacts)
2241 struct ofpact_set_field *sf;
2245 sf = ofpact_put_SET_FIELD(ofpacts);
2247 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2248 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2249 error = nx_pull_entry(&b, &sf->field, &sf->value,
2250 may_mask ? &sf->mask : NULL);
2252 return (error == OFPERR_OFPBMC_BAD_MASK
2253 ? OFPERR_OFPBAC_BAD_SET_MASK
2257 memset(&sf->mask, 0xff, sf->field->n_bytes);
2260 if (!is_all_zeros(b.data, b.size)) {
2261 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2264 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2266 if (sf->field->id == MFF_IN_PORT_OXM) {
2267 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2270 /* oxm_length is now validated to be compatible with mf_value. */
2271 if (!sf->field->writable) {
2272 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2274 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2277 /* The value must be valid for match. OpenFlow 1.5 also says,
2278 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2279 * a 1-bit in oxm_value and in oxm_mask." */
2280 if (!mf_is_value_valid(sf->field, &sf->value)
2281 || (sf->field->id == MFF_VLAN_VID
2282 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2283 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2284 struct ds ds = DS_EMPTY_INITIALIZER;
2285 mf_format(sf->field, &sf->value, NULL, &ds);
2286 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2287 sf->field->name, ds_cstr(&ds));
2290 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2296 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2297 enum ofp_version ofp_version OVS_UNUSED,
2298 struct ofpbuf *ofpacts)
2300 return decode_ofpat_set_field(oasf, false, ofpacts);
2304 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2305 enum ofp_version ofp_version OVS_UNUSED,
2306 struct ofpbuf *ofpacts)
2308 return decode_ofpat_set_field(oasf, true, ofpacts);
2312 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2313 enum ofp_version ofp_version OVS_UNUSED,
2316 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2317 struct mf_subfield dst;
2320 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2322 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2323 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2324 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2325 error = mf_check_dst(&dst, NULL);
2330 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2332 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2333 return OFPERR_OFPBAC_BAD_ARGUMENT;
2336 sf->field = dst.field;
2337 bitwise_put(ntohll(narl->value),
2338 &sf->value, dst.field->n_bytes, dst.ofs,
2340 bitwise_put(UINT64_MAX,
2341 &sf->mask, dst.field->n_bytes, dst.ofs,
2348 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2349 enum ofp_version ofp_version OVS_UNUSED,
2352 struct ofpact_set_field *sf;
2356 sf = ofpact_put_SET_FIELD(out);
2357 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2359 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2360 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2361 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2365 if (!is_all_zeros(b.data, b.size)) {
2366 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2369 if (!sf->field->writable) {
2370 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2372 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2378 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2379 enum mf_field_id field, uint64_t value_)
2381 struct ofp12_action_set_field *oasf OVS_UNUSED;
2382 int n_bytes = mf_from_id(field)->n_bytes;
2383 size_t start_ofs = openflow->size;
2384 union mf_value value;
2386 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2388 oasf = put_OFPAT12_SET_FIELD(openflow);
2389 openflow->size = openflow->size - sizeof oasf->pad;
2390 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2391 pad_ofpat(openflow, start_ofs);
2395 next_load_segment(const struct ofpact_set_field *sf,
2396 struct mf_subfield *dst, uint64_t *value)
2398 int n_bits = sf->field->n_bits;
2399 int n_bytes = sf->field->n_bytes;
2400 int start = dst->ofs + dst->n_bits;
2402 if (start < n_bits) {
2403 dst->field = sf->field;
2404 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2405 if (dst->ofs < n_bits) {
2406 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2407 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2408 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2415 /* Convert 'sf' to a series of REG_LOADs. */
2417 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2419 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2420 * experimenter OXM or is variable length (or if it came in as
2421 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2422 * NXAST_REG_LOAD, which is backward compatible. */
2423 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2424 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2425 struct nx_action_reg_load2 *narl OVS_UNUSED;
2426 size_t start_ofs = openflow->size;
2428 narl = put_NXAST_REG_LOAD2(openflow);
2429 openflow->size = openflow->size - sizeof narl->pad;
2430 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2431 pad_ofpat(openflow, start_ofs);
2433 struct mf_subfield dst;
2436 dst.ofs = dst.n_bits = 0;
2437 while (next_load_segment(sf, &dst, &value)) {
2438 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2439 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2440 narl->dst = htonl(mf_nxm_header(dst.field->id));
2441 narl->value = htonll(value);
2446 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2447 * actions, if we can, falling back to Nicira extensions if we must.
2449 * We check only meta-flow types that can appear within set field actions and
2450 * that have a mapping to compatible action types. These struct mf_field
2451 * definitions have a defined OXM or NXM header value and specify the field as
2454 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2455 enum ofp_version ofp_version,
2458 switch ((int) sf->field->id) {
2459 case MFF_VLAN_TCI: {
2460 ovs_be16 tci = sf->value.be16;
2461 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2462 uint16_t vid = vlan_tci_to_vid(tci);
2463 uint8_t pcp = vlan_tci_to_pcp(tci);
2465 if (ofp_version < OFP11_VERSION) {
2466 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2468 * If CFI=1, Add or modify VLAN VID & PCP.
2469 * If CFI=0, strip VLAN header, if any.
2472 put_OFPAT10_SET_VLAN_VID(out, vid);
2473 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2475 put_OFPAT10_STRIP_VLAN(out);
2478 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2480 * If CFI=1, Add or modify VLAN VID & PCP.
2481 * OpenFlow 1.1 set actions only apply if the packet
2482 * already has VLAN tags. To be sure that is the case
2483 * we have to push a VLAN header. As we do not support
2484 * multiple layers of VLANs, this is a no-op, if a VLAN
2485 * header already exists. This may backfire, however,
2486 * when we start supporting multiple layers of VLANs.
2487 * If CFI=0, strip VLAN header, if any.
2490 /* Push a VLAN tag, if one was not seen at action validation
2492 if (!sf->flow_has_vlan) {
2493 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2495 put_OFPAT11_SET_VLAN_VID(out, vid);
2496 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2498 /* If the flow did not match on vlan, we have no way of
2499 * knowing if the vlan tag exists, so we must POP just to be
2501 put_OFPAT11_POP_VLAN(out);
2507 case MFF_VLAN_VID: {
2508 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2509 if (ofp_version == OFP10_VERSION) {
2510 put_OFPAT10_SET_VLAN_VID(out, vid);
2512 put_OFPAT11_SET_VLAN_VID(out, vid);
2518 if (ofp_version == OFP10_VERSION) {
2519 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2521 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2526 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2530 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2534 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2538 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2542 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2545 case MFF_IP_DSCP_SHIFTED:
2546 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2551 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2556 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2560 set_field_to_nxast(sf, out);
2566 set_field_to_set_field(const struct ofpact_set_field *sf,
2567 enum ofp_version ofp_version, struct ofpbuf *out)
2569 struct ofp12_action_set_field *oasf OVS_UNUSED;
2570 size_t start_ofs = out->size;
2572 oasf = put_OFPAT12_SET_FIELD(out);
2573 out->size = out->size - sizeof oasf->pad;
2574 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2575 pad_ofpat(out, start_ofs);
2579 encode_SET_FIELD(const struct ofpact_set_field *sf,
2580 enum ofp_version ofp_version, struct ofpbuf *out)
2582 if (ofp_version >= OFP15_VERSION) {
2583 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2585 set_field_to_set_field(sf, ofp_version, out);
2586 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2587 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2588 /* It came in as reg_load, send it out the same way. */
2589 set_field_to_nxast(sf, out);
2590 } else if (ofp_version < OFP12_VERSION) {
2591 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2592 set_field_to_legacy_openflow(sf, ofp_version, out);
2593 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2594 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2595 * entire field, so encode it as OFPAT_SET_FIELD. */
2596 set_field_to_set_field(sf, ofp_version, out);
2598 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2599 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2600 * so encode it as reg_load. */
2601 set_field_to_nxast(sf, out);
2605 /* Parses the input argument 'arg' into the key, value, and delimiter
2606 * components that are common across the reg_load and set_field action format.
2608 * With an argument like "1->metadata", sets the following pointers to
2609 * point within 'arg':
2614 * Returns NULL if successful, otherwise a malloc()'d string describing the
2615 * error. The caller is responsible for freeing the returned string. */
2616 static char * OVS_WARN_UNUSED_RESULT
2617 set_field_split_str(char *arg, char **key, char **value, char **delim)
2622 value_end = strstr(arg, "->");
2623 *key = value_end + strlen("->");
2629 return xasprintf("%s: missing `->'", arg);
2631 if (strlen(value_end) <= strlen("->")) {
2632 return xasprintf("%s: missing field name following `->'", arg);
2638 /* Parses a "set_field" action with argument 'arg', appending the parsed
2639 * action to 'ofpacts'.
2641 * Returns NULL if successful, otherwise a malloc()'d string describing the
2642 * error. The caller is responsible for freeing the returned string. */
2643 static char * OVS_WARN_UNUSED_RESULT
2644 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2645 enum ofputil_protocol *usable_protocols)
2647 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2651 const struct mf_field *mf;
2654 error = set_field_split_str(arg, &key, &value, &delim);
2659 mf = mf_from_name(key);
2661 return xasprintf("%s is not a valid OXM field name", key);
2663 if (!mf->writable) {
2664 return xasprintf("%s is read-only", key);
2668 error = mf_parse(mf, value, &sf->value, &sf->mask);
2673 if (!mf_is_value_valid(mf, &sf->value)) {
2674 return xasprintf("%s is not a valid value for field %s", value, key);
2677 *usable_protocols &= mf->usable_protocols_exact;
2681 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2682 * action to 'ofpacts'.
2684 * Returns NULL if successful, otherwise a malloc()'d string describing the
2685 * error. The caller is responsible for freeing the returned string. */
2686 static char * OVS_WARN_UNUSED_RESULT
2687 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2688 enum ofputil_protocol *usable_protocols)
2690 char *copy = xstrdup(arg);
2691 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2696 static char * OVS_WARN_UNUSED_RESULT
2697 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2699 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2700 struct mf_subfield dst;
2701 char *key, *value_str;
2702 union mf_value value;
2705 error = set_field_split_str(arg, &key, &value_str, NULL);
2710 error = mf_parse_subfield(&dst, key);
2715 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2717 return xasprintf("%s: cannot parse integer value", arg);
2720 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2721 dst.field->n_bytes * 8 - dst.n_bits)) {
2725 mf_format(dst.field, &value, NULL, &ds);
2726 error = xasprintf("%s: value %s does not fit into %d bits",
2727 arg, ds_cstr(&ds), dst.n_bits);
2732 sf->field = dst.field;
2733 memset(&sf->value, 0, sizeof sf->value);
2734 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2735 dst.field->n_bytes, dst.ofs, dst.n_bits);
2736 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2742 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2744 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2745 struct mf_subfield dst;
2748 dst.ofs = dst.n_bits = 0;
2749 while (next_load_segment(a, &dst, &value)) {
2750 ds_put_format(s, "load:%#"PRIx64"->", value);
2751 mf_format_subfield(&dst, s);
2752 ds_put_char(s, ',');
2756 ds_put_cstr(s, "set_field:");
2757 mf_format(a->field, &a->value, &a->mask, s);
2758 ds_put_format(s, "->%s", a->field->name);
2762 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2763 * is marked such that, if possible, it will be translated to OpenFlow as
2764 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2765 * that was the way that the action was expressed when it came into OVS or for
2766 * backward compatibility. */
2767 struct ofpact_set_field *
2768 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2770 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2771 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2775 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2777 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2780 struct nx_action_stack {
2781 ovs_be16 type; /* OFPAT_VENDOR. */
2782 ovs_be16 len; /* Length is 16. */
2783 ovs_be32 vendor; /* NX_VENDOR_ID. */
2784 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2785 ovs_be16 offset; /* Bit offset into the field. */
2787 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2788 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2789 * - Enough 0-bytes to pad out the action to 24 bytes. */
2790 uint8_t pad[12]; /* See above. */
2792 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2795 decode_stack_action(const struct nx_action_stack *nasp,
2796 struct ofpact_stack *stack_action)
2801 stack_action->subfield.ofs = ntohs(nasp->offset);
2803 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2804 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2805 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2809 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2811 if (!is_all_zeros(b.data, b.size)) {
2812 return OFPERR_NXBRC_MUST_BE_ZERO;
2819 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2820 enum ofp_version ofp_version OVS_UNUSED,
2821 struct ofpbuf *ofpacts)
2823 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2824 enum ofperr error = decode_stack_action(nasp, push);
2825 return error ? error : nxm_stack_push_check(push, NULL);
2829 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2830 enum ofp_version ofp_version OVS_UNUSED,
2831 struct ofpbuf *ofpacts)
2833 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2834 enum ofperr error = decode_stack_action(nasp, pop);
2835 return error ? error : nxm_stack_pop_check(pop, NULL);
2839 encode_STACK_op(const struct ofpact_stack *stack_action,
2840 struct nx_action_stack *nasp)
2845 nasp->offset = htons(stack_action->subfield.ofs);
2847 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2848 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2849 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2850 n_bits = htons(stack_action->subfield.n_bits);
2851 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2855 encode_STACK_PUSH(const struct ofpact_stack *stack,
2856 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2858 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2862 encode_STACK_POP(const struct ofpact_stack *stack,
2863 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2865 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2868 static char * OVS_WARN_UNUSED_RESULT
2869 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2870 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2872 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2875 static char * OVS_WARN_UNUSED_RESULT
2876 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2877 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2879 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2883 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2885 nxm_format_stack_push(a, s);
2889 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2891 nxm_format_stack_pop(a, s);
2894 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2896 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2897 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2898 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2899 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2901 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2902 * sent only to controllers with id 0.)
2904 struct nx_action_cnt_ids {
2905 ovs_be16 type; /* OFPAT_VENDOR. */
2906 ovs_be16 len; /* Length including slaves. */
2907 ovs_be32 vendor; /* NX_VENDOR_ID. */
2908 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2910 ovs_be16 n_controllers; /* Number of controllers. */
2911 uint8_t zeros[4]; /* Must be zero. */
2913 /* Followed by 1 or more controller ids.
2915 * uint16_t cnt_ids[]; // Controller ids.
2916 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2919 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2922 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2925 struct ofpact_cnt_ids *ids;
2926 enum ofperr error = 0;
2928 ids = ofpact_put_DEC_TTL(out);
2929 ids->n_controllers = 1;
2930 ofpbuf_put(out, &id, sizeof id);
2932 ofpact_update_len(out, &ids->ofpact);
2937 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2938 enum ofp_version ofp_version OVS_UNUSED,
2941 struct ofpact_cnt_ids *ids;
2945 ids = ofpact_put_DEC_TTL(out);
2946 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2947 ids->n_controllers = ntohs(nac_ids->n_controllers);
2948 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2950 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2951 return OFPERR_NXBRC_MUST_BE_ZERO;
2954 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2955 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2956 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2957 "are required for %"PRIu16" controllers.",
2958 ids_size, ids->n_controllers * sizeof(ovs_be16),
2959 ids->n_controllers);
2960 return OFPERR_OFPBAC_BAD_LEN;
2963 for (i = 0; i < ids->n_controllers; i++) {
2964 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2965 ofpbuf_put(out, &id, sizeof id);
2969 ofpact_update_len(out, &ids->ofpact);
2975 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2976 enum ofp_version ofp_version, struct ofpbuf *out)
2978 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2979 || dec_ttl->n_controllers != 1
2980 || dec_ttl->cnt_ids[0] != 0) {
2981 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2982 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2986 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2987 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2989 ids = ofpbuf_put_zeros(out, ids_len);
2990 for (i = 0; i < dec_ttl->n_controllers; i++) {
2991 ids[i] = htons(dec_ttl->cnt_ids[i]);
2994 put_OFPAT_DEC_NW_TTL(out, ofp_version);
2999 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
3001 struct ofpact_cnt_ids *ids;
3004 ofpact_put_DEC_TTL(ofpacts);
3005 ofpbuf_put(ofpacts, &id, sizeof id);
3006 ids = ofpacts->header;
3007 ids->n_controllers++;
3008 ofpact_update_len(ofpacts, &ids->ofpact);
3011 static char * OVS_WARN_UNUSED_RESULT
3012 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
3013 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3016 parse_noargs_dec_ttl(ofpacts);
3018 struct ofpact_cnt_ids *ids;
3021 ids = ofpact_put_DEC_TTL(ofpacts);
3022 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3023 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
3024 cntr = strtok_r(NULL, ", ", &arg)) {
3025 uint16_t id = atoi(cntr);
3027 ofpbuf_put(ofpacts, &id, sizeof id);
3028 ids = ofpacts->header;
3029 ids->n_controllers++;
3031 if (!ids->n_controllers) {
3032 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3035 ofpact_update_len(ofpacts, &ids->ofpact);
3041 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
3045 ds_put_cstr(s, "dec_ttl");
3046 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
3047 ds_put_cstr(s, "(");
3048 for (i = 0; i < a->n_controllers; i++) {
3050 ds_put_cstr(s, ",");
3052 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
3054 ds_put_cstr(s, ")");
3058 /* Set MPLS label actions. */
3061 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
3062 enum ofp_version ofp_version OVS_UNUSED,
3065 ofpact_put_SET_MPLS_LABEL(out)->label = label;
3070 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3071 enum ofp_version ofp_version,
3074 if (ofp_version < OFP12_VERSION) {
3075 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3077 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
3078 ntohl(label->label));
3082 static char * OVS_WARN_UNUSED_RESULT
3083 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3084 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3086 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3088 return xstrdup("set_mpls_label: expected label.");
3091 mpls_label->label = htonl(atoi(arg));
3096 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3098 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
3101 /* Set MPLS TC actions. */
3104 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3105 enum ofp_version ofp_version OVS_UNUSED,
3108 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3113 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3114 enum ofp_version ofp_version, struct ofpbuf *out)
3116 if (ofp_version < OFP12_VERSION) {
3117 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3119 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3123 static char * OVS_WARN_UNUSED_RESULT
3124 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3125 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3127 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3130 return xstrdup("set_mpls_tc: expected tc.");
3133 mpls_tc->tc = atoi(arg);
3138 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3140 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
3143 /* Set MPLS TTL actions. */
3146 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3147 enum ofp_version ofp_version OVS_UNUSED,
3150 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3155 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3156 enum ofp_version ofp_version, struct ofpbuf *out)
3158 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3161 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3162 * action to 'ofpacts'.
3164 * Returns NULL if successful, otherwise a malloc()'d string describing the
3165 * error. The caller is responsible for freeing the returned string. */
3166 static char * OVS_WARN_UNUSED_RESULT
3167 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3168 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3170 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3173 return xstrdup("set_mpls_ttl: expected ttl.");
3176 mpls_ttl->ttl = atoi(arg);
3181 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3183 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3186 /* Decrement MPLS TTL actions. */
3189 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3191 ofpact_put_DEC_MPLS_TTL(out);
3196 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3197 enum ofp_version ofp_version, struct ofpbuf *out)
3199 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3202 static char * OVS_WARN_UNUSED_RESULT
3203 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3204 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3206 ofpact_put_DEC_MPLS_TTL(ofpacts);
3211 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3213 ds_put_cstr(s, "dec_mpls_ttl");
3216 /* Push MPLS label action. */
3219 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3220 enum ofp_version ofp_version OVS_UNUSED,
3223 struct ofpact_push_mpls *oam;
3225 if (!eth_type_mpls(ethertype)) {
3226 return OFPERR_OFPBAC_BAD_ARGUMENT;
3228 oam = ofpact_put_PUSH_MPLS(out);
3229 oam->ethertype = ethertype;
3235 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3236 enum ofp_version ofp_version, struct ofpbuf *out)
3238 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3241 static char * OVS_WARN_UNUSED_RESULT
3242 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3243 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3248 error = str_to_u16(arg, "push_mpls", ðertype);
3250 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3256 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3258 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3261 /* Pop MPLS label action. */
3264 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3265 enum ofp_version ofp_version OVS_UNUSED,
3268 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3273 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3274 enum ofp_version ofp_version, struct ofpbuf *out)
3276 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3279 static char * OVS_WARN_UNUSED_RESULT
3280 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3281 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3286 error = str_to_u16(arg, "pop_mpls", ðertype);
3288 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3294 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3296 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3299 /* Set tunnel ID actions. */
3302 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3303 enum ofp_version ofp_version OVS_UNUSED,
3306 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3307 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3308 tunnel->tun_id = tun_id;
3313 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3314 enum ofp_version ofp_version OVS_UNUSED,
3317 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3318 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3319 tunnel->tun_id = tun_id;
3324 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3325 enum ofp_version ofp_version, struct ofpbuf *out)
3327 uint64_t tun_id = tunnel->tun_id;
3329 if (ofp_version < OFP12_VERSION) {
3330 if (tun_id <= UINT32_MAX
3331 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3332 put_NXAST_SET_TUNNEL(out, tun_id);
3334 put_NXAST_SET_TUNNEL64(out, tun_id);
3337 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3341 static char * OVS_WARN_UNUSED_RESULT
3342 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3343 enum ofp_raw_action_type raw)
3345 struct ofpact_tunnel *tunnel;
3347 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3348 tunnel->ofpact.raw = raw;
3349 return str_to_u64(arg, &tunnel->tun_id);
3352 static char * OVS_WARN_UNUSED_RESULT
3353 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3354 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3356 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3360 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3362 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3363 (a->tun_id > UINT32_MAX
3364 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3368 /* Set queue action. */
3371 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3372 enum ofp_version ofp_version OVS_UNUSED,
3375 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3380 encode_SET_QUEUE(const struct ofpact_queue *queue,
3381 enum ofp_version ofp_version, struct ofpbuf *out)
3383 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3386 static char * OVS_WARN_UNUSED_RESULT
3387 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3388 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3390 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3394 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3396 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3399 /* Pop queue action. */
3402 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3404 ofpact_put_POP_QUEUE(out);
3409 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3410 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3412 put_NXAST_POP_QUEUE(out);
3415 static char * OVS_WARN_UNUSED_RESULT
3416 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3417 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3419 ofpact_put_POP_QUEUE(ofpacts);
3424 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3426 ds_put_cstr(s, "pop_queue");
3429 /* Action structure for NXAST_FIN_TIMEOUT.
3431 * This action changes the idle timeout or hard timeout, or both, of this
3432 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3433 * When such a packet is observed, the action reduces the rule's idle timeout
3434 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3435 * action has no effect on an existing timeout that is already shorter than the
3436 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3437 * of zero has no effect on the respective timeout.
3439 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3440 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3441 * receipt of the FIN or RST.
3443 * This is useful for quickly discarding learned TCP flows that otherwise will
3444 * take a long time to expire.
3446 * This action is intended for use with an OpenFlow rule that matches only a
3447 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3448 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3449 * RST in any of those flows will cause the entire OpenFlow rule to expire
3450 * early, which is not normally desirable.
3452 struct nx_action_fin_timeout {
3453 ovs_be16 type; /* OFPAT_VENDOR. */
3454 ovs_be16 len; /* 16. */
3455 ovs_be32 vendor; /* NX_VENDOR_ID. */
3456 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3457 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3458 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3459 ovs_be16 pad; /* Must be zero. */
3461 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3464 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3465 enum ofp_version ofp_version OVS_UNUSED,
3468 struct ofpact_fin_timeout *oft;
3470 oft = ofpact_put_FIN_TIMEOUT(out);
3471 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3472 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3477 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3478 enum ofp_version ofp_version OVS_UNUSED,
3481 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3482 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3483 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3486 static char * OVS_WARN_UNUSED_RESULT
3487 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3488 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3490 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3493 while (ofputil_parse_key_value(&arg, &key, &value)) {
3496 if (!strcmp(key, "idle_timeout")) {
3497 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3498 } else if (!strcmp(key, "hard_timeout")) {
3499 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3501 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3513 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3515 ds_put_cstr(s, "fin_timeout(");
3516 if (a->fin_idle_timeout) {
3517 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3519 if (a->fin_hard_timeout) {
3520 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3523 ds_put_char(s, ')');
3526 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3528 * These actions search one of the switch's flow tables:
3530 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3531 * it specifies the table to search.
3533 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3534 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3535 * table, that is, the OpenFlow flow table that contains the flow from
3536 * which this action was obtained. If this action did not come from a
3537 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3538 * is the current table.
3540 * The flow table lookup uses a flow that may be slightly modified from the
3543 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3544 * is used as the flow's in_port.
3546 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3547 * then its value is used as the flow's in_port. Otherwise, the original
3550 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3551 * resubmit action, then the flow is updated with the new values.
3553 * Following the lookup, the original in_port is restored.
3555 * If the modified flow matched in the flow table, then the corresponding
3556 * actions are executed. Afterward, actions following the resubmit in the
3557 * original set of actions, if any, are executed; any changes made to the
3558 * packet (e.g. changes to VLAN) by secondary actions persist when those
3559 * actions are executed, although the original in_port is restored.
3561 * Resubmit actions may be used any number of times within a set of actions.
3563 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3564 * implementation-defined depth, further resubmit actions are simply ignored.
3566 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3567 * 'pad' to be all-bits-zero.
3569 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3570 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3572 struct nx_action_resubmit {
3573 ovs_be16 type; /* OFPAT_VENDOR. */
3574 ovs_be16 len; /* Length is 16. */
3575 ovs_be32 vendor; /* NX_VENDOR_ID. */
3576 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3577 ovs_be16 in_port; /* New in_port for checking flow table. */
3578 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3581 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3584 decode_NXAST_RAW_RESUBMIT(uint16_t port,
3585 enum ofp_version ofp_version OVS_UNUSED,
3588 struct ofpact_resubmit *resubmit;
3590 resubmit = ofpact_put_RESUBMIT(out);
3591 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3592 resubmit->in_port = u16_to_ofp(port);
3593 resubmit->table_id = 0xff;
3598 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3599 enum ofp_version ofp_version OVS_UNUSED,
3602 struct ofpact_resubmit *resubmit;
3604 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3605 return OFPERR_OFPBAC_BAD_ARGUMENT;
3608 resubmit = ofpact_put_RESUBMIT(out);
3609 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3610 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3611 resubmit->table_id = nar->table;
3616 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3617 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3619 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3621 if (resubmit->table_id == 0xff
3622 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3623 put_NXAST_RESUBMIT(out, in_port);
3625 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3626 nar->table = resubmit->table_id;
3627 nar->in_port = htons(in_port);
3631 static char * OVS_WARN_UNUSED_RESULT
3632 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3633 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3635 struct ofpact_resubmit *resubmit;
3636 char *in_port_s, *table_s;
3638 resubmit = ofpact_put_RESUBMIT(ofpacts);
3640 in_port_s = strsep(&arg, ",");
3641 if (in_port_s && in_port_s[0]) {
3642 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3643 return xasprintf("%s: resubmit to unknown port", in_port_s);
3646 resubmit->in_port = OFPP_IN_PORT;
3649 table_s = strsep(&arg, ",");
3650 if (table_s && table_s[0]) {
3651 uint32_t table_id = 0;
3654 error = str_to_u32(table_s, &table_id);
3658 resubmit->table_id = table_id;
3660 resubmit->table_id = 255;
3663 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3664 return xstrdup("at least one \"in_port\" or \"table\" must be "
3665 "specified on resubmit");
3671 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3673 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3674 ds_put_cstr(s, "resubmit:");
3675 ofputil_format_port(a->in_port, s);
3677 ds_put_format(s, "resubmit(");
3678 if (a->in_port != OFPP_IN_PORT) {
3679 ofputil_format_port(a->in_port, s);
3681 ds_put_char(s, ',');
3682 if (a->table_id != 255) {
3683 ds_put_format(s, "%"PRIu8, a->table_id);
3685 ds_put_char(s, ')');
3689 /* Action structure for NXAST_LEARN.
3691 * This action adds or modifies a flow in an OpenFlow table, similar to
3692 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3693 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3694 * flow's match criteria and actions are built by applying each of the series
3695 * of flow_mod_spec elements included as part of the action.
3697 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3698 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3699 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3700 * bits from a source to a destination. In this case, the header contains
3703 * 15 14 13 12 11 10 0
3704 * +------+---+------+---------------------------------+
3705 * | 0 |src| dst | n_bits |
3706 * +------+---+------+---------------------------------+
3708 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3709 * following table summarizes the meaning of each possible combination.
3710 * Details follow the table:
3713 * --- --- ----------------------------------------------------------
3714 * 0 0 Add match criteria based on value in a field.
3715 * 1 0 Add match criteria based on an immediate value.
3716 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3717 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3718 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3719 * All other combinations are undefined and not allowed.
3721 * The flow_mod_spec header is followed by a source specification and a
3722 * destination specification. The format and meaning of the source
3723 * specification depends on 'src':
3725 * - If 'src' is 0, the source bits are taken from a field in the flow to
3726 * which this action is attached. (This should be a wildcarded field. If
3727 * its value is fully specified then the source bits being copied have
3730 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3731 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3732 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3733 * 'field' and 'ofs' are subject to the same restrictions as the source
3734 * field in NXAST_REG_MOVE.
3736 * - If 'src' is 1, the source bits are a constant value. The source
3737 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3738 * number in network order, the source bits are the 'n_bits'
3739 * least-significant bits. The switch will report an error if other bits
3740 * in the constant are nonzero.
3742 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3743 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3744 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3745 * meaning of the flow_mod_spec depends on 'dst':
3747 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3748 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3749 * packet equal the source bits. 'field' may be any nxm_header with
3750 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3752 * Order is significant. Earlier flow_mod_specs must satisfy any
3753 * prerequisites for matching fields specified later, by copying constant
3754 * values into prerequisite fields.
3756 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3759 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3760 * the new flow. The new flow copies the source bits into
3761 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3764 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3765 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3767 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3768 * empty. It has the following meaning:
3770 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3771 * The new flow outputs to the OpenFlow port specified by the source field.
3772 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3773 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3776 * Resource Management
3777 * -------------------
3779 * A switch has a finite amount of flow table space available for learning.
3780 * When this space is exhausted, no new learning table entries will be learned
3781 * until some existing flow table entries expire. The controller should be
3782 * prepared to handle this by flooding (which can be implemented as a
3783 * low-priority flow).
3785 * If a learned flow matches a single TCP stream with a relatively long
3786 * timeout, one may make the best of resource constraints by setting
3787 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3788 * both, to shorter timeouts. When either of these is specified as a nonzero
3789 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3795 * The following examples give a prose description of the flow_mod_specs along
3796 * with informal notation for how those would be represented and a hex dump of
3797 * the bytes that would be required.
3799 * These examples could work with various nx_action_learn parameters. Typical
3800 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3801 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3803 * 1. Learn input port based on the source MAC, with lookup into
3804 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3806 * Match on in_port=99:
3807 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3808 * ovs_be16(99), 00 63
3809 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3811 * Match Ethernet destination on Ethernet source from packet:
3812 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3813 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3814 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3816 * Set NXM_NX_REG1[16:31] to the packet's input port:
3817 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3818 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3819 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3821 * Given a packet that arrived on port A with Ethernet source address B,
3822 * this would set up the flow "in_port=99, dl_dst=B,
3823 * actions=load:A->NXM_NX_REG1[16..31]".
3825 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3826 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3827 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3829 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3830 * into NXM_NX_REG1[16:31]:
3832 * Match on same VLAN ID as packet:
3833 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3834 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3835 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3837 * Match Ethernet destination on Ethernet source from packet:
3838 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3839 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3840 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3842 * Output to the packet's input port:
3843 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3844 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3846 * Given a packet that arrived on port A with Ethernet source address B in
3847 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3848 * actions=output:A".
3850 * In syntax accepted by ovs-ofctl, this action is:
3851 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3852 * output:NXM_OF_IN_PORT[])
3854 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3855 * 10-second MAC expiration time to make it easier to see what's going on
3857 * ovs-vsctl del-controller br0
3858 * ovs-ofctl del-flows br0
3859 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3860 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3861 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3862 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3863 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3865 * You can then dump the MAC learning table with:
3867 * ovs-ofctl dump-flows br0 table=1
3872 * For best performance, segregate learned flows into a table that is not used
3873 * for any other flows except possibly for a lowest-priority "catch-all" flow
3874 * (a flow with no match criteria). If different learning actions specify
3875 * different match criteria, use different tables for the learned flows.
3877 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3878 * These timeouts apply to the flow that is added, which means that a flow with
3879 * an idle timeout will expire when no traffic has been sent *to* the learned
3880 * address. This is not usually the intent in MAC learning; instead, we want
3881 * the MAC learn entry to expire when no traffic has been sent *from* the
3882 * learned address. Use a hard timeout for that.
3885 * Visibility of Changes
3886 * ---------------------
3888 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3889 * flow translation are visible to flow table lookups made later in the flow
3890 * translation. This means that, in the example above, a MAC learned by the
3891 * learn action in table 0 would be found in table 1 (if the packet being
3892 * processed had the same source and destination MAC address).
3894 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3895 * modify a flow) by a "learn" action are visible only for later flow
3896 * translations, not for later lookups within the same flow translation. In
3897 * the MAC learning example, a MAC learned by the learn action in table 0 would
3898 * not be found in table 1 if the flow translation would resubmit to table 1
3899 * after the processing of the learn action, meaning that if this MAC had not
3900 * been learned before then the packet would be flooded. */
3901 struct nx_action_learn {
3902 ovs_be16 type; /* OFPAT_VENDOR. */
3903 ovs_be16 len; /* At least 24. */
3904 ovs_be32 vendor; /* NX_VENDOR_ID. */
3905 ovs_be16 subtype; /* NXAST_LEARN. */
3906 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3907 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3908 ovs_be16 priority; /* Priority level of flow entry. */
3909 ovs_be64 cookie; /* Cookie for new flow. */
3910 ovs_be16 flags; /* NX_LEARN_F_*. */
3911 uint8_t table_id; /* Table to insert flow entry. */
3912 uint8_t pad; /* Must be zero. */
3913 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3914 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3915 /* Followed by a sequence of flow_mod_spec elements, as described above,
3916 * until the end of the action is reached. */
3918 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3921 get_be16(const void **pp)
3923 const ovs_be16 *p = *pp;
3924 ovs_be16 value = *p;
3930 get_be32(const void **pp)
3932 const ovs_be32 *p = *pp;
3933 ovs_be32 value = get_unaligned_be32(p);
3939 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3941 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3942 sf->ofs = ntohs(get_be16(p));
3943 sf->n_bits = n_bits;
3947 learn_min_len(uint16_t header)
3949 int n_bits = header & NX_LEARN_N_BITS_MASK;
3950 int src_type = header & NX_LEARN_SRC_MASK;
3951 int dst_type = header & NX_LEARN_DST_MASK;
3952 unsigned int min_len;
3955 if (src_type == NX_LEARN_SRC_FIELD) {
3956 min_len += sizeof(ovs_be32); /* src_field */
3957 min_len += sizeof(ovs_be16); /* src_ofs */
3959 min_len += DIV_ROUND_UP(n_bits, 16);
3961 if (dst_type == NX_LEARN_DST_MATCH ||
3962 dst_type == NX_LEARN_DST_LOAD) {
3963 min_len += sizeof(ovs_be32); /* dst_field */
3964 min_len += sizeof(ovs_be16); /* dst_ofs */
3969 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3970 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3972 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3973 enum ofp_version ofp_version OVS_UNUSED,
3974 struct ofpbuf *ofpacts)
3976 struct ofpact_learn *learn;
3977 const void *p, *end;
3980 return OFPERR_OFPBAC_BAD_ARGUMENT;
3983 learn = ofpact_put_LEARN(ofpacts);
3985 learn->idle_timeout = ntohs(nal->idle_timeout);
3986 learn->hard_timeout = ntohs(nal->hard_timeout);
3987 learn->priority = ntohs(nal->priority);
3988 learn->cookie = nal->cookie;
3989 learn->table_id = nal->table_id;
3990 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3991 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3993 learn->flags = ntohs(nal->flags);
3994 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3995 NX_LEARN_F_DELETE_LEARNED)) {
3996 return OFPERR_OFPBAC_BAD_ARGUMENT;
3999 if (learn->table_id == 0xff) {
4000 return OFPERR_OFPBAC_BAD_ARGUMENT;
4003 end = (char *) nal + ntohs(nal->len);
4004 for (p = nal + 1; p != end; ) {
4005 struct ofpact_learn_spec *spec;
4006 uint16_t header = ntohs(get_be16(&p));
4012 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
4013 learn = ofpacts->header;
4016 spec->src_type = header & NX_LEARN_SRC_MASK;
4017 spec->dst_type = header & NX_LEARN_DST_MASK;
4018 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
4020 /* Check for valid src and dst type combination. */
4021 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4022 spec->dst_type == NX_LEARN_DST_LOAD ||
4023 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
4024 spec->src_type == NX_LEARN_SRC_FIELD)) {
4027 return OFPERR_OFPBAC_BAD_ARGUMENT;
4030 /* Check that the arguments don't overrun the end of the action. */
4031 if ((char *) end - (char *) p < learn_min_len(header)) {
4032 return OFPERR_OFPBAC_BAD_LEN;
4035 /* Get the source. */
4036 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4037 get_subfield(spec->n_bits, &p, &spec->src);
4039 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4041 bitwise_copy(p, p_bytes, 0,
4042 &spec->src_imm, sizeof spec->src_imm, 0,
4044 p = (const uint8_t *) p + p_bytes;
4047 /* Get the destination. */
4048 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4049 spec->dst_type == NX_LEARN_DST_LOAD) {
4050 get_subfield(spec->n_bits, &p, &spec->dst);
4053 ofpact_update_len(ofpacts, &learn->ofpact);
4055 if (!is_all_zeros(p, (char *) end - (char *) p)) {
4056 return OFPERR_OFPBAC_BAD_ARGUMENT;
4063 put_be16(struct ofpbuf *b, ovs_be16 x)
4065 ofpbuf_put(b, &x, sizeof x);
4069 put_be32(struct ofpbuf *b, ovs_be32 x)
4071 ofpbuf_put(b, &x, sizeof x);
4075 put_u16(struct ofpbuf *b, uint16_t x)
4077 put_be16(b, htons(x));
4081 put_u32(struct ofpbuf *b, uint32_t x)
4083 put_be32(b, htonl(x));
4087 encode_LEARN(const struct ofpact_learn *learn,
4088 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4090 const struct ofpact_learn_spec *spec;
4091 struct nx_action_learn *nal;
4094 start_ofs = out->size;
4095 nal = put_NXAST_LEARN(out);
4096 nal->idle_timeout = htons(learn->idle_timeout);
4097 nal->hard_timeout = htons(learn->hard_timeout);
4098 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4099 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4100 nal->priority = htons(learn->priority);
4101 nal->cookie = learn->cookie;
4102 nal->flags = htons(learn->flags);
4103 nal->table_id = learn->table_id;
4105 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4106 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4108 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4109 put_u32(out, mf_nxm_header(spec->src.field->id));
4110 put_u16(out, spec->src.ofs);
4112 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4113 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4114 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4115 bits, n_dst_bytes, 0,
4119 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4120 spec->dst_type == NX_LEARN_DST_LOAD) {
4121 put_u32(out, mf_nxm_header(spec->dst.field->id));
4122 put_u16(out, spec->dst.ofs);
4126 pad_ofpat(out, start_ofs);
4129 static char * OVS_WARN_UNUSED_RESULT
4130 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4131 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4133 return learn_parse(arg, ofpacts);
4137 format_LEARN(const struct ofpact_learn *a, struct ds *s)
4142 /* Action structure for NXAST_CONJUNCTION. */
4143 struct nx_action_conjunction {
4144 ovs_be16 type; /* OFPAT_VENDOR. */
4145 ovs_be16 len; /* At least 16. */
4146 ovs_be32 vendor; /* NX_VENDOR_ID. */
4147 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4152 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4155 add_conjunction(struct ofpbuf *out,
4156 uint32_t id, uint8_t clause, uint8_t n_clauses)
4158 struct ofpact_conjunction *oc;
4160 oc = ofpact_put_CONJUNCTION(out);
4162 oc->clause = clause;
4163 oc->n_clauses = n_clauses;
4167 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4168 enum ofp_version ofp_version OVS_UNUSED,
4171 if (nac->n_clauses < 2 || nac->n_clauses > 64
4172 || nac->clause >= nac->n_clauses) {
4173 return OFPERR_NXBAC_BAD_CONJUNCTION;
4175 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4181 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4182 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4184 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4185 nac->clause = oc->clause;
4186 nac->n_clauses = oc->n_clauses;
4187 nac->id = htonl(oc->id);
4191 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4193 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4194 oc->id, oc->clause + 1, oc->n_clauses);
4197 static char * OVS_WARN_UNUSED_RESULT
4198 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4199 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4206 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4207 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4208 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4211 if (n_clauses < 2) {
4212 return xstrdup("conjunction must have at least 2 clauses");
4213 } else if (n_clauses > 64) {
4214 return xstrdup("conjunction must have at most 64 clauses");
4215 } else if (clause < 1) {
4216 return xstrdup("clause index must be positive");
4217 } else if (clause > n_clauses) {
4218 return xstrdup("clause index must be less than or equal to "
4219 "number of clauses");
4222 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4226 /* Action structure for NXAST_MULTIPATH.
4228 * This action performs the following steps in sequence:
4230 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4231 * Refer to the definition of "enum nx_mp_fields" for details.
4233 * The 'basis' value is used as a universal hash parameter, that is,
4234 * different values of 'basis' yield different hash functions. The
4235 * particular universal hash function used is implementation-defined.
4237 * The hashed fields' values are drawn from the current state of the
4238 * flow, including all modifications that have been made by actions up to
4241 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4242 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4245 * The output of the algorithm is 'link', an unsigned integer less than
4246 * or equal to 'max_link'.
4248 * Some algorithms use 'arg' as an additional argument.
4250 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4251 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4254 * The switch will reject actions that have an unknown 'fields', or an unknown
4255 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4256 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4257 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4259 struct nx_action_multipath {
4260 ovs_be16 type; /* OFPAT_VENDOR. */
4261 ovs_be16 len; /* Length is 32. */
4262 ovs_be32 vendor; /* NX_VENDOR_ID. */
4263 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4265 /* What fields to hash and how. */
4266 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4267 ovs_be16 basis; /* Universal hash parameter. */
4270 /* Multipath link choice algorithm to apply to hash value. */
4271 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4272 ovs_be16 max_link; /* Number of output links, minus 1. */
4273 ovs_be32 arg; /* Algorithm-specific argument. */
4276 /* Where to store the result. */
4277 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4278 ovs_be32 dst; /* Destination. */
4280 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4283 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4284 enum ofp_version ofp_version OVS_UNUSED,
4287 uint32_t n_links = ntohs(nam->max_link) + 1;
4288 size_t min_n_bits = log_2_ceil(n_links);
4289 struct ofpact_multipath *mp;
4291 mp = ofpact_put_MULTIPATH(out);
4292 mp->fields = ntohs(nam->fields);
4293 mp->basis = ntohs(nam->basis);
4294 mp->algorithm = ntohs(nam->algorithm);
4295 mp->max_link = ntohs(nam->max_link);
4296 mp->arg = ntohl(nam->arg);
4297 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4298 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4299 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4301 if (!flow_hash_fields_valid(mp->fields)) {
4302 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4303 return OFPERR_OFPBAC_BAD_ARGUMENT;
4304 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4305 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4306 && mp->algorithm != NX_MP_ALG_HRW
4307 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4308 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4309 return OFPERR_OFPBAC_BAD_ARGUMENT;
4310 } else if (mp->dst.n_bits < min_n_bits) {
4311 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4312 "%"PRIu32" links", min_n_bits, n_links);
4313 return OFPERR_OFPBAC_BAD_ARGUMENT;
4316 return multipath_check(mp, NULL);
4320 encode_MULTIPATH(const struct ofpact_multipath *mp,
4321 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4323 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4325 nam->fields = htons(mp->fields);
4326 nam->basis = htons(mp->basis);
4327 nam->algorithm = htons(mp->algorithm);
4328 nam->max_link = htons(mp->max_link);
4329 nam->arg = htonl(mp->arg);
4330 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4331 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4334 static char * OVS_WARN_UNUSED_RESULT
4335 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4336 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4338 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4342 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4344 multipath_format(a, s);
4347 /* Action structure for NXAST_NOTE.
4349 * This action has no effect. It is variable length. The switch does not
4350 * attempt to interpret the user-defined 'note' data in any way. A controller
4351 * can use this action to attach arbitrary metadata to a flow.
4353 * This action might go away in the future.
4355 struct nx_action_note {
4356 ovs_be16 type; /* OFPAT_VENDOR. */
4357 ovs_be16 len; /* A multiple of 8, but at least 16. */
4358 ovs_be32 vendor; /* NX_VENDOR_ID. */
4359 ovs_be16 subtype; /* NXAST_NOTE. */
4360 uint8_t note[6]; /* Start of user-defined data. */
4361 /* Possibly followed by additional user-defined data. */
4363 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4366 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4367 enum ofp_version ofp_version OVS_UNUSED,
4370 struct ofpact_note *note;
4371 unsigned int length;
4373 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4374 note = ofpact_put(out, OFPACT_NOTE,
4375 offsetof(struct ofpact_note, data) + length);
4376 note->length = length;
4377 memcpy(note->data, nan->note, length);
4383 encode_NOTE(const struct ofpact_note *note,
4384 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4386 size_t start_ofs = out->size;
4387 struct nx_action_note *nan;
4388 unsigned int remainder;
4391 put_NXAST_NOTE(out);
4392 out->size = out->size - sizeof nan->note;
4394 ofpbuf_put(out, note->data, note->length);
4396 len = out->size - start_ofs;
4397 remainder = len % OFP_ACTION_ALIGN;
4399 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
4401 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
4402 nan->len = htons(out->size - start_ofs);
4405 static char * OVS_WARN_UNUSED_RESULT
4406 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4407 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4409 struct ofpact_note *note;
4411 note = ofpact_put_NOTE(ofpacts);
4412 while (*arg != '\0') {
4423 byte = hexits_value(arg, 2, &ok);
4425 return xstrdup("bad hex digit in `note' argument");
4427 ofpbuf_put(ofpacts, &byte, 1);
4429 note = ofpacts->header;
4434 ofpact_update_len(ofpacts, ¬e->ofpact);
4439 format_NOTE(const struct ofpact_note *a, struct ds *s)
4443 ds_put_cstr(s, "note:");
4444 for (i = 0; i < a->length; i++) {
4446 ds_put_char(s, '.');
4448 ds_put_format(s, "%02"PRIx8, a->data[i]);
4455 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4457 ofpact_put_EXIT(out);
4462 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4463 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4465 put_NXAST_EXIT(out);
4468 static char * OVS_WARN_UNUSED_RESULT
4469 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4470 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4472 ofpact_put_EXIT(ofpacts);
4477 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4479 ds_put_cstr(s, "exit");
4482 /* Unroll xlate action. */
4485 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4486 enum ofp_version ofp_version OVS_UNUSED,
4487 struct ofpbuf *out OVS_UNUSED)
4492 static char * OVS_WARN_UNUSED_RESULT
4493 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4494 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4501 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4504 ds_put_cstr(s, "unroll_xlate");
4507 /* Action structure for NXAST_SAMPLE.
4509 * Samples matching packets with the given probability and sends them
4510 * each to the set of collectors identified with the given ID. The
4511 * probability is expressed as a number of packets to be sampled out
4512 * of USHRT_MAX packets, and must be >0.
4514 * When sending packet samples to IPFIX collectors, the IPFIX flow
4515 * record sent for each sampled packet is associated with the given
4516 * observation domain ID and observation point ID. Each IPFIX flow
4517 * record contain the sampled packet's headers when executing this
4518 * rule. If a sampled packet's headers are modified by previous
4519 * actions in the flow, those modified headers are sent. */
4520 struct nx_action_sample {
4521 ovs_be16 type; /* OFPAT_VENDOR. */
4522 ovs_be16 len; /* Length is 24. */
4523 ovs_be32 vendor; /* NX_VENDOR_ID. */
4524 ovs_be16 subtype; /* NXAST_SAMPLE. */
4525 ovs_be16 probability; /* Fraction of packets to sample. */
4526 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4527 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4528 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4530 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4533 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4534 enum ofp_version ofp_version OVS_UNUSED,
4537 struct ofpact_sample *sample;
4539 sample = ofpact_put_SAMPLE(out);
4540 sample->probability = ntohs(nas->probability);
4541 sample->collector_set_id = ntohl(nas->collector_set_id);
4542 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4543 sample->obs_point_id = ntohl(nas->obs_point_id);
4545 if (sample->probability == 0) {
4546 return OFPERR_OFPBAC_BAD_ARGUMENT;
4553 encode_SAMPLE(const struct ofpact_sample *sample,
4554 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4556 struct nx_action_sample *nas;
4558 nas = put_NXAST_SAMPLE(out);
4559 nas->probability = htons(sample->probability);
4560 nas->collector_set_id = htonl(sample->collector_set_id);
4561 nas->obs_domain_id = htonl(sample->obs_domain_id);
4562 nas->obs_point_id = htonl(sample->obs_point_id);
4565 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4566 * action to 'ofpacts'.
4568 * Returns NULL if successful, otherwise a malloc()'d string describing the
4569 * error. The caller is responsible for freeing the returned string. */
4570 static char * OVS_WARN_UNUSED_RESULT
4571 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4572 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4574 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4577 while (ofputil_parse_key_value(&arg, &key, &value)) {
4580 if (!strcmp(key, "probability")) {
4581 error = str_to_u16(value, "probability", &os->probability);
4582 if (!error && os->probability == 0) {
4583 error = xasprintf("invalid probability value \"%s\"", value);
4585 } else if (!strcmp(key, "collector_set_id")) {
4586 error = str_to_u32(value, &os->collector_set_id);
4587 } else if (!strcmp(key, "obs_domain_id")) {
4588 error = str_to_u32(value, &os->obs_domain_id);
4589 } else if (!strcmp(key, "obs_point_id")) {
4590 error = str_to_u32(value, &os->obs_point_id);
4592 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4599 if (os->probability == 0) {
4600 return xstrdup("non-zero \"probability\" must be specified on sample");
4606 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4608 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4609 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4610 a->probability, a->collector_set_id,
4611 a->obs_domain_id, a->obs_point_id);
4614 /* debug_recirc instruction. */
4616 static bool enable_debug;
4619 ofpact_dummy_enable(void)
4621 enable_debug = true;
4625 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4627 if (!enable_debug) {
4628 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4631 ofpact_put_DEBUG_RECIRC(out);
4636 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4637 enum ofp_version ofp_version OVS_UNUSED,
4640 put_NXAST_DEBUG_RECIRC(out);
4643 static char * OVS_WARN_UNUSED_RESULT
4644 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4645 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4647 ofpact_put_DEBUG_RECIRC(ofpacts);
4652 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4654 ds_put_cstr(s, "debug_recirc");
4657 /* Action structure for NXAST_CT.
4659 * Pass traffic to the connection tracker.
4661 * There are two important concepts to understanding the connection tracking
4662 * interface: Packet state and Connection state. Packets may be "Untracked" or
4663 * "Tracked". Connections may be "Uncommitted" or "Committed".
4667 * Untracked packets have not yet passed through the connection tracker,
4668 * and the connection state for such packets is unknown. In most cases,
4669 * packets entering the OpenFlow pipeline will initially be in the
4670 * untracked state. Untracked packets may become tracked by executing
4671 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4672 * about the connection available, in particular the connection state.
4674 * Tracked packets have previously passed through the connection tracker.
4675 * These packets will remain tracked through until the end of the OpenFlow
4676 * pipeline. Tracked packets which have NXAST_CT executed with a
4677 * "recirc_table" specified will return to the tracked state.
4679 * The packet state is only significant for the duration of packet
4680 * processing within the OpenFlow pipeline.
4682 * - Connection State:
4684 * Multiple packets may be associated with a single connection. Initially,
4685 * all connections are uncommitted. The connection state corresponding to
4686 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4688 * Uncommitted connections have no state stored about them. Uncommitted
4689 * connections may transition into the committed state by executing
4690 * NXAST_CT with the NX_CT_F_COMMIT flag.
4692 * Once a connection becomes committed, information may be gathered about
4693 * the connection by passing subsequent packets through the connection
4694 * tracker, and the state of the connection will be stored beyond the
4695 * lifetime of packet processing.
4697 * Connections may transition back into the uncommitted state due to
4698 * external timers, or due to the contents of packets that are sent to the
4699 * connection tracker. This behaviour is outside of the scope of the
4700 * OpenFlow interface.
4702 * The "zone" specifies a context within which the tracking is done:
4704 * The connection tracking zone is a 16-bit number. Each zone is an
4705 * independent connection tracking context. The connection state for each
4706 * connection is completely separate for each zone, so if a connection
4707 * is committed to zone A, then it will remain uncommitted in zone B.
4708 * If NXAST_CT is executed with the same zone multiple times, later
4709 * executions have no effect.
4711 * If 'zone_src' is nonzero, this specifies that the zone should be
4712 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4713 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4714 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4715 * are the same as the acceptable nxm_header values for the 'src' field of
4718 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4719 * connection tracking zone.
4721 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4723 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4724 * packet will be logically cloned prior to executing this action. One
4725 * copy will be sent to the connection tracker, then will be re-injected
4726 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
4727 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4728 * fields will be populated. The original instance of the packet will
4729 * continue the current actions list. This can be thought of as similar to
4730 * the effect of the "output" action: One copy is sent out (in this case,
4731 * to the connection tracker), but the current copy continues processing.
4733 * It is strongly recommended that this table is later than the current
4734 * table, to prevent loops.
4736 * The "alg" attaches protocol-specific behaviour to this action:
4738 * The ALG is a 16-bit number which specifies that additional
4739 * processing should be applied to this traffic.
4741 * Protocol | Value | Meaning
4742 * --------------------------------------------------------------------
4743 * None | 0 | No protocol-specific behaviour.
4744 * FTP | 21 | Parse FTP control connections and observe the
4745 * | | negotiation of related data connections.
4746 * Other | Other | Unsupported protocols.
4748 * By way of example, if FTP control connections have this action applied
4749 * with the ALG set to FTP (21), then the connection tracker will observe
4750 * the negotiation of data connections. This allows the connection
4751 * tracker to identify subsequent data connections as "related" to this
4752 * existing connection. The "related" flag will be populated in the
4753 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
4756 * Zero or more actions may immediately follow this action. These actions will
4757 * be executed within the context of the connection tracker, and they require
4758 * the NX_CT_F_COMMIT flag to be set.
4760 struct nx_action_conntrack {
4761 ovs_be16 type; /* OFPAT_VENDOR. */
4762 ovs_be16 len; /* At least 24. */
4763 ovs_be32 vendor; /* NX_VENDOR_ID. */
4764 ovs_be16 subtype; /* NXAST_CT. */
4765 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
4766 * Unspecified flag bits must be zero. */
4767 ovs_be32 zone_src; /* Connection tracking context. */
4769 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
4770 ovs_be16 zone_imm; /* Immediate value for zone. */
4772 uint8_t recirc_table; /* Recirculate to a specific table, or
4773 NX_CT_RECIRC_NONE for no recirculation. */
4774 uint8_t pad[3]; /* Zeroes */
4775 ovs_be16 alg; /* Well-known port number for the protocol.
4776 * 0 indicates no ALG is required. */
4777 /* Followed by a sequence of zero or more OpenFlow actions. The length of
4778 * these is included in 'len'. */
4780 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
4783 decode_ct_zone(const struct nx_action_conntrack *nac,
4784 struct ofpact_conntrack *out)
4786 if (nac->zone_src) {
4789 out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
4790 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
4791 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
4792 error = mf_check_src(&out->zone_src, NULL);
4797 if (out->zone_src.n_bits != 16) {
4798 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
4799 out->zone_src.n_bits);
4800 return OFPERR_OFPBAC_BAD_SET_LEN;
4803 out->zone_src.field = NULL;
4804 out->zone_imm = ntohs(nac->zone_imm);
4811 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
4812 enum ofp_version ofp_version, struct ofpbuf *out)
4814 const size_t ct_offset = ofpacts_pull(out);
4815 struct ofpact_conntrack *conntrack;
4816 struct ofpbuf openflow;
4819 conntrack = ofpact_put_CT(out);
4820 conntrack->flags = ntohs(nac->flags);
4821 error = decode_ct_zone(nac, conntrack);
4825 conntrack->recirc_table = nac->recirc_table;
4826 conntrack->alg = ntohs(nac->alg);
4828 ofpbuf_pull(out, sizeof(*conntrack));
4830 ofpbuf_use_const(&openflow, nac + 1, ntohs(nac->len) - sizeof(*nac));
4831 error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
4833 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4839 conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
4840 out->header = &conntrack->ofpact;
4841 ofpact_update_len(out, &conntrack->ofpact);
4843 if (conntrack->ofpact.len > sizeof(*conntrack)
4844 && !(conntrack->flags & NX_CT_F_COMMIT)) {
4845 VLOG_WARN_RL(&rl, "CT action requires commit flag if actions are "
4847 error = OFPERR_OFPBAC_BAD_ARGUMENT;
4851 ofpbuf_push_uninit(out, ct_offset);
4856 encode_CT(const struct ofpact_conntrack *conntrack,
4857 enum ofp_version ofp_version, struct ofpbuf *out)
4859 struct nx_action_conntrack *nac;
4860 const size_t ofs = out->size;
4863 nac = put_NXAST_CT(out);
4864 nac->flags = htons(conntrack->flags);
4865 if (conntrack->zone_src.field) {
4866 nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
4867 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
4868 conntrack->zone_src.n_bits);
4870 nac->zone_src = htonl(0);
4871 nac->zone_imm = htons(conntrack->zone_imm);
4873 nac->recirc_table = conntrack->recirc_table;
4874 nac->alg = htons(conntrack->alg);
4876 len = ofpacts_put_openflow_actions(conntrack->actions,
4877 ofpact_ct_get_action_len(conntrack),
4879 len += sizeof(*nac);
4880 nac = ofpbuf_at(out, ofs, sizeof(*nac));
4881 nac->len = htons(len);
4884 /* Parses 'arg' as the argument to a "ct" action, and appends such an
4885 * action to 'ofpacts'.
4887 * Returns NULL if successful, otherwise a malloc()'d string describing the
4888 * error. The caller is responsible for freeing the returned string. */
4889 static char * OVS_WARN_UNUSED_RESULT
4890 parse_CT(char *arg, struct ofpbuf *ofpacts,
4891 enum ofputil_protocol *usable_protocols)
4893 const size_t ct_offset = ofpacts_pull(ofpacts);
4894 struct ofpact_conntrack *oc;
4898 oc = ofpact_put_CT(ofpacts);
4900 oc->recirc_table = NX_CT_RECIRC_NONE;
4901 while (ofputil_parse_key_value(&arg, &key, &value)) {
4902 if (!strcmp(key, "commit")) {
4903 oc->flags |= NX_CT_F_COMMIT;
4904 } else if (!strcmp(key, "table")) {
4905 error = str_to_u8(value, "recirc_table", &oc->recirc_table);
4906 if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
4907 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
4909 } else if (!strcmp(key, "zone")) {
4910 error = str_to_u16(value, "zone", &oc->zone_imm);
4914 error = mf_parse_subfield(&oc->zone_src, value);
4919 } else if (!strcmp(key, "alg")) {
4920 error = str_to_connhelper(value, &oc->alg);
4921 } else if (!strcmp(key, "exec")) {
4922 /* Hide existing actions from ofpacts_parse_copy(), so the
4923 * nesting can be handled transparently. */
4924 enum ofputil_protocol usable_protocols2;
4926 ofpbuf_pull(ofpacts, sizeof(*oc));
4927 /* Initializes 'usable_protocol2', fold it back to
4928 * '*usable_protocols' afterwards, so that we do not lose
4929 * restrictions already in there. */
4930 error = ofpacts_parse_copy(value, ofpacts, &usable_protocols2,
4932 *usable_protocols &= usable_protocols2;
4933 ofpact_pad(ofpacts);
4934 ofpacts->header = ofpbuf_push_uninit(ofpacts, sizeof(*oc));
4935 oc = ofpacts->header;
4937 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
4944 ofpact_update_len(ofpacts, &oc->ofpact);
4945 ofpbuf_push_uninit(ofpacts, ct_offset);
4950 format_alg(int port, struct ds *s)
4952 if (port == IPPORT_FTP) {
4953 ds_put_format(s, "alg=ftp,");
4955 ds_put_format(s, "alg=%d,", port);
4960 format_CT(const struct ofpact_conntrack *a, struct ds *s)
4962 ds_put_cstr(s, "ct(");
4963 if (a->flags & NX_CT_F_COMMIT) {
4964 ds_put_cstr(s, "commit,");
4966 if (a->recirc_table != NX_CT_RECIRC_NONE) {
4967 ds_put_format(s, "table=%"PRIu8",", a->recirc_table);
4969 if (a->zone_src.field) {
4970 ds_put_format(s, "zone=");
4971 mf_format_subfield(&a->zone_src, s);
4972 ds_put_char(s, ',');
4973 } else if (a->zone_imm) {
4974 ds_put_format(s, "zone=%"PRIu16",", a->zone_imm);
4976 if (ofpact_ct_get_action_len(a)) {
4977 ds_put_cstr(s, "exec(");
4978 ofpacts_format(a->actions, ofpact_ct_get_action_len(a), s);
4979 ds_put_format(s, "),");
4981 format_alg(a->alg, s);
4983 ds_put_char(s, ')');
4986 /* Meter instruction. */
4989 encode_METER(const struct ofpact_meter *meter,
4990 enum ofp_version ofp_version, struct ofpbuf *out)
4992 if (ofp_version >= OFP13_VERSION) {
4993 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
4997 static char * OVS_WARN_UNUSED_RESULT
4998 parse_METER(char *arg, struct ofpbuf *ofpacts,
4999 enum ofputil_protocol *usable_protocols)
5001 *usable_protocols &= OFPUTIL_P_OF13_UP;
5002 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
5006 format_METER(const struct ofpact_meter *a, struct ds *s)
5008 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
5011 /* Clear-Actions instruction. */
5014 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
5015 enum ofp_version ofp_version OVS_UNUSED,
5016 struct ofpbuf *out OVS_UNUSED)
5018 if (ofp_version > OFP10_VERSION) {
5019 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
5023 static char * OVS_WARN_UNUSED_RESULT
5024 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
5025 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5027 ofpact_put_CLEAR_ACTIONS(ofpacts);
5032 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
5034 ds_put_cstr(s, "clear_actions");
5037 /* Write-Actions instruction. */
5040 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
5041 enum ofp_version ofp_version, struct ofpbuf *out)
5043 if (ofp_version > OFP10_VERSION) {
5044 const size_t ofs = out->size;
5046 instruction_put_OFPIT11_WRITE_ACTIONS(out);
5047 ofpacts_put_openflow_actions(actions->actions,
5048 ofpact_nest_get_action_len(actions),
5050 ofpacts_update_instruction_actions(out, ofs);
5054 static char * OVS_WARN_UNUSED_RESULT
5055 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
5056 enum ofputil_protocol *usable_protocols)
5058 size_t ofs = ofpacts_pull(ofpacts);
5059 struct ofpact_nest *on;
5062 /* Add a Write-Actions instruction and then pull it off. */
5063 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
5064 ofpbuf_pull(ofpacts, sizeof *on);
5066 /* Parse nested actions.
5068 * We pulled off "write-actions" and the previous actions because the
5069 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
5070 * that it doesn't actually include the nested actions. That means that
5071 * ofpacts_parse() would reject them as being part of an Apply-Actions that
5072 * follows a Write-Actions, which is an invalid order. */
5073 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
5074 OFPACT_WRITE_ACTIONS);
5076 /* Put the Write-Actions back on and update its length. */
5077 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
5078 on->ofpact.len = ofpacts->size;
5080 /* Put any previous actions or instructions back on. */
5081 ofpbuf_push_uninit(ofpacts, ofs);
5087 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
5089 ds_put_cstr(s, "write_actions(");
5090 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
5091 ds_put_char(s, ')');
5094 /* Action structure for NXAST_WRITE_METADATA.
5096 * Modifies the 'mask' bits of the metadata value. */
5097 struct nx_action_write_metadata {
5098 ovs_be16 type; /* OFPAT_VENDOR. */
5099 ovs_be16 len; /* Length is 32. */
5100 ovs_be32 vendor; /* NX_VENDOR_ID. */
5101 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
5102 uint8_t zeros[6]; /* Must be zero. */
5103 ovs_be64 metadata; /* Metadata register. */
5104 ovs_be64 mask; /* Metadata mask. */
5106 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
5109 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
5110 enum ofp_version ofp_version OVS_UNUSED,
5113 struct ofpact_metadata *om;
5115 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
5116 return OFPERR_NXBRC_MUST_BE_ZERO;
5119 om = ofpact_put_WRITE_METADATA(out);
5120 om->metadata = nawm->metadata;
5121 om->mask = nawm->mask;
5127 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
5128 enum ofp_version ofp_version, struct ofpbuf *out)
5130 if (ofp_version == OFP10_VERSION) {
5131 struct nx_action_write_metadata *nawm;
5133 nawm = put_NXAST_WRITE_METADATA(out);
5134 nawm->metadata = metadata->metadata;
5135 nawm->mask = metadata->mask;
5137 struct ofp11_instruction_write_metadata *oiwm;
5139 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
5140 oiwm->metadata = metadata->metadata;
5141 oiwm->metadata_mask = metadata->mask;
5145 static char * OVS_WARN_UNUSED_RESULT
5146 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
5147 enum ofputil_protocol *usable_protocols)
5149 struct ofpact_metadata *om;
5150 char *mask = strchr(arg, '/');
5152 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
5154 om = ofpact_put_WRITE_METADATA(ofpacts);
5159 error = str_to_be64(mask + 1, &om->mask);
5164 om->mask = OVS_BE64_MAX;
5167 return str_to_be64(arg, &om->metadata);
5171 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5173 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
5174 if (a->mask != OVS_BE64_MAX) {
5175 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5179 /* Goto-Table instruction. */
5182 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5183 enum ofp_version ofp_version, struct ofpbuf *out)
5185 if (ofp_version == OFP10_VERSION) {
5186 struct nx_action_resubmit *nar;
5188 nar = put_NXAST_RESUBMIT_TABLE(out);
5189 nar->table = goto_table->table_id;
5190 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5192 struct ofp11_instruction_goto_table *oigt;
5194 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5195 oigt->table_id = goto_table->table_id;
5196 memset(oigt->pad, 0, sizeof oigt->pad);
5200 static char * OVS_WARN_UNUSED_RESULT
5201 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5202 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5204 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5205 char *table_s = strsep(&arg, ",");
5206 if (!table_s || !table_s[0]) {
5207 return xstrdup("instruction goto-table needs table id");
5209 return str_to_u8(table_s, "table", &ogt->table_id);
5213 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5215 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
5219 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
5220 const struct ofp_action_header *bad_action, enum ofperr error)
5222 if (!VLOG_DROP_WARN(&rl)) {
5226 ds_put_hex_dump(&s, actions, actions_len, 0, false);
5227 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5228 (char *)bad_action - (char *)actions,
5229 ofperr_get_name(error), ds_cstr(&s));
5235 ofpacts_decode(const void *actions, size_t actions_len,
5236 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5238 struct ofpbuf openflow;
5240 ofpbuf_use_const(&openflow, actions, actions_len);
5241 while (openflow.size) {
5242 const struct ofp_action_header *action = openflow.data;
5243 enum ofp_raw_action_type raw;
5247 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5249 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
5253 log_bad_action(actions, actions_len, action, error);
5258 ofpact_pad(ofpacts);
5263 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5264 unsigned int actions_len,
5265 enum ofp_version version,
5266 uint32_t allowed_ovsinsts,
5267 struct ofpbuf *ofpacts,
5268 enum ofpact_type outer_action)
5270 const struct ofp_action_header *actions;
5273 if (!outer_action) {
5274 ofpbuf_clear(ofpacts);
5277 if (actions_len % OFP_ACTION_ALIGN != 0) {
5278 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5279 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5280 return OFPERR_OFPBRC_BAD_LEN;
5283 actions = ofpbuf_try_pull(openflow, actions_len);
5284 if (actions == NULL) {
5285 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
5286 "remaining message length (%"PRIu32")",
5287 actions_len, openflow->size);
5288 return OFPERR_OFPBRC_BAD_LEN;
5291 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5293 ofpbuf_clear(ofpacts);
5297 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
5300 ofpbuf_clear(ofpacts);
5305 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
5306 * front of 'openflow' into ofpacts. On success, replaces any existing content
5307 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
5308 * Returns 0 if successful, otherwise an OpenFlow error.
5310 * Actions are processed according to their OpenFlow version which
5311 * is provided in the 'version' parameter.
5313 * In most places in OpenFlow, actions appear encapsulated in instructions, so
5314 * you should call ofpacts_pull_openflow_instructions() instead of this
5317 * The parsed actions are valid generically, but they may not be valid in a
5318 * specific context. For example, port numbers up to OFPP_MAX are valid
5319 * generically, but specific datapaths may only support port numbers in a
5320 * smaller range. Use ofpacts_check() to additional check whether actions are
5321 * valid in a specific context. */
5323 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
5324 unsigned int actions_len,
5325 enum ofp_version version,
5326 struct ofpbuf *ofpacts)
5328 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
5329 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
5333 /* OpenFlow 1.1 actions. */
5336 /* True if an action sets the value of a field
5337 * in a way that is compatibile with the action set.
5338 * The field can be set via either a set or a move action.
5339 * False otherwise. */
5341 ofpact_is_set_or_move_action(const struct ofpact *a)
5344 case OFPACT_SET_FIELD:
5345 case OFPACT_REG_MOVE:
5346 case OFPACT_SET_ETH_DST:
5347 case OFPACT_SET_ETH_SRC:
5348 case OFPACT_SET_IP_DSCP:
5349 case OFPACT_SET_IP_ECN:
5350 case OFPACT_SET_IP_TTL:
5351 case OFPACT_SET_IPV4_DST:
5352 case OFPACT_SET_IPV4_SRC:
5353 case OFPACT_SET_L4_DST_PORT:
5354 case OFPACT_SET_L4_SRC_PORT:
5355 case OFPACT_SET_MPLS_LABEL:
5356 case OFPACT_SET_MPLS_TC:
5357 case OFPACT_SET_MPLS_TTL:
5358 case OFPACT_SET_QUEUE:
5359 case OFPACT_SET_TUNNEL:
5360 case OFPACT_SET_VLAN_PCP:
5361 case OFPACT_SET_VLAN_VID:
5364 case OFPACT_CLEAR_ACTIONS:
5366 case OFPACT_CONTROLLER:
5367 case OFPACT_DEC_MPLS_TTL:
5368 case OFPACT_DEC_TTL:
5369 case OFPACT_ENQUEUE:
5371 case OFPACT_UNROLL_XLATE:
5372 case OFPACT_FIN_TIMEOUT:
5373 case OFPACT_GOTO_TABLE:
5376 case OFPACT_CONJUNCTION:
5378 case OFPACT_MULTIPATH:
5381 case OFPACT_OUTPUT_REG:
5382 case OFPACT_POP_MPLS:
5383 case OFPACT_POP_QUEUE:
5384 case OFPACT_PUSH_MPLS:
5385 case OFPACT_PUSH_VLAN:
5386 case OFPACT_RESUBMIT:
5388 case OFPACT_STACK_POP:
5389 case OFPACT_STACK_PUSH:
5390 case OFPACT_STRIP_VLAN:
5391 case OFPACT_WRITE_ACTIONS:
5392 case OFPACT_WRITE_METADATA:
5393 case OFPACT_DEBUG_RECIRC:
5400 /* True if an action is allowed in the action set.
5401 * False otherwise. */
5403 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
5406 case OFPACT_DEC_MPLS_TTL:
5407 case OFPACT_DEC_TTL:
5410 case OFPACT_POP_MPLS:
5411 case OFPACT_PUSH_MPLS:
5412 case OFPACT_PUSH_VLAN:
5413 case OFPACT_REG_MOVE:
5414 case OFPACT_SET_FIELD:
5415 case OFPACT_SET_ETH_DST:
5416 case OFPACT_SET_ETH_SRC:
5417 case OFPACT_SET_IP_DSCP:
5418 case OFPACT_SET_IP_ECN:
5419 case OFPACT_SET_IP_TTL:
5420 case OFPACT_SET_IPV4_DST:
5421 case OFPACT_SET_IPV4_SRC:
5422 case OFPACT_SET_L4_DST_PORT:
5423 case OFPACT_SET_L4_SRC_PORT:
5424 case OFPACT_SET_MPLS_LABEL:
5425 case OFPACT_SET_MPLS_TC:
5426 case OFPACT_SET_MPLS_TTL:
5427 case OFPACT_SET_QUEUE:
5428 case OFPACT_SET_TUNNEL:
5429 case OFPACT_SET_VLAN_PCP:
5430 case OFPACT_SET_VLAN_VID:
5431 case OFPACT_STRIP_VLAN:
5434 /* In general these actions are excluded because they are not part of
5435 * the OpenFlow specification nor map to actions that are defined in
5436 * the specification. Thus the order in which they should be applied
5437 * in the action set is undefined. */
5439 case OFPACT_CONTROLLER:
5441 case OFPACT_ENQUEUE:
5443 case OFPACT_UNROLL_XLATE:
5444 case OFPACT_FIN_TIMEOUT:
5446 case OFPACT_CONJUNCTION:
5447 case OFPACT_MULTIPATH:
5449 case OFPACT_OUTPUT_REG:
5450 case OFPACT_POP_QUEUE:
5451 case OFPACT_RESUBMIT:
5453 case OFPACT_STACK_POP:
5454 case OFPACT_STACK_PUSH:
5455 case OFPACT_DEBUG_RECIRC:
5457 /* The action set may only include actions and thus
5458 * may not include any instructions */
5459 case OFPACT_CLEAR_ACTIONS:
5460 case OFPACT_GOTO_TABLE:
5462 case OFPACT_WRITE_ACTIONS:
5463 case OFPACT_WRITE_METADATA:
5470 /* Append ofpact 'a' onto the tail of 'out' */
5472 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
5474 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
5477 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
5479 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
5480 enum ofpact_type filter)
5482 const struct ofpact *target;
5483 const struct ofpact *a;
5486 OFPACT_FOR_EACH (a, in->data, in->size) {
5487 if (a->type == filter) {
5492 ofpact_copy(out, target);
5494 return target != NULL;
5497 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5498 * The order of appended ofpacts is preserved between 'in' and 'out' */
5500 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5501 bool (*filter)(const struct ofpact *))
5503 const struct ofpact *a;
5505 OFPACT_FOR_EACH (a, in->data, in->size) {
5507 ofpact_copy(out, a);
5512 /* Reads 'action_set', which contains ofpacts accumulated by
5513 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5514 * executed directly into 'action_list'. (These names correspond to the
5515 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5517 * In general this involves appending the last instance of each action that is
5518 * admissible in the action set in the order described in the OpenFlow
5522 * + output action is only appended if no group action was present in 'in'.
5523 * + As a simplification all set actions are copied in the order the are
5524 * provided in 'in' as many set actions applied to a field has the same
5525 * affect as only applying the last action that sets a field and
5526 * duplicates are removed by do_xlate_actions().
5527 * This has an unwanted side-effect of compsoting multiple
5528 * LOAD_REG actions that touch different regions of the same field. */
5530 ofpacts_execute_action_set(struct ofpbuf *action_list,
5531 const struct ofpbuf *action_set)
5533 /* The OpenFlow spec "Action Set" section specifies this order. */
5534 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5535 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5536 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5537 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5538 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5539 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5540 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5541 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5543 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5544 * we should execute only OFPACT_GROUP.
5546 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5547 * all the actions because there's no point in modifying a packet that will
5548 * not be sent anywhere. */
5549 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5550 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5551 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5552 ofpbuf_clear(action_list);
5558 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5559 size_t n_in, enum ofp_version version,
5564 size_t start = out->size;
5566 error = ofpacts_decode(in, n_in, version, out);
5572 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5573 if (!ofpact_is_allowed_in_actions_set(a)) {
5574 VLOG_WARN_RL(&rl, "disallowed action in action set");
5575 return OFPERR_OFPBAC_BAD_TYPE;
5582 /* OpenFlow 1.1 instructions. */
5584 struct instruction_type_info {
5585 enum ovs_instruction_type type;
5589 static const struct instruction_type_info inst_info[] = {
5590 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5596 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5598 return inst_info[type].name;
5602 ovs_instruction_type_from_name(const char *name)
5604 const struct instruction_type_info *p;
5605 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5606 if (!strcasecmp(name, p->name)) {
5613 enum ovs_instruction_type
5614 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5618 return OVSINST_OFPIT13_METER;
5619 case OFPACT_CLEAR_ACTIONS:
5620 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5621 case OFPACT_WRITE_ACTIONS:
5622 return OVSINST_OFPIT11_WRITE_ACTIONS;
5623 case OFPACT_WRITE_METADATA:
5624 return OVSINST_OFPIT11_WRITE_METADATA;
5625 case OFPACT_GOTO_TABLE:
5626 return OVSINST_OFPIT11_GOTO_TABLE;
5629 case OFPACT_CONTROLLER:
5630 case OFPACT_ENQUEUE:
5631 case OFPACT_OUTPUT_REG:
5633 case OFPACT_SET_VLAN_VID:
5634 case OFPACT_SET_VLAN_PCP:
5635 case OFPACT_STRIP_VLAN:
5636 case OFPACT_PUSH_VLAN:
5637 case OFPACT_SET_ETH_SRC:
5638 case OFPACT_SET_ETH_DST:
5639 case OFPACT_SET_IPV4_SRC:
5640 case OFPACT_SET_IPV4_DST:
5641 case OFPACT_SET_IP_DSCP:
5642 case OFPACT_SET_IP_ECN:
5643 case OFPACT_SET_IP_TTL:
5644 case OFPACT_SET_L4_SRC_PORT:
5645 case OFPACT_SET_L4_DST_PORT:
5646 case OFPACT_REG_MOVE:
5647 case OFPACT_SET_FIELD:
5648 case OFPACT_STACK_PUSH:
5649 case OFPACT_STACK_POP:
5650 case OFPACT_DEC_TTL:
5651 case OFPACT_SET_MPLS_LABEL:
5652 case OFPACT_SET_MPLS_TC:
5653 case OFPACT_SET_MPLS_TTL:
5654 case OFPACT_DEC_MPLS_TTL:
5655 case OFPACT_PUSH_MPLS:
5656 case OFPACT_POP_MPLS:
5657 case OFPACT_SET_TUNNEL:
5658 case OFPACT_SET_QUEUE:
5659 case OFPACT_POP_QUEUE:
5660 case OFPACT_FIN_TIMEOUT:
5661 case OFPACT_RESUBMIT:
5663 case OFPACT_CONJUNCTION:
5664 case OFPACT_MULTIPATH:
5667 case OFPACT_UNROLL_XLATE:
5669 case OFPACT_DEBUG_RECIRC:
5672 return OVSINST_OFPIT11_APPLY_ACTIONS;
5677 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
5678 const uint16_t inst_type)
5680 switch (inst_type) {
5682 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5684 *instruction_type = OVSINST_##ENUM; \
5690 return OFPERR_OFPBIC_UNKNOWN_INST;
5694 /* Two-way translation between OVS's internal "OVSINST_*" representation of
5695 * instructions and the "OFPIT_*" representation used in OpenFlow. */
5696 struct ovsinst_map {
5697 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
5698 int ofpit; /* OFPIT_* number from OpenFlow spec. */
5701 static const struct ovsinst_map *
5702 get_ovsinst_map(enum ofp_version version)
5704 /* OpenFlow 1.1 and 1.2 instructions. */
5705 static const struct ovsinst_map of11[] = {
5706 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5707 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5708 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5709 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5710 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5714 /* OpenFlow 1.3+ instructions. */
5715 static const struct ovsinst_map of13[] = {
5716 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
5717 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
5718 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
5719 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
5720 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
5721 { OVSINST_OFPIT13_METER, 6 },
5725 return version < OFP13_VERSION ? of11 : of13;
5728 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
5729 * values, into a bitmap of instructions suitable for OpenFlow 'version'
5730 * (OFP11_VERSION or later), and returns the result. */
5732 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
5734 uint32_t ofpit_bitmap = 0;
5735 const struct ovsinst_map *x;
5737 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5738 if (ovsinst_bitmap & (1u << x->ovsinst)) {
5739 ofpit_bitmap |= 1u << x->ofpit;
5742 return htonl(ofpit_bitmap);
5745 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
5746 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
5747 * correspond to OVSINST_* values, and returns the result. */
5749 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
5751 uint32_t ovsinst_bitmap = 0;
5752 const struct ovsinst_map *x;
5754 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
5755 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
5756 ovsinst_bitmap |= 1u << x->ovsinst;
5759 return ovsinst_bitmap;
5762 static inline struct ofp11_instruction *
5763 instruction_next(const struct ofp11_instruction *inst)
5765 return ((struct ofp11_instruction *) (void *)
5766 ((uint8_t *) inst + ntohs(inst->len)));
5770 instruction_is_valid(const struct ofp11_instruction *inst,
5771 size_t n_instructions)
5773 uint16_t len = ntohs(inst->len);
5774 return (!(len % OFP11_INSTRUCTION_ALIGN)
5775 && len >= sizeof *inst
5776 && len / sizeof *inst <= n_instructions);
5779 /* This macro is careful to check for instructions with bad lengths. */
5780 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
5781 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
5782 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
5783 ((LEFT) -= (ntohs((ITER)->len) \
5784 / sizeof(struct ofp11_instruction)), \
5785 (ITER) = instruction_next(ITER)))
5788 decode_openflow11_instruction(const struct ofp11_instruction *inst,
5789 enum ovs_instruction_type *type)
5791 uint16_t len = ntohs(inst->len);
5793 switch (inst->type) {
5794 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
5795 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
5797 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
5798 case CONSTANT_HTONS(ENUM): \
5800 ? len >= sizeof(struct STRUCT) \
5801 : len == sizeof(struct STRUCT)) { \
5802 *type = OVSINST_##ENUM; \
5805 return OFPERR_OFPBIC_BAD_LEN; \
5811 return OFPERR_OFPBIC_UNKNOWN_INST;
5816 decode_openflow11_instructions(const struct ofp11_instruction insts[],
5818 const struct ofp11_instruction *out[])
5820 const struct ofp11_instruction *inst;
5823 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
5824 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
5825 enum ovs_instruction_type type;
5828 error = decode_openflow11_instruction(inst, &type);
5834 return OFPERR_OFPBIC_DUP_INST;
5840 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
5841 (n_insts - left) * sizeof *inst);
5842 return OFPERR_OFPBIC_BAD_LEN;
5848 get_actions_from_instruction(const struct ofp11_instruction *inst,
5849 const struct ofp_action_header **actions,
5850 size_t *actions_len)
5852 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
5853 *actions_len = ntohs(inst->len) - sizeof *inst;
5857 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
5858 unsigned int instructions_len,
5859 enum ofp_version version,
5860 struct ofpbuf *ofpacts)
5862 const struct ofp11_instruction *instructions;
5863 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
5866 if (version == OFP10_VERSION) {
5867 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
5869 (1u << N_OVS_INSTRUCTIONS) - 1,
5873 ofpbuf_clear(ofpacts);
5875 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
5876 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
5878 instructions_len, OFP11_INSTRUCTION_ALIGN);
5879 error = OFPERR_OFPBIC_BAD_LEN;
5883 instructions = ofpbuf_try_pull(openflow, instructions_len);
5884 if (instructions == NULL) {
5885 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
5886 "remaining message length (%"PRIu32")",
5887 instructions_len, openflow->size);
5888 error = OFPERR_OFPBIC_BAD_LEN;
5892 error = decode_openflow11_instructions(
5893 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
5899 if (insts[OVSINST_OFPIT13_METER]) {
5900 const struct ofp13_instruction_meter *oim;
5901 struct ofpact_meter *om;
5903 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
5904 insts[OVSINST_OFPIT13_METER]);
5906 om = ofpact_put_METER(ofpacts);
5907 om->meter_id = ntohl(oim->meter_id);
5909 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
5910 const struct ofp_action_header *actions;
5913 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
5914 &actions, &actions_len);
5915 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5920 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
5921 instruction_get_OFPIT11_CLEAR_ACTIONS(
5922 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
5923 ofpact_put_CLEAR_ACTIONS(ofpacts);
5925 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
5926 struct ofpact_nest *on;
5927 const struct ofp_action_header *actions;
5931 ofpact_pad(ofpacts);
5932 start = ofpacts->size;
5933 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
5934 offsetof(struct ofpact_nest, actions));
5935 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
5936 &actions, &actions_len);
5937 error = ofpacts_decode_for_action_set(actions, actions_len,
5942 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
5943 on->ofpact.len = ofpacts->size - start;
5945 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
5946 const struct ofp11_instruction_write_metadata *oiwm;
5947 struct ofpact_metadata *om;
5949 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
5950 insts[OVSINST_OFPIT11_WRITE_METADATA]);
5952 om = ofpact_put_WRITE_METADATA(ofpacts);
5953 om->metadata = oiwm->metadata;
5954 om->mask = oiwm->metadata_mask;
5956 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
5957 const struct ofp11_instruction_goto_table *oigt;
5958 struct ofpact_goto_table *ogt;
5960 oigt = instruction_get_OFPIT11_GOTO_TABLE(
5961 insts[OVSINST_OFPIT11_GOTO_TABLE]);
5962 ogt = ofpact_put_GOTO_TABLE(ofpacts);
5963 ogt->table_id = oigt->table_id;
5966 error = ofpacts_verify(ofpacts->data, ofpacts->size,
5967 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
5970 ofpbuf_clear(ofpacts);
5975 /* Update the length of the instruction that begins at offset 'ofs' within
5976 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
5977 * If the instruction contains no nested actions, deletes it entirely. */
5979 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
5981 struct ofp11_instruction_actions *oia;
5983 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
5984 if (openflow->size > ofs + sizeof *oia) {
5985 oia->len = htons(openflow->size - ofs);
5987 openflow->size = ofs;
5991 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
5992 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
5993 * valid, otherwise an OpenFlow error code. */
5995 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
6003 case OFPP_CONTROLLER:
6008 return OFPERR_OFPBAC_BAD_OUT_PORT;
6011 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
6014 return OFPERR_OFPBAC_BAD_OUT_PORT;
6018 /* Removes the protocols that require consistency between match and actions
6019 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
6021 * (An example of an inconsistency between match and actions is a flow that
6022 * does not match on an MPLS Ethertype but has an action that pops an MPLS
6025 inconsistent_match(enum ofputil_protocol *usable_protocols)
6027 *usable_protocols &= OFPUTIL_P_OF10_ANY;
6030 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
6031 * caller must restore them.
6033 * Modifies some actions, filling in fields that could not be properly set
6034 * without context. */
6036 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
6037 struct flow *flow, ofp_port_t max_ports,
6038 uint8_t table_id, uint8_t n_tables)
6040 const struct ofpact_enqueue *enqueue;
6041 const struct mf_field *mf;
6045 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
6048 case OFPACT_CONTROLLER:
6051 case OFPACT_ENQUEUE:
6052 enqueue = ofpact_get_ENQUEUE(a);
6053 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
6054 && enqueue->port != OFPP_IN_PORT
6055 && enqueue->port != OFPP_LOCAL) {
6056 return OFPERR_OFPBAC_BAD_OUT_PORT;
6060 case OFPACT_OUTPUT_REG:
6061 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
6064 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
6066 case OFPACT_SET_VLAN_VID:
6067 /* Remember if we saw a vlan tag in the flow to aid translating to
6068 * OpenFlow 1.1+ if need be. */
6069 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
6070 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6071 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6072 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
6073 inconsistent_match(usable_protocols);
6075 /* Temporary mark that we have a vlan tag. */
6076 flow->vlan_tci |= htons(VLAN_CFI);
6079 case OFPACT_SET_VLAN_PCP:
6080 /* Remember if we saw a vlan tag in the flow to aid translating to
6081 * OpenFlow 1.1+ if need be. */
6082 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
6083 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6084 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6085 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
6086 inconsistent_match(usable_protocols);
6088 /* Temporary mark that we have a vlan tag. */
6089 flow->vlan_tci |= htons(VLAN_CFI);
6092 case OFPACT_STRIP_VLAN:
6093 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
6094 inconsistent_match(usable_protocols);
6096 /* Temporary mark that we have no vlan tag. */
6097 flow->vlan_tci = htons(0);
6100 case OFPACT_PUSH_VLAN:
6101 if (flow->vlan_tci & htons(VLAN_CFI)) {
6102 /* Multiple VLAN headers not supported. */
6103 return OFPERR_OFPBAC_BAD_TAG;
6105 /* Temporary mark that we have a vlan tag. */
6106 flow->vlan_tci |= htons(VLAN_CFI);
6109 case OFPACT_SET_ETH_SRC:
6110 case OFPACT_SET_ETH_DST:
6113 case OFPACT_SET_IPV4_SRC:
6114 case OFPACT_SET_IPV4_DST:
6115 if (flow->dl_type != htons(ETH_TYPE_IP)) {
6116 inconsistent_match(usable_protocols);
6120 case OFPACT_SET_IP_DSCP:
6121 case OFPACT_SET_IP_ECN:
6122 case OFPACT_SET_IP_TTL:
6123 case OFPACT_DEC_TTL:
6124 if (!is_ip_any(flow)) {
6125 inconsistent_match(usable_protocols);
6129 case OFPACT_SET_L4_SRC_PORT:
6130 case OFPACT_SET_L4_DST_PORT:
6131 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
6132 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
6133 && flow->nw_proto != IPPROTO_SCTP)) {
6134 inconsistent_match(usable_protocols);
6136 /* Note on which transport protocol the port numbers are set.
6137 * This allows this set action to be converted to an OF1.2 set field
6139 if (a->type == OFPACT_SET_L4_SRC_PORT) {
6140 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
6142 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
6146 case OFPACT_REG_MOVE:
6147 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
6149 case OFPACT_SET_FIELD:
6150 mf = ofpact_get_SET_FIELD(a)->field;
6151 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6152 if (!mf_are_prereqs_ok(mf, flow) ||
6153 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
6154 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
6156 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6158 /* Remember if we saw a vlan tag in the flow to aid translating to
6159 * OpenFlow 1.1 if need be. */
6160 ofpact_get_SET_FIELD(a)->flow_has_vlan =
6161 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6162 if (mf->id == MFF_VLAN_TCI) {
6163 /* The set field may add or remove the vlan tag,
6164 * Mark the status temporarily. */
6165 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
6169 case OFPACT_STACK_PUSH:
6170 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
6172 case OFPACT_STACK_POP:
6173 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6175 case OFPACT_SET_MPLS_LABEL:
6176 case OFPACT_SET_MPLS_TC:
6177 case OFPACT_SET_MPLS_TTL:
6178 case OFPACT_DEC_MPLS_TTL:
6179 if (!eth_type_mpls(flow->dl_type)) {
6180 inconsistent_match(usable_protocols);
6184 case OFPACT_SET_TUNNEL:
6185 case OFPACT_SET_QUEUE:
6186 case OFPACT_POP_QUEUE:
6187 case OFPACT_RESUBMIT:
6190 case OFPACT_FIN_TIMEOUT:
6191 if (flow->nw_proto != IPPROTO_TCP) {
6192 inconsistent_match(usable_protocols);
6197 return learn_check(ofpact_get_LEARN(a), flow);
6199 case OFPACT_CONJUNCTION:
6202 case OFPACT_MULTIPATH:
6203 return multipath_check(ofpact_get_MULTIPATH(a), flow);
6209 case OFPACT_PUSH_MPLS:
6210 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6211 /* The packet is now MPLS and the MPLS payload is opaque.
6212 * Thus nothing can be assumed about the network protocol.
6213 * Temporarily mark that we have no nw_proto. */
6217 case OFPACT_POP_MPLS:
6218 if (!eth_type_mpls(flow->dl_type)) {
6219 inconsistent_match(usable_protocols);
6221 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6228 struct ofpact_conntrack *oc = ofpact_get_CT(a);
6231 if (!dl_type_is_ip_any(flow->dl_type)
6232 || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6233 inconsistent_match(usable_protocols);
6236 if (oc->zone_src.field) {
6237 return mf_check_src(&oc->zone_src, flow);
6240 err = ofpacts_check(oc->actions, ofpact_ct_get_action_len(oc),
6241 flow, max_ports, table_id, n_tables,
6246 case OFPACT_CLEAR_ACTIONS:
6249 case OFPACT_WRITE_ACTIONS: {
6250 /* Use a temporary copy of 'usable_protocols' because we can't check
6251 * consistency of an action set. */
6252 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6253 enum ofputil_protocol p = *usable_protocols;
6254 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6255 flow, max_ports, table_id, n_tables, &p);
6258 case OFPACT_WRITE_METADATA:
6261 case OFPACT_METER: {
6262 uint32_t mid = ofpact_get_METER(a)->meter_id;
6263 if (mid == 0 || mid > OFPM13_MAX) {
6264 return OFPERR_OFPMMFC_INVALID_METER;
6269 case OFPACT_GOTO_TABLE: {
6270 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
6271 if ((table_id != 255 && goto_table <= table_id)
6272 || (n_tables != 255 && goto_table >= n_tables)) {
6273 return OFPERR_OFPBIC_BAD_TABLE_ID;
6281 case OFPACT_UNROLL_XLATE:
6282 /* UNROLL is an internal action that should never be seen via
6284 return OFPERR_OFPBAC_BAD_TYPE;
6286 case OFPACT_DEBUG_RECIRC:
6294 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6295 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6296 * switch with no more than 'max_ports' ports.
6298 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6299 * '*usable_protocols' the protocols that forbid the inconsistency. (An
6300 * example of an inconsistency between match and actions is a flow that does
6301 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6303 * May annotate ofpacts with information gathered from the 'flow'.
6305 * May temporarily modify 'flow', but restores the changes before returning. */
6307 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
6308 struct flow *flow, ofp_port_t max_ports,
6309 uint8_t table_id, uint8_t n_tables,
6310 enum ofputil_protocol *usable_protocols)
6313 ovs_be16 dl_type = flow->dl_type;
6314 ovs_be16 vlan_tci = flow->vlan_tci;
6315 uint8_t nw_proto = flow->nw_proto;
6316 enum ofperr error = 0;
6318 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6319 error = ofpact_check__(usable_protocols, a, flow,
6320 max_ports, table_id, n_tables);
6325 /* Restore fields that may have been modified. */
6326 flow->dl_type = dl_type;
6327 flow->vlan_tci = vlan_tci;
6328 flow->nw_proto = nw_proto;
6332 /* Like ofpacts_check(), but reports inconsistencies as
6333 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6335 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
6336 struct flow *flow, ofp_port_t max_ports,
6337 uint8_t table_id, uint8_t n_tables,
6338 enum ofputil_protocol usable_protocols)
6340 enum ofputil_protocol p = usable_protocols;
6343 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
6344 table_id, n_tables, &p);
6345 return (error ? error
6346 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
6350 /* Returns the destination field that 'ofpact' would write to, or NULL
6351 * if the action would not write to an mf_field. */
6352 const struct mf_field *
6353 ofpact_get_mf_dst(const struct ofpact *ofpact)
6355 if (ofpact->type == OFPACT_SET_FIELD) {
6356 const struct ofpact_set_field *orl;
6358 orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
6360 } else if (ofpact->type == OFPACT_REG_MOVE) {
6361 const struct ofpact_reg_move *orm;
6363 orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
6364 return orm->dst.field;
6371 unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action)
6373 VLOG_WARN("%s action doesn't support nested action %s",
6374 ofpact_name(outer_action), ofpact_name(action));
6375 return OFPERR_OFPBAC_BAD_ARGUMENT;
6379 field_requires_ct(enum mf_field_id field)
6381 return field == MFF_CT_MARK || field == MFF_CT_LABEL;
6384 /* Apply nesting constraints for actions */
6386 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
6388 const struct mf_field *field = ofpact_get_mf_dst(a);
6390 if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
6391 VLOG_WARN("cannot set CT fields outside of ct action");
6392 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6396 ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
6397 || outer_action == OFPACT_CT);
6399 if (outer_action == OFPACT_CT) {
6401 return unsupported_nesting(a->type, outer_action);
6402 } else if (!field_requires_ct(field->id)) {
6403 VLOG_WARN("%s action doesn't support nested modification "
6404 "of %s", ofpact_name(outer_action), field->name);
6405 return OFPERR_OFPBAC_BAD_ARGUMENT;
6413 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
6414 * appropriate order as defined by the OpenFlow spec and as required by Open
6417 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
6418 * instructions that are allowed within 'ofpacts[]'.
6420 * If 'outer_action' is not zero, it specifies that the actions are nested
6421 * within another action of type 'outer_action'. */
6423 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
6424 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
6426 const struct ofpact *a;
6427 enum ovs_instruction_type inst;
6429 inst = OVSINST_OFPIT13_METER;
6430 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6431 enum ovs_instruction_type next;
6434 if (a->type == OFPACT_CONJUNCTION) {
6435 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6436 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
6437 VLOG_WARN("\"conjunction\" actions may be used along with "
6438 "\"note\" but not any other kind of action "
6439 "(such as the \"%s\" action used here)",
6440 ofpact_name(a->type));
6441 return OFPERR_NXBAC_BAD_CONJUNCTION;
6447 error = ofpacts_verify_nested(a, outer_action);
6452 next = ovs_instruction_type_from_ofpact_type(a->type);
6454 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
6457 const char *name = ovs_instruction_name_from_type(inst);
6458 const char *next_name = ovs_instruction_name_from_type(next);
6461 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
6462 "1.1+ compatibility", name);
6464 VLOG_WARN("invalid instruction ordering: %s must appear "
6465 "before %s, for OpenFlow 1.1+ compatibility",
6468 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
6470 if (!((1u << next) & allowed_ovsinsts)) {
6471 const char *name = ovs_instruction_name_from_type(next);
6473 VLOG_WARN("%s instruction not allowed here", name);
6474 return OFPERR_OFPBIC_UNSUP_INST;
6483 /* Converting ofpacts to OpenFlow. */
6486 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
6490 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6491 case OFPACT_##ENUM: \
6492 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
6501 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
6502 * actions in 'openflow', appending the actions to any existing data in
6505 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
6506 struct ofpbuf *openflow,
6507 enum ofp_version ofp_version)
6509 const struct ofpact *a;
6510 size_t start_size = openflow->size;
6512 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6513 encode_ofpact(a, ofp_version, openflow);
6515 return openflow->size - start_size;
6518 static enum ovs_instruction_type
6519 ofpact_is_apply_actions(const struct ofpact *a)
6521 return (ovs_instruction_type_from_ofpact_type(a->type)
6522 == OVSINST_OFPIT11_APPLY_ACTIONS);
6526 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
6528 struct ofpbuf *openflow,
6529 enum ofp_version ofp_version)
6531 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
6532 const struct ofpact *a;
6534 if (ofp_version == OFP10_VERSION) {
6535 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
6542 if (ofpact_is_apply_actions(a)) {
6543 size_t ofs = openflow->size;
6545 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
6547 encode_ofpact(a, ofp_version, openflow);
6549 } while (a < end && ofpact_is_apply_actions(a));
6550 ofpacts_update_instruction_actions(openflow, ofs);
6552 encode_ofpact(a, ofp_version, openflow);
6558 /* Sets of supported actions. */
6560 /* Two-way translation between OVS's internal "OFPACT_*" representation of
6561 * actions and the "OFPAT_*" representation used in some OpenFlow version.
6562 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
6563 * instance is specific to one OpenFlow version.) */
6565 enum ofpact_type ofpact; /* Internal name for action type. */
6566 int ofpat; /* OFPAT_* number from OpenFlow spec. */
6569 static const struct ofpact_map *
6570 get_ofpact_map(enum ofp_version version)
6572 /* OpenFlow 1.0 actions. */
6573 static const struct ofpact_map of10[] = {
6574 { OFPACT_OUTPUT, 0 },
6575 { OFPACT_SET_VLAN_VID, 1 },
6576 { OFPACT_SET_VLAN_PCP, 2 },
6577 { OFPACT_STRIP_VLAN, 3 },
6578 { OFPACT_SET_ETH_SRC, 4 },
6579 { OFPACT_SET_ETH_DST, 5 },
6580 { OFPACT_SET_IPV4_SRC, 6 },
6581 { OFPACT_SET_IPV4_DST, 7 },
6582 { OFPACT_SET_IP_DSCP, 8 },
6583 { OFPACT_SET_L4_SRC_PORT, 9 },
6584 { OFPACT_SET_L4_DST_PORT, 10 },
6585 { OFPACT_ENQUEUE, 11 },
6589 /* OpenFlow 1.1 actions. */
6590 static const struct ofpact_map of11[] = {
6591 { OFPACT_OUTPUT, 0 },
6592 { OFPACT_SET_VLAN_VID, 1 },
6593 { OFPACT_SET_VLAN_PCP, 2 },
6594 { OFPACT_SET_ETH_SRC, 3 },
6595 { OFPACT_SET_ETH_DST, 4 },
6596 { OFPACT_SET_IPV4_SRC, 5 },
6597 { OFPACT_SET_IPV4_DST, 6 },
6598 { OFPACT_SET_IP_DSCP, 7 },
6599 { OFPACT_SET_IP_ECN, 8 },
6600 { OFPACT_SET_L4_SRC_PORT, 9 },
6601 { OFPACT_SET_L4_DST_PORT, 10 },
6602 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6603 /* OFPAT_COPY_TTL_IN (12) not supported. */
6604 { OFPACT_SET_MPLS_LABEL, 13 },
6605 { OFPACT_SET_MPLS_TC, 14 },
6606 { OFPACT_SET_MPLS_TTL, 15 },
6607 { OFPACT_DEC_MPLS_TTL, 16 },
6608 { OFPACT_PUSH_VLAN, 17 },
6609 { OFPACT_STRIP_VLAN, 18 },
6610 { OFPACT_PUSH_MPLS, 19 },
6611 { OFPACT_POP_MPLS, 20 },
6612 { OFPACT_SET_QUEUE, 21 },
6613 { OFPACT_GROUP, 22 },
6614 { OFPACT_SET_IP_TTL, 23 },
6615 { OFPACT_DEC_TTL, 24 },
6619 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6620 static const struct ofpact_map of12[] = {
6621 { OFPACT_OUTPUT, 0 },
6622 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6623 /* OFPAT_COPY_TTL_IN (12) not supported. */
6624 { OFPACT_SET_MPLS_TTL, 15 },
6625 { OFPACT_DEC_MPLS_TTL, 16 },
6626 { OFPACT_PUSH_VLAN, 17 },
6627 { OFPACT_STRIP_VLAN, 18 },
6628 { OFPACT_PUSH_MPLS, 19 },
6629 { OFPACT_POP_MPLS, 20 },
6630 { OFPACT_SET_QUEUE, 21 },
6631 { OFPACT_GROUP, 22 },
6632 { OFPACT_SET_IP_TTL, 23 },
6633 { OFPACT_DEC_TTL, 24 },
6634 { OFPACT_SET_FIELD, 25 },
6635 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
6636 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
6656 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
6657 * values, into a bitmap of actions suitable for OpenFlow 'version', and
6658 * returns the result. */
6660 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
6662 uint32_t openflow_bitmap = 0;
6663 const struct ofpact_map *x;
6665 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6666 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
6667 openflow_bitmap |= 1u << x->ofpat;
6670 return htonl(openflow_bitmap);
6673 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
6674 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
6675 * and returns the result. */
6677 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
6679 uint64_t ofpact_bitmap = 0;
6680 const struct ofpact_map *x;
6682 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
6683 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
6684 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
6687 return ofpact_bitmap;
6690 /* Appends to 's' a string representation of the set of OFPACT_* represented
6691 * by 'ofpacts_bitmap'. */
6693 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
6695 if (!ofpacts_bitmap) {
6696 ds_put_cstr(s, "<none>");
6698 while (ofpacts_bitmap) {
6699 ds_put_format(s, "%s ",
6700 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
6701 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
6707 /* Returns true if 'action' outputs to 'port', false otherwise. */
6709 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
6711 switch (ofpact->type) {
6713 return ofpact_get_OUTPUT(ofpact)->port == port;
6714 case OFPACT_ENQUEUE:
6715 return ofpact_get_ENQUEUE(ofpact)->port == port;
6716 case OFPACT_CONTROLLER:
6717 return port == OFPP_CONTROLLER;
6719 case OFPACT_OUTPUT_REG:
6721 case OFPACT_SET_VLAN_VID:
6722 case OFPACT_SET_VLAN_PCP:
6723 case OFPACT_STRIP_VLAN:
6724 case OFPACT_PUSH_VLAN:
6725 case OFPACT_SET_ETH_SRC:
6726 case OFPACT_SET_ETH_DST:
6727 case OFPACT_SET_IPV4_SRC:
6728 case OFPACT_SET_IPV4_DST:
6729 case OFPACT_SET_IP_DSCP:
6730 case OFPACT_SET_IP_ECN:
6731 case OFPACT_SET_IP_TTL:
6732 case OFPACT_SET_L4_SRC_PORT:
6733 case OFPACT_SET_L4_DST_PORT:
6734 case OFPACT_REG_MOVE:
6735 case OFPACT_SET_FIELD:
6736 case OFPACT_STACK_PUSH:
6737 case OFPACT_STACK_POP:
6738 case OFPACT_DEC_TTL:
6739 case OFPACT_SET_MPLS_LABEL:
6740 case OFPACT_SET_MPLS_TC:
6741 case OFPACT_SET_MPLS_TTL:
6742 case OFPACT_DEC_MPLS_TTL:
6743 case OFPACT_SET_TUNNEL:
6744 case OFPACT_WRITE_METADATA:
6745 case OFPACT_SET_QUEUE:
6746 case OFPACT_POP_QUEUE:
6747 case OFPACT_FIN_TIMEOUT:
6748 case OFPACT_RESUBMIT:
6750 case OFPACT_CONJUNCTION:
6751 case OFPACT_MULTIPATH:
6754 case OFPACT_UNROLL_XLATE:
6755 case OFPACT_PUSH_MPLS:
6756 case OFPACT_POP_MPLS:
6758 case OFPACT_CLEAR_ACTIONS:
6759 case OFPACT_WRITE_ACTIONS:
6760 case OFPACT_GOTO_TABLE:
6763 case OFPACT_DEBUG_RECIRC:
6770 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6771 * to 'port', false otherwise. */
6773 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
6776 const struct ofpact *a;
6778 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
6779 if (ofpact_outputs_to_port(a, port)) {
6787 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
6788 * to 'group', false otherwise. */
6790 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
6793 const struct ofpact *a;
6795 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
6796 if (a->type == OFPACT_GROUP
6797 && ofpact_get_GROUP(a)->group_id == group_id) {
6806 ofpacts_equal(const struct ofpact *a, size_t a_len,
6807 const struct ofpact *b, size_t b_len)
6809 return a_len == b_len && !memcmp(a, b, a_len);
6812 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
6813 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
6815 * This function relies on the order of 'ofpacts' being correct (as checked by
6816 * ofpacts_verify()). */
6818 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
6820 const struct ofpact *a;
6822 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6823 enum ovs_instruction_type inst;
6825 inst = ovs_instruction_type_from_ofpact_type(a->type);
6826 if (a->type == OFPACT_METER) {
6827 return ofpact_get_METER(a)->meter_id;
6828 } else if (inst > OVSINST_OFPIT13_METER) {
6836 /* Formatting ofpacts. */
6839 ofpact_format(const struct ofpact *a, struct ds *s)
6842 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6843 case OFPACT_##ENUM: \
6844 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
6853 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
6854 * 'ofpacts' to 'string'. */
6856 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
6860 ds_put_cstr(string, "drop");
6862 const struct ofpact *a;
6864 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6866 ds_put_cstr(string, ",");
6869 /* XXX write-actions */
6870 ofpact_format(a, string);
6875 /* Internal use by helpers. */
6878 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
6880 struct ofpact *ofpact;
6882 ofpact_pad(ofpacts);
6883 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
6884 ofpact = ofpacts->header;
6885 ofpact_init(ofpact, type, len);
6890 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
6892 memset(ofpact, 0, len);
6893 ofpact->type = type;
6898 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
6899 * starting at 'ofpact'.
6901 * This is the correct way to update a variable-length ofpact's length after
6902 * adding the variable-length part of the payload. (See the large comment
6903 * near the end of ofp-actions.h for more information.) */
6905 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
6907 ovs_assert(ofpact == ofpacts->header);
6908 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
6911 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
6912 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
6913 * client must call this itself after adding the final ofpact to an array of
6916 * (The consequences of failing to call this function are probably not dire.
6917 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
6918 * not dereference it. That's undefined behavior, technically, but it will not
6919 * cause a real problem on common systems. Still, it seems better to call
6922 ofpact_pad(struct ofpbuf *ofpacts)
6924 unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
6926 ofpbuf_put_zeros(ofpacts, pad);
6933 static char * OVS_WARN_UNUSED_RESULT
6934 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
6935 enum ofputil_protocol *usable_protocols)
6938 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6939 case OFPACT_##ENUM: \
6940 return parse_##ENUM(value, ofpacts, usable_protocols);
6949 ofpact_type_from_name(const char *name, enum ofpact_type *type)
6951 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6952 if (!strcasecmp(name, NAME)) { \
6953 *type = OFPACT_##ENUM; \
6962 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
6964 * Returns NULL if successful, otherwise a malloc()'d string describing the
6965 * error. The caller is responsible for freeing the returned string.
6967 * If 'outer_action' is specified, indicates that the actions being parsed
6968 * are nested within another action of the type specified in 'outer_action'. */
6969 static char * OVS_WARN_UNUSED_RESULT
6970 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
6971 enum ofputil_protocol *usable_protocols,
6972 bool allow_instructions, enum ofpact_type outer_action)
6981 while (ofputil_parse_key_value(&pos, &key, &value)) {
6982 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
6983 enum ofpact_type type;
6987 if (ofpact_type_from_name(key, &type)) {
6988 error = ofpact_parse(type, value, ofpacts, usable_protocols);
6989 inst = ovs_instruction_type_from_ofpact_type(type);
6990 } else if (!strcasecmp(key, "mod_vlan_vid")) {
6991 error = parse_set_vlan_vid(value, ofpacts, true);
6992 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
6993 error = parse_set_vlan_pcp(value, ofpacts, true);
6994 } else if (!strcasecmp(key, "set_nw_ttl")) {
6995 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
6996 } else if (!strcasecmp(key, "pop_vlan")) {
6997 error = parse_pop_vlan(ofpacts);
6998 } else if (!strcasecmp(key, "set_tunnel64")) {
6999 error = parse_set_tunnel(value, ofpacts,
7000 NXAST_RAW_SET_TUNNEL64);
7001 } else if (!strcasecmp(key, "load")) {
7002 error = parse_reg_load(value, ofpacts);
7003 } else if (!strcasecmp(key, "bundle_load")) {
7004 error = parse_bundle_load(value, ofpacts);
7005 } else if (!strcasecmp(key, "drop")) {
7007 } else if (!strcasecmp(key, "apply_actions")) {
7008 return xstrdup("apply_actions is the default instruction");
7009 } else if (ofputil_port_from_string(key, &port)) {
7010 ofpact_put_OUTPUT(ofpacts)->port = port;
7012 return xasprintf("unknown action %s", key);
7018 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
7019 if (!allow_instructions) {
7020 return xasprintf("only actions are allowed here (not "
7022 ovs_instruction_name_from_type(inst));
7024 if (inst == prev_inst) {
7025 return xasprintf("instruction %s may be specified only once",
7026 ovs_instruction_name_from_type(inst));
7029 if (prev_inst != -1 && inst < prev_inst) {
7030 return xasprintf("instruction %s must be specified before %s",
7031 ovs_instruction_name_from_type(inst),
7032 ovs_instruction_name_from_type(prev_inst));
7036 ofpact_pad(ofpacts);
7038 if (drop && ofpacts->size) {
7039 return xstrdup("\"drop\" must not be accompanied by any other action "
7043 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
7045 ? (1u << N_OVS_INSTRUCTIONS) - 1
7046 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
7049 return xstrdup("Incorrect instruction ordering");
7055 static char * OVS_WARN_UNUSED_RESULT
7056 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
7057 enum ofputil_protocol *usable_protocols, bool allow_instructions,
7058 enum ofpact_type outer_action)
7060 uint32_t orig_size = ofpacts->size;
7061 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
7062 allow_instructions, outer_action);
7064 ofpacts->size = orig_size;
7069 static char * OVS_WARN_UNUSED_RESULT
7070 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
7071 enum ofputil_protocol *usable_protocols,
7072 bool allow_instructions, enum ofpact_type outer_action)
7076 *usable_protocols = OFPUTIL_P_ANY;
7079 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
7086 /* Parses 's' as a set of OpenFlow actions and appends the actions to
7087 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
7088 * that are nested within the action of type 'outer_action'.
7090 * Returns NULL if successful, otherwise a malloc()'d string describing the
7091 * error. The caller is responsible for freeing the returned string. */
7092 char * OVS_WARN_UNUSED_RESULT
7093 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
7094 enum ofputil_protocol *usable_protocols)
7096 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
7099 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
7102 * Returns NULL if successful, otherwise a malloc()'d string describing the
7103 * error. The caller is responsible for freeing the returned string. */
7104 char * OVS_WARN_UNUSED_RESULT
7105 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
7106 enum ofputil_protocol *usable_protocols)
7108 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
7112 ofpact_name(enum ofpact_type type)
7115 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7122 /* Low-level action decoding and encoding functions. */
7124 /* Everything needed to identify a particular OpenFlow action. */
7125 struct ofpact_hdrs {
7126 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
7127 uint16_t type; /* Type if standard, otherwise subtype. */
7128 uint8_t ofp_version; /* From ofp_header. */
7131 /* Information about a particular OpenFlow action. */
7132 struct ofpact_raw_instance {
7133 /* The action's identity. */
7134 struct ofpact_hdrs hdrs;
7135 enum ofp_raw_action_type raw;
7137 /* Looking up the action. */
7138 struct hmap_node decode_node; /* Based on 'hdrs'. */
7139 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
7141 /* The action's encoded size.
7143 * If this action is fixed-length, 'min_length' == 'max_length'.
7144 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7145 * OFP_ACTION_ALIGN) == 65528. */
7146 unsigned short int min_length;
7147 unsigned short int max_length;
7149 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7150 * offset of that argument from the beginning of the action and 'arg_len'
7151 * its length, both in bytes.
7153 * For actions that take other forms, these are both zero. */
7154 unsigned short int arg_ofs;
7155 unsigned short int arg_len;
7157 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7160 /* If this action is deprecated, a human-readable string with a brief
7162 const char *deprecation;
7165 /* Action header. */
7166 struct ofp_action_header {
7167 /* The meaning of other values of 'type' generally depends on the OpenFlow
7168 * version (see enum ofp_raw_action_type).
7170 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7171 * designates an OpenFlow vendor ID and that the remainder of the action
7172 * structure has a vendor-defined meaning.
7174 #define OFPAT_VENDOR 0xffff
7177 /* Always a multiple of 8. */
7180 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7181 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
7184 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
7186 /* Header for Nicira-defined actions and for ONF vendor extensions.
7188 * This cannot be used as an entirely generic vendor extension action header,
7189 * because OpenFlow does not specify the location or size of the action
7190 * subtype; it just happens that ONF extensions and Nicira extensions share
7192 struct ext_action_header {
7193 ovs_be16 type; /* OFPAT_VENDOR. */
7194 ovs_be16 len; /* At least 16. */
7195 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
7196 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
7199 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
7202 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
7203 const struct ofpact_hdrs *b)
7205 return (a->vendor == b->vendor
7206 && a->type == b->type
7207 && a->ofp_version == b->ofp_version);
7211 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
7213 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
7216 #include "ofp-actions.inc2"
7218 static struct hmap *
7219 ofpact_decode_hmap(void)
7221 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7222 static struct hmap hmap;
7224 if (ovsthread_once_start(&once)) {
7225 struct ofpact_raw_instance *inst;
7228 for (inst = all_raw_instances;
7229 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7231 hmap_insert(&hmap, &inst->decode_node,
7232 ofpact_hdrs_hash(&inst->hdrs));
7234 ovsthread_once_done(&once);
7239 static struct hmap *
7240 ofpact_encode_hmap(void)
7242 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7243 static struct hmap hmap;
7245 if (ovsthread_once_start(&once)) {
7246 struct ofpact_raw_instance *inst;
7249 for (inst = all_raw_instances;
7250 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7252 hmap_insert(&hmap, &inst->encode_node,
7253 hash_2words(inst->raw, inst->hdrs.ofp_version));
7255 ovsthread_once_done(&once);
7261 ofpact_decode_raw(enum ofp_version ofp_version,
7262 const struct ofp_action_header *oah, size_t length,
7263 const struct ofpact_raw_instance **instp)
7265 const struct ofpact_raw_instance *inst;
7266 struct ofpact_hdrs hdrs;
7269 if (length < sizeof *oah) {
7270 return OFPERR_OFPBAC_BAD_LEN;
7273 /* Get base action type. */
7274 if (oah->type == htons(OFPAT_VENDOR)) {
7276 hdrs.vendor = ntohl(oah->vendor);
7277 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
7278 /* Get extension subtype. */
7279 const struct ext_action_header *nah;
7281 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
7282 if (length < sizeof *nah) {
7283 return OFPERR_OFPBAC_BAD_LEN;
7285 hdrs.type = ntohs(nah->subtype);
7287 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
7289 return OFPERR_OFPBAC_BAD_VENDOR;
7293 hdrs.type = ntohs(oah->type);
7296 hdrs.ofp_version = ofp_version;
7297 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
7298 ofpact_decode_hmap()) {
7299 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
7306 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7307 : OFPERR_OFPBAC_BAD_TYPE);
7311 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7312 enum ofp_raw_action_type *raw, uint64_t *arg)
7314 const struct ofp_action_header *oah = buf->data;
7315 const struct ofpact_raw_instance *action;
7316 unsigned int length;
7320 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
7325 if (action->deprecation) {
7326 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
7327 action->name, ofputil_version_to_string(ofp_version),
7328 action->deprecation);
7331 length = ntohs(oah->len);
7332 if (length > buf->size) {
7333 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
7334 "length %"PRIu32, action->name, length, buf->size);
7335 return OFPERR_OFPBAC_BAD_LEN;
7337 if (length < action->min_length || length > action->max_length) {
7338 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
7339 "[%hu,%hu]", action->name, length,
7340 action->min_length, action->max_length);
7341 return OFPERR_OFPBAC_BAD_LEN;
7344 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
7345 "of 8", action->name, length);
7346 return OFPERR_OFPBAC_BAD_LEN;
7351 if (action->arg_len) {
7355 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
7356 for (i = 0; i < action->arg_len; i++) {
7357 *arg = (*arg << 8) | p[i];
7361 ofpbuf_pull(buf, length);
7366 static const struct ofpact_raw_instance *
7367 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
7369 const struct ofpact_raw_instance *inst;
7371 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
7372 ofpact_encode_hmap()) {
7373 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
7381 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7382 enum ofp_raw_action_type raw, uint64_t arg)
7384 const struct ofpact_raw_instance *inst;
7385 struct ofp_action_header *oah;
7386 const struct ofpact_hdrs *hdrs;
7388 inst = ofpact_raw_lookup(ofp_version, raw);
7391 oah = ofpbuf_put_zeros(buf, inst->min_length);
7392 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
7393 oah->len = htons(inst->min_length);
7394 oah->vendor = htonl(hdrs->vendor);
7396 switch (hdrs->vendor) {
7401 case ONF_VENDOR_ID: {
7402 struct ext_action_header *nah = (struct ext_action_header *) oah;
7403 nah->subtype = htons(hdrs->type);
7411 if (inst->arg_len) {
7412 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
7415 for (i = 0; i < inst->arg_len; i++) {
7427 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
7429 struct ofp_action_header *oah;
7431 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs, 8));
7433 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
7434 oah->len = htons(openflow->size - start_ofs);