datapath: Remove 5-tuple optimization.
[sliver-openvswitch.git] / datapath / flow_table.c
1 /*
2  * Copyright (c) 2007-2013 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #include "flow.h"
20 #include "datapath.h"
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/hash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
32 #include <linux/in.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/sctp.h>
38 #include <linux/tcp.h>
39 #include <linux/udp.h>
40 #include <linux/icmp.h>
41 #include <linux/icmpv6.h>
42 #include <linux/rculist.h>
43 #include <net/ip.h>
44 #include <net/ipv6.h>
45 #include <net/ndisc.h>
46
47 #include "vlan.h"
48
49 #define TBL_MIN_BUCKETS         1024
50 #define REHASH_INTERVAL         (10 * 60 * HZ)
51
52 static struct kmem_cache *flow_cache;
53
54 static u16 range_n_bytes(const struct sw_flow_key_range *range)
55 {
56         return range->end - range->start;
57 }
58
59 void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
60                        const struct sw_flow_mask *mask)
61 {
62         const long *m = (const long *)((const u8 *)&mask->key +
63                                 mask->range.start);
64         const long *s = (const long *)((const u8 *)src +
65                                 mask->range.start);
66         long *d = (long *)((u8 *)dst + mask->range.start);
67         int i;
68
69         /* The memory outside of the 'mask->range' are not set since
70          * further operations on 'dst' only uses contents within
71          * 'mask->range'.
72          */
73         for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
74                 *d++ = *s++ & *m++;
75 }
76
77 struct sw_flow *ovs_flow_alloc(void)
78 {
79         struct sw_flow *flow;
80         int cpu;
81
82         flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
83         if (!flow)
84                 return ERR_PTR(-ENOMEM);
85
86         flow->sf_acts = NULL;
87         flow->mask = NULL;
88
89         flow->stats = alloc_percpu(struct flow_stats);
90         if (!flow->stats)
91                 goto err;
92
93         for_each_possible_cpu(cpu) {
94                 struct flow_stats *cpu_stats;
95
96                 cpu_stats = per_cpu_ptr(flow->stats, cpu);
97                 spin_lock_init(&cpu_stats->lock);
98         }
99         return flow;
100 err:
101         kmem_cache_free(flow_cache, flow);
102         return ERR_PTR(-ENOMEM);
103 }
104
105 int ovs_flow_tbl_count(struct flow_table *table)
106 {
107         return table->count;
108 }
109
110 static struct flex_array *alloc_buckets(unsigned int n_buckets)
111 {
112         struct flex_array *buckets;
113         int i, err;
114
115         buckets = flex_array_alloc(sizeof(struct hlist_head),
116                                    n_buckets, GFP_KERNEL);
117         if (!buckets)
118                 return NULL;
119
120         err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
121         if (err) {
122                 flex_array_free(buckets);
123                 return NULL;
124         }
125
126         for (i = 0; i < n_buckets; i++)
127                 INIT_HLIST_HEAD((struct hlist_head *)
128                                         flex_array_get(buckets, i));
129
130         return buckets;
131 }
132
133 static void flow_free(struct sw_flow *flow)
134 {
135         kfree((struct sf_flow_acts __force *)flow->sf_acts);
136         free_percpu(flow->stats);
137         kmem_cache_free(flow_cache, flow);
138 }
139
140 static void rcu_free_flow_callback(struct rcu_head *rcu)
141 {
142         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
143
144         flow_free(flow);
145 }
146
147 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
148 {
149         struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
150
151         kfree(mask);
152 }
153
154 void ovs_flow_free(struct sw_flow *flow, bool deferred)
155 {
156         if (!flow)
157                 return;
158
159         if (flow->mask) {
160                 struct sw_flow_mask *mask = flow->mask;
161
162                 /* ovs-lock is required to protect mask-refcount and
163                  * mask list.
164                  */
165                 ASSERT_OVSL();
166                 BUG_ON(!mask->ref_count);
167                 mask->ref_count--;
168
169                 if (!mask->ref_count) {
170                         list_del_rcu(&mask->list);
171                         if (deferred)
172                                 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
173                         else
174                                 kfree(mask);
175                 }
176         }
177
178         if (deferred)
179                 call_rcu(&flow->rcu, rcu_free_flow_callback);
180         else
181                 flow_free(flow);
182 }
183
184 static void free_buckets(struct flex_array *buckets)
185 {
186         flex_array_free(buckets);
187 }
188
189
190 static void __table_instance_destroy(struct table_instance *ti)
191 {
192         free_buckets(ti->buckets);
193         kfree(ti);
194 }
195
196 static struct table_instance *table_instance_alloc(int new_size)
197 {
198         struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
199
200         if (!ti)
201                 return NULL;
202
203         ti->buckets = alloc_buckets(new_size);
204
205         if (!ti->buckets) {
206                 kfree(ti);
207                 return NULL;
208         }
209         ti->n_buckets = new_size;
210         ti->node_ver = 0;
211         ti->keep_flows = false;
212         get_random_bytes(&ti->hash_seed, sizeof(u32));
213
214         return ti;
215 }
216
217 int ovs_flow_tbl_init(struct flow_table *table)
218 {
219         struct table_instance *ti;
220
221         ti = table_instance_alloc(TBL_MIN_BUCKETS);
222
223         if (!ti)
224                 return -ENOMEM;
225
226         rcu_assign_pointer(table->ti, ti);
227         INIT_LIST_HEAD(&table->mask_list);
228         table->last_rehash = jiffies;
229         table->count = 0;
230         return 0;
231 }
232
233 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
234 {
235         struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
236
237         __table_instance_destroy(ti);
238 }
239
240 static void table_instance_destroy(struct table_instance *ti, bool deferred)
241 {
242         int i;
243
244         if (!ti)
245                 return;
246
247         if (ti->keep_flows)
248                 goto skip_flows;
249
250         for (i = 0; i < ti->n_buckets; i++) {
251                 struct sw_flow *flow;
252                 struct hlist_head *head = flex_array_get(ti->buckets, i);
253                 struct hlist_node *n;
254                 int ver = ti->node_ver;
255
256                 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
257                         hlist_del_rcu(&flow->hash_node[ver]);
258                         ovs_flow_free(flow, deferred);
259                 }
260         }
261
262 skip_flows:
263         if (deferred)
264                 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
265         else
266                 __table_instance_destroy(ti);
267 }
268
269 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
270 {
271         struct table_instance *ti = ovsl_dereference(table->ti);
272
273         table_instance_destroy(ti, deferred);
274 }
275
276 struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
277                                        u32 *bucket, u32 *last)
278 {
279         struct sw_flow *flow;
280         struct hlist_head *head;
281         int ver;
282         int i;
283
284         ver = ti->node_ver;
285         while (*bucket < ti->n_buckets) {
286                 i = 0;
287                 head = flex_array_get(ti->buckets, *bucket);
288                 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
289                         if (i < *last) {
290                                 i++;
291                                 continue;
292                         }
293                         *last = i + 1;
294                         return flow;
295                 }
296                 (*bucket)++;
297                 *last = 0;
298         }
299
300         return NULL;
301 }
302
303 static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
304 {
305         hash = jhash_1word(hash, ti->hash_seed);
306         return flex_array_get(ti->buckets,
307                                 (hash & (ti->n_buckets - 1)));
308 }
309
310 static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow)
311 {
312         struct hlist_head *head;
313
314         head = find_bucket(ti, flow->hash);
315         hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head);
316 }
317
318 static void flow_table_copy_flows(struct table_instance *old,
319                                   struct table_instance *new)
320 {
321         int old_ver;
322         int i;
323
324         old_ver = old->node_ver;
325         new->node_ver = !old_ver;
326
327         /* Insert in new table. */
328         for (i = 0; i < old->n_buckets; i++) {
329                 struct sw_flow *flow;
330                 struct hlist_head *head;
331
332                 head = flex_array_get(old->buckets, i);
333
334                 hlist_for_each_entry(flow, head, hash_node[old_ver])
335                         table_instance_insert(new, flow);
336         }
337
338         old->keep_flows = true;
339 }
340
341 static struct table_instance *table_instance_rehash(struct table_instance *ti,
342                                             int n_buckets)
343 {
344         struct table_instance *new_ti;
345
346         new_ti = table_instance_alloc(n_buckets);
347         if (!new_ti)
348                 return NULL;
349
350         flow_table_copy_flows(ti, new_ti);
351
352         return new_ti;
353 }
354
355 int ovs_flow_tbl_flush(struct flow_table *flow_table)
356 {
357         struct table_instance *old_ti;
358         struct table_instance *new_ti;
359
360         old_ti = ovsl_dereference(flow_table->ti);
361         new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
362         if (!new_ti)
363                 return -ENOMEM;
364
365         rcu_assign_pointer(flow_table->ti, new_ti);
366         flow_table->last_rehash = jiffies;
367         flow_table->count = 0;
368
369         table_instance_destroy(old_ti, true);
370         return 0;
371 }
372
373 static u32 flow_hash(const struct sw_flow_key *key, int key_start,
374                      int key_end)
375 {
376         const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
377         int hash_u32s = (key_end - key_start) >> 2;
378
379         /* Make sure number of hash bytes are multiple of u32. */
380         BUILD_BUG_ON(sizeof(long) % sizeof(u32));
381
382         return arch_fast_hash2(hash_key, hash_u32s, 0);
383 }
384
385 static int flow_key_start(const struct sw_flow_key *key)
386 {
387         if (key->tun_key.ipv4_dst)
388                 return 0;
389         else
390                 return rounddown(offsetof(struct sw_flow_key, phy),
391                                           sizeof(long));
392 }
393
394 static bool cmp_key(const struct sw_flow_key *key1,
395                     const struct sw_flow_key *key2,
396                     int key_start, int key_end)
397 {
398         const long *cp1 = (const long *)((const u8 *)key1 + key_start);
399         const long *cp2 = (const long *)((const u8 *)key2 + key_start);
400         long diffs = 0;
401         int i;
402
403         for (i = key_start; i < key_end;  i += sizeof(long))
404                 diffs |= *cp1++ ^ *cp2++;
405
406         return diffs == 0;
407 }
408
409 static bool flow_cmp_masked_key(const struct sw_flow *flow,
410                                 const struct sw_flow_key *key,
411                                 int key_start, int key_end)
412 {
413         return cmp_key(&flow->key, key, key_start, key_end);
414 }
415
416 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
417                                struct sw_flow_match *match)
418 {
419         struct sw_flow_key *key = match->key;
420         int key_start = flow_key_start(key);
421         int key_end = match->range.end;
422
423         return cmp_key(&flow->unmasked_key, key, key_start, key_end);
424 }
425
426 static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
427                                           const struct sw_flow_key *unmasked,
428                                           struct sw_flow_mask *mask)
429 {
430         struct sw_flow *flow;
431         struct hlist_head *head;
432         int key_start = mask->range.start;
433         int key_end = mask->range.end;
434         u32 hash;
435         struct sw_flow_key masked_key;
436
437         ovs_flow_mask_key(&masked_key, unmasked, mask);
438         hash = flow_hash(&masked_key, key_start, key_end);
439         head = find_bucket(ti, hash);
440         hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) {
441                 if (flow->mask == mask && flow->hash == hash &&
442                     flow_cmp_masked_key(flow, &masked_key,
443                                           key_start, key_end))
444                         return flow;
445         }
446         return NULL;
447 }
448
449 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
450                                     const struct sw_flow_key *key,
451                                     u32 *n_mask_hit)
452 {
453         struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
454         struct sw_flow_mask *mask;
455         struct sw_flow *flow;
456
457         *n_mask_hit = 0;
458         list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
459                 (*n_mask_hit)++;
460                 flow = masked_flow_lookup(ti, key, mask);
461                 if (flow)  /* Found */
462                         return flow;
463         }
464         return NULL;
465 }
466
467 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
468                                     const struct sw_flow_key *key)
469 {
470         u32 __always_unused n_mask_hit;
471
472         return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
473 }
474
475 int ovs_flow_tbl_num_masks(const struct flow_table *table)
476 {
477         struct sw_flow_mask *mask;
478         int num = 0;
479
480         list_for_each_entry(mask, &table->mask_list, list)
481                 num++;
482
483         return num;
484 }
485
486 static struct table_instance *table_instance_expand(struct table_instance *ti)
487 {
488         return table_instance_rehash(ti, ti->n_buckets * 2);
489 }
490
491 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
492 {
493         struct table_instance *ti = ovsl_dereference(table->ti);
494
495         BUG_ON(table->count == 0);
496         hlist_del_rcu(&flow->hash_node[ti->node_ver]);
497         table->count--;
498 }
499
500 static struct sw_flow_mask *mask_alloc(void)
501 {
502         struct sw_flow_mask *mask;
503
504         mask = kmalloc(sizeof(*mask), GFP_KERNEL);
505         if (mask)
506                 mask->ref_count = 1;
507
508         return mask;
509 }
510
511 static bool mask_equal(const struct sw_flow_mask *a,
512                        const struct sw_flow_mask *b)
513 {
514         const u8 *a_ = (const u8 *)&a->key + a->range.start;
515         const u8 *b_ = (const u8 *)&b->key + b->range.start;
516
517         return  (a->range.end == b->range.end)
518                 && (a->range.start == b->range.start)
519                 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
520 }
521
522 static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
523                                            const struct sw_flow_mask *mask)
524 {
525         struct list_head *ml;
526
527         list_for_each(ml, &tbl->mask_list) {
528                 struct sw_flow_mask *m;
529                 m = container_of(ml, struct sw_flow_mask, list);
530                 if (mask_equal(mask, m))
531                         return m;
532         }
533
534         return NULL;
535 }
536
537 /* Add 'mask' into the mask list, if it is not already there. */
538 static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
539                             struct sw_flow_mask *new)
540 {
541         struct sw_flow_mask *mask;
542         mask = flow_mask_find(tbl, new);
543         if (!mask) {
544                 /* Allocate a new mask if none exsits. */
545                 mask = mask_alloc();
546                 if (!mask)
547                         return -ENOMEM;
548                 mask->key = new->key;
549                 mask->range = new->range;
550                 list_add_rcu(&mask->list, &tbl->mask_list);
551         } else {
552                 BUG_ON(!mask->ref_count);
553                 mask->ref_count++;
554         }
555
556         flow->mask = mask;
557         return 0;
558 }
559
560 int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
561                         struct sw_flow_mask *mask)
562 {
563         struct table_instance *new_ti = NULL;
564         struct table_instance *ti;
565         int err;
566
567         err = flow_mask_insert(table, flow, mask);
568         if (err)
569                 return err;
570
571         flow->hash = flow_hash(&flow->key, flow->mask->range.start,
572                         flow->mask->range.end);
573         ti = ovsl_dereference(table->ti);
574         table_instance_insert(ti, flow);
575         table->count++;
576
577         /* Expand table, if necessary, to make room. */
578         if (table->count > ti->n_buckets)
579                 new_ti = table_instance_expand(ti);
580         else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
581                 new_ti = table_instance_rehash(ti, ti->n_buckets);
582
583         if (new_ti) {
584                 rcu_assign_pointer(table->ti, new_ti);
585                 table_instance_destroy(ti, true);
586                 table->last_rehash = jiffies;
587         }
588         return 0;
589 }
590
591 /* Initializes the flow module.
592  * Returns zero if successful or a negative error code. */
593 int ovs_flow_init(void)
594 {
595         BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
596         BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
597
598         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
599                                         0, NULL);
600         if (flow_cache == NULL)
601                 return -ENOMEM;
602
603         return 0;
604 }
605
606 /* Uninitializes the flow module. */
607 void ovs_flow_exit(void)
608 {
609         kmem_cache_destroy(flow_cache);
610 }