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
, 0);
106 spin_lock_init(&stats
->lock
);
108 RCU_INIT_POINTER(flow
->stats
[0], stats
);
112 RCU_INIT_POINTER(flow
->stats
[node
], NULL
);
116 kmem_cache_free(flow_cache
, flow
);
117 return ERR_PTR(-ENOMEM
);
120 int ovs_flow_tbl_count(const struct flow_table
*table
)
125 static struct flex_array
*alloc_buckets(unsigned int n_buckets
)
127 struct flex_array
*buckets
;
130 buckets
= flex_array_alloc(sizeof(struct hlist_head
),
131 n_buckets
, GFP_KERNEL
);
135 err
= flex_array_prealloc(buckets
, 0, n_buckets
, GFP_KERNEL
);
137 flex_array_free(buckets
);
141 for (i
= 0; i
< n_buckets
; i
++)
142 INIT_HLIST_HEAD((struct hlist_head
*)
143 flex_array_get(buckets
, i
));
148 static void flow_free(struct sw_flow
*flow
)
152 if (ovs_identifier_is_key(&flow
->id
))
153 kfree(flow
->id
.unmasked_key
);
155 ovs_nla_free_flow_actions((struct sw_flow_actions __force
*)flow
->sf_acts
);
157 if (flow
->stats
[node
])
158 kmem_cache_free(flow_stats_cache
,
159 rcu_dereference_raw(flow
->stats
[node
]));
160 kmem_cache_free(flow_cache
, flow
);
163 static void rcu_free_flow_callback(struct rcu_head
*rcu
)
165 struct sw_flow
*flow
= container_of(rcu
, struct sw_flow
, rcu
);
170 void ovs_flow_free(struct sw_flow
*flow
, bool deferred
)
176 call_rcu(&flow
->rcu
, rcu_free_flow_callback
);
181 static void free_buckets(struct flex_array
*buckets
)
183 flex_array_free(buckets
);
187 static void __table_instance_destroy(struct table_instance
*ti
)
189 free_buckets(ti
->buckets
);
193 static struct table_instance
*table_instance_alloc(int new_size
)
195 struct table_instance
*ti
= kmalloc(sizeof(*ti
), GFP_KERNEL
);
200 ti
->buckets
= alloc_buckets(new_size
);
206 ti
->n_buckets
= new_size
;
208 ti
->keep_flows
= false;
209 get_random_bytes(&ti
->hash_seed
, sizeof(u32
));
214 static void mask_array_rcu_cb(struct rcu_head
*rcu
)
216 struct mask_array
*ma
= container_of(rcu
, struct mask_array
, rcu
);
221 static struct mask_array
*tbl_mask_array_alloc(int size
)
223 struct mask_array
*new;
225 size
= max(MASK_ARRAY_SIZE_MIN
, size
);
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
< old
->max
; i
++) {
251 if (ovsl_dereference(old
->masks
[i
]))
252 new->masks
[count
++] = old
->masks
[i
];
257 rcu_assign_pointer(tbl
->mask_array
, new);
260 call_rcu(&old
->rcu
, mask_array_rcu_cb
);
265 int ovs_flow_tbl_init(struct flow_table
*table
)
267 struct table_instance
*ti
, *ufid_ti
;
268 struct mask_array
*ma
;
270 table
->mask_cache
= __alloc_percpu(sizeof(struct mask_cache_entry
) *
271 MC_HASH_ENTRIES
, __alignof__(struct mask_cache_entry
));
272 if (!table
->mask_cache
)
275 ma
= tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN
);
277 goto free_mask_cache
;
279 ti
= table_instance_alloc(TBL_MIN_BUCKETS
);
281 goto free_mask_array
;
283 ufid_ti
= table_instance_alloc(TBL_MIN_BUCKETS
);
287 rcu_assign_pointer(table
->ti
, ti
);
288 rcu_assign_pointer(table
->ufid_ti
, ufid_ti
);
289 rcu_assign_pointer(table
->mask_array
, ma
);
290 table
->last_rehash
= jiffies
;
292 table
->ufid_count
= 0;
296 __table_instance_destroy(ti
);
300 free_percpu(table
->mask_cache
);
304 static void flow_tbl_destroy_rcu_cb(struct rcu_head
*rcu
)
306 struct table_instance
*ti
= container_of(rcu
, struct table_instance
, rcu
);
308 __table_instance_destroy(ti
);
311 static void table_instance_destroy(struct table_instance
*ti
,
312 struct table_instance
*ufid_ti
,
324 for (i
= 0; i
< ti
->n_buckets
; i
++) {
325 struct sw_flow
*flow
;
326 struct hlist_head
*head
= flex_array_get(ti
->buckets
, i
);
327 struct hlist_node
*n
;
328 int ver
= ti
->node_ver
;
329 int ufid_ver
= ufid_ti
->node_ver
;
331 hlist_for_each_entry_safe(flow
, n
, head
, flow_table
.node
[ver
]) {
332 hlist_del_rcu(&flow
->flow_table
.node
[ver
]);
333 if (ovs_identifier_is_ufid(&flow
->id
))
334 hlist_del_rcu(&flow
->ufid_table
.node
[ufid_ver
]);
335 ovs_flow_free(flow
, deferred
);
341 call_rcu(&ti
->rcu
, flow_tbl_destroy_rcu_cb
);
342 call_rcu(&ufid_ti
->rcu
, flow_tbl_destroy_rcu_cb
);
344 __table_instance_destroy(ti
);
345 __table_instance_destroy(ufid_ti
);
349 /* No need for locking this function is called from RCU callback or
352 void ovs_flow_tbl_destroy(struct flow_table
*table
)
354 struct table_instance
*ti
= rcu_dereference_raw(table
->ti
);
355 struct table_instance
*ufid_ti
= rcu_dereference_raw(table
->ufid_ti
);
357 free_percpu(table
->mask_cache
);
358 kfree(rcu_dereference_raw(table
->mask_array
));
359 table_instance_destroy(ti
, ufid_ti
, false);
362 struct sw_flow
*ovs_flow_tbl_dump_next(struct table_instance
*ti
,
363 u32
*bucket
, u32
*last
)
365 struct sw_flow
*flow
;
366 struct hlist_head
*head
;
371 while (*bucket
< ti
->n_buckets
) {
373 head
= flex_array_get(ti
->buckets
, *bucket
);
374 hlist_for_each_entry_rcu(flow
, head
, flow_table
.node
[ver
]) {
389 static struct hlist_head
*find_bucket(struct table_instance
*ti
, u32 hash
)
391 hash
= jhash_1word(hash
, ti
->hash_seed
);
392 return flex_array_get(ti
->buckets
,
393 (hash
& (ti
->n_buckets
- 1)));
396 static void table_instance_insert(struct table_instance
*ti
,
397 struct sw_flow
*flow
)
399 struct hlist_head
*head
;
401 head
= find_bucket(ti
, flow
->flow_table
.hash
);
402 hlist_add_head_rcu(&flow
->flow_table
.node
[ti
->node_ver
], head
);
405 static void ufid_table_instance_insert(struct table_instance
*ti
,
406 struct sw_flow
*flow
)
408 struct hlist_head
*head
;
410 head
= find_bucket(ti
, flow
->ufid_table
.hash
);
411 hlist_add_head_rcu(&flow
->ufid_table
.node
[ti
->node_ver
], head
);
414 static void flow_table_copy_flows(struct table_instance
*old
,
415 struct table_instance
*new, bool ufid
)
420 old_ver
= old
->node_ver
;
421 new->node_ver
= !old_ver
;
423 /* Insert in new table. */
424 for (i
= 0; i
< old
->n_buckets
; i
++) {
425 struct sw_flow
*flow
;
426 struct hlist_head
*head
;
428 head
= flex_array_get(old
->buckets
, i
);
431 hlist_for_each_entry(flow
, head
,
432 ufid_table
.node
[old_ver
])
433 ufid_table_instance_insert(new, flow
);
435 hlist_for_each_entry(flow
, head
,
436 flow_table
.node
[old_ver
])
437 table_instance_insert(new, flow
);
440 old
->keep_flows
= true;
443 static struct table_instance
*table_instance_rehash(struct table_instance
*ti
,
444 int n_buckets
, bool ufid
)
446 struct table_instance
*new_ti
;
448 new_ti
= table_instance_alloc(n_buckets
);
452 flow_table_copy_flows(ti
, new_ti
, ufid
);
457 int ovs_flow_tbl_flush(struct flow_table
*flow_table
)
459 struct table_instance
*old_ti
, *new_ti
;
460 struct table_instance
*old_ufid_ti
, *new_ufid_ti
;
462 new_ti
= table_instance_alloc(TBL_MIN_BUCKETS
);
465 new_ufid_ti
= table_instance_alloc(TBL_MIN_BUCKETS
);
469 old_ti
= ovsl_dereference(flow_table
->ti
);
470 old_ufid_ti
= ovsl_dereference(flow_table
->ufid_ti
);
472 rcu_assign_pointer(flow_table
->ti
, new_ti
);
473 rcu_assign_pointer(flow_table
->ufid_ti
, new_ufid_ti
);
474 flow_table
->last_rehash
= jiffies
;
475 flow_table
->count
= 0;
476 flow_table
->ufid_count
= 0;
478 table_instance_destroy(old_ti
, old_ufid_ti
, true);
482 __table_instance_destroy(new_ti
);
486 static u32
flow_hash(const struct sw_flow_key
*key
,
487 const struct sw_flow_key_range
*range
)
489 int key_start
= range
->start
;
490 int key_end
= range
->end
;
491 const u32
*hash_key
= (const u32
*)((const u8
*)key
+ key_start
);
492 int hash_u32s
= (key_end
- key_start
) >> 2;
494 /* Make sure number of hash bytes are multiple of u32. */
495 BUILD_BUG_ON(sizeof(long) % sizeof(u32
));
497 return jhash2(hash_key
, hash_u32s
, 0);
500 static int flow_key_start(const struct sw_flow_key
*key
)
502 if (key
->tun_key
.u
.ipv4
.dst
)
505 return rounddown(offsetof(struct sw_flow_key
, phy
),
509 static bool cmp_key(const struct sw_flow_key
*key1
,
510 const struct sw_flow_key
*key2
,
511 int key_start
, int key_end
)
513 const long *cp1
= (const long *)((const u8
*)key1
+ key_start
);
514 const long *cp2
= (const long *)((const u8
*)key2
+ key_start
);
518 for (i
= key_start
; i
< key_end
; i
+= sizeof(long))
519 diffs
|= *cp1
++ ^ *cp2
++;
524 static bool flow_cmp_masked_key(const struct sw_flow
*flow
,
525 const struct sw_flow_key
*key
,
526 const struct sw_flow_key_range
*range
)
528 return cmp_key(&flow
->key
, key
, range
->start
, range
->end
);
531 static bool ovs_flow_cmp_unmasked_key(const struct sw_flow
*flow
,
532 const struct sw_flow_match
*match
)
534 struct sw_flow_key
*key
= match
->key
;
535 int key_start
= flow_key_start(key
);
536 int key_end
= match
->range
.end
;
538 BUG_ON(ovs_identifier_is_ufid(&flow
->id
));
539 return cmp_key(flow
->id
.unmasked_key
, key
, key_start
, key_end
);
542 static struct sw_flow
*masked_flow_lookup(struct table_instance
*ti
,
543 const struct sw_flow_key
*unmasked
,
544 const struct sw_flow_mask
*mask
,
547 struct sw_flow
*flow
;
548 struct hlist_head
*head
;
550 struct sw_flow_key masked_key
;
552 ovs_flow_mask_key(&masked_key
, unmasked
, false, mask
);
553 hash
= flow_hash(&masked_key
, &mask
->range
);
554 head
= find_bucket(ti
, hash
);
556 hlist_for_each_entry_rcu(flow
, head
, flow_table
.node
[ti
->node_ver
]) {
557 if (flow
->mask
== mask
&& flow
->flow_table
.hash
== hash
&&
558 flow_cmp_masked_key(flow
, &masked_key
, &mask
->range
))
564 /* Flow lookup does full lookup on flow table. It starts with
565 * mask from index passed in *index.
567 static struct sw_flow
*flow_lookup(struct flow_table
*tbl
,
568 struct table_instance
*ti
,
569 const struct mask_array
*ma
,
570 const struct sw_flow_key
*key
,
574 struct sw_flow_mask
*mask
;
575 struct sw_flow
*flow
;
578 if (*index
< ma
->max
) {
579 mask
= rcu_dereference_ovsl(ma
->masks
[*index
]);
581 flow
= masked_flow_lookup(ti
, key
, mask
, n_mask_hit
);
587 for (i
= 0; i
< ma
->max
; i
++) {
592 mask
= rcu_dereference_ovsl(ma
->masks
[i
]);
596 flow
= masked_flow_lookup(ti
, key
, mask
, n_mask_hit
);
597 if (flow
) { /* Found */
607 * mask_cache maps flow to probable mask. This cache is not tightly
608 * coupled cache, It means updates to mask list can result in inconsistent
609 * cache entry in mask cache.
610 * This is per cpu cache and is divided in MC_HASH_SEGS segments.
611 * In case of a hash collision the entry is hashed in next segment.
613 struct sw_flow
*ovs_flow_tbl_lookup_stats(struct flow_table
*tbl
,
614 const struct sw_flow_key
*key
,
618 struct mask_array
*ma
= rcu_dereference(tbl
->mask_array
);
619 struct table_instance
*ti
= rcu_dereference(tbl
->ti
);
620 struct mask_cache_entry
*entries
, *ce
;
621 struct sw_flow
*flow
;
626 if (unlikely(!skb_hash
)) {
629 return flow_lookup(tbl
, ti
, ma
, key
, n_mask_hit
, &mask_index
);
632 /* Pre and post recirulation flows usually have the same skb_hash
633 * value. To avoid hash collisions, rehash the 'skb_hash' with
636 skb_hash
= jhash_1word(skb_hash
, key
->recirc_id
);
640 entries
= this_cpu_ptr(tbl
->mask_cache
);
642 /* Find the cache entry 'ce' to operate on. */
643 for (seg
= 0; seg
< MC_HASH_SEGS
; seg
++) {
644 int index
= hash
& (MC_HASH_ENTRIES
- 1);
645 struct mask_cache_entry
*e
;
648 if (e
->skb_hash
== skb_hash
) {
649 flow
= flow_lookup(tbl
, ti
, ma
, key
, n_mask_hit
,
656 if (!ce
|| e
->skb_hash
< ce
->skb_hash
)
657 ce
= e
; /* A better replacement cache candidate. */
659 hash
>>= MC_HASH_SHIFT
;
662 /* Cache miss, do full lookup. */
663 flow
= flow_lookup(tbl
, ti
, ma
, key
, n_mask_hit
, &ce
->mask_index
);
665 ce
->skb_hash
= skb_hash
;
670 struct sw_flow
*ovs_flow_tbl_lookup(struct flow_table
*tbl
,
671 const struct sw_flow_key
*key
)
673 struct table_instance
*ti
= rcu_dereference_ovsl(tbl
->ti
);
674 struct mask_array
*ma
= rcu_dereference_ovsl(tbl
->mask_array
);
675 u32 __always_unused n_mask_hit
;
678 return flow_lookup(tbl
, ti
, ma
, key
, &n_mask_hit
, &index
);
681 struct sw_flow
*ovs_flow_tbl_lookup_exact(struct flow_table
*tbl
,
682 const struct sw_flow_match
*match
)
684 struct mask_array
*ma
= ovsl_dereference(tbl
->mask_array
);
687 /* Always called under ovs-mutex. */
688 for (i
= 0; i
< ma
->max
; i
++) {
689 struct table_instance
*ti
= ovsl_dereference(tbl
->ti
);
690 u32 __always_unused n_mask_hit
;
691 struct sw_flow_mask
*mask
;
692 struct sw_flow
*flow
;
694 mask
= ovsl_dereference(ma
->masks
[i
]);
697 flow
= masked_flow_lookup(ti
, match
->key
, mask
, &n_mask_hit
);
698 if (flow
&& ovs_identifier_is_key(&flow
->id
) &&
699 ovs_flow_cmp_unmasked_key(flow
, match
))
705 static u32
ufid_hash(const struct sw_flow_id
*sfid
)
707 return jhash(sfid
->ufid
, sfid
->ufid_len
, 0);
710 static bool ovs_flow_cmp_ufid(const struct sw_flow
*flow
,
711 const struct sw_flow_id
*sfid
)
713 if (flow
->id
.ufid_len
!= sfid
->ufid_len
)
716 return !memcmp(flow
->id
.ufid
, sfid
->ufid
, sfid
->ufid_len
);
719 bool ovs_flow_cmp(const struct sw_flow
*flow
, const struct sw_flow_match
*match
)
721 if (ovs_identifier_is_ufid(&flow
->id
))
722 return flow_cmp_masked_key(flow
, match
->key
, &match
->range
);
724 return ovs_flow_cmp_unmasked_key(flow
, match
);
727 struct sw_flow
*ovs_flow_tbl_lookup_ufid(struct flow_table
*tbl
,
728 const struct sw_flow_id
*ufid
)
730 struct table_instance
*ti
= rcu_dereference_ovsl(tbl
->ufid_ti
);
731 struct sw_flow
*flow
;
732 struct hlist_head
*head
;
735 hash
= ufid_hash(ufid
);
736 head
= find_bucket(ti
, hash
);
737 hlist_for_each_entry_rcu(flow
, head
, ufid_table
.node
[ti
->node_ver
]) {
738 if (flow
->ufid_table
.hash
== hash
&&
739 ovs_flow_cmp_ufid(flow
, ufid
))
745 int ovs_flow_tbl_num_masks(const struct flow_table
*table
)
747 struct mask_array
*ma
;
749 ma
= rcu_dereference_ovsl(table
->mask_array
);
753 static struct table_instance
*table_instance_expand(struct table_instance
*ti
,
756 return table_instance_rehash(ti
, ti
->n_buckets
* 2, ufid
);
759 static void tbl_mask_array_delete_mask(struct mask_array
*ma
,
760 struct sw_flow_mask
*mask
)
764 /* Remove the deleted mask pointers from the array */
765 for (i
= 0; i
< ma
->max
; i
++) {
766 if (mask
== ovsl_dereference(ma
->masks
[i
])) {
767 RCU_INIT_POINTER(ma
->masks
[i
], NULL
);
769 kfree_rcu(mask
, rcu
);
776 /* Remove 'mask' from the mask list, if it is not needed any more. */
777 static void flow_mask_remove(struct flow_table
*tbl
, struct sw_flow_mask
*mask
)
780 /* ovs-lock is required to protect mask-refcount and
784 BUG_ON(!mask
->ref_count
);
787 if (!mask
->ref_count
) {
788 struct mask_array
*ma
;
790 ma
= ovsl_dereference(tbl
->mask_array
);
791 tbl_mask_array_delete_mask(ma
, mask
);
793 /* Shrink the mask array if necessary. */
794 if (ma
->max
>= (MASK_ARRAY_SIZE_MIN
* 2) &&
795 ma
->count
<= (ma
->max
/ 3))
796 tbl_mask_array_realloc(tbl
, ma
->max
/ 2);
802 /* Must be called with OVS mutex held. */
803 void ovs_flow_tbl_remove(struct flow_table
*table
, struct sw_flow
*flow
)
805 struct table_instance
*ti
= ovsl_dereference(table
->ti
);
806 struct table_instance
*ufid_ti
= ovsl_dereference(table
->ufid_ti
);
808 BUG_ON(table
->count
== 0);
809 hlist_del_rcu(&flow
->flow_table
.node
[ti
->node_ver
]);
811 if (ovs_identifier_is_ufid(&flow
->id
)) {
812 hlist_del_rcu(&flow
->ufid_table
.node
[ufid_ti
->node_ver
]);
816 /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
817 * accessible as long as the RCU read lock is held.
819 flow_mask_remove(table
, flow
->mask
);
822 static struct sw_flow_mask
*mask_alloc(void)
824 struct sw_flow_mask
*mask
;
826 mask
= kmalloc(sizeof(*mask
), GFP_KERNEL
);
833 static bool mask_equal(const struct sw_flow_mask
*a
,
834 const struct sw_flow_mask
*b
)
836 const u8
*a_
= (const u8
*)&a
->key
+ a
->range
.start
;
837 const u8
*b_
= (const u8
*)&b
->key
+ b
->range
.start
;
839 return (a
->range
.end
== b
->range
.end
)
840 && (a
->range
.start
== b
->range
.start
)
841 && (memcmp(a_
, b_
, range_n_bytes(&a
->range
)) == 0);
844 static struct sw_flow_mask
*flow_mask_find(const struct flow_table
*tbl
,
845 const struct sw_flow_mask
*mask
)
847 struct mask_array
*ma
;
850 ma
= ovsl_dereference(tbl
->mask_array
);
851 for (i
= 0; i
< ma
->max
; i
++) {
852 struct sw_flow_mask
*t
;
854 t
= ovsl_dereference(ma
->masks
[i
]);
855 if (t
&& mask_equal(mask
, t
))
862 /* Add 'mask' into the mask list, if it is not already there. */
863 static int flow_mask_insert(struct flow_table
*tbl
, struct sw_flow
*flow
,
864 const struct sw_flow_mask
*new)
866 struct sw_flow_mask
*mask
;
868 mask
= flow_mask_find(tbl
, new);
870 struct mask_array
*ma
;
873 /* Allocate a new mask if none exsits. */
878 mask
->key
= new->key
;
879 mask
->range
= new->range
;
881 /* Add mask to mask-list. */
882 ma
= ovsl_dereference(tbl
->mask_array
);
883 if (ma
->count
>= ma
->max
) {
886 err
= tbl_mask_array_realloc(tbl
, ma
->max
+
887 MASK_ARRAY_SIZE_MIN
);
892 ma
= ovsl_dereference(tbl
->mask_array
);
895 for (i
= 0; i
< ma
->max
; i
++) {
896 struct sw_flow_mask
*t
;
898 t
= ovsl_dereference(ma
->masks
[i
]);
900 rcu_assign_pointer(ma
->masks
[i
], mask
);
907 BUG_ON(!mask
->ref_count
);
915 /* Must be called with OVS mutex held. */
916 static void flow_key_insert(struct flow_table
*table
, struct sw_flow
*flow
)
918 struct table_instance
*new_ti
= NULL
;
919 struct table_instance
*ti
;
921 flow
->flow_table
.hash
= flow_hash(&flow
->key
, &flow
->mask
->range
);
922 ti
= ovsl_dereference(table
->ti
);
923 table_instance_insert(ti
, flow
);
926 /* Expand table, if necessary, to make room. */
927 if (table
->count
> ti
->n_buckets
)
928 new_ti
= table_instance_expand(ti
, false);
929 else if (time_after(jiffies
, table
->last_rehash
+ REHASH_INTERVAL
))
930 new_ti
= table_instance_rehash(ti
, ti
->n_buckets
, false);
933 rcu_assign_pointer(table
->ti
, new_ti
);
934 call_rcu(&ti
->rcu
, flow_tbl_destroy_rcu_cb
);
935 table
->last_rehash
= jiffies
;
939 /* Must be called with OVS mutex held. */
940 static void flow_ufid_insert(struct flow_table
*table
, struct sw_flow
*flow
)
942 struct table_instance
*ti
;
944 flow
->ufid_table
.hash
= ufid_hash(&flow
->id
);
945 ti
= ovsl_dereference(table
->ufid_ti
);
946 ufid_table_instance_insert(ti
, flow
);
949 /* Expand table, if necessary, to make room. */
950 if (table
->ufid_count
> ti
->n_buckets
) {
951 struct table_instance
*new_ti
;
953 new_ti
= table_instance_expand(ti
, true);
955 rcu_assign_pointer(table
->ufid_ti
, new_ti
);
956 call_rcu(&ti
->rcu
, flow_tbl_destroy_rcu_cb
);
961 /* Must be called with OVS mutex held. */
962 int ovs_flow_tbl_insert(struct flow_table
*table
, struct sw_flow
*flow
,
963 const struct sw_flow_mask
*mask
)
967 err
= flow_mask_insert(table
, flow
, mask
);
970 flow_key_insert(table
, flow
);
971 if (ovs_identifier_is_ufid(&flow
->id
))
972 flow_ufid_insert(table
, flow
);
977 /* Initializes the flow module.
978 * Returns zero if successful or a negative error code.
980 int ovs_flow_init(void)
982 BUILD_BUG_ON(__alignof__(struct sw_flow_key
) % __alignof__(long));
983 BUILD_BUG_ON(sizeof(struct sw_flow_key
) % sizeof(long));
985 flow_cache
= kmem_cache_create("sw_flow", sizeof(struct sw_flow
)
987 * sizeof(struct flow_stats
*)),
989 if (flow_cache
== NULL
)
993 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats
),
994 0, SLAB_HWCACHE_ALIGN
, NULL
);
995 if (flow_stats_cache
== NULL
) {
996 kmem_cache_destroy(flow_cache
);
1004 /* Uninitializes the flow module. */
1005 void ovs_flow_exit(void)
1007 kmem_cache_destroy(flow_stats_cache
);
1008 kmem_cache_destroy(flow_cache
);