1 #include <linux/rcupdate.h>
2 #include <linux/spinlock.h>
3 #include <linux/jiffies.h>
4 #include <linux/bootmem.h>
5 #include <linux/module.h>
6 #include <linux/cache.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
11 #include <net/inet_connection_sock.h>
12 #include <net/net_namespace.h>
13 #include <net/request_sock.h>
14 #include <net/inetpeer.h>
20 int sysctl_tcp_nometrics_save __read_mostly;
22 enum tcp_metric_index {
27 TCP_METRIC_REORDERING,
33 struct tcp_metrics_block {
34 struct tcp_metrics_block __rcu *tcpm_next;
35 struct inetpeer_addr tcpm_addr;
36 unsigned long tcpm_stamp;
40 u32 tcpm_vals[TCP_METRIC_MAX];
43 static bool tcp_metric_locked(struct tcp_metrics_block *tm,
44 enum tcp_metric_index idx)
46 return tm->tcpm_lock & (1 << idx);
49 static u32 tcp_metric_get(struct tcp_metrics_block *tm,
50 enum tcp_metric_index idx)
52 return tm->tcpm_vals[idx];
55 static u32 tcp_metric_get_jiffies(struct tcp_metrics_block *tm,
56 enum tcp_metric_index idx)
58 return msecs_to_jiffies(tm->tcpm_vals[idx]);
61 static void tcp_metric_set(struct tcp_metrics_block *tm,
62 enum tcp_metric_index idx,
65 tm->tcpm_vals[idx] = val;
68 static void tcp_metric_set_msecs(struct tcp_metrics_block *tm,
69 enum tcp_metric_index idx,
72 tm->tcpm_vals[idx] = jiffies_to_msecs(val);
75 static bool addr_same(const struct inetpeer_addr *a,
76 const struct inetpeer_addr *b)
78 const struct in6_addr *a6, *b6;
80 if (a->family != b->family)
82 if (a->family == AF_INET)
83 return a->addr.a4 == b->addr.a4;
85 a6 = (const struct in6_addr *) &a->addr.a6[0];
86 b6 = (const struct in6_addr *) &b->addr.a6[0];
88 return ipv6_addr_equal(a6, b6);
91 struct tcpm_hash_bucket {
92 struct tcp_metrics_block __rcu *chain;
95 static DEFINE_SPINLOCK(tcp_metrics_lock);
97 static void tcpm_suck_dst(struct tcp_metrics_block *tm, struct dst_entry *dst)
102 if (dst_metric_locked(dst, RTAX_RTT))
103 val |= 1 << TCP_METRIC_RTT;
104 if (dst_metric_locked(dst, RTAX_RTTVAR))
105 val |= 1 << TCP_METRIC_RTTVAR;
106 if (dst_metric_locked(dst, RTAX_SSTHRESH))
107 val |= 1 << TCP_METRIC_SSTHRESH;
108 if (dst_metric_locked(dst, RTAX_CWND))
109 val |= 1 << TCP_METRIC_CWND;
110 if (dst_metric_locked(dst, RTAX_REORDERING))
111 val |= 1 << TCP_METRIC_REORDERING;
114 tm->tcpm_vals[TCP_METRIC_RTT] = dst_metric_raw(dst, RTAX_RTT);
115 tm->tcpm_vals[TCP_METRIC_RTTVAR] = dst_metric_raw(dst, RTAX_RTTVAR);
116 tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
117 tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
118 tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
120 tm->tcpm_ts_stamp = 0;
123 static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
124 struct inetpeer_addr *addr,
128 struct tcp_metrics_block *tm;
131 spin_lock_bh(&tcp_metrics_lock);
132 net = dev_net(dst->dev);
133 if (unlikely(reclaim)) {
134 struct tcp_metrics_block *oldest;
136 oldest = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain);
137 for (tm = rcu_dereference(oldest->tcpm_next); tm;
138 tm = rcu_dereference(tm->tcpm_next)) {
139 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
144 tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
148 tm->tcpm_addr = *addr;
149 tm->tcpm_stamp = jiffies;
151 tcpm_suck_dst(tm, dst);
153 if (likely(!reclaim)) {
154 tm->tcpm_next = net->ipv4.tcp_metrics_hash[hash].chain;
155 rcu_assign_pointer(net->ipv4.tcp_metrics_hash[hash].chain, tm);
159 spin_unlock_bh(&tcp_metrics_lock);
163 #define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
165 static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
167 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
168 tcpm_suck_dst(tm, dst);
171 #define TCP_METRICS_RECLAIM_DEPTH 5
172 #define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
174 static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
178 if (depth > TCP_METRICS_RECLAIM_DEPTH)
179 return TCP_METRICS_RECLAIM_PTR;
183 static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *addr,
184 struct net *net, unsigned int hash)
186 struct tcp_metrics_block *tm;
189 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
190 tm = rcu_dereference(tm->tcpm_next)) {
191 if (addr_same(&tm->tcpm_addr, addr))
195 return tcp_get_encode(tm, depth);
198 static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
199 struct dst_entry *dst)
201 struct tcp_metrics_block *tm;
202 struct inetpeer_addr addr;
206 addr.family = req->rsk_ops->family;
207 switch (addr.family) {
209 addr.addr.a4 = inet_rsk(req)->rmt_addr;
210 hash = (__force unsigned int) addr.addr.a4;
213 *(struct in6_addr *)addr.addr.a6 = inet6_rsk(req)->rmt_addr;
214 hash = ipv6_addr_hash(&inet6_rsk(req)->rmt_addr);
220 hash ^= (hash >> 24) ^ (hash >> 16) ^ (hash >> 8);
222 net = dev_net(dst->dev);
223 hash &= net->ipv4.tcp_metrics_hash_mask;
225 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
226 tm = rcu_dereference(tm->tcpm_next)) {
227 if (addr_same(&tm->tcpm_addr, &addr))
230 tcpm_check_stamp(tm, dst);
234 static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
236 struct inet6_timewait_sock *tw6;
237 struct tcp_metrics_block *tm;
238 struct inetpeer_addr addr;
242 addr.family = tw->tw_family;
243 switch (addr.family) {
245 addr.addr.a4 = tw->tw_daddr;
246 hash = (__force unsigned int) addr.addr.a4;
249 tw6 = inet6_twsk((struct sock *)tw);
250 *(struct in6_addr *)addr.addr.a6 = tw6->tw_v6_daddr;
251 hash = ipv6_addr_hash(&tw6->tw_v6_daddr);
257 hash ^= (hash >> 24) ^ (hash >> 16) ^ (hash >> 8);
260 hash &= net->ipv4.tcp_metrics_hash_mask;
262 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
263 tm = rcu_dereference(tm->tcpm_next)) {
264 if (addr_same(&tm->tcpm_addr, &addr))
270 static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
271 struct dst_entry *dst,
274 struct tcp_metrics_block *tm;
275 struct inetpeer_addr addr;
280 addr.family = sk->sk_family;
281 switch (addr.family) {
283 addr.addr.a4 = inet_sk(sk)->inet_daddr;
284 hash = (__force unsigned int) addr.addr.a4;
287 *(struct in6_addr *)addr.addr.a6 = inet6_sk(sk)->daddr;
288 hash = ipv6_addr_hash(&inet6_sk(sk)->daddr);
294 hash ^= (hash >> 24) ^ (hash >> 16) ^ (hash >> 8);
296 net = dev_net(dst->dev);
297 hash &= net->ipv4.tcp_metrics_hash_mask;
299 tm = __tcp_get_metrics(&addr, net, hash);
301 if (tm == TCP_METRICS_RECLAIM_PTR) {
306 tm = tcpm_new(dst, &addr, hash, reclaim);
308 tcpm_check_stamp(tm, dst);
313 /* Save metrics learned by this TCP session. This function is called
314 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
315 * or goes from LAST-ACK to CLOSE.
317 void tcp_update_metrics(struct sock *sk)
319 const struct inet_connection_sock *icsk = inet_csk(sk);
320 struct dst_entry *dst = __sk_dst_get(sk);
321 struct tcp_sock *tp = tcp_sk(sk);
322 struct tcp_metrics_block *tm;
327 if (sysctl_tcp_nometrics_save || !dst)
330 if (dst->flags & DST_HOST)
334 if (icsk->icsk_backoff || !tp->srtt) {
335 /* This session failed to estimate rtt. Why?
336 * Probably, no packets returned in time. Reset our
339 tm = tcp_get_metrics(sk, dst, false);
340 if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
341 tcp_metric_set(tm, TCP_METRIC_RTT, 0);
344 tm = tcp_get_metrics(sk, dst, true);
349 rtt = tcp_metric_get_jiffies(tm, TCP_METRIC_RTT);
352 /* If newly calculated rtt larger than stored one, store new
353 * one. Otherwise, use EWMA. Remember, rtt overestimation is
354 * always better than underestimation.
356 if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
361 tcp_metric_set_msecs(tm, TCP_METRIC_RTT, rtt);
364 if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
370 /* Scale deviation to rttvar fixed point */
375 var = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
379 var -= (var - m) >> 2;
381 tcp_metric_set_msecs(tm, TCP_METRIC_RTTVAR, var);
384 if (tcp_in_initial_slowstart(tp)) {
385 /* Slow start still did not finish. */
386 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
387 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
388 if (val && (tp->snd_cwnd >> 1) > val)
389 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
392 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
393 val = tcp_metric_get(tm, TCP_METRIC_CWND);
394 if (tp->snd_cwnd > val)
395 tcp_metric_set(tm, TCP_METRIC_CWND,
398 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
399 icsk->icsk_ca_state == TCP_CA_Open) {
400 /* Cong. avoidance phase, cwnd is reliable. */
401 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
402 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
403 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
404 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
405 val = tcp_metric_get(tm, TCP_METRIC_CWND);
406 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
409 /* Else slow start did not finish, cwnd is non-sense,
410 * ssthresh may be also invalid.
412 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
413 val = tcp_metric_get(tm, TCP_METRIC_CWND);
414 tcp_metric_set(tm, TCP_METRIC_CWND,
415 (val + tp->snd_ssthresh) >> 1);
417 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
418 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
419 if (val && tp->snd_ssthresh > val)
420 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
423 if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
424 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
425 if (val < tp->reordering &&
426 tp->reordering != sysctl_tcp_reordering)
427 tcp_metric_set(tm, TCP_METRIC_REORDERING,
431 tm->tcpm_stamp = jiffies;
436 /* Initialize metrics on socket. */
438 void tcp_init_metrics(struct sock *sk)
440 struct dst_entry *dst = __sk_dst_get(sk);
441 struct tcp_sock *tp = tcp_sk(sk);
442 struct tcp_metrics_block *tm;
451 tm = tcp_get_metrics(sk, dst, true);
457 if (tcp_metric_locked(tm, TCP_METRIC_CWND))
458 tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
460 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
462 tp->snd_ssthresh = val;
463 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
464 tp->snd_ssthresh = tp->snd_cwnd_clamp;
466 /* ssthresh may have been reduced unnecessarily during.
467 * 3WHS. Restore it back to its initial default.
469 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
471 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
472 if (val && tp->reordering != val) {
473 tcp_disable_fack(tp);
474 tcp_disable_early_retrans(tp);
475 tp->reordering = val;
478 val = tcp_metric_get(tm, TCP_METRIC_RTT);
479 if (val == 0 || tp->srtt == 0) {
483 /* Initial rtt is determined from SYN,SYN-ACK.
484 * The segment is small and rtt may appear much
485 * less than real one. Use per-dst memory
486 * to make it more realistic.
488 * A bit of theory. RTT is time passed after "normal" sized packet
489 * is sent until it is ACKed. In normal circumstances sending small
490 * packets force peer to delay ACKs and calculation is correct too.
491 * The algorithm is adaptive and, provided we follow specs, it
492 * NEVER underestimate RTT. BUT! If peer tries to make some clever
493 * tricks sort of "quick acks" for time long enough to decrease RTT
494 * to low value, and then abruptly stops to do it and starts to delay
495 * ACKs, wait for troubles.
497 val = msecs_to_jiffies(val);
498 if (val > tp->srtt) {
500 tp->rtt_seq = tp->snd_nxt;
502 val = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
503 if (val > tp->mdev) {
505 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
512 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
513 * 3WHS. This is most likely due to retransmission,
514 * including spurious one. Reset the RTO back to 3secs
515 * from the more aggressive 1sec to avoid more spurious
518 tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_FALLBACK;
519 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
521 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
522 * retransmitted. In light of RFC6298 more aggressive 1sec
523 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
524 * retransmission has occurred.
526 if (tp->total_retrans > 1)
529 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
530 tp->snd_cwnd_stamp = tcp_time_stamp;
533 bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check)
535 struct tcp_metrics_block *tm;
542 tm = __tcp_get_metrics_req(req, dst);
545 (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
546 (s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW)
551 if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
560 EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
562 void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
564 struct tcp_metrics_block *tm;
567 tm = tcp_get_metrics(sk, dst, true);
569 struct tcp_sock *tp = tcp_sk(sk);
571 if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
572 tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
573 tp->rx_opt.ts_recent = tm->tcpm_ts;
578 EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
580 /* VJ's idea. Save last timestamp seen from this destination and hold
581 * it at least for normal timewait interval to use for duplicate
582 * segment detection in subsequent connections, before they enter
583 * synchronized state.
585 bool tcp_remember_stamp(struct sock *sk)
587 struct dst_entry *dst = __sk_dst_get(sk);
591 struct tcp_metrics_block *tm;
594 tm = tcp_get_metrics(sk, dst, true);
596 struct tcp_sock *tp = tcp_sk(sk);
598 if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
599 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
600 tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
601 tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
602 tm->tcpm_ts = tp->rx_opt.ts_recent;
611 bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
613 struct tcp_metrics_block *tm;
617 tm = __tcp_get_metrics_tw(tw);
619 const struct tcp_timewait_sock *tcptw;
620 struct sock *sk = (struct sock *) tw;
622 tcptw = tcp_twsk(sk);
623 if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
624 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
625 tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
626 tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
627 tm->tcpm_ts = tcptw->tw_ts_recent;
636 static unsigned long tcpmhash_entries;
637 static int __init set_tcpmhash_entries(char *str)
644 ret = kstrtoul(str, 0, &tcpmhash_entries);
650 __setup("tcpmhash_entries=", set_tcpmhash_entries);
652 static int __net_init tcp_net_metrics_init(struct net *net)
656 slots = tcpmhash_entries;
658 if (totalram_pages >= 128 * 1024)
664 size = slots * sizeof(struct tcpm_hash_bucket);
666 net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL);
667 if (!net->ipv4.tcp_metrics_hash)
670 net->ipv4.tcp_metrics_hash_mask = (slots - 1);
675 static void __net_exit tcp_net_metrics_exit(struct net *net)
677 kfree(net->ipv4.tcp_metrics_hash);
680 static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
681 .init = tcp_net_metrics_init,
682 .exit = tcp_net_metrics_exit,
685 void __init tcp_metrics_init(void)
687 register_pernet_subsys(&tcp_net_metrics_ops);