Add support for OFPFC_MODIFY Flow Mod command.
[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, 
56             offsetof(struct sw_flow_key, wildcards));
57     return &th->buckets[crc & th->bucket_mask];
58 }
59
60 static struct sw_flow *table_hash_lookup(struct sw_table *swt,
61                                          const struct sw_flow_key *key)
62 {
63     struct sw_flow *flow = *find_bucket(swt, key);
64     return flow && !flow_compare(&flow->key.flow, &key->flow) ? flow : NULL;
65 }
66
67 static int table_hash_insert(struct sw_table *swt, struct sw_flow *flow)
68 {
69     struct sw_table_hash *th = (struct sw_table_hash *) swt;
70     struct sw_flow **bucket;
71     int retval;
72
73     if (flow->key.wildcards != 0)
74         return 0;
75
76     bucket = find_bucket(swt, &flow->key);
77     if (*bucket == NULL) {
78         th->n_flows++;
79         *bucket = flow;
80         retval = 1;
81     } else {
82         struct sw_flow *old_flow = *bucket;
83         if (!flow_compare(&old_flow->key.flow, &flow->key.flow)) {
84             /* Keep stats from the original flow */
85             flow->used = old_flow->used;
86             flow->created = old_flow->created;
87             flow->packet_count = old_flow->packet_count;
88             flow->byte_count = old_flow->byte_count;
89
90             *bucket = flow;
91             flow_free(old_flow);
92             retval = 1;
93         } else {
94             retval = 0;
95         }
96     }
97     return retval;
98 }
99
100 static int table_hash_modify(struct sw_table *swt, 
101         const struct sw_flow_key *key,
102         const struct ofp_action *actions, int n_actions) 
103 {
104     struct sw_table_hash *th = (struct sw_table_hash *) swt;
105     unsigned int count = 0;
106
107     if (key->wildcards == 0) {
108         struct sw_flow **bucket = find_bucket(swt, key);
109         struct sw_flow *flow = *bucket;
110         if (flow && flow_matches_1wild(&flow->key, key)) {
111             flow_replace_acts(flow, actions, n_actions);
112             count = 1;
113         }
114     } else {
115         unsigned int i;
116
117         for (i = 0; i <= th->bucket_mask; i++) {
118             struct sw_flow **bucket = &th->buckets[i];
119             struct sw_flow *flow = *bucket;
120             if (flow && flow_matches_1wild(&flow->key, key)) {
121                 flow_replace_acts(flow, actions, n_actions);
122                 count++;
123             }
124         }
125     }
126     return count;
127 }
128
129 /* Caller must update n_flows. */
130 static void
131 do_delete(struct sw_flow **bucket)
132 {
133     flow_free(*bucket);
134     *bucket = NULL;
135 }
136
137 /* Returns number of deleted flows.  We can igonre the priority
138  * argument, since all exact-match entries are the same (highest)
139  * priority. */
140 static int table_hash_delete(struct sw_table *swt,
141                              const struct sw_flow_key *key, 
142                              uint16_t priority, int strict)
143 {
144     struct sw_table_hash *th = (struct sw_table_hash *) swt;
145     unsigned int count = 0;
146
147     if (key->wildcards == 0) {
148         struct sw_flow **bucket = find_bucket(swt, key);
149         struct sw_flow *flow = *bucket;
150         if (flow && !flow_compare(&flow->key.flow, &key->flow)) {
151             do_delete(bucket);
152             count = 1;
153         }
154     } else {
155         unsigned int i;
156
157         for (i = 0; i <= th->bucket_mask; i++) {
158             struct sw_flow **bucket = &th->buckets[i];
159             struct sw_flow *flow = *bucket;
160             if (flow && flow_del_matches(&flow->key, key, strict)) {
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,
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         return flow ? callback(flow, private) : 0;
214     } else {
215         int i;
216
217         for (i = position->private[0]; i <= th->bucket_mask; i++) {
218             struct sw_flow *flow = th->buckets[i];
219             if (flow && flow_matches_1wild(&flow->key, key)) {
220                 int error = callback(flow, private);
221                 if (error) {
222                     position->private[0] = i + 1;
223                     return error;
224                 }
225             }
226         }
227         return 0;
228     }
229 }
230
231 static void table_hash_stats(struct sw_table *swt,
232                              struct sw_table_stats *stats) 
233 {
234     struct sw_table_hash *th = (struct sw_table_hash *) swt;
235     stats->name = "hash";
236     stats->wildcards = 0;        /* No wildcards are supported. */
237     stats->n_flows   = th->n_flows;
238     stats->max_flows = th->bucket_mask + 1;
239     stats->n_matched = swt->n_matched;
240 }
241
242 struct sw_table *table_hash_create(unsigned int polynomial,
243                                    unsigned int n_buckets)
244 {
245     struct sw_table_hash *th;
246     struct sw_table *swt;
247
248     th = malloc(sizeof *th);
249     if (th == NULL)
250         return NULL;
251     memset(th, '\0', sizeof *th);
252
253     assert(!(n_buckets & (n_buckets - 1)));
254     th->buckets = calloc(n_buckets, sizeof *th->buckets);
255     if (th->buckets == NULL) {
256         printf("failed to allocate %u buckets\n", n_buckets);
257         free(th);
258         return NULL;
259     }
260     th->n_flows = 0;
261     th->bucket_mask = n_buckets - 1;
262
263     swt = &th->swt;
264     swt->lookup = table_hash_lookup;
265     swt->insert = table_hash_insert;
266     swt->modify = table_hash_modify;
267     swt->delete = table_hash_delete;
268     swt->timeout = table_hash_timeout;
269     swt->destroy = table_hash_destroy;
270     swt->iterate = table_hash_iterate;
271     swt->stats = table_hash_stats;
272
273     crc32_init(&th->crc32, polynomial);
274
275     return swt;
276 }
277
278 /* Double-hashing table. */
279
280 struct sw_table_hash2 {
281     struct sw_table swt;
282     struct sw_table *subtable[2];
283 };
284
285 static struct sw_flow *table_hash2_lookup(struct sw_table *swt,
286                                           const struct sw_flow_key *key)
287 {
288     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
289     int i;
290         
291     for (i = 0; i < 2; i++) {
292         struct sw_flow *flow = *find_bucket(t2->subtable[i], key);
293         if (flow && !flow_compare(&flow->key.flow, &key->flow))
294             return flow;
295     }
296     return NULL;
297 }
298
299 static int table_hash2_insert(struct sw_table *swt, struct sw_flow *flow)
300 {
301     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
302
303     if (table_hash_insert(t2->subtable[0], flow))
304         return 1;
305     return table_hash_insert(t2->subtable[1], flow);
306 }
307
308 static int table_hash2_modify(struct sw_table *swt, 
309         const struct sw_flow_key *key,
310         const struct ofp_action *actions, int n_actions) 
311 {
312     struct sw_table_hash2 *t2 = (struct sw_table_hash2 *) swt;
313     return (table_hash_modify(t2->subtable[0], key, actions, n_actions)
314             + table_hash_modify(t2->subtable[1], key, actions, n_actions));
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_matched = swt->n_matched;
376 }
377
378 struct sw_table *table_hash2_create(unsigned int poly0, unsigned int buckets0,
379                                     unsigned int poly1, unsigned int buckets1)
380
381 {
382     struct sw_table_hash2 *t2;
383     struct sw_table *swt;
384
385     t2 = malloc(sizeof *t2);
386     if (t2 == NULL)
387         return NULL;
388     memset(t2, '\0', sizeof *t2);
389
390     t2->subtable[0] = table_hash_create(poly0, buckets0);
391     if (t2->subtable[0] == NULL)
392         goto out_free_t2;
393
394     t2->subtable[1] = table_hash_create(poly1, buckets1);
395     if (t2->subtable[1] == NULL)
396         goto out_free_subtable0;
397
398     swt = &t2->swt;
399     swt->lookup = table_hash2_lookup;
400     swt->insert = table_hash2_insert;
401     swt->modify = table_hash2_modify;
402     swt->delete = table_hash2_delete;
403     swt->timeout = table_hash2_timeout;
404     swt->destroy = table_hash2_destroy;
405     swt->iterate = table_hash2_iterate;
406     swt->stats = table_hash2_stats;
407
408     return swt;
409
410 out_free_subtable0:
411     table_hash_destroy(t2->subtable[0]);
412 out_free_t2:
413     free(t2);
414     return NULL;
415 }