xref: /openbmc/linux/net/openvswitch/flow_table.c (revision 63e7959c4b9bd6f791061c460a22d9ee32ae2240)
1e6445719SPravin B Shelar /*
2e6445719SPravin B Shelar  * Copyright (c) 2007-2013 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>
28500f8087SFrancesco Fusco #include <linux/hash.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;
51*63e7959cSJarno 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;
79*63e7959cSJarno Rajahalme 	struct flow_stats *stats;
80*63e7959cSJarno 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*63e7959cSJarno Rajahalme 	flow->stats_last_writer = NUMA_NO_NODE;
89e6445719SPravin B Shelar 
90*63e7959cSJarno Rajahalme 	/* Initialize the default stat node. */
91*63e7959cSJarno Rajahalme 	stats = kmem_cache_alloc_node(flow_stats_cache,
92*63e7959cSJarno Rajahalme 				      GFP_KERNEL | __GFP_ZERO, 0);
93*63e7959cSJarno Rajahalme 	if (!stats)
94e298e505SPravin B Shelar 		goto err;
95e298e505SPravin B Shelar 
96*63e7959cSJarno Rajahalme 	spin_lock_init(&stats->lock);
97e298e505SPravin B Shelar 
98*63e7959cSJarno Rajahalme 	RCU_INIT_POINTER(flow->stats[0], stats);
99*63e7959cSJarno Rajahalme 
100*63e7959cSJarno Rajahalme 	for_each_node(node)
101*63e7959cSJarno Rajahalme 		if (node != 0)
102*63e7959cSJarno Rajahalme 			RCU_INIT_POINTER(flow->stats[node], NULL);
103*63e7959cSJarno 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 
110b637e498SPravin B Shelar int ovs_flow_tbl_count(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 {
140*63e7959cSJarno Rajahalme 	int node;
141*63e7959cSJarno Rajahalme 
142e6445719SPravin B Shelar 	kfree((struct sf_flow_acts __force *)flow->sf_acts);
143*63e7959cSJarno Rajahalme 	for_each_node(node)
144*63e7959cSJarno Rajahalme 		if (flow->stats[node])
145*63e7959cSJarno Rajahalme 			kmem_cache_free(flow_stats_cache,
146*63e7959cSJarno Rajahalme 					(struct flow_stats __force *)flow->stats[node]);
147e6445719SPravin B Shelar 	kmem_cache_free(flow_cache, flow);
148e6445719SPravin B Shelar }
149e6445719SPravin B Shelar 
150e6445719SPravin B Shelar static void rcu_free_flow_callback(struct rcu_head *rcu)
151e6445719SPravin B Shelar {
152e6445719SPravin B Shelar 	struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
153e6445719SPravin B Shelar 
154e6445719SPravin B Shelar 	flow_free(flow);
155e6445719SPravin B Shelar }
156e6445719SPravin B Shelar 
157e80857ccSAndy Zhou void ovs_flow_free(struct sw_flow *flow, bool deferred)
158618ed0c8SPravin B Shelar {
159e80857ccSAndy Zhou 	if (!flow)
160618ed0c8SPravin B Shelar 		return;
161618ed0c8SPravin B Shelar 
162e80857ccSAndy Zhou 	if (flow->mask) {
163e80857ccSAndy Zhou 		struct sw_flow_mask *mask = flow->mask;
164e80857ccSAndy Zhou 
165e4c6d759SPravin B Shelar 		/* ovs-lock is required to protect mask-refcount and
166e4c6d759SPravin B Shelar 		 * mask list.
167e4c6d759SPravin B Shelar 		 */
168e4c6d759SPravin B Shelar 		ASSERT_OVSL();
169618ed0c8SPravin B Shelar 		BUG_ON(!mask->ref_count);
170618ed0c8SPravin B Shelar 		mask->ref_count--;
171618ed0c8SPravin B Shelar 
172618ed0c8SPravin B Shelar 		if (!mask->ref_count) {
173618ed0c8SPravin B Shelar 			list_del_rcu(&mask->list);
174618ed0c8SPravin B Shelar 			if (deferred)
17511d6c461SDaniel Borkmann 				kfree_rcu(mask, rcu);
176618ed0c8SPravin B Shelar 			else
177618ed0c8SPravin B Shelar 				kfree(mask);
178618ed0c8SPravin B Shelar 		}
179618ed0c8SPravin B Shelar 	}
180618ed0c8SPravin B Shelar 
181e6445719SPravin B Shelar 	if (deferred)
182e6445719SPravin B Shelar 		call_rcu(&flow->rcu, rcu_free_flow_callback);
183e6445719SPravin B Shelar 	else
184e6445719SPravin B Shelar 		flow_free(flow);
185e6445719SPravin B Shelar }
186e6445719SPravin B Shelar 
187e6445719SPravin B Shelar static void free_buckets(struct flex_array *buckets)
188e6445719SPravin B Shelar {
189e6445719SPravin B Shelar 	flex_array_free(buckets);
190e6445719SPravin B Shelar }
191e6445719SPravin B Shelar 
192e80857ccSAndy Zhou 
193b637e498SPravin B Shelar static void __table_instance_destroy(struct table_instance *ti)
194e6445719SPravin B Shelar {
195b637e498SPravin B Shelar 	free_buckets(ti->buckets);
196b637e498SPravin B Shelar 	kfree(ti);
197e6445719SPravin B Shelar }
198e6445719SPravin B Shelar 
199b637e498SPravin B Shelar static struct table_instance *table_instance_alloc(int new_size)
200e6445719SPravin B Shelar {
201b637e498SPravin B Shelar 	struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
202e6445719SPravin B Shelar 
203b637e498SPravin B Shelar 	if (!ti)
204e6445719SPravin B Shelar 		return NULL;
205e6445719SPravin B Shelar 
206b637e498SPravin B Shelar 	ti->buckets = alloc_buckets(new_size);
207e6445719SPravin B Shelar 
208b637e498SPravin B Shelar 	if (!ti->buckets) {
209b637e498SPravin B Shelar 		kfree(ti);
210e6445719SPravin B Shelar 		return NULL;
211e6445719SPravin B Shelar 	}
212b637e498SPravin B Shelar 	ti->n_buckets = new_size;
213b637e498SPravin B Shelar 	ti->node_ver = 0;
214b637e498SPravin B Shelar 	ti->keep_flows = false;
215b637e498SPravin B Shelar 	get_random_bytes(&ti->hash_seed, sizeof(u32));
216b637e498SPravin B Shelar 
217b637e498SPravin B Shelar 	return ti;
218b637e498SPravin B Shelar }
219b637e498SPravin B Shelar 
220b637e498SPravin B Shelar int ovs_flow_tbl_init(struct flow_table *table)
221b637e498SPravin B Shelar {
222b637e498SPravin B Shelar 	struct table_instance *ti;
223b637e498SPravin B Shelar 
224b637e498SPravin B Shelar 	ti = table_instance_alloc(TBL_MIN_BUCKETS);
225b637e498SPravin B Shelar 
226b637e498SPravin B Shelar 	if (!ti)
227b637e498SPravin B Shelar 		return -ENOMEM;
228b637e498SPravin B Shelar 
229b637e498SPravin B Shelar 	rcu_assign_pointer(table->ti, ti);
230b637e498SPravin B Shelar 	INIT_LIST_HEAD(&table->mask_list);
231b637e498SPravin B Shelar 	table->last_rehash = jiffies;
232e6445719SPravin B Shelar 	table->count = 0;
233b637e498SPravin B Shelar 	return 0;
234e6445719SPravin B Shelar }
235e6445719SPravin B Shelar 
236e6445719SPravin B Shelar static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
237e6445719SPravin B Shelar {
238b637e498SPravin B Shelar 	struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
239e6445719SPravin B Shelar 
240b637e498SPravin B Shelar 	__table_instance_destroy(ti);
241b637e498SPravin B Shelar }
242b637e498SPravin B Shelar 
243b637e498SPravin B Shelar static void table_instance_destroy(struct table_instance *ti, bool deferred)
244b637e498SPravin B Shelar {
245e80857ccSAndy Zhou 	int i;
246e80857ccSAndy Zhou 
247b637e498SPravin B Shelar 	if (!ti)
248b637e498SPravin B Shelar 		return;
249b637e498SPravin B Shelar 
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;
258e80857ccSAndy Zhou 
259e80857ccSAndy Zhou 		hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
260e80857ccSAndy Zhou 			hlist_del_rcu(&flow->hash_node[ver]);
261e80857ccSAndy Zhou 			ovs_flow_free(flow, deferred);
262e80857ccSAndy Zhou 		}
263e80857ccSAndy Zhou 	}
264e80857ccSAndy Zhou 
265e80857ccSAndy Zhou skip_flows:
266b637e498SPravin B Shelar 	if (deferred)
267b637e498SPravin B Shelar 		call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
268b637e498SPravin B Shelar 	else
269b637e498SPravin B Shelar 		__table_instance_destroy(ti);
270e6445719SPravin B Shelar }
271e6445719SPravin B Shelar 
272e80857ccSAndy Zhou void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
273e6445719SPravin B Shelar {
274b637e498SPravin B Shelar 	struct table_instance *ti = ovsl_dereference(table->ti);
275e6445719SPravin B Shelar 
276e80857ccSAndy Zhou 	table_instance_destroy(ti, deferred);
277e6445719SPravin B Shelar }
278e6445719SPravin B Shelar 
279b637e498SPravin B Shelar struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
280e6445719SPravin B Shelar 				       u32 *bucket, u32 *last)
281e6445719SPravin B Shelar {
282e6445719SPravin B Shelar 	struct sw_flow *flow;
283e6445719SPravin B Shelar 	struct hlist_head *head;
284e6445719SPravin B Shelar 	int ver;
285e6445719SPravin B Shelar 	int i;
286e6445719SPravin B Shelar 
287b637e498SPravin B Shelar 	ver = ti->node_ver;
288b637e498SPravin B Shelar 	while (*bucket < ti->n_buckets) {
289e6445719SPravin B Shelar 		i = 0;
290b637e498SPravin B Shelar 		head = flex_array_get(ti->buckets, *bucket);
291e6445719SPravin B Shelar 		hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
292e6445719SPravin B Shelar 			if (i < *last) {
293e6445719SPravin B Shelar 				i++;
294e6445719SPravin B Shelar 				continue;
295e6445719SPravin B Shelar 			}
296e6445719SPravin B Shelar 			*last = i + 1;
297e6445719SPravin B Shelar 			return flow;
298e6445719SPravin B Shelar 		}
299e6445719SPravin B Shelar 		(*bucket)++;
300e6445719SPravin B Shelar 		*last = 0;
301e6445719SPravin B Shelar 	}
302e6445719SPravin B Shelar 
303e6445719SPravin B Shelar 	return NULL;
304e6445719SPravin B Shelar }
305e6445719SPravin B Shelar 
306b637e498SPravin B Shelar static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
307e6445719SPravin B Shelar {
308b637e498SPravin B Shelar 	hash = jhash_1word(hash, ti->hash_seed);
309b637e498SPravin B Shelar 	return flex_array_get(ti->buckets,
310b637e498SPravin B Shelar 				(hash & (ti->n_buckets - 1)));
311e6445719SPravin B Shelar }
312e6445719SPravin B Shelar 
313b637e498SPravin B Shelar static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow)
314e6445719SPravin B Shelar {
315e6445719SPravin B Shelar 	struct hlist_head *head;
316e6445719SPravin B Shelar 
317b637e498SPravin B Shelar 	head = find_bucket(ti, flow->hash);
318b637e498SPravin B Shelar 	hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head);
319e6445719SPravin B Shelar }
320e6445719SPravin B Shelar 
321b637e498SPravin B Shelar static void flow_table_copy_flows(struct table_instance *old,
322b637e498SPravin B Shelar 				  struct table_instance *new)
323e6445719SPravin B Shelar {
324e6445719SPravin B Shelar 	int old_ver;
325e6445719SPravin B Shelar 	int i;
326e6445719SPravin B Shelar 
327e6445719SPravin B Shelar 	old_ver = old->node_ver;
328e6445719SPravin B Shelar 	new->node_ver = !old_ver;
329e6445719SPravin B Shelar 
330e6445719SPravin B Shelar 	/* Insert in new table. */
331e6445719SPravin B Shelar 	for (i = 0; i < old->n_buckets; i++) {
332e6445719SPravin B Shelar 		struct sw_flow *flow;
333e6445719SPravin B Shelar 		struct hlist_head *head;
334e6445719SPravin B Shelar 
335e6445719SPravin B Shelar 		head = flex_array_get(old->buckets, i);
336e6445719SPravin B Shelar 
337e6445719SPravin B Shelar 		hlist_for_each_entry(flow, head, hash_node[old_ver])
338b637e498SPravin B Shelar 			table_instance_insert(new, flow);
339e6445719SPravin B Shelar 	}
340e6445719SPravin B Shelar 
341e6445719SPravin B Shelar 	old->keep_flows = true;
342e6445719SPravin B Shelar }
343e6445719SPravin B Shelar 
344b637e498SPravin B Shelar static struct table_instance *table_instance_rehash(struct table_instance *ti,
345e6445719SPravin B Shelar 					    int n_buckets)
346e6445719SPravin B Shelar {
347b637e498SPravin B Shelar 	struct table_instance *new_ti;
348e6445719SPravin B Shelar 
349b637e498SPravin B Shelar 	new_ti = table_instance_alloc(n_buckets);
350b637e498SPravin B Shelar 	if (!new_ti)
351618ed0c8SPravin B Shelar 		return NULL;
352e6445719SPravin B Shelar 
353b637e498SPravin B Shelar 	flow_table_copy_flows(ti, new_ti);
354e6445719SPravin B Shelar 
355b637e498SPravin B Shelar 	return new_ti;
356e6445719SPravin B Shelar }
357e6445719SPravin B Shelar 
358b637e498SPravin B Shelar int ovs_flow_tbl_flush(struct flow_table *flow_table)
359e6445719SPravin B Shelar {
360b637e498SPravin B Shelar 	struct table_instance *old_ti;
361b637e498SPravin B Shelar 	struct table_instance *new_ti;
362e6445719SPravin B Shelar 
363b637e498SPravin B Shelar 	old_ti = ovsl_dereference(flow_table->ti);
364b637e498SPravin B Shelar 	new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
365b637e498SPravin B Shelar 	if (!new_ti)
366b637e498SPravin B Shelar 		return -ENOMEM;
367b637e498SPravin B Shelar 
368b637e498SPravin B Shelar 	rcu_assign_pointer(flow_table->ti, new_ti);
369b637e498SPravin B Shelar 	flow_table->last_rehash = jiffies;
370b637e498SPravin B Shelar 	flow_table->count = 0;
371b637e498SPravin B Shelar 
372b637e498SPravin B Shelar 	table_instance_destroy(old_ti, true);
373b637e498SPravin B Shelar 	return 0;
374e6445719SPravin B Shelar }
375e6445719SPravin B Shelar 
376e6445719SPravin B Shelar static u32 flow_hash(const struct sw_flow_key *key, int key_start,
377e6445719SPravin B Shelar 		     int key_end)
378e6445719SPravin B Shelar {
3797085130bSDaniele Di Proietto 	const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
380e6445719SPravin B Shelar 	int hash_u32s = (key_end - key_start) >> 2;
381e6445719SPravin B Shelar 
382e6445719SPravin B Shelar 	/* Make sure number of hash bytes are multiple of u32. */
383e6445719SPravin B Shelar 	BUILD_BUG_ON(sizeof(long) % sizeof(u32));
384e6445719SPravin B Shelar 
385500f8087SFrancesco Fusco 	return arch_fast_hash2(hash_key, hash_u32s, 0);
386e6445719SPravin B Shelar }
387e6445719SPravin B Shelar 
388e6445719SPravin B Shelar static int flow_key_start(const struct sw_flow_key *key)
389e6445719SPravin B Shelar {
390e6445719SPravin B Shelar 	if (key->tun_key.ipv4_dst)
391e6445719SPravin B Shelar 		return 0;
392e6445719SPravin B Shelar 	else
393e6445719SPravin B Shelar 		return rounddown(offsetof(struct sw_flow_key, phy),
394e6445719SPravin B Shelar 					  sizeof(long));
395e6445719SPravin B Shelar }
396e6445719SPravin B Shelar 
397e6445719SPravin B Shelar static bool cmp_key(const struct sw_flow_key *key1,
398e6445719SPravin B Shelar 		    const struct sw_flow_key *key2,
399e6445719SPravin B Shelar 		    int key_start, int key_end)
400e6445719SPravin B Shelar {
4017085130bSDaniele Di Proietto 	const long *cp1 = (const long *)((const u8 *)key1 + key_start);
4027085130bSDaniele Di Proietto 	const long *cp2 = (const long *)((const u8 *)key2 + key_start);
403e6445719SPravin B Shelar 	long diffs = 0;
404e6445719SPravin B Shelar 	int i;
405e6445719SPravin B Shelar 
406e6445719SPravin B Shelar 	for (i = key_start; i < key_end;  i += sizeof(long))
407e6445719SPravin B Shelar 		diffs |= *cp1++ ^ *cp2++;
408e6445719SPravin B Shelar 
409e6445719SPravin B Shelar 	return diffs == 0;
410e6445719SPravin B Shelar }
411e6445719SPravin B Shelar 
412e6445719SPravin B Shelar static bool flow_cmp_masked_key(const struct sw_flow *flow,
413e6445719SPravin B Shelar 				const struct sw_flow_key *key,
414e6445719SPravin B Shelar 				int key_start, int key_end)
415e6445719SPravin B Shelar {
416e6445719SPravin B Shelar 	return cmp_key(&flow->key, key, key_start, key_end);
417e6445719SPravin B Shelar }
418e6445719SPravin B Shelar 
419e6445719SPravin B Shelar bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
420e6445719SPravin B Shelar 			       struct sw_flow_match *match)
421e6445719SPravin B Shelar {
422e6445719SPravin B Shelar 	struct sw_flow_key *key = match->key;
423e6445719SPravin B Shelar 	int key_start = flow_key_start(key);
424e6445719SPravin B Shelar 	int key_end = match->range.end;
425e6445719SPravin B Shelar 
426e6445719SPravin B Shelar 	return cmp_key(&flow->unmasked_key, key, key_start, key_end);
427e6445719SPravin B Shelar }
428e6445719SPravin B Shelar 
429b637e498SPravin B Shelar static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
430e6445719SPravin B Shelar 					  const struct sw_flow_key *unmasked,
431e6445719SPravin B Shelar 					  struct sw_flow_mask *mask)
432e6445719SPravin B Shelar {
433e6445719SPravin B Shelar 	struct sw_flow *flow;
434e6445719SPravin B Shelar 	struct hlist_head *head;
435e6445719SPravin B Shelar 	int key_start = mask->range.start;
436e6445719SPravin B Shelar 	int key_end = mask->range.end;
437e6445719SPravin B Shelar 	u32 hash;
438e6445719SPravin B Shelar 	struct sw_flow_key masked_key;
439e6445719SPravin B Shelar 
440e6445719SPravin B Shelar 	ovs_flow_mask_key(&masked_key, unmasked, mask);
441e6445719SPravin B Shelar 	hash = flow_hash(&masked_key, key_start, key_end);
442b637e498SPravin B Shelar 	head = find_bucket(ti, hash);
443b637e498SPravin B Shelar 	hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) {
4448ddd0946SPravin B Shelar 		if (flow->mask == mask && flow->hash == hash &&
445e6445719SPravin B Shelar 		    flow_cmp_masked_key(flow, &masked_key,
446e6445719SPravin B Shelar 					  key_start, key_end))
447e6445719SPravin B Shelar 			return flow;
448e6445719SPravin B Shelar 	}
449e6445719SPravin B Shelar 	return NULL;
450e6445719SPravin B Shelar }
451e6445719SPravin B Shelar 
4525bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
4531bd7116fSAndy Zhou 				    const struct sw_flow_key *key,
4541bd7116fSAndy Zhou 				    u32 *n_mask_hit)
455e6445719SPravin B Shelar {
456663efa36SJesse Gross 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
457e6445719SPravin B Shelar 	struct sw_flow_mask *mask;
458b637e498SPravin B Shelar 	struct sw_flow *flow;
459e6445719SPravin B Shelar 
4601bd7116fSAndy Zhou 	*n_mask_hit = 0;
461b637e498SPravin B Shelar 	list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
4621bd7116fSAndy Zhou 		(*n_mask_hit)++;
463b637e498SPravin B Shelar 		flow = masked_flow_lookup(ti, key, mask);
464e6445719SPravin B Shelar 		if (flow)  /* Found */
465b637e498SPravin B Shelar 			return flow;
466b637e498SPravin B Shelar 	}
467b637e498SPravin B Shelar 	return NULL;
468e6445719SPravin B Shelar }
469e6445719SPravin B Shelar 
4705bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
4715bb50632SAndy Zhou 				    const struct sw_flow_key *key)
4725bb50632SAndy Zhou {
4735bb50632SAndy Zhou 	u32 __always_unused n_mask_hit;
4745bb50632SAndy Zhou 
4755bb50632SAndy Zhou 	return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
4765bb50632SAndy Zhou }
4775bb50632SAndy Zhou 
4781bd7116fSAndy Zhou int ovs_flow_tbl_num_masks(const struct flow_table *table)
4791bd7116fSAndy Zhou {
4801bd7116fSAndy Zhou 	struct sw_flow_mask *mask;
4811bd7116fSAndy Zhou 	int num = 0;
4821bd7116fSAndy Zhou 
4831bd7116fSAndy Zhou 	list_for_each_entry(mask, &table->mask_list, list)
4841bd7116fSAndy Zhou 		num++;
4851bd7116fSAndy Zhou 
4861bd7116fSAndy Zhou 	return num;
4871bd7116fSAndy Zhou }
4881bd7116fSAndy Zhou 
489b637e498SPravin B Shelar static struct table_instance *table_instance_expand(struct table_instance *ti)
490b637e498SPravin B Shelar {
491b637e498SPravin B Shelar 	return table_instance_rehash(ti, ti->n_buckets * 2);
492e6445719SPravin B Shelar }
493e6445719SPravin B Shelar 
494e6445719SPravin B Shelar void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
495e6445719SPravin B Shelar {
496b637e498SPravin B Shelar 	struct table_instance *ti = ovsl_dereference(table->ti);
497b637e498SPravin B Shelar 
498e6445719SPravin B Shelar 	BUG_ON(table->count == 0);
499b637e498SPravin B Shelar 	hlist_del_rcu(&flow->hash_node[ti->node_ver]);
500e6445719SPravin B Shelar 	table->count--;
501e6445719SPravin B Shelar }
502e6445719SPravin B Shelar 
503618ed0c8SPravin B Shelar static struct sw_flow_mask *mask_alloc(void)
504e6445719SPravin B Shelar {
505e6445719SPravin B Shelar 	struct sw_flow_mask *mask;
506e6445719SPravin B Shelar 
507e6445719SPravin B Shelar 	mask = kmalloc(sizeof(*mask), GFP_KERNEL);
508e6445719SPravin B Shelar 	if (mask)
509e80857ccSAndy Zhou 		mask->ref_count = 1;
510e6445719SPravin B Shelar 
511e6445719SPravin B Shelar 	return mask;
512e6445719SPravin B Shelar }
513e6445719SPravin B Shelar 
514e6445719SPravin B Shelar static bool mask_equal(const struct sw_flow_mask *a,
515e6445719SPravin B Shelar 		       const struct sw_flow_mask *b)
516e6445719SPravin B Shelar {
5177085130bSDaniele Di Proietto 	const u8 *a_ = (const u8 *)&a->key + a->range.start;
5187085130bSDaniele Di Proietto 	const u8 *b_ = (const u8 *)&b->key + b->range.start;
519e6445719SPravin B Shelar 
520e6445719SPravin B Shelar 	return  (a->range.end == b->range.end)
521e6445719SPravin B Shelar 		&& (a->range.start == b->range.start)
522e6445719SPravin B Shelar 		&& (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
523e6445719SPravin B Shelar }
524e6445719SPravin B Shelar 
525618ed0c8SPravin B Shelar static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
526e6445719SPravin B Shelar 					   const struct sw_flow_mask *mask)
527e6445719SPravin B Shelar {
528e6445719SPravin B Shelar 	struct list_head *ml;
529e6445719SPravin B Shelar 
530b637e498SPravin B Shelar 	list_for_each(ml, &tbl->mask_list) {
531e6445719SPravin B Shelar 		struct sw_flow_mask *m;
532e6445719SPravin B Shelar 		m = container_of(ml, struct sw_flow_mask, list);
533e6445719SPravin B Shelar 		if (mask_equal(mask, m))
534e6445719SPravin B Shelar 			return m;
535e6445719SPravin B Shelar 	}
536e6445719SPravin B Shelar 
537e6445719SPravin B Shelar 	return NULL;
538e6445719SPravin B Shelar }
539e6445719SPravin B Shelar 
540d1211908SBen Pfaff /* Add 'mask' into the mask list, if it is not already there. */
541618ed0c8SPravin B Shelar static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
542618ed0c8SPravin B Shelar 			    struct sw_flow_mask *new)
543e6445719SPravin B Shelar {
544618ed0c8SPravin B Shelar 	struct sw_flow_mask *mask;
545618ed0c8SPravin B Shelar 	mask = flow_mask_find(tbl, new);
546618ed0c8SPravin B Shelar 	if (!mask) {
547618ed0c8SPravin B Shelar 		/* Allocate a new mask if none exsits. */
548618ed0c8SPravin B Shelar 		mask = mask_alloc();
549618ed0c8SPravin B Shelar 		if (!mask)
550618ed0c8SPravin B Shelar 			return -ENOMEM;
551618ed0c8SPravin B Shelar 		mask->key = new->key;
552618ed0c8SPravin B Shelar 		mask->range = new->range;
553b637e498SPravin B Shelar 		list_add_rcu(&mask->list, &tbl->mask_list);
554e80857ccSAndy Zhou 	} else {
555e80857ccSAndy Zhou 		BUG_ON(!mask->ref_count);
556e80857ccSAndy Zhou 		mask->ref_count++;
557e6445719SPravin B Shelar 	}
558e6445719SPravin B Shelar 
559618ed0c8SPravin B Shelar 	flow->mask = mask;
560618ed0c8SPravin B Shelar 	return 0;
561618ed0c8SPravin B Shelar }
562618ed0c8SPravin B Shelar 
563618ed0c8SPravin B Shelar int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
564618ed0c8SPravin B Shelar 			struct sw_flow_mask *mask)
565618ed0c8SPravin B Shelar {
566618ed0c8SPravin B Shelar 	struct table_instance *new_ti = NULL;
567618ed0c8SPravin B Shelar 	struct table_instance *ti;
568618ed0c8SPravin B Shelar 	int err;
569618ed0c8SPravin B Shelar 
570618ed0c8SPravin B Shelar 	err = flow_mask_insert(table, flow, mask);
571618ed0c8SPravin B Shelar 	if (err)
572618ed0c8SPravin B Shelar 		return err;
573618ed0c8SPravin B Shelar 
574618ed0c8SPravin B Shelar 	flow->hash = flow_hash(&flow->key, flow->mask->range.start,
575618ed0c8SPravin B Shelar 			flow->mask->range.end);
576618ed0c8SPravin B Shelar 	ti = ovsl_dereference(table->ti);
577618ed0c8SPravin B Shelar 	table_instance_insert(ti, flow);
578618ed0c8SPravin B Shelar 	table->count++;
579618ed0c8SPravin B Shelar 
580618ed0c8SPravin B Shelar 	/* Expand table, if necessary, to make room. */
581618ed0c8SPravin B Shelar 	if (table->count > ti->n_buckets)
582618ed0c8SPravin B Shelar 		new_ti = table_instance_expand(ti);
583618ed0c8SPravin B Shelar 	else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
584618ed0c8SPravin B Shelar 		new_ti = table_instance_rehash(ti, ti->n_buckets);
585618ed0c8SPravin B Shelar 
586618ed0c8SPravin B Shelar 	if (new_ti) {
587618ed0c8SPravin B Shelar 		rcu_assign_pointer(table->ti, new_ti);
588618ed0c8SPravin B Shelar 		table_instance_destroy(ti, true);
589618ed0c8SPravin B Shelar 		table->last_rehash = jiffies;
590618ed0c8SPravin B Shelar 	}
591618ed0c8SPravin B Shelar 	return 0;
592618ed0c8SPravin B Shelar }
593618ed0c8SPravin B Shelar 
594e6445719SPravin B Shelar /* Initializes the flow module.
595e6445719SPravin B Shelar  * Returns zero if successful or a negative error code. */
596e6445719SPravin B Shelar int ovs_flow_init(void)
597e6445719SPravin B Shelar {
598e6445719SPravin B Shelar 	BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
599e6445719SPravin B Shelar 	BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
600e6445719SPravin B Shelar 
601*63e7959cSJarno Rajahalme 	flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
602*63e7959cSJarno Rajahalme 				       + (num_possible_nodes()
603*63e7959cSJarno Rajahalme 					  * sizeof(struct flow_stats *)),
604*63e7959cSJarno Rajahalme 				       0, 0, NULL);
605e6445719SPravin B Shelar 	if (flow_cache == NULL)
606e6445719SPravin B Shelar 		return -ENOMEM;
607e6445719SPravin B Shelar 
608*63e7959cSJarno Rajahalme 	flow_stats_cache
609*63e7959cSJarno Rajahalme 		= kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
610*63e7959cSJarno Rajahalme 				    0, SLAB_HWCACHE_ALIGN, NULL);
611*63e7959cSJarno Rajahalme 	if (flow_stats_cache == NULL) {
612*63e7959cSJarno Rajahalme 		kmem_cache_destroy(flow_cache);
613*63e7959cSJarno Rajahalme 		flow_cache = NULL;
614*63e7959cSJarno Rajahalme 		return -ENOMEM;
615*63e7959cSJarno Rajahalme 	}
616*63e7959cSJarno Rajahalme 
617e6445719SPravin B Shelar 	return 0;
618e6445719SPravin B Shelar }
619e6445719SPravin B Shelar 
620e6445719SPravin B Shelar /* Uninitializes the flow module. */
621e6445719SPravin B Shelar void ovs_flow_exit(void)
622e6445719SPravin B Shelar {
623*63e7959cSJarno Rajahalme 	kmem_cache_destroy(flow_stats_cache);
624e6445719SPravin B Shelar 	kmem_cache_destroy(flow_cache);
625e6445719SPravin B Shelar }
626