dst.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * net/core/dst.c Protocol independent destination cache.
  3. *
  4. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  5. *
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <net/dst.h>
  18. /* Locking strategy:
  19. * 1) Garbage collection state of dead destination cache
  20. * entries is protected by dst_lock.
  21. * 2) GC is run only from BH context, and is the only remover
  22. * of entries.
  23. * 3) Entries are added to the garbage list from both BH
  24. * and non-BH context, so local BH disabling is needed.
  25. * 4) All operations modify state, so a spinlock is used.
  26. */
  27. static struct dst_entry *dst_garbage_list;
  28. #if RT_CACHE_DEBUG >= 2
  29. static atomic_t dst_total = ATOMIC_INIT(0);
  30. #endif
  31. static DEFINE_SPINLOCK(dst_lock);
  32. static unsigned long dst_gc_timer_expires;
  33. static unsigned long dst_gc_timer_inc = DST_GC_MAX;
  34. static void dst_run_gc(unsigned long);
  35. static void ___dst_free(struct dst_entry * dst);
  36. static DEFINE_TIMER(dst_gc_timer, dst_run_gc, DST_GC_MIN, 0);
  37. static void dst_run_gc(unsigned long dummy)
  38. {
  39. int delayed = 0;
  40. int work_performed;
  41. struct dst_entry * dst, **dstp;
  42. if (!spin_trylock(&dst_lock)) {
  43. mod_timer(&dst_gc_timer, jiffies + HZ/10);
  44. return;
  45. }
  46. del_timer(&dst_gc_timer);
  47. dstp = &dst_garbage_list;
  48. work_performed = 0;
  49. while ((dst = *dstp) != NULL) {
  50. if (atomic_read(&dst->__refcnt)) {
  51. dstp = &dst->next;
  52. delayed++;
  53. continue;
  54. }
  55. *dstp = dst->next;
  56. work_performed = 1;
  57. dst = dst_destroy(dst);
  58. if (dst) {
  59. /* NOHASH and still referenced. Unless it is already
  60. * on gc list, invalidate it and add to gc list.
  61. *
  62. * Note: this is temporary. Actually, NOHASH dst's
  63. * must be obsoleted when parent is obsoleted.
  64. * But we do not have state "obsoleted, but
  65. * referenced by parent", so it is right.
  66. */
  67. if (dst->obsolete > 1)
  68. continue;
  69. ___dst_free(dst);
  70. dst->next = *dstp;
  71. *dstp = dst;
  72. dstp = &dst->next;
  73. }
  74. }
  75. if (!dst_garbage_list) {
  76. dst_gc_timer_inc = DST_GC_MAX;
  77. goto out;
  78. }
  79. if (!work_performed) {
  80. if ((dst_gc_timer_expires += dst_gc_timer_inc) > DST_GC_MAX)
  81. dst_gc_timer_expires = DST_GC_MAX;
  82. dst_gc_timer_inc += DST_GC_INC;
  83. } else {
  84. dst_gc_timer_inc = DST_GC_INC;
  85. dst_gc_timer_expires = DST_GC_MIN;
  86. }
  87. #if RT_CACHE_DEBUG >= 2
  88. printk("dst_total: %d/%d %ld\n",
  89. atomic_read(&dst_total), delayed, dst_gc_timer_expires);
  90. #endif
  91. /* if the next desired timer is more than 4 seconds in the future
  92. * then round the timer to whole seconds
  93. */
  94. if (dst_gc_timer_expires > 4*HZ)
  95. mod_timer(&dst_gc_timer,
  96. round_jiffies(jiffies + dst_gc_timer_expires));
  97. else
  98. mod_timer(&dst_gc_timer, jiffies + dst_gc_timer_expires);
  99. out:
  100. spin_unlock(&dst_lock);
  101. }
  102. static int dst_discard_in(struct sk_buff *skb)
  103. {
  104. kfree_skb(skb);
  105. return 0;
  106. }
  107. static int dst_discard_out(struct sk_buff *skb)
  108. {
  109. kfree_skb(skb);
  110. return 0;
  111. }
  112. void * dst_alloc(struct dst_ops * ops)
  113. {
  114. struct dst_entry * dst;
  115. if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
  116. if (ops->gc())
  117. return NULL;
  118. }
  119. dst = kmem_cache_zalloc(ops->kmem_cachep, GFP_ATOMIC);
  120. if (!dst)
  121. return NULL;
  122. atomic_set(&dst->__refcnt, 0);
  123. dst->ops = ops;
  124. dst->lastuse = jiffies;
  125. dst->path = dst;
  126. dst->input = dst_discard_in;
  127. dst->output = dst_discard_out;
  128. #if RT_CACHE_DEBUG >= 2
  129. atomic_inc(&dst_total);
  130. #endif
  131. atomic_inc(&ops->entries);
  132. return dst;
  133. }
  134. static void ___dst_free(struct dst_entry * dst)
  135. {
  136. /* The first case (dev==NULL) is required, when
  137. protocol module is unloaded.
  138. */
  139. if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
  140. dst->input = dst_discard_in;
  141. dst->output = dst_discard_out;
  142. }
  143. dst->obsolete = 2;
  144. }
  145. void __dst_free(struct dst_entry * dst)
  146. {
  147. spin_lock_bh(&dst_lock);
  148. ___dst_free(dst);
  149. dst->next = dst_garbage_list;
  150. dst_garbage_list = dst;
  151. if (dst_gc_timer_inc > DST_GC_INC) {
  152. dst_gc_timer_inc = DST_GC_INC;
  153. dst_gc_timer_expires = DST_GC_MIN;
  154. mod_timer(&dst_gc_timer, jiffies + dst_gc_timer_expires);
  155. }
  156. spin_unlock_bh(&dst_lock);
  157. }
  158. struct dst_entry *dst_destroy(struct dst_entry * dst)
  159. {
  160. struct dst_entry *child;
  161. struct neighbour *neigh;
  162. struct hh_cache *hh;
  163. smp_rmb();
  164. again:
  165. neigh = dst->neighbour;
  166. hh = dst->hh;
  167. child = dst->child;
  168. dst->hh = NULL;
  169. if (hh && atomic_dec_and_test(&hh->hh_refcnt))
  170. kfree(hh);
  171. if (neigh) {
  172. dst->neighbour = NULL;
  173. neigh_release(neigh);
  174. }
  175. atomic_dec(&dst->ops->entries);
  176. if (dst->ops->destroy)
  177. dst->ops->destroy(dst);
  178. if (dst->dev)
  179. dev_put(dst->dev);
  180. #if RT_CACHE_DEBUG >= 2
  181. atomic_dec(&dst_total);
  182. #endif
  183. kmem_cache_free(dst->ops->kmem_cachep, dst);
  184. dst = child;
  185. if (dst) {
  186. int nohash = dst->flags & DST_NOHASH;
  187. if (atomic_dec_and_test(&dst->__refcnt)) {
  188. /* We were real parent of this dst, so kill child. */
  189. if (nohash)
  190. goto again;
  191. } else {
  192. /* Child is still referenced, return it for freeing. */
  193. if (nohash)
  194. return dst;
  195. /* Child is still in his hash table */
  196. }
  197. }
  198. return NULL;
  199. }
  200. /* Dirty hack. We did it in 2.2 (in __dst_free),
  201. * we have _very_ good reasons not to repeat
  202. * this mistake in 2.3, but we have no choice
  203. * now. _It_ _is_ _explicit_ _deliberate_
  204. * _race_ _condition_.
  205. *
  206. * Commented and originally written by Alexey.
  207. */
  208. static inline void dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  209. int unregister)
  210. {
  211. if (dst->ops->ifdown)
  212. dst->ops->ifdown(dst, dev, unregister);
  213. if (dev != dst->dev)
  214. return;
  215. if (!unregister) {
  216. dst->input = dst_discard_in;
  217. dst->output = dst_discard_out;
  218. } else {
  219. dst->dev = &loopback_dev;
  220. dev_hold(&loopback_dev);
  221. dev_put(dev);
  222. if (dst->neighbour && dst->neighbour->dev == dev) {
  223. dst->neighbour->dev = &loopback_dev;
  224. dev_put(dev);
  225. dev_hold(&loopback_dev);
  226. }
  227. }
  228. }
  229. static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  230. {
  231. struct net_device *dev = ptr;
  232. struct dst_entry *dst;
  233. switch (event) {
  234. case NETDEV_UNREGISTER:
  235. case NETDEV_DOWN:
  236. spin_lock_bh(&dst_lock);
  237. for (dst = dst_garbage_list; dst; dst = dst->next) {
  238. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  239. }
  240. spin_unlock_bh(&dst_lock);
  241. break;
  242. }
  243. return NOTIFY_DONE;
  244. }
  245. static struct notifier_block dst_dev_notifier = {
  246. .notifier_call = dst_dev_event,
  247. };
  248. void __init dst_init(void)
  249. {
  250. register_netdevice_notifier(&dst_dev_notifier);
  251. }
  252. EXPORT_SYMBOL(__dst_free);
  253. EXPORT_SYMBOL(dst_alloc);
  254. EXPORT_SYMBOL(dst_destroy);