2 * Copyright (c) 2007-2013 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
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>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.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>
45 #include <net/ndisc.h>
49 #define TBL_MIN_BUCKETS 1024
50 #define MASK_ARRAY_SIZE_MIN 16
51 #define REHASH_INTERVAL (10 * 60 * HZ)
53 #define MC_HASH_SHIFT 8
54 #define MC_HASH_ENTRIES (1u << MC_HASH_SHIFT)
55 #define MC_HASH_SEGS ((sizeof(uint32_t) * 8) / MC_HASH_SHIFT)
57 static struct kmem_cache *flow_cache;
58 struct kmem_cache *flow_stats_cache __read_mostly;
60 static u16 range_n_bytes(const struct sw_flow_key_range *range)
62 return range->end - range->start;
65 void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
66 const struct sw_flow_mask *mask)
68 const long *m = (const long *)((const u8 *)&mask->key +
70 const long *s = (const long *)((const u8 *)src +
72 long *d = (long *)((u8 *)dst + mask->range.start);
75 /* The memory outside of the 'mask->range' are not set since
76 * further operations on 'dst' only uses contents within
79 for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
83 struct sw_flow *ovs_flow_alloc(void)
86 struct flow_stats *stats;
89 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
91 return ERR_PTR(-ENOMEM);
95 flow->stats_last_writer = NUMA_NO_NODE;
97 /* Initialize the default stat node. */
98 stats = kmem_cache_alloc_node(flow_stats_cache,
99 GFP_KERNEL | __GFP_ZERO, 0);
103 spin_lock_init(&stats->lock);
105 RCU_INIT_POINTER(flow->stats[0], stats);
109 RCU_INIT_POINTER(flow->stats[node], NULL);
113 kmem_cache_free(flow_cache, flow);
114 return ERR_PTR(-ENOMEM);
117 int ovs_flow_tbl_count(struct flow_table *table)
122 static struct flex_array *alloc_buckets(unsigned int n_buckets)
124 struct flex_array *buckets;
127 buckets = flex_array_alloc(sizeof(struct hlist_head),
128 n_buckets, GFP_KERNEL);
132 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
134 flex_array_free(buckets);
138 for (i = 0; i < n_buckets; i++)
139 INIT_HLIST_HEAD((struct hlist_head *)
140 flex_array_get(buckets, i));
145 static void flow_free(struct sw_flow *flow)
149 kfree((struct sw_flow_actions __force *)flow->sf_acts);
151 if (flow->stats[node])
152 kmem_cache_free(flow_stats_cache,
153 (struct flow_stats __force *)flow->stats[node]);
154 kmem_cache_free(flow_cache, flow);
157 static void rcu_free_flow_callback(struct rcu_head *rcu)
159 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
164 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
166 struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
171 void ovs_flow_free(struct sw_flow *flow, bool deferred)
177 call_rcu(&flow->rcu, rcu_free_flow_callback);
182 static void free_buckets(struct flex_array *buckets)
184 flex_array_free(buckets);
188 static void __table_instance_destroy(struct table_instance *ti)
190 free_buckets(ti->buckets);
194 static struct table_instance *table_instance_alloc(int new_size)
196 struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
201 ti->buckets = alloc_buckets(new_size);
207 ti->n_buckets = new_size;
209 ti->keep_flows = false;
210 get_random_bytes(&ti->hash_seed, sizeof(u32));
215 static void mask_array_rcu_cb(struct rcu_head *rcu)
217 struct mask_array *ma = container_of(rcu, struct mask_array, rcu);
222 static struct mask_array *tbl_mask_array_alloc(int size)
224 struct mask_array *new;
226 new = kzalloc(sizeof(struct mask_array) +
227 sizeof(struct sw_flow_mask *) * size, GFP_KERNEL);
237 static int tbl_mask_array_realloc(struct flow_table *tbl, int size)
239 struct mask_array *old;
240 struct mask_array *new;
242 new = tbl_mask_array_alloc(size);
246 old = ovsl_dereference(tbl->mask_array);
250 for (i = 0; i < min(old->max, new->max); i++)
251 new->masks[i] = old->masks[i];
253 BUG_ON(old->count > new->max);
254 new->count = old->count;
256 rcu_assign_pointer(tbl->mask_array, new);
259 call_rcu(&old->rcu, mask_array_rcu_cb);
264 static void tbl_mask_array_delete_mask(struct mask_array *ma,
265 const struct sw_flow_mask *mask)
269 /* Delete a mask pointer from the valid section.
271 * Also move the last entry in its place, so there is no
272 * whole in the valid section.
274 * Notice the last entry still points to the original mask.
276 * <Note>: there is a small race window that may cause a mask
277 * to be missed in a search. Imaging a core is
278 * walking through the array, passing the index of deleting mask.
279 * But before reaching the last entry, it is overwritten,
280 * by another core that is adding a new mask, now the last entry
281 * will point to the new mask. In this case, the moved up last
282 * entry can be missed by the core walking the mask array.
284 * In case this missed mask would have led to successful
285 * lookup, Hitting the race window could cause a packet to miss
286 * kernel flow cache, and be sent to the user space.
289 for (i = 0; i < ma->count; i++)
290 if (mask == ovsl_dereference(ma->masks[i])) {
291 struct sw_flow_mask *last;
293 last = ovsl_dereference(ma->masks[ma->count - 1]);
294 rcu_assign_pointer(ma->masks[i], last);
299 /* Remove the deleted mask pointers from the invalid section. */
300 for (i = ma->count; i < ma->max; i++)
301 if (mask == ovsl_dereference(ma->masks[i]))
302 RCU_INIT_POINTER(ma->masks[i], NULL);
305 int ovs_flow_tbl_init(struct flow_table *table)
307 struct table_instance *ti;
308 struct mask_array *ma;
310 table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) *
311 MC_HASH_ENTRIES, __alignof__(struct mask_cache_entry));
312 if (!table->mask_cache)
315 ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
317 goto free_mask_cache;
319 ti = table_instance_alloc(TBL_MIN_BUCKETS);
321 goto free_mask_array;
323 rcu_assign_pointer(table->ti, ti);
324 rcu_assign_pointer(table->mask_array, ma);
325 table->last_rehash = jiffies;
330 kfree((struct mask_array __force *)table->mask_array);
332 free_percpu(table->mask_cache);
336 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
338 struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
340 __table_instance_destroy(ti);
343 static void table_instance_destroy(struct table_instance *ti, bool deferred)
353 for (i = 0; i < ti->n_buckets; i++) {
354 struct sw_flow *flow;
355 struct hlist_head *head = flex_array_get(ti->buckets, i);
356 struct hlist_node *n;
357 int ver = ti->node_ver;
359 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
360 hlist_del_rcu(&flow->hash_node[ver]);
361 ovs_flow_free(flow, deferred);
367 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
369 __table_instance_destroy(ti);
372 /* No need for locking this function is called from RCU callback or
374 void ovs_flow_tbl_destroy(struct flow_table *table)
376 struct table_instance *ti = (struct table_instance __force *)table->ti;
378 free_percpu(table->mask_cache);
379 kfree((struct mask_array __force *)table->mask_array);
380 table_instance_destroy(ti, false);
383 struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
384 u32 *bucket, u32 *last)
386 struct sw_flow *flow;
387 struct hlist_head *head;
392 while (*bucket < ti->n_buckets) {
394 head = flex_array_get(ti->buckets, *bucket);
395 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
410 static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
412 hash = jhash_1word(hash, ti->hash_seed);
413 return flex_array_get(ti->buckets,
414 (hash & (ti->n_buckets - 1)));
417 static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow)
419 struct hlist_head *head;
421 head = find_bucket(ti, flow->hash);
422 hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head);
425 static void flow_table_copy_flows(struct table_instance *old,
426 struct table_instance *new)
431 old_ver = old->node_ver;
432 new->node_ver = !old_ver;
434 /* Insert in new table. */
435 for (i = 0; i < old->n_buckets; i++) {
436 struct sw_flow *flow;
437 struct hlist_head *head;
439 head = flex_array_get(old->buckets, i);
441 hlist_for_each_entry(flow, head, hash_node[old_ver])
442 table_instance_insert(new, flow);
445 old->keep_flows = true;
448 static struct table_instance *table_instance_rehash(struct table_instance *ti,
451 struct table_instance *new_ti;
453 new_ti = table_instance_alloc(n_buckets);
457 flow_table_copy_flows(ti, new_ti);
462 int ovs_flow_tbl_flush(struct flow_table *flow_table)
464 struct table_instance *old_ti;
465 struct table_instance *new_ti;
467 old_ti = ovsl_dereference(flow_table->ti);
468 new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
472 rcu_assign_pointer(flow_table->ti, new_ti);
473 flow_table->last_rehash = jiffies;
474 flow_table->count = 0;
476 table_instance_destroy(old_ti, true);
480 static u32 flow_hash(const struct sw_flow_key *key, int key_start,
483 const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
484 int hash_u32s = (key_end - key_start) >> 2;
486 /* Make sure number of hash bytes are multiple of u32. */
487 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
489 return arch_fast_hash2(hash_key, hash_u32s, 0);
492 static int flow_key_start(const struct sw_flow_key *key)
494 if (key->tun_key.ipv4_dst)
497 return rounddown(offsetof(struct sw_flow_key, phy),
501 static bool cmp_key(const struct sw_flow_key *key1,
502 const struct sw_flow_key *key2,
503 int key_start, int key_end)
505 const long *cp1 = (const long *)((const u8 *)key1 + key_start);
506 const long *cp2 = (const long *)((const u8 *)key2 + key_start);
510 for (i = key_start; i < key_end; i += sizeof(long))
511 diffs |= *cp1++ ^ *cp2++;
516 static bool flow_cmp_masked_key(const struct sw_flow *flow,
517 const struct sw_flow_key *key,
518 int key_start, int key_end)
520 return cmp_key(&flow->key, key, key_start, key_end);
523 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
524 struct sw_flow_match *match)
526 struct sw_flow_key *key = match->key;
527 int key_start = flow_key_start(key);
528 int key_end = match->range.end;
530 return cmp_key(&flow->unmasked_key, key, key_start, key_end);
533 static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
534 const struct sw_flow_key *unmasked,
535 struct sw_flow_mask *mask,
538 struct sw_flow *flow;
539 struct hlist_head *head;
540 int key_start = mask->range.start;
541 int key_end = mask->range.end;
543 struct sw_flow_key masked_key;
545 ovs_flow_mask_key(&masked_key, unmasked, mask);
546 hash = flow_hash(&masked_key, key_start, key_end);
547 head = find_bucket(ti, hash);
549 hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) {
550 if (flow->mask == mask && flow->hash == hash &&
551 flow_cmp_masked_key(flow, &masked_key,
558 static struct sw_flow *flow_lookup(struct flow_table *tbl,
559 struct table_instance *ti,
560 struct mask_array *ma,
561 const struct sw_flow_key *key,
565 struct sw_flow *flow;
568 for (i = 0; i < ma->max; i++) {
569 struct sw_flow_mask *mask;
571 mask = rcu_dereference_ovsl(ma->masks[i]);
575 flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
576 if (flow) { /* Found */
586 * mask_cache maps flow to probable mask. This cache is not tightly
587 * coupled cache, It means updates to mask list can result in inconsistent
588 * cache entry in mask cache.
589 * This is per cpu cache and is divided in MC_HASH_SEGS segments.
590 * In case of a hash collision the entry is hashed in next segment.
592 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
593 const struct sw_flow_key *key,
597 struct mask_array *ma = rcu_dereference(tbl->mask_array);
598 struct table_instance *ti = rcu_dereference(tbl->ti);
599 struct mask_cache_entry *entries, *ce;
600 struct sw_flow *flow;
605 if (unlikely(!skb_hash)) {
606 u32 __always_unused mask_index;
608 return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index);
612 entries = this_cpu_ptr(tbl->mask_cache);
614 /* Find the cache entry 'ce' to operate on. */
615 for (seg = 0; seg < MC_HASH_SEGS; seg++) {
616 int index = hash & (MC_HASH_ENTRIES - 1);
617 struct mask_cache_entry *e;
620 if (e->skb_hash == skb_hash) {
621 struct sw_flow_mask *cache;
622 int i = e->mask_index;
624 if (likely(i < ma->max)) {
625 cache = rcu_dereference(ma->masks[i]);
627 flow = masked_flow_lookup(ti, key,
634 /* Cache miss. This is the best cache
635 * replacement candidate. */
641 if (!ce || e->skb_hash < ce->skb_hash)
642 ce = e; /* A better replacement cache candidate. */
644 hash >>= MC_HASH_SHIFT;
647 /* Cache miss, do full lookup. */
648 flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &ce->mask_index);
650 ce->skb_hash = skb_hash;
655 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
656 const struct sw_flow_key *key)
658 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
659 struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
660 u32 __always_unused n_mask_hit;
661 u32 __always_unused index;
663 return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index);
666 struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
667 struct sw_flow_match *match)
669 struct mask_array *ma = ovsl_dereference(tbl->mask_array);
672 /* Always called under ovs-mutex. */
673 for (i = 0; i < ma->count; i++) {
674 struct table_instance *ti = ovsl_dereference(tbl->ti);
675 u32 __always_unused n_mask_hit;
676 struct sw_flow_mask *mask;
677 struct sw_flow *flow;
679 mask = ovsl_dereference(ma->masks[i]);
680 flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit);
681 if (flow && ovs_flow_cmp_unmasked_key(flow, match))
687 int ovs_flow_tbl_num_masks(const struct flow_table *table)
689 struct mask_array *ma;
691 ma = rcu_dereference_ovsl(table->mask_array);
695 static struct table_instance *table_instance_expand(struct table_instance *ti)
697 return table_instance_rehash(ti, ti->n_buckets * 2);
700 /* Remove 'mask' from the mask list, if it is not needed any more. */
701 static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
704 /* ovs-lock is required to protect mask-refcount and
708 BUG_ON(!mask->ref_count);
711 if (!mask->ref_count) {
712 struct mask_array *ma;
714 ma = ovsl_dereference(tbl->mask_array);
715 /* Shrink the mask array if necessary. */
716 if (ma->max > MASK_ARRAY_SIZE_MIN * 2
717 && ma->count <= ma->max / 4) {
719 tbl_mask_array_realloc(tbl, ma->max / 2);
720 ma = ovsl_dereference(tbl->mask_array);
723 tbl_mask_array_delete_mask(ma, mask);
724 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
729 /* Must be called with OVS mutex held. */
730 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
732 struct table_instance *ti = ovsl_dereference(table->ti);
734 BUG_ON(table->count == 0);
735 hlist_del_rcu(&flow->hash_node[ti->node_ver]);
738 /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
739 * accessible as long as the RCU read lock is held. */
740 flow_mask_remove(table, flow->mask);
743 static struct sw_flow_mask *mask_alloc(void)
745 struct sw_flow_mask *mask;
747 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
754 static bool mask_equal(const struct sw_flow_mask *a,
755 const struct sw_flow_mask *b)
757 const u8 *a_ = (const u8 *)&a->key + a->range.start;
758 const u8 *b_ = (const u8 *)&b->key + b->range.start;
760 return (a->range.end == b->range.end)
761 && (a->range.start == b->range.start)
762 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
765 static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
766 const struct sw_flow_mask *mask)
768 struct mask_array *ma;
771 ma = ovsl_dereference(tbl->mask_array);
772 for (i = 0; i < ma->count; i++) {
773 struct sw_flow_mask *t;
775 t = ovsl_dereference(ma->masks[i]);
776 if (t && mask_equal(mask, t))
783 /* Add 'mask' into the mask list, if it is not already there. */
784 static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
785 struct sw_flow_mask *new)
787 struct sw_flow_mask *mask;
789 mask = flow_mask_find(tbl, new);
791 struct mask_array *ma;
793 /* Allocate a new mask if none exsits. */
798 mask->key = new->key;
799 mask->range = new->range;
801 /* Add mask to mask-list. */
802 ma = ovsl_dereference(tbl->mask_array);
803 if (ma->count >= ma->max) {
806 err = tbl_mask_array_realloc(tbl, ma->max +
807 MASK_ARRAY_SIZE_MIN);
812 ma = ovsl_dereference(tbl->mask_array);
815 rcu_assign_pointer(ma->masks[ma->count], mask);
818 BUG_ON(!mask->ref_count);
826 /* Must be called with OVS mutex held. */
827 int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
828 struct sw_flow_mask *mask)
830 struct table_instance *new_ti = NULL;
831 struct table_instance *ti;
834 err = flow_mask_insert(table, flow, mask);
838 flow->hash = flow_hash(&flow->key, flow->mask->range.start,
839 flow->mask->range.end);
840 ti = ovsl_dereference(table->ti);
841 table_instance_insert(ti, flow);
844 /* Expand table, if necessary, to make room. */
845 if (table->count > ti->n_buckets)
846 new_ti = table_instance_expand(ti);
847 else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
848 new_ti = table_instance_rehash(ti, ti->n_buckets);
851 rcu_assign_pointer(table->ti, new_ti);
852 table_instance_destroy(ti, true);
853 table->last_rehash = jiffies;
858 /* Initializes the flow module.
859 * Returns zero if successful or a negative error code. */
860 int ovs_flow_init(void)
862 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
863 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
865 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
866 + (num_possible_nodes()
867 * sizeof(struct flow_stats *)),
869 if (flow_cache == NULL)
873 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
874 0, SLAB_HWCACHE_ALIGN, NULL);
875 if (flow_stats_cache == NULL) {
876 kmem_cache_destroy(flow_cache);
884 /* Uninitializes the flow module. */
885 void ovs_flow_exit(void)
887 kmem_cache_destroy(flow_stats_cache);
888 kmem_cache_destroy(flow_cache);