Initial implementation of sFlow.
[sliver-openvswitch.git] / datapath / datapath.h
1 /*
2  * Copyright (c) 2009 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 /* Mask for the priority bits in a vlan header.  If we ever merge upstream
25  * then this should go into include/linux/if_vlan.h. */
26 #define VLAN_PCP_MASK 0xe000
27 #define VLAN_PCP_SHIFT 13
28
29 #define DP_MAX_PORTS 1024
30 #define DP_MAX_GROUPS 16
31
32 #define DP_L2_BITS (PAGE_SHIFT - ilog2(sizeof(struct dp_bucket*)))
33 #define DP_L2_SIZE (1 << DP_L2_BITS)
34 #define DP_L2_SHIFT 0
35
36 #define DP_L1_BITS (PAGE_SHIFT - ilog2(sizeof(struct dp_bucket**)))
37 #define DP_L1_SIZE (1 << DP_L1_BITS)
38 #define DP_L1_SHIFT DP_L2_BITS
39
40 /* For 4 kB pages, this is 1,048,576 on 32-bit or 262,144 on 64-bit. */
41 #define DP_MAX_BUCKETS (DP_L1_SIZE * DP_L2_SIZE)
42
43 /**
44  * struct dp_table - flow table
45  * @n_buckets: number of buckets (a power of 2 between %DP_L1_SIZE and
46  * %DP_MAX_BUCKETS)
47  * @buckets: pointer to @n_buckets/%DP_L1_SIZE pointers to %DP_L1_SIZE pointers
48  * to buckets
49  * @hash_seed: random number used for flow hashing, to make the hash
50  * distribution harder to predict
51  * @rcu: RCU callback structure
52  *
53  * The @buckets array is logically an array of pointers to buckets.  It is
54  * broken into two levels to avoid the need to kmalloc() any object larger than
55  * a single page or to use vmalloc().  @buckets is always nonnull, as is each
56  * @buckets[i], but each @buckets[i][j] is nonnull only if the specified hash
57  * bucket is nonempty (for 0 <= i < @n_buckets/%DP_L1_SIZE, 0 <= j <
58  * %DP_L1_SIZE).
59  */
60 struct dp_table {
61         unsigned int n_buckets;
62         struct dp_bucket ***buckets;
63         unsigned int hash_seed;
64         struct rcu_head rcu;
65 };
66
67 /**
68  * struct dp_bucket - single bucket within datapath flow table
69  * @rcu: RCU callback structure
70  * @n_flows: number of flows in @flows[] array
71  * @flows: array of @n_flows pointers to flows
72  *
73  * The expected number of flows per bucket is 1, but this allows for an
74  * arbitrary number of collisions.
75  */
76 struct dp_bucket {
77         struct rcu_head rcu;
78         unsigned int n_flows;
79         struct sw_flow *flows[];
80 };
81
82 #define DP_N_QUEUES 3
83 #define DP_MAX_QUEUE_LEN 100
84
85 struct dp_stats_percpu {
86         u64 n_frags;
87         u64 n_hit;
88         u64 n_missed;
89         u64 n_lost;
90         u64 sflow_pool;         /* Packets that could have been sampled. */
91 };
92
93 struct dp_port_group {
94         struct rcu_head rcu;
95         int n_ports;
96         u16 ports[];
97 };
98
99 /**
100  * struct datapath - datapath for flow-based packet switching
101  * @mutex: Mutual exclusion for ioctls.
102  * @dp_idx: Datapath number (index into the dps[] array in datapath.c).
103  * @ifobj: &struct kobject representing the datapath.
104  * @drop_frags: Drop all IP fragments if nonzero.
105  * @queues: %DP_N_QUEUES sets of queued packets for userspace to handle.
106  * @waitqueue: Waitqueue, for waiting for new packets in @queues.
107  * @n_flows: Number of flows currently in flow table.
108  * @table: Current flow table (RCU protected).
109  * @groups: Port groups, used by ODPAT_OUTPUT_GROUP action (RCU protected).
110  * @n_ports: Number of ports currently in @ports.
111  * @ports: Map from port number to &struct net_bridge_port.  %ODPP_LOCAL port
112  * always exists, other ports may be %NULL.
113  * @port_list: List of all ports in @ports in arbitrary order.
114  * @stats_percpu: Per-CPU datapath statistics.
115  * @sflow_probability: Probability of sampling a packet to the %ODPL_SFLOW
116  * queue, where 0 means never sample, UINT_MAX means always sample, and
117  * other values are intermediate probabilities.
118  */
119 struct datapath {
120         struct mutex mutex;
121         int dp_idx;
122         struct kobject ifobj;
123
124         int drop_frags;
125
126         /* Queued data. */
127         struct sk_buff_head queues[DP_N_QUEUES];
128         wait_queue_head_t waitqueue;
129
130         /* Flow table. */
131         unsigned int n_flows;
132         struct dp_table *table;
133
134         /* Port groups. */
135         struct dp_port_group *groups[DP_MAX_GROUPS];
136
137         /* Switch ports. */
138         unsigned int n_ports;
139         struct net_bridge_port *ports[DP_MAX_PORTS];
140         struct list_head port_list;
141
142         /* Stats. */
143         struct dp_stats_percpu *stats_percpu;
144
145         /* sFlow Sampling */
146         unsigned int sflow_probability;
147 };
148
149 struct net_bridge_port {
150         u16 port_no;
151         struct datapath *dp;
152         struct net_device *dev;
153         struct kobject kobj;
154         char linkname[IFNAMSIZ];
155         struct list_head node;   /* Element in datapath.ports. */
156 };
157
158 extern struct notifier_block dp_device_notifier;
159 extern int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
160
161 /* Flow table. */
162 struct dp_table *dp_table_create(unsigned int n_buckets);
163 void dp_table_destroy(struct dp_table *, int free_flows);
164 struct sw_flow *dp_table_lookup(struct dp_table *, const struct odp_flow_key *);
165 int dp_table_insert(struct dp_table *, struct sw_flow *);
166 int dp_table_delete(struct dp_table *, struct sw_flow *);
167 int dp_table_expand(struct datapath *);
168 int dp_table_flush(struct datapath *);
169 int dp_table_foreach(struct dp_table *table,
170                      int (*callback)(struct sw_flow *flow, void *aux),
171                      void *aux);
172
173 void dp_process_received_packet(struct sk_buff *, struct net_bridge_port *);
174 int dp_del_port(struct net_bridge_port *);
175 int dp_output_control(struct datapath *, struct sk_buff *, int, u32 arg);
176 int dp_min_mtu(const struct datapath *dp);
177
178 struct datapath *get_dp(int dp_idx);
179
180 static inline const char *dp_name(const struct datapath *dp)
181 {
182         return dp->ports[ODPP_LOCAL]->dev->name;
183 }
184
185 #ifdef CONFIG_XEN
186 int skb_checksum_setup(struct sk_buff *skb);
187 #else
188 static inline int skb_checksum_setup(struct sk_buff *skb)
189 {
190         return 0;
191 }
192 #endif
193
194 int vswitch_skb_checksum_setup(struct sk_buff *skb);
195
196 #endif /* datapath.h */