datapath: Free up flow_extract() return value for reporting errors.
[sliver-openvswitch.git] / datapath / datapath.h
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 /* Interface exported by openvswitch_mod. */
10
11 #ifndef DATAPATH_H
12 #define DATAPATH_H 1
13
14 #include <asm/page.h>
15 #include <linux/kernel.h>
16 #include <linux/mutex.h>
17 #include <linux/netdevice.h>
18 #include <linux/workqueue.h>
19 #include <linux/seqlock.h>
20 #include <linux/skbuff.h>
21 #include <linux/version.h>
22 #include "flow.h"
23 #include "dp_sysfs.h"
24
25 struct vport;
26 struct dp_port;
27
28 /* Mask for the priority bits in a vlan header.  If we ever merge upstream
29  * then this should go into include/linux/if_vlan.h. */
30 #define VLAN_PCP_MASK 0xe000
31 #define VLAN_PCP_SHIFT 13
32
33 #define DP_MAX_PORTS 1024
34 #define DP_MAX_GROUPS 16
35
36 #define DP_N_QUEUES 3
37 #define DP_MAX_QUEUE_LEN 100
38
39 /**
40  * struct dp_stats_percpu - per-cpu packet processing statistics for a given
41  * datapath.
42  * @n_frags: Number of IP fragments processed by datapath.
43  * @n_hit: Number of received packets for which a matching flow was found in
44  * the flow table.
45  * @n_miss: Number of received packets that had no matching flow in the flow
46  * table.  The sum of @n_hit and @n_miss is the number of packets that have
47  * been received by the datapath.
48  * @n_lost: Number of received packets that had no matching flow in the flow
49  * table that could not be sent to userspace (normally due to an overflow in
50  * one of the datapath's queues).
51  */
52 struct dp_stats_percpu {
53         u64 n_frags;
54         u64 n_hit;
55         u64 n_missed;
56         u64 n_lost;
57         seqcount_t seqlock;
58 };
59
60 struct dp_port_group {
61         struct rcu_head rcu;
62         int n_ports;
63         u16 ports[];
64 };
65
66 /**
67  * struct datapath - datapath for flow-based packet switching
68  * @mutex: Mutual exclusion for ioctls.
69  * @dp_idx: Datapath number (index into the dps[] array in datapath.c).
70  * @ifobj: Represents /sys/class/net/<devname>/brif.
71  * @drop_frags: Drop all IP fragments if nonzero.
72  * @queues: %DP_N_QUEUES sets of queued packets for userspace to handle.
73  * @waitqueue: Waitqueue, for waiting for new packets in @queues.
74  * @n_flows: Number of flows currently in flow table.
75  * @table: Current flow table (RCU protected).
76  * @groups: Port groups, used by ODPAT_OUTPUT_GROUP action (RCU protected).
77  * @n_ports: Number of ports currently in @ports.
78  * @ports: Map from port number to &struct dp_port.  %ODPP_LOCAL port
79  * always exists, other ports may be %NULL.
80  * @port_list: List of all ports in @ports in arbitrary order.
81  * @stats_percpu: Per-CPU datapath statistics.
82  * @sflow_probability: Number of packets out of UINT_MAX to sample to the
83  * %ODPL_SFLOW queue, e.g. (@sflow_probability/UINT_MAX) is the probability of
84  * sampling a given packet.
85  */
86 struct datapath {
87         struct mutex mutex;
88         int dp_idx;
89         struct kobject ifobj;
90
91         int drop_frags;
92
93         /* Queued data. */
94         struct sk_buff_head queues[DP_N_QUEUES];
95         wait_queue_head_t waitqueue;
96
97         /* Flow table. */
98         struct tbl *table;
99
100         /* Port groups. */
101         struct dp_port_group *groups[DP_MAX_GROUPS];
102
103         /* Switch ports. */
104         unsigned int n_ports;
105         struct dp_port *ports[DP_MAX_PORTS];
106         struct list_head port_list;
107
108         /* Stats. */
109         struct dp_stats_percpu *stats_percpu;
110
111         /* sFlow Sampling */
112         unsigned int sflow_probability;
113 };
114
115 /**
116  * struct dp_port - one port within a datapath
117  * @port_no: Index into @dp's @ports array.
118  * @dp: Datapath to which this port belongs.
119  * @vport: The network device attached to this port.  The contents depends on
120  * the device and should be accessed only through the vport_* functions.
121  * @kobj: Represents /sys/class/net/<devname>/brport.
122  * @linkname: The name of the link from /sys/class/net/<datapath>/brif to this
123  * &struct dp_port.  (We keep this around so that we can delete it if the
124  * device gets renamed.)  Set to the null string when no link exists.
125  * @node: Element in @dp's @port_list.
126  * @sflow_pool: Number of packets that were candidates for sFlow sampling,
127  * regardless of whether they were actually chosen and sent down to userspace.
128  */
129 struct dp_port {
130         u16 port_no;
131         struct datapath *dp;
132         struct vport *vport;
133         struct kobject kobj;
134         char linkname[IFNAMSIZ];
135         struct list_head node;
136         atomic_t sflow_pool;
137 };
138
139 enum csum_type {
140         OVS_CSUM_NONE = 0,
141         OVS_CSUM_UNNECESSARY = 1,
142         OVS_CSUM_COMPLETE = 2,
143         OVS_CSUM_PARTIAL = 3,
144 };
145
146 /**
147  * struct ovs_skb_cb - OVS data in skb CB
148  * @dp_port: The datapath port on which the skb entered the switch.
149  * @ip_summed: Consistently stores L4 checksumming status across different
150  * kernel versions.
151  * @tun_id: ID (in network byte order) of the tunnel that encapsulated this
152  * packet. It is 0 if the packet was not received on a tunnel.
153  * @is_frag: %true if this packet is an IPv4 fragment, %false otherwise.
154  */
155 struct ovs_skb_cb {
156         struct dp_port          *dp_port;
157         enum csum_type          ip_summed;
158         __be32                  tun_id;
159         bool                    is_frag;
160 };
161 #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
162
163 extern struct notifier_block dp_device_notifier;
164 extern int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
165
166 void dp_process_received_packet(struct dp_port *, struct sk_buff *);
167 int dp_detach_port(struct dp_port *, int may_delete);
168 int dp_output_control(struct datapath *, struct sk_buff *, int, u32 arg);
169 int dp_min_mtu(const struct datapath *dp);
170 void set_internal_devs_mtu(const struct datapath *dp);
171
172 struct datapath *get_dp(int dp_idx);
173 const char *dp_name(const struct datapath *dp);
174
175 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
176 int vswitch_skb_checksum_setup(struct sk_buff *skb);
177 #else
178 static inline int vswitch_skb_checksum_setup(struct sk_buff *skb)
179 {
180         return 0;
181 }
182 #endif
183
184 void compute_ip_summed(struct sk_buff *skb, bool xmit);
185 void forward_ip_summed(struct sk_buff *skb);
186
187 #endif /* datapath.h */