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