datapath: backport: net: add dst_cache support
[cascardo/ovs.git] / datapath / linux / compat / dst_cache.c
1 /*
2  * net/core/dst_cache.c - dst entry cache
3  *
4  * Copyright (c) 2016 Paolo Abeni <pabeni@redhat.com>
5  *
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.
10  */
11
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>
18 #endif
19 #include <uapi/linux/in.h>
20
21 #ifndef HAVE_METADATA_DST
22 struct dst_cache_pcpu {
23         unsigned long refresh_ts;
24         struct dst_entry *dst;
25         u32 cookie;
26         union {
27                 struct in_addr in_saddr;
28                 struct in6_addr in6_saddr;
29         };
30 };
31
32 static void dst_cache_per_cpu_dst_set(struct dst_cache_pcpu *dst_cache,
33                                       struct dst_entry *dst, u32 cookie)
34 {
35         dst_release(dst_cache->dst);
36         if (dst)
37                 dst_hold(dst);
38
39         dst_cache->cookie = cookie;
40         dst_cache->dst = dst;
41 }
42
43 static struct dst_entry *dst_cache_per_cpu_get(struct dst_cache *dst_cache,
44                                                struct dst_cache_pcpu *idst)
45 {
46         struct dst_entry *dst;
47
48         dst = idst->dst;
49         if (!dst)
50                 goto fail;
51
52         /* the cache already hold a dst reference; it can't go away */
53         dst_hold(dst);
54
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);
58                 dst_release(dst);
59                 goto fail;
60         }
61         return dst;
62
63 fail:
64         idst->refresh_ts = jiffies;
65         return NULL;
66 }
67
68 struct dst_entry *rpl_dst_cache_get(struct dst_cache *dst_cache)
69 {
70         if (!dst_cache->cache)
71                 return NULL;
72
73         return dst_cache_per_cpu_get(dst_cache, this_cpu_ptr(dst_cache->cache));
74 }
75 EXPORT_SYMBOL_GPL(rpl_dst_cache_get);
76
77 struct rtable *rpl_dst_cache_get_ip4(struct dst_cache *dst_cache, __be32 *saddr)
78 {
79         struct dst_cache_pcpu *idst;
80         struct dst_entry *dst;
81
82         if (!dst_cache->cache)
83                 return NULL;
84
85         idst = this_cpu_ptr(dst_cache->cache);
86         dst = dst_cache_per_cpu_get(dst_cache, idst);
87         if (!dst)
88                 return NULL;
89
90         *saddr = idst->in_saddr.s_addr;
91         return container_of(dst, struct rtable, dst);
92 }
93 EXPORT_SYMBOL_GPL(rpl_dst_cache_get_ip4);
94
95 void rpl_dst_cache_set_ip4(struct dst_cache *dst_cache, struct dst_entry *dst,
96                        __be32 saddr)
97 {
98         struct dst_cache_pcpu *idst;
99
100         if (!dst_cache->cache)
101                 return;
102
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;
106 }
107 EXPORT_SYMBOL_GPL(rpl_dst_cache_set_ip4);
108
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)
112 {
113         struct dst_cache_pcpu *idst;
114
115         if (!dst_cache->cache)
116                 return;
117
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;
122 }
123 EXPORT_SYMBOL_GPL(rpl_dst_cache_set_ip6);
124
125 struct dst_entry *rpl_dst_cache_get_ip6(struct dst_cache *dst_cache,
126                                     struct in6_addr *saddr)
127 {
128         struct dst_cache_pcpu *idst;
129         struct dst_entry *dst;
130
131         if (!dst_cache->cache)
132                 return NULL;
133
134         idst = this_cpu_ptr(dst_cache->cache);
135         dst = dst_cache_per_cpu_get(dst_cache, idst);
136         if (!dst)
137                 return NULL;
138
139         *saddr = idst->in6_saddr;
140         return dst;
141 }
142 EXPORT_SYMBOL_GPL(rpl_dst_cache_get_ip6);
143
144 #endif
145
146 int rpl_dst_cache_init(struct dst_cache *dst_cache, gfp_t gfp)
147 {
148         dst_cache->cache = alloc_percpu_gfp(struct dst_cache_pcpu,
149                                             gfp | __GFP_ZERO);
150         if (!dst_cache->cache)
151                 return -ENOMEM;
152
153         dst_cache_reset(dst_cache);
154         return 0;
155 }
156 EXPORT_SYMBOL_GPL(rpl_dst_cache_init);
157
158 void rpl_dst_cache_destroy(struct dst_cache *dst_cache)
159 {
160         int i;
161
162         if (!dst_cache->cache)
163                 return;
164
165         for_each_possible_cpu(i)
166                 dst_release(per_cpu_ptr(dst_cache->cache, i)->dst);
167
168         free_percpu(dst_cache->cache);
169 }
170 EXPORT_SYMBOL_GPL(rpl_dst_cache_destroy);
171 #endif /*HAVE_METADATA_DST */