dst.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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(struct sk_buff *skb)
  103. {
  104. kfree_skb(skb);
  105. return 0;
  106. }
  107. void * dst_alloc(struct dst_ops * ops)
  108. {
  109. struct dst_entry * dst;
  110. if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
  111. if (ops->gc())
  112. return NULL;
  113. }
  114. dst = kmem_cache_zalloc(ops->kmem_cachep, GFP_ATOMIC);
  115. if (!dst)
  116. return NULL;
  117. atomic_set(&dst->__refcnt, 0);
  118. dst->ops = ops;
  119. dst->lastuse = jiffies;
  120. dst->path = dst;
  121. dst->input = dst->output = dst_discard;
  122. #if RT_CACHE_DEBUG >= 2
  123. atomic_inc(&dst_total);
  124. #endif
  125. atomic_inc(&ops->entries);
  126. return dst;
  127. }
  128. static void ___dst_free(struct dst_entry * dst)
  129. {
  130. /* The first case (dev==NULL) is required, when
  131. protocol module is unloaded.
  132. */
  133. if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
  134. dst->input = dst->output = dst_discard;
  135. }
  136. dst->obsolete = 2;
  137. }
  138. void __dst_free(struct dst_entry * dst)
  139. {
  140. spin_lock_bh(&dst_lock);
  141. ___dst_free(dst);
  142. dst->next = dst_garbage_list;
  143. dst_garbage_list = dst;
  144. if (dst_gc_timer_inc > DST_GC_INC) {
  145. dst_gc_timer_inc = DST_GC_INC;
  146. dst_gc_timer_expires = DST_GC_MIN;
  147. mod_timer(&dst_gc_timer, jiffies + dst_gc_timer_expires);
  148. }
  149. spin_unlock_bh(&dst_lock);
  150. }
  151. struct dst_entry *dst_destroy(struct dst_entry * dst)
  152. {
  153. struct dst_entry *child;
  154. struct neighbour *neigh;
  155. struct hh_cache *hh;
  156. smp_rmb();
  157. again:
  158. neigh = dst->neighbour;
  159. hh = dst->hh;
  160. child = dst->child;
  161. dst->hh = NULL;
  162. if (hh && atomic_dec_and_test(&hh->hh_refcnt))
  163. kfree(hh);
  164. if (neigh) {
  165. dst->neighbour = NULL;
  166. neigh_release(neigh);
  167. }
  168. atomic_dec(&dst->ops->entries);
  169. if (dst->ops->destroy)
  170. dst->ops->destroy(dst);
  171. if (dst->dev)
  172. dev_put(dst->dev);
  173. #if RT_CACHE_DEBUG >= 2
  174. atomic_dec(&dst_total);
  175. #endif
  176. kmem_cache_free(dst->ops->kmem_cachep, dst);
  177. dst = child;
  178. if (dst) {
  179. int nohash = dst->flags & DST_NOHASH;
  180. if (atomic_dec_and_test(&dst->__refcnt)) {
  181. /* We were real parent of this dst, so kill child. */
  182. if (nohash)
  183. goto again;
  184. } else {
  185. /* Child is still referenced, return it for freeing. */
  186. if (nohash)
  187. return dst;
  188. /* Child is still in his hash table */
  189. }
  190. }
  191. return NULL;
  192. }
  193. /* Dirty hack. We did it in 2.2 (in __dst_free),
  194. * we have _very_ good reasons not to repeat
  195. * this mistake in 2.3, but we have no choice
  196. * now. _It_ _is_ _explicit_ _deliberate_
  197. * _race_ _condition_.
  198. *
  199. * Commented and originally written by Alexey.
  200. */
  201. static inline void dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  202. int unregister)
  203. {
  204. if (dst->ops->ifdown)
  205. dst->ops->ifdown(dst, dev, unregister);
  206. if (dev != dst->dev)
  207. return;
  208. if (!unregister) {
  209. dst->input = dst->output = dst_discard;
  210. } else {
  211. dst->dev = &loopback_dev;
  212. dev_hold(&loopback_dev);
  213. dev_put(dev);
  214. if (dst->neighbour && dst->neighbour->dev == dev) {
  215. dst->neighbour->dev = &loopback_dev;
  216. dev_put(dev);
  217. dev_hold(&loopback_dev);
  218. }
  219. }
  220. }
  221. static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  222. {
  223. struct net_device *dev = ptr;
  224. struct dst_entry *dst;
  225. switch (event) {
  226. case NETDEV_UNREGISTER:
  227. case NETDEV_DOWN:
  228. spin_lock_bh(&dst_lock);
  229. for (dst = dst_garbage_list; dst; dst = dst->next) {
  230. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  231. }
  232. spin_unlock_bh(&dst_lock);
  233. break;
  234. }
  235. return NOTIFY_DONE;
  236. }
  237. static struct notifier_block dst_dev_notifier = {
  238. .notifier_call = dst_dev_event,
  239. };
  240. void __init dst_init(void)
  241. {
  242. register_netdevice_notifier(&dst_dev_notifier);
  243. }
  244. EXPORT_SYMBOL(__dst_free);
  245. EXPORT_SYMBOL(dst_alloc);
  246. EXPORT_SYMBOL(dst_destroy);