Update copyright on all non-GPL files
[sliver-openvswitch.git] / switch / table-hash.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
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:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
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
27  * SOFTWARE.
28  * 
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.
32  */
33
34 #include "table.h"
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "crc32.h"
39 #include "flow.h"
40 #include "datapath.h"
41
42 struct sw_table_hash {
43     struct sw_table swt;
44     struct crc32 crc32;
45     unsigned int n_flows;
46     unsigned int bucket_mask; /* Number of buckets minus 1. */
47     struct sw_flow **buckets;
48 };
49
50 static struct sw_flow **find_bucket(struct sw_table *swt,
51                                     const struct sw_flow_key *key)
52 {
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];
56 }
57
58 static struct sw_flow *table_hash_lookup(struct sw_table *swt,
59                                          const struct sw_flow_key *key)
60 {
61     struct sw_flow *flow = *find_bucket(swt, key);
62     return flow && !memcmp(&flow->key, key, sizeof *key) ? flow : NULL;
63 }
64
65 static int table_hash_insert(struct sw_table *swt, struct sw_flow *flow)
66 {
67     struct sw_table_hash *th = (struct sw_table_hash *) swt;
68     struct sw_flow **bucket;
69     int retval;
70
71     if (flow->key.wildcards != 0)
72         return 0;
73
74     bucket = find_bucket(swt, &flow->key);
75     if (*bucket == NULL) {
76         th->n_flows++;
77         *bucket = flow;
78         retval = 1;
79     } else {
80         struct sw_flow *old_flow = *bucket;
81         if (!memcmp(&old_flow->key, &flow->key, sizeof flow->key)) {
82             *bucket = flow;
83             flow_free(old_flow);
84             retval = 1;
85         } else {
86             retval = 0;
87         }
88     }
89     return retval;
90 }
91
92 /* Caller must update n_flows. */
93 static void
94 do_delete(struct sw_flow **bucket)
95 {
96     flow_free(*bucket);
97     *bucket = NULL;
98 }
99
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)
103 {
104     struct sw_table_hash *th = (struct sw_table_hash *) swt;
105     unsigned int count = 0;
106
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)) {
111             do_delete(bucket);
112             count = 1;
113         }
114     } else {
115         unsigned int i;
116
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)) {
121                 do_delete(bucket);
122                 count++;
123             }
124         }
125     }
126     th->n_flows -= count;
127     return count;
128 }
129
130 static int table_hash_timeout(struct datapath *dp, struct sw_table *swt)
131 {
132     struct sw_table_hash *th = (struct sw_table_hash *) swt;
133     unsigned int i;
134     int count = 0;
135
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);
141             do_delete(bucket);
142             count++;
143         }
144     }
145     th->n_flows -= count;
146     return count;
147 }
148
149 static void table_hash_destroy(struct sw_table *swt)
150 {
151     struct sw_table_hash *th = (struct sw_table_hash *) swt;
152     unsigned int i;
153     for (i = 0; i <= th->bucket_mask; i++) {
154         if (th->buckets[i]) {
155             flow_free(th->buckets[i]); 
156         }
157     }
158     free(th->buckets);
159     free(th);
160 }
161
162 struct swt_iterator_hash {
163     struct sw_table_hash *th;
164     unsigned int bucket_i;
165 };
166
167 static struct sw_flow *next_flow(struct swt_iterator_hash *ih)
168 {
169     for (;ih->bucket_i <= ih->th->bucket_mask; ih->bucket_i++) {
170         struct sw_flow *f = ih->th->buckets[ih->bucket_i];
171         if (f != NULL)
172             return f;
173     }
174
175     return NULL;
176 }
177
178 static int table_hash_iterator(struct sw_table *swt,
179                                struct swt_iterator *swt_iter)
180 {
181     struct swt_iterator_hash *ih;
182
183     swt_iter->private = ih = malloc(sizeof *ih);
184
185     if (ih == NULL)
186         return 0;
187
188     ih->th = (struct sw_table_hash *) swt;
189
190     ih->bucket_i = 0;
191     swt_iter->flow = next_flow(ih);
192
193     return 1;
194 }
195
196 static void table_hash_next(struct swt_iterator *swt_iter)
197 {
198     struct swt_iterator_hash *ih;
199
200     if (swt_iter->flow == NULL)
201         return;
202
203     ih = (struct swt_iterator_hash *) swt_iter->private;
204
205     ih->bucket_i++;
206     swt_iter->flow = next_flow(ih);
207 }
208
209 static void table_hash_iterator_destroy(struct swt_iterator *swt_iter)
210 {
211     free(swt_iter->private);
212 }
213
214 static void table_hash_stats(struct sw_table *swt,
215                              struct sw_table_stats *stats) 
216 {
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;
221 }
222
223 struct sw_table *table_hash_create(unsigned int polynomial,
224                                    unsigned int n_buckets)
225 {
226     struct sw_table_hash *th;
227     struct sw_table *swt;
228
229     th = malloc(sizeof *th);
230     if (th == NULL)
231         return NULL;
232
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);
237         free(th);
238         return NULL;
239     }
240     th->bucket_mask = n_buckets - 1;
241
242     swt = &th->swt;
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;
252
253     crc32_init(&th->crc32, polynomial);
254
255     return swt;
256 }
257
258 /* Double-hashing table. */
259
260 struct sw_table_hash2 {
261     struct sw_table swt;
262     struct sw_table *subtable[2];
263 };
264
265 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
266                                           const struct sw_flow_key *key)
267 {
268     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
269     int i;
270         
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))
274             return flow;
275     }
276     return NULL;
277 }
278
279 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
280 {
281     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
282
283     if (table_hash_insert(t2->subtable[0], flow))
284         return 1;
285     return table_hash_insert(t2->subtable[1], flow);
286 }
287
288 static int table_hash2_delete(struct sw_table *swt,
289                               const struct sw_flow_key *key, int strict)
290 {
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));
294 }
295
296 static int table_hash2_timeout(struct datapath *dp, struct sw_table *swt)
297 {
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]));
301 }
302
303 static void table_hash2_destroy(struct sw_table *swt)
304 {
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]);
308     free(t2);
309 }
310
311 struct swt_iterator_hash2 {
312     struct sw_table_hash2 *th2;
313     struct swt_iterator ih;
314     uint8_t table_i;
315 };
316
317 static int table_hash2_iterator(struct sw_table *swt,
318                                 struct swt_iterator *swt_iter)
319 {
320     struct swt_iterator_hash2 *ih2;
321
322     swt_iter->private = ih2 = malloc(sizeof *ih2);
323     if (ih2 == NULL)
324         return 0;
325
326     ih2->th2 = (struct sw_table_hash2 *) swt;
327     if (!table_hash_iterator(ih2->th2->subtable[0], &ih2->ih)) {
328         free(ih2);
329         return 0;
330     }
331
332     if (ih2->ih.flow != NULL) {
333         swt_iter->flow = ih2->ih.flow;
334         ih2->table_i = 0;
335     } else {
336         table_hash_iterator_destroy(&ih2->ih);
337         ih2->table_i = 1;
338         if (!table_hash_iterator(ih2->th2->subtable[1], &ih2->ih)) {
339             free(ih2);
340             return 0;
341         }
342         swt_iter->flow = ih2->ih.flow;
343     }
344
345     return 1;
346 }
347
348 static void table_hash2_next(struct swt_iterator *swt_iter) 
349 {
350     struct swt_iterator_hash2 *ih2;
351
352     if (swt_iter->flow == NULL)
353         return;
354
355     ih2 = (struct swt_iterator_hash2 *) swt_iter->private;
356     table_hash_next(&ih2->ih);
357
358     if (ih2->ih.flow != NULL) {
359         swt_iter->flow = ih2->ih.flow;
360     } else {
361         if (ih2->table_i == 0) {
362             table_hash_iterator_destroy(&ih2->ih);
363             ih2->table_i = 1;
364             if (!table_hash_iterator(ih2->th2->subtable[1], &ih2->ih)) {
365                 ih2->ih.private = NULL;
366                 swt_iter->flow = NULL;
367             } else {
368                 swt_iter->flow = ih2->ih.flow;
369             }
370         } else {
371             swt_iter->flow = NULL;
372         }
373     }
374 }
375
376 static void table_hash2_iterator_destroy(struct swt_iterator *swt_iter)
377 {
378     struct swt_iterator_hash2 *ih2;
379
380     ih2 = (struct swt_iterator_hash2 *) swt_iter->private;
381     if (ih2->ih.private != NULL)
382         table_hash_iterator_destroy(&ih2->ih);
383     free(ih2);
384 }
385
386 static void table_hash2_stats(struct sw_table *swt,
387                               struct sw_table_stats *stats)
388 {
389     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
390     struct sw_table_stats substats[2];
391     int i;
392
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;
398 }
399
400 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
401                                     unsigned int poly1, unsigned int buckets1)
402
403 {
404     struct sw_table_hash2 *t2;
405     struct sw_table *swt;
406
407     t2 = malloc(sizeof *t2);
408     if (t2 == NULL)
409         return NULL;
410
411     t2->subtable[0] = table_hash_create(poly0, buckets0);
412     if (t2->subtable[0] == NULL)
413         goto out_free_t2;
414
415     t2->subtable[1] = table_hash_create(poly1, buckets1);
416     if (t2->subtable[1] == NULL)
417         goto out_free_subtable0;
418
419     swt = &t2->swt;
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;
426
427     swt->iterator = table_hash2_iterator;
428     swt->iterator_next = table_hash2_next;
429     swt->iterator_destroy = table_hash2_iterator_destroy;
430
431     return swt;
432
433 out_free_subtable0:
434     table_hash_destroy(t2->subtable[0]);
435 out_free_t2:
436     free(t2);
437     return NULL;
438 }