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