Merge "master" into "next".
[cascardo/ovs.git] / include / openvswitch / datapath-protocol.h
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  *
4  * This file is offered under your choice of two licenses: Apache 2.0 or GNU
5  * GPL 2.0 or later.  The permission statements for each of these licenses is
6  * given below.  You may license your modifications to this file under either
7  * of these licenses or both.  If you wish to license your modifications under
8  * only one of these licenses, delete the permission text for the other
9  * license.
10  *
11  * ----------------------------------------------------------------------
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at:
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  * ----------------------------------------------------------------------
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License along
35  * with this program; if not, write to the Free Software Foundation, Inc.,
36  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37  * ----------------------------------------------------------------------
38  */
39
40 /* Protocol between userspace and kernel datapath. */
41
42 #ifndef OPENVSWITCH_DATAPATH_PROTOCOL_H
43 #define OPENVSWITCH_DATAPATH_PROTOCOL_H 1
44
45 #ifdef __KERNEL__
46 #include <linux/types.h>
47 #else
48 #include <sys/types.h>
49 #endif
50 #include <linux/if_ether.h>
51
52 #define ODP_MAX 256             /* Maximum number of datapaths. */
53
54 #define ODP_DP_CREATE           _IO('O', 0)
55 #define ODP_DP_DESTROY          _IO('O', 1)
56 #define ODP_DP_STATS            _IOW('O', 2, struct odp_stats)
57
58 #define ODP_GET_DROP_FRAGS      _IOW('O', 3, int)
59 #define ODP_SET_DROP_FRAGS      _IOR('O', 4, int)
60
61 #define ODP_GET_LISTEN_MASK     _IOW('O', 5, int)
62 #define ODP_SET_LISTEN_MASK     _IOR('O', 6, int)
63
64 #define ODP_PORT_ADD            _IOR('O', 7, struct odp_port)
65 #define ODP_PORT_DEL            _IOR('O', 8, int)
66 #define ODP_PORT_QUERY          _IOWR('O', 9, struct odp_port)
67 #define ODP_PORT_LIST           _IOWR('O', 10, struct odp_portvec)
68
69 #define ODP_PORT_GROUP_SET      _IOR('O', 11, struct odp_port_group)
70 #define ODP_PORT_GROUP_GET      _IOWR('O', 12, struct odp_port_group)
71
72 #define ODP_FLOW_GET            _IOWR('O', 13, struct odp_flow)
73 #define ODP_FLOW_PUT            _IOWR('O', 14, struct odp_flow)
74 #define ODP_FLOW_LIST           _IOWR('O', 15, struct odp_flowvec)
75 #define ODP_FLOW_FLUSH          _IO('O', 16)
76 #define ODP_FLOW_DEL            _IOWR('O', 17, struct odp_flow)
77
78 #define ODP_EXECUTE             _IOR('O', 18, struct odp_execute)
79
80 #define ODP_SET_SFLOW_PROBABILITY _IOR('O', 19, int)
81 #define ODP_GET_SFLOW_PROBABILITY _IOW('O', 20, int)
82
83 struct odp_stats {
84     /* Flows. */
85     __u32 n_flows;              /* Number of flows in flow table. */
86     __u32 cur_capacity;         /* Current flow table capacity. */
87     __u32 max_capacity;         /* Maximum expansion of flow table capacity. */
88
89     /* Ports. */
90     __u32 n_ports;              /* Current number of ports. */
91     __u32 max_ports;            /* Maximum supported number of ports. */
92     __u16 max_groups;           /* Maximum number of port groups. */
93     __u16 reserved;
94
95     /* Lookups. */
96     __u64 n_frags;               /* Number of dropped IP fragments. */
97     __u64 n_hit;                 /* Number of flow table matches. */
98     __u64 n_missed;              /* Number of flow table misses. */
99     __u64 n_lost;                /* Number of misses not sent to userspace. */
100
101     /* Queues. */
102     __u16 max_miss_queue;       /* Max length of ODPL_MISS queue. */
103     __u16 max_action_queue;     /* Max length of ODPL_ACTION queue. */
104     __u16 max_sflow_queue;      /* Max length of ODPL_SFLOW queue. */
105 };
106
107 /* Logical ports. */
108 #define ODPP_LOCAL      ((__u16)0)
109 #define ODPP_NONE       ((__u16)-1)
110 #define ODPP_NORMAL     ((__u16)-2)
111
112 /* Listening channels. */
113 #define _ODPL_MISS_NR   0       /* Packet missed in flow table. */
114 #define ODPL_MISS       (1 << _ODPL_MISS_NR)
115 #define _ODPL_ACTION_NR 1       /* Packet output to ODPP_CONTROLLER. */
116 #define ODPL_ACTION     (1 << _ODPL_ACTION_NR)
117 #define _ODPL_SFLOW_NR  2       /* sFlow samples. */
118 #define ODPL_SFLOW      (1 << _ODPL_SFLOW_NR)
119 #define ODPL_ALL        (ODPL_MISS | ODPL_ACTION | ODPL_SFLOW)
120
121 /**
122  * struct odp_msg - format of messages read from datapath fd.
123  * @type: One of the %_ODPL_* constants.
124  * @length: Total length of message, including this header.
125  * @port: Port that received the packet embedded in this message.
126  * @reserved: Not currently used.  Should be set to 0.
127  * @arg: Argument value whose meaning depends on @type.
128  *
129  * For @type == %_ODPL_MISS_NR, the header is followed by packet data.  The
130  * @arg member is unused and set to 0.
131  *
132  * For @type == %_ODPL_ACTION_NR, the header is followed by packet data.  The
133  * @arg member is copied from the &struct odp_action_controller that caused
134  * the &struct odp_msg to be composed.
135  *
136  * For @type == %_ODPL_SFLOW_NR, the header is followed by &struct
137  * odp_sflow_sample_header, then by an array of &union odp_action (the number
138  * of which is specified in &struct odp_sflow_sample_header), then by packet
139  * data.
140  */
141 struct odp_msg {
142     __u32 type;
143     __u32 length;
144     __u16 port;
145     __u16 reserved;
146     __u32 arg;
147 };
148
149 /**
150  * struct odp_sflow_sample_header - header added to sFlow sampled packet.
151  * @sample_pool: Number of packets that were candidates for sFlow sampling,
152  * regardless of whether they were actually chosen and sent down to userspace.
153  * @n_actions: Number of "union odp_action"s immediately following this header.
154  *
155  * This header follows &struct odp_msg when that structure's @type is
156  * %_ODPL_SFLOW_NR, and it is itself followed by an array of &union odp_action
157  * (the number of which is specified in @n_actions) and then by packet data.
158  */
159 struct odp_sflow_sample_header {
160     __u32 sample_pool;
161     __u32 n_actions;
162 };
163
164 #define ODP_PORT_INTERNAL (1 << 0) /* This port is simulated. */
165 struct odp_port {
166     char devname[16];           /* IFNAMSIZ */
167     __u16 port;
168     __u16 flags;
169     __u32 reserved2;
170 };
171
172 struct odp_portvec {
173     struct odp_port *ports;
174     int n_ports;
175 };
176
177 struct odp_port_group {
178     __u16 *ports;
179     __u16 n_ports;                /* Number of ports. */
180     __u16 group;                  /* Group number. */
181 };
182
183 struct odp_flow_stats {
184     __u64 n_packets;            /* Number of matched packets. */
185     __u64 n_bytes;              /* Number of matched bytes. */
186     __u64 used_sec;             /* Time last used. */
187     __u32 used_nsec;
188     __u8 tcp_flags;
189     __u8 ip_tos;
190     __u16 error;                /* Used by ODP_FLOW_GET. */
191 };
192
193 struct odp_flow_key {
194     __be32 nw_src;               /* IP source address. */
195     __be32 nw_dst;               /* IP destination address. */
196     __u16  in_port;              /* Input switch port. */
197     __be16 dl_vlan;              /* Input VLAN. */
198     __be16 dl_type;              /* Ethernet frame type. */
199     __be16 tp_src;               /* TCP/UDP source port. */
200     __be16 tp_dst;               /* TCP/UDP destination port. */
201     __u8   dl_src[ETH_ALEN];     /* Ethernet source address. */
202     __u8   dl_dst[ETH_ALEN];     /* Ethernet destination address. */
203     __u8   nw_proto;             /* IP protocol or lower 8 bits of 
204                                     ARP opcode. */
205     __u8   dl_vlan_pcp;          /* Input VLAN priority. */
206     __u8   nw_tos;               /* IP ToS (DSCP field, 6 bits). */
207     __u8   reserved[3];          /* Align to 32-bits...must be zeroed. */
208 };
209
210 /* Flags for ODP_FLOW. */
211 #define ODPFF_ZERO_TCP_FLAGS (1 << 0) /* Zero the TCP flags. */
212
213 struct odp_flow {
214     struct odp_flow_stats stats;
215     struct odp_flow_key key;
216     union odp_action *actions;
217     __u32 n_actions;
218     __u32 flags;
219 };
220
221 /* Flags for ODP_FLOW_PUT. */
222 #define ODPPF_CREATE        (1 << 0) /* Allow creating a new flow. */
223 #define ODPPF_MODIFY        (1 << 1) /* Allow modifying an existing flow. */
224 #define ODPPF_ZERO_STATS    (1 << 2) /* Zero the stats of an existing flow. */
225
226 /* ODP_FLOW_PUT argument. */
227 struct odp_flow_put {
228     struct odp_flow flow;
229     __u32 flags;
230 };
231
232 struct odp_flowvec {
233     struct odp_flow *flows;
234     int n_flows;
235 };
236
237 /* The VLAN id is 12 bits, so we can use the entire 16 bits to indicate
238  * special conditions.  All ones is used to match that no VLAN id was
239  * set. */
240 #define ODP_VLAN_NONE      0xffff
241
242 /* Action types. */
243 #define ODPAT_OUTPUT            0    /* Output to switch port. */
244 #define ODPAT_OUTPUT_GROUP      1    /* Output to all ports in group. */
245 #define ODPAT_CONTROLLER        2    /* Send copy to controller. */
246 #define ODPAT_SET_VLAN_VID      3    /* Set the 802.1q VLAN id. */
247 #define ODPAT_SET_VLAN_PCP      4    /* Set the 802.1q priority. */
248 #define ODPAT_STRIP_VLAN        5    /* Strip the 802.1q header. */
249 #define ODPAT_SET_DL_SRC        6    /* Ethernet source address. */
250 #define ODPAT_SET_DL_DST        7    /* Ethernet destination address. */
251 #define ODPAT_SET_NW_SRC        8    /* IP source address. */
252 #define ODPAT_SET_NW_DST        9    /* IP destination address. */
253 #define ODPAT_SET_NW_TOS        10   /* IP ToS/DSCP field (6 bits). */
254 #define ODPAT_SET_TP_SRC        11   /* TCP/UDP source port. */
255 #define ODPAT_SET_TP_DST        12   /* TCP/UDP destination port. */
256 #define ODPAT_N_ACTIONS         13
257
258 struct odp_action_output {
259     __u16 type;                  /* ODPAT_OUTPUT. */
260     __u16 port;                  /* Output port. */
261     __u16 reserved1;
262     __u16 reserved2;
263 };
264
265 struct odp_action_output_group {
266     __u16 type;                 /* ODPAT_OUTPUT_GROUP. */
267     __u16 group;                /* Group number. */
268     __u16 reserved1;
269     __u16 reserved2;
270 };
271
272 struct odp_action_controller {
273     __u16 type;                 /* ODPAT_OUTPUT_CONTROLLER. */
274     __u16 reserved;
275     __u32 arg;                  /* Copied to struct odp_msg 'arg' member. */
276 };
277
278 /* Action structure for ODPAT_SET_VLAN_VID. */
279 struct odp_action_vlan_vid {
280     __u16 type;                  /* ODPAT_SET_VLAN_VID. */
281     __be16 vlan_vid;             /* VLAN id. */
282     __u16 reserved1;
283     __u16 reserved2;
284 };
285
286 /* Action structure for ODPAT_SET_VLAN_PCP. */
287 struct odp_action_vlan_pcp {
288     __u16 type;                  /* ODPAT_SET_VLAN_PCP. */
289     __u8 vlan_pcp;               /* VLAN priority. */
290     __u8 reserved1;
291     __u16 reserved2;
292     __u16 reserved3;
293 };
294
295 /* Action structure for ODPAT_SET_DL_SRC/DST. */
296 struct odp_action_dl_addr {
297     __u16 type;                  /* ODPAT_SET_DL_SRC/DST. */
298     __u8 dl_addr[ETH_ALEN];      /* Ethernet address. */
299 };
300
301 /* Action structure for ODPAT_SET_NW_SRC/DST. */
302 struct odp_action_nw_addr {
303     __u16 type;                 /* ODPAT_SET_TW_SRC/DST. */
304     __u16 reserved;
305     __be32 nw_addr;             /* IP address. */
306 };
307
308 struct odp_action_nw_tos {
309     __u16 type;                  /* ODPAT_SET_NW_TOS. */
310     __u8 nw_tos;                 /* IP ToS/DSCP field (6 bits). */
311     __u8 reserved1;
312     __u16 reserved2;
313     __u16 reserved3;
314 };
315
316 /* Action structure for ODPAT_SET_TP_SRC/DST. */
317 struct odp_action_tp_port {
318     __u16 type;                  /* ODPAT_SET_TP_SRC/DST. */
319     __be16 tp_port;              /* TCP/UDP port. */
320     __u16 reserved1;
321     __u16 reserved2;
322 };
323
324 union odp_action {
325     __u16 type;
326     struct odp_action_output output;
327     struct odp_action_output_group output_group;
328     struct odp_action_controller controller;
329     struct odp_action_vlan_vid vlan_vid;
330     struct odp_action_vlan_pcp vlan_pcp;
331     struct odp_action_dl_addr dl_addr;
332     struct odp_action_nw_addr nw_addr;
333     struct odp_action_nw_tos nw_tos;
334     struct odp_action_tp_port tp_port;
335 };
336
337 struct odp_execute {
338     __u16 in_port;
339     __u16 reserved1;
340     __u32 reserved2;
341
342     union odp_action *actions;
343     __u32 n_actions;
344
345     const void *data;
346     __u32 length;
347 };
348
349 /* Values below this cutoff are 802.3 packets and the two bytes
350  * following MAC addresses are used as a frame length.  Otherwise, the
351  * two bytes are used as the Ethernet type.
352  */
353 #define ODP_DL_TYPE_ETH2_CUTOFF   0x0600
354
355 /* Value of dl_type to indicate that the frame does not include an
356  * Ethernet type.
357  */
358 #define ODP_DL_TYPE_NOT_ETH_TYPE  0x05ff
359
360 /* The VLAN id is 12-bits, so we can use the entire 16 bits to indicate
361  * special conditions.  All ones indicates that no VLAN id was set.
362  */
363 #define ODP_VLAN_NONE      0xffff
364
365 #endif /* openvswitch/datapath-protocol.h */