Fix null pointer dereference when a delete flow command is executed.
[sliver-openvswitch.git] / 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                         rcu_assign_pointer(*bucket, flow);
64                         flow_deferred_free(old_flow);
65                         retval = 1;
66                 } else {
67                         retval = 0;
68                 }
69         }
70         return retval;
71 }
72
73 static int table_hash_modify(struct sw_table *swt, 
74                 const struct sw_flow_key *key, uint16_t priority, int strict,
75                 const struct ofp_action_header *actions, size_t actions_len) 
76 {
77         struct sw_table_hash *th = (struct sw_table_hash *) swt;
78         unsigned int count = 0;
79
80         if (key->wildcards == 0) {
81                 struct sw_flow **bucket = find_bucket(swt, key);
82                 struct sw_flow *flow = *bucket;
83                 if (flow && flow_matches_desc(&flow->key, key, strict)
84                                 && (!strict || (flow->priority == priority))) {
85                         flow_replace_acts(flow, actions, actions_len);
86                         count = 1;
87                 }
88         } else {
89                 unsigned int i;
90
91                 for (i = 0; i <= th->bucket_mask; i++) {
92                         struct sw_flow **bucket = &th->buckets[i];
93                         struct sw_flow *flow = *bucket;
94                         if (flow && flow_matches_desc(&flow->key, key, strict)
95                                         && (!strict || (flow->priority == priority))) {
96                                 flow_replace_acts(flow, actions, actions_len);
97                                 count++;
98                         }
99                 }
100         }
101         return count;
102 }
103
104 /* Caller must update n_flows. */
105 static int do_delete(struct datapath *dp, struct sw_flow **bucket, 
106                         struct sw_flow *flow, enum nx_flow_end_reason reason)
107 {
108         dp_send_flow_end(dp, flow, reason);
109         rcu_assign_pointer(*bucket, NULL);
110         flow_deferred_free(flow);
111         return 1;
112 }
113
114 /* Returns number of deleted flows.  We can ignore the priority
115  * argument, since all exact-match entries are the same (highest)
116  * priority. */
117 static int table_hash_delete(struct datapath *dp, struct sw_table *swt,
118                                         const struct sw_flow_key *key,  uint16_t out_port, 
119                                         uint16_t priority, int strict)
120 {
121         struct sw_table_hash *th = (struct sw_table_hash *) swt;
122         unsigned int count = 0;
123
124         if (key->wildcards == 0) {
125                 struct sw_flow **bucket = find_bucket(swt, key);
126                 struct sw_flow *flow = *bucket;
127                 if (flow && flow_keys_equal(&flow->key, key)
128                                 && flow_has_out_port(flow, out_port)) 
129                         count = do_delete(dp, bucket, flow, NXFER_DELETE);
130         } else {
131                 unsigned int i;
132
133                 for (i = 0; i <= th->bucket_mask; i++) {
134                         struct sw_flow **bucket = &th->buckets[i];
135                         struct sw_flow *flow = *bucket;
136                         if (flow && flow_matches_desc(&flow->key, key, strict)
137                                         && flow_has_out_port(flow, out_port))
138                                 count = do_delete(dp, bucket, flow, NXFER_DELETE);
139                 }
140         }
141         th->n_flows -= count;
142         return count;
143 }
144
145 static int table_hash_timeout(struct datapath *dp, struct sw_table *swt)
146 {
147         struct sw_table_hash *th = (struct sw_table_hash *) swt;
148         unsigned int i;
149         int count = 0;
150
151         mutex_lock(&dp_mutex);
152         for (i = 0; i <= th->bucket_mask; i++) {
153                 struct sw_flow **bucket = &th->buckets[i];
154                 struct sw_flow *flow = *bucket;
155                 if (flow) {
156                         int reason = flow_timeout(flow);
157                         if (reason >= 0) {
158                                 count += do_delete(dp, bucket, flow, reason); 
159                         }
160                 }
161         }
162         th->n_flows -= count;
163         mutex_unlock(&dp_mutex);
164
165         return count;
166 }
167
168 static void table_hash_destroy(struct sw_table *swt)
169 {
170         struct sw_table_hash *th = (struct sw_table_hash *) swt;
171         unsigned int i;
172         for (i = 0; i <= th->bucket_mask; i++)
173         if (th->buckets[i])
174                 flow_free(th->buckets[i]);
175         kmem_free(th->buckets, (th->bucket_mask + 1) * sizeof *th->buckets);
176         kfree(th);
177 }
178
179 static int table_hash_iterate(struct sw_table *swt,
180                               const struct sw_flow_key *key, uint16_t out_port,
181                               struct sw_table_position *position,
182                               int (*callback)(struct sw_flow *, void *private),
183                               void *private) 
184 {
185         struct sw_table_hash *th = (struct sw_table_hash *) swt;
186
187         if (position->private[0] > th->bucket_mask)
188                 return 0;
189
190         if (key->wildcards == 0) {
191                 struct sw_flow *flow;
192                 int error;
193
194                 flow = table_hash_lookup(swt, key);
195                 if (!flow || !flow_has_out_port(flow, out_port))
196                         return 0;
197
198                 error = callback(flow, private);
199                 if (!error)
200                         position->private[0] = -1;
201                 return error;
202         } else {
203                 int i;
204
205                 for (i = position->private[0]; i <= th->bucket_mask; i++) {
206                         struct sw_flow *flow = th->buckets[i];
207                         if (flow && flow_matches_1wild(&flow->key, key)
208                                         && flow_has_out_port(flow, out_port)) {
209                                 int error = callback(flow, private);
210                                 if (error) {
211                                         position->private[0] = i;
212                                         return error;
213                                 }
214                         }
215                 }
216                 return 0;
217         }
218 }
219 static void table_hash_stats(struct sw_table *swt,
220                                  struct sw_table_stats *stats) 
221 {
222         struct sw_table_hash *th = (struct sw_table_hash *) swt;
223         stats->name = "hash";
224         stats->wildcards = 0;          /* No wildcards are supported. */
225         stats->n_flows   = th->n_flows;
226         stats->max_flows = th->bucket_mask + 1;
227         stats->n_lookup  = swt->n_lookup;
228         stats->n_matched = swt->n_matched;
229 }
230
231 struct sw_table *table_hash_create(unsigned int polynomial,
232                         unsigned int n_buckets)
233 {
234         struct sw_table_hash *th;
235         struct sw_table *swt;
236
237         th = kzalloc(sizeof *th, GFP_KERNEL);
238         if (th == NULL)
239                 return NULL;
240
241         BUG_ON(n_buckets & (n_buckets - 1));
242         th->buckets = kmem_zalloc(n_buckets * sizeof *th->buckets);
243         if (th->buckets == NULL) {
244                 printk("failed to allocate %u buckets\n", n_buckets);
245                 kfree(th);
246                 return NULL;
247         }
248         th->bucket_mask = n_buckets - 1;
249
250         swt = &th->swt;
251         swt->lookup = table_hash_lookup;
252         swt->insert = table_hash_insert;
253         swt->delete = table_hash_delete;
254         swt->timeout = table_hash_timeout;
255         swt->destroy = table_hash_destroy;
256         swt->iterate = table_hash_iterate;
257         swt->stats = table_hash_stats;
258
259         crc32_init(&th->crc32, polynomial);
260         th->n_flows = 0;
261
262         return swt;
263 }
264
265 /* Double-hashing table. */
266
267 struct sw_table_hash2 {
268         struct sw_table swt;
269         struct sw_table *subtable[2];
270 };
271
272 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
273                                                                                   const struct sw_flow_key *key)
274 {
275         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
276         int i;
277         
278         for (i = 0; i < 2; i++) {
279                 struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
280                 if (flow && flow_keys_equal(&flow->key, key))
281                         return flow;
282         }
283         return NULL;
284 }
285
286 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
287 {
288         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
289
290         if (table_hash_insert(t2->subtable[0], flow))
291                 return 1;
292         return table_hash_insert(t2->subtable[1], flow);
293 }
294
295 static int table_hash2_modify(struct sw_table *swt, 
296                 const struct sw_flow_key *key, uint16_t priority, int strict,
297                 const struct ofp_action_header *actions, size_t actions_len)
298 {
299         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
300         return (table_hash_modify(t2->subtable[0], key, priority, strict, 
301                                         actions, actions_len)
302                         + table_hash_modify(t2->subtable[1], key, priority, strict, 
303                                         actions, actions_len));
304 }
305
306 static int table_hash2_delete(struct datapath *dp, struct sw_table *swt,
307                                                           const struct sw_flow_key *key, 
308                                                           uint16_t out_port,
309                                                           uint16_t priority, int strict)
310 {
311         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
312         return (table_hash_delete(dp, t2->subtable[0], key, out_port, 
313                                 priority, strict)
314                         + table_hash_delete(dp, t2->subtable[1], key, out_port, 
315                                 priority, strict));
316 }
317
318 static int table_hash2_timeout(struct datapath *dp, struct sw_table *swt)
319 {
320         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
321         return (table_hash_timeout(dp, t2->subtable[0])
322                         + table_hash_timeout(dp, t2->subtable[1]));
323 }
324
325 static void table_hash2_destroy(struct sw_table *swt)
326 {
327         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
328         table_hash_destroy(t2->subtable[0]);
329         table_hash_destroy(t2->subtable[1]);
330         kfree(t2);
331 }
332
333 static int table_hash2_iterate(struct sw_table *swt,
334                                const struct sw_flow_key *key, uint16_t out_port,
335                                struct sw_table_position *position,
336                                int (*callback)(struct sw_flow *, void *),
337                                void *private)
338 {
339         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
340         int i;
341
342         for (i = position->private[1]; i < 2; i++) {
343                 int error = table_hash_iterate(t2->subtable[i], key, out_port, 
344                                 position, callback, private);
345                 if (error) {
346                         return error;
347                 }
348                 position->private[0] = 0;
349                 position->private[1]++;
350         }
351         return 0;
352 }
353
354 static void table_hash2_stats(struct sw_table *swt,
355                                  struct sw_table_stats *stats)
356 {
357         struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
358         struct sw_table_stats substats[2];
359         int i;
360
361         for (i = 0; i < 2; i++)
362                 table_hash_stats(t2->subtable[i], &substats[i]);
363         stats->name = "hash2";
364         stats->wildcards = 0;          /* No wildcards are supported. */
365         stats->n_flows   = substats[0].n_flows + substats[1].n_flows;
366         stats->max_flows = substats[0].max_flows + substats[1].max_flows;
367         stats->n_lookup  = swt->n_lookup;
368         stats->n_matched = swt->n_matched;
369 }
370
371 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
372                                                                         unsigned int poly1, unsigned int buckets1)
373
374 {
375         struct sw_table_hash2 *t2;
376         struct sw_table *swt;
377
378         t2 = kzalloc(sizeof *t2, GFP_KERNEL);
379         if (t2 == NULL)
380                 return NULL;
381
382         t2->subtable[0] = table_hash_create(poly0, buckets0);
383         if (t2->subtable[0] == NULL)
384                 goto out_free_t2;
385
386         t2->subtable[1] = table_hash_create(poly1, buckets1);
387         if (t2->subtable[1] == NULL)
388                 goto out_free_subtable0;
389
390         swt = &t2->swt;
391         swt->lookup = table_hash2_lookup;
392         swt->insert = table_hash2_insert;
393         swt->modify = table_hash2_modify;
394         swt->delete = table_hash2_delete;
395         swt->timeout = table_hash2_timeout;
396         swt->destroy = table_hash2_destroy;
397         swt->iterate = table_hash2_iterate;
398         swt->stats = table_hash2_stats;
399
400         return swt;
401
402 out_free_subtable0:
403         table_hash_destroy(t2->subtable[0]);
404 out_free_t2:
405         kfree(t2);
406         return NULL;
407 }
408
409 /* From fs/xfs/linux-2.4/kmem.c. */
410
411 static void *
412 kmem_alloc(size_t size)
413 {
414         void *ptr;
415
416 #ifdef KMALLOC_MAX_SIZE
417         if (size > KMALLOC_MAX_SIZE)
418                 return NULL;
419 #endif
420         ptr = kmalloc(size, GFP_KERNEL);
421         if (!ptr) {
422                 ptr = vmalloc(size);
423                 if (ptr)
424                         printk("openflow: used vmalloc for %lu bytes\n", 
425                                         (unsigned long)size);
426         }
427         return ptr;
428 }
429
430 static void *
431 kmem_zalloc(size_t size)
432 {
433         void *ptr = kmem_alloc(size);
434         if (ptr)
435                 memset(ptr, 0, size);
436         return ptr;
437 }
438
439 static void
440 kmem_free(void *ptr, size_t size)
441 {
442         if (((unsigned long)ptr < VMALLOC_START) ||
443                 ((unsigned long)ptr >= VMALLOC_END)) {
444                 kfree(ptr);
445         } else {
446                 vfree(ptr);
447         }
448 }