conntrack: Add support for NAT.
[cascardo/ovs.git] / lib / ofp-actions.h
1 /*
2  * Copyright (c) 2012, 2013, 2014, 2015 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,  ofpact, "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 static inline struct ofpact *
189 ofpact_next(const struct ofpact *ofpact)
190 {
191     return (void *) ((uint8_t *) ofpact + OFPACT_ALIGN(ofpact->len));
192 }
193
194 static inline struct ofpact *
195 ofpact_end(const struct ofpact *ofpacts, size_t ofpacts_len)
196 {
197     return (void *) ((uint8_t *) ofpacts + ofpacts_len);
198 }
199
200 /* Assigns POS to each ofpact, in turn, in the OFPACTS_LEN bytes of ofpacts
201  * starting at OFPACTS. */
202 #define OFPACT_FOR_EACH(POS, OFPACTS, OFPACTS_LEN)                      \
203     for ((POS) = (OFPACTS); (POS) < ofpact_end(OFPACTS, OFPACTS_LEN);  \
204          (POS) = ofpact_next(POS))
205 \f
206 /* Action structure for each OFPACT_*. */
207
208 /* OFPACT_STRIP_VLAN, OFPACT_POP_QUEUE, OFPACT_EXIT, OFPACT_CLEAR_ACTIONS.
209  *
210  * Used for OFPAT10_STRIP_VLAN, NXAST_POP_QUEUE, NXAST_EXIT,
211  * OFPAT11_POP_VLAN, OFPIT11_CLEAR_ACTIONS.
212  *
213  * Action structure for actions that do not have any extra data beyond the
214  * action type. */
215 struct ofpact_null {
216     struct ofpact ofpact;
217 };
218
219 /* OFPACT_OUTPUT.
220  *
221  * Used for OFPAT10_OUTPUT. */
222 struct ofpact_output {
223     struct ofpact ofpact;
224     ofp_port_t port;            /* Output port. */
225     uint16_t max_len;           /* Max send len, for port OFPP_CONTROLLER. */
226 };
227
228 /* OFPACT_CONTROLLER.
229  *
230  * Used for NXAST_CONTROLLER. */
231 struct ofpact_controller {
232     struct ofpact ofpact;
233     uint16_t max_len;           /* Maximum length to send to controller. */
234     uint16_t controller_id;     /* Controller ID to send packet-in. */
235     enum ofp_packet_in_reason reason; /* Reason to put in packet-in. */
236 };
237
238 /* OFPACT_ENQUEUE.
239  *
240  * Used for OFPAT10_ENQUEUE. */
241 struct ofpact_enqueue {
242     struct ofpact ofpact;
243     ofp_port_t port;
244     uint32_t queue;
245 };
246
247 /* OFPACT_OUTPUT_REG.
248  *
249  * Used for NXAST_OUTPUT_REG. */
250 struct ofpact_output_reg {
251     struct ofpact ofpact;
252     uint16_t max_len;
253     struct mf_subfield src;
254 };
255
256 /* Bundle slave choice algorithm to apply.
257  *
258  * In the descriptions below, 'slaves' is the list of possible slaves in the
259  * order they appear in the OpenFlow action. */
260 enum nx_bd_algorithm {
261     /* Chooses the first live slave listed in the bundle.
262      *
263      * O(n_slaves) performance. */
264     NX_BD_ALG_ACTIVE_BACKUP = 0,
265
266     /* Highest Random Weight.
267      *
268      * for i in [0,n_slaves):
269      *   weights[i] = hash(flow, i)
270      * slave = { slaves[i] such that weights[i] >= weights[j] for all j != i }
271      *
272      * Redistributes 1/n_slaves of traffic when a slave's liveness changes.
273      * O(n_slaves) performance.
274      *
275      * Uses the 'fields' and 'basis' parameters. */
276     NX_BD_ALG_HRW = 1
277 };
278
279 /* OFPACT_BUNDLE.
280  *
281  * Used for NXAST_BUNDLE. */
282 struct ofpact_bundle {
283     struct ofpact ofpact;
284
285     /* Slave choice algorithm to apply to hash value. */
286     enum nx_bd_algorithm algorithm;
287
288     /* What fields to hash and how. */
289     enum nx_hash_fields fields;
290     uint16_t basis;             /* Universal hash parameter. */
291
292     struct mf_subfield dst;
293
294     /* Slaves for output. */
295     unsigned int n_slaves;
296     ofp_port_t slaves[];
297 };
298
299 /* OFPACT_SET_VLAN_VID.
300  *
301  * We keep track if vlan was present at action validation time to avoid a
302  * PUSH_VLAN when translating to OpenFlow 1.1+.
303  *
304  * We also keep the originating OFPUTIL action code in ofpact.compat.
305  *
306  * Used for OFPAT10_SET_VLAN_VID and OFPAT11_SET_VLAN_VID. */
307 struct ofpact_vlan_vid {
308     struct ofpact ofpact;
309     uint16_t vlan_vid;          /* VLAN VID in low 12 bits, 0 in other bits. */
310     bool push_vlan_if_needed;   /* OF 1.0 semantics if true. */
311     bool flow_has_vlan;         /* VLAN present at action validation time? */
312 };
313
314 /* OFPACT_SET_VLAN_PCP.
315  *
316  * We keep track if vlan was present at action validation time to avoid a
317  * PUSH_VLAN when translating to OpenFlow 1.1+.
318  *
319  * We also keep the originating OFPUTIL action code in ofpact.compat.
320  *
321  * Used for OFPAT10_SET_VLAN_PCP and OFPAT11_SET_VLAN_PCP. */
322 struct ofpact_vlan_pcp {
323     struct ofpact ofpact;
324     uint8_t vlan_pcp;           /* VLAN PCP in low 3 bits, 0 in other bits. */
325     bool push_vlan_if_needed;   /* OF 1.0 semantics if true. */
326     bool flow_has_vlan;         /* VLAN present at action validation time? */
327 };
328
329 /* OFPACT_SET_ETH_SRC, OFPACT_SET_ETH_DST.
330  *
331  * Used for OFPAT10_SET_DL_SRC, OFPAT10_SET_DL_DST. */
332 struct ofpact_mac {
333     struct ofpact ofpact;
334     struct eth_addr mac;
335 };
336
337 /* OFPACT_SET_IPV4_SRC, OFPACT_SET_IPV4_DST.
338  *
339  * Used for OFPAT10_SET_NW_SRC, OFPAT10_SET_NW_DST. */
340 struct ofpact_ipv4 {
341     struct ofpact ofpact;
342     ovs_be32 ipv4;
343 };
344
345 /* OFPACT_SET_IP_DSCP.
346  *
347  * Used for OFPAT10_SET_NW_TOS. */
348 struct ofpact_dscp {
349     struct ofpact ofpact;
350     uint8_t dscp;               /* DSCP in high 6 bits, rest ignored. */
351 };
352
353 /* OFPACT_SET_IP_ECN.
354  *
355  * Used for OFPAT11_SET_NW_ECN. */
356 struct ofpact_ecn {
357     struct ofpact ofpact;
358     uint8_t ecn;               /* ECN in low 2 bits, rest ignored. */
359 };
360
361 /* OFPACT_SET_IP_TTL.
362  *
363  * Used for OFPAT11_SET_NW_TTL. */
364 struct ofpact_ip_ttl {
365     struct ofpact ofpact;
366     uint8_t ttl;
367 };
368
369 /* OFPACT_SET_L4_SRC_PORT, OFPACT_SET_L4_DST_PORT.
370  *
371  * Used for OFPAT10_SET_TP_SRC, OFPAT10_SET_TP_DST. */
372 struct ofpact_l4_port {
373     struct ofpact ofpact;
374     uint16_t port;              /* TCP, UDP or SCTP port number. */
375     uint8_t  flow_ip_proto;     /* IP proto from corresponding match, or 0 */
376 };
377
378 /* OFPACT_REG_MOVE.
379  *
380  * Used for NXAST_REG_MOVE. */
381 struct ofpact_reg_move {
382     struct ofpact ofpact;
383     struct mf_subfield src;
384     struct mf_subfield dst;
385 };
386
387 /* OFPACT_STACK_PUSH.
388  *
389  * Used for NXAST_STACK_PUSH and NXAST_STACK_POP. */
390 struct ofpact_stack {
391     struct ofpact ofpact;
392     struct mf_subfield subfield;
393 };
394
395 /* OFPACT_SET_FIELD.
396  *
397  * Used for NXAST_REG_LOAD and OFPAT12_SET_FIELD. */
398 struct ofpact_set_field {
399     struct ofpact ofpact;
400     const struct mf_field *field;
401     bool flow_has_vlan;   /* VLAN present at action validation time. */
402     union mf_value value;
403     union mf_value mask;
404 };
405
406 /* OFPACT_PUSH_VLAN/MPLS/PBB
407  *
408  * Used for NXAST_PUSH_MPLS, OFPAT11_PUSH_MPLS. */
409 struct ofpact_push_mpls {
410     struct ofpact ofpact;
411     ovs_be16 ethertype;
412 };
413
414 /* OFPACT_POP_MPLS
415  *
416  * Used for NXAST_POP_MPLS, OFPAT11_POP_MPLS.. */
417 struct ofpact_pop_mpls {
418     struct ofpact ofpact;
419     ovs_be16 ethertype;
420 };
421
422 /* OFPACT_SET_TUNNEL.
423  *
424  * Used for NXAST_SET_TUNNEL, NXAST_SET_TUNNEL64. */
425 struct ofpact_tunnel {
426     struct ofpact ofpact;
427     uint64_t tun_id;
428 };
429
430 /* OFPACT_SET_QUEUE.
431  *
432  * Used for NXAST_SET_QUEUE. */
433 struct ofpact_queue {
434     struct ofpact ofpact;
435     uint32_t queue_id;
436 };
437
438 /* OFPACT_FIN_TIMEOUT.
439  *
440  * Used for NXAST_FIN_TIMEOUT. */
441 struct ofpact_fin_timeout {
442     struct ofpact ofpact;
443     uint16_t fin_idle_timeout;
444     uint16_t fin_hard_timeout;
445 };
446
447 /* OFPACT_WRITE_METADATA.
448  *
449  * Used for NXAST_WRITE_METADATA. */
450 struct ofpact_metadata {
451     struct ofpact ofpact;
452     ovs_be64 metadata;
453     ovs_be64 mask;
454 };
455
456 /* OFPACT_METER.
457  *
458  * Used for OFPIT13_METER. */
459 struct ofpact_meter {
460     struct ofpact ofpact;
461     uint32_t meter_id;
462 };
463
464 /* OFPACT_WRITE_ACTIONS.
465  *
466  * Used for OFPIT11_WRITE_ACTIONS. */
467 struct ofpact_nest {
468     struct ofpact ofpact;
469     uint8_t pad[PAD_SIZE(sizeof(struct ofpact), OFPACT_ALIGNTO)];
470     struct ofpact actions[];
471 };
472 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest, actions) % OFPACT_ALIGNTO == 0);
473 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest, actions)
474                   == sizeof(struct ofpact_nest));
475
476 /* Bits for 'flags' in struct nx_action_conntrack.
477  *
478  * If NX_CT_F_COMMIT is set, then the connection entry is moved from the
479  * unconfirmed to confirmed list in the tracker. */
480 enum nx_conntrack_flags {
481     NX_CT_F_COMMIT = 1 << 0,
482 };
483
484 /* Magic value for struct nx_action_conntrack 'recirc_table' field, to specify
485  * that the packet should not be recirculated. */
486 #define NX_CT_RECIRC_NONE OFPTT_ALL
487
488 /* We want to determine the size of these elements at compile time to ensure
489  * actions alignment, but we also want to allow ofpact_conntrack to have
490  * basic _put(), _get(), etc accessors defined below which access these
491  * members directly from ofpact_conntrack. An anonymous struct will serve
492  * both of these purposes. */
493 #define CT_MEMBERS                      \
494 struct {                                \
495     struct ofpact ofpact;               \
496     uint16_t flags;                     \
497     uint16_t zone_imm;                  \
498     struct mf_subfield zone_src;        \
499     uint16_t alg;                       \
500     uint8_t recirc_table;               \
501 }
502
503 /* OFPACT_CT.
504  *
505  * Used for NXAST_CT. */
506 struct ofpact_conntrack {
507     union {
508         CT_MEMBERS;
509         uint8_t pad[OFPACT_ALIGN(sizeof(CT_MEMBERS))];
510     };
511     struct ofpact actions[0];
512 };
513 BUILD_ASSERT_DECL(offsetof(struct ofpact_conntrack, actions)
514                   % OFPACT_ALIGNTO == 0);
515 BUILD_ASSERT_DECL(offsetof(struct ofpact_conntrack, actions)
516                   == sizeof(struct ofpact_conntrack));
517
518 static inline size_t
519 ofpact_ct_get_action_len(const struct ofpact_conntrack *oc)
520 {
521     return oc->ofpact.len - offsetof(struct ofpact_conntrack, actions);
522 }
523
524 static inline size_t
525 ofpact_nest_get_action_len(const struct ofpact_nest *on)
526 {
527     return on->ofpact.len - offsetof(struct ofpact_nest, actions);
528 }
529
530 void ofpacts_execute_action_set(struct ofpbuf *action_list,
531                                 const struct ofpbuf *action_set);
532
533 /* Bits for 'flags' in struct nx_action_nat.
534  */
535 enum nx_nat_flags {
536     NX_NAT_F_SRC          = 1 << 0,
537     NX_NAT_F_DST          = 1 << 1,
538     NX_NAT_F_PERSISTENT   = 1 << 2,
539     NX_NAT_F_PROTO_HASH   = 1 << 3,
540     NX_NAT_F_PROTO_RANDOM = 1 << 4,
541 };
542
543 /* OFPACT_NAT.
544  *
545  * Used for NXAST_NAT. */
546 struct ofpact_nat {
547     struct ofpact ofpact;
548     uint8_t range_af; /* AF_UNSPEC, AF_INET, or AF_INET6 */
549     uint16_t flags;  /* NX_NAT_F_* */
550     struct {
551         struct {
552             uint16_t min;
553             uint16_t max;
554         } proto;
555         union {
556             struct {
557                 ovs_be32 min;
558                 ovs_be32 max;
559             } ipv4;
560             struct {
561                 struct in6_addr min;
562                 struct in6_addr max;
563             } ipv6;
564         } addr;
565     } range;
566 };
567
568
569 /* OFPACT_RESUBMIT.
570  *
571  * Used for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE. */
572 struct ofpact_resubmit {
573     struct ofpact ofpact;
574     ofp_port_t in_port;
575     uint8_t table_id;
576 };
577
578 /* Part of struct ofpact_learn, below. */
579 struct ofpact_learn_spec {
580     int n_bits;                 /* Number of bits in source and dest. */
581
582     int src_type;               /* One of NX_LEARN_SRC_*. */
583     struct mf_subfield src;     /* NX_LEARN_SRC_FIELD only. */
584     union mf_subvalue src_imm;  /* NX_LEARN_SRC_IMMEDIATE only. */
585
586     int dst_type;             /* One of NX_LEARN_DST_*. */
587     struct mf_subfield dst;   /* NX_LEARN_DST_MATCH, NX_LEARN_DST_LOAD only. */
588 };
589
590
591 /* Bits for 'flags' in struct nx_action_learn.
592  *
593  * If NX_LEARN_F_SEND_FLOW_REM is set, then the learned flows will have their
594  * OFPFF_SEND_FLOW_REM flag set.
595  *
596  * If NX_LEARN_F_DELETE_LEARNED is set, then removing this action will delete
597  * all the flows from the learn action's 'table_id' that have the learn
598  * action's 'cookie'.  Important points:
599  *
600  *     - The deleted flows include those created by this action, those created
601  *       by other learn actions with the same 'table_id' and 'cookie', those
602  *       created by flow_mod requests by a controller in the specified table
603  *       with the specified cookie, and those created through any other
604  *       means.
605  *
606  *     - If multiple flows specify "learn" actions with
607  *       NX_LEARN_F_DELETE_LEARNED with the same 'table_id' and 'cookie', then
608  *       no deletion occurs until all of those "learn" actions are deleted.
609  *
610  *     - Deleting a flow that contains a learn action is the most obvious way
611  *       to delete a learn action.  Modifying a flow's actions, or replacing it
612  *       by a new flow, can also delete a learn action.  Finally, replacing a
613  *       learn action with NX_LEARN_F_DELETE_LEARNED with a learn action
614  *       without that flag also effectively deletes the learn action and can
615  *       trigger flow deletion.
616  *
617  * NX_LEARN_F_DELETE_LEARNED was added in Open vSwitch 2.4. */
618 enum nx_learn_flags {
619     NX_LEARN_F_SEND_FLOW_REM = 1 << 0,
620     NX_LEARN_F_DELETE_LEARNED = 1 << 1,
621 };
622
623 #define NX_LEARN_N_BITS_MASK    0x3ff
624
625 #define NX_LEARN_SRC_FIELD     (0 << 13) /* Copy from field. */
626 #define NX_LEARN_SRC_IMMEDIATE (1 << 13) /* Copy from immediate value. */
627 #define NX_LEARN_SRC_MASK      (1 << 13)
628
629 #define NX_LEARN_DST_MATCH     (0 << 11) /* Add match criterion. */
630 #define NX_LEARN_DST_LOAD      (1 << 11) /* Add NXAST_REG_LOAD action. */
631 #define NX_LEARN_DST_OUTPUT    (2 << 11) /* Add OFPAT_OUTPUT action. */
632 #define NX_LEARN_DST_RESERVED  (3 << 11) /* Not yet defined. */
633 #define NX_LEARN_DST_MASK      (3 << 11)
634
635 /* OFPACT_LEARN.
636  *
637  * Used for NXAST_LEARN. */
638 struct ofpact_learn {
639     struct ofpact ofpact;
640
641     uint16_t idle_timeout;      /* Idle time before discarding (seconds). */
642     uint16_t hard_timeout;      /* Max time before discarding (seconds). */
643     uint16_t priority;          /* Priority level of flow entry. */
644     uint8_t table_id;           /* Table to insert flow entry. */
645     ovs_be64 cookie;            /* Cookie for new flow. */
646     enum nx_learn_flags flags;  /* NX_LEARN_F_*. */
647     uint16_t fin_idle_timeout;  /* Idle timeout after FIN, if nonzero. */
648     uint16_t fin_hard_timeout;  /* Hard timeout after FIN, if nonzero. */
649
650     unsigned int n_specs;
651     struct ofpact_learn_spec specs[];
652 };
653
654 /* Multipath link choice algorithm to apply.
655  *
656  * In the descriptions below, 'n_links' is max_link + 1. */
657 enum nx_mp_algorithm {
658     /* link = hash(flow) % n_links.
659      *
660      * Redistributes all traffic when n_links changes.  O(1) performance.  See
661      * RFC 2992.
662      *
663      * Use UINT16_MAX for max_link to get a raw hash value. */
664     NX_MP_ALG_MODULO_N = 0,
665
666     /* link = hash(flow) / (MAX_HASH / n_links).
667      *
668      * Redistributes between one-quarter and one-half of traffic when n_links
669      * changes.  O(1) performance.  See RFC 2992.
670      */
671     NX_MP_ALG_HASH_THRESHOLD = 1,
672
673     /* Highest Random Weight.
674      *
675      * for i in [0,n_links):
676      *   weights[i] = hash(flow, i)
677      * link = { i such that weights[i] >= weights[j] for all j != i }
678      *
679      * Redistributes 1/n_links of traffic when n_links changes.  O(n_links)
680      * performance.  If n_links is greater than a threshold (currently 64, but
681      * subject to change), Open vSwitch will substitute another algorithm
682      * automatically.  See RFC 2992. */
683     NX_MP_ALG_HRW = 2,
684
685     /* Iterative Hash.
686      *
687      * i = 0
688      * repeat:
689      *     i = i + 1
690      *     link = hash(flow, i) % arg
691      * while link > max_link
692      *
693      * Redistributes 1/n_links of traffic when n_links changes.  O(1)
694      * performance when arg/max_link is bounded by a constant.
695      *
696      * Redistributes all traffic when arg changes.
697      *
698      * arg must be greater than max_link and for best performance should be no
699      * more than approximately max_link * 2.  If arg is outside the acceptable
700      * range, Open vSwitch will automatically substitute the least power of 2
701      * greater than max_link.
702      *
703      * This algorithm is specific to Open vSwitch.
704      */
705     NX_MP_ALG_ITER_HASH = 3,
706 };
707
708 /* OFPACT_CONJUNCTION.
709  *
710  * Used for NXAST_CONJUNCTION. */
711 struct ofpact_conjunction {
712     struct ofpact ofpact;
713     uint8_t clause;
714     uint8_t n_clauses;
715     uint32_t id;
716 };
717
718 /* OFPACT_MULTIPATH.
719  *
720  * Used for NXAST_MULTIPATH. */
721 struct ofpact_multipath {
722     struct ofpact ofpact;
723
724     /* What fields to hash and how. */
725     enum nx_hash_fields fields;
726     uint16_t basis;             /* Universal hash parameter. */
727
728     /* Multipath link choice algorithm to apply to hash value. */
729     enum nx_mp_algorithm algorithm;
730     uint16_t max_link;          /* Number of output links, minus 1. */
731     uint32_t arg;               /* Algorithm-specific argument. */
732
733     /* Where to store the result. */
734     struct mf_subfield dst;
735 };
736
737 /* OFPACT_NOTE.
738  *
739  * Used for NXAST_NOTE. */
740 struct ofpact_note {
741     struct ofpact ofpact;
742     size_t length;
743     uint8_t data[];
744 };
745
746 /* OFPACT_SAMPLE.
747  *
748  * Used for NXAST_SAMPLE. */
749 struct ofpact_sample {
750     struct ofpact ofpact;
751     uint16_t probability;  // Always >0.
752     uint32_t collector_set_id;
753     uint32_t obs_domain_id;
754     uint32_t obs_point_id;
755 };
756
757 /* OFPACT_DEC_TTL.
758  *
759  * Used for OFPAT11_DEC_NW_TTL, NXAST_DEC_TTL and NXAST_DEC_TTL_CNT_IDS. */
760 struct ofpact_cnt_ids {
761     struct ofpact ofpact;
762
763     /* Controller ids. */
764     unsigned int n_controllers;
765     uint16_t cnt_ids[];
766 };
767
768 /* OFPACT_SET_MPLS_LABEL.
769  *
770  * Used for OFPAT11_SET_MPLS_LABEL and NXAST_SET_MPLS_LABEL */
771 struct ofpact_mpls_label {
772     struct ofpact ofpact;
773
774     ovs_be32 label;
775 };
776
777 /* OFPACT_SET_MPLS_TC.
778  *
779  * Used for OFPAT11_SET_MPLS_TC and NXAST_SET_MPLS_TC */
780 struct ofpact_mpls_tc {
781     struct ofpact ofpact;
782
783     uint8_t tc;
784 };
785
786 /* OFPACT_SET_MPLS_TTL.
787  *
788  * Used for OFPAT11_SET_MPLS_TTL and NXAST_SET_MPLS_TTL */
789 struct ofpact_mpls_ttl {
790     struct ofpact ofpact;
791
792     uint8_t ttl;
793 };
794
795 /* OFPACT_GOTO_TABLE
796  *
797  * Used for OFPIT11_GOTO_TABLE */
798 struct ofpact_goto_table {
799     struct ofpact ofpact;
800     uint8_t table_id;
801 };
802
803 /* OFPACT_GROUP.
804  *
805  * Used for OFPAT11_GROUP. */
806 struct ofpact_group {
807     struct ofpact ofpact;
808     uint32_t group_id;
809 };
810
811 /* OFPACT_UNROLL_XLATE.
812  *
813  * Used only internally. */
814 struct ofpact_unroll_xlate {
815     struct ofpact ofpact;
816
817     /* Metadata in xlate context, visible to controller via PACKET_INs. */
818     uint8_t  rule_table_id;       /* 0xFF if none. */
819     ovs_be64 rule_cookie;         /* OVS_BE64_MAX if none. */
820 };
821
822 /* Converting OpenFlow to ofpacts. */
823 enum ofperr ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
824                                           unsigned int actions_len,
825                                           enum ofp_version version,
826                                           struct ofpbuf *ofpacts);
827 enum ofperr ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
828                                                unsigned int instructions_len,
829                                                enum ofp_version version,
830                                                struct ofpbuf *ofpacts);
831 enum ofperr ofpacts_check(struct ofpact[], size_t ofpacts_len,
832                           struct flow *, ofp_port_t max_ports,
833                           uint8_t table_id, uint8_t n_tables,
834                           enum ofputil_protocol *usable_protocols);
835 enum ofperr ofpacts_check_consistency(struct ofpact[], size_t ofpacts_len,
836                                       struct flow *, ofp_port_t max_ports,
837                                       uint8_t table_id, uint8_t n_tables,
838                                       enum ofputil_protocol usable_protocols);
839 enum ofperr ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports);
840
841 /* Converting ofpacts to OpenFlow. */
842 size_t ofpacts_put_openflow_actions(const struct ofpact[], size_t ofpacts_len,
843                                     struct ofpbuf *openflow, enum ofp_version);
844 void ofpacts_put_openflow_instructions(const struct ofpact[],
845                                        size_t ofpacts_len,
846                                        struct ofpbuf *openflow,
847                                        enum ofp_version ofp_version);
848
849 /* Sets of supported actions. */
850 ovs_be32 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version);
851 uint64_t ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version);
852 void ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *);
853
854 /* Working with ofpacts. */
855 bool ofpacts_output_to_port(const struct ofpact[], size_t ofpacts_len,
856                             ofp_port_t port);
857 bool ofpacts_output_to_group(const struct ofpact[], size_t ofpacts_len,
858                              uint32_t group_id);
859 bool ofpacts_equal(const struct ofpact a[], size_t a_len,
860                    const struct ofpact b[], size_t b_len);
861 uint32_t ofpacts_get_meter(const struct ofpact[], size_t ofpacts_len);
862
863 /* Formatting and parsing ofpacts. */
864 void ofpacts_format(const struct ofpact[], size_t ofpacts_len, struct ds *);
865 char *ofpacts_parse_actions(const char *, struct ofpbuf *ofpacts,
866                             enum ofputil_protocol *usable_protocols)
867     OVS_WARN_UNUSED_RESULT;
868 char *ofpacts_parse_instructions(const char *, struct ofpbuf *ofpacts,
869                                  enum ofputil_protocol *usable_protocols)
870     OVS_WARN_UNUSED_RESULT;
871 const char *ofpact_name(enum ofpact_type);
872
873 /* Internal use by the helpers below. */
874 void ofpact_init(struct ofpact *, enum ofpact_type, size_t len);
875 void *ofpact_put(struct ofpbuf *, enum ofpact_type, size_t len);
876
877 /* For each OFPACT_<ENUM> with a corresponding struct <STRUCT>, this defines
878  * the following commonly useful functions:
879  *
880  *   struct <STRUCT> *ofpact_put_<ENUM>(struct ofpbuf *ofpacts);
881  *
882  *     Appends a new 'ofpact', of length OFPACT_<ENUM>_RAW_SIZE, to 'ofpacts',
883  *     initializes it with ofpact_init_<ENUM>(), and returns it.  Also sets
884  *     'ofpacts->header' to the returned action.
885  *
886  *     After using this function to add a variable-length action, add the
887  *     elements of the flexible array (e.g. with ofpbuf_put()), then use
888  *     ofpact_update_len() to update the length embedded into the action.
889  *     (Keep in mind the need to refresh the structure from 'ofpacts->frame'
890  *     after adding data to 'ofpacts'.)
891  *
892  *   struct <STRUCT> *ofpact_get_<ENUM>(const struct ofpact *ofpact);
893  *
894  *     Returns 'ofpact' cast to "struct <STRUCT> *".  'ofpact->type' must be
895  *     OFPACT_<ENUM>.
896  *
897  * as well as the following more rarely useful definitions:
898  *
899  *   void ofpact_init_<ENUM>(struct <STRUCT> *ofpact);
900  *
901  *     Initializes the parts of 'ofpact' that identify it as having type
902  *     OFPACT_<ENUM> and length OFPACT_<ENUM>_RAW_SIZE and zeros the rest.
903  *
904  *   <ENUM>_RAW_SIZE
905  *
906  *     The size of the action structure.  For a fixed-length action, this is
907  *     sizeof(struct <STRUCT>).  For a variable-length action, this is the
908  *     offset to the variable-length part.
909  *
910  *   <ENUM>_SIZE
911  *
912  *     An integer constant, the value of OFPACT_<ENUM>_RAW_SIZE rounded up to a
913  *     multiple of OFPACT_ALIGNTO.
914  */
915 #define OFPACT(ENUM, STRUCT, MEMBER, NAME)                              \
916     BUILD_ASSERT_DECL(offsetof(struct STRUCT, ofpact) == 0);            \
917                                                                         \
918     enum { OFPACT_##ENUM##_RAW_SIZE                                     \
919            = (offsetof(struct STRUCT, MEMBER)                           \
920               ? offsetof(struct STRUCT, MEMBER)                         \
921               : sizeof(struct STRUCT)) };                               \
922                                                                         \
923     enum { OFPACT_##ENUM##_SIZE                                         \
924            = ROUND_UP(OFPACT_##ENUM##_RAW_SIZE, OFPACT_ALIGNTO) };      \
925                                                                         \
926     static inline struct STRUCT *                                       \
927     ofpact_get_##ENUM(const struct ofpact *ofpact)                      \
928     {                                                                   \
929         ovs_assert(ofpact->type == OFPACT_##ENUM);                      \
930         return ALIGNED_CAST(struct STRUCT *, ofpact);                   \
931     }                                                                   \
932                                                                         \
933     static inline struct STRUCT *                                       \
934     ofpact_put_##ENUM(struct ofpbuf *ofpacts)                           \
935     {                                                                   \
936         return ofpact_put(ofpacts, OFPACT_##ENUM,                       \
937                           OFPACT_##ENUM##_RAW_SIZE);                    \
938     }                                                                   \
939                                                                         \
940     static inline void                                                  \
941     ofpact_init_##ENUM(struct STRUCT *ofpact)                           \
942     {                                                                   \
943         ofpact_init(&ofpact->ofpact, OFPACT_##ENUM,                     \
944                     OFPACT_##ENUM##_RAW_SIZE);                          \
945     }
946 OFPACTS
947 #undef OFPACT
948
949 /* Functions to use after adding ofpacts to a buffer. */
950 void ofpact_update_len(struct ofpbuf *, struct ofpact *);
951 void ofpact_pad(struct ofpbuf *);
952
953 /* Additional functions for composing ofpacts. */
954 struct ofpact_set_field *ofpact_put_reg_load(struct ofpbuf *ofpacts);
955
956 /* OpenFlow 1.1 instructions.
957  * The order is sorted in execution order. Not in the value of OFPIT11_xxx.
958  * It is enforced on parser from text string.
959  */
960 #define OVS_INSTRUCTIONS                                    \
961     DEFINE_INST(OFPIT13_METER,                              \
962                 ofp13_instruction_meter,          false,    \
963                 "meter")                                    \
964                                                             \
965     DEFINE_INST(OFPIT11_APPLY_ACTIONS,                      \
966                 ofp11_instruction_actions,        true,     \
967                 "apply_actions")                            \
968                                                             \
969     DEFINE_INST(OFPIT11_CLEAR_ACTIONS,                      \
970                 ofp11_instruction,                false,    \
971                 "clear_actions")                            \
972                                                             \
973     DEFINE_INST(OFPIT11_WRITE_ACTIONS,                      \
974                 ofp11_instruction_actions,        true,     \
975                 "write_actions")                            \
976                                                             \
977     DEFINE_INST(OFPIT11_WRITE_METADATA,                     \
978                 ofp11_instruction_write_metadata, false,    \
979                 "write_metadata")                           \
980                                                             \
981     DEFINE_INST(OFPIT11_GOTO_TABLE,                         \
982                 ofp11_instruction_goto_table,     false,    \
983                 "goto_table")
984
985 enum ovs_instruction_type {
986 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) OVSINST_##ENUM,
987     OVS_INSTRUCTIONS
988 #undef DEFINE_INST
989 };
990
991 enum {
992 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
993     N_OVS_INSTRUCTIONS = OVS_INSTRUCTIONS
994 #undef DEFINE_INST
995 };
996
997 const char *ovs_instruction_name_from_type(enum ovs_instruction_type type);
998 int ovs_instruction_type_from_name(const char *name);
999 enum ovs_instruction_type ovs_instruction_type_from_ofpact_type(
1000     enum ofpact_type);
1001 enum ofperr ovs_instruction_type_from_inst_type(
1002     enum ovs_instruction_type *instruction_type, const uint16_t inst_type);
1003
1004 ovs_be32 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version);
1005 uint32_t ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap,
1006                                       enum ofp_version);
1007
1008 #endif /* ofp-actions.h */