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