ofp-util: Move ofputil_check_output_port() to ofp-actions, rename.
[sliver-openvswitch.git] / lib / ofp-actions.h
1 /*
2  * Copyright (c) 2012, 2013 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 DEFINE_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 #define OFPACTS                                                     \
53     /* Output. */                                                   \
54     DEFINE_OFPACT(OUTPUT,          ofpact_output,        ofpact)    \
55     DEFINE_OFPACT(GROUP,           ofpact_group,         ofpact)    \
56     DEFINE_OFPACT(CONTROLLER,      ofpact_controller,    ofpact)    \
57     DEFINE_OFPACT(ENQUEUE,         ofpact_enqueue,       ofpact)    \
58     DEFINE_OFPACT(OUTPUT_REG,      ofpact_output_reg,    ofpact)    \
59     DEFINE_OFPACT(BUNDLE,          ofpact_bundle,        slaves)    \
60                                                                     \
61     /* Header changes. */                                           \
62     DEFINE_OFPACT(SET_FIELD,       ofpact_set_field,     ofpact)    \
63     DEFINE_OFPACT(SET_VLAN_VID,    ofpact_vlan_vid,      ofpact)    \
64     DEFINE_OFPACT(SET_VLAN_PCP,    ofpact_vlan_pcp,      ofpact)    \
65     DEFINE_OFPACT(STRIP_VLAN,      ofpact_null,          ofpact)    \
66     DEFINE_OFPACT(PUSH_VLAN,       ofpact_null,          ofpact)    \
67     DEFINE_OFPACT(SET_ETH_SRC,     ofpact_mac,           ofpact)    \
68     DEFINE_OFPACT(SET_ETH_DST,     ofpact_mac,           ofpact)    \
69     DEFINE_OFPACT(SET_IPV4_SRC,    ofpact_ipv4,          ofpact)    \
70     DEFINE_OFPACT(SET_IPV4_DST,    ofpact_ipv4,          ofpact)    \
71     DEFINE_OFPACT(SET_IP_DSCP,     ofpact_dscp,          ofpact)    \
72     DEFINE_OFPACT(SET_IP_ECN,      ofpact_ecn,           ofpact)    \
73     DEFINE_OFPACT(SET_IP_TTL,      ofpact_ip_ttl,        ofpact)    \
74     DEFINE_OFPACT(SET_L4_SRC_PORT, ofpact_l4_port,       ofpact)    \
75     DEFINE_OFPACT(SET_L4_DST_PORT, ofpact_l4_port,       ofpact)    \
76     DEFINE_OFPACT(REG_MOVE,        ofpact_reg_move,      ofpact)    \
77     DEFINE_OFPACT(REG_LOAD,        ofpact_reg_load,      ofpact)    \
78     DEFINE_OFPACT(STACK_PUSH,      ofpact_stack,         ofpact)    \
79     DEFINE_OFPACT(STACK_POP,       ofpact_stack,         ofpact)    \
80     DEFINE_OFPACT(DEC_TTL,         ofpact_cnt_ids,       cnt_ids)   \
81     DEFINE_OFPACT(SET_MPLS_TTL,    ofpact_mpls_ttl,      ofpact)    \
82     DEFINE_OFPACT(DEC_MPLS_TTL,    ofpact_null,          ofpact)    \
83     DEFINE_OFPACT(PUSH_MPLS,       ofpact_push_mpls,     ofpact)    \
84     DEFINE_OFPACT(POP_MPLS,        ofpact_pop_mpls,      ofpact)    \
85                                                                     \
86     /* Metadata. */                                                 \
87     DEFINE_OFPACT(SET_TUNNEL,      ofpact_tunnel,        ofpact)    \
88     DEFINE_OFPACT(SET_QUEUE,       ofpact_queue,         ofpact)    \
89     DEFINE_OFPACT(POP_QUEUE,       ofpact_null,          ofpact)    \
90     DEFINE_OFPACT(FIN_TIMEOUT,     ofpact_fin_timeout,   ofpact)    \
91                                                                     \
92     /* Flow table interaction. */                                   \
93     DEFINE_OFPACT(RESUBMIT,        ofpact_resubmit,      ofpact)    \
94     DEFINE_OFPACT(LEARN,           ofpact_learn,         specs)     \
95                                                                     \
96     /* Arithmetic. */                                               \
97     DEFINE_OFPACT(MULTIPATH,       ofpact_multipath,     ofpact)    \
98                                                                     \
99     /* Other. */                                                    \
100     DEFINE_OFPACT(NOTE,            ofpact_note,          data)      \
101     DEFINE_OFPACT(EXIT,            ofpact_null,          ofpact)    \
102     DEFINE_OFPACT(SAMPLE,          ofpact_sample,        ofpact)    \
103                                                                     \
104     /* Instructions */                                              \
105     DEFINE_OFPACT(METER,           ofpact_meter,         ofpact)    \
106     DEFINE_OFPACT(CLEAR_ACTIONS,   ofpact_null,          ofpact)    \
107     DEFINE_OFPACT(WRITE_ACTIONS,   ofpact_nest,          ofpact)    \
108     DEFINE_OFPACT(WRITE_METADATA,  ofpact_metadata,      ofpact)    \
109     DEFINE_OFPACT(GOTO_TABLE,      ofpact_goto_table,    ofpact)
110
111 /* enum ofpact_type, with a member OFPACT_<ENUM> for each action. */
112 enum OVS_PACKED_ENUM ofpact_type {
113 #define DEFINE_OFPACT(ENUM, STRUCT, MEMBER) OFPACT_##ENUM,
114     OFPACTS
115 #undef DEFINE_OFPACT
116 };
117
118 /* N_OFPACTS, the number of values of "enum ofpact_type". */
119 enum {
120     N_OFPACTS =
121 #define DEFINE_OFPACT(ENUM, STRUCT, MEMBER) + 1
122     OFPACTS
123 #undef DEFINE_OFPACT
124 };
125
126 /* Header for an action.
127  *
128  * Each action is a structure "struct ofpact_*" that begins with "struct
129  * ofpact", usually followed by other data that describes the action.  Actions
130  * are padded out to a multiple of OFPACT_ALIGNTO bytes in length.
131  *
132  * The 'compat' member is special:
133  *
134  *     - Most "struct ofpact"s correspond to one particular kind of OpenFlow
135  *       action, at least in a given OpenFlow version.  For example,
136  *       OFPACT_SET_VLAN_VID corresponds to OFPAT10_SET_VLAN_VID in OpenFlow
137  *       1.0.
138  *
139  *       For such actions, the 'compat' member is not meaningful and generally
140  *       should be zero.
141  *
142  *     - A few "struct ofpact"s correspond to multiple OpenFlow actions.  For
143  *       example, OFPACT_SET_TUNNEL can be NXAST_SET_TUNNEL or
144  *       NXAST_SET_TUNNEL64.  In these cases, if the "struct ofpact" originated
145  *       from OpenFlow, then we want to make sure that, if it gets translated
146  *       back to OpenFlow later, it is translated back to the same action type.
147  *       (Otherwise, we'd violate the promise made in DESIGN, in the "Action
148  *       Reproduction" section.)
149  *
150  *       For such actions, the 'compat' member should be the original action
151  *       type.  (If the action didn't originate from OpenFlow, then setting
152  *       'compat' to zero should be fine: code to translate the ofpact to
153  *       OpenFlow must tolerate this case.)
154  */
155 struct ofpact {
156     enum ofpact_type type;      /* OFPACT_*. */
157     enum ofputil_action_code compat; /* Original type when added, if any. */
158     uint16_t len;               /* Length of the action, in bytes, including
159                                  * struct ofpact, excluding padding. */
160 };
161
162 #ifdef __GNUC__
163 /* Make sure that OVS_PACKED_ENUM really worked. */
164 BUILD_ASSERT_DECL(sizeof(struct ofpact) == 4);
165 #endif
166
167 /* Alignment. */
168 #define OFPACT_ALIGNTO 8
169 #define OFPACT_ALIGN(SIZE) ROUND_UP(SIZE, OFPACT_ALIGNTO)
170
171 static inline struct ofpact *
172 ofpact_next(const struct ofpact *ofpact)
173 {
174     return (void *) ((uint8_t *) ofpact + OFPACT_ALIGN(ofpact->len));
175 }
176
177 static inline struct ofpact *
178 ofpact_end(const struct ofpact *ofpacts, size_t ofpacts_len)
179 {
180     return (void *) ((uint8_t *) ofpacts + ofpacts_len);
181 }
182
183 /* Assigns POS to each ofpact, in turn, in the OFPACTS_LEN bytes of ofpacts
184  * starting at OFPACTS. */
185 #define OFPACT_FOR_EACH(POS, OFPACTS, OFPACTS_LEN)                      \
186     for ((POS) = (OFPACTS); (POS) < ofpact_end(OFPACTS, OFPACTS_LEN);  \
187          (POS) = ofpact_next(POS))
188 \f
189 /* Action structure for each OFPACT_*. */
190
191 /* OFPACT_STRIP_VLAN, OFPACT_POP_QUEUE, OFPACT_EXIT, OFPACT_CLEAR_ACTIONS.
192  *
193  * Used for OFPAT10_STRIP_VLAN, NXAST_POP_QUEUE, NXAST_EXIT,
194  * OFPAT11_POP_VLAN, OFPIT11_CLEAR_ACTIONS.
195  *
196  * Action structure for actions that do not have any extra data beyond the
197  * action type. */
198 struct ofpact_null {
199     struct ofpact ofpact;
200 };
201
202 /* OFPACT_OUTPUT.
203  *
204  * Used for OFPAT10_OUTPUT. */
205 struct ofpact_output {
206     struct ofpact ofpact;
207     ofp_port_t port;            /* Output port. */
208     uint16_t max_len;           /* Max send len, for port OFPP_CONTROLLER. */
209 };
210
211 /* OFPACT_CONTROLLER.
212  *
213  * Used for NXAST_CONTROLLER. */
214 struct ofpact_controller {
215     struct ofpact ofpact;
216     uint16_t max_len;           /* Maximum length to send to controller. */
217     uint16_t controller_id;     /* Controller ID to send packet-in. */
218     enum ofp_packet_in_reason reason; /* Reason to put in packet-in. */
219 };
220
221 /* OFPACT_ENQUEUE.
222  *
223  * Used for OFPAT10_ENQUEUE. */
224 struct ofpact_enqueue {
225     struct ofpact ofpact;
226     ofp_port_t port;
227     uint32_t queue;
228 };
229
230 /* OFPACT_OUTPUT_REG.
231  *
232  * Used for NXAST_OUTPUT_REG. */
233 struct ofpact_output_reg {
234     struct ofpact ofpact;
235     struct mf_subfield src;
236     uint16_t max_len;
237 };
238
239 /* OFPACT_BUNDLE.
240  *
241  * Used for NXAST_BUNDLE. */
242 struct ofpact_bundle {
243     struct ofpact ofpact;
244
245     /* Slave choice algorithm to apply to hash value. */
246     enum nx_bd_algorithm algorithm;
247
248     /* What fields to hash and how. */
249     enum nx_hash_fields fields;
250     uint16_t basis;             /* Universal hash parameter. */
251
252     struct mf_subfield dst;
253
254     /* Slaves for output. */
255     unsigned int n_slaves;
256     ofp_port_t slaves[];
257 };
258
259 /* OFPACT_SET_VLAN_VID.
260  *
261  * We keep track if vlan was present at action validation time to avoid a
262  * PUSH_VLAN when translating to OpenFlow 1.1+.
263  *
264  * We also keep the originating OFPUTIL action code in ofpact.compat.
265  *
266  * Used for OFPAT10_SET_VLAN_VID and OFPAT11_SET_VLAN_VID. */
267 struct ofpact_vlan_vid {
268     struct ofpact ofpact;
269     uint16_t vlan_vid;          /* VLAN VID in low 12 bits, 0 in other bits. */
270     bool push_vlan_if_needed;   /* OF 1.0 semantics if true. */
271     bool flow_has_vlan;         /* VLAN present at action validation time? */
272 };
273
274 /* OFPACT_SET_VLAN_PCP.
275  *
276  * We keep track if vlan was present at action validation time to avoid a
277  * PUSH_VLAN when translating to OpenFlow 1.1+.
278  *
279  * We also keep the originating OFPUTIL action code in ofpact.compat.
280  *
281  * Used for OFPAT10_SET_VLAN_PCP and OFPAT11_SET_VLAN_PCP. */
282 struct ofpact_vlan_pcp {
283     struct ofpact ofpact;
284     uint8_t vlan_pcp;           /* VLAN PCP in low 3 bits, 0 in other bits. */
285     bool push_vlan_if_needed;   /* OF 1.0 semantics if true. */
286     bool flow_has_vlan;         /* VLAN present at action validation time? */
287 };
288
289 /* OFPACT_SET_ETH_SRC, OFPACT_SET_ETH_DST.
290  *
291  * Used for OFPAT10_SET_DL_SRC, OFPAT10_SET_DL_DST. */
292 struct ofpact_mac {
293     struct ofpact ofpact;
294     uint8_t mac[ETH_ADDR_LEN];
295 };
296
297 /* OFPACT_SET_IPV4_SRC, OFPACT_SET_IPV4_DST.
298  *
299  * Used for OFPAT10_SET_NW_SRC, OFPAT10_SET_NW_DST. */
300 struct ofpact_ipv4 {
301     struct ofpact ofpact;
302     ovs_be32 ipv4;
303 };
304
305 /* OFPACT_SET_IP_DSCP.
306  *
307  * Used for OFPAT10_SET_NW_TOS. */
308 struct ofpact_dscp {
309     struct ofpact ofpact;
310     uint8_t dscp;               /* DSCP in high 6 bits, rest ignored. */
311 };
312
313 /* OFPACT_SET_IP_ECN.
314  *
315  * Used for OFPAT11_SET_NW_ECN. */
316 struct ofpact_ecn {
317     struct ofpact ofpact;
318     uint8_t ecn;               /* ECN in low 2 bits, rest ignored. */
319 };
320
321 /* OFPACT_SET_IP_TTL.
322  *
323  * Used for OFPAT11_SET_NW_TTL. */
324 struct ofpact_ip_ttl {
325     struct ofpact ofpact;
326     uint8_t ttl;
327 };
328
329 /* OFPACT_SET_L4_SRC_PORT, OFPACT_SET_L4_DST_PORT.
330  *
331  * Used for OFPAT10_SET_TP_SRC, OFPAT10_SET_TP_DST. */
332 struct ofpact_l4_port {
333     struct ofpact ofpact;
334     uint16_t port;              /* TCP, UDP or SCTP port number. */
335     uint8_t  flow_ip_proto;     /* IP proto from corresponding match, or 0 */
336 };
337
338 /* OFPACT_REG_MOVE.
339  *
340  * Used for NXAST_REG_MOVE. */
341 struct ofpact_reg_move {
342     struct ofpact ofpact;
343     struct mf_subfield src;
344     struct mf_subfield dst;
345 };
346
347 /* OFPACT_STACK_PUSH.
348  *
349  * Used for NXAST_STACK_PUSH and NXAST_STACK_POP. */
350 struct ofpact_stack {
351     struct ofpact ofpact;
352     struct mf_subfield subfield;
353 };
354
355 /* OFPACT_REG_LOAD.
356  *
357  * Used for NXAST_REG_LOAD. */
358 struct ofpact_reg_load {
359     struct ofpact ofpact;
360     struct mf_subfield dst;
361     union mf_subvalue subvalue; /* Least-significant bits are used. */
362 };
363
364 /* The position in the packet at which to insert an MPLS header.
365  *
366  * Used NXAST_PUSH_MPLS, OFPAT11_PUSH_MPLS. */
367 enum ofpact_mpls_position {
368     /* Add the MPLS LSE after the Ethernet header but before any VLAN tags.
369      * OpenFlow 1.3+ requires this behavior. */
370    OFPACT_MPLS_BEFORE_VLAN,
371
372    /* Add the MPLS LSE after the Ethernet header and any VLAN tags.
373     * OpenFlow 1.1 and 1.2 require this behavior. */
374    OFPACT_MPLS_AFTER_VLAN
375 };
376
377 /* OFPACT_SET_FIELD.
378  *
379  * Used for OFPAT12_SET_FIELD. */
380 struct ofpact_set_field {
381     struct ofpact ofpact;
382     const struct mf_field *field;
383     bool flow_has_vlan;   /* VLAN present at action validation time. */
384     union mf_value value;
385 };
386
387 /* OFPACT_PUSH_VLAN/MPLS/PBB
388  *
389  * Used for NXAST_PUSH_MPLS, OFPAT11_PUSH_MPLS. */
390 struct ofpact_push_mpls {
391     struct ofpact ofpact;
392     ovs_be16 ethertype;
393     enum ofpact_mpls_position position;
394 };
395
396 /* OFPACT_POP_MPLS
397  *
398  * Used for NXAST_POP_MPLS, OFPAT11_POP_MPLS.. */
399 struct ofpact_pop_mpls {
400     struct ofpact ofpact;
401     ovs_be16 ethertype;
402 };
403
404 /* OFPACT_SET_TUNNEL.
405  *
406  * Used for NXAST_SET_TUNNEL, NXAST_SET_TUNNEL64. */
407 struct ofpact_tunnel {
408     struct ofpact ofpact;
409     uint64_t tun_id;
410 };
411
412 /* OFPACT_SET_QUEUE.
413  *
414  * Used for NXAST_SET_QUEUE. */
415 struct ofpact_queue {
416     struct ofpact ofpact;
417     uint32_t queue_id;
418 };
419
420 /* OFPACT_FIN_TIMEOUT.
421  *
422  * Used for NXAST_FIN_TIMEOUT. */
423 struct ofpact_fin_timeout {
424     struct ofpact ofpact;
425     uint16_t fin_idle_timeout;
426     uint16_t fin_hard_timeout;
427 };
428
429 /* OFPACT_WRITE_METADATA.
430  *
431  * Used for NXAST_WRITE_METADATA. */
432 struct ofpact_metadata {
433     struct ofpact ofpact;
434     ovs_be64 metadata;
435     ovs_be64 mask;
436 };
437
438 /* OFPACT_METER.
439  *
440  * Used for OFPIT13_METER. */
441 struct ofpact_meter {
442     struct ofpact ofpact;
443     uint32_t meter_id;
444 };
445
446 /* OFPACT_WRITE_ACTIONS.
447  *
448  * Used for OFPIT11_WRITE_ACTIONS. */
449 struct ofpact_nest {
450     struct ofpact ofpact;
451     uint8_t pad[OFPACT_ALIGN(sizeof(struct ofpact)) - sizeof(struct ofpact)];
452     struct ofpact actions[];
453 };
454 BUILD_ASSERT_DECL(offsetof(struct ofpact_nest, actions) == OFPACT_ALIGNTO);
455
456 static inline size_t
457 ofpact_nest_get_action_len(const struct ofpact_nest *on)
458 {
459     return on->ofpact.len - offsetof(struct ofpact_nest, actions);
460 }
461
462 void ofpacts_execute_action_set(struct ofpbuf *action_list,
463                                 const struct ofpbuf *action_set);
464
465 /* OFPACT_RESUBMIT.
466  *
467  * Used for NXAST_RESUBMIT, NXAST_RESUBMIT_TABLE. */
468 struct ofpact_resubmit {
469     struct ofpact ofpact;
470     ofp_port_t in_port;
471     uint8_t table_id;
472 };
473
474 /* Part of struct ofpact_learn, below. */
475 struct ofpact_learn_spec {
476     int n_bits;                 /* Number of bits in source and dest. */
477
478     int src_type;               /* One of NX_LEARN_SRC_*. */
479     struct mf_subfield src;     /* NX_LEARN_SRC_FIELD only. */
480     union mf_subvalue src_imm;  /* NX_LEARN_SRC_IMMEDIATE only. */
481
482     int dst_type;             /* One of NX_LEARN_DST_*. */
483     struct mf_subfield dst;   /* NX_LEARN_DST_MATCH, NX_LEARN_DST_LOAD only. */
484 };
485
486 /* OFPACT_LEARN.
487  *
488  * Used for NXAST_LEARN. */
489 struct ofpact_learn {
490     struct ofpact ofpact;
491
492     uint16_t idle_timeout;      /* Idle time before discarding (seconds). */
493     uint16_t hard_timeout;      /* Max time before discarding (seconds). */
494     uint16_t priority;          /* Priority level of flow entry. */
495     uint64_t cookie;            /* Cookie for new flow. */
496     enum ofputil_flow_mod_flags flags;
497     uint8_t table_id;           /* Table to insert flow entry. */
498     uint16_t fin_idle_timeout;  /* Idle timeout after FIN, if nonzero. */
499     uint16_t fin_hard_timeout;  /* Hard timeout after FIN, if nonzero. */
500
501     unsigned int n_specs;
502     struct ofpact_learn_spec specs[];
503 };
504
505 /* OFPACT_MULTIPATH.
506  *
507  * Used for NXAST_MULTIPATH. */
508 struct ofpact_multipath {
509     struct ofpact ofpact;
510
511     /* What fields to hash and how. */
512     enum nx_hash_fields fields;
513     uint16_t basis;             /* Universal hash parameter. */
514
515     /* Multipath link choice algorithm to apply to hash value. */
516     enum nx_mp_algorithm algorithm;
517     uint16_t max_link;          /* Number of output links, minus 1. */
518     uint32_t arg;               /* Algorithm-specific argument. */
519
520     /* Where to store the result. */
521     struct mf_subfield dst;
522 };
523
524 /* OFPACT_NOTE.
525  *
526  * Used for NXAST_NOTE. */
527 struct ofpact_note {
528     struct ofpact ofpact;
529     size_t length;
530     uint8_t data[];
531 };
532
533 /* OFPACT_SAMPLE.
534  *
535  * Used for NXAST_SAMPLE. */
536 struct ofpact_sample {
537     struct ofpact ofpact;
538     uint16_t probability;  // Always >0.
539     uint32_t collector_set_id;
540     uint32_t obs_domain_id;
541     uint32_t obs_point_id;
542 };
543
544 /* OFPACT_DEC_TTL.
545  *
546  * Used for OFPAT11_DEC_NW_TTL, NXAST_DEC_TTL and NXAST_DEC_TTL_CNT_IDS. */
547 struct ofpact_cnt_ids {
548     struct ofpact ofpact;
549
550     /* Controller ids. */
551     unsigned int n_controllers;
552     uint16_t cnt_ids[];
553 };
554
555 /* OFPACT_SET_MPLS_TTL.
556  *
557  * Used for NXAST_SET_MPLS_TTL */
558 struct ofpact_mpls_ttl {
559     struct ofpact ofpact;
560
561     uint8_t ttl;
562 };
563
564 /* OFPACT_GOTO_TABLE
565  *
566  * Used for OFPIT11_GOTO_TABLE */
567 struct ofpact_goto_table {
568     struct ofpact ofpact;
569     uint8_t table_id;
570 };
571
572 /* OFPACT_GROUP.
573  *
574  * Used for OFPAT11_GROUP. */
575 struct ofpact_group {
576     struct ofpact ofpact;
577     uint32_t group_id;
578 };
579
580 /* Converting OpenFlow to ofpacts. */
581 enum ofperr ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
582                                           unsigned int actions_len,
583                                           enum ofp_version version,
584                                           struct ofpbuf *ofpacts);
585 enum ofperr ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
586                                                unsigned int instructions_len,
587                                                enum ofp_version version,
588                                                struct ofpbuf *ofpacts);
589 enum ofperr ofpacts_check(struct ofpact[], size_t ofpacts_len,
590                           struct flow *, ofp_port_t max_ports,
591                           uint8_t table_id, bool enforce_consistency);
592 enum ofperr ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len);
593 enum ofperr ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports);
594
595 /* Converting ofpacts to OpenFlow. */
596 size_t ofpacts_put_openflow_actions(const struct ofpact[], size_t ofpacts_len,
597                                     struct ofpbuf *openflow, enum ofp_version);
598 void ofpacts_put_openflow_instructions(const struct ofpact[],
599                                        size_t ofpacts_len,
600                                        struct ofpbuf *openflow,
601                                        enum ofp_version ofp_version);
602
603 /* Working with ofpacts. */
604 bool ofpacts_output_to_port(const struct ofpact[], size_t ofpacts_len,
605                             ofp_port_t port);
606 bool ofpacts_output_to_group(const struct ofpact[], size_t ofpacts_len,
607                              uint32_t group_id);
608 bool ofpacts_equal(const struct ofpact a[], size_t a_len,
609                    const struct ofpact b[], size_t b_len);
610 uint32_t ofpacts_get_meter(const struct ofpact[], size_t ofpacts_len);
611
612 /* Formatting ofpacts.
613  *
614  * (For parsing ofpacts, see ofp-parse.h.) */
615 void ofpacts_format(const struct ofpact[], size_t ofpacts_len, struct ds *);
616
617 /* Internal use by the helpers below. */
618 void ofpact_init(struct ofpact *, enum ofpact_type, size_t len);
619 void *ofpact_put(struct ofpbuf *, enum ofpact_type, size_t len);
620
621 /* For each OFPACT_<ENUM> with a corresponding struct <STRUCT>, this defines
622  * the following commonly useful functions:
623  *
624  *   struct <STRUCT> *ofpact_put_<ENUM>(struct ofpbuf *ofpacts);
625  *
626  *     Appends a new 'ofpact', of length OFPACT_<ENUM>_RAW_SIZE, to 'ofpacts',
627  *     initializes it with ofpact_init_<ENUM>(), and returns it.  Also sets
628  *     'ofpacts->l2' to the returned action.
629  *
630  *     After using this function to add a variable-length action, add the
631  *     elements of the flexible array (e.g. with ofpbuf_put()), then use
632  *     ofpact_update_len() to update the length embedded into the action.
633  *     (Keep in mind the need to refresh the structure from 'ofpacts->l2' after
634  *     adding data to 'ofpacts'.)
635  *
636  *   struct <STRUCT> *ofpact_get_<ENUM>(const struct ofpact *ofpact);
637  *
638  *     Returns 'ofpact' cast to "struct <STRUCT> *".  'ofpact->type' must be
639  *     OFPACT_<ENUM>.
640  *
641  * as well as the following more rarely useful definitions:
642  *
643  *   void ofpact_init_<ENUM>(struct <STRUCT> *ofpact);
644  *
645  *     Initializes the parts of 'ofpact' that identify it as having type
646  *     OFPACT_<ENUM> and length OFPACT_<ENUM>_RAW_SIZE and zeros the rest.
647  *
648  *   <ENUM>_RAW_SIZE
649  *
650  *     The size of the action structure.  For a fixed-length action, this is
651  *     sizeof(struct <STRUCT>).  For a variable-length action, this is the
652  *     offset to the variable-length part.
653  *
654  *   <ENUM>_SIZE
655  *
656  *     An integer constant, the value of OFPACT_<ENUM>_RAW_SIZE rounded up to a
657  *     multiple of OFPACT_ALIGNTO.
658  */
659 #define DEFINE_OFPACT(ENUM, STRUCT, MEMBER)                             \
660     BUILD_ASSERT_DECL(offsetof(struct STRUCT, ofpact) == 0);            \
661                                                                         \
662     enum { OFPACT_##ENUM##_RAW_SIZE                                     \
663            = (offsetof(struct STRUCT, MEMBER)                           \
664               ? offsetof(struct STRUCT, MEMBER)                         \
665               : sizeof(struct STRUCT)) };                               \
666                                                                         \
667     enum { OFPACT_##ENUM##_SIZE                                         \
668            = ROUND_UP(OFPACT_##ENUM##_RAW_SIZE, OFPACT_ALIGNTO) };      \
669                                                                         \
670     static inline struct STRUCT *                                       \
671     ofpact_get_##ENUM(const struct ofpact *ofpact)                      \
672     {                                                                   \
673         ovs_assert(ofpact->type == OFPACT_##ENUM);                      \
674         return ALIGNED_CAST(struct STRUCT *, ofpact);                   \
675     }                                                                   \
676                                                                         \
677     static inline struct STRUCT *                                       \
678     ofpact_put_##ENUM(struct ofpbuf *ofpacts)                           \
679     {                                                                   \
680         return ofpact_put(ofpacts, OFPACT_##ENUM,                       \
681                           OFPACT_##ENUM##_RAW_SIZE);                    \
682     }                                                                   \
683                                                                         \
684     static inline void                                                  \
685     ofpact_init_##ENUM(struct STRUCT *ofpact)                           \
686     {                                                                   \
687         ofpact_init(&ofpact->ofpact, OFPACT_##ENUM,                     \
688                     OFPACT_##ENUM##_RAW_SIZE);                          \
689     }
690 OFPACTS
691 #undef DEFINE_OFPACT
692
693 /* Functions to use after adding ofpacts to a buffer. */
694 void ofpact_update_len(struct ofpbuf *, struct ofpact *);
695 void ofpact_pad(struct ofpbuf *);
696
697 /* OpenFlow 1.1 instructions.
698  * The order is sorted in execution order. Not in the value of OFPIT11_xxx.
699  * It is enforced on parser from text string.
700  */
701 #define OVS_INSTRUCTIONS                                    \
702     DEFINE_INST(OFPIT13_METER,                              \
703                 ofp13_instruction_meter,          false,    \
704                 "meter")                                    \
705                                                             \
706     DEFINE_INST(OFPIT11_APPLY_ACTIONS,                      \
707                 ofp11_instruction_actions,        true,     \
708                 "apply_actions")                            \
709                                                             \
710     DEFINE_INST(OFPIT11_CLEAR_ACTIONS,                      \
711                 ofp11_instruction,                false,    \
712                 "clear_actions")                            \
713                                                             \
714     DEFINE_INST(OFPIT11_WRITE_ACTIONS,                      \
715                 ofp11_instruction_actions,        true,     \
716                 "write_actions")                            \
717                                                             \
718     DEFINE_INST(OFPIT11_WRITE_METADATA,                     \
719                 ofp11_instruction_write_metadata, false,    \
720                 "write_metadata")                           \
721                                                             \
722     DEFINE_INST(OFPIT11_GOTO_TABLE,                         \
723                 ofp11_instruction_goto_table,     false,    \
724                 "goto_table")
725
726 enum ovs_instruction_type {
727 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) OVSINST_##ENUM,
728     OVS_INSTRUCTIONS
729 #undef DEFINE_INST
730 };
731
732 enum {
733 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
734     N_OVS_INSTRUCTIONS = OVS_INSTRUCTIONS
735 #undef DEFINE_INST
736 };
737
738 const char *ovs_instruction_name_from_type(enum ovs_instruction_type type);
739 int ovs_instruction_type_from_name(const char *name);
740 enum ovs_instruction_type ovs_instruction_type_from_ofpact_type(
741     enum ofpact_type);
742 #endif /* ofp-actions.h */