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