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