tcp_metrics.c 17 KB

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