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 #include "switch-flow.h"
44 struct sw_table_hash {
48 unsigned int bucket_mask; /* Number of buckets minus 1. */
49 struct sw_flow **buckets;
52 static struct sw_flow **find_bucket(struct sw_table *swt,
53 const struct sw_flow_key *key)
55 struct sw_table_hash *th = (struct sw_table_hash *) swt;
56 unsigned int crc = crc32_calculate(&th->crc32, key,
57 offsetof(struct sw_flow_key, wildcards));
58 return &th->buckets[crc & th->bucket_mask];
61 static struct sw_flow *table_hash_lookup(struct sw_table *swt,
62 const struct sw_flow_key *key)
64 struct sw_flow *flow = *find_bucket(swt, key);
65 return flow && !flow_compare(&flow->key.flow, &key->flow) ? flow : NULL;
68 static int table_hash_insert(struct sw_table *swt, struct sw_flow *flow)
70 struct sw_table_hash *th = (struct sw_table_hash *) swt;
71 struct sw_flow **bucket;
74 if (flow->key.wildcards != 0)
77 bucket = find_bucket(swt, &flow->key);
78 if (*bucket == NULL) {
83 struct sw_flow *old_flow = *bucket;
84 if (!flow_compare(&old_flow->key.flow, &flow->key.flow)) {
95 static int table_hash_modify(struct sw_table *swt,
96 const struct sw_flow_key *key, uint16_t priority, int strict,
97 const struct ofp_action_header *actions, size_t actions_len)
99 struct sw_table_hash *th = (struct sw_table_hash *) swt;
100 unsigned int count = 0;
102 if (key->wildcards == 0) {
103 struct sw_flow **bucket = find_bucket(swt, key);
104 struct sw_flow *flow = *bucket;
105 if (flow && flow_matches_desc(&flow->key, key, strict)
106 && (!strict || (flow->priority == priority))) {
107 flow_replace_acts(flow, actions, actions_len);
113 for (i = 0; i <= th->bucket_mask; i++) {
114 struct sw_flow **bucket = &th->buckets[i];
115 struct sw_flow *flow = *bucket;
116 if (flow && flow_matches_desc(&flow->key, key, strict)
117 && (!strict || (flow->priority == priority))) {
118 flow_replace_acts(flow, actions, actions_len);
126 /* Caller must update n_flows. */
128 do_delete(struct sw_flow **bucket)
134 /* Returns number of deleted flows. We can igonre the priority
135 * argument, since all exact-match entries are the same (highest)
137 static int table_hash_delete(struct sw_table *swt,
138 const struct sw_flow_key *key,
140 uint16_t priority, int strict)
142 struct sw_table_hash *th = (struct sw_table_hash *) swt;
143 unsigned int count = 0;
145 if (key->wildcards == 0) {
146 struct sw_flow **bucket = find_bucket(swt, key);
147 struct sw_flow *flow = *bucket;
148 if (flow && !flow_compare(&flow->key.flow, &key->flow)
149 && flow_has_out_port(flow, out_port)) {
156 for (i = 0; i <= th->bucket_mask; i++) {
157 struct sw_flow **bucket = &th->buckets[i];
158 struct sw_flow *flow = *bucket;
159 if (flow && flow_matches_desc(&flow->key, key, strict)
160 && flow_has_out_port(flow, out_port)) {
166 th->n_flows -= count;
170 static void table_hash_timeout(struct sw_table *swt, struct list *deleted)
172 struct sw_table_hash *th = (struct sw_table_hash *) swt;
175 for (i = 0; i <= th->bucket_mask; i++) {
176 struct sw_flow **bucket = &th->buckets[i];
177 struct sw_flow *flow = *bucket;
178 if (flow && flow_timeout(flow)) {
179 list_push_back(deleted, &flow->node);
186 static void table_hash_destroy(struct sw_table *swt)
188 struct sw_table_hash *th = (struct sw_table_hash *) swt;
190 for (i = 0; i <= th->bucket_mask; i++) {
191 if (th->buckets[i]) {
192 flow_free(th->buckets[i]);
199 static int table_hash_iterate(struct sw_table *swt,
200 const struct sw_flow_key *key, uint16_t out_port,
201 struct sw_table_position *position,
202 int (*callback)(struct sw_flow *, void *private),
205 struct sw_table_hash *th = (struct sw_table_hash *) swt;
207 if (position->private[0] > th->bucket_mask)
210 if (key->wildcards == 0) {
211 struct sw_flow *flow = table_hash_lookup(swt, key);
212 position->private[0] = -1;
213 if (!flow || !flow_has_out_port(flow, out_port)) {
216 return callback(flow, private);
220 for (i = position->private[0]; i <= th->bucket_mask; i++) {
221 struct sw_flow *flow = th->buckets[i];
222 if (flow && flow_matches_1wild(&flow->key, key)
223 && flow_has_out_port(flow, out_port)) {
224 int error = callback(flow, private);
226 position->private[0] = i + 1;
235 static void table_hash_stats(struct sw_table *swt,
236 struct sw_table_stats *stats)
238 struct sw_table_hash *th = (struct sw_table_hash *) swt;
239 stats->name = "hash";
240 stats->wildcards = 0; /* No wildcards are supported. */
241 stats->n_flows = th->n_flows;
242 stats->max_flows = th->bucket_mask + 1;
243 stats->n_lookup = swt->n_lookup;
244 stats->n_matched = swt->n_matched;
247 struct sw_table *table_hash_create(unsigned int polynomial,
248 unsigned int n_buckets)
250 struct sw_table_hash *th;
251 struct sw_table *swt;
253 th = malloc(sizeof *th);
256 memset(th, '\0', sizeof *th);
258 assert(!(n_buckets & (n_buckets - 1)));
259 th->buckets = calloc(n_buckets, sizeof *th->buckets);
260 if (th->buckets == NULL) {
261 printf("failed to allocate %u buckets\n", n_buckets);
266 th->bucket_mask = n_buckets - 1;
269 swt->lookup = table_hash_lookup;
270 swt->insert = table_hash_insert;
271 swt->modify = table_hash_modify;
272 swt->delete = table_hash_delete;
273 swt->timeout = table_hash_timeout;
274 swt->destroy = table_hash_destroy;
275 swt->iterate = table_hash_iterate;
276 swt->stats = table_hash_stats;
278 crc32_init(&th->crc32, polynomial);
283 /* Double-hashing table. */
285 struct sw_table_hash2 {
287 struct sw_table *subtable[2];
290 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
291 const struct sw_flow_key *key)
293 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
296 for (i = 0; i < 2; i++) {
297 struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
298 if (flow && !flow_compare(&flow->key.flow, &key->flow))
304 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
306 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
308 if (table_hash_insert(t2->subtable[0], flow))
310 return table_hash_insert(t2->subtable[1], flow);
313 static int table_hash2_modify(struct sw_table *swt,
314 const struct sw_flow_key *key, uint16_t priority, int strict,
315 const struct ofp_action_header *actions, size_t actions_len)
317 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
318 return (table_hash_modify(t2->subtable[0], key, priority, strict,
319 actions, actions_len)
320 + table_hash_modify(t2->subtable[1], key, priority, strict,
321 actions, actions_len));
324 static int table_hash2_delete(struct sw_table *swt,
325 const struct sw_flow_key *key,
327 uint16_t priority, int strict)
329 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
330 return (table_hash_delete(t2->subtable[0], key, out_port, priority, strict)
331 + table_hash_delete(t2->subtable[1], key, out_port, priority,
335 static void table_hash2_timeout(struct sw_table *swt, struct list *deleted)
337 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
338 table_hash_timeout(t2->subtable[0], deleted);
339 table_hash_timeout(t2->subtable[1], deleted);
342 static void table_hash2_destroy(struct sw_table *swt)
344 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
345 table_hash_destroy(t2->subtable[0]);
346 table_hash_destroy(t2->subtable[1]);
350 static int table_hash2_iterate(struct sw_table *swt,
351 const struct sw_flow_key *key,
353 struct sw_table_position *position,
354 int (*callback)(struct sw_flow *, void *),
357 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
360 for (i = position->private[1]; i < 2; i++) {
361 int error = table_hash_iterate(t2->subtable[i], key, out_port,
362 position, callback, private);
366 position->private[0] = 0;
367 position->private[1]++;
372 static void table_hash2_stats(struct sw_table *swt,
373 struct sw_table_stats *stats)
375 struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
376 struct sw_table_stats substats[2];
379 for (i = 0; i < 2; i++)
380 table_hash_stats(t2->subtable[i], &substats[i]);
381 stats->name = "hash2";
382 stats->wildcards = 0; /* No wildcards are supported. */
383 stats->n_flows = substats[0].n_flows + substats[1].n_flows;
384 stats->max_flows = substats[0].max_flows + substats[1].max_flows;
385 stats->n_lookup = swt->n_lookup;
386 stats->n_matched = swt->n_matched;
389 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
390 unsigned int poly1, unsigned int buckets1)
393 struct sw_table_hash2 *t2;
394 struct sw_table *swt;
396 t2 = malloc(sizeof *t2);
399 memset(t2, '\0', sizeof *t2);
401 t2->subtable[0] = table_hash_create(poly0, buckets0);
402 if (t2->subtable[0] == NULL)
405 t2->subtable[1] = table_hash_create(poly1, buckets1);
406 if (t2->subtable[1] == NULL)
407 goto out_free_subtable0;
410 swt->lookup = table_hash2_lookup;
411 swt->insert = table_hash2_insert;
412 swt->modify = table_hash2_modify;
413 swt->delete = table_hash2_delete;
414 swt->timeout = table_hash2_timeout;
415 swt->destroy = table_hash2_destroy;
416 swt->iterate = table_hash2_iterate;
417 swt->stats = table_hash2_stats;
422 table_hash_destroy(t2->subtable[0]);