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