0076537e4754eae15999d944d5dfd3975dbaab9c
[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 <config.h>
35 #include "table.h"
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "crc32.h"
40 #include "flow.h"
41 #include "datapath.h"
42
43 struct sw_table_hash {
44     struct sw_table swt;
45     struct crc32 crc32;
46     unsigned int n_flows;
47     unsigned int bucket_mask; /* Number of buckets minus 1. */
48     struct sw_flow **buckets;
49 };
50
51 static struct sw_flow **find_bucket(struct sw_table *swt,
52                                     const struct sw_flow_key *key)
53 {
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];
58 }
59
60 static struct sw_flow *table_hash_lookup(struct sw_table *swt,
61                                          const struct sw_flow_key *key)
62 {
63     struct sw_flow *flow = *find_bucket(swt, key);
64     return flow && !flow_compare(&flow->key.flow, &key->flow) ? flow : NULL;
65 }
66
67 static int table_hash_insert(struct sw_table *swt, struct sw_flow *flow)
68 {
69     struct sw_table_hash *th = (struct sw_table_hash *) swt;
70     struct sw_flow **bucket;
71     int retval;
72
73     if (flow->key.wildcards != 0)
74         return 0;
75
76     bucket = find_bucket(swt, &flow->key);
77     if (*bucket == NULL) {
78         th->n_flows++;
79         *bucket = flow;
80         retval = 1;
81     } else {
82         struct sw_flow *old_flow = *bucket;
83         if (!flow_compare(&old_flow->key.flow, &flow->key.flow)) {
84             *bucket = flow;
85             flow_free(old_flow);
86             retval = 1;
87         } else {
88             retval = 0;
89         }
90     }
91     return retval;
92 }
93
94 /* Caller must update n_flows. */
95 static void
96 do_delete(struct sw_flow **bucket)
97 {
98     flow_free(*bucket);
99     *bucket = NULL;
100 }
101
102 /* Returns number of deleted flows.  We can igonre the priority
103  * argument, since all exact-match entries are the same (highest)
104  * priority. */
105 static int table_hash_delete(struct sw_table *swt,
106                              const struct sw_flow_key *key, 
107                              uint16_t priority, int strict)
108 {
109     struct sw_table_hash *th = (struct sw_table_hash *) swt;
110     unsigned int count = 0;
111
112     if (key->wildcards == 0) {
113         struct sw_flow **bucket = find_bucket(swt, key);
114         struct sw_flow *flow = *bucket;
115         if (flow && !flow_compare(&flow->key.flow, &key->flow)) {
116             do_delete(bucket);
117             count = 1;
118         }
119     } else {
120         unsigned int i;
121
122         for (i = 0; i <= th->bucket_mask; i++) {
123             struct sw_flow **bucket = &th->buckets[i];
124             struct sw_flow *flow = *bucket;
125             if (flow && flow_del_matches(&flow->key, key, strict)) {
126                 do_delete(bucket);
127                 count++;
128             }
129         }
130     }
131     th->n_flows -= count;
132     return count;
133 }
134
135 static void table_hash_timeout(struct sw_table *swt, struct list *deleted)
136 {
137     struct sw_table_hash *th = (struct sw_table_hash *) swt;
138     unsigned int i;
139
140     for (i = 0; i <= th->bucket_mask; i++) {
141         struct sw_flow **bucket = &th->buckets[i];
142         struct sw_flow *flow = *bucket;
143         if (flow && flow_timeout(flow)) {
144             list_push_back(deleted, &flow->node);
145             *bucket = NULL;
146             th->n_flows--;
147         }
148     }
149 }
150
151 static void table_hash_destroy(struct sw_table *swt)
152 {
153     struct sw_table_hash *th = (struct sw_table_hash *) swt;
154     unsigned int i;
155     for (i = 0; i <= th->bucket_mask; i++) {
156         if (th->buckets[i]) {
157             flow_free(th->buckets[i]); 
158         }
159     }
160     free(th->buckets);
161     free(th);
162 }
163
164 static int table_hash_iterate(struct sw_table *swt,
165                               const struct sw_flow_key *key,
166                               struct sw_table_position *position,
167                               int (*callback)(struct sw_flow *, void *private),
168                               void *private) 
169 {
170     struct sw_table_hash *th = (struct sw_table_hash *) swt;
171
172     if (position->private[0] > th->bucket_mask)
173         return 0;
174
175     if (key->wildcards == 0) {
176         struct sw_flow *flow = table_hash_lookup(swt, key);
177         position->private[0] = -1;
178         return flow ? callback(flow, private) : 0;
179     } else {
180         int i;
181
182         for (i = position->private[0]; i <= th->bucket_mask; i++) {
183             struct sw_flow *flow = th->buckets[i];
184             if (flow && flow_matches_1wild(&flow->key, key)) {
185                 int error = callback(flow, private);
186                 if (error) {
187                     position->private[0] = i + 1;
188                     return error;
189                 }
190             }
191         }
192         return 0;
193     }
194 }
195
196 static void table_hash_stats(struct sw_table *swt,
197                              struct sw_table_stats *stats) 
198 {
199     struct sw_table_hash *th = (struct sw_table_hash *) swt;
200     stats->name = "hash";
201     stats->n_flows = th->n_flows;
202     stats->max_flows = th->bucket_mask + 1;
203     stats->n_matched = swt->n_matched;
204 }
205
206 struct sw_table *table_hash_create(unsigned int polynomial,
207                                    unsigned int n_buckets)
208 {
209     struct sw_table_hash *th;
210     struct sw_table *swt;
211
212     th = malloc(sizeof *th);
213     if (th == NULL)
214         return NULL;
215     memset(th, '\0', sizeof *th);
216
217     assert(!(n_buckets & (n_buckets - 1)));
218     th->buckets = calloc(n_buckets, sizeof *th->buckets);
219     if (th->buckets == NULL) {
220         printf("failed to allocate %u buckets\n", n_buckets);
221         free(th);
222         return NULL;
223     }
224     th->n_flows = 0;
225     th->bucket_mask = n_buckets - 1;
226
227     swt = &th->swt;
228     swt->lookup = table_hash_lookup;
229     swt->insert = table_hash_insert;
230     swt->delete = table_hash_delete;
231     swt->timeout = table_hash_timeout;
232     swt->destroy = table_hash_destroy;
233     swt->iterate = table_hash_iterate;
234     swt->stats = table_hash_stats;
235
236     crc32_init(&th->crc32, polynomial);
237
238     return swt;
239 }
240
241 /* Double-hashing table. */
242
243 struct sw_table_hash2 {
244     struct sw_table swt;
245     struct sw_table *subtable[2];
246 };
247
248 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
249                                           const struct sw_flow_key *key)
250 {
251     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
252     int i;
253         
254     for (i = 0; i < 2; i++) {
255         struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
256         if (flow && !flow_compare(&flow->key.flow, &key->flow))
257             return flow;
258     }
259     return NULL;
260 }
261
262 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
263 {
264     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
265
266     if (table_hash_insert(t2->subtable[0], flow))
267         return 1;
268     return table_hash_insert(t2->subtable[1], flow);
269 }
270
271 static int table_hash2_delete(struct sw_table *swt,
272                               const struct sw_flow_key *key, 
273                               uint16_t priority, int strict)
274 {
275     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
276     return (table_hash_delete(t2->subtable[0], key, priority, strict)
277             + table_hash_delete(t2->subtable[1], key, priority, strict));
278 }
279
280 static void table_hash2_timeout(struct sw_table *swt, struct list *deleted)
281 {
282     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
283     table_hash_timeout(t2->subtable[0], deleted);
284     table_hash_timeout(t2->subtable[1], deleted);
285 }
286
287 static void table_hash2_destroy(struct sw_table *swt)
288 {
289     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
290     table_hash_destroy(t2->subtable[0]);
291     table_hash_destroy(t2->subtable[1]);
292     free(t2);
293 }
294
295 static int table_hash2_iterate(struct sw_table *swt,
296                                const struct sw_flow_key *key,
297                                struct sw_table_position *position,
298                                int (*callback)(struct sw_flow *, void *),
299                                void *private)
300 {
301     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
302     int i;
303
304     for (i = position->private[1]; i < 2; i++) {
305         int error = table_hash_iterate(t2->subtable[i], key, position,
306                                        callback, private);
307         if (error) {
308             return error;
309         }
310         position->private[0] = 0;
311         position->private[1]++;
312     }
313     return 0;
314 }
315
316 static void table_hash2_stats(struct sw_table *swt,
317                               struct sw_table_stats *stats)
318 {
319     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
320     struct sw_table_stats substats[2];
321     int i;
322
323     for (i = 0; i < 2; i++)
324         table_hash_stats(t2->subtable[i], &substats[i]);
325     stats->name = "hash2";
326     stats->n_flows = substats[0].n_flows + substats[1].n_flows;
327     stats->max_flows = substats[0].max_flows + substats[1].max_flows;
328     stats->n_matched = swt->n_matched;
329 }
330
331 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
332                                     unsigned int poly1, unsigned int buckets1)
333
334 {
335     struct sw_table_hash2 *t2;
336     struct sw_table *swt;
337
338     t2 = malloc(sizeof *t2);
339     if (t2 == NULL)
340         return NULL;
341     memset(t2, '\0', sizeof *t2);
342
343     t2->subtable[0] = table_hash_create(poly0, buckets0);
344     if (t2->subtable[0] == NULL)
345         goto out_free_t2;
346
347     t2->subtable[1] = table_hash_create(poly1, buckets1);
348     if (t2->subtable[1] == NULL)
349         goto out_free_subtable0;
350
351     swt = &t2->swt;
352     swt->lookup = table_hash2_lookup;
353     swt->insert = table_hash2_insert;
354     swt->delete = table_hash2_delete;
355     swt->timeout = table_hash2_timeout;
356     swt->destroy = table_hash2_destroy;
357     swt->iterate = table_hash2_iterate;
358     swt->stats = table_hash2_stats;
359
360     return swt;
361
362 out_free_subtable0:
363     table_hash_destroy(t2->subtable[0]);
364 out_free_t2:
365     free(t2);
366     return NULL;
367 }