dst.c 6.3 KB

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