blob: 4877d5a212a2b3c3a6963ccc1e249e49e93dbd50 [file] [log] [blame]
Pravin B Shelare6445712013-10-03 18:16:47 -07001/*
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>
Francesco Fusco500f8082013-12-12 16:09:06 +010028#include <linux/hash.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070029#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
Pravin B Shelarb637e492013-10-04 00:14:23 -070047#define TBL_MIN_BUCKETS 1024
48#define REHASH_INTERVAL (10 * 60 * HZ)
49
Pravin B Shelare6445712013-10-03 18:16:47 -070050static struct kmem_cache *flow_cache;
51
52static u16 range_n_bytes(const struct sw_flow_key_range *range)
53{
54 return range->end - range->start;
55}
56
57void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
Jesse Gross6f6e8412015-09-21 20:21:20 -070058 bool full, const struct sw_flow_mask *mask)
Pravin B Shelare6445712013-10-03 18:16:47 -070059{
Jesse Gross6f6e8412015-09-21 20:21:20 -070060 int start = full ? 0 : mask->range.start;
61 int len = full ? sizeof *dst : range_n_bytes(&mask->range);
62 const long *m = (const long *)((const u8 *)&mask->key + start);
63 const long *s = (const long *)((const u8 *)src + start);
64 long *d = (long *)((u8 *)dst + start);
Pravin B Shelare6445712013-10-03 18:16:47 -070065 int i;
66
Jesse Gross6f6e8412015-09-21 20:21:20 -070067 /* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
68 * if 'full' is false the memory outside of the 'mask->range' is left
69 * uninitialized. This can be used as an optimization when further
70 * operations on 'dst' only use contents within 'mask->range'.
Pravin B Shelare6445712013-10-03 18:16:47 -070071 */
Jesse Gross6f6e8412015-09-21 20:21:20 -070072 for (i = 0; i < len; i += sizeof(long))
Pravin B Shelare6445712013-10-03 18:16:47 -070073 *d++ = *s++ & *m++;
74}
75
Pravin B Shelare298e502013-10-29 17:22:21 -070076struct sw_flow *ovs_flow_alloc(bool percpu_stats)
Pravin B Shelare6445712013-10-03 18:16:47 -070077{
78 struct sw_flow *flow;
Pravin B Shelare298e502013-10-29 17:22:21 -070079 int cpu;
Pravin B Shelare6445712013-10-03 18:16:47 -070080
81 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
82 if (!flow)
83 return ERR_PTR(-ENOMEM);
84
Pravin B Shelare6445712013-10-03 18:16:47 -070085 flow->sf_acts = NULL;
86 flow->mask = NULL;
87
Pravin B Shelare298e502013-10-29 17:22:21 -070088 flow->stats.is_percpu = percpu_stats;
89
90 if (!percpu_stats) {
91 flow->stats.stat = kzalloc(sizeof(*flow->stats.stat), GFP_KERNEL);
92 if (!flow->stats.stat)
93 goto err;
94
95 spin_lock_init(&flow->stats.stat->lock);
96 } else {
97 flow->stats.cpu_stats = alloc_percpu(struct flow_stats);
98 if (!flow->stats.cpu_stats)
99 goto err;
100
101 for_each_possible_cpu(cpu) {
102 struct flow_stats *cpu_stats;
103
104 cpu_stats = per_cpu_ptr(flow->stats.cpu_stats, cpu);
105 spin_lock_init(&cpu_stats->lock);
106 }
107 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700108 return flow;
Pravin B Shelare298e502013-10-29 17:22:21 -0700109err:
Wei Yongjunece37c82014-01-08 18:13:14 +0800110 kmem_cache_free(flow_cache, flow);
Pravin B Shelare298e502013-10-29 17:22:21 -0700111 return ERR_PTR(-ENOMEM);
Pravin B Shelare6445712013-10-03 18:16:47 -0700112}
113
Pravin B Shelarb637e492013-10-04 00:14:23 -0700114int ovs_flow_tbl_count(struct flow_table *table)
115{
116 return table->count;
117}
118
Pravin B Shelare6445712013-10-03 18:16:47 -0700119static struct flex_array *alloc_buckets(unsigned int n_buckets)
120{
121 struct flex_array *buckets;
122 int i, err;
123
124 buckets = flex_array_alloc(sizeof(struct hlist_head),
125 n_buckets, GFP_KERNEL);
126 if (!buckets)
127 return NULL;
128
129 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
130 if (err) {
131 flex_array_free(buckets);
132 return NULL;
133 }
134
135 for (i = 0; i < n_buckets; i++)
136 INIT_HLIST_HEAD((struct hlist_head *)
137 flex_array_get(buckets, i));
138
139 return buckets;
140}
141
142static void flow_free(struct sw_flow *flow)
143{
144 kfree((struct sf_flow_acts __force *)flow->sf_acts);
Pravin B Shelare298e502013-10-29 17:22:21 -0700145 if (flow->stats.is_percpu)
146 free_percpu(flow->stats.cpu_stats);
147 else
148 kfree(flow->stats.stat);
Pravin B Shelare6445712013-10-03 18:16:47 -0700149 kmem_cache_free(flow_cache, flow);
150}
151
152static void rcu_free_flow_callback(struct rcu_head *rcu)
153{
154 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
155
156 flow_free(flow);
157}
158
159void ovs_flow_free(struct sw_flow *flow, bool deferred)
160{
161 if (!flow)
162 return;
163
Andy Zhoue80857c2014-01-21 09:31:04 -0800164 if (flow->mask) {
165 struct sw_flow_mask *mask = flow->mask;
166
Pravin B Shelare4c6d752014-01-31 09:43:23 -0800167 /* ovs-lock is required to protect mask-refcount and
168 * mask list.
169 */
170 ASSERT_OVSL();
Andy Zhoue80857c2014-01-21 09:31:04 -0800171 BUG_ON(!mask->ref_count);
172 mask->ref_count--;
173
174 if (!mask->ref_count) {
175 list_del_rcu(&mask->list);
176 if (deferred)
177 kfree_rcu(mask, rcu);
178 else
179 kfree(mask);
180 }
181 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700182
183 if (deferred)
184 call_rcu(&flow->rcu, rcu_free_flow_callback);
185 else
186 flow_free(flow);
187}
188
189static void free_buckets(struct flex_array *buckets)
190{
191 flex_array_free(buckets);
192}
193
Andy Zhoue80857c2014-01-21 09:31:04 -0800194
Pravin B Shelarb637e492013-10-04 00:14:23 -0700195static void __table_instance_destroy(struct table_instance *ti)
Pravin B Shelare6445712013-10-03 18:16:47 -0700196{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700197 free_buckets(ti->buckets);
198 kfree(ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700199}
200
Pravin B Shelarb637e492013-10-04 00:14:23 -0700201static struct table_instance *table_instance_alloc(int new_size)
Pravin B Shelare6445712013-10-03 18:16:47 -0700202{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700203 struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700204
Pravin B Shelarb637e492013-10-04 00:14:23 -0700205 if (!ti)
Pravin B Shelare6445712013-10-03 18:16:47 -0700206 return NULL;
207
Pravin B Shelarb637e492013-10-04 00:14:23 -0700208 ti->buckets = alloc_buckets(new_size);
Pravin B Shelare6445712013-10-03 18:16:47 -0700209
Pravin B Shelarb637e492013-10-04 00:14:23 -0700210 if (!ti->buckets) {
211 kfree(ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700212 return NULL;
213 }
Pravin B Shelarb637e492013-10-04 00:14:23 -0700214 ti->n_buckets = new_size;
215 ti->node_ver = 0;
216 ti->keep_flows = false;
217 get_random_bytes(&ti->hash_seed, sizeof(u32));
218
219 return ti;
220}
221
222int ovs_flow_tbl_init(struct flow_table *table)
223{
224 struct table_instance *ti;
225
226 ti = table_instance_alloc(TBL_MIN_BUCKETS);
227
228 if (!ti)
229 return -ENOMEM;
230
231 rcu_assign_pointer(table->ti, ti);
232 INIT_LIST_HEAD(&table->mask_list);
233 table->last_rehash = jiffies;
Pravin B Shelare6445712013-10-03 18:16:47 -0700234 table->count = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700235 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700236}
237
238static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
239{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700240 struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -0700241
Pravin B Shelarb637e492013-10-04 00:14:23 -0700242 __table_instance_destroy(ti);
243}
244
245static void table_instance_destroy(struct table_instance *ti, bool deferred)
246{
Andy Zhoue80857c2014-01-21 09:31:04 -0800247 int i;
248
Pravin B Shelarb637e492013-10-04 00:14:23 -0700249 if (!ti)
250 return;
251
Andy Zhoue80857c2014-01-21 09:31:04 -0800252 if (ti->keep_flows)
253 goto skip_flows;
254
255 for (i = 0; i < ti->n_buckets; i++) {
256 struct sw_flow *flow;
257 struct hlist_head *head = flex_array_get(ti->buckets, i);
258 struct hlist_node *n;
259 int ver = ti->node_ver;
260
261 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
262 hlist_del_rcu(&flow->hash_node[ver]);
263 ovs_flow_free(flow, deferred);
264 }
265 }
266
267skip_flows:
Pravin B Shelarb637e492013-10-04 00:14:23 -0700268 if (deferred)
269 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
270 else
271 __table_instance_destroy(ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700272}
273
Andy Zhoue80857c2014-01-21 09:31:04 -0800274void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
Pravin B Shelare6445712013-10-03 18:16:47 -0700275{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700276 struct table_instance *ti = ovsl_dereference(table->ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700277
Andy Zhoue80857c2014-01-21 09:31:04 -0800278 table_instance_destroy(ti, deferred);
Pravin B Shelare6445712013-10-03 18:16:47 -0700279}
280
Pravin B Shelarb637e492013-10-04 00:14:23 -0700281struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
Pravin B Shelare6445712013-10-03 18:16:47 -0700282 u32 *bucket, u32 *last)
283{
284 struct sw_flow *flow;
285 struct hlist_head *head;
286 int ver;
287 int i;
288
Pravin B Shelarb637e492013-10-04 00:14:23 -0700289 ver = ti->node_ver;
290 while (*bucket < ti->n_buckets) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700291 i = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700292 head = flex_array_get(ti->buckets, *bucket);
Pravin B Shelare6445712013-10-03 18:16:47 -0700293 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
294 if (i < *last) {
295 i++;
296 continue;
297 }
298 *last = i + 1;
299 return flow;
300 }
301 (*bucket)++;
302 *last = 0;
303 }
304
305 return NULL;
306}
307
Pravin B Shelarb637e492013-10-04 00:14:23 -0700308static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
Pravin B Shelare6445712013-10-03 18:16:47 -0700309{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700310 hash = jhash_1word(hash, ti->hash_seed);
311 return flex_array_get(ti->buckets,
312 (hash & (ti->n_buckets - 1)));
Pravin B Shelare6445712013-10-03 18:16:47 -0700313}
314
Pravin B Shelarb637e492013-10-04 00:14:23 -0700315static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow)
Pravin B Shelare6445712013-10-03 18:16:47 -0700316{
317 struct hlist_head *head;
318
Pravin B Shelarb637e492013-10-04 00:14:23 -0700319 head = find_bucket(ti, flow->hash);
320 hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head);
Pravin B Shelare6445712013-10-03 18:16:47 -0700321}
322
Pravin B Shelarb637e492013-10-04 00:14:23 -0700323static void flow_table_copy_flows(struct table_instance *old,
324 struct table_instance *new)
Pravin B Shelare6445712013-10-03 18:16:47 -0700325{
326 int old_ver;
327 int i;
328
329 old_ver = old->node_ver;
330 new->node_ver = !old_ver;
331
332 /* Insert in new table. */
333 for (i = 0; i < old->n_buckets; i++) {
334 struct sw_flow *flow;
335 struct hlist_head *head;
336
337 head = flex_array_get(old->buckets, i);
338
339 hlist_for_each_entry(flow, head, hash_node[old_ver])
Pravin B Shelarb637e492013-10-04 00:14:23 -0700340 table_instance_insert(new, flow);
Pravin B Shelare6445712013-10-03 18:16:47 -0700341 }
342
Pravin B Shelare6445712013-10-03 18:16:47 -0700343 old->keep_flows = true;
344}
345
Pravin B Shelarb637e492013-10-04 00:14:23 -0700346static struct table_instance *table_instance_rehash(struct table_instance *ti,
Pravin B Shelare6445712013-10-03 18:16:47 -0700347 int n_buckets)
348{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700349 struct table_instance *new_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700350
Pravin B Shelarb637e492013-10-04 00:14:23 -0700351 new_ti = table_instance_alloc(n_buckets);
352 if (!new_ti)
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700353 return NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700354
Pravin B Shelarb637e492013-10-04 00:14:23 -0700355 flow_table_copy_flows(ti, new_ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700356
Pravin B Shelarb637e492013-10-04 00:14:23 -0700357 return new_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700358}
359
Pravin B Shelarb637e492013-10-04 00:14:23 -0700360int ovs_flow_tbl_flush(struct flow_table *flow_table)
Pravin B Shelare6445712013-10-03 18:16:47 -0700361{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700362 struct table_instance *old_ti;
363 struct table_instance *new_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700364
Pravin B Shelarb637e492013-10-04 00:14:23 -0700365 old_ti = ovsl_dereference(flow_table->ti);
366 new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
367 if (!new_ti)
368 return -ENOMEM;
369
370 rcu_assign_pointer(flow_table->ti, new_ti);
371 flow_table->last_rehash = jiffies;
372 flow_table->count = 0;
373
374 table_instance_destroy(old_ti, true);
375 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700376}
377
378static u32 flow_hash(const struct sw_flow_key *key, int key_start,
379 int key_end)
380{
381 u32 *hash_key = (u32 *)((u8 *)key + key_start);
382 int hash_u32s = (key_end - key_start) >> 2;
383
384 /* Make sure number of hash bytes are multiple of u32. */
385 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
386
Francesco Fusco500f8082013-12-12 16:09:06 +0100387 return arch_fast_hash2(hash_key, hash_u32s, 0);
Pravin B Shelare6445712013-10-03 18:16:47 -0700388}
389
390static int flow_key_start(const struct sw_flow_key *key)
391{
392 if (key->tun_key.ipv4_dst)
393 return 0;
394 else
395 return rounddown(offsetof(struct sw_flow_key, phy),
396 sizeof(long));
397}
398
399static bool cmp_key(const struct sw_flow_key *key1,
400 const struct sw_flow_key *key2,
401 int key_start, int key_end)
402{
403 const long *cp1 = (long *)((u8 *)key1 + key_start);
404 const long *cp2 = (long *)((u8 *)key2 + key_start);
405 long diffs = 0;
406 int i;
407
408 for (i = key_start; i < key_end; i += sizeof(long))
409 diffs |= *cp1++ ^ *cp2++;
410
411 return diffs == 0;
412}
413
414static bool flow_cmp_masked_key(const struct sw_flow *flow,
415 const struct sw_flow_key *key,
416 int key_start, int key_end)
417{
418 return cmp_key(&flow->key, key, key_start, key_end);
419}
420
421bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
422 struct sw_flow_match *match)
423{
424 struct sw_flow_key *key = match->key;
425 int key_start = flow_key_start(key);
426 int key_end = match->range.end;
427
428 return cmp_key(&flow->unmasked_key, key, key_start, key_end);
429}
430
Pravin B Shelarb637e492013-10-04 00:14:23 -0700431static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
Pravin B Shelare6445712013-10-03 18:16:47 -0700432 const struct sw_flow_key *unmasked,
433 struct sw_flow_mask *mask)
434{
435 struct sw_flow *flow;
436 struct hlist_head *head;
437 int key_start = mask->range.start;
438 int key_end = mask->range.end;
439 u32 hash;
440 struct sw_flow_key masked_key;
441
Jesse Gross6f6e8412015-09-21 20:21:20 -0700442 ovs_flow_mask_key(&masked_key, unmasked, false, mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700443 hash = flow_hash(&masked_key, key_start, key_end);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700444 head = find_bucket(ti, hash);
445 hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) {
Pravin B Shelar8ddd0942013-10-29 23:10:58 -0700446 if (flow->mask == mask && flow->hash == hash &&
Pravin B Shelare6445712013-10-03 18:16:47 -0700447 flow_cmp_masked_key(flow, &masked_key,
448 key_start, key_end))
449 return flow;
450 }
451 return NULL;
452}
453
Andy Zhou5bb50632013-11-25 10:42:46 -0800454struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
Andy Zhou1bd71162013-10-22 10:42:46 -0700455 const struct sw_flow_key *key,
456 u32 *n_mask_hit)
Pravin B Shelare6445712013-10-03 18:16:47 -0700457{
Jesse Gross663efa32013-12-03 10:58:53 -0800458 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700459 struct sw_flow_mask *mask;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700460 struct sw_flow *flow;
Pravin B Shelare6445712013-10-03 18:16:47 -0700461
Andy Zhou1bd71162013-10-22 10:42:46 -0700462 *n_mask_hit = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700463 list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
Andy Zhou1bd71162013-10-22 10:42:46 -0700464 (*n_mask_hit)++;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700465 flow = masked_flow_lookup(ti, key, mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700466 if (flow) /* Found */
Pravin B Shelarb637e492013-10-04 00:14:23 -0700467 return flow;
Pravin B Shelare6445712013-10-03 18:16:47 -0700468 }
Pravin B Shelarb637e492013-10-04 00:14:23 -0700469 return NULL;
470}
Pravin B Shelare6445712013-10-03 18:16:47 -0700471
Andy Zhou5bb50632013-11-25 10:42:46 -0800472struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
473 const struct sw_flow_key *key)
474{
475 u32 __always_unused n_mask_hit;
476
477 return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
478}
479
Andy Zhou1bd71162013-10-22 10:42:46 -0700480int ovs_flow_tbl_num_masks(const struct flow_table *table)
481{
482 struct sw_flow_mask *mask;
483 int num = 0;
484
485 list_for_each_entry(mask, &table->mask_list, list)
486 num++;
487
488 return num;
489}
490
Pravin B Shelarb637e492013-10-04 00:14:23 -0700491static struct table_instance *table_instance_expand(struct table_instance *ti)
492{
493 return table_instance_rehash(ti, ti->n_buckets * 2);
Pravin B Shelare6445712013-10-03 18:16:47 -0700494}
495
Pravin B Shelare6445712013-10-03 18:16:47 -0700496void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
497{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700498 struct table_instance *ti = ovsl_dereference(table->ti);
499
Pravin B Shelare6445712013-10-03 18:16:47 -0700500 BUG_ON(table->count == 0);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700501 hlist_del_rcu(&flow->hash_node[ti->node_ver]);
Pravin B Shelare6445712013-10-03 18:16:47 -0700502 table->count--;
503}
504
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700505static struct sw_flow_mask *mask_alloc(void)
Pravin B Shelare6445712013-10-03 18:16:47 -0700506{
507 struct sw_flow_mask *mask;
508
509 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
510 if (mask)
Andy Zhoue80857c2014-01-21 09:31:04 -0800511 mask->ref_count = 1;
Pravin B Shelare6445712013-10-03 18:16:47 -0700512
513 return mask;
514}
515
Pravin B Shelare6445712013-10-03 18:16:47 -0700516static bool mask_equal(const struct sw_flow_mask *a,
517 const struct sw_flow_mask *b)
518{
519 u8 *a_ = (u8 *)&a->key + a->range.start;
520 u8 *b_ = (u8 *)&b->key + b->range.start;
521
522 return (a->range.end == b->range.end)
523 && (a->range.start == b->range.start)
524 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
525}
526
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700527static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700528 const struct sw_flow_mask *mask)
529{
530 struct list_head *ml;
531
Pravin B Shelarb637e492013-10-04 00:14:23 -0700532 list_for_each(ml, &tbl->mask_list) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700533 struct sw_flow_mask *m;
534 m = container_of(ml, struct sw_flow_mask, list);
535 if (mask_equal(mask, m))
536 return m;
537 }
538
539 return NULL;
540}
541
Ben Pfaffd1211902013-11-25 10:40:51 -0800542/* Add 'mask' into the mask list, if it is not already there. */
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700543static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
544 struct sw_flow_mask *new)
Pravin B Shelare6445712013-10-03 18:16:47 -0700545{
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700546 struct sw_flow_mask *mask;
547 mask = flow_mask_find(tbl, new);
548 if (!mask) {
549 /* Allocate a new mask if none exsits. */
550 mask = mask_alloc();
551 if (!mask)
552 return -ENOMEM;
553 mask->key = new->key;
554 mask->range = new->range;
555 list_add_rcu(&mask->list, &tbl->mask_list);
Andy Zhoue80857c2014-01-21 09:31:04 -0800556 } else {
557 BUG_ON(!mask->ref_count);
558 mask->ref_count++;
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700559 }
560
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700561 flow->mask = mask;
562 return 0;
563}
564
565int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
566 struct sw_flow_mask *mask)
567{
568 struct table_instance *new_ti = NULL;
569 struct table_instance *ti;
570 int err;
571
572 err = flow_mask_insert(table, flow, mask);
573 if (err)
574 return err;
575
576 flow->hash = flow_hash(&flow->key, flow->mask->range.start,
577 flow->mask->range.end);
578 ti = ovsl_dereference(table->ti);
579 table_instance_insert(ti, flow);
580 table->count++;
581
582 /* Expand table, if necessary, to make room. */
583 if (table->count > ti->n_buckets)
584 new_ti = table_instance_expand(ti);
585 else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
586 new_ti = table_instance_rehash(ti, ti->n_buckets);
587
588 if (new_ti) {
589 rcu_assign_pointer(table->ti, new_ti);
590 table_instance_destroy(ti, true);
591 table->last_rehash = jiffies;
592 }
593 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700594}
595
596/* Initializes the flow module.
597 * Returns zero if successful or a negative error code. */
598int ovs_flow_init(void)
599{
600 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
601 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
602
603 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
604 0, NULL);
605 if (flow_cache == NULL)
606 return -ENOMEM;
607
608 return 0;
609}
610
611/* Uninitializes the flow module. */
612void ovs_flow_exit(void)
613{
614 kmem_cache_destroy(flow_cache);
615}