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>
32eac87c41SEelco Chaudron #include <linux/sort.h>
33e6445719SPravin B Shelar #include <net/ip.h>
34e6445719SPravin B Shelar #include <net/ipv6.h>
35e6445719SPravin B Shelar #include <net/ndisc.h>
36e6445719SPravin B Shelar
37b637e498SPravin B Shelar #define TBL_MIN_BUCKETS 1024
384bc63b1bSTonghao Zhang #define MASK_ARRAY_SIZE_MIN 16
39b637e498SPravin B Shelar #define REHASH_INTERVAL (10 * 60 * HZ)
40b637e498SPravin B Shelar
419bf24f59SEelco Chaudron #define MC_DEFAULT_HASH_ENTRIES 256
4204b7d136STonghao Zhang #define MC_HASH_SHIFT 8
4304b7d136STonghao Zhang #define MC_HASH_SEGS ((sizeof(uint32_t) * 8) / MC_HASH_SHIFT)
4404b7d136STonghao Zhang
45e6445719SPravin B Shelar static struct kmem_cache *flow_cache;
4663e7959cSJarno Rajahalme struct kmem_cache *flow_stats_cache __read_mostly;
47e6445719SPravin B Shelar
range_n_bytes(const struct sw_flow_key_range * range)48e6445719SPravin B Shelar static u16 range_n_bytes(const struct sw_flow_key_range *range)
49e6445719SPravin B Shelar {
50e6445719SPravin B Shelar return range->end - range->start;
51e6445719SPravin B Shelar }
52e6445719SPravin B Shelar
ovs_flow_mask_key(struct sw_flow_key * dst,const struct sw_flow_key * src,bool full,const struct sw_flow_mask * mask)53e6445719SPravin B Shelar void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
54ae5f2fb1SJesse Gross bool full, const struct sw_flow_mask *mask)
55e6445719SPravin B Shelar {
56ae5f2fb1SJesse Gross int start = full ? 0 : mask->range.start;
57ae5f2fb1SJesse Gross int len = full ? sizeof *dst : range_n_bytes(&mask->range);
58ae5f2fb1SJesse Gross const long *m = (const long *)((const u8 *)&mask->key + start);
59ae5f2fb1SJesse Gross const long *s = (const long *)((const u8 *)src + start);
60ae5f2fb1SJesse Gross long *d = (long *)((u8 *)dst + start);
61e6445719SPravin B Shelar int i;
62e6445719SPravin B Shelar
63ae5f2fb1SJesse Gross /* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
64ae5f2fb1SJesse Gross * if 'full' is false the memory outside of the 'mask->range' is left
65ae5f2fb1SJesse Gross * uninitialized. This can be used as an optimization when further
66ae5f2fb1SJesse Gross * operations on 'dst' only use contents within 'mask->range'.
67e6445719SPravin B Shelar */
68ae5f2fb1SJesse Gross for (i = 0; i < len; i += sizeof(long))
69e6445719SPravin B Shelar *d++ = *s++ & *m++;
70e6445719SPravin B Shelar }
71e6445719SPravin B Shelar
ovs_flow_alloc(void)7223dabf88SJarno Rajahalme struct sw_flow *ovs_flow_alloc(void)
73e6445719SPravin B Shelar {
74e6445719SPravin B Shelar struct sw_flow *flow;
75aef833c5SPablo Neira Ayuso struct sw_flow_stats *stats;
76e6445719SPravin B Shelar
77db74a333SThadeu Lima de Souza Cascardo flow = kmem_cache_zalloc(flow_cache, GFP_KERNEL);
78e6445719SPravin B Shelar if (!flow)
79e6445719SPravin B Shelar return ERR_PTR(-ENOMEM);
80e6445719SPravin B Shelar
81db74a333SThadeu Lima de Souza Cascardo flow->stats_last_writer = -1;
82*15ea59a0SEddy Tao flow->cpu_used_mask = (struct cpumask *)&flow->stats[nr_cpu_ids];
83e6445719SPravin B Shelar
8463e7959cSJarno Rajahalme /* Initialize the default stat node. */
8563e7959cSJarno Rajahalme stats = kmem_cache_alloc_node(flow_stats_cache,
86598c12d0SKonstantin Khlebnikov GFP_KERNEL | __GFP_ZERO,
87598c12d0SKonstantin Khlebnikov node_online(0) ? 0 : NUMA_NO_NODE);
8863e7959cSJarno Rajahalme if (!stats)
89e298e505SPravin B Shelar goto err;
90e298e505SPravin B Shelar
9163e7959cSJarno Rajahalme spin_lock_init(&stats->lock);
92e298e505SPravin B Shelar
9363e7959cSJarno Rajahalme RCU_INIT_POINTER(flow->stats[0], stats);
9463e7959cSJarno Rajahalme
95*15ea59a0SEddy Tao cpumask_set_cpu(0, flow->cpu_used_mask);
96c4b2bf6bSTonghao Zhang
97e6445719SPravin B Shelar return flow;
98e298e505SPravin B Shelar err:
99ece37c87SWei Yongjun kmem_cache_free(flow_cache, flow);
100e298e505SPravin B Shelar return ERR_PTR(-ENOMEM);
101e6445719SPravin B Shelar }
102e6445719SPravin B Shelar
ovs_flow_tbl_count(const struct flow_table * table)10312eb18f7SThomas Graf int ovs_flow_tbl_count(const struct flow_table *table)
104b637e498SPravin B Shelar {
105b637e498SPravin B Shelar return table->count;
106b637e498SPravin B Shelar }
107b637e498SPravin B Shelar
flow_free(struct sw_flow * flow)108e6445719SPravin B Shelar static void flow_free(struct sw_flow *flow)
109e6445719SPravin B Shelar {
110db74a333SThadeu Lima de Souza Cascardo int cpu;
11163e7959cSJarno Rajahalme
11274ed7ab9SJoe Stringer if (ovs_identifier_is_key(&flow->id))
11374ed7ab9SJoe Stringer kfree(flow->id.unmasked_key);
11434ae932aSThomas Graf if (flow->sf_acts)
115cf3266adSTonghao Zhang ovs_nla_free_flow_actions((struct sw_flow_actions __force *)
116cf3266adSTonghao Zhang flow->sf_acts);
117db74a333SThadeu Lima de Souza Cascardo /* We open code this to make sure cpu 0 is always considered */
118cf3266adSTonghao Zhang for (cpu = 0; cpu < nr_cpu_ids;
119*15ea59a0SEddy Tao cpu = cpumask_next(cpu, flow->cpu_used_mask)) {
120db74a333SThadeu Lima de Souza Cascardo if (flow->stats[cpu])
12163e7959cSJarno Rajahalme kmem_cache_free(flow_stats_cache,
122aef833c5SPablo Neira Ayuso (struct sw_flow_stats __force *)flow->stats[cpu]);
123cf3266adSTonghao Zhang }
124cf3266adSTonghao Zhang
125e6445719SPravin B Shelar kmem_cache_free(flow_cache, flow);
126e6445719SPravin B Shelar }
127e6445719SPravin B Shelar
rcu_free_flow_callback(struct rcu_head * rcu)128e6445719SPravin B Shelar static void rcu_free_flow_callback(struct rcu_head *rcu)
129e6445719SPravin B Shelar {
130e6445719SPravin B Shelar struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
131e6445719SPravin B Shelar
132e6445719SPravin B Shelar flow_free(flow);
133e6445719SPravin B Shelar }
134e6445719SPravin B Shelar
ovs_flow_free(struct sw_flow * flow,bool deferred)135e80857ccSAndy Zhou void ovs_flow_free(struct sw_flow *flow, bool deferred)
136618ed0c8SPravin B Shelar {
137e80857ccSAndy Zhou if (!flow)
138618ed0c8SPravin B Shelar return;
139618ed0c8SPravin B Shelar
140e6445719SPravin B Shelar if (deferred)
141e6445719SPravin B Shelar call_rcu(&flow->rcu, rcu_free_flow_callback);
142e6445719SPravin B Shelar else
143e6445719SPravin B Shelar flow_free(flow);
144e6445719SPravin B Shelar }
145e6445719SPravin B Shelar
__table_instance_destroy(struct table_instance * ti)146b637e498SPravin B Shelar static void __table_instance_destroy(struct table_instance *ti)
147e6445719SPravin B Shelar {
148ee9c5e67SKent Overstreet kvfree(ti->buckets);
149b637e498SPravin B Shelar kfree(ti);
150e6445719SPravin B Shelar }
151e6445719SPravin B Shelar
table_instance_alloc(int new_size)152b637e498SPravin B Shelar static struct table_instance *table_instance_alloc(int new_size)
153e6445719SPravin B Shelar {
154b637e498SPravin B Shelar struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
155ee9c5e67SKent Overstreet int i;
156e6445719SPravin B Shelar
157b637e498SPravin B Shelar if (!ti)
158e6445719SPravin B Shelar return NULL;
159e6445719SPravin B Shelar
160ee9c5e67SKent Overstreet ti->buckets = kvmalloc_array(new_size, sizeof(struct hlist_head),
161ee9c5e67SKent Overstreet GFP_KERNEL);
162b637e498SPravin B Shelar if (!ti->buckets) {
163b637e498SPravin B Shelar kfree(ti);
164e6445719SPravin B Shelar return NULL;
165e6445719SPravin B Shelar }
166ee9c5e67SKent Overstreet
167ee9c5e67SKent Overstreet for (i = 0; i < new_size; i++)
168ee9c5e67SKent Overstreet INIT_HLIST_HEAD(&ti->buckets[i]);
169ee9c5e67SKent Overstreet
170b637e498SPravin B Shelar ti->n_buckets = new_size;
171b637e498SPravin B Shelar ti->node_ver = 0;
172b637e498SPravin B Shelar get_random_bytes(&ti->hash_seed, sizeof(u32));
173b637e498SPravin B Shelar
174b637e498SPravin B Shelar return ti;
175b637e498SPravin B Shelar }
176b637e498SPravin B Shelar
__mask_array_destroy(struct mask_array * ma)177eac87c41SEelco Chaudron static void __mask_array_destroy(struct mask_array *ma)
178eac87c41SEelco Chaudron {
179f981fc3dSEelco Chaudron free_percpu(ma->masks_usage_stats);
180eac87c41SEelco Chaudron kfree(ma);
181eac87c41SEelco Chaudron }
182eac87c41SEelco Chaudron
mask_array_rcu_cb(struct rcu_head * rcu)183eac87c41SEelco Chaudron static void mask_array_rcu_cb(struct rcu_head *rcu)
184eac87c41SEelco Chaudron {
185eac87c41SEelco Chaudron struct mask_array *ma = container_of(rcu, struct mask_array, rcu);
186eac87c41SEelco Chaudron
187eac87c41SEelco Chaudron __mask_array_destroy(ma);
188eac87c41SEelco Chaudron }
189eac87c41SEelco Chaudron
tbl_mask_array_reset_counters(struct mask_array * ma)190eac87c41SEelco Chaudron static void tbl_mask_array_reset_counters(struct mask_array *ma)
191eac87c41SEelco Chaudron {
192eac87c41SEelco Chaudron int i, cpu;
193eac87c41SEelco Chaudron
194eac87c41SEelco Chaudron /* As the per CPU counters are not atomic we can not go ahead and
195eac87c41SEelco Chaudron * reset them from another CPU. To be able to still have an approximate
196eac87c41SEelco Chaudron * zero based counter we store the value at reset, and subtract it
197eac87c41SEelco Chaudron * later when processing.
198eac87c41SEelco Chaudron */
199eac87c41SEelco Chaudron for (i = 0; i < ma->max; i++) {
200eac87c41SEelco Chaudron ma->masks_usage_zero_cntr[i] = 0;
201eac87c41SEelco Chaudron
202eac87c41SEelco Chaudron for_each_possible_cpu(cpu) {
203f981fc3dSEelco Chaudron struct mask_array_stats *stats;
204eac87c41SEelco Chaudron unsigned int start;
205eac87c41SEelco Chaudron u64 counter;
206eac87c41SEelco Chaudron
207f981fc3dSEelco Chaudron stats = per_cpu_ptr(ma->masks_usage_stats, cpu);
208eac87c41SEelco Chaudron do {
209d120d1a6SThomas Gleixner start = u64_stats_fetch_begin(&stats->syncp);
210f981fc3dSEelco Chaudron counter = stats->usage_cntrs[i];
211d120d1a6SThomas Gleixner } while (u64_stats_fetch_retry(&stats->syncp, start));
212eac87c41SEelco Chaudron
213eac87c41SEelco Chaudron ma->masks_usage_zero_cntr[i] += counter;
214eac87c41SEelco Chaudron }
215eac87c41SEelco Chaudron }
216eac87c41SEelco Chaudron }
217eac87c41SEelco Chaudron
tbl_mask_array_alloc(int size)2184bc63b1bSTonghao Zhang static struct mask_array *tbl_mask_array_alloc(int size)
2194bc63b1bSTonghao Zhang {
2204bc63b1bSTonghao Zhang struct mask_array *new;
2214bc63b1bSTonghao Zhang
2224bc63b1bSTonghao Zhang size = max(MASK_ARRAY_SIZE_MIN, size);
2234bc63b1bSTonghao Zhang new = kzalloc(sizeof(struct mask_array) +
224eac87c41SEelco Chaudron sizeof(struct sw_flow_mask *) * size +
225eac87c41SEelco Chaudron sizeof(u64) * size, GFP_KERNEL);
2264bc63b1bSTonghao Zhang if (!new)
2274bc63b1bSTonghao Zhang return NULL;
2284bc63b1bSTonghao Zhang
229eac87c41SEelco Chaudron new->masks_usage_zero_cntr = (u64 *)((u8 *)new +
230eac87c41SEelco Chaudron sizeof(struct mask_array) +
231eac87c41SEelco Chaudron sizeof(struct sw_flow_mask *) *
232eac87c41SEelco Chaudron size);
233eac87c41SEelco Chaudron
234f981fc3dSEelco Chaudron new->masks_usage_stats = __alloc_percpu(sizeof(struct mask_array_stats) +
235f981fc3dSEelco Chaudron sizeof(u64) * size,
236eac87c41SEelco Chaudron __alignof__(u64));
237f981fc3dSEelco Chaudron if (!new->masks_usage_stats) {
238eac87c41SEelco Chaudron kfree(new);
239eac87c41SEelco Chaudron return NULL;
240eac87c41SEelco Chaudron }
241eac87c41SEelco Chaudron
2424bc63b1bSTonghao Zhang new->count = 0;
2434bc63b1bSTonghao Zhang new->max = size;
2444bc63b1bSTonghao Zhang
2454bc63b1bSTonghao Zhang return new;
2464bc63b1bSTonghao Zhang }
2474bc63b1bSTonghao Zhang
tbl_mask_array_realloc(struct flow_table * tbl,int size)2484bc63b1bSTonghao Zhang static int tbl_mask_array_realloc(struct flow_table *tbl, int size)
2494bc63b1bSTonghao Zhang {
2504bc63b1bSTonghao Zhang struct mask_array *old;
2514bc63b1bSTonghao Zhang struct mask_array *new;
2524bc63b1bSTonghao Zhang
2534bc63b1bSTonghao Zhang new = tbl_mask_array_alloc(size);
2544bc63b1bSTonghao Zhang if (!new)
2554bc63b1bSTonghao Zhang return -ENOMEM;
2564bc63b1bSTonghao Zhang
2574bc63b1bSTonghao Zhang old = ovsl_dereference(tbl->mask_array);
2584bc63b1bSTonghao Zhang if (old) {
2594bc63b1bSTonghao Zhang int i;
2604bc63b1bSTonghao Zhang
2614bc63b1bSTonghao Zhang for (i = 0; i < old->max; i++) {
2624bc63b1bSTonghao Zhang if (ovsl_dereference(old->masks[i]))
2634bc63b1bSTonghao Zhang new->masks[new->count++] = old->masks[i];
2644bc63b1bSTonghao Zhang }
265eac87c41SEelco Chaudron call_rcu(&old->rcu, mask_array_rcu_cb);
2664bc63b1bSTonghao Zhang }
2674bc63b1bSTonghao Zhang
2684bc63b1bSTonghao Zhang rcu_assign_pointer(tbl->mask_array, new);
2694bc63b1bSTonghao Zhang
2704bc63b1bSTonghao Zhang return 0;
2714bc63b1bSTonghao Zhang }
2724bc63b1bSTonghao Zhang
tbl_mask_array_add_mask(struct flow_table * tbl,struct sw_flow_mask * new)27350b0e61bSTonghao Zhang static int tbl_mask_array_add_mask(struct flow_table *tbl,
27450b0e61bSTonghao Zhang struct sw_flow_mask *new)
27550b0e61bSTonghao Zhang {
27650b0e61bSTonghao Zhang struct mask_array *ma = ovsl_dereference(tbl->mask_array);
27750b0e61bSTonghao Zhang int err, ma_count = READ_ONCE(ma->count);
27850b0e61bSTonghao Zhang
27950b0e61bSTonghao Zhang if (ma_count >= ma->max) {
28050b0e61bSTonghao Zhang err = tbl_mask_array_realloc(tbl, ma->max +
28150b0e61bSTonghao Zhang MASK_ARRAY_SIZE_MIN);
28250b0e61bSTonghao Zhang if (err)
28350b0e61bSTonghao Zhang return err;
28450b0e61bSTonghao Zhang
28550b0e61bSTonghao Zhang ma = ovsl_dereference(tbl->mask_array);
286eac87c41SEelco Chaudron } else {
287eac87c41SEelco Chaudron /* On every add or delete we need to reset the counters so
288eac87c41SEelco Chaudron * every new mask gets a fair chance of being prioritized.
289eac87c41SEelco Chaudron */
290eac87c41SEelco Chaudron tbl_mask_array_reset_counters(ma);
29150b0e61bSTonghao Zhang }
29250b0e61bSTonghao Zhang
29350b0e61bSTonghao Zhang BUG_ON(ovsl_dereference(ma->masks[ma_count]));
29450b0e61bSTonghao Zhang
29550b0e61bSTonghao Zhang rcu_assign_pointer(ma->masks[ma_count], new);
29650b0e61bSTonghao Zhang WRITE_ONCE(ma->count, ma_count + 1);
29750b0e61bSTonghao Zhang
29850b0e61bSTonghao Zhang return 0;
29950b0e61bSTonghao Zhang }
30050b0e61bSTonghao Zhang
tbl_mask_array_del_mask(struct flow_table * tbl,struct sw_flow_mask * mask)30150b0e61bSTonghao Zhang static void tbl_mask_array_del_mask(struct flow_table *tbl,
30250b0e61bSTonghao Zhang struct sw_flow_mask *mask)
30350b0e61bSTonghao Zhang {
30450b0e61bSTonghao Zhang struct mask_array *ma = ovsl_dereference(tbl->mask_array);
30550b0e61bSTonghao Zhang int i, ma_count = READ_ONCE(ma->count);
30650b0e61bSTonghao Zhang
30750b0e61bSTonghao Zhang /* Remove the deleted mask pointers from the array */
30850b0e61bSTonghao Zhang for (i = 0; i < ma_count; i++) {
30950b0e61bSTonghao Zhang if (mask == ovsl_dereference(ma->masks[i]))
31050b0e61bSTonghao Zhang goto found;
31150b0e61bSTonghao Zhang }
31250b0e61bSTonghao Zhang
31350b0e61bSTonghao Zhang BUG();
31450b0e61bSTonghao Zhang return;
31550b0e61bSTonghao Zhang
31650b0e61bSTonghao Zhang found:
31750b0e61bSTonghao Zhang WRITE_ONCE(ma->count, ma_count - 1);
31850b0e61bSTonghao Zhang
31950b0e61bSTonghao Zhang rcu_assign_pointer(ma->masks[i], ma->masks[ma_count - 1]);
32050b0e61bSTonghao Zhang RCU_INIT_POINTER(ma->masks[ma_count - 1], NULL);
32150b0e61bSTonghao Zhang
32250b0e61bSTonghao Zhang kfree_rcu(mask, rcu);
32350b0e61bSTonghao Zhang
32450b0e61bSTonghao Zhang /* Shrink the mask array if necessary. */
32550b0e61bSTonghao Zhang if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) &&
32650b0e61bSTonghao Zhang ma_count <= (ma->max / 3))
32750b0e61bSTonghao Zhang tbl_mask_array_realloc(tbl, ma->max / 2);
328eac87c41SEelco Chaudron else
329eac87c41SEelco Chaudron tbl_mask_array_reset_counters(ma);
330eac87c41SEelco Chaudron
33150b0e61bSTonghao Zhang }
33250b0e61bSTonghao Zhang
33350b0e61bSTonghao Zhang /* Remove 'mask' from the mask list, if it is not needed any more. */
flow_mask_remove(struct flow_table * tbl,struct sw_flow_mask * mask)33450b0e61bSTonghao Zhang static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
33550b0e61bSTonghao Zhang {
33650b0e61bSTonghao Zhang if (mask) {
33750b0e61bSTonghao Zhang /* ovs-lock is required to protect mask-refcount and
33850b0e61bSTonghao Zhang * mask list.
33950b0e61bSTonghao Zhang */
34050b0e61bSTonghao Zhang ASSERT_OVSL();
34150b0e61bSTonghao Zhang BUG_ON(!mask->ref_count);
34250b0e61bSTonghao Zhang mask->ref_count--;
34350b0e61bSTonghao Zhang
34450b0e61bSTonghao Zhang if (!mask->ref_count)
34550b0e61bSTonghao Zhang tbl_mask_array_del_mask(tbl, mask);
34650b0e61bSTonghao Zhang }
34750b0e61bSTonghao Zhang }
34850b0e61bSTonghao Zhang
__mask_cache_destroy(struct mask_cache * mc)3499bf24f59SEelco Chaudron static void __mask_cache_destroy(struct mask_cache *mc)
3509bf24f59SEelco Chaudron {
3519bf24f59SEelco Chaudron free_percpu(mc->mask_cache);
3529bf24f59SEelco Chaudron kfree(mc);
3539bf24f59SEelco Chaudron }
3549bf24f59SEelco Chaudron
mask_cache_rcu_cb(struct rcu_head * rcu)3559bf24f59SEelco Chaudron static void mask_cache_rcu_cb(struct rcu_head *rcu)
3569bf24f59SEelco Chaudron {
3579bf24f59SEelco Chaudron struct mask_cache *mc = container_of(rcu, struct mask_cache, rcu);
3589bf24f59SEelco Chaudron
3599bf24f59SEelco Chaudron __mask_cache_destroy(mc);
3609bf24f59SEelco Chaudron }
3619bf24f59SEelco Chaudron
tbl_mask_cache_alloc(u32 size)3629bf24f59SEelco Chaudron static struct mask_cache *tbl_mask_cache_alloc(u32 size)
3639bf24f59SEelco Chaudron {
3649bf24f59SEelco Chaudron struct mask_cache_entry __percpu *cache = NULL;
3659bf24f59SEelco Chaudron struct mask_cache *new;
3669bf24f59SEelco Chaudron
3679bf24f59SEelco Chaudron /* Only allow size to be 0, or a power of 2, and does not exceed
3689bf24f59SEelco Chaudron * percpu allocation size.
3699bf24f59SEelco Chaudron */
3709bf24f59SEelco Chaudron if ((!is_power_of_2(size) && size != 0) ||
3719bf24f59SEelco Chaudron (size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE)
3729bf24f59SEelco Chaudron return NULL;
3739bf24f59SEelco Chaudron
3749bf24f59SEelco Chaudron new = kzalloc(sizeof(*new), GFP_KERNEL);
3759bf24f59SEelco Chaudron if (!new)
3769bf24f59SEelco Chaudron return NULL;
3779bf24f59SEelco Chaudron
3789bf24f59SEelco Chaudron new->cache_size = size;
3799bf24f59SEelco Chaudron if (new->cache_size > 0) {
3809bf24f59SEelco Chaudron cache = __alloc_percpu(array_size(sizeof(struct mask_cache_entry),
3819bf24f59SEelco Chaudron new->cache_size),
3829bf24f59SEelco Chaudron __alignof__(struct mask_cache_entry));
3839bf24f59SEelco Chaudron if (!cache) {
3849bf24f59SEelco Chaudron kfree(new);
3859bf24f59SEelco Chaudron return NULL;
3869bf24f59SEelco Chaudron }
3879bf24f59SEelco Chaudron }
3889bf24f59SEelco Chaudron
3899bf24f59SEelco Chaudron new->mask_cache = cache;
3909bf24f59SEelco Chaudron return new;
3919bf24f59SEelco Chaudron }
ovs_flow_tbl_masks_cache_resize(struct flow_table * table,u32 size)3929bf24f59SEelco Chaudron int ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size)
3939bf24f59SEelco Chaudron {
394fea07a48SEelco Chaudron struct mask_cache *mc = rcu_dereference_ovsl(table->mask_cache);
3959bf24f59SEelco Chaudron struct mask_cache *new;
3969bf24f59SEelco Chaudron
3979bf24f59SEelco Chaudron if (size == mc->cache_size)
3989bf24f59SEelco Chaudron return 0;
3999bf24f59SEelco Chaudron
4009bf24f59SEelco Chaudron if ((!is_power_of_2(size) && size != 0) ||
4019bf24f59SEelco Chaudron (size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE)
4029bf24f59SEelco Chaudron return -EINVAL;
4039bf24f59SEelco Chaudron
4049bf24f59SEelco Chaudron new = tbl_mask_cache_alloc(size);
4059bf24f59SEelco Chaudron if (!new)
4069bf24f59SEelco Chaudron return -ENOMEM;
4079bf24f59SEelco Chaudron
4089bf24f59SEelco Chaudron rcu_assign_pointer(table->mask_cache, new);
4099bf24f59SEelco Chaudron call_rcu(&mc->rcu, mask_cache_rcu_cb);
4109bf24f59SEelco Chaudron
4119bf24f59SEelco Chaudron return 0;
4129bf24f59SEelco Chaudron }
4139bf24f59SEelco Chaudron
ovs_flow_tbl_init(struct flow_table * table)414b637e498SPravin B Shelar int ovs_flow_tbl_init(struct flow_table *table)
415b637e498SPravin B Shelar {
41674ed7ab9SJoe Stringer struct table_instance *ti, *ufid_ti;
4179bf24f59SEelco Chaudron struct mask_cache *mc;
4184bc63b1bSTonghao Zhang struct mask_array *ma;
419b637e498SPravin B Shelar
4209bf24f59SEelco Chaudron mc = tbl_mask_cache_alloc(MC_DEFAULT_HASH_ENTRIES);
4219bf24f59SEelco Chaudron if (!mc)
422b637e498SPravin B Shelar return -ENOMEM;
423b637e498SPravin B Shelar
4244bc63b1bSTonghao Zhang ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
4254bc63b1bSTonghao Zhang if (!ma)
4264bc63b1bSTonghao Zhang goto free_mask_cache;
4274bc63b1bSTonghao Zhang
42804b7d136STonghao Zhang ti = table_instance_alloc(TBL_MIN_BUCKETS);
42904b7d136STonghao Zhang if (!ti)
4304bc63b1bSTonghao Zhang goto free_mask_array;
43104b7d136STonghao Zhang
43274ed7ab9SJoe Stringer ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
43374ed7ab9SJoe Stringer if (!ufid_ti)
43474ed7ab9SJoe Stringer goto free_ti;
43574ed7ab9SJoe Stringer
436b637e498SPravin B Shelar rcu_assign_pointer(table->ti, ti);
43774ed7ab9SJoe Stringer rcu_assign_pointer(table->ufid_ti, ufid_ti);
4384bc63b1bSTonghao Zhang rcu_assign_pointer(table->mask_array, ma);
4399bf24f59SEelco Chaudron rcu_assign_pointer(table->mask_cache, mc);
440b637e498SPravin B Shelar table->last_rehash = jiffies;
441e6445719SPravin B Shelar table->count = 0;
44274ed7ab9SJoe Stringer table->ufid_count = 0;
443b637e498SPravin B Shelar return 0;
44474ed7ab9SJoe Stringer
44574ed7ab9SJoe Stringer free_ti:
44674ed7ab9SJoe Stringer __table_instance_destroy(ti);
4474bc63b1bSTonghao Zhang free_mask_array:
448eac87c41SEelco Chaudron __mask_array_destroy(ma);
44904b7d136STonghao Zhang free_mask_cache:
4509bf24f59SEelco Chaudron __mask_cache_destroy(mc);
45174ed7ab9SJoe Stringer return -ENOMEM;
452e6445719SPravin B Shelar }
453e6445719SPravin B Shelar
flow_tbl_destroy_rcu_cb(struct rcu_head * rcu)454e6445719SPravin B Shelar static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
455e6445719SPravin B Shelar {
456cf3266adSTonghao Zhang struct table_instance *ti;
457e6445719SPravin B Shelar
458cf3266adSTonghao Zhang ti = container_of(rcu, struct table_instance, rcu);
459b637e498SPravin B Shelar __table_instance_destroy(ti);
460b637e498SPravin B Shelar }
461b637e498SPravin B Shelar
table_instance_flow_free(struct flow_table * table,struct table_instance * ti,struct table_instance * ufid_ti,struct sw_flow * flow)46250b0e61bSTonghao Zhang static void table_instance_flow_free(struct flow_table *table,
46350b0e61bSTonghao Zhang struct table_instance *ti,
46450b0e61bSTonghao Zhang struct table_instance *ufid_ti,
465df68d64eSTonghao Zhang struct sw_flow *flow)
46650b0e61bSTonghao Zhang {
46750b0e61bSTonghao Zhang hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
46850b0e61bSTonghao Zhang table->count--;
46950b0e61bSTonghao Zhang
47050b0e61bSTonghao Zhang if (ovs_identifier_is_ufid(&flow->id)) {
47150b0e61bSTonghao Zhang hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
47250b0e61bSTonghao Zhang table->ufid_count--;
47350b0e61bSTonghao Zhang }
47450b0e61bSTonghao Zhang
47550b0e61bSTonghao Zhang flow_mask_remove(table, flow->mask);
47650b0e61bSTonghao Zhang }
47750b0e61bSTonghao Zhang
4781f3a090bSTonghao Zhang /* Must be called with OVS mutex held. */
table_instance_flow_flush(struct flow_table * table,struct table_instance * ti,struct table_instance * ufid_ti)4791f3a090bSTonghao Zhang void table_instance_flow_flush(struct flow_table *table,
48050b0e61bSTonghao Zhang struct table_instance *ti,
4811f3a090bSTonghao Zhang struct table_instance *ufid_ti)
482b637e498SPravin B Shelar {
483e80857ccSAndy Zhou int i;
484e80857ccSAndy Zhou
485e80857ccSAndy Zhou for (i = 0; i < ti->n_buckets; i++) {
486ee9c5e67SKent Overstreet struct hlist_head *head = &ti->buckets[i];
487e80857ccSAndy Zhou struct hlist_node *n;
488cf3266adSTonghao Zhang struct sw_flow *flow;
489e80857ccSAndy Zhou
49050b0e61bSTonghao Zhang hlist_for_each_entry_safe(flow, n, head,
49150b0e61bSTonghao Zhang flow_table.node[ti->node_ver]) {
49250b0e61bSTonghao Zhang
49350b0e61bSTonghao Zhang table_instance_flow_free(table, ti, ufid_ti,
494df68d64eSTonghao Zhang flow);
4951f3a090bSTonghao Zhang ovs_flow_free(flow, true);
4961f3a090bSTonghao Zhang }
497e80857ccSAndy Zhou }
498df68d64eSTonghao Zhang
499df68d64eSTonghao Zhang if (WARN_ON(table->count != 0 ||
500df68d64eSTonghao Zhang table->ufid_count != 0)) {
501df68d64eSTonghao Zhang table->count = 0;
502df68d64eSTonghao Zhang table->ufid_count = 0;
503df68d64eSTonghao Zhang }
504e80857ccSAndy Zhou }
505e80857ccSAndy Zhou
table_instance_destroy(struct table_instance * ti,struct table_instance * ufid_ti)5061f3a090bSTonghao Zhang static void table_instance_destroy(struct table_instance *ti,
5071f3a090bSTonghao Zhang struct table_instance *ufid_ti)
5081f3a090bSTonghao Zhang {
509b637e498SPravin B Shelar call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
51074ed7ab9SJoe Stringer call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
511e6445719SPravin B Shelar }
512e6445719SPravin B Shelar
5139b996e54SPravin B Shelar /* No need for locking this function is called from RCU callback or
5149b996e54SPravin B Shelar * error path.
5159b996e54SPravin B Shelar */
ovs_flow_tbl_destroy(struct flow_table * table)5169b996e54SPravin B Shelar void ovs_flow_tbl_destroy(struct flow_table *table)
517e6445719SPravin B Shelar {
5189b996e54SPravin B Shelar struct table_instance *ti = rcu_dereference_raw(table->ti);
51974ed7ab9SJoe Stringer struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
5205845589eSTonghao Zhang struct mask_cache *mc = rcu_dereference_raw(table->mask_cache);
5215845589eSTonghao Zhang struct mask_array *ma = rcu_dereference_raw(table->mask_array);
522e6445719SPravin B Shelar
5239bf24f59SEelco Chaudron call_rcu(&mc->rcu, mask_cache_rcu_cb);
5249bf24f59SEelco Chaudron call_rcu(&ma->rcu, mask_array_rcu_cb);
5251f3a090bSTonghao Zhang table_instance_destroy(ti, ufid_ti);
526e6445719SPravin B Shelar }
527e6445719SPravin B Shelar
ovs_flow_tbl_dump_next(struct table_instance * ti,u32 * bucket,u32 * last)528b637e498SPravin B Shelar struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
529e6445719SPravin B Shelar u32 *bucket, u32 *last)
530e6445719SPravin B Shelar {
531e6445719SPravin B Shelar struct sw_flow *flow;
532e6445719SPravin B Shelar struct hlist_head *head;
533e6445719SPravin B Shelar int ver;
534e6445719SPravin B Shelar int i;
535e6445719SPravin B Shelar
536b637e498SPravin B Shelar ver = ti->node_ver;
537b637e498SPravin B Shelar while (*bucket < ti->n_buckets) {
538e6445719SPravin B Shelar i = 0;
539ee9c5e67SKent Overstreet head = &ti->buckets[*bucket];
54074ed7ab9SJoe Stringer hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
541e6445719SPravin B Shelar if (i < *last) {
542e6445719SPravin B Shelar i++;
543e6445719SPravin B Shelar continue;
544e6445719SPravin B Shelar }
545e6445719SPravin B Shelar *last = i + 1;
546e6445719SPravin B Shelar return flow;
547e6445719SPravin B Shelar }
548e6445719SPravin B Shelar (*bucket)++;
549e6445719SPravin B Shelar *last = 0;
550e6445719SPravin B Shelar }
551e6445719SPravin B Shelar
552e6445719SPravin B Shelar return NULL;
553e6445719SPravin B Shelar }
554e6445719SPravin B Shelar
find_bucket(struct table_instance * ti,u32 hash)555b637e498SPravin B Shelar static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
556e6445719SPravin B Shelar {
557b637e498SPravin B Shelar hash = jhash_1word(hash, ti->hash_seed);
558ee9c5e67SKent Overstreet return &ti->buckets[hash & (ti->n_buckets - 1)];
559e6445719SPravin B Shelar }
560e6445719SPravin B Shelar
table_instance_insert(struct table_instance * ti,struct sw_flow * flow)56174ed7ab9SJoe Stringer static void table_instance_insert(struct table_instance *ti,
56274ed7ab9SJoe Stringer struct sw_flow *flow)
563e6445719SPravin B Shelar {
564e6445719SPravin B Shelar struct hlist_head *head;
565e6445719SPravin B Shelar
56674ed7ab9SJoe Stringer head = find_bucket(ti, flow->flow_table.hash);
56774ed7ab9SJoe Stringer hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
56874ed7ab9SJoe Stringer }
56974ed7ab9SJoe Stringer
ufid_table_instance_insert(struct table_instance * ti,struct sw_flow * flow)57074ed7ab9SJoe Stringer static void ufid_table_instance_insert(struct table_instance *ti,
57174ed7ab9SJoe Stringer struct sw_flow *flow)
57274ed7ab9SJoe Stringer {
57374ed7ab9SJoe Stringer struct hlist_head *head;
57474ed7ab9SJoe Stringer
57574ed7ab9SJoe Stringer head = find_bucket(ti, flow->ufid_table.hash);
57674ed7ab9SJoe Stringer hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
577e6445719SPravin B Shelar }
578e6445719SPravin B Shelar
flow_table_copy_flows(struct table_instance * old,struct table_instance * new,bool ufid)579b637e498SPravin B Shelar static void flow_table_copy_flows(struct table_instance *old,
58074ed7ab9SJoe Stringer struct table_instance *new, bool ufid)
581e6445719SPravin B Shelar {
582e6445719SPravin B Shelar int old_ver;
583e6445719SPravin B Shelar int i;
584e6445719SPravin B Shelar
585e6445719SPravin B Shelar old_ver = old->node_ver;
586e6445719SPravin B Shelar new->node_ver = !old_ver;
587e6445719SPravin B Shelar
588e6445719SPravin B Shelar /* Insert in new table. */
589e6445719SPravin B Shelar for (i = 0; i < old->n_buckets; i++) {
590e6445719SPravin B Shelar struct sw_flow *flow;
591ee9c5e67SKent Overstreet struct hlist_head *head = &old->buckets[i];
592e6445719SPravin B Shelar
59374ed7ab9SJoe Stringer if (ufid)
59464948427STonghao Zhang hlist_for_each_entry_rcu(flow, head,
59564948427STonghao Zhang ufid_table.node[old_ver],
59664948427STonghao Zhang lockdep_ovsl_is_held())
59774ed7ab9SJoe Stringer ufid_table_instance_insert(new, flow);
59874ed7ab9SJoe Stringer else
59964948427STonghao Zhang hlist_for_each_entry_rcu(flow, head,
60064948427STonghao Zhang flow_table.node[old_ver],
60164948427STonghao Zhang lockdep_ovsl_is_held())
602b637e498SPravin B Shelar table_instance_insert(new, flow);
603e6445719SPravin B Shelar }
604e6445719SPravin B Shelar }
605e6445719SPravin B Shelar
table_instance_rehash(struct table_instance * ti,int n_buckets,bool ufid)606b637e498SPravin B Shelar static struct table_instance *table_instance_rehash(struct table_instance *ti,
60774ed7ab9SJoe Stringer int n_buckets, bool ufid)
608e6445719SPravin B Shelar {
609b637e498SPravin B Shelar struct table_instance *new_ti;
610e6445719SPravin B Shelar
611b637e498SPravin B Shelar new_ti = table_instance_alloc(n_buckets);
612b637e498SPravin B Shelar if (!new_ti)
613618ed0c8SPravin B Shelar return NULL;
614e6445719SPravin B Shelar
61574ed7ab9SJoe Stringer flow_table_copy_flows(ti, new_ti, ufid);
616e6445719SPravin B Shelar
617b637e498SPravin B Shelar return new_ti;
618e6445719SPravin B Shelar }
619e6445719SPravin B Shelar
ovs_flow_tbl_flush(struct flow_table * flow_table)620b637e498SPravin B Shelar int ovs_flow_tbl_flush(struct flow_table *flow_table)
621e6445719SPravin B Shelar {
62274ed7ab9SJoe Stringer struct table_instance *old_ti, *new_ti;
62374ed7ab9SJoe Stringer struct table_instance *old_ufid_ti, *new_ufid_ti;
624e6445719SPravin B Shelar
625b637e498SPravin B Shelar new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
626b637e498SPravin B Shelar if (!new_ti)
627b637e498SPravin B Shelar return -ENOMEM;
62874ed7ab9SJoe Stringer new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
62974ed7ab9SJoe Stringer if (!new_ufid_ti)
63074ed7ab9SJoe Stringer goto err_free_ti;
63174ed7ab9SJoe Stringer
63274ed7ab9SJoe Stringer old_ti = ovsl_dereference(flow_table->ti);
63374ed7ab9SJoe Stringer old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
634b637e498SPravin B Shelar
635b637e498SPravin B Shelar rcu_assign_pointer(flow_table->ti, new_ti);
63674ed7ab9SJoe Stringer rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
637b637e498SPravin B Shelar flow_table->last_rehash = jiffies;
638b637e498SPravin B Shelar
6391f3a090bSTonghao Zhang table_instance_flow_flush(flow_table, old_ti, old_ufid_ti);
6401f3a090bSTonghao Zhang table_instance_destroy(old_ti, old_ufid_ti);
641b637e498SPravin B Shelar return 0;
64274ed7ab9SJoe Stringer
64374ed7ab9SJoe Stringer err_free_ti:
64474ed7ab9SJoe Stringer __table_instance_destroy(new_ti);
64574ed7ab9SJoe Stringer return -ENOMEM;
646e6445719SPravin B Shelar }
647e6445719SPravin B Shelar
flow_hash(const struct sw_flow_key * key,const struct sw_flow_key_range * range)648272c2cf8SJoe Stringer static u32 flow_hash(const struct sw_flow_key *key,
649272c2cf8SJoe Stringer const struct sw_flow_key_range *range)
650e6445719SPravin B Shelar {
651515b65a4STonghao Zhang const u32 *hash_key = (const u32 *)((const u8 *)key + range->start);
652e6445719SPravin B Shelar
653e6445719SPravin B Shelar /* Make sure number of hash bytes are multiple of u32. */
654515b65a4STonghao Zhang int hash_u32s = range_n_bytes(range) >> 2;
655e6445719SPravin B Shelar
65687545899SDaniel Borkmann return jhash2(hash_key, hash_u32s, 0);
657e6445719SPravin B Shelar }
658e6445719SPravin B Shelar
flow_key_start(const struct sw_flow_key * key)659e6445719SPravin B Shelar static int flow_key_start(const struct sw_flow_key *key)
660e6445719SPravin B Shelar {
66100a93babSJiri Benc if (key->tun_proto)
662e6445719SPravin B Shelar return 0;
663e6445719SPravin B Shelar else
664e6445719SPravin B Shelar return rounddown(offsetof(struct sw_flow_key, phy),
665e6445719SPravin B Shelar sizeof(long));
666e6445719SPravin B Shelar }
667e6445719SPravin B Shelar
cmp_key(const struct sw_flow_key * key1,const struct sw_flow_key * key2,int key_start,int key_end)668e6445719SPravin B Shelar static bool cmp_key(const struct sw_flow_key *key1,
669e6445719SPravin B Shelar const struct sw_flow_key *key2,
670e6445719SPravin B Shelar int key_start, int key_end)
671e6445719SPravin B Shelar {
6727085130bSDaniele Di Proietto const long *cp1 = (const long *)((const u8 *)key1 + key_start);
6737085130bSDaniele Di Proietto const long *cp2 = (const long *)((const u8 *)key2 + key_start);
674e6445719SPravin B Shelar int i;
675e6445719SPravin B Shelar
676e6445719SPravin B Shelar for (i = key_start; i < key_end; i += sizeof(long))
677b1811447SBaowen Zheng if (*cp1++ ^ *cp2++)
678b1811447SBaowen Zheng return false;
679e6445719SPravin B Shelar
680b1811447SBaowen Zheng return true;
681e6445719SPravin B Shelar }
682e6445719SPravin B Shelar
flow_cmp_masked_key(const struct sw_flow * flow,const struct sw_flow_key * key,const struct sw_flow_key_range * range)683e6445719SPravin B Shelar static bool flow_cmp_masked_key(const struct sw_flow *flow,
684e6445719SPravin B Shelar const struct sw_flow_key *key,
685272c2cf8SJoe Stringer const struct sw_flow_key_range *range)
686e6445719SPravin B Shelar {
687272c2cf8SJoe Stringer return cmp_key(&flow->key, key, range->start, range->end);
688e6445719SPravin B Shelar }
689e6445719SPravin B Shelar
ovs_flow_cmp_unmasked_key(const struct sw_flow * flow,const struct sw_flow_match * match)69074ed7ab9SJoe Stringer static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
69112eb18f7SThomas Graf const struct sw_flow_match *match)
692e6445719SPravin B Shelar {
693e6445719SPravin B Shelar struct sw_flow_key *key = match->key;
694e6445719SPravin B Shelar int key_start = flow_key_start(key);
695e6445719SPravin B Shelar int key_end = match->range.end;
696e6445719SPravin B Shelar
69774ed7ab9SJoe Stringer BUG_ON(ovs_identifier_is_ufid(&flow->id));
69874ed7ab9SJoe Stringer return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
699e6445719SPravin B Shelar }
700e6445719SPravin B Shelar
masked_flow_lookup(struct table_instance * ti,const struct sw_flow_key * unmasked,const struct sw_flow_mask * mask,u32 * n_mask_hit)701b637e498SPravin B Shelar static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
702e6445719SPravin B Shelar const struct sw_flow_key *unmasked,
70304b7d136STonghao Zhang const struct sw_flow_mask *mask,
70404b7d136STonghao Zhang u32 *n_mask_hit)
705e6445719SPravin B Shelar {
706e6445719SPravin B Shelar struct sw_flow *flow;
707e6445719SPravin B Shelar struct hlist_head *head;
708e6445719SPravin B Shelar u32 hash;
709e6445719SPravin B Shelar struct sw_flow_key masked_key;
710e6445719SPravin B Shelar
711ae5f2fb1SJesse Gross ovs_flow_mask_key(&masked_key, unmasked, false, mask);
712272c2cf8SJoe Stringer hash = flow_hash(&masked_key, &mask->range);
713b637e498SPravin B Shelar head = find_bucket(ti, hash);
71404b7d136STonghao Zhang (*n_mask_hit)++;
71504b7d136STonghao Zhang
716a2cfb96cSMadhuparna Bhowmik hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver],
717a2cfb96cSMadhuparna Bhowmik lockdep_ovsl_is_held()) {
71874ed7ab9SJoe Stringer if (flow->mask == mask && flow->flow_table.hash == hash &&
719272c2cf8SJoe Stringer flow_cmp_masked_key(flow, &masked_key, &mask->range))
720e6445719SPravin B Shelar return flow;
721e6445719SPravin B Shelar }
722e6445719SPravin B Shelar return NULL;
723e6445719SPravin B Shelar }
724e6445719SPravin B Shelar
725a7f35e78STonghao Zhang /* Flow lookup does full lookup on flow table. It starts with
726a7f35e78STonghao Zhang * mask from index passed in *index.
727f981fc3dSEelco Chaudron * This function MUST be called with BH disabled due to the use
728f981fc3dSEelco Chaudron * of CPU specific variables.
729a7f35e78STonghao Zhang */
flow_lookup(struct flow_table * tbl,struct table_instance * ti,struct mask_array * ma,const struct sw_flow_key * key,u32 * n_mask_hit,u32 * n_cache_hit,u32 * index)73004b7d136STonghao Zhang static struct sw_flow *flow_lookup(struct flow_table *tbl,
73104b7d136STonghao Zhang struct table_instance *ti,
7324bc63b1bSTonghao Zhang struct mask_array *ma,
7331bd7116fSAndy Zhou const struct sw_flow_key *key,
7344bc63b1bSTonghao Zhang u32 *n_mask_hit,
7359d2f627bSEelco Chaudron u32 *n_cache_hit,
7364bc63b1bSTonghao Zhang u32 *index)
737e6445719SPravin B Shelar {
738f981fc3dSEelco Chaudron struct mask_array_stats *stats = this_cpu_ptr(ma->masks_usage_stats);
739b637e498SPravin B Shelar struct sw_flow *flow;
74057f7d7b9STonghao Zhang struct sw_flow_mask *mask;
7414bc63b1bSTonghao Zhang int i;
742e6445719SPravin B Shelar
7430a3e0137STonghao Zhang if (likely(*index < ma->max)) {
744a7f35e78STonghao Zhang mask = rcu_dereference_ovsl(ma->masks[*index]);
745a7f35e78STonghao Zhang if (mask) {
746a7f35e78STonghao Zhang flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
747eac87c41SEelco Chaudron if (flow) {
748f981fc3dSEelco Chaudron u64_stats_update_begin(&stats->syncp);
749f981fc3dSEelco Chaudron stats->usage_cntrs[*index]++;
750f981fc3dSEelco Chaudron u64_stats_update_end(&stats->syncp);
7519d2f627bSEelco Chaudron (*n_cache_hit)++;
752a7f35e78STonghao Zhang return flow;
753a7f35e78STonghao Zhang }
754a7f35e78STonghao Zhang }
755eac87c41SEelco Chaudron }
756a7f35e78STonghao Zhang
7574bc63b1bSTonghao Zhang for (i = 0; i < ma->max; i++) {
758a7f35e78STonghao Zhang
759a7f35e78STonghao Zhang if (i == *index)
760a7f35e78STonghao Zhang continue;
7614bc63b1bSTonghao Zhang
7624bc63b1bSTonghao Zhang mask = rcu_dereference_ovsl(ma->masks[i]);
7630a3e0137STonghao Zhang if (unlikely(!mask))
76457f7d7b9STonghao Zhang break;
765a7f35e78STonghao Zhang
76604b7d136STonghao Zhang flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
7674bc63b1bSTonghao Zhang if (flow) { /* Found */
7684bc63b1bSTonghao Zhang *index = i;
769f981fc3dSEelco Chaudron u64_stats_update_begin(&stats->syncp);
770f981fc3dSEelco Chaudron stats->usage_cntrs[*index]++;
771f981fc3dSEelco Chaudron u64_stats_update_end(&stats->syncp);
772b637e498SPravin B Shelar return flow;
773b637e498SPravin B Shelar }
7744bc63b1bSTonghao Zhang }
7754bc63b1bSTonghao Zhang
776b637e498SPravin B Shelar return NULL;
777e6445719SPravin B Shelar }
778e6445719SPravin B Shelar
77904b7d136STonghao Zhang /*
78004b7d136STonghao Zhang * mask_cache maps flow to probable mask. This cache is not tightly
78104b7d136STonghao Zhang * coupled cache, It means updates to mask list can result in inconsistent
78204b7d136STonghao Zhang * cache entry in mask cache.
78304b7d136STonghao Zhang * This is per cpu cache and is divided in MC_HASH_SEGS segments.
78404b7d136STonghao Zhang * In case of a hash collision the entry is hashed in next segment.
78504b7d136STonghao Zhang * */
ovs_flow_tbl_lookup_stats(struct flow_table * tbl,const struct sw_flow_key * key,u32 skb_hash,u32 * n_mask_hit,u32 * n_cache_hit)78604b7d136STonghao Zhang struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
78704b7d136STonghao Zhang const struct sw_flow_key *key,
78804b7d136STonghao Zhang u32 skb_hash,
7899d2f627bSEelco Chaudron u32 *n_mask_hit,
7909d2f627bSEelco Chaudron u32 *n_cache_hit)
79104b7d136STonghao Zhang {
7929bf24f59SEelco Chaudron struct mask_cache *mc = rcu_dereference(tbl->mask_cache);
793a7f35e78STonghao Zhang struct mask_array *ma = rcu_dereference(tbl->mask_array);
794a7f35e78STonghao Zhang struct table_instance *ti = rcu_dereference(tbl->ti);
795a7f35e78STonghao Zhang struct mask_cache_entry *entries, *ce;
79604b7d136STonghao Zhang struct sw_flow *flow;
797a7f35e78STonghao Zhang u32 hash;
79804b7d136STonghao Zhang int seg;
79904b7d136STonghao Zhang
80004b7d136STonghao Zhang *n_mask_hit = 0;
8019d2f627bSEelco Chaudron *n_cache_hit = 0;
8029bf24f59SEelco Chaudron if (unlikely(!skb_hash || mc->cache_size == 0)) {
803a7f35e78STonghao Zhang u32 mask_index = 0;
8049d2f627bSEelco Chaudron u32 cache = 0;
8054bc63b1bSTonghao Zhang
8069d2f627bSEelco Chaudron return flow_lookup(tbl, ti, ma, key, n_mask_hit, &cache,
8079d2f627bSEelco Chaudron &mask_index);
8084bc63b1bSTonghao Zhang }
80904b7d136STonghao Zhang
810a7f35e78STonghao Zhang /* Pre and post recirulation flows usually have the same skb_hash
811a7f35e78STonghao Zhang * value. To avoid hash collisions, rehash the 'skb_hash' with
812a7f35e78STonghao Zhang * 'recirc_id'. */
813a7f35e78STonghao Zhang if (key->recirc_id)
814a7f35e78STonghao Zhang skb_hash = jhash_1word(skb_hash, key->recirc_id);
815a7f35e78STonghao Zhang
816a7f35e78STonghao Zhang ce = NULL;
817a7f35e78STonghao Zhang hash = skb_hash;
8189bf24f59SEelco Chaudron entries = this_cpu_ptr(mc->mask_cache);
81904b7d136STonghao Zhang
820a7f35e78STonghao Zhang /* Find the cache entry 'ce' to operate on. */
82104b7d136STonghao Zhang for (seg = 0; seg < MC_HASH_SEGS; seg++) {
8229bf24f59SEelco Chaudron int index = hash & (mc->cache_size - 1);
823a7f35e78STonghao Zhang struct mask_cache_entry *e;
82404b7d136STonghao Zhang
825a7f35e78STonghao Zhang e = &entries[index];
826a7f35e78STonghao Zhang if (e->skb_hash == skb_hash) {
827a7f35e78STonghao Zhang flow = flow_lookup(tbl, ti, ma, key, n_mask_hit,
8289d2f627bSEelco Chaudron n_cache_hit, &e->mask_index);
829a7f35e78STonghao Zhang if (!flow)
830a7f35e78STonghao Zhang e->skb_hash = 0;
83104b7d136STonghao Zhang return flow;
83204b7d136STonghao Zhang }
83304b7d136STonghao Zhang
834a7f35e78STonghao Zhang if (!ce || e->skb_hash < ce->skb_hash)
835a7f35e78STonghao Zhang ce = e; /* A better replacement cache candidate. */
83604b7d136STonghao Zhang
83704b7d136STonghao Zhang hash >>= MC_HASH_SHIFT;
83804b7d136STonghao Zhang }
83904b7d136STonghao Zhang
840a7f35e78STonghao Zhang /* Cache miss, do full lookup. */
8419d2f627bSEelco Chaudron flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, n_cache_hit,
8429d2f627bSEelco Chaudron &ce->mask_index);
8434bc63b1bSTonghao Zhang if (flow)
844a7f35e78STonghao Zhang ce->skb_hash = skb_hash;
84504b7d136STonghao Zhang
8469d2f627bSEelco Chaudron *n_cache_hit = 0;
84704b7d136STonghao Zhang return flow;
84804b7d136STonghao Zhang }
84904b7d136STonghao Zhang
ovs_flow_tbl_lookup(struct flow_table * tbl,const struct sw_flow_key * key)8505bb50632SAndy Zhou struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
8515bb50632SAndy Zhou const struct sw_flow_key *key)
8525bb50632SAndy Zhou {
85304b7d136STonghao Zhang struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
8544bc63b1bSTonghao Zhang struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
8554bc63b1bSTonghao Zhang u32 __always_unused n_mask_hit;
8569d2f627bSEelco Chaudron u32 __always_unused n_cache_hit;
857f981fc3dSEelco Chaudron struct sw_flow *flow;
858a7f35e78STonghao Zhang u32 index = 0;
8594bc63b1bSTonghao Zhang
860f981fc3dSEelco Chaudron /* This function gets called trough the netlink interface and therefore
861f981fc3dSEelco Chaudron * is preemptible. However, flow_lookup() function needs to be called
862f981fc3dSEelco Chaudron * with BH disabled due to CPU specific variables.
863f981fc3dSEelco Chaudron */
864f981fc3dSEelco Chaudron local_bh_disable();
865f981fc3dSEelco Chaudron flow = flow_lookup(tbl, ti, ma, key, &n_mask_hit, &n_cache_hit, &index);
866f981fc3dSEelco Chaudron local_bh_enable();
867f981fc3dSEelco Chaudron return flow;
8685bb50632SAndy Zhou }
8695bb50632SAndy Zhou
ovs_flow_tbl_lookup_exact(struct flow_table * tbl,const struct sw_flow_match * match)8704a46b24eSAlex Wang struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
87112eb18f7SThomas Graf const struct sw_flow_match *match)
8724a46b24eSAlex Wang {
8734bc63b1bSTonghao Zhang struct mask_array *ma = ovsl_dereference(tbl->mask_array);
8744bc63b1bSTonghao Zhang int i;
8754a46b24eSAlex Wang
8764a46b24eSAlex Wang /* Always called under ovs-mutex. */
8774bc63b1bSTonghao Zhang for (i = 0; i < ma->max; i++) {
8784bc63b1bSTonghao Zhang struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
8794bc63b1bSTonghao Zhang u32 __always_unused n_mask_hit;
8804bc63b1bSTonghao Zhang struct sw_flow_mask *mask;
8814bc63b1bSTonghao Zhang struct sw_flow *flow;
8824bc63b1bSTonghao Zhang
8834bc63b1bSTonghao Zhang mask = ovsl_dereference(ma->masks[i]);
8844bc63b1bSTonghao Zhang if (!mask)
8854bc63b1bSTonghao Zhang continue;
8864bc63b1bSTonghao Zhang
88704b7d136STonghao Zhang flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit);
88874ed7ab9SJoe Stringer if (flow && ovs_identifier_is_key(&flow->id) &&
8894bc63b1bSTonghao Zhang ovs_flow_cmp_unmasked_key(flow, match)) {
89074ed7ab9SJoe Stringer return flow;
89174ed7ab9SJoe Stringer }
8924bc63b1bSTonghao Zhang }
8934bc63b1bSTonghao Zhang
89474ed7ab9SJoe Stringer return NULL;
89574ed7ab9SJoe Stringer }
89674ed7ab9SJoe Stringer
ufid_hash(const struct sw_flow_id * sfid)89774ed7ab9SJoe Stringer static u32 ufid_hash(const struct sw_flow_id *sfid)
89874ed7ab9SJoe Stringer {
89974ed7ab9SJoe Stringer return jhash(sfid->ufid, sfid->ufid_len, 0);
90074ed7ab9SJoe Stringer }
90174ed7ab9SJoe Stringer
ovs_flow_cmp_ufid(const struct sw_flow * flow,const struct sw_flow_id * sfid)90274ed7ab9SJoe Stringer static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
90374ed7ab9SJoe Stringer const struct sw_flow_id *sfid)
90474ed7ab9SJoe Stringer {
90574ed7ab9SJoe Stringer if (flow->id.ufid_len != sfid->ufid_len)
90674ed7ab9SJoe Stringer return false;
90774ed7ab9SJoe Stringer
90874ed7ab9SJoe Stringer return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
90974ed7ab9SJoe Stringer }
91074ed7ab9SJoe Stringer
ovs_flow_cmp(const struct sw_flow * flow,const struct sw_flow_match * match)911cf3266adSTonghao Zhang bool ovs_flow_cmp(const struct sw_flow *flow,
912cf3266adSTonghao Zhang const struct sw_flow_match *match)
91374ed7ab9SJoe Stringer {
91474ed7ab9SJoe Stringer if (ovs_identifier_is_ufid(&flow->id))
91574ed7ab9SJoe Stringer return flow_cmp_masked_key(flow, match->key, &match->range);
91674ed7ab9SJoe Stringer
91774ed7ab9SJoe Stringer return ovs_flow_cmp_unmasked_key(flow, match);
91874ed7ab9SJoe Stringer }
91974ed7ab9SJoe Stringer
ovs_flow_tbl_lookup_ufid(struct flow_table * tbl,const struct sw_flow_id * ufid)92074ed7ab9SJoe Stringer struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
92174ed7ab9SJoe Stringer const struct sw_flow_id *ufid)
92274ed7ab9SJoe Stringer {
92374ed7ab9SJoe Stringer struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
92474ed7ab9SJoe Stringer struct sw_flow *flow;
92574ed7ab9SJoe Stringer struct hlist_head *head;
92674ed7ab9SJoe Stringer u32 hash;
92774ed7ab9SJoe Stringer
92874ed7ab9SJoe Stringer hash = ufid_hash(ufid);
92974ed7ab9SJoe Stringer head = find_bucket(ti, hash);
930a2cfb96cSMadhuparna Bhowmik hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver],
931a2cfb96cSMadhuparna Bhowmik lockdep_ovsl_is_held()) {
93274ed7ab9SJoe Stringer if (flow->ufid_table.hash == hash &&
93374ed7ab9SJoe Stringer ovs_flow_cmp_ufid(flow, ufid))
9344a46b24eSAlex Wang return flow;
9354a46b24eSAlex Wang }
9364a46b24eSAlex Wang return NULL;
9374a46b24eSAlex Wang }
9384a46b24eSAlex Wang
ovs_flow_tbl_num_masks(const struct flow_table * table)9391bd7116fSAndy Zhou int ovs_flow_tbl_num_masks(const struct flow_table *table)
9401bd7116fSAndy Zhou {
9414bc63b1bSTonghao Zhang struct mask_array *ma = rcu_dereference_ovsl(table->mask_array);
94257f7d7b9STonghao Zhang return READ_ONCE(ma->count);
9431bd7116fSAndy Zhou }
9441bd7116fSAndy Zhou
ovs_flow_tbl_masks_cache_size(const struct flow_table * table)9459bf24f59SEelco Chaudron u32 ovs_flow_tbl_masks_cache_size(const struct flow_table *table)
9469bf24f59SEelco Chaudron {
9475845589eSTonghao Zhang struct mask_cache *mc = rcu_dereference_ovsl(table->mask_cache);
9489bf24f59SEelco Chaudron
9499bf24f59SEelco Chaudron return READ_ONCE(mc->cache_size);
9509bf24f59SEelco Chaudron }
9519bf24f59SEelco Chaudron
table_instance_expand(struct table_instance * ti,bool ufid)95274ed7ab9SJoe Stringer static struct table_instance *table_instance_expand(struct table_instance *ti,
95374ed7ab9SJoe Stringer bool ufid)
954b637e498SPravin B Shelar {
95574ed7ab9SJoe Stringer return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
956e6445719SPravin B Shelar }
957e6445719SPravin B Shelar
95856c19868SJarno Rajahalme /* Must be called with OVS mutex held. */
ovs_flow_tbl_remove(struct flow_table * table,struct sw_flow * flow)959e6445719SPravin B Shelar void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
960e6445719SPravin B Shelar {
961b637e498SPravin B Shelar struct table_instance *ti = ovsl_dereference(table->ti);
96274ed7ab9SJoe Stringer struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
963b637e498SPravin B Shelar
964e6445719SPravin B Shelar BUG_ON(table->count == 0);
965df68d64eSTonghao Zhang table_instance_flow_free(table, ti, ufid_ti, flow);
966e6445719SPravin B Shelar }
967e6445719SPravin B Shelar
mask_alloc(void)968618ed0c8SPravin B Shelar static struct sw_flow_mask *mask_alloc(void)
969e6445719SPravin B Shelar {
970e6445719SPravin B Shelar struct sw_flow_mask *mask;
971e6445719SPravin B Shelar
972e6445719SPravin B Shelar mask = kmalloc(sizeof(*mask), GFP_KERNEL);
973e6445719SPravin B Shelar if (mask)
974e80857ccSAndy Zhou mask->ref_count = 1;
975e6445719SPravin B Shelar
976e6445719SPravin B Shelar return mask;
977e6445719SPravin B Shelar }
978e6445719SPravin B Shelar
mask_equal(const struct sw_flow_mask * a,const struct sw_flow_mask * b)979e6445719SPravin B Shelar static bool mask_equal(const struct sw_flow_mask *a,
980e6445719SPravin B Shelar const struct sw_flow_mask *b)
981e6445719SPravin B Shelar {
9827085130bSDaniele Di Proietto const u8 *a_ = (const u8 *)&a->key + a->range.start;
9837085130bSDaniele Di Proietto const u8 *b_ = (const u8 *)&b->key + b->range.start;
984e6445719SPravin B Shelar
985e6445719SPravin B Shelar return (a->range.end == b->range.end)
986e6445719SPravin B Shelar && (a->range.start == b->range.start)
987e6445719SPravin B Shelar && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
988e6445719SPravin B Shelar }
989e6445719SPravin B Shelar
flow_mask_find(const struct flow_table * tbl,const struct sw_flow_mask * mask)990618ed0c8SPravin B Shelar static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
991e6445719SPravin B Shelar const struct sw_flow_mask *mask)
992e6445719SPravin B Shelar {
9934bc63b1bSTonghao Zhang struct mask_array *ma;
9944bc63b1bSTonghao Zhang int i;
995e6445719SPravin B Shelar
9964bc63b1bSTonghao Zhang ma = ovsl_dereference(tbl->mask_array);
9974bc63b1bSTonghao Zhang for (i = 0; i < ma->max; i++) {
9984bc63b1bSTonghao Zhang struct sw_flow_mask *t;
9994bc63b1bSTonghao Zhang t = ovsl_dereference(ma->masks[i]);
10004bc63b1bSTonghao Zhang
10014bc63b1bSTonghao Zhang if (t && mask_equal(mask, t))
10024bc63b1bSTonghao Zhang return t;
1003e6445719SPravin B Shelar }
1004e6445719SPravin B Shelar
1005e6445719SPravin B Shelar return NULL;
1006e6445719SPravin B Shelar }
1007e6445719SPravin B Shelar
1008d1211908SBen Pfaff /* Add 'mask' into the mask list, if it is not already there. */
flow_mask_insert(struct flow_table * tbl,struct sw_flow * flow,const struct sw_flow_mask * new)1009618ed0c8SPravin B Shelar static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
101012eb18f7SThomas Graf const struct sw_flow_mask *new)
1011e6445719SPravin B Shelar {
1012618ed0c8SPravin B Shelar struct sw_flow_mask *mask;
10134bc63b1bSTonghao Zhang
1014618ed0c8SPravin B Shelar mask = flow_mask_find(tbl, new);
1015618ed0c8SPravin B Shelar if (!mask) {
10161b381f6fSLuca Ceresoli /* Allocate a new mask if none exists. */
1017618ed0c8SPravin B Shelar mask = mask_alloc();
1018618ed0c8SPravin B Shelar if (!mask)
1019618ed0c8SPravin B Shelar return -ENOMEM;
1020618ed0c8SPravin B Shelar mask->key = new->key;
1021618ed0c8SPravin B Shelar mask->range = new->range;
10224bc63b1bSTonghao Zhang
10234bc63b1bSTonghao Zhang /* Add mask to mask-list. */
102457f7d7b9STonghao Zhang if (tbl_mask_array_add_mask(tbl, mask)) {
10254bc63b1bSTonghao Zhang kfree(mask);
102657f7d7b9STonghao Zhang return -ENOMEM;
10274bc63b1bSTonghao Zhang }
1028e80857ccSAndy Zhou } else {
1029e80857ccSAndy Zhou BUG_ON(!mask->ref_count);
1030e80857ccSAndy Zhou mask->ref_count++;
1031e6445719SPravin B Shelar }
1032e6445719SPravin B Shelar
1033618ed0c8SPravin B Shelar flow->mask = mask;
1034618ed0c8SPravin B Shelar return 0;
1035618ed0c8SPravin B Shelar }
1036618ed0c8SPravin B Shelar
103756c19868SJarno Rajahalme /* Must be called with OVS mutex held. */
flow_key_insert(struct flow_table * table,struct sw_flow * flow)1038d29ab6f8SJoe Stringer static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
1039618ed0c8SPravin B Shelar {
1040618ed0c8SPravin B Shelar struct table_instance *new_ti = NULL;
1041618ed0c8SPravin B Shelar struct table_instance *ti;
1042618ed0c8SPravin B Shelar
104374ed7ab9SJoe Stringer flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
1044618ed0c8SPravin B Shelar ti = ovsl_dereference(table->ti);
1045618ed0c8SPravin B Shelar table_instance_insert(ti, flow);
1046618ed0c8SPravin B Shelar table->count++;
1047618ed0c8SPravin B Shelar
1048618ed0c8SPravin B Shelar /* Expand table, if necessary, to make room. */
1049618ed0c8SPravin B Shelar if (table->count > ti->n_buckets)
105074ed7ab9SJoe Stringer new_ti = table_instance_expand(ti, false);
1051618ed0c8SPravin B Shelar else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
105274ed7ab9SJoe Stringer new_ti = table_instance_rehash(ti, ti->n_buckets, false);
1053618ed0c8SPravin B Shelar
1054618ed0c8SPravin B Shelar if (new_ti) {
1055618ed0c8SPravin B Shelar rcu_assign_pointer(table->ti, new_ti);
105674ed7ab9SJoe Stringer call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
1057618ed0c8SPravin B Shelar table->last_rehash = jiffies;
1058618ed0c8SPravin B Shelar }
1059d29ab6f8SJoe Stringer }
1060d29ab6f8SJoe Stringer
1061d29ab6f8SJoe Stringer /* Must be called with OVS mutex held. */
flow_ufid_insert(struct flow_table * table,struct sw_flow * flow)106274ed7ab9SJoe Stringer static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
106374ed7ab9SJoe Stringer {
106474ed7ab9SJoe Stringer struct table_instance *ti;
106574ed7ab9SJoe Stringer
106674ed7ab9SJoe Stringer flow->ufid_table.hash = ufid_hash(&flow->id);
106774ed7ab9SJoe Stringer ti = ovsl_dereference(table->ufid_ti);
106874ed7ab9SJoe Stringer ufid_table_instance_insert(ti, flow);
106974ed7ab9SJoe Stringer table->ufid_count++;
107074ed7ab9SJoe Stringer
107174ed7ab9SJoe Stringer /* Expand table, if necessary, to make room. */
107274ed7ab9SJoe Stringer if (table->ufid_count > ti->n_buckets) {
107374ed7ab9SJoe Stringer struct table_instance *new_ti;
107474ed7ab9SJoe Stringer
107574ed7ab9SJoe Stringer new_ti = table_instance_expand(ti, true);
107674ed7ab9SJoe Stringer if (new_ti) {
107774ed7ab9SJoe Stringer rcu_assign_pointer(table->ufid_ti, new_ti);
107874ed7ab9SJoe Stringer call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
107974ed7ab9SJoe Stringer }
108074ed7ab9SJoe Stringer }
108174ed7ab9SJoe Stringer }
108274ed7ab9SJoe Stringer
108374ed7ab9SJoe Stringer /* Must be called with OVS mutex held. */
ovs_flow_tbl_insert(struct flow_table * table,struct sw_flow * flow,const struct sw_flow_mask * mask)1084d29ab6f8SJoe Stringer int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
1085d29ab6f8SJoe Stringer const struct sw_flow_mask *mask)
1086d29ab6f8SJoe Stringer {
1087d29ab6f8SJoe Stringer int err;
1088d29ab6f8SJoe Stringer
1089d29ab6f8SJoe Stringer err = flow_mask_insert(table, flow, mask);
1090d29ab6f8SJoe Stringer if (err)
1091d29ab6f8SJoe Stringer return err;
1092d29ab6f8SJoe Stringer flow_key_insert(table, flow);
109374ed7ab9SJoe Stringer if (ovs_identifier_is_ufid(&flow->id))
109474ed7ab9SJoe Stringer flow_ufid_insert(table, flow);
1095d29ab6f8SJoe Stringer
1096618ed0c8SPravin B Shelar return 0;
1097618ed0c8SPravin B Shelar }
1098618ed0c8SPravin B Shelar
compare_mask_and_count(const void * a,const void * b)1099eac87c41SEelco Chaudron static int compare_mask_and_count(const void *a, const void *b)
1100eac87c41SEelco Chaudron {
1101eac87c41SEelco Chaudron const struct mask_count *mc_a = a;
1102eac87c41SEelco Chaudron const struct mask_count *mc_b = b;
1103eac87c41SEelco Chaudron
1104eac87c41SEelco Chaudron return (s64)mc_b->counter - (s64)mc_a->counter;
1105eac87c41SEelco Chaudron }
1106eac87c41SEelco Chaudron
1107eac87c41SEelco Chaudron /* Must be called with OVS mutex held. */
ovs_flow_masks_rebalance(struct flow_table * table)1108eac87c41SEelco Chaudron void ovs_flow_masks_rebalance(struct flow_table *table)
1109eac87c41SEelco Chaudron {
1110eac87c41SEelco Chaudron struct mask_array *ma = rcu_dereference_ovsl(table->mask_array);
1111eac87c41SEelco Chaudron struct mask_count *masks_and_count;
1112eac87c41SEelco Chaudron struct mask_array *new;
1113eac87c41SEelco Chaudron int masks_entries = 0;
1114eac87c41SEelco Chaudron int i;
1115eac87c41SEelco Chaudron
1116eac87c41SEelco Chaudron /* Build array of all current entries with use counters. */
1117eac87c41SEelco Chaudron masks_and_count = kmalloc_array(ma->max, sizeof(*masks_and_count),
1118eac87c41SEelco Chaudron GFP_KERNEL);
1119eac87c41SEelco Chaudron if (!masks_and_count)
1120eac87c41SEelco Chaudron return;
1121eac87c41SEelco Chaudron
1122eac87c41SEelco Chaudron for (i = 0; i < ma->max; i++) {
1123eac87c41SEelco Chaudron struct sw_flow_mask *mask;
1124eac87c41SEelco Chaudron int cpu;
1125eac87c41SEelco Chaudron
1126eac87c41SEelco Chaudron mask = rcu_dereference_ovsl(ma->masks[i]);
1127eac87c41SEelco Chaudron if (unlikely(!mask))
1128eac87c41SEelco Chaudron break;
1129eac87c41SEelco Chaudron
1130eac87c41SEelco Chaudron masks_and_count[i].index = i;
1131eac87c41SEelco Chaudron masks_and_count[i].counter = 0;
1132eac87c41SEelco Chaudron
1133eac87c41SEelco Chaudron for_each_possible_cpu(cpu) {
1134f981fc3dSEelco Chaudron struct mask_array_stats *stats;
1135f981fc3dSEelco Chaudron unsigned int start;
1136eac87c41SEelco Chaudron u64 counter;
1137eac87c41SEelco Chaudron
1138f981fc3dSEelco Chaudron stats = per_cpu_ptr(ma->masks_usage_stats, cpu);
1139eac87c41SEelco Chaudron do {
1140d120d1a6SThomas Gleixner start = u64_stats_fetch_begin(&stats->syncp);
1141f981fc3dSEelco Chaudron counter = stats->usage_cntrs[i];
1142d120d1a6SThomas Gleixner } while (u64_stats_fetch_retry(&stats->syncp, start));
1143eac87c41SEelco Chaudron
1144eac87c41SEelco Chaudron masks_and_count[i].counter += counter;
1145eac87c41SEelco Chaudron }
1146eac87c41SEelco Chaudron
1147eac87c41SEelco Chaudron /* Subtract the zero count value. */
1148eac87c41SEelco Chaudron masks_and_count[i].counter -= ma->masks_usage_zero_cntr[i];
1149eac87c41SEelco Chaudron
1150eac87c41SEelco Chaudron /* Rather than calling tbl_mask_array_reset_counters()
1151eac87c41SEelco Chaudron * below when no change is needed, do it inline here.
1152eac87c41SEelco Chaudron */
1153eac87c41SEelco Chaudron ma->masks_usage_zero_cntr[i] += masks_and_count[i].counter;
1154eac87c41SEelco Chaudron }
1155eac87c41SEelco Chaudron
1156eac87c41SEelco Chaudron if (i == 0)
1157eac87c41SEelco Chaudron goto free_mask_entries;
1158eac87c41SEelco Chaudron
1159eac87c41SEelco Chaudron /* Sort the entries */
1160eac87c41SEelco Chaudron masks_entries = i;
1161eac87c41SEelco Chaudron sort(masks_and_count, masks_entries, sizeof(*masks_and_count),
1162eac87c41SEelco Chaudron compare_mask_and_count, NULL);
1163eac87c41SEelco Chaudron
1164eac87c41SEelco Chaudron /* If the order is the same, nothing to do... */
1165eac87c41SEelco Chaudron for (i = 0; i < masks_entries; i++) {
1166eac87c41SEelco Chaudron if (i != masks_and_count[i].index)
1167eac87c41SEelco Chaudron break;
1168eac87c41SEelco Chaudron }
1169eac87c41SEelco Chaudron if (i == masks_entries)
1170eac87c41SEelco Chaudron goto free_mask_entries;
1171eac87c41SEelco Chaudron
1172eac87c41SEelco Chaudron /* Rebuilt the new list in order of usage. */
1173eac87c41SEelco Chaudron new = tbl_mask_array_alloc(ma->max);
1174eac87c41SEelco Chaudron if (!new)
1175eac87c41SEelco Chaudron goto free_mask_entries;
1176eac87c41SEelco Chaudron
1177eac87c41SEelco Chaudron for (i = 0; i < masks_entries; i++) {
1178eac87c41SEelco Chaudron int index = masks_and_count[i].index;
1179eac87c41SEelco Chaudron
11809bf24f59SEelco Chaudron if (ovsl_dereference(ma->masks[index]))
11819bf24f59SEelco Chaudron new->masks[new->count++] = ma->masks[index];
1182eac87c41SEelco Chaudron }
1183eac87c41SEelco Chaudron
1184eac87c41SEelco Chaudron rcu_assign_pointer(table->mask_array, new);
1185eac87c41SEelco Chaudron call_rcu(&ma->rcu, mask_array_rcu_cb);
1186eac87c41SEelco Chaudron
1187eac87c41SEelco Chaudron free_mask_entries:
1188eac87c41SEelco Chaudron kfree(masks_and_count);
1189eac87c41SEelco Chaudron }
1190eac87c41SEelco Chaudron
1191e6445719SPravin B Shelar /* Initializes the flow module.
1192e6445719SPravin B Shelar * Returns zero if successful or a negative error code. */
ovs_flow_init(void)1193e6445719SPravin B Shelar int ovs_flow_init(void)
1194e6445719SPravin B Shelar {
1195e6445719SPravin B Shelar BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
1196e6445719SPravin B Shelar BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
1197e6445719SPravin B Shelar
119863e7959cSJarno Rajahalme flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
1199db74a333SThadeu Lima de Souza Cascardo + (nr_cpu_ids
1200*15ea59a0SEddy Tao * sizeof(struct sw_flow_stats *))
1201*15ea59a0SEddy Tao + cpumask_size(),
120263e7959cSJarno Rajahalme 0, 0, NULL);
1203e6445719SPravin B Shelar if (flow_cache == NULL)
1204e6445719SPravin B Shelar return -ENOMEM;
1205e6445719SPravin B Shelar
120663e7959cSJarno Rajahalme flow_stats_cache
1207aef833c5SPablo Neira Ayuso = kmem_cache_create("sw_flow_stats", sizeof(struct sw_flow_stats),
120863e7959cSJarno Rajahalme 0, SLAB_HWCACHE_ALIGN, NULL);
120963e7959cSJarno Rajahalme if (flow_stats_cache == NULL) {
121063e7959cSJarno Rajahalme kmem_cache_destroy(flow_cache);
121163e7959cSJarno Rajahalme flow_cache = NULL;
121263e7959cSJarno Rajahalme return -ENOMEM;
121363e7959cSJarno Rajahalme }
121463e7959cSJarno Rajahalme
1215e6445719SPravin B Shelar return 0;
1216e6445719SPravin B Shelar }
1217e6445719SPravin B Shelar
1218e6445719SPravin B Shelar /* Uninitializes the flow module. */
ovs_flow_exit(void)1219e6445719SPravin B Shelar void ovs_flow_exit(void)
1220e6445719SPravin B Shelar {
122163e7959cSJarno Rajahalme kmem_cache_destroy(flow_stats_cache);
1222e6445719SPravin B Shelar kmem_cache_destroy(flow_cache);
1223e6445719SPravin B Shelar }
1224