5687792dff708408048e2afcc70c38d04ddaff7f
[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 #ifndef OPENVSWITCH_DATAPATH_PROTOCOL_H
41 #define OPENVSWITCH_DATAPATH_PROTOCOL_H 1
42
43 #ifdef __KERNEL__
44 #include <linux/types.h>
45 #include <linux/socket.h>
46 #define ovs_be16 __be16
47 #define ovs_be32 __be32
48 #define ovs_be64 __be64
49 #else
50 #include "openvswitch/types.h"
51 #include <sys/socket.h>
52 #endif
53
54 #include <linux/if_link.h>
55 #include <linux/netlink.h>
56 \f
57 /* datapaths. */
58
59 #define OVS_DATAPATH_FAMILY  "ovs_datapath"
60 #define OVS_DATAPATH_MCGROUP "ovs_datapath"
61
62 enum ovs_datapath_cmd {
63         OVS_DP_CMD_UNSPEC,
64         OVS_DP_CMD_NEW,
65         OVS_DP_CMD_DEL,
66         OVS_DP_CMD_GET,
67         OVS_DP_CMD_SET
68 };
69
70 /**
71  * struct ovs_header - header for OVS Generic Netlink messages.
72  * @dp_ifindex: ifindex of local port for datapath (0 to make a request not
73  * specific to a datapath).
74  *
75  * Attributes following the header are specific to a particular OVS Generic
76  * Netlink family, but all of the OVS families use this header.
77  */
78 struct ovs_header {
79         int dp_ifindex;
80 };
81 \f
82 /**
83  * enum ovs_datapath_attr - attributes for %OVS_DP_* commands.
84  * @OVS_DP_ATTR_NAME: Name of the network device that serves as the "local
85  * port".  This is the name of the network device whose dp_ifindex is given in
86  * the &struct ovs_header.  Always present in notifications.  Required in
87  * %OVS_DP_NEW requests.  May be used as an alternative to specifying
88  * dp_ifindex in other requests (with a dp_ifindex of 0).
89  * @OVS_DP_ATTR_STATS: Statistics about packets that have passed through the
90  * datapath.  Always present in notifications.
91  * @OVS_DP_ATTR_IPV4_FRAGS: One of %OVS_DP_FRAG_*.  Always present in
92  * notifications.  May be included in %OVS_DP_NEW or %OVS_DP_SET requests to
93  * change the fragment handling policy.
94  * @OVS_DP_ATTR_SAMPLING: 32-bit fraction of packets to sample with
95  * @OVS_PACKET_CMD_SAMPLE.  A value of 0 samples no packets, a value of
96  * %UINT32_MAX samples all packets, and intermediate values sample intermediate
97  * fractions of packets.
98  * @OVS_DP_ATTR_MCGROUPS: Nested attributes with multicast groups.  Each nested
99  * attribute has a %OVS_PACKET_CMD_* type with a 32-bit value giving the
100  * Generic Netlink multicast group number used for sending this datapath's
101  * messages with that command type up to userspace.
102  *
103  * These attributes follow the &struct ovs_header within the Generic Netlink
104  * payload for %OVS_DP_* commands.
105  */
106 enum ovs_datapath_attr {
107         OVS_DP_ATTR_UNSPEC,
108         OVS_DP_ATTR_NAME,       /* name of dp_ifindex netdev */
109         OVS_DP_ATTR_STATS,      /* struct ovs_dp_stats */
110         OVS_DP_ATTR_IPV4_FRAGS, /* 32-bit enum ovs_frag_handling */
111         OVS_DP_ATTR_SAMPLING,   /* 32-bit fraction of packets to sample. */
112         OVS_DP_ATTR_MCGROUPS,   /* Nested attributes with multicast groups. */
113         __OVS_DP_ATTR_MAX
114 };
115
116 #define OVS_DP_ATTR_MAX (__OVS_DP_ATTR_MAX - 1)
117
118 /**
119  * enum ovs_frag_handling - policy for handling received IPv4 fragments.
120  * @OVS_DP_FRAG_ZERO: Treat IP fragments as IP protocol 0 and transport ports
121  * zero.
122  * @OVS_DP_FRAG_DROP: Drop IP fragments.  Do not pass them through the flow
123  * table or up to userspace.
124  */
125 enum ovs_frag_handling {
126         OVS_DP_FRAG_UNSPEC,
127         OVS_DP_FRAG_ZERO,       /* Treat IP fragments as transport port 0. */
128         OVS_DP_FRAG_DROP        /* Drop IP fragments. */
129 };
130
131 struct ovs_dp_stats {
132     uint64_t n_frags;           /* Number of dropped IP fragments. */
133     uint64_t n_hit;             /* Number of flow table matches. */
134     uint64_t n_missed;          /* Number of flow table misses. */
135     uint64_t n_lost;            /* Number of misses not sent to userspace. */
136     uint64_t n_flows;           /* Number of flows present */
137 };
138
139 /* Logical ports. */
140 #define OVSP_LOCAL      ((uint16_t)0)
141 \f
142 #define OVS_PACKET_FAMILY "ovs_packet"
143
144 enum ovs_packet_cmd {
145         OVS_PACKET_CMD_UNSPEC,
146
147         /* Kernel-to-user notifications. */
148         OVS_PACKET_CMD_MISS,    /* Flow table miss. */
149         OVS_PACKET_CMD_ACTION,  /* OVS_ACTION_ATTR_USERSPACE action. */
150         OVS_PACKET_CMD_SAMPLE,  /* Sampled packet. */
151
152         /* User commands. */
153         OVS_PACKET_CMD_EXECUTE  /* Apply actions to a packet. */
154 };
155
156 /**
157  * enum ovs_packet_attr - attributes for %OVS_PACKET_* commands.
158  * @OVS_PACKET_ATTR_PACKET: Present for all notifications.  Contains the entire
159  * packet as received, from the start of the Ethernet header onward.  For
160  * %OVS_PACKET_CMD_ACTION, %OVS_PACKET_ATTR_PACKET reflects changes made by
161  * actions preceding %OVS_ACTION_ATTR_USERSPACE, but %OVS_PACKET_ATTR_KEY is
162  * the flow key extracted from the packet as originally received.
163  * @OVS_PACKET_ATTR_KEY: Present for all notifications.  Contains the flow key
164  * extracted from the packet as nested %OVS_KEY_ATTR_* attributes.  This allows
165  * userspace to adapt its flow setup strategy by comparing its notion of the
166  * flow key against the kernel's.
167  * @OVS_PACKET_ATTR_USERDATA: Present for an %OVS_PACKET_CMD_ACTION
168  * notification if the %OVS_ACTION_ATTR_USERSPACE, action's argument was
169  * nonzero.
170  * @OVS_PACKET_ATTR_SAMPLE_POOL: Present for %OVS_PACKET_CMD_SAMPLE.  Contains
171  * the number of packets processed so far that were candidates for sampling.
172  * @OVS_PACKET_ATTR_ACTIONS: Present for %OVS_PACKET_CMD_SAMPLE.  Contains a
173  * copy of the actions applied to the packet, as nested %OVS_ACTION_ATTR_*
174  * attributes.
175  *
176  * These attributes follow the &struct ovs_header within the Generic Netlink
177  * payload for %OVS_PACKET_* commands.
178  */
179 enum ovs_packet_attr {
180         OVS_PACKET_ATTR_UNSPEC,
181         OVS_PACKET_ATTR_PACKET,      /* Packet data. */
182         OVS_PACKET_ATTR_KEY,         /* Nested OVS_KEY_ATTR_* attributes. */
183         OVS_PACKET_ATTR_USERDATA,    /* u64 OVS_ACTION_ATTR_USERSPACE arg. */
184         OVS_PACKET_ATTR_SAMPLE_POOL, /* # sampling candidate packets so far. */
185         OVS_PACKET_ATTR_ACTIONS,     /* Nested OVS_ACTION_ATTR_* attributes. */
186         __OVS_PACKET_ATTR_MAX
187 };
188
189 #define OVS_PACKET_ATTR_MAX (__OVS_PACKET_ATTR_MAX - 1)
190 \f
191 enum ovs_vport_type {
192         OVS_VPORT_TYPE_UNSPEC,
193         OVS_VPORT_TYPE_NETDEV,   /* network device */
194         OVS_VPORT_TYPE_INTERNAL, /* network device implemented by datapath */
195         OVS_VPORT_TYPE_PATCH,    /* virtual tunnel connecting two vports */
196         OVS_VPORT_TYPE_GRE,      /* GRE tunnel */
197         OVS_VPORT_TYPE_CAPWAP,   /* CAPWAP tunnel */
198         __OVS_VPORT_TYPE_MAX
199 };
200
201 #define OVS_VPORT_TYPE_MAX (__OVS_VPORT_TYPE_MAX - 1)
202 \f
203 #define OVS_VPORT_FAMILY  "ovs_vport"
204 #define OVS_VPORT_MCGROUP "ovs_vport"
205
206 enum ovs_vport_cmd {
207         OVS_VPORT_CMD_UNSPEC,
208         OVS_VPORT_CMD_NEW,
209         OVS_VPORT_CMD_DEL,
210         OVS_VPORT_CMD_GET,
211         OVS_VPORT_CMD_SET
212 };
213
214 /**
215  * enum ovs_vport_attr - attributes for %OVS_VPORT_* commands.
216  * @OVS_VPORT_ATTR_PORT_NO: 32-bit port number within datapath.
217  * @OVS_VPORT_ATTR_TYPE: 32-bit %OVS_VPORT_TYPE_* constant describing the type
218  * of vport.
219  * @OVS_VPORT_ATTR_NAME: Name of vport.  For a vport based on a network device
220  * this is the name of the network device.  Maximum length %IFNAMSIZ-1 bytes
221  * plus a null terminator.
222  * @OVS_VPORT_ATTR_STATS: A &struct rtnl_link_stats64 giving statistics for
223  * packets sent or received through the vport.
224  * @OVS_VPORT_ATTR_ADDRESS: A 6-byte Ethernet address for the vport.
225  * @OVS_VPORT_ATTR_IFINDEX: ifindex of the underlying network device, if any.
226  *
227  * These attributes follow the &struct ovs_header within the Generic Netlink
228  * payload for %OVS_VPORT_* commands.
229  *
230  * All attributes applicable to a given port are present in notifications.
231  * This means that, for example, a vport that has no corresponding network
232  * device would omit %OVS_VPORT_ATTR_IFINDEX.
233  *
234  * For %OVS_VPORT_CMD_NEW requests, the %OVS_VPORT_ATTR_TYPE and
235  * %OVS_VPORT_ATTR_NAME attributes are required.  %OVS_VPORT_ATTR_PORT_NO is
236  * optional; if not specified a free port number is automatically selected.
237  * Whether %OVS_VPORT_ATTR_OPTIONS is required or optional depends on the type
238  * of vport.  %OVS_VPORT_ATTR_STATS and %OVS_VPORT_ATTR_ADDRESS are optional,
239  * and other attributes are ignored.
240  *
241  * For other requests, if %OVS_VPORT_ATTR_NAME is specified then it is used to
242  * look up the vport to operate on; otherwise dp_idx from the &struct
243  * ovs_header plus %OVS_VPORT_ATTR_PORT_NO determine the vport.
244  */
245 enum ovs_vport_attr {
246         OVS_VPORT_ATTR_UNSPEC,
247         OVS_VPORT_ATTR_PORT_NO, /* port number within datapath */
248         OVS_VPORT_ATTR_TYPE,    /* 32-bit OVS_VPORT_TYPE_* constant. */
249         OVS_VPORT_ATTR_NAME,    /* string name, up to IFNAMSIZ bytes long */
250         OVS_VPORT_ATTR_STATS,   /* struct rtnl_link_stats64 */
251         OVS_VPORT_ATTR_ADDRESS, /* hardware address */
252         OVS_VPORT_ATTR_OPTIONS, /* nested attributes, varies by vport type */
253         OVS_VPORT_ATTR_IFINDEX, /* 32-bit ifindex of backing netdev */
254         __OVS_VPORT_ATTR_MAX
255 };
256
257 #define OVS_VPORT_ATTR_MAX (__OVS_VPORT_ATTR_MAX - 1)
258
259 /* OVS_VPORT_ATTR_OPTIONS attributes for patch vports. */
260 enum {
261         OVS_PATCH_ATTR_UNSPEC,
262         OVS_PATCH_ATTR_PEER,    /* name of peer vport, as a string */
263         __OVS_PATCH_ATTR_MAX
264 };
265
266 #define OVS_PATCH_ATTR_MAX (__OVS_PATCH_ATTR_MAX - 1)
267 \f
268 /* Flows. */
269
270 #define OVS_FLOW_FAMILY  "ovs_flow"
271 #define OVS_FLOW_MCGROUP "ovs_flow"
272
273 enum ovs_flow_cmd {
274         OVS_FLOW_CMD_UNSPEC,
275         OVS_FLOW_CMD_NEW,
276         OVS_FLOW_CMD_DEL,
277         OVS_FLOW_CMD_GET,
278         OVS_FLOW_CMD_SET
279 };
280
281 struct ovs_flow_stats {
282     uint64_t n_packets;         /* Number of matched packets. */
283     uint64_t n_bytes;           /* Number of matched bytes. */
284 };
285
286 enum ovs_key_type {
287         OVS_KEY_ATTR_UNSPEC,
288         OVS_KEY_ATTR_TUN_ID,    /* 64-bit tunnel ID */
289         OVS_KEY_ATTR_IN_PORT,   /* 32-bit OVS dp port number */
290         OVS_KEY_ATTR_ETHERNET,  /* struct ovs_key_ethernet */
291         OVS_KEY_ATTR_8021Q,     /* struct ovs_key_8021q */
292         OVS_KEY_ATTR_ETHERTYPE, /* 16-bit Ethernet type */
293         OVS_KEY_ATTR_IPV4,      /* struct ovs_key_ipv4 */
294         OVS_KEY_ATTR_IPV6,      /* struct ovs_key_ipv6 */
295         OVS_KEY_ATTR_TCP,       /* struct ovs_key_tcp */
296         OVS_KEY_ATTR_UDP,       /* struct ovs_key_udp */
297         OVS_KEY_ATTR_ICMP,      /* struct ovs_key_icmp */
298         OVS_KEY_ATTR_ICMPV6,    /* struct ovs_key_icmpv6 */
299         OVS_KEY_ATTR_ARP,       /* struct ovs_key_arp */
300         OVS_KEY_ATTR_ND,        /* struct ovs_key_nd */
301         __OVS_KEY_ATTR_MAX
302 };
303
304 #define OVS_KEY_ATTR_MAX (__OVS_KEY_ATTR_MAX - 1)
305
306 struct ovs_key_ethernet {
307         uint8_t  eth_src[6];
308         uint8_t  eth_dst[6];
309 };
310
311 struct ovs_key_8021q {
312         ovs_be16 q_tpid;
313         ovs_be16 q_tci;
314 };
315
316 struct ovs_key_ipv4 {
317         ovs_be32 ipv4_src;
318         ovs_be32 ipv4_dst;
319         uint8_t  ipv4_proto;
320         uint8_t  ipv4_tos;
321 };
322
323 struct ovs_key_ipv6 {
324         ovs_be32 ipv6_src[4];
325         ovs_be32 ipv6_dst[4];
326         uint8_t  ipv6_proto;
327         uint8_t  ipv6_tos;
328 };
329
330 struct ovs_key_tcp {
331         ovs_be16 tcp_src;
332         ovs_be16 tcp_dst;
333 };
334
335 struct ovs_key_udp {
336         ovs_be16 udp_src;
337         ovs_be16 udp_dst;
338 };
339
340 struct ovs_key_icmp {
341         uint8_t icmp_type;
342         uint8_t icmp_code;
343 };
344
345 struct ovs_key_icmpv6 {
346         uint8_t icmpv6_type;
347         uint8_t icmpv6_code;
348 };
349
350 struct ovs_key_arp {
351         ovs_be32 arp_sip;
352         ovs_be32 arp_tip;
353         ovs_be16 arp_op;
354         uint8_t  arp_sha[6];
355         uint8_t  arp_tha[6];
356 };
357
358 struct ovs_key_nd {
359         uint32_t nd_target[4];
360         uint8_t  nd_sll[6];
361         uint8_t  nd_tll[6];
362 };
363
364 /**
365  * enum ovs_flow_attr - attributes for %OVS_FLOW_* commands.
366  * @OVS_FLOW_ATTR_KEY: Nested %OVS_KEY_ATTR_* attributes specifying the flow
367  * key.  Always present in notifications.  Required for all requests (except
368  * dumps).
369  * @OVS_FLOW_ATTR_ACTIONS: Nested %OVS_ACTION_ATTR_* attributes specifying
370  * the actions to take for packets that match the key.  Always present in
371  * notifications.  Required for %OVS_FLOW_CMD_NEW requests, optional
372  * on %OVS_FLOW_CMD_SET request to change the existing actions, ignored for
373  * other requests.
374  * @OVS_FLOW_ATTR_STATS: &struct ovs_flow_stats giving statistics for this
375  * flow.  Present in notifications if the stats would be nonzero.  Ignored in
376  * requests.
377  * @OVS_FLOW_ATTR_TCP_FLAGS: An 8-bit value giving the OR'd value of all of the
378  * TCP flags seen on packets in this flow.  Only present in notifications for
379  * TCP flows, and only if it would be nonzero.  Ignored in requests.
380  * @OVS_FLOW_ATTR_USED: A 64-bit integer giving the time, in milliseconds on
381  * the system monotonic clock, at which a packet was last processed for this
382  * flow.  Only present in notifications if a packet has been processed for this
383  * flow.  Ignored in requests.
384  * @OVS_FLOW_ATTR_CLEAR: If present in a %OVS_FLOW_CMD_SET request, clears the
385  * last-used time, accumulated TCP flags, and statistics for this flow.
386  * Otherwise ignored in requests.  Never present in notifications.
387  *
388  * These attributes follow the &struct ovs_header within the Generic Netlink
389  * payload for %OVS_FLOW_* commands.
390  */
391 enum ovs_flow_attr {
392         OVS_FLOW_ATTR_UNSPEC,
393         OVS_FLOW_ATTR_KEY,       /* Sequence of OVS_KEY_ATTR_* attributes. */
394         OVS_FLOW_ATTR_ACTIONS,   /* Nested OVS_ACTION_ATTR_* attributes. */
395         OVS_FLOW_ATTR_STATS,     /* struct ovs_flow_stats. */
396         OVS_FLOW_ATTR_TCP_FLAGS, /* 8-bit OR'd TCP flags. */
397         OVS_FLOW_ATTR_USED,      /* u64 msecs last used in monotonic time. */
398         OVS_FLOW_ATTR_CLEAR,     /* Flag to clear stats, tcp_flags, used. */
399         __OVS_FLOW_ATTR_MAX
400 };
401
402 #define OVS_FLOW_ATTR_MAX (__OVS_FLOW_ATTR_MAX - 1)
403
404 /* Action types. */
405 enum ovs_action_type {
406         OVS_ACTION_ATTR_UNSPEC,
407         OVS_ACTION_ATTR_OUTPUT,       /* Output to switch port. */
408         OVS_ACTION_ATTR_USERSPACE,    /* Send copy to userspace. */
409         OVS_ACTION_ATTR_PUSH_VLAN,    /* Set the 802.1q TCI value. */
410         OVS_ACTION_ATTR_POP_VLAN,     /* Strip the 802.1q header. */
411         OVS_ACTION_ATTR_SET_DL_SRC,   /* Ethernet source address. */
412         OVS_ACTION_ATTR_SET_DL_DST,   /* Ethernet destination address. */
413         OVS_ACTION_ATTR_SET_NW_SRC,   /* IPv4 source address. */
414         OVS_ACTION_ATTR_SET_NW_DST,   /* IPv4 destination address. */
415         OVS_ACTION_ATTR_SET_NW_TOS,   /* IP ToS/DSCP field (6 bits). */
416         OVS_ACTION_ATTR_SET_TP_SRC,   /* TCP/UDP source port. */
417         OVS_ACTION_ATTR_SET_TP_DST,   /* TCP/UDP destination port. */
418         OVS_ACTION_ATTR_SET_TUNNEL,   /* Set the encapsulating tunnel ID. */
419         OVS_ACTION_ATTR_SET_PRIORITY, /* Set skb->priority. */
420         OVS_ACTION_ATTR_POP_PRIORITY, /* Restore original skb->priority. */
421         __OVS_ACTION_ATTR_MAX
422 };
423
424 #define OVS_ACTION_ATTR_MAX (__OVS_ACTION_ATTR_MAX - 1)
425
426 #endif  /* openvswitch/datapath-protocol.h */