aaec5039daad8a56585e1ec7fca3d8a66437fe34
[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.  We can igonre the priority
101  * argument, since all exact-match entries are the same (highest)
102  * priority. */
103 static int table_hash_delete(struct sw_table *swt,
104                              const struct sw_flow_key *key, 
105                              uint16_t priority, int strict)
106 {
107     struct sw_table_hash *th = (struct sw_table_hash *) swt;
108     unsigned int count = 0;
109
110     if (key->wildcards == 0) {
111         struct sw_flow **bucket = find_bucket(swt, key);
112         struct sw_flow *flow = *bucket;
113         if (flow && !memcmp(&flow->key, key, sizeof *key)) {
114             do_delete(bucket);
115             count = 1;
116         }
117     } else {
118         unsigned int i;
119
120         for (i = 0; i <= th->bucket_mask; i++) {
121             struct sw_flow **bucket = &th->buckets[i];
122             struct sw_flow *flow = *bucket;
123             if (flow && flow_del_matches(&flow->key, key, strict)) {
124                 do_delete(bucket);
125                 count++;
126             }
127         }
128     }
129     th->n_flows -= count;
130     return count;
131 }
132
133 static void table_hash_timeout(struct sw_table *swt, struct list *deleted)
134 {
135     struct sw_table_hash *th = (struct sw_table_hash *) swt;
136     unsigned int i;
137
138     for (i = 0; i <= th->bucket_mask; i++) {
139         struct sw_flow **bucket = &th->buckets[i];
140         struct sw_flow *flow = *bucket;
141         if (flow && flow_timeout(flow)) {
142             list_push_back(deleted, &flow->node);
143             *bucket = NULL;
144             th->n_flows--;
145         }
146     }
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 static int table_hash_iterate(struct sw_table *swt,
163                               const struct sw_flow_key *key,
164                               struct sw_table_position *position,
165                               int (*callback)(struct sw_flow *, void *private),
166                               void *private) 
167 {
168     struct sw_table_hash *th = (struct sw_table_hash *) swt;
169
170     if (position->private[0] > th->bucket_mask)
171         return 0;
172
173     if (key->wildcards == 0) {
174         struct sw_flow *flow = table_hash_lookup(swt, key);
175         position->private[0] = -1;
176         return flow ? callback(flow, private) : 0;
177     } else {
178         int i;
179
180         for (i = position->private[0]; i <= th->bucket_mask; i++) {
181             struct sw_flow *flow = th->buckets[i];
182             if (flow && flow_matches(key, &flow->key)) {
183                 int error = callback(flow, private);
184                 if (error) {
185                     position->private[0] = i + 1;
186                     return error;
187                 }
188             }
189         }
190         return 0;
191     }
192 }
193
194 static void table_hash_stats(struct sw_table *swt,
195                              struct sw_table_stats *stats) 
196 {
197     struct sw_table_hash *th = (struct sw_table_hash *) swt;
198     stats->name = "hash";
199     stats->n_flows = th->n_flows;
200     stats->max_flows = th->bucket_mask + 1;
201 }
202
203 struct sw_table *table_hash_create(unsigned int polynomial,
204                                    unsigned int n_buckets)
205 {
206     struct sw_table_hash *th;
207     struct sw_table *swt;
208
209     th = malloc(sizeof *th);
210     if (th == NULL)
211         return NULL;
212
213     assert(!(n_buckets & (n_buckets - 1)));
214     th->buckets = calloc(n_buckets, sizeof *th->buckets);
215     if (th->buckets == NULL) {
216         printf("failed to allocate %u buckets\n", n_buckets);
217         free(th);
218         return NULL;
219     }
220     th->n_flows = 0;
221     th->bucket_mask = n_buckets - 1;
222
223     swt = &th->swt;
224     swt->lookup = table_hash_lookup;
225     swt->insert = table_hash_insert;
226     swt->delete = table_hash_delete;
227     swt->timeout = table_hash_timeout;
228     swt->destroy = table_hash_destroy;
229     swt->iterate = table_hash_iterate;
230     swt->stats = table_hash_stats;
231
232     crc32_init(&th->crc32, polynomial);
233
234     return swt;
235 }
236
237 /* Double-hashing table. */
238
239 struct sw_table_hash2 {
240     struct sw_table swt;
241     struct sw_table *subtable[2];
242 };
243
244 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
245                                           const struct sw_flow_key *key)
246 {
247     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
248     int i;
249         
250     for (i = 0; i < 2; i++) {
251         struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
252         if (flow && !memcmp(&flow->key, key, sizeof *key))
253             return flow;
254     }
255     return NULL;
256 }
257
258 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
259 {
260     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
261
262     if (table_hash_insert(t2->subtable[0], flow))
263         return 1;
264     return table_hash_insert(t2->subtable[1], flow);
265 }
266
267 static int table_hash2_delete(struct sw_table *swt,
268                               const struct sw_flow_key *key, 
269                               uint16_t priority, int strict)
270 {
271     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
272     return (table_hash_delete(t2->subtable[0], key, priority, strict)
273             + table_hash_delete(t2->subtable[1], key, priority, strict));
274 }
275
276 static void table_hash2_timeout(struct sw_table *swt, struct list *deleted)
277 {
278     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
279     table_hash_timeout(t2->subtable[0], deleted);
280     table_hash_timeout(t2->subtable[1], deleted);
281 }
282
283 static void table_hash2_destroy(struct sw_table *swt)
284 {
285     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
286     table_hash_destroy(t2->subtable[0]);
287     table_hash_destroy(t2->subtable[1]);
288     free(t2);
289 }
290
291 static int table_hash2_iterate(struct sw_table *swt,
292                                const struct sw_flow_key *key,
293                                struct sw_table_position *position,
294                                int (*callback)(struct sw_flow *, void *),
295                                void *private)
296 {
297     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
298     int i;
299
300     for (i = position->private[1]; i < 2; i++) {
301         int error = table_hash_iterate(t2->subtable[i], key, position,
302                                        callback, private);
303         if (error) {
304             return error;
305         }
306         position->private[0] = 0;
307         position->private[1]++;
308     }
309     return 0;
310 }
311
312 static void table_hash2_stats(struct sw_table *swt,
313                               struct sw_table_stats *stats)
314 {
315     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
316     struct sw_table_stats substats[2];
317     int i;
318
319     for (i = 0; i < 2; i++)
320         table_hash_stats(t2->subtable[i], &substats[i]);
321     stats->name = "hash2";
322     stats->n_flows = substats[0].n_flows + substats[1].n_flows;
323     stats->max_flows = substats[0].max_flows + substats[1].max_flows;
324 }
325
326 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
327                                     unsigned int poly1, unsigned int buckets1)
328
329 {
330     struct sw_table_hash2 *t2;
331     struct sw_table *swt;
332
333     t2 = malloc(sizeof *t2);
334     if (t2 == NULL)
335         return NULL;
336
337     t2->subtable[0] = table_hash_create(poly0, buckets0);
338     if (t2->subtable[0] == NULL)
339         goto out_free_t2;
340
341     t2->subtable[1] = table_hash_create(poly1, buckets1);
342     if (t2->subtable[1] == NULL)
343         goto out_free_subtable0;
344
345     swt = &t2->swt;
346     swt->lookup = table_hash2_lookup;
347     swt->insert = table_hash2_insert;
348     swt->delete = table_hash2_delete;
349     swt->timeout = table_hash2_timeout;
350     swt->destroy = table_hash2_destroy;
351     swt->iterate = table_hash2_iterate;
352     swt->stats = table_hash2_stats;
353
354     return swt;
355
356 out_free_subtable0:
357     table_hash_destroy(t2->subtable[0]);
358 out_free_t2:
359     free(t2);
360     return NULL;
361 }