dst_cache.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * net/core/dst_cache.c - dst entry cache
  3. *
  4. * Copyright (c) 2016 Paolo Abeni <pabeni@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/percpu.h>
  13. #include <net/dst_cache.h>
  14. #include <net/route.h>
  15. #if IS_ENABLED(CONFIG_IPV6)
  16. #include <net/ip6_fib.h>
  17. #endif
  18. #include <uapi/linux/in.h>
  19. struct dst_cache_pcpu {
  20. unsigned long refresh_ts;
  21. struct dst_entry *dst;
  22. u32 cookie;
  23. union {
  24. struct in_addr in_saddr;
  25. struct in6_addr in6_saddr;
  26. };
  27. };
  28. static void dst_cache_per_cpu_dst_set(struct dst_cache_pcpu *dst_cache,
  29. struct dst_entry *dst, u32 cookie)
  30. {
  31. dst_release(dst_cache->dst);
  32. if (dst)
  33. dst_hold(dst);
  34. dst_cache->cookie = cookie;
  35. dst_cache->dst = dst;
  36. }
  37. static struct dst_entry *dst_cache_per_cpu_get(struct dst_cache *dst_cache,
  38. struct dst_cache_pcpu *idst)
  39. {
  40. struct dst_entry *dst;
  41. dst = idst->dst;
  42. if (!dst)
  43. goto fail;
  44. /* the cache already hold a dst reference; it can't go away */
  45. dst_hold(dst);
  46. if (unlikely(!time_after(idst->refresh_ts, dst_cache->reset_ts) ||
  47. (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. if (!dst_cache->cache)
  60. return NULL;
  61. return dst_cache_per_cpu_get(dst_cache, this_cpu_ptr(dst_cache->cache));
  62. }
  63. EXPORT_SYMBOL_GPL(dst_cache_get);
  64. struct rtable *dst_cache_get_ip4(struct dst_cache *dst_cache, __be32 *saddr)
  65. {
  66. struct dst_cache_pcpu *idst;
  67. struct dst_entry *dst;
  68. if (!dst_cache->cache)
  69. return NULL;
  70. idst = this_cpu_ptr(dst_cache->cache);
  71. dst = dst_cache_per_cpu_get(dst_cache, idst);
  72. if (!dst)
  73. return NULL;
  74. *saddr = idst->in_saddr.s_addr;
  75. return container_of(dst, struct rtable, dst);
  76. }
  77. EXPORT_SYMBOL_GPL(dst_cache_get_ip4);
  78. void dst_cache_set_ip4(struct dst_cache *dst_cache, struct dst_entry *dst,
  79. __be32 saddr)
  80. {
  81. struct dst_cache_pcpu *idst;
  82. if (!dst_cache->cache)
  83. return;
  84. idst = this_cpu_ptr(dst_cache->cache);
  85. dst_cache_per_cpu_dst_set(idst, dst, 0);
  86. idst->in_saddr.s_addr = saddr;
  87. }
  88. EXPORT_SYMBOL_GPL(dst_cache_set_ip4);
  89. #if IS_ENABLED(CONFIG_IPV6)
  90. void dst_cache_set_ip6(struct dst_cache *dst_cache, struct dst_entry *dst,
  91. const struct in6_addr *addr)
  92. {
  93. struct dst_cache_pcpu *idst;
  94. if (!dst_cache->cache)
  95. return;
  96. idst = this_cpu_ptr(dst_cache->cache);
  97. dst_cache_per_cpu_dst_set(this_cpu_ptr(dst_cache->cache), dst,
  98. rt6_get_cookie((struct rt6_info *)dst));
  99. idst->in6_saddr = *addr;
  100. }
  101. EXPORT_SYMBOL_GPL(dst_cache_set_ip6);
  102. struct dst_entry *dst_cache_get_ip6(struct dst_cache *dst_cache,
  103. struct in6_addr *saddr)
  104. {
  105. struct dst_cache_pcpu *idst;
  106. struct dst_entry *dst;
  107. if (!dst_cache->cache)
  108. return NULL;
  109. idst = this_cpu_ptr(dst_cache->cache);
  110. dst = dst_cache_per_cpu_get(dst_cache, idst);
  111. if (!dst)
  112. return NULL;
  113. *saddr = idst->in6_saddr;
  114. return dst;
  115. }
  116. EXPORT_SYMBOL_GPL(dst_cache_get_ip6);
  117. #endif
  118. int dst_cache_init(struct dst_cache *dst_cache, gfp_t gfp)
  119. {
  120. dst_cache->cache = alloc_percpu_gfp(struct dst_cache_pcpu,
  121. gfp | __GFP_ZERO);
  122. if (!dst_cache->cache)
  123. return -ENOMEM;
  124. dst_cache_reset(dst_cache);
  125. return 0;
  126. }
  127. EXPORT_SYMBOL_GPL(dst_cache_init);
  128. void dst_cache_destroy(struct dst_cache *dst_cache)
  129. {
  130. int i;
  131. if (!dst_cache->cache)
  132. return;
  133. for_each_possible_cpu(i)
  134. dst_release(per_cpu_ptr(dst_cache->cache, i)->dst);
  135. free_percpu(dst_cache->cache);
  136. }
  137. EXPORT_SYMBOL_GPL(dst_cache_destroy);