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, sizeof *key);
56 return &th->buckets[crc & th->bucket_mask];
59 static struct sw_flow *table_hash_lookup(struct sw_table *swt,
60 const struct sw_flow_key *key)
62 struct sw_flow *flow = *find_bucket(swt, key);
63 return flow && !memcmp(&flow->key, key, sizeof *key) ? flow : NULL;
66 static int table_hash_insert(struct sw_table *swt, struct sw_flow *flow)
68 struct sw_table_hash *th = (struct sw_table_hash *) swt;
69 struct sw_flow **bucket;
72 if (flow->key.wildcards != 0)
75 bucket = find_bucket(swt, &flow->key);
76 if (*bucket == NULL) {
81 struct sw_flow *old_flow = *bucket;
82 if (!memcmp(&old_flow->key, &flow->key, sizeof flow->key)) {
93 /* Caller must update n_flows. */
95 do_delete(struct sw_flow **bucket)
101 /* Returns number of deleted flows. We can igonre the priority
102 * argument, since all exact-match entries are the same (highest)
104 static int table_hash_delete(struct sw_table *swt,
105 const struct sw_flow_key *key,
106 uint16_t priority, int strict)
108 struct sw_table_hash *th = (struct sw_table_hash *) swt;
109 unsigned int count = 0;
111 if (key->wildcards == 0) {
112 struct sw_flow **bucket = find_bucket(swt, key);
113 struct sw_flow *flow = *bucket;
114 if (flow && !memcmp(&flow->key, key, sizeof *key)) {
121 for (i = 0; i <= th->bucket_mask; i++) {
122 struct sw_flow **bucket = &th->buckets[i];
123 struct sw_flow *flow = *bucket;
124 if (flow && flow_del_matches(&flow->key, key, strict)) {
130 th->n_flows -= count;
134 static void table_hash_timeout(struct sw_table *swt, struct list *deleted)
136 struct sw_table_hash *th = (struct sw_table_hash *) swt;
139 for (i = 0; i <= th->bucket_mask; i++) {
140 struct sw_flow **bucket = &th->buckets[i];
141 struct sw_flow *flow = *bucket;
142 if (flow && flow_timeout(flow)) {
143 list_push_back(deleted, &flow->node);
150 static void table_hash_destroy(struct sw_table *swt)
152 struct sw_table_hash *th = (struct sw_table_hash *) swt;
154 for (i = 0; i <= th->bucket_mask; i++) {
155 if (th->buckets[i]) {
156 flow_free(th->buckets[i]);
163 static int table_hash_iterate(struct sw_table *swt,
164 const struct sw_flow_key *key,
165 struct sw_table_position *position,
166 int (*callback)(struct sw_flow *, void *private),
169 struct sw_table_hash *th = (struct sw_table_hash *) swt;
171 if (position->private[0] > th->bucket_mask)
174 if (key->wildcards == 0) {
175 struct sw_flow *flow = table_hash_lookup(swt, key);
176 position->private[0] = -1;
177 return flow ? callback(flow, private) : 0;
181 for (i = position->private[0]; i <= th->bucket_mask; i++) {
182 struct sw_flow *flow = th->buckets[i];
183 if (flow && flow_matches_1wild(&flow->key, key)) {
184 int error = callback(flow, private);
186 position->private[0] = i + 1;
195 static void table_hash_stats(struct sw_table *swt,
196 struct sw_table_stats *stats)
198 struct sw_table_hash *th = (struct sw_table_hash *) swt;
199 stats->name = "hash";
200 stats->n_flows = th->n_flows;
201 stats->max_flows = th->bucket_mask + 1;
202 stats->n_matched = swt->n_matched;
205 struct sw_table *table_hash_create(unsigned int polynomial,
206 unsigned int n_buckets)
208 struct sw_table_hash *th;
209 struct sw_table *swt;
211 th = malloc(sizeof *th);
215 assert(!(n_buckets & (n_buckets - 1)));
216 th->buckets = calloc(n_buckets, sizeof *th->buckets);
217 if (th->buckets == NULL) {
218 printf("failed to allocate %u buckets\n", n_buckets);
223 th->bucket_mask = n_buckets - 1;
226 swt->lookup = table_hash_lookup;
227 swt->insert = table_hash_insert;
228 swt->delete = table_hash_delete;
229 swt->timeout = table_hash_timeout;
230 swt->destroy = table_hash_destroy;
231 swt->iterate = table_hash_iterate;
232 swt->stats = table_hash_stats;
234 crc32_init(&th->crc32, polynomial);
239 /* Double-hashing table. */
241 struct sw_table_hash2 {
243 struct sw_table *subtable[2];
246 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
247 const struct sw_flow_key *key)
249 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
252 for (i = 0; i < 2; i++) {
253 struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
254 if (flow && !memcmp(&flow->key, key, sizeof *key))
260 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
262 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
264 if (table_hash_insert(t2->subtable[0], flow))
266 return table_hash_insert(t2->subtable[1], flow);
269 static int table_hash2_delete(struct sw_table *swt,
270 const struct sw_flow_key *key,
271 uint16_t priority, int strict)
273 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
274 return (table_hash_delete(t2->subtable[0], key, priority, strict)
275 + table_hash_delete(t2->subtable[1], key, priority, strict));
278 static void table_hash2_timeout(struct sw_table *swt, struct list *deleted)
280 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
281 table_hash_timeout(t2->subtable[0], deleted);
282 table_hash_timeout(t2->subtable[1], deleted);
285 static void table_hash2_destroy(struct sw_table *swt)
287 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
288 table_hash_destroy(t2->subtable[0]);
289 table_hash_destroy(t2->subtable[1]);
293 static int table_hash2_iterate(struct sw_table *swt,
294 const struct sw_flow_key *key,
295 struct sw_table_position *position,
296 int (*callback)(struct sw_flow *, void *),
299 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
302 for (i = position->private[1]; i < 2; i++) {
303 int error = table_hash_iterate(t2->subtable[i], key, position,
308 position->private[0] = 0;
309 position->private[1]++;
314 static void table_hash2_stats(struct sw_table *swt,
315 struct sw_table_stats *stats)
317 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
318 struct sw_table_stats substats[2];
321 for (i = 0; i < 2; i++)
322 table_hash_stats(t2->subtable[i], &substats[i]);
323 stats->name = "hash2";
324 stats->n_flows = substats[0].n_flows + substats[1].n_flows;
325 stats->max_flows = substats[0].max_flows + substats[1].max_flows;
326 stats->n_matched = swt->n_matched;
329 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
330 unsigned int poly1, unsigned int buckets1)
333 struct sw_table_hash2 *t2;
334 struct sw_table *swt;
336 t2 = malloc(sizeof *t2);
340 t2->subtable[0] = table_hash_create(poly0, buckets0);
341 if (t2->subtable[0] == NULL)
344 t2->subtable[1] = table_hash_create(poly1, buckets1);
345 if (t2->subtable[1] == NULL)
346 goto out_free_subtable0;
349 swt->lookup = table_hash2_lookup;
350 swt->insert = table_hash2_insert;
351 swt->delete = table_hash2_delete;
352 swt->timeout = table_hash2_timeout;
353 swt->destroy = table_hash2_destroy;
354 swt->iterate = table_hash2_iterate;
355 swt->stats = table_hash2_stats;
360 table_hash_destroy(t2->subtable[0]);