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
;
539 /* OFPACT_WRITE_ACTIONS, OFPACT_CLONE.
541 * Used for OFPIT11_WRITE_ACTIONS, NXAST_CLONE. */
543 OFPACT_PADDED_MEMBERS(struct ofpact ofpact
;);
544 struct ofpact actions
[];
546 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest
, actions
) % OFPACT_ALIGNTO
== 0);
547 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest
, actions
)
548 == sizeof(struct ofpact_nest
));
551 ofpact_nest_get_action_len(const struct ofpact_nest
*on
)
553 return on
->ofpact
.len
- offsetof(struct ofpact_nest
, actions
);
556 /* Bits for 'flags' in struct nx_action_conntrack.
558 * If NX_CT_F_COMMIT is set, then the connection entry is moved from the
559 * unconfirmed to confirmed list in the tracker. */
560 enum nx_conntrack_flags
{
561 NX_CT_F_COMMIT
= 1 << 0,
564 /* Magic value for struct nx_action_conntrack 'recirc_table' field, to specify
565 * that the packet should not be recirculated. */
566 #define NX_CT_RECIRC_NONE OFPTT_ALL
568 #if !defined(IPPORT_FTP)
569 #define IPPORT_FTP 21
572 #if !defined(IPPORT_TFTP)
573 #define IPPORT_TFTP 69
578 * Used for NXAST_CT. */
579 struct ofpact_conntrack
{
580 OFPACT_PADDED_MEMBERS(
581 struct ofpact ofpact
;
584 struct mf_subfield zone_src
;
586 uint8_t recirc_table
;
588 struct ofpact actions
[0];
590 BUILD_ASSERT_DECL(offsetof(struct ofpact_conntrack
, actions
)
591 % OFPACT_ALIGNTO
== 0);
592 BUILD_ASSERT_DECL(offsetof(struct ofpact_conntrack
, actions
)
593 == sizeof(struct ofpact_conntrack
));
596 ofpact_ct_get_action_len(const struct ofpact_conntrack
*oc
)
598 return oc
->ofpact
.len
- offsetof(struct ofpact_conntrack
, actions
);
601 void ofpacts_execute_action_set(struct ofpbuf
*action_list
,
602 const struct ofpbuf
*action_set
);
604 /* Bits for 'flags' in struct nx_action_nat.
607 NX_NAT_F_SRC
= 1 << 0, /* Mutually exclusive with NX_NAT_F_DST. */
608 NX_NAT_F_DST
= 1 << 1,
609 NX_NAT_F_PERSISTENT
= 1 << 2,
610 NX_NAT_F_PROTO_HASH
= 1 << 3, /* Mutually exclusive with PROTO_RANDOM. */
611 NX_NAT_F_PROTO_RANDOM
= 1 << 4,
612 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
)
617 * Used for NXAST_NAT. */
619 struct ofpact ofpact
;
620 uint8_t range_af
; /* AF_UNSPEC, AF_INET, or AF_INET6 */
621 uint16_t flags
; /* NX_NAT_F_* */
643 * Used for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE. */
644 struct ofpact_resubmit
{
645 struct ofpact ofpact
;
650 /* Bits for 'flags' in struct nx_action_learn.
652 * If NX_LEARN_F_SEND_FLOW_REM is set, then the learned flows will have their
653 * OFPFF_SEND_FLOW_REM flag set.
655 * If NX_LEARN_F_DELETE_LEARNED is set, then removing this action will delete
656 * all the flows from the learn action's 'table_id' that have the learn
657 * action's 'cookie'. Important points:
659 * - The deleted flows include those created by this action, those created
660 * by other learn actions with the same 'table_id' and 'cookie', those
661 * created by flow_mod requests by a controller in the specified table
662 * with the specified cookie, and those created through any other
665 * - If multiple flows specify "learn" actions with
666 * NX_LEARN_F_DELETE_LEARNED with the same 'table_id' and 'cookie', then
667 * no deletion occurs until all of those "learn" actions are deleted.
669 * - Deleting a flow that contains a learn action is the most obvious way
670 * to delete a learn action. Modifying a flow's actions, or replacing it
671 * by a new flow, can also delete a learn action. Finally, replacing a
672 * learn action with NX_LEARN_F_DELETE_LEARNED with a learn action
673 * without that flag also effectively deletes the learn action and can
674 * trigger flow deletion.
676 * NX_LEARN_F_DELETE_LEARNED was added in Open vSwitch 2.4. */
677 enum nx_learn_flags
{
678 NX_LEARN_F_SEND_FLOW_REM
= 1 << 0,
679 NX_LEARN_F_DELETE_LEARNED
= 1 << 1,
682 #define NX_LEARN_N_BITS_MASK 0x3ff
684 #define NX_LEARN_SRC_FIELD (0 << 13) /* Copy from field. */
685 #define NX_LEARN_SRC_IMMEDIATE (1 << 13) /* Copy from immediate value. */
686 #define NX_LEARN_SRC_MASK (1 << 13)
688 #define NX_LEARN_DST_MATCH (0 << 11) /* Add match criterion. */
689 #define NX_LEARN_DST_LOAD (1 << 11) /* Add NXAST_REG_LOAD action. */
690 #define NX_LEARN_DST_OUTPUT (2 << 11) /* Add OFPAT_OUTPUT action. */
691 #define NX_LEARN_DST_RESERVED (3 << 11) /* Not yet defined. */
692 #define NX_LEARN_DST_MASK (3 << 11)
694 /* Part of struct ofpact_learn, below. */
695 struct ofpact_learn_spec
{
696 OFPACT_PADDED_MEMBERS(
697 struct mf_subfield src
; /* NX_LEARN_SRC_FIELD only. */
698 struct mf_subfield dst
; /* NX_LEARN_DST_MATCH,
699 * NX_LEARN_DST_LOAD only. */
700 uint16_t src_type
; /* One of NX_LEARN_SRC_*. */
701 uint16_t dst_type
; /* One of NX_LEARN_DST_*. */
702 uint8_t n_bits
; /* Number of bits in source and dest. */
704 /* Followed by 'DIV_ROUND_UP(n_bits, 8)' bytes of immediate data for
705 * match 'dst_type's NX_LEARN_DST_MATCH and NX_LEARN_DST_LOAD when
706 * NX_LEARN_SRC_IMMEDIATE is set in 'src_type', followed by zeroes to align
707 * to OFPACT_ALIGNTO. */
709 BUILD_ASSERT_DECL(sizeof(struct ofpact_learn_spec
) % OFPACT_ALIGNTO
== 0);
711 static inline const void *
712 ofpact_learn_spec_imm(const struct ofpact_learn_spec
*spec
)
717 static inline const struct ofpact_learn_spec
*
718 ofpact_learn_spec_next(const struct ofpact_learn_spec
*spec
)
720 if (spec
->src_type
== NX_LEARN_SRC_IMMEDIATE
) {
721 unsigned int n_bytes
= OFPACT_ALIGN(DIV_ROUND_UP(spec
->n_bits
, 8));
722 return ALIGNED_CAST(const struct ofpact_learn_spec
*,
723 (const uint8_t *)(spec
+ 1) + n_bytes
);
730 * Used for NXAST_LEARN. */
731 struct ofpact_learn
{
732 OFPACT_PADDED_MEMBERS(
733 struct ofpact ofpact
;
735 uint16_t idle_timeout
; /* Idle time before discarding (seconds). */
736 uint16_t hard_timeout
; /* Max time before discarding (seconds). */
737 uint16_t priority
; /* Priority level of flow entry. */
738 uint8_t table_id
; /* Table to insert flow entry. */
739 enum nx_learn_flags flags
; /* NX_LEARN_F_*. */
740 ovs_be64 cookie
; /* Cookie for new flow. */
741 uint16_t fin_idle_timeout
; /* Idle timeout after FIN, if nonzero. */
742 uint16_t fin_hard_timeout
; /* Hard timeout after FIN, if nonzero. */
745 struct ofpact_learn_spec specs
[];
748 static inline const struct ofpact_learn_spec
*
749 ofpact_learn_spec_end(const struct ofpact_learn
*learn
)
751 return ALIGNED_CAST(const struct ofpact_learn_spec
*,
752 ofpact_next(&learn
->ofpact
));
755 #define OFPACT_LEARN_SPEC_FOR_EACH(SPEC, LEARN) \
756 for ((SPEC) = (LEARN)->specs; \
757 (SPEC) < ofpact_learn_spec_end(LEARN); \
758 (SPEC) = ofpact_learn_spec_next(SPEC))
760 /* Multipath link choice algorithm to apply.
762 * In the descriptions below, 'n_links' is max_link + 1. */
763 enum nx_mp_algorithm
{
764 /* link = hash(flow) % n_links.
766 * Redistributes all traffic when n_links changes. O(1) performance. See
769 * Use UINT16_MAX for max_link to get a raw hash value. */
770 NX_MP_ALG_MODULO_N
= 0,
772 /* link = hash(flow) / (MAX_HASH / n_links).
774 * Redistributes between one-quarter and one-half of traffic when n_links
775 * changes. O(1) performance. See RFC 2992.
777 NX_MP_ALG_HASH_THRESHOLD
= 1,
779 /* Highest Random Weight.
781 * for i in [0,n_links):
782 * weights[i] = hash(flow, i)
783 * link = { i such that weights[i] >= weights[j] for all j != i }
785 * Redistributes 1/n_links of traffic when n_links changes. O(n_links)
786 * performance. If n_links is greater than a threshold (currently 64, but
787 * subject to change), Open vSwitch will substitute another algorithm
788 * automatically. See RFC 2992. */
796 * link = hash(flow, i) % arg
797 * while link > max_link
799 * Redistributes 1/n_links of traffic when n_links changes. O(1)
800 * performance when arg/max_link is bounded by a constant.
802 * Redistributes all traffic when arg changes.
804 * arg must be greater than max_link and for best performance should be no
805 * more than approximately max_link * 2. If arg is outside the acceptable
806 * range, Open vSwitch will automatically substitute the least power of 2
807 * greater than max_link.
809 * This algorithm is specific to Open vSwitch.
811 NX_MP_ALG_ITER_HASH
= 3,
814 /* OFPACT_CONJUNCTION.
816 * Used for NXAST_CONJUNCTION. */
817 struct ofpact_conjunction
{
818 struct ofpact ofpact
;
826 * Used for NXAST_MULTIPATH. */
827 struct ofpact_multipath
{
828 struct ofpact ofpact
;
830 /* What fields to hash and how. */
831 enum nx_hash_fields fields
;
832 uint16_t basis
; /* Universal hash parameter. */
834 /* Multipath link choice algorithm to apply to hash value. */
835 enum nx_mp_algorithm algorithm
;
836 uint16_t max_link
; /* Number of output links, minus 1. */
837 uint32_t arg
; /* Algorithm-specific argument. */
839 /* Where to store the result. */
840 struct mf_subfield dst
;
845 * Used for NXAST_NOTE. */
847 struct ofpact ofpact
;
852 /* Direction of sampled packets. */
853 enum nx_action_sample_direction
{
854 /* OVS will attempt to infer the sample's direction based on whether
855 * 'sampling_port' is the packet's output port. This is generally
856 * effective except when sampling happens as part of an output to a patch
857 * port, which doesn't involve a datapath output action. */
858 NX_ACTION_SAMPLE_DEFAULT
,
860 /* Explicit direction. This is useful for sampling packets coming in from
861 * or going out of a patch port, where the direction cannot be inferred. */
862 NX_ACTION_SAMPLE_INGRESS
,
863 NX_ACTION_SAMPLE_EGRESS
868 * Used for NXAST_SAMPLE, NXAST_SAMPLE2, and NXAST_SAMPLE3. */
869 struct ofpact_sample
{
870 struct ofpact ofpact
;
871 uint16_t probability
; /* Always positive. */
872 uint32_t collector_set_id
;
873 uint32_t obs_domain_id
;
874 uint32_t obs_point_id
;
875 ofp_port_t sampling_port
;
876 enum nx_action_sample_direction direction
;
881 * Used for OFPAT11_DEC_NW_TTL, NXAST_DEC_TTL and NXAST_DEC_TTL_CNT_IDS. */
882 struct ofpact_cnt_ids
{
883 struct ofpact ofpact
;
885 /* Controller ids. */
886 unsigned int n_controllers
;
890 /* OFPACT_SET_MPLS_LABEL.
892 * Used for OFPAT11_SET_MPLS_LABEL and NXAST_SET_MPLS_LABEL */
893 struct ofpact_mpls_label
{
894 struct ofpact ofpact
;
899 /* OFPACT_SET_MPLS_TC.
901 * Used for OFPAT11_SET_MPLS_TC and NXAST_SET_MPLS_TC */
902 struct ofpact_mpls_tc
{
903 struct ofpact ofpact
;
908 /* OFPACT_SET_MPLS_TTL.
910 * Used for OFPAT11_SET_MPLS_TTL and NXAST_SET_MPLS_TTL */
911 struct ofpact_mpls_ttl
{
912 struct ofpact ofpact
;
919 * Used for OFPIT11_GOTO_TABLE */
920 struct ofpact_goto_table
{
921 struct ofpact ofpact
;
927 * Used for OFPAT11_GROUP. */
928 struct ofpact_group
{
929 struct ofpact ofpact
;
933 /* OFPACT_UNROLL_XLATE.
935 * Used only internally. */
936 struct ofpact_unroll_xlate
{
937 struct ofpact ofpact
;
939 /* Metadata in xlate context, visible to controller via PACKET_INs. */
940 uint8_t rule_table_id
; /* 0xFF if none. */
941 ovs_be64 rule_cookie
; /* OVS_BE64_MAX if none. */
944 /* Converting OpenFlow to ofpacts. */
945 enum ofperr
ofpacts_pull_openflow_actions(struct ofpbuf
*openflow
,
946 unsigned int actions_len
,
947 enum ofp_version version
,
948 const struct vl_mff_map
*,
949 struct ofpbuf
*ofpacts
);
951 ofpacts_pull_openflow_instructions(struct ofpbuf
*openflow
,
952 unsigned int instructions_len
,
953 enum ofp_version version
,
954 const struct vl_mff_map
*vl_mff_map
,
955 struct ofpbuf
*ofpacts
);
956 enum ofperr
ofpacts_check(struct ofpact
[], size_t ofpacts_len
,
957 struct flow
*, ofp_port_t max_ports
,
958 uint8_t table_id
, uint8_t n_tables
,
959 enum ofputil_protocol
*usable_protocols
);
960 enum ofperr
ofpacts_check_consistency(struct ofpact
[], size_t ofpacts_len
,
961 struct flow
*, ofp_port_t max_ports
,
962 uint8_t table_id
, uint8_t n_tables
,
963 enum ofputil_protocol usable_protocols
);
964 enum ofperr
ofpact_check_output_port(ofp_port_t port
, ofp_port_t max_ports
);
966 /* Converting ofpacts to OpenFlow. */
967 size_t ofpacts_put_openflow_actions(const struct ofpact
[], size_t ofpacts_len
,
968 struct ofpbuf
*openflow
, enum ofp_version
);
969 void ofpacts_put_openflow_instructions(const struct ofpact
[],
971 struct ofpbuf
*openflow
,
972 enum ofp_version ofp_version
);
974 /* Sets of supported actions. */
975 ovs_be32
ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap
, enum ofp_version
);
976 uint64_t ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap
, enum ofp_version
);
977 void ofpact_bitmap_format(uint64_t ofpacts_bitmap
, struct ds
*);
979 /* Working with ofpacts. */
980 bool ofpacts_output_to_port(const struct ofpact
[], size_t ofpacts_len
,
982 bool ofpacts_output_to_group(const struct ofpact
[], size_t ofpacts_len
,
984 bool ofpacts_equal(const struct ofpact a
[], size_t a_len
,
985 const struct ofpact b
[], size_t b_len
);
986 const struct mf_field
*ofpact_get_mf_dst(const struct ofpact
*ofpact
);
987 uint32_t ofpacts_get_meter(const struct ofpact
[], size_t ofpacts_len
);
989 /* Formatting and parsing ofpacts. */
990 void ofpacts_format(const struct ofpact
[], size_t ofpacts_len
, struct ds
*);
991 char *ofpacts_parse_actions(const char *, struct ofpbuf
*ofpacts
,
992 enum ofputil_protocol
*usable_protocols
)
993 OVS_WARN_UNUSED_RESULT
;
994 char *ofpacts_parse_instructions(const char *, struct ofpbuf
*ofpacts
,
995 enum ofputil_protocol
*usable_protocols
)
996 OVS_WARN_UNUSED_RESULT
;
997 const char *ofpact_name(enum ofpact_type
);
999 /* Internal use by the helpers below. */
1000 void ofpact_init(struct ofpact
*, enum ofpact_type
, size_t len
);
1001 void *ofpact_put(struct ofpbuf
*, enum ofpact_type
, size_t len
);
1002 void *ofpact_finish(struct ofpbuf
*, struct ofpact
*);
1004 /* For each OFPACT_<ENUM> with a corresponding struct <STRUCT>, this defines
1005 * the following commonly useful functions:
1007 * struct <STRUCT> *ofpact_put_<ENUM>(struct ofpbuf *ofpacts);
1009 * Appends a new 'ofpact', of length OFPACT_<ENUM>_SIZE, to 'ofpacts',
1010 * initializes it with ofpact_init_<ENUM>(), and returns it. Also sets
1011 * 'ofpacts->header' to the returned action.
1013 * After using this function to add a variable-length action, add the
1014 * elements of the flexible array (e.g. with ofpbuf_put()), then use
1015 * ofpact_finish() to pad the action to a multiple of OFPACT_ALIGNTO bytes
1016 * and update its embedded length field. (Keep in mind the need to refresh
1017 * the structure from 'ofpacts->header' after adding data to 'ofpacts'.)
1019 * struct <STRUCT> *ofpact_get_<ENUM>(const struct ofpact *ofpact);
1021 * Returns 'ofpact' cast to "struct <STRUCT> *". 'ofpact->type' must be
1024 * void ofpact_finish_<ENUM>(struct ofpbuf *ofpacts, struct <STRUCT> **ap);
1026 * Finishes composing variable-length action '*ap' (begun using
1027 * ofpact_put_<NAME>() on 'ofpacts'), by padding the action to a multiple
1028 * of OFPACT_ALIGNTO bytes and updating its embedded length field.
1030 * May reallocate 'ofpacts', and so as a convenience automatically updates
1031 * '*ap' to point to the new location. If the caller has other pointers
1032 * within 'ap' or 'ofpacts', it needs to update them manually.
1034 * as well as the following more rarely useful definitions:
1036 * void ofpact_init_<ENUM>(struct <STRUCT> *ofpact);
1038 * Initializes the parts of 'ofpact' that identify it as having type
1039 * OFPACT_<ENUM> and length OFPACT_<ENUM>_SIZE and zeros the rest.
1043 * The size of the action structure. For a fixed-length action, this is
1044 * sizeof(struct <STRUCT>) rounded up to a multiple of OFPACT_ALIGNTO. For
1045 * a variable-length action, this is the offset to the variable-length
1048 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
1049 BUILD_ASSERT_DECL(offsetof(struct STRUCT, ofpact) == 0); \
1051 enum { OFPACT_##ENUM##_SIZE \
1052 = (offsetof(struct STRUCT, MEMBER) \
1053 ? offsetof(struct STRUCT, MEMBER) \
1054 : OFPACT_ALIGN(sizeof(struct STRUCT))) }; \
1056 static inline struct STRUCT * \
1057 ofpact_get_##ENUM(const struct ofpact *ofpact) \
1059 ovs_assert(ofpact->type == OFPACT_##ENUM); \
1060 return ALIGNED_CAST(struct STRUCT *, ofpact); \
1063 static inline struct STRUCT * \
1064 ofpact_get_##ENUM##_nullable(const struct ofpact *ofpact) \
1066 ovs_assert(!ofpact || ofpact->type == OFPACT_##ENUM); \
1067 return ALIGNED_CAST(struct STRUCT *, ofpact); \
1070 static inline struct STRUCT * \
1071 ofpact_put_##ENUM(struct ofpbuf *ofpacts) \
1073 return ofpact_put(ofpacts, OFPACT_##ENUM, \
1074 OFPACT_##ENUM##_SIZE); \
1077 static inline void \
1078 ofpact_init_##ENUM(struct STRUCT *ofpact) \
1080 ofpact_init(&ofpact->ofpact, OFPACT_##ENUM, \
1081 OFPACT_##ENUM##_SIZE); \
1084 static inline void \
1085 ofpact_finish_##ENUM(struct ofpbuf *ofpbuf, struct STRUCT **ofpactp) \
1087 struct ofpact *ofpact = &(*ofpactp)->ofpact; \
1088 ovs_assert(ofpact->type == OFPACT_##ENUM); \
1089 *ofpactp = ofpact_finish(ofpbuf, ofpact); \
1094 /* Additional functions for composing ofpacts. */
1095 struct ofpact_set_field
*ofpact_put_set_field(struct ofpbuf
*ofpacts
,
1096 const struct mf_field
*,
1099 struct ofpact_set_field
*ofpact_put_reg_load(struct ofpbuf
*ofpacts
,
1100 const struct mf_field
*,
1103 struct ofpact_set_field
*ofpact_put_reg_load2(struct ofpbuf
*ofpacts
,
1104 const struct mf_field
*,
1108 /* OpenFlow 1.1 instructions.
1109 * The order is sorted in execution order. Not in the value of OFPIT11_xxx.
1110 * It is enforced on parser from text string.
1112 #define OVS_INSTRUCTIONS \
1113 DEFINE_INST(OFPIT13_METER, \
1114 ofp13_instruction_meter, false, \
1117 DEFINE_INST(OFPIT11_APPLY_ACTIONS, \
1118 ofp11_instruction_actions, true, \
1121 DEFINE_INST(OFPIT11_CLEAR_ACTIONS, \
1122 ofp11_instruction, false, \
1125 DEFINE_INST(OFPIT11_WRITE_ACTIONS, \
1126 ofp11_instruction_actions, true, \
1129 DEFINE_INST(OFPIT11_WRITE_METADATA, \
1130 ofp11_instruction_write_metadata, false, \
1133 DEFINE_INST(OFPIT11_GOTO_TABLE, \
1134 ofp11_instruction_goto_table, false, \
1137 enum ovs_instruction_type
{
1138 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) OVSINST_##ENUM,
1144 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
1145 N_OVS_INSTRUCTIONS
= OVS_INSTRUCTIONS
1149 const char *ovs_instruction_name_from_type(enum ovs_instruction_type type
);
1150 int ovs_instruction_type_from_name(const char *name
);
1151 enum ovs_instruction_type
ovs_instruction_type_from_ofpact_type(
1153 enum ofperr
ovs_instruction_type_from_inst_type(
1154 enum ovs_instruction_type
*instruction_type
, const uint16_t inst_type
);
1156 ovs_be32
ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap
, enum ofp_version
);
1157 uint32_t ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap
,
1160 #endif /* ofp-actions.h */