tcp_metrics.c 28 KB

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