datapath: Add generic virtual port layer.
[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/skbuff.h>
20 #include <linux/version.h>
21 #include "flow.h"
22 #include "dp_sysfs.h"
23
24 struct vport;
25 struct dp_port;
26
27 /* Mask for the priority bits in a vlan header.  If we ever merge upstream
28  * then this should go into include/linux/if_vlan.h. */
29 #define VLAN_PCP_MASK 0xe000
30 #define VLAN_PCP_SHIFT 13
31
32 #define DP_MAX_PORTS 1024
33 #define DP_MAX_GROUPS 16
34
35 #define DP_L2_BITS (PAGE_SHIFT - ilog2(sizeof(struct dp_bucket*)))
36 #define DP_L2_SIZE (1 << DP_L2_BITS)
37 #define DP_L2_SHIFT 0
38
39 #define DP_L1_BITS (PAGE_SHIFT - ilog2(sizeof(struct dp_bucket**)))
40 #define DP_L1_SIZE (1 << DP_L1_BITS)
41 #define DP_L1_SHIFT DP_L2_BITS
42
43 /* For 4 kB pages, this is 1,048,576 on 32-bit or 262,144 on 64-bit. */
44 #define DP_MAX_BUCKETS (DP_L1_SIZE * DP_L2_SIZE)
45
46 /**
47  * struct dp_table - flow table
48  * @n_buckets: number of buckets (a power of 2 between %DP_L1_SIZE and
49  * %DP_MAX_BUCKETS)
50  * @buckets: pointer to @n_buckets/%DP_L1_SIZE pointers to %DP_L1_SIZE pointers
51  * to buckets
52  * @hash_seed: random number used for flow hashing, to make the hash
53  * distribution harder to predict
54  * @rcu: RCU callback structure
55  *
56  * The @buckets array is logically an array of pointers to buckets.  It is
57  * broken into two levels to avoid the need to kmalloc() any object larger than
58  * a single page or to use vmalloc().  @buckets is always nonnull, as is each
59  * @buckets[i], but each @buckets[i][j] is nonnull only if the specified hash
60  * bucket is nonempty (for 0 <= i < @n_buckets/%DP_L1_SIZE, 0 <= j <
61  * %DP_L1_SIZE).
62  */
63 struct dp_table {
64         unsigned int n_buckets;
65         struct dp_bucket ***buckets;
66         unsigned int hash_seed;
67         struct rcu_head rcu;
68 };
69
70 /**
71  * struct dp_bucket - single bucket within datapath flow table
72  * @rcu: RCU callback structure
73  * @n_flows: number of flows in @flows[] array
74  * @flows: array of @n_flows pointers to flows
75  *
76  * The expected number of flows per bucket is 1, but this allows for an
77  * arbitrary number of collisions.
78  */
79 struct dp_bucket {
80         struct rcu_head rcu;
81         unsigned int n_flows;
82         struct sw_flow *flows[];
83 };
84
85 #define DP_N_QUEUES 3
86 #define DP_MAX_QUEUE_LEN 100
87
88 /**
89  * struct dp_stats_percpu - per-cpu packet processing statistics for a given
90  * datapath.
91  * @n_frags: Number of IP fragments processed by datapath.
92  * @n_hit: Number of received packets for which a matching flow was found in
93  * the flow table.
94  * @n_miss: Number of received packets that had no matching flow in the flow
95  * table.  The sum of @n_hit and @n_miss is the number of packets that have
96  * been received by the datapath.
97  * @n_lost: Number of received packets that had no matching flow in the flow
98  * table that could not be sent to userspace (normally due to an overflow in
99  * one of the datapath's queues).
100  */
101 struct dp_stats_percpu {
102         u64 n_frags;
103         u64 n_hit;
104         u64 n_missed;
105         u64 n_lost;
106 };
107
108 struct dp_port_group {
109         struct rcu_head rcu;
110         int n_ports;
111         u16 ports[];
112 };
113
114 /**
115  * struct datapath - datapath for flow-based packet switching
116  * @mutex: Mutual exclusion for ioctls.
117  * @dp_idx: Datapath number (index into the dps[] array in datapath.c).
118  * @ifobj: Represents /sys/class/net/<devname>/brif.
119  * @drop_frags: Drop all IP fragments if nonzero.
120  * @queues: %DP_N_QUEUES sets of queued packets for userspace to handle.
121  * @waitqueue: Waitqueue, for waiting for new packets in @queues.
122  * @n_flows: Number of flows currently in flow table.
123  * @table: Current flow table (RCU protected).
124  * @groups: Port groups, used by ODPAT_OUTPUT_GROUP action (RCU protected).
125  * @n_ports: Number of ports currently in @ports.
126  * @ports: Map from port number to &struct dp_port.  %ODPP_LOCAL port
127  * always exists, other ports may be %NULL.
128  * @port_list: List of all ports in @ports in arbitrary order.
129  * @stats_percpu: Per-CPU datapath statistics.
130  * @sflow_probability: Number of packets out of UINT_MAX to sample to the
131  * %ODPL_SFLOW queue, e.g. (@sflow_probability/UINT_MAX) is the probability of
132  * sampling a given packet.
133  */
134 struct datapath {
135         struct mutex mutex;
136         int dp_idx;
137         struct kobject ifobj;
138
139         int drop_frags;
140
141         /* Queued data. */
142         struct sk_buff_head queues[DP_N_QUEUES];
143         wait_queue_head_t waitqueue;
144
145         /* Flow table. */
146         unsigned int n_flows;
147         struct dp_table *table;
148
149         /* Port groups. */
150         struct dp_port_group *groups[DP_MAX_GROUPS];
151
152         /* Switch ports. */
153         unsigned int n_ports;
154         struct dp_port *ports[DP_MAX_PORTS];
155         struct list_head port_list;
156
157         /* Stats. */
158         struct dp_stats_percpu *stats_percpu;
159
160         /* sFlow Sampling */
161         unsigned int sflow_probability;
162 };
163
164 /**
165  * struct dp_port - one port within a datapath
166  * @port_no: Index into @dp's @ports array.
167  * @dp: Datapath to which this port belongs.
168  * @vport: The network device attached to this port.  The contents depends on
169  * the device and should be accessed only through the vport_* functions.
170  * @kobj: Represents /sys/class/net/<devname>/brport.
171  * @linkname: The name of the link from /sys/class/net/<datapath>/brif to this
172  * &struct dp_port.  (We keep this around so that we can delete it if the
173  * device gets renamed.)  Set to the null string when no link exists.
174  * @node: Element in @dp's @port_list.
175  * @sflow_pool: Number of packets that were candidates for sFlow sampling,
176  * regardless of whether they were actually chosen and sent down to userspace.
177  */
178 struct dp_port {
179         u16 port_no;
180         struct datapath *dp;
181         struct vport *vport;
182         struct kobject kobj;
183         char linkname[IFNAMSIZ];
184         struct list_head node;
185         atomic_t sflow_pool;
186 };
187
188 enum csum_type {
189         OVS_CSUM_NONE = 0,
190         OVS_CSUM_UNNECESSARY = 1,
191         OVS_CSUM_COMPLETE = 2,
192         OVS_CSUM_PARTIAL = 3,
193 };
194
195 /**
196  * struct ovs_skb_cb - OVS data in skb CB
197  * @br_port: The bridge port on which the skb entered the switch.
198  * @ip_summed: Consistently stores L4 checksumming status across different
199  * kernel versions.
200  */
201 struct ovs_skb_cb {
202         struct dp_port          *dp_port;
203         enum csum_type          ip_summed;
204         __be32                  tun_id;
205 };
206 #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
207
208 extern struct notifier_block dp_device_notifier;
209 extern int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
210
211 /* Flow table. */
212 struct dp_table *dp_table_create(unsigned int n_buckets);
213 void dp_table_destroy(struct dp_table *, int free_flows);
214 struct sw_flow *dp_table_lookup(struct dp_table *, const struct odp_flow_key *);
215 int dp_table_insert(struct dp_table *, struct sw_flow *);
216 int dp_table_delete(struct dp_table *, struct sw_flow *);
217 int dp_table_expand(struct datapath *);
218 int dp_table_flush(struct datapath *);
219 int dp_table_foreach(struct dp_table *table,
220                      int (*callback)(struct sw_flow *flow, void *aux),
221                      void *aux);
222
223 void dp_process_received_packet(struct dp_port *, struct sk_buff *);
224 int dp_detach_port(struct dp_port *, int may_delete);
225 int dp_output_control(struct datapath *, struct sk_buff *, int, u32 arg);
226 int dp_min_mtu(const struct datapath *dp);
227 void set_internal_devs_mtu(const struct datapath *dp);
228
229 struct datapath *get_dp(int dp_idx);
230 const char *dp_name(const struct datapath *dp);
231
232 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
233 int vswitch_skb_checksum_setup(struct sk_buff *skb);
234 #else
235 static inline int vswitch_skb_checksum_setup(struct sk_buff *skb)
236 {
237         return 0;
238 }
239 #endif
240
241 void compute_ip_summed(struct sk_buff *skb, bool xmit);
242 void forward_ip_summed(struct sk_buff *skb);
243
244 #endif /* datapath.h */