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 size_t start_ofs = ofpacts->size;
4405 ofpact_put_NOTE(ofpacts);
4406 arg = ofpbuf_put_hex(ofpacts, arg, NULL);
4408 return xstrdup("bad hex digit in `note' argument");
4410 struct ofpact_note *note = ofpbuf_at_assert(ofpacts, start_ofs,
4412 note->length = ofpacts->size - (start_ofs + sizeof *note);
4413 ofpact_finish(ofpacts, ¬e->ofpact);
4418 format_NOTE(const struct ofpact_note *a, struct ds *s)
4422 ds_put_cstr(s, "note:");
4423 for (i = 0; i < a->length; i++) {
4425 ds_put_char(s, '.');
4427 ds_put_format(s, "%02"PRIx8, a->data[i]);
4434 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4436 ofpact_put_EXIT(out);
4441 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4442 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4444 put_NXAST_EXIT(out);
4447 static char * OVS_WARN_UNUSED_RESULT
4448 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4449 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4451 ofpact_put_EXIT(ofpacts);
4456 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4458 ds_put_cstr(s, "exit");
4461 /* Unroll xlate action. */
4464 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4465 enum ofp_version ofp_version OVS_UNUSED,
4466 struct ofpbuf *out OVS_UNUSED)
4471 static char * OVS_WARN_UNUSED_RESULT
4472 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4473 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4480 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a, struct ds *s)
4482 ds_put_format(s, "unroll_xlate(table=%"PRIu8", cookie=%"PRIu64")",
4483 a->rule_table_id, ntohll(a->rule_cookie));
4486 /* Action structure for NXAST_SAMPLE.
4488 * Samples matching packets with the given probability and sends them
4489 * each to the set of collectors identified with the given ID. The
4490 * probability is expressed as a number of packets to be sampled out
4491 * of USHRT_MAX packets, and must be >0.
4493 * When sending packet samples to IPFIX collectors, the IPFIX flow
4494 * record sent for each sampled packet is associated with the given
4495 * observation domain ID and observation point ID. Each IPFIX flow
4496 * record contain the sampled packet's headers when executing this
4497 * rule. If a sampled packet's headers are modified by previous
4498 * actions in the flow, those modified headers are sent. */
4499 struct nx_action_sample {
4500 ovs_be16 type; /* OFPAT_VENDOR. */
4501 ovs_be16 len; /* Length is 24. */
4502 ovs_be32 vendor; /* NX_VENDOR_ID. */
4503 ovs_be16 subtype; /* NXAST_SAMPLE. */
4504 ovs_be16 probability; /* Fraction of packets to sample. */
4505 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4506 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4507 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4509 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4512 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4513 enum ofp_version ofp_version OVS_UNUSED,
4516 struct ofpact_sample *sample;
4518 sample = ofpact_put_SAMPLE(out);
4519 sample->probability = ntohs(nas->probability);
4520 sample->collector_set_id = ntohl(nas->collector_set_id);
4521 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4522 sample->obs_point_id = ntohl(nas->obs_point_id);
4524 if (sample->probability == 0) {
4525 return OFPERR_OFPBAC_BAD_ARGUMENT;
4532 encode_SAMPLE(const struct ofpact_sample *sample,
4533 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4535 struct nx_action_sample *nas;
4537 nas = put_NXAST_SAMPLE(out);
4538 nas->probability = htons(sample->probability);
4539 nas->collector_set_id = htonl(sample->collector_set_id);
4540 nas->obs_domain_id = htonl(sample->obs_domain_id);
4541 nas->obs_point_id = htonl(sample->obs_point_id);
4544 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4545 * action to 'ofpacts'.
4547 * Returns NULL if successful, otherwise a malloc()'d string describing the
4548 * error. The caller is responsible for freeing the returned string. */
4549 static char * OVS_WARN_UNUSED_RESULT
4550 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4551 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4553 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4556 while (ofputil_parse_key_value(&arg, &key, &value)) {
4559 if (!strcmp(key, "probability")) {
4560 error = str_to_u16(value, "probability", &os->probability);
4561 if (!error && os->probability == 0) {
4562 error = xasprintf("invalid probability value \"%s\"", value);
4564 } else if (!strcmp(key, "collector_set_id")) {
4565 error = str_to_u32(value, &os->collector_set_id);
4566 } else if (!strcmp(key, "obs_domain_id")) {
4567 error = str_to_u32(value, &os->obs_domain_id);
4568 } else if (!strcmp(key, "obs_point_id")) {
4569 error = str_to_u32(value, &os->obs_point_id);
4571 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4578 if (os->probability == 0) {
4579 return xstrdup("non-zero \"probability\" must be specified on sample");
4585 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4587 ds_put_format(s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
4588 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
4589 a->probability, a->collector_set_id,
4590 a->obs_domain_id, a->obs_point_id);
4593 /* debug_recirc instruction. */
4595 static bool enable_debug;
4598 ofpact_dummy_enable(void)
4600 enable_debug = true;
4604 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4606 if (!enable_debug) {
4607 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4610 ofpact_put_DEBUG_RECIRC(out);
4615 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4616 enum ofp_version ofp_version OVS_UNUSED,
4619 put_NXAST_DEBUG_RECIRC(out);
4622 static char * OVS_WARN_UNUSED_RESULT
4623 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4624 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4626 ofpact_put_DEBUG_RECIRC(ofpacts);
4631 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4633 ds_put_cstr(s, "debug_recirc");
4636 /* Action structure for NXAST_CT.
4638 * Pass traffic to the connection tracker.
4640 * There are two important concepts to understanding the connection tracking
4641 * interface: Packet state and Connection state. Packets may be "Untracked" or
4642 * "Tracked". Connections may be "Uncommitted" or "Committed".
4646 * Untracked packets have not yet passed through the connection tracker,
4647 * and the connection state for such packets is unknown. In most cases,
4648 * packets entering the OpenFlow pipeline will initially be in the
4649 * untracked state. Untracked packets may become tracked by executing
4650 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4651 * about the connection available, in particular the connection state.
4653 * Tracked packets have previously passed through the connection tracker.
4654 * These packets will remain tracked through until the end of the OpenFlow
4655 * pipeline. Tracked packets which have NXAST_CT executed with a
4656 * "recirc_table" specified will return to the tracked state.
4658 * The packet state is only significant for the duration of packet
4659 * processing within the OpenFlow pipeline.
4661 * - Connection State:
4663 * Multiple packets may be associated with a single connection. Initially,
4664 * all connections are uncommitted. The connection state corresponding to
4665 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4667 * Uncommitted connections have no state stored about them. Uncommitted
4668 * connections may transition into the committed state by executing
4669 * NXAST_CT with the NX_CT_F_COMMIT flag.
4671 * Once a connection becomes committed, information may be gathered about
4672 * the connection by passing subsequent packets through the connection
4673 * tracker, and the state of the connection will be stored beyond the
4674 * lifetime of packet processing.
4676 * Connections may transition back into the uncommitted state due to
4677 * external timers, or due to the contents of packets that are sent to the
4678 * connection tracker. This behaviour is outside of the scope of the
4679 * OpenFlow interface.
4681 * The "zone" specifies a context within which the tracking is done:
4683 * The connection tracking zone is a 16-bit number. Each zone is an
4684 * independent connection tracking context. The connection state for each
4685 * connection is completely separate for each zone, so if a connection
4686 * is committed to zone A, then it will remain uncommitted in zone B.
4687 * If NXAST_CT is executed with the same zone multiple times, later
4688 * executions have no effect.
4690 * If 'zone_src' is nonzero, this specifies that the zone should be
4691 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4692 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4693 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4694 * are the same as the acceptable nxm_header values for the 'src' field of
4697 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4698 * connection tracking zone.
4700 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4702 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4703 * packet will be logically cloned prior to executing this action. One
4704 * copy will be sent to the connection tracker, then will be re-injected
4705 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
4706 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4707 * fields will be populated. The original instance of the packet will
4708 * continue the current actions list. This can be thought of as similar to
4709 * the effect of the "output" action: One copy is sent out (in this case,
4710 * to the connection tracker), but the current copy continues processing.
4712 * It is strongly recommended that this table is later than the current
4713 * table, to prevent loops.
4715 * The "alg" attaches protocol-specific behaviour to this action:
4717 * The ALG is a 16-bit number which specifies that additional
4718 * processing should be applied to this traffic.
4720 * Protocol | Value | Meaning
4721 * --------------------------------------------------------------------
4722 * None | 0 | No protocol-specific behaviour.
4723 * FTP | 21 | Parse FTP control connections and observe the
4724 * | | negotiation of related data connections.
4725 * Other | Other | Unsupported protocols.
4727 * By way of example, if FTP control connections have this action applied
4728 * with the ALG set to FTP (21), then the connection tracker will observe
4729 * the negotiation of data connections. This allows the connection
4730 * tracker to identify subsequent data connections as "related" to this
4731 * existing connection. The "related" flag will be populated in the
4732 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
4735 * Zero or more actions may immediately follow this action. These actions will
4736 * be executed within the context of the connection tracker, and they require
4737 * the NX_CT_F_COMMIT flag to be set.
4739 struct nx_action_conntrack {
4740 ovs_be16 type; /* OFPAT_VENDOR. */
4741 ovs_be16 len; /* At least 24. */
4742 ovs_be32 vendor; /* NX_VENDOR_ID. */
4743 ovs_be16 subtype; /* NXAST_CT. */
4744 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
4745 * Unspecified flag bits must be zero. */
4746 ovs_be32 zone_src; /* Connection tracking context. */
4748 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
4749 ovs_be16 zone_imm; /* Immediate value for zone. */
4751 uint8_t recirc_table; /* Recirculate to a specific table, or
4752 NX_CT_RECIRC_NONE for no recirculation. */
4753 uint8_t pad[3]; /* Zeroes */
4754 ovs_be16 alg; /* Well-known port number for the protocol.
4755 * 0 indicates no ALG is required. */
4756 /* Followed by a sequence of zero or more OpenFlow actions. The length of
4757 * these is included in 'len'. */
4759 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
4762 decode_ct_zone(const struct nx_action_conntrack *nac,
4763 struct ofpact_conntrack *out)
4765 if (nac->zone_src) {
4768 out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
4769 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
4770 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
4771 error = mf_check_src(&out->zone_src, NULL);
4776 if (out->zone_src.n_bits != 16) {
4777 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
4778 out->zone_src.n_bits);
4779 return OFPERR_OFPBAC_BAD_SET_LEN;
4782 out->zone_src.field = NULL;
4783 out->zone_imm = ntohs(nac->zone_imm);
4790 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
4791 enum ofp_version ofp_version, struct ofpbuf *out)
4793 const size_t ct_offset = ofpacts_pull(out);
4794 struct ofpact_conntrack *conntrack;
4795 struct ofpbuf openflow;
4798 conntrack = ofpact_put_CT(out);
4799 conntrack->flags = ntohs(nac->flags);
4800 error = decode_ct_zone(nac, conntrack);
4804 conntrack->recirc_table = nac->recirc_table;
4805 conntrack->alg = ntohs(nac->alg);
4807 ofpbuf_pull(out, sizeof(*conntrack));
4809 ofpbuf_use_const(&openflow, nac + 1, ntohs(nac->len) - sizeof(*nac));
4810 error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
4812 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4818 conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
4819 out->header = &conntrack->ofpact;
4820 ofpact_finish(out, &conntrack->ofpact);
4822 if (conntrack->ofpact.len > sizeof(*conntrack)
4823 && !(conntrack->flags & NX_CT_F_COMMIT)) {
4824 const struct ofpact *a;
4825 size_t ofpacts_len = conntrack->ofpact.len - sizeof(*conntrack);
4827 OFPACT_FOR_EACH (a, conntrack->actions, ofpacts_len) {
4828 if (a->type != OFPACT_NAT || ofpact_get_NAT(a)->flags
4829 || ofpact_get_NAT(a)->range_af != AF_UNSPEC) {
4830 VLOG_WARN_RL(&rl, "CT action requires commit flag if actions "
4831 "other than NAT without arguments are specified.");
4832 error = OFPERR_OFPBAC_BAD_ARGUMENT;
4839 ofpbuf_push_uninit(out, ct_offset);
4844 encode_CT(const struct ofpact_conntrack *conntrack,
4845 enum ofp_version ofp_version, struct ofpbuf *out)
4847 struct nx_action_conntrack *nac;
4848 const size_t ofs = out->size;
4851 nac = put_NXAST_CT(out);
4852 nac->flags = htons(conntrack->flags);
4853 if (conntrack->zone_src.field) {
4854 nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
4855 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
4856 conntrack->zone_src.n_bits);
4858 nac->zone_src = htonl(0);
4859 nac->zone_imm = htons(conntrack->zone_imm);
4861 nac->recirc_table = conntrack->recirc_table;
4862 nac->alg = htons(conntrack->alg);
4864 len = ofpacts_put_openflow_actions(conntrack->actions,
4865 ofpact_ct_get_action_len(conntrack),
4867 len += sizeof(*nac);
4868 nac = ofpbuf_at(out, ofs, sizeof(*nac));
4869 nac->len = htons(len);
4872 static char * OVS_WARN_UNUSED_RESULT parse_NAT(char *arg, struct ofpbuf *,
4873 enum ofputil_protocol * OVS_UNUSED);
4875 /* Parses 'arg' as the argument to a "ct" action, and appends such an
4876 * action to 'ofpacts'.
4878 * Returns NULL if successful, otherwise a malloc()'d string describing the
4879 * error. The caller is responsible for freeing the returned string. */
4880 static char * OVS_WARN_UNUSED_RESULT
4881 parse_CT(char *arg, struct ofpbuf *ofpacts,
4882 enum ofputil_protocol *usable_protocols)
4884 const size_t ct_offset = ofpacts_pull(ofpacts);
4885 struct ofpact_conntrack *oc;
4889 oc = ofpact_put_CT(ofpacts);
4891 oc->recirc_table = NX_CT_RECIRC_NONE;
4892 while (ofputil_parse_key_value(&arg, &key, &value)) {
4893 if (!strcmp(key, "commit")) {
4894 oc->flags |= NX_CT_F_COMMIT;
4895 } else if (!strcmp(key, "table")) {
4896 error = str_to_u8(value, "recirc_table", &oc->recirc_table);
4897 if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
4898 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
4900 } else if (!strcmp(key, "zone")) {
4901 error = str_to_u16(value, "zone", &oc->zone_imm);
4905 error = mf_parse_subfield(&oc->zone_src, value);
4910 } else if (!strcmp(key, "alg")) {
4911 error = str_to_connhelper(value, &oc->alg);
4912 } else if (!strcmp(key, "nat")) {
4913 const size_t nat_offset = ofpacts_pull(ofpacts);
4915 error = parse_NAT(value, ofpacts, usable_protocols);
4916 /* Update CT action pointer and length. */
4917 ofpacts->header = ofpbuf_push_uninit(ofpacts, nat_offset);
4918 oc = ofpacts->header;
4919 } else if (!strcmp(key, "exec")) {
4920 /* Hide existing actions from ofpacts_parse_copy(), so the
4921 * nesting can be handled transparently. */
4922 enum ofputil_protocol usable_protocols2;
4923 const size_t exec_offset = ofpacts_pull(ofpacts);
4925 /* Initializes 'usable_protocol2', fold it back to
4926 * '*usable_protocols' afterwards, so that we do not lose
4927 * restrictions already in there. */
4928 error = ofpacts_parse_copy(value, ofpacts, &usable_protocols2,
4930 *usable_protocols &= usable_protocols2;
4931 ofpacts->header = ofpbuf_push_uninit(ofpacts, exec_offset);
4932 oc = ofpacts->header;
4934 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
4941 ofpact_finish(ofpacts, &oc->ofpact);
4942 ofpbuf_push_uninit(ofpacts, ct_offset);
4947 format_alg(int port, struct ds *s)
4949 if (port == IPPORT_FTP) {
4950 ds_put_format(s, "alg=ftp,");
4952 ds_put_format(s, "alg=%d,", port);
4956 static void format_NAT(const struct ofpact_nat *a, struct ds *ds);
4959 format_CT(const struct ofpact_conntrack *a, struct ds *s)
4961 ds_put_cstr(s, "ct(");
4962 if (a->flags & NX_CT_F_COMMIT) {
4963 ds_put_cstr(s, "commit,");
4965 if (a->recirc_table != NX_CT_RECIRC_NONE) {
4966 ds_put_format(s, "table=%"PRIu8",", a->recirc_table);
4968 if (a->zone_src.field) {
4969 ds_put_format(s, "zone=");
4970 mf_format_subfield(&a->zone_src, s);
4971 ds_put_char(s, ',');
4972 } else if (a->zone_imm) {
4973 ds_put_format(s, "zone=%"PRIu16",", a->zone_imm);
4975 /* If the first action is a NAT action, format it outside of the 'exec'
4977 const struct ofpact *action = a->actions;
4978 size_t actions_len = ofpact_ct_get_action_len(a);
4979 if (actions_len && action->type == OFPACT_NAT) {
4980 format_NAT(ofpact_get_NAT(action), s);
4981 ds_put_char(s, ',');
4982 actions_len -= OFPACT_ALIGN(action->len);
4983 action = ofpact_next(action);
4986 ds_put_cstr(s, "exec(");
4987 ofpacts_format(action, actions_len, s);
4988 ds_put_cstr(s, "),");
4990 format_alg(a->alg, s);
4992 ds_put_char(s, ')');
4997 /* Which optional fields are present? */
4999 NX_NAT_RANGE_IPV4_MIN = 1 << 0, /* ovs_be32 */
5000 NX_NAT_RANGE_IPV4_MAX = 1 << 1, /* ovs_be32 */
5001 NX_NAT_RANGE_IPV6_MIN = 1 << 2, /* struct in6_addr */
5002 NX_NAT_RANGE_IPV6_MAX = 1 << 3, /* struct in6_addr */
5003 NX_NAT_RANGE_PROTO_MIN = 1 << 4, /* ovs_be16 */
5004 NX_NAT_RANGE_PROTO_MAX = 1 << 5, /* ovs_be16 */
5007 /* Action structure for NXAST_NAT. */
5008 struct nx_action_nat {
5009 ovs_be16 type; /* OFPAT_VENDOR. */
5010 ovs_be16 len; /* At least 16. */
5011 ovs_be32 vendor; /* NX_VENDOR_ID. */
5012 ovs_be16 subtype; /* NXAST_NAT. */
5013 uint8_t pad[2]; /* Must be zero. */
5014 ovs_be16 flags; /* Zero or more NX_NAT_F_* flags.
5015 * Unspecified flag bits must be zero. */
5016 ovs_be16 range_present; /* NX_NAT_RANGE_* */
5017 /* Followed by optional parameters as specified by 'range_present' */
5019 OFP_ASSERT(sizeof(struct nx_action_nat) == 16);
5022 encode_NAT(const struct ofpact_nat *nat,
5023 enum ofp_version ofp_version OVS_UNUSED,
5026 struct nx_action_nat *nan;
5027 const size_t ofs = out->size;
5028 uint16_t range_present = 0;
5030 nan = put_NXAST_NAT(out);
5031 nan->flags = htons(nat->flags);
5032 if (nat->range_af == AF_INET) {
5033 if (nat->range.addr.ipv4.min) {
5034 ovs_be32 *min = ofpbuf_put_uninit(out, sizeof *min);
5035 *min = nat->range.addr.ipv4.min;
5036 range_present |= NX_NAT_RANGE_IPV4_MIN;
5038 if (nat->range.addr.ipv4.max) {
5039 ovs_be32 *max = ofpbuf_put_uninit(out, sizeof *max);
5040 *max = nat->range.addr.ipv4.max;
5041 range_present |= NX_NAT_RANGE_IPV4_MAX;
5043 } else if (nat->range_af == AF_INET6) {
5044 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5045 struct in6_addr *min = ofpbuf_put_uninit(out, sizeof *min);
5046 *min = nat->range.addr.ipv6.min;
5047 range_present |= NX_NAT_RANGE_IPV6_MIN;
5049 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.max)) {
5050 struct in6_addr *max = ofpbuf_put_uninit(out, sizeof *max);
5051 *max = nat->range.addr.ipv6.max;
5052 range_present |= NX_NAT_RANGE_IPV6_MAX;
5055 if (nat->range_af != AF_UNSPEC) {
5056 if (nat->range.proto.min) {
5057 ovs_be16 *min = ofpbuf_put_uninit(out, sizeof *min);
5058 *min = htons(nat->range.proto.min);
5059 range_present |= NX_NAT_RANGE_PROTO_MIN;
5061 if (nat->range.proto.max) {
5062 ovs_be16 *max = ofpbuf_put_uninit(out, sizeof *max);
5063 *max = htons(nat->range.proto.max);
5064 range_present |= NX_NAT_RANGE_PROTO_MAX;
5067 pad_ofpat(out, ofs);
5068 nan = ofpbuf_at(out, ofs, sizeof *nan);
5069 nan->range_present = htons(range_present);
5073 decode_NXAST_RAW_NAT(const struct nx_action_nat *nan,
5074 enum ofp_version ofp_version OVS_UNUSED,
5077 struct ofpact_nat *nat;
5078 uint16_t range_present = ntohs(nan->range_present);
5079 const char *opts = (char *)(nan + 1);
5080 uint16_t len = ntohs(nan->len) - sizeof *nan;
5082 nat = ofpact_put_NAT(out);
5083 nat->flags = ntohs(nan->flags);
5085 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
5086 (LEN >= sizeof(TYPE) \
5087 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
5088 SRC += sizeof(TYPE)) \
5091 nat->range_af = AF_UNSPEC;
5092 if (range_present & NX_NAT_RANGE_IPV4_MIN) {
5093 if (range_present & (NX_NAT_RANGE_IPV6_MIN | NX_NAT_RANGE_IPV6_MAX)) {
5094 return OFPERR_OFPBAC_BAD_ARGUMENT;
5097 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.min, opts, len, ovs_be32)
5098 || !nat->range.addr.ipv4.min) {
5099 return OFPERR_OFPBAC_BAD_ARGUMENT;
5102 nat->range_af = AF_INET;
5104 if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5105 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.max, opts, len,
5107 return OFPERR_OFPBAC_BAD_ARGUMENT;
5109 if (ntohl(nat->range.addr.ipv4.max)
5110 < ntohl(nat->range.addr.ipv4.min)) {
5111 return OFPERR_OFPBAC_BAD_ARGUMENT;
5114 } else if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5115 return OFPERR_OFPBAC_BAD_ARGUMENT;
5116 } else if (range_present & NX_NAT_RANGE_IPV6_MIN) {
5117 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.min, opts, len,
5119 || ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5120 return OFPERR_OFPBAC_BAD_ARGUMENT;
5123 nat->range_af = AF_INET6;
5125 if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5126 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.max, opts, len,
5128 return OFPERR_OFPBAC_BAD_ARGUMENT;
5130 if (memcmp(&nat->range.addr.ipv6.max, &nat->range.addr.ipv6.min,
5131 sizeof(struct in6_addr)) < 0) {
5132 return OFPERR_OFPBAC_BAD_ARGUMENT;
5135 } else if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5136 return OFPERR_OFPBAC_BAD_ARGUMENT;
5139 if (range_present & NX_NAT_RANGE_PROTO_MIN) {
5142 if (nat->range_af == AF_UNSPEC) {
5143 return OFPERR_OFPBAC_BAD_ARGUMENT;
5145 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16) || proto == 0) {
5146 return OFPERR_OFPBAC_BAD_ARGUMENT;
5148 nat->range.proto.min = ntohs(proto);
5149 if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5150 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16)) {
5151 return OFPERR_OFPBAC_BAD_ARGUMENT;
5153 nat->range.proto.max = ntohs(proto);
5154 if (nat->range.proto.max < nat->range.proto.min) {
5155 return OFPERR_OFPBAC_BAD_ARGUMENT;
5158 } else if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5159 return OFPERR_OFPBAC_BAD_ARGUMENT;
5166 format_NAT(const struct ofpact_nat *a, struct ds *ds)
5168 ds_put_cstr(ds, "nat");
5170 if (a->flags & (NX_NAT_F_SRC | NX_NAT_F_DST)) {
5171 ds_put_char(ds, '(');
5172 ds_put_cstr(ds, a->flags & NX_NAT_F_SRC ? "src" : "dst");
5174 if (a->range_af != AF_UNSPEC) {
5175 ds_put_cstr(ds, "=");
5177 if (a->range_af == AF_INET) {
5178 ds_put_format(ds, IP_FMT, IP_ARGS(a->range.addr.ipv4.min));
5180 if (a->range.addr.ipv4.max
5181 && a->range.addr.ipv4.max != a->range.addr.ipv4.min) {
5182 ds_put_format(ds, "-"IP_FMT,
5183 IP_ARGS(a->range.addr.ipv4.max));
5185 } else if (a->range_af == AF_INET6) {
5186 ipv6_format_addr_bracket(&a->range.addr.ipv6.min, ds,
5187 a->range.proto.min);
5189 if (!ipv6_mask_is_any(&a->range.addr.ipv6.max)
5190 && memcmp(&a->range.addr.ipv6.max, &a->range.addr.ipv6.min,
5191 sizeof(struct in6_addr)) != 0) {
5192 ds_put_char(ds, '-');
5193 ipv6_format_addr_bracket(&a->range.addr.ipv6.max, ds,
5194 a->range.proto.min);
5197 if (a->range.proto.min) {
5198 ds_put_char(ds, ':');
5199 ds_put_format(ds, "%"PRIu16, a->range.proto.min);
5201 if (a->range.proto.max
5202 && a->range.proto.max != a->range.proto.min) {
5203 ds_put_format(ds, "-%"PRIu16, a->range.proto.max);
5206 ds_put_char(ds, ',');
5208 if (a->flags & NX_NAT_F_PERSISTENT) {
5209 ds_put_cstr(ds, "persistent,");
5211 if (a->flags & NX_NAT_F_PROTO_HASH) {
5212 ds_put_cstr(ds, "hash,");
5214 if (a->flags & NX_NAT_F_PROTO_RANDOM) {
5215 ds_put_cstr(ds, "random,");
5219 ds_put_char(ds, ')');
5223 static char * OVS_WARN_UNUSED_RESULT
5224 str_to_nat_range(const char *s, struct ofpact_nat *on)
5226 char ipv6_s[IPV6_SCAN_LEN + 1];
5229 on->range_af = AF_UNSPEC;
5230 if (ovs_scan_len(s, &n, IP_SCAN_FMT,
5231 IP_SCAN_ARGS(&on->range.addr.ipv4.min))) {
5232 on->range_af = AF_INET;
5236 if (!ovs_scan_len(s, &n, IP_SCAN_FMT,
5237 IP_SCAN_ARGS(&on->range.addr.ipv4.max))
5238 || (ntohl(on->range.addr.ipv4.max)
5239 < ntohl(on->range.addr.ipv4.min))) {
5243 } else if ((ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5244 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5245 && inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.min) == 1) {
5246 on->range_af = AF_INET6;
5250 if (!(ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5251 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5252 || inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.max) != 1
5253 || memcmp(&on->range.addr.ipv6.max, &on->range.addr.ipv6.min,
5254 sizeof on->range.addr.ipv6.max) < 0) {
5259 if (on->range_af != AF_UNSPEC && s[n] == ':') {
5261 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.min)) {
5266 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.max)
5267 || on->range.proto.max < on->range.proto.min) {
5272 if (strlen(s) != n) {
5273 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
5278 return xasprintf("invalid nat range \"%s\"", s);
5282 /* Parses 'arg' as the argument to a "nat" action, and appends such an
5283 * action to 'ofpacts'.
5285 * Returns NULL if successful, otherwise a malloc()'d string describing the
5286 * error. The caller is responsible for freeing the returned string. */
5287 static char * OVS_WARN_UNUSED_RESULT
5288 parse_NAT(char *arg, struct ofpbuf *ofpacts,
5289 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5291 struct ofpact_nat *on = ofpact_put_NAT(ofpacts);
5295 on->range_af = AF_UNSPEC;
5297 while (ofputil_parse_key_value(&arg, &key, &value)) {
5300 if (!strcmp(key, "src")) {
5301 on->flags |= NX_NAT_F_SRC;
5302 error = str_to_nat_range(value, on);
5303 } else if (!strcmp(key, "dst")) {
5304 on->flags |= NX_NAT_F_DST;
5305 error = str_to_nat_range(value, on);
5306 } else if (!strcmp(key, "persistent")) {
5307 on->flags |= NX_NAT_F_PERSISTENT;
5308 } else if (!strcmp(key, "hash")) {
5309 on->flags |= NX_NAT_F_PROTO_HASH;
5310 } else if (!strcmp(key, "random")) {
5311 on->flags |= NX_NAT_F_PROTO_RANDOM;
5313 error = xasprintf("invalid key \"%s\" in \"nat\" argument",
5320 if (on->flags & NX_NAT_F_SRC && on->flags & NX_NAT_F_DST) {
5321 return xasprintf("May only specify one of \"snat\" or \"dnat\".");
5323 if (!(on->flags & NX_NAT_F_SRC || on->flags & NX_NAT_F_DST)) {
5325 return xasprintf("Flags allowed only with \"snat\" or \"dnat\".");
5327 if (on->range_af != AF_UNSPEC) {
5328 return xasprintf("Range allowed only with \"snat\" or \"dnat\".");
5335 /* Meter instruction. */
5338 encode_METER(const struct ofpact_meter *meter,
5339 enum ofp_version ofp_version, struct ofpbuf *out)
5341 if (ofp_version >= OFP13_VERSION) {
5342 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
5346 static char * OVS_WARN_UNUSED_RESULT
5347 parse_METER(char *arg, struct ofpbuf *ofpacts,
5348 enum ofputil_protocol *usable_protocols)
5350 *usable_protocols &= OFPUTIL_P_OF13_UP;
5351 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
5355 format_METER(const struct ofpact_meter *a, struct ds *s)
5357 ds_put_format(s, "meter:%"PRIu32, a->meter_id);
5360 /* Clear-Actions instruction. */
5363 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
5364 enum ofp_version ofp_version OVS_UNUSED,
5365 struct ofpbuf *out OVS_UNUSED)
5367 if (ofp_version > OFP10_VERSION) {
5368 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
5372 static char * OVS_WARN_UNUSED_RESULT
5373 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
5374 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5376 ofpact_put_CLEAR_ACTIONS(ofpacts);
5381 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
5383 ds_put_cstr(s, "clear_actions");
5386 /* Write-Actions instruction. */
5389 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
5390 enum ofp_version ofp_version, struct ofpbuf *out)
5392 if (ofp_version > OFP10_VERSION) {
5393 const size_t ofs = out->size;
5395 instruction_put_OFPIT11_WRITE_ACTIONS(out);
5396 ofpacts_put_openflow_actions(actions->actions,
5397 ofpact_nest_get_action_len(actions),
5399 ofpacts_update_instruction_actions(out, ofs);
5403 static char * OVS_WARN_UNUSED_RESULT
5404 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
5405 enum ofputil_protocol *usable_protocols)
5407 size_t ofs = ofpacts_pull(ofpacts);
5408 struct ofpact_nest *on;
5411 /* Add a Write-Actions instruction and then pull it off. */
5412 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
5413 ofpbuf_pull(ofpacts, sizeof *on);
5415 /* Parse nested actions.
5417 * We pulled off "write-actions" and the previous actions because the
5418 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
5419 * that it doesn't actually include the nested actions. That means that
5420 * ofpacts_parse() would reject them as being part of an Apply-Actions that
5421 * follows a Write-Actions, which is an invalid order. */
5422 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
5423 OFPACT_WRITE_ACTIONS);
5425 /* Put the Write-Actions back on and update its length. */
5426 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
5427 on->ofpact.len = ofpacts->size;
5429 /* Put any previous actions or instructions back on. */
5430 ofpbuf_push_uninit(ofpacts, ofs);
5436 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
5438 ds_put_cstr(s, "write_actions(");
5439 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
5440 ds_put_char(s, ')');
5443 /* Action structure for NXAST_WRITE_METADATA.
5445 * Modifies the 'mask' bits of the metadata value. */
5446 struct nx_action_write_metadata {
5447 ovs_be16 type; /* OFPAT_VENDOR. */
5448 ovs_be16 len; /* Length is 32. */
5449 ovs_be32 vendor; /* NX_VENDOR_ID. */
5450 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
5451 uint8_t zeros[6]; /* Must be zero. */
5452 ovs_be64 metadata; /* Metadata register. */
5453 ovs_be64 mask; /* Metadata mask. */
5455 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
5458 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
5459 enum ofp_version ofp_version OVS_UNUSED,
5462 struct ofpact_metadata *om;
5464 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
5465 return OFPERR_NXBRC_MUST_BE_ZERO;
5468 om = ofpact_put_WRITE_METADATA(out);
5469 om->metadata = nawm->metadata;
5470 om->mask = nawm->mask;
5476 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
5477 enum ofp_version ofp_version, struct ofpbuf *out)
5479 if (ofp_version == OFP10_VERSION) {
5480 struct nx_action_write_metadata *nawm;
5482 nawm = put_NXAST_WRITE_METADATA(out);
5483 nawm->metadata = metadata->metadata;
5484 nawm->mask = metadata->mask;
5486 struct ofp11_instruction_write_metadata *oiwm;
5488 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
5489 oiwm->metadata = metadata->metadata;
5490 oiwm->metadata_mask = metadata->mask;
5494 static char * OVS_WARN_UNUSED_RESULT
5495 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
5496 enum ofputil_protocol *usable_protocols)
5498 struct ofpact_metadata *om;
5499 char *mask = strchr(arg, '/');
5501 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
5503 om = ofpact_put_WRITE_METADATA(ofpacts);
5508 error = str_to_be64(mask + 1, &om->mask);
5513 om->mask = OVS_BE64_MAX;
5516 return str_to_be64(arg, &om->metadata);
5520 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5522 ds_put_format(s, "write_metadata:%#"PRIx64, ntohll(a->metadata));
5523 if (a->mask != OVS_BE64_MAX) {
5524 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5528 /* Goto-Table instruction. */
5531 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5532 enum ofp_version ofp_version, struct ofpbuf *out)
5534 if (ofp_version == OFP10_VERSION) {
5535 struct nx_action_resubmit *nar;
5537 nar = put_NXAST_RESUBMIT_TABLE(out);
5538 nar->table = goto_table->table_id;
5539 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5541 struct ofp11_instruction_goto_table *oigt;
5543 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5544 oigt->table_id = goto_table->table_id;
5545 memset(oigt->pad, 0, sizeof oigt->pad);
5549 static char * OVS_WARN_UNUSED_RESULT
5550 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5551 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5553 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5554 char *table_s = strsep(&arg, ",");
5555 if (!table_s || !table_s[0]) {
5556 return xstrdup("instruction goto-table needs table id");
5558 return str_to_u8(table_s, "table", &ogt->table_id);
5562 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5564 ds_put_format(s, "goto_table:%"PRIu8, a->table_id);
5568 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
5569 const struct ofp_action_header *bad_action, enum ofperr error)
5571 if (!VLOG_DROP_WARN(&rl)) {
5575 ds_put_hex_dump(&s, actions, actions_len, 0, false);
5576 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5577 (char *)bad_action - (char *)actions,
5578 ofperr_get_name(error), ds_cstr(&s));
5584 ofpacts_decode(const void *actions, size_t actions_len,
5585 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5587 struct ofpbuf openflow;
5589 ofpbuf_use_const(&openflow, actions, actions_len);
5590 while (openflow.size) {
5591 const struct ofp_action_header *action = openflow.data;
5592 enum ofp_raw_action_type raw;
5596 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5598 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
5602 log_bad_action(actions, actions_len, action, error);
5610 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5611 unsigned int actions_len,
5612 enum ofp_version version,
5613 uint32_t allowed_ovsinsts,
5614 struct ofpbuf *ofpacts,
5615 enum ofpact_type outer_action)
5617 const struct ofp_action_header *actions;
5618 size_t orig_size = ofpacts->size;
5621 if (actions_len % OFP_ACTION_ALIGN != 0) {
5622 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5623 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5624 return OFPERR_OFPBRC_BAD_LEN;
5627 actions = ofpbuf_try_pull(openflow, actions_len);
5628 if (actions == NULL) {
5629 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
5630 "remaining message length (%"PRIu32")",
5631 actions_len, openflow->size);
5632 return OFPERR_OFPBRC_BAD_LEN;
5635 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5637 ofpacts->size = orig_size;
5641 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
5644 ofpacts->size = orig_size;
5649 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
5650 * of 'openflow' into ofpacts. On success, appends the converted actions to
5651 * 'ofpacts'; on failure, 'ofpacts' is unchanged (but might be reallocated) .
5652 * Returns 0 if successful, otherwise an OpenFlow error.
5654 * Actions are processed according to their OpenFlow version which
5655 * is provided in the 'version' parameter.
5657 * In most places in OpenFlow, actions appear encapsulated in instructions, so
5658 * you should call ofpacts_pull_openflow_instructions() instead of this
5661 * The parsed actions are valid generically, but they may not be valid in a
5662 * specific context. For example, port numbers up to OFPP_MAX are valid
5663 * generically, but specific datapaths may only support port numbers in a
5664 * smaller range. Use ofpacts_check() to additional check whether actions are
5665 * valid in a specific context. */
5667 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
5668 unsigned int actions_len,
5669 enum ofp_version version,
5670 struct ofpbuf *ofpacts)
5672 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
5673 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
5677 /* OpenFlow 1.1 actions. */
5680 /* True if an action sets the value of a field
5681 * in a way that is compatibile with the action set.
5682 * The field can be set via either a set or a move action.
5683 * False otherwise. */
5685 ofpact_is_set_or_move_action(const struct ofpact *a)
5688 case OFPACT_SET_FIELD:
5689 case OFPACT_REG_MOVE:
5690 case OFPACT_SET_ETH_DST:
5691 case OFPACT_SET_ETH_SRC:
5692 case OFPACT_SET_IP_DSCP:
5693 case OFPACT_SET_IP_ECN:
5694 case OFPACT_SET_IP_TTL:
5695 case OFPACT_SET_IPV4_DST:
5696 case OFPACT_SET_IPV4_SRC:
5697 case OFPACT_SET_L4_DST_PORT:
5698 case OFPACT_SET_L4_SRC_PORT:
5699 case OFPACT_SET_MPLS_LABEL:
5700 case OFPACT_SET_MPLS_TC:
5701 case OFPACT_SET_MPLS_TTL:
5702 case OFPACT_SET_QUEUE:
5703 case OFPACT_SET_TUNNEL:
5704 case OFPACT_SET_VLAN_PCP:
5705 case OFPACT_SET_VLAN_VID:
5708 case OFPACT_CLEAR_ACTIONS:
5711 case OFPACT_CONTROLLER:
5712 case OFPACT_DEC_MPLS_TTL:
5713 case OFPACT_DEC_TTL:
5714 case OFPACT_ENQUEUE:
5716 case OFPACT_UNROLL_XLATE:
5717 case OFPACT_FIN_TIMEOUT:
5718 case OFPACT_GOTO_TABLE:
5721 case OFPACT_CONJUNCTION:
5723 case OFPACT_MULTIPATH:
5726 case OFPACT_OUTPUT_REG:
5727 case OFPACT_POP_MPLS:
5728 case OFPACT_POP_QUEUE:
5729 case OFPACT_PUSH_MPLS:
5730 case OFPACT_PUSH_VLAN:
5731 case OFPACT_RESUBMIT:
5733 case OFPACT_STACK_POP:
5734 case OFPACT_STACK_PUSH:
5735 case OFPACT_STRIP_VLAN:
5736 case OFPACT_WRITE_ACTIONS:
5737 case OFPACT_WRITE_METADATA:
5738 case OFPACT_DEBUG_RECIRC:
5745 /* True if an action is allowed in the action set.
5746 * False otherwise. */
5748 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
5751 case OFPACT_DEC_MPLS_TTL:
5752 case OFPACT_DEC_TTL:
5755 case OFPACT_POP_MPLS:
5756 case OFPACT_PUSH_MPLS:
5757 case OFPACT_PUSH_VLAN:
5758 case OFPACT_REG_MOVE:
5759 case OFPACT_SET_FIELD:
5760 case OFPACT_SET_ETH_DST:
5761 case OFPACT_SET_ETH_SRC:
5762 case OFPACT_SET_IP_DSCP:
5763 case OFPACT_SET_IP_ECN:
5764 case OFPACT_SET_IP_TTL:
5765 case OFPACT_SET_IPV4_DST:
5766 case OFPACT_SET_IPV4_SRC:
5767 case OFPACT_SET_L4_DST_PORT:
5768 case OFPACT_SET_L4_SRC_PORT:
5769 case OFPACT_SET_MPLS_LABEL:
5770 case OFPACT_SET_MPLS_TC:
5771 case OFPACT_SET_MPLS_TTL:
5772 case OFPACT_SET_QUEUE:
5773 case OFPACT_SET_TUNNEL:
5774 case OFPACT_SET_VLAN_PCP:
5775 case OFPACT_SET_VLAN_VID:
5776 case OFPACT_STRIP_VLAN:
5779 /* In general these actions are excluded because they are not part of
5780 * the OpenFlow specification nor map to actions that are defined in
5781 * the specification. Thus the order in which they should be applied
5782 * in the action set is undefined. */
5784 case OFPACT_CONTROLLER:
5787 case OFPACT_ENQUEUE:
5789 case OFPACT_UNROLL_XLATE:
5790 case OFPACT_FIN_TIMEOUT:
5792 case OFPACT_CONJUNCTION:
5793 case OFPACT_MULTIPATH:
5795 case OFPACT_OUTPUT_REG:
5796 case OFPACT_POP_QUEUE:
5797 case OFPACT_RESUBMIT:
5799 case OFPACT_STACK_POP:
5800 case OFPACT_STACK_PUSH:
5801 case OFPACT_DEBUG_RECIRC:
5803 /* The action set may only include actions and thus
5804 * may not include any instructions */
5805 case OFPACT_CLEAR_ACTIONS:
5806 case OFPACT_GOTO_TABLE:
5808 case OFPACT_WRITE_ACTIONS:
5809 case OFPACT_WRITE_METADATA:
5816 /* Append ofpact 'a' onto the tail of 'out' */
5818 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
5820 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
5823 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
5825 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
5826 enum ofpact_type filter)
5828 const struct ofpact *target;
5829 const struct ofpact *a;
5832 OFPACT_FOR_EACH (a, in->data, in->size) {
5833 if (a->type == filter) {
5838 ofpact_copy(out, target);
5840 return target != NULL;
5843 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
5844 * The order of appended ofpacts is preserved between 'in' and 'out' */
5846 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
5847 bool (*filter)(const struct ofpact *))
5849 const struct ofpact *a;
5851 OFPACT_FOR_EACH (a, in->data, in->size) {
5853 ofpact_copy(out, a);
5858 /* Reads 'action_set', which contains ofpacts accumulated by
5859 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
5860 * executed directly into 'action_list'. (These names correspond to the
5861 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
5863 * In general this involves appending the last instance of each action that is
5864 * admissible in the action set in the order described in the OpenFlow
5868 * + output action is only appended if no group action was present in 'in'.
5869 * + As a simplification all set actions are copied in the order the are
5870 * provided in 'in' as many set actions applied to a field has the same
5871 * affect as only applying the last action that sets a field and
5872 * duplicates are removed by do_xlate_actions().
5873 * This has an unwanted side-effect of compsoting multiple
5874 * LOAD_REG actions that touch different regions of the same field. */
5876 ofpacts_execute_action_set(struct ofpbuf *action_list,
5877 const struct ofpbuf *action_set)
5879 /* The OpenFlow spec "Action Set" section specifies this order. */
5880 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
5881 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
5882 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
5883 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
5884 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
5885 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
5886 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
5887 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
5889 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
5890 * we should execute only OFPACT_GROUP.
5892 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
5893 * all the actions because there's no point in modifying a packet that will
5894 * not be sent anywhere. */
5895 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
5896 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
5897 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
5898 ofpbuf_clear(action_list);
5904 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
5905 size_t n_in, enum ofp_version version,
5910 size_t start = out->size;
5912 error = ofpacts_decode(in, n_in, version, out);
5918 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
5919 if (!ofpact_is_allowed_in_actions_set(a)) {
5920 VLOG_WARN_RL(&rl, "disallowed action in action set");
5921 return OFPERR_OFPBAC_BAD_TYPE;
5928 /* OpenFlow 1.1 instructions. */
5930 struct instruction_type_info {
5931 enum ovs_instruction_type type;
5935 static const struct instruction_type_info inst_info[] = {
5936 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
5942 ovs_instruction_name_from_type(enum ovs_instruction_type type)
5944 return inst_info[type].name;
5948 ovs_instruction_type_from_name(const char *name)
5950 const struct instruction_type_info *p;
5951 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
5952 if (!strcasecmp(name, p->name)) {
5959 enum ovs_instruction_type
5960 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
5964 return OVSINST_OFPIT13_METER;
5965 case OFPACT_CLEAR_ACTIONS:
5966 return OVSINST_OFPIT11_CLEAR_ACTIONS;
5967 case OFPACT_WRITE_ACTIONS:
5968 return OVSINST_OFPIT11_WRITE_ACTIONS;
5969 case OFPACT_WRITE_METADATA:
5970 return OVSINST_OFPIT11_WRITE_METADATA;
5971 case OFPACT_GOTO_TABLE:
5972 return OVSINST_OFPIT11_GOTO_TABLE;
5975 case OFPACT_CONTROLLER:
5976 case OFPACT_ENQUEUE:
5977 case OFPACT_OUTPUT_REG:
5979 case OFPACT_SET_VLAN_VID:
5980 case OFPACT_SET_VLAN_PCP:
5981 case OFPACT_STRIP_VLAN:
5982 case OFPACT_PUSH_VLAN:
5983 case OFPACT_SET_ETH_SRC:
5984 case OFPACT_SET_ETH_DST:
5985 case OFPACT_SET_IPV4_SRC:
5986 case OFPACT_SET_IPV4_DST:
5987 case OFPACT_SET_IP_DSCP:
5988 case OFPACT_SET_IP_ECN:
5989 case OFPACT_SET_IP_TTL:
5990 case OFPACT_SET_L4_SRC_PORT:
5991 case OFPACT_SET_L4_DST_PORT:
5992 case OFPACT_REG_MOVE:
5993 case OFPACT_SET_FIELD:
5994 case OFPACT_STACK_PUSH:
5995 case OFPACT_STACK_POP:
5996 case OFPACT_DEC_TTL:
5997 case OFPACT_SET_MPLS_LABEL:
5998 case OFPACT_SET_MPLS_TC:
5999 case OFPACT_SET_MPLS_TTL:
6000 case OFPACT_DEC_MPLS_TTL:
6001 case OFPACT_PUSH_MPLS:
6002 case OFPACT_POP_MPLS:
6003 case OFPACT_SET_TUNNEL:
6004 case OFPACT_SET_QUEUE:
6005 case OFPACT_POP_QUEUE:
6006 case OFPACT_FIN_TIMEOUT:
6007 case OFPACT_RESUBMIT:
6009 case OFPACT_CONJUNCTION:
6010 case OFPACT_MULTIPATH:
6013 case OFPACT_UNROLL_XLATE:
6015 case OFPACT_DEBUG_RECIRC:
6019 return OVSINST_OFPIT11_APPLY_ACTIONS;
6024 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
6025 const uint16_t inst_type)
6027 switch (inst_type) {
6029 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6031 *instruction_type = OVSINST_##ENUM; \
6037 return OFPERR_OFPBIC_UNKNOWN_INST;
6041 /* Two-way translation between OVS's internal "OVSINST_*" representation of
6042 * instructions and the "OFPIT_*" representation used in OpenFlow. */
6043 struct ovsinst_map {
6044 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
6045 int ofpit; /* OFPIT_* number from OpenFlow spec. */
6048 static const struct ovsinst_map *
6049 get_ovsinst_map(enum ofp_version version)
6051 /* OpenFlow 1.1 and 1.2 instructions. */
6052 static const struct ovsinst_map of11[] = {
6053 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6054 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6055 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6056 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6057 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6061 /* OpenFlow 1.3+ instructions. */
6062 static const struct ovsinst_map of13[] = {
6063 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6064 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6065 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6066 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6067 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6068 { OVSINST_OFPIT13_METER, 6 },
6072 return version < OFP13_VERSION ? of11 : of13;
6075 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
6076 * values, into a bitmap of instructions suitable for OpenFlow 'version'
6077 * (OFP11_VERSION or later), and returns the result. */
6079 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
6081 uint32_t ofpit_bitmap = 0;
6082 const struct ovsinst_map *x;
6084 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6085 if (ovsinst_bitmap & (1u << x->ovsinst)) {
6086 ofpit_bitmap |= 1u << x->ofpit;
6089 return htonl(ofpit_bitmap);
6092 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
6093 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
6094 * correspond to OVSINST_* values, and returns the result. */
6096 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
6098 uint32_t ovsinst_bitmap = 0;
6099 const struct ovsinst_map *x;
6101 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6102 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
6103 ovsinst_bitmap |= 1u << x->ovsinst;
6106 return ovsinst_bitmap;
6109 static inline struct ofp11_instruction *
6110 instruction_next(const struct ofp11_instruction *inst)
6112 return ((struct ofp11_instruction *) (void *)
6113 ((uint8_t *) inst + ntohs(inst->len)));
6117 instruction_is_valid(const struct ofp11_instruction *inst,
6118 size_t n_instructions)
6120 uint16_t len = ntohs(inst->len);
6121 return (!(len % OFP11_INSTRUCTION_ALIGN)
6122 && len >= sizeof *inst
6123 && len / sizeof *inst <= n_instructions);
6126 /* This macro is careful to check for instructions with bad lengths. */
6127 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
6128 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
6129 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
6130 ((LEFT) -= (ntohs((ITER)->len) \
6131 / sizeof(struct ofp11_instruction)), \
6132 (ITER) = instruction_next(ITER)))
6135 decode_openflow11_instruction(const struct ofp11_instruction *inst,
6136 enum ovs_instruction_type *type)
6138 uint16_t len = ntohs(inst->len);
6140 switch (inst->type) {
6141 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
6142 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
6144 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6145 case CONSTANT_HTONS(ENUM): \
6147 ? len >= sizeof(struct STRUCT) \
6148 : len == sizeof(struct STRUCT)) { \
6149 *type = OVSINST_##ENUM; \
6152 return OFPERR_OFPBIC_BAD_LEN; \
6158 return OFPERR_OFPBIC_UNKNOWN_INST;
6163 decode_openflow11_instructions(const struct ofp11_instruction insts[],
6165 const struct ofp11_instruction *out[])
6167 const struct ofp11_instruction *inst;
6170 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
6171 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
6172 enum ovs_instruction_type type;
6175 error = decode_openflow11_instruction(inst, &type);
6181 return OFPERR_OFPBIC_DUP_INST;
6187 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
6188 (n_insts - left) * sizeof *inst);
6189 return OFPERR_OFPBIC_BAD_LEN;
6195 get_actions_from_instruction(const struct ofp11_instruction *inst,
6196 const struct ofp_action_header **actions,
6197 size_t *actions_len)
6199 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
6200 *actions_len = ntohs(inst->len) - sizeof *inst;
6204 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
6205 unsigned int instructions_len,
6206 enum ofp_version version,
6207 struct ofpbuf *ofpacts)
6209 const struct ofp11_instruction *instructions;
6210 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
6213 ofpbuf_clear(ofpacts);
6214 if (version == OFP10_VERSION) {
6215 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
6217 (1u << N_OVS_INSTRUCTIONS) - 1,
6221 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
6222 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
6224 instructions_len, OFP11_INSTRUCTION_ALIGN);
6225 error = OFPERR_OFPBIC_BAD_LEN;
6229 instructions = ofpbuf_try_pull(openflow, instructions_len);
6230 if (instructions == NULL) {
6231 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
6232 "remaining message length (%"PRIu32")",
6233 instructions_len, openflow->size);
6234 error = OFPERR_OFPBIC_BAD_LEN;
6238 error = decode_openflow11_instructions(
6239 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
6245 if (insts[OVSINST_OFPIT13_METER]) {
6246 const struct ofp13_instruction_meter *oim;
6247 struct ofpact_meter *om;
6249 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
6250 insts[OVSINST_OFPIT13_METER]);
6252 om = ofpact_put_METER(ofpacts);
6253 om->meter_id = ntohl(oim->meter_id);
6255 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
6256 const struct ofp_action_header *actions;
6259 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
6260 &actions, &actions_len);
6261 error = ofpacts_decode(actions, actions_len, version, ofpacts);
6266 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
6267 instruction_get_OFPIT11_CLEAR_ACTIONS(
6268 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
6269 ofpact_put_CLEAR_ACTIONS(ofpacts);
6271 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
6272 struct ofpact_nest *on;
6273 const struct ofp_action_header *actions;
6275 size_t start = ofpacts->size;
6276 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
6277 offsetof(struct ofpact_nest, actions));
6278 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
6279 &actions, &actions_len);
6280 error = ofpacts_decode_for_action_set(actions, actions_len,
6285 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
6286 on->ofpact.len = ofpacts->size - start;
6288 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
6289 const struct ofp11_instruction_write_metadata *oiwm;
6290 struct ofpact_metadata *om;
6292 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
6293 insts[OVSINST_OFPIT11_WRITE_METADATA]);
6295 om = ofpact_put_WRITE_METADATA(ofpacts);
6296 om->metadata = oiwm->metadata;
6297 om->mask = oiwm->metadata_mask;
6299 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
6300 const struct ofp11_instruction_goto_table *oigt;
6301 struct ofpact_goto_table *ogt;
6303 oigt = instruction_get_OFPIT11_GOTO_TABLE(
6304 insts[OVSINST_OFPIT11_GOTO_TABLE]);
6305 ogt = ofpact_put_GOTO_TABLE(ofpacts);
6306 ogt->table_id = oigt->table_id;
6309 error = ofpacts_verify(ofpacts->data, ofpacts->size,
6310 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
6313 ofpbuf_clear(ofpacts);
6318 /* Update the length of the instruction that begins at offset 'ofs' within
6319 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
6320 * If the instruction contains no nested actions, deletes it entirely. */
6322 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
6324 struct ofp11_instruction_actions *oia;
6326 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
6327 if (openflow->size > ofs + sizeof *oia) {
6328 oia->len = htons(openflow->size - ofs);
6330 openflow->size = ofs;
6334 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
6335 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
6336 * valid, otherwise an OpenFlow error code. */
6338 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
6346 case OFPP_CONTROLLER:
6351 return OFPERR_OFPBAC_BAD_OUT_PORT;
6354 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
6357 return OFPERR_OFPBAC_BAD_OUT_PORT;
6361 /* Removes the protocols that require consistency between match and actions
6362 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
6364 * (An example of an inconsistency between match and actions is a flow that
6365 * does not match on an MPLS Ethertype but has an action that pops an MPLS
6368 inconsistent_match(enum ofputil_protocol *usable_protocols)
6370 *usable_protocols &= OFPUTIL_P_OF10_ANY;
6373 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
6374 * caller must restore them.
6376 * Modifies some actions, filling in fields that could not be properly set
6377 * without context. */
6379 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
6380 struct flow *flow, ofp_port_t max_ports,
6381 uint8_t table_id, uint8_t n_tables)
6383 const struct ofpact_enqueue *enqueue;
6384 const struct mf_field *mf;
6388 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
6391 case OFPACT_CONTROLLER:
6394 case OFPACT_ENQUEUE:
6395 enqueue = ofpact_get_ENQUEUE(a);
6396 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
6397 && enqueue->port != OFPP_IN_PORT
6398 && enqueue->port != OFPP_LOCAL) {
6399 return OFPERR_OFPBAC_BAD_OUT_PORT;
6403 case OFPACT_OUTPUT_REG:
6404 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
6407 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
6409 case OFPACT_SET_VLAN_VID:
6410 /* Remember if we saw a vlan tag in the flow to aid translating to
6411 * OpenFlow 1.1+ if need be. */
6412 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
6413 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6414 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6415 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
6416 inconsistent_match(usable_protocols);
6418 /* Temporary mark that we have a vlan tag. */
6419 flow->vlan_tci |= htons(VLAN_CFI);
6422 case OFPACT_SET_VLAN_PCP:
6423 /* Remember if we saw a vlan tag in the flow to aid translating to
6424 * OpenFlow 1.1+ if need be. */
6425 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
6426 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6427 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6428 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
6429 inconsistent_match(usable_protocols);
6431 /* Temporary mark that we have a vlan tag. */
6432 flow->vlan_tci |= htons(VLAN_CFI);
6435 case OFPACT_STRIP_VLAN:
6436 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
6437 inconsistent_match(usable_protocols);
6439 /* Temporary mark that we have no vlan tag. */
6440 flow->vlan_tci = htons(0);
6443 case OFPACT_PUSH_VLAN:
6444 if (flow->vlan_tci & htons(VLAN_CFI)) {
6445 /* Multiple VLAN headers not supported. */
6446 return OFPERR_OFPBAC_BAD_TAG;
6448 /* Temporary mark that we have a vlan tag. */
6449 flow->vlan_tci |= htons(VLAN_CFI);
6452 case OFPACT_SET_ETH_SRC:
6453 case OFPACT_SET_ETH_DST:
6456 case OFPACT_SET_IPV4_SRC:
6457 case OFPACT_SET_IPV4_DST:
6458 if (flow->dl_type != htons(ETH_TYPE_IP)) {
6459 inconsistent_match(usable_protocols);
6463 case OFPACT_SET_IP_DSCP:
6464 case OFPACT_SET_IP_ECN:
6465 case OFPACT_SET_IP_TTL:
6466 case OFPACT_DEC_TTL:
6467 if (!is_ip_any(flow)) {
6468 inconsistent_match(usable_protocols);
6472 case OFPACT_SET_L4_SRC_PORT:
6473 case OFPACT_SET_L4_DST_PORT:
6474 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
6475 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
6476 && flow->nw_proto != IPPROTO_SCTP)) {
6477 inconsistent_match(usable_protocols);
6479 /* Note on which transport protocol the port numbers are set.
6480 * This allows this set action to be converted to an OF1.2 set field
6482 if (a->type == OFPACT_SET_L4_SRC_PORT) {
6483 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
6485 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
6489 case OFPACT_REG_MOVE:
6490 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
6492 case OFPACT_SET_FIELD:
6493 mf = ofpact_get_SET_FIELD(a)->field;
6494 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6495 if (!mf_are_prereqs_ok(mf, flow) ||
6496 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
6497 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
6499 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6501 /* Remember if we saw a vlan tag in the flow to aid translating to
6502 * OpenFlow 1.1 if need be. */
6503 ofpact_get_SET_FIELD(a)->flow_has_vlan =
6504 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6505 if (mf->id == MFF_VLAN_TCI) {
6506 /* The set field may add or remove the vlan tag,
6507 * Mark the status temporarily. */
6508 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
6512 case OFPACT_STACK_PUSH:
6513 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
6515 case OFPACT_STACK_POP:
6516 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6518 case OFPACT_SET_MPLS_LABEL:
6519 case OFPACT_SET_MPLS_TC:
6520 case OFPACT_SET_MPLS_TTL:
6521 case OFPACT_DEC_MPLS_TTL:
6522 if (!eth_type_mpls(flow->dl_type)) {
6523 inconsistent_match(usable_protocols);
6527 case OFPACT_SET_TUNNEL:
6528 case OFPACT_SET_QUEUE:
6529 case OFPACT_POP_QUEUE:
6530 case OFPACT_RESUBMIT:
6533 case OFPACT_FIN_TIMEOUT:
6534 if (flow->nw_proto != IPPROTO_TCP) {
6535 inconsistent_match(usable_protocols);
6540 return learn_check(ofpact_get_LEARN(a), flow);
6542 case OFPACT_CONJUNCTION:
6545 case OFPACT_MULTIPATH:
6546 return multipath_check(ofpact_get_MULTIPATH(a), flow);
6552 case OFPACT_PUSH_MPLS:
6553 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6554 /* The packet is now MPLS and the MPLS payload is opaque.
6555 * Thus nothing can be assumed about the network protocol.
6556 * Temporarily mark that we have no nw_proto. */
6560 case OFPACT_POP_MPLS:
6561 if (!eth_type_mpls(flow->dl_type)) {
6562 inconsistent_match(usable_protocols);
6564 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6571 struct ofpact_conntrack *oc = ofpact_get_CT(a);
6574 if (!dl_type_is_ip_any(flow->dl_type)
6575 || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6576 inconsistent_match(usable_protocols);
6579 if (oc->zone_src.field) {
6580 return mf_check_src(&oc->zone_src, flow);
6583 err = ofpacts_check(oc->actions, ofpact_ct_get_action_len(oc),
6584 flow, max_ports, table_id, n_tables,
6590 struct ofpact_nat *on = ofpact_get_NAT(a);
6592 if (!dl_type_is_ip_any(flow->dl_type) ||
6593 (on->range_af == AF_INET && flow->dl_type != htons(ETH_TYPE_IP)) ||
6594 (on->range_af == AF_INET6
6595 && flow->dl_type != htons(ETH_TYPE_IPV6))) {
6596 inconsistent_match(usable_protocols);
6601 case OFPACT_CLEAR_ACTIONS:
6604 case OFPACT_WRITE_ACTIONS: {
6605 /* Use a temporary copy of 'usable_protocols' because we can't check
6606 * consistency of an action set. */
6607 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6608 enum ofputil_protocol p = *usable_protocols;
6609 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6610 flow, max_ports, table_id, n_tables, &p);
6613 case OFPACT_WRITE_METADATA:
6616 case OFPACT_METER: {
6617 uint32_t mid = ofpact_get_METER(a)->meter_id;
6618 if (mid == 0 || mid > OFPM13_MAX) {
6619 return OFPERR_OFPMMFC_INVALID_METER;
6624 case OFPACT_GOTO_TABLE: {
6625 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
6626 if ((table_id != 255 && goto_table <= table_id)
6627 || (n_tables != 255 && goto_table >= n_tables)) {
6628 return OFPERR_OFPBIC_BAD_TABLE_ID;
6636 case OFPACT_UNROLL_XLATE:
6637 /* UNROLL is an internal action that should never be seen via
6639 return OFPERR_OFPBAC_BAD_TYPE;
6641 case OFPACT_DEBUG_RECIRC:
6649 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6650 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6651 * switch with no more than 'max_ports' ports.
6653 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6654 * '*usable_protocols' the protocols that forbid the inconsistency. (An
6655 * example of an inconsistency between match and actions is a flow that does
6656 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6658 * May annotate ofpacts with information gathered from the 'flow'.
6660 * May temporarily modify 'flow', but restores the changes before returning. */
6662 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
6663 struct flow *flow, ofp_port_t max_ports,
6664 uint8_t table_id, uint8_t n_tables,
6665 enum ofputil_protocol *usable_protocols)
6668 ovs_be16 dl_type = flow->dl_type;
6669 ovs_be16 vlan_tci = flow->vlan_tci;
6670 uint8_t nw_proto = flow->nw_proto;
6671 enum ofperr error = 0;
6673 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6674 error = ofpact_check__(usable_protocols, a, flow,
6675 max_ports, table_id, n_tables);
6680 /* Restore fields that may have been modified. */
6681 flow->dl_type = dl_type;
6682 flow->vlan_tci = vlan_tci;
6683 flow->nw_proto = nw_proto;
6687 /* Like ofpacts_check(), but reports inconsistencies as
6688 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6690 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
6691 struct flow *flow, ofp_port_t max_ports,
6692 uint8_t table_id, uint8_t n_tables,
6693 enum ofputil_protocol usable_protocols)
6695 enum ofputil_protocol p = usable_protocols;
6698 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
6699 table_id, n_tables, &p);
6700 return (error ? error
6701 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
6705 /* Returns the destination field that 'ofpact' would write to, or NULL
6706 * if the action would not write to an mf_field. */
6707 const struct mf_field *
6708 ofpact_get_mf_dst(const struct ofpact *ofpact)
6710 if (ofpact->type == OFPACT_SET_FIELD) {
6711 const struct ofpact_set_field *orl;
6713 orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
6715 } else if (ofpact->type == OFPACT_REG_MOVE) {
6716 const struct ofpact_reg_move *orm;
6718 orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
6719 return orm->dst.field;
6726 unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action)
6728 VLOG_WARN("%s action doesn't support nested action %s",
6729 ofpact_name(outer_action), ofpact_name(action));
6730 return OFPERR_OFPBAC_BAD_ARGUMENT;
6734 field_requires_ct(enum mf_field_id field)
6736 return field == MFF_CT_MARK || field == MFF_CT_LABEL;
6739 /* Apply nesting constraints for actions */
6741 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
6743 const struct mf_field *field = ofpact_get_mf_dst(a);
6745 if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
6746 VLOG_WARN("cannot set CT fields outside of ct action");
6747 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6749 if (a->type == OFPACT_NAT) {
6750 if (outer_action != OFPACT_CT) {
6751 VLOG_WARN("Cannot have NAT action outside of \"ct\" action");
6752 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6758 ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
6759 || outer_action == OFPACT_CT);
6761 if (outer_action == OFPACT_CT) {
6763 return unsupported_nesting(a->type, outer_action);
6764 } else if (!field_requires_ct(field->id)) {
6765 VLOG_WARN("%s action doesn't support nested modification "
6766 "of %s", ofpact_name(outer_action), field->name);
6767 return OFPERR_OFPBAC_BAD_ARGUMENT;
6775 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
6776 * appropriate order as defined by the OpenFlow spec and as required by Open
6779 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
6780 * instructions that are allowed within 'ofpacts[]'.
6782 * If 'outer_action' is not zero, it specifies that the actions are nested
6783 * within another action of type 'outer_action'. */
6785 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
6786 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
6788 const struct ofpact *a;
6789 enum ovs_instruction_type inst;
6791 inst = OVSINST_OFPIT13_METER;
6792 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6793 enum ovs_instruction_type next;
6796 if (a->type == OFPACT_CONJUNCTION) {
6797 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6798 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
6799 VLOG_WARN("\"conjunction\" actions may be used along with "
6800 "\"note\" but not any other kind of action "
6801 "(such as the \"%s\" action used here)",
6802 ofpact_name(a->type));
6803 return OFPERR_NXBAC_BAD_CONJUNCTION;
6809 error = ofpacts_verify_nested(a, outer_action);
6814 next = ovs_instruction_type_from_ofpact_type(a->type);
6816 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
6819 const char *name = ovs_instruction_name_from_type(inst);
6820 const char *next_name = ovs_instruction_name_from_type(next);
6823 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
6824 "1.1+ compatibility", name);
6826 VLOG_WARN("invalid instruction ordering: %s must appear "
6827 "before %s, for OpenFlow 1.1+ compatibility",
6830 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
6832 if (!((1u << next) & allowed_ovsinsts)) {
6833 const char *name = ovs_instruction_name_from_type(next);
6835 VLOG_WARN("%s instruction not allowed here", name);
6836 return OFPERR_OFPBIC_UNSUP_INST;
6845 /* Converting ofpacts to OpenFlow. */
6848 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
6852 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
6853 case OFPACT_##ENUM: \
6854 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
6863 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
6864 * actions in 'openflow', appending the actions to any existing data in
6867 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
6868 struct ofpbuf *openflow,
6869 enum ofp_version ofp_version)
6871 const struct ofpact *a;
6872 size_t start_size = openflow->size;
6874 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6875 encode_ofpact(a, ofp_version, openflow);
6877 return openflow->size - start_size;
6880 static enum ovs_instruction_type
6881 ofpact_is_apply_actions(const struct ofpact *a)
6883 return (ovs_instruction_type_from_ofpact_type(a->type)
6884 == OVSINST_OFPIT11_APPLY_ACTIONS);
6888 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
6890 struct ofpbuf *openflow,
6891 enum ofp_version ofp_version)
6893 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
6894 const struct ofpact *a;
6896 if (ofp_version == OFP10_VERSION) {
6897 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
6904 if (ofpact_is_apply_actions(a)) {
6905 size_t ofs = openflow->size;
6907 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
6909 encode_ofpact(a, ofp_version, openflow);
6911 } while (a < end && ofpact_is_apply_actions(a));
6912 ofpacts_update_instruction_actions(openflow, ofs);
6914 encode_ofpact(a, ofp_version, openflow);
6920 /* Sets of supported actions. */
6922 /* Two-way translation between OVS's internal "OFPACT_*" representation of
6923 * actions and the "OFPAT_*" representation used in some OpenFlow version.
6924 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
6925 * instance is specific to one OpenFlow version.) */
6927 enum ofpact_type ofpact; /* Internal name for action type. */
6928 int ofpat; /* OFPAT_* number from OpenFlow spec. */
6931 static const struct ofpact_map *
6932 get_ofpact_map(enum ofp_version version)
6934 /* OpenFlow 1.0 actions. */
6935 static const struct ofpact_map of10[] = {
6936 { OFPACT_OUTPUT, 0 },
6937 { OFPACT_SET_VLAN_VID, 1 },
6938 { OFPACT_SET_VLAN_PCP, 2 },
6939 { OFPACT_STRIP_VLAN, 3 },
6940 { OFPACT_SET_ETH_SRC, 4 },
6941 { OFPACT_SET_ETH_DST, 5 },
6942 { OFPACT_SET_IPV4_SRC, 6 },
6943 { OFPACT_SET_IPV4_DST, 7 },
6944 { OFPACT_SET_IP_DSCP, 8 },
6945 { OFPACT_SET_L4_SRC_PORT, 9 },
6946 { OFPACT_SET_L4_DST_PORT, 10 },
6947 { OFPACT_ENQUEUE, 11 },
6951 /* OpenFlow 1.1 actions. */
6952 static const struct ofpact_map of11[] = {
6953 { OFPACT_OUTPUT, 0 },
6954 { OFPACT_SET_VLAN_VID, 1 },
6955 { OFPACT_SET_VLAN_PCP, 2 },
6956 { OFPACT_SET_ETH_SRC, 3 },
6957 { OFPACT_SET_ETH_DST, 4 },
6958 { OFPACT_SET_IPV4_SRC, 5 },
6959 { OFPACT_SET_IPV4_DST, 6 },
6960 { OFPACT_SET_IP_DSCP, 7 },
6961 { OFPACT_SET_IP_ECN, 8 },
6962 { OFPACT_SET_L4_SRC_PORT, 9 },
6963 { OFPACT_SET_L4_DST_PORT, 10 },
6964 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6965 /* OFPAT_COPY_TTL_IN (12) not supported. */
6966 { OFPACT_SET_MPLS_LABEL, 13 },
6967 { OFPACT_SET_MPLS_TC, 14 },
6968 { OFPACT_SET_MPLS_TTL, 15 },
6969 { OFPACT_DEC_MPLS_TTL, 16 },
6970 { OFPACT_PUSH_VLAN, 17 },
6971 { OFPACT_STRIP_VLAN, 18 },
6972 { OFPACT_PUSH_MPLS, 19 },
6973 { OFPACT_POP_MPLS, 20 },
6974 { OFPACT_SET_QUEUE, 21 },
6975 { OFPACT_GROUP, 22 },
6976 { OFPACT_SET_IP_TTL, 23 },
6977 { OFPACT_DEC_TTL, 24 },
6981 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
6982 static const struct ofpact_map of12[] = {
6983 { OFPACT_OUTPUT, 0 },
6984 /* OFPAT_COPY_TTL_OUT (11) not supported. */
6985 /* OFPAT_COPY_TTL_IN (12) not supported. */
6986 { OFPACT_SET_MPLS_TTL, 15 },
6987 { OFPACT_DEC_MPLS_TTL, 16 },
6988 { OFPACT_PUSH_VLAN, 17 },
6989 { OFPACT_STRIP_VLAN, 18 },
6990 { OFPACT_PUSH_MPLS, 19 },
6991 { OFPACT_POP_MPLS, 20 },
6992 { OFPACT_SET_QUEUE, 21 },
6993 { OFPACT_GROUP, 22 },
6994 { OFPACT_SET_IP_TTL, 23 },
6995 { OFPACT_DEC_TTL, 24 },
6996 { OFPACT_SET_FIELD, 25 },
6997 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
6998 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
7018 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
7019 * values, into a bitmap of actions suitable for OpenFlow 'version', and
7020 * returns the result. */
7022 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
7024 uint32_t openflow_bitmap = 0;
7025 const struct ofpact_map *x;
7027 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7028 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
7029 openflow_bitmap |= 1u << x->ofpat;
7032 return htonl(openflow_bitmap);
7035 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
7036 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
7037 * and returns the result. */
7039 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
7041 uint64_t ofpact_bitmap = 0;
7042 const struct ofpact_map *x;
7044 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7045 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
7046 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
7049 return ofpact_bitmap;
7052 /* Appends to 's' a string representation of the set of OFPACT_* represented
7053 * by 'ofpacts_bitmap'. */
7055 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
7057 if (!ofpacts_bitmap) {
7058 ds_put_cstr(s, "<none>");
7060 while (ofpacts_bitmap) {
7061 ds_put_format(s, "%s ",
7062 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
7063 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
7069 /* Returns true if 'action' outputs to 'port', false otherwise. */
7071 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
7073 switch (ofpact->type) {
7075 return ofpact_get_OUTPUT(ofpact)->port == port;
7076 case OFPACT_ENQUEUE:
7077 return ofpact_get_ENQUEUE(ofpact)->port == port;
7078 case OFPACT_CONTROLLER:
7079 return port == OFPP_CONTROLLER;
7081 case OFPACT_OUTPUT_REG:
7083 case OFPACT_SET_VLAN_VID:
7084 case OFPACT_SET_VLAN_PCP:
7085 case OFPACT_STRIP_VLAN:
7086 case OFPACT_PUSH_VLAN:
7087 case OFPACT_SET_ETH_SRC:
7088 case OFPACT_SET_ETH_DST:
7089 case OFPACT_SET_IPV4_SRC:
7090 case OFPACT_SET_IPV4_DST:
7091 case OFPACT_SET_IP_DSCP:
7092 case OFPACT_SET_IP_ECN:
7093 case OFPACT_SET_IP_TTL:
7094 case OFPACT_SET_L4_SRC_PORT:
7095 case OFPACT_SET_L4_DST_PORT:
7096 case OFPACT_REG_MOVE:
7097 case OFPACT_SET_FIELD:
7098 case OFPACT_STACK_PUSH:
7099 case OFPACT_STACK_POP:
7100 case OFPACT_DEC_TTL:
7101 case OFPACT_SET_MPLS_LABEL:
7102 case OFPACT_SET_MPLS_TC:
7103 case OFPACT_SET_MPLS_TTL:
7104 case OFPACT_DEC_MPLS_TTL:
7105 case OFPACT_SET_TUNNEL:
7106 case OFPACT_WRITE_METADATA:
7107 case OFPACT_SET_QUEUE:
7108 case OFPACT_POP_QUEUE:
7109 case OFPACT_FIN_TIMEOUT:
7110 case OFPACT_RESUBMIT:
7112 case OFPACT_CONJUNCTION:
7113 case OFPACT_MULTIPATH:
7116 case OFPACT_UNROLL_XLATE:
7117 case OFPACT_PUSH_MPLS:
7118 case OFPACT_POP_MPLS:
7120 case OFPACT_CLEAR_ACTIONS:
7121 case OFPACT_WRITE_ACTIONS:
7122 case OFPACT_GOTO_TABLE:
7125 case OFPACT_DEBUG_RECIRC:
7133 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7134 * to 'port', false otherwise. */
7136 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
7139 const struct ofpact *a;
7141 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
7142 if (ofpact_outputs_to_port(a, port)) {
7150 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7151 * to 'group', false otherwise. */
7153 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
7156 const struct ofpact *a;
7158 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
7159 if (a->type == OFPACT_GROUP
7160 && ofpact_get_GROUP(a)->group_id == group_id) {
7169 ofpacts_equal(const struct ofpact *a, size_t a_len,
7170 const struct ofpact *b, size_t b_len)
7172 return a_len == b_len && !memcmp(a, b, a_len);
7175 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
7176 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
7178 * This function relies on the order of 'ofpacts' being correct (as checked by
7179 * ofpacts_verify()). */
7181 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
7183 const struct ofpact *a;
7185 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7186 enum ovs_instruction_type inst;
7188 inst = ovs_instruction_type_from_ofpact_type(a->type);
7189 if (a->type == OFPACT_METER) {
7190 return ofpact_get_METER(a)->meter_id;
7191 } else if (inst > OVSINST_OFPIT13_METER) {
7199 /* Formatting ofpacts. */
7202 ofpact_format(const struct ofpact *a, struct ds *s)
7205 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7206 case OFPACT_##ENUM: \
7207 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
7216 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
7217 * 'ofpacts' to 'string'. */
7219 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
7223 ds_put_cstr(string, "drop");
7225 const struct ofpact *a;
7227 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7229 ds_put_cstr(string, ",");
7232 /* XXX write-actions */
7233 ofpact_format(a, string);
7238 /* Internal use by helpers. */
7241 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
7243 struct ofpact *ofpact;
7245 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
7246 ofpact = ofpacts->header;
7247 ofpact_init(ofpact, type, len);
7252 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
7254 memset(ofpact, 0, len);
7255 ofpact->type = type;
7260 /* Finishes composing a variable-length action (begun using
7261 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
7262 * bytes and updating its embedded length field. See the large comment near
7263 * the end of ofp-actions.h for more information. */
7265 ofpact_finish(struct ofpbuf *ofpacts, struct ofpact *ofpact)
7267 ovs_assert(ofpact == ofpacts->header);
7268 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
7269 ofpbuf_padto(ofpacts, OFPACT_ALIGN(ofpacts->size));
7272 static char * OVS_WARN_UNUSED_RESULT
7273 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
7274 enum ofputil_protocol *usable_protocols)
7277 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7278 case OFPACT_##ENUM: \
7279 return parse_##ENUM(value, ofpacts, usable_protocols);
7288 ofpact_type_from_name(const char *name, enum ofpact_type *type)
7290 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7291 if (!strcasecmp(name, NAME)) { \
7292 *type = OFPACT_##ENUM; \
7301 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
7303 * Returns NULL if successful, otherwise a malloc()'d string describing the
7304 * error. The caller is responsible for freeing the returned string.
7306 * If 'outer_action' is specified, indicates that the actions being parsed
7307 * are nested within another action of the type specified in 'outer_action'. */
7308 static char * OVS_WARN_UNUSED_RESULT
7309 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
7310 enum ofputil_protocol *usable_protocols,
7311 bool allow_instructions, enum ofpact_type outer_action)
7320 while (ofputil_parse_key_value(&pos, &key, &value)) {
7321 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
7322 enum ofpact_type type;
7326 if (ofpact_type_from_name(key, &type)) {
7327 error = ofpact_parse(type, value, ofpacts, usable_protocols);
7328 inst = ovs_instruction_type_from_ofpact_type(type);
7329 } else if (!strcasecmp(key, "mod_vlan_vid")) {
7330 error = parse_set_vlan_vid(value, ofpacts, true);
7331 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
7332 error = parse_set_vlan_pcp(value, ofpacts, true);
7333 } else if (!strcasecmp(key, "set_nw_ttl")) {
7334 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
7335 } else if (!strcasecmp(key, "pop_vlan")) {
7336 error = parse_pop_vlan(ofpacts);
7337 } else if (!strcasecmp(key, "set_tunnel64")) {
7338 error = parse_set_tunnel(value, ofpacts,
7339 NXAST_RAW_SET_TUNNEL64);
7340 } else if (!strcasecmp(key, "load")) {
7341 error = parse_reg_load(value, ofpacts);
7342 } else if (!strcasecmp(key, "bundle_load")) {
7343 error = parse_bundle_load(value, ofpacts);
7344 } else if (!strcasecmp(key, "drop")) {
7346 } else if (!strcasecmp(key, "apply_actions")) {
7347 return xstrdup("apply_actions is the default instruction");
7348 } else if (ofputil_port_from_string(key, &port)) {
7349 ofpact_put_OUTPUT(ofpacts)->port = port;
7351 return xasprintf("unknown action %s", key);
7357 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
7358 if (!allow_instructions) {
7359 return xasprintf("only actions are allowed here (not "
7361 ovs_instruction_name_from_type(inst));
7363 if (inst == prev_inst) {
7364 return xasprintf("instruction %s may be specified only once",
7365 ovs_instruction_name_from_type(inst));
7368 if (prev_inst != -1 && inst < prev_inst) {
7369 return xasprintf("instruction %s must be specified before %s",
7370 ovs_instruction_name_from_type(inst),
7371 ovs_instruction_name_from_type(prev_inst));
7376 if (drop && ofpacts->size) {
7377 return xstrdup("\"drop\" must not be accompanied by any other action "
7381 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
7383 ? (1u << N_OVS_INSTRUCTIONS) - 1
7384 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
7387 return xstrdup("Incorrect instruction ordering");
7393 static char * OVS_WARN_UNUSED_RESULT
7394 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
7395 enum ofputil_protocol *usable_protocols, bool allow_instructions,
7396 enum ofpact_type outer_action)
7398 uint32_t orig_size = ofpacts->size;
7399 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
7400 allow_instructions, outer_action);
7402 ofpacts->size = orig_size;
7407 static char * OVS_WARN_UNUSED_RESULT
7408 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
7409 enum ofputil_protocol *usable_protocols,
7410 bool allow_instructions, enum ofpact_type outer_action)
7414 *usable_protocols = OFPUTIL_P_ANY;
7417 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
7424 /* Parses 's' as a set of OpenFlow actions and appends the actions to
7425 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
7426 * that are nested within the action of type 'outer_action'.
7428 * Returns NULL if successful, otherwise a malloc()'d string describing the
7429 * error. The caller is responsible for freeing the returned string. */
7430 char * OVS_WARN_UNUSED_RESULT
7431 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
7432 enum ofputil_protocol *usable_protocols)
7434 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
7437 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
7440 * Returns NULL if successful, otherwise a malloc()'d string describing the
7441 * error. The caller is responsible for freeing the returned string. */
7442 char * OVS_WARN_UNUSED_RESULT
7443 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
7444 enum ofputil_protocol *usable_protocols)
7446 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
7450 ofpact_name(enum ofpact_type type)
7453 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7460 /* Low-level action decoding and encoding functions. */
7462 /* Everything needed to identify a particular OpenFlow action. */
7463 struct ofpact_hdrs {
7464 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
7465 uint16_t type; /* Type if standard, otherwise subtype. */
7466 uint8_t ofp_version; /* From ofp_header. */
7469 /* Information about a particular OpenFlow action. */
7470 struct ofpact_raw_instance {
7471 /* The action's identity. */
7472 struct ofpact_hdrs hdrs;
7473 enum ofp_raw_action_type raw;
7475 /* Looking up the action. */
7476 struct hmap_node decode_node; /* Based on 'hdrs'. */
7477 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
7479 /* The action's encoded size.
7481 * If this action is fixed-length, 'min_length' == 'max_length'.
7482 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7483 * OFP_ACTION_ALIGN) == 65528. */
7484 unsigned short int min_length;
7485 unsigned short int max_length;
7487 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7488 * offset of that argument from the beginning of the action and 'arg_len'
7489 * its length, both in bytes.
7491 * For actions that take other forms, these are both zero. */
7492 unsigned short int arg_ofs;
7493 unsigned short int arg_len;
7495 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7498 /* If this action is deprecated, a human-readable string with a brief
7500 const char *deprecation;
7503 /* Action header. */
7504 struct ofp_action_header {
7505 /* The meaning of other values of 'type' generally depends on the OpenFlow
7506 * version (see enum ofp_raw_action_type).
7508 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7509 * designates an OpenFlow vendor ID and that the remainder of the action
7510 * structure has a vendor-defined meaning.
7512 #define OFPAT_VENDOR 0xffff
7515 /* Always a multiple of 8. */
7518 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7519 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
7522 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
7524 /* Header for Nicira-defined actions and for ONF vendor extensions.
7526 * This cannot be used as an entirely generic vendor extension action header,
7527 * because OpenFlow does not specify the location or size of the action
7528 * subtype; it just happens that ONF extensions and Nicira extensions share
7530 struct ext_action_header {
7531 ovs_be16 type; /* OFPAT_VENDOR. */
7532 ovs_be16 len; /* At least 16. */
7533 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
7534 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
7537 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
7540 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
7541 const struct ofpact_hdrs *b)
7543 return (a->vendor == b->vendor
7544 && a->type == b->type
7545 && a->ofp_version == b->ofp_version);
7549 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
7551 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
7554 #include "ofp-actions.inc2"
7556 static struct hmap *
7557 ofpact_decode_hmap(void)
7559 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7560 static struct hmap hmap;
7562 if (ovsthread_once_start(&once)) {
7563 struct ofpact_raw_instance *inst;
7566 for (inst = all_raw_instances;
7567 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7569 hmap_insert(&hmap, &inst->decode_node,
7570 ofpact_hdrs_hash(&inst->hdrs));
7572 ovsthread_once_done(&once);
7577 static struct hmap *
7578 ofpact_encode_hmap(void)
7580 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7581 static struct hmap hmap;
7583 if (ovsthread_once_start(&once)) {
7584 struct ofpact_raw_instance *inst;
7587 for (inst = all_raw_instances;
7588 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7590 hmap_insert(&hmap, &inst->encode_node,
7591 hash_2words(inst->raw, inst->hdrs.ofp_version));
7593 ovsthread_once_done(&once);
7599 ofpact_decode_raw(enum ofp_version ofp_version,
7600 const struct ofp_action_header *oah, size_t length,
7601 const struct ofpact_raw_instance **instp)
7603 const struct ofpact_raw_instance *inst;
7604 struct ofpact_hdrs hdrs;
7607 if (length < sizeof *oah) {
7608 return OFPERR_OFPBAC_BAD_LEN;
7611 /* Get base action type. */
7612 if (oah->type == htons(OFPAT_VENDOR)) {
7614 hdrs.vendor = ntohl(oah->vendor);
7615 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
7616 /* Get extension subtype. */
7617 const struct ext_action_header *nah;
7619 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
7620 if (length < sizeof *nah) {
7621 return OFPERR_OFPBAC_BAD_LEN;
7623 hdrs.type = ntohs(nah->subtype);
7625 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
7627 return OFPERR_OFPBAC_BAD_VENDOR;
7631 hdrs.type = ntohs(oah->type);
7634 hdrs.ofp_version = ofp_version;
7635 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
7636 ofpact_decode_hmap()) {
7637 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
7644 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7645 : OFPERR_OFPBAC_BAD_TYPE);
7649 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7650 enum ofp_raw_action_type *raw, uint64_t *arg)
7652 const struct ofp_action_header *oah = buf->data;
7653 const struct ofpact_raw_instance *action;
7654 unsigned int length;
7658 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
7663 if (action->deprecation) {
7664 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
7665 action->name, ofputil_version_to_string(ofp_version),
7666 action->deprecation);
7669 length = ntohs(oah->len);
7670 if (length > buf->size) {
7671 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
7672 "length %"PRIu32, action->name, length, buf->size);
7673 return OFPERR_OFPBAC_BAD_LEN;
7675 if (length < action->min_length || length > action->max_length) {
7676 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
7677 "[%hu,%hu]", action->name, length,
7678 action->min_length, action->max_length);
7679 return OFPERR_OFPBAC_BAD_LEN;
7682 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
7683 "of 8", action->name, length);
7684 return OFPERR_OFPBAC_BAD_LEN;
7689 if (action->arg_len) {
7693 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
7694 for (i = 0; i < action->arg_len; i++) {
7695 *arg = (*arg << 8) | p[i];
7699 ofpbuf_pull(buf, length);
7704 static const struct ofpact_raw_instance *
7705 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
7707 const struct ofpact_raw_instance *inst;
7709 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
7710 ofpact_encode_hmap()) {
7711 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
7719 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7720 enum ofp_raw_action_type raw, uint64_t arg)
7722 const struct ofpact_raw_instance *inst;
7723 struct ofp_action_header *oah;
7724 const struct ofpact_hdrs *hdrs;
7726 inst = ofpact_raw_lookup(ofp_version, raw);
7729 oah = ofpbuf_put_zeros(buf, inst->min_length);
7730 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
7731 oah->len = htons(inst->min_length);
7732 oah->vendor = htonl(hdrs->vendor);
7734 switch (hdrs->vendor) {
7739 case ONF_VENDOR_ID: {
7740 struct ext_action_header *nah = (struct ext_action_header *) oah;
7741 nah->subtype = htons(hdrs->type);
7749 if (inst->arg_len) {
7750 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
7753 for (i = 0; i < inst->arg_len; i++) {
7765 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
7767 struct ofp_action_header *oah;
7769 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs,
7772 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
7773 oah->len = htons(openflow->size - start_ofs);