tcp_metrics.c 27 KB

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