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
12 #include <linux/slab.h>
13 #include <linux/vmalloc.h>
15 #include <linux/highmem.h>
16 #include <asm/pgtable.h>
18 static void *kmem_alloc(size_t);
19 static void *kmem_zalloc(size_t);
20 static void kmem_free(void *, size_t);
22 struct sw_table_hash {
26 unsigned int bucket_mask; /* Number of buckets minus 1. */
27 struct sw_flow **buckets;
30 static struct sw_flow **find_bucket(struct sw_table *swt,
31 const struct sw_flow_key *key)
33 struct sw_table_hash *th = (struct sw_table_hash *) swt;
34 unsigned int crc = crc32_calculate(&th->crc32, key, sizeof *key);
35 return &th->buckets[crc & th->bucket_mask];
38 static struct sw_flow *table_hash_lookup(struct sw_table *swt,
39 const struct sw_flow_key *key)
41 struct sw_flow *flow = *find_bucket(swt, key);
42 return flow && !memcmp(&flow->key, key, sizeof *key) ? flow : NULL;
45 static int table_hash_insert(struct sw_table *swt, struct sw_flow *flow)
47 struct sw_table_hash *th = (struct sw_table_hash *) swt;
48 struct sw_flow **bucket;
51 if (flow->key.wildcards != 0)
54 bucket = find_bucket(swt, &flow->key);
55 if (*bucket == NULL) {
57 rcu_assign_pointer(*bucket, flow);
60 struct sw_flow *old_flow = *bucket;
61 if (!memcmp(&old_flow->key, &flow->key, sizeof flow->key)) {
62 rcu_assign_pointer(*bucket, flow);
63 flow_deferred_free(old_flow);
72 /* Caller must update n_flows. */
73 static int do_delete(struct sw_flow **bucket, struct sw_flow *flow)
75 rcu_assign_pointer(*bucket, NULL);
76 flow_deferred_free(flow);
80 /* Returns number of deleted flows. We can ignore the priority
81 * argument, since all exact-match entries are the same (highest)
83 static int table_hash_delete(struct sw_table *swt,
84 const struct sw_flow_key *key,
85 uint16_t priority, int strict)
87 struct sw_table_hash *th = (struct sw_table_hash *) swt;
88 unsigned int count = 0;
90 if (key->wildcards == 0) {
91 struct sw_flow **bucket = find_bucket(swt, key);
92 struct sw_flow *flow = *bucket;
93 if (flow && !memcmp(&flow->key, key, sizeof *key))
94 count = do_delete(bucket, flow);
98 for (i = 0; i <= th->bucket_mask; i++) {
99 struct sw_flow **bucket = &th->buckets[i];
100 struct sw_flow *flow = *bucket;
101 if (flow && flow_del_matches(&flow->key, key, strict))
102 count += do_delete(bucket, flow);
105 th->n_flows -= count;
109 static int table_hash_timeout(struct datapath *dp, struct sw_table *swt)
111 struct sw_table_hash *th = (struct sw_table_hash *) swt;
115 mutex_lock(&dp_mutex);
116 for (i = 0; i <= th->bucket_mask; i++) {
117 struct sw_flow **bucket = &th->buckets[i];
118 struct sw_flow *flow = *bucket;
119 if (flow && flow_timeout(flow)) {
120 count += do_delete(bucket, flow);
121 if (dp->flags & OFPC_SEND_FLOW_EXP)
122 dp_send_flow_expired(dp, flow);
125 th->n_flows -= count;
126 mutex_unlock(&dp_mutex);
131 static void table_hash_destroy(struct sw_table *swt)
133 struct sw_table_hash *th = (struct sw_table_hash *) swt;
135 for (i = 0; i <= th->bucket_mask; i++)
137 flow_free(th->buckets[i]);
138 kmem_free(th->buckets, (th->bucket_mask + 1) * sizeof *th->buckets);
142 static int table_hash_iterate(struct sw_table *swt,
143 const struct sw_flow_key *key,
144 struct sw_table_position *position,
145 int (*callback)(struct sw_flow *, void *private),
148 struct sw_table_hash *th = (struct sw_table_hash *) swt;
150 if (position->private[0] > th->bucket_mask)
153 if (key->wildcards == 0) {
154 struct sw_flow *flow;
157 flow = table_hash_lookup(swt, key);
161 error = callback(flow, private);
163 position->private[0] = -1;
168 for (i = position->private[0]; i <= th->bucket_mask; i++) {
169 struct sw_flow *flow = th->buckets[i];
170 if (flow && flow_matches(key, &flow->key)) {
171 int error = callback(flow, private);
173 position->private[0] = i;
181 static void table_hash_stats(struct sw_table *swt,
182 struct sw_table_stats *stats)
184 struct sw_table_hash *th = (struct sw_table_hash *) swt;
185 stats->name = "hash";
186 stats->n_flows = th->n_flows;
187 stats->max_flows = th->bucket_mask + 1;
188 stats->n_matched = swt->n_matched;
191 struct sw_table *table_hash_create(unsigned int polynomial,
192 unsigned int n_buckets)
194 struct sw_table_hash *th;
195 struct sw_table *swt;
197 th = kzalloc(sizeof *th, GFP_KERNEL);
201 BUG_ON(n_buckets & (n_buckets - 1));
202 th->buckets = kmem_zalloc(n_buckets * sizeof *th->buckets);
203 if (th->buckets == NULL) {
204 printk("failed to allocate %u buckets\n", n_buckets);
208 th->bucket_mask = n_buckets - 1;
211 swt->lookup = table_hash_lookup;
212 swt->insert = table_hash_insert;
213 swt->delete = table_hash_delete;
214 swt->timeout = table_hash_timeout;
215 swt->destroy = table_hash_destroy;
216 swt->iterate = table_hash_iterate;
217 swt->stats = table_hash_stats;
219 crc32_init(&th->crc32, polynomial);
225 /* Double-hashing table. */
227 struct sw_table_hash2 {
229 struct sw_table *subtable[2];
232 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
233 const struct sw_flow_key *key)
235 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
238 for (i = 0; i < 2; i++) {
239 struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
240 if (flow && !memcmp(&flow->key, key, sizeof *key))
246 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
248 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
250 if (table_hash_insert(t2->subtable[0], flow))
252 return table_hash_insert(t2->subtable[1], flow);
255 static int table_hash2_delete(struct sw_table *swt,
256 const struct sw_flow_key *key,
257 uint16_t priority, int strict)
259 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
260 return (table_hash_delete(t2->subtable[0], key, priority, strict)
261 + table_hash_delete(t2->subtable[1], key, priority, strict));
264 static int table_hash2_timeout(struct datapath *dp, struct sw_table *swt)
266 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
267 return (table_hash_timeout(dp, t2->subtable[0])
268 + table_hash_timeout(dp, t2->subtable[1]));
271 static void table_hash2_destroy(struct sw_table *swt)
273 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
274 table_hash_destroy(t2->subtable[0]);
275 table_hash_destroy(t2->subtable[1]);
279 static int table_hash2_iterate(struct sw_table *swt,
280 const struct sw_flow_key *key,
281 struct sw_table_position *position,
282 int (*callback)(struct sw_flow *, void *),
285 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
288 for (i = position->private[1]; i < 2; i++) {
289 int error = table_hash_iterate(t2->subtable[i], key, position,
294 position->private[0] = 0;
295 position->private[1]++;
300 static void table_hash2_stats(struct sw_table *swt,
301 struct sw_table_stats *stats)
303 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
304 struct sw_table_stats substats[2];
307 for (i = 0; i < 2; i++)
308 table_hash_stats(t2->subtable[i], &substats[i]);
309 stats->name = "hash2";
310 stats->n_flows = substats[0].n_flows + substats[1].n_flows;
311 stats->max_flows = substats[0].max_flows + substats[1].max_flows;
312 stats->n_matched = swt->n_matched;
315 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
316 unsigned int poly1, unsigned int buckets1)
319 struct sw_table_hash2 *t2;
320 struct sw_table *swt;
322 t2 = kzalloc(sizeof *t2, GFP_KERNEL);
326 t2->subtable[0] = table_hash_create(poly0, buckets0);
327 if (t2->subtable[0] == NULL)
330 t2->subtable[1] = table_hash_create(poly1, buckets1);
331 if (t2->subtable[1] == NULL)
332 goto out_free_subtable0;
335 swt->lookup = table_hash2_lookup;
336 swt->insert = table_hash2_insert;
337 swt->delete = table_hash2_delete;
338 swt->timeout = table_hash2_timeout;
339 swt->destroy = table_hash2_destroy;
340 swt->iterate = table_hash2_iterate;
341 swt->stats = table_hash2_stats;
346 table_hash_destroy(t2->subtable[0]);
352 /* From fs/xfs/linux-2.4/kmem.c. */
355 kmem_alloc(size_t size)
359 #ifdef KMALLOC_MAX_SIZE
360 if (size > KMALLOC_MAX_SIZE)
363 ptr = kmalloc(size, GFP_KERNEL);
367 printk("openflow: used vmalloc for %lu bytes\n",
368 (unsigned long)size);
374 kmem_zalloc(size_t size)
376 void *ptr = kmem_alloc(size);
378 memset(ptr, 0, size);
383 kmem_free(void *ptr, size_t size)
385 if (((unsigned long)ptr < VMALLOC_START) ||
386 ((unsigned long)ptr >= VMALLOC_END)) {