dst_cache.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/core/dst_cache.c - dst entry cache
  4. *
  5. * Copyright (c) 2016 Paolo Abeni <pabeni@redhat.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/percpu.h>
  9. #include <net/dst_cache.h>
  10. #include <net/route.h>
  11. #if IS_ENABLED(CONFIG_IPV6)
  12. #include <net/ip6_fib.h>
  13. #endif
  14. #include <uapi/linux/in.h>
  15. struct dst_cache_pcpu {
  16. unsigned long refresh_ts;
  17. struct dst_entry *dst;
  18. u32 cookie;
  19. union {
  20. struct in_addr in_saddr;
  21. struct in6_addr in6_saddr;
  22. };
  23. };
  24. static void dst_cache_per_cpu_dst_set(struct dst_cache_pcpu *dst_cache,
  25. struct dst_entry *dst, u32 cookie)
  26. {
  27. DEBUG_NET_WARN_ON_ONCE(!in_softirq());
  28. dst_release(dst_cache->dst);
  29. if (dst)
  30. dst_hold(dst);
  31. dst_cache->cookie = cookie;
  32. dst_cache->dst = dst;
  33. }
  34. static struct dst_entry *dst_cache_per_cpu_get(struct dst_cache *dst_cache,
  35. struct dst_cache_pcpu *idst)
  36. {
  37. struct dst_entry *dst;
  38. DEBUG_NET_WARN_ON_ONCE(!in_softirq());
  39. dst = idst->dst;
  40. if (!dst)
  41. goto fail;
  42. /* the cache already hold a dst reference; it can't go away */
  43. dst_hold(dst);
  44. if (unlikely(!time_after(idst->refresh_ts,
  45. READ_ONCE(dst_cache->reset_ts)) ||
  46. (dst->obsolete && !dst->ops->check(dst, idst->cookie)))) {
  47. dst_cache_per_cpu_dst_set(idst, NULL, 0);
  48. dst_release(dst);
  49. goto fail;
  50. }
  51. return dst;
  52. fail:
  53. idst->refresh_ts = jiffies;
  54. return NULL;
  55. }
  56. struct dst_entry *dst_cache_get(struct dst_cache *dst_cache)
  57. {
  58. if (!dst_cache->cache)
  59. return NULL;
  60. return dst_cache_per_cpu_get(dst_cache, this_cpu_ptr(dst_cache->cache));
  61. }
  62. EXPORT_SYMBOL_GPL(dst_cache_get);
  63. struct rtable *dst_cache_get_ip4(struct dst_cache *dst_cache, __be32 *saddr)
  64. {
  65. struct dst_cache_pcpu *idst;
  66. struct dst_entry *dst;
  67. if (!dst_cache->cache)
  68. return NULL;
  69. idst = this_cpu_ptr(dst_cache->cache);
  70. dst = dst_cache_per_cpu_get(dst_cache, idst);
  71. if (!dst)
  72. return NULL;
  73. *saddr = idst->in_saddr.s_addr;
  74. return dst_rtable(dst);
  75. }
  76. EXPORT_SYMBOL_GPL(dst_cache_get_ip4);
  77. void dst_cache_set_ip4(struct dst_cache *dst_cache, struct dst_entry *dst,
  78. __be32 saddr)
  79. {
  80. struct dst_cache_pcpu *idst;
  81. if (!dst_cache->cache)
  82. return;
  83. idst = this_cpu_ptr(dst_cache->cache);
  84. dst_cache_per_cpu_dst_set(idst, dst, 0);
  85. idst->in_saddr.s_addr = saddr;
  86. }
  87. EXPORT_SYMBOL_GPL(dst_cache_set_ip4);
  88. #if IS_ENABLED(CONFIG_IPV6)
  89. void dst_cache_set_ip6(struct dst_cache *dst_cache, struct dst_entry *dst,
  90. const struct in6_addr *saddr)
  91. {
  92. struct dst_cache_pcpu *idst;
  93. if (!dst_cache->cache)
  94. return;
  95. idst = this_cpu_ptr(dst_cache->cache);
  96. dst_cache_per_cpu_dst_set(idst, dst,
  97. rt6_get_cookie(dst_rt6_info(dst)));
  98. idst->in6_saddr = *saddr;
  99. }
  100. EXPORT_SYMBOL_GPL(dst_cache_set_ip6);
  101. struct dst_entry *dst_cache_get_ip6(struct dst_cache *dst_cache,
  102. struct in6_addr *saddr)
  103. {
  104. struct dst_cache_pcpu *idst;
  105. struct dst_entry *dst;
  106. if (!dst_cache->cache)
  107. return NULL;
  108. idst = this_cpu_ptr(dst_cache->cache);
  109. dst = dst_cache_per_cpu_get(dst_cache, idst);
  110. if (!dst)
  111. return NULL;
  112. *saddr = idst->in6_saddr;
  113. return dst;
  114. }
  115. EXPORT_SYMBOL_GPL(dst_cache_get_ip6);
  116. #endif
  117. int dst_cache_init(struct dst_cache *dst_cache, gfp_t gfp)
  118. {
  119. dst_cache->cache = alloc_percpu_gfp(struct dst_cache_pcpu,
  120. gfp | __GFP_ZERO);
  121. if (!dst_cache->cache)
  122. return -ENOMEM;
  123. dst_cache_reset(dst_cache);
  124. return 0;
  125. }
  126. EXPORT_SYMBOL_GPL(dst_cache_init);
  127. void dst_cache_destroy(struct dst_cache *dst_cache)
  128. {
  129. int i;
  130. if (!dst_cache->cache)
  131. return;
  132. for_each_possible_cpu(i)
  133. dst_release(per_cpu_ptr(dst_cache->cache, i)->dst);
  134. free_percpu(dst_cache->cache);
  135. }
  136. EXPORT_SYMBOL_GPL(dst_cache_destroy);
  137. void dst_cache_reset_now(struct dst_cache *dst_cache)
  138. {
  139. int i;
  140. if (!dst_cache->cache)
  141. return;
  142. dst_cache_reset(dst_cache);
  143. for_each_possible_cpu(i) {
  144. struct dst_cache_pcpu *idst = per_cpu_ptr(dst_cache->cache, i);
  145. struct dst_entry *dst = idst->dst;
  146. idst->cookie = 0;
  147. idst->dst = NULL;
  148. dst_release(dst);
  149. }
  150. }
  151. EXPORT_SYMBOL_GPL(dst_cache_reset_now);