dst.c 8.1 KB

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