dst.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/slab.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/string.h>
  18. #include <linux/types.h>
  19. #include <net/net_namespace.h>
  20. #include <linux/sched.h>
  21. #include <net/dst.h>
  22. /*
  23. * Theory of operations:
  24. * 1) We use a list, protected by a spinlock, to add
  25. * new entries from both BH and non-BH context.
  26. * 2) In order to keep spinlock held for a small delay,
  27. * we use a second list where are stored long lived
  28. * entries, that are handled by the garbage collect thread
  29. * fired by a workqueue.
  30. * 3) This list is guarded by a mutex,
  31. * so that the gc_task and dst_dev_event() can be synchronized.
  32. */
  33. #if RT_CACHE_DEBUG >= 2
  34. static atomic_t dst_total = ATOMIC_INIT(0);
  35. #endif
  36. /*
  37. * We want to keep lock & list close together
  38. * to dirty as few cache lines as possible in __dst_free().
  39. * As this is not a very strong hint, we dont force an alignment on SMP.
  40. */
  41. static struct {
  42. spinlock_t lock;
  43. struct dst_entry *list;
  44. unsigned long timer_inc;
  45. unsigned long timer_expires;
  46. } dst_garbage = {
  47. .lock = __SPIN_LOCK_UNLOCKED(dst_garbage.lock),
  48. .timer_inc = DST_GC_MAX,
  49. };
  50. static void dst_gc_task(struct work_struct *work);
  51. static void ___dst_free(struct dst_entry *dst);
  52. static DECLARE_DELAYED_WORK(dst_gc_work, dst_gc_task);
  53. static DEFINE_MUTEX(dst_gc_mutex);
  54. /*
  55. * long lived entries are maintained in this list, guarded by dst_gc_mutex
  56. */
  57. static struct dst_entry *dst_busy_list;
  58. static void dst_gc_task(struct work_struct *work)
  59. {
  60. int delayed = 0;
  61. int work_performed = 0;
  62. unsigned long expires = ~0L;
  63. struct dst_entry *dst, *next, head;
  64. struct dst_entry *last = &head;
  65. #if RT_CACHE_DEBUG >= 2
  66. ktime_t time_start = ktime_get();
  67. struct timespec elapsed;
  68. #endif
  69. mutex_lock(&dst_gc_mutex);
  70. next = dst_busy_list;
  71. loop:
  72. while ((dst = next) != NULL) {
  73. next = dst->next;
  74. prefetch(&next->next);
  75. cond_resched();
  76. if (likely(atomic_read(&dst->__refcnt))) {
  77. last->next = dst;
  78. last = dst;
  79. delayed++;
  80. continue;
  81. }
  82. work_performed++;
  83. dst = dst_destroy(dst);
  84. if (dst) {
  85. /* NOHASH and still referenced. Unless it is already
  86. * on gc list, invalidate it and add to gc list.
  87. *
  88. * Note: this is temporary. Actually, NOHASH dst's
  89. * must be obsoleted when parent is obsoleted.
  90. * But we do not have state "obsoleted, but
  91. * referenced by parent", so it is right.
  92. */
  93. if (dst->obsolete > 1)
  94. continue;
  95. ___dst_free(dst);
  96. dst->next = next;
  97. next = dst;
  98. }
  99. }
  100. spin_lock_bh(&dst_garbage.lock);
  101. next = dst_garbage.list;
  102. if (next) {
  103. dst_garbage.list = NULL;
  104. spin_unlock_bh(&dst_garbage.lock);
  105. goto loop;
  106. }
  107. last->next = NULL;
  108. dst_busy_list = head.next;
  109. if (!dst_busy_list)
  110. dst_garbage.timer_inc = DST_GC_MAX;
  111. else {
  112. /*
  113. * if we freed less than 1/10 of delayed entries,
  114. * we can sleep longer.
  115. */
  116. if (work_performed <= delayed/10) {
  117. dst_garbage.timer_expires += dst_garbage.timer_inc;
  118. if (dst_garbage.timer_expires > DST_GC_MAX)
  119. dst_garbage.timer_expires = DST_GC_MAX;
  120. dst_garbage.timer_inc += DST_GC_INC;
  121. } else {
  122. dst_garbage.timer_inc = DST_GC_INC;
  123. dst_garbage.timer_expires = DST_GC_MIN;
  124. }
  125. expires = dst_garbage.timer_expires;
  126. /*
  127. * if the next desired timer is more than 4 seconds in the
  128. * future then round the timer to whole seconds
  129. */
  130. if (expires > 4*HZ)
  131. expires = round_jiffies_relative(expires);
  132. schedule_delayed_work(&dst_gc_work, expires);
  133. }
  134. spin_unlock_bh(&dst_garbage.lock);
  135. mutex_unlock(&dst_gc_mutex);
  136. #if RT_CACHE_DEBUG >= 2
  137. elapsed = ktime_to_timespec(ktime_sub(ktime_get(), time_start));
  138. printk(KERN_DEBUG "dst_total: %d delayed: %d work_perf: %d"
  139. " expires: %lu elapsed: %lu us\n",
  140. atomic_read(&dst_total), delayed, work_performed,
  141. expires,
  142. elapsed.tv_sec * USEC_PER_SEC +
  143. elapsed.tv_nsec / NSEC_PER_USEC);
  144. #endif
  145. }
  146. int dst_discard(struct sk_buff *skb)
  147. {
  148. kfree_skb(skb);
  149. return 0;
  150. }
  151. EXPORT_SYMBOL(dst_discard);
  152. const u32 dst_default_metrics[RTAX_MAX];
  153. void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
  154. int initial_ref, int initial_obsolete, int flags)
  155. {
  156. struct dst_entry *dst;
  157. if (ops->gc && dst_entries_get_fast(ops) > ops->gc_thresh) {
  158. if (ops->gc(ops))
  159. return NULL;
  160. }
  161. dst = kmem_cache_alloc(ops->kmem_cachep, GFP_ATOMIC);
  162. if (!dst)
  163. return NULL;
  164. dst->child = NULL;
  165. dst->dev = dev;
  166. if (dev)
  167. dev_hold(dev);
  168. dst->ops = ops;
  169. dst_init_metrics(dst, dst_default_metrics, true);
  170. dst->expires = 0UL;
  171. dst->path = dst;
  172. dst->neighbour = NULL;
  173. dst->hh = NULL;
  174. #ifdef CONFIG_XFRM
  175. dst->xfrm = NULL;
  176. #endif
  177. dst->input = dst_discard;
  178. dst->output = dst_discard;
  179. dst->error = 0;
  180. dst->obsolete = initial_obsolete;
  181. dst->header_len = 0;
  182. dst->trailer_len = 0;
  183. #ifdef CONFIG_IP_ROUTE_CLASSID
  184. dst->tclassid = 0;
  185. #endif
  186. atomic_set(&dst->__refcnt, initial_ref);
  187. dst->__use = 0;
  188. dst->lastuse = jiffies;
  189. dst->flags = flags;
  190. dst->next = NULL;
  191. #if RT_CACHE_DEBUG >= 2
  192. atomic_inc(&dst_total);
  193. #endif
  194. dst_entries_add(ops, 1);
  195. return dst;
  196. }
  197. EXPORT_SYMBOL(dst_alloc);
  198. static void ___dst_free(struct dst_entry *dst)
  199. {
  200. /* The first case (dev==NULL) is required, when
  201. protocol module is unloaded.
  202. */
  203. if (dst->dev == NULL || !(dst->dev->flags&IFF_UP))
  204. dst->input = dst->output = dst_discard;
  205. dst->obsolete = 2;
  206. }
  207. void __dst_free(struct dst_entry *dst)
  208. {
  209. spin_lock_bh(&dst_garbage.lock);
  210. ___dst_free(dst);
  211. dst->next = dst_garbage.list;
  212. dst_garbage.list = dst;
  213. if (dst_garbage.timer_inc > DST_GC_INC) {
  214. dst_garbage.timer_inc = DST_GC_INC;
  215. dst_garbage.timer_expires = DST_GC_MIN;
  216. cancel_delayed_work(&dst_gc_work);
  217. schedule_delayed_work(&dst_gc_work, dst_garbage.timer_expires);
  218. }
  219. spin_unlock_bh(&dst_garbage.lock);
  220. }
  221. EXPORT_SYMBOL(__dst_free);
  222. struct dst_entry *dst_destroy(struct dst_entry * dst)
  223. {
  224. struct dst_entry *child;
  225. struct neighbour *neigh;
  226. struct hh_cache *hh;
  227. smp_rmb();
  228. again:
  229. neigh = dst->neighbour;
  230. hh = dst->hh;
  231. child = dst->child;
  232. dst->hh = NULL;
  233. if (hh)
  234. hh_cache_put(hh);
  235. if (neigh) {
  236. dst->neighbour = NULL;
  237. neigh_release(neigh);
  238. }
  239. dst_entries_add(dst->ops, -1);
  240. if (dst->ops->destroy)
  241. dst->ops->destroy(dst);
  242. if (dst->dev)
  243. dev_put(dst->dev);
  244. #if RT_CACHE_DEBUG >= 2
  245. atomic_dec(&dst_total);
  246. #endif
  247. kmem_cache_free(dst->ops->kmem_cachep, dst);
  248. dst = child;
  249. if (dst) {
  250. int nohash = dst->flags & DST_NOHASH;
  251. if (atomic_dec_and_test(&dst->__refcnt)) {
  252. /* We were real parent of this dst, so kill child. */
  253. if (nohash)
  254. goto again;
  255. } else {
  256. /* Child is still referenced, return it for freeing. */
  257. if (nohash)
  258. return dst;
  259. /* Child is still in his hash table */
  260. }
  261. }
  262. return NULL;
  263. }
  264. EXPORT_SYMBOL(dst_destroy);
  265. void dst_release(struct dst_entry *dst)
  266. {
  267. if (dst) {
  268. int newrefcnt;
  269. newrefcnt = atomic_dec_return(&dst->__refcnt);
  270. WARN_ON(newrefcnt < 0);
  271. if (unlikely(dst->flags & DST_NOCACHE) && !newrefcnt) {
  272. dst = dst_destroy(dst);
  273. if (dst)
  274. __dst_free(dst);
  275. }
  276. }
  277. }
  278. EXPORT_SYMBOL(dst_release);
  279. u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old)
  280. {
  281. u32 *p = kmalloc(sizeof(u32) * RTAX_MAX, GFP_ATOMIC);
  282. if (p) {
  283. u32 *old_p = __DST_METRICS_PTR(old);
  284. unsigned long prev, new;
  285. memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
  286. new = (unsigned long) p;
  287. prev = cmpxchg(&dst->_metrics, old, new);
  288. if (prev != old) {
  289. kfree(p);
  290. p = __DST_METRICS_PTR(prev);
  291. if (prev & DST_METRICS_READ_ONLY)
  292. p = NULL;
  293. }
  294. }
  295. return p;
  296. }
  297. EXPORT_SYMBOL(dst_cow_metrics_generic);
  298. /* Caller asserts that dst_metrics_read_only(dst) is false. */
  299. void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old)
  300. {
  301. unsigned long prev, new;
  302. new = (unsigned long) dst_default_metrics;
  303. prev = cmpxchg(&dst->_metrics, old, new);
  304. if (prev == old)
  305. kfree(__DST_METRICS_PTR(old));
  306. }
  307. EXPORT_SYMBOL(__dst_destroy_metrics_generic);
  308. /**
  309. * skb_dst_set_noref - sets skb dst, without a reference
  310. * @skb: buffer
  311. * @dst: dst entry
  312. *
  313. * Sets skb dst, assuming a reference was not taken on dst
  314. * skb_dst_drop() should not dst_release() this dst
  315. */
  316. void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst)
  317. {
  318. WARN_ON(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
  319. /* If dst not in cache, we must take a reference, because
  320. * dst_release() will destroy dst as soon as its refcount becomes zero
  321. */
  322. if (unlikely(dst->flags & DST_NOCACHE)) {
  323. dst_hold(dst);
  324. skb_dst_set(skb, dst);
  325. } else {
  326. skb->_skb_refdst = (unsigned long)dst | SKB_DST_NOREF;
  327. }
  328. }
  329. EXPORT_SYMBOL(skb_dst_set_noref);
  330. /* Dirty hack. We did it in 2.2 (in __dst_free),
  331. * we have _very_ good reasons not to repeat
  332. * this mistake in 2.3, but we have no choice
  333. * now. _It_ _is_ _explicit_ _deliberate_
  334. * _race_ _condition_.
  335. *
  336. * Commented and originally written by Alexey.
  337. */
  338. static void dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  339. int unregister)
  340. {
  341. if (dst->ops->ifdown)
  342. dst->ops->ifdown(dst, dev, unregister);
  343. if (dev != dst->dev)
  344. return;
  345. if (!unregister) {
  346. dst->input = dst->output = dst_discard;
  347. } else {
  348. dst->dev = dev_net(dst->dev)->loopback_dev;
  349. dev_hold(dst->dev);
  350. dev_put(dev);
  351. if (dst->neighbour && dst->neighbour->dev == dev) {
  352. dst->neighbour->dev = dst->dev;
  353. dev_hold(dst->dev);
  354. dev_put(dev);
  355. }
  356. }
  357. }
  358. static int dst_dev_event(struct notifier_block *this, unsigned long event,
  359. void *ptr)
  360. {
  361. struct net_device *dev = ptr;
  362. struct dst_entry *dst, *last = NULL;
  363. switch (event) {
  364. case NETDEV_UNREGISTER:
  365. case NETDEV_DOWN:
  366. mutex_lock(&dst_gc_mutex);
  367. for (dst = dst_busy_list; dst; dst = dst->next) {
  368. last = dst;
  369. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  370. }
  371. spin_lock_bh(&dst_garbage.lock);
  372. dst = dst_garbage.list;
  373. dst_garbage.list = NULL;
  374. spin_unlock_bh(&dst_garbage.lock);
  375. if (last)
  376. last->next = dst;
  377. else
  378. dst_busy_list = dst;
  379. for (; dst; dst = dst->next)
  380. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  381. mutex_unlock(&dst_gc_mutex);
  382. break;
  383. }
  384. return NOTIFY_DONE;
  385. }
  386. static struct notifier_block dst_dev_notifier = {
  387. .notifier_call = dst_dev_event,
  388. .priority = -10, /* must be called after other network notifiers */
  389. };
  390. void __init dst_init(void)
  391. {
  392. register_netdevice_notifier(&dst_dev_notifier);
  393. }