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 int table_hash_timeout(struct datapath *dp, struct sw_table *swt)
132 struct sw_table_hash *th = (struct sw_table_hash *) swt;
136 for (i = 0; i <= th->bucket_mask; i++) {
137 struct sw_flow **bucket = &th->buckets[i];
138 struct sw_flow *flow = *bucket;
139 if (flow && flow_timeout(flow)) {
140 dp_send_flow_expired(dp, flow);
145 th->n_flows -= count;
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);
240 th->bucket_mask = n_buckets - 1;
243 swt->lookup = table_hash_lookup;
244 swt->insert = table_hash_insert;
245 swt->delete = table_hash_delete;
246 swt->timeout = table_hash_timeout;
247 swt->destroy = table_hash_destroy;
248 swt->iterator = table_hash_iterator;
249 swt->iterator_next = table_hash_next;
250 swt->iterator_destroy = table_hash_iterator_destroy;
251 swt->stats = table_hash_stats;
253 crc32_init(&th->crc32, polynomial);
258 /* Double-hashing table. */
260 struct sw_table_hash2 {
262 struct sw_table *subtable[2];
265 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
266 const struct sw_flow_key *key)
268 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
271 for (i = 0; i < 2; i++) {
272 struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
273 if (flow && !memcmp(&flow->key, key, sizeof *key))
279 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
281 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
283 if (table_hash_insert(t2->subtable[0], flow))
285 return table_hash_insert(t2->subtable[1], flow);
288 static int table_hash2_delete(struct sw_table *swt,
289 const struct sw_flow_key *key, int strict)
291 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
292 return (table_hash_delete(t2->subtable[0], key, strict)
293 + table_hash_delete(t2->subtable[1], key, strict));
296 static int table_hash2_timeout(struct datapath *dp, struct sw_table *swt)
298 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
299 return (table_hash_timeout(dp, t2->subtable[0])
300 + table_hash_timeout(dp, t2->subtable[1]));
303 static void table_hash2_destroy(struct sw_table *swt)
305 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
306 table_hash_destroy(t2->subtable[0]);
307 table_hash_destroy(t2->subtable[1]);
311 struct swt_iterator_hash2 {
312 struct sw_table_hash2 *th2;
313 struct swt_iterator ih;
317 static int table_hash2_iterator(struct sw_table *swt,
318 struct swt_iterator *swt_iter)
320 struct swt_iterator_hash2 *ih2;
322 swt_iter->private = ih2 = malloc(sizeof *ih2);
326 ih2->th2 = (struct sw_table_hash2 *) swt;
327 if (!table_hash_iterator(ih2->th2->subtable[0], &ih2->ih)) {
332 if (ih2->ih.flow != NULL) {
333 swt_iter->flow = ih2->ih.flow;
336 table_hash_iterator_destroy(&ih2->ih);
338 if (!table_hash_iterator(ih2->th2->subtable[1], &ih2->ih)) {
342 swt_iter->flow = ih2->ih.flow;
348 static void table_hash2_next(struct swt_iterator *swt_iter)
350 struct swt_iterator_hash2 *ih2;
352 if (swt_iter->flow == NULL)
355 ih2 = (struct swt_iterator_hash2 *) swt_iter->private;
356 table_hash_next(&ih2->ih);
358 if (ih2->ih.flow != NULL) {
359 swt_iter->flow = ih2->ih.flow;
361 if (ih2->table_i == 0) {
362 table_hash_iterator_destroy(&ih2->ih);
364 if (!table_hash_iterator(ih2->th2->subtable[1], &ih2->ih)) {
365 ih2->ih.private = NULL;
366 swt_iter->flow = NULL;
368 swt_iter->flow = ih2->ih.flow;
371 swt_iter->flow = NULL;
376 static void table_hash2_iterator_destroy(struct swt_iterator *swt_iter)
378 struct swt_iterator_hash2 *ih2;
380 ih2 = (struct swt_iterator_hash2 *) swt_iter->private;
381 if (ih2->ih.private != NULL)
382 table_hash_iterator_destroy(&ih2->ih);
386 static void table_hash2_stats(struct sw_table *swt,
387 struct sw_table_stats *stats)
389 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
390 struct sw_table_stats substats[2];
393 for (i = 0; i < 2; i++)
394 table_hash_stats(t2->subtable[i], &substats[i]);
395 stats->name = "hash2";
396 stats->n_flows = substats[0].n_flows + substats[1].n_flows;
397 stats->max_flows = substats[0].max_flows + substats[1].max_flows;
400 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
401 unsigned int poly1, unsigned int buckets1)
404 struct sw_table_hash2 *t2;
405 struct sw_table *swt;
407 t2 = malloc(sizeof *t2);
411 t2->subtable[0] = table_hash_create(poly0, buckets0);
412 if (t2->subtable[0] == NULL)
415 t2->subtable[1] = table_hash_create(poly1, buckets1);
416 if (t2->subtable[1] == NULL)
417 goto out_free_subtable0;
420 swt->lookup = table_hash2_lookup;
421 swt->insert = table_hash2_insert;
422 swt->delete = table_hash2_delete;
423 swt->timeout = table_hash2_timeout;
424 swt->destroy = table_hash2_destroy;
425 swt->stats = table_hash2_stats;
427 swt->iterator = table_hash2_iterator;
428 swt->iterator_next = table_hash2_next;
429 swt->iterator_destroy = table_hash2_iterator_destroy;
434 table_hash_destroy(t2->subtable[0]);