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