dst.c 9.7 KB

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