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