tcp_metrics.c 27 KB

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