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/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 #include "flow_netlink.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 bool full, const struct sw_flow_mask *mask)
68 int start = full ? 0 : mask->range.start;
69 int len = full ? sizeof *dst : range_n_bytes(&mask->range);
70 const long *m = (const long *)((const u8 *)&mask->key + start);
71 const long *s = (const long *)((const u8 *)src + start);
72 long *d = (long *)((u8 *)dst + start);
75 /* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
76 * if 'full' is false the memory outside of the 'mask->range' is left
77 * uninitialized. This can be used as an optimization when further
78 * operations on 'dst' only use contents within 'mask->range'.
80 for (i = 0; i < len; i += sizeof(long))
84 struct sw_flow *ovs_flow_alloc(void)
87 struct flow_stats *stats;
90 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
92 return ERR_PTR(-ENOMEM);
96 flow->id.ufid_len = 0;
97 flow->id.unmasked_key = NULL;
98 flow->stats_last_writer = NUMA_NO_NODE;
100 /* Initialize the default stat node. */
101 stats = kmem_cache_alloc_node(flow_stats_cache,
102 GFP_KERNEL | __GFP_ZERO,
103 node_online(0) ? 0 : NUMA_NO_NODE);
107 spin_lock_init(&stats->lock);
109 RCU_INIT_POINTER(flow->stats[0], stats);
113 RCU_INIT_POINTER(flow->stats[node], NULL);
117 kmem_cache_free(flow_cache, flow);
118 return ERR_PTR(-ENOMEM);
121 int ovs_flow_tbl_count(const struct flow_table *table)
126 static struct flex_array *alloc_buckets(unsigned int n_buckets)
128 struct flex_array *buckets;
131 buckets = flex_array_alloc(sizeof(struct hlist_head),
132 n_buckets, GFP_KERNEL);
136 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
138 flex_array_free(buckets);
142 for (i = 0; i < n_buckets; i++)
143 INIT_HLIST_HEAD((struct hlist_head *)
144 flex_array_get(buckets, i));
149 static void flow_free(struct sw_flow *flow)
153 if (ovs_identifier_is_key(&flow->id))
154 kfree(flow->id.unmasked_key);
156 ovs_nla_free_flow_actions((struct sw_flow_actions __force *)flow->sf_acts);
158 if (flow->stats[node])
159 kmem_cache_free(flow_stats_cache,
160 rcu_dereference_raw(flow->stats[node]));
161 kmem_cache_free(flow_cache, flow);
164 static void rcu_free_flow_callback(struct rcu_head *rcu)
166 struct sw_flow *flow = container_of(rcu, struct sw_flow, 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 size = max(MASK_ARRAY_SIZE_MIN, size);
227 new = kzalloc(sizeof(struct mask_array) +
228 sizeof(struct sw_flow_mask *) * size, GFP_KERNEL);
238 static int tbl_mask_array_realloc(struct flow_table *tbl, int size)
240 struct mask_array *old;
241 struct mask_array *new;
243 new = tbl_mask_array_alloc(size);
247 old = ovsl_dereference(tbl->mask_array);
251 for (i = 0; i < old->max; i++) {
252 if (ovsl_dereference(old->masks[i]))
253 new->masks[count++] = old->masks[i];
258 rcu_assign_pointer(tbl->mask_array, new);
261 call_rcu(&old->rcu, mask_array_rcu_cb);
266 int ovs_flow_tbl_init(struct flow_table *table)
268 struct table_instance *ti, *ufid_ti;
269 struct mask_array *ma;
271 table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) *
272 MC_HASH_ENTRIES, __alignof__(struct mask_cache_entry));
273 if (!table->mask_cache)
276 ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
278 goto free_mask_cache;
280 ti = table_instance_alloc(TBL_MIN_BUCKETS);
282 goto free_mask_array;
284 ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
288 rcu_assign_pointer(table->ti, ti);
289 rcu_assign_pointer(table->ufid_ti, ufid_ti);
290 rcu_assign_pointer(table->mask_array, ma);
291 table->last_rehash = jiffies;
293 table->ufid_count = 0;
297 __table_instance_destroy(ti);
301 free_percpu(table->mask_cache);
305 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
307 struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
309 __table_instance_destroy(ti);
312 static void table_instance_destroy(struct table_instance *ti,
313 struct table_instance *ufid_ti,
325 for (i = 0; i < ti->n_buckets; i++) {
326 struct sw_flow *flow;
327 struct hlist_head *head = flex_array_get(ti->buckets, i);
328 struct hlist_node *n;
329 int ver = ti->node_ver;
330 int ufid_ver = ufid_ti->node_ver;
332 hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
333 hlist_del_rcu(&flow->flow_table.node[ver]);
334 if (ovs_identifier_is_ufid(&flow->id))
335 hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
336 ovs_flow_free(flow, deferred);
342 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
343 call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
345 __table_instance_destroy(ti);
346 __table_instance_destroy(ufid_ti);
350 /* No need for locking this function is called from RCU callback or
353 void ovs_flow_tbl_destroy(struct flow_table *table)
355 struct table_instance *ti = rcu_dereference_raw(table->ti);
356 struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
358 free_percpu(table->mask_cache);
359 kfree(rcu_dereference_raw(table->mask_array));
360 table_instance_destroy(ti, ufid_ti, false);
363 struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
364 u32 *bucket, u32 *last)
366 struct sw_flow *flow;
367 struct hlist_head *head;
372 while (*bucket < ti->n_buckets) {
374 head = flex_array_get(ti->buckets, *bucket);
375 hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
390 static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
392 hash = jhash_1word(hash, ti->hash_seed);
393 return flex_array_get(ti->buckets,
394 (hash & (ti->n_buckets - 1)));
397 static void table_instance_insert(struct table_instance *ti,
398 struct sw_flow *flow)
400 struct hlist_head *head;
402 head = find_bucket(ti, flow->flow_table.hash);
403 hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
406 static void ufid_table_instance_insert(struct table_instance *ti,
407 struct sw_flow *flow)
409 struct hlist_head *head;
411 head = find_bucket(ti, flow->ufid_table.hash);
412 hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
415 static void flow_table_copy_flows(struct table_instance *old,
416 struct table_instance *new, bool ufid)
421 old_ver = old->node_ver;
422 new->node_ver = !old_ver;
424 /* Insert in new table. */
425 for (i = 0; i < old->n_buckets; i++) {
426 struct sw_flow *flow;
427 struct hlist_head *head;
429 head = flex_array_get(old->buckets, i);
432 hlist_for_each_entry(flow, head,
433 ufid_table.node[old_ver])
434 ufid_table_instance_insert(new, flow);
436 hlist_for_each_entry(flow, head,
437 flow_table.node[old_ver])
438 table_instance_insert(new, flow);
441 old->keep_flows = true;
444 static struct table_instance *table_instance_rehash(struct table_instance *ti,
445 int n_buckets, bool ufid)
447 struct table_instance *new_ti;
449 new_ti = table_instance_alloc(n_buckets);
453 flow_table_copy_flows(ti, new_ti, ufid);
458 int ovs_flow_tbl_flush(struct flow_table *flow_table)
460 struct table_instance *old_ti, *new_ti;
461 struct table_instance *old_ufid_ti, *new_ufid_ti;
463 new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
466 new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
470 old_ti = ovsl_dereference(flow_table->ti);
471 old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
473 rcu_assign_pointer(flow_table->ti, new_ti);
474 rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
475 flow_table->last_rehash = jiffies;
476 flow_table->count = 0;
477 flow_table->ufid_count = 0;
479 table_instance_destroy(old_ti, old_ufid_ti, true);
483 __table_instance_destroy(new_ti);
487 static u32 flow_hash(const struct sw_flow_key *key,
488 const struct sw_flow_key_range *range)
490 int key_start = range->start;
491 int key_end = range->end;
492 const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
493 int hash_u32s = (key_end - key_start) >> 2;
495 /* Make sure number of hash bytes are multiple of u32. */
496 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
498 return jhash2(hash_key, hash_u32s, 0);
501 static int flow_key_start(const struct sw_flow_key *key)
506 return rounddown(offsetof(struct sw_flow_key, phy),
510 static bool cmp_key(const struct sw_flow_key *key1,
511 const struct sw_flow_key *key2,
512 int key_start, int key_end)
514 const long *cp1 = (const long *)((const u8 *)key1 + key_start);
515 const long *cp2 = (const long *)((const u8 *)key2 + key_start);
519 for (i = key_start; i < key_end; i += sizeof(long))
520 diffs |= *cp1++ ^ *cp2++;
525 static bool flow_cmp_masked_key(const struct sw_flow *flow,
526 const struct sw_flow_key *key,
527 const struct sw_flow_key_range *range)
529 return cmp_key(&flow->key, key, range->start, range->end);
532 static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
533 const struct sw_flow_match *match)
535 struct sw_flow_key *key = match->key;
536 int key_start = flow_key_start(key);
537 int key_end = match->range.end;
539 BUG_ON(ovs_identifier_is_ufid(&flow->id));
540 return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
543 static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
544 const struct sw_flow_key *unmasked,
545 const struct sw_flow_mask *mask,
548 struct sw_flow *flow;
549 struct hlist_head *head;
551 struct sw_flow_key masked_key;
553 ovs_flow_mask_key(&masked_key, unmasked, false, mask);
554 hash = flow_hash(&masked_key, &mask->range);
555 head = find_bucket(ti, hash);
557 hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
558 if (flow->mask == mask && flow->flow_table.hash == hash &&
559 flow_cmp_masked_key(flow, &masked_key, &mask->range))
565 /* Flow lookup does full lookup on flow table. It starts with
566 * mask from index passed in *index.
568 static struct sw_flow *flow_lookup(struct flow_table *tbl,
569 struct table_instance *ti,
570 const struct mask_array *ma,
571 const struct sw_flow_key *key,
575 struct sw_flow_mask *mask;
576 struct sw_flow *flow;
579 if (*index < ma->max) {
580 mask = rcu_dereference_ovsl(ma->masks[*index]);
582 flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
588 for (i = 0; i < ma->max; i++) {
593 mask = rcu_dereference_ovsl(ma->masks[i]);
597 flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
598 if (flow) { /* Found */
608 * mask_cache maps flow to probable mask. This cache is not tightly
609 * coupled cache, It means updates to mask list can result in inconsistent
610 * cache entry in mask cache.
611 * This is per cpu cache and is divided in MC_HASH_SEGS segments.
612 * In case of a hash collision the entry is hashed in next segment.
614 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
615 const struct sw_flow_key *key,
619 struct mask_array *ma = rcu_dereference(tbl->mask_array);
620 struct table_instance *ti = rcu_dereference(tbl->ti);
621 struct mask_cache_entry *entries, *ce;
622 struct sw_flow *flow;
627 if (unlikely(!skb_hash)) {
630 return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index);
633 /* Pre and post recirulation flows usually have the same skb_hash
634 * value. To avoid hash collisions, rehash the 'skb_hash' with
637 skb_hash = jhash_1word(skb_hash, key->recirc_id);
641 entries = this_cpu_ptr(tbl->mask_cache);
643 /* Find the cache entry 'ce' to operate on. */
644 for (seg = 0; seg < MC_HASH_SEGS; seg++) {
645 int index = hash & (MC_HASH_ENTRIES - 1);
646 struct mask_cache_entry *e;
649 if (e->skb_hash == skb_hash) {
650 flow = flow_lookup(tbl, ti, ma, key, n_mask_hit,
657 if (!ce || e->skb_hash < ce->skb_hash)
658 ce = e; /* A better replacement cache candidate. */
660 hash >>= MC_HASH_SHIFT;
663 /* Cache miss, do full lookup. */
664 flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &ce->mask_index);
666 ce->skb_hash = skb_hash;
671 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
672 const struct sw_flow_key *key)
674 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
675 struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
676 u32 __always_unused n_mask_hit;
679 return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index);
682 struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
683 const struct sw_flow_match *match)
685 struct mask_array *ma = ovsl_dereference(tbl->mask_array);
688 /* Always called under ovs-mutex. */
689 for (i = 0; i < ma->max; i++) {
690 struct table_instance *ti = ovsl_dereference(tbl->ti);
691 u32 __always_unused n_mask_hit;
692 struct sw_flow_mask *mask;
693 struct sw_flow *flow;
695 mask = ovsl_dereference(ma->masks[i]);
698 flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit);
699 if (flow && ovs_identifier_is_key(&flow->id) &&
700 ovs_flow_cmp_unmasked_key(flow, match))
706 static u32 ufid_hash(const struct sw_flow_id *sfid)
708 return jhash(sfid->ufid, sfid->ufid_len, 0);
711 static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
712 const struct sw_flow_id *sfid)
714 if (flow->id.ufid_len != sfid->ufid_len)
717 return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
720 bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
722 if (ovs_identifier_is_ufid(&flow->id))
723 return flow_cmp_masked_key(flow, match->key, &match->range);
725 return ovs_flow_cmp_unmasked_key(flow, match);
728 struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
729 const struct sw_flow_id *ufid)
731 struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
732 struct sw_flow *flow;
733 struct hlist_head *head;
736 hash = ufid_hash(ufid);
737 head = find_bucket(ti, hash);
738 hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
739 if (flow->ufid_table.hash == hash &&
740 ovs_flow_cmp_ufid(flow, ufid))
746 int ovs_flow_tbl_num_masks(const struct flow_table *table)
748 struct mask_array *ma;
750 ma = rcu_dereference_ovsl(table->mask_array);
754 static struct table_instance *table_instance_expand(struct table_instance *ti,
757 return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
760 static void tbl_mask_array_delete_mask(struct mask_array *ma,
761 struct sw_flow_mask *mask)
765 /* Remove the deleted mask pointers from the array */
766 for (i = 0; i < ma->max; i++) {
767 if (mask == ovsl_dereference(ma->masks[i])) {
768 RCU_INIT_POINTER(ma->masks[i], NULL);
770 kfree_rcu(mask, rcu);
777 /* Remove 'mask' from the mask list, if it is not needed any more. */
778 static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
781 /* ovs-lock is required to protect mask-refcount and
785 BUG_ON(!mask->ref_count);
788 if (!mask->ref_count) {
789 struct mask_array *ma;
791 ma = ovsl_dereference(tbl->mask_array);
792 tbl_mask_array_delete_mask(ma, mask);
794 /* Shrink the mask array if necessary. */
795 if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) &&
796 ma->count <= (ma->max / 3))
797 tbl_mask_array_realloc(tbl, ma->max / 2);
803 /* Must be called with OVS mutex held. */
804 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
806 struct table_instance *ti = ovsl_dereference(table->ti);
807 struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
809 BUG_ON(table->count == 0);
810 hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
812 if (ovs_identifier_is_ufid(&flow->id)) {
813 hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
817 /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
818 * accessible as long as the RCU read lock is held.
820 flow_mask_remove(table, flow->mask);
823 static struct sw_flow_mask *mask_alloc(void)
825 struct sw_flow_mask *mask;
827 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
834 static bool mask_equal(const struct sw_flow_mask *a,
835 const struct sw_flow_mask *b)
837 const u8 *a_ = (const u8 *)&a->key + a->range.start;
838 const u8 *b_ = (const u8 *)&b->key + b->range.start;
840 return (a->range.end == b->range.end)
841 && (a->range.start == b->range.start)
842 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
845 static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
846 const struct sw_flow_mask *mask)
848 struct mask_array *ma;
851 ma = ovsl_dereference(tbl->mask_array);
852 for (i = 0; i < ma->max; i++) {
853 struct sw_flow_mask *t;
855 t = ovsl_dereference(ma->masks[i]);
856 if (t && mask_equal(mask, t))
863 /* Add 'mask' into the mask list, if it is not already there. */
864 static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
865 const struct sw_flow_mask *new)
867 struct sw_flow_mask *mask;
869 mask = flow_mask_find(tbl, new);
871 struct mask_array *ma;
874 /* Allocate a new mask if none exsits. */
879 mask->key = new->key;
880 mask->range = new->range;
882 /* Add mask to mask-list. */
883 ma = ovsl_dereference(tbl->mask_array);
884 if (ma->count >= ma->max) {
887 err = tbl_mask_array_realloc(tbl, ma->max +
888 MASK_ARRAY_SIZE_MIN);
893 ma = ovsl_dereference(tbl->mask_array);
896 for (i = 0; i < ma->max; i++) {
897 struct sw_flow_mask *t;
899 t = ovsl_dereference(ma->masks[i]);
901 rcu_assign_pointer(ma->masks[i], mask);
908 BUG_ON(!mask->ref_count);
916 /* Must be called with OVS mutex held. */
917 static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
919 struct table_instance *new_ti = NULL;
920 struct table_instance *ti;
922 flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
923 ti = ovsl_dereference(table->ti);
924 table_instance_insert(ti, flow);
927 /* Expand table, if necessary, to make room. */
928 if (table->count > ti->n_buckets)
929 new_ti = table_instance_expand(ti, false);
930 else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
931 new_ti = table_instance_rehash(ti, ti->n_buckets, false);
934 rcu_assign_pointer(table->ti, new_ti);
935 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
936 table->last_rehash = jiffies;
940 /* Must be called with OVS mutex held. */
941 static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
943 struct table_instance *ti;
945 flow->ufid_table.hash = ufid_hash(&flow->id);
946 ti = ovsl_dereference(table->ufid_ti);
947 ufid_table_instance_insert(ti, flow);
950 /* Expand table, if necessary, to make room. */
951 if (table->ufid_count > ti->n_buckets) {
952 struct table_instance *new_ti;
954 new_ti = table_instance_expand(ti, true);
956 rcu_assign_pointer(table->ufid_ti, new_ti);
957 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
962 /* Must be called with OVS mutex held. */
963 int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
964 const struct sw_flow_mask *mask)
968 err = flow_mask_insert(table, flow, mask);
971 flow_key_insert(table, flow);
972 if (ovs_identifier_is_ufid(&flow->id))
973 flow_ufid_insert(table, flow);
978 /* Initializes the flow module.
979 * Returns zero if successful or a negative error code.
981 int ovs_flow_init(void)
983 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
984 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
986 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
988 * sizeof(struct flow_stats *)),
990 if (flow_cache == NULL)
994 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
995 0, SLAB_HWCACHE_ALIGN, NULL);
996 if (flow_stats_cache == NULL) {
997 kmem_cache_destroy(flow_cache);
1005 /* Uninitializes the flow module. */
1006 void ovs_flow_exit(void)
1008 kmem_cache_destroy(flow_stats_cache);
1009 kmem_cache_destroy(flow_cache);