ofp-util: Support for OpenFlow 1.3 meters.
[sliver-openvswitch.git] / include / openflow / openflow-common.h
1 /* Copyright (c) 2008, 2011, 2012 The Board of Trustees of The Leland Stanford
2  * Junior University
3  *
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 /*
35  * Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc.
36  *
37  * Licensed under the Apache License, Version 2.0 (the "License");
38  * you may not use this file except in compliance with the License.
39  * You may obtain a copy of the License at:
40  *
41  *     http://www.apache.org/licenses/LICENSE-2.0
42  *
43  * Unless required by applicable law or agreed to in writing, software
44  * distributed under the License is distributed on an "AS IS" BASIS,
45  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46  * See the License for the specific language governing permissions and
47  * limitations under the License.
48  */
49
50 #ifndef OPENFLOW_COMMON_H
51 #define OPENFLOW_COMMON_H 1
52
53 #include "openvswitch/types.h"
54
55 #ifdef SWIG
56 #define OFP_ASSERT(EXPR)        /* SWIG can't handle OFP_ASSERT. */
57 #elif !defined(__cplusplus)
58 /* Build-time assertion for use in a declaration context. */
59 #define OFP_ASSERT(EXPR)                                                \
60         extern int (*build_assert(void))[ sizeof(struct {               \
61                     unsigned int build_assert_failed : (EXPR) ? 1 : -1; })]
62 #else /* __cplusplus */
63 #include <boost/static_assert.hpp>
64 #define OFP_ASSERT BOOST_STATIC_ASSERT
65 #endif /* __cplusplus */
66
67 /* Version number:
68  * Non-experimental versions released: 0x01 0x02
69  * Experimental versions released: 0x81 -- 0x99
70  */
71 /* The most significant bit being set in the version field indicates an
72  * experimental OpenFlow version.
73  */
74 enum ofp_version {
75     OFP10_VERSION = 0x01,
76     OFP11_VERSION = 0x02,
77     OFP12_VERSION = 0x03,
78     OFP13_VERSION = 0x04
79 };
80
81 #define OFP_MAX_TABLE_NAME_LEN 32
82 #define OFP_MAX_PORT_NAME_LEN  16
83
84 #define OFP_TCP_PORT  6633
85 #define OFP_SSL_PORT  6633
86
87 #define OFP_ETH_ALEN 6          /* Bytes in an Ethernet address. */
88
89 #define OFP_DEFAULT_MISS_SEND_LEN   128
90
91 /* Values below this cutoff are 802.3 packets and the two bytes
92  * following MAC addresses are used as a frame length.  Otherwise, the
93  * two bytes are used as the Ethernet type.
94  */
95 #define OFP_DL_TYPE_ETH2_CUTOFF   0x0600
96
97 /* Value of dl_type to indicate that the frame does not include an
98  * Ethernet type.
99  */
100 #define OFP_DL_TYPE_NOT_ETH_TYPE  0x05ff
101
102 /* Value used in "idle_timeout" and "hard_timeout" to indicate that the entry
103  * is permanent. */
104 #define OFP_FLOW_PERMANENT 0
105
106 /* By default, choose a priority in the middle. */
107 #define OFP_DEFAULT_PRIORITY 0x8000
108
109
110 /* Header on all OpenFlow packets. */
111 struct ofp_header {
112     uint8_t version;    /* An OpenFlow version number, e.g. OFP10_VERSION. */
113     uint8_t type;       /* One of the OFPT_ constants. */
114     ovs_be16 length;    /* Length including this ofp_header. */
115     ovs_be32 xid;       /* Transaction id associated with this packet.
116                            Replies use the same id as was in the request
117                            to facilitate pairing. */
118 };
119 OFP_ASSERT(sizeof(struct ofp_header) == 8);
120
121 /* OFPT_ERROR: Error message (datapath -> controller). */
122 struct ofp_error_msg {
123     ovs_be16 type;
124     ovs_be16 code;
125     uint8_t data[0];          /* Variable-length data.  Interpreted based
126                                  on the type and code. */
127 };
128 OFP_ASSERT(sizeof(struct ofp_error_msg) == 4);
129
130 enum ofp_config_flags {
131     /* Handling of IP fragments. */
132     OFPC_FRAG_NORMAL   = 0,  /* No special handling for fragments. */
133     OFPC_FRAG_DROP     = 1,  /* Drop fragments. */
134     OFPC_FRAG_REASM    = 2,  /* Reassemble (only if OFPC_IP_REASM set). */
135     OFPC_FRAG_NX_MATCH = 3,  /* Make first fragments available for matching. */
136     OFPC_FRAG_MASK     = 3,
137
138     /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OpenFlow 1.3 */
139
140     /* TTL processing - applicable for IP and MPLS packets. */
141     OFPC_INVALID_TTL_TO_CONTROLLER = 1 << 2, /* Send packets with invalid TTL
142                                                 to the controller. */
143 };
144
145 /* Switch configuration. */
146 struct ofp_switch_config {
147     ovs_be16 flags;             /* OFPC_* flags. */
148     ovs_be16 miss_send_len;     /* Max bytes of new flow that datapath should
149                                    send to the controller. */
150 };
151 OFP_ASSERT(sizeof(struct ofp_switch_config) == 4);
152
153
154 /* Common flags to indicate behavior of the physical port.  These flags are
155  * used in ofp_port to describe the current configuration.  They are used in
156  * the ofp_port_mod message to configure the port's behavior.
157  */
158 enum ofp_port_config {
159     OFPPC_PORT_DOWN    = 1 << 0,  /* Port is administratively down. */
160
161     OFPPC_NO_RECV      = 1 << 2,  /* Drop all packets received by port. */
162     OFPPC_NO_FWD       = 1 << 5,  /* Drop packets forwarded to port. */
163     OFPPC_NO_PACKET_IN = 1 << 6   /* Do not send packet-in msgs for port. */
164 };
165
166 /* Common current state of the physical port.  These are not configurable from
167  * the controller.
168  */
169 enum ofp_port_state {
170     OFPPS_LINK_DOWN    = 1 << 0,  /* No physical link present. */
171 };
172
173 /* Common features of physical ports available in a datapath. */
174 enum ofp_port_features {
175     OFPPF_10MB_HD    = 1 << 0,  /* 10 Mb half-duplex rate support. */
176     OFPPF_10MB_FD    = 1 << 1,  /* 10 Mb full-duplex rate support. */
177     OFPPF_100MB_HD   = 1 << 2,  /* 100 Mb half-duplex rate support. */
178     OFPPF_100MB_FD   = 1 << 3,  /* 100 Mb full-duplex rate support. */
179     OFPPF_1GB_HD     = 1 << 4,  /* 1 Gb half-duplex rate support. */
180     OFPPF_1GB_FD     = 1 << 5,  /* 1 Gb full-duplex rate support. */
181     OFPPF_10GB_FD    = 1 << 6,  /* 10 Gb full-duplex rate support. */
182 };
183
184 struct ofp_packet_queue {
185     ovs_be32 queue_id;          /* id for the specific queue. */
186     ovs_be16 len;               /* Length in bytes of this queue desc. */
187     uint8_t pad[2];             /* 64-bit alignment. */
188     /* struct ofp_queue_prop_header properties[0]; List of properties.  */
189 };
190 OFP_ASSERT(sizeof(struct ofp_packet_queue) == 8);
191
192 enum ofp_queue_properties {
193     OFPQT_NONE = 0,       /* No property defined for queue (default). */
194     OFPQT_MIN_RATE,       /* Minimum datarate guaranteed. */
195                           /* Other types should be added here
196                            * (i.e. max rate, precedence, etc). */
197 };
198
199 /* Common description for a queue. */
200 struct ofp_queue_prop_header {
201     ovs_be16 property; /* One of OFPQT_. */
202     ovs_be16 len;      /* Length of property, including this header. */
203     uint8_t pad[4];    /* 64-bit alignemnt. */
204 };
205 OFP_ASSERT(sizeof(struct ofp_queue_prop_header) == 8);
206
207 /* Min-Rate queue property description. */
208 struct ofp_queue_prop_min_rate {
209     struct ofp_queue_prop_header prop_header; /* prop: OFPQT_MIN, len: 16. */
210     ovs_be16 rate;        /* In 1/10 of a percent; >1000 -> disabled. */
211     uint8_t pad[6];       /* 64-bit alignment */
212 };
213 OFP_ASSERT(sizeof(struct ofp_queue_prop_min_rate) == 16);
214
215 /* Switch features. */
216 struct ofp_switch_features {
217     ovs_be64 datapath_id;   /* Datapath unique ID.  The lower 48-bits are for
218                                a MAC address, while the upper 16-bits are
219                                implementer-defined. */
220
221     ovs_be32 n_buffers;     /* Max packets buffered at once. */
222
223     uint8_t n_tables;       /* Number of tables supported by datapath. */
224     uint8_t auxiliary_id;   /* OF 1.3: Identify auxiliary connections */
225     uint8_t pad[2];         /* Align to 64-bits. */
226
227     /* Features. */
228     ovs_be32 capabilities;  /* OFPC_*, OFPC10_*, OFPC11_*, OFPC12_*. */
229     ovs_be32 actions;       /* Bitmap of supported "ofp_action_type"s.
230                              * DEPRECATED in OpenFlow 1.1 */
231
232     /* Followed by an array of struct ofp10_phy_port or struct ofp11_port
233      * structures.  The number is inferred from header.length.
234      * REMOVED in OpenFlow 1.3 */
235 };
236 OFP_ASSERT(sizeof(struct ofp_switch_features) == 24);
237
238 /* Common capabilities supported by the datapath (struct ofp_switch_features,
239  * member capabilities). */
240 enum ofp_capabilities {
241     OFPC_FLOW_STATS     = 1 << 0,  /* Flow statistics. */
242     OFPC_TABLE_STATS    = 1 << 1,  /* Table statistics. */
243     OFPC_PORT_STATS     = 1 << 2,  /* Port statistics. */
244     OFPC_IP_REASM       = 1 << 5,  /* Can reassemble IP fragments. */
245     OFPC_QUEUE_STATS    = 1 << 6,  /* Queue statistics. */
246     OFPC_ARP_MATCH_IP   = 1 << 7   /* Match IP addresses in ARP
247                                       pkts. */
248 };
249
250 /* Why is this packet being sent to the controller? */
251 enum ofp_packet_in_reason {
252     OFPR_NO_MATCH,          /* No matching flow. */
253     OFPR_ACTION,            /* Action explicitly output to controller. */
254     OFPR_INVALID_TTL        /* Packet has invalid TTL. */,
255     OFPR_N_REASONS
256 };
257
258 enum ofp_flow_mod_command {
259     OFPFC_ADD,              /* New flow. */
260     OFPFC_MODIFY,           /* Modify all matching flows. */
261     OFPFC_MODIFY_STRICT,    /* Modify entry strictly matching wildcards */
262     OFPFC_DELETE,           /* Delete all matching flows. */
263     OFPFC_DELETE_STRICT     /* Strictly match wildcards and priority. */
264 };
265
266 enum ofp_flow_mod_flags {
267     OFPFF_SEND_FLOW_REM = 1 << 0,  /* Send flow removed message when flow
268                                     * expires or is deleted. */
269     OFPFF_CHECK_OVERLAP = 1 << 1,  /* Check for overlapping entries first. */
270 };
271
272 /* Action header for OFPAT10_VENDOR and OFPAT11_EXPERIMEMNTER.
273  * The rest of the body is vendor-defined. */
274 struct ofp_action_vendor_header {
275     ovs_be16 type;                  /* OFPAT10_VENDOR. */
276     ovs_be16 len;                   /* Length is a multiple of 8. */
277     ovs_be32 vendor;                /* Vendor ID, which takes the same form
278                                        as in "struct ofp_vendor_header". */
279 };
280 OFP_ASSERT(sizeof(struct ofp_action_vendor_header) == 8);
281
282 /* Action header that is common to all actions.  The length includes the
283  * header and any padding used to make the action 64-bit aligned.
284  * NB: The length of an action *must* always be a multiple of eight. */
285 struct ofp_action_header {
286     ovs_be16 type;                  /* One of OFPAT10_*. */
287     ovs_be16 len;                   /* Length of action, including this
288                                        header.  This is the length of action,
289                                        including any padding to make it
290                                        64-bit aligned. */
291     uint8_t pad[4];
292 };
293 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
294
295 /* Action structure for OFPAT10_SET_VLAN_VID and OFPAT11_SET_VLAN_VID. */
296 struct ofp_action_vlan_vid {
297     ovs_be16 type;                  /* Type. */
298     ovs_be16 len;                   /* Length is 8. */
299     ovs_be16 vlan_vid;              /* VLAN id. */
300     uint8_t pad[2];
301 };
302 OFP_ASSERT(sizeof(struct ofp_action_vlan_vid) == 8);
303
304 /* Action structure for OFPAT10_SET_VLAN_PCP and OFPAT11_SET_VLAN_PCP. */
305 struct ofp_action_vlan_pcp {
306     ovs_be16 type;                  /* Type. */
307     ovs_be16 len;                   /* Length is 8. */
308     uint8_t vlan_pcp;               /* VLAN priority. */
309     uint8_t pad[3];
310 };
311 OFP_ASSERT(sizeof(struct ofp_action_vlan_pcp) == 8);
312
313 /* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
314 struct ofp_action_dl_addr {
315     ovs_be16 type;                  /* Type. */
316     ovs_be16 len;                   /* Length is 16. */
317     uint8_t dl_addr[OFP_ETH_ALEN];  /* Ethernet address. */
318     uint8_t pad[6];
319 };
320 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
321
322 /* Action structure for OFPAT10_SET_NW_SRC/DST and OFPAT11_SET_NW_SRC/DST. */
323 struct ofp_action_nw_addr {
324     ovs_be16 type;                  /* Type. */
325     ovs_be16 len;                   /* Length is 8. */
326     ovs_be32 nw_addr;               /* IP address. */
327 };
328 OFP_ASSERT(sizeof(struct ofp_action_nw_addr) == 8);
329
330 /* Action structure for OFPAT10_SET_NW_TOS and OFPAT11_SET_NW_TOS. */
331 struct ofp_action_nw_tos {
332     ovs_be16 type;                  /* Type.. */
333     ovs_be16 len;                   /* Length is 8. */
334     uint8_t nw_tos;                 /* DSCP in high 6 bits, rest ignored. */
335     uint8_t pad[3];
336 };
337 OFP_ASSERT(sizeof(struct ofp_action_nw_tos) == 8);
338
339 /* Action structure for OFPAT10_SET_TP_SRC/DST and OFPAT11_SET_TP_SRC/DST. */
340 struct ofp_action_tp_port {
341     ovs_be16 type;                  /* Type. */
342     ovs_be16 len;                   /* Length is 8. */
343     ovs_be16 tp_port;               /* TCP/UDP port. */
344     uint8_t pad[2];
345 };
346 OFP_ASSERT(sizeof(struct ofp_action_tp_port) == 8);
347
348 /* Why was this flow removed? */
349 enum ofp_flow_removed_reason {
350     OFPRR_IDLE_TIMEOUT,         /* Flow idle time exceeded idle_timeout. */
351     OFPRR_HARD_TIMEOUT,         /* Time exceeded hard_timeout. */
352     OFPRR_DELETE,               /* Evicted by a DELETE flow mod. */
353     OFPRR_GROUP_DELETE,         /* Group was removed. */
354     OFPRR_EVICTION,             /* Switch eviction to free resources. */
355     OFPRR_METER_DELETE,         /* Meter was removed. */
356 };
357
358 /* What changed about the physical port */
359 enum ofp_port_reason {
360     OFPPR_ADD,              /* The port was added. */
361     OFPPR_DELETE,           /* The port was removed. */
362     OFPPR_MODIFY            /* Some attribute of the port has changed. */
363 };
364
365 /* A physical port has changed in the datapath */
366 struct ofp_port_status {
367     uint8_t reason;          /* One of OFPPR_*. */
368     uint8_t pad[7];          /* Align to 64-bits. */
369     /* Followed by struct ofp10_phy_port or struct ofp11_port.  */
370 };
371 OFP_ASSERT(sizeof(struct ofp_port_status) == 8);
372
373 enum ofp_stats_reply_flags {
374     OFPSF_REPLY_MORE  = 1 << 0  /* More replies to follow. */
375 };
376
377 #define DESC_STR_LEN   256
378 #define SERIAL_NUM_LEN 32
379 /* Body of reply to OFPST_DESC request.  Each entry is a NULL-terminated ASCII
380  * string. */
381 struct ofp_desc_stats {
382     char mfr_desc[DESC_STR_LEN];       /* Manufacturer description. */
383     char hw_desc[DESC_STR_LEN];        /* Hardware description. */
384     char sw_desc[DESC_STR_LEN];        /* Software description. */
385     char serial_num[SERIAL_NUM_LEN];   /* Serial number. */
386     char dp_desc[DESC_STR_LEN];        /* Human readable description of
387                                           the datapath. */
388 };
389 OFP_ASSERT(sizeof(struct ofp_desc_stats) == 1056);
390
391 /* Reply to OFPST_AGGREGATE request. */
392 struct ofp_aggregate_stats_reply {
393     ovs_32aligned_be64 packet_count; /* Number of packets in flows. */
394     ovs_32aligned_be64 byte_count;   /* Number of bytes in flows. */
395     ovs_be32 flow_count;      /* Number of flows. */
396     uint8_t pad[4];           /* Align to 64 bits. */
397 };
398 OFP_ASSERT(sizeof(struct ofp_aggregate_stats_reply) == 24);
399
400 /* The match type indicates the match structure (set of fields that compose the
401  * match) in use. The match type is placed in the type field at the beginning
402  * of all match structures. The "OpenFlow Extensible Match" type corresponds
403  * to OXM TLV format described below and must be supported by all OpenFlow
404  * switches. Extensions that define other match types may be published on the
405  * ONF wiki. Support for extensions is optional.
406  */
407 enum ofp_match_type {
408     OFPMT_STANDARD = 0,         /* The match fields defined in the ofp11_match
409                                    structure apply */
410     OFPMT_OXM = 1,              /* OpenFlow Extensible Match */
411 };
412
413 /* Group numbering. Groups can use any number up to OFPG_MAX. */
414 enum ofp_group {
415     /* Last usable group number. */
416     OFPG_MAX        = 0xffffff00,
417
418     /* Fake groups. */
419     OFPG_ALL        = 0xfffffffc,  /* All groups, for group delete commands. */
420     OFPG_ANY        = 0xffffffff   /* Wildcard, for flow stats requests. */
421 };
422
423 enum ofp_hello_elem_type {
424     OFPHET_VERSIONBITMAP          = 1, /* Bitmap of version supported. */
425 };
426
427 /* Common header for all Hello Elements */
428 struct ofp_hello_elem_header {
429     ovs_be16    type;        /* One of OFPHET_*. */
430     ovs_be16    length;      /* Length in bytes of this element. */
431 };
432 OFP_ASSERT(sizeof(struct ofp_hello_elem_header) == 4);
433
434 /* Vendor extension. */
435 struct ofp_vendor_header {
436     struct ofp_header header;   /* Type OFPT_VENDOR or OFPT_EXPERIMENTER. */
437     ovs_be32 vendor;            /* Vendor ID:
438                                  * - MSB 0: low-order bytes are IEEE OUI.
439                                  * - MSB != 0: defined by OpenFlow
440                                  *   consortium. */
441     /* Vendor-defined arbitrary additional data. */
442 };
443 OFP_ASSERT(sizeof(struct ofp_vendor_header) == 12);
444
445 #endif /* openflow/openflow-common.h */