blob: d073fff82fdb8c6c8d39b57690d37eedeb12423a [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"
Thomas Graf34ae9322015-07-21 10:44:03 +020021#include "flow_netlink.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070022#include <linux/uaccess.h>
23#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
25#include <linux/if_ether.h>
26#include <linux/if_vlan.h>
27#include <net/llc_pdu.h>
28#include <linux/kernel.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010029#include <linux/jhash.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070030#include <linux/jiffies.h>
31#include <linux/llc.h>
32#include <linux/module.h>
33#include <linux/in.h>
34#include <linux/rcupdate.h>
35#include <linux/if_arp.h>
36#include <linux/ip.h>
37#include <linux/ipv6.h>
38#include <linux/sctp.h>
39#include <linux/tcp.h>
40#include <linux/udp.h>
41#include <linux/icmp.h>
42#include <linux/icmpv6.h>
43#include <linux/rculist.h>
44#include <net/ip.h>
45#include <net/ipv6.h>
46#include <net/ndisc.h>
47
Pravin B Shelarb637e492013-10-04 00:14:23 -070048#define TBL_MIN_BUCKETS 1024
49#define REHASH_INTERVAL (10 * 60 * HZ)
50
Pravin B Shelare6445712013-10-03 18:16:47 -070051static struct kmem_cache *flow_cache;
Jarno Rajahalme63e79592014-03-27 12:42:54 -070052struct kmem_cache *flow_stats_cache __read_mostly;
Pravin B Shelare6445712013-10-03 18:16:47 -070053
54static u16 range_n_bytes(const struct sw_flow_key_range *range)
55{
56 return range->end - range->start;
57}
58
59void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
Jesse Grossae5f2fb2015-09-21 20:21:20 -070060 bool full, const struct sw_flow_mask *mask)
Pravin B Shelare6445712013-10-03 18:16:47 -070061{
Jesse Grossae5f2fb2015-09-21 20:21:20 -070062 int start = full ? 0 : mask->range.start;
63 int len = full ? sizeof *dst : range_n_bytes(&mask->range);
64 const long *m = (const long *)((const u8 *)&mask->key + start);
65 const long *s = (const long *)((const u8 *)src + start);
66 long *d = (long *)((u8 *)dst + start);
Pravin B Shelare6445712013-10-03 18:16:47 -070067 int i;
68
Jesse Grossae5f2fb2015-09-21 20:21:20 -070069 /* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
70 * if 'full' is false the memory outside of the 'mask->range' is left
71 * uninitialized. This can be used as an optimization when further
72 * operations on 'dst' only use contents within 'mask->range'.
Pravin B Shelare6445712013-10-03 18:16:47 -070073 */
Jesse Grossae5f2fb2015-09-21 20:21:20 -070074 for (i = 0; i < len; i += sizeof(long))
Pravin B Shelare6445712013-10-03 18:16:47 -070075 *d++ = *s++ & *m++;
76}
77
Jarno Rajahalme23dabf82014-03-27 12:35:23 -070078struct sw_flow *ovs_flow_alloc(void)
Pravin B Shelare6445712013-10-03 18:16:47 -070079{
80 struct sw_flow *flow;
Jarno Rajahalme63e79592014-03-27 12:42:54 -070081 struct flow_stats *stats;
82 int node;
Pravin B Shelare6445712013-10-03 18:16:47 -070083
84 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
85 if (!flow)
86 return ERR_PTR(-ENOMEM);
87
Pravin B Shelare6445712013-10-03 18:16:47 -070088 flow->sf_acts = NULL;
89 flow->mask = NULL;
Pravin B Shelarca539342015-02-06 11:17:13 -080090 flow->id.unmasked_key = NULL;
91 flow->id.ufid_len = 0;
Jarno Rajahalme63e79592014-03-27 12:42:54 -070092 flow->stats_last_writer = NUMA_NO_NODE;
Pravin B Shelare6445712013-10-03 18:16:47 -070093
Jarno Rajahalme63e79592014-03-27 12:42:54 -070094 /* Initialize the default stat node. */
95 stats = kmem_cache_alloc_node(flow_stats_cache,
Konstantin Khlebnikov598c12d2015-10-02 13:18:22 +030096 GFP_KERNEL | __GFP_ZERO,
97 node_online(0) ? 0 : NUMA_NO_NODE);
Jarno Rajahalme63e79592014-03-27 12:42:54 -070098 if (!stats)
Jarno Rajahalme23dabf82014-03-27 12:35:23 -070099 goto err;
Pravin B Shelare298e502013-10-29 17:22:21 -0700100
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700101 spin_lock_init(&stats->lock);
Pravin B Shelare298e502013-10-29 17:22:21 -0700102
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700103 RCU_INIT_POINTER(flow->stats[0], stats);
104
105 for_each_node(node)
106 if (node != 0)
107 RCU_INIT_POINTER(flow->stats[node], NULL);
108
Pravin B Shelare6445712013-10-03 18:16:47 -0700109 return flow;
Pravin B Shelare298e502013-10-29 17:22:21 -0700110err:
Wei Yongjunece37c82014-01-08 18:13:14 +0800111 kmem_cache_free(flow_cache, flow);
Pravin B Shelare298e502013-10-29 17:22:21 -0700112 return ERR_PTR(-ENOMEM);
Pravin B Shelare6445712013-10-03 18:16:47 -0700113}
114
Thomas Graf12eb18f2014-11-06 06:58:52 -0800115int ovs_flow_tbl_count(const struct flow_table *table)
Pravin B Shelarb637e492013-10-04 00:14:23 -0700116{
117 return table->count;
118}
119
Pravin B Shelare6445712013-10-03 18:16:47 -0700120static struct flex_array *alloc_buckets(unsigned int n_buckets)
121{
122 struct flex_array *buckets;
123 int i, err;
124
125 buckets = flex_array_alloc(sizeof(struct hlist_head),
126 n_buckets, GFP_KERNEL);
127 if (!buckets)
128 return NULL;
129
130 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
131 if (err) {
132 flex_array_free(buckets);
133 return NULL;
134 }
135
136 for (i = 0; i < n_buckets; i++)
137 INIT_HLIST_HEAD((struct hlist_head *)
138 flex_array_get(buckets, i));
139
140 return buckets;
141}
142
143static void flow_free(struct sw_flow *flow)
144{
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700145 int node;
146
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800147 if (ovs_identifier_is_key(&flow->id))
148 kfree(flow->id.unmasked_key);
Thomas Graf34ae9322015-07-21 10:44:03 +0200149 if (flow->sf_acts)
150 ovs_nla_free_flow_actions((struct sw_flow_actions __force *)flow->sf_acts);
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700151 for_each_node(node)
152 if (flow->stats[node])
153 kmem_cache_free(flow_stats_cache,
154 (struct flow_stats __force *)flow->stats[node]);
Pravin B Shelare6445712013-10-03 18:16:47 -0700155 kmem_cache_free(flow_cache, flow);
156}
157
158static void rcu_free_flow_callback(struct rcu_head *rcu)
159{
160 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
161
162 flow_free(flow);
163}
164
165void ovs_flow_free(struct sw_flow *flow, bool deferred)
166{
167 if (!flow)
168 return;
169
Pravin B Shelare6445712013-10-03 18:16:47 -0700170 if (deferred)
171 call_rcu(&flow->rcu, rcu_free_flow_callback);
172 else
173 flow_free(flow);
174}
175
176static void free_buckets(struct flex_array *buckets)
177{
178 flex_array_free(buckets);
179}
180
Andy Zhoue80857c2014-01-21 09:31:04 -0800181
Pravin B Shelarb637e492013-10-04 00:14:23 -0700182static void __table_instance_destroy(struct table_instance *ti)
Pravin B Shelare6445712013-10-03 18:16:47 -0700183{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700184 free_buckets(ti->buckets);
185 kfree(ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700186}
187
Pravin B Shelarb637e492013-10-04 00:14:23 -0700188static struct table_instance *table_instance_alloc(int new_size)
Pravin B Shelare6445712013-10-03 18:16:47 -0700189{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700190 struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700191
Pravin B Shelarb637e492013-10-04 00:14:23 -0700192 if (!ti)
Pravin B Shelare6445712013-10-03 18:16:47 -0700193 return NULL;
194
Pravin B Shelarb637e492013-10-04 00:14:23 -0700195 ti->buckets = alloc_buckets(new_size);
Pravin B Shelare6445712013-10-03 18:16:47 -0700196
Pravin B Shelarb637e492013-10-04 00:14:23 -0700197 if (!ti->buckets) {
198 kfree(ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700199 return NULL;
200 }
Pravin B Shelarb637e492013-10-04 00:14:23 -0700201 ti->n_buckets = new_size;
202 ti->node_ver = 0;
203 ti->keep_flows = false;
204 get_random_bytes(&ti->hash_seed, sizeof(u32));
205
206 return ti;
207}
208
209int ovs_flow_tbl_init(struct flow_table *table)
210{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800211 struct table_instance *ti, *ufid_ti;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700212
213 ti = table_instance_alloc(TBL_MIN_BUCKETS);
214
215 if (!ti)
216 return -ENOMEM;
217
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800218 ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
219 if (!ufid_ti)
220 goto free_ti;
221
Pravin B Shelarb637e492013-10-04 00:14:23 -0700222 rcu_assign_pointer(table->ti, ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800223 rcu_assign_pointer(table->ufid_ti, ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700224 INIT_LIST_HEAD(&table->mask_list);
225 table->last_rehash = jiffies;
Pravin B Shelare6445712013-10-03 18:16:47 -0700226 table->count = 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800227 table->ufid_count = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700228 return 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800229
230free_ti:
231 __table_instance_destroy(ti);
232 return -ENOMEM;
Pravin B Shelare6445712013-10-03 18:16:47 -0700233}
234
235static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
236{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700237 struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -0700238
Pravin B Shelarb637e492013-10-04 00:14:23 -0700239 __table_instance_destroy(ti);
240}
241
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800242static void table_instance_destroy(struct table_instance *ti,
243 struct table_instance *ufid_ti,
244 bool deferred)
Pravin B Shelarb637e492013-10-04 00:14:23 -0700245{
Andy Zhoue80857c2014-01-21 09:31:04 -0800246 int i;
247
Pravin B Shelarb637e492013-10-04 00:14:23 -0700248 if (!ti)
249 return;
250
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800251 BUG_ON(!ufid_ti);
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;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800260 int ufid_ver = ufid_ti->node_ver;
Andy Zhoue80857c2014-01-21 09:31:04 -0800261
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800262 hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
263 hlist_del_rcu(&flow->flow_table.node[ver]);
264 if (ovs_identifier_is_ufid(&flow->id))
265 hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
Andy Zhoue80857c2014-01-21 09:31:04 -0800266 ovs_flow_free(flow, deferred);
267 }
268 }
269
270skip_flows:
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800271 if (deferred) {
Pravin B Shelarb637e492013-10-04 00:14:23 -0700272 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800273 call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
274 } else {
Pravin B Shelarb637e492013-10-04 00:14:23 -0700275 __table_instance_destroy(ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800276 __table_instance_destroy(ufid_ti);
277 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700278}
279
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700280/* No need for locking this function is called from RCU callback or
281 * error path.
282 */
283void ovs_flow_tbl_destroy(struct flow_table *table)
Pravin B Shelare6445712013-10-03 18:16:47 -0700284{
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700285 struct table_instance *ti = rcu_dereference_raw(table->ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800286 struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700287
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800288 table_instance_destroy(ti, ufid_ti, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700289}
290
Pravin B Shelarb637e492013-10-04 00:14:23 -0700291struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
Pravin B Shelare6445712013-10-03 18:16:47 -0700292 u32 *bucket, u32 *last)
293{
294 struct sw_flow *flow;
295 struct hlist_head *head;
296 int ver;
297 int i;
298
Pravin B Shelarb637e492013-10-04 00:14:23 -0700299 ver = ti->node_ver;
300 while (*bucket < ti->n_buckets) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700301 i = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700302 head = flex_array_get(ti->buckets, *bucket);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800303 hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700304 if (i < *last) {
305 i++;
306 continue;
307 }
308 *last = i + 1;
309 return flow;
310 }
311 (*bucket)++;
312 *last = 0;
313 }
314
315 return NULL;
316}
317
Pravin B Shelarb637e492013-10-04 00:14:23 -0700318static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
Pravin B Shelare6445712013-10-03 18:16:47 -0700319{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700320 hash = jhash_1word(hash, ti->hash_seed);
321 return flex_array_get(ti->buckets,
322 (hash & (ti->n_buckets - 1)));
Pravin B Shelare6445712013-10-03 18:16:47 -0700323}
324
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800325static void table_instance_insert(struct table_instance *ti,
326 struct sw_flow *flow)
Pravin B Shelare6445712013-10-03 18:16:47 -0700327{
328 struct hlist_head *head;
329
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800330 head = find_bucket(ti, flow->flow_table.hash);
331 hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
332}
333
334static void ufid_table_instance_insert(struct table_instance *ti,
335 struct sw_flow *flow)
336{
337 struct hlist_head *head;
338
339 head = find_bucket(ti, flow->ufid_table.hash);
340 hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
Pravin B Shelare6445712013-10-03 18:16:47 -0700341}
342
Pravin B Shelarb637e492013-10-04 00:14:23 -0700343static void flow_table_copy_flows(struct table_instance *old,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800344 struct table_instance *new, bool ufid)
Pravin B Shelare6445712013-10-03 18:16:47 -0700345{
346 int old_ver;
347 int i;
348
349 old_ver = old->node_ver;
350 new->node_ver = !old_ver;
351
352 /* Insert in new table. */
353 for (i = 0; i < old->n_buckets; i++) {
354 struct sw_flow *flow;
355 struct hlist_head *head;
356
357 head = flex_array_get(old->buckets, i);
358
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800359 if (ufid)
360 hlist_for_each_entry(flow, head,
361 ufid_table.node[old_ver])
362 ufid_table_instance_insert(new, flow);
363 else
364 hlist_for_each_entry(flow, head,
365 flow_table.node[old_ver])
366 table_instance_insert(new, flow);
Pravin B Shelare6445712013-10-03 18:16:47 -0700367 }
368
Pravin B Shelare6445712013-10-03 18:16:47 -0700369 old->keep_flows = true;
370}
371
Pravin B Shelarb637e492013-10-04 00:14:23 -0700372static struct table_instance *table_instance_rehash(struct table_instance *ti,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800373 int n_buckets, bool ufid)
Pravin B Shelare6445712013-10-03 18:16:47 -0700374{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700375 struct table_instance *new_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700376
Pravin B Shelarb637e492013-10-04 00:14:23 -0700377 new_ti = table_instance_alloc(n_buckets);
378 if (!new_ti)
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700379 return NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700380
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800381 flow_table_copy_flows(ti, new_ti, ufid);
Pravin B Shelare6445712013-10-03 18:16:47 -0700382
Pravin B Shelarb637e492013-10-04 00:14:23 -0700383 return new_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700384}
385
Pravin B Shelarb637e492013-10-04 00:14:23 -0700386int ovs_flow_tbl_flush(struct flow_table *flow_table)
Pravin B Shelare6445712013-10-03 18:16:47 -0700387{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800388 struct table_instance *old_ti, *new_ti;
389 struct table_instance *old_ufid_ti, *new_ufid_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700390
Pravin B Shelarb637e492013-10-04 00:14:23 -0700391 new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
392 if (!new_ti)
393 return -ENOMEM;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800394 new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
395 if (!new_ufid_ti)
396 goto err_free_ti;
397
398 old_ti = ovsl_dereference(flow_table->ti);
399 old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700400
401 rcu_assign_pointer(flow_table->ti, new_ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800402 rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700403 flow_table->last_rehash = jiffies;
404 flow_table->count = 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800405 flow_table->ufid_count = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700406
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800407 table_instance_destroy(old_ti, old_ufid_ti, true);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700408 return 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800409
410err_free_ti:
411 __table_instance_destroy(new_ti);
412 return -ENOMEM;
Pravin B Shelare6445712013-10-03 18:16:47 -0700413}
414
Joe Stringer272c2cf2015-01-21 16:42:50 -0800415static u32 flow_hash(const struct sw_flow_key *key,
416 const struct sw_flow_key_range *range)
Pravin B Shelare6445712013-10-03 18:16:47 -0700417{
Joe Stringer272c2cf2015-01-21 16:42:50 -0800418 int key_start = range->start;
419 int key_end = range->end;
Daniele Di Proietto70851302014-01-23 10:56:49 -0800420 const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
Pravin B Shelare6445712013-10-03 18:16:47 -0700421 int hash_u32s = (key_end - key_start) >> 2;
422
423 /* Make sure number of hash bytes are multiple of u32. */
424 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
425
Daniel Borkmann87545892014-12-10 16:33:11 +0100426 return jhash2(hash_key, hash_u32s, 0);
Pravin B Shelare6445712013-10-03 18:16:47 -0700427}
428
429static int flow_key_start(const struct sw_flow_key *key)
430{
Jiri Benc00a93ba2015-10-05 13:09:46 +0200431 if (key->tun_proto)
Pravin B Shelare6445712013-10-03 18:16:47 -0700432 return 0;
433 else
434 return rounddown(offsetof(struct sw_flow_key, phy),
435 sizeof(long));
436}
437
438static bool cmp_key(const struct sw_flow_key *key1,
439 const struct sw_flow_key *key2,
440 int key_start, int key_end)
441{
Daniele Di Proietto70851302014-01-23 10:56:49 -0800442 const long *cp1 = (const long *)((const u8 *)key1 + key_start);
443 const long *cp2 = (const long *)((const u8 *)key2 + key_start);
Pravin B Shelare6445712013-10-03 18:16:47 -0700444 long diffs = 0;
445 int i;
446
447 for (i = key_start; i < key_end; i += sizeof(long))
448 diffs |= *cp1++ ^ *cp2++;
449
450 return diffs == 0;
451}
452
453static bool flow_cmp_masked_key(const struct sw_flow *flow,
454 const struct sw_flow_key *key,
Joe Stringer272c2cf2015-01-21 16:42:50 -0800455 const struct sw_flow_key_range *range)
Pravin B Shelare6445712013-10-03 18:16:47 -0700456{
Joe Stringer272c2cf2015-01-21 16:42:50 -0800457 return cmp_key(&flow->key, key, range->start, range->end);
Pravin B Shelare6445712013-10-03 18:16:47 -0700458}
459
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800460static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
461 const struct sw_flow_match *match)
Pravin B Shelare6445712013-10-03 18:16:47 -0700462{
463 struct sw_flow_key *key = match->key;
464 int key_start = flow_key_start(key);
465 int key_end = match->range.end;
466
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800467 BUG_ON(ovs_identifier_is_ufid(&flow->id));
468 return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
Pravin B Shelare6445712013-10-03 18:16:47 -0700469}
470
Pravin B Shelarb637e492013-10-04 00:14:23 -0700471static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
Pravin B Shelare6445712013-10-03 18:16:47 -0700472 const struct sw_flow_key *unmasked,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800473 const struct sw_flow_mask *mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700474{
475 struct sw_flow *flow;
476 struct hlist_head *head;
Pravin B Shelare6445712013-10-03 18:16:47 -0700477 u32 hash;
478 struct sw_flow_key masked_key;
479
Jesse Grossae5f2fb2015-09-21 20:21:20 -0700480 ovs_flow_mask_key(&masked_key, unmasked, false, mask);
Joe Stringer272c2cf2015-01-21 16:42:50 -0800481 hash = flow_hash(&masked_key, &mask->range);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700482 head = find_bucket(ti, hash);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800483 hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
484 if (flow->mask == mask && flow->flow_table.hash == hash &&
Joe Stringer272c2cf2015-01-21 16:42:50 -0800485 flow_cmp_masked_key(flow, &masked_key, &mask->range))
Pravin B Shelare6445712013-10-03 18:16:47 -0700486 return flow;
487 }
488 return NULL;
489}
490
Andy Zhou5bb50632013-11-25 10:42:46 -0800491struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
Andy Zhou1bd71162013-10-22 10:42:46 -0700492 const struct sw_flow_key *key,
493 u32 *n_mask_hit)
Pravin B Shelare6445712013-10-03 18:16:47 -0700494{
Jesse Gross663efa32013-12-03 10:58:53 -0800495 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700496 struct sw_flow_mask *mask;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700497 struct sw_flow *flow;
Pravin B Shelare6445712013-10-03 18:16:47 -0700498
Andy Zhou1bd71162013-10-22 10:42:46 -0700499 *n_mask_hit = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700500 list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
Andy Zhou1bd71162013-10-22 10:42:46 -0700501 (*n_mask_hit)++;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700502 flow = masked_flow_lookup(ti, key, mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700503 if (flow) /* Found */
Pravin B Shelarb637e492013-10-04 00:14:23 -0700504 return flow;
Pravin B Shelare6445712013-10-03 18:16:47 -0700505 }
Pravin B Shelarb637e492013-10-04 00:14:23 -0700506 return NULL;
507}
Pravin B Shelare6445712013-10-03 18:16:47 -0700508
Andy Zhou5bb50632013-11-25 10:42:46 -0800509struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
510 const struct sw_flow_key *key)
511{
512 u32 __always_unused n_mask_hit;
513
514 return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
515}
516
Alex Wang4a46b242014-06-30 20:30:29 -0700517struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800518 const struct sw_flow_match *match)
Alex Wang4a46b242014-06-30 20:30:29 -0700519{
520 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
521 struct sw_flow_mask *mask;
522 struct sw_flow *flow;
523
524 /* Always called under ovs-mutex. */
525 list_for_each_entry(mask, &tbl->mask_list, list) {
526 flow = masked_flow_lookup(ti, match->key, mask);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800527 if (flow && ovs_identifier_is_key(&flow->id) &&
528 ovs_flow_cmp_unmasked_key(flow, match))
529 return flow;
530 }
531 return NULL;
532}
533
534static u32 ufid_hash(const struct sw_flow_id *sfid)
535{
536 return jhash(sfid->ufid, sfid->ufid_len, 0);
537}
538
539static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
540 const struct sw_flow_id *sfid)
541{
542 if (flow->id.ufid_len != sfid->ufid_len)
543 return false;
544
545 return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
546}
547
548bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
549{
550 if (ovs_identifier_is_ufid(&flow->id))
551 return flow_cmp_masked_key(flow, match->key, &match->range);
552
553 return ovs_flow_cmp_unmasked_key(flow, match);
554}
555
556struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
557 const struct sw_flow_id *ufid)
558{
559 struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
560 struct sw_flow *flow;
561 struct hlist_head *head;
562 u32 hash;
563
564 hash = ufid_hash(ufid);
565 head = find_bucket(ti, hash);
566 hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
567 if (flow->ufid_table.hash == hash &&
568 ovs_flow_cmp_ufid(flow, ufid))
Alex Wang4a46b242014-06-30 20:30:29 -0700569 return flow;
570 }
571 return NULL;
572}
573
Andy Zhou1bd71162013-10-22 10:42:46 -0700574int ovs_flow_tbl_num_masks(const struct flow_table *table)
575{
576 struct sw_flow_mask *mask;
577 int num = 0;
578
579 list_for_each_entry(mask, &table->mask_list, list)
580 num++;
581
582 return num;
583}
584
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800585static struct table_instance *table_instance_expand(struct table_instance *ti,
586 bool ufid)
Pravin B Shelarb637e492013-10-04 00:14:23 -0700587{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800588 return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
Pravin B Shelare6445712013-10-03 18:16:47 -0700589}
590
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700591/* Remove 'mask' from the mask list, if it is not needed any more. */
592static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
593{
594 if (mask) {
595 /* ovs-lock is required to protect mask-refcount and
596 * mask list.
597 */
598 ASSERT_OVSL();
599 BUG_ON(!mask->ref_count);
600 mask->ref_count--;
601
602 if (!mask->ref_count) {
603 list_del_rcu(&mask->list);
604 kfree_rcu(mask, rcu);
605 }
606 }
607}
608
609/* Must be called with OVS mutex held. */
Pravin B Shelare6445712013-10-03 18:16:47 -0700610void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
611{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700612 struct table_instance *ti = ovsl_dereference(table->ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800613 struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700614
Pravin B Shelare6445712013-10-03 18:16:47 -0700615 BUG_ON(table->count == 0);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800616 hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
Pravin B Shelare6445712013-10-03 18:16:47 -0700617 table->count--;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800618 if (ovs_identifier_is_ufid(&flow->id)) {
619 hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
620 table->ufid_count--;
621 }
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700622
623 /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
624 * accessible as long as the RCU read lock is held.
625 */
626 flow_mask_remove(table, flow->mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700627}
628
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700629static struct sw_flow_mask *mask_alloc(void)
Pravin B Shelare6445712013-10-03 18:16:47 -0700630{
631 struct sw_flow_mask *mask;
632
633 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
634 if (mask)
Andy Zhoue80857c2014-01-21 09:31:04 -0800635 mask->ref_count = 1;
Pravin B Shelare6445712013-10-03 18:16:47 -0700636
637 return mask;
638}
639
Pravin B Shelare6445712013-10-03 18:16:47 -0700640static bool mask_equal(const struct sw_flow_mask *a,
641 const struct sw_flow_mask *b)
642{
Daniele Di Proietto70851302014-01-23 10:56:49 -0800643 const u8 *a_ = (const u8 *)&a->key + a->range.start;
644 const u8 *b_ = (const u8 *)&b->key + b->range.start;
Pravin B Shelare6445712013-10-03 18:16:47 -0700645
646 return (a->range.end == b->range.end)
647 && (a->range.start == b->range.start)
648 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
649}
650
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700651static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700652 const struct sw_flow_mask *mask)
653{
654 struct list_head *ml;
655
Pravin B Shelarb637e492013-10-04 00:14:23 -0700656 list_for_each(ml, &tbl->mask_list) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700657 struct sw_flow_mask *m;
658 m = container_of(ml, struct sw_flow_mask, list);
659 if (mask_equal(mask, m))
660 return m;
661 }
662
663 return NULL;
664}
665
Ben Pfaffd1211902013-11-25 10:40:51 -0800666/* Add 'mask' into the mask list, if it is not already there. */
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700667static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800668 const struct sw_flow_mask *new)
Pravin B Shelare6445712013-10-03 18:16:47 -0700669{
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700670 struct sw_flow_mask *mask;
671 mask = flow_mask_find(tbl, new);
672 if (!mask) {
673 /* Allocate a new mask if none exsits. */
674 mask = mask_alloc();
675 if (!mask)
676 return -ENOMEM;
677 mask->key = new->key;
678 mask->range = new->range;
679 list_add_rcu(&mask->list, &tbl->mask_list);
Andy Zhoue80857c2014-01-21 09:31:04 -0800680 } else {
681 BUG_ON(!mask->ref_count);
682 mask->ref_count++;
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700683 }
684
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700685 flow->mask = mask;
686 return 0;
687}
688
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700689/* Must be called with OVS mutex held. */
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800690static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700691{
692 struct table_instance *new_ti = NULL;
693 struct table_instance *ti;
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700694
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800695 flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700696 ti = ovsl_dereference(table->ti);
697 table_instance_insert(ti, flow);
698 table->count++;
699
700 /* Expand table, if necessary, to make room. */
701 if (table->count > ti->n_buckets)
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800702 new_ti = table_instance_expand(ti, false);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700703 else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800704 new_ti = table_instance_rehash(ti, ti->n_buckets, false);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700705
706 if (new_ti) {
707 rcu_assign_pointer(table->ti, new_ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800708 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700709 table->last_rehash = jiffies;
710 }
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800711}
712
713/* Must be called with OVS mutex held. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800714static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
715{
716 struct table_instance *ti;
717
718 flow->ufid_table.hash = ufid_hash(&flow->id);
719 ti = ovsl_dereference(table->ufid_ti);
720 ufid_table_instance_insert(ti, flow);
721 table->ufid_count++;
722
723 /* Expand table, if necessary, to make room. */
724 if (table->ufid_count > ti->n_buckets) {
725 struct table_instance *new_ti;
726
727 new_ti = table_instance_expand(ti, true);
728 if (new_ti) {
729 rcu_assign_pointer(table->ufid_ti, new_ti);
730 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
731 }
732 }
733}
734
735/* Must be called with OVS mutex held. */
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800736int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
737 const struct sw_flow_mask *mask)
738{
739 int err;
740
741 err = flow_mask_insert(table, flow, mask);
742 if (err)
743 return err;
744 flow_key_insert(table, flow);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800745 if (ovs_identifier_is_ufid(&flow->id))
746 flow_ufid_insert(table, flow);
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800747
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700748 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700749}
750
751/* Initializes the flow module.
752 * Returns zero if successful or a negative error code. */
753int ovs_flow_init(void)
754{
755 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
756 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
757
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700758 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
Chris J Argesbac541e2015-07-21 12:36:33 -0500759 + (nr_node_ids
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700760 * sizeof(struct flow_stats *)),
761 0, 0, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700762 if (flow_cache == NULL)
763 return -ENOMEM;
764
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700765 flow_stats_cache
766 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
767 0, SLAB_HWCACHE_ALIGN, NULL);
768 if (flow_stats_cache == NULL) {
769 kmem_cache_destroy(flow_cache);
770 flow_cache = NULL;
771 return -ENOMEM;
772 }
773
Pravin B Shelare6445712013-10-03 18:16:47 -0700774 return 0;
775}
776
777/* Uninitializes the flow module. */
778void ovs_flow_exit(void)
779{
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700780 kmem_cache_destroy(flow_stats_cache);
Pravin B Shelare6445712013-10-03 18:16:47 -0700781 kmem_cache_destroy(flow_cache);
782}