ofp-util: Treat a packet-out in_port of OFPP_CONTROLLER as OFPP_NONE.
[sliver-openvswitch.git] / lib / ofp-util.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 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_UTIL_H
18 #define OFP_UTIL_H 1
19
20 #include <assert.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include "classifier.h"
25 #include "flow.h"
26 #include "netdev.h"
27 #include "openflow/nicira-ext.h"
28 #include "openvswitch/types.h"
29
30 struct cls_rule;
31 struct ofpbuf;
32
33 /* Basic decoding and length validation of OpenFlow messages. */
34 enum ofputil_msg_code {
35     OFPUTIL_MSG_INVALID,
36
37     /* OFPT_* messages. */
38     OFPUTIL_OFPT_HELLO,
39     OFPUTIL_OFPT_ERROR,
40     OFPUTIL_OFPT_ECHO_REQUEST,
41     OFPUTIL_OFPT_ECHO_REPLY,
42     OFPUTIL_OFPT_FEATURES_REQUEST,
43     OFPUTIL_OFPT_FEATURES_REPLY,
44     OFPUTIL_OFPT_GET_CONFIG_REQUEST,
45     OFPUTIL_OFPT_GET_CONFIG_REPLY,
46     OFPUTIL_OFPT_SET_CONFIG,
47     OFPUTIL_OFPT_PACKET_IN,
48     OFPUTIL_OFPT_FLOW_REMOVED,
49     OFPUTIL_OFPT_PORT_STATUS,
50     OFPUTIL_OFPT_PACKET_OUT,
51     OFPUTIL_OFPT_FLOW_MOD,
52     OFPUTIL_OFPT_PORT_MOD,
53     OFPUTIL_OFPT_BARRIER_REQUEST,
54     OFPUTIL_OFPT_BARRIER_REPLY,
55     OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST,
56     OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY,
57
58     /* OFPST_* stat requests. */
59     OFPUTIL_OFPST_DESC_REQUEST,
60     OFPUTIL_OFPST_FLOW_REQUEST,
61     OFPUTIL_OFPST_AGGREGATE_REQUEST,
62     OFPUTIL_OFPST_TABLE_REQUEST,
63     OFPUTIL_OFPST_PORT_REQUEST,
64     OFPUTIL_OFPST_QUEUE_REQUEST,
65
66     /* OFPST_* stat replies. */
67     OFPUTIL_OFPST_DESC_REPLY,
68     OFPUTIL_OFPST_FLOW_REPLY,
69     OFPUTIL_OFPST_QUEUE_REPLY,
70     OFPUTIL_OFPST_PORT_REPLY,
71     OFPUTIL_OFPST_TABLE_REPLY,
72     OFPUTIL_OFPST_AGGREGATE_REPLY,
73
74     /* NXT_* messages. */
75     OFPUTIL_NXT_ROLE_REQUEST,
76     OFPUTIL_NXT_ROLE_REPLY,
77     OFPUTIL_NXT_SET_FLOW_FORMAT,
78     OFPUTIL_NXT_FLOW_MOD_TABLE_ID,
79     OFPUTIL_NXT_FLOW_MOD,
80     OFPUTIL_NXT_FLOW_REMOVED,
81     OFPUTIL_NXT_SET_PACKET_IN_FORMAT,
82     OFPUTIL_NXT_PACKET_IN,
83     OFPUTIL_NXT_FLOW_AGE,
84     OFPUTIL_NXT_SET_ASYNC_CONFIG,
85     OFPUTIL_NXT_SET_CONTROLLER_ID,
86
87     /* NXST_* stat requests. */
88     OFPUTIL_NXST_FLOW_REQUEST,
89     OFPUTIL_NXST_AGGREGATE_REQUEST,
90
91     /* NXST_* stat replies. */
92     OFPUTIL_NXST_FLOW_REPLY,
93     OFPUTIL_NXST_AGGREGATE_REPLY
94 };
95
96 struct ofputil_msg_type;
97 enum ofperr ofputil_decode_msg_type(const struct ofp_header *,
98                                     const struct ofputil_msg_type **);
99 enum ofperr ofputil_decode_msg_type_partial(const struct ofp_header *,
100                                             size_t length,
101                                             const struct ofputil_msg_type **);
102 enum ofputil_msg_code ofputil_msg_type_code(const struct ofputil_msg_type *);
103 const char *ofputil_msg_type_name(const struct ofputil_msg_type *);
104
105 /* Port numbers. */
106 enum ofperr ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port);
107 ovs_be32 ofputil_port_to_ofp11(uint16_t ofp10_port);
108
109 enum ofperr ofputil_check_output_port(uint16_t ofp_port, int max_ports);
110 bool ofputil_port_from_string(const char *, uint16_t *port);
111 void ofputil_format_port(uint16_t port, struct ds *);
112
113 /* Converting OFPFW_NW_SRC_MASK and OFPFW_NW_DST_MASK wildcard bit counts to
114  * and from IP bitmasks. */
115 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
116 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
117
118 /* Protocols.
119  *
120  * These are arranged from most portable to least portable, or alternatively
121  * from least powerful to most powerful.  Formats earlier on the list are more
122  * likely to be understood for the purpose of making requests, but formats
123  * later on the list are more likely to accurately describe a flow within a
124  * switch.
125  *
126  * On any given OpenFlow connection, a single protocol is in effect at any
127  * given time.  These values use separate bits only because that makes it easy
128  * to test whether a particular protocol is within a given set of protocols and
129  * to implement set union and intersection.
130  */
131 enum ofputil_protocol {
132     /* OpenFlow 1.0-based protocols. */
133     OFPUTIL_P_OF10     = 1 << 0, /* OpenFlow 1.0 flow format. */
134     OFPUTIL_P_OF10_TID = 1 << 1, /* OF1.0 + flow_mod_table_id extension. */
135 #define OFPUTIL_P_OF10_ANY (OFPUTIL_P_OF10 | OFPUTIL_P_OF10_TID)
136
137     /* OpenFlow 1.0 with NXM-based flow formats. */
138     OFPUTIL_P_NXM      = 1 << 2, /* Nicira extended match. */
139     OFPUTIL_P_NXM_TID  = 1 << 3, /* NXM + flow_mod_table_id extension. */
140 #define OFPUTIL_P_NXM_ANY (OFPUTIL_P_NXM | OFPUTIL_P_NXM_TID)
141
142     /* All protocols. */
143 #define OFPUTIL_P_ANY (OFPUTIL_P_OF10_ANY | OFPUTIL_P_NXM_ANY)
144
145     /* Protocols in which a specific table may be specified in flow_mods. */
146 #define OFPUTIL_P_TID (OFPUTIL_P_OF10_TID | OFPUTIL_P_NXM_TID)
147 };
148
149 /* Protocols to use for flow dumps, from most to least preferred. */
150 extern enum ofputil_protocol ofputil_flow_dump_protocols[];
151 extern size_t ofputil_n_flow_dump_protocols;
152
153 enum ofputil_protocol ofputil_protocol_from_ofp_version(int version);
154 uint8_t ofputil_protocol_to_ofp_version(enum ofputil_protocol);
155
156 bool ofputil_protocol_is_valid(enum ofputil_protocol);
157 enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
158                                                bool enable);
159 enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
160 enum ofputil_protocol ofputil_protocol_set_base(
161     enum ofputil_protocol cur, enum ofputil_protocol new_base);
162
163 const char *ofputil_protocol_to_string(enum ofputil_protocol);
164 char *ofputil_protocols_to_string(enum ofputil_protocol);
165 enum ofputil_protocol ofputil_protocols_from_string(const char *);
166 enum ofputil_protocol ofputil_usable_protocols(const struct cls_rule *);
167
168 struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
169                                            enum ofputil_protocol want,
170                                            enum ofputil_protocol *next);
171
172 /* nx_flow_format */
173 struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
174 enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
175 bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
176 const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
177
178 /* Work with OpenFlow 1.0 ofp_match. */
179 void ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *);
180 void ofputil_cls_rule_from_match(const struct ofp_match *,
181                                  unsigned int priority, struct cls_rule *);
182 void ofputil_normalize_rule(struct cls_rule *);
183 void ofputil_cls_rule_to_match(const struct cls_rule *, struct ofp_match *);
184
185 /* dl_type translation between OpenFlow and 'struct flow' format. */
186 ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
187 ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
188
189 /* PACKET_IN. */
190 bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format);
191 int ofputil_packet_in_format_from_string(const char *);
192 const char *ofputil_packet_in_format_to_string(enum nx_packet_in_format);
193 struct ofpbuf *ofputil_make_set_packet_in_format(enum nx_packet_in_format);
194
195 /* NXT_FLOW_MOD_TABLE_ID extension. */
196 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
197
198 /* Protocol-independent flow_mod. */
199 struct ofputil_flow_mod {
200     struct cls_rule cr;
201     ovs_be64 cookie;
202     ovs_be64 cookie_mask;
203     uint8_t table_id;
204     uint16_t command;
205     uint16_t idle_timeout;
206     uint16_t hard_timeout;
207     uint32_t buffer_id;
208     uint16_t out_port;
209     uint16_t flags;
210     union ofp_action *actions;
211     size_t n_actions;
212 };
213
214 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
215                                     const struct ofp_header *,
216                                     enum ofputil_protocol);
217 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
218                                        enum ofputil_protocol);
219
220 enum ofputil_protocol ofputil_flow_mod_usable_protocols(
221     const struct ofputil_flow_mod *fms, size_t n_fms);
222
223 /* Flow stats or aggregate stats request, independent of protocol. */
224 struct ofputil_flow_stats_request {
225     bool aggregate;             /* Aggregate results? */
226     struct cls_rule match;
227     ovs_be64 cookie;
228     ovs_be64 cookie_mask;
229     uint16_t out_port;
230     uint8_t table_id;
231 };
232
233 enum ofperr ofputil_decode_flow_stats_request(
234     struct ofputil_flow_stats_request *, const struct ofp_header *);
235 struct ofpbuf *ofputil_encode_flow_stats_request(
236     const struct ofputil_flow_stats_request *, enum ofputil_protocol);
237 enum ofputil_protocol ofputil_flow_stats_request_usable_protocols(
238     const struct ofputil_flow_stats_request *);
239
240 /* Flow stats reply, independent of protocol. */
241 struct ofputil_flow_stats {
242     struct cls_rule rule;
243     ovs_be64 cookie;
244     uint8_t table_id;
245     uint32_t duration_sec;
246     uint32_t duration_nsec;
247     uint16_t idle_timeout;
248     uint16_t hard_timeout;
249     int idle_age;               /* Seconds since last packet, -1 if unknown. */
250     int hard_age;               /* Seconds since last change, -1 if unknown. */
251     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
252     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
253     union ofp_action *actions;
254     size_t n_actions;
255 };
256
257 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
258                                     struct ofpbuf *msg,
259                                     bool flow_age_extension);
260 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
261                                      struct list *replies);
262
263 /* Aggregate stats reply, independent of protocol. */
264 struct ofputil_aggregate_stats {
265     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
266     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
267     uint32_t flow_count;
268 };
269
270 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
271     const struct ofputil_aggregate_stats *stats,
272     const struct ofp_stats_msg *request);
273
274 /* Flow removed message, independent of protocol. */
275 struct ofputil_flow_removed {
276     struct cls_rule rule;
277     ovs_be64 cookie;
278     uint8_t reason;             /* One of OFPRR_*. */
279     uint32_t duration_sec;
280     uint32_t duration_nsec;
281     uint16_t idle_timeout;
282     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
283     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
284 };
285
286 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
287                                         const struct ofp_header *);
288 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
289                                            enum ofputil_protocol);
290
291 /* Abstract packet-in message. */
292 struct ofputil_packet_in {
293     const void *packet;
294     size_t packet_len;
295
296     enum ofp_packet_in_reason reason;    /* One of OFPR_*. */
297     uint16_t controller_id;              /* Controller ID to send to. */
298     uint8_t table_id;
299     ovs_be64 cookie;
300
301     uint32_t buffer_id;
302     int send_len;
303     uint16_t total_len;         /* Full length of frame. */
304
305     struct flow_metadata fmd;   /* Metadata at creation time. */
306 };
307
308 int ofputil_decode_packet_in(struct ofputil_packet_in *,
309                              const struct ofp_header *);
310 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
311                                         enum nx_packet_in_format);
312
313 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason);
314 bool ofputil_packet_in_reason_from_string(const char *,
315                                           enum ofp_packet_in_reason *);
316
317 /* Abstract packet-out message.
318  *
319  * ofputil_decode_packet_out() will ensure that 'in_port' is a physical port
320  * (OFPP_MAX or less) or one of OFPP_LOCAL, OFPP_NONE, or OFPP_CONTROLLER. */
321 struct ofputil_packet_out {
322     const void *packet;         /* Packet data, if buffer_id == UINT32_MAX. */
323     size_t packet_len;          /* Length of packet data in bytes. */
324     uint32_t buffer_id;         /* Buffer id or UINT32_MAX if no buffer. */
325     uint16_t in_port;           /* Packet's input port. */
326     union ofp_action *actions;  /* Actions. */
327     size_t n_actions;           /* Number of elements in 'actions' array. */
328 };
329
330 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
331                                       const struct ofp_packet_out *);
332 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *);
333
334 enum ofputil_port_config {
335     /* OpenFlow 1.0 and 1.1 share these values for these port config bits. */
336     OFPUTIL_PC_PORT_DOWN    = 1 << 0, /* Port is administratively down. */
337     OFPUTIL_PC_NO_RECV      = 1 << 2, /* Drop all packets received by port. */
338     OFPUTIL_PC_NO_FWD       = 1 << 5, /* Drop packets forwarded to port. */
339     OFPUTIL_PC_NO_PACKET_IN = 1 << 6, /* No send packet-in msgs for port. */
340     /* OpenFlow 1.0 only. */
341     OFPUTIL_PC_NO_STP       = 1 << 1, /* No 802.1D spanning tree for port. */
342     OFPUTIL_PC_NO_RECV_STP  = 1 << 3, /* Drop received 802.1D STP packets. */
343     OFPUTIL_PC_NO_FLOOD     = 1 << 4, /* Do not include port when flooding. */
344     /* There are no OpenFlow 1.1-only bits. */
345 };
346
347 enum ofputil_port_state {
348     /* OpenFlow 1.0 and 1.1 share this values for these port state bits. */
349     OFPUTIL_PS_LINK_DOWN   = 1 << 0, /* No physical link present. */
350     /* OpenFlow 1.1 only. */
351     OFPUTIL_PS_BLOCKED     = 1 << 1, /* Port is blocked */
352     OFPUTIL_PS_LIVE        = 1 << 2, /* Live for Fast Failover Group. */
353     /* OpenFlow 1.0 only. */
354     OFPUTIL_PS_STP_LISTEN  = 0 << 8, /* Not learning or relaying frames. */
355     OFPUTIL_PS_STP_LEARN   = 1 << 8, /* Learning but not relaying frames. */
356     OFPUTIL_PS_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
357     OFPUTIL_PS_STP_BLOCK   = 3 << 8, /* Not part of spanning tree. */
358     OFPUTIL_PS_STP_MASK    = 3 << 8  /* Bit mask for OFPPS10_STP_* values. */
359 };
360
361 /* Abstract ofp10_phy_port or ofp11_port. */
362 struct ofputil_phy_port {
363     uint16_t port_no;
364     uint8_t hw_addr[OFP_ETH_ALEN];
365     char name[OFP_MAX_PORT_NAME_LEN];
366     enum ofputil_port_config config;
367     enum ofputil_port_state state;
368
369     /* NETDEV_F_* feature bitmasks. */
370     enum netdev_features curr;       /* Current features. */
371     enum netdev_features advertised; /* Features advertised by the port. */
372     enum netdev_features supported;  /* Features supported by the port. */
373     enum netdev_features peer;       /* Features advertised by peer. */
374
375     /* Speed. */
376     uint32_t curr_speed;        /* Current speed, in kbps. */
377     uint32_t max_speed;         /* Maximum supported speed, in kbps. */
378 };
379
380 enum ofputil_capabilities {
381     /* OpenFlow 1.0 and 1.1 share these values for these capabilities. */
382     OFPUTIL_C_FLOW_STATS     = 1 << 0,  /* Flow statistics. */
383     OFPUTIL_C_TABLE_STATS    = 1 << 1,  /* Table statistics. */
384     OFPUTIL_C_PORT_STATS     = 1 << 2,  /* Port statistics. */
385     OFPUTIL_C_IP_REASM       = 1 << 5,  /* Can reassemble IP fragments. */
386     OFPUTIL_C_QUEUE_STATS    = 1 << 6,  /* Queue statistics. */
387     OFPUTIL_C_ARP_MATCH_IP   = 1 << 7,  /* Match IP addresses in ARP pkts. */
388
389     /* OpenFlow 1.0 only. */
390     OFPUTIL_C_STP            = 1 << 3,  /* 802.1d spanning tree. */
391
392     /* OpenFlow 1.1 only. */
393     OFPUTIL_C_GROUP_STATS    = 1 << 4,  /* Group statistics. */
394 };
395
396 enum ofputil_action_bitmap {
397     OFPUTIL_A_OUTPUT         = 1 << 0,
398     OFPUTIL_A_SET_VLAN_VID   = 1 << 1,
399     OFPUTIL_A_SET_VLAN_PCP   = 1 << 2,
400     OFPUTIL_A_STRIP_VLAN     = 1 << 3,
401     OFPUTIL_A_SET_DL_SRC     = 1 << 4,
402     OFPUTIL_A_SET_DL_DST     = 1 << 5,
403     OFPUTIL_A_SET_NW_SRC     = 1 << 6,
404     OFPUTIL_A_SET_NW_DST     = 1 << 7,
405     OFPUTIL_A_SET_NW_ECN     = 1 << 8,
406     OFPUTIL_A_SET_NW_TOS     = 1 << 9,
407     OFPUTIL_A_SET_TP_SRC     = 1 << 10,
408     OFPUTIL_A_SET_TP_DST     = 1 << 11,
409     OFPUTIL_A_ENQUEUE        = 1 << 12,
410     OFPUTIL_A_COPY_TTL_OUT   = 1 << 13,
411     OFPUTIL_A_COPY_TTL_IN    = 1 << 14,
412     OFPUTIL_A_SET_MPLS_LABEL = 1 << 15,
413     OFPUTIL_A_SET_MPLS_TC    = 1 << 16,
414     OFPUTIL_A_SET_MPLS_TTL   = 1 << 17,
415     OFPUTIL_A_DEC_MPLS_TTL   = 1 << 18,
416     OFPUTIL_A_PUSH_VLAN      = 1 << 19,
417     OFPUTIL_A_POP_VLAN       = 1 << 20,
418     OFPUTIL_A_PUSH_MPLS      = 1 << 21,
419     OFPUTIL_A_POP_MPLS       = 1 << 22,
420     OFPUTIL_A_SET_QUEUE      = 1 << 23,
421     OFPUTIL_A_GROUP          = 1 << 24,
422     OFPUTIL_A_SET_NW_TTL     = 1 << 25,
423     OFPUTIL_A_DEC_NW_TTL     = 1 << 26,
424 };
425
426 /* Abstract ofp_switch_features. */
427 struct ofputil_switch_features {
428     uint64_t datapath_id;       /* Datapath unique ID. */
429     uint32_t n_buffers;         /* Max packets buffered at once. */
430     uint8_t n_tables;           /* Number of tables supported by datapath. */
431     enum ofputil_capabilities capabilities;
432     enum ofputil_action_bitmap actions;
433 };
434
435 enum ofperr ofputil_decode_switch_features(const struct ofp_switch_features *,
436                                            struct ofputil_switch_features *,
437                                            struct ofpbuf *);
438 int ofputil_pull_switch_features_port(struct ofpbuf *,
439                                       struct ofputil_phy_port *);
440 size_t ofputil_count_phy_ports(const struct ofp_switch_features *);
441
442 struct ofpbuf *ofputil_encode_switch_features(
443     const struct ofputil_switch_features *, enum ofputil_protocol,
444     ovs_be32 xid);
445 void ofputil_put_switch_features_port(const struct ofputil_phy_port *,
446                                       struct ofpbuf *);
447
448 /* Abstract ofp_port_status. */
449 struct ofputil_port_status {
450     enum ofp_port_reason reason;
451     struct ofputil_phy_port desc;
452 };
453
454 enum ofperr ofputil_decode_port_status(const struct ofp_port_status *,
455                                        struct ofputil_port_status *);
456 struct ofpbuf *ofputil_encode_port_status(const struct ofputil_port_status *,
457                                           enum ofputil_protocol);
458
459 /* Abstract ofp_port_mod. */
460 struct ofputil_port_mod {
461     uint16_t port_no;
462     uint8_t hw_addr[OFP_ETH_ALEN];
463     enum ofputil_port_config config;
464     enum ofputil_port_config mask;
465     enum netdev_features advertise;
466 };
467
468 enum ofperr ofputil_decode_port_mod(const struct ofp_header *,
469                                     struct ofputil_port_mod *);
470 struct ofpbuf *ofputil_encode_port_mod(const struct ofputil_port_mod *,
471                                        enum ofputil_protocol);
472
473 /* OpenFlow protocol utility functions. */
474 void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
475 void *make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **);
476
477 void *make_openflow_xid(size_t openflow_len, uint8_t type,
478                         ovs_be32 xid, struct ofpbuf **);
479 void *make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
480                      struct ofpbuf **);
481
482 void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
483 void *put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
484                        struct ofpbuf *);
485
486 void *put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *);
487 void *put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
488                     struct ofpbuf *);
489
490 void update_openflow_length(struct ofpbuf *);
491
492 void *ofputil_make_stats_request(size_t openflow_len, uint16_t type,
493                                  uint32_t subtype, struct ofpbuf **);
494 void *ofputil_make_stats_reply(size_t openflow_len,
495                                const struct ofp_stats_msg *request,
496                                struct ofpbuf **);
497
498 void ofputil_start_stats_reply(const struct ofp_stats_msg *request,
499                                struct list *);
500 struct ofpbuf *ofputil_reserve_stats_reply(size_t len, struct list *);
501 void *ofputil_append_stats_reply(size_t len, struct list *);
502
503 const void *ofputil_stats_body(const struct ofp_header *);
504 size_t ofputil_stats_body_len(const struct ofp_header *);
505
506 const void *ofputil_nxstats_body(const struct ofp_header *);
507 size_t ofputil_nxstats_body_len(const struct ofp_header *);
508
509 struct ofpbuf *make_flow_mod(uint16_t command, const struct cls_rule *,
510                              size_t actions_len);
511 struct ofpbuf *make_add_flow(const struct cls_rule *, uint32_t buffer_id,
512                              uint16_t max_idle, size_t actions_len);
513 struct ofpbuf *make_del_flow(const struct cls_rule *);
514 struct ofpbuf *make_add_simple_flow(const struct cls_rule *,
515                                     uint32_t buffer_id, uint16_t out_port,
516                                     uint16_t max_idle);
517 struct ofpbuf *make_packet_in(uint32_t buffer_id, uint16_t in_port,
518                               uint8_t reason,
519                               const struct ofpbuf *payload, int max_send_len);
520 struct ofpbuf *make_echo_request(void);
521 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
522
523 struct ofpbuf *ofputil_encode_barrier_request(void);
524
525 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
526 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
527 \f
528 /* Actions. */
529
530 /* The type of an action.
531  *
532  * For each implemented OFPAT10_* and NXAST_* action type, there is a
533  * corresponding constant prefixed with OFPUTIL_, e.g.:
534  *
535  * OFPUTIL_OFPAT10_OUTPUT
536  * OFPUTIL_OFPAT10_SET_VLAN_VID
537  * OFPUTIL_OFPAT10_SET_VLAN_PCP
538  * OFPUTIL_OFPAT10_STRIP_VLAN
539  * OFPUTIL_OFPAT10_SET_DL_SRC
540  * OFPUTIL_OFPAT10_SET_DL_DST
541  * OFPUTIL_OFPAT10_SET_NW_SRC
542  * OFPUTIL_OFPAT10_SET_NW_DST
543  * OFPUTIL_OFPAT10_SET_NW_TOS
544  * OFPUTIL_OFPAT10_SET_TP_SRC
545  * OFPUTIL_OFPAT10_SET_TP_DST
546  * OFPUTIL_OFPAT10_ENQUEUE
547  * OFPUTIL_NXAST_RESUBMIT
548  * OFPUTIL_NXAST_SET_TUNNEL
549  * OFPUTIL_NXAST_SET_QUEUE
550  * OFPUTIL_NXAST_POP_QUEUE
551  * OFPUTIL_NXAST_REG_MOVE
552  * OFPUTIL_NXAST_REG_LOAD
553  * OFPUTIL_NXAST_NOTE
554  * OFPUTIL_NXAST_SET_TUNNEL64
555  * OFPUTIL_NXAST_MULTIPATH
556  * OFPUTIL_NXAST_AUTOPATH
557  * OFPUTIL_NXAST_BUNDLE
558  * OFPUTIL_NXAST_BUNDLE_LOAD
559  * OFPUTIL_NXAST_RESUBMIT_TABLE
560  * OFPUTIL_NXAST_OUTPUT_REG
561  * OFPUTIL_NXAST_LEARN
562  * OFPUTIL_NXAST_DEC_TTL
563  * OFPUTIL_NXAST_FIN_TIMEOUT
564  *
565  * (The above list helps developers who want to "grep" for these definitions.)
566  */
567 enum ofputil_action_code {
568 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             OFPUTIL_##ENUM,
569 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
570 #include "ofp-util.def"
571 };
572
573 /* The number of values of "enum ofputil_action_code". */
574 enum {
575 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             + 1
576 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
577     OFPUTIL_N_ACTIONS = 0
578 #include "ofp-util.def"
579 };
580
581 int ofputil_decode_action(const union ofp_action *);
582 enum ofputil_action_code ofputil_decode_action_unsafe(
583     const union ofp_action *);
584
585 int ofputil_action_code_from_name(const char *);
586
587 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
588
589 /* For each OpenFlow action <ENUM> that has a corresponding action structure
590  * struct <STRUCT>, this defines two functions:
591  *
592  *   void ofputil_init_<ENUM>(struct <STRUCT> *action);
593  *
594  *     Initializes the parts of 'action' that identify it as having type <ENUM>
595  *     and length 'sizeof *action' and zeros the rest.  For actions that have
596  *     variable length, the length used and cleared is that of struct <STRUCT>.
597  *
598  *  struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
599  *
600  *     Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
601  *     initializes it with ofputil_init_<ENUM>(), and returns it.
602  */
603 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)              \
604     void ofputil_init_##ENUM(struct STRUCT *);          \
605     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
606 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
607     void ofputil_init_##ENUM(struct STRUCT *);          \
608     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
609 #include "ofp-util.def"
610
611 #define OFP_ACTION_ALIGN 8      /* Alignment of ofp_actions. */
612
613 static inline union ofp_action *
614 ofputil_action_next(const union ofp_action *a)
615 {
616     return ((union ofp_action *) (void *)
617             ((uint8_t *) a + ntohs(a->header.len)));
618 }
619
620 static inline bool
621 ofputil_action_is_valid(const union ofp_action *a, size_t n_actions)
622 {
623     uint16_t len = ntohs(a->header.len);
624     return (!(len % OFP_ACTION_ALIGN)
625             && len >= sizeof *a
626             && len / sizeof *a <= n_actions);
627 }
628
629 /* This macro is careful to check for actions with bad lengths. */
630 #define OFPUTIL_ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS)         \
631     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
632          (LEFT) > 0 && ofputil_action_is_valid(ITER, LEFT);             \
633          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
634           (ITER) = ofputil_action_next(ITER)))
635
636 /* This macro does not check for actions with bad lengths.  It should only be
637  * used with actions from trusted sources or with actions that have already
638  * been validated (e.g. with OFPUTIL_ACTION_FOR_EACH).  */
639 #define OFPUTIL_ACTION_FOR_EACH_UNSAFE(ITER, LEFT, ACTIONS, N_ACTIONS)  \
640     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
641          (LEFT) > 0;                                                    \
642          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
643           (ITER) = ofputil_action_next(ITER)))
644
645 enum ofperr validate_actions(const union ofp_action *, size_t n_actions,
646                              const struct flow *, int max_ports);
647 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
648
649 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
650                                  union ofp_action **, size_t *);
651
652 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
653                            const union ofp_action *b, size_t n_b);
654 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
655
656 /* Handy utility for parsing flows and actions. */
657 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
658
659 #endif /* ofp-util.h */