d2432e89da2ceacf34eaf7dd390196ab8f40d8c7
[sliver-openvswitch.git] / datapath / table.c
1 /*
2  * Copyright (c) 2009, 2010 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 #include "flow.h"
10 #include "datapath.h"
11 #include "table.h"
12
13 #include <linux/gfp.h>
14 #include <linux/slab.h>
15 #include <linux/mm.h>
16 #include <asm/pgtable.h>
17
18 /**
19  * struct tbl_bucket - single bucket within a hash table
20  * @rcu: RCU callback structure
21  * @n_objs: number of objects in @objs[] array
22  * @objs: array of @n_objs pointers to table nodes contained inside objects
23  *
24  * The expected number of objects per bucket is 1, but this allows for an
25  * arbitrary number of collisions.
26  */
27 struct tbl_bucket {
28         struct rcu_head rcu;
29         unsigned int n_objs;
30         struct tbl_node *objs[];
31 };
32
33 static inline int bucket_size(int n_objs)
34 {
35         return sizeof(struct tbl_bucket) + sizeof(struct tbl_node *) * n_objs;
36 }
37
38 static struct tbl_bucket *bucket_alloc(int n_objs)
39 {
40         return kmalloc(bucket_size(n_objs), GFP_KERNEL);
41 }
42
43 static void free_buckets(struct tbl_bucket ***l1, unsigned int n_buckets,
44                          void (*free_obj)(struct tbl_node *))
45 {
46         unsigned int i;
47
48         for (i = 0; i < n_buckets >> TBL_L1_SHIFT; i++) {
49                 struct tbl_bucket **l2 = l1[i];
50                 unsigned int j;
51
52                 for (j = 0; j < TBL_L2_SIZE; j++) {
53                         struct tbl_bucket *bucket = l2[j];
54                         if (!bucket)
55                                 continue;
56
57                         if (free_obj) {
58                                 unsigned int k;
59                                 for (k = 0; k < bucket->n_objs; k++)
60                                         free_obj(bucket->objs[k]);
61                         }
62                         kfree(bucket);
63                 }
64                 free_page((unsigned long)l2);
65         }
66         kfree(l1);
67 }
68
69 static struct tbl_bucket ***alloc_buckets(unsigned int n_buckets)
70 {
71         struct tbl_bucket ***l1;
72         unsigned int i;
73
74         l1 = kmalloc((n_buckets >> TBL_L1_SHIFT) * sizeof(struct tbl_bucket **),
75                      GFP_KERNEL);
76         if (!l1)
77                 return NULL;
78         for (i = 0; i < n_buckets >> TBL_L1_SHIFT; i++) {
79                 l1[i] = (struct tbl_bucket **)get_zeroed_page(GFP_KERNEL);
80                 if (!l1[i]) {
81                         free_buckets(l1, i << TBL_L1_SHIFT, NULL);
82                         return NULL;
83                 }
84         }
85         return l1;
86 }
87
88 /**
89  * tbl_create - create and return a new hash table
90  * @n_buckets: number of buckets in the new table
91  *
92  * Creates and returns a new hash table, or %NULL if memory cannot be
93  * allocated.  @n_buckets must be a power of 2 in the range %TBL_MIN_BUCKETS to
94  * %TBL_MAX_BUCKETS.
95  */
96 struct tbl *tbl_create(unsigned int n_buckets)
97 {
98         struct tbl *table;
99
100         table = kzalloc(sizeof *table, GFP_KERNEL);
101         if (!table)
102                 goto err;
103
104         table->n_buckets = n_buckets;
105         table->buckets = alloc_buckets(n_buckets);
106         if (!table->buckets)
107                 goto err_free_table;
108
109         return table;
110
111 err_free_table:
112         kfree(table);
113 err:
114         return NULL;
115 }
116
117 /**
118  * tbl_destroy - destroy hash table and optionally the objects it contains
119  * @table: table to destroy
120  * @destructor: function to be called on objects at destruction time
121  *
122  * If a destructor is null, then the buckets in @table are destroyed
123  * but not the objects within those buckets.  This behavior is useful when a
124  * table is being replaced by a larger or smaller one without destroying the
125  * objects.
126  *
127  * If a destructor is not null, then it is called on the objects in @table
128  * before destroying the buckets.
129  */
130 void tbl_destroy(struct tbl *table, void (*destructor)(struct tbl_node *))
131 {
132         if (!table)
133                 return;
134
135         free_buckets(table->buckets, table->n_buckets, destructor);
136         kfree(table);
137 }
138
139 static void destroy_table_rcu(struct rcu_head *rcu)
140 {
141         struct tbl *table = container_of(rcu, struct tbl, rcu);
142         tbl_destroy(table, table->obj_destructor);
143 }
144
145 /**
146  * tbl_deferred_destroy - destroy table after a RCU grace period
147  * @table: table to destroy
148  * @destructor: function to be called on objects at destruction time
149  *
150  * Calls tbl_destroy() on @table after an RCU grace period. If @destructor is
151  * not null it is called on every element before the table is destroyed. */
152 void tbl_deferred_destroy(struct tbl *table, void (*destructor)(struct tbl_node *))
153 {
154         if (!table)
155                 return;
156
157         table->obj_destructor = destructor;
158         call_rcu(&table->rcu, destroy_table_rcu);
159 }
160
161 static struct tbl_bucket __rcu **find_bucket(struct tbl *table, u32 hash)
162 {
163         unsigned int l1 = (hash & (table->n_buckets - 1)) >> TBL_L1_SHIFT;
164         unsigned int l2 = hash & ((1 << TBL_L2_BITS) - 1);
165         return &table->buckets[l1][l2];
166 }
167
168 static int search_bucket(const struct tbl_bucket *bucket, void *target, u32 hash,
169                          int (*cmp)(const struct tbl_node *, void *))
170 {
171         int i;
172
173         for (i = 0; i < bucket->n_objs; i++) {
174                 struct tbl_node *obj = bucket->objs[i];
175                 if (obj->hash == hash && likely(cmp(obj, target)))
176                         return i;
177         }
178
179         return -1;
180 }
181
182 /**
183  * tbl_lookup - searches hash table for a matching object
184  * @table: hash table to search
185  * @target: identifier for the object that is being searched for, will be
186  * provided as an argument to @cmp when making comparisions
187  * @hash: hash of @target
188  * @cmp: comparision function to match objects with the given hash, returns
189  * nonzero if the objects match, zero otherwise
190  *
191  * Searches @table for an object identified by @target.  Returns the tbl_node
192  * contained in the object if successful, otherwise %NULL.
193  */
194 struct tbl_node *tbl_lookup(struct tbl *table, void *target, u32 hash,
195                             int (*cmp)(const struct tbl_node *, void *))
196 {
197         struct tbl_bucket __rcu **bucketp = find_bucket(table, hash);
198         struct tbl_bucket *bucket = rcu_dereference(*bucketp);
199         int index;
200
201         if (!bucket)
202                 return NULL;
203
204         index = search_bucket(bucket, target, hash, cmp);
205         if (index < 0)
206                 return NULL;
207
208         return bucket->objs[index];
209 }
210
211 /**
212  * tbl_foreach - iterate through hash table
213  * @table: table to iterate
214  * @callback: function to call for each entry
215  * @aux: Extra data to pass to @callback
216  *
217  * Iterates through all of the objects in @table in hash order, passing each of
218  * them in turn to @callback.  If @callback returns nonzero, this terminates
219  * the iteration and tbl_foreach() returns the same value.  Returns 0 if
220  * @callback never returns nonzero.
221  *
222  * This function does not try to intelligently handle the case where @callback
223  * adds or removes flows in @table.
224  */
225 int tbl_foreach(struct tbl *table,
226                 int (*callback)(struct tbl_node *, void *aux), void *aux)
227 {
228         unsigned int n_l1 = table->n_buckets >> TBL_L1_SHIFT;
229         unsigned int l1_idx;
230
231         for (l1_idx = 0; l1_idx < n_l1; l1_idx++) {
232                 struct tbl_bucket __rcu **l2 = table->buckets[l1_idx];
233                 unsigned int l2_idx;
234
235                 for (l2_idx = 0; l2_idx < TBL_L2_SIZE; l2_idx++) {
236                         struct tbl_bucket *bucket;
237                         unsigned int i;
238
239                         bucket = rcu_dereference(l2[l2_idx]);
240                         if (!bucket)
241                                 continue;
242
243                         for (i = 0; i < bucket->n_objs; i++) {
244                                 int error = (*callback)(bucket->objs[i], aux);
245                                 if (error)
246                                         return error;
247                         }
248                 }
249         }
250         return 0;
251 }
252
253 static int insert_table_flow(struct tbl_node *node, void *new_table_)
254 {
255         struct tbl *new_table = new_table_;
256         return tbl_insert(new_table, node, node->hash);
257 }
258
259 /**
260  * tbl_expand - create a hash table with more buckets
261  * @table: table to expand
262  *
263  * Creates a new table containing the same objects as @table but with twice
264  * as many buckets.  Returns 0 if successful, otherwise a negative error.  The
265  * caller should free @table upon success (probably using
266  * tbl_deferred_destroy()).
267  */
268 struct tbl *tbl_expand(struct tbl *table)
269 {
270         int err;
271         int n_buckets = table->n_buckets * 2;
272         struct tbl *new_table;
273
274         if (n_buckets >= TBL_MAX_BUCKETS) {
275                 err = -ENOSPC;
276                 goto error;
277         }
278
279         err = -ENOMEM;
280         new_table = tbl_create(TBL_MIN_BUCKETS);
281         if (!new_table)
282                 goto error;
283
284         if (tbl_foreach(table, insert_table_flow, new_table))
285                 goto error_free_new_table;
286
287         return new_table;
288
289 error_free_new_table:
290         tbl_destroy(new_table, NULL);
291 error:
292         return ERR_PTR(err);
293 }
294
295 /**
296  * tbl_n_buckets - returns the number of buckets
297  * @table: table to examine
298  *
299  * Returns the number of buckets currently allocated in @table, useful when
300  * deciding whether to expand.
301  */
302 int tbl_n_buckets(struct tbl *table)
303 {
304         return table->n_buckets;
305 }
306
307 static void free_bucket_rcu(struct rcu_head *rcu)
308 {
309         struct tbl_bucket *bucket = container_of(rcu, struct tbl_bucket, rcu);
310         kfree(bucket);
311 }
312
313 /**
314  * tbl_insert - insert object into table
315  * @table: table in which to insert object
316  * @target: tbl_node contained in object to insert
317  * @hash: hash of object to insert
318  *
319  * The caller must ensure that no object considered to be identical to @target
320  * already exists in @table.  Returns 0 or a negative error (currently just
321  * -ENOMEM).
322  */
323 int tbl_insert(struct tbl *table, struct tbl_node *target, u32 hash)
324 {
325         struct tbl_bucket __rcu **oldp = find_bucket(table, hash);
326         struct tbl_bucket *old = rcu_dereference(*oldp);
327         unsigned int n = old ? old->n_objs : 0;
328         struct tbl_bucket *new = bucket_alloc(n + 1);
329
330         if (!new)
331                 return -ENOMEM;
332
333         target->hash = hash;
334
335         new->n_objs = n + 1;
336         if (old)
337                 memcpy(new->objs, old->objs, n * sizeof(struct tbl_node *));
338         new->objs[n] = target;
339
340         rcu_assign_pointer(*oldp, new);
341         if (old)
342                 call_rcu(&old->rcu, free_bucket_rcu);
343
344         table->count++;
345
346         return 0;
347 }
348
349 /**
350  * tbl_remove - remove object from table
351  * @table: table from which to remove object
352  * @target: tbl_node inside of object to remove
353  *
354  * The caller must ensure that @target itself is in @table.  (It is not
355  * good enough for @table to contain a different object considered identical
356  * @target.)
357  *
358  * Returns 0 or a negative error (currently just -ENOMEM).  Yes, it *is*
359  * possible for object deletion to fail due to lack of memory.
360  */
361 int tbl_remove(struct tbl *table, struct tbl_node *target)
362 {
363         struct tbl_bucket __rcu **oldp = find_bucket(table, target->hash);
364         struct tbl_bucket *old = rcu_dereference(*oldp);
365         unsigned int n = old->n_objs;
366         struct tbl_bucket *new;
367
368         if (n > 1) {
369                 unsigned int i;
370
371                 new = bucket_alloc(n - 1);
372                 if (!new)
373                         return -ENOMEM;
374
375                 new->n_objs = 0;
376                 for (i = 0; i < n; i++) {
377                         struct tbl_node *obj = old->objs[i];
378                         if (obj != target)
379                                 new->objs[new->n_objs++] = obj;
380                 }
381                 WARN_ON_ONCE(new->n_objs != n - 1);
382         } else {
383                 new = NULL;
384         }
385
386         rcu_assign_pointer(*oldp, new);
387         call_rcu(&old->rcu, free_bucket_rcu);
388
389         table->count--;
390
391         return 0;
392 }
393
394 /**
395  * tbl_count - retrieves the number of stored objects
396  * @table: table to count
397  *
398  * Returns the number of objects that have been inserted into the hash table.
399  */
400 unsigned int tbl_count(struct tbl *table)
401 {
402         return table->count;
403 }