xref: /openbmc/linux/net/openvswitch/flow_table.c (revision 74ed7ab9264c54471c7f057409d352052820d750)
1e6445719SPravin B Shelar /*
29b996e54SPravin B Shelar  * Copyright (c) 2007-2014 Nicira, Inc.
3e6445719SPravin B Shelar  *
4e6445719SPravin B Shelar  * This program is free software; you can redistribute it and/or
5e6445719SPravin B Shelar  * modify it under the terms of version 2 of the GNU General Public
6e6445719SPravin B Shelar  * License as published by the Free Software Foundation.
7e6445719SPravin B Shelar  *
8e6445719SPravin B Shelar  * This program is distributed in the hope that it will be useful, but
9e6445719SPravin B Shelar  * WITHOUT ANY WARRANTY; without even the implied warranty of
10e6445719SPravin B Shelar  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11e6445719SPravin B Shelar  * General Public License for more details.
12e6445719SPravin B Shelar  *
13e6445719SPravin B Shelar  * You should have received a copy of the GNU General Public License
14e6445719SPravin B Shelar  * along with this program; if not, write to the Free Software
15e6445719SPravin B Shelar  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16e6445719SPravin B Shelar  * 02110-1301, USA
17e6445719SPravin B Shelar  */
18e6445719SPravin B Shelar 
19e6445719SPravin B Shelar #include "flow.h"
20e6445719SPravin B Shelar #include "datapath.h"
21e6445719SPravin B Shelar #include <linux/uaccess.h>
22e6445719SPravin B Shelar #include <linux/netdevice.h>
23e6445719SPravin B Shelar #include <linux/etherdevice.h>
24e6445719SPravin B Shelar #include <linux/if_ether.h>
25e6445719SPravin B Shelar #include <linux/if_vlan.h>
26e6445719SPravin B Shelar #include <net/llc_pdu.h>
27e6445719SPravin B Shelar #include <linux/kernel.h>
2887545899SDaniel Borkmann #include <linux/jhash.h>
29e6445719SPravin B Shelar #include <linux/jiffies.h>
30e6445719SPravin B Shelar #include <linux/llc.h>
31e6445719SPravin B Shelar #include <linux/module.h>
32e6445719SPravin B Shelar #include <linux/in.h>
33e6445719SPravin B Shelar #include <linux/rcupdate.h>
34e6445719SPravin B Shelar #include <linux/if_arp.h>
35e6445719SPravin B Shelar #include <linux/ip.h>
36e6445719SPravin B Shelar #include <linux/ipv6.h>
37e6445719SPravin B Shelar #include <linux/sctp.h>
38e6445719SPravin B Shelar #include <linux/tcp.h>
39e6445719SPravin B Shelar #include <linux/udp.h>
40e6445719SPravin B Shelar #include <linux/icmp.h>
41e6445719SPravin B Shelar #include <linux/icmpv6.h>
42e6445719SPravin B Shelar #include <linux/rculist.h>
43e6445719SPravin B Shelar #include <net/ip.h>
44e6445719SPravin B Shelar #include <net/ipv6.h>
45e6445719SPravin B Shelar #include <net/ndisc.h>
46e6445719SPravin B Shelar 
47b637e498SPravin B Shelar #define TBL_MIN_BUCKETS		1024
48b637e498SPravin B Shelar #define REHASH_INTERVAL		(10 * 60 * HZ)
49b637e498SPravin B Shelar 
50e6445719SPravin B Shelar static struct kmem_cache *flow_cache;
5163e7959cSJarno Rajahalme struct kmem_cache *flow_stats_cache __read_mostly;
52e6445719SPravin B Shelar 
53e6445719SPravin B Shelar static u16 range_n_bytes(const struct sw_flow_key_range *range)
54e6445719SPravin B Shelar {
55e6445719SPravin B Shelar 	return range->end - range->start;
56e6445719SPravin B Shelar }
57e6445719SPravin B Shelar 
58e6445719SPravin B Shelar void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
59e6445719SPravin B Shelar 		       const struct sw_flow_mask *mask)
60e6445719SPravin B Shelar {
617085130bSDaniele Di Proietto 	const long *m = (const long *)((const u8 *)&mask->key +
627085130bSDaniele Di Proietto 				mask->range.start);
637085130bSDaniele Di Proietto 	const long *s = (const long *)((const u8 *)src +
647085130bSDaniele Di Proietto 				mask->range.start);
65e6445719SPravin B Shelar 	long *d = (long *)((u8 *)dst + mask->range.start);
66e6445719SPravin B Shelar 	int i;
67e6445719SPravin B Shelar 
68e6445719SPravin B Shelar 	/* The memory outside of the 'mask->range' are not set since
69e6445719SPravin B Shelar 	 * further operations on 'dst' only uses contents within
70e6445719SPravin B Shelar 	 * 'mask->range'.
71e6445719SPravin B Shelar 	 */
72e6445719SPravin B Shelar 	for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
73e6445719SPravin B Shelar 		*d++ = *s++ & *m++;
74e6445719SPravin B Shelar }
75e6445719SPravin B Shelar 
7623dabf88SJarno Rajahalme struct sw_flow *ovs_flow_alloc(void)
77e6445719SPravin B Shelar {
78e6445719SPravin B Shelar 	struct sw_flow *flow;
7963e7959cSJarno Rajahalme 	struct flow_stats *stats;
8063e7959cSJarno Rajahalme 	int node;
81e6445719SPravin B Shelar 
82e6445719SPravin B Shelar 	flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
83e6445719SPravin B Shelar 	if (!flow)
84e6445719SPravin B Shelar 		return ERR_PTR(-ENOMEM);
85e6445719SPravin B Shelar 
86e6445719SPravin B Shelar 	flow->sf_acts = NULL;
87e6445719SPravin B Shelar 	flow->mask = NULL;
8863e7959cSJarno Rajahalme 	flow->stats_last_writer = NUMA_NO_NODE;
89e6445719SPravin B Shelar 
9063e7959cSJarno Rajahalme 	/* Initialize the default stat node. */
9163e7959cSJarno Rajahalme 	stats = kmem_cache_alloc_node(flow_stats_cache,
9263e7959cSJarno Rajahalme 				      GFP_KERNEL | __GFP_ZERO, 0);
9363e7959cSJarno Rajahalme 	if (!stats)
94e298e505SPravin B Shelar 		goto err;
95e298e505SPravin B Shelar 
9663e7959cSJarno Rajahalme 	spin_lock_init(&stats->lock);
97e298e505SPravin B Shelar 
9863e7959cSJarno Rajahalme 	RCU_INIT_POINTER(flow->stats[0], stats);
9963e7959cSJarno Rajahalme 
10063e7959cSJarno Rajahalme 	for_each_node(node)
10163e7959cSJarno Rajahalme 		if (node != 0)
10263e7959cSJarno Rajahalme 			RCU_INIT_POINTER(flow->stats[node], NULL);
10363e7959cSJarno Rajahalme 
104e6445719SPravin B Shelar 	return flow;
105e298e505SPravin B Shelar err:
106ece37c87SWei Yongjun 	kmem_cache_free(flow_cache, flow);
107e298e505SPravin B Shelar 	return ERR_PTR(-ENOMEM);
108e6445719SPravin B Shelar }
109e6445719SPravin B Shelar 
11012eb18f7SThomas Graf int ovs_flow_tbl_count(const struct flow_table *table)
111b637e498SPravin B Shelar {
112b637e498SPravin B Shelar 	return table->count;
113b637e498SPravin B Shelar }
114b637e498SPravin B Shelar 
115e6445719SPravin B Shelar static struct flex_array *alloc_buckets(unsigned int n_buckets)
116e6445719SPravin B Shelar {
117e6445719SPravin B Shelar 	struct flex_array *buckets;
118e6445719SPravin B Shelar 	int i, err;
119e6445719SPravin B Shelar 
120e6445719SPravin B Shelar 	buckets = flex_array_alloc(sizeof(struct hlist_head),
121e6445719SPravin B Shelar 				   n_buckets, GFP_KERNEL);
122e6445719SPravin B Shelar 	if (!buckets)
123e6445719SPravin B Shelar 		return NULL;
124e6445719SPravin B Shelar 
125e6445719SPravin B Shelar 	err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
126e6445719SPravin B Shelar 	if (err) {
127e6445719SPravin B Shelar 		flex_array_free(buckets);
128e6445719SPravin B Shelar 		return NULL;
129e6445719SPravin B Shelar 	}
130e6445719SPravin B Shelar 
131e6445719SPravin B Shelar 	for (i = 0; i < n_buckets; i++)
132e6445719SPravin B Shelar 		INIT_HLIST_HEAD((struct hlist_head *)
133e6445719SPravin B Shelar 					flex_array_get(buckets, i));
134e6445719SPravin B Shelar 
135e6445719SPravin B Shelar 	return buckets;
136e6445719SPravin B Shelar }
137e6445719SPravin B Shelar 
138e6445719SPravin B Shelar static void flow_free(struct sw_flow *flow)
139e6445719SPravin B Shelar {
14063e7959cSJarno Rajahalme 	int node;
14163e7959cSJarno Rajahalme 
142*74ed7ab9SJoe Stringer 	if (ovs_identifier_is_key(&flow->id))
143*74ed7ab9SJoe Stringer 		kfree(flow->id.unmasked_key);
144eb072659SJarno Rajahalme 	kfree((struct sw_flow_actions __force *)flow->sf_acts);
14563e7959cSJarno Rajahalme 	for_each_node(node)
14663e7959cSJarno Rajahalme 		if (flow->stats[node])
14763e7959cSJarno Rajahalme 			kmem_cache_free(flow_stats_cache,
14863e7959cSJarno Rajahalme 					(struct flow_stats __force *)flow->stats[node]);
149e6445719SPravin B Shelar 	kmem_cache_free(flow_cache, flow);
150e6445719SPravin B Shelar }
151e6445719SPravin B Shelar 
152e6445719SPravin B Shelar static void rcu_free_flow_callback(struct rcu_head *rcu)
153e6445719SPravin B Shelar {
154e6445719SPravin B Shelar 	struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
155e6445719SPravin B Shelar 
156e6445719SPravin B Shelar 	flow_free(flow);
157e6445719SPravin B Shelar }
158e6445719SPravin B Shelar 
159e80857ccSAndy Zhou void ovs_flow_free(struct sw_flow *flow, bool deferred)
160618ed0c8SPravin B Shelar {
161e80857ccSAndy Zhou 	if (!flow)
162618ed0c8SPravin B Shelar 		return;
163618ed0c8SPravin B Shelar 
164e6445719SPravin B Shelar 	if (deferred)
165e6445719SPravin B Shelar 		call_rcu(&flow->rcu, rcu_free_flow_callback);
166e6445719SPravin B Shelar 	else
167e6445719SPravin B Shelar 		flow_free(flow);
168e6445719SPravin B Shelar }
169e6445719SPravin B Shelar 
170e6445719SPravin B Shelar static void free_buckets(struct flex_array *buckets)
171e6445719SPravin B Shelar {
172e6445719SPravin B Shelar 	flex_array_free(buckets);
173e6445719SPravin B Shelar }
174e6445719SPravin B Shelar 
175e80857ccSAndy Zhou 
176b637e498SPravin B Shelar static void __table_instance_destroy(struct table_instance *ti)
177e6445719SPravin B Shelar {
178b637e498SPravin B Shelar 	free_buckets(ti->buckets);
179b637e498SPravin B Shelar 	kfree(ti);
180e6445719SPravin B Shelar }
181e6445719SPravin B Shelar 
182b637e498SPravin B Shelar static struct table_instance *table_instance_alloc(int new_size)
183e6445719SPravin B Shelar {
184b637e498SPravin B Shelar 	struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
185e6445719SPravin B Shelar 
186b637e498SPravin B Shelar 	if (!ti)
187e6445719SPravin B Shelar 		return NULL;
188e6445719SPravin B Shelar 
189b637e498SPravin B Shelar 	ti->buckets = alloc_buckets(new_size);
190e6445719SPravin B Shelar 
191b637e498SPravin B Shelar 	if (!ti->buckets) {
192b637e498SPravin B Shelar 		kfree(ti);
193e6445719SPravin B Shelar 		return NULL;
194e6445719SPravin B Shelar 	}
195b637e498SPravin B Shelar 	ti->n_buckets = new_size;
196b637e498SPravin B Shelar 	ti->node_ver = 0;
197b637e498SPravin B Shelar 	ti->keep_flows = false;
198b637e498SPravin B Shelar 	get_random_bytes(&ti->hash_seed, sizeof(u32));
199b637e498SPravin B Shelar 
200b637e498SPravin B Shelar 	return ti;
201b637e498SPravin B Shelar }
202b637e498SPravin B Shelar 
203b637e498SPravin B Shelar int ovs_flow_tbl_init(struct flow_table *table)
204b637e498SPravin B Shelar {
205*74ed7ab9SJoe Stringer 	struct table_instance *ti, *ufid_ti;
206b637e498SPravin B Shelar 
207b637e498SPravin B Shelar 	ti = table_instance_alloc(TBL_MIN_BUCKETS);
208b637e498SPravin B Shelar 
209b637e498SPravin B Shelar 	if (!ti)
210b637e498SPravin B Shelar 		return -ENOMEM;
211b637e498SPravin B Shelar 
212*74ed7ab9SJoe Stringer 	ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
213*74ed7ab9SJoe Stringer 	if (!ufid_ti)
214*74ed7ab9SJoe Stringer 		goto free_ti;
215*74ed7ab9SJoe Stringer 
216b637e498SPravin B Shelar 	rcu_assign_pointer(table->ti, ti);
217*74ed7ab9SJoe Stringer 	rcu_assign_pointer(table->ufid_ti, ufid_ti);
218b637e498SPravin B Shelar 	INIT_LIST_HEAD(&table->mask_list);
219b637e498SPravin B Shelar 	table->last_rehash = jiffies;
220e6445719SPravin B Shelar 	table->count = 0;
221*74ed7ab9SJoe Stringer 	table->ufid_count = 0;
222b637e498SPravin B Shelar 	return 0;
223*74ed7ab9SJoe Stringer 
224*74ed7ab9SJoe Stringer free_ti:
225*74ed7ab9SJoe Stringer 	__table_instance_destroy(ti);
226*74ed7ab9SJoe Stringer 	return -ENOMEM;
227e6445719SPravin B Shelar }
228e6445719SPravin B Shelar 
229e6445719SPravin B Shelar static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
230e6445719SPravin B Shelar {
231b637e498SPravin B Shelar 	struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
232e6445719SPravin B Shelar 
233b637e498SPravin B Shelar 	__table_instance_destroy(ti);
234b637e498SPravin B Shelar }
235b637e498SPravin B Shelar 
236*74ed7ab9SJoe Stringer static void table_instance_destroy(struct table_instance *ti,
237*74ed7ab9SJoe Stringer 				   struct table_instance *ufid_ti,
238*74ed7ab9SJoe Stringer 				   bool deferred)
239b637e498SPravin B Shelar {
240e80857ccSAndy Zhou 	int i;
241e80857ccSAndy Zhou 
242b637e498SPravin B Shelar 	if (!ti)
243b637e498SPravin B Shelar 		return;
244b637e498SPravin B Shelar 
245*74ed7ab9SJoe Stringer 	BUG_ON(!ufid_ti);
246e80857ccSAndy Zhou 	if (ti->keep_flows)
247e80857ccSAndy Zhou 		goto skip_flows;
248e80857ccSAndy Zhou 
249e80857ccSAndy Zhou 	for (i = 0; i < ti->n_buckets; i++) {
250e80857ccSAndy Zhou 		struct sw_flow *flow;
251e80857ccSAndy Zhou 		struct hlist_head *head = flex_array_get(ti->buckets, i);
252e80857ccSAndy Zhou 		struct hlist_node *n;
253e80857ccSAndy Zhou 		int ver = ti->node_ver;
254*74ed7ab9SJoe Stringer 		int ufid_ver = ufid_ti->node_ver;
255e80857ccSAndy Zhou 
256*74ed7ab9SJoe Stringer 		hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
257*74ed7ab9SJoe Stringer 			hlist_del_rcu(&flow->flow_table.node[ver]);
258*74ed7ab9SJoe Stringer 			if (ovs_identifier_is_ufid(&flow->id))
259*74ed7ab9SJoe Stringer 				hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
260e80857ccSAndy Zhou 			ovs_flow_free(flow, deferred);
261e80857ccSAndy Zhou 		}
262e80857ccSAndy Zhou 	}
263e80857ccSAndy Zhou 
264e80857ccSAndy Zhou skip_flows:
265*74ed7ab9SJoe Stringer 	if (deferred) {
266b637e498SPravin B Shelar 		call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
267*74ed7ab9SJoe Stringer 		call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
268*74ed7ab9SJoe Stringer 	} else {
269b637e498SPravin B Shelar 		__table_instance_destroy(ti);
270*74ed7ab9SJoe Stringer 		__table_instance_destroy(ufid_ti);
271*74ed7ab9SJoe Stringer 	}
272e6445719SPravin B Shelar }
273e6445719SPravin B Shelar 
2749b996e54SPravin B Shelar /* No need for locking this function is called from RCU callback or
2759b996e54SPravin B Shelar  * error path.
2769b996e54SPravin B Shelar  */
2779b996e54SPravin B Shelar void ovs_flow_tbl_destroy(struct flow_table *table)
278e6445719SPravin B Shelar {
2799b996e54SPravin B Shelar 	struct table_instance *ti = rcu_dereference_raw(table->ti);
280*74ed7ab9SJoe Stringer 	struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
281e6445719SPravin B Shelar 
282*74ed7ab9SJoe Stringer 	table_instance_destroy(ti, ufid_ti, false);
283e6445719SPravin B Shelar }
284e6445719SPravin B Shelar 
285b637e498SPravin B Shelar struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
286e6445719SPravin B Shelar 				       u32 *bucket, u32 *last)
287e6445719SPravin B Shelar {
288e6445719SPravin B Shelar 	struct sw_flow *flow;
289e6445719SPravin B Shelar 	struct hlist_head *head;
290e6445719SPravin B Shelar 	int ver;
291e6445719SPravin B Shelar 	int i;
292e6445719SPravin B Shelar 
293b637e498SPravin B Shelar 	ver = ti->node_ver;
294b637e498SPravin B Shelar 	while (*bucket < ti->n_buckets) {
295e6445719SPravin B Shelar 		i = 0;
296b637e498SPravin B Shelar 		head = flex_array_get(ti->buckets, *bucket);
297*74ed7ab9SJoe Stringer 		hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
298e6445719SPravin B Shelar 			if (i < *last) {
299e6445719SPravin B Shelar 				i++;
300e6445719SPravin B Shelar 				continue;
301e6445719SPravin B Shelar 			}
302e6445719SPravin B Shelar 			*last = i + 1;
303e6445719SPravin B Shelar 			return flow;
304e6445719SPravin B Shelar 		}
305e6445719SPravin B Shelar 		(*bucket)++;
306e6445719SPravin B Shelar 		*last = 0;
307e6445719SPravin B Shelar 	}
308e6445719SPravin B Shelar 
309e6445719SPravin B Shelar 	return NULL;
310e6445719SPravin B Shelar }
311e6445719SPravin B Shelar 
312b637e498SPravin B Shelar static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
313e6445719SPravin B Shelar {
314b637e498SPravin B Shelar 	hash = jhash_1word(hash, ti->hash_seed);
315b637e498SPravin B Shelar 	return flex_array_get(ti->buckets,
316b637e498SPravin B Shelar 				(hash & (ti->n_buckets - 1)));
317e6445719SPravin B Shelar }
318e6445719SPravin B Shelar 
319*74ed7ab9SJoe Stringer static void table_instance_insert(struct table_instance *ti,
320*74ed7ab9SJoe Stringer 				  struct sw_flow *flow)
321e6445719SPravin B Shelar {
322e6445719SPravin B Shelar 	struct hlist_head *head;
323e6445719SPravin B Shelar 
324*74ed7ab9SJoe Stringer 	head = find_bucket(ti, flow->flow_table.hash);
325*74ed7ab9SJoe Stringer 	hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
326*74ed7ab9SJoe Stringer }
327*74ed7ab9SJoe Stringer 
328*74ed7ab9SJoe Stringer static void ufid_table_instance_insert(struct table_instance *ti,
329*74ed7ab9SJoe Stringer 				       struct sw_flow *flow)
330*74ed7ab9SJoe Stringer {
331*74ed7ab9SJoe Stringer 	struct hlist_head *head;
332*74ed7ab9SJoe Stringer 
333*74ed7ab9SJoe Stringer 	head = find_bucket(ti, flow->ufid_table.hash);
334*74ed7ab9SJoe Stringer 	hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
335e6445719SPravin B Shelar }
336e6445719SPravin B Shelar 
337b637e498SPravin B Shelar static void flow_table_copy_flows(struct table_instance *old,
338*74ed7ab9SJoe Stringer 				  struct table_instance *new, bool ufid)
339e6445719SPravin B Shelar {
340e6445719SPravin B Shelar 	int old_ver;
341e6445719SPravin B Shelar 	int i;
342e6445719SPravin B Shelar 
343e6445719SPravin B Shelar 	old_ver = old->node_ver;
344e6445719SPravin B Shelar 	new->node_ver = !old_ver;
345e6445719SPravin B Shelar 
346e6445719SPravin B Shelar 	/* Insert in new table. */
347e6445719SPravin B Shelar 	for (i = 0; i < old->n_buckets; i++) {
348e6445719SPravin B Shelar 		struct sw_flow *flow;
349e6445719SPravin B Shelar 		struct hlist_head *head;
350e6445719SPravin B Shelar 
351e6445719SPravin B Shelar 		head = flex_array_get(old->buckets, i);
352e6445719SPravin B Shelar 
353*74ed7ab9SJoe Stringer 		if (ufid)
354*74ed7ab9SJoe Stringer 			hlist_for_each_entry(flow, head,
355*74ed7ab9SJoe Stringer 					     ufid_table.node[old_ver])
356*74ed7ab9SJoe Stringer 				ufid_table_instance_insert(new, flow);
357*74ed7ab9SJoe Stringer 		else
358*74ed7ab9SJoe Stringer 			hlist_for_each_entry(flow, head,
359*74ed7ab9SJoe Stringer 					     flow_table.node[old_ver])
360b637e498SPravin B Shelar 				table_instance_insert(new, flow);
361e6445719SPravin B Shelar 	}
362e6445719SPravin B Shelar 
363e6445719SPravin B Shelar 	old->keep_flows = true;
364e6445719SPravin B Shelar }
365e6445719SPravin B Shelar 
366b637e498SPravin B Shelar static struct table_instance *table_instance_rehash(struct table_instance *ti,
367*74ed7ab9SJoe Stringer 						    int n_buckets, bool ufid)
368e6445719SPravin B Shelar {
369b637e498SPravin B Shelar 	struct table_instance *new_ti;
370e6445719SPravin B Shelar 
371b637e498SPravin B Shelar 	new_ti = table_instance_alloc(n_buckets);
372b637e498SPravin B Shelar 	if (!new_ti)
373618ed0c8SPravin B Shelar 		return NULL;
374e6445719SPravin B Shelar 
375*74ed7ab9SJoe Stringer 	flow_table_copy_flows(ti, new_ti, ufid);
376e6445719SPravin B Shelar 
377b637e498SPravin B Shelar 	return new_ti;
378e6445719SPravin B Shelar }
379e6445719SPravin B Shelar 
380b637e498SPravin B Shelar int ovs_flow_tbl_flush(struct flow_table *flow_table)
381e6445719SPravin B Shelar {
382*74ed7ab9SJoe Stringer 	struct table_instance *old_ti, *new_ti;
383*74ed7ab9SJoe Stringer 	struct table_instance *old_ufid_ti, *new_ufid_ti;
384e6445719SPravin B Shelar 
385b637e498SPravin B Shelar 	new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
386b637e498SPravin B Shelar 	if (!new_ti)
387b637e498SPravin B Shelar 		return -ENOMEM;
388*74ed7ab9SJoe Stringer 	new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
389*74ed7ab9SJoe Stringer 	if (!new_ufid_ti)
390*74ed7ab9SJoe Stringer 		goto err_free_ti;
391*74ed7ab9SJoe Stringer 
392*74ed7ab9SJoe Stringer 	old_ti = ovsl_dereference(flow_table->ti);
393*74ed7ab9SJoe Stringer 	old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
394b637e498SPravin B Shelar 
395b637e498SPravin B Shelar 	rcu_assign_pointer(flow_table->ti, new_ti);
396*74ed7ab9SJoe Stringer 	rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
397b637e498SPravin B Shelar 	flow_table->last_rehash = jiffies;
398b637e498SPravin B Shelar 	flow_table->count = 0;
399*74ed7ab9SJoe Stringer 	flow_table->ufid_count = 0;
400b637e498SPravin B Shelar 
401*74ed7ab9SJoe Stringer 	table_instance_destroy(old_ti, old_ufid_ti, true);
402b637e498SPravin B Shelar 	return 0;
403*74ed7ab9SJoe Stringer 
404*74ed7ab9SJoe Stringer err_free_ti:
405*74ed7ab9SJoe Stringer 	__table_instance_destroy(new_ti);
406*74ed7ab9SJoe Stringer 	return -ENOMEM;
407e6445719SPravin B Shelar }
408e6445719SPravin B Shelar 
409272c2cf8SJoe Stringer static u32 flow_hash(const struct sw_flow_key *key,
410272c2cf8SJoe Stringer 		     const struct sw_flow_key_range *range)
411e6445719SPravin B Shelar {
412272c2cf8SJoe Stringer 	int key_start = range->start;
413272c2cf8SJoe Stringer 	int key_end = range->end;
4147085130bSDaniele Di Proietto 	const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
415e6445719SPravin B Shelar 	int hash_u32s = (key_end - key_start) >> 2;
416e6445719SPravin B Shelar 
417e6445719SPravin B Shelar 	/* Make sure number of hash bytes are multiple of u32. */
418e6445719SPravin B Shelar 	BUILD_BUG_ON(sizeof(long) % sizeof(u32));
419e6445719SPravin B Shelar 
42087545899SDaniel Borkmann 	return jhash2(hash_key, hash_u32s, 0);
421e6445719SPravin B Shelar }
422e6445719SPravin B Shelar 
423e6445719SPravin B Shelar static int flow_key_start(const struct sw_flow_key *key)
424e6445719SPravin B Shelar {
425e6445719SPravin B Shelar 	if (key->tun_key.ipv4_dst)
426e6445719SPravin B Shelar 		return 0;
427e6445719SPravin B Shelar 	else
428e6445719SPravin B Shelar 		return rounddown(offsetof(struct sw_flow_key, phy),
429e6445719SPravin B Shelar 					  sizeof(long));
430e6445719SPravin B Shelar }
431e6445719SPravin B Shelar 
432e6445719SPravin B Shelar static bool cmp_key(const struct sw_flow_key *key1,
433e6445719SPravin B Shelar 		    const struct sw_flow_key *key2,
434e6445719SPravin B Shelar 		    int key_start, int key_end)
435e6445719SPravin B Shelar {
4367085130bSDaniele Di Proietto 	const long *cp1 = (const long *)((const u8 *)key1 + key_start);
4377085130bSDaniele Di Proietto 	const long *cp2 = (const long *)((const u8 *)key2 + key_start);
438e6445719SPravin B Shelar 	long diffs = 0;
439e6445719SPravin B Shelar 	int i;
440e6445719SPravin B Shelar 
441e6445719SPravin B Shelar 	for (i = key_start; i < key_end;  i += sizeof(long))
442e6445719SPravin B Shelar 		diffs |= *cp1++ ^ *cp2++;
443e6445719SPravin B Shelar 
444e6445719SPravin B Shelar 	return diffs == 0;
445e6445719SPravin B Shelar }
446e6445719SPravin B Shelar 
447e6445719SPravin B Shelar static bool flow_cmp_masked_key(const struct sw_flow *flow,
448e6445719SPravin B Shelar 				const struct sw_flow_key *key,
449272c2cf8SJoe Stringer 				const struct sw_flow_key_range *range)
450e6445719SPravin B Shelar {
451272c2cf8SJoe Stringer 	return cmp_key(&flow->key, key, range->start, range->end);
452e6445719SPravin B Shelar }
453e6445719SPravin B Shelar 
454*74ed7ab9SJoe Stringer static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
45512eb18f7SThomas Graf 				      const struct sw_flow_match *match)
456e6445719SPravin B Shelar {
457e6445719SPravin B Shelar 	struct sw_flow_key *key = match->key;
458e6445719SPravin B Shelar 	int key_start = flow_key_start(key);
459e6445719SPravin B Shelar 	int key_end = match->range.end;
460e6445719SPravin B Shelar 
461*74ed7ab9SJoe Stringer 	BUG_ON(ovs_identifier_is_ufid(&flow->id));
462*74ed7ab9SJoe Stringer 	return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
463e6445719SPravin B Shelar }
464e6445719SPravin B Shelar 
465b637e498SPravin B Shelar static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
466e6445719SPravin B Shelar 					  const struct sw_flow_key *unmasked,
46712eb18f7SThomas Graf 					  const struct sw_flow_mask *mask)
468e6445719SPravin B Shelar {
469e6445719SPravin B Shelar 	struct sw_flow *flow;
470e6445719SPravin B Shelar 	struct hlist_head *head;
471e6445719SPravin B Shelar 	u32 hash;
472e6445719SPravin B Shelar 	struct sw_flow_key masked_key;
473e6445719SPravin B Shelar 
474e6445719SPravin B Shelar 	ovs_flow_mask_key(&masked_key, unmasked, mask);
475272c2cf8SJoe Stringer 	hash = flow_hash(&masked_key, &mask->range);
476b637e498SPravin B Shelar 	head = find_bucket(ti, hash);
477*74ed7ab9SJoe Stringer 	hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
478*74ed7ab9SJoe Stringer 		if (flow->mask == mask && flow->flow_table.hash == hash &&
479272c2cf8SJoe Stringer 		    flow_cmp_masked_key(flow, &masked_key, &mask->range))
480e6445719SPravin B Shelar 			return flow;
481e6445719SPravin B Shelar 	}
482e6445719SPravin B Shelar 	return NULL;
483e6445719SPravin B Shelar }
484e6445719SPravin B Shelar 
4855bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
4861bd7116fSAndy Zhou 				    const struct sw_flow_key *key,
4871bd7116fSAndy Zhou 				    u32 *n_mask_hit)
488e6445719SPravin B Shelar {
489663efa36SJesse Gross 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
490e6445719SPravin B Shelar 	struct sw_flow_mask *mask;
491b637e498SPravin B Shelar 	struct sw_flow *flow;
492e6445719SPravin B Shelar 
4931bd7116fSAndy Zhou 	*n_mask_hit = 0;
494b637e498SPravin B Shelar 	list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
4951bd7116fSAndy Zhou 		(*n_mask_hit)++;
496b637e498SPravin B Shelar 		flow = masked_flow_lookup(ti, key, mask);
497e6445719SPravin B Shelar 		if (flow)  /* Found */
498b637e498SPravin B Shelar 			return flow;
499b637e498SPravin B Shelar 	}
500b637e498SPravin B Shelar 	return NULL;
501e6445719SPravin B Shelar }
502e6445719SPravin B Shelar 
5035bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
5045bb50632SAndy Zhou 				    const struct sw_flow_key *key)
5055bb50632SAndy Zhou {
5065bb50632SAndy Zhou 	u32 __always_unused n_mask_hit;
5075bb50632SAndy Zhou 
5085bb50632SAndy Zhou 	return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
5095bb50632SAndy Zhou }
5105bb50632SAndy Zhou 
5114a46b24eSAlex Wang struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
51212eb18f7SThomas Graf 					  const struct sw_flow_match *match)
5134a46b24eSAlex Wang {
5144a46b24eSAlex Wang 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
5154a46b24eSAlex Wang 	struct sw_flow_mask *mask;
5164a46b24eSAlex Wang 	struct sw_flow *flow;
5174a46b24eSAlex Wang 
5184a46b24eSAlex Wang 	/* Always called under ovs-mutex. */
5194a46b24eSAlex Wang 	list_for_each_entry(mask, &tbl->mask_list, list) {
5204a46b24eSAlex Wang 		flow = masked_flow_lookup(ti, match->key, mask);
521*74ed7ab9SJoe Stringer 		if (flow && ovs_identifier_is_key(&flow->id) &&
522*74ed7ab9SJoe Stringer 		    ovs_flow_cmp_unmasked_key(flow, match))
523*74ed7ab9SJoe Stringer 			return flow;
524*74ed7ab9SJoe Stringer 	}
525*74ed7ab9SJoe Stringer 	return NULL;
526*74ed7ab9SJoe Stringer }
527*74ed7ab9SJoe Stringer 
528*74ed7ab9SJoe Stringer static u32 ufid_hash(const struct sw_flow_id *sfid)
529*74ed7ab9SJoe Stringer {
530*74ed7ab9SJoe Stringer 	return jhash(sfid->ufid, sfid->ufid_len, 0);
531*74ed7ab9SJoe Stringer }
532*74ed7ab9SJoe Stringer 
533*74ed7ab9SJoe Stringer static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
534*74ed7ab9SJoe Stringer 			      const struct sw_flow_id *sfid)
535*74ed7ab9SJoe Stringer {
536*74ed7ab9SJoe Stringer 	if (flow->id.ufid_len != sfid->ufid_len)
537*74ed7ab9SJoe Stringer 		return false;
538*74ed7ab9SJoe Stringer 
539*74ed7ab9SJoe Stringer 	return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
540*74ed7ab9SJoe Stringer }
541*74ed7ab9SJoe Stringer 
542*74ed7ab9SJoe Stringer bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
543*74ed7ab9SJoe Stringer {
544*74ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id))
545*74ed7ab9SJoe Stringer 		return flow_cmp_masked_key(flow, match->key, &match->range);
546*74ed7ab9SJoe Stringer 
547*74ed7ab9SJoe Stringer 	return ovs_flow_cmp_unmasked_key(flow, match);
548*74ed7ab9SJoe Stringer }
549*74ed7ab9SJoe Stringer 
550*74ed7ab9SJoe Stringer struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
551*74ed7ab9SJoe Stringer 					 const struct sw_flow_id *ufid)
552*74ed7ab9SJoe Stringer {
553*74ed7ab9SJoe Stringer 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
554*74ed7ab9SJoe Stringer 	struct sw_flow *flow;
555*74ed7ab9SJoe Stringer 	struct hlist_head *head;
556*74ed7ab9SJoe Stringer 	u32 hash;
557*74ed7ab9SJoe Stringer 
558*74ed7ab9SJoe Stringer 	hash = ufid_hash(ufid);
559*74ed7ab9SJoe Stringer 	head = find_bucket(ti, hash);
560*74ed7ab9SJoe Stringer 	hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
561*74ed7ab9SJoe Stringer 		if (flow->ufid_table.hash == hash &&
562*74ed7ab9SJoe Stringer 		    ovs_flow_cmp_ufid(flow, ufid))
5634a46b24eSAlex Wang 			return flow;
5644a46b24eSAlex Wang 	}
5654a46b24eSAlex Wang 	return NULL;
5664a46b24eSAlex Wang }
5674a46b24eSAlex Wang 
5681bd7116fSAndy Zhou int ovs_flow_tbl_num_masks(const struct flow_table *table)
5691bd7116fSAndy Zhou {
5701bd7116fSAndy Zhou 	struct sw_flow_mask *mask;
5711bd7116fSAndy Zhou 	int num = 0;
5721bd7116fSAndy Zhou 
5731bd7116fSAndy Zhou 	list_for_each_entry(mask, &table->mask_list, list)
5741bd7116fSAndy Zhou 		num++;
5751bd7116fSAndy Zhou 
5761bd7116fSAndy Zhou 	return num;
5771bd7116fSAndy Zhou }
5781bd7116fSAndy Zhou 
579*74ed7ab9SJoe Stringer static struct table_instance *table_instance_expand(struct table_instance *ti,
580*74ed7ab9SJoe Stringer 						    bool ufid)
581b637e498SPravin B Shelar {
582*74ed7ab9SJoe Stringer 	return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
583e6445719SPravin B Shelar }
584e6445719SPravin B Shelar 
58556c19868SJarno Rajahalme /* Remove 'mask' from the mask list, if it is not needed any more. */
58656c19868SJarno Rajahalme static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
58756c19868SJarno Rajahalme {
58856c19868SJarno Rajahalme 	if (mask) {
58956c19868SJarno Rajahalme 		/* ovs-lock is required to protect mask-refcount and
59056c19868SJarno Rajahalme 		 * mask list.
59156c19868SJarno Rajahalme 		 */
59256c19868SJarno Rajahalme 		ASSERT_OVSL();
59356c19868SJarno Rajahalme 		BUG_ON(!mask->ref_count);
59456c19868SJarno Rajahalme 		mask->ref_count--;
59556c19868SJarno Rajahalme 
59656c19868SJarno Rajahalme 		if (!mask->ref_count) {
59756c19868SJarno Rajahalme 			list_del_rcu(&mask->list);
59856c19868SJarno Rajahalme 			kfree_rcu(mask, rcu);
59956c19868SJarno Rajahalme 		}
60056c19868SJarno Rajahalme 	}
60156c19868SJarno Rajahalme }
60256c19868SJarno Rajahalme 
60356c19868SJarno Rajahalme /* Must be called with OVS mutex held. */
604e6445719SPravin B Shelar void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
605e6445719SPravin B Shelar {
606b637e498SPravin B Shelar 	struct table_instance *ti = ovsl_dereference(table->ti);
607*74ed7ab9SJoe Stringer 	struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
608b637e498SPravin B Shelar 
609e6445719SPravin B Shelar 	BUG_ON(table->count == 0);
610*74ed7ab9SJoe Stringer 	hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
611e6445719SPravin B Shelar 	table->count--;
612*74ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id)) {
613*74ed7ab9SJoe Stringer 		hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
614*74ed7ab9SJoe Stringer 		table->ufid_count--;
615*74ed7ab9SJoe Stringer 	}
61656c19868SJarno Rajahalme 
61756c19868SJarno Rajahalme 	/* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
61856c19868SJarno Rajahalme 	 * accessible as long as the RCU read lock is held.
61956c19868SJarno Rajahalme 	 */
62056c19868SJarno Rajahalme 	flow_mask_remove(table, flow->mask);
621e6445719SPravin B Shelar }
622e6445719SPravin B Shelar 
623618ed0c8SPravin B Shelar static struct sw_flow_mask *mask_alloc(void)
624e6445719SPravin B Shelar {
625e6445719SPravin B Shelar 	struct sw_flow_mask *mask;
626e6445719SPravin B Shelar 
627e6445719SPravin B Shelar 	mask = kmalloc(sizeof(*mask), GFP_KERNEL);
628e6445719SPravin B Shelar 	if (mask)
629e80857ccSAndy Zhou 		mask->ref_count = 1;
630e6445719SPravin B Shelar 
631e6445719SPravin B Shelar 	return mask;
632e6445719SPravin B Shelar }
633e6445719SPravin B Shelar 
634e6445719SPravin B Shelar static bool mask_equal(const struct sw_flow_mask *a,
635e6445719SPravin B Shelar 		       const struct sw_flow_mask *b)
636e6445719SPravin B Shelar {
6377085130bSDaniele Di Proietto 	const u8 *a_ = (const u8 *)&a->key + a->range.start;
6387085130bSDaniele Di Proietto 	const u8 *b_ = (const u8 *)&b->key + b->range.start;
639e6445719SPravin B Shelar 
640e6445719SPravin B Shelar 	return  (a->range.end == b->range.end)
641e6445719SPravin B Shelar 		&& (a->range.start == b->range.start)
642e6445719SPravin B Shelar 		&& (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
643e6445719SPravin B Shelar }
644e6445719SPravin B Shelar 
645618ed0c8SPravin B Shelar static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
646e6445719SPravin B Shelar 					   const struct sw_flow_mask *mask)
647e6445719SPravin B Shelar {
648e6445719SPravin B Shelar 	struct list_head *ml;
649e6445719SPravin B Shelar 
650b637e498SPravin B Shelar 	list_for_each(ml, &tbl->mask_list) {
651e6445719SPravin B Shelar 		struct sw_flow_mask *m;
652e6445719SPravin B Shelar 		m = container_of(ml, struct sw_flow_mask, list);
653e6445719SPravin B Shelar 		if (mask_equal(mask, m))
654e6445719SPravin B Shelar 			return m;
655e6445719SPravin B Shelar 	}
656e6445719SPravin B Shelar 
657e6445719SPravin B Shelar 	return NULL;
658e6445719SPravin B Shelar }
659e6445719SPravin B Shelar 
660d1211908SBen Pfaff /* Add 'mask' into the mask list, if it is not already there. */
661618ed0c8SPravin B Shelar static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
66212eb18f7SThomas Graf 			    const struct sw_flow_mask *new)
663e6445719SPravin B Shelar {
664618ed0c8SPravin B Shelar 	struct sw_flow_mask *mask;
665618ed0c8SPravin B Shelar 	mask = flow_mask_find(tbl, new);
666618ed0c8SPravin B Shelar 	if (!mask) {
667618ed0c8SPravin B Shelar 		/* Allocate a new mask if none exsits. */
668618ed0c8SPravin B Shelar 		mask = mask_alloc();
669618ed0c8SPravin B Shelar 		if (!mask)
670618ed0c8SPravin B Shelar 			return -ENOMEM;
671618ed0c8SPravin B Shelar 		mask->key = new->key;
672618ed0c8SPravin B Shelar 		mask->range = new->range;
673b637e498SPravin B Shelar 		list_add_rcu(&mask->list, &tbl->mask_list);
674e80857ccSAndy Zhou 	} else {
675e80857ccSAndy Zhou 		BUG_ON(!mask->ref_count);
676e80857ccSAndy Zhou 		mask->ref_count++;
677e6445719SPravin B Shelar 	}
678e6445719SPravin B Shelar 
679618ed0c8SPravin B Shelar 	flow->mask = mask;
680618ed0c8SPravin B Shelar 	return 0;
681618ed0c8SPravin B Shelar }
682618ed0c8SPravin B Shelar 
68356c19868SJarno Rajahalme /* Must be called with OVS mutex held. */
684d29ab6f8SJoe Stringer static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
685618ed0c8SPravin B Shelar {
686618ed0c8SPravin B Shelar 	struct table_instance *new_ti = NULL;
687618ed0c8SPravin B Shelar 	struct table_instance *ti;
688618ed0c8SPravin B Shelar 
689*74ed7ab9SJoe Stringer 	flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
690618ed0c8SPravin B Shelar 	ti = ovsl_dereference(table->ti);
691618ed0c8SPravin B Shelar 	table_instance_insert(ti, flow);
692618ed0c8SPravin B Shelar 	table->count++;
693618ed0c8SPravin B Shelar 
694618ed0c8SPravin B Shelar 	/* Expand table, if necessary, to make room. */
695618ed0c8SPravin B Shelar 	if (table->count > ti->n_buckets)
696*74ed7ab9SJoe Stringer 		new_ti = table_instance_expand(ti, false);
697618ed0c8SPravin B Shelar 	else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
698*74ed7ab9SJoe Stringer 		new_ti = table_instance_rehash(ti, ti->n_buckets, false);
699618ed0c8SPravin B Shelar 
700618ed0c8SPravin B Shelar 	if (new_ti) {
701618ed0c8SPravin B Shelar 		rcu_assign_pointer(table->ti, new_ti);
702*74ed7ab9SJoe Stringer 		call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
703618ed0c8SPravin B Shelar 		table->last_rehash = jiffies;
704618ed0c8SPravin B Shelar 	}
705d29ab6f8SJoe Stringer }
706d29ab6f8SJoe Stringer 
707d29ab6f8SJoe Stringer /* Must be called with OVS mutex held. */
708*74ed7ab9SJoe Stringer static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
709*74ed7ab9SJoe Stringer {
710*74ed7ab9SJoe Stringer 	struct table_instance *ti;
711*74ed7ab9SJoe Stringer 
712*74ed7ab9SJoe Stringer 	flow->ufid_table.hash = ufid_hash(&flow->id);
713*74ed7ab9SJoe Stringer 	ti = ovsl_dereference(table->ufid_ti);
714*74ed7ab9SJoe Stringer 	ufid_table_instance_insert(ti, flow);
715*74ed7ab9SJoe Stringer 	table->ufid_count++;
716*74ed7ab9SJoe Stringer 
717*74ed7ab9SJoe Stringer 	/* Expand table, if necessary, to make room. */
718*74ed7ab9SJoe Stringer 	if (table->ufid_count > ti->n_buckets) {
719*74ed7ab9SJoe Stringer 		struct table_instance *new_ti;
720*74ed7ab9SJoe Stringer 
721*74ed7ab9SJoe Stringer 		new_ti = table_instance_expand(ti, true);
722*74ed7ab9SJoe Stringer 		if (new_ti) {
723*74ed7ab9SJoe Stringer 			rcu_assign_pointer(table->ufid_ti, new_ti);
724*74ed7ab9SJoe Stringer 			call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
725*74ed7ab9SJoe Stringer 		}
726*74ed7ab9SJoe Stringer 	}
727*74ed7ab9SJoe Stringer }
728*74ed7ab9SJoe Stringer 
729*74ed7ab9SJoe Stringer /* Must be called with OVS mutex held. */
730d29ab6f8SJoe Stringer int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
731d29ab6f8SJoe Stringer 			const struct sw_flow_mask *mask)
732d29ab6f8SJoe Stringer {
733d29ab6f8SJoe Stringer 	int err;
734d29ab6f8SJoe Stringer 
735d29ab6f8SJoe Stringer 	err = flow_mask_insert(table, flow, mask);
736d29ab6f8SJoe Stringer 	if (err)
737d29ab6f8SJoe Stringer 		return err;
738d29ab6f8SJoe Stringer 	flow_key_insert(table, flow);
739*74ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id))
740*74ed7ab9SJoe Stringer 		flow_ufid_insert(table, flow);
741d29ab6f8SJoe Stringer 
742618ed0c8SPravin B Shelar 	return 0;
743618ed0c8SPravin B Shelar }
744618ed0c8SPravin B Shelar 
745e6445719SPravin B Shelar /* Initializes the flow module.
746e6445719SPravin B Shelar  * Returns zero if successful or a negative error code. */
747e6445719SPravin B Shelar int ovs_flow_init(void)
748e6445719SPravin B Shelar {
749e6445719SPravin B Shelar 	BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
750e6445719SPravin B Shelar 	BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
751e6445719SPravin B Shelar 
75263e7959cSJarno Rajahalme 	flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
75363e7959cSJarno Rajahalme 				       + (num_possible_nodes()
75463e7959cSJarno Rajahalme 					  * sizeof(struct flow_stats *)),
75563e7959cSJarno Rajahalme 				       0, 0, NULL);
756e6445719SPravin B Shelar 	if (flow_cache == NULL)
757e6445719SPravin B Shelar 		return -ENOMEM;
758e6445719SPravin B Shelar 
75963e7959cSJarno Rajahalme 	flow_stats_cache
76063e7959cSJarno Rajahalme 		= kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
76163e7959cSJarno Rajahalme 				    0, SLAB_HWCACHE_ALIGN, NULL);
76263e7959cSJarno Rajahalme 	if (flow_stats_cache == NULL) {
76363e7959cSJarno Rajahalme 		kmem_cache_destroy(flow_cache);
76463e7959cSJarno Rajahalme 		flow_cache = NULL;
76563e7959cSJarno Rajahalme 		return -ENOMEM;
76663e7959cSJarno Rajahalme 	}
76763e7959cSJarno Rajahalme 
768e6445719SPravin B Shelar 	return 0;
769e6445719SPravin B Shelar }
770e6445719SPravin B Shelar 
771e6445719SPravin B Shelar /* Uninitializes the flow module. */
772e6445719SPravin B Shelar void ovs_flow_exit(void)
773e6445719SPravin B Shelar {
77463e7959cSJarno Rajahalme 	kmem_cache_destroy(flow_stats_cache);
775e6445719SPravin B Shelar 	kmem_cache_destroy(flow_cache);
776e6445719SPravin B Shelar }
777