dst.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. int dst_discard(struct sk_buff *skb)
  143. {
  144. kfree_skb(skb);
  145. return 0;
  146. }
  147. EXPORT_SYMBOL(dst_discard);
  148. void * dst_alloc(struct dst_ops * ops)
  149. {
  150. struct dst_entry * dst;
  151. if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
  152. if (ops->gc(ops))
  153. return NULL;
  154. }
  155. dst = kmem_cache_zalloc(ops->kmem_cachep, GFP_ATOMIC);
  156. if (!dst)
  157. return NULL;
  158. atomic_set(&dst->__refcnt, 0);
  159. dst->ops = ops;
  160. dst->lastuse = jiffies;
  161. dst->path = dst;
  162. dst->input = dst->output = dst_discard;
  163. #if RT_CACHE_DEBUG >= 2
  164. atomic_inc(&dst_total);
  165. #endif
  166. atomic_inc(&ops->entries);
  167. return dst;
  168. }
  169. static void ___dst_free(struct dst_entry * dst)
  170. {
  171. /* The first case (dev==NULL) is required, when
  172. protocol module is unloaded.
  173. */
  174. if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
  175. dst->input = dst->output = dst_discard;
  176. }
  177. dst->obsolete = 2;
  178. }
  179. void __dst_free(struct dst_entry * dst)
  180. {
  181. spin_lock_bh(&dst_garbage.lock);
  182. ___dst_free(dst);
  183. dst->next = dst_garbage.list;
  184. dst_garbage.list = dst;
  185. if (dst_garbage.timer_inc > DST_GC_INC) {
  186. dst_garbage.timer_inc = DST_GC_INC;
  187. dst_garbage.timer_expires = DST_GC_MIN;
  188. cancel_delayed_work(&dst_gc_work);
  189. schedule_delayed_work(&dst_gc_work, dst_garbage.timer_expires);
  190. }
  191. spin_unlock_bh(&dst_garbage.lock);
  192. }
  193. struct dst_entry *dst_destroy(struct dst_entry * dst)
  194. {
  195. struct dst_entry *child;
  196. struct neighbour *neigh;
  197. struct hh_cache *hh;
  198. smp_rmb();
  199. again:
  200. neigh = dst->neighbour;
  201. hh = dst->hh;
  202. child = dst->child;
  203. dst->hh = NULL;
  204. if (hh && atomic_dec_and_test(&hh->hh_refcnt))
  205. kfree(hh);
  206. if (neigh) {
  207. dst->neighbour = NULL;
  208. neigh_release(neigh);
  209. }
  210. atomic_dec(&dst->ops->entries);
  211. if (dst->ops->destroy)
  212. dst->ops->destroy(dst);
  213. if (dst->dev)
  214. dev_put(dst->dev);
  215. #if RT_CACHE_DEBUG >= 2
  216. atomic_dec(&dst_total);
  217. #endif
  218. kmem_cache_free(dst->ops->kmem_cachep, dst);
  219. dst = child;
  220. if (dst) {
  221. int nohash = dst->flags & DST_NOHASH;
  222. if (atomic_dec_and_test(&dst->__refcnt)) {
  223. /* We were real parent of this dst, so kill child. */
  224. if (nohash)
  225. goto again;
  226. } else {
  227. /* Child is still referenced, return it for freeing. */
  228. if (nohash)
  229. return dst;
  230. /* Child is still in his hash table */
  231. }
  232. }
  233. return NULL;
  234. }
  235. void dst_release(struct dst_entry *dst)
  236. {
  237. if (dst) {
  238. int newrefcnt;
  239. smp_mb__before_atomic_dec();
  240. newrefcnt = atomic_dec_return(&dst->__refcnt);
  241. WARN_ON(newrefcnt < 0);
  242. }
  243. }
  244. EXPORT_SYMBOL(dst_release);
  245. /* Dirty hack. We did it in 2.2 (in __dst_free),
  246. * we have _very_ good reasons not to repeat
  247. * this mistake in 2.3, but we have no choice
  248. * now. _It_ _is_ _explicit_ _deliberate_
  249. * _race_ _condition_.
  250. *
  251. * Commented and originally written by Alexey.
  252. */
  253. static inline void dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  254. int unregister)
  255. {
  256. if (dst->ops->ifdown)
  257. dst->ops->ifdown(dst, dev, unregister);
  258. if (dev != dst->dev)
  259. return;
  260. if (!unregister) {
  261. dst->input = dst->output = dst_discard;
  262. } else {
  263. dst->dev = dev_net(dst->dev)->loopback_dev;
  264. dev_hold(dst->dev);
  265. dev_put(dev);
  266. if (dst->neighbour && dst->neighbour->dev == dev) {
  267. dst->neighbour->dev = dst->dev;
  268. dev_hold(dst->dev);
  269. dev_put(dev);
  270. }
  271. }
  272. }
  273. static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  274. {
  275. struct net_device *dev = ptr;
  276. struct dst_entry *dst, *last = NULL;
  277. switch (event) {
  278. case NETDEV_UNREGISTER:
  279. case NETDEV_DOWN:
  280. mutex_lock(&dst_gc_mutex);
  281. for (dst = dst_busy_list; dst; dst = dst->next) {
  282. last = dst;
  283. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  284. }
  285. spin_lock_bh(&dst_garbage.lock);
  286. dst = dst_garbage.list;
  287. dst_garbage.list = NULL;
  288. spin_unlock_bh(&dst_garbage.lock);
  289. if (last)
  290. last->next = dst;
  291. else
  292. dst_busy_list = dst;
  293. for (; dst; dst = dst->next) {
  294. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  295. }
  296. mutex_unlock(&dst_gc_mutex);
  297. break;
  298. }
  299. return NOTIFY_DONE;
  300. }
  301. static struct notifier_block dst_dev_notifier = {
  302. .notifier_call = dst_dev_event,
  303. };
  304. void __init dst_init(void)
  305. {
  306. register_netdevice_notifier(&dst_dev_notifier);
  307. }
  308. EXPORT_SYMBOL(__dst_free);
  309. EXPORT_SYMBOL(dst_alloc);
  310. EXPORT_SYMBOL(dst_destroy);