Global replace of Nicira Networks.
[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 struct ofputil_packet_out {
319     const void *packet;         /* Packet data, if buffer_id == UINT32_MAX. */
320     size_t packet_len;          /* Length of packet data in bytes. */
321     uint32_t buffer_id;         /* Buffer id or UINT32_MAX if no buffer. */
322     uint16_t in_port;           /* Packet's input port or OFPP_NONE. */
323     union ofp_action *actions;  /* Actions. */
324     size_t n_actions;           /* Number of elements in 'actions' array. */
325 };
326
327 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
328                                       const struct ofp_packet_out *);
329 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *);
330
331 enum ofputil_port_config {
332     /* OpenFlow 1.0 and 1.1 share these values for these port config bits. */
333     OFPUTIL_PC_PORT_DOWN    = 1 << 0, /* Port is administratively down. */
334     OFPUTIL_PC_NO_RECV      = 1 << 2, /* Drop all packets received by port. */
335     OFPUTIL_PC_NO_FWD       = 1 << 5, /* Drop packets forwarded to port. */
336     OFPUTIL_PC_NO_PACKET_IN = 1 << 6, /* No send packet-in msgs for port. */
337     /* OpenFlow 1.0 only. */
338     OFPUTIL_PC_NO_STP       = 1 << 1, /* No 802.1D spanning tree for port. */
339     OFPUTIL_PC_NO_RECV_STP  = 1 << 3, /* Drop received 802.1D STP packets. */
340     OFPUTIL_PC_NO_FLOOD     = 1 << 4, /* Do not include port when flooding. */
341     /* There are no OpenFlow 1.1-only bits. */
342 };
343
344 enum ofputil_port_state {
345     /* OpenFlow 1.0 and 1.1 share this values for these port state bits. */
346     OFPUTIL_PS_LINK_DOWN   = 1 << 0, /* No physical link present. */
347     /* OpenFlow 1.1 only. */
348     OFPUTIL_PS_BLOCKED     = 1 << 1, /* Port is blocked */
349     OFPUTIL_PS_LIVE        = 1 << 2, /* Live for Fast Failover Group. */
350     /* OpenFlow 1.0 only. */
351     OFPUTIL_PS_STP_LISTEN  = 0 << 8, /* Not learning or relaying frames. */
352     OFPUTIL_PS_STP_LEARN   = 1 << 8, /* Learning but not relaying frames. */
353     OFPUTIL_PS_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
354     OFPUTIL_PS_STP_BLOCK   = 3 << 8, /* Not part of spanning tree. */
355     OFPUTIL_PS_STP_MASK    = 3 << 8  /* Bit mask for OFPPS10_STP_* values. */
356 };
357
358 /* Abstract ofp10_phy_port or ofp11_port. */
359 struct ofputil_phy_port {
360     uint16_t port_no;
361     uint8_t hw_addr[OFP_ETH_ALEN];
362     char name[OFP_MAX_PORT_NAME_LEN];
363     enum ofputil_port_config config;
364     enum ofputil_port_state state;
365
366     /* NETDEV_F_* feature bitmasks. */
367     enum netdev_features curr;       /* Current features. */
368     enum netdev_features advertised; /* Features advertised by the port. */
369     enum netdev_features supported;  /* Features supported by the port. */
370     enum netdev_features peer;       /* Features advertised by peer. */
371
372     /* Speed. */
373     uint32_t curr_speed;        /* Current speed, in kbps. */
374     uint32_t max_speed;         /* Maximum supported speed, in kbps. */
375 };
376
377 enum ofputil_capabilities {
378     /* OpenFlow 1.0 and 1.1 share these values for these capabilities. */
379     OFPUTIL_C_FLOW_STATS     = 1 << 0,  /* Flow statistics. */
380     OFPUTIL_C_TABLE_STATS    = 1 << 1,  /* Table statistics. */
381     OFPUTIL_C_PORT_STATS     = 1 << 2,  /* Port statistics. */
382     OFPUTIL_C_IP_REASM       = 1 << 5,  /* Can reassemble IP fragments. */
383     OFPUTIL_C_QUEUE_STATS    = 1 << 6,  /* Queue statistics. */
384     OFPUTIL_C_ARP_MATCH_IP   = 1 << 7,  /* Match IP addresses in ARP pkts. */
385
386     /* OpenFlow 1.0 only. */
387     OFPUTIL_C_STP            = 1 << 3,  /* 802.1d spanning tree. */
388
389     /* OpenFlow 1.1 only. */
390     OFPUTIL_C_GROUP_STATS    = 1 << 4,  /* Group statistics. */
391 };
392
393 enum ofputil_action_bitmap {
394     OFPUTIL_A_OUTPUT         = 1 << 0,
395     OFPUTIL_A_SET_VLAN_VID   = 1 << 1,
396     OFPUTIL_A_SET_VLAN_PCP   = 1 << 2,
397     OFPUTIL_A_STRIP_VLAN     = 1 << 3,
398     OFPUTIL_A_SET_DL_SRC     = 1 << 4,
399     OFPUTIL_A_SET_DL_DST     = 1 << 5,
400     OFPUTIL_A_SET_NW_SRC     = 1 << 6,
401     OFPUTIL_A_SET_NW_DST     = 1 << 7,
402     OFPUTIL_A_SET_NW_ECN     = 1 << 8,
403     OFPUTIL_A_SET_NW_TOS     = 1 << 9,
404     OFPUTIL_A_SET_TP_SRC     = 1 << 10,
405     OFPUTIL_A_SET_TP_DST     = 1 << 11,
406     OFPUTIL_A_ENQUEUE        = 1 << 12,
407     OFPUTIL_A_COPY_TTL_OUT   = 1 << 13,
408     OFPUTIL_A_COPY_TTL_IN    = 1 << 14,
409     OFPUTIL_A_SET_MPLS_LABEL = 1 << 15,
410     OFPUTIL_A_SET_MPLS_TC    = 1 << 16,
411     OFPUTIL_A_SET_MPLS_TTL   = 1 << 17,
412     OFPUTIL_A_DEC_MPLS_TTL   = 1 << 18,
413     OFPUTIL_A_PUSH_VLAN      = 1 << 19,
414     OFPUTIL_A_POP_VLAN       = 1 << 20,
415     OFPUTIL_A_PUSH_MPLS      = 1 << 21,
416     OFPUTIL_A_POP_MPLS       = 1 << 22,
417     OFPUTIL_A_SET_QUEUE      = 1 << 23,
418     OFPUTIL_A_GROUP          = 1 << 24,
419     OFPUTIL_A_SET_NW_TTL     = 1 << 25,
420     OFPUTIL_A_DEC_NW_TTL     = 1 << 26,
421 };
422
423 /* Abstract ofp_switch_features. */
424 struct ofputil_switch_features {
425     uint64_t datapath_id;       /* Datapath unique ID. */
426     uint32_t n_buffers;         /* Max packets buffered at once. */
427     uint8_t n_tables;           /* Number of tables supported by datapath. */
428     enum ofputil_capabilities capabilities;
429     enum ofputil_action_bitmap actions;
430 };
431
432 enum ofperr ofputil_decode_switch_features(const struct ofp_switch_features *,
433                                            struct ofputil_switch_features *,
434                                            struct ofpbuf *);
435 int ofputil_pull_switch_features_port(struct ofpbuf *,
436                                       struct ofputil_phy_port *);
437 size_t ofputil_count_phy_ports(const struct ofp_switch_features *);
438
439 struct ofpbuf *ofputil_encode_switch_features(
440     const struct ofputil_switch_features *, enum ofputil_protocol,
441     ovs_be32 xid);
442 void ofputil_put_switch_features_port(const struct ofputil_phy_port *,
443                                       struct ofpbuf *);
444
445 /* Abstract ofp_port_status. */
446 struct ofputil_port_status {
447     enum ofp_port_reason reason;
448     struct ofputil_phy_port desc;
449 };
450
451 enum ofperr ofputil_decode_port_status(const struct ofp_port_status *,
452                                        struct ofputil_port_status *);
453 struct ofpbuf *ofputil_encode_port_status(const struct ofputil_port_status *,
454                                           enum ofputil_protocol);
455
456 /* Abstract ofp_port_mod. */
457 struct ofputil_port_mod {
458     uint16_t port_no;
459     uint8_t hw_addr[OFP_ETH_ALEN];
460     enum ofputil_port_config config;
461     enum ofputil_port_config mask;
462     enum netdev_features advertise;
463 };
464
465 enum ofperr ofputil_decode_port_mod(const struct ofp_header *,
466                                     struct ofputil_port_mod *);
467 struct ofpbuf *ofputil_encode_port_mod(const struct ofputil_port_mod *,
468                                        enum ofputil_protocol);
469
470 /* OpenFlow protocol utility functions. */
471 void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
472 void *make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **);
473
474 void *make_openflow_xid(size_t openflow_len, uint8_t type,
475                         ovs_be32 xid, struct ofpbuf **);
476 void *make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
477                      struct ofpbuf **);
478
479 void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
480 void *put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
481                        struct ofpbuf *);
482
483 void *put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *);
484 void *put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
485                     struct ofpbuf *);
486
487 void update_openflow_length(struct ofpbuf *);
488
489 void *ofputil_make_stats_request(size_t openflow_len, uint16_t type,
490                                  uint32_t subtype, struct ofpbuf **);
491 void *ofputil_make_stats_reply(size_t openflow_len,
492                                const struct ofp_stats_msg *request,
493                                struct ofpbuf **);
494
495 void ofputil_start_stats_reply(const struct ofp_stats_msg *request,
496                                struct list *);
497 struct ofpbuf *ofputil_reserve_stats_reply(size_t len, struct list *);
498 void *ofputil_append_stats_reply(size_t len, struct list *);
499
500 const void *ofputil_stats_body(const struct ofp_header *);
501 size_t ofputil_stats_body_len(const struct ofp_header *);
502
503 const void *ofputil_nxstats_body(const struct ofp_header *);
504 size_t ofputil_nxstats_body_len(const struct ofp_header *);
505
506 struct ofpbuf *make_flow_mod(uint16_t command, const struct cls_rule *,
507                              size_t actions_len);
508 struct ofpbuf *make_add_flow(const struct cls_rule *, uint32_t buffer_id,
509                              uint16_t max_idle, size_t actions_len);
510 struct ofpbuf *make_del_flow(const struct cls_rule *);
511 struct ofpbuf *make_add_simple_flow(const struct cls_rule *,
512                                     uint32_t buffer_id, uint16_t out_port,
513                                     uint16_t max_idle);
514 struct ofpbuf *make_packet_in(uint32_t buffer_id, uint16_t in_port,
515                               uint8_t reason,
516                               const struct ofpbuf *payload, int max_send_len);
517 struct ofpbuf *make_echo_request(void);
518 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
519
520 struct ofpbuf *ofputil_encode_barrier_request(void);
521
522 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
523 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
524 \f
525 /* Actions. */
526
527 /* The type of an action.
528  *
529  * For each implemented OFPAT10_* and NXAST_* action type, there is a
530  * corresponding constant prefixed with OFPUTIL_, e.g.:
531  *
532  * OFPUTIL_OFPAT10_OUTPUT
533  * OFPUTIL_OFPAT10_SET_VLAN_VID
534  * OFPUTIL_OFPAT10_SET_VLAN_PCP
535  * OFPUTIL_OFPAT10_STRIP_VLAN
536  * OFPUTIL_OFPAT10_SET_DL_SRC
537  * OFPUTIL_OFPAT10_SET_DL_DST
538  * OFPUTIL_OFPAT10_SET_NW_SRC
539  * OFPUTIL_OFPAT10_SET_NW_DST
540  * OFPUTIL_OFPAT10_SET_NW_TOS
541  * OFPUTIL_OFPAT10_SET_TP_SRC
542  * OFPUTIL_OFPAT10_SET_TP_DST
543  * OFPUTIL_OFPAT10_ENQUEUE
544  * OFPUTIL_NXAST_RESUBMIT
545  * OFPUTIL_NXAST_SET_TUNNEL
546  * OFPUTIL_NXAST_SET_QUEUE
547  * OFPUTIL_NXAST_POP_QUEUE
548  * OFPUTIL_NXAST_REG_MOVE
549  * OFPUTIL_NXAST_REG_LOAD
550  * OFPUTIL_NXAST_NOTE
551  * OFPUTIL_NXAST_SET_TUNNEL64
552  * OFPUTIL_NXAST_MULTIPATH
553  * OFPUTIL_NXAST_AUTOPATH
554  * OFPUTIL_NXAST_BUNDLE
555  * OFPUTIL_NXAST_BUNDLE_LOAD
556  * OFPUTIL_NXAST_RESUBMIT_TABLE
557  * OFPUTIL_NXAST_OUTPUT_REG
558  * OFPUTIL_NXAST_LEARN
559  * OFPUTIL_NXAST_DEC_TTL
560  * OFPUTIL_NXAST_FIN_TIMEOUT
561  *
562  * (The above list helps developers who want to "grep" for these definitions.)
563  */
564 enum ofputil_action_code {
565 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             OFPUTIL_##ENUM,
566 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
567 #include "ofp-util.def"
568 };
569
570 /* The number of values of "enum ofputil_action_code". */
571 enum {
572 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             + 1
573 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
574     OFPUTIL_N_ACTIONS = 0
575 #include "ofp-util.def"
576 };
577
578 int ofputil_decode_action(const union ofp_action *);
579 enum ofputil_action_code ofputil_decode_action_unsafe(
580     const union ofp_action *);
581
582 int ofputil_action_code_from_name(const char *);
583
584 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
585
586 /* For each OpenFlow action <ENUM> that has a corresponding action structure
587  * struct <STRUCT>, this defines two functions:
588  *
589  *   void ofputil_init_<ENUM>(struct <STRUCT> *action);
590  *
591  *     Initializes the parts of 'action' that identify it as having type <ENUM>
592  *     and length 'sizeof *action' and zeros the rest.  For actions that have
593  *     variable length, the length used and cleared is that of struct <STRUCT>.
594  *
595  *  struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
596  *
597  *     Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
598  *     initializes it with ofputil_init_<ENUM>(), and returns it.
599  */
600 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)              \
601     void ofputil_init_##ENUM(struct STRUCT *);          \
602     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
603 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
604     void ofputil_init_##ENUM(struct STRUCT *);          \
605     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
606 #include "ofp-util.def"
607
608 #define OFP_ACTION_ALIGN 8      /* Alignment of ofp_actions. */
609
610 static inline union ofp_action *
611 ofputil_action_next(const union ofp_action *a)
612 {
613     return ((union ofp_action *) (void *)
614             ((uint8_t *) a + ntohs(a->header.len)));
615 }
616
617 static inline bool
618 ofputil_action_is_valid(const union ofp_action *a, size_t n_actions)
619 {
620     uint16_t len = ntohs(a->header.len);
621     return (!(len % OFP_ACTION_ALIGN)
622             && len >= sizeof *a
623             && len / sizeof *a <= n_actions);
624 }
625
626 /* This macro is careful to check for actions with bad lengths. */
627 #define OFPUTIL_ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS)         \
628     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
629          (LEFT) > 0 && ofputil_action_is_valid(ITER, LEFT);             \
630          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
631           (ITER) = ofputil_action_next(ITER)))
632
633 /* This macro does not check for actions with bad lengths.  It should only be
634  * used with actions from trusted sources or with actions that have already
635  * been validated (e.g. with OFPUTIL_ACTION_FOR_EACH).  */
636 #define OFPUTIL_ACTION_FOR_EACH_UNSAFE(ITER, LEFT, ACTIONS, N_ACTIONS)  \
637     for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS);                      \
638          (LEFT) > 0;                                                    \
639          ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
640           (ITER) = ofputil_action_next(ITER)))
641
642 enum ofperr validate_actions(const union ofp_action *, size_t n_actions,
643                              const struct flow *, int max_ports);
644 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
645
646 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
647                                  union ofp_action **, size_t *);
648
649 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
650                            const union ofp_action *b, size_t n_b);
651 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
652
653 /* Handy utility for parsing flows and actions. */
654 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
655
656 #endif /* ofp-util.h */