]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/ipv4/tcp_metrics.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-hirsute-kernel.git] / net / ipv4 / tcp_metrics.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
51c5d0c4
DM
2#include <linux/rcupdate.h>
3#include <linux/spinlock.h>
4#include <linux/jiffies.h>
ab92bb2f 5#include <linux/module.h>
4aabd8ef 6#include <linux/cache.h>
51c5d0c4
DM
7#include <linux/slab.h>
8#include <linux/init.h>
4aabd8ef 9#include <linux/tcp.h>
5815d5e7 10#include <linux/hash.h>
d23ff701 11#include <linux/tcp_metrics.h>
976a702a 12#include <linux/vmalloc.h>
4aabd8ef
DM
13
14#include <net/inet_connection_sock.h>
51c5d0c4 15#include <net/net_namespace.h>
ab92bb2f 16#include <net/request_sock.h>
51c5d0c4 17#include <net/inetpeer.h>
4aabd8ef 18#include <net/sock.h>
51c5d0c4 19#include <net/ipv6.h>
4aabd8ef
DM
20#include <net/dst.h>
21#include <net/tcp.h>
d23ff701 22#include <net/genetlink.h>
4aabd8ef
DM
23
24int sysctl_tcp_nometrics_save __read_mostly;
25
41804420
DM
26static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
27 const struct inetpeer_addr *daddr,
77f99ad1
CP
28 struct net *net, unsigned int hash);
29
1fe4c481
YC
30struct tcp_fastopen_metrics {
31 u16 mss;
2646c831
DL
32 u16 syn_loss:10, /* Recurring Fast Open SYN losses */
33 try_exp:2; /* Request w/ exp. option (once) */
aab48743 34 unsigned long last_syn_loss; /* Last Fast Open SYN loss */
1fe4c481
YC
35 struct tcp_fastopen_cookie cookie;
36};
37
740b0f18
ED
38/* TCP_METRIC_MAX includes 2 extra fields for userspace compatibility
39 * Kernel only stores RTT and RTTVAR in usec resolution
40 */
41#define TCP_METRIC_MAX_KERNEL (TCP_METRIC_MAX - 2)
42
51c5d0c4
DM
43struct tcp_metrics_block {
44 struct tcp_metrics_block __rcu *tcpm_next;
849e8a0c 45 possible_net_t tcpm_net;
a5443028 46 struct inetpeer_addr tcpm_saddr;
324fd55a 47 struct inetpeer_addr tcpm_daddr;
51c5d0c4
DM
48 unsigned long tcpm_stamp;
49 u32 tcpm_lock;
740b0f18 50 u32 tcpm_vals[TCP_METRIC_MAX_KERNEL + 1];
1fe4c481 51 struct tcp_fastopen_metrics tcpm_fastopen;
d23ff701
JA
52
53 struct rcu_head rcu_head;
51c5d0c4
DM
54};
55
849e8a0c
EB
56static inline struct net *tm_net(struct tcp_metrics_block *tm)
57{
58 return read_pnet(&tm->tcpm_net);
59}
60
51c5d0c4
DM
61static 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
51c5d0c4
DM
73static 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
51c5d0c4
DM
80static bool addr_same(const struct inetpeer_addr *a,
81 const struct inetpeer_addr *b)
82{
d39d14ff 83 return inetpeer_addr_cmp(a, b) == 0;
51c5d0c4
DM
84}
85
86struct tcpm_hash_bucket {
87 struct tcp_metrics_block __rcu *chain;
88};
89
098a697b
EB
90static struct tcpm_hash_bucket *tcp_metrics_hash __read_mostly;
91static unsigned int tcp_metrics_hash_log __read_mostly;
92
51c5d0c4
DM
93static DEFINE_SPINLOCK(tcp_metrics_lock);
94
740b0f18
ED
95static void tcpm_suck_dst(struct tcp_metrics_block *tm,
96 const struct dst_entry *dst,
efeaa555 97 bool fastopen_clear)
51c5d0c4 98{
740b0f18 99 u32 msval;
51c5d0c4
DM
100 u32 val;
101
9a0a9502
JA
102 tm->tcpm_stamp = jiffies;
103
51c5d0c4
DM
104 val = 0;
105 if (dst_metric_locked(dst, RTAX_RTT))
106 val |= 1 << TCP_METRIC_RTT;
107 if (dst_metric_locked(dst, RTAX_RTTVAR))
108 val |= 1 << TCP_METRIC_RTTVAR;
109 if (dst_metric_locked(dst, RTAX_SSTHRESH))
110 val |= 1 << TCP_METRIC_SSTHRESH;
111 if (dst_metric_locked(dst, RTAX_CWND))
112 val |= 1 << TCP_METRIC_CWND;
113 if (dst_metric_locked(dst, RTAX_REORDERING))
114 val |= 1 << TCP_METRIC_REORDERING;
115 tm->tcpm_lock = val;
116
740b0f18
ED
117 msval = dst_metric_raw(dst, RTAX_RTT);
118 tm->tcpm_vals[TCP_METRIC_RTT] = msval * USEC_PER_MSEC;
119
120 msval = dst_metric_raw(dst, RTAX_RTTVAR);
121 tm->tcpm_vals[TCP_METRIC_RTTVAR] = msval * USEC_PER_MSEC;
51c5d0c4
DM
122 tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
123 tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
124 tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
efeaa555
ED
125 if (fastopen_clear) {
126 tm->tcpm_fastopen.mss = 0;
127 tm->tcpm_fastopen.syn_loss = 0;
2646c831
DL
128 tm->tcpm_fastopen.try_exp = 0;
129 tm->tcpm_fastopen.cookie.exp = false;
efeaa555
ED
130 tm->tcpm_fastopen.cookie.len = 0;
131 }
51c5d0c4
DM
132}
133
77f99ad1
CP
134#define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
135
136static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
137{
138 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
139 tcpm_suck_dst(tm, dst, false);
140}
141
142#define TCP_METRICS_RECLAIM_DEPTH 5
143#define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
144
9f1ab186
ED
145#define deref_locked(p) \
146 rcu_dereference_protected(p, lockdep_is_held(&tcp_metrics_lock))
147
51c5d0c4 148static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
a5443028 149 struct inetpeer_addr *saddr,
324fd55a 150 struct inetpeer_addr *daddr,
77f99ad1 151 unsigned int hash)
51c5d0c4
DM
152{
153 struct tcp_metrics_block *tm;
154 struct net *net;
77f99ad1 155 bool reclaim = false;
51c5d0c4
DM
156
157 spin_lock_bh(&tcp_metrics_lock);
158 net = dev_net(dst->dev);
77f99ad1
CP
159
160 /* While waiting for the spin-lock the cache might have been populated
161 * with this entry and so we have to check again.
162 */
41804420 163 tm = __tcp_get_metrics(saddr, daddr, net, hash);
77f99ad1
CP
164 if (tm == TCP_METRICS_RECLAIM_PTR) {
165 reclaim = true;
166 tm = NULL;
167 }
168 if (tm) {
169 tcpm_check_stamp(tm, dst);
170 goto out_unlock;
171 }
172
51c5d0c4
DM
173 if (unlikely(reclaim)) {
174 struct tcp_metrics_block *oldest;
175
9f1ab186
ED
176 oldest = deref_locked(tcp_metrics_hash[hash].chain);
177 for (tm = deref_locked(oldest->tcpm_next); tm;
178 tm = deref_locked(tm->tcpm_next)) {
51c5d0c4
DM
179 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
180 oldest = tm;
181 }
182 tm = oldest;
183 } else {
184 tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
185 if (!tm)
186 goto out_unlock;
187 }
849e8a0c 188 write_pnet(&tm->tcpm_net, net);
a5443028 189 tm->tcpm_saddr = *saddr;
324fd55a 190 tm->tcpm_daddr = *daddr;
51c5d0c4 191
efeaa555 192 tcpm_suck_dst(tm, dst, true);
51c5d0c4
DM
193
194 if (likely(!reclaim)) {
098a697b
EB
195 tm->tcpm_next = tcp_metrics_hash[hash].chain;
196 rcu_assign_pointer(tcp_metrics_hash[hash].chain, tm);
51c5d0c4
DM
197 }
198
199out_unlock:
200 spin_unlock_bh(&tcp_metrics_lock);
201 return tm;
202}
203
51c5d0c4
DM
204static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
205{
206 if (tm)
207 return tm;
208 if (depth > TCP_METRICS_RECLAIM_DEPTH)
209 return TCP_METRICS_RECLAIM_PTR;
210 return NULL;
211}
212
a5443028
CP
213static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
214 const struct inetpeer_addr *daddr,
51c5d0c4
DM
215 struct net *net, unsigned int hash)
216{
217 struct tcp_metrics_block *tm;
218 int depth = 0;
219
098a697b 220 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
51c5d0c4 221 tm = rcu_dereference(tm->tcpm_next)) {
a5443028 222 if (addr_same(&tm->tcpm_saddr, saddr) &&
849e8a0c
EB
223 addr_same(&tm->tcpm_daddr, daddr) &&
224 net_eq(tm_net(tm), net))
51c5d0c4
DM
225 break;
226 depth++;
227 }
228 return tcp_get_encode(tm, depth);
229}
230
231static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
232 struct dst_entry *dst)
233{
234 struct tcp_metrics_block *tm;
a5443028 235 struct inetpeer_addr saddr, daddr;
51c5d0c4
DM
236 unsigned int hash;
237 struct net *net;
238
a5443028 239 saddr.family = req->rsk_ops->family;
324fd55a
CP
240 daddr.family = req->rsk_ops->family;
241 switch (daddr.family) {
51c5d0c4 242 case AF_INET:
3abef286
DA
243 inetpeer_set_addr_v4(&saddr, inet_rsk(req)->ir_loc_addr);
244 inetpeer_set_addr_v4(&daddr, inet_rsk(req)->ir_rmt_addr);
72afa352 245 hash = ipv4_addr_hash(inet_rsk(req)->ir_rmt_addr);
51c5d0c4 246 break;
634fb979 247#if IS_ENABLED(CONFIG_IPV6)
51c5d0c4 248 case AF_INET6:
3abef286
DA
249 inetpeer_set_addr_v6(&saddr, &inet_rsk(req)->ir_v6_loc_addr);
250 inetpeer_set_addr_v6(&daddr, &inet_rsk(req)->ir_v6_rmt_addr);
634fb979 251 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
51c5d0c4 252 break;
634fb979 253#endif
51c5d0c4
DM
254 default:
255 return NULL;
256 }
257
51c5d0c4 258 net = dev_net(dst->dev);
3e5da62d 259 hash ^= net_hash_mix(net);
098a697b 260 hash = hash_32(hash, tcp_metrics_hash_log);
51c5d0c4 261
098a697b 262 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
51c5d0c4 263 tm = rcu_dereference(tm->tcpm_next)) {
a5443028 264 if (addr_same(&tm->tcpm_saddr, &saddr) &&
849e8a0c
EB
265 addr_same(&tm->tcpm_daddr, &daddr) &&
266 net_eq(tm_net(tm), net))
51c5d0c4
DM
267 break;
268 }
269 tcpm_check_stamp(tm, dst);
270 return tm;
271}
272
273static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
274 struct dst_entry *dst,
275 bool create)
276{
277 struct tcp_metrics_block *tm;
a5443028 278 struct inetpeer_addr saddr, daddr;
51c5d0c4
DM
279 unsigned int hash;
280 struct net *net;
51c5d0c4 281
3ad88cf7 282 if (sk->sk_family == AF_INET) {
3abef286
DA
283 inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr);
284 inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr);
72afa352 285 hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr);
3ad88cf7 286 }
c2bb06db 287#if IS_ENABLED(CONFIG_IPV6)
3ad88cf7
CP
288 else if (sk->sk_family == AF_INET6) {
289 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
3abef286
DA
290 inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr);
291 inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr);
72afa352 292 hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr);
3ad88cf7 293 } else {
3abef286
DA
294 inetpeer_set_addr_v6(&saddr, &sk->sk_v6_rcv_saddr);
295 inetpeer_set_addr_v6(&daddr, &sk->sk_v6_daddr);
3ad88cf7
CP
296 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
297 }
298 }
c2bb06db 299#endif
3ad88cf7 300 else
51c5d0c4 301 return NULL;
51c5d0c4 302
51c5d0c4 303 net = dev_net(dst->dev);
3e5da62d 304 hash ^= net_hash_mix(net);
098a697b 305 hash = hash_32(hash, tcp_metrics_hash_log);
51c5d0c4 306
a5443028 307 tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
77f99ad1 308 if (tm == TCP_METRICS_RECLAIM_PTR)
51c5d0c4 309 tm = NULL;
51c5d0c4 310 if (!tm && create)
41804420 311 tm = tcpm_new(dst, &saddr, &daddr, hash);
51c5d0c4
DM
312 else
313 tcpm_check_stamp(tm, dst);
314
315 return tm;
316}
317
4aabd8ef
DM
318/* Save metrics learned by this TCP session. This function is called
319 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
320 * or goes from LAST-ACK to CLOSE.
321 */
322void tcp_update_metrics(struct sock *sk)
323{
51c5d0c4 324 const struct inet_connection_sock *icsk = inet_csk(sk);
4aabd8ef 325 struct dst_entry *dst = __sk_dst_get(sk);
51c5d0c4 326 struct tcp_sock *tp = tcp_sk(sk);
1043e25f 327 struct net *net = sock_net(sk);
51c5d0c4
DM
328 struct tcp_metrics_block *tm;
329 unsigned long rtt;
330 u32 val;
331 int m;
4aabd8ef 332
c3a2e837 333 sk_dst_confirm(sk);
51c5d0c4 334 if (sysctl_tcp_nometrics_save || !dst)
4aabd8ef
DM
335 return;
336
51c5d0c4 337 rcu_read_lock();
740b0f18 338 if (icsk->icsk_backoff || !tp->srtt_us) {
51c5d0c4
DM
339 /* This session failed to estimate rtt. Why?
340 * Probably, no packets returned in time. Reset our
341 * results.
342 */
343 tm = tcp_get_metrics(sk, dst, false);
344 if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
345 tcp_metric_set(tm, TCP_METRIC_RTT, 0);
346 goto out_unlock;
347 } else
348 tm = tcp_get_metrics(sk, dst, true);
4aabd8ef 349
51c5d0c4
DM
350 if (!tm)
351 goto out_unlock;
4aabd8ef 352
740b0f18
ED
353 rtt = tcp_metric_get(tm, TCP_METRIC_RTT);
354 m = rtt - tp->srtt_us;
4aabd8ef 355
51c5d0c4
DM
356 /* If newly calculated rtt larger than stored one, store new
357 * one. Otherwise, use EWMA. Remember, rtt overestimation is
358 * always better than underestimation.
359 */
360 if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
361 if (m <= 0)
740b0f18 362 rtt = tp->srtt_us;
51c5d0c4
DM
363 else
364 rtt -= (m >> 3);
740b0f18 365 tcp_metric_set(tm, TCP_METRIC_RTT, rtt);
51c5d0c4 366 }
4aabd8ef 367
51c5d0c4
DM
368 if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
369 unsigned long var;
4aabd8ef 370
51c5d0c4
DM
371 if (m < 0)
372 m = -m;
4aabd8ef 373
51c5d0c4
DM
374 /* Scale deviation to rttvar fixed point */
375 m >>= 1;
740b0f18
ED
376 if (m < tp->mdev_us)
377 m = tp->mdev_us;
4aabd8ef 378
740b0f18 379 var = tcp_metric_get(tm, TCP_METRIC_RTTVAR);
51c5d0c4
DM
380 if (m >= var)
381 var = m;
382 else
383 var -= (var - m) >> 2;
4aabd8ef 384
740b0f18 385 tcp_metric_set(tm, TCP_METRIC_RTTVAR, var);
51c5d0c4
DM
386 }
387
388 if (tcp_in_initial_slowstart(tp)) {
389 /* Slow start still did not finish. */
390 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
391 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
392 if (val && (tp->snd_cwnd >> 1) > val)
393 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
394 tp->snd_cwnd >> 1);
395 }
396 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
397 val = tcp_metric_get(tm, TCP_METRIC_CWND);
398 if (tp->snd_cwnd > val)
399 tcp_metric_set(tm, TCP_METRIC_CWND,
400 tp->snd_cwnd);
401 }
071d5080 402 } else if (!tcp_in_slow_start(tp) &&
51c5d0c4
DM
403 icsk->icsk_ca_state == TCP_CA_Open) {
404 /* Cong. avoidance phase, cwnd is reliable. */
405 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
406 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
407 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
408 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
409 val = tcp_metric_get(tm, TCP_METRIC_CWND);
2100844c 410 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
51c5d0c4
DM
411 }
412 } else {
413 /* Else slow start did not finish, cwnd is non-sense,
414 * ssthresh may be also invalid.
415 */
416 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
417 val = tcp_metric_get(tm, TCP_METRIC_CWND);
418 tcp_metric_set(tm, TCP_METRIC_CWND,
419 (val + tp->snd_ssthresh) >> 1);
420 }
421 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
422 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
423 if (val && tp->snd_ssthresh > val)
424 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
425 tp->snd_ssthresh);
426 }
427 if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
428 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
429 if (val < tp->reordering &&
1043e25f 430 tp->reordering != net->ipv4.sysctl_tcp_reordering)
51c5d0c4
DM
431 tcp_metric_set(tm, TCP_METRIC_REORDERING,
432 tp->reordering);
4aabd8ef
DM
433 }
434 }
51c5d0c4
DM
435 tm->tcpm_stamp = jiffies;
436out_unlock:
437 rcu_read_unlock();
4aabd8ef
DM
438}
439
440/* Initialize metrics on socket. */
441
442void tcp_init_metrics(struct sock *sk)
443{
4aabd8ef 444 struct dst_entry *dst = __sk_dst_get(sk);
51c5d0c4
DM
445 struct tcp_sock *tp = tcp_sk(sk);
446 struct tcp_metrics_block *tm;
1b7fdd2a 447 u32 val, crtt = 0; /* cached RTT scaled by 8 */
4aabd8ef 448
c3a2e837 449 sk_dst_confirm(sk);
51456b29 450 if (!dst)
4aabd8ef
DM
451 goto reset;
452
51c5d0c4
DM
453 rcu_read_lock();
454 tm = tcp_get_metrics(sk, dst, true);
455 if (!tm) {
456 rcu_read_unlock();
457 goto reset;
458 }
459
460 if (tcp_metric_locked(tm, TCP_METRIC_CWND))
461 tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
462
463 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
464 if (val) {
465 tp->snd_ssthresh = val;
4aabd8ef
DM
466 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
467 tp->snd_ssthresh = tp->snd_cwnd_clamp;
468 } else {
469 /* ssthresh may have been reduced unnecessarily during.
470 * 3WHS. Restore it back to its initial default.
471 */
472 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
473 }
51c5d0c4
DM
474 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
475 if (val && tp->reordering != val) {
4aabd8ef 476 tcp_disable_fack(tp);
51c5d0c4 477 tp->reordering = val;
4aabd8ef
DM
478 }
479
740b0f18 480 crtt = tcp_metric_get(tm, TCP_METRIC_RTT);
51c5d0c4 481 rcu_read_unlock();
4aabd8ef 482reset:
52f20e65
YC
483 /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
484 * to seed the RTO for later data packets because SYN packets are
485 * small. Use the per-dst cached values to seed the RTO but keep
486 * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
487 * Later the RTO will be updated immediately upon obtaining the first
488 * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
489 * influences the first RTO but not later RTT estimation.
490 *
491 * But if RTT is not available from the SYN (due to retransmits or
492 * syn cookies) or the cache, force a conservative 3secs timeout.
493 *
494 * A bit of theory. RTT is time passed after "normal" sized packet
495 * is sent until it is ACKed. In normal circumstances sending small
496 * packets force peer to delay ACKs and calculation is correct too.
497 * The algorithm is adaptive and, provided we follow specs, it
498 * NEVER underestimate RTT. BUT! If peer tries to make some clever
499 * tricks sort of "quick acks" for time long enough to decrease RTT
500 * to low value, and then abruptly stops to do it and starts to delay
501 * ACKs, wait for troubles.
502 */
740b0f18 503 if (crtt > tp->srtt_us) {
269aa759 504 /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
9bdfb3b7 505 crtt /= 8 * USEC_PER_SEC / HZ;
269aa759 506 inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
740b0f18 507 } else if (tp->srtt_us == 0) {
4aabd8ef
DM
508 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
509 * 3WHS. This is most likely due to retransmission,
510 * including spurious one. Reset the RTO back to 3secs
511 * from the more aggressive 1sec to avoid more spurious
512 * retransmission.
513 */
740b0f18
ED
514 tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK);
515 tp->mdev_us = tp->mdev_max_us = tp->rttvar_us;
516
4aabd8ef
DM
517 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
518 }
519 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
520 * retransmitted. In light of RFC6298 more aggressive 1sec
521 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
522 * retransmission has occurred.
523 */
524 if (tp->total_retrans > 1)
525 tp->snd_cwnd = 1;
526 else
527 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
c2203cf7 528 tp->snd_cwnd_stamp = tcp_jiffies32;
4aabd8ef 529}
ab92bb2f 530
d82bae12 531bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst)
ab92bb2f 532{
51c5d0c4
DM
533 struct tcp_metrics_block *tm;
534 bool ret;
535
ab92bb2f
DM
536 if (!dst)
537 return false;
51c5d0c4
DM
538
539 rcu_read_lock();
540 tm = __tcp_get_metrics_req(req, dst);
d82bae12 541 if (tm && tcp_metric_get(tm, TCP_METRIC_RTT))
81166dd6 542 ret = true;
d82bae12
SHY
543 else
544 ret = false;
81166dd6
DM
545 rcu_read_unlock();
546
547 return ret;
548}
549
1fe4c481
YC
550static DEFINE_SEQLOCK(fastopen_seqlock);
551
552void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
aab48743
YC
553 struct tcp_fastopen_cookie *cookie,
554 int *syn_loss, unsigned long *last_syn_loss)
1fe4c481
YC
555{
556 struct tcp_metrics_block *tm;
557
558 rcu_read_lock();
559 tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
560 if (tm) {
561 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
562 unsigned int seq;
563
564 do {
565 seq = read_seqbegin(&fastopen_seqlock);
566 if (tfom->mss)
567 *mss = tfom->mss;
568 *cookie = tfom->cookie;
2646c831
DL
569 if (cookie->len <= 0 && tfom->try_exp == 1)
570 cookie->exp = true;
aab48743
YC
571 *syn_loss = tfom->syn_loss;
572 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
1fe4c481
YC
573 } while (read_seqretry(&fastopen_seqlock, seq));
574 }
575 rcu_read_unlock();
576}
577
1fe4c481 578void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
2646c831
DL
579 struct tcp_fastopen_cookie *cookie, bool syn_lost,
580 u16 try_exp)
1fe4c481 581{
dccf76ca 582 struct dst_entry *dst = __sk_dst_get(sk);
1fe4c481
YC
583 struct tcp_metrics_block *tm;
584
dccf76ca
ED
585 if (!dst)
586 return;
1fe4c481 587 rcu_read_lock();
dccf76ca 588 tm = tcp_get_metrics(sk, dst, true);
1fe4c481
YC
589 if (tm) {
590 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
591
592 write_seqlock_bh(&fastopen_seqlock);
c968601d
YC
593 if (mss)
594 tfom->mss = mss;
595 if (cookie && cookie->len > 0)
1fe4c481 596 tfom->cookie = *cookie;
2646c831
DL
597 else if (try_exp > tfom->try_exp &&
598 tfom->cookie.len <= 0 && !tfom->cookie.exp)
599 tfom->try_exp = try_exp;
aab48743
YC
600 if (syn_lost) {
601 ++tfom->syn_loss;
602 tfom->last_syn_loss = jiffies;
603 } else
604 tfom->syn_loss = 0;
1fe4c481
YC
605 write_sequnlock_bh(&fastopen_seqlock);
606 }
607 rcu_read_unlock();
608}
609
489111e5 610static struct genl_family tcp_metrics_nl_family;
d23ff701 611
4f70c96f 612static const struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
d23ff701
JA
613 [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
614 [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
615 .len = sizeof(struct in6_addr), },
616 /* Following attributes are not received for GET/DEL,
617 * we keep them for reference
618 */
619#if 0
620 [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
621 [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
622 [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
623 [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
624 [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
625 [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
626 [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
627 [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
628 .len = TCP_FASTOPEN_COOKIE_MAX, },
629#endif
630};
631
632/* Add attributes, caller cancels its header on failure */
633static int tcp_metrics_fill_info(struct sk_buff *msg,
634 struct tcp_metrics_block *tm)
635{
636 struct nlattr *nest;
637 int i;
638
324fd55a 639 switch (tm->tcpm_daddr.family) {
d23ff701 640 case AF_INET:
930345ea 641 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_ADDR_IPV4,
3abef286 642 inetpeer_get_addr_v4(&tm->tcpm_daddr)) < 0)
d23ff701 643 goto nla_put_failure;
930345ea 644 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_SADDR_IPV4,
3abef286 645 inetpeer_get_addr_v4(&tm->tcpm_saddr)) < 0)
8a59359c 646 goto nla_put_failure;
d23ff701
JA
647 break;
648 case AF_INET6:
930345ea 649 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_ADDR_IPV6,
3abef286 650 inetpeer_get_addr_v6(&tm->tcpm_daddr)) < 0)
d23ff701 651 goto nla_put_failure;
930345ea 652 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_SADDR_IPV6,
3abef286 653 inetpeer_get_addr_v6(&tm->tcpm_saddr)) < 0)
8a59359c 654 goto nla_put_failure;
d23ff701
JA
655 break;
656 default:
657 return -EAFNOSUPPORT;
658 }
659
660 if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
2175d87c
ND
661 jiffies - tm->tcpm_stamp,
662 TCP_METRICS_ATTR_PAD) < 0)
d23ff701 663 goto nla_put_failure;
d23ff701
JA
664
665 {
666 int n = 0;
667
668 nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
669 if (!nest)
670 goto nla_put_failure;
740b0f18
ED
671 for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
672 u32 val = tm->tcpm_vals[i];
673
674 if (!val)
d23ff701 675 continue;
740b0f18
ED
676 if (i == TCP_METRIC_RTT) {
677 if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
678 val) < 0)
679 goto nla_put_failure;
680 n++;
681 val = max(val / 1000, 1U);
682 }
683 if (i == TCP_METRIC_RTTVAR) {
684 if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
685 val) < 0)
686 goto nla_put_failure;
687 n++;
688 val = max(val / 1000, 1U);
689 }
690 if (nla_put_u32(msg, i + 1, val) < 0)
d23ff701
JA
691 goto nla_put_failure;
692 n++;
693 }
694 if (n)
695 nla_nest_end(msg, nest);
696 else
697 nla_nest_cancel(msg, nest);
698 }
699
700 {
701 struct tcp_fastopen_metrics tfom_copy[1], *tfom;
702 unsigned int seq;
703
704 do {
705 seq = read_seqbegin(&fastopen_seqlock);
706 tfom_copy[0] = tm->tcpm_fastopen;
707 } while (read_seqretry(&fastopen_seqlock, seq));
708
709 tfom = tfom_copy;
710 if (tfom->mss &&
711 nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
712 tfom->mss) < 0)
713 goto nla_put_failure;
714 if (tfom->syn_loss &&
715 (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
716 tfom->syn_loss) < 0 ||
717 nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
2175d87c
ND
718 jiffies - tfom->last_syn_loss,
719 TCP_METRICS_ATTR_PAD) < 0))
d23ff701
JA
720 goto nla_put_failure;
721 if (tfom->cookie.len > 0 &&
722 nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
723 tfom->cookie.len, tfom->cookie.val) < 0)
724 goto nla_put_failure;
725 }
726
727 return 0;
728
729nla_put_failure:
730 return -EMSGSIZE;
731}
732
733static int tcp_metrics_dump_info(struct sk_buff *skb,
734 struct netlink_callback *cb,
735 struct tcp_metrics_block *tm)
736{
737 void *hdr;
738
15e47304 739 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
d23ff701
JA
740 &tcp_metrics_nl_family, NLM_F_MULTI,
741 TCP_METRICS_CMD_GET);
742 if (!hdr)
743 return -EMSGSIZE;
744
745 if (tcp_metrics_fill_info(skb, tm) < 0)
746 goto nla_put_failure;
747
053c095a
JB
748 genlmsg_end(skb, hdr);
749 return 0;
d23ff701
JA
750
751nla_put_failure:
752 genlmsg_cancel(skb, hdr);
753 return -EMSGSIZE;
754}
755
756static int tcp_metrics_nl_dump(struct sk_buff *skb,
757 struct netlink_callback *cb)
758{
759 struct net *net = sock_net(skb->sk);
098a697b 760 unsigned int max_rows = 1U << tcp_metrics_hash_log;
d23ff701
JA
761 unsigned int row, s_row = cb->args[0];
762 int s_col = cb->args[1], col = s_col;
763
764 for (row = s_row; row < max_rows; row++, s_col = 0) {
765 struct tcp_metrics_block *tm;
098a697b 766 struct tcpm_hash_bucket *hb = tcp_metrics_hash + row;
d23ff701
JA
767
768 rcu_read_lock();
769 for (col = 0, tm = rcu_dereference(hb->chain); tm;
770 tm = rcu_dereference(tm->tcpm_next), col++) {
849e8a0c
EB
771 if (!net_eq(tm_net(tm), net))
772 continue;
d23ff701
JA
773 if (col < s_col)
774 continue;
775 if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
776 rcu_read_unlock();
777 goto done;
778 }
779 }
780 rcu_read_unlock();
781 }
782
783done:
784 cb->args[0] = row;
785 cb->args[1] = col;
786 return skb->len;
787}
788
3e7013dd
CP
789static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
790 unsigned int *hash, int optional, int v4, int v6)
d23ff701
JA
791{
792 struct nlattr *a;
793
3e7013dd 794 a = info->attrs[v4];
d23ff701 795 if (a) {
3abef286 796 inetpeer_set_addr_v4(addr, nla_get_in_addr(a));
3e7013dd 797 if (hash)
3abef286 798 *hash = ipv4_addr_hash(inetpeer_get_addr_v4(addr));
d23ff701
JA
799 return 0;
800 }
3e7013dd 801 a = info->attrs[v6];
d23ff701 802 if (a) {
3abef286
DA
803 struct in6_addr in6;
804
2c42a3fb 805 if (nla_len(a) != sizeof(struct in6_addr))
d23ff701 806 return -EINVAL;
3abef286
DA
807 in6 = nla_get_in6_addr(a);
808 inetpeer_set_addr_v6(addr, &in6);
3e7013dd 809 if (hash)
3abef286 810 *hash = ipv6_addr_hash(inetpeer_get_addr_v6(addr));
d23ff701
JA
811 return 0;
812 }
813 return optional ? 1 : -EAFNOSUPPORT;
814}
815
3e7013dd
CP
816static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
817 unsigned int *hash, int optional)
818{
819 return __parse_nl_addr(info, addr, hash, optional,
820 TCP_METRICS_ATTR_ADDR_IPV4,
821 TCP_METRICS_ATTR_ADDR_IPV6);
822}
823
824static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
825{
826 return __parse_nl_addr(info, addr, NULL, 0,
827 TCP_METRICS_ATTR_SADDR_IPV4,
828 TCP_METRICS_ATTR_SADDR_IPV6);
829}
830
d23ff701
JA
831static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
832{
833 struct tcp_metrics_block *tm;
3e7013dd 834 struct inetpeer_addr saddr, daddr;
d23ff701
JA
835 unsigned int hash;
836 struct sk_buff *msg;
837 struct net *net = genl_info_net(info);
838 void *reply;
839 int ret;
3e7013dd 840 bool src = true;
d23ff701 841
324fd55a 842 ret = parse_nl_addr(info, &daddr, &hash, 0);
d23ff701
JA
843 if (ret < 0)
844 return ret;
845
3e7013dd
CP
846 ret = parse_nl_saddr(info, &saddr);
847 if (ret < 0)
848 src = false;
849
d23ff701
JA
850 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
851 if (!msg)
852 return -ENOMEM;
853
854 reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
855 info->genlhdr->cmd);
856 if (!reply)
857 goto nla_put_failure;
858
3e5da62d 859 hash ^= net_hash_mix(net);
098a697b 860 hash = hash_32(hash, tcp_metrics_hash_log);
d23ff701
JA
861 ret = -ESRCH;
862 rcu_read_lock();
098a697b 863 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
d23ff701 864 tm = rcu_dereference(tm->tcpm_next)) {
3e7013dd 865 if (addr_same(&tm->tcpm_daddr, &daddr) &&
849e8a0c
EB
866 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
867 net_eq(tm_net(tm), net)) {
d23ff701
JA
868 ret = tcp_metrics_fill_info(msg, tm);
869 break;
870 }
871 }
872 rcu_read_unlock();
873 if (ret < 0)
874 goto out_free;
875
876 genlmsg_end(msg, reply);
877 return genlmsg_reply(msg, info);
878
879nla_put_failure:
880 ret = -EMSGSIZE;
881
882out_free:
883 nlmsg_free(msg);
884 return ret;
885}
886
8a4bff71 887static void tcp_metrics_flush_all(struct net *net)
d23ff701 888{
098a697b
EB
889 unsigned int max_rows = 1U << tcp_metrics_hash_log;
890 struct tcpm_hash_bucket *hb = tcp_metrics_hash;
d23ff701
JA
891 struct tcp_metrics_block *tm;
892 unsigned int row;
893
894 for (row = 0; row < max_rows; row++, hb++) {
04f721c6 895 struct tcp_metrics_block __rcu **pp;
d23ff701 896 spin_lock_bh(&tcp_metrics_lock);
04f721c6 897 pp = &hb->chain;
9f1ab186 898 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
04f721c6
EB
899 if (net_eq(tm_net(tm), net)) {
900 *pp = tm->tcpm_next;
901 kfree_rcu(tm, rcu_head);
902 } else {
903 pp = &tm->tcpm_next;
904 }
d23ff701 905 }
04f721c6 906 spin_unlock_bh(&tcp_metrics_lock);
d23ff701 907 }
d23ff701
JA
908}
909
910static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
911{
912 struct tcpm_hash_bucket *hb;
00ca9c5b 913 struct tcp_metrics_block *tm;
d23ff701 914 struct tcp_metrics_block __rcu **pp;
3e7013dd 915 struct inetpeer_addr saddr, daddr;
d23ff701
JA
916 unsigned int hash;
917 struct net *net = genl_info_net(info);
918 int ret;
00ca9c5b 919 bool src = true, found = false;
d23ff701 920
324fd55a 921 ret = parse_nl_addr(info, &daddr, &hash, 1);
d23ff701
JA
922 if (ret < 0)
923 return ret;
8a4bff71
EB
924 if (ret > 0) {
925 tcp_metrics_flush_all(net);
926 return 0;
927 }
3e7013dd
CP
928 ret = parse_nl_saddr(info, &saddr);
929 if (ret < 0)
930 src = false;
d23ff701 931
3e5da62d 932 hash ^= net_hash_mix(net);
098a697b
EB
933 hash = hash_32(hash, tcp_metrics_hash_log);
934 hb = tcp_metrics_hash + hash;
d23ff701
JA
935 pp = &hb->chain;
936 spin_lock_bh(&tcp_metrics_lock);
9f1ab186 937 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
3e7013dd 938 if (addr_same(&tm->tcpm_daddr, &daddr) &&
849e8a0c
EB
939 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
940 net_eq(tm_net(tm), net)) {
d23ff701 941 *pp = tm->tcpm_next;
00ca9c5b
CP
942 kfree_rcu(tm, rcu_head);
943 found = true;
bbf852b9
CP
944 } else {
945 pp = &tm->tcpm_next;
d23ff701
JA
946 }
947 }
948 spin_unlock_bh(&tcp_metrics_lock);
00ca9c5b 949 if (!found)
d23ff701 950 return -ESRCH;
d23ff701
JA
951 return 0;
952}
953
4534de83 954static const struct genl_ops tcp_metrics_nl_ops[] = {
d23ff701
JA
955 {
956 .cmd = TCP_METRICS_CMD_GET,
957 .doit = tcp_metrics_nl_cmd_get,
958 .dumpit = tcp_metrics_nl_dump,
959 .policy = tcp_metrics_nl_policy,
d23ff701
JA
960 },
961 {
962 .cmd = TCP_METRICS_CMD_DEL,
963 .doit = tcp_metrics_nl_cmd_del,
964 .policy = tcp_metrics_nl_policy,
965 .flags = GENL_ADMIN_PERM,
966 },
967};
968
56989f6d 969static struct genl_family tcp_metrics_nl_family __ro_after_init = {
489111e5
JB
970 .hdrsize = 0,
971 .name = TCP_METRICS_GENL_NAME,
972 .version = TCP_METRICS_GENL_VERSION,
973 .maxattr = TCP_METRICS_ATTR_MAX,
974 .netnsok = true,
975 .module = THIS_MODULE,
976 .ops = tcp_metrics_nl_ops,
977 .n_ops = ARRAY_SIZE(tcp_metrics_nl_ops),
978};
979
5815d5e7 980static unsigned int tcpmhash_entries;
51c5d0c4
DM
981static int __init set_tcpmhash_entries(char *str)
982{
983 ssize_t ret;
984
985 if (!str)
986 return 0;
987
5815d5e7 988 ret = kstrtouint(str, 0, &tcpmhash_entries);
51c5d0c4
DM
989 if (ret)
990 return 0;
991
992 return 1;
993}
994__setup("tcpmhash_entries=", set_tcpmhash_entries);
995
996static int __net_init tcp_net_metrics_init(struct net *net)
997{
5815d5e7
ED
998 size_t size;
999 unsigned int slots;
51c5d0c4 1000
098a697b
EB
1001 if (!net_eq(net, &init_net))
1002 return 0;
1003
51c5d0c4
DM
1004 slots = tcpmhash_entries;
1005 if (!slots) {
1006 if (totalram_pages >= 128 * 1024)
1007 slots = 16 * 1024;
1008 else
1009 slots = 8 * 1024;
1010 }
1011
098a697b
EB
1012 tcp_metrics_hash_log = order_base_2(slots);
1013 size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log;
51c5d0c4 1014
752ade68 1015 tcp_metrics_hash = kvzalloc(size, GFP_KERNEL);
098a697b 1016 if (!tcp_metrics_hash)
51c5d0c4
DM
1017 return -ENOMEM;
1018
51c5d0c4
DM
1019 return 0;
1020}
1021
1022static void __net_exit tcp_net_metrics_exit(struct net *net)
1023{
098a697b 1024 tcp_metrics_flush_all(net);
51c5d0c4
DM
1025}
1026
1027static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
1028 .init = tcp_net_metrics_init,
1029 .exit = tcp_net_metrics_exit,
1030};
1031
1032void __init tcp_metrics_init(void)
1033{
d23ff701
JA
1034 int ret;
1035
1036 ret = register_pernet_subsys(&tcp_net_metrics_ops);
1037 if (ret < 0)
6493517e
EB
1038 panic("Could not allocate the tcp_metrics hash table\n");
1039
489111e5 1040 ret = genl_register_family(&tcp_metrics_nl_family);
d23ff701 1041 if (ret < 0)
6493517e 1042 panic("Could not register tcp_metrics generic netlink\n");
51c5d0c4 1043}