dpif-linux: Recheck the socket pointer existence before getting its pid.
[cascardo/ovs.git] / lib / ofp-util.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 <stdbool.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include "bitmap.h"
24 #include "compiler.h"
25 #include "flow.h"
26 #include "list.h"
27 #include "match.h"
28 #include "netdev.h"
29 #include "openflow/nicira-ext.h"
30 #include "openvswitch/types.h"
31 #include "type-props.h"
32
33 struct ofpbuf;
34 union ofp_action;
35 struct ofpact_set_field;
36
37 /* Port numbers. */
38 enum ofperr ofputil_port_from_ofp11(ovs_be32 ofp11_port,
39                                     ofp_port_t *ofp10_port);
40 ovs_be32 ofputil_port_to_ofp11(ofp_port_t ofp10_port);
41
42 bool ofputil_port_from_string(const char *, ofp_port_t *portp);
43 void ofputil_format_port(ofp_port_t port, struct ds *);
44 void ofputil_port_to_string(ofp_port_t, char namebuf[OFP_MAX_PORT_NAME_LEN],
45                             size_t bufsize);
46
47 /* Group numbers. */
48 enum { MAX_GROUP_NAME_LEN = INT_STRLEN(uint32_t) };
49 bool ofputil_group_from_string(const char *, uint32_t *group_id);
50 void ofputil_format_group(uint32_t group_id, struct ds *);
51 void ofputil_group_to_string(uint32_t group_id,
52                              char namebuf[MAX_GROUP_NAME_LEN + 1],
53                              size_t bufsize);
54
55 /* Converting OFPFW10_NW_SRC_MASK and OFPFW10_NW_DST_MASK wildcard bit counts
56  * to and from IP bitmasks. */
57 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
58 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
59
60 /* Protocols.
61  *
62  * A "protocol" is an OpenFlow version plus, for some OpenFlow versions,
63  * a bit extra about the flow match format in use.
64  *
65  * These are arranged from most portable to least portable, or alternatively
66  * from least powerful to most powerful.  Protocols earlier on the list are
67  * more likely to be understood for the purpose of making requests, but
68  * protocol later on the list are more likely to accurately describe a flow
69  * within a switch.
70  *
71  * On any given OpenFlow connection, a single protocol is in effect at any
72  * given time.  These values use separate bits only because that makes it easy
73  * to test whether a particular protocol is within a given set of protocols and
74  * to implement set union and intersection.
75  */
76 enum ofputil_protocol {
77     /* OpenFlow 1.0 protocols.
78      *
79      * The "STD" protocols use the standard OpenFlow 1.0 flow format.
80      * The "NXM" protocols use the Nicira Extensible Match (NXM) flow format.
81      *
82      * The protocols with "TID" mean that the nx_flow_mod_table_id Nicira
83      * extension has been enabled.  The other protocols have it disabled.
84      */
85 #define OFPUTIL_P_NONE 0
86     OFPUTIL_P_OF10_STD     = 1 << 0,
87     OFPUTIL_P_OF10_STD_TID = 1 << 1,
88     OFPUTIL_P_OF10_NXM     = 1 << 2,
89     OFPUTIL_P_OF10_NXM_TID = 1 << 3,
90 #define OFPUTIL_P_OF10_STD_ANY (OFPUTIL_P_OF10_STD | OFPUTIL_P_OF10_STD_TID)
91 #define OFPUTIL_P_OF10_NXM_ANY (OFPUTIL_P_OF10_NXM | OFPUTIL_P_OF10_NXM_TID)
92 #define OFPUTIL_P_OF10_ANY (OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY)
93
94     /* OpenFlow 1.1 protocol.
95      *
96      * We only support the standard OpenFlow 1.1 flow format.
97      *
98      * OpenFlow 1.1 always operates with an equivalent of the
99      * nx_flow_mod_table_id Nicira extension enabled, so there is no "TID"
100      * variant. */
101     OFPUTIL_P_OF11_STD     = 1 << 4,
102
103     /* OpenFlow 1.2+ protocols (only one variant each).
104      *
105      * These use the standard OpenFlow Extensible Match (OXM) flow format.
106      *
107      * OpenFlow 1.2+ always operates with an equivalent of the
108      * nx_flow_mod_table_id Nicira extension enabled, so there is no "TID"
109      * variant. */
110     OFPUTIL_P_OF12_OXM      = 1 << 5,
111     OFPUTIL_P_OF13_OXM      = 1 << 6,
112     OFPUTIL_P_OF14_OXM      = 1 << 7,
113     OFPUTIL_P_OF15_OXM      = 1 << 8,
114 #define OFPUTIL_P_ANY_OXM (OFPUTIL_P_OF12_OXM | \
115                            OFPUTIL_P_OF13_OXM | \
116                            OFPUTIL_P_OF14_OXM | \
117                            OFPUTIL_P_OF15_OXM)
118
119 #define OFPUTIL_P_NXM_OF11_UP (OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF11_STD | \
120                                OFPUTIL_P_ANY_OXM)
121
122 #define OFPUTIL_P_NXM_OXM_ANY (OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_ANY_OXM)
123
124 #define OFPUTIL_P_OF11_UP (OFPUTIL_P_OF11_STD | OFPUTIL_P_ANY_OXM)
125
126 #define OFPUTIL_P_OF12_UP (OFPUTIL_P_OF12_OXM | OFPUTIL_P_OF13_UP)
127 #define OFPUTIL_P_OF13_UP (OFPUTIL_P_OF13_OXM | OFPUTIL_P_OF14_UP)
128 #define OFPUTIL_P_OF14_UP (OFPUTIL_P_OF14_OXM | OFPUTIL_P_OF15_UP)
129 #define OFPUTIL_P_OF15_UP OFPUTIL_P_OF15_OXM
130
131     /* All protocols. */
132 #define OFPUTIL_P_ANY ((1 << 9) - 1)
133
134     /* Protocols in which a specific table may be specified in flow_mods. */
135 #define OFPUTIL_P_TID (OFPUTIL_P_OF10_STD_TID | \
136                        OFPUTIL_P_OF10_NXM_TID | \
137                        OFPUTIL_P_OF11_STD |     \
138                        OFPUTIL_P_ANY_OXM)
139 };
140
141 /* Protocols to use for flow dumps, from most to least preferred. */
142 extern enum ofputil_protocol ofputil_flow_dump_protocols[];
143 extern size_t ofputil_n_flow_dump_protocols;
144
145 enum ofputil_protocol ofputil_protocol_from_ofp_version(enum ofp_version);
146 enum ofputil_protocol ofputil_protocols_from_ofp_version(enum ofp_version);
147 enum ofp_version ofputil_protocol_to_ofp_version(enum ofputil_protocol);
148
149 bool ofputil_protocol_is_valid(enum ofputil_protocol);
150 enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
151                                                bool enable);
152 enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
153 enum ofputil_protocol ofputil_protocol_set_base(
154     enum ofputil_protocol cur, enum ofputil_protocol new_base);
155
156 const char *ofputil_protocol_to_string(enum ofputil_protocol);
157 char *ofputil_protocols_to_string(enum ofputil_protocol);
158 enum ofputil_protocol ofputil_protocols_from_string(const char *);
159
160 void ofputil_format_version(struct ds *, enum ofp_version);
161 void ofputil_format_version_name(struct ds *, enum ofp_version);
162
163 /* A bitmap of version numbers
164  *
165  * Bit offsets correspond to ofp_version numbers which in turn correspond to
166  * wire-protocol numbers for Open Flow versions..  E.g. (1u << OFP11_VERSION)
167  * is the mask for Open Flow 1.1.  If the bit for a version is set then it is
168  * allowed, otherwise it is disallowed. */
169
170 void ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap);
171 void ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap);
172
173 enum ofp_version ofputil_version_from_string(const char *s);
174
175 uint32_t ofputil_protocols_to_version_bitmap(enum ofputil_protocol);
176 enum ofputil_protocol ofputil_protocols_from_version_bitmap(uint32_t bitmap);
177
178 /* Bitmaps of OpenFlow versions that Open vSwitch supports, and that it enables
179  * by default.  When Open vSwitch has experimental or incomplete support for
180  * newer versions of OpenFlow, those versions should not be supported by
181  * default and thus should be omitted from the latter bitmap. */
182 #define OFPUTIL_SUPPORTED_VERSIONS ((1u << OFP10_VERSION) | \
183                                     (1u << OFP11_VERSION) | \
184                                     (1u << OFP12_VERSION) | \
185                                     (1u << OFP13_VERSION))
186 #define OFPUTIL_DEFAULT_VERSIONS OFPUTIL_SUPPORTED_VERSIONS
187
188 enum ofputil_protocol ofputil_protocols_from_string(const char *s);
189
190 const char *ofputil_version_to_string(enum ofp_version ofp_version);
191 uint32_t ofputil_versions_from_string(const char *s);
192 uint32_t ofputil_versions_from_strings(char ** const s, size_t count);
193
194 bool ofputil_decode_hello(const struct ofp_header *,
195                           uint32_t *allowed_versions);
196 struct ofpbuf *ofputil_encode_hello(uint32_t version_bitmap);
197
198 struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
199                                            enum ofputil_protocol want,
200                                            enum ofputil_protocol *next);
201
202 /* nx_flow_format */
203 struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
204 enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
205 bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
206 const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
207
208 /* Work with ofp10_match. */
209 void ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *);
210 void ofputil_match_from_ofp10_match(const struct ofp10_match *,
211                                     struct match *);
212 void ofputil_normalize_match(struct match *);
213 void ofputil_normalize_match_quiet(struct match *);
214 void ofputil_match_to_ofp10_match(const struct match *, struct ofp10_match *);
215
216 /* Work with ofp11_match. */
217 enum ofperr ofputil_pull_ofp11_match(struct ofpbuf *, struct match *,
218                                      uint16_t *padded_match_len);
219 enum ofperr ofputil_match_from_ofp11_match(const struct ofp11_match *,
220                                            struct match *);
221 int ofputil_put_ofp11_match(struct ofpbuf *, const struct match *,
222                             enum ofputil_protocol);
223 void ofputil_match_to_ofp11_match(const struct match *, struct ofp11_match *);
224 int ofputil_match_typical_len(enum ofputil_protocol);
225
226 /* dl_type translation between OpenFlow and 'struct flow' format. */
227 ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
228 ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
229
230 /* PACKET_IN. */
231 bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format);
232 int ofputil_packet_in_format_from_string(const char *);
233 const char *ofputil_packet_in_format_to_string(enum nx_packet_in_format);
234 struct ofpbuf *ofputil_make_set_packet_in_format(enum ofp_version,
235                                                  enum nx_packet_in_format);
236
237 /* NXT_FLOW_MOD_TABLE_ID extension. */
238 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
239
240 /* Protocol-independent flow_mod flags. */
241 enum ofputil_flow_mod_flags {
242     /* Flags that are maintained with a flow as part of its state.
243      *
244      * (OFPUTIL_FF_EMERG would be here too, if OVS supported it.) */
245     OFPUTIL_FF_SEND_FLOW_REM = 1 << 0, /* All versions. */
246     OFPUTIL_FF_NO_PKT_COUNTS = 1 << 1, /* OpenFlow 1.3+. */
247     OFPUTIL_FF_NO_BYT_COUNTS = 1 << 2, /* OpenFlow 1.3+. */
248 #define OFPUTIL_FF_STATE (OFPUTIL_FF_SEND_FLOW_REM      \
249                           | OFPUTIL_FF_NO_PKT_COUNTS    \
250                           | OFPUTIL_FF_NO_BYT_COUNTS)
251
252     /* Flags that affect flow_mod behavior but are not part of flow state. */
253     OFPUTIL_FF_CHECK_OVERLAP = 1 << 3, /* All versions. */
254     OFPUTIL_FF_EMERG         = 1 << 4, /* OpenFlow 1.0 only. */
255     OFPUTIL_FF_RESET_COUNTS  = 1 << 5, /* OpenFlow 1.2+. */
256
257     /* Flags that are only set by OVS for its internal use.  Cannot be set via
258      * OpenFlow. */
259     OFPUTIL_FF_HIDDEN_FIELDS = 1 << 6, /* Allow hidden match fields to be
260                                           set or modified. */
261     OFPUTIL_FF_NO_READONLY   = 1 << 7, /* Allow rules within read only tables
262                                           to be modified */
263 };
264
265 /* Protocol-independent flow_mod.
266  *
267  * The handling of cookies across multiple versions of OpenFlow is a bit
268  * confusing.  See DESIGN for the details. */
269 struct ofputil_flow_mod {
270     struct list list_node;    /* For queuing flow_mods. */
271
272     struct match match;
273     unsigned int priority;
274
275     /* Cookie matching.  The flow_mod affects only flows that have cookies that
276      * bitwise match 'cookie' bits in positions where 'cookie_mask has 1-bits.
277      *
278      * 'cookie_mask' should be zero for OFPFC_ADD flow_mods. */
279     ovs_be64 cookie;         /* Cookie bits to match. */
280     ovs_be64 cookie_mask;    /* 1-bit in each 'cookie' bit to match. */
281
282     /* Cookie changes.
283      *
284      * OFPFC_ADD uses 'new_cookie' as the new flow's cookie.  'new_cookie'
285      * should not be UINT64_MAX.
286      *
287      * OFPFC_MODIFY and OFPFC_MODIFY_STRICT have two cases:
288      *
289      *   - If one or more matching flows exist and 'modify_cookie' is true,
290      *     then the flow_mod changes the existing flows' cookies to
291      *     'new_cookie'.  'new_cookie' should not be UINT64_MAX.
292      *
293      *   - If no matching flow exists, 'new_cookie' is not UINT64_MAX, and
294      *     'cookie_mask' is 0, then the flow_mod adds a new flow with
295      *     'new_cookie' as its cookie.
296      */
297     ovs_be64 new_cookie;     /* New cookie to install or UINT64_MAX. */
298     bool modify_cookie;      /* Set cookie of existing flow to 'new_cookie'? */
299
300     uint8_t table_id;
301     uint16_t command;
302     uint16_t idle_timeout;
303     uint16_t hard_timeout;
304     uint32_t buffer_id;
305     ofp_port_t out_port;
306     uint32_t out_group;
307     enum ofputil_flow_mod_flags flags;
308     struct ofpact *ofpacts;  /* Series of "struct ofpact"s. */
309     size_t ofpacts_len;      /* Length of ofpacts, in bytes. */
310 };
311
312 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
313                                     const struct ofp_header *,
314                                     enum ofputil_protocol,
315                                     struct ofpbuf *ofpacts,
316                                     ofp_port_t max_port,
317                                     uint8_t max_table);
318 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
319                                        enum ofputil_protocol);
320
321 /* Flow stats or aggregate stats request, independent of protocol. */
322 struct ofputil_flow_stats_request {
323     bool aggregate;             /* Aggregate results? */
324     struct match match;
325     ovs_be64 cookie;
326     ovs_be64 cookie_mask;
327     ofp_port_t out_port;
328     uint32_t out_group;
329     uint8_t table_id;
330 };
331
332 enum ofperr ofputil_decode_flow_stats_request(
333     struct ofputil_flow_stats_request *, const struct ofp_header *);
334 struct ofpbuf *ofputil_encode_flow_stats_request(
335     const struct ofputil_flow_stats_request *, enum ofputil_protocol);
336
337 /* Flow stats reply, independent of protocol. */
338 struct ofputil_flow_stats {
339     struct match match;
340     ovs_be64 cookie;
341     uint8_t table_id;
342     uint16_t priority;
343     uint16_t idle_timeout;
344     uint16_t hard_timeout;
345     uint32_t duration_sec;
346     uint32_t duration_nsec;
347     int idle_age;               /* Seconds since last packet, -1 if unknown. */
348     int hard_age;               /* Seconds since last change, -1 if unknown. */
349     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
350     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
351     const struct ofpact *ofpacts;
352     size_t ofpacts_len;
353     enum ofputil_flow_mod_flags flags;
354 };
355
356 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
357                                     struct ofpbuf *msg,
358                                     bool flow_age_extension,
359                                     struct ofpbuf *ofpacts);
360 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
361                                      struct list *replies);
362
363 /* Aggregate stats reply, independent of protocol. */
364 struct ofputil_aggregate_stats {
365     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
366     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
367     uint32_t flow_count;
368 };
369
370 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
371     const struct ofputil_aggregate_stats *stats,
372     const struct ofp_header *request);
373 enum ofperr ofputil_decode_aggregate_stats_reply(
374     struct ofputil_aggregate_stats *,
375     const struct ofp_header *reply);
376
377 /* Flow removed message, independent of protocol. */
378 struct ofputil_flow_removed {
379     struct match match;
380     ovs_be64 cookie;
381     uint16_t priority;
382     uint8_t reason;             /* One of OFPRR_*. */
383     uint8_t table_id;           /* 255 if message didn't include table ID. */
384     uint32_t duration_sec;
385     uint32_t duration_nsec;
386     uint16_t idle_timeout;
387     uint16_t hard_timeout;
388     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
389     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
390 };
391
392 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
393                                         const struct ofp_header *);
394 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
395                                            enum ofputil_protocol);
396
397 /* Abstract packet-in message. */
398 struct ofputil_packet_in {
399     /* Packet data and metadata.
400      *
401      * To save bandwidth, in some cases a switch may send only the first
402      * several bytes of a packet, indicated by 'packet_len < total_len'.  When
403      * the full packet is included, 'packet_len == total_len'. */
404     const void *packet;
405     size_t packet_len;          /* Number of bytes in 'packet'. */
406     size_t total_len;           /* Size of packet, pre-truncation. */
407     struct flow_metadata fmd;
408
409     /* Identifies a buffer in the switch that contains the full packet, to
410      * allow the controller to reference it later without having to send the
411      * entire packet back to the switch.
412      *
413      * UINT32_MAX indicates that the packet is not buffered in the switch.  A
414      * switch should only use UINT32_MAX when it sends the entire packet. */
415     uint32_t buffer_id;
416
417     /* Reason that the packet-in is being sent. */
418     enum ofp_packet_in_reason reason;    /* One of OFPR_*. */
419
420     /* Information about the OpenFlow flow that triggered the packet-in.
421      *
422      * A packet-in triggered by a flow table miss has no associated flow.  In
423      * that case, 'cookie' is UINT64_MAX. */
424     uint8_t table_id;                    /* OpenFlow table ID. */
425     ovs_be64 cookie;                     /* Flow's cookie. */
426 };
427
428 enum ofperr ofputil_decode_packet_in(struct ofputil_packet_in *,
429                                      const struct ofp_header *);
430 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
431                                         enum ofputil_protocol protocol,
432                                         enum nx_packet_in_format);
433
434 enum { OFPUTIL_PACKET_IN_REASON_BUFSIZE = INT_STRLEN(int) + 1 };
435 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason,
436                                                char *reasonbuf,
437                                                size_t bufsize);
438 bool ofputil_packet_in_reason_from_string(const char *,
439                                           enum ofp_packet_in_reason *);
440
441 /* Abstract packet-out message.
442  *
443  * ofputil_decode_packet_out() will ensure that 'in_port' is a physical port
444  * (OFPP_MAX or less) or one of OFPP_LOCAL, OFPP_NONE, or OFPP_CONTROLLER. */
445 struct ofputil_packet_out {
446     const void *packet;         /* Packet data, if buffer_id == UINT32_MAX. */
447     size_t packet_len;          /* Length of packet data in bytes. */
448     uint32_t buffer_id;         /* Buffer id or UINT32_MAX if no buffer. */
449     ofp_port_t in_port;         /* Packet's input port. */
450     struct ofpact *ofpacts;     /* Actions. */
451     size_t ofpacts_len;         /* Size of ofpacts in bytes. */
452 };
453
454 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
455                                       const struct ofp_header *,
456                                       struct ofpbuf *ofpacts);
457 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *,
458                                          enum ofputil_protocol protocol);
459
460 enum ofputil_port_config {
461     /* OpenFlow 1.0 and 1.1 share these values for these port config bits. */
462     OFPUTIL_PC_PORT_DOWN    = 1 << 0, /* Port is administratively down. */
463     OFPUTIL_PC_NO_RECV      = 1 << 2, /* Drop all packets received by port. */
464     OFPUTIL_PC_NO_FWD       = 1 << 5, /* Drop packets forwarded to port. */
465     OFPUTIL_PC_NO_PACKET_IN = 1 << 6, /* No send packet-in msgs for port. */
466     /* OpenFlow 1.0 only. */
467     OFPUTIL_PC_NO_STP       = 1 << 1, /* No 802.1D spanning tree for port. */
468     OFPUTIL_PC_NO_RECV_STP  = 1 << 3, /* Drop received 802.1D STP packets. */
469     OFPUTIL_PC_NO_FLOOD     = 1 << 4, /* Do not include port when flooding. */
470     /* There are no OpenFlow 1.1-only bits. */
471 };
472
473 enum ofputil_port_state {
474     /* OpenFlow 1.0 and 1.1 share this values for these port state bits. */
475     OFPUTIL_PS_LINK_DOWN   = 1 << 0, /* No physical link present. */
476     /* OpenFlow 1.1 only. */
477     OFPUTIL_PS_BLOCKED     = 1 << 1, /* Port is blocked */
478     OFPUTIL_PS_LIVE        = 1 << 2, /* Live for Fast Failover Group. */
479     /* OpenFlow 1.0 only. */
480     OFPUTIL_PS_STP_LISTEN  = 0 << 8, /* Not learning or relaying frames. */
481     OFPUTIL_PS_STP_LEARN   = 1 << 8, /* Learning but not relaying frames. */
482     OFPUTIL_PS_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
483     OFPUTIL_PS_STP_BLOCK   = 3 << 8, /* Not part of spanning tree. */
484     OFPUTIL_PS_STP_MASK    = 3 << 8  /* Bit mask for OFPPS10_STP_* values. */
485 };
486
487 /* Abstract ofp10_phy_port or ofp11_port. */
488 struct ofputil_phy_port {
489     ofp_port_t port_no;
490     uint8_t hw_addr[OFP_ETH_ALEN];
491     char name[OFP_MAX_PORT_NAME_LEN];
492     enum ofputil_port_config config;
493     enum ofputil_port_state state;
494
495     /* NETDEV_F_* feature bitmasks. */
496     enum netdev_features curr;       /* Current features. */
497     enum netdev_features advertised; /* Features advertised by the port. */
498     enum netdev_features supported;  /* Features supported by the port. */
499     enum netdev_features peer;       /* Features advertised by peer. */
500
501     /* Speed. */
502     uint32_t curr_speed;        /* Current speed, in kbps. */
503     uint32_t max_speed;         /* Maximum supported speed, in kbps. */
504 };
505
506 enum ofputil_capabilities {
507     /* OpenFlow 1.0, 1.1, 1.2, and 1.3 share these capability values. */
508     OFPUTIL_C_FLOW_STATS     = 1 << 0,  /* Flow statistics. */
509     OFPUTIL_C_TABLE_STATS    = 1 << 1,  /* Table statistics. */
510     OFPUTIL_C_PORT_STATS     = 1 << 2,  /* Port statistics. */
511     OFPUTIL_C_IP_REASM       = 1 << 5,  /* Can reassemble IP fragments. */
512     OFPUTIL_C_QUEUE_STATS    = 1 << 6,  /* Queue statistics. */
513
514     /* OpenFlow 1.0 and 1.1 share this capability. */
515     OFPUTIL_C_ARP_MATCH_IP   = 1 << 7,  /* Match IP addresses in ARP pkts. */
516
517     /* OpenFlow 1.0 only. */
518     OFPUTIL_C_STP            = 1 << 3,  /* 802.1d spanning tree. */
519
520     /* OpenFlow 1.1, 1.2, and 1.3 share this capability. */
521     OFPUTIL_C_GROUP_STATS    = 1 << 4,  /* Group statistics. */
522
523     /* OpenFlow 1.2 and 1.3 share this capability */
524     OFPUTIL_C_PORT_BLOCKED   = 1 << 8,  /* Switch will block looping ports */
525 };
526
527 enum ofputil_action_bitmap {
528     OFPUTIL_A_OUTPUT         = 1 << 0,
529     OFPUTIL_A_SET_VLAN_VID   = 1 << 1,
530     OFPUTIL_A_SET_VLAN_PCP   = 1 << 2,
531     OFPUTIL_A_STRIP_VLAN     = 1 << 3,
532     OFPUTIL_A_SET_DL_SRC     = 1 << 4,
533     OFPUTIL_A_SET_DL_DST     = 1 << 5,
534     OFPUTIL_A_SET_NW_SRC     = 1 << 6,
535     OFPUTIL_A_SET_NW_DST     = 1 << 7,
536     OFPUTIL_A_SET_NW_ECN     = 1 << 8,
537     OFPUTIL_A_SET_NW_TOS     = 1 << 9,
538     OFPUTIL_A_SET_TP_SRC     = 1 << 10,
539     OFPUTIL_A_SET_TP_DST     = 1 << 11,
540     OFPUTIL_A_ENQUEUE        = 1 << 12,
541     OFPUTIL_A_COPY_TTL_OUT   = 1 << 13,
542     OFPUTIL_A_COPY_TTL_IN    = 1 << 14,
543     OFPUTIL_A_SET_MPLS_LABEL = 1 << 15,
544     OFPUTIL_A_SET_MPLS_TC    = 1 << 16,
545     OFPUTIL_A_SET_MPLS_TTL   = 1 << 17,
546     OFPUTIL_A_DEC_MPLS_TTL   = 1 << 18,
547     OFPUTIL_A_PUSH_VLAN      = 1 << 19,
548     OFPUTIL_A_POP_VLAN       = 1 << 20,
549     OFPUTIL_A_PUSH_MPLS      = 1 << 21,
550     OFPUTIL_A_POP_MPLS       = 1 << 22,
551     OFPUTIL_A_SET_QUEUE      = 1 << 23,
552     OFPUTIL_A_GROUP          = 1 << 24,
553     OFPUTIL_A_SET_NW_TTL     = 1 << 25,
554     OFPUTIL_A_DEC_NW_TTL     = 1 << 26,
555     OFPUTIL_A_SET_FIELD      = 1 << 27,
556 };
557
558 /* Abstract ofp_switch_features. */
559 struct ofputil_switch_features {
560     uint64_t datapath_id;       /* Datapath unique ID. */
561     uint32_t n_buffers;         /* Max packets buffered at once. */
562     uint8_t n_tables;           /* Number of tables supported by datapath. */
563     uint8_t auxiliary_id;       /* Identify auxiliary connections */
564     enum ofputil_capabilities capabilities;
565     enum ofputil_action_bitmap actions;
566 };
567
568 enum ofperr ofputil_decode_switch_features(const struct ofp_header *,
569                                            struct ofputil_switch_features *,
570                                            struct ofpbuf *);
571
572 struct ofpbuf *ofputil_encode_switch_features(
573     const struct ofputil_switch_features *, enum ofputil_protocol,
574     ovs_be32 xid);
575 void ofputil_put_switch_features_port(const struct ofputil_phy_port *,
576                                       struct ofpbuf *);
577 bool ofputil_switch_features_has_ports(struct ofpbuf *b);
578
579 /* phy_port helper functions. */
580 int ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *,
581                           struct ofputil_phy_port *);
582
583 /* Abstract ofp_port_status. */
584 struct ofputil_port_status {
585     enum ofp_port_reason reason;
586     struct ofputil_phy_port desc;
587 };
588
589 enum ofperr ofputil_decode_port_status(const struct ofp_header *,
590                                        struct ofputil_port_status *);
591 struct ofpbuf *ofputil_encode_port_status(const struct ofputil_port_status *,
592                                           enum ofputil_protocol);
593
594 /* Abstract ofp_port_mod. */
595 struct ofputil_port_mod {
596     ofp_port_t port_no;
597     uint8_t hw_addr[OFP_ETH_ALEN];
598     enum ofputil_port_config config;
599     enum ofputil_port_config mask;
600     enum netdev_features advertise;
601 };
602
603 enum ofperr ofputil_decode_port_mod(const struct ofp_header *,
604                                     struct ofputil_port_mod *, bool loose);
605 struct ofpbuf *ofputil_encode_port_mod(const struct ofputil_port_mod *,
606                                        enum ofputil_protocol);
607
608 /* Abstract ofp_table_mod. */
609 struct ofputil_table_mod {
610     uint8_t table_id;         /* ID of the table, 0xff indicates all tables. */
611     enum ofp_table_config config;
612 };
613
614 enum ofperr ofputil_decode_table_mod(const struct ofp_header *,
615                                     struct ofputil_table_mod *);
616 struct ofpbuf *ofputil_encode_table_mod(const struct ofputil_table_mod *,
617                                        enum ofputil_protocol);
618
619 /* Abstract ofp_table_features. */
620 struct ofputil_table_features {
621     uint8_t table_id;         /* Identifier of table. Lower numbered tables
622                                  are consulted first. */
623     char name[OFP_MAX_TABLE_NAME_LEN];
624     ovs_be64 metadata_match;  /* Bits of metadata table can match. */
625     ovs_be64 metadata_write;  /* Bits of metadata table can write. */
626     uint32_t config;          /* Bitmap of OFPTC_* values */
627     uint32_t max_entries;     /* Max number of entries supported. */
628
629     /* Table features related to instructions.  There are two instances:
630      *
631      *   - 'miss' reports features available in the table miss flow.
632      *
633      *   - 'nonmiss' reports features available in other flows. */
634     struct ofputil_table_instruction_features {
635         /* Tables that "goto-table" may jump to. */
636         unsigned long int next[BITMAP_N_LONGS(255)];
637
638         /* Bitmap of OVSINST_* for supported instructions. */
639         uint32_t instructions;
640
641         /* Table features related to actions.  There are two instances:
642          *
643          *    - 'write' reports features available in a "write_actions"
644          *      instruction.
645          *
646          *    - 'apply' reports features available in an "apply_actions"
647          *      instruction. */
648         struct ofputil_table_action_features {
649             uint32_t actions;     /* Bitmap of supported OFPAT*. */
650             uint64_t set_fields;  /* Bitmap of MFF_* "set-field" supports. */
651         } write, apply;
652     } nonmiss, miss;
653
654     /* MFF_* bitmaps.
655      *
656      * For any given field the following combinations are valid:
657      *
658      *    - match=0, wildcard=0, mask=0: Flows in this table cannot match on
659      *      this field.
660      *
661      *    - match=1, wildcard=0, mask=0: Flows in this table must match on all
662      *      the bits in this field.
663      *
664      *    - match=1, wildcard=1, mask=0: Flows in this table must either match
665      *      on all the bits in the field or wildcard the field entirely.
666      *
667      *    - match=1, wildcard=1, mask=1: Flows in this table may arbitrarily
668      *      mask this field (as special cases, they may match on all the bits
669      *      or wildcard it entirely).
670      *
671      * Other combinations do not make sense.
672      */
673     uint64_t match;             /* Fields that may be matched. */
674     uint64_t mask;              /* Subset of 'match' that may have masks. */
675     uint64_t wildcard;          /* Subset of 'match' that may be wildcarded. */
676 };
677
678 int ofputil_decode_table_features(struct ofpbuf *,
679                                   struct ofputil_table_features *, bool loose);
680 struct ofpbuf *ofputil_encode_table_features_request(
681                             enum ofp_version ofp_version);
682 void ofputil_append_table_features_reply(
683                             const struct ofputil_table_features *tf,
684                             struct list *replies);
685
686 uint16_t table_feature_prop_get_size(enum ofp13_table_feature_prop_type type);
687 char *table_feature_prop_get_name(enum ofp13_table_feature_prop_type type);
688
689 /* Meter band configuration for all supported band types. */
690 struct ofputil_meter_band {
691     uint16_t type;
692     uint8_t prec_level;         /* Non-zero if type == OFPMBT_DSCP_REMARK. */
693     uint32_t rate;
694     uint32_t burst_size;
695 };
696
697 struct ofputil_meter_band_stats {
698     uint64_t packet_count;
699     uint64_t byte_count;
700 };
701
702 struct ofputil_meter_config {
703     uint32_t meter_id;
704     uint16_t flags;
705     uint16_t n_bands;
706     struct ofputil_meter_band *bands;
707 };
708
709 /* Abstract ofp_meter_mod. */
710 struct ofputil_meter_mod {
711     uint16_t command;
712     struct ofputil_meter_config meter;
713 };
714
715 struct ofputil_meter_stats {
716     uint32_t meter_id;
717     uint32_t flow_count;
718     uint64_t packet_in_count;
719     uint64_t byte_in_count;
720     uint32_t duration_sec;
721     uint32_t duration_nsec;
722     uint16_t n_bands;
723     struct ofputil_meter_band_stats *bands;
724 };
725
726 struct ofputil_meter_features {
727     uint32_t max_meters;        /* Maximum number of meters. */
728     uint32_t band_types;        /* Can support max 32 band types. */
729     uint32_t capabilities;      /* Supported flags. */
730     uint8_t  max_bands;
731     uint8_t  max_color;
732 };
733
734 enum ofperr ofputil_decode_meter_mod(const struct ofp_header *,
735                                      struct ofputil_meter_mod *,
736                                      struct ofpbuf *bands);
737 struct ofpbuf *ofputil_encode_meter_mod(enum ofp_version,
738                                         const struct ofputil_meter_mod *);
739
740 void ofputil_decode_meter_features(const struct ofp_header *,
741                                    struct ofputil_meter_features *);
742 struct ofpbuf *ofputil_encode_meter_features_reply(const struct
743                                                    ofputil_meter_features *,
744                                                    const struct ofp_header *
745                                                    request);
746 void ofputil_decode_meter_request(const struct ofp_header *,
747                                   uint32_t *meter_id);
748
749 void ofputil_append_meter_config(struct list *replies,
750                                  const struct ofputil_meter_config *);
751
752 void ofputil_append_meter_stats(struct list *replies,
753                                 const struct ofputil_meter_stats *);
754
755 enum ofputil_meter_request_type {
756     OFPUTIL_METER_FEATURES,
757     OFPUTIL_METER_CONFIG,
758     OFPUTIL_METER_STATS
759 };
760
761 struct ofpbuf *ofputil_encode_meter_request(enum ofp_version,
762                                             enum ofputil_meter_request_type,
763                                             uint32_t meter_id);
764
765 int ofputil_decode_meter_stats(struct ofpbuf *,
766                                struct ofputil_meter_stats *,
767                                struct ofpbuf *bands);
768
769 int ofputil_decode_meter_config(struct ofpbuf *,
770                                 struct ofputil_meter_config *,
771                                 struct ofpbuf *bands);
772
773 /* Type for meter_id in ofproto provider interface, UINT32_MAX if invalid. */
774 typedef struct { uint32_t uint32; } ofproto_meter_id;
775
776 /* Abstract ofp_role_request and reply. */
777 struct ofputil_role_request {
778     enum ofp12_controller_role role;
779     bool have_generation_id;
780     uint64_t generation_id;
781 };
782
783 struct ofputil_role_status {
784     enum ofp12_controller_role role;
785     enum ofp14_controller_role_reason reason;
786     uint64_t generation_id;
787 };
788
789 enum ofperr ofputil_decode_role_message(const struct ofp_header *,
790                                         struct ofputil_role_request *);
791 struct ofpbuf *ofputil_encode_role_reply(const struct ofp_header *,
792                                          const struct ofputil_role_request *);
793
794 struct ofpbuf *ofputil_encode_role_status(
795                                 const struct ofputil_role_status *status,
796                                 enum ofputil_protocol protocol);
797
798 enum ofperr ofputil_decode_role_status(const struct ofp_header *oh,
799                                        struct ofputil_role_status *rs);
800 /* Abstract table stats.
801  *
802  * For now we use ofp12_table_stats as a superset of the other protocol
803  * versions' table stats. */
804
805 struct ofpbuf *ofputil_encode_table_stats_reply(
806     const struct ofp12_table_stats[], int n,
807     const struct ofp_header *request);
808
809 /* Queue configuration request. */
810 struct ofpbuf *ofputil_encode_queue_get_config_request(enum ofp_version,
811                                                        ofp_port_t port);
812 enum ofperr ofputil_decode_queue_get_config_request(const struct ofp_header *,
813                                                     ofp_port_t *port);
814
815 /* Queue configuration reply. */
816 struct ofputil_queue_config {
817     uint32_t queue_id;
818
819     /* Each of these optional values is expressed in tenths of a percent.
820      * Values greater than 1000 indicate that the feature is disabled.
821      * UINT16_MAX indicates that the value is omitted. */
822     uint16_t min_rate;
823     uint16_t max_rate;
824 };
825
826 struct ofpbuf *ofputil_encode_queue_get_config_reply(
827     const struct ofp_header *request);
828 void ofputil_append_queue_get_config_reply(
829     struct ofpbuf *reply, const struct ofputil_queue_config *);
830
831 enum ofperr ofputil_decode_queue_get_config_reply(struct ofpbuf *reply,
832                                                   ofp_port_t *);
833 int ofputil_pull_queue_get_config_reply(struct ofpbuf *reply,
834                                         struct ofputil_queue_config *);
835
836
837 /* Abstract nx_flow_monitor_request. */
838 struct ofputil_flow_monitor_request {
839     uint32_t id;
840     enum nx_flow_monitor_flags flags;
841     ofp_port_t out_port;
842     uint8_t table_id;
843     struct match match;
844 };
845
846 int ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *,
847                                         struct ofpbuf *msg);
848 void ofputil_append_flow_monitor_request(
849     const struct ofputil_flow_monitor_request *, struct ofpbuf *msg);
850
851 /* Abstract nx_flow_update. */
852 struct ofputil_flow_update {
853     enum nx_flow_update_event event;
854
855     /* Used only for NXFME_ADDED, NXFME_DELETED, NXFME_MODIFIED. */
856     enum ofp_flow_removed_reason reason;
857     uint16_t idle_timeout;
858     uint16_t hard_timeout;
859     uint8_t table_id;
860     uint16_t priority;
861     ovs_be64 cookie;
862     struct match *match;
863     const struct ofpact *ofpacts;
864     size_t ofpacts_len;
865
866     /* Used only for NXFME_ABBREV. */
867     ovs_be32 xid;
868 };
869
870 int ofputil_decode_flow_update(struct ofputil_flow_update *,
871                                struct ofpbuf *msg, struct ofpbuf *ofpacts);
872 void ofputil_start_flow_update(struct list *replies);
873 void ofputil_append_flow_update(const struct ofputil_flow_update *,
874                                 struct list *replies);
875
876 /* Abstract nx_flow_monitor_cancel. */
877 uint32_t ofputil_decode_flow_monitor_cancel(const struct ofp_header *);
878 struct ofpbuf *ofputil_encode_flow_monitor_cancel(uint32_t id);
879
880 /* Port desc stats requests and replies. */
881 enum ofperr ofputil_decode_port_desc_stats_request(const struct ofp_header *,
882                                                    ofp_port_t *portp);
883 struct ofpbuf *ofputil_encode_port_desc_stats_request(
884     enum ofp_version ofp_version, ofp_port_t);
885
886 void ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
887                                           struct list *replies);
888
889 /* Encoding simple OpenFlow messages. */
890 struct ofpbuf *make_echo_request(enum ofp_version);
891 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
892
893 struct ofpbuf *ofputil_encode_barrier_request(enum ofp_version);
894
895 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
896 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
897
898 \f
899 /* Actions. */
900
901 /* The type of an action.
902  *
903  * For each implemented OFPAT10_* and NXAST_* action type, there is a
904  * corresponding constant prefixed with OFPUTIL_, e.g.:
905  *
906  * OFPUTIL_OFPAT10_OUTPUT
907  * OFPUTIL_OFPAT10_SET_VLAN_VID
908  * OFPUTIL_OFPAT10_SET_VLAN_PCP
909  * OFPUTIL_OFPAT10_STRIP_VLAN
910  * OFPUTIL_OFPAT10_SET_DL_SRC
911  * OFPUTIL_OFPAT10_SET_DL_DST
912  * OFPUTIL_OFPAT10_SET_NW_SRC
913  * OFPUTIL_OFPAT10_SET_NW_DST
914  * OFPUTIL_OFPAT10_SET_NW_TOS
915  * OFPUTIL_OFPAT10_SET_TP_SRC
916  * OFPUTIL_OFPAT10_SET_TP_DST
917  * OFPUTIL_OFPAT10_ENQUEUE
918  * OFPUTIL_NXAST_RESUBMIT
919  * OFPUTIL_NXAST_SET_TUNNEL
920  * OFPUTIL_NXAST_SET_METADATA
921  * OFPUTIL_NXAST_SET_QUEUE
922  * OFPUTIL_NXAST_POP_QUEUE
923  * OFPUTIL_NXAST_REG_MOVE
924  * OFPUTIL_NXAST_REG_LOAD
925  * OFPUTIL_NXAST_NOTE
926  * OFPUTIL_NXAST_SET_TUNNEL64
927  * OFPUTIL_NXAST_MULTIPATH
928  * OFPUTIL_NXAST_BUNDLE
929  * OFPUTIL_NXAST_BUNDLE_LOAD
930  * OFPUTIL_NXAST_RESUBMIT_TABLE
931  * OFPUTIL_NXAST_OUTPUT_REG
932  * OFPUTIL_NXAST_LEARN
933  * OFPUTIL_NXAST_DEC_TTL
934  * OFPUTIL_NXAST_FIN_TIMEOUT
935  *
936  * (The above list helps developers who want to "grep" for these definitions.)
937  */
938 enum OVS_PACKED_ENUM ofputil_action_code {
939     OFPUTIL_ACTION_INVALID,
940 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             OFPUTIL_##ENUM,
941 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
942 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
943 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   OFPUTIL_##ENUM,
944 #include "ofp-util.def"
945 };
946
947 /* The number of values of "enum ofputil_action_code". */
948 enum {
949 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             + 1
950 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
951 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
952 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   + 1
953     OFPUTIL_N_ACTIONS = 1
954 #include "ofp-util.def"
955 };
956
957 int ofputil_action_code_from_name(const char *);
958 const char * ofputil_action_name_from_code(enum ofputil_action_code code);
959 enum ofputil_action_code ofputil_action_code_from_ofp13_action(
960     enum ofp13_action_type type);
961
962 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
963
964 /* For each OpenFlow action <ENUM> that has a corresponding action structure
965  * struct <STRUCT>, this defines two functions:
966  *
967  *   void ofputil_init_<ENUM>(struct <STRUCT> *action);
968  *
969  *     Initializes the parts of 'action' that identify it as having type <ENUM>
970  *     and length 'sizeof *action' and zeros the rest.  For actions that have
971  *     variable length, the length used and cleared is that of struct <STRUCT>.
972  *
973  *  struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
974  *
975  *     Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
976  *     initializes it with ofputil_init_<ENUM>(), and returns it.
977  */
978 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)              \
979     void ofputil_init_##ENUM(struct STRUCT *);          \
980     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
981 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
982     void ofputil_init_##ENUM(struct STRUCT *);          \
983     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
984 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
985     void ofputil_init_##ENUM(struct STRUCT *);          \
986     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
987 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
988     void ofputil_init_##ENUM(struct STRUCT *);          \
989     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
990 #include "ofp-util.def"
991
992 #define OFP_ACTION_ALIGN 8      /* Alignment of ofp_actions. */
993
994 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
995
996 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
997                                  union ofp_action **, size_t *);
998
999 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
1000                            const union ofp_action *b, size_t n_b);
1001 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
1002
1003 /* Handy utility for parsing flows and actions. */
1004 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
1005
1006 struct ofputil_port_stats {
1007     ofp_port_t port_no;
1008     struct netdev_stats stats;
1009     uint32_t duration_sec;      /* UINT32_MAX if unknown. */
1010     uint32_t duration_nsec;
1011 };
1012
1013 struct ofpbuf *ofputil_encode_dump_ports_request(enum ofp_version ofp_version,
1014                                                  ofp_port_t port);
1015 void ofputil_append_port_stat(struct list *replies,
1016                               const struct ofputil_port_stats *ops);
1017 size_t ofputil_count_port_stats(const struct ofp_header *);
1018 int ofputil_decode_port_stats(struct ofputil_port_stats *, struct ofpbuf *msg);
1019 enum ofperr ofputil_decode_port_stats_request(const struct ofp_header *request,
1020                                               ofp_port_t *ofp10_port);
1021
1022 struct ofputil_queue_stats_request {
1023     ofp_port_t port_no;           /* OFPP_ANY means "all ports". */
1024     uint32_t queue_id;
1025 };
1026
1027 enum ofperr
1028 ofputil_decode_queue_stats_request(const struct ofp_header *request,
1029                                    struct ofputil_queue_stats_request *oqsr);
1030 struct ofpbuf *
1031 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
1032                                    const struct ofputil_queue_stats_request *oqsr);
1033
1034 struct ofputil_queue_stats {
1035     ofp_port_t port_no;
1036     uint32_t queue_id;
1037
1038     /* Values of unsupported statistics are set to all-1-bits (UINT64_MAX). */
1039     uint64_t tx_bytes;
1040     uint64_t tx_packets;
1041     uint64_t tx_errors;
1042
1043     /* UINT32_MAX if unknown. */
1044     uint32_t duration_sec;
1045     uint32_t duration_nsec;
1046 };
1047
1048 size_t ofputil_count_queue_stats(const struct ofp_header *);
1049 int ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg);
1050 void ofputil_append_queue_stat(struct list *replies,
1051                                const struct ofputil_queue_stats *oqs);
1052
1053 /* Bucket for use in groups. */
1054 struct ofputil_bucket {
1055     struct list list_node;
1056     uint16_t weight;            /* Relative weight, for "select" groups. */
1057     ofp_port_t watch_port;      /* Port whose state affects whether this bucket
1058                                  * is live. Only required for fast failover
1059                                  * groups. */
1060     uint32_t watch_group;       /* Group whose state affects whether this
1061                                  * bucket is live. Only required for fast
1062                                  * failover groups. */
1063     struct ofpact *ofpacts;     /* Series of "struct ofpact"s. */
1064     size_t ofpacts_len;         /* Length of ofpacts, in bytes. */
1065 };
1066
1067 /* Protocol-independent group_mod. */
1068 struct ofputil_group_mod {
1069     uint16_t command;             /* One of OFPGC11_*. */
1070     uint8_t type;                 /* One of OFPGT11_*. */
1071     uint32_t group_id;            /* Group identifier. */
1072     struct list buckets;          /* Contains "struct ofputil_bucket"s. */
1073 };
1074
1075 struct bucket_counter {
1076     uint64_t packet_count;   /* Number of packets processed by bucket. */
1077     uint64_t byte_count;     /* Number of bytes processed by bucket. */
1078 };
1079
1080 /* Group stats reply, independent of protocol. */
1081 struct ofputil_group_stats {
1082     uint32_t group_id;    /* Group identifier. */
1083     uint32_t ref_count;
1084     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
1085     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
1086     uint32_t duration_sec;      /* UINT32_MAX if unknown. */
1087     uint32_t duration_nsec;
1088     uint32_t n_buckets;
1089     struct bucket_counter *bucket_stats;
1090 };
1091
1092 /* Group features reply, independent of protocol.
1093  *
1094  * Only OF1.2 and later support group features replies. */
1095 struct ofputil_group_features {
1096     uint32_t  types;           /* Bitmap of OFPGT_* values supported. */
1097     uint32_t  capabilities;    /* Bitmap of OFPGFC12_* capability supported. */
1098     uint32_t  max_groups[4];   /* Maximum number of groups for each type. */
1099
1100     /* Bitmaps of OFPAT_* that are supported.  OF1.2+ actions only. */
1101     uint32_t  actions[4];
1102 };
1103
1104 /* Group desc reply, independent of protocol. */
1105 struct ofputil_group_desc {
1106     uint8_t type;               /* One of OFPGT_*. */
1107     uint32_t group_id;          /* Group identifier. */
1108     struct list buckets;        /* Contains "struct ofputil_bucket"s. */
1109 };
1110
1111 void ofputil_bucket_list_destroy(struct list *buckets);
1112
1113 static inline bool
1114 ofputil_bucket_has_liveness(const struct ofputil_bucket *bucket)
1115 {
1116     return (bucket->watch_port != OFPP_ANY ||
1117             bucket->watch_group != OFPG_ANY);
1118 }
1119
1120 struct ofpbuf *ofputil_encode_group_stats_request(enum ofp_version,
1121                                                   uint32_t group_id);
1122 enum ofperr ofputil_decode_group_stats_request(
1123     const struct ofp_header *request, uint32_t *group_id);
1124 void ofputil_append_group_stats(struct list *replies,
1125                                 const struct ofputil_group_stats *);
1126 struct ofpbuf *ofputil_encode_group_features_request(enum ofp_version);
1127 struct ofpbuf *ofputil_encode_group_features_reply(
1128     const struct ofputil_group_features *, const struct ofp_header *request);
1129 void ofputil_decode_group_features_reply(const struct ofp_header *,
1130                                          struct ofputil_group_features *);
1131 struct ofpbuf *ofputil_encode_group_mod(enum ofp_version ofp_version,
1132                                         const struct ofputil_group_mod *gm);
1133
1134 enum ofperr ofputil_decode_group_mod(const struct ofp_header *,
1135                                      struct ofputil_group_mod *);
1136
1137 int ofputil_decode_group_stats_reply(struct ofpbuf *,
1138                                      struct ofputil_group_stats *);
1139
1140 uint32_t ofputil_decode_group_desc_request(const struct ofp_header *);
1141 struct ofpbuf *ofputil_encode_group_desc_request(enum ofp_version,
1142                                                  uint32_t group_id);
1143
1144 int ofputil_decode_group_desc_reply(struct ofputil_group_desc *,
1145                                     struct ofpbuf *, enum ofp_version);
1146
1147 void ofputil_append_group_desc_reply(const struct ofputil_group_desc *,
1148                                      struct list *buckets,
1149                                      struct list *replies);
1150
1151 struct ofputil_bundle_ctrl_msg {
1152     uint32_t    bundle_id;
1153     uint16_t    type;
1154     uint16_t    flags;
1155 };
1156
1157 struct ofputil_bundle_add_msg {
1158     uint32_t            bundle_id;
1159     uint16_t            flags;
1160     const struct ofp_header   *msg;
1161 };
1162
1163 enum ofperr ofputil_decode_bundle_ctrl(const struct ofp_header *,
1164                                        struct ofputil_bundle_ctrl_msg *);
1165
1166 struct ofpbuf *ofputil_encode_bundle_ctrl_reply(const struct ofp_header *,
1167                                                 struct ofputil_bundle_ctrl_msg *);
1168
1169 struct ofpbuf *ofputil_encode_bundle_add(enum ofp_version ofp_version,
1170                                          struct ofputil_bundle_add_msg *msg);
1171
1172 enum ofperr ofputil_decode_bundle_add(const struct ofp_header *,
1173                                       struct ofputil_bundle_add_msg *);
1174 #endif /* ofp-util.h */