dst.c 6.2 KB

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