xref: /openbmc/linux/net/core/dst.c (revision 43c28172)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * net/core/dst.c	Protocol independent destination cache.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Authors:		Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/bitops.h>
101da177e4SLinus Torvalds #include <linux/errno.h>
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/kernel.h>
1386bba269SEric Dumazet #include <linux/workqueue.h>
141da177e4SLinus Torvalds #include <linux/mm.h>
151da177e4SLinus Torvalds #include <linux/module.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/netdevice.h>
181da177e4SLinus Torvalds #include <linux/skbuff.h>
191da177e4SLinus Torvalds #include <linux/string.h>
201da177e4SLinus Torvalds #include <linux/types.h>
21e9dc8653SEric W. Biederman #include <net/net_namespace.h>
222fc1b5ddSEric Dumazet #include <linux/sched.h>
23268bb0ceSLinus Torvalds #include <linux/prefetch.h>
2461adedf3SJiri Benc #include <net/lwtunnel.h>
25b6ca8bd5SDavid Miller #include <net/xfrm.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include <net/dst.h>
28f38a9eb1SThomas Graf #include <net/dst_metadata.h>
291da177e4SLinus Torvalds 
dst_discard_out(struct net * net,struct sock * sk,struct sk_buff * skb)30ede2059dSEric W. Biederman int dst_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	kfree_skb(skb);
331da177e4SLinus Torvalds 	return 0;
341da177e4SLinus Torvalds }
35ede2059dSEric W. Biederman EXPORT_SYMBOL(dst_discard_out);
361da177e4SLinus Torvalds 
373fb07dafSEric Dumazet const struct dst_metrics dst_default_metrics = {
38a37e6e34SEric Dumazet 	/* This initializer is needed to force linker to place this variable
39a37e6e34SEric Dumazet 	 * into const section. Otherwise it might end into bss section.
40a37e6e34SEric Dumazet 	 * We really want to avoid false sharing on this variable, and catch
41a37e6e34SEric Dumazet 	 * any writes on it.
42a37e6e34SEric Dumazet 	 */
439620fef2SEric Dumazet 	.refcnt = REFCOUNT_INIT(1),
44a37e6e34SEric Dumazet };
45d4ead6b3SDavid Ahern EXPORT_SYMBOL(dst_default_metrics);
46a37e6e34SEric Dumazet 
dst_init(struct dst_entry * dst,struct dst_ops * ops,struct net_device * dev,int initial_ref,int initial_obsolete,unsigned short flags)47f38a9eb1SThomas Graf void dst_init(struct dst_entry *dst, struct dst_ops *ops,
48f38a9eb1SThomas Graf 	      struct net_device *dev, int initial_ref, int initial_obsolete,
49f38a9eb1SThomas Graf 	      unsigned short flags)
501da177e4SLinus Torvalds {
515c1e6aa3SDavid S. Miller 	dst->dev = dev;
52d62607c3SJakub Kicinski 	netdev_hold(dev, &dst->dev_tracker, GFP_ATOMIC);
531da177e4SLinus Torvalds 	dst->ops = ops;
543fb07dafSEric Dumazet 	dst_init_metrics(dst, dst_default_metrics.metrics, true);
55cf911662SDavid S. Miller 	dst->expires = 0UL;
56cf911662SDavid S. Miller #ifdef CONFIG_XFRM
57cf911662SDavid S. Miller 	dst->xfrm = NULL;
581da177e4SLinus Torvalds #endif
595c1e6aa3SDavid S. Miller 	dst->input = dst_discard;
60ede2059dSEric W. Biederman 	dst->output = dst_discard_out;
61cf911662SDavid S. Miller 	dst->error = 0;
625c1e6aa3SDavid S. Miller 	dst->obsolete = initial_obsolete;
63cf911662SDavid S. Miller 	dst->header_len = 0;
64cf911662SDavid S. Miller 	dst->trailer_len = 0;
65cf911662SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID
66cf911662SDavid S. Miller 	dst->tclassid = 0;
67cf911662SDavid S. Miller #endif
6861adedf3SJiri Benc 	dst->lwtstate = NULL;
69bc9d3a9fSThomas Gleixner 	rcuref_init(&dst->__rcuref, initial_ref);
70418a7307SMaxime Bizon 	INIT_LIST_HEAD(&dst->rt_uncached);
71cf911662SDavid S. Miller 	dst->__use = 0;
725c1e6aa3SDavid S. Miller 	dst->lastuse = jiffies;
735c1e6aa3SDavid S. Miller 	dst->flags = flags;
74957c665fSDavid S. Miller 	if (!(flags & DST_NOCOUNT))
75fc66f95cSEric Dumazet 		dst_entries_add(ops, 1);
76f38a9eb1SThomas Graf }
77f38a9eb1SThomas Graf EXPORT_SYMBOL(dst_init);
78f38a9eb1SThomas Graf 
dst_alloc(struct dst_ops * ops,struct net_device * dev,int initial_ref,int initial_obsolete,unsigned short flags)79f38a9eb1SThomas Graf void *dst_alloc(struct dst_ops *ops, struct net_device *dev,
80f38a9eb1SThomas Graf 		int initial_ref, int initial_obsolete, unsigned short flags)
81f38a9eb1SThomas Graf {
82f38a9eb1SThomas Graf 	struct dst_entry *dst;
83f38a9eb1SThomas Graf 
84cf86a086SEric Dumazet 	if (ops->gc &&
85cf86a086SEric Dumazet 	    !(flags & DST_NOCOUNT) &&
86af6d1034SJon Maxwell 	    dst_entries_get_fast(ops) > ops->gc_thresh)
87af6d1034SJon Maxwell 		ops->gc(ops);
88f38a9eb1SThomas Graf 
89f38a9eb1SThomas Graf 	dst = kmem_cache_alloc(ops->kmem_cachep, GFP_ATOMIC);
90f38a9eb1SThomas Graf 	if (!dst)
91f38a9eb1SThomas Graf 		return NULL;
92f38a9eb1SThomas Graf 
93f38a9eb1SThomas Graf 	dst_init(dst, ops, dev, initial_ref, initial_obsolete, flags);
94f38a9eb1SThomas Graf 
951da177e4SLinus Torvalds 	return dst;
961da177e4SLinus Torvalds }
97598ed936Slaurent chavey EXPORT_SYMBOL(dst_alloc);
981da177e4SLinus Torvalds 
dst_destroy(struct dst_entry * dst)991da177e4SLinus Torvalds struct dst_entry *dst_destroy(struct dst_entry * dst)
1001da177e4SLinus Torvalds {
101b92cf4aaSDavid Miller 	struct dst_entry *child = NULL;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	smp_rmb();
1041da177e4SLinus Torvalds 
105b92cf4aaSDavid Miller #ifdef CONFIG_XFRM
106b6ca8bd5SDavid Miller 	if (dst->xfrm) {
107b6ca8bd5SDavid Miller 		struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
108b6ca8bd5SDavid Miller 
109b6ca8bd5SDavid Miller 		child = xdst->child;
110b6ca8bd5SDavid Miller 	}
111b92cf4aaSDavid Miller #endif
112957c665fSDavid S. Miller 	if (!(dst->flags & DST_NOCOUNT))
113fc66f95cSEric Dumazet 		dst_entries_add(dst->ops, -1);
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	if (dst->ops->destroy)
1161da177e4SLinus Torvalds 		dst->ops->destroy(dst);
117d62607c3SJakub Kicinski 	netdev_put(dst->dev, &dst->dev_tracker);
118f38a9eb1SThomas Graf 
119e252b3d1SWANG Cong 	lwtstate_put(dst->lwtstate);
120e252b3d1SWANG Cong 
121f38a9eb1SThomas Graf 	if (dst->flags & DST_METADATA)
122d71785ffSPaolo Abeni 		metadata_dst_free((struct metadata_dst *)dst);
123f38a9eb1SThomas Graf 	else
1241da177e4SLinus Torvalds 		kmem_cache_free(dst->ops->kmem_cachep, dst);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	dst = child;
12752df157fSWei Wang 	if (dst)
12852df157fSWei Wang 		dst_release_immediate(dst);
1291da177e4SLinus Torvalds 	return NULL;
1301da177e4SLinus Torvalds }
131598ed936Slaurent chavey EXPORT_SYMBOL(dst_destroy);
1321da177e4SLinus Torvalds 
dst_destroy_rcu(struct rcu_head * head)133f8864972SEric Dumazet static void dst_destroy_rcu(struct rcu_head *head)
134f8864972SEric Dumazet {
135f8864972SEric Dumazet 	struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
136f8864972SEric Dumazet 
137f8864972SEric Dumazet 	dst = dst_destroy(dst);
138f8864972SEric Dumazet }
139f8864972SEric Dumazet 
1404a6ce2b6SWei Wang /* Operations to mark dst as DEAD and clean up the net device referenced
1414a6ce2b6SWei Wang  * by dst:
1421be107deSMiaohe Lin  * 1. put the dst under blackhole interface and discard all tx/rx packets
1434a6ce2b6SWei Wang  *    on this route.
1444a6ce2b6SWei Wang  * 2. release the net_device
1454a6ce2b6SWei Wang  * This function should be called when removing routes from the fib tree
1464a6ce2b6SWei Wang  * in preparation for a NETDEV_DOWN/NETDEV_UNREGISTER event and also to
1474a6ce2b6SWei Wang  * make the next dst_ops->check() fail.
1484a6ce2b6SWei Wang  */
dst_dev_put(struct dst_entry * dst)1494a6ce2b6SWei Wang void dst_dev_put(struct dst_entry *dst)
1504a6ce2b6SWei Wang {
1514a6ce2b6SWei Wang 	struct net_device *dev = dst->dev;
1524a6ce2b6SWei Wang 
1534a6ce2b6SWei Wang 	dst->obsolete = DST_OBSOLETE_DEAD;
1544a6ce2b6SWei Wang 	if (dst->ops->ifdown)
155*43c28172SZhengchao Shao 		dst->ops->ifdown(dst, dev);
1564a6ce2b6SWei Wang 	dst->input = dst_discard;
1574a6ce2b6SWei Wang 	dst->output = dst_discard_out;
1588d7017fdSMahesh Bandewar 	dst->dev = blackhole_netdev;
159d62607c3SJakub Kicinski 	netdev_ref_replace(dev, blackhole_netdev, &dst->dev_tracker,
1609038c320SEric Dumazet 			   GFP_ATOMIC);
1614a6ce2b6SWei Wang }
1624a6ce2b6SWei Wang EXPORT_SYMBOL(dst_dev_put);
1634a6ce2b6SWei Wang 
dst_release(struct dst_entry * dst)1648d330868SIlpo Järvinen void dst_release(struct dst_entry *dst)
1658d330868SIlpo Järvinen {
166bc9d3a9fSThomas Gleixner 	if (dst && rcuref_put(&dst->__rcuref))
167483c26ffSJoel Fernandes (Google) 		call_rcu_hurry(&dst->rcu_head, dst_destroy_rcu);
1688d330868SIlpo Järvinen }
1698d330868SIlpo Järvinen EXPORT_SYMBOL(dst_release);
1708d330868SIlpo Järvinen 
dst_release_immediate(struct dst_entry * dst)1715f56f409SWei Wang void dst_release_immediate(struct dst_entry *dst)
1725f56f409SWei Wang {
173bc9d3a9fSThomas Gleixner 	if (dst && rcuref_put(&dst->__rcuref))
1745f56f409SWei Wang 		dst_destroy(dst);
1755f56f409SWei Wang }
1765f56f409SWei Wang EXPORT_SYMBOL(dst_release_immediate);
1775f56f409SWei Wang 
dst_cow_metrics_generic(struct dst_entry * dst,unsigned long old)17862fa8a84SDavid S. Miller u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old)
17962fa8a84SDavid S. Miller {
1803fb07dafSEric Dumazet 	struct dst_metrics *p = kmalloc(sizeof(*p), GFP_ATOMIC);
18162fa8a84SDavid S. Miller 
18262fa8a84SDavid S. Miller 	if (p) {
1833fb07dafSEric Dumazet 		struct dst_metrics *old_p = (struct dst_metrics *)__DST_METRICS_PTR(old);
18462fa8a84SDavid S. Miller 		unsigned long prev, new;
18562fa8a84SDavid S. Miller 
1869620fef2SEric Dumazet 		refcount_set(&p->refcnt, 1);
1873fb07dafSEric Dumazet 		memcpy(p->metrics, old_p->metrics, sizeof(p->metrics));
18862fa8a84SDavid S. Miller 
18962fa8a84SDavid S. Miller 		new = (unsigned long) p;
19062fa8a84SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
19162fa8a84SDavid S. Miller 
19262fa8a84SDavid S. Miller 		if (prev != old) {
19362fa8a84SDavid S. Miller 			kfree(p);
1943fb07dafSEric Dumazet 			p = (struct dst_metrics *)__DST_METRICS_PTR(prev);
19562fa8a84SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
19662fa8a84SDavid S. Miller 				p = NULL;
1973fb07dafSEric Dumazet 		} else if (prev & DST_METRICS_REFCOUNTED) {
1989620fef2SEric Dumazet 			if (refcount_dec_and_test(&old_p->refcnt))
1993fb07dafSEric Dumazet 				kfree(old_p);
20062fa8a84SDavid S. Miller 		}
20162fa8a84SDavid S. Miller 	}
2023fb07dafSEric Dumazet 	BUILD_BUG_ON(offsetof(struct dst_metrics, metrics) != 0);
2033fb07dafSEric Dumazet 	return (u32 *)p;
20462fa8a84SDavid S. Miller }
20562fa8a84SDavid S. Miller EXPORT_SYMBOL(dst_cow_metrics_generic);
20662fa8a84SDavid S. Miller 
20762fa8a84SDavid S. Miller /* Caller asserts that dst_metrics_read_only(dst) is false.  */
__dst_destroy_metrics_generic(struct dst_entry * dst,unsigned long old)20862fa8a84SDavid S. Miller void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old)
20962fa8a84SDavid S. Miller {
21062fa8a84SDavid S. Miller 	unsigned long prev, new;
21162fa8a84SDavid S. Miller 
2123fb07dafSEric Dumazet 	new = ((unsigned long) &dst_default_metrics) | DST_METRICS_READ_ONLY;
21362fa8a84SDavid S. Miller 	prev = cmpxchg(&dst->_metrics, old, new);
21462fa8a84SDavid S. Miller 	if (prev == old)
21562fa8a84SDavid S. Miller 		kfree(__DST_METRICS_PTR(old));
21662fa8a84SDavid S. Miller }
21762fa8a84SDavid S. Miller EXPORT_SYMBOL(__dst_destroy_metrics_generic);
21862fa8a84SDavid S. Miller 
dst_blackhole_check(struct dst_entry * dst,u32 cookie)219c4c877b2SDaniel Borkmann struct dst_entry *dst_blackhole_check(struct dst_entry *dst, u32 cookie)
220c4c877b2SDaniel Borkmann {
221c4c877b2SDaniel Borkmann 	return NULL;
222c4c877b2SDaniel Borkmann }
223c4c877b2SDaniel Borkmann 
dst_blackhole_cow_metrics(struct dst_entry * dst,unsigned long old)224c4c877b2SDaniel Borkmann u32 *dst_blackhole_cow_metrics(struct dst_entry *dst, unsigned long old)
225c4c877b2SDaniel Borkmann {
226c4c877b2SDaniel Borkmann 	return NULL;
227c4c877b2SDaniel Borkmann }
228c4c877b2SDaniel Borkmann 
dst_blackhole_neigh_lookup(const struct dst_entry * dst,struct sk_buff * skb,const void * daddr)229c4c877b2SDaniel Borkmann struct neighbour *dst_blackhole_neigh_lookup(const struct dst_entry *dst,
230c4c877b2SDaniel Borkmann 					     struct sk_buff *skb,
231c4c877b2SDaniel Borkmann 					     const void *daddr)
232c4c877b2SDaniel Borkmann {
233c4c877b2SDaniel Borkmann 	return NULL;
234c4c877b2SDaniel Borkmann }
235c4c877b2SDaniel Borkmann 
dst_blackhole_update_pmtu(struct dst_entry * dst,struct sock * sk,struct sk_buff * skb,u32 mtu,bool confirm_neigh)236c4c877b2SDaniel Borkmann void dst_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
237c4c877b2SDaniel Borkmann 			       struct sk_buff *skb, u32 mtu,
238c4c877b2SDaniel Borkmann 			       bool confirm_neigh)
239c4c877b2SDaniel Borkmann {
240c4c877b2SDaniel Borkmann }
241c4c877b2SDaniel Borkmann EXPORT_SYMBOL_GPL(dst_blackhole_update_pmtu);
242c4c877b2SDaniel Borkmann 
dst_blackhole_redirect(struct dst_entry * dst,struct sock * sk,struct sk_buff * skb)243c4c877b2SDaniel Borkmann void dst_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
244c4c877b2SDaniel Borkmann 			    struct sk_buff *skb)
245c4c877b2SDaniel Borkmann {
246c4c877b2SDaniel Borkmann }
247c4c877b2SDaniel Borkmann EXPORT_SYMBOL_GPL(dst_blackhole_redirect);
248c4c877b2SDaniel Borkmann 
dst_blackhole_mtu(const struct dst_entry * dst)249c4c877b2SDaniel Borkmann unsigned int dst_blackhole_mtu(const struct dst_entry *dst)
250c4c877b2SDaniel Borkmann {
251c4c877b2SDaniel Borkmann 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
252c4c877b2SDaniel Borkmann 
253c4c877b2SDaniel Borkmann 	return mtu ? : dst->dev->mtu;
254c4c877b2SDaniel Borkmann }
255c4c877b2SDaniel Borkmann EXPORT_SYMBOL_GPL(dst_blackhole_mtu);
256c4c877b2SDaniel Borkmann 
257a188bb56SDaniel Borkmann static struct dst_ops dst_blackhole_ops = {
258f38a9eb1SThomas Graf 	.family		= AF_UNSPEC,
259a188bb56SDaniel Borkmann 	.neigh_lookup	= dst_blackhole_neigh_lookup,
260a188bb56SDaniel Borkmann 	.check		= dst_blackhole_check,
261a188bb56SDaniel Borkmann 	.cow_metrics	= dst_blackhole_cow_metrics,
262a188bb56SDaniel Borkmann 	.update_pmtu	= dst_blackhole_update_pmtu,
263a188bb56SDaniel Borkmann 	.redirect	= dst_blackhole_redirect,
264a188bb56SDaniel Borkmann 	.mtu		= dst_blackhole_mtu,
265f38a9eb1SThomas Graf };
266f38a9eb1SThomas Graf 
__metadata_dst_init(struct metadata_dst * md_dst,enum metadata_type type,u8 optslen)2673fcece12SJakub Kicinski static void __metadata_dst_init(struct metadata_dst *md_dst,
2683fcece12SJakub Kicinski 				enum metadata_type type, u8 optslen)
269f38a9eb1SThomas Graf {
270f38a9eb1SThomas Graf 	struct dst_entry *dst;
271f38a9eb1SThomas Graf 
272f38a9eb1SThomas Graf 	dst = &md_dst->dst;
273a188bb56SDaniel Borkmann 	dst_init(dst, &dst_blackhole_ops, NULL, 1, DST_OBSOLETE_NONE,
274a4c2fd7fSWei Wang 		 DST_METADATA | DST_NOCOUNT);
275f38a9eb1SThomas Graf 	memset(dst + 1, 0, sizeof(*md_dst) + optslen - sizeof(*dst));
2763fcece12SJakub Kicinski 	md_dst->type = type;
277d3aa45ceSAlexei Starovoitov }
278d3aa45ceSAlexei Starovoitov 
metadata_dst_alloc(u8 optslen,enum metadata_type type,gfp_t flags)2793fcece12SJakub Kicinski struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
2803fcece12SJakub Kicinski 					gfp_t flags)
281d3aa45ceSAlexei Starovoitov {
282d3aa45ceSAlexei Starovoitov 	struct metadata_dst *md_dst;
283d3aa45ceSAlexei Starovoitov 
284d3aa45ceSAlexei Starovoitov 	md_dst = kmalloc(sizeof(*md_dst) + optslen, flags);
285d3aa45ceSAlexei Starovoitov 	if (!md_dst)
286d3aa45ceSAlexei Starovoitov 		return NULL;
287d3aa45ceSAlexei Starovoitov 
2883fcece12SJakub Kicinski 	__metadata_dst_init(md_dst, type, optslen);
289f38a9eb1SThomas Graf 
290f38a9eb1SThomas Graf 	return md_dst;
291f38a9eb1SThomas Graf }
292f38a9eb1SThomas Graf EXPORT_SYMBOL_GPL(metadata_dst_alloc);
293f38a9eb1SThomas Graf 
metadata_dst_free(struct metadata_dst * md_dst)294d71785ffSPaolo Abeni void metadata_dst_free(struct metadata_dst *md_dst)
295d71785ffSPaolo Abeni {
296d71785ffSPaolo Abeni #ifdef CONFIG_DST_CACHE
297e65a4955SDavid Lamparter 	if (md_dst->type == METADATA_IP_TUNNEL)
298d71785ffSPaolo Abeni 		dst_cache_destroy(&md_dst->u.tun_info.dst_cache);
299d71785ffSPaolo Abeni #endif
30094151f5aSEyal Birger 	if (md_dst->type == METADATA_XFRM)
30194151f5aSEyal Birger 		dst_release(md_dst->u.xfrm_info.dst_orig);
302d71785ffSPaolo Abeni 	kfree(md_dst);
303d71785ffSPaolo Abeni }
304af308b94SPablo Neira Ayuso EXPORT_SYMBOL_GPL(metadata_dst_free);
305d71785ffSPaolo Abeni 
3063fcece12SJakub Kicinski struct metadata_dst __percpu *
metadata_dst_alloc_percpu(u8 optslen,enum metadata_type type,gfp_t flags)3073fcece12SJakub Kicinski metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags)
308d3aa45ceSAlexei Starovoitov {
309d3aa45ceSAlexei Starovoitov 	int cpu;
310d3aa45ceSAlexei Starovoitov 	struct metadata_dst __percpu *md_dst;
311d3aa45ceSAlexei Starovoitov 
312d3aa45ceSAlexei Starovoitov 	md_dst = __alloc_percpu_gfp(sizeof(struct metadata_dst) + optslen,
313d3aa45ceSAlexei Starovoitov 				    __alignof__(struct metadata_dst), flags);
314d3aa45ceSAlexei Starovoitov 	if (!md_dst)
315d3aa45ceSAlexei Starovoitov 		return NULL;
316d3aa45ceSAlexei Starovoitov 
317d3aa45ceSAlexei Starovoitov 	for_each_possible_cpu(cpu)
3183fcece12SJakub Kicinski 		__metadata_dst_init(per_cpu_ptr(md_dst, cpu), type, optslen);
319d3aa45ceSAlexei Starovoitov 
320d3aa45ceSAlexei Starovoitov 	return md_dst;
321d3aa45ceSAlexei Starovoitov }
322d3aa45ceSAlexei Starovoitov EXPORT_SYMBOL_GPL(metadata_dst_alloc_percpu);
323d66f2b91SJakub Kicinski 
metadata_dst_free_percpu(struct metadata_dst __percpu * md_dst)324d66f2b91SJakub Kicinski void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst)
325d66f2b91SJakub Kicinski {
326d66f2b91SJakub Kicinski 	int cpu;
327d66f2b91SJakub Kicinski 
328d66f2b91SJakub Kicinski 	for_each_possible_cpu(cpu) {
329d66f2b91SJakub Kicinski 		struct metadata_dst *one_md_dst = per_cpu_ptr(md_dst, cpu);
330d66f2b91SJakub Kicinski 
33194151f5aSEyal Birger #ifdef CONFIG_DST_CACHE
332d66f2b91SJakub Kicinski 		if (one_md_dst->type == METADATA_IP_TUNNEL)
333d66f2b91SJakub Kicinski 			dst_cache_destroy(&one_md_dst->u.tun_info.dst_cache);
334d66f2b91SJakub Kicinski #endif
33594151f5aSEyal Birger 		if (one_md_dst->type == METADATA_XFRM)
33694151f5aSEyal Birger 			dst_release(one_md_dst->u.xfrm_info.dst_orig);
33794151f5aSEyal Birger 	}
338d66f2b91SJakub Kicinski 	free_percpu(md_dst);
339d66f2b91SJakub Kicinski }
340d66f2b91SJakub Kicinski EXPORT_SYMBOL_GPL(metadata_dst_free_percpu);
341