ofp-util: Meter fixes.
[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 /* Meter band configuration for all supported band types. */
518 struct ofputil_meter_band {
519     uint16_t type;
520     uint8_t prec_level;         /* Non-zero if type == OFPMBT_DSCP_REMARK. */
521     uint32_t rate;
522     uint32_t burst_size;
523 };
524
525 struct ofputil_meter_band_stats {
526     uint64_t packet_count;
527     uint64_t byte_count;
528 };
529
530 struct ofputil_meter_config {
531     uint32_t meter_id;
532     uint16_t flags;
533     uint16_t n_bands;
534     struct ofputil_meter_band *bands;
535 };
536
537 /* Abstract ofp_meter_mod. */
538 struct ofputil_meter_mod {
539     uint16_t command;
540     struct ofputil_meter_config meter;
541 };
542
543 struct ofputil_meter_stats {
544     uint32_t meter_id;
545     uint32_t flow_count;
546     uint64_t packet_in_count;
547     uint64_t byte_in_count;
548     uint32_t duration_sec;
549     uint32_t duration_nsec;
550     uint16_t n_bands;
551     struct ofputil_meter_band_stats *bands;
552 };
553
554 struct ofputil_meter_features {
555     uint32_t max_meters;        /* Maximum number of meters. */
556     uint32_t band_types;        /* Can support max 32 band types. */
557     uint32_t capabilities;      /* Supported flags. */
558     uint8_t  max_bands;
559     uint8_t  max_color;
560 };
561
562 enum ofperr ofputil_decode_meter_mod(const struct ofp_header *,
563                                      struct ofputil_meter_mod *,
564                                      struct ofpbuf *bands);
565 struct ofpbuf *ofputil_encode_meter_mod(enum ofp_version,
566                                         const struct ofputil_meter_mod *);
567
568 void ofputil_decode_meter_features(const struct ofp_header *,
569                                    struct ofputil_meter_features *);
570 struct ofpbuf *ofputil_encode_meter_features_reply(const struct
571                                                    ofputil_meter_features *,
572                                                    const struct ofp_header *
573                                                    request);
574 void ofputil_decode_meter_request(const struct ofp_header *,
575                                   uint32_t *meter_id);
576
577 void ofputil_append_meter_config(struct list *replies,
578                                  const struct ofputil_meter_config *);
579
580 void ofputil_append_meter_stats(struct list *replies,
581                                 const struct ofputil_meter_stats *);
582
583 enum ofputil_meter_request_type {
584     OFPUTIL_METER_FEATURES,
585     OFPUTIL_METER_CONFIG,
586     OFPUTIL_METER_STATS
587 };
588
589 struct ofpbuf *ofputil_encode_meter_request(enum ofp_version,
590                                             enum ofputil_meter_request_type,
591                                             uint32_t meter_id);
592
593 int ofputil_decode_meter_stats(struct ofpbuf *,
594                                struct ofputil_meter_stats *,
595                                struct ofpbuf *bands);
596
597 int ofputil_decode_meter_config(struct ofpbuf *,
598                                 struct ofputil_meter_config *,
599                                 struct ofpbuf *bands);
600
601 /* Type for meter_id in ofproto provider interface, UINT32_MAX if invalid. */
602 typedef struct { uint32_t uint32; } ofproto_meter_id;
603
604 /* Abstract ofp_role_request and reply. */
605 struct ofputil_role_request {
606     enum ofp12_controller_role role;
607     bool have_generation_id;
608     uint64_t generation_id;
609 };
610
611 enum ofperr ofputil_decode_role_message(const struct ofp_header *,
612                                         struct ofputil_role_request *);
613 struct ofpbuf *ofputil_encode_role_reply(const struct ofp_header *,
614                                          const struct ofputil_role_request *);
615
616 /* Abstract table stats.
617  *
618  * For now we use ofp12_table_stats as a superset of the other protocol
619  * versions' table stats. */
620
621 struct ofpbuf *ofputil_encode_table_stats_reply(
622     const struct ofp12_table_stats[], int n,
623     const struct ofp_header *request);
624
625 /* Abstract nx_flow_monitor_request. */
626 struct ofputil_flow_monitor_request {
627     uint32_t id;
628     enum nx_flow_monitor_flags flags;
629     ofp_port_t out_port;
630     uint8_t table_id;
631     struct match match;
632 };
633
634 int ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *,
635                                         struct ofpbuf *msg);
636 void ofputil_append_flow_monitor_request(
637     const struct ofputil_flow_monitor_request *, struct ofpbuf *msg);
638
639 /* Abstract nx_flow_update. */
640 struct ofputil_flow_update {
641     enum nx_flow_update_event event;
642
643     /* Used only for NXFME_ADDED, NXFME_DELETED, NXFME_MODIFIED. */
644     enum ofp_flow_removed_reason reason;
645     uint16_t idle_timeout;
646     uint16_t hard_timeout;
647     uint8_t table_id;
648     ovs_be64 cookie;
649     struct match *match;
650     uint16_t priority;
651     struct ofpact *ofpacts;
652     size_t ofpacts_len;
653
654     /* Used only for NXFME_ABBREV. */
655     ovs_be32 xid;
656 };
657
658 int ofputil_decode_flow_update(struct ofputil_flow_update *,
659                                struct ofpbuf *msg, struct ofpbuf *ofpacts);
660 void ofputil_start_flow_update(struct list *replies);
661 void ofputil_append_flow_update(const struct ofputil_flow_update *,
662                                 struct list *replies);
663
664 /* Abstract nx_flow_monitor_cancel. */
665 uint32_t ofputil_decode_flow_monitor_cancel(const struct ofp_header *);
666 struct ofpbuf *ofputil_encode_flow_monitor_cancel(uint32_t id);
667
668 /* Encoding OpenFlow stats messages. */
669 void ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
670                                           const struct ofputil_phy_port *pp,
671                                           struct list *replies);
672
673 /* Encoding simple OpenFlow messages. */
674 struct ofpbuf *make_echo_request(enum ofp_version);
675 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
676
677 struct ofpbuf *ofputil_encode_barrier_request(enum ofp_version);
678
679 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
680 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
681
682 \f
683 /* Actions. */
684
685 /* The type of an action.
686  *
687  * For each implemented OFPAT10_* and NXAST_* action type, there is a
688  * corresponding constant prefixed with OFPUTIL_, e.g.:
689  *
690  * OFPUTIL_OFPAT10_OUTPUT
691  * OFPUTIL_OFPAT10_SET_VLAN_VID
692  * OFPUTIL_OFPAT10_SET_VLAN_PCP
693  * OFPUTIL_OFPAT10_STRIP_VLAN
694  * OFPUTIL_OFPAT10_SET_DL_SRC
695  * OFPUTIL_OFPAT10_SET_DL_DST
696  * OFPUTIL_OFPAT10_SET_NW_SRC
697  * OFPUTIL_OFPAT10_SET_NW_DST
698  * OFPUTIL_OFPAT10_SET_NW_TOS
699  * OFPUTIL_OFPAT10_SET_TP_SRC
700  * OFPUTIL_OFPAT10_SET_TP_DST
701  * OFPUTIL_OFPAT10_ENQUEUE
702  * OFPUTIL_NXAST_RESUBMIT
703  * OFPUTIL_NXAST_SET_TUNNEL
704  * OFPUTIL_NXAST_SET_METADATA
705  * OFPUTIL_NXAST_SET_QUEUE
706  * OFPUTIL_NXAST_POP_QUEUE
707  * OFPUTIL_NXAST_REG_MOVE
708  * OFPUTIL_NXAST_REG_LOAD
709  * OFPUTIL_NXAST_NOTE
710  * OFPUTIL_NXAST_SET_TUNNEL64
711  * OFPUTIL_NXAST_MULTIPATH
712  * OFPUTIL_NXAST_BUNDLE
713  * OFPUTIL_NXAST_BUNDLE_LOAD
714  * OFPUTIL_NXAST_RESUBMIT_TABLE
715  * OFPUTIL_NXAST_OUTPUT_REG
716  * OFPUTIL_NXAST_LEARN
717  * OFPUTIL_NXAST_DEC_TTL
718  * OFPUTIL_NXAST_FIN_TIMEOUT
719  *
720  * (The above list helps developers who want to "grep" for these definitions.)
721  */
722 enum OVS_PACKED_ENUM ofputil_action_code {
723     OFPUTIL_ACTION_INVALID,
724 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             OFPUTIL_##ENUM,
725 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
726 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   OFPUTIL_##ENUM,
727 #include "ofp-util.def"
728 };
729
730 /* The number of values of "enum ofputil_action_code". */
731 enum {
732 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             + 1
733 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
734 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   + 1
735     OFPUTIL_N_ACTIONS = 1
736 #include "ofp-util.def"
737 };
738
739 int ofputil_action_code_from_name(const char *);
740
741 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
742
743 /* For each OpenFlow action <ENUM> that has a corresponding action structure
744  * struct <STRUCT>, this defines two functions:
745  *
746  *   void ofputil_init_<ENUM>(struct <STRUCT> *action);
747  *
748  *     Initializes the parts of 'action' that identify it as having type <ENUM>
749  *     and length 'sizeof *action' and zeros the rest.  For actions that have
750  *     variable length, the length used and cleared is that of struct <STRUCT>.
751  *
752  *  struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
753  *
754  *     Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
755  *     initializes it with ofputil_init_<ENUM>(), and returns it.
756  */
757 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)              \
758     void ofputil_init_##ENUM(struct STRUCT *);          \
759     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
760 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
761     void ofputil_init_##ENUM(struct STRUCT *);          \
762     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
763 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
764     void ofputil_init_##ENUM(struct STRUCT *);          \
765     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
766 #include "ofp-util.def"
767
768 #define OFP_ACTION_ALIGN 8      /* Alignment of ofp_actions. */
769
770 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
771
772 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
773                                  union ofp_action **, size_t *);
774
775 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
776                            const union ofp_action *b, size_t n_b);
777 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
778
779 /* Handy utility for parsing flows and actions. */
780 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
781
782 struct ofputil_port_stats {
783     ofp_port_t port_no;
784     struct netdev_stats stats;
785     uint32_t duration_sec;      /* UINT32_MAX if unknown. */
786     uint32_t duration_nsec;
787 };
788
789 struct ofpbuf *ofputil_encode_dump_ports_request(enum ofp_version ofp_version,
790                                                  ofp_port_t port);
791 void ofputil_append_port_stat(struct list *replies,
792                               const struct ofputil_port_stats *ops);
793 size_t ofputil_count_port_stats(const struct ofp_header *);
794 int ofputil_decode_port_stats(struct ofputil_port_stats *, struct ofpbuf *msg);
795 enum ofperr ofputil_decode_port_stats_request(const struct ofp_header *request,
796                                               ofp_port_t *ofp10_port);
797
798 struct ofputil_queue_stats_request {
799     ofp_port_t port_no;           /* OFPP_ANY means "all ports". */
800     uint32_t queue_id;
801 };
802
803 enum ofperr
804 ofputil_decode_queue_stats_request(const struct ofp_header *request,
805                                    struct ofputil_queue_stats_request *oqsr);
806 struct ofpbuf *
807 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
808                                    const struct ofputil_queue_stats_request *oqsr);
809
810 struct ofputil_queue_stats {
811     ofp_port_t port_no;
812     uint32_t queue_id;
813     struct netdev_queue_stats stats;
814 };
815
816 size_t ofputil_count_queue_stats(const struct ofp_header *);
817 int ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg);
818 void ofputil_append_queue_stat(struct list *replies,
819                                const struct ofputil_queue_stats *oqs);
820
821 #endif /* ofp-util.h */