tcp_metrics.c 28 KB

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