xref: /openbmc/linux/net/openvswitch/flow_table.c (revision a7f35e78e701744368d4ac38bdb61a86bfac2162)
1c9422999SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e6445719SPravin B Shelar /*
39b996e54SPravin B Shelar  * Copyright (c) 2007-2014 Nicira, Inc.
4e6445719SPravin B Shelar  */
5e6445719SPravin B Shelar 
6e6445719SPravin B Shelar #include "flow.h"
7e6445719SPravin B Shelar #include "datapath.h"
834ae932aSThomas Graf #include "flow_netlink.h"
9e6445719SPravin B Shelar #include <linux/uaccess.h>
10e6445719SPravin B Shelar #include <linux/netdevice.h>
11e6445719SPravin B Shelar #include <linux/etherdevice.h>
12e6445719SPravin B Shelar #include <linux/if_ether.h>
13e6445719SPravin B Shelar #include <linux/if_vlan.h>
14e6445719SPravin B Shelar #include <net/llc_pdu.h>
15e6445719SPravin B Shelar #include <linux/kernel.h>
1687545899SDaniel Borkmann #include <linux/jhash.h>
17e6445719SPravin B Shelar #include <linux/jiffies.h>
18e6445719SPravin B Shelar #include <linux/llc.h>
19e6445719SPravin B Shelar #include <linux/module.h>
20e6445719SPravin B Shelar #include <linux/in.h>
21e6445719SPravin B Shelar #include <linux/rcupdate.h>
22db74a333SThadeu Lima de Souza Cascardo #include <linux/cpumask.h>
23e6445719SPravin B Shelar #include <linux/if_arp.h>
24e6445719SPravin B Shelar #include <linux/ip.h>
25e6445719SPravin B Shelar #include <linux/ipv6.h>
26e6445719SPravin B Shelar #include <linux/sctp.h>
27e6445719SPravin B Shelar #include <linux/tcp.h>
28e6445719SPravin B Shelar #include <linux/udp.h>
29e6445719SPravin B Shelar #include <linux/icmp.h>
30e6445719SPravin B Shelar #include <linux/icmpv6.h>
31e6445719SPravin B Shelar #include <linux/rculist.h>
32e6445719SPravin B Shelar #include <net/ip.h>
33e6445719SPravin B Shelar #include <net/ipv6.h>
34e6445719SPravin B Shelar #include <net/ndisc.h>
35e6445719SPravin B Shelar 
36b637e498SPravin B Shelar #define TBL_MIN_BUCKETS		1024
374bc63b1bSTonghao Zhang #define MASK_ARRAY_SIZE_MIN	16
38b637e498SPravin B Shelar #define REHASH_INTERVAL		(10 * 60 * HZ)
39b637e498SPravin B Shelar 
4004b7d136STonghao Zhang #define MC_HASH_SHIFT		8
4104b7d136STonghao Zhang #define MC_HASH_ENTRIES		(1u << MC_HASH_SHIFT)
4204b7d136STonghao Zhang #define MC_HASH_SEGS		((sizeof(uint32_t) * 8) / MC_HASH_SHIFT)
4304b7d136STonghao Zhang 
44e6445719SPravin B Shelar static struct kmem_cache *flow_cache;
4563e7959cSJarno Rajahalme struct kmem_cache *flow_stats_cache __read_mostly;
46e6445719SPravin B Shelar 
47e6445719SPravin B Shelar static u16 range_n_bytes(const struct sw_flow_key_range *range)
48e6445719SPravin B Shelar {
49e6445719SPravin B Shelar 	return range->end - range->start;
50e6445719SPravin B Shelar }
51e6445719SPravin B Shelar 
52e6445719SPravin B Shelar void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
53ae5f2fb1SJesse Gross 		       bool full, const struct sw_flow_mask *mask)
54e6445719SPravin B Shelar {
55ae5f2fb1SJesse Gross 	int start = full ? 0 : mask->range.start;
56ae5f2fb1SJesse Gross 	int len = full ? sizeof *dst : range_n_bytes(&mask->range);
57ae5f2fb1SJesse Gross 	const long *m = (const long *)((const u8 *)&mask->key + start);
58ae5f2fb1SJesse Gross 	const long *s = (const long *)((const u8 *)src + start);
59ae5f2fb1SJesse Gross 	long *d = (long *)((u8 *)dst + start);
60e6445719SPravin B Shelar 	int i;
61e6445719SPravin B Shelar 
62ae5f2fb1SJesse Gross 	/* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
63ae5f2fb1SJesse Gross 	 * if 'full' is false the memory outside of the 'mask->range' is left
64ae5f2fb1SJesse Gross 	 * uninitialized. This can be used as an optimization when further
65ae5f2fb1SJesse Gross 	 * operations on 'dst' only use contents within 'mask->range'.
66e6445719SPravin B Shelar 	 */
67ae5f2fb1SJesse Gross 	for (i = 0; i < len; i += sizeof(long))
68e6445719SPravin B Shelar 		*d++ = *s++ & *m++;
69e6445719SPravin B Shelar }
70e6445719SPravin B Shelar 
7123dabf88SJarno Rajahalme struct sw_flow *ovs_flow_alloc(void)
72e6445719SPravin B Shelar {
73e6445719SPravin B Shelar 	struct sw_flow *flow;
74aef833c5SPablo Neira Ayuso 	struct sw_flow_stats *stats;
75e6445719SPravin B Shelar 
76db74a333SThadeu Lima de Souza Cascardo 	flow = kmem_cache_zalloc(flow_cache, GFP_KERNEL);
77e6445719SPravin B Shelar 	if (!flow)
78e6445719SPravin B Shelar 		return ERR_PTR(-ENOMEM);
79e6445719SPravin B Shelar 
80db74a333SThadeu Lima de Souza Cascardo 	flow->stats_last_writer = -1;
81e6445719SPravin B Shelar 
8263e7959cSJarno Rajahalme 	/* Initialize the default stat node. */
8363e7959cSJarno Rajahalme 	stats = kmem_cache_alloc_node(flow_stats_cache,
84598c12d0SKonstantin Khlebnikov 				      GFP_KERNEL | __GFP_ZERO,
85598c12d0SKonstantin Khlebnikov 				      node_online(0) ? 0 : NUMA_NO_NODE);
8663e7959cSJarno Rajahalme 	if (!stats)
87e298e505SPravin B Shelar 		goto err;
88e298e505SPravin B Shelar 
8963e7959cSJarno Rajahalme 	spin_lock_init(&stats->lock);
90e298e505SPravin B Shelar 
9163e7959cSJarno Rajahalme 	RCU_INIT_POINTER(flow->stats[0], stats);
9263e7959cSJarno Rajahalme 
93c4b2bf6bSTonghao Zhang 	cpumask_set_cpu(0, &flow->cpu_used_mask);
94c4b2bf6bSTonghao Zhang 
95e6445719SPravin B Shelar 	return flow;
96e298e505SPravin B Shelar err:
97ece37c87SWei Yongjun 	kmem_cache_free(flow_cache, flow);
98e298e505SPravin B Shelar 	return ERR_PTR(-ENOMEM);
99e6445719SPravin B Shelar }
100e6445719SPravin B Shelar 
10112eb18f7SThomas Graf int ovs_flow_tbl_count(const struct flow_table *table)
102b637e498SPravin B Shelar {
103b637e498SPravin B Shelar 	return table->count;
104b637e498SPravin B Shelar }
105b637e498SPravin B Shelar 
106e6445719SPravin B Shelar static void flow_free(struct sw_flow *flow)
107e6445719SPravin B Shelar {
108db74a333SThadeu Lima de Souza Cascardo 	int cpu;
10963e7959cSJarno Rajahalme 
11074ed7ab9SJoe Stringer 	if (ovs_identifier_is_key(&flow->id))
11174ed7ab9SJoe Stringer 		kfree(flow->id.unmasked_key);
11234ae932aSThomas Graf 	if (flow->sf_acts)
11334ae932aSThomas Graf 		ovs_nla_free_flow_actions((struct sw_flow_actions __force *)flow->sf_acts);
114db74a333SThadeu Lima de Souza Cascardo 	/* We open code this to make sure cpu 0 is always considered */
115c4b2bf6bSTonghao Zhang 	for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, &flow->cpu_used_mask))
116db74a333SThadeu Lima de Souza Cascardo 		if (flow->stats[cpu])
11763e7959cSJarno Rajahalme 			kmem_cache_free(flow_stats_cache,
118aef833c5SPablo Neira Ayuso 					(struct sw_flow_stats __force *)flow->stats[cpu]);
119e6445719SPravin B Shelar 	kmem_cache_free(flow_cache, flow);
120e6445719SPravin B Shelar }
121e6445719SPravin B Shelar 
122e6445719SPravin B Shelar static void rcu_free_flow_callback(struct rcu_head *rcu)
123e6445719SPravin B Shelar {
124e6445719SPravin B Shelar 	struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
125e6445719SPravin B Shelar 
126e6445719SPravin B Shelar 	flow_free(flow);
127e6445719SPravin B Shelar }
128e6445719SPravin B Shelar 
129e80857ccSAndy Zhou void ovs_flow_free(struct sw_flow *flow, bool deferred)
130618ed0c8SPravin B Shelar {
131e80857ccSAndy Zhou 	if (!flow)
132618ed0c8SPravin B Shelar 		return;
133618ed0c8SPravin B Shelar 
134e6445719SPravin B Shelar 	if (deferred)
135e6445719SPravin B Shelar 		call_rcu(&flow->rcu, rcu_free_flow_callback);
136e6445719SPravin B Shelar 	else
137e6445719SPravin B Shelar 		flow_free(flow);
138e6445719SPravin B Shelar }
139e6445719SPravin B Shelar 
140b637e498SPravin B Shelar static void __table_instance_destroy(struct table_instance *ti)
141e6445719SPravin B Shelar {
142ee9c5e67SKent Overstreet 	kvfree(ti->buckets);
143b637e498SPravin B Shelar 	kfree(ti);
144e6445719SPravin B Shelar }
145e6445719SPravin B Shelar 
146b637e498SPravin B Shelar static struct table_instance *table_instance_alloc(int new_size)
147e6445719SPravin B Shelar {
148b637e498SPravin B Shelar 	struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
149ee9c5e67SKent Overstreet 	int i;
150e6445719SPravin B Shelar 
151b637e498SPravin B Shelar 	if (!ti)
152e6445719SPravin B Shelar 		return NULL;
153e6445719SPravin B Shelar 
154ee9c5e67SKent Overstreet 	ti->buckets = kvmalloc_array(new_size, sizeof(struct hlist_head),
155ee9c5e67SKent Overstreet 				     GFP_KERNEL);
156b637e498SPravin B Shelar 	if (!ti->buckets) {
157b637e498SPravin B Shelar 		kfree(ti);
158e6445719SPravin B Shelar 		return NULL;
159e6445719SPravin B Shelar 	}
160ee9c5e67SKent Overstreet 
161ee9c5e67SKent Overstreet 	for (i = 0; i < new_size; i++)
162ee9c5e67SKent Overstreet 		INIT_HLIST_HEAD(&ti->buckets[i]);
163ee9c5e67SKent Overstreet 
164b637e498SPravin B Shelar 	ti->n_buckets = new_size;
165b637e498SPravin B Shelar 	ti->node_ver = 0;
166b637e498SPravin B Shelar 	ti->keep_flows = false;
167b637e498SPravin B Shelar 	get_random_bytes(&ti->hash_seed, sizeof(u32));
168b637e498SPravin B Shelar 
169b637e498SPravin B Shelar 	return ti;
170b637e498SPravin B Shelar }
171b637e498SPravin B Shelar 
1724bc63b1bSTonghao Zhang static struct mask_array *tbl_mask_array_alloc(int size)
1734bc63b1bSTonghao Zhang {
1744bc63b1bSTonghao Zhang 	struct mask_array *new;
1754bc63b1bSTonghao Zhang 
1764bc63b1bSTonghao Zhang 	size = max(MASK_ARRAY_SIZE_MIN, size);
1774bc63b1bSTonghao Zhang 	new = kzalloc(sizeof(struct mask_array) +
1784bc63b1bSTonghao Zhang 		      sizeof(struct sw_flow_mask *) * size, GFP_KERNEL);
1794bc63b1bSTonghao Zhang 	if (!new)
1804bc63b1bSTonghao Zhang 		return NULL;
1814bc63b1bSTonghao Zhang 
1824bc63b1bSTonghao Zhang 	new->count = 0;
1834bc63b1bSTonghao Zhang 	new->max = size;
1844bc63b1bSTonghao Zhang 
1854bc63b1bSTonghao Zhang 	return new;
1864bc63b1bSTonghao Zhang }
1874bc63b1bSTonghao Zhang 
1884bc63b1bSTonghao Zhang static int tbl_mask_array_realloc(struct flow_table *tbl, int size)
1894bc63b1bSTonghao Zhang {
1904bc63b1bSTonghao Zhang 	struct mask_array *old;
1914bc63b1bSTonghao Zhang 	struct mask_array *new;
1924bc63b1bSTonghao Zhang 
1934bc63b1bSTonghao Zhang 	new = tbl_mask_array_alloc(size);
1944bc63b1bSTonghao Zhang 	if (!new)
1954bc63b1bSTonghao Zhang 		return -ENOMEM;
1964bc63b1bSTonghao Zhang 
1974bc63b1bSTonghao Zhang 	old = ovsl_dereference(tbl->mask_array);
1984bc63b1bSTonghao Zhang 	if (old) {
1994bc63b1bSTonghao Zhang 		int i;
2004bc63b1bSTonghao Zhang 
2014bc63b1bSTonghao Zhang 		for (i = 0; i < old->max; i++) {
2024bc63b1bSTonghao Zhang 			if (ovsl_dereference(old->masks[i]))
2034bc63b1bSTonghao Zhang 				new->masks[new->count++] = old->masks[i];
2044bc63b1bSTonghao Zhang 		}
2054bc63b1bSTonghao Zhang 	}
2064bc63b1bSTonghao Zhang 
2074bc63b1bSTonghao Zhang 	rcu_assign_pointer(tbl->mask_array, new);
2084bc63b1bSTonghao Zhang 	kfree_rcu(old, rcu);
2094bc63b1bSTonghao Zhang 
2104bc63b1bSTonghao Zhang 	return 0;
2114bc63b1bSTonghao Zhang }
2124bc63b1bSTonghao Zhang 
213b637e498SPravin B Shelar int ovs_flow_tbl_init(struct flow_table *table)
214b637e498SPravin B Shelar {
21574ed7ab9SJoe Stringer 	struct table_instance *ti, *ufid_ti;
2164bc63b1bSTonghao Zhang 	struct mask_array *ma;
217b637e498SPravin B Shelar 
21804b7d136STonghao Zhang 	table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) *
21904b7d136STonghao Zhang 					   MC_HASH_ENTRIES,
22004b7d136STonghao Zhang 					   __alignof__(struct mask_cache_entry));
22104b7d136STonghao Zhang 	if (!table->mask_cache)
222b637e498SPravin B Shelar 		return -ENOMEM;
223b637e498SPravin B Shelar 
2244bc63b1bSTonghao Zhang 	ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
2254bc63b1bSTonghao Zhang 	if (!ma)
2264bc63b1bSTonghao Zhang 		goto free_mask_cache;
2274bc63b1bSTonghao Zhang 
22804b7d136STonghao Zhang 	ti = table_instance_alloc(TBL_MIN_BUCKETS);
22904b7d136STonghao Zhang 	if (!ti)
2304bc63b1bSTonghao Zhang 		goto free_mask_array;
23104b7d136STonghao Zhang 
23274ed7ab9SJoe Stringer 	ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
23374ed7ab9SJoe Stringer 	if (!ufid_ti)
23474ed7ab9SJoe Stringer 		goto free_ti;
23574ed7ab9SJoe Stringer 
236b637e498SPravin B Shelar 	rcu_assign_pointer(table->ti, ti);
23774ed7ab9SJoe Stringer 	rcu_assign_pointer(table->ufid_ti, ufid_ti);
2384bc63b1bSTonghao Zhang 	rcu_assign_pointer(table->mask_array, ma);
239b637e498SPravin B Shelar 	table->last_rehash = jiffies;
240e6445719SPravin B Shelar 	table->count = 0;
24174ed7ab9SJoe Stringer 	table->ufid_count = 0;
242b637e498SPravin B Shelar 	return 0;
24374ed7ab9SJoe Stringer 
24474ed7ab9SJoe Stringer free_ti:
24574ed7ab9SJoe Stringer 	__table_instance_destroy(ti);
2464bc63b1bSTonghao Zhang free_mask_array:
2474bc63b1bSTonghao Zhang 	kfree(ma);
24804b7d136STonghao Zhang free_mask_cache:
24904b7d136STonghao Zhang 	free_percpu(table->mask_cache);
25074ed7ab9SJoe Stringer 	return -ENOMEM;
251e6445719SPravin B Shelar }
252e6445719SPravin B Shelar 
253e6445719SPravin B Shelar static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
254e6445719SPravin B Shelar {
255b637e498SPravin B Shelar 	struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
256e6445719SPravin B Shelar 
257b637e498SPravin B Shelar 	__table_instance_destroy(ti);
258b637e498SPravin B Shelar }
259b637e498SPravin B Shelar 
26074ed7ab9SJoe Stringer static void table_instance_destroy(struct table_instance *ti,
26174ed7ab9SJoe Stringer 				   struct table_instance *ufid_ti,
26274ed7ab9SJoe Stringer 				   bool deferred)
263b637e498SPravin B Shelar {
264e80857ccSAndy Zhou 	int i;
265e80857ccSAndy Zhou 
266b637e498SPravin B Shelar 	if (!ti)
267b637e498SPravin B Shelar 		return;
268b637e498SPravin B Shelar 
26974ed7ab9SJoe Stringer 	BUG_ON(!ufid_ti);
270e80857ccSAndy Zhou 	if (ti->keep_flows)
271e80857ccSAndy Zhou 		goto skip_flows;
272e80857ccSAndy Zhou 
273e80857ccSAndy Zhou 	for (i = 0; i < ti->n_buckets; i++) {
274e80857ccSAndy Zhou 		struct sw_flow *flow;
275ee9c5e67SKent Overstreet 		struct hlist_head *head = &ti->buckets[i];
276e80857ccSAndy Zhou 		struct hlist_node *n;
277e80857ccSAndy Zhou 		int ver = ti->node_ver;
27874ed7ab9SJoe Stringer 		int ufid_ver = ufid_ti->node_ver;
279e80857ccSAndy Zhou 
28074ed7ab9SJoe Stringer 		hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
28174ed7ab9SJoe Stringer 			hlist_del_rcu(&flow->flow_table.node[ver]);
28274ed7ab9SJoe Stringer 			if (ovs_identifier_is_ufid(&flow->id))
28374ed7ab9SJoe Stringer 				hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
284e80857ccSAndy Zhou 			ovs_flow_free(flow, deferred);
285e80857ccSAndy Zhou 		}
286e80857ccSAndy Zhou 	}
287e80857ccSAndy Zhou 
288e80857ccSAndy Zhou skip_flows:
28974ed7ab9SJoe Stringer 	if (deferred) {
290b637e498SPravin B Shelar 		call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
29174ed7ab9SJoe Stringer 		call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
29274ed7ab9SJoe Stringer 	} else {
293b637e498SPravin B Shelar 		__table_instance_destroy(ti);
29474ed7ab9SJoe Stringer 		__table_instance_destroy(ufid_ti);
29574ed7ab9SJoe Stringer 	}
296e6445719SPravin B Shelar }
297e6445719SPravin B Shelar 
2989b996e54SPravin B Shelar /* No need for locking this function is called from RCU callback or
2999b996e54SPravin B Shelar  * error path.
3009b996e54SPravin B Shelar  */
3019b996e54SPravin B Shelar void ovs_flow_tbl_destroy(struct flow_table *table)
302e6445719SPravin B Shelar {
3039b996e54SPravin B Shelar 	struct table_instance *ti = rcu_dereference_raw(table->ti);
30474ed7ab9SJoe Stringer 	struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
305e6445719SPravin B Shelar 
30604b7d136STonghao Zhang 	free_percpu(table->mask_cache);
3074bc63b1bSTonghao Zhang 	kfree_rcu(rcu_dereference_raw(table->mask_array), rcu);
30874ed7ab9SJoe Stringer 	table_instance_destroy(ti, ufid_ti, false);
309e6445719SPravin B Shelar }
310e6445719SPravin B Shelar 
311b637e498SPravin B Shelar struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
312e6445719SPravin B Shelar 				       u32 *bucket, u32 *last)
313e6445719SPravin B Shelar {
314e6445719SPravin B Shelar 	struct sw_flow *flow;
315e6445719SPravin B Shelar 	struct hlist_head *head;
316e6445719SPravin B Shelar 	int ver;
317e6445719SPravin B Shelar 	int i;
318e6445719SPravin B Shelar 
319b637e498SPravin B Shelar 	ver = ti->node_ver;
320b637e498SPravin B Shelar 	while (*bucket < ti->n_buckets) {
321e6445719SPravin B Shelar 		i = 0;
322ee9c5e67SKent Overstreet 		head = &ti->buckets[*bucket];
32374ed7ab9SJoe Stringer 		hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
324e6445719SPravin B Shelar 			if (i < *last) {
325e6445719SPravin B Shelar 				i++;
326e6445719SPravin B Shelar 				continue;
327e6445719SPravin B Shelar 			}
328e6445719SPravin B Shelar 			*last = i + 1;
329e6445719SPravin B Shelar 			return flow;
330e6445719SPravin B Shelar 		}
331e6445719SPravin B Shelar 		(*bucket)++;
332e6445719SPravin B Shelar 		*last = 0;
333e6445719SPravin B Shelar 	}
334e6445719SPravin B Shelar 
335e6445719SPravin B Shelar 	return NULL;
336e6445719SPravin B Shelar }
337e6445719SPravin B Shelar 
338b637e498SPravin B Shelar static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
339e6445719SPravin B Shelar {
340b637e498SPravin B Shelar 	hash = jhash_1word(hash, ti->hash_seed);
341ee9c5e67SKent Overstreet 	return &ti->buckets[hash & (ti->n_buckets - 1)];
342e6445719SPravin B Shelar }
343e6445719SPravin B Shelar 
34474ed7ab9SJoe Stringer static void table_instance_insert(struct table_instance *ti,
34574ed7ab9SJoe Stringer 				  struct sw_flow *flow)
346e6445719SPravin B Shelar {
347e6445719SPravin B Shelar 	struct hlist_head *head;
348e6445719SPravin B Shelar 
34974ed7ab9SJoe Stringer 	head = find_bucket(ti, flow->flow_table.hash);
35074ed7ab9SJoe Stringer 	hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
35174ed7ab9SJoe Stringer }
35274ed7ab9SJoe Stringer 
35374ed7ab9SJoe Stringer static void ufid_table_instance_insert(struct table_instance *ti,
35474ed7ab9SJoe Stringer 				       struct sw_flow *flow)
35574ed7ab9SJoe Stringer {
35674ed7ab9SJoe Stringer 	struct hlist_head *head;
35774ed7ab9SJoe Stringer 
35874ed7ab9SJoe Stringer 	head = find_bucket(ti, flow->ufid_table.hash);
35974ed7ab9SJoe Stringer 	hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
360e6445719SPravin B Shelar }
361e6445719SPravin B Shelar 
362b637e498SPravin B Shelar static void flow_table_copy_flows(struct table_instance *old,
36374ed7ab9SJoe Stringer 				  struct table_instance *new, bool ufid)
364e6445719SPravin B Shelar {
365e6445719SPravin B Shelar 	int old_ver;
366e6445719SPravin B Shelar 	int i;
367e6445719SPravin B Shelar 
368e6445719SPravin B Shelar 	old_ver = old->node_ver;
369e6445719SPravin B Shelar 	new->node_ver = !old_ver;
370e6445719SPravin B Shelar 
371e6445719SPravin B Shelar 	/* Insert in new table. */
372e6445719SPravin B Shelar 	for (i = 0; i < old->n_buckets; i++) {
373e6445719SPravin B Shelar 		struct sw_flow *flow;
374ee9c5e67SKent Overstreet 		struct hlist_head *head = &old->buckets[i];
375e6445719SPravin B Shelar 
37674ed7ab9SJoe Stringer 		if (ufid)
37774ed7ab9SJoe Stringer 			hlist_for_each_entry(flow, head,
37874ed7ab9SJoe Stringer 					     ufid_table.node[old_ver])
37974ed7ab9SJoe Stringer 				ufid_table_instance_insert(new, flow);
38074ed7ab9SJoe Stringer 		else
38174ed7ab9SJoe Stringer 			hlist_for_each_entry(flow, head,
38274ed7ab9SJoe Stringer 					     flow_table.node[old_ver])
383b637e498SPravin B Shelar 				table_instance_insert(new, flow);
384e6445719SPravin B Shelar 	}
385e6445719SPravin B Shelar 
386e6445719SPravin B Shelar 	old->keep_flows = true;
387e6445719SPravin B Shelar }
388e6445719SPravin B Shelar 
389b637e498SPravin B Shelar static struct table_instance *table_instance_rehash(struct table_instance *ti,
39074ed7ab9SJoe Stringer 						    int n_buckets, bool ufid)
391e6445719SPravin B Shelar {
392b637e498SPravin B Shelar 	struct table_instance *new_ti;
393e6445719SPravin B Shelar 
394b637e498SPravin B Shelar 	new_ti = table_instance_alloc(n_buckets);
395b637e498SPravin B Shelar 	if (!new_ti)
396618ed0c8SPravin B Shelar 		return NULL;
397e6445719SPravin B Shelar 
39874ed7ab9SJoe Stringer 	flow_table_copy_flows(ti, new_ti, ufid);
399e6445719SPravin B Shelar 
400b637e498SPravin B Shelar 	return new_ti;
401e6445719SPravin B Shelar }
402e6445719SPravin B Shelar 
403b637e498SPravin B Shelar int ovs_flow_tbl_flush(struct flow_table *flow_table)
404e6445719SPravin B Shelar {
40574ed7ab9SJoe Stringer 	struct table_instance *old_ti, *new_ti;
40674ed7ab9SJoe Stringer 	struct table_instance *old_ufid_ti, *new_ufid_ti;
407e6445719SPravin B Shelar 
408b637e498SPravin B Shelar 	new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
409b637e498SPravin B Shelar 	if (!new_ti)
410b637e498SPravin B Shelar 		return -ENOMEM;
41174ed7ab9SJoe Stringer 	new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
41274ed7ab9SJoe Stringer 	if (!new_ufid_ti)
41374ed7ab9SJoe Stringer 		goto err_free_ti;
41474ed7ab9SJoe Stringer 
41574ed7ab9SJoe Stringer 	old_ti = ovsl_dereference(flow_table->ti);
41674ed7ab9SJoe Stringer 	old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
417b637e498SPravin B Shelar 
418b637e498SPravin B Shelar 	rcu_assign_pointer(flow_table->ti, new_ti);
41974ed7ab9SJoe Stringer 	rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
420b637e498SPravin B Shelar 	flow_table->last_rehash = jiffies;
421b637e498SPravin B Shelar 	flow_table->count = 0;
42274ed7ab9SJoe Stringer 	flow_table->ufid_count = 0;
423b637e498SPravin B Shelar 
42474ed7ab9SJoe Stringer 	table_instance_destroy(old_ti, old_ufid_ti, true);
425b637e498SPravin B Shelar 	return 0;
42674ed7ab9SJoe Stringer 
42774ed7ab9SJoe Stringer err_free_ti:
42874ed7ab9SJoe Stringer 	__table_instance_destroy(new_ti);
42974ed7ab9SJoe Stringer 	return -ENOMEM;
430e6445719SPravin B Shelar }
431e6445719SPravin B Shelar 
432272c2cf8SJoe Stringer static u32 flow_hash(const struct sw_flow_key *key,
433272c2cf8SJoe Stringer 		     const struct sw_flow_key_range *range)
434e6445719SPravin B Shelar {
435272c2cf8SJoe Stringer 	int key_start = range->start;
436272c2cf8SJoe Stringer 	int key_end = range->end;
4377085130bSDaniele Di Proietto 	const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
438e6445719SPravin B Shelar 	int hash_u32s = (key_end - key_start) >> 2;
439e6445719SPravin B Shelar 
440e6445719SPravin B Shelar 	/* Make sure number of hash bytes are multiple of u32. */
441e6445719SPravin B Shelar 	BUILD_BUG_ON(sizeof(long) % sizeof(u32));
442e6445719SPravin B Shelar 
44387545899SDaniel Borkmann 	return jhash2(hash_key, hash_u32s, 0);
444e6445719SPravin B Shelar }
445e6445719SPravin B Shelar 
446e6445719SPravin B Shelar static int flow_key_start(const struct sw_flow_key *key)
447e6445719SPravin B Shelar {
44800a93babSJiri Benc 	if (key->tun_proto)
449e6445719SPravin B Shelar 		return 0;
450e6445719SPravin B Shelar 	else
451e6445719SPravin B Shelar 		return rounddown(offsetof(struct sw_flow_key, phy),
452e6445719SPravin B Shelar 					  sizeof(long));
453e6445719SPravin B Shelar }
454e6445719SPravin B Shelar 
455e6445719SPravin B Shelar static bool cmp_key(const struct sw_flow_key *key1,
456e6445719SPravin B Shelar 		    const struct sw_flow_key *key2,
457e6445719SPravin B Shelar 		    int key_start, int key_end)
458e6445719SPravin B Shelar {
4597085130bSDaniele Di Proietto 	const long *cp1 = (const long *)((const u8 *)key1 + key_start);
4607085130bSDaniele Di Proietto 	const long *cp2 = (const long *)((const u8 *)key2 + key_start);
461e6445719SPravin B Shelar 	long diffs = 0;
462e6445719SPravin B Shelar 	int i;
463e6445719SPravin B Shelar 
464e6445719SPravin B Shelar 	for (i = key_start; i < key_end;  i += sizeof(long))
465e6445719SPravin B Shelar 		diffs |= *cp1++ ^ *cp2++;
466e6445719SPravin B Shelar 
467e6445719SPravin B Shelar 	return diffs == 0;
468e6445719SPravin B Shelar }
469e6445719SPravin B Shelar 
470e6445719SPravin B Shelar static bool flow_cmp_masked_key(const struct sw_flow *flow,
471e6445719SPravin B Shelar 				const struct sw_flow_key *key,
472272c2cf8SJoe Stringer 				const struct sw_flow_key_range *range)
473e6445719SPravin B Shelar {
474272c2cf8SJoe Stringer 	return cmp_key(&flow->key, key, range->start, range->end);
475e6445719SPravin B Shelar }
476e6445719SPravin B Shelar 
47774ed7ab9SJoe Stringer static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
47812eb18f7SThomas Graf 				      const struct sw_flow_match *match)
479e6445719SPravin B Shelar {
480e6445719SPravin B Shelar 	struct sw_flow_key *key = match->key;
481e6445719SPravin B Shelar 	int key_start = flow_key_start(key);
482e6445719SPravin B Shelar 	int key_end = match->range.end;
483e6445719SPravin B Shelar 
48474ed7ab9SJoe Stringer 	BUG_ON(ovs_identifier_is_ufid(&flow->id));
48574ed7ab9SJoe Stringer 	return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
486e6445719SPravin B Shelar }
487e6445719SPravin B Shelar 
488b637e498SPravin B Shelar static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
489e6445719SPravin B Shelar 					  const struct sw_flow_key *unmasked,
49004b7d136STonghao Zhang 					  const struct sw_flow_mask *mask,
49104b7d136STonghao Zhang 					  u32 *n_mask_hit)
492e6445719SPravin B Shelar {
493e6445719SPravin B Shelar 	struct sw_flow *flow;
494e6445719SPravin B Shelar 	struct hlist_head *head;
495e6445719SPravin B Shelar 	u32 hash;
496e6445719SPravin B Shelar 	struct sw_flow_key masked_key;
497e6445719SPravin B Shelar 
498ae5f2fb1SJesse Gross 	ovs_flow_mask_key(&masked_key, unmasked, false, mask);
499272c2cf8SJoe Stringer 	hash = flow_hash(&masked_key, &mask->range);
500b637e498SPravin B Shelar 	head = find_bucket(ti, hash);
50104b7d136STonghao Zhang 	(*n_mask_hit)++;
50204b7d136STonghao Zhang 
50374ed7ab9SJoe Stringer 	hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
50474ed7ab9SJoe Stringer 		if (flow->mask == mask && flow->flow_table.hash == hash &&
505272c2cf8SJoe Stringer 		    flow_cmp_masked_key(flow, &masked_key, &mask->range))
506e6445719SPravin B Shelar 			return flow;
507e6445719SPravin B Shelar 	}
508e6445719SPravin B Shelar 	return NULL;
509e6445719SPravin B Shelar }
510e6445719SPravin B Shelar 
511*a7f35e78STonghao Zhang /* Flow lookup does full lookup on flow table. It starts with
512*a7f35e78STonghao Zhang  * mask from index passed in *index.
513*a7f35e78STonghao Zhang  */
51404b7d136STonghao Zhang static struct sw_flow *flow_lookup(struct flow_table *tbl,
51504b7d136STonghao Zhang 				   struct table_instance *ti,
5164bc63b1bSTonghao Zhang 				   struct mask_array *ma,
5171bd7116fSAndy Zhou 				   const struct sw_flow_key *key,
5184bc63b1bSTonghao Zhang 				   u32 *n_mask_hit,
5194bc63b1bSTonghao Zhang 				   u32 *index)
520e6445719SPravin B Shelar {
521*a7f35e78STonghao Zhang 	struct sw_flow_mask *mask;
522b637e498SPravin B Shelar 	struct sw_flow *flow;
5234bc63b1bSTonghao Zhang 	int i;
524e6445719SPravin B Shelar 
525*a7f35e78STonghao Zhang 	if (*index < ma->max) {
526*a7f35e78STonghao Zhang 		mask = rcu_dereference_ovsl(ma->masks[*index]);
527*a7f35e78STonghao Zhang 		if (mask) {
528*a7f35e78STonghao Zhang 			flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
529*a7f35e78STonghao Zhang 			if (flow)
530*a7f35e78STonghao Zhang 				return flow;
531*a7f35e78STonghao Zhang 		}
532*a7f35e78STonghao Zhang 	}
533*a7f35e78STonghao Zhang 
5344bc63b1bSTonghao Zhang 	for (i = 0; i < ma->max; i++)  {
535*a7f35e78STonghao Zhang 
536*a7f35e78STonghao Zhang 		if (i == *index)
537*a7f35e78STonghao Zhang 			continue;
5384bc63b1bSTonghao Zhang 
5394bc63b1bSTonghao Zhang 		mask = rcu_dereference_ovsl(ma->masks[i]);
540*a7f35e78STonghao Zhang 		if (!mask)
541*a7f35e78STonghao Zhang 			continue;
542*a7f35e78STonghao Zhang 
54304b7d136STonghao Zhang 		flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
5444bc63b1bSTonghao Zhang 		if (flow) { /* Found */
5454bc63b1bSTonghao Zhang 			*index = i;
546b637e498SPravin B Shelar 			return flow;
547b637e498SPravin B Shelar 		}
5484bc63b1bSTonghao Zhang 	}
5494bc63b1bSTonghao Zhang 
550b637e498SPravin B Shelar 	return NULL;
551e6445719SPravin B Shelar }
552e6445719SPravin B Shelar 
55304b7d136STonghao Zhang /*
55404b7d136STonghao Zhang  * mask_cache maps flow to probable mask. This cache is not tightly
55504b7d136STonghao Zhang  * coupled cache, It means updates to  mask list can result in inconsistent
55604b7d136STonghao Zhang  * cache entry in mask cache.
55704b7d136STonghao Zhang  * This is per cpu cache and is divided in MC_HASH_SEGS segments.
55804b7d136STonghao Zhang  * In case of a hash collision the entry is hashed in next segment.
55904b7d136STonghao Zhang  * */
56004b7d136STonghao Zhang struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
56104b7d136STonghao Zhang 					  const struct sw_flow_key *key,
56204b7d136STonghao Zhang 					  u32 skb_hash,
56304b7d136STonghao Zhang 					  u32 *n_mask_hit)
56404b7d136STonghao Zhang {
565*a7f35e78STonghao Zhang 	struct mask_array *ma = rcu_dereference(tbl->mask_array);
566*a7f35e78STonghao Zhang 	struct table_instance *ti = rcu_dereference(tbl->ti);
567*a7f35e78STonghao Zhang 	struct mask_cache_entry *entries, *ce;
56804b7d136STonghao Zhang 	struct sw_flow *flow;
569*a7f35e78STonghao Zhang 	u32 hash;
57004b7d136STonghao Zhang 	int seg;
57104b7d136STonghao Zhang 
57204b7d136STonghao Zhang 	*n_mask_hit = 0;
5734bc63b1bSTonghao Zhang 	if (unlikely(!skb_hash)) {
574*a7f35e78STonghao Zhang 		u32 mask_index = 0;
5754bc63b1bSTonghao Zhang 
5764bc63b1bSTonghao Zhang 		return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index);
5774bc63b1bSTonghao Zhang 	}
57804b7d136STonghao Zhang 
579*a7f35e78STonghao Zhang 	/* Pre and post recirulation flows usually have the same skb_hash
580*a7f35e78STonghao Zhang 	 * value. To avoid hash collisions, rehash the 'skb_hash' with
581*a7f35e78STonghao Zhang 	 * 'recirc_id'.  */
582*a7f35e78STonghao Zhang 	if (key->recirc_id)
583*a7f35e78STonghao Zhang 		skb_hash = jhash_1word(skb_hash, key->recirc_id);
584*a7f35e78STonghao Zhang 
585*a7f35e78STonghao Zhang 	ce = NULL;
586*a7f35e78STonghao Zhang 	hash = skb_hash;
58704b7d136STonghao Zhang 	entries = this_cpu_ptr(tbl->mask_cache);
58804b7d136STonghao Zhang 
589*a7f35e78STonghao Zhang 	/* Find the cache entry 'ce' to operate on. */
59004b7d136STonghao Zhang 	for (seg = 0; seg < MC_HASH_SEGS; seg++) {
591*a7f35e78STonghao Zhang 		int index = hash & (MC_HASH_ENTRIES - 1);
592*a7f35e78STonghao Zhang 		struct mask_cache_entry *e;
59304b7d136STonghao Zhang 
594*a7f35e78STonghao Zhang 		e = &entries[index];
595*a7f35e78STonghao Zhang 		if (e->skb_hash == skb_hash) {
596*a7f35e78STonghao Zhang 			flow = flow_lookup(tbl, ti, ma, key, n_mask_hit,
597*a7f35e78STonghao Zhang 					   &e->mask_index);
598*a7f35e78STonghao Zhang 			if (!flow)
599*a7f35e78STonghao Zhang 				e->skb_hash = 0;
60004b7d136STonghao Zhang 			return flow;
60104b7d136STonghao Zhang 		}
60204b7d136STonghao Zhang 
603*a7f35e78STonghao Zhang 		if (!ce || e->skb_hash < ce->skb_hash)
604*a7f35e78STonghao Zhang 			ce = e;  /* A better replacement cache candidate. */
60504b7d136STonghao Zhang 
60604b7d136STonghao Zhang 		hash >>= MC_HASH_SHIFT;
60704b7d136STonghao Zhang 	}
60804b7d136STonghao Zhang 
609*a7f35e78STonghao Zhang 	/* Cache miss, do full lookup. */
610*a7f35e78STonghao Zhang 	flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &ce->mask_index);
6114bc63b1bSTonghao Zhang 	if (flow)
612*a7f35e78STonghao Zhang 		ce->skb_hash = skb_hash;
61304b7d136STonghao Zhang 
61404b7d136STonghao Zhang 	return flow;
61504b7d136STonghao Zhang }
61604b7d136STonghao Zhang 
6175bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
6185bb50632SAndy Zhou 				    const struct sw_flow_key *key)
6195bb50632SAndy Zhou {
62004b7d136STonghao Zhang 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
6214bc63b1bSTonghao Zhang 	struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
6224bc63b1bSTonghao Zhang 	u32 __always_unused n_mask_hit;
623*a7f35e78STonghao Zhang 	u32 index = 0;
6244bc63b1bSTonghao Zhang 
6254bc63b1bSTonghao Zhang 	return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index);
6265bb50632SAndy Zhou }
6275bb50632SAndy Zhou 
6284a46b24eSAlex Wang struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
62912eb18f7SThomas Graf 					  const struct sw_flow_match *match)
6304a46b24eSAlex Wang {
6314bc63b1bSTonghao Zhang 	struct mask_array *ma = ovsl_dereference(tbl->mask_array);
6324bc63b1bSTonghao Zhang 	int i;
6334a46b24eSAlex Wang 
6344a46b24eSAlex Wang 	/* Always called under ovs-mutex. */
6354bc63b1bSTonghao Zhang 	for (i = 0; i < ma->max; i++) {
6364bc63b1bSTonghao Zhang 		struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
6374bc63b1bSTonghao Zhang 		u32 __always_unused n_mask_hit;
6384bc63b1bSTonghao Zhang 		struct sw_flow_mask *mask;
6394bc63b1bSTonghao Zhang 		struct sw_flow *flow;
6404bc63b1bSTonghao Zhang 
6414bc63b1bSTonghao Zhang 		mask = ovsl_dereference(ma->masks[i]);
6424bc63b1bSTonghao Zhang 		if (!mask)
6434bc63b1bSTonghao Zhang 			continue;
6444bc63b1bSTonghao Zhang 
64504b7d136STonghao Zhang 		flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit);
64674ed7ab9SJoe Stringer 		if (flow && ovs_identifier_is_key(&flow->id) &&
6474bc63b1bSTonghao Zhang 		    ovs_flow_cmp_unmasked_key(flow, match)) {
64874ed7ab9SJoe Stringer 			return flow;
64974ed7ab9SJoe Stringer 		}
6504bc63b1bSTonghao Zhang 	}
6514bc63b1bSTonghao Zhang 
65274ed7ab9SJoe Stringer 	return NULL;
65374ed7ab9SJoe Stringer }
65474ed7ab9SJoe Stringer 
65574ed7ab9SJoe Stringer static u32 ufid_hash(const struct sw_flow_id *sfid)
65674ed7ab9SJoe Stringer {
65774ed7ab9SJoe Stringer 	return jhash(sfid->ufid, sfid->ufid_len, 0);
65874ed7ab9SJoe Stringer }
65974ed7ab9SJoe Stringer 
66074ed7ab9SJoe Stringer static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
66174ed7ab9SJoe Stringer 			      const struct sw_flow_id *sfid)
66274ed7ab9SJoe Stringer {
66374ed7ab9SJoe Stringer 	if (flow->id.ufid_len != sfid->ufid_len)
66474ed7ab9SJoe Stringer 		return false;
66574ed7ab9SJoe Stringer 
66674ed7ab9SJoe Stringer 	return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
66774ed7ab9SJoe Stringer }
66874ed7ab9SJoe Stringer 
66974ed7ab9SJoe Stringer bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
67074ed7ab9SJoe Stringer {
67174ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id))
67274ed7ab9SJoe Stringer 		return flow_cmp_masked_key(flow, match->key, &match->range);
67374ed7ab9SJoe Stringer 
67474ed7ab9SJoe Stringer 	return ovs_flow_cmp_unmasked_key(flow, match);
67574ed7ab9SJoe Stringer }
67674ed7ab9SJoe Stringer 
67774ed7ab9SJoe Stringer struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
67874ed7ab9SJoe Stringer 					 const struct sw_flow_id *ufid)
67974ed7ab9SJoe Stringer {
68074ed7ab9SJoe Stringer 	struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
68174ed7ab9SJoe Stringer 	struct sw_flow *flow;
68274ed7ab9SJoe Stringer 	struct hlist_head *head;
68374ed7ab9SJoe Stringer 	u32 hash;
68474ed7ab9SJoe Stringer 
68574ed7ab9SJoe Stringer 	hash = ufid_hash(ufid);
68674ed7ab9SJoe Stringer 	head = find_bucket(ti, hash);
68774ed7ab9SJoe Stringer 	hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
68874ed7ab9SJoe Stringer 		if (flow->ufid_table.hash == hash &&
68974ed7ab9SJoe Stringer 		    ovs_flow_cmp_ufid(flow, ufid))
6904a46b24eSAlex Wang 			return flow;
6914a46b24eSAlex Wang 	}
6924a46b24eSAlex Wang 	return NULL;
6934a46b24eSAlex Wang }
6944a46b24eSAlex Wang 
6951bd7116fSAndy Zhou int ovs_flow_tbl_num_masks(const struct flow_table *table)
6961bd7116fSAndy Zhou {
6974bc63b1bSTonghao Zhang 	struct mask_array *ma = rcu_dereference_ovsl(table->mask_array);
6981bd7116fSAndy Zhou 
6994bc63b1bSTonghao Zhang 	return ma->count;
7001bd7116fSAndy Zhou }
7011bd7116fSAndy Zhou 
70274ed7ab9SJoe Stringer static struct table_instance *table_instance_expand(struct table_instance *ti,
70374ed7ab9SJoe Stringer 						    bool ufid)
704b637e498SPravin B Shelar {
70574ed7ab9SJoe Stringer 	return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
706e6445719SPravin B Shelar }
707e6445719SPravin B Shelar 
7081689754dSTonghao Zhang static void tbl_mask_array_delete_mask(struct mask_array *ma,
7091689754dSTonghao Zhang 				       struct sw_flow_mask *mask)
7101689754dSTonghao Zhang {
7111689754dSTonghao Zhang 	int i;
7121689754dSTonghao Zhang 
7131689754dSTonghao Zhang 	/* Remove the deleted mask pointers from the array */
7141689754dSTonghao Zhang 	for (i = 0; i < ma->max; i++) {
7151689754dSTonghao Zhang 		if (mask == ovsl_dereference(ma->masks[i])) {
7161689754dSTonghao Zhang 			RCU_INIT_POINTER(ma->masks[i], NULL);
7171689754dSTonghao Zhang 			ma->count--;
7181689754dSTonghao Zhang 			kfree_rcu(mask, rcu);
7191689754dSTonghao Zhang 			return;
7201689754dSTonghao Zhang 		}
7211689754dSTonghao Zhang 	}
7221689754dSTonghao Zhang 	BUG();
7231689754dSTonghao Zhang }
7241689754dSTonghao Zhang 
72556c19868SJarno Rajahalme /* Remove 'mask' from the mask list, if it is not needed any more. */
72656c19868SJarno Rajahalme static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
72756c19868SJarno Rajahalme {
72856c19868SJarno Rajahalme 	if (mask) {
72956c19868SJarno Rajahalme 		/* ovs-lock is required to protect mask-refcount and
73056c19868SJarno Rajahalme 		 * mask list.
73156c19868SJarno Rajahalme 		 */
73256c19868SJarno Rajahalme 		ASSERT_OVSL();
73356c19868SJarno Rajahalme 		BUG_ON(!mask->ref_count);
73456c19868SJarno Rajahalme 		mask->ref_count--;
73556c19868SJarno Rajahalme 
73656c19868SJarno Rajahalme 		if (!mask->ref_count) {
7374bc63b1bSTonghao Zhang 			struct mask_array *ma;
7384bc63b1bSTonghao Zhang 
7394bc63b1bSTonghao Zhang 			ma = ovsl_dereference(tbl->mask_array);
7401689754dSTonghao Zhang 			tbl_mask_array_delete_mask(ma, mask);
7411689754dSTonghao Zhang 
7421689754dSTonghao Zhang 			/* Shrink the mask array if necessary. */
7431689754dSTonghao Zhang 			if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) &&
7441689754dSTonghao Zhang 			    ma->count <= (ma->max / 3))
7451689754dSTonghao Zhang 				tbl_mask_array_realloc(tbl, ma->max / 2);
74656c19868SJarno Rajahalme 		}
74756c19868SJarno Rajahalme 	}
74856c19868SJarno Rajahalme }
74956c19868SJarno Rajahalme 
75056c19868SJarno Rajahalme /* Must be called with OVS mutex held. */
751e6445719SPravin B Shelar void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
752e6445719SPravin B Shelar {
753b637e498SPravin B Shelar 	struct table_instance *ti = ovsl_dereference(table->ti);
75474ed7ab9SJoe Stringer 	struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
755b637e498SPravin B Shelar 
756e6445719SPravin B Shelar 	BUG_ON(table->count == 0);
75774ed7ab9SJoe Stringer 	hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
758e6445719SPravin B Shelar 	table->count--;
75974ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id)) {
76074ed7ab9SJoe Stringer 		hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
76174ed7ab9SJoe Stringer 		table->ufid_count--;
76274ed7ab9SJoe Stringer 	}
76356c19868SJarno Rajahalme 
76456c19868SJarno Rajahalme 	/* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
76556c19868SJarno Rajahalme 	 * accessible as long as the RCU read lock is held.
76656c19868SJarno Rajahalme 	 */
76756c19868SJarno Rajahalme 	flow_mask_remove(table, flow->mask);
768e6445719SPravin B Shelar }
769e6445719SPravin B Shelar 
770618ed0c8SPravin B Shelar static struct sw_flow_mask *mask_alloc(void)
771e6445719SPravin B Shelar {
772e6445719SPravin B Shelar 	struct sw_flow_mask *mask;
773e6445719SPravin B Shelar 
774e6445719SPravin B Shelar 	mask = kmalloc(sizeof(*mask), GFP_KERNEL);
775e6445719SPravin B Shelar 	if (mask)
776e80857ccSAndy Zhou 		mask->ref_count = 1;
777e6445719SPravin B Shelar 
778e6445719SPravin B Shelar 	return mask;
779e6445719SPravin B Shelar }
780e6445719SPravin B Shelar 
781e6445719SPravin B Shelar static bool mask_equal(const struct sw_flow_mask *a,
782e6445719SPravin B Shelar 		       const struct sw_flow_mask *b)
783e6445719SPravin B Shelar {
7847085130bSDaniele Di Proietto 	const u8 *a_ = (const u8 *)&a->key + a->range.start;
7857085130bSDaniele Di Proietto 	const u8 *b_ = (const u8 *)&b->key + b->range.start;
786e6445719SPravin B Shelar 
787e6445719SPravin B Shelar 	return  (a->range.end == b->range.end)
788e6445719SPravin B Shelar 		&& (a->range.start == b->range.start)
789e6445719SPravin B Shelar 		&& (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
790e6445719SPravin B Shelar }
791e6445719SPravin B Shelar 
792618ed0c8SPravin B Shelar static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
793e6445719SPravin B Shelar 					   const struct sw_flow_mask *mask)
794e6445719SPravin B Shelar {
7954bc63b1bSTonghao Zhang 	struct mask_array *ma;
7964bc63b1bSTonghao Zhang 	int i;
797e6445719SPravin B Shelar 
7984bc63b1bSTonghao Zhang 	ma = ovsl_dereference(tbl->mask_array);
7994bc63b1bSTonghao Zhang 	for (i = 0; i < ma->max; i++) {
8004bc63b1bSTonghao Zhang 		struct sw_flow_mask *t;
8014bc63b1bSTonghao Zhang 		t = ovsl_dereference(ma->masks[i]);
8024bc63b1bSTonghao Zhang 
8034bc63b1bSTonghao Zhang 		if (t && mask_equal(mask, t))
8044bc63b1bSTonghao Zhang 			return t;
805e6445719SPravin B Shelar 	}
806e6445719SPravin B Shelar 
807e6445719SPravin B Shelar 	return NULL;
808e6445719SPravin B Shelar }
809e6445719SPravin B Shelar 
810d1211908SBen Pfaff /* Add 'mask' into the mask list, if it is not already there. */
811618ed0c8SPravin B Shelar static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
81212eb18f7SThomas Graf 			    const struct sw_flow_mask *new)
813e6445719SPravin B Shelar {
814618ed0c8SPravin B Shelar 	struct sw_flow_mask *mask;
8154bc63b1bSTonghao Zhang 
816618ed0c8SPravin B Shelar 	mask = flow_mask_find(tbl, new);
817618ed0c8SPravin B Shelar 	if (!mask) {
8184bc63b1bSTonghao Zhang 		struct mask_array *ma;
8194bc63b1bSTonghao Zhang 		int i;
8204bc63b1bSTonghao Zhang 
821618ed0c8SPravin B Shelar 		/* Allocate a new mask if none exsits. */
822618ed0c8SPravin B Shelar 		mask = mask_alloc();
823618ed0c8SPravin B Shelar 		if (!mask)
824618ed0c8SPravin B Shelar 			return -ENOMEM;
825618ed0c8SPravin B Shelar 		mask->key = new->key;
826618ed0c8SPravin B Shelar 		mask->range = new->range;
8274bc63b1bSTonghao Zhang 
8284bc63b1bSTonghao Zhang 		/* Add mask to mask-list. */
8294bc63b1bSTonghao Zhang 		ma = ovsl_dereference(tbl->mask_array);
8304bc63b1bSTonghao Zhang 		if (ma->count >= ma->max) {
8314bc63b1bSTonghao Zhang 			int err;
8324bc63b1bSTonghao Zhang 
8334bc63b1bSTonghao Zhang 			err = tbl_mask_array_realloc(tbl, ma->max +
8344bc63b1bSTonghao Zhang 						     MASK_ARRAY_SIZE_MIN);
8354bc63b1bSTonghao Zhang 			if (err) {
8364bc63b1bSTonghao Zhang 				kfree(mask);
8374bc63b1bSTonghao Zhang 				return err;
8384bc63b1bSTonghao Zhang 			}
8394bc63b1bSTonghao Zhang 
8404bc63b1bSTonghao Zhang 			ma = ovsl_dereference(tbl->mask_array);
8414bc63b1bSTonghao Zhang 		}
8424bc63b1bSTonghao Zhang 
8434bc63b1bSTonghao Zhang 		for (i = 0; i < ma->max; i++) {
8444bc63b1bSTonghao Zhang 			const struct sw_flow_mask *t;
8454bc63b1bSTonghao Zhang 
8464bc63b1bSTonghao Zhang 			t = ovsl_dereference(ma->masks[i]);
8474bc63b1bSTonghao Zhang 			if (!t) {
8484bc63b1bSTonghao Zhang 				rcu_assign_pointer(ma->masks[i], mask);
8494bc63b1bSTonghao Zhang 				ma->count++;
8504bc63b1bSTonghao Zhang 				break;
8514bc63b1bSTonghao Zhang 			}
8524bc63b1bSTonghao Zhang 		}
853e80857ccSAndy Zhou 	} else {
854e80857ccSAndy Zhou 		BUG_ON(!mask->ref_count);
855e80857ccSAndy Zhou 		mask->ref_count++;
856e6445719SPravin B Shelar 	}
857e6445719SPravin B Shelar 
858618ed0c8SPravin B Shelar 	flow->mask = mask;
859618ed0c8SPravin B Shelar 	return 0;
860618ed0c8SPravin B Shelar }
861618ed0c8SPravin B Shelar 
86256c19868SJarno Rajahalme /* Must be called with OVS mutex held. */
863d29ab6f8SJoe Stringer static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
864618ed0c8SPravin B Shelar {
865618ed0c8SPravin B Shelar 	struct table_instance *new_ti = NULL;
866618ed0c8SPravin B Shelar 	struct table_instance *ti;
867618ed0c8SPravin B Shelar 
86874ed7ab9SJoe Stringer 	flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
869618ed0c8SPravin B Shelar 	ti = ovsl_dereference(table->ti);
870618ed0c8SPravin B Shelar 	table_instance_insert(ti, flow);
871618ed0c8SPravin B Shelar 	table->count++;
872618ed0c8SPravin B Shelar 
873618ed0c8SPravin B Shelar 	/* Expand table, if necessary, to make room. */
874618ed0c8SPravin B Shelar 	if (table->count > ti->n_buckets)
87574ed7ab9SJoe Stringer 		new_ti = table_instance_expand(ti, false);
876618ed0c8SPravin B Shelar 	else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
87774ed7ab9SJoe Stringer 		new_ti = table_instance_rehash(ti, ti->n_buckets, false);
878618ed0c8SPravin B Shelar 
879618ed0c8SPravin B Shelar 	if (new_ti) {
880618ed0c8SPravin B Shelar 		rcu_assign_pointer(table->ti, new_ti);
88174ed7ab9SJoe Stringer 		call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
882618ed0c8SPravin B Shelar 		table->last_rehash = jiffies;
883618ed0c8SPravin B Shelar 	}
884d29ab6f8SJoe Stringer }
885d29ab6f8SJoe Stringer 
886d29ab6f8SJoe Stringer /* Must be called with OVS mutex held. */
88774ed7ab9SJoe Stringer static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
88874ed7ab9SJoe Stringer {
88974ed7ab9SJoe Stringer 	struct table_instance *ti;
89074ed7ab9SJoe Stringer 
89174ed7ab9SJoe Stringer 	flow->ufid_table.hash = ufid_hash(&flow->id);
89274ed7ab9SJoe Stringer 	ti = ovsl_dereference(table->ufid_ti);
89374ed7ab9SJoe Stringer 	ufid_table_instance_insert(ti, flow);
89474ed7ab9SJoe Stringer 	table->ufid_count++;
89574ed7ab9SJoe Stringer 
89674ed7ab9SJoe Stringer 	/* Expand table, if necessary, to make room. */
89774ed7ab9SJoe Stringer 	if (table->ufid_count > ti->n_buckets) {
89874ed7ab9SJoe Stringer 		struct table_instance *new_ti;
89974ed7ab9SJoe Stringer 
90074ed7ab9SJoe Stringer 		new_ti = table_instance_expand(ti, true);
90174ed7ab9SJoe Stringer 		if (new_ti) {
90274ed7ab9SJoe Stringer 			rcu_assign_pointer(table->ufid_ti, new_ti);
90374ed7ab9SJoe Stringer 			call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
90474ed7ab9SJoe Stringer 		}
90574ed7ab9SJoe Stringer 	}
90674ed7ab9SJoe Stringer }
90774ed7ab9SJoe Stringer 
90874ed7ab9SJoe Stringer /* Must be called with OVS mutex held. */
909d29ab6f8SJoe Stringer int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
910d29ab6f8SJoe Stringer 			const struct sw_flow_mask *mask)
911d29ab6f8SJoe Stringer {
912d29ab6f8SJoe Stringer 	int err;
913d29ab6f8SJoe Stringer 
914d29ab6f8SJoe Stringer 	err = flow_mask_insert(table, flow, mask);
915d29ab6f8SJoe Stringer 	if (err)
916d29ab6f8SJoe Stringer 		return err;
917d29ab6f8SJoe Stringer 	flow_key_insert(table, flow);
91874ed7ab9SJoe Stringer 	if (ovs_identifier_is_ufid(&flow->id))
91974ed7ab9SJoe Stringer 		flow_ufid_insert(table, flow);
920d29ab6f8SJoe Stringer 
921618ed0c8SPravin B Shelar 	return 0;
922618ed0c8SPravin B Shelar }
923618ed0c8SPravin B Shelar 
924e6445719SPravin B Shelar /* Initializes the flow module.
925e6445719SPravin B Shelar  * Returns zero if successful or a negative error code. */
926e6445719SPravin B Shelar int ovs_flow_init(void)
927e6445719SPravin B Shelar {
928e6445719SPravin B Shelar 	BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
929e6445719SPravin B Shelar 	BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
930e6445719SPravin B Shelar 
93163e7959cSJarno Rajahalme 	flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
932db74a333SThadeu Lima de Souza Cascardo 				       + (nr_cpu_ids
933aef833c5SPablo Neira Ayuso 					  * sizeof(struct sw_flow_stats *)),
93463e7959cSJarno Rajahalme 				       0, 0, NULL);
935e6445719SPravin B Shelar 	if (flow_cache == NULL)
936e6445719SPravin B Shelar 		return -ENOMEM;
937e6445719SPravin B Shelar 
93863e7959cSJarno Rajahalme 	flow_stats_cache
939aef833c5SPablo Neira Ayuso 		= kmem_cache_create("sw_flow_stats", sizeof(struct sw_flow_stats),
94063e7959cSJarno Rajahalme 				    0, SLAB_HWCACHE_ALIGN, NULL);
94163e7959cSJarno Rajahalme 	if (flow_stats_cache == NULL) {
94263e7959cSJarno Rajahalme 		kmem_cache_destroy(flow_cache);
94363e7959cSJarno Rajahalme 		flow_cache = NULL;
94463e7959cSJarno Rajahalme 		return -ENOMEM;
94563e7959cSJarno Rajahalme 	}
94663e7959cSJarno Rajahalme 
947e6445719SPravin B Shelar 	return 0;
948e6445719SPravin B Shelar }
949e6445719SPravin B Shelar 
950e6445719SPravin B Shelar /* Uninitializes the flow module. */
951e6445719SPravin B Shelar void ovs_flow_exit(void)
952e6445719SPravin B Shelar {
95363e7959cSJarno Rajahalme 	kmem_cache_destroy(flow_stats_cache);
954e6445719SPravin B Shelar 	kmem_cache_destroy(flow_cache);
955e6445719SPravin B Shelar }
956