dst.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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/lwtunnel.h>
  23. #include <net/dst.h>
  24. #include <net/dst_metadata.h>
  25. /*
  26. * Theory of operations:
  27. * 1) We use a list, protected by a spinlock, to add
  28. * new entries from both BH and non-BH context.
  29. * 2) In order to keep spinlock held for a small delay,
  30. * we use a second list where are stored long lived
  31. * entries, that are handled by the garbage collect thread
  32. * fired by a workqueue.
  33. * 3) This list is guarded by a mutex,
  34. * so that the gc_task and dst_dev_event() can be synchronized.
  35. */
  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. mutex_lock(&dst_gc_mutex);
  66. next = dst_busy_list;
  67. loop:
  68. while ((dst = next) != NULL) {
  69. next = dst->next;
  70. prefetch(&next->next);
  71. cond_resched();
  72. if (likely(atomic_read(&dst->__refcnt))) {
  73. last->next = dst;
  74. last = dst;
  75. delayed++;
  76. continue;
  77. }
  78. work_performed++;
  79. dst = dst_destroy(dst);
  80. if (dst) {
  81. /* NOHASH and still referenced. Unless it is already
  82. * on gc list, invalidate it and add to gc list.
  83. *
  84. * Note: this is temporary. Actually, NOHASH dst's
  85. * must be obsoleted when parent is obsoleted.
  86. * But we do not have state "obsoleted, but
  87. * referenced by parent", so it is right.
  88. */
  89. if (dst->obsolete > 0)
  90. continue;
  91. ___dst_free(dst);
  92. dst->next = next;
  93. next = dst;
  94. }
  95. }
  96. spin_lock_bh(&dst_garbage.lock);
  97. next = dst_garbage.list;
  98. if (next) {
  99. dst_garbage.list = NULL;
  100. spin_unlock_bh(&dst_garbage.lock);
  101. goto loop;
  102. }
  103. last->next = NULL;
  104. dst_busy_list = head.next;
  105. if (!dst_busy_list)
  106. dst_garbage.timer_inc = DST_GC_MAX;
  107. else {
  108. /*
  109. * if we freed less than 1/10 of delayed entries,
  110. * we can sleep longer.
  111. */
  112. if (work_performed <= delayed/10) {
  113. dst_garbage.timer_expires += dst_garbage.timer_inc;
  114. if (dst_garbage.timer_expires > DST_GC_MAX)
  115. dst_garbage.timer_expires = DST_GC_MAX;
  116. dst_garbage.timer_inc += DST_GC_INC;
  117. } else {
  118. dst_garbage.timer_inc = DST_GC_INC;
  119. dst_garbage.timer_expires = DST_GC_MIN;
  120. }
  121. expires = dst_garbage.timer_expires;
  122. /*
  123. * if the next desired timer is more than 4 seconds in the
  124. * future then round the timer to whole seconds
  125. */
  126. if (expires > 4*HZ)
  127. expires = round_jiffies_relative(expires);
  128. schedule_delayed_work(&dst_gc_work, expires);
  129. }
  130. spin_unlock_bh(&dst_garbage.lock);
  131. mutex_unlock(&dst_gc_mutex);
  132. }
  133. int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  134. {
  135. kfree_skb(skb);
  136. return 0;
  137. }
  138. EXPORT_SYMBOL(dst_discard_out);
  139. const struct dst_metrics dst_default_metrics = {
  140. /* This initializer is needed to force linker to place this variable
  141. * into const section. Otherwise it might end into bss section.
  142. * We really want to avoid false sharing on this variable, and catch
  143. * any writes on it.
  144. */
  145. .refcnt = ATOMIC_INIT(1),
  146. };
  147. void dst_init(struct dst_entry *dst, struct dst_ops *ops,
  148. struct net_device *dev, int initial_ref, int initial_obsolete,
  149. unsigned short flags)
  150. {
  151. dst->child = NULL;
  152. dst->dev = dev;
  153. if (dev)
  154. dev_hold(dev);
  155. dst->ops = ops;
  156. dst_init_metrics(dst, dst_default_metrics.metrics, true);
  157. dst->expires = 0UL;
  158. dst->path = dst;
  159. dst->from = NULL;
  160. #ifdef CONFIG_XFRM
  161. dst->xfrm = NULL;
  162. #endif
  163. dst->input = dst_discard;
  164. dst->output = dst_discard_out;
  165. dst->error = 0;
  166. dst->obsolete = initial_obsolete;
  167. dst->header_len = 0;
  168. dst->trailer_len = 0;
  169. #ifdef CONFIG_IP_ROUTE_CLASSID
  170. dst->tclassid = 0;
  171. #endif
  172. dst->lwtstate = NULL;
  173. atomic_set(&dst->__refcnt, initial_ref);
  174. dst->__use = 0;
  175. dst->lastuse = jiffies;
  176. dst->flags = flags;
  177. dst->pending_confirm = 0;
  178. dst->next = NULL;
  179. if (!(flags & DST_NOCOUNT))
  180. dst_entries_add(ops, 1);
  181. }
  182. EXPORT_SYMBOL(dst_init);
  183. void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
  184. int initial_ref, int initial_obsolete, unsigned short flags)
  185. {
  186. struct dst_entry *dst;
  187. if (ops->gc && dst_entries_get_fast(ops) > ops->gc_thresh) {
  188. if (ops->gc(ops))
  189. return NULL;
  190. }
  191. dst = kmem_cache_alloc(ops->kmem_cachep, GFP_ATOMIC);
  192. if (!dst)
  193. return NULL;
  194. dst_init(dst, ops, dev, initial_ref, initial_obsolete, flags);
  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_discard;
  205. dst->output = dst_discard_out;
  206. }
  207. dst->obsolete = DST_OBSOLETE_DEAD;
  208. }
  209. void __dst_free(struct dst_entry *dst)
  210. {
  211. spin_lock_bh(&dst_garbage.lock);
  212. ___dst_free(dst);
  213. dst->next = dst_garbage.list;
  214. dst_garbage.list = dst;
  215. if (dst_garbage.timer_inc > DST_GC_INC) {
  216. dst_garbage.timer_inc = DST_GC_INC;
  217. dst_garbage.timer_expires = DST_GC_MIN;
  218. mod_delayed_work(system_wq, &dst_gc_work,
  219. dst_garbage.timer_expires);
  220. }
  221. spin_unlock_bh(&dst_garbage.lock);
  222. }
  223. EXPORT_SYMBOL(__dst_free);
  224. struct dst_entry *dst_destroy(struct dst_entry * dst)
  225. {
  226. struct dst_entry *child;
  227. smp_rmb();
  228. again:
  229. child = dst->child;
  230. if (!(dst->flags & DST_NOCOUNT))
  231. dst_entries_add(dst->ops, -1);
  232. if (dst->ops->destroy)
  233. dst->ops->destroy(dst);
  234. if (dst->dev)
  235. dev_put(dst->dev);
  236. lwtstate_put(dst->lwtstate);
  237. if (dst->flags & DST_METADATA)
  238. kfree(dst);
  239. else
  240. kmem_cache_free(dst->ops->kmem_cachep, dst);
  241. dst = child;
  242. if (dst) {
  243. int nohash = dst->flags & DST_NOHASH;
  244. if (atomic_dec_and_test(&dst->__refcnt)) {
  245. /* We were real parent of this dst, so kill child. */
  246. if (nohash)
  247. goto again;
  248. } else {
  249. /* Child is still referenced, return it for freeing. */
  250. if (nohash)
  251. return dst;
  252. /* Child is still in his hash table */
  253. }
  254. }
  255. return NULL;
  256. }
  257. EXPORT_SYMBOL(dst_destroy);
  258. static void dst_destroy_rcu(struct rcu_head *head)
  259. {
  260. struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
  261. dst = dst_destroy(dst);
  262. if (dst)
  263. __dst_free(dst);
  264. }
  265. void dst_release(struct dst_entry *dst)
  266. {
  267. if (dst) {
  268. int newrefcnt;
  269. unsigned short nocache = dst->flags & DST_NOCACHE;
  270. newrefcnt = atomic_dec_return(&dst->__refcnt);
  271. if (unlikely(newrefcnt < 0))
  272. net_warn_ratelimited("%s: dst:%p refcnt:%d\n",
  273. __func__, dst, newrefcnt);
  274. if (!newrefcnt && unlikely(nocache))
  275. call_rcu(&dst->rcu_head, dst_destroy_rcu);
  276. }
  277. }
  278. EXPORT_SYMBOL(dst_release);
  279. u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old)
  280. {
  281. struct dst_metrics *p = kmalloc(sizeof(*p), GFP_ATOMIC);
  282. if (p) {
  283. struct dst_metrics *old_p = (struct dst_metrics *)__DST_METRICS_PTR(old);
  284. unsigned long prev, new;
  285. atomic_set(&p->refcnt, 1);
  286. memcpy(p->metrics, old_p->metrics, sizeof(p->metrics));
  287. new = (unsigned long) p;
  288. prev = cmpxchg(&dst->_metrics, old, new);
  289. if (prev != old) {
  290. kfree(p);
  291. p = (struct dst_metrics *)__DST_METRICS_PTR(prev);
  292. if (prev & DST_METRICS_READ_ONLY)
  293. p = NULL;
  294. } else if (prev & DST_METRICS_REFCOUNTED) {
  295. if (atomic_dec_and_test(&old_p->refcnt))
  296. kfree(old_p);
  297. }
  298. }
  299. BUILD_BUG_ON(offsetof(struct dst_metrics, metrics) != 0);
  300. return (u32 *)p;
  301. }
  302. EXPORT_SYMBOL(dst_cow_metrics_generic);
  303. /* Caller asserts that dst_metrics_read_only(dst) is false. */
  304. void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old)
  305. {
  306. unsigned long prev, new;
  307. new = ((unsigned long) &dst_default_metrics) | DST_METRICS_READ_ONLY;
  308. prev = cmpxchg(&dst->_metrics, old, new);
  309. if (prev == old)
  310. kfree(__DST_METRICS_PTR(old));
  311. }
  312. EXPORT_SYMBOL(__dst_destroy_metrics_generic);
  313. static struct dst_ops md_dst_ops = {
  314. .family = AF_UNSPEC,
  315. };
  316. static int dst_md_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  317. {
  318. WARN_ONCE(1, "Attempting to call output on metadata dst\n");
  319. kfree_skb(skb);
  320. return 0;
  321. }
  322. static int dst_md_discard(struct sk_buff *skb)
  323. {
  324. WARN_ONCE(1, "Attempting to call input on metadata dst\n");
  325. kfree_skb(skb);
  326. return 0;
  327. }
  328. static void __metadata_dst_init(struct metadata_dst *md_dst, u8 optslen)
  329. {
  330. struct dst_entry *dst;
  331. dst = &md_dst->dst;
  332. dst_init(dst, &md_dst_ops, NULL, 1, DST_OBSOLETE_NONE,
  333. DST_METADATA | DST_NOCACHE | DST_NOCOUNT);
  334. dst->input = dst_md_discard;
  335. dst->output = dst_md_discard_out;
  336. memset(dst + 1, 0, sizeof(*md_dst) + optslen - sizeof(*dst));
  337. }
  338. struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags)
  339. {
  340. struct metadata_dst *md_dst;
  341. md_dst = kmalloc(sizeof(*md_dst) + optslen, flags);
  342. if (!md_dst)
  343. return NULL;
  344. __metadata_dst_init(md_dst, optslen);
  345. return md_dst;
  346. }
  347. EXPORT_SYMBOL_GPL(metadata_dst_alloc);
  348. struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags)
  349. {
  350. int cpu;
  351. struct metadata_dst __percpu *md_dst;
  352. md_dst = __alloc_percpu_gfp(sizeof(struct metadata_dst) + optslen,
  353. __alignof__(struct metadata_dst), flags);
  354. if (!md_dst)
  355. return NULL;
  356. for_each_possible_cpu(cpu)
  357. __metadata_dst_init(per_cpu_ptr(md_dst, cpu), optslen);
  358. return md_dst;
  359. }
  360. EXPORT_SYMBOL_GPL(metadata_dst_alloc_percpu);
  361. /* Dirty hack. We did it in 2.2 (in __dst_free),
  362. * we have _very_ good reasons not to repeat
  363. * this mistake in 2.3, but we have no choice
  364. * now. _It_ _is_ _explicit_ _deliberate_
  365. * _race_ _condition_.
  366. *
  367. * Commented and originally written by Alexey.
  368. */
  369. static void dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  370. int unregister)
  371. {
  372. if (dst->ops->ifdown)
  373. dst->ops->ifdown(dst, dev, unregister);
  374. if (dev != dst->dev)
  375. return;
  376. if (!unregister) {
  377. dst->input = dst_discard;
  378. dst->output = dst_discard_out;
  379. } else {
  380. dst->dev = dev_net(dst->dev)->loopback_dev;
  381. dev_hold(dst->dev);
  382. dev_put(dev);
  383. }
  384. }
  385. static int dst_dev_event(struct notifier_block *this, unsigned long event,
  386. void *ptr)
  387. {
  388. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  389. struct dst_entry *dst, *last = NULL;
  390. switch (event) {
  391. case NETDEV_UNREGISTER_FINAL:
  392. case NETDEV_DOWN:
  393. mutex_lock(&dst_gc_mutex);
  394. for (dst = dst_busy_list; dst; dst = dst->next) {
  395. last = dst;
  396. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  397. }
  398. spin_lock_bh(&dst_garbage.lock);
  399. dst = dst_garbage.list;
  400. dst_garbage.list = NULL;
  401. /* The code in dst_ifdown places a hold on the loopback device.
  402. * If the gc entry processing is set to expire after a lengthy
  403. * interval, this hold can cause netdev_wait_allrefs() to hang
  404. * out and wait for a long time -- until the the loopback
  405. * interface is released. If we're really unlucky, it'll emit
  406. * pr_emerg messages to console too. Reset the interval here,
  407. * so dst cleanups occur in a more timely fashion.
  408. */
  409. if (dst_garbage.timer_inc > DST_GC_INC) {
  410. dst_garbage.timer_inc = DST_GC_INC;
  411. dst_garbage.timer_expires = DST_GC_MIN;
  412. mod_delayed_work(system_wq, &dst_gc_work,
  413. dst_garbage.timer_expires);
  414. }
  415. spin_unlock_bh(&dst_garbage.lock);
  416. if (last)
  417. last->next = dst;
  418. else
  419. dst_busy_list = dst;
  420. for (; dst; dst = dst->next)
  421. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  422. mutex_unlock(&dst_gc_mutex);
  423. break;
  424. }
  425. return NOTIFY_DONE;
  426. }
  427. static struct notifier_block dst_dev_notifier = {
  428. .notifier_call = dst_dev_event,
  429. .priority = -10, /* must be called after other network notifiers */
  430. };
  431. void __init dst_subsys_init(void)
  432. {
  433. register_netdevice_notifier(&dst_dev_notifier);
  434. }