2 * Copyright (c) 2008-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>
21 #include "byte-order.h"
27 #include "multipath.h"
29 #include "odp-netlink.h"
30 #include "openvswitch/dynamic-string.h"
31 #include "openvswitch/meta-flow.h"
32 #include "openvswitch/ofp-actions.h"
33 #include "openvswitch/ofp-util.h"
34 #include "openvswitch/ofp-parse.h"
35 #include "openvswitch/ofp-prop.h"
36 #include "openvswitch/ofpbuf.h"
37 #include "openvswitch/vlog.h"
38 #include "unaligned.h"
41 VLOG_DEFINE_THIS_MODULE(ofp_actions);
43 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
45 struct ofp_action_header;
47 /* Raw identifiers for OpenFlow actions.
49 * Decoding and encoding OpenFlow actions across multiple versions is difficult
50 * to do in a clean, consistent way. This enumeration lays out all of the
51 * forms of actions that Open vSwitch supports.
53 * The comments here must follow a stylized form because the
54 * "extract-ofp-actions" program parses them at build time to generate data
57 * - The first part of each comment specifies the vendor, OpenFlow versions,
58 * and type for each protocol that supports the action:
60 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
61 * extension actions. (Support for other vendors can be added, but
62 * it can't be done just based on a vendor ID definition alone
63 * because OpenFlow doesn't define a standard way to specify a
64 * subtype for vendor actions, so other vendors might do it different
67 * # The version can specify a specific OpenFlow version, a version
68 * range delimited by "-", or an open-ended range with "+".
70 * # The type, in parentheses, is the action type number (for standard
71 * OpenFlow actions) or subtype (for vendor extension actions).
73 * # Optionally one may add "is deprecated" followed by a
74 * human-readable reason in parentheses (which will be used in log
75 * messages), if a particular action should no longer be used.
77 * Multiple such specifications may be separated by commas.
79 * - The second part describes the action's wire format. It may be:
81 * # "struct <name>": The struct fully specifies the wire format. The
82 * action is exactly the size of the struct. (Thus, the struct must
83 * be an exact multiple of 8 bytes in size.)
85 * # "struct <name>, ...": The struct specifies the beginning of the
86 * wire format. An instance of the action is either the struct's
87 * exact size, or a multiple of 8 bytes longer.
89 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
90 * vendor extension) header, followed by 0 or more pad bytes to align
91 * to a multiple of <N> bits, followed by an argument of the given
92 * type, followed by 0 or more pad bytes to bring the total action up
93 * to a multiple of 8 bytes.
95 * # "void": The action is just a (standard or vendor extension)
98 * - Optional additional text enclosed in square brackets is commentary for
101 enum ofp_raw_action_type {
102 /* ## ----------------- ## */
103 /* ## Standard actions. ## */
104 /* ## ----------------- ## */
106 /* OF1.0(0): struct ofp10_action_output. */
108 /* OF1.1+(0): struct ofp11_action_output. */
111 /* OF1.0(1): uint16_t. */
112 OFPAT_RAW10_SET_VLAN_VID,
113 /* OF1.0(2): uint8_t. */
114 OFPAT_RAW10_SET_VLAN_PCP,
116 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
118 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
119 * modification actions: the 1.0 versions push a VLAN header if none is
120 * present, but the 1.1 versions do not. That is the only reason that we
121 * distinguish their raw action types.] */
122 OFPAT_RAW11_SET_VLAN_VID,
123 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
124 OFPAT_RAW11_SET_VLAN_PCP,
126 /* OF1.1+(17): ovs_be16.
128 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
130 OFPAT_RAW11_PUSH_VLAN,
132 /* OF1.0(3): void. */
133 OFPAT_RAW10_STRIP_VLAN,
134 /* OF1.1+(18): void. */
135 OFPAT_RAW11_POP_VLAN,
137 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
138 * ofp_action_dl_addr. */
139 OFPAT_RAW_SET_DL_SRC,
141 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
142 * ofp_action_dl_addr. */
143 OFPAT_RAW_SET_DL_DST,
145 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
147 OFPAT_RAW_SET_NW_SRC,
149 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
151 OFPAT_RAW_SET_NW_DST,
153 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
154 OFPAT_RAW_SET_NW_TOS,
156 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
157 OFPAT_RAW11_SET_NW_ECN,
159 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
161 OFPAT_RAW_SET_TP_SRC,
163 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
165 OFPAT_RAW_SET_TP_DST,
167 /* OF1.0(11): struct ofp10_action_enqueue. */
170 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
172 OFPAT_RAW_SET_MPLS_LABEL,
174 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
176 OFPAT_RAW_SET_MPLS_TC,
178 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
180 OFPAT_RAW_SET_MPLS_TTL,
182 /* NX1.0(26), OF1.1+(16): void. */
183 OFPAT_RAW_DEC_MPLS_TTL,
185 /* NX1.0(23), OF1.1+(19): ovs_be16.
187 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
190 /* NX1.0(24), OF1.1+(20): ovs_be16.
192 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
193 * ETH_TYPE_MPLS otherwise, not the label.] */
196 /* NX1.0(4), OF1.1+(21): uint32_t. */
199 /* OF1.1+(22): uint32_t. */
202 /* OF1.1+(23): uint8_t. */
203 OFPAT_RAW11_SET_NW_TTL,
205 /* NX1.0(18), OF1.1+(24): void. */
206 OFPAT_RAW_DEC_NW_TTL,
207 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
208 NXAST_RAW_DEC_TTL_CNT_IDS,
210 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
211 OFPAT_RAW12_SET_FIELD,
212 /* OF1.5+(25): struct ofp12_action_set_field, ... */
213 OFPAT_RAW15_SET_FIELD,
214 /* NX1.0-1.4(7): struct nx_action_reg_load.
216 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
217 * we drop reg_load.] */
219 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
221 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
222 * we drop reg_load2.] */
225 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
226 OFPAT_RAW15_COPY_FIELD,
227 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
228 ONFACT_RAW13_COPY_FIELD,
229 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
232 /* ## ------------------------- ## */
233 /* ## Nicira extension actions. ## */
234 /* ## ------------------------- ## */
236 /* Actions similar to standard actions are listed with the standard actions. */
238 /* NX1.0+(1): uint16_t. */
240 /* NX1.0+(14): struct nx_action_resubmit. */
241 NXAST_RAW_RESUBMIT_TABLE,
243 /* NX1.0+(2): uint32_t. */
244 NXAST_RAW_SET_TUNNEL,
245 /* NX1.0+(9): uint64_t. */
246 NXAST_RAW_SET_TUNNEL64,
248 /* NX1.0+(5): void. */
251 /* NX1.0+(8): struct nx_action_note, ... */
254 /* NX1.0+(10): struct nx_action_multipath. */
257 /* NX1.0+(12): struct nx_action_bundle, ... */
259 /* NX1.0+(13): struct nx_action_bundle, ... */
260 NXAST_RAW_BUNDLE_LOAD,
262 /* NX1.0+(15): struct nx_action_output_reg. */
263 NXAST_RAW_OUTPUT_REG,
264 /* NX1.0+(32): struct nx_action_output_reg2. */
265 NXAST_RAW_OUTPUT_REG2,
267 /* NX1.0+(16): struct nx_action_learn, ... */
270 /* NX1.0+(17): void. */
273 /* NX1.0+(19): struct nx_action_fin_timeout. */
274 NXAST_RAW_FIN_TIMEOUT,
276 /* NX1.0+(20): struct nx_action_controller. */
277 NXAST_RAW_CONTROLLER,
278 /* NX1.0+(37): struct nx_action_controller2, ... */
279 NXAST_RAW_CONTROLLER2,
281 /* NX1.0+(22): struct nx_action_write_metadata. */
282 NXAST_RAW_WRITE_METADATA,
284 /* NX1.0+(27): struct nx_action_stack. */
285 NXAST_RAW_STACK_PUSH,
287 /* NX1.0+(28): struct nx_action_stack. */
290 /* NX1.0+(29): struct nx_action_sample. */
293 /* NX1.0+(34): struct nx_action_conjunction. */
294 NXAST_RAW_CONJUNCTION,
296 /* NX1.0+(35): struct nx_action_conntrack, ... */
299 /* NX1.0+(36): struct nx_action_nat, ... */
302 /* ## ------------------ ## */
303 /* ## Debugging actions. ## */
304 /* ## ------------------ ## */
306 /* These are intentionally undocumented, subject to change, and ovs-vswitchd */
307 /* accepts them only if started with --enable-dummy. */
309 /* NX1.0+(255): void. */
310 NXAST_RAW_DEBUG_RECIRC,
313 /* OpenFlow actions are always a multiple of 8 bytes in length. */
314 #define OFP_ACTION_ALIGN 8
316 /* Define a few functions for working with instructions. */
317 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
318 static inline const struct STRUCT * OVS_UNUSED \
319 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
321 ovs_assert(inst->type == htons(ENUM)); \
322 return ALIGNED_CAST(struct STRUCT *, inst); \
325 static inline void OVS_UNUSED \
326 instruction_init_##ENUM(struct STRUCT *s) \
328 memset(s, 0, sizeof *s); \
329 s->type = htons(ENUM); \
330 s->len = htons(sizeof *s); \
333 static inline struct STRUCT * OVS_UNUSED \
334 instruction_put_##ENUM(struct ofpbuf *buf) \
336 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
337 instruction_init_##ENUM(s); \
343 static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
345 static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
347 static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
348 uint32_t allowed_ovsinsts,
349 enum ofpact_type outer_action);
351 static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
352 enum mf_field_id, uint64_t value);
354 static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
355 enum ofp_raw_action_type *, uint64_t *arg);
356 static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
357 enum ofp_raw_action_type, uint64_t arg);
359 static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
360 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
361 bool allow_instructions, enum ofpact_type outer_action);
362 static enum ofperr ofpacts_pull_openflow_actions__(
363 struct ofpbuf *openflow, unsigned int actions_len,
364 enum ofp_version version, uint32_t allowed_ovsinsts,
365 struct ofpbuf *ofpacts, enum ofpact_type outer_action);
366 static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
367 const char *s_, struct ofpbuf *ofpacts,
368 enum ofputil_protocol *usable_protocols,
369 bool allow_instructions, enum ofpact_type outer_action);
371 /* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
372 * nested ofpacts it returns the first one. */
374 ofpact_next_flattened(const struct ofpact *ofpact)
376 switch (ofpact->type) {
379 case OFPACT_CONTROLLER:
381 case OFPACT_OUTPUT_REG:
383 case OFPACT_SET_FIELD:
384 case OFPACT_SET_VLAN_VID:
385 case OFPACT_SET_VLAN_PCP:
386 case OFPACT_STRIP_VLAN:
387 case OFPACT_PUSH_VLAN:
388 case OFPACT_SET_ETH_SRC:
389 case OFPACT_SET_ETH_DST:
390 case OFPACT_SET_IPV4_SRC:
391 case OFPACT_SET_IPV4_DST:
392 case OFPACT_SET_IP_DSCP:
393 case OFPACT_SET_IP_ECN:
394 case OFPACT_SET_IP_TTL:
395 case OFPACT_SET_L4_SRC_PORT:
396 case OFPACT_SET_L4_DST_PORT:
397 case OFPACT_REG_MOVE:
398 case OFPACT_STACK_PUSH:
399 case OFPACT_STACK_POP:
401 case OFPACT_SET_MPLS_LABEL:
402 case OFPACT_SET_MPLS_TC:
403 case OFPACT_SET_MPLS_TTL:
404 case OFPACT_DEC_MPLS_TTL:
405 case OFPACT_PUSH_MPLS:
406 case OFPACT_POP_MPLS:
407 case OFPACT_SET_TUNNEL:
408 case OFPACT_SET_QUEUE:
409 case OFPACT_POP_QUEUE:
410 case OFPACT_FIN_TIMEOUT:
411 case OFPACT_RESUBMIT:
413 case OFPACT_CONJUNCTION:
414 case OFPACT_MULTIPATH:
418 case OFPACT_UNROLL_XLATE:
419 case OFPACT_DEBUG_RECIRC:
421 case OFPACT_CLEAR_ACTIONS:
422 case OFPACT_WRITE_METADATA:
423 case OFPACT_GOTO_TABLE:
425 return ofpact_next(ofpact);
428 return ofpact_get_CT(ofpact)->actions;
430 case OFPACT_WRITE_ACTIONS:
431 return ofpact_get_WRITE_ACTIONS(ofpact)->actions;
437 /* Pull off existing actions or instructions. Used by nesting actions to keep
438 * ofpacts_parse() oblivious of actions nesting.
440 * Push the actions back on after nested parsing, e.g.:
442 * size_t ofs = ofpacts_pull(ofpacts);
443 * ...nested parsing...
444 * ofpbuf_push_uninit(ofpacts, ofs);
447 ofpacts_pull(struct ofpbuf *ofpacts)
452 ofpbuf_pull(ofpacts, ofs);
457 #include "ofp-actions.inc1"
459 /* Output actions. */
461 /* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
462 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
463 * number of bytes to send. A 'max_len' of zero means no bytes of the
464 * packet should be sent. */
465 struct ofp10_action_output {
466 ovs_be16 type; /* OFPAT10_OUTPUT. */
467 ovs_be16 len; /* Length is 8. */
468 ovs_be16 port; /* Output port. */
469 ovs_be16 max_len; /* Max length to send to controller. */
471 OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
473 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
474 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
475 * number of bytes to send. A 'max_len' of zero means no bytes of the
476 * packet should be sent.*/
477 struct ofp11_action_output {
478 ovs_be16 type; /* OFPAT11_OUTPUT. */
479 ovs_be16 len; /* Length is 16. */
480 ovs_be32 port; /* Output port. */
481 ovs_be16 max_len; /* Max length to send to controller. */
482 uint8_t pad[6]; /* Pad to 64 bits. */
484 OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
487 decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
488 enum ofp_version ofp_version OVS_UNUSED,
491 struct ofpact_output *output;
493 output = ofpact_put_OUTPUT(out);
494 output->port = u16_to_ofp(ntohs(oao->port));
495 output->max_len = ntohs(oao->max_len);
497 return ofpact_check_output_port(output->port, OFPP_MAX);
501 decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
502 enum ofp_version ofp_version OVS_UNUSED,
505 struct ofpact_output *output;
508 output = ofpact_put_OUTPUT(out);
509 output->max_len = ntohs(oao->max_len);
511 error = ofputil_port_from_ofp11(oao->port, &output->port);
516 return ofpact_check_output_port(output->port, OFPP_MAX);
520 encode_OUTPUT(const struct ofpact_output *output,
521 enum ofp_version ofp_version, struct ofpbuf *out)
523 if (ofp_version == OFP10_VERSION) {
524 struct ofp10_action_output *oao;
526 oao = put_OFPAT10_OUTPUT(out);
527 oao->port = htons(ofp_to_u16(output->port));
528 oao->max_len = htons(output->max_len);
530 struct ofp11_action_output *oao;
532 oao = put_OFPAT11_OUTPUT(out);
533 oao->port = ofputil_port_to_ofp11(output->port);
534 oao->max_len = htons(output->max_len);
538 static char * OVS_WARN_UNUSED_RESULT
539 parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
540 enum ofputil_protocol *usable_protocols OVS_UNUSED)
542 if (strchr(arg, '[')) {
543 struct ofpact_output_reg *output_reg;
545 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
546 output_reg->max_len = UINT16_MAX;
547 return mf_parse_subfield(&output_reg->src, arg);
549 struct ofpact_output *output;
551 output = ofpact_put_OUTPUT(ofpacts);
552 if (!ofputil_port_from_string(arg, &output->port)) {
553 return xasprintf("%s: output to unknown port", arg);
555 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
561 format_OUTPUT(const struct ofpact_output *a, struct ds *s)
563 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
564 ds_put_format(s, "%soutput:%s%"PRIu16,
565 colors.special, colors.end, a->port);
567 ofputil_format_port(a->port, s);
568 if (a->port == OFPP_CONTROLLER) {
569 ds_put_format(s, ":%"PRIu16, a->max_len);
577 decode_OFPAT_RAW11_GROUP(uint32_t group_id,
578 enum ofp_version ofp_version OVS_UNUSED,
581 ofpact_put_GROUP(out)->group_id = group_id;
586 encode_GROUP(const struct ofpact_group *group,
587 enum ofp_version ofp_version, struct ofpbuf *out)
589 if (ofp_version == OFP10_VERSION) {
592 put_OFPAT11_GROUP(out, group->group_id);
596 static char * OVS_WARN_UNUSED_RESULT
597 parse_GROUP(char *arg, struct ofpbuf *ofpacts,
598 enum ofputil_protocol *usable_protocols OVS_UNUSED)
600 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
604 format_GROUP(const struct ofpact_group *a, struct ds *s)
606 ds_put_format(s, "%sgroup:%s%"PRIu32,
607 colors.special, colors.end, a->group_id);
610 /* Action structure for NXAST_CONTROLLER.
612 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
613 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
616 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
618 * - 'controller_id': The ID of the controller connection to which the
619 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
620 * sent only to controllers that have the specified controller connection
621 * ID. See "struct nx_controller_id" for more information. */
622 struct nx_action_controller {
623 ovs_be16 type; /* OFPAT_VENDOR. */
624 ovs_be16 len; /* Length is 16. */
625 ovs_be32 vendor; /* NX_VENDOR_ID. */
626 ovs_be16 subtype; /* NXAST_CONTROLLER. */
627 ovs_be16 max_len; /* Maximum length to send to controller. */
628 ovs_be16 controller_id; /* Controller ID to send packet-in. */
629 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
630 uint8_t zero; /* Must be zero. */
632 OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
634 /* Properties for NXAST_CONTROLLER2.
636 * For more information on the effect of NXAC2PT_PAUSE, see the large comment
637 * on NXT_PACKET_IN2 in nicira-ext.h */
638 enum nx_action_controller2_prop_type {
639 NXAC2PT_MAX_LEN, /* ovs_be16 max bytes to send (default all). */
640 NXAC2PT_CONTROLLER_ID, /* ovs_be16 dest controller ID (default 0). */
641 NXAC2PT_REASON, /* uint8_t reason (OFPR_*), default 0. */
642 NXAC2PT_USERDATA, /* Data to copy into NXPINT_USERDATA. */
643 NXAC2PT_PAUSE, /* Flag to pause pipeline to resume later. */
646 /* Action structure for NXAST_CONTROLLER2.
648 * This replacement for NXAST_CONTROLLER makes it extensible via properties. */
649 struct nx_action_controller2 {
650 ovs_be16 type; /* OFPAT_VENDOR. */
651 ovs_be16 len; /* Length is 16 or more. */
652 ovs_be32 vendor; /* NX_VENDOR_ID. */
653 ovs_be16 subtype; /* NXAST_CONTROLLER2. */
654 uint8_t zeros[6]; /* Must be zero. */
655 /* Followed by NXAC2PT_* properties. */
657 OFP_ASSERT(sizeof(struct nx_action_controller2) == 16);
660 decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
661 enum ofp_version ofp_version OVS_UNUSED,
664 struct ofpact_controller *oc;
666 oc = ofpact_put_CONTROLLER(out);
667 oc->ofpact.raw = NXAST_RAW_CONTROLLER;
668 oc->max_len = ntohs(nac->max_len);
669 oc->controller_id = ntohs(nac->controller_id);
670 oc->reason = nac->reason;
671 ofpact_finish_CONTROLLER(out, &oc);
677 decode_NXAST_RAW_CONTROLLER2(const struct nx_action_controller2 *nac2,
678 enum ofp_version ofp_version OVS_UNUSED,
681 if (!is_all_zeros(nac2->zeros, sizeof nac2->zeros)) {
682 return OFPERR_NXBRC_MUST_BE_ZERO;
685 size_t start_ofs = out->size;
686 struct ofpact_controller *oc = ofpact_put_CONTROLLER(out);
687 oc->ofpact.raw = NXAST_RAW_CONTROLLER2;
688 oc->max_len = UINT16_MAX;
689 oc->reason = OFPR_ACTION;
691 struct ofpbuf properties;
692 ofpbuf_use_const(&properties, nac2, ntohs(nac2->len));
693 ofpbuf_pull(&properties, sizeof *nac2);
695 while (properties.size > 0) {
696 struct ofpbuf payload;
699 enum ofperr error = ofpprop_pull(&properties, &payload, &type);
705 case NXAC2PT_MAX_LEN:
706 error = ofpprop_parse_u16(&payload, &oc->max_len);
709 case NXAC2PT_CONTROLLER_ID:
710 error = ofpprop_parse_u16(&payload, &oc->controller_id);
713 case NXAC2PT_REASON: {
715 error = ofpprop_parse_u8(&payload, &u8);
720 case NXAC2PT_USERDATA:
721 out->size = start_ofs + OFPACT_CONTROLLER_SIZE;
722 ofpbuf_put(out, payload.msg, ofpbuf_msgsize(&payload));
723 oc = ofpbuf_at_assert(out, start_ofs, sizeof *oc);
724 oc->userdata_len = ofpbuf_msgsize(&payload);
732 error = OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type);
740 ofpact_finish_CONTROLLER(out, &oc);
746 encode_CONTROLLER(const struct ofpact_controller *controller,
747 enum ofp_version ofp_version OVS_UNUSED,
750 if (controller->userdata_len
752 || controller->ofpact.raw == NXAST_RAW_CONTROLLER2) {
753 size_t start_ofs = out->size;
754 put_NXAST_CONTROLLER2(out);
755 if (controller->max_len != UINT16_MAX) {
756 ofpprop_put_u16(out, NXAC2PT_MAX_LEN, controller->max_len);
758 if (controller->controller_id != 0) {
759 ofpprop_put_u16(out, NXAC2PT_CONTROLLER_ID,
760 controller->controller_id);
762 if (controller->reason != OFPR_ACTION) {
763 ofpprop_put_u8(out, NXAC2PT_REASON, controller->reason);
765 if (controller->userdata_len != 0) {
766 ofpprop_put(out, NXAC2PT_USERDATA, controller->userdata,
767 controller->userdata_len);
769 if (controller->pause) {
770 ofpprop_put_flag(out, NXAC2PT_PAUSE);
772 pad_ofpat(out, start_ofs);
774 struct nx_action_controller *nac;
776 nac = put_NXAST_CONTROLLER(out);
777 nac->max_len = htons(controller->max_len);
778 nac->controller_id = htons(controller->controller_id);
779 nac->reason = controller->reason;
783 static char * OVS_WARN_UNUSED_RESULT
784 parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
785 enum ofputil_protocol *usable_protocols OVS_UNUSED)
787 enum ofp_packet_in_reason reason = OFPR_ACTION;
788 uint16_t controller_id = 0;
789 uint16_t max_len = UINT16_MAX;
790 const char *userdata = NULL;
795 } else if (strspn(arg, "0123456789") == strlen(arg)) {
796 char *error = str_to_u16(arg, "max_len", &max_len);
803 while (ofputil_parse_key_value(&arg, &name, &value)) {
804 if (!strcmp(name, "reason")) {
805 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
806 return xasprintf("unknown reason \"%s\"", value);
808 } else if (!strcmp(name, "max_len")) {
809 char *error = str_to_u16(value, "max_len", &max_len);
813 } else if (!strcmp(name, "id")) {
814 char *error = str_to_u16(value, "id", &controller_id);
818 } else if (!strcmp(name, "userdata")) {
820 } else if (!strcmp(name, "pause")) {
823 return xasprintf("unknown key \"%s\" parsing controller "
829 if (reason == OFPR_ACTION && controller_id == 0 && !userdata && !pause) {
830 struct ofpact_output *output;
832 output = ofpact_put_OUTPUT(ofpacts);
833 output->port = OFPP_CONTROLLER;
834 output->max_len = max_len;
836 struct ofpact_controller *controller;
838 controller = ofpact_put_CONTROLLER(ofpacts);
839 controller->max_len = max_len;
840 controller->reason = reason;
841 controller->controller_id = controller_id;
842 controller->pause = pause;
845 size_t start_ofs = ofpacts->size;
846 const char *end = ofpbuf_put_hex(ofpacts, userdata, NULL);
848 return xstrdup("bad hex digit in `controller' "
849 "action `userdata'");
851 size_t userdata_len = ofpacts->size - start_ofs;
852 controller = ofpacts->header;
853 controller->userdata_len = userdata_len;
855 ofpact_finish_CONTROLLER(ofpacts, &controller);
862 format_hex_arg(struct ds *s, const uint8_t *data, size_t len)
864 for (size_t i = 0; i < len; i++) {
868 ds_put_format(s, "%02"PRIx8, data[i]);
873 format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
875 if (a->reason == OFPR_ACTION && !a->controller_id && !a->userdata_len
877 ds_put_format(s, "%sCONTROLLER:%s%"PRIu16,
878 colors.special, colors.end, a->max_len);
880 enum ofp_packet_in_reason reason = a->reason;
882 ds_put_format(s, "%scontroller(%s", colors.paren, colors.end);
883 if (reason != OFPR_ACTION) {
884 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
886 ds_put_format(s, "%sreason=%s%s,", colors.param, colors.end,
887 ofputil_packet_in_reason_to_string(
888 reason, reasonbuf, sizeof reasonbuf));
890 if (a->max_len != UINT16_MAX) {
891 ds_put_format(s, "%smax_len=%s%"PRIu16",",
892 colors.param, colors.end, a->max_len);
894 if (a->controller_id != 0) {
895 ds_put_format(s, "%sid=%s%"PRIu16",",
896 colors.param, colors.end, a->controller_id);
898 if (a->userdata_len) {
899 ds_put_format(s, "%suserdata=%s", colors.param, colors.end);
900 format_hex_arg(s, a->userdata, a->userdata_len);
904 ds_put_format(s, "%spause%s,", colors.value, colors.end);
907 ds_put_format(s, "%s)%s", colors.paren, colors.end);
911 /* Enqueue action. */
912 struct ofp10_action_enqueue {
913 ovs_be16 type; /* OFPAT10_ENQUEUE. */
914 ovs_be16 len; /* Len is 16. */
915 ovs_be16 port; /* Port that queue belongs. Should
916 refer to a valid physical port
917 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
918 uint8_t pad[6]; /* Pad for 64-bit alignment. */
919 ovs_be32 queue_id; /* Where to enqueue the packets. */
921 OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
924 decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
925 enum ofp_version ofp_version OVS_UNUSED,
928 struct ofpact_enqueue *enqueue;
930 enqueue = ofpact_put_ENQUEUE(out);
931 enqueue->port = u16_to_ofp(ntohs(oae->port));
932 enqueue->queue = ntohl(oae->queue_id);
933 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
934 && enqueue->port != OFPP_IN_PORT
935 && enqueue->port != OFPP_LOCAL) {
936 return OFPERR_OFPBAC_BAD_OUT_PORT;
942 encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
943 enum ofp_version ofp_version, struct ofpbuf *out)
945 if (ofp_version == OFP10_VERSION) {
946 struct ofp10_action_enqueue *oae;
948 oae = put_OFPAT10_ENQUEUE(out);
949 oae->port = htons(ofp_to_u16(enqueue->port));
950 oae->queue_id = htonl(enqueue->queue);
956 static char * OVS_WARN_UNUSED_RESULT
957 parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
958 enum ofputil_protocol *usable_protocols OVS_UNUSED)
961 char *port = strtok_r(arg, ":q,", &sp);
962 char *queue = strtok_r(NULL, "", &sp);
963 struct ofpact_enqueue *enqueue;
965 if (port == NULL || queue == NULL) {
966 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
967 "\"enqueue(PORT,QUEUE)\"");
970 enqueue = ofpact_put_ENQUEUE(ofpacts);
971 if (!ofputil_port_from_string(port, &enqueue->port)) {
972 return xasprintf("%s: enqueue to unknown port", port);
974 return str_to_u32(queue, &enqueue->queue);
978 format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
980 ds_put_format(s, "%senqueue:%s", colors.param, colors.end);
981 ofputil_format_port(a->port, s);
982 ds_put_format(s, ":%"PRIu32, a->queue);
985 /* Action structure for NXAST_OUTPUT_REG.
987 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
989 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
990 * the NXAST_REG_LOAD action.
992 * The acceptable nxm_header values for 'src' are the same as the acceptable
993 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
995 * The 'max_len' field indicates the number of bytes to send when the chosen
996 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
997 * field of OFPAT_OUTPUT.
999 * The 'zero' field is required to be zeroed for forward compatibility. */
1000 struct nx_action_output_reg {
1001 ovs_be16 type; /* OFPAT_VENDOR. */
1002 ovs_be16 len; /* 24. */
1003 ovs_be32 vendor; /* NX_VENDOR_ID. */
1004 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
1006 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1007 ovs_be32 src; /* Source. */
1009 ovs_be16 max_len; /* Max length to send to controller. */
1011 uint8_t zero[6]; /* Reserved, must be zero. */
1013 OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
1015 /* Action structure for NXAST_OUTPUT_REG2.
1017 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
1018 * experimenter OXM as 'src'.
1020 struct nx_action_output_reg2 {
1021 ovs_be16 type; /* OFPAT_VENDOR. */
1022 ovs_be16 len; /* 24. */
1023 ovs_be32 vendor; /* NX_VENDOR_ID. */
1024 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
1026 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1027 ovs_be16 max_len; /* Max length to send to controller. */
1030 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1031 * - Enough 0-bytes to pad the action out to 24 bytes. */
1034 OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
1037 decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
1038 enum ofp_version ofp_version OVS_UNUSED,
1041 struct ofpact_output_reg *output_reg;
1043 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
1044 return OFPERR_OFPBAC_BAD_ARGUMENT;
1047 output_reg = ofpact_put_OUTPUT_REG(out);
1048 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
1049 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
1050 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1051 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1052 output_reg->max_len = ntohs(naor->max_len);
1054 return mf_check_src(&output_reg->src, NULL);
1058 decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
1059 enum ofp_version ofp_version OVS_UNUSED,
1062 struct ofpact_output_reg *output_reg;
1063 output_reg = ofpact_put_OUTPUT_REG(out);
1064 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
1065 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1066 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1067 output_reg->max_len = ntohs(naor->max_len);
1069 struct ofpbuf b = ofpbuf_const_initializer(naor, ntohs(naor->len));
1070 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
1072 enum ofperr error = nx_pull_header(&b, &output_reg->src.field, NULL);
1076 if (!is_all_zeros(b.data, b.size)) {
1077 return OFPERR_NXBRC_MUST_BE_ZERO;
1080 return mf_check_src(&output_reg->src, NULL);
1084 encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
1085 enum ofp_version ofp_version OVS_UNUSED,
1088 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1089 * cannot be encoded in the older form, encode it as
1090 * NXAST_RAW_OUTPUT_REG2. */
1091 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
1092 || !mf_nxm_header(output_reg->src.field->id)) {
1093 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
1094 size_t size = out->size;
1096 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1097 output_reg->src.n_bits);
1098 naor->max_len = htons(output_reg->max_len);
1100 out->size = size - sizeof naor->pad;
1101 nx_put_header(out, output_reg->src.field->id, 0, false);
1104 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
1106 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1107 output_reg->src.n_bits);
1108 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
1109 naor->max_len = htons(output_reg->max_len);
1113 static char * OVS_WARN_UNUSED_RESULT
1114 parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
1115 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1117 return parse_OUTPUT(arg, ofpacts, usable_protocols);
1121 format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
1123 ds_put_format(s, "%soutput:%s", colors.special, colors.end);
1124 mf_format_subfield(&a->src, s);
1127 /* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1129 * The bundle actions choose a slave from a supplied list of options.
1130 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
1131 * selection to a register.
1133 * The list of possible slaves follows the nx_action_bundle structure. The size
1134 * of each slave is governed by its type as indicated by the 'slave_type'
1135 * parameter. The list of slaves should be padded at its end with zeros to make
1136 * the total length of the action a multiple of 8.
1138 * Switches infer from the 'slave_type' parameter the size of each slave. All
1139 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
1140 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
1141 * 2 byte width. Switches should reject actions which indicate unknown or
1142 * unsupported slave types.
1144 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1145 * slave. If the switch does not support the specified 'algorithm' parameter,
1146 * it should reject the action.
1148 * Several algorithms take into account liveness when selecting slaves. The
1149 * liveness of a slave is implementation defined (with one exception), but will
1150 * generally take into account things like its carrier status and the results
1151 * of any link monitoring protocols which happen to be running on it. In order
1152 * to give controllers a place-holder value, the OFPP_NONE port is always
1155 * Some slave selection strategies require the use of a hash function, in which
1156 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
1157 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
1158 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
1159 * 'basis' parameter is used as a universal hash parameter. Different values
1160 * of 'basis' yield different hash results.
1162 * The 'zero' parameter at the end of the action structure is reserved for
1163 * future use. Switches are required to reject actions which have nonzero
1164 * bytes in the 'zero' field.
1166 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
1167 * should reject actions which have nonzero bytes in either of these fields.
1169 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
1170 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
1171 * similar to those for the NXAST_REG_LOAD action. */
1172 struct nx_action_bundle {
1173 ovs_be16 type; /* OFPAT_VENDOR. */
1174 ovs_be16 len; /* Length including slaves. */
1175 ovs_be32 vendor; /* NX_VENDOR_ID. */
1176 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
1178 /* Slave choice algorithm to apply to hash value. */
1179 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
1181 /* What fields to hash and how. */
1182 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
1183 ovs_be16 basis; /* Universal hash parameter. */
1185 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
1186 ovs_be16 n_slaves; /* Number of slaves. */
1188 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1189 ovs_be32 dst; /* Destination. */
1191 uint8_t zero[4]; /* Reserved. Must be zero. */
1193 OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
1196 decode_bundle(bool load, const struct nx_action_bundle *nab,
1197 struct ofpbuf *ofpacts)
1199 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1200 struct ofpact_bundle *bundle;
1201 uint32_t slave_type;
1202 size_t slaves_size, i;
1205 bundle = ofpact_put_BUNDLE(ofpacts);
1207 bundle->n_slaves = ntohs(nab->n_slaves);
1208 bundle->basis = ntohs(nab->basis);
1209 bundle->fields = ntohs(nab->fields);
1210 bundle->algorithm = ntohs(nab->algorithm);
1211 slave_type = ntohl(nab->slave_type);
1212 slaves_size = ntohs(nab->len) - sizeof *nab;
1214 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1215 if (!flow_hash_fields_valid(bundle->fields)) {
1216 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
1217 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
1218 VLOG_WARN_RL(&rl, "too many slaves");
1219 } else if (bundle->algorithm != NX_BD_ALG_HRW
1220 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
1221 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
1222 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
1223 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
1228 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
1229 VLOG_WARN_RL(&rl, "reserved field is nonzero");
1230 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1234 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
1235 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
1236 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
1238 if (bundle->dst.n_bits < 16) {
1239 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
1241 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1244 if (nab->ofs_nbits || nab->dst) {
1245 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
1246 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1250 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1251 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1252 "allocated for slaves. %"PRIuSIZE" bytes are required "
1253 "for %"PRIu16" slaves.",
1254 load ? "bundle_load" : "bundle", slaves_size,
1255 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1256 error = OFPERR_OFPBAC_BAD_LEN;
1259 for (i = 0; i < bundle->n_slaves; i++) {
1260 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1261 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
1262 bundle = ofpacts->header;
1265 ofpact_finish_BUNDLE(ofpacts, &bundle);
1267 error = bundle_check(bundle, OFPP_MAX, NULL);
1273 decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1274 enum ofp_version ofp_version OVS_UNUSED,
1277 return decode_bundle(false, nab, out);
1281 decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
1282 enum ofp_version ofp_version OVS_UNUSED,
1285 return decode_bundle(true, nab, out);
1289 encode_BUNDLE(const struct ofpact_bundle *bundle,
1290 enum ofp_version ofp_version OVS_UNUSED,
1293 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1294 struct nx_action_bundle *nab;
1298 nab = (bundle->dst.field
1299 ? put_NXAST_BUNDLE_LOAD(out)
1300 : put_NXAST_BUNDLE(out));
1301 nab->len = htons(ntohs(nab->len) + slaves_len);
1302 nab->algorithm = htons(bundle->algorithm);
1303 nab->fields = htons(bundle->fields);
1304 nab->basis = htons(bundle->basis);
1305 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
1306 nab->n_slaves = htons(bundle->n_slaves);
1307 if (bundle->dst.field) {
1308 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1309 bundle->dst.n_bits);
1310 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
1313 slaves = ofpbuf_put_zeros(out, slaves_len);
1314 for (i = 0; i < bundle->n_slaves; i++) {
1315 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1319 static char * OVS_WARN_UNUSED_RESULT
1320 parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1321 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1323 return bundle_parse(arg, ofpacts);
1326 static char * OVS_WARN_UNUSED_RESULT
1327 parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1329 return bundle_parse_load(arg, ofpacts);
1333 format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1335 bundle_format(a, s);
1338 /* Set VLAN actions. */
1341 decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1344 return OFPERR_OFPBAC_BAD_ARGUMENT;
1346 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1347 vlan_vid->vlan_vid = vid;
1348 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1354 decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1355 enum ofp_version ofp_version OVS_UNUSED,
1358 return decode_set_vlan_vid(vid, true, out);
1362 decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1363 enum ofp_version ofp_version OVS_UNUSED,
1366 return decode_set_vlan_vid(vid, false, out);
1370 encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1371 enum ofp_version ofp_version, struct ofpbuf *out)
1373 uint16_t vid = vlan_vid->vlan_vid;
1375 /* Push a VLAN tag, if none is present and this form of the action calls
1376 * for such a feature. */
1377 if (ofp_version > OFP10_VERSION
1378 && vlan_vid->push_vlan_if_needed
1379 && !vlan_vid->flow_has_vlan) {
1380 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1383 if (ofp_version == OFP10_VERSION) {
1384 put_OFPAT10_SET_VLAN_VID(out, vid);
1385 } else if (ofp_version == OFP11_VERSION) {
1386 put_OFPAT11_SET_VLAN_VID(out, vid);
1388 ofpact_put_set_field(out, ofp_version,
1389 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1393 static char * OVS_WARN_UNUSED_RESULT
1394 parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1396 struct ofpact_vlan_vid *vlan_vid;
1400 error = str_to_u16(arg, "VLAN VID", &vid);
1405 if (vid & ~VLAN_VID_MASK) {
1406 return xasprintf("%s: not a valid VLAN VID", arg);
1408 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1409 vlan_vid->vlan_vid = vid;
1410 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1414 static char * OVS_WARN_UNUSED_RESULT
1415 parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1416 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1418 return parse_set_vlan_vid(arg, ofpacts, false);
1422 format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1424 ds_put_format(s, "%s%s:%s%"PRIu16, colors.param,
1425 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
1426 colors.end, a->vlan_vid);
1429 /* Set PCP actions. */
1432 decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
1435 return OFPERR_OFPBAC_BAD_ARGUMENT;
1437 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1438 vlan_pcp->vlan_pcp = pcp;
1439 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1445 decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1446 enum ofp_version ofp_version OVS_UNUSED,
1449 return decode_set_vlan_pcp(pcp, true, out);
1453 decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1454 enum ofp_version ofp_version OVS_UNUSED,
1457 return decode_set_vlan_pcp(pcp, false, out);
1461 encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1462 enum ofp_version ofp_version, struct ofpbuf *out)
1464 uint8_t pcp = vlan_pcp->vlan_pcp;
1466 /* Push a VLAN tag, if none is present and this form of the action calls
1467 * for such a feature. */
1468 if (ofp_version > OFP10_VERSION
1469 && vlan_pcp->push_vlan_if_needed
1470 && !vlan_pcp->flow_has_vlan) {
1471 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1474 if (ofp_version == OFP10_VERSION) {
1475 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1476 } else if (ofp_version == OFP11_VERSION) {
1477 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1479 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
1483 static char * OVS_WARN_UNUSED_RESULT
1484 parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
1486 struct ofpact_vlan_pcp *vlan_pcp;
1490 error = str_to_u8(arg, "VLAN PCP", &pcp);
1496 return xasprintf("%s: not a valid VLAN PCP", arg);
1498 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1499 vlan_pcp->vlan_pcp = pcp;
1500 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1504 static char * OVS_WARN_UNUSED_RESULT
1505 parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1506 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1508 return parse_set_vlan_pcp(arg, ofpacts, false);
1512 format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1514 ds_put_format(s, "%s%s:%s%"PRIu8, colors.param,
1515 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
1516 colors.end, a->vlan_pcp);
1519 /* Strip VLAN actions. */
1522 decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1524 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1529 decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
1531 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1536 encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1537 enum ofp_version ofp_version, struct ofpbuf *out)
1539 if (ofp_version == OFP10_VERSION) {
1540 put_OFPAT10_STRIP_VLAN(out);
1542 put_OFPAT11_POP_VLAN(out);
1546 static char * OVS_WARN_UNUSED_RESULT
1547 parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1548 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1550 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1554 static char * OVS_WARN_UNUSED_RESULT
1555 parse_pop_vlan(struct ofpbuf *ofpacts)
1557 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1562 format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1564 ds_put_format(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1566 : "%sstrip_vlan%s"),
1567 colors.value, colors.end);
1570 /* Push VLAN action. */
1573 decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1574 enum ofp_version ofp_version OVS_UNUSED,
1577 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1578 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1579 return OFPERR_OFPBAC_BAD_ARGUMENT;
1581 ofpact_put_PUSH_VLAN(out);
1586 encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1587 enum ofp_version ofp_version, struct ofpbuf *out)
1589 if (ofp_version == OFP10_VERSION) {
1590 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1591 * follow this action. */
1593 /* XXX ETH_TYPE_VLAN_8021AD case */
1594 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1598 static char * OVS_WARN_UNUSED_RESULT
1599 parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1600 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1605 *usable_protocols &= OFPUTIL_P_OF11_UP;
1606 error = str_to_u16(arg, "ethertype", ðertype);
1611 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1612 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1613 return xasprintf("%s: not a valid VLAN ethertype", arg);
1616 ofpact_put_PUSH_VLAN(ofpacts);
1621 format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
1623 /* XXX 802.1AD case*/
1624 ds_put_format(s, "%spush_vlan:%s%#"PRIx16,
1625 colors.param, colors.end, ETH_TYPE_VLAN_8021Q);
1628 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1629 struct ofp_action_dl_addr {
1630 ovs_be16 type; /* Type. */
1631 ovs_be16 len; /* Length is 16. */
1632 struct eth_addr dl_addr; /* Ethernet address. */
1635 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
1638 decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
1639 enum ofp_version ofp_version OVS_UNUSED,
1642 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
1647 decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
1648 enum ofp_version ofp_version OVS_UNUSED,
1651 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
1656 encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1657 enum ofp_raw_action_type raw, enum mf_field_id field,
1660 if (ofp_version < OFP12_VERSION) {
1661 struct ofp_action_dl_addr *oada;
1663 oada = ofpact_put_raw(out, ofp_version, raw, 0);
1664 oada->dl_addr = mac->mac;
1666 ofpact_put_set_field(out, ofp_version, field,
1667 eth_addr_to_uint64(mac->mac));
1672 encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1675 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1681 encode_SET_ETH_DST(const struct ofpact_mac *mac,
1682 enum ofp_version ofp_version,
1685 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1690 static char * OVS_WARN_UNUSED_RESULT
1691 parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1692 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1694 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
1697 static char * OVS_WARN_UNUSED_RESULT
1698 parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1699 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1701 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
1705 format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
1707 ds_put_format(s, "%smod_dl_src:%s"ETH_ADDR_FMT,
1708 colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
1712 format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1714 ds_put_format(s, "%smod_dl_dst:%s"ETH_ADDR_FMT,
1715 colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
1718 /* Set IPv4 address actions. */
1721 decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1722 enum ofp_version ofp_version OVS_UNUSED,
1725 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1730 decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1731 enum ofp_version ofp_version OVS_UNUSED,
1734 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1739 encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1740 enum ofp_version ofp_version,
1741 enum ofp_raw_action_type raw, enum mf_field_id field,
1744 ovs_be32 addr = ipv4->ipv4;
1745 if (ofp_version < OFP12_VERSION) {
1746 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
1748 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
1753 encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1754 enum ofp_version ofp_version, struct ofpbuf *out)
1756 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1761 encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1762 enum ofp_version ofp_version, struct ofpbuf *out)
1764 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1768 static char * OVS_WARN_UNUSED_RESULT
1769 parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1770 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1772 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1775 static char * OVS_WARN_UNUSED_RESULT
1776 parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1777 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1779 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1783 format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1785 ds_put_format(s, "%smod_nw_src:%s"IP_FMT,
1786 colors.param, colors.end, IP_ARGS(a->ipv4));
1790 format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1792 ds_put_format(s, "%smod_nw_dst:%s"IP_FMT,
1793 colors.param, colors.end, IP_ARGS(a->ipv4));
1796 /* Set IPv4/v6 TOS actions. */
1799 decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1800 enum ofp_version ofp_version OVS_UNUSED,
1803 if (dscp & ~IP_DSCP_MASK) {
1804 return OFPERR_OFPBAC_BAD_ARGUMENT;
1806 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1812 encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1813 enum ofp_version ofp_version, struct ofpbuf *out)
1815 if (ofp_version < OFP12_VERSION) {
1816 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1818 ofpact_put_set_field(out, ofp_version,
1819 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1823 static char * OVS_WARN_UNUSED_RESULT
1824 parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1825 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1830 error = str_to_u8(arg, "TOS", &tos);
1835 if (tos & ~IP_DSCP_MASK) {
1836 return xasprintf("%s: not a valid TOS", arg);
1838 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1843 format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
1845 ds_put_format(s, "%smod_nw_tos:%s%d", colors.param, colors.end, a->dscp);
1848 /* Set IPv4/v6 ECN actions. */
1851 decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1852 enum ofp_version ofp_version OVS_UNUSED,
1855 if (ecn & ~IP_ECN_MASK) {
1856 return OFPERR_OFPBAC_BAD_ARGUMENT;
1858 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1864 encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1865 enum ofp_version ofp_version, struct ofpbuf *out)
1867 uint8_t ecn = ip_ecn->ecn;
1868 if (ofp_version == OFP10_VERSION) {
1870 } else if (ofp_version == OFP11_VERSION) {
1871 put_OFPAT11_SET_NW_ECN(out, ecn);
1873 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1877 static char * OVS_WARN_UNUSED_RESULT
1878 parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1879 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1884 error = str_to_u8(arg, "ECN", &ecn);
1889 if (ecn & ~IP_ECN_MASK) {
1890 return xasprintf("%s: not a valid ECN", arg);
1892 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1897 format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1899 ds_put_format(s, "%smod_nw_ecn:%s%d",
1900 colors.param, colors.end, a->ecn);
1903 /* Set IPv4/v6 TTL actions. */
1906 decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1907 enum ofp_version ofp_version OVS_UNUSED,
1910 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1915 encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1916 enum ofp_version ofp_version, struct ofpbuf *out)
1918 if (ofp_version >= OFP11_VERSION) {
1919 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1925 static char * OVS_WARN_UNUSED_RESULT
1926 parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1927 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1932 error = str_to_u8(arg, "TTL", &ttl);
1937 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1942 format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1944 ds_put_format(s, "%smod_nw_ttl:%s%d", colors.param, colors.end, a->ttl);
1947 /* Set TCP/UDP/SCTP port actions. */
1950 decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1951 enum ofp_version ofp_version OVS_UNUSED,
1954 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1959 decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1960 enum ofp_version ofp_version OVS_UNUSED,
1963 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1968 encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1969 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1970 enum mf_field_id field, struct ofpbuf *out)
1972 uint16_t port = l4_port->port;
1974 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1975 ofpact_put_set_field(out, ofp_version, field, port);
1977 ofpact_put_raw(out, ofp_version, raw, port);
1982 encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1983 enum ofp_version ofp_version, struct ofpbuf *out)
1985 uint8_t proto = l4_port->flow_ip_proto;
1986 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1987 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1988 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1991 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1995 encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1996 enum ofp_version ofp_version,
1999 uint8_t proto = l4_port->flow_ip_proto;
2000 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
2001 : proto == IPPROTO_UDP ? MFF_UDP_DST
2002 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
2005 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
2008 static char * OVS_WARN_UNUSED_RESULT
2009 parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
2010 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2012 return str_to_u16(arg, "source port",
2013 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
2016 static char * OVS_WARN_UNUSED_RESULT
2017 parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
2018 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2020 return str_to_u16(arg, "destination port",
2021 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
2025 format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
2027 ds_put_format(s, "%smod_tp_src:%s%d", colors.param, colors.end, a->port);
2031 format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
2033 ds_put_format(s, "%smod_tp_dst:%s%d", colors.param, colors.end, a->port);
2036 /* Action structure for OFPAT_COPY_FIELD. */
2037 struct ofp15_action_copy_field {
2038 ovs_be16 type; /* OFPAT_COPY_FIELD. */
2039 ovs_be16 len; /* Length is padded to 64 bits. */
2040 ovs_be16 n_bits; /* Number of bits to copy. */
2041 ovs_be16 src_offset; /* Starting bit offset in source. */
2042 ovs_be16 dst_offset; /* Starting bit offset in destination. */
2045 * - OXM header for source field.
2046 * - OXM header for destination field.
2047 * - Padding with 0-bytes to a multiple of 8 bytes.
2048 * The "pad2" member is the beginning of the above. */
2051 OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
2053 /* Action structure for OpenFlow 1.3 extension copy-field action.. */
2054 struct onf_action_copy_field {
2055 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
2056 ovs_be16 len; /* Length is padded to 64 bits. */
2057 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
2058 ovs_be16 exp_type; /* 3200. */
2059 uint8_t pad[2]; /* Not used. */
2060 ovs_be16 n_bits; /* Number of bits to copy. */
2061 ovs_be16 src_offset; /* Starting bit offset in source. */
2062 ovs_be16 dst_offset; /* Starting bit offset in destination. */
2063 uint8_t pad2[2]; /* Not used. */
2065 * - OXM header for source field.
2066 * - OXM header for destination field.
2067 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2068 * The "pad3" member is the beginning of the above. */
2069 uint8_t pad3[4]; /* Not used. */
2071 OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
2073 /* Action structure for NXAST_REG_MOVE.
2075 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2076 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2077 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
2078 * the next most significant bit, and so on.
2080 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
2081 * sense to use nxm_hasmask=1 because the action does not do any kind of
2082 * matching; it uses the actual value of a field.)
2084 * The following nxm_header values are potentially acceptable as 'src':
2099 * - NXM_OF_ICMP_TYPE
2100 * - NXM_OF_ICMP_CODE
2107 * - NXM_NX_ICMPV6_TYPE
2108 * - NXM_NX_ICMPV6_CODE
2111 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2113 * - NXM_NX_TUN_IPV4_SRC
2114 * - NXM_NX_TUN_IPV4_DST
2116 * The following nxm_header values are potentially acceptable as 'dst':
2127 * - NXM_OF_ICMP_TYPE
2128 * - NXM_OF_ICMP_CODE
2129 * - NXM_NX_ICMPV6_TYPE
2130 * - NXM_NX_ICMPV6_CODE
2136 * Modifying any of the above fields changes the corresponding packet
2141 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2145 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
2146 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
2147 * header (if any), ignoring the other bits. Setting a value with CFI=1
2148 * adds or modifies the 802.1Q header appropriately, setting the TCI field
2149 * to the field's new value (with the CFI bit masked out).
2151 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
2152 * any of these values modifies the corresponding tunnel header field used
2153 * for the packet's next tunnel encapsulation, if allowed by the
2154 * configuration of the output tunnel port.
2156 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2157 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
2158 * used only if the flow's nx_match includes an nxm_entry that specifies
2159 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2161 * The switch will reject actions for which src_ofs+n_bits is greater than the
2162 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2163 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2165 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2166 * behaves as if 'src' were copied out to a temporary buffer, then the
2167 * temporary buffer copied to 'dst'.
2169 struct nx_action_reg_move {
2170 ovs_be16 type; /* OFPAT_VENDOR. */
2171 ovs_be16 len; /* Length is 24. */
2172 ovs_be32 vendor; /* NX_VENDOR_ID. */
2173 ovs_be16 subtype; /* NXAST_REG_MOVE. */
2174 ovs_be16 n_bits; /* Number of bits. */
2175 ovs_be16 src_ofs; /* Starting bit offset in source. */
2176 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
2178 * - OXM/NXM header for source field (4 or 8 bytes).
2179 * - OXM/NXM header for destination field (4 or 8 bytes).
2180 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
2182 OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
2185 decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
2186 const void *action, ovs_be16 action_len, size_t oxm_offset,
2187 struct ofpbuf *ofpacts)
2189 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2190 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
2191 move->src.ofs = ntohs(src_offset);
2192 move->src.n_bits = ntohs(n_bits);
2193 move->dst.ofs = ntohs(dst_offset);
2194 move->dst.n_bits = ntohs(n_bits);
2196 struct ofpbuf b = ofpbuf_const_initializer(action, ntohs(action_len));
2197 ofpbuf_pull(&b, oxm_offset);
2199 enum ofperr error = nx_pull_header(&b, &move->src.field, NULL);
2203 error = nx_pull_header(&b, &move->dst.field, NULL);
2208 if (!is_all_zeros(b.data, b.size)) {
2209 return OFPERR_NXBRC_MUST_BE_ZERO;
2212 return nxm_reg_move_check(move, NULL);
2216 decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
2217 enum ofp_version ofp_version OVS_UNUSED,
2218 struct ofpbuf *ofpacts)
2220 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2221 oacf->n_bits, oacf, oacf->len,
2222 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
2226 decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
2227 enum ofp_version ofp_version OVS_UNUSED,
2228 struct ofpbuf *ofpacts)
2230 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2231 oacf->n_bits, oacf, oacf->len,
2232 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
2236 decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
2237 enum ofp_version ofp_version OVS_UNUSED,
2238 struct ofpbuf *ofpacts)
2240 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2241 move->ofpact.raw = NXAST_RAW_REG_MOVE;
2242 move->src.ofs = ntohs(narm->src_ofs);
2243 move->src.n_bits = ntohs(narm->n_bits);
2244 move->dst.ofs = ntohs(narm->dst_ofs);
2245 move->dst.n_bits = ntohs(narm->n_bits);
2247 struct ofpbuf b = ofpbuf_const_initializer(narm, ntohs(narm->len));
2248 ofpbuf_pull(&b, sizeof *narm);
2250 enum ofperr error = nx_pull_header(&b, &move->src.field, NULL);
2254 error = nx_pull_header(&b, &move->dst.field, NULL);
2258 if (!is_all_zeros(b.data, b.size)) {
2259 return OFPERR_NXBRC_MUST_BE_ZERO;
2262 return nxm_reg_move_check(move, NULL);
2266 encode_REG_MOVE(const struct ofpact_reg_move *move,
2267 enum ofp_version ofp_version, struct ofpbuf *out)
2269 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2270 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2271 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2272 * probe for support. Until we have that ability, we currently prefer
2273 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2275 size_t start_ofs = out->size;
2276 if (ofp_version >= OFP15_VERSION) {
2277 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
2278 copy->n_bits = htons(move->dst.n_bits);
2279 copy->src_offset = htons(move->src.ofs);
2280 copy->dst_offset = htons(move->dst.ofs);
2281 out->size = out->size - sizeof copy->pad2;
2282 nx_put_header(out, move->src.field->id, ofp_version, false);
2283 nx_put_header(out, move->dst.field->id, ofp_version, false);
2284 } else if (ofp_version == OFP13_VERSION
2285 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2286 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2287 copy->n_bits = htons(move->dst.n_bits);
2288 copy->src_offset = htons(move->src.ofs);
2289 copy->dst_offset = htons(move->dst.ofs);
2290 out->size = out->size - sizeof copy->pad3;
2291 nx_put_header(out, move->src.field->id, ofp_version, false);
2292 nx_put_header(out, move->dst.field->id, ofp_version, false);
2294 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
2295 narm->n_bits = htons(move->dst.n_bits);
2296 narm->src_ofs = htons(move->src.ofs);
2297 narm->dst_ofs = htons(move->dst.ofs);
2298 nx_put_header(out, move->src.field->id, 0, false);
2299 nx_put_header(out, move->dst.field->id, 0, false);
2301 pad_ofpat(out, start_ofs);
2304 static char * OVS_WARN_UNUSED_RESULT
2305 parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2306 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2308 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2309 const char *full_arg = arg;
2312 error = mf_parse_subfield__(&move->src, &arg);
2316 if (strncmp(arg, "->", 2)) {
2317 return xasprintf("%s: missing `->' following source", full_arg);
2320 error = mf_parse_subfield(&move->dst, arg);
2325 if (move->src.n_bits != move->dst.n_bits) {
2326 return xasprintf("%s: source field is %d bits wide but destination is "
2327 "%d bits wide", full_arg,
2328 move->src.n_bits, move->dst.n_bits);
2334 format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2336 nxm_format_reg_move(a, s);
2339 /* Action structure for OFPAT12_SET_FIELD. */
2340 struct ofp12_action_set_field {
2341 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2342 ovs_be16 len; /* Length is padded to 64 bits. */
2345 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2346 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2348 * The "pad" member is the beginning of the above. */
2351 OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2353 /* Action structure for NXAST_REG_LOAD.
2355 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2356 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2357 * starts at 0 for the least-significant bit, 1 for the next most significant
2360 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2361 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2364 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2365 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2366 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2367 * also stored as one less than its true value:
2370 * +------------------------------+------------------+
2371 * | ofs | n_bits - 1 |
2372 * +------------------------------+------------------+
2374 * The switch will reject actions for which ofs+n_bits is greater than the
2375 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2376 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2377 * OFPBAC_BAD_ARGUMENT.
2379 struct nx_action_reg_load {
2380 ovs_be16 type; /* OFPAT_VENDOR. */
2381 ovs_be16 len; /* Length is 24. */
2382 ovs_be32 vendor; /* NX_VENDOR_ID. */
2383 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2384 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2385 ovs_be32 dst; /* Destination register. */
2386 ovs_be64 value; /* Immediate value. */
2388 OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
2390 /* Action structure for NXAST_REG_LOAD2.
2392 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2393 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2394 * OXM experimenter fields. */
2395 struct nx_action_reg_load2 {
2396 ovs_be16 type; /* OFPAT_VENDOR. */
2397 ovs_be16 len; /* At least 16. */
2398 ovs_be32 vendor; /* NX_VENDOR_ID. */
2399 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2402 * - An NXM/OXM header, value, and optionally a mask.
2403 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2405 * The "pad" member is the beginning of the above. */
2408 OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2411 decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2412 bool may_mask, struct ofpbuf *ofpacts)
2414 struct ofpbuf b = ofpbuf_const_initializer(oasf, ntohs(oasf->len));
2415 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
2417 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2418 enum ofperr error = nx_pull_entry(&b, &sf->field, &sf->value,
2419 may_mask ? &sf->mask : NULL);
2421 return (error == OFPERR_OFPBMC_BAD_MASK
2422 ? OFPERR_OFPBAC_BAD_SET_MASK
2426 memset(&sf->mask, 0xff, sf->field->n_bytes);
2429 if (!is_all_zeros(b.data, b.size)) {
2430 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2433 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2435 if (sf->field->id == MFF_IN_PORT_OXM) {
2436 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2439 /* oxm_length is now validated to be compatible with mf_value. */
2440 if (!sf->field->writable) {
2441 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2443 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2446 /* The value must be valid for match. OpenFlow 1.5 also says,
2447 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2448 * a 1-bit in oxm_value and in oxm_mask." */
2449 if (!mf_is_value_valid(sf->field, &sf->value)
2450 || (sf->field->id == MFF_VLAN_VID
2451 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2452 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
2453 struct ds ds = DS_EMPTY_INITIALIZER;
2454 mf_format(sf->field, &sf->value, NULL, &ds);
2455 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
2456 sf->field->name, ds_cstr(&ds));
2459 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2465 decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
2466 enum ofp_version ofp_version OVS_UNUSED,
2467 struct ofpbuf *ofpacts)
2469 return decode_ofpat_set_field(oasf, false, ofpacts);
2473 decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
2474 enum ofp_version ofp_version OVS_UNUSED,
2475 struct ofpbuf *ofpacts)
2477 return decode_ofpat_set_field(oasf, true, ofpacts);
2481 decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
2482 enum ofp_version ofp_version OVS_UNUSED,
2485 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2486 struct mf_subfield dst;
2489 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2491 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2492 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2493 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2494 error = mf_check_dst(&dst, NULL);
2499 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2501 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2502 return OFPERR_OFPBAC_BAD_ARGUMENT;
2505 sf->field = dst.field;
2506 bitwise_put(ntohll(narl->value),
2507 &sf->value, dst.field->n_bytes, dst.ofs,
2509 bitwise_put(UINT64_MAX,
2510 &sf->mask, dst.field->n_bytes, dst.ofs,
2517 decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
2518 enum ofp_version ofp_version OVS_UNUSED,
2521 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(out);
2522 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2524 struct ofpbuf b = ofpbuf_const_initializer(narl, ntohs(narl->len));
2525 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
2527 enum ofperr error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
2531 if (!is_all_zeros(b.data, b.size)) {
2532 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2535 if (!sf->field->writable) {
2536 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2538 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2544 ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
2545 enum mf_field_id field, uint64_t value_)
2547 struct ofp12_action_set_field *oasf OVS_UNUSED;
2548 int n_bytes = mf_from_id(field)->n_bytes;
2549 size_t start_ofs = openflow->size;
2550 union mf_value value;
2552 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
2554 oasf = put_OFPAT12_SET_FIELD(openflow);
2555 openflow->size = openflow->size - sizeof oasf->pad;
2556 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2557 pad_ofpat(openflow, start_ofs);
2561 next_load_segment(const struct ofpact_set_field *sf,
2562 struct mf_subfield *dst, uint64_t *value)
2564 int n_bits = sf->field->n_bits;
2565 int n_bytes = sf->field->n_bytes;
2566 int start = dst->ofs + dst->n_bits;
2568 if (start < n_bits) {
2569 dst->field = sf->field;
2570 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2571 if (dst->ofs < n_bits) {
2572 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2573 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2574 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
2581 /* Convert 'sf' to a series of REG_LOADs. */
2583 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
2585 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
2586 * experimenter OXM or is variable length (or if it came in as
2587 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2588 * NXAST_REG_LOAD, which is backward compatible. */
2589 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
2590 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
2591 struct nx_action_reg_load2 *narl OVS_UNUSED;
2592 size_t start_ofs = openflow->size;
2594 narl = put_NXAST_REG_LOAD2(openflow);
2595 openflow->size = openflow->size - sizeof narl->pad;
2596 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2597 pad_ofpat(openflow, start_ofs);
2599 struct mf_subfield dst;
2602 dst.ofs = dst.n_bits = 0;
2603 while (next_load_segment(sf, &dst, &value)) {
2604 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2605 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2606 narl->dst = htonl(mf_nxm_header(dst.field->id));
2607 narl->value = htonll(value);
2612 /* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2613 * actions, if we can, falling back to Nicira extensions if we must.
2615 * We check only meta-flow types that can appear within set field actions and
2616 * that have a mapping to compatible action types. These struct mf_field
2617 * definitions have a defined OXM or NXM header value and specify the field as
2620 set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2621 enum ofp_version ofp_version,
2624 switch ((int) sf->field->id) {
2625 case MFF_VLAN_TCI: {
2626 ovs_be16 tci = sf->value.be16;
2627 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2628 uint16_t vid = vlan_tci_to_vid(tci);
2629 uint8_t pcp = vlan_tci_to_pcp(tci);
2631 if (ofp_version < OFP11_VERSION) {
2632 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2634 * If CFI=1, Add or modify VLAN VID & PCP.
2635 * If CFI=0, strip VLAN header, if any.
2638 put_OFPAT10_SET_VLAN_VID(out, vid);
2639 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2641 put_OFPAT10_STRIP_VLAN(out);
2644 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2646 * If CFI=1, Add or modify VLAN VID & PCP.
2647 * OpenFlow 1.1 set actions only apply if the packet
2648 * already has VLAN tags. To be sure that is the case
2649 * we have to push a VLAN header. As we do not support
2650 * multiple layers of VLANs, this is a no-op, if a VLAN
2651 * header already exists. This may backfire, however,
2652 * when we start supporting multiple layers of VLANs.
2653 * If CFI=0, strip VLAN header, if any.
2656 /* Push a VLAN tag, if one was not seen at action validation
2658 if (!sf->flow_has_vlan) {
2659 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2661 put_OFPAT11_SET_VLAN_VID(out, vid);
2662 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2664 /* If the flow did not match on vlan, we have no way of
2665 * knowing if the vlan tag exists, so we must POP just to be
2667 put_OFPAT11_POP_VLAN(out);
2673 case MFF_VLAN_VID: {
2674 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2675 if (ofp_version == OFP10_VERSION) {
2676 put_OFPAT10_SET_VLAN_VID(out, vid);
2678 put_OFPAT11_SET_VLAN_VID(out, vid);
2684 if (ofp_version == OFP10_VERSION) {
2685 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2687 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2692 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
2696 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
2700 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2704 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2708 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2711 case MFF_IP_DSCP_SHIFTED:
2712 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2717 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2722 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2726 set_field_to_nxast(sf, out);
2732 set_field_to_set_field(const struct ofpact_set_field *sf,
2733 enum ofp_version ofp_version, struct ofpbuf *out)
2735 struct ofp12_action_set_field *oasf OVS_UNUSED;
2736 size_t start_ofs = out->size;
2738 oasf = put_OFPAT12_SET_FIELD(out);
2739 out->size = out->size - sizeof oasf->pad;
2740 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2741 pad_ofpat(out, start_ofs);
2745 encode_SET_FIELD(const struct ofpact_set_field *sf,
2746 enum ofp_version ofp_version, struct ofpbuf *out)
2748 if (ofp_version >= OFP15_VERSION) {
2749 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2751 set_field_to_set_field(sf, ofp_version, out);
2752 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2753 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2754 /* It came in as reg_load, send it out the same way. */
2755 set_field_to_nxast(sf, out);
2756 } else if (ofp_version < OFP12_VERSION) {
2757 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
2758 set_field_to_legacy_openflow(sf, ofp_version, out);
2759 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2760 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2761 * entire field, so encode it as OFPAT_SET_FIELD. */
2762 set_field_to_set_field(sf, ofp_version, out);
2764 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2765 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2766 * so encode it as reg_load. */
2767 set_field_to_nxast(sf, out);
2771 /* Parses the input argument 'arg' into the key, value, and delimiter
2772 * components that are common across the reg_load and set_field action format.
2774 * With an argument like "1->metadata", sets the following pointers to
2775 * point within 'arg':
2780 * Returns NULL if successful, otherwise a malloc()'d string describing the
2781 * error. The caller is responsible for freeing the returned string. */
2782 static char * OVS_WARN_UNUSED_RESULT
2783 set_field_split_str(char *arg, char **key, char **value, char **delim)
2788 value_end = strstr(arg, "->");
2789 *key = value_end + strlen("->");
2795 return xasprintf("%s: missing `->'", arg);
2797 if (strlen(value_end) <= strlen("->")) {
2798 return xasprintf("%s: missing field name following `->'", arg);
2804 /* Parses a "set_field" action with argument 'arg', appending the parsed
2805 * action to 'ofpacts'.
2807 * Returns NULL if successful, otherwise a malloc()'d string describing the
2808 * error. The caller is responsible for freeing the returned string. */
2809 static char * OVS_WARN_UNUSED_RESULT
2810 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2811 enum ofputil_protocol *usable_protocols)
2813 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2817 const struct mf_field *mf;
2820 error = set_field_split_str(arg, &key, &value, &delim);
2825 mf = mf_from_name(key);
2827 return xasprintf("%s is not a valid OXM field name", key);
2829 if (!mf->writable) {
2830 return xasprintf("%s is read-only", key);
2834 error = mf_parse(mf, value, &sf->value, &sf->mask);
2839 if (!mf_is_value_valid(mf, &sf->value)) {
2840 return xasprintf("%s is not a valid value for field %s", value, key);
2843 *usable_protocols &= mf->usable_protocols_exact;
2847 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
2848 * action to 'ofpacts'.
2850 * Returns NULL if successful, otherwise a malloc()'d string describing the
2851 * error. The caller is responsible for freeing the returned string. */
2852 static char * OVS_WARN_UNUSED_RESULT
2853 parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2854 enum ofputil_protocol *usable_protocols)
2856 char *copy = xstrdup(arg);
2857 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2862 static char * OVS_WARN_UNUSED_RESULT
2863 parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2865 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
2866 struct mf_subfield dst;
2867 char *key, *value_str;
2868 union mf_value value;
2871 error = set_field_split_str(arg, &key, &value_str, NULL);
2876 error = mf_parse_subfield(&dst, key);
2881 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2883 return xasprintf("%s: cannot parse integer value", arg);
2886 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2887 dst.field->n_bytes * 8 - dst.n_bits)) {
2891 mf_format(dst.field, &value, NULL, &ds);
2892 error = xasprintf("%s: value %s does not fit into %d bits",
2893 arg, ds_cstr(&ds), dst.n_bits);
2898 sf->field = dst.field;
2899 memset(&sf->value, 0, sizeof sf->value);
2900 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2901 dst.field->n_bytes, dst.ofs, dst.n_bits);
2902 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2908 format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2910 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2911 struct mf_subfield dst;
2914 dst.ofs = dst.n_bits = 0;
2915 while (next_load_segment(a, &dst, &value)) {
2916 ds_put_format(s, "%sload:%s%#"PRIx64"%s->%s",
2917 colors.special, colors.end, value,
2918 colors.special, colors.end);
2919 mf_format_subfield(&dst, s);
2920 ds_put_char(s, ',');
2924 ds_put_format(s, "%sset_field:%s", colors.special, colors.end);
2925 mf_format(a->field, &a->value, &a->mask, s);
2926 ds_put_format(s, "%s->%s%s",
2927 colors.special, colors.end, a->field->name);
2931 /* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2932 * is marked such that, if possible, it will be translated to OpenFlow as
2933 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2934 * that was the way that the action was expressed when it came into OVS or for
2935 * backward compatibility. */
2936 struct ofpact_set_field *
2937 ofpact_put_reg_load(struct ofpbuf *ofpacts)
2939 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2940 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2944 /* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2946 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2949 struct nx_action_stack {
2950 ovs_be16 type; /* OFPAT_VENDOR. */
2951 ovs_be16 len; /* Length is 16. */
2952 ovs_be32 vendor; /* NX_VENDOR_ID. */
2953 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2954 ovs_be16 offset; /* Bit offset into the field. */
2956 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2957 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2958 * - Enough 0-bytes to pad out the action to 24 bytes. */
2959 uint8_t pad[12]; /* See above. */
2961 OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
2964 decode_stack_action(const struct nx_action_stack *nasp,
2965 struct ofpact_stack *stack_action)
2967 stack_action->subfield.ofs = ntohs(nasp->offset);
2969 struct ofpbuf b = ofpbuf_const_initializer(nasp, sizeof *nasp);
2970 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
2971 enum ofperr error = nx_pull_header(&b, &stack_action->subfield.field,
2976 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
2978 if (!is_all_zeros(b.data, b.size)) {
2979 return OFPERR_NXBRC_MUST_BE_ZERO;
2986 decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
2987 enum ofp_version ofp_version OVS_UNUSED,
2988 struct ofpbuf *ofpacts)
2990 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
2991 enum ofperr error = decode_stack_action(nasp, push);
2992 return error ? error : nxm_stack_push_check(push, NULL);
2996 decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
2997 enum ofp_version ofp_version OVS_UNUSED,
2998 struct ofpbuf *ofpacts)
3000 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
3001 enum ofperr error = decode_stack_action(nasp, pop);
3002 return error ? error : nxm_stack_pop_check(pop, NULL);
3006 encode_STACK_op(const struct ofpact_stack *stack_action,
3007 struct nx_action_stack *nasp)
3012 nasp->offset = htons(stack_action->subfield.ofs);
3014 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
3015 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
3016 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
3017 n_bits = htons(stack_action->subfield.n_bits);
3018 ofpbuf_put(&b, &n_bits, sizeof n_bits);
3022 encode_STACK_PUSH(const struct ofpact_stack *stack,
3023 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3025 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
3029 encode_STACK_POP(const struct ofpact_stack *stack,
3030 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3032 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
3035 static char * OVS_WARN_UNUSED_RESULT
3036 parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
3037 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3039 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
3042 static char * OVS_WARN_UNUSED_RESULT
3043 parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
3044 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3046 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
3050 format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
3052 nxm_format_stack_push(a, s);
3056 format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
3058 nxm_format_stack_pop(a, s);
3061 /* Action structure for NXAST_DEC_TTL_CNT_IDS.
3063 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
3064 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
3065 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3066 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3068 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3069 * sent only to controllers with id 0.)
3071 struct nx_action_cnt_ids {
3072 ovs_be16 type; /* OFPAT_VENDOR. */
3073 ovs_be16 len; /* Length including slaves. */
3074 ovs_be32 vendor; /* NX_VENDOR_ID. */
3075 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
3077 ovs_be16 n_controllers; /* Number of controllers. */
3078 uint8_t zeros[4]; /* Must be zero. */
3080 /* Followed by 1 or more controller ids.
3082 * uint16_t cnt_ids[]; // Controller ids.
3083 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
3086 OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
3089 decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
3092 struct ofpact_cnt_ids *ids;
3093 enum ofperr error = 0;
3095 ids = ofpact_put_DEC_TTL(out);
3096 ids->n_controllers = 1;
3097 ofpbuf_put(out, &id, sizeof id);
3099 ofpact_finish_DEC_TTL(out, &ids);
3104 decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
3105 enum ofp_version ofp_version OVS_UNUSED,
3108 struct ofpact_cnt_ids *ids;
3112 ids = ofpact_put_DEC_TTL(out);
3113 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3114 ids->n_controllers = ntohs(nac_ids->n_controllers);
3115 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
3117 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
3118 return OFPERR_NXBRC_MUST_BE_ZERO;
3121 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
3122 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
3123 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
3124 "are required for %"PRIu16" controllers.",
3125 ids_size, ids->n_controllers * sizeof(ovs_be16),
3126 ids->n_controllers);
3127 return OFPERR_OFPBAC_BAD_LEN;
3130 for (i = 0; i < ids->n_controllers; i++) {
3131 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
3132 ofpbuf_put(out, &id, sizeof id);
3136 ofpact_finish_DEC_TTL(out, &ids);
3142 encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
3143 enum ofp_version ofp_version, struct ofpbuf *out)
3145 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
3146 || dec_ttl->n_controllers != 1
3147 || dec_ttl->cnt_ids[0] != 0) {
3148 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
3149 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
3153 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
3154 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
3156 ids = ofpbuf_put_zeros(out, ids_len);
3157 for (i = 0; i < dec_ttl->n_controllers; i++) {
3158 ids[i] = htons(dec_ttl->cnt_ids[i]);
3161 put_OFPAT_DEC_NW_TTL(out, ofp_version);
3166 parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
3168 struct ofpact_cnt_ids *ids;
3171 ofpact_put_DEC_TTL(ofpacts);
3172 ofpbuf_put(ofpacts, &id, sizeof id);
3173 ids = ofpacts->header;
3174 ids->n_controllers++;
3175 ofpact_finish_DEC_TTL(ofpacts, &ids);
3178 static char * OVS_WARN_UNUSED_RESULT
3179 parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
3180 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3183 parse_noargs_dec_ttl(ofpacts);
3185 struct ofpact_cnt_ids *ids;
3188 ids = ofpact_put_DEC_TTL(ofpacts);
3189 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3190 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
3191 cntr = strtok_r(NULL, ", ", &arg)) {
3192 uint16_t id = atoi(cntr);
3194 ofpbuf_put(ofpacts, &id, sizeof id);
3195 ids = ofpacts->header;
3196 ids->n_controllers++;
3198 if (!ids->n_controllers) {
3199 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3202 ofpact_finish_DEC_TTL(ofpacts, &ids);
3208 format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
3212 ds_put_format(s, "%sdec_ttl%s", colors.paren, colors.end);
3213 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
3214 ds_put_format(s, "%s(%s", colors.paren, colors.end);
3215 for (i = 0; i < a->n_controllers; i++) {
3217 ds_put_cstr(s, ",");
3219 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
3221 ds_put_format(s, "%s)%s", colors.paren, colors.end);
3225 /* Set MPLS label actions. */
3228 decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
3229 enum ofp_version ofp_version OVS_UNUSED,
3232 ofpact_put_SET_MPLS_LABEL(out)->label = label;
3237 encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3238 enum ofp_version ofp_version,
3241 if (ofp_version < OFP12_VERSION) {
3242 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3244 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
3245 ntohl(label->label));
3249 static char * OVS_WARN_UNUSED_RESULT
3250 parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3251 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3253 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3255 return xstrdup("set_mpls_label: expected label.");
3258 mpls_label->label = htonl(atoi(arg));
3263 format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
3265 ds_put_format(s, "%sset_mpls_label(%s%"PRIu32"%s)%s",
3266 colors.paren, colors.end, ntohl(a->label),
3267 colors.paren, colors.end);
3270 /* Set MPLS TC actions. */
3273 decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3274 enum ofp_version ofp_version OVS_UNUSED,
3277 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3282 encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3283 enum ofp_version ofp_version, struct ofpbuf *out)
3285 if (ofp_version < OFP12_VERSION) {
3286 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3288 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
3292 static char * OVS_WARN_UNUSED_RESULT
3293 parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3294 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3296 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
3299 return xstrdup("set_mpls_tc: expected tc.");
3302 mpls_tc->tc = atoi(arg);
3307 format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3309 ds_put_format(s, "%sset_mpls_ttl(%s%"PRIu8"%s)%s",
3310 colors.paren, colors.end, a->tc,
3311 colors.paren, colors.end);
3314 /* Set MPLS TTL actions. */
3317 decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3318 enum ofp_version ofp_version OVS_UNUSED,
3321 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3326 encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3327 enum ofp_version ofp_version, struct ofpbuf *out)
3329 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3332 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3333 * action to 'ofpacts'.
3335 * Returns NULL if successful, otherwise a malloc()'d string describing the
3336 * error. The caller is responsible for freeing the returned string. */
3337 static char * OVS_WARN_UNUSED_RESULT
3338 parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3339 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3341 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
3344 return xstrdup("set_mpls_ttl: expected ttl.");
3347 mpls_ttl->ttl = atoi(arg);
3352 format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3354 ds_put_format(s, "%sset_mpls_ttl(%s%"PRIu8"%s)%s",
3355 colors.paren, colors.end, a->ttl,
3356 colors.paren, colors.end);
3359 /* Decrement MPLS TTL actions. */
3362 decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3364 ofpact_put_DEC_MPLS_TTL(out);
3369 encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3370 enum ofp_version ofp_version, struct ofpbuf *out)
3372 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3375 static char * OVS_WARN_UNUSED_RESULT
3376 parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3377 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3379 ofpact_put_DEC_MPLS_TTL(ofpacts);
3384 format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3386 ds_put_format(s, "%sdec_mpls_ttl%s", colors.value, colors.end);
3389 /* Push MPLS label action. */
3392 decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3393 enum ofp_version ofp_version OVS_UNUSED,
3396 struct ofpact_push_mpls *oam;
3398 if (!eth_type_mpls(ethertype)) {
3399 return OFPERR_OFPBAC_BAD_ARGUMENT;
3401 oam = ofpact_put_PUSH_MPLS(out);
3402 oam->ethertype = ethertype;
3408 encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3409 enum ofp_version ofp_version, struct ofpbuf *out)
3411 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3414 static char * OVS_WARN_UNUSED_RESULT
3415 parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3416 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3421 error = str_to_u16(arg, "push_mpls", ðertype);
3423 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
3429 format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3431 ds_put_format(s, "%spush_mpls:%s0x%04"PRIx16,
3432 colors.param, colors.end, ntohs(a->ethertype));
3435 /* Pop MPLS label action. */
3438 decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3439 enum ofp_version ofp_version OVS_UNUSED,
3442 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3447 encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3448 enum ofp_version ofp_version, struct ofpbuf *out)
3450 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3453 static char * OVS_WARN_UNUSED_RESULT
3454 parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3455 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3460 error = str_to_u16(arg, "pop_mpls", ðertype);
3462 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
3468 format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
3470 ds_put_format(s, "%spop_mpls:%s0x%04"PRIx16,
3471 colors.param, colors.end, ntohs(a->ethertype));
3474 /* Set tunnel ID actions. */
3477 decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3478 enum ofp_version ofp_version OVS_UNUSED,
3481 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3482 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3483 tunnel->tun_id = tun_id;
3488 decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3489 enum ofp_version ofp_version OVS_UNUSED,
3492 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3493 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3494 tunnel->tun_id = tun_id;
3499 encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3500 enum ofp_version ofp_version, struct ofpbuf *out)
3502 uint64_t tun_id = tunnel->tun_id;
3504 if (ofp_version < OFP12_VERSION) {
3505 if (tun_id <= UINT32_MAX
3506 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3507 put_NXAST_SET_TUNNEL(out, tun_id);
3509 put_NXAST_SET_TUNNEL64(out, tun_id);
3512 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3516 static char * OVS_WARN_UNUSED_RESULT
3517 parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3518 enum ofp_raw_action_type raw)
3520 struct ofpact_tunnel *tunnel;
3522 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3523 tunnel->ofpact.raw = raw;
3524 return str_to_u64(arg, &tunnel->tun_id);
3527 static char * OVS_WARN_UNUSED_RESULT
3528 parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3529 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3531 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3535 format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3537 ds_put_format(s, "%sset_tunnel%s:%s%#"PRIx64, colors.param,
3538 (a->tun_id > UINT32_MAX
3539 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
3540 colors.end, a->tun_id);
3543 /* Set queue action. */
3546 decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3547 enum ofp_version ofp_version OVS_UNUSED,
3550 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3555 encode_SET_QUEUE(const struct ofpact_queue *queue,
3556 enum ofp_version ofp_version, struct ofpbuf *out)
3558 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3561 static char * OVS_WARN_UNUSED_RESULT
3562 parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3563 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3565 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3569 format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3571 ds_put_format(s, "%sset_queue:%s%"PRIu32,
3572 colors.param, colors.end, a->queue_id);
3575 /* Pop queue action. */
3578 decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3580 ofpact_put_POP_QUEUE(out);
3585 encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3586 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3588 put_NXAST_POP_QUEUE(out);
3591 static char * OVS_WARN_UNUSED_RESULT
3592 parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3593 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3595 ofpact_put_POP_QUEUE(ofpacts);
3600 format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3602 ds_put_format(s, "%spop_queue%s", colors.value, colors.end);
3605 /* Action structure for NXAST_FIN_TIMEOUT.
3607 * This action changes the idle timeout or hard timeout, or both, of this
3608 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3609 * When such a packet is observed, the action reduces the rule's idle timeout
3610 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3611 * action has no effect on an existing timeout that is already shorter than the
3612 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3613 * of zero has no effect on the respective timeout.
3615 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3616 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3617 * receipt of the FIN or RST.
3619 * This is useful for quickly discarding learned TCP flows that otherwise will
3620 * take a long time to expire.
3622 * This action is intended for use with an OpenFlow rule that matches only a
3623 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3624 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3625 * RST in any of those flows will cause the entire OpenFlow rule to expire
3626 * early, which is not normally desirable.
3628 struct nx_action_fin_timeout {
3629 ovs_be16 type; /* OFPAT_VENDOR. */
3630 ovs_be16 len; /* 16. */
3631 ovs_be32 vendor; /* NX_VENDOR_ID. */
3632 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3633 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3634 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3635 ovs_be16 pad; /* Must be zero. */
3637 OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
3640 decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
3641 enum ofp_version ofp_version OVS_UNUSED,
3644 struct ofpact_fin_timeout *oft;
3646 oft = ofpact_put_FIN_TIMEOUT(out);
3647 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3648 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3653 encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3654 enum ofp_version ofp_version OVS_UNUSED,
3657 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3658 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3659 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3662 static char * OVS_WARN_UNUSED_RESULT
3663 parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3664 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3666 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3669 while (ofputil_parse_key_value(&arg, &key, &value)) {
3672 if (!strcmp(key, "idle_timeout")) {
3673 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3674 } else if (!strcmp(key, "hard_timeout")) {
3675 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3677 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3689 format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3691 ds_put_format(s, "%sfin_timeout(%s", colors.paren, colors.end);
3692 if (a->fin_idle_timeout) {
3693 ds_put_format(s, "%sidle_timeout=%s%"PRIu16",",
3694 colors.param, colors.end, a->fin_idle_timeout);
3696 if (a->fin_hard_timeout) {
3697 ds_put_format(s, "%shard_timeout=%s%"PRIu16",",
3698 colors.param, colors.end, a->fin_hard_timeout);
3701 ds_put_format(s, "%s)%s", colors.paren, colors.end);
3704 /* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
3706 * These actions search one of the switch's flow tables:
3708 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3709 * it specifies the table to search.
3711 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3712 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3713 * table, that is, the OpenFlow flow table that contains the flow from
3714 * which this action was obtained. If this action did not come from a
3715 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3716 * is the current table.
3718 * The flow table lookup uses a flow that may be slightly modified from the
3721 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3722 * is used as the flow's in_port.
3724 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3725 * then its value is used as the flow's in_port. Otherwise, the original
3728 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3729 * resubmit action, then the flow is updated with the new values.
3731 * Following the lookup, the original in_port is restored.
3733 * If the modified flow matched in the flow table, then the corresponding
3734 * actions are executed. Afterward, actions following the resubmit in the
3735 * original set of actions, if any, are executed; any changes made to the
3736 * packet (e.g. changes to VLAN) by secondary actions persist when those
3737 * actions are executed, although the original in_port is restored.
3739 * Resubmit actions may be used any number of times within a set of actions.
3741 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3742 * implementation-defined depth, further resubmit actions are simply ignored.
3744 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3745 * 'pad' to be all-bits-zero.
3747 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3748 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3750 struct nx_action_resubmit {
3751 ovs_be16 type; /* OFPAT_VENDOR. */
3752 ovs_be16 len; /* Length is 16. */
3753 ovs_be32 vendor; /* NX_VENDOR_ID. */
3754 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3755 ovs_be16 in_port; /* New in_port for checking flow table. */
3756 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3759 OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3762 decode_NXAST_RAW_RESUBMIT(uint16_t port,
3763 enum ofp_version ofp_version OVS_UNUSED,
3766 struct ofpact_resubmit *resubmit;
3768 resubmit = ofpact_put_RESUBMIT(out);
3769 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3770 resubmit->in_port = u16_to_ofp(port);
3771 resubmit->table_id = 0xff;
3776 decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
3777 enum ofp_version ofp_version OVS_UNUSED,
3780 struct ofpact_resubmit *resubmit;
3782 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3783 return OFPERR_OFPBAC_BAD_ARGUMENT;
3786 resubmit = ofpact_put_RESUBMIT(out);
3787 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3788 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3789 resubmit->table_id = nar->table;
3794 encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3795 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3797 uint16_t in_port = ofp_to_u16(resubmit->in_port);
3799 if (resubmit->table_id == 0xff
3800 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3801 put_NXAST_RESUBMIT(out, in_port);
3803 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3804 nar->table = resubmit->table_id;
3805 nar->in_port = htons(in_port);
3809 static char * OVS_WARN_UNUSED_RESULT
3810 parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3811 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3813 struct ofpact_resubmit *resubmit;
3814 char *in_port_s, *table_s;
3816 resubmit = ofpact_put_RESUBMIT(ofpacts);
3818 in_port_s = strsep(&arg, ",");
3819 if (in_port_s && in_port_s[0]) {
3820 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3821 return xasprintf("%s: resubmit to unknown port", in_port_s);
3824 resubmit->in_port = OFPP_IN_PORT;
3827 table_s = strsep(&arg, ",");
3828 if (table_s && table_s[0]) {
3829 uint32_t table_id = 0;
3832 error = str_to_u32(table_s, &table_id);
3836 resubmit->table_id = table_id;
3838 resubmit->table_id = 255;
3841 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3842 return xstrdup("at least one \"in_port\" or \"table\" must be "
3843 "specified on resubmit");
3849 format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3851 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
3852 ds_put_format(s, "%sresubmit:%s", colors.special, colors.end);
3853 ofputil_format_port(a->in_port, s);
3855 ds_put_format(s, "%sresubmit(%s", colors.paren, colors.end);
3856 if (a->in_port != OFPP_IN_PORT) {
3857 ofputil_format_port(a->in_port, s);
3859 ds_put_char(s, ',');
3860 if (a->table_id != 255) {
3861 ds_put_format(s, "%"PRIu8, a->table_id);
3863 ds_put_format(s, "%s)%s", colors.paren, colors.end);
3867 /* Action structure for NXAST_LEARN.
3869 * This action adds or modifies a flow in an OpenFlow table, similar to
3870 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3871 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3872 * flow's match criteria and actions are built by applying each of the series
3873 * of flow_mod_spec elements included as part of the action.
3875 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3876 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3877 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3878 * bits from a source to a destination. In this case, the header contains
3881 * 15 14 13 12 11 10 0
3882 * +------+---+------+---------------------------------+
3883 * | 0 |src| dst | n_bits |
3884 * +------+---+------+---------------------------------+
3886 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3887 * following table summarizes the meaning of each possible combination.
3888 * Details follow the table:
3891 * --- --- ----------------------------------------------------------
3892 * 0 0 Add match criteria based on value in a field.
3893 * 1 0 Add match criteria based on an immediate value.
3894 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3895 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3896 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3897 * All other combinations are undefined and not allowed.
3899 * The flow_mod_spec header is followed by a source specification and a
3900 * destination specification. The format and meaning of the source
3901 * specification depends on 'src':
3903 * - If 'src' is 0, the source bits are taken from a field in the flow to
3904 * which this action is attached. (This should be a wildcarded field. If
3905 * its value is fully specified then the source bits being copied have
3908 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3909 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3910 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3911 * 'field' and 'ofs' are subject to the same restrictions as the source
3912 * field in NXAST_REG_MOVE.
3914 * - If 'src' is 1, the source bits are a constant value. The source
3915 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3916 * number in network order, the source bits are the 'n_bits'
3917 * least-significant bits. The switch will report an error if other bits
3918 * in the constant are nonzero.
3920 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3921 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3922 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3923 * meaning of the flow_mod_spec depends on 'dst':
3925 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3926 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3927 * packet equal the source bits. 'field' may be any nxm_header with
3928 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3930 * Order is significant. Earlier flow_mod_specs must satisfy any
3931 * prerequisites for matching fields specified later, by copying constant
3932 * values into prerequisite fields.
3934 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3937 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3938 * the new flow. The new flow copies the source bits into
3939 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3942 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3943 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3945 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3946 * empty. It has the following meaning:
3948 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3949 * The new flow outputs to the OpenFlow port specified by the source field.
3950 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3951 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3954 * Resource Management
3955 * -------------------
3957 * A switch has a finite amount of flow table space available for learning.
3958 * When this space is exhausted, no new learning table entries will be learned
3959 * until some existing flow table entries expire. The controller should be
3960 * prepared to handle this by flooding (which can be implemented as a
3961 * low-priority flow).
3963 * If a learned flow matches a single TCP stream with a relatively long
3964 * timeout, one may make the best of resource constraints by setting
3965 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3966 * both, to shorter timeouts. When either of these is specified as a nonzero
3967 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3973 * The following examples give a prose description of the flow_mod_specs along
3974 * with informal notation for how those would be represented and a hex dump of
3975 * the bytes that would be required.
3977 * These examples could work with various nx_action_learn parameters. Typical
3978 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3979 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3981 * 1. Learn input port based on the source MAC, with lookup into
3982 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3984 * Match on in_port=99:
3985 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3986 * ovs_be16(99), 00 63
3987 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3989 * Match Ethernet destination on Ethernet source from packet:
3990 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3991 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3992 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3994 * Set NXM_NX_REG1[16:31] to the packet's input port:
3995 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3996 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3997 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3999 * Given a packet that arrived on port A with Ethernet source address B,
4000 * this would set up the flow "in_port=99, dl_dst=B,
4001 * actions=load:A->NXM_NX_REG1[16..31]".
4003 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
4004 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4005 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
4007 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
4008 * into NXM_NX_REG1[16:31]:
4010 * Match on same VLAN ID as packet:
4011 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
4012 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4013 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4015 * Match Ethernet destination on Ethernet source from packet:
4016 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4017 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4018 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4020 * Output to the packet's input port:
4021 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
4022 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4024 * Given a packet that arrived on port A with Ethernet source address B in
4025 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
4026 * actions=output:A".
4028 * In syntax accepted by ovs-ofctl, this action is:
4029 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4030 * output:NXM_OF_IN_PORT[])
4032 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
4033 * 10-second MAC expiration time to make it easier to see what's going on
4035 * ovs-vsctl del-controller br0
4036 * ovs-ofctl del-flows br0
4037 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
4038 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
4039 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
4040 output:NXM_OF_IN_PORT[]), resubmit(,1)"
4041 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
4043 * You can then dump the MAC learning table with:
4045 * ovs-ofctl dump-flows br0 table=1
4050 * For best performance, segregate learned flows into a table that is not used
4051 * for any other flows except possibly for a lowest-priority "catch-all" flow
4052 * (a flow with no match criteria). If different learning actions specify
4053 * different match criteria, use different tables for the learned flows.
4055 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
4056 * These timeouts apply to the flow that is added, which means that a flow with
4057 * an idle timeout will expire when no traffic has been sent *to* the learned
4058 * address. This is not usually the intent in MAC learning; instead, we want
4059 * the MAC learn entry to expire when no traffic has been sent *from* the
4060 * learned address. Use a hard timeout for that.
4063 * Visibility of Changes
4064 * ---------------------
4066 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
4067 * flow translation are visible to flow table lookups made later in the flow
4068 * translation. This means that, in the example above, a MAC learned by the
4069 * learn action in table 0 would be found in table 1 (if the packet being
4070 * processed had the same source and destination MAC address).
4072 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
4073 * modify a flow) by a "learn" action are visible only for later flow
4074 * translations, not for later lookups within the same flow translation. In
4075 * the MAC learning example, a MAC learned by the learn action in table 0 would
4076 * not be found in table 1 if the flow translation would resubmit to table 1
4077 * after the processing of the learn action, meaning that if this MAC had not
4078 * been learned before then the packet would be flooded. */
4079 struct nx_action_learn {
4080 ovs_be16 type; /* OFPAT_VENDOR. */
4081 ovs_be16 len; /* At least 24. */
4082 ovs_be32 vendor; /* NX_VENDOR_ID. */
4083 ovs_be16 subtype; /* NXAST_LEARN. */
4084 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
4085 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
4086 ovs_be16 priority; /* Priority level of flow entry. */
4087 ovs_be64 cookie; /* Cookie for new flow. */
4088 ovs_be16 flags; /* NX_LEARN_F_*. */
4089 uint8_t table_id; /* Table to insert flow entry. */
4090 uint8_t pad; /* Must be zero. */
4091 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
4092 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
4093 /* Followed by a sequence of flow_mod_spec elements, as described above,
4094 * until the end of the action is reached. */
4096 OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
4099 get_be16(const void **pp)
4101 const ovs_be16 *p = *pp;
4102 ovs_be16 value = *p;
4108 get_be32(const void **pp)
4110 const ovs_be32 *p = *pp;
4111 ovs_be32 value = get_unaligned_be32(p);
4117 get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
4119 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
4120 sf->ofs = ntohs(get_be16(p));
4121 sf->n_bits = n_bits;
4125 learn_min_len(uint16_t header)
4127 int n_bits = header & NX_LEARN_N_BITS_MASK;
4128 int src_type = header & NX_LEARN_SRC_MASK;
4129 int dst_type = header & NX_LEARN_DST_MASK;
4130 unsigned int min_len;
4133 if (src_type == NX_LEARN_SRC_FIELD) {
4134 min_len += sizeof(ovs_be32); /* src_field */
4135 min_len += sizeof(ovs_be16); /* src_ofs */
4137 min_len += DIV_ROUND_UP(n_bits, 16);
4139 if (dst_type == NX_LEARN_DST_MATCH ||
4140 dst_type == NX_LEARN_DST_LOAD) {
4141 min_len += sizeof(ovs_be32); /* dst_field */
4142 min_len += sizeof(ovs_be16); /* dst_ofs */
4147 /* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
4148 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
4150 decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
4151 enum ofp_version ofp_version OVS_UNUSED,
4152 struct ofpbuf *ofpacts)
4154 struct ofpact_learn *learn;
4155 const void *p, *end;
4158 return OFPERR_OFPBAC_BAD_ARGUMENT;
4161 learn = ofpact_put_LEARN(ofpacts);
4163 learn->idle_timeout = ntohs(nal->idle_timeout);
4164 learn->hard_timeout = ntohs(nal->hard_timeout);
4165 learn->priority = ntohs(nal->priority);
4166 learn->cookie = nal->cookie;
4167 learn->table_id = nal->table_id;
4168 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
4169 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
4171 learn->flags = ntohs(nal->flags);
4172 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
4173 NX_LEARN_F_DELETE_LEARNED)) {
4174 return OFPERR_OFPBAC_BAD_ARGUMENT;
4177 if (learn->table_id == 0xff) {
4178 return OFPERR_OFPBAC_BAD_ARGUMENT;
4181 end = (char *) nal + ntohs(nal->len);
4182 for (p = nal + 1; p != end; ) {
4183 struct ofpact_learn_spec *spec;
4184 uint16_t header = ntohs(get_be16(&p));
4190 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
4191 learn = ofpacts->header;
4194 spec->src_type = header & NX_LEARN_SRC_MASK;
4195 spec->dst_type = header & NX_LEARN_DST_MASK;
4196 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
4198 /* Check for valid src and dst type combination. */
4199 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4200 spec->dst_type == NX_LEARN_DST_LOAD ||
4201 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
4202 spec->src_type == NX_LEARN_SRC_FIELD)) {
4205 return OFPERR_OFPBAC_BAD_ARGUMENT;
4208 /* Check that the arguments don't overrun the end of the action. */
4209 if ((char *) end - (char *) p < learn_min_len(header)) {
4210 return OFPERR_OFPBAC_BAD_LEN;
4213 /* Get the source. */
4214 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4215 get_subfield(spec->n_bits, &p, &spec->src);
4217 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4219 bitwise_copy(p, p_bytes, 0,
4220 &spec->src_imm, sizeof spec->src_imm, 0,
4222 p = (const uint8_t *) p + p_bytes;
4225 /* Get the destination. */
4226 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4227 spec->dst_type == NX_LEARN_DST_LOAD) {
4228 get_subfield(spec->n_bits, &p, &spec->dst);
4231 ofpact_finish_LEARN(ofpacts, &learn);
4233 if (!is_all_zeros(p, (char *) end - (char *) p)) {
4234 return OFPERR_OFPBAC_BAD_ARGUMENT;
4241 put_be16(struct ofpbuf *b, ovs_be16 x)
4243 ofpbuf_put(b, &x, sizeof x);
4247 put_be32(struct ofpbuf *b, ovs_be32 x)
4249 ofpbuf_put(b, &x, sizeof x);
4253 put_u16(struct ofpbuf *b, uint16_t x)
4255 put_be16(b, htons(x));
4259 put_u32(struct ofpbuf *b, uint32_t x)
4261 put_be32(b, htonl(x));
4265 encode_LEARN(const struct ofpact_learn *learn,
4266 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4268 const struct ofpact_learn_spec *spec;
4269 struct nx_action_learn *nal;
4272 start_ofs = out->size;
4273 nal = put_NXAST_LEARN(out);
4274 nal->idle_timeout = htons(learn->idle_timeout);
4275 nal->hard_timeout = htons(learn->hard_timeout);
4276 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4277 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4278 nal->priority = htons(learn->priority);
4279 nal->cookie = learn->cookie;
4280 nal->flags = htons(learn->flags);
4281 nal->table_id = learn->table_id;
4283 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4284 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4286 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4287 put_u32(out, mf_nxm_header(spec->src.field->id));
4288 put_u16(out, spec->src.ofs);
4290 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4291 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4292 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4293 bits, n_dst_bytes, 0,
4297 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4298 spec->dst_type == NX_LEARN_DST_LOAD) {
4299 put_u32(out, mf_nxm_header(spec->dst.field->id));
4300 put_u16(out, spec->dst.ofs);
4304 pad_ofpat(out, start_ofs);
4307 static char * OVS_WARN_UNUSED_RESULT
4308 parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4309 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4311 return learn_parse(arg, ofpacts);
4315 format_LEARN(const struct ofpact_learn *a, struct ds *s)
4320 /* Action structure for NXAST_CONJUNCTION. */
4321 struct nx_action_conjunction {
4322 ovs_be16 type; /* OFPAT_VENDOR. */
4323 ovs_be16 len; /* At least 16. */
4324 ovs_be32 vendor; /* NX_VENDOR_ID. */
4325 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4330 OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4333 add_conjunction(struct ofpbuf *out,
4334 uint32_t id, uint8_t clause, uint8_t n_clauses)
4336 struct ofpact_conjunction *oc;
4338 oc = ofpact_put_CONJUNCTION(out);
4340 oc->clause = clause;
4341 oc->n_clauses = n_clauses;
4345 decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
4346 enum ofp_version ofp_version OVS_UNUSED,
4349 if (nac->n_clauses < 2 || nac->n_clauses > 64
4350 || nac->clause >= nac->n_clauses) {
4351 return OFPERR_NXBAC_BAD_CONJUNCTION;
4353 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4359 encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4360 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4362 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4363 nac->clause = oc->clause;
4364 nac->n_clauses = oc->n_clauses;
4365 nac->id = htonl(oc->id);
4369 format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4371 ds_put_format(s, "%sconjunction(%s%"PRIu32",%"PRIu8"/%"PRIu8"%s)%s",
4372 colors.paren, colors.end,
4373 oc->id, oc->clause + 1, oc->n_clauses,
4374 colors.paren, colors.end);
4377 static char * OVS_WARN_UNUSED_RESULT
4378 parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4379 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4386 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4387 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4388 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4391 if (n_clauses < 2) {
4392 return xstrdup("conjunction must have at least 2 clauses");
4393 } else if (n_clauses > 64) {
4394 return xstrdup("conjunction must have at most 64 clauses");
4395 } else if (clause < 1) {
4396 return xstrdup("clause index must be positive");
4397 } else if (clause > n_clauses) {
4398 return xstrdup("clause index must be less than or equal to "
4399 "number of clauses");
4402 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4406 /* Action structure for NXAST_MULTIPATH.
4408 * This action performs the following steps in sequence:
4410 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4411 * Refer to the definition of "enum nx_mp_fields" for details.
4413 * The 'basis' value is used as a universal hash parameter, that is,
4414 * different values of 'basis' yield different hash functions. The
4415 * particular universal hash function used is implementation-defined.
4417 * The hashed fields' values are drawn from the current state of the
4418 * flow, including all modifications that have been made by actions up to
4421 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4422 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4425 * The output of the algorithm is 'link', an unsigned integer less than
4426 * or equal to 'max_link'.
4428 * Some algorithms use 'arg' as an additional argument.
4430 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4431 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4434 * The switch will reject actions that have an unknown 'fields', or an unknown
4435 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4436 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4437 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4439 struct nx_action_multipath {
4440 ovs_be16 type; /* OFPAT_VENDOR. */
4441 ovs_be16 len; /* Length is 32. */
4442 ovs_be32 vendor; /* NX_VENDOR_ID. */
4443 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4445 /* What fields to hash and how. */
4446 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4447 ovs_be16 basis; /* Universal hash parameter. */
4450 /* Multipath link choice algorithm to apply to hash value. */
4451 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4452 ovs_be16 max_link; /* Number of output links, minus 1. */
4453 ovs_be32 arg; /* Algorithm-specific argument. */
4456 /* Where to store the result. */
4457 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4458 ovs_be32 dst; /* Destination. */
4460 OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4463 decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
4464 enum ofp_version ofp_version OVS_UNUSED,
4467 uint32_t n_links = ntohs(nam->max_link) + 1;
4468 size_t min_n_bits = log_2_ceil(n_links);
4469 struct ofpact_multipath *mp;
4471 mp = ofpact_put_MULTIPATH(out);
4472 mp->fields = ntohs(nam->fields);
4473 mp->basis = ntohs(nam->basis);
4474 mp->algorithm = ntohs(nam->algorithm);
4475 mp->max_link = ntohs(nam->max_link);
4476 mp->arg = ntohl(nam->arg);
4477 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4478 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4479 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4481 if (!flow_hash_fields_valid(mp->fields)) {
4482 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4483 return OFPERR_OFPBAC_BAD_ARGUMENT;
4484 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4485 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4486 && mp->algorithm != NX_MP_ALG_HRW
4487 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4488 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4489 return OFPERR_OFPBAC_BAD_ARGUMENT;
4490 } else if (mp->dst.n_bits < min_n_bits) {
4491 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4492 "%"PRIu32" links", min_n_bits, n_links);
4493 return OFPERR_OFPBAC_BAD_ARGUMENT;
4496 return multipath_check(mp, NULL);
4500 encode_MULTIPATH(const struct ofpact_multipath *mp,
4501 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4503 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4505 nam->fields = htons(mp->fields);
4506 nam->basis = htons(mp->basis);
4507 nam->algorithm = htons(mp->algorithm);
4508 nam->max_link = htons(mp->max_link);
4509 nam->arg = htonl(mp->arg);
4510 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
4511 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
4514 static char * OVS_WARN_UNUSED_RESULT
4515 parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4516 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4518 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4522 format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4524 multipath_format(a, s);
4527 /* Action structure for NXAST_NOTE.
4529 * This action has no effect. It is variable length. The switch does not
4530 * attempt to interpret the user-defined 'note' data in any way. A controller
4531 * can use this action to attach arbitrary metadata to a flow.
4533 * This action might go away in the future.
4535 struct nx_action_note {
4536 ovs_be16 type; /* OFPAT_VENDOR. */
4537 ovs_be16 len; /* A multiple of 8, but at least 16. */
4538 ovs_be32 vendor; /* NX_VENDOR_ID. */
4539 ovs_be16 subtype; /* NXAST_NOTE. */
4540 uint8_t note[6]; /* Start of user-defined data. */
4541 /* Possibly followed by additional user-defined data. */
4543 OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4546 decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4547 enum ofp_version ofp_version OVS_UNUSED,
4550 struct ofpact_note *note;
4551 unsigned int length;
4553 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
4554 note = ofpact_put_NOTE(out);
4555 note->length = length;
4556 ofpbuf_put(out, nan->note, length);
4558 ofpact_finish_NOTE(out, ¬e);
4564 encode_NOTE(const struct ofpact_note *note,
4565 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4567 size_t start_ofs = out->size;
4568 struct nx_action_note *nan;
4570 put_NXAST_NOTE(out);
4571 out->size = out->size - sizeof nan->note;
4573 ofpbuf_put(out, note->data, note->length);
4574 pad_ofpat(out, start_ofs);
4577 static char * OVS_WARN_UNUSED_RESULT
4578 parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4579 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4581 size_t start_ofs = ofpacts->size;
4582 ofpact_put_NOTE(ofpacts);
4583 arg = ofpbuf_put_hex(ofpacts, arg, NULL);
4585 return xstrdup("bad hex digit in `note' argument");
4587 struct ofpact_note *note = ofpbuf_at_assert(ofpacts, start_ofs,
4589 note->length = ofpacts->size - (start_ofs + sizeof *note);
4590 ofpact_finish_NOTE(ofpacts, ¬e);
4595 format_NOTE(const struct ofpact_note *a, struct ds *s)
4597 ds_put_format(s, "%snote:%s", colors.param, colors.end);
4598 format_hex_arg(s, a->data, a->length);
4604 decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4606 ofpact_put_EXIT(out);
4611 encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4612 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4614 put_NXAST_EXIT(out);
4617 static char * OVS_WARN_UNUSED_RESULT
4618 parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4619 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4621 ofpact_put_EXIT(ofpacts);
4626 format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4628 ds_put_format(s, "%sexit%s", colors.special, colors.end);
4631 /* Unroll xlate action. */
4634 encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4635 enum ofp_version ofp_version OVS_UNUSED,
4636 struct ofpbuf *out OVS_UNUSED)
4641 static char * OVS_WARN_UNUSED_RESULT
4642 parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4643 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4650 format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a, struct ds *s)
4652 ds_put_format(s, "%sunroll_xlate(%s%stable=%s%"PRIu8
4653 ", %scookie=%s%"PRIu64"%s)%s",
4654 colors.paren, colors.end,
4655 colors.special, colors.end, a->rule_table_id,
4656 colors.param, colors.end, ntohll(a->rule_cookie),
4657 colors.paren, colors.end);
4660 /* Action structure for NXAST_SAMPLE.
4662 * Samples matching packets with the given probability and sends them
4663 * each to the set of collectors identified with the given ID. The
4664 * probability is expressed as a number of packets to be sampled out
4665 * of USHRT_MAX packets, and must be >0.
4667 * When sending packet samples to IPFIX collectors, the IPFIX flow
4668 * record sent for each sampled packet is associated with the given
4669 * observation domain ID and observation point ID. Each IPFIX flow
4670 * record contain the sampled packet's headers when executing this
4671 * rule. If a sampled packet's headers are modified by previous
4672 * actions in the flow, those modified headers are sent. */
4673 struct nx_action_sample {
4674 ovs_be16 type; /* OFPAT_VENDOR. */
4675 ovs_be16 len; /* Length is 24. */
4676 ovs_be32 vendor; /* NX_VENDOR_ID. */
4677 ovs_be16 subtype; /* NXAST_SAMPLE. */
4678 ovs_be16 probability; /* Fraction of packets to sample. */
4679 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4680 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4681 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4683 OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4686 decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4687 enum ofp_version ofp_version OVS_UNUSED,
4690 struct ofpact_sample *sample;
4692 sample = ofpact_put_SAMPLE(out);
4693 sample->probability = ntohs(nas->probability);
4694 sample->collector_set_id = ntohl(nas->collector_set_id);
4695 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4696 sample->obs_point_id = ntohl(nas->obs_point_id);
4698 if (sample->probability == 0) {
4699 return OFPERR_OFPBAC_BAD_ARGUMENT;
4706 encode_SAMPLE(const struct ofpact_sample *sample,
4707 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4709 struct nx_action_sample *nas;
4711 nas = put_NXAST_SAMPLE(out);
4712 nas->probability = htons(sample->probability);
4713 nas->collector_set_id = htonl(sample->collector_set_id);
4714 nas->obs_domain_id = htonl(sample->obs_domain_id);
4715 nas->obs_point_id = htonl(sample->obs_point_id);
4718 /* Parses 'arg' as the argument to a "sample" action, and appends such an
4719 * action to 'ofpacts'.
4721 * Returns NULL if successful, otherwise a malloc()'d string describing the
4722 * error. The caller is responsible for freeing the returned string. */
4723 static char * OVS_WARN_UNUSED_RESULT
4724 parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4725 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4727 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4730 while (ofputil_parse_key_value(&arg, &key, &value)) {
4733 if (!strcmp(key, "probability")) {
4734 error = str_to_u16(value, "probability", &os->probability);
4735 if (!error && os->probability == 0) {
4736 error = xasprintf("invalid probability value \"%s\"", value);
4738 } else if (!strcmp(key, "collector_set_id")) {
4739 error = str_to_u32(value, &os->collector_set_id);
4740 } else if (!strcmp(key, "obs_domain_id")) {
4741 error = str_to_u32(value, &os->obs_domain_id);
4742 } else if (!strcmp(key, "obs_point_id")) {
4743 error = str_to_u32(value, &os->obs_point_id);
4745 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4752 if (os->probability == 0) {
4753 return xstrdup("non-zero \"probability\" must be specified on sample");
4759 format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4761 ds_put_format(s, "%ssample(%s%sprobability=%s%"PRIu16
4762 ",%scollector_set_id=%s%"PRIu32
4763 ",%sobs_domain_id=%s%"PRIu32
4764 ",%sobs_point_id=%s%"PRIu32"%s)%s",
4765 colors.paren, colors.end,
4766 colors.param, colors.end, a->probability,
4767 colors.param, colors.end, a->collector_set_id,
4768 colors.param, colors.end, a->obs_domain_id,
4769 colors.param, colors.end, a->obs_point_id,
4770 colors.paren, colors.end);
4773 /* debug_recirc instruction. */
4775 static bool enable_debug;
4778 ofpact_dummy_enable(void)
4780 enable_debug = true;
4784 decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4786 if (!enable_debug) {
4787 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4790 ofpact_put_DEBUG_RECIRC(out);
4795 encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4796 enum ofp_version ofp_version OVS_UNUSED,
4799 put_NXAST_DEBUG_RECIRC(out);
4802 static char * OVS_WARN_UNUSED_RESULT
4803 parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4804 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4806 ofpact_put_DEBUG_RECIRC(ofpacts);
4811 format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4813 ds_put_format(s, "%sdebug_recirc%s", colors.value, colors.end);
4816 /* Action structure for NXAST_CT.
4818 * Pass traffic to the connection tracker.
4820 * There are two important concepts to understanding the connection tracking
4821 * interface: Packet state and Connection state. Packets may be "Untracked" or
4822 * "Tracked". Connections may be "Uncommitted" or "Committed".
4826 * Untracked packets have not yet passed through the connection tracker,
4827 * and the connection state for such packets is unknown. In most cases,
4828 * packets entering the OpenFlow pipeline will initially be in the
4829 * untracked state. Untracked packets may become tracked by executing
4830 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4831 * about the connection available, in particular the connection state.
4833 * Tracked packets have previously passed through the connection tracker.
4834 * These packets will remain tracked through until the end of the OpenFlow
4835 * pipeline. Tracked packets which have NXAST_CT executed with a
4836 * "recirc_table" specified will return to the tracked state.
4838 * The packet state is only significant for the duration of packet
4839 * processing within the OpenFlow pipeline.
4841 * - Connection State:
4843 * Multiple packets may be associated with a single connection. Initially,
4844 * all connections are uncommitted. The connection state corresponding to
4845 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4847 * Uncommitted connections have no state stored about them. Uncommitted
4848 * connections may transition into the committed state by executing
4849 * NXAST_CT with the NX_CT_F_COMMIT flag.
4851 * Once a connection becomes committed, information may be gathered about
4852 * the connection by passing subsequent packets through the connection
4853 * tracker, and the state of the connection will be stored beyond the
4854 * lifetime of packet processing.
4856 * Connections may transition back into the uncommitted state due to
4857 * external timers, or due to the contents of packets that are sent to the
4858 * connection tracker. This behaviour is outside of the scope of the
4859 * OpenFlow interface.
4861 * The "zone" specifies a context within which the tracking is done:
4863 * The connection tracking zone is a 16-bit number. Each zone is an
4864 * independent connection tracking context. The connection state for each
4865 * connection is completely separate for each zone, so if a connection
4866 * is committed to zone A, then it will remain uncommitted in zone B.
4867 * If NXAST_CT is executed with the same zone multiple times, later
4868 * executions have no effect.
4870 * If 'zone_src' is nonzero, this specifies that the zone should be
4871 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4872 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4873 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4874 * are the same as the acceptable nxm_header values for the 'src' field of
4877 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4878 * connection tracking zone.
4880 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4882 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4883 * packet will be logically cloned prior to executing this action. One
4884 * copy will be sent to the connection tracker, then will be re-injected
4885 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
4886 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4887 * fields will be populated. The original instance of the packet will
4888 * continue the current actions list. This can be thought of as similar to
4889 * the effect of the "output" action: One copy is sent out (in this case,
4890 * to the connection tracker), but the current copy continues processing.
4892 * It is strongly recommended that this table is later than the current
4893 * table, to prevent loops.
4895 * The "alg" attaches protocol-specific behaviour to this action:
4897 * The ALG is a 16-bit number which specifies that additional
4898 * processing should be applied to this traffic.
4900 * Protocol | Value | Meaning
4901 * --------------------------------------------------------------------
4902 * None | 0 | No protocol-specific behaviour.
4903 * FTP | 21 | Parse FTP control connections and observe the
4904 * | | negotiation of related data connections.
4905 * Other | Other | Unsupported protocols.
4907 * By way of example, if FTP control connections have this action applied
4908 * with the ALG set to FTP (21), then the connection tracker will observe
4909 * the negotiation of data connections. This allows the connection
4910 * tracker to identify subsequent data connections as "related" to this
4911 * existing connection. The "related" flag will be populated in the
4912 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
4915 * Zero or more actions may immediately follow this action. These actions will
4916 * be executed within the context of the connection tracker, and they require
4917 * the NX_CT_F_COMMIT flag to be set.
4919 struct nx_action_conntrack {
4920 ovs_be16 type; /* OFPAT_VENDOR. */
4921 ovs_be16 len; /* At least 24. */
4922 ovs_be32 vendor; /* NX_VENDOR_ID. */
4923 ovs_be16 subtype; /* NXAST_CT. */
4924 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
4925 * Unspecified flag bits must be zero. */
4926 ovs_be32 zone_src; /* Connection tracking context. */
4928 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
4929 ovs_be16 zone_imm; /* Immediate value for zone. */
4931 uint8_t recirc_table; /* Recirculate to a specific table, or
4932 NX_CT_RECIRC_NONE for no recirculation. */
4933 uint8_t pad[3]; /* Zeroes */
4934 ovs_be16 alg; /* Well-known port number for the protocol.
4935 * 0 indicates no ALG is required. */
4936 /* Followed by a sequence of zero or more OpenFlow actions. The length of
4937 * these is included in 'len'. */
4939 OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
4942 decode_ct_zone(const struct nx_action_conntrack *nac,
4943 struct ofpact_conntrack *out)
4945 if (nac->zone_src) {
4948 out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
4949 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
4950 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
4951 error = mf_check_src(&out->zone_src, NULL);
4956 if (out->zone_src.n_bits != 16) {
4957 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
4958 out->zone_src.n_bits);
4959 return OFPERR_OFPBAC_BAD_SET_LEN;
4962 out->zone_src.field = NULL;
4963 out->zone_imm = ntohs(nac->zone_imm);
4970 decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
4971 enum ofp_version ofp_version, struct ofpbuf *out)
4973 const size_t ct_offset = ofpacts_pull(out);
4974 struct ofpact_conntrack *conntrack = ofpact_put_CT(out);
4975 conntrack->flags = ntohs(nac->flags);
4977 int error = decode_ct_zone(nac, conntrack);
4981 conntrack->recirc_table = nac->recirc_table;
4982 conntrack->alg = ntohs(nac->alg);
4984 ofpbuf_pull(out, sizeof(*conntrack));
4986 struct ofpbuf openflow = ofpbuf_const_initializer(
4987 nac + 1, ntohs(nac->len) - sizeof(*nac));
4988 error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
4990 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4996 conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
4997 out->header = &conntrack->ofpact;
4998 ofpact_finish_CT(out, &conntrack);
5000 if (conntrack->ofpact.len > sizeof(*conntrack)
5001 && !(conntrack->flags & NX_CT_F_COMMIT)) {
5002 const struct ofpact *a;
5003 size_t ofpacts_len = conntrack->ofpact.len - sizeof(*conntrack);
5005 OFPACT_FOR_EACH (a, conntrack->actions, ofpacts_len) {
5006 if (a->type != OFPACT_NAT || ofpact_get_NAT(a)->flags
5007 || ofpact_get_NAT(a)->range_af != AF_UNSPEC) {
5008 VLOG_WARN_RL(&rl, "CT action requires commit flag if actions "
5009 "other than NAT without arguments are specified.");
5010 error = OFPERR_OFPBAC_BAD_ARGUMENT;
5017 ofpbuf_push_uninit(out, ct_offset);
5022 encode_CT(const struct ofpact_conntrack *conntrack,
5023 enum ofp_version ofp_version, struct ofpbuf *out)
5025 struct nx_action_conntrack *nac;
5026 const size_t ofs = out->size;
5029 nac = put_NXAST_CT(out);
5030 nac->flags = htons(conntrack->flags);
5031 if (conntrack->zone_src.field) {
5032 nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
5033 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
5034 conntrack->zone_src.n_bits);
5036 nac->zone_src = htonl(0);
5037 nac->zone_imm = htons(conntrack->zone_imm);
5039 nac->recirc_table = conntrack->recirc_table;
5040 nac->alg = htons(conntrack->alg);
5042 len = ofpacts_put_openflow_actions(conntrack->actions,
5043 ofpact_ct_get_action_len(conntrack),
5045 len += sizeof(*nac);
5046 nac = ofpbuf_at(out, ofs, sizeof(*nac));
5047 nac->len = htons(len);
5050 static char * OVS_WARN_UNUSED_RESULT parse_NAT(char *arg, struct ofpbuf *,
5051 enum ofputil_protocol * OVS_UNUSED);
5053 /* Parses 'arg' as the argument to a "ct" action, and appends such an
5054 * action to 'ofpacts'.
5056 * Returns NULL if successful, otherwise a malloc()'d string describing the
5057 * error. The caller is responsible for freeing the returned string. */
5058 static char * OVS_WARN_UNUSED_RESULT
5059 parse_CT(char *arg, struct ofpbuf *ofpacts,
5060 enum ofputil_protocol *usable_protocols)
5062 const size_t ct_offset = ofpacts_pull(ofpacts);
5063 struct ofpact_conntrack *oc;
5067 oc = ofpact_put_CT(ofpacts);
5069 oc->recirc_table = NX_CT_RECIRC_NONE;
5070 while (ofputil_parse_key_value(&arg, &key, &value)) {
5071 if (!strcmp(key, "commit")) {
5072 oc->flags |= NX_CT_F_COMMIT;
5073 } else if (!strcmp(key, "table")) {
5074 error = str_to_u8(value, "recirc_table", &oc->recirc_table);
5075 if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
5076 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
5078 } else if (!strcmp(key, "zone")) {
5079 error = str_to_u16(value, "zone", &oc->zone_imm);
5083 error = mf_parse_subfield(&oc->zone_src, value);
5088 } else if (!strcmp(key, "alg")) {
5089 error = str_to_connhelper(value, &oc->alg);
5090 } else if (!strcmp(key, "nat")) {
5091 const size_t nat_offset = ofpacts_pull(ofpacts);
5093 error = parse_NAT(value, ofpacts, usable_protocols);
5094 /* Update CT action pointer and length. */
5095 ofpacts->header = ofpbuf_push_uninit(ofpacts, nat_offset);
5096 oc = ofpacts->header;
5097 } else if (!strcmp(key, "exec")) {
5098 /* Hide existing actions from ofpacts_parse_copy(), so the
5099 * nesting can be handled transparently. */
5100 enum ofputil_protocol usable_protocols2;
5101 const size_t exec_offset = ofpacts_pull(ofpacts);
5103 /* Initializes 'usable_protocol2', fold it back to
5104 * '*usable_protocols' afterwards, so that we do not lose
5105 * restrictions already in there. */
5106 error = ofpacts_parse_copy(value, ofpacts, &usable_protocols2,
5108 *usable_protocols &= usable_protocols2;
5109 ofpacts->header = ofpbuf_push_uninit(ofpacts, exec_offset);
5110 oc = ofpacts->header;
5112 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
5119 ofpact_finish_CT(ofpacts, &oc);
5120 ofpbuf_push_uninit(ofpacts, ct_offset);
5125 format_alg(int port, struct ds *s)
5127 if (port == IPPORT_FTP) {
5128 ds_put_format(s, "%salg=%sftp,", colors.param, colors.end);
5130 ds_put_format(s, "%salg=%s%d,", colors.param, colors.end, port);
5134 static void format_NAT(const struct ofpact_nat *a, struct ds *ds);
5137 format_CT(const struct ofpact_conntrack *a, struct ds *s)
5139 ds_put_format(s, "%sct(%s", colors.paren, colors.end);
5140 if (a->flags & NX_CT_F_COMMIT) {
5141 ds_put_format(s, "%scommit%s,", colors.value, colors.end);
5143 if (a->recirc_table != NX_CT_RECIRC_NONE) {
5144 ds_put_format(s, "%stable=%s%"PRIu8",",
5145 colors.special, colors.end, a->recirc_table);
5147 if (a->zone_src.field) {
5148 ds_put_format(s, "%szone=%s", colors.param, colors.end);
5149 mf_format_subfield(&a->zone_src, s);
5150 ds_put_char(s, ',');
5151 } else if (a->zone_imm) {
5152 ds_put_format(s, "%szone=%s%"PRIu16",",
5153 colors.param, colors.end, a->zone_imm);
5155 /* If the first action is a NAT action, format it outside of the 'exec'
5157 const struct ofpact *action = a->actions;
5158 size_t actions_len = ofpact_ct_get_action_len(a);
5159 if (actions_len && action->type == OFPACT_NAT) {
5160 format_NAT(ofpact_get_NAT(action), s);
5161 ds_put_char(s, ',');
5162 actions_len -= OFPACT_ALIGN(action->len);
5163 action = ofpact_next(action);
5166 ds_put_format(s, "%sexec(%s", colors.paren, colors.end);
5167 ofpacts_format(action, actions_len, s);
5168 ds_put_format(s, "%s),%s", colors.paren, colors.end);
5170 format_alg(a->alg, s);
5172 ds_put_format(s, "%s)%s", colors.paren, colors.end);
5177 /* Which optional fields are present? */
5179 NX_NAT_RANGE_IPV4_MIN = 1 << 0, /* ovs_be32 */
5180 NX_NAT_RANGE_IPV4_MAX = 1 << 1, /* ovs_be32 */
5181 NX_NAT_RANGE_IPV6_MIN = 1 << 2, /* struct in6_addr */
5182 NX_NAT_RANGE_IPV6_MAX = 1 << 3, /* struct in6_addr */
5183 NX_NAT_RANGE_PROTO_MIN = 1 << 4, /* ovs_be16 */
5184 NX_NAT_RANGE_PROTO_MAX = 1 << 5, /* ovs_be16 */
5187 /* Action structure for NXAST_NAT. */
5188 struct nx_action_nat {
5189 ovs_be16 type; /* OFPAT_VENDOR. */
5190 ovs_be16 len; /* At least 16. */
5191 ovs_be32 vendor; /* NX_VENDOR_ID. */
5192 ovs_be16 subtype; /* NXAST_NAT. */
5193 uint8_t pad[2]; /* Must be zero. */
5194 ovs_be16 flags; /* Zero or more NX_NAT_F_* flags.
5195 * Unspecified flag bits must be zero. */
5196 ovs_be16 range_present; /* NX_NAT_RANGE_* */
5197 /* Followed by optional parameters as specified by 'range_present' */
5199 OFP_ASSERT(sizeof(struct nx_action_nat) == 16);
5202 encode_NAT(const struct ofpact_nat *nat,
5203 enum ofp_version ofp_version OVS_UNUSED,
5206 struct nx_action_nat *nan;
5207 const size_t ofs = out->size;
5208 uint16_t range_present = 0;
5210 nan = put_NXAST_NAT(out);
5211 nan->flags = htons(nat->flags);
5212 if (nat->range_af == AF_INET) {
5213 if (nat->range.addr.ipv4.min) {
5214 ovs_be32 *min = ofpbuf_put_uninit(out, sizeof *min);
5215 *min = nat->range.addr.ipv4.min;
5216 range_present |= NX_NAT_RANGE_IPV4_MIN;
5218 if (nat->range.addr.ipv4.max) {
5219 ovs_be32 *max = ofpbuf_put_uninit(out, sizeof *max);
5220 *max = nat->range.addr.ipv4.max;
5221 range_present |= NX_NAT_RANGE_IPV4_MAX;
5223 } else if (nat->range_af == AF_INET6) {
5224 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5225 struct in6_addr *min = ofpbuf_put_uninit(out, sizeof *min);
5226 *min = nat->range.addr.ipv6.min;
5227 range_present |= NX_NAT_RANGE_IPV6_MIN;
5229 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.max)) {
5230 struct in6_addr *max = ofpbuf_put_uninit(out, sizeof *max);
5231 *max = nat->range.addr.ipv6.max;
5232 range_present |= NX_NAT_RANGE_IPV6_MAX;
5235 if (nat->range_af != AF_UNSPEC) {
5236 if (nat->range.proto.min) {
5237 ovs_be16 *min = ofpbuf_put_uninit(out, sizeof *min);
5238 *min = htons(nat->range.proto.min);
5239 range_present |= NX_NAT_RANGE_PROTO_MIN;
5241 if (nat->range.proto.max) {
5242 ovs_be16 *max = ofpbuf_put_uninit(out, sizeof *max);
5243 *max = htons(nat->range.proto.max);
5244 range_present |= NX_NAT_RANGE_PROTO_MAX;
5247 pad_ofpat(out, ofs);
5248 nan = ofpbuf_at(out, ofs, sizeof *nan);
5249 nan->range_present = htons(range_present);
5253 decode_NXAST_RAW_NAT(const struct nx_action_nat *nan,
5254 enum ofp_version ofp_version OVS_UNUSED,
5257 struct ofpact_nat *nat;
5258 uint16_t range_present = ntohs(nan->range_present);
5259 const char *opts = (char *)(nan + 1);
5260 uint16_t len = ntohs(nan->len) - sizeof *nan;
5262 nat = ofpact_put_NAT(out);
5263 nat->flags = ntohs(nan->flags);
5265 #define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
5266 (LEN >= sizeof(TYPE) \
5267 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
5268 SRC += sizeof(TYPE)) \
5271 nat->range_af = AF_UNSPEC;
5272 if (range_present & NX_NAT_RANGE_IPV4_MIN) {
5273 if (range_present & (NX_NAT_RANGE_IPV6_MIN | NX_NAT_RANGE_IPV6_MAX)) {
5274 return OFPERR_OFPBAC_BAD_ARGUMENT;
5277 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.min, opts, len, ovs_be32)
5278 || !nat->range.addr.ipv4.min) {
5279 return OFPERR_OFPBAC_BAD_ARGUMENT;
5282 nat->range_af = AF_INET;
5284 if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5285 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.max, opts, len,
5287 return OFPERR_OFPBAC_BAD_ARGUMENT;
5289 if (ntohl(nat->range.addr.ipv4.max)
5290 < ntohl(nat->range.addr.ipv4.min)) {
5291 return OFPERR_OFPBAC_BAD_ARGUMENT;
5294 } else if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5295 return OFPERR_OFPBAC_BAD_ARGUMENT;
5296 } else if (range_present & NX_NAT_RANGE_IPV6_MIN) {
5297 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.min, opts, len,
5299 || ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5300 return OFPERR_OFPBAC_BAD_ARGUMENT;
5303 nat->range_af = AF_INET6;
5305 if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5306 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.max, opts, len,
5308 return OFPERR_OFPBAC_BAD_ARGUMENT;
5310 if (memcmp(&nat->range.addr.ipv6.max, &nat->range.addr.ipv6.min,
5311 sizeof(struct in6_addr)) < 0) {
5312 return OFPERR_OFPBAC_BAD_ARGUMENT;
5315 } else if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5316 return OFPERR_OFPBAC_BAD_ARGUMENT;
5319 if (range_present & NX_NAT_RANGE_PROTO_MIN) {
5322 if (nat->range_af == AF_UNSPEC) {
5323 return OFPERR_OFPBAC_BAD_ARGUMENT;
5325 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16) || proto == 0) {
5326 return OFPERR_OFPBAC_BAD_ARGUMENT;
5328 nat->range.proto.min = ntohs(proto);
5329 if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5330 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16)) {
5331 return OFPERR_OFPBAC_BAD_ARGUMENT;
5333 nat->range.proto.max = ntohs(proto);
5334 if (nat->range.proto.max < nat->range.proto.min) {
5335 return OFPERR_OFPBAC_BAD_ARGUMENT;
5338 } else if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5339 return OFPERR_OFPBAC_BAD_ARGUMENT;
5346 format_NAT(const struct ofpact_nat *a, struct ds *ds)
5348 ds_put_format(ds, "%snat%s", colors.paren, colors.end);
5350 if (a->flags & (NX_NAT_F_SRC | NX_NAT_F_DST)) {
5351 ds_put_format(ds, "%s(%s", colors.paren, colors.end);
5352 ds_put_format(ds, a->flags & NX_NAT_F_SRC ? "%ssrc%s" : "%sdst%s",
5353 colors.param, colors.end);
5355 if (a->range_af != AF_UNSPEC) {
5356 ds_put_format(ds, "%s=%s", colors.param, colors.end);
5358 if (a->range_af == AF_INET) {
5359 ds_put_format(ds, IP_FMT, IP_ARGS(a->range.addr.ipv4.min));
5361 if (a->range.addr.ipv4.max
5362 && a->range.addr.ipv4.max != a->range.addr.ipv4.min) {
5363 ds_put_format(ds, "-"IP_FMT,
5364 IP_ARGS(a->range.addr.ipv4.max));
5366 } else if (a->range_af == AF_INET6) {
5367 ipv6_format_addr_bracket(&a->range.addr.ipv6.min, ds,
5368 a->range.proto.min);
5370 if (!ipv6_mask_is_any(&a->range.addr.ipv6.max)
5371 && memcmp(&a->range.addr.ipv6.max, &a->range.addr.ipv6.min,
5372 sizeof(struct in6_addr)) != 0) {
5373 ds_put_char(ds, '-');
5374 ipv6_format_addr_bracket(&a->range.addr.ipv6.max, ds,
5375 a->range.proto.min);
5378 if (a->range.proto.min) {
5379 ds_put_char(ds, ':');
5380 ds_put_format(ds, "%"PRIu16, a->range.proto.min);
5382 if (a->range.proto.max
5383 && a->range.proto.max != a->range.proto.min) {
5384 ds_put_format(ds, "-%"PRIu16, a->range.proto.max);
5387 ds_put_char(ds, ',');
5389 if (a->flags & NX_NAT_F_PERSISTENT) {
5390 ds_put_format(ds, "%spersistent%s,",
5391 colors.value, colors.end);
5393 if (a->flags & NX_NAT_F_PROTO_HASH) {
5394 ds_put_format(ds, "%shash%s,", colors.value, colors.end);
5396 if (a->flags & NX_NAT_F_PROTO_RANDOM) {
5397 ds_put_format(ds, "%srandom%s,", colors.value, colors.end);
5401 ds_put_format(ds, "%s)%s", colors.paren, colors.end);
5405 static char * OVS_WARN_UNUSED_RESULT
5406 str_to_nat_range(const char *s, struct ofpact_nat *on)
5408 char ipv6_s[IPV6_SCAN_LEN + 1];
5411 on->range_af = AF_UNSPEC;
5412 if (ovs_scan_len(s, &n, IP_SCAN_FMT,
5413 IP_SCAN_ARGS(&on->range.addr.ipv4.min))) {
5414 on->range_af = AF_INET;
5418 if (!ovs_scan_len(s, &n, IP_SCAN_FMT,
5419 IP_SCAN_ARGS(&on->range.addr.ipv4.max))
5420 || (ntohl(on->range.addr.ipv4.max)
5421 < ntohl(on->range.addr.ipv4.min))) {
5425 } else if ((ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5426 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5427 && inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.min) == 1) {
5428 on->range_af = AF_INET6;
5432 if (!(ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5433 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5434 || inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.max) != 1
5435 || memcmp(&on->range.addr.ipv6.max, &on->range.addr.ipv6.min,
5436 sizeof on->range.addr.ipv6.max) < 0) {
5441 if (on->range_af != AF_UNSPEC && s[n] == ':') {
5443 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.min)) {
5448 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.max)
5449 || on->range.proto.max < on->range.proto.min) {
5454 if (strlen(s) != n) {
5455 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
5460 return xasprintf("invalid nat range \"%s\"", s);
5464 /* Parses 'arg' as the argument to a "nat" action, and appends such an
5465 * action to 'ofpacts'.
5467 * Returns NULL if successful, otherwise a malloc()'d string describing the
5468 * error. The caller is responsible for freeing the returned string. */
5469 static char * OVS_WARN_UNUSED_RESULT
5470 parse_NAT(char *arg, struct ofpbuf *ofpacts,
5471 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5473 struct ofpact_nat *on = ofpact_put_NAT(ofpacts);
5477 on->range_af = AF_UNSPEC;
5479 while (ofputil_parse_key_value(&arg, &key, &value)) {
5482 if (!strcmp(key, "src")) {
5483 on->flags |= NX_NAT_F_SRC;
5484 error = str_to_nat_range(value, on);
5485 } else if (!strcmp(key, "dst")) {
5486 on->flags |= NX_NAT_F_DST;
5487 error = str_to_nat_range(value, on);
5488 } else if (!strcmp(key, "persistent")) {
5489 on->flags |= NX_NAT_F_PERSISTENT;
5490 } else if (!strcmp(key, "hash")) {
5491 on->flags |= NX_NAT_F_PROTO_HASH;
5492 } else if (!strcmp(key, "random")) {
5493 on->flags |= NX_NAT_F_PROTO_RANDOM;
5495 error = xasprintf("invalid key \"%s\" in \"nat\" argument",
5502 if (on->flags & NX_NAT_F_SRC && on->flags & NX_NAT_F_DST) {
5503 return xasprintf("May only specify one of \"snat\" or \"dnat\".");
5505 if (!(on->flags & NX_NAT_F_SRC || on->flags & NX_NAT_F_DST)) {
5507 return xasprintf("Flags allowed only with \"snat\" or \"dnat\".");
5509 if (on->range_af != AF_UNSPEC) {
5510 return xasprintf("Range allowed only with \"snat\" or \"dnat\".");
5517 /* Meter instruction. */
5520 encode_METER(const struct ofpact_meter *meter,
5521 enum ofp_version ofp_version, struct ofpbuf *out)
5523 if (ofp_version >= OFP13_VERSION) {
5524 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
5528 static char * OVS_WARN_UNUSED_RESULT
5529 parse_METER(char *arg, struct ofpbuf *ofpacts,
5530 enum ofputil_protocol *usable_protocols)
5532 *usable_protocols &= OFPUTIL_P_OF13_UP;
5533 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
5537 format_METER(const struct ofpact_meter *a, struct ds *s)
5539 ds_put_format(s, "%smeter:%s%"PRIu32,
5540 colors.param, colors.end, a->meter_id);
5543 /* Clear-Actions instruction. */
5546 encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
5547 enum ofp_version ofp_version OVS_UNUSED,
5548 struct ofpbuf *out OVS_UNUSED)
5550 if (ofp_version > OFP10_VERSION) {
5551 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
5555 static char * OVS_WARN_UNUSED_RESULT
5556 parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
5557 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5559 ofpact_put_CLEAR_ACTIONS(ofpacts);
5564 format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
5566 ds_put_format(s, "%sclear_actions%s", colors.value, colors.end);
5569 /* Write-Actions instruction. */
5572 encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
5573 enum ofp_version ofp_version, struct ofpbuf *out)
5575 if (ofp_version > OFP10_VERSION) {
5576 const size_t ofs = out->size;
5578 instruction_put_OFPIT11_WRITE_ACTIONS(out);
5579 ofpacts_put_openflow_actions(actions->actions,
5580 ofpact_nest_get_action_len(actions),
5582 ofpacts_update_instruction_actions(out, ofs);
5586 static char * OVS_WARN_UNUSED_RESULT
5587 parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
5588 enum ofputil_protocol *usable_protocols)
5590 size_t ofs = ofpacts_pull(ofpacts);
5591 struct ofpact_nest *on;
5594 /* Add a Write-Actions instruction and then pull it off. */
5595 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
5596 ofpbuf_pull(ofpacts, sizeof *on);
5598 /* Parse nested actions.
5600 * We pulled off "write-actions" and the previous actions because the
5601 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
5602 * that it doesn't actually include the nested actions. That means that
5603 * ofpacts_parse() would reject them as being part of an Apply-Actions that
5604 * follows a Write-Actions, which is an invalid order. */
5605 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
5606 OFPACT_WRITE_ACTIONS);
5608 /* Put the Write-Actions back on and update its length. */
5609 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
5610 on->ofpact.len = ofpacts->size;
5612 /* Put any previous actions or instructions back on. */
5613 ofpbuf_push_uninit(ofpacts, ofs);
5619 format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
5621 ds_put_format(s, "%swrite_actions(%s", colors.paren, colors.end);
5622 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
5623 ds_put_format(s, "%s)%s", colors.paren, colors.end);
5626 /* Action structure for NXAST_WRITE_METADATA.
5628 * Modifies the 'mask' bits of the metadata value. */
5629 struct nx_action_write_metadata {
5630 ovs_be16 type; /* OFPAT_VENDOR. */
5631 ovs_be16 len; /* Length is 32. */
5632 ovs_be32 vendor; /* NX_VENDOR_ID. */
5633 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
5634 uint8_t zeros[6]; /* Must be zero. */
5635 ovs_be64 metadata; /* Metadata register. */
5636 ovs_be64 mask; /* Metadata mask. */
5638 OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
5641 decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
5642 enum ofp_version ofp_version OVS_UNUSED,
5645 struct ofpact_metadata *om;
5647 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
5648 return OFPERR_NXBRC_MUST_BE_ZERO;
5651 om = ofpact_put_WRITE_METADATA(out);
5652 om->metadata = nawm->metadata;
5653 om->mask = nawm->mask;
5659 encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
5660 enum ofp_version ofp_version, struct ofpbuf *out)
5662 if (ofp_version == OFP10_VERSION) {
5663 struct nx_action_write_metadata *nawm;
5665 nawm = put_NXAST_WRITE_METADATA(out);
5666 nawm->metadata = metadata->metadata;
5667 nawm->mask = metadata->mask;
5669 struct ofp11_instruction_write_metadata *oiwm;
5671 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
5672 oiwm->metadata = metadata->metadata;
5673 oiwm->metadata_mask = metadata->mask;
5677 static char * OVS_WARN_UNUSED_RESULT
5678 parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
5679 enum ofputil_protocol *usable_protocols)
5681 struct ofpact_metadata *om;
5682 char *mask = strchr(arg, '/');
5684 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
5686 om = ofpact_put_WRITE_METADATA(ofpacts);
5691 error = str_to_be64(mask + 1, &om->mask);
5696 om->mask = OVS_BE64_MAX;
5699 return str_to_be64(arg, &om->metadata);
5703 format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5705 ds_put_format(s, "%swrite_metadata:%s%#"PRIx64,
5706 colors.param, colors.end, ntohll(a->metadata));
5707 if (a->mask != OVS_BE64_MAX) {
5708 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5712 /* Goto-Table instruction. */
5715 encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5716 enum ofp_version ofp_version, struct ofpbuf *out)
5718 if (ofp_version == OFP10_VERSION) {
5719 struct nx_action_resubmit *nar;
5721 nar = put_NXAST_RESUBMIT_TABLE(out);
5722 nar->table = goto_table->table_id;
5723 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5725 struct ofp11_instruction_goto_table *oigt;
5727 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5728 oigt->table_id = goto_table->table_id;
5729 memset(oigt->pad, 0, sizeof oigt->pad);
5733 static char * OVS_WARN_UNUSED_RESULT
5734 parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5735 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5737 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5738 char *table_s = strsep(&arg, ",");
5739 if (!table_s || !table_s[0]) {
5740 return xstrdup("instruction goto-table needs table id");
5742 return str_to_u8(table_s, "table", &ogt->table_id);
5746 format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5748 ds_put_format(s, "%sgoto_table:%s%"PRIu8,
5749 colors.param, colors.end, a->table_id);
5753 log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
5754 const struct ofp_action_header *bad_action, enum ofperr error)
5756 if (!VLOG_DROP_WARN(&rl)) {
5760 ds_put_hex_dump(&s, actions, actions_len, 0, false);
5761 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5762 (char *)bad_action - (char *)actions,
5763 ofperr_get_name(error), ds_cstr(&s));
5769 ofpacts_decode(const void *actions, size_t actions_len,
5770 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5772 struct ofpbuf openflow = ofpbuf_const_initializer(actions, actions_len);
5773 while (openflow.size) {
5774 const struct ofp_action_header *action = openflow.data;
5775 enum ofp_raw_action_type raw;
5779 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5781 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
5785 log_bad_action(actions, actions_len, action, error);
5793 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5794 unsigned int actions_len,
5795 enum ofp_version version,
5796 uint32_t allowed_ovsinsts,
5797 struct ofpbuf *ofpacts,
5798 enum ofpact_type outer_action)
5800 const struct ofp_action_header *actions;
5801 size_t orig_size = ofpacts->size;
5804 if (actions_len % OFP_ACTION_ALIGN != 0) {
5805 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5806 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5807 return OFPERR_OFPBRC_BAD_LEN;
5810 actions = ofpbuf_try_pull(openflow, actions_len);
5811 if (actions == NULL) {
5812 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
5813 "remaining message length (%"PRIu32")",
5814 actions_len, openflow->size);
5815 return OFPERR_OFPBRC_BAD_LEN;
5818 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5820 ofpacts->size = orig_size;
5824 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
5827 ofpacts->size = orig_size;
5832 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
5833 * of 'openflow' into ofpacts. On success, appends the converted actions to
5834 * 'ofpacts'; on failure, 'ofpacts' is unchanged (but might be reallocated) .
5835 * Returns 0 if successful, otherwise an OpenFlow error.
5837 * Actions are processed according to their OpenFlow version which
5838 * is provided in the 'version' parameter.
5840 * In most places in OpenFlow, actions appear encapsulated in instructions, so
5841 * you should call ofpacts_pull_openflow_instructions() instead of this
5844 * The parsed actions are valid generically, but they may not be valid in a
5845 * specific context. For example, port numbers up to OFPP_MAX are valid
5846 * generically, but specific datapaths may only support port numbers in a
5847 * smaller range. Use ofpacts_check() to additional check whether actions are
5848 * valid in a specific context. */
5850 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
5851 unsigned int actions_len,
5852 enum ofp_version version,
5853 struct ofpbuf *ofpacts)
5855 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
5856 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
5860 /* OpenFlow 1.1 actions. */
5863 /* True if an action sets the value of a field
5864 * in a way that is compatibile with the action set.
5865 * The field can be set via either a set or a move action.
5866 * False otherwise. */
5868 ofpact_is_set_or_move_action(const struct ofpact *a)
5871 case OFPACT_SET_FIELD:
5872 case OFPACT_REG_MOVE:
5873 case OFPACT_SET_ETH_DST:
5874 case OFPACT_SET_ETH_SRC:
5875 case OFPACT_SET_IP_DSCP:
5876 case OFPACT_SET_IP_ECN:
5877 case OFPACT_SET_IP_TTL:
5878 case OFPACT_SET_IPV4_DST:
5879 case OFPACT_SET_IPV4_SRC:
5880 case OFPACT_SET_L4_DST_PORT:
5881 case OFPACT_SET_L4_SRC_PORT:
5882 case OFPACT_SET_MPLS_LABEL:
5883 case OFPACT_SET_MPLS_TC:
5884 case OFPACT_SET_MPLS_TTL:
5885 case OFPACT_SET_QUEUE:
5886 case OFPACT_SET_TUNNEL:
5887 case OFPACT_SET_VLAN_PCP:
5888 case OFPACT_SET_VLAN_VID:
5891 case OFPACT_CLEAR_ACTIONS:
5894 case OFPACT_CONTROLLER:
5895 case OFPACT_DEC_MPLS_TTL:
5896 case OFPACT_DEC_TTL:
5897 case OFPACT_ENQUEUE:
5899 case OFPACT_UNROLL_XLATE:
5900 case OFPACT_FIN_TIMEOUT:
5901 case OFPACT_GOTO_TABLE:
5904 case OFPACT_CONJUNCTION:
5906 case OFPACT_MULTIPATH:
5909 case OFPACT_OUTPUT_REG:
5910 case OFPACT_POP_MPLS:
5911 case OFPACT_POP_QUEUE:
5912 case OFPACT_PUSH_MPLS:
5913 case OFPACT_PUSH_VLAN:
5914 case OFPACT_RESUBMIT:
5916 case OFPACT_STACK_POP:
5917 case OFPACT_STACK_PUSH:
5918 case OFPACT_STRIP_VLAN:
5919 case OFPACT_WRITE_ACTIONS:
5920 case OFPACT_WRITE_METADATA:
5921 case OFPACT_DEBUG_RECIRC:
5928 /* True if an action is allowed in the action set.
5929 * False otherwise. */
5931 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
5934 case OFPACT_DEC_MPLS_TTL:
5935 case OFPACT_DEC_TTL:
5938 case OFPACT_POP_MPLS:
5939 case OFPACT_PUSH_MPLS:
5940 case OFPACT_PUSH_VLAN:
5941 case OFPACT_REG_MOVE:
5942 case OFPACT_SET_FIELD:
5943 case OFPACT_SET_ETH_DST:
5944 case OFPACT_SET_ETH_SRC:
5945 case OFPACT_SET_IP_DSCP:
5946 case OFPACT_SET_IP_ECN:
5947 case OFPACT_SET_IP_TTL:
5948 case OFPACT_SET_IPV4_DST:
5949 case OFPACT_SET_IPV4_SRC:
5950 case OFPACT_SET_L4_DST_PORT:
5951 case OFPACT_SET_L4_SRC_PORT:
5952 case OFPACT_SET_MPLS_LABEL:
5953 case OFPACT_SET_MPLS_TC:
5954 case OFPACT_SET_MPLS_TTL:
5955 case OFPACT_SET_QUEUE:
5956 case OFPACT_SET_TUNNEL:
5957 case OFPACT_SET_VLAN_PCP:
5958 case OFPACT_SET_VLAN_VID:
5959 case OFPACT_STRIP_VLAN:
5962 /* In general these actions are excluded because they are not part of
5963 * the OpenFlow specification nor map to actions that are defined in
5964 * the specification. Thus the order in which they should be applied
5965 * in the action set is undefined. */
5967 case OFPACT_CONTROLLER:
5970 case OFPACT_ENQUEUE:
5972 case OFPACT_UNROLL_XLATE:
5973 case OFPACT_FIN_TIMEOUT:
5975 case OFPACT_CONJUNCTION:
5976 case OFPACT_MULTIPATH:
5978 case OFPACT_OUTPUT_REG:
5979 case OFPACT_POP_QUEUE:
5980 case OFPACT_RESUBMIT:
5982 case OFPACT_STACK_POP:
5983 case OFPACT_STACK_PUSH:
5984 case OFPACT_DEBUG_RECIRC:
5986 /* The action set may only include actions and thus
5987 * may not include any instructions */
5988 case OFPACT_CLEAR_ACTIONS:
5989 case OFPACT_GOTO_TABLE:
5991 case OFPACT_WRITE_ACTIONS:
5992 case OFPACT_WRITE_METADATA:
5999 /* Append ofpact 'a' onto the tail of 'out' */
6001 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
6003 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
6006 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
6008 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
6009 enum ofpact_type filter)
6011 const struct ofpact *target;
6012 const struct ofpact *a;
6015 OFPACT_FOR_EACH (a, in->data, in->size) {
6016 if (a->type == filter) {
6021 ofpact_copy(out, target);
6023 return target != NULL;
6026 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
6027 * The order of appended ofpacts is preserved between 'in' and 'out' */
6029 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
6030 bool (*filter)(const struct ofpact *))
6032 const struct ofpact *a;
6034 OFPACT_FOR_EACH (a, in->data, in->size) {
6036 ofpact_copy(out, a);
6041 /* Reads 'action_set', which contains ofpacts accumulated by
6042 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
6043 * executed directly into 'action_list'. (These names correspond to the
6044 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
6046 * In general this involves appending the last instance of each action that is
6047 * admissible in the action set in the order described in the OpenFlow
6051 * + output action is only appended if no group action was present in 'in'.
6052 * + As a simplification all set actions are copied in the order the are
6053 * provided in 'in' as many set actions applied to a field has the same
6054 * affect as only applying the last action that sets a field and
6055 * duplicates are removed by do_xlate_actions().
6056 * This has an unwanted side-effect of compsoting multiple
6057 * LOAD_REG actions that touch different regions of the same field. */
6059 ofpacts_execute_action_set(struct ofpbuf *action_list,
6060 const struct ofpbuf *action_set)
6062 /* The OpenFlow spec "Action Set" section specifies this order. */
6063 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
6064 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
6065 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
6066 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
6067 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
6068 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
6069 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
6070 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
6072 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
6073 * we should execute only OFPACT_GROUP.
6075 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
6076 * all the actions because there's no point in modifying a packet that will
6077 * not be sent anywhere. */
6078 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
6079 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
6080 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
6081 ofpbuf_clear(action_list);
6087 ofpacts_decode_for_action_set(const struct ofp_action_header *in,
6088 size_t n_in, enum ofp_version version,
6093 size_t start = out->size;
6095 error = ofpacts_decode(in, n_in, version, out);
6101 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
6102 if (!ofpact_is_allowed_in_actions_set(a)) {
6103 VLOG_WARN_RL(&rl, "disallowed action in action set");
6104 return OFPERR_OFPBAC_BAD_TYPE;
6111 /* OpenFlow 1.1 instructions. */
6113 struct instruction_type_info {
6114 enum ovs_instruction_type type;
6118 static const struct instruction_type_info inst_info[] = {
6119 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
6125 ovs_instruction_name_from_type(enum ovs_instruction_type type)
6127 return inst_info[type].name;
6131 ovs_instruction_type_from_name(const char *name)
6133 const struct instruction_type_info *p;
6134 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
6135 if (!strcasecmp(name, p->name)) {
6142 enum ovs_instruction_type
6143 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
6147 return OVSINST_OFPIT13_METER;
6148 case OFPACT_CLEAR_ACTIONS:
6149 return OVSINST_OFPIT11_CLEAR_ACTIONS;
6150 case OFPACT_WRITE_ACTIONS:
6151 return OVSINST_OFPIT11_WRITE_ACTIONS;
6152 case OFPACT_WRITE_METADATA:
6153 return OVSINST_OFPIT11_WRITE_METADATA;
6154 case OFPACT_GOTO_TABLE:
6155 return OVSINST_OFPIT11_GOTO_TABLE;
6158 case OFPACT_CONTROLLER:
6159 case OFPACT_ENQUEUE:
6160 case OFPACT_OUTPUT_REG:
6162 case OFPACT_SET_VLAN_VID:
6163 case OFPACT_SET_VLAN_PCP:
6164 case OFPACT_STRIP_VLAN:
6165 case OFPACT_PUSH_VLAN:
6166 case OFPACT_SET_ETH_SRC:
6167 case OFPACT_SET_ETH_DST:
6168 case OFPACT_SET_IPV4_SRC:
6169 case OFPACT_SET_IPV4_DST:
6170 case OFPACT_SET_IP_DSCP:
6171 case OFPACT_SET_IP_ECN:
6172 case OFPACT_SET_IP_TTL:
6173 case OFPACT_SET_L4_SRC_PORT:
6174 case OFPACT_SET_L4_DST_PORT:
6175 case OFPACT_REG_MOVE:
6176 case OFPACT_SET_FIELD:
6177 case OFPACT_STACK_PUSH:
6178 case OFPACT_STACK_POP:
6179 case OFPACT_DEC_TTL:
6180 case OFPACT_SET_MPLS_LABEL:
6181 case OFPACT_SET_MPLS_TC:
6182 case OFPACT_SET_MPLS_TTL:
6183 case OFPACT_DEC_MPLS_TTL:
6184 case OFPACT_PUSH_MPLS:
6185 case OFPACT_POP_MPLS:
6186 case OFPACT_SET_TUNNEL:
6187 case OFPACT_SET_QUEUE:
6188 case OFPACT_POP_QUEUE:
6189 case OFPACT_FIN_TIMEOUT:
6190 case OFPACT_RESUBMIT:
6192 case OFPACT_CONJUNCTION:
6193 case OFPACT_MULTIPATH:
6196 case OFPACT_UNROLL_XLATE:
6198 case OFPACT_DEBUG_RECIRC:
6202 return OVSINST_OFPIT11_APPLY_ACTIONS;
6207 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
6208 const uint16_t inst_type)
6210 switch (inst_type) {
6212 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6214 *instruction_type = OVSINST_##ENUM; \
6220 return OFPERR_OFPBIC_UNKNOWN_INST;
6224 /* Two-way translation between OVS's internal "OVSINST_*" representation of
6225 * instructions and the "OFPIT_*" representation used in OpenFlow. */
6226 struct ovsinst_map {
6227 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
6228 int ofpit; /* OFPIT_* number from OpenFlow spec. */
6231 static const struct ovsinst_map *
6232 get_ovsinst_map(enum ofp_version version)
6234 /* OpenFlow 1.1 and 1.2 instructions. */
6235 static const struct ovsinst_map of11[] = {
6236 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6237 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6238 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6239 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6240 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6244 /* OpenFlow 1.3+ instructions. */
6245 static const struct ovsinst_map of13[] = {
6246 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6247 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6248 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6249 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6250 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6251 { OVSINST_OFPIT13_METER, 6 },
6255 return version < OFP13_VERSION ? of11 : of13;
6258 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
6259 * values, into a bitmap of instructions suitable for OpenFlow 'version'
6260 * (OFP11_VERSION or later), and returns the result. */
6262 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
6264 uint32_t ofpit_bitmap = 0;
6265 const struct ovsinst_map *x;
6267 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6268 if (ovsinst_bitmap & (1u << x->ovsinst)) {
6269 ofpit_bitmap |= 1u << x->ofpit;
6272 return htonl(ofpit_bitmap);
6275 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
6276 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
6277 * correspond to OVSINST_* values, and returns the result. */
6279 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
6281 uint32_t ovsinst_bitmap = 0;
6282 const struct ovsinst_map *x;
6284 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6285 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
6286 ovsinst_bitmap |= 1u << x->ovsinst;
6289 return ovsinst_bitmap;
6292 static inline struct ofp11_instruction *
6293 instruction_next(const struct ofp11_instruction *inst)
6295 return ((struct ofp11_instruction *) (void *)
6296 ((uint8_t *) inst + ntohs(inst->len)));
6300 instruction_is_valid(const struct ofp11_instruction *inst,
6301 size_t n_instructions)
6303 uint16_t len = ntohs(inst->len);
6304 return (!(len % OFP11_INSTRUCTION_ALIGN)
6305 && len >= sizeof *inst
6306 && len / sizeof *inst <= n_instructions);
6309 /* This macro is careful to check for instructions with bad lengths. */
6310 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
6311 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
6312 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
6313 ((LEFT) -= (ntohs((ITER)->len) \
6314 / sizeof(struct ofp11_instruction)), \
6315 (ITER) = instruction_next(ITER)))
6318 decode_openflow11_instruction(const struct ofp11_instruction *inst,
6319 enum ovs_instruction_type *type)
6321 uint16_t len = ntohs(inst->len);
6323 switch (inst->type) {
6324 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
6325 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
6327 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6328 case CONSTANT_HTONS(ENUM): \
6330 ? len >= sizeof(struct STRUCT) \
6331 : len == sizeof(struct STRUCT)) { \
6332 *type = OVSINST_##ENUM; \
6335 return OFPERR_OFPBIC_BAD_LEN; \
6341 return OFPERR_OFPBIC_UNKNOWN_INST;
6346 decode_openflow11_instructions(const struct ofp11_instruction insts[],
6348 const struct ofp11_instruction *out[])
6350 const struct ofp11_instruction *inst;
6353 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
6354 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
6355 enum ovs_instruction_type type;
6358 error = decode_openflow11_instruction(inst, &type);
6364 return OFPERR_OFPBIC_DUP_INST;
6370 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
6371 (n_insts - left) * sizeof *inst);
6372 return OFPERR_OFPBIC_BAD_LEN;
6378 get_actions_from_instruction(const struct ofp11_instruction *inst,
6379 const struct ofp_action_header **actions,
6380 size_t *actions_len)
6382 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
6383 *actions_len = ntohs(inst->len) - sizeof *inst;
6387 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
6388 unsigned int instructions_len,
6389 enum ofp_version version,
6390 struct ofpbuf *ofpacts)
6392 const struct ofp11_instruction *instructions;
6393 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
6396 ofpbuf_clear(ofpacts);
6397 if (version == OFP10_VERSION) {
6398 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
6400 (1u << N_OVS_INSTRUCTIONS) - 1,
6404 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
6405 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
6407 instructions_len, OFP11_INSTRUCTION_ALIGN);
6408 error = OFPERR_OFPBIC_BAD_LEN;
6412 instructions = ofpbuf_try_pull(openflow, instructions_len);
6413 if (instructions == NULL) {
6414 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
6415 "remaining message length (%"PRIu32")",
6416 instructions_len, openflow->size);
6417 error = OFPERR_OFPBIC_BAD_LEN;
6421 error = decode_openflow11_instructions(
6422 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
6428 if (insts[OVSINST_OFPIT13_METER]) {
6429 const struct ofp13_instruction_meter *oim;
6430 struct ofpact_meter *om;
6432 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
6433 insts[OVSINST_OFPIT13_METER]);
6435 om = ofpact_put_METER(ofpacts);
6436 om->meter_id = ntohl(oim->meter_id);
6438 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
6439 const struct ofp_action_header *actions;
6442 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
6443 &actions, &actions_len);
6444 error = ofpacts_decode(actions, actions_len, version, ofpacts);
6449 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
6450 instruction_get_OFPIT11_CLEAR_ACTIONS(
6451 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
6452 ofpact_put_CLEAR_ACTIONS(ofpacts);
6454 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
6455 struct ofpact_nest *on;
6456 const struct ofp_action_header *actions;
6458 size_t start = ofpacts->size;
6459 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
6460 offsetof(struct ofpact_nest, actions));
6461 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
6462 &actions, &actions_len);
6463 error = ofpacts_decode_for_action_set(actions, actions_len,
6468 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
6469 on->ofpact.len = ofpacts->size - start;
6471 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
6472 const struct ofp11_instruction_write_metadata *oiwm;
6473 struct ofpact_metadata *om;
6475 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
6476 insts[OVSINST_OFPIT11_WRITE_METADATA]);
6478 om = ofpact_put_WRITE_METADATA(ofpacts);
6479 om->metadata = oiwm->metadata;
6480 om->mask = oiwm->metadata_mask;
6482 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
6483 const struct ofp11_instruction_goto_table *oigt;
6484 struct ofpact_goto_table *ogt;
6486 oigt = instruction_get_OFPIT11_GOTO_TABLE(
6487 insts[OVSINST_OFPIT11_GOTO_TABLE]);
6488 ogt = ofpact_put_GOTO_TABLE(ofpacts);
6489 ogt->table_id = oigt->table_id;
6492 error = ofpacts_verify(ofpacts->data, ofpacts->size,
6493 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
6496 ofpbuf_clear(ofpacts);
6501 /* Update the length of the instruction that begins at offset 'ofs' within
6502 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
6503 * If the instruction contains no nested actions, deletes it entirely. */
6505 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
6507 struct ofp11_instruction_actions *oia;
6509 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
6510 if (openflow->size > ofs + sizeof *oia) {
6511 oia->len = htons(openflow->size - ofs);
6513 openflow->size = ofs;
6517 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
6518 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
6519 * valid, otherwise an OpenFlow error code. */
6521 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
6529 case OFPP_CONTROLLER:
6534 return OFPERR_OFPBAC_BAD_OUT_PORT;
6537 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
6540 return OFPERR_OFPBAC_BAD_OUT_PORT;
6544 /* Removes the protocols that require consistency between match and actions
6545 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
6547 * (An example of an inconsistency between match and actions is a flow that
6548 * does not match on an MPLS Ethertype but has an action that pops an MPLS
6551 inconsistent_match(enum ofputil_protocol *usable_protocols)
6553 *usable_protocols &= OFPUTIL_P_OF10_ANY;
6556 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
6557 * caller must restore them.
6559 * Modifies some actions, filling in fields that could not be properly set
6560 * without context. */
6562 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
6563 struct flow *flow, ofp_port_t max_ports,
6564 uint8_t table_id, uint8_t n_tables)
6566 const struct ofpact_enqueue *enqueue;
6567 const struct mf_field *mf;
6571 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
6574 case OFPACT_CONTROLLER:
6577 case OFPACT_ENQUEUE:
6578 enqueue = ofpact_get_ENQUEUE(a);
6579 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
6580 && enqueue->port != OFPP_IN_PORT
6581 && enqueue->port != OFPP_LOCAL) {
6582 return OFPERR_OFPBAC_BAD_OUT_PORT;
6586 case OFPACT_OUTPUT_REG:
6587 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
6590 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
6592 case OFPACT_SET_VLAN_VID:
6593 /* Remember if we saw a vlan tag in the flow to aid translating to
6594 * OpenFlow 1.1+ if need be. */
6595 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
6596 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6597 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6598 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
6599 inconsistent_match(usable_protocols);
6601 /* Temporary mark that we have a vlan tag. */
6602 flow->vlan_tci |= htons(VLAN_CFI);
6605 case OFPACT_SET_VLAN_PCP:
6606 /* Remember if we saw a vlan tag in the flow to aid translating to
6607 * OpenFlow 1.1+ if need be. */
6608 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
6609 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6610 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6611 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
6612 inconsistent_match(usable_protocols);
6614 /* Temporary mark that we have a vlan tag. */
6615 flow->vlan_tci |= htons(VLAN_CFI);
6618 case OFPACT_STRIP_VLAN:
6619 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
6620 inconsistent_match(usable_protocols);
6622 /* Temporary mark that we have no vlan tag. */
6623 flow->vlan_tci = htons(0);
6626 case OFPACT_PUSH_VLAN:
6627 if (flow->vlan_tci & htons(VLAN_CFI)) {
6628 /* Multiple VLAN headers not supported. */
6629 return OFPERR_OFPBAC_BAD_TAG;
6631 /* Temporary mark that we have a vlan tag. */
6632 flow->vlan_tci |= htons(VLAN_CFI);
6635 case OFPACT_SET_ETH_SRC:
6636 case OFPACT_SET_ETH_DST:
6639 case OFPACT_SET_IPV4_SRC:
6640 case OFPACT_SET_IPV4_DST:
6641 if (flow->dl_type != htons(ETH_TYPE_IP)) {
6642 inconsistent_match(usable_protocols);
6646 case OFPACT_SET_IP_DSCP:
6647 case OFPACT_SET_IP_ECN:
6648 case OFPACT_SET_IP_TTL:
6649 case OFPACT_DEC_TTL:
6650 if (!is_ip_any(flow)) {
6651 inconsistent_match(usable_protocols);
6655 case OFPACT_SET_L4_SRC_PORT:
6656 case OFPACT_SET_L4_DST_PORT:
6657 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
6658 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
6659 && flow->nw_proto != IPPROTO_SCTP)) {
6660 inconsistent_match(usable_protocols);
6662 /* Note on which transport protocol the port numbers are set.
6663 * This allows this set action to be converted to an OF1.2 set field
6665 if (a->type == OFPACT_SET_L4_SRC_PORT) {
6666 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
6668 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
6672 case OFPACT_REG_MOVE:
6673 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
6675 case OFPACT_SET_FIELD:
6676 mf = ofpact_get_SET_FIELD(a)->field;
6677 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6678 if (!mf_are_prereqs_ok(mf, flow) ||
6679 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
6680 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
6682 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6684 /* Remember if we saw a vlan tag in the flow to aid translating to
6685 * OpenFlow 1.1 if need be. */
6686 ofpact_get_SET_FIELD(a)->flow_has_vlan =
6687 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6688 if (mf->id == MFF_VLAN_TCI) {
6689 /* The set field may add or remove the vlan tag,
6690 * Mark the status temporarily. */
6691 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
6695 case OFPACT_STACK_PUSH:
6696 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
6698 case OFPACT_STACK_POP:
6699 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6701 case OFPACT_SET_MPLS_LABEL:
6702 case OFPACT_SET_MPLS_TC:
6703 case OFPACT_SET_MPLS_TTL:
6704 case OFPACT_DEC_MPLS_TTL:
6705 if (!eth_type_mpls(flow->dl_type)) {
6706 inconsistent_match(usable_protocols);
6710 case OFPACT_SET_TUNNEL:
6711 case OFPACT_SET_QUEUE:
6712 case OFPACT_POP_QUEUE:
6713 case OFPACT_RESUBMIT:
6716 case OFPACT_FIN_TIMEOUT:
6717 if (flow->nw_proto != IPPROTO_TCP) {
6718 inconsistent_match(usable_protocols);
6723 return learn_check(ofpact_get_LEARN(a), flow);
6725 case OFPACT_CONJUNCTION:
6728 case OFPACT_MULTIPATH:
6729 return multipath_check(ofpact_get_MULTIPATH(a), flow);
6735 case OFPACT_PUSH_MPLS:
6736 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6737 /* The packet is now MPLS and the MPLS payload is opaque.
6738 * Thus nothing can be assumed about the network protocol.
6739 * Temporarily mark that we have no nw_proto. */
6743 case OFPACT_POP_MPLS:
6744 if (!eth_type_mpls(flow->dl_type)) {
6745 inconsistent_match(usable_protocols);
6747 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6754 struct ofpact_conntrack *oc = ofpact_get_CT(a);
6757 if (!dl_type_is_ip_any(flow->dl_type)
6758 || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6759 inconsistent_match(usable_protocols);
6762 if (oc->zone_src.field) {
6763 return mf_check_src(&oc->zone_src, flow);
6766 err = ofpacts_check(oc->actions, ofpact_ct_get_action_len(oc),
6767 flow, max_ports, table_id, n_tables,
6773 struct ofpact_nat *on = ofpact_get_NAT(a);
6775 if (!dl_type_is_ip_any(flow->dl_type) ||
6776 (on->range_af == AF_INET && flow->dl_type != htons(ETH_TYPE_IP)) ||
6777 (on->range_af == AF_INET6
6778 && flow->dl_type != htons(ETH_TYPE_IPV6))) {
6779 inconsistent_match(usable_protocols);
6784 case OFPACT_CLEAR_ACTIONS:
6787 case OFPACT_WRITE_ACTIONS: {
6788 /* Use a temporary copy of 'usable_protocols' because we can't check
6789 * consistency of an action set. */
6790 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6791 enum ofputil_protocol p = *usable_protocols;
6792 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6793 flow, max_ports, table_id, n_tables, &p);
6796 case OFPACT_WRITE_METADATA:
6799 case OFPACT_METER: {
6800 uint32_t mid = ofpact_get_METER(a)->meter_id;
6801 if (mid == 0 || mid > OFPM13_MAX) {
6802 return OFPERR_OFPMMFC_INVALID_METER;
6807 case OFPACT_GOTO_TABLE: {
6808 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
6809 if ((table_id != 255 && goto_table <= table_id)
6810 || (n_tables != 255 && goto_table >= n_tables)) {
6811 return OFPERR_OFPBIC_BAD_TABLE_ID;
6819 case OFPACT_UNROLL_XLATE:
6820 /* UNROLL is an internal action that should never be seen via
6822 return OFPERR_OFPBAC_BAD_TYPE;
6824 case OFPACT_DEBUG_RECIRC:
6832 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6833 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6834 * switch with no more than 'max_ports' ports.
6836 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6837 * '*usable_protocols' the protocols that forbid the inconsistency. (An
6838 * example of an inconsistency between match and actions is a flow that does
6839 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6841 * May annotate ofpacts with information gathered from the 'flow'.
6843 * May temporarily modify 'flow', but restores the changes before returning. */
6845 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
6846 struct flow *flow, ofp_port_t max_ports,
6847 uint8_t table_id, uint8_t n_tables,
6848 enum ofputil_protocol *usable_protocols)
6851 ovs_be16 dl_type = flow->dl_type;
6852 ovs_be16 vlan_tci = flow->vlan_tci;
6853 uint8_t nw_proto = flow->nw_proto;
6854 enum ofperr error = 0;
6856 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6857 error = ofpact_check__(usable_protocols, a, flow,
6858 max_ports, table_id, n_tables);
6863 /* Restore fields that may have been modified. */
6864 flow->dl_type = dl_type;
6865 flow->vlan_tci = vlan_tci;
6866 flow->nw_proto = nw_proto;
6870 /* Like ofpacts_check(), but reports inconsistencies as
6871 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6873 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
6874 struct flow *flow, ofp_port_t max_ports,
6875 uint8_t table_id, uint8_t n_tables,
6876 enum ofputil_protocol usable_protocols)
6878 enum ofputil_protocol p = usable_protocols;
6881 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
6882 table_id, n_tables, &p);
6883 return (error ? error
6884 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
6888 /* Returns the destination field that 'ofpact' would write to, or NULL
6889 * if the action would not write to an mf_field. */
6890 const struct mf_field *
6891 ofpact_get_mf_dst(const struct ofpact *ofpact)
6893 if (ofpact->type == OFPACT_SET_FIELD) {
6894 const struct ofpact_set_field *orl;
6896 orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
6898 } else if (ofpact->type == OFPACT_REG_MOVE) {
6899 const struct ofpact_reg_move *orm;
6901 orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
6902 return orm->dst.field;
6909 unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action)
6911 VLOG_WARN("%s action doesn't support nested action %s",
6912 ofpact_name(outer_action), ofpact_name(action));
6913 return OFPERR_OFPBAC_BAD_ARGUMENT;
6917 field_requires_ct(enum mf_field_id field)
6919 return field == MFF_CT_MARK || field == MFF_CT_LABEL;
6922 /* Apply nesting constraints for actions */
6924 ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
6926 const struct mf_field *field = ofpact_get_mf_dst(a);
6928 if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
6929 VLOG_WARN("cannot set CT fields outside of ct action");
6930 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6932 if (a->type == OFPACT_NAT) {
6933 if (outer_action != OFPACT_CT) {
6934 VLOG_WARN("Cannot have NAT action outside of \"ct\" action");
6935 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6941 ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
6942 || outer_action == OFPACT_CT);
6944 if (outer_action == OFPACT_CT) {
6946 return unsupported_nesting(a->type, outer_action);
6947 } else if (!field_requires_ct(field->id)) {
6948 VLOG_WARN("%s action doesn't support nested modification "
6949 "of %s", ofpact_name(outer_action), field->name);
6950 return OFPERR_OFPBAC_BAD_ARGUMENT;
6958 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
6959 * appropriate order as defined by the OpenFlow spec and as required by Open
6962 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
6963 * instructions that are allowed within 'ofpacts[]'.
6965 * If 'outer_action' is not zero, it specifies that the actions are nested
6966 * within another action of type 'outer_action'. */
6968 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
6969 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
6971 const struct ofpact *a;
6972 enum ovs_instruction_type inst;
6974 inst = OVSINST_OFPIT13_METER;
6975 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6976 enum ovs_instruction_type next;
6979 if (a->type == OFPACT_CONJUNCTION) {
6980 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6981 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
6982 VLOG_WARN("\"conjunction\" actions may be used along with "
6983 "\"note\" but not any other kind of action "
6984 "(such as the \"%s\" action used here)",
6985 ofpact_name(a->type));
6986 return OFPERR_NXBAC_BAD_CONJUNCTION;
6992 error = ofpacts_verify_nested(a, outer_action);
6997 next = ovs_instruction_type_from_ofpact_type(a->type);
6999 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
7002 const char *name = ovs_instruction_name_from_type(inst);
7003 const char *next_name = ovs_instruction_name_from_type(next);
7006 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
7007 "1.1+ compatibility", name);
7009 VLOG_WARN("invalid instruction ordering: %s must appear "
7010 "before %s, for OpenFlow 1.1+ compatibility",
7013 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
7015 if (!((1u << next) & allowed_ovsinsts)) {
7016 const char *name = ovs_instruction_name_from_type(next);
7018 VLOG_WARN("%s instruction not allowed here", name);
7019 return OFPERR_OFPBIC_UNSUP_INST;
7028 /* Converting ofpacts to OpenFlow. */
7031 encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
7035 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7036 case OFPACT_##ENUM: \
7037 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
7046 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
7047 * actions in 'openflow', appending the actions to any existing data in
7050 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
7051 struct ofpbuf *openflow,
7052 enum ofp_version ofp_version)
7054 const struct ofpact *a;
7055 size_t start_size = openflow->size;
7057 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7058 encode_ofpact(a, ofp_version, openflow);
7060 return openflow->size - start_size;
7063 static enum ovs_instruction_type
7064 ofpact_is_apply_actions(const struct ofpact *a)
7066 return (ovs_instruction_type_from_ofpact_type(a->type)
7067 == OVSINST_OFPIT11_APPLY_ACTIONS);
7071 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
7073 struct ofpbuf *openflow,
7074 enum ofp_version ofp_version)
7076 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
7077 const struct ofpact *a;
7079 if (ofp_version == OFP10_VERSION) {
7080 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
7087 if (ofpact_is_apply_actions(a)) {
7088 size_t ofs = openflow->size;
7090 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
7092 encode_ofpact(a, ofp_version, openflow);
7094 } while (a < end && ofpact_is_apply_actions(a));
7095 ofpacts_update_instruction_actions(openflow, ofs);
7097 encode_ofpact(a, ofp_version, openflow);
7103 /* Sets of supported actions. */
7105 /* Two-way translation between OVS's internal "OFPACT_*" representation of
7106 * actions and the "OFPAT_*" representation used in some OpenFlow version.
7107 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
7108 * instance is specific to one OpenFlow version.) */
7110 enum ofpact_type ofpact; /* Internal name for action type. */
7111 int ofpat; /* OFPAT_* number from OpenFlow spec. */
7114 static const struct ofpact_map *
7115 get_ofpact_map(enum ofp_version version)
7117 /* OpenFlow 1.0 actions. */
7118 static const struct ofpact_map of10[] = {
7119 { OFPACT_OUTPUT, 0 },
7120 { OFPACT_SET_VLAN_VID, 1 },
7121 { OFPACT_SET_VLAN_PCP, 2 },
7122 { OFPACT_STRIP_VLAN, 3 },
7123 { OFPACT_SET_ETH_SRC, 4 },
7124 { OFPACT_SET_ETH_DST, 5 },
7125 { OFPACT_SET_IPV4_SRC, 6 },
7126 { OFPACT_SET_IPV4_DST, 7 },
7127 { OFPACT_SET_IP_DSCP, 8 },
7128 { OFPACT_SET_L4_SRC_PORT, 9 },
7129 { OFPACT_SET_L4_DST_PORT, 10 },
7130 { OFPACT_ENQUEUE, 11 },
7134 /* OpenFlow 1.1 actions. */
7135 static const struct ofpact_map of11[] = {
7136 { OFPACT_OUTPUT, 0 },
7137 { OFPACT_SET_VLAN_VID, 1 },
7138 { OFPACT_SET_VLAN_PCP, 2 },
7139 { OFPACT_SET_ETH_SRC, 3 },
7140 { OFPACT_SET_ETH_DST, 4 },
7141 { OFPACT_SET_IPV4_SRC, 5 },
7142 { OFPACT_SET_IPV4_DST, 6 },
7143 { OFPACT_SET_IP_DSCP, 7 },
7144 { OFPACT_SET_IP_ECN, 8 },
7145 { OFPACT_SET_L4_SRC_PORT, 9 },
7146 { OFPACT_SET_L4_DST_PORT, 10 },
7147 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7148 /* OFPAT_COPY_TTL_IN (12) not supported. */
7149 { OFPACT_SET_MPLS_LABEL, 13 },
7150 { OFPACT_SET_MPLS_TC, 14 },
7151 { OFPACT_SET_MPLS_TTL, 15 },
7152 { OFPACT_DEC_MPLS_TTL, 16 },
7153 { OFPACT_PUSH_VLAN, 17 },
7154 { OFPACT_STRIP_VLAN, 18 },
7155 { OFPACT_PUSH_MPLS, 19 },
7156 { OFPACT_POP_MPLS, 20 },
7157 { OFPACT_SET_QUEUE, 21 },
7158 { OFPACT_GROUP, 22 },
7159 { OFPACT_SET_IP_TTL, 23 },
7160 { OFPACT_DEC_TTL, 24 },
7164 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
7165 static const struct ofpact_map of12[] = {
7166 { OFPACT_OUTPUT, 0 },
7167 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7168 /* OFPAT_COPY_TTL_IN (12) not supported. */
7169 { OFPACT_SET_MPLS_TTL, 15 },
7170 { OFPACT_DEC_MPLS_TTL, 16 },
7171 { OFPACT_PUSH_VLAN, 17 },
7172 { OFPACT_STRIP_VLAN, 18 },
7173 { OFPACT_PUSH_MPLS, 19 },
7174 { OFPACT_POP_MPLS, 20 },
7175 { OFPACT_SET_QUEUE, 21 },
7176 { OFPACT_GROUP, 22 },
7177 { OFPACT_SET_IP_TTL, 23 },
7178 { OFPACT_DEC_TTL, 24 },
7179 { OFPACT_SET_FIELD, 25 },
7180 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
7181 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
7202 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
7203 * values, into a bitmap of actions suitable for OpenFlow 'version', and
7204 * returns the result. */
7206 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
7208 uint32_t openflow_bitmap = 0;
7209 const struct ofpact_map *x;
7211 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7212 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
7213 openflow_bitmap |= 1u << x->ofpat;
7216 return htonl(openflow_bitmap);
7219 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
7220 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
7221 * and returns the result. */
7223 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
7225 uint64_t ofpact_bitmap = 0;
7226 const struct ofpact_map *x;
7228 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7229 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
7230 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
7233 return ofpact_bitmap;
7236 /* Appends to 's' a string representation of the set of OFPACT_* represented
7237 * by 'ofpacts_bitmap'. */
7239 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
7241 if (!ofpacts_bitmap) {
7242 ds_put_cstr(s, "<none>");
7244 while (ofpacts_bitmap) {
7245 ds_put_format(s, "%s ",
7246 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
7247 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
7253 /* Returns true if 'action' outputs to 'port', false otherwise. */
7255 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
7257 switch (ofpact->type) {
7259 return ofpact_get_OUTPUT(ofpact)->port == port;
7260 case OFPACT_ENQUEUE:
7261 return ofpact_get_ENQUEUE(ofpact)->port == port;
7262 case OFPACT_CONTROLLER:
7263 return port == OFPP_CONTROLLER;
7265 case OFPACT_OUTPUT_REG:
7267 case OFPACT_SET_VLAN_VID:
7268 case OFPACT_SET_VLAN_PCP:
7269 case OFPACT_STRIP_VLAN:
7270 case OFPACT_PUSH_VLAN:
7271 case OFPACT_SET_ETH_SRC:
7272 case OFPACT_SET_ETH_DST:
7273 case OFPACT_SET_IPV4_SRC:
7274 case OFPACT_SET_IPV4_DST:
7275 case OFPACT_SET_IP_DSCP:
7276 case OFPACT_SET_IP_ECN:
7277 case OFPACT_SET_IP_TTL:
7278 case OFPACT_SET_L4_SRC_PORT:
7279 case OFPACT_SET_L4_DST_PORT:
7280 case OFPACT_REG_MOVE:
7281 case OFPACT_SET_FIELD:
7282 case OFPACT_STACK_PUSH:
7283 case OFPACT_STACK_POP:
7284 case OFPACT_DEC_TTL:
7285 case OFPACT_SET_MPLS_LABEL:
7286 case OFPACT_SET_MPLS_TC:
7287 case OFPACT_SET_MPLS_TTL:
7288 case OFPACT_DEC_MPLS_TTL:
7289 case OFPACT_SET_TUNNEL:
7290 case OFPACT_WRITE_METADATA:
7291 case OFPACT_SET_QUEUE:
7292 case OFPACT_POP_QUEUE:
7293 case OFPACT_FIN_TIMEOUT:
7294 case OFPACT_RESUBMIT:
7296 case OFPACT_CONJUNCTION:
7297 case OFPACT_MULTIPATH:
7300 case OFPACT_UNROLL_XLATE:
7301 case OFPACT_PUSH_MPLS:
7302 case OFPACT_POP_MPLS:
7304 case OFPACT_CLEAR_ACTIONS:
7305 case OFPACT_WRITE_ACTIONS:
7306 case OFPACT_GOTO_TABLE:
7309 case OFPACT_DEBUG_RECIRC:
7317 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7318 * to 'port', false otherwise. */
7320 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
7323 const struct ofpact *a;
7325 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
7326 if (ofpact_outputs_to_port(a, port)) {
7334 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7335 * to 'group', false otherwise. */
7337 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
7340 const struct ofpact *a;
7342 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
7343 if (a->type == OFPACT_GROUP
7344 && ofpact_get_GROUP(a)->group_id == group_id) {
7353 ofpacts_equal(const struct ofpact *a, size_t a_len,
7354 const struct ofpact *b, size_t b_len)
7356 return a_len == b_len && !memcmp(a, b, a_len);
7359 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
7360 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
7362 * This function relies on the order of 'ofpacts' being correct (as checked by
7363 * ofpacts_verify()). */
7365 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
7367 const struct ofpact *a;
7369 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7370 enum ovs_instruction_type inst;
7372 inst = ovs_instruction_type_from_ofpact_type(a->type);
7373 if (a->type == OFPACT_METER) {
7374 return ofpact_get_METER(a)->meter_id;
7375 } else if (inst > OVSINST_OFPIT13_METER) {
7383 /* Formatting ofpacts. */
7386 ofpact_format(const struct ofpact *a, struct ds *s)
7389 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7390 case OFPACT_##ENUM: \
7391 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
7400 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
7401 * 'ofpacts' to 'string'. */
7403 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
7407 ds_put_format(string, "%sdrop%s", colors.drop, colors.end);
7409 const struct ofpact *a;
7411 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7413 ds_put_char(string, ',');
7416 /* XXX write-actions */
7417 ofpact_format(a, string);
7422 /* Internal use by helpers. */
7424 /* Implementation of ofpact_put_<ENUM>(). */
7426 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
7428 struct ofpact *ofpact;
7430 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
7431 ofpact = ofpacts->header;
7432 ofpact_init(ofpact, type, len);
7436 /* Implementation of ofpact_init_<ENUM>(). */
7438 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
7440 memset(ofpact, 0, len);
7441 ofpact->type = type;
7446 /* Implementation of ofpact_finish_<ENUM>().
7448 * Finishes composing a variable-length action (begun using
7449 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
7450 * bytes and updating its embedded length field. See the large comment near
7451 * the end of ofp-actions.h for more information.
7453 * May reallocate 'ofpacts'. Callers should consider updating their 'ofpact'
7454 * pointer to the return value of this function. */
7456 ofpact_finish(struct ofpbuf *ofpacts, struct ofpact *ofpact)
7460 ovs_assert(ofpact == ofpacts->header);
7461 len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
7462 ovs_assert(len > 0 && len <= UINT16_MAX);
7464 ofpbuf_padto(ofpacts, OFPACT_ALIGN(ofpacts->size));
7466 return ofpacts->header;
7469 static char * OVS_WARN_UNUSED_RESULT
7470 ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
7471 enum ofputil_protocol *usable_protocols)
7474 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7475 case OFPACT_##ENUM: \
7476 return parse_##ENUM(value, ofpacts, usable_protocols);
7485 ofpact_type_from_name(const char *name, enum ofpact_type *type)
7487 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7488 if (!strcasecmp(name, NAME)) { \
7489 *type = OFPACT_##ENUM; \
7498 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
7500 * Returns NULL if successful, otherwise a malloc()'d string describing the
7501 * error. The caller is responsible for freeing the returned string.
7503 * If 'outer_action' is specified, indicates that the actions being parsed
7504 * are nested within another action of the type specified in 'outer_action'. */
7505 static char * OVS_WARN_UNUSED_RESULT
7506 ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
7507 enum ofputil_protocol *usable_protocols,
7508 bool allow_instructions, enum ofpact_type outer_action)
7517 while (ofputil_parse_key_value(&pos, &key, &value)) {
7518 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
7519 enum ofpact_type type;
7523 if (ofpact_type_from_name(key, &type)) {
7524 error = ofpact_parse(type, value, ofpacts, usable_protocols);
7525 inst = ovs_instruction_type_from_ofpact_type(type);
7526 } else if (!strcasecmp(key, "mod_vlan_vid")) {
7527 error = parse_set_vlan_vid(value, ofpacts, true);
7528 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
7529 error = parse_set_vlan_pcp(value, ofpacts, true);
7530 } else if (!strcasecmp(key, "set_nw_ttl")) {
7531 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
7532 } else if (!strcasecmp(key, "pop_vlan")) {
7533 error = parse_pop_vlan(ofpacts);
7534 } else if (!strcasecmp(key, "set_tunnel64")) {
7535 error = parse_set_tunnel(value, ofpacts,
7536 NXAST_RAW_SET_TUNNEL64);
7537 } else if (!strcasecmp(key, "load")) {
7538 error = parse_reg_load(value, ofpacts);
7539 } else if (!strcasecmp(key, "bundle_load")) {
7540 error = parse_bundle_load(value, ofpacts);
7541 } else if (!strcasecmp(key, "drop")) {
7543 } else if (!strcasecmp(key, "apply_actions")) {
7544 return xstrdup("apply_actions is the default instruction");
7545 } else if (ofputil_port_from_string(key, &port)) {
7546 ofpact_put_OUTPUT(ofpacts)->port = port;
7548 return xasprintf("unknown action %s", key);
7554 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
7555 if (!allow_instructions) {
7556 return xasprintf("only actions are allowed here (not "
7558 ovs_instruction_name_from_type(inst));
7560 if (inst == prev_inst) {
7561 return xasprintf("instruction %s may be specified only once",
7562 ovs_instruction_name_from_type(inst));
7565 if (prev_inst != -1 && inst < prev_inst) {
7566 return xasprintf("instruction %s must be specified before %s",
7567 ovs_instruction_name_from_type(inst),
7568 ovs_instruction_name_from_type(prev_inst));
7573 if (drop && ofpacts->size) {
7574 return xstrdup("\"drop\" must not be accompanied by any other action "
7578 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
7580 ? (1u << N_OVS_INSTRUCTIONS) - 1
7581 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
7584 return xstrdup("Incorrect instruction ordering");
7590 static char * OVS_WARN_UNUSED_RESULT
7591 ofpacts_parse(char *str, struct ofpbuf *ofpacts,
7592 enum ofputil_protocol *usable_protocols, bool allow_instructions,
7593 enum ofpact_type outer_action)
7595 uint32_t orig_size = ofpacts->size;
7596 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
7597 allow_instructions, outer_action);
7599 ofpacts->size = orig_size;
7604 static char * OVS_WARN_UNUSED_RESULT
7605 ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
7606 enum ofputil_protocol *usable_protocols,
7607 bool allow_instructions, enum ofpact_type outer_action)
7611 *usable_protocols = OFPUTIL_P_ANY;
7614 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
7621 /* Parses 's' as a set of OpenFlow actions and appends the actions to
7622 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
7623 * that are nested within the action of type 'outer_action'.
7625 * Returns NULL if successful, otherwise a malloc()'d string describing the
7626 * error. The caller is responsible for freeing the returned string. */
7627 char * OVS_WARN_UNUSED_RESULT
7628 ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
7629 enum ofputil_protocol *usable_protocols)
7631 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
7634 /* Parses 's' as a set of OpenFlow instructions and appends the instructions to
7637 * Returns NULL if successful, otherwise a malloc()'d string describing the
7638 * error. The caller is responsible for freeing the returned string. */
7639 char * OVS_WARN_UNUSED_RESULT
7640 ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
7641 enum ofputil_protocol *usable_protocols)
7643 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
7647 ofpact_name(enum ofpact_type type)
7650 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7657 /* Low-level action decoding and encoding functions. */
7659 /* Everything needed to identify a particular OpenFlow action. */
7660 struct ofpact_hdrs {
7661 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
7662 uint16_t type; /* Type if standard, otherwise subtype. */
7663 uint8_t ofp_version; /* From ofp_header. */
7666 /* Information about a particular OpenFlow action. */
7667 struct ofpact_raw_instance {
7668 /* The action's identity. */
7669 struct ofpact_hdrs hdrs;
7670 enum ofp_raw_action_type raw;
7672 /* Looking up the action. */
7673 struct hmap_node decode_node; /* Based on 'hdrs'. */
7674 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
7676 /* The action's encoded size.
7678 * If this action is fixed-length, 'min_length' == 'max_length'.
7679 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7680 * OFP_ACTION_ALIGN) == 65528. */
7681 unsigned short int min_length;
7682 unsigned short int max_length;
7684 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7685 * offset of that argument from the beginning of the action and 'arg_len'
7686 * its length, both in bytes.
7688 * For actions that take other forms, these are both zero. */
7689 unsigned short int arg_ofs;
7690 unsigned short int arg_len;
7692 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7695 /* If this action is deprecated, a human-readable string with a brief
7697 const char *deprecation;
7700 /* Action header. */
7701 struct ofp_action_header {
7702 /* The meaning of other values of 'type' generally depends on the OpenFlow
7703 * version (see enum ofp_raw_action_type).
7705 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7706 * designates an OpenFlow vendor ID and that the remainder of the action
7707 * structure has a vendor-defined meaning.
7709 #define OFPAT_VENDOR 0xffff
7712 /* Always a multiple of 8. */
7715 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7716 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
7719 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
7721 /* Header for Nicira-defined actions and for ONF vendor extensions.
7723 * This cannot be used as an entirely generic vendor extension action header,
7724 * because OpenFlow does not specify the location or size of the action
7725 * subtype; it just happens that ONF extensions and Nicira extensions share
7727 struct ext_action_header {
7728 ovs_be16 type; /* OFPAT_VENDOR. */
7729 ovs_be16 len; /* At least 16. */
7730 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
7731 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
7734 OFP_ASSERT(sizeof(struct ext_action_header) == 16);
7737 ofpact_hdrs_equal(const struct ofpact_hdrs *a,
7738 const struct ofpact_hdrs *b)
7740 return (a->vendor == b->vendor
7741 && a->type == b->type
7742 && a->ofp_version == b->ofp_version);
7746 ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
7748 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
7751 #include "ofp-actions.inc2"
7753 static struct hmap *
7754 ofpact_decode_hmap(void)
7756 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7757 static struct hmap hmap;
7759 if (ovsthread_once_start(&once)) {
7760 struct ofpact_raw_instance *inst;
7763 for (inst = all_raw_instances;
7764 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7766 hmap_insert(&hmap, &inst->decode_node,
7767 ofpact_hdrs_hash(&inst->hdrs));
7769 ovsthread_once_done(&once);
7774 static struct hmap *
7775 ofpact_encode_hmap(void)
7777 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7778 static struct hmap hmap;
7780 if (ovsthread_once_start(&once)) {
7781 struct ofpact_raw_instance *inst;
7784 for (inst = all_raw_instances;
7785 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7787 hmap_insert(&hmap, &inst->encode_node,
7788 hash_2words(inst->raw, inst->hdrs.ofp_version));
7790 ovsthread_once_done(&once);
7796 ofpact_decode_raw(enum ofp_version ofp_version,
7797 const struct ofp_action_header *oah, size_t length,
7798 const struct ofpact_raw_instance **instp)
7800 const struct ofpact_raw_instance *inst;
7801 struct ofpact_hdrs hdrs;
7804 if (length < sizeof *oah) {
7805 return OFPERR_OFPBAC_BAD_LEN;
7808 /* Get base action type. */
7809 if (oah->type == htons(OFPAT_VENDOR)) {
7811 hdrs.vendor = ntohl(oah->vendor);
7812 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
7813 /* Get extension subtype. */
7814 const struct ext_action_header *nah;
7816 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
7817 if (length < sizeof *nah) {
7818 return OFPERR_OFPBAC_BAD_LEN;
7820 hdrs.type = ntohs(nah->subtype);
7822 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
7824 return OFPERR_OFPBAC_BAD_VENDOR;
7828 hdrs.type = ntohs(oah->type);
7831 hdrs.ofp_version = ofp_version;
7832 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
7833 ofpact_decode_hmap()) {
7834 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
7841 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7842 : OFPERR_OFPBAC_BAD_TYPE);
7846 ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7847 enum ofp_raw_action_type *raw, uint64_t *arg)
7849 const struct ofp_action_header *oah = buf->data;
7850 const struct ofpact_raw_instance *action;
7851 unsigned int length;
7855 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
7860 if (action->deprecation) {
7861 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
7862 action->name, ofputil_version_to_string(ofp_version),
7863 action->deprecation);
7866 length = ntohs(oah->len);
7867 if (length > buf->size) {
7868 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
7869 "length %"PRIu32, action->name, length, buf->size);
7870 return OFPERR_OFPBAC_BAD_LEN;
7872 if (length < action->min_length || length > action->max_length) {
7873 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
7874 "[%hu,%hu]", action->name, length,
7875 action->min_length, action->max_length);
7876 return OFPERR_OFPBAC_BAD_LEN;
7879 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
7880 "of 8", action->name, length);
7881 return OFPERR_OFPBAC_BAD_LEN;
7886 if (action->arg_len) {
7890 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
7891 for (i = 0; i < action->arg_len; i++) {
7892 *arg = (*arg << 8) | p[i];
7896 ofpbuf_pull(buf, length);
7901 static const struct ofpact_raw_instance *
7902 ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
7904 const struct ofpact_raw_instance *inst;
7906 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
7907 ofpact_encode_hmap()) {
7908 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
7916 ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7917 enum ofp_raw_action_type raw, uint64_t arg)
7919 const struct ofpact_raw_instance *inst;
7920 struct ofp_action_header *oah;
7921 const struct ofpact_hdrs *hdrs;
7923 inst = ofpact_raw_lookup(ofp_version, raw);
7926 oah = ofpbuf_put_zeros(buf, inst->min_length);
7927 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
7928 oah->len = htons(inst->min_length);
7929 oah->vendor = htonl(hdrs->vendor);
7931 switch (hdrs->vendor) {
7936 case ONF_VENDOR_ID: {
7937 struct ext_action_header *nah = (struct ext_action_header *) oah;
7938 nah->subtype = htons(hdrs->type);
7946 if (inst->arg_len) {
7947 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
7950 for (i = 0; i < inst->arg_len; i++) {
7962 pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
7964 struct ofp_action_header *oah;
7966 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs,
7969 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
7970 oah->len = htons(openflow->size - start_ofs);