159572b59016a0b9b0b3a401f6c32191ba4f848a
[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 struct kmem_cache *flow_stats_cache __read_mostly;
54
55 static u16 range_n_bytes(const struct sw_flow_key_range *range)
56 {
57         return range->end - range->start;
58 }
59
60 void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
61                        const struct sw_flow_mask *mask)
62 {
63         const long *m = (const long *)((const u8 *)&mask->key +
64                                 mask->range.start);
65         const long *s = (const long *)((const u8 *)src +
66                                 mask->range.start);
67         long *d = (long *)((u8 *)dst + mask->range.start);
68         int i;
69
70         /* The memory outside of the 'mask->range' are not set since
71          * further operations on 'dst' only uses contents within
72          * 'mask->range'.
73          */
74         for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
75                 *d++ = *s++ & *m++;
76 }
77
78 struct sw_flow *ovs_flow_alloc(void)
79 {
80         struct sw_flow *flow;
81         struct flow_stats *stats;
82         int node;
83
84         flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
85         if (!flow)
86                 return ERR_PTR(-ENOMEM);
87
88         flow->sf_acts = NULL;
89         flow->mask = NULL;
90         flow->stats_last_writer = NUMA_NO_NODE;
91
92         /* Initialize the default stat node. */
93         stats = kmem_cache_alloc_node(flow_stats_cache,
94                                       GFP_KERNEL | __GFP_ZERO, 0);
95         if (!stats)
96                 goto err;
97
98         spin_lock_init(&stats->lock);
99
100         RCU_INIT_POINTER(flow->stats[0], stats);
101
102         for_each_node(node)
103                 if (node != 0)
104                         RCU_INIT_POINTER(flow->stats[node], NULL);
105
106         return flow;
107 err:
108         kmem_cache_free(flow_cache, flow);
109         return ERR_PTR(-ENOMEM);
110 }
111
112 int ovs_flow_tbl_count(struct flow_table *table)
113 {
114         return table->count;
115 }
116
117 static struct flex_array *alloc_buckets(unsigned int n_buckets)
118 {
119         struct flex_array *buckets;
120         int i, err;
121
122         buckets = flex_array_alloc(sizeof(struct hlist_head),
123                                    n_buckets, GFP_KERNEL);
124         if (!buckets)
125                 return NULL;
126
127         err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
128         if (err) {
129                 flex_array_free(buckets);
130                 return NULL;
131         }
132
133         for (i = 0; i < n_buckets; i++)
134                 INIT_HLIST_HEAD((struct hlist_head *)
135                                         flex_array_get(buckets, i));
136
137         return buckets;
138 }
139
140 static void flow_free(struct sw_flow *flow)
141 {
142         int node;
143
144         kfree((struct sw_flow_actions __force *)flow->sf_acts);
145         for_each_node(node)
146                 if (flow->stats[node])
147                         kmem_cache_free(flow_stats_cache,
148                                         (struct flow_stats __force *)flow->stats[node]);
149         kmem_cache_free(flow_cache, flow);
150 }
151
152 static void rcu_free_flow_callback(struct rcu_head *rcu)
153 {
154         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
155
156         flow_free(flow);
157 }
158
159 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
160 {
161         struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
162
163         kfree(mask);
164 }
165
166 void ovs_flow_free(struct sw_flow *flow, bool deferred)
167 {
168         if (!flow)
169                 return;
170
171         if (deferred)
172                 call_rcu(&flow->rcu, rcu_free_flow_callback);
173         else
174                 flow_free(flow);
175 }
176
177 static void free_buckets(struct flex_array *buckets)
178 {
179         flex_array_free(buckets);
180 }
181
182
183 static void __table_instance_destroy(struct table_instance *ti)
184 {
185         free_buckets(ti->buckets);
186         kfree(ti);
187 }
188
189 static struct table_instance *table_instance_alloc(int new_size)
190 {
191         struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
192
193         if (!ti)
194                 return NULL;
195
196         ti->buckets = alloc_buckets(new_size);
197
198         if (!ti->buckets) {
199                 kfree(ti);
200                 return NULL;
201         }
202         ti->n_buckets = new_size;
203         ti->node_ver = 0;
204         ti->keep_flows = false;
205         get_random_bytes(&ti->hash_seed, sizeof(u32));
206
207         return ti;
208 }
209
210 int ovs_flow_tbl_init(struct flow_table *table)
211 {
212         struct table_instance *ti;
213
214         ti = table_instance_alloc(TBL_MIN_BUCKETS);
215
216         if (!ti)
217                 return -ENOMEM;
218
219         rcu_assign_pointer(table->ti, ti);
220         INIT_LIST_HEAD(&table->mask_list);
221         table->last_rehash = jiffies;
222         table->count = 0;
223         return 0;
224 }
225
226 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
227 {
228         struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
229
230         __table_instance_destroy(ti);
231 }
232
233 static void table_instance_destroy(struct table_instance *ti, bool deferred)
234 {
235         int i;
236
237         if (!ti)
238                 return;
239
240         if (ti->keep_flows)
241                 goto skip_flows;
242
243         for (i = 0; i < ti->n_buckets; i++) {
244                 struct sw_flow *flow;
245                 struct hlist_head *head = flex_array_get(ti->buckets, i);
246                 struct hlist_node *n;
247                 int ver = ti->node_ver;
248
249                 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
250                         hlist_del_rcu(&flow->hash_node[ver]);
251                         ovs_flow_free(flow, deferred);
252                 }
253         }
254
255 skip_flows:
256         if (deferred)
257                 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
258         else
259                 __table_instance_destroy(ti);
260 }
261
262 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
263 {
264         struct table_instance *ti = ovsl_dereference(table->ti);
265
266         table_instance_destroy(ti, deferred);
267 }
268
269 struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
270                                        u32 *bucket, u32 *last)
271 {
272         struct sw_flow *flow;
273         struct hlist_head *head;
274         int ver;
275         int i;
276
277         ver = ti->node_ver;
278         while (*bucket < ti->n_buckets) {
279                 i = 0;
280                 head = flex_array_get(ti->buckets, *bucket);
281                 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
282                         if (i < *last) {
283                                 i++;
284                                 continue;
285                         }
286                         *last = i + 1;
287                         return flow;
288                 }
289                 (*bucket)++;
290                 *last = 0;
291         }
292
293         return NULL;
294 }
295
296 static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
297 {
298         hash = jhash_1word(hash, ti->hash_seed);
299         return flex_array_get(ti->buckets,
300                                 (hash & (ti->n_buckets - 1)));
301 }
302
303 static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow)
304 {
305         struct hlist_head *head;
306
307         head = find_bucket(ti, flow->hash);
308         hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head);
309 }
310
311 static void flow_table_copy_flows(struct table_instance *old,
312                                   struct table_instance *new)
313 {
314         int old_ver;
315         int i;
316
317         old_ver = old->node_ver;
318         new->node_ver = !old_ver;
319
320         /* Insert in new table. */
321         for (i = 0; i < old->n_buckets; i++) {
322                 struct sw_flow *flow;
323                 struct hlist_head *head;
324
325                 head = flex_array_get(old->buckets, i);
326
327                 hlist_for_each_entry(flow, head, hash_node[old_ver])
328                         table_instance_insert(new, flow);
329         }
330
331         old->keep_flows = true;
332 }
333
334 static struct table_instance *table_instance_rehash(struct table_instance *ti,
335                                             int n_buckets)
336 {
337         struct table_instance *new_ti;
338
339         new_ti = table_instance_alloc(n_buckets);
340         if (!new_ti)
341                 return NULL;
342
343         flow_table_copy_flows(ti, new_ti);
344
345         return new_ti;
346 }
347
348 int ovs_flow_tbl_flush(struct flow_table *flow_table)
349 {
350         struct table_instance *old_ti;
351         struct table_instance *new_ti;
352
353         old_ti = ovsl_dereference(flow_table->ti);
354         new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
355         if (!new_ti)
356                 return -ENOMEM;
357
358         rcu_assign_pointer(flow_table->ti, new_ti);
359         flow_table->last_rehash = jiffies;
360         flow_table->count = 0;
361
362         table_instance_destroy(old_ti, true);
363         return 0;
364 }
365
366 static u32 flow_hash(const struct sw_flow_key *key, int key_start,
367                      int key_end)
368 {
369         const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
370         int hash_u32s = (key_end - key_start) >> 2;
371
372         /* Make sure number of hash bytes are multiple of u32. */
373         BUILD_BUG_ON(sizeof(long) % sizeof(u32));
374
375         return arch_fast_hash2(hash_key, hash_u32s, 0);
376 }
377
378 static int flow_key_start(const struct sw_flow_key *key)
379 {
380         if (key->tun_key.ipv4_dst)
381                 return 0;
382         else
383                 return rounddown(offsetof(struct sw_flow_key, phy),
384                                           sizeof(long));
385 }
386
387 static bool cmp_key(const struct sw_flow_key *key1,
388                     const struct sw_flow_key *key2,
389                     int key_start, int key_end)
390 {
391         const long *cp1 = (const long *)((const u8 *)key1 + key_start);
392         const long *cp2 = (const long *)((const u8 *)key2 + key_start);
393         long diffs = 0;
394         int i;
395
396         for (i = key_start; i < key_end;  i += sizeof(long))
397                 diffs |= *cp1++ ^ *cp2++;
398
399         return diffs == 0;
400 }
401
402 static bool flow_cmp_masked_key(const struct sw_flow *flow,
403                                 const struct sw_flow_key *key,
404                                 int key_start, int key_end)
405 {
406         return cmp_key(&flow->key, key, key_start, key_end);
407 }
408
409 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
410                                struct sw_flow_match *match)
411 {
412         struct sw_flow_key *key = match->key;
413         int key_start = flow_key_start(key);
414         int key_end = match->range.end;
415
416         return cmp_key(&flow->unmasked_key, key, key_start, key_end);
417 }
418
419 static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
420                                           const struct sw_flow_key *unmasked,
421                                           struct sw_flow_mask *mask)
422 {
423         struct sw_flow *flow;
424         struct hlist_head *head;
425         int key_start = mask->range.start;
426         int key_end = mask->range.end;
427         u32 hash;
428         struct sw_flow_key masked_key;
429
430         ovs_flow_mask_key(&masked_key, unmasked, mask);
431         hash = flow_hash(&masked_key, key_start, key_end);
432         head = find_bucket(ti, hash);
433         hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) {
434                 if (flow->mask == mask && flow->hash == hash &&
435                     flow_cmp_masked_key(flow, &masked_key,
436                                           key_start, key_end))
437                         return flow;
438         }
439         return NULL;
440 }
441
442 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
443                                     const struct sw_flow_key *key,
444                                     u32 *n_mask_hit)
445 {
446         struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
447         struct sw_flow_mask *mask;
448         struct sw_flow *flow;
449
450         *n_mask_hit = 0;
451         list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
452                 (*n_mask_hit)++;
453                 flow = masked_flow_lookup(ti, key, mask);
454                 if (flow)  /* Found */
455                         return flow;
456         }
457         return NULL;
458 }
459
460 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
461                                     const struct sw_flow_key *key)
462 {
463         u32 __always_unused n_mask_hit;
464
465         return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
466 }
467
468 int ovs_flow_tbl_num_masks(const struct flow_table *table)
469 {
470         struct sw_flow_mask *mask;
471         int num = 0;
472
473         list_for_each_entry(mask, &table->mask_list, list)
474                 num++;
475
476         return num;
477 }
478
479 static struct table_instance *table_instance_expand(struct table_instance *ti)
480 {
481         return table_instance_rehash(ti, ti->n_buckets * 2);
482 }
483
484 /* Remove 'mask' from the mask list, if it is not needed any more. */
485 static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
486 {
487         if (mask) {
488                 /* ovs-lock is required to protect mask-refcount and
489                  * mask list.
490                  */
491                 ASSERT_OVSL();
492                 BUG_ON(!mask->ref_count);
493                 mask->ref_count--;
494
495                 if (!mask->ref_count) {
496                         list_del_rcu(&mask->list);
497                         call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
498                 }
499         }
500 }
501
502 /* Must be called with OVS mutex held. */
503 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
504 {
505         struct table_instance *ti = ovsl_dereference(table->ti);
506
507         BUG_ON(table->count == 0);
508         hlist_del_rcu(&flow->hash_node[ti->node_ver]);
509         table->count--;
510
511         /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
512          * accessible as long as the RCU read lock is held. */
513         flow_mask_remove(table, flow->mask);
514 }
515
516 static struct sw_flow_mask *mask_alloc(void)
517 {
518         struct sw_flow_mask *mask;
519
520         mask = kmalloc(sizeof(*mask), GFP_KERNEL);
521         if (mask)
522                 mask->ref_count = 1;
523
524         return mask;
525 }
526
527 static bool mask_equal(const struct sw_flow_mask *a,
528                        const struct sw_flow_mask *b)
529 {
530         const u8 *a_ = (const u8 *)&a->key + a->range.start;
531         const u8 *b_ = (const u8 *)&b->key + b->range.start;
532
533         return  (a->range.end == b->range.end)
534                 && (a->range.start == b->range.start)
535                 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
536 }
537
538 static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
539                                            const struct sw_flow_mask *mask)
540 {
541         struct list_head *ml;
542
543         list_for_each(ml, &tbl->mask_list) {
544                 struct sw_flow_mask *m;
545                 m = container_of(ml, struct sw_flow_mask, list);
546                 if (mask_equal(mask, m))
547                         return m;
548         }
549
550         return NULL;
551 }
552
553 /* Add 'mask' into the mask list, if it is not already there. */
554 static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
555                             struct sw_flow_mask *new)
556 {
557         struct sw_flow_mask *mask;
558         mask = flow_mask_find(tbl, new);
559         if (!mask) {
560                 /* Allocate a new mask if none exsits. */
561                 mask = mask_alloc();
562                 if (!mask)
563                         return -ENOMEM;
564                 mask->key = new->key;
565                 mask->range = new->range;
566                 list_add_rcu(&mask->list, &tbl->mask_list);
567         } else {
568                 BUG_ON(!mask->ref_count);
569                 mask->ref_count++;
570         }
571
572         flow->mask = mask;
573         return 0;
574 }
575
576 /* Must be called with OVS mutex held. */
577 int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
578                         struct sw_flow_mask *mask)
579 {
580         struct table_instance *new_ti = NULL;
581         struct table_instance *ti;
582         int err;
583
584         err = flow_mask_insert(table, flow, mask);
585         if (err)
586                 return err;
587
588         flow->hash = flow_hash(&flow->key, flow->mask->range.start,
589                         flow->mask->range.end);
590         ti = ovsl_dereference(table->ti);
591         table_instance_insert(ti, flow);
592         table->count++;
593
594         /* Expand table, if necessary, to make room. */
595         if (table->count > ti->n_buckets)
596                 new_ti = table_instance_expand(ti);
597         else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
598                 new_ti = table_instance_rehash(ti, ti->n_buckets);
599
600         if (new_ti) {
601                 rcu_assign_pointer(table->ti, new_ti);
602                 table_instance_destroy(ti, true);
603                 table->last_rehash = jiffies;
604         }
605         return 0;
606 }
607
608 /* Initializes the flow module.
609  * Returns zero if successful or a negative error code. */
610 int ovs_flow_init(void)
611 {
612         BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
613         BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
614
615         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
616                                        + (num_possible_nodes()
617                                           * sizeof(struct flow_stats *)),
618                                        0, 0, NULL);
619         if (flow_cache == NULL)
620                 return -ENOMEM;
621
622         flow_stats_cache
623                 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
624                                     0, SLAB_HWCACHE_ALIGN, NULL);
625         if (flow_stats_cache == NULL) {
626                 kmem_cache_destroy(flow_cache);
627                 flow_cache = NULL;
628                 return -ENOMEM;
629         }
630
631         return 0;
632 }
633
634 /* Uninitializes the flow module. */
635 void ovs_flow_exit(void)
636 {
637         kmem_cache_destroy(flow_stats_cache);
638         kmem_cache_destroy(flow_cache);
639 }