dst.c 6.4 KB

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