New function vconn_get_ip().
[sliver-openvswitch.git] / include / openflow.h
1 /* Copyright (c) 2008 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 /* OpenFlow: protocol between controller and datapath. */
35
36 #ifndef OPENFLOW_H
37 #define OPENFLOW_H 1
38
39 #ifdef __KERNEL__
40 #include <linux/types.h>
41 #else
42 #include <stdint.h>
43 #endif
44
45 #ifndef __cplusplus
46 /* Build-time assertion for use in a declaration context. */
47 #define OFP_ASSERT(EXPR)                                                \
48         extern int (*build_assert(void))[ sizeof(struct {               \
49                     unsigned int build_assert_failed : (EXPR) ? 1 : -1; })]
50 #else /* __cplusplus */
51 #include <boost/static_assert.hpp>
52 #define OFP_ASSERT BOOST_STATIC_ASSERT
53 #endif /* __cplusplus */
54
55 /* Maximum length of a OpenFlow packet. */
56 #define OFP_MAXLEN (sizeof(struct ofp_switch_features) \
57         + (sizeof(struct ofp_phy_port) * OFPP_MAX) + 200)
58
59
60 /* The most significant bit being set in the version field indicates an
61  * experimental OpenFlow version.  
62  */
63 #define OFP_VERSION   0x85
64
65 #define OFP_MAX_TABLE_NAME_LEN 32
66 #define OFP_MAX_PORT_NAME_LEN  16
67
68 #define OFP_TCP_PORT  975
69 #define OFP_SSL_PORT  976
70
71 #define OFP_ETH_ALEN 6          /* Bytes in an Ethernet address. */
72
73 /* Port numbering.  Physical ports are numbered starting from 0. */
74 enum ofp_port {
75     /* Maximum number of physical switch ports. */
76     OFPP_MAX = 0x100,
77
78     /* Fake output "ports". */
79     OFPP_TABLE      = 0xfff9,  /* Perform actions in flow table.  
80                                 * NB: This can only be the destination
81                                 * port for packet-out messages. 
82                                 */
83     OFPP_NORMAL     = 0xfffa,  /* Process with normal L2/L3 switching. */
84     OFPP_FLOOD      = 0xfffb,  /* All physical ports except input port and 
85                                   those disabled by STP. */
86     OFPP_ALL        = 0xfffc,  /* All physical ports except input port. */
87     OFPP_CONTROLLER = 0xfffd,  /* Send to controller. */ 
88     OFPP_LOCAL      = 0xfffe,  /* Local openflow "port". */
89     OFPP_NONE       = 0xffff   /* Not associated with a physical port. */
90 };
91
92 enum ofp_type {
93     OFPT_FEATURES_REQUEST,    /*  0 Controller/switch message */
94     OFPT_FEATURES_REPLY,      /*  1 Controller/switch message */
95     OFPT_GET_CONFIG_REQUEST,  /*  2 Controller/switch message */
96     OFPT_GET_CONFIG_REPLY,    /*  3 Controller/switch message */
97     OFPT_SET_CONFIG,          /*  4 Controller/switch message */
98     OFPT_PACKET_IN,           /*  5 Async message */
99     OFPT_PACKET_OUT,          /*  6 Controller/switch message */
100     OFPT_FLOW_MOD,            /*  7 Controller/switch message */
101     OFPT_FLOW_EXPIRED,        /*  8 Async message */
102     OFPT_TABLE,               /*  9 Controller/switch message */
103     OFPT_PORT_MOD,            /* 10 Controller/switch message */
104     OFPT_PORT_STATUS,         /* 11 Async message */
105     OFPT_ERROR_MSG,           /* 12 Async message */
106     OFPT_STATS_REQUEST,       /* 13 Controller/switch message */
107     OFPT_STATS_REPLY,         /* 14 Controller/switch message */
108     OFPT_ECHO_REQUEST,        /* 15 Symmetric message */
109     OFPT_ECHO_REPLY           /* 16 Symmetric message */
110 };
111
112 /* Header on all OpenFlow packets. */
113 struct ofp_header {
114     uint8_t version;    /* OFP_VERSION. */
115     uint8_t type;       /* One of the OFPT_ constants. */
116     uint16_t length;    /* Length including this ofp_header. */
117     uint32_t xid;       /* Transactin id associated with this packet.
118                            Replies use the same id as was in the request
119                            to facilitate pairing. */
120 };
121 OFP_ASSERT(sizeof(struct ofp_header) == 8);
122
123 #define OFP_DEFAULT_MISS_SEND_LEN   128
124
125 enum ofp_config_flags {
126     /* Tells datapath to notify the controller of expired flow entries. */
127     OFPC_SEND_FLOW_EXP = 1 << 0
128 };
129
130 /* Switch configuration. */
131 struct ofp_switch_config {
132     struct ofp_header header;
133     uint16_t flags;             /* OFPC_* flags. */
134     uint16_t miss_send_len;     /* Max bytes of new flow that datapath should
135                                    send to the controller. */
136 };
137 OFP_ASSERT(sizeof(struct ofp_switch_config) == 12);
138
139 /* Capabilities supported by the datapath. */
140 enum ofp_capabilities {
141     OFPC_FLOW_STATS   = 1 << 0, /* Flow statistics. */
142     OFPC_TABLE_STATS  = 1 << 1, /* Table statistics. */
143     OFPC_PORT_STATS   = 1 << 2, /* Port statistics. */
144     OFPC_STP          = 1 << 3, /* 802.11d spanning tree. */
145     OFPC_MULTI_PHY_TX = 1 << 4  /* Supports transmitting through multiple
146                                    physical interfaces */
147 };
148
149 /* Flags to indicate behavior of the physical port */
150 enum ofp_port_flags {
151     OFPPFL_NO_FLOOD  = 1 << 0, /* Do not include this port when flooding */
152 };
153
154 /* Features of physical ports available in a datapath. */
155 enum ofp_port_features {
156     OFPPF_10MB_HD    = 1 << 0, /* 10 Mb half-duplex rate support. */
157     OFPPF_10MB_FD    = 1 << 1, /* 10 Mb full-duplex rate support. */
158     OFPPF_100MB_HD   = 1 << 2, /* 100 Mb half-duplex rate support. */
159     OFPPF_100MB_FD   = 1 << 3, /* 100 Mb full-duplex rate support. */
160     OFPPF_1GB_HD     = 1 << 4, /* 1 Gb half-duplex rate support. */
161     OFPPF_1GB_FD     = 1 << 5, /* 1 Gb full-duplex rate support. */
162     OFPPF_10GB_FD    = 1 << 6, /* 10 Gb full-duplex rate support. */
163 };
164
165
166 /* Description of a physical port */
167 struct ofp_phy_port {
168     uint16_t port_no;
169     uint8_t hw_addr[OFP_ETH_ALEN];
170     uint8_t name[OFP_MAX_PORT_NAME_LEN]; /* Null-terminated */
171     uint32_t flags;         /* Bitmap of "ofp_port_flags". */
172     uint32_t speed;         /* Current speed in Mbps */
173     uint32_t features;      /* Bitmap of supported "ofp_port_features"s. */
174 };
175 OFP_ASSERT(sizeof(struct ofp_phy_port) == 36);
176
177 /* Switch features. */
178 struct ofp_switch_features {
179     struct ofp_header header;
180     uint64_t datapath_id;   /* Datapath unique ID.  Only the lower 48-bits
181                                are meaningful. */
182
183     /* Table info. */
184     uint32_t n_exact;       /* Max exact-match table entries. */
185     uint32_t n_compression; /* Max entries compressed on service port. */
186     uint32_t n_general;     /* Max entries of arbitrary form. */
187
188     /* Buffer limits.  A datapath that cannot buffer reports 0.*/
189     uint32_t buffer_mb;     /* Space for buffering packets, in MB. */
190     uint32_t n_buffers;     /* Max packets buffered at once. */
191
192     /* Features. */
193     uint32_t capabilities;  /* Bitmap of support "ofp_capabilities". */
194     uint32_t actions;       /* Bitmap of supported "ofp_action_type"s. */
195     uint8_t pad[4];         /* Align to 64-bits. */
196
197     /* Port info.*/
198     struct ofp_phy_port ports[0];   /* Port definitions.  The number of ports
199                                       is inferred from the length field in
200                                       the header. */
201 };
202 OFP_ASSERT(sizeof(struct ofp_switch_features) == 48);
203
204 /* What changed about the phsyical port */
205 enum ofp_port_reason {
206     OFPPR_ADD,              /* The port was added */
207     OFPPR_DELETE,           /* The port was removed */
208     OFPPR_MOD               /* Some attribute of the port has changed */
209 };
210
211 /* A physical port has changed in the datapath */
212 struct ofp_port_status {
213     struct ofp_header header;
214     uint8_t reason;          /* One of OFPPR_* */
215     uint8_t pad[3];          /* Align to 32-bits */
216     struct ofp_phy_port desc;
217 };
218 OFP_ASSERT(sizeof(struct ofp_port_status) == 48);
219
220 /* Modify behavior of the physical port */
221 struct ofp_port_mod {
222     struct ofp_header header;
223     struct ofp_phy_port desc;
224 };
225 OFP_ASSERT(sizeof(struct ofp_port_mod) == 44);
226
227 /* Why is this packet being sent to the controller? */
228 enum ofp_reason {
229     OFPR_NO_MATCH,          /* No matching flow. */
230     OFPR_ACTION             /* Action explicitly output to controller. */
231 };
232
233 /* Packet received on port (datapath -> controller). */
234 struct ofp_packet_in {
235     struct ofp_header header;
236     uint32_t buffer_id;     /* ID assigned by datapath. */
237     uint16_t total_len;     /* Full length of frame. */
238     uint16_t in_port;       /* Port on which frame was received. */
239     uint8_t reason;         /* Reason packet is being sent (one of OFPR_*) */
240     uint8_t pad;
241     uint8_t data[0];        /* Ethernet frame, halfway through 32-bit word,
242                                so the IP header is 32-bit aligned.  The 
243                                amount of data is inferred from the length
244                                field in the header.  Because of padding,
245                                offsetof(struct ofp_packet_in, data) ==
246                                sizeof(struct ofp_packet_in) - 2. */
247 };
248 OFP_ASSERT(sizeof(struct ofp_packet_in) == 20);
249
250 enum ofp_action_type {
251     OFPAT_OUTPUT,           /* Output to switch port. */
252     OFPAT_SET_DL_VLAN,      /* VLAN. */
253     OFPAT_SET_DL_SRC,       /* Ethernet source address. */
254     OFPAT_SET_DL_DST,       /* Ethernet destination address. */
255     OFPAT_SET_NW_SRC,       /* IP source address. */
256     OFPAT_SET_NW_DST,       /* IP destination address. */
257     OFPAT_SET_TP_SRC,       /* TCP/UDP source port. */
258     OFPAT_SET_TP_DST        /* TCP/UDP destination port. */
259 };
260
261 /* An output action sends packets out 'port'.  When the 'port' is the
262  * OFPP_CONTROLLER, 'max_len' indicates the max number of bytes to
263  * send.  A 'max_len' of zero means the entire packet should be sent. */
264 struct ofp_action_output {
265     uint16_t max_len;
266     uint16_t port;
267 };
268 OFP_ASSERT(sizeof(struct ofp_action_output) == 4);
269
270 /* The VLAN id is 12-bits, so we'll use the entire 16 bits to indicate
271  * special conditions.  All ones is used to indicate that no VLAN id was
272  * set, or if used as an action, that the VLAN header should be
273  * stripped.
274  */
275 #define OFP_VLAN_NONE      0xffff
276
277 struct ofp_action {
278     uint16_t type;                       /* One of OFPAT_* */
279     union {
280         struct ofp_action_output output; /* OFPAT_OUTPUT: output struct. */
281         uint16_t vlan_id;                /* OFPAT_SET_DL_VLAN: VLAN id. */
282         uint8_t  dl_addr[OFP_ETH_ALEN];  /* OFPAT_SET_DL_SRC/DST */
283         uint32_t nw_addr __attribute__((packed)); /* OFPAT_SET_NW_SRC/DST */
284         uint16_t tp;                     /* OFPAT_SET_TP_SRC/DST */
285     } arg;
286 };
287 OFP_ASSERT(sizeof(struct ofp_action) == 8);
288
289 /* Send packet (controller -> datapath). */
290 struct ofp_packet_out {
291     struct ofp_header header;
292     uint32_t buffer_id;     /* ID assigned by datapath (-1 if none). */
293     uint16_t in_port;       /* Packet's input port (OFPP_NONE if none). */
294     uint16_t out_port;      /* Output port (if buffer_id == -1). */
295     union {
296         struct ofp_action actions[0]; /* buffer_id != -1 */
297         uint8_t data[0];              /* buffer_id == -1 */
298     } u;
299 };
300 OFP_ASSERT(sizeof(struct ofp_packet_out) == 16);
301
302 enum ofp_flow_mod_command {
303     OFPFC_ADD,              /* New flow. */
304     OFPFC_DELETE,           /* Delete all matching flows. */
305     OFPFC_DELETE_STRICT     /* Strictly match wildcards and priority. */
306 };
307
308 /* Flow wildcards. */
309 enum ofp_flow_wildcards {
310     OFPFW_IN_PORT  = 1 << 0,  /* Switch input port. */
311     OFPFW_DL_VLAN  = 1 << 1,  /* VLAN. */
312     OFPFW_DL_SRC   = 1 << 2,  /* Ethernet source address. */
313     OFPFW_DL_DST   = 1 << 3,  /* Ethernet destination address. */
314     OFPFW_DL_TYPE  = 1 << 4,  /* Ethernet frame type. */
315     OFPFW_NW_SRC   = 1 << 5,  /* IP source address. */
316     OFPFW_NW_DST   = 1 << 6,  /* IP destination address. */
317     OFPFW_NW_PROTO = 1 << 7,  /* IP protocol. */
318     OFPFW_TP_SRC   = 1 << 8,  /* TCP/UDP source port. */
319     OFPFW_TP_DST   = 1 << 9,  /* TCP/UDP destination port. */
320     OFPFW_ALL      = (1 << 10) - 1
321 };
322
323 /* Values below this cutoff are 802.3 packets and the two bytes
324  * following MAC addresses are used as a frame length.  Otherwise, the
325  * two bytes are used as the Ethernet type.
326  */
327 #define OFP_DL_TYPE_ETH2_CUTOFF   0x0600
328
329 /* Value of dl_type to indicate that the frame does not include an
330  * Ethernet type.
331  */
332 #define OFP_DL_TYPE_NOT_ETH_TYPE  0x05ff
333
334 /* Fields to match against flows */
335 struct ofp_match {
336     uint16_t wildcards;        /* Wildcard fields. */
337     uint16_t in_port;          /* Input switch port. */
338     uint8_t dl_src[OFP_ETH_ALEN]; /* Ethernet source address. */
339     uint8_t dl_dst[OFP_ETH_ALEN]; /* Ethernet destination address. */
340     uint16_t dl_vlan;          /* Input VLAN. */
341     uint16_t dl_type;          /* Ethernet frame type. */
342     uint32_t nw_src;           /* IP source address. */
343     uint32_t nw_dst;           /* IP destination address. */
344     uint8_t nw_proto;          /* IP protocol. */
345     uint8_t pad[3];            /* Align to 32-bits. */
346     uint16_t tp_src;           /* TCP/UDP source port. */
347     uint16_t tp_dst;           /* TCP/UDP destination port. */
348 };
349 OFP_ASSERT(sizeof(struct ofp_match) == 36);
350
351 /* Value used in "max_idle" to indicate that the entry is permanent */
352 #define OFP_FLOW_PERMANENT 0
353
354 /* By default, choose a priority in the middle */
355 #define OFP_DEFAULT_PRIORITY 0x8000
356
357 /* Flow setup and teardown (controller -> datapath). */
358 struct ofp_flow_mod {
359     struct ofp_header header;
360     struct ofp_match match;      /* Fields to match */
361
362     /* Flow actions. */
363     uint16_t command;             /* One of OFPFC_*. */
364     uint16_t max_idle;            /* Idle time before discarding (seconds). */
365     uint32_t buffer_id;           /* Buffered packet to apply to (or -1). */
366     uint16_t priority;            /* Priority level of flow entry. */
367     uint8_t pad[2];               /* Align to 32-bits. */
368     uint32_t reserved;            /* Reserved for future use. */
369     struct ofp_action actions[0]; /* The number of actions is inferred from
370                                     the length field in the header. */
371 };
372 OFP_ASSERT(sizeof(struct ofp_flow_mod) == 60);
373
374 /* Flow expiration (datapath -> controller). */
375 struct ofp_flow_expired {
376     struct ofp_header header;
377     struct ofp_match match;   /* Description of fields */
378
379     uint16_t priority;        /* Priority level of flow entry. */
380     uint8_t pad[2];           /* Align to 32-bits. */
381
382     uint32_t duration;        /* Time flow was alive in seconds. */
383     uint8_t pad2[4];          /* Align to 64-bits. */
384     uint64_t packet_count;    
385     uint64_t byte_count;
386 };
387 OFP_ASSERT(sizeof(struct ofp_flow_expired) == 72);
388
389 /* Error message (datapath -> controller). */
390 struct ofp_error_msg {
391     struct ofp_header header;
392
393     uint16_t type;
394     uint16_t code;
395     uint8_t data[0];          /* Variable-length data.  Interpreted based 
396                                  on the type and code. */
397 };
398 OFP_ASSERT(sizeof(struct ofp_error_msg) == 12);
399
400 enum ofp_stats_types {
401     /* Individual flow statistics.
402      * The request body is struct ofp_flow_stats_request.
403      * The reply body is an array of struct ofp_flow_stats. */
404     OFPST_FLOW,
405
406     /* Aggregate flow statistics.
407      * The request body is struct ofp_aggregate_stats_request.
408      * The reply body is struct ofp_aggregate_stats_reply. */
409     OFPST_AGGREGATE,
410
411     /* Flow table statistics.
412      * The request body is empty.
413      * The reply body is an array of struct ofp_table_stats. */
414     OFPST_TABLE,
415
416     /* Physical port statistics.
417      * The request body is empty.
418      * The reply body is an array of struct ofp_port_stats. */
419     OFPST_PORT
420 };
421
422 struct ofp_stats_request {
423     struct ofp_header header;
424     uint16_t type;              /* One of the OFPST_* constants. */
425     uint16_t flags;             /* OFPSF_REQ_* flags (none yet defined). */
426     uint8_t body[0];            /* Body of the request. */
427 };
428 OFP_ASSERT(sizeof(struct ofp_stats_request) == 12);
429
430 enum ofp_stats_reply_flags {
431     OFPSF_REPLY_MORE  = 1 << 0, /* More replies to follow */
432 };
433
434 struct ofp_stats_reply {
435     struct ofp_header header;
436     uint16_t type;              /* One of the OFPST_* constants. */
437     uint16_t flags;             /* OFPSF_REPLY_* flags. */
438     uint8_t body[0];            /* Body of the reply. */
439 };
440 OFP_ASSERT(sizeof(struct ofp_stats_reply) == 12);
441
442 /* Body for ofp_stats_request of type OFPST_FLOW. */
443 struct ofp_flow_stats_request {
444     struct ofp_match match;   /* Fields to match */
445     uint8_t table_id;         /* ID of table to read (from ofp_table_stats)
446                                  or 0xff for all tables. */
447     uint8_t pad[3];           /* Align to 32 bits. */
448 };
449 OFP_ASSERT(sizeof(struct ofp_flow_stats_request) == 40);
450
451 /* Body of reply to OFPST_FLOW request. */
452 struct ofp_flow_stats {
453     uint16_t length;          /* Length of this entry. */
454     uint8_t table_id;         /* ID of table flow came from. */
455     uint8_t pad;
456     struct ofp_match match;   /* Description of fields. */
457     uint32_t duration;        /* Time flow has been alive in seconds. */
458     uint16_t priority;        /* Priority of the entry. Only meaningful
459                                  when this is not an exact-match entry. */
460     uint16_t max_idle;        /* Number of seconds idle before expiration. */
461     uint64_t packet_count;    /* Number of packets in flow. */
462     uint64_t byte_count;      /* Number of bytes in flow. */
463     struct ofp_action actions[0]; /* Actions. */
464 };
465 OFP_ASSERT(sizeof(struct ofp_flow_stats) == 64);
466
467 /* Body for ofp_stats_request of type OFPST_AGGREGATE. */
468 struct ofp_aggregate_stats_request {
469     struct ofp_match match;   /* Fields to match */
470     uint8_t table_id;         /* ID of table to read (from ofp_table_stats)
471                                  or 0xff for all tables. */
472     uint8_t pad[3];           /* Align to 32 bits. */
473 };
474 OFP_ASSERT(sizeof(struct ofp_aggregate_stats_request) == 40);
475
476 /* Body of reply to OFPST_AGGREGATE request. */
477 struct ofp_aggregate_stats_reply {
478     uint64_t packet_count;    /* Number of packets in flows. */
479     uint64_t byte_count;      /* Number of bytes in flows. */
480     uint32_t flow_count;      /* Number of flows. */
481     uint8_t pad[4];           /* Align to 64 bits. */
482 };
483 OFP_ASSERT(sizeof(struct ofp_aggregate_stats_reply) == 24);
484
485 /* Body of reply to OFPST_TABLE request. */
486 struct ofp_table_stats {
487     uint8_t table_id;
488     uint8_t pad[3];          /* Align to 32-bits */
489     char name[OFP_MAX_TABLE_NAME_LEN];
490     uint32_t max_entries;    /* Max number of entries supported */
491     uint32_t active_count;   /* Number of active entries */
492     uint8_t pad2[4];         /* Align to 64 bits. */
493     uint64_t matched_count;  /* Number of packets that hit table */
494 };
495 OFP_ASSERT(sizeof(struct ofp_table_stats) == 56);
496
497 /* Statistics about a particular port */
498 struct ofp_port_stats {
499     uint16_t port_no;
500     uint8_t pad[6];          /* Align to 64-bits */
501     uint64_t rx_count;       /* Number of received packets */
502     uint64_t tx_count;       /* Number of transmitted packets */
503     uint64_t drop_count;     /* Number of packets dropped by interface */
504 };
505 OFP_ASSERT(sizeof(struct ofp_port_stats) == 32);
506
507 #endif /* openflow.h */