dst.c 6.5 KB

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