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