dst_cache.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _NET_DST_CACHE_H
  3. #define _NET_DST_CACHE_H
  4. #include <linux/jiffies.h>
  5. #include <net/dst.h>
  6. #if IS_ENABLED(CONFIG_IPV6)
  7. #include <net/ip6_fib.h>
  8. #endif
  9. struct dst_cache {
  10. struct dst_cache_pcpu __percpu *cache;
  11. unsigned long reset_ts;
  12. };
  13. /**
  14. * dst_cache_get - perform cache lookup
  15. * @dst_cache: the cache
  16. *
  17. * The caller should use dst_cache_get_ip4() if it need to retrieve the
  18. * source address to be used when xmitting to the cached dst.
  19. * local BH must be disabled.
  20. */
  21. struct dst_entry *dst_cache_get(struct dst_cache *dst_cache);
  22. /**
  23. * dst_cache_get_ip4 - perform cache lookup and fetch ipv4 source address
  24. * @dst_cache: the cache
  25. * @saddr: return value for the retrieved source address
  26. *
  27. * local BH must be disabled.
  28. */
  29. struct rtable *dst_cache_get_ip4(struct dst_cache *dst_cache, __be32 *saddr);
  30. /**
  31. * dst_cache_set_ip4 - store the ipv4 dst into the cache
  32. * @dst_cache: the cache
  33. * @dst: the entry to be cached
  34. * @saddr: the source address to be stored inside the cache
  35. *
  36. * local BH must be disabled.
  37. */
  38. void dst_cache_set_ip4(struct dst_cache *dst_cache, struct dst_entry *dst,
  39. __be32 saddr);
  40. #if IS_ENABLED(CONFIG_IPV6)
  41. /**
  42. * dst_cache_set_ip6 - store the ipv6 dst into the cache
  43. * @dst_cache: the cache
  44. * @dst: the entry to be cached
  45. * @saddr: the source address to be stored inside the cache
  46. *
  47. * local BH must be disabled.
  48. */
  49. void dst_cache_set_ip6(struct dst_cache *dst_cache, struct dst_entry *dst,
  50. const struct in6_addr *saddr);
  51. /**
  52. * dst_cache_get_ip6 - perform cache lookup and fetch ipv6 source address
  53. * @dst_cache: the cache
  54. * @saddr: return value for the retrieved source address
  55. *
  56. * local BH must be disabled.
  57. */
  58. struct dst_entry *dst_cache_get_ip6(struct dst_cache *dst_cache,
  59. struct in6_addr *saddr);
  60. #endif
  61. /**
  62. * dst_cache_reset - invalidate the cache contents
  63. * @dst_cache: the cache
  64. *
  65. * This does not free the cached dst to avoid races and contentions.
  66. * the dst will be freed on later cache lookup.
  67. */
  68. static inline void dst_cache_reset(struct dst_cache *dst_cache)
  69. {
  70. dst_cache->reset_ts = jiffies;
  71. }
  72. /**
  73. * dst_cache_init - initialize the cache, allocating the required storage
  74. * @dst_cache: the cache
  75. * @gfp: allocation flags
  76. */
  77. int dst_cache_init(struct dst_cache *dst_cache, gfp_t gfp);
  78. /**
  79. * dst_cache_destroy - empty the cache and free the allocated storage
  80. * @dst_cache: the cache
  81. *
  82. * No synchronization is enforced: it must be called only when the cache
  83. * is unsed.
  84. */
  85. void dst_cache_destroy(struct dst_cache *dst_cache);
  86. #endif