blob: aa349514e4cb5da84f9184c5a02038b8d6fff862 [file] [log] [blame]
Pravin B Shelare6445712013-10-03 18:16:47 -07001/*
Pravin B Shelar9b996e52014-05-06 18:41:20 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Pravin B Shelare6445712013-10-03 18:16:47 -07003 *
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>
Daniel Borkmann87545892014-12-10 16:33:11 +010028#include <linux/jhash.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;
Jarno Rajahalme63e79592014-03-27 12:42:54 -070051struct kmem_cache *flow_stats_cache __read_mostly;
Pravin B Shelare6445712013-10-03 18:16:47 -070052
53static u16 range_n_bytes(const struct sw_flow_key_range *range)
54{
55 return range->end - range->start;
56}
57
58void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
Jesse Gross6d80e352015-09-21 20:21:20 -070059 bool full, const struct sw_flow_mask *mask)
Pravin B Shelare6445712013-10-03 18:16:47 -070060{
Jesse Gross6d80e352015-09-21 20:21:20 -070061 int start = full ? 0 : mask->range.start;
62 int len = full ? sizeof *dst : range_n_bytes(&mask->range);
63 const long *m = (const long *)((const u8 *)&mask->key + start);
64 const long *s = (const long *)((const u8 *)src + start);
65 long *d = (long *)((u8 *)dst + start);
Pravin B Shelare6445712013-10-03 18:16:47 -070066 int i;
67
Jesse Gross6d80e352015-09-21 20:21:20 -070068 /* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
69 * if 'full' is false the memory outside of the 'mask->range' is left
70 * uninitialized. This can be used as an optimization when further
71 * operations on 'dst' only use contents within 'mask->range'.
Pravin B Shelare6445712013-10-03 18:16:47 -070072 */
Jesse Gross6d80e352015-09-21 20:21:20 -070073 for (i = 0; i < len; i += sizeof(long))
Pravin B Shelare6445712013-10-03 18:16:47 -070074 *d++ = *s++ & *m++;
75}
76
Jarno Rajahalme23dabf82014-03-27 12:35:23 -070077struct sw_flow *ovs_flow_alloc(void)
Pravin B Shelare6445712013-10-03 18:16:47 -070078{
79 struct sw_flow *flow;
Jarno Rajahalme63e79592014-03-27 12:42:54 -070080 struct flow_stats *stats;
81 int node;
Pravin B Shelare6445712013-10-03 18:16:47 -070082
83 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
84 if (!flow)
85 return ERR_PTR(-ENOMEM);
86
Pravin B Shelare6445712013-10-03 18:16:47 -070087 flow->sf_acts = NULL;
88 flow->mask = NULL;
Pravin B Shelarca539342015-02-06 11:17:13 -080089 flow->id.unmasked_key = NULL;
90 flow->id.ufid_len = 0;
Jarno Rajahalme63e79592014-03-27 12:42:54 -070091 flow->stats_last_writer = NUMA_NO_NODE;
Pravin B Shelare6445712013-10-03 18:16:47 -070092
Jarno Rajahalme63e79592014-03-27 12:42:54 -070093 /* Initialize the default stat node. */
94 stats = kmem_cache_alloc_node(flow_stats_cache,
95 GFP_KERNEL | __GFP_ZERO, 0);
96 if (!stats)
Jarno Rajahalme23dabf82014-03-27 12:35:23 -070097 goto err;
Pravin B Shelare298e502013-10-29 17:22:21 -070098
Jarno Rajahalme63e79592014-03-27 12:42:54 -070099 spin_lock_init(&stats->lock);
Pravin B Shelare298e502013-10-29 17:22:21 -0700100
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700101 RCU_INIT_POINTER(flow->stats[0], stats);
102
103 for_each_node(node)
104 if (node != 0)
105 RCU_INIT_POINTER(flow->stats[node], NULL);
106
Pravin B Shelare6445712013-10-03 18:16:47 -0700107 return flow;
Pravin B Shelare298e502013-10-29 17:22:21 -0700108err:
Wei Yongjunece37c82014-01-08 18:13:14 +0800109 kmem_cache_free(flow_cache, flow);
Pravin B Shelare298e502013-10-29 17:22:21 -0700110 return ERR_PTR(-ENOMEM);
Pravin B Shelare6445712013-10-03 18:16:47 -0700111}
112
Thomas Graf12eb18f2014-11-06 06:58:52 -0800113int ovs_flow_tbl_count(const struct flow_table *table)
Pravin B Shelarb637e492013-10-04 00:14:23 -0700114{
115 return table->count;
116}
117
Pravin B Shelare6445712013-10-03 18:16:47 -0700118static struct flex_array *alloc_buckets(unsigned int n_buckets)
119{
120 struct flex_array *buckets;
121 int i, err;
122
123 buckets = flex_array_alloc(sizeof(struct hlist_head),
124 n_buckets, GFP_KERNEL);
125 if (!buckets)
126 return NULL;
127
128 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
129 if (err) {
130 flex_array_free(buckets);
131 return NULL;
132 }
133
134 for (i = 0; i < n_buckets; i++)
135 INIT_HLIST_HEAD((struct hlist_head *)
136 flex_array_get(buckets, i));
137
138 return buckets;
139}
140
141static void flow_free(struct sw_flow *flow)
142{
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700143 int node;
144
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800145 if (ovs_identifier_is_key(&flow->id))
146 kfree(flow->id.unmasked_key);
Jarno Rajahalmeeb072652014-05-05 14:15:18 -0700147 kfree((struct sw_flow_actions __force *)flow->sf_acts);
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700148 for_each_node(node)
149 if (flow->stats[node])
150 kmem_cache_free(flow_stats_cache,
151 (struct flow_stats __force *)flow->stats[node]);
Pravin B Shelare6445712013-10-03 18:16:47 -0700152 kmem_cache_free(flow_cache, flow);
153}
154
155static void rcu_free_flow_callback(struct rcu_head *rcu)
156{
157 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
158
159 flow_free(flow);
160}
161
162void ovs_flow_free(struct sw_flow *flow, bool deferred)
163{
164 if (!flow)
165 return;
166
Pravin B Shelare6445712013-10-03 18:16:47 -0700167 if (deferred)
168 call_rcu(&flow->rcu, rcu_free_flow_callback);
169 else
170 flow_free(flow);
171}
172
173static void free_buckets(struct flex_array *buckets)
174{
175 flex_array_free(buckets);
176}
177
Andy Zhoue80857c2014-01-21 09:31:04 -0800178
Pravin B Shelarb637e492013-10-04 00:14:23 -0700179static void __table_instance_destroy(struct table_instance *ti)
Pravin B Shelare6445712013-10-03 18:16:47 -0700180{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700181 free_buckets(ti->buckets);
182 kfree(ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700183}
184
Pravin B Shelarb637e492013-10-04 00:14:23 -0700185static struct table_instance *table_instance_alloc(int new_size)
Pravin B Shelare6445712013-10-03 18:16:47 -0700186{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700187 struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700188
Pravin B Shelarb637e492013-10-04 00:14:23 -0700189 if (!ti)
Pravin B Shelare6445712013-10-03 18:16:47 -0700190 return NULL;
191
Pravin B Shelarb637e492013-10-04 00:14:23 -0700192 ti->buckets = alloc_buckets(new_size);
Pravin B Shelare6445712013-10-03 18:16:47 -0700193
Pravin B Shelarb637e492013-10-04 00:14:23 -0700194 if (!ti->buckets) {
195 kfree(ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700196 return NULL;
197 }
Pravin B Shelarb637e492013-10-04 00:14:23 -0700198 ti->n_buckets = new_size;
199 ti->node_ver = 0;
200 ti->keep_flows = false;
201 get_random_bytes(&ti->hash_seed, sizeof(u32));
202
203 return ti;
204}
205
206int ovs_flow_tbl_init(struct flow_table *table)
207{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800208 struct table_instance *ti, *ufid_ti;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700209
210 ti = table_instance_alloc(TBL_MIN_BUCKETS);
211
212 if (!ti)
213 return -ENOMEM;
214
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800215 ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
216 if (!ufid_ti)
217 goto free_ti;
218
Pravin B Shelarb637e492013-10-04 00:14:23 -0700219 rcu_assign_pointer(table->ti, ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800220 rcu_assign_pointer(table->ufid_ti, ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700221 INIT_LIST_HEAD(&table->mask_list);
222 table->last_rehash = jiffies;
Pravin B Shelare6445712013-10-03 18:16:47 -0700223 table->count = 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800224 table->ufid_count = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700225 return 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800226
227free_ti:
228 __table_instance_destroy(ti);
229 return -ENOMEM;
Pravin B Shelare6445712013-10-03 18:16:47 -0700230}
231
232static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
233{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700234 struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -0700235
Pravin B Shelarb637e492013-10-04 00:14:23 -0700236 __table_instance_destroy(ti);
237}
238
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800239static void table_instance_destroy(struct table_instance *ti,
240 struct table_instance *ufid_ti,
241 bool deferred)
Pravin B Shelarb637e492013-10-04 00:14:23 -0700242{
Andy Zhoue80857c2014-01-21 09:31:04 -0800243 int i;
244
Pravin B Shelarb637e492013-10-04 00:14:23 -0700245 if (!ti)
246 return;
247
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800248 BUG_ON(!ufid_ti);
Andy Zhoue80857c2014-01-21 09:31:04 -0800249 if (ti->keep_flows)
250 goto skip_flows;
251
252 for (i = 0; i < ti->n_buckets; i++) {
253 struct sw_flow *flow;
254 struct hlist_head *head = flex_array_get(ti->buckets, i);
255 struct hlist_node *n;
256 int ver = ti->node_ver;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800257 int ufid_ver = ufid_ti->node_ver;
Andy Zhoue80857c2014-01-21 09:31:04 -0800258
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800259 hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
260 hlist_del_rcu(&flow->flow_table.node[ver]);
261 if (ovs_identifier_is_ufid(&flow->id))
262 hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
Andy Zhoue80857c2014-01-21 09:31:04 -0800263 ovs_flow_free(flow, deferred);
264 }
265 }
266
267skip_flows:
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800268 if (deferred) {
Pravin B Shelarb637e492013-10-04 00:14:23 -0700269 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800270 call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
271 } else {
Pravin B Shelarb637e492013-10-04 00:14:23 -0700272 __table_instance_destroy(ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800273 __table_instance_destroy(ufid_ti);
274 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700275}
276
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700277/* No need for locking this function is called from RCU callback or
278 * error path.
279 */
280void ovs_flow_tbl_destroy(struct flow_table *table)
Pravin B Shelare6445712013-10-03 18:16:47 -0700281{
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700282 struct table_instance *ti = rcu_dereference_raw(table->ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800283 struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700284
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800285 table_instance_destroy(ti, ufid_ti, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700286}
287
Pravin B Shelarb637e492013-10-04 00:14:23 -0700288struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
Pravin B Shelare6445712013-10-03 18:16:47 -0700289 u32 *bucket, u32 *last)
290{
291 struct sw_flow *flow;
292 struct hlist_head *head;
293 int ver;
294 int i;
295
Pravin B Shelarb637e492013-10-04 00:14:23 -0700296 ver = ti->node_ver;
297 while (*bucket < ti->n_buckets) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700298 i = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700299 head = flex_array_get(ti->buckets, *bucket);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800300 hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700301 if (i < *last) {
302 i++;
303 continue;
304 }
305 *last = i + 1;
306 return flow;
307 }
308 (*bucket)++;
309 *last = 0;
310 }
311
312 return NULL;
313}
314
Pravin B Shelarb637e492013-10-04 00:14:23 -0700315static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
Pravin B Shelare6445712013-10-03 18:16:47 -0700316{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700317 hash = jhash_1word(hash, ti->hash_seed);
318 return flex_array_get(ti->buckets,
319 (hash & (ti->n_buckets - 1)));
Pravin B Shelare6445712013-10-03 18:16:47 -0700320}
321
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800322static void table_instance_insert(struct table_instance *ti,
323 struct sw_flow *flow)
Pravin B Shelare6445712013-10-03 18:16:47 -0700324{
325 struct hlist_head *head;
326
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800327 head = find_bucket(ti, flow->flow_table.hash);
328 hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
329}
330
331static void ufid_table_instance_insert(struct table_instance *ti,
332 struct sw_flow *flow)
333{
334 struct hlist_head *head;
335
336 head = find_bucket(ti, flow->ufid_table.hash);
337 hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
Pravin B Shelare6445712013-10-03 18:16:47 -0700338}
339
Pravin B Shelarb637e492013-10-04 00:14:23 -0700340static void flow_table_copy_flows(struct table_instance *old,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800341 struct table_instance *new, bool ufid)
Pravin B Shelare6445712013-10-03 18:16:47 -0700342{
343 int old_ver;
344 int i;
345
346 old_ver = old->node_ver;
347 new->node_ver = !old_ver;
348
349 /* Insert in new table. */
350 for (i = 0; i < old->n_buckets; i++) {
351 struct sw_flow *flow;
352 struct hlist_head *head;
353
354 head = flex_array_get(old->buckets, i);
355
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800356 if (ufid)
357 hlist_for_each_entry(flow, head,
358 ufid_table.node[old_ver])
359 ufid_table_instance_insert(new, flow);
360 else
361 hlist_for_each_entry(flow, head,
362 flow_table.node[old_ver])
363 table_instance_insert(new, flow);
Pravin B Shelare6445712013-10-03 18:16:47 -0700364 }
365
Pravin B Shelare6445712013-10-03 18:16:47 -0700366 old->keep_flows = true;
367}
368
Pravin B Shelarb637e492013-10-04 00:14:23 -0700369static struct table_instance *table_instance_rehash(struct table_instance *ti,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800370 int n_buckets, bool ufid)
Pravin B Shelare6445712013-10-03 18:16:47 -0700371{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700372 struct table_instance *new_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700373
Pravin B Shelarb637e492013-10-04 00:14:23 -0700374 new_ti = table_instance_alloc(n_buckets);
375 if (!new_ti)
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700376 return NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700377
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800378 flow_table_copy_flows(ti, new_ti, ufid);
Pravin B Shelare6445712013-10-03 18:16:47 -0700379
Pravin B Shelarb637e492013-10-04 00:14:23 -0700380 return new_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700381}
382
Pravin B Shelarb637e492013-10-04 00:14:23 -0700383int ovs_flow_tbl_flush(struct flow_table *flow_table)
Pravin B Shelare6445712013-10-03 18:16:47 -0700384{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800385 struct table_instance *old_ti, *new_ti;
386 struct table_instance *old_ufid_ti, *new_ufid_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700387
Pravin B Shelarb637e492013-10-04 00:14:23 -0700388 new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
389 if (!new_ti)
390 return -ENOMEM;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800391 new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
392 if (!new_ufid_ti)
393 goto err_free_ti;
394
395 old_ti = ovsl_dereference(flow_table->ti);
396 old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700397
398 rcu_assign_pointer(flow_table->ti, new_ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800399 rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700400 flow_table->last_rehash = jiffies;
401 flow_table->count = 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800402 flow_table->ufid_count = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700403
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800404 table_instance_destroy(old_ti, old_ufid_ti, true);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700405 return 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800406
407err_free_ti:
408 __table_instance_destroy(new_ti);
409 return -ENOMEM;
Pravin B Shelare6445712013-10-03 18:16:47 -0700410}
411
Joe Stringer272c2cf2015-01-21 16:42:50 -0800412static u32 flow_hash(const struct sw_flow_key *key,
413 const struct sw_flow_key_range *range)
Pravin B Shelare6445712013-10-03 18:16:47 -0700414{
Joe Stringer272c2cf2015-01-21 16:42:50 -0800415 int key_start = range->start;
416 int key_end = range->end;
Daniele Di Proietto70851302014-01-23 10:56:49 -0800417 const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
Pravin B Shelare6445712013-10-03 18:16:47 -0700418 int hash_u32s = (key_end - key_start) >> 2;
419
420 /* Make sure number of hash bytes are multiple of u32. */
421 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
422
Daniel Borkmann87545892014-12-10 16:33:11 +0100423 return jhash2(hash_key, hash_u32s, 0);
Pravin B Shelare6445712013-10-03 18:16:47 -0700424}
425
426static int flow_key_start(const struct sw_flow_key *key)
427{
428 if (key->tun_key.ipv4_dst)
429 return 0;
430 else
431 return rounddown(offsetof(struct sw_flow_key, phy),
432 sizeof(long));
433}
434
435static bool cmp_key(const struct sw_flow_key *key1,
436 const struct sw_flow_key *key2,
437 int key_start, int key_end)
438{
Daniele Di Proietto70851302014-01-23 10:56:49 -0800439 const long *cp1 = (const long *)((const u8 *)key1 + key_start);
440 const long *cp2 = (const long *)((const u8 *)key2 + key_start);
Pravin B Shelare6445712013-10-03 18:16:47 -0700441 long diffs = 0;
442 int i;
443
444 for (i = key_start; i < key_end; i += sizeof(long))
445 diffs |= *cp1++ ^ *cp2++;
446
447 return diffs == 0;
448}
449
450static bool flow_cmp_masked_key(const struct sw_flow *flow,
451 const struct sw_flow_key *key,
Joe Stringer272c2cf2015-01-21 16:42:50 -0800452 const struct sw_flow_key_range *range)
Pravin B Shelare6445712013-10-03 18:16:47 -0700453{
Joe Stringer272c2cf2015-01-21 16:42:50 -0800454 return cmp_key(&flow->key, key, range->start, range->end);
Pravin B Shelare6445712013-10-03 18:16:47 -0700455}
456
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800457static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
458 const struct sw_flow_match *match)
Pravin B Shelare6445712013-10-03 18:16:47 -0700459{
460 struct sw_flow_key *key = match->key;
461 int key_start = flow_key_start(key);
462 int key_end = match->range.end;
463
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800464 BUG_ON(ovs_identifier_is_ufid(&flow->id));
465 return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
Pravin B Shelare6445712013-10-03 18:16:47 -0700466}
467
Pravin B Shelarb637e492013-10-04 00:14:23 -0700468static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
Pravin B Shelare6445712013-10-03 18:16:47 -0700469 const struct sw_flow_key *unmasked,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800470 const struct sw_flow_mask *mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700471{
472 struct sw_flow *flow;
473 struct hlist_head *head;
Pravin B Shelare6445712013-10-03 18:16:47 -0700474 u32 hash;
475 struct sw_flow_key masked_key;
476
Jesse Gross6d80e352015-09-21 20:21:20 -0700477 ovs_flow_mask_key(&masked_key, unmasked, false, mask);
Joe Stringer272c2cf2015-01-21 16:42:50 -0800478 hash = flow_hash(&masked_key, &mask->range);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700479 head = find_bucket(ti, hash);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800480 hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
481 if (flow->mask == mask && flow->flow_table.hash == hash &&
Joe Stringer272c2cf2015-01-21 16:42:50 -0800482 flow_cmp_masked_key(flow, &masked_key, &mask->range))
Pravin B Shelare6445712013-10-03 18:16:47 -0700483 return flow;
484 }
485 return NULL;
486}
487
Andy Zhou5bb50632013-11-25 10:42:46 -0800488struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
Andy Zhou1bd71162013-10-22 10:42:46 -0700489 const struct sw_flow_key *key,
490 u32 *n_mask_hit)
Pravin B Shelare6445712013-10-03 18:16:47 -0700491{
Jesse Gross663efa32013-12-03 10:58:53 -0800492 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700493 struct sw_flow_mask *mask;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700494 struct sw_flow *flow;
Pravin B Shelare6445712013-10-03 18:16:47 -0700495
Andy Zhou1bd71162013-10-22 10:42:46 -0700496 *n_mask_hit = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700497 list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
Andy Zhou1bd71162013-10-22 10:42:46 -0700498 (*n_mask_hit)++;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700499 flow = masked_flow_lookup(ti, key, mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700500 if (flow) /* Found */
Pravin B Shelarb637e492013-10-04 00:14:23 -0700501 return flow;
Pravin B Shelare6445712013-10-03 18:16:47 -0700502 }
Pravin B Shelarb637e492013-10-04 00:14:23 -0700503 return NULL;
504}
Pravin B Shelare6445712013-10-03 18:16:47 -0700505
Andy Zhou5bb50632013-11-25 10:42:46 -0800506struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
507 const struct sw_flow_key *key)
508{
509 u32 __always_unused n_mask_hit;
510
511 return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
512}
513
Alex Wang4a46b242014-06-30 20:30:29 -0700514struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800515 const struct sw_flow_match *match)
Alex Wang4a46b242014-06-30 20:30:29 -0700516{
517 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
518 struct sw_flow_mask *mask;
519 struct sw_flow *flow;
520
521 /* Always called under ovs-mutex. */
522 list_for_each_entry(mask, &tbl->mask_list, list) {
523 flow = masked_flow_lookup(ti, match->key, mask);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800524 if (flow && ovs_identifier_is_key(&flow->id) &&
525 ovs_flow_cmp_unmasked_key(flow, match))
526 return flow;
527 }
528 return NULL;
529}
530
531static u32 ufid_hash(const struct sw_flow_id *sfid)
532{
533 return jhash(sfid->ufid, sfid->ufid_len, 0);
534}
535
536static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
537 const struct sw_flow_id *sfid)
538{
539 if (flow->id.ufid_len != sfid->ufid_len)
540 return false;
541
542 return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
543}
544
545bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
546{
547 if (ovs_identifier_is_ufid(&flow->id))
548 return flow_cmp_masked_key(flow, match->key, &match->range);
549
550 return ovs_flow_cmp_unmasked_key(flow, match);
551}
552
553struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
554 const struct sw_flow_id *ufid)
555{
556 struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
557 struct sw_flow *flow;
558 struct hlist_head *head;
559 u32 hash;
560
561 hash = ufid_hash(ufid);
562 head = find_bucket(ti, hash);
563 hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
564 if (flow->ufid_table.hash == hash &&
565 ovs_flow_cmp_ufid(flow, ufid))
Alex Wang4a46b242014-06-30 20:30:29 -0700566 return flow;
567 }
568 return NULL;
569}
570
Andy Zhou1bd71162013-10-22 10:42:46 -0700571int ovs_flow_tbl_num_masks(const struct flow_table *table)
572{
573 struct sw_flow_mask *mask;
574 int num = 0;
575
576 list_for_each_entry(mask, &table->mask_list, list)
577 num++;
578
579 return num;
580}
581
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800582static struct table_instance *table_instance_expand(struct table_instance *ti,
583 bool ufid)
Pravin B Shelarb637e492013-10-04 00:14:23 -0700584{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800585 return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
Pravin B Shelare6445712013-10-03 18:16:47 -0700586}
587
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700588/* Remove 'mask' from the mask list, if it is not needed any more. */
589static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
590{
591 if (mask) {
592 /* ovs-lock is required to protect mask-refcount and
593 * mask list.
594 */
595 ASSERT_OVSL();
596 BUG_ON(!mask->ref_count);
597 mask->ref_count--;
598
599 if (!mask->ref_count) {
600 list_del_rcu(&mask->list);
601 kfree_rcu(mask, rcu);
602 }
603 }
604}
605
606/* Must be called with OVS mutex held. */
Pravin B Shelare6445712013-10-03 18:16:47 -0700607void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
608{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700609 struct table_instance *ti = ovsl_dereference(table->ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800610 struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700611
Pravin B Shelare6445712013-10-03 18:16:47 -0700612 BUG_ON(table->count == 0);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800613 hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
Pravin B Shelare6445712013-10-03 18:16:47 -0700614 table->count--;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800615 if (ovs_identifier_is_ufid(&flow->id)) {
616 hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
617 table->ufid_count--;
618 }
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700619
620 /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
621 * accessible as long as the RCU read lock is held.
622 */
623 flow_mask_remove(table, flow->mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700624}
625
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700626static struct sw_flow_mask *mask_alloc(void)
Pravin B Shelare6445712013-10-03 18:16:47 -0700627{
628 struct sw_flow_mask *mask;
629
630 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
631 if (mask)
Andy Zhoue80857c2014-01-21 09:31:04 -0800632 mask->ref_count = 1;
Pravin B Shelare6445712013-10-03 18:16:47 -0700633
634 return mask;
635}
636
Pravin B Shelare6445712013-10-03 18:16:47 -0700637static bool mask_equal(const struct sw_flow_mask *a,
638 const struct sw_flow_mask *b)
639{
Daniele Di Proietto70851302014-01-23 10:56:49 -0800640 const u8 *a_ = (const u8 *)&a->key + a->range.start;
641 const u8 *b_ = (const u8 *)&b->key + b->range.start;
Pravin B Shelare6445712013-10-03 18:16:47 -0700642
643 return (a->range.end == b->range.end)
644 && (a->range.start == b->range.start)
645 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
646}
647
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700648static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700649 const struct sw_flow_mask *mask)
650{
651 struct list_head *ml;
652
Pravin B Shelarb637e492013-10-04 00:14:23 -0700653 list_for_each(ml, &tbl->mask_list) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700654 struct sw_flow_mask *m;
655 m = container_of(ml, struct sw_flow_mask, list);
656 if (mask_equal(mask, m))
657 return m;
658 }
659
660 return NULL;
661}
662
Ben Pfaffd1211902013-11-25 10:40:51 -0800663/* Add 'mask' into the mask list, if it is not already there. */
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700664static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800665 const struct sw_flow_mask *new)
Pravin B Shelare6445712013-10-03 18:16:47 -0700666{
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700667 struct sw_flow_mask *mask;
668 mask = flow_mask_find(tbl, new);
669 if (!mask) {
670 /* Allocate a new mask if none exsits. */
671 mask = mask_alloc();
672 if (!mask)
673 return -ENOMEM;
674 mask->key = new->key;
675 mask->range = new->range;
676 list_add_rcu(&mask->list, &tbl->mask_list);
Andy Zhoue80857c2014-01-21 09:31:04 -0800677 } else {
678 BUG_ON(!mask->ref_count);
679 mask->ref_count++;
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700680 }
681
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700682 flow->mask = mask;
683 return 0;
684}
685
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700686/* Must be called with OVS mutex held. */
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800687static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700688{
689 struct table_instance *new_ti = NULL;
690 struct table_instance *ti;
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700691
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800692 flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700693 ti = ovsl_dereference(table->ti);
694 table_instance_insert(ti, flow);
695 table->count++;
696
697 /* Expand table, if necessary, to make room. */
698 if (table->count > ti->n_buckets)
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800699 new_ti = table_instance_expand(ti, false);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700700 else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800701 new_ti = table_instance_rehash(ti, ti->n_buckets, false);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700702
703 if (new_ti) {
704 rcu_assign_pointer(table->ti, new_ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800705 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700706 table->last_rehash = jiffies;
707 }
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800708}
709
710/* Must be called with OVS mutex held. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800711static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
712{
713 struct table_instance *ti;
714
715 flow->ufid_table.hash = ufid_hash(&flow->id);
716 ti = ovsl_dereference(table->ufid_ti);
717 ufid_table_instance_insert(ti, flow);
718 table->ufid_count++;
719
720 /* Expand table, if necessary, to make room. */
721 if (table->ufid_count > ti->n_buckets) {
722 struct table_instance *new_ti;
723
724 new_ti = table_instance_expand(ti, true);
725 if (new_ti) {
726 rcu_assign_pointer(table->ufid_ti, new_ti);
727 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
728 }
729 }
730}
731
732/* Must be called with OVS mutex held. */
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800733int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
734 const struct sw_flow_mask *mask)
735{
736 int err;
737
738 err = flow_mask_insert(table, flow, mask);
739 if (err)
740 return err;
741 flow_key_insert(table, flow);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800742 if (ovs_identifier_is_ufid(&flow->id))
743 flow_ufid_insert(table, flow);
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800744
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700745 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700746}
747
748/* Initializes the flow module.
749 * Returns zero if successful or a negative error code. */
750int ovs_flow_init(void)
751{
752 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
753 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
754
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700755 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
756 + (num_possible_nodes()
757 * sizeof(struct flow_stats *)),
758 0, 0, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700759 if (flow_cache == NULL)
760 return -ENOMEM;
761
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700762 flow_stats_cache
763 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
764 0, SLAB_HWCACHE_ALIGN, NULL);
765 if (flow_stats_cache == NULL) {
766 kmem_cache_destroy(flow_cache);
767 flow_cache = NULL;
768 return -ENOMEM;
769 }
770
Pravin B Shelare6445712013-10-03 18:16:47 -0700771 return 0;
772}
773
774/* Uninitializes the flow module. */
775void ovs_flow_exit(void)
776{
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700777 kmem_cache_destroy(flow_stats_cache);
Pravin B Shelare6445712013-10-03 18:16:47 -0700778 kmem_cache_destroy(flow_cache);
779}