blob: 5e57628e6584d59f6e3428f34b914c4594227ed9 [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,
59 const struct sw_flow_mask *mask)
60{
Daniele Di Proietto70851302014-01-23 10:56:49 -080061 const long *m = (const long *)((const u8 *)&mask->key +
62 mask->range.start);
63 const long *s = (const long *)((const u8 *)src +
64 mask->range.start);
Pravin B Shelare6445712013-10-03 18:16:47 -070065 long *d = (long *)((u8 *)dst + mask->range.start);
66 int i;
67
68 /* The memory outside of the 'mask->range' are not set since
69 * further operations on 'dst' only uses contents within
70 * 'mask->range'.
71 */
72 for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
73 *d++ = *s++ & *m++;
74}
75
Jarno Rajahalme23dabf82014-03-27 12:35:23 -070076struct sw_flow *ovs_flow_alloc(void)
Pravin B Shelare6445712013-10-03 18:16:47 -070077{
78 struct sw_flow *flow;
Jarno Rajahalme63e79592014-03-27 12:42:54 -070079 struct flow_stats *stats;
80 int node;
Pravin B Shelare6445712013-10-03 18:16:47 -070081
82 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
83 if (!flow)
84 return ERR_PTR(-ENOMEM);
85
Pravin B Shelare6445712013-10-03 18:16:47 -070086 flow->sf_acts = NULL;
87 flow->mask = NULL;
Jarno Rajahalme63e79592014-03-27 12:42:54 -070088 flow->stats_last_writer = NUMA_NO_NODE;
Pravin B Shelare6445712013-10-03 18:16:47 -070089
Jarno Rajahalme63e79592014-03-27 12:42:54 -070090 /* Initialize the default stat node. */
91 stats = kmem_cache_alloc_node(flow_stats_cache,
92 GFP_KERNEL | __GFP_ZERO, 0);
93 if (!stats)
Jarno Rajahalme23dabf82014-03-27 12:35:23 -070094 goto err;
Pravin B Shelare298e502013-10-29 17:22:21 -070095
Jarno Rajahalme63e79592014-03-27 12:42:54 -070096 spin_lock_init(&stats->lock);
Pravin B Shelare298e502013-10-29 17:22:21 -070097
Jarno Rajahalme63e79592014-03-27 12:42:54 -070098 RCU_INIT_POINTER(flow->stats[0], stats);
99
100 for_each_node(node)
101 if (node != 0)
102 RCU_INIT_POINTER(flow->stats[node], NULL);
103
Pravin B Shelare6445712013-10-03 18:16:47 -0700104 return flow;
Pravin B Shelare298e502013-10-29 17:22:21 -0700105err:
Wei Yongjunece37c82014-01-08 18:13:14 +0800106 kmem_cache_free(flow_cache, flow);
Pravin B Shelare298e502013-10-29 17:22:21 -0700107 return ERR_PTR(-ENOMEM);
Pravin B Shelare6445712013-10-03 18:16:47 -0700108}
109
Thomas Graf12eb18f2014-11-06 06:58:52 -0800110int ovs_flow_tbl_count(const struct flow_table *table)
Pravin B Shelarb637e492013-10-04 00:14:23 -0700111{
112 return table->count;
113}
114
Pravin B Shelare6445712013-10-03 18:16:47 -0700115static struct flex_array *alloc_buckets(unsigned int n_buckets)
116{
117 struct flex_array *buckets;
118 int i, err;
119
120 buckets = flex_array_alloc(sizeof(struct hlist_head),
121 n_buckets, GFP_KERNEL);
122 if (!buckets)
123 return NULL;
124
125 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
126 if (err) {
127 flex_array_free(buckets);
128 return NULL;
129 }
130
131 for (i = 0; i < n_buckets; i++)
132 INIT_HLIST_HEAD((struct hlist_head *)
133 flex_array_get(buckets, i));
134
135 return buckets;
136}
137
138static void flow_free(struct sw_flow *flow)
139{
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700140 int node;
141
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800142 if (ovs_identifier_is_key(&flow->id))
143 kfree(flow->id.unmasked_key);
Jarno Rajahalmeeb072652014-05-05 14:15:18 -0700144 kfree((struct sw_flow_actions __force *)flow->sf_acts);
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700145 for_each_node(node)
146 if (flow->stats[node])
147 kmem_cache_free(flow_stats_cache,
148 (struct flow_stats __force *)flow->stats[node]);
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
Pravin B Shelare6445712013-10-03 18:16:47 -0700164 if (deferred)
165 call_rcu(&flow->rcu, rcu_free_flow_callback);
166 else
167 flow_free(flow);
168}
169
170static void free_buckets(struct flex_array *buckets)
171{
172 flex_array_free(buckets);
173}
174
Andy Zhoue80857c2014-01-21 09:31:04 -0800175
Pravin B Shelarb637e492013-10-04 00:14:23 -0700176static void __table_instance_destroy(struct table_instance *ti)
Pravin B Shelare6445712013-10-03 18:16:47 -0700177{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700178 free_buckets(ti->buckets);
179 kfree(ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700180}
181
Pravin B Shelarb637e492013-10-04 00:14:23 -0700182static struct table_instance *table_instance_alloc(int new_size)
Pravin B Shelare6445712013-10-03 18:16:47 -0700183{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700184 struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700185
Pravin B Shelarb637e492013-10-04 00:14:23 -0700186 if (!ti)
Pravin B Shelare6445712013-10-03 18:16:47 -0700187 return NULL;
188
Pravin B Shelarb637e492013-10-04 00:14:23 -0700189 ti->buckets = alloc_buckets(new_size);
Pravin B Shelare6445712013-10-03 18:16:47 -0700190
Pravin B Shelarb637e492013-10-04 00:14:23 -0700191 if (!ti->buckets) {
192 kfree(ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700193 return NULL;
194 }
Pravin B Shelarb637e492013-10-04 00:14:23 -0700195 ti->n_buckets = new_size;
196 ti->node_ver = 0;
197 ti->keep_flows = false;
198 get_random_bytes(&ti->hash_seed, sizeof(u32));
199
200 return ti;
201}
202
203int ovs_flow_tbl_init(struct flow_table *table)
204{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800205 struct table_instance *ti, *ufid_ti;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700206
207 ti = table_instance_alloc(TBL_MIN_BUCKETS);
208
209 if (!ti)
210 return -ENOMEM;
211
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800212 ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
213 if (!ufid_ti)
214 goto free_ti;
215
Pravin B Shelarb637e492013-10-04 00:14:23 -0700216 rcu_assign_pointer(table->ti, ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800217 rcu_assign_pointer(table->ufid_ti, ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700218 INIT_LIST_HEAD(&table->mask_list);
219 table->last_rehash = jiffies;
Pravin B Shelare6445712013-10-03 18:16:47 -0700220 table->count = 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800221 table->ufid_count = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700222 return 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800223
224free_ti:
225 __table_instance_destroy(ti);
226 return -ENOMEM;
Pravin B Shelare6445712013-10-03 18:16:47 -0700227}
228
229static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
230{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700231 struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -0700232
Pravin B Shelarb637e492013-10-04 00:14:23 -0700233 __table_instance_destroy(ti);
234}
235
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800236static void table_instance_destroy(struct table_instance *ti,
237 struct table_instance *ufid_ti,
238 bool deferred)
Pravin B Shelarb637e492013-10-04 00:14:23 -0700239{
Andy Zhoue80857c2014-01-21 09:31:04 -0800240 int i;
241
Pravin B Shelarb637e492013-10-04 00:14:23 -0700242 if (!ti)
243 return;
244
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800245 BUG_ON(!ufid_ti);
Andy Zhoue80857c2014-01-21 09:31:04 -0800246 if (ti->keep_flows)
247 goto skip_flows;
248
249 for (i = 0; i < ti->n_buckets; i++) {
250 struct sw_flow *flow;
251 struct hlist_head *head = flex_array_get(ti->buckets, i);
252 struct hlist_node *n;
253 int ver = ti->node_ver;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800254 int ufid_ver = ufid_ti->node_ver;
Andy Zhoue80857c2014-01-21 09:31:04 -0800255
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800256 hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
257 hlist_del_rcu(&flow->flow_table.node[ver]);
258 if (ovs_identifier_is_ufid(&flow->id))
259 hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
Andy Zhoue80857c2014-01-21 09:31:04 -0800260 ovs_flow_free(flow, deferred);
261 }
262 }
263
264skip_flows:
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800265 if (deferred) {
Pravin B Shelarb637e492013-10-04 00:14:23 -0700266 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800267 call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
268 } else {
Pravin B Shelarb637e492013-10-04 00:14:23 -0700269 __table_instance_destroy(ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800270 __table_instance_destroy(ufid_ti);
271 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700272}
273
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700274/* No need for locking this function is called from RCU callback or
275 * error path.
276 */
277void ovs_flow_tbl_destroy(struct flow_table *table)
Pravin B Shelare6445712013-10-03 18:16:47 -0700278{
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700279 struct table_instance *ti = rcu_dereference_raw(table->ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800280 struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700281
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800282 table_instance_destroy(ti, ufid_ti, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700283}
284
Pravin B Shelarb637e492013-10-04 00:14:23 -0700285struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
Pravin B Shelare6445712013-10-03 18:16:47 -0700286 u32 *bucket, u32 *last)
287{
288 struct sw_flow *flow;
289 struct hlist_head *head;
290 int ver;
291 int i;
292
Pravin B Shelarb637e492013-10-04 00:14:23 -0700293 ver = ti->node_ver;
294 while (*bucket < ti->n_buckets) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700295 i = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700296 head = flex_array_get(ti->buckets, *bucket);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800297 hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700298 if (i < *last) {
299 i++;
300 continue;
301 }
302 *last = i + 1;
303 return flow;
304 }
305 (*bucket)++;
306 *last = 0;
307 }
308
309 return NULL;
310}
311
Pravin B Shelarb637e492013-10-04 00:14:23 -0700312static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
Pravin B Shelare6445712013-10-03 18:16:47 -0700313{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700314 hash = jhash_1word(hash, ti->hash_seed);
315 return flex_array_get(ti->buckets,
316 (hash & (ti->n_buckets - 1)));
Pravin B Shelare6445712013-10-03 18:16:47 -0700317}
318
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800319static void table_instance_insert(struct table_instance *ti,
320 struct sw_flow *flow)
Pravin B Shelare6445712013-10-03 18:16:47 -0700321{
322 struct hlist_head *head;
323
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800324 head = find_bucket(ti, flow->flow_table.hash);
325 hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
326}
327
328static void ufid_table_instance_insert(struct table_instance *ti,
329 struct sw_flow *flow)
330{
331 struct hlist_head *head;
332
333 head = find_bucket(ti, flow->ufid_table.hash);
334 hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
Pravin B Shelare6445712013-10-03 18:16:47 -0700335}
336
Pravin B Shelarb637e492013-10-04 00:14:23 -0700337static void flow_table_copy_flows(struct table_instance *old,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800338 struct table_instance *new, bool ufid)
Pravin B Shelare6445712013-10-03 18:16:47 -0700339{
340 int old_ver;
341 int i;
342
343 old_ver = old->node_ver;
344 new->node_ver = !old_ver;
345
346 /* Insert in new table. */
347 for (i = 0; i < old->n_buckets; i++) {
348 struct sw_flow *flow;
349 struct hlist_head *head;
350
351 head = flex_array_get(old->buckets, i);
352
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800353 if (ufid)
354 hlist_for_each_entry(flow, head,
355 ufid_table.node[old_ver])
356 ufid_table_instance_insert(new, flow);
357 else
358 hlist_for_each_entry(flow, head,
359 flow_table.node[old_ver])
360 table_instance_insert(new, flow);
Pravin B Shelare6445712013-10-03 18:16:47 -0700361 }
362
Pravin B Shelare6445712013-10-03 18:16:47 -0700363 old->keep_flows = true;
364}
365
Pravin B Shelarb637e492013-10-04 00:14:23 -0700366static struct table_instance *table_instance_rehash(struct table_instance *ti,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800367 int n_buckets, bool ufid)
Pravin B Shelare6445712013-10-03 18:16:47 -0700368{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700369 struct table_instance *new_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700370
Pravin B Shelarb637e492013-10-04 00:14:23 -0700371 new_ti = table_instance_alloc(n_buckets);
372 if (!new_ti)
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700373 return NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700374
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800375 flow_table_copy_flows(ti, new_ti, ufid);
Pravin B Shelare6445712013-10-03 18:16:47 -0700376
Pravin B Shelarb637e492013-10-04 00:14:23 -0700377 return new_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700378}
379
Pravin B Shelarb637e492013-10-04 00:14:23 -0700380int ovs_flow_tbl_flush(struct flow_table *flow_table)
Pravin B Shelare6445712013-10-03 18:16:47 -0700381{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800382 struct table_instance *old_ti, *new_ti;
383 struct table_instance *old_ufid_ti, *new_ufid_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700384
Pravin B Shelarb637e492013-10-04 00:14:23 -0700385 new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
386 if (!new_ti)
387 return -ENOMEM;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800388 new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
389 if (!new_ufid_ti)
390 goto err_free_ti;
391
392 old_ti = ovsl_dereference(flow_table->ti);
393 old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700394
395 rcu_assign_pointer(flow_table->ti, new_ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800396 rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700397 flow_table->last_rehash = jiffies;
398 flow_table->count = 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800399 flow_table->ufid_count = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700400
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800401 table_instance_destroy(old_ti, old_ufid_ti, true);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700402 return 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800403
404err_free_ti:
405 __table_instance_destroy(new_ti);
406 return -ENOMEM;
Pravin B Shelare6445712013-10-03 18:16:47 -0700407}
408
Joe Stringer272c2cf2015-01-21 16:42:50 -0800409static u32 flow_hash(const struct sw_flow_key *key,
410 const struct sw_flow_key_range *range)
Pravin B Shelare6445712013-10-03 18:16:47 -0700411{
Joe Stringer272c2cf2015-01-21 16:42:50 -0800412 int key_start = range->start;
413 int key_end = range->end;
Daniele Di Proietto70851302014-01-23 10:56:49 -0800414 const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
Pravin B Shelare6445712013-10-03 18:16:47 -0700415 int hash_u32s = (key_end - key_start) >> 2;
416
417 /* Make sure number of hash bytes are multiple of u32. */
418 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
419
Daniel Borkmann87545892014-12-10 16:33:11 +0100420 return jhash2(hash_key, hash_u32s, 0);
Pravin B Shelare6445712013-10-03 18:16:47 -0700421}
422
423static int flow_key_start(const struct sw_flow_key *key)
424{
425 if (key->tun_key.ipv4_dst)
426 return 0;
427 else
428 return rounddown(offsetof(struct sw_flow_key, phy),
429 sizeof(long));
430}
431
432static bool cmp_key(const struct sw_flow_key *key1,
433 const struct sw_flow_key *key2,
434 int key_start, int key_end)
435{
Daniele Di Proietto70851302014-01-23 10:56:49 -0800436 const long *cp1 = (const long *)((const u8 *)key1 + key_start);
437 const long *cp2 = (const long *)((const u8 *)key2 + key_start);
Pravin B Shelare6445712013-10-03 18:16:47 -0700438 long diffs = 0;
439 int i;
440
441 for (i = key_start; i < key_end; i += sizeof(long))
442 diffs |= *cp1++ ^ *cp2++;
443
444 return diffs == 0;
445}
446
447static bool flow_cmp_masked_key(const struct sw_flow *flow,
448 const struct sw_flow_key *key,
Joe Stringer272c2cf2015-01-21 16:42:50 -0800449 const struct sw_flow_key_range *range)
Pravin B Shelare6445712013-10-03 18:16:47 -0700450{
Joe Stringer272c2cf2015-01-21 16:42:50 -0800451 return cmp_key(&flow->key, key, range->start, range->end);
Pravin B Shelare6445712013-10-03 18:16:47 -0700452}
453
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800454static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
455 const struct sw_flow_match *match)
Pravin B Shelare6445712013-10-03 18:16:47 -0700456{
457 struct sw_flow_key *key = match->key;
458 int key_start = flow_key_start(key);
459 int key_end = match->range.end;
460
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800461 BUG_ON(ovs_identifier_is_ufid(&flow->id));
462 return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
Pravin B Shelare6445712013-10-03 18:16:47 -0700463}
464
Pravin B Shelarb637e492013-10-04 00:14:23 -0700465static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
Pravin B Shelare6445712013-10-03 18:16:47 -0700466 const struct sw_flow_key *unmasked,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800467 const struct sw_flow_mask *mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700468{
469 struct sw_flow *flow;
470 struct hlist_head *head;
Pravin B Shelare6445712013-10-03 18:16:47 -0700471 u32 hash;
472 struct sw_flow_key masked_key;
473
474 ovs_flow_mask_key(&masked_key, unmasked, mask);
Joe Stringer272c2cf2015-01-21 16:42:50 -0800475 hash = flow_hash(&masked_key, &mask->range);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700476 head = find_bucket(ti, hash);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800477 hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
478 if (flow->mask == mask && flow->flow_table.hash == hash &&
Joe Stringer272c2cf2015-01-21 16:42:50 -0800479 flow_cmp_masked_key(flow, &masked_key, &mask->range))
Pravin B Shelare6445712013-10-03 18:16:47 -0700480 return flow;
481 }
482 return NULL;
483}
484
Andy Zhou5bb50632013-11-25 10:42:46 -0800485struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
Andy Zhou1bd71162013-10-22 10:42:46 -0700486 const struct sw_flow_key *key,
487 u32 *n_mask_hit)
Pravin B Shelare6445712013-10-03 18:16:47 -0700488{
Jesse Gross663efa32013-12-03 10:58:53 -0800489 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700490 struct sw_flow_mask *mask;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700491 struct sw_flow *flow;
Pravin B Shelare6445712013-10-03 18:16:47 -0700492
Andy Zhou1bd71162013-10-22 10:42:46 -0700493 *n_mask_hit = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700494 list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
Andy Zhou1bd71162013-10-22 10:42:46 -0700495 (*n_mask_hit)++;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700496 flow = masked_flow_lookup(ti, key, mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700497 if (flow) /* Found */
Pravin B Shelarb637e492013-10-04 00:14:23 -0700498 return flow;
Pravin B Shelare6445712013-10-03 18:16:47 -0700499 }
Pravin B Shelarb637e492013-10-04 00:14:23 -0700500 return NULL;
501}
Pravin B Shelare6445712013-10-03 18:16:47 -0700502
Andy Zhou5bb50632013-11-25 10:42:46 -0800503struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
504 const struct sw_flow_key *key)
505{
506 u32 __always_unused n_mask_hit;
507
508 return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
509}
510
Alex Wang4a46b242014-06-30 20:30:29 -0700511struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800512 const struct sw_flow_match *match)
Alex Wang4a46b242014-06-30 20:30:29 -0700513{
514 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
515 struct sw_flow_mask *mask;
516 struct sw_flow *flow;
517
518 /* Always called under ovs-mutex. */
519 list_for_each_entry(mask, &tbl->mask_list, list) {
520 flow = masked_flow_lookup(ti, match->key, mask);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800521 if (flow && ovs_identifier_is_key(&flow->id) &&
522 ovs_flow_cmp_unmasked_key(flow, match))
523 return flow;
524 }
525 return NULL;
526}
527
528static u32 ufid_hash(const struct sw_flow_id *sfid)
529{
530 return jhash(sfid->ufid, sfid->ufid_len, 0);
531}
532
533static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
534 const struct sw_flow_id *sfid)
535{
536 if (flow->id.ufid_len != sfid->ufid_len)
537 return false;
538
539 return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
540}
541
542bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
543{
544 if (ovs_identifier_is_ufid(&flow->id))
545 return flow_cmp_masked_key(flow, match->key, &match->range);
546
547 return ovs_flow_cmp_unmasked_key(flow, match);
548}
549
550struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
551 const struct sw_flow_id *ufid)
552{
553 struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
554 struct sw_flow *flow;
555 struct hlist_head *head;
556 u32 hash;
557
558 hash = ufid_hash(ufid);
559 head = find_bucket(ti, hash);
560 hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
561 if (flow->ufid_table.hash == hash &&
562 ovs_flow_cmp_ufid(flow, ufid))
Alex Wang4a46b242014-06-30 20:30:29 -0700563 return flow;
564 }
565 return NULL;
566}
567
Andy Zhou1bd71162013-10-22 10:42:46 -0700568int ovs_flow_tbl_num_masks(const struct flow_table *table)
569{
570 struct sw_flow_mask *mask;
571 int num = 0;
572
573 list_for_each_entry(mask, &table->mask_list, list)
574 num++;
575
576 return num;
577}
578
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800579static struct table_instance *table_instance_expand(struct table_instance *ti,
580 bool ufid)
Pravin B Shelarb637e492013-10-04 00:14:23 -0700581{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800582 return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
Pravin B Shelare6445712013-10-03 18:16:47 -0700583}
584
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700585/* Remove 'mask' from the mask list, if it is not needed any more. */
586static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
587{
588 if (mask) {
589 /* ovs-lock is required to protect mask-refcount and
590 * mask list.
591 */
592 ASSERT_OVSL();
593 BUG_ON(!mask->ref_count);
594 mask->ref_count--;
595
596 if (!mask->ref_count) {
597 list_del_rcu(&mask->list);
598 kfree_rcu(mask, rcu);
599 }
600 }
601}
602
603/* Must be called with OVS mutex held. */
Pravin B Shelare6445712013-10-03 18:16:47 -0700604void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
605{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700606 struct table_instance *ti = ovsl_dereference(table->ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800607 struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700608
Pravin B Shelare6445712013-10-03 18:16:47 -0700609 BUG_ON(table->count == 0);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800610 hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
Pravin B Shelare6445712013-10-03 18:16:47 -0700611 table->count--;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800612 if (ovs_identifier_is_ufid(&flow->id)) {
613 hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
614 table->ufid_count--;
615 }
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700616
617 /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
618 * accessible as long as the RCU read lock is held.
619 */
620 flow_mask_remove(table, flow->mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700621}
622
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700623static struct sw_flow_mask *mask_alloc(void)
Pravin B Shelare6445712013-10-03 18:16:47 -0700624{
625 struct sw_flow_mask *mask;
626
627 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
628 if (mask)
Andy Zhoue80857c2014-01-21 09:31:04 -0800629 mask->ref_count = 1;
Pravin B Shelare6445712013-10-03 18:16:47 -0700630
631 return mask;
632}
633
Pravin B Shelare6445712013-10-03 18:16:47 -0700634static bool mask_equal(const struct sw_flow_mask *a,
635 const struct sw_flow_mask *b)
636{
Daniele Di Proietto70851302014-01-23 10:56:49 -0800637 const u8 *a_ = (const u8 *)&a->key + a->range.start;
638 const u8 *b_ = (const u8 *)&b->key + b->range.start;
Pravin B Shelare6445712013-10-03 18:16:47 -0700639
640 return (a->range.end == b->range.end)
641 && (a->range.start == b->range.start)
642 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
643}
644
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700645static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700646 const struct sw_flow_mask *mask)
647{
648 struct list_head *ml;
649
Pravin B Shelarb637e492013-10-04 00:14:23 -0700650 list_for_each(ml, &tbl->mask_list) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700651 struct sw_flow_mask *m;
652 m = container_of(ml, struct sw_flow_mask, list);
653 if (mask_equal(mask, m))
654 return m;
655 }
656
657 return NULL;
658}
659
Ben Pfaffd1211902013-11-25 10:40:51 -0800660/* Add 'mask' into the mask list, if it is not already there. */
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700661static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800662 const struct sw_flow_mask *new)
Pravin B Shelare6445712013-10-03 18:16:47 -0700663{
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700664 struct sw_flow_mask *mask;
665 mask = flow_mask_find(tbl, new);
666 if (!mask) {
667 /* Allocate a new mask if none exsits. */
668 mask = mask_alloc();
669 if (!mask)
670 return -ENOMEM;
671 mask->key = new->key;
672 mask->range = new->range;
673 list_add_rcu(&mask->list, &tbl->mask_list);
Andy Zhoue80857c2014-01-21 09:31:04 -0800674 } else {
675 BUG_ON(!mask->ref_count);
676 mask->ref_count++;
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700677 }
678
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700679 flow->mask = mask;
680 return 0;
681}
682
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700683/* Must be called with OVS mutex held. */
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800684static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700685{
686 struct table_instance *new_ti = NULL;
687 struct table_instance *ti;
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700688
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800689 flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700690 ti = ovsl_dereference(table->ti);
691 table_instance_insert(ti, flow);
692 table->count++;
693
694 /* Expand table, if necessary, to make room. */
695 if (table->count > ti->n_buckets)
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800696 new_ti = table_instance_expand(ti, false);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700697 else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800698 new_ti = table_instance_rehash(ti, ti->n_buckets, false);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700699
700 if (new_ti) {
701 rcu_assign_pointer(table->ti, new_ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800702 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700703 table->last_rehash = jiffies;
704 }
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800705}
706
707/* Must be called with OVS mutex held. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800708static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
709{
710 struct table_instance *ti;
711
712 flow->ufid_table.hash = ufid_hash(&flow->id);
713 ti = ovsl_dereference(table->ufid_ti);
714 ufid_table_instance_insert(ti, flow);
715 table->ufid_count++;
716
717 /* Expand table, if necessary, to make room. */
718 if (table->ufid_count > ti->n_buckets) {
719 struct table_instance *new_ti;
720
721 new_ti = table_instance_expand(ti, true);
722 if (new_ti) {
723 rcu_assign_pointer(table->ufid_ti, new_ti);
724 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
725 }
726 }
727}
728
729/* Must be called with OVS mutex held. */
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800730int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
731 const struct sw_flow_mask *mask)
732{
733 int err;
734
735 err = flow_mask_insert(table, flow, mask);
736 if (err)
737 return err;
738 flow_key_insert(table, flow);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800739 if (ovs_identifier_is_ufid(&flow->id))
740 flow_ufid_insert(table, flow);
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800741
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700742 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700743}
744
745/* Initializes the flow module.
746 * Returns zero if successful or a negative error code. */
747int ovs_flow_init(void)
748{
749 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
750 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
751
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700752 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
753 + (num_possible_nodes()
754 * sizeof(struct flow_stats *)),
755 0, 0, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700756 if (flow_cache == NULL)
757 return -ENOMEM;
758
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700759 flow_stats_cache
760 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
761 0, SLAB_HWCACHE_ALIGN, NULL);
762 if (flow_stats_cache == NULL) {
763 kmem_cache_destroy(flow_cache);
764 flow_cache = NULL;
765 return -ENOMEM;
766 }
767
Pravin B Shelare6445712013-10-03 18:16:47 -0700768 return 0;
769}
770
771/* Uninitializes the flow module. */
772void ovs_flow_exit(void)
773{
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700774 kmem_cache_destroy(flow_stats_cache);
Pravin B Shelare6445712013-10-03 18:16:47 -0700775 kmem_cache_destroy(flow_cache);
776}