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