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