tcp_metrics.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. possible_net_t 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(struct tcp_metrics_block *tm)
  48. {
  49. return read_pnet(&tm->tcpm_net);
  50. }
  51. static bool tcp_metric_locked(struct tcp_metrics_block *tm,
  52. enum tcp_metric_index idx)
  53. {
  54. return tm->tcpm_lock & (1 << idx);
  55. }
  56. static u32 tcp_metric_get(struct tcp_metrics_block *tm,
  57. enum tcp_metric_index idx)
  58. {
  59. return tm->tcpm_vals[idx];
  60. }
  61. static void tcp_metric_set(struct tcp_metrics_block *tm,
  62. enum tcp_metric_index idx,
  63. u32 val)
  64. {
  65. tm->tcpm_vals[idx] = val;
  66. }
  67. static bool addr_same(const struct inetpeer_addr *a,
  68. const struct inetpeer_addr *b)
  69. {
  70. return inetpeer_addr_cmp(a, b) == 0;
  71. }
  72. struct tcpm_hash_bucket {
  73. struct tcp_metrics_block __rcu *chain;
  74. };
  75. static struct tcpm_hash_bucket *tcp_metrics_hash __read_mostly;
  76. static unsigned int tcp_metrics_hash_log __read_mostly;
  77. static DEFINE_SPINLOCK(tcp_metrics_lock);
  78. static void tcpm_suck_dst(struct tcp_metrics_block *tm,
  79. const struct dst_entry *dst,
  80. bool fastopen_clear)
  81. {
  82. u32 msval;
  83. u32 val;
  84. tm->tcpm_stamp = jiffies;
  85. val = 0;
  86. if (dst_metric_locked(dst, RTAX_RTT))
  87. val |= 1 << TCP_METRIC_RTT;
  88. if (dst_metric_locked(dst, RTAX_RTTVAR))
  89. val |= 1 << TCP_METRIC_RTTVAR;
  90. if (dst_metric_locked(dst, RTAX_SSTHRESH))
  91. val |= 1 << TCP_METRIC_SSTHRESH;
  92. if (dst_metric_locked(dst, RTAX_CWND))
  93. val |= 1 << TCP_METRIC_CWND;
  94. if (dst_metric_locked(dst, RTAX_REORDERING))
  95. val |= 1 << TCP_METRIC_REORDERING;
  96. tm->tcpm_lock = val;
  97. msval = dst_metric_raw(dst, RTAX_RTT);
  98. tm->tcpm_vals[TCP_METRIC_RTT] = msval * USEC_PER_MSEC;
  99. msval = dst_metric_raw(dst, RTAX_RTTVAR);
  100. tm->tcpm_vals[TCP_METRIC_RTTVAR] = msval * USEC_PER_MSEC;
  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. if (fastopen_clear) {
  105. tm->tcpm_fastopen.mss = 0;
  106. tm->tcpm_fastopen.syn_loss = 0;
  107. tm->tcpm_fastopen.try_exp = 0;
  108. tm->tcpm_fastopen.cookie.exp = false;
  109. tm->tcpm_fastopen.cookie.len = 0;
  110. }
  111. }
  112. #define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
  113. static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
  114. {
  115. if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
  116. tcpm_suck_dst(tm, dst, false);
  117. }
  118. #define TCP_METRICS_RECLAIM_DEPTH 5
  119. #define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
  120. #define deref_locked(p) \
  121. rcu_dereference_protected(p, lockdep_is_held(&tcp_metrics_lock))
  122. static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
  123. struct inetpeer_addr *saddr,
  124. struct inetpeer_addr *daddr,
  125. unsigned int hash)
  126. {
  127. struct tcp_metrics_block *tm;
  128. struct net *net;
  129. bool reclaim = false;
  130. spin_lock_bh(&tcp_metrics_lock);
  131. net = dev_net(dst->dev);
  132. /* While waiting for the spin-lock the cache might have been populated
  133. * with this entry and so we have to check again.
  134. */
  135. tm = __tcp_get_metrics(saddr, daddr, net, hash);
  136. if (tm == TCP_METRICS_RECLAIM_PTR) {
  137. reclaim = true;
  138. tm = NULL;
  139. }
  140. if (tm) {
  141. tcpm_check_stamp(tm, dst);
  142. goto out_unlock;
  143. }
  144. if (unlikely(reclaim)) {
  145. struct tcp_metrics_block *oldest;
  146. oldest = deref_locked(tcp_metrics_hash[hash].chain);
  147. for (tm = deref_locked(oldest->tcpm_next); tm;
  148. tm = deref_locked(tm->tcpm_next)) {
  149. if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
  150. oldest = tm;
  151. }
  152. tm = oldest;
  153. } else {
  154. tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
  155. if (!tm)
  156. goto out_unlock;
  157. }
  158. write_pnet(&tm->tcpm_net, net);
  159. tm->tcpm_saddr = *saddr;
  160. tm->tcpm_daddr = *daddr;
  161. tcpm_suck_dst(tm, dst, true);
  162. if (likely(!reclaim)) {
  163. tm->tcpm_next = tcp_metrics_hash[hash].chain;
  164. rcu_assign_pointer(tcp_metrics_hash[hash].chain, tm);
  165. }
  166. out_unlock:
  167. spin_unlock_bh(&tcp_metrics_lock);
  168. return tm;
  169. }
  170. static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
  171. {
  172. if (tm)
  173. return tm;
  174. if (depth > TCP_METRICS_RECLAIM_DEPTH)
  175. return TCP_METRICS_RECLAIM_PTR;
  176. return NULL;
  177. }
  178. static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
  179. const struct inetpeer_addr *daddr,
  180. struct net *net, unsigned int hash)
  181. {
  182. struct tcp_metrics_block *tm;
  183. int depth = 0;
  184. for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
  185. tm = rcu_dereference(tm->tcpm_next)) {
  186. if (addr_same(&tm->tcpm_saddr, saddr) &&
  187. addr_same(&tm->tcpm_daddr, daddr) &&
  188. net_eq(tm_net(tm), net))
  189. break;
  190. depth++;
  191. }
  192. return tcp_get_encode(tm, depth);
  193. }
  194. static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
  195. struct dst_entry *dst)
  196. {
  197. struct tcp_metrics_block *tm;
  198. struct inetpeer_addr saddr, daddr;
  199. unsigned int hash;
  200. struct net *net;
  201. saddr.family = req->rsk_ops->family;
  202. daddr.family = req->rsk_ops->family;
  203. switch (daddr.family) {
  204. case AF_INET:
  205. inetpeer_set_addr_v4(&saddr, inet_rsk(req)->ir_loc_addr);
  206. inetpeer_set_addr_v4(&daddr, inet_rsk(req)->ir_rmt_addr);
  207. hash = ipv4_addr_hash(inet_rsk(req)->ir_rmt_addr);
  208. break;
  209. #if IS_ENABLED(CONFIG_IPV6)
  210. case AF_INET6:
  211. inetpeer_set_addr_v6(&saddr, &inet_rsk(req)->ir_v6_loc_addr);
  212. inetpeer_set_addr_v6(&daddr, &inet_rsk(req)->ir_v6_rmt_addr);
  213. hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
  214. break;
  215. #endif
  216. default:
  217. return NULL;
  218. }
  219. net = dev_net(dst->dev);
  220. hash ^= net_hash_mix(net);
  221. hash = hash_32(hash, tcp_metrics_hash_log);
  222. for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
  223. tm = rcu_dereference(tm->tcpm_next)) {
  224. if (addr_same(&tm->tcpm_saddr, &saddr) &&
  225. addr_same(&tm->tcpm_daddr, &daddr) &&
  226. net_eq(tm_net(tm), net))
  227. break;
  228. }
  229. tcpm_check_stamp(tm, dst);
  230. return tm;
  231. }
  232. static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
  233. struct dst_entry *dst,
  234. bool create)
  235. {
  236. struct tcp_metrics_block *tm;
  237. struct inetpeer_addr saddr, daddr;
  238. unsigned int hash;
  239. struct net *net;
  240. if (sk->sk_family == AF_INET) {
  241. inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr);
  242. inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr);
  243. hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr);
  244. }
  245. #if IS_ENABLED(CONFIG_IPV6)
  246. else if (sk->sk_family == AF_INET6) {
  247. if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
  248. inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr);
  249. inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr);
  250. hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr);
  251. } else {
  252. inetpeer_set_addr_v6(&saddr, &sk->sk_v6_rcv_saddr);
  253. inetpeer_set_addr_v6(&daddr, &sk->sk_v6_daddr);
  254. hash = ipv6_addr_hash(&sk->sk_v6_daddr);
  255. }
  256. }
  257. #endif
  258. else
  259. return NULL;
  260. net = dev_net(dst->dev);
  261. hash ^= net_hash_mix(net);
  262. hash = hash_32(hash, tcp_metrics_hash_log);
  263. tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
  264. if (tm == TCP_METRICS_RECLAIM_PTR)
  265. tm = NULL;
  266. if (!tm && create)
  267. tm = tcpm_new(dst, &saddr, &daddr, hash);
  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 net *net = sock_net(sk);
  282. struct tcp_metrics_block *tm;
  283. unsigned long rtt;
  284. u32 val;
  285. int m;
  286. sk_dst_confirm(sk);
  287. if (net->ipv4.sysctl_tcp_nometrics_save || !dst)
  288. return;
  289. rcu_read_lock();
  290. if (icsk->icsk_backoff || !tp->srtt_us) {
  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(tm, TCP_METRIC_RTT);
  304. m = rtt - tp->srtt_us;
  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_us;
  312. else
  313. rtt -= (m >> 3);
  314. tcp_metric_set(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_us)
  323. m = tp->mdev_us;
  324. var = tcp_metric_get(tm, TCP_METRIC_RTTVAR);
  325. if (m >= var)
  326. var = m;
  327. else
  328. var -= (var - m) >> 2;
  329. tcp_metric_set(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 (!tcp_in_slow_start(tp) &&
  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 != net->ipv4.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. sk_dst_confirm(sk);
  390. if (!dst)
  391. goto reset;
  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. tp->reordering = val;
  414. crtt = tcp_metric_get(tm, TCP_METRIC_RTT);
  415. rcu_read_unlock();
  416. reset:
  417. /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
  418. * to seed the RTO for later data packets because SYN packets are
  419. * small. Use the per-dst cached values to seed the RTO but keep
  420. * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
  421. * Later the RTO will be updated immediately upon obtaining the first
  422. * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
  423. * influences the first RTO but not later RTT estimation.
  424. *
  425. * But if RTT is not available from the SYN (due to retransmits or
  426. * syn cookies) or the cache, force a conservative 3secs timeout.
  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. if (crtt > tp->srtt_us) {
  438. /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
  439. crtt /= 8 * USEC_PER_SEC / HZ;
  440. inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
  441. } else if (tp->srtt_us == 0) {
  442. /* RFC6298: 5.7 We've failed to get a valid RTT sample from
  443. * 3WHS. This is most likely due to retransmission,
  444. * including spurious one. Reset the RTO back to 3secs
  445. * from the more aggressive 1sec to avoid more spurious
  446. * retransmission.
  447. */
  448. tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK);
  449. tp->mdev_us = tp->mdev_max_us = tp->rttvar_us;
  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_jiffies32;
  462. }
  463. bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst)
  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 (tm && tcp_metric_get(tm, TCP_METRIC_RTT))
  472. ret = true;
  473. else
  474. ret = false;
  475. rcu_read_unlock();
  476. return ret;
  477. }
  478. static DEFINE_SEQLOCK(fastopen_seqlock);
  479. void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
  480. struct tcp_fastopen_cookie *cookie,
  481. int *syn_loss, unsigned long *last_syn_loss)
  482. {
  483. struct tcp_metrics_block *tm;
  484. rcu_read_lock();
  485. tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
  486. if (tm) {
  487. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  488. unsigned int seq;
  489. do {
  490. seq = read_seqbegin(&fastopen_seqlock);
  491. if (tfom->mss)
  492. *mss = tfom->mss;
  493. *cookie = tfom->cookie;
  494. if (cookie->len <= 0 && tfom->try_exp == 1)
  495. cookie->exp = true;
  496. *syn_loss = tfom->syn_loss;
  497. *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
  498. } while (read_seqretry(&fastopen_seqlock, seq));
  499. }
  500. rcu_read_unlock();
  501. }
  502. void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
  503. struct tcp_fastopen_cookie *cookie, bool syn_lost,
  504. u16 try_exp)
  505. {
  506. struct dst_entry *dst = __sk_dst_get(sk);
  507. struct tcp_metrics_block *tm;
  508. if (!dst)
  509. return;
  510. rcu_read_lock();
  511. tm = tcp_get_metrics(sk, dst, true);
  512. if (tm) {
  513. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  514. write_seqlock_bh(&fastopen_seqlock);
  515. if (mss)
  516. tfom->mss = mss;
  517. if (cookie && cookie->len > 0)
  518. tfom->cookie = *cookie;
  519. else if (try_exp > tfom->try_exp &&
  520. tfom->cookie.len <= 0 && !tfom->cookie.exp)
  521. tfom->try_exp = try_exp;
  522. if (syn_lost) {
  523. ++tfom->syn_loss;
  524. tfom->last_syn_loss = jiffies;
  525. } else
  526. tfom->syn_loss = 0;
  527. write_sequnlock_bh(&fastopen_seqlock);
  528. }
  529. rcu_read_unlock();
  530. }
  531. static struct genl_family tcp_metrics_nl_family;
  532. static const struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
  533. [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
  534. [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
  535. .len = sizeof(struct in6_addr), },
  536. /* Following attributes are not received for GET/DEL,
  537. * we keep them for reference
  538. */
  539. #if 0
  540. [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
  541. [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
  542. [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
  543. [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
  544. [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
  545. [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
  546. [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
  547. [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
  548. .len = TCP_FASTOPEN_COOKIE_MAX, },
  549. #endif
  550. };
  551. /* Add attributes, caller cancels its header on failure */
  552. static int tcp_metrics_fill_info(struct sk_buff *msg,
  553. struct tcp_metrics_block *tm)
  554. {
  555. struct nlattr *nest;
  556. int i;
  557. switch (tm->tcpm_daddr.family) {
  558. case AF_INET:
  559. if (nla_put_in_addr(msg, TCP_METRICS_ATTR_ADDR_IPV4,
  560. inetpeer_get_addr_v4(&tm->tcpm_daddr)) < 0)
  561. goto nla_put_failure;
  562. if (nla_put_in_addr(msg, TCP_METRICS_ATTR_SADDR_IPV4,
  563. inetpeer_get_addr_v4(&tm->tcpm_saddr)) < 0)
  564. goto nla_put_failure;
  565. break;
  566. case AF_INET6:
  567. if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_ADDR_IPV6,
  568. inetpeer_get_addr_v6(&tm->tcpm_daddr)) < 0)
  569. goto nla_put_failure;
  570. if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_SADDR_IPV6,
  571. inetpeer_get_addr_v6(&tm->tcpm_saddr)) < 0)
  572. goto nla_put_failure;
  573. break;
  574. default:
  575. return -EAFNOSUPPORT;
  576. }
  577. if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
  578. jiffies - tm->tcpm_stamp,
  579. TCP_METRICS_ATTR_PAD) < 0)
  580. goto nla_put_failure;
  581. {
  582. int n = 0;
  583. nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
  584. if (!nest)
  585. goto nla_put_failure;
  586. for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
  587. u32 val = tm->tcpm_vals[i];
  588. if (!val)
  589. continue;
  590. if (i == TCP_METRIC_RTT) {
  591. if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
  592. val) < 0)
  593. goto nla_put_failure;
  594. n++;
  595. val = max(val / 1000, 1U);
  596. }
  597. if (i == TCP_METRIC_RTTVAR) {
  598. if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
  599. val) < 0)
  600. goto nla_put_failure;
  601. n++;
  602. val = max(val / 1000, 1U);
  603. }
  604. if (nla_put_u32(msg, i + 1, val) < 0)
  605. goto nla_put_failure;
  606. n++;
  607. }
  608. if (n)
  609. nla_nest_end(msg, nest);
  610. else
  611. nla_nest_cancel(msg, nest);
  612. }
  613. {
  614. struct tcp_fastopen_metrics tfom_copy[1], *tfom;
  615. unsigned int seq;
  616. do {
  617. seq = read_seqbegin(&fastopen_seqlock);
  618. tfom_copy[0] = tm->tcpm_fastopen;
  619. } while (read_seqretry(&fastopen_seqlock, seq));
  620. tfom = tfom_copy;
  621. if (tfom->mss &&
  622. nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
  623. tfom->mss) < 0)
  624. goto nla_put_failure;
  625. if (tfom->syn_loss &&
  626. (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
  627. tfom->syn_loss) < 0 ||
  628. nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
  629. jiffies - tfom->last_syn_loss,
  630. TCP_METRICS_ATTR_PAD) < 0))
  631. goto nla_put_failure;
  632. if (tfom->cookie.len > 0 &&
  633. nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
  634. tfom->cookie.len, tfom->cookie.val) < 0)
  635. goto nla_put_failure;
  636. }
  637. return 0;
  638. nla_put_failure:
  639. return -EMSGSIZE;
  640. }
  641. static int tcp_metrics_dump_info(struct sk_buff *skb,
  642. struct netlink_callback *cb,
  643. struct tcp_metrics_block *tm)
  644. {
  645. void *hdr;
  646. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  647. &tcp_metrics_nl_family, NLM_F_MULTI,
  648. TCP_METRICS_CMD_GET);
  649. if (!hdr)
  650. return -EMSGSIZE;
  651. if (tcp_metrics_fill_info(skb, tm) < 0)
  652. goto nla_put_failure;
  653. genlmsg_end(skb, hdr);
  654. return 0;
  655. nla_put_failure:
  656. genlmsg_cancel(skb, hdr);
  657. return -EMSGSIZE;
  658. }
  659. static int tcp_metrics_nl_dump(struct sk_buff *skb,
  660. struct netlink_callback *cb)
  661. {
  662. struct net *net = sock_net(skb->sk);
  663. unsigned int max_rows = 1U << tcp_metrics_hash_log;
  664. unsigned int row, s_row = cb->args[0];
  665. int s_col = cb->args[1], col = s_col;
  666. for (row = s_row; row < max_rows; row++, s_col = 0) {
  667. struct tcp_metrics_block *tm;
  668. struct tcpm_hash_bucket *hb = tcp_metrics_hash + row;
  669. rcu_read_lock();
  670. for (col = 0, tm = rcu_dereference(hb->chain); tm;
  671. tm = rcu_dereference(tm->tcpm_next), col++) {
  672. if (!net_eq(tm_net(tm), net))
  673. continue;
  674. if (col < s_col)
  675. continue;
  676. if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
  677. rcu_read_unlock();
  678. goto done;
  679. }
  680. }
  681. rcu_read_unlock();
  682. }
  683. done:
  684. cb->args[0] = row;
  685. cb->args[1] = col;
  686. return skb->len;
  687. }
  688. static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  689. unsigned int *hash, int optional, int v4, int v6)
  690. {
  691. struct nlattr *a;
  692. a = info->attrs[v4];
  693. if (a) {
  694. inetpeer_set_addr_v4(addr, nla_get_in_addr(a));
  695. if (hash)
  696. *hash = ipv4_addr_hash(inetpeer_get_addr_v4(addr));
  697. return 0;
  698. }
  699. a = info->attrs[v6];
  700. if (a) {
  701. struct in6_addr in6;
  702. if (nla_len(a) != sizeof(struct in6_addr))
  703. return -EINVAL;
  704. in6 = nla_get_in6_addr(a);
  705. inetpeer_set_addr_v6(addr, &in6);
  706. if (hash)
  707. *hash = ipv6_addr_hash(inetpeer_get_addr_v6(addr));
  708. return 0;
  709. }
  710. return optional ? 1 : -EAFNOSUPPORT;
  711. }
  712. static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  713. unsigned int *hash, int optional)
  714. {
  715. return __parse_nl_addr(info, addr, hash, optional,
  716. TCP_METRICS_ATTR_ADDR_IPV4,
  717. TCP_METRICS_ATTR_ADDR_IPV6);
  718. }
  719. static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
  720. {
  721. return __parse_nl_addr(info, addr, NULL, 0,
  722. TCP_METRICS_ATTR_SADDR_IPV4,
  723. TCP_METRICS_ATTR_SADDR_IPV6);
  724. }
  725. static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
  726. {
  727. struct tcp_metrics_block *tm;
  728. struct inetpeer_addr saddr, daddr;
  729. unsigned int hash;
  730. struct sk_buff *msg;
  731. struct net *net = genl_info_net(info);
  732. void *reply;
  733. int ret;
  734. bool src = true;
  735. ret = parse_nl_addr(info, &daddr, &hash, 0);
  736. if (ret < 0)
  737. return ret;
  738. ret = parse_nl_saddr(info, &saddr);
  739. if (ret < 0)
  740. src = false;
  741. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  742. if (!msg)
  743. return -ENOMEM;
  744. reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
  745. info->genlhdr->cmd);
  746. if (!reply)
  747. goto nla_put_failure;
  748. hash ^= net_hash_mix(net);
  749. hash = hash_32(hash, tcp_metrics_hash_log);
  750. ret = -ESRCH;
  751. rcu_read_lock();
  752. for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
  753. tm = rcu_dereference(tm->tcpm_next)) {
  754. if (addr_same(&tm->tcpm_daddr, &daddr) &&
  755. (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
  756. net_eq(tm_net(tm), net)) {
  757. ret = tcp_metrics_fill_info(msg, tm);
  758. break;
  759. }
  760. }
  761. rcu_read_unlock();
  762. if (ret < 0)
  763. goto out_free;
  764. genlmsg_end(msg, reply);
  765. return genlmsg_reply(msg, info);
  766. nla_put_failure:
  767. ret = -EMSGSIZE;
  768. out_free:
  769. nlmsg_free(msg);
  770. return ret;
  771. }
  772. static void tcp_metrics_flush_all(struct net *net)
  773. {
  774. unsigned int max_rows = 1U << tcp_metrics_hash_log;
  775. struct tcpm_hash_bucket *hb = tcp_metrics_hash;
  776. struct tcp_metrics_block *tm;
  777. unsigned int row;
  778. for (row = 0; row < max_rows; row++, hb++) {
  779. struct tcp_metrics_block __rcu **pp;
  780. bool match;
  781. spin_lock_bh(&tcp_metrics_lock);
  782. pp = &hb->chain;
  783. for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
  784. match = net ? net_eq(tm_net(tm), net) :
  785. !atomic_read(&tm_net(tm)->count);
  786. if (match) {
  787. *pp = tm->tcpm_next;
  788. kfree_rcu(tm, rcu_head);
  789. } else {
  790. pp = &tm->tcpm_next;
  791. }
  792. }
  793. spin_unlock_bh(&tcp_metrics_lock);
  794. }
  795. }
  796. static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
  797. {
  798. struct tcpm_hash_bucket *hb;
  799. struct tcp_metrics_block *tm;
  800. struct tcp_metrics_block __rcu **pp;
  801. struct inetpeer_addr saddr, daddr;
  802. unsigned int hash;
  803. struct net *net = genl_info_net(info);
  804. int ret;
  805. bool src = true, found = false;
  806. ret = parse_nl_addr(info, &daddr, &hash, 1);
  807. if (ret < 0)
  808. return ret;
  809. if (ret > 0) {
  810. tcp_metrics_flush_all(net);
  811. return 0;
  812. }
  813. ret = parse_nl_saddr(info, &saddr);
  814. if (ret < 0)
  815. src = false;
  816. hash ^= net_hash_mix(net);
  817. hash = hash_32(hash, tcp_metrics_hash_log);
  818. hb = tcp_metrics_hash + hash;
  819. pp = &hb->chain;
  820. spin_lock_bh(&tcp_metrics_lock);
  821. for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
  822. if (addr_same(&tm->tcpm_daddr, &daddr) &&
  823. (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
  824. net_eq(tm_net(tm), net)) {
  825. *pp = tm->tcpm_next;
  826. kfree_rcu(tm, rcu_head);
  827. found = true;
  828. } else {
  829. pp = &tm->tcpm_next;
  830. }
  831. }
  832. spin_unlock_bh(&tcp_metrics_lock);
  833. if (!found)
  834. return -ESRCH;
  835. return 0;
  836. }
  837. static const struct genl_ops tcp_metrics_nl_ops[] = {
  838. {
  839. .cmd = TCP_METRICS_CMD_GET,
  840. .doit = tcp_metrics_nl_cmd_get,
  841. .dumpit = tcp_metrics_nl_dump,
  842. .policy = tcp_metrics_nl_policy,
  843. },
  844. {
  845. .cmd = TCP_METRICS_CMD_DEL,
  846. .doit = tcp_metrics_nl_cmd_del,
  847. .policy = tcp_metrics_nl_policy,
  848. .flags = GENL_ADMIN_PERM,
  849. },
  850. };
  851. static struct genl_family tcp_metrics_nl_family __ro_after_init = {
  852. .hdrsize = 0,
  853. .name = TCP_METRICS_GENL_NAME,
  854. .version = TCP_METRICS_GENL_VERSION,
  855. .maxattr = TCP_METRICS_ATTR_MAX,
  856. .netnsok = true,
  857. .module = THIS_MODULE,
  858. .ops = tcp_metrics_nl_ops,
  859. .n_ops = ARRAY_SIZE(tcp_metrics_nl_ops),
  860. };
  861. static unsigned int tcpmhash_entries;
  862. static int __init set_tcpmhash_entries(char *str)
  863. {
  864. ssize_t ret;
  865. if (!str)
  866. return 0;
  867. ret = kstrtouint(str, 0, &tcpmhash_entries);
  868. if (ret)
  869. return 0;
  870. return 1;
  871. }
  872. __setup("tcpmhash_entries=", set_tcpmhash_entries);
  873. static int __net_init tcp_net_metrics_init(struct net *net)
  874. {
  875. size_t size;
  876. unsigned int slots;
  877. if (!net_eq(net, &init_net))
  878. return 0;
  879. slots = tcpmhash_entries;
  880. if (!slots) {
  881. if (totalram_pages >= 128 * 1024)
  882. slots = 16 * 1024;
  883. else
  884. slots = 8 * 1024;
  885. }
  886. tcp_metrics_hash_log = order_base_2(slots);
  887. size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log;
  888. tcp_metrics_hash = kvzalloc(size, GFP_KERNEL);
  889. if (!tcp_metrics_hash)
  890. return -ENOMEM;
  891. return 0;
  892. }
  893. static void __net_exit tcp_net_metrics_exit_batch(struct list_head *net_exit_list)
  894. {
  895. tcp_metrics_flush_all(NULL);
  896. }
  897. static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
  898. .init = tcp_net_metrics_init,
  899. .exit_batch = tcp_net_metrics_exit_batch,
  900. };
  901. void __init tcp_metrics_init(void)
  902. {
  903. int ret;
  904. ret = register_pernet_subsys(&tcp_net_metrics_ops);
  905. if (ret < 0)
  906. panic("Could not allocate the tcp_metrics hash table\n");
  907. ret = genl_register_family(&tcp_metrics_nl_family);
  908. if (ret < 0)
  909. panic("Could not register tcp_metrics generic netlink\n");
  910. }