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