Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Paolo Abeni | 911362c | 2016-02-12 15:43:53 +0100 | [diff] [blame] | 2 | #ifndef _NET_DST_CACHE_H |
| 3 | #define _NET_DST_CACHE_H |
| 4 | |
| 5 | #include <linux/jiffies.h> |
| 6 | #include <net/dst.h> |
| 7 | #if IS_ENABLED(CONFIG_IPV6) |
| 8 | #include <net/ip6_fib.h> |
| 9 | #endif |
| 10 | |
| 11 | struct dst_cache { |
| 12 | struct dst_cache_pcpu __percpu *cache; |
| 13 | unsigned long reset_ts; |
| 14 | }; |
| 15 | |
| 16 | /** |
| 17 | * dst_cache_get - perform cache lookup |
| 18 | * @dst_cache: the cache |
| 19 | * |
| 20 | * The caller should use dst_cache_get_ip4() if it need to retrieve the |
| 21 | * source address to be used when xmitting to the cached dst. |
| 22 | * local BH must be disabled. |
| 23 | */ |
| 24 | struct dst_entry *dst_cache_get(struct dst_cache *dst_cache); |
| 25 | |
| 26 | /** |
| 27 | * dst_cache_get_ip4 - perform cache lookup and fetch ipv4 source address |
| 28 | * @dst_cache: the cache |
| 29 | * @saddr: return value for the retrieved source address |
| 30 | * |
| 31 | * local BH must be disabled. |
| 32 | */ |
| 33 | struct rtable *dst_cache_get_ip4(struct dst_cache *dst_cache, __be32 *saddr); |
| 34 | |
| 35 | /** |
| 36 | * dst_cache_set_ip4 - store the ipv4 dst into the cache |
| 37 | * @dst_cache: the cache |
| 38 | * @dst: the entry to be cached |
| 39 | * @saddr: the source address to be stored inside the cache |
| 40 | * |
| 41 | * local BH must be disabled. |
| 42 | */ |
| 43 | void dst_cache_set_ip4(struct dst_cache *dst_cache, struct dst_entry *dst, |
| 44 | __be32 saddr); |
| 45 | |
| 46 | #if IS_ENABLED(CONFIG_IPV6) |
| 47 | |
| 48 | /** |
| 49 | * dst_cache_set_ip6 - store the ipv6 dst into the cache |
| 50 | * @dst_cache: the cache |
| 51 | * @dst: the entry to be cached |
| 52 | * @saddr: the source address to be stored inside the cache |
| 53 | * |
| 54 | * local BH must be disabled. |
| 55 | */ |
| 56 | void dst_cache_set_ip6(struct dst_cache *dst_cache, struct dst_entry *dst, |
Jonathan Neuschäfer | 4c1342d | 2018-03-04 03:29:52 +0100 | [diff] [blame] | 57 | const struct in6_addr *saddr); |
Paolo Abeni | 911362c | 2016-02-12 15:43:53 +0100 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * dst_cache_get_ip6 - perform cache lookup and fetch ipv6 source address |
| 61 | * @dst_cache: the cache |
| 62 | * @saddr: return value for the retrieved source address |
| 63 | * |
| 64 | * local BH must be disabled. |
| 65 | */ |
| 66 | struct dst_entry *dst_cache_get_ip6(struct dst_cache *dst_cache, |
| 67 | struct in6_addr *saddr); |
| 68 | #endif |
| 69 | |
| 70 | /** |
| 71 | * dst_cache_reset - invalidate the cache contents |
| 72 | * @dst_cache: the cache |
| 73 | * |
Jonathan Neuschäfer | 76b1297 | 2018-03-04 03:29:51 +0100 | [diff] [blame] | 74 | * This does not free the cached dst to avoid races and contentions. |
Paolo Abeni | 911362c | 2016-02-12 15:43:53 +0100 | [diff] [blame] | 75 | * the dst will be freed on later cache lookup. |
| 76 | */ |
| 77 | static inline void dst_cache_reset(struct dst_cache *dst_cache) |
| 78 | { |
| 79 | dst_cache->reset_ts = jiffies; |
| 80 | } |
| 81 | |
| 82 | /** |
Jason A. Donenfeld | 20ae1d6 | 2021-11-29 10:39:25 -0500 | [diff] [blame] | 83 | * dst_cache_reset_now - invalidate the cache contents immediately |
| 84 | * @dst_cache: the cache |
| 85 | * |
| 86 | * The caller must be sure there are no concurrent users, as this frees |
| 87 | * all dst_cache users immediately, rather than waiting for the next |
| 88 | * per-cpu usage like dst_cache_reset does. Most callers should use the |
| 89 | * higher speed lazily-freed dst_cache_reset function instead. |
| 90 | */ |
| 91 | void dst_cache_reset_now(struct dst_cache *dst_cache); |
| 92 | |
| 93 | /** |
Paolo Abeni | 911362c | 2016-02-12 15:43:53 +0100 | [diff] [blame] | 94 | * dst_cache_init - initialize the cache, allocating the required storage |
| 95 | * @dst_cache: the cache |
| 96 | * @gfp: allocation flags |
| 97 | */ |
| 98 | int dst_cache_init(struct dst_cache *dst_cache, gfp_t gfp); |
| 99 | |
| 100 | /** |
| 101 | * dst_cache_destroy - empty the cache and free the allocated storage |
| 102 | * @dst_cache: the cache |
| 103 | * |
| 104 | * No synchronization is enforced: it must be called only when the cache |
| 105 | * is unsed. |
| 106 | */ |
| 107 | void dst_cache_destroy(struct dst_cache *dst_cache); |
| 108 | |
| 109 | #endif |