tcp_metrics.c 27 KB

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