ofp-util: Add functions to support version number bitmaps.
[sliver-openvswitch.git] / lib / ofp-util.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 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 <assert.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include "classifier.h"
25 #include "compiler.h"
26 #include "flow.h"
27 #include "match.h"
28 #include "netdev.h"
29 #include "openflow/nicira-ext.h"
30 #include "openvswitch/types.h"
31
32 struct ofpbuf;
33
34 /* Port numbers. */
35 enum ofperr ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port);
36 ovs_be32 ofputil_port_to_ofp11(uint16_t ofp10_port);
37
38 enum ofperr ofputil_check_output_port(uint16_t ofp_port, int max_ports);
39 bool ofputil_port_from_string(const char *, uint16_t *portp);
40 void ofputil_format_port(uint16_t port, struct ds *);
41
42 /* Converting OFPFW10_NW_SRC_MASK and OFPFW10_NW_DST_MASK wildcard bit counts
43  * to and from IP bitmasks. */
44 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
45 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
46
47 /* Protocols.
48  *
49  * These are arranged from most portable to least portable, or alternatively
50  * from least powerful to most powerful.  Formats earlier on the list are more
51  * likely to be understood for the purpose of making requests, but formats
52  * later on the list are more likely to accurately describe a flow within a
53  * switch.
54  *
55  * On any given OpenFlow connection, a single protocol is in effect at any
56  * given time.  These values use separate bits only because that makes it easy
57  * to test whether a particular protocol is within a given set of protocols and
58  * to implement set union and intersection.
59  */
60 enum ofputil_protocol {
61     /* OpenFlow 1.0-based protocols. */
62     OFPUTIL_P_OF10     = 1 << 0, /* OpenFlow 1.0 flow format. */
63     OFPUTIL_P_OF10_TID = 1 << 1, /* OF1.0 + flow_mod_table_id extension. */
64 #define OFPUTIL_P_OF10_ANY (OFPUTIL_P_OF10 | OFPUTIL_P_OF10_TID)
65
66     /* OpenFlow 1.0 with NXM-based flow formats. */
67     OFPUTIL_P_NXM      = 1 << 2, /* Nicira extended match. */
68     OFPUTIL_P_NXM_TID  = 1 << 3, /* NXM + flow_mod_table_id extension. */
69 #define OFPUTIL_P_NXM_ANY (OFPUTIL_P_NXM | OFPUTIL_P_NXM_TID)
70
71     /* OpenFlow 1.2 */
72     OFPUTIL_P_OF12      = 1 << 4, /* OpenFlow 1.2 flow format. */
73
74     /* All protocols. */
75 #define OFPUTIL_P_ANY (OFPUTIL_P_OF10_ANY | OFPUTIL_P_NXM_ANY)
76
77     /* Protocols in which a specific table may be specified in flow_mods. */
78 #define OFPUTIL_P_TID (OFPUTIL_P_OF10_TID | OFPUTIL_P_NXM_TID)
79 };
80
81 /* Protocols to use for flow dumps, from most to least preferred. */
82 extern enum ofputil_protocol ofputil_flow_dump_protocols[];
83 extern size_t ofputil_n_flow_dump_protocols;
84
85 enum ofputil_protocol
86 ofputil_protocol_from_ofp_version(enum ofp_version version);
87 enum ofp_version  ofputil_protocol_to_ofp_version(enum ofputil_protocol);
88
89 bool ofputil_protocol_is_valid(enum ofputil_protocol);
90 enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
91                                                bool enable);
92 enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
93 enum ofputil_protocol ofputil_protocol_set_base(
94     enum ofputil_protocol cur, enum ofputil_protocol new_base);
95
96 const char *ofputil_protocol_to_string(enum ofputil_protocol);
97 char *ofputil_protocols_to_string(enum ofputil_protocol);
98 enum ofputil_protocol ofputil_protocols_from_string(const char *);
99 enum ofputil_protocol ofputil_usable_protocols(const struct match *);
100
101 void ofputil_format_version(struct ds *, enum ofp_version);
102 void ofputil_format_version_name(struct ds *, enum ofp_version);
103
104 /* A bitmap of version numbers
105  *
106  * Bit offsets correspond to ofp_version numbers which in turn correspond to
107  * wire-protocol numbers for Open Flow versions..  E.g. (1u << OFP11_VERSION)
108  * is the mask for Open Flow 1.1.  If the bit for a version is set then it is
109  * allowed, otherwise it is disallowed. */
110
111 void ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap);
112 void ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap);
113
114 /* Bitmap of OpenFlow versions that Open vSwitch supports. */
115 #define OFPUTIL_SUPPORTED_VERSIONS \
116         ((1u << OFP10_VERSION) | (1u << OFP12_VERSION))
117
118 /* Bitmap of OpenFlow versions to enable by default (a subset of
119  * OFPUTIL_SUPPORTED_VERSIONS). */
120 #define OFPUTIL_DEFAULT_VERSIONS (1u << OFP10_VERSION)
121
122 enum ofputil_protocol ofputil_protocols_from_string(const char *s);
123
124 const char *ofputil_version_to_string(enum ofp_version ofp_version);
125 uint32_t ofputil_versions_from_string(const char *s);
126
127 struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
128                                            enum ofputil_protocol want,
129                                            enum ofputil_protocol *next);
130
131 /* nx_flow_format */
132 struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
133 enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
134 bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
135 const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
136
137 /* Work with ofp10_match. */
138 void ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *);
139 void ofputil_match_from_ofp10_match(const struct ofp10_match *,
140                                     struct match *);
141 void ofputil_normalize_match(struct match *);
142 void ofputil_normalize_match_quiet(struct match *);
143 void ofputil_match_to_ofp10_match(const struct match *, struct ofp10_match *);
144
145 /* Work with ofp11_match. */
146 enum ofperr ofputil_pull_ofp11_match(struct ofpbuf *, struct match *,
147                                      uint16_t *padded_match_len);
148 enum ofperr ofputil_match_from_ofp11_match(const struct ofp11_match *,
149                                            struct match *);
150 void ofputil_match_to_ofp11_match(const struct match *, struct ofp11_match *);
151
152 /* dl_type translation between OpenFlow and 'struct flow' format. */
153 ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
154 ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
155
156 /* PACKET_IN. */
157 bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format);
158 int ofputil_packet_in_format_from_string(const char *);
159 const char *ofputil_packet_in_format_to_string(enum nx_packet_in_format);
160 struct ofpbuf *ofputil_make_set_packet_in_format(enum ofp_version,
161                                                  enum nx_packet_in_format);
162
163 /* NXT_FLOW_MOD_TABLE_ID extension. */
164 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
165
166 /* Protocol-independent flow_mod.
167  *
168  * The handling of cookies across multiple versions of OpenFlow is a bit
169  * confusing.  A full description of Open vSwitch's cookie handling is
170  * in the DESIGN file.  The following table shows the expected values of
171  * the cookie-related fields for the different flow_mod commands in
172  * OpenFlow 1.0 ("OF10") and NXM.  "<used>" and "-" indicate a value
173  * that may be populated and an ignored field, respectively.
174  *
175  *               cookie  cookie_mask  new_cookie
176  *               ======  ===========  ==========
177  * OF10 Add        -          0         <used>
178  * OF10 Modify     -          0         <used>
179  * OF10 Delete     -          0           -
180  * NXM Add         -          0         <used>
181  * NXM Modify    <used>     <used>      <used>
182  * NXM Delete    <used>     <used>        -
183  */
184 struct ofputil_flow_mod {
185     struct match match;
186     unsigned int priority;
187     ovs_be64 cookie;         /* Cookie bits to match. */
188     ovs_be64 cookie_mask;    /* 1-bit in each 'cookie' bit to match. */
189     ovs_be64 new_cookie;     /* New cookie to install or -1. */
190     uint8_t table_id;
191     uint16_t command;
192     uint16_t idle_timeout;
193     uint16_t hard_timeout;
194     uint32_t buffer_id;
195     uint16_t out_port;
196     uint16_t flags;
197     struct ofpact *ofpacts;     /* Series of "struct ofpact"s. */
198     size_t ofpacts_len;         /* Length of ofpacts, in bytes. */
199 };
200
201 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
202                                     const struct ofp_header *,
203                                     enum ofputil_protocol,
204                                     struct ofpbuf *ofpacts);
205 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
206                                        enum ofputil_protocol);
207
208 enum ofputil_protocol ofputil_flow_mod_usable_protocols(
209     const struct ofputil_flow_mod *fms, size_t n_fms);
210
211 /* Flow stats or aggregate stats request, independent of protocol. */
212 struct ofputil_flow_stats_request {
213     bool aggregate;             /* Aggregate results? */
214     struct match match;
215     ovs_be64 cookie;
216     ovs_be64 cookie_mask;
217     uint16_t out_port;
218     uint8_t table_id;
219 };
220
221 enum ofperr ofputil_decode_flow_stats_request(
222     struct ofputil_flow_stats_request *, const struct ofp_header *);
223 struct ofpbuf *ofputil_encode_flow_stats_request(
224     const struct ofputil_flow_stats_request *, enum ofputil_protocol);
225 enum ofputil_protocol ofputil_flow_stats_request_usable_protocols(
226     const struct ofputil_flow_stats_request *);
227
228 /* Flow stats reply, independent of protocol. */
229 struct ofputil_flow_stats {
230     struct match match;
231     ovs_be64 cookie;
232     uint8_t table_id;
233     uint32_t duration_sec;
234     uint32_t duration_nsec;
235     uint16_t priority;
236     uint16_t idle_timeout;
237     uint16_t hard_timeout;
238     int idle_age;               /* Seconds since last packet, -1 if unknown. */
239     int hard_age;               /* Seconds since last change, -1 if unknown. */
240     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
241     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
242     struct ofpact *ofpacts;
243     size_t ofpacts_len;
244 };
245
246 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
247                                     struct ofpbuf *msg,
248                                     bool flow_age_extension,
249                                     struct ofpbuf *ofpacts);
250 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
251                                      struct list *replies);
252
253 /* Aggregate stats reply, independent of protocol. */
254 struct ofputil_aggregate_stats {
255     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
256     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
257     uint32_t flow_count;
258 };
259
260 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
261     const struct ofputil_aggregate_stats *stats,
262     const struct ofp_header *request);
263 enum ofperr ofputil_decode_aggregate_stats_reply(
264     struct ofputil_aggregate_stats *,
265     const struct ofp_header *reply);
266
267 /* Flow removed message, independent of protocol. */
268 struct ofputil_flow_removed {
269     struct match match;
270     uint16_t priority;
271     ovs_be64 cookie;
272     uint8_t reason;             /* One of OFPRR_*. */
273     uint8_t table_id;           /* 255 if message didn't include table ID. */
274     uint32_t duration_sec;
275     uint32_t duration_nsec;
276     uint16_t idle_timeout;
277     uint16_t hard_timeout;
278     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
279     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
280 };
281
282 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
283                                         const struct ofp_header *);
284 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
285                                            enum ofputil_protocol);
286
287 /* Abstract packet-in message. */
288 struct ofputil_packet_in {
289     const void *packet;
290     size_t packet_len;
291
292     enum ofp_packet_in_reason reason;    /* One of OFPR_*. */
293     uint16_t controller_id;              /* Controller ID to send to. */
294     uint8_t table_id;
295     ovs_be64 cookie;
296
297     uint32_t buffer_id;
298     int send_len;
299     uint16_t total_len;         /* Full length of frame. */
300
301     struct flow_metadata fmd;   /* Metadata at creation time. */
302 };
303
304 enum ofperr ofputil_decode_packet_in(struct ofputil_packet_in *,
305                                      const struct ofp_header *);
306 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
307                                         enum ofputil_protocol protocol,
308                                         enum nx_packet_in_format);
309
310 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason);
311 bool ofputil_packet_in_reason_from_string(const char *,
312                                           enum ofp_packet_in_reason *);
313
314 /* Abstract packet-out message.
315  *
316  * ofputil_decode_packet_out() will ensure that 'in_port' is a physical port
317  * (OFPP_MAX or less) or one of OFPP_LOCAL, OFPP_NONE, or OFPP_CONTROLLER. */
318 struct ofputil_packet_out {
319     const void *packet;         /* Packet data, if buffer_id == UINT32_MAX. */
320     size_t packet_len;          /* Length of packet data in bytes. */
321     uint32_t buffer_id;         /* Buffer id or UINT32_MAX if no buffer. */
322     uint16_t in_port;           /* Packet's input port. */
323     struct ofpact *ofpacts;     /* Actions. */
324     size_t ofpacts_len;         /* Size of ofpacts in bytes. */
325 };
326
327 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
328                                       const struct ofp_header *,
329                                       struct ofpbuf *ofpacts);
330 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *,
331                                          enum ofputil_protocol protocol);
332
333 enum ofputil_port_config {
334     /* OpenFlow 1.0 and 1.1 share these values for these port config bits. */
335     OFPUTIL_PC_PORT_DOWN    = 1 << 0, /* Port is administratively down. */
336     OFPUTIL_PC_NO_RECV      = 1 << 2, /* Drop all packets received by port. */
337     OFPUTIL_PC_NO_FWD       = 1 << 5, /* Drop packets forwarded to port. */
338     OFPUTIL_PC_NO_PACKET_IN = 1 << 6, /* No send packet-in msgs for port. */
339     /* OpenFlow 1.0 only. */
340     OFPUTIL_PC_NO_STP       = 1 << 1, /* No 802.1D spanning tree for port. */
341     OFPUTIL_PC_NO_RECV_STP  = 1 << 3, /* Drop received 802.1D STP packets. */
342     OFPUTIL_PC_NO_FLOOD     = 1 << 4, /* Do not include port when flooding. */
343     /* There are no OpenFlow 1.1-only bits. */
344 };
345
346 enum ofputil_port_state {
347     /* OpenFlow 1.0 and 1.1 share this values for these port state bits. */
348     OFPUTIL_PS_LINK_DOWN   = 1 << 0, /* No physical link present. */
349     /* OpenFlow 1.1 only. */
350     OFPUTIL_PS_BLOCKED     = 1 << 1, /* Port is blocked */
351     OFPUTIL_PS_LIVE        = 1 << 2, /* Live for Fast Failover Group. */
352     /* OpenFlow 1.0 only. */
353     OFPUTIL_PS_STP_LISTEN  = 0 << 8, /* Not learning or relaying frames. */
354     OFPUTIL_PS_STP_LEARN   = 1 << 8, /* Learning but not relaying frames. */
355     OFPUTIL_PS_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
356     OFPUTIL_PS_STP_BLOCK   = 3 << 8, /* Not part of spanning tree. */
357     OFPUTIL_PS_STP_MASK    = 3 << 8  /* Bit mask for OFPPS10_STP_* values. */
358 };
359
360 /* Abstract ofp10_phy_port or ofp11_port. */
361 struct ofputil_phy_port {
362     uint16_t port_no;
363     uint8_t hw_addr[OFP_ETH_ALEN];
364     char name[OFP_MAX_PORT_NAME_LEN];
365     enum ofputil_port_config config;
366     enum ofputil_port_state state;
367
368     /* NETDEV_F_* feature bitmasks. */
369     enum netdev_features curr;       /* Current features. */
370     enum netdev_features advertised; /* Features advertised by the port. */
371     enum netdev_features supported;  /* Features supported by the port. */
372     enum netdev_features peer;       /* Features advertised by peer. */
373
374     /* Speed. */
375     uint32_t curr_speed;        /* Current speed, in kbps. */
376     uint32_t max_speed;         /* Maximum supported speed, in kbps. */
377 };
378
379 enum ofputil_capabilities {
380     /* OpenFlow 1.0, 1.1 and 1.2 share these values for these capabilities. */
381     OFPUTIL_C_FLOW_STATS     = 1 << 0,  /* Flow statistics. */
382     OFPUTIL_C_TABLE_STATS    = 1 << 1,  /* Table statistics. */
383     OFPUTIL_C_PORT_STATS     = 1 << 2,  /* Port statistics. */
384     OFPUTIL_C_IP_REASM       = 1 << 5,  /* Can reassemble IP fragments. */
385     OFPUTIL_C_QUEUE_STATS    = 1 << 6,  /* Queue statistics. */
386
387     /* OpenFlow 1.0 and 1.1 share this capability. */
388     OFPUTIL_C_ARP_MATCH_IP   = 1 << 7,  /* Match IP addresses in ARP pkts. */
389
390     /* OpenFlow 1.0 only. */
391     OFPUTIL_C_STP            = 1 << 3,  /* 802.1d spanning tree. */
392
393     /* OpenFlow 1.1 and 1.2 share this capability. */
394     OFPUTIL_C_GROUP_STATS    = 1 << 4,  /* Group statistics. */
395
396     /* OpenFlow 1.2 only */
397     OFPUTIL_C_PORT_BLOCKED   = 1 << 8,  /* Switch will block looping ports */
398 };
399
400 enum ofputil_action_bitmap {
401     OFPUTIL_A_OUTPUT         = 1 << 0,
402     OFPUTIL_A_SET_VLAN_VID   = 1 << 1,
403     OFPUTIL_A_SET_VLAN_PCP   = 1 << 2,
404     OFPUTIL_A_STRIP_VLAN     = 1 << 3,
405     OFPUTIL_A_SET_DL_SRC     = 1 << 4,
406     OFPUTIL_A_SET_DL_DST     = 1 << 5,
407     OFPUTIL_A_SET_NW_SRC     = 1 << 6,
408     OFPUTIL_A_SET_NW_DST     = 1 << 7,
409     OFPUTIL_A_SET_NW_ECN     = 1 << 8,
410     OFPUTIL_A_SET_NW_TOS     = 1 << 9,
411     OFPUTIL_A_SET_TP_SRC     = 1 << 10,
412     OFPUTIL_A_SET_TP_DST     = 1 << 11,
413     OFPUTIL_A_ENQUEUE        = 1 << 12,
414     OFPUTIL_A_COPY_TTL_OUT   = 1 << 13,
415     OFPUTIL_A_COPY_TTL_IN    = 1 << 14,
416     OFPUTIL_A_SET_MPLS_LABEL = 1 << 15,
417     OFPUTIL_A_SET_MPLS_TC    = 1 << 16,
418     OFPUTIL_A_SET_MPLS_TTL   = 1 << 17,
419     OFPUTIL_A_DEC_MPLS_TTL   = 1 << 18,
420     OFPUTIL_A_PUSH_VLAN      = 1 << 19,
421     OFPUTIL_A_POP_VLAN       = 1 << 20,
422     OFPUTIL_A_PUSH_MPLS      = 1 << 21,
423     OFPUTIL_A_POP_MPLS       = 1 << 22,
424     OFPUTIL_A_SET_QUEUE      = 1 << 23,
425     OFPUTIL_A_GROUP          = 1 << 24,
426     OFPUTIL_A_SET_NW_TTL     = 1 << 25,
427     OFPUTIL_A_DEC_NW_TTL     = 1 << 26,
428     OFPUTIL_A_SET_FIELD      = 1 << 27,
429 };
430
431 /* Abstract ofp_switch_features. */
432 struct ofputil_switch_features {
433     uint64_t datapath_id;       /* Datapath unique ID. */
434     uint32_t n_buffers;         /* Max packets buffered at once. */
435     uint8_t n_tables;           /* Number of tables supported by datapath. */
436     enum ofputil_capabilities capabilities;
437     enum ofputil_action_bitmap actions;
438 };
439
440 enum ofperr ofputil_decode_switch_features(const struct ofp_header *,
441                                            struct ofputil_switch_features *,
442                                            struct ofpbuf *);
443
444 struct ofpbuf *ofputil_encode_switch_features(
445     const struct ofputil_switch_features *, enum ofputil_protocol,
446     ovs_be32 xid);
447 void ofputil_put_switch_features_port(const struct ofputil_phy_port *,
448                                       struct ofpbuf *);
449 bool ofputil_switch_features_ports_trunc(struct ofpbuf *b);
450
451 /* phy_port helper functions. */
452 int ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *,
453                           struct ofputil_phy_port *);
454 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *);
455
456 /* Abstract ofp_port_status. */
457 struct ofputil_port_status {
458     enum ofp_port_reason reason;
459     struct ofputil_phy_port desc;
460 };
461
462 enum ofperr ofputil_decode_port_status(const struct ofp_header *,
463                                        struct ofputil_port_status *);
464 struct ofpbuf *ofputil_encode_port_status(const struct ofputil_port_status *,
465                                           enum ofputil_protocol);
466
467 /* Abstract ofp_port_mod. */
468 struct ofputil_port_mod {
469     uint16_t port_no;
470     uint8_t hw_addr[OFP_ETH_ALEN];
471     enum ofputil_port_config config;
472     enum ofputil_port_config mask;
473     enum netdev_features advertise;
474 };
475
476 enum ofperr ofputil_decode_port_mod(const struct ofp_header *,
477                                     struct ofputil_port_mod *);
478 struct ofpbuf *ofputil_encode_port_mod(const struct ofputil_port_mod *,
479                                        enum ofputil_protocol);
480
481 /* Abstract table stats.
482  *
483  * For now we use ofp12_table_stats as a superset of the other protocol
484  * versions' table stats. */
485
486 struct ofpbuf *ofputil_encode_table_stats_reply(
487     const struct ofp12_table_stats[], int n,
488     const struct ofp_header *request);
489
490 /* Abstract nx_flow_monitor_request. */
491 struct ofputil_flow_monitor_request {
492     uint32_t id;
493     enum nx_flow_monitor_flags flags;
494     uint16_t out_port;
495     uint8_t table_id;
496     struct match match;
497 };
498
499 int ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *,
500                                         struct ofpbuf *msg);
501 void ofputil_append_flow_monitor_request(
502     const struct ofputil_flow_monitor_request *, struct ofpbuf *msg);
503
504 /* Abstract nx_flow_update. */
505 struct ofputil_flow_update {
506     enum nx_flow_update_event event;
507
508     /* Used only for NXFME_ADDED, NXFME_DELETED, NXFME_MODIFIED. */
509     enum ofp_flow_removed_reason reason;
510     uint16_t idle_timeout;
511     uint16_t hard_timeout;
512     uint8_t table_id;
513     ovs_be64 cookie;
514     struct match *match;
515     uint16_t priority;
516     struct ofpact *ofpacts;
517     size_t ofpacts_len;
518
519     /* Used only for NXFME_ABBREV. */
520     ovs_be32 xid;
521 };
522
523 int ofputil_decode_flow_update(struct ofputil_flow_update *,
524                                struct ofpbuf *msg, struct ofpbuf *ofpacts);
525 void ofputil_start_flow_update(struct list *replies);
526 void ofputil_append_flow_update(const struct ofputil_flow_update *,
527                                 struct list *replies);
528
529 /* Abstract nx_flow_monitor_cancel. */
530 uint32_t ofputil_decode_flow_monitor_cancel(const struct ofp_header *);
531 struct ofpbuf *ofputil_encode_flow_monitor_cancel(uint32_t id);
532
533 /* Encoding OpenFlow stats messages. */
534 void ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
535                                           const struct ofputil_phy_port *pp,
536                                           struct list *replies);
537
538 /* Encoding simple OpenFlow messages. */
539 struct ofpbuf *make_echo_request(enum ofp_version);
540 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
541
542 struct ofpbuf *ofputil_encode_barrier_request(enum ofp_version);
543
544 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
545 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
546
547 \f
548 /* Actions. */
549
550 /* The type of an action.
551  *
552  * For each implemented OFPAT10_* and NXAST_* action type, there is a
553  * corresponding constant prefixed with OFPUTIL_, e.g.:
554  *
555  * OFPUTIL_OFPAT10_OUTPUT
556  * OFPUTIL_OFPAT10_SET_VLAN_VID
557  * OFPUTIL_OFPAT10_SET_VLAN_PCP
558  * OFPUTIL_OFPAT10_STRIP_VLAN
559  * OFPUTIL_OFPAT10_SET_DL_SRC
560  * OFPUTIL_OFPAT10_SET_DL_DST
561  * OFPUTIL_OFPAT10_SET_NW_SRC
562  * OFPUTIL_OFPAT10_SET_NW_DST
563  * OFPUTIL_OFPAT10_SET_NW_TOS
564  * OFPUTIL_OFPAT10_SET_TP_SRC
565  * OFPUTIL_OFPAT10_SET_TP_DST
566  * OFPUTIL_OFPAT10_ENQUEUE
567  * OFPUTIL_NXAST_RESUBMIT
568  * OFPUTIL_NXAST_SET_TUNNEL
569  * OFPUTIL_NXAST_SET_METADATA
570  * OFPUTIL_NXAST_SET_QUEUE
571  * OFPUTIL_NXAST_POP_QUEUE
572  * OFPUTIL_NXAST_REG_MOVE
573  * OFPUTIL_NXAST_REG_LOAD
574  * OFPUTIL_NXAST_NOTE
575  * OFPUTIL_NXAST_SET_TUNNEL64
576  * OFPUTIL_NXAST_MULTIPATH
577  * OFPUTIL_NXAST_AUTOPATH
578  * OFPUTIL_NXAST_BUNDLE
579  * OFPUTIL_NXAST_BUNDLE_LOAD
580  * OFPUTIL_NXAST_RESUBMIT_TABLE
581  * OFPUTIL_NXAST_OUTPUT_REG
582  * OFPUTIL_NXAST_LEARN
583  * OFPUTIL_NXAST_DEC_TTL
584  * OFPUTIL_NXAST_FIN_TIMEOUT
585  *
586  * (The above list helps developers who want to "grep" for these definitions.)
587  */
588 enum OVS_PACKED_ENUM ofputil_action_code {
589     OFPUTIL_ACTION_INVALID,
590 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             OFPUTIL_##ENUM,
591 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
592 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   OFPUTIL_##ENUM,
593 #include "ofp-util.def"
594 };
595
596 /* The number of values of "enum ofputil_action_code". */
597 enum {
598 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             + 1
599 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
600 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   + 1
601     OFPUTIL_N_ACTIONS = 1
602 #include "ofp-util.def"
603 };
604
605 int ofputil_action_code_from_name(const char *);
606
607 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
608
609 /* For each OpenFlow action <ENUM> that has a corresponding action structure
610  * struct <STRUCT>, this defines two functions:
611  *
612  *   void ofputil_init_<ENUM>(struct <STRUCT> *action);
613  *
614  *     Initializes the parts of 'action' that identify it as having type <ENUM>
615  *     and length 'sizeof *action' and zeros the rest.  For actions that have
616  *     variable length, the length used and cleared is that of struct <STRUCT>.
617  *
618  *  struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
619  *
620  *     Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
621  *     initializes it with ofputil_init_<ENUM>(), and returns it.
622  */
623 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)              \
624     void ofputil_init_##ENUM(struct STRUCT *);          \
625     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
626 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
627     void ofputil_init_##ENUM(struct STRUCT *);          \
628     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
629 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
630     void ofputil_init_##ENUM(struct STRUCT *);          \
631     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
632 #include "ofp-util.def"
633
634 #define OFP_ACTION_ALIGN 8      /* Alignment of ofp_actions. */
635
636 enum ofperr validate_actions(const union ofp_action *, size_t n_actions,
637                              const struct flow *, int max_ports);
638 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
639
640 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
641                                  union ofp_action **, size_t *);
642
643 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
644                            const union ofp_action *b, size_t n_b);
645 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
646
647 /* Handy utility for parsing flows and actions. */
648 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
649
650 struct ofpbuf *ofputlil_dump_ports(enum ofp_version ofp_version, int16_t port);
651
652 struct ofputil_port_stats {
653     uint16_t port_no;
654     struct netdev_stats stats;
655 };
656
657 struct ofpbuf *ofputil_encode_dump_ports_request(enum ofp_version ofp_version,
658                                                  int16_t port);
659 void ofputil_append_port_stat(struct list *replies,
660                               const struct ofputil_port_stats *ops);
661 size_t ofputil_count_port_stats(const struct ofp_header *);
662 int ofputil_decode_port_stats(struct ofputil_port_stats *, struct ofpbuf *msg);
663 enum ofperr ofputil_decode_port_stats_request(const struct ofp_header *request,
664                                               uint16_t *ofp10_port);
665
666 struct ofputil_queue_stats_request {
667     uint16_t port_no;
668     uint32_t queue_id;
669 };
670
671 enum ofperr
672 ofputil_decode_queue_stats_request(const struct ofp_header *request,
673                                    struct ofputil_queue_stats_request *oqsr);
674 struct ofpbuf *
675 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
676                                    const struct ofputil_queue_stats_request *oqsr);
677
678 struct ofputil_queue_stats {
679     uint16_t port_no;
680     uint32_t queue_id;
681     struct netdev_queue_stats stats;
682 };
683
684 size_t ofputil_count_queue_stats(const struct ofp_header *);
685 int ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg);
686 void ofputil_append_queue_stat(struct list *replies,
687                                const struct ofputil_queue_stats *oqs);
688
689 #endif /* ofp-util.h */