tcp_metrics.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include <linux/cache.h>
  2. #include <linux/tcp.h>
  3. #include <net/inet_connection_sock.h>
  4. #include <net/sock.h>
  5. #include <net/dst.h>
  6. #include <net/tcp.h>
  7. int sysctl_tcp_nometrics_save __read_mostly;
  8. /* Save metrics learned by this TCP session. This function is called
  9. * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
  10. * or goes from LAST-ACK to CLOSE.
  11. */
  12. void tcp_update_metrics(struct sock *sk)
  13. {
  14. struct tcp_sock *tp = tcp_sk(sk);
  15. struct dst_entry *dst = __sk_dst_get(sk);
  16. if (sysctl_tcp_nometrics_save)
  17. return;
  18. if (dst && (dst->flags & DST_HOST)) {
  19. const struct inet_connection_sock *icsk = inet_csk(sk);
  20. int m;
  21. unsigned long rtt;
  22. dst_confirm(dst);
  23. if (icsk->icsk_backoff || !tp->srtt) {
  24. /* This session failed to estimate rtt. Why?
  25. * Probably, no packets returned in time.
  26. * Reset our results.
  27. */
  28. if (!(dst_metric_locked(dst, RTAX_RTT)))
  29. dst_metric_set(dst, RTAX_RTT, 0);
  30. return;
  31. }
  32. rtt = dst_metric_rtt(dst, RTAX_RTT);
  33. m = rtt - tp->srtt;
  34. /* If newly calculated rtt larger than stored one,
  35. * store new one. Otherwise, use EWMA. Remember,
  36. * rtt overestimation is always better than underestimation.
  37. */
  38. if (!(dst_metric_locked(dst, RTAX_RTT))) {
  39. if (m <= 0)
  40. set_dst_metric_rtt(dst, RTAX_RTT, tp->srtt);
  41. else
  42. set_dst_metric_rtt(dst, RTAX_RTT, rtt - (m >> 3));
  43. }
  44. if (!(dst_metric_locked(dst, RTAX_RTTVAR))) {
  45. unsigned long var;
  46. if (m < 0)
  47. m = -m;
  48. /* Scale deviation to rttvar fixed point */
  49. m >>= 1;
  50. if (m < tp->mdev)
  51. m = tp->mdev;
  52. var = dst_metric_rtt(dst, RTAX_RTTVAR);
  53. if (m >= var)
  54. var = m;
  55. else
  56. var -= (var - m) >> 2;
  57. set_dst_metric_rtt(dst, RTAX_RTTVAR, var);
  58. }
  59. if (tcp_in_initial_slowstart(tp)) {
  60. /* Slow start still did not finish. */
  61. if (dst_metric(dst, RTAX_SSTHRESH) &&
  62. !dst_metric_locked(dst, RTAX_SSTHRESH) &&
  63. (tp->snd_cwnd >> 1) > dst_metric(dst, RTAX_SSTHRESH))
  64. dst_metric_set(dst, RTAX_SSTHRESH, tp->snd_cwnd >> 1);
  65. if (!dst_metric_locked(dst, RTAX_CWND) &&
  66. tp->snd_cwnd > dst_metric(dst, RTAX_CWND))
  67. dst_metric_set(dst, RTAX_CWND, tp->snd_cwnd);
  68. } else if (tp->snd_cwnd > tp->snd_ssthresh &&
  69. icsk->icsk_ca_state == TCP_CA_Open) {
  70. /* Cong. avoidance phase, cwnd is reliable. */
  71. if (!dst_metric_locked(dst, RTAX_SSTHRESH))
  72. dst_metric_set(dst, RTAX_SSTHRESH,
  73. max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
  74. if (!dst_metric_locked(dst, RTAX_CWND))
  75. dst_metric_set(dst, RTAX_CWND,
  76. (dst_metric(dst, RTAX_CWND) +
  77. tp->snd_cwnd) >> 1);
  78. } else {
  79. /* Else slow start did not finish, cwnd is non-sense,
  80. ssthresh may be also invalid.
  81. */
  82. if (!dst_metric_locked(dst, RTAX_CWND))
  83. dst_metric_set(dst, RTAX_CWND,
  84. (dst_metric(dst, RTAX_CWND) +
  85. tp->snd_ssthresh) >> 1);
  86. if (dst_metric(dst, RTAX_SSTHRESH) &&
  87. !dst_metric_locked(dst, RTAX_SSTHRESH) &&
  88. tp->snd_ssthresh > dst_metric(dst, RTAX_SSTHRESH))
  89. dst_metric_set(dst, RTAX_SSTHRESH, tp->snd_ssthresh);
  90. }
  91. if (!dst_metric_locked(dst, RTAX_REORDERING)) {
  92. if (dst_metric(dst, RTAX_REORDERING) < tp->reordering &&
  93. tp->reordering != sysctl_tcp_reordering)
  94. dst_metric_set(dst, RTAX_REORDERING, tp->reordering);
  95. }
  96. }
  97. }
  98. /* Initialize metrics on socket. */
  99. void tcp_init_metrics(struct sock *sk)
  100. {
  101. struct tcp_sock *tp = tcp_sk(sk);
  102. struct dst_entry *dst = __sk_dst_get(sk);
  103. if (dst == NULL)
  104. goto reset;
  105. dst_confirm(dst);
  106. if (dst_metric_locked(dst, RTAX_CWND))
  107. tp->snd_cwnd_clamp = dst_metric(dst, RTAX_CWND);
  108. if (dst_metric(dst, RTAX_SSTHRESH)) {
  109. tp->snd_ssthresh = dst_metric(dst, RTAX_SSTHRESH);
  110. if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
  111. tp->snd_ssthresh = tp->snd_cwnd_clamp;
  112. } else {
  113. /* ssthresh may have been reduced unnecessarily during.
  114. * 3WHS. Restore it back to its initial default.
  115. */
  116. tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  117. }
  118. if (dst_metric(dst, RTAX_REORDERING) &&
  119. tp->reordering != dst_metric(dst, RTAX_REORDERING)) {
  120. tcp_disable_fack(tp);
  121. tcp_disable_early_retrans(tp);
  122. tp->reordering = dst_metric(dst, RTAX_REORDERING);
  123. }
  124. if (dst_metric(dst, RTAX_RTT) == 0 || tp->srtt == 0)
  125. goto reset;
  126. /* Initial rtt is determined from SYN,SYN-ACK.
  127. * The segment is small and rtt may appear much
  128. * less than real one. Use per-dst memory
  129. * to make it more realistic.
  130. *
  131. * A bit of theory. RTT is time passed after "normal" sized packet
  132. * is sent until it is ACKed. In normal circumstances sending small
  133. * packets force peer to delay ACKs and calculation is correct too.
  134. * The algorithm is adaptive and, provided we follow specs, it
  135. * NEVER underestimate RTT. BUT! If peer tries to make some clever
  136. * tricks sort of "quick acks" for time long enough to decrease RTT
  137. * to low value, and then abruptly stops to do it and starts to delay
  138. * ACKs, wait for troubles.
  139. */
  140. if (dst_metric_rtt(dst, RTAX_RTT) > tp->srtt) {
  141. tp->srtt = dst_metric_rtt(dst, RTAX_RTT);
  142. tp->rtt_seq = tp->snd_nxt;
  143. }
  144. if (dst_metric_rtt(dst, RTAX_RTTVAR) > tp->mdev) {
  145. tp->mdev = dst_metric_rtt(dst, RTAX_RTTVAR);
  146. tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
  147. }
  148. tcp_set_rto(sk);
  149. reset:
  150. if (tp->srtt == 0) {
  151. /* RFC6298: 5.7 We've failed to get a valid RTT sample from
  152. * 3WHS. This is most likely due to retransmission,
  153. * including spurious one. Reset the RTO back to 3secs
  154. * from the more aggressive 1sec to avoid more spurious
  155. * retransmission.
  156. */
  157. tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_FALLBACK;
  158. inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
  159. }
  160. /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
  161. * retransmitted. In light of RFC6298 more aggressive 1sec
  162. * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
  163. * retransmission has occurred.
  164. */
  165. if (tp->total_retrans > 1)
  166. tp->snd_cwnd = 1;
  167. else
  168. tp->snd_cwnd = tcp_init_cwnd(tp, dst);
  169. tp->snd_cwnd_stamp = tcp_time_stamp;
  170. }