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. We can igonre the priority
101 * argument, since all exact-match entries are the same (highest)
103 static int table_hash_delete(struct sw_table *swt,
104 const struct sw_flow_key *key,
105 uint16_t priority, int strict)
107 struct sw_table_hash *th = (struct sw_table_hash *) swt;
108 unsigned int count = 0;
110 if (key->wildcards == 0) {
111 struct sw_flow **bucket = find_bucket(swt, key);
112 struct sw_flow *flow = *bucket;
113 if (flow && !memcmp(&flow->key, key, sizeof *key)) {
120 for (i = 0; i <= th->bucket_mask; i++) {
121 struct sw_flow **bucket = &th->buckets[i];
122 struct sw_flow *flow = *bucket;
123 if (flow && flow_del_matches(&flow->key, key, strict)) {
129 th->n_flows -= count;
133 static void table_hash_timeout(struct sw_table *swt, struct list *deleted)
135 struct sw_table_hash *th = (struct sw_table_hash *) swt;
138 for (i = 0; i <= th->bucket_mask; i++) {
139 struct sw_flow **bucket = &th->buckets[i];
140 struct sw_flow *flow = *bucket;
141 if (flow && flow_timeout(flow)) {
142 list_push_back(deleted, &flow->node);
149 static void table_hash_destroy(struct sw_table *swt)
151 struct sw_table_hash *th = (struct sw_table_hash *) swt;
153 for (i = 0; i <= th->bucket_mask; i++) {
154 if (th->buckets[i]) {
155 flow_free(th->buckets[i]);
162 struct swt_iterator_hash {
163 struct sw_table_hash *th;
164 unsigned int bucket_i;
167 static struct sw_flow *next_flow(struct swt_iterator_hash *ih)
169 for (;ih->bucket_i <= ih->th->bucket_mask; ih->bucket_i++) {
170 struct sw_flow *f = ih->th->buckets[ih->bucket_i];
178 static int table_hash_iterator(struct sw_table *swt,
179 struct swt_iterator *swt_iter)
181 struct swt_iterator_hash *ih;
183 swt_iter->private = ih = malloc(sizeof *ih);
188 ih->th = (struct sw_table_hash *) swt;
191 swt_iter->flow = next_flow(ih);
196 static void table_hash_next(struct swt_iterator *swt_iter)
198 struct swt_iterator_hash *ih;
200 if (swt_iter->flow == NULL)
203 ih = (struct swt_iterator_hash *) swt_iter->private;
206 swt_iter->flow = next_flow(ih);
209 static void table_hash_iterator_destroy(struct swt_iterator *swt_iter)
211 free(swt_iter->private);
214 static void table_hash_stats(struct sw_table *swt,
215 struct sw_table_stats *stats)
217 struct sw_table_hash *th = (struct sw_table_hash *) swt;
218 stats->name = "hash";
219 stats->n_flows = th->n_flows;
220 stats->max_flows = th->bucket_mask + 1;
223 struct sw_table *table_hash_create(unsigned int polynomial,
224 unsigned int n_buckets)
226 struct sw_table_hash *th;
227 struct sw_table *swt;
229 th = malloc(sizeof *th);
233 assert(!(n_buckets & (n_buckets - 1)));
234 th->buckets = calloc(n_buckets, sizeof *th->buckets);
235 if (th->buckets == NULL) {
236 printf("failed to allocate %u buckets\n", n_buckets);
241 th->bucket_mask = n_buckets - 1;
244 swt->lookup = table_hash_lookup;
245 swt->insert = table_hash_insert;
246 swt->delete = table_hash_delete;
247 swt->timeout = table_hash_timeout;
248 swt->destroy = table_hash_destroy;
249 swt->iterator = table_hash_iterator;
250 swt->iterator_next = table_hash_next;
251 swt->iterator_destroy = table_hash_iterator_destroy;
252 swt->stats = table_hash_stats;
254 crc32_init(&th->crc32, polynomial);
259 /* Double-hashing table. */
261 struct sw_table_hash2 {
263 struct sw_table *subtable[2];
266 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
267 const struct sw_flow_key *key)
269 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
272 for (i = 0; i < 2; i++) {
273 struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
274 if (flow && !memcmp(&flow->key, key, sizeof *key))
280 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
282 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
284 if (table_hash_insert(t2->subtable[0], flow))
286 return table_hash_insert(t2->subtable[1], flow);
289 static int table_hash2_delete(struct sw_table *swt,
290 const struct sw_flow_key *key,
291 uint16_t priority, int strict)
293 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
294 return (table_hash_delete(t2->subtable[0], key, priority, strict)
295 + table_hash_delete(t2->subtable[1], key, priority, strict));
298 static void table_hash2_timeout(struct sw_table *swt, struct list *deleted)
300 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
301 table_hash_timeout(t2->subtable[0], deleted);
302 table_hash_timeout(t2->subtable[1], deleted);
305 static void table_hash2_destroy(struct sw_table *swt)
307 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
308 table_hash_destroy(t2->subtable[0]);
309 table_hash_destroy(t2->subtable[1]);
313 struct swt_iterator_hash2 {
314 struct sw_table_hash2 *th2;
315 struct swt_iterator ih;
319 static int table_hash2_iterator(struct sw_table *swt,
320 struct swt_iterator *swt_iter)
322 struct swt_iterator_hash2 *ih2;
324 swt_iter->private = ih2 = malloc(sizeof *ih2);
328 ih2->th2 = (struct sw_table_hash2 *) swt;
329 if (!table_hash_iterator(ih2->th2->subtable[0], &ih2->ih)) {
334 if (ih2->ih.flow != NULL) {
335 swt_iter->flow = ih2->ih.flow;
338 table_hash_iterator_destroy(&ih2->ih);
340 if (!table_hash_iterator(ih2->th2->subtable[1], &ih2->ih)) {
344 swt_iter->flow = ih2->ih.flow;
350 static void table_hash2_next(struct swt_iterator *swt_iter)
352 struct swt_iterator_hash2 *ih2;
354 if (swt_iter->flow == NULL)
357 ih2 = (struct swt_iterator_hash2 *) swt_iter->private;
358 table_hash_next(&ih2->ih);
360 if (ih2->ih.flow != NULL) {
361 swt_iter->flow = ih2->ih.flow;
363 if (ih2->table_i == 0) {
364 table_hash_iterator_destroy(&ih2->ih);
366 if (!table_hash_iterator(ih2->th2->subtable[1], &ih2->ih)) {
367 ih2->ih.private = NULL;
368 swt_iter->flow = NULL;
370 swt_iter->flow = ih2->ih.flow;
373 swt_iter->flow = NULL;
378 static void table_hash2_iterator_destroy(struct swt_iterator *swt_iter)
380 struct swt_iterator_hash2 *ih2;
382 ih2 = (struct swt_iterator_hash2 *) swt_iter->private;
383 if (ih2->ih.private != NULL)
384 table_hash_iterator_destroy(&ih2->ih);
388 static void table_hash2_stats(struct sw_table *swt,
389 struct sw_table_stats *stats)
391 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
392 struct sw_table_stats substats[2];
395 for (i = 0; i < 2; i++)
396 table_hash_stats(t2->subtable[i], &substats[i]);
397 stats->name = "hash2";
398 stats->n_flows = substats[0].n_flows + substats[1].n_flows;
399 stats->max_flows = substats[0].max_flows + substats[1].max_flows;
402 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
403 unsigned int poly1, unsigned int buckets1)
406 struct sw_table_hash2 *t2;
407 struct sw_table *swt;
409 t2 = malloc(sizeof *t2);
413 t2->subtable[0] = table_hash_create(poly0, buckets0);
414 if (t2->subtable[0] == NULL)
417 t2->subtable[1] = table_hash_create(poly1, buckets1);
418 if (t2->subtable[1] == NULL)
419 goto out_free_subtable0;
422 swt->lookup = table_hash2_lookup;
423 swt->insert = table_hash2_insert;
424 swt->delete = table_hash2_delete;
425 swt->timeout = table_hash2_timeout;
426 swt->destroy = table_hash2_destroy;
427 swt->stats = table_hash2_stats;
429 swt->iterator = table_hash2_iterator;
430 swt->iterator_next = table_hash2_next;
431 swt->iterator_destroy = table_hash2_iterator_destroy;
436 table_hash_destroy(t2->subtable[0]);