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