xref: /openbmc/linux/net/openvswitch/flow_table.c (revision 34ae932a40369be6bd6ea97d66b6686361b4370d)
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"
21*34ae932aSThomas Graf #include "flow_netlink.h"
22e6445719SPravin B Shelar #include <linux/uaccess.h>
23e6445719SPravin B Shelar #include <linux/netdevice.h>
24e6445719SPravin B Shelar #include <linux/etherdevice.h>
25e6445719SPravin B Shelar #include <linux/if_ether.h>
26e6445719SPravin B Shelar #include <linux/if_vlan.h>
27e6445719SPravin B Shelar #include <net/llc_pdu.h>
28e6445719SPravin B Shelar #include <linux/kernel.h>
2987545899SDaniel Borkmann #include <linux/jhash.h>
30e6445719SPravin B Shelar #include <linux/jiffies.h>
31e6445719SPravin B Shelar #include <linux/llc.h>
32e6445719SPravin B Shelar #include <linux/module.h>
33e6445719SPravin B Shelar #include <linux/in.h>
34e6445719SPravin B Shelar #include <linux/rcupdate.h>
35e6445719SPravin B Shelar #include <linux/if_arp.h>
36e6445719SPravin B Shelar #include <linux/ip.h>
37e6445719SPravin B Shelar #include <linux/ipv6.h>
38e6445719SPravin B Shelar #include <linux/sctp.h>
39e6445719SPravin B Shelar #include <linux/tcp.h>
40e6445719SPravin B Shelar #include <linux/udp.h>
41e6445719SPravin B Shelar #include <linux/icmp.h>
42e6445719SPravin B Shelar #include <linux/icmpv6.h>
43e6445719SPravin B Shelar #include <linux/rculist.h>
44e6445719SPravin B Shelar #include <net/ip.h>
45e6445719SPravin B Shelar #include <net/ipv6.h>
46e6445719SPravin B Shelar #include <net/ndisc.h>
47e6445719SPravin B Shelar 
48b637e498SPravin B Shelar #define TBL_MIN_BUCKETS		1024
49b637e498SPravin B Shelar #define REHASH_INTERVAL		(10 * 60 * HZ)
50b637e498SPravin B Shelar 
51e6445719SPravin B Shelar static struct kmem_cache *flow_cache;
5263e7959cSJarno Rajahalme struct kmem_cache *flow_stats_cache __read_mostly;
53e6445719SPravin B Shelar 
54e6445719SPravin B Shelar static u16 range_n_bytes(const struct sw_flow_key_range *range)
55e6445719SPravin B Shelar {
56e6445719SPravin B Shelar 	return range->end - range->start;
57e6445719SPravin B Shelar }
58e6445719SPravin B Shelar 
59e6445719SPravin B Shelar void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
60e6445719SPravin B Shelar 		       const struct sw_flow_mask *mask)
61e6445719SPravin B Shelar {
627085130bSDaniele Di Proietto 	const long *m = (const long *)((const u8 *)&mask->key +
637085130bSDaniele Di Proietto 				mask->range.start);
647085130bSDaniele Di Proietto 	const long *s = (const long *)((const u8 *)src +
657085130bSDaniele Di Proietto 				mask->range.start);
66e6445719SPravin B Shelar 	long *d = (long *)((u8 *)dst + mask->range.start);
67e6445719SPravin B Shelar 	int i;
68e6445719SPravin B Shelar 
69e6445719SPravin B Shelar 	/* The memory outside of the 'mask->range' are not set since
70e6445719SPravin B Shelar 	 * further operations on 'dst' only uses contents within
71e6445719SPravin B Shelar 	 * 'mask->range'.
72e6445719SPravin B Shelar 	 */
73e6445719SPravin B Shelar 	for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
74e6445719SPravin B Shelar 		*d++ = *s++ & *m++;
75e6445719SPravin B Shelar }
76e6445719SPravin B Shelar 
7723dabf88SJarno Rajahalme struct sw_flow *ovs_flow_alloc(void)
78e6445719SPravin B Shelar {
79e6445719SPravin B Shelar 	struct sw_flow *flow;
8063e7959cSJarno Rajahalme 	struct flow_stats *stats;
8163e7959cSJarno Rajahalme 	int node;
82e6445719SPravin B Shelar 
83e6445719SPravin B Shelar 	flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
84e6445719SPravin B Shelar 	if (!flow)
85e6445719SPravin B Shelar 		return ERR_PTR(-ENOMEM);
86e6445719SPravin B Shelar 
87e6445719SPravin B Shelar 	flow->sf_acts = NULL;
88e6445719SPravin B Shelar 	flow->mask = NULL;
89ca539345SPravin B Shelar 	flow->id.unmasked_key = NULL;
90ca539345SPravin B Shelar 	flow->id.ufid_len = 0;
9163e7959cSJarno Rajahalme 	flow->stats_last_writer = NUMA_NO_NODE;
92e6445719SPravin B Shelar 
9363e7959cSJarno Rajahalme 	/* Initialize the default stat node. */
9463e7959cSJarno Rajahalme 	stats = kmem_cache_alloc_node(flow_stats_cache,
9563e7959cSJarno Rajahalme 				      GFP_KERNEL | __GFP_ZERO, 0);
9663e7959cSJarno Rajahalme 	if (!stats)
97e298e505SPravin B Shelar 		goto err;
98e298e505SPravin B Shelar 
9963e7959cSJarno Rajahalme 	spin_lock_init(&stats->lock);
100e298e505SPravin B Shelar 
10163e7959cSJarno Rajahalme 	RCU_INIT_POINTER(flow->stats[0], stats);
10263e7959cSJarno Rajahalme 
10363e7959cSJarno Rajahalme 	for_each_node(node)
10463e7959cSJarno Rajahalme 		if (node != 0)
10563e7959cSJarno Rajahalme 			RCU_INIT_POINTER(flow->stats[node], NULL);
10663e7959cSJarno Rajahalme 
107e6445719SPravin B Shelar 	return flow;
108e298e505SPravin B Shelar err:
109ece37c87SWei Yongjun 	kmem_cache_free(flow_cache, flow);
110e298e505SPravin B Shelar 	return ERR_PTR(-ENOMEM);
111e6445719SPravin B Shelar }
112e6445719SPravin B Shelar 
11312eb18f7SThomas Graf int ovs_flow_tbl_count(const struct flow_table *table)
114b637e498SPravin B Shelar {
115b637e498SPravin B Shelar 	return table->count;
116b637e498SPravin B Shelar }
117b637e498SPravin B Shelar 
118e6445719SPravin B Shelar static struct flex_array *alloc_buckets(unsigned int n_buckets)
119e6445719SPravin B Shelar {
120e6445719SPravin B Shelar 	struct flex_array *buckets;
121e6445719SPravin B Shelar 	int i, err;
122e6445719SPravin B Shelar 
123e6445719SPravin B Shelar 	buckets = flex_array_alloc(sizeof(struct hlist_head),
124e6445719SPravin B Shelar 				   n_buckets, GFP_KERNEL);
125e6445719SPravin B Shelar 	if (!buckets)
126e6445719SPravin B Shelar 		return NULL;
127e6445719SPravin B Shelar 
128e6445719SPravin B Shelar 	err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
129e6445719SPravin B Shelar 	if (err) {
130e6445719SPravin B Shelar 		flex_array_free(buckets);
131e6445719SPravin B Shelar 		return NULL;
132e6445719SPravin B Shelar 	}
133e6445719SPravin B Shelar 
134e6445719SPravin B Shelar 	for (i = 0; i < n_buckets; i++)
135e6445719SPravin B Shelar 		INIT_HLIST_HEAD((struct hlist_head *)
136e6445719SPravin B Shelar 					flex_array_get(buckets, i));
137e6445719SPravin B Shelar 
138e6445719SPravin B Shelar 	return buckets;
139e6445719SPravin B Shelar }
140e6445719SPravin B Shelar 
141e6445719SPravin B Shelar static void flow_free(struct sw_flow *flow)
142e6445719SPravin B Shelar {
14363e7959cSJarno Rajahalme 	int node;
14463e7959cSJarno Rajahalme 
14574ed7ab9SJoe Stringer 	if (ovs_identifier_is_key(&flow->id))
14674ed7ab9SJoe Stringer 		kfree(flow->id.unmasked_key);
147*34ae932aSThomas Graf 	if (flow->sf_acts)
148*34ae932aSThomas Graf 		ovs_nla_free_flow_actions((struct sw_flow_actions __force *)flow->sf_acts);
14963e7959cSJarno Rajahalme 	for_each_node(node)
15063e7959cSJarno Rajahalme 		if (flow->stats[node])
15163e7959cSJarno Rajahalme 			kmem_cache_free(flow_stats_cache,
15263e7959cSJarno Rajahalme 					(struct flow_stats __force *)flow->stats[node]);
153e6445719SPravin B Shelar 	kmem_cache_free(flow_cache, flow);
154e6445719SPravin B Shelar }
155e6445719SPravin B Shelar 
156e6445719SPravin B Shelar static void rcu_free_flow_callback(struct rcu_head *rcu)
157e6445719SPravin B Shelar {
158e6445719SPravin B Shelar 	struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
159e6445719SPravin B Shelar 
160e6445719SPravin B Shelar 	flow_free(flow);
161e6445719SPravin B Shelar }
162e6445719SPravin B Shelar 
163e80857ccSAndy Zhou void ovs_flow_free(struct sw_flow *flow, bool deferred)
164618ed0c8SPravin B Shelar {
165e80857ccSAndy Zhou 	if (!flow)
166618ed0c8SPravin B Shelar 		return;
167618ed0c8SPravin B Shelar 
168e6445719SPravin B Shelar 	if (deferred)
169e6445719SPravin B Shelar 		call_rcu(&flow->rcu, rcu_free_flow_callback);
170e6445719SPravin B Shelar 	else
171e6445719SPravin B Shelar 		flow_free(flow);
172e6445719SPravin B Shelar }
173e6445719SPravin B Shelar 
174e6445719SPravin B Shelar static void free_buckets(struct flex_array *buckets)
175e6445719SPravin B Shelar {
176e6445719SPravin B Shelar 	flex_array_free(buckets);
177e6445719SPravin B Shelar }
178e6445719SPravin B Shelar 
179e80857ccSAndy Zhou 
180b637e498SPravin B Shelar static void __table_instance_destroy(struct table_instance *ti)
181e6445719SPravin B Shelar {
182b637e498SPravin B Shelar 	free_buckets(ti->buckets);
183b637e498SPravin B Shelar 	kfree(ti);
184e6445719SPravin B Shelar }
185e6445719SPravin B Shelar 
186b637e498SPravin B Shelar static struct table_instance *table_instance_alloc(int new_size)
187e6445719SPravin B Shelar {
188b637e498SPravin B Shelar 	struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
189e6445719SPravin B Shelar 
190b637e498SPravin B Shelar 	if (!ti)
191e6445719SPravin B Shelar 		return NULL;
192e6445719SPravin B Shelar 
193b637e498SPravin B Shelar 	ti->buckets = alloc_buckets(new_size);
194e6445719SPravin B Shelar 
195b637e498SPravin B Shelar 	if (!ti->buckets) {
196b637e498SPravin B Shelar 		kfree(ti);
197e6445719SPravin B Shelar 		return NULL;
198e6445719SPravin B Shelar 	}
199b637e498SPravin B Shelar 	ti->n_buckets = new_size;
200b637e498SPravin B Shelar 	ti->node_ver = 0;
201b637e498SPravin B Shelar 	ti->keep_flows = false;
202b637e498SPravin B Shelar 	get_random_bytes(&ti->hash_seed, sizeof(u32));
203b637e498SPravin B Shelar 
204b637e498SPravin B Shelar 	return ti;
205b637e498SPravin B Shelar }
206b637e498SPravin B Shelar 
207b637e498SPravin B Shelar int ovs_flow_tbl_init(struct flow_table *table)
208b637e498SPravin B Shelar {
20974ed7ab9SJoe Stringer 	struct table_instance *ti, *ufid_ti;
210b637e498SPravin B Shelar 
211b637e498SPravin B Shelar 	ti = table_instance_alloc(TBL_MIN_BUCKETS);
212b637e498SPravin B Shelar 
213b637e498SPravin B Shelar 	if (!ti)
214b637e498SPravin B Shelar 		return -ENOMEM;
215b637e498SPravin B Shelar 
21674ed7ab9SJoe Stringer 	ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
21774ed7ab9SJoe Stringer 	if (!ufid_ti)
21874ed7ab9SJoe Stringer 		goto free_ti;
21974ed7ab9SJoe Stringer 
220b637e498SPravin B Shelar 	rcu_assign_pointer(table->ti, ti);
22174ed7ab9SJoe Stringer 	rcu_assign_pointer(table->ufid_ti, ufid_ti);
222b637e498SPravin B Shelar 	INIT_LIST_HEAD(&table->mask_list);
223b637e498SPravin B Shelar 	table->last_rehash = jiffies;
224e6445719SPravin B Shelar 	table->count = 0;
22574ed7ab9SJoe Stringer 	table->ufid_count = 0;
226b637e498SPravin B Shelar 	return 0;
22774ed7ab9SJoe Stringer 
22874ed7ab9SJoe Stringer free_ti:
22974ed7ab9SJoe Stringer 	__table_instance_destroy(ti);
23074ed7ab9SJoe Stringer 	return -ENOMEM;
231e6445719SPravin B Shelar }
232e6445719SPravin B Shelar 
233e6445719SPravin B Shelar static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
234e6445719SPravin B Shelar {
235b637e498SPravin B Shelar 	struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
236e6445719SPravin B Shelar 
237b637e498SPravin B Shelar 	__table_instance_destroy(ti);
238b637e498SPravin B Shelar }
239b637e498SPravin B Shelar 
24074ed7ab9SJoe Stringer static void table_instance_destroy(struct table_instance *ti,
24174ed7ab9SJoe Stringer 				   struct table_instance *ufid_ti,
24274ed7ab9SJoe Stringer 				   bool deferred)
243b637e498SPravin B Shelar {
244e80857ccSAndy Zhou 	int i;
245e80857ccSAndy Zhou 
246b637e498SPravin B Shelar 	if (!ti)
247b637e498SPravin B Shelar 		return;
248b637e498SPravin B Shelar 
24974ed7ab9SJoe Stringer 	BUG_ON(!ufid_ti);
250e80857ccSAndy Zhou 	if (ti->keep_flows)
251e80857ccSAndy Zhou 		goto skip_flows;
252e80857ccSAndy Zhou 
253e80857ccSAndy Zhou 	for (i = 0; i < ti->n_buckets; i++) {
254e80857ccSAndy Zhou 		struct sw_flow *flow;
255e80857ccSAndy Zhou 		struct hlist_head *head = flex_array_get(ti->buckets, i);
256e80857ccSAndy Zhou 		struct hlist_node *n;
257e80857ccSAndy Zhou 		int ver = ti->node_ver;
25874ed7ab9SJoe Stringer 		int ufid_ver = ufid_ti->node_ver;
259e80857ccSAndy Zhou 
26074ed7ab9SJoe Stringer 		hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
26174ed7ab9SJoe Stringer 			hlist_del_rcu(&flow->flow_table.node[ver]);
26274ed7ab9SJoe Stringer 			if (ovs_identifier_is_ufid(&flow->id))
26374ed7ab9SJoe Stringer 				hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
264e80857ccSAndy Zhou 			ovs_flow_free(flow, deferred);
265e80857ccSAndy Zhou 		}
266e80857ccSAndy Zhou 	}
267e80857ccSAndy Zhou 
268e80857ccSAndy Zhou skip_flows:
26974ed7ab9SJoe Stringer 	if (deferred) {
270b637e498SPravin B Shelar 		call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
27174ed7ab9SJoe Stringer 		call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
27274ed7ab9SJoe Stringer 	} else {
273b637e498SPravin B Shelar 		__table_instance_destroy(ti);
27474ed7ab9SJoe Stringer 		__table_instance_destroy(ufid_ti);
27574ed7ab9SJoe Stringer 	}
276e6445719SPravin B Shelar }
277e6445719SPravin B Shelar 
2789b996e54SPravin B Shelar /* No need for locking this function is called from RCU callback or
2799b996e54SPravin B Shelar  * error path.
2809b996e54SPravin B Shelar  */
2819b996e54SPravin B Shelar void ovs_flow_tbl_destroy(struct flow_table *table)
282e6445719SPravin B Shelar {
2839b996e54SPravin B Shelar 	struct table_instance *ti = rcu_dereference_raw(table->ti);
28474ed7ab9SJoe Stringer 	struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
285e6445719SPravin B Shelar 
28674ed7ab9SJoe Stringer 	table_instance_destroy(ti, ufid_ti, false);
287e6445719SPravin B Shelar }
288e6445719SPravin B Shelar 
289b637e498SPravin B Shelar struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
290e6445719SPravin B Shelar 				       u32 *bucket, u32 *last)
291e6445719SPravin B Shelar {
292e6445719SPravin B Shelar 	struct sw_flow *flow;
293e6445719SPravin B Shelar 	struct hlist_head *head;
294e6445719SPravin B Shelar 	int ver;
295e6445719SPravin B Shelar 	int i;
296e6445719SPravin B Shelar 
297b637e498SPravin B Shelar 	ver = ti->node_ver;
298b637e498SPravin B Shelar 	while (*bucket < ti->n_buckets) {
299e6445719SPravin B Shelar 		i = 0;
300b637e498SPravin B Shelar 		head = flex_array_get(ti->buckets, *bucket);
30174ed7ab9SJoe Stringer 		hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
302e6445719SPravin B Shelar 			if (i < *last) {
303e6445719SPravin B Shelar 				i++;
304e6445719SPravin B Shelar 				continue;
305e6445719SPravin B Shelar 			}
306e6445719SPravin B Shelar 			*last = i + 1;
307e6445719SPravin B Shelar 			return flow;
308e6445719SPravin B Shelar 		}
309e6445719SPravin B Shelar 		(*bucket)++;
310e6445719SPravin B Shelar 		*last = 0;
311e6445719SPravin B Shelar 	}
312e6445719SPravin B Shelar 
313e6445719SPravin B Shelar 	return NULL;
314e6445719SPravin B Shelar }
315e6445719SPravin B Shelar 
316b637e498SPravin B Shelar static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
317e6445719SPravin B Shelar {
318b637e498SPravin B Shelar 	hash = jhash_1word(hash, ti->hash_seed);
319b637e498SPravin B Shelar 	return flex_array_get(ti->buckets,
320b637e498SPravin B Shelar 				(hash & (ti->n_buckets - 1)));
321e6445719SPravin B Shelar }
322e6445719SPravin B Shelar 
32374ed7ab9SJoe Stringer static void table_instance_insert(struct table_instance *ti,
32474ed7ab9SJoe Stringer 				  struct sw_flow *flow)
325e6445719SPravin B Shelar {
326e6445719SPravin B Shelar 	struct hlist_head *head;
327e6445719SPravin B Shelar 
32874ed7ab9SJoe Stringer 	head = find_bucket(ti, flow->flow_table.hash);
32974ed7ab9SJoe Stringer 	hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
33074ed7ab9SJoe Stringer }
33174ed7ab9SJoe Stringer 
33274ed7ab9SJoe Stringer static void ufid_table_instance_insert(struct table_instance *ti,
33374ed7ab9SJoe Stringer 				       struct sw_flow *flow)
33474ed7ab9SJoe Stringer {
33574ed7ab9SJoe Stringer 	struct hlist_head *head;
33674ed7ab9SJoe Stringer 
33774ed7ab9SJoe Stringer 	head = find_bucket(ti, flow->ufid_table.hash);
33874ed7ab9SJoe Stringer 	hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
339e6445719SPravin B Shelar }
340e6445719SPravin B Shelar 
341b637e498SPravin B Shelar static void flow_table_copy_flows(struct table_instance *old,
34274ed7ab9SJoe Stringer 				  struct table_instance *new, bool ufid)
343e6445719SPravin B Shelar {
344e6445719SPravin B Shelar 	int old_ver;
345e6445719SPravin B Shelar 	int i;
346e6445719SPravin B Shelar 
347e6445719SPravin B Shelar 	old_ver = old->node_ver;
348e6445719SPravin B Shelar 	new->node_ver = !old_ver;
349e6445719SPravin B Shelar 
350e6445719SPravin B Shelar 	/* Insert in new table. */
351e6445719SPravin B Shelar 	for (i = 0; i < old->n_buckets; i++) {
352e6445719SPravin B Shelar 		struct sw_flow *flow;
353e6445719SPravin B Shelar 		struct hlist_head *head;
354e6445719SPravin B Shelar 
355e6445719SPravin B Shelar 		head = flex_array_get(old->buckets, i);
356e6445719SPravin B Shelar 
35774ed7ab9SJoe Stringer 		if (ufid)
35874ed7ab9SJoe Stringer 			hlist_for_each_entry(flow, head,
35974ed7ab9SJoe Stringer 					     ufid_table.node[old_ver])
36074ed7ab9SJoe Stringer 				ufid_table_instance_insert(new, flow);
36174ed7ab9SJoe Stringer 		else
36274ed7ab9SJoe Stringer 			hlist_for_each_entry(flow, head,
36374ed7ab9SJoe Stringer 					     flow_table.node[old_ver])
364b637e498SPravin B Shelar 				table_instance_insert(new, flow);
365e6445719SPravin B Shelar 	}
366e6445719SPravin B Shelar 
367e6445719SPravin B Shelar 	old->keep_flows = true;
368e6445719SPravin B Shelar }
369e6445719SPravin B Shelar 
370b637e498SPravin B Shelar static struct table_instance *table_instance_rehash(struct table_instance *ti,
37174ed7ab9SJoe Stringer 						    int n_buckets, bool ufid)
372e6445719SPravin B Shelar {
373b637e498SPravin B Shelar 	struct table_instance *new_ti;
374e6445719SPravin B Shelar 
375b637e498SPravin B Shelar 	new_ti = table_instance_alloc(n_buckets);
376b637e498SPravin B Shelar 	if (!new_ti)
377618ed0c8SPravin B Shelar 		return NULL;
378e6445719SPravin B Shelar 
37974ed7ab9SJoe Stringer 	flow_table_copy_flows(ti, new_ti, ufid);
380e6445719SPravin B Shelar 
381b637e498SPravin B Shelar 	return new_ti;
382e6445719SPravin B Shelar }
383e6445719SPravin B Shelar 
384b637e498SPravin B Shelar int ovs_flow_tbl_flush(struct flow_table *flow_table)
385e6445719SPravin B Shelar {
38674ed7ab9SJoe Stringer 	struct table_instance *old_ti, *new_ti;
38774ed7ab9SJoe Stringer 	struct table_instance *old_ufid_ti, *new_ufid_ti;
388e6445719SPravin B Shelar 
389b637e498SPravin B Shelar 	new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
390b637e498SPravin B Shelar 	if (!new_ti)
391b637e498SPravin B Shelar 		return -ENOMEM;
39274ed7ab9SJoe Stringer 	new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
39374ed7ab9SJoe Stringer 	if (!new_ufid_ti)
39474ed7ab9SJoe Stringer 		goto err_free_ti;
39574ed7ab9SJoe Stringer 
39674ed7ab9SJoe Stringer 	old_ti = ovsl_dereference(flow_table->ti);
39774ed7ab9SJoe Stringer 	old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
398b637e498SPravin B Shelar 
399b637e498SPravin B Shelar 	rcu_assign_pointer(flow_table->ti, new_ti);
40074ed7ab9SJoe Stringer 	rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
401b637e498SPravin B Shelar 	flow_table->last_rehash = jiffies;
402b637e498SPravin B Shelar 	flow_table->count = 0;
40374ed7ab9SJoe Stringer 	flow_table->ufid_count = 0;
404b637e498SPravin B Shelar 
40574ed7ab9SJoe Stringer 	table_instance_destroy(old_ti, old_ufid_ti, true);
406b637e498SPravin B Shelar 	return 0;
40774ed7ab9SJoe Stringer 
40874ed7ab9SJoe Stringer err_free_ti:
40974ed7ab9SJoe Stringer 	__table_instance_destroy(new_ti);
41074ed7ab9SJoe Stringer 	return -ENOMEM;
411e6445719SPravin B Shelar }
412e6445719SPravin B Shelar 
413272c2cf8SJoe Stringer static u32 flow_hash(const struct sw_flow_key *key,
414272c2cf8SJoe Stringer 		     const struct sw_flow_key_range *range)
415e6445719SPravin B Shelar {
416272c2cf8SJoe Stringer 	int key_start = range->start;
417272c2cf8SJoe Stringer 	int key_end = range->end;
4187085130bSDaniele Di Proietto 	const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
419e6445719SPravin B Shelar 	int hash_u32s = (key_end - key_start) >> 2;
420e6445719SPravin B Shelar 
421e6445719SPravin B Shelar 	/* Make sure number of hash bytes are multiple of u32. */
422e6445719SPravin B Shelar 	BUILD_BUG_ON(sizeof(long) % sizeof(u32));
423e6445719SPravin B Shelar 
42487545899SDaniel Borkmann 	return jhash2(hash_key, hash_u32s, 0);
425e6445719SPravin B Shelar }
426e6445719SPravin B Shelar 
427e6445719SPravin B Shelar static int flow_key_start(const struct sw_flow_key *key)
428e6445719SPravin B Shelar {
429e6445719SPravin B Shelar 	if (key->tun_key.ipv4_dst)
430e6445719SPravin B Shelar 		return 0;
431e6445719SPravin B Shelar 	else
432e6445719SPravin B Shelar 		return rounddown(offsetof(struct sw_flow_key, phy),
433e6445719SPravin B Shelar 					  sizeof(long));
434e6445719SPravin B Shelar }
435e6445719SPravin B Shelar 
436e6445719SPravin B Shelar static bool cmp_key(const struct sw_flow_key *key1,
437e6445719SPravin B Shelar 		    const struct sw_flow_key *key2,
438e6445719SPravin B Shelar 		    int key_start, int key_end)
439e6445719SPravin B Shelar {
4407085130bSDaniele Di Proietto 	const long *cp1 = (const long *)((const u8 *)key1 + key_start);
4417085130bSDaniele Di Proietto 	const long *cp2 = (const long *)((const u8 *)key2 + key_start);
442e6445719SPravin B Shelar 	long diffs = 0;
443e6445719SPravin B Shelar 	int i;
444e6445719SPravin B Shelar 
445e6445719SPravin B Shelar 	for (i = key_start; i < key_end;  i += sizeof(long))
446e6445719SPravin B Shelar 		diffs |= *cp1++ ^ *cp2++;
447e6445719SPravin B Shelar 
448e6445719SPravin B Shelar 	return diffs == 0;
449e6445719SPravin B Shelar }
450e6445719SPravin B Shelar 
451e6445719SPravin B Shelar static bool flow_cmp_masked_key(const struct sw_flow *flow,
452e6445719SPravin B Shelar 				const struct sw_flow_key *key,
453272c2cf8SJoe Stringer 				const struct sw_flow_key_range *range)
454e6445719SPravin B Shelar {
455272c2cf8SJoe Stringer 	return cmp_key(&flow->key, key, range->start, range->end);
456e6445719SPravin B Shelar }
457e6445719SPravin B Shelar 
45874ed7ab9SJoe Stringer static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
45912eb18f7SThomas Graf 				      const struct sw_flow_match *match)
460e6445719SPravin B Shelar {
461e6445719SPravin B Shelar 	struct sw_flow_key *key = match->key;
462e6445719SPravin B Shelar 	int key_start = flow_key_start(key);
463e6445719SPravin B Shelar 	int key_end = match->range.end;
464e6445719SPravin B Shelar 
46574ed7ab9SJoe Stringer 	BUG_ON(ovs_identifier_is_ufid(&flow->id));
46674ed7ab9SJoe Stringer 	return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
467e6445719SPravin B Shelar }
468e6445719SPravin B Shelar 
469b637e498SPravin B Shelar static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
470e6445719SPravin B Shelar 					  const struct sw_flow_key *unmasked,
47112eb18f7SThomas Graf 					  const struct sw_flow_mask *mask)
472e6445719SPravin B Shelar {
473e6445719SPravin B Shelar 	struct sw_flow *flow;
474e6445719SPravin B Shelar 	struct hlist_head *head;
475e6445719SPravin B Shelar 	u32 hash;
476e6445719SPravin B Shelar 	struct sw_flow_key masked_key;
477e6445719SPravin B Shelar 
478e6445719SPravin B Shelar 	ovs_flow_mask_key(&masked_key, unmasked, mask);
479272c2cf8SJoe Stringer 	hash = flow_hash(&masked_key, &mask->range);
480b637e498SPravin B Shelar 	head = find_bucket(ti, hash);
48174ed7ab9SJoe Stringer 	hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
48274ed7ab9SJoe Stringer 		if (flow->mask == mask && flow->flow_table.hash == hash &&
483272c2cf8SJoe Stringer 		    flow_cmp_masked_key(flow, &masked_key, &mask->range))
484e6445719SPravin B Shelar 			return flow;
485e6445719SPravin B Shelar 	}
486e6445719SPravin B Shelar 	return NULL;
487e6445719SPravin B Shelar }
488e6445719SPravin B Shelar 
4895bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
4901bd7116fSAndy Zhou 				    const struct sw_flow_key *key,
4911bd7116fSAndy Zhou 				    u32 *n_mask_hit)
492e6445719SPravin B Shelar {
493663efa36SJesse Gross 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
494e6445719SPravin B Shelar 	struct sw_flow_mask *mask;
495b637e498SPravin B Shelar 	struct sw_flow *flow;
496e6445719SPravin B Shelar 
4971bd7116fSAndy Zhou 	*n_mask_hit = 0;
498b637e498SPravin B Shelar 	list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
4991bd7116fSAndy Zhou 		(*n_mask_hit)++;
500b637e498SPravin B Shelar 		flow = masked_flow_lookup(ti, key, mask);
501e6445719SPravin B Shelar 		if (flow)  /* Found */
502b637e498SPravin B Shelar 			return flow;
503b637e498SPravin B Shelar 	}
504b637e498SPravin B Shelar 	return NULL;
505e6445719SPravin B Shelar }
506e6445719SPravin B Shelar 
5075bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
5085bb50632SAndy Zhou 				    const struct sw_flow_key *key)
5095bb50632SAndy Zhou {
5105bb50632SAndy Zhou 	u32 __always_unused n_mask_hit;
5115bb50632SAndy Zhou 
5125bb50632SAndy Zhou 	return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
5135bb50632SAndy Zhou }
5145bb50632SAndy Zhou 
5154a46b24eSAlex Wang struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
51612eb18f7SThomas Graf 					  const struct sw_flow_match *match)
5174a46b24eSAlex Wang {
5184a46b24eSAlex Wang 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
5194a46b24eSAlex Wang 	struct sw_flow_mask *mask;
5204a46b24eSAlex Wang 	struct sw_flow *flow;
5214a46b24eSAlex Wang 
5224a46b24eSAlex Wang 	/* Always called under ovs-mutex. */
5234a46b24eSAlex Wang 	list_for_each_entry(mask, &tbl->mask_list, list) {
5244a46b24eSAlex Wang 		flow = masked_flow_lookup(ti, match->key, mask);
52574ed7ab9SJoe Stringer 		if (flow && ovs_identifier_is_key(&flow->id) &&
52674ed7ab9SJoe Stringer 		    ovs_flow_cmp_unmasked_key(flow, match))
52774ed7ab9SJoe Stringer 			return flow;
52874ed7ab9SJoe Stringer 	}
52974ed7ab9SJoe Stringer 	return NULL;
53074ed7ab9SJoe Stringer }
53174ed7ab9SJoe Stringer 
53274ed7ab9SJoe Stringer static u32 ufid_hash(const struct sw_flow_id *sfid)
53374ed7ab9SJoe Stringer {
53474ed7ab9SJoe Stringer 	return jhash(sfid->ufid, sfid->ufid_len, 0);
53574ed7ab9SJoe Stringer }
53674ed7ab9SJoe Stringer 
53774ed7ab9SJoe Stringer static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
53874ed7ab9SJoe Stringer 			      const struct sw_flow_id *sfid)
53974ed7ab9SJoe Stringer {
54074ed7ab9SJoe Stringer 	if (flow->id.ufid_len != sfid->ufid_len)
54174ed7ab9SJoe Stringer 		return false;
54274ed7ab9SJoe Stringer 
54374ed7ab9SJoe Stringer 	return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
54474ed7ab9SJoe Stringer }
54574ed7ab9SJoe Stringer 
54674ed7ab9SJoe Stringer bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
54774ed7ab9SJoe Stringer {
54874ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id))
54974ed7ab9SJoe Stringer 		return flow_cmp_masked_key(flow, match->key, &match->range);
55074ed7ab9SJoe Stringer 
55174ed7ab9SJoe Stringer 	return ovs_flow_cmp_unmasked_key(flow, match);
55274ed7ab9SJoe Stringer }
55374ed7ab9SJoe Stringer 
55474ed7ab9SJoe Stringer struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
55574ed7ab9SJoe Stringer 					 const struct sw_flow_id *ufid)
55674ed7ab9SJoe Stringer {
55774ed7ab9SJoe Stringer 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
55874ed7ab9SJoe Stringer 	struct sw_flow *flow;
55974ed7ab9SJoe Stringer 	struct hlist_head *head;
56074ed7ab9SJoe Stringer 	u32 hash;
56174ed7ab9SJoe Stringer 
56274ed7ab9SJoe Stringer 	hash = ufid_hash(ufid);
56374ed7ab9SJoe Stringer 	head = find_bucket(ti, hash);
56474ed7ab9SJoe Stringer 	hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
56574ed7ab9SJoe Stringer 		if (flow->ufid_table.hash == hash &&
56674ed7ab9SJoe Stringer 		    ovs_flow_cmp_ufid(flow, ufid))
5674a46b24eSAlex Wang 			return flow;
5684a46b24eSAlex Wang 	}
5694a46b24eSAlex Wang 	return NULL;
5704a46b24eSAlex Wang }
5714a46b24eSAlex Wang 
5721bd7116fSAndy Zhou int ovs_flow_tbl_num_masks(const struct flow_table *table)
5731bd7116fSAndy Zhou {
5741bd7116fSAndy Zhou 	struct sw_flow_mask *mask;
5751bd7116fSAndy Zhou 	int num = 0;
5761bd7116fSAndy Zhou 
5771bd7116fSAndy Zhou 	list_for_each_entry(mask, &table->mask_list, list)
5781bd7116fSAndy Zhou 		num++;
5791bd7116fSAndy Zhou 
5801bd7116fSAndy Zhou 	return num;
5811bd7116fSAndy Zhou }
5821bd7116fSAndy Zhou 
58374ed7ab9SJoe Stringer static struct table_instance *table_instance_expand(struct table_instance *ti,
58474ed7ab9SJoe Stringer 						    bool ufid)
585b637e498SPravin B Shelar {
58674ed7ab9SJoe Stringer 	return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
587e6445719SPravin B Shelar }
588e6445719SPravin B Shelar 
58956c19868SJarno Rajahalme /* Remove 'mask' from the mask list, if it is not needed any more. */
59056c19868SJarno Rajahalme static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
59156c19868SJarno Rajahalme {
59256c19868SJarno Rajahalme 	if (mask) {
59356c19868SJarno Rajahalme 		/* ovs-lock is required to protect mask-refcount and
59456c19868SJarno Rajahalme 		 * mask list.
59556c19868SJarno Rajahalme 		 */
59656c19868SJarno Rajahalme 		ASSERT_OVSL();
59756c19868SJarno Rajahalme 		BUG_ON(!mask->ref_count);
59856c19868SJarno Rajahalme 		mask->ref_count--;
59956c19868SJarno Rajahalme 
60056c19868SJarno Rajahalme 		if (!mask->ref_count) {
60156c19868SJarno Rajahalme 			list_del_rcu(&mask->list);
60256c19868SJarno Rajahalme 			kfree_rcu(mask, rcu);
60356c19868SJarno Rajahalme 		}
60456c19868SJarno Rajahalme 	}
60556c19868SJarno Rajahalme }
60656c19868SJarno Rajahalme 
60756c19868SJarno Rajahalme /* Must be called with OVS mutex held. */
608e6445719SPravin B Shelar void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
609e6445719SPravin B Shelar {
610b637e498SPravin B Shelar 	struct table_instance *ti = ovsl_dereference(table->ti);
61174ed7ab9SJoe Stringer 	struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
612b637e498SPravin B Shelar 
613e6445719SPravin B Shelar 	BUG_ON(table->count == 0);
61474ed7ab9SJoe Stringer 	hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
615e6445719SPravin B Shelar 	table->count--;
61674ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id)) {
61774ed7ab9SJoe Stringer 		hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
61874ed7ab9SJoe Stringer 		table->ufid_count--;
61974ed7ab9SJoe Stringer 	}
62056c19868SJarno Rajahalme 
62156c19868SJarno Rajahalme 	/* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
62256c19868SJarno Rajahalme 	 * accessible as long as the RCU read lock is held.
62356c19868SJarno Rajahalme 	 */
62456c19868SJarno Rajahalme 	flow_mask_remove(table, flow->mask);
625e6445719SPravin B Shelar }
626e6445719SPravin B Shelar 
627618ed0c8SPravin B Shelar static struct sw_flow_mask *mask_alloc(void)
628e6445719SPravin B Shelar {
629e6445719SPravin B Shelar 	struct sw_flow_mask *mask;
630e6445719SPravin B Shelar 
631e6445719SPravin B Shelar 	mask = kmalloc(sizeof(*mask), GFP_KERNEL);
632e6445719SPravin B Shelar 	if (mask)
633e80857ccSAndy Zhou 		mask->ref_count = 1;
634e6445719SPravin B Shelar 
635e6445719SPravin B Shelar 	return mask;
636e6445719SPravin B Shelar }
637e6445719SPravin B Shelar 
638e6445719SPravin B Shelar static bool mask_equal(const struct sw_flow_mask *a,
639e6445719SPravin B Shelar 		       const struct sw_flow_mask *b)
640e6445719SPravin B Shelar {
6417085130bSDaniele Di Proietto 	const u8 *a_ = (const u8 *)&a->key + a->range.start;
6427085130bSDaniele Di Proietto 	const u8 *b_ = (const u8 *)&b->key + b->range.start;
643e6445719SPravin B Shelar 
644e6445719SPravin B Shelar 	return  (a->range.end == b->range.end)
645e6445719SPravin B Shelar 		&& (a->range.start == b->range.start)
646e6445719SPravin B Shelar 		&& (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
647e6445719SPravin B Shelar }
648e6445719SPravin B Shelar 
649618ed0c8SPravin B Shelar static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
650e6445719SPravin B Shelar 					   const struct sw_flow_mask *mask)
651e6445719SPravin B Shelar {
652e6445719SPravin B Shelar 	struct list_head *ml;
653e6445719SPravin B Shelar 
654b637e498SPravin B Shelar 	list_for_each(ml, &tbl->mask_list) {
655e6445719SPravin B Shelar 		struct sw_flow_mask *m;
656e6445719SPravin B Shelar 		m = container_of(ml, struct sw_flow_mask, list);
657e6445719SPravin B Shelar 		if (mask_equal(mask, m))
658e6445719SPravin B Shelar 			return m;
659e6445719SPravin B Shelar 	}
660e6445719SPravin B Shelar 
661e6445719SPravin B Shelar 	return NULL;
662e6445719SPravin B Shelar }
663e6445719SPravin B Shelar 
664d1211908SBen Pfaff /* Add 'mask' into the mask list, if it is not already there. */
665618ed0c8SPravin B Shelar static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
66612eb18f7SThomas Graf 			    const struct sw_flow_mask *new)
667e6445719SPravin B Shelar {
668618ed0c8SPravin B Shelar 	struct sw_flow_mask *mask;
669618ed0c8SPravin B Shelar 	mask = flow_mask_find(tbl, new);
670618ed0c8SPravin B Shelar 	if (!mask) {
671618ed0c8SPravin B Shelar 		/* Allocate a new mask if none exsits. */
672618ed0c8SPravin B Shelar 		mask = mask_alloc();
673618ed0c8SPravin B Shelar 		if (!mask)
674618ed0c8SPravin B Shelar 			return -ENOMEM;
675618ed0c8SPravin B Shelar 		mask->key = new->key;
676618ed0c8SPravin B Shelar 		mask->range = new->range;
677b637e498SPravin B Shelar 		list_add_rcu(&mask->list, &tbl->mask_list);
678e80857ccSAndy Zhou 	} else {
679e80857ccSAndy Zhou 		BUG_ON(!mask->ref_count);
680e80857ccSAndy Zhou 		mask->ref_count++;
681e6445719SPravin B Shelar 	}
682e6445719SPravin B Shelar 
683618ed0c8SPravin B Shelar 	flow->mask = mask;
684618ed0c8SPravin B Shelar 	return 0;
685618ed0c8SPravin B Shelar }
686618ed0c8SPravin B Shelar 
68756c19868SJarno Rajahalme /* Must be called with OVS mutex held. */
688d29ab6f8SJoe Stringer static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
689618ed0c8SPravin B Shelar {
690618ed0c8SPravin B Shelar 	struct table_instance *new_ti = NULL;
691618ed0c8SPravin B Shelar 	struct table_instance *ti;
692618ed0c8SPravin B Shelar 
69374ed7ab9SJoe Stringer 	flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
694618ed0c8SPravin B Shelar 	ti = ovsl_dereference(table->ti);
695618ed0c8SPravin B Shelar 	table_instance_insert(ti, flow);
696618ed0c8SPravin B Shelar 	table->count++;
697618ed0c8SPravin B Shelar 
698618ed0c8SPravin B Shelar 	/* Expand table, if necessary, to make room. */
699618ed0c8SPravin B Shelar 	if (table->count > ti->n_buckets)
70074ed7ab9SJoe Stringer 		new_ti = table_instance_expand(ti, false);
701618ed0c8SPravin B Shelar 	else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
70274ed7ab9SJoe Stringer 		new_ti = table_instance_rehash(ti, ti->n_buckets, false);
703618ed0c8SPravin B Shelar 
704618ed0c8SPravin B Shelar 	if (new_ti) {
705618ed0c8SPravin B Shelar 		rcu_assign_pointer(table->ti, new_ti);
70674ed7ab9SJoe Stringer 		call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
707618ed0c8SPravin B Shelar 		table->last_rehash = jiffies;
708618ed0c8SPravin B Shelar 	}
709d29ab6f8SJoe Stringer }
710d29ab6f8SJoe Stringer 
711d29ab6f8SJoe Stringer /* Must be called with OVS mutex held. */
71274ed7ab9SJoe Stringer static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
71374ed7ab9SJoe Stringer {
71474ed7ab9SJoe Stringer 	struct table_instance *ti;
71574ed7ab9SJoe Stringer 
71674ed7ab9SJoe Stringer 	flow->ufid_table.hash = ufid_hash(&flow->id);
71774ed7ab9SJoe Stringer 	ti = ovsl_dereference(table->ufid_ti);
71874ed7ab9SJoe Stringer 	ufid_table_instance_insert(ti, flow);
71974ed7ab9SJoe Stringer 	table->ufid_count++;
72074ed7ab9SJoe Stringer 
72174ed7ab9SJoe Stringer 	/* Expand table, if necessary, to make room. */
72274ed7ab9SJoe Stringer 	if (table->ufid_count > ti->n_buckets) {
72374ed7ab9SJoe Stringer 		struct table_instance *new_ti;
72474ed7ab9SJoe Stringer 
72574ed7ab9SJoe Stringer 		new_ti = table_instance_expand(ti, true);
72674ed7ab9SJoe Stringer 		if (new_ti) {
72774ed7ab9SJoe Stringer 			rcu_assign_pointer(table->ufid_ti, new_ti);
72874ed7ab9SJoe Stringer 			call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
72974ed7ab9SJoe Stringer 		}
73074ed7ab9SJoe Stringer 	}
73174ed7ab9SJoe Stringer }
73274ed7ab9SJoe Stringer 
73374ed7ab9SJoe Stringer /* Must be called with OVS mutex held. */
734d29ab6f8SJoe Stringer int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
735d29ab6f8SJoe Stringer 			const struct sw_flow_mask *mask)
736d29ab6f8SJoe Stringer {
737d29ab6f8SJoe Stringer 	int err;
738d29ab6f8SJoe Stringer 
739d29ab6f8SJoe Stringer 	err = flow_mask_insert(table, flow, mask);
740d29ab6f8SJoe Stringer 	if (err)
741d29ab6f8SJoe Stringer 		return err;
742d29ab6f8SJoe Stringer 	flow_key_insert(table, flow);
74374ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id))
74474ed7ab9SJoe Stringer 		flow_ufid_insert(table, flow);
745d29ab6f8SJoe Stringer 
746618ed0c8SPravin B Shelar 	return 0;
747618ed0c8SPravin B Shelar }
748618ed0c8SPravin B Shelar 
749e6445719SPravin B Shelar /* Initializes the flow module.
750e6445719SPravin B Shelar  * Returns zero if successful or a negative error code. */
751e6445719SPravin B Shelar int ovs_flow_init(void)
752e6445719SPravin B Shelar {
753e6445719SPravin B Shelar 	BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
754e6445719SPravin B Shelar 	BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
755e6445719SPravin B Shelar 
75663e7959cSJarno Rajahalme 	flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
75763e7959cSJarno Rajahalme 				       + (num_possible_nodes()
75863e7959cSJarno Rajahalme 					  * sizeof(struct flow_stats *)),
75963e7959cSJarno Rajahalme 				       0, 0, NULL);
760e6445719SPravin B Shelar 	if (flow_cache == NULL)
761e6445719SPravin B Shelar 		return -ENOMEM;
762e6445719SPravin B Shelar 
76363e7959cSJarno Rajahalme 	flow_stats_cache
76463e7959cSJarno Rajahalme 		= kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
76563e7959cSJarno Rajahalme 				    0, SLAB_HWCACHE_ALIGN, NULL);
76663e7959cSJarno Rajahalme 	if (flow_stats_cache == NULL) {
76763e7959cSJarno Rajahalme 		kmem_cache_destroy(flow_cache);
76863e7959cSJarno Rajahalme 		flow_cache = NULL;
76963e7959cSJarno Rajahalme 		return -ENOMEM;
77063e7959cSJarno Rajahalme 	}
77163e7959cSJarno Rajahalme 
772e6445719SPravin B Shelar 	return 0;
773e6445719SPravin B Shelar }
774e6445719SPravin B Shelar 
775e6445719SPravin B Shelar /* Uninitializes the flow module. */
776e6445719SPravin B Shelar void ovs_flow_exit(void)
777e6445719SPravin B Shelar {
77863e7959cSJarno Rajahalme 	kmem_cache_destroy(flow_stats_cache);
779e6445719SPravin B Shelar 	kmem_cache_destroy(flow_cache);
780e6445719SPravin B Shelar }
781