dst.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. EXPORT_SYMBOL(dst_default_metrics);
  57. void dst_init(struct dst_entry *dst, struct dst_ops *ops,
  58. struct net_device *dev, int initial_ref, int initial_obsolete,
  59. unsigned short flags)
  60. {
  61. dst->dev = dev;
  62. if (dev)
  63. dev_hold(dev);
  64. dst->ops = ops;
  65. dst_init_metrics(dst, dst_default_metrics.metrics, true);
  66. dst->expires = 0UL;
  67. #ifdef CONFIG_XFRM
  68. dst->xfrm = NULL;
  69. #endif
  70. dst->input = dst_discard;
  71. dst->output = dst_discard_out;
  72. dst->error = 0;
  73. dst->obsolete = initial_obsolete;
  74. dst->header_len = 0;
  75. dst->trailer_len = 0;
  76. #ifdef CONFIG_IP_ROUTE_CLASSID
  77. dst->tclassid = 0;
  78. #endif
  79. dst->lwtstate = NULL;
  80. atomic_set(&dst->__refcnt, initial_ref);
  81. dst->__use = 0;
  82. dst->lastuse = jiffies;
  83. dst->flags = flags;
  84. if (!(flags & DST_NOCOUNT))
  85. dst_entries_add(ops, 1);
  86. }
  87. EXPORT_SYMBOL(dst_init);
  88. void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
  89. int initial_ref, int initial_obsolete, unsigned short flags)
  90. {
  91. struct dst_entry *dst;
  92. if (ops->gc && dst_entries_get_fast(ops) > ops->gc_thresh) {
  93. if (ops->gc(ops))
  94. return NULL;
  95. }
  96. dst = kmem_cache_alloc(ops->kmem_cachep, GFP_ATOMIC);
  97. if (!dst)
  98. return NULL;
  99. dst_init(dst, ops, dev, initial_ref, initial_obsolete, flags);
  100. return dst;
  101. }
  102. EXPORT_SYMBOL(dst_alloc);
  103. struct dst_entry *dst_destroy(struct dst_entry * dst)
  104. {
  105. struct dst_entry *child = NULL;
  106. smp_rmb();
  107. #ifdef CONFIG_XFRM
  108. if (dst->xfrm) {
  109. struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
  110. child = xdst->child;
  111. }
  112. #endif
  113. if (!(dst->flags & DST_NOCOUNT))
  114. dst_entries_add(dst->ops, -1);
  115. if (dst->ops->destroy)
  116. dst->ops->destroy(dst);
  117. if (dst->dev)
  118. dev_put(dst->dev);
  119. lwtstate_put(dst->lwtstate);
  120. if (dst->flags & DST_METADATA)
  121. metadata_dst_free((struct metadata_dst *)dst);
  122. else
  123. kmem_cache_free(dst->ops->kmem_cachep, dst);
  124. dst = child;
  125. if (dst)
  126. dst_release_immediate(dst);
  127. return NULL;
  128. }
  129. EXPORT_SYMBOL(dst_destroy);
  130. static void dst_destroy_rcu(struct rcu_head *head)
  131. {
  132. struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
  133. dst = dst_destroy(dst);
  134. }
  135. /* Operations to mark dst as DEAD and clean up the net device referenced
  136. * by dst:
  137. * 1. put the dst under loopback interface and discard all tx/rx packets
  138. * on this route.
  139. * 2. release the net_device
  140. * This function should be called when removing routes from the fib tree
  141. * in preparation for a NETDEV_DOWN/NETDEV_UNREGISTER event and also to
  142. * make the next dst_ops->check() fail.
  143. */
  144. void dst_dev_put(struct dst_entry *dst)
  145. {
  146. struct net_device *dev = dst->dev;
  147. dst->obsolete = DST_OBSOLETE_DEAD;
  148. if (dst->ops->ifdown)
  149. dst->ops->ifdown(dst, dev, true);
  150. dst->input = dst_discard;
  151. dst->output = dst_discard_out;
  152. dst->dev = dev_net(dst->dev)->loopback_dev;
  153. dev_hold(dst->dev);
  154. dev_put(dev);
  155. }
  156. EXPORT_SYMBOL(dst_dev_put);
  157. void dst_release(struct dst_entry *dst)
  158. {
  159. if (dst) {
  160. int newrefcnt;
  161. newrefcnt = atomic_dec_return(&dst->__refcnt);
  162. if (unlikely(newrefcnt < 0))
  163. net_warn_ratelimited("%s: dst:%p refcnt:%d\n",
  164. __func__, dst, newrefcnt);
  165. if (!newrefcnt)
  166. call_rcu(&dst->rcu_head, dst_destroy_rcu);
  167. }
  168. }
  169. EXPORT_SYMBOL(dst_release);
  170. void dst_release_immediate(struct dst_entry *dst)
  171. {
  172. if (dst) {
  173. int newrefcnt;
  174. newrefcnt = atomic_dec_return(&dst->__refcnt);
  175. if (unlikely(newrefcnt < 0))
  176. net_warn_ratelimited("%s: dst:%p refcnt:%d\n",
  177. __func__, dst, newrefcnt);
  178. if (!newrefcnt)
  179. dst_destroy(dst);
  180. }
  181. }
  182. EXPORT_SYMBOL(dst_release_immediate);
  183. u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old)
  184. {
  185. struct dst_metrics *p = kmalloc(sizeof(*p), GFP_ATOMIC);
  186. if (p) {
  187. struct dst_metrics *old_p = (struct dst_metrics *)__DST_METRICS_PTR(old);
  188. unsigned long prev, new;
  189. refcount_set(&p->refcnt, 1);
  190. memcpy(p->metrics, old_p->metrics, sizeof(p->metrics));
  191. new = (unsigned long) p;
  192. prev = cmpxchg(&dst->_metrics, old, new);
  193. if (prev != old) {
  194. kfree(p);
  195. p = (struct dst_metrics *)__DST_METRICS_PTR(prev);
  196. if (prev & DST_METRICS_READ_ONLY)
  197. p = NULL;
  198. } else if (prev & DST_METRICS_REFCOUNTED) {
  199. if (refcount_dec_and_test(&old_p->refcnt))
  200. kfree(old_p);
  201. }
  202. }
  203. BUILD_BUG_ON(offsetof(struct dst_metrics, metrics) != 0);
  204. return (u32 *)p;
  205. }
  206. EXPORT_SYMBOL(dst_cow_metrics_generic);
  207. /* Caller asserts that dst_metrics_read_only(dst) is false. */
  208. void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old)
  209. {
  210. unsigned long prev, new;
  211. new = ((unsigned long) &dst_default_metrics) | DST_METRICS_READ_ONLY;
  212. prev = cmpxchg(&dst->_metrics, old, new);
  213. if (prev == old)
  214. kfree(__DST_METRICS_PTR(old));
  215. }
  216. EXPORT_SYMBOL(__dst_destroy_metrics_generic);
  217. static struct dst_ops md_dst_ops = {
  218. .family = AF_UNSPEC,
  219. };
  220. static int dst_md_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  221. {
  222. WARN_ONCE(1, "Attempting to call output on metadata dst\n");
  223. kfree_skb(skb);
  224. return 0;
  225. }
  226. static int dst_md_discard(struct sk_buff *skb)
  227. {
  228. WARN_ONCE(1, "Attempting to call input on metadata dst\n");
  229. kfree_skb(skb);
  230. return 0;
  231. }
  232. static void __metadata_dst_init(struct metadata_dst *md_dst,
  233. enum metadata_type type, u8 optslen)
  234. {
  235. struct dst_entry *dst;
  236. dst = &md_dst->dst;
  237. dst_init(dst, &md_dst_ops, NULL, 1, DST_OBSOLETE_NONE,
  238. DST_METADATA | DST_NOCOUNT);
  239. dst->input = dst_md_discard;
  240. dst->output = dst_md_discard_out;
  241. memset(dst + 1, 0, sizeof(*md_dst) + optslen - sizeof(*dst));
  242. md_dst->type = type;
  243. }
  244. struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
  245. gfp_t flags)
  246. {
  247. struct metadata_dst *md_dst;
  248. md_dst = kmalloc(sizeof(*md_dst) + optslen, flags);
  249. if (!md_dst)
  250. return NULL;
  251. __metadata_dst_init(md_dst, type, optslen);
  252. return md_dst;
  253. }
  254. EXPORT_SYMBOL_GPL(metadata_dst_alloc);
  255. void metadata_dst_free(struct metadata_dst *md_dst)
  256. {
  257. #ifdef CONFIG_DST_CACHE
  258. if (md_dst->type == METADATA_IP_TUNNEL)
  259. dst_cache_destroy(&md_dst->u.tun_info.dst_cache);
  260. #endif
  261. kfree(md_dst);
  262. }
  263. EXPORT_SYMBOL_GPL(metadata_dst_free);
  264. struct metadata_dst __percpu *
  265. metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags)
  266. {
  267. int cpu;
  268. struct metadata_dst __percpu *md_dst;
  269. md_dst = __alloc_percpu_gfp(sizeof(struct metadata_dst) + optslen,
  270. __alignof__(struct metadata_dst), flags);
  271. if (!md_dst)
  272. return NULL;
  273. for_each_possible_cpu(cpu)
  274. __metadata_dst_init(per_cpu_ptr(md_dst, cpu), type, optslen);
  275. return md_dst;
  276. }
  277. EXPORT_SYMBOL_GPL(metadata_dst_alloc_percpu);
  278. void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst)
  279. {
  280. #ifdef CONFIG_DST_CACHE
  281. int cpu;
  282. for_each_possible_cpu(cpu) {
  283. struct metadata_dst *one_md_dst = per_cpu_ptr(md_dst, cpu);
  284. if (one_md_dst->type == METADATA_IP_TUNNEL)
  285. dst_cache_destroy(&one_md_dst->u.tun_info.dst_cache);
  286. }
  287. #endif
  288. free_percpu(md_dst);
  289. }
  290. EXPORT_SYMBOL_GPL(metadata_dst_free_percpu);