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