tcp_metrics.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. #include <linux/rcupdate.h>
  2. #include <linux/spinlock.h>
  3. #include <linux/jiffies.h>
  4. #include <linux/bootmem.h>
  5. #include <linux/module.h>
  6. #include <linux/cache.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <linux/tcp.h>
  10. #include <linux/hash.h>
  11. #include <net/inet_connection_sock.h>
  12. #include <net/net_namespace.h>
  13. #include <net/request_sock.h>
  14. #include <net/inetpeer.h>
  15. #include <net/sock.h>
  16. #include <net/ipv6.h>
  17. #include <net/dst.h>
  18. #include <net/tcp.h>
  19. int sysctl_tcp_nometrics_save __read_mostly;
  20. enum tcp_metric_index {
  21. TCP_METRIC_RTT,
  22. TCP_METRIC_RTTVAR,
  23. TCP_METRIC_SSTHRESH,
  24. TCP_METRIC_CWND,
  25. TCP_METRIC_REORDERING,
  26. /* Always last. */
  27. TCP_METRIC_MAX,
  28. };
  29. struct tcp_fastopen_metrics {
  30. u16 mss;
  31. u16 syn_loss:10; /* Recurring Fast Open SYN losses */
  32. unsigned long last_syn_loss; /* Last Fast Open SYN loss */
  33. struct tcp_fastopen_cookie cookie;
  34. };
  35. struct tcp_metrics_block {
  36. struct tcp_metrics_block __rcu *tcpm_next;
  37. struct inetpeer_addr tcpm_addr;
  38. unsigned long tcpm_stamp;
  39. u32 tcpm_ts;
  40. u32 tcpm_ts_stamp;
  41. u32 tcpm_lock;
  42. u32 tcpm_vals[TCP_METRIC_MAX];
  43. struct tcp_fastopen_metrics tcpm_fastopen;
  44. };
  45. static bool tcp_metric_locked(struct tcp_metrics_block *tm,
  46. enum tcp_metric_index idx)
  47. {
  48. return tm->tcpm_lock & (1 << idx);
  49. }
  50. static u32 tcp_metric_get(struct tcp_metrics_block *tm,
  51. enum tcp_metric_index idx)
  52. {
  53. return tm->tcpm_vals[idx];
  54. }
  55. static u32 tcp_metric_get_jiffies(struct tcp_metrics_block *tm,
  56. enum tcp_metric_index idx)
  57. {
  58. return msecs_to_jiffies(tm->tcpm_vals[idx]);
  59. }
  60. static void tcp_metric_set(struct tcp_metrics_block *tm,
  61. enum tcp_metric_index idx,
  62. u32 val)
  63. {
  64. tm->tcpm_vals[idx] = val;
  65. }
  66. static void tcp_metric_set_msecs(struct tcp_metrics_block *tm,
  67. enum tcp_metric_index idx,
  68. u32 val)
  69. {
  70. tm->tcpm_vals[idx] = jiffies_to_msecs(val);
  71. }
  72. static bool addr_same(const struct inetpeer_addr *a,
  73. const struct inetpeer_addr *b)
  74. {
  75. const struct in6_addr *a6, *b6;
  76. if (a->family != b->family)
  77. return false;
  78. if (a->family == AF_INET)
  79. return a->addr.a4 == b->addr.a4;
  80. a6 = (const struct in6_addr *) &a->addr.a6[0];
  81. b6 = (const struct in6_addr *) &b->addr.a6[0];
  82. return ipv6_addr_equal(a6, b6);
  83. }
  84. struct tcpm_hash_bucket {
  85. struct tcp_metrics_block __rcu *chain;
  86. };
  87. static DEFINE_SPINLOCK(tcp_metrics_lock);
  88. static void tcpm_suck_dst(struct tcp_metrics_block *tm, struct dst_entry *dst)
  89. {
  90. u32 val;
  91. tm->tcpm_stamp = jiffies;
  92. val = 0;
  93. if (dst_metric_locked(dst, RTAX_RTT))
  94. val |= 1 << TCP_METRIC_RTT;
  95. if (dst_metric_locked(dst, RTAX_RTTVAR))
  96. val |= 1 << TCP_METRIC_RTTVAR;
  97. if (dst_metric_locked(dst, RTAX_SSTHRESH))
  98. val |= 1 << TCP_METRIC_SSTHRESH;
  99. if (dst_metric_locked(dst, RTAX_CWND))
  100. val |= 1 << TCP_METRIC_CWND;
  101. if (dst_metric_locked(dst, RTAX_REORDERING))
  102. val |= 1 << TCP_METRIC_REORDERING;
  103. tm->tcpm_lock = val;
  104. tm->tcpm_vals[TCP_METRIC_RTT] = dst_metric_raw(dst, RTAX_RTT);
  105. tm->tcpm_vals[TCP_METRIC_RTTVAR] = dst_metric_raw(dst, RTAX_RTTVAR);
  106. tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
  107. tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
  108. tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
  109. tm->tcpm_ts = 0;
  110. tm->tcpm_ts_stamp = 0;
  111. tm->tcpm_fastopen.mss = 0;
  112. tm->tcpm_fastopen.syn_loss = 0;
  113. tm->tcpm_fastopen.cookie.len = 0;
  114. }
  115. static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
  116. struct inetpeer_addr *addr,
  117. unsigned int hash,
  118. bool reclaim)
  119. {
  120. struct tcp_metrics_block *tm;
  121. struct net *net;
  122. spin_lock_bh(&tcp_metrics_lock);
  123. net = dev_net(dst->dev);
  124. if (unlikely(reclaim)) {
  125. struct tcp_metrics_block *oldest;
  126. oldest = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain);
  127. for (tm = rcu_dereference(oldest->tcpm_next); tm;
  128. tm = rcu_dereference(tm->tcpm_next)) {
  129. if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
  130. oldest = tm;
  131. }
  132. tm = oldest;
  133. } else {
  134. tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
  135. if (!tm)
  136. goto out_unlock;
  137. }
  138. tm->tcpm_addr = *addr;
  139. tcpm_suck_dst(tm, dst);
  140. if (likely(!reclaim)) {
  141. tm->tcpm_next = net->ipv4.tcp_metrics_hash[hash].chain;
  142. rcu_assign_pointer(net->ipv4.tcp_metrics_hash[hash].chain, tm);
  143. }
  144. out_unlock:
  145. spin_unlock_bh(&tcp_metrics_lock);
  146. return tm;
  147. }
  148. #define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
  149. static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
  150. {
  151. if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
  152. tcpm_suck_dst(tm, dst);
  153. }
  154. #define TCP_METRICS_RECLAIM_DEPTH 5
  155. #define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
  156. static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
  157. {
  158. if (tm)
  159. return tm;
  160. if (depth > TCP_METRICS_RECLAIM_DEPTH)
  161. return TCP_METRICS_RECLAIM_PTR;
  162. return NULL;
  163. }
  164. static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *addr,
  165. struct net *net, unsigned int hash)
  166. {
  167. struct tcp_metrics_block *tm;
  168. int depth = 0;
  169. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  170. tm = rcu_dereference(tm->tcpm_next)) {
  171. if (addr_same(&tm->tcpm_addr, addr))
  172. break;
  173. depth++;
  174. }
  175. return tcp_get_encode(tm, depth);
  176. }
  177. static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
  178. struct dst_entry *dst)
  179. {
  180. struct tcp_metrics_block *tm;
  181. struct inetpeer_addr addr;
  182. unsigned int hash;
  183. struct net *net;
  184. addr.family = req->rsk_ops->family;
  185. switch (addr.family) {
  186. case AF_INET:
  187. addr.addr.a4 = inet_rsk(req)->rmt_addr;
  188. hash = (__force unsigned int) addr.addr.a4;
  189. break;
  190. case AF_INET6:
  191. *(struct in6_addr *)addr.addr.a6 = inet6_rsk(req)->rmt_addr;
  192. hash = ipv6_addr_hash(&inet6_rsk(req)->rmt_addr);
  193. break;
  194. default:
  195. return NULL;
  196. }
  197. net = dev_net(dst->dev);
  198. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  199. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  200. tm = rcu_dereference(tm->tcpm_next)) {
  201. if (addr_same(&tm->tcpm_addr, &addr))
  202. break;
  203. }
  204. tcpm_check_stamp(tm, dst);
  205. return tm;
  206. }
  207. static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
  208. {
  209. struct inet6_timewait_sock *tw6;
  210. struct tcp_metrics_block *tm;
  211. struct inetpeer_addr addr;
  212. unsigned int hash;
  213. struct net *net;
  214. addr.family = tw->tw_family;
  215. switch (addr.family) {
  216. case AF_INET:
  217. addr.addr.a4 = tw->tw_daddr;
  218. hash = (__force unsigned int) addr.addr.a4;
  219. break;
  220. case AF_INET6:
  221. tw6 = inet6_twsk((struct sock *)tw);
  222. *(struct in6_addr *)addr.addr.a6 = tw6->tw_v6_daddr;
  223. hash = ipv6_addr_hash(&tw6->tw_v6_daddr);
  224. break;
  225. default:
  226. return NULL;
  227. }
  228. net = twsk_net(tw);
  229. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  230. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  231. tm = rcu_dereference(tm->tcpm_next)) {
  232. if (addr_same(&tm->tcpm_addr, &addr))
  233. break;
  234. }
  235. return tm;
  236. }
  237. static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
  238. struct dst_entry *dst,
  239. bool create)
  240. {
  241. struct tcp_metrics_block *tm;
  242. struct inetpeer_addr addr;
  243. unsigned int hash;
  244. struct net *net;
  245. bool reclaim;
  246. addr.family = sk->sk_family;
  247. switch (addr.family) {
  248. case AF_INET:
  249. addr.addr.a4 = inet_sk(sk)->inet_daddr;
  250. hash = (__force unsigned int) addr.addr.a4;
  251. break;
  252. case AF_INET6:
  253. *(struct in6_addr *)addr.addr.a6 = inet6_sk(sk)->daddr;
  254. hash = ipv6_addr_hash(&inet6_sk(sk)->daddr);
  255. break;
  256. default:
  257. return NULL;
  258. }
  259. net = dev_net(dst->dev);
  260. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  261. tm = __tcp_get_metrics(&addr, net, hash);
  262. reclaim = false;
  263. if (tm == TCP_METRICS_RECLAIM_PTR) {
  264. reclaim = true;
  265. tm = NULL;
  266. }
  267. if (!tm && create)
  268. tm = tcpm_new(dst, &addr, hash, reclaim);
  269. else
  270. tcpm_check_stamp(tm, dst);
  271. return tm;
  272. }
  273. /* Save metrics learned by this TCP session. This function is called
  274. * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
  275. * or goes from LAST-ACK to CLOSE.
  276. */
  277. void tcp_update_metrics(struct sock *sk)
  278. {
  279. const struct inet_connection_sock *icsk = inet_csk(sk);
  280. struct dst_entry *dst = __sk_dst_get(sk);
  281. struct tcp_sock *tp = tcp_sk(sk);
  282. struct tcp_metrics_block *tm;
  283. unsigned long rtt;
  284. u32 val;
  285. int m;
  286. if (sysctl_tcp_nometrics_save || !dst)
  287. return;
  288. if (dst->flags & DST_HOST)
  289. dst_confirm(dst);
  290. rcu_read_lock();
  291. if (icsk->icsk_backoff || !tp->srtt) {
  292. /* This session failed to estimate rtt. Why?
  293. * Probably, no packets returned in time. Reset our
  294. * results.
  295. */
  296. tm = tcp_get_metrics(sk, dst, false);
  297. if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
  298. tcp_metric_set(tm, TCP_METRIC_RTT, 0);
  299. goto out_unlock;
  300. } else
  301. tm = tcp_get_metrics(sk, dst, true);
  302. if (!tm)
  303. goto out_unlock;
  304. rtt = tcp_metric_get_jiffies(tm, TCP_METRIC_RTT);
  305. m = rtt - tp->srtt;
  306. /* If newly calculated rtt larger than stored one, store new
  307. * one. Otherwise, use EWMA. Remember, rtt overestimation is
  308. * always better than underestimation.
  309. */
  310. if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
  311. if (m <= 0)
  312. rtt = tp->srtt;
  313. else
  314. rtt -= (m >> 3);
  315. tcp_metric_set_msecs(tm, TCP_METRIC_RTT, rtt);
  316. }
  317. if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
  318. unsigned long var;
  319. if (m < 0)
  320. m = -m;
  321. /* Scale deviation to rttvar fixed point */
  322. m >>= 1;
  323. if (m < tp->mdev)
  324. m = tp->mdev;
  325. var = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
  326. if (m >= var)
  327. var = m;
  328. else
  329. var -= (var - m) >> 2;
  330. tcp_metric_set_msecs(tm, TCP_METRIC_RTTVAR, var);
  331. }
  332. if (tcp_in_initial_slowstart(tp)) {
  333. /* Slow start still did not finish. */
  334. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
  335. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  336. if (val && (tp->snd_cwnd >> 1) > val)
  337. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  338. tp->snd_cwnd >> 1);
  339. }
  340. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  341. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  342. if (tp->snd_cwnd > val)
  343. tcp_metric_set(tm, TCP_METRIC_CWND,
  344. tp->snd_cwnd);
  345. }
  346. } else if (tp->snd_cwnd > tp->snd_ssthresh &&
  347. icsk->icsk_ca_state == TCP_CA_Open) {
  348. /* Cong. avoidance phase, cwnd is reliable. */
  349. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
  350. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  351. max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
  352. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  353. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  354. tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
  355. }
  356. } else {
  357. /* Else slow start did not finish, cwnd is non-sense,
  358. * ssthresh may be also invalid.
  359. */
  360. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  361. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  362. tcp_metric_set(tm, TCP_METRIC_CWND,
  363. (val + tp->snd_ssthresh) >> 1);
  364. }
  365. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
  366. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  367. if (val && tp->snd_ssthresh > val)
  368. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  369. tp->snd_ssthresh);
  370. }
  371. if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
  372. val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
  373. if (val < tp->reordering &&
  374. tp->reordering != sysctl_tcp_reordering)
  375. tcp_metric_set(tm, TCP_METRIC_REORDERING,
  376. tp->reordering);
  377. }
  378. }
  379. tm->tcpm_stamp = jiffies;
  380. out_unlock:
  381. rcu_read_unlock();
  382. }
  383. /* Initialize metrics on socket. */
  384. void tcp_init_metrics(struct sock *sk)
  385. {
  386. struct dst_entry *dst = __sk_dst_get(sk);
  387. struct tcp_sock *tp = tcp_sk(sk);
  388. struct tcp_metrics_block *tm;
  389. u32 val;
  390. if (dst == NULL)
  391. goto reset;
  392. dst_confirm(dst);
  393. rcu_read_lock();
  394. tm = tcp_get_metrics(sk, dst, true);
  395. if (!tm) {
  396. rcu_read_unlock();
  397. goto reset;
  398. }
  399. if (tcp_metric_locked(tm, TCP_METRIC_CWND))
  400. tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
  401. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  402. if (val) {
  403. tp->snd_ssthresh = val;
  404. if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
  405. tp->snd_ssthresh = tp->snd_cwnd_clamp;
  406. } else {
  407. /* ssthresh may have been reduced unnecessarily during.
  408. * 3WHS. Restore it back to its initial default.
  409. */
  410. tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  411. }
  412. val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
  413. if (val && tp->reordering != val) {
  414. tcp_disable_fack(tp);
  415. tcp_disable_early_retrans(tp);
  416. tp->reordering = val;
  417. }
  418. val = tcp_metric_get(tm, TCP_METRIC_RTT);
  419. if (val == 0 || tp->srtt == 0) {
  420. rcu_read_unlock();
  421. goto reset;
  422. }
  423. /* Initial rtt is determined from SYN,SYN-ACK.
  424. * The segment is small and rtt may appear much
  425. * less than real one. Use per-dst memory
  426. * to make it more realistic.
  427. *
  428. * A bit of theory. RTT is time passed after "normal" sized packet
  429. * is sent until it is ACKed. In normal circumstances sending small
  430. * packets force peer to delay ACKs and calculation is correct too.
  431. * The algorithm is adaptive and, provided we follow specs, it
  432. * NEVER underestimate RTT. BUT! If peer tries to make some clever
  433. * tricks sort of "quick acks" for time long enough to decrease RTT
  434. * to low value, and then abruptly stops to do it and starts to delay
  435. * ACKs, wait for troubles.
  436. */
  437. val = msecs_to_jiffies(val);
  438. if (val > tp->srtt) {
  439. tp->srtt = val;
  440. tp->rtt_seq = tp->snd_nxt;
  441. }
  442. val = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
  443. if (val > tp->mdev) {
  444. tp->mdev = val;
  445. tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
  446. }
  447. rcu_read_unlock();
  448. tcp_set_rto(sk);
  449. reset:
  450. if (tp->srtt == 0) {
  451. /* RFC6298: 5.7 We've failed to get a valid RTT sample from
  452. * 3WHS. This is most likely due to retransmission,
  453. * including spurious one. Reset the RTO back to 3secs
  454. * from the more aggressive 1sec to avoid more spurious
  455. * retransmission.
  456. */
  457. tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_FALLBACK;
  458. inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
  459. }
  460. /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
  461. * retransmitted. In light of RFC6298 more aggressive 1sec
  462. * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
  463. * retransmission has occurred.
  464. */
  465. if (tp->total_retrans > 1)
  466. tp->snd_cwnd = 1;
  467. else
  468. tp->snd_cwnd = tcp_init_cwnd(tp, dst);
  469. tp->snd_cwnd_stamp = tcp_time_stamp;
  470. }
  471. bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check)
  472. {
  473. struct tcp_metrics_block *tm;
  474. bool ret;
  475. if (!dst)
  476. return false;
  477. rcu_read_lock();
  478. tm = __tcp_get_metrics_req(req, dst);
  479. if (paws_check) {
  480. if (tm &&
  481. (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
  482. (s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW)
  483. ret = false;
  484. else
  485. ret = true;
  486. } else {
  487. if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
  488. ret = true;
  489. else
  490. ret = false;
  491. }
  492. rcu_read_unlock();
  493. return ret;
  494. }
  495. EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
  496. void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
  497. {
  498. struct tcp_metrics_block *tm;
  499. rcu_read_lock();
  500. tm = tcp_get_metrics(sk, dst, true);
  501. if (tm) {
  502. struct tcp_sock *tp = tcp_sk(sk);
  503. if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
  504. tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
  505. tp->rx_opt.ts_recent = tm->tcpm_ts;
  506. }
  507. }
  508. rcu_read_unlock();
  509. }
  510. EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
  511. /* VJ's idea. Save last timestamp seen from this destination and hold
  512. * it at least for normal timewait interval to use for duplicate
  513. * segment detection in subsequent connections, before they enter
  514. * synchronized state.
  515. */
  516. bool tcp_remember_stamp(struct sock *sk)
  517. {
  518. struct dst_entry *dst = __sk_dst_get(sk);
  519. bool ret = false;
  520. if (dst) {
  521. struct tcp_metrics_block *tm;
  522. rcu_read_lock();
  523. tm = tcp_get_metrics(sk, dst, true);
  524. if (tm) {
  525. struct tcp_sock *tp = tcp_sk(sk);
  526. if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
  527. ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
  528. tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
  529. tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
  530. tm->tcpm_ts = tp->rx_opt.ts_recent;
  531. }
  532. ret = true;
  533. }
  534. rcu_read_unlock();
  535. }
  536. return ret;
  537. }
  538. bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
  539. {
  540. struct tcp_metrics_block *tm;
  541. bool ret = false;
  542. rcu_read_lock();
  543. tm = __tcp_get_metrics_tw(tw);
  544. if (tm) {
  545. const struct tcp_timewait_sock *tcptw;
  546. struct sock *sk = (struct sock *) tw;
  547. tcptw = tcp_twsk(sk);
  548. if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
  549. ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
  550. tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
  551. tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
  552. tm->tcpm_ts = tcptw->tw_ts_recent;
  553. }
  554. ret = true;
  555. }
  556. rcu_read_unlock();
  557. return ret;
  558. }
  559. static DEFINE_SEQLOCK(fastopen_seqlock);
  560. void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
  561. struct tcp_fastopen_cookie *cookie,
  562. int *syn_loss, unsigned long *last_syn_loss)
  563. {
  564. struct tcp_metrics_block *tm;
  565. rcu_read_lock();
  566. tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
  567. if (tm) {
  568. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  569. unsigned int seq;
  570. do {
  571. seq = read_seqbegin(&fastopen_seqlock);
  572. if (tfom->mss)
  573. *mss = tfom->mss;
  574. *cookie = tfom->cookie;
  575. *syn_loss = tfom->syn_loss;
  576. *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
  577. } while (read_seqretry(&fastopen_seqlock, seq));
  578. }
  579. rcu_read_unlock();
  580. }
  581. void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
  582. struct tcp_fastopen_cookie *cookie, bool syn_lost)
  583. {
  584. struct tcp_metrics_block *tm;
  585. rcu_read_lock();
  586. tm = tcp_get_metrics(sk, __sk_dst_get(sk), true);
  587. if (tm) {
  588. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  589. write_seqlock_bh(&fastopen_seqlock);
  590. tfom->mss = mss;
  591. if (cookie->len > 0)
  592. tfom->cookie = *cookie;
  593. if (syn_lost) {
  594. ++tfom->syn_loss;
  595. tfom->last_syn_loss = jiffies;
  596. } else
  597. tfom->syn_loss = 0;
  598. write_sequnlock_bh(&fastopen_seqlock);
  599. }
  600. rcu_read_unlock();
  601. }
  602. static unsigned int tcpmhash_entries;
  603. static int __init set_tcpmhash_entries(char *str)
  604. {
  605. ssize_t ret;
  606. if (!str)
  607. return 0;
  608. ret = kstrtouint(str, 0, &tcpmhash_entries);
  609. if (ret)
  610. return 0;
  611. return 1;
  612. }
  613. __setup("tcpmhash_entries=", set_tcpmhash_entries);
  614. static int __net_init tcp_net_metrics_init(struct net *net)
  615. {
  616. size_t size;
  617. unsigned int slots;
  618. slots = tcpmhash_entries;
  619. if (!slots) {
  620. if (totalram_pages >= 128 * 1024)
  621. slots = 16 * 1024;
  622. else
  623. slots = 8 * 1024;
  624. }
  625. net->ipv4.tcp_metrics_hash_log = order_base_2(slots);
  626. size = sizeof(struct tcpm_hash_bucket) << net->ipv4.tcp_metrics_hash_log;
  627. net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL);
  628. if (!net->ipv4.tcp_metrics_hash)
  629. return -ENOMEM;
  630. return 0;
  631. }
  632. static void __net_exit tcp_net_metrics_exit(struct net *net)
  633. {
  634. kfree(net->ipv4.tcp_metrics_hash);
  635. }
  636. static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
  637. .init = tcp_net_metrics_init,
  638. .exit = tcp_net_metrics_exit,
  639. };
  640. void __init tcp_metrics_init(void)
  641. {
  642. register_pernet_subsys(&tcp_net_metrics_ops);
  643. }