Merge branch 'locking'
[openvswitch] / datapath / table-hash.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008 The Board of Trustees of The Leland 
4  * Stanford Junior University
5  */
6
7 #include "table.h"
8 #include "crc32.h"
9 #include "flow.h"
10 #include "datapath.h"
11
12 #include <linux/slab.h>
13 #include <linux/vmalloc.h>
14 #include <linux/mm.h>
15 #include <linux/highmem.h>
16 #include <asm/pgtable.h>
17
18 static void *kmem_alloc(size_t);
19 static void *kmem_zalloc(size_t);
20 static void kmem_free(void *, size_t);
21
22 struct sw_table_hash {
23         struct sw_table swt;
24         struct crc32 crc32;
25         unsigned int n_flows;
26         unsigned int bucket_mask; /* Number of buckets minus 1. */
27         struct sw_flow **buckets;
28 };
29
30 static struct sw_flow **find_bucket(struct sw_table *swt,
31                                                                         const struct sw_flow_key *key)
32 {
33         struct sw_table_hash *th = (struct sw_table_hash *) swt;
34         unsigned int crc = crc32_calculate(&th->crc32, key, sizeof *key);
35         return &th->buckets[crc & th->bucket_mask];
36 }
37
38 static struct sw_flow *table_hash_lookup(struct sw_table *swt,
39                                                                                  const struct sw_flow_key *key)
40 {
41         struct sw_flow *flow = *find_bucket(swt, key);
42         return flow && !memcmp(&flow->key, key, sizeof *key) ? flow : NULL;
43 }
44
45 static int table_hash_insert(struct sw_table *swt, struct sw_flow *flow)
46 {
47         struct sw_table_hash *th = (struct sw_table_hash *) swt;
48         struct sw_flow **bucket;
49         int retval;
50
51         if (flow->key.wildcards != 0)
52                 return 0;
53
54         bucket = find_bucket(swt, &flow->key);
55         if (*bucket == NULL) {
56                 th->n_flows++;
57                 rcu_assign_pointer(*bucket, flow);
58                 retval = 1;
59         } else {
60                 struct sw_flow *old_flow = *bucket;
61                 if (!memcmp(&old_flow->key, &flow->key, sizeof flow->key)) {
62                         rcu_assign_pointer(*bucket, flow);
63                         flow_deferred_free(old_flow);
64                         retval = 1;
65                 } else {
66                         retval = 0;
67                 }
68         }
69         return retval;
70 }
71
72 /* Caller must update n_flows. */
73 static int do_delete(struct sw_flow **bucket, struct sw_flow *flow)
74 {
75         rcu_assign_pointer(*bucket, NULL);
76         flow_deferred_free(flow);
77         return 1;
78 }
79
80 /* Returns number of deleted flows.  We can ignore the priority
81  * argument, since all exact-match entries are the same (highest)
82  * priority. */
83 static int table_hash_delete(struct sw_table *swt,
84                                                          const struct sw_flow_key *key, 
85                                                          uint16_t priority, int strict)
86 {
87         struct sw_table_hash *th = (struct sw_table_hash *) swt;
88         unsigned int count = 0;
89
90         if (key->wildcards == 0) {
91                 struct sw_flow **bucket = find_bucket(swt, key);
92                 struct sw_flow *flow = *bucket;
93                 if (flow && !memcmp(&flow->key, key, sizeof *key))
94                         count = do_delete(bucket, flow);
95         } else {
96                 unsigned int i;
97
98                 for (i = 0; i <= th->bucket_mask; i++) {
99                         struct sw_flow **bucket = &th->buckets[i];
100                         struct sw_flow *flow = *bucket;
101                         if (flow && flow_del_matches(&flow->key, key, strict))
102                                 count += do_delete(bucket, flow);
103                 }
104         }
105         th->n_flows -= count;
106         return count;
107 }
108
109 static int table_hash_timeout(struct datapath *dp, struct sw_table *swt)
110 {
111         struct sw_table_hash *th = (struct sw_table_hash *) swt;
112         unsigned int i;
113         int count = 0;
114
115         mutex_lock(&dp_mutex);
116         for (i = 0; i <= th->bucket_mask; i++) {
117                 struct sw_flow **bucket = &th->buckets[i];
118                 struct sw_flow *flow = *bucket;
119                 if (flow && flow_timeout(flow)) {
120                         count += do_delete(bucket, flow); 
121                         if (dp->flags & OFPC_SEND_FLOW_EXP)
122                                 dp_send_flow_expired(dp, flow);
123                 }
124         }
125         th->n_flows -= count;
126         mutex_unlock(&dp_mutex);
127
128         return count;
129 }
130
131 static void table_hash_destroy(struct sw_table *swt)
132 {
133         struct sw_table_hash *th = (struct sw_table_hash *) swt;
134         unsigned int i;
135         for (i = 0; i <= th->bucket_mask; i++)
136         if (th->buckets[i])
137                 flow_free(th->buckets[i]);
138         kmem_free(th->buckets, (th->bucket_mask + 1) * sizeof *th->buckets);
139         kfree(th);
140 }
141
142 static int table_hash_iterate(struct sw_table *swt,
143                               const struct sw_flow_key *key,
144                               struct sw_table_position *position,
145                               int (*callback)(struct sw_flow *, void *private),
146                               void *private) 
147 {
148         struct sw_table_hash *th = (struct sw_table_hash *) swt;
149
150         if (position->private[0] > th->bucket_mask)
151                 return 0;
152
153         if (key->wildcards == 0) {
154                 struct sw_flow *flow;
155                 int error;
156
157                 flow = table_hash_lookup(swt, key);
158                 if (!flow)
159                         return 0;
160
161                 error = callback(flow, private);
162                 if (!error)
163                         position->private[0] = -1;
164                 return error;
165         } else {
166                 int i;
167
168                 for (i = position->private[0]; i <= th->bucket_mask; i++) {
169                         struct sw_flow *flow = th->buckets[i];
170                         if (flow && flow_matches(key, &flow->key)) {
171                                 int error = callback(flow, private);
172                                 if (error) {
173                                         position->private[0] = i;
174                                         return error;
175                                 }
176                         }
177                 }
178                 return 0;
179         }
180 }
181 static void table_hash_stats(struct sw_table *swt,
182                                  struct sw_table_stats *stats) 
183 {
184         struct sw_table_hash *th = (struct sw_table_hash *) swt;
185         stats->name = "hash";
186         stats->n_flows = th->n_flows;
187         stats->max_flows = th->bucket_mask + 1;
188 }
189
190 struct sw_table *table_hash_create(unsigned int polynomial,
191                         unsigned int n_buckets)
192 {
193         struct sw_table_hash *th;
194         struct sw_table *swt;
195
196         th = kmalloc(sizeof *th, GFP_KERNEL);
197         if (th == NULL)
198                 return NULL;
199
200         BUG_ON(n_buckets & (n_buckets - 1));
201         th->buckets = kmem_zalloc(n_buckets * sizeof *th->buckets);
202         if (th->buckets == NULL) {
203                 printk("failed to allocate %u buckets\n", n_buckets);
204                 kfree(th);
205                 return NULL;
206         }
207         th->bucket_mask = n_buckets - 1;
208
209         swt = &th->swt;
210         swt->lookup = table_hash_lookup;
211         swt->insert = table_hash_insert;
212         swt->delete = table_hash_delete;
213         swt->timeout = table_hash_timeout;
214         swt->destroy = table_hash_destroy;
215         swt->iterate = table_hash_iterate;
216         swt->stats = table_hash_stats;
217
218         crc32_init(&th->crc32, polynomial);
219         th->n_flows = 0;
220
221         return swt;
222 }
223
224 /* Double-hashing table. */
225
226 struct sw_table_hash2 {
227         struct sw_table swt;
228         struct sw_table *subtable[2];
229 };
230
231 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
232                                                                                   const struct sw_flow_key *key)
233 {
234         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
235         int i;
236         
237         for (i = 0; i < 2; i++) {
238                 struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
239                 if (flow && !memcmp(&flow->key, key, sizeof *key))
240                         return flow;
241         }
242         return NULL;
243 }
244
245 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
246 {
247         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
248
249         if (table_hash_insert(t2->subtable[0], flow))
250                 return 1;
251         return table_hash_insert(t2->subtable[1], flow);
252 }
253
254 static int table_hash2_delete(struct sw_table *swt,
255                                                           const struct sw_flow_key *key, 
256                                                           uint16_t priority, int strict)
257 {
258         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
259         return (table_hash_delete(t2->subtable[0], key, priority, strict)
260                         + table_hash_delete(t2->subtable[1], key, priority, strict));
261 }
262
263 static int table_hash2_timeout(struct datapath *dp, struct sw_table *swt)
264 {
265         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
266         return (table_hash_timeout(dp, t2->subtable[0])
267                         + table_hash_timeout(dp, t2->subtable[1]));
268 }
269
270 static void table_hash2_destroy(struct sw_table *swt)
271 {
272         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
273         table_hash_destroy(t2->subtable[0]);
274         table_hash_destroy(t2->subtable[1]);
275         kfree(t2);
276 }
277
278 static int table_hash2_iterate(struct sw_table *swt,
279                                const struct sw_flow_key *key,
280                                struct sw_table_position *position,
281                                int (*callback)(struct sw_flow *, void *),
282                                void *private)
283 {
284         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
285         int i;
286
287         for (i = position->private[1]; i < 2; i++) {
288                 int error = table_hash_iterate(t2->subtable[i], key, position,
289                                                callback, private);
290                 if (error) {
291                         return error;
292                 }
293                 position->private[0] = 0;
294                 position->private[1]++;
295         }
296         return 0;
297 }
298
299 static void table_hash2_stats(struct sw_table *swt,
300                                  struct sw_table_stats *stats)
301 {
302         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
303         struct sw_table_stats substats[2];
304         int i;
305
306         for (i = 0; i < 2; i++)
307                 table_hash_stats(t2->subtable[i], &substats[i]);
308         stats->name = "hash2";
309         stats->n_flows = substats[0].n_flows + substats[1].n_flows;
310         stats->max_flows = substats[0].max_flows + substats[1].max_flows;
311 }
312
313 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
314                                                                         unsigned int poly1, unsigned int buckets1)
315
316 {
317         struct sw_table_hash2 *t2;
318         struct sw_table *swt;
319
320         t2 = kmalloc(sizeof *t2, GFP_KERNEL);
321         if (t2 == NULL)
322                 return NULL;
323
324         t2->subtable[0] = table_hash_create(poly0, buckets0);
325         if (t2->subtable[0] == NULL)
326                 goto out_free_t2;
327
328         t2->subtable[1] = table_hash_create(poly1, buckets1);
329         if (t2->subtable[1] == NULL)
330                 goto out_free_subtable0;
331
332         swt = &t2->swt;
333         swt->lookup = table_hash2_lookup;
334         swt->insert = table_hash2_insert;
335         swt->delete = table_hash2_delete;
336         swt->timeout = table_hash2_timeout;
337         swt->destroy = table_hash2_destroy;
338         swt->iterate = table_hash2_iterate;
339         swt->stats = table_hash2_stats;
340
341         return swt;
342
343 out_free_subtable0:
344         table_hash_destroy(t2->subtable[0]);
345 out_free_t2:
346         kfree(t2);
347         return NULL;
348 }
349
350 /* From fs/xfs/linux-2.4/kmem.c. */
351
352 static void *
353 kmem_alloc(size_t size)
354 {
355         void *ptr;
356
357 #ifdef KMALLOC_MAX_SIZE
358         if (size > KMALLOC_MAX_SIZE)
359                 return NULL;
360 #endif
361         ptr = kmalloc(size, GFP_KERNEL);
362         if (!ptr) {
363                 ptr = vmalloc(size);
364                 if (ptr)
365                         printk("openflow: used vmalloc for %lu bytes\n", 
366                                         (unsigned long)size);
367         }
368         return ptr;
369 }
370
371 static void *
372 kmem_zalloc(size_t size)
373 {
374         void *ptr = kmem_alloc(size);
375         if (ptr)
376                 memset(ptr, 0, size);
377         return ptr;
378 }
379
380 static void
381 kmem_free(void *ptr, size_t size)
382 {
383         if (((unsigned long)ptr < VMALLOC_START) ||
384                 ((unsigned long)ptr >= VMALLOC_END)) {
385                 kfree(ptr);
386         } else {
387                 vfree(ptr);
388         }
389 }