xref: /openbmc/linux/net/openvswitch/flow_table.c (revision ca539345f8767cca221b5aa77bf4329c725d0d7e)
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;
88*ca539345SPravin B Shelar 	flow->id.unmasked_key = NULL;
89*ca539345SPravin B Shelar 	flow->id.ufid_len = 0;
9063e7959cSJarno Rajahalme 	flow->stats_last_writer = NUMA_NO_NODE;
91e6445719SPravin B Shelar 
9263e7959cSJarno Rajahalme 	/* Initialize the default stat node. */
9363e7959cSJarno Rajahalme 	stats = kmem_cache_alloc_node(flow_stats_cache,
9463e7959cSJarno Rajahalme 				      GFP_KERNEL | __GFP_ZERO, 0);
9563e7959cSJarno Rajahalme 	if (!stats)
96e298e505SPravin B Shelar 		goto err;
97e298e505SPravin B Shelar 
9863e7959cSJarno Rajahalme 	spin_lock_init(&stats->lock);
99e298e505SPravin B Shelar 
10063e7959cSJarno Rajahalme 	RCU_INIT_POINTER(flow->stats[0], stats);
10163e7959cSJarno Rajahalme 
10263e7959cSJarno Rajahalme 	for_each_node(node)
10363e7959cSJarno Rajahalme 		if (node != 0)
10463e7959cSJarno Rajahalme 			RCU_INIT_POINTER(flow->stats[node], NULL);
10563e7959cSJarno Rajahalme 
106e6445719SPravin B Shelar 	return flow;
107e298e505SPravin B Shelar err:
108ece37c87SWei Yongjun 	kmem_cache_free(flow_cache, flow);
109e298e505SPravin B Shelar 	return ERR_PTR(-ENOMEM);
110e6445719SPravin B Shelar }
111e6445719SPravin B Shelar 
11212eb18f7SThomas Graf int ovs_flow_tbl_count(const struct flow_table *table)
113b637e498SPravin B Shelar {
114b637e498SPravin B Shelar 	return table->count;
115b637e498SPravin B Shelar }
116b637e498SPravin B Shelar 
117e6445719SPravin B Shelar static struct flex_array *alloc_buckets(unsigned int n_buckets)
118e6445719SPravin B Shelar {
119e6445719SPravin B Shelar 	struct flex_array *buckets;
120e6445719SPravin B Shelar 	int i, err;
121e6445719SPravin B Shelar 
122e6445719SPravin B Shelar 	buckets = flex_array_alloc(sizeof(struct hlist_head),
123e6445719SPravin B Shelar 				   n_buckets, GFP_KERNEL);
124e6445719SPravin B Shelar 	if (!buckets)
125e6445719SPravin B Shelar 		return NULL;
126e6445719SPravin B Shelar 
127e6445719SPravin B Shelar 	err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
128e6445719SPravin B Shelar 	if (err) {
129e6445719SPravin B Shelar 		flex_array_free(buckets);
130e6445719SPravin B Shelar 		return NULL;
131e6445719SPravin B Shelar 	}
132e6445719SPravin B Shelar 
133e6445719SPravin B Shelar 	for (i = 0; i < n_buckets; i++)
134e6445719SPravin B Shelar 		INIT_HLIST_HEAD((struct hlist_head *)
135e6445719SPravin B Shelar 					flex_array_get(buckets, i));
136e6445719SPravin B Shelar 
137e6445719SPravin B Shelar 	return buckets;
138e6445719SPravin B Shelar }
139e6445719SPravin B Shelar 
140e6445719SPravin B Shelar static void flow_free(struct sw_flow *flow)
141e6445719SPravin B Shelar {
14263e7959cSJarno Rajahalme 	int node;
14363e7959cSJarno Rajahalme 
14474ed7ab9SJoe Stringer 	if (ovs_identifier_is_key(&flow->id))
14574ed7ab9SJoe Stringer 		kfree(flow->id.unmasked_key);
146eb072659SJarno Rajahalme 	kfree((struct sw_flow_actions __force *)flow->sf_acts);
14763e7959cSJarno Rajahalme 	for_each_node(node)
14863e7959cSJarno Rajahalme 		if (flow->stats[node])
14963e7959cSJarno Rajahalme 			kmem_cache_free(flow_stats_cache,
15063e7959cSJarno Rajahalme 					(struct flow_stats __force *)flow->stats[node]);
151e6445719SPravin B Shelar 	kmem_cache_free(flow_cache, flow);
152e6445719SPravin B Shelar }
153e6445719SPravin B Shelar 
154e6445719SPravin B Shelar static void rcu_free_flow_callback(struct rcu_head *rcu)
155e6445719SPravin B Shelar {
156e6445719SPravin B Shelar 	struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
157e6445719SPravin B Shelar 
158e6445719SPravin B Shelar 	flow_free(flow);
159e6445719SPravin B Shelar }
160e6445719SPravin B Shelar 
161e80857ccSAndy Zhou void ovs_flow_free(struct sw_flow *flow, bool deferred)
162618ed0c8SPravin B Shelar {
163e80857ccSAndy Zhou 	if (!flow)
164618ed0c8SPravin B Shelar 		return;
165618ed0c8SPravin B Shelar 
166e6445719SPravin B Shelar 	if (deferred)
167e6445719SPravin B Shelar 		call_rcu(&flow->rcu, rcu_free_flow_callback);
168e6445719SPravin B Shelar 	else
169e6445719SPravin B Shelar 		flow_free(flow);
170e6445719SPravin B Shelar }
171e6445719SPravin B Shelar 
172e6445719SPravin B Shelar static void free_buckets(struct flex_array *buckets)
173e6445719SPravin B Shelar {
174e6445719SPravin B Shelar 	flex_array_free(buckets);
175e6445719SPravin B Shelar }
176e6445719SPravin B Shelar 
177e80857ccSAndy Zhou 
178b637e498SPravin B Shelar static void __table_instance_destroy(struct table_instance *ti)
179e6445719SPravin B Shelar {
180b637e498SPravin B Shelar 	free_buckets(ti->buckets);
181b637e498SPravin B Shelar 	kfree(ti);
182e6445719SPravin B Shelar }
183e6445719SPravin B Shelar 
184b637e498SPravin B Shelar static struct table_instance *table_instance_alloc(int new_size)
185e6445719SPravin B Shelar {
186b637e498SPravin B Shelar 	struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
187e6445719SPravin B Shelar 
188b637e498SPravin B Shelar 	if (!ti)
189e6445719SPravin B Shelar 		return NULL;
190e6445719SPravin B Shelar 
191b637e498SPravin B Shelar 	ti->buckets = alloc_buckets(new_size);
192e6445719SPravin B Shelar 
193b637e498SPravin B Shelar 	if (!ti->buckets) {
194b637e498SPravin B Shelar 		kfree(ti);
195e6445719SPravin B Shelar 		return NULL;
196e6445719SPravin B Shelar 	}
197b637e498SPravin B Shelar 	ti->n_buckets = new_size;
198b637e498SPravin B Shelar 	ti->node_ver = 0;
199b637e498SPravin B Shelar 	ti->keep_flows = false;
200b637e498SPravin B Shelar 	get_random_bytes(&ti->hash_seed, sizeof(u32));
201b637e498SPravin B Shelar 
202b637e498SPravin B Shelar 	return ti;
203b637e498SPravin B Shelar }
204b637e498SPravin B Shelar 
205b637e498SPravin B Shelar int ovs_flow_tbl_init(struct flow_table *table)
206b637e498SPravin B Shelar {
20774ed7ab9SJoe Stringer 	struct table_instance *ti, *ufid_ti;
208b637e498SPravin B Shelar 
209b637e498SPravin B Shelar 	ti = table_instance_alloc(TBL_MIN_BUCKETS);
210b637e498SPravin B Shelar 
211b637e498SPravin B Shelar 	if (!ti)
212b637e498SPravin B Shelar 		return -ENOMEM;
213b637e498SPravin B Shelar 
21474ed7ab9SJoe Stringer 	ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
21574ed7ab9SJoe Stringer 	if (!ufid_ti)
21674ed7ab9SJoe Stringer 		goto free_ti;
21774ed7ab9SJoe Stringer 
218b637e498SPravin B Shelar 	rcu_assign_pointer(table->ti, ti);
21974ed7ab9SJoe Stringer 	rcu_assign_pointer(table->ufid_ti, ufid_ti);
220b637e498SPravin B Shelar 	INIT_LIST_HEAD(&table->mask_list);
221b637e498SPravin B Shelar 	table->last_rehash = jiffies;
222e6445719SPravin B Shelar 	table->count = 0;
22374ed7ab9SJoe Stringer 	table->ufid_count = 0;
224b637e498SPravin B Shelar 	return 0;
22574ed7ab9SJoe Stringer 
22674ed7ab9SJoe Stringer free_ti:
22774ed7ab9SJoe Stringer 	__table_instance_destroy(ti);
22874ed7ab9SJoe Stringer 	return -ENOMEM;
229e6445719SPravin B Shelar }
230e6445719SPravin B Shelar 
231e6445719SPravin B Shelar static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
232e6445719SPravin B Shelar {
233b637e498SPravin B Shelar 	struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
234e6445719SPravin B Shelar 
235b637e498SPravin B Shelar 	__table_instance_destroy(ti);
236b637e498SPravin B Shelar }
237b637e498SPravin B Shelar 
23874ed7ab9SJoe Stringer static void table_instance_destroy(struct table_instance *ti,
23974ed7ab9SJoe Stringer 				   struct table_instance *ufid_ti,
24074ed7ab9SJoe Stringer 				   bool deferred)
241b637e498SPravin B Shelar {
242e80857ccSAndy Zhou 	int i;
243e80857ccSAndy Zhou 
244b637e498SPravin B Shelar 	if (!ti)
245b637e498SPravin B Shelar 		return;
246b637e498SPravin B Shelar 
24774ed7ab9SJoe Stringer 	BUG_ON(!ufid_ti);
248e80857ccSAndy Zhou 	if (ti->keep_flows)
249e80857ccSAndy Zhou 		goto skip_flows;
250e80857ccSAndy Zhou 
251e80857ccSAndy Zhou 	for (i = 0; i < ti->n_buckets; i++) {
252e80857ccSAndy Zhou 		struct sw_flow *flow;
253e80857ccSAndy Zhou 		struct hlist_head *head = flex_array_get(ti->buckets, i);
254e80857ccSAndy Zhou 		struct hlist_node *n;
255e80857ccSAndy Zhou 		int ver = ti->node_ver;
25674ed7ab9SJoe Stringer 		int ufid_ver = ufid_ti->node_ver;
257e80857ccSAndy Zhou 
25874ed7ab9SJoe Stringer 		hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
25974ed7ab9SJoe Stringer 			hlist_del_rcu(&flow->flow_table.node[ver]);
26074ed7ab9SJoe Stringer 			if (ovs_identifier_is_ufid(&flow->id))
26174ed7ab9SJoe Stringer 				hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
262e80857ccSAndy Zhou 			ovs_flow_free(flow, deferred);
263e80857ccSAndy Zhou 		}
264e80857ccSAndy Zhou 	}
265e80857ccSAndy Zhou 
266e80857ccSAndy Zhou skip_flows:
26774ed7ab9SJoe Stringer 	if (deferred) {
268b637e498SPravin B Shelar 		call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
26974ed7ab9SJoe Stringer 		call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
27074ed7ab9SJoe Stringer 	} else {
271b637e498SPravin B Shelar 		__table_instance_destroy(ti);
27274ed7ab9SJoe Stringer 		__table_instance_destroy(ufid_ti);
27374ed7ab9SJoe Stringer 	}
274e6445719SPravin B Shelar }
275e6445719SPravin B Shelar 
2769b996e54SPravin B Shelar /* No need for locking this function is called from RCU callback or
2779b996e54SPravin B Shelar  * error path.
2789b996e54SPravin B Shelar  */
2799b996e54SPravin B Shelar void ovs_flow_tbl_destroy(struct flow_table *table)
280e6445719SPravin B Shelar {
2819b996e54SPravin B Shelar 	struct table_instance *ti = rcu_dereference_raw(table->ti);
28274ed7ab9SJoe Stringer 	struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
283e6445719SPravin B Shelar 
28474ed7ab9SJoe Stringer 	table_instance_destroy(ti, ufid_ti, false);
285e6445719SPravin B Shelar }
286e6445719SPravin B Shelar 
287b637e498SPravin B Shelar struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
288e6445719SPravin B Shelar 				       u32 *bucket, u32 *last)
289e6445719SPravin B Shelar {
290e6445719SPravin B Shelar 	struct sw_flow *flow;
291e6445719SPravin B Shelar 	struct hlist_head *head;
292e6445719SPravin B Shelar 	int ver;
293e6445719SPravin B Shelar 	int i;
294e6445719SPravin B Shelar 
295b637e498SPravin B Shelar 	ver = ti->node_ver;
296b637e498SPravin B Shelar 	while (*bucket < ti->n_buckets) {
297e6445719SPravin B Shelar 		i = 0;
298b637e498SPravin B Shelar 		head = flex_array_get(ti->buckets, *bucket);
29974ed7ab9SJoe Stringer 		hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
300e6445719SPravin B Shelar 			if (i < *last) {
301e6445719SPravin B Shelar 				i++;
302e6445719SPravin B Shelar 				continue;
303e6445719SPravin B Shelar 			}
304e6445719SPravin B Shelar 			*last = i + 1;
305e6445719SPravin B Shelar 			return flow;
306e6445719SPravin B Shelar 		}
307e6445719SPravin B Shelar 		(*bucket)++;
308e6445719SPravin B Shelar 		*last = 0;
309e6445719SPravin B Shelar 	}
310e6445719SPravin B Shelar 
311e6445719SPravin B Shelar 	return NULL;
312e6445719SPravin B Shelar }
313e6445719SPravin B Shelar 
314b637e498SPravin B Shelar static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
315e6445719SPravin B Shelar {
316b637e498SPravin B Shelar 	hash = jhash_1word(hash, ti->hash_seed);
317b637e498SPravin B Shelar 	return flex_array_get(ti->buckets,
318b637e498SPravin B Shelar 				(hash & (ti->n_buckets - 1)));
319e6445719SPravin B Shelar }
320e6445719SPravin B Shelar 
32174ed7ab9SJoe Stringer static void table_instance_insert(struct table_instance *ti,
32274ed7ab9SJoe Stringer 				  struct sw_flow *flow)
323e6445719SPravin B Shelar {
324e6445719SPravin B Shelar 	struct hlist_head *head;
325e6445719SPravin B Shelar 
32674ed7ab9SJoe Stringer 	head = find_bucket(ti, flow->flow_table.hash);
32774ed7ab9SJoe Stringer 	hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
32874ed7ab9SJoe Stringer }
32974ed7ab9SJoe Stringer 
33074ed7ab9SJoe Stringer static void ufid_table_instance_insert(struct table_instance *ti,
33174ed7ab9SJoe Stringer 				       struct sw_flow *flow)
33274ed7ab9SJoe Stringer {
33374ed7ab9SJoe Stringer 	struct hlist_head *head;
33474ed7ab9SJoe Stringer 
33574ed7ab9SJoe Stringer 	head = find_bucket(ti, flow->ufid_table.hash);
33674ed7ab9SJoe Stringer 	hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
337e6445719SPravin B Shelar }
338e6445719SPravin B Shelar 
339b637e498SPravin B Shelar static void flow_table_copy_flows(struct table_instance *old,
34074ed7ab9SJoe Stringer 				  struct table_instance *new, bool ufid)
341e6445719SPravin B Shelar {
342e6445719SPravin B Shelar 	int old_ver;
343e6445719SPravin B Shelar 	int i;
344e6445719SPravin B Shelar 
345e6445719SPravin B Shelar 	old_ver = old->node_ver;
346e6445719SPravin B Shelar 	new->node_ver = !old_ver;
347e6445719SPravin B Shelar 
348e6445719SPravin B Shelar 	/* Insert in new table. */
349e6445719SPravin B Shelar 	for (i = 0; i < old->n_buckets; i++) {
350e6445719SPravin B Shelar 		struct sw_flow *flow;
351e6445719SPravin B Shelar 		struct hlist_head *head;
352e6445719SPravin B Shelar 
353e6445719SPravin B Shelar 		head = flex_array_get(old->buckets, i);
354e6445719SPravin B Shelar 
35574ed7ab9SJoe Stringer 		if (ufid)
35674ed7ab9SJoe Stringer 			hlist_for_each_entry(flow, head,
35774ed7ab9SJoe Stringer 					     ufid_table.node[old_ver])
35874ed7ab9SJoe Stringer 				ufid_table_instance_insert(new, flow);
35974ed7ab9SJoe Stringer 		else
36074ed7ab9SJoe Stringer 			hlist_for_each_entry(flow, head,
36174ed7ab9SJoe Stringer 					     flow_table.node[old_ver])
362b637e498SPravin B Shelar 				table_instance_insert(new, flow);
363e6445719SPravin B Shelar 	}
364e6445719SPravin B Shelar 
365e6445719SPravin B Shelar 	old->keep_flows = true;
366e6445719SPravin B Shelar }
367e6445719SPravin B Shelar 
368b637e498SPravin B Shelar static struct table_instance *table_instance_rehash(struct table_instance *ti,
36974ed7ab9SJoe Stringer 						    int n_buckets, bool ufid)
370e6445719SPravin B Shelar {
371b637e498SPravin B Shelar 	struct table_instance *new_ti;
372e6445719SPravin B Shelar 
373b637e498SPravin B Shelar 	new_ti = table_instance_alloc(n_buckets);
374b637e498SPravin B Shelar 	if (!new_ti)
375618ed0c8SPravin B Shelar 		return NULL;
376e6445719SPravin B Shelar 
37774ed7ab9SJoe Stringer 	flow_table_copy_flows(ti, new_ti, ufid);
378e6445719SPravin B Shelar 
379b637e498SPravin B Shelar 	return new_ti;
380e6445719SPravin B Shelar }
381e6445719SPravin B Shelar 
382b637e498SPravin B Shelar int ovs_flow_tbl_flush(struct flow_table *flow_table)
383e6445719SPravin B Shelar {
38474ed7ab9SJoe Stringer 	struct table_instance *old_ti, *new_ti;
38574ed7ab9SJoe Stringer 	struct table_instance *old_ufid_ti, *new_ufid_ti;
386e6445719SPravin B Shelar 
387b637e498SPravin B Shelar 	new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
388b637e498SPravin B Shelar 	if (!new_ti)
389b637e498SPravin B Shelar 		return -ENOMEM;
39074ed7ab9SJoe Stringer 	new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
39174ed7ab9SJoe Stringer 	if (!new_ufid_ti)
39274ed7ab9SJoe Stringer 		goto err_free_ti;
39374ed7ab9SJoe Stringer 
39474ed7ab9SJoe Stringer 	old_ti = ovsl_dereference(flow_table->ti);
39574ed7ab9SJoe Stringer 	old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
396b637e498SPravin B Shelar 
397b637e498SPravin B Shelar 	rcu_assign_pointer(flow_table->ti, new_ti);
39874ed7ab9SJoe Stringer 	rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
399b637e498SPravin B Shelar 	flow_table->last_rehash = jiffies;
400b637e498SPravin B Shelar 	flow_table->count = 0;
40174ed7ab9SJoe Stringer 	flow_table->ufid_count = 0;
402b637e498SPravin B Shelar 
40374ed7ab9SJoe Stringer 	table_instance_destroy(old_ti, old_ufid_ti, true);
404b637e498SPravin B Shelar 	return 0;
40574ed7ab9SJoe Stringer 
40674ed7ab9SJoe Stringer err_free_ti:
40774ed7ab9SJoe Stringer 	__table_instance_destroy(new_ti);
40874ed7ab9SJoe Stringer 	return -ENOMEM;
409e6445719SPravin B Shelar }
410e6445719SPravin B Shelar 
411272c2cf8SJoe Stringer static u32 flow_hash(const struct sw_flow_key *key,
412272c2cf8SJoe Stringer 		     const struct sw_flow_key_range *range)
413e6445719SPravin B Shelar {
414272c2cf8SJoe Stringer 	int key_start = range->start;
415272c2cf8SJoe Stringer 	int key_end = range->end;
4167085130bSDaniele Di Proietto 	const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
417e6445719SPravin B Shelar 	int hash_u32s = (key_end - key_start) >> 2;
418e6445719SPravin B Shelar 
419e6445719SPravin B Shelar 	/* Make sure number of hash bytes are multiple of u32. */
420e6445719SPravin B Shelar 	BUILD_BUG_ON(sizeof(long) % sizeof(u32));
421e6445719SPravin B Shelar 
42287545899SDaniel Borkmann 	return jhash2(hash_key, hash_u32s, 0);
423e6445719SPravin B Shelar }
424e6445719SPravin B Shelar 
425e6445719SPravin B Shelar static int flow_key_start(const struct sw_flow_key *key)
426e6445719SPravin B Shelar {
427e6445719SPravin B Shelar 	if (key->tun_key.ipv4_dst)
428e6445719SPravin B Shelar 		return 0;
429e6445719SPravin B Shelar 	else
430e6445719SPravin B Shelar 		return rounddown(offsetof(struct sw_flow_key, phy),
431e6445719SPravin B Shelar 					  sizeof(long));
432e6445719SPravin B Shelar }
433e6445719SPravin B Shelar 
434e6445719SPravin B Shelar static bool cmp_key(const struct sw_flow_key *key1,
435e6445719SPravin B Shelar 		    const struct sw_flow_key *key2,
436e6445719SPravin B Shelar 		    int key_start, int key_end)
437e6445719SPravin B Shelar {
4387085130bSDaniele Di Proietto 	const long *cp1 = (const long *)((const u8 *)key1 + key_start);
4397085130bSDaniele Di Proietto 	const long *cp2 = (const long *)((const u8 *)key2 + key_start);
440e6445719SPravin B Shelar 	long diffs = 0;
441e6445719SPravin B Shelar 	int i;
442e6445719SPravin B Shelar 
443e6445719SPravin B Shelar 	for (i = key_start; i < key_end;  i += sizeof(long))
444e6445719SPravin B Shelar 		diffs |= *cp1++ ^ *cp2++;
445e6445719SPravin B Shelar 
446e6445719SPravin B Shelar 	return diffs == 0;
447e6445719SPravin B Shelar }
448e6445719SPravin B Shelar 
449e6445719SPravin B Shelar static bool flow_cmp_masked_key(const struct sw_flow *flow,
450e6445719SPravin B Shelar 				const struct sw_flow_key *key,
451272c2cf8SJoe Stringer 				const struct sw_flow_key_range *range)
452e6445719SPravin B Shelar {
453272c2cf8SJoe Stringer 	return cmp_key(&flow->key, key, range->start, range->end);
454e6445719SPravin B Shelar }
455e6445719SPravin B Shelar 
45674ed7ab9SJoe Stringer static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
45712eb18f7SThomas Graf 				      const struct sw_flow_match *match)
458e6445719SPravin B Shelar {
459e6445719SPravin B Shelar 	struct sw_flow_key *key = match->key;
460e6445719SPravin B Shelar 	int key_start = flow_key_start(key);
461e6445719SPravin B Shelar 	int key_end = match->range.end;
462e6445719SPravin B Shelar 
46374ed7ab9SJoe Stringer 	BUG_ON(ovs_identifier_is_ufid(&flow->id));
46474ed7ab9SJoe Stringer 	return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
465e6445719SPravin B Shelar }
466e6445719SPravin B Shelar 
467b637e498SPravin B Shelar static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
468e6445719SPravin B Shelar 					  const struct sw_flow_key *unmasked,
46912eb18f7SThomas Graf 					  const struct sw_flow_mask *mask)
470e6445719SPravin B Shelar {
471e6445719SPravin B Shelar 	struct sw_flow *flow;
472e6445719SPravin B Shelar 	struct hlist_head *head;
473e6445719SPravin B Shelar 	u32 hash;
474e6445719SPravin B Shelar 	struct sw_flow_key masked_key;
475e6445719SPravin B Shelar 
476e6445719SPravin B Shelar 	ovs_flow_mask_key(&masked_key, unmasked, mask);
477272c2cf8SJoe Stringer 	hash = flow_hash(&masked_key, &mask->range);
478b637e498SPravin B Shelar 	head = find_bucket(ti, hash);
47974ed7ab9SJoe Stringer 	hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
48074ed7ab9SJoe Stringer 		if (flow->mask == mask && flow->flow_table.hash == hash &&
481272c2cf8SJoe Stringer 		    flow_cmp_masked_key(flow, &masked_key, &mask->range))
482e6445719SPravin B Shelar 			return flow;
483e6445719SPravin B Shelar 	}
484e6445719SPravin B Shelar 	return NULL;
485e6445719SPravin B Shelar }
486e6445719SPravin B Shelar 
4875bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
4881bd7116fSAndy Zhou 				    const struct sw_flow_key *key,
4891bd7116fSAndy Zhou 				    u32 *n_mask_hit)
490e6445719SPravin B Shelar {
491663efa36SJesse Gross 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
492e6445719SPravin B Shelar 	struct sw_flow_mask *mask;
493b637e498SPravin B Shelar 	struct sw_flow *flow;
494e6445719SPravin B Shelar 
4951bd7116fSAndy Zhou 	*n_mask_hit = 0;
496b637e498SPravin B Shelar 	list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
4971bd7116fSAndy Zhou 		(*n_mask_hit)++;
498b637e498SPravin B Shelar 		flow = masked_flow_lookup(ti, key, mask);
499e6445719SPravin B Shelar 		if (flow)  /* Found */
500b637e498SPravin B Shelar 			return flow;
501b637e498SPravin B Shelar 	}
502b637e498SPravin B Shelar 	return NULL;
503e6445719SPravin B Shelar }
504e6445719SPravin B Shelar 
5055bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
5065bb50632SAndy Zhou 				    const struct sw_flow_key *key)
5075bb50632SAndy Zhou {
5085bb50632SAndy Zhou 	u32 __always_unused n_mask_hit;
5095bb50632SAndy Zhou 
5105bb50632SAndy Zhou 	return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
5115bb50632SAndy Zhou }
5125bb50632SAndy Zhou 
5134a46b24eSAlex Wang struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
51412eb18f7SThomas Graf 					  const struct sw_flow_match *match)
5154a46b24eSAlex Wang {
5164a46b24eSAlex Wang 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
5174a46b24eSAlex Wang 	struct sw_flow_mask *mask;
5184a46b24eSAlex Wang 	struct sw_flow *flow;
5194a46b24eSAlex Wang 
5204a46b24eSAlex Wang 	/* Always called under ovs-mutex. */
5214a46b24eSAlex Wang 	list_for_each_entry(mask, &tbl->mask_list, list) {
5224a46b24eSAlex Wang 		flow = masked_flow_lookup(ti, match->key, mask);
52374ed7ab9SJoe Stringer 		if (flow && ovs_identifier_is_key(&flow->id) &&
52474ed7ab9SJoe Stringer 		    ovs_flow_cmp_unmasked_key(flow, match))
52574ed7ab9SJoe Stringer 			return flow;
52674ed7ab9SJoe Stringer 	}
52774ed7ab9SJoe Stringer 	return NULL;
52874ed7ab9SJoe Stringer }
52974ed7ab9SJoe Stringer 
53074ed7ab9SJoe Stringer static u32 ufid_hash(const struct sw_flow_id *sfid)
53174ed7ab9SJoe Stringer {
53274ed7ab9SJoe Stringer 	return jhash(sfid->ufid, sfid->ufid_len, 0);
53374ed7ab9SJoe Stringer }
53474ed7ab9SJoe Stringer 
53574ed7ab9SJoe Stringer static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
53674ed7ab9SJoe Stringer 			      const struct sw_flow_id *sfid)
53774ed7ab9SJoe Stringer {
53874ed7ab9SJoe Stringer 	if (flow->id.ufid_len != sfid->ufid_len)
53974ed7ab9SJoe Stringer 		return false;
54074ed7ab9SJoe Stringer 
54174ed7ab9SJoe Stringer 	return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
54274ed7ab9SJoe Stringer }
54374ed7ab9SJoe Stringer 
54474ed7ab9SJoe Stringer bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
54574ed7ab9SJoe Stringer {
54674ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id))
54774ed7ab9SJoe Stringer 		return flow_cmp_masked_key(flow, match->key, &match->range);
54874ed7ab9SJoe Stringer 
54974ed7ab9SJoe Stringer 	return ovs_flow_cmp_unmasked_key(flow, match);
55074ed7ab9SJoe Stringer }
55174ed7ab9SJoe Stringer 
55274ed7ab9SJoe Stringer struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
55374ed7ab9SJoe Stringer 					 const struct sw_flow_id *ufid)
55474ed7ab9SJoe Stringer {
55574ed7ab9SJoe Stringer 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
55674ed7ab9SJoe Stringer 	struct sw_flow *flow;
55774ed7ab9SJoe Stringer 	struct hlist_head *head;
55874ed7ab9SJoe Stringer 	u32 hash;
55974ed7ab9SJoe Stringer 
56074ed7ab9SJoe Stringer 	hash = ufid_hash(ufid);
56174ed7ab9SJoe Stringer 	head = find_bucket(ti, hash);
56274ed7ab9SJoe Stringer 	hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
56374ed7ab9SJoe Stringer 		if (flow->ufid_table.hash == hash &&
56474ed7ab9SJoe Stringer 		    ovs_flow_cmp_ufid(flow, ufid))
5654a46b24eSAlex Wang 			return flow;
5664a46b24eSAlex Wang 	}
5674a46b24eSAlex Wang 	return NULL;
5684a46b24eSAlex Wang }
5694a46b24eSAlex Wang 
5701bd7116fSAndy Zhou int ovs_flow_tbl_num_masks(const struct flow_table *table)
5711bd7116fSAndy Zhou {
5721bd7116fSAndy Zhou 	struct sw_flow_mask *mask;
5731bd7116fSAndy Zhou 	int num = 0;
5741bd7116fSAndy Zhou 
5751bd7116fSAndy Zhou 	list_for_each_entry(mask, &table->mask_list, list)
5761bd7116fSAndy Zhou 		num++;
5771bd7116fSAndy Zhou 
5781bd7116fSAndy Zhou 	return num;
5791bd7116fSAndy Zhou }
5801bd7116fSAndy Zhou 
58174ed7ab9SJoe Stringer static struct table_instance *table_instance_expand(struct table_instance *ti,
58274ed7ab9SJoe Stringer 						    bool ufid)
583b637e498SPravin B Shelar {
58474ed7ab9SJoe Stringer 	return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
585e6445719SPravin B Shelar }
586e6445719SPravin B Shelar 
58756c19868SJarno Rajahalme /* Remove 'mask' from the mask list, if it is not needed any more. */
58856c19868SJarno Rajahalme static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
58956c19868SJarno Rajahalme {
59056c19868SJarno Rajahalme 	if (mask) {
59156c19868SJarno Rajahalme 		/* ovs-lock is required to protect mask-refcount and
59256c19868SJarno Rajahalme 		 * mask list.
59356c19868SJarno Rajahalme 		 */
59456c19868SJarno Rajahalme 		ASSERT_OVSL();
59556c19868SJarno Rajahalme 		BUG_ON(!mask->ref_count);
59656c19868SJarno Rajahalme 		mask->ref_count--;
59756c19868SJarno Rajahalme 
59856c19868SJarno Rajahalme 		if (!mask->ref_count) {
59956c19868SJarno Rajahalme 			list_del_rcu(&mask->list);
60056c19868SJarno Rajahalme 			kfree_rcu(mask, rcu);
60156c19868SJarno Rajahalme 		}
60256c19868SJarno Rajahalme 	}
60356c19868SJarno Rajahalme }
60456c19868SJarno Rajahalme 
60556c19868SJarno Rajahalme /* Must be called with OVS mutex held. */
606e6445719SPravin B Shelar void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
607e6445719SPravin B Shelar {
608b637e498SPravin B Shelar 	struct table_instance *ti = ovsl_dereference(table->ti);
60974ed7ab9SJoe Stringer 	struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
610b637e498SPravin B Shelar 
611e6445719SPravin B Shelar 	BUG_ON(table->count == 0);
61274ed7ab9SJoe Stringer 	hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
613e6445719SPravin B Shelar 	table->count--;
61474ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id)) {
61574ed7ab9SJoe Stringer 		hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
61674ed7ab9SJoe Stringer 		table->ufid_count--;
61774ed7ab9SJoe Stringer 	}
61856c19868SJarno Rajahalme 
61956c19868SJarno Rajahalme 	/* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
62056c19868SJarno Rajahalme 	 * accessible as long as the RCU read lock is held.
62156c19868SJarno Rajahalme 	 */
62256c19868SJarno Rajahalme 	flow_mask_remove(table, flow->mask);
623e6445719SPravin B Shelar }
624e6445719SPravin B Shelar 
625618ed0c8SPravin B Shelar static struct sw_flow_mask *mask_alloc(void)
626e6445719SPravin B Shelar {
627e6445719SPravin B Shelar 	struct sw_flow_mask *mask;
628e6445719SPravin B Shelar 
629e6445719SPravin B Shelar 	mask = kmalloc(sizeof(*mask), GFP_KERNEL);
630e6445719SPravin B Shelar 	if (mask)
631e80857ccSAndy Zhou 		mask->ref_count = 1;
632e6445719SPravin B Shelar 
633e6445719SPravin B Shelar 	return mask;
634e6445719SPravin B Shelar }
635e6445719SPravin B Shelar 
636e6445719SPravin B Shelar static bool mask_equal(const struct sw_flow_mask *a,
637e6445719SPravin B Shelar 		       const struct sw_flow_mask *b)
638e6445719SPravin B Shelar {
6397085130bSDaniele Di Proietto 	const u8 *a_ = (const u8 *)&a->key + a->range.start;
6407085130bSDaniele Di Proietto 	const u8 *b_ = (const u8 *)&b->key + b->range.start;
641e6445719SPravin B Shelar 
642e6445719SPravin B Shelar 	return  (a->range.end == b->range.end)
643e6445719SPravin B Shelar 		&& (a->range.start == b->range.start)
644e6445719SPravin B Shelar 		&& (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
645e6445719SPravin B Shelar }
646e6445719SPravin B Shelar 
647618ed0c8SPravin B Shelar static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
648e6445719SPravin B Shelar 					   const struct sw_flow_mask *mask)
649e6445719SPravin B Shelar {
650e6445719SPravin B Shelar 	struct list_head *ml;
651e6445719SPravin B Shelar 
652b637e498SPravin B Shelar 	list_for_each(ml, &tbl->mask_list) {
653e6445719SPravin B Shelar 		struct sw_flow_mask *m;
654e6445719SPravin B Shelar 		m = container_of(ml, struct sw_flow_mask, list);
655e6445719SPravin B Shelar 		if (mask_equal(mask, m))
656e6445719SPravin B Shelar 			return m;
657e6445719SPravin B Shelar 	}
658e6445719SPravin B Shelar 
659e6445719SPravin B Shelar 	return NULL;
660e6445719SPravin B Shelar }
661e6445719SPravin B Shelar 
662d1211908SBen Pfaff /* Add 'mask' into the mask list, if it is not already there. */
663618ed0c8SPravin B Shelar static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
66412eb18f7SThomas Graf 			    const struct sw_flow_mask *new)
665e6445719SPravin B Shelar {
666618ed0c8SPravin B Shelar 	struct sw_flow_mask *mask;
667618ed0c8SPravin B Shelar 	mask = flow_mask_find(tbl, new);
668618ed0c8SPravin B Shelar 	if (!mask) {
669618ed0c8SPravin B Shelar 		/* Allocate a new mask if none exsits. */
670618ed0c8SPravin B Shelar 		mask = mask_alloc();
671618ed0c8SPravin B Shelar 		if (!mask)
672618ed0c8SPravin B Shelar 			return -ENOMEM;
673618ed0c8SPravin B Shelar 		mask->key = new->key;
674618ed0c8SPravin B Shelar 		mask->range = new->range;
675b637e498SPravin B Shelar 		list_add_rcu(&mask->list, &tbl->mask_list);
676e80857ccSAndy Zhou 	} else {
677e80857ccSAndy Zhou 		BUG_ON(!mask->ref_count);
678e80857ccSAndy Zhou 		mask->ref_count++;
679e6445719SPravin B Shelar 	}
680e6445719SPravin B Shelar 
681618ed0c8SPravin B Shelar 	flow->mask = mask;
682618ed0c8SPravin B Shelar 	return 0;
683618ed0c8SPravin B Shelar }
684618ed0c8SPravin B Shelar 
68556c19868SJarno Rajahalme /* Must be called with OVS mutex held. */
686d29ab6f8SJoe Stringer static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
687618ed0c8SPravin B Shelar {
688618ed0c8SPravin B Shelar 	struct table_instance *new_ti = NULL;
689618ed0c8SPravin B Shelar 	struct table_instance *ti;
690618ed0c8SPravin B Shelar 
69174ed7ab9SJoe Stringer 	flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
692618ed0c8SPravin B Shelar 	ti = ovsl_dereference(table->ti);
693618ed0c8SPravin B Shelar 	table_instance_insert(ti, flow);
694618ed0c8SPravin B Shelar 	table->count++;
695618ed0c8SPravin B Shelar 
696618ed0c8SPravin B Shelar 	/* Expand table, if necessary, to make room. */
697618ed0c8SPravin B Shelar 	if (table->count > ti->n_buckets)
69874ed7ab9SJoe Stringer 		new_ti = table_instance_expand(ti, false);
699618ed0c8SPravin B Shelar 	else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
70074ed7ab9SJoe Stringer 		new_ti = table_instance_rehash(ti, ti->n_buckets, false);
701618ed0c8SPravin B Shelar 
702618ed0c8SPravin B Shelar 	if (new_ti) {
703618ed0c8SPravin B Shelar 		rcu_assign_pointer(table->ti, new_ti);
70474ed7ab9SJoe Stringer 		call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
705618ed0c8SPravin B Shelar 		table->last_rehash = jiffies;
706618ed0c8SPravin B Shelar 	}
707d29ab6f8SJoe Stringer }
708d29ab6f8SJoe Stringer 
709d29ab6f8SJoe Stringer /* Must be called with OVS mutex held. */
71074ed7ab9SJoe Stringer static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
71174ed7ab9SJoe Stringer {
71274ed7ab9SJoe Stringer 	struct table_instance *ti;
71374ed7ab9SJoe Stringer 
71474ed7ab9SJoe Stringer 	flow->ufid_table.hash = ufid_hash(&flow->id);
71574ed7ab9SJoe Stringer 	ti = ovsl_dereference(table->ufid_ti);
71674ed7ab9SJoe Stringer 	ufid_table_instance_insert(ti, flow);
71774ed7ab9SJoe Stringer 	table->ufid_count++;
71874ed7ab9SJoe Stringer 
71974ed7ab9SJoe Stringer 	/* Expand table, if necessary, to make room. */
72074ed7ab9SJoe Stringer 	if (table->ufid_count > ti->n_buckets) {
72174ed7ab9SJoe Stringer 		struct table_instance *new_ti;
72274ed7ab9SJoe Stringer 
72374ed7ab9SJoe Stringer 		new_ti = table_instance_expand(ti, true);
72474ed7ab9SJoe Stringer 		if (new_ti) {
72574ed7ab9SJoe Stringer 			rcu_assign_pointer(table->ufid_ti, new_ti);
72674ed7ab9SJoe Stringer 			call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
72774ed7ab9SJoe Stringer 		}
72874ed7ab9SJoe Stringer 	}
72974ed7ab9SJoe Stringer }
73074ed7ab9SJoe Stringer 
73174ed7ab9SJoe Stringer /* Must be called with OVS mutex held. */
732d29ab6f8SJoe Stringer int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
733d29ab6f8SJoe Stringer 			const struct sw_flow_mask *mask)
734d29ab6f8SJoe Stringer {
735d29ab6f8SJoe Stringer 	int err;
736d29ab6f8SJoe Stringer 
737d29ab6f8SJoe Stringer 	err = flow_mask_insert(table, flow, mask);
738d29ab6f8SJoe Stringer 	if (err)
739d29ab6f8SJoe Stringer 		return err;
740d29ab6f8SJoe Stringer 	flow_key_insert(table, flow);
74174ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id))
74274ed7ab9SJoe Stringer 		flow_ufid_insert(table, flow);
743d29ab6f8SJoe Stringer 
744618ed0c8SPravin B Shelar 	return 0;
745618ed0c8SPravin B Shelar }
746618ed0c8SPravin B Shelar 
747e6445719SPravin B Shelar /* Initializes the flow module.
748e6445719SPravin B Shelar  * Returns zero if successful or a negative error code. */
749e6445719SPravin B Shelar int ovs_flow_init(void)
750e6445719SPravin B Shelar {
751e6445719SPravin B Shelar 	BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
752e6445719SPravin B Shelar 	BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
753e6445719SPravin B Shelar 
75463e7959cSJarno Rajahalme 	flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
75563e7959cSJarno Rajahalme 				       + (num_possible_nodes()
75663e7959cSJarno Rajahalme 					  * sizeof(struct flow_stats *)),
75763e7959cSJarno Rajahalme 				       0, 0, NULL);
758e6445719SPravin B Shelar 	if (flow_cache == NULL)
759e6445719SPravin B Shelar 		return -ENOMEM;
760e6445719SPravin B Shelar 
76163e7959cSJarno Rajahalme 	flow_stats_cache
76263e7959cSJarno Rajahalme 		= kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
76363e7959cSJarno Rajahalme 				    0, SLAB_HWCACHE_ALIGN, NULL);
76463e7959cSJarno Rajahalme 	if (flow_stats_cache == NULL) {
76563e7959cSJarno Rajahalme 		kmem_cache_destroy(flow_cache);
76663e7959cSJarno Rajahalme 		flow_cache = NULL;
76763e7959cSJarno Rajahalme 		return -ENOMEM;
76863e7959cSJarno Rajahalme 	}
76963e7959cSJarno Rajahalme 
770e6445719SPravin B Shelar 	return 0;
771e6445719SPravin B Shelar }
772e6445719SPravin B Shelar 
773e6445719SPravin B Shelar /* Uninitializes the flow module. */
774e6445719SPravin B Shelar void ovs_flow_exit(void)
775e6445719SPravin B Shelar {
77663e7959cSJarno Rajahalme 	kmem_cache_destroy(flow_stats_cache);
777e6445719SPravin B Shelar 	kmem_cache_destroy(flow_cache);
778e6445719SPravin B Shelar }
779