dst_cache.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. local_lock_t bh_lock;
  19. u32 cookie;
  20. union {
  21. struct in_addr in_saddr;
  22. struct in6_addr in6_saddr;
  23. };
  24. };
  25. static void dst_cache_per_cpu_dst_set(struct dst_cache_pcpu *dst_cache,
  26. struct dst_entry *dst, u32 cookie)
  27. {
  28. DEBUG_NET_WARN_ON_ONCE(!in_softirq());
  29. dst_release(dst_cache->dst);
  30. if (dst)
  31. dst_hold(dst);
  32. dst_cache->cookie = cookie;
  33. dst_cache->dst = dst;
  34. }
  35. static struct dst_entry *dst_cache_per_cpu_get(struct dst_cache *dst_cache,
  36. struct dst_cache_pcpu *idst)
  37. {
  38. struct dst_entry *dst;
  39. DEBUG_NET_WARN_ON_ONCE(!in_softirq());
  40. dst = idst->dst;
  41. if (!dst)
  42. goto fail;
  43. /* the cache already hold a dst reference; it can't go away */
  44. dst_hold(dst);
  45. if (unlikely(!time_after(idst->refresh_ts,
  46. READ_ONCE(dst_cache->reset_ts)) ||
  47. (READ_ONCE(dst->obsolete) && !dst->ops->check(dst, idst->cookie)))) {
  48. dst_cache_per_cpu_dst_set(idst, NULL, 0);
  49. dst_release(dst);
  50. goto fail;
  51. }
  52. return dst;
  53. fail:
  54. idst->refresh_ts = jiffies;
  55. return NULL;
  56. }
  57. struct dst_entry *dst_cache_get(struct dst_cache *dst_cache)
  58. {
  59. struct dst_entry *dst;
  60. if (!dst_cache->cache)
  61. return NULL;
  62. local_lock_nested_bh(&dst_cache->cache->bh_lock);
  63. dst = dst_cache_per_cpu_get(dst_cache, this_cpu_ptr(dst_cache->cache));
  64. local_unlock_nested_bh(&dst_cache->cache->bh_lock);
  65. return dst;
  66. }
  67. EXPORT_SYMBOL_GPL(dst_cache_get);
  68. struct rtable *dst_cache_get_ip4(struct dst_cache *dst_cache, __be32 *saddr)
  69. {
  70. struct dst_cache_pcpu *idst;
  71. struct dst_entry *dst;
  72. if (!dst_cache->cache)
  73. return NULL;
  74. local_lock_nested_bh(&dst_cache->cache->bh_lock);
  75. idst = this_cpu_ptr(dst_cache->cache);
  76. dst = dst_cache_per_cpu_get(dst_cache, idst);
  77. if (!dst) {
  78. local_unlock_nested_bh(&dst_cache->cache->bh_lock);
  79. return NULL;
  80. }
  81. *saddr = idst->in_saddr.s_addr;
  82. local_unlock_nested_bh(&dst_cache->cache->bh_lock);
  83. return dst_rtable(dst);
  84. }
  85. EXPORT_SYMBOL_GPL(dst_cache_get_ip4);
  86. void dst_cache_set_ip4(struct dst_cache *dst_cache, struct dst_entry *dst,
  87. __be32 saddr)
  88. {
  89. struct dst_cache_pcpu *idst;
  90. if (!dst_cache->cache)
  91. return;
  92. local_lock_nested_bh(&dst_cache->cache->bh_lock);
  93. idst = this_cpu_ptr(dst_cache->cache);
  94. dst_cache_per_cpu_dst_set(idst, dst, 0);
  95. idst->in_saddr.s_addr = saddr;
  96. local_unlock_nested_bh(&dst_cache->cache->bh_lock);
  97. }
  98. EXPORT_SYMBOL_GPL(dst_cache_set_ip4);
  99. #if IS_ENABLED(CONFIG_IPV6)
  100. void dst_cache_set_ip6(struct dst_cache *dst_cache, struct dst_entry *dst,
  101. const struct in6_addr *saddr)
  102. {
  103. struct dst_cache_pcpu *idst;
  104. if (!dst_cache->cache)
  105. return;
  106. local_lock_nested_bh(&dst_cache->cache->bh_lock);
  107. idst = this_cpu_ptr(dst_cache->cache);
  108. dst_cache_per_cpu_dst_set(idst, dst,
  109. rt6_get_cookie(dst_rt6_info(dst)));
  110. idst->in6_saddr = *saddr;
  111. local_unlock_nested_bh(&dst_cache->cache->bh_lock);
  112. }
  113. EXPORT_SYMBOL_GPL(dst_cache_set_ip6);
  114. struct dst_entry *dst_cache_get_ip6(struct dst_cache *dst_cache,
  115. struct in6_addr *saddr)
  116. {
  117. struct dst_cache_pcpu *idst;
  118. struct dst_entry *dst;
  119. if (!dst_cache->cache)
  120. return NULL;
  121. local_lock_nested_bh(&dst_cache->cache->bh_lock);
  122. idst = this_cpu_ptr(dst_cache->cache);
  123. dst = dst_cache_per_cpu_get(dst_cache, idst);
  124. if (!dst) {
  125. local_unlock_nested_bh(&dst_cache->cache->bh_lock);
  126. return NULL;
  127. }
  128. *saddr = idst->in6_saddr;
  129. local_unlock_nested_bh(&dst_cache->cache->bh_lock);
  130. return dst;
  131. }
  132. EXPORT_SYMBOL_GPL(dst_cache_get_ip6);
  133. #endif
  134. int dst_cache_init(struct dst_cache *dst_cache, gfp_t gfp)
  135. {
  136. unsigned int i;
  137. dst_cache->cache = alloc_percpu_gfp(struct dst_cache_pcpu,
  138. gfp | __GFP_ZERO);
  139. if (!dst_cache->cache)
  140. return -ENOMEM;
  141. for_each_possible_cpu(i)
  142. local_lock_init(&per_cpu_ptr(dst_cache->cache, i)->bh_lock);
  143. dst_cache_reset(dst_cache);
  144. return 0;
  145. }
  146. EXPORT_SYMBOL_GPL(dst_cache_init);
  147. void dst_cache_destroy(struct dst_cache *dst_cache)
  148. {
  149. int i;
  150. if (!dst_cache->cache)
  151. return;
  152. for_each_possible_cpu(i)
  153. dst_release(per_cpu_ptr(dst_cache->cache, i)->dst);
  154. free_percpu(dst_cache->cache);
  155. }
  156. EXPORT_SYMBOL_GPL(dst_cache_destroy);
  157. void dst_cache_reset_now(struct dst_cache *dst_cache)
  158. {
  159. int i;
  160. if (!dst_cache->cache)
  161. return;
  162. dst_cache_reset(dst_cache);
  163. for_each_possible_cpu(i) {
  164. struct dst_cache_pcpu *idst = per_cpu_ptr(dst_cache->cache, i);
  165. struct dst_entry *dst = idst->dst;
  166. idst->cookie = 0;
  167. idst->dst = NULL;
  168. dst_release(dst);
  169. }
  170. }
  171. EXPORT_SYMBOL_GPL(dst_cache_reset_now);