dbf1f5e630aeda92b8e0e5ff5b88d5c4892240f1
[sliver-openvswitch.git] / include / openvswitch / datapath-protocol.h
1 /*
2  * Copyright (c) 2009, 2010, 2011 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  * Be sure to update datapath/odp-compat.h if you change any of the structures
43  * in here. */
44
45 #ifndef OPENVSWITCH_DATAPATH_PROTOCOL_H
46 #define OPENVSWITCH_DATAPATH_PROTOCOL_H 1
47
48 /* The ovs_be<N> types indicate that an object is in big-endian, not
49  * native-endian, byte order.  They are otherwise equivalent to uint<N>_t.
50  * The Linux kernel already has __be<N> types for this, which take on
51  * additional semantics when the "sparse" static checker is used, so we use
52  * those types when compiling the kernel. */
53 #ifdef __KERNEL__
54 #include <linux/types.h>
55 #include <linux/socket.h>
56 #define ovs_be16 __be16
57 #define ovs_be32 __be32
58 #define ovs_be64 __be64
59 #else
60 #include "openvswitch/types.h"
61 #include <sys/socket.h>
62 #endif
63
64 #include <linux/if_link.h>
65 #include <linux/netlink.h>
66 \f
67 /* datapaths. */
68
69 #define ODP_DATAPATH_FAMILY  "odp_datapath"
70 #define ODP_DATAPATH_MCGROUP "odp_datapath"
71
72 enum odp_datapath_cmd {
73         ODP_DP_CMD_UNSPEC,
74         ODP_DP_CMD_NEW,
75         ODP_DP_CMD_DEL,
76         ODP_DP_CMD_GET,
77         ODP_DP_CMD_SET
78 };
79
80 /**
81  * struct odp_header - header for ODP Generic Netlink messages.
82  * @dp_ifindex: ifindex of local port for datapath (0 to make a request not
83  * specific to a datapath).
84  *
85  * Attributes following the header are specific to a particular ODP Generic
86  * Netlink family, but all of the ODP families use this header.
87  */
88 struct odp_header {
89         int dp_ifindex;
90 };
91 \f
92 /**
93  * enum odp_datapath_attr - attributes for %ODP_DP_* commands.
94  * @ODP_DP_ATTR_NAME: Name of the network device that serves as the "local
95  * port".  This is the name of the network device whose dp_ifindex is given in
96  * the &struct odp_header.  Always present in notifications.  Required in
97  * %ODP_DP_NEW requests.  May be used as an alternative to specifying
98  * dp_ifindex in other requests (with a dp_ifindex of 0).
99  * @ODP_DP_ATTR_STATS: Statistics about packets that have passed through the
100  * datapath.  Always present in notifications.
101  * @ODP_DP_ATTR_IPV4_FRAGS: One of %ODP_DP_FRAG_*.  Always present in
102  * notifications.  May be included in %ODP_DP_NEW or %ODP_DP_SET requests to
103  * change the fragment handling policy.
104  * @ODP_DP_ATTR_SAMPLING: 32-bit fraction of packets to sample with
105  * @ODP_PACKET_CMD_SAMPLE.  A value of 0 samples no packets, a value of
106  * %UINT32_MAX samples all packets, and intermediate values sample intermediate
107  * fractions of packets.
108  * @ODP_DP_ATTR_MCGROUPS: Nested attributes with multicast groups.  Each nested
109  * attribute has a %ODP_PACKET_CMD_* type with a 32-bit value giving the
110  * Generic Netlink multicast group number used for sending this datapath's
111  * messages with that command type up to userspace.
112  *
113  * These attributes follow the &struct odp_header within the Generic Netlink
114  * payload for %ODP_DP_* commands.
115  */
116 enum odp_datapath_attr {
117         ODP_DP_ATTR_UNSPEC,
118         ODP_DP_ATTR_NAME,       /* name of dp_ifindex netdev */
119         ODP_DP_ATTR_STATS,      /* struct odp_stats */
120         ODP_DP_ATTR_IPV4_FRAGS, /* 32-bit enum odp_frag_handling */
121         ODP_DP_ATTR_SAMPLING,   /* 32-bit fraction of packets to sample. */
122         ODP_DP_ATTR_MCGROUPS,   /* Nested attributes with multicast groups. */
123         __ODP_DP_ATTR_MAX
124 };
125
126 #define ODP_DP_ATTR_MAX (__ODP_DP_ATTR_MAX - 1)
127
128 /**
129  * enum odp_frag_handling - policy for handling received IPv4 fragments.
130  * @ODP_DP_FRAG_ZERO: Treat IP fragments as IP protocol 0 and transport ports
131  * zero.
132  * @ODP_DP_FRAG_DROP: Drop IP fragments.  Do not pass them through the flow
133  * table or up to userspace.
134  */
135 enum odp_frag_handling {
136         ODP_DP_FRAG_UNSPEC,
137         ODP_DP_FRAG_ZERO,       /* Treat IP fragments as transport port 0. */
138         ODP_DP_FRAG_DROP        /* Drop IP fragments. */
139 };
140
141 struct odp_stats {
142     uint64_t n_frags;           /* Number of dropped IP fragments. */
143     uint64_t n_hit;             /* Number of flow table matches. */
144     uint64_t n_missed;          /* Number of flow table misses. */
145     uint64_t n_lost;            /* Number of misses not sent to userspace. */
146 };
147
148 /* Logical ports. */
149 #define ODPP_LOCAL      ((uint16_t)0)
150 \f
151 #define ODP_PACKET_FAMILY "odp_packet"
152
153 enum odp_packet_cmd {
154         ODP_PACKET_CMD_UNSPEC,
155
156         /* Kernel-to-user notifications. */
157         ODP_PACKET_CMD_MISS,    /* Flow table miss. */
158         ODP_PACKET_CMD_ACTION,  /* ODP_ACTION_ATTR_CONTROLLER action. */
159         ODP_PACKET_CMD_SAMPLE,  /* Sampled packet. */
160
161         /* User commands. */
162         ODP_PACKET_CMD_EXECUTE  /* Apply actions to a packet. */
163 };
164
165 /**
166  * enum odp_packet_attr - attributes for %ODP_PACKET_* commands.
167  * @ODP_PACKET_ATTR_PACKET: Present for all notifications.  Contains the entire
168  * packet as received, from the start of the Ethernet header onward.  For
169  * %ODP_PACKET_CMD_ACTION, %ODP_PACKET_ATTR_PACKET reflects changes made by
170  * actions preceding %ODP_ACTION_ATTR_CONTROLLER, but %ODP_PACKET_ATTR_KEY is
171  * the flow key extracted from the packet as originally received.
172  * @ODP_PACKET_ATTR_KEY: Present for all notifications.  Contains the flow key
173  * extracted from the packet as nested %ODP_KEY_ATTR_* attributes.  This allows
174  * userspace to adapt its flow setup strategy by comparing its notion of the
175  * flow key against the kernel's.
176  * @ODP_PACKET_ATTR_USERDATA: Present for an %ODP_PACKET_CMD_ACTION
177  * notification if the %ODP_ACTION_ATTR_CONTROLLER, action's argument was
178  * nonzero.
179  * @ODP_PACKET_ATTR_SAMPLE_POOL: Present for %ODP_PACKET_CMD_SAMPLE.  Contains
180  * the number of packets processed so far that were candidates for sampling.
181  * @ODP_PACKET_ATTR_ACTIONS: Present for %ODP_PACKET_CMD_SAMPLE.  Contains a
182  * copy of the actions applied to the packet, as nested %ODP_ACTION_ATTR_*
183  * attributes.
184  *
185  * These attributes follow the &struct odp_header within the Generic Netlink
186  * payload for %ODP_PACKET_* commands.
187  */
188 enum odp_packet_attr {
189         ODP_PACKET_ATTR_UNSPEC,
190         ODP_PACKET_ATTR_PACKET,      /* Packet data. */
191         ODP_PACKET_ATTR_KEY,         /* Nested ODP_KEY_ATTR_* attributes. */
192         ODP_PACKET_ATTR_USERDATA,    /* u64 ODP_ACTION_ATTR_CONTROLLER arg. */
193         ODP_PACKET_ATTR_SAMPLE_POOL, /* # sampling candidate packets so far. */
194         ODP_PACKET_ATTR_ACTIONS,     /* Nested ODP_ACTION_ATTR_* attributes. */
195         __ODP_PACKET_ATTR_MAX
196 };
197
198 #define ODP_PACKET_ATTR_MAX (__ODP_PACKET_ATTR_MAX - 1)
199 \f
200 enum odp_vport_type {
201         ODP_VPORT_TYPE_UNSPEC,
202         ODP_VPORT_TYPE_NETDEV,   /* network device */
203         ODP_VPORT_TYPE_INTERNAL, /* network device implemented by datapath */
204         ODP_VPORT_TYPE_PATCH,    /* virtual tunnel connecting two vports */
205         ODP_VPORT_TYPE_GRE,      /* GRE tunnel */
206         ODP_VPORT_TYPE_CAPWAP,   /* CAPWAP tunnel */
207         __ODP_VPORT_TYPE_MAX
208 };
209
210 #define ODP_VPORT_TYPE_MAX (__ODP_VPORT_TYPE_MAX - 1)
211 \f
212 #define ODP_VPORT_FAMILY  "odp_vport"
213 #define ODP_VPORT_MCGROUP "odp_vport"
214
215 enum odp_vport_cmd {
216         ODP_VPORT_CMD_UNSPEC,
217         ODP_VPORT_CMD_NEW,
218         ODP_VPORT_CMD_DEL,
219         ODP_VPORT_CMD_GET,
220         ODP_VPORT_CMD_SET
221 };
222
223 /**
224  * enum odp_vport_attr - attributes for %ODP_VPORT_* commands.
225  * @ODP_VPORT_ATTR_PORT_NO: 32-bit port number within datapath.
226  * @ODP_VPORT_ATTR_TYPE: 32-bit %ODP_VPORT_TYPE_* constant describing the type
227  * of vport.
228  * @ODP_VPORT_ATTR_NAME: Name of vport.  For a vport based on a network device
229  * this is the name of the network device.  Maximum length %IFNAMSIZ-1 bytes
230  * plus a null terminator.
231  * @ODP_VPORT_ATTR_STATS: A &struct rtnl_link_stats64 giving statistics for
232  * packets sent or received through the vport.
233  * @ODP_VPORT_ATTR_ADDRESS: A 6-byte Ethernet address for the vport.
234  * @ODP_VPORT_ATTR_MTU: MTU for the vport.  Omitted if the vport does not have
235  * an MTU as, e.g., some tunnels do not.
236  * @ODP_VPORT_ATTR_IFINDEX: ifindex of the underlying network device, if any.
237  * @ODP_VPORT_ATTR_IFLINK: ifindex of the device on which packets are sent (for
238  * tunnels), if any.
239  *
240  * These attributes follow the &struct odp_header within the Generic Netlink
241  * payload for %ODP_VPORT_* commands.
242  *
243  * All attributes applicable to a given port are present in notifications.
244  * This means that, for example, a vport that has no corresponding network
245  * device would omit %ODP_VPORT_ATTR_IFINDEX.
246  *
247  * For %ODP_VPORT_CMD_NEW requests, the %ODP_VPORT_ATTR_TYPE and
248  * %ODP_VPORT_ATTR_NAME attributes are required.  %ODP_VPORT_ATTR_PORT_NO is
249  * optional; if not specified a free port number is automatically selected.
250  * Whether %ODP_VPORT_ATTR_OPTIONS is required or optional depends on the type
251  * of vport.  %ODP_VPORT_ATTR_STATS, %ODP_VPORT_ATTR_ADDRESS, and
252  * %ODP_VPORT_ATTR_MTU are optional, and other attributes are ignored.
253  *
254  * For other requests, if %ODP_VPORT_ATTR_NAME is specified then it is used to
255  * look up the vport to operate on; otherwise dp_idx from the &struct
256  * odp_header plus %ODP_VPORT_ATTR_PORT_NO determine the vport.
257  */
258 enum odp_vport_attr {
259         ODP_VPORT_ATTR_UNSPEC,
260         ODP_VPORT_ATTR_PORT_NO, /* port number within datapath */
261         ODP_VPORT_ATTR_TYPE,    /* 32-bit ODP_VPORT_TYPE_* constant. */
262         ODP_VPORT_ATTR_NAME,    /* string name, up to IFNAMSIZ bytes long */
263         ODP_VPORT_ATTR_STATS,   /* struct rtnl_link_stats64 */
264         ODP_VPORT_ATTR_ADDRESS, /* hardware address */
265         ODP_VPORT_ATTR_MTU,     /* 32-bit maximum transmission unit */
266         ODP_VPORT_ATTR_OPTIONS, /* nested attributes, varies by vport type */
267         ODP_VPORT_ATTR_IFINDEX, /* 32-bit ifindex of backing netdev */
268         ODP_VPORT_ATTR_IFLINK,  /* 32-bit ifindex on which packets are sent */
269         __ODP_VPORT_ATTR_MAX
270 };
271
272 #define ODP_VPORT_ATTR_MAX (__ODP_VPORT_ATTR_MAX - 1)
273
274 /* ODP_VPORT_ATTR_OPTIONS attributes for patch vports. */
275 enum {
276         ODP_PATCH_ATTR_UNSPEC,
277         ODP_PATCH_ATTR_PEER,    /* name of peer vport, as a string */
278         __ODP_PATCH_ATTR_MAX
279 };
280
281 #define ODP_PATCH_ATTR_MAX (__ODP_PATCH_ATTR_MAX - 1)
282 \f
283 /* Flows. */
284
285 #define ODP_FLOW_FAMILY  "odp_flow"
286 #define ODP_FLOW_MCGROUP "odp_flow"
287
288 enum odp_flow_cmd {
289         ODP_FLOW_CMD_UNSPEC,
290         ODP_FLOW_CMD_NEW,
291         ODP_FLOW_CMD_DEL,
292         ODP_FLOW_CMD_GET,
293         ODP_FLOW_CMD_SET
294 };
295
296 struct odp_flow_stats {
297     uint64_t n_packets;         /* Number of matched packets. */
298     uint64_t n_bytes;           /* Number of matched bytes. */
299 };
300
301 enum odp_key_type {
302         ODP_KEY_ATTR_UNSPEC,
303         ODP_KEY_ATTR_TUN_ID,    /* 64-bit tunnel ID */
304         ODP_KEY_ATTR_IN_PORT,   /* 32-bit ODP port number */
305         ODP_KEY_ATTR_ETHERNET,  /* struct odp_key_ethernet */
306         ODP_KEY_ATTR_8021Q,     /* struct odp_key_8021q */
307         ODP_KEY_ATTR_ETHERTYPE, /* 16-bit Ethernet type */
308         ODP_KEY_ATTR_IPV4,      /* struct odp_key_ipv4 */
309         ODP_KEY_ATTR_IPV6,      /* struct odp_key_ipv6 */
310         ODP_KEY_ATTR_TCP,       /* struct odp_key_tcp */
311         ODP_KEY_ATTR_UDP,       /* struct odp_key_udp */
312         ODP_KEY_ATTR_ICMP,      /* struct odp_key_icmp */
313         ODP_KEY_ATTR_ICMPV6,    /* struct odp_key_icmpv6 */
314         ODP_KEY_ATTR_ARP,       /* struct odp_key_arp */
315         ODP_KEY_ATTR_ND,        /* struct odp_key_nd */
316         __ODP_KEY_ATTR_MAX
317 };
318
319 #define ODP_KEY_ATTR_MAX (__ODP_KEY_ATTR_MAX - 1)
320
321 struct odp_key_ethernet {
322         uint8_t  eth_src[6];
323         uint8_t  eth_dst[6];
324 };
325
326 struct odp_key_8021q {
327         ovs_be16 q_tpid;
328         ovs_be16 q_tci;
329 };
330
331 struct odp_key_ipv4 {
332         ovs_be32 ipv4_src;
333         ovs_be32 ipv4_dst;
334         uint8_t  ipv4_proto;
335         uint8_t  ipv4_tos;
336 };
337
338 struct odp_key_ipv6 {
339         ovs_be32 ipv6_src[4];
340         ovs_be32 ipv6_dst[4];
341         uint8_t  ipv6_proto;
342         uint8_t  ipv6_tos;
343 };
344
345 struct odp_key_tcp {
346         ovs_be16 tcp_src;
347         ovs_be16 tcp_dst;
348 };
349
350 struct odp_key_udp {
351         ovs_be16 udp_src;
352         ovs_be16 udp_dst;
353 };
354
355 struct odp_key_icmp {
356         uint8_t icmp_type;
357         uint8_t icmp_code;
358 };
359
360 struct odp_key_icmpv6 {
361         uint8_t icmpv6_type;
362         uint8_t icmpv6_code;
363 };
364
365 struct odp_key_arp {
366         ovs_be32 arp_sip;
367         ovs_be32 arp_tip;
368         ovs_be16 arp_op;
369         uint8_t  arp_sha[6];
370         uint8_t  arp_tha[6];
371 };
372
373 struct odp_key_nd {
374         uint32_t nd_target[4];
375         uint8_t  nd_sll[6];
376         uint8_t  nd_tll[6];
377 };
378
379 /**
380  * enum odp_flow_attr - attributes for %ODP_FLOW_* commands.
381  * @ODP_FLOW_ATTR_KEY: Nested %ODP_KEY_ATTR_* attributes specifying the flow
382  * key.  Always present in notifications.  Required for all requests (except
383  * dumps).
384  * @ODP_FLOW_ATTR_ACTIONS: Nested %ODPAT_* attributes specifying the actions to
385  * take for packets that match the key.  Always present in notifications.
386  * Required for %ODP_FLOW_CMD_NEW requests, optional on %ODP_FLOW_CMD_SET
387  * request to change the existing actions, ignored for other requests.
388  * @ODP_FLOW_ATTR_STATS: &struct odp_flow_stats giving statistics for this
389  * flow.  Present in notifications if the stats would be nonzero.  Ignored in
390  * requests.
391  * @ODP_FLOW_ATTR_TCP_FLAGS: An 8-bit value giving the OR'd value of all of the
392  * TCP flags seen on packets in this flow.  Only present in notifications for
393  * TCP flows, and only if it would be nonzero.  Ignored in requests.
394  * @ODP_FLOW_ATTR_USED: A 64-bit integer giving the time, in milliseconds on
395  * the system monotonic clock, at which a packet was last processed for this
396  * flow.  Only present in notifications if a packet has been processed for this
397  * flow.  Ignored in requests.
398  * @ODP_FLOW_ATTR_CLEAR: If present in a %ODP_FLOW_CMD_SET request, clears the
399  * last-used time, accumulated TCP flags, and statistics for this flow.
400  * Otherwise ignored in requests.  Never present in notifications.
401  *
402  * These attributes follow the &struct odp_header within the Generic Netlink
403  * payload for %ODP_FLOW_* commands.
404  */
405 enum odp_flow_attr {
406         ODP_FLOW_ATTR_UNSPEC,
407         ODP_FLOW_ATTR_KEY,       /* Sequence of ODP_KEY_ATTR_* attributes. */
408         ODP_FLOW_ATTR_ACTIONS,   /* Nested ODP_ACTION_ATTR_* attributes. */
409         ODP_FLOW_ATTR_STATS,     /* struct odp_flow_stats. */
410         ODP_FLOW_ATTR_TCP_FLAGS, /* 8-bit OR'd TCP flags. */
411         ODP_FLOW_ATTR_USED,      /* u64 msecs last used in monotonic time. */
412         ODP_FLOW_ATTR_CLEAR,     /* Flag to clear stats, tcp_flags, used. */
413         __ODP_FLOW_ATTR_MAX
414 };
415
416 #define ODP_FLOW_ATTR_MAX (__ODP_FLOW_ATTR_MAX - 1)
417
418 /* Action types. */
419 enum odp_action_type {
420         ODP_ACTION_ATTR_UNSPEC,
421         ODP_ACTION_ATTR_OUTPUT,       /* Output to switch port. */
422         ODP_ACTION_ATTR_CONTROLLER,   /* Send copy to controller. */
423         ODP_ACTION_ATTR_SET_DL_TCI,   /* Set the 802.1q TCI value. */
424         ODP_ACTION_ATTR_STRIP_VLAN,   /* Strip the 802.1q header. */
425         ODP_ACTION_ATTR_SET_DL_SRC,   /* Ethernet source address. */
426         ODP_ACTION_ATTR_SET_DL_DST,   /* Ethernet destination address. */
427         ODP_ACTION_ATTR_SET_NW_SRC,   /* IPv4 source address. */
428         ODP_ACTION_ATTR_SET_NW_DST,   /* IPv4 destination address. */
429         ODP_ACTION_ATTR_SET_NW_TOS,   /* IP ToS/DSCP field (6 bits). */
430         ODP_ACTION_ATTR_SET_TP_SRC,   /* TCP/UDP source port. */
431         ODP_ACTION_ATTR_SET_TP_DST,   /* TCP/UDP destination port. */
432         ODP_ACTION_ATTR_SET_TUNNEL,   /* Set the encapsulating tunnel ID. */
433         ODP_ACTION_ATTR_SET_PRIORITY, /* Set skb->priority. */
434         ODP_ACTION_ATTR_POP_PRIORITY, /* Restore original skb->priority. */
435         ODP_ACTION_ATTR_DROP_SPOOFED_ARP, /* Drop ARPs with spoofed source MAC. */
436         __ODP_ACTION_ATTR_MAX
437 };
438
439 #define ODP_ACTION_ATTR_MAX (__ODP_ACTION_ATTR_MAX - 1)
440
441 #endif  /* openvswitch/datapath-protocol.h */