2 * Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017 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.
17 #ifndef OPENVSWITCH_OFP_ACTIONS_H
18 #define OPENVSWITCH_OFP_ACTIONS_H 1
22 #include "openflow/openflow.h"
23 #include "openflow/nicira-ext.h"
24 #include "openvswitch/meta-flow.h"
25 #include "openvswitch/ofp-util.h"
26 #include "openvswitch/ofp-errors.h"
27 #include "openvswitch/types.h"
31 /* List of OVS abstracted actions.
33 * This macro is used directly only internally by this header, but the list is
34 * still of interest to developers.
36 * Each OFPACT invocation has the following parameters:
38 * 1. <ENUM>, used below in the enum definition of OFPACT_<ENUM>, and
41 * 2. <STRUCT> corresponding to a structure "struct <STRUCT>", that must be
42 * defined below. This structure must be an abstract definition of the
43 * action. Its first member must have type "struct ofpact" and name
44 * "ofpact". It may be fixed length or end with a flexible array member
45 * (e.g. "int member[];").
47 * 3. <MEMBER>, which has one of two possible values:
49 * - If "struct <STRUCT>" is fixed-length, it must be "ofpact".
51 * - If "struct <STRUCT>" is variable-length, it must be the name of the
52 * flexible array member.
54 * 4. <NAME>, a quoted string that gives the name of the action, for use in
55 * parsing actions from text.
59 OFPACT(OUTPUT, ofpact_output, ofpact, "output") \
60 OFPACT(GROUP, ofpact_group, ofpact, "group") \
61 OFPACT(CONTROLLER, ofpact_controller, userdata, "controller") \
62 OFPACT(ENQUEUE, ofpact_enqueue, ofpact, "enqueue") \
63 OFPACT(OUTPUT_REG, ofpact_output_reg, ofpact, "output_reg") \
64 OFPACT(BUNDLE, ofpact_bundle, slaves, "bundle") \
66 /* Header changes. */ \
67 OFPACT(SET_FIELD, ofpact_set_field, ofpact, "set_field") \
68 OFPACT(SET_VLAN_VID, ofpact_vlan_vid, ofpact, "set_vlan_vid") \
69 OFPACT(SET_VLAN_PCP, ofpact_vlan_pcp, ofpact, "set_vlan_pcp") \
70 OFPACT(STRIP_VLAN, ofpact_null, ofpact, "strip_vlan") \
71 OFPACT(PUSH_VLAN, ofpact_null, ofpact, "push_vlan") \
72 OFPACT(SET_ETH_SRC, ofpact_mac, ofpact, "mod_dl_src") \
73 OFPACT(SET_ETH_DST, ofpact_mac, ofpact, "mod_dl_dst") \
74 OFPACT(SET_IPV4_SRC, ofpact_ipv4, ofpact, "mod_nw_src") \
75 OFPACT(SET_IPV4_DST, ofpact_ipv4, ofpact, "mod_nw_dst") \
76 OFPACT(SET_IP_DSCP, ofpact_dscp, ofpact, "mod_nw_tos") \
77 OFPACT(SET_IP_ECN, ofpact_ecn, ofpact, "mod_nw_ecn") \
78 OFPACT(SET_IP_TTL, ofpact_ip_ttl, ofpact, "mod_nw_ttl") \
79 OFPACT(SET_L4_SRC_PORT, ofpact_l4_port, ofpact, "mod_tp_src") \
80 OFPACT(SET_L4_DST_PORT, ofpact_l4_port, ofpact, "mod_tp_dst") \
81 OFPACT(REG_MOVE, ofpact_reg_move, ofpact, "move") \
82 OFPACT(STACK_PUSH, ofpact_stack, ofpact, "push") \
83 OFPACT(STACK_POP, ofpact_stack, ofpact, "pop") \
84 OFPACT(DEC_TTL, ofpact_cnt_ids, cnt_ids, "dec_ttl") \
85 OFPACT(SET_MPLS_LABEL, ofpact_mpls_label, ofpact, "set_mpls_label") \
86 OFPACT(SET_MPLS_TC, ofpact_mpls_tc, ofpact, "set_mpls_tc") \
87 OFPACT(SET_MPLS_TTL, ofpact_mpls_ttl, ofpact, "set_mpls_ttl") \
88 OFPACT(DEC_MPLS_TTL, ofpact_null, ofpact, "dec_mpls_ttl") \
89 OFPACT(PUSH_MPLS, ofpact_push_mpls, ofpact, "push_mpls") \
90 OFPACT(POP_MPLS, ofpact_pop_mpls, ofpact, "pop_mpls") \
93 OFPACT(SET_TUNNEL, ofpact_tunnel, ofpact, "set_tunnel") \
94 OFPACT(SET_QUEUE, ofpact_queue, ofpact, "set_queue") \
95 OFPACT(POP_QUEUE, ofpact_null, ofpact, "pop_queue") \
96 OFPACT(FIN_TIMEOUT, ofpact_fin_timeout, ofpact, "fin_timeout") \
98 /* Flow table interaction. */ \
99 OFPACT(RESUBMIT, ofpact_resubmit, ofpact, "resubmit") \
100 OFPACT(LEARN, ofpact_learn, specs, "learn") \
101 OFPACT(CONJUNCTION, ofpact_conjunction, ofpact, "conjunction") \
104 OFPACT(MULTIPATH, ofpact_multipath, ofpact, "multipath") \
107 OFPACT(NOTE, ofpact_note, data, "note") \
108 OFPACT(EXIT, ofpact_null, ofpact, "exit") \
109 OFPACT(SAMPLE, ofpact_sample, ofpact, "sample") \
110 OFPACT(UNROLL_XLATE, ofpact_unroll_xlate, ofpact, "unroll_xlate") \
111 OFPACT(CT, ofpact_conntrack, ofpact, "ct") \
112 OFPACT(CT_CLEAR, ofpact_null, ofpact, "ct_clear") \
113 OFPACT(NAT, ofpact_nat, ofpact, "nat") \
114 OFPACT(OUTPUT_TRUNC, ofpact_output_trunc,ofpact, "output_trunc") \
115 OFPACT(CLONE, ofpact_nest, actions, "clone") \
117 /* Debugging actions. \
119 * These are intentionally undocumented, subject to change, and \
120 * only accepted if ovs-vswitchd is started with --enable-dummy. */ \
121 OFPACT(DEBUG_RECIRC, ofpact_null, ofpact, "debug_recirc") \
123 /* Instructions. */ \
124 OFPACT(METER, ofpact_meter, ofpact, "meter") \
125 OFPACT(CLEAR_ACTIONS, ofpact_null, ofpact, "clear_actions") \
126 OFPACT(WRITE_ACTIONS, ofpact_nest, actions, "write_actions") \
127 OFPACT(WRITE_METADATA, ofpact_metadata, ofpact, "write_metadata") \
128 OFPACT(GOTO_TABLE, ofpact_goto_table, ofpact, "goto_table")
130 /* enum ofpact_type, with a member OFPACT_<ENUM> for each action. */
131 enum OVS_PACKED_ENUM ofpact_type
{
132 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) OFPACT_##ENUM,
137 /* Define N_OFPACTS to the number of types of ofpacts. */
139 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) + 1
144 /* Header for an action.
146 * Each action is a structure "struct ofpact_*" that begins with "struct
147 * ofpact", usually followed by other data that describes the action. Actions
148 * are padded out to a multiple of OFPACT_ALIGNTO bytes in length.
150 * The 'raw' member is special:
152 * - Most "struct ofpact"s correspond to one particular kind of OpenFlow
153 * action, at least in a given OpenFlow version. For example,
154 * OFPACT_SET_VLAN_VID corresponds to OFPAT10_SET_VLAN_VID in OpenFlow
157 * For such actions, the 'raw' member is not meaningful and generally
160 * - A few "struct ofpact"s correspond to multiple OpenFlow actions. For
161 * example, OFPACT_SET_TUNNEL can be NXAST_SET_TUNNEL or
162 * NXAST_SET_TUNNEL64. In these cases, if the "struct ofpact" originated
163 * from OpenFlow, then we want to make sure that, if it gets translated
164 * back to OpenFlow later, it is translated back to the same action type.
165 * (Otherwise, we'd violate the promise made in the topics/design doc, in
166 * the "Action Reproduction" section.)
168 * For such actions, the 'raw' member should be the "enum ofp_raw_action"
169 * originally extracted from the OpenFlow action. (If the action didn't
170 * originate from OpenFlow, then setting 'raw' to zero should be fine:
171 * code to translate the ofpact to OpenFlow must tolerate this case.)
174 /* We want the space advantage of an 8-bit type here on every
175 * implementation, without giving up the advantage of having a useful type
176 * on implementations that support packed enums. */
177 #ifdef HAVE_PACKED_ENUM
178 enum ofpact_type type
; /* OFPACT_*. */
180 uint8_t type
; /* OFPACT_* */
183 uint8_t raw
; /* Original type when added, if any. */
184 uint16_t len
; /* Length of the action, in bytes, including
185 * struct ofpact, excluding padding. */
187 BUILD_ASSERT_DECL(sizeof(struct ofpact
) == 4);
190 #define OFPACT_ALIGNTO 8
191 #define OFPACT_ALIGN(SIZE) ROUND_UP(SIZE, OFPACT_ALIGNTO)
192 #define OFPACT_PADDED_MEMBERS(MEMBERS) PADDED_MEMBERS(OFPACT_ALIGNTO, MEMBERS)
194 /* Returns the ofpact following 'ofpact'. */
195 static inline struct ofpact
*
196 ofpact_next(const struct ofpact
*ofpact
)
198 return (void *) ((uint8_t *) ofpact
+ OFPACT_ALIGN(ofpact
->len
));
201 struct ofpact
*ofpact_next_flattened(const struct ofpact
*);
203 static inline struct ofpact
*
204 ofpact_end(const struct ofpact
*ofpacts
, size_t ofpacts_len
)
206 return (void *) ((uint8_t *) ofpacts
+ ofpacts_len
);
209 static inline const struct ofpact
*
210 ofpact_find_type_flattened(const struct ofpact
*a
, enum ofpact_type type
,
211 const struct ofpact
* const end
)
214 if (a
->type
== type
) {
217 a
= ofpact_next_flattened(a
);
222 #define OFPACT_FIND_TYPE_FLATTENED(A, TYPE, END) \
223 ofpact_get_##TYPE##_nullable( \
224 ofpact_find_type_flattened(A, OFPACT_##TYPE, END))
226 /* Assigns POS to each ofpact, in turn, in the OFPACTS_LEN bytes of ofpacts
227 * starting at OFPACTS. */
228 #define OFPACT_FOR_EACH(POS, OFPACTS, OFPACTS_LEN) \
229 for ((POS) = (OFPACTS); (POS) < ofpact_end(OFPACTS, OFPACTS_LEN); \
230 (POS) = ofpact_next(POS))
232 /* Assigns POS to each ofpact, in turn, in the OFPACTS_LEN bytes of ofpacts
233 * starting at OFPACTS.
235 * For ofpacts that contain nested ofpacts, this visits each of the inner
236 * ofpacts as well. */
237 #define OFPACT_FOR_EACH_FLATTENED(POS, OFPACTS, OFPACTS_LEN) \
238 for ((POS) = (OFPACTS); (POS) < ofpact_end(OFPACTS, OFPACTS_LEN); \
239 (POS) = ofpact_next_flattened(POS))
241 #define OFPACT_FOR_EACH_TYPE_FLATTENED(POS, TYPE, OFPACTS, OFPACTS_LEN) \
242 for ((POS) = OFPACT_FIND_TYPE_FLATTENED(OFPACTS, TYPE, \
243 ofpact_end(OFPACTS, OFPACTS_LEN)); \
245 (POS) = OFPACT_FIND_TYPE_FLATTENED( \
246 ofpact_next_flattened(&(POS)->ofpact), TYPE, \
247 ofpact_end(OFPACTS, OFPACTS_LEN)))
249 /* Action structure for each OFPACT_*. */
251 /* OFPACT_STRIP_VLAN, OFPACT_POP_QUEUE, OFPACT_EXIT, OFPACT_CLEAR_ACTIONS.
253 * Used for OFPAT10_STRIP_VLAN, NXAST_POP_QUEUE, NXAST_EXIT,
254 * OFPAT11_POP_VLAN, OFPIT11_CLEAR_ACTIONS.
256 * Action structure for actions that do not have any extra data beyond the
259 struct ofpact ofpact
;
264 * Used for OFPAT10_OUTPUT. */
265 struct ofpact_output
{
266 struct ofpact ofpact
;
267 ofp_port_t port
; /* Output port. */
268 uint16_t max_len
; /* Max send len, for port OFPP_CONTROLLER. */
271 /* OFPACT_CONTROLLER.
273 * Used for NXAST_CONTROLLER. */
274 struct ofpact_controller
{
275 OFPACT_PADDED_MEMBERS(
276 struct ofpact ofpact
;
277 uint16_t max_len
; /* Max length to send to controller. */
278 uint16_t controller_id
; /* Controller ID to send packet-in. */
279 enum ofp_packet_in_reason reason
; /* Reason to put in packet-in. */
281 /* If true, this action freezes packet traversal of the OpenFlow
282 * tables and adds a continuation to the packet-in message, that
283 * a controller can use to resume that traversal. */
286 /* Arbitrary data to include in the packet-in message (currently,
287 * only in NXT_PACKET_IN2). */
288 uint16_t userdata_len
;
295 * Used for OFPAT10_ENQUEUE. */
296 struct ofpact_enqueue
{
297 struct ofpact ofpact
;
302 /* OFPACT_OUTPUT_REG.
304 * Used for NXAST_OUTPUT_REG. */
305 struct ofpact_output_reg
{
306 struct ofpact ofpact
;
308 struct mf_subfield src
;
311 /* OFPACT_OUTPUT_TRUNC.
313 * Used for NXAST_OUTPUT_TRUNC. */
314 struct ofpact_output_trunc
{
315 struct ofpact ofpact
;
316 ofp_port_t port
; /* Output port. */
317 uint32_t max_len
; /* Max send len. */
320 /* Bundle slave choice algorithm to apply.
322 * In the descriptions below, 'slaves' is the list of possible slaves in the
323 * order they appear in the OpenFlow action. */
324 enum nx_bd_algorithm
{
325 /* Chooses the first live slave listed in the bundle.
327 * O(n_slaves) performance. */
328 NX_BD_ALG_ACTIVE_BACKUP
= 0,
330 /* Highest Random Weight.
332 * for i in [0,n_slaves):
333 * weights[i] = hash(flow, i)
334 * slave = { slaves[i] such that weights[i] >= weights[j] for all j != i }
336 * Redistributes 1/n_slaves of traffic when a slave's liveness changes.
337 * O(n_slaves) performance.
339 * Uses the 'fields' and 'basis' parameters. */
345 * Used for NXAST_BUNDLE. */
346 struct ofpact_bundle
{
347 struct ofpact ofpact
;
349 /* Slave choice algorithm to apply to hash value. */
350 enum nx_bd_algorithm algorithm
;
352 /* What fields to hash and how. */
353 enum nx_hash_fields fields
;
354 uint16_t basis
; /* Universal hash parameter. */
356 struct mf_subfield dst
;
358 /* Slaves for output. */
359 unsigned int n_slaves
;
363 /* OFPACT_SET_VLAN_VID.
365 * We keep track if vlan was present at action validation time to avoid a
366 * PUSH_VLAN when translating to OpenFlow 1.1+.
368 * We also keep the originating OFPUTIL action code in ofpact.compat.
370 * Used for OFPAT10_SET_VLAN_VID and OFPAT11_SET_VLAN_VID. */
371 struct ofpact_vlan_vid
{
372 struct ofpact ofpact
;
373 uint16_t vlan_vid
; /* VLAN VID in low 12 bits, 0 in other bits. */
374 bool push_vlan_if_needed
; /* OF 1.0 semantics if true. */
375 bool flow_has_vlan
; /* VLAN present at action validation time? */
378 /* OFPACT_SET_VLAN_PCP.
380 * We keep track if vlan was present at action validation time to avoid a
381 * PUSH_VLAN when translating to OpenFlow 1.1+.
383 * We also keep the originating OFPUTIL action code in ofpact.compat.
385 * Used for OFPAT10_SET_VLAN_PCP and OFPAT11_SET_VLAN_PCP. */
386 struct ofpact_vlan_pcp
{
387 struct ofpact ofpact
;
388 uint8_t vlan_pcp
; /* VLAN PCP in low 3 bits, 0 in other bits. */
389 bool push_vlan_if_needed
; /* OF 1.0 semantics if true. */
390 bool flow_has_vlan
; /* VLAN present at action validation time? */
393 /* OFPACT_SET_ETH_SRC, OFPACT_SET_ETH_DST.
395 * Used for OFPAT10_SET_DL_SRC, OFPAT10_SET_DL_DST. */
397 struct ofpact ofpact
;
401 /* OFPACT_SET_IPV4_SRC, OFPACT_SET_IPV4_DST.
403 * Used for OFPAT10_SET_NW_SRC, OFPAT10_SET_NW_DST. */
405 struct ofpact ofpact
;
409 /* OFPACT_SET_IP_DSCP.
411 * Used for OFPAT10_SET_NW_TOS. */
413 struct ofpact ofpact
;
414 uint8_t dscp
; /* DSCP in high 6 bits, rest ignored. */
417 /* OFPACT_SET_IP_ECN.
419 * Used for OFPAT11_SET_NW_ECN. */
421 struct ofpact ofpact
;
422 uint8_t ecn
; /* ECN in low 2 bits, rest ignored. */
425 /* OFPACT_SET_IP_TTL.
427 * Used for OFPAT11_SET_NW_TTL. */
428 struct ofpact_ip_ttl
{
429 struct ofpact ofpact
;
433 /* OFPACT_SET_L4_SRC_PORT, OFPACT_SET_L4_DST_PORT.
435 * Used for OFPAT10_SET_TP_SRC, OFPAT10_SET_TP_DST. */
436 struct ofpact_l4_port
{
437 struct ofpact ofpact
;
438 uint16_t port
; /* TCP, UDP or SCTP port number. */
439 uint8_t flow_ip_proto
; /* IP proto from corresponding match, or 0 */
444 * Used for NXAST_REG_MOVE. */
445 struct ofpact_reg_move
{
446 struct ofpact ofpact
;
447 struct mf_subfield src
;
448 struct mf_subfield dst
;
451 /* OFPACT_STACK_PUSH.
453 * Used for NXAST_STACK_PUSH and NXAST_STACK_POP. */
454 struct ofpact_stack
{
455 struct ofpact ofpact
;
456 struct mf_subfield subfield
;
461 * Used for NXAST_REG_LOAD and OFPAT12_SET_FIELD. */
462 struct ofpact_set_field
{
463 OFPACT_PADDED_MEMBERS(
464 struct ofpact ofpact
;
465 bool flow_has_vlan
; /* VLAN present at action validation time. */
466 const struct mf_field
*field
;
468 union mf_value value
[]; /* Significant value bytes followed by
469 * significant mask bytes. */
471 BUILD_ASSERT_DECL(offsetof(struct ofpact_set_field
, value
)
472 % OFPACT_ALIGNTO
== 0);
473 BUILD_ASSERT_DECL(offsetof(struct ofpact_set_field
, value
)
474 == sizeof(struct ofpact_set_field
));
476 /* Use macro to not have to deal with constness. */
477 #define ofpact_set_field_mask(SF) \
478 ALIGNED_CAST(union mf_value *, \
479 (uint8_t *)(SF)->value + (SF)->field->n_bytes)
481 /* OFPACT_PUSH_VLAN/MPLS/PBB
483 * Used for NXAST_PUSH_MPLS, OFPAT11_PUSH_MPLS. */
484 struct ofpact_push_mpls
{
485 struct ofpact ofpact
;
491 * Used for NXAST_POP_MPLS, OFPAT11_POP_MPLS.. */
492 struct ofpact_pop_mpls
{
493 struct ofpact ofpact
;
497 /* OFPACT_SET_TUNNEL.
499 * Used for NXAST_SET_TUNNEL, NXAST_SET_TUNNEL64. */
500 struct ofpact_tunnel
{
501 struct ofpact ofpact
;
507 * Used for NXAST_SET_QUEUE. */
508 struct ofpact_queue
{
509 struct ofpact ofpact
;
513 /* OFPACT_FIN_TIMEOUT.
515 * Used for NXAST_FIN_TIMEOUT. */
516 struct ofpact_fin_timeout
{
517 struct ofpact ofpact
;
518 uint16_t fin_idle_timeout
;
519 uint16_t fin_hard_timeout
;
522 /* OFPACT_WRITE_METADATA.
524 * Used for NXAST_WRITE_METADATA. */
525 struct ofpact_metadata
{
526 struct ofpact ofpact
;
533 * Used for OFPIT13_METER. */
534 struct ofpact_meter
{
535 struct ofpact ofpact
;
537 uint32_t provider_meter_id
;
540 /* OFPACT_WRITE_ACTIONS, OFPACT_CLONE.
542 * Used for OFPIT11_WRITE_ACTIONS, NXAST_CLONE. */
544 OFPACT_PADDED_MEMBERS(struct ofpact ofpact
;);
545 struct ofpact actions
[];
547 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest
, actions
) % OFPACT_ALIGNTO
== 0);
548 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest
, actions
)
549 == sizeof(struct ofpact_nest
));
552 ofpact_nest_get_action_len(const struct ofpact_nest
*on
)
554 return on
->ofpact
.len
- offsetof(struct ofpact_nest
, actions
);
557 /* Bits for 'flags' in struct nx_action_conntrack.
559 * If NX_CT_F_COMMIT is set, then the connection entry is moved from the
560 * unconfirmed to confirmed list in the tracker.
561 * If NX_CT_F_FORCE is set, in addition to NX_CT_F_COMMIT, then the conntrack
562 * entry is replaced with a new one in case the original direction of the
563 * existing entry is opposite of the current packet direction.
565 enum nx_conntrack_flags
{
566 NX_CT_F_COMMIT
= 1 << 0,
567 NX_CT_F_FORCE
= 1 << 1,
570 /* Magic value for struct nx_action_conntrack 'recirc_table' field, to specify
571 * that the packet should not be recirculated. */
572 #define NX_CT_RECIRC_NONE OFPTT_ALL
574 #if !defined(IPPORT_FTP)
575 #define IPPORT_FTP 21
578 #if !defined(IPPORT_TFTP)
579 #define IPPORT_TFTP 69
584 * Used for NXAST_CT. */
585 struct ofpact_conntrack
{
586 OFPACT_PADDED_MEMBERS(
587 struct ofpact ofpact
;
590 struct mf_subfield zone_src
;
592 uint8_t recirc_table
;
594 struct ofpact actions
[0];
596 BUILD_ASSERT_DECL(offsetof(struct ofpact_conntrack
, actions
)
597 % OFPACT_ALIGNTO
== 0);
598 BUILD_ASSERT_DECL(offsetof(struct ofpact_conntrack
, actions
)
599 == sizeof(struct ofpact_conntrack
));
602 ofpact_ct_get_action_len(const struct ofpact_conntrack
*oc
)
604 return oc
->ofpact
.len
- offsetof(struct ofpact_conntrack
, actions
);
607 void ofpacts_execute_action_set(struct ofpbuf
*action_list
,
608 const struct ofpbuf
*action_set
);
610 /* Bits for 'flags' in struct nx_action_nat.
613 NX_NAT_F_SRC
= 1 << 0, /* Mutually exclusive with NX_NAT_F_DST. */
614 NX_NAT_F_DST
= 1 << 1,
615 NX_NAT_F_PERSISTENT
= 1 << 2,
616 NX_NAT_F_PROTO_HASH
= 1 << 3, /* Mutually exclusive with PROTO_RANDOM. */
617 NX_NAT_F_PROTO_RANDOM
= 1 << 4,
618 NX_NAT_F_MASK
= (NX_NAT_F_SRC
| NX_NAT_F_DST
| NX_NAT_F_PERSISTENT
| NX_NAT_F_PROTO_HASH
| NX_NAT_F_PROTO_RANDOM
)
623 * Used for NXAST_NAT. */
625 struct ofpact ofpact
;
626 uint8_t range_af
; /* AF_UNSPEC, AF_INET, or AF_INET6 */
627 uint16_t flags
; /* NX_NAT_F_* */
649 * Used for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE, NXAST_RESUBMIT_TABLE_CT. */
650 struct ofpact_resubmit
{
651 struct ofpact ofpact
;
654 bool with_ct_orig
; /* Resubmit with Conntrack original direction tuple
655 * fields in place of IP header fields. */
658 /* Bits for 'flags' in struct nx_action_learn.
660 * If NX_LEARN_F_SEND_FLOW_REM is set, then the learned flows will have their
661 * OFPFF_SEND_FLOW_REM flag set.
663 * If NX_LEARN_F_DELETE_LEARNED is set, then removing this action will delete
664 * all the flows from the learn action's 'table_id' that have the learn
665 * action's 'cookie'. Important points:
667 * - The deleted flows include those created by this action, those created
668 * by other learn actions with the same 'table_id' and 'cookie', those
669 * created by flow_mod requests by a controller in the specified table
670 * with the specified cookie, and those created through any other
673 * - If multiple flows specify "learn" actions with
674 * NX_LEARN_F_DELETE_LEARNED with the same 'table_id' and 'cookie', then
675 * no deletion occurs until all of those "learn" actions are deleted.
677 * - Deleting a flow that contains a learn action is the most obvious way
678 * to delete a learn action. Modifying a flow's actions, or replacing it
679 * by a new flow, can also delete a learn action. Finally, replacing a
680 * learn action with NX_LEARN_F_DELETE_LEARNED with a learn action
681 * without that flag also effectively deletes the learn action and can
682 * trigger flow deletion.
684 * NX_LEARN_F_DELETE_LEARNED was added in Open vSwitch 2.4. */
685 enum nx_learn_flags
{
686 NX_LEARN_F_SEND_FLOW_REM
= 1 << 0,
687 NX_LEARN_F_DELETE_LEARNED
= 1 << 1,
690 #define NX_LEARN_N_BITS_MASK 0x3ff
692 #define NX_LEARN_SRC_FIELD (0 << 13) /* Copy from field. */
693 #define NX_LEARN_SRC_IMMEDIATE (1 << 13) /* Copy from immediate value. */
694 #define NX_LEARN_SRC_MASK (1 << 13)
696 #define NX_LEARN_DST_MATCH (0 << 11) /* Add match criterion. */
697 #define NX_LEARN_DST_LOAD (1 << 11) /* Add NXAST_REG_LOAD action. */
698 #define NX_LEARN_DST_OUTPUT (2 << 11) /* Add OFPAT_OUTPUT action. */
699 #define NX_LEARN_DST_RESERVED (3 << 11) /* Not yet defined. */
700 #define NX_LEARN_DST_MASK (3 << 11)
702 /* Part of struct ofpact_learn, below. */
703 struct ofpact_learn_spec
{
704 OFPACT_PADDED_MEMBERS(
705 struct mf_subfield src
; /* NX_LEARN_SRC_FIELD only. */
706 struct mf_subfield dst
; /* NX_LEARN_DST_MATCH,
707 * NX_LEARN_DST_LOAD only. */
708 uint16_t src_type
; /* One of NX_LEARN_SRC_*. */
709 uint16_t dst_type
; /* One of NX_LEARN_DST_*. */
710 uint8_t n_bits
; /* Number of bits in source and dest. */
712 /* Followed by 'DIV_ROUND_UP(n_bits, 8)' bytes of immediate data for
713 * match 'dst_type's NX_LEARN_DST_MATCH and NX_LEARN_DST_LOAD when
714 * NX_LEARN_SRC_IMMEDIATE is set in 'src_type', followed by zeroes to align
715 * to OFPACT_ALIGNTO. */
717 BUILD_ASSERT_DECL(sizeof(struct ofpact_learn_spec
) % OFPACT_ALIGNTO
== 0);
719 static inline const void *
720 ofpact_learn_spec_imm(const struct ofpact_learn_spec
*spec
)
725 static inline const struct ofpact_learn_spec
*
726 ofpact_learn_spec_next(const struct ofpact_learn_spec
*spec
)
728 if (spec
->src_type
== NX_LEARN_SRC_IMMEDIATE
) {
729 unsigned int n_bytes
= OFPACT_ALIGN(DIV_ROUND_UP(spec
->n_bits
, 8));
730 return ALIGNED_CAST(const struct ofpact_learn_spec
*,
731 (const uint8_t *)(spec
+ 1) + n_bytes
);
738 * Used for NXAST_LEARN. */
739 struct ofpact_learn
{
740 OFPACT_PADDED_MEMBERS(
741 struct ofpact ofpact
;
743 uint16_t idle_timeout
; /* Idle time before discarding (seconds). */
744 uint16_t hard_timeout
; /* Max time before discarding (seconds). */
745 uint16_t priority
; /* Priority level of flow entry. */
746 uint8_t table_id
; /* Table to insert flow entry. */
747 enum nx_learn_flags flags
; /* NX_LEARN_F_*. */
748 ovs_be64 cookie
; /* Cookie for new flow. */
749 uint16_t fin_idle_timeout
; /* Idle timeout after FIN, if nonzero. */
750 uint16_t fin_hard_timeout
; /* Hard timeout after FIN, if nonzero. */
753 struct ofpact_learn_spec specs
[];
756 static inline const struct ofpact_learn_spec
*
757 ofpact_learn_spec_end(const struct ofpact_learn
*learn
)
759 return ALIGNED_CAST(const struct ofpact_learn_spec
*,
760 ofpact_next(&learn
->ofpact
));
763 #define OFPACT_LEARN_SPEC_FOR_EACH(SPEC, LEARN) \
764 for ((SPEC) = (LEARN)->specs; \
765 (SPEC) < ofpact_learn_spec_end(LEARN); \
766 (SPEC) = ofpact_learn_spec_next(SPEC))
768 /* Multipath link choice algorithm to apply.
770 * In the descriptions below, 'n_links' is max_link + 1. */
771 enum nx_mp_algorithm
{
772 /* link = hash(flow) % n_links.
774 * Redistributes all traffic when n_links changes. O(1) performance. See
777 * Use UINT16_MAX for max_link to get a raw hash value. */
778 NX_MP_ALG_MODULO_N
= 0,
780 /* link = hash(flow) / (MAX_HASH / n_links).
782 * Redistributes between one-quarter and one-half of traffic when n_links
783 * changes. O(1) performance. See RFC 2992.
785 NX_MP_ALG_HASH_THRESHOLD
= 1,
787 /* Highest Random Weight.
789 * for i in [0,n_links):
790 * weights[i] = hash(flow, i)
791 * link = { i such that weights[i] >= weights[j] for all j != i }
793 * Redistributes 1/n_links of traffic when n_links changes. O(n_links)
794 * performance. If n_links is greater than a threshold (currently 64, but
795 * subject to change), Open vSwitch will substitute another algorithm
796 * automatically. See RFC 2992. */
804 * link = hash(flow, i) % arg
805 * while link > max_link
807 * Redistributes 1/n_links of traffic when n_links changes. O(1)
808 * performance when arg/max_link is bounded by a constant.
810 * Redistributes all traffic when arg changes.
812 * arg must be greater than max_link and for best performance should be no
813 * more than approximately max_link * 2. If arg is outside the acceptable
814 * range, Open vSwitch will automatically substitute the least power of 2
815 * greater than max_link.
817 * This algorithm is specific to Open vSwitch.
819 NX_MP_ALG_ITER_HASH
= 3,
822 /* OFPACT_CONJUNCTION.
824 * Used for NXAST_CONJUNCTION. */
825 struct ofpact_conjunction
{
826 struct ofpact ofpact
;
834 * Used for NXAST_MULTIPATH. */
835 struct ofpact_multipath
{
836 struct ofpact ofpact
;
838 /* What fields to hash and how. */
839 enum nx_hash_fields fields
;
840 uint16_t basis
; /* Universal hash parameter. */
842 /* Multipath link choice algorithm to apply to hash value. */
843 enum nx_mp_algorithm algorithm
;
844 uint16_t max_link
; /* Number of output links, minus 1. */
845 uint32_t arg
; /* Algorithm-specific argument. */
847 /* Where to store the result. */
848 struct mf_subfield dst
;
853 * Used for NXAST_NOTE. */
855 struct ofpact ofpact
;
860 /* Direction of sampled packets. */
861 enum nx_action_sample_direction
{
862 /* OVS will attempt to infer the sample's direction based on whether
863 * 'sampling_port' is the packet's output port. This is generally
864 * effective except when sampling happens as part of an output to a patch
865 * port, which doesn't involve a datapath output action. */
866 NX_ACTION_SAMPLE_DEFAULT
,
868 /* Explicit direction. This is useful for sampling packets coming in from
869 * or going out of a patch port, where the direction cannot be inferred. */
870 NX_ACTION_SAMPLE_INGRESS
,
871 NX_ACTION_SAMPLE_EGRESS
876 * Used for NXAST_SAMPLE, NXAST_SAMPLE2, and NXAST_SAMPLE3. */
877 struct ofpact_sample
{
878 struct ofpact ofpact
;
879 uint16_t probability
; /* Always positive. */
880 uint32_t collector_set_id
;
881 uint32_t obs_domain_id
;
882 uint32_t obs_point_id
;
883 ofp_port_t sampling_port
;
884 enum nx_action_sample_direction direction
;
889 * Used for OFPAT11_DEC_NW_TTL, NXAST_DEC_TTL and NXAST_DEC_TTL_CNT_IDS. */
890 struct ofpact_cnt_ids
{
891 struct ofpact ofpact
;
893 /* Controller ids. */
894 unsigned int n_controllers
;
898 /* OFPACT_SET_MPLS_LABEL.
900 * Used for OFPAT11_SET_MPLS_LABEL and NXAST_SET_MPLS_LABEL */
901 struct ofpact_mpls_label
{
902 struct ofpact ofpact
;
907 /* OFPACT_SET_MPLS_TC.
909 * Used for OFPAT11_SET_MPLS_TC and NXAST_SET_MPLS_TC */
910 struct ofpact_mpls_tc
{
911 struct ofpact ofpact
;
916 /* OFPACT_SET_MPLS_TTL.
918 * Used for OFPAT11_SET_MPLS_TTL and NXAST_SET_MPLS_TTL */
919 struct ofpact_mpls_ttl
{
920 struct ofpact ofpact
;
927 * Used for OFPIT11_GOTO_TABLE */
928 struct ofpact_goto_table
{
929 struct ofpact ofpact
;
935 * Used for OFPAT11_GROUP. */
936 struct ofpact_group
{
937 struct ofpact ofpact
;
941 /* OFPACT_UNROLL_XLATE.
943 * Used only internally. */
944 struct ofpact_unroll_xlate
{
945 struct ofpact ofpact
;
947 /* Metadata in xlate context, visible to controller via PACKET_INs. */
948 uint8_t rule_table_id
; /* 0xFF if none. */
949 ovs_be64 rule_cookie
; /* OVS_BE64_MAX if none. */
952 /* Converting OpenFlow to ofpacts. */
953 enum ofperr
ofpacts_pull_openflow_actions(struct ofpbuf
*openflow
,
954 unsigned int actions_len
,
955 enum ofp_version version
,
956 const struct vl_mff_map
*,
957 struct ofpbuf
*ofpacts
);
959 ofpacts_pull_openflow_instructions(struct ofpbuf
*openflow
,
960 unsigned int instructions_len
,
961 enum ofp_version version
,
962 const struct vl_mff_map
*vl_mff_map
,
963 struct ofpbuf
*ofpacts
);
964 enum ofperr
ofpacts_check(struct ofpact
[], size_t ofpacts_len
,
965 struct match
*, ofp_port_t max_ports
,
966 uint8_t table_id
, uint8_t n_tables
,
967 enum ofputil_protocol
*usable_protocols
);
968 enum ofperr
ofpacts_check_consistency(struct ofpact
[], size_t ofpacts_len
,
969 struct match
*, ofp_port_t max_ports
,
970 uint8_t table_id
, uint8_t n_tables
,
971 enum ofputil_protocol usable_protocols
);
972 enum ofperr
ofpact_check_output_port(ofp_port_t port
, ofp_port_t max_ports
);
974 /* Converting ofpacts to OpenFlow. */
975 size_t ofpacts_put_openflow_actions(const struct ofpact
[], size_t ofpacts_len
,
976 struct ofpbuf
*openflow
, enum ofp_version
);
977 void ofpacts_put_openflow_instructions(const struct ofpact
[],
979 struct ofpbuf
*openflow
,
980 enum ofp_version ofp_version
);
982 /* Sets of supported actions. */
983 ovs_be32
ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap
, enum ofp_version
);
984 uint64_t ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap
, enum ofp_version
);
985 void ofpact_bitmap_format(uint64_t ofpacts_bitmap
, struct ds
*);
987 /* Working with ofpacts. */
988 bool ofpacts_output_to_port(const struct ofpact
[], size_t ofpacts_len
,
990 bool ofpacts_output_to_group(const struct ofpact
[], size_t ofpacts_len
,
992 bool ofpacts_equal(const struct ofpact a
[], size_t a_len
,
993 const struct ofpact b
[], size_t b_len
);
994 const struct mf_field
*ofpact_get_mf_dst(const struct ofpact
*ofpact
);
995 uint32_t ofpacts_get_meter(const struct ofpact
[], size_t ofpacts_len
);
997 /* Formatting and parsing ofpacts. */
998 void ofpacts_format(const struct ofpact
[], size_t ofpacts_len
, struct ds
*);
999 char *ofpacts_parse_actions(const char *, struct ofpbuf
*ofpacts
,
1000 enum ofputil_protocol
*usable_protocols
)
1001 OVS_WARN_UNUSED_RESULT
;
1002 char *ofpacts_parse_instructions(const char *, struct ofpbuf
*ofpacts
,
1003 enum ofputil_protocol
*usable_protocols
)
1004 OVS_WARN_UNUSED_RESULT
;
1005 const char *ofpact_name(enum ofpact_type
);
1007 /* Internal use by the helpers below. */
1008 void ofpact_init(struct ofpact
*, enum ofpact_type
, size_t len
);
1009 void *ofpact_put(struct ofpbuf
*, enum ofpact_type
, size_t len
);
1010 void *ofpact_finish(struct ofpbuf
*, struct ofpact
*);
1012 /* For each OFPACT_<ENUM> with a corresponding struct <STRUCT>, this defines
1013 * the following commonly useful functions:
1015 * struct <STRUCT> *ofpact_put_<ENUM>(struct ofpbuf *ofpacts);
1017 * Appends a new 'ofpact', of length OFPACT_<ENUM>_SIZE, to 'ofpacts',
1018 * initializes it with ofpact_init_<ENUM>(), and returns it. Also sets
1019 * 'ofpacts->header' to the returned action.
1021 * After using this function to add a variable-length action, add the
1022 * elements of the flexible array (e.g. with ofpbuf_put()), then use
1023 * ofpact_finish() to pad the action to a multiple of OFPACT_ALIGNTO bytes
1024 * and update its embedded length field. (Keep in mind the need to refresh
1025 * the structure from 'ofpacts->header' after adding data to 'ofpacts'.)
1027 * struct <STRUCT> *ofpact_get_<ENUM>(const struct ofpact *ofpact);
1029 * Returns 'ofpact' cast to "struct <STRUCT> *". 'ofpact->type' must be
1032 * void ofpact_finish_<ENUM>(struct ofpbuf *ofpacts, struct <STRUCT> **ap);
1034 * Finishes composing variable-length action '*ap' (begun using
1035 * ofpact_put_<NAME>() on 'ofpacts'), by padding the action to a multiple
1036 * of OFPACT_ALIGNTO bytes and updating its embedded length field.
1038 * May reallocate 'ofpacts', and so as a convenience automatically updates
1039 * '*ap' to point to the new location. If the caller has other pointers
1040 * within 'ap' or 'ofpacts', it needs to update them manually.
1042 * as well as the following more rarely useful definitions:
1044 * void ofpact_init_<ENUM>(struct <STRUCT> *ofpact);
1046 * Initializes the parts of 'ofpact' that identify it as having type
1047 * OFPACT_<ENUM> and length OFPACT_<ENUM>_SIZE and zeros the rest.
1051 * The size of the action structure. For a fixed-length action, this is
1052 * sizeof(struct <STRUCT>) rounded up to a multiple of OFPACT_ALIGNTO. For
1053 * a variable-length action, this is the offset to the variable-length
1056 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
1057 BUILD_ASSERT_DECL(offsetof(struct STRUCT, ofpact) == 0); \
1059 enum { OFPACT_##ENUM##_SIZE \
1060 = (offsetof(struct STRUCT, MEMBER) \
1061 ? offsetof(struct STRUCT, MEMBER) \
1062 : OFPACT_ALIGN(sizeof(struct STRUCT))) }; \
1064 static inline struct STRUCT * \
1065 ofpact_get_##ENUM(const struct ofpact *ofpact) \
1067 ovs_assert(ofpact->type == OFPACT_##ENUM); \
1068 return ALIGNED_CAST(struct STRUCT *, ofpact); \
1071 static inline struct STRUCT * \
1072 ofpact_get_##ENUM##_nullable(const struct ofpact *ofpact) \
1074 ovs_assert(!ofpact || ofpact->type == OFPACT_##ENUM); \
1075 return ALIGNED_CAST(struct STRUCT *, ofpact); \
1078 static inline struct STRUCT * \
1079 ofpact_put_##ENUM(struct ofpbuf *ofpacts) \
1081 return ofpact_put(ofpacts, OFPACT_##ENUM, \
1082 OFPACT_##ENUM##_SIZE); \
1085 static inline void \
1086 ofpact_init_##ENUM(struct STRUCT *ofpact) \
1088 ofpact_init(&ofpact->ofpact, OFPACT_##ENUM, \
1089 OFPACT_##ENUM##_SIZE); \
1092 static inline void \
1093 ofpact_finish_##ENUM(struct ofpbuf *ofpbuf, struct STRUCT **ofpactp) \
1095 struct ofpact *ofpact = &(*ofpactp)->ofpact; \
1096 ovs_assert(ofpact->type == OFPACT_##ENUM); \
1097 *ofpactp = ofpact_finish(ofpbuf, ofpact); \
1102 /* Additional functions for composing ofpacts. */
1103 struct ofpact_set_field
*ofpact_put_set_field(struct ofpbuf
*ofpacts
,
1104 const struct mf_field
*,
1107 struct ofpact_set_field
*ofpact_put_reg_load(struct ofpbuf
*ofpacts
,
1108 const struct mf_field
*,
1111 struct ofpact_set_field
*ofpact_put_reg_load2(struct ofpbuf
*ofpacts
,
1112 const struct mf_field
*,
1116 /* OpenFlow 1.1 instructions.
1117 * The order is sorted in execution order. Not in the value of OFPIT11_xxx.
1118 * It is enforced on parser from text string.
1120 #define OVS_INSTRUCTIONS \
1121 DEFINE_INST(OFPIT13_METER, \
1122 ofp13_instruction_meter, false, \
1125 DEFINE_INST(OFPIT11_APPLY_ACTIONS, \
1126 ofp11_instruction_actions, true, \
1129 DEFINE_INST(OFPIT11_CLEAR_ACTIONS, \
1130 ofp11_instruction, false, \
1133 DEFINE_INST(OFPIT11_WRITE_ACTIONS, \
1134 ofp11_instruction_actions, true, \
1137 DEFINE_INST(OFPIT11_WRITE_METADATA, \
1138 ofp11_instruction_write_metadata, false, \
1141 DEFINE_INST(OFPIT11_GOTO_TABLE, \
1142 ofp11_instruction_goto_table, false, \
1145 enum ovs_instruction_type
{
1146 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) OVSINST_##ENUM,
1152 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
1153 N_OVS_INSTRUCTIONS
= OVS_INSTRUCTIONS
1157 const char *ovs_instruction_name_from_type(enum ovs_instruction_type type
);
1158 int ovs_instruction_type_from_name(const char *name
);
1159 enum ovs_instruction_type
ovs_instruction_type_from_ofpact_type(
1161 enum ofperr
ovs_instruction_type_from_inst_type(
1162 enum ovs_instruction_type
*instruction_type
, const uint16_t inst_type
);
1164 ovs_be32
ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap
, enum ofp_version
);
1165 uint32_t ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap
,
1168 #endif /* ofp-actions.h */