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