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