openflow: Change ofp_phy_port's 'name' member from uint8_t[] to char[].
[sliver-openvswitch.git] / include / openflow / openflow.h
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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 /* OpenFlow: protocol between controller and datapath. */
18
19 #ifndef OPENFLOW_OPENFLOW_H
20 #define OPENFLOW_OPENFLOW_H 1
21
22 #ifdef __KERNEL__
23 #include <linux/types.h>
24 #else
25 #include <stdint.h>
26 #endif
27
28 #ifdef SWIG
29 #define OFP_ASSERT(EXPR)        /* SWIG can't handle OFP_ASSERT. */
30 #elif !defined(__cplusplus)
31 /* Build-time assertion for use in a declaration context. */
32 #define OFP_ASSERT(EXPR)                                                \
33         extern int (*build_assert(void))[ sizeof(struct {               \
34                     unsigned int build_assert_failed : (EXPR) ? 1 : -1; })]
35 #else /* __cplusplus */
36 #include <boost/static_assert.hpp>
37 #define OFP_ASSERT BOOST_STATIC_ASSERT
38 #endif /* __cplusplus */
39
40 /* Version number:
41  * Non-experimental versions released: 0x01
42  * Experimental versions released: 0x81 -- 0x99
43  */
44 /* The most significant bit being set in the version field indicates an
45  * experimental OpenFlow version.
46  */
47 #define OFP_VERSION   0x01
48
49 #define OFP_MAX_TABLE_NAME_LEN 32
50 #define OFP_MAX_PORT_NAME_LEN  16
51
52 #define OFP_TCP_PORT  6633
53 #define OFP_SSL_PORT  6633
54
55 #define OFP_ETH_ALEN 6          /* Bytes in an Ethernet address. */
56
57 /* Port numbering.  Physical ports are numbered starting from 1. */
58 enum ofp_port {
59     /* Maximum number of physical switch ports. */
60     OFPP_MAX = 0xff00,
61
62     /* Fake output "ports". */
63     OFPP_IN_PORT    = 0xfff8,  /* Send the packet out the input port.  This
64                                   virtual port must be explicitly used
65                                   in order to send back out of the input
66                                   port. */
67     OFPP_TABLE      = 0xfff9,  /* Perform actions in flow table.
68                                   NB: This can only be the destination
69                                   port for packet-out messages. */
70     OFPP_NORMAL     = 0xfffa,  /* Process with normal L2/L3 switching. */
71     OFPP_FLOOD      = 0xfffb,  /* All physical ports except input port and
72                                   those disabled by STP. */
73     OFPP_ALL        = 0xfffc,  /* All physical ports except input port. */
74     OFPP_CONTROLLER = 0xfffd,  /* Send to controller. */
75     OFPP_LOCAL      = 0xfffe,  /* Local openflow "port". */
76     OFPP_NONE       = 0xffff   /* Not associated with a physical port. */
77 };
78
79 enum ofp_type {
80     /* Immutable messages. */
81     OFPT_HELLO,               /* Symmetric message */
82     OFPT_ERROR,               /* Symmetric message */
83     OFPT_ECHO_REQUEST,        /* Symmetric message */
84     OFPT_ECHO_REPLY,          /* Symmetric message */
85     OFPT_VENDOR,              /* Symmetric message */
86
87     /* Switch configuration messages. */
88     OFPT_FEATURES_REQUEST,    /* Controller/switch message */
89     OFPT_FEATURES_REPLY,      /* Controller/switch message */
90     OFPT_GET_CONFIG_REQUEST,  /* Controller/switch message */
91     OFPT_GET_CONFIG_REPLY,    /* Controller/switch message */
92     OFPT_SET_CONFIG,          /* Controller/switch message */
93
94     /* Asynchronous messages. */
95     OFPT_PACKET_IN,           /* Async message */
96     OFPT_FLOW_REMOVED,        /* Async message */
97     OFPT_PORT_STATUS,         /* Async message */
98
99     /* Controller command messages. */
100     OFPT_PACKET_OUT,          /* Controller/switch message */
101     OFPT_FLOW_MOD,            /* Controller/switch message */
102     OFPT_PORT_MOD,            /* Controller/switch message */
103
104     /* Statistics messages. */
105     OFPT_STATS_REQUEST,       /* Controller/switch message */
106     OFPT_STATS_REPLY,         /* Controller/switch message */
107
108     /* Barrier messages. */
109     OFPT_BARRIER_REQUEST,     /* Controller/switch message */
110     OFPT_BARRIER_REPLY,       /* Controller/switch message */
111
112     /* Queue Configuration messages. */
113     OFPT_QUEUE_GET_CONFIG_REQUEST,  /* Controller/switch message */
114     OFPT_QUEUE_GET_CONFIG_REPLY     /* Controller/switch message */
115 };
116
117 /* Header on all OpenFlow packets. */
118 struct ofp_header {
119     uint8_t version;    /* OFP_VERSION. */
120     uint8_t type;       /* One of the OFPT_ constants. */
121     uint16_t length;    /* Length including this ofp_header. */
122     uint32_t xid;       /* Transaction id associated with this packet.
123                            Replies use the same id as was in the request
124                            to facilitate pairing. */
125 };
126 OFP_ASSERT(sizeof(struct ofp_header) == 8);
127
128 /* OFPT_HELLO.  This message has an empty body, but implementations must
129  * ignore any data included in the body, to allow for future extensions. */
130 struct ofp_hello {
131     struct ofp_header header;
132 };
133
134 #define OFP_DEFAULT_MISS_SEND_LEN   128
135
136 enum ofp_config_flags {
137     /* Handling of IP fragments. */
138     OFPC_FRAG_NORMAL   = 0,  /* No special handling for fragments. */
139     OFPC_FRAG_DROP     = 1,  /* Drop fragments. */
140     OFPC_FRAG_REASM    = 2,  /* Reassemble (only if OFPC_IP_REASM set). */
141     OFPC_FRAG_MASK     = 3
142 };
143
144 /* Switch configuration. */
145 struct ofp_switch_config {
146     struct ofp_header header;
147     uint16_t flags;             /* OFPC_* flags. */
148     uint16_t 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) == 12);
152
153 /* Capabilities supported by the datapath. */
154 enum ofp_capabilities {
155     OFPC_FLOW_STATS     = 1 << 0,  /* Flow statistics. */
156     OFPC_TABLE_STATS    = 1 << 1,  /* Table statistics. */
157     OFPC_PORT_STATS     = 1 << 2,  /* Port statistics. */
158     OFPC_STP            = 1 << 3,  /* 802.1d spanning tree. */
159     OFPC_RESERVED       = 1 << 4,  /* Reserved, must not be set. */
160     OFPC_IP_REASM       = 1 << 5,  /* Can reassemble IP fragments. */
161     OFPC_QUEUE_STATS    = 1 << 6,  /* Queue statistics. */
162     OFPC_ARP_MATCH_IP   = 1 << 7   /* Match IP addresses in ARP
163                                       pkts. */
164 };
165
166 /* Flags to indicate behavior of the physical port.  These flags are
167  * used in ofp_phy_port to describe the current configuration.  They are
168  * used in the ofp_port_mod message to configure the port's behavior.
169  */
170 enum ofp_port_config {
171     OFPPC_PORT_DOWN    = 1 << 0,  /* Port is administratively down. */
172
173     OFPPC_NO_STP       = 1 << 1,  /* Disable 802.1D spanning tree on port. */
174     OFPPC_NO_RECV      = 1 << 2,  /* Drop all packets except 802.1D
175                                      spanning tree packets. */
176     OFPPC_NO_RECV_STP  = 1 << 3,  /* Drop received 802.1D STP packets. */
177     OFPPC_NO_FLOOD     = 1 << 4,  /* Do not include this port when flooding. */
178     OFPPC_NO_FWD       = 1 << 5,  /* Drop packets forwarded to port. */
179     OFPPC_NO_PACKET_IN = 1 << 6   /* Do not send packet-in msgs for port. */
180 };
181
182 /* Current state of the physical port.  These are not configurable from
183  * the controller.
184  */
185 enum ofp_port_state {
186     OFPPS_LINK_DOWN   = 1 << 0, /* No physical link present. */
187
188     /* The OFPPS_STP_* bits have no effect on switch operation.  The
189      * controller must adjust OFPPC_NO_RECV, OFPPC_NO_FWD, and
190      * OFPPC_NO_PACKET_IN appropriately to fully implement an 802.1D spanning
191      * tree. */
192     OFPPS_STP_LISTEN  = 0 << 8, /* Not learning or relaying frames. */
193     OFPPS_STP_LEARN   = 1 << 8, /* Learning but not relaying frames. */
194     OFPPS_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
195     OFPPS_STP_BLOCK   = 3 << 8, /* Not part of spanning tree. */
196     OFPPS_STP_MASK    = 3 << 8  /* Bit mask for OFPPS_STP_* values. */
197 };
198
199 /* Features of physical ports available in a datapath. */
200 enum ofp_port_features {
201     OFPPF_10MB_HD    = 1 << 0,  /* 10 Mb half-duplex rate support. */
202     OFPPF_10MB_FD    = 1 << 1,  /* 10 Mb full-duplex rate support. */
203     OFPPF_100MB_HD   = 1 << 2,  /* 100 Mb half-duplex rate support. */
204     OFPPF_100MB_FD   = 1 << 3,  /* 100 Mb full-duplex rate support. */
205     OFPPF_1GB_HD     = 1 << 4,  /* 1 Gb half-duplex rate support. */
206     OFPPF_1GB_FD     = 1 << 5,  /* 1 Gb full-duplex rate support. */
207     OFPPF_10GB_FD    = 1 << 6,  /* 10 Gb full-duplex rate support. */
208     OFPPF_COPPER     = 1 << 7,  /* Copper medium. */
209     OFPPF_FIBER      = 1 << 8,  /* Fiber medium. */
210     OFPPF_AUTONEG    = 1 << 9,  /* Auto-negotiation. */
211     OFPPF_PAUSE      = 1 << 10, /* Pause. */
212     OFPPF_PAUSE_ASYM = 1 << 11  /* Asymmetric pause. */
213 };
214
215 /* Description of a physical port */
216 struct ofp_phy_port {
217     uint16_t port_no;
218     uint8_t hw_addr[OFP_ETH_ALEN];
219     char name[OFP_MAX_PORT_NAME_LEN]; /* Null-terminated */
220
221     uint32_t config;        /* Bitmap of OFPPC_* flags. */
222     uint32_t state;         /* Bitmap of OFPPS_* flags. */
223
224     /* Bitmaps of OFPPF_* that describe features.  All bits zeroed if
225      * unsupported or unavailable. */
226     uint32_t curr;          /* Current features. */
227     uint32_t advertised;    /* Features being advertised by the port. */
228     uint32_t supported;     /* Features supported by the port. */
229     uint32_t peer;          /* Features advertised by peer. */
230 };
231 OFP_ASSERT(sizeof(struct ofp_phy_port) == 48);
232
233 /* Switch features. */
234 struct ofp_switch_features {
235     struct ofp_header header;
236     uint64_t datapath_id;   /* Datapath unique ID.  The lower 48-bits are for
237                                a MAC address, while the upper 16-bits are
238                                implementer-defined. */
239
240     uint32_t n_buffers;     /* Max packets buffered at once. */
241
242     uint8_t n_tables;       /* Number of tables supported by datapath. */
243     uint8_t pad[3];         /* Align to 64-bits. */
244
245     /* Features. */
246     uint32_t capabilities;  /* Bitmap of support "ofp_capabilities". */
247     uint32_t actions;       /* Bitmap of supported "ofp_action_type"s. */
248
249     /* Port info.*/
250     struct ofp_phy_port ports[0];  /* Port definitions.  The number of ports
251                                       is inferred from the length field in
252                                       the header. */
253 };
254 OFP_ASSERT(sizeof(struct ofp_switch_features) == 32);
255
256 /* What changed about the physical port */
257 enum ofp_port_reason {
258     OFPPR_ADD,              /* The port was added. */
259     OFPPR_DELETE,           /* The port was removed. */
260     OFPPR_MODIFY            /* Some attribute of the port has changed. */
261 };
262
263 /* A physical port has changed in the datapath */
264 struct ofp_port_status {
265     struct ofp_header header;
266     uint8_t reason;          /* One of OFPPR_*. */
267     uint8_t pad[7];          /* Align to 64-bits. */
268     struct ofp_phy_port desc;
269 };
270 OFP_ASSERT(sizeof(struct ofp_port_status) == 64);
271
272 /* Modify behavior of the physical port */
273 struct ofp_port_mod {
274     struct ofp_header header;
275     uint16_t port_no;
276     uint8_t hw_addr[OFP_ETH_ALEN]; /* The hardware address is not
277                                       configurable.  This is used to
278                                       sanity-check the request, so it must
279                                       be the same as returned in an
280                                       ofp_phy_port struct. */
281
282     uint32_t config;        /* Bitmap of OFPPC_* flags. */
283     uint32_t mask;          /* Bitmap of OFPPC_* flags to be changed. */
284
285     uint32_t advertise;     /* Bitmap of "ofp_port_features"s.  Zero all
286                                bits to prevent any action taking place. */
287     uint8_t pad[4];         /* Pad to 64-bits. */
288 };
289 OFP_ASSERT(sizeof(struct ofp_port_mod) == 32);
290
291 /* Why is this packet being sent to the controller? */
292 enum ofp_packet_in_reason {
293     OFPR_NO_MATCH,          /* No matching flow. */
294     OFPR_ACTION             /* Action explicitly output to controller. */
295 };
296
297 /* Packet received on port (datapath -> controller). */
298 struct ofp_packet_in {
299     struct ofp_header header;
300     uint32_t buffer_id;     /* ID assigned by datapath. */
301     uint16_t total_len;     /* Full length of frame. */
302     uint16_t in_port;       /* Port on which frame was received. */
303     uint8_t reason;         /* Reason packet is being sent (one of OFPR_*) */
304     uint8_t pad;
305     uint8_t data[0];        /* Ethernet frame, halfway through 32-bit word,
306                                so the IP header is 32-bit aligned.  The
307                                amount of data is inferred from the length
308                                field in the header.  Because of padding,
309                                offsetof(struct ofp_packet_in, data) ==
310                                sizeof(struct ofp_packet_in) - 2. */
311 };
312 OFP_ASSERT(sizeof(struct ofp_packet_in) == 20);
313
314 enum ofp_action_type {
315     OFPAT_OUTPUT,           /* Output to switch port. */
316     OFPAT_SET_VLAN_VID,     /* Set the 802.1q VLAN id. */
317     OFPAT_SET_VLAN_PCP,     /* Set the 802.1q priority. */
318     OFPAT_STRIP_VLAN,       /* Strip the 802.1q header. */
319     OFPAT_SET_DL_SRC,       /* Ethernet source address. */
320     OFPAT_SET_DL_DST,       /* Ethernet destination address. */
321     OFPAT_SET_NW_SRC,       /* IP source address. */
322     OFPAT_SET_NW_DST,       /* IP destination address. */
323     OFPAT_SET_NW_TOS,       /* IP ToS (DSCP field, 6 bits). */
324     OFPAT_SET_TP_SRC,       /* TCP/UDP source port. */
325     OFPAT_SET_TP_DST,       /* TCP/UDP destination port. */
326     OFPAT_ENQUEUE,          /* Output to queue. */
327     OFPAT_VENDOR = 0xffff
328 };
329
330 /* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
331  * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
332  * number of bytes to send.  A 'max_len' of zero means no bytes of the
333  * packet should be sent. */
334 struct ofp_action_output {
335     uint16_t type;                  /* OFPAT_OUTPUT. */
336     uint16_t len;                   /* Length is 8. */
337     uint16_t port;                  /* Output port. */
338     uint16_t max_len;               /* Max length to send to controller. */
339 };
340 OFP_ASSERT(sizeof(struct ofp_action_output) == 8);
341
342 /* The VLAN id is 12 bits, so we can use the entire 16 bits to indicate
343  * special conditions.  All ones is used to match that no VLAN id was
344  * set. */
345 #define OFP_VLAN_NONE      0xffff
346
347 /* Action structure for OFPAT_SET_VLAN_VID. */
348 struct ofp_action_vlan_vid {
349     uint16_t type;                  /* OFPAT_SET_VLAN_VID. */
350     uint16_t len;                   /* Length is 8. */
351     uint16_t vlan_vid;              /* VLAN id. */
352     uint8_t pad[2];
353 };
354 OFP_ASSERT(sizeof(struct ofp_action_vlan_vid) == 8);
355
356 /* Action structure for OFPAT_SET_VLAN_PCP. */
357 struct ofp_action_vlan_pcp {
358     uint16_t type;                  /* OFPAT_SET_VLAN_PCP. */
359     uint16_t len;                   /* Length is 8. */
360     uint8_t vlan_pcp;               /* VLAN priority. */
361     uint8_t pad[3];
362 };
363 OFP_ASSERT(sizeof(struct ofp_action_vlan_pcp) == 8);
364
365 /* Action structure for OFPAT_SET_DL_SRC/DST. */
366 struct ofp_action_dl_addr {
367     uint16_t type;                  /* OFPAT_SET_DL_SRC/DST. */
368     uint16_t len;                   /* Length is 16. */
369     uint8_t dl_addr[OFP_ETH_ALEN];  /* Ethernet address. */
370     uint8_t pad[6];
371 };
372 OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
373
374 /* Action structure for OFPAT_SET_NW_SRC/DST. */
375 struct ofp_action_nw_addr {
376     uint16_t type;                  /* OFPAT_SET_TW_SRC/DST. */
377     uint16_t len;                   /* Length is 8. */
378     uint32_t nw_addr;               /* IP address. */
379 };
380 OFP_ASSERT(sizeof(struct ofp_action_nw_addr) == 8);
381
382 /* Action structure for OFPAT_SET_NW_TOS. */
383 struct ofp_action_nw_tos {
384     uint16_t type;                  /* OFPAT_SET_TW_TOS. */
385     uint16_t len;                   /* Length is 8. */
386     uint8_t nw_tos;                 /* IP TOS (DSCP field, 6 bits). */
387     uint8_t pad[3];
388 };
389 OFP_ASSERT(sizeof(struct ofp_action_nw_tos) == 8);
390
391 /* Action structure for OFPAT_SET_TP_SRC/DST. */
392 struct ofp_action_tp_port {
393     uint16_t type;                  /* OFPAT_SET_TP_SRC/DST. */
394     uint16_t len;                   /* Length is 8. */
395     uint16_t tp_port;               /* TCP/UDP port. */
396     uint8_t pad[2];
397 };
398 OFP_ASSERT(sizeof(struct ofp_action_tp_port) == 8);
399
400 /* Action header for OFPAT_VENDOR. The rest of the body is vendor-defined. */
401 struct ofp_action_vendor_header {
402     uint16_t type;                  /* OFPAT_VENDOR. */
403     uint16_t len;                   /* Length is a multiple of 8. */
404     uint32_t vendor;                /* Vendor ID, which takes the same form
405                                        as in "struct ofp_vendor_header". */
406 };
407 OFP_ASSERT(sizeof(struct ofp_action_vendor_header) == 8);
408
409 /* Action header that is common to all actions.  The length includes the
410  * header and any padding used to make the action 64-bit aligned.
411  * NB: The length of an action *must* always be a multiple of eight. */
412 struct ofp_action_header {
413     uint16_t type;                  /* One of OFPAT_*. */
414     uint16_t len;                   /* Length of action, including this
415                                        header.  This is the length of action,
416                                        including any padding to make it
417                                        64-bit aligned. */
418     uint8_t pad[4];
419 };
420 OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
421
422 /* OFPAT_ENQUEUE action struct: send packets to given queue on port. */
423 struct ofp_action_enqueue {
424     uint16_t type;            /* OFPAT_ENQUEUE. */
425     uint16_t len;             /* Len is 16. */
426     uint16_t port;            /* Port that queue belongs. Should
427                                  refer to a valid physical port
428                                  (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
429     uint8_t pad[6];           /* Pad for 64-bit alignment. */
430     uint32_t queue_id;        /* Where to enqueue the packets. */
431 };
432 OFP_ASSERT(sizeof(struct ofp_action_enqueue) == 16);
433
434 union ofp_action {
435     uint16_t type;
436     struct ofp_action_header header;
437     struct ofp_action_vendor_header vendor;
438     struct ofp_action_output output;
439     struct ofp_action_vlan_vid vlan_vid;
440     struct ofp_action_vlan_pcp vlan_pcp;
441     struct ofp_action_nw_addr nw_addr;
442     struct ofp_action_nw_tos nw_tos;
443     struct ofp_action_tp_port tp_port;
444 };
445 OFP_ASSERT(sizeof(union ofp_action) == 8);
446
447 /* Send packet (controller -> datapath). */
448 struct ofp_packet_out {
449     struct ofp_header header;
450     uint32_t buffer_id;           /* ID assigned by datapath (-1 if none). */
451     uint16_t in_port;             /* Packet's input port (OFPP_NONE if none). */
452     uint16_t actions_len;         /* Size of action array in bytes. */
453     struct ofp_action_header actions[0]; /* Actions. */
454     /* uint8_t data[0]; */        /* Packet data.  The length is inferred
455                                      from the length field in the header.
456                                      (Only meaningful if buffer_id == -1.) */
457 };
458 OFP_ASSERT(sizeof(struct ofp_packet_out) == 16);
459
460 enum ofp_flow_mod_command {
461     OFPFC_ADD,              /* New flow. */
462     OFPFC_MODIFY,           /* Modify all matching flows. */
463     OFPFC_MODIFY_STRICT,    /* Modify entry strictly matching wildcards */
464     OFPFC_DELETE,           /* Delete all matching flows. */
465     OFPFC_DELETE_STRICT     /* Strictly match wildcards and priority. */
466 };
467
468 /* Flow wildcards. */
469 enum ofp_flow_wildcards {
470     OFPFW_IN_PORT    = 1 << 0,  /* Switch input port. */
471     OFPFW_DL_VLAN    = 1 << 1,  /* VLAN vid. */
472     OFPFW_DL_SRC     = 1 << 2,  /* Ethernet source address. */
473     OFPFW_DL_DST     = 1 << 3,  /* Ethernet destination address. */
474     OFPFW_DL_TYPE    = 1 << 4,  /* Ethernet frame type. */
475     OFPFW_NW_PROTO   = 1 << 5,  /* IP protocol. */
476     OFPFW_TP_SRC     = 1 << 6,  /* TCP/UDP source port. */
477     OFPFW_TP_DST     = 1 << 7,  /* TCP/UDP destination port. */
478
479     /* IP source address wildcard bit count.  0 is exact match, 1 ignores the
480      * LSB, 2 ignores the 2 least-significant bits, ..., 32 and higher wildcard
481      * the entire field.  This is the *opposite* of the usual convention where
482      * e.g. /24 indicates that 8 bits (not 24 bits) are wildcarded. */
483     OFPFW_NW_SRC_SHIFT = 8,
484     OFPFW_NW_SRC_BITS = 6,
485     OFPFW_NW_SRC_MASK = ((1 << OFPFW_NW_SRC_BITS) - 1) << OFPFW_NW_SRC_SHIFT,
486     OFPFW_NW_SRC_ALL = 32 << OFPFW_NW_SRC_SHIFT,
487
488     /* IP destination address wildcard bit count.  Same format as source. */
489     OFPFW_NW_DST_SHIFT = 14,
490     OFPFW_NW_DST_BITS = 6,
491     OFPFW_NW_DST_MASK = ((1 << OFPFW_NW_DST_BITS) - 1) << OFPFW_NW_DST_SHIFT,
492     OFPFW_NW_DST_ALL = 32 << OFPFW_NW_DST_SHIFT,
493
494     OFPFW_DL_VLAN_PCP = 1 << 20, /* VLAN priority. */
495     OFPFW_NW_TOS = 1 << 21, /* IP ToS (DSCP field, 6 bits). */
496
497     /* Wildcard all fields. */
498     OFPFW_ALL = ((1 << 22) - 1)
499 };
500
501 /* The wildcards for ICMP type and code fields use the transport source
502  * and destination port fields, respectively. */
503 #define OFPFW_ICMP_TYPE OFPFW_TP_SRC
504 #define OFPFW_ICMP_CODE OFPFW_TP_DST
505
506 /* Values below this cutoff are 802.3 packets and the two bytes
507  * following MAC addresses are used as a frame length.  Otherwise, the
508  * two bytes are used as the Ethernet type.
509  */
510 #define OFP_DL_TYPE_ETH2_CUTOFF   0x0600
511
512 /* Value of dl_type to indicate that the frame does not include an
513  * Ethernet type.
514  */
515 #define OFP_DL_TYPE_NOT_ETH_TYPE  0x05ff
516
517 /* The VLAN id is 12-bits, so we can use the entire 16 bits to indicate
518  * special conditions.  All ones indicates that no VLAN id was set.
519  */
520 #define OFP_VLAN_NONE      0xffff
521
522 /* Fields to match against flows */
523 struct ofp_match {
524     uint32_t wildcards;        /* Wildcard fields. */
525     uint16_t in_port;          /* Input switch port. */
526     uint8_t dl_src[OFP_ETH_ALEN]; /* Ethernet source address. */
527     uint8_t dl_dst[OFP_ETH_ALEN]; /* Ethernet destination address. */
528     uint16_t dl_vlan;          /* Input VLAN. */
529     uint8_t dl_vlan_pcp;       /* Input VLAN priority. */
530     uint8_t pad1[1];           /* Align to 64-bits. */
531     uint16_t dl_type;          /* Ethernet frame type. */
532     uint8_t nw_tos;            /* IP ToS (DSCP field, 6 bits). */
533     uint8_t nw_proto;          /* IP protocol or lower 8 bits of
534                                   ARP opcode. */
535     uint8_t pad2[2];           /* Align to 64-bits. */
536     uint32_t nw_src;           /* IP source address. */
537     uint32_t nw_dst;           /* IP destination address. */
538     uint16_t tp_src;           /* TCP/UDP source port. */
539     uint16_t tp_dst;           /* TCP/UDP destination port. */
540 };
541 OFP_ASSERT(sizeof(struct ofp_match) == 40);
542
543 /* The match fields for ICMP type and code use the transport source and
544  * destination port fields, respectively. */
545 #define icmp_type tp_src
546 #define icmp_code tp_dst
547
548 /* Value used in "idle_timeout" and "hard_timeout" to indicate that the entry
549  * is permanent. */
550 #define OFP_FLOW_PERMANENT 0
551
552 /* By default, choose a priority in the middle. */
553 #define OFP_DEFAULT_PRIORITY 0x8000
554
555 enum ofp_flow_mod_flags {
556     OFPFF_SEND_FLOW_REM = 1 << 0,  /* Send flow removed message when flow
557                                     * expires or is deleted. */
558     OFPFF_CHECK_OVERLAP = 1 << 1,  /* Check for overlapping entries first. */
559     OFPFF_EMERG         = 1 << 2   /* Ramark this is for emergency. */
560 };
561
562 /* Flow setup and teardown (controller -> datapath). */
563 struct ofp_flow_mod {
564     struct ofp_header header;
565     struct ofp_match match;      /* Fields to match */
566     uint64_t cookie;             /* Opaque controller-issued identifier. */
567
568     /* Flow actions. */
569     uint16_t command;             /* One of OFPFC_*. */
570     uint16_t idle_timeout;        /* Idle time before discarding (seconds). */
571     uint16_t hard_timeout;        /* Max time before discarding (seconds). */
572     uint16_t priority;            /* Priority level of flow entry. */
573     uint32_t buffer_id;           /* Buffered packet to apply to (or -1).
574                                      Not meaningful for OFPFC_DELETE*. */
575     uint16_t out_port;            /* For OFPFC_DELETE* commands, require
576                                      matching entries to include this as an
577                                      output port.  A value of OFPP_NONE
578                                      indicates no restriction. */
579     uint16_t flags;               /* One of OFPFF_*. */
580     struct ofp_action_header actions[0]; /* The action length is inferred
581                                             from the length field in the
582                                             header. */
583 };
584 OFP_ASSERT(sizeof(struct ofp_flow_mod) == 72);
585
586 /* Why was this flow removed? */
587 enum ofp_flow_removed_reason {
588     OFPRR_IDLE_TIMEOUT,         /* Flow idle time exceeded idle_timeout. */
589     OFPRR_HARD_TIMEOUT,         /* Time exceeded hard_timeout. */
590     OFPRR_DELETE                /* Evicted by a DELETE flow mod. */
591 };
592
593 /* Flow removed (datapath -> controller). */
594 struct ofp_flow_removed {
595     struct ofp_header header;
596     struct ofp_match match;   /* Description of fields. */
597     uint64_t cookie;          /* Opaque controller-issued identifier. */
598
599     uint16_t priority;        /* Priority level of flow entry. */
600     uint8_t reason;           /* One of OFPRR_*. */
601     uint8_t pad[1];           /* Align to 32-bits. */
602
603     uint32_t duration_sec;    /* Time flow was alive in seconds. */
604     uint32_t duration_nsec;   /* Time flow was alive in nanoseconds beyond
605                                  duration_sec. */
606     uint16_t idle_timeout;    /* Idle timeout from original flow mod. */
607     uint8_t pad2[2];          /* Align to 64-bits. */
608     uint64_t packet_count;
609     uint64_t byte_count;
610 };
611 OFP_ASSERT(sizeof(struct ofp_flow_removed) == 88);
612
613 /* Values for 'type' in ofp_error_message.  These values are immutable: they
614  * will not change in future versions of the protocol (although new values may
615  * be added). */
616 enum ofp_error_type {
617     OFPET_HELLO_FAILED,         /* Hello protocol failed. */
618     OFPET_BAD_REQUEST,          /* Request was not understood. */
619     OFPET_BAD_ACTION,           /* Error in action description. */
620     OFPET_FLOW_MOD_FAILED,      /* Problem modifying flow entry. */
621     OFPET_PORT_MOD_FAILED,      /* OFPT_PORT_MOD failed. */
622     OFPET_QUEUE_OP_FAILED       /* Queue operation failed. */
623 };
624
625 /* ofp_error_msg 'code' values for OFPET_HELLO_FAILED.  'data' contains an
626  * ASCII text string that may give failure details. */
627 enum ofp_hello_failed_code {
628     OFPHFC_INCOMPATIBLE,        /* No compatible version. */
629     OFPHFC_EPERM                /* Permissions error. */
630 };
631
632 /* ofp_error_msg 'code' values for OFPET_BAD_REQUEST.  'data' contains at least
633  * the first 64 bytes of the failed request. */
634 enum ofp_bad_request_code {
635     OFPBRC_BAD_VERSION,         /* ofp_header.version not supported. */
636     OFPBRC_BAD_TYPE,            /* ofp_header.type not supported. */
637     OFPBRC_BAD_STAT,            /* ofp_stats_request.type not supported. */
638     OFPBRC_BAD_VENDOR,          /* Vendor not supported (in ofp_vendor_header
639                                  * or ofp_stats_request or ofp_stats_reply). */
640     OFPBRC_BAD_SUBTYPE,         /* Vendor subtype not supported. */
641     OFPBRC_EPERM,               /* Permissions error. */
642     OFPBRC_BAD_LEN,             /* Wrong request length for type. */
643     OFPBRC_BUFFER_EMPTY,        /* Specified buffer has already been used. */
644     OFPBRC_BUFFER_UNKNOWN       /* Specified buffer does not exist. */
645 };
646
647 /* ofp_error_msg 'code' values for OFPET_BAD_ACTION.  'data' contains at least
648  * the first 64 bytes of the failed request. */
649 enum ofp_bad_action_code {
650     OFPBAC_BAD_TYPE,           /* Unknown action type. */
651     OFPBAC_BAD_LEN,            /* Length problem in actions. */
652     OFPBAC_BAD_VENDOR,         /* Unknown vendor id specified. */
653     OFPBAC_BAD_VENDOR_TYPE,    /* Unknown action type for vendor id. */
654     OFPBAC_BAD_OUT_PORT,       /* Problem validating output action. */
655     OFPBAC_BAD_ARGUMENT,       /* Bad action argument. */
656     OFPBAC_EPERM,              /* Permissions error. */
657     OFPBAC_TOO_MANY,           /* Can't handle this many actions. */
658     OFPBAC_BAD_QUEUE           /* Problem validating output queue. */
659 };
660
661 /* ofp_error_msg 'code' values for OFPET_FLOW_MOD_FAILED.  'data' contains
662  * at least the first 64 bytes of the failed request. */
663 enum ofp_flow_mod_failed_code {
664     OFPFMFC_ALL_TABLES_FULL,    /* Flow not added because of full tables. */
665     OFPFMFC_OVERLAP,            /* Attempted to add overlapping flow with
666                                  * CHECK_OVERLAP flag set. */
667     OFPFMFC_EPERM,              /* Permissions error. */
668     OFPFMFC_BAD_EMERG_TIMEOUT,  /* Flow not added because of non-zero idle/hard
669                                  * timeout. */
670     OFPFMFC_BAD_COMMAND,        /* Unknown command. */
671     OFPFMFC_UNSUPPORTED         /* Unsupported action list - cannot process in
672                                    the order specified. */
673 };
674
675 /* ofp_error_msg 'code' values for OFPET_PORT_MOD_FAILED.  'data' contains
676  * at least the first 64 bytes of the failed request. */
677 enum ofp_port_mod_failed_code {
678     OFPPMFC_BAD_PORT,            /* Specified port does not exist. */
679     OFPPMFC_BAD_HW_ADDR,         /* Specified hardware address is wrong. */
680 };
681
682 /* ofp_error msg 'code' values for OFPET_QUEUE_OP_FAILED. 'data' contains
683  * at least the first 64 bytes of the failed request */
684 enum ofp_queue_op_failed_code {
685     OFPQOFC_BAD_PORT,           /* Invalid port (or port does not exist). */
686     OFPQOFC_BAD_QUEUE,          /* Queue does not exist. */
687     OFPQOFC_EPERM               /* Permissions error. */
688 };
689
690 /* OFPT_ERROR: Error message (datapath -> controller). */
691 struct ofp_error_msg {
692     struct ofp_header header;
693
694     uint16_t type;
695     uint16_t code;
696     uint8_t data[0];          /* Variable-length data.  Interpreted based
697                                  on the type and code. */
698 };
699 OFP_ASSERT(sizeof(struct ofp_error_msg) == 12);
700
701 enum ofp_stats_types {
702     /* Description of this OpenFlow switch.
703      * The request body is empty.
704      * The reply body is struct ofp_desc_stats. */
705     OFPST_DESC,
706
707     /* Individual flow statistics.
708      * The request body is struct ofp_flow_stats_request.
709      * The reply body is an array of struct ofp_flow_stats. */
710     OFPST_FLOW,
711
712     /* Aggregate flow statistics.
713      * The request body is struct ofp_aggregate_stats_request.
714      * The reply body is struct ofp_aggregate_stats_reply. */
715     OFPST_AGGREGATE,
716
717     /* Flow table statistics.
718      * The request body is empty.
719      * The reply body is an array of struct ofp_table_stats. */
720     OFPST_TABLE,
721
722     /* Physical port statistics.
723      * The request body is struct ofp_port_stats_request.
724      * The reply body is an array of struct ofp_port_stats. */
725     OFPST_PORT,
726
727     /* Queue statistics for a port
728      * The request body is struct ofp_queue_stats_request.
729      * The reply body is an array of struct ofp_queue_stats. */
730     OFPST_QUEUE,
731
732     /* Vendor extension.
733      * The request and reply bodies begin with a 32-bit vendor ID, which takes
734      * the same form as in "struct ofp_vendor_header".  The request and reply
735      * bodies are otherwise vendor-defined. */
736     OFPST_VENDOR = 0xffff
737 };
738
739 struct ofp_stats_request {
740     struct ofp_header header;
741     uint16_t type;              /* One of the OFPST_* constants. */
742     uint16_t flags;             /* OFPSF_REQ_* flags (none yet defined). */
743     uint8_t body[0];            /* Body of the request. */
744 };
745 OFP_ASSERT(sizeof(struct ofp_stats_request) == 12);
746
747 enum ofp_stats_reply_flags {
748     OFPSF_REPLY_MORE  = 1 << 0  /* More replies to follow. */
749 };
750
751 struct ofp_stats_reply {
752     struct ofp_header header;
753     uint16_t type;              /* One of the OFPST_* constants. */
754     uint16_t flags;             /* OFPSF_REPLY_* flags. */
755     uint8_t body[0];            /* Body of the reply. */
756 };
757 OFP_ASSERT(sizeof(struct ofp_stats_reply) == 12);
758
759 #define DESC_STR_LEN   256
760 #define SERIAL_NUM_LEN 32
761 /* Body of reply to OFPST_DESC request.  Each entry is a NULL-terminated
762  * ASCII string. */
763 struct ofp_desc_stats {
764     char mfr_desc[DESC_STR_LEN];       /* Manufacturer description. */
765     char hw_desc[DESC_STR_LEN];        /* Hardware description. */
766     char sw_desc[DESC_STR_LEN];        /* Software description. */
767     char serial_num[SERIAL_NUM_LEN];   /* Serial number. */
768     char dp_desc[DESC_STR_LEN];        /* Human readable description of
769                                           the datapath. */
770 };
771 OFP_ASSERT(sizeof(struct ofp_desc_stats) == 1056);
772
773 /* Body for ofp_stats_request of type OFPST_FLOW. */
774 struct ofp_flow_stats_request {
775     struct ofp_match match;   /* Fields to match. */
776     uint8_t table_id;         /* ID of table to read (from ofp_table_stats)
777                                  or 0xff for all tables. */
778     uint8_t pad;              /* Align to 32 bits. */
779     uint16_t out_port;        /* Require matching entries to include this
780                                  as an output port.  A value of OFPP_NONE
781                                  indicates no restriction. */
782 };
783 OFP_ASSERT(sizeof(struct ofp_flow_stats_request) == 44);
784
785 /* Body of reply to OFPST_FLOW request. */
786 struct ofp_flow_stats {
787     uint16_t length;          /* Length of this entry. */
788     uint8_t table_id;         /* ID of table flow came from. */
789     uint8_t pad;
790     struct ofp_match match;   /* Description of fields. */
791     uint32_t duration_sec;    /* Time flow has been alive in seconds. */
792     uint32_t duration_nsec;   /* Time flow has been alive in nanoseconds
793                                  beyond duration_sec. */
794     uint16_t priority;        /* Priority of the entry. Only meaningful
795                                  when this is not an exact-match entry. */
796     uint16_t idle_timeout;    /* Number of seconds idle before expiration. */
797     uint16_t hard_timeout;    /* Number of seconds before expiration. */
798     uint8_t pad2[6];          /* Align to 64 bits. */
799     uint64_t cookie;          /* Opaque controller-issued identifier. */
800     uint64_t packet_count;    /* Number of packets in flow. */
801     uint64_t byte_count;      /* Number of bytes in flow. */
802     struct ofp_action_header actions[0]; /* Actions. */
803 };
804 OFP_ASSERT(sizeof(struct ofp_flow_stats) == 88);
805
806 /* Body for ofp_stats_request of type OFPST_AGGREGATE. */
807 struct ofp_aggregate_stats_request {
808     struct ofp_match match;   /* Fields to match. */
809     uint8_t table_id;         /* ID of table to read (from ofp_table_stats)
810                                  or 0xff for all tables. */
811     uint8_t pad;              /* Align to 32 bits. */
812     uint16_t out_port;        /* Require matching entries to include this
813                                  as an output port.  A value of OFPP_NONE
814                                  indicates no restriction. */
815 };
816 OFP_ASSERT(sizeof(struct ofp_aggregate_stats_request) == 44);
817
818 /* Body of reply to OFPST_AGGREGATE request. */
819 struct ofp_aggregate_stats_reply {
820     uint64_t packet_count;    /* Number of packets in flows. */
821     uint64_t byte_count;      /* Number of bytes in flows. */
822     uint32_t flow_count;      /* Number of flows. */
823     uint8_t pad[4];           /* Align to 64 bits. */
824 };
825 OFP_ASSERT(sizeof(struct ofp_aggregate_stats_reply) == 24);
826
827 /* Body of reply to OFPST_TABLE request. */
828 struct ofp_table_stats {
829     uint8_t table_id;        /* Identifier of table.  Lower numbered tables
830                                 are consulted first. */
831     uint8_t pad[3];          /* Align to 32-bits. */
832     char name[OFP_MAX_TABLE_NAME_LEN];
833     uint32_t wildcards;      /* Bitmap of OFPFW_* wildcards that are
834                                 supported by the table. */
835     uint32_t max_entries;    /* Max number of entries supported. */
836     uint32_t active_count;   /* Number of active entries. */
837     uint64_t lookup_count;   /* Number of packets looked up in table. */
838     uint64_t matched_count;  /* Number of packets that hit table. */
839 };
840 OFP_ASSERT(sizeof(struct ofp_table_stats) == 64);
841
842 /* Body for ofp_stats_request of type OFPST_PORT. */
843 struct ofp_port_stats_request {
844     uint16_t port_no;        /* OFPST_PORT message may request statistics
845                                 for a single port (specified with port_no)
846                                 or for all ports (port_no == OFPP_NONE). */
847     uint8_t pad[6];
848 };
849 OFP_ASSERT(sizeof(struct ofp_port_stats_request) == 8);
850
851 /* Body of reply to OFPST_PORT request. If a counter is unsupported, set
852  * the field to all ones. */
853 struct ofp_port_stats {
854     uint16_t port_no;
855     uint8_t pad[6];          /* Align to 64-bits. */
856     uint64_t rx_packets;     /* Number of received packets. */
857     uint64_t tx_packets;     /* Number of transmitted packets. */
858     uint64_t rx_bytes;       /* Number of received bytes. */
859     uint64_t tx_bytes;       /* Number of transmitted bytes. */
860     uint64_t rx_dropped;     /* Number of packets dropped by RX. */
861     uint64_t tx_dropped;     /* Number of packets dropped by TX. */
862     uint64_t rx_errors;      /* Number of receive errors.  This is a super-set
863                                 of receive errors and should be great than or
864                                 equal to the sum of all rx_*_err values. */
865     uint64_t tx_errors;      /* Number of transmit errors.  This is a super-set
866                                 of transmit errors. */
867     uint64_t rx_frame_err;   /* Number of frame alignment errors. */
868     uint64_t rx_over_err;    /* Number of packets with RX overrun. */
869     uint64_t rx_crc_err;     /* Number of CRC errors. */
870     uint64_t collisions;     /* Number of collisions. */
871 };
872 OFP_ASSERT(sizeof(struct ofp_port_stats) == 104);
873
874 /* All ones is used to indicate all queues in a port (for stats retrieval). */
875 #define OFPQ_ALL      0xffffffff
876
877 /* Body for ofp_stats_request of type OFPST_QUEUE. */
878 struct ofp_queue_stats_request {
879     uint16_t port_no;        /* All ports if OFPP_ALL. */
880     uint8_t pad[2];          /* Align to 32-bits. */
881     uint32_t queue_id;       /* All queues if OFPQ_ALL. */
882 };
883 OFP_ASSERT(sizeof(struct ofp_queue_stats_request) == 8);
884
885 /* Body for ofp_stats_reply of type OFPST_QUEUE consists of an array of this
886  * structure type. */
887 struct ofp_queue_stats {
888     uint16_t port_no;
889     uint8_t pad[2];          /* Align to 32-bits. */
890     uint32_t queue_id;       /* Queue id. */
891     uint64_t tx_bytes;       /* Number of transmitted bytes. */
892     uint64_t tx_packets;     /* Number of transmitted packets. */
893     uint64_t tx_errors;      /* Number of packets dropped due to overrun. */
894 };
895 OFP_ASSERT(sizeof(struct ofp_queue_stats) == 32);
896
897 /* Vendor extension. */
898 struct ofp_vendor_header {
899     struct ofp_header header;   /* Type OFPT_VENDOR. */
900     uint32_t vendor;            /* Vendor ID:
901                                  * - MSB 0: low-order bytes are IEEE OUI.
902                                  * - MSB != 0: defined by OpenFlow
903                                  *   consortium. */
904     /* Vendor-defined arbitrary additional data. */
905 };
906 OFP_ASSERT(sizeof(struct ofp_vendor_header) == 12);
907
908 #endif /* openflow/openflow.h */