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