netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / ofp-actions.h
1 /*
2  * Copyright (c) 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef OFP_ACTIONS_H
18 #define OFP_ACTIONS_H 1
19
20 #include <stddef.h>
21 #include <stdint.h>
22 #include "meta-flow.h"
23 #include "ofp-errors.h"
24 #include "ofp-util.h"
25 #include "openflow/openflow.h"
26 #include "openflow/nicira-ext.h"
27 #include "openvswitch/types.h"
28
29 /* List of OVS abstracted actions.
30  *
31  * This macro is used directly only internally by this header, but the list is
32  * still of interest to developers.
33  *
34  * Each OFPACT invocation has the following parameters:
35  *
36  * 1. <ENUM>, used below in the enum definition of OFPACT_<ENUM>, and
37  *    elsewhere.
38  *
39  * 2. <STRUCT> corresponding to a structure "struct <STRUCT>", that must be
40  *    defined below.  This structure must be an abstract definition of the
41  *    action.  Its first member must have type "struct ofpact" and name
42  *    "ofpact".  It may be fixed length or end with a flexible array member
43  *    (e.g. "int member[];").
44  *
45  * 3. <MEMBER>, which has one of two possible values:
46  *
47  *        - If "struct <STRUCT>" is fixed-length, it must be "ofpact".
48  *
49  *        - If "struct <STRUCT>" is variable-length, it must be the name of the
50  *          flexible array member.
51  *
52  * 4. <NAME>, a quoted string that gives the name of the action, for use in
53  *    parsing actions from text.
54  */
55 #define OFPACTS                                                         \
56     /* Output. */                                                       \
57     OFPACT(OUTPUT,          ofpact_output,      ofpact, "output")       \
58     OFPACT(GROUP,           ofpact_group,       ofpact, "group")        \
59     OFPACT(CONTROLLER,      ofpact_controller,  userdata, "controller") \
60     OFPACT(ENQUEUE,         ofpact_enqueue,     ofpact, "enqueue")      \
61     OFPACT(OUTPUT_REG,      ofpact_output_reg,  ofpact, "output_reg")   \
62     OFPACT(BUNDLE,          ofpact_bundle,      slaves, "bundle")       \
63                                                                         \
64     /* Header changes. */                                               \
65     OFPACT(SET_FIELD,       ofpact_set_field,   ofpact, "set_field")    \
66     OFPACT(SET_VLAN_VID,    ofpact_vlan_vid,    ofpact, "set_vlan_vid") \
67     OFPACT(SET_VLAN_PCP,    ofpact_vlan_pcp,    ofpact, "set_vlan_pcp") \
68     OFPACT(STRIP_VLAN,      ofpact_null,        ofpact, "strip_vlan")   \
69     OFPACT(PUSH_VLAN,       ofpact_null,        ofpact, "push_vlan")    \
70     OFPACT(SET_ETH_SRC,     ofpact_mac,         ofpact, "mod_dl_src")   \
71     OFPACT(SET_ETH_DST,     ofpact_mac,         ofpact, "mod_dl_dst")   \
72     OFPACT(SET_IPV4_SRC,    ofpact_ipv4,        ofpact, "mod_nw_src")   \
73     OFPACT(SET_IPV4_DST,    ofpact_ipv4,        ofpact, "mod_nw_dst")   \
74     OFPACT(SET_IP_DSCP,     ofpact_dscp,        ofpact, "mod_nw_tos")   \
75     OFPACT(SET_IP_ECN,      ofpact_ecn,         ofpact, "mod_nw_ecn")   \
76     OFPACT(SET_IP_TTL,      ofpact_ip_ttl,      ofpact, "mod_nw_ttl")   \
77     OFPACT(SET_L4_SRC_PORT, ofpact_l4_port,     ofpact, "mod_tp_src")   \
78     OFPACT(SET_L4_DST_PORT, ofpact_l4_port,     ofpact, "mod_tp_dst")   \
79     OFPACT(REG_MOVE,        ofpact_reg_move,    ofpact, "move")         \
80     OFPACT(STACK_PUSH,      ofpact_stack,       ofpact, "push")         \
81     OFPACT(STACK_POP,       ofpact_stack,       ofpact, "pop")          \
82     OFPACT(DEC_TTL,         ofpact_cnt_ids,     cnt_ids, "dec_ttl")     \
83     OFPACT(SET_MPLS_LABEL,  ofpact_mpls_label,  ofpact, "set_mpls_label") \
84     OFPACT(SET_MPLS_TC,     ofpact_mpls_tc,     ofpact, "set_mpls_tc")  \
85     OFPACT(SET_MPLS_TTL,    ofpact_mpls_ttl,    ofpact, "set_mpls_ttl") \
86     OFPACT(DEC_MPLS_TTL,    ofpact_null,        ofpact, "dec_mpls_ttl") \
87     OFPACT(PUSH_MPLS,       ofpact_push_mpls,   ofpact, "push_mpls")    \
88     OFPACT(POP_MPLS,        ofpact_pop_mpls,    ofpact, "pop_mpls")     \
89                                                                         \
90     /* Metadata. */                                                     \
91     OFPACT(SET_TUNNEL,      ofpact_tunnel,      ofpact, "set_tunnel")   \
92     OFPACT(SET_QUEUE,       ofpact_queue,       ofpact, "set_queue")    \
93     OFPACT(POP_QUEUE,       ofpact_null,        ofpact, "pop_queue")    \
94     OFPACT(FIN_TIMEOUT,     ofpact_fin_timeout, ofpact, "fin_timeout")  \
95                                                                         \
96     /* Flow table interaction. */                                       \
97     OFPACT(RESUBMIT,        ofpact_resubmit,    ofpact, "resubmit")     \
98     OFPACT(LEARN,           ofpact_learn,       specs, "learn")         \
99     OFPACT(CONJUNCTION,     ofpact_conjunction, ofpact, "conjunction")  \
100                                                                         \
101     /* Arithmetic. */                                                   \
102     OFPACT(MULTIPATH,       ofpact_multipath,   ofpact, "multipath")    \
103                                                                         \
104     /* Other. */                                                        \
105     OFPACT(NOTE,            ofpact_note,        data, "note")           \
106     OFPACT(EXIT,            ofpact_null,        ofpact, "exit")         \
107     OFPACT(SAMPLE,          ofpact_sample,      ofpact, "sample")       \
108     OFPACT(UNROLL_XLATE,    ofpact_unroll_xlate, ofpact, "unroll_xlate") \
109     OFPACT(CT,              ofpact_conntrack,   ofpact, "ct")           \
110     OFPACT(NAT,             ofpact_nat,         ofpact, "nat")          \
111                                                                         \
112     /* Debugging actions.                                               \
113      *                                                                  \
114      * These are intentionally undocumented, subject to change, and     \
115      * only accepted if ovs-vswitchd is started with --enable-dummy. */ \
116     OFPACT(DEBUG_RECIRC, ofpact_null,           ofpact, "debug_recirc") \
117                                                                         \
118     /* Instructions. */                                                 \
119     OFPACT(METER,           ofpact_meter,       ofpact, "meter")        \
120     OFPACT(CLEAR_ACTIONS,   ofpact_null,        ofpact, "clear_actions") \
121     OFPACT(WRITE_ACTIONS,   ofpact_nest,        ofpact, "write_actions") \
122     OFPACT(WRITE_METADATA,  ofpact_metadata,    ofpact, "write_metadata") \
123     OFPACT(GOTO_TABLE,      ofpact_goto_table,  ofpact, "goto_table")
124
125 /* enum ofpact_type, with a member OFPACT_<ENUM> for each action. */
126 enum OVS_PACKED_ENUM ofpact_type {
127 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) OFPACT_##ENUM,
128     OFPACTS
129 #undef OFPACT
130 };
131
132 /* Define N_OFPACTS to the number of types of ofpacts. */
133 enum {
134 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) + 1
135     N_OFPACTS = OFPACTS
136 #undef OFPACT
137 };
138
139 /* Header for an action.
140  *
141  * Each action is a structure "struct ofpact_*" that begins with "struct
142  * ofpact", usually followed by other data that describes the action.  Actions
143  * are padded out to a multiple of OFPACT_ALIGNTO bytes in length.
144  *
145  * The 'raw' member is special:
146  *
147  *     - Most "struct ofpact"s correspond to one particular kind of OpenFlow
148  *       action, at least in a given OpenFlow version.  For example,
149  *       OFPACT_SET_VLAN_VID corresponds to OFPAT10_SET_VLAN_VID in OpenFlow
150  *       1.0.
151  *
152  *       For such actions, the 'raw' member is not meaningful and generally
153  *       should be zero.
154  *
155  *     - A few "struct ofpact"s correspond to multiple OpenFlow actions.  For
156  *       example, OFPACT_SET_TUNNEL can be NXAST_SET_TUNNEL or
157  *       NXAST_SET_TUNNEL64.  In these cases, if the "struct ofpact" originated
158  *       from OpenFlow, then we want to make sure that, if it gets translated
159  *       back to OpenFlow later, it is translated back to the same action type.
160  *       (Otherwise, we'd violate the promise made in DESIGN, in the "Action
161  *       Reproduction" section.)
162  *
163  *       For such actions, the 'raw' member should be the "enum ofp_raw_action"
164  *       originally extracted from the OpenFlow action.  (If the action didn't
165  *       originate from OpenFlow, then setting 'raw' to zero should be fine:
166  *       code to translate the ofpact to OpenFlow must tolerate this case.)
167  */
168 struct ofpact {
169     /* We want the space advantage of an 8-bit type here on every
170      * implementation, without giving up the advantage of having a useful type
171      * on implementations that support packed enums. */
172 #ifdef HAVE_PACKED_ENUM
173     enum ofpact_type type;      /* OFPACT_*. */
174 #else
175     uint8_t type;               /* OFPACT_* */
176 #endif
177
178     uint8_t raw;                /* Original type when added, if any. */
179     uint16_t len;               /* Length of the action, in bytes, including
180                                  * struct ofpact, excluding padding. */
181 };
182 BUILD_ASSERT_DECL(sizeof(struct ofpact) == 4);
183
184 /* Alignment. */
185 #define OFPACT_ALIGNTO 8
186 #define OFPACT_ALIGN(SIZE) ROUND_UP(SIZE, OFPACT_ALIGNTO)
187
188 /* Expands to an anonymous union that contains:
189  *
190  *    - MEMBERS in a nested anonymous struct.
191  *
192  *    - An array as large as MEMBERS plus padding to a multiple of 8 bytes.
193  *
194  * The effect is to pad MEMBERS to a multiple of 8 bytes. */
195 #define OFPACT_PADDED_MEMBERS(MEMBERS)                          \
196     union {                                                     \
197         struct { MEMBERS };                                     \
198         uint8_t pad[OFPACT_ALIGN(sizeof(struct { MEMBERS }))];  \
199     }
200
201 /* Returns the ofpact following 'ofpact'. */
202 static inline struct ofpact *
203 ofpact_next(const struct ofpact *ofpact)
204 {
205     return (void *) ((uint8_t *) ofpact + OFPACT_ALIGN(ofpact->len));
206 }
207
208 struct ofpact *ofpact_next_flattened(const struct ofpact *);
209
210 static inline struct ofpact *
211 ofpact_end(const struct ofpact *ofpacts, size_t ofpacts_len)
212 {
213     return (void *) ((uint8_t *) ofpacts + ofpacts_len);
214 }
215
216 /* Assigns POS to each ofpact, in turn, in the OFPACTS_LEN bytes of ofpacts
217  * starting at OFPACTS. */
218 #define OFPACT_FOR_EACH(POS, OFPACTS, OFPACTS_LEN)                      \
219     for ((POS) = (OFPACTS); (POS) < ofpact_end(OFPACTS, OFPACTS_LEN);  \
220          (POS) = ofpact_next(POS))
221
222 /* Assigns POS to each ofpact, in turn, in the OFPACTS_LEN bytes of ofpacts
223  * starting at OFPACTS.
224  *
225  * For ofpacts that contain nested ofpacts, this visits each of the inner
226  * ofpacts as well. */
227 #define OFPACT_FOR_EACH_FLATTENED(POS, OFPACTS, OFPACTS_LEN)           \
228     for ((POS) = (OFPACTS); (POS) < ofpact_end(OFPACTS, OFPACTS_LEN);  \
229          (POS) = ofpact_next_flattened(POS))
230 \f
231 /* Action structure for each OFPACT_*. */
232
233 /* OFPACT_STRIP_VLAN, OFPACT_POP_QUEUE, OFPACT_EXIT, OFPACT_CLEAR_ACTIONS.
234  *
235  * Used for OFPAT10_STRIP_VLAN, NXAST_POP_QUEUE, NXAST_EXIT,
236  * OFPAT11_POP_VLAN, OFPIT11_CLEAR_ACTIONS.
237  *
238  * Action structure for actions that do not have any extra data beyond the
239  * action type. */
240 struct ofpact_null {
241     struct ofpact ofpact;
242 };
243
244 /* OFPACT_OUTPUT.
245  *
246  * Used for OFPAT10_OUTPUT. */
247 struct ofpact_output {
248     struct ofpact ofpact;
249     ofp_port_t port;            /* Output port. */
250     uint16_t max_len;           /* Max send len, for port OFPP_CONTROLLER. */
251 };
252
253 /* OFPACT_CONTROLLER.
254  *
255  * Used for NXAST_CONTROLLER. */
256 struct ofpact_controller {
257     struct ofpact ofpact;
258     uint16_t max_len;           /* Maximum length to send to controller. */
259     uint16_t controller_id;     /* Controller ID to send packet-in. */
260     enum ofp_packet_in_reason reason; /* Reason to put in packet-in. */
261
262     /* If true, this action freezes packet traversal of the OpenFlow tables and
263      * adds a continuation to the packet-in message, that a controller can use
264      * to resume that traversal. */
265     bool pause;
266
267     /* Arbitrary data to include in the packet-in message (currently, only in
268      * NXT_PACKET_IN2). */
269     uint16_t userdata_len;
270     uint8_t userdata[];
271 };
272
273 /* OFPACT_ENQUEUE.
274  *
275  * Used for OFPAT10_ENQUEUE. */
276 struct ofpact_enqueue {
277     struct ofpact ofpact;
278     ofp_port_t port;
279     uint32_t queue;
280 };
281
282 /* OFPACT_OUTPUT_REG.
283  *
284  * Used for NXAST_OUTPUT_REG. */
285 struct ofpact_output_reg {
286     struct ofpact ofpact;
287     uint16_t max_len;
288     struct mf_subfield src;
289 };
290
291 /* Bundle slave choice algorithm to apply.
292  *
293  * In the descriptions below, 'slaves' is the list of possible slaves in the
294  * order they appear in the OpenFlow action. */
295 enum nx_bd_algorithm {
296     /* Chooses the first live slave listed in the bundle.
297      *
298      * O(n_slaves) performance. */
299     NX_BD_ALG_ACTIVE_BACKUP = 0,
300
301     /* Highest Random Weight.
302      *
303      * for i in [0,n_slaves):
304      *   weights[i] = hash(flow, i)
305      * slave = { slaves[i] such that weights[i] >= weights[j] for all j != i }
306      *
307      * Redistributes 1/n_slaves of traffic when a slave's liveness changes.
308      * O(n_slaves) performance.
309      *
310      * Uses the 'fields' and 'basis' parameters. */
311     NX_BD_ALG_HRW = 1
312 };
313
314 /* OFPACT_BUNDLE.
315  *
316  * Used for NXAST_BUNDLE. */
317 struct ofpact_bundle {
318     struct ofpact ofpact;
319
320     /* Slave choice algorithm to apply to hash value. */
321     enum nx_bd_algorithm algorithm;
322
323     /* What fields to hash and how. */
324     enum nx_hash_fields fields;
325     uint16_t basis;             /* Universal hash parameter. */
326
327     struct mf_subfield dst;
328
329     /* Slaves for output. */
330     unsigned int n_slaves;
331     ofp_port_t slaves[];
332 };
333
334 /* OFPACT_SET_VLAN_VID.
335  *
336  * We keep track if vlan was present at action validation time to avoid a
337  * PUSH_VLAN when translating to OpenFlow 1.1+.
338  *
339  * We also keep the originating OFPUTIL action code in ofpact.compat.
340  *
341  * Used for OFPAT10_SET_VLAN_VID and OFPAT11_SET_VLAN_VID. */
342 struct ofpact_vlan_vid {
343     struct ofpact ofpact;
344     uint16_t vlan_vid;          /* VLAN VID in low 12 bits, 0 in other bits. */
345     bool push_vlan_if_needed;   /* OF 1.0 semantics if true. */
346     bool flow_has_vlan;         /* VLAN present at action validation time? */
347 };
348
349 /* OFPACT_SET_VLAN_PCP.
350  *
351  * We keep track if vlan was present at action validation time to avoid a
352  * PUSH_VLAN when translating to OpenFlow 1.1+.
353  *
354  * We also keep the originating OFPUTIL action code in ofpact.compat.
355  *
356  * Used for OFPAT10_SET_VLAN_PCP and OFPAT11_SET_VLAN_PCP. */
357 struct ofpact_vlan_pcp {
358     struct ofpact ofpact;
359     uint8_t vlan_pcp;           /* VLAN PCP in low 3 bits, 0 in other bits. */
360     bool push_vlan_if_needed;   /* OF 1.0 semantics if true. */
361     bool flow_has_vlan;         /* VLAN present at action validation time? */
362 };
363
364 /* OFPACT_SET_ETH_SRC, OFPACT_SET_ETH_DST.
365  *
366  * Used for OFPAT10_SET_DL_SRC, OFPAT10_SET_DL_DST. */
367 struct ofpact_mac {
368     struct ofpact ofpact;
369     struct eth_addr mac;
370 };
371
372 /* OFPACT_SET_IPV4_SRC, OFPACT_SET_IPV4_DST.
373  *
374  * Used for OFPAT10_SET_NW_SRC, OFPAT10_SET_NW_DST. */
375 struct ofpact_ipv4 {
376     struct ofpact ofpact;
377     ovs_be32 ipv4;
378 };
379
380 /* OFPACT_SET_IP_DSCP.
381  *
382  * Used for OFPAT10_SET_NW_TOS. */
383 struct ofpact_dscp {
384     struct ofpact ofpact;
385     uint8_t dscp;               /* DSCP in high 6 bits, rest ignored. */
386 };
387
388 /* OFPACT_SET_IP_ECN.
389  *
390  * Used for OFPAT11_SET_NW_ECN. */
391 struct ofpact_ecn {
392     struct ofpact ofpact;
393     uint8_t ecn;               /* ECN in low 2 bits, rest ignored. */
394 };
395
396 /* OFPACT_SET_IP_TTL.
397  *
398  * Used for OFPAT11_SET_NW_TTL. */
399 struct ofpact_ip_ttl {
400     struct ofpact ofpact;
401     uint8_t ttl;
402 };
403
404 /* OFPACT_SET_L4_SRC_PORT, OFPACT_SET_L4_DST_PORT.
405  *
406  * Used for OFPAT10_SET_TP_SRC, OFPAT10_SET_TP_DST. */
407 struct ofpact_l4_port {
408     struct ofpact ofpact;
409     uint16_t port;              /* TCP, UDP or SCTP port number. */
410     uint8_t  flow_ip_proto;     /* IP proto from corresponding match, or 0 */
411 };
412
413 /* OFPACT_REG_MOVE.
414  *
415  * Used for NXAST_REG_MOVE. */
416 struct ofpact_reg_move {
417     struct ofpact ofpact;
418     struct mf_subfield src;
419     struct mf_subfield dst;
420 };
421
422 /* OFPACT_STACK_PUSH.
423  *
424  * Used for NXAST_STACK_PUSH and NXAST_STACK_POP. */
425 struct ofpact_stack {
426     struct ofpact ofpact;
427     struct mf_subfield subfield;
428 };
429
430 /* OFPACT_SET_FIELD.
431  *
432  * Used for NXAST_REG_LOAD and OFPAT12_SET_FIELD. */
433 struct ofpact_set_field {
434     struct ofpact ofpact;
435     const struct mf_field *field;
436     bool flow_has_vlan;   /* VLAN present at action validation time. */
437     union mf_value value;
438     union mf_value mask;
439 };
440
441 /* OFPACT_PUSH_VLAN/MPLS/PBB
442  *
443  * Used for NXAST_PUSH_MPLS, OFPAT11_PUSH_MPLS. */
444 struct ofpact_push_mpls {
445     struct ofpact ofpact;
446     ovs_be16 ethertype;
447 };
448
449 /* OFPACT_POP_MPLS
450  *
451  * Used for NXAST_POP_MPLS, OFPAT11_POP_MPLS.. */
452 struct ofpact_pop_mpls {
453     struct ofpact ofpact;
454     ovs_be16 ethertype;
455 };
456
457 /* OFPACT_SET_TUNNEL.
458  *
459  * Used for NXAST_SET_TUNNEL, NXAST_SET_TUNNEL64. */
460 struct ofpact_tunnel {
461     struct ofpact ofpact;
462     uint64_t tun_id;
463 };
464
465 /* OFPACT_SET_QUEUE.
466  *
467  * Used for NXAST_SET_QUEUE. */
468 struct ofpact_queue {
469     struct ofpact ofpact;
470     uint32_t queue_id;
471 };
472
473 /* OFPACT_FIN_TIMEOUT.
474  *
475  * Used for NXAST_FIN_TIMEOUT. */
476 struct ofpact_fin_timeout {
477     struct ofpact ofpact;
478     uint16_t fin_idle_timeout;
479     uint16_t fin_hard_timeout;
480 };
481
482 /* OFPACT_WRITE_METADATA.
483  *
484  * Used for NXAST_WRITE_METADATA. */
485 struct ofpact_metadata {
486     struct ofpact ofpact;
487     ovs_be64 metadata;
488     ovs_be64 mask;
489 };
490
491 /* OFPACT_METER.
492  *
493  * Used for OFPIT13_METER. */
494 struct ofpact_meter {
495     struct ofpact ofpact;
496     uint32_t meter_id;
497 };
498
499 /* OFPACT_WRITE_ACTIONS.
500  *
501  * Used for OFPIT11_WRITE_ACTIONS. */
502 struct ofpact_nest {
503     OFPACT_PADDED_MEMBERS(struct ofpact ofpact;);
504     struct ofpact actions[];
505 };
506 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest, actions) % OFPACT_ALIGNTO == 0);
507 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest, actions)
508                   == sizeof(struct ofpact_nest));
509
510 /* Bits for 'flags' in struct nx_action_conntrack.
511  *
512  * If NX_CT_F_COMMIT is set, then the connection entry is moved from the
513  * unconfirmed to confirmed list in the tracker. */
514 enum nx_conntrack_flags {
515     NX_CT_F_COMMIT = 1 << 0,
516 };
517
518 /* Magic value for struct nx_action_conntrack 'recirc_table' field, to specify
519  * that the packet should not be recirculated. */
520 #define NX_CT_RECIRC_NONE OFPTT_ALL
521
522 #if !defined(IPPORT_FTP)
523 #define IPPORT_FTP  21
524 #endif
525
526 /* OFPACT_CT.
527  *
528  * Used for NXAST_CT. */
529 struct ofpact_conntrack {
530     OFPACT_PADDED_MEMBERS(
531         struct ofpact ofpact;
532         uint16_t flags;
533         uint16_t zone_imm;
534         struct mf_subfield zone_src;
535         uint16_t alg;
536         uint8_t recirc_table;
537     );
538     struct ofpact actions[0];
539 };
540 BUILD_ASSERT_DECL(offsetof(struct ofpact_conntrack, actions)
541                   % OFPACT_ALIGNTO == 0);
542 BUILD_ASSERT_DECL(offsetof(struct ofpact_conntrack, actions)
543                   == sizeof(struct ofpact_conntrack));
544
545 static inline size_t
546 ofpact_ct_get_action_len(const struct ofpact_conntrack *oc)
547 {
548     return oc->ofpact.len - offsetof(struct ofpact_conntrack, actions);
549 }
550
551 static inline size_t
552 ofpact_nest_get_action_len(const struct ofpact_nest *on)
553 {
554     return on->ofpact.len - offsetof(struct ofpact_nest, actions);
555 }
556
557 void ofpacts_execute_action_set(struct ofpbuf *action_list,
558                                 const struct ofpbuf *action_set);
559
560 /* Bits for 'flags' in struct nx_action_nat.
561  */
562 enum nx_nat_flags {
563     NX_NAT_F_SRC          = 1 << 0,
564     NX_NAT_F_DST          = 1 << 1,
565     NX_NAT_F_PERSISTENT   = 1 << 2,
566     NX_NAT_F_PROTO_HASH   = 1 << 3,
567     NX_NAT_F_PROTO_RANDOM = 1 << 4,
568 };
569
570 /* OFPACT_NAT.
571  *
572  * Used for NXAST_NAT. */
573 struct ofpact_nat {
574     struct ofpact ofpact;
575     uint8_t range_af; /* AF_UNSPEC, AF_INET, or AF_INET6 */
576     uint16_t flags;  /* NX_NAT_F_* */
577     struct {
578         struct {
579             uint16_t min;
580             uint16_t max;
581         } proto;
582         union {
583             struct {
584                 ovs_be32 min;
585                 ovs_be32 max;
586             } ipv4;
587             struct {
588                 struct in6_addr min;
589                 struct in6_addr max;
590             } ipv6;
591         } addr;
592     } range;
593 };
594
595
596 /* OFPACT_RESUBMIT.
597  *
598  * Used for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE. */
599 struct ofpact_resubmit {
600     struct ofpact ofpact;
601     ofp_port_t in_port;
602     uint8_t table_id;
603 };
604
605 /* Part of struct ofpact_learn, below. */
606 struct ofpact_learn_spec {
607     int n_bits;                 /* Number of bits in source and dest. */
608
609     int src_type;               /* One of NX_LEARN_SRC_*. */
610     struct mf_subfield src;     /* NX_LEARN_SRC_FIELD only. */
611     union mf_subvalue src_imm;  /* NX_LEARN_SRC_IMMEDIATE only. */
612
613     int dst_type;             /* One of NX_LEARN_DST_*. */
614     struct mf_subfield dst;   /* NX_LEARN_DST_MATCH, NX_LEARN_DST_LOAD only. */
615 };
616
617
618 /* Bits for 'flags' in struct nx_action_learn.
619  *
620  * If NX_LEARN_F_SEND_FLOW_REM is set, then the learned flows will have their
621  * OFPFF_SEND_FLOW_REM flag set.
622  *
623  * If NX_LEARN_F_DELETE_LEARNED is set, then removing this action will delete
624  * all the flows from the learn action's 'table_id' that have the learn
625  * action's 'cookie'.  Important points:
626  *
627  *     - The deleted flows include those created by this action, those created
628  *       by other learn actions with the same 'table_id' and 'cookie', those
629  *       created by flow_mod requests by a controller in the specified table
630  *       with the specified cookie, and those created through any other
631  *       means.
632  *
633  *     - If multiple flows specify "learn" actions with
634  *       NX_LEARN_F_DELETE_LEARNED with the same 'table_id' and 'cookie', then
635  *       no deletion occurs until all of those "learn" actions are deleted.
636  *
637  *     - Deleting a flow that contains a learn action is the most obvious way
638  *       to delete a learn action.  Modifying a flow's actions, or replacing it
639  *       by a new flow, can also delete a learn action.  Finally, replacing a
640  *       learn action with NX_LEARN_F_DELETE_LEARNED with a learn action
641  *       without that flag also effectively deletes the learn action and can
642  *       trigger flow deletion.
643  *
644  * NX_LEARN_F_DELETE_LEARNED was added in Open vSwitch 2.4. */
645 enum nx_learn_flags {
646     NX_LEARN_F_SEND_FLOW_REM = 1 << 0,
647     NX_LEARN_F_DELETE_LEARNED = 1 << 1,
648 };
649
650 #define NX_LEARN_N_BITS_MASK    0x3ff
651
652 #define NX_LEARN_SRC_FIELD     (0 << 13) /* Copy from field. */
653 #define NX_LEARN_SRC_IMMEDIATE (1 << 13) /* Copy from immediate value. */
654 #define NX_LEARN_SRC_MASK      (1 << 13)
655
656 #define NX_LEARN_DST_MATCH     (0 << 11) /* Add match criterion. */
657 #define NX_LEARN_DST_LOAD      (1 << 11) /* Add NXAST_REG_LOAD action. */
658 #define NX_LEARN_DST_OUTPUT    (2 << 11) /* Add OFPAT_OUTPUT action. */
659 #define NX_LEARN_DST_RESERVED  (3 << 11) /* Not yet defined. */
660 #define NX_LEARN_DST_MASK      (3 << 11)
661
662 /* OFPACT_LEARN.
663  *
664  * Used for NXAST_LEARN. */
665 struct ofpact_learn {
666     struct ofpact ofpact;
667
668     uint16_t idle_timeout;      /* Idle time before discarding (seconds). */
669     uint16_t hard_timeout;      /* Max time before discarding (seconds). */
670     uint16_t priority;          /* Priority level of flow entry. */
671     uint8_t table_id;           /* Table to insert flow entry. */
672     ovs_be64 cookie;            /* Cookie for new flow. */
673     enum nx_learn_flags flags;  /* NX_LEARN_F_*. */
674     uint16_t fin_idle_timeout;  /* Idle timeout after FIN, if nonzero. */
675     uint16_t fin_hard_timeout;  /* Hard timeout after FIN, if nonzero. */
676
677     unsigned int n_specs;
678     struct ofpact_learn_spec specs[];
679 };
680
681 /* Multipath link choice algorithm to apply.
682  *
683  * In the descriptions below, 'n_links' is max_link + 1. */
684 enum nx_mp_algorithm {
685     /* link = hash(flow) % n_links.
686      *
687      * Redistributes all traffic when n_links changes.  O(1) performance.  See
688      * RFC 2992.
689      *
690      * Use UINT16_MAX for max_link to get a raw hash value. */
691     NX_MP_ALG_MODULO_N = 0,
692
693     /* link = hash(flow) / (MAX_HASH / n_links).
694      *
695      * Redistributes between one-quarter and one-half of traffic when n_links
696      * changes.  O(1) performance.  See RFC 2992.
697      */
698     NX_MP_ALG_HASH_THRESHOLD = 1,
699
700     /* Highest Random Weight.
701      *
702      * for i in [0,n_links):
703      *   weights[i] = hash(flow, i)
704      * link = { i such that weights[i] >= weights[j] for all j != i }
705      *
706      * Redistributes 1/n_links of traffic when n_links changes.  O(n_links)
707      * performance.  If n_links is greater than a threshold (currently 64, but
708      * subject to change), Open vSwitch will substitute another algorithm
709      * automatically.  See RFC 2992. */
710     NX_MP_ALG_HRW = 2,
711
712     /* Iterative Hash.
713      *
714      * i = 0
715      * repeat:
716      *     i = i + 1
717      *     link = hash(flow, i) % arg
718      * while link > max_link
719      *
720      * Redistributes 1/n_links of traffic when n_links changes.  O(1)
721      * performance when arg/max_link is bounded by a constant.
722      *
723      * Redistributes all traffic when arg changes.
724      *
725      * arg must be greater than max_link and for best performance should be no
726      * more than approximately max_link * 2.  If arg is outside the acceptable
727      * range, Open vSwitch will automatically substitute the least power of 2
728      * greater than max_link.
729      *
730      * This algorithm is specific to Open vSwitch.
731      */
732     NX_MP_ALG_ITER_HASH = 3,
733 };
734
735 /* OFPACT_CONJUNCTION.
736  *
737  * Used for NXAST_CONJUNCTION. */
738 struct ofpact_conjunction {
739     struct ofpact ofpact;
740     uint8_t clause;
741     uint8_t n_clauses;
742     uint32_t id;
743 };
744
745 /* OFPACT_MULTIPATH.
746  *
747  * Used for NXAST_MULTIPATH. */
748 struct ofpact_multipath {
749     struct ofpact ofpact;
750
751     /* What fields to hash and how. */
752     enum nx_hash_fields fields;
753     uint16_t basis;             /* Universal hash parameter. */
754
755     /* Multipath link choice algorithm to apply to hash value. */
756     enum nx_mp_algorithm algorithm;
757     uint16_t max_link;          /* Number of output links, minus 1. */
758     uint32_t arg;               /* Algorithm-specific argument. */
759
760     /* Where to store the result. */
761     struct mf_subfield dst;
762 };
763
764 /* OFPACT_NOTE.
765  *
766  * Used for NXAST_NOTE. */
767 struct ofpact_note {
768     struct ofpact ofpact;
769     size_t length;
770     uint8_t data[];
771 };
772
773 /* OFPACT_SAMPLE.
774  *
775  * Used for NXAST_SAMPLE. */
776 struct ofpact_sample {
777     struct ofpact ofpact;
778     uint16_t probability;  // Always >0.
779     uint32_t collector_set_id;
780     uint32_t obs_domain_id;
781     uint32_t obs_point_id;
782 };
783
784 /* OFPACT_DEC_TTL.
785  *
786  * Used for OFPAT11_DEC_NW_TTL, NXAST_DEC_TTL and NXAST_DEC_TTL_CNT_IDS. */
787 struct ofpact_cnt_ids {
788     struct ofpact ofpact;
789
790     /* Controller ids. */
791     unsigned int n_controllers;
792     uint16_t cnt_ids[];
793 };
794
795 /* OFPACT_SET_MPLS_LABEL.
796  *
797  * Used for OFPAT11_SET_MPLS_LABEL and NXAST_SET_MPLS_LABEL */
798 struct ofpact_mpls_label {
799     struct ofpact ofpact;
800
801     ovs_be32 label;
802 };
803
804 /* OFPACT_SET_MPLS_TC.
805  *
806  * Used for OFPAT11_SET_MPLS_TC and NXAST_SET_MPLS_TC */
807 struct ofpact_mpls_tc {
808     struct ofpact ofpact;
809
810     uint8_t tc;
811 };
812
813 /* OFPACT_SET_MPLS_TTL.
814  *
815  * Used for OFPAT11_SET_MPLS_TTL and NXAST_SET_MPLS_TTL */
816 struct ofpact_mpls_ttl {
817     struct ofpact ofpact;
818
819     uint8_t ttl;
820 };
821
822 /* OFPACT_GOTO_TABLE
823  *
824  * Used for OFPIT11_GOTO_TABLE */
825 struct ofpact_goto_table {
826     struct ofpact ofpact;
827     uint8_t table_id;
828 };
829
830 /* OFPACT_GROUP.
831  *
832  * Used for OFPAT11_GROUP. */
833 struct ofpact_group {
834     struct ofpact ofpact;
835     uint32_t group_id;
836 };
837
838 /* OFPACT_UNROLL_XLATE.
839  *
840  * Used only internally. */
841 struct ofpact_unroll_xlate {
842     struct ofpact ofpact;
843
844     /* Metadata in xlate context, visible to controller via PACKET_INs. */
845     uint8_t  rule_table_id;       /* 0xFF if none. */
846     ovs_be64 rule_cookie;         /* OVS_BE64_MAX if none. */
847 };
848
849 /* Converting OpenFlow to ofpacts. */
850 enum ofperr ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
851                                           unsigned int actions_len,
852                                           enum ofp_version version,
853                                           struct ofpbuf *ofpacts);
854 enum ofperr ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
855                                                unsigned int instructions_len,
856                                                enum ofp_version version,
857                                                struct ofpbuf *ofpacts);
858 enum ofperr ofpacts_check(struct ofpact[], size_t ofpacts_len,
859                           struct flow *, ofp_port_t max_ports,
860                           uint8_t table_id, uint8_t n_tables,
861                           enum ofputil_protocol *usable_protocols);
862 enum ofperr ofpacts_check_consistency(struct ofpact[], size_t ofpacts_len,
863                                       struct flow *, ofp_port_t max_ports,
864                                       uint8_t table_id, uint8_t n_tables,
865                                       enum ofputil_protocol usable_protocols);
866 enum ofperr ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports);
867
868 /* Converting ofpacts to OpenFlow. */
869 size_t ofpacts_put_openflow_actions(const struct ofpact[], size_t ofpacts_len,
870                                     struct ofpbuf *openflow, enum ofp_version);
871 void ofpacts_put_openflow_instructions(const struct ofpact[],
872                                        size_t ofpacts_len,
873                                        struct ofpbuf *openflow,
874                                        enum ofp_version ofp_version);
875
876 /* Sets of supported actions. */
877 ovs_be32 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version);
878 uint64_t ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version);
879 void ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *);
880
881 /* Working with ofpacts. */
882 bool ofpacts_output_to_port(const struct ofpact[], size_t ofpacts_len,
883                             ofp_port_t port);
884 bool ofpacts_output_to_group(const struct ofpact[], size_t ofpacts_len,
885                              uint32_t group_id);
886 bool ofpacts_equal(const struct ofpact a[], size_t a_len,
887                    const struct ofpact b[], size_t b_len);
888 const struct mf_field *ofpact_get_mf_dst(const struct ofpact *ofpact);
889 uint32_t ofpacts_get_meter(const struct ofpact[], size_t ofpacts_len);
890
891 /* Formatting and parsing ofpacts. */
892 void ofpacts_format(const struct ofpact[], size_t ofpacts_len, struct ds *);
893 char *ofpacts_parse_actions(const char *, struct ofpbuf *ofpacts,
894                             enum ofputil_protocol *usable_protocols)
895     OVS_WARN_UNUSED_RESULT;
896 char *ofpacts_parse_instructions(const char *, struct ofpbuf *ofpacts,
897                                  enum ofputil_protocol *usable_protocols)
898     OVS_WARN_UNUSED_RESULT;
899 const char *ofpact_name(enum ofpact_type);
900
901 /* Internal use by the helpers below. */
902 void ofpact_init(struct ofpact *, enum ofpact_type, size_t len);
903 void *ofpact_put(struct ofpbuf *, enum ofpact_type, size_t len);
904
905 /* For each OFPACT_<ENUM> with a corresponding struct <STRUCT>, this defines
906  * the following commonly useful functions:
907  *
908  *   struct <STRUCT> *ofpact_put_<ENUM>(struct ofpbuf *ofpacts);
909  *
910  *     Appends a new 'ofpact', of length OFPACT_<ENUM>_SIZE, to 'ofpacts',
911  *     initializes it with ofpact_init_<ENUM>(), and returns it.  Also sets
912  *     'ofpacts->header' to the returned action.
913  *
914  *     After using this function to add a variable-length action, add the
915  *     elements of the flexible array (e.g. with ofpbuf_put()), then use
916  *     ofpact_finish() to pad the action to a multiple of OFPACT_ALIGNTO bytes
917  *     and update its embedded length field.  (Keep in mind the need to refresh
918  *     the structure from 'ofpacts->header' after adding data to 'ofpacts'.)
919  *
920  *   struct <STRUCT> *ofpact_get_<ENUM>(const struct ofpact *ofpact);
921  *
922  *     Returns 'ofpact' cast to "struct <STRUCT> *".  'ofpact->type' must be
923  *     OFPACT_<ENUM>.
924  *
925  * as well as the following more rarely useful definitions:
926  *
927  *   void ofpact_init_<ENUM>(struct <STRUCT> *ofpact);
928  *
929  *     Initializes the parts of 'ofpact' that identify it as having type
930  *     OFPACT_<ENUM> and length OFPACT_<ENUM>_SIZE and zeros the rest.
931  *
932  *   <ENUM>_SIZE
933  *
934  *     The size of the action structure.  For a fixed-length action, this is
935  *     sizeof(struct <STRUCT>) rounded up to a multiple of OFPACT_ALIGNTO.  For
936  *     a variable-length action, this is the offset to the variable-length
937  *     part.
938  */
939 #define OFPACT(ENUM, STRUCT, MEMBER, NAME)                              \
940     BUILD_ASSERT_DECL(offsetof(struct STRUCT, ofpact) == 0);            \
941                                                                         \
942     enum { OFPACT_##ENUM##_SIZE                                         \
943            = (offsetof(struct STRUCT, MEMBER)                           \
944               ? offsetof(struct STRUCT, MEMBER)                         \
945               : OFPACT_ALIGN(sizeof(struct STRUCT))) };                 \
946                                                                         \
947     static inline struct STRUCT *                                       \
948     ofpact_get_##ENUM(const struct ofpact *ofpact)                      \
949     {                                                                   \
950         ovs_assert(ofpact->type == OFPACT_##ENUM);                      \
951         return ALIGNED_CAST(struct STRUCT *, ofpact);                   \
952     }                                                                   \
953                                                                         \
954     static inline struct STRUCT *                                       \
955     ofpact_put_##ENUM(struct ofpbuf *ofpacts)                           \
956     {                                                                   \
957         return ofpact_put(ofpacts, OFPACT_##ENUM,                       \
958                           OFPACT_##ENUM##_SIZE);                        \
959     }                                                                   \
960                                                                         \
961     static inline void                                                  \
962     ofpact_init_##ENUM(struct STRUCT *ofpact)                           \
963     {                                                                   \
964         ofpact_init(&ofpact->ofpact, OFPACT_##ENUM,                     \
965                     OFPACT_##ENUM##_SIZE);                              \
966     }
967 OFPACTS
968 #undef OFPACT
969
970 /* Call after adding the variable-length part to a variable-length action. */
971 void ofpact_finish(struct ofpbuf *, struct ofpact *);
972
973 /* Additional functions for composing ofpacts. */
974 struct ofpact_set_field *ofpact_put_reg_load(struct ofpbuf *ofpacts);
975
976 /* OpenFlow 1.1 instructions.
977  * The order is sorted in execution order. Not in the value of OFPIT11_xxx.
978  * It is enforced on parser from text string.
979  */
980 #define OVS_INSTRUCTIONS                                    \
981     DEFINE_INST(OFPIT13_METER,                              \
982                 ofp13_instruction_meter,          false,    \
983                 "meter")                                    \
984                                                             \
985     DEFINE_INST(OFPIT11_APPLY_ACTIONS,                      \
986                 ofp11_instruction_actions,        true,     \
987                 "apply_actions")                            \
988                                                             \
989     DEFINE_INST(OFPIT11_CLEAR_ACTIONS,                      \
990                 ofp11_instruction,                false,    \
991                 "clear_actions")                            \
992                                                             \
993     DEFINE_INST(OFPIT11_WRITE_ACTIONS,                      \
994                 ofp11_instruction_actions,        true,     \
995                 "write_actions")                            \
996                                                             \
997     DEFINE_INST(OFPIT11_WRITE_METADATA,                     \
998                 ofp11_instruction_write_metadata, false,    \
999                 "write_metadata")                           \
1000                                                             \
1001     DEFINE_INST(OFPIT11_GOTO_TABLE,                         \
1002                 ofp11_instruction_goto_table,     false,    \
1003                 "goto_table")
1004
1005 enum ovs_instruction_type {
1006 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) OVSINST_##ENUM,
1007     OVS_INSTRUCTIONS
1008 #undef DEFINE_INST
1009 };
1010
1011 enum {
1012 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
1013     N_OVS_INSTRUCTIONS = OVS_INSTRUCTIONS
1014 #undef DEFINE_INST
1015 };
1016
1017 const char *ovs_instruction_name_from_type(enum ovs_instruction_type type);
1018 int ovs_instruction_type_from_name(const char *name);
1019 enum ovs_instruction_type ovs_instruction_type_from_ofpact_type(
1020     enum ofpact_type);
1021 enum ofperr ovs_instruction_type_from_inst_type(
1022     enum ovs_instruction_type *instruction_type, const uint16_t inst_type);
1023
1024 ovs_be32 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version);
1025 uint32_t ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap,
1026                                       enum ofp_version);
1027
1028 #endif /* ofp-actions.h */