ofp-actions: Pretend that OpenFlow 1.0 has instructions.
[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 'compat' 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 'compat' 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 'compat' member should be the original action
155  *       type.  (If the action didn't originate from OpenFlow, then setting
156  *       'compat' to zero should be fine: code to translate the ofpact to
157  *       OpenFlow must tolerate this case.)
158  */
159 struct ofpact {
160     enum ofpact_type type;      /* OFPACT_*. */
161     enum ofputil_action_code compat; /* 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 /* OFPACT_BUNDLE.
244  *
245  * Used for NXAST_BUNDLE. */
246 struct ofpact_bundle {
247     struct ofpact ofpact;
248
249     /* Slave choice algorithm to apply to hash value. */
250     enum nx_bd_algorithm algorithm;
251
252     /* What fields to hash and how. */
253     enum nx_hash_fields fields;
254     uint16_t basis;             /* Universal hash parameter. */
255
256     struct mf_subfield dst;
257
258     /* Slaves for output. */
259     unsigned int n_slaves;
260     ofp_port_t slaves[];
261 };
262
263 /* OFPACT_SET_VLAN_VID.
264  *
265  * We keep track if vlan was present at action validation time to avoid a
266  * PUSH_VLAN when translating to OpenFlow 1.1+.
267  *
268  * We also keep the originating OFPUTIL action code in ofpact.compat.
269  *
270  * Used for OFPAT10_SET_VLAN_VID and OFPAT11_SET_VLAN_VID. */
271 struct ofpact_vlan_vid {
272     struct ofpact ofpact;
273     uint16_t vlan_vid;          /* VLAN VID in low 12 bits, 0 in other bits. */
274     bool push_vlan_if_needed;   /* OF 1.0 semantics if true. */
275     bool flow_has_vlan;         /* VLAN present at action validation time? */
276 };
277
278 /* OFPACT_SET_VLAN_PCP.
279  *
280  * We keep track if vlan was present at action validation time to avoid a
281  * PUSH_VLAN when translating to OpenFlow 1.1+.
282  *
283  * We also keep the originating OFPUTIL action code in ofpact.compat.
284  *
285  * Used for OFPAT10_SET_VLAN_PCP and OFPAT11_SET_VLAN_PCP. */
286 struct ofpact_vlan_pcp {
287     struct ofpact ofpact;
288     uint8_t vlan_pcp;           /* VLAN PCP in low 3 bits, 0 in other bits. */
289     bool push_vlan_if_needed;   /* OF 1.0 semantics if true. */
290     bool flow_has_vlan;         /* VLAN present at action validation time? */
291 };
292
293 /* OFPACT_SET_ETH_SRC, OFPACT_SET_ETH_DST.
294  *
295  * Used for OFPAT10_SET_DL_SRC, OFPAT10_SET_DL_DST. */
296 struct ofpact_mac {
297     struct ofpact ofpact;
298     uint8_t mac[ETH_ADDR_LEN];
299 };
300
301 /* OFPACT_SET_IPV4_SRC, OFPACT_SET_IPV4_DST.
302  *
303  * Used for OFPAT10_SET_NW_SRC, OFPAT10_SET_NW_DST. */
304 struct ofpact_ipv4 {
305     struct ofpact ofpact;
306     ovs_be32 ipv4;
307 };
308
309 /* OFPACT_SET_IP_DSCP.
310  *
311  * Used for OFPAT10_SET_NW_TOS. */
312 struct ofpact_dscp {
313     struct ofpact ofpact;
314     uint8_t dscp;               /* DSCP in high 6 bits, rest ignored. */
315 };
316
317 /* OFPACT_SET_IP_ECN.
318  *
319  * Used for OFPAT11_SET_NW_ECN. */
320 struct ofpact_ecn {
321     struct ofpact ofpact;
322     uint8_t ecn;               /* ECN in low 2 bits, rest ignored. */
323 };
324
325 /* OFPACT_SET_IP_TTL.
326  *
327  * Used for OFPAT11_SET_NW_TTL. */
328 struct ofpact_ip_ttl {
329     struct ofpact ofpact;
330     uint8_t ttl;
331 };
332
333 /* OFPACT_SET_L4_SRC_PORT, OFPACT_SET_L4_DST_PORT.
334  *
335  * Used for OFPAT10_SET_TP_SRC, OFPAT10_SET_TP_DST. */
336 struct ofpact_l4_port {
337     struct ofpact ofpact;
338     uint16_t port;              /* TCP, UDP or SCTP port number. */
339     uint8_t  flow_ip_proto;     /* IP proto from corresponding match, or 0 */
340 };
341
342 /* OFPACT_REG_MOVE.
343  *
344  * Used for NXAST_REG_MOVE. */
345 struct ofpact_reg_move {
346     struct ofpact ofpact;
347     struct mf_subfield src;
348     struct mf_subfield dst;
349 };
350
351 /* OFPACT_STACK_PUSH.
352  *
353  * Used for NXAST_STACK_PUSH and NXAST_STACK_POP. */
354 struct ofpact_stack {
355     struct ofpact ofpact;
356     struct mf_subfield subfield;
357 };
358
359 /* OFPACT_REG_LOAD.
360  *
361  * Used for NXAST_REG_LOAD. */
362 struct ofpact_reg_load {
363     struct ofpact ofpact;
364     struct mf_subfield dst;
365     union mf_subvalue subvalue; /* Least-significant bits are used. */
366 };
367
368 /* OFPACT_SET_FIELD.
369  *
370  * Used for OFPAT12_SET_FIELD. */
371 struct ofpact_set_field {
372     struct ofpact ofpact;
373     const struct mf_field *field;
374     bool flow_has_vlan;   /* VLAN present at action validation time. */
375     union mf_value value;
376 };
377
378 /* OFPACT_PUSH_VLAN/MPLS/PBB
379  *
380  * Used for NXAST_PUSH_MPLS, OFPAT11_PUSH_MPLS. */
381 struct ofpact_push_mpls {
382     struct ofpact ofpact;
383     ovs_be16 ethertype;
384 };
385
386 /* OFPACT_POP_MPLS
387  *
388  * Used for NXAST_POP_MPLS, OFPAT11_POP_MPLS.. */
389 struct ofpact_pop_mpls {
390     struct ofpact ofpact;
391     ovs_be16 ethertype;
392 };
393
394 /* OFPACT_SET_TUNNEL.
395  *
396  * Used for NXAST_SET_TUNNEL, NXAST_SET_TUNNEL64. */
397 struct ofpact_tunnel {
398     struct ofpact ofpact;
399     uint64_t tun_id;
400 };
401
402 /* OFPACT_SET_QUEUE.
403  *
404  * Used for NXAST_SET_QUEUE. */
405 struct ofpact_queue {
406     struct ofpact ofpact;
407     uint32_t queue_id;
408 };
409
410 /* OFPACT_FIN_TIMEOUT.
411  *
412  * Used for NXAST_FIN_TIMEOUT. */
413 struct ofpact_fin_timeout {
414     struct ofpact ofpact;
415     uint16_t fin_idle_timeout;
416     uint16_t fin_hard_timeout;
417 };
418
419 /* OFPACT_WRITE_METADATA.
420  *
421  * Used for NXAST_WRITE_METADATA. */
422 struct ofpact_metadata {
423     struct ofpact ofpact;
424     ovs_be64 metadata;
425     ovs_be64 mask;
426 };
427
428 /* OFPACT_METER.
429  *
430  * Used for OFPIT13_METER. */
431 struct ofpact_meter {
432     struct ofpact ofpact;
433     uint32_t meter_id;
434 };
435
436 /* OFPACT_WRITE_ACTIONS.
437  *
438  * Used for OFPIT11_WRITE_ACTIONS. */
439 struct ofpact_nest {
440     struct ofpact ofpact;
441     uint8_t pad[PAD_SIZE(sizeof(struct ofpact), OFPACT_ALIGNTO)];
442     struct ofpact actions[];
443 };
444 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest, actions) % OFPACT_ALIGNTO == 0);
445
446 static inline size_t
447 ofpact_nest_get_action_len(const struct ofpact_nest *on)
448 {
449     return on->ofpact.len - offsetof(struct ofpact_nest, actions);
450 }
451
452 void ofpacts_execute_action_set(struct ofpbuf *action_list,
453                                 const struct ofpbuf *action_set);
454
455 /* OFPACT_RESUBMIT.
456  *
457  * Used for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE. */
458 struct ofpact_resubmit {
459     struct ofpact ofpact;
460     ofp_port_t in_port;
461     uint8_t table_id;
462 };
463
464 /* Part of struct ofpact_learn, below. */
465 struct ofpact_learn_spec {
466     int n_bits;                 /* Number of bits in source and dest. */
467
468     int src_type;               /* One of NX_LEARN_SRC_*. */
469     struct mf_subfield src;     /* NX_LEARN_SRC_FIELD only. */
470     union mf_subvalue src_imm;  /* NX_LEARN_SRC_IMMEDIATE only. */
471
472     int dst_type;             /* One of NX_LEARN_DST_*. */
473     struct mf_subfield dst;   /* NX_LEARN_DST_MATCH, NX_LEARN_DST_LOAD only. */
474 };
475
476 /* OFPACT_LEARN.
477  *
478  * Used for NXAST_LEARN. */
479 struct ofpact_learn {
480     struct ofpact ofpact;
481
482     uint16_t idle_timeout;      /* Idle time before discarding (seconds). */
483     uint16_t hard_timeout;      /* Max time before discarding (seconds). */
484     uint16_t priority;          /* Priority level of flow entry. */
485     uint8_t table_id;           /* Table to insert flow entry. */
486     ovs_be64 cookie;            /* Cookie for new flow. */
487     enum nx_learn_flags flags;  /* NX_LEARN_F_*. */
488     uint16_t fin_idle_timeout;  /* Idle timeout after FIN, if nonzero. */
489     uint16_t fin_hard_timeout;  /* Hard timeout after FIN, if nonzero. */
490
491     unsigned int n_specs;
492     struct ofpact_learn_spec specs[];
493 };
494
495 /* OFPACT_MULTIPATH.
496  *
497  * Used for NXAST_MULTIPATH. */
498 struct ofpact_multipath {
499     struct ofpact ofpact;
500
501     /* What fields to hash and how. */
502     enum nx_hash_fields fields;
503     uint16_t basis;             /* Universal hash parameter. */
504
505     /* Multipath link choice algorithm to apply to hash value. */
506     enum nx_mp_algorithm algorithm;
507     uint16_t max_link;          /* Number of output links, minus 1. */
508     uint32_t arg;               /* Algorithm-specific argument. */
509
510     /* Where to store the result. */
511     struct mf_subfield dst;
512 };
513
514 /* OFPACT_NOTE.
515  *
516  * Used for NXAST_NOTE. */
517 struct ofpact_note {
518     struct ofpact ofpact;
519     size_t length;
520     uint8_t data[];
521 };
522
523 /* OFPACT_SAMPLE.
524  *
525  * Used for NXAST_SAMPLE. */
526 struct ofpact_sample {
527     struct ofpact ofpact;
528     uint16_t probability;  // Always >0.
529     uint32_t collector_set_id;
530     uint32_t obs_domain_id;
531     uint32_t obs_point_id;
532 };
533
534 /* OFPACT_DEC_TTL.
535  *
536  * Used for OFPAT11_DEC_NW_TTL, NXAST_DEC_TTL and NXAST_DEC_TTL_CNT_IDS. */
537 struct ofpact_cnt_ids {
538     struct ofpact ofpact;
539
540     /* Controller ids. */
541     unsigned int n_controllers;
542     uint16_t cnt_ids[];
543 };
544
545 /* OFPACT_SET_MPLS_LABEL.
546  *
547  * Used for OFPAT11_SET_MPLS_LABEL and NXAST_SET_MPLS_LABEL */
548 struct ofpact_mpls_label {
549     struct ofpact ofpact;
550
551     ovs_be32 label;
552 };
553
554 /* OFPACT_SET_MPLS_TC.
555  *
556  * Used for OFPAT11_SET_MPLS_TC and NXAST_SET_MPLS_TC */
557 struct ofpact_mpls_tc {
558     struct ofpact ofpact;
559
560     uint8_t tc;
561 };
562
563 /* OFPACT_SET_MPLS_TTL.
564  *
565  * Used for OFPAT11_SET_MPLS_TTL and NXAST_SET_MPLS_TTL */
566 struct ofpact_mpls_ttl {
567     struct ofpact ofpact;
568
569     uint8_t ttl;
570 };
571
572 /* OFPACT_GOTO_TABLE
573  *
574  * Used for OFPIT11_GOTO_TABLE */
575 struct ofpact_goto_table {
576     struct ofpact ofpact;
577     uint8_t table_id;
578 };
579
580 /* OFPACT_GROUP.
581  *
582  * Used for OFPAT11_GROUP. */
583 struct ofpact_group {
584     struct ofpact ofpact;
585     uint32_t group_id;
586 };
587
588 /* Converting OpenFlow to ofpacts. */
589 enum ofperr ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
590                                           unsigned int actions_len,
591                                           enum ofp_version version,
592                                           struct ofpbuf *ofpacts);
593 enum ofperr ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
594                                                unsigned int instructions_len,
595                                                enum ofp_version version,
596                                                struct ofpbuf *ofpacts);
597 enum ofperr ofpacts_check(struct ofpact[], size_t ofpacts_len,
598                           struct flow *, ofp_port_t max_ports,
599                           uint8_t table_id, uint8_t n_tables,
600                           enum ofputil_protocol *usable_protocols);
601 enum ofperr ofpacts_check_consistency(struct ofpact[], size_t ofpacts_len,
602                                       struct flow *, ofp_port_t max_ports,
603                                       uint8_t table_id, uint8_t n_tables,
604                                       enum ofputil_protocol usable_protocols);
605 enum ofperr ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
606                            uint32_t allowed_ovsinsts);
607 enum ofperr ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports);
608
609 /* Converting ofpacts to OpenFlow. */
610 size_t ofpacts_put_openflow_actions(const struct ofpact[], size_t ofpacts_len,
611                                     struct ofpbuf *openflow, enum ofp_version);
612 void ofpacts_put_openflow_instructions(const struct ofpact[],
613                                        size_t ofpacts_len,
614                                        struct ofpbuf *openflow,
615                                        enum ofp_version ofp_version);
616
617 /* Sets of supported actions. */
618 ovs_be32 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version);
619 uint64_t ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version);
620 void ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *);
621
622 /* Working with ofpacts. */
623 bool ofpacts_output_to_port(const struct ofpact[], size_t ofpacts_len,
624                             ofp_port_t port);
625 bool ofpacts_output_to_group(const struct ofpact[], size_t ofpacts_len,
626                              uint32_t group_id);
627 bool ofpacts_equal(const struct ofpact a[], size_t a_len,
628                    const struct ofpact b[], size_t b_len);
629 uint32_t ofpacts_get_meter(const struct ofpact[], size_t ofpacts_len);
630
631 /* Formatting ofpacts.
632  *
633  * (For parsing ofpacts, see ofp-parse.h.) */
634 void ofpacts_format(const struct ofpact[], size_t ofpacts_len, struct ds *);
635 const char *ofpact_name(enum ofpact_type);
636
637 /* Internal use by the helpers below. */
638 void ofpact_init(struct ofpact *, enum ofpact_type, size_t len);
639 void *ofpact_put(struct ofpbuf *, enum ofpact_type, size_t len);
640
641 /* For each OFPACT_<ENUM> with a corresponding struct <STRUCT>, this defines
642  * the following commonly useful functions:
643  *
644  *   struct <STRUCT> *ofpact_put_<ENUM>(struct ofpbuf *ofpacts);
645  *
646  *     Appends a new 'ofpact', of length OFPACT_<ENUM>_RAW_SIZE, to 'ofpacts',
647  *     initializes it with ofpact_init_<ENUM>(), and returns it.  Also sets
648  *     'ofpacts->l2' to the returned action.
649  *
650  *     After using this function to add a variable-length action, add the
651  *     elements of the flexible array (e.g. with ofpbuf_put()), then use
652  *     ofpact_update_len() to update the length embedded into the action.
653  *     (Keep in mind the need to refresh the structure from 'ofpacts->frame'
654  *     after adding data to 'ofpacts'.)
655  *
656  *   struct <STRUCT> *ofpact_get_<ENUM>(const struct ofpact *ofpact);
657  *
658  *     Returns 'ofpact' cast to "struct <STRUCT> *".  'ofpact->type' must be
659  *     OFPACT_<ENUM>.
660  *
661  * as well as the following more rarely useful definitions:
662  *
663  *   void ofpact_init_<ENUM>(struct <STRUCT> *ofpact);
664  *
665  *     Initializes the parts of 'ofpact' that identify it as having type
666  *     OFPACT_<ENUM> and length OFPACT_<ENUM>_RAW_SIZE and zeros the rest.
667  *
668  *   <ENUM>_RAW_SIZE
669  *
670  *     The size of the action structure.  For a fixed-length action, this is
671  *     sizeof(struct <STRUCT>).  For a variable-length action, this is the
672  *     offset to the variable-length part.
673  *
674  *   <ENUM>_SIZE
675  *
676  *     An integer constant, the value of OFPACT_<ENUM>_RAW_SIZE rounded up to a
677  *     multiple of OFPACT_ALIGNTO.
678  */
679 #define OFPACT(ENUM, STRUCT, MEMBER, NAME)                              \
680     BUILD_ASSERT_DECL(offsetof(struct STRUCT, ofpact) == 0);            \
681                                                                         \
682     enum { OFPACT_##ENUM##_RAW_SIZE                                     \
683            = (offsetof(struct STRUCT, MEMBER)                           \
684               ? offsetof(struct STRUCT, MEMBER)                         \
685               : sizeof(struct STRUCT)) };                               \
686                                                                         \
687     enum { OFPACT_##ENUM##_SIZE                                         \
688            = ROUND_UP(OFPACT_##ENUM##_RAW_SIZE, OFPACT_ALIGNTO) };      \
689                                                                         \
690     static inline struct STRUCT *                                       \
691     ofpact_get_##ENUM(const struct ofpact *ofpact)                      \
692     {                                                                   \
693         ovs_assert(ofpact->type == OFPACT_##ENUM);                      \
694         return ALIGNED_CAST(struct STRUCT *, ofpact);                   \
695     }                                                                   \
696                                                                         \
697     static inline struct STRUCT *                                       \
698     ofpact_put_##ENUM(struct ofpbuf *ofpacts)                           \
699     {                                                                   \
700         return ofpact_put(ofpacts, OFPACT_##ENUM,                       \
701                           OFPACT_##ENUM##_RAW_SIZE);                    \
702     }                                                                   \
703                                                                         \
704     static inline void                                                  \
705     ofpact_init_##ENUM(struct STRUCT *ofpact)                           \
706     {                                                                   \
707         ofpact_init(&ofpact->ofpact, OFPACT_##ENUM,                     \
708                     OFPACT_##ENUM##_RAW_SIZE);                          \
709     }
710 OFPACTS
711 #undef OFPACT
712
713 /* Functions to use after adding ofpacts to a buffer. */
714 void ofpact_update_len(struct ofpbuf *, struct ofpact *);
715 void ofpact_pad(struct ofpbuf *);
716
717 /* OpenFlow 1.1 instructions.
718  * The order is sorted in execution order. Not in the value of OFPIT11_xxx.
719  * It is enforced on parser from text string.
720  */
721 #define OVS_INSTRUCTIONS                                    \
722     DEFINE_INST(OFPIT13_METER,                              \
723                 ofp13_instruction_meter,          false,    \
724                 "meter")                                    \
725                                                             \
726     DEFINE_INST(OFPIT11_APPLY_ACTIONS,                      \
727                 ofp11_instruction_actions,        true,     \
728                 "apply_actions")                            \
729                                                             \
730     DEFINE_INST(OFPIT11_CLEAR_ACTIONS,                      \
731                 ofp11_instruction,                false,    \
732                 "clear_actions")                            \
733                                                             \
734     DEFINE_INST(OFPIT11_WRITE_ACTIONS,                      \
735                 ofp11_instruction_actions,        true,     \
736                 "write_actions")                            \
737                                                             \
738     DEFINE_INST(OFPIT11_WRITE_METADATA,                     \
739                 ofp11_instruction_write_metadata, false,    \
740                 "write_metadata")                           \
741                                                             \
742     DEFINE_INST(OFPIT11_GOTO_TABLE,                         \
743                 ofp11_instruction_goto_table,     false,    \
744                 "goto_table")
745
746 enum ovs_instruction_type {
747 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) OVSINST_##ENUM,
748     OVS_INSTRUCTIONS
749 #undef DEFINE_INST
750 };
751
752 enum {
753 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
754     N_OVS_INSTRUCTIONS = OVS_INSTRUCTIONS
755 #undef DEFINE_INST
756 };
757
758 const char *ovs_instruction_name_from_type(enum ovs_instruction_type type);
759 int ovs_instruction_type_from_name(const char *name);
760 enum ovs_instruction_type ovs_instruction_type_from_ofpact_type(
761     enum ofpact_type);
762 enum ofperr ovs_instruction_type_from_inst_type(
763     enum ovs_instruction_type *instruction_type, const uint16_t inst_type);
764
765 ovs_be32 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version);
766 uint32_t ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap,
767                                       enum ofp_version);
768
769 #endif /* ofp-actions.h */