Merge branch 'core-objtool-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / net / openvswitch / flow_table.c
CommitLineData
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>
32#include <net/ip.h>
33#include <net/ipv6.h>
34#include <net/ndisc.h>
35
b637e498 36#define TBL_MIN_BUCKETS 1024
4bc63b1b 37#define MASK_ARRAY_SIZE_MIN 16
b637e498
PS
38#define REHASH_INTERVAL (10 * 60 * HZ)
39
04b7d136
TZ
40#define MC_HASH_SHIFT 8
41#define MC_HASH_ENTRIES (1u << MC_HASH_SHIFT)
42#define MC_HASH_SEGS ((sizeof(uint32_t) * 8) / MC_HASH_SHIFT)
43
e6445719 44static struct kmem_cache *flow_cache;
63e7959c 45struct kmem_cache *flow_stats_cache __read_mostly;
e6445719
PS
46
47static u16 range_n_bytes(const struct sw_flow_key_range *range)
48{
49 return range->end - range->start;
50}
51
52void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
ae5f2fb1 53 bool full, const struct sw_flow_mask *mask)
e6445719 54{
ae5f2fb1
JG
55 int start = full ? 0 : mask->range.start;
56 int len = full ? sizeof *dst : range_n_bytes(&mask->range);
57 const long *m = (const long *)((const u8 *)&mask->key + start);
58 const long *s = (const long *)((const u8 *)src + start);
59 long *d = (long *)((u8 *)dst + start);
e6445719
PS
60 int i;
61
ae5f2fb1
JG
62 /* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
63 * if 'full' is false the memory outside of the 'mask->range' is left
64 * uninitialized. This can be used as an optimization when further
65 * operations on 'dst' only use contents within 'mask->range'.
e6445719 66 */
ae5f2fb1 67 for (i = 0; i < len; i += sizeof(long))
e6445719
PS
68 *d++ = *s++ & *m++;
69}
70
23dabf88 71struct sw_flow *ovs_flow_alloc(void)
e6445719
PS
72{
73 struct sw_flow *flow;
aef833c5 74 struct sw_flow_stats *stats;
e6445719 75
db74a333 76 flow = kmem_cache_zalloc(flow_cache, GFP_KERNEL);
e6445719
PS
77 if (!flow)
78 return ERR_PTR(-ENOMEM);
79
db74a333 80 flow->stats_last_writer = -1;
e6445719 81
63e7959c
JR
82 /* Initialize the default stat node. */
83 stats = kmem_cache_alloc_node(flow_stats_cache,
598c12d0
KK
84 GFP_KERNEL | __GFP_ZERO,
85 node_online(0) ? 0 : NUMA_NO_NODE);
63e7959c 86 if (!stats)
23dabf88 87 goto err;
e298e505 88
63e7959c
JR
89 spin_lock_init(&stats->lock);
90
91 RCU_INIT_POINTER(flow->stats[0], stats);
92
c4b2bf6b
TZ
93 cpumask_set_cpu(0, &flow->cpu_used_mask);
94
e6445719 95 return flow;
e298e505 96err:
ece37c87 97 kmem_cache_free(flow_cache, flow);
e298e505 98 return ERR_PTR(-ENOMEM);
e6445719
PS
99}
100
12eb18f7 101int ovs_flow_tbl_count(const struct flow_table *table)
b637e498
PS
102{
103 return table->count;
104}
105
e6445719
PS
106static void flow_free(struct sw_flow *flow)
107{
db74a333 108 int cpu;
63e7959c 109
74ed7ab9
JS
110 if (ovs_identifier_is_key(&flow->id))
111 kfree(flow->id.unmasked_key);
34ae932a
TG
112 if (flow->sf_acts)
113 ovs_nla_free_flow_actions((struct sw_flow_actions __force *)flow->sf_acts);
db74a333 114 /* We open code this to make sure cpu 0 is always considered */
c4b2bf6b 115 for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, &flow->cpu_used_mask))
db74a333 116 if (flow->stats[cpu])
63e7959c 117 kmem_cache_free(flow_stats_cache,
aef833c5 118 (struct sw_flow_stats __force *)flow->stats[cpu]);
e6445719
PS
119 kmem_cache_free(flow_cache, flow);
120}
121
122static void rcu_free_flow_callback(struct rcu_head *rcu)
123{
124 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
125
126 flow_free(flow);
127}
128
e80857cc 129void ovs_flow_free(struct sw_flow *flow, bool deferred)
618ed0c8 130{
e80857cc 131 if (!flow)
618ed0c8
PS
132 return;
133
e6445719
PS
134 if (deferred)
135 call_rcu(&flow->rcu, rcu_free_flow_callback);
136 else
137 flow_free(flow);
138}
139
b637e498 140static void __table_instance_destroy(struct table_instance *ti)
e6445719 141{
ee9c5e67 142 kvfree(ti->buckets);
b637e498 143 kfree(ti);
e6445719
PS
144}
145
b637e498 146static struct table_instance *table_instance_alloc(int new_size)
e6445719 147{
b637e498 148 struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
ee9c5e67 149 int i;
e6445719 150
b637e498 151 if (!ti)
e6445719
PS
152 return NULL;
153
ee9c5e67
KO
154 ti->buckets = kvmalloc_array(new_size, sizeof(struct hlist_head),
155 GFP_KERNEL);
b637e498
PS
156 if (!ti->buckets) {
157 kfree(ti);
e6445719
PS
158 return NULL;
159 }
ee9c5e67
KO
160
161 for (i = 0; i < new_size; i++)
162 INIT_HLIST_HEAD(&ti->buckets[i]);
163
b637e498
PS
164 ti->n_buckets = new_size;
165 ti->node_ver = 0;
166 ti->keep_flows = false;
167 get_random_bytes(&ti->hash_seed, sizeof(u32));
e6445719 168
b637e498 169 return ti;
e6445719
PS
170}
171
4bc63b1b
TZ
172static struct mask_array *tbl_mask_array_alloc(int size)
173{
174 struct mask_array *new;
175
176 size = max(MASK_ARRAY_SIZE_MIN, size);
177 new = kzalloc(sizeof(struct mask_array) +
178 sizeof(struct sw_flow_mask *) * size, GFP_KERNEL);
179 if (!new)
180 return NULL;
181
182 new->count = 0;
183 new->max = size;
184
185 return new;
186}
187
188static int tbl_mask_array_realloc(struct flow_table *tbl, int size)
189{
190 struct mask_array *old;
191 struct mask_array *new;
192
193 new = tbl_mask_array_alloc(size);
194 if (!new)
195 return -ENOMEM;
196
197 old = ovsl_dereference(tbl->mask_array);
198 if (old) {
199 int i;
200
201 for (i = 0; i < old->max; i++) {
202 if (ovsl_dereference(old->masks[i]))
203 new->masks[new->count++] = old->masks[i];
204 }
205 }
206
207 rcu_assign_pointer(tbl->mask_array, new);
208 kfree_rcu(old, rcu);
209
210 return 0;
211}
212
50b0e61b
TZ
213static int tbl_mask_array_add_mask(struct flow_table *tbl,
214 struct sw_flow_mask *new)
215{
216 struct mask_array *ma = ovsl_dereference(tbl->mask_array);
217 int err, ma_count = READ_ONCE(ma->count);
218
219 if (ma_count >= ma->max) {
220 err = tbl_mask_array_realloc(tbl, ma->max +
221 MASK_ARRAY_SIZE_MIN);
222 if (err)
223 return err;
224
225 ma = ovsl_dereference(tbl->mask_array);
226 }
227
228 BUG_ON(ovsl_dereference(ma->masks[ma_count]));
229
230 rcu_assign_pointer(ma->masks[ma_count], new);
231 WRITE_ONCE(ma->count, ma_count +1);
232
233 return 0;
234}
235
236static void tbl_mask_array_del_mask(struct flow_table *tbl,
237 struct sw_flow_mask *mask)
238{
239 struct mask_array *ma = ovsl_dereference(tbl->mask_array);
240 int i, ma_count = READ_ONCE(ma->count);
241
242 /* Remove the deleted mask pointers from the array */
243 for (i = 0; i < ma_count; i++) {
244 if (mask == ovsl_dereference(ma->masks[i]))
245 goto found;
246 }
247
248 BUG();
249 return;
250
251found:
252 WRITE_ONCE(ma->count, ma_count -1);
253
254 rcu_assign_pointer(ma->masks[i], ma->masks[ma_count -1]);
255 RCU_INIT_POINTER(ma->masks[ma_count -1], NULL);
256
257 kfree_rcu(mask, rcu);
258
259 /* Shrink the mask array if necessary. */
260 if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) &&
261 ma_count <= (ma->max / 3))
262 tbl_mask_array_realloc(tbl, ma->max / 2);
263}
264
265/* Remove 'mask' from the mask list, if it is not needed any more. */
266static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
267{
268 if (mask) {
269 /* ovs-lock is required to protect mask-refcount and
270 * mask list.
271 */
272 ASSERT_OVSL();
273 BUG_ON(!mask->ref_count);
274 mask->ref_count--;
275
276 if (!mask->ref_count)
277 tbl_mask_array_del_mask(tbl, mask);
278 }
279}
280
b637e498 281int ovs_flow_tbl_init(struct flow_table *table)
e6445719 282{
74ed7ab9 283 struct table_instance *ti, *ufid_ti;
4bc63b1b 284 struct mask_array *ma;
e6445719 285
04b7d136
TZ
286 table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) *
287 MC_HASH_ENTRIES,
288 __alignof__(struct mask_cache_entry));
289 if (!table->mask_cache)
290 return -ENOMEM;
e6445719 291
4bc63b1b
TZ
292 ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
293 if (!ma)
294 goto free_mask_cache;
295
04b7d136 296 ti = table_instance_alloc(TBL_MIN_BUCKETS);
b637e498 297 if (!ti)
4bc63b1b 298 goto free_mask_array;
e6445719 299
74ed7ab9
JS
300 ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
301 if (!ufid_ti)
302 goto free_ti;
303
b637e498 304 rcu_assign_pointer(table->ti, ti);
74ed7ab9 305 rcu_assign_pointer(table->ufid_ti, ufid_ti);
4bc63b1b 306 rcu_assign_pointer(table->mask_array, ma);
b637e498
PS
307 table->last_rehash = jiffies;
308 table->count = 0;
74ed7ab9 309 table->ufid_count = 0;
b637e498 310 return 0;
74ed7ab9
JS
311
312free_ti:
313 __table_instance_destroy(ti);
4bc63b1b
TZ
314free_mask_array:
315 kfree(ma);
04b7d136
TZ
316free_mask_cache:
317 free_percpu(table->mask_cache);
74ed7ab9 318 return -ENOMEM;
e6445719
PS
319}
320
321static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
322{
b637e498 323 struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
e6445719 324
b637e498 325 __table_instance_destroy(ti);
e6445719
PS
326}
327
50b0e61b
TZ
328static void table_instance_flow_free(struct flow_table *table,
329 struct table_instance *ti,
330 struct table_instance *ufid_ti,
331 struct sw_flow *flow,
332 bool count)
333{
334 hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
335 if (count)
336 table->count--;
337
338 if (ovs_identifier_is_ufid(&flow->id)) {
339 hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
340
341 if (count)
342 table->ufid_count--;
343 }
344
345 flow_mask_remove(table, flow->mask);
346}
347
348static void table_instance_destroy(struct flow_table *table,
349 struct table_instance *ti,
74ed7ab9
JS
350 struct table_instance *ufid_ti,
351 bool deferred)
e6445719 352{
e80857cc
AZ
353 int i;
354
b637e498 355 if (!ti)
e6445719
PS
356 return;
357
74ed7ab9 358 BUG_ON(!ufid_ti);
e80857cc
AZ
359 if (ti->keep_flows)
360 goto skip_flows;
361
362 for (i = 0; i < ti->n_buckets; i++) {
363 struct sw_flow *flow;
ee9c5e67 364 struct hlist_head *head = &ti->buckets[i];
e80857cc 365 struct hlist_node *n;
e80857cc 366
50b0e61b
TZ
367 hlist_for_each_entry_safe(flow, n, head,
368 flow_table.node[ti->node_ver]) {
369
370 table_instance_flow_free(table, ti, ufid_ti,
371 flow, false);
e80857cc
AZ
372 ovs_flow_free(flow, deferred);
373 }
374 }
375
376skip_flows:
74ed7ab9 377 if (deferred) {
b637e498 378 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
74ed7ab9
JS
379 call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
380 } else {
b637e498 381 __table_instance_destroy(ti);
74ed7ab9
JS
382 __table_instance_destroy(ufid_ti);
383 }
b637e498
PS
384}
385
9b996e54
PS
386/* No need for locking this function is called from RCU callback or
387 * error path.
388 */
389void ovs_flow_tbl_destroy(struct flow_table *table)
b637e498 390{
9b996e54 391 struct table_instance *ti = rcu_dereference_raw(table->ti);
74ed7ab9 392 struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
b637e498 393
04b7d136 394 free_percpu(table->mask_cache);
4bc63b1b 395 kfree_rcu(rcu_dereference_raw(table->mask_array), rcu);
50b0e61b 396 table_instance_destroy(table, ti, ufid_ti, false);
e6445719
PS
397}
398
b637e498 399struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
e6445719
PS
400 u32 *bucket, u32 *last)
401{
402 struct sw_flow *flow;
403 struct hlist_head *head;
404 int ver;
405 int i;
406
b637e498
PS
407 ver = ti->node_ver;
408 while (*bucket < ti->n_buckets) {
e6445719 409 i = 0;
ee9c5e67 410 head = &ti->buckets[*bucket];
74ed7ab9 411 hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
e6445719
PS
412 if (i < *last) {
413 i++;
414 continue;
415 }
416 *last = i + 1;
417 return flow;
418 }
419 (*bucket)++;
420 *last = 0;
421 }
422
423 return NULL;
424}
425
b637e498 426static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
e6445719 427{
b637e498 428 hash = jhash_1word(hash, ti->hash_seed);
ee9c5e67 429 return &ti->buckets[hash & (ti->n_buckets - 1)];
e6445719
PS
430}
431
74ed7ab9
JS
432static void table_instance_insert(struct table_instance *ti,
433 struct sw_flow *flow)
e6445719
PS
434{
435 struct hlist_head *head;
436
74ed7ab9
JS
437 head = find_bucket(ti, flow->flow_table.hash);
438 hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
439}
440
441static void ufid_table_instance_insert(struct table_instance *ti,
442 struct sw_flow *flow)
443{
444 struct hlist_head *head;
445
446 head = find_bucket(ti, flow->ufid_table.hash);
447 hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
e6445719
PS
448}
449
b637e498 450static void flow_table_copy_flows(struct table_instance *old,
74ed7ab9 451 struct table_instance *new, bool ufid)
e6445719
PS
452{
453 int old_ver;
454 int i;
455
456 old_ver = old->node_ver;
457 new->node_ver = !old_ver;
458
459 /* Insert in new table. */
460 for (i = 0; i < old->n_buckets; i++) {
461 struct sw_flow *flow;
ee9c5e67 462 struct hlist_head *head = &old->buckets[i];
e6445719 463
74ed7ab9
JS
464 if (ufid)
465 hlist_for_each_entry(flow, head,
466 ufid_table.node[old_ver])
467 ufid_table_instance_insert(new, flow);
468 else
469 hlist_for_each_entry(flow, head,
470 flow_table.node[old_ver])
471 table_instance_insert(new, flow);
e6445719
PS
472 }
473
e6445719
PS
474 old->keep_flows = true;
475}
476
b637e498 477static struct table_instance *table_instance_rehash(struct table_instance *ti,
74ed7ab9 478 int n_buckets, bool ufid)
e6445719 479{
b637e498 480 struct table_instance *new_ti;
e6445719 481
b637e498
PS
482 new_ti = table_instance_alloc(n_buckets);
483 if (!new_ti)
618ed0c8 484 return NULL;
e6445719 485
74ed7ab9 486 flow_table_copy_flows(ti, new_ti, ufid);
e6445719 487
b637e498 488 return new_ti;
e6445719
PS
489}
490
b637e498 491int ovs_flow_tbl_flush(struct flow_table *flow_table)
e6445719 492{
74ed7ab9
JS
493 struct table_instance *old_ti, *new_ti;
494 struct table_instance *old_ufid_ti, *new_ufid_ti;
e6445719 495
b637e498
PS
496 new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
497 if (!new_ti)
498 return -ENOMEM;
74ed7ab9
JS
499 new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
500 if (!new_ufid_ti)
501 goto err_free_ti;
502
503 old_ti = ovsl_dereference(flow_table->ti);
504 old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
b637e498
PS
505
506 rcu_assign_pointer(flow_table->ti, new_ti);
74ed7ab9 507 rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
b637e498
PS
508 flow_table->last_rehash = jiffies;
509 flow_table->count = 0;
74ed7ab9 510 flow_table->ufid_count = 0;
b637e498 511
50b0e61b 512 table_instance_destroy(flow_table, old_ti, old_ufid_ti, true);
b637e498 513 return 0;
74ed7ab9
JS
514
515err_free_ti:
516 __table_instance_destroy(new_ti);
517 return -ENOMEM;
e6445719
PS
518}
519
272c2cf8
JS
520static u32 flow_hash(const struct sw_flow_key *key,
521 const struct sw_flow_key_range *range)
e6445719 522{
515b65a4 523 const u32 *hash_key = (const u32 *)((const u8 *)key + range->start);
e6445719
PS
524
525 /* Make sure number of hash bytes are multiple of u32. */
515b65a4 526 int hash_u32s = range_n_bytes(range) >> 2;
e6445719 527
87545899 528 return jhash2(hash_key, hash_u32s, 0);
e6445719
PS
529}
530
531static int flow_key_start(const struct sw_flow_key *key)
532{
00a93bab 533 if (key->tun_proto)
e6445719
PS
534 return 0;
535 else
536 return rounddown(offsetof(struct sw_flow_key, phy),
537 sizeof(long));
538}
539
540static bool cmp_key(const struct sw_flow_key *key1,
541 const struct sw_flow_key *key2,
542 int key_start, int key_end)
543{
7085130b
DDP
544 const long *cp1 = (const long *)((const u8 *)key1 + key_start);
545 const long *cp2 = (const long *)((const u8 *)key2 + key_start);
e6445719
PS
546 long diffs = 0;
547 int i;
548
549 for (i = key_start; i < key_end; i += sizeof(long))
550 diffs |= *cp1++ ^ *cp2++;
551
552 return diffs == 0;
553}
554
555static bool flow_cmp_masked_key(const struct sw_flow *flow,
556 const struct sw_flow_key *key,
272c2cf8 557 const struct sw_flow_key_range *range)
e6445719 558{
272c2cf8 559 return cmp_key(&flow->key, key, range->start, range->end);
e6445719
PS
560}
561
74ed7ab9
JS
562static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
563 const struct sw_flow_match *match)
e6445719
PS
564{
565 struct sw_flow_key *key = match->key;
566 int key_start = flow_key_start(key);
567 int key_end = match->range.end;
568
74ed7ab9
JS
569 BUG_ON(ovs_identifier_is_ufid(&flow->id));
570 return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
e6445719
PS
571}
572
b637e498 573static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
e6445719 574 const struct sw_flow_key *unmasked,
04b7d136
TZ
575 const struct sw_flow_mask *mask,
576 u32 *n_mask_hit)
e6445719
PS
577{
578 struct sw_flow *flow;
579 struct hlist_head *head;
e6445719
PS
580 u32 hash;
581 struct sw_flow_key masked_key;
582
ae5f2fb1 583 ovs_flow_mask_key(&masked_key, unmasked, false, mask);
272c2cf8 584 hash = flow_hash(&masked_key, &mask->range);
b637e498 585 head = find_bucket(ti, hash);
04b7d136
TZ
586 (*n_mask_hit)++;
587
a2cfb96c
MB
588 hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver],
589 lockdep_ovsl_is_held()) {
74ed7ab9 590 if (flow->mask == mask && flow->flow_table.hash == hash &&
272c2cf8 591 flow_cmp_masked_key(flow, &masked_key, &mask->range))
e6445719
PS
592 return flow;
593 }
594 return NULL;
595}
596
a7f35e78
TZ
597/* Flow lookup does full lookup on flow table. It starts with
598 * mask from index passed in *index.
599 */
04b7d136
TZ
600static struct sw_flow *flow_lookup(struct flow_table *tbl,
601 struct table_instance *ti,
4bc63b1b 602 struct mask_array *ma,
04b7d136 603 const struct sw_flow_key *key,
4bc63b1b
TZ
604 u32 *n_mask_hit,
605 u32 *index)
e6445719 606{
b637e498 607 struct sw_flow *flow;
57f7d7b9 608 struct sw_flow_mask *mask;
4bc63b1b 609 int i;
e6445719 610
0a3e0137 611 if (likely(*index < ma->max)) {
a7f35e78 612 mask = rcu_dereference_ovsl(ma->masks[*index]);
4bc63b1b
TZ
613 if (mask) {
614 flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
a7f35e78 615 if (flow)
4bc63b1b 616 return flow;
a7f35e78
TZ
617 }
618 }
619
620 for (i = 0; i < ma->max; i++) {
621
622 if (i == *index)
623 continue;
624
625 mask = rcu_dereference_ovsl(ma->masks[i]);
0a3e0137 626 if (unlikely(!mask))
57f7d7b9 627 break;
a7f35e78
TZ
628
629 flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
630 if (flow) { /* Found */
631 *index = i;
632 return flow;
4bc63b1b 633 }
e6445719 634 }
4bc63b1b 635
b637e498
PS
636 return NULL;
637}
e6445719 638
04b7d136
TZ
639/*
640 * mask_cache maps flow to probable mask. This cache is not tightly
641 * coupled cache, It means updates to mask list can result in inconsistent
642 * cache entry in mask cache.
643 * This is per cpu cache and is divided in MC_HASH_SEGS segments.
644 * In case of a hash collision the entry is hashed in next segment.
645 * */
646struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
647 const struct sw_flow_key *key,
648 u32 skb_hash,
649 u32 *n_mask_hit)
650{
a7f35e78
TZ
651 struct mask_array *ma = rcu_dereference(tbl->mask_array);
652 struct table_instance *ti = rcu_dereference(tbl->ti);
653 struct mask_cache_entry *entries, *ce;
04b7d136 654 struct sw_flow *flow;
a7f35e78 655 u32 hash;
04b7d136
TZ
656 int seg;
657
658 *n_mask_hit = 0;
4bc63b1b 659 if (unlikely(!skb_hash)) {
a7f35e78 660 u32 mask_index = 0;
4bc63b1b
TZ
661
662 return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index);
663 }
04b7d136 664
a7f35e78
TZ
665 /* Pre and post recirulation flows usually have the same skb_hash
666 * value. To avoid hash collisions, rehash the 'skb_hash' with
667 * 'recirc_id'. */
668 if (key->recirc_id)
669 skb_hash = jhash_1word(skb_hash, key->recirc_id);
670
671 ce = NULL;
672 hash = skb_hash;
04b7d136
TZ
673 entries = this_cpu_ptr(tbl->mask_cache);
674
a7f35e78 675 /* Find the cache entry 'ce' to operate on. */
04b7d136 676 for (seg = 0; seg < MC_HASH_SEGS; seg++) {
a7f35e78
TZ
677 int index = hash & (MC_HASH_ENTRIES - 1);
678 struct mask_cache_entry *e;
679
680 e = &entries[index];
681 if (e->skb_hash == skb_hash) {
682 flow = flow_lookup(tbl, ti, ma, key, n_mask_hit,
683 &e->mask_index);
684 if (!flow)
685 e->skb_hash = 0;
686 return flow;
04b7d136
TZ
687 }
688
a7f35e78
TZ
689 if (!ce || e->skb_hash < ce->skb_hash)
690 ce = e; /* A better replacement cache candidate. */
04b7d136
TZ
691
692 hash >>= MC_HASH_SHIFT;
693 }
694
a7f35e78
TZ
695 /* Cache miss, do full lookup. */
696 flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &ce->mask_index);
4bc63b1b 697 if (flow)
a7f35e78 698 ce->skb_hash = skb_hash;
04b7d136
TZ
699
700 return flow;
701}
702
5bb50632
AZ
703struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
704 const struct sw_flow_key *key)
705{
04b7d136 706 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
4bc63b1b 707 struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
5bb50632 708 u32 __always_unused n_mask_hit;
a7f35e78 709 u32 index = 0;
5bb50632 710
4bc63b1b 711 return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index);
5bb50632
AZ
712}
713
4a46b24e 714struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
12eb18f7 715 const struct sw_flow_match *match)
4a46b24e 716{
4bc63b1b
TZ
717 struct mask_array *ma = ovsl_dereference(tbl->mask_array);
718 int i;
4a46b24e
AW
719
720 /* Always called under ovs-mutex. */
4bc63b1b
TZ
721 for (i = 0; i < ma->max; i++) {
722 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
723 u32 __always_unused n_mask_hit;
724 struct sw_flow_mask *mask;
725 struct sw_flow *flow;
726
727 mask = ovsl_dereference(ma->masks[i]);
728 if (!mask)
729 continue;
730
04b7d136 731 flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit);
74ed7ab9 732 if (flow && ovs_identifier_is_key(&flow->id) &&
4bc63b1b 733 ovs_flow_cmp_unmasked_key(flow, match)) {
74ed7ab9 734 return flow;
4bc63b1b 735 }
74ed7ab9 736 }
4bc63b1b 737
74ed7ab9
JS
738 return NULL;
739}
740
741static u32 ufid_hash(const struct sw_flow_id *sfid)
742{
743 return jhash(sfid->ufid, sfid->ufid_len, 0);
744}
745
746static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
747 const struct sw_flow_id *sfid)
748{
749 if (flow->id.ufid_len != sfid->ufid_len)
750 return false;
751
752 return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
753}
754
755bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
756{
757 if (ovs_identifier_is_ufid(&flow->id))
758 return flow_cmp_masked_key(flow, match->key, &match->range);
759
760 return ovs_flow_cmp_unmasked_key(flow, match);
761}
762
763struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
764 const struct sw_flow_id *ufid)
765{
766 struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
767 struct sw_flow *flow;
768 struct hlist_head *head;
769 u32 hash;
770
771 hash = ufid_hash(ufid);
772 head = find_bucket(ti, hash);
a2cfb96c
MB
773 hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver],
774 lockdep_ovsl_is_held()) {
74ed7ab9
JS
775 if (flow->ufid_table.hash == hash &&
776 ovs_flow_cmp_ufid(flow, ufid))
4a46b24e
AW
777 return flow;
778 }
779 return NULL;
780}
781
1bd7116f
AZ
782int ovs_flow_tbl_num_masks(const struct flow_table *table)
783{
4bc63b1b 784 struct mask_array *ma = rcu_dereference_ovsl(table->mask_array);
57f7d7b9 785 return READ_ONCE(ma->count);
1bd7116f
AZ
786}
787
74ed7ab9
JS
788static struct table_instance *table_instance_expand(struct table_instance *ti,
789 bool ufid)
b637e498 790{
74ed7ab9 791 return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
e6445719
PS
792}
793
56c19868 794/* Must be called with OVS mutex held. */
e6445719
PS
795void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
796{
b637e498 797 struct table_instance *ti = ovsl_dereference(table->ti);
74ed7ab9 798 struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
b637e498 799
e6445719 800 BUG_ON(table->count == 0);
50b0e61b 801 table_instance_flow_free(table, ti, ufid_ti, flow, true);
e6445719
PS
802}
803
618ed0c8 804static struct sw_flow_mask *mask_alloc(void)
e6445719
PS
805{
806 struct sw_flow_mask *mask;
807
808 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
809 if (mask)
e80857cc 810 mask->ref_count = 1;
e6445719
PS
811
812 return mask;
813}
814
e6445719
PS
815static bool mask_equal(const struct sw_flow_mask *a,
816 const struct sw_flow_mask *b)
817{
7085130b
DDP
818 const u8 *a_ = (const u8 *)&a->key + a->range.start;
819 const u8 *b_ = (const u8 *)&b->key + b->range.start;
e6445719
PS
820
821 return (a->range.end == b->range.end)
822 && (a->range.start == b->range.start)
823 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
824}
825
618ed0c8 826static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
e6445719
PS
827 const struct sw_flow_mask *mask)
828{
4bc63b1b
TZ
829 struct mask_array *ma;
830 int i;
e6445719 831
4bc63b1b
TZ
832 ma = ovsl_dereference(tbl->mask_array);
833 for (i = 0; i < ma->max; i++) {
834 struct sw_flow_mask *t;
835 t = ovsl_dereference(ma->masks[i]);
836
837 if (t && mask_equal(mask, t))
838 return t;
e6445719
PS
839 }
840
841 return NULL;
842}
843
d1211908 844/* Add 'mask' into the mask list, if it is not already there. */
618ed0c8 845static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
12eb18f7 846 const struct sw_flow_mask *new)
618ed0c8
PS
847{
848 struct sw_flow_mask *mask;
4bc63b1b 849
618ed0c8
PS
850 mask = flow_mask_find(tbl, new);
851 if (!mask) {
852 /* Allocate a new mask if none exsits. */
853 mask = mask_alloc();
854 if (!mask)
855 return -ENOMEM;
856 mask->key = new->key;
857 mask->range = new->range;
4bc63b1b
TZ
858
859 /* Add mask to mask-list. */
57f7d7b9
TZ
860 if (tbl_mask_array_add_mask(tbl, mask)) {
861 kfree(mask);
862 return -ENOMEM;
4bc63b1b 863 }
e80857cc
AZ
864 } else {
865 BUG_ON(!mask->ref_count);
866 mask->ref_count++;
618ed0c8
PS
867 }
868
618ed0c8
PS
869 flow->mask = mask;
870 return 0;
871}
872
56c19868 873/* Must be called with OVS mutex held. */
d29ab6f8 874static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
e6445719 875{
618ed0c8
PS
876 struct table_instance *new_ti = NULL;
877 struct table_instance *ti;
618ed0c8 878
74ed7ab9 879 flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
618ed0c8
PS
880 ti = ovsl_dereference(table->ti);
881 table_instance_insert(ti, flow);
882 table->count++;
883
884 /* Expand table, if necessary, to make room. */
885 if (table->count > ti->n_buckets)
74ed7ab9 886 new_ti = table_instance_expand(ti, false);
618ed0c8 887 else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
74ed7ab9 888 new_ti = table_instance_rehash(ti, ti->n_buckets, false);
618ed0c8
PS
889
890 if (new_ti) {
891 rcu_assign_pointer(table->ti, new_ti);
74ed7ab9 892 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
618ed0c8
PS
893 table->last_rehash = jiffies;
894 }
d29ab6f8
JS
895}
896
74ed7ab9
JS
897/* Must be called with OVS mutex held. */
898static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
899{
900 struct table_instance *ti;
901
902 flow->ufid_table.hash = ufid_hash(&flow->id);
903 ti = ovsl_dereference(table->ufid_ti);
904 ufid_table_instance_insert(ti, flow);
905 table->ufid_count++;
906
907 /* Expand table, if necessary, to make room. */
908 if (table->ufid_count > ti->n_buckets) {
909 struct table_instance *new_ti;
910
911 new_ti = table_instance_expand(ti, true);
912 if (new_ti) {
913 rcu_assign_pointer(table->ufid_ti, new_ti);
914 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
915 }
916 }
917}
918
d29ab6f8
JS
919/* Must be called with OVS mutex held. */
920int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
921 const struct sw_flow_mask *mask)
922{
923 int err;
924
925 err = flow_mask_insert(table, flow, mask);
926 if (err)
927 return err;
928 flow_key_insert(table, flow);
74ed7ab9
JS
929 if (ovs_identifier_is_ufid(&flow->id))
930 flow_ufid_insert(table, flow);
d29ab6f8 931
618ed0c8 932 return 0;
e6445719
PS
933}
934
935/* Initializes the flow module.
936 * Returns zero if successful or a negative error code. */
937int ovs_flow_init(void)
938{
939 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
940 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
941
63e7959c 942 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
db74a333 943 + (nr_cpu_ids
aef833c5 944 * sizeof(struct sw_flow_stats *)),
63e7959c 945 0, 0, NULL);
e6445719
PS
946 if (flow_cache == NULL)
947 return -ENOMEM;
948
63e7959c 949 flow_stats_cache
aef833c5 950 = kmem_cache_create("sw_flow_stats", sizeof(struct sw_flow_stats),
63e7959c
JR
951 0, SLAB_HWCACHE_ALIGN, NULL);
952 if (flow_stats_cache == NULL) {
953 kmem_cache_destroy(flow_cache);
954 flow_cache = NULL;
955 return -ENOMEM;
956 }
957
e6445719
PS
958 return 0;
959}
960
961/* Uninitializes the flow module. */
962void ovs_flow_exit(void)
963{
63e7959c 964 kmem_cache_destroy(flow_stats_cache);
e6445719
PS
965 kmem_cache_destroy(flow_cache);
966}