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