~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/openvswitch/flow_table.c

Version: ~ [ linux-5.9-rc5 ] ~ [ linux-5.8.10 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.66 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.146 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.198 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.236 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.236 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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>
 28 #include <linux/hash.h>
 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 
 47 #define TBL_MIN_BUCKETS         1024
 48 #define REHASH_INTERVAL         (10 * 60 * HZ)
 49 
 50 static struct kmem_cache *flow_cache;
 51 
 52 static u16 range_n_bytes(const struct sw_flow_key_range *range)
 53 {
 54         return range->end - range->start;
 55 }
 56 
 57 void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
 58                        bool full, const struct sw_flow_mask *mask)
 59 {
 60         int start = full ? 0 : mask->range.start;
 61         int len = full ? sizeof *dst : range_n_bytes(&mask->range);
 62         const long *m = (const long *)((const u8 *)&mask->key + start);
 63         const long *s = (const long *)((const u8 *)src + start);
 64         long *d = (long *)((u8 *)dst + start);
 65         int i;
 66 
 67         /* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
 68          * if 'full' is false the memory outside of the 'mask->range' is left
 69          * uninitialized. This can be used as an optimization when further
 70          * operations on 'dst' only use contents within 'mask->range'.
 71          */
 72         for (i = 0; i < len; i += sizeof(long))
 73                 *d++ = *s++ & *m++;
 74 }
 75 
 76 struct sw_flow *ovs_flow_alloc(bool percpu_stats)
 77 {
 78         struct sw_flow *flow;
 79         int cpu;
 80 
 81         flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
 82         if (!flow)
 83                 return ERR_PTR(-ENOMEM);
 84 
 85         flow->sf_acts = NULL;
 86         flow->mask = NULL;
 87 
 88         flow->stats.is_percpu = percpu_stats;
 89 
 90         if (!percpu_stats) {
 91                 flow->stats.stat = kzalloc(sizeof(*flow->stats.stat), GFP_KERNEL);
 92                 if (!flow->stats.stat)
 93                         goto err;
 94 
 95                 spin_lock_init(&flow->stats.stat->lock);
 96         } else {
 97                 flow->stats.cpu_stats = alloc_percpu(struct flow_stats);
 98                 if (!flow->stats.cpu_stats)
 99                         goto err;
100 
101                 for_each_possible_cpu(cpu) {
102                         struct flow_stats *cpu_stats;
103 
104                         cpu_stats = per_cpu_ptr(flow->stats.cpu_stats, cpu);
105                         spin_lock_init(&cpu_stats->lock);
106                 }
107         }
108         return flow;
109 err:
110         kmem_cache_free(flow_cache, flow);
111         return ERR_PTR(-ENOMEM);
112 }
113 
114 int ovs_flow_tbl_count(struct flow_table *table)
115 {
116         return table->count;
117 }
118 
119 static struct flex_array *alloc_buckets(unsigned int n_buckets)
120 {
121         struct flex_array *buckets;
122         int i, err;
123 
124         buckets = flex_array_alloc(sizeof(struct hlist_head),
125                                    n_buckets, GFP_KERNEL);
126         if (!buckets)
127                 return NULL;
128 
129         err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
130         if (err) {
131                 flex_array_free(buckets);
132                 return NULL;
133         }
134 
135         for (i = 0; i < n_buckets; i++)
136                 INIT_HLIST_HEAD((struct hlist_head *)
137                                         flex_array_get(buckets, i));
138 
139         return buckets;
140 }
141 
142 static void flow_free(struct sw_flow *flow)
143 {
144         kfree((struct sf_flow_acts __force *)flow->sf_acts);
145         if (flow->stats.is_percpu)
146                 free_percpu(flow->stats.cpu_stats);
147         else
148                 kfree(flow->stats.stat);
149         kmem_cache_free(flow_cache, flow);
150 }
151 
152 static void rcu_free_flow_callback(struct rcu_head *rcu)
153 {
154         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
155 
156         flow_free(flow);
157 }
158 
159 void ovs_flow_free(struct sw_flow *flow, bool deferred)
160 {
161         if (!flow)
162                 return;
163 
164         if (flow->mask) {
165                 struct sw_flow_mask *mask = flow->mask;
166 
167                 /* ovs-lock is required to protect mask-refcount and
168                  * mask list.
169                  */
170                 ASSERT_OVSL();
171                 BUG_ON(!mask->ref_count);
172                 mask->ref_count--;
173 
174                 if (!mask->ref_count) {
175                         list_del_rcu(&mask->list);
176                         if (deferred)
177                                 kfree_rcu(mask, rcu);
178                         else
179                                 kfree(mask);
180                 }
181         }
182 
183         if (deferred)
184                 call_rcu(&flow->rcu, rcu_free_flow_callback);
185         else
186                 flow_free(flow);
187 }
188 
189 static void free_buckets(struct flex_array *buckets)
190 {
191         flex_array_free(buckets);
192 }
193 
194 
195 static void __table_instance_destroy(struct table_instance *ti)
196 {
197         free_buckets(ti->buckets);
198         kfree(ti);
199 }
200 
201 static struct table_instance *table_instance_alloc(int new_size)
202 {
203         struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
204 
205         if (!ti)
206                 return NULL;
207 
208         ti->buckets = alloc_buckets(new_size);
209 
210         if (!ti->buckets) {
211                 kfree(ti);
212                 return NULL;
213         }
214         ti->n_buckets = new_size;
215         ti->node_ver = 0;
216         ti->keep_flows = false;
217         get_random_bytes(&ti->hash_seed, sizeof(u32));
218 
219         return ti;
220 }
221 
222 int ovs_flow_tbl_init(struct flow_table *table)
223 {
224         struct table_instance *ti;
225 
226         ti = table_instance_alloc(TBL_MIN_BUCKETS);
227 
228         if (!ti)
229                 return -ENOMEM;
230 
231         rcu_assign_pointer(table->ti, ti);
232         INIT_LIST_HEAD(&table->mask_list);
233         table->last_rehash = jiffies;
234         table->count = 0;
235         return 0;
236 }
237 
238 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
239 {
240         struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
241 
242         __table_instance_destroy(ti);
243 }
244 
245 static void table_instance_destroy(struct table_instance *ti, bool deferred)
246 {
247         int i;
248 
249         if (!ti)
250                 return;
251 
252         if (ti->keep_flows)
253                 goto skip_flows;
254 
255         for (i = 0; i < ti->n_buckets; i++) {
256                 struct sw_flow *flow;
257                 struct hlist_head *head = flex_array_get(ti->buckets, i);
258                 struct hlist_node *n;
259                 int ver = ti->node_ver;
260 
261                 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
262                         hlist_del_rcu(&flow->hash_node[ver]);
263                         ovs_flow_free(flow, deferred);
264                 }
265         }
266 
267 skip_flows:
268         if (deferred)
269                 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
270         else
271                 __table_instance_destroy(ti);
272 }
273 
274 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
275 {
276         struct table_instance *ti = ovsl_dereference(table->ti);
277 
278         table_instance_destroy(ti, deferred);
279 }
280 
281 struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
282                                        u32 *bucket, u32 *last)
283 {
284         struct sw_flow *flow;
285         struct hlist_head *head;
286         int ver;
287         int i;
288 
289         ver = ti->node_ver;
290         while (*bucket < ti->n_buckets) {
291                 i = 0;
292                 head = flex_array_get(ti->buckets, *bucket);
293                 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
294                         if (i < *last) {
295                                 i++;
296                                 continue;
297                         }
298                         *last = i + 1;
299                         return flow;
300                 }
301                 (*bucket)++;
302                 *last = 0;
303         }
304 
305         return NULL;
306 }
307 
308 static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
309 {
310         hash = jhash_1word(hash, ti->hash_seed);
311         return flex_array_get(ti->buckets,
312                                 (hash & (ti->n_buckets - 1)));
313 }
314 
315 static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow)
316 {
317         struct hlist_head *head;
318 
319         head = find_bucket(ti, flow->hash);
320         hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head);
321 }
322 
323 static void flow_table_copy_flows(struct table_instance *old,
324                                   struct table_instance *new)
325 {
326         int old_ver;
327         int i;
328 
329         old_ver = old->node_ver;
330         new->node_ver = !old_ver;
331 
332         /* Insert in new table. */
333         for (i = 0; i < old->n_buckets; i++) {
334                 struct sw_flow *flow;
335                 struct hlist_head *head;
336 
337                 head = flex_array_get(old->buckets, i);
338 
339                 hlist_for_each_entry(flow, head, hash_node[old_ver])
340                         table_instance_insert(new, flow);
341         }
342 
343         old->keep_flows = true;
344 }
345 
346 static struct table_instance *table_instance_rehash(struct table_instance *ti,
347                                             int n_buckets)
348 {
349         struct table_instance *new_ti;
350 
351         new_ti = table_instance_alloc(n_buckets);
352         if (!new_ti)
353                 return NULL;
354 
355         flow_table_copy_flows(ti, new_ti);
356 
357         return new_ti;
358 }
359 
360 int ovs_flow_tbl_flush(struct flow_table *flow_table)
361 {
362         struct table_instance *old_ti;
363         struct table_instance *new_ti;
364 
365         old_ti = ovsl_dereference(flow_table->ti);
366         new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
367         if (!new_ti)
368                 return -ENOMEM;
369 
370         rcu_assign_pointer(flow_table->ti, new_ti);
371         flow_table->last_rehash = jiffies;
372         flow_table->count = 0;
373 
374         table_instance_destroy(old_ti, true);
375         return 0;
376 }
377 
378 static u32 flow_hash(const struct sw_flow_key *key, int key_start,
379                      int key_end)
380 {
381         u32 *hash_key = (u32 *)((u8 *)key + key_start);
382         int hash_u32s = (key_end - key_start) >> 2;
383 
384         /* Make sure number of hash bytes are multiple of u32. */
385         BUILD_BUG_ON(sizeof(long) % sizeof(u32));
386 
387         return arch_fast_hash2(hash_key, hash_u32s, 0);
388 }
389 
390 static int flow_key_start(const struct sw_flow_key *key)
391 {
392         if (key->tun_key.ipv4_dst)
393                 return 0;
394         else
395                 return rounddown(offsetof(struct sw_flow_key, phy),
396                                           sizeof(long));
397 }
398 
399 static bool cmp_key(const struct sw_flow_key *key1,
400                     const struct sw_flow_key *key2,
401                     int key_start, int key_end)
402 {
403         const long *cp1 = (long *)((u8 *)key1 + key_start);
404         const long *cp2 = (long *)((u8 *)key2 + key_start);
405         long diffs = 0;
406         int i;
407 
408         for (i = key_start; i < key_end;  i += sizeof(long))
409                 diffs |= *cp1++ ^ *cp2++;
410 
411         return diffs == 0;
412 }
413 
414 static bool flow_cmp_masked_key(const struct sw_flow *flow,
415                                 const struct sw_flow_key *key,
416                                 int key_start, int key_end)
417 {
418         return cmp_key(&flow->key, key, key_start, key_end);
419 }
420 
421 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
422                                struct sw_flow_match *match)
423 {
424         struct sw_flow_key *key = match->key;
425         int key_start = flow_key_start(key);
426         int key_end = match->range.end;
427 
428         return cmp_key(&flow->unmasked_key, key, key_start, key_end);
429 }
430 
431 static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
432                                           const struct sw_flow_key *unmasked,
433                                           struct sw_flow_mask *mask)
434 {
435         struct sw_flow *flow;
436         struct hlist_head *head;
437         int key_start = mask->range.start;
438         int key_end = mask->range.end;
439         u32 hash;
440         struct sw_flow_key masked_key;
441 
442         ovs_flow_mask_key(&masked_key, unmasked, false, mask);
443         hash = flow_hash(&masked_key, key_start, key_end);
444         head = find_bucket(ti, hash);
445         hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) {
446                 if (flow->mask == mask && flow->hash == hash &&
447                     flow_cmp_masked_key(flow, &masked_key,
448                                           key_start, key_end))
449                         return flow;
450         }
451         return NULL;
452 }
453 
454 struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
455                                     const struct sw_flow_key *key,
456                                     u32 *n_mask_hit)
457 {
458         struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
459         struct sw_flow_mask *mask;
460         struct sw_flow *flow;
461 
462         *n_mask_hit = 0;
463         list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
464                 (*n_mask_hit)++;
465                 flow = masked_flow_lookup(ti, key, mask);
466                 if (flow)  /* Found */
467                         return flow;
468         }
469         return NULL;
470 }
471 
472 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
473                                     const struct sw_flow_key *key)
474 {
475         u32 __always_unused n_mask_hit;
476 
477         return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
478 }
479 
480 int ovs_flow_tbl_num_masks(const struct flow_table *table)
481 {
482         struct sw_flow_mask *mask;
483         int num = 0;
484 
485         list_for_each_entry(mask, &table->mask_list, list)
486                 num++;
487 
488         return num;
489 }
490 
491 static struct table_instance *table_instance_expand(struct table_instance *ti)
492 {
493         return table_instance_rehash(ti, ti->n_buckets * 2);
494 }
495 
496 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
497 {
498         struct table_instance *ti = ovsl_dereference(table->ti);
499 
500         BUG_ON(table->count == 0);
501         hlist_del_rcu(&flow->hash_node[ti->node_ver]);
502         table->count--;
503 }
504 
505 static struct sw_flow_mask *mask_alloc(void)
506 {
507         struct sw_flow_mask *mask;
508 
509         mask = kmalloc(sizeof(*mask), GFP_KERNEL);
510         if (mask)
511                 mask->ref_count = 1;
512 
513         return mask;
514 }
515 
516 static bool mask_equal(const struct sw_flow_mask *a,
517                        const struct sw_flow_mask *b)
518 {
519         u8 *a_ = (u8 *)&a->key + a->range.start;
520         u8 *b_ = (u8 *)&b->key + b->range.start;
521 
522         return  (a->range.end == b->range.end)
523                 && (a->range.start == b->range.start)
524                 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
525 }
526 
527 static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
528                                            const struct sw_flow_mask *mask)
529 {
530         struct list_head *ml;
531 
532         list_for_each(ml, &tbl->mask_list) {
533                 struct sw_flow_mask *m;
534                 m = container_of(ml, struct sw_flow_mask, list);
535                 if (mask_equal(mask, m))
536                         return m;
537         }
538 
539         return NULL;
540 }
541 
542 /* Add 'mask' into the mask list, if it is not already there. */
543 static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
544                             struct sw_flow_mask *new)
545 {
546         struct sw_flow_mask *mask;
547         mask = flow_mask_find(tbl, new);
548         if (!mask) {
549                 /* Allocate a new mask if none exsits. */
550                 mask = mask_alloc();
551                 if (!mask)
552                         return -ENOMEM;
553                 mask->key = new->key;
554                 mask->range = new->range;
555                 list_add_rcu(&mask->list, &tbl->mask_list);
556         } else {
557                 BUG_ON(!mask->ref_count);
558                 mask->ref_count++;
559         }
560 
561         flow->mask = mask;
562         return 0;
563 }
564 
565 int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
566                         struct sw_flow_mask *mask)
567 {
568         struct table_instance *new_ti = NULL;
569         struct table_instance *ti;
570         int err;
571 
572         err = flow_mask_insert(table, flow, mask);
573         if (err)
574                 return err;
575 
576         flow->hash = flow_hash(&flow->key, flow->mask->range.start,
577                         flow->mask->range.end);
578         ti = ovsl_dereference(table->ti);
579         table_instance_insert(ti, flow);
580         table->count++;
581 
582         /* Expand table, if necessary, to make room. */
583         if (table->count > ti->n_buckets)
584                 new_ti = table_instance_expand(ti);
585         else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
586                 new_ti = table_instance_rehash(ti, ti->n_buckets);
587 
588         if (new_ti) {
589                 rcu_assign_pointer(table->ti, new_ti);
590                 table_instance_destroy(ti, true);
591                 table->last_rehash = jiffies;
592         }
593         return 0;
594 }
595 
596 /* Initializes the flow module.
597  * Returns zero if successful or a negative error code. */
598 int ovs_flow_init(void)
599 {
600         BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
601         BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
602 
603         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
604                                         0, NULL);
605         if (flow_cache == NULL)
606                 return -ENOMEM;
607 
608         return 0;
609 }
610 
611 /* Uninitializes the flow module. */
612 void ovs_flow_exit(void)
613 {
614         kmem_cache_destroy(flow_cache);
615 }
616 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp