Make "struct vconn" opaque.
[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, 
35                                 offsetof(struct sw_flow_key, wildcards));
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 && flow_keys_equal(&flow->key, 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         int retval;
51
52         if (flow->key.wildcards != 0)
53                 return 0;
54
55         bucket = find_bucket(swt, &flow->key);
56         if (*bucket == NULL) {
57                 th->n_flows++;
58                 rcu_assign_pointer(*bucket, flow);
59                 retval = 1;
60         } else {
61                 struct sw_flow *old_flow = *bucket;
62                 if (flow_keys_equal(&old_flow->key, &flow->key)) {
63                         /* Keep stats from the original flow */
64                         flow->init_time = old_flow->init_time;
65                         flow->packet_count = old_flow->packet_count;
66                         flow->byte_count = old_flow->byte_count;
67
68                         rcu_assign_pointer(*bucket, flow);
69                         flow_deferred_free(old_flow);
70                         retval = 1;
71                 } else {
72                         retval = 0;
73                 }
74         }
75         return retval;
76 }
77
78 /* Caller must update n_flows. */
79 static int do_delete(struct sw_flow **bucket, struct sw_flow *flow)
80 {
81         rcu_assign_pointer(*bucket, NULL);
82         flow_deferred_free(flow);
83         return 1;
84 }
85
86 /* Returns number of deleted flows.  We can ignore the priority
87  * argument, since all exact-match entries are the same (highest)
88  * priority. */
89 static int table_hash_delete(struct sw_table *swt,
90                                                          const struct sw_flow_key *key, 
91                                                          uint16_t priority, int strict)
92 {
93         struct sw_table_hash *th = (struct sw_table_hash *) swt;
94         unsigned int count = 0;
95
96         if (key->wildcards == 0) {
97                 struct sw_flow **bucket = find_bucket(swt, key);
98                 struct sw_flow *flow = *bucket;
99                 if (flow && flow_keys_equal(&flow->key, key))
100                         count = do_delete(bucket, flow);
101         } else {
102                 unsigned int i;
103
104                 for (i = 0; i <= th->bucket_mask; i++) {
105                         struct sw_flow **bucket = &th->buckets[i];
106                         struct sw_flow *flow = *bucket;
107                         if (flow && flow_del_matches(&flow->key, key, strict))
108                                 count += do_delete(bucket, flow);
109                 }
110         }
111         th->n_flows -= count;
112         return count;
113 }
114
115 static int table_hash_timeout(struct datapath *dp, struct sw_table *swt)
116 {
117         struct sw_table_hash *th = (struct sw_table_hash *) swt;
118         unsigned int i;
119         int count = 0;
120
121         mutex_lock(&dp_mutex);
122         for (i = 0; i <= th->bucket_mask; i++) {
123                 struct sw_flow **bucket = &th->buckets[i];
124                 struct sw_flow *flow = *bucket;
125                 if (flow) {
126                         int reason = flow_timeout(flow);
127                         if (reason >= 0) {
128                                 count += do_delete(bucket, flow); 
129                                 dp_send_flow_expired(dp, flow, reason);
130                         }
131                 }
132         }
133         th->n_flows -= count;
134         mutex_unlock(&dp_mutex);
135
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_1wild(&flow->key, 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->wildcards = 0;          /* No wildcards are supported. */
195         stats->n_flows   = th->n_flows;
196         stats->max_flows = th->bucket_mask + 1;
197         stats->n_matched = swt->n_matched;
198 }
199
200 struct sw_table *table_hash_create(unsigned int polynomial,
201                         unsigned int n_buckets)
202 {
203         struct sw_table_hash *th;
204         struct sw_table *swt;
205
206         th = kzalloc(sizeof *th, GFP_KERNEL);
207         if (th == NULL)
208                 return NULL;
209
210         BUG_ON(n_buckets & (n_buckets - 1));
211         th->buckets = kmem_zalloc(n_buckets * sizeof *th->buckets);
212         if (th->buckets == NULL) {
213                 printk("failed to allocate %u buckets\n", n_buckets);
214                 kfree(th);
215                 return NULL;
216         }
217         th->bucket_mask = n_buckets - 1;
218
219         swt = &th->swt;
220         swt->lookup = table_hash_lookup;
221         swt->insert = table_hash_insert;
222         swt->delete = table_hash_delete;
223         swt->timeout = table_hash_timeout;
224         swt->destroy = table_hash_destroy;
225         swt->iterate = table_hash_iterate;
226         swt->stats = table_hash_stats;
227
228         crc32_init(&th->crc32, polynomial);
229         th->n_flows = 0;
230
231         return swt;
232 }
233
234 /* Double-hashing table. */
235
236 struct sw_table_hash2 {
237         struct sw_table swt;
238         struct sw_table *subtable[2];
239 };
240
241 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
242                                                                                   const struct sw_flow_key *key)
243 {
244         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
245         int i;
246         
247         for (i = 0; i < 2; i++) {
248                 struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
249                 if (flow && flow_keys_equal(&flow->key, key))
250                         return flow;
251         }
252         return NULL;
253 }
254
255 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
256 {
257         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
258
259         if (table_hash_insert(t2->subtable[0], flow))
260                 return 1;
261         return table_hash_insert(t2->subtable[1], flow);
262 }
263
264 static int table_hash2_delete(struct sw_table *swt,
265                                                           const struct sw_flow_key *key, 
266                                                           uint16_t priority, int strict)
267 {
268         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
269         return (table_hash_delete(t2->subtable[0], key, priority, strict)
270                         + table_hash_delete(t2->subtable[1], key, priority, strict));
271 }
272
273 static int table_hash2_timeout(struct datapath *dp, struct sw_table *swt)
274 {
275         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
276         return (table_hash_timeout(dp, t2->subtable[0])
277                         + table_hash_timeout(dp, t2->subtable[1]));
278 }
279
280 static void table_hash2_destroy(struct sw_table *swt)
281 {
282         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
283         table_hash_destroy(t2->subtable[0]);
284         table_hash_destroy(t2->subtable[1]);
285         kfree(t2);
286 }
287
288 static int table_hash2_iterate(struct sw_table *swt,
289                                const struct sw_flow_key *key,
290                                struct sw_table_position *position,
291                                int (*callback)(struct sw_flow *, void *),
292                                void *private)
293 {
294         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
295         int i;
296
297         for (i = position->private[1]; i < 2; i++) {
298                 int error = table_hash_iterate(t2->subtable[i], key, position,
299                                                callback, private);
300                 if (error) {
301                         return error;
302                 }
303                 position->private[0] = 0;
304                 position->private[1]++;
305         }
306         return 0;
307 }
308
309 static void table_hash2_stats(struct sw_table *swt,
310                                  struct sw_table_stats *stats)
311 {
312         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
313         struct sw_table_stats substats[2];
314         int i;
315
316         for (i = 0; i < 2; i++)
317                 table_hash_stats(t2->subtable[i], &substats[i]);
318         stats->name = "hash2";
319         stats->wildcards = 0;          /* No wildcards are supported. */
320         stats->n_flows   = substats[0].n_flows + substats[1].n_flows;
321         stats->max_flows = substats[0].max_flows + substats[1].max_flows;
322         stats->n_matched = swt->n_matched;
323 }
324
325 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
326                                                                         unsigned int poly1, unsigned int buckets1)
327
328 {
329         struct sw_table_hash2 *t2;
330         struct sw_table *swt;
331
332         t2 = kzalloc(sizeof *t2, GFP_KERNEL);
333         if (t2 == NULL)
334                 return NULL;
335
336         t2->subtable[0] = table_hash_create(poly0, buckets0);
337         if (t2->subtable[0] == NULL)
338                 goto out_free_t2;
339
340         t2->subtable[1] = table_hash_create(poly1, buckets1);
341         if (t2->subtable[1] == NULL)
342                 goto out_free_subtable0;
343
344         swt = &t2->swt;
345         swt->lookup = table_hash2_lookup;
346         swt->insert = table_hash2_insert;
347         swt->delete = table_hash2_delete;
348         swt->timeout = table_hash2_timeout;
349         swt->destroy = table_hash2_destroy;
350         swt->iterate = table_hash2_iterate;
351         swt->stats = table_hash2_stats;
352
353         return swt;
354
355 out_free_subtable0:
356         table_hash_destroy(t2->subtable[0]);
357 out_free_t2:
358         kfree(t2);
359         return NULL;
360 }
361
362 /* From fs/xfs/linux-2.4/kmem.c. */
363
364 static void *
365 kmem_alloc(size_t size)
366 {
367         void *ptr;
368
369 #ifdef KMALLOC_MAX_SIZE
370         if (size > KMALLOC_MAX_SIZE)
371                 return NULL;
372 #endif
373         ptr = kmalloc(size, GFP_KERNEL);
374         if (!ptr) {
375                 ptr = vmalloc(size);
376                 if (ptr)
377                         printk("openflow: used vmalloc for %lu bytes\n", 
378                                         (unsigned long)size);
379         }
380         return ptr;
381 }
382
383 static void *
384 kmem_zalloc(size_t size)
385 {
386         void *ptr = kmem_alloc(size);
387         if (ptr)
388                 memset(ptr, 0, size);
389         return ptr;
390 }
391
392 static void
393 kmem_free(void *ptr, size_t size)
394 {
395         if (((unsigned long)ptr < VMALLOC_START) ||
396                 ((unsigned long)ptr >= VMALLOC_END)) {
397                 kfree(ptr);
398         } else {
399                 vfree(ptr);
400         }
401 }