datapath: Convert mask list in mask array.
[sliver-openvswitch.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/hash.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 "vlan.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                        const struct sw_flow_mask *mask)
67 {
68         const long *m = (const long *)((const u8 *)&mask->key +
69                                 mask->range.start);
70         const long *s = (const long *)((const u8 *)src +
71                                 mask->range.start);
72         long *d = (long *)((u8 *)dst + mask->range.start);
73         int i;
74
75         /* The memory outside of the 'mask->range' are not set since
76          * further operations on 'dst' only uses contents within
77          * 'mask->range'.
78          */
79         for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
80                 *d++ = *s++ & *m++;
81 }
82
83 struct sw_flow *ovs_flow_alloc(void)
84 {
85         struct sw_flow *flow;
86         struct flow_stats *stats;
87         int node;
88
89         flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
90         if (!flow)
91                 return ERR_PTR(-ENOMEM);
92
93         flow->sf_acts = NULL;
94         flow->mask = NULL;
95         flow->stats_last_writer = NUMA_NO_NODE;
96
97         /* Initialize the default stat node. */
98         stats = kmem_cache_alloc_node(flow_stats_cache,
99                                       GFP_KERNEL | __GFP_ZERO, 0);
100         if (!stats)
101                 goto err;
102
103         spin_lock_init(&stats->lock);
104
105         RCU_INIT_POINTER(flow->stats[0], stats);
106
107         for_each_node(node)
108                 if (node != 0)
109                         RCU_INIT_POINTER(flow->stats[node], NULL);
110
111         return flow;
112 err:
113         kmem_cache_free(flow_cache, flow);
114         return ERR_PTR(-ENOMEM);
115 }
116
117 int ovs_flow_tbl_count(struct flow_table *table)
118 {
119         return table->count;
120 }
121
122 static struct flex_array *alloc_buckets(unsigned int n_buckets)
123 {
124         struct flex_array *buckets;
125         int i, err;
126
127         buckets = flex_array_alloc(sizeof(struct hlist_head),
128                                    n_buckets, GFP_KERNEL);
129         if (!buckets)
130                 return NULL;
131
132         err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
133         if (err) {
134                 flex_array_free(buckets);
135                 return NULL;
136         }
137
138         for (i = 0; i < n_buckets; i++)
139                 INIT_HLIST_HEAD((struct hlist_head *)
140                                         flex_array_get(buckets, i));
141
142         return buckets;
143 }
144
145 static void flow_free(struct sw_flow *flow)
146 {
147         int node;
148
149         kfree((struct sw_flow_actions __force *)flow->sf_acts);
150         for_each_node(node)
151                 if (flow->stats[node])
152                         kmem_cache_free(flow_stats_cache,
153                                         (struct flow_stats __force *)flow->stats[node]);
154         kmem_cache_free(flow_cache, flow);
155 }
156
157 static void rcu_free_flow_callback(struct rcu_head *rcu)
158 {
159         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
160
161         flow_free(flow);
162 }
163
164 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
165 {
166         struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
167
168         kfree(mask);
169 }
170
171 void ovs_flow_free(struct sw_flow *flow, bool deferred)
172 {
173         if (!flow)
174                 return;
175
176         if (deferred)
177                 call_rcu(&flow->rcu, rcu_free_flow_callback);
178         else
179                 flow_free(flow);
180 }
181
182 static void free_buckets(struct flex_array *buckets)
183 {
184         flex_array_free(buckets);
185 }
186
187
188 static void __table_instance_destroy(struct table_instance *ti)
189 {
190         free_buckets(ti->buckets);
191         kfree(ti);
192 }
193
194 static struct table_instance *table_instance_alloc(int new_size)
195 {
196         struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
197
198         if (!ti)
199                 return NULL;
200
201         ti->buckets = alloc_buckets(new_size);
202
203         if (!ti->buckets) {
204                 kfree(ti);
205                 return NULL;
206         }
207         ti->n_buckets = new_size;
208         ti->node_ver = 0;
209         ti->keep_flows = false;
210         get_random_bytes(&ti->hash_seed, sizeof(u32));
211
212         return ti;
213 }
214
215 static void mask_array_rcu_cb(struct rcu_head *rcu)
216 {
217         struct mask_array *ma = container_of(rcu, struct mask_array, rcu);
218
219         kfree(ma);
220 }
221
222 static struct mask_array *tbl_mask_array_alloc(int size)
223 {
224         struct mask_array *new;
225
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;
249
250                 for (i = 0; i < old->max; i++) {
251                         if (old->masks[i])
252                                 new->masks[new->count++] = old->masks[i];
253                 }
254         }
255         rcu_assign_pointer(tbl->mask_array, new);
256
257         if (old)
258                 call_rcu(&old->rcu, mask_array_rcu_cb);
259
260         return 0;
261 }
262
263 int ovs_flow_tbl_init(struct flow_table *table)
264 {
265         struct table_instance *ti;
266         struct mask_array *ma;
267
268         table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) *
269                                           MC_HASH_ENTRIES, __alignof__(struct mask_cache_entry));
270         if (!table->mask_cache)
271                 return -ENOMEM;
272
273         ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
274         if (!ma)
275                 goto free_mask_cache;
276
277         ti = table_instance_alloc(TBL_MIN_BUCKETS);
278         if (!ti)
279                 goto free_mask_array;
280
281         rcu_assign_pointer(table->ti, ti);
282         rcu_assign_pointer(table->mask_array, ma);
283         table->last_rehash = jiffies;
284         table->count = 0;
285         return 0;
286
287 free_mask_array:
288         kfree((struct mask_array __force *)table->mask_array);
289 free_mask_cache:
290         free_percpu(table->mask_cache);
291         return -ENOMEM;
292 }
293
294 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
295 {
296         struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
297
298         __table_instance_destroy(ti);
299 }
300
301 static void table_instance_destroy(struct table_instance *ti, bool deferred)
302 {
303         int i;
304
305         if (!ti)
306                 return;
307
308         if (ti->keep_flows)
309                 goto skip_flows;
310
311         for (i = 0; i < ti->n_buckets; i++) {
312                 struct sw_flow *flow;
313                 struct hlist_head *head = flex_array_get(ti->buckets, i);
314                 struct hlist_node *n;
315                 int ver = ti->node_ver;
316
317                 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
318                         hlist_del_rcu(&flow->hash_node[ver]);
319                         ovs_flow_free(flow, deferred);
320                 }
321         }
322
323 skip_flows:
324         if (deferred)
325                 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
326         else
327                 __table_instance_destroy(ti);
328 }
329
330 /* No need for locking this function is called from RCU callback or
331  * error path. */
332 void ovs_flow_tbl_destroy(struct flow_table *table)
333 {
334         struct table_instance *ti = (struct table_instance __force *)table->ti;
335
336         free_percpu(table->mask_cache);
337         kfree((struct mask_array __force *)table->mask_array);
338         table_instance_destroy(ti, false);
339 }
340
341 struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
342                                        u32 *bucket, u32 *last)
343 {
344         struct sw_flow *flow;
345         struct hlist_head *head;
346         int ver;
347         int i;
348
349         ver = ti->node_ver;
350         while (*bucket < ti->n_buckets) {
351                 i = 0;
352                 head = flex_array_get(ti->buckets, *bucket);
353                 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
354                         if (i < *last) {
355                                 i++;
356                                 continue;
357                         }
358                         *last = i + 1;
359                         return flow;
360                 }
361                 (*bucket)++;
362                 *last = 0;
363         }
364
365         return NULL;
366 }
367
368 static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
369 {
370         hash = jhash_1word(hash, ti->hash_seed);
371         return flex_array_get(ti->buckets,
372                                 (hash & (ti->n_buckets - 1)));
373 }
374
375 static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow)
376 {
377         struct hlist_head *head;
378
379         head = find_bucket(ti, flow->hash);
380         hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head);
381 }
382
383 static void flow_table_copy_flows(struct table_instance *old,
384                                   struct table_instance *new)
385 {
386         int old_ver;
387         int i;
388
389         old_ver = old->node_ver;
390         new->node_ver = !old_ver;
391
392         /* Insert in new table. */
393         for (i = 0; i < old->n_buckets; i++) {
394                 struct sw_flow *flow;
395                 struct hlist_head *head;
396
397                 head = flex_array_get(old->buckets, i);
398
399                 hlist_for_each_entry(flow, head, hash_node[old_ver])
400                         table_instance_insert(new, flow);
401         }
402
403         old->keep_flows = true;
404 }
405
406 static struct table_instance *table_instance_rehash(struct table_instance *ti,
407                                             int n_buckets)
408 {
409         struct table_instance *new_ti;
410
411         new_ti = table_instance_alloc(n_buckets);
412         if (!new_ti)
413                 return NULL;
414
415         flow_table_copy_flows(ti, new_ti);
416
417         return new_ti;
418 }
419
420 int ovs_flow_tbl_flush(struct flow_table *flow_table)
421 {
422         struct table_instance *old_ti;
423         struct table_instance *new_ti;
424
425         old_ti = ovsl_dereference(flow_table->ti);
426         new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
427         if (!new_ti)
428                 return -ENOMEM;
429
430         rcu_assign_pointer(flow_table->ti, new_ti);
431         flow_table->last_rehash = jiffies;
432         flow_table->count = 0;
433
434         table_instance_destroy(old_ti, true);
435         return 0;
436 }
437
438 static u32 flow_hash(const struct sw_flow_key *key, int key_start,
439                      int key_end)
440 {
441         const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
442         int hash_u32s = (key_end - key_start) >> 2;
443
444         /* Make sure number of hash bytes are multiple of u32. */
445         BUILD_BUG_ON(sizeof(long) % sizeof(u32));
446
447         return arch_fast_hash2(hash_key, hash_u32s, 0);
448 }
449
450 static int flow_key_start(const struct sw_flow_key *key)
451 {
452         if (key->tun_key.ipv4_dst)
453                 return 0;
454         else
455                 return rounddown(offsetof(struct sw_flow_key, phy),
456                                           sizeof(long));
457 }
458
459 static bool cmp_key(const struct sw_flow_key *key1,
460                     const struct sw_flow_key *key2,
461                     int key_start, int key_end)
462 {
463         const long *cp1 = (const long *)((const u8 *)key1 + key_start);
464         const long *cp2 = (const long *)((const u8 *)key2 + key_start);
465         long diffs = 0;
466         int i;
467
468         for (i = key_start; i < key_end;  i += sizeof(long))
469                 diffs |= *cp1++ ^ *cp2++;
470
471         return diffs == 0;
472 }
473
474 static bool flow_cmp_masked_key(const struct sw_flow *flow,
475                                 const struct sw_flow_key *key,
476                                 int key_start, int key_end)
477 {
478         return cmp_key(&flow->key, key, key_start, key_end);
479 }
480
481 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
482                                struct sw_flow_match *match)
483 {
484         struct sw_flow_key *key = match->key;
485         int key_start = flow_key_start(key);
486         int key_end = match->range.end;
487
488         return cmp_key(&flow->unmasked_key, key, key_start, key_end);
489 }
490
491 static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
492                                           const struct sw_flow_key *unmasked,
493                                           struct sw_flow_mask *mask,
494                                           u32 *n_mask_hit)
495 {
496         struct sw_flow *flow;
497         struct hlist_head *head;
498         int key_start = mask->range.start;
499         int key_end = mask->range.end;
500         u32 hash;
501         struct sw_flow_key masked_key;
502
503         ovs_flow_mask_key(&masked_key, unmasked, mask);
504         hash = flow_hash(&masked_key, key_start, key_end);
505         head = find_bucket(ti, hash);
506         (*n_mask_hit)++;
507         hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) {
508                 if (flow->mask == mask && flow->hash == hash &&
509                     flow_cmp_masked_key(flow, &masked_key,
510                                           key_start, key_end))
511                         return flow;
512         }
513         return NULL;
514 }
515
516
517 static struct sw_flow *flow_lookup(struct flow_table *tbl,
518                                    struct table_instance *ti,
519                                    struct mask_array *ma,
520                                    const struct sw_flow_key *key,
521                                    u32 *n_mask_hit,
522                                    u32 *index)
523 {
524         struct sw_flow *flow;
525         int i;
526
527         for (i = 0; i < ma->max; i++) {
528                 struct sw_flow_mask *mask;
529
530                 mask = rcu_dereference_ovsl(ma->masks[i]);
531                 if (mask) {
532                         flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
533                         if (flow) { /* Found */
534                                 *index = i;
535                                 return flow;
536                         }
537                 }
538         }
539
540         return NULL;
541 }
542
543 /*
544  * mask_cache maps flow to probable mask. This cache is not tightly
545  * coupled cache, It means updates to  mask list can result in inconsistent
546  * cache entry in mask cache.
547  * This is per cpu cache and is divided in MC_HASH_SEGS segments.
548  * In case of a hash collision the entry is hashed in next segment.
549  * */
550 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
551                                           const struct sw_flow_key *key,
552                                           u32 skb_hash,
553                                           u32 *n_mask_hit)
554 {
555         struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
556         struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
557         struct mask_cache_entry  *entries, *ce, *del;
558         struct sw_flow *flow;
559         u32 hash = skb_hash;
560         int seg;
561
562         *n_mask_hit = 0;
563         if (unlikely(!skb_hash)) {
564                 u32 __always_unused mask_index;
565
566                 return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index);
567         }
568
569         del = NULL;
570         entries = this_cpu_ptr(tbl->mask_cache);
571
572         for (seg = 0; seg < MC_HASH_SEGS; seg++) {
573                 int index;
574
575                 index = hash & (MC_HASH_ENTRIES - 1);
576                 ce = &entries[index];
577
578                 if (ce->skb_hash == skb_hash) {
579                         struct sw_flow_mask *mask;
580                         struct sw_flow *flow;
581
582                         mask = rcu_dereference_ovsl(ma->masks[ce->mask_index]);
583                         if (mask) {
584                                 flow = masked_flow_lookup(ti, key, mask,
585                                                           n_mask_hit);
586                                 if (flow)  /* Found */
587                                         return flow;
588
589                         }
590                         del = ce;
591                         break;
592                 }
593
594                 if (!del || (del->skb_hash && !ce->skb_hash) ||
595                     (rcu_dereference_ovsl(ma->masks[del->mask_index]) &&
596                     !rcu_dereference_ovsl(ma->masks[ce->mask_index]))) {
597                         del = ce;
598                 }
599
600                 hash >>= MC_HASH_SHIFT;
601         }
602
603         flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &del->mask_index);
604         if (flow)
605                 del->skb_hash = skb_hash;
606
607         return flow;
608 }
609
610 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
611                                     const struct sw_flow_key *key)
612 {
613         struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
614         struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
615         u32 __always_unused n_mask_hit;
616         u32 __always_unused index;
617
618         n_mask_hit = 0;
619         return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index);
620 }
621
622 int ovs_flow_tbl_num_masks(const struct flow_table *table)
623 {
624         struct mask_array *ma;
625
626         ma = rcu_dereference_ovsl(table->mask_array);
627         return ma->count;
628 }
629
630 static struct table_instance *table_instance_expand(struct table_instance *ti)
631 {
632         return table_instance_rehash(ti, ti->n_buckets * 2);
633 }
634
635 /* Remove 'mask' from the mask list, if it is not needed any more. */
636 static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
637 {
638         if (mask) {
639                 /* ovs-lock is required to protect mask-refcount and
640                  * mask list.
641                  */
642                 ASSERT_OVSL();
643                 BUG_ON(!mask->ref_count);
644                 mask->ref_count--;
645
646                 if (!mask->ref_count) {
647                         struct mask_array *ma;
648                         int i;
649
650                         ma = ovsl_dereference(tbl->mask_array);
651                         for (i = 0; i < ma->max; i++) {
652                                 if (mask == ovsl_dereference(ma->masks[i])) {
653                                         RCU_INIT_POINTER(ma->masks[i], NULL);
654                                         ma->count--;
655                                         goto free;
656                                 }
657                         }
658                         BUG();
659 free:
660                         call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
661                 }
662         }
663 }
664
665 /* Must be called with OVS mutex held. */
666 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
667 {
668         struct table_instance *ti = ovsl_dereference(table->ti);
669
670         BUG_ON(table->count == 0);
671         hlist_del_rcu(&flow->hash_node[ti->node_ver]);
672         table->count--;
673
674         /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
675          * accessible as long as the RCU read lock is held. */
676         flow_mask_remove(table, flow->mask);
677 }
678
679 static struct sw_flow_mask *mask_alloc(void)
680 {
681         struct sw_flow_mask *mask;
682
683         mask = kmalloc(sizeof(*mask), GFP_KERNEL);
684         if (mask)
685                 mask->ref_count = 1;
686
687         return mask;
688 }
689
690 static bool mask_equal(const struct sw_flow_mask *a,
691                        const struct sw_flow_mask *b)
692 {
693         const u8 *a_ = (const u8 *)&a->key + a->range.start;
694         const u8 *b_ = (const u8 *)&b->key + b->range.start;
695
696         return  (a->range.end == b->range.end)
697                 && (a->range.start == b->range.start)
698                 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
699 }
700
701 static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
702                                            const struct sw_flow_mask *mask)
703 {
704         struct mask_array *ma;
705         int i;
706
707         ma = ovsl_dereference(tbl->mask_array);
708         for (i = 0; i < ma->max; i++) {
709                 struct sw_flow_mask *t;
710
711                 t = ovsl_dereference(ma->masks[i]);
712                 if (t && mask_equal(mask, t))
713                         return t;
714         }
715
716         return NULL;
717 }
718
719 /* Add 'mask' into the mask list, if it is not already there. */
720 static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
721                             struct sw_flow_mask *new)
722 {
723         struct sw_flow_mask *mask;
724
725         mask = flow_mask_find(tbl, new);
726         if (!mask) {
727                 struct mask_array *ma;
728                 int i;
729
730                 /* Allocate a new mask if none exsits. */
731                 mask = mask_alloc();
732                 if (!mask)
733                         return -ENOMEM;
734
735                 mask->key = new->key;
736                 mask->range = new->range;
737
738                 /* Add mask to mask-list. */
739                 ma = ovsl_dereference(tbl->mask_array);
740                 if (ma->count >= ma->max) {
741                         int err;
742
743                         err = tbl_mask_array_realloc(tbl, ma->max +
744                                                         MASK_ARRAY_SIZE_MIN);
745                         if (err) {
746                                 kfree(mask);
747                                 return err;
748                         }
749                         ma = ovsl_dereference(tbl->mask_array);
750                 }
751                 for (i = 0; i < ma->max; i++) {
752                         const struct sw_flow_mask *t;
753
754                         t = ovsl_dereference(ma->masks[i]);
755                         if (!t) {
756                                 rcu_assign_pointer(ma->masks[i], mask);
757                                 ma->count++;
758                                 break;
759                         }
760                 }
761         } else {
762                 BUG_ON(!mask->ref_count);
763                 mask->ref_count++;
764         }
765
766         flow->mask = mask;
767         return 0;
768 }
769
770 /* Must be called with OVS mutex held. */
771 int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
772                         struct sw_flow_mask *mask)
773 {
774         struct table_instance *new_ti = NULL;
775         struct table_instance *ti;
776         int err;
777
778         err = flow_mask_insert(table, flow, mask);
779         if (err)
780                 return err;
781
782         flow->hash = flow_hash(&flow->key, flow->mask->range.start,
783                         flow->mask->range.end);
784         ti = ovsl_dereference(table->ti);
785         table_instance_insert(ti, flow);
786         table->count++;
787
788         /* Expand table, if necessary, to make room. */
789         if (table->count > ti->n_buckets)
790                 new_ti = table_instance_expand(ti);
791         else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
792                 new_ti = table_instance_rehash(ti, ti->n_buckets);
793
794         if (new_ti) {
795                 rcu_assign_pointer(table->ti, new_ti);
796                 table_instance_destroy(ti, true);
797                 table->last_rehash = jiffies;
798         }
799         return 0;
800 }
801
802 /* Initializes the flow module.
803  * Returns zero if successful or a negative error code. */
804 int ovs_flow_init(void)
805 {
806         BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
807         BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
808
809         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
810                                        + (num_possible_nodes()
811                                           * sizeof(struct flow_stats *)),
812                                        0, 0, NULL);
813         if (flow_cache == NULL)
814                 return -ENOMEM;
815
816         flow_stats_cache
817                 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
818                                     0, SLAB_HWCACHE_ALIGN, NULL);
819         if (flow_stats_cache == NULL) {
820                 kmem_cache_destroy(flow_cache);
821                 flow_cache = NULL;
822                 return -ENOMEM;
823         }
824
825         return 0;
826 }
827
828 /* Uninitializes the flow module. */
829 void ovs_flow_exit(void)
830 {
831         kmem_cache_destroy(flow_stats_cache);
832         kmem_cache_destroy(flow_cache);
833 }