]>
Commit | Line | Data |
---|---|---|
a097c0b2 PS |
1 | /* |
2 | * Copyright (c) 2007-2013 Nicira, Inc. | |
3 | * | |
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> | |
1dfb9f31 | 28 | #include <linux/jhash.h> |
a097c0b2 PS |
29 | #include <linux/jiffies.h> |
30 | #include <linux/llc.h> | |
31 | #include <linux/module.h> | |
32 | #include <linux/in.h> | |
33 | #include <linux/rcupdate.h> | |
f7dc6d34 | 34 | #include <linux/cpumask.h> |
a097c0b2 PS |
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 | ||
e23775f2 | 48 | #include "flow_netlink.h" |
a097c0b2 | 49 | |
994dc286 | 50 | #define TBL_MIN_BUCKETS 1024 |
d49fc3ff | 51 | #define MASK_ARRAY_SIZE_MIN 16 |
994dc286 PS |
52 | #define REHASH_INTERVAL (10 * 60 * HZ) |
53 | ||
5604935e PS |
54 | #define MC_HASH_SHIFT 8 |
55 | #define MC_HASH_ENTRIES (1u << MC_HASH_SHIFT) | |
56 | #define MC_HASH_SEGS ((sizeof(uint32_t) * 8) / MC_HASH_SHIFT) | |
57 | ||
a097c0b2 | 58 | static struct kmem_cache *flow_cache; |
9ac56358 | 59 | struct kmem_cache *flow_stats_cache __read_mostly; |
a097c0b2 PS |
60 | |
61 | static u16 range_n_bytes(const struct sw_flow_key_range *range) | |
62 | { | |
63 | return range->end - range->start; | |
64 | } | |
65 | ||
66 | void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src, | |
ad4adec2 | 67 | bool full, const struct sw_flow_mask *mask) |
a097c0b2 | 68 | { |
ad4adec2 JG |
69 | int start = full ? 0 : mask->range.start; |
70 | int len = full ? sizeof *dst : range_n_bytes(&mask->range); | |
71 | const long *m = (const long *)((const u8 *)&mask->key + start); | |
72 | const long *s = (const long *)((const u8 *)src + start); | |
73 | long *d = (long *)((u8 *)dst + start); | |
a097c0b2 PS |
74 | int i; |
75 | ||
ad4adec2 JG |
76 | /* If 'full' is true then all of 'dst' is fully initialized. Otherwise, |
77 | * if 'full' is false the memory outside of the 'mask->range' is left | |
78 | * uninitialized. This can be used as an optimization when further | |
79 | * operations on 'dst' only use contents within 'mask->range'. | |
a097c0b2 | 80 | */ |
ad4adec2 | 81 | for (i = 0; i < len; i += sizeof(long)) |
a097c0b2 PS |
82 | *d++ = *s++ & *m++; |
83 | } | |
84 | ||
df65fec1 | 85 | struct sw_flow *ovs_flow_alloc(void) |
a097c0b2 PS |
86 | { |
87 | struct sw_flow *flow; | |
a2bdad67 | 88 | struct sw_flow_stats *stats; |
a097c0b2 | 89 | |
f7dc6d34 | 90 | flow = kmem_cache_zalloc(flow_cache, GFP_KERNEL); |
a097c0b2 PS |
91 | if (!flow) |
92 | return ERR_PTR(-ENOMEM); | |
93 | ||
f7dc6d34 | 94 | flow->stats_last_writer = -1; |
a097c0b2 | 95 | |
9ac56358 JR |
96 | /* Initialize the default stat node. */ |
97 | stats = kmem_cache_alloc_node(flow_stats_cache, | |
96e72484 PS |
98 | GFP_KERNEL | __GFP_ZERO, |
99 | node_online(0) ? 0 : NUMA_NO_NODE); | |
9ac56358 | 100 | if (!stats) |
df65fec1 | 101 | goto err; |
b0b906cc | 102 | |
9ac56358 JR |
103 | spin_lock_init(&stats->lock); |
104 | ||
105 | RCU_INIT_POINTER(flow->stats[0], stats); | |
106 | ||
657a6e89 TZ |
107 | cpumask_set_cpu(0, &flow->cpu_used_mask); |
108 | ||
a097c0b2 | 109 | return flow; |
b0f3a2fe | 110 | err: |
5f67d45a | 111 | kmem_cache_free(flow_cache, flow); |
b0f3a2fe | 112 | return ERR_PTR(-ENOMEM); |
a097c0b2 PS |
113 | } |
114 | ||
f1f60b85 | 115 | int ovs_flow_tbl_count(const struct flow_table *table) |
994dc286 PS |
116 | { |
117 | return table->count; | |
118 | } | |
119 | ||
a097c0b2 PS |
120 | static void flow_free(struct sw_flow *flow) |
121 | { | |
f7dc6d34 | 122 | int cpu; |
9ac56358 | 123 | |
bc619e29 JS |
124 | if (ovs_identifier_is_key(&flow->id)) |
125 | kfree(flow->id.unmasked_key); | |
e23775f2 PS |
126 | if (flow->sf_acts) |
127 | ovs_nla_free_flow_actions((struct sw_flow_actions __force *)flow->sf_acts); | |
f7dc6d34 | 128 | /* We open code this to make sure cpu 0 is always considered */ |
657a6e89 | 129 | for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, &flow->cpu_used_mask)) |
f7dc6d34 | 130 | if (flow->stats[cpu]) |
9ac56358 | 131 | kmem_cache_free(flow_stats_cache, |
f7dc6d34 | 132 | rcu_dereference_raw(flow->stats[cpu])); |
a097c0b2 PS |
133 | kmem_cache_free(flow_cache, flow); |
134 | } | |
135 | ||
136 | static void rcu_free_flow_callback(struct rcu_head *rcu) | |
137 | { | |
138 | struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); | |
139 | ||
140 | flow_free(flow); | |
141 | } | |
142 | ||
d103f479 | 143 | void ovs_flow_free(struct sw_flow *flow, bool deferred) |
0585f7a8 | 144 | { |
d103f479 | 145 | if (!flow) |
0585f7a8 PS |
146 | return; |
147 | ||
a097c0b2 PS |
148 | if (deferred) |
149 | call_rcu(&flow->rcu, rcu_free_flow_callback); | |
150 | else | |
151 | flow_free(flow); | |
152 | } | |
153 | ||
994dc286 | 154 | static void __table_instance_destroy(struct table_instance *ti) |
a097c0b2 | 155 | { |
4383e54b | 156 | kvfree(ti->buckets); |
994dc286 | 157 | kfree(ti); |
a097c0b2 PS |
158 | } |
159 | ||
994dc286 | 160 | static struct table_instance *table_instance_alloc(int new_size) |
a097c0b2 | 161 | { |
994dc286 | 162 | struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL); |
4383e54b | 163 | int i; |
a097c0b2 | 164 | |
994dc286 | 165 | if (!ti) |
a097c0b2 PS |
166 | return NULL; |
167 | ||
4383e54b KO |
168 | ti->buckets = kvmalloc_array(new_size, sizeof(struct hlist_head), |
169 | GFP_KERNEL); | |
994dc286 PS |
170 | if (!ti->buckets) { |
171 | kfree(ti); | |
a097c0b2 PS |
172 | return NULL; |
173 | } | |
4383e54b KO |
174 | |
175 | for (i = 0; i < new_size; i++) | |
176 | INIT_HLIST_HEAD(&ti->buckets[i]); | |
177 | ||
994dc286 PS |
178 | ti->n_buckets = new_size; |
179 | ti->node_ver = 0; | |
180 | ti->keep_flows = false; | |
181 | get_random_bytes(&ti->hash_seed, sizeof(u32)); | |
a097c0b2 | 182 | |
994dc286 | 183 | return ti; |
a097c0b2 PS |
184 | } |
185 | ||
d49fc3ff PS |
186 | static void mask_array_rcu_cb(struct rcu_head *rcu) |
187 | { | |
188 | struct mask_array *ma = container_of(rcu, struct mask_array, rcu); | |
189 | ||
190 | kfree(ma); | |
191 | } | |
192 | ||
193 | static struct mask_array *tbl_mask_array_alloc(int size) | |
194 | { | |
195 | struct mask_array *new; | |
196 | ||
3dd6d3a2 | 197 | size = max(MASK_ARRAY_SIZE_MIN, size); |
d49fc3ff PS |
198 | new = kzalloc(sizeof(struct mask_array) + |
199 | sizeof(struct sw_flow_mask *) * size, GFP_KERNEL); | |
200 | if (!new) | |
201 | return NULL; | |
202 | ||
203 | new->count = 0; | |
204 | new->max = size; | |
205 | ||
206 | return new; | |
207 | } | |
208 | ||
209 | static int tbl_mask_array_realloc(struct flow_table *tbl, int size) | |
210 | { | |
211 | struct mask_array *old; | |
212 | struct mask_array *new; | |
213 | ||
214 | new = tbl_mask_array_alloc(size); | |
215 | if (!new) | |
216 | return -ENOMEM; | |
217 | ||
218 | old = ovsl_dereference(tbl->mask_array); | |
219 | if (old) { | |
3dd6d3a2 | 220 | int i, count = 0; |
d49fc3ff | 221 | |
3dd6d3a2 PS |
222 | for (i = 0; i < old->max; i++) { |
223 | if (ovsl_dereference(old->masks[i])) | |
224 | new->masks[count++] = old->masks[i]; | |
225 | } | |
6ddb6313 | 226 | |
3dd6d3a2 | 227 | new->count = count; |
d49fc3ff PS |
228 | } |
229 | rcu_assign_pointer(tbl->mask_array, new); | |
230 | ||
231 | if (old) | |
232 | call_rcu(&old->rcu, mask_array_rcu_cb); | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
994dc286 | 237 | int ovs_flow_tbl_init(struct flow_table *table) |
a097c0b2 | 238 | { |
bc619e29 | 239 | struct table_instance *ti, *ufid_ti; |
d49fc3ff | 240 | struct mask_array *ma; |
a097c0b2 | 241 | |
5604935e PS |
242 | table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) * |
243 | MC_HASH_ENTRIES, __alignof__(struct mask_cache_entry)); | |
244 | if (!table->mask_cache) | |
245 | return -ENOMEM; | |
a097c0b2 | 246 | |
d49fc3ff PS |
247 | ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN); |
248 | if (!ma) | |
249 | goto free_mask_cache; | |
250 | ||
5604935e | 251 | ti = table_instance_alloc(TBL_MIN_BUCKETS); |
d49fc3ff PS |
252 | if (!ti) |
253 | goto free_mask_array; | |
a097c0b2 | 254 | |
bc619e29 JS |
255 | ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
256 | if (!ufid_ti) | |
257 | goto free_ti; | |
258 | ||
994dc286 | 259 | rcu_assign_pointer(table->ti, ti); |
bc619e29 | 260 | rcu_assign_pointer(table->ufid_ti, ufid_ti); |
d49fc3ff | 261 | rcu_assign_pointer(table->mask_array, ma); |
994dc286 PS |
262 | table->last_rehash = jiffies; |
263 | table->count = 0; | |
bc619e29 | 264 | table->ufid_count = 0; |
994dc286 | 265 | return 0; |
d49fc3ff | 266 | |
bc619e29 JS |
267 | free_ti: |
268 | __table_instance_destroy(ti); | |
d49fc3ff | 269 | free_mask_array: |
3dd6d3a2 | 270 | kfree(ma); |
d49fc3ff PS |
271 | free_mask_cache: |
272 | free_percpu(table->mask_cache); | |
273 | return -ENOMEM; | |
a097c0b2 PS |
274 | } |
275 | ||
276 | static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) | |
277 | { | |
994dc286 | 278 | struct table_instance *ti = container_of(rcu, struct table_instance, rcu); |
a097c0b2 | 279 | |
994dc286 | 280 | __table_instance_destroy(ti); |
a097c0b2 PS |
281 | } |
282 | ||
bc619e29 JS |
283 | static void table_instance_destroy(struct table_instance *ti, |
284 | struct table_instance *ufid_ti, | |
285 | bool deferred) | |
a097c0b2 | 286 | { |
d103f479 AZ |
287 | int i; |
288 | ||
994dc286 | 289 | if (!ti) |
a097c0b2 PS |
290 | return; |
291 | ||
bc619e29 | 292 | BUG_ON(!ufid_ti); |
d103f479 AZ |
293 | if (ti->keep_flows) |
294 | goto skip_flows; | |
295 | ||
296 | for (i = 0; i < ti->n_buckets; i++) { | |
297 | struct sw_flow *flow; | |
4383e54b | 298 | struct hlist_head *head = &ti->buckets[i]; |
d103f479 AZ |
299 | struct hlist_node *n; |
300 | int ver = ti->node_ver; | |
bc619e29 | 301 | int ufid_ver = ufid_ti->node_ver; |
d103f479 | 302 | |
bc619e29 JS |
303 | hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) { |
304 | hlist_del_rcu(&flow->flow_table.node[ver]); | |
305 | if (ovs_identifier_is_ufid(&flow->id)) | |
306 | hlist_del_rcu(&flow->ufid_table.node[ufid_ver]); | |
d103f479 AZ |
307 | ovs_flow_free(flow, deferred); |
308 | } | |
309 | } | |
310 | ||
311 | skip_flows: | |
bc619e29 | 312 | if (deferred) { |
994dc286 | 313 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); |
bc619e29 JS |
314 | call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb); |
315 | } else { | |
994dc286 | 316 | __table_instance_destroy(ti); |
bc619e29 JS |
317 | __table_instance_destroy(ufid_ti); |
318 | } | |
994dc286 PS |
319 | } |
320 | ||
e379e4d1 | 321 | /* No need for locking this function is called from RCU callback or |
af465b67 PS |
322 | * error path. |
323 | */ | |
e379e4d1 | 324 | void ovs_flow_tbl_destroy(struct flow_table *table) |
994dc286 | 325 | { |
46051cf8 | 326 | struct table_instance *ti = rcu_dereference_raw(table->ti); |
bc619e29 | 327 | struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti); |
994dc286 | 328 | |
5604935e | 329 | free_percpu(table->mask_cache); |
46051cf8 | 330 | kfree(rcu_dereference_raw(table->mask_array)); |
bc619e29 | 331 | table_instance_destroy(ti, ufid_ti, false); |
a097c0b2 PS |
332 | } |
333 | ||
994dc286 | 334 | struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti, |
a097c0b2 PS |
335 | u32 *bucket, u32 *last) |
336 | { | |
337 | struct sw_flow *flow; | |
338 | struct hlist_head *head; | |
339 | int ver; | |
340 | int i; | |
341 | ||
994dc286 PS |
342 | ver = ti->node_ver; |
343 | while (*bucket < ti->n_buckets) { | |
a097c0b2 | 344 | i = 0; |
4383e54b | 345 | head = &ti->buckets[*bucket]; |
bc619e29 | 346 | hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) { |
a097c0b2 PS |
347 | if (i < *last) { |
348 | i++; | |
349 | continue; | |
350 | } | |
351 | *last = i + 1; | |
352 | return flow; | |
353 | } | |
354 | (*bucket)++; | |
355 | *last = 0; | |
356 | } | |
357 | ||
358 | return NULL; | |
359 | } | |
360 | ||
994dc286 | 361 | static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash) |
a097c0b2 | 362 | { |
994dc286 | 363 | hash = jhash_1word(hash, ti->hash_seed); |
4383e54b | 364 | return &ti->buckets[hash & (ti->n_buckets - 1)]; |
a097c0b2 PS |
365 | } |
366 | ||
bc619e29 JS |
367 | static void table_instance_insert(struct table_instance *ti, |
368 | struct sw_flow *flow) | |
a097c0b2 PS |
369 | { |
370 | struct hlist_head *head; | |
371 | ||
bc619e29 JS |
372 | head = find_bucket(ti, flow->flow_table.hash); |
373 | hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head); | |
374 | } | |
375 | ||
376 | static void ufid_table_instance_insert(struct table_instance *ti, | |
377 | struct sw_flow *flow) | |
378 | { | |
379 | struct hlist_head *head; | |
380 | ||
381 | head = find_bucket(ti, flow->ufid_table.hash); | |
382 | hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head); | |
a097c0b2 PS |
383 | } |
384 | ||
994dc286 | 385 | static void flow_table_copy_flows(struct table_instance *old, |
bc619e29 | 386 | struct table_instance *new, bool ufid) |
a097c0b2 PS |
387 | { |
388 | int old_ver; | |
389 | int i; | |
390 | ||
391 | old_ver = old->node_ver; | |
392 | new->node_ver = !old_ver; | |
393 | ||
394 | /* Insert in new table. */ | |
395 | for (i = 0; i < old->n_buckets; i++) { | |
396 | struct sw_flow *flow; | |
4383e54b | 397 | struct hlist_head *head = &old->buckets[i]; |
a097c0b2 | 398 | |
bc619e29 JS |
399 | if (ufid) |
400 | hlist_for_each_entry(flow, head, | |
401 | ufid_table.node[old_ver]) | |
402 | ufid_table_instance_insert(new, flow); | |
403 | else | |
404 | hlist_for_each_entry(flow, head, | |
405 | flow_table.node[old_ver]) | |
406 | table_instance_insert(new, flow); | |
a097c0b2 PS |
407 | } |
408 | ||
a097c0b2 PS |
409 | old->keep_flows = true; |
410 | } | |
411 | ||
994dc286 | 412 | static struct table_instance *table_instance_rehash(struct table_instance *ti, |
bc619e29 | 413 | int n_buckets, bool ufid) |
a097c0b2 | 414 | { |
994dc286 | 415 | struct table_instance *new_ti; |
a097c0b2 | 416 | |
994dc286 PS |
417 | new_ti = table_instance_alloc(n_buckets); |
418 | if (!new_ti) | |
0585f7a8 | 419 | return NULL; |
a097c0b2 | 420 | |
bc619e29 | 421 | flow_table_copy_flows(ti, new_ti, ufid); |
a097c0b2 | 422 | |
994dc286 | 423 | return new_ti; |
a097c0b2 PS |
424 | } |
425 | ||
994dc286 | 426 | int ovs_flow_tbl_flush(struct flow_table *flow_table) |
a097c0b2 | 427 | { |
bc619e29 JS |
428 | struct table_instance *old_ti, *new_ti; |
429 | struct table_instance *old_ufid_ti, *new_ufid_ti; | |
a097c0b2 | 430 | |
994dc286 PS |
431 | new_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
432 | if (!new_ti) | |
433 | return -ENOMEM; | |
bc619e29 JS |
434 | new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
435 | if (!new_ufid_ti) | |
436 | goto err_free_ti; | |
437 | ||
438 | old_ti = ovsl_dereference(flow_table->ti); | |
439 | old_ufid_ti = ovsl_dereference(flow_table->ufid_ti); | |
994dc286 PS |
440 | |
441 | rcu_assign_pointer(flow_table->ti, new_ti); | |
bc619e29 | 442 | rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti); |
994dc286 PS |
443 | flow_table->last_rehash = jiffies; |
444 | flow_table->count = 0; | |
bc619e29 | 445 | flow_table->ufid_count = 0; |
994dc286 | 446 | |
bc619e29 | 447 | table_instance_destroy(old_ti, old_ufid_ti, true); |
994dc286 | 448 | return 0; |
bc619e29 JS |
449 | |
450 | err_free_ti: | |
451 | __table_instance_destroy(new_ti); | |
452 | return -ENOMEM; | |
a097c0b2 PS |
453 | } |
454 | ||
0d8e6be8 JS |
455 | static u32 flow_hash(const struct sw_flow_key *key, |
456 | const struct sw_flow_key_range *range) | |
a097c0b2 | 457 | { |
0d8e6be8 JS |
458 | int key_start = range->start; |
459 | int key_end = range->end; | |
d15ae707 | 460 | const u32 *hash_key = (const u32 *)((const u8 *)key + key_start); |
a097c0b2 PS |
461 | int hash_u32s = (key_end - key_start) >> 2; |
462 | ||
463 | /* Make sure number of hash bytes are multiple of u32. */ | |
464 | BUILD_BUG_ON(sizeof(long) % sizeof(u32)); | |
465 | ||
1dfb9f31 | 466 | return jhash2(hash_key, hash_u32s, 0); |
a097c0b2 PS |
467 | } |
468 | ||
469 | static int flow_key_start(const struct sw_flow_key *key) | |
470 | { | |
bdcbce89 | 471 | if (key->tun_proto) |
a097c0b2 PS |
472 | return 0; |
473 | else | |
474 | return rounddown(offsetof(struct sw_flow_key, phy), | |
475 | sizeof(long)); | |
476 | } | |
477 | ||
478 | static bool cmp_key(const struct sw_flow_key *key1, | |
479 | const struct sw_flow_key *key2, | |
480 | int key_start, int key_end) | |
481 | { | |
d15ae707 DDP |
482 | const long *cp1 = (const long *)((const u8 *)key1 + key_start); |
483 | const long *cp2 = (const long *)((const u8 *)key2 + key_start); | |
a097c0b2 PS |
484 | long diffs = 0; |
485 | int i; | |
486 | ||
487 | for (i = key_start; i < key_end; i += sizeof(long)) | |
488 | diffs |= *cp1++ ^ *cp2++; | |
489 | ||
490 | return diffs == 0; | |
491 | } | |
492 | ||
493 | static bool flow_cmp_masked_key(const struct sw_flow *flow, | |
494 | const struct sw_flow_key *key, | |
0d8e6be8 | 495 | const struct sw_flow_key_range *range) |
a097c0b2 | 496 | { |
bc619e29 | 497 | return cmp_key(&flow->key, key, range->start, range->end); |
a097c0b2 PS |
498 | } |
499 | ||
91f83167 PS |
500 | static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, |
501 | const struct sw_flow_match *match) | |
a097c0b2 PS |
502 | { |
503 | struct sw_flow_key *key = match->key; | |
504 | int key_start = flow_key_start(key); | |
505 | int key_end = match->range.end; | |
506 | ||
bc619e29 JS |
507 | BUG_ON(ovs_identifier_is_ufid(&flow->id)); |
508 | return cmp_key(flow->id.unmasked_key, key, key_start, key_end); | |
a097c0b2 PS |
509 | } |
510 | ||
994dc286 | 511 | static struct sw_flow *masked_flow_lookup(struct table_instance *ti, |
a097c0b2 | 512 | const struct sw_flow_key *unmasked, |
f1f60b85 | 513 | const struct sw_flow_mask *mask, |
5604935e | 514 | u32 *n_mask_hit) |
a097c0b2 PS |
515 | { |
516 | struct sw_flow *flow; | |
517 | struct hlist_head *head; | |
a097c0b2 PS |
518 | u32 hash; |
519 | struct sw_flow_key masked_key; | |
520 | ||
ad4adec2 | 521 | ovs_flow_mask_key(&masked_key, unmasked, false, mask); |
0d8e6be8 | 522 | hash = flow_hash(&masked_key, &mask->range); |
994dc286 | 523 | head = find_bucket(ti, hash); |
5604935e | 524 | (*n_mask_hit)++; |
bc619e29 JS |
525 | hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) { |
526 | if (flow->mask == mask && flow->flow_table.hash == hash && | |
0d8e6be8 | 527 | flow_cmp_masked_key(flow, &masked_key, &mask->range)) |
a097c0b2 PS |
528 | return flow; |
529 | } | |
530 | return NULL; | |
531 | } | |
532 | ||
0e6efbe2 PS |
533 | /* Flow lookup does full lookup on flow table. It starts with |
534 | * mask from index passed in *index. | |
535 | */ | |
5604935e PS |
536 | static struct sw_flow *flow_lookup(struct flow_table *tbl, |
537 | struct table_instance *ti, | |
f1f60b85 | 538 | const struct mask_array *ma, |
5604935e | 539 | const struct sw_flow_key *key, |
d49fc3ff PS |
540 | u32 *n_mask_hit, |
541 | u32 *index) | |
a097c0b2 | 542 | { |
0e6efbe2 | 543 | struct sw_flow_mask *mask; |
994dc286 | 544 | struct sw_flow *flow; |
d49fc3ff | 545 | int i; |
a097c0b2 | 546 | |
0e6efbe2 PS |
547 | if (*index < ma->max) { |
548 | mask = rcu_dereference_ovsl(ma->masks[*index]); | |
549 | if (mask) { | |
550 | flow = masked_flow_lookup(ti, key, mask, n_mask_hit); | |
551 | if (flow) | |
552 | return flow; | |
553 | } | |
554 | } | |
555 | ||
556 | for (i = 0; i < ma->max; i++) { | |
557 | ||
558 | if (i == *index) | |
559 | continue; | |
d49fc3ff PS |
560 | |
561 | mask = rcu_dereference_ovsl(ma->masks[i]); | |
5fdbade3 | 562 | if (!mask) |
3dd6d3a2 | 563 | continue; |
5fdbade3 PS |
564 | |
565 | flow = masked_flow_lookup(ti, key, mask, n_mask_hit); | |
566 | if (flow) { /* Found */ | |
567 | *index = i; | |
568 | return flow; | |
d49fc3ff | 569 | } |
a097c0b2 | 570 | } |
d49fc3ff | 571 | |
994dc286 PS |
572 | return NULL; |
573 | } | |
a097c0b2 | 574 | |
5604935e PS |
575 | /* |
576 | * mask_cache maps flow to probable mask. This cache is not tightly | |
577 | * coupled cache, It means updates to mask list can result in inconsistent | |
578 | * cache entry in mask cache. | |
579 | * This is per cpu cache and is divided in MC_HASH_SEGS segments. | |
580 | * In case of a hash collision the entry is hashed in next segment. | |
af465b67 | 581 | */ |
5604935e PS |
582 | struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, |
583 | const struct sw_flow_key *key, | |
584 | u32 skb_hash, | |
585 | u32 *n_mask_hit) | |
586 | { | |
5fdbade3 PS |
587 | struct mask_array *ma = rcu_dereference(tbl->mask_array); |
588 | struct table_instance *ti = rcu_dereference(tbl->ti); | |
9e26a02a | 589 | struct mask_cache_entry *entries, *ce; |
5604935e | 590 | struct sw_flow *flow; |
28465887 | 591 | u32 hash; |
5604935e PS |
592 | int seg; |
593 | ||
594 | *n_mask_hit = 0; | |
d49fc3ff | 595 | if (unlikely(!skb_hash)) { |
0e6efbe2 | 596 | u32 mask_index = 0; |
d49fc3ff PS |
597 | |
598 | return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index); | |
599 | } | |
5604935e | 600 | |
28465887 AZ |
601 | /* Pre and post recirulation flows usually have the same skb_hash |
602 | * value. To avoid hash collisions, rehash the 'skb_hash' with | |
603 | * 'recirc_id'. */ | |
604 | if (key->recirc_id) | |
605 | skb_hash = jhash_1word(skb_hash, key->recirc_id); | |
606 | ||
9e26a02a | 607 | ce = NULL; |
28465887 | 608 | hash = skb_hash; |
5604935e PS |
609 | entries = this_cpu_ptr(tbl->mask_cache); |
610 | ||
9e26a02a | 611 | /* Find the cache entry 'ce' to operate on. */ |
5604935e | 612 | for (seg = 0; seg < MC_HASH_SEGS; seg++) { |
9e26a02a AZ |
613 | int index = hash & (MC_HASH_ENTRIES - 1); |
614 | struct mask_cache_entry *e; | |
5604935e | 615 | |
9e26a02a AZ |
616 | e = &entries[index]; |
617 | if (e->skb_hash == skb_hash) { | |
0e6efbe2 PS |
618 | flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, |
619 | &e->mask_index); | |
620 | if (!flow) | |
621 | e->skb_hash = 0; | |
622 | return flow; | |
5604935e PS |
623 | } |
624 | ||
9e26a02a AZ |
625 | if (!ce || e->skb_hash < ce->skb_hash) |
626 | ce = e; /* A better replacement cache candidate. */ | |
5604935e PS |
627 | |
628 | hash >>= MC_HASH_SHIFT; | |
629 | } | |
630 | ||
9e26a02a AZ |
631 | /* Cache miss, do full lookup. */ |
632 | flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &ce->mask_index); | |
d49fc3ff | 633 | if (flow) |
9e26a02a | 634 | ce->skb_hash = skb_hash; |
d49fc3ff | 635 | |
5604935e PS |
636 | return flow; |
637 | } | |
638 | ||
4f88b5e5 AZ |
639 | struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, |
640 | const struct sw_flow_key *key) | |
641 | { | |
5604935e | 642 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); |
d49fc3ff | 643 | struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array); |
4f88b5e5 | 644 | u32 __always_unused n_mask_hit; |
0e6efbe2 | 645 | u32 index = 0; |
4f88b5e5 | 646 | |
d49fc3ff | 647 | return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index); |
4f88b5e5 AZ |
648 | } |
649 | ||
3440e4bc | 650 | struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, |
f1f60b85 | 651 | const struct sw_flow_match *match) |
3440e4bc | 652 | { |
8fe27b20 | 653 | struct mask_array *ma = ovsl_dereference(tbl->mask_array); |
3440e4bc AW |
654 | int i; |
655 | ||
656 | /* Always called under ovs-mutex. */ | |
3dd6d3a2 | 657 | for (i = 0; i < ma->max; i++) { |
8fe27b20 PS |
658 | struct table_instance *ti = ovsl_dereference(tbl->ti); |
659 | u32 __always_unused n_mask_hit; | |
3440e4bc | 660 | struct sw_flow_mask *mask; |
8fe27b20 | 661 | struct sw_flow *flow; |
3440e4bc AW |
662 | |
663 | mask = ovsl_dereference(ma->masks[i]); | |
3dd6d3a2 PS |
664 | if (!mask) |
665 | continue; | |
8fe27b20 | 666 | flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit); |
bc619e29 JS |
667 | if (flow && ovs_identifier_is_key(&flow->id) && |
668 | ovs_flow_cmp_unmasked_key(flow, match)) | |
669 | return flow; | |
670 | } | |
671 | return NULL; | |
672 | } | |
673 | ||
674 | static u32 ufid_hash(const struct sw_flow_id *sfid) | |
675 | { | |
676 | return jhash(sfid->ufid, sfid->ufid_len, 0); | |
677 | } | |
678 | ||
679 | static bool ovs_flow_cmp_ufid(const struct sw_flow *flow, | |
680 | const struct sw_flow_id *sfid) | |
681 | { | |
682 | if (flow->id.ufid_len != sfid->ufid_len) | |
683 | return false; | |
684 | ||
685 | return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len); | |
686 | } | |
687 | ||
688 | bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match) | |
689 | { | |
690 | if (ovs_identifier_is_ufid(&flow->id)) | |
691 | return flow_cmp_masked_key(flow, match->key, &match->range); | |
692 | ||
693 | return ovs_flow_cmp_unmasked_key(flow, match); | |
694 | } | |
695 | ||
696 | struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl, | |
697 | const struct sw_flow_id *ufid) | |
698 | { | |
699 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti); | |
700 | struct sw_flow *flow; | |
701 | struct hlist_head *head; | |
702 | u32 hash; | |
703 | ||
704 | hash = ufid_hash(ufid); | |
705 | head = find_bucket(ti, hash); | |
706 | hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) { | |
707 | if (flow->ufid_table.hash == hash && | |
708 | ovs_flow_cmp_ufid(flow, ufid)) | |
8fe27b20 | 709 | return flow; |
3440e4bc AW |
710 | } |
711 | return NULL; | |
712 | } | |
713 | ||
4fa72a95 AZ |
714 | int ovs_flow_tbl_num_masks(const struct flow_table *table) |
715 | { | |
d49fc3ff | 716 | struct mask_array *ma; |
4fa72a95 | 717 | |
d49fc3ff PS |
718 | ma = rcu_dereference_ovsl(table->mask_array); |
719 | return ma->count; | |
4fa72a95 AZ |
720 | } |
721 | ||
bc619e29 JS |
722 | static struct table_instance *table_instance_expand(struct table_instance *ti, |
723 | bool ufid) | |
994dc286 | 724 | { |
bc619e29 | 725 | return table_instance_rehash(ti, ti->n_buckets * 2, ufid); |
a097c0b2 PS |
726 | } |
727 | ||
3dd6d3a2 PS |
728 | static void tbl_mask_array_delete_mask(struct mask_array *ma, |
729 | struct sw_flow_mask *mask) | |
730 | { | |
731 | int i; | |
732 | ||
733 | /* Remove the deleted mask pointers from the array */ | |
734 | for (i = 0; i < ma->max; i++) { | |
735 | if (mask == ovsl_dereference(ma->masks[i])) { | |
736 | RCU_INIT_POINTER(ma->masks[i], NULL); | |
737 | ma->count--; | |
8063e095 | 738 | kfree_rcu(mask, rcu); |
3dd6d3a2 PS |
739 | return; |
740 | } | |
741 | } | |
742 | BUG(); | |
743 | } | |
744 | ||
0641a4fb JR |
745 | /* Remove 'mask' from the mask list, if it is not needed any more. */ |
746 | static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) | |
747 | { | |
748 | if (mask) { | |
749 | /* ovs-lock is required to protect mask-refcount and | |
750 | * mask list. | |
751 | */ | |
752 | ASSERT_OVSL(); | |
753 | BUG_ON(!mask->ref_count); | |
754 | mask->ref_count--; | |
755 | ||
756 | if (!mask->ref_count) { | |
d49fc3ff | 757 | struct mask_array *ma; |
d49fc3ff PS |
758 | |
759 | ma = ovsl_dereference(tbl->mask_array); | |
3dd6d3a2 | 760 | tbl_mask_array_delete_mask(ma, mask); |
6ddb6313 | 761 | |
3dd6d3a2 PS |
762 | /* Shrink the mask array if necessary. */ |
763 | if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) && | |
764 | ma->count <= (ma->max / 3)) | |
6ddb6313 | 765 | tbl_mask_array_realloc(tbl, ma->max / 2); |
6ddb6313 | 766 | |
0641a4fb JR |
767 | } |
768 | } | |
769 | } | |
770 | ||
771 | /* Must be called with OVS mutex held. */ | |
a097c0b2 PS |
772 | void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) |
773 | { | |
994dc286 | 774 | struct table_instance *ti = ovsl_dereference(table->ti); |
bc619e29 | 775 | struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti); |
994dc286 | 776 | |
a097c0b2 | 777 | BUG_ON(table->count == 0); |
bc619e29 | 778 | hlist_del_rcu(&flow->flow_table.node[ti->node_ver]); |
a097c0b2 | 779 | table->count--; |
bc619e29 JS |
780 | if (ovs_identifier_is_ufid(&flow->id)) { |
781 | hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]); | |
782 | table->ufid_count--; | |
783 | } | |
0641a4fb JR |
784 | |
785 | /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be | |
af465b67 PS |
786 | * accessible as long as the RCU read lock is held. |
787 | */ | |
0641a4fb | 788 | flow_mask_remove(table, flow->mask); |
a097c0b2 PS |
789 | } |
790 | ||
0585f7a8 | 791 | static struct sw_flow_mask *mask_alloc(void) |
a097c0b2 PS |
792 | { |
793 | struct sw_flow_mask *mask; | |
794 | ||
795 | mask = kmalloc(sizeof(*mask), GFP_KERNEL); | |
796 | if (mask) | |
d103f479 | 797 | mask->ref_count = 1; |
a097c0b2 PS |
798 | |
799 | return mask; | |
800 | } | |
801 | ||
a097c0b2 PS |
802 | static bool mask_equal(const struct sw_flow_mask *a, |
803 | const struct sw_flow_mask *b) | |
804 | { | |
d15ae707 DDP |
805 | const u8 *a_ = (const u8 *)&a->key + a->range.start; |
806 | const u8 *b_ = (const u8 *)&b->key + b->range.start; | |
a097c0b2 PS |
807 | |
808 | return (a->range.end == b->range.end) | |
809 | && (a->range.start == b->range.start) | |
810 | && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0); | |
811 | } | |
812 | ||
0585f7a8 | 813 | static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, |
a097c0b2 PS |
814 | const struct sw_flow_mask *mask) |
815 | { | |
d49fc3ff PS |
816 | struct mask_array *ma; |
817 | int i; | |
818 | ||
819 | ma = ovsl_dereference(tbl->mask_array); | |
3dd6d3a2 | 820 | for (i = 0; i < ma->max; i++) { |
d49fc3ff | 821 | struct sw_flow_mask *t; |
a097c0b2 | 822 | |
d49fc3ff PS |
823 | t = ovsl_dereference(ma->masks[i]); |
824 | if (t && mask_equal(mask, t)) | |
825 | return t; | |
a097c0b2 PS |
826 | } |
827 | ||
828 | return NULL; | |
829 | } | |
830 | ||
a2f3ccc5 | 831 | /* Add 'mask' into the mask list, if it is not already there. */ |
0585f7a8 | 832 | static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, |
f1f60b85 | 833 | const struct sw_flow_mask *new) |
0585f7a8 PS |
834 | { |
835 | struct sw_flow_mask *mask; | |
d49fc3ff | 836 | |
0585f7a8 PS |
837 | mask = flow_mask_find(tbl, new); |
838 | if (!mask) { | |
d49fc3ff | 839 | struct mask_array *ma; |
3dd6d3a2 | 840 | int i; |
d49fc3ff | 841 | |
0585f7a8 PS |
842 | /* Allocate a new mask if none exsits. */ |
843 | mask = mask_alloc(); | |
844 | if (!mask) | |
845 | return -ENOMEM; | |
d49fc3ff | 846 | |
0585f7a8 PS |
847 | mask->key = new->key; |
848 | mask->range = new->range; | |
d49fc3ff PS |
849 | |
850 | /* Add mask to mask-list. */ | |
851 | ma = ovsl_dereference(tbl->mask_array); | |
852 | if (ma->count >= ma->max) { | |
853 | int err; | |
854 | ||
855 | err = tbl_mask_array_realloc(tbl, ma->max + | |
3dd6d3a2 | 856 | MASK_ARRAY_SIZE_MIN); |
d49fc3ff PS |
857 | if (err) { |
858 | kfree(mask); | |
859 | return err; | |
860 | } | |
861 | ma = ovsl_dereference(tbl->mask_array); | |
862 | } | |
6ddb6313 | 863 | |
3dd6d3a2 PS |
864 | for (i = 0; i < ma->max; i++) { |
865 | struct sw_flow_mask *t; | |
866 | ||
867 | t = ovsl_dereference(ma->masks[i]); | |
868 | if (!t) { | |
869 | rcu_assign_pointer(ma->masks[i], mask); | |
870 | ma->count++; | |
871 | break; | |
872 | } | |
873 | } | |
874 | ||
d103f479 AZ |
875 | } else { |
876 | BUG_ON(!mask->ref_count); | |
877 | mask->ref_count++; | |
0585f7a8 PS |
878 | } |
879 | ||
0585f7a8 PS |
880 | flow->mask = mask; |
881 | return 0; | |
882 | } | |
883 | ||
0641a4fb | 884 | /* Must be called with OVS mutex held. */ |
7bdf9b15 | 885 | static void flow_key_insert(struct flow_table *table, struct sw_flow *flow) |
a097c0b2 | 886 | { |
0585f7a8 PS |
887 | struct table_instance *new_ti = NULL; |
888 | struct table_instance *ti; | |
0585f7a8 | 889 | |
bc619e29 | 890 | flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range); |
0585f7a8 PS |
891 | ti = ovsl_dereference(table->ti); |
892 | table_instance_insert(ti, flow); | |
893 | table->count++; | |
894 | ||
895 | /* Expand table, if necessary, to make room. */ | |
896 | if (table->count > ti->n_buckets) | |
bc619e29 | 897 | new_ti = table_instance_expand(ti, false); |
0585f7a8 | 898 | else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL)) |
bc619e29 | 899 | new_ti = table_instance_rehash(ti, ti->n_buckets, false); |
0585f7a8 PS |
900 | |
901 | if (new_ti) { | |
902 | rcu_assign_pointer(table->ti, new_ti); | |
bc619e29 | 903 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); |
0585f7a8 PS |
904 | table->last_rehash = jiffies; |
905 | } | |
7bdf9b15 JS |
906 | } |
907 | ||
bc619e29 JS |
908 | /* Must be called with OVS mutex held. */ |
909 | static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow) | |
910 | { | |
911 | struct table_instance *ti; | |
912 | ||
913 | flow->ufid_table.hash = ufid_hash(&flow->id); | |
914 | ti = ovsl_dereference(table->ufid_ti); | |
915 | ufid_table_instance_insert(ti, flow); | |
916 | table->ufid_count++; | |
917 | ||
918 | /* Expand table, if necessary, to make room. */ | |
919 | if (table->ufid_count > ti->n_buckets) { | |
920 | struct table_instance *new_ti; | |
921 | ||
922 | new_ti = table_instance_expand(ti, true); | |
923 | if (new_ti) { | |
924 | rcu_assign_pointer(table->ufid_ti, new_ti); | |
925 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); | |
926 | } | |
927 | } | |
928 | } | |
929 | ||
7bdf9b15 JS |
930 | /* Must be called with OVS mutex held. */ |
931 | int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, | |
932 | const struct sw_flow_mask *mask) | |
933 | { | |
934 | int err; | |
935 | ||
936 | err = flow_mask_insert(table, flow, mask); | |
937 | if (err) | |
938 | return err; | |
939 | flow_key_insert(table, flow); | |
bc619e29 JS |
940 | if (ovs_identifier_is_ufid(&flow->id)) |
941 | flow_ufid_insert(table, flow); | |
7bdf9b15 | 942 | |
0585f7a8 | 943 | return 0; |
a097c0b2 PS |
944 | } |
945 | ||
946 | /* Initializes the flow module. | |
af465b67 PS |
947 | * Returns zero if successful or a negative error code. |
948 | */ | |
a097c0b2 PS |
949 | int ovs_flow_init(void) |
950 | { | |
951 | BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long)); | |
952 | BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long)); | |
953 | ||
9ac56358 | 954 | flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow) |
f7dc6d34 | 955 | + (nr_cpu_ids |
a2bdad67 | 956 | * sizeof(struct sw_flow_stats *)), |
9ac56358 | 957 | 0, 0, NULL); |
a097c0b2 PS |
958 | if (flow_cache == NULL) |
959 | return -ENOMEM; | |
960 | ||
9ac56358 | 961 | flow_stats_cache |
a2bdad67 | 962 | = kmem_cache_create("sw_flow_stats", sizeof(struct sw_flow_stats), |
9ac56358 JR |
963 | 0, SLAB_HWCACHE_ALIGN, NULL); |
964 | if (flow_stats_cache == NULL) { | |
965 | kmem_cache_destroy(flow_cache); | |
966 | flow_cache = NULL; | |
967 | return -ENOMEM; | |
968 | } | |
969 | ||
a097c0b2 PS |
970 | return 0; |
971 | } | |
972 | ||
973 | /* Uninitializes the flow module. */ | |
974 | void ovs_flow_exit(void) | |
975 | { | |
9ac56358 | 976 | kmem_cache_destroy(flow_stats_cache); |
a097c0b2 PS |
977 | kmem_cache_destroy(flow_cache); |
978 | } |