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