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