Add support for vendor-defined and variable-length actions.
[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 priority, int strict)
140 {
141     struct sw_table_hash *th = (struct sw_table_hash *) swt;
142     unsigned int count = 0;
143
144     if (key->wildcards == 0) {
145         struct sw_flow **bucket = find_bucket(swt, key);
146         struct sw_flow *flow = *bucket;
147         if (flow && !flow_compare(&flow->key.flow, &key->flow)) {
148             do_delete(bucket);
149             count = 1;
150         }
151     } else {
152         unsigned int i;
153
154         for (i = 0; i <= th->bucket_mask; i++) {
155             struct sw_flow **bucket = &th->buckets[i];
156             struct sw_flow *flow = *bucket;
157             if (flow && flow_matches_desc(&flow->key, key, strict)) {
158                 do_delete(bucket);
159                 count++;
160             }
161         }
162     }
163     th->n_flows -= count;
164     return count;
165 }
166
167 static void table_hash_timeout(struct sw_table *swt, struct list *deleted)
168 {
169     struct sw_table_hash *th = (struct sw_table_hash *) swt;
170     unsigned int i;
171
172     for (i = 0; i <= th->bucket_mask; i++) {
173         struct sw_flow **bucket = &th->buckets[i];
174         struct sw_flow *flow = *bucket;
175         if (flow && flow_timeout(flow)) {
176             list_push_back(deleted, &flow->node);
177             *bucket = NULL;
178             th->n_flows--;
179         }
180     }
181 }
182
183 static void table_hash_destroy(struct sw_table *swt)
184 {
185     struct sw_table_hash *th = (struct sw_table_hash *) swt;
186     unsigned int i;
187     for (i = 0; i <= th->bucket_mask; i++) {
188         if (th->buckets[i]) {
189             flow_free(th->buckets[i]); 
190         }
191     }
192     free(th->buckets);
193     free(th);
194 }
195
196 static int table_hash_iterate(struct sw_table *swt,
197                               const struct sw_flow_key *key,
198                               struct sw_table_position *position,
199                               int (*callback)(struct sw_flow *, void *private),
200                               void *private) 
201 {
202     struct sw_table_hash *th = (struct sw_table_hash *) swt;
203
204     if (position->private[0] > th->bucket_mask)
205         return 0;
206
207     if (key->wildcards == 0) {
208         struct sw_flow *flow = table_hash_lookup(swt, key);
209         position->private[0] = -1;
210         return flow ? callback(flow, private) : 0;
211     } else {
212         int i;
213
214         for (i = position->private[0]; i <= th->bucket_mask; i++) {
215             struct sw_flow *flow = th->buckets[i];
216             if (flow && flow_matches_1wild(&flow->key, key)) {
217                 int error = callback(flow, private);
218                 if (error) {
219                     position->private[0] = i + 1;
220                     return error;
221                 }
222             }
223         }
224         return 0;
225     }
226 }
227
228 static void table_hash_stats(struct sw_table *swt,
229                              struct sw_table_stats *stats) 
230 {
231     struct sw_table_hash *th = (struct sw_table_hash *) swt;
232     stats->name = "hash";
233     stats->wildcards = 0;        /* No wildcards are supported. */
234     stats->n_flows   = th->n_flows;
235     stats->max_flows = th->bucket_mask + 1;
236     stats->n_lookup  = swt->n_lookup;
237     stats->n_matched = swt->n_matched;
238 }
239
240 struct sw_table *table_hash_create(unsigned int polynomial,
241                                    unsigned int n_buckets)
242 {
243     struct sw_table_hash *th;
244     struct sw_table *swt;
245
246     th = malloc(sizeof *th);
247     if (th == NULL)
248         return NULL;
249     memset(th, '\0', sizeof *th);
250
251     assert(!(n_buckets & (n_buckets - 1)));
252     th->buckets = calloc(n_buckets, sizeof *th->buckets);
253     if (th->buckets == NULL) {
254         printf("failed to allocate %u buckets\n", n_buckets);
255         free(th);
256         return NULL;
257     }
258     th->n_flows = 0;
259     th->bucket_mask = n_buckets - 1;
260
261     swt = &th->swt;
262     swt->lookup = table_hash_lookup;
263     swt->insert = table_hash_insert;
264     swt->modify = table_hash_modify;
265     swt->delete = table_hash_delete;
266     swt->timeout = table_hash_timeout;
267     swt->destroy = table_hash_destroy;
268     swt->iterate = table_hash_iterate;
269     swt->stats = table_hash_stats;
270
271     crc32_init(&th->crc32, polynomial);
272
273     return swt;
274 }
275
276 /* Double-hashing table. */
277
278 struct sw_table_hash2 {
279     struct sw_table swt;
280     struct sw_table *subtable[2];
281 };
282
283 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
284                                           const struct sw_flow_key *key)
285 {
286     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
287     int i;
288         
289     for (i = 0; i < 2; i++) {
290         struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
291         if (flow && !flow_compare(&flow->key.flow, &key->flow))
292             return flow;
293     }
294     return NULL;
295 }
296
297 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
298 {
299     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
300
301     if (table_hash_insert(t2->subtable[0], flow))
302         return 1;
303     return table_hash_insert(t2->subtable[1], flow);
304 }
305
306 static int table_hash2_modify(struct sw_table *swt, 
307         const struct sw_flow_key *key, uint16_t priority, int strict,
308         const struct ofp_action_header *actions, size_t actions_len) 
309 {
310     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
311     return (table_hash_modify(t2->subtable[0], key, priority, strict,
312                     actions, actions_len)
313             + table_hash_modify(t2->subtable[1], key, priority, strict,
314                     actions, actions_len));
315 }
316
317 static int table_hash2_delete(struct sw_table *swt,
318                               const struct sw_flow_key *key, 
319                               uint16_t priority, int strict)
320 {
321     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
322     return (table_hash_delete(t2->subtable[0], key, priority, strict)
323             + table_hash_delete(t2->subtable[1], key, priority, strict));
324 }
325
326 static void table_hash2_timeout(struct sw_table *swt, struct list *deleted)
327 {
328     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
329     table_hash_timeout(t2->subtable[0], deleted);
330     table_hash_timeout(t2->subtable[1], deleted);
331 }
332
333 static void table_hash2_destroy(struct sw_table *swt)
334 {
335     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
336     table_hash_destroy(t2->subtable[0]);
337     table_hash_destroy(t2->subtable[1]);
338     free(t2);
339 }
340
341 static int table_hash2_iterate(struct sw_table *swt,
342                                const struct sw_flow_key *key,
343                                struct sw_table_position *position,
344                                int (*callback)(struct sw_flow *, void *),
345                                void *private)
346 {
347     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
348     int i;
349
350     for (i = position->private[1]; i < 2; i++) {
351         int error = table_hash_iterate(t2->subtable[i], key, position,
352                                        callback, private);
353         if (error) {
354             return error;
355         }
356         position->private[0] = 0;
357         position->private[1]++;
358     }
359     return 0;
360 }
361
362 static void table_hash2_stats(struct sw_table *swt,
363                               struct sw_table_stats *stats)
364 {
365     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
366     struct sw_table_stats substats[2];
367     int i;
368
369     for (i = 0; i < 2; i++)
370         table_hash_stats(t2->subtable[i], &substats[i]);
371     stats->name = "hash2";
372     stats->wildcards = 0;        /* No wildcards are supported. */
373     stats->n_flows   = substats[0].n_flows + substats[1].n_flows;
374     stats->max_flows = substats[0].max_flows + substats[1].max_flows;
375     stats->n_lookup  = swt->n_lookup;
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 }