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