Commit | Line | Data |
---|---|---|
c9422999 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
e6445719 | 2 | /* |
9b996e54 | 3 | * Copyright (c) 2007-2014 Nicira, Inc. |
e6445719 PS |
4 | */ |
5 | ||
6 | #include "flow.h" | |
7 | #include "datapath.h" | |
34ae932a | 8 | #include "flow_netlink.h" |
e6445719 PS |
9 | #include <linux/uaccess.h> |
10 | #include <linux/netdevice.h> | |
11 | #include <linux/etherdevice.h> | |
12 | #include <linux/if_ether.h> | |
13 | #include <linux/if_vlan.h> | |
14 | #include <net/llc_pdu.h> | |
15 | #include <linux/kernel.h> | |
87545899 | 16 | #include <linux/jhash.h> |
e6445719 PS |
17 | #include <linux/jiffies.h> |
18 | #include <linux/llc.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/in.h> | |
21 | #include <linux/rcupdate.h> | |
db74a333 | 22 | #include <linux/cpumask.h> |
e6445719 PS |
23 | #include <linux/if_arp.h> |
24 | #include <linux/ip.h> | |
25 | #include <linux/ipv6.h> | |
26 | #include <linux/sctp.h> | |
27 | #include <linux/tcp.h> | |
28 | #include <linux/udp.h> | |
29 | #include <linux/icmp.h> | |
30 | #include <linux/icmpv6.h> | |
31 | #include <linux/rculist.h> | |
eac87c41 | 32 | #include <linux/sort.h> |
e6445719 PS |
33 | #include <net/ip.h> |
34 | #include <net/ipv6.h> | |
35 | #include <net/ndisc.h> | |
36 | ||
b637e498 | 37 | #define TBL_MIN_BUCKETS 1024 |
4bc63b1b | 38 | #define MASK_ARRAY_SIZE_MIN 16 |
b637e498 PS |
39 | #define REHASH_INTERVAL (10 * 60 * HZ) |
40 | ||
9bf24f59 | 41 | #define MC_DEFAULT_HASH_ENTRIES 256 |
04b7d136 | 42 | #define MC_HASH_SHIFT 8 |
04b7d136 TZ |
43 | #define MC_HASH_SEGS ((sizeof(uint32_t) * 8) / MC_HASH_SHIFT) |
44 | ||
e6445719 | 45 | static struct kmem_cache *flow_cache; |
63e7959c | 46 | struct kmem_cache *flow_stats_cache __read_mostly; |
e6445719 PS |
47 | |
48 | static u16 range_n_bytes(const struct sw_flow_key_range *range) | |
49 | { | |
50 | return range->end - range->start; | |
51 | } | |
52 | ||
53 | void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src, | |
ae5f2fb1 | 54 | bool full, const struct sw_flow_mask *mask) |
e6445719 | 55 | { |
ae5f2fb1 JG |
56 | int start = full ? 0 : mask->range.start; |
57 | int len = full ? sizeof *dst : range_n_bytes(&mask->range); | |
58 | const long *m = (const long *)((const u8 *)&mask->key + start); | |
59 | const long *s = (const long *)((const u8 *)src + start); | |
60 | long *d = (long *)((u8 *)dst + start); | |
e6445719 PS |
61 | int i; |
62 | ||
ae5f2fb1 JG |
63 | /* If 'full' is true then all of 'dst' is fully initialized. Otherwise, |
64 | * if 'full' is false the memory outside of the 'mask->range' is left | |
65 | * uninitialized. This can be used as an optimization when further | |
66 | * operations on 'dst' only use contents within 'mask->range'. | |
e6445719 | 67 | */ |
ae5f2fb1 | 68 | for (i = 0; i < len; i += sizeof(long)) |
e6445719 PS |
69 | *d++ = *s++ & *m++; |
70 | } | |
71 | ||
23dabf88 | 72 | struct sw_flow *ovs_flow_alloc(void) |
e6445719 PS |
73 | { |
74 | struct sw_flow *flow; | |
aef833c5 | 75 | struct sw_flow_stats *stats; |
e6445719 | 76 | |
db74a333 | 77 | flow = kmem_cache_zalloc(flow_cache, GFP_KERNEL); |
e6445719 PS |
78 | if (!flow) |
79 | return ERR_PTR(-ENOMEM); | |
80 | ||
db74a333 | 81 | flow->stats_last_writer = -1; |
15ea59a0 | 82 | flow->cpu_used_mask = (struct cpumask *)&flow->stats[nr_cpu_ids]; |
e6445719 | 83 | |
63e7959c JR |
84 | /* Initialize the default stat node. */ |
85 | stats = kmem_cache_alloc_node(flow_stats_cache, | |
598c12d0 KK |
86 | GFP_KERNEL | __GFP_ZERO, |
87 | node_online(0) ? 0 : NUMA_NO_NODE); | |
63e7959c | 88 | if (!stats) |
23dabf88 | 89 | goto err; |
e298e505 | 90 | |
63e7959c JR |
91 | spin_lock_init(&stats->lock); |
92 | ||
93 | RCU_INIT_POINTER(flow->stats[0], stats); | |
94 | ||
15ea59a0 | 95 | cpumask_set_cpu(0, flow->cpu_used_mask); |
c4b2bf6b | 96 | |
e6445719 | 97 | return flow; |
e298e505 | 98 | err: |
ece37c87 | 99 | kmem_cache_free(flow_cache, flow); |
e298e505 | 100 | return ERR_PTR(-ENOMEM); |
e6445719 PS |
101 | } |
102 | ||
12eb18f7 | 103 | int ovs_flow_tbl_count(const struct flow_table *table) |
b637e498 PS |
104 | { |
105 | return table->count; | |
106 | } | |
107 | ||
e6445719 PS |
108 | static void flow_free(struct sw_flow *flow) |
109 | { | |
db74a333 | 110 | int cpu; |
63e7959c | 111 | |
74ed7ab9 JS |
112 | if (ovs_identifier_is_key(&flow->id)) |
113 | kfree(flow->id.unmasked_key); | |
34ae932a | 114 | if (flow->sf_acts) |
cf3266ad TZ |
115 | ovs_nla_free_flow_actions((struct sw_flow_actions __force *) |
116 | flow->sf_acts); | |
db74a333 | 117 | /* We open code this to make sure cpu 0 is always considered */ |
cf3266ad | 118 | for (cpu = 0; cpu < nr_cpu_ids; |
15ea59a0 | 119 | cpu = cpumask_next(cpu, flow->cpu_used_mask)) { |
db74a333 | 120 | if (flow->stats[cpu]) |
63e7959c | 121 | kmem_cache_free(flow_stats_cache, |
aef833c5 | 122 | (struct sw_flow_stats __force *)flow->stats[cpu]); |
cf3266ad TZ |
123 | } |
124 | ||
e6445719 PS |
125 | kmem_cache_free(flow_cache, flow); |
126 | } | |
127 | ||
128 | static void rcu_free_flow_callback(struct rcu_head *rcu) | |
129 | { | |
130 | struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); | |
131 | ||
132 | flow_free(flow); | |
133 | } | |
134 | ||
e80857cc | 135 | void ovs_flow_free(struct sw_flow *flow, bool deferred) |
618ed0c8 | 136 | { |
e80857cc | 137 | if (!flow) |
618ed0c8 PS |
138 | return; |
139 | ||
e6445719 PS |
140 | if (deferred) |
141 | call_rcu(&flow->rcu, rcu_free_flow_callback); | |
142 | else | |
143 | flow_free(flow); | |
144 | } | |
145 | ||
b637e498 | 146 | static void __table_instance_destroy(struct table_instance *ti) |
e6445719 | 147 | { |
ee9c5e67 | 148 | kvfree(ti->buckets); |
b637e498 | 149 | kfree(ti); |
e6445719 PS |
150 | } |
151 | ||
b637e498 | 152 | static struct table_instance *table_instance_alloc(int new_size) |
e6445719 | 153 | { |
b637e498 | 154 | struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL); |
ee9c5e67 | 155 | int i; |
e6445719 | 156 | |
b637e498 | 157 | if (!ti) |
e6445719 PS |
158 | return NULL; |
159 | ||
ee9c5e67 KO |
160 | ti->buckets = kvmalloc_array(new_size, sizeof(struct hlist_head), |
161 | GFP_KERNEL); | |
b637e498 PS |
162 | if (!ti->buckets) { |
163 | kfree(ti); | |
e6445719 PS |
164 | return NULL; |
165 | } | |
ee9c5e67 KO |
166 | |
167 | for (i = 0; i < new_size; i++) | |
168 | INIT_HLIST_HEAD(&ti->buckets[i]); | |
169 | ||
b637e498 PS |
170 | ti->n_buckets = new_size; |
171 | ti->node_ver = 0; | |
b637e498 | 172 | get_random_bytes(&ti->hash_seed, sizeof(u32)); |
e6445719 | 173 | |
b637e498 | 174 | return ti; |
e6445719 PS |
175 | } |
176 | ||
eac87c41 EC |
177 | static void __mask_array_destroy(struct mask_array *ma) |
178 | { | |
f981fc3d | 179 | free_percpu(ma->masks_usage_stats); |
eac87c41 EC |
180 | kfree(ma); |
181 | } | |
182 | ||
183 | static void mask_array_rcu_cb(struct rcu_head *rcu) | |
184 | { | |
185 | struct mask_array *ma = container_of(rcu, struct mask_array, rcu); | |
186 | ||
187 | __mask_array_destroy(ma); | |
188 | } | |
189 | ||
190 | static void tbl_mask_array_reset_counters(struct mask_array *ma) | |
191 | { | |
192 | int i, cpu; | |
193 | ||
194 | /* As the per CPU counters are not atomic we can not go ahead and | |
195 | * reset them from another CPU. To be able to still have an approximate | |
196 | * zero based counter we store the value at reset, and subtract it | |
197 | * later when processing. | |
198 | */ | |
cf3266ad | 199 | for (i = 0; i < ma->max; i++) { |
eac87c41 EC |
200 | ma->masks_usage_zero_cntr[i] = 0; |
201 | ||
202 | for_each_possible_cpu(cpu) { | |
f981fc3d | 203 | struct mask_array_stats *stats; |
eac87c41 EC |
204 | unsigned int start; |
205 | u64 counter; | |
206 | ||
f981fc3d | 207 | stats = per_cpu_ptr(ma->masks_usage_stats, cpu); |
eac87c41 | 208 | do { |
d120d1a6 | 209 | start = u64_stats_fetch_begin(&stats->syncp); |
f981fc3d | 210 | counter = stats->usage_cntrs[i]; |
d120d1a6 | 211 | } while (u64_stats_fetch_retry(&stats->syncp, start)); |
eac87c41 EC |
212 | |
213 | ma->masks_usage_zero_cntr[i] += counter; | |
214 | } | |
215 | } | |
216 | } | |
217 | ||
4bc63b1b TZ |
218 | static struct mask_array *tbl_mask_array_alloc(int size) |
219 | { | |
220 | struct mask_array *new; | |
221 | ||
222 | size = max(MASK_ARRAY_SIZE_MIN, size); | |
df3bf90f | 223 | new = kzalloc(struct_size(new, masks, size) + |
eac87c41 | 224 | sizeof(u64) * size, GFP_KERNEL); |
4bc63b1b TZ |
225 | if (!new) |
226 | return NULL; | |
227 | ||
eac87c41 | 228 | new->masks_usage_zero_cntr = (u64 *)((u8 *)new + |
df3bf90f | 229 | struct_size(new, masks, size)); |
eac87c41 | 230 | |
f981fc3d EC |
231 | new->masks_usage_stats = __alloc_percpu(sizeof(struct mask_array_stats) + |
232 | sizeof(u64) * size, | |
233 | __alignof__(u64)); | |
234 | if (!new->masks_usage_stats) { | |
eac87c41 EC |
235 | kfree(new); |
236 | return NULL; | |
237 | } | |
238 | ||
4bc63b1b TZ |
239 | new->count = 0; |
240 | new->max = size; | |
241 | ||
242 | return new; | |
243 | } | |
244 | ||
245 | static int tbl_mask_array_realloc(struct flow_table *tbl, int size) | |
246 | { | |
247 | struct mask_array *old; | |
248 | struct mask_array *new; | |
249 | ||
250 | new = tbl_mask_array_alloc(size); | |
251 | if (!new) | |
252 | return -ENOMEM; | |
253 | ||
254 | old = ovsl_dereference(tbl->mask_array); | |
255 | if (old) { | |
256 | int i; | |
257 | ||
258 | for (i = 0; i < old->max; i++) { | |
259 | if (ovsl_dereference(old->masks[i])) | |
260 | new->masks[new->count++] = old->masks[i]; | |
261 | } | |
eac87c41 | 262 | call_rcu(&old->rcu, mask_array_rcu_cb); |
4bc63b1b TZ |
263 | } |
264 | ||
265 | rcu_assign_pointer(tbl->mask_array, new); | |
4bc63b1b TZ |
266 | |
267 | return 0; | |
268 | } | |
269 | ||
50b0e61b TZ |
270 | static int tbl_mask_array_add_mask(struct flow_table *tbl, |
271 | struct sw_flow_mask *new) | |
272 | { | |
273 | struct mask_array *ma = ovsl_dereference(tbl->mask_array); | |
274 | int err, ma_count = READ_ONCE(ma->count); | |
275 | ||
276 | if (ma_count >= ma->max) { | |
277 | err = tbl_mask_array_realloc(tbl, ma->max + | |
cf3266ad | 278 | MASK_ARRAY_SIZE_MIN); |
50b0e61b TZ |
279 | if (err) |
280 | return err; | |
281 | ||
282 | ma = ovsl_dereference(tbl->mask_array); | |
eac87c41 EC |
283 | } else { |
284 | /* On every add or delete we need to reset the counters so | |
285 | * every new mask gets a fair chance of being prioritized. | |
286 | */ | |
287 | tbl_mask_array_reset_counters(ma); | |
50b0e61b TZ |
288 | } |
289 | ||
290 | BUG_ON(ovsl_dereference(ma->masks[ma_count])); | |
291 | ||
292 | rcu_assign_pointer(ma->masks[ma_count], new); | |
cf3266ad | 293 | WRITE_ONCE(ma->count, ma_count + 1); |
50b0e61b TZ |
294 | |
295 | return 0; | |
296 | } | |
297 | ||
298 | static void tbl_mask_array_del_mask(struct flow_table *tbl, | |
299 | struct sw_flow_mask *mask) | |
300 | { | |
301 | struct mask_array *ma = ovsl_dereference(tbl->mask_array); | |
302 | int i, ma_count = READ_ONCE(ma->count); | |
303 | ||
304 | /* Remove the deleted mask pointers from the array */ | |
305 | for (i = 0; i < ma_count; i++) { | |
306 | if (mask == ovsl_dereference(ma->masks[i])) | |
307 | goto found; | |
308 | } | |
309 | ||
310 | BUG(); | |
311 | return; | |
312 | ||
313 | found: | |
cf3266ad | 314 | WRITE_ONCE(ma->count, ma_count - 1); |
50b0e61b | 315 | |
cf3266ad TZ |
316 | rcu_assign_pointer(ma->masks[i], ma->masks[ma_count - 1]); |
317 | RCU_INIT_POINTER(ma->masks[ma_count - 1], NULL); | |
50b0e61b TZ |
318 | |
319 | kfree_rcu(mask, rcu); | |
320 | ||
321 | /* Shrink the mask array if necessary. */ | |
322 | if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) && | |
323 | ma_count <= (ma->max / 3)) | |
324 | tbl_mask_array_realloc(tbl, ma->max / 2); | |
eac87c41 EC |
325 | else |
326 | tbl_mask_array_reset_counters(ma); | |
327 | ||
50b0e61b TZ |
328 | } |
329 | ||
330 | /* Remove 'mask' from the mask list, if it is not needed any more. */ | |
331 | static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) | |
332 | { | |
333 | if (mask) { | |
334 | /* ovs-lock is required to protect mask-refcount and | |
335 | * mask list. | |
336 | */ | |
337 | ASSERT_OVSL(); | |
338 | BUG_ON(!mask->ref_count); | |
339 | mask->ref_count--; | |
340 | ||
341 | if (!mask->ref_count) | |
342 | tbl_mask_array_del_mask(tbl, mask); | |
343 | } | |
344 | } | |
345 | ||
9bf24f59 EC |
346 | static void __mask_cache_destroy(struct mask_cache *mc) |
347 | { | |
348 | free_percpu(mc->mask_cache); | |
349 | kfree(mc); | |
350 | } | |
351 | ||
352 | static void mask_cache_rcu_cb(struct rcu_head *rcu) | |
353 | { | |
354 | struct mask_cache *mc = container_of(rcu, struct mask_cache, rcu); | |
355 | ||
356 | __mask_cache_destroy(mc); | |
357 | } | |
358 | ||
359 | static struct mask_cache *tbl_mask_cache_alloc(u32 size) | |
360 | { | |
361 | struct mask_cache_entry __percpu *cache = NULL; | |
362 | struct mask_cache *new; | |
363 | ||
364 | /* Only allow size to be 0, or a power of 2, and does not exceed | |
365 | * percpu allocation size. | |
366 | */ | |
367 | if ((!is_power_of_2(size) && size != 0) || | |
368 | (size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE) | |
369 | return NULL; | |
370 | ||
371 | new = kzalloc(sizeof(*new), GFP_KERNEL); | |
372 | if (!new) | |
373 | return NULL; | |
374 | ||
375 | new->cache_size = size; | |
376 | if (new->cache_size > 0) { | |
377 | cache = __alloc_percpu(array_size(sizeof(struct mask_cache_entry), | |
378 | new->cache_size), | |
379 | __alignof__(struct mask_cache_entry)); | |
380 | if (!cache) { | |
381 | kfree(new); | |
382 | return NULL; | |
383 | } | |
384 | } | |
385 | ||
386 | new->mask_cache = cache; | |
387 | return new; | |
388 | } | |
389 | int ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size) | |
390 | { | |
fea07a48 | 391 | struct mask_cache *mc = rcu_dereference_ovsl(table->mask_cache); |
9bf24f59 EC |
392 | struct mask_cache *new; |
393 | ||
394 | if (size == mc->cache_size) | |
395 | return 0; | |
396 | ||
397 | if ((!is_power_of_2(size) && size != 0) || | |
398 | (size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE) | |
399 | return -EINVAL; | |
400 | ||
401 | new = tbl_mask_cache_alloc(size); | |
402 | if (!new) | |
403 | return -ENOMEM; | |
404 | ||
405 | rcu_assign_pointer(table->mask_cache, new); | |
406 | call_rcu(&mc->rcu, mask_cache_rcu_cb); | |
407 | ||
408 | return 0; | |
409 | } | |
410 | ||
b637e498 | 411 | int ovs_flow_tbl_init(struct flow_table *table) |
e6445719 | 412 | { |
74ed7ab9 | 413 | struct table_instance *ti, *ufid_ti; |
9bf24f59 | 414 | struct mask_cache *mc; |
4bc63b1b | 415 | struct mask_array *ma; |
e6445719 | 416 | |
9bf24f59 EC |
417 | mc = tbl_mask_cache_alloc(MC_DEFAULT_HASH_ENTRIES); |
418 | if (!mc) | |
04b7d136 | 419 | return -ENOMEM; |
e6445719 | 420 | |
4bc63b1b TZ |
421 | ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN); |
422 | if (!ma) | |
423 | goto free_mask_cache; | |
424 | ||
04b7d136 | 425 | ti = table_instance_alloc(TBL_MIN_BUCKETS); |
b637e498 | 426 | if (!ti) |
4bc63b1b | 427 | goto free_mask_array; |
e6445719 | 428 | |
74ed7ab9 JS |
429 | ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
430 | if (!ufid_ti) | |
431 | goto free_ti; | |
432 | ||
b637e498 | 433 | rcu_assign_pointer(table->ti, ti); |
74ed7ab9 | 434 | rcu_assign_pointer(table->ufid_ti, ufid_ti); |
4bc63b1b | 435 | rcu_assign_pointer(table->mask_array, ma); |
9bf24f59 | 436 | rcu_assign_pointer(table->mask_cache, mc); |
b637e498 PS |
437 | table->last_rehash = jiffies; |
438 | table->count = 0; | |
74ed7ab9 | 439 | table->ufid_count = 0; |
b637e498 | 440 | return 0; |
74ed7ab9 JS |
441 | |
442 | free_ti: | |
443 | __table_instance_destroy(ti); | |
4bc63b1b | 444 | free_mask_array: |
eac87c41 | 445 | __mask_array_destroy(ma); |
04b7d136 | 446 | free_mask_cache: |
9bf24f59 | 447 | __mask_cache_destroy(mc); |
74ed7ab9 | 448 | return -ENOMEM; |
e6445719 PS |
449 | } |
450 | ||
451 | static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) | |
452 | { | |
cf3266ad | 453 | struct table_instance *ti; |
e6445719 | 454 | |
cf3266ad | 455 | ti = container_of(rcu, struct table_instance, rcu); |
b637e498 | 456 | __table_instance_destroy(ti); |
e6445719 PS |
457 | } |
458 | ||
50b0e61b | 459 | static void table_instance_flow_free(struct flow_table *table, |
cf3266ad TZ |
460 | struct table_instance *ti, |
461 | struct table_instance *ufid_ti, | |
df68d64e | 462 | struct sw_flow *flow) |
50b0e61b TZ |
463 | { |
464 | hlist_del_rcu(&flow->flow_table.node[ti->node_ver]); | |
df68d64e | 465 | table->count--; |
50b0e61b TZ |
466 | |
467 | if (ovs_identifier_is_ufid(&flow->id)) { | |
468 | hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]); | |
df68d64e | 469 | table->ufid_count--; |
50b0e61b TZ |
470 | } |
471 | ||
472 | flow_mask_remove(table, flow->mask); | |
473 | } | |
474 | ||
1f3a090b TZ |
475 | /* Must be called with OVS mutex held. */ |
476 | void table_instance_flow_flush(struct flow_table *table, | |
477 | struct table_instance *ti, | |
478 | struct table_instance *ufid_ti) | |
e6445719 | 479 | { |
e80857cc AZ |
480 | int i; |
481 | ||
e80857cc | 482 | for (i = 0; i < ti->n_buckets; i++) { |
ee9c5e67 | 483 | struct hlist_head *head = &ti->buckets[i]; |
e80857cc | 484 | struct hlist_node *n; |
cf3266ad | 485 | struct sw_flow *flow; |
e80857cc | 486 | |
50b0e61b TZ |
487 | hlist_for_each_entry_safe(flow, n, head, |
488 | flow_table.node[ti->node_ver]) { | |
489 | ||
490 | table_instance_flow_free(table, ti, ufid_ti, | |
df68d64e | 491 | flow); |
1f3a090b | 492 | ovs_flow_free(flow, true); |
e80857cc AZ |
493 | } |
494 | } | |
df68d64e TZ |
495 | |
496 | if (WARN_ON(table->count != 0 || | |
497 | table->ufid_count != 0)) { | |
498 | table->count = 0; | |
499 | table->ufid_count = 0; | |
500 | } | |
1f3a090b | 501 | } |
e80857cc | 502 | |
1f3a090b TZ |
503 | static void table_instance_destroy(struct table_instance *ti, |
504 | struct table_instance *ufid_ti) | |
505 | { | |
506 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); | |
507 | call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb); | |
b637e498 PS |
508 | } |
509 | ||
9b996e54 PS |
510 | /* No need for locking this function is called from RCU callback or |
511 | * error path. | |
512 | */ | |
513 | void ovs_flow_tbl_destroy(struct flow_table *table) | |
b637e498 | 514 | { |
9b996e54 | 515 | struct table_instance *ti = rcu_dereference_raw(table->ti); |
74ed7ab9 | 516 | struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti); |
5845589e TZ |
517 | struct mask_cache *mc = rcu_dereference_raw(table->mask_cache); |
518 | struct mask_array *ma = rcu_dereference_raw(table->mask_array); | |
b637e498 | 519 | |
9bf24f59 EC |
520 | call_rcu(&mc->rcu, mask_cache_rcu_cb); |
521 | call_rcu(&ma->rcu, mask_array_rcu_cb); | |
1f3a090b | 522 | table_instance_destroy(ti, ufid_ti); |
e6445719 PS |
523 | } |
524 | ||
b637e498 | 525 | struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti, |
e6445719 PS |
526 | u32 *bucket, u32 *last) |
527 | { | |
528 | struct sw_flow *flow; | |
529 | struct hlist_head *head; | |
530 | int ver; | |
531 | int i; | |
532 | ||
b637e498 PS |
533 | ver = ti->node_ver; |
534 | while (*bucket < ti->n_buckets) { | |
e6445719 | 535 | i = 0; |
ee9c5e67 | 536 | head = &ti->buckets[*bucket]; |
74ed7ab9 | 537 | hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) { |
e6445719 PS |
538 | if (i < *last) { |
539 | i++; | |
540 | continue; | |
541 | } | |
542 | *last = i + 1; | |
543 | return flow; | |
544 | } | |
545 | (*bucket)++; | |
546 | *last = 0; | |
547 | } | |
548 | ||
549 | return NULL; | |
550 | } | |
551 | ||
b637e498 | 552 | static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash) |
e6445719 | 553 | { |
b637e498 | 554 | hash = jhash_1word(hash, ti->hash_seed); |
ee9c5e67 | 555 | return &ti->buckets[hash & (ti->n_buckets - 1)]; |
e6445719 PS |
556 | } |
557 | ||
74ed7ab9 JS |
558 | static void table_instance_insert(struct table_instance *ti, |
559 | struct sw_flow *flow) | |
e6445719 PS |
560 | { |
561 | struct hlist_head *head; | |
562 | ||
74ed7ab9 JS |
563 | head = find_bucket(ti, flow->flow_table.hash); |
564 | hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head); | |
565 | } | |
566 | ||
567 | static void ufid_table_instance_insert(struct table_instance *ti, | |
568 | struct sw_flow *flow) | |
569 | { | |
570 | struct hlist_head *head; | |
571 | ||
572 | head = find_bucket(ti, flow->ufid_table.hash); | |
573 | hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head); | |
e6445719 PS |
574 | } |
575 | ||
b637e498 | 576 | static void flow_table_copy_flows(struct table_instance *old, |
74ed7ab9 | 577 | struct table_instance *new, bool ufid) |
e6445719 PS |
578 | { |
579 | int old_ver; | |
580 | int i; | |
581 | ||
582 | old_ver = old->node_ver; | |
583 | new->node_ver = !old_ver; | |
584 | ||
585 | /* Insert in new table. */ | |
586 | for (i = 0; i < old->n_buckets; i++) { | |
587 | struct sw_flow *flow; | |
ee9c5e67 | 588 | struct hlist_head *head = &old->buckets[i]; |
e6445719 | 589 | |
74ed7ab9 | 590 | if (ufid) |
64948427 TZ |
591 | hlist_for_each_entry_rcu(flow, head, |
592 | ufid_table.node[old_ver], | |
593 | lockdep_ovsl_is_held()) | |
74ed7ab9 JS |
594 | ufid_table_instance_insert(new, flow); |
595 | else | |
64948427 TZ |
596 | hlist_for_each_entry_rcu(flow, head, |
597 | flow_table.node[old_ver], | |
598 | lockdep_ovsl_is_held()) | |
74ed7ab9 | 599 | table_instance_insert(new, flow); |
e6445719 | 600 | } |
e6445719 PS |
601 | } |
602 | ||
b637e498 | 603 | static struct table_instance *table_instance_rehash(struct table_instance *ti, |
74ed7ab9 | 604 | int n_buckets, bool ufid) |
e6445719 | 605 | { |
b637e498 | 606 | struct table_instance *new_ti; |
e6445719 | 607 | |
b637e498 PS |
608 | new_ti = table_instance_alloc(n_buckets); |
609 | if (!new_ti) | |
618ed0c8 | 610 | return NULL; |
e6445719 | 611 | |
74ed7ab9 | 612 | flow_table_copy_flows(ti, new_ti, ufid); |
e6445719 | 613 | |
b637e498 | 614 | return new_ti; |
e6445719 PS |
615 | } |
616 | ||
b637e498 | 617 | int ovs_flow_tbl_flush(struct flow_table *flow_table) |
e6445719 | 618 | { |
74ed7ab9 JS |
619 | struct table_instance *old_ti, *new_ti; |
620 | struct table_instance *old_ufid_ti, *new_ufid_ti; | |
e6445719 | 621 | |
b637e498 PS |
622 | new_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
623 | if (!new_ti) | |
624 | return -ENOMEM; | |
74ed7ab9 JS |
625 | new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
626 | if (!new_ufid_ti) | |
627 | goto err_free_ti; | |
628 | ||
629 | old_ti = ovsl_dereference(flow_table->ti); | |
630 | old_ufid_ti = ovsl_dereference(flow_table->ufid_ti); | |
b637e498 PS |
631 | |
632 | rcu_assign_pointer(flow_table->ti, new_ti); | |
74ed7ab9 | 633 | rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti); |
b637e498 | 634 | flow_table->last_rehash = jiffies; |
b637e498 | 635 | |
1f3a090b TZ |
636 | table_instance_flow_flush(flow_table, old_ti, old_ufid_ti); |
637 | table_instance_destroy(old_ti, old_ufid_ti); | |
b637e498 | 638 | return 0; |
74ed7ab9 JS |
639 | |
640 | err_free_ti: | |
641 | __table_instance_destroy(new_ti); | |
642 | return -ENOMEM; | |
e6445719 PS |
643 | } |
644 | ||
272c2cf8 JS |
645 | static u32 flow_hash(const struct sw_flow_key *key, |
646 | const struct sw_flow_key_range *range) | |
e6445719 | 647 | { |
515b65a4 | 648 | const u32 *hash_key = (const u32 *)((const u8 *)key + range->start); |
e6445719 PS |
649 | |
650 | /* Make sure number of hash bytes are multiple of u32. */ | |
515b65a4 | 651 | int hash_u32s = range_n_bytes(range) >> 2; |
e6445719 | 652 | |
87545899 | 653 | return jhash2(hash_key, hash_u32s, 0); |
e6445719 PS |
654 | } |
655 | ||
656 | static int flow_key_start(const struct sw_flow_key *key) | |
657 | { | |
00a93bab | 658 | if (key->tun_proto) |
e6445719 PS |
659 | return 0; |
660 | else | |
661 | return rounddown(offsetof(struct sw_flow_key, phy), | |
cf3266ad | 662 | sizeof(long)); |
e6445719 PS |
663 | } |
664 | ||
665 | static bool cmp_key(const struct sw_flow_key *key1, | |
666 | const struct sw_flow_key *key2, | |
667 | int key_start, int key_end) | |
668 | { | |
7085130b DDP |
669 | const long *cp1 = (const long *)((const u8 *)key1 + key_start); |
670 | const long *cp2 = (const long *)((const u8 *)key2 + key_start); | |
e6445719 PS |
671 | int i; |
672 | ||
cf3266ad | 673 | for (i = key_start; i < key_end; i += sizeof(long)) |
b1811447 BZ |
674 | if (*cp1++ ^ *cp2++) |
675 | return false; | |
e6445719 | 676 | |
b1811447 | 677 | return true; |
e6445719 PS |
678 | } |
679 | ||
680 | static bool flow_cmp_masked_key(const struct sw_flow *flow, | |
681 | const struct sw_flow_key *key, | |
272c2cf8 | 682 | const struct sw_flow_key_range *range) |
e6445719 | 683 | { |
272c2cf8 | 684 | return cmp_key(&flow->key, key, range->start, range->end); |
e6445719 PS |
685 | } |
686 | ||
74ed7ab9 JS |
687 | static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, |
688 | const struct sw_flow_match *match) | |
e6445719 PS |
689 | { |
690 | struct sw_flow_key *key = match->key; | |
691 | int key_start = flow_key_start(key); | |
692 | int key_end = match->range.end; | |
693 | ||
74ed7ab9 JS |
694 | BUG_ON(ovs_identifier_is_ufid(&flow->id)); |
695 | return cmp_key(flow->id.unmasked_key, key, key_start, key_end); | |
e6445719 PS |
696 | } |
697 | ||
b637e498 | 698 | static struct sw_flow *masked_flow_lookup(struct table_instance *ti, |
e6445719 | 699 | const struct sw_flow_key *unmasked, |
04b7d136 TZ |
700 | const struct sw_flow_mask *mask, |
701 | u32 *n_mask_hit) | |
e6445719 PS |
702 | { |
703 | struct sw_flow *flow; | |
704 | struct hlist_head *head; | |
e6445719 PS |
705 | u32 hash; |
706 | struct sw_flow_key masked_key; | |
707 | ||
ae5f2fb1 | 708 | ovs_flow_mask_key(&masked_key, unmasked, false, mask); |
272c2cf8 | 709 | hash = flow_hash(&masked_key, &mask->range); |
b637e498 | 710 | head = find_bucket(ti, hash); |
04b7d136 TZ |
711 | (*n_mask_hit)++; |
712 | ||
a2cfb96c | 713 | hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver], |
cf3266ad | 714 | lockdep_ovsl_is_held()) { |
74ed7ab9 | 715 | if (flow->mask == mask && flow->flow_table.hash == hash && |
272c2cf8 | 716 | flow_cmp_masked_key(flow, &masked_key, &mask->range)) |
e6445719 PS |
717 | return flow; |
718 | } | |
719 | return NULL; | |
720 | } | |
721 | ||
a7f35e78 TZ |
722 | /* Flow lookup does full lookup on flow table. It starts with |
723 | * mask from index passed in *index. | |
f981fc3d EC |
724 | * This function MUST be called with BH disabled due to the use |
725 | * of CPU specific variables. | |
a7f35e78 | 726 | */ |
04b7d136 TZ |
727 | static struct sw_flow *flow_lookup(struct flow_table *tbl, |
728 | struct table_instance *ti, | |
4bc63b1b | 729 | struct mask_array *ma, |
04b7d136 | 730 | const struct sw_flow_key *key, |
4bc63b1b | 731 | u32 *n_mask_hit, |
9d2f627b | 732 | u32 *n_cache_hit, |
4bc63b1b | 733 | u32 *index) |
e6445719 | 734 | { |
f981fc3d | 735 | struct mask_array_stats *stats = this_cpu_ptr(ma->masks_usage_stats); |
b637e498 | 736 | struct sw_flow *flow; |
57f7d7b9 | 737 | struct sw_flow_mask *mask; |
4bc63b1b | 738 | int i; |
e6445719 | 739 | |
0a3e0137 | 740 | if (likely(*index < ma->max)) { |
a7f35e78 | 741 | mask = rcu_dereference_ovsl(ma->masks[*index]); |
4bc63b1b TZ |
742 | if (mask) { |
743 | flow = masked_flow_lookup(ti, key, mask, n_mask_hit); | |
eac87c41 | 744 | if (flow) { |
f981fc3d EC |
745 | u64_stats_update_begin(&stats->syncp); |
746 | stats->usage_cntrs[*index]++; | |
747 | u64_stats_update_end(&stats->syncp); | |
9d2f627b | 748 | (*n_cache_hit)++; |
4bc63b1b | 749 | return flow; |
eac87c41 | 750 | } |
a7f35e78 TZ |
751 | } |
752 | } | |
753 | ||
754 | for (i = 0; i < ma->max; i++) { | |
755 | ||
756 | if (i == *index) | |
757 | continue; | |
758 | ||
759 | mask = rcu_dereference_ovsl(ma->masks[i]); | |
0a3e0137 | 760 | if (unlikely(!mask)) |
57f7d7b9 | 761 | break; |
a7f35e78 TZ |
762 | |
763 | flow = masked_flow_lookup(ti, key, mask, n_mask_hit); | |
764 | if (flow) { /* Found */ | |
765 | *index = i; | |
f981fc3d EC |
766 | u64_stats_update_begin(&stats->syncp); |
767 | stats->usage_cntrs[*index]++; | |
768 | u64_stats_update_end(&stats->syncp); | |
a7f35e78 | 769 | return flow; |
4bc63b1b | 770 | } |
e6445719 | 771 | } |
4bc63b1b | 772 | |
b637e498 PS |
773 | return NULL; |
774 | } | |
e6445719 | 775 | |
04b7d136 TZ |
776 | /* |
777 | * mask_cache maps flow to probable mask. This cache is not tightly | |
778 | * coupled cache, It means updates to mask list can result in inconsistent | |
779 | * cache entry in mask cache. | |
780 | * This is per cpu cache and is divided in MC_HASH_SEGS segments. | |
781 | * In case of a hash collision the entry is hashed in next segment. | |
782 | * */ | |
783 | struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, | |
784 | const struct sw_flow_key *key, | |
785 | u32 skb_hash, | |
9d2f627b EC |
786 | u32 *n_mask_hit, |
787 | u32 *n_cache_hit) | |
04b7d136 | 788 | { |
9bf24f59 | 789 | struct mask_cache *mc = rcu_dereference(tbl->mask_cache); |
a7f35e78 TZ |
790 | struct mask_array *ma = rcu_dereference(tbl->mask_array); |
791 | struct table_instance *ti = rcu_dereference(tbl->ti); | |
792 | struct mask_cache_entry *entries, *ce; | |
04b7d136 | 793 | struct sw_flow *flow; |
a7f35e78 | 794 | u32 hash; |
04b7d136 TZ |
795 | int seg; |
796 | ||
797 | *n_mask_hit = 0; | |
9d2f627b | 798 | *n_cache_hit = 0; |
9bf24f59 | 799 | if (unlikely(!skb_hash || mc->cache_size == 0)) { |
a7f35e78 | 800 | u32 mask_index = 0; |
9d2f627b | 801 | u32 cache = 0; |
4bc63b1b | 802 | |
9d2f627b EC |
803 | return flow_lookup(tbl, ti, ma, key, n_mask_hit, &cache, |
804 | &mask_index); | |
4bc63b1b | 805 | } |
04b7d136 | 806 | |
a7f35e78 TZ |
807 | /* Pre and post recirulation flows usually have the same skb_hash |
808 | * value. To avoid hash collisions, rehash the 'skb_hash' with | |
809 | * 'recirc_id'. */ | |
810 | if (key->recirc_id) | |
811 | skb_hash = jhash_1word(skb_hash, key->recirc_id); | |
812 | ||
813 | ce = NULL; | |
814 | hash = skb_hash; | |
9bf24f59 | 815 | entries = this_cpu_ptr(mc->mask_cache); |
04b7d136 | 816 | |
a7f35e78 | 817 | /* Find the cache entry 'ce' to operate on. */ |
04b7d136 | 818 | for (seg = 0; seg < MC_HASH_SEGS; seg++) { |
9bf24f59 | 819 | int index = hash & (mc->cache_size - 1); |
a7f35e78 TZ |
820 | struct mask_cache_entry *e; |
821 | ||
822 | e = &entries[index]; | |
823 | if (e->skb_hash == skb_hash) { | |
824 | flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, | |
9d2f627b | 825 | n_cache_hit, &e->mask_index); |
a7f35e78 TZ |
826 | if (!flow) |
827 | e->skb_hash = 0; | |
828 | return flow; | |
04b7d136 TZ |
829 | } |
830 | ||
a7f35e78 TZ |
831 | if (!ce || e->skb_hash < ce->skb_hash) |
832 | ce = e; /* A better replacement cache candidate. */ | |
04b7d136 TZ |
833 | |
834 | hash >>= MC_HASH_SHIFT; | |
835 | } | |
836 | ||
a7f35e78 | 837 | /* Cache miss, do full lookup. */ |
9d2f627b EC |
838 | flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, n_cache_hit, |
839 | &ce->mask_index); | |
4bc63b1b | 840 | if (flow) |
a7f35e78 | 841 | ce->skb_hash = skb_hash; |
04b7d136 | 842 | |
9d2f627b | 843 | *n_cache_hit = 0; |
04b7d136 TZ |
844 | return flow; |
845 | } | |
846 | ||
5bb50632 AZ |
847 | struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, |
848 | const struct sw_flow_key *key) | |
849 | { | |
04b7d136 | 850 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); |
4bc63b1b | 851 | struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array); |
5bb50632 | 852 | u32 __always_unused n_mask_hit; |
9d2f627b | 853 | u32 __always_unused n_cache_hit; |
f981fc3d | 854 | struct sw_flow *flow; |
a7f35e78 | 855 | u32 index = 0; |
5bb50632 | 856 | |
f981fc3d EC |
857 | /* This function gets called trough the netlink interface and therefore |
858 | * is preemptible. However, flow_lookup() function needs to be called | |
859 | * with BH disabled due to CPU specific variables. | |
860 | */ | |
861 | local_bh_disable(); | |
862 | flow = flow_lookup(tbl, ti, ma, key, &n_mask_hit, &n_cache_hit, &index); | |
863 | local_bh_enable(); | |
864 | return flow; | |
5bb50632 AZ |
865 | } |
866 | ||
4a46b24e | 867 | struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, |
12eb18f7 | 868 | const struct sw_flow_match *match) |
4a46b24e | 869 | { |
4bc63b1b TZ |
870 | struct mask_array *ma = ovsl_dereference(tbl->mask_array); |
871 | int i; | |
4a46b24e AW |
872 | |
873 | /* Always called under ovs-mutex. */ | |
4bc63b1b TZ |
874 | for (i = 0; i < ma->max; i++) { |
875 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); | |
876 | u32 __always_unused n_mask_hit; | |
877 | struct sw_flow_mask *mask; | |
878 | struct sw_flow *flow; | |
879 | ||
880 | mask = ovsl_dereference(ma->masks[i]); | |
881 | if (!mask) | |
882 | continue; | |
883 | ||
04b7d136 | 884 | flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit); |
74ed7ab9 | 885 | if (flow && ovs_identifier_is_key(&flow->id) && |
4bc63b1b | 886 | ovs_flow_cmp_unmasked_key(flow, match)) { |
74ed7ab9 | 887 | return flow; |
4bc63b1b | 888 | } |
74ed7ab9 | 889 | } |
4bc63b1b | 890 | |
74ed7ab9 JS |
891 | return NULL; |
892 | } | |
893 | ||
894 | static u32 ufid_hash(const struct sw_flow_id *sfid) | |
895 | { | |
896 | return jhash(sfid->ufid, sfid->ufid_len, 0); | |
897 | } | |
898 | ||
899 | static bool ovs_flow_cmp_ufid(const struct sw_flow *flow, | |
900 | const struct sw_flow_id *sfid) | |
901 | { | |
902 | if (flow->id.ufid_len != sfid->ufid_len) | |
903 | return false; | |
904 | ||
905 | return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len); | |
906 | } | |
907 | ||
cf3266ad TZ |
908 | bool ovs_flow_cmp(const struct sw_flow *flow, |
909 | const struct sw_flow_match *match) | |
74ed7ab9 JS |
910 | { |
911 | if (ovs_identifier_is_ufid(&flow->id)) | |
912 | return flow_cmp_masked_key(flow, match->key, &match->range); | |
913 | ||
914 | return ovs_flow_cmp_unmasked_key(flow, match); | |
915 | } | |
916 | ||
917 | struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl, | |
918 | const struct sw_flow_id *ufid) | |
919 | { | |
920 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti); | |
921 | struct sw_flow *flow; | |
922 | struct hlist_head *head; | |
923 | u32 hash; | |
924 | ||
925 | hash = ufid_hash(ufid); | |
926 | head = find_bucket(ti, hash); | |
a2cfb96c | 927 | hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver], |
cf3266ad | 928 | lockdep_ovsl_is_held()) { |
74ed7ab9 JS |
929 | if (flow->ufid_table.hash == hash && |
930 | ovs_flow_cmp_ufid(flow, ufid)) | |
4a46b24e AW |
931 | return flow; |
932 | } | |
933 | return NULL; | |
934 | } | |
935 | ||
1bd7116f AZ |
936 | int ovs_flow_tbl_num_masks(const struct flow_table *table) |
937 | { | |
4bc63b1b | 938 | struct mask_array *ma = rcu_dereference_ovsl(table->mask_array); |
57f7d7b9 | 939 | return READ_ONCE(ma->count); |
1bd7116f AZ |
940 | } |
941 | ||
9bf24f59 EC |
942 | u32 ovs_flow_tbl_masks_cache_size(const struct flow_table *table) |
943 | { | |
5845589e | 944 | struct mask_cache *mc = rcu_dereference_ovsl(table->mask_cache); |
9bf24f59 EC |
945 | |
946 | return READ_ONCE(mc->cache_size); | |
947 | } | |
948 | ||
74ed7ab9 JS |
949 | static struct table_instance *table_instance_expand(struct table_instance *ti, |
950 | bool ufid) | |
b637e498 | 951 | { |
74ed7ab9 | 952 | return table_instance_rehash(ti, ti->n_buckets * 2, ufid); |
e6445719 PS |
953 | } |
954 | ||
56c19868 | 955 | /* Must be called with OVS mutex held. */ |
e6445719 PS |
956 | void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) |
957 | { | |
b637e498 | 958 | struct table_instance *ti = ovsl_dereference(table->ti); |
74ed7ab9 | 959 | struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti); |
b637e498 | 960 | |
e6445719 | 961 | BUG_ON(table->count == 0); |
df68d64e | 962 | table_instance_flow_free(table, ti, ufid_ti, flow); |
e6445719 PS |
963 | } |
964 | ||
618ed0c8 | 965 | static struct sw_flow_mask *mask_alloc(void) |
e6445719 PS |
966 | { |
967 | struct sw_flow_mask *mask; | |
968 | ||
969 | mask = kmalloc(sizeof(*mask), GFP_KERNEL); | |
970 | if (mask) | |
e80857cc | 971 | mask->ref_count = 1; |
e6445719 PS |
972 | |
973 | return mask; | |
974 | } | |
975 | ||
e6445719 PS |
976 | static bool mask_equal(const struct sw_flow_mask *a, |
977 | const struct sw_flow_mask *b) | |
978 | { | |
7085130b DDP |
979 | const u8 *a_ = (const u8 *)&a->key + a->range.start; |
980 | const u8 *b_ = (const u8 *)&b->key + b->range.start; | |
e6445719 PS |
981 | |
982 | return (a->range.end == b->range.end) | |
983 | && (a->range.start == b->range.start) | |
984 | && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0); | |
985 | } | |
986 | ||
618ed0c8 | 987 | static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, |
e6445719 PS |
988 | const struct sw_flow_mask *mask) |
989 | { | |
4bc63b1b TZ |
990 | struct mask_array *ma; |
991 | int i; | |
e6445719 | 992 | |
4bc63b1b TZ |
993 | ma = ovsl_dereference(tbl->mask_array); |
994 | for (i = 0; i < ma->max; i++) { | |
995 | struct sw_flow_mask *t; | |
996 | t = ovsl_dereference(ma->masks[i]); | |
997 | ||
998 | if (t && mask_equal(mask, t)) | |
999 | return t; | |
e6445719 PS |
1000 | } |
1001 | ||
1002 | return NULL; | |
1003 | } | |
1004 | ||
d1211908 | 1005 | /* Add 'mask' into the mask list, if it is not already there. */ |
618ed0c8 | 1006 | static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, |
12eb18f7 | 1007 | const struct sw_flow_mask *new) |
618ed0c8 PS |
1008 | { |
1009 | struct sw_flow_mask *mask; | |
4bc63b1b | 1010 | |
618ed0c8 PS |
1011 | mask = flow_mask_find(tbl, new); |
1012 | if (!mask) { | |
1b381f6f | 1013 | /* Allocate a new mask if none exists. */ |
618ed0c8 PS |
1014 | mask = mask_alloc(); |
1015 | if (!mask) | |
1016 | return -ENOMEM; | |
1017 | mask->key = new->key; | |
1018 | mask->range = new->range; | |
4bc63b1b TZ |
1019 | |
1020 | /* Add mask to mask-list. */ | |
57f7d7b9 TZ |
1021 | if (tbl_mask_array_add_mask(tbl, mask)) { |
1022 | kfree(mask); | |
1023 | return -ENOMEM; | |
4bc63b1b | 1024 | } |
e80857cc AZ |
1025 | } else { |
1026 | BUG_ON(!mask->ref_count); | |
1027 | mask->ref_count++; | |
618ed0c8 PS |
1028 | } |
1029 | ||
618ed0c8 PS |
1030 | flow->mask = mask; |
1031 | return 0; | |
1032 | } | |
1033 | ||
56c19868 | 1034 | /* Must be called with OVS mutex held. */ |
d29ab6f8 | 1035 | static void flow_key_insert(struct flow_table *table, struct sw_flow *flow) |
e6445719 | 1036 | { |
618ed0c8 PS |
1037 | struct table_instance *new_ti = NULL; |
1038 | struct table_instance *ti; | |
618ed0c8 | 1039 | |
74ed7ab9 | 1040 | flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range); |
618ed0c8 PS |
1041 | ti = ovsl_dereference(table->ti); |
1042 | table_instance_insert(ti, flow); | |
1043 | table->count++; | |
1044 | ||
1045 | /* Expand table, if necessary, to make room. */ | |
1046 | if (table->count > ti->n_buckets) | |
74ed7ab9 | 1047 | new_ti = table_instance_expand(ti, false); |
618ed0c8 | 1048 | else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL)) |
74ed7ab9 | 1049 | new_ti = table_instance_rehash(ti, ti->n_buckets, false); |
618ed0c8 PS |
1050 | |
1051 | if (new_ti) { | |
1052 | rcu_assign_pointer(table->ti, new_ti); | |
74ed7ab9 | 1053 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); |
618ed0c8 PS |
1054 | table->last_rehash = jiffies; |
1055 | } | |
d29ab6f8 JS |
1056 | } |
1057 | ||
74ed7ab9 JS |
1058 | /* Must be called with OVS mutex held. */ |
1059 | static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow) | |
1060 | { | |
1061 | struct table_instance *ti; | |
1062 | ||
1063 | flow->ufid_table.hash = ufid_hash(&flow->id); | |
1064 | ti = ovsl_dereference(table->ufid_ti); | |
1065 | ufid_table_instance_insert(ti, flow); | |
1066 | table->ufid_count++; | |
1067 | ||
1068 | /* Expand table, if necessary, to make room. */ | |
1069 | if (table->ufid_count > ti->n_buckets) { | |
1070 | struct table_instance *new_ti; | |
1071 | ||
1072 | new_ti = table_instance_expand(ti, true); | |
1073 | if (new_ti) { | |
1074 | rcu_assign_pointer(table->ufid_ti, new_ti); | |
1075 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); | |
1076 | } | |
1077 | } | |
1078 | } | |
1079 | ||
d29ab6f8 JS |
1080 | /* Must be called with OVS mutex held. */ |
1081 | int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, | |
1082 | const struct sw_flow_mask *mask) | |
1083 | { | |
1084 | int err; | |
1085 | ||
1086 | err = flow_mask_insert(table, flow, mask); | |
1087 | if (err) | |
1088 | return err; | |
1089 | flow_key_insert(table, flow); | |
74ed7ab9 JS |
1090 | if (ovs_identifier_is_ufid(&flow->id)) |
1091 | flow_ufid_insert(table, flow); | |
d29ab6f8 | 1092 | |
618ed0c8 | 1093 | return 0; |
e6445719 PS |
1094 | } |
1095 | ||
eac87c41 EC |
1096 | static int compare_mask_and_count(const void *a, const void *b) |
1097 | { | |
1098 | const struct mask_count *mc_a = a; | |
1099 | const struct mask_count *mc_b = b; | |
1100 | ||
1101 | return (s64)mc_b->counter - (s64)mc_a->counter; | |
1102 | } | |
1103 | ||
1104 | /* Must be called with OVS mutex held. */ | |
1105 | void ovs_flow_masks_rebalance(struct flow_table *table) | |
1106 | { | |
1107 | struct mask_array *ma = rcu_dereference_ovsl(table->mask_array); | |
1108 | struct mask_count *masks_and_count; | |
1109 | struct mask_array *new; | |
1110 | int masks_entries = 0; | |
1111 | int i; | |
1112 | ||
1113 | /* Build array of all current entries with use counters. */ | |
1114 | masks_and_count = kmalloc_array(ma->max, sizeof(*masks_and_count), | |
1115 | GFP_KERNEL); | |
1116 | if (!masks_and_count) | |
1117 | return; | |
1118 | ||
cf3266ad | 1119 | for (i = 0; i < ma->max; i++) { |
eac87c41 | 1120 | struct sw_flow_mask *mask; |
eac87c41 EC |
1121 | int cpu; |
1122 | ||
1123 | mask = rcu_dereference_ovsl(ma->masks[i]); | |
1124 | if (unlikely(!mask)) | |
1125 | break; | |
1126 | ||
1127 | masks_and_count[i].index = i; | |
1128 | masks_and_count[i].counter = 0; | |
1129 | ||
1130 | for_each_possible_cpu(cpu) { | |
f981fc3d EC |
1131 | struct mask_array_stats *stats; |
1132 | unsigned int start; | |
eac87c41 EC |
1133 | u64 counter; |
1134 | ||
f981fc3d | 1135 | stats = per_cpu_ptr(ma->masks_usage_stats, cpu); |
eac87c41 | 1136 | do { |
d120d1a6 | 1137 | start = u64_stats_fetch_begin(&stats->syncp); |
f981fc3d | 1138 | counter = stats->usage_cntrs[i]; |
d120d1a6 | 1139 | } while (u64_stats_fetch_retry(&stats->syncp, start)); |
eac87c41 EC |
1140 | |
1141 | masks_and_count[i].counter += counter; | |
1142 | } | |
1143 | ||
1144 | /* Subtract the zero count value. */ | |
1145 | masks_and_count[i].counter -= ma->masks_usage_zero_cntr[i]; | |
1146 | ||
1147 | /* Rather than calling tbl_mask_array_reset_counters() | |
1148 | * below when no change is needed, do it inline here. | |
1149 | */ | |
1150 | ma->masks_usage_zero_cntr[i] += masks_and_count[i].counter; | |
1151 | } | |
1152 | ||
1153 | if (i == 0) | |
1154 | goto free_mask_entries; | |
1155 | ||
1156 | /* Sort the entries */ | |
1157 | masks_entries = i; | |
1158 | sort(masks_and_count, masks_entries, sizeof(*masks_and_count), | |
1159 | compare_mask_and_count, NULL); | |
1160 | ||
1161 | /* If the order is the same, nothing to do... */ | |
1162 | for (i = 0; i < masks_entries; i++) { | |
1163 | if (i != masks_and_count[i].index) | |
1164 | break; | |
1165 | } | |
1166 | if (i == masks_entries) | |
1167 | goto free_mask_entries; | |
1168 | ||
1169 | /* Rebuilt the new list in order of usage. */ | |
1170 | new = tbl_mask_array_alloc(ma->max); | |
1171 | if (!new) | |
1172 | goto free_mask_entries; | |
1173 | ||
1174 | for (i = 0; i < masks_entries; i++) { | |
1175 | int index = masks_and_count[i].index; | |
1176 | ||
9bf24f59 EC |
1177 | if (ovsl_dereference(ma->masks[index])) |
1178 | new->masks[new->count++] = ma->masks[index]; | |
eac87c41 EC |
1179 | } |
1180 | ||
1181 | rcu_assign_pointer(table->mask_array, new); | |
1182 | call_rcu(&ma->rcu, mask_array_rcu_cb); | |
1183 | ||
1184 | free_mask_entries: | |
1185 | kfree(masks_and_count); | |
1186 | } | |
1187 | ||
e6445719 PS |
1188 | /* Initializes the flow module. |
1189 | * Returns zero if successful or a negative error code. */ | |
1190 | int ovs_flow_init(void) | |
1191 | { | |
1192 | BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long)); | |
1193 | BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long)); | |
1194 | ||
63e7959c | 1195 | flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow) |
db74a333 | 1196 | + (nr_cpu_ids |
15ea59a0 ET |
1197 | * sizeof(struct sw_flow_stats *)) |
1198 | + cpumask_size(), | |
63e7959c | 1199 | 0, 0, NULL); |
e6445719 PS |
1200 | if (flow_cache == NULL) |
1201 | return -ENOMEM; | |
1202 | ||
63e7959c | 1203 | flow_stats_cache |
aef833c5 | 1204 | = kmem_cache_create("sw_flow_stats", sizeof(struct sw_flow_stats), |
63e7959c JR |
1205 | 0, SLAB_HWCACHE_ALIGN, NULL); |
1206 | if (flow_stats_cache == NULL) { | |
1207 | kmem_cache_destroy(flow_cache); | |
1208 | flow_cache = NULL; | |
1209 | return -ENOMEM; | |
1210 | } | |
1211 | ||
e6445719 PS |
1212 | return 0; |
1213 | } | |
1214 | ||
1215 | /* Uninitializes the flow module. */ | |
1216 | void ovs_flow_exit(void) | |
1217 | { | |
63e7959c | 1218 | kmem_cache_destroy(flow_stats_cache); |
e6445719 PS |
1219 | kmem_cache_destroy(flow_cache); |
1220 | } |