dst.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/workqueue.h>
  12. #include <linux/mm.h>
  13. #include <linux/module.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <net/net_namespace.h>
  19. #include <net/dst.h>
  20. /*
  21. * Theory of operations:
  22. * 1) We use a list, protected by a spinlock, to add
  23. * new entries from both BH and non-BH context.
  24. * 2) In order to keep spinlock held for a small delay,
  25. * we use a second list where are stored long lived
  26. * entries, that are handled by the garbage collect thread
  27. * fired by a workqueue.
  28. * 3) This list is guarded by a mutex,
  29. * so that the gc_task and dst_dev_event() can be synchronized.
  30. */
  31. #if RT_CACHE_DEBUG >= 2
  32. static atomic_t dst_total = ATOMIC_INIT(0);
  33. #endif
  34. /*
  35. * We want to keep lock & list close together
  36. * to dirty as few cache lines as possible in __dst_free().
  37. * As this is not a very strong hint, we dont force an alignment on SMP.
  38. */
  39. static struct {
  40. spinlock_t lock;
  41. struct dst_entry *list;
  42. unsigned long timer_inc;
  43. unsigned long timer_expires;
  44. } dst_garbage = {
  45. .lock = __SPIN_LOCK_UNLOCKED(dst_garbage.lock),
  46. .timer_inc = DST_GC_MAX,
  47. };
  48. static void dst_gc_task(struct work_struct *work);
  49. static void ___dst_free(struct dst_entry * dst);
  50. static DECLARE_DELAYED_WORK(dst_gc_work, dst_gc_task);
  51. static DEFINE_MUTEX(dst_gc_mutex);
  52. /*
  53. * long lived entries are maintained in this list, guarded by dst_gc_mutex
  54. */
  55. static struct dst_entry *dst_busy_list;
  56. static void dst_gc_task(struct work_struct *work)
  57. {
  58. int delayed = 0;
  59. int work_performed = 0;
  60. unsigned long expires = ~0L;
  61. struct dst_entry *dst, *next, head;
  62. struct dst_entry *last = &head;
  63. #if RT_CACHE_DEBUG >= 2
  64. ktime_t time_start = ktime_get();
  65. struct timespec elapsed;
  66. #endif
  67. mutex_lock(&dst_gc_mutex);
  68. next = dst_busy_list;
  69. loop:
  70. while ((dst = next) != NULL) {
  71. next = dst->next;
  72. prefetch(&next->next);
  73. if (likely(atomic_read(&dst->__refcnt))) {
  74. last->next = dst;
  75. last = dst;
  76. delayed++;
  77. continue;
  78. }
  79. work_performed++;
  80. dst = dst_destroy(dst);
  81. if (dst) {
  82. /* NOHASH and still referenced. Unless it is already
  83. * on gc list, invalidate it and add to gc list.
  84. *
  85. * Note: this is temporary. Actually, NOHASH dst's
  86. * must be obsoleted when parent is obsoleted.
  87. * But we do not have state "obsoleted, but
  88. * referenced by parent", so it is right.
  89. */
  90. if (dst->obsolete > 1)
  91. continue;
  92. ___dst_free(dst);
  93. dst->next = next;
  94. next = dst;
  95. }
  96. }
  97. spin_lock_bh(&dst_garbage.lock);
  98. next = dst_garbage.list;
  99. if (next) {
  100. dst_garbage.list = NULL;
  101. spin_unlock_bh(&dst_garbage.lock);
  102. goto loop;
  103. }
  104. last->next = NULL;
  105. dst_busy_list = head.next;
  106. if (!dst_busy_list)
  107. dst_garbage.timer_inc = DST_GC_MAX;
  108. else {
  109. /*
  110. * if we freed less than 1/10 of delayed entries,
  111. * we can sleep longer.
  112. */
  113. if (work_performed <= delayed/10) {
  114. dst_garbage.timer_expires += dst_garbage.timer_inc;
  115. if (dst_garbage.timer_expires > DST_GC_MAX)
  116. dst_garbage.timer_expires = DST_GC_MAX;
  117. dst_garbage.timer_inc += DST_GC_INC;
  118. } else {
  119. dst_garbage.timer_inc = DST_GC_INC;
  120. dst_garbage.timer_expires = DST_GC_MIN;
  121. }
  122. expires = dst_garbage.timer_expires;
  123. /*
  124. * if the next desired timer is more than 4 seconds in the future
  125. * then round the timer to whole seconds
  126. */
  127. if (expires > 4*HZ)
  128. expires = round_jiffies_relative(expires);
  129. schedule_delayed_work(&dst_gc_work, expires);
  130. }
  131. spin_unlock_bh(&dst_garbage.lock);
  132. mutex_unlock(&dst_gc_mutex);
  133. #if RT_CACHE_DEBUG >= 2
  134. elapsed = ktime_to_timespec(ktime_sub(ktime_get(), time_start));
  135. printk(KERN_DEBUG "dst_total: %d delayed: %d work_perf: %d"
  136. " expires: %lu elapsed: %lu us\n",
  137. atomic_read(&dst_total), delayed, work_performed,
  138. expires,
  139. elapsed.tv_sec * USEC_PER_SEC + elapsed.tv_nsec / NSEC_PER_USEC);
  140. #endif
  141. }
  142. static int dst_discard(struct sk_buff *skb)
  143. {
  144. kfree_skb(skb);
  145. return 0;
  146. }
  147. void * dst_alloc(struct dst_ops * ops)
  148. {
  149. struct dst_entry * dst;
  150. if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
  151. if (ops->gc())
  152. return NULL;
  153. }
  154. dst = kmem_cache_zalloc(ops->kmem_cachep, GFP_ATOMIC);
  155. if (!dst)
  156. return NULL;
  157. atomic_set(&dst->__refcnt, 0);
  158. dst->ops = ops;
  159. dst->lastuse = jiffies;
  160. dst->path = dst;
  161. dst->input = dst->output = dst_discard;
  162. #if RT_CACHE_DEBUG >= 2
  163. atomic_inc(&dst_total);
  164. #endif
  165. atomic_inc(&ops->entries);
  166. return dst;
  167. }
  168. static void ___dst_free(struct dst_entry * dst)
  169. {
  170. /* The first case (dev==NULL) is required, when
  171. protocol module is unloaded.
  172. */
  173. if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
  174. dst->input = dst->output = dst_discard;
  175. }
  176. dst->obsolete = 2;
  177. }
  178. void __dst_free(struct dst_entry * dst)
  179. {
  180. spin_lock_bh(&dst_garbage.lock);
  181. ___dst_free(dst);
  182. dst->next = dst_garbage.list;
  183. dst_garbage.list = dst;
  184. if (dst_garbage.timer_inc > DST_GC_INC) {
  185. dst_garbage.timer_inc = DST_GC_INC;
  186. dst_garbage.timer_expires = DST_GC_MIN;
  187. schedule_delayed_work(&dst_gc_work, dst_garbage.timer_expires);
  188. }
  189. spin_unlock_bh(&dst_garbage.lock);
  190. }
  191. struct dst_entry *dst_destroy(struct dst_entry * dst)
  192. {
  193. struct dst_entry *child;
  194. struct neighbour *neigh;
  195. struct hh_cache *hh;
  196. smp_rmb();
  197. again:
  198. neigh = dst->neighbour;
  199. hh = dst->hh;
  200. child = dst->child;
  201. dst->hh = NULL;
  202. if (hh && atomic_dec_and_test(&hh->hh_refcnt))
  203. kfree(hh);
  204. if (neigh) {
  205. dst->neighbour = NULL;
  206. neigh_release(neigh);
  207. }
  208. atomic_dec(&dst->ops->entries);
  209. if (dst->ops->destroy)
  210. dst->ops->destroy(dst);
  211. if (dst->dev)
  212. dev_put(dst->dev);
  213. #if RT_CACHE_DEBUG >= 2
  214. atomic_dec(&dst_total);
  215. #endif
  216. kmem_cache_free(dst->ops->kmem_cachep, dst);
  217. dst = child;
  218. if (dst) {
  219. int nohash = dst->flags & DST_NOHASH;
  220. if (atomic_dec_and_test(&dst->__refcnt)) {
  221. /* We were real parent of this dst, so kill child. */
  222. if (nohash)
  223. goto again;
  224. } else {
  225. /* Child is still referenced, return it for freeing. */
  226. if (nohash)
  227. return dst;
  228. /* Child is still in his hash table */
  229. }
  230. }
  231. return NULL;
  232. }
  233. /* Dirty hack. We did it in 2.2 (in __dst_free),
  234. * we have _very_ good reasons not to repeat
  235. * this mistake in 2.3, but we have no choice
  236. * now. _It_ _is_ _explicit_ _deliberate_
  237. * _race_ _condition_.
  238. *
  239. * Commented and originally written by Alexey.
  240. */
  241. static inline void dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  242. int unregister)
  243. {
  244. if (dst->ops->ifdown)
  245. dst->ops->ifdown(dst, dev, unregister);
  246. if (dev != dst->dev)
  247. return;
  248. if (!unregister) {
  249. dst->input = dst->output = dst_discard;
  250. } else {
  251. dst->dev = init_net.loopback_dev;
  252. dev_hold(dst->dev);
  253. dev_put(dev);
  254. if (dst->neighbour && dst->neighbour->dev == dev) {
  255. dst->neighbour->dev = init_net.loopback_dev;
  256. dev_put(dev);
  257. dev_hold(dst->neighbour->dev);
  258. }
  259. }
  260. }
  261. static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  262. {
  263. struct net_device *dev = ptr;
  264. struct dst_entry *dst, *last = NULL;
  265. if (dev->nd_net != &init_net)
  266. return NOTIFY_DONE;
  267. switch (event) {
  268. case NETDEV_UNREGISTER:
  269. case NETDEV_DOWN:
  270. mutex_lock(&dst_gc_mutex);
  271. for (dst = dst_busy_list; dst; dst = dst->next) {
  272. last = dst;
  273. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  274. }
  275. spin_lock_bh(&dst_garbage.lock);
  276. dst = dst_garbage.list;
  277. dst_garbage.list = NULL;
  278. spin_unlock_bh(&dst_garbage.lock);
  279. if (last)
  280. last->next = dst;
  281. else
  282. dst_busy_list = dst;
  283. for (; dst; dst = dst->next) {
  284. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  285. }
  286. mutex_unlock(&dst_gc_mutex);
  287. break;
  288. }
  289. return NOTIFY_DONE;
  290. }
  291. static struct notifier_block dst_dev_notifier = {
  292. .notifier_call = dst_dev_event,
  293. };
  294. void __init dst_init(void)
  295. {
  296. register_netdevice_notifier(&dst_dev_notifier);
  297. }
  298. EXPORT_SYMBOL(__dst_free);
  299. EXPORT_SYMBOL(dst_alloc);
  300. EXPORT_SYMBOL(dst_destroy);