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