1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
42 struct sw_table_hash {
46 unsigned int bucket_mask; /* Number of buckets minus 1. */
47 struct sw_flow **buckets;
50 static struct sw_flow **find_bucket(struct sw_table *swt,
51 const struct sw_flow_key *key)
53 struct sw_table_hash *th = (struct sw_table_hash *) swt;
54 unsigned int crc = crc32_calculate(&th->crc32, key, sizeof *key);
55 return &th->buckets[crc & th->bucket_mask];
58 static struct sw_flow *table_hash_lookup(struct sw_table *swt,
59 const struct sw_flow_key *key)
61 struct sw_flow *flow = *find_bucket(swt, key);
62 return flow && !memcmp(&flow->key, key, sizeof *key) ? flow : NULL;
65 static int table_hash_insert(struct sw_table *swt, struct sw_flow *flow)
67 struct sw_table_hash *th = (struct sw_table_hash *) swt;
68 struct sw_flow **bucket;
71 if (flow->key.wildcards != 0)
74 bucket = find_bucket(swt, &flow->key);
75 if (*bucket == NULL) {
80 struct sw_flow *old_flow = *bucket;
81 if (!memcmp(&old_flow->key, &flow->key, sizeof flow->key)) {
92 /* Caller must update n_flows. */
94 do_delete(struct sw_flow **bucket)
100 /* Returns number of deleted flows. */
101 static int table_hash_delete(struct sw_table *swt,
102 const struct sw_flow_key *key, int strict)
104 struct sw_table_hash *th = (struct sw_table_hash *) swt;
105 unsigned int count = 0;
107 if (key->wildcards == 0) {
108 struct sw_flow **bucket = find_bucket(swt, key);
109 struct sw_flow *flow = *bucket;
110 if (flow && !memcmp(&flow->key, key, sizeof *key)) {
117 for (i = 0; i <= th->bucket_mask; i++) {
118 struct sw_flow **bucket = &th->buckets[i];
119 struct sw_flow *flow = *bucket;
120 if (flow && flow_del_matches(&flow->key, key, strict)) {
126 th->n_flows -= count;
130 static void table_hash_timeout(struct sw_table *swt, struct list *deleted)
132 struct sw_table_hash *th = (struct sw_table_hash *) swt;
135 for (i = 0; i <= th->bucket_mask; i++) {
136 struct sw_flow **bucket = &th->buckets[i];
137 struct sw_flow *flow = *bucket;
138 if (flow && flow_timeout(flow)) {
139 list_push_back(deleted, &flow->node);
146 static void table_hash_destroy(struct sw_table *swt)
148 struct sw_table_hash *th = (struct sw_table_hash *) swt;
150 for (i = 0; i <= th->bucket_mask; i++) {
151 if (th->buckets[i]) {
152 flow_free(th->buckets[i]);
159 struct swt_iterator_hash {
160 struct sw_table_hash *th;
161 unsigned int bucket_i;
164 static struct sw_flow *next_flow(struct swt_iterator_hash *ih)
166 for (;ih->bucket_i <= ih->th->bucket_mask; ih->bucket_i++) {
167 struct sw_flow *f = ih->th->buckets[ih->bucket_i];
175 static int table_hash_iterator(struct sw_table *swt,
176 struct swt_iterator *swt_iter)
178 struct swt_iterator_hash *ih;
180 swt_iter->private = ih = malloc(sizeof *ih);
185 ih->th = (struct sw_table_hash *) swt;
188 swt_iter->flow = next_flow(ih);
193 static void table_hash_next(struct swt_iterator *swt_iter)
195 struct swt_iterator_hash *ih;
197 if (swt_iter->flow == NULL)
200 ih = (struct swt_iterator_hash *) swt_iter->private;
203 swt_iter->flow = next_flow(ih);
206 static void table_hash_iterator_destroy(struct swt_iterator *swt_iter)
208 free(swt_iter->private);
211 static void table_hash_stats(struct sw_table *swt,
212 struct sw_table_stats *stats)
214 struct sw_table_hash *th = (struct sw_table_hash *) swt;
215 stats->name = "hash";
216 stats->n_flows = th->n_flows;
217 stats->max_flows = th->bucket_mask + 1;
220 struct sw_table *table_hash_create(unsigned int polynomial,
221 unsigned int n_buckets)
223 struct sw_table_hash *th;
224 struct sw_table *swt;
226 th = malloc(sizeof *th);
230 assert(!(n_buckets & (n_buckets - 1)));
231 th->buckets = calloc(n_buckets, sizeof *th->buckets);
232 if (th->buckets == NULL) {
233 printf("failed to allocate %u buckets\n", n_buckets);
237 th->bucket_mask = n_buckets - 1;
240 swt->lookup = table_hash_lookup;
241 swt->insert = table_hash_insert;
242 swt->delete = table_hash_delete;
243 swt->timeout = table_hash_timeout;
244 swt->destroy = table_hash_destroy;
245 swt->iterator = table_hash_iterator;
246 swt->iterator_next = table_hash_next;
247 swt->iterator_destroy = table_hash_iterator_destroy;
248 swt->stats = table_hash_stats;
250 crc32_init(&th->crc32, polynomial);
255 /* Double-hashing table. */
257 struct sw_table_hash2 {
259 struct sw_table *subtable[2];
262 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
263 const struct sw_flow_key *key)
265 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
268 for (i = 0; i < 2; i++) {
269 struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
270 if (flow && !memcmp(&flow->key, key, sizeof *key))
276 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
278 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
280 if (table_hash_insert(t2->subtable[0], flow))
282 return table_hash_insert(t2->subtable[1], flow);
285 static int table_hash2_delete(struct sw_table *swt,
286 const struct sw_flow_key *key, int strict)
288 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
289 return (table_hash_delete(t2->subtable[0], key, strict)
290 + table_hash_delete(t2->subtable[1], key, strict));
293 static void table_hash2_timeout(struct sw_table *swt, struct list *deleted)
295 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
296 table_hash_timeout(t2->subtable[0], deleted);
297 table_hash_timeout(t2->subtable[1], deleted);
300 static void table_hash2_destroy(struct sw_table *swt)
302 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
303 table_hash_destroy(t2->subtable[0]);
304 table_hash_destroy(t2->subtable[1]);
308 struct swt_iterator_hash2 {
309 struct sw_table_hash2 *th2;
310 struct swt_iterator ih;
314 static int table_hash2_iterator(struct sw_table *swt,
315 struct swt_iterator *swt_iter)
317 struct swt_iterator_hash2 *ih2;
319 swt_iter->private = ih2 = malloc(sizeof *ih2);
323 ih2->th2 = (struct sw_table_hash2 *) swt;
324 if (!table_hash_iterator(ih2->th2->subtable[0], &ih2->ih)) {
329 if (ih2->ih.flow != NULL) {
330 swt_iter->flow = ih2->ih.flow;
333 table_hash_iterator_destroy(&ih2->ih);
335 if (!table_hash_iterator(ih2->th2->subtable[1], &ih2->ih)) {
339 swt_iter->flow = ih2->ih.flow;
345 static void table_hash2_next(struct swt_iterator *swt_iter)
347 struct swt_iterator_hash2 *ih2;
349 if (swt_iter->flow == NULL)
352 ih2 = (struct swt_iterator_hash2 *) swt_iter->private;
353 table_hash_next(&ih2->ih);
355 if (ih2->ih.flow != NULL) {
356 swt_iter->flow = ih2->ih.flow;
358 if (ih2->table_i == 0) {
359 table_hash_iterator_destroy(&ih2->ih);
361 if (!table_hash_iterator(ih2->th2->subtable[1], &ih2->ih)) {
362 ih2->ih.private = NULL;
363 swt_iter->flow = NULL;
365 swt_iter->flow = ih2->ih.flow;
368 swt_iter->flow = NULL;
373 static void table_hash2_iterator_destroy(struct swt_iterator *swt_iter)
375 struct swt_iterator_hash2 *ih2;
377 ih2 = (struct swt_iterator_hash2 *) swt_iter->private;
378 if (ih2->ih.private != NULL)
379 table_hash_iterator_destroy(&ih2->ih);
383 static void table_hash2_stats(struct sw_table *swt,
384 struct sw_table_stats *stats)
386 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
387 struct sw_table_stats substats[2];
390 for (i = 0; i < 2; i++)
391 table_hash_stats(t2->subtable[i], &substats[i]);
392 stats->name = "hash2";
393 stats->n_flows = substats[0].n_flows + substats[1].n_flows;
394 stats->max_flows = substats[0].max_flows + substats[1].max_flows;
397 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
398 unsigned int poly1, unsigned int buckets1)
401 struct sw_table_hash2 *t2;
402 struct sw_table *swt;
404 t2 = malloc(sizeof *t2);
408 t2->subtable[0] = table_hash_create(poly0, buckets0);
409 if (t2->subtable[0] == NULL)
412 t2->subtable[1] = table_hash_create(poly1, buckets1);
413 if (t2->subtable[1] == NULL)
414 goto out_free_subtable0;
417 swt->lookup = table_hash2_lookup;
418 swt->insert = table_hash2_insert;
419 swt->delete = table_hash2_delete;
420 swt->timeout = table_hash2_timeout;
421 swt->destroy = table_hash2_destroy;
422 swt->stats = table_hash2_stats;
424 swt->iterator = table_hash2_iterator;
425 swt->iterator_next = table_hash2_next;
426 swt->iterator_destroy = table_hash2_iterator_destroy;
431 table_hash_destroy(t2->subtable[0]);