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