tcp_metrics.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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)->rmt_addr;
  185. hash = (__force unsigned int) addr.addr.a4;
  186. break;
  187. case AF_INET6:
  188. *(struct in6_addr *)addr.addr.a6 = inet6_rsk(req)->rmt_addr;
  189. hash = ipv6_addr_hash(&inet6_rsk(req)->rmt_addr);
  190. break;
  191. default:
  192. return NULL;
  193. }
  194. net = dev_net(dst->dev);
  195. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  196. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  197. tm = rcu_dereference(tm->tcpm_next)) {
  198. if (addr_same(&tm->tcpm_addr, &addr))
  199. break;
  200. }
  201. tcpm_check_stamp(tm, dst);
  202. return tm;
  203. }
  204. static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
  205. {
  206. struct inet6_timewait_sock *tw6;
  207. struct tcp_metrics_block *tm;
  208. struct inetpeer_addr addr;
  209. unsigned int hash;
  210. struct net *net;
  211. addr.family = tw->tw_family;
  212. switch (addr.family) {
  213. case AF_INET:
  214. addr.addr.a4 = tw->tw_daddr;
  215. hash = (__force unsigned int) addr.addr.a4;
  216. break;
  217. case AF_INET6:
  218. tw6 = inet6_twsk((struct sock *)tw);
  219. *(struct in6_addr *)addr.addr.a6 = tw6->tw_v6_daddr;
  220. hash = ipv6_addr_hash(&tw6->tw_v6_daddr);
  221. break;
  222. default:
  223. return NULL;
  224. }
  225. net = twsk_net(tw);
  226. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  227. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  228. tm = rcu_dereference(tm->tcpm_next)) {
  229. if (addr_same(&tm->tcpm_addr, &addr))
  230. break;
  231. }
  232. return tm;
  233. }
  234. static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
  235. struct dst_entry *dst,
  236. bool create)
  237. {
  238. struct tcp_metrics_block *tm;
  239. struct inetpeer_addr addr;
  240. unsigned int hash;
  241. struct net *net;
  242. bool reclaim;
  243. addr.family = sk->sk_family;
  244. switch (addr.family) {
  245. case AF_INET:
  246. addr.addr.a4 = inet_sk(sk)->inet_daddr;
  247. hash = (__force unsigned int) addr.addr.a4;
  248. break;
  249. case AF_INET6:
  250. *(struct in6_addr *)addr.addr.a6 = inet6_sk(sk)->daddr;
  251. hash = ipv6_addr_hash(&inet6_sk(sk)->daddr);
  252. break;
  253. default:
  254. return NULL;
  255. }
  256. net = dev_net(dst->dev);
  257. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  258. tm = __tcp_get_metrics(&addr, net, hash);
  259. reclaim = false;
  260. if (tm == TCP_METRICS_RECLAIM_PTR) {
  261. reclaim = true;
  262. tm = NULL;
  263. }
  264. if (!tm && create)
  265. tm = tcpm_new(dst, &addr, hash, reclaim);
  266. else
  267. tcpm_check_stamp(tm, dst);
  268. return tm;
  269. }
  270. /* Save metrics learned by this TCP session. This function is called
  271. * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
  272. * or goes from LAST-ACK to CLOSE.
  273. */
  274. void tcp_update_metrics(struct sock *sk)
  275. {
  276. const struct inet_connection_sock *icsk = inet_csk(sk);
  277. struct dst_entry *dst = __sk_dst_get(sk);
  278. struct tcp_sock *tp = tcp_sk(sk);
  279. struct tcp_metrics_block *tm;
  280. unsigned long rtt;
  281. u32 val;
  282. int m;
  283. if (sysctl_tcp_nometrics_save || !dst)
  284. return;
  285. if (dst->flags & DST_HOST)
  286. dst_confirm(dst);
  287. rcu_read_lock();
  288. if (icsk->icsk_backoff || !tp->srtt) {
  289. /* This session failed to estimate rtt. Why?
  290. * Probably, no packets returned in time. Reset our
  291. * results.
  292. */
  293. tm = tcp_get_metrics(sk, dst, false);
  294. if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
  295. tcp_metric_set(tm, TCP_METRIC_RTT, 0);
  296. goto out_unlock;
  297. } else
  298. tm = tcp_get_metrics(sk, dst, true);
  299. if (!tm)
  300. goto out_unlock;
  301. rtt = tcp_metric_get_jiffies(tm, TCP_METRIC_RTT);
  302. m = rtt - tp->srtt;
  303. /* If newly calculated rtt larger than stored one, store new
  304. * one. Otherwise, use EWMA. Remember, rtt overestimation is
  305. * always better than underestimation.
  306. */
  307. if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
  308. if (m <= 0)
  309. rtt = tp->srtt;
  310. else
  311. rtt -= (m >> 3);
  312. tcp_metric_set_msecs(tm, TCP_METRIC_RTT, rtt);
  313. }
  314. if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
  315. unsigned long var;
  316. if (m < 0)
  317. m = -m;
  318. /* Scale deviation to rttvar fixed point */
  319. m >>= 1;
  320. if (m < tp->mdev)
  321. m = tp->mdev;
  322. var = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
  323. if (m >= var)
  324. var = m;
  325. else
  326. var -= (var - m) >> 2;
  327. tcp_metric_set_msecs(tm, TCP_METRIC_RTTVAR, var);
  328. }
  329. if (tcp_in_initial_slowstart(tp)) {
  330. /* Slow start still did not finish. */
  331. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
  332. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  333. if (val && (tp->snd_cwnd >> 1) > val)
  334. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  335. tp->snd_cwnd >> 1);
  336. }
  337. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  338. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  339. if (tp->snd_cwnd > val)
  340. tcp_metric_set(tm, TCP_METRIC_CWND,
  341. tp->snd_cwnd);
  342. }
  343. } else if (tp->snd_cwnd > tp->snd_ssthresh &&
  344. icsk->icsk_ca_state == TCP_CA_Open) {
  345. /* Cong. avoidance phase, cwnd is reliable. */
  346. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
  347. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  348. max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
  349. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  350. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  351. tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
  352. }
  353. } else {
  354. /* Else slow start did not finish, cwnd is non-sense,
  355. * ssthresh may be also invalid.
  356. */
  357. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  358. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  359. tcp_metric_set(tm, TCP_METRIC_CWND,
  360. (val + tp->snd_ssthresh) >> 1);
  361. }
  362. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
  363. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  364. if (val && tp->snd_ssthresh > val)
  365. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  366. tp->snd_ssthresh);
  367. }
  368. if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
  369. val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
  370. if (val < tp->reordering &&
  371. tp->reordering != sysctl_tcp_reordering)
  372. tcp_metric_set(tm, TCP_METRIC_REORDERING,
  373. tp->reordering);
  374. }
  375. }
  376. tm->tcpm_stamp = jiffies;
  377. out_unlock:
  378. rcu_read_unlock();
  379. }
  380. /* Initialize metrics on socket. */
  381. void tcp_init_metrics(struct sock *sk)
  382. {
  383. struct dst_entry *dst = __sk_dst_get(sk);
  384. struct tcp_sock *tp = tcp_sk(sk);
  385. struct tcp_metrics_block *tm;
  386. u32 val, crtt = 0; /* cached RTT scaled by 8 */
  387. if (dst == NULL)
  388. goto reset;
  389. dst_confirm(dst);
  390. rcu_read_lock();
  391. tm = tcp_get_metrics(sk, dst, true);
  392. if (!tm) {
  393. rcu_read_unlock();
  394. goto reset;
  395. }
  396. if (tcp_metric_locked(tm, TCP_METRIC_CWND))
  397. tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
  398. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  399. if (val) {
  400. tp->snd_ssthresh = val;
  401. if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
  402. tp->snd_ssthresh = tp->snd_cwnd_clamp;
  403. } else {
  404. /* ssthresh may have been reduced unnecessarily during.
  405. * 3WHS. Restore it back to its initial default.
  406. */
  407. tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  408. }
  409. val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
  410. if (val && tp->reordering != val) {
  411. tcp_disable_fack(tp);
  412. tcp_disable_early_retrans(tp);
  413. tp->reordering = val;
  414. }
  415. crtt = tcp_metric_get_jiffies(tm, TCP_METRIC_RTT);
  416. rcu_read_unlock();
  417. reset:
  418. if (crtt > tp->srtt) {
  419. /* Initial RTT (tp->srtt) from SYN usually don't measure
  420. * serialization delay on low BW links well so RTO may be
  421. * under-estimated. Stay conservative and seed RTO with
  422. * the RTTs from past data exchanges, using the same seeding
  423. * formula in tcp_rtt_estimator().
  424. */
  425. inet_csk(sk)->icsk_rto = crtt + max(crtt >> 2, tcp_rto_min(sk));
  426. } else if (tp->srtt == 0) {
  427. /* RFC6298: 5.7 We've failed to get a valid RTT sample from
  428. * 3WHS. This is most likely due to retransmission,
  429. * including spurious one. Reset the RTO back to 3secs
  430. * from the more aggressive 1sec to avoid more spurious
  431. * retransmission.
  432. */
  433. tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_FALLBACK;
  434. inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
  435. }
  436. /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
  437. * retransmitted. In light of RFC6298 more aggressive 1sec
  438. * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
  439. * retransmission has occurred.
  440. */
  441. if (tp->total_retrans > 1)
  442. tp->snd_cwnd = 1;
  443. else
  444. tp->snd_cwnd = tcp_init_cwnd(tp, dst);
  445. tp->snd_cwnd_stamp = tcp_time_stamp;
  446. }
  447. bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check)
  448. {
  449. struct tcp_metrics_block *tm;
  450. bool ret;
  451. if (!dst)
  452. return false;
  453. rcu_read_lock();
  454. tm = __tcp_get_metrics_req(req, dst);
  455. if (paws_check) {
  456. if (tm &&
  457. (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
  458. (s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW)
  459. ret = false;
  460. else
  461. ret = true;
  462. } else {
  463. if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
  464. ret = true;
  465. else
  466. ret = false;
  467. }
  468. rcu_read_unlock();
  469. return ret;
  470. }
  471. EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
  472. void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
  473. {
  474. struct tcp_metrics_block *tm;
  475. rcu_read_lock();
  476. tm = tcp_get_metrics(sk, dst, true);
  477. if (tm) {
  478. struct tcp_sock *tp = tcp_sk(sk);
  479. if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
  480. tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
  481. tp->rx_opt.ts_recent = tm->tcpm_ts;
  482. }
  483. }
  484. rcu_read_unlock();
  485. }
  486. EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
  487. /* VJ's idea. Save last timestamp seen from this destination and hold
  488. * it at least for normal timewait interval to use for duplicate
  489. * segment detection in subsequent connections, before they enter
  490. * synchronized state.
  491. */
  492. bool tcp_remember_stamp(struct sock *sk)
  493. {
  494. struct dst_entry *dst = __sk_dst_get(sk);
  495. bool ret = false;
  496. if (dst) {
  497. struct tcp_metrics_block *tm;
  498. rcu_read_lock();
  499. tm = tcp_get_metrics(sk, dst, true);
  500. if (tm) {
  501. struct tcp_sock *tp = tcp_sk(sk);
  502. if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
  503. ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
  504. tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
  505. tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
  506. tm->tcpm_ts = tp->rx_opt.ts_recent;
  507. }
  508. ret = true;
  509. }
  510. rcu_read_unlock();
  511. }
  512. return ret;
  513. }
  514. bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
  515. {
  516. struct tcp_metrics_block *tm;
  517. bool ret = false;
  518. rcu_read_lock();
  519. tm = __tcp_get_metrics_tw(tw);
  520. if (tm) {
  521. const struct tcp_timewait_sock *tcptw;
  522. struct sock *sk = (struct sock *) tw;
  523. tcptw = tcp_twsk(sk);
  524. if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
  525. ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
  526. tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
  527. tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
  528. tm->tcpm_ts = tcptw->tw_ts_recent;
  529. }
  530. ret = true;
  531. }
  532. rcu_read_unlock();
  533. return ret;
  534. }
  535. static DEFINE_SEQLOCK(fastopen_seqlock);
  536. void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
  537. struct tcp_fastopen_cookie *cookie,
  538. int *syn_loss, unsigned long *last_syn_loss)
  539. {
  540. struct tcp_metrics_block *tm;
  541. rcu_read_lock();
  542. tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
  543. if (tm) {
  544. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  545. unsigned int seq;
  546. do {
  547. seq = read_seqbegin(&fastopen_seqlock);
  548. if (tfom->mss)
  549. *mss = tfom->mss;
  550. *cookie = tfom->cookie;
  551. *syn_loss = tfom->syn_loss;
  552. *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
  553. } while (read_seqretry(&fastopen_seqlock, seq));
  554. }
  555. rcu_read_unlock();
  556. }
  557. void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
  558. struct tcp_fastopen_cookie *cookie, bool syn_lost)
  559. {
  560. struct tcp_metrics_block *tm;
  561. rcu_read_lock();
  562. tm = tcp_get_metrics(sk, __sk_dst_get(sk), true);
  563. if (tm) {
  564. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  565. write_seqlock_bh(&fastopen_seqlock);
  566. tfom->mss = mss;
  567. if (cookie->len > 0)
  568. tfom->cookie = *cookie;
  569. if (syn_lost) {
  570. ++tfom->syn_loss;
  571. tfom->last_syn_loss = jiffies;
  572. } else
  573. tfom->syn_loss = 0;
  574. write_sequnlock_bh(&fastopen_seqlock);
  575. }
  576. rcu_read_unlock();
  577. }
  578. static struct genl_family tcp_metrics_nl_family = {
  579. .id = GENL_ID_GENERATE,
  580. .hdrsize = 0,
  581. .name = TCP_METRICS_GENL_NAME,
  582. .version = TCP_METRICS_GENL_VERSION,
  583. .maxattr = TCP_METRICS_ATTR_MAX,
  584. .netnsok = true,
  585. };
  586. static struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
  587. [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
  588. [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
  589. .len = sizeof(struct in6_addr), },
  590. /* Following attributes are not received for GET/DEL,
  591. * we keep them for reference
  592. */
  593. #if 0
  594. [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
  595. [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
  596. [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
  597. [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
  598. [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
  599. [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
  600. [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
  601. [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
  602. .len = TCP_FASTOPEN_COOKIE_MAX, },
  603. #endif
  604. };
  605. /* Add attributes, caller cancels its header on failure */
  606. static int tcp_metrics_fill_info(struct sk_buff *msg,
  607. struct tcp_metrics_block *tm)
  608. {
  609. struct nlattr *nest;
  610. int i;
  611. switch (tm->tcpm_addr.family) {
  612. case AF_INET:
  613. if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4,
  614. tm->tcpm_addr.addr.a4) < 0)
  615. goto nla_put_failure;
  616. break;
  617. case AF_INET6:
  618. if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16,
  619. tm->tcpm_addr.addr.a6) < 0)
  620. goto nla_put_failure;
  621. break;
  622. default:
  623. return -EAFNOSUPPORT;
  624. }
  625. if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
  626. jiffies - tm->tcpm_stamp) < 0)
  627. goto nla_put_failure;
  628. if (tm->tcpm_ts_stamp) {
  629. if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP,
  630. (s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0)
  631. goto nla_put_failure;
  632. if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL,
  633. tm->tcpm_ts) < 0)
  634. goto nla_put_failure;
  635. }
  636. {
  637. int n = 0;
  638. nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
  639. if (!nest)
  640. goto nla_put_failure;
  641. for (i = 0; i < TCP_METRIC_MAX + 1; i++) {
  642. if (!tm->tcpm_vals[i])
  643. continue;
  644. if (nla_put_u32(msg, i + 1, tm->tcpm_vals[i]) < 0)
  645. goto nla_put_failure;
  646. n++;
  647. }
  648. if (n)
  649. nla_nest_end(msg, nest);
  650. else
  651. nla_nest_cancel(msg, nest);
  652. }
  653. {
  654. struct tcp_fastopen_metrics tfom_copy[1], *tfom;
  655. unsigned int seq;
  656. do {
  657. seq = read_seqbegin(&fastopen_seqlock);
  658. tfom_copy[0] = tm->tcpm_fastopen;
  659. } while (read_seqretry(&fastopen_seqlock, seq));
  660. tfom = tfom_copy;
  661. if (tfom->mss &&
  662. nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
  663. tfom->mss) < 0)
  664. goto nla_put_failure;
  665. if (tfom->syn_loss &&
  666. (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
  667. tfom->syn_loss) < 0 ||
  668. nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
  669. jiffies - tfom->last_syn_loss) < 0))
  670. goto nla_put_failure;
  671. if (tfom->cookie.len > 0 &&
  672. nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
  673. tfom->cookie.len, tfom->cookie.val) < 0)
  674. goto nla_put_failure;
  675. }
  676. return 0;
  677. nla_put_failure:
  678. return -EMSGSIZE;
  679. }
  680. static int tcp_metrics_dump_info(struct sk_buff *skb,
  681. struct netlink_callback *cb,
  682. struct tcp_metrics_block *tm)
  683. {
  684. void *hdr;
  685. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  686. &tcp_metrics_nl_family, NLM_F_MULTI,
  687. TCP_METRICS_CMD_GET);
  688. if (!hdr)
  689. return -EMSGSIZE;
  690. if (tcp_metrics_fill_info(skb, tm) < 0)
  691. goto nla_put_failure;
  692. return genlmsg_end(skb, hdr);
  693. nla_put_failure:
  694. genlmsg_cancel(skb, hdr);
  695. return -EMSGSIZE;
  696. }
  697. static int tcp_metrics_nl_dump(struct sk_buff *skb,
  698. struct netlink_callback *cb)
  699. {
  700. struct net *net = sock_net(skb->sk);
  701. unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
  702. unsigned int row, s_row = cb->args[0];
  703. int s_col = cb->args[1], col = s_col;
  704. for (row = s_row; row < max_rows; row++, s_col = 0) {
  705. struct tcp_metrics_block *tm;
  706. struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash + row;
  707. rcu_read_lock();
  708. for (col = 0, tm = rcu_dereference(hb->chain); tm;
  709. tm = rcu_dereference(tm->tcpm_next), col++) {
  710. if (col < s_col)
  711. continue;
  712. if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
  713. rcu_read_unlock();
  714. goto done;
  715. }
  716. }
  717. rcu_read_unlock();
  718. }
  719. done:
  720. cb->args[0] = row;
  721. cb->args[1] = col;
  722. return skb->len;
  723. }
  724. static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  725. unsigned int *hash, int optional)
  726. {
  727. struct nlattr *a;
  728. a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV4];
  729. if (a) {
  730. addr->family = AF_INET;
  731. addr->addr.a4 = nla_get_be32(a);
  732. *hash = (__force unsigned int) addr->addr.a4;
  733. return 0;
  734. }
  735. a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV6];
  736. if (a) {
  737. if (nla_len(a) != sizeof(struct in6_addr))
  738. return -EINVAL;
  739. addr->family = AF_INET6;
  740. memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6));
  741. *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
  742. return 0;
  743. }
  744. return optional ? 1 : -EAFNOSUPPORT;
  745. }
  746. static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
  747. {
  748. struct tcp_metrics_block *tm;
  749. struct inetpeer_addr addr;
  750. unsigned int hash;
  751. struct sk_buff *msg;
  752. struct net *net = genl_info_net(info);
  753. void *reply;
  754. int ret;
  755. ret = parse_nl_addr(info, &addr, &hash, 0);
  756. if (ret < 0)
  757. return ret;
  758. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  759. if (!msg)
  760. return -ENOMEM;
  761. reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
  762. info->genlhdr->cmd);
  763. if (!reply)
  764. goto nla_put_failure;
  765. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  766. ret = -ESRCH;
  767. rcu_read_lock();
  768. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  769. tm = rcu_dereference(tm->tcpm_next)) {
  770. if (addr_same(&tm->tcpm_addr, &addr)) {
  771. ret = tcp_metrics_fill_info(msg, tm);
  772. break;
  773. }
  774. }
  775. rcu_read_unlock();
  776. if (ret < 0)
  777. goto out_free;
  778. genlmsg_end(msg, reply);
  779. return genlmsg_reply(msg, info);
  780. nla_put_failure:
  781. ret = -EMSGSIZE;
  782. out_free:
  783. nlmsg_free(msg);
  784. return ret;
  785. }
  786. #define deref_locked_genl(p) \
  787. rcu_dereference_protected(p, lockdep_genl_is_held() && \
  788. lockdep_is_held(&tcp_metrics_lock))
  789. #define deref_genl(p) rcu_dereference_protected(p, lockdep_genl_is_held())
  790. static int tcp_metrics_flush_all(struct net *net)
  791. {
  792. unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
  793. struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash;
  794. struct tcp_metrics_block *tm;
  795. unsigned int row;
  796. for (row = 0; row < max_rows; row++, hb++) {
  797. spin_lock_bh(&tcp_metrics_lock);
  798. tm = deref_locked_genl(hb->chain);
  799. if (tm)
  800. hb->chain = NULL;
  801. spin_unlock_bh(&tcp_metrics_lock);
  802. while (tm) {
  803. struct tcp_metrics_block *next;
  804. next = deref_genl(tm->tcpm_next);
  805. kfree_rcu(tm, rcu_head);
  806. tm = next;
  807. }
  808. }
  809. return 0;
  810. }
  811. static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
  812. {
  813. struct tcpm_hash_bucket *hb;
  814. struct tcp_metrics_block *tm;
  815. struct tcp_metrics_block __rcu **pp;
  816. struct inetpeer_addr addr;
  817. unsigned int hash;
  818. struct net *net = genl_info_net(info);
  819. int ret;
  820. ret = parse_nl_addr(info, &addr, &hash, 1);
  821. if (ret < 0)
  822. return ret;
  823. if (ret > 0)
  824. return tcp_metrics_flush_all(net);
  825. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  826. hb = net->ipv4.tcp_metrics_hash + hash;
  827. pp = &hb->chain;
  828. spin_lock_bh(&tcp_metrics_lock);
  829. for (tm = deref_locked_genl(*pp); tm;
  830. pp = &tm->tcpm_next, tm = deref_locked_genl(*pp)) {
  831. if (addr_same(&tm->tcpm_addr, &addr)) {
  832. *pp = tm->tcpm_next;
  833. break;
  834. }
  835. }
  836. spin_unlock_bh(&tcp_metrics_lock);
  837. if (!tm)
  838. return -ESRCH;
  839. kfree_rcu(tm, rcu_head);
  840. return 0;
  841. }
  842. static struct genl_ops tcp_metrics_nl_ops[] = {
  843. {
  844. .cmd = TCP_METRICS_CMD_GET,
  845. .doit = tcp_metrics_nl_cmd_get,
  846. .dumpit = tcp_metrics_nl_dump,
  847. .policy = tcp_metrics_nl_policy,
  848. .flags = GENL_ADMIN_PERM,
  849. },
  850. {
  851. .cmd = TCP_METRICS_CMD_DEL,
  852. .doit = tcp_metrics_nl_cmd_del,
  853. .policy = tcp_metrics_nl_policy,
  854. .flags = GENL_ADMIN_PERM,
  855. },
  856. };
  857. static unsigned int tcpmhash_entries;
  858. static int __init set_tcpmhash_entries(char *str)
  859. {
  860. ssize_t ret;
  861. if (!str)
  862. return 0;
  863. ret = kstrtouint(str, 0, &tcpmhash_entries);
  864. if (ret)
  865. return 0;
  866. return 1;
  867. }
  868. __setup("tcpmhash_entries=", set_tcpmhash_entries);
  869. static int __net_init tcp_net_metrics_init(struct net *net)
  870. {
  871. size_t size;
  872. unsigned int slots;
  873. slots = tcpmhash_entries;
  874. if (!slots) {
  875. if (totalram_pages >= 128 * 1024)
  876. slots = 16 * 1024;
  877. else
  878. slots = 8 * 1024;
  879. }
  880. net->ipv4.tcp_metrics_hash_log = order_base_2(slots);
  881. size = sizeof(struct tcpm_hash_bucket) << net->ipv4.tcp_metrics_hash_log;
  882. net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
  883. if (!net->ipv4.tcp_metrics_hash)
  884. net->ipv4.tcp_metrics_hash = vzalloc(size);
  885. if (!net->ipv4.tcp_metrics_hash)
  886. return -ENOMEM;
  887. return 0;
  888. }
  889. static void __net_exit tcp_net_metrics_exit(struct net *net)
  890. {
  891. unsigned int i;
  892. for (i = 0; i < (1U << net->ipv4.tcp_metrics_hash_log) ; i++) {
  893. struct tcp_metrics_block *tm, *next;
  894. tm = rcu_dereference_protected(net->ipv4.tcp_metrics_hash[i].chain, 1);
  895. while (tm) {
  896. next = rcu_dereference_protected(tm->tcpm_next, 1);
  897. kfree(tm);
  898. tm = next;
  899. }
  900. }
  901. if (is_vmalloc_addr(net->ipv4.tcp_metrics_hash))
  902. vfree(net->ipv4.tcp_metrics_hash);
  903. else
  904. kfree(net->ipv4.tcp_metrics_hash);
  905. }
  906. static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
  907. .init = tcp_net_metrics_init,
  908. .exit = tcp_net_metrics_exit,
  909. };
  910. void __init tcp_metrics_init(void)
  911. {
  912. int ret;
  913. ret = register_pernet_subsys(&tcp_net_metrics_ops);
  914. if (ret < 0)
  915. goto cleanup;
  916. ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
  917. tcp_metrics_nl_ops,
  918. ARRAY_SIZE(tcp_metrics_nl_ops));
  919. if (ret < 0)
  920. goto cleanup_subsys;
  921. return;
  922. cleanup_subsys:
  923. unregister_pernet_subsys(&tcp_net_metrics_ops);
  924. cleanup:
  925. return;
  926. }