]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ofp-util.h
ofp-print: Print OpenFlow version number of message, unless it's 1.0.
[mirror_ovs.git] / lib / ofp-util.h
CommitLineData
fa37b408 1/*
f27f2134 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
fa37b408
BP
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_UTIL_H
18#define OFP_UTIL_H 1
19
20#include <assert.h>
c1c9c9c4 21#include <stdbool.h>
fa37b408
BP
22#include <stddef.h>
23#include <stdint.h>
d1e2cf21 24#include "classifier.h"
fa37b408 25#include "flow.h"
492f7572 26#include "openflow/nicira-ext.h"
44381c1b 27#include "openvswitch/types.h"
fa37b408 28
d8ae4d67 29struct cls_rule;
fa37b408 30struct ofpbuf;
fa37b408 31
d1e2cf21
BP
32/* Basic decoding and length validation of OpenFlow messages. */
33enum ofputil_msg_code {
8f93e93c 34 OFPUTIL_MSG_INVALID,
d1e2cf21
BP
35
36 /* OFPT_* messages. */
37 OFPUTIL_OFPT_HELLO,
38 OFPUTIL_OFPT_ERROR,
39 OFPUTIL_OFPT_ECHO_REQUEST,
40 OFPUTIL_OFPT_ECHO_REPLY,
41 OFPUTIL_OFPT_FEATURES_REQUEST,
42 OFPUTIL_OFPT_FEATURES_REPLY,
43 OFPUTIL_OFPT_GET_CONFIG_REQUEST,
44 OFPUTIL_OFPT_GET_CONFIG_REPLY,
45 OFPUTIL_OFPT_SET_CONFIG,
46 OFPUTIL_OFPT_PACKET_IN,
47 OFPUTIL_OFPT_FLOW_REMOVED,
48 OFPUTIL_OFPT_PORT_STATUS,
49 OFPUTIL_OFPT_PACKET_OUT,
50 OFPUTIL_OFPT_FLOW_MOD,
51 OFPUTIL_OFPT_PORT_MOD,
52 OFPUTIL_OFPT_BARRIER_REQUEST,
53 OFPUTIL_OFPT_BARRIER_REPLY,
54 OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST,
55 OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY,
56
57 /* OFPST_* stat requests. */
58 OFPUTIL_OFPST_DESC_REQUEST,
59 OFPUTIL_OFPST_FLOW_REQUEST,
60 OFPUTIL_OFPST_AGGREGATE_REQUEST,
61 OFPUTIL_OFPST_TABLE_REQUEST,
62 OFPUTIL_OFPST_PORT_REQUEST,
63 OFPUTIL_OFPST_QUEUE_REQUEST,
64
65 /* OFPST_* stat replies. */
66 OFPUTIL_OFPST_DESC_REPLY,
67 OFPUTIL_OFPST_FLOW_REPLY,
68 OFPUTIL_OFPST_QUEUE_REPLY,
69 OFPUTIL_OFPST_PORT_REPLY,
70 OFPUTIL_OFPST_TABLE_REPLY,
71 OFPUTIL_OFPST_AGGREGATE_REPLY,
72
73 /* NXT_* messages. */
d1e2cf21
BP
74 OFPUTIL_NXT_ROLE_REQUEST,
75 OFPUTIL_NXT_ROLE_REPLY,
76 OFPUTIL_NXT_SET_FLOW_FORMAT,
6c1491fb 77 OFPUTIL_NXT_FLOW_MOD_TABLE_ID,
d1e2cf21
BP
78 OFPUTIL_NXT_FLOW_MOD,
79 OFPUTIL_NXT_FLOW_REMOVED,
54834960
EJ
80 OFPUTIL_NXT_SET_PACKET_IN_FORMAT,
81 OFPUTIL_NXT_PACKET_IN,
f27f2134 82 OFPUTIL_NXT_FLOW_AGE,
80d5aefd 83 OFPUTIL_NXT_SET_ASYNC_CONFIG,
a7349929 84 OFPUTIL_NXT_SET_CONTROLLER_ID,
d1e2cf21
BP
85
86 /* NXST_* stat requests. */
87 OFPUTIL_NXST_FLOW_REQUEST,
88 OFPUTIL_NXST_AGGREGATE_REQUEST,
89
90 /* NXST_* stat replies. */
91 OFPUTIL_NXST_FLOW_REPLY,
92 OFPUTIL_NXST_AGGREGATE_REPLY
93};
94
95struct ofputil_msg_type;
90bf1e07 96enum ofperr ofputil_decode_msg_type(const struct ofp_header *,
5a020ef3 97 const struct ofputil_msg_type **);
90bf1e07
BP
98enum ofperr ofputil_decode_msg_type_partial(const struct ofp_header *,
99 size_t length,
100 const struct ofputil_msg_type **);
d1e2cf21
BP
101enum ofputil_msg_code ofputil_msg_type_code(const struct ofputil_msg_type *);
102const char *ofputil_msg_type_name(const struct ofputil_msg_type *);
39dc9082
BP
103
104/* Port numbers. */
90bf1e07 105enum ofperr ofputil_check_output_port(uint16_t ofp_port, int max_ports);
39dc9082
BP
106bool ofputil_port_from_string(const char *, uint16_t *port);
107void ofputil_format_port(uint16_t port, struct ds *);
d1e2cf21 108
0596e897
BP
109/* Converting OFPFW_NW_SRC_MASK and OFPFW_NW_DST_MASK wildcard bit counts to
110 * and from IP bitmasks. */
111ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
112int ofputil_netmask_to_wcbits(ovs_be32 netmask);
113
27527aa0
BP
114/* Protocols.
115 *
116 * These are arranged from most portable to least portable, or alternatively
117 * from least powerful to most powerful. Formats earlier on the list are more
118 * likely to be understood for the purpose of making requests, but formats
119 * later on the list are more likely to accurately describe a flow within a
120 * switch.
121 *
122 * On any given OpenFlow connection, a single protocol is in effect at any
123 * given time. These values use separate bits only because that makes it easy
124 * to test whether a particular protocol is within a given set of protocols and
125 * to implement set union and intersection.
126 */
127enum ofputil_protocol {
128 /* OpenFlow 1.0-based protocols. */
129 OFPUTIL_P_OF10 = 1 << 0, /* OpenFlow 1.0 flow format. */
130 OFPUTIL_P_OF10_TID = 1 << 1, /* OF1.0 + flow_mod_table_id extension. */
131#define OFPUTIL_P_OF10_ANY (OFPUTIL_P_OF10 | OFPUTIL_P_OF10_TID)
132
133 /* OpenFlow 1.0 with NXM-based flow formats. */
134 OFPUTIL_P_NXM = 1 << 2, /* Nicira extended match. */
135 OFPUTIL_P_NXM_TID = 1 << 3, /* NXM + flow_mod_table_id extension. */
136#define OFPUTIL_P_NXM_ANY (OFPUTIL_P_NXM | OFPUTIL_P_NXM_TID)
137
138 /* All protocols. */
139#define OFPUTIL_P_ANY (OFPUTIL_P_OF10_ANY | OFPUTIL_P_NXM_ANY)
140
141 /* Protocols in which a specific table may be specified in flow_mods. */
142#define OFPUTIL_P_TID (OFPUTIL_P_OF10_TID | OFPUTIL_P_NXM_TID)
143};
144
145/* Protocols to use for flow dumps, from most to least preferred. */
146extern enum ofputil_protocol ofputil_flow_dump_protocols[];
147extern size_t ofputil_n_flow_dump_protocols;
148
149enum ofputil_protocol ofputil_protocol_from_ofp_version(int version);
150bool ofputil_protocol_is_valid(enum ofputil_protocol);
151enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
152 bool enable);
153enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
154enum ofputil_protocol ofputil_protocol_set_base(
155 enum ofputil_protocol cur, enum ofputil_protocol new_base);
156
157const char *ofputil_protocol_to_string(enum ofputil_protocol);
158char *ofputil_protocols_to_string(enum ofputil_protocol);
159enum ofputil_protocol ofputil_protocols_from_string(const char *);
160enum ofputil_protocol ofputil_usable_protocols(const struct cls_rule *);
161
162struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
163 enum ofputil_protocol want,
164 enum ofputil_protocol *next);
165
166/* nx_flow_format */
167struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
168enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
169bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
170const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
171
d8ae4d67 172/* Work with OpenFlow 1.0 ofp_match. */
7286b1e1 173void ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *);
d8ae4d67 174void ofputil_cls_rule_from_match(const struct ofp_match *,
b78f6b77 175 unsigned int priority, struct cls_rule *);
27527aa0 176void ofputil_normalize_rule(struct cls_rule *);
b78f6b77 177void ofputil_cls_rule_to_match(const struct cls_rule *, struct ofp_match *);
d8ae4d67 178
36956a7d
BP
179/* dl_type translation between OpenFlow and 'struct flow' format. */
180ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
181ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
182
54834960
EJ
183/* PACKET_IN. */
184bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format);
185int ofputil_packet_in_format_from_string(const char *);
186const char *ofputil_packet_in_format_to_string(enum nx_packet_in_format);
187struct ofpbuf *ofputil_make_set_packet_in_format(enum nx_packet_in_format);
188
6c1491fb
BP
189/* NXT_FLOW_MOD_TABLE_ID extension. */
190struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
191
27527aa0 192/* Protocol-independent flow_mod. */
a9a2da38 193struct ofputil_flow_mod {
2e4f5fcf
BP
194 struct cls_rule cr;
195 ovs_be64 cookie;
e729e793 196 ovs_be64 cookie_mask;
6c1491fb 197 uint8_t table_id;
2e4f5fcf
BP
198 uint16_t command;
199 uint16_t idle_timeout;
200 uint16_t hard_timeout;
201 uint32_t buffer_id;
202 uint16_t out_port;
203 uint16_t flags;
204 union ofp_action *actions;
205 size_t n_actions;
206};
207
90bf1e07
BP
208enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
209 const struct ofp_header *,
27527aa0 210 enum ofputil_protocol);
a9a2da38 211struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
27527aa0
BP
212 enum ofputil_protocol);
213
214enum ofputil_protocol ofputil_flow_mod_usable_protocols(
215 const struct ofputil_flow_mod *fms, size_t n_fms);
2e4f5fcf 216
27527aa0 217/* Flow stats or aggregate stats request, independent of protocol. */
81d1ea94 218struct ofputil_flow_stats_request {
2e4f5fcf
BP
219 bool aggregate; /* Aggregate results? */
220 struct cls_rule match;
e729e793
JP
221 ovs_be64 cookie;
222 ovs_be64 cookie_mask;
2e4f5fcf
BP
223 uint16_t out_port;
224 uint8_t table_id;
225};
226
90bf1e07
BP
227enum ofperr ofputil_decode_flow_stats_request(
228 struct ofputil_flow_stats_request *, const struct ofp_header *);
2e4f5fcf 229struct ofpbuf *ofputil_encode_flow_stats_request(
27527aa0
BP
230 const struct ofputil_flow_stats_request *, enum ofputil_protocol);
231enum ofputil_protocol ofputil_flow_stats_request_usable_protocols(
232 const struct ofputil_flow_stats_request *);
2e4f5fcf 233
27527aa0 234/* Flow stats reply, independent of protocol. */
4ffd1b43
BP
235struct ofputil_flow_stats {
236 struct cls_rule rule;
237 ovs_be64 cookie;
238 uint8_t table_id;
239 uint32_t duration_sec;
240 uint32_t duration_nsec;
241 uint16_t idle_timeout;
242 uint16_t hard_timeout;
f27f2134
BP
243 int idle_age; /* Seconds since last packet, -1 if unknown. */
244 int hard_age; /* Seconds since last change, -1 if unknown. */
5e9d0469
BP
245 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
246 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
4ffd1b43
BP
247 union ofp_action *actions;
248 size_t n_actions;
249};
250
251int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
f27f2134
BP
252 struct ofpbuf *msg,
253 bool flow_age_extension);
349adfb2
BP
254void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
255 struct list *replies);
4ffd1b43 256
27527aa0 257/* Aggregate stats reply, independent of protocol. */
76c93b22 258struct ofputil_aggregate_stats {
5e9d0469
BP
259 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
260 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
76c93b22
BP
261 uint32_t flow_count;
262};
263
264struct ofpbuf *ofputil_encode_aggregate_stats_reply(
265 const struct ofputil_aggregate_stats *stats,
266 const struct ofp_stats_msg *request);
267
27527aa0 268/* Flow removed message, independent of protocol. */
9b045a0c
BP
269struct ofputil_flow_removed {
270 struct cls_rule rule;
271 ovs_be64 cookie;
272 uint8_t reason; /* One of OFPRR_*. */
273 uint32_t duration_sec;
274 uint32_t duration_nsec;
275 uint16_t idle_timeout;
5e9d0469
BP
276 uint64_t packet_count; /* Packet count, UINT64_MAX if unknown. */
277 uint64_t byte_count; /* Byte count, UINT64_MAX if unknown. */
9b045a0c
BP
278};
279
90bf1e07
BP
280enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
281 const struct ofp_header *);
588cd7b5 282struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
27527aa0 283 enum ofputil_protocol);
9b045a0c 284
ebb57021
BP
285/* Abstract packet-in message. */
286struct ofputil_packet_in {
3e3252fa
EJ
287 const void *packet;
288 size_t packet_len;
289
f0fd1a17 290 enum ofp_packet_in_reason reason; /* One of OFPRR_*. */
a7349929 291 uint16_t controller_id; /* Controller ID to send to. */
54834960
EJ
292 uint8_t table_id;
293 ovs_be64 cookie;
ebb57021
BP
294
295 uint32_t buffer_id;
296 int send_len;
65120a8a 297 uint16_t total_len; /* Full length of frame. */
5d6c3af0
EJ
298
299 struct flow_metadata fmd; /* Metadata at creation time. */
ebb57021
BP
300};
301
65120a8a
EJ
302int ofputil_decode_packet_in(struct ofputil_packet_in *,
303 const struct ofp_header *);
54834960
EJ
304struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
305 enum nx_packet_in_format);
ebb57021 306
7c1a76a4
BP
307const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason);
308bool ofputil_packet_in_reason_from_string(const char *,
309 enum ofp_packet_in_reason *);
310
c6a93eb7
BP
311/* Abstract packet-out message. */
312struct ofputil_packet_out {
313 const void *packet; /* Packet data, if buffer_id == UINT32_MAX. */
314 size_t packet_len; /* Length of packet data in bytes. */
315 uint32_t buffer_id; /* Buffer id or UINT32_MAX if no buffer. */
316 uint16_t in_port; /* Packet's input port or OFPP_NONE. */
317 union ofp_action *actions; /* Actions. */
318 size_t n_actions; /* Number of elements in 'actions' array. */
319};
320
321enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
322 const struct ofp_packet_out *);
323struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *);
324
fa37b408
BP
325/* OpenFlow protocol utility functions. */
326void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
0bd0c660 327void *make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **);
dfdfc8d4 328
fa37b408 329void *make_openflow_xid(size_t openflow_len, uint8_t type,
44381c1b
BP
330 ovs_be32 xid, struct ofpbuf **);
331void *make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
0bd0c660 332 struct ofpbuf **);
dfdfc8d4 333
fa37b408 334void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
44381c1b 335void *put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
fa37b408 336 struct ofpbuf *);
dfdfc8d4
BP
337
338void *put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *);
339void *put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
340 struct ofpbuf *);
341
fa37b408 342void update_openflow_length(struct ofpbuf *);
d1e2cf21 343
63f2140a
BP
344void *ofputil_make_stats_request(size_t openflow_len, uint16_t type,
345 uint32_t subtype, struct ofpbuf **);
346void *ofputil_make_stats_reply(size_t openflow_len,
347 const struct ofp_stats_msg *request,
348 struct ofpbuf **);
349
350void ofputil_start_stats_reply(const struct ofp_stats_msg *request,
351 struct list *);
352struct ofpbuf *ofputil_reserve_stats_reply(size_t len, struct list *);
353void *ofputil_append_stats_reply(size_t len, struct list *);
dfdfc8d4 354
d1e2cf21 355const void *ofputil_stats_body(const struct ofp_header *);
c6430da5
BP
356size_t ofputil_stats_body_len(const struct ofp_header *);
357
358const void *ofputil_nxstats_body(const struct ofp_header *);
359size_t ofputil_nxstats_body_len(const struct ofp_header *);
d1e2cf21 360
daa68e9f 361struct ofpbuf *make_flow_mod(uint16_t command, const struct cls_rule *,
fa37b408 362 size_t actions_len);
daa68e9f 363struct ofpbuf *make_add_flow(const struct cls_rule *, uint32_t buffer_id,
fa37b408 364 uint16_t max_idle, size_t actions_len);
daa68e9f
BP
365struct ofpbuf *make_del_flow(const struct cls_rule *);
366struct ofpbuf *make_add_simple_flow(const struct cls_rule *,
fa37b408
BP
367 uint32_t buffer_id, uint16_t out_port,
368 uint16_t max_idle);
369struct ofpbuf *make_packet_in(uint32_t buffer_id, uint16_t in_port,
370 uint8_t reason,
371 const struct ofpbuf *payload, int max_send_len);
fa37b408
BP
372struct ofpbuf *make_echo_request(void);
373struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
7257b535 374
efb80167
BP
375struct ofpbuf *ofputil_encode_barrier_request(void);
376
7257b535
BP
377const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
378bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
9aee0764
BP
379\f
380/* Actions. */
381
e23ae585
BP
382/* The type of an action.
383 *
384 * For each implemented OFPAT_* and NXAST_* action type, there is a
385 * corresponding constant prefixed with OFPUTIL_, e.g.:
386 *
387 * OFPUTIL_OFPAT_OUTPUT
388 * OFPUTIL_OFPAT_SET_VLAN_VID
389 * OFPUTIL_OFPAT_SET_VLAN_PCP
390 * OFPUTIL_OFPAT_STRIP_VLAN
391 * OFPUTIL_OFPAT_SET_DL_SRC
392 * OFPUTIL_OFPAT_SET_DL_DST
393 * OFPUTIL_OFPAT_SET_NW_SRC
394 * OFPUTIL_OFPAT_SET_NW_DST
395 * OFPUTIL_OFPAT_SET_NW_TOS
396 * OFPUTIL_OFPAT_SET_TP_SRC
397 * OFPUTIL_OFPAT_SET_TP_DST
398 * OFPUTIL_OFPAT_ENQUEUE
399 * OFPUTIL_NXAST_RESUBMIT
400 * OFPUTIL_NXAST_SET_TUNNEL
401 * OFPUTIL_NXAST_SET_QUEUE
402 * OFPUTIL_NXAST_POP_QUEUE
403 * OFPUTIL_NXAST_REG_MOVE
404 * OFPUTIL_NXAST_REG_LOAD
405 * OFPUTIL_NXAST_NOTE
406 * OFPUTIL_NXAST_SET_TUNNEL64
407 * OFPUTIL_NXAST_MULTIPATH
408 * OFPUTIL_NXAST_AUTOPATH
409 * OFPUTIL_NXAST_BUNDLE
410 * OFPUTIL_NXAST_BUNDLE_LOAD
411 * OFPUTIL_NXAST_RESUBMIT_TABLE
412 * OFPUTIL_NXAST_OUTPUT_REG
0e553d9c
BP
413 * OFPUTIL_NXAST_LEARN
414 * OFPUTIL_NXAST_DEC_TTL
415 * OFPUTIL_NXAST_FIN_TIMEOUT
e23ae585
BP
416 *
417 * (The above list helps developers who want to "grep" for these definitions.)
418 */
38f2e360 419enum ofputil_action_code {
e23ae585
BP
420#define OFPAT_ACTION(ENUM, STRUCT, NAME) OFPUTIL_##ENUM,
421#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
422#include "ofp-util.def"
423};
424
425/* The number of values of "enum ofputil_action_code". */
426enum {
427#define OFPAT_ACTION(ENUM, STRUCT, NAME) + 1
428#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
429 OFPUTIL_N_ACTIONS = 0
430#include "ofp-util.def"
38f2e360
BP
431};
432
433int ofputil_decode_action(const union ofp_action *);
434enum ofputil_action_code ofputil_decode_action_unsafe(
435 const union ofp_action *);
436
e23ae585
BP
437int ofputil_action_code_from_name(const char *);
438
93996add
BP
439void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
440
441/* For each OpenFlow action <ENUM> that has a corresponding action structure
442 * struct <STRUCT>, this defines two functions:
443 *
444 * void ofputil_init_<ENUM>(struct <STRUCT> *action);
445 *
446 * Initializes the parts of 'action' that identify it as having type <ENUM>
447 * and length 'sizeof *action' and zeros the rest. For actions that have
448 * variable length, the length used and cleared is that of struct <STRUCT>.
449 *
450 * struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
451 *
452 * Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
453 * initializes it with ofputil_init_<ENUM>(), and returns it.
454 */
455#define OFPAT_ACTION(ENUM, STRUCT, NAME) \
456 void ofputil_init_##ENUM(struct STRUCT *); \
457 struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
458#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
459 void ofputil_init_##ENUM(struct STRUCT *); \
460 struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
461#include "ofp-util.def"
462
9aee0764 463#define OFP_ACTION_ALIGN 8 /* Alignment of ofp_actions. */
fa37b408 464
b4b8c781
BP
465static inline union ofp_action *
466ofputil_action_next(const union ofp_action *a)
467{
43d1478b
CB
468 return ((union ofp_action *) (void *)
469 ((uint8_t *) a + ntohs(a->header.len)));
b4b8c781
BP
470}
471
472static inline bool
473ofputil_action_is_valid(const union ofp_action *a, size_t n_actions)
474{
475 uint16_t len = ntohs(a->header.len);
476 return (!(len % OFP_ACTION_ALIGN)
477 && len >= sizeof *a
478 && len / sizeof *a <= n_actions);
479}
480
481/* This macro is careful to check for actions with bad lengths. */
482#define OFPUTIL_ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
483 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
484 (LEFT) > 0 && ofputil_action_is_valid(ITER, LEFT); \
485 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
486 (ITER) = ofputil_action_next(ITER)))
487
488/* This macro does not check for actions with bad lengths. It should only be
489 * used with actions from trusted sources or with actions that have already
490 * been validated (e.g. with OFPUTIL_ACTION_FOR_EACH). */
491#define OFPUTIL_ACTION_FOR_EACH_UNSAFE(ITER, LEFT, ACTIONS, N_ACTIONS) \
492 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
493 (LEFT) > 0; \
494 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
495 (ITER) = ofputil_action_next(ITER)))
9aee0764 496
90bf1e07
BP
497enum ofperr validate_actions(const union ofp_action *, size_t n_actions,
498 const struct flow *, int max_ports);
dbba996b 499bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
3052b0c5 500
90bf1e07
BP
501enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
502 union ofp_action **, size_t *);
18ddadc2
BP
503
504bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
505 const union ofp_action *b, size_t n_b);
506union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
26c112c2 507
0ff22822
BP
508/* Handy utility for parsing flows and actions. */
509bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
510
fa37b408 511#endif /* ofp-util.h */