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