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