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