ofp-util: Add 'modify_cookie' to struct ofputil_flow_mod, to support OF1.1.
[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.  See DESIGN for the details. */
201 struct ofputil_flow_mod {
202     struct match match;
203     unsigned int priority;
204
205     /* Cookie matching.  The flow_mod affects only flows that have cookies that
206      * bitwise match 'cookie' bits in positions where 'cookie_mask has 1-bits.
207      *
208      * 'cookie_mask' should be zero for OFPFC_ADD flow_mods. */
209     ovs_be64 cookie;         /* Cookie bits to match. */
210     ovs_be64 cookie_mask;    /* 1-bit in each 'cookie' bit to match. */
211
212     /* Cookie changes.
213      *
214      * OFPFC_ADD uses 'new_cookie' as the new flow's cookie.  'new_cookie'
215      * should not be UINT64_MAX.
216      *
217      * OFPFC_MODIFY and OFPFC_MODIFY_STRICT have two cases:
218      *
219      *   - If one or more matching flows exist and 'modify_cookie' is true,
220      *     then the flow_mod changes the existing flows' cookies to
221      *     'new_cookie'.  'new_cookie' should not be UINT64_MAX.
222      *
223      *   - If no matching flow exists, 'new_cookie' is not UINT64_MAX, and
224      *     'cookie_mask' is 0, then the flow_mod adds a new flow with
225      *     'new_cookie' as its cookie.
226      */
227     ovs_be64 new_cookie;     /* New cookie to install or UINT64_MAX. */
228     bool modify_cookie;      /* Set cookie of existing flow to 'new_cookie'? */
229
230     uint8_t table_id;
231     uint16_t command;
232     uint16_t idle_timeout;
233     uint16_t hard_timeout;
234     uint32_t buffer_id;
235     ofp_port_t out_port;
236     uint16_t flags;
237     struct ofpact *ofpacts;     /* Series of "struct ofpact"s. */
238     size_t ofpacts_len;         /* Length of ofpacts, in bytes. */
239 };
240
241 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
242                                     const struct ofp_header *,
243                                     enum ofputil_protocol,
244                                     struct ofpbuf *ofpacts);
245 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
246                                        enum ofputil_protocol);
247
248 enum ofputil_protocol ofputil_flow_mod_usable_protocols(
249     const struct ofputil_flow_mod *fms, size_t n_fms);
250
251 /* Flow stats or aggregate stats request, independent of protocol. */
252 struct ofputil_flow_stats_request {
253     bool aggregate;             /* Aggregate results? */
254     struct match match;
255     ovs_be64 cookie;
256     ovs_be64 cookie_mask;
257     ofp_port_t out_port;
258     uint8_t table_id;
259 };
260
261 enum ofperr ofputil_decode_flow_stats_request(
262     struct ofputil_flow_stats_request *, const struct ofp_header *);
263 struct ofpbuf *ofputil_encode_flow_stats_request(
264     const struct ofputil_flow_stats_request *, enum ofputil_protocol);
265 enum ofputil_protocol ofputil_flow_stats_request_usable_protocols(
266     const struct ofputil_flow_stats_request *);
267
268 /* Flow stats reply, independent of protocol. */
269 struct ofputil_flow_stats {
270     struct match match;
271     ovs_be64 cookie;
272     uint8_t table_id;
273     uint32_t duration_sec;
274     uint32_t duration_nsec;
275     uint16_t priority;
276     uint16_t idle_timeout;
277     uint16_t hard_timeout;
278     int idle_age;               /* Seconds since last packet, -1 if unknown. */
279     int hard_age;               /* Seconds since last change, -1 if unknown. */
280     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
281     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
282     struct ofpact *ofpacts;
283     size_t ofpacts_len;
284     uint16_t flags;             /* Added for OF 1.3 */
285 };
286
287 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
288                                     struct ofpbuf *msg,
289                                     bool flow_age_extension,
290                                     struct ofpbuf *ofpacts);
291 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
292                                      struct list *replies);
293
294 /* Aggregate stats reply, independent of protocol. */
295 struct ofputil_aggregate_stats {
296     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
297     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
298     uint32_t flow_count;
299 };
300
301 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
302     const struct ofputil_aggregate_stats *stats,
303     const struct ofp_header *request);
304 enum ofperr ofputil_decode_aggregate_stats_reply(
305     struct ofputil_aggregate_stats *,
306     const struct ofp_header *reply);
307
308 /* Flow removed message, independent of protocol. */
309 struct ofputil_flow_removed {
310     struct match match;
311     uint16_t priority;
312     ovs_be64 cookie;
313     uint8_t reason;             /* One of OFPRR_*. */
314     uint8_t table_id;           /* 255 if message didn't include table ID. */
315     uint32_t duration_sec;
316     uint32_t duration_nsec;
317     uint16_t idle_timeout;
318     uint16_t hard_timeout;
319     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
320     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
321 };
322
323 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
324                                         const struct ofp_header *);
325 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
326                                            enum ofputil_protocol);
327
328 /* Abstract packet-in message. */
329 struct ofputil_packet_in {
330     const void *packet;
331     size_t packet_len;
332
333     enum ofp_packet_in_reason reason;    /* One of OFPR_*. */
334     uint16_t controller_id;              /* Controller ID to send to. */
335     uint8_t table_id;
336     ovs_be64 cookie;
337
338     uint32_t buffer_id;
339     int send_len;
340     uint16_t total_len;         /* Full length of frame. */
341
342     struct flow_metadata fmd;   /* Metadata at creation time. */
343 };
344
345 enum ofperr ofputil_decode_packet_in(struct ofputil_packet_in *,
346                                      const struct ofp_header *);
347 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
348                                         enum ofputil_protocol protocol,
349                                         enum nx_packet_in_format);
350
351 enum { OFPUTIL_PACKET_IN_REASON_BUFSIZE = INT_STRLEN(int) + 1 };
352 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason,
353                                                char *reasonbuf,
354                                                size_t bufsize);
355 bool ofputil_packet_in_reason_from_string(const char *,
356                                           enum ofp_packet_in_reason *);
357
358 /* Abstract packet-out message.
359  *
360  * ofputil_decode_packet_out() will ensure that 'in_port' is a physical port
361  * (OFPP_MAX or less) or one of OFPP_LOCAL, OFPP_NONE, or OFPP_CONTROLLER. */
362 struct ofputil_packet_out {
363     const void *packet;         /* Packet data, if buffer_id == UINT32_MAX. */
364     size_t packet_len;          /* Length of packet data in bytes. */
365     uint32_t buffer_id;         /* Buffer id or UINT32_MAX if no buffer. */
366     ofp_port_t in_port;         /* Packet's input port. */
367     struct ofpact *ofpacts;     /* Actions. */
368     size_t ofpacts_len;         /* Size of ofpacts in bytes. */
369 };
370
371 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
372                                       const struct ofp_header *,
373                                       struct ofpbuf *ofpacts);
374 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *,
375                                          enum ofputil_protocol protocol);
376
377 enum ofputil_port_config {
378     /* OpenFlow 1.0 and 1.1 share these values for these port config bits. */
379     OFPUTIL_PC_PORT_DOWN    = 1 << 0, /* Port is administratively down. */
380     OFPUTIL_PC_NO_RECV      = 1 << 2, /* Drop all packets received by port. */
381     OFPUTIL_PC_NO_FWD       = 1 << 5, /* Drop packets forwarded to port. */
382     OFPUTIL_PC_NO_PACKET_IN = 1 << 6, /* No send packet-in msgs for port. */
383     /* OpenFlow 1.0 only. */
384     OFPUTIL_PC_NO_STP       = 1 << 1, /* No 802.1D spanning tree for port. */
385     OFPUTIL_PC_NO_RECV_STP  = 1 << 3, /* Drop received 802.1D STP packets. */
386     OFPUTIL_PC_NO_FLOOD     = 1 << 4, /* Do not include port when flooding. */
387     /* There are no OpenFlow 1.1-only bits. */
388 };
389
390 enum ofputil_port_state {
391     /* OpenFlow 1.0 and 1.1 share this values for these port state bits. */
392     OFPUTIL_PS_LINK_DOWN   = 1 << 0, /* No physical link present. */
393     /* OpenFlow 1.1 only. */
394     OFPUTIL_PS_BLOCKED     = 1 << 1, /* Port is blocked */
395     OFPUTIL_PS_LIVE        = 1 << 2, /* Live for Fast Failover Group. */
396     /* OpenFlow 1.0 only. */
397     OFPUTIL_PS_STP_LISTEN  = 0 << 8, /* Not learning or relaying frames. */
398     OFPUTIL_PS_STP_LEARN   = 1 << 8, /* Learning but not relaying frames. */
399     OFPUTIL_PS_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
400     OFPUTIL_PS_STP_BLOCK   = 3 << 8, /* Not part of spanning tree. */
401     OFPUTIL_PS_STP_MASK    = 3 << 8  /* Bit mask for OFPPS10_STP_* values. */
402 };
403
404 /* Abstract ofp10_phy_port or ofp11_port. */
405 struct ofputil_phy_port {
406     ofp_port_t port_no;
407     uint8_t hw_addr[OFP_ETH_ALEN];
408     char name[OFP_MAX_PORT_NAME_LEN];
409     enum ofputil_port_config config;
410     enum ofputil_port_state state;
411
412     /* NETDEV_F_* feature bitmasks. */
413     enum netdev_features curr;       /* Current features. */
414     enum netdev_features advertised; /* Features advertised by the port. */
415     enum netdev_features supported;  /* Features supported by the port. */
416     enum netdev_features peer;       /* Features advertised by peer. */
417
418     /* Speed. */
419     uint32_t curr_speed;        /* Current speed, in kbps. */
420     uint32_t max_speed;         /* Maximum supported speed, in kbps. */
421 };
422
423 enum ofputil_capabilities {
424     /* OpenFlow 1.0, 1.1, 1.2, and 1.3 share these capability values. */
425     OFPUTIL_C_FLOW_STATS     = 1 << 0,  /* Flow statistics. */
426     OFPUTIL_C_TABLE_STATS    = 1 << 1,  /* Table statistics. */
427     OFPUTIL_C_PORT_STATS     = 1 << 2,  /* Port statistics. */
428     OFPUTIL_C_IP_REASM       = 1 << 5,  /* Can reassemble IP fragments. */
429     OFPUTIL_C_QUEUE_STATS    = 1 << 6,  /* Queue statistics. */
430
431     /* OpenFlow 1.0 and 1.1 share this capability. */
432     OFPUTIL_C_ARP_MATCH_IP   = 1 << 7,  /* Match IP addresses in ARP pkts. */
433
434     /* OpenFlow 1.0 only. */
435     OFPUTIL_C_STP            = 1 << 3,  /* 802.1d spanning tree. */
436
437     /* OpenFlow 1.1, 1.2, and 1.3 share this capability. */
438     OFPUTIL_C_GROUP_STATS    = 1 << 4,  /* Group statistics. */
439
440     /* OpenFlow 1.2 and 1.3 share this capability */
441     OFPUTIL_C_PORT_BLOCKED   = 1 << 8,  /* Switch will block looping ports */
442 };
443
444 enum ofputil_action_bitmap {
445     OFPUTIL_A_OUTPUT         = 1 << 0,
446     OFPUTIL_A_SET_VLAN_VID   = 1 << 1,
447     OFPUTIL_A_SET_VLAN_PCP   = 1 << 2,
448     OFPUTIL_A_STRIP_VLAN     = 1 << 3,
449     OFPUTIL_A_SET_DL_SRC     = 1 << 4,
450     OFPUTIL_A_SET_DL_DST     = 1 << 5,
451     OFPUTIL_A_SET_NW_SRC     = 1 << 6,
452     OFPUTIL_A_SET_NW_DST     = 1 << 7,
453     OFPUTIL_A_SET_NW_ECN     = 1 << 8,
454     OFPUTIL_A_SET_NW_TOS     = 1 << 9,
455     OFPUTIL_A_SET_TP_SRC     = 1 << 10,
456     OFPUTIL_A_SET_TP_DST     = 1 << 11,
457     OFPUTIL_A_ENQUEUE        = 1 << 12,
458     OFPUTIL_A_COPY_TTL_OUT   = 1 << 13,
459     OFPUTIL_A_COPY_TTL_IN    = 1 << 14,
460     OFPUTIL_A_SET_MPLS_LABEL = 1 << 15,
461     OFPUTIL_A_SET_MPLS_TC    = 1 << 16,
462     OFPUTIL_A_SET_MPLS_TTL   = 1 << 17,
463     OFPUTIL_A_DEC_MPLS_TTL   = 1 << 18,
464     OFPUTIL_A_PUSH_VLAN      = 1 << 19,
465     OFPUTIL_A_POP_VLAN       = 1 << 20,
466     OFPUTIL_A_PUSH_MPLS      = 1 << 21,
467     OFPUTIL_A_POP_MPLS       = 1 << 22,
468     OFPUTIL_A_SET_QUEUE      = 1 << 23,
469     OFPUTIL_A_GROUP          = 1 << 24,
470     OFPUTIL_A_SET_NW_TTL     = 1 << 25,
471     OFPUTIL_A_DEC_NW_TTL     = 1 << 26,
472     OFPUTIL_A_SET_FIELD      = 1 << 27,
473 };
474
475 /* Abstract ofp_switch_features. */
476 struct ofputil_switch_features {
477     uint64_t datapath_id;       /* Datapath unique ID. */
478     uint32_t n_buffers;         /* Max packets buffered at once. */
479     uint8_t n_tables;           /* Number of tables supported by datapath. */
480     uint8_t auxiliary_id;       /* Identify auxiliary connections */
481     enum ofputil_capabilities capabilities;
482     enum ofputil_action_bitmap actions;
483 };
484
485 enum ofperr ofputil_decode_switch_features(const struct ofp_header *,
486                                            struct ofputil_switch_features *,
487                                            struct ofpbuf *);
488
489 struct ofpbuf *ofputil_encode_switch_features(
490     const struct ofputil_switch_features *, enum ofputil_protocol,
491     ovs_be32 xid);
492 void ofputil_put_switch_features_port(const struct ofputil_phy_port *,
493                                       struct ofpbuf *);
494 bool ofputil_switch_features_ports_trunc(struct ofpbuf *b);
495
496 /* phy_port helper functions. */
497 int ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *,
498                           struct ofputil_phy_port *);
499 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *);
500
501 /* Abstract ofp_port_status. */
502 struct ofputil_port_status {
503     enum ofp_port_reason reason;
504     struct ofputil_phy_port desc;
505 };
506
507 enum ofperr ofputil_decode_port_status(const struct ofp_header *,
508                                        struct ofputil_port_status *);
509 struct ofpbuf *ofputil_encode_port_status(const struct ofputil_port_status *,
510                                           enum ofputil_protocol);
511
512 /* Abstract ofp_port_mod. */
513 struct ofputil_port_mod {
514     ofp_port_t port_no;
515     uint8_t hw_addr[OFP_ETH_ALEN];
516     enum ofputil_port_config config;
517     enum ofputil_port_config mask;
518     enum netdev_features advertise;
519 };
520
521 enum ofperr ofputil_decode_port_mod(const struct ofp_header *,
522                                     struct ofputil_port_mod *);
523 struct ofpbuf *ofputil_encode_port_mod(const struct ofputil_port_mod *,
524                                        enum ofputil_protocol);
525
526 /* Meter band configuration for all supported band types. */
527 struct ofputil_meter_band {
528     uint16_t type;
529     uint8_t prec_level;         /* Non-zero if type == OFPMBT_DSCP_REMARK. */
530     uint32_t rate;
531     uint32_t burst_size;
532 };
533
534 struct ofputil_meter_band_stats {
535     uint64_t packet_count;
536     uint64_t byte_count;
537 };
538
539 struct ofputil_meter_config {
540     uint32_t meter_id;
541     uint16_t flags;
542     uint16_t n_bands;
543     struct ofputil_meter_band *bands;
544 };
545
546 /* Abstract ofp_meter_mod. */
547 struct ofputil_meter_mod {
548     uint16_t command;
549     struct ofputil_meter_config meter;
550 };
551
552 struct ofputil_meter_stats {
553     uint32_t meter_id;
554     uint32_t flow_count;
555     uint64_t packet_in_count;
556     uint64_t byte_in_count;
557     uint32_t duration_sec;
558     uint32_t duration_nsec;
559     uint16_t n_bands;
560     struct ofputil_meter_band_stats *bands;
561 };
562
563 struct ofputil_meter_features {
564     uint32_t max_meters;        /* Maximum number of meters. */
565     uint32_t band_types;        /* Can support max 32 band types. */
566     uint32_t capabilities;      /* Supported flags. */
567     uint8_t  max_bands;
568     uint8_t  max_color;
569 };
570
571 enum ofperr ofputil_decode_meter_mod(const struct ofp_header *,
572                                      struct ofputil_meter_mod *,
573                                      struct ofpbuf *bands);
574 struct ofpbuf *ofputil_encode_meter_mod(enum ofp_version,
575                                         const struct ofputil_meter_mod *);
576
577 void ofputil_decode_meter_features(const struct ofp_header *,
578                                    struct ofputil_meter_features *);
579 struct ofpbuf *ofputil_encode_meter_features_reply(const struct
580                                                    ofputil_meter_features *,
581                                                    const struct ofp_header *
582                                                    request);
583 void ofputil_decode_meter_request(const struct ofp_header *,
584                                   uint32_t *meter_id);
585
586 void ofputil_append_meter_config(struct list *replies,
587                                  const struct ofputil_meter_config *);
588
589 void ofputil_append_meter_stats(struct list *replies,
590                                 const struct ofputil_meter_stats *);
591
592 enum ofputil_meter_request_type {
593     OFPUTIL_METER_FEATURES,
594     OFPUTIL_METER_CONFIG,
595     OFPUTIL_METER_STATS
596 };
597
598 struct ofpbuf *ofputil_encode_meter_request(enum ofp_version,
599                                             enum ofputil_meter_request_type,
600                                             uint32_t meter_id);
601
602 int ofputil_decode_meter_stats(struct ofpbuf *,
603                                struct ofputil_meter_stats *,
604                                struct ofpbuf *bands);
605
606 int ofputil_decode_meter_config(struct ofpbuf *,
607                                 struct ofputil_meter_config *,
608                                 struct ofpbuf *bands);
609
610 /* Type for meter_id in ofproto provider interface, UINT32_MAX if invalid. */
611 typedef struct { uint32_t uint32; } ofproto_meter_id;
612
613 /* Abstract ofp_role_request and reply. */
614 struct ofputil_role_request {
615     enum ofp12_controller_role role;
616     bool have_generation_id;
617     uint64_t generation_id;
618 };
619
620 enum ofperr ofputil_decode_role_message(const struct ofp_header *,
621                                         struct ofputil_role_request *);
622 struct ofpbuf *ofputil_encode_role_reply(const struct ofp_header *,
623                                          const struct ofputil_role_request *);
624
625 /* Abstract table stats.
626  *
627  * For now we use ofp12_table_stats as a superset of the other protocol
628  * versions' table stats. */
629
630 struct ofpbuf *ofputil_encode_table_stats_reply(
631     const struct ofp12_table_stats[], int n,
632     const struct ofp_header *request);
633
634 /* Abstract nx_flow_monitor_request. */
635 struct ofputil_flow_monitor_request {
636     uint32_t id;
637     enum nx_flow_monitor_flags flags;
638     ofp_port_t out_port;
639     uint8_t table_id;
640     struct match match;
641 };
642
643 int ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *,
644                                         struct ofpbuf *msg);
645 void ofputil_append_flow_monitor_request(
646     const struct ofputil_flow_monitor_request *, struct ofpbuf *msg);
647
648 /* Abstract nx_flow_update. */
649 struct ofputil_flow_update {
650     enum nx_flow_update_event event;
651
652     /* Used only for NXFME_ADDED, NXFME_DELETED, NXFME_MODIFIED. */
653     enum ofp_flow_removed_reason reason;
654     uint16_t idle_timeout;
655     uint16_t hard_timeout;
656     uint8_t table_id;
657     ovs_be64 cookie;
658     struct match *match;
659     uint16_t priority;
660     struct ofpact *ofpacts;
661     size_t ofpacts_len;
662
663     /* Used only for NXFME_ABBREV. */
664     ovs_be32 xid;
665 };
666
667 int ofputil_decode_flow_update(struct ofputil_flow_update *,
668                                struct ofpbuf *msg, struct ofpbuf *ofpacts);
669 void ofputil_start_flow_update(struct list *replies);
670 void ofputil_append_flow_update(const struct ofputil_flow_update *,
671                                 struct list *replies);
672
673 /* Abstract nx_flow_monitor_cancel. */
674 uint32_t ofputil_decode_flow_monitor_cancel(const struct ofp_header *);
675 struct ofpbuf *ofputil_encode_flow_monitor_cancel(uint32_t id);
676
677 /* Encoding OpenFlow stats messages. */
678 void ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
679                                           const struct ofputil_phy_port *pp,
680                                           struct list *replies);
681
682 /* Encoding simple OpenFlow messages. */
683 struct ofpbuf *make_echo_request(enum ofp_version);
684 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
685
686 struct ofpbuf *ofputil_encode_barrier_request(enum ofp_version);
687
688 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
689 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
690
691 \f
692 /* Actions. */
693
694 /* The type of an action.
695  *
696  * For each implemented OFPAT10_* and NXAST_* action type, there is a
697  * corresponding constant prefixed with OFPUTIL_, e.g.:
698  *
699  * OFPUTIL_OFPAT10_OUTPUT
700  * OFPUTIL_OFPAT10_SET_VLAN_VID
701  * OFPUTIL_OFPAT10_SET_VLAN_PCP
702  * OFPUTIL_OFPAT10_STRIP_VLAN
703  * OFPUTIL_OFPAT10_SET_DL_SRC
704  * OFPUTIL_OFPAT10_SET_DL_DST
705  * OFPUTIL_OFPAT10_SET_NW_SRC
706  * OFPUTIL_OFPAT10_SET_NW_DST
707  * OFPUTIL_OFPAT10_SET_NW_TOS
708  * OFPUTIL_OFPAT10_SET_TP_SRC
709  * OFPUTIL_OFPAT10_SET_TP_DST
710  * OFPUTIL_OFPAT10_ENQUEUE
711  * OFPUTIL_NXAST_RESUBMIT
712  * OFPUTIL_NXAST_SET_TUNNEL
713  * OFPUTIL_NXAST_SET_METADATA
714  * OFPUTIL_NXAST_SET_QUEUE
715  * OFPUTIL_NXAST_POP_QUEUE
716  * OFPUTIL_NXAST_REG_MOVE
717  * OFPUTIL_NXAST_REG_LOAD
718  * OFPUTIL_NXAST_NOTE
719  * OFPUTIL_NXAST_SET_TUNNEL64
720  * OFPUTIL_NXAST_MULTIPATH
721  * OFPUTIL_NXAST_BUNDLE
722  * OFPUTIL_NXAST_BUNDLE_LOAD
723  * OFPUTIL_NXAST_RESUBMIT_TABLE
724  * OFPUTIL_NXAST_OUTPUT_REG
725  * OFPUTIL_NXAST_LEARN
726  * OFPUTIL_NXAST_DEC_TTL
727  * OFPUTIL_NXAST_FIN_TIMEOUT
728  *
729  * (The above list helps developers who want to "grep" for these definitions.)
730  */
731 enum OVS_PACKED_ENUM ofputil_action_code {
732     OFPUTIL_ACTION_INVALID,
733 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             OFPUTIL_##ENUM,
734 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
735 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   OFPUTIL_##ENUM,
736 #include "ofp-util.def"
737 };
738
739 /* The number of values of "enum ofputil_action_code". */
740 enum {
741 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             + 1
742 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
743 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   + 1
744     OFPUTIL_N_ACTIONS = 1
745 #include "ofp-util.def"
746 };
747
748 int ofputil_action_code_from_name(const char *);
749
750 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
751
752 /* For each OpenFlow action <ENUM> that has a corresponding action structure
753  * struct <STRUCT>, this defines two functions:
754  *
755  *   void ofputil_init_<ENUM>(struct <STRUCT> *action);
756  *
757  *     Initializes the parts of 'action' that identify it as having type <ENUM>
758  *     and length 'sizeof *action' and zeros the rest.  For actions that have
759  *     variable length, the length used and cleared is that of struct <STRUCT>.
760  *
761  *  struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
762  *
763  *     Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
764  *     initializes it with ofputil_init_<ENUM>(), and returns it.
765  */
766 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)              \
767     void ofputil_init_##ENUM(struct STRUCT *);          \
768     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
769 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
770     void ofputil_init_##ENUM(struct STRUCT *);          \
771     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
772 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
773     void ofputil_init_##ENUM(struct STRUCT *);          \
774     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
775 #include "ofp-util.def"
776
777 #define OFP_ACTION_ALIGN 8      /* Alignment of ofp_actions. */
778
779 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
780
781 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
782                                  union ofp_action **, size_t *);
783
784 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
785                            const union ofp_action *b, size_t n_b);
786 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
787
788 /* Handy utility for parsing flows and actions. */
789 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
790
791 struct ofputil_port_stats {
792     ofp_port_t port_no;
793     struct netdev_stats stats;
794     uint32_t duration_sec;      /* UINT32_MAX if unknown. */
795     uint32_t duration_nsec;
796 };
797
798 struct ofpbuf *ofputil_encode_dump_ports_request(enum ofp_version ofp_version,
799                                                  ofp_port_t port);
800 void ofputil_append_port_stat(struct list *replies,
801                               const struct ofputil_port_stats *ops);
802 size_t ofputil_count_port_stats(const struct ofp_header *);
803 int ofputil_decode_port_stats(struct ofputil_port_stats *, struct ofpbuf *msg);
804 enum ofperr ofputil_decode_port_stats_request(const struct ofp_header *request,
805                                               ofp_port_t *ofp10_port);
806
807 struct ofputil_queue_stats_request {
808     ofp_port_t port_no;           /* OFPP_ANY means "all ports". */
809     uint32_t queue_id;
810 };
811
812 enum ofperr
813 ofputil_decode_queue_stats_request(const struct ofp_header *request,
814                                    struct ofputil_queue_stats_request *oqsr);
815 struct ofpbuf *
816 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
817                                    const struct ofputil_queue_stats_request *oqsr);
818
819 struct ofputil_queue_stats {
820     ofp_port_t port_no;
821     uint32_t queue_id;
822     struct netdev_queue_stats stats;
823 };
824
825 size_t ofputil_count_queue_stats(const struct ofp_header *);
826 int ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg);
827 void ofputil_append_queue_stat(struct list *replies,
828                                const struct ofputil_queue_stats *oqs);
829
830 #endif /* ofp-util.h */