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
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/jhash.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>
47 #define TBL_MIN_BUCKETS 1024
48 #define REHASH_INTERVAL (10 * 60 * HZ)
50 static struct kmem_cache *flow_cache;
51 struct kmem_cache *flow_stats_cache __read_mostly;
53 static u16 range_n_bytes(const struct sw_flow_key_range *range)
55 return range->end - range->start;
58 void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
59 bool full, const struct sw_flow_mask *mask)
61 int start = full ? 0 : mask->range.start;
62 int len = full ? sizeof *dst : range_n_bytes(&mask->range);
63 const long *m = (const long *)((const u8 *)&mask->key + start);
64 const long *s = (const long *)((const u8 *)src + start);
65 long *d = (long *)((u8 *)dst + start);
68 /* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
69 * if 'full' is false the memory outside of the 'mask->range' is left
70 * uninitialized. This can be used as an optimization when further
71 * operations on 'dst' only use contents within 'mask->range'.
73 for (i = 0; i < len; i += sizeof(long))
77 struct sw_flow *ovs_flow_alloc(void)
80 struct flow_stats *stats;
83 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
85 return ERR_PTR(-ENOMEM);
89 flow->id.unmasked_key = NULL;
90 flow->id.ufid_len = 0;
91 flow->stats_last_writer = NUMA_NO_NODE;
93 /* Initialize the default stat node. */
94 stats = kmem_cache_alloc_node(flow_stats_cache,
95 GFP_KERNEL | __GFP_ZERO, 0);
99 spin_lock_init(&stats->lock);
101 RCU_INIT_POINTER(flow->stats[0], stats);
105 RCU_INIT_POINTER(flow->stats[node], NULL);
109 kmem_cache_free(flow_cache, flow);
110 return ERR_PTR(-ENOMEM);
113 int ovs_flow_tbl_count(const struct flow_table *table)
118 static struct flex_array *alloc_buckets(unsigned int n_buckets)
120 struct flex_array *buckets;
123 buckets = flex_array_alloc(sizeof(struct hlist_head),
124 n_buckets, GFP_KERNEL);
128 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
130 flex_array_free(buckets);
134 for (i = 0; i < n_buckets; i++)
135 INIT_HLIST_HEAD((struct hlist_head *)
136 flex_array_get(buckets, i));
141 static void flow_free(struct sw_flow *flow)
145 if (ovs_identifier_is_key(&flow->id))
146 kfree(flow->id.unmasked_key);
147 kfree((struct sw_flow_actions __force *)flow->sf_acts);
149 if (flow->stats[node])
150 kmem_cache_free(flow_stats_cache,
151 (struct flow_stats __force *)flow->stats[node]);
152 kmem_cache_free(flow_cache, flow);
155 static void rcu_free_flow_callback(struct rcu_head *rcu)
157 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
162 void ovs_flow_free(struct sw_flow *flow, bool deferred)
168 call_rcu(&flow->rcu, rcu_free_flow_callback);
173 static void free_buckets(struct flex_array *buckets)
175 flex_array_free(buckets);
179 static void __table_instance_destroy(struct table_instance *ti)
181 free_buckets(ti->buckets);
185 static struct table_instance *table_instance_alloc(int new_size)
187 struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
192 ti->buckets = alloc_buckets(new_size);
198 ti->n_buckets = new_size;
200 ti->keep_flows = false;
201 get_random_bytes(&ti->hash_seed, sizeof(u32));
206 int ovs_flow_tbl_init(struct flow_table *table)
208 struct table_instance *ti, *ufid_ti;
210 ti = table_instance_alloc(TBL_MIN_BUCKETS);
215 ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
219 rcu_assign_pointer(table->ti, ti);
220 rcu_assign_pointer(table->ufid_ti, ufid_ti);
221 INIT_LIST_HEAD(&table->mask_list);
222 table->last_rehash = jiffies;
224 table->ufid_count = 0;
228 __table_instance_destroy(ti);
232 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
234 struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
236 __table_instance_destroy(ti);
239 static void table_instance_destroy(struct table_instance *ti,
240 struct table_instance *ufid_ti,
252 for (i = 0; i < ti->n_buckets; i++) {
253 struct sw_flow *flow;
254 struct hlist_head *head = flex_array_get(ti->buckets, i);
255 struct hlist_node *n;
256 int ver = ti->node_ver;
257 int ufid_ver = ufid_ti->node_ver;
259 hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
260 hlist_del_rcu(&flow->flow_table.node[ver]);
261 if (ovs_identifier_is_ufid(&flow->id))
262 hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
263 ovs_flow_free(flow, deferred);
269 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
270 call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
272 __table_instance_destroy(ti);
273 __table_instance_destroy(ufid_ti);
277 /* No need for locking this function is called from RCU callback or
280 void ovs_flow_tbl_destroy(struct flow_table *table)
282 struct table_instance *ti = rcu_dereference_raw(table->ti);
283 struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
285 table_instance_destroy(ti, ufid_ti, false);
288 struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
289 u32 *bucket, u32 *last)
291 struct sw_flow *flow;
292 struct hlist_head *head;
297 while (*bucket < ti->n_buckets) {
299 head = flex_array_get(ti->buckets, *bucket);
300 hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
315 static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
317 hash = jhash_1word(hash, ti->hash_seed);
318 return flex_array_get(ti->buckets,
319 (hash & (ti->n_buckets - 1)));
322 static void table_instance_insert(struct table_instance *ti,
323 struct sw_flow *flow)
325 struct hlist_head *head;
327 head = find_bucket(ti, flow->flow_table.hash);
328 hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
331 static void ufid_table_instance_insert(struct table_instance *ti,
332 struct sw_flow *flow)
334 struct hlist_head *head;
336 head = find_bucket(ti, flow->ufid_table.hash);
337 hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
340 static void flow_table_copy_flows(struct table_instance *old,
341 struct table_instance *new, bool ufid)
346 old_ver = old->node_ver;
347 new->node_ver = !old_ver;
349 /* Insert in new table. */
350 for (i = 0; i < old->n_buckets; i++) {
351 struct sw_flow *flow;
352 struct hlist_head *head;
354 head = flex_array_get(old->buckets, i);
357 hlist_for_each_entry(flow, head,
358 ufid_table.node[old_ver])
359 ufid_table_instance_insert(new, flow);
361 hlist_for_each_entry(flow, head,
362 flow_table.node[old_ver])
363 table_instance_insert(new, flow);
366 old->keep_flows = true;
369 static struct table_instance *table_instance_rehash(struct table_instance *ti,
370 int n_buckets, bool ufid)
372 struct table_instance *new_ti;
374 new_ti = table_instance_alloc(n_buckets);
378 flow_table_copy_flows(ti, new_ti, ufid);
383 int ovs_flow_tbl_flush(struct flow_table *flow_table)
385 struct table_instance *old_ti, *new_ti;
386 struct table_instance *old_ufid_ti, *new_ufid_ti;
388 new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
391 new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
395 old_ti = ovsl_dereference(flow_table->ti);
396 old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
398 rcu_assign_pointer(flow_table->ti, new_ti);
399 rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
400 flow_table->last_rehash = jiffies;
401 flow_table->count = 0;
402 flow_table->ufid_count = 0;
404 table_instance_destroy(old_ti, old_ufid_ti, true);
408 __table_instance_destroy(new_ti);
412 static u32 flow_hash(const struct sw_flow_key *key,
413 const struct sw_flow_key_range *range)
415 int key_start = range->start;
416 int key_end = range->end;
417 const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
418 int hash_u32s = (key_end - key_start) >> 2;
420 /* Make sure number of hash bytes are multiple of u32. */
421 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
423 return jhash2(hash_key, hash_u32s, 0);
426 static int flow_key_start(const struct sw_flow_key *key)
428 if (key->tun_key.ipv4_dst)
431 return rounddown(offsetof(struct sw_flow_key, phy),
435 static bool cmp_key(const struct sw_flow_key *key1,
436 const struct sw_flow_key *key2,
437 int key_start, int key_end)
439 const long *cp1 = (const long *)((const u8 *)key1 + key_start);
440 const long *cp2 = (const long *)((const u8 *)key2 + key_start);
444 for (i = key_start; i < key_end; i += sizeof(long))
445 diffs |= *cp1++ ^ *cp2++;
450 static bool flow_cmp_masked_key(const struct sw_flow *flow,
451 const struct sw_flow_key *key,
452 const struct sw_flow_key_range *range)
454 return cmp_key(&flow->key, key, range->start, range->end);
457 static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
458 const struct sw_flow_match *match)
460 struct sw_flow_key *key = match->key;
461 int key_start = flow_key_start(key);
462 int key_end = match->range.end;
464 BUG_ON(ovs_identifier_is_ufid(&flow->id));
465 return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
468 static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
469 const struct sw_flow_key *unmasked,
470 const struct sw_flow_mask *mask)
472 struct sw_flow *flow;
473 struct hlist_head *head;
475 struct sw_flow_key masked_key;
477 ovs_flow_mask_key(&masked_key, unmasked, false, mask);
478 hash = flow_hash(&masked_key, &mask->range);
479 head = find_bucket(ti, hash);
480 hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
481 if (flow->mask == mask && flow->flow_table.hash == hash &&
482 flow_cmp_masked_key(flow, &masked_key, &mask->range))
488 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
489 const struct sw_flow_key *key,
492 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
493 struct sw_flow_mask *mask;
494 struct sw_flow *flow;
497 list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
499 flow = masked_flow_lookup(ti, key, mask);
500 if (flow) /* Found */
506 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
507 const struct sw_flow_key *key)
509 u32 __always_unused n_mask_hit;
511 return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
514 struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
515 const struct sw_flow_match *match)
517 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
518 struct sw_flow_mask *mask;
519 struct sw_flow *flow;
521 /* Always called under ovs-mutex. */
522 list_for_each_entry(mask, &tbl->mask_list, list) {
523 flow = masked_flow_lookup(ti, match->key, mask);
524 if (flow && ovs_identifier_is_key(&flow->id) &&
525 ovs_flow_cmp_unmasked_key(flow, match))
531 static u32 ufid_hash(const struct sw_flow_id *sfid)
533 return jhash(sfid->ufid, sfid->ufid_len, 0);
536 static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
537 const struct sw_flow_id *sfid)
539 if (flow->id.ufid_len != sfid->ufid_len)
542 return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
545 bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
547 if (ovs_identifier_is_ufid(&flow->id))
548 return flow_cmp_masked_key(flow, match->key, &match->range);
550 return ovs_flow_cmp_unmasked_key(flow, match);
553 struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
554 const struct sw_flow_id *ufid)
556 struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
557 struct sw_flow *flow;
558 struct hlist_head *head;
561 hash = ufid_hash(ufid);
562 head = find_bucket(ti, hash);
563 hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
564 if (flow->ufid_table.hash == hash &&
565 ovs_flow_cmp_ufid(flow, ufid))
571 int ovs_flow_tbl_num_masks(const struct flow_table *table)
573 struct sw_flow_mask *mask;
576 list_for_each_entry(mask, &table->mask_list, list)
582 static struct table_instance *table_instance_expand(struct table_instance *ti,
585 return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
588 /* Remove 'mask' from the mask list, if it is not needed any more. */
589 static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
592 /* ovs-lock is required to protect mask-refcount and
596 BUG_ON(!mask->ref_count);
599 if (!mask->ref_count) {
600 list_del_rcu(&mask->list);
601 kfree_rcu(mask, rcu);
606 /* Must be called with OVS mutex held. */
607 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
609 struct table_instance *ti = ovsl_dereference(table->ti);
610 struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
612 BUG_ON(table->count == 0);
613 hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
615 if (ovs_identifier_is_ufid(&flow->id)) {
616 hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
620 /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
621 * accessible as long as the RCU read lock is held.
623 flow_mask_remove(table, flow->mask);
626 static struct sw_flow_mask *mask_alloc(void)
628 struct sw_flow_mask *mask;
630 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
637 static bool mask_equal(const struct sw_flow_mask *a,
638 const struct sw_flow_mask *b)
640 const u8 *a_ = (const u8 *)&a->key + a->range.start;
641 const u8 *b_ = (const u8 *)&b->key + b->range.start;
643 return (a->range.end == b->range.end)
644 && (a->range.start == b->range.start)
645 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
648 static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
649 const struct sw_flow_mask *mask)
651 struct list_head *ml;
653 list_for_each(ml, &tbl->mask_list) {
654 struct sw_flow_mask *m;
655 m = container_of(ml, struct sw_flow_mask, list);
656 if (mask_equal(mask, m))
663 /* Add 'mask' into the mask list, if it is not already there. */
664 static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
665 const struct sw_flow_mask *new)
667 struct sw_flow_mask *mask;
668 mask = flow_mask_find(tbl, new);
670 /* Allocate a new mask if none exsits. */
674 mask->key = new->key;
675 mask->range = new->range;
676 list_add_rcu(&mask->list, &tbl->mask_list);
678 BUG_ON(!mask->ref_count);
686 /* Must be called with OVS mutex held. */
687 static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
689 struct table_instance *new_ti = NULL;
690 struct table_instance *ti;
692 flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
693 ti = ovsl_dereference(table->ti);
694 table_instance_insert(ti, flow);
697 /* Expand table, if necessary, to make room. */
698 if (table->count > ti->n_buckets)
699 new_ti = table_instance_expand(ti, false);
700 else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
701 new_ti = table_instance_rehash(ti, ti->n_buckets, false);
704 rcu_assign_pointer(table->ti, new_ti);
705 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
706 table->last_rehash = jiffies;
710 /* Must be called with OVS mutex held. */
711 static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
713 struct table_instance *ti;
715 flow->ufid_table.hash = ufid_hash(&flow->id);
716 ti = ovsl_dereference(table->ufid_ti);
717 ufid_table_instance_insert(ti, flow);
720 /* Expand table, if necessary, to make room. */
721 if (table->ufid_count > ti->n_buckets) {
722 struct table_instance *new_ti;
724 new_ti = table_instance_expand(ti, true);
726 rcu_assign_pointer(table->ufid_ti, new_ti);
727 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
732 /* Must be called with OVS mutex held. */
733 int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
734 const struct sw_flow_mask *mask)
738 err = flow_mask_insert(table, flow, mask);
741 flow_key_insert(table, flow);
742 if (ovs_identifier_is_ufid(&flow->id))
743 flow_ufid_insert(table, flow);
748 /* Initializes the flow module.
749 * Returns zero if successful or a negative error code. */
750 int ovs_flow_init(void)
752 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
753 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
755 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
756 + (num_possible_nodes()
757 * sizeof(struct flow_stats *)),
759 if (flow_cache == NULL)
763 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
764 0, SLAB_HWCACHE_ALIGN, NULL);
765 if (flow_stats_cache == NULL) {
766 kmem_cache_destroy(flow_cache);
774 /* Uninitializes the flow module. */
775 void ovs_flow_exit(void)
777 kmem_cache_destroy(flow_stats_cache);
778 kmem_cache_destroy(flow_cache);