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