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