dst.c 6.3 KB

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