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