Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / net / openvswitch / flow_table.c
blobd073fff82fdb8c6c8d39b57690d37eedeb12423a
1 /*
2 * Copyright (c) 2007-2014 Nicira, Inc.
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.
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.
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
19 #include "flow.h"
20 #include "datapath.h"
21 #include "flow_netlink.h"
22 #include <linux/uaccess.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/if_ether.h>
26 #include <linux/if_vlan.h>
27 #include <net/llc_pdu.h>
28 #include <linux/kernel.h>
29 #include <linux/jhash.h>
30 #include <linux/jiffies.h>
31 #include <linux/llc.h>
32 #include <linux/module.h>
33 #include <linux/in.h>
34 #include <linux/rcupdate.h>
35 #include <linux/if_arp.h>
36 #include <linux/ip.h>
37 #include <linux/ipv6.h>
38 #include <linux/sctp.h>
39 #include <linux/tcp.h>
40 #include <linux/udp.h>
41 #include <linux/icmp.h>
42 #include <linux/icmpv6.h>
43 #include <linux/rculist.h>
44 #include <net/ip.h>
45 #include <net/ipv6.h>
46 #include <net/ndisc.h>
48 #define TBL_MIN_BUCKETS 1024
49 #define REHASH_INTERVAL (10 * 60 * HZ)
51 static struct kmem_cache *flow_cache;
52 struct kmem_cache *flow_stats_cache __read_mostly;
54 static u16 range_n_bytes(const struct sw_flow_key_range *range)
56 return range->end - range->start;
59 void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
60 bool full, const struct sw_flow_mask *mask)
62 int start = full ? 0 : mask->range.start;
63 int len = full ? sizeof *dst : range_n_bytes(&mask->range);
64 const long *m = (const long *)((const u8 *)&mask->key + start);
65 const long *s = (const long *)((const u8 *)src + start);
66 long *d = (long *)((u8 *)dst + start);
67 int i;
69 /* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
70 * if 'full' is false the memory outside of the 'mask->range' is left
71 * uninitialized. This can be used as an optimization when further
72 * operations on 'dst' only use contents within 'mask->range'.
74 for (i = 0; i < len; i += sizeof(long))
75 *d++ = *s++ & *m++;
78 struct sw_flow *ovs_flow_alloc(void)
80 struct sw_flow *flow;
81 struct flow_stats *stats;
82 int node;
84 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
85 if (!flow)
86 return ERR_PTR(-ENOMEM);
88 flow->sf_acts = NULL;
89 flow->mask = NULL;
90 flow->id.unmasked_key = NULL;
91 flow->id.ufid_len = 0;
92 flow->stats_last_writer = NUMA_NO_NODE;
94 /* Initialize the default stat node. */
95 stats = kmem_cache_alloc_node(flow_stats_cache,
96 GFP_KERNEL | __GFP_ZERO,
97 node_online(0) ? 0 : NUMA_NO_NODE);
98 if (!stats)
99 goto err;
101 spin_lock_init(&stats->lock);
103 RCU_INIT_POINTER(flow->stats[0], stats);
105 for_each_node(node)
106 if (node != 0)
107 RCU_INIT_POINTER(flow->stats[node], NULL);
109 return flow;
110 err:
111 kmem_cache_free(flow_cache, flow);
112 return ERR_PTR(-ENOMEM);
115 int ovs_flow_tbl_count(const struct flow_table *table)
117 return table->count;
120 static struct flex_array *alloc_buckets(unsigned int n_buckets)
122 struct flex_array *buckets;
123 int i, err;
125 buckets = flex_array_alloc(sizeof(struct hlist_head),
126 n_buckets, GFP_KERNEL);
127 if (!buckets)
128 return NULL;
130 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
131 if (err) {
132 flex_array_free(buckets);
133 return NULL;
136 for (i = 0; i < n_buckets; i++)
137 INIT_HLIST_HEAD((struct hlist_head *)
138 flex_array_get(buckets, i));
140 return buckets;
143 static void flow_free(struct sw_flow *flow)
145 int node;
147 if (ovs_identifier_is_key(&flow->id))
148 kfree(flow->id.unmasked_key);
149 if (flow->sf_acts)
150 ovs_nla_free_flow_actions((struct sw_flow_actions __force *)flow->sf_acts);
151 for_each_node(node)
152 if (flow->stats[node])
153 kmem_cache_free(flow_stats_cache,
154 (struct flow_stats __force *)flow->stats[node]);
155 kmem_cache_free(flow_cache, flow);
158 static void rcu_free_flow_callback(struct rcu_head *rcu)
160 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
162 flow_free(flow);
165 void ovs_flow_free(struct sw_flow *flow, bool deferred)
167 if (!flow)
168 return;
170 if (deferred)
171 call_rcu(&flow->rcu, rcu_free_flow_callback);
172 else
173 flow_free(flow);
176 static void free_buckets(struct flex_array *buckets)
178 flex_array_free(buckets);
182 static void __table_instance_destroy(struct table_instance *ti)
184 free_buckets(ti->buckets);
185 kfree(ti);
188 static struct table_instance *table_instance_alloc(int new_size)
190 struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
192 if (!ti)
193 return NULL;
195 ti->buckets = alloc_buckets(new_size);
197 if (!ti->buckets) {
198 kfree(ti);
199 return NULL;
201 ti->n_buckets = new_size;
202 ti->node_ver = 0;
203 ti->keep_flows = false;
204 get_random_bytes(&ti->hash_seed, sizeof(u32));
206 return ti;
209 int ovs_flow_tbl_init(struct flow_table *table)
211 struct table_instance *ti, *ufid_ti;
213 ti = table_instance_alloc(TBL_MIN_BUCKETS);
215 if (!ti)
216 return -ENOMEM;
218 ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
219 if (!ufid_ti)
220 goto free_ti;
222 rcu_assign_pointer(table->ti, ti);
223 rcu_assign_pointer(table->ufid_ti, ufid_ti);
224 INIT_LIST_HEAD(&table->mask_list);
225 table->last_rehash = jiffies;
226 table->count = 0;
227 table->ufid_count = 0;
228 return 0;
230 free_ti:
231 __table_instance_destroy(ti);
232 return -ENOMEM;
235 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
237 struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
239 __table_instance_destroy(ti);
242 static void table_instance_destroy(struct table_instance *ti,
243 struct table_instance *ufid_ti,
244 bool deferred)
246 int i;
248 if (!ti)
249 return;
251 BUG_ON(!ufid_ti);
252 if (ti->keep_flows)
253 goto skip_flows;
255 for (i = 0; i < ti->n_buckets; i++) {
256 struct sw_flow *flow;
257 struct hlist_head *head = flex_array_get(ti->buckets, i);
258 struct hlist_node *n;
259 int ver = ti->node_ver;
260 int ufid_ver = ufid_ti->node_ver;
262 hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
263 hlist_del_rcu(&flow->flow_table.node[ver]);
264 if (ovs_identifier_is_ufid(&flow->id))
265 hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
266 ovs_flow_free(flow, deferred);
270 skip_flows:
271 if (deferred) {
272 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
273 call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
274 } else {
275 __table_instance_destroy(ti);
276 __table_instance_destroy(ufid_ti);
280 /* No need for locking this function is called from RCU callback or
281 * error path.
283 void ovs_flow_tbl_destroy(struct flow_table *table)
285 struct table_instance *ti = rcu_dereference_raw(table->ti);
286 struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
288 table_instance_destroy(ti, ufid_ti, false);
291 struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
292 u32 *bucket, u32 *last)
294 struct sw_flow *flow;
295 struct hlist_head *head;
296 int ver;
297 int i;
299 ver = ti->node_ver;
300 while (*bucket < ti->n_buckets) {
301 i = 0;
302 head = flex_array_get(ti->buckets, *bucket);
303 hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
304 if (i < *last) {
305 i++;
306 continue;
308 *last = i + 1;
309 return flow;
311 (*bucket)++;
312 *last = 0;
315 return NULL;
318 static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
320 hash = jhash_1word(hash, ti->hash_seed);
321 return flex_array_get(ti->buckets,
322 (hash & (ti->n_buckets - 1)));
325 static void table_instance_insert(struct table_instance *ti,
326 struct sw_flow *flow)
328 struct hlist_head *head;
330 head = find_bucket(ti, flow->flow_table.hash);
331 hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
334 static void ufid_table_instance_insert(struct table_instance *ti,
335 struct sw_flow *flow)
337 struct hlist_head *head;
339 head = find_bucket(ti, flow->ufid_table.hash);
340 hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
343 static void flow_table_copy_flows(struct table_instance *old,
344 struct table_instance *new, bool ufid)
346 int old_ver;
347 int i;
349 old_ver = old->node_ver;
350 new->node_ver = !old_ver;
352 /* Insert in new table. */
353 for (i = 0; i < old->n_buckets; i++) {
354 struct sw_flow *flow;
355 struct hlist_head *head;
357 head = flex_array_get(old->buckets, i);
359 if (ufid)
360 hlist_for_each_entry(flow, head,
361 ufid_table.node[old_ver])
362 ufid_table_instance_insert(new, flow);
363 else
364 hlist_for_each_entry(flow, head,
365 flow_table.node[old_ver])
366 table_instance_insert(new, flow);
369 old->keep_flows = true;
372 static struct table_instance *table_instance_rehash(struct table_instance *ti,
373 int n_buckets, bool ufid)
375 struct table_instance *new_ti;
377 new_ti = table_instance_alloc(n_buckets);
378 if (!new_ti)
379 return NULL;
381 flow_table_copy_flows(ti, new_ti, ufid);
383 return new_ti;
386 int ovs_flow_tbl_flush(struct flow_table *flow_table)
388 struct table_instance *old_ti, *new_ti;
389 struct table_instance *old_ufid_ti, *new_ufid_ti;
391 new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
392 if (!new_ti)
393 return -ENOMEM;
394 new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
395 if (!new_ufid_ti)
396 goto err_free_ti;
398 old_ti = ovsl_dereference(flow_table->ti);
399 old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
401 rcu_assign_pointer(flow_table->ti, new_ti);
402 rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
403 flow_table->last_rehash = jiffies;
404 flow_table->count = 0;
405 flow_table->ufid_count = 0;
407 table_instance_destroy(old_ti, old_ufid_ti, true);
408 return 0;
410 err_free_ti:
411 __table_instance_destroy(new_ti);
412 return -ENOMEM;
415 static u32 flow_hash(const struct sw_flow_key *key,
416 const struct sw_flow_key_range *range)
418 int key_start = range->start;
419 int key_end = range->end;
420 const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
421 int hash_u32s = (key_end - key_start) >> 2;
423 /* Make sure number of hash bytes are multiple of u32. */
424 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
426 return jhash2(hash_key, hash_u32s, 0);
429 static int flow_key_start(const struct sw_flow_key *key)
431 if (key->tun_proto)
432 return 0;
433 else
434 return rounddown(offsetof(struct sw_flow_key, phy),
435 sizeof(long));
438 static bool cmp_key(const struct sw_flow_key *key1,
439 const struct sw_flow_key *key2,
440 int key_start, int key_end)
442 const long *cp1 = (const long *)((const u8 *)key1 + key_start);
443 const long *cp2 = (const long *)((const u8 *)key2 + key_start);
444 long diffs = 0;
445 int i;
447 for (i = key_start; i < key_end; i += sizeof(long))
448 diffs |= *cp1++ ^ *cp2++;
450 return diffs == 0;
453 static bool flow_cmp_masked_key(const struct sw_flow *flow,
454 const struct sw_flow_key *key,
455 const struct sw_flow_key_range *range)
457 return cmp_key(&flow->key, key, range->start, range->end);
460 static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
461 const struct sw_flow_match *match)
463 struct sw_flow_key *key = match->key;
464 int key_start = flow_key_start(key);
465 int key_end = match->range.end;
467 BUG_ON(ovs_identifier_is_ufid(&flow->id));
468 return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
471 static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
472 const struct sw_flow_key *unmasked,
473 const struct sw_flow_mask *mask)
475 struct sw_flow *flow;
476 struct hlist_head *head;
477 u32 hash;
478 struct sw_flow_key masked_key;
480 ovs_flow_mask_key(&masked_key, unmasked, false, mask);
481 hash = flow_hash(&masked_key, &mask->range);
482 head = find_bucket(ti, hash);
483 hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
484 if (flow->mask == mask && flow->flow_table.hash == hash &&
485 flow_cmp_masked_key(flow, &masked_key, &mask->range))
486 return flow;
488 return NULL;
491 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
492 const struct sw_flow_key *key,
493 u32 *n_mask_hit)
495 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
496 struct sw_flow_mask *mask;
497 struct sw_flow *flow;
499 *n_mask_hit = 0;
500 list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
501 (*n_mask_hit)++;
502 flow = masked_flow_lookup(ti, key, mask);
503 if (flow) /* Found */
504 return flow;
506 return NULL;
509 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
510 const struct sw_flow_key *key)
512 u32 __always_unused n_mask_hit;
514 return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
517 struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
518 const struct sw_flow_match *match)
520 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
521 struct sw_flow_mask *mask;
522 struct sw_flow *flow;
524 /* Always called under ovs-mutex. */
525 list_for_each_entry(mask, &tbl->mask_list, list) {
526 flow = masked_flow_lookup(ti, match->key, mask);
527 if (flow && ovs_identifier_is_key(&flow->id) &&
528 ovs_flow_cmp_unmasked_key(flow, match))
529 return flow;
531 return NULL;
534 static u32 ufid_hash(const struct sw_flow_id *sfid)
536 return jhash(sfid->ufid, sfid->ufid_len, 0);
539 static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
540 const struct sw_flow_id *sfid)
542 if (flow->id.ufid_len != sfid->ufid_len)
543 return false;
545 return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
548 bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
550 if (ovs_identifier_is_ufid(&flow->id))
551 return flow_cmp_masked_key(flow, match->key, &match->range);
553 return ovs_flow_cmp_unmasked_key(flow, match);
556 struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
557 const struct sw_flow_id *ufid)
559 struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
560 struct sw_flow *flow;
561 struct hlist_head *head;
562 u32 hash;
564 hash = ufid_hash(ufid);
565 head = find_bucket(ti, hash);
566 hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
567 if (flow->ufid_table.hash == hash &&
568 ovs_flow_cmp_ufid(flow, ufid))
569 return flow;
571 return NULL;
574 int ovs_flow_tbl_num_masks(const struct flow_table *table)
576 struct sw_flow_mask *mask;
577 int num = 0;
579 list_for_each_entry(mask, &table->mask_list, list)
580 num++;
582 return num;
585 static struct table_instance *table_instance_expand(struct table_instance *ti,
586 bool ufid)
588 return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
591 /* Remove 'mask' from the mask list, if it is not needed any more. */
592 static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
594 if (mask) {
595 /* ovs-lock is required to protect mask-refcount and
596 * mask list.
598 ASSERT_OVSL();
599 BUG_ON(!mask->ref_count);
600 mask->ref_count--;
602 if (!mask->ref_count) {
603 list_del_rcu(&mask->list);
604 kfree_rcu(mask, rcu);
609 /* Must be called with OVS mutex held. */
610 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
612 struct table_instance *ti = ovsl_dereference(table->ti);
613 struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
615 BUG_ON(table->count == 0);
616 hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
617 table->count--;
618 if (ovs_identifier_is_ufid(&flow->id)) {
619 hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
620 table->ufid_count--;
623 /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
624 * accessible as long as the RCU read lock is held.
626 flow_mask_remove(table, flow->mask);
629 static struct sw_flow_mask *mask_alloc(void)
631 struct sw_flow_mask *mask;
633 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
634 if (mask)
635 mask->ref_count = 1;
637 return mask;
640 static bool mask_equal(const struct sw_flow_mask *a,
641 const struct sw_flow_mask *b)
643 const u8 *a_ = (const u8 *)&a->key + a->range.start;
644 const u8 *b_ = (const u8 *)&b->key + b->range.start;
646 return (a->range.end == b->range.end)
647 && (a->range.start == b->range.start)
648 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
651 static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
652 const struct sw_flow_mask *mask)
654 struct list_head *ml;
656 list_for_each(ml, &tbl->mask_list) {
657 struct sw_flow_mask *m;
658 m = container_of(ml, struct sw_flow_mask, list);
659 if (mask_equal(mask, m))
660 return m;
663 return NULL;
666 /* Add 'mask' into the mask list, if it is not already there. */
667 static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
668 const struct sw_flow_mask *new)
670 struct sw_flow_mask *mask;
671 mask = flow_mask_find(tbl, new);
672 if (!mask) {
673 /* Allocate a new mask if none exsits. */
674 mask = mask_alloc();
675 if (!mask)
676 return -ENOMEM;
677 mask->key = new->key;
678 mask->range = new->range;
679 list_add_rcu(&mask->list, &tbl->mask_list);
680 } else {
681 BUG_ON(!mask->ref_count);
682 mask->ref_count++;
685 flow->mask = mask;
686 return 0;
689 /* Must be called with OVS mutex held. */
690 static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
692 struct table_instance *new_ti = NULL;
693 struct table_instance *ti;
695 flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
696 ti = ovsl_dereference(table->ti);
697 table_instance_insert(ti, flow);
698 table->count++;
700 /* Expand table, if necessary, to make room. */
701 if (table->count > ti->n_buckets)
702 new_ti = table_instance_expand(ti, false);
703 else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
704 new_ti = table_instance_rehash(ti, ti->n_buckets, false);
706 if (new_ti) {
707 rcu_assign_pointer(table->ti, new_ti);
708 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
709 table->last_rehash = jiffies;
713 /* Must be called with OVS mutex held. */
714 static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
716 struct table_instance *ti;
718 flow->ufid_table.hash = ufid_hash(&flow->id);
719 ti = ovsl_dereference(table->ufid_ti);
720 ufid_table_instance_insert(ti, flow);
721 table->ufid_count++;
723 /* Expand table, if necessary, to make room. */
724 if (table->ufid_count > ti->n_buckets) {
725 struct table_instance *new_ti;
727 new_ti = table_instance_expand(ti, true);
728 if (new_ti) {
729 rcu_assign_pointer(table->ufid_ti, new_ti);
730 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
735 /* Must be called with OVS mutex held. */
736 int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
737 const struct sw_flow_mask *mask)
739 int err;
741 err = flow_mask_insert(table, flow, mask);
742 if (err)
743 return err;
744 flow_key_insert(table, flow);
745 if (ovs_identifier_is_ufid(&flow->id))
746 flow_ufid_insert(table, flow);
748 return 0;
751 /* Initializes the flow module.
752 * Returns zero if successful or a negative error code. */
753 int ovs_flow_init(void)
755 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
756 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
758 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
759 + (nr_node_ids
760 * sizeof(struct flow_stats *)),
761 0, 0, NULL);
762 if (flow_cache == NULL)
763 return -ENOMEM;
765 flow_stats_cache
766 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
767 0, SLAB_HWCACHE_ALIGN, NULL);
768 if (flow_stats_cache == NULL) {
769 kmem_cache_destroy(flow_cache);
770 flow_cache = NULL;
771 return -ENOMEM;
774 return 0;
777 /* Uninitializes the flow module. */
778 void ovs_flow_exit(void)
780 kmem_cache_destroy(flow_stats_cache);
781 kmem_cache_destroy(flow_cache);