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