]>
Commit | Line | Data |
---|---|---|
e6445719 | 1 | /* |
9b996e54 | 2 | * Copyright (c) 2007-2014 Nicira, Inc. |
e6445719 PS |
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" | |
34ae932a | 21 | #include "flow_netlink.h" |
e6445719 PS |
22 | #include <linux/uaccess.h> |
23 | #include <linux/netdevice.h> | |
24 | #include <linux/etherdevice.h> | |
25 | #include <linux/if_ether.h> | |
26 | #include <linux/if_vlan.h> | |
27 | #include <net/llc_pdu.h> | |
28 | #include <linux/kernel.h> | |
87545899 | 29 | #include <linux/jhash.h> |
e6445719 PS |
30 | #include <linux/jiffies.h> |
31 | #include <linux/llc.h> | |
32 | #include <linux/module.h> | |
33 | #include <linux/in.h> | |
34 | #include <linux/rcupdate.h> | |
35 | #include <linux/if_arp.h> | |
36 | #include <linux/ip.h> | |
37 | #include <linux/ipv6.h> | |
38 | #include <linux/sctp.h> | |
39 | #include <linux/tcp.h> | |
40 | #include <linux/udp.h> | |
41 | #include <linux/icmp.h> | |
42 | #include <linux/icmpv6.h> | |
43 | #include <linux/rculist.h> | |
44 | #include <net/ip.h> | |
45 | #include <net/ipv6.h> | |
46 | #include <net/ndisc.h> | |
47 | ||
b637e498 PS |
48 | #define TBL_MIN_BUCKETS 1024 |
49 | #define REHASH_INTERVAL (10 * 60 * HZ) | |
50 | ||
e6445719 | 51 | static struct kmem_cache *flow_cache; |
63e7959c | 52 | struct kmem_cache *flow_stats_cache __read_mostly; |
e6445719 PS |
53 | |
54 | static u16 range_n_bytes(const struct sw_flow_key_range *range) | |
55 | { | |
56 | return range->end - range->start; | |
57 | } | |
58 | ||
59 | void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src, | |
ae5f2fb1 | 60 | bool full, const struct sw_flow_mask *mask) |
e6445719 | 61 | { |
ae5f2fb1 JG |
62 | int start = full ? 0 : mask->range.start; |
63 | int len = full ? sizeof *dst : range_n_bytes(&mask->range); | |
64 | const long *m = (const long *)((const u8 *)&mask->key + start); | |
65 | const long *s = (const long *)((const u8 *)src + start); | |
66 | long *d = (long *)((u8 *)dst + start); | |
e6445719 PS |
67 | int i; |
68 | ||
ae5f2fb1 JG |
69 | /* If 'full' is true then all of 'dst' is fully initialized. Otherwise, |
70 | * if 'full' is false the memory outside of the 'mask->range' is left | |
71 | * uninitialized. This can be used as an optimization when further | |
72 | * operations on 'dst' only use contents within 'mask->range'. | |
e6445719 | 73 | */ |
ae5f2fb1 | 74 | for (i = 0; i < len; i += sizeof(long)) |
e6445719 PS |
75 | *d++ = *s++ & *m++; |
76 | } | |
77 | ||
23dabf88 | 78 | struct sw_flow *ovs_flow_alloc(void) |
e6445719 PS |
79 | { |
80 | struct sw_flow *flow; | |
63e7959c JR |
81 | struct flow_stats *stats; |
82 | int node; | |
e6445719 PS |
83 | |
84 | flow = kmem_cache_alloc(flow_cache, GFP_KERNEL); | |
85 | if (!flow) | |
86 | return ERR_PTR(-ENOMEM); | |
87 | ||
e6445719 PS |
88 | flow->sf_acts = NULL; |
89 | flow->mask = NULL; | |
ca539345 PS |
90 | flow->id.unmasked_key = NULL; |
91 | flow->id.ufid_len = 0; | |
63e7959c | 92 | flow->stats_last_writer = NUMA_NO_NODE; |
e6445719 | 93 | |
63e7959c JR |
94 | /* Initialize the default stat node. */ |
95 | stats = kmem_cache_alloc_node(flow_stats_cache, | |
598c12d0 KK |
96 | GFP_KERNEL | __GFP_ZERO, |
97 | node_online(0) ? 0 : NUMA_NO_NODE); | |
63e7959c | 98 | if (!stats) |
23dabf88 | 99 | goto err; |
e298e505 | 100 | |
63e7959c JR |
101 | spin_lock_init(&stats->lock); |
102 | ||
103 | RCU_INIT_POINTER(flow->stats[0], stats); | |
104 | ||
105 | for_each_node(node) | |
106 | if (node != 0) | |
107 | RCU_INIT_POINTER(flow->stats[node], NULL); | |
e298e505 | 108 | |
e6445719 | 109 | return flow; |
e298e505 | 110 | err: |
ece37c87 | 111 | kmem_cache_free(flow_cache, flow); |
e298e505 | 112 | return ERR_PTR(-ENOMEM); |
e6445719 PS |
113 | } |
114 | ||
12eb18f7 | 115 | int ovs_flow_tbl_count(const struct flow_table *table) |
b637e498 PS |
116 | { |
117 | return table->count; | |
118 | } | |
119 | ||
e6445719 PS |
120 | static struct flex_array *alloc_buckets(unsigned int n_buckets) |
121 | { | |
122 | struct flex_array *buckets; | |
123 | int i, err; | |
124 | ||
125 | buckets = flex_array_alloc(sizeof(struct hlist_head), | |
126 | n_buckets, GFP_KERNEL); | |
127 | if (!buckets) | |
128 | return NULL; | |
129 | ||
130 | err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL); | |
131 | if (err) { | |
132 | flex_array_free(buckets); | |
133 | return NULL; | |
134 | } | |
135 | ||
136 | for (i = 0; i < n_buckets; i++) | |
137 | INIT_HLIST_HEAD((struct hlist_head *) | |
138 | flex_array_get(buckets, i)); | |
139 | ||
140 | return buckets; | |
141 | } | |
142 | ||
143 | static void flow_free(struct sw_flow *flow) | |
144 | { | |
63e7959c JR |
145 | int node; |
146 | ||
74ed7ab9 JS |
147 | if (ovs_identifier_is_key(&flow->id)) |
148 | kfree(flow->id.unmasked_key); | |
34ae932a TG |
149 | if (flow->sf_acts) |
150 | ovs_nla_free_flow_actions((struct sw_flow_actions __force *)flow->sf_acts); | |
40773966 TLSC |
151 | /* We open code this to make sure node 0 is always considered */ |
152 | for (node = 0; node < MAX_NUMNODES; node = next_node(node, node_possible_map)) | |
153 | if (node != 0 && flow->stats[node]) | |
63e7959c JR |
154 | kmem_cache_free(flow_stats_cache, |
155 | (struct flow_stats __force *)flow->stats[node]); | |
e6445719 PS |
156 | kmem_cache_free(flow_cache, flow); |
157 | } | |
158 | ||
159 | static void rcu_free_flow_callback(struct rcu_head *rcu) | |
160 | { | |
161 | struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); | |
162 | ||
163 | flow_free(flow); | |
164 | } | |
165 | ||
e80857cc | 166 | void ovs_flow_free(struct sw_flow *flow, bool deferred) |
618ed0c8 | 167 | { |
e80857cc | 168 | if (!flow) |
618ed0c8 PS |
169 | return; |
170 | ||
e6445719 PS |
171 | if (deferred) |
172 | call_rcu(&flow->rcu, rcu_free_flow_callback); | |
173 | else | |
174 | flow_free(flow); | |
175 | } | |
176 | ||
177 | static void free_buckets(struct flex_array *buckets) | |
178 | { | |
179 | flex_array_free(buckets); | |
180 | } | |
181 | ||
e80857cc | 182 | |
b637e498 | 183 | static void __table_instance_destroy(struct table_instance *ti) |
e6445719 | 184 | { |
b637e498 PS |
185 | free_buckets(ti->buckets); |
186 | kfree(ti); | |
e6445719 PS |
187 | } |
188 | ||
b637e498 | 189 | static struct table_instance *table_instance_alloc(int new_size) |
e6445719 | 190 | { |
b637e498 | 191 | struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL); |
e6445719 | 192 | |
b637e498 | 193 | if (!ti) |
e6445719 PS |
194 | return NULL; |
195 | ||
b637e498 | 196 | ti->buckets = alloc_buckets(new_size); |
e6445719 | 197 | |
b637e498 PS |
198 | if (!ti->buckets) { |
199 | kfree(ti); | |
e6445719 PS |
200 | return NULL; |
201 | } | |
b637e498 PS |
202 | ti->n_buckets = new_size; |
203 | ti->node_ver = 0; | |
204 | ti->keep_flows = false; | |
205 | get_random_bytes(&ti->hash_seed, sizeof(u32)); | |
e6445719 | 206 | |
b637e498 | 207 | return ti; |
e6445719 PS |
208 | } |
209 | ||
b637e498 | 210 | int ovs_flow_tbl_init(struct flow_table *table) |
e6445719 | 211 | { |
74ed7ab9 | 212 | struct table_instance *ti, *ufid_ti; |
e6445719 | 213 | |
b637e498 | 214 | ti = table_instance_alloc(TBL_MIN_BUCKETS); |
e6445719 | 215 | |
b637e498 PS |
216 | if (!ti) |
217 | return -ENOMEM; | |
e6445719 | 218 | |
74ed7ab9 JS |
219 | ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
220 | if (!ufid_ti) | |
221 | goto free_ti; | |
222 | ||
b637e498 | 223 | rcu_assign_pointer(table->ti, ti); |
74ed7ab9 | 224 | rcu_assign_pointer(table->ufid_ti, ufid_ti); |
b637e498 PS |
225 | INIT_LIST_HEAD(&table->mask_list); |
226 | table->last_rehash = jiffies; | |
227 | table->count = 0; | |
74ed7ab9 | 228 | table->ufid_count = 0; |
b637e498 | 229 | return 0; |
74ed7ab9 JS |
230 | |
231 | free_ti: | |
232 | __table_instance_destroy(ti); | |
233 | return -ENOMEM; | |
e6445719 PS |
234 | } |
235 | ||
236 | static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) | |
237 | { | |
b637e498 | 238 | struct table_instance *ti = container_of(rcu, struct table_instance, rcu); |
e6445719 | 239 | |
b637e498 | 240 | __table_instance_destroy(ti); |
e6445719 PS |
241 | } |
242 | ||
74ed7ab9 JS |
243 | static void table_instance_destroy(struct table_instance *ti, |
244 | struct table_instance *ufid_ti, | |
245 | bool deferred) | |
e6445719 | 246 | { |
e80857cc AZ |
247 | int i; |
248 | ||
b637e498 | 249 | if (!ti) |
e6445719 PS |
250 | return; |
251 | ||
74ed7ab9 | 252 | BUG_ON(!ufid_ti); |
e80857cc AZ |
253 | if (ti->keep_flows) |
254 | goto skip_flows; | |
255 | ||
256 | for (i = 0; i < ti->n_buckets; i++) { | |
257 | struct sw_flow *flow; | |
258 | struct hlist_head *head = flex_array_get(ti->buckets, i); | |
259 | struct hlist_node *n; | |
260 | int ver = ti->node_ver; | |
74ed7ab9 | 261 | int ufid_ver = ufid_ti->node_ver; |
e80857cc | 262 | |
74ed7ab9 JS |
263 | hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) { |
264 | hlist_del_rcu(&flow->flow_table.node[ver]); | |
265 | if (ovs_identifier_is_ufid(&flow->id)) | |
266 | hlist_del_rcu(&flow->ufid_table.node[ufid_ver]); | |
e80857cc AZ |
267 | ovs_flow_free(flow, deferred); |
268 | } | |
269 | } | |
270 | ||
271 | skip_flows: | |
74ed7ab9 | 272 | if (deferred) { |
b637e498 | 273 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); |
74ed7ab9 JS |
274 | call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb); |
275 | } else { | |
b637e498 | 276 | __table_instance_destroy(ti); |
74ed7ab9 JS |
277 | __table_instance_destroy(ufid_ti); |
278 | } | |
b637e498 PS |
279 | } |
280 | ||
9b996e54 PS |
281 | /* No need for locking this function is called from RCU callback or |
282 | * error path. | |
283 | */ | |
284 | void ovs_flow_tbl_destroy(struct flow_table *table) | |
b637e498 | 285 | { |
9b996e54 | 286 | struct table_instance *ti = rcu_dereference_raw(table->ti); |
74ed7ab9 | 287 | struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti); |
b637e498 | 288 | |
74ed7ab9 | 289 | table_instance_destroy(ti, ufid_ti, false); |
e6445719 PS |
290 | } |
291 | ||
b637e498 | 292 | struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti, |
e6445719 PS |
293 | u32 *bucket, u32 *last) |
294 | { | |
295 | struct sw_flow *flow; | |
296 | struct hlist_head *head; | |
297 | int ver; | |
298 | int i; | |
299 | ||
b637e498 PS |
300 | ver = ti->node_ver; |
301 | while (*bucket < ti->n_buckets) { | |
e6445719 | 302 | i = 0; |
b637e498 | 303 | head = flex_array_get(ti->buckets, *bucket); |
74ed7ab9 | 304 | hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) { |
e6445719 PS |
305 | if (i < *last) { |
306 | i++; | |
307 | continue; | |
308 | } | |
309 | *last = i + 1; | |
310 | return flow; | |
311 | } | |
312 | (*bucket)++; | |
313 | *last = 0; | |
314 | } | |
315 | ||
316 | return NULL; | |
317 | } | |
318 | ||
b637e498 | 319 | static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash) |
e6445719 | 320 | { |
b637e498 PS |
321 | hash = jhash_1word(hash, ti->hash_seed); |
322 | return flex_array_get(ti->buckets, | |
323 | (hash & (ti->n_buckets - 1))); | |
e6445719 PS |
324 | } |
325 | ||
74ed7ab9 JS |
326 | static void table_instance_insert(struct table_instance *ti, |
327 | struct sw_flow *flow) | |
e6445719 PS |
328 | { |
329 | struct hlist_head *head; | |
330 | ||
74ed7ab9 JS |
331 | head = find_bucket(ti, flow->flow_table.hash); |
332 | hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head); | |
333 | } | |
334 | ||
335 | static void ufid_table_instance_insert(struct table_instance *ti, | |
336 | struct sw_flow *flow) | |
337 | { | |
338 | struct hlist_head *head; | |
339 | ||
340 | head = find_bucket(ti, flow->ufid_table.hash); | |
341 | hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head); | |
e6445719 PS |
342 | } |
343 | ||
b637e498 | 344 | static void flow_table_copy_flows(struct table_instance *old, |
74ed7ab9 | 345 | struct table_instance *new, bool ufid) |
e6445719 PS |
346 | { |
347 | int old_ver; | |
348 | int i; | |
349 | ||
350 | old_ver = old->node_ver; | |
351 | new->node_ver = !old_ver; | |
352 | ||
353 | /* Insert in new table. */ | |
354 | for (i = 0; i < old->n_buckets; i++) { | |
355 | struct sw_flow *flow; | |
356 | struct hlist_head *head; | |
357 | ||
358 | head = flex_array_get(old->buckets, i); | |
359 | ||
74ed7ab9 JS |
360 | if (ufid) |
361 | hlist_for_each_entry(flow, head, | |
362 | ufid_table.node[old_ver]) | |
363 | ufid_table_instance_insert(new, flow); | |
364 | else | |
365 | hlist_for_each_entry(flow, head, | |
366 | flow_table.node[old_ver]) | |
367 | table_instance_insert(new, flow); | |
e6445719 PS |
368 | } |
369 | ||
e6445719 PS |
370 | old->keep_flows = true; |
371 | } | |
372 | ||
b637e498 | 373 | static struct table_instance *table_instance_rehash(struct table_instance *ti, |
74ed7ab9 | 374 | int n_buckets, bool ufid) |
e6445719 | 375 | { |
b637e498 | 376 | struct table_instance *new_ti; |
e6445719 | 377 | |
b637e498 PS |
378 | new_ti = table_instance_alloc(n_buckets); |
379 | if (!new_ti) | |
618ed0c8 | 380 | return NULL; |
e6445719 | 381 | |
74ed7ab9 | 382 | flow_table_copy_flows(ti, new_ti, ufid); |
e6445719 | 383 | |
b637e498 | 384 | return new_ti; |
e6445719 PS |
385 | } |
386 | ||
b637e498 | 387 | int ovs_flow_tbl_flush(struct flow_table *flow_table) |
e6445719 | 388 | { |
74ed7ab9 JS |
389 | struct table_instance *old_ti, *new_ti; |
390 | struct table_instance *old_ufid_ti, *new_ufid_ti; | |
e6445719 | 391 | |
b637e498 PS |
392 | new_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
393 | if (!new_ti) | |
394 | return -ENOMEM; | |
74ed7ab9 JS |
395 | new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
396 | if (!new_ufid_ti) | |
397 | goto err_free_ti; | |
398 | ||
399 | old_ti = ovsl_dereference(flow_table->ti); | |
400 | old_ufid_ti = ovsl_dereference(flow_table->ufid_ti); | |
b637e498 PS |
401 | |
402 | rcu_assign_pointer(flow_table->ti, new_ti); | |
74ed7ab9 | 403 | rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti); |
b637e498 PS |
404 | flow_table->last_rehash = jiffies; |
405 | flow_table->count = 0; | |
74ed7ab9 | 406 | flow_table->ufid_count = 0; |
b637e498 | 407 | |
74ed7ab9 | 408 | table_instance_destroy(old_ti, old_ufid_ti, true); |
b637e498 | 409 | return 0; |
74ed7ab9 JS |
410 | |
411 | err_free_ti: | |
412 | __table_instance_destroy(new_ti); | |
413 | return -ENOMEM; | |
e6445719 PS |
414 | } |
415 | ||
272c2cf8 JS |
416 | static u32 flow_hash(const struct sw_flow_key *key, |
417 | const struct sw_flow_key_range *range) | |
e6445719 | 418 | { |
272c2cf8 JS |
419 | int key_start = range->start; |
420 | int key_end = range->end; | |
7085130b | 421 | const u32 *hash_key = (const u32 *)((const u8 *)key + key_start); |
e6445719 PS |
422 | int hash_u32s = (key_end - key_start) >> 2; |
423 | ||
424 | /* Make sure number of hash bytes are multiple of u32. */ | |
425 | BUILD_BUG_ON(sizeof(long) % sizeof(u32)); | |
426 | ||
87545899 | 427 | return jhash2(hash_key, hash_u32s, 0); |
e6445719 PS |
428 | } |
429 | ||
430 | static int flow_key_start(const struct sw_flow_key *key) | |
431 | { | |
00a93bab | 432 | if (key->tun_proto) |
e6445719 PS |
433 | return 0; |
434 | else | |
435 | return rounddown(offsetof(struct sw_flow_key, phy), | |
436 | sizeof(long)); | |
437 | } | |
438 | ||
439 | static bool cmp_key(const struct sw_flow_key *key1, | |
440 | const struct sw_flow_key *key2, | |
441 | int key_start, int key_end) | |
442 | { | |
7085130b DDP |
443 | const long *cp1 = (const long *)((const u8 *)key1 + key_start); |
444 | const long *cp2 = (const long *)((const u8 *)key2 + key_start); | |
e6445719 PS |
445 | long diffs = 0; |
446 | int i; | |
447 | ||
448 | for (i = key_start; i < key_end; i += sizeof(long)) | |
449 | diffs |= *cp1++ ^ *cp2++; | |
450 | ||
451 | return diffs == 0; | |
452 | } | |
453 | ||
454 | static bool flow_cmp_masked_key(const struct sw_flow *flow, | |
455 | const struct sw_flow_key *key, | |
272c2cf8 | 456 | const struct sw_flow_key_range *range) |
e6445719 | 457 | { |
272c2cf8 | 458 | return cmp_key(&flow->key, key, range->start, range->end); |
e6445719 PS |
459 | } |
460 | ||
74ed7ab9 JS |
461 | static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, |
462 | const struct sw_flow_match *match) | |
e6445719 PS |
463 | { |
464 | struct sw_flow_key *key = match->key; | |
465 | int key_start = flow_key_start(key); | |
466 | int key_end = match->range.end; | |
467 | ||
74ed7ab9 JS |
468 | BUG_ON(ovs_identifier_is_ufid(&flow->id)); |
469 | return cmp_key(flow->id.unmasked_key, key, key_start, key_end); | |
e6445719 PS |
470 | } |
471 | ||
b637e498 | 472 | static struct sw_flow *masked_flow_lookup(struct table_instance *ti, |
e6445719 | 473 | const struct sw_flow_key *unmasked, |
12eb18f7 | 474 | const struct sw_flow_mask *mask) |
e6445719 PS |
475 | { |
476 | struct sw_flow *flow; | |
477 | struct hlist_head *head; | |
e6445719 PS |
478 | u32 hash; |
479 | struct sw_flow_key masked_key; | |
480 | ||
ae5f2fb1 | 481 | ovs_flow_mask_key(&masked_key, unmasked, false, mask); |
272c2cf8 | 482 | hash = flow_hash(&masked_key, &mask->range); |
b637e498 | 483 | head = find_bucket(ti, hash); |
74ed7ab9 JS |
484 | hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) { |
485 | if (flow->mask == mask && flow->flow_table.hash == hash && | |
272c2cf8 | 486 | flow_cmp_masked_key(flow, &masked_key, &mask->range)) |
e6445719 PS |
487 | return flow; |
488 | } | |
489 | return NULL; | |
490 | } | |
491 | ||
5bb50632 | 492 | struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, |
1bd7116f AZ |
493 | const struct sw_flow_key *key, |
494 | u32 *n_mask_hit) | |
e6445719 | 495 | { |
663efa36 | 496 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); |
e6445719 | 497 | struct sw_flow_mask *mask; |
b637e498 | 498 | struct sw_flow *flow; |
e6445719 | 499 | |
1bd7116f | 500 | *n_mask_hit = 0; |
b637e498 | 501 | list_for_each_entry_rcu(mask, &tbl->mask_list, list) { |
1bd7116f | 502 | (*n_mask_hit)++; |
b637e498 | 503 | flow = masked_flow_lookup(ti, key, mask); |
e6445719 | 504 | if (flow) /* Found */ |
b637e498 | 505 | return flow; |
e6445719 | 506 | } |
b637e498 PS |
507 | return NULL; |
508 | } | |
e6445719 | 509 | |
5bb50632 AZ |
510 | struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, |
511 | const struct sw_flow_key *key) | |
512 | { | |
513 | u32 __always_unused n_mask_hit; | |
514 | ||
515 | return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit); | |
516 | } | |
517 | ||
4a46b24e | 518 | struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, |
12eb18f7 | 519 | const struct sw_flow_match *match) |
4a46b24e AW |
520 | { |
521 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); | |
522 | struct sw_flow_mask *mask; | |
523 | struct sw_flow *flow; | |
524 | ||
525 | /* Always called under ovs-mutex. */ | |
526 | list_for_each_entry(mask, &tbl->mask_list, list) { | |
527 | flow = masked_flow_lookup(ti, match->key, mask); | |
74ed7ab9 JS |
528 | if (flow && ovs_identifier_is_key(&flow->id) && |
529 | ovs_flow_cmp_unmasked_key(flow, match)) | |
530 | return flow; | |
531 | } | |
532 | return NULL; | |
533 | } | |
534 | ||
535 | static u32 ufid_hash(const struct sw_flow_id *sfid) | |
536 | { | |
537 | return jhash(sfid->ufid, sfid->ufid_len, 0); | |
538 | } | |
539 | ||
540 | static bool ovs_flow_cmp_ufid(const struct sw_flow *flow, | |
541 | const struct sw_flow_id *sfid) | |
542 | { | |
543 | if (flow->id.ufid_len != sfid->ufid_len) | |
544 | return false; | |
545 | ||
546 | return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len); | |
547 | } | |
548 | ||
549 | bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match) | |
550 | { | |
551 | if (ovs_identifier_is_ufid(&flow->id)) | |
552 | return flow_cmp_masked_key(flow, match->key, &match->range); | |
553 | ||
554 | return ovs_flow_cmp_unmasked_key(flow, match); | |
555 | } | |
556 | ||
557 | struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl, | |
558 | const struct sw_flow_id *ufid) | |
559 | { | |
560 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti); | |
561 | struct sw_flow *flow; | |
562 | struct hlist_head *head; | |
563 | u32 hash; | |
564 | ||
565 | hash = ufid_hash(ufid); | |
566 | head = find_bucket(ti, hash); | |
567 | hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) { | |
568 | if (flow->ufid_table.hash == hash && | |
569 | ovs_flow_cmp_ufid(flow, ufid)) | |
4a46b24e AW |
570 | return flow; |
571 | } | |
572 | return NULL; | |
573 | } | |
574 | ||
1bd7116f AZ |
575 | int ovs_flow_tbl_num_masks(const struct flow_table *table) |
576 | { | |
577 | struct sw_flow_mask *mask; | |
578 | int num = 0; | |
579 | ||
580 | list_for_each_entry(mask, &table->mask_list, list) | |
581 | num++; | |
582 | ||
583 | return num; | |
584 | } | |
585 | ||
74ed7ab9 JS |
586 | static struct table_instance *table_instance_expand(struct table_instance *ti, |
587 | bool ufid) | |
b637e498 | 588 | { |
74ed7ab9 | 589 | return table_instance_rehash(ti, ti->n_buckets * 2, ufid); |
e6445719 PS |
590 | } |
591 | ||
56c19868 JR |
592 | /* Remove 'mask' from the mask list, if it is not needed any more. */ |
593 | static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) | |
594 | { | |
595 | if (mask) { | |
596 | /* ovs-lock is required to protect mask-refcount and | |
597 | * mask list. | |
598 | */ | |
599 | ASSERT_OVSL(); | |
600 | BUG_ON(!mask->ref_count); | |
601 | mask->ref_count--; | |
602 | ||
603 | if (!mask->ref_count) { | |
604 | list_del_rcu(&mask->list); | |
605 | kfree_rcu(mask, rcu); | |
606 | } | |
607 | } | |
608 | } | |
609 | ||
610 | /* Must be called with OVS mutex held. */ | |
e6445719 PS |
611 | void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) |
612 | { | |
b637e498 | 613 | struct table_instance *ti = ovsl_dereference(table->ti); |
74ed7ab9 | 614 | struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti); |
b637e498 | 615 | |
e6445719 | 616 | BUG_ON(table->count == 0); |
74ed7ab9 | 617 | hlist_del_rcu(&flow->flow_table.node[ti->node_ver]); |
e6445719 | 618 | table->count--; |
74ed7ab9 JS |
619 | if (ovs_identifier_is_ufid(&flow->id)) { |
620 | hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]); | |
621 | table->ufid_count--; | |
622 | } | |
56c19868 JR |
623 | |
624 | /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be | |
625 | * accessible as long as the RCU read lock is held. | |
626 | */ | |
627 | flow_mask_remove(table, flow->mask); | |
e6445719 PS |
628 | } |
629 | ||
618ed0c8 | 630 | static struct sw_flow_mask *mask_alloc(void) |
e6445719 PS |
631 | { |
632 | struct sw_flow_mask *mask; | |
633 | ||
634 | mask = kmalloc(sizeof(*mask), GFP_KERNEL); | |
635 | if (mask) | |
e80857cc | 636 | mask->ref_count = 1; |
e6445719 PS |
637 | |
638 | return mask; | |
639 | } | |
640 | ||
e6445719 PS |
641 | static bool mask_equal(const struct sw_flow_mask *a, |
642 | const struct sw_flow_mask *b) | |
643 | { | |
7085130b DDP |
644 | const u8 *a_ = (const u8 *)&a->key + a->range.start; |
645 | const u8 *b_ = (const u8 *)&b->key + b->range.start; | |
e6445719 PS |
646 | |
647 | return (a->range.end == b->range.end) | |
648 | && (a->range.start == b->range.start) | |
649 | && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0); | |
650 | } | |
651 | ||
618ed0c8 | 652 | static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, |
e6445719 PS |
653 | const struct sw_flow_mask *mask) |
654 | { | |
655 | struct list_head *ml; | |
656 | ||
b637e498 | 657 | list_for_each(ml, &tbl->mask_list) { |
e6445719 PS |
658 | struct sw_flow_mask *m; |
659 | m = container_of(ml, struct sw_flow_mask, list); | |
660 | if (mask_equal(mask, m)) | |
661 | return m; | |
662 | } | |
663 | ||
664 | return NULL; | |
665 | } | |
666 | ||
d1211908 | 667 | /* Add 'mask' into the mask list, if it is not already there. */ |
618ed0c8 | 668 | static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, |
12eb18f7 | 669 | const struct sw_flow_mask *new) |
618ed0c8 PS |
670 | { |
671 | struct sw_flow_mask *mask; | |
672 | mask = flow_mask_find(tbl, new); | |
673 | if (!mask) { | |
674 | /* Allocate a new mask if none exsits. */ | |
675 | mask = mask_alloc(); | |
676 | if (!mask) | |
677 | return -ENOMEM; | |
678 | mask->key = new->key; | |
679 | mask->range = new->range; | |
680 | list_add_rcu(&mask->list, &tbl->mask_list); | |
e80857cc AZ |
681 | } else { |
682 | BUG_ON(!mask->ref_count); | |
683 | mask->ref_count++; | |
618ed0c8 PS |
684 | } |
685 | ||
618ed0c8 PS |
686 | flow->mask = mask; |
687 | return 0; | |
688 | } | |
689 | ||
56c19868 | 690 | /* Must be called with OVS mutex held. */ |
d29ab6f8 | 691 | static void flow_key_insert(struct flow_table *table, struct sw_flow *flow) |
e6445719 | 692 | { |
618ed0c8 PS |
693 | struct table_instance *new_ti = NULL; |
694 | struct table_instance *ti; | |
618ed0c8 | 695 | |
74ed7ab9 | 696 | flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range); |
618ed0c8 PS |
697 | ti = ovsl_dereference(table->ti); |
698 | table_instance_insert(ti, flow); | |
699 | table->count++; | |
700 | ||
701 | /* Expand table, if necessary, to make room. */ | |
702 | if (table->count > ti->n_buckets) | |
74ed7ab9 | 703 | new_ti = table_instance_expand(ti, false); |
618ed0c8 | 704 | else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL)) |
74ed7ab9 | 705 | new_ti = table_instance_rehash(ti, ti->n_buckets, false); |
618ed0c8 PS |
706 | |
707 | if (new_ti) { | |
708 | rcu_assign_pointer(table->ti, new_ti); | |
74ed7ab9 | 709 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); |
618ed0c8 PS |
710 | table->last_rehash = jiffies; |
711 | } | |
d29ab6f8 JS |
712 | } |
713 | ||
74ed7ab9 JS |
714 | /* Must be called with OVS mutex held. */ |
715 | static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow) | |
716 | { | |
717 | struct table_instance *ti; | |
718 | ||
719 | flow->ufid_table.hash = ufid_hash(&flow->id); | |
720 | ti = ovsl_dereference(table->ufid_ti); | |
721 | ufid_table_instance_insert(ti, flow); | |
722 | table->ufid_count++; | |
723 | ||
724 | /* Expand table, if necessary, to make room. */ | |
725 | if (table->ufid_count > ti->n_buckets) { | |
726 | struct table_instance *new_ti; | |
727 | ||
728 | new_ti = table_instance_expand(ti, true); | |
729 | if (new_ti) { | |
730 | rcu_assign_pointer(table->ufid_ti, new_ti); | |
731 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); | |
732 | } | |
733 | } | |
734 | } | |
735 | ||
d29ab6f8 JS |
736 | /* Must be called with OVS mutex held. */ |
737 | int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, | |
738 | const struct sw_flow_mask *mask) | |
739 | { | |
740 | int err; | |
741 | ||
742 | err = flow_mask_insert(table, flow, mask); | |
743 | if (err) | |
744 | return err; | |
745 | flow_key_insert(table, flow); | |
74ed7ab9 JS |
746 | if (ovs_identifier_is_ufid(&flow->id)) |
747 | flow_ufid_insert(table, flow); | |
d29ab6f8 | 748 | |
618ed0c8 | 749 | return 0; |
e6445719 PS |
750 | } |
751 | ||
752 | /* Initializes the flow module. | |
753 | * Returns zero if successful or a negative error code. */ | |
754 | int ovs_flow_init(void) | |
755 | { | |
756 | BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long)); | |
757 | BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long)); | |
758 | ||
63e7959c | 759 | flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow) |
bac541e4 | 760 | + (nr_node_ids |
63e7959c JR |
761 | * sizeof(struct flow_stats *)), |
762 | 0, 0, NULL); | |
e6445719 PS |
763 | if (flow_cache == NULL) |
764 | return -ENOMEM; | |
765 | ||
63e7959c JR |
766 | flow_stats_cache |
767 | = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats), | |
768 | 0, SLAB_HWCACHE_ALIGN, NULL); | |
769 | if (flow_stats_cache == NULL) { | |
770 | kmem_cache_destroy(flow_cache); | |
771 | flow_cache = NULL; | |
772 | return -ENOMEM; | |
773 | } | |
774 | ||
e6445719 PS |
775 | return 0; |
776 | } | |
777 | ||
778 | /* Uninitializes the flow module. */ | |
779 | void ovs_flow_exit(void) | |
780 | { | |
63e7959c | 781 | kmem_cache_destroy(flow_stats_cache); |
e6445719 PS |
782 | kmem_cache_destroy(flow_cache); |
783 | } |