xref: /openbmc/linux/net/core/dst.c (revision 7b7fd0ac7dc1ffcaf24d9bca0f051b0168e43cd4)
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
1121da177e4SLinus Torvalds 	if (dst->ops->destroy)
1131da177e4SLinus Torvalds 		dst->ops->destroy(dst);
114d62607c3SJakub Kicinski 	netdev_put(dst->dev, &dst->dev_tracker);
115f38a9eb1SThomas Graf 
116e252b3d1SWANG Cong 	lwtstate_put(dst->lwtstate);
117e252b3d1SWANG Cong 
118f38a9eb1SThomas Graf 	if (dst->flags & DST_METADATA)
119d71785ffSPaolo Abeni 		metadata_dst_free((struct metadata_dst *)dst);
120f38a9eb1SThomas Graf 	else
1211da177e4SLinus Torvalds 		kmem_cache_free(dst->ops->kmem_cachep, dst);
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	dst = child;
12452df157fSWei Wang 	if (dst)
12552df157fSWei Wang 		dst_release_immediate(dst);
1261da177e4SLinus Torvalds 	return NULL;
1271da177e4SLinus Torvalds }
128598ed936Slaurent chavey EXPORT_SYMBOL(dst_destroy);
1291da177e4SLinus Torvalds 
dst_destroy_rcu(struct rcu_head * head)130f8864972SEric Dumazet static void dst_destroy_rcu(struct rcu_head *head)
131f8864972SEric Dumazet {
132f8864972SEric Dumazet 	struct dst_entry *dst = container_of(head, struct dst_entry, rcu_head);
133f8864972SEric Dumazet 
134f8864972SEric Dumazet 	dst = dst_destroy(dst);
135f8864972SEric Dumazet }
136f8864972SEric Dumazet 
1374a6ce2b6SWei Wang /* Operations to mark dst as DEAD and clean up the net device referenced
1384a6ce2b6SWei Wang  * by dst:
1391be107deSMiaohe Lin  * 1. put the dst under blackhole interface and discard all tx/rx packets
1404a6ce2b6SWei Wang  *    on this route.
1414a6ce2b6SWei Wang  * 2. release the net_device
1424a6ce2b6SWei Wang  * This function should be called when removing routes from the fib tree
1434a6ce2b6SWei Wang  * in preparation for a NETDEV_DOWN/NETDEV_UNREGISTER event and also to
1444a6ce2b6SWei Wang  * make the next dst_ops->check() fail.
1454a6ce2b6SWei Wang  */
dst_dev_put(struct dst_entry * dst)1464a6ce2b6SWei Wang void dst_dev_put(struct dst_entry *dst)
1474a6ce2b6SWei Wang {
1484a6ce2b6SWei Wang 	struct net_device *dev = dst->dev;
1494a6ce2b6SWei Wang 
1504a6ce2b6SWei Wang 	dst->obsolete = DST_OBSOLETE_DEAD;
1514a6ce2b6SWei Wang 	if (dst->ops->ifdown)
15243c28172SZhengchao Shao 		dst->ops->ifdown(dst, dev);
1534a6ce2b6SWei Wang 	dst->input = dst_discard;
1544a6ce2b6SWei Wang 	dst->output = dst_discard_out;
1558d7017fdSMahesh Bandewar 	dst->dev = blackhole_netdev;
156d62607c3SJakub Kicinski 	netdev_ref_replace(dev, blackhole_netdev, &dst->dev_tracker,
1579038c320SEric Dumazet 			   GFP_ATOMIC);
1584a6ce2b6SWei Wang }
1594a6ce2b6SWei Wang EXPORT_SYMBOL(dst_dev_put);
1604a6ce2b6SWei Wang 
dst_count_dec(struct dst_entry * dst)161*eae7435bSEric Dumazet static void dst_count_dec(struct dst_entry *dst)
162*eae7435bSEric Dumazet {
163*eae7435bSEric Dumazet 	if (!(dst->flags & DST_NOCOUNT))
164*eae7435bSEric Dumazet 		dst_entries_add(dst->ops, -1);
165*eae7435bSEric Dumazet }
166*eae7435bSEric Dumazet 
dst_release(struct dst_entry * dst)1678d330868SIlpo Järvinen void dst_release(struct dst_entry *dst)
1688d330868SIlpo Järvinen {
169*eae7435bSEric Dumazet 	if (dst && rcuref_put(&dst->__rcuref)) {
170*eae7435bSEric Dumazet 		dst_count_dec(dst);
171483c26ffSJoel Fernandes (Google) 		call_rcu_hurry(&dst->rcu_head, dst_destroy_rcu);
1728d330868SIlpo Järvinen 	}
173*eae7435bSEric Dumazet }
1748d330868SIlpo Järvinen EXPORT_SYMBOL(dst_release);
1758d330868SIlpo Järvinen 
dst_release_immediate(struct dst_entry * dst)1765f56f409SWei Wang void dst_release_immediate(struct dst_entry *dst)
1775f56f409SWei Wang {
178*eae7435bSEric Dumazet 	if (dst && rcuref_put(&dst->__rcuref)) {
179*eae7435bSEric Dumazet 		dst_count_dec(dst);
1805f56f409SWei Wang 		dst_destroy(dst);
1815f56f409SWei Wang 	}
182*eae7435bSEric Dumazet }
1835f56f409SWei Wang EXPORT_SYMBOL(dst_release_immediate);
1845f56f409SWei Wang 
dst_cow_metrics_generic(struct dst_entry * dst,unsigned long old)18562fa8a84SDavid S. Miller u32 *dst_cow_metrics_generic(struct dst_entry *dst, unsigned long old)
18662fa8a84SDavid S. Miller {
1873fb07dafSEric Dumazet 	struct dst_metrics *p = kmalloc(sizeof(*p), GFP_ATOMIC);
18862fa8a84SDavid S. Miller 
18962fa8a84SDavid S. Miller 	if (p) {
1903fb07dafSEric Dumazet 		struct dst_metrics *old_p = (struct dst_metrics *)__DST_METRICS_PTR(old);
19162fa8a84SDavid S. Miller 		unsigned long prev, new;
19262fa8a84SDavid S. Miller 
1939620fef2SEric Dumazet 		refcount_set(&p->refcnt, 1);
1943fb07dafSEric Dumazet 		memcpy(p->metrics, old_p->metrics, sizeof(p->metrics));
19562fa8a84SDavid S. Miller 
19662fa8a84SDavid S. Miller 		new = (unsigned long) p;
19762fa8a84SDavid S. Miller 		prev = cmpxchg(&dst->_metrics, old, new);
19862fa8a84SDavid S. Miller 
19962fa8a84SDavid S. Miller 		if (prev != old) {
20062fa8a84SDavid S. Miller 			kfree(p);
2013fb07dafSEric Dumazet 			p = (struct dst_metrics *)__DST_METRICS_PTR(prev);
20262fa8a84SDavid S. Miller 			if (prev & DST_METRICS_READ_ONLY)
20362fa8a84SDavid S. Miller 				p = NULL;
2043fb07dafSEric Dumazet 		} else if (prev & DST_METRICS_REFCOUNTED) {
2059620fef2SEric Dumazet 			if (refcount_dec_and_test(&old_p->refcnt))
2063fb07dafSEric Dumazet 				kfree(old_p);
20762fa8a84SDavid S. Miller 		}
20862fa8a84SDavid S. Miller 	}
2093fb07dafSEric Dumazet 	BUILD_BUG_ON(offsetof(struct dst_metrics, metrics) != 0);
2103fb07dafSEric Dumazet 	return (u32 *)p;
21162fa8a84SDavid S. Miller }
21262fa8a84SDavid S. Miller EXPORT_SYMBOL(dst_cow_metrics_generic);
21362fa8a84SDavid S. Miller 
21462fa8a84SDavid S. Miller /* Caller asserts that dst_metrics_read_only(dst) is false.  */
__dst_destroy_metrics_generic(struct dst_entry * dst,unsigned long old)21562fa8a84SDavid S. Miller void __dst_destroy_metrics_generic(struct dst_entry *dst, unsigned long old)
21662fa8a84SDavid S. Miller {
21762fa8a84SDavid S. Miller 	unsigned long prev, new;
21862fa8a84SDavid S. Miller 
2193fb07dafSEric Dumazet 	new = ((unsigned long) &dst_default_metrics) | DST_METRICS_READ_ONLY;
22062fa8a84SDavid S. Miller 	prev = cmpxchg(&dst->_metrics, old, new);
22162fa8a84SDavid S. Miller 	if (prev == old)
22262fa8a84SDavid S. Miller 		kfree(__DST_METRICS_PTR(old));
22362fa8a84SDavid S. Miller }
22462fa8a84SDavid S. Miller EXPORT_SYMBOL(__dst_destroy_metrics_generic);
22562fa8a84SDavid S. Miller 
dst_blackhole_check(struct dst_entry * dst,u32 cookie)226c4c877b2SDaniel Borkmann struct dst_entry *dst_blackhole_check(struct dst_entry *dst, u32 cookie)
227c4c877b2SDaniel Borkmann {
228c4c877b2SDaniel Borkmann 	return NULL;
229c4c877b2SDaniel Borkmann }
230c4c877b2SDaniel Borkmann 
dst_blackhole_cow_metrics(struct dst_entry * dst,unsigned long old)231c4c877b2SDaniel Borkmann u32 *dst_blackhole_cow_metrics(struct dst_entry *dst, unsigned long old)
232c4c877b2SDaniel Borkmann {
233c4c877b2SDaniel Borkmann 	return NULL;
234c4c877b2SDaniel Borkmann }
235c4c877b2SDaniel Borkmann 
dst_blackhole_neigh_lookup(const struct dst_entry * dst,struct sk_buff * skb,const void * daddr)236c4c877b2SDaniel Borkmann struct neighbour *dst_blackhole_neigh_lookup(const struct dst_entry *dst,
237c4c877b2SDaniel Borkmann 					     struct sk_buff *skb,
238c4c877b2SDaniel Borkmann 					     const void *daddr)
239c4c877b2SDaniel Borkmann {
240c4c877b2SDaniel Borkmann 	return NULL;
241c4c877b2SDaniel Borkmann }
242c4c877b2SDaniel Borkmann 
dst_blackhole_update_pmtu(struct dst_entry * dst,struct sock * sk,struct sk_buff * skb,u32 mtu,bool confirm_neigh)243c4c877b2SDaniel Borkmann void dst_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk,
244c4c877b2SDaniel Borkmann 			       struct sk_buff *skb, u32 mtu,
245c4c877b2SDaniel Borkmann 			       bool confirm_neigh)
246c4c877b2SDaniel Borkmann {
247c4c877b2SDaniel Borkmann }
248c4c877b2SDaniel Borkmann EXPORT_SYMBOL_GPL(dst_blackhole_update_pmtu);
249c4c877b2SDaniel Borkmann 
dst_blackhole_redirect(struct dst_entry * dst,struct sock * sk,struct sk_buff * skb)250c4c877b2SDaniel Borkmann void dst_blackhole_redirect(struct dst_entry *dst, struct sock *sk,
251c4c877b2SDaniel Borkmann 			    struct sk_buff *skb)
252c4c877b2SDaniel Borkmann {
253c4c877b2SDaniel Borkmann }
254c4c877b2SDaniel Borkmann EXPORT_SYMBOL_GPL(dst_blackhole_redirect);
255c4c877b2SDaniel Borkmann 
dst_blackhole_mtu(const struct dst_entry * dst)256c4c877b2SDaniel Borkmann unsigned int dst_blackhole_mtu(const struct dst_entry *dst)
257c4c877b2SDaniel Borkmann {
258c4c877b2SDaniel Borkmann 	unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
259c4c877b2SDaniel Borkmann 
260c4c877b2SDaniel Borkmann 	return mtu ? : dst->dev->mtu;
261c4c877b2SDaniel Borkmann }
262c4c877b2SDaniel Borkmann EXPORT_SYMBOL_GPL(dst_blackhole_mtu);
263c4c877b2SDaniel Borkmann 
264a188bb56SDaniel Borkmann static struct dst_ops dst_blackhole_ops = {
265f38a9eb1SThomas Graf 	.family		= AF_UNSPEC,
266a188bb56SDaniel Borkmann 	.neigh_lookup	= dst_blackhole_neigh_lookup,
267a188bb56SDaniel Borkmann 	.check		= dst_blackhole_check,
268a188bb56SDaniel Borkmann 	.cow_metrics	= dst_blackhole_cow_metrics,
269a188bb56SDaniel Borkmann 	.update_pmtu	= dst_blackhole_update_pmtu,
270a188bb56SDaniel Borkmann 	.redirect	= dst_blackhole_redirect,
271a188bb56SDaniel Borkmann 	.mtu		= dst_blackhole_mtu,
272f38a9eb1SThomas Graf };
273f38a9eb1SThomas Graf 
__metadata_dst_init(struct metadata_dst * md_dst,enum metadata_type type,u8 optslen)2743fcece12SJakub Kicinski static void __metadata_dst_init(struct metadata_dst *md_dst,
2753fcece12SJakub Kicinski 				enum metadata_type type, u8 optslen)
276f38a9eb1SThomas Graf {
277f38a9eb1SThomas Graf 	struct dst_entry *dst;
278f38a9eb1SThomas Graf 
279f38a9eb1SThomas Graf 	dst = &md_dst->dst;
280a188bb56SDaniel Borkmann 	dst_init(dst, &dst_blackhole_ops, NULL, 1, DST_OBSOLETE_NONE,
281a4c2fd7fSWei Wang 		 DST_METADATA | DST_NOCOUNT);
282f38a9eb1SThomas Graf 	memset(dst + 1, 0, sizeof(*md_dst) + optslen - sizeof(*dst));
2833fcece12SJakub Kicinski 	md_dst->type = type;
284d3aa45ceSAlexei Starovoitov }
285d3aa45ceSAlexei Starovoitov 
metadata_dst_alloc(u8 optslen,enum metadata_type type,gfp_t flags)2863fcece12SJakub Kicinski struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
2873fcece12SJakub Kicinski 					gfp_t flags)
288d3aa45ceSAlexei Starovoitov {
289d3aa45ceSAlexei Starovoitov 	struct metadata_dst *md_dst;
290d3aa45ceSAlexei Starovoitov 
291d3aa45ceSAlexei Starovoitov 	md_dst = kmalloc(sizeof(*md_dst) + optslen, flags);
292d3aa45ceSAlexei Starovoitov 	if (!md_dst)
293d3aa45ceSAlexei Starovoitov 		return NULL;
294d3aa45ceSAlexei Starovoitov 
2953fcece12SJakub Kicinski 	__metadata_dst_init(md_dst, type, optslen);
296f38a9eb1SThomas Graf 
297f38a9eb1SThomas Graf 	return md_dst;
298f38a9eb1SThomas Graf }
299f38a9eb1SThomas Graf EXPORT_SYMBOL_GPL(metadata_dst_alloc);
300f38a9eb1SThomas Graf 
metadata_dst_free(struct metadata_dst * md_dst)301d71785ffSPaolo Abeni void metadata_dst_free(struct metadata_dst *md_dst)
302d71785ffSPaolo Abeni {
303d71785ffSPaolo Abeni #ifdef CONFIG_DST_CACHE
304e65a4955SDavid Lamparter 	if (md_dst->type == METADATA_IP_TUNNEL)
305d71785ffSPaolo Abeni 		dst_cache_destroy(&md_dst->u.tun_info.dst_cache);
306d71785ffSPaolo Abeni #endif
30794151f5aSEyal Birger 	if (md_dst->type == METADATA_XFRM)
30894151f5aSEyal Birger 		dst_release(md_dst->u.xfrm_info.dst_orig);
309d71785ffSPaolo Abeni 	kfree(md_dst);
310d71785ffSPaolo Abeni }
311af308b94SPablo Neira Ayuso EXPORT_SYMBOL_GPL(metadata_dst_free);
312d71785ffSPaolo Abeni 
3133fcece12SJakub Kicinski struct metadata_dst __percpu *
metadata_dst_alloc_percpu(u8 optslen,enum metadata_type type,gfp_t flags)3143fcece12SJakub Kicinski metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags)
315d3aa45ceSAlexei Starovoitov {
316d3aa45ceSAlexei Starovoitov 	int cpu;
317d3aa45ceSAlexei Starovoitov 	struct metadata_dst __percpu *md_dst;
318d3aa45ceSAlexei Starovoitov 
319d3aa45ceSAlexei Starovoitov 	md_dst = __alloc_percpu_gfp(sizeof(struct metadata_dst) + optslen,
320d3aa45ceSAlexei Starovoitov 				    __alignof__(struct metadata_dst), flags);
321d3aa45ceSAlexei Starovoitov 	if (!md_dst)
322d3aa45ceSAlexei Starovoitov 		return NULL;
323d3aa45ceSAlexei Starovoitov 
324d3aa45ceSAlexei Starovoitov 	for_each_possible_cpu(cpu)
3253fcece12SJakub Kicinski 		__metadata_dst_init(per_cpu_ptr(md_dst, cpu), type, optslen);
326d3aa45ceSAlexei Starovoitov 
327d3aa45ceSAlexei Starovoitov 	return md_dst;
328d3aa45ceSAlexei Starovoitov }
329d3aa45ceSAlexei Starovoitov EXPORT_SYMBOL_GPL(metadata_dst_alloc_percpu);
330d66f2b91SJakub Kicinski 
metadata_dst_free_percpu(struct metadata_dst __percpu * md_dst)331d66f2b91SJakub Kicinski void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst)
332d66f2b91SJakub Kicinski {
333d66f2b91SJakub Kicinski 	int cpu;
334d66f2b91SJakub Kicinski 
335d66f2b91SJakub Kicinski 	for_each_possible_cpu(cpu) {
336d66f2b91SJakub Kicinski 		struct metadata_dst *one_md_dst = per_cpu_ptr(md_dst, cpu);
337d66f2b91SJakub Kicinski 
33894151f5aSEyal Birger #ifdef CONFIG_DST_CACHE
339d66f2b91SJakub Kicinski 		if (one_md_dst->type == METADATA_IP_TUNNEL)
340d66f2b91SJakub Kicinski 			dst_cache_destroy(&one_md_dst->u.tun_info.dst_cache);
341d66f2b91SJakub Kicinski #endif
34294151f5aSEyal Birger 		if (one_md_dst->type == METADATA_XFRM)
34394151f5aSEyal Birger 			dst_release(one_md_dst->u.xfrm_info.dst_orig);
34494151f5aSEyal Birger 	}
345d66f2b91SJakub Kicinski 	free_percpu(md_dst);
346d66f2b91SJakub Kicinski }
347d66f2b91SJakub Kicinski EXPORT_SYMBOL_GPL(metadata_dst_free_percpu);
348