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