blob: 957a3c31dbb03077e47f08fc0b02708c82497331 [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);
Thadeu Lima de Souza Cascardo40773962016-09-15 19:11:52 -0300151 /* We open code this to make sure node 0 is always considered */
152 for (node = 0; node < MAX_NUMNODES; node = next_node(node, node_possible_map))
153 if (node != 0 && flow->stats[node])
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700154 kmem_cache_free(flow_stats_cache,
155 (struct flow_stats __force *)flow->stats[node]);
Pravin B Shelare6445712013-10-03 18:16:47 -0700156 kmem_cache_free(flow_cache, flow);
157}
158
159static void rcu_free_flow_callback(struct rcu_head *rcu)
160{
161 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
162
163 flow_free(flow);
164}
165
166void ovs_flow_free(struct sw_flow *flow, bool deferred)
167{
168 if (!flow)
169 return;
170
Pravin B Shelare6445712013-10-03 18:16:47 -0700171 if (deferred)
172 call_rcu(&flow->rcu, rcu_free_flow_callback);
173 else
174 flow_free(flow);
175}
176
177static void free_buckets(struct flex_array *buckets)
178{
179 flex_array_free(buckets);
180}
181
Andy Zhoue80857c2014-01-21 09:31:04 -0800182
Pravin B Shelarb637e492013-10-04 00:14:23 -0700183static void __table_instance_destroy(struct table_instance *ti)
Pravin B Shelare6445712013-10-03 18:16:47 -0700184{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700185 free_buckets(ti->buckets);
186 kfree(ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700187}
188
Pravin B Shelarb637e492013-10-04 00:14:23 -0700189static struct table_instance *table_instance_alloc(int new_size)
Pravin B Shelare6445712013-10-03 18:16:47 -0700190{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700191 struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700192
Pravin B Shelarb637e492013-10-04 00:14:23 -0700193 if (!ti)
Pravin B Shelare6445712013-10-03 18:16:47 -0700194 return NULL;
195
Pravin B Shelarb637e492013-10-04 00:14:23 -0700196 ti->buckets = alloc_buckets(new_size);
Pravin B Shelare6445712013-10-03 18:16:47 -0700197
Pravin B Shelarb637e492013-10-04 00:14:23 -0700198 if (!ti->buckets) {
199 kfree(ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700200 return NULL;
201 }
Pravin B Shelarb637e492013-10-04 00:14:23 -0700202 ti->n_buckets = new_size;
203 ti->node_ver = 0;
204 ti->keep_flows = false;
205 get_random_bytes(&ti->hash_seed, sizeof(u32));
206
207 return ti;
208}
209
210int ovs_flow_tbl_init(struct flow_table *table)
211{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800212 struct table_instance *ti, *ufid_ti;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700213
214 ti = table_instance_alloc(TBL_MIN_BUCKETS);
215
216 if (!ti)
217 return -ENOMEM;
218
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800219 ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
220 if (!ufid_ti)
221 goto free_ti;
222
Pravin B Shelarb637e492013-10-04 00:14:23 -0700223 rcu_assign_pointer(table->ti, ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800224 rcu_assign_pointer(table->ufid_ti, ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700225 INIT_LIST_HEAD(&table->mask_list);
226 table->last_rehash = jiffies;
Pravin B Shelare6445712013-10-03 18:16:47 -0700227 table->count = 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800228 table->ufid_count = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700229 return 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800230
231free_ti:
232 __table_instance_destroy(ti);
233 return -ENOMEM;
Pravin B Shelare6445712013-10-03 18:16:47 -0700234}
235
236static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
237{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700238 struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -0700239
Pravin B Shelarb637e492013-10-04 00:14:23 -0700240 __table_instance_destroy(ti);
241}
242
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800243static void table_instance_destroy(struct table_instance *ti,
244 struct table_instance *ufid_ti,
245 bool deferred)
Pravin B Shelarb637e492013-10-04 00:14:23 -0700246{
Andy Zhoue80857c2014-01-21 09:31:04 -0800247 int i;
248
Pravin B Shelarb637e492013-10-04 00:14:23 -0700249 if (!ti)
250 return;
251
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800252 BUG_ON(!ufid_ti);
Andy Zhoue80857c2014-01-21 09:31:04 -0800253 if (ti->keep_flows)
254 goto skip_flows;
255
256 for (i = 0; i < ti->n_buckets; i++) {
257 struct sw_flow *flow;
258 struct hlist_head *head = flex_array_get(ti->buckets, i);
259 struct hlist_node *n;
260 int ver = ti->node_ver;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800261 int ufid_ver = ufid_ti->node_ver;
Andy Zhoue80857c2014-01-21 09:31:04 -0800262
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800263 hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
264 hlist_del_rcu(&flow->flow_table.node[ver]);
265 if (ovs_identifier_is_ufid(&flow->id))
266 hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
Andy Zhoue80857c2014-01-21 09:31:04 -0800267 ovs_flow_free(flow, deferred);
268 }
269 }
270
271skip_flows:
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800272 if (deferred) {
Pravin B Shelarb637e492013-10-04 00:14:23 -0700273 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800274 call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
275 } else {
Pravin B Shelarb637e492013-10-04 00:14:23 -0700276 __table_instance_destroy(ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800277 __table_instance_destroy(ufid_ti);
278 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700279}
280
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700281/* No need for locking this function is called from RCU callback or
282 * error path.
283 */
284void ovs_flow_tbl_destroy(struct flow_table *table)
Pravin B Shelare6445712013-10-03 18:16:47 -0700285{
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700286 struct table_instance *ti = rcu_dereference_raw(table->ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800287 struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700288
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800289 table_instance_destroy(ti, ufid_ti, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700290}
291
Pravin B Shelarb637e492013-10-04 00:14:23 -0700292struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
Pravin B Shelare6445712013-10-03 18:16:47 -0700293 u32 *bucket, u32 *last)
294{
295 struct sw_flow *flow;
296 struct hlist_head *head;
297 int ver;
298 int i;
299
Pravin B Shelarb637e492013-10-04 00:14:23 -0700300 ver = ti->node_ver;
301 while (*bucket < ti->n_buckets) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700302 i = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700303 head = flex_array_get(ti->buckets, *bucket);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800304 hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700305 if (i < *last) {
306 i++;
307 continue;
308 }
309 *last = i + 1;
310 return flow;
311 }
312 (*bucket)++;
313 *last = 0;
314 }
315
316 return NULL;
317}
318
Pravin B Shelarb637e492013-10-04 00:14:23 -0700319static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
Pravin B Shelare6445712013-10-03 18:16:47 -0700320{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700321 hash = jhash_1word(hash, ti->hash_seed);
322 return flex_array_get(ti->buckets,
323 (hash & (ti->n_buckets - 1)));
Pravin B Shelare6445712013-10-03 18:16:47 -0700324}
325
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800326static void table_instance_insert(struct table_instance *ti,
327 struct sw_flow *flow)
Pravin B Shelare6445712013-10-03 18:16:47 -0700328{
329 struct hlist_head *head;
330
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800331 head = find_bucket(ti, flow->flow_table.hash);
332 hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
333}
334
335static void ufid_table_instance_insert(struct table_instance *ti,
336 struct sw_flow *flow)
337{
338 struct hlist_head *head;
339
340 head = find_bucket(ti, flow->ufid_table.hash);
341 hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
Pravin B Shelare6445712013-10-03 18:16:47 -0700342}
343
Pravin B Shelarb637e492013-10-04 00:14:23 -0700344static void flow_table_copy_flows(struct table_instance *old,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800345 struct table_instance *new, bool ufid)
Pravin B Shelare6445712013-10-03 18:16:47 -0700346{
347 int old_ver;
348 int i;
349
350 old_ver = old->node_ver;
351 new->node_ver = !old_ver;
352
353 /* Insert in new table. */
354 for (i = 0; i < old->n_buckets; i++) {
355 struct sw_flow *flow;
356 struct hlist_head *head;
357
358 head = flex_array_get(old->buckets, i);
359
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800360 if (ufid)
361 hlist_for_each_entry(flow, head,
362 ufid_table.node[old_ver])
363 ufid_table_instance_insert(new, flow);
364 else
365 hlist_for_each_entry(flow, head,
366 flow_table.node[old_ver])
367 table_instance_insert(new, flow);
Pravin B Shelare6445712013-10-03 18:16:47 -0700368 }
369
Pravin B Shelare6445712013-10-03 18:16:47 -0700370 old->keep_flows = true;
371}
372
Pravin B Shelarb637e492013-10-04 00:14:23 -0700373static struct table_instance *table_instance_rehash(struct table_instance *ti,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800374 int n_buckets, bool ufid)
Pravin B Shelare6445712013-10-03 18:16:47 -0700375{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700376 struct table_instance *new_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700377
Pravin B Shelarb637e492013-10-04 00:14:23 -0700378 new_ti = table_instance_alloc(n_buckets);
379 if (!new_ti)
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700380 return NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700381
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800382 flow_table_copy_flows(ti, new_ti, ufid);
Pravin B Shelare6445712013-10-03 18:16:47 -0700383
Pravin B Shelarb637e492013-10-04 00:14:23 -0700384 return new_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700385}
386
Pravin B Shelarb637e492013-10-04 00:14:23 -0700387int ovs_flow_tbl_flush(struct flow_table *flow_table)
Pravin B Shelare6445712013-10-03 18:16:47 -0700388{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800389 struct table_instance *old_ti, *new_ti;
390 struct table_instance *old_ufid_ti, *new_ufid_ti;
Pravin B Shelare6445712013-10-03 18:16:47 -0700391
Pravin B Shelarb637e492013-10-04 00:14:23 -0700392 new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
393 if (!new_ti)
394 return -ENOMEM;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800395 new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
396 if (!new_ufid_ti)
397 goto err_free_ti;
398
399 old_ti = ovsl_dereference(flow_table->ti);
400 old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700401
402 rcu_assign_pointer(flow_table->ti, new_ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800403 rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700404 flow_table->last_rehash = jiffies;
405 flow_table->count = 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800406 flow_table->ufid_count = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700407
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800408 table_instance_destroy(old_ti, old_ufid_ti, true);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700409 return 0;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800410
411err_free_ti:
412 __table_instance_destroy(new_ti);
413 return -ENOMEM;
Pravin B Shelare6445712013-10-03 18:16:47 -0700414}
415
Joe Stringer272c2cf2015-01-21 16:42:50 -0800416static u32 flow_hash(const struct sw_flow_key *key,
417 const struct sw_flow_key_range *range)
Pravin B Shelare6445712013-10-03 18:16:47 -0700418{
Joe Stringer272c2cf2015-01-21 16:42:50 -0800419 int key_start = range->start;
420 int key_end = range->end;
Daniele Di Proietto70851302014-01-23 10:56:49 -0800421 const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
Pravin B Shelare6445712013-10-03 18:16:47 -0700422 int hash_u32s = (key_end - key_start) >> 2;
423
424 /* Make sure number of hash bytes are multiple of u32. */
425 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
426
Daniel Borkmann87545892014-12-10 16:33:11 +0100427 return jhash2(hash_key, hash_u32s, 0);
Pravin B Shelare6445712013-10-03 18:16:47 -0700428}
429
430static int flow_key_start(const struct sw_flow_key *key)
431{
Jiri Benc00a93ba2015-10-05 13:09:46 +0200432 if (key->tun_proto)
Pravin B Shelare6445712013-10-03 18:16:47 -0700433 return 0;
434 else
435 return rounddown(offsetof(struct sw_flow_key, phy),
436 sizeof(long));
437}
438
439static bool cmp_key(const struct sw_flow_key *key1,
440 const struct sw_flow_key *key2,
441 int key_start, int key_end)
442{
Daniele Di Proietto70851302014-01-23 10:56:49 -0800443 const long *cp1 = (const long *)((const u8 *)key1 + key_start);
444 const long *cp2 = (const long *)((const u8 *)key2 + key_start);
Pravin B Shelare6445712013-10-03 18:16:47 -0700445 long diffs = 0;
446 int i;
447
448 for (i = key_start; i < key_end; i += sizeof(long))
449 diffs |= *cp1++ ^ *cp2++;
450
451 return diffs == 0;
452}
453
454static bool flow_cmp_masked_key(const struct sw_flow *flow,
455 const struct sw_flow_key *key,
Joe Stringer272c2cf2015-01-21 16:42:50 -0800456 const struct sw_flow_key_range *range)
Pravin B Shelare6445712013-10-03 18:16:47 -0700457{
Joe Stringer272c2cf2015-01-21 16:42:50 -0800458 return cmp_key(&flow->key, key, range->start, range->end);
Pravin B Shelare6445712013-10-03 18:16:47 -0700459}
460
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800461static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
462 const struct sw_flow_match *match)
Pravin B Shelare6445712013-10-03 18:16:47 -0700463{
464 struct sw_flow_key *key = match->key;
465 int key_start = flow_key_start(key);
466 int key_end = match->range.end;
467
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800468 BUG_ON(ovs_identifier_is_ufid(&flow->id));
469 return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
Pravin B Shelare6445712013-10-03 18:16:47 -0700470}
471
Pravin B Shelarb637e492013-10-04 00:14:23 -0700472static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
Pravin B Shelare6445712013-10-03 18:16:47 -0700473 const struct sw_flow_key *unmasked,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800474 const struct sw_flow_mask *mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700475{
476 struct sw_flow *flow;
477 struct hlist_head *head;
Pravin B Shelare6445712013-10-03 18:16:47 -0700478 u32 hash;
479 struct sw_flow_key masked_key;
480
Jesse Grossae5f2fb2015-09-21 20:21:20 -0700481 ovs_flow_mask_key(&masked_key, unmasked, false, mask);
Joe Stringer272c2cf2015-01-21 16:42:50 -0800482 hash = flow_hash(&masked_key, &mask->range);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700483 head = find_bucket(ti, hash);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800484 hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
485 if (flow->mask == mask && flow->flow_table.hash == hash &&
Joe Stringer272c2cf2015-01-21 16:42:50 -0800486 flow_cmp_masked_key(flow, &masked_key, &mask->range))
Pravin B Shelare6445712013-10-03 18:16:47 -0700487 return flow;
488 }
489 return NULL;
490}
491
Andy Zhou5bb50632013-11-25 10:42:46 -0800492struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
Andy Zhou1bd71162013-10-22 10:42:46 -0700493 const struct sw_flow_key *key,
494 u32 *n_mask_hit)
Pravin B Shelare6445712013-10-03 18:16:47 -0700495{
Jesse Gross663efa32013-12-03 10:58:53 -0800496 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
Pravin B Shelare6445712013-10-03 18:16:47 -0700497 struct sw_flow_mask *mask;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700498 struct sw_flow *flow;
Pravin B Shelare6445712013-10-03 18:16:47 -0700499
Andy Zhou1bd71162013-10-22 10:42:46 -0700500 *n_mask_hit = 0;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700501 list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
Andy Zhou1bd71162013-10-22 10:42:46 -0700502 (*n_mask_hit)++;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700503 flow = masked_flow_lookup(ti, key, mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700504 if (flow) /* Found */
Pravin B Shelarb637e492013-10-04 00:14:23 -0700505 return flow;
Pravin B Shelare6445712013-10-03 18:16:47 -0700506 }
Pravin B Shelarb637e492013-10-04 00:14:23 -0700507 return NULL;
508}
Pravin B Shelare6445712013-10-03 18:16:47 -0700509
Andy Zhou5bb50632013-11-25 10:42:46 -0800510struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
511 const struct sw_flow_key *key)
512{
513 u32 __always_unused n_mask_hit;
514
515 return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
516}
517
Alex Wang4a46b242014-06-30 20:30:29 -0700518struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800519 const struct sw_flow_match *match)
Alex Wang4a46b242014-06-30 20:30:29 -0700520{
521 struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
522 struct sw_flow_mask *mask;
523 struct sw_flow *flow;
524
525 /* Always called under ovs-mutex. */
526 list_for_each_entry(mask, &tbl->mask_list, list) {
527 flow = masked_flow_lookup(ti, match->key, mask);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800528 if (flow && ovs_identifier_is_key(&flow->id) &&
529 ovs_flow_cmp_unmasked_key(flow, match))
530 return flow;
531 }
532 return NULL;
533}
534
535static u32 ufid_hash(const struct sw_flow_id *sfid)
536{
537 return jhash(sfid->ufid, sfid->ufid_len, 0);
538}
539
540static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
541 const struct sw_flow_id *sfid)
542{
543 if (flow->id.ufid_len != sfid->ufid_len)
544 return false;
545
546 return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
547}
548
549bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
550{
551 if (ovs_identifier_is_ufid(&flow->id))
552 return flow_cmp_masked_key(flow, match->key, &match->range);
553
554 return ovs_flow_cmp_unmasked_key(flow, match);
555}
556
557struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
558 const struct sw_flow_id *ufid)
559{
560 struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
561 struct sw_flow *flow;
562 struct hlist_head *head;
563 u32 hash;
564
565 hash = ufid_hash(ufid);
566 head = find_bucket(ti, hash);
567 hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
568 if (flow->ufid_table.hash == hash &&
569 ovs_flow_cmp_ufid(flow, ufid))
Alex Wang4a46b242014-06-30 20:30:29 -0700570 return flow;
571 }
572 return NULL;
573}
574
Andy Zhou1bd71162013-10-22 10:42:46 -0700575int ovs_flow_tbl_num_masks(const struct flow_table *table)
576{
577 struct sw_flow_mask *mask;
578 int num = 0;
579
580 list_for_each_entry(mask, &table->mask_list, list)
581 num++;
582
583 return num;
584}
585
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800586static struct table_instance *table_instance_expand(struct table_instance *ti,
587 bool ufid)
Pravin B Shelarb637e492013-10-04 00:14:23 -0700588{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800589 return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
Pravin B Shelare6445712013-10-03 18:16:47 -0700590}
591
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700592/* Remove 'mask' from the mask list, if it is not needed any more. */
593static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
594{
595 if (mask) {
596 /* ovs-lock is required to protect mask-refcount and
597 * mask list.
598 */
599 ASSERT_OVSL();
600 BUG_ON(!mask->ref_count);
601 mask->ref_count--;
602
603 if (!mask->ref_count) {
604 list_del_rcu(&mask->list);
605 kfree_rcu(mask, rcu);
606 }
607 }
608}
609
610/* Must be called with OVS mutex held. */
Pravin B Shelare6445712013-10-03 18:16:47 -0700611void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
612{
Pravin B Shelarb637e492013-10-04 00:14:23 -0700613 struct table_instance *ti = ovsl_dereference(table->ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800614 struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
Pravin B Shelarb637e492013-10-04 00:14:23 -0700615
Pravin B Shelare6445712013-10-03 18:16:47 -0700616 BUG_ON(table->count == 0);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800617 hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
Pravin B Shelare6445712013-10-03 18:16:47 -0700618 table->count--;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800619 if (ovs_identifier_is_ufid(&flow->id)) {
620 hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
621 table->ufid_count--;
622 }
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700623
624 /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
625 * accessible as long as the RCU read lock is held.
626 */
627 flow_mask_remove(table, flow->mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700628}
629
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700630static struct sw_flow_mask *mask_alloc(void)
Pravin B Shelare6445712013-10-03 18:16:47 -0700631{
632 struct sw_flow_mask *mask;
633
634 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
635 if (mask)
Andy Zhoue80857c2014-01-21 09:31:04 -0800636 mask->ref_count = 1;
Pravin B Shelare6445712013-10-03 18:16:47 -0700637
638 return mask;
639}
640
Pravin B Shelare6445712013-10-03 18:16:47 -0700641static bool mask_equal(const struct sw_flow_mask *a,
642 const struct sw_flow_mask *b)
643{
Daniele Di Proietto70851302014-01-23 10:56:49 -0800644 const u8 *a_ = (const u8 *)&a->key + a->range.start;
645 const u8 *b_ = (const u8 *)&b->key + b->range.start;
Pravin B Shelare6445712013-10-03 18:16:47 -0700646
647 return (a->range.end == b->range.end)
648 && (a->range.start == b->range.start)
649 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
650}
651
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700652static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700653 const struct sw_flow_mask *mask)
654{
655 struct list_head *ml;
656
Pravin B Shelarb637e492013-10-04 00:14:23 -0700657 list_for_each(ml, &tbl->mask_list) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700658 struct sw_flow_mask *m;
659 m = container_of(ml, struct sw_flow_mask, list);
660 if (mask_equal(mask, m))
661 return m;
662 }
663
664 return NULL;
665}
666
Ben Pfaffd1211902013-11-25 10:40:51 -0800667/* Add 'mask' into the mask list, if it is not already there. */
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700668static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800669 const struct sw_flow_mask *new)
Pravin B Shelare6445712013-10-03 18:16:47 -0700670{
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700671 struct sw_flow_mask *mask;
672 mask = flow_mask_find(tbl, new);
673 if (!mask) {
674 /* Allocate a new mask if none exsits. */
675 mask = mask_alloc();
676 if (!mask)
677 return -ENOMEM;
678 mask->key = new->key;
679 mask->range = new->range;
680 list_add_rcu(&mask->list, &tbl->mask_list);
Andy Zhoue80857c2014-01-21 09:31:04 -0800681 } else {
682 BUG_ON(!mask->ref_count);
683 mask->ref_count++;
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700684 }
685
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700686 flow->mask = mask;
687 return 0;
688}
689
Jarno Rajahalme56c19862014-05-05 13:24:53 -0700690/* Must be called with OVS mutex held. */
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800691static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700692{
693 struct table_instance *new_ti = NULL;
694 struct table_instance *ti;
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700695
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800696 flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700697 ti = ovsl_dereference(table->ti);
698 table_instance_insert(ti, flow);
699 table->count++;
700
701 /* Expand table, if necessary, to make room. */
702 if (table->count > ti->n_buckets)
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800703 new_ti = table_instance_expand(ti, false);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700704 else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800705 new_ti = table_instance_rehash(ti, ti->n_buckets, false);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700706
707 if (new_ti) {
708 rcu_assign_pointer(table->ti, new_ti);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800709 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700710 table->last_rehash = jiffies;
711 }
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800712}
713
714/* Must be called with OVS mutex held. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800715static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
716{
717 struct table_instance *ti;
718
719 flow->ufid_table.hash = ufid_hash(&flow->id);
720 ti = ovsl_dereference(table->ufid_ti);
721 ufid_table_instance_insert(ti, flow);
722 table->ufid_count++;
723
724 /* Expand table, if necessary, to make room. */
725 if (table->ufid_count > ti->n_buckets) {
726 struct table_instance *new_ti;
727
728 new_ti = table_instance_expand(ti, true);
729 if (new_ti) {
730 rcu_assign_pointer(table->ufid_ti, new_ti);
731 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
732 }
733 }
734}
735
736/* Must be called with OVS mutex held. */
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800737int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
738 const struct sw_flow_mask *mask)
739{
740 int err;
741
742 err = flow_mask_insert(table, flow, mask);
743 if (err)
744 return err;
745 flow_key_insert(table, flow);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800746 if (ovs_identifier_is_ufid(&flow->id))
747 flow_ufid_insert(table, flow);
Joe Stringerd29ab6f2015-01-21 16:42:49 -0800748
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700749 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700750}
751
752/* Initializes the flow module.
753 * Returns zero if successful or a negative error code. */
754int ovs_flow_init(void)
755{
756 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
757 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
758
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700759 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
Chris J Argesbac541e2015-07-21 12:36:33 -0500760 + (nr_node_ids
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700761 * sizeof(struct flow_stats *)),
762 0, 0, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700763 if (flow_cache == NULL)
764 return -ENOMEM;
765
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700766 flow_stats_cache
767 = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
768 0, SLAB_HWCACHE_ALIGN, NULL);
769 if (flow_stats_cache == NULL) {
770 kmem_cache_destroy(flow_cache);
771 flow_cache = NULL;
772 return -ENOMEM;
773 }
774
Pravin B Shelare6445712013-10-03 18:16:47 -0700775 return 0;
776}
777
778/* Uninitializes the flow module. */
779void ovs_flow_exit(void)
780{
Jarno Rajahalme63e79592014-03-27 12:42:54 -0700781 kmem_cache_destroy(flow_stats_cache);
Pravin B Shelare6445712013-10-03 18:16:47 -0700782 kmem_cache_destroy(flow_cache);
783}