]>
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 | ||
6d1cf7f3 TZ |
237 | static int tbl_mask_array_add_mask(struct flow_table *tbl, |
238 | struct sw_flow_mask *new) | |
239 | { | |
240 | struct mask_array *ma = ovsl_dereference(tbl->mask_array); | |
241 | int err, ma_count = READ_ONCE(ma->count); | |
242 | ||
243 | if (ma_count >= ma->max) { | |
244 | err = tbl_mask_array_realloc(tbl, ma->max + | |
245 | MASK_ARRAY_SIZE_MIN); | |
246 | if (err) | |
247 | return err; | |
248 | ||
249 | ma = ovsl_dereference(tbl->mask_array); | |
250 | } | |
251 | ||
252 | BUG_ON(ovsl_dereference(ma->masks[ma_count])); | |
253 | ||
254 | rcu_assign_pointer(ma->masks[ma_count], new); | |
255 | WRITE_ONCE(ma->count, ma_count +1); | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
260 | static void tbl_mask_array_del_mask(struct flow_table *tbl, | |
261 | struct sw_flow_mask *mask) | |
262 | { | |
263 | struct mask_array *ma = ovsl_dereference(tbl->mask_array); | |
264 | int i, ma_count = READ_ONCE(ma->count); | |
265 | ||
266 | /* Remove the deleted mask pointers from the array */ | |
267 | for (i = 0; i < ma_count; i++) { | |
268 | if (mask == ovsl_dereference(ma->masks[i])) | |
269 | goto found; | |
270 | } | |
271 | ||
272 | BUG(); | |
273 | return; | |
274 | ||
275 | found: | |
276 | WRITE_ONCE(ma->count, ma_count -1); | |
277 | ||
278 | rcu_assign_pointer(ma->masks[i], ma->masks[ma_count -1]); | |
279 | RCU_INIT_POINTER(ma->masks[ma_count -1], NULL); | |
280 | ||
281 | kfree_rcu(mask, rcu); | |
282 | ||
283 | /* Shrink the mask array if necessary. */ | |
284 | if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) && | |
285 | ma_count <= (ma->max / 3)) | |
286 | tbl_mask_array_realloc(tbl, ma->max / 2); | |
287 | } | |
288 | ||
289 | /* Remove 'mask' from the mask list, if it is not needed any more. */ | |
290 | static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) | |
291 | { | |
292 | if (mask) { | |
293 | /* ovs-lock is required to protect mask-refcount and | |
294 | * mask list. | |
295 | */ | |
296 | ASSERT_OVSL(); | |
297 | BUG_ON(!mask->ref_count); | |
298 | mask->ref_count--; | |
299 | ||
300 | if (!mask->ref_count) | |
301 | tbl_mask_array_del_mask(tbl, mask); | |
302 | } | |
303 | } | |
304 | ||
994dc286 | 305 | int ovs_flow_tbl_init(struct flow_table *table) |
a097c0b2 | 306 | { |
bc619e29 | 307 | struct table_instance *ti, *ufid_ti; |
d49fc3ff | 308 | struct mask_array *ma; |
a097c0b2 | 309 | |
5604935e PS |
310 | table->mask_cache = __alloc_percpu(sizeof(struct mask_cache_entry) * |
311 | MC_HASH_ENTRIES, __alignof__(struct mask_cache_entry)); | |
312 | if (!table->mask_cache) | |
313 | return -ENOMEM; | |
a097c0b2 | 314 | |
d49fc3ff PS |
315 | ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN); |
316 | if (!ma) | |
317 | goto free_mask_cache; | |
318 | ||
5604935e | 319 | ti = table_instance_alloc(TBL_MIN_BUCKETS); |
d49fc3ff PS |
320 | if (!ti) |
321 | goto free_mask_array; | |
a097c0b2 | 322 | |
bc619e29 JS |
323 | ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
324 | if (!ufid_ti) | |
325 | goto free_ti; | |
326 | ||
994dc286 | 327 | rcu_assign_pointer(table->ti, ti); |
bc619e29 | 328 | rcu_assign_pointer(table->ufid_ti, ufid_ti); |
d49fc3ff | 329 | rcu_assign_pointer(table->mask_array, ma); |
994dc286 PS |
330 | table->last_rehash = jiffies; |
331 | table->count = 0; | |
bc619e29 | 332 | table->ufid_count = 0; |
994dc286 | 333 | return 0; |
d49fc3ff | 334 | |
bc619e29 JS |
335 | free_ti: |
336 | __table_instance_destroy(ti); | |
d49fc3ff | 337 | free_mask_array: |
3dd6d3a2 | 338 | kfree(ma); |
d49fc3ff PS |
339 | free_mask_cache: |
340 | free_percpu(table->mask_cache); | |
341 | return -ENOMEM; | |
a097c0b2 PS |
342 | } |
343 | ||
344 | static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) | |
345 | { | |
994dc286 | 346 | struct table_instance *ti = container_of(rcu, struct table_instance, rcu); |
a097c0b2 | 347 | |
994dc286 | 348 | __table_instance_destroy(ti); |
a097c0b2 PS |
349 | } |
350 | ||
6d1cf7f3 TZ |
351 | static void table_instance_flow_free(struct flow_table *table, |
352 | struct table_instance *ti, | |
353 | struct table_instance *ufid_ti, | |
354 | struct sw_flow *flow, | |
355 | bool count) | |
356 | { | |
357 | hlist_del_rcu(&flow->flow_table.node[ti->node_ver]); | |
358 | if (count) | |
359 | table->count--; | |
360 | ||
361 | if (ovs_identifier_is_ufid(&flow->id)) { | |
362 | hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]); | |
363 | ||
364 | if (count) | |
365 | table->ufid_count--; | |
366 | } | |
367 | ||
368 | flow_mask_remove(table, flow->mask); | |
369 | } | |
370 | ||
371 | static void table_instance_destroy(struct flow_table *table, | |
372 | struct table_instance *ti, | |
bc619e29 JS |
373 | struct table_instance *ufid_ti, |
374 | bool deferred) | |
a097c0b2 | 375 | { |
d103f479 AZ |
376 | int i; |
377 | ||
994dc286 | 378 | if (!ti) |
a097c0b2 PS |
379 | return; |
380 | ||
bc619e29 | 381 | BUG_ON(!ufid_ti); |
d103f479 AZ |
382 | if (ti->keep_flows) |
383 | goto skip_flows; | |
384 | ||
385 | for (i = 0; i < ti->n_buckets; i++) { | |
386 | struct sw_flow *flow; | |
4383e54b | 387 | struct hlist_head *head = &ti->buckets[i]; |
d103f479 | 388 | struct hlist_node *n; |
d103f479 | 389 | |
6d1cf7f3 TZ |
390 | hlist_for_each_entry_safe(flow, n, head, |
391 | flow_table.node[ti->node_ver]) { | |
392 | ||
393 | table_instance_flow_free(table, ti, ufid_ti, | |
394 | flow, false); | |
d103f479 AZ |
395 | ovs_flow_free(flow, deferred); |
396 | } | |
397 | } | |
398 | ||
399 | skip_flows: | |
bc619e29 | 400 | if (deferred) { |
994dc286 | 401 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); |
bc619e29 JS |
402 | call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb); |
403 | } else { | |
994dc286 | 404 | __table_instance_destroy(ti); |
bc619e29 JS |
405 | __table_instance_destroy(ufid_ti); |
406 | } | |
994dc286 PS |
407 | } |
408 | ||
e379e4d1 | 409 | /* No need for locking this function is called from RCU callback or |
af465b67 PS |
410 | * error path. |
411 | */ | |
e379e4d1 | 412 | void ovs_flow_tbl_destroy(struct flow_table *table) |
994dc286 | 413 | { |
46051cf8 | 414 | struct table_instance *ti = rcu_dereference_raw(table->ti); |
bc619e29 | 415 | struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti); |
994dc286 | 416 | |
5604935e | 417 | free_percpu(table->mask_cache); |
46051cf8 | 418 | kfree(rcu_dereference_raw(table->mask_array)); |
6d1cf7f3 | 419 | table_instance_destroy(table, ti, ufid_ti, false); |
a097c0b2 PS |
420 | } |
421 | ||
994dc286 | 422 | struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti, |
a097c0b2 PS |
423 | u32 *bucket, u32 *last) |
424 | { | |
425 | struct sw_flow *flow; | |
426 | struct hlist_head *head; | |
427 | int ver; | |
428 | int i; | |
429 | ||
994dc286 PS |
430 | ver = ti->node_ver; |
431 | while (*bucket < ti->n_buckets) { | |
a097c0b2 | 432 | i = 0; |
4383e54b | 433 | head = &ti->buckets[*bucket]; |
bc619e29 | 434 | hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) { |
a097c0b2 PS |
435 | if (i < *last) { |
436 | i++; | |
437 | continue; | |
438 | } | |
439 | *last = i + 1; | |
440 | return flow; | |
441 | } | |
442 | (*bucket)++; | |
443 | *last = 0; | |
444 | } | |
445 | ||
446 | return NULL; | |
447 | } | |
448 | ||
994dc286 | 449 | static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash) |
a097c0b2 | 450 | { |
994dc286 | 451 | hash = jhash_1word(hash, ti->hash_seed); |
4383e54b | 452 | return &ti->buckets[hash & (ti->n_buckets - 1)]; |
a097c0b2 PS |
453 | } |
454 | ||
bc619e29 JS |
455 | static void table_instance_insert(struct table_instance *ti, |
456 | struct sw_flow *flow) | |
a097c0b2 PS |
457 | { |
458 | struct hlist_head *head; | |
459 | ||
bc619e29 JS |
460 | head = find_bucket(ti, flow->flow_table.hash); |
461 | hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head); | |
462 | } | |
463 | ||
464 | static void ufid_table_instance_insert(struct table_instance *ti, | |
465 | struct sw_flow *flow) | |
466 | { | |
467 | struct hlist_head *head; | |
468 | ||
469 | head = find_bucket(ti, flow->ufid_table.hash); | |
470 | hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head); | |
a097c0b2 PS |
471 | } |
472 | ||
994dc286 | 473 | static void flow_table_copy_flows(struct table_instance *old, |
bc619e29 | 474 | struct table_instance *new, bool ufid) |
a097c0b2 PS |
475 | { |
476 | int old_ver; | |
477 | int i; | |
478 | ||
479 | old_ver = old->node_ver; | |
480 | new->node_ver = !old_ver; | |
481 | ||
482 | /* Insert in new table. */ | |
483 | for (i = 0; i < old->n_buckets; i++) { | |
484 | struct sw_flow *flow; | |
4383e54b | 485 | struct hlist_head *head = &old->buckets[i]; |
a097c0b2 | 486 | |
bc619e29 | 487 | if (ufid) |
afe72101 TZ |
488 | hlist_for_each_entry_rcu(flow, head, |
489 | ufid_table.node[old_ver]) | |
bc619e29 JS |
490 | ufid_table_instance_insert(new, flow); |
491 | else | |
afe72101 TZ |
492 | hlist_for_each_entry_rcu(flow, head, |
493 | flow_table.node[old_ver]) | |
bc619e29 | 494 | table_instance_insert(new, flow); |
a097c0b2 PS |
495 | } |
496 | ||
a097c0b2 PS |
497 | old->keep_flows = true; |
498 | } | |
499 | ||
994dc286 | 500 | static struct table_instance *table_instance_rehash(struct table_instance *ti, |
bc619e29 | 501 | int n_buckets, bool ufid) |
a097c0b2 | 502 | { |
994dc286 | 503 | struct table_instance *new_ti; |
a097c0b2 | 504 | |
994dc286 PS |
505 | new_ti = table_instance_alloc(n_buckets); |
506 | if (!new_ti) | |
0585f7a8 | 507 | return NULL; |
a097c0b2 | 508 | |
bc619e29 | 509 | flow_table_copy_flows(ti, new_ti, ufid); |
a097c0b2 | 510 | |
994dc286 | 511 | return new_ti; |
a097c0b2 PS |
512 | } |
513 | ||
994dc286 | 514 | int ovs_flow_tbl_flush(struct flow_table *flow_table) |
a097c0b2 | 515 | { |
bc619e29 JS |
516 | struct table_instance *old_ti, *new_ti; |
517 | struct table_instance *old_ufid_ti, *new_ufid_ti; | |
a097c0b2 | 518 | |
994dc286 PS |
519 | new_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
520 | if (!new_ti) | |
521 | return -ENOMEM; | |
bc619e29 JS |
522 | new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
523 | if (!new_ufid_ti) | |
524 | goto err_free_ti; | |
525 | ||
526 | old_ti = ovsl_dereference(flow_table->ti); | |
527 | old_ufid_ti = ovsl_dereference(flow_table->ufid_ti); | |
994dc286 PS |
528 | |
529 | rcu_assign_pointer(flow_table->ti, new_ti); | |
bc619e29 | 530 | rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti); |
994dc286 PS |
531 | flow_table->last_rehash = jiffies; |
532 | flow_table->count = 0; | |
bc619e29 | 533 | flow_table->ufid_count = 0; |
994dc286 | 534 | |
6d1cf7f3 | 535 | table_instance_destroy(flow_table, old_ti, old_ufid_ti, true); |
994dc286 | 536 | return 0; |
bc619e29 JS |
537 | |
538 | err_free_ti: | |
539 | __table_instance_destroy(new_ti); | |
540 | return -ENOMEM; | |
a097c0b2 PS |
541 | } |
542 | ||
0d8e6be8 JS |
543 | static u32 flow_hash(const struct sw_flow_key *key, |
544 | const struct sw_flow_key_range *range) | |
a097c0b2 | 545 | { |
cdc58fe7 | 546 | const u32 *hash_key = (const u32 *)((const u8 *)key + range->start); |
a097c0b2 PS |
547 | |
548 | /* Make sure number of hash bytes are multiple of u32. */ | |
cdc58fe7 | 549 | int hash_u32s = range_n_bytes(range) >> 2; |
a097c0b2 | 550 | |
1dfb9f31 | 551 | return jhash2(hash_key, hash_u32s, 0); |
a097c0b2 PS |
552 | } |
553 | ||
554 | static int flow_key_start(const struct sw_flow_key *key) | |
555 | { | |
bdcbce89 | 556 | if (key->tun_proto) |
a097c0b2 PS |
557 | return 0; |
558 | else | |
559 | return rounddown(offsetof(struct sw_flow_key, phy), | |
560 | sizeof(long)); | |
561 | } | |
562 | ||
563 | static bool cmp_key(const struct sw_flow_key *key1, | |
564 | const struct sw_flow_key *key2, | |
565 | int key_start, int key_end) | |
566 | { | |
d15ae707 DDP |
567 | const long *cp1 = (const long *)((const u8 *)key1 + key_start); |
568 | const long *cp2 = (const long *)((const u8 *)key2 + key_start); | |
a097c0b2 PS |
569 | long diffs = 0; |
570 | int i; | |
571 | ||
572 | for (i = key_start; i < key_end; i += sizeof(long)) | |
573 | diffs |= *cp1++ ^ *cp2++; | |
574 | ||
575 | return diffs == 0; | |
576 | } | |
577 | ||
578 | static bool flow_cmp_masked_key(const struct sw_flow *flow, | |
579 | const struct sw_flow_key *key, | |
0d8e6be8 | 580 | const struct sw_flow_key_range *range) |
a097c0b2 | 581 | { |
bc619e29 | 582 | return cmp_key(&flow->key, key, range->start, range->end); |
a097c0b2 PS |
583 | } |
584 | ||
91f83167 PS |
585 | static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, |
586 | const struct sw_flow_match *match) | |
a097c0b2 PS |
587 | { |
588 | struct sw_flow_key *key = match->key; | |
589 | int key_start = flow_key_start(key); | |
590 | int key_end = match->range.end; | |
591 | ||
bc619e29 JS |
592 | BUG_ON(ovs_identifier_is_ufid(&flow->id)); |
593 | return cmp_key(flow->id.unmasked_key, key, key_start, key_end); | |
a097c0b2 PS |
594 | } |
595 | ||
994dc286 | 596 | static struct sw_flow *masked_flow_lookup(struct table_instance *ti, |
a097c0b2 | 597 | const struct sw_flow_key *unmasked, |
f1f60b85 | 598 | const struct sw_flow_mask *mask, |
5604935e | 599 | u32 *n_mask_hit) |
a097c0b2 PS |
600 | { |
601 | struct sw_flow *flow; | |
602 | struct hlist_head *head; | |
a097c0b2 PS |
603 | u32 hash; |
604 | struct sw_flow_key masked_key; | |
605 | ||
ad4adec2 | 606 | ovs_flow_mask_key(&masked_key, unmasked, false, mask); |
0d8e6be8 | 607 | hash = flow_hash(&masked_key, &mask->range); |
994dc286 | 608 | head = find_bucket(ti, hash); |
5604935e | 609 | (*n_mask_hit)++; |
bc619e29 JS |
610 | hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) { |
611 | if (flow->mask == mask && flow->flow_table.hash == hash && | |
0d8e6be8 | 612 | flow_cmp_masked_key(flow, &masked_key, &mask->range)) |
a097c0b2 PS |
613 | return flow; |
614 | } | |
615 | return NULL; | |
616 | } | |
617 | ||
0e6efbe2 PS |
618 | /* Flow lookup does full lookup on flow table. It starts with |
619 | * mask from index passed in *index. | |
620 | */ | |
5604935e PS |
621 | static struct sw_flow *flow_lookup(struct flow_table *tbl, |
622 | struct table_instance *ti, | |
f1f60b85 | 623 | const struct mask_array *ma, |
5604935e | 624 | const struct sw_flow_key *key, |
d49fc3ff PS |
625 | u32 *n_mask_hit, |
626 | u32 *index) | |
a097c0b2 | 627 | { |
994dc286 | 628 | struct sw_flow *flow; |
fef7abfc | 629 | struct sw_flow_mask *mask; |
d49fc3ff | 630 | int i; |
a097c0b2 | 631 | |
e5466316 | 632 | if (likely(*index < ma->max)) { |
0e6efbe2 PS |
633 | mask = rcu_dereference_ovsl(ma->masks[*index]); |
634 | if (mask) { | |
635 | flow = masked_flow_lookup(ti, key, mask, n_mask_hit); | |
636 | if (flow) | |
637 | return flow; | |
638 | } | |
639 | } | |
640 | ||
641 | for (i = 0; i < ma->max; i++) { | |
642 | ||
643 | if (i == *index) | |
644 | continue; | |
d49fc3ff PS |
645 | |
646 | mask = rcu_dereference_ovsl(ma->masks[i]); | |
e5466316 | 647 | if (unlikely(!mask)) |
fef7abfc | 648 | break; |
5fdbade3 PS |
649 | |
650 | flow = masked_flow_lookup(ti, key, mask, n_mask_hit); | |
651 | if (flow) { /* Found */ | |
652 | *index = i; | |
653 | return flow; | |
d49fc3ff | 654 | } |
a097c0b2 | 655 | } |
d49fc3ff | 656 | |
994dc286 PS |
657 | return NULL; |
658 | } | |
a097c0b2 | 659 | |
5604935e PS |
660 | /* |
661 | * mask_cache maps flow to probable mask. This cache is not tightly | |
662 | * coupled cache, It means updates to mask list can result in inconsistent | |
663 | * cache entry in mask cache. | |
664 | * This is per cpu cache and is divided in MC_HASH_SEGS segments. | |
665 | * In case of a hash collision the entry is hashed in next segment. | |
af465b67 | 666 | */ |
5604935e PS |
667 | struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, |
668 | const struct sw_flow_key *key, | |
669 | u32 skb_hash, | |
670 | u32 *n_mask_hit) | |
671 | { | |
5fdbade3 PS |
672 | struct mask_array *ma = rcu_dereference(tbl->mask_array); |
673 | struct table_instance *ti = rcu_dereference(tbl->ti); | |
9e26a02a | 674 | struct mask_cache_entry *entries, *ce; |
5604935e | 675 | struct sw_flow *flow; |
28465887 | 676 | u32 hash; |
5604935e PS |
677 | int seg; |
678 | ||
679 | *n_mask_hit = 0; | |
d49fc3ff | 680 | if (unlikely(!skb_hash)) { |
0e6efbe2 | 681 | u32 mask_index = 0; |
d49fc3ff PS |
682 | |
683 | return flow_lookup(tbl, ti, ma, key, n_mask_hit, &mask_index); | |
684 | } | |
5604935e | 685 | |
28465887 AZ |
686 | /* Pre and post recirulation flows usually have the same skb_hash |
687 | * value. To avoid hash collisions, rehash the 'skb_hash' with | |
688 | * 'recirc_id'. */ | |
689 | if (key->recirc_id) | |
690 | skb_hash = jhash_1word(skb_hash, key->recirc_id); | |
691 | ||
9e26a02a | 692 | ce = NULL; |
28465887 | 693 | hash = skb_hash; |
5604935e PS |
694 | entries = this_cpu_ptr(tbl->mask_cache); |
695 | ||
9e26a02a | 696 | /* Find the cache entry 'ce' to operate on. */ |
5604935e | 697 | for (seg = 0; seg < MC_HASH_SEGS; seg++) { |
9e26a02a AZ |
698 | int index = hash & (MC_HASH_ENTRIES - 1); |
699 | struct mask_cache_entry *e; | |
5604935e | 700 | |
9e26a02a AZ |
701 | e = &entries[index]; |
702 | if (e->skb_hash == skb_hash) { | |
0e6efbe2 PS |
703 | flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, |
704 | &e->mask_index); | |
705 | if (!flow) | |
706 | e->skb_hash = 0; | |
707 | return flow; | |
5604935e PS |
708 | } |
709 | ||
9e26a02a AZ |
710 | if (!ce || e->skb_hash < ce->skb_hash) |
711 | ce = e; /* A better replacement cache candidate. */ | |
5604935e PS |
712 | |
713 | hash >>= MC_HASH_SHIFT; | |
714 | } | |
715 | ||
9e26a02a AZ |
716 | /* Cache miss, do full lookup. */ |
717 | flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, &ce->mask_index); | |
d49fc3ff | 718 | if (flow) |
9e26a02a | 719 | ce->skb_hash = skb_hash; |
d49fc3ff | 720 | |
5604935e PS |
721 | return flow; |
722 | } | |
723 | ||
4f88b5e5 AZ |
724 | struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, |
725 | const struct sw_flow_key *key) | |
726 | { | |
5604935e | 727 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); |
d49fc3ff | 728 | struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array); |
4f88b5e5 | 729 | u32 __always_unused n_mask_hit; |
0e6efbe2 | 730 | u32 index = 0; |
4f88b5e5 | 731 | |
d49fc3ff | 732 | return flow_lookup(tbl, ti, ma, key, &n_mask_hit, &index); |
4f88b5e5 AZ |
733 | } |
734 | ||
3440e4bc | 735 | struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, |
f1f60b85 | 736 | const struct sw_flow_match *match) |
3440e4bc | 737 | { |
8fe27b20 | 738 | struct mask_array *ma = ovsl_dereference(tbl->mask_array); |
3440e4bc AW |
739 | int i; |
740 | ||
741 | /* Always called under ovs-mutex. */ | |
3dd6d3a2 | 742 | for (i = 0; i < ma->max; i++) { |
8fe27b20 PS |
743 | struct table_instance *ti = ovsl_dereference(tbl->ti); |
744 | u32 __always_unused n_mask_hit; | |
3440e4bc | 745 | struct sw_flow_mask *mask; |
8fe27b20 | 746 | struct sw_flow *flow; |
3440e4bc AW |
747 | |
748 | mask = ovsl_dereference(ma->masks[i]); | |
3dd6d3a2 PS |
749 | if (!mask) |
750 | continue; | |
8fe27b20 | 751 | flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit); |
bc619e29 JS |
752 | if (flow && ovs_identifier_is_key(&flow->id) && |
753 | ovs_flow_cmp_unmasked_key(flow, match)) | |
754 | return flow; | |
755 | } | |
756 | return NULL; | |
757 | } | |
758 | ||
759 | static u32 ufid_hash(const struct sw_flow_id *sfid) | |
760 | { | |
761 | return jhash(sfid->ufid, sfid->ufid_len, 0); | |
762 | } | |
763 | ||
764 | static bool ovs_flow_cmp_ufid(const struct sw_flow *flow, | |
765 | const struct sw_flow_id *sfid) | |
766 | { | |
767 | if (flow->id.ufid_len != sfid->ufid_len) | |
768 | return false; | |
769 | ||
770 | return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len); | |
771 | } | |
772 | ||
773 | bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match) | |
774 | { | |
775 | if (ovs_identifier_is_ufid(&flow->id)) | |
776 | return flow_cmp_masked_key(flow, match->key, &match->range); | |
777 | ||
778 | return ovs_flow_cmp_unmasked_key(flow, match); | |
779 | } | |
780 | ||
781 | struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl, | |
782 | const struct sw_flow_id *ufid) | |
783 | { | |
784 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti); | |
785 | struct sw_flow *flow; | |
786 | struct hlist_head *head; | |
787 | u32 hash; | |
788 | ||
789 | hash = ufid_hash(ufid); | |
790 | head = find_bucket(ti, hash); | |
791 | hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) { | |
792 | if (flow->ufid_table.hash == hash && | |
793 | ovs_flow_cmp_ufid(flow, ufid)) | |
8fe27b20 | 794 | return flow; |
3440e4bc AW |
795 | } |
796 | return NULL; | |
797 | } | |
798 | ||
4fa72a95 AZ |
799 | int ovs_flow_tbl_num_masks(const struct flow_table *table) |
800 | { | |
d49fc3ff | 801 | struct mask_array *ma; |
4fa72a95 | 802 | |
d49fc3ff | 803 | ma = rcu_dereference_ovsl(table->mask_array); |
fef7abfc | 804 | return READ_ONCE(ma->count); |
4fa72a95 AZ |
805 | } |
806 | ||
bc619e29 JS |
807 | static struct table_instance *table_instance_expand(struct table_instance *ti, |
808 | bool ufid) | |
994dc286 | 809 | { |
bc619e29 | 810 | return table_instance_rehash(ti, ti->n_buckets * 2, ufid); |
a097c0b2 PS |
811 | } |
812 | ||
0641a4fb | 813 | /* Must be called with OVS mutex held. */ |
a097c0b2 PS |
814 | void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) |
815 | { | |
994dc286 | 816 | struct table_instance *ti = ovsl_dereference(table->ti); |
bc619e29 | 817 | struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti); |
994dc286 | 818 | |
a097c0b2 | 819 | BUG_ON(table->count == 0); |
6d1cf7f3 | 820 | table_instance_flow_free(table, ti, ufid_ti, flow, true); |
a097c0b2 PS |
821 | } |
822 | ||
0585f7a8 | 823 | static struct sw_flow_mask *mask_alloc(void) |
a097c0b2 PS |
824 | { |
825 | struct sw_flow_mask *mask; | |
826 | ||
827 | mask = kmalloc(sizeof(*mask), GFP_KERNEL); | |
828 | if (mask) | |
d103f479 | 829 | mask->ref_count = 1; |
a097c0b2 PS |
830 | |
831 | return mask; | |
832 | } | |
833 | ||
a097c0b2 PS |
834 | static bool mask_equal(const struct sw_flow_mask *a, |
835 | const struct sw_flow_mask *b) | |
836 | { | |
d15ae707 DDP |
837 | const u8 *a_ = (const u8 *)&a->key + a->range.start; |
838 | const u8 *b_ = (const u8 *)&b->key + b->range.start; | |
a097c0b2 PS |
839 | |
840 | return (a->range.end == b->range.end) | |
841 | && (a->range.start == b->range.start) | |
842 | && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0); | |
843 | } | |
844 | ||
0585f7a8 | 845 | static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, |
a097c0b2 PS |
846 | const struct sw_flow_mask *mask) |
847 | { | |
d49fc3ff PS |
848 | struct mask_array *ma; |
849 | int i; | |
850 | ||
851 | ma = ovsl_dereference(tbl->mask_array); | |
3dd6d3a2 | 852 | for (i = 0; i < ma->max; i++) { |
d49fc3ff | 853 | struct sw_flow_mask *t; |
a097c0b2 | 854 | |
d49fc3ff PS |
855 | t = ovsl_dereference(ma->masks[i]); |
856 | if (t && mask_equal(mask, t)) | |
857 | return t; | |
a097c0b2 PS |
858 | } |
859 | ||
860 | return NULL; | |
861 | } | |
862 | ||
a2f3ccc5 | 863 | /* Add 'mask' into the mask list, if it is not already there. */ |
0585f7a8 | 864 | static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, |
f1f60b85 | 865 | const struct sw_flow_mask *new) |
0585f7a8 PS |
866 | { |
867 | struct sw_flow_mask *mask; | |
d49fc3ff | 868 | |
0585f7a8 PS |
869 | mask = flow_mask_find(tbl, new); |
870 | if (!mask) { | |
871 | /* Allocate a new mask if none exsits. */ | |
872 | mask = mask_alloc(); | |
873 | if (!mask) | |
874 | return -ENOMEM; | |
d49fc3ff | 875 | |
0585f7a8 PS |
876 | mask->key = new->key; |
877 | mask->range = new->range; | |
d49fc3ff PS |
878 | |
879 | /* Add mask to mask-list. */ | |
fef7abfc TZ |
880 | if (tbl_mask_array_add_mask(tbl, mask)) { |
881 | kfree(mask); | |
882 | return -ENOMEM; | |
3dd6d3a2 PS |
883 | } |
884 | ||
d103f479 AZ |
885 | } else { |
886 | BUG_ON(!mask->ref_count); | |
887 | mask->ref_count++; | |
0585f7a8 PS |
888 | } |
889 | ||
0585f7a8 PS |
890 | flow->mask = mask; |
891 | return 0; | |
892 | } | |
893 | ||
0641a4fb | 894 | /* Must be called with OVS mutex held. */ |
7bdf9b15 | 895 | static void flow_key_insert(struct flow_table *table, struct sw_flow *flow) |
a097c0b2 | 896 | { |
0585f7a8 PS |
897 | struct table_instance *new_ti = NULL; |
898 | struct table_instance *ti; | |
0585f7a8 | 899 | |
bc619e29 | 900 | flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range); |
0585f7a8 PS |
901 | ti = ovsl_dereference(table->ti); |
902 | table_instance_insert(ti, flow); | |
903 | table->count++; | |
904 | ||
905 | /* Expand table, if necessary, to make room. */ | |
906 | if (table->count > ti->n_buckets) | |
bc619e29 | 907 | new_ti = table_instance_expand(ti, false); |
0585f7a8 | 908 | else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL)) |
bc619e29 | 909 | new_ti = table_instance_rehash(ti, ti->n_buckets, false); |
0585f7a8 PS |
910 | |
911 | if (new_ti) { | |
912 | rcu_assign_pointer(table->ti, new_ti); | |
bc619e29 | 913 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); |
0585f7a8 PS |
914 | table->last_rehash = jiffies; |
915 | } | |
7bdf9b15 JS |
916 | } |
917 | ||
bc619e29 JS |
918 | /* Must be called with OVS mutex held. */ |
919 | static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow) | |
920 | { | |
921 | struct table_instance *ti; | |
922 | ||
923 | flow->ufid_table.hash = ufid_hash(&flow->id); | |
924 | ti = ovsl_dereference(table->ufid_ti); | |
925 | ufid_table_instance_insert(ti, flow); | |
926 | table->ufid_count++; | |
927 | ||
928 | /* Expand table, if necessary, to make room. */ | |
929 | if (table->ufid_count > ti->n_buckets) { | |
930 | struct table_instance *new_ti; | |
931 | ||
932 | new_ti = table_instance_expand(ti, true); | |
933 | if (new_ti) { | |
934 | rcu_assign_pointer(table->ufid_ti, new_ti); | |
935 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); | |
936 | } | |
937 | } | |
938 | } | |
939 | ||
7bdf9b15 JS |
940 | /* Must be called with OVS mutex held. */ |
941 | int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, | |
942 | const struct sw_flow_mask *mask) | |
943 | { | |
944 | int err; | |
945 | ||
946 | err = flow_mask_insert(table, flow, mask); | |
947 | if (err) | |
948 | return err; | |
949 | flow_key_insert(table, flow); | |
bc619e29 JS |
950 | if (ovs_identifier_is_ufid(&flow->id)) |
951 | flow_ufid_insert(table, flow); | |
7bdf9b15 | 952 | |
0585f7a8 | 953 | return 0; |
a097c0b2 PS |
954 | } |
955 | ||
956 | /* Initializes the flow module. | |
af465b67 PS |
957 | * Returns zero if successful or a negative error code. |
958 | */ | |
a097c0b2 PS |
959 | int ovs_flow_init(void) |
960 | { | |
961 | BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long)); | |
962 | BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long)); | |
963 | ||
9ac56358 | 964 | flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow) |
f7dc6d34 | 965 | + (nr_cpu_ids |
a2bdad67 | 966 | * sizeof(struct sw_flow_stats *)), |
9ac56358 | 967 | 0, 0, NULL); |
a097c0b2 PS |
968 | if (flow_cache == NULL) |
969 | return -ENOMEM; | |
970 | ||
9ac56358 | 971 | flow_stats_cache |
a2bdad67 | 972 | = kmem_cache_create("sw_flow_stats", sizeof(struct sw_flow_stats), |
9ac56358 JR |
973 | 0, SLAB_HWCACHE_ALIGN, NULL); |
974 | if (flow_stats_cache == NULL) { | |
975 | kmem_cache_destroy(flow_cache); | |
976 | flow_cache = NULL; | |
977 | return -ENOMEM; | |
978 | } | |
979 | ||
a097c0b2 PS |
980 | return 0; |
981 | } | |
982 | ||
983 | /* Uninitializes the flow module. */ | |
984 | void ovs_flow_exit(void) | |
985 | { | |
9ac56358 | 986 | kmem_cache_destroy(flow_stats_cache); |
a097c0b2 PS |
987 | kmem_cache_destroy(flow_cache); |
988 | } |