dst.c 6.1 KB

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