2 * Copyright (c) 2007-2013 Nicira, Inc.
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.
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.
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
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>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.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>
45 #include <net/ndisc.h>
47 static struct kmem_cache *flow_cache;
49 static u16 range_n_bytes(const struct sw_flow_key_range *range)
51 return range->end - range->start;
54 void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
55 const struct sw_flow_mask *mask)
57 const long *m = (long *)((u8 *)&mask->key + mask->range.start);
58 const long *s = (long *)((u8 *)src + mask->range.start);
59 long *d = (long *)((u8 *)dst + mask->range.start);
62 /* The memory outside of the 'mask->range' are not set since
63 * further operations on 'dst' only uses contents within
66 for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
70 struct sw_flow *ovs_flow_alloc(void)
74 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
76 return ERR_PTR(-ENOMEM);
78 spin_lock_init(&flow->lock);
85 static struct flex_array *alloc_buckets(unsigned int n_buckets)
87 struct flex_array *buckets;
90 buckets = flex_array_alloc(sizeof(struct hlist_head),
91 n_buckets, GFP_KERNEL);
95 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
97 flex_array_free(buckets);
101 for (i = 0; i < n_buckets; i++)
102 INIT_HLIST_HEAD((struct hlist_head *)
103 flex_array_get(buckets, i));
108 static void flow_free(struct sw_flow *flow)
110 kfree((struct sf_flow_acts __force *)flow->sf_acts);
111 kmem_cache_free(flow_cache, flow);
114 static void rcu_free_flow_callback(struct rcu_head *rcu)
116 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
121 void ovs_flow_free(struct sw_flow *flow, bool deferred)
126 ovs_sw_flow_mask_del_ref(flow->mask, deferred);
129 call_rcu(&flow->rcu, rcu_free_flow_callback);
134 static void free_buckets(struct flex_array *buckets)
136 flex_array_free(buckets);
139 static void __flow_tbl_destroy(struct flow_table *table)
143 if (table->keep_flows)
146 for (i = 0; i < table->n_buckets; i++) {
147 struct sw_flow *flow;
148 struct hlist_head *head = flex_array_get(table->buckets, i);
149 struct hlist_node *n;
150 int ver = table->node_ver;
152 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
153 hlist_del(&flow->hash_node[ver]);
154 ovs_flow_free(flow, false);
158 BUG_ON(!list_empty(table->mask_list));
159 kfree(table->mask_list);
162 free_buckets(table->buckets);
166 static struct flow_table *__flow_tbl_alloc(int new_size)
168 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
173 table->buckets = alloc_buckets(new_size);
175 if (!table->buckets) {
179 table->n_buckets = new_size;
182 table->keep_flows = false;
183 get_random_bytes(&table->hash_seed, sizeof(u32));
184 table->mask_list = NULL;
189 struct flow_table *ovs_flow_tbl_alloc(int new_size)
191 struct flow_table *table = __flow_tbl_alloc(new_size);
196 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
197 if (!table->mask_list) {
198 table->keep_flows = true;
199 __flow_tbl_destroy(table);
202 INIT_LIST_HEAD(table->mask_list);
207 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
209 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
211 __flow_tbl_destroy(table);
214 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
220 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
222 __flow_tbl_destroy(table);
225 struct sw_flow *ovs_flow_tbl_dump_next(struct flow_table *table,
226 u32 *bucket, u32 *last)
228 struct sw_flow *flow;
229 struct hlist_head *head;
233 ver = table->node_ver;
234 while (*bucket < table->n_buckets) {
236 head = flex_array_get(table->buckets, *bucket);
237 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
252 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
254 hash = jhash_1word(hash, table->hash_seed);
255 return flex_array_get(table->buckets,
256 (hash & (table->n_buckets - 1)));
259 static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
261 struct hlist_head *head;
263 head = find_bucket(table, flow->hash);
264 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
269 static void flow_table_copy_flows(struct flow_table *old,
270 struct flow_table *new)
275 old_ver = old->node_ver;
276 new->node_ver = !old_ver;
278 /* Insert in new table. */
279 for (i = 0; i < old->n_buckets; i++) {
280 struct sw_flow *flow;
281 struct hlist_head *head;
283 head = flex_array_get(old->buckets, i);
285 hlist_for_each_entry(flow, head, hash_node[old_ver])
286 __tbl_insert(new, flow);
289 new->mask_list = old->mask_list;
290 old->keep_flows = true;
293 static struct flow_table *__flow_tbl_rehash(struct flow_table *table,
296 struct flow_table *new_table;
298 new_table = __flow_tbl_alloc(n_buckets);
300 return ERR_PTR(-ENOMEM);
302 flow_table_copy_flows(table, new_table);
307 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
309 return __flow_tbl_rehash(table, table->n_buckets);
312 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
314 return __flow_tbl_rehash(table, table->n_buckets * 2);
317 static u32 flow_hash(const struct sw_flow_key *key, int key_start,
320 u32 *hash_key = (u32 *)((u8 *)key + key_start);
321 int hash_u32s = (key_end - key_start) >> 2;
323 /* Make sure number of hash bytes are multiple of u32. */
324 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
326 return jhash2(hash_key, hash_u32s, 0);
329 static int flow_key_start(const struct sw_flow_key *key)
331 if (key->tun_key.ipv4_dst)
334 return rounddown(offsetof(struct sw_flow_key, phy),
338 static bool cmp_key(const struct sw_flow_key *key1,
339 const struct sw_flow_key *key2,
340 int key_start, int key_end)
342 const long *cp1 = (long *)((u8 *)key1 + key_start);
343 const long *cp2 = (long *)((u8 *)key2 + key_start);
347 for (i = key_start; i < key_end; i += sizeof(long))
348 diffs |= *cp1++ ^ *cp2++;
353 static bool flow_cmp_masked_key(const struct sw_flow *flow,
354 const struct sw_flow_key *key,
355 int key_start, int key_end)
357 return cmp_key(&flow->key, key, key_start, key_end);
360 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
361 struct sw_flow_match *match)
363 struct sw_flow_key *key = match->key;
364 int key_start = flow_key_start(key);
365 int key_end = match->range.end;
367 return cmp_key(&flow->unmasked_key, key, key_start, key_end);
370 static struct sw_flow *masked_flow_lookup(struct flow_table *table,
371 const struct sw_flow_key *unmasked,
372 struct sw_flow_mask *mask)
374 struct sw_flow *flow;
375 struct hlist_head *head;
376 int key_start = mask->range.start;
377 int key_end = mask->range.end;
379 struct sw_flow_key masked_key;
381 ovs_flow_mask_key(&masked_key, unmasked, mask);
382 hash = flow_hash(&masked_key, key_start, key_end);
383 head = find_bucket(table, hash);
384 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
385 if (flow->mask == mask &&
386 flow_cmp_masked_key(flow, &masked_key,
393 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
394 const struct sw_flow_key *key)
396 struct sw_flow *flow = NULL;
397 struct sw_flow_mask *mask;
399 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
400 flow = masked_flow_lookup(tbl, key, mask);
401 if (flow) /* Found */
408 void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
410 flow->hash = flow_hash(&flow->key, flow->mask->range.start,
411 flow->mask->range.end);
412 __tbl_insert(table, flow);
415 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
417 BUG_ON(table->count == 0);
418 hlist_del_rcu(&flow->hash_node[table->node_ver]);
422 struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
424 struct sw_flow_mask *mask;
426 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
433 void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
438 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
440 struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
445 void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
450 BUG_ON(!mask->ref_count);
453 if (!mask->ref_count) {
454 list_del_rcu(&mask->list);
456 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
462 static bool mask_equal(const struct sw_flow_mask *a,
463 const struct sw_flow_mask *b)
465 u8 *a_ = (u8 *)&a->key + a->range.start;
466 u8 *b_ = (u8 *)&b->key + b->range.start;
468 return (a->range.end == b->range.end)
469 && (a->range.start == b->range.start)
470 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
473 struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
474 const struct sw_flow_mask *mask)
476 struct list_head *ml;
478 list_for_each(ml, tbl->mask_list) {
479 struct sw_flow_mask *m;
480 m = container_of(ml, struct sw_flow_mask, list);
481 if (mask_equal(mask, m))
489 * add a new mask into the mask list.
490 * The caller needs to make sure that 'mask' is not the same
491 * as any masks that are already on the list.
493 void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
495 list_add_rcu(&mask->list, tbl->mask_list);
498 /* Initializes the flow module.
499 * Returns zero if successful or a negative error code. */
500 int ovs_flow_init(void)
502 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
503 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
505 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
507 if (flow_cache == NULL)
513 /* Uninitializes the flow module. */
514 void ovs_flow_exit(void)
516 kmem_cache_destroy(flow_cache);