2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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 "odp-netlink.h"
32 #include "ofp-parse.h"
35 #include "unaligned.h"
37 #include "openvswitch/vlog.h"
39 VLOG_DEFINE_THIS_MODULE(ofp_actions);
41 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
43 struct ofp_action_header;
45 /* Raw identifiers for OpenFlow actions.
47 * Decoding and encoding OpenFlow actions across multiple versions is difficult
48 * to do in a clean, consistent way. This enumeration lays out all of the
49 * forms of actions that Open vSwitch supports.
51 * The comments here must follow a stylized form because the
52 * "extract-ofp-actions" program parses them at build time to generate data
55 * - The first part of each comment specifies the vendor, OpenFlow versions,
56 * and type for each protocol that supports the action:
58 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
59 * extension actions. (Support for other vendors can be added, but
60 * it can't be done just based on a vendor ID definition alone
61 * because OpenFlow doesn't define a standard way to specify a
62 * subtype for vendor actions, so other vendors might do it different
65 * # The version can specify a specific OpenFlow version, a version
66 * range delimited by "-", or an open-ended range with "+".
68 * # The type, in parentheses, is the action type number (for standard
69 * OpenFlow actions) or subtype (for vendor extension actions).
71 * # Optionally one may add "is deprecated" followed by a
72 * human-readable reason in parentheses (which will be used in log
73 * messages), if a particular action should no longer be used.
75 * Multiple such specifications may be separated by commas.
77 * - The second part describes the action's wire format. It may be:
79 * # "struct <name>": The struct fully specifies the wire format. The
80 * action is exactly the size of the struct. (Thus, the struct must
81 * be an exact multiple of 8 bytes in size.)
83 * # "struct <name>, ...": The struct specifies the beginning of the
84 * wire format. An instance of the action is either the struct's
85 * exact size, or a multiple of 8 bytes longer.
87 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
88 * vendor extension) header, followed by 0 or more pad bytes to align
89 * to a multiple of <N> bits, followed by an argument of the given
90 * type, followed by 0 or more pad bytes to bring the total action up
91 * to a multiple of 8 bytes.
93 * # "void": The action is just a (standard or vendor extension)
96 * - Optional additional text enclosed in square brackets is commentary for
99 enum ofp_raw_action_type {
100 /* ## ----------------- ## */
101 /* ## Standard actions. ## */
102 /* ## ----------------- ## */
104 /* OF1.0(0): struct ofp10_action_output. */
106 /* OF1.1+(0): struct ofp11_action_output. */
109 /* OF1.0(1): uint16_t. */
110 OFPAT_RAW10_SET_VLAN_VID,
111 /* OF1.0(2): uint8_t. */
112 OFPAT_RAW10_SET_VLAN_PCP,
114 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
116 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
117 * modification actions: the 1.0 versions push a VLAN header if none is
118 * present, but the 1.1 versions do not. That is the only reason that we
119 * distinguish their raw action types.] */
120 OFPAT_RAW11_SET_VLAN_VID,
121 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
122 OFPAT_RAW11_SET_VLAN_PCP,
124 /* OF1.1+(17): ovs_be16.
126 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
128 OFPAT_RAW11_PUSH_VLAN,
130 /* OF1.0(3): void. */
131 OFPAT_RAW10_STRIP_VLAN,
132 /* OF1.1+(18): void. */
133 OFPAT_RAW11_POP_VLAN,
135 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
136 * ofp_action_dl_addr. */
137 OFPAT_RAW_SET_DL_SRC,
139 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
140 * ofp_action_dl_addr. */
141 OFPAT_RAW_SET_DL_DST,
143 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
145 OFPAT_RAW_SET_NW_SRC,
147 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
149 OFPAT_RAW_SET_NW_DST,
151 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
152 OFPAT_RAW_SET_NW_TOS,
154 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
155 OFPAT_RAW11_SET_NW_ECN,
157 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
159 OFPAT_RAW_SET_TP_SRC,
161 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
163 OFPAT_RAW_SET_TP_DST,
165 /* OF1.0(11): struct ofp10_action_enqueue. */
168 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
170 OFPAT_RAW_SET_MPLS_LABEL,
172 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
174 OFPAT_RAW_SET_MPLS_TC,
176 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
178 OFPAT_RAW_SET_MPLS_TTL,
180 /* NX1.0(26), OF1.1+(16): void. */
181 OFPAT_RAW_DEC_MPLS_TTL,
183 /* NX1.0(23), OF1.1+(19): ovs_be16.
185 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
188 /* NX1.0(24), OF1.1+(20): ovs_be16.
190 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
191 * ETH_TYPE_MPLS otherwise, not the label.] */
194 /* NX1.0(4), OF1.1+(21): uint32_t. */
197 /* OF1.1+(22): uint32_t. */
200 /* OF1.1+(23): uint8_t. */
201 OFPAT_RAW11_SET_NW_TTL,
203 /* NX1.0(18), OF1.1+(24): void. */
204 OFPAT_RAW_DEC_NW_TTL,
205 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
206 NXAST_RAW_DEC_TTL_CNT_IDS,
208 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
209 OFPAT_RAW12_SET_FIELD,
210 /* OF1.5+(25): struct ofp12_action_set_field, ... */
211 OFPAT_RAW15_SET_FIELD,
212 /* NX1.0-1.4(7): struct nx_action_reg_load.
214 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
215 * we drop reg_load.] */
217 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
219 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
220 * we drop reg_load2.] */
223 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
224 OFPAT_RAW15_COPY_FIELD,
225 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
226 ONFACT_RAW13_COPY_FIELD,
227 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
230 /* ## ------------------------- ## */
231 /* ## Nicira extension actions. ## */
232 /* ## ------------------------- ## */
234 /* Actions similar to standard actions are listed with the standard actions. */
236 /* NX1.0+(1): uint16_t. */
238 /* NX1.0+(14): struct nx_action_resubmit. */
239 NXAST_RAW_RESUBMIT_TABLE,
241 /* NX1.0+(2): uint32_t. */
242 NXAST_RAW_SET_TUNNEL,
243 /* NX1.0+(9): uint64_t. */
244 NXAST_RAW_SET_TUNNEL64,
246 /* NX1.0+(5): void. */
249 /* NX1.0+(8): struct nx_action_note, ... */
252 /* NX1.0+(10): struct nx_action_multipath. */
255 /* NX1.0+(12): struct nx_action_bundle, ... */
257 /* NX1.0+(13): struct nx_action_bundle, ... */
258 NXAST_RAW_BUNDLE_LOAD,
260 /* NX1.0+(15): struct nx_action_output_reg. */
261 NXAST_RAW_OUTPUT_REG,
262 /* NX1.0+(32): struct nx_action_output_reg2. */
263 NXAST_RAW_OUTPUT_REG2,
265 /* NX1.0+(16): struct nx_action_learn, ... */
268 /* NX1.0+(17): void. */
271 /* NX1.0+(19): struct nx_action_fin_timeout. */
272 NXAST_RAW_FIN_TIMEOUT,
274 /* NX1.0+(20): struct nx_action_controller. */
275 NXAST_RAW_CONTROLLER,
277 /* NX1.0+(22): struct nx_action_write_metadata. */
278 NXAST_RAW_WRITE_METADATA,
280 /* NX1.0+(27): struct nx_action_stack. */
281 NXAST_RAW_STACK_PUSH,
283 /* NX1.0+(28): struct nx_action_stack. */
286 /* NX1.0+(29): struct nx_action_sample. */
289 /* NX1.0+(34): struct nx_action_conjunction. */
290 NXAST_RAW_CONJUNCTION,
292 /* NX1.0+(35): struct nx_action_conntrack, ... */
295 /* NX1.0+(36): struct nx_action_nat, ... */
298 /* ## ------------------ ## */
299 /* ## Debugging actions. ## */
300 /* ## ------------------ ## */
302 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
303 /* accepts them only if started with --enable-dummy. */
305 /* NX1.0+(255): void. */
306 NXAST_RAW_DEBUG_RECIRC,
309 /* OpenFlow actions are always a multiple of 8 bytes in length. */
310 #define OFP_ACTION_ALIGN 8
312 /* Define a few functions for working with instructions. */
313 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
314 static inline const struct STRUCT * OVS_UNUSED \
315 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
317 ovs_assert(inst->type == htons(ENUM)); \
318 return ALIGNED_CAST(struct STRUCT *, inst); \
321 static inline void OVS_UNUSED \
322 instruction_init_##ENUM(struct STRUCT *s) \
324 memset(s, 0, sizeof *s); \
325 s->type = htons(ENUM); \
326 s->len = htons(sizeof *s); \
329 static inline struct STRUCT * OVS_UNUSED \
330 instruction_put_##ENUM(struct ofpbuf *buf) \
332 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
333 instruction_init_##ENUM(s); \
339 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
341 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
343 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
344 uint32_t allowed_ovsinsts,
345 enum ofpact_type outer_action);
347 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
348 enum mf_field_id, uint64_t value);
350 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
351 enum ofp_raw_action_type *, uint64_t *arg);
352 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
353 enum ofp_raw_action_type, uint64_t arg);
355 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
356 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
357 bool allow_instructions, enum ofpact_type outer_action);
358 static enum ofperr ofpacts_pull_openflow_actions__(
359 struct ofpbuf *openflow, unsigned int actions_len,
360 enum ofp_version version, uint32_t allowed_ovsinsts,
361 struct ofpbuf *ofpacts, enum ofpact_type outer_action);
362 static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
363 const char *s_, struct ofpbuf *ofpacts,
364 enum ofputil_protocol *usable_protocols,
365 bool allow_instructions, enum ofpact_type outer_action);
367 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
368 * nested ofpacts it returns the first one. */
370 ofpact_next_flattened(const struct ofpact *ofpact)
372 switch (ofpact->type) {
375 case OFPACT_CONTROLLER:
377 case OFPACT_OUTPUT_REG:
379 case OFPACT_SET_FIELD:
380 case OFPACT_SET_VLAN_VID:
381 case OFPACT_SET_VLAN_PCP:
382 case OFPACT_STRIP_VLAN:
383 case OFPACT_PUSH_VLAN:
384 case OFPACT_SET_ETH_SRC:
385 case OFPACT_SET_ETH_DST:
386 case OFPACT_SET_IPV4_SRC:
387 case OFPACT_SET_IPV4_DST:
388 case OFPACT_SET_IP_DSCP:
389 case OFPACT_SET_IP_ECN:
390 case OFPACT_SET_IP_TTL:
391 case OFPACT_SET_L4_SRC_PORT:
392 case OFPACT_SET_L4_DST_PORT:
393 case OFPACT_REG_MOVE:
394 case OFPACT_STACK_PUSH:
395 case OFPACT_STACK_POP:
397 case OFPACT_SET_MPLS_LABEL:
398 case OFPACT_SET_MPLS_TC:
399 case OFPACT_SET_MPLS_TTL:
400 case OFPACT_DEC_MPLS_TTL:
401 case OFPACT_PUSH_MPLS:
402 case OFPACT_POP_MPLS:
403 case OFPACT_SET_TUNNEL:
404 case OFPACT_SET_QUEUE:
405 case OFPACT_POP_QUEUE:
406 case OFPACT_FIN_TIMEOUT:
407 case OFPACT_RESUBMIT:
409 case OFPACT_CONJUNCTION:
410 case OFPACT_MULTIPATH:
414 case OFPACT_UNROLL_XLATE:
415 case OFPACT_DEBUG_RECIRC:
417 case OFPACT_CLEAR_ACTIONS:
418 case OFPACT_WRITE_METADATA:
419 case OFPACT_GOTO_TABLE:
421 return ofpact_next(ofpact);
424 return ofpact_get_CT(ofpact)->actions;
426 case OFPACT_WRITE_ACTIONS:
427 return ofpact_get_WRITE_ACTIONS(ofpact)->actions;
433 /* Pull off existing actions or instructions. Used by nesting actions to keep
434 * ofpacts_parse() oblivious of actions nesting.
436 * Push the actions back on after nested parsing, e.g.:
438 * size_t ofs = ofpacts_pull(ofpacts);
439 * ...nested parsing...
440 * ofpbuf_push_uninit(ofpacts, ofs);
443 ofpacts_pull(struct ofpbuf *ofpacts)
448 ofpbuf_pull(ofpacts, ofs);
453 #include "ofp-actions.inc1"
455 /* Output actions. */
457 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
458 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
459 * number of bytes to send. A 'max_len' of zero means no bytes of the
460 * packet should be sent. */
461 struct ofp10_action_output {
462 ovs_be16 type; /* OFPAT10_OUTPUT. */
463 ovs_be16 len; /* Length is 8. */
464 ovs_be16 port; /* Output port. */
465 ovs_be16 max_len; /* Max length to send to controller. */
467 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
469 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
470 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
471 * number of bytes to send. A 'max_len' of zero means no bytes of the
472 * packet should be sent.*/
473 struct ofp11_action_output {
474 ovs_be16 type; /* OFPAT11_OUTPUT. */
475 ovs_be16 len; /* Length is 16. */
476 ovs_be32 port; /* Output port. */
477 ovs_be16 max_len; /* Max length to send to controller. */
478 uint8_t pad[6]; /* Pad to 64 bits. */
480 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
483 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
484 enum ofp_version ofp_version OVS_UNUSED,
487 struct ofpact_output *output;
489 output = ofpact_put_OUTPUT(out);
490 output->port = u16_to_ofp(ntohs(oao->port));
491 output->max_len = ntohs(oao->max_len);
493 return ofpact_check_output_port(output->port, OFPP_MAX);
497 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
498 enum ofp_version ofp_version OVS_UNUSED,
501 struct ofpact_output *output;
504 output = ofpact_put_OUTPUT(out);
505 output->max_len = ntohs(oao->max_len);
507 error = ofputil_port_from_ofp11(oao->port, &output->port);
512 return ofpact_check_output_port(output->port, OFPP_MAX);
516 encode_OUTPUT(const struct ofpact_output *output,
517 enum ofp_version ofp_version, struct ofpbuf *out)
519 if (ofp_version == OFP10_VERSION) {
520 struct ofp10_action_output *oao;
522 oao = put_OFPAT10_OUTPUT(out);
523 oao->port = htons(ofp_to_u16(output->port));
524 oao->max_len = htons(output->max_len);
526 struct ofp11_action_output *oao;
528 oao = put_OFPAT11_OUTPUT(out);
529 oao->port = ofputil_port_to_ofp11(output->port);
530 oao->max_len = htons(output->max_len);
534 static char * OVS_WARN_UNUSED_RESULT
535 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
536 enum ofputil_protocol *usable_protocols OVS_UNUSED)
538 if (strchr(arg, '[')) {
539 struct ofpact_output_reg *output_reg;
541 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
542 output_reg->max_len = UINT16_MAX;
543 return mf_parse_subfield(&output_reg->src, arg);
545 struct ofpact_output *output;
547 output = ofpact_put_OUTPUT(ofpacts);
548 if (!ofputil_port_from_string(arg, &output->port)) {
549 return xasprintf("%s: output to unknown port", arg);
551 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
557 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
559 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
560 ds_put_format(s, "output:%"PRIu16, a->port);
562 ofputil_format_port(a->port, s);
563 if (a->port == OFPP_CONTROLLER) {
564 ds_put_format(s, ":%"PRIu16, a->max_len);
572 decode_OFPAT_RAW11_GROUP(uint32_t group_id,
573 enum ofp_version ofp_version OVS_UNUSED,
576 ofpact_put_GROUP(out)->group_id = group_id;
581 encode_GROUP(const struct ofpact_group *group,
582 enum ofp_version ofp_version, struct ofpbuf *out)
584 if (ofp_version == OFP10_VERSION) {
587 put_OFPAT11_GROUP(out, group->group_id);
591 static char * OVS_WARN_UNUSED_RESULT
592 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
593 enum ofputil_protocol *usable_protocols OVS_UNUSED)
595 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
599 format_GROUP(const struct ofpact_group *a, struct ds *s)
601 ds_put_format(s, "group:%"PRIu32, a->group_id);
604 /* Action structure for NXAST_CONTROLLER.
606 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
607 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
610 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
612 * - 'controller_id': The ID of the controller connection to which the
613 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
614 * sent only to controllers that have the specified controller connection
615 * ID. See "struct nx_controller_id" for more information. */
616 struct nx_action_controller {
617 ovs_be16 type; /* OFPAT_VENDOR. */
618 ovs_be16 len; /* Length is 16. */
619 ovs_be32 vendor; /* NX_VENDOR_ID. */
620 ovs_be16 subtype; /* NXAST_CONTROLLER. */
621 ovs_be16 max_len; /* Maximum length to send to controller. */
622 ovs_be16 controller_id; /* Controller ID to send packet-in. */
623 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
624 uint8_t zero; /* Must be zero. */
626 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
629 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
630 enum ofp_version ofp_version OVS_UNUSED,
633 struct ofpact_controller *oc;
635 oc = ofpact_put_CONTROLLER(out);
636 oc->max_len = ntohs(nac->max_len);
637 oc->controller_id = ntohs(nac->controller_id);
638 oc->reason = nac->reason;
643 encode_CONTROLLER(const struct ofpact_controller *controller,
644 enum ofp_version ofp_version OVS_UNUSED,
647 struct nx_action_controller *nac;
649 nac = put_NXAST_CONTROLLER(out);
650 nac->max_len = htons(controller->max_len);
651 nac->controller_id = htons(controller->controller_id);
652 nac->reason = controller->reason;
655 static char * OVS_WARN_UNUSED_RESULT
656 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
657 enum ofputil_protocol *usable_protocols OVS_UNUSED)
659 enum ofp_packet_in_reason reason = OFPR_ACTION;
660 uint16_t controller_id = 0;
661 uint16_t max_len = UINT16_MAX;
665 } else if (strspn(arg, "0123456789") == strlen(arg)) {
666 char *error = str_to_u16(arg, "max_len", &max_len);
673 while (ofputil_parse_key_value(&arg, &name, &value)) {
674 if (!strcmp(name, "reason")) {
675 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
676 return xasprintf("unknown reason \"%s\"", value);
678 } else if (!strcmp(name, "max_len")) {
679 char *error = str_to_u16(value, "max_len", &max_len);
683 } else if (!strcmp(name, "id")) {
684 char *error = str_to_u16(value, "id", &controller_id);
689 return xasprintf("unknown key \"%s\" parsing controller "
695 if (reason == OFPR_ACTION && controller_id == 0) {
696 struct ofpact_output *output;
698 output = ofpact_put_OUTPUT(ofpacts);
699 output->port = OFPP_CONTROLLER;
700 output->max_len = max_len;
702 struct ofpact_controller *controller;
704 controller = ofpact_put_CONTROLLER(ofpacts);
705 controller->max_len = max_len;
706 controller->reason = reason;
707 controller->controller_id = controller_id;
714 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
716 if (a->reason == OFPR_ACTION && a->controller_id == 0) {
717 ds_put_format(s, "CONTROLLER:%"PRIu16, a->max_len);
719 enum ofp_packet_in_reason reason = a->reason;
721 ds_put_cstr(s, "controller(");
722 if (reason != OFPR_ACTION) {
723 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
725 ds_put_format(s, "reason=%s,",
726 ofputil_packet_in_reason_to_string(
727 reason, reasonbuf, sizeof reasonbuf));
729 if (a->max_len != UINT16_MAX) {
730 ds_put_format(s, "max_len=%"PRIu16",", a->max_len);
732 if (a->controller_id != 0) {
733 ds_put_format(s, "id=%"PRIu16",", a->controller_id);
740 /* Enqueue action. */
741 struct ofp10_action_enqueue {
742 ovs_be16 type; /* OFPAT10_ENQUEUE. */
743 ovs_be16 len; /* Len is 16. */
744 ovs_be16 port; /* Port that queue belongs. Should
745 refer to a valid physical port
746 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
747 uint8_t pad[6]; /* Pad for 64-bit alignment. */
748 ovs_be32 queue_id; /* Where to enqueue the packets. */
750 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
753 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
754 enum ofp_version ofp_version OVS_UNUSED,
757 struct ofpact_enqueue *enqueue;
759 enqueue = ofpact_put_ENQUEUE(out);
760 enqueue->port = u16_to_ofp(ntohs(oae->port));
761 enqueue->queue = ntohl(oae->queue_id);
762 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
763 && enqueue->port != OFPP_IN_PORT
764 && enqueue->port != OFPP_LOCAL) {
765 return OFPERR_OFPBAC_BAD_OUT_PORT;
771 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
772 enum ofp_version ofp_version, struct ofpbuf *out)
774 if (ofp_version == OFP10_VERSION) {
775 struct ofp10_action_enqueue *oae;
777 oae = put_OFPAT10_ENQUEUE(out);
778 oae->port = htons(ofp_to_u16(enqueue->port));
779 oae->queue_id = htonl(enqueue->queue);
785 static char * OVS_WARN_UNUSED_RESULT
786 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
787 enum ofputil_protocol *usable_protocols OVS_UNUSED)
790 char *port = strtok_r(arg, ":q,", &sp);
791 char *queue = strtok_r(NULL, "", &sp);
792 struct ofpact_enqueue *enqueue;
794 if (port == NULL || queue == NULL) {
795 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
796 "\"enqueue(PORT,QUEUE)\"");
799 enqueue = ofpact_put_ENQUEUE(ofpacts);
800 if (!ofputil_port_from_string(port, &enqueue->port)) {
801 return xasprintf("%s: enqueue to unknown port", port);
803 return str_to_u32(queue, &enqueue->queue);
807 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
809 ds_put_format(s, "enqueue:");
810 ofputil_format_port(a->port, s);
811 ds_put_format(s, ":%"PRIu32, a->queue);
814 /* Action structure for NXAST_OUTPUT_REG.
816 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
818 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
819 * the NXAST_REG_LOAD action.
821 * The acceptable nxm_header values for 'src' are the same as the acceptable
822 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
824 * The 'max_len' field indicates the number of bytes to send when the chosen
825 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
826 * field of OFPAT_OUTPUT.
828 * The 'zero' field is required to be zeroed for forward compatibility. */
829 struct nx_action_output_reg {
830 ovs_be16 type; /* OFPAT_VENDOR. */
831 ovs_be16 len; /* 24. */
832 ovs_be32 vendor; /* NX_VENDOR_ID. */
833 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
835 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
836 ovs_be32 src; /* Source. */
838 ovs_be16 max_len; /* Max length to send to controller. */
840 uint8_t zero[6]; /* Reserved, must be zero. */
842 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
844 /* Action structure for NXAST_OUTPUT_REG2.
846 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
847 * experimenter OXM as 'src'.
849 struct nx_action_output_reg2 {
850 ovs_be16 type; /* OFPAT_VENDOR. */
851 ovs_be16 len; /* 24. */
852 ovs_be32 vendor; /* NX_VENDOR_ID. */
853 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
855 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
856 ovs_be16 max_len; /* Max length to send to controller. */
859 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
860 * - Enough 0-bytes to pad the action out to 24 bytes. */
863 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
866 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
867 enum ofp_version ofp_version OVS_UNUSED,
870 struct ofpact_output_reg *output_reg;
872 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
873 return OFPERR_OFPBAC_BAD_ARGUMENT;
876 output_reg = ofpact_put_OUTPUT_REG(out);
877 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
878 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
879 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
880 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
881 output_reg->max_len = ntohs(naor->max_len);
883 return mf_check_src(&output_reg->src, NULL);
887 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
888 enum ofp_version ofp_version OVS_UNUSED,
891 struct ofpact_output_reg *output_reg;
895 output_reg = ofpact_put_OUTPUT_REG(out);
896 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
897 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
898 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
899 output_reg->max_len = ntohs(naor->max_len);
901 ofpbuf_use_const(&b, naor, ntohs(naor->len));
902 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
903 error = nx_pull_header(&b, &output_reg->src.field, NULL);
907 if (!is_all_zeros(b.data, b.size)) {
908 return OFPERR_NXBRC_MUST_BE_ZERO;
911 return mf_check_src(&output_reg->src, NULL);
915 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
916 enum ofp_version ofp_version OVS_UNUSED,
919 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
920 * cannot be encoded in the older form, encode it as
921 * NXAST_RAW_OUTPUT_REG2. */
922 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
923 || !mf_nxm_header(output_reg->src.field->id)) {
924 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
925 size_t size = out->size;
927 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
928 output_reg->src.n_bits);
929 naor->max_len = htons(output_reg->max_len);
931 out->size = size - sizeof naor->pad;
932 nx_put_header(out, output_reg->src.field->id, 0, false);
935 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
937 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
938 output_reg->src.n_bits);
939 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
940 naor->max_len = htons(output_reg->max_len);
944 static char * OVS_WARN_UNUSED_RESULT
945 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
946 enum ofputil_protocol *usable_protocols OVS_UNUSED)
948 return parse_OUTPUT(arg, ofpacts, usable_protocols);
952 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
954 ds_put_cstr(s, "output:");
955 mf_format_subfield(&a->src, s);
958 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
960 * The bundle actions choose a slave from a supplied list of options.
961 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
962 * selection to a register.
964 * The list of possible slaves follows the nx_action_bundle structure. The size
965 * of each slave is governed by its type as indicated by the 'slave_type'
966 * parameter. The list of slaves should be padded at its end with zeros to make
967 * the total length of the action a multiple of 8.
969 * Switches infer from the 'slave_type' parameter the size of each slave. All
970 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
971 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
972 * 2 byte width. Switches should reject actions which indicate unknown or
973 * unsupported slave types.
975 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
976 * slave. If the switch does not support the specified 'algorithm' parameter,
977 * it should reject the action.
979 * Several algorithms take into account liveness when selecting slaves. The
980 * liveness of a slave is implementation defined (with one exception), but will
981 * generally take into account things like its carrier status and the results
982 * of any link monitoring protocols which happen to be running on it. In order
983 * to give controllers a place-holder value, the OFPP_NONE port is always
986 * Some slave selection strategies require the use of a hash function, in which
987 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
988 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
989 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
990 * 'basis' parameter is used as a universal hash parameter. Different values
991 * of 'basis' yield different hash results.
993 * The 'zero' parameter at the end of the action structure is reserved for
994 * future use. Switches are required to reject actions which have nonzero
995 * bytes in the 'zero' field.
997 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
998 * should reject actions which have nonzero bytes in either of these fields.
1000 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
1001 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
1002 * similar to those for the NXAST_REG_LOAD action. */
1003 struct nx_action_bundle {
1004 ovs_be16 type; /* OFPAT_VENDOR. */
1005 ovs_be16 len; /* Length including slaves. */
1006 ovs_be32 vendor; /* NX_VENDOR_ID. */
1007 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1009 /* Slave choice algorithm to apply to hash value. */
1010 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
1012 /* What fields to hash and how. */
1013 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
1014 ovs_be16 basis; /* Universal hash parameter. */
1016 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
1017 ovs_be16 n_slaves; /* Number of slaves. */
1019 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1020 ovs_be32 dst; /* Destination. */
1022 uint8_t zero[4]; /* Reserved. Must be zero. */
1024 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
1027 decode_bundle(bool load, const struct nx_action_bundle *nab,
1028 struct ofpbuf *ofpacts)
1030 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1031 struct ofpact_bundle *bundle;
1032 uint32_t slave_type;
1033 size_t slaves_size, i;
1036 bundle = ofpact_put_BUNDLE(ofpacts);
1038 bundle->n_slaves = ntohs(nab->n_slaves);
1039 bundle->basis = ntohs(nab->basis);
1040 bundle->fields = ntohs(nab->fields);
1041 bundle->algorithm = ntohs(nab->algorithm);
1042 slave_type = ntohl(nab->slave_type);
1043 slaves_size = ntohs(nab->len) - sizeof *nab;
1045 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1046 if (!flow_hash_fields_valid(bundle->fields)) {
1047 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
1048 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
1049 VLOG_WARN_RL(&rl, "too many slaves");
1050 } else if (bundle->algorithm != NX_BD_ALG_HRW
1051 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
1052 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
1053 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
1054 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
1059 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
1060 VLOG_WARN_RL(&rl, "reserved field is nonzero");
1061 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1065 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
1066 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
1067 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
1069 if (bundle->dst.n_bits < 16) {
1070 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
1072 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1075 if (nab->ofs_nbits || nab->dst) {
1076 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
1077 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1081 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1082 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1083 "allocated for slaves. %"PRIuSIZE" bytes are required "
1084 "for %"PRIu16" slaves.",
1085 load ? "bundle_load" : "bundle", slaves_size,
1086 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1087 error = OFPERR_OFPBAC_BAD_LEN;
1090 for (i = 0; i < bundle->n_slaves; i++) {
1091 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1092 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1095 bundle = ofpacts->header;
1096 ofpact_finish(ofpacts, &bundle->ofpact);
1099 error = bundle_check(bundle, OFPP_MAX, NULL);
1105 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1106 enum ofp_version ofp_version OVS_UNUSED,
1109 return decode_bundle(false, nab, out);
1113 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1114 enum ofp_version ofp_version OVS_UNUSED,
1117 return decode_bundle(true, nab, out);
1121 encode_BUNDLE(const struct ofpact_bundle *bundle,
1122 enum ofp_version ofp_version OVS_UNUSED,
1125 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1126 struct nx_action_bundle *nab;
1130 nab = (bundle->dst.field
1131 ? put_NXAST_BUNDLE_LOAD(out)
1132 : put_NXAST_BUNDLE(out));
1133 nab->len = htons(ntohs(nab->len) + slaves_len);
1134 nab->algorithm = htons(bundle->algorithm);
1135 nab->fields = htons(bundle->fields);
1136 nab->basis = htons(bundle->basis);
1137 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1138 nab->n_slaves = htons(bundle->n_slaves);
1139 if (bundle->dst.field) {
1140 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1141 bundle->dst.n_bits);
1142 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1145 slaves = ofpbuf_put_zeros(out, slaves_len);
1146 for (i = 0; i < bundle->n_slaves; i++) {
1147 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1151 static char * OVS_WARN_UNUSED_RESULT
1152 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1153 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1155 return bundle_parse(arg, ofpacts);
1158 static char * OVS_WARN_UNUSED_RESULT
1159 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1161 return bundle_parse_load(arg, ofpacts);
1165 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1167 bundle_format(a, s);
1170 /* Set VLAN actions. */
1173 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1176 return OFPERR_OFPBAC_BAD_ARGUMENT;
1178 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1179 vlan_vid->vlan_vid = vid;
1180 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1186 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1187 enum ofp_version ofp_version OVS_UNUSED,
1190 return decode_set_vlan_vid(vid, true, out);
1194 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1195 enum ofp_version ofp_version OVS_UNUSED,
1198 return decode_set_vlan_vid(vid, false, out);
1202 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1203 enum ofp_version ofp_version, struct ofpbuf *out)
1205 uint16_t vid = vlan_vid->vlan_vid;
1207 /* Push a VLAN tag, if none is present and this form of the action calls
1208 * for such a feature. */
1209 if (ofp_version > OFP10_VERSION
1210 && vlan_vid->push_vlan_if_needed
1211 && !vlan_vid->flow_has_vlan) {
1212 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1215 if (ofp_version == OFP10_VERSION) {
1216 put_OFPAT10_SET_VLAN_VID(out, vid);
1217 } else if (ofp_version == OFP11_VERSION) {
1218 put_OFPAT11_SET_VLAN_VID(out, vid);
1220 ofpact_put_set_field(out, ofp_version,
1221 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1225 static char * OVS_WARN_UNUSED_RESULT
1226 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1228 struct ofpact_vlan_vid *vlan_vid;
1232 error = str_to_u16(arg, "VLAN VID", &vid);
1237 if (vid & ~VLAN_VID_MASK) {
1238 return xasprintf("%s: not a valid VLAN VID", arg);
1240 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1241 vlan_vid->vlan_vid = vid;
1242 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1246 static char * OVS_WARN_UNUSED_RESULT
1247 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1248 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1250 return parse_set_vlan_vid(arg, ofpacts, false);
1254 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1256 ds_put_format(s, "%s:%"PRIu16,
1257 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1261 /* Set PCP actions. */
1264 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1267 return OFPERR_OFPBAC_BAD_ARGUMENT;
1269 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1270 vlan_pcp->vlan_pcp = pcp;
1271 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1277 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1278 enum ofp_version ofp_version OVS_UNUSED,
1281 return decode_set_vlan_pcp(pcp, true, out);
1285 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1286 enum ofp_version ofp_version OVS_UNUSED,
1289 return decode_set_vlan_pcp(pcp, false, out);
1293 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1294 enum ofp_version ofp_version, struct ofpbuf *out)
1296 uint8_t pcp = vlan_pcp->vlan_pcp;
1298 /* Push a VLAN tag, if none is present and this form of the action calls
1299 * for such a feature. */
1300 if (ofp_version > OFP10_VERSION
1301 && vlan_pcp->push_vlan_if_needed
1302 && !vlan_pcp->flow_has_vlan) {
1303 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1306 if (ofp_version == OFP10_VERSION) {
1307 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1308 } else if (ofp_version == OFP11_VERSION) {
1309 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1311 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1315 static char * OVS_WARN_UNUSED_RESULT
1316 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1318 struct ofpact_vlan_pcp *vlan_pcp;
1322 error = str_to_u8(arg, "VLAN PCP", &pcp);
1328 return xasprintf("%s: not a valid VLAN PCP", arg);
1330 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1331 vlan_pcp->vlan_pcp = pcp;
1332 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1336 static char * OVS_WARN_UNUSED_RESULT
1337 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1338 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1340 return parse_set_vlan_pcp(arg, ofpacts, false);
1344 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1346 ds_put_format(s, "%s:%"PRIu8,
1347 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1351 /* Strip VLAN actions. */
1354 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1356 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1361 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1363 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1368 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1369 enum ofp_version ofp_version, struct ofpbuf *out)
1371 if (ofp_version == OFP10_VERSION) {
1372 put_OFPAT10_STRIP_VLAN(out);
1374 put_OFPAT11_POP_VLAN(out);
1378 static char * OVS_WARN_UNUSED_RESULT
1379 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1380 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1382 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1386 static char * OVS_WARN_UNUSED_RESULT
1387 parse_pop_vlan(struct ofpbuf *ofpacts)
1389 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1394 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1396 ds_put_cstr(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1401 /* Push VLAN action. */
1404 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1405 enum ofp_version ofp_version OVS_UNUSED,
1408 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1409 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1410 return OFPERR_OFPBAC_BAD_ARGUMENT;
1412 ofpact_put_PUSH_VLAN(out);
1417 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1418 enum ofp_version ofp_version, struct ofpbuf *out)
1420 if (ofp_version == OFP10_VERSION) {
1421 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1422 * follow this action. */
1424 /* XXX ETH_TYPE_VLAN_8021AD case */
1425 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1429 static char * OVS_WARN_UNUSED_RESULT
1430 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1431 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1436 *usable_protocols &= OFPUTIL_P_OF11_UP;
1437 error = str_to_u16(arg, "ethertype", ðertype);
1442 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1443 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1444 return xasprintf("%s: not a valid VLAN ethertype", arg);
1447 ofpact_put_PUSH_VLAN(ofpacts);
1452 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1454 /* XXX 802.1AD case*/
1455 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1458 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1459 struct ofp_action_dl_addr {
1460 ovs_be16 type; /* Type. */
1461 ovs_be16 len; /* Length is 16. */
1462 struct eth_addr dl_addr; /* Ethernet address. */
1465 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1468 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1469 enum ofp_version ofp_version OVS_UNUSED,
1472 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1477 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1478 enum ofp_version ofp_version OVS_UNUSED,
1481 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1486 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1487 enum ofp_raw_action_type raw, enum mf_field_id field,
1490 if (ofp_version < OFP12_VERSION) {
1491 struct ofp_action_dl_addr *oada;
1493 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1494 oada->dl_addr = mac->mac;
1496 ofpact_put_set_field(out, ofp_version, field,
1497 eth_addr_to_uint64(mac->mac));
1502 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1505 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1511 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1512 enum ofp_version ofp_version,
1515 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1520 static char * OVS_WARN_UNUSED_RESULT
1521 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1522 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1524 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1527 static char * OVS_WARN_UNUSED_RESULT
1528 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1529 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1531 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1535 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1537 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1541 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1543 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT, ETH_ADDR_ARGS(a->mac));
1546 /* Set IPv4 address actions. */
1549 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1550 enum ofp_version ofp_version OVS_UNUSED,
1553 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1558 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1559 enum ofp_version ofp_version OVS_UNUSED,
1562 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1567 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1568 enum ofp_version ofp_version,
1569 enum ofp_raw_action_type raw, enum mf_field_id field,
1572 ovs_be32 addr = ipv4->ipv4;
1573 if (ofp_version < OFP12_VERSION) {
1574 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1576 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1581 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1582 enum ofp_version ofp_version, struct ofpbuf *out)
1584 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1589 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1590 enum ofp_version ofp_version, struct ofpbuf *out)
1592 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1596 static char * OVS_WARN_UNUSED_RESULT
1597 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1598 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1600 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1603 static char * OVS_WARN_UNUSED_RESULT
1604 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1605 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1607 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1611 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1613 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(a->ipv4));
1617 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1619 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(a->ipv4));
1622 /* Set IPv4/v6 TOS actions. */
1625 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1626 enum ofp_version ofp_version OVS_UNUSED,
1629 if (dscp & ~IP_DSCP_MASK) {
1630 return OFPERR_OFPBAC_BAD_ARGUMENT;
1632 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1638 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1639 enum ofp_version ofp_version, struct ofpbuf *out)
1641 if (ofp_version < OFP12_VERSION) {
1642 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1644 ofpact_put_set_field(out, ofp_version,
1645 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1649 static char * OVS_WARN_UNUSED_RESULT
1650 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1651 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1656 error = str_to_u8(arg, "TOS", &tos);
1661 if (tos & ~IP_DSCP_MASK) {
1662 return xasprintf("%s: not a valid TOS", arg);
1664 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1669 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1671 ds_put_format(s, "mod_nw_tos:%d", a->dscp);
1674 /* Set IPv4/v6 ECN actions. */
1677 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1678 enum ofp_version ofp_version OVS_UNUSED,
1681 if (ecn & ~IP_ECN_MASK) {
1682 return OFPERR_OFPBAC_BAD_ARGUMENT;
1684 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1690 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1691 enum ofp_version ofp_version, struct ofpbuf *out)
1693 uint8_t ecn = ip_ecn->ecn;
1694 if (ofp_version == OFP10_VERSION) {
1696 } else if (ofp_version == OFP11_VERSION) {
1697 put_OFPAT11_SET_NW_ECN(out, ecn);
1699 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1703 static char * OVS_WARN_UNUSED_RESULT
1704 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1705 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1710 error = str_to_u8(arg, "ECN", &ecn);
1715 if (ecn & ~IP_ECN_MASK) {
1716 return xasprintf("%s: not a valid ECN", arg);
1718 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1723 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1725 ds_put_format(s, "mod_nw_ecn:%d", a->ecn);
1728 /* Set IPv4/v6 TTL actions. */
1731 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1732 enum ofp_version ofp_version OVS_UNUSED,
1735 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1740 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1741 enum ofp_version ofp_version, struct ofpbuf *out)
1743 if (ofp_version >= OFP11_VERSION) {
1744 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1750 static char * OVS_WARN_UNUSED_RESULT
1751 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1752 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1757 error = str_to_u8(arg, "TTL", &ttl);
1762 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1767 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1769 ds_put_format(s, "mod_nw_ttl:%d", a->ttl);
1772 /* Set TCP/UDP/SCTP port actions. */
1775 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1776 enum ofp_version ofp_version OVS_UNUSED,
1779 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1784 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1785 enum ofp_version ofp_version OVS_UNUSED,
1788 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1793 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1794 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1795 enum mf_field_id field, struct ofpbuf *out)
1797 uint16_t port = l4_port->port;
1799 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1800 ofpact_put_set_field(out, ofp_version, field, port);
1802 ofpact_put_raw(out, ofp_version, raw, port);
1807 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1808 enum ofp_version ofp_version, struct ofpbuf *out)
1810 uint8_t proto = l4_port->flow_ip_proto;
1811 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1812 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1813 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1816 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1820 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1821 enum ofp_version ofp_version,
1824 uint8_t proto = l4_port->flow_ip_proto;
1825 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
1826 : proto == IPPROTO_UDP ? MFF_UDP_DST
1827 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
1830 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
1833 static char * OVS_WARN_UNUSED_RESULT
1834 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
1835 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1837 return str_to_u16(arg, "source port",
1838 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
1841 static char * OVS_WARN_UNUSED_RESULT
1842 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
1843 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1845 return str_to_u16(arg, "destination port",
1846 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
1850 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
1852 ds_put_format(s, "mod_tp_src:%d", a->port);
1856 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
1858 ds_put_format(s, "mod_tp_dst:%d", a->port);
1861 /* Action structure for OFPAT_COPY_FIELD. */
1862 struct ofp15_action_copy_field {
1863 ovs_be16 type; /* OFPAT_COPY_FIELD. */
1864 ovs_be16 len; /* Length is padded to 64 bits. */
1865 ovs_be16 n_bits; /* Number of bits to copy. */
1866 ovs_be16 src_offset; /* Starting bit offset in source. */
1867 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1870 * - OXM header for source field.
1871 * - OXM header for destination field.
1872 * - Padding with 0-bytes to a multiple of 8 bytes.
1873 * The "pad2" member is the beginning of the above. */
1876 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
1878 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
1879 struct onf_action_copy_field {
1880 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
1881 ovs_be16 len; /* Length is padded to 64 bits. */
1882 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
1883 ovs_be16 exp_type; /* 3200. */
1884 uint8_t pad[2]; /* Not used. */
1885 ovs_be16 n_bits; /* Number of bits to copy. */
1886 ovs_be16 src_offset; /* Starting bit offset in source. */
1887 ovs_be16 dst_offset; /* Starting bit offset in destination. */
1888 uint8_t pad2[2]; /* Not used. */
1890 * - OXM header for source field.
1891 * - OXM header for destination field.
1892 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
1893 * The "pad3" member is the beginning of the above. */
1894 uint8_t pad3[4]; /* Not used. */
1896 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
1898 /* Action structure for NXAST_REG_MOVE.
1900 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
1901 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
1902 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
1903 * the next most significant bit, and so on.
1905 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
1906 * sense to use nxm_hasmask=1 because the action does not do any kind of
1907 * matching; it uses the actual value of a field.)
1909 * The following nxm_header values are potentially acceptable as 'src':
1924 * - NXM_OF_ICMP_TYPE
1925 * - NXM_OF_ICMP_CODE
1932 * - NXM_NX_ICMPV6_TYPE
1933 * - NXM_NX_ICMPV6_CODE
1936 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1938 * - NXM_NX_TUN_IPV4_SRC
1939 * - NXM_NX_TUN_IPV4_DST
1941 * The following nxm_header values are potentially acceptable as 'dst':
1952 * - NXM_OF_ICMP_TYPE
1953 * - NXM_OF_ICMP_CODE
1954 * - NXM_NX_ICMPV6_TYPE
1955 * - NXM_NX_ICMPV6_CODE
1961 * Modifying any of the above fields changes the corresponding packet
1966 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
1970 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
1971 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
1972 * header (if any), ignoring the other bits. Setting a value with CFI=1
1973 * adds or modifies the 802.1Q header appropriately, setting the TCI field
1974 * to the field's new value (with the CFI bit masked out).
1976 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
1977 * any of these values modifies the corresponding tunnel header field used
1978 * for the packet's next tunnel encapsulation, if allowed by the
1979 * configuration of the output tunnel port.
1981 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
1982 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
1983 * used only if the flow's nx_match includes an nxm_entry that specifies
1984 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
1986 * The switch will reject actions for which src_ofs+n_bits is greater than the
1987 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
1988 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
1990 * This action behaves properly when 'src' overlaps with 'dst', that is, it
1991 * behaves as if 'src' were copied out to a temporary buffer, then the
1992 * temporary buffer copied to 'dst'.
1994 struct nx_action_reg_move {
1995 ovs_be16 type; /* OFPAT_VENDOR. */
1996 ovs_be16 len; /* Length is 24. */
1997 ovs_be32 vendor; /* NX_VENDOR_ID. */
1998 ovs_be16 subtype; /* NXAST_REG_MOVE. */
1999 ovs_be16 n_bits; /* Number of bits. */
2000 ovs_be16 src_ofs; /* Starting bit offset in source. */
2001 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
2003 * - OXM/NXM header for source field (4 or 8 bytes).
2004 * - OXM/NXM header for destination field (4 or 8 bytes).
2005 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2007 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
2010 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
2011 const void *action, ovs_be16 action_len, size_t oxm_offset,
2012 struct ofpbuf *ofpacts)
2014 struct ofpact_reg_move *move;
2018 move = ofpact_put_REG_MOVE(ofpacts);
2019 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
2020 move->src.ofs = ntohs(src_offset);
2021 move->src.n_bits = ntohs(n_bits);
2022 move->dst.ofs = ntohs(dst_offset);
2023 move->dst.n_bits = ntohs(n_bits);
2025 ofpbuf_use_const(&b, action, ntohs(action_len));
2026 ofpbuf_pull(&b, oxm_offset);
2027 error = nx_pull_header(&b, &move->src.field, NULL);
2031 error = nx_pull_header(&b, &move->dst.field, NULL);
2036 if (!is_all_zeros(b.data, b.size)) {
2037 return OFPERR_NXBRC_MUST_BE_ZERO;
2040 return nxm_reg_move_check(move, NULL);
2044 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
2045 enum ofp_version ofp_version OVS_UNUSED,
2046 struct ofpbuf *ofpacts)
2048 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2049 oacf->n_bits, oacf, oacf->len,
2050 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
2054 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
2055 enum ofp_version ofp_version OVS_UNUSED,
2056 struct ofpbuf *ofpacts)
2058 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2059 oacf->n_bits, oacf, oacf->len,
2060 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
2064 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
2065 enum ofp_version ofp_version OVS_UNUSED,
2066 struct ofpbuf *ofpacts)
2068 struct ofpact_reg_move *move;
2072 move = ofpact_put_REG_MOVE(ofpacts);
2073 move->ofpact.raw = NXAST_RAW_REG_MOVE;
2074 move->src.ofs = ntohs(narm->src_ofs);
2075 move->src.n_bits = ntohs(narm->n_bits);
2076 move->dst.ofs = ntohs(narm->dst_ofs);
2077 move->dst.n_bits = ntohs(narm->n_bits);
2079 ofpbuf_use_const(&b, narm, ntohs(narm->len));
2080 ofpbuf_pull(&b, sizeof *narm);
2081 error = nx_pull_header(&b, &move->src.field, NULL);
2085 error = nx_pull_header(&b, &move->dst.field, NULL);
2089 if (!is_all_zeros(b.data, b.size)) {
2090 return OFPERR_NXBRC_MUST_BE_ZERO;
2093 return nxm_reg_move_check(move, NULL);
2097 encode_REG_MOVE(const struct ofpact_reg_move *move,
2098 enum ofp_version ofp_version, struct ofpbuf *out)
2100 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2101 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2102 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2103 * probe for support. Until we have that ability, we currently prefer
2104 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2106 size_t start_ofs = out->size;
2107 if (ofp_version >= OFP15_VERSION) {
2108 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2109 copy->n_bits = htons(move->dst.n_bits);
2110 copy->src_offset = htons(move->src.ofs);
2111 copy->dst_offset = htons(move->dst.ofs);
2112 out->size = out->size - sizeof copy->pad2;
2113 nx_put_header(out, move->src.field->id, ofp_version, false);
2114 nx_put_header(out, move->dst.field->id, ofp_version, false);
2115 } else if (ofp_version == OFP13_VERSION
2116 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2117 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2118 copy->n_bits = htons(move->dst.n_bits);
2119 copy->src_offset = htons(move->src.ofs);
2120 copy->dst_offset = htons(move->dst.ofs);
2121 out->size = out->size - sizeof copy->pad3;
2122 nx_put_header(out, move->src.field->id, ofp_version, false);
2123 nx_put_header(out, move->dst.field->id, ofp_version, false);
2125 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2126 narm->n_bits = htons(move->dst.n_bits);
2127 narm->src_ofs = htons(move->src.ofs);
2128 narm->dst_ofs = htons(move->dst.ofs);
2129 nx_put_header(out, move->src.field->id, 0, false);
2130 nx_put_header(out, move->dst.field->id, 0, false);
2132 pad_ofpat(out, start_ofs);
2135 static char * OVS_WARN_UNUSED_RESULT
2136 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2137 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2139 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2140 const char *full_arg = arg;
2143 error = mf_parse_subfield__(&move->src, &arg);
2147 if (strncmp(arg, "->", 2)) {
2148 return xasprintf("%s: missing `->' following source", full_arg);
2151 error = mf_parse_subfield(&move->dst, arg);
2156 if (move->src.n_bits != move->dst.n_bits) {
2157 return xasprintf("%s: source field is %d bits wide but destination is "
2158 "%d bits wide", full_arg,
2159 move->src.n_bits, move->dst.n_bits);
2165 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2167 nxm_format_reg_move(a, s);
2170 /* Action structure for OFPAT12_SET_FIELD. */
2171 struct ofp12_action_set_field {
2172 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2173 ovs_be16 len; /* Length is padded to 64 bits. */
2176 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2177 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2179 * The "pad" member is the beginning of the above. */
2182 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2184 /* Action structure for NXAST_REG_LOAD.
2186 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2187 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2188 * starts at 0 for the least-significant bit, 1 for the next most significant
2191 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2192 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2195 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2196 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2197 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2198 * also stored as one less than its true value:
2201 * +------------------------------+------------------+
2202 * | ofs | n_bits - 1 |
2203 * +------------------------------+------------------+
2205 * The switch will reject actions for which ofs+n_bits is greater than the
2206 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2207 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2208 * OFPBAC_BAD_ARGUMENT.
2210 struct nx_action_reg_load {
2211 ovs_be16 type; /* OFPAT_VENDOR. */
2212 ovs_be16 len; /* Length is 24. */
2213 ovs_be32 vendor; /* NX_VENDOR_ID. */
2214 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2215 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2216 ovs_be32 dst; /* Destination register. */
2217 ovs_be64 value; /* Immediate value. */
2219 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2221 /* Action structure for NXAST_REG_LOAD2.
2223 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2224 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2225 * OXM experimenter fields. */
2226 struct nx_action_reg_load2 {
2227 ovs_be16 type; /* OFPAT_VENDOR. */
2228 ovs_be16 len; /* At least 16. */
2229 ovs_be32 vendor; /* NX_VENDOR_ID. */
2230 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2233 * - An NXM/OXM header, value, and optionally a mask.
2234 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2236 * The "pad" member is the beginning of the above. */
2239 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2242 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2243 bool may_mask, struct ofpbuf *ofpacts)
2245 struct ofpact_set_field *sf;
2249 sf = ofpact_put_SET_FIELD(ofpacts);
2251 ofpbuf_use_const(&b, oasf, ntohs(oasf->len));
2252 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2253 error = nx_pull_entry(&b, &sf->field, &sf->value,
2254 may_mask ? &sf->mask : NULL);
2256 return (error == OFPERR_OFPBMC_BAD_MASK
2257 ? OFPERR_OFPBAC_BAD_SET_MASK
2261 memset(&sf->mask, 0xff, sf->field->n_bytes);
2264 if (!is_all_zeros(b.data, b.size)) {
2265 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2268 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2270 if (sf->field->id == MFF_IN_PORT_OXM) {
2271 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2274 /* oxm_length is now validated to be compatible with mf_value. */
2275 if (!sf->field->writable) {
2276 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2278 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2281 /* The value must be valid for match. OpenFlow 1.5 also says,
2282 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2283 * a 1-bit in oxm_value and in oxm_mask." */
2284 if (!mf_is_value_valid(sf->field, &sf->value)
2285 || (sf->field->id == MFF_VLAN_VID
2286 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2287 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2288 struct ds ds = DS_EMPTY_INITIALIZER;
2289 mf_format(sf->field, &sf->value, NULL, &ds);
2290 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2291 sf->field->name, ds_cstr(&ds));
2294 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2300 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2301 enum ofp_version ofp_version OVS_UNUSED,
2302 struct ofpbuf *ofpacts)
2304 return decode_ofpat_set_field(oasf, false, ofpacts);
2308 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2309 enum ofp_version ofp_version OVS_UNUSED,
2310 struct ofpbuf *ofpacts)
2312 return decode_ofpat_set_field(oasf, true, ofpacts);
2316 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2317 enum ofp_version ofp_version OVS_UNUSED,
2320 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2321 struct mf_subfield dst;
2324 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2326 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2327 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2328 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2329 error = mf_check_dst(&dst, NULL);
2334 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2336 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2337 return OFPERR_OFPBAC_BAD_ARGUMENT;
2340 sf->field = dst.field;
2341 bitwise_put(ntohll(narl->value),
2342 &sf->value, dst.field->n_bytes, dst.ofs,
2344 bitwise_put(UINT64_MAX,
2345 &sf->mask, dst.field->n_bytes, dst.ofs,
2352 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2353 enum ofp_version ofp_version OVS_UNUSED,
2356 struct ofpact_set_field *sf;
2360 sf = ofpact_put_SET_FIELD(out);
2361 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2363 ofpbuf_use_const(&b, narl, ntohs(narl->len));
2364 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2365 error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2369 if (!is_all_zeros(b.data, b.size)) {
2370 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2373 if (!sf->field->writable) {
2374 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2376 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2382 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2383 enum mf_field_id field, uint64_t value_)
2385 struct ofp12_action_set_field *oasf OVS_UNUSED;
2386 int n_bytes = mf_from_id(field)->n_bytes;
2387 size_t start_ofs = openflow->size;
2388 union mf_value value;
2390 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2392 oasf = put_OFPAT12_SET_FIELD(openflow);
2393 openflow->size = openflow->size - sizeof oasf->pad;
2394 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2395 pad_ofpat(openflow, start_ofs);
2399 next_load_segment(const struct ofpact_set_field *sf,
2400 struct mf_subfield *dst, uint64_t *value)
2402 int n_bits = sf->field->n_bits;
2403 int n_bytes = sf->field->n_bytes;
2404 int start = dst->ofs + dst->n_bits;
2406 if (start < n_bits) {
2407 dst->field = sf->field;
2408 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2409 if (dst->ofs < n_bits) {
2410 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2411 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2412 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2419 /* Convert 'sf' to a series of REG_LOADs. */
2421 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2423 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2424 * experimenter OXM or is variable length (or if it came in as
2425 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2426 * NXAST_REG_LOAD, which is backward compatible. */
2427 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2428 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2429 struct nx_action_reg_load2 *narl OVS_UNUSED;
2430 size_t start_ofs = openflow->size;
2432 narl = put_NXAST_REG_LOAD2(openflow);
2433 openflow->size = openflow->size - sizeof narl->pad;
2434 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2435 pad_ofpat(openflow, start_ofs);
2437 struct mf_subfield dst;
2440 dst.ofs = dst.n_bits = 0;
2441 while (next_load_segment(sf, &dst, &value)) {
2442 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2443 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2444 narl->dst = htonl(mf_nxm_header(dst.field->id));
2445 narl->value = htonll(value);
2450 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2451 * actions, if we can, falling back to Nicira extensions if we must.
2453 * We check only meta-flow types that can appear within set field actions and
2454 * that have a mapping to compatible action types. These struct mf_field
2455 * definitions have a defined OXM or NXM header value and specify the field as
2458 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2459 enum ofp_version ofp_version,
2462 switch ((int) sf->field->id) {
2463 case MFF_VLAN_TCI: {
2464 ovs_be16 tci = sf->value.be16;
2465 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2466 uint16_t vid = vlan_tci_to_vid(tci);
2467 uint8_t pcp = vlan_tci_to_pcp(tci);
2469 if (ofp_version < OFP11_VERSION) {
2470 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2472 * If CFI=1, Add or modify VLAN VID & PCP.
2473 * If CFI=0, strip VLAN header, if any.
2476 put_OFPAT10_SET_VLAN_VID(out, vid);
2477 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2479 put_OFPAT10_STRIP_VLAN(out);
2482 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2484 * If CFI=1, Add or modify VLAN VID & PCP.
2485 * OpenFlow 1.1 set actions only apply if the packet
2486 * already has VLAN tags. To be sure that is the case
2487 * we have to push a VLAN header. As we do not support
2488 * multiple layers of VLANs, this is a no-op, if a VLAN
2489 * header already exists. This may backfire, however,
2490 * when we start supporting multiple layers of VLANs.
2491 * If CFI=0, strip VLAN header, if any.
2494 /* Push a VLAN tag, if one was not seen at action validation
2496 if (!sf->flow_has_vlan) {
2497 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2499 put_OFPAT11_SET_VLAN_VID(out, vid);
2500 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2502 /* If the flow did not match on vlan, we have no way of
2503 * knowing if the vlan tag exists, so we must POP just to be
2505 put_OFPAT11_POP_VLAN(out);
2511 case MFF_VLAN_VID: {
2512 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2513 if (ofp_version == OFP10_VERSION) {
2514 put_OFPAT10_SET_VLAN_VID(out, vid);
2516 put_OFPAT11_SET_VLAN_VID(out, vid);
2522 if (ofp_version == OFP10_VERSION) {
2523 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2525 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2530 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2534 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2538 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2542 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2546 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2549 case MFF_IP_DSCP_SHIFTED:
2550 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2555 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2560 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2564 set_field_to_nxast(sf, out);
2570 set_field_to_set_field(const struct ofpact_set_field *sf,
2571 enum ofp_version ofp_version, struct ofpbuf *out)
2573 struct ofp12_action_set_field *oasf OVS_UNUSED;
2574 size_t start_ofs = out->size;
2576 oasf = put_OFPAT12_SET_FIELD(out);
2577 out->size = out->size - sizeof oasf->pad;
2578 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2579 pad_ofpat(out, start_ofs);
2583 encode_SET_FIELD(const struct ofpact_set_field *sf,
2584 enum ofp_version ofp_version, struct ofpbuf *out)
2586 if (ofp_version >= OFP15_VERSION) {
2587 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2589 set_field_to_set_field(sf, ofp_version, out);
2590 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2591 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2592 /* It came in as reg_load, send it out the same way. */
2593 set_field_to_nxast(sf, out);
2594 } else if (ofp_version < OFP12_VERSION) {
2595 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2596 set_field_to_legacy_openflow(sf, ofp_version, out);
2597 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2598 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2599 * entire field, so encode it as OFPAT_SET_FIELD. */
2600 set_field_to_set_field(sf, ofp_version, out);
2602 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2603 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2604 * so encode it as reg_load. */
2605 set_field_to_nxast(sf, out);
2609 /* Parses the input argument 'arg' into the key, value, and delimiter
2610 * components that are common across the reg_load and set_field action format.
2612 * With an argument like "1->metadata", sets the following pointers to
2613 * point within 'arg':
2618 * Returns NULL if successful, otherwise a malloc()'d string describing the
2619 * error. The caller is responsible for freeing the returned string. */
2620 static char * OVS_WARN_UNUSED_RESULT
2621 set_field_split_str(char *arg, char **key, char **value, char **delim)
2626 value_end = strstr(arg, "->");
2627 *key = value_end + strlen("->");
2633 return xasprintf("%s: missing `->'", arg);
2635 if (strlen(value_end) <= strlen("->")) {
2636 return xasprintf("%s: missing field name following `->'", arg);
2642 /* Parses a "set_field" action with argument 'arg', appending the parsed
2643 * action to 'ofpacts'.
2645 * Returns NULL if successful, otherwise a malloc()'d string describing the
2646 * error. The caller is responsible for freeing the returned string. */
2647 static char * OVS_WARN_UNUSED_RESULT
2648 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2649 enum ofputil_protocol *usable_protocols)
2651 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2655 const struct mf_field *mf;
2658 error = set_field_split_str(arg, &key, &value, &delim);
2663 mf = mf_from_name(key);
2665 return xasprintf("%s is not a valid OXM field name", key);
2667 if (!mf->writable) {
2668 return xasprintf("%s is read-only", key);
2672 error = mf_parse(mf, value, &sf->value, &sf->mask);
2677 if (!mf_is_value_valid(mf, &sf->value)) {
2678 return xasprintf("%s is not a valid value for field %s", value, key);
2681 *usable_protocols &= mf->usable_protocols_exact;
2685 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2686 * action to 'ofpacts'.
2688 * Returns NULL if successful, otherwise a malloc()'d string describing the
2689 * error. The caller is responsible for freeing the returned string. */
2690 static char * OVS_WARN_UNUSED_RESULT
2691 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2692 enum ofputil_protocol *usable_protocols)
2694 char *copy = xstrdup(arg);
2695 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2700 static char * OVS_WARN_UNUSED_RESULT
2701 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2703 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2704 struct mf_subfield dst;
2705 char *key, *value_str;
2706 union mf_value value;
2709 error = set_field_split_str(arg, &key, &value_str, NULL);
2714 error = mf_parse_subfield(&dst, key);
2719 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2721 return xasprintf("%s: cannot parse integer value", arg);
2724 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2725 dst.field->n_bytes * 8 - dst.n_bits)) {
2729 mf_format(dst.field, &value, NULL, &ds);
2730 error = xasprintf("%s: value %s does not fit into %d bits",
2731 arg, ds_cstr(&ds), dst.n_bits);
2736 sf->field = dst.field;
2737 memset(&sf->value, 0, sizeof sf->value);
2738 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2739 dst.field->n_bytes, dst.ofs, dst.n_bits);
2740 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2746 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2748 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2749 struct mf_subfield dst;
2752 dst.ofs = dst.n_bits = 0;
2753 while (next_load_segment(a, &dst, &value)) {
2754 ds_put_format(s, "load:%#"PRIx64"->", value);
2755 mf_format_subfield(&dst, s);
2756 ds_put_char(s, ',');
2760 ds_put_cstr(s, "set_field:");
2761 mf_format(a->field, &a->value, &a->mask, s);
2762 ds_put_format(s, "->%s", a->field->name);
2766 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2767 * is marked such that, if possible, it will be translated to OpenFlow as
2768 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2769 * that was the way that the action was expressed when it came into OVS or for
2770 * backward compatibility. */
2771 struct ofpact_set_field *
2772 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2774 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2775 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2779 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2781 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2784 struct nx_action_stack {
2785 ovs_be16 type; /* OFPAT_VENDOR. */
2786 ovs_be16 len; /* Length is 16. */
2787 ovs_be32 vendor; /* NX_VENDOR_ID. */
2788 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2789 ovs_be16 offset; /* Bit offset into the field. */
2791 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2792 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2793 * - Enough 0-bytes to pad out the action to 24 bytes. */
2794 uint8_t pad[12]; /* See above. */
2796 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2799 decode_stack_action(const struct nx_action_stack *nasp,
2800 struct ofpact_stack *stack_action)
2805 stack_action->subfield.ofs = ntohs(nasp->offset);
2807 ofpbuf_use_const(&b, nasp, sizeof *nasp);
2808 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2809 error = nx_pull_header(&b, &stack_action->subfield.field, NULL);
2813 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2815 if (!is_all_zeros(b.data, b.size)) {
2816 return OFPERR_NXBRC_MUST_BE_ZERO;
2823 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2824 enum ofp_version ofp_version OVS_UNUSED,
2825 struct ofpbuf *ofpacts)
2827 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2828 enum ofperr error = decode_stack_action(nasp, push);
2829 return error ? error : nxm_stack_push_check(push, NULL);
2833 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2834 enum ofp_version ofp_version OVS_UNUSED,
2835 struct ofpbuf *ofpacts)
2837 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
2838 enum ofperr error = decode_stack_action(nasp, pop);
2839 return error ? error : nxm_stack_pop_check(pop, NULL);
2843 encode_STACK_op(const struct ofpact_stack *stack_action,
2844 struct nx_action_stack *nasp)
2849 nasp->offset = htons(stack_action->subfield.ofs);
2851 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
2852 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
2853 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
2854 n_bits = htons(stack_action->subfield.n_bits);
2855 ofpbuf_put(&b, &n_bits, sizeof n_bits);
2859 encode_STACK_PUSH(const struct ofpact_stack *stack,
2860 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2862 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
2866 encode_STACK_POP(const struct ofpact_stack *stack,
2867 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
2869 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
2872 static char * OVS_WARN_UNUSED_RESULT
2873 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
2874 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2876 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
2879 static char * OVS_WARN_UNUSED_RESULT
2880 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
2881 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2883 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
2887 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
2889 nxm_format_stack_push(a, s);
2893 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
2895 nxm_format_stack_pop(a, s);
2898 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
2900 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
2901 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
2902 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
2903 * 'n_controllers' controller IDs specified in 'cnt_ids'.
2905 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
2906 * sent only to controllers with id 0.)
2908 struct nx_action_cnt_ids {
2909 ovs_be16 type; /* OFPAT_VENDOR. */
2910 ovs_be16 len; /* Length including slaves. */
2911 ovs_be32 vendor; /* NX_VENDOR_ID. */
2912 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
2914 ovs_be16 n_controllers; /* Number of controllers. */
2915 uint8_t zeros[4]; /* Must be zero. */
2917 /* Followed by 1 or more controller ids.
2919 * uint16_t cnt_ids[]; // Controller ids.
2920 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
2923 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
2926 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
2929 struct ofpact_cnt_ids *ids;
2930 enum ofperr error = 0;
2932 ids = ofpact_put_DEC_TTL(out);
2933 ids->n_controllers = 1;
2934 ofpbuf_put(out, &id, sizeof id);
2936 ofpact_finish(out, &ids->ofpact);
2941 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
2942 enum ofp_version ofp_version OVS_UNUSED,
2945 struct ofpact_cnt_ids *ids;
2949 ids = ofpact_put_DEC_TTL(out);
2950 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
2951 ids->n_controllers = ntohs(nac_ids->n_controllers);
2952 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
2954 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
2955 return OFPERR_NXBRC_MUST_BE_ZERO;
2958 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
2959 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
2960 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
2961 "are required for %"PRIu16" controllers.",
2962 ids_size, ids->n_controllers * sizeof(ovs_be16),
2963 ids->n_controllers);
2964 return OFPERR_OFPBAC_BAD_LEN;
2967 for (i = 0; i < ids->n_controllers; i++) {
2968 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
2969 ofpbuf_put(out, &id, sizeof id);
2973 ofpact_finish(out, &ids->ofpact);
2979 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
2980 enum ofp_version ofp_version, struct ofpbuf *out)
2982 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
2983 || dec_ttl->n_controllers != 1
2984 || dec_ttl->cnt_ids[0] != 0) {
2985 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
2986 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
2990 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2991 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
2993 ids = ofpbuf_put_zeros(out, ids_len);
2994 for (i = 0; i < dec_ttl->n_controllers; i++) {
2995 ids[i] = htons(dec_ttl->cnt_ids[i]);
2998 put_OFPAT_DEC_NW_TTL(out, ofp_version);
3003 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
3005 struct ofpact_cnt_ids *ids;
3008 ofpact_put_DEC_TTL(ofpacts);
3009 ofpbuf_put(ofpacts, &id, sizeof id);
3010 ids = ofpacts->header;
3011 ids->n_controllers++;
3012 ofpact_finish(ofpacts, &ids->ofpact);
3015 static char * OVS_WARN_UNUSED_RESULT
3016 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
3017 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3020 parse_noargs_dec_ttl(ofpacts);
3022 struct ofpact_cnt_ids *ids;
3025 ids = ofpact_put_DEC_TTL(ofpacts);
3026 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3027 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
3028 cntr = strtok_r(NULL, ", ", &arg)) {
3029 uint16_t id = atoi(cntr);
3031 ofpbuf_put(ofpacts, &id, sizeof id);
3032 ids = ofpacts->header;
3033 ids->n_controllers++;
3035 if (!ids->n_controllers) {
3036 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3039 ofpact_finish(ofpacts, &ids->ofpact);
3045 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
3049 ds_put_cstr(s, "dec_ttl");
3050 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
3051 ds_put_cstr(s, "(");
3052 for (i = 0; i < a->n_controllers; i++) {
3054 ds_put_cstr(s, ",");
3056 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
3058 ds_put_cstr(s, ")");
3062 /* Set MPLS label actions. */
3065 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
3066 enum ofp_version ofp_version OVS_UNUSED,
3069 ofpact_put_SET_MPLS_LABEL(out)->label = label;
3074 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3075 enum ofp_version ofp_version,
3078 if (ofp_version < OFP12_VERSION) {
3079 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3081 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
3082 ntohl(label->label));
3086 static char * OVS_WARN_UNUSED_RESULT
3087 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3088 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3090 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3092 return xstrdup("set_mpls_label: expected label.");
3095 mpls_label->label = htonl(atoi(arg));
3100 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3102 ds_put_format(s, "set_mpls_label(%"PRIu32")", ntohl(a->label));
3105 /* Set MPLS TC actions. */
3108 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3109 enum ofp_version ofp_version OVS_UNUSED,
3112 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3117 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3118 enum ofp_version ofp_version, struct ofpbuf *out)
3120 if (ofp_version < OFP12_VERSION) {
3121 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3123 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3127 static char * OVS_WARN_UNUSED_RESULT
3128 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3129 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3131 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3134 return xstrdup("set_mpls_tc: expected tc.");
3137 mpls_tc->tc = atoi(arg);
3142 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3144 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->tc);
3147 /* Set MPLS TTL actions. */
3150 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3151 enum ofp_version ofp_version OVS_UNUSED,
3154 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3159 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3160 enum ofp_version ofp_version, struct ofpbuf *out)
3162 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3165 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3166 * action to 'ofpacts'.
3168 * Returns NULL if successful, otherwise a malloc()'d string describing the
3169 * error. The caller is responsible for freeing the returned string. */
3170 static char * OVS_WARN_UNUSED_RESULT
3171 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3172 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3174 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3177 return xstrdup("set_mpls_ttl: expected ttl.");
3180 mpls_ttl->ttl = atoi(arg);
3185 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3187 ds_put_format(s, "set_mpls_ttl(%"PRIu8")", a->ttl);
3190 /* Decrement MPLS TTL actions. */
3193 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3195 ofpact_put_DEC_MPLS_TTL(out);
3200 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3201 enum ofp_version ofp_version, struct ofpbuf *out)
3203 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3206 static char * OVS_WARN_UNUSED_RESULT
3207 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3208 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3210 ofpact_put_DEC_MPLS_TTL(ofpacts);
3215 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3217 ds_put_cstr(s, "dec_mpls_ttl");
3220 /* Push MPLS label action. */
3223 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3224 enum ofp_version ofp_version OVS_UNUSED,
3227 struct ofpact_push_mpls *oam;
3229 if (!eth_type_mpls(ethertype)) {
3230 return OFPERR_OFPBAC_BAD_ARGUMENT;
3232 oam = ofpact_put_PUSH_MPLS(out);
3233 oam->ethertype = ethertype;
3239 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3240 enum ofp_version ofp_version, struct ofpbuf *out)
3242 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3245 static char * OVS_WARN_UNUSED_RESULT
3246 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3247 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3252 error = str_to_u16(arg, "push_mpls", ðertype);
3254 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3260 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3262 ds_put_format(s, "push_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3265 /* Pop MPLS label action. */
3268 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3269 enum ofp_version ofp_version OVS_UNUSED,
3272 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3277 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3278 enum ofp_version ofp_version, struct ofpbuf *out)
3280 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3283 static char * OVS_WARN_UNUSED_RESULT
3284 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3285 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3290 error = str_to_u16(arg, "pop_mpls", ðertype);
3292 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3298 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3300 ds_put_format(s, "pop_mpls:0x%04"PRIx16, ntohs(a->ethertype));
3303 /* Set tunnel ID actions. */
3306 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3307 enum ofp_version ofp_version OVS_UNUSED,
3310 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3311 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3312 tunnel->tun_id = tun_id;
3317 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3318 enum ofp_version ofp_version OVS_UNUSED,
3321 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3322 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3323 tunnel->tun_id = tun_id;
3328 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3329 enum ofp_version ofp_version, struct ofpbuf *out)
3331 uint64_t tun_id = tunnel->tun_id;
3333 if (ofp_version < OFP12_VERSION) {
3334 if (tun_id <= UINT32_MAX
3335 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3336 put_NXAST_SET_TUNNEL(out, tun_id);
3338 put_NXAST_SET_TUNNEL64(out, tun_id);
3341 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3345 static char * OVS_WARN_UNUSED_RESULT
3346 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3347 enum ofp_raw_action_type raw)
3349 struct ofpact_tunnel *tunnel;
3351 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3352 tunnel->ofpact.raw = raw;
3353 return str_to_u64(arg, &tunnel->tun_id);
3356 static char * OVS_WARN_UNUSED_RESULT
3357 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3358 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3360 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3364 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3366 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3367 (a->tun_id > UINT32_MAX
3368 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3372 /* Set queue action. */
3375 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3376 enum ofp_version ofp_version OVS_UNUSED,
3379 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3384 encode_SET_QUEUE(const struct ofpact_queue *queue,
3385 enum ofp_version ofp_version, struct ofpbuf *out)
3387 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3390 static char * OVS_WARN_UNUSED_RESULT
3391 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3392 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3394 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3398 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3400 ds_put_format(s, "set_queue:%"PRIu32, a->queue_id);
3403 /* Pop queue action. */
3406 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3408 ofpact_put_POP_QUEUE(out);
3413 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3414 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3416 put_NXAST_POP_QUEUE(out);
3419 static char * OVS_WARN_UNUSED_RESULT
3420 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3421 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3423 ofpact_put_POP_QUEUE(ofpacts);
3428 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3430 ds_put_cstr(s, "pop_queue");
3433 /* Action structure for NXAST_FIN_TIMEOUT.
3435 * This action changes the idle timeout or hard timeout, or both, of this
3436 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3437 * When such a packet is observed, the action reduces the rule's idle timeout
3438 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3439 * action has no effect on an existing timeout that is already shorter than the
3440 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3441 * of zero has no effect on the respective timeout.
3443 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3444 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3445 * receipt of the FIN or RST.
3447 * This is useful for quickly discarding learned TCP flows that otherwise will
3448 * take a long time to expire.
3450 * This action is intended for use with an OpenFlow rule that matches only a
3451 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3452 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3453 * RST in any of those flows will cause the entire OpenFlow rule to expire
3454 * early, which is not normally desirable.
3456 struct nx_action_fin_timeout {
3457 ovs_be16 type; /* OFPAT_VENDOR. */
3458 ovs_be16 len; /* 16. */
3459 ovs_be32 vendor; /* NX_VENDOR_ID. */
3460 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3461 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3462 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3463 ovs_be16 pad; /* Must be zero. */
3465 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3468 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3469 enum ofp_version ofp_version OVS_UNUSED,
3472 struct ofpact_fin_timeout *oft;
3474 oft = ofpact_put_FIN_TIMEOUT(out);
3475 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3476 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3481 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3482 enum ofp_version ofp_version OVS_UNUSED,
3485 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3486 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3487 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3490 static char * OVS_WARN_UNUSED_RESULT
3491 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3492 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3494 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3497 while (ofputil_parse_key_value(&arg, &key, &value)) {
3500 if (!strcmp(key, "idle_timeout")) {
3501 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3502 } else if (!strcmp(key, "hard_timeout")) {
3503 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3505 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3517 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3519 ds_put_cstr(s, "fin_timeout(");
3520 if (a->fin_idle_timeout) {
3521 ds_put_format(s, "idle_timeout=%"PRIu16",", a->fin_idle_timeout);
3523 if (a->fin_hard_timeout) {
3524 ds_put_format(s, "hard_timeout=%"PRIu16",", a->fin_hard_timeout);
3527 ds_put_char(s, ')');
3530 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3532 * These actions search one of the switch's flow tables:
3534 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3535 * it specifies the table to search.
3537 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3538 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3539 * table, that is, the OpenFlow flow table that contains the flow from
3540 * which this action was obtained. If this action did not come from a
3541 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3542 * is the current table.
3544 * The flow table lookup uses a flow that may be slightly modified from the
3547 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3548 * is used as the flow's in_port.
3550 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3551 * then its value is used as the flow's in_port. Otherwise, the original
3554 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3555 * resubmit action, then the flow is updated with the new values.
3557 * Following the lookup, the original in_port is restored.
3559 * If the modified flow matched in the flow table, then the corresponding
3560 * actions are executed. Afterward, actions following the resubmit in the
3561 * original set of actions, if any, are executed; any changes made to the
3562 * packet (e.g. changes to VLAN) by secondary actions persist when those
3563 * actions are executed, although the original in_port is restored.
3565 * Resubmit actions may be used any number of times within a set of actions.
3567 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3568 * implementation-defined depth, further resubmit actions are simply ignored.
3570 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3571 * 'pad' to be all-bits-zero.
3573 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3574 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3576 struct nx_action_resubmit {
3577 ovs_be16 type; /* OFPAT_VENDOR. */
3578 ovs_be16 len; /* Length is 16. */
3579 ovs_be32 vendor; /* NX_VENDOR_ID. */
3580 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3581 ovs_be16 in_port; /* New in_port for checking flow table. */
3582 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3585 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3588 decode_NXAST_RAW_RESUBMIT(uint16_t port,
3589 enum ofp_version ofp_version OVS_UNUSED,
3592 struct ofpact_resubmit *resubmit;
3594 resubmit = ofpact_put_RESUBMIT(out);
3595 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3596 resubmit->in_port = u16_to_ofp(port);
3597 resubmit->table_id = 0xff;
3602 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3603 enum ofp_version ofp_version OVS_UNUSED,
3606 struct ofpact_resubmit *resubmit;
3608 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3609 return OFPERR_OFPBAC_BAD_ARGUMENT;
3612 resubmit = ofpact_put_RESUBMIT(out);
3613 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3614 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3615 resubmit->table_id = nar->table;
3620 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3621 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3623 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3625 if (resubmit->table_id == 0xff
3626 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3627 put_NXAST_RESUBMIT(out, in_port);
3629 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3630 nar->table = resubmit->table_id;
3631 nar->in_port = htons(in_port);
3635 static char * OVS_WARN_UNUSED_RESULT
3636 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3637 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3639 struct ofpact_resubmit *resubmit;
3640 char *in_port_s, *table_s;
3642 resubmit = ofpact_put_RESUBMIT(ofpacts);
3644 in_port_s = strsep(&arg, ",");
3645 if (in_port_s && in_port_s[0]) {
3646 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3647 return xasprintf("%s: resubmit to unknown port", in_port_s);
3650 resubmit->in_port = OFPP_IN_PORT;
3653 table_s = strsep(&arg, ",");
3654 if (table_s && table_s[0]) {
3655 uint32_t table_id = 0;
3658 error = str_to_u32(table_s, &table_id);
3662 resubmit->table_id = table_id;
3664 resubmit->table_id = 255;
3667 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3668 return xstrdup("at least one \"in_port\" or \"table\" must be "
3669 "specified on resubmit");
3675 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3677 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3678 ds_put_cstr(s, "resubmit:");
3679 ofputil_format_port(a->in_port, s);
3681 ds_put_format(s, "resubmit(");
3682 if (a->in_port != OFPP_IN_PORT) {
3683 ofputil_format_port(a->in_port, s);
3685 ds_put_char(s, ',');
3686 if (a->table_id != 255) {
3687 ds_put_format(s, "%"PRIu8, a->table_id);
3689 ds_put_char(s, ')');
3693 /* Action structure for NXAST_LEARN.
3695 * This action adds or modifies a flow in an OpenFlow table, similar to
3696 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3697 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3698 * flow's match criteria and actions are built by applying each of the series
3699 * of flow_mod_spec elements included as part of the action.
3701 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3702 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3703 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3704 * bits from a source to a destination. In this case, the header contains
3707 * 15 14 13 12 11 10 0
3708 * +------+---+------+---------------------------------+
3709 * | 0 |src| dst | n_bits |
3710 * +------+---+------+---------------------------------+
3712 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3713 * following table summarizes the meaning of each possible combination.
3714 * Details follow the table:
3717 * --- --- ----------------------------------------------------------
3718 * 0 0 Add match criteria based on value in a field.
3719 * 1 0 Add match criteria based on an immediate value.
3720 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3721 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3722 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3723 * All other combinations are undefined and not allowed.
3725 * The flow_mod_spec header is followed by a source specification and a
3726 * destination specification. The format and meaning of the source
3727 * specification depends on 'src':
3729 * - If 'src' is 0, the source bits are taken from a field in the flow to
3730 * which this action is attached. (This should be a wildcarded field. If
3731 * its value is fully specified then the source bits being copied have
3734 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3735 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3736 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3737 * 'field' and 'ofs' are subject to the same restrictions as the source
3738 * field in NXAST_REG_MOVE.
3740 * - If 'src' is 1, the source bits are a constant value. The source
3741 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3742 * number in network order, the source bits are the 'n_bits'
3743 * least-significant bits. The switch will report an error if other bits
3744 * in the constant are nonzero.
3746 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3747 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3748 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3749 * meaning of the flow_mod_spec depends on 'dst':
3751 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3752 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3753 * packet equal the source bits. 'field' may be any nxm_header with
3754 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3756 * Order is significant. Earlier flow_mod_specs must satisfy any
3757 * prerequisites for matching fields specified later, by copying constant
3758 * values into prerequisite fields.
3760 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3763 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3764 * the new flow. The new flow copies the source bits into
3765 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3768 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3769 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3771 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3772 * empty. It has the following meaning:
3774 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3775 * The new flow outputs to the OpenFlow port specified by the source field.
3776 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3777 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3780 * Resource Management
3781 * -------------------
3783 * A switch has a finite amount of flow table space available for learning.
3784 * When this space is exhausted, no new learning table entries will be learned
3785 * until some existing flow table entries expire. The controller should be
3786 * prepared to handle this by flooding (which can be implemented as a
3787 * low-priority flow).
3789 * If a learned flow matches a single TCP stream with a relatively long
3790 * timeout, one may make the best of resource constraints by setting
3791 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3792 * both, to shorter timeouts. When either of these is specified as a nonzero
3793 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3799 * The following examples give a prose description of the flow_mod_specs along
3800 * with informal notation for how those would be represented and a hex dump of
3801 * the bytes that would be required.
3803 * These examples could work with various nx_action_learn parameters. Typical
3804 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3805 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3807 * 1. Learn input port based on the source MAC, with lookup into
3808 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3810 * Match on in_port=99:
3811 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3812 * ovs_be16(99), 00 63
3813 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3815 * Match Ethernet destination on Ethernet source from packet:
3816 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3817 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3818 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3820 * Set NXM_NX_REG1[16:31] to the packet's input port:
3821 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3822 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3823 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3825 * Given a packet that arrived on port A with Ethernet source address B,
3826 * this would set up the flow "in_port=99, dl_dst=B,
3827 * actions=load:A->NXM_NX_REG1[16..31]".
3829 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
3830 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3831 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
3833 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
3834 * into NXM_NX_REG1[16:31]:
3836 * Match on same VLAN ID as packet:
3837 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
3838 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3839 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
3841 * Match Ethernet destination on Ethernet source from packet:
3842 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3843 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3844 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3846 * Output to the packet's input port:
3847 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
3848 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3850 * Given a packet that arrived on port A with Ethernet source address B in
3851 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
3852 * actions=output:A".
3854 * In syntax accepted by ovs-ofctl, this action is:
3855 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
3856 * output:NXM_OF_IN_PORT[])
3858 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
3859 * 10-second MAC expiration time to make it easier to see what's going on
3861 * ovs-vsctl del-controller br0
3862 * ovs-ofctl del-flows br0
3863 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
3864 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
3865 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
3866 output:NXM_OF_IN_PORT[]), resubmit(,1)"
3867 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
3869 * You can then dump the MAC learning table with:
3871 * ovs-ofctl dump-flows br0 table=1
3876 * For best performance, segregate learned flows into a table that is not used
3877 * for any other flows except possibly for a lowest-priority "catch-all" flow
3878 * (a flow with no match criteria). If different learning actions specify
3879 * different match criteria, use different tables for the learned flows.
3881 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
3882 * These timeouts apply to the flow that is added, which means that a flow with
3883 * an idle timeout will expire when no traffic has been sent *to* the learned
3884 * address. This is not usually the intent in MAC learning; instead, we want
3885 * the MAC learn entry to expire when no traffic has been sent *from* the
3886 * learned address. Use a hard timeout for that.
3889 * Visibility of Changes
3890 * ---------------------
3892 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
3893 * flow translation are visible to flow table lookups made later in the flow
3894 * translation. This means that, in the example above, a MAC learned by the
3895 * learn action in table 0 would be found in table 1 (if the packet being
3896 * processed had the same source and destination MAC address).
3898 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
3899 * modify a flow) by a "learn" action are visible only for later flow
3900 * translations, not for later lookups within the same flow translation. In
3901 * the MAC learning example, a MAC learned by the learn action in table 0 would
3902 * not be found in table 1 if the flow translation would resubmit to table 1
3903 * after the processing of the learn action, meaning that if this MAC had not
3904 * been learned before then the packet would be flooded. */
3905 struct nx_action_learn {
3906 ovs_be16 type; /* OFPAT_VENDOR. */
3907 ovs_be16 len; /* At least 24. */
3908 ovs_be32 vendor; /* NX_VENDOR_ID. */
3909 ovs_be16 subtype; /* NXAST_LEARN. */
3910 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
3911 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
3912 ovs_be16 priority; /* Priority level of flow entry. */
3913 ovs_be64 cookie; /* Cookie for new flow. */
3914 ovs_be16 flags; /* NX_LEARN_F_*. */
3915 uint8_t table_id; /* Table to insert flow entry. */
3916 uint8_t pad; /* Must be zero. */
3917 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
3918 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
3919 /* Followed by a sequence of flow_mod_spec elements, as described above,
3920 * until the end of the action is reached. */
3922 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
3925 get_be16(const void **pp)
3927 const ovs_be16 *p = *pp;
3928 ovs_be16 value = *p;
3934 get_be32(const void **pp)
3936 const ovs_be32 *p = *pp;
3937 ovs_be32 value = get_unaligned_be32(p);
3943 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
3945 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
3946 sf->ofs = ntohs(get_be16(p));
3947 sf->n_bits = n_bits;
3951 learn_min_len(uint16_t header)
3953 int n_bits = header & NX_LEARN_N_BITS_MASK;
3954 int src_type = header & NX_LEARN_SRC_MASK;
3955 int dst_type = header & NX_LEARN_DST_MASK;
3956 unsigned int min_len;
3959 if (src_type == NX_LEARN_SRC_FIELD) {
3960 min_len += sizeof(ovs_be32); /* src_field */
3961 min_len += sizeof(ovs_be16); /* src_ofs */
3963 min_len += DIV_ROUND_UP(n_bits, 16);
3965 if (dst_type == NX_LEARN_DST_MATCH ||
3966 dst_type == NX_LEARN_DST_LOAD) {
3967 min_len += sizeof(ovs_be32); /* dst_field */
3968 min_len += sizeof(ovs_be16); /* dst_ofs */
3973 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
3974 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
3976 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
3977 enum ofp_version ofp_version OVS_UNUSED,
3978 struct ofpbuf *ofpacts)
3980 struct ofpact_learn *learn;
3981 const void *p, *end;
3984 return OFPERR_OFPBAC_BAD_ARGUMENT;
3987 learn = ofpact_put_LEARN(ofpacts);
3989 learn->idle_timeout = ntohs(nal->idle_timeout);
3990 learn->hard_timeout = ntohs(nal->hard_timeout);
3991 learn->priority = ntohs(nal->priority);
3992 learn->cookie = nal->cookie;
3993 learn->table_id = nal->table_id;
3994 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
3995 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
3997 learn->flags = ntohs(nal->flags);
3998 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
3999 NX_LEARN_F_DELETE_LEARNED)) {
4000 return OFPERR_OFPBAC_BAD_ARGUMENT;
4003 if (learn->table_id == 0xff) {
4004 return OFPERR_OFPBAC_BAD_ARGUMENT;
4007 end = (char *) nal + ntohs(nal->len);
4008 for (p = nal + 1; p != end; ) {
4009 struct ofpact_learn_spec *spec;
4010 uint16_t header = ntohs(get_be16(&p));
4016 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
4017 learn = ofpacts->header;
4020 spec->src_type = header & NX_LEARN_SRC_MASK;
4021 spec->dst_type = header & NX_LEARN_DST_MASK;
4022 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
4024 /* Check for valid src and dst type combination. */
4025 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4026 spec->dst_type == NX_LEARN_DST_LOAD ||
4027 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
4028 spec->src_type == NX_LEARN_SRC_FIELD)) {
4031 return OFPERR_OFPBAC_BAD_ARGUMENT;
4034 /* Check that the arguments don't overrun the end of the action. */
4035 if ((char *) end - (char *) p < learn_min_len(header)) {
4036 return OFPERR_OFPBAC_BAD_LEN;
4039 /* Get the source. */
4040 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4041 get_subfield(spec->n_bits, &p, &spec->src);
4043 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4045 bitwise_copy(p, p_bytes, 0,
4046 &spec->src_imm, sizeof spec->src_imm, 0,
4048 p = (const uint8_t *) p + p_bytes;
4051 /* Get the destination. */
4052 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4053 spec->dst_type == NX_LEARN_DST_LOAD) {
4054 get_subfield(spec->n_bits, &p, &spec->dst);
4057 ofpact_finish(ofpacts, &learn->ofpact);
4059 if (!is_all_zeros(p, (char *) end - (char *) p)) {
4060 return OFPERR_OFPBAC_BAD_ARGUMENT;
4067 put_be16(struct ofpbuf *b, ovs_be16 x)
4069 ofpbuf_put(b, &x, sizeof x);
4073 put_be32(struct ofpbuf *b, ovs_be32 x)
4075 ofpbuf_put(b, &x, sizeof x);
4079 put_u16(struct ofpbuf *b, uint16_t x)
4081 put_be16(b, htons(x));
4085 put_u32(struct ofpbuf *b, uint32_t x)
4087 put_be32(b, htonl(x));
4091 encode_LEARN(const struct ofpact_learn *learn,
4092 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4094 const struct ofpact_learn_spec *spec;
4095 struct nx_action_learn *nal;
4098 start_ofs = out->size;
4099 nal = put_NXAST_LEARN(out);
4100 nal->idle_timeout = htons(learn->idle_timeout);
4101 nal->hard_timeout = htons(learn->hard_timeout);
4102 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4103 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4104 nal->priority = htons(learn->priority);
4105 nal->cookie = learn->cookie;
4106 nal->flags = htons(learn->flags);
4107 nal->table_id = learn->table_id;
4109 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4110 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4112 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4113 put_u32(out, mf_nxm_header(spec->src.field->id));
4114 put_u16(out, spec->src.ofs);
4116 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4117 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4118 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4119 bits, n_dst_bytes, 0,
4123 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4124 spec->dst_type == NX_LEARN_DST_LOAD) {
4125 put_u32(out, mf_nxm_header(spec->dst.field->id));
4126 put_u16(out, spec->dst.ofs);
4130 pad_ofpat(out, start_ofs);
4133 static char * OVS_WARN_UNUSED_RESULT
4134 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4135 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4137 return learn_parse(arg, ofpacts);
4141 format_LEARN(const struct ofpact_learn *a, struct ds *s)
4146 /* Action structure for NXAST_CONJUNCTION. */
4147 struct nx_action_conjunction {
4148 ovs_be16 type; /* OFPAT_VENDOR. */
4149 ovs_be16 len; /* At least 16. */
4150 ovs_be32 vendor; /* NX_VENDOR_ID. */
4151 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4156 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4159 add_conjunction(struct ofpbuf *out,
4160 uint32_t id, uint8_t clause, uint8_t n_clauses)
4162 struct ofpact_conjunction *oc;
4164 oc = ofpact_put_CONJUNCTION(out);
4166 oc->clause = clause;
4167 oc->n_clauses = n_clauses;
4171 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4172 enum ofp_version ofp_version OVS_UNUSED,
4175 if (nac->n_clauses < 2 || nac->n_clauses > 64
4176 || nac->clause >= nac->n_clauses) {
4177 return OFPERR_NXBAC_BAD_CONJUNCTION;
4179 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4185 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4186 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4188 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4189 nac->clause = oc->clause;
4190 nac->n_clauses = oc->n_clauses;
4191 nac->id = htonl(oc->id);
4195 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4197 ds_put_format(s, "conjunction(%"PRIu32",%"PRIu8"/%"PRIu8")",
4198 oc->id, oc->clause + 1, oc->n_clauses);
4201 static char * OVS_WARN_UNUSED_RESULT
4202 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4203 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4210 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4211 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4212 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4215 if (n_clauses < 2) {
4216 return xstrdup("conjunction must have at least 2 clauses");
4217 } else if (n_clauses > 64) {
4218 return xstrdup("conjunction must have at most 64 clauses");
4219 } else if (clause < 1) {
4220 return xstrdup("clause index must be positive");
4221 } else if (clause > n_clauses) {
4222 return xstrdup("clause index must be less than or equal to "
4223 "number of clauses");
4226 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4230 /* Action structure for NXAST_MULTIPATH.
4232 * This action performs the following steps in sequence:
4234 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4235 * Refer to the definition of "enum nx_mp_fields" for details.
4237 * The 'basis' value is used as a universal hash parameter, that is,
4238 * different values of 'basis' yield different hash functions. The
4239 * particular universal hash function used is implementation-defined.
4241 * The hashed fields' values are drawn from the current state of the
4242 * flow, including all modifications that have been made by actions up to
4245 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4246 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4249 * The output of the algorithm is 'link', an unsigned integer less than
4250 * or equal to 'max_link'.
4252 * Some algorithms use 'arg' as an additional argument.
4254 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4255 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4258 * The switch will reject actions that have an unknown 'fields', or an unknown
4259 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4260 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4261 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4263 struct nx_action_multipath {
4264 ovs_be16 type; /* OFPAT_VENDOR. */
4265 ovs_be16 len; /* Length is 32. */
4266 ovs_be32 vendor; /* NX_VENDOR_ID. */
4267 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4269 /* What fields to hash and how. */
4270 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4271 ovs_be16 basis; /* Universal hash parameter. */
4274 /* Multipath link choice algorithm to apply to hash value. */
4275 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4276 ovs_be16 max_link; /* Number of output links, minus 1. */
4277 ovs_be32 arg; /* Algorithm-specific argument. */
4280 /* Where to store the result. */
4281 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4282 ovs_be32 dst; /* Destination. */
4284 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4287 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4288 enum ofp_version ofp_version OVS_UNUSED,
4291 uint32_t n_links = ntohs(nam->max_link) + 1;
4292 size_t min_n_bits = log_2_ceil(n_links);
4293 struct ofpact_multipath *mp;
4295 mp = ofpact_put_MULTIPATH(out);
4296 mp->fields = ntohs(nam->fields);
4297 mp->basis = ntohs(nam->basis);
4298 mp->algorithm = ntohs(nam->algorithm);
4299 mp->max_link = ntohs(nam->max_link);
4300 mp->arg = ntohl(nam->arg);
4301 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4302 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4303 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4305 if (!flow_hash_fields_valid(mp->fields)) {
4306 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4307 return OFPERR_OFPBAC_BAD_ARGUMENT;
4308 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4309 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4310 && mp->algorithm != NX_MP_ALG_HRW
4311 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4312 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4313 return OFPERR_OFPBAC_BAD_ARGUMENT;
4314 } else if (mp->dst.n_bits < min_n_bits) {
4315 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4316 "%"PRIu32" links", min_n_bits, n_links);
4317 return OFPERR_OFPBAC_BAD_ARGUMENT;
4320 return multipath_check(mp, NULL);
4324 encode_MULTIPATH(const struct ofpact_multipath *mp,
4325 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4327 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4329 nam->fields = htons(mp->fields);
4330 nam->basis = htons(mp->basis);
4331 nam->algorithm = htons(mp->algorithm);
4332 nam->max_link = htons(mp->max_link);
4333 nam->arg = htonl(mp->arg);
4334 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4335 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4338 static char * OVS_WARN_UNUSED_RESULT
4339 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4340 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4342 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4346 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4348 multipath_format(a, s);
4351 /* Action structure for NXAST_NOTE.
4353 * This action has no effect. It is variable length. The switch does not
4354 * attempt to interpret the user-defined 'note' data in any way. A controller
4355 * can use this action to attach arbitrary metadata to a flow.
4357 * This action might go away in the future.
4359 struct nx_action_note {
4360 ovs_be16 type; /* OFPAT_VENDOR. */
4361 ovs_be16 len; /* A multiple of 8, but at least 16. */
4362 ovs_be32 vendor; /* NX_VENDOR_ID. */
4363 ovs_be16 subtype; /* NXAST_NOTE. */
4364 uint8_t note[6]; /* Start of user-defined data. */
4365 /* Possibly followed by additional user-defined data. */
4367 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4370 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4371 enum ofp_version ofp_version OVS_UNUSED,
4374 struct ofpact_note *note;
4375 unsigned int length;
4377 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4378 note = ofpact_put_NOTE(out);
4379 note->length = length;
4380 ofpbuf_put(out, nan->note, length);
4381 ofpact_finish(out, out->header);
4387 encode_NOTE(const struct ofpact_note *note,
4388 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4390 size_t start_ofs = out->size;
4391 struct nx_action_note *nan;
4393 put_NXAST_NOTE(out);
4394 out->size = out->size - sizeof nan->note;
4396 ofpbuf_put(out, note->data, note->length);
4397 pad_ofpat(out, start_ofs);
4400 static char * OVS_WARN_UNUSED_RESULT
4401 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4402 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4404 struct ofpact_note *note;
4406 note = ofpact_put_NOTE(ofpacts);
4407 while (*arg != '\0') {
4418 byte = hexits_value(arg, 2, &ok);
4420 return xstrdup("bad hex digit in `note' argument");
4422 ofpbuf_put(ofpacts, &byte, 1);
4424 note = ofpacts->header;
4429 ofpact_finish(ofpacts, ¬e->ofpact);
4434 format_NOTE(const struct ofpact_note *a, struct ds *s)
4438 ds_put_cstr(s, "note:");
4439 for (i = 0; i < a->length; i++) {
4441 ds_put_char(s, '.');
4443 ds_put_format(s, "%02"PRIx8, a->data[i]);
4450 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4452 ofpact_put_EXIT(out);
4457 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4458 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4460 put_NXAST_EXIT(out);
4463 static char * OVS_WARN_UNUSED_RESULT
4464 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4465 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4467 ofpact_put_EXIT(ofpacts);
4472 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4474 ds_put_cstr(s, "exit");
4477 /* Unroll xlate action. */
4480 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4481 enum ofp_version ofp_version OVS_UNUSED,
4482 struct ofpbuf *out OVS_UNUSED)
4487 static char * OVS_WARN_UNUSED_RESULT
4488 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4489 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4496 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
4499 ds_put_cstr(s, "unroll_xlate");
4502 /* Action structure for NXAST_SAMPLE.
4504 * Samples matching packets with the given probability and sends them
4505 * each to the set of collectors identified with the given ID. The
4506 * probability is expressed as a number of packets to be sampled out
4507 * of USHRT_MAX packets, and must be >0.
4509 * When sending packet samples to IPFIX collectors, the IPFIX flow
4510 * record sent for each sampled packet is associated with the given
4511 * observation domain ID and observation point ID. Each IPFIX flow
4512 * record contain the sampled packet's headers when executing this
4513 * rule. If a sampled packet's headers are modified by previous
4514 * actions in the flow, those modified headers are sent. */
4515 struct nx_action_sample {
4516 ovs_be16 type; /* OFPAT_VENDOR. */
4517 ovs_be16 len; /* Length is 24. */
4518 ovs_be32 vendor; /* NX_VENDOR_ID. */
4519 ovs_be16 subtype; /* NXAST_SAMPLE. */
4520 ovs_be16 probability; /* Fraction of packets to sample. */
4521 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4522 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4523 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4525 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4528 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4529 enum ofp_version ofp_version OVS_UNUSED,
4532 struct ofpact_sample *sample;
4534 sample = ofpact_put_SAMPLE(out);
4535 sample->probability = ntohs(nas->probability);
4536 sample->collector_set_id = ntohl(nas->collector_set_id);
4537 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4538 sample->obs_point_id = ntohl(nas->obs_point_id);
4540 if (sample->probability == 0) {
4541 return OFPERR_OFPBAC_BAD_ARGUMENT;
4548 encode_SAMPLE(const struct ofpact_sample *sample,
4549 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4551 struct nx_action_sample *nas;
4553 nas = put_NXAST_SAMPLE(out);
4554 nas->probability = htons(sample->probability);
4555 nas->collector_set_id = htonl(sample->collector_set_id);
4556 nas->obs_domain_id = htonl(sample->obs_domain_id);
4557 nas->obs_point_id = htonl(sample->obs_point_id);
4560 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4561 * action to 'ofpacts'.
4563 * Returns NULL if successful, otherwise a malloc()'d string describing the
4564 * error. The caller is responsible for freeing the returned string. */
4565 static char * OVS_WARN_UNUSED_RESULT
4566 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4567 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4569 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4572 while (ofputil_parse_key_value(&arg, &key, &value)) {
4575 if (!strcmp(key, "probability")) {
4576 error = str_to_u16(value, "probability", &os->probability);
4577 if (!error && os->probability == 0) {
4578 error = xasprintf("invalid probability value \"%s\"", value);
4580 } else if (!strcmp(key, "collector_set_id")) {
4581 error = str_to_u32(value, &os->collector_set_id);
4582 } else if (!strcmp(key, "obs_domain_id")) {
4583 error = str_to_u32(value, &os->obs_domain_id);
4584 } else if (!strcmp(key, "obs_point_id")) {
4585 error = str_to_u32(value, &os->obs_point_id);
4587 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4594 if (os->probability == 0) {
4595 return xstrdup("non-zero \"probability\" must be specified on sample");
4601 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4603 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4604 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4605 a->probability, a->collector_set_id,
4606 a->obs_domain_id, a->obs_point_id);
4609 /* debug_recirc instruction. */
4611 static bool enable_debug;
4614 ofpact_dummy_enable(void)
4616 enable_debug = true;
4620 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4622 if (!enable_debug) {
4623 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4626 ofpact_put_DEBUG_RECIRC(out);
4631 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4632 enum ofp_version ofp_version OVS_UNUSED,
4635 put_NXAST_DEBUG_RECIRC(out);
4638 static char * OVS_WARN_UNUSED_RESULT
4639 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4640 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4642 ofpact_put_DEBUG_RECIRC(ofpacts);
4647 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4649 ds_put_cstr(s, "debug_recirc");
4652 /* Action structure for NXAST_CT.
4654 * Pass traffic to the connection tracker.
4656 * There are two important concepts to understanding the connection tracking
4657 * interface: Packet state and Connection state. Packets may be "Untracked" or
4658 * "Tracked". Connections may be "Uncommitted" or "Committed".
4662 * Untracked packets have not yet passed through the connection tracker,
4663 * and the connection state for such packets is unknown. In most cases,
4664 * packets entering the OpenFlow pipeline will initially be in the
4665 * untracked state. Untracked packets may become tracked by executing
4666 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4667 * about the connection available, in particular the connection state.
4669 * Tracked packets have previously passed through the connection tracker.
4670 * These packets will remain tracked through until the end of the OpenFlow
4671 * pipeline. Tracked packets which have NXAST_CT executed with a
4672 * "recirc_table" specified will return to the tracked state.
4674 * The packet state is only significant for the duration of packet
4675 * processing within the OpenFlow pipeline.
4677 * - Connection State:
4679 * Multiple packets may be associated with a single connection. Initially,
4680 * all connections are uncommitted. The connection state corresponding to
4681 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4683 * Uncommitted connections have no state stored about them. Uncommitted
4684 * connections may transition into the committed state by executing
4685 * NXAST_CT with the NX_CT_F_COMMIT flag.
4687 * Once a connection becomes committed, information may be gathered about
4688 * the connection by passing subsequent packets through the connection
4689 * tracker, and the state of the connection will be stored beyond the
4690 * lifetime of packet processing.
4692 * Connections may transition back into the uncommitted state due to
4693 * external timers, or due to the contents of packets that are sent to the
4694 * connection tracker. This behaviour is outside of the scope of the
4695 * OpenFlow interface.
4697 * The "zone" specifies a context within which the tracking is done:
4699 * The connection tracking zone is a 16-bit number. Each zone is an
4700 * independent connection tracking context. The connection state for each
4701 * connection is completely separate for each zone, so if a connection
4702 * is committed to zone A, then it will remain uncommitted in zone B.
4703 * If NXAST_CT is executed with the same zone multiple times, later
4704 * executions have no effect.
4706 * If 'zone_src' is nonzero, this specifies that the zone should be
4707 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4708 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4709 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4710 * are the same as the acceptable nxm_header values for the 'src' field of
4713 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4714 * connection tracking zone.
4716 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4718 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4719 * packet will be logically cloned prior to executing this action. One
4720 * copy will be sent to the connection tracker, then will be re-injected
4721 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
4722 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4723 * fields will be populated. The original instance of the packet will
4724 * continue the current actions list. This can be thought of as similar to
4725 * the effect of the "output" action: One copy is sent out (in this case,
4726 * to the connection tracker), but the current copy continues processing.
4728 * It is strongly recommended that this table is later than the current
4729 * table, to prevent loops.
4731 * The "alg" attaches protocol-specific behaviour to this action:
4733 * The ALG is a 16-bit number which specifies that additional
4734 * processing should be applied to this traffic.
4736 * Protocol | Value | Meaning
4737 * --------------------------------------------------------------------
4738 * None | 0 | No protocol-specific behaviour.
4739 * FTP | 21 | Parse FTP control connections and observe the
4740 * | | negotiation of related data connections.
4741 * Other | Other | Unsupported protocols.
4743 * By way of example, if FTP control connections have this action applied
4744 * with the ALG set to FTP (21), then the connection tracker will observe
4745 * the negotiation of data connections. This allows the connection
4746 * tracker to identify subsequent data connections as "related" to this
4747 * existing connection. The "related" flag will be populated in the
4748 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
4751 * Zero or more actions may immediately follow this action. These actions will
4752 * be executed within the context of the connection tracker, and they require
4753 * the NX_CT_F_COMMIT flag to be set.
4755 struct nx_action_conntrack {
4756 ovs_be16 type; /* OFPAT_VENDOR. */
4757 ovs_be16 len; /* At least 24. */
4758 ovs_be32 vendor; /* NX_VENDOR_ID. */
4759 ovs_be16 subtype; /* NXAST_CT. */
4760 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
4761 * Unspecified flag bits must be zero. */
4762 ovs_be32 zone_src; /* Connection tracking context. */
4764 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
4765 ovs_be16 zone_imm; /* Immediate value for zone. */
4767 uint8_t recirc_table; /* Recirculate to a specific table, or
4768 NX_CT_RECIRC_NONE for no recirculation. */
4769 uint8_t pad[3]; /* Zeroes */
4770 ovs_be16 alg; /* Well-known port number for the protocol.
4771 * 0 indicates no ALG is required. */
4772 /* Followed by a sequence of zero or more OpenFlow actions. The length of
4773 * these is included in 'len'. */
4775 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
4778 decode_ct_zone(const struct nx_action_conntrack *nac,
4779 struct ofpact_conntrack *out)
4781 if (nac->zone_src) {
4784 out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
4785 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
4786 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
4787 error = mf_check_src(&out->zone_src, NULL);
4792 if (out->zone_src.n_bits != 16) {
4793 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
4794 out->zone_src.n_bits);
4795 return OFPERR_OFPBAC_BAD_SET_LEN;
4798 out->zone_src.field = NULL;
4799 out->zone_imm = ntohs(nac->zone_imm);
4806 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
4807 enum ofp_version ofp_version, struct ofpbuf *out)
4809 const size_t ct_offset = ofpacts_pull(out);
4810 struct ofpact_conntrack *conntrack;
4811 struct ofpbuf openflow;
4814 conntrack = ofpact_put_CT(out);
4815 conntrack->flags = ntohs(nac->flags);
4816 error = decode_ct_zone(nac, conntrack);
4820 conntrack->recirc_table = nac->recirc_table;
4821 conntrack->alg = ntohs(nac->alg);
4823 ofpbuf_pull(out, sizeof(*conntrack));
4825 ofpbuf_use_const(&openflow, nac + 1, ntohs(nac->len) - sizeof(*nac));
4826 error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
4828 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4834 conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
4835 out->header = &conntrack->ofpact;
4836 ofpact_finish(out, &conntrack->ofpact);
4838 if (conntrack->ofpact.len > sizeof(*conntrack)
4839 && !(conntrack->flags & NX_CT_F_COMMIT)) {
4840 const struct ofpact *a;
4841 size_t ofpacts_len = conntrack->ofpact.len - sizeof(*conntrack);
4843 OFPACT_FOR_EACH (a, conntrack->actions, ofpacts_len) {
4844 if (a->type != OFPACT_NAT || ofpact_get_NAT(a)->flags
4845 || ofpact_get_NAT(a)->range_af != AF_UNSPEC) {
4846 VLOG_WARN_RL(&rl, "CT action requires commit flag if actions "
4847 "other than NAT without arguments are specified.");
4848 error = OFPERR_OFPBAC_BAD_ARGUMENT;
4855 ofpbuf_push_uninit(out, ct_offset);
4860 encode_CT(const struct ofpact_conntrack *conntrack,
4861 enum ofp_version ofp_version, struct ofpbuf *out)
4863 struct nx_action_conntrack *nac;
4864 const size_t ofs = out->size;
4867 nac = put_NXAST_CT(out);
4868 nac->flags = htons(conntrack->flags);
4869 if (conntrack->zone_src.field) {
4870 nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
4871 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
4872 conntrack->zone_src.n_bits);
4874 nac->zone_src = htonl(0);
4875 nac->zone_imm = htons(conntrack->zone_imm);
4877 nac->recirc_table = conntrack->recirc_table;
4878 nac->alg = htons(conntrack->alg);
4880 len = ofpacts_put_openflow_actions(conntrack->actions,
4881 ofpact_ct_get_action_len(conntrack),
4883 len += sizeof(*nac);
4884 nac = ofpbuf_at(out, ofs, sizeof(*nac));
4885 nac->len = htons(len);
4888 static char * OVS_WARN_UNUSED_RESULT parse_NAT(char *arg, struct ofpbuf *,
4889 enum ofputil_protocol * OVS_UNUSED);
4891 /* Parses 'arg' as the argument to a "ct" action, and appends such an
4892 * action to 'ofpacts'.
4894 * Returns NULL if successful, otherwise a malloc()'d string describing the
4895 * error. The caller is responsible for freeing the returned string. */
4896 static char * OVS_WARN_UNUSED_RESULT
4897 parse_CT(char *arg, struct ofpbuf *ofpacts,
4898 enum ofputil_protocol *usable_protocols)
4900 const size_t ct_offset = ofpacts_pull(ofpacts);
4901 struct ofpact_conntrack *oc;
4905 oc = ofpact_put_CT(ofpacts);
4907 oc->recirc_table = NX_CT_RECIRC_NONE;
4908 while (ofputil_parse_key_value(&arg, &key, &value)) {
4909 if (!strcmp(key, "commit")) {
4910 oc->flags |= NX_CT_F_COMMIT;
4911 } else if (!strcmp(key, "table")) {
4912 error = str_to_u8(value, "recirc_table", &oc->recirc_table);
4913 if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
4914 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
4916 } else if (!strcmp(key, "zone")) {
4917 error = str_to_u16(value, "zone", &oc->zone_imm);
4921 error = mf_parse_subfield(&oc->zone_src, value);
4926 } else if (!strcmp(key, "alg")) {
4927 error = str_to_connhelper(value, &oc->alg);
4928 } else if (!strcmp(key, "nat")) {
4929 const size_t nat_offset = ofpacts_pull(ofpacts);
4931 error = parse_NAT(value, ofpacts, usable_protocols);
4932 /* Update CT action pointer and length. */
4933 ofpacts->header = ofpbuf_push_uninit(ofpacts, nat_offset);
4934 oc = ofpacts->header;
4935 } else if (!strcmp(key, "exec")) {
4936 /* Hide existing actions from ofpacts_parse_copy(), so the
4937 * nesting can be handled transparently. */
4938 enum ofputil_protocol usable_protocols2;
4939 const size_t exec_offset = ofpacts_pull(ofpacts);
4941 /* Initializes 'usable_protocol2', fold it back to
4942 * '*usable_protocols' afterwards, so that we do not lose
4943 * restrictions already in there. */
4944 error = ofpacts_parse_copy(value, ofpacts, &usable_protocols2,
4946 *usable_protocols &= usable_protocols2;
4947 ofpacts->header = ofpbuf_push_uninit(ofpacts, exec_offset);
4948 oc = ofpacts->header;
4950 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
4957 ofpact_finish(ofpacts, &oc->ofpact);
4958 ofpbuf_push_uninit(ofpacts, ct_offset);
4963 format_alg(int port, struct ds *s)
4965 if (port == IPPORT_FTP) {
4966 ds_put_format(s, "alg=ftp,");
4968 ds_put_format(s, "alg=%d,", port);
4972 static void format_NAT(const struct ofpact_nat *a, struct ds *ds);
4975 format_CT(const struct ofpact_conntrack *a, struct ds *s)
4977 ds_put_cstr(s, "ct(");
4978 if (a->flags & NX_CT_F_COMMIT) {
4979 ds_put_cstr(s, "commit,");
4981 if (a->recirc_table != NX_CT_RECIRC_NONE) {
4982 ds_put_format(s, "table=%"PRIu8",", a->recirc_table);
4984 if (a->zone_src.field) {
4985 ds_put_format(s, "zone=");
4986 mf_format_subfield(&a->zone_src, s);
4987 ds_put_char(s, ',');
4988 } else if (a->zone_imm) {
4989 ds_put_format(s, "zone=%"PRIu16",", a->zone_imm);
4991 /* If the first action is a NAT action, format it outside of the 'exec'
4993 const struct ofpact *action = a->actions;
4994 size_t actions_len = ofpact_ct_get_action_len(a);
4995 if (actions_len && action->type == OFPACT_NAT) {
4996 format_NAT(ofpact_get_NAT(action), s);
4997 ds_put_char(s, ',');
4998 actions_len -= OFPACT_ALIGN(action->len);
4999 action = ofpact_next(action);
5002 ds_put_cstr(s, "exec(");
5003 ofpacts_format(action, actions_len, s);
5004 ds_put_cstr(s, "),");
5006 format_alg(a->alg, s);
5008 ds_put_char(s, ')');
5013 /* Which optional fields are present? */
5015 NX_NAT_RANGE_IPV4_MIN = 1 << 0, /* ovs_be32 */
5016 NX_NAT_RANGE_IPV4_MAX = 1 << 1, /* ovs_be32 */
5017 NX_NAT_RANGE_IPV6_MIN = 1 << 2, /* struct in6_addr */
5018 NX_NAT_RANGE_IPV6_MAX = 1 << 3, /* struct in6_addr */
5019 NX_NAT_RANGE_PROTO_MIN = 1 << 4, /* ovs_be16 */
5020 NX_NAT_RANGE_PROTO_MAX = 1 << 5, /* ovs_be16 */
5023 /* Action structure for NXAST_NAT. */
5024 struct nx_action_nat {
5025 ovs_be16 type; /* OFPAT_VENDOR. */
5026 ovs_be16 len; /* At least 16. */
5027 ovs_be32 vendor; /* NX_VENDOR_ID. */
5028 ovs_be16 subtype; /* NXAST_NAT. */
5029 uint8_t pad[2]; /* Must be zero. */
5030 ovs_be16 flags; /* Zero or more NX_NAT_F_* flags.
5031 * Unspecified flag bits must be zero. */
5032 ovs_be16 range_present; /* NX_NAT_RANGE_* */
5033 /* Followed by optional parameters as specified by 'range_present' */
5035 OFP_ASSERT(sizeof(struct nx_action_nat) == 16);
5038 encode_NAT(const struct ofpact_nat *nat,
5039 enum ofp_version ofp_version OVS_UNUSED,
5042 struct nx_action_nat *nan;
5043 const size_t ofs = out->size;
5044 uint16_t range_present = 0;
5046 nan = put_NXAST_NAT(out);
5047 nan->flags = htons(nat->flags);
5048 if (nat->range_af == AF_INET) {
5049 if (nat->range.addr.ipv4.min) {
5050 ovs_be32 *min = ofpbuf_put_uninit(out, sizeof *min);
5051 *min = nat->range.addr.ipv4.min;
5052 range_present |= NX_NAT_RANGE_IPV4_MIN;
5054 if (nat->range.addr.ipv4.max) {
5055 ovs_be32 *max = ofpbuf_put_uninit(out, sizeof *max);
5056 *max = nat->range.addr.ipv4.max;
5057 range_present |= NX_NAT_RANGE_IPV4_MAX;
5059 } else if (nat->range_af == AF_INET6) {
5060 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5061 struct in6_addr *min = ofpbuf_put_uninit(out, sizeof *min);
5062 *min = nat->range.addr.ipv6.min;
5063 range_present |= NX_NAT_RANGE_IPV6_MIN;
5065 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.max)) {
5066 struct in6_addr *max = ofpbuf_put_uninit(out, sizeof *max);
5067 *max = nat->range.addr.ipv6.max;
5068 range_present |= NX_NAT_RANGE_IPV6_MAX;
5071 if (nat->range_af != AF_UNSPEC) {
5072 if (nat->range.proto.min) {
5073 ovs_be16 *min = ofpbuf_put_uninit(out, sizeof *min);
5074 *min = htons(nat->range.proto.min);
5075 range_present |= NX_NAT_RANGE_PROTO_MIN;
5077 if (nat->range.proto.max) {
5078 ovs_be16 *max = ofpbuf_put_uninit(out, sizeof *max);
5079 *max = htons(nat->range.proto.max);
5080 range_present |= NX_NAT_RANGE_PROTO_MAX;
5083 pad_ofpat(out, ofs);
5084 nan = ofpbuf_at(out, ofs, sizeof *nan);
5085 nan->range_present = htons(range_present);
5089 decode_NXAST_RAW_NAT(const struct nx_action_nat *nan,
5090 enum ofp_version ofp_version OVS_UNUSED,
5093 struct ofpact_nat *nat;
5094 uint16_t range_present = ntohs(nan->range_present);
5095 const char *opts = (char *)(nan + 1);
5096 uint16_t len = ntohs(nan->len) - sizeof *nan;
5098 nat = ofpact_put_NAT(out);
5099 nat->flags = ntohs(nan->flags);
5101 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
5102 (LEN >= sizeof(TYPE) \
5103 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
5104 SRC += sizeof(TYPE)) \
5107 nat->range_af = AF_UNSPEC;
5108 if (range_present & NX_NAT_RANGE_IPV4_MIN) {
5109 if (range_present & (NX_NAT_RANGE_IPV6_MIN | NX_NAT_RANGE_IPV6_MAX)) {
5110 return OFPERR_OFPBAC_BAD_ARGUMENT;
5113 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.min, opts, len, ovs_be32)
5114 || !nat->range.addr.ipv4.min) {
5115 return OFPERR_OFPBAC_BAD_ARGUMENT;
5118 nat->range_af = AF_INET;
5120 if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5121 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.max, opts, len,
5123 return OFPERR_OFPBAC_BAD_ARGUMENT;
5125 if (ntohl(nat->range.addr.ipv4.max)
5126 < ntohl(nat->range.addr.ipv4.min)) {
5127 return OFPERR_OFPBAC_BAD_ARGUMENT;
5130 } else if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5131 return OFPERR_OFPBAC_BAD_ARGUMENT;
5132 } else if (range_present & NX_NAT_RANGE_IPV6_MIN) {
5133 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.min, opts, len,
5135 || ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5136 return OFPERR_OFPBAC_BAD_ARGUMENT;
5139 nat->range_af = AF_INET6;
5141 if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5142 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.max, opts, len,
5144 return OFPERR_OFPBAC_BAD_ARGUMENT;
5146 if (memcmp(&nat->range.addr.ipv6.max, &nat->range.addr.ipv6.min,
5147 sizeof(struct in6_addr)) < 0) {
5148 return OFPERR_OFPBAC_BAD_ARGUMENT;
5151 } else if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5152 return OFPERR_OFPBAC_BAD_ARGUMENT;
5155 if (range_present & NX_NAT_RANGE_PROTO_MIN) {
5158 if (nat->range_af == AF_UNSPEC) {
5159 return OFPERR_OFPBAC_BAD_ARGUMENT;
5161 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16) || proto == 0) {
5162 return OFPERR_OFPBAC_BAD_ARGUMENT;
5164 nat->range.proto.min = ntohs(proto);
5165 if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5166 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16)) {
5167 return OFPERR_OFPBAC_BAD_ARGUMENT;
5169 nat->range.proto.max = ntohs(proto);
5170 if (nat->range.proto.max < nat->range.proto.min) {
5171 return OFPERR_OFPBAC_BAD_ARGUMENT;
5174 } else if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5175 return OFPERR_OFPBAC_BAD_ARGUMENT;
5182 format_NAT(const struct ofpact_nat *a, struct ds *ds)
5184 ds_put_cstr(ds, "nat");
5186 if (a->flags & (NX_NAT_F_SRC | NX_NAT_F_DST)) {
5187 ds_put_char(ds, '(');
5188 ds_put_cstr(ds, a->flags & NX_NAT_F_SRC ? "src" : "dst");
5190 if (a->range_af != AF_UNSPEC) {
5191 ds_put_cstr(ds, "=");
5193 if (a->range_af == AF_INET) {
5194 ds_put_format(ds, IP_FMT, IP_ARGS(a->range.addr.ipv4.min));
5196 if (a->range.addr.ipv4.max
5197 && a->range.addr.ipv4.max != a->range.addr.ipv4.min) {
5198 ds_put_format(ds, "-"IP_FMT,
5199 IP_ARGS(a->range.addr.ipv4.max));
5201 } else if (a->range_af == AF_INET6) {
5202 ipv6_format_addr_bracket(&a->range.addr.ipv6.min, ds,
5203 a->range.proto.min);
5205 if (!ipv6_mask_is_any(&a->range.addr.ipv6.max)
5206 && memcmp(&a->range.addr.ipv6.max, &a->range.addr.ipv6.min,
5207 sizeof(struct in6_addr)) != 0) {
5208 ds_put_char(ds, '-');
5209 ipv6_format_addr_bracket(&a->range.addr.ipv6.max, ds,
5210 a->range.proto.min);
5213 if (a->range.proto.min) {
5214 ds_put_char(ds, ':');
5215 ds_put_format(ds, "%"PRIu16, a->range.proto.min);
5217 if (a->range.proto.max
5218 && a->range.proto.max != a->range.proto.min) {
5219 ds_put_format(ds, "-%"PRIu16, a->range.proto.max);
5222 ds_put_char(ds, ',');
5224 if (a->flags & NX_NAT_F_PERSISTENT) {
5225 ds_put_cstr(ds, "persistent,");
5227 if (a->flags & NX_NAT_F_PROTO_HASH) {
5228 ds_put_cstr(ds, "hash,");
5230 if (a->flags & NX_NAT_F_PROTO_RANDOM) {
5231 ds_put_cstr(ds, "random,");
5235 ds_put_char(ds, ')');
5239 static char * OVS_WARN_UNUSED_RESULT
5240 str_to_nat_range(const char *s, struct ofpact_nat *on)
5242 char ipv6_s[IPV6_SCAN_LEN + 1];
5245 on->range_af = AF_UNSPEC;
5246 if (ovs_scan_len(s, &n, IP_SCAN_FMT,
5247 IP_SCAN_ARGS(&on->range.addr.ipv4.min))) {
5248 on->range_af = AF_INET;
5252 if (!ovs_scan_len(s, &n, IP_SCAN_FMT,
5253 IP_SCAN_ARGS(&on->range.addr.ipv4.max))
5254 || (ntohl(on->range.addr.ipv4.max)
5255 < ntohl(on->range.addr.ipv4.min))) {
5259 } else if ((ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5260 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5261 && inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.min) == 1) {
5262 on->range_af = AF_INET6;
5266 if (!(ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5267 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5268 || inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.max) != 1
5269 || memcmp(&on->range.addr.ipv6.max, &on->range.addr.ipv6.min,
5270 sizeof on->range.addr.ipv6.max) < 0) {
5275 if (on->range_af != AF_UNSPEC && s[n] == ':') {
5277 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.min)) {
5282 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.max)
5283 || on->range.proto.max < on->range.proto.min) {
5288 if (strlen(s) != n) {
5289 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
5294 return xasprintf("invalid nat range \"%s\"", s);
5298 /* Parses 'arg' as the argument to a "nat" action, and appends such an
5299 * action to 'ofpacts'.
5301 * Returns NULL if successful, otherwise a malloc()'d string describing the
5302 * error. The caller is responsible for freeing the returned string. */
5303 static char * OVS_WARN_UNUSED_RESULT
5304 parse_NAT(char *arg, struct ofpbuf *ofpacts,
5305 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5307 struct ofpact_nat *on = ofpact_put_NAT(ofpacts);
5311 on->range_af = AF_UNSPEC;
5313 while (ofputil_parse_key_value(&arg, &key, &value)) {
5316 if (!strcmp(key, "src")) {
5317 on->flags |= NX_NAT_F_SRC;
5318 error = str_to_nat_range(value, on);
5319 } else if (!strcmp(key, "dst")) {
5320 on->flags |= NX_NAT_F_DST;
5321 error = str_to_nat_range(value, on);
5322 } else if (!strcmp(key, "persistent")) {
5323 on->flags |= NX_NAT_F_PERSISTENT;
5324 } else if (!strcmp(key, "hash")) {
5325 on->flags |= NX_NAT_F_PROTO_HASH;
5326 } else if (!strcmp(key, "random")) {
5327 on->flags |= NX_NAT_F_PROTO_RANDOM;
5329 error = xasprintf("invalid key \"%s\" in \"nat\" argument",
5336 if (on->flags & NX_NAT_F_SRC && on->flags & NX_NAT_F_DST) {
5337 return xasprintf("May only specify one of \"snat\" or \"dnat\".");
5339 if (!(on->flags & NX_NAT_F_SRC || on->flags & NX_NAT_F_DST)) {
5341 return xasprintf("Flags allowed only with \"snat\" or \"dnat\".");
5343 if (on->range_af != AF_UNSPEC) {
5344 return xasprintf("Range allowed only with \"snat\" or \"dnat\".");
5351 /* Meter instruction. */
5354 encode_METER(const struct ofpact_meter *meter,
5355 enum ofp_version ofp_version, struct ofpbuf *out)
5357 if (ofp_version >= OFP13_VERSION) {
5358 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
5362 static char * OVS_WARN_UNUSED_RESULT
5363 parse_METER(char *arg, struct ofpbuf *ofpacts,
5364 enum ofputil_protocol *usable_protocols)
5366 *usable_protocols &= OFPUTIL_P_OF13_UP;
5367 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
5371 format_METER(const struct ofpact_meter *a, struct ds *s)
5373 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
5376 /* Clear-Actions instruction. */
5379 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
5380 enum ofp_version ofp_version OVS_UNUSED,
5381 struct ofpbuf *out OVS_UNUSED)
5383 if (ofp_version > OFP10_VERSION) {
5384 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
5388 static char * OVS_WARN_UNUSED_RESULT
5389 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
5390 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5392 ofpact_put_CLEAR_ACTIONS(ofpacts);
5397 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
5399 ds_put_cstr(s, "clear_actions");
5402 /* Write-Actions instruction. */
5405 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
5406 enum ofp_version ofp_version, struct ofpbuf *out)
5408 if (ofp_version > OFP10_VERSION) {
5409 const size_t ofs = out->size;
5411 instruction_put_OFPIT11_WRITE_ACTIONS(out);
5412 ofpacts_put_openflow_actions(actions->actions,
5413 ofpact_nest_get_action_len(actions),
5415 ofpacts_update_instruction_actions(out, ofs);
5419 static char * OVS_WARN_UNUSED_RESULT
5420 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
5421 enum ofputil_protocol *usable_protocols)
5423 size_t ofs = ofpacts_pull(ofpacts);
5424 struct ofpact_nest *on;
5427 /* Add a Write-Actions instruction and then pull it off. */
5428 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
5429 ofpbuf_pull(ofpacts, sizeof *on);
5431 /* Parse nested actions.
5433 * We pulled off "write-actions" and the previous actions because the
5434 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
5435 * that it doesn't actually include the nested actions. That means that
5436 * ofpacts_parse() would reject them as being part of an Apply-Actions that
5437 * follows a Write-Actions, which is an invalid order. */
5438 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
5439 OFPACT_WRITE_ACTIONS);
5441 /* Put the Write-Actions back on and update its length. */
5442 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
5443 on->ofpact.len = ofpacts->size;
5445 /* Put any previous actions or instructions back on. */
5446 ofpbuf_push_uninit(ofpacts, ofs);
5452 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
5454 ds_put_cstr(s, "write_actions(");
5455 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
5456 ds_put_char(s, ')');
5459 /* Action structure for NXAST_WRITE_METADATA.
5461 * Modifies the 'mask' bits of the metadata value. */
5462 struct nx_action_write_metadata {
5463 ovs_be16 type; /* OFPAT_VENDOR. */
5464 ovs_be16 len; /* Length is 32. */
5465 ovs_be32 vendor; /* NX_VENDOR_ID. */
5466 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
5467 uint8_t zeros[6]; /* Must be zero. */
5468 ovs_be64 metadata; /* Metadata register. */
5469 ovs_be64 mask; /* Metadata mask. */
5471 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
5474 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
5475 enum ofp_version ofp_version OVS_UNUSED,
5478 struct ofpact_metadata *om;
5480 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
5481 return OFPERR_NXBRC_MUST_BE_ZERO;
5484 om = ofpact_put_WRITE_METADATA(out);
5485 om->metadata = nawm->metadata;
5486 om->mask = nawm->mask;
5492 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
5493 enum ofp_version ofp_version, struct ofpbuf *out)
5495 if (ofp_version == OFP10_VERSION) {
5496 struct nx_action_write_metadata *nawm;
5498 nawm = put_NXAST_WRITE_METADATA(out);
5499 nawm->metadata = metadata->metadata;
5500 nawm->mask = metadata->mask;
5502 struct ofp11_instruction_write_metadata *oiwm;
5504 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
5505 oiwm->metadata = metadata->metadata;
5506 oiwm->metadata_mask = metadata->mask;
5510 static char * OVS_WARN_UNUSED_RESULT
5511 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
5512 enum ofputil_protocol *usable_protocols)
5514 struct ofpact_metadata *om;
5515 char *mask = strchr(arg, '/');
5517 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
5519 om = ofpact_put_WRITE_METADATA(ofpacts);
5524 error = str_to_be64(mask + 1, &om->mask);
5529 om->mask = OVS_BE64_MAX;
5532 return str_to_be64(arg, &om->metadata);
5536 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5538 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
5539 if (a->mask != OVS_BE64_MAX) {
5540 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5544 /* Goto-Table instruction. */
5547 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5548 enum ofp_version ofp_version, struct ofpbuf *out)
5550 if (ofp_version == OFP10_VERSION) {
5551 struct nx_action_resubmit *nar;
5553 nar = put_NXAST_RESUBMIT_TABLE(out);
5554 nar->table = goto_table->table_id;
5555 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5557 struct ofp11_instruction_goto_table *oigt;
5559 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5560 oigt->table_id = goto_table->table_id;
5561 memset(oigt->pad, 0, sizeof oigt->pad);
5565 static char * OVS_WARN_UNUSED_RESULT
5566 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5567 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5569 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5570 char *table_s = strsep(&arg, ",");
5571 if (!table_s || !table_s[0]) {
5572 return xstrdup("instruction goto-table needs table id");
5574 return str_to_u8(table_s, "table", &ogt->table_id);
5578 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5580 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
5584 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
5585 const struct ofp_action_header *bad_action, enum ofperr error)
5587 if (!VLOG_DROP_WARN(&rl)) {
5591 ds_put_hex_dump(&s, actions, actions_len, 0, false);
5592 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5593 (char *)bad_action - (char *)actions,
5594 ofperr_get_name(error), ds_cstr(&s));
5600 ofpacts_decode(const void *actions, size_t actions_len,
5601 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5603 struct ofpbuf openflow;
5605 ofpbuf_use_const(&openflow, actions, actions_len);
5606 while (openflow.size) {
5607 const struct ofp_action_header *action = openflow.data;
5608 enum ofp_raw_action_type raw;
5612 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5614 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
5618 log_bad_action(actions, actions_len, action, error);
5626 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5627 unsigned int actions_len,
5628 enum ofp_version version,
5629 uint32_t allowed_ovsinsts,
5630 struct ofpbuf *ofpacts,
5631 enum ofpact_type outer_action)
5633 const struct ofp_action_header *actions;
5634 size_t orig_size = ofpacts->size;
5637 if (actions_len % OFP_ACTION_ALIGN != 0) {
5638 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5639 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5640 return OFPERR_OFPBRC_BAD_LEN;
5643 actions = ofpbuf_try_pull(openflow, actions_len);
5644 if (actions == NULL) {
5645 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
5646 "remaining message length (%"PRIu32")",
5647 actions_len, openflow->size);
5648 return OFPERR_OFPBRC_BAD_LEN;
5651 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5653 ofpacts->size = orig_size;
5657 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
5660 ofpacts->size = orig_size;
5665 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
5666 * of 'openflow' into ofpacts. On success, appends the converted actions to
5667 * 'ofpacts'; on failure, 'ofpacts' is unchanged (but might be reallocated) .
5668 * Returns 0 if successful, otherwise an OpenFlow error.
5670 * Actions are processed according to their OpenFlow version which
5671 * is provided in the 'version' parameter.
5673 * In most places in OpenFlow, actions appear encapsulated in instructions, so
5674 * you should call ofpacts_pull_openflow_instructions() instead of this
5677 * The parsed actions are valid generically, but they may not be valid in a
5678 * specific context. For example, port numbers up to OFPP_MAX are valid
5679 * generically, but specific datapaths may only support port numbers in a
5680 * smaller range. Use ofpacts_check() to additional check whether actions are
5681 * valid in a specific context. */
5683 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
5684 unsigned int actions_len,
5685 enum ofp_version version,
5686 struct ofpbuf *ofpacts)
5688 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
5689 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
5693 /* OpenFlow 1.1 actions. */
5696 /* True if an action sets the value of a field
5697 * in a way that is compatibile with the action set.
5698 * The field can be set via either a set or a move action.
5699 * False otherwise. */
5701 ofpact_is_set_or_move_action(const struct ofpact *a)
5704 case OFPACT_SET_FIELD:
5705 case OFPACT_REG_MOVE:
5706 case OFPACT_SET_ETH_DST:
5707 case OFPACT_SET_ETH_SRC:
5708 case OFPACT_SET_IP_DSCP:
5709 case OFPACT_SET_IP_ECN:
5710 case OFPACT_SET_IP_TTL:
5711 case OFPACT_SET_IPV4_DST:
5712 case OFPACT_SET_IPV4_SRC:
5713 case OFPACT_SET_L4_DST_PORT:
5714 case OFPACT_SET_L4_SRC_PORT:
5715 case OFPACT_SET_MPLS_LABEL:
5716 case OFPACT_SET_MPLS_TC:
5717 case OFPACT_SET_MPLS_TTL:
5718 case OFPACT_SET_QUEUE:
5719 case OFPACT_SET_TUNNEL:
5720 case OFPACT_SET_VLAN_PCP:
5721 case OFPACT_SET_VLAN_VID:
5724 case OFPACT_CLEAR_ACTIONS:
5727 case OFPACT_CONTROLLER:
5728 case OFPACT_DEC_MPLS_TTL:
5729 case OFPACT_DEC_TTL:
5730 case OFPACT_ENQUEUE:
5732 case OFPACT_UNROLL_XLATE:
5733 case OFPACT_FIN_TIMEOUT:
5734 case OFPACT_GOTO_TABLE:
5737 case OFPACT_CONJUNCTION:
5739 case OFPACT_MULTIPATH:
5742 case OFPACT_OUTPUT_REG:
5743 case OFPACT_POP_MPLS:
5744 case OFPACT_POP_QUEUE:
5745 case OFPACT_PUSH_MPLS:
5746 case OFPACT_PUSH_VLAN:
5747 case OFPACT_RESUBMIT:
5749 case OFPACT_STACK_POP:
5750 case OFPACT_STACK_PUSH:
5751 case OFPACT_STRIP_VLAN:
5752 case OFPACT_WRITE_ACTIONS:
5753 case OFPACT_WRITE_METADATA:
5754 case OFPACT_DEBUG_RECIRC:
5761 /* True if an action is allowed in the action set.
5762 * False otherwise. */
5764 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
5767 case OFPACT_DEC_MPLS_TTL:
5768 case OFPACT_DEC_TTL:
5771 case OFPACT_POP_MPLS:
5772 case OFPACT_PUSH_MPLS:
5773 case OFPACT_PUSH_VLAN:
5774 case OFPACT_REG_MOVE:
5775 case OFPACT_SET_FIELD:
5776 case OFPACT_SET_ETH_DST:
5777 case OFPACT_SET_ETH_SRC:
5778 case OFPACT_SET_IP_DSCP:
5779 case OFPACT_SET_IP_ECN:
5780 case OFPACT_SET_IP_TTL:
5781 case OFPACT_SET_IPV4_DST:
5782 case OFPACT_SET_IPV4_SRC:
5783 case OFPACT_SET_L4_DST_PORT:
5784 case OFPACT_SET_L4_SRC_PORT:
5785 case OFPACT_SET_MPLS_LABEL:
5786 case OFPACT_SET_MPLS_TC:
5787 case OFPACT_SET_MPLS_TTL:
5788 case OFPACT_SET_QUEUE:
5789 case OFPACT_SET_TUNNEL:
5790 case OFPACT_SET_VLAN_PCP:
5791 case OFPACT_SET_VLAN_VID:
5792 case OFPACT_STRIP_VLAN:
5795 /* In general these actions are excluded because they are not part of
5796 * the OpenFlow specification nor map to actions that are defined in
5797 * the specification. Thus the order in which they should be applied
5798 * in the action set is undefined. */
5800 case OFPACT_CONTROLLER:
5803 case OFPACT_ENQUEUE:
5805 case OFPACT_UNROLL_XLATE:
5806 case OFPACT_FIN_TIMEOUT:
5808 case OFPACT_CONJUNCTION:
5809 case OFPACT_MULTIPATH:
5811 case OFPACT_OUTPUT_REG:
5812 case OFPACT_POP_QUEUE:
5813 case OFPACT_RESUBMIT:
5815 case OFPACT_STACK_POP:
5816 case OFPACT_STACK_PUSH:
5817 case OFPACT_DEBUG_RECIRC:
5819 /* The action set may only include actions and thus
5820 * may not include any instructions */
5821 case OFPACT_CLEAR_ACTIONS:
5822 case OFPACT_GOTO_TABLE:
5824 case OFPACT_WRITE_ACTIONS:
5825 case OFPACT_WRITE_METADATA:
5832 /* Append ofpact 'a' onto the tail of 'out' */
5834 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
5836 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
5839 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
5841 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
5842 enum ofpact_type filter)
5844 const struct ofpact *target;
5845 const struct ofpact *a;
5848 OFPACT_FOR_EACH (a, in->data, in->size) {
5849 if (a->type == filter) {
5854 ofpact_copy(out, target);
5856 return target != NULL;
5859 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5860 * The order of appended ofpacts is preserved between 'in' and 'out' */
5862 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5863 bool (*filter)(const struct ofpact *))
5865 const struct ofpact *a;
5867 OFPACT_FOR_EACH (a, in->data, in->size) {
5869 ofpact_copy(out, a);
5874 /* Reads 'action_set', which contains ofpacts accumulated by
5875 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5876 * executed directly into 'action_list'. (These names correspond to the
5877 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5879 * In general this involves appending the last instance of each action that is
5880 * admissible in the action set in the order described in the OpenFlow
5884 * + output action is only appended if no group action was present in 'in'.
5885 * + As a simplification all set actions are copied in the order the are
5886 * provided in 'in' as many set actions applied to a field has the same
5887 * affect as only applying the last action that sets a field and
5888 * duplicates are removed by do_xlate_actions().
5889 * This has an unwanted side-effect of compsoting multiple
5890 * LOAD_REG actions that touch different regions of the same field. */
5892 ofpacts_execute_action_set(struct ofpbuf *action_list,
5893 const struct ofpbuf *action_set)
5895 /* The OpenFlow spec "Action Set" section specifies this order. */
5896 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5897 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5898 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5899 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5900 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5901 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5902 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5903 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5905 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5906 * we should execute only OFPACT_GROUP.
5908 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5909 * all the actions because there's no point in modifying a packet that will
5910 * not be sent anywhere. */
5911 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5912 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5913 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5914 ofpbuf_clear(action_list);
5920 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5921 size_t n_in, enum ofp_version version,
5926 size_t start = out->size;
5928 error = ofpacts_decode(in, n_in, version, out);
5934 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5935 if (!ofpact_is_allowed_in_actions_set(a)) {
5936 VLOG_WARN_RL(&rl, "disallowed action in action set");
5937 return OFPERR_OFPBAC_BAD_TYPE;
5944 /* OpenFlow 1.1 instructions. */
5946 struct instruction_type_info {
5947 enum ovs_instruction_type type;
5951 static const struct instruction_type_info inst_info[] = {
5952 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5958 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5960 return inst_info[type].name;
5964 ovs_instruction_type_from_name(const char *name)
5966 const struct instruction_type_info *p;
5967 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5968 if (!strcasecmp(name, p->name)) {
5975 enum ovs_instruction_type
5976 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5980 return OVSINST_OFPIT13_METER;
5981 case OFPACT_CLEAR_ACTIONS:
5982 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5983 case OFPACT_WRITE_ACTIONS:
5984 return OVSINST_OFPIT11_WRITE_ACTIONS;
5985 case OFPACT_WRITE_METADATA:
5986 return OVSINST_OFPIT11_WRITE_METADATA;
5987 case OFPACT_GOTO_TABLE:
5988 return OVSINST_OFPIT11_GOTO_TABLE;
5991 case OFPACT_CONTROLLER:
5992 case OFPACT_ENQUEUE:
5993 case OFPACT_OUTPUT_REG:
5995 case OFPACT_SET_VLAN_VID:
5996 case OFPACT_SET_VLAN_PCP:
5997 case OFPACT_STRIP_VLAN:
5998 case OFPACT_PUSH_VLAN:
5999 case OFPACT_SET_ETH_SRC:
6000 case OFPACT_SET_ETH_DST:
6001 case OFPACT_SET_IPV4_SRC:
6002 case OFPACT_SET_IPV4_DST:
6003 case OFPACT_SET_IP_DSCP:
6004 case OFPACT_SET_IP_ECN:
6005 case OFPACT_SET_IP_TTL:
6006 case OFPACT_SET_L4_SRC_PORT:
6007 case OFPACT_SET_L4_DST_PORT:
6008 case OFPACT_REG_MOVE:
6009 case OFPACT_SET_FIELD:
6010 case OFPACT_STACK_PUSH:
6011 case OFPACT_STACK_POP:
6012 case OFPACT_DEC_TTL:
6013 case OFPACT_SET_MPLS_LABEL:
6014 case OFPACT_SET_MPLS_TC:
6015 case OFPACT_SET_MPLS_TTL:
6016 case OFPACT_DEC_MPLS_TTL:
6017 case OFPACT_PUSH_MPLS:
6018 case OFPACT_POP_MPLS:
6019 case OFPACT_SET_TUNNEL:
6020 case OFPACT_SET_QUEUE:
6021 case OFPACT_POP_QUEUE:
6022 case OFPACT_FIN_TIMEOUT:
6023 case OFPACT_RESUBMIT:
6025 case OFPACT_CONJUNCTION:
6026 case OFPACT_MULTIPATH:
6029 case OFPACT_UNROLL_XLATE:
6031 case OFPACT_DEBUG_RECIRC:
6035 return OVSINST_OFPIT11_APPLY_ACTIONS;
6040 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
6041 const uint16_t inst_type)
6043 switch (inst_type) {
6045 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6047 *instruction_type = OVSINST_##ENUM; \
6053 return OFPERR_OFPBIC_UNKNOWN_INST;
6057 /* Two-way translation between OVS's internal "OVSINST_*" representation of
6058 * instructions and the "OFPIT_*" representation used in OpenFlow. */
6059 struct ovsinst_map {
6060 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
6061 int ofpit; /* OFPIT_* number from OpenFlow spec. */
6064 static const struct ovsinst_map *
6065 get_ovsinst_map(enum ofp_version version)
6067 /* OpenFlow 1.1 and 1.2 instructions. */
6068 static const struct ovsinst_map of11[] = {
6069 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6070 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6071 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6072 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6073 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6077 /* OpenFlow 1.3+ instructions. */
6078 static const struct ovsinst_map of13[] = {
6079 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6080 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6081 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6082 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6083 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6084 { OVSINST_OFPIT13_METER, 6 },
6088 return version < OFP13_VERSION ? of11 : of13;
6091 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
6092 * values, into a bitmap of instructions suitable for OpenFlow 'version'
6093 * (OFP11_VERSION or later), and returns the result. */
6095 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
6097 uint32_t ofpit_bitmap = 0;
6098 const struct ovsinst_map *x;
6100 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6101 if (ovsinst_bitmap & (1u << x->ovsinst)) {
6102 ofpit_bitmap |= 1u << x->ofpit;
6105 return htonl(ofpit_bitmap);
6108 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
6109 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
6110 * correspond to OVSINST_* values, and returns the result. */
6112 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
6114 uint32_t ovsinst_bitmap = 0;
6115 const struct ovsinst_map *x;
6117 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6118 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
6119 ovsinst_bitmap |= 1u << x->ovsinst;
6122 return ovsinst_bitmap;
6125 static inline struct ofp11_instruction *
6126 instruction_next(const struct ofp11_instruction *inst)
6128 return ((struct ofp11_instruction *) (void *)
6129 ((uint8_t *) inst + ntohs(inst->len)));
6133 instruction_is_valid(const struct ofp11_instruction *inst,
6134 size_t n_instructions)
6136 uint16_t len = ntohs(inst->len);
6137 return (!(len % OFP11_INSTRUCTION_ALIGN)
6138 && len >= sizeof *inst
6139 && len / sizeof *inst <= n_instructions);
6142 /* This macro is careful to check for instructions with bad lengths. */
6143 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
6144 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
6145 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
6146 ((LEFT) -= (ntohs((ITER)->len) \
6147 / sizeof(struct ofp11_instruction)), \
6148 (ITER) = instruction_next(ITER)))
6151 decode_openflow11_instruction(const struct ofp11_instruction *inst,
6152 enum ovs_instruction_type *type)
6154 uint16_t len = ntohs(inst->len);
6156 switch (inst->type) {
6157 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
6158 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
6160 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6161 case CONSTANT_HTONS(ENUM): \
6163 ? len >= sizeof(struct STRUCT) \
6164 : len == sizeof(struct STRUCT)) { \
6165 *type = OVSINST_##ENUM; \
6168 return OFPERR_OFPBIC_BAD_LEN; \
6174 return OFPERR_OFPBIC_UNKNOWN_INST;
6179 decode_openflow11_instructions(const struct ofp11_instruction insts[],
6181 const struct ofp11_instruction *out[])
6183 const struct ofp11_instruction *inst;
6186 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
6187 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
6188 enum ovs_instruction_type type;
6191 error = decode_openflow11_instruction(inst, &type);
6197 return OFPERR_OFPBIC_DUP_INST;
6203 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
6204 (n_insts - left) * sizeof *inst);
6205 return OFPERR_OFPBIC_BAD_LEN;
6211 get_actions_from_instruction(const struct ofp11_instruction *inst,
6212 const struct ofp_action_header **actions,
6213 size_t *actions_len)
6215 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
6216 *actions_len = ntohs(inst->len) - sizeof *inst;
6220 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
6221 unsigned int instructions_len,
6222 enum ofp_version version,
6223 struct ofpbuf *ofpacts)
6225 const struct ofp11_instruction *instructions;
6226 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
6229 ofpbuf_clear(ofpacts);
6230 if (version == OFP10_VERSION) {
6231 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
6233 (1u << N_OVS_INSTRUCTIONS) - 1,
6237 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
6238 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
6240 instructions_len, OFP11_INSTRUCTION_ALIGN);
6241 error = OFPERR_OFPBIC_BAD_LEN;
6245 instructions = ofpbuf_try_pull(openflow, instructions_len);
6246 if (instructions == NULL) {
6247 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
6248 "remaining message length (%"PRIu32")",
6249 instructions_len, openflow->size);
6250 error = OFPERR_OFPBIC_BAD_LEN;
6254 error = decode_openflow11_instructions(
6255 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
6261 if (insts[OVSINST_OFPIT13_METER]) {
6262 const struct ofp13_instruction_meter *oim;
6263 struct ofpact_meter *om;
6265 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
6266 insts[OVSINST_OFPIT13_METER]);
6268 om = ofpact_put_METER(ofpacts);
6269 om->meter_id = ntohl(oim->meter_id);
6271 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
6272 const struct ofp_action_header *actions;
6275 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
6276 &actions, &actions_len);
6277 error = ofpacts_decode(actions, actions_len, version, ofpacts);
6282 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
6283 instruction_get_OFPIT11_CLEAR_ACTIONS(
6284 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
6285 ofpact_put_CLEAR_ACTIONS(ofpacts);
6287 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
6288 struct ofpact_nest *on;
6289 const struct ofp_action_header *actions;
6291 size_t start = ofpacts->size;
6292 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
6293 offsetof(struct ofpact_nest, actions));
6294 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
6295 &actions, &actions_len);
6296 error = ofpacts_decode_for_action_set(actions, actions_len,
6301 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
6302 on->ofpact.len = ofpacts->size - start;
6304 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
6305 const struct ofp11_instruction_write_metadata *oiwm;
6306 struct ofpact_metadata *om;
6308 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
6309 insts[OVSINST_OFPIT11_WRITE_METADATA]);
6311 om = ofpact_put_WRITE_METADATA(ofpacts);
6312 om->metadata = oiwm->metadata;
6313 om->mask = oiwm->metadata_mask;
6315 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
6316 const struct ofp11_instruction_goto_table *oigt;
6317 struct ofpact_goto_table *ogt;
6319 oigt = instruction_get_OFPIT11_GOTO_TABLE(
6320 insts[OVSINST_OFPIT11_GOTO_TABLE]);
6321 ogt = ofpact_put_GOTO_TABLE(ofpacts);
6322 ogt->table_id = oigt->table_id;
6325 error = ofpacts_verify(ofpacts->data, ofpacts->size,
6326 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
6329 ofpbuf_clear(ofpacts);
6334 /* Update the length of the instruction that begins at offset 'ofs' within
6335 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
6336 * If the instruction contains no nested actions, deletes it entirely. */
6338 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
6340 struct ofp11_instruction_actions *oia;
6342 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
6343 if (openflow->size > ofs + sizeof *oia) {
6344 oia->len = htons(openflow->size - ofs);
6346 openflow->size = ofs;
6350 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
6351 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
6352 * valid, otherwise an OpenFlow error code. */
6354 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
6362 case OFPP_CONTROLLER:
6367 return OFPERR_OFPBAC_BAD_OUT_PORT;
6370 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
6373 return OFPERR_OFPBAC_BAD_OUT_PORT;
6377 /* Removes the protocols that require consistency between match and actions
6378 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
6380 * (An example of an inconsistency between match and actions is a flow that
6381 * does not match on an MPLS Ethertype but has an action that pops an MPLS
6384 inconsistent_match(enum ofputil_protocol *usable_protocols)
6386 *usable_protocols &= OFPUTIL_P_OF10_ANY;
6389 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
6390 * caller must restore them.
6392 * Modifies some actions, filling in fields that could not be properly set
6393 * without context. */
6395 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
6396 struct flow *flow, ofp_port_t max_ports,
6397 uint8_t table_id, uint8_t n_tables)
6399 const struct ofpact_enqueue *enqueue;
6400 const struct mf_field *mf;
6404 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
6407 case OFPACT_CONTROLLER:
6410 case OFPACT_ENQUEUE:
6411 enqueue = ofpact_get_ENQUEUE(a);
6412 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
6413 && enqueue->port != OFPP_IN_PORT
6414 && enqueue->port != OFPP_LOCAL) {
6415 return OFPERR_OFPBAC_BAD_OUT_PORT;
6419 case OFPACT_OUTPUT_REG:
6420 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
6423 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
6425 case OFPACT_SET_VLAN_VID:
6426 /* Remember if we saw a vlan tag in the flow to aid translating to
6427 * OpenFlow 1.1+ if need be. */
6428 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
6429 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6430 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6431 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
6432 inconsistent_match(usable_protocols);
6434 /* Temporary mark that we have a vlan tag. */
6435 flow->vlan_tci |= htons(VLAN_CFI);
6438 case OFPACT_SET_VLAN_PCP:
6439 /* Remember if we saw a vlan tag in the flow to aid translating to
6440 * OpenFlow 1.1+ if need be. */
6441 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
6442 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6443 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6444 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
6445 inconsistent_match(usable_protocols);
6447 /* Temporary mark that we have a vlan tag. */
6448 flow->vlan_tci |= htons(VLAN_CFI);
6451 case OFPACT_STRIP_VLAN:
6452 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
6453 inconsistent_match(usable_protocols);
6455 /* Temporary mark that we have no vlan tag. */
6456 flow->vlan_tci = htons(0);
6459 case OFPACT_PUSH_VLAN:
6460 if (flow->vlan_tci & htons(VLAN_CFI)) {
6461 /* Multiple VLAN headers not supported. */
6462 return OFPERR_OFPBAC_BAD_TAG;
6464 /* Temporary mark that we have a vlan tag. */
6465 flow->vlan_tci |= htons(VLAN_CFI);
6468 case OFPACT_SET_ETH_SRC:
6469 case OFPACT_SET_ETH_DST:
6472 case OFPACT_SET_IPV4_SRC:
6473 case OFPACT_SET_IPV4_DST:
6474 if (flow->dl_type != htons(ETH_TYPE_IP)) {
6475 inconsistent_match(usable_protocols);
6479 case OFPACT_SET_IP_DSCP:
6480 case OFPACT_SET_IP_ECN:
6481 case OFPACT_SET_IP_TTL:
6482 case OFPACT_DEC_TTL:
6483 if (!is_ip_any(flow)) {
6484 inconsistent_match(usable_protocols);
6488 case OFPACT_SET_L4_SRC_PORT:
6489 case OFPACT_SET_L4_DST_PORT:
6490 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
6491 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
6492 && flow->nw_proto != IPPROTO_SCTP)) {
6493 inconsistent_match(usable_protocols);
6495 /* Note on which transport protocol the port numbers are set.
6496 * This allows this set action to be converted to an OF1.2 set field
6498 if (a->type == OFPACT_SET_L4_SRC_PORT) {
6499 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
6501 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
6505 case OFPACT_REG_MOVE:
6506 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
6508 case OFPACT_SET_FIELD:
6509 mf = ofpact_get_SET_FIELD(a)->field;
6510 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6511 if (!mf_are_prereqs_ok(mf, flow) ||
6512 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
6513 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
6515 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6517 /* Remember if we saw a vlan tag in the flow to aid translating to
6518 * OpenFlow 1.1 if need be. */
6519 ofpact_get_SET_FIELD(a)->flow_has_vlan =
6520 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6521 if (mf->id == MFF_VLAN_TCI) {
6522 /* The set field may add or remove the vlan tag,
6523 * Mark the status temporarily. */
6524 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
6528 case OFPACT_STACK_PUSH:
6529 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
6531 case OFPACT_STACK_POP:
6532 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6534 case OFPACT_SET_MPLS_LABEL:
6535 case OFPACT_SET_MPLS_TC:
6536 case OFPACT_SET_MPLS_TTL:
6537 case OFPACT_DEC_MPLS_TTL:
6538 if (!eth_type_mpls(flow->dl_type)) {
6539 inconsistent_match(usable_protocols);
6543 case OFPACT_SET_TUNNEL:
6544 case OFPACT_SET_QUEUE:
6545 case OFPACT_POP_QUEUE:
6546 case OFPACT_RESUBMIT:
6549 case OFPACT_FIN_TIMEOUT:
6550 if (flow->nw_proto != IPPROTO_TCP) {
6551 inconsistent_match(usable_protocols);
6556 return learn_check(ofpact_get_LEARN(a), flow);
6558 case OFPACT_CONJUNCTION:
6561 case OFPACT_MULTIPATH:
6562 return multipath_check(ofpact_get_MULTIPATH(a), flow);
6568 case OFPACT_PUSH_MPLS:
6569 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6570 /* The packet is now MPLS and the MPLS payload is opaque.
6571 * Thus nothing can be assumed about the network protocol.
6572 * Temporarily mark that we have no nw_proto. */
6576 case OFPACT_POP_MPLS:
6577 if (!eth_type_mpls(flow->dl_type)) {
6578 inconsistent_match(usable_protocols);
6580 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6587 struct ofpact_conntrack *oc = ofpact_get_CT(a);
6590 if (!dl_type_is_ip_any(flow->dl_type)
6591 || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6592 inconsistent_match(usable_protocols);
6595 if (oc->zone_src.field) {
6596 return mf_check_src(&oc->zone_src, flow);
6599 err = ofpacts_check(oc->actions, ofpact_ct_get_action_len(oc),
6600 flow, max_ports, table_id, n_tables,
6606 struct ofpact_nat *on = ofpact_get_NAT(a);
6608 if (!dl_type_is_ip_any(flow->dl_type) ||
6609 (on->range_af == AF_INET && flow->dl_type != htons(ETH_TYPE_IP)) ||
6610 (on->range_af == AF_INET6
6611 && flow->dl_type != htons(ETH_TYPE_IPV6))) {
6612 inconsistent_match(usable_protocols);
6617 case OFPACT_CLEAR_ACTIONS:
6620 case OFPACT_WRITE_ACTIONS: {
6621 /* Use a temporary copy of 'usable_protocols' because we can't check
6622 * consistency of an action set. */
6623 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6624 enum ofputil_protocol p = *usable_protocols;
6625 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6626 flow, max_ports, table_id, n_tables, &p);
6629 case OFPACT_WRITE_METADATA:
6632 case OFPACT_METER: {
6633 uint32_t mid = ofpact_get_METER(a)->meter_id;
6634 if (mid == 0 || mid > OFPM13_MAX) {
6635 return OFPERR_OFPMMFC_INVALID_METER;
6640 case OFPACT_GOTO_TABLE: {
6641 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
6642 if ((table_id != 255 && goto_table <= table_id)
6643 || (n_tables != 255 && goto_table >= n_tables)) {
6644 return OFPERR_OFPBIC_BAD_TABLE_ID;
6652 case OFPACT_UNROLL_XLATE:
6653 /* UNROLL is an internal action that should never be seen via
6655 return OFPERR_OFPBAC_BAD_TYPE;
6657 case OFPACT_DEBUG_RECIRC:
6665 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6666 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6667 * switch with no more than 'max_ports' ports.
6669 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6670 * '*usable_protocols' the protocols that forbid the inconsistency. (An
6671 * example of an inconsistency between match and actions is a flow that does
6672 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6674 * May annotate ofpacts with information gathered from the 'flow'.
6676 * May temporarily modify 'flow', but restores the changes before returning. */
6678 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
6679 struct flow *flow, ofp_port_t max_ports,
6680 uint8_t table_id, uint8_t n_tables,
6681 enum ofputil_protocol *usable_protocols)
6684 ovs_be16 dl_type = flow->dl_type;
6685 ovs_be16 vlan_tci = flow->vlan_tci;
6686 uint8_t nw_proto = flow->nw_proto;
6687 enum ofperr error = 0;
6689 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6690 error = ofpact_check__(usable_protocols, a, flow,
6691 max_ports, table_id, n_tables);
6696 /* Restore fields that may have been modified. */
6697 flow->dl_type = dl_type;
6698 flow->vlan_tci = vlan_tci;
6699 flow->nw_proto = nw_proto;
6703 /* Like ofpacts_check(), but reports inconsistencies as
6704 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6706 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
6707 struct flow *flow, ofp_port_t max_ports,
6708 uint8_t table_id, uint8_t n_tables,
6709 enum ofputil_protocol usable_protocols)
6711 enum ofputil_protocol p = usable_protocols;
6714 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
6715 table_id, n_tables, &p);
6716 return (error ? error
6717 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
6721 /* Returns the destination field that 'ofpact' would write to, or NULL
6722 * if the action would not write to an mf_field. */
6723 const struct mf_field *
6724 ofpact_get_mf_dst(const struct ofpact *ofpact)
6726 if (ofpact->type == OFPACT_SET_FIELD) {
6727 const struct ofpact_set_field *orl;
6729 orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
6731 } else if (ofpact->type == OFPACT_REG_MOVE) {
6732 const struct ofpact_reg_move *orm;
6734 orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
6735 return orm->dst.field;
6742 unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action)
6744 VLOG_WARN("%s action doesn't support nested action %s",
6745 ofpact_name(outer_action), ofpact_name(action));
6746 return OFPERR_OFPBAC_BAD_ARGUMENT;
6750 field_requires_ct(enum mf_field_id field)
6752 return field == MFF_CT_MARK || field == MFF_CT_LABEL;
6755 /* Apply nesting constraints for actions */
6757 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
6759 const struct mf_field *field = ofpact_get_mf_dst(a);
6761 if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
6762 VLOG_WARN("cannot set CT fields outside of ct action");
6763 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6765 if (a->type == OFPACT_NAT) {
6766 if (outer_action != OFPACT_CT) {
6767 VLOG_WARN("Cannot have NAT action outside of \"ct\" action");
6768 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6774 ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
6775 || outer_action == OFPACT_CT);
6777 if (outer_action == OFPACT_CT) {
6779 return unsupported_nesting(a->type, outer_action);
6780 } else if (!field_requires_ct(field->id)) {
6781 VLOG_WARN("%s action doesn't support nested modification "
6782 "of %s", ofpact_name(outer_action), field->name);
6783 return OFPERR_OFPBAC_BAD_ARGUMENT;
6791 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
6792 * appropriate order as defined by the OpenFlow spec and as required by Open
6795 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
6796 * instructions that are allowed within 'ofpacts[]'.
6798 * If 'outer_action' is not zero, it specifies that the actions are nested
6799 * within another action of type 'outer_action'. */
6801 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
6802 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
6804 const struct ofpact *a;
6805 enum ovs_instruction_type inst;
6807 inst = OVSINST_OFPIT13_METER;
6808 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6809 enum ovs_instruction_type next;
6812 if (a->type == OFPACT_CONJUNCTION) {
6813 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6814 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
6815 VLOG_WARN("\"conjunction\" actions may be used along with "
6816 "\"note\" but not any other kind of action "
6817 "(such as the \"%s\" action used here)",
6818 ofpact_name(a->type));
6819 return OFPERR_NXBAC_BAD_CONJUNCTION;
6825 error = ofpacts_verify_nested(a, outer_action);
6830 next = ovs_instruction_type_from_ofpact_type(a->type);
6832 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
6835 const char *name = ovs_instruction_name_from_type(inst);
6836 const char *next_name = ovs_instruction_name_from_type(next);
6839 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
6840 "1.1+ compatibility", name);
6842 VLOG_WARN("invalid instruction ordering: %s must appear "
6843 "before %s, for OpenFlow 1.1+ compatibility",
6846 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
6848 if (!((1u << next) & allowed_ovsinsts)) {
6849 const char *name = ovs_instruction_name_from_type(next);
6851 VLOG_WARN("%s instruction not allowed here", name);
6852 return OFPERR_OFPBIC_UNSUP_INST;
6861 /* Converting ofpacts to OpenFlow. */
6864 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
6868 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6869 case OFPACT_##ENUM: \
6870 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
6879 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
6880 * actions in 'openflow', appending the actions to any existing data in
6883 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
6884 struct ofpbuf *openflow,
6885 enum ofp_version ofp_version)
6887 const struct ofpact *a;
6888 size_t start_size = openflow->size;
6890 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6891 encode_ofpact(a, ofp_version, openflow);
6893 return openflow->size - start_size;
6896 static enum ovs_instruction_type
6897 ofpact_is_apply_actions(const struct ofpact *a)
6899 return (ovs_instruction_type_from_ofpact_type(a->type)
6900 == OVSINST_OFPIT11_APPLY_ACTIONS);
6904 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
6906 struct ofpbuf *openflow,
6907 enum ofp_version ofp_version)
6909 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
6910 const struct ofpact *a;
6912 if (ofp_version == OFP10_VERSION) {
6913 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
6920 if (ofpact_is_apply_actions(a)) {
6921 size_t ofs = openflow->size;
6923 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
6925 encode_ofpact(a, ofp_version, openflow);
6927 } while (a < end && ofpact_is_apply_actions(a));
6928 ofpacts_update_instruction_actions(openflow, ofs);
6930 encode_ofpact(a, ofp_version, openflow);
6936 /* Sets of supported actions. */
6938 /* Two-way translation between OVS's internal "OFPACT_*" representation of
6939 * actions and the "OFPAT_*" representation used in some OpenFlow version.
6940 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
6941 * instance is specific to one OpenFlow version.) */
6943 enum ofpact_type ofpact; /* Internal name for action type. */
6944 int ofpat; /* OFPAT_* number from OpenFlow spec. */
6947 static const struct ofpact_map *
6948 get_ofpact_map(enum ofp_version version)
6950 /* OpenFlow 1.0 actions. */
6951 static const struct ofpact_map of10[] = {
6952 { OFPACT_OUTPUT, 0 },
6953 { OFPACT_SET_VLAN_VID, 1 },
6954 { OFPACT_SET_VLAN_PCP, 2 },
6955 { OFPACT_STRIP_VLAN, 3 },
6956 { OFPACT_SET_ETH_SRC, 4 },
6957 { OFPACT_SET_ETH_DST, 5 },
6958 { OFPACT_SET_IPV4_SRC, 6 },
6959 { OFPACT_SET_IPV4_DST, 7 },
6960 { OFPACT_SET_IP_DSCP, 8 },
6961 { OFPACT_SET_L4_SRC_PORT, 9 },
6962 { OFPACT_SET_L4_DST_PORT, 10 },
6963 { OFPACT_ENQUEUE, 11 },
6967 /* OpenFlow 1.1 actions. */
6968 static const struct ofpact_map of11[] = {
6969 { OFPACT_OUTPUT, 0 },
6970 { OFPACT_SET_VLAN_VID, 1 },
6971 { OFPACT_SET_VLAN_PCP, 2 },
6972 { OFPACT_SET_ETH_SRC, 3 },
6973 { OFPACT_SET_ETH_DST, 4 },
6974 { OFPACT_SET_IPV4_SRC, 5 },
6975 { OFPACT_SET_IPV4_DST, 6 },
6976 { OFPACT_SET_IP_DSCP, 7 },
6977 { OFPACT_SET_IP_ECN, 8 },
6978 { OFPACT_SET_L4_SRC_PORT, 9 },
6979 { OFPACT_SET_L4_DST_PORT, 10 },
6980 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6981 /* OFPAT_COPY_TTL_IN (12) not supported. */
6982 { OFPACT_SET_MPLS_LABEL, 13 },
6983 { OFPACT_SET_MPLS_TC, 14 },
6984 { OFPACT_SET_MPLS_TTL, 15 },
6985 { OFPACT_DEC_MPLS_TTL, 16 },
6986 { OFPACT_PUSH_VLAN, 17 },
6987 { OFPACT_STRIP_VLAN, 18 },
6988 { OFPACT_PUSH_MPLS, 19 },
6989 { OFPACT_POP_MPLS, 20 },
6990 { OFPACT_SET_QUEUE, 21 },
6991 { OFPACT_GROUP, 22 },
6992 { OFPACT_SET_IP_TTL, 23 },
6993 { OFPACT_DEC_TTL, 24 },
6997 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6998 static const struct ofpact_map of12[] = {
6999 { OFPACT_OUTPUT, 0 },
7000 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7001 /* OFPAT_COPY_TTL_IN (12) not supported. */
7002 { OFPACT_SET_MPLS_TTL, 15 },
7003 { OFPACT_DEC_MPLS_TTL, 16 },
7004 { OFPACT_PUSH_VLAN, 17 },
7005 { OFPACT_STRIP_VLAN, 18 },
7006 { OFPACT_PUSH_MPLS, 19 },
7007 { OFPACT_POP_MPLS, 20 },
7008 { OFPACT_SET_QUEUE, 21 },
7009 { OFPACT_GROUP, 22 },
7010 { OFPACT_SET_IP_TTL, 23 },
7011 { OFPACT_DEC_TTL, 24 },
7012 { OFPACT_SET_FIELD, 25 },
7013 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
7014 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
7034 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
7035 * values, into a bitmap of actions suitable for OpenFlow 'version', and
7036 * returns the result. */
7038 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
7040 uint32_t openflow_bitmap = 0;
7041 const struct ofpact_map *x;
7043 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7044 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
7045 openflow_bitmap |= 1u << x->ofpat;
7048 return htonl(openflow_bitmap);
7051 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
7052 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
7053 * and returns the result. */
7055 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
7057 uint64_t ofpact_bitmap = 0;
7058 const struct ofpact_map *x;
7060 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7061 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
7062 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
7065 return ofpact_bitmap;
7068 /* Appends to 's' a string representation of the set of OFPACT_* represented
7069 * by 'ofpacts_bitmap'. */
7071 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
7073 if (!ofpacts_bitmap) {
7074 ds_put_cstr(s, "<none>");
7076 while (ofpacts_bitmap) {
7077 ds_put_format(s, "%s ",
7078 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
7079 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
7085 /* Returns true if 'action' outputs to 'port', false otherwise. */
7087 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
7089 switch (ofpact->type) {
7091 return ofpact_get_OUTPUT(ofpact)->port == port;
7092 case OFPACT_ENQUEUE:
7093 return ofpact_get_ENQUEUE(ofpact)->port == port;
7094 case OFPACT_CONTROLLER:
7095 return port == OFPP_CONTROLLER;
7097 case OFPACT_OUTPUT_REG:
7099 case OFPACT_SET_VLAN_VID:
7100 case OFPACT_SET_VLAN_PCP:
7101 case OFPACT_STRIP_VLAN:
7102 case OFPACT_PUSH_VLAN:
7103 case OFPACT_SET_ETH_SRC:
7104 case OFPACT_SET_ETH_DST:
7105 case OFPACT_SET_IPV4_SRC:
7106 case OFPACT_SET_IPV4_DST:
7107 case OFPACT_SET_IP_DSCP:
7108 case OFPACT_SET_IP_ECN:
7109 case OFPACT_SET_IP_TTL:
7110 case OFPACT_SET_L4_SRC_PORT:
7111 case OFPACT_SET_L4_DST_PORT:
7112 case OFPACT_REG_MOVE:
7113 case OFPACT_SET_FIELD:
7114 case OFPACT_STACK_PUSH:
7115 case OFPACT_STACK_POP:
7116 case OFPACT_DEC_TTL:
7117 case OFPACT_SET_MPLS_LABEL:
7118 case OFPACT_SET_MPLS_TC:
7119 case OFPACT_SET_MPLS_TTL:
7120 case OFPACT_DEC_MPLS_TTL:
7121 case OFPACT_SET_TUNNEL:
7122 case OFPACT_WRITE_METADATA:
7123 case OFPACT_SET_QUEUE:
7124 case OFPACT_POP_QUEUE:
7125 case OFPACT_FIN_TIMEOUT:
7126 case OFPACT_RESUBMIT:
7128 case OFPACT_CONJUNCTION:
7129 case OFPACT_MULTIPATH:
7132 case OFPACT_UNROLL_XLATE:
7133 case OFPACT_PUSH_MPLS:
7134 case OFPACT_POP_MPLS:
7136 case OFPACT_CLEAR_ACTIONS:
7137 case OFPACT_WRITE_ACTIONS:
7138 case OFPACT_GOTO_TABLE:
7141 case OFPACT_DEBUG_RECIRC:
7149 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7150 * to 'port', false otherwise. */
7152 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
7155 const struct ofpact *a;
7157 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
7158 if (ofpact_outputs_to_port(a, port)) {
7166 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7167 * to 'group', false otherwise. */
7169 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
7172 const struct ofpact *a;
7174 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
7175 if (a->type == OFPACT_GROUP
7176 && ofpact_get_GROUP(a)->group_id == group_id) {
7185 ofpacts_equal(const struct ofpact *a, size_t a_len,
7186 const struct ofpact *b, size_t b_len)
7188 return a_len == b_len && !memcmp(a, b, a_len);
7191 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
7192 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
7194 * This function relies on the order of 'ofpacts' being correct (as checked by
7195 * ofpacts_verify()). */
7197 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
7199 const struct ofpact *a;
7201 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7202 enum ovs_instruction_type inst;
7204 inst = ovs_instruction_type_from_ofpact_type(a->type);
7205 if (a->type == OFPACT_METER) {
7206 return ofpact_get_METER(a)->meter_id;
7207 } else if (inst > OVSINST_OFPIT13_METER) {
7215 /* Formatting ofpacts. */
7218 ofpact_format(const struct ofpact *a, struct ds *s)
7221 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7222 case OFPACT_##ENUM: \
7223 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
7232 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
7233 * 'ofpacts' to 'string'. */
7235 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
7239 ds_put_cstr(string, "drop");
7241 const struct ofpact *a;
7243 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7245 ds_put_cstr(string, ",");
7248 /* XXX write-actions */
7249 ofpact_format(a, string);
7254 /* Internal use by helpers. */
7257 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
7259 struct ofpact *ofpact;
7261 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
7262 ofpact = ofpacts->header;
7263 ofpact_init(ofpact, type, len);
7268 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
7270 memset(ofpact, 0, len);
7271 ofpact->type = type;
7276 /* Finishes composing a variable-length action (begun using
7277 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
7278 * bytes and updating its embedded length field. See the large comment near
7279 * the end of ofp-actions.h for more information. */
7281 ofpact_finish(struct ofpbuf *ofpacts, struct ofpact *ofpact)
7283 ovs_assert(ofpact == ofpacts->header);
7284 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
7285 ofpbuf_padto(ofpacts, OFPACT_ALIGN(ofpacts->size));
7288 static char * OVS_WARN_UNUSED_RESULT
7289 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
7290 enum ofputil_protocol *usable_protocols)
7293 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7294 case OFPACT_##ENUM: \
7295 return parse_##ENUM(value, ofpacts, usable_protocols);
7304 ofpact_type_from_name(const char *name, enum ofpact_type *type)
7306 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7307 if (!strcasecmp(name, NAME)) { \
7308 *type = OFPACT_##ENUM; \
7317 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
7319 * Returns NULL if successful, otherwise a malloc()'d string describing the
7320 * error. The caller is responsible for freeing the returned string.
7322 * If 'outer_action' is specified, indicates that the actions being parsed
7323 * are nested within another action of the type specified in 'outer_action'. */
7324 static char * OVS_WARN_UNUSED_RESULT
7325 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
7326 enum ofputil_protocol *usable_protocols,
7327 bool allow_instructions, enum ofpact_type outer_action)
7336 while (ofputil_parse_key_value(&pos, &key, &value)) {
7337 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
7338 enum ofpact_type type;
7342 if (ofpact_type_from_name(key, &type)) {
7343 error = ofpact_parse(type, value, ofpacts, usable_protocols);
7344 inst = ovs_instruction_type_from_ofpact_type(type);
7345 } else if (!strcasecmp(key, "mod_vlan_vid")) {
7346 error = parse_set_vlan_vid(value, ofpacts, true);
7347 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
7348 error = parse_set_vlan_pcp(value, ofpacts, true);
7349 } else if (!strcasecmp(key, "set_nw_ttl")) {
7350 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
7351 } else if (!strcasecmp(key, "pop_vlan")) {
7352 error = parse_pop_vlan(ofpacts);
7353 } else if (!strcasecmp(key, "set_tunnel64")) {
7354 error = parse_set_tunnel(value, ofpacts,
7355 NXAST_RAW_SET_TUNNEL64);
7356 } else if (!strcasecmp(key, "load")) {
7357 error = parse_reg_load(value, ofpacts);
7358 } else if (!strcasecmp(key, "bundle_load")) {
7359 error = parse_bundle_load(value, ofpacts);
7360 } else if (!strcasecmp(key, "drop")) {
7362 } else if (!strcasecmp(key, "apply_actions")) {
7363 return xstrdup("apply_actions is the default instruction");
7364 } else if (ofputil_port_from_string(key, &port)) {
7365 ofpact_put_OUTPUT(ofpacts)->port = port;
7367 return xasprintf("unknown action %s", key);
7373 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
7374 if (!allow_instructions) {
7375 return xasprintf("only actions are allowed here (not "
7377 ovs_instruction_name_from_type(inst));
7379 if (inst == prev_inst) {
7380 return xasprintf("instruction %s may be specified only once",
7381 ovs_instruction_name_from_type(inst));
7384 if (prev_inst != -1 && inst < prev_inst) {
7385 return xasprintf("instruction %s must be specified before %s",
7386 ovs_instruction_name_from_type(inst),
7387 ovs_instruction_name_from_type(prev_inst));
7392 if (drop && ofpacts->size) {
7393 return xstrdup("\"drop\" must not be accompanied by any other action "
7397 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
7399 ? (1u << N_OVS_INSTRUCTIONS) - 1
7400 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
7403 return xstrdup("Incorrect instruction ordering");
7409 static char * OVS_WARN_UNUSED_RESULT
7410 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
7411 enum ofputil_protocol *usable_protocols, bool allow_instructions,
7412 enum ofpact_type outer_action)
7414 uint32_t orig_size = ofpacts->size;
7415 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
7416 allow_instructions, outer_action);
7418 ofpacts->size = orig_size;
7423 static char * OVS_WARN_UNUSED_RESULT
7424 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
7425 enum ofputil_protocol *usable_protocols,
7426 bool allow_instructions, enum ofpact_type outer_action)
7430 *usable_protocols = OFPUTIL_P_ANY;
7433 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
7440 /* Parses 's' as a set of OpenFlow actions and appends the actions to
7441 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
7442 * that are nested within the action of type 'outer_action'.
7444 * Returns NULL if successful, otherwise a malloc()'d string describing the
7445 * error. The caller is responsible for freeing the returned string. */
7446 char * OVS_WARN_UNUSED_RESULT
7447 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
7448 enum ofputil_protocol *usable_protocols)
7450 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
7453 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
7456 * Returns NULL if successful, otherwise a malloc()'d string describing the
7457 * error. The caller is responsible for freeing the returned string. */
7458 char * OVS_WARN_UNUSED_RESULT
7459 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
7460 enum ofputil_protocol *usable_protocols)
7462 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
7466 ofpact_name(enum ofpact_type type)
7469 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7476 /* Low-level action decoding and encoding functions. */
7478 /* Everything needed to identify a particular OpenFlow action. */
7479 struct ofpact_hdrs {
7480 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
7481 uint16_t type; /* Type if standard, otherwise subtype. */
7482 uint8_t ofp_version; /* From ofp_header. */
7485 /* Information about a particular OpenFlow action. */
7486 struct ofpact_raw_instance {
7487 /* The action's identity. */
7488 struct ofpact_hdrs hdrs;
7489 enum ofp_raw_action_type raw;
7491 /* Looking up the action. */
7492 struct hmap_node decode_node; /* Based on 'hdrs'. */
7493 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
7495 /* The action's encoded size.
7497 * If this action is fixed-length, 'min_length' == 'max_length'.
7498 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7499 * OFP_ACTION_ALIGN) == 65528. */
7500 unsigned short int min_length;
7501 unsigned short int max_length;
7503 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7504 * offset of that argument from the beginning of the action and 'arg_len'
7505 * its length, both in bytes.
7507 * For actions that take other forms, these are both zero. */
7508 unsigned short int arg_ofs;
7509 unsigned short int arg_len;
7511 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7514 /* If this action is deprecated, a human-readable string with a brief
7516 const char *deprecation;
7519 /* Action header. */
7520 struct ofp_action_header {
7521 /* The meaning of other values of 'type' generally depends on the OpenFlow
7522 * version (see enum ofp_raw_action_type).
7524 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7525 * designates an OpenFlow vendor ID and that the remainder of the action
7526 * structure has a vendor-defined meaning.
7528 #define OFPAT_VENDOR 0xffff
7531 /* Always a multiple of 8. */
7534 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7535 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
7538 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
7540 /* Header for Nicira-defined actions and for ONF vendor extensions.
7542 * This cannot be used as an entirely generic vendor extension action header,
7543 * because OpenFlow does not specify the location or size of the action
7544 * subtype; it just happens that ONF extensions and Nicira extensions share
7546 struct ext_action_header {
7547 ovs_be16 type; /* OFPAT_VENDOR. */
7548 ovs_be16 len; /* At least 16. */
7549 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
7550 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
7553 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
7556 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
7557 const struct ofpact_hdrs *b)
7559 return (a->vendor == b->vendor
7560 && a->type == b->type
7561 && a->ofp_version == b->ofp_version);
7565 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
7567 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
7570 #include "ofp-actions.inc2"
7572 static struct hmap *
7573 ofpact_decode_hmap(void)
7575 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7576 static struct hmap hmap;
7578 if (ovsthread_once_start(&once)) {
7579 struct ofpact_raw_instance *inst;
7582 for (inst = all_raw_instances;
7583 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7585 hmap_insert(&hmap, &inst->decode_node,
7586 ofpact_hdrs_hash(&inst->hdrs));
7588 ovsthread_once_done(&once);
7593 static struct hmap *
7594 ofpact_encode_hmap(void)
7596 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7597 static struct hmap hmap;
7599 if (ovsthread_once_start(&once)) {
7600 struct ofpact_raw_instance *inst;
7603 for (inst = all_raw_instances;
7604 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7606 hmap_insert(&hmap, &inst->encode_node,
7607 hash_2words(inst->raw, inst->hdrs.ofp_version));
7609 ovsthread_once_done(&once);
7615 ofpact_decode_raw(enum ofp_version ofp_version,
7616 const struct ofp_action_header *oah, size_t length,
7617 const struct ofpact_raw_instance **instp)
7619 const struct ofpact_raw_instance *inst;
7620 struct ofpact_hdrs hdrs;
7623 if (length < sizeof *oah) {
7624 return OFPERR_OFPBAC_BAD_LEN;
7627 /* Get base action type. */
7628 if (oah->type == htons(OFPAT_VENDOR)) {
7630 hdrs.vendor = ntohl(oah->vendor);
7631 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
7632 /* Get extension subtype. */
7633 const struct ext_action_header *nah;
7635 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
7636 if (length < sizeof *nah) {
7637 return OFPERR_OFPBAC_BAD_LEN;
7639 hdrs.type = ntohs(nah->subtype);
7641 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
7643 return OFPERR_OFPBAC_BAD_VENDOR;
7647 hdrs.type = ntohs(oah->type);
7650 hdrs.ofp_version = ofp_version;
7651 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
7652 ofpact_decode_hmap()) {
7653 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
7660 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7661 : OFPERR_OFPBAC_BAD_TYPE);
7665 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7666 enum ofp_raw_action_type *raw, uint64_t *arg)
7668 const struct ofp_action_header *oah = buf->data;
7669 const struct ofpact_raw_instance *action;
7670 unsigned int length;
7674 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
7679 if (action->deprecation) {
7680 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
7681 action->name, ofputil_version_to_string(ofp_version),
7682 action->deprecation);
7685 length = ntohs(oah->len);
7686 if (length > buf->size) {
7687 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
7688 "length %"PRIu32, action->name, length, buf->size);
7689 return OFPERR_OFPBAC_BAD_LEN;
7691 if (length < action->min_length || length > action->max_length) {
7692 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
7693 "[%hu,%hu]", action->name, length,
7694 action->min_length, action->max_length);
7695 return OFPERR_OFPBAC_BAD_LEN;
7698 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
7699 "of 8", action->name, length);
7700 return OFPERR_OFPBAC_BAD_LEN;
7705 if (action->arg_len) {
7709 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
7710 for (i = 0; i < action->arg_len; i++) {
7711 *arg = (*arg << 8) | p[i];
7715 ofpbuf_pull(buf, length);
7720 static const struct ofpact_raw_instance *
7721 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
7723 const struct ofpact_raw_instance *inst;
7725 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
7726 ofpact_encode_hmap()) {
7727 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
7735 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7736 enum ofp_raw_action_type raw, uint64_t arg)
7738 const struct ofpact_raw_instance *inst;
7739 struct ofp_action_header *oah;
7740 const struct ofpact_hdrs *hdrs;
7742 inst = ofpact_raw_lookup(ofp_version, raw);
7745 oah = ofpbuf_put_zeros(buf, inst->min_length);
7746 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
7747 oah->len = htons(inst->min_length);
7748 oah->vendor = htonl(hdrs->vendor);
7750 switch (hdrs->vendor) {
7755 case ONF_VENDOR_ID: {
7756 struct ext_action_header *nah = (struct ext_action_header *) oah;
7757 nah->subtype = htons(hdrs->type);
7765 if (inst->arg_len) {
7766 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
7769 for (i = 0; i < inst->arg_len; i++) {
7781 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
7783 struct ofp_action_header *oah;
7785 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs,
7788 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
7789 oah->len = htons(openflow->size - start_ofs);