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.
43 struct sw_table_hash {
47 unsigned int bucket_mask; /* Number of buckets minus 1. */
48 struct sw_flow **buckets;
51 static struct sw_flow **find_bucket(struct sw_table *swt,
52 const struct sw_flow_key *key)
54 struct sw_table_hash *th = (struct sw_table_hash *) swt;
55 unsigned int crc = crc32_calculate(&th->crc32, key,
56 offsetof(struct sw_flow_key, wildcards));
57 return &th->buckets[crc & th->bucket_mask];
60 static struct sw_flow *table_hash_lookup(struct sw_table *swt,
61 const struct sw_flow_key *key)
63 struct sw_flow *flow = *find_bucket(swt, key);
64 return flow && !flow_compare(&flow->key.flow, &key->flow) ? flow : NULL;
67 static int table_hash_insert(struct sw_table *swt, struct sw_flow *flow)
69 struct sw_table_hash *th = (struct sw_table_hash *) swt;
70 struct sw_flow **bucket;
73 if (flow->key.wildcards != 0)
76 bucket = find_bucket(swt, &flow->key);
77 if (*bucket == NULL) {
82 struct sw_flow *old_flow = *bucket;
83 if (!flow_compare(&old_flow->key.flow, &flow->key.flow)) {
84 /* Keep stats from the original flow */
85 flow->used = old_flow->used;
86 flow->created = old_flow->created;
87 flow->packet_count = old_flow->packet_count;
88 flow->byte_count = old_flow->byte_count;
100 /* Caller must update n_flows. */
102 do_delete(struct sw_flow **bucket)
108 /* Returns number of deleted flows. We can igonre the priority
109 * argument, since all exact-match entries are the same (highest)
111 static int table_hash_delete(struct sw_table *swt,
112 const struct sw_flow_key *key,
113 uint16_t priority, int strict)
115 struct sw_table_hash *th = (struct sw_table_hash *) swt;
116 unsigned int count = 0;
118 if (key->wildcards == 0) {
119 struct sw_flow **bucket = find_bucket(swt, key);
120 struct sw_flow *flow = *bucket;
121 if (flow && !flow_compare(&flow->key.flow, &key->flow)) {
128 for (i = 0; i <= th->bucket_mask; i++) {
129 struct sw_flow **bucket = &th->buckets[i];
130 struct sw_flow *flow = *bucket;
131 if (flow && flow_del_matches(&flow->key, key, strict)) {
137 th->n_flows -= count;
141 static void table_hash_timeout(struct sw_table *swt, struct list *deleted)
143 struct sw_table_hash *th = (struct sw_table_hash *) swt;
146 for (i = 0; i <= th->bucket_mask; i++) {
147 struct sw_flow **bucket = &th->buckets[i];
148 struct sw_flow *flow = *bucket;
149 if (flow && flow_timeout(flow)) {
150 list_push_back(deleted, &flow->node);
157 static void table_hash_destroy(struct sw_table *swt)
159 struct sw_table_hash *th = (struct sw_table_hash *) swt;
161 for (i = 0; i <= th->bucket_mask; i++) {
162 if (th->buckets[i]) {
163 flow_free(th->buckets[i]);
170 static int table_hash_iterate(struct sw_table *swt,
171 const struct sw_flow_key *key,
172 struct sw_table_position *position,
173 int (*callback)(struct sw_flow *, void *private),
176 struct sw_table_hash *th = (struct sw_table_hash *) swt;
178 if (position->private[0] > th->bucket_mask)
181 if (key->wildcards == 0) {
182 struct sw_flow *flow = table_hash_lookup(swt, key);
183 position->private[0] = -1;
184 return flow ? callback(flow, private) : 0;
188 for (i = position->private[0]; i <= th->bucket_mask; i++) {
189 struct sw_flow *flow = th->buckets[i];
190 if (flow && flow_matches_1wild(&flow->key, key)) {
191 int error = callback(flow, private);
193 position->private[0] = i + 1;
202 static void table_hash_stats(struct sw_table *swt,
203 struct sw_table_stats *stats)
205 struct sw_table_hash *th = (struct sw_table_hash *) swt;
206 stats->name = "hash";
207 stats->wildcards = 0; /* No wildcards are supported. */
208 stats->n_flows = th->n_flows;
209 stats->max_flows = th->bucket_mask + 1;
210 stats->n_matched = swt->n_matched;
213 struct sw_table *table_hash_create(unsigned int polynomial,
214 unsigned int n_buckets)
216 struct sw_table_hash *th;
217 struct sw_table *swt;
219 th = malloc(sizeof *th);
222 memset(th, '\0', sizeof *th);
224 assert(!(n_buckets & (n_buckets - 1)));
225 th->buckets = calloc(n_buckets, sizeof *th->buckets);
226 if (th->buckets == NULL) {
227 printf("failed to allocate %u buckets\n", n_buckets);
232 th->bucket_mask = n_buckets - 1;
235 swt->lookup = table_hash_lookup;
236 swt->insert = table_hash_insert;
237 swt->delete = table_hash_delete;
238 swt->timeout = table_hash_timeout;
239 swt->destroy = table_hash_destroy;
240 swt->iterate = table_hash_iterate;
241 swt->stats = table_hash_stats;
243 crc32_init(&th->crc32, polynomial);
248 /* Double-hashing table. */
250 struct sw_table_hash2 {
252 struct sw_table *subtable[2];
255 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
256 const struct sw_flow_key *key)
258 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
261 for (i = 0; i < 2; i++) {
262 struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
263 if (flow && !flow_compare(&flow->key.flow, &key->flow))
269 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
271 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
273 if (table_hash_insert(t2->subtable[0], flow))
275 return table_hash_insert(t2->subtable[1], flow);
278 static int table_hash2_delete(struct sw_table *swt,
279 const struct sw_flow_key *key,
280 uint16_t priority, int strict)
282 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
283 return (table_hash_delete(t2->subtable[0], key, priority, strict)
284 + table_hash_delete(t2->subtable[1], key, priority, strict));
287 static void table_hash2_timeout(struct sw_table *swt, struct list *deleted)
289 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
290 table_hash_timeout(t2->subtable[0], deleted);
291 table_hash_timeout(t2->subtable[1], deleted);
294 static void table_hash2_destroy(struct sw_table *swt)
296 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
297 table_hash_destroy(t2->subtable[0]);
298 table_hash_destroy(t2->subtable[1]);
302 static int table_hash2_iterate(struct sw_table *swt,
303 const struct sw_flow_key *key,
304 struct sw_table_position *position,
305 int (*callback)(struct sw_flow *, void *),
308 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
311 for (i = position->private[1]; i < 2; i++) {
312 int error = table_hash_iterate(t2->subtable[i], key, position,
317 position->private[0] = 0;
318 position->private[1]++;
323 static void table_hash2_stats(struct sw_table *swt,
324 struct sw_table_stats *stats)
326 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
327 struct sw_table_stats substats[2];
330 for (i = 0; i < 2; i++)
331 table_hash_stats(t2->subtable[i], &substats[i]);
332 stats->name = "hash2";
333 stats->wildcards = 0; /* No wildcards are supported. */
334 stats->n_flows = substats[0].n_flows + substats[1].n_flows;
335 stats->max_flows = substats[0].max_flows + substats[1].max_flows;
336 stats->n_matched = swt->n_matched;
339 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
340 unsigned int poly1, unsigned int buckets1)
343 struct sw_table_hash2 *t2;
344 struct sw_table *swt;
346 t2 = malloc(sizeof *t2);
349 memset(t2, '\0', sizeof *t2);
351 t2->subtable[0] = table_hash_create(poly0, buckets0);
352 if (t2->subtable[0] == NULL)
355 t2->subtable[1] = table_hash_create(poly1, buckets1);
356 if (t2->subtable[1] == NULL)
357 goto out_free_subtable0;
360 swt->lookup = table_hash2_lookup;
361 swt->insert = table_hash2_insert;
362 swt->delete = table_hash2_delete;
363 swt->timeout = table_hash2_timeout;
364 swt->destroy = table_hash2_destroy;
365 swt->iterate = table_hash2_iterate;
366 swt->stats = table_hash2_stats;
371 table_hash_destroy(t2->subtable[0]);