datapath: Fix comment style.
[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/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(const 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(rcu_dereference_raw(flow->sf_acts));
150         for_each_node(node)
151                 if (flow->stats[node])
152                         kmem_cache_free(flow_stats_cache,
153                                         rcu_dereference_raw(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         size = max(MASK_ARRAY_SIZE_MIN, size);
227         new = kzalloc(sizeof(struct mask_array) +
228                       sizeof(struct sw_flow_mask *) * size, GFP_KERNEL);
229         if (!new)
230                 return NULL;
231
232         new->count = 0;
233         new->max = size;
234
235         return new;
236 }
237
238 static int tbl_mask_array_realloc(struct flow_table *tbl, int size)
239 {
240         struct mask_array *old;
241         struct mask_array *new;
242
243         new = tbl_mask_array_alloc(size);
244         if (!new)
245                 return -ENOMEM;
246
247         old = ovsl_dereference(tbl->mask_array);
248         if (old) {
249                 int i, count = 0;
250
251                 for (i = 0; i < old->max; i++) {
252                         if (ovsl_dereference(old->masks[i]))
253                                 new->masks[count++] = old->masks[i];
254                 }
255
256                 new->count = count;
257         }
258         rcu_assign_pointer(tbl->mask_array, new);
259
260         if (old)
261                 call_rcu(&old->rcu, mask_array_rcu_cb);
262
263         return 0;
264 }
265
266 int ovs_flow_tbl_init(struct flow_table *table)
267 {
268         struct table_instance *ti;
269         struct mask_array *ma;
270
271         table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) *
272                                           MC_HASH_ENTRIES, __alignof__(struct mask_cache_entry));
273         if (!table->mask_cache)
274                 return -ENOMEM;
275
276         ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
277         if (!ma)
278                 goto free_mask_cache;
279
280         ti = table_instance_alloc(TBL_MIN_BUCKETS);
281         if (!ti)
282                 goto free_mask_array;
283
284         rcu_assign_pointer(table->ti, ti);
285         rcu_assign_pointer(table->mask_array, ma);
286         table->last_rehash = jiffies;
287         table->count = 0;
288         return 0;
289
290 free_mask_array:
291         kfree(ma);
292 free_mask_cache:
293         free_percpu(table->mask_cache);
294         return -ENOMEM;
295 }
296
297 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
298 {
299         struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
300
301         __table_instance_destroy(ti);
302 }
303
304 static void table_instance_destroy(struct table_instance *ti, bool deferred)
305 {
306         int i;
307
308         if (!ti)
309                 return;
310
311         if (ti->keep_flows)
312                 goto skip_flows;
313
314         for (i = 0; i < ti->n_buckets; i++) {
315                 struct sw_flow *flow;
316                 struct hlist_head *head = flex_array_get(ti->buckets, i);
317                 struct hlist_node *n;
318                 int ver = ti->node_ver;
319
320                 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
321                         hlist_del_rcu(&flow->hash_node[ver]);
322                         ovs_flow_free(flow, deferred);
323                 }
324         }
325
326 skip_flows:
327         if (deferred)
328                 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
329         else
330                 __table_instance_destroy(ti);
331 }
332
333 /* No need for locking this function is called from RCU callback or
334  * error path.
335  */
336 void ovs_flow_tbl_destroy(struct flow_table *table)
337 {
338         struct table_instance *ti = rcu_dereference_raw(table->ti);
339
340         free_percpu(table->mask_cache);
341         kfree(rcu_dereference_raw(table->mask_array));
342         table_instance_destroy(ti, false);
343 }
344
345 struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
346                                        u32 *bucket, u32 *last)
347 {
348         struct sw_flow *flow;
349         struct hlist_head *head;
350         int ver;
351         int i;
352
353         ver = ti->node_ver;
354         while (*bucket < ti->n_buckets) {
355                 i = 0;
356                 head = flex_array_get(ti->buckets, *bucket);
357                 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
358                         if (i < *last) {
359                                 i++;
360                                 continue;
361                         }
362                         *last = i + 1;
363                         return flow;
364                 }
365                 (*bucket)++;
366                 *last = 0;
367         }
368
369         return NULL;
370 }
371
372 static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
373 {
374         hash = jhash_1word(hash, ti->hash_seed);
375         return flex_array_get(ti->buckets,
376                                 (hash & (ti->n_buckets - 1)));
377 }
378
379 static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow)
380 {
381         struct hlist_head *head;
382
383         head = find_bucket(ti, flow->hash);
384         hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head);
385 }
386
387 static void flow_table_copy_flows(struct table_instance *old,
388                                   struct table_instance *new)
389 {
390         int old_ver;
391         int i;
392
393         old_ver = old->node_ver;
394         new->node_ver = !old_ver;
395
396         /* Insert in new table. */
397         for (i = 0; i < old->n_buckets; i++) {
398                 struct sw_flow *flow;
399                 struct hlist_head *head;
400
401                 head = flex_array_get(old->buckets, i);
402
403                 hlist_for_each_entry(flow, head, hash_node[old_ver])
404                         table_instance_insert(new, flow);
405         }
406
407         old->keep_flows = true;
408 }
409
410 static struct table_instance *table_instance_rehash(struct table_instance *ti,
411                                             int n_buckets)
412 {
413         struct table_instance *new_ti;
414
415         new_ti = table_instance_alloc(n_buckets);
416         if (!new_ti)
417                 return NULL;
418
419         flow_table_copy_flows(ti, new_ti);
420
421         return new_ti;
422 }
423
424 int ovs_flow_tbl_flush(struct flow_table *flow_table)
425 {
426         struct table_instance *old_ti;
427         struct table_instance *new_ti;
428
429         old_ti = ovsl_dereference(flow_table->ti);
430         new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
431         if (!new_ti)
432                 return -ENOMEM;
433
434         rcu_assign_pointer(flow_table->ti, new_ti);
435         flow_table->last_rehash = jiffies;
436         flow_table->count = 0;
437
438         table_instance_destroy(old_ti, true);
439         return 0;
440 }
441
442 static u32 flow_hash(const struct sw_flow_key *key, int key_start,
443                      int key_end)
444 {
445         const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
446         int hash_u32s = (key_end - key_start) >> 2;
447
448         /* Make sure number of hash bytes are multiple of u32. */
449         BUILD_BUG_ON(sizeof(long) % sizeof(u32));
450
451         return arch_fast_hash2(hash_key, hash_u32s, 0);
452 }
453
454 static int flow_key_start(const struct sw_flow_key *key)
455 {
456         if (key->tun_key.ipv4_dst)
457                 return 0;
458         else
459                 return rounddown(offsetof(struct sw_flow_key, phy),
460                                           sizeof(long));
461 }
462
463 static bool cmp_key(const struct sw_flow_key *key1,
464                     const struct sw_flow_key *key2,
465                     int key_start, int key_end)
466 {
467         const long *cp1 = (const long *)((const u8 *)key1 + key_start);
468         const long *cp2 = (const long *)((const u8 *)key2 + key_start);
469         long diffs = 0;
470         int i;
471
472         for (i = key_start; i < key_end;  i += sizeof(long))
473                 diffs |= *cp1++ ^ *cp2++;
474
475         return diffs == 0;
476 }
477
478 static bool flow_cmp_masked_key(const struct sw_flow *flow,
479                                 const struct sw_flow_key *key,
480                                 int key_start, int key_end)
481 {
482         return cmp_key(&flow->key, key, key_start, key_end);
483 }
484
485 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
486                                const struct sw_flow_match *match)
487 {
488         struct sw_flow_key *key = match->key;
489         int key_start = flow_key_start(key);
490         int key_end = match->range.end;
491
492         return cmp_key(&flow->unmasked_key, key, key_start, key_end);
493 }
494
495 static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
496                                           const struct sw_flow_key *unmasked,
497                                           const struct sw_flow_mask *mask,
498                                           u32 *n_mask_hit)
499 {
500         struct sw_flow *flow;
501         struct hlist_head *head;
502         int key_start = mask->range.start;
503         int key_end = mask->range.end;
504         u32 hash;
505         struct sw_flow_key masked_key;
506
507         ovs_flow_mask_key(&masked_key, unmasked, mask);
508         hash = flow_hash(&masked_key, key_start, key_end);
509         head = find_bucket(ti, hash);
510         (*n_mask_hit)++;
511         hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) {
512                 if (flow->mask == mask && flow->hash == hash &&
513                     flow_cmp_masked_key(flow, &masked_key,
514                                           key_start, key_end))
515                         return flow;
516         }
517         return NULL;
518 }
519
520 /* Flow lookup does full lookup on flow table. It starts with
521  * mask from index passed in *index.
522  */
523 static struct sw_flow *flow_lookup(struct flow_table *tbl,
524                                    struct table_instance *ti,
525                                    const struct mask_array *ma,
526                                    const struct sw_flow_key *key,
527                                    u32 *n_mask_hit,
528                                    u32 *index)
529 {
530         struct sw_flow_mask *mask;
531         struct sw_flow *flow;
532         int i;
533
534         if (*index < ma->max) {
535                 mask = rcu_dereference_ovsl(ma->masks[*index]);
536                 if (mask) {
537                         flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
538                         if (flow)
539                                 return flow;
540                 }
541         }
542
543         for (i = 0; i < ma->max; i++)  {
544
545                 if (i == *index)
546                         continue;
547
548                 mask = rcu_dereference_ovsl(ma->masks[i]);
549                 if (!mask)
550                         continue;
551
552                 flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
553                 if (flow) { /* Found */
554                         *index = i;
555                         return flow;
556                 }
557         }
558
559         return NULL;
560 }
561
562 /*
563  * mask_cache maps flow to probable mask. This cache is not tightly
564  * coupled cache, It means updates to  mask list can result in inconsistent
565  * cache entry in mask cache.
566  * This is per cpu cache and is divided in MC_HASH_SEGS segments.
567  * In case of a hash collision the entry is hashed in next segment.
568  */
569 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
570                                           const struct sw_flow_key *key,
571                                           u32 skb_hash,
572                                           u32 *n_mask_hit)
573 {
574         struct mask_array *ma = rcu_dereference(tbl->mask_array);
575         struct table_instance *ti = rcu_dereference(tbl->ti);
576         struct mask_cache_entry *entries, *ce;
577         struct sw_flow *flow;
578         u32 hash = skb_hash;
579         int seg;
580
581         *n_mask_hit = 0;
582         if (unlikely(!skb_hash)) {
583                 u32 mask_index = 0;
584
585                 return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index);
586         }
587
588         ce = NULL;
589         entries = this_cpu_ptr(tbl->mask_cache);
590
591         /* Find the cache entry 'ce' to operate on. */
592         for (seg = 0; seg < MC_HASH_SEGS; seg++) {
593                 int index = hash & (MC_HASH_ENTRIES - 1);
594                 struct mask_cache_entry *e;
595
596                 e = &entries[index];
597                 if (e->skb_hash == skb_hash) {
598                         flow = flow_lookup(tbl, ti, ma, key, n_mask_hit,
599                                            &e->mask_index);
600                         if (!flow)
601                                 e->skb_hash = 0;
602                         return flow;
603                 }
604
605                 if (!ce || e->skb_hash < ce->skb_hash)
606                         ce = e;  /* A better replacement cache candidate. */
607
608                 hash >>= MC_HASH_SHIFT;
609         }
610
611         /* Cache miss, do full lookup. */
612         flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &ce->mask_index);
613         if (flow)
614                 ce->skb_hash = skb_hash;
615
616         return flow;
617 }
618
619 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
620                                     const struct sw_flow_key *key)
621 {
622         struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
623         struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
624         u32 __always_unused n_mask_hit;
625         u32 index = 0;
626
627         return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index);
628 }
629
630 struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
631                                           const struct sw_flow_match *match)
632 {
633         struct mask_array *ma = ovsl_dereference(tbl->mask_array);
634         int i;
635
636         /* Always called under ovs-mutex. */
637         for (i = 0; i < ma->max; i++) {
638                 struct table_instance *ti = ovsl_dereference(tbl->ti);
639                 u32 __always_unused n_mask_hit;
640                 struct sw_flow_mask *mask;
641                 struct sw_flow *flow;
642
643                 mask = ovsl_dereference(ma->masks[i]);
644                 if (!mask)
645                         continue;
646                 flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit);
647                 if (flow && ovs_flow_cmp_unmasked_key(flow, match))
648                         return flow;
649         }
650         return NULL;
651 }
652
653 int ovs_flow_tbl_num_masks(const struct flow_table *table)
654 {
655         struct mask_array *ma;
656
657         ma = rcu_dereference_ovsl(table->mask_array);
658         return ma->count;
659 }
660
661 static struct table_instance *table_instance_expand(struct table_instance *ti)
662 {
663         return table_instance_rehash(ti, ti->n_buckets * 2);
664 }
665
666 static void tbl_mask_array_delete_mask(struct mask_array *ma,
667                                        struct sw_flow_mask *mask)
668 {
669         int i;
670
671         /* Remove the deleted mask pointers from the array */
672         for (i = 0; i < ma->max; i++) {
673                 if (mask == ovsl_dereference(ma->masks[i])) {
674                         RCU_INIT_POINTER(ma->masks[i], NULL);
675                         ma->count--;
676                         call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
677                         return;
678                 }
679         }
680         BUG();
681 }
682
683 /* Remove 'mask' from the mask list, if it is not needed any more. */
684 static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
685 {
686         if (mask) {
687                 /* ovs-lock is required to protect mask-refcount and
688                  * mask list.
689                  */
690                 ASSERT_OVSL();
691                 BUG_ON(!mask->ref_count);
692                 mask->ref_count--;
693
694                 if (!mask->ref_count) {
695                         struct mask_array *ma;
696
697                         ma = ovsl_dereference(tbl->mask_array);
698                         tbl_mask_array_delete_mask(ma, mask);
699
700                         /* Shrink the mask array if necessary. */
701                         if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) &&
702                             ma->count <= (ma->max / 3))
703                                 tbl_mask_array_realloc(tbl, ma->max / 2);
704
705                 }
706         }
707 }
708
709 /* Must be called with OVS mutex held. */
710 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
711 {
712         struct table_instance *ti = ovsl_dereference(table->ti);
713
714         BUG_ON(table->count == 0);
715         hlist_del_rcu(&flow->hash_node[ti->node_ver]);
716         table->count--;
717
718         /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
719          * accessible as long as the RCU read lock is held.
720          */
721         flow_mask_remove(table, flow->mask);
722 }
723
724 static struct sw_flow_mask *mask_alloc(void)
725 {
726         struct sw_flow_mask *mask;
727
728         mask = kmalloc(sizeof(*mask), GFP_KERNEL);
729         if (mask)
730                 mask->ref_count = 1;
731
732         return mask;
733 }
734
735 static bool mask_equal(const struct sw_flow_mask *a,
736                        const struct sw_flow_mask *b)
737 {
738         const u8 *a_ = (const u8 *)&a->key + a->range.start;
739         const u8 *b_ = (const u8 *)&b->key + b->range.start;
740
741         return  (a->range.end == b->range.end)
742                 && (a->range.start == b->range.start)
743                 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
744 }
745
746 static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
747                                            const struct sw_flow_mask *mask)
748 {
749         struct mask_array *ma;
750         int i;
751
752         ma = ovsl_dereference(tbl->mask_array);
753         for (i = 0; i < ma->max; i++) {
754                 struct sw_flow_mask *t;
755
756                 t = ovsl_dereference(ma->masks[i]);
757                 if (t && mask_equal(mask, t))
758                         return t;
759         }
760
761         return NULL;
762 }
763
764 /* Add 'mask' into the mask list, if it is not already there. */
765 static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
766                             const struct sw_flow_mask *new)
767 {
768         struct sw_flow_mask *mask;
769
770         mask = flow_mask_find(tbl, new);
771         if (!mask) {
772                 struct mask_array *ma;
773                 int i;
774
775                 /* Allocate a new mask if none exsits. */
776                 mask = mask_alloc();
777                 if (!mask)
778                         return -ENOMEM;
779
780                 mask->key = new->key;
781                 mask->range = new->range;
782
783                 /* Add mask to mask-list. */
784                 ma = ovsl_dereference(tbl->mask_array);
785                 if (ma->count >= ma->max) {
786                         int err;
787
788                         err = tbl_mask_array_realloc(tbl, ma->max +
789                                                           MASK_ARRAY_SIZE_MIN);
790                         if (err) {
791                                 kfree(mask);
792                                 return err;
793                         }
794                         ma = ovsl_dereference(tbl->mask_array);
795                 }
796
797                 for (i = 0; i < ma->max; i++) {
798                         struct sw_flow_mask *t;
799
800                         t = ovsl_dereference(ma->masks[i]);
801                         if (!t) {
802                                 rcu_assign_pointer(ma->masks[i], mask);
803                                 ma->count++;
804                                 break;
805                         }
806                 }
807
808         } else {
809                 BUG_ON(!mask->ref_count);
810                 mask->ref_count++;
811         }
812
813         flow->mask = mask;
814         return 0;
815 }
816
817 /* Must be called with OVS mutex held. */
818 int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
819                         const struct sw_flow_mask *mask)
820 {
821         struct table_instance *new_ti = NULL;
822         struct table_instance *ti;
823         int err;
824
825         err = flow_mask_insert(table, flow, mask);
826         if (err)
827                 return err;
828
829         flow->hash = flow_hash(&flow->key, flow->mask->range.start,
830                         flow->mask->range.end);
831         ti = ovsl_dereference(table->ti);
832         table_instance_insert(ti, flow);
833         table->count++;
834
835         /* Expand table, if necessary, to make room. */
836         if (table->count > ti->n_buckets)
837                 new_ti = table_instance_expand(ti);
838         else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
839                 new_ti = table_instance_rehash(ti, ti->n_buckets);
840
841         if (new_ti) {
842                 rcu_assign_pointer(table->ti, new_ti);
843                 table_instance_destroy(ti, true);
844                 table->last_rehash = jiffies;
845         }
846         return 0;
847 }
848
849 /* Initializes the flow module.
850  * Returns zero if successful or a negative error code.
851  */
852 int ovs_flow_init(void)
853 {
854         BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
855         BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
856
857         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
858                                        + (num_possible_nodes()
859                                           * sizeof(struct flow_stats *)),
860                                        0, 0, NULL);
861         if (flow_cache == NULL)
862                 return -ENOMEM;
863
864         flow_stats_cache
865                 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
866                                     0, SLAB_HWCACHE_ALIGN, NULL);
867         if (flow_stats_cache == NULL) {
868                 kmem_cache_destroy(flow_cache);
869                 flow_cache = NULL;
870                 return -ENOMEM;
871         }
872
873         return 0;
874 }
875
876 /* Uninitializes the flow module. */
877 void ovs_flow_exit(void)
878 {
879         kmem_cache_destroy(flow_stats_cache);
880         kmem_cache_destroy(flow_cache);
881 }