dst.c 7.8 KB

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