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