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