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