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