dst.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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/xfrm.h>
  24. #include <net/dst.h>
  25. #include <net/dst_metadata.h>
  26. /*
  27. * Theory of operations:
  28. * 1) We use a list, protected by a spinlock, to add
  29. * new entries from both BH and non-BH context.
  30. * 2) In order to keep spinlock held for a small delay,
  31. * we use a second list where are stored long lived
  32. * entries, that are handled by the garbage collect thread
  33. * fired by a workqueue.
  34. * 3) This list is guarded by a mutex,
  35. * so that the gc_task and dst_dev_event() can be synchronized.
  36. */
  37. /*
  38. * We want to keep lock & list close together
  39. * to dirty as few cache lines as possible in __dst_free().
  40. * As this is not a very strong hint, we dont force an alignment on SMP.
  41. */
  42. int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  43. {
  44. kfree_skb(skb);
  45. return 0;
  46. }
  47. EXPORT_SYMBOL(dst_discard_out);
  48. const struct dst_metrics dst_default_metrics = {
  49. /* This initializer is needed to force linker to place this variable
  50. * into const section. Otherwise it might end into bss section.
  51. * We really want to avoid false sharing on this variable, and catch
  52. * any writes on it.
  53. */
  54. .refcnt = REFCOUNT_INIT(1),
  55. };
  56. void dst_init(struct dst_entry *dst, struct dst_ops *ops,
  57. struct net_device *dev, int initial_ref, int initial_obsolete,
  58. unsigned short flags)
  59. {
  60. dst->dev = dev;
  61. if (dev)
  62. dev_hold(dev);
  63. dst->ops = ops;
  64. dst_init_metrics(dst, dst_default_metrics.metrics, true);
  65. dst->expires = 0UL;
  66. #ifdef CONFIG_XFRM
  67. dst->xfrm = NULL;
  68. #endif
  69. dst->input = dst_discard;
  70. dst->output = dst_discard_out;
  71. dst->error = 0;
  72. dst->obsolete = initial_obsolete;
  73. dst->header_len = 0;
  74. dst->trailer_len = 0;
  75. #ifdef CONFIG_IP_ROUTE_CLASSID
  76. dst->tclassid = 0;
  77. #endif
  78. dst->lwtstate = NULL;
  79. atomic_set(&dst->__refcnt, initial_ref);
  80. dst->__use = 0;
  81. dst->lastuse = jiffies;
  82. dst->flags = flags;
  83. if (!(flags & DST_NOCOUNT))
  84. dst_entries_add(ops, 1);
  85. }
  86. EXPORT_SYMBOL(dst_init);
  87. void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
  88. int initial_ref, int initial_obsolete, unsigned short flags)
  89. {
  90. struct dst_entry *dst;
  91. if (ops->gc && dst_entries_get_fast(ops) > ops->gc_thresh) {
  92. if (ops->gc(ops))
  93. return NULL;
  94. }
  95. dst = kmem_cache_alloc(ops->kmem_cachep, GFP_ATOMIC);
  96. if (!dst)
  97. return NULL;
  98. dst_init(dst, ops, dev, initial_ref, initial_obsolete, flags);
  99. return dst;
  100. }
  101. EXPORT_SYMBOL(dst_alloc);
  102. struct dst_entry *dst_destroy(struct dst_entry * dst)
  103. {
  104. struct dst_entry *child = NULL;
  105. smp_rmb();
  106. #ifdef CONFIG_XFRM
  107. if (dst->xfrm) {
  108. struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
  109. child = xdst->child;
  110. }
  111. #endif
  112. if (!(dst->flags & DST_NOCOUNT))
  113. dst_entries_add(dst->ops, -1);
  114. if (dst->ops->destroy)
  115. dst->ops->destroy(dst);
  116. if (dst->dev)
  117. dev_put(dst->dev);
  118. lwtstate_put(dst->lwtstate);
  119. if (dst->flags & DST_METADATA)
  120. metadata_dst_free((struct metadata_dst *)dst);
  121. else
  122. kmem_cache_free(dst->ops->kmem_cachep, dst);
  123. dst = child;
  124. if (dst)
  125. dst_release_immediate(dst);
  126. return NULL;
  127. }
  128. EXPORT_SYMBOL(dst_destroy);
  129. static void dst_destroy_rcu(struct rcu_head *head)
  130. {
  131. struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
  132. dst = dst_destroy(dst);
  133. }
  134. /* Operations to mark dst as DEAD and clean up the net device referenced
  135. * by dst:
  136. * 1. put the dst under loopback interface and discard all tx/rx packets
  137. * on this route.
  138. * 2. release the net_device
  139. * This function should be called when removing routes from the fib tree
  140. * in preparation for a NETDEV_DOWN/NETDEV_UNREGISTER event and also to
  141. * make the next dst_ops->check() fail.
  142. */
  143. void dst_dev_put(struct dst_entry *dst)
  144. {
  145. struct net_device *dev = dst->dev;
  146. dst->obsolete = DST_OBSOLETE_DEAD;
  147. if (dst->ops->ifdown)
  148. dst->ops->ifdown(dst, dev, true);
  149. dst->input = dst_discard;
  150. dst->output = dst_discard_out;
  151. dst->dev = dev_net(dst->dev)->loopback_dev;
  152. dev_hold(dst->dev);
  153. dev_put(dev);
  154. }
  155. EXPORT_SYMBOL(dst_dev_put);
  156. void dst_release(struct dst_entry *dst)
  157. {
  158. if (dst) {
  159. int newrefcnt;
  160. newrefcnt = atomic_dec_return(&dst->__refcnt);
  161. if (unlikely(newrefcnt < 0))
  162. net_warn_ratelimited("%s: dst:%p refcnt:%d\n",
  163. __func__, dst, newrefcnt);
  164. if (!newrefcnt)
  165. call_rcu(&dst->rcu_head, dst_destroy_rcu);
  166. }
  167. }
  168. EXPORT_SYMBOL(dst_release);
  169. void dst_release_immediate(struct dst_entry *dst)
  170. {
  171. if (dst) {
  172. int newrefcnt;
  173. newrefcnt = atomic_dec_return(&dst->__refcnt);
  174. if (unlikely(newrefcnt < 0))
  175. net_warn_ratelimited("%s: dst:%p refcnt:%d\n",
  176. __func__, dst, newrefcnt);
  177. if (!newrefcnt)
  178. dst_destroy(dst);
  179. }
  180. }
  181. EXPORT_SYMBOL(dst_release_immediate);
  182. u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old)
  183. {
  184. struct dst_metrics *p = kmalloc(sizeof(*p), GFP_ATOMIC);
  185. if (p) {
  186. struct dst_metrics *old_p = (struct dst_metrics *)__DST_METRICS_PTR(old);
  187. unsigned long prev, new;
  188. refcount_set(&p->refcnt, 1);
  189. memcpy(p->metrics, old_p->metrics, sizeof(p->metrics));
  190. new = (unsigned long) p;
  191. prev = cmpxchg(&dst->_metrics, old, new);
  192. if (prev != old) {
  193. kfree(p);
  194. p = (struct dst_metrics *)__DST_METRICS_PTR(prev);
  195. if (prev & DST_METRICS_READ_ONLY)
  196. p = NULL;
  197. } else if (prev & DST_METRICS_REFCOUNTED) {
  198. if (refcount_dec_and_test(&old_p->refcnt))
  199. kfree(old_p);
  200. }
  201. }
  202. BUILD_BUG_ON(offsetof(struct dst_metrics, metrics) != 0);
  203. return (u32 *)p;
  204. }
  205. EXPORT_SYMBOL(dst_cow_metrics_generic);
  206. /* Caller asserts that dst_metrics_read_only(dst) is false. */
  207. void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old)
  208. {
  209. unsigned long prev, new;
  210. new = ((unsigned long) &dst_default_metrics) | DST_METRICS_READ_ONLY;
  211. prev = cmpxchg(&dst->_metrics, old, new);
  212. if (prev == old)
  213. kfree(__DST_METRICS_PTR(old));
  214. }
  215. EXPORT_SYMBOL(__dst_destroy_metrics_generic);
  216. static struct dst_ops md_dst_ops = {
  217. .family = AF_UNSPEC,
  218. };
  219. static int dst_md_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  220. {
  221. WARN_ONCE(1, "Attempting to call output on metadata dst\n");
  222. kfree_skb(skb);
  223. return 0;
  224. }
  225. static int dst_md_discard(struct sk_buff *skb)
  226. {
  227. WARN_ONCE(1, "Attempting to call input on metadata dst\n");
  228. kfree_skb(skb);
  229. return 0;
  230. }
  231. static void __metadata_dst_init(struct metadata_dst *md_dst,
  232. enum metadata_type type, u8 optslen)
  233. {
  234. struct dst_entry *dst;
  235. dst = &md_dst->dst;
  236. dst_init(dst, &md_dst_ops, NULL, 1, DST_OBSOLETE_NONE,
  237. DST_METADATA | DST_NOCOUNT);
  238. dst->input = dst_md_discard;
  239. dst->output = dst_md_discard_out;
  240. memset(dst + 1, 0, sizeof(*md_dst) + optslen - sizeof(*dst));
  241. md_dst->type = type;
  242. }
  243. struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
  244. gfp_t flags)
  245. {
  246. struct metadata_dst *md_dst;
  247. md_dst = kmalloc(sizeof(*md_dst) + optslen, flags);
  248. if (!md_dst)
  249. return NULL;
  250. __metadata_dst_init(md_dst, type, optslen);
  251. return md_dst;
  252. }
  253. EXPORT_SYMBOL_GPL(metadata_dst_alloc);
  254. void metadata_dst_free(struct metadata_dst *md_dst)
  255. {
  256. #ifdef CONFIG_DST_CACHE
  257. if (md_dst->type == METADATA_IP_TUNNEL)
  258. dst_cache_destroy(&md_dst->u.tun_info.dst_cache);
  259. #endif
  260. kfree(md_dst);
  261. }
  262. struct metadata_dst __percpu *
  263. metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags)
  264. {
  265. int cpu;
  266. struct metadata_dst __percpu *md_dst;
  267. md_dst = __alloc_percpu_gfp(sizeof(struct metadata_dst) + optslen,
  268. __alignof__(struct metadata_dst), flags);
  269. if (!md_dst)
  270. return NULL;
  271. for_each_possible_cpu(cpu)
  272. __metadata_dst_init(per_cpu_ptr(md_dst, cpu), type, optslen);
  273. return md_dst;
  274. }
  275. EXPORT_SYMBOL_GPL(metadata_dst_alloc_percpu);
  276. void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst)
  277. {
  278. #ifdef CONFIG_DST_CACHE
  279. int cpu;
  280. for_each_possible_cpu(cpu) {
  281. struct metadata_dst *one_md_dst = per_cpu_ptr(md_dst, cpu);
  282. if (one_md_dst->type == METADATA_IP_TUNNEL)
  283. dst_cache_destroy(&one_md_dst->u.tun_info.dst_cache);
  284. }
  285. #endif
  286. free_percpu(md_dst);
  287. }
  288. EXPORT_SYMBOL_GPL(metadata_dst_free_percpu);