datapath: Use hash table more tolerant of collisions for flow table.
[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/netlink.h>
18 #include <linux/netdevice.h>
19 #include <linux/workqueue.h>
20 #include <linux/skbuff.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
28 #define DP_MAX_PORTS 256
29 #define DP_MAX_GROUPS 16
30
31 #define DP_L2_BITS (PAGE_SHIFT - ilog2(sizeof(struct dp_bucket*)))
32 #define DP_L2_SIZE (1 << DP_L2_BITS)
33 #define DP_L2_SHIFT 0
34
35 #define DP_L1_BITS (PAGE_SHIFT - ilog2(sizeof(struct dp_bucket**)))
36 #define DP_L1_SIZE (1 << DP_L1_BITS)
37 #define DP_L1_SHIFT DP_L2_BITS
38
39 /* For 4 kB pages, this is 1,048,576 on 32-bit or 262,144 on 64-bit. */
40 #define DP_MAX_BUCKETS (DP_L1_SIZE * DP_L2_SIZE)
41
42 /**
43  * struct dp_table - flow table
44  * @n_buckets: number of buckets (a power of 2 between %DP_L1_SIZE and
45  * %DP_MAX_BUCKETS)
46  * @buckets: pointer to @n_buckets/%DP_L1_SIZE pointers to %DP_L1_SIZE pointers
47  * to buckets
48  * @hash_seed: random number used for flow hashing, to make the hash
49  * distribution harder to predict
50  * @rcu: RCU callback structure
51  *
52  * The @buckets array is logically an array of pointers to buckets.  It is
53  * broken into two levels to avoid the need to kmalloc() any object larger than
54  * a single page or to use vmalloc().  @buckets is always nonnull, as is each
55  * @buckets[i], but each @buckets[i][j] is nonnull only if the specified hash
56  * bucket is nonempty (for 0 <= i < @n_buckets/%DP_L1_SIZE, 0 <= j <
57  * %DP_L1_SIZE).
58  */
59 struct dp_table {
60         unsigned int n_buckets;
61         struct dp_bucket ***buckets;
62         unsigned int hash_seed;
63         struct rcu_head rcu;
64 };
65
66 /**
67  * struct dp_bucket - single bucket within datapath flow table
68  * @rcu: RCU callback structure
69  * @n_flows: number of flows in @flows[] array
70  * @flows: array of @n_flows pointers to flows
71  *
72  * The expected number of flows per bucket is 1, but this allows for an
73  * arbitrary number of collisions.
74  */
75 struct dp_bucket {
76         struct rcu_head rcu;
77         unsigned int n_flows;
78         struct sw_flow *flows[];
79 };
80
81 #define DP_N_QUEUES 2
82 #define DP_MAX_QUEUE_LEN 100
83
84 struct dp_stats_percpu {
85         u64 n_frags;
86         u64 n_hit;
87         u64 n_missed;
88         u64 n_lost;
89 };
90
91 struct dp_port_group {
92         struct rcu_head rcu;
93         int n_ports;
94         u16 ports[];
95 };
96
97 struct datapath {
98         struct mutex mutex;
99         int dp_idx;
100
101         struct kobject ifobj;
102
103         int drop_frags;
104
105         /* Queued data. */
106         struct sk_buff_head queues[DP_N_QUEUES];
107         wait_queue_head_t waitqueue;
108
109         /* Flow table. */
110         unsigned int n_flows;
111         struct dp_table *table;
112
113         /* Port groups. */
114         struct dp_port_group *groups[DP_MAX_GROUPS];
115
116         /* Switch ports. */
117         unsigned int n_ports;
118         struct net_bridge_port *ports[DP_MAX_PORTS];
119         struct list_head port_list; /* All ports, including local_port. */
120
121         /* Stats. */
122         struct dp_stats_percpu *stats_percpu;
123 };
124
125 struct net_bridge_port {
126         u16 port_no;
127         struct datapath *dp;
128         struct net_device *dev;
129         struct kobject kobj;
130         char linkname[IFNAMSIZ];
131         struct list_head node;   /* Element in datapath.ports. */
132 };
133
134 extern struct notifier_block dp_device_notifier;
135 extern int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
136
137 /* Flow table. */
138 struct dp_table *dp_table_create(unsigned int n_buckets);
139 void dp_table_destroy(struct dp_table *, int free_flows);
140 struct sw_flow *dp_table_lookup(struct dp_table *, const struct odp_flow_key *);
141 int dp_table_insert(struct dp_table *, struct sw_flow *);
142 int dp_table_delete(struct dp_table *, struct sw_flow *);
143 int dp_table_expand(struct datapath *);
144 int dp_table_flush(struct datapath *);
145 int dp_table_foreach(struct dp_table *table,
146                      int (*callback)(struct sw_flow *flow, void *aux),
147                      void *aux);
148
149 void dp_process_received_packet(struct sk_buff *, struct net_bridge_port *);
150 int dp_del_port(struct net_bridge_port *);
151 int dp_output_control(struct datapath *, struct sk_buff *, int, u32 arg);
152 int dp_min_mtu(const struct datapath *dp);
153
154 struct datapath *get_dp(int dp_idx);
155
156 static inline const char *dp_name(const struct datapath *dp)
157 {
158         return dp->ports[ODPP_LOCAL]->dev->name;
159 }
160
161 #ifdef CONFIG_XEN
162 int skb_checksum_setup(struct sk_buff *skb);
163 #else
164 static inline int skb_checksum_setup(struct sk_buff *skb)
165 {
166         return 0;
167 }
168 #endif
169
170 #endif /* datapath.h */