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