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