xref: /linux/include/net/dst_cache.h (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
1 /* SPDX-License-Identifier: GPL-2.0 */
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,
57 		       const struct in6_addr *saddr);
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  *
74  *	This does not free the cached dst to avoid races and contentions.
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 	WRITE_ONCE(dst_cache->reset_ts, jiffies);
80 }
81 
82 /**
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 /**
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
110