ofproto: update flow_stats flags on flow_stats_request
[sliver-openvswitch.git] / lib / ofp-util.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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 "classifier.h"
24 #include "compiler.h"
25 #include "flow.h"
26 #include "match.h"
27 #include "netdev.h"
28 #include "openflow/nicira-ext.h"
29 #include "openvswitch/types.h"
30 #include "type-props.h"
31
32 struct ofpbuf;
33
34 /* Port numbers. */
35 enum ofperr ofputil_port_from_ofp11(ovs_be32 ofp11_port,
36                                     ofp_port_t *ofp10_port);
37 ovs_be32 ofputil_port_to_ofp11(ofp_port_t ofp10_port);
38
39 enum ofperr ofputil_check_output_port(ofp_port_t ofp_port,
40                                       ofp_port_t max_ports);
41 bool ofputil_port_from_string(const char *, ofp_port_t *portp);
42 void ofputil_format_port(ofp_port_t port, struct ds *);
43 void ofputil_port_to_string(ofp_port_t, char namebuf[OFP_MAX_PORT_NAME_LEN],
44                             size_t bufsize);
45
46 /* Group numbers. */
47 enum { MAX_GROUP_NAME_LEN = INT_STRLEN(uint32_t) };
48 bool ofputil_group_from_string(const char *, uint32_t *group_id);
49 void ofputil_format_group(uint32_t group_id, struct ds *);
50 void ofputil_group_to_string(uint32_t group_id,
51                              char namebuf[MAX_GROUP_NAME_LEN + 1],
52                              size_t bufsize);
53
54 /* Converting OFPFW10_NW_SRC_MASK and OFPFW10_NW_DST_MASK wildcard bit counts
55  * to and from IP bitmasks. */
56 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
57 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
58
59 /* Protocols.
60  *
61  * A "protocol" is an OpenFlow version plus, for some OpenFlow versions,
62  * a bit extra about the flow match format in use.
63  *
64  * These are arranged from most portable to least portable, or alternatively
65  * from least powerful to most powerful.  Protocols earlier on the list are
66  * more likely to be understood for the purpose of making requests, but
67  * protocol later on the list are more likely to accurately describe a flow
68  * within a switch.
69  *
70  * On any given OpenFlow connection, a single protocol is in effect at any
71  * given time.  These values use separate bits only because that makes it easy
72  * to test whether a particular protocol is within a given set of protocols and
73  * to implement set union and intersection.
74  */
75 enum ofputil_protocol {
76     /* OpenFlow 1.0 protocols.
77      *
78      * The "STD" protocols use the standard OpenFlow 1.0 flow format.
79      * The "NXM" protocols use the Nicira Extensible Match (NXM) flow format.
80      *
81      * The protocols with "TID" mean that the nx_flow_mod_table_id Nicira
82      * extension has been enabled.  The other protocols have it disabled.
83      */
84 #define OFPUTIL_P_NONE 0
85     OFPUTIL_P_OF10_STD     = 1 << 0,
86     OFPUTIL_P_OF10_STD_TID = 1 << 1,
87     OFPUTIL_P_OF10_NXM     = 1 << 2,
88     OFPUTIL_P_OF10_NXM_TID = 1 << 3,
89 #define OFPUTIL_P_OF10_STD_ANY (OFPUTIL_P_OF10_STD | OFPUTIL_P_OF10_STD_TID)
90 #define OFPUTIL_P_OF10_NXM_ANY (OFPUTIL_P_OF10_NXM | OFPUTIL_P_OF10_NXM_TID)
91
92     /* OpenFlow 1.1 protocol.
93      *
94      * We only support the standard OpenFlow 1.1 flow format.
95      *
96      * OpenFlow 1.1 always operates with an equivalent of the
97      * nx_flow_mod_table_id Nicira extension enabled, so there is no "TID"
98      * variant. */
99     OFPUTIL_P_OF11_STD     = 1 << 4,
100
101     /* OpenFlow 1.2+ protocols (only one variant each).
102      *
103      * These use the standard OpenFlow Extensible Match (OXM) flow format.
104      *
105      * OpenFlow 1.2+ always operates with an equivalent of the
106      * nx_flow_mod_table_id Nicira extension enabled, so there is no "TID"
107      * variant. */
108     OFPUTIL_P_OF12_OXM      = 1 << 5,
109     OFPUTIL_P_OF13_OXM      = 1 << 6,
110 #define OFPUTIL_P_ANY_OXM (OFPUTIL_P_OF12_OXM | OFPUTIL_P_OF13_OXM)
111
112 #define OFPUTIL_P_NXM_OF11_UP (OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF11_STD | \
113                                OFPUTIL_P_ANY_OXM)
114
115 #define OFPUTIL_P_NXM_OXM_ANY (OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_ANY_OXM)
116
117 #define OFPUTIL_P_OF11_UP (OFPUTIL_P_OF11_STD | OFPUTIL_P_ANY_OXM)
118
119 #define OFPUTIL_P_OF12_UP (OFPUTIL_P_ANY_OXM)
120
121 #define OFPUTIL_P_OF13_UP (OFPUTIL_P_OF13_OXM)
122
123     /* All protocols. */
124 #define OFPUTIL_P_ANY ((1 << 7) - 1)
125
126     /* Protocols in which a specific table may be specified in flow_mods. */
127 #define OFPUTIL_P_TID (OFPUTIL_P_OF10_STD_TID | \
128                        OFPUTIL_P_OF10_NXM_TID | \
129                        OFPUTIL_P_OF11_STD |     \
130                        OFPUTIL_P_ANY_OXM)
131 };
132
133 /* Protocols to use for flow dumps, from most to least preferred. */
134 extern enum ofputil_protocol ofputil_flow_dump_protocols[];
135 extern size_t ofputil_n_flow_dump_protocols;
136
137 enum ofputil_protocol ofputil_protocol_from_ofp_version(enum ofp_version);
138 enum ofputil_protocol ofputil_protocols_from_ofp_version(enum ofp_version);
139 enum ofp_version ofputil_protocol_to_ofp_version(enum ofputil_protocol);
140
141 bool ofputil_protocol_is_valid(enum ofputil_protocol);
142 enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
143                                                bool enable);
144 enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
145 enum ofputil_protocol ofputil_protocol_set_base(
146     enum ofputil_protocol cur, enum ofputil_protocol new_base);
147
148 const char *ofputil_protocol_to_string(enum ofputil_protocol);
149 char *ofputil_protocols_to_string(enum ofputil_protocol);
150 enum ofputil_protocol ofputil_protocols_from_string(const char *);
151
152 void ofputil_format_version(struct ds *, enum ofp_version);
153 void ofputil_format_version_name(struct ds *, enum ofp_version);
154
155 /* A bitmap of version numbers
156  *
157  * Bit offsets correspond to ofp_version numbers which in turn correspond to
158  * wire-protocol numbers for Open Flow versions..  E.g. (1u << OFP11_VERSION)
159  * is the mask for Open Flow 1.1.  If the bit for a version is set then it is
160  * allowed, otherwise it is disallowed. */
161
162 void ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap);
163 void ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap);
164
165 uint32_t ofputil_protocols_to_version_bitmap(enum ofputil_protocol);
166 enum ofputil_protocol ofputil_protocols_from_version_bitmap(uint32_t bitmap);
167
168 /* Bitmap of OpenFlow versions that Open vSwitch supports. */
169 #define OFPUTIL_SUPPORTED_VERSIONS \
170     ((1u << OFP10_VERSION) | (1u << OFP12_VERSION) | (1u << OFP13_VERSION))
171
172 /* Bitmap of OpenFlow versions to enable by default (a subset of
173  * OFPUTIL_SUPPORTED_VERSIONS). */
174 #define OFPUTIL_DEFAULT_VERSIONS (1u << OFP10_VERSION)
175
176 enum ofputil_protocol ofputil_protocols_from_string(const char *s);
177
178 const char *ofputil_version_to_string(enum ofp_version ofp_version);
179 uint32_t ofputil_versions_from_string(const char *s);
180 uint32_t ofputil_versions_from_strings(char ** const s, size_t count);
181
182 bool ofputil_decode_hello(const struct ofp_header *,
183                           uint32_t *allowed_versions);
184 struct ofpbuf *ofputil_encode_hello(uint32_t version_bitmap);
185
186 struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
187                                            enum ofputil_protocol want,
188                                            enum ofputil_protocol *next);
189
190 /* nx_flow_format */
191 struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
192 enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
193 bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
194 const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
195
196 /* Work with ofp10_match. */
197 void ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *);
198 void ofputil_match_from_ofp10_match(const struct ofp10_match *,
199                                     struct match *);
200 void ofputil_normalize_match(struct match *);
201 void ofputil_normalize_match_quiet(struct match *);
202 void ofputil_match_to_ofp10_match(const struct match *, struct ofp10_match *);
203
204 /* Work with ofp11_match. */
205 enum ofperr ofputil_pull_ofp11_match(struct ofpbuf *, struct match *,
206                                      uint16_t *padded_match_len);
207 enum ofperr ofputil_match_from_ofp11_match(const struct ofp11_match *,
208                                            struct match *);
209 int ofputil_put_ofp11_match(struct ofpbuf *, const struct match *,
210                             enum ofputil_protocol);
211 void ofputil_match_to_ofp11_match(const struct match *, struct ofp11_match *);
212 int ofputil_match_typical_len(enum ofputil_protocol);
213
214 /* dl_type translation between OpenFlow and 'struct flow' format. */
215 ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
216 ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
217
218 /* PACKET_IN. */
219 bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format);
220 int ofputil_packet_in_format_from_string(const char *);
221 const char *ofputil_packet_in_format_to_string(enum nx_packet_in_format);
222 struct ofpbuf *ofputil_make_set_packet_in_format(enum ofp_version,
223                                                  enum nx_packet_in_format);
224
225 /* NXT_FLOW_MOD_TABLE_ID extension. */
226 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
227
228 /* Protocol-independent flow_mod flags. */
229 enum ofputil_flow_mod_flags {
230     /* Flags that are maintained with a flow as part of its state.
231      *
232      * (OFPUTIL_FF_EMERG would be here too, if OVS supported it.) */
233     OFPUTIL_FF_SEND_FLOW_REM = 1 << 0, /* All versions. */
234     OFPUTIL_FF_NO_PKT_COUNTS = 1 << 1, /* OpenFlow 1.3+. */
235     OFPUTIL_FF_NO_BYT_COUNTS = 1 << 2, /* OpenFlow 1.3+. */
236 #define OFPUTIL_FF_STATE (OFPUTIL_FF_SEND_FLOW_REM      \
237                           | OFPUTIL_FF_NO_PKT_COUNTS    \
238                           | OFPUTIL_FF_NO_BYT_COUNTS)
239
240     /* Flags that affect flow_mod behavior but are not part of flow state. */
241     OFPUTIL_FF_CHECK_OVERLAP = 1 << 3, /* All versions. */
242     OFPUTIL_FF_EMERG         = 1 << 4, /* OpenFlow 1.0 only. */
243     OFPUTIL_FF_RESET_COUNTS  = 1 << 5, /* OpenFlow 1.2+. */
244 };
245
246 /* Protocol-independent flow_mod.
247  *
248  * The handling of cookies across multiple versions of OpenFlow is a bit
249  * confusing.  See DESIGN for the details. */
250 struct ofputil_flow_mod {
251     struct list list_node;    /* For queuing flow_mods. */
252
253     struct match match;
254     unsigned int priority;
255
256     /* Cookie matching.  The flow_mod affects only flows that have cookies that
257      * bitwise match 'cookie' bits in positions where 'cookie_mask has 1-bits.
258      *
259      * 'cookie_mask' should be zero for OFPFC_ADD flow_mods. */
260     ovs_be64 cookie;         /* Cookie bits to match. */
261     ovs_be64 cookie_mask;    /* 1-bit in each 'cookie' bit to match. */
262
263     /* Cookie changes.
264      *
265      * OFPFC_ADD uses 'new_cookie' as the new flow's cookie.  'new_cookie'
266      * should not be UINT64_MAX.
267      *
268      * OFPFC_MODIFY and OFPFC_MODIFY_STRICT have two cases:
269      *
270      *   - If one or more matching flows exist and 'modify_cookie' is true,
271      *     then the flow_mod changes the existing flows' cookies to
272      *     'new_cookie'.  'new_cookie' should not be UINT64_MAX.
273      *
274      *   - If no matching flow exists, 'new_cookie' is not UINT64_MAX, and
275      *     'cookie_mask' is 0, then the flow_mod adds a new flow with
276      *     'new_cookie' as its cookie.
277      */
278     ovs_be64 new_cookie;     /* New cookie to install or UINT64_MAX. */
279     bool modify_cookie;      /* Set cookie of existing flow to 'new_cookie'? */
280
281     uint8_t table_id;
282     uint16_t command;
283     uint16_t idle_timeout;
284     uint16_t hard_timeout;
285     uint32_t buffer_id;
286     ofp_port_t out_port;
287     uint32_t out_group;
288     enum ofputil_flow_mod_flags flags;
289     struct ofpact *ofpacts;     /* Series of "struct ofpact"s. */
290     size_t ofpacts_len;         /* Length of ofpacts, in bytes. */
291 };
292
293 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
294                                     const struct ofp_header *,
295                                     enum ofputil_protocol,
296                                     struct ofpbuf *ofpacts);
297 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
298                                        enum ofputil_protocol);
299
300 /* Flow stats or aggregate stats request, independent of protocol. */
301 struct ofputil_flow_stats_request {
302     bool aggregate;             /* Aggregate results? */
303     struct match match;
304     ovs_be64 cookie;
305     ovs_be64 cookie_mask;
306     ofp_port_t out_port;
307     uint32_t out_group;
308     uint8_t table_id;
309 };
310
311 enum ofperr ofputil_decode_flow_stats_request(
312     struct ofputil_flow_stats_request *, const struct ofp_header *);
313 struct ofpbuf *ofputil_encode_flow_stats_request(
314     const struct ofputil_flow_stats_request *, enum ofputil_protocol);
315
316 /* Flow stats reply, independent of protocol. */
317 struct ofputil_flow_stats {
318     struct match match;
319     ovs_be64 cookie;
320     uint8_t table_id;
321     uint32_t duration_sec;
322     uint32_t duration_nsec;
323     uint16_t priority;
324     uint16_t idle_timeout;
325     uint16_t hard_timeout;
326     int idle_age;               /* Seconds since last packet, -1 if unknown. */
327     int hard_age;               /* Seconds since last change, -1 if unknown. */
328     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
329     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
330     struct ofpact *ofpacts;
331     size_t ofpacts_len;
332     enum ofputil_flow_mod_flags flags;
333 };
334
335 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
336                                     struct ofpbuf *msg,
337                                     bool flow_age_extension,
338                                     struct ofpbuf *ofpacts);
339 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
340                                      struct list *replies);
341
342 /* Aggregate stats reply, independent of protocol. */
343 struct ofputil_aggregate_stats {
344     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
345     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
346     uint32_t flow_count;
347 };
348
349 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
350     const struct ofputil_aggregate_stats *stats,
351     const struct ofp_header *request);
352 enum ofperr ofputil_decode_aggregate_stats_reply(
353     struct ofputil_aggregate_stats *,
354     const struct ofp_header *reply);
355
356 /* Flow removed message, independent of protocol. */
357 struct ofputil_flow_removed {
358     struct match match;
359     uint16_t priority;
360     ovs_be64 cookie;
361     uint8_t reason;             /* One of OFPRR_*. */
362     uint8_t table_id;           /* 255 if message didn't include table ID. */
363     uint32_t duration_sec;
364     uint32_t duration_nsec;
365     uint16_t idle_timeout;
366     uint16_t hard_timeout;
367     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
368     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
369 };
370
371 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
372                                         const struct ofp_header *);
373 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
374                                            enum ofputil_protocol);
375
376 /* Abstract packet-in message. */
377 struct ofputil_packet_in {
378     struct list list_node; /* For queueing packet_ins. */
379
380     const void *packet;
381     size_t packet_len;
382
383     enum ofp_packet_in_reason reason;    /* One of OFPR_*. */
384     uint16_t controller_id;              /* Controller ID to send to. */
385     uint8_t table_id;
386     ovs_be64 cookie;
387
388     uint32_t buffer_id;
389     int send_len;
390     uint16_t total_len;         /* Full length of frame. */
391
392     struct flow_metadata fmd;   /* Metadata at creation time. */
393 };
394
395 enum ofperr ofputil_decode_packet_in(struct ofputil_packet_in *,
396                                      const struct ofp_header *);
397 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
398                                         enum ofputil_protocol protocol,
399                                         enum nx_packet_in_format);
400
401 enum { OFPUTIL_PACKET_IN_REASON_BUFSIZE = INT_STRLEN(int) + 1 };
402 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason,
403                                                char *reasonbuf,
404                                                size_t bufsize);
405 bool ofputil_packet_in_reason_from_string(const char *,
406                                           enum ofp_packet_in_reason *);
407
408 /* Abstract packet-out message.
409  *
410  * ofputil_decode_packet_out() will ensure that 'in_port' is a physical port
411  * (OFPP_MAX or less) or one of OFPP_LOCAL, OFPP_NONE, or OFPP_CONTROLLER. */
412 struct ofputil_packet_out {
413     const void *packet;         /* Packet data, if buffer_id == UINT32_MAX. */
414     size_t packet_len;          /* Length of packet data in bytes. */
415     uint32_t buffer_id;         /* Buffer id or UINT32_MAX if no buffer. */
416     ofp_port_t in_port;         /* Packet's input port. */
417     struct ofpact *ofpacts;     /* Actions. */
418     size_t ofpacts_len;         /* Size of ofpacts in bytes. */
419 };
420
421 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
422                                       const struct ofp_header *,
423                                       struct ofpbuf *ofpacts);
424 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *,
425                                          enum ofputil_protocol protocol);
426
427 enum ofputil_port_config {
428     /* OpenFlow 1.0 and 1.1 share these values for these port config bits. */
429     OFPUTIL_PC_PORT_DOWN    = 1 << 0, /* Port is administratively down. */
430     OFPUTIL_PC_NO_RECV      = 1 << 2, /* Drop all packets received by port. */
431     OFPUTIL_PC_NO_FWD       = 1 << 5, /* Drop packets forwarded to port. */
432     OFPUTIL_PC_NO_PACKET_IN = 1 << 6, /* No send packet-in msgs for port. */
433     /* OpenFlow 1.0 only. */
434     OFPUTIL_PC_NO_STP       = 1 << 1, /* No 802.1D spanning tree for port. */
435     OFPUTIL_PC_NO_RECV_STP  = 1 << 3, /* Drop received 802.1D STP packets. */
436     OFPUTIL_PC_NO_FLOOD     = 1 << 4, /* Do not include port when flooding. */
437     /* There are no OpenFlow 1.1-only bits. */
438 };
439
440 enum ofputil_port_state {
441     /* OpenFlow 1.0 and 1.1 share this values for these port state bits. */
442     OFPUTIL_PS_LINK_DOWN   = 1 << 0, /* No physical link present. */
443     /* OpenFlow 1.1 only. */
444     OFPUTIL_PS_BLOCKED     = 1 << 1, /* Port is blocked */
445     OFPUTIL_PS_LIVE        = 1 << 2, /* Live for Fast Failover Group. */
446     /* OpenFlow 1.0 only. */
447     OFPUTIL_PS_STP_LISTEN  = 0 << 8, /* Not learning or relaying frames. */
448     OFPUTIL_PS_STP_LEARN   = 1 << 8, /* Learning but not relaying frames. */
449     OFPUTIL_PS_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
450     OFPUTIL_PS_STP_BLOCK   = 3 << 8, /* Not part of spanning tree. */
451     OFPUTIL_PS_STP_MASK    = 3 << 8  /* Bit mask for OFPPS10_STP_* values. */
452 };
453
454 /* Abstract ofp10_phy_port or ofp11_port. */
455 struct ofputil_phy_port {
456     ofp_port_t port_no;
457     uint8_t hw_addr[OFP_ETH_ALEN];
458     char name[OFP_MAX_PORT_NAME_LEN];
459     enum ofputil_port_config config;
460     enum ofputil_port_state state;
461
462     /* NETDEV_F_* feature bitmasks. */
463     enum netdev_features curr;       /* Current features. */
464     enum netdev_features advertised; /* Features advertised by the port. */
465     enum netdev_features supported;  /* Features supported by the port. */
466     enum netdev_features peer;       /* Features advertised by peer. */
467
468     /* Speed. */
469     uint32_t curr_speed;        /* Current speed, in kbps. */
470     uint32_t max_speed;         /* Maximum supported speed, in kbps. */
471 };
472
473 enum ofputil_capabilities {
474     /* OpenFlow 1.0, 1.1, 1.2, and 1.3 share these capability values. */
475     OFPUTIL_C_FLOW_STATS     = 1 << 0,  /* Flow statistics. */
476     OFPUTIL_C_TABLE_STATS    = 1 << 1,  /* Table statistics. */
477     OFPUTIL_C_PORT_STATS     = 1 << 2,  /* Port statistics. */
478     OFPUTIL_C_IP_REASM       = 1 << 5,  /* Can reassemble IP fragments. */
479     OFPUTIL_C_QUEUE_STATS    = 1 << 6,  /* Queue statistics. */
480
481     /* OpenFlow 1.0 and 1.1 share this capability. */
482     OFPUTIL_C_ARP_MATCH_IP   = 1 << 7,  /* Match IP addresses in ARP pkts. */
483
484     /* OpenFlow 1.0 only. */
485     OFPUTIL_C_STP            = 1 << 3,  /* 802.1d spanning tree. */
486
487     /* OpenFlow 1.1, 1.2, and 1.3 share this capability. */
488     OFPUTIL_C_GROUP_STATS    = 1 << 4,  /* Group statistics. */
489
490     /* OpenFlow 1.2 and 1.3 share this capability */
491     OFPUTIL_C_PORT_BLOCKED   = 1 << 8,  /* Switch will block looping ports */
492 };
493
494 enum ofputil_action_bitmap {
495     OFPUTIL_A_OUTPUT         = 1 << 0,
496     OFPUTIL_A_SET_VLAN_VID   = 1 << 1,
497     OFPUTIL_A_SET_VLAN_PCP   = 1 << 2,
498     OFPUTIL_A_STRIP_VLAN     = 1 << 3,
499     OFPUTIL_A_SET_DL_SRC     = 1 << 4,
500     OFPUTIL_A_SET_DL_DST     = 1 << 5,
501     OFPUTIL_A_SET_NW_SRC     = 1 << 6,
502     OFPUTIL_A_SET_NW_DST     = 1 << 7,
503     OFPUTIL_A_SET_NW_ECN     = 1 << 8,
504     OFPUTIL_A_SET_NW_TOS     = 1 << 9,
505     OFPUTIL_A_SET_TP_SRC     = 1 << 10,
506     OFPUTIL_A_SET_TP_DST     = 1 << 11,
507     OFPUTIL_A_ENQUEUE        = 1 << 12,
508     OFPUTIL_A_COPY_TTL_OUT   = 1 << 13,
509     OFPUTIL_A_COPY_TTL_IN    = 1 << 14,
510     OFPUTIL_A_SET_MPLS_LABEL = 1 << 15,
511     OFPUTIL_A_SET_MPLS_TC    = 1 << 16,
512     OFPUTIL_A_SET_MPLS_TTL   = 1 << 17,
513     OFPUTIL_A_DEC_MPLS_TTL   = 1 << 18,
514     OFPUTIL_A_PUSH_VLAN      = 1 << 19,
515     OFPUTIL_A_POP_VLAN       = 1 << 20,
516     OFPUTIL_A_PUSH_MPLS      = 1 << 21,
517     OFPUTIL_A_POP_MPLS       = 1 << 22,
518     OFPUTIL_A_SET_QUEUE      = 1 << 23,
519     OFPUTIL_A_GROUP          = 1 << 24,
520     OFPUTIL_A_SET_NW_TTL     = 1 << 25,
521     OFPUTIL_A_DEC_NW_TTL     = 1 << 26,
522     OFPUTIL_A_SET_FIELD      = 1 << 27,
523 };
524
525 /* Abstract ofp_switch_features. */
526 struct ofputil_switch_features {
527     uint64_t datapath_id;       /* Datapath unique ID. */
528     uint32_t n_buffers;         /* Max packets buffered at once. */
529     uint8_t n_tables;           /* Number of tables supported by datapath. */
530     uint8_t auxiliary_id;       /* Identify auxiliary connections */
531     enum ofputil_capabilities capabilities;
532     enum ofputil_action_bitmap actions;
533 };
534
535 enum ofperr ofputil_decode_switch_features(const struct ofp_header *,
536                                            struct ofputil_switch_features *,
537                                            struct ofpbuf *);
538
539 struct ofpbuf *ofputil_encode_switch_features(
540     const struct ofputil_switch_features *, enum ofputil_protocol,
541     ovs_be32 xid);
542 void ofputil_put_switch_features_port(const struct ofputil_phy_port *,
543                                       struct ofpbuf *);
544 bool ofputil_switch_features_ports_trunc(struct ofpbuf *b);
545
546 /* phy_port helper functions. */
547 int ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *,
548                           struct ofputil_phy_port *);
549 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *);
550
551 /* Abstract ofp_port_status. */
552 struct ofputil_port_status {
553     enum ofp_port_reason reason;
554     struct ofputil_phy_port desc;
555 };
556
557 enum ofperr ofputil_decode_port_status(const struct ofp_header *,
558                                        struct ofputil_port_status *);
559 struct ofpbuf *ofputil_encode_port_status(const struct ofputil_port_status *,
560                                           enum ofputil_protocol);
561
562 /* Abstract ofp_port_mod. */
563 struct ofputil_port_mod {
564     ofp_port_t port_no;
565     uint8_t hw_addr[OFP_ETH_ALEN];
566     enum ofputil_port_config config;
567     enum ofputil_port_config mask;
568     enum netdev_features advertise;
569 };
570
571 enum ofperr ofputil_decode_port_mod(const struct ofp_header *,
572                                     struct ofputil_port_mod *);
573 struct ofpbuf *ofputil_encode_port_mod(const struct ofputil_port_mod *,
574                                        enum ofputil_protocol);
575
576 /* Abstract ofp_table_mod. */
577 struct ofputil_table_mod {
578     uint8_t table_id;         /* ID of the table, 0xff indicates all tables. */
579     uint32_t config;
580 };
581
582 enum ofperr ofputil_decode_table_mod(const struct ofp_header *,
583                                     struct ofputil_table_mod *);
584 struct ofpbuf *ofputil_encode_table_mod(const struct ofputil_table_mod *,
585                                        enum ofputil_protocol);
586
587 /* Meter band configuration for all supported band types. */
588 struct ofputil_meter_band {
589     uint16_t type;
590     uint8_t prec_level;         /* Non-zero if type == OFPMBT_DSCP_REMARK. */
591     uint32_t rate;
592     uint32_t burst_size;
593 };
594
595 struct ofputil_meter_band_stats {
596     uint64_t packet_count;
597     uint64_t byte_count;
598 };
599
600 struct ofputil_meter_config {
601     uint32_t meter_id;
602     uint16_t flags;
603     uint16_t n_bands;
604     struct ofputil_meter_band *bands;
605 };
606
607 /* Abstract ofp_meter_mod. */
608 struct ofputil_meter_mod {
609     uint16_t command;
610     struct ofputil_meter_config meter;
611 };
612
613 struct ofputil_meter_stats {
614     uint32_t meter_id;
615     uint32_t flow_count;
616     uint64_t packet_in_count;
617     uint64_t byte_in_count;
618     uint32_t duration_sec;
619     uint32_t duration_nsec;
620     uint16_t n_bands;
621     struct ofputil_meter_band_stats *bands;
622 };
623
624 struct ofputil_meter_features {
625     uint32_t max_meters;        /* Maximum number of meters. */
626     uint32_t band_types;        /* Can support max 32 band types. */
627     uint32_t capabilities;      /* Supported flags. */
628     uint8_t  max_bands;
629     uint8_t  max_color;
630 };
631
632 enum ofperr ofputil_decode_meter_mod(const struct ofp_header *,
633                                      struct ofputil_meter_mod *,
634                                      struct ofpbuf *bands);
635 struct ofpbuf *ofputil_encode_meter_mod(enum ofp_version,
636                                         const struct ofputil_meter_mod *);
637
638 void ofputil_decode_meter_features(const struct ofp_header *,
639                                    struct ofputil_meter_features *);
640 struct ofpbuf *ofputil_encode_meter_features_reply(const struct
641                                                    ofputil_meter_features *,
642                                                    const struct ofp_header *
643                                                    request);
644 void ofputil_decode_meter_request(const struct ofp_header *,
645                                   uint32_t *meter_id);
646
647 void ofputil_append_meter_config(struct list *replies,
648                                  const struct ofputil_meter_config *);
649
650 void ofputil_append_meter_stats(struct list *replies,
651                                 const struct ofputil_meter_stats *);
652
653 enum ofputil_meter_request_type {
654     OFPUTIL_METER_FEATURES,
655     OFPUTIL_METER_CONFIG,
656     OFPUTIL_METER_STATS
657 };
658
659 struct ofpbuf *ofputil_encode_meter_request(enum ofp_version,
660                                             enum ofputil_meter_request_type,
661                                             uint32_t meter_id);
662
663 int ofputil_decode_meter_stats(struct ofpbuf *,
664                                struct ofputil_meter_stats *,
665                                struct ofpbuf *bands);
666
667 int ofputil_decode_meter_config(struct ofpbuf *,
668                                 struct ofputil_meter_config *,
669                                 struct ofpbuf *bands);
670
671 /* Type for meter_id in ofproto provider interface, UINT32_MAX if invalid. */
672 typedef struct { uint32_t uint32; } ofproto_meter_id;
673
674 /* Abstract ofp_role_request and reply. */
675 struct ofputil_role_request {
676     enum ofp12_controller_role role;
677     bool have_generation_id;
678     uint64_t generation_id;
679 };
680
681 enum ofperr ofputil_decode_role_message(const struct ofp_header *,
682                                         struct ofputil_role_request *);
683 struct ofpbuf *ofputil_encode_role_reply(const struct ofp_header *,
684                                          const struct ofputil_role_request *);
685
686 /* Abstract table stats.
687  *
688  * For now we use ofp12_table_stats as a superset of the other protocol
689  * versions' table stats. */
690
691 struct ofpbuf *ofputil_encode_table_stats_reply(
692     const struct ofp12_table_stats[], int n,
693     const struct ofp_header *request);
694
695 /* Abstract nx_flow_monitor_request. */
696 struct ofputil_flow_monitor_request {
697     uint32_t id;
698     enum nx_flow_monitor_flags flags;
699     ofp_port_t out_port;
700     uint8_t table_id;
701     struct match match;
702 };
703
704 int ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *,
705                                         struct ofpbuf *msg);
706 void ofputil_append_flow_monitor_request(
707     const struct ofputil_flow_monitor_request *, struct ofpbuf *msg);
708
709 /* Abstract nx_flow_update. */
710 struct ofputil_flow_update {
711     enum nx_flow_update_event event;
712
713     /* Used only for NXFME_ADDED, NXFME_DELETED, NXFME_MODIFIED. */
714     enum ofp_flow_removed_reason reason;
715     uint16_t idle_timeout;
716     uint16_t hard_timeout;
717     uint8_t table_id;
718     ovs_be64 cookie;
719     struct match *match;
720     uint16_t priority;
721     struct ofpact *ofpacts;
722     size_t ofpacts_len;
723
724     /* Used only for NXFME_ABBREV. */
725     ovs_be32 xid;
726 };
727
728 int ofputil_decode_flow_update(struct ofputil_flow_update *,
729                                struct ofpbuf *msg, struct ofpbuf *ofpacts);
730 void ofputil_start_flow_update(struct list *replies);
731 void ofputil_append_flow_update(const struct ofputil_flow_update *,
732                                 struct list *replies);
733
734 /* Abstract nx_flow_monitor_cancel. */
735 uint32_t ofputil_decode_flow_monitor_cancel(const struct ofp_header *);
736 struct ofpbuf *ofputil_encode_flow_monitor_cancel(uint32_t id);
737
738 /* Encoding OpenFlow stats messages. */
739 void ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
740                                           const struct ofputil_phy_port *pp,
741                                           struct list *replies);
742
743 /* Encoding simple OpenFlow messages. */
744 struct ofpbuf *make_echo_request(enum ofp_version);
745 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
746
747 struct ofpbuf *ofputil_encode_barrier_request(enum ofp_version);
748
749 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
750 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
751
752 \f
753 /* Actions. */
754
755 /* The type of an action.
756  *
757  * For each implemented OFPAT10_* and NXAST_* action type, there is a
758  * corresponding constant prefixed with OFPUTIL_, e.g.:
759  *
760  * OFPUTIL_OFPAT10_OUTPUT
761  * OFPUTIL_OFPAT10_SET_VLAN_VID
762  * OFPUTIL_OFPAT10_SET_VLAN_PCP
763  * OFPUTIL_OFPAT10_STRIP_VLAN
764  * OFPUTIL_OFPAT10_SET_DL_SRC
765  * OFPUTIL_OFPAT10_SET_DL_DST
766  * OFPUTIL_OFPAT10_SET_NW_SRC
767  * OFPUTIL_OFPAT10_SET_NW_DST
768  * OFPUTIL_OFPAT10_SET_NW_TOS
769  * OFPUTIL_OFPAT10_SET_TP_SRC
770  * OFPUTIL_OFPAT10_SET_TP_DST
771  * OFPUTIL_OFPAT10_ENQUEUE
772  * OFPUTIL_NXAST_RESUBMIT
773  * OFPUTIL_NXAST_SET_TUNNEL
774  * OFPUTIL_NXAST_SET_METADATA
775  * OFPUTIL_NXAST_SET_QUEUE
776  * OFPUTIL_NXAST_POP_QUEUE
777  * OFPUTIL_NXAST_REG_MOVE
778  * OFPUTIL_NXAST_REG_LOAD
779  * OFPUTIL_NXAST_NOTE
780  * OFPUTIL_NXAST_SET_TUNNEL64
781  * OFPUTIL_NXAST_MULTIPATH
782  * OFPUTIL_NXAST_BUNDLE
783  * OFPUTIL_NXAST_BUNDLE_LOAD
784  * OFPUTIL_NXAST_RESUBMIT_TABLE
785  * OFPUTIL_NXAST_OUTPUT_REG
786  * OFPUTIL_NXAST_LEARN
787  * OFPUTIL_NXAST_DEC_TTL
788  * OFPUTIL_NXAST_FIN_TIMEOUT
789  *
790  * (The above list helps developers who want to "grep" for these definitions.)
791  */
792 enum OVS_PACKED_ENUM ofputil_action_code {
793     OFPUTIL_ACTION_INVALID,
794 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             OFPUTIL_##ENUM,
795 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
796 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   OFPUTIL_##ENUM,
797 #include "ofp-util.def"
798 };
799
800 /* The number of values of "enum ofputil_action_code". */
801 enum {
802 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             + 1
803 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
804 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   + 1
805     OFPUTIL_N_ACTIONS = 1
806 #include "ofp-util.def"
807 };
808
809 int ofputil_action_code_from_name(const char *);
810
811 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
812
813 /* For each OpenFlow action <ENUM> that has a corresponding action structure
814  * struct <STRUCT>, this defines two functions:
815  *
816  *   void ofputil_init_<ENUM>(struct <STRUCT> *action);
817  *
818  *     Initializes the parts of 'action' that identify it as having type <ENUM>
819  *     and length 'sizeof *action' and zeros the rest.  For actions that have
820  *     variable length, the length used and cleared is that of struct <STRUCT>.
821  *
822  *  struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
823  *
824  *     Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
825  *     initializes it with ofputil_init_<ENUM>(), and returns it.
826  */
827 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)              \
828     void ofputil_init_##ENUM(struct STRUCT *);          \
829     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
830 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
831     void ofputil_init_##ENUM(struct STRUCT *);          \
832     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
833 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
834     void ofputil_init_##ENUM(struct STRUCT *);          \
835     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
836 #include "ofp-util.def"
837
838 #define OFP_ACTION_ALIGN 8      /* Alignment of ofp_actions. */
839
840 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
841
842 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
843                                  union ofp_action **, size_t *);
844
845 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
846                            const union ofp_action *b, size_t n_b);
847 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
848
849 /* Handy utility for parsing flows and actions. */
850 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
851
852 struct ofputil_port_stats {
853     ofp_port_t port_no;
854     struct netdev_stats stats;
855     uint32_t duration_sec;      /* UINT32_MAX if unknown. */
856     uint32_t duration_nsec;
857 };
858
859 struct ofpbuf *ofputil_encode_dump_ports_request(enum ofp_version ofp_version,
860                                                  ofp_port_t port);
861 void ofputil_append_port_stat(struct list *replies,
862                               const struct ofputil_port_stats *ops);
863 size_t ofputil_count_port_stats(const struct ofp_header *);
864 int ofputil_decode_port_stats(struct ofputil_port_stats *, struct ofpbuf *msg);
865 enum ofperr ofputil_decode_port_stats_request(const struct ofp_header *request,
866                                               ofp_port_t *ofp10_port);
867
868 struct ofputil_queue_stats_request {
869     ofp_port_t port_no;           /* OFPP_ANY means "all ports". */
870     uint32_t queue_id;
871 };
872
873 enum ofperr
874 ofputil_decode_queue_stats_request(const struct ofp_header *request,
875                                    struct ofputil_queue_stats_request *oqsr);
876 struct ofpbuf *
877 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
878                                    const struct ofputil_queue_stats_request *oqsr);
879
880 struct ofputil_queue_stats {
881     ofp_port_t port_no;
882     uint32_t queue_id;
883
884     /* Values of unsupported statistics are set to all-1-bits (UINT64_MAX). */
885     uint64_t tx_bytes;
886     uint64_t tx_packets;
887     uint64_t tx_errors;
888
889     /* UINT32_MAX if unknown. */
890     uint32_t duration_sec;
891     uint32_t duration_nsec;
892 };
893
894 size_t ofputil_count_queue_stats(const struct ofp_header *);
895 int ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg);
896 void ofputil_append_queue_stat(struct list *replies,
897                                const struct ofputil_queue_stats *oqs);
898
899 /* Bucket for use in groups. */
900 struct ofputil_bucket {
901     struct list list_node;
902     uint16_t weight;            /* Relative weight, for "select" groups. */
903     ofp_port_t watch_port;      /* Port whose state affects whether this bucket
904                                  * is live. Only required for fast failover
905                                  * groups. */
906     uint32_t watch_group;       /* Group whose state affects whether this
907                                  * bucket is live. Only required for fast
908                                  * failover groups. */
909     struct ofpact *ofpacts;     /* Series of "struct ofpact"s. */
910     size_t ofpacts_len;         /* Length of ofpacts, in bytes. */
911 };
912
913 /* Protocol-independent group_mod. */
914 struct ofputil_group_mod {
915     uint16_t command;             /* One of OFPGC11_*. */
916     uint8_t type;                 /* One of OFPGT11_*. */
917     uint32_t group_id;            /* Group identifier. */
918     struct list buckets;          /* Contains "struct ofputil_bucket"s. */
919 };
920
921 struct bucket_counter {
922     uint64_t packet_count;   /* Number of packets processed by bucket. */
923     uint64_t byte_count;     /* Number of bytes processed by bucket. */
924 };
925
926 /* Group stats reply, independent of protocol. */
927 struct ofputil_group_stats {
928     uint32_t group_id;    /* Group identifier. */
929     uint32_t ref_count;
930     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
931     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
932     uint32_t duration_sec;      /* UINT32_MAX if unknown. */
933     uint32_t duration_nsec;
934     uint32_t n_buckets;
935     struct bucket_counter *bucket_stats;
936 };
937
938 /* Group features reply, independent of protocol. */
939 struct ofputil_group_features {
940     uint32_t  types;           /* Bitmap of OFPGT_* values supported. */
941     uint32_t  capabilities;    /* Bitmap of OFPGFC12_* capability supported. */
942     uint32_t  max_groups[4];   /* Maximum number of groups for each type. */
943     uint32_t  actions[4];      /* Bitmaps of OFPAT_* that are supported. */
944 };
945
946 /* Group desc reply, independent of protocol. */
947 struct ofputil_group_desc {
948     uint8_t type;               /* One of OFPGT_*. */
949     uint32_t group_id;          /* Group identifier. */
950     struct list buckets;        /* Contains "struct ofputil_bucket"s. */
951 };
952
953 void ofputil_bucket_list_destroy(struct list *buckets);
954
955 struct ofpbuf *ofputil_encode_group_stats_request(enum ofp_version,
956                                                   uint32_t group_id);
957 enum ofperr ofputil_decode_group_stats_request(
958     const struct ofp_header *request, uint32_t *group_id);
959 void ofputil_append_group_stats(struct list *replies,
960                                 const struct ofputil_group_stats *);
961 struct ofpbuf *ofputil_encode_group_features_request(enum ofp_version);
962 struct ofpbuf *ofputil_encode_group_features_reply(
963     const struct ofputil_group_features *, const struct ofp_header *request);
964 void ofputil_decode_group_features_reply(const struct ofp_header *,
965                                          struct ofputil_group_features *);
966 struct ofpbuf *ofputil_encode_group_mod(enum ofp_version ofp_version,
967                                         const struct ofputil_group_mod *gm);
968
969 enum ofperr ofputil_decode_group_mod(const struct ofp_header *,
970                                      struct ofputil_group_mod *);
971
972 int ofputil_decode_group_stats_reply(struct ofpbuf *,
973                                      struct ofputil_group_stats *);
974
975 int ofputil_decode_group_desc_reply(struct ofputil_group_desc *,
976                                     struct ofpbuf *);
977
978 void ofputil_append_group_desc_reply(const struct ofputil_group_desc *,
979                                      struct list *buckets,
980                                      struct list *replies);
981 struct ofpbuf *ofputil_encode_group_desc_request(enum ofp_version);
982
983 #endif /* ofp-util.h */