dst.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 u32 dst_default_metrics[RTAX_MAX + 1] = {
  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. [RTAX_MAX] = 0xdeadbeef,
  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, 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->next = NULL;
  178. if (!(flags & DST_NOCOUNT))
  179. dst_entries_add(ops, 1);
  180. }
  181. EXPORT_SYMBOL(dst_init);
  182. void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
  183. int initial_ref, int initial_obsolete, unsigned short flags)
  184. {
  185. struct dst_entry *dst;
  186. if (ops->gc && dst_entries_get_fast(ops) > ops->gc_thresh) {
  187. if (ops->gc(ops))
  188. return NULL;
  189. }
  190. dst = kmem_cache_alloc(ops->kmem_cachep, GFP_ATOMIC);
  191. if (!dst)
  192. return NULL;
  193. dst_init(dst, ops, dev, initial_ref, initial_obsolete, flags);
  194. return dst;
  195. }
  196. EXPORT_SYMBOL(dst_alloc);
  197. static void ___dst_free(struct dst_entry *dst)
  198. {
  199. /* The first case (dev==NULL) is required, when
  200. protocol module is unloaded.
  201. */
  202. if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
  203. dst->input = dst_discard;
  204. dst->output = dst_discard_out;
  205. }
  206. dst->obsolete = DST_OBSOLETE_DEAD;
  207. }
  208. void __dst_free(struct dst_entry *dst)
  209. {
  210. spin_lock_bh(&dst_garbage.lock);
  211. ___dst_free(dst);
  212. dst->next = dst_garbage.list;
  213. dst_garbage.list = dst;
  214. if (dst_garbage.timer_inc > DST_GC_INC) {
  215. dst_garbage.timer_inc = DST_GC_INC;
  216. dst_garbage.timer_expires = DST_GC_MIN;
  217. mod_delayed_work(system_wq, &dst_gc_work,
  218. dst_garbage.timer_expires);
  219. }
  220. spin_unlock_bh(&dst_garbage.lock);
  221. }
  222. EXPORT_SYMBOL(__dst_free);
  223. struct dst_entry *dst_destroy(struct dst_entry * dst)
  224. {
  225. struct dst_entry *child;
  226. smp_rmb();
  227. again:
  228. child = dst->child;
  229. if (!(dst->flags & DST_NOCOUNT))
  230. dst_entries_add(dst->ops, -1);
  231. if (dst->ops->destroy)
  232. dst->ops->destroy(dst);
  233. if (dst->dev)
  234. dev_put(dst->dev);
  235. lwtstate_put(dst->lwtstate);
  236. if (dst->flags & DST_METADATA)
  237. metadata_dst_free((struct metadata_dst *)dst);
  238. else
  239. kmem_cache_free(dst->ops->kmem_cachep, dst);
  240. dst = child;
  241. if (dst) {
  242. int nohash = dst->flags & DST_NOHASH;
  243. if (atomic_dec_and_test(&dst->__refcnt)) {
  244. /* We were real parent of this dst, so kill child. */
  245. if (nohash)
  246. goto again;
  247. } else {
  248. /* Child is still referenced, return it for freeing. */
  249. if (nohash)
  250. return dst;
  251. /* Child is still in his hash table */
  252. }
  253. }
  254. return NULL;
  255. }
  256. EXPORT_SYMBOL(dst_destroy);
  257. static void dst_destroy_rcu(struct rcu_head *head)
  258. {
  259. struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
  260. dst = dst_destroy(dst);
  261. if (dst)
  262. __dst_free(dst);
  263. }
  264. void dst_release(struct dst_entry *dst)
  265. {
  266. if (dst) {
  267. int newrefcnt;
  268. unsigned short nocache = dst->flags & DST_NOCACHE;
  269. newrefcnt = atomic_dec_return(&dst->__refcnt);
  270. if (unlikely(newrefcnt < 0))
  271. net_warn_ratelimited("%s: dst:%p refcnt:%d\n",
  272. __func__, dst, newrefcnt);
  273. if (!newrefcnt && unlikely(nocache))
  274. call_rcu(&dst->rcu_head, dst_destroy_rcu);
  275. }
  276. }
  277. EXPORT_SYMBOL(dst_release);
  278. u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old)
  279. {
  280. u32 *p = kmalloc(sizeof(u32) * RTAX_MAX, GFP_ATOMIC);
  281. if (p) {
  282. u32 *old_p = __DST_METRICS_PTR(old);
  283. unsigned long prev, new;
  284. memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
  285. new = (unsigned long) p;
  286. prev = cmpxchg(&dst->_metrics, old, new);
  287. if (prev != old) {
  288. kfree(p);
  289. p = __DST_METRICS_PTR(prev);
  290. if (prev & DST_METRICS_READ_ONLY)
  291. p = NULL;
  292. }
  293. }
  294. return p;
  295. }
  296. EXPORT_SYMBOL(dst_cow_metrics_generic);
  297. /* Caller asserts that dst_metrics_read_only(dst) is false. */
  298. void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old)
  299. {
  300. unsigned long prev, new;
  301. new = ((unsigned long) dst_default_metrics) | DST_METRICS_READ_ONLY;
  302. prev = cmpxchg(&dst->_metrics, old, new);
  303. if (prev == old)
  304. kfree(__DST_METRICS_PTR(old));
  305. }
  306. EXPORT_SYMBOL(__dst_destroy_metrics_generic);
  307. static struct dst_ops md_dst_ops = {
  308. .family = AF_UNSPEC,
  309. };
  310. static int dst_md_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  311. {
  312. WARN_ONCE(1, "Attempting to call output on metadata dst\n");
  313. kfree_skb(skb);
  314. return 0;
  315. }
  316. static int dst_md_discard(struct sk_buff *skb)
  317. {
  318. WARN_ONCE(1, "Attempting to call input on metadata dst\n");
  319. kfree_skb(skb);
  320. return 0;
  321. }
  322. static void __metadata_dst_init(struct metadata_dst *md_dst, u8 optslen)
  323. {
  324. struct dst_entry *dst;
  325. dst = &md_dst->dst;
  326. dst_init(dst, &md_dst_ops, NULL, 1, DST_OBSOLETE_NONE,
  327. DST_METADATA | DST_NOCACHE | DST_NOCOUNT);
  328. dst->input = dst_md_discard;
  329. dst->output = dst_md_discard_out;
  330. memset(dst + 1, 0, sizeof(*md_dst) + optslen - sizeof(*dst));
  331. }
  332. struct metadata_dst *metadata_dst_alloc(u8 optslen, gfp_t flags)
  333. {
  334. struct metadata_dst *md_dst;
  335. md_dst = kmalloc(sizeof(*md_dst) + optslen, flags);
  336. if (!md_dst)
  337. return NULL;
  338. __metadata_dst_init(md_dst, optslen);
  339. return md_dst;
  340. }
  341. EXPORT_SYMBOL_GPL(metadata_dst_alloc);
  342. void metadata_dst_free(struct metadata_dst *md_dst)
  343. {
  344. #ifdef CONFIG_DST_CACHE
  345. dst_cache_destroy(&md_dst->u.tun_info.dst_cache);
  346. #endif
  347. kfree(md_dst);
  348. }
  349. struct metadata_dst __percpu *metadata_dst_alloc_percpu(u8 optslen, gfp_t flags)
  350. {
  351. int cpu;
  352. struct metadata_dst __percpu *md_dst;
  353. md_dst = __alloc_percpu_gfp(sizeof(struct metadata_dst) + optslen,
  354. __alignof__(struct metadata_dst), flags);
  355. if (!md_dst)
  356. return NULL;
  357. for_each_possible_cpu(cpu)
  358. __metadata_dst_init(per_cpu_ptr(md_dst, cpu), optslen);
  359. return md_dst;
  360. }
  361. EXPORT_SYMBOL_GPL(metadata_dst_alloc_percpu);
  362. /* Dirty hack. We did it in 2.2 (in __dst_free),
  363. * we have _very_ good reasons not to repeat
  364. * this mistake in 2.3, but we have no choice
  365. * now. _It_ _is_ _explicit_ _deliberate_
  366. * _race_ _condition_.
  367. *
  368. * Commented and originally written by Alexey.
  369. */
  370. static void dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  371. int unregister)
  372. {
  373. if (dst->ops->ifdown)
  374. dst->ops->ifdown(dst, dev, unregister);
  375. if (dev != dst->dev)
  376. return;
  377. if (!unregister) {
  378. dst->input = dst_discard;
  379. dst->output = dst_discard_out;
  380. } else {
  381. dst->dev = dev_net(dst->dev)->loopback_dev;
  382. dev_hold(dst->dev);
  383. dev_put(dev);
  384. }
  385. }
  386. static int dst_dev_event(struct notifier_block *this, unsigned long event,
  387. void *ptr)
  388. {
  389. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  390. struct dst_entry *dst, *last = NULL;
  391. switch (event) {
  392. case NETDEV_UNREGISTER_FINAL:
  393. case NETDEV_DOWN:
  394. mutex_lock(&dst_gc_mutex);
  395. for (dst = dst_busy_list; dst; dst = dst->next) {
  396. last = dst;
  397. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  398. }
  399. spin_lock_bh(&dst_garbage.lock);
  400. dst = dst_garbage.list;
  401. dst_garbage.list = NULL;
  402. spin_unlock_bh(&dst_garbage.lock);
  403. if (last)
  404. last->next = dst;
  405. else
  406. dst_busy_list = dst;
  407. for (; dst; dst = dst->next)
  408. dst_ifdown(dst, dev, event != NETDEV_DOWN);
  409. mutex_unlock(&dst_gc_mutex);
  410. break;
  411. }
  412. return NOTIFY_DONE;
  413. }
  414. static struct notifier_block dst_dev_notifier = {
  415. .notifier_call = dst_dev_event,
  416. .priority = -10, /* must be called after other network notifiers */
  417. };
  418. void __init dst_subsys_init(void)
  419. {
  420. register_netdevice_notifier(&dst_dev_notifier);
  421. }