2 * net/core/dst_cache.c - dst entry cache
4 * Copyright (c) 2016 Paolo Abeni <pabeni@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/percpu.h>
14 #include <net/dst_cache.h>
15 #include <net/route.h>
16 #if IS_ENABLED(CONFIG_IPV6)
17 #include <net/ip6_fib.h>
19 #include <uapi/linux/in.h>
21 #ifndef HAVE_METADATA_DST
22 struct dst_cache_pcpu {
23 unsigned long refresh_ts;
24 struct dst_entry *dst;
27 struct in_addr in_saddr;
28 struct in6_addr in6_saddr;
32 static void dst_cache_per_cpu_dst_set(struct dst_cache_pcpu *dst_cache,
33 struct dst_entry *dst, u32 cookie)
35 dst_release(dst_cache->dst);
39 dst_cache->cookie = cookie;
43 static struct dst_entry *dst_cache_per_cpu_get(struct dst_cache *dst_cache,
44 struct dst_cache_pcpu *idst)
46 struct dst_entry *dst;
52 /* the cache already hold a dst reference; it can't go away */
55 if (unlikely(!time_after(idst->refresh_ts, dst_cache->reset_ts) ||
56 (dst->obsolete && !dst->ops->check(dst, idst->cookie)))) {
57 dst_cache_per_cpu_dst_set(idst, NULL, 0);
64 idst->refresh_ts = jiffies;
68 struct dst_entry *rpl_dst_cache_get(struct dst_cache *dst_cache)
70 if (!dst_cache->cache)
73 return dst_cache_per_cpu_get(dst_cache, this_cpu_ptr(dst_cache->cache));
75 EXPORT_SYMBOL_GPL(rpl_dst_cache_get);
77 struct rtable *rpl_dst_cache_get_ip4(struct dst_cache *dst_cache, __be32 *saddr)
79 struct dst_cache_pcpu *idst;
80 struct dst_entry *dst;
82 if (!dst_cache->cache)
85 idst = this_cpu_ptr(dst_cache->cache);
86 dst = dst_cache_per_cpu_get(dst_cache, idst);
90 *saddr = idst->in_saddr.s_addr;
91 return container_of(dst, struct rtable, dst);
93 EXPORT_SYMBOL_GPL(rpl_dst_cache_get_ip4);
95 void rpl_dst_cache_set_ip4(struct dst_cache *dst_cache, struct dst_entry *dst,
98 struct dst_cache_pcpu *idst;
100 if (!dst_cache->cache)
103 idst = this_cpu_ptr(dst_cache->cache);
104 dst_cache_per_cpu_dst_set(idst, dst, 0);
105 idst->in_saddr.s_addr = saddr;
107 EXPORT_SYMBOL_GPL(rpl_dst_cache_set_ip4);
109 #if IS_ENABLED(CONFIG_IPV6)
110 void rpl_dst_cache_set_ip6(struct dst_cache *dst_cache, struct dst_entry *dst,
111 const struct in6_addr *addr)
113 struct dst_cache_pcpu *idst;
115 if (!dst_cache->cache)
118 idst = this_cpu_ptr(dst_cache->cache);
119 dst_cache_per_cpu_dst_set(this_cpu_ptr(dst_cache->cache), dst,
120 rt6_get_cookie((struct rt6_info *)dst));
121 idst->in6_saddr = *addr;
123 EXPORT_SYMBOL_GPL(rpl_dst_cache_set_ip6);
125 struct dst_entry *rpl_dst_cache_get_ip6(struct dst_cache *dst_cache,
126 struct in6_addr *saddr)
128 struct dst_cache_pcpu *idst;
129 struct dst_entry *dst;
131 if (!dst_cache->cache)
134 idst = this_cpu_ptr(dst_cache->cache);
135 dst = dst_cache_per_cpu_get(dst_cache, idst);
139 *saddr = idst->in6_saddr;
142 EXPORT_SYMBOL_GPL(rpl_dst_cache_get_ip6);
146 int rpl_dst_cache_init(struct dst_cache *dst_cache, gfp_t gfp)
148 dst_cache->cache = alloc_percpu_gfp(struct dst_cache_pcpu,
150 if (!dst_cache->cache)
153 dst_cache_reset(dst_cache);
156 EXPORT_SYMBOL_GPL(rpl_dst_cache_init);
158 void rpl_dst_cache_destroy(struct dst_cache *dst_cache)
162 if (!dst_cache->cache)
165 for_each_possible_cpu(i)
166 dst_release(per_cpu_ptr(dst_cache->cache, i)->dst);
168 free_percpu(dst_cache->cache);
170 EXPORT_SYMBOL_GPL(rpl_dst_cache_destroy);
171 #endif /*HAVE_METADATA_DST */