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