blob: 5bef3513af77afc8943d99a107f74155b9979aaa [file] [log] [blame]
David S. Miller51c5d0c2012-07-10 00:49:14 -07001#include <linux/rcupdate.h>
2#include <linux/spinlock.h>
3#include <linux/jiffies.h>
David S. Millerab92bb22012-07-09 16:19:30 -07004#include <linux/module.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -07005#include <linux/cache.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -07006#include <linux/slab.h>
7#include <linux/init.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -07008#include <linux/tcp.h>
Eric Dumazet5815d5e2012-07-19 23:02:34 +00009#include <linux/hash.h>
Julian Anastasovd23ff702012-09-04 11:03:15 +000010#include <linux/tcp_metrics.h>
Eric Dumazet976a7022012-11-16 05:31:53 +000011#include <linux/vmalloc.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -070012
13#include <net/inet_connection_sock.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -070014#include <net/net_namespace.h>
David S. Millerab92bb22012-07-09 16:19:30 -070015#include <net/request_sock.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -070016#include <net/inetpeer.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -070017#include <net/sock.h>
David S. Miller51c5d0c2012-07-10 00:49:14 -070018#include <net/ipv6.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -070019#include <net/dst.h>
20#include <net/tcp.h>
Julian Anastasovd23ff702012-09-04 11:03:15 +000021#include <net/genetlink.h>
David S. Miller4aabd8e2012-07-09 16:07:30 -070022
23int sysctl_tcp_nometrics_save __read_mostly;
24
David S. Miller41804422014-01-18 00:55:41 -080025static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
26 const struct inetpeer_addr *daddr,
Christoph Paasch77f99ad2014-01-16 20:01:21 +010027 struct net *net, unsigned int hash);
28
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000029struct tcp_fastopen_metrics {
30 u16 mss;
Yuchung Chengaab48742012-07-19 06:43:10 +000031 u16 syn_loss:10; /* Recurring Fast Open SYN losses */
32 unsigned long last_syn_loss; /* Last Fast Open SYN loss */
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000033 struct tcp_fastopen_cookie cookie;
34};
35
Eric Dumazet740b0f12014-02-26 14:02:48 -080036/* TCP_METRIC_MAX includes 2 extra fields for userspace compatibility
37 * Kernel only stores RTT and RTTVAR in usec resolution
38 */
39#define TCP_METRIC_MAX_KERNEL (TCP_METRIC_MAX - 2)
40
David S. Miller51c5d0c2012-07-10 00:49:14 -070041struct tcp_metrics_block {
42 struct tcp_metrics_block __rcu *tcpm_next;
Eric W. Biederman849e8a02015-03-13 00:05:52 -050043 possible_net_t tcpm_net;
Christoph Paascha5443022014-01-08 16:05:56 +010044 struct inetpeer_addr tcpm_saddr;
Christoph Paasch324fd552014-01-08 16:05:55 +010045 struct inetpeer_addr tcpm_daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -070046 unsigned long tcpm_stamp;
David S. Miller81166dd2012-07-10 03:14:24 -070047 u32 tcpm_ts;
48 u32 tcpm_ts_stamp;
David S. Miller51c5d0c2012-07-10 00:49:14 -070049 u32 tcpm_lock;
Eric Dumazet740b0f12014-02-26 14:02:48 -080050 u32 tcpm_vals[TCP_METRIC_MAX_KERNEL + 1];
Yuchung Cheng1fe4c482012-07-19 06:43:06 +000051 struct tcp_fastopen_metrics tcpm_fastopen;
Julian Anastasovd23ff702012-09-04 11:03:15 +000052
53 struct rcu_head rcu_head;
David S. Miller51c5d0c2012-07-10 00:49:14 -070054};
55
Eric W. Biederman849e8a02015-03-13 00:05:52 -050056static inline struct net *tm_net(struct tcp_metrics_block *tm)
57{
58 return read_pnet(&tm->tcpm_net);
59}
60
David S. Miller51c5d0c2012-07-10 00:49:14 -070061static bool tcp_metric_locked(struct tcp_metrics_block *tm,
62 enum tcp_metric_index idx)
63{
64 return tm->tcpm_lock & (1 << idx);
65}
66
67static u32 tcp_metric_get(struct tcp_metrics_block *tm,
68 enum tcp_metric_index idx)
69{
70 return tm->tcpm_vals[idx];
71}
72
David S. Miller51c5d0c2012-07-10 00:49:14 -070073static void tcp_metric_set(struct tcp_metrics_block *tm,
74 enum tcp_metric_index idx,
75 u32 val)
76{
77 tm->tcpm_vals[idx] = val;
78}
79
David S. Miller51c5d0c2012-07-10 00:49:14 -070080static bool addr_same(const struct inetpeer_addr *a,
81 const struct inetpeer_addr *b)
82{
83 const struct in6_addr *a6, *b6;
84
85 if (a->family != b->family)
86 return false;
87 if (a->family == AF_INET)
88 return a->addr.a4 == b->addr.a4;
89
90 a6 = (const struct in6_addr *) &a->addr.a6[0];
91 b6 = (const struct in6_addr *) &b->addr.a6[0];
92
93 return ipv6_addr_equal(a6, b6);
94}
95
96struct tcpm_hash_bucket {
97 struct tcp_metrics_block __rcu *chain;
98};
99
Eric W. Biederman098a6972015-03-13 00:07:44 -0500100static struct tcpm_hash_bucket *tcp_metrics_hash __read_mostly;
101static unsigned int tcp_metrics_hash_log __read_mostly;
102
David S. Miller51c5d0c2012-07-10 00:49:14 -0700103static DEFINE_SPINLOCK(tcp_metrics_lock);
104
Eric Dumazet740b0f12014-02-26 14:02:48 -0800105static void tcpm_suck_dst(struct tcp_metrics_block *tm,
106 const struct dst_entry *dst,
Eric Dumazetefeaa552013-05-03 19:12:45 +0000107 bool fastopen_clear)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700108{
Eric Dumazet740b0f12014-02-26 14:02:48 -0800109 u32 msval;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700110 u32 val;
111
Julian Anastasov9a0a9502012-07-23 10:46:38 +0300112 tm->tcpm_stamp = jiffies;
113
David S. Miller51c5d0c2012-07-10 00:49:14 -0700114 val = 0;
115 if (dst_metric_locked(dst, RTAX_RTT))
116 val |= 1 << TCP_METRIC_RTT;
117 if (dst_metric_locked(dst, RTAX_RTTVAR))
118 val |= 1 << TCP_METRIC_RTTVAR;
119 if (dst_metric_locked(dst, RTAX_SSTHRESH))
120 val |= 1 << TCP_METRIC_SSTHRESH;
121 if (dst_metric_locked(dst, RTAX_CWND))
122 val |= 1 << TCP_METRIC_CWND;
123 if (dst_metric_locked(dst, RTAX_REORDERING))
124 val |= 1 << TCP_METRIC_REORDERING;
125 tm->tcpm_lock = val;
126
Eric Dumazet740b0f12014-02-26 14:02:48 -0800127 msval = dst_metric_raw(dst, RTAX_RTT);
128 tm->tcpm_vals[TCP_METRIC_RTT] = msval * USEC_PER_MSEC;
129
130 msval = dst_metric_raw(dst, RTAX_RTTVAR);
131 tm->tcpm_vals[TCP_METRIC_RTTVAR] = msval * USEC_PER_MSEC;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700132 tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
133 tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
134 tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
David S. Miller81166dd2012-07-10 03:14:24 -0700135 tm->tcpm_ts = 0;
136 tm->tcpm_ts_stamp = 0;
Eric Dumazetefeaa552013-05-03 19:12:45 +0000137 if (fastopen_clear) {
138 tm->tcpm_fastopen.mss = 0;
139 tm->tcpm_fastopen.syn_loss = 0;
140 tm->tcpm_fastopen.cookie.len = 0;
141 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700142}
143
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100144#define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
145
146static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
147{
148 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
149 tcpm_suck_dst(tm, dst, false);
150}
151
152#define TCP_METRICS_RECLAIM_DEPTH 5
153#define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
154
Eric Dumazet9f1ab182015-03-16 07:14:34 -0700155#define deref_locked(p) \
156 rcu_dereference_protected(p, lockdep_is_held(&tcp_metrics_lock))
157
David S. Miller51c5d0c2012-07-10 00:49:14 -0700158static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
Christoph Paascha5443022014-01-08 16:05:56 +0100159 struct inetpeer_addr *saddr,
Christoph Paasch324fd552014-01-08 16:05:55 +0100160 struct inetpeer_addr *daddr,
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100161 unsigned int hash)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700162{
163 struct tcp_metrics_block *tm;
164 struct net *net;
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100165 bool reclaim = false;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700166
167 spin_lock_bh(&tcp_metrics_lock);
168 net = dev_net(dst->dev);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100169
170 /* While waiting for the spin-lock the cache might have been populated
171 * with this entry and so we have to check again.
172 */
David S. Miller41804422014-01-18 00:55:41 -0800173 tm = __tcp_get_metrics(saddr, daddr, net, hash);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100174 if (tm == TCP_METRICS_RECLAIM_PTR) {
175 reclaim = true;
176 tm = NULL;
177 }
178 if (tm) {
179 tcpm_check_stamp(tm, dst);
180 goto out_unlock;
181 }
182
David S. Miller51c5d0c2012-07-10 00:49:14 -0700183 if (unlikely(reclaim)) {
184 struct tcp_metrics_block *oldest;
185
Eric Dumazet9f1ab182015-03-16 07:14:34 -0700186 oldest = deref_locked(tcp_metrics_hash[hash].chain);
187 for (tm = deref_locked(oldest->tcpm_next); tm;
188 tm = deref_locked(tm->tcpm_next)) {
David S. Miller51c5d0c2012-07-10 00:49:14 -0700189 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
190 oldest = tm;
191 }
192 tm = oldest;
193 } else {
194 tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
195 if (!tm)
196 goto out_unlock;
197 }
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500198 write_pnet(&tm->tcpm_net, net);
Christoph Paascha5443022014-01-08 16:05:56 +0100199 tm->tcpm_saddr = *saddr;
Christoph Paasch324fd552014-01-08 16:05:55 +0100200 tm->tcpm_daddr = *daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700201
Eric Dumazetefeaa552013-05-03 19:12:45 +0000202 tcpm_suck_dst(tm, dst, true);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700203
204 if (likely(!reclaim)) {
Eric W. Biederman098a6972015-03-13 00:07:44 -0500205 tm->tcpm_next = tcp_metrics_hash[hash].chain;
206 rcu_assign_pointer(tcp_metrics_hash[hash].chain, tm);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700207 }
208
209out_unlock:
210 spin_unlock_bh(&tcp_metrics_lock);
211 return tm;
212}
213
David S. Miller51c5d0c2012-07-10 00:49:14 -0700214static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
215{
216 if (tm)
217 return tm;
218 if (depth > TCP_METRICS_RECLAIM_DEPTH)
219 return TCP_METRICS_RECLAIM_PTR;
220 return NULL;
221}
222
Christoph Paascha5443022014-01-08 16:05:56 +0100223static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
224 const struct inetpeer_addr *daddr,
David S. Miller51c5d0c2012-07-10 00:49:14 -0700225 struct net *net, unsigned int hash)
226{
227 struct tcp_metrics_block *tm;
228 int depth = 0;
229
Eric W. Biederman098a6972015-03-13 00:07:44 -0500230 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700231 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100232 if (addr_same(&tm->tcpm_saddr, saddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500233 addr_same(&tm->tcpm_daddr, daddr) &&
234 net_eq(tm_net(tm), net))
David S. Miller51c5d0c2012-07-10 00:49:14 -0700235 break;
236 depth++;
237 }
238 return tcp_get_encode(tm, depth);
239}
240
241static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
242 struct dst_entry *dst)
243{
244 struct tcp_metrics_block *tm;
Christoph Paascha5443022014-01-08 16:05:56 +0100245 struct inetpeer_addr saddr, daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700246 unsigned int hash;
247 struct net *net;
248
Christoph Paascha5443022014-01-08 16:05:56 +0100249 saddr.family = req->rsk_ops->family;
Christoph Paasch324fd552014-01-08 16:05:55 +0100250 daddr.family = req->rsk_ops->family;
251 switch (daddr.family) {
David S. Miller51c5d0c2012-07-10 00:49:14 -0700252 case AF_INET:
Christoph Paascha5443022014-01-08 16:05:56 +0100253 saddr.addr.a4 = inet_rsk(req)->ir_loc_addr;
Christoph Paasch324fd552014-01-08 16:05:55 +0100254 daddr.addr.a4 = inet_rsk(req)->ir_rmt_addr;
255 hash = (__force unsigned int) daddr.addr.a4;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700256 break;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700257#if IS_ENABLED(CONFIG_IPV6)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700258 case AF_INET6:
Christoph Paascha5443022014-01-08 16:05:56 +0100259 *(struct in6_addr *)saddr.addr.a6 = inet_rsk(req)->ir_v6_loc_addr;
Christoph Paasch324fd552014-01-08 16:05:55 +0100260 *(struct in6_addr *)daddr.addr.a6 = inet_rsk(req)->ir_v6_rmt_addr;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700261 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700262 break;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700263#endif
David S. Miller51c5d0c2012-07-10 00:49:14 -0700264 default:
265 return NULL;
266 }
267
David S. Miller51c5d0c2012-07-10 00:49:14 -0700268 net = dev_net(dst->dev);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500269 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500270 hash = hash_32(hash, tcp_metrics_hash_log);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700271
Eric W. Biederman098a6972015-03-13 00:07:44 -0500272 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700273 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100274 if (addr_same(&tm->tcpm_saddr, &saddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500275 addr_same(&tm->tcpm_daddr, &daddr) &&
276 net_eq(tm_net(tm), net))
David S. Miller51c5d0c2012-07-10 00:49:14 -0700277 break;
278 }
279 tcpm_check_stamp(tm, dst);
280 return tm;
281}
282
David S. Miller81166dd2012-07-10 03:14:24 -0700283static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
284{
David S. Miller81166dd2012-07-10 03:14:24 -0700285 struct tcp_metrics_block *tm;
Christoph Paascha5443022014-01-08 16:05:56 +0100286 struct inetpeer_addr saddr, daddr;
David S. Miller81166dd2012-07-10 03:14:24 -0700287 unsigned int hash;
288 struct net *net;
289
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100290 if (tw->tw_family == AF_INET) {
291 saddr.family = AF_INET;
Christoph Paascha5443022014-01-08 16:05:56 +0100292 saddr.addr.a4 = tw->tw_rcv_saddr;
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100293 daddr.family = AF_INET;
Christoph Paasch324fd552014-01-08 16:05:55 +0100294 daddr.addr.a4 = tw->tw_daddr;
295 hash = (__force unsigned int) daddr.addr.a4;
David S. Miller81166dd2012-07-10 03:14:24 -0700296 }
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100297#if IS_ENABLED(CONFIG_IPV6)
298 else if (tw->tw_family == AF_INET6) {
299 if (ipv6_addr_v4mapped(&tw->tw_v6_daddr)) {
300 saddr.family = AF_INET;
301 saddr.addr.a4 = tw->tw_rcv_saddr;
302 daddr.family = AF_INET;
303 daddr.addr.a4 = tw->tw_daddr;
304 hash = (__force unsigned int) daddr.addr.a4;
305 } else {
306 saddr.family = AF_INET6;
307 *(struct in6_addr *)saddr.addr.a6 = tw->tw_v6_rcv_saddr;
308 daddr.family = AF_INET6;
309 *(struct in6_addr *)daddr.addr.a6 = tw->tw_v6_daddr;
310 hash = ipv6_addr_hash(&tw->tw_v6_daddr);
311 }
312 }
313#endif
314 else
315 return NULL;
David S. Miller81166dd2012-07-10 03:14:24 -0700316
David S. Miller81166dd2012-07-10 03:14:24 -0700317 net = twsk_net(tw);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500318 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500319 hash = hash_32(hash, tcp_metrics_hash_log);
David S. Miller81166dd2012-07-10 03:14:24 -0700320
Eric W. Biederman098a6972015-03-13 00:07:44 -0500321 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
David S. Miller81166dd2012-07-10 03:14:24 -0700322 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paascha5443022014-01-08 16:05:56 +0100323 if (addr_same(&tm->tcpm_saddr, &saddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500324 addr_same(&tm->tcpm_daddr, &daddr) &&
325 net_eq(tm_net(tm), net))
David S. Miller81166dd2012-07-10 03:14:24 -0700326 break;
327 }
328 return tm;
329}
330
David S. Miller51c5d0c2012-07-10 00:49:14 -0700331static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
332 struct dst_entry *dst,
333 bool create)
334{
335 struct tcp_metrics_block *tm;
Christoph Paascha5443022014-01-08 16:05:56 +0100336 struct inetpeer_addr saddr, daddr;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700337 unsigned int hash;
338 struct net *net;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700339
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100340 if (sk->sk_family == AF_INET) {
341 saddr.family = AF_INET;
Christoph Paascha5443022014-01-08 16:05:56 +0100342 saddr.addr.a4 = inet_sk(sk)->inet_saddr;
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100343 daddr.family = AF_INET;
Christoph Paasch324fd552014-01-08 16:05:55 +0100344 daddr.addr.a4 = inet_sk(sk)->inet_daddr;
345 hash = (__force unsigned int) daddr.addr.a4;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700346 }
Christoph Paasch3ad88cf2014-01-22 13:58:44 +0100347#if IS_ENABLED(CONFIG_IPV6)
348 else if (sk->sk_family == AF_INET6) {
349 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
350 saddr.family = AF_INET;
351 saddr.addr.a4 = inet_sk(sk)->inet_saddr;
352 daddr.family = AF_INET;
353 daddr.addr.a4 = inet_sk(sk)->inet_daddr;
354 hash = (__force unsigned int) daddr.addr.a4;
355 } else {
356 saddr.family = AF_INET6;
357 *(struct in6_addr *)saddr.addr.a6 = sk->sk_v6_rcv_saddr;
358 daddr.family = AF_INET6;
359 *(struct in6_addr *)daddr.addr.a6 = sk->sk_v6_daddr;
360 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
361 }
362 }
363#endif
364 else
365 return NULL;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700366
David S. Miller51c5d0c2012-07-10 00:49:14 -0700367 net = dev_net(dst->dev);
Eric W. Biederman3e5da622015-03-13 00:05:24 -0500368 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500369 hash = hash_32(hash, tcp_metrics_hash_log);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700370
Christoph Paascha5443022014-01-08 16:05:56 +0100371 tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
Christoph Paasch77f99ad2014-01-16 20:01:21 +0100372 if (tm == TCP_METRICS_RECLAIM_PTR)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700373 tm = NULL;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700374 if (!tm && create)
David S. Miller41804422014-01-18 00:55:41 -0800375 tm = tcpm_new(dst, &saddr, &daddr, hash);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700376 else
377 tcpm_check_stamp(tm, dst);
378
379 return tm;
380}
381
David S. Miller4aabd8e2012-07-09 16:07:30 -0700382/* Save metrics learned by this TCP session. This function is called
383 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
384 * or goes from LAST-ACK to CLOSE.
385 */
386void tcp_update_metrics(struct sock *sk)
387{
David S. Miller51c5d0c2012-07-10 00:49:14 -0700388 const struct inet_connection_sock *icsk = inet_csk(sk);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700389 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700390 struct tcp_sock *tp = tcp_sk(sk);
391 struct tcp_metrics_block *tm;
392 unsigned long rtt;
393 u32 val;
394 int m;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700395
David S. Miller51c5d0c2012-07-10 00:49:14 -0700396 if (sysctl_tcp_nometrics_save || !dst)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700397 return;
398
David S. Miller51c5d0c2012-07-10 00:49:14 -0700399 if (dst->flags & DST_HOST)
David S. Miller4aabd8e2012-07-09 16:07:30 -0700400 dst_confirm(dst);
401
David S. Miller51c5d0c2012-07-10 00:49:14 -0700402 rcu_read_lock();
Eric Dumazet740b0f12014-02-26 14:02:48 -0800403 if (icsk->icsk_backoff || !tp->srtt_us) {
David S. Miller51c5d0c2012-07-10 00:49:14 -0700404 /* This session failed to estimate rtt. Why?
405 * Probably, no packets returned in time. Reset our
406 * results.
David S. Miller4aabd8e2012-07-09 16:07:30 -0700407 */
David S. Miller51c5d0c2012-07-10 00:49:14 -0700408 tm = tcp_get_metrics(sk, dst, false);
409 if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
410 tcp_metric_set(tm, TCP_METRIC_RTT, 0);
411 goto out_unlock;
412 } else
413 tm = tcp_get_metrics(sk, dst, true);
414
415 if (!tm)
416 goto out_unlock;
417
Eric Dumazet740b0f12014-02-26 14:02:48 -0800418 rtt = tcp_metric_get(tm, TCP_METRIC_RTT);
419 m = rtt - tp->srtt_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700420
421 /* If newly calculated rtt larger than stored one, store new
422 * one. Otherwise, use EWMA. Remember, rtt overestimation is
423 * always better than underestimation.
424 */
425 if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
426 if (m <= 0)
Eric Dumazet740b0f12014-02-26 14:02:48 -0800427 rtt = tp->srtt_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700428 else
429 rtt -= (m >> 3);
Eric Dumazet740b0f12014-02-26 14:02:48 -0800430 tcp_metric_set(tm, TCP_METRIC_RTT, rtt);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700431 }
432
433 if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
434 unsigned long var;
435
436 if (m < 0)
437 m = -m;
438
439 /* Scale deviation to rttvar fixed point */
440 m >>= 1;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800441 if (m < tp->mdev_us)
442 m = tp->mdev_us;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700443
Eric Dumazet740b0f12014-02-26 14:02:48 -0800444 var = tcp_metric_get(tm, TCP_METRIC_RTTVAR);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700445 if (m >= var)
446 var = m;
447 else
448 var -= (var - m) >> 2;
449
Eric Dumazet740b0f12014-02-26 14:02:48 -0800450 tcp_metric_set(tm, TCP_METRIC_RTTVAR, var);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700451 }
452
453 if (tcp_in_initial_slowstart(tp)) {
454 /* Slow start still did not finish. */
455 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
456 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
457 if (val && (tp->snd_cwnd >> 1) > val)
458 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
459 tp->snd_cwnd >> 1);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700460 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700461 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
462 val = tcp_metric_get(tm, TCP_METRIC_CWND);
463 if (tp->snd_cwnd > val)
464 tcp_metric_set(tm, TCP_METRIC_CWND,
465 tp->snd_cwnd);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700466 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700467 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
468 icsk->icsk_ca_state == TCP_CA_Open) {
469 /* Cong. avoidance phase, cwnd is reliable. */
470 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
471 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
472 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
473 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
474 val = tcp_metric_get(tm, TCP_METRIC_CWND);
Alexander Duyck21008442012-07-11 17:18:04 -0700475 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700476 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700477 } else {
478 /* Else slow start did not finish, cwnd is non-sense,
479 * ssthresh may be also invalid.
480 */
481 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
482 val = tcp_metric_get(tm, TCP_METRIC_CWND);
483 tcp_metric_set(tm, TCP_METRIC_CWND,
484 (val + tp->snd_ssthresh) >> 1);
485 }
486 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
487 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
488 if (val && tp->snd_ssthresh > val)
489 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
490 tp->snd_ssthresh);
491 }
492 if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
493 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
494 if (val < tp->reordering &&
David S. Miller4aabd8e2012-07-09 16:07:30 -0700495 tp->reordering != sysctl_tcp_reordering)
David S. Miller51c5d0c2012-07-10 00:49:14 -0700496 tcp_metric_set(tm, TCP_METRIC_REORDERING,
497 tp->reordering);
David S. Miller4aabd8e2012-07-09 16:07:30 -0700498 }
499 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700500 tm->tcpm_stamp = jiffies;
501out_unlock:
502 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700503}
504
505/* Initialize metrics on socket. */
506
507void tcp_init_metrics(struct sock *sk)
508{
David S. Miller4aabd8e2012-07-09 16:07:30 -0700509 struct dst_entry *dst = __sk_dst_get(sk);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700510 struct tcp_sock *tp = tcp_sk(sk);
511 struct tcp_metrics_block *tm;
Yuchung Cheng1b7fdd22013-08-30 08:35:53 -0700512 u32 val, crtt = 0; /* cached RTT scaled by 8 */
David S. Miller4aabd8e2012-07-09 16:07:30 -0700513
514 if (dst == NULL)
515 goto reset;
516
517 dst_confirm(dst);
518
David S. Miller51c5d0c2012-07-10 00:49:14 -0700519 rcu_read_lock();
520 tm = tcp_get_metrics(sk, dst, true);
521 if (!tm) {
522 rcu_read_unlock();
523 goto reset;
524 }
525
526 if (tcp_metric_locked(tm, TCP_METRIC_CWND))
527 tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
528
529 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
530 if (val) {
531 tp->snd_ssthresh = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700532 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
533 tp->snd_ssthresh = tp->snd_cwnd_clamp;
534 } else {
535 /* ssthresh may have been reduced unnecessarily during.
536 * 3WHS. Restore it back to its initial default.
537 */
538 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
539 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700540 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
541 if (val && tp->reordering != val) {
David S. Miller4aabd8e2012-07-09 16:07:30 -0700542 tcp_disable_fack(tp);
543 tcp_disable_early_retrans(tp);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700544 tp->reordering = val;
David S. Miller4aabd8e2012-07-09 16:07:30 -0700545 }
546
Eric Dumazet740b0f12014-02-26 14:02:48 -0800547 crtt = tcp_metric_get(tm, TCP_METRIC_RTT);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700548 rcu_read_unlock();
David S. Miller4aabd8e2012-07-09 16:07:30 -0700549reset:
Yuchung Cheng52f20e62013-09-03 14:14:35 -0700550 /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
551 * to seed the RTO for later data packets because SYN packets are
552 * small. Use the per-dst cached values to seed the RTO but keep
553 * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
554 * Later the RTO will be updated immediately upon obtaining the first
555 * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
556 * influences the first RTO but not later RTT estimation.
557 *
558 * But if RTT is not available from the SYN (due to retransmits or
559 * syn cookies) or the cache, force a conservative 3secs timeout.
560 *
561 * A bit of theory. RTT is time passed after "normal" sized packet
562 * is sent until it is ACKed. In normal circumstances sending small
563 * packets force peer to delay ACKs and calculation is correct too.
564 * The algorithm is adaptive and, provided we follow specs, it
565 * NEVER underestimate RTT. BUT! If peer tries to make some clever
566 * tricks sort of "quick acks" for time long enough to decrease RTT
567 * to low value, and then abruptly stops to do it and starts to delay
568 * ACKs, wait for troubles.
569 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800570 if (crtt > tp->srtt_us) {
Neal Cardwell269aa752013-09-16 21:44:20 -0400571 /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800572 crtt /= 8 * USEC_PER_MSEC;
Neal Cardwell269aa752013-09-16 21:44:20 -0400573 inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
Eric Dumazet740b0f12014-02-26 14:02:48 -0800574 } else if (tp->srtt_us == 0) {
David S. Miller4aabd8e2012-07-09 16:07:30 -0700575 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
576 * 3WHS. This is most likely due to retransmission,
577 * including spurious one. Reset the RTO back to 3secs
578 * from the more aggressive 1sec to avoid more spurious
579 * retransmission.
580 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800581 tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK);
582 tp->mdev_us = tp->mdev_max_us = tp->rttvar_us;
583
David S. Miller4aabd8e2012-07-09 16:07:30 -0700584 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
585 }
586 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
587 * retransmitted. In light of RFC6298 more aggressive 1sec
588 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
589 * retransmission has occurred.
590 */
591 if (tp->total_retrans > 1)
592 tp->snd_cwnd = 1;
593 else
594 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
595 tp->snd_cwnd_stamp = tcp_time_stamp;
596}
David S. Millerab92bb22012-07-09 16:19:30 -0700597
Hannes Frederic Sowaa26552a2014-08-14 22:06:12 +0200598bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst,
599 bool paws_check, bool timestamps)
David S. Millerab92bb22012-07-09 16:19:30 -0700600{
David S. Miller51c5d0c2012-07-10 00:49:14 -0700601 struct tcp_metrics_block *tm;
602 bool ret;
603
David S. Millerab92bb22012-07-09 16:19:30 -0700604 if (!dst)
605 return false;
David S. Miller51c5d0c2012-07-10 00:49:14 -0700606
607 rcu_read_lock();
608 tm = __tcp_get_metrics_req(req, dst);
David S. Miller81166dd2012-07-10 03:14:24 -0700609 if (paws_check) {
610 if (tm &&
611 (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
Hannes Frederic Sowaa26552a2014-08-14 22:06:12 +0200612 ((s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW ||
613 !timestamps))
David S. Miller81166dd2012-07-10 03:14:24 -0700614 ret = false;
615 else
616 ret = true;
617 } else {
618 if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
619 ret = true;
620 else
621 ret = false;
622 }
David S. Miller51c5d0c2012-07-10 00:49:14 -0700623 rcu_read_unlock();
624
625 return ret;
David S. Millerab92bb22012-07-09 16:19:30 -0700626}
627EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
David S. Miller51c5d0c2012-07-10 00:49:14 -0700628
David S. Miller81166dd2012-07-10 03:14:24 -0700629void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
630{
631 struct tcp_metrics_block *tm;
632
633 rcu_read_lock();
634 tm = tcp_get_metrics(sk, dst, true);
635 if (tm) {
636 struct tcp_sock *tp = tcp_sk(sk);
637
638 if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
639 tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
640 tp->rx_opt.ts_recent = tm->tcpm_ts;
641 }
642 }
643 rcu_read_unlock();
644}
645EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
646
647/* VJ's idea. Save last timestamp seen from this destination and hold
648 * it at least for normal timewait interval to use for duplicate
649 * segment detection in subsequent connections, before they enter
650 * synchronized state.
651 */
652bool tcp_remember_stamp(struct sock *sk)
653{
654 struct dst_entry *dst = __sk_dst_get(sk);
655 bool ret = false;
656
657 if (dst) {
658 struct tcp_metrics_block *tm;
659
660 rcu_read_lock();
661 tm = tcp_get_metrics(sk, dst, true);
662 if (tm) {
663 struct tcp_sock *tp = tcp_sk(sk);
664
665 if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
666 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
667 tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
668 tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
669 tm->tcpm_ts = tp->rx_opt.ts_recent;
670 }
671 ret = true;
672 }
673 rcu_read_unlock();
674 }
675 return ret;
676}
677
678bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
679{
680 struct tcp_metrics_block *tm;
681 bool ret = false;
682
683 rcu_read_lock();
684 tm = __tcp_get_metrics_tw(tw);
Julian Anastasov9a0a9502012-07-23 10:46:38 +0300685 if (tm) {
David S. Miller81166dd2012-07-10 03:14:24 -0700686 const struct tcp_timewait_sock *tcptw;
687 struct sock *sk = (struct sock *) tw;
688
689 tcptw = tcp_twsk(sk);
690 if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
691 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
692 tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
693 tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
694 tm->tcpm_ts = tcptw->tw_ts_recent;
695 }
696 ret = true;
697 }
698 rcu_read_unlock();
699
700 return ret;
701}
702
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000703static DEFINE_SEQLOCK(fastopen_seqlock);
704
705void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
Yuchung Chengaab48742012-07-19 06:43:10 +0000706 struct tcp_fastopen_cookie *cookie,
707 int *syn_loss, unsigned long *last_syn_loss)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000708{
709 struct tcp_metrics_block *tm;
710
711 rcu_read_lock();
712 tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
713 if (tm) {
714 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
715 unsigned int seq;
716
717 do {
718 seq = read_seqbegin(&fastopen_seqlock);
719 if (tfom->mss)
720 *mss = tfom->mss;
721 *cookie = tfom->cookie;
Yuchung Chengaab48742012-07-19 06:43:10 +0000722 *syn_loss = tfom->syn_loss;
723 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000724 } while (read_seqretry(&fastopen_seqlock, seq));
725 }
726 rcu_read_unlock();
727}
728
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000729void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
Yuchung Chengaab48742012-07-19 06:43:10 +0000730 struct tcp_fastopen_cookie *cookie, bool syn_lost)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000731{
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800732 struct dst_entry *dst = __sk_dst_get(sk);
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000733 struct tcp_metrics_block *tm;
734
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800735 if (!dst)
736 return;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000737 rcu_read_lock();
Eric Dumazetdccf76c2013-11-13 15:00:46 -0800738 tm = tcp_get_metrics(sk, dst, true);
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000739 if (tm) {
740 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
741
742 write_seqlock_bh(&fastopen_seqlock);
Yuchung Chengc9686012013-10-29 10:09:05 -0700743 if (mss)
744 tfom->mss = mss;
745 if (cookie && cookie->len > 0)
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000746 tfom->cookie = *cookie;
Yuchung Chengaab48742012-07-19 06:43:10 +0000747 if (syn_lost) {
748 ++tfom->syn_loss;
749 tfom->last_syn_loss = jiffies;
750 } else
751 tfom->syn_loss = 0;
Yuchung Cheng1fe4c482012-07-19 06:43:06 +0000752 write_sequnlock_bh(&fastopen_seqlock);
753 }
754 rcu_read_unlock();
755}
756
Julian Anastasovd23ff702012-09-04 11:03:15 +0000757static struct genl_family tcp_metrics_nl_family = {
758 .id = GENL_ID_GENERATE,
759 .hdrsize = 0,
760 .name = TCP_METRICS_GENL_NAME,
761 .version = TCP_METRICS_GENL_VERSION,
762 .maxattr = TCP_METRICS_ATTR_MAX,
763 .netnsok = true,
764};
765
766static struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
767 [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
768 [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
769 .len = sizeof(struct in6_addr), },
770 /* Following attributes are not received for GET/DEL,
771 * we keep them for reference
772 */
773#if 0
774 [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
775 [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
776 [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
777 [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
778 [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
779 [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
780 [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
781 [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
782 .len = TCP_FASTOPEN_COOKIE_MAX, },
783#endif
784};
785
786/* Add attributes, caller cancels its header on failure */
787static int tcp_metrics_fill_info(struct sk_buff *msg,
788 struct tcp_metrics_block *tm)
789{
790 struct nlattr *nest;
791 int i;
792
Christoph Paasch324fd552014-01-08 16:05:55 +0100793 switch (tm->tcpm_daddr.family) {
Julian Anastasovd23ff702012-09-04 11:03:15 +0000794 case AF_INET:
795 if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4,
Christoph Paasch324fd552014-01-08 16:05:55 +0100796 tm->tcpm_daddr.addr.a4) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000797 goto nla_put_failure;
Christoph Paasch8a59359c2014-01-08 16:05:57 +0100798 if (nla_put_be32(msg, TCP_METRICS_ATTR_SADDR_IPV4,
799 tm->tcpm_saddr.addr.a4) < 0)
800 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000801 break;
802 case AF_INET6:
803 if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16,
Christoph Paasch324fd552014-01-08 16:05:55 +0100804 tm->tcpm_daddr.addr.a6) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000805 goto nla_put_failure;
Christoph Paasch8a59359c2014-01-08 16:05:57 +0100806 if (nla_put(msg, TCP_METRICS_ATTR_SADDR_IPV6, 16,
807 tm->tcpm_saddr.addr.a6) < 0)
808 goto nla_put_failure;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000809 break;
810 default:
811 return -EAFNOSUPPORT;
812 }
813
814 if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
815 jiffies - tm->tcpm_stamp) < 0)
816 goto nla_put_failure;
817 if (tm->tcpm_ts_stamp) {
818 if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP,
819 (s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0)
820 goto nla_put_failure;
821 if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL,
822 tm->tcpm_ts) < 0)
823 goto nla_put_failure;
824 }
825
826 {
827 int n = 0;
828
829 nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
830 if (!nest)
831 goto nla_put_failure;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800832 for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
833 u32 val = tm->tcpm_vals[i];
834
835 if (!val)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000836 continue;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800837 if (i == TCP_METRIC_RTT) {
838 if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
839 val) < 0)
840 goto nla_put_failure;
841 n++;
842 val = max(val / 1000, 1U);
843 }
844 if (i == TCP_METRIC_RTTVAR) {
845 if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
846 val) < 0)
847 goto nla_put_failure;
848 n++;
849 val = max(val / 1000, 1U);
850 }
851 if (nla_put_u32(msg, i + 1, val) < 0)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000852 goto nla_put_failure;
853 n++;
854 }
855 if (n)
856 nla_nest_end(msg, nest);
857 else
858 nla_nest_cancel(msg, nest);
859 }
860
861 {
862 struct tcp_fastopen_metrics tfom_copy[1], *tfom;
863 unsigned int seq;
864
865 do {
866 seq = read_seqbegin(&fastopen_seqlock);
867 tfom_copy[0] = tm->tcpm_fastopen;
868 } while (read_seqretry(&fastopen_seqlock, seq));
869
870 tfom = tfom_copy;
871 if (tfom->mss &&
872 nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
873 tfom->mss) < 0)
874 goto nla_put_failure;
875 if (tfom->syn_loss &&
876 (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
877 tfom->syn_loss) < 0 ||
878 nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
879 jiffies - tfom->last_syn_loss) < 0))
880 goto nla_put_failure;
881 if (tfom->cookie.len > 0 &&
882 nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
883 tfom->cookie.len, tfom->cookie.val) < 0)
884 goto nla_put_failure;
885 }
886
887 return 0;
888
889nla_put_failure:
890 return -EMSGSIZE;
891}
892
893static int tcp_metrics_dump_info(struct sk_buff *skb,
894 struct netlink_callback *cb,
895 struct tcp_metrics_block *tm)
896{
897 void *hdr;
898
Eric W. Biederman15e47302012-09-07 20:12:54 +0000899 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Julian Anastasovd23ff702012-09-04 11:03:15 +0000900 &tcp_metrics_nl_family, NLM_F_MULTI,
901 TCP_METRICS_CMD_GET);
902 if (!hdr)
903 return -EMSGSIZE;
904
905 if (tcp_metrics_fill_info(skb, tm) < 0)
906 goto nla_put_failure;
907
Johannes Berg053c0952015-01-16 22:09:00 +0100908 genlmsg_end(skb, hdr);
909 return 0;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000910
911nla_put_failure:
912 genlmsg_cancel(skb, hdr);
913 return -EMSGSIZE;
914}
915
916static int tcp_metrics_nl_dump(struct sk_buff *skb,
917 struct netlink_callback *cb)
918{
919 struct net *net = sock_net(skb->sk);
Eric W. Biederman098a6972015-03-13 00:07:44 -0500920 unsigned int max_rows = 1U << tcp_metrics_hash_log;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000921 unsigned int row, s_row = cb->args[0];
922 int s_col = cb->args[1], col = s_col;
923
924 for (row = s_row; row < max_rows; row++, s_col = 0) {
925 struct tcp_metrics_block *tm;
Eric W. Biederman098a6972015-03-13 00:07:44 -0500926 struct tcpm_hash_bucket *hb = tcp_metrics_hash + row;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000927
928 rcu_read_lock();
929 for (col = 0, tm = rcu_dereference(hb->chain); tm;
930 tm = rcu_dereference(tm->tcpm_next), col++) {
Eric W. Biederman849e8a02015-03-13 00:05:52 -0500931 if (!net_eq(tm_net(tm), net))
932 continue;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000933 if (col < s_col)
934 continue;
935 if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
936 rcu_read_unlock();
937 goto done;
938 }
939 }
940 rcu_read_unlock();
941 }
942
943done:
944 cb->args[0] = row;
945 cb->args[1] = col;
946 return skb->len;
947}
948
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100949static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
950 unsigned int *hash, int optional, int v4, int v6)
Julian Anastasovd23ff702012-09-04 11:03:15 +0000951{
952 struct nlattr *a;
953
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100954 a = info->attrs[v4];
Julian Anastasovd23ff702012-09-04 11:03:15 +0000955 if (a) {
956 addr->family = AF_INET;
957 addr->addr.a4 = nla_get_be32(a);
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100958 if (hash)
959 *hash = (__force unsigned int) addr->addr.a4;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000960 return 0;
961 }
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100962 a = info->attrs[v6];
Julian Anastasovd23ff702012-09-04 11:03:15 +0000963 if (a) {
Julian Anastasov2c42a3f2012-10-30 12:03:09 +0000964 if (nla_len(a) != sizeof(struct in6_addr))
Julian Anastasovd23ff702012-09-04 11:03:15 +0000965 return -EINVAL;
966 addr->family = AF_INET6;
967 memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6));
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100968 if (hash)
969 *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
Julian Anastasovd23ff702012-09-04 11:03:15 +0000970 return 0;
971 }
972 return optional ? 1 : -EAFNOSUPPORT;
973}
974
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100975static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
976 unsigned int *hash, int optional)
977{
978 return __parse_nl_addr(info, addr, hash, optional,
979 TCP_METRICS_ATTR_ADDR_IPV4,
980 TCP_METRICS_ATTR_ADDR_IPV6);
981}
982
983static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
984{
985 return __parse_nl_addr(info, addr, NULL, 0,
986 TCP_METRICS_ATTR_SADDR_IPV4,
987 TCP_METRICS_ATTR_SADDR_IPV6);
988}
989
Julian Anastasovd23ff702012-09-04 11:03:15 +0000990static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
991{
992 struct tcp_metrics_block *tm;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100993 struct inetpeer_addr saddr, daddr;
Julian Anastasovd23ff702012-09-04 11:03:15 +0000994 unsigned int hash;
995 struct sk_buff *msg;
996 struct net *net = genl_info_net(info);
997 void *reply;
998 int ret;
Christoph Paasch3e7013d2014-01-08 16:05:59 +0100999 bool src = true;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001000
Christoph Paasch324fd552014-01-08 16:05:55 +01001001 ret = parse_nl_addr(info, &daddr, &hash, 0);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001002 if (ret < 0)
1003 return ret;
1004
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001005 ret = parse_nl_saddr(info, &saddr);
1006 if (ret < 0)
1007 src = false;
1008
Julian Anastasovd23ff702012-09-04 11:03:15 +00001009 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1010 if (!msg)
1011 return -ENOMEM;
1012
1013 reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
1014 info->genlhdr->cmd);
1015 if (!reply)
1016 goto nla_put_failure;
1017
Eric W. Biederman3e5da622015-03-13 00:05:24 -05001018 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -05001019 hash = hash_32(hash, tcp_metrics_hash_log);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001020 ret = -ESRCH;
1021 rcu_read_lock();
Eric W. Biederman098a6972015-03-13 00:07:44 -05001022 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001023 tm = rcu_dereference(tm->tcpm_next)) {
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001024 if (addr_same(&tm->tcpm_daddr, &daddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -05001025 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
1026 net_eq(tm_net(tm), net)) {
Julian Anastasovd23ff702012-09-04 11:03:15 +00001027 ret = tcp_metrics_fill_info(msg, tm);
1028 break;
1029 }
1030 }
1031 rcu_read_unlock();
1032 if (ret < 0)
1033 goto out_free;
1034
1035 genlmsg_end(msg, reply);
1036 return genlmsg_reply(msg, info);
1037
1038nla_put_failure:
1039 ret = -EMSGSIZE;
1040
1041out_free:
1042 nlmsg_free(msg);
1043 return ret;
1044}
1045
Eric W. Biederman8a4bff72015-03-13 00:06:43 -05001046static void tcp_metrics_flush_all(struct net *net)
Julian Anastasovd23ff702012-09-04 11:03:15 +00001047{
Eric W. Biederman098a6972015-03-13 00:07:44 -05001048 unsigned int max_rows = 1U << tcp_metrics_hash_log;
1049 struct tcpm_hash_bucket *hb = tcp_metrics_hash;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001050 struct tcp_metrics_block *tm;
1051 unsigned int row;
1052
1053 for (row = 0; row < max_rows; row++, hb++) {
Eric W. Biederman04f721c2015-03-13 00:07:10 -05001054 struct tcp_metrics_block __rcu **pp;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001055 spin_lock_bh(&tcp_metrics_lock);
Eric W. Biederman04f721c2015-03-13 00:07:10 -05001056 pp = &hb->chain;
Eric Dumazet9f1ab182015-03-16 07:14:34 -07001057 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
Eric W. Biederman04f721c2015-03-13 00:07:10 -05001058 if (net_eq(tm_net(tm), net)) {
1059 *pp = tm->tcpm_next;
1060 kfree_rcu(tm, rcu_head);
1061 } else {
1062 pp = &tm->tcpm_next;
1063 }
Julian Anastasovd23ff702012-09-04 11:03:15 +00001064 }
Eric W. Biederman04f721c2015-03-13 00:07:10 -05001065 spin_unlock_bh(&tcp_metrics_lock);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001066 }
Julian Anastasovd23ff702012-09-04 11:03:15 +00001067}
1068
1069static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
1070{
1071 struct tcpm_hash_bucket *hb;
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001072 struct tcp_metrics_block *tm;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001073 struct tcp_metrics_block __rcu **pp;
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001074 struct inetpeer_addr saddr, daddr;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001075 unsigned int hash;
1076 struct net *net = genl_info_net(info);
1077 int ret;
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001078 bool src = true, found = false;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001079
Christoph Paasch324fd552014-01-08 16:05:55 +01001080 ret = parse_nl_addr(info, &daddr, &hash, 1);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001081 if (ret < 0)
1082 return ret;
Eric W. Biederman8a4bff72015-03-13 00:06:43 -05001083 if (ret > 0) {
1084 tcp_metrics_flush_all(net);
1085 return 0;
1086 }
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001087 ret = parse_nl_saddr(info, &saddr);
1088 if (ret < 0)
1089 src = false;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001090
Eric W. Biederman3e5da622015-03-13 00:05:24 -05001091 hash ^= net_hash_mix(net);
Eric W. Biederman098a6972015-03-13 00:07:44 -05001092 hash = hash_32(hash, tcp_metrics_hash_log);
1093 hb = tcp_metrics_hash + hash;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001094 pp = &hb->chain;
1095 spin_lock_bh(&tcp_metrics_lock);
Eric Dumazet9f1ab182015-03-16 07:14:34 -07001096 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
Christoph Paasch3e7013d2014-01-08 16:05:59 +01001097 if (addr_same(&tm->tcpm_daddr, &daddr) &&
Eric W. Biederman849e8a02015-03-13 00:05:52 -05001098 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
1099 net_eq(tm_net(tm), net)) {
Julian Anastasovd23ff702012-09-04 11:03:15 +00001100 *pp = tm->tcpm_next;
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001101 kfree_rcu(tm, rcu_head);
1102 found = true;
Christoph Paaschbbf852b2014-01-08 16:05:58 +01001103 } else {
1104 pp = &tm->tcpm_next;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001105 }
1106 }
1107 spin_unlock_bh(&tcp_metrics_lock);
Christoph Paasch00ca9c52014-01-21 13:30:26 +01001108 if (!found)
Julian Anastasovd23ff702012-09-04 11:03:15 +00001109 return -ESRCH;
Julian Anastasovd23ff702012-09-04 11:03:15 +00001110 return 0;
1111}
1112
Johannes Berg4534de82013-11-14 17:14:46 +01001113static const struct genl_ops tcp_metrics_nl_ops[] = {
Julian Anastasovd23ff702012-09-04 11:03:15 +00001114 {
1115 .cmd = TCP_METRICS_CMD_GET,
1116 .doit = tcp_metrics_nl_cmd_get,
1117 .dumpit = tcp_metrics_nl_dump,
1118 .policy = tcp_metrics_nl_policy,
Julian Anastasovd23ff702012-09-04 11:03:15 +00001119 },
1120 {
1121 .cmd = TCP_METRICS_CMD_DEL,
1122 .doit = tcp_metrics_nl_cmd_del,
1123 .policy = tcp_metrics_nl_policy,
1124 .flags = GENL_ADMIN_PERM,
1125 },
1126};
1127
Eric Dumazet5815d5e2012-07-19 23:02:34 +00001128static unsigned int tcpmhash_entries;
David S. Miller51c5d0c2012-07-10 00:49:14 -07001129static int __init set_tcpmhash_entries(char *str)
1130{
1131 ssize_t ret;
1132
1133 if (!str)
1134 return 0;
1135
Eric Dumazet5815d5e2012-07-19 23:02:34 +00001136 ret = kstrtouint(str, 0, &tcpmhash_entries);
David S. Miller51c5d0c2012-07-10 00:49:14 -07001137 if (ret)
1138 return 0;
1139
1140 return 1;
1141}
1142__setup("tcpmhash_entries=", set_tcpmhash_entries);
1143
1144static int __net_init tcp_net_metrics_init(struct net *net)
1145{
Eric Dumazet5815d5e2012-07-19 23:02:34 +00001146 size_t size;
1147 unsigned int slots;
David S. Miller51c5d0c2012-07-10 00:49:14 -07001148
Eric W. Biederman098a6972015-03-13 00:07:44 -05001149 if (!net_eq(net, &init_net))
1150 return 0;
1151
David S. Miller51c5d0c2012-07-10 00:49:14 -07001152 slots = tcpmhash_entries;
1153 if (!slots) {
1154 if (totalram_pages >= 128 * 1024)
1155 slots = 16 * 1024;
1156 else
1157 slots = 8 * 1024;
1158 }
1159
Eric W. Biederman098a6972015-03-13 00:07:44 -05001160 tcp_metrics_hash_log = order_base_2(slots);
1161 size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log;
David S. Miller51c5d0c2012-07-10 00:49:14 -07001162
Eric W. Biederman098a6972015-03-13 00:07:44 -05001163 tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1164 if (!tcp_metrics_hash)
1165 tcp_metrics_hash = vzalloc(size);
Eric Dumazet976a7022012-11-16 05:31:53 +00001166
Eric W. Biederman098a6972015-03-13 00:07:44 -05001167 if (!tcp_metrics_hash)
David S. Miller51c5d0c2012-07-10 00:49:14 -07001168 return -ENOMEM;
1169
David S. Miller51c5d0c2012-07-10 00:49:14 -07001170 return 0;
1171}
1172
1173static void __net_exit tcp_net_metrics_exit(struct net *net)
1174{
Eric W. Biederman098a6972015-03-13 00:07:44 -05001175 tcp_metrics_flush_all(net);
David S. Miller51c5d0c2012-07-10 00:49:14 -07001176}
1177
1178static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
1179 .init = tcp_net_metrics_init,
1180 .exit = tcp_net_metrics_exit,
1181};
1182
1183void __init tcp_metrics_init(void)
1184{
Julian Anastasovd23ff702012-09-04 11:03:15 +00001185 int ret;
1186
1187 ret = register_pernet_subsys(&tcp_net_metrics_ops);
1188 if (ret < 0)
Eric W. Biederman64935172015-03-13 00:04:51 -05001189 panic("Could not allocate the tcp_metrics hash table\n");
1190
Julian Anastasovd23ff702012-09-04 11:03:15 +00001191 ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
Johannes Bergc53ed742013-11-19 15:19:31 +01001192 tcp_metrics_nl_ops);
Julian Anastasovd23ff702012-09-04 11:03:15 +00001193 if (ret < 0)
Eric W. Biederman64935172015-03-13 00:04:51 -05001194 panic("Could not register tcp_metrics generic netlink\n");
David S. Miller51c5d0c2012-07-10 00:49:14 -07001195}