tcp_metrics.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. {
  482. struct tcp_metrics_block *tm;
  483. rcu_read_lock();
  484. tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
  485. if (tm) {
  486. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  487. unsigned int seq;
  488. do {
  489. seq = read_seqbegin(&fastopen_seqlock);
  490. if (tfom->mss)
  491. *mss = tfom->mss;
  492. *cookie = tfom->cookie;
  493. if (cookie->len <= 0 && tfom->try_exp == 1)
  494. cookie->exp = true;
  495. } while (read_seqretry(&fastopen_seqlock, seq));
  496. }
  497. rcu_read_unlock();
  498. }
  499. void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
  500. struct tcp_fastopen_cookie *cookie, bool syn_lost,
  501. u16 try_exp)
  502. {
  503. struct dst_entry *dst = __sk_dst_get(sk);
  504. struct tcp_metrics_block *tm;
  505. if (!dst)
  506. return;
  507. rcu_read_lock();
  508. tm = tcp_get_metrics(sk, dst, true);
  509. if (tm) {
  510. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  511. write_seqlock_bh(&fastopen_seqlock);
  512. if (mss)
  513. tfom->mss = mss;
  514. if (cookie && cookie->len > 0)
  515. tfom->cookie = *cookie;
  516. else if (try_exp > tfom->try_exp &&
  517. tfom->cookie.len <= 0 && !tfom->cookie.exp)
  518. tfom->try_exp = try_exp;
  519. if (syn_lost) {
  520. ++tfom->syn_loss;
  521. tfom->last_syn_loss = jiffies;
  522. } else
  523. tfom->syn_loss = 0;
  524. write_sequnlock_bh(&fastopen_seqlock);
  525. }
  526. rcu_read_unlock();
  527. }
  528. static struct genl_family tcp_metrics_nl_family;
  529. static const struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
  530. [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
  531. [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
  532. .len = sizeof(struct in6_addr), },
  533. /* Following attributes are not received for GET/DEL,
  534. * we keep them for reference
  535. */
  536. #if 0
  537. [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
  538. [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
  539. [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
  540. [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
  541. [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
  542. [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
  543. [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
  544. [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
  545. .len = TCP_FASTOPEN_COOKIE_MAX, },
  546. #endif
  547. };
  548. /* Add attributes, caller cancels its header on failure */
  549. static int tcp_metrics_fill_info(struct sk_buff *msg,
  550. struct tcp_metrics_block *tm)
  551. {
  552. struct nlattr *nest;
  553. int i;
  554. switch (tm->tcpm_daddr.family) {
  555. case AF_INET:
  556. if (nla_put_in_addr(msg, TCP_METRICS_ATTR_ADDR_IPV4,
  557. inetpeer_get_addr_v4(&tm->tcpm_daddr)) < 0)
  558. goto nla_put_failure;
  559. if (nla_put_in_addr(msg, TCP_METRICS_ATTR_SADDR_IPV4,
  560. inetpeer_get_addr_v4(&tm->tcpm_saddr)) < 0)
  561. goto nla_put_failure;
  562. break;
  563. case AF_INET6:
  564. if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_ADDR_IPV6,
  565. inetpeer_get_addr_v6(&tm->tcpm_daddr)) < 0)
  566. goto nla_put_failure;
  567. if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_SADDR_IPV6,
  568. inetpeer_get_addr_v6(&tm->tcpm_saddr)) < 0)
  569. goto nla_put_failure;
  570. break;
  571. default:
  572. return -EAFNOSUPPORT;
  573. }
  574. if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
  575. jiffies - tm->tcpm_stamp,
  576. TCP_METRICS_ATTR_PAD) < 0)
  577. goto nla_put_failure;
  578. {
  579. int n = 0;
  580. nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
  581. if (!nest)
  582. goto nla_put_failure;
  583. for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
  584. u32 val = tm->tcpm_vals[i];
  585. if (!val)
  586. continue;
  587. if (i == TCP_METRIC_RTT) {
  588. if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
  589. val) < 0)
  590. goto nla_put_failure;
  591. n++;
  592. val = max(val / 1000, 1U);
  593. }
  594. if (i == TCP_METRIC_RTTVAR) {
  595. if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
  596. val) < 0)
  597. goto nla_put_failure;
  598. n++;
  599. val = max(val / 1000, 1U);
  600. }
  601. if (nla_put_u32(msg, i + 1, val) < 0)
  602. goto nla_put_failure;
  603. n++;
  604. }
  605. if (n)
  606. nla_nest_end(msg, nest);
  607. else
  608. nla_nest_cancel(msg, nest);
  609. }
  610. {
  611. struct tcp_fastopen_metrics tfom_copy[1], *tfom;
  612. unsigned int seq;
  613. do {
  614. seq = read_seqbegin(&fastopen_seqlock);
  615. tfom_copy[0] = tm->tcpm_fastopen;
  616. } while (read_seqretry(&fastopen_seqlock, seq));
  617. tfom = tfom_copy;
  618. if (tfom->mss &&
  619. nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
  620. tfom->mss) < 0)
  621. goto nla_put_failure;
  622. if (tfom->syn_loss &&
  623. (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
  624. tfom->syn_loss) < 0 ||
  625. nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
  626. jiffies - tfom->last_syn_loss,
  627. TCP_METRICS_ATTR_PAD) < 0))
  628. goto nla_put_failure;
  629. if (tfom->cookie.len > 0 &&
  630. nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
  631. tfom->cookie.len, tfom->cookie.val) < 0)
  632. goto nla_put_failure;
  633. }
  634. return 0;
  635. nla_put_failure:
  636. return -EMSGSIZE;
  637. }
  638. static int tcp_metrics_dump_info(struct sk_buff *skb,
  639. struct netlink_callback *cb,
  640. struct tcp_metrics_block *tm)
  641. {
  642. void *hdr;
  643. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  644. &tcp_metrics_nl_family, NLM_F_MULTI,
  645. TCP_METRICS_CMD_GET);
  646. if (!hdr)
  647. return -EMSGSIZE;
  648. if (tcp_metrics_fill_info(skb, tm) < 0)
  649. goto nla_put_failure;
  650. genlmsg_end(skb, hdr);
  651. return 0;
  652. nla_put_failure:
  653. genlmsg_cancel(skb, hdr);
  654. return -EMSGSIZE;
  655. }
  656. static int tcp_metrics_nl_dump(struct sk_buff *skb,
  657. struct netlink_callback *cb)
  658. {
  659. struct net *net = sock_net(skb->sk);
  660. unsigned int max_rows = 1U << tcp_metrics_hash_log;
  661. unsigned int row, s_row = cb->args[0];
  662. int s_col = cb->args[1], col = s_col;
  663. for (row = s_row; row < max_rows; row++, s_col = 0) {
  664. struct tcp_metrics_block *tm;
  665. struct tcpm_hash_bucket *hb = tcp_metrics_hash + row;
  666. rcu_read_lock();
  667. for (col = 0, tm = rcu_dereference(hb->chain); tm;
  668. tm = rcu_dereference(tm->tcpm_next), col++) {
  669. if (!net_eq(tm_net(tm), net))
  670. continue;
  671. if (col < s_col)
  672. continue;
  673. if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
  674. rcu_read_unlock();
  675. goto done;
  676. }
  677. }
  678. rcu_read_unlock();
  679. }
  680. done:
  681. cb->args[0] = row;
  682. cb->args[1] = col;
  683. return skb->len;
  684. }
  685. static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  686. unsigned int *hash, int optional, int v4, int v6)
  687. {
  688. struct nlattr *a;
  689. a = info->attrs[v4];
  690. if (a) {
  691. inetpeer_set_addr_v4(addr, nla_get_in_addr(a));
  692. if (hash)
  693. *hash = ipv4_addr_hash(inetpeer_get_addr_v4(addr));
  694. return 0;
  695. }
  696. a = info->attrs[v6];
  697. if (a) {
  698. struct in6_addr in6;
  699. if (nla_len(a) != sizeof(struct in6_addr))
  700. return -EINVAL;
  701. in6 = nla_get_in6_addr(a);
  702. inetpeer_set_addr_v6(addr, &in6);
  703. if (hash)
  704. *hash = ipv6_addr_hash(inetpeer_get_addr_v6(addr));
  705. return 0;
  706. }
  707. return optional ? 1 : -EAFNOSUPPORT;
  708. }
  709. static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  710. unsigned int *hash, int optional)
  711. {
  712. return __parse_nl_addr(info, addr, hash, optional,
  713. TCP_METRICS_ATTR_ADDR_IPV4,
  714. TCP_METRICS_ATTR_ADDR_IPV6);
  715. }
  716. static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
  717. {
  718. return __parse_nl_addr(info, addr, NULL, 0,
  719. TCP_METRICS_ATTR_SADDR_IPV4,
  720. TCP_METRICS_ATTR_SADDR_IPV6);
  721. }
  722. static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
  723. {
  724. struct tcp_metrics_block *tm;
  725. struct inetpeer_addr saddr, daddr;
  726. unsigned int hash;
  727. struct sk_buff *msg;
  728. struct net *net = genl_info_net(info);
  729. void *reply;
  730. int ret;
  731. bool src = true;
  732. ret = parse_nl_addr(info, &daddr, &hash, 0);
  733. if (ret < 0)
  734. return ret;
  735. ret = parse_nl_saddr(info, &saddr);
  736. if (ret < 0)
  737. src = false;
  738. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  739. if (!msg)
  740. return -ENOMEM;
  741. reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
  742. info->genlhdr->cmd);
  743. if (!reply)
  744. goto nla_put_failure;
  745. hash ^= net_hash_mix(net);
  746. hash = hash_32(hash, tcp_metrics_hash_log);
  747. ret = -ESRCH;
  748. rcu_read_lock();
  749. for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
  750. tm = rcu_dereference(tm->tcpm_next)) {
  751. if (addr_same(&tm->tcpm_daddr, &daddr) &&
  752. (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
  753. net_eq(tm_net(tm), net)) {
  754. ret = tcp_metrics_fill_info(msg, tm);
  755. break;
  756. }
  757. }
  758. rcu_read_unlock();
  759. if (ret < 0)
  760. goto out_free;
  761. genlmsg_end(msg, reply);
  762. return genlmsg_reply(msg, info);
  763. nla_put_failure:
  764. ret = -EMSGSIZE;
  765. out_free:
  766. nlmsg_free(msg);
  767. return ret;
  768. }
  769. static void tcp_metrics_flush_all(struct net *net)
  770. {
  771. unsigned int max_rows = 1U << tcp_metrics_hash_log;
  772. struct tcpm_hash_bucket *hb = tcp_metrics_hash;
  773. struct tcp_metrics_block *tm;
  774. unsigned int row;
  775. for (row = 0; row < max_rows; row++, hb++) {
  776. struct tcp_metrics_block __rcu **pp;
  777. bool match;
  778. spin_lock_bh(&tcp_metrics_lock);
  779. pp = &hb->chain;
  780. for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
  781. match = net ? net_eq(tm_net(tm), net) :
  782. !refcount_read(&tm_net(tm)->count);
  783. if (match) {
  784. *pp = tm->tcpm_next;
  785. kfree_rcu(tm, rcu_head);
  786. } else {
  787. pp = &tm->tcpm_next;
  788. }
  789. }
  790. spin_unlock_bh(&tcp_metrics_lock);
  791. }
  792. }
  793. static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
  794. {
  795. struct tcpm_hash_bucket *hb;
  796. struct tcp_metrics_block *tm;
  797. struct tcp_metrics_block __rcu **pp;
  798. struct inetpeer_addr saddr, daddr;
  799. unsigned int hash;
  800. struct net *net = genl_info_net(info);
  801. int ret;
  802. bool src = true, found = false;
  803. ret = parse_nl_addr(info, &daddr, &hash, 1);
  804. if (ret < 0)
  805. return ret;
  806. if (ret > 0) {
  807. tcp_metrics_flush_all(net);
  808. return 0;
  809. }
  810. ret = parse_nl_saddr(info, &saddr);
  811. if (ret < 0)
  812. src = false;
  813. hash ^= net_hash_mix(net);
  814. hash = hash_32(hash, tcp_metrics_hash_log);
  815. hb = tcp_metrics_hash + hash;
  816. pp = &hb->chain;
  817. spin_lock_bh(&tcp_metrics_lock);
  818. for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
  819. if (addr_same(&tm->tcpm_daddr, &daddr) &&
  820. (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
  821. net_eq(tm_net(tm), net)) {
  822. *pp = tm->tcpm_next;
  823. kfree_rcu(tm, rcu_head);
  824. found = true;
  825. } else {
  826. pp = &tm->tcpm_next;
  827. }
  828. }
  829. spin_unlock_bh(&tcp_metrics_lock);
  830. if (!found)
  831. return -ESRCH;
  832. return 0;
  833. }
  834. static const struct genl_ops tcp_metrics_nl_ops[] = {
  835. {
  836. .cmd = TCP_METRICS_CMD_GET,
  837. .doit = tcp_metrics_nl_cmd_get,
  838. .dumpit = tcp_metrics_nl_dump,
  839. .policy = tcp_metrics_nl_policy,
  840. },
  841. {
  842. .cmd = TCP_METRICS_CMD_DEL,
  843. .doit = tcp_metrics_nl_cmd_del,
  844. .policy = tcp_metrics_nl_policy,
  845. .flags = GENL_ADMIN_PERM,
  846. },
  847. };
  848. static struct genl_family tcp_metrics_nl_family __ro_after_init = {
  849. .hdrsize = 0,
  850. .name = TCP_METRICS_GENL_NAME,
  851. .version = TCP_METRICS_GENL_VERSION,
  852. .maxattr = TCP_METRICS_ATTR_MAX,
  853. .netnsok = true,
  854. .module = THIS_MODULE,
  855. .ops = tcp_metrics_nl_ops,
  856. .n_ops = ARRAY_SIZE(tcp_metrics_nl_ops),
  857. };
  858. static unsigned int tcpmhash_entries;
  859. static int __init set_tcpmhash_entries(char *str)
  860. {
  861. ssize_t ret;
  862. if (!str)
  863. return 0;
  864. ret = kstrtouint(str, 0, &tcpmhash_entries);
  865. if (ret)
  866. return 0;
  867. return 1;
  868. }
  869. __setup("tcpmhash_entries=", set_tcpmhash_entries);
  870. static int __net_init tcp_net_metrics_init(struct net *net)
  871. {
  872. size_t size;
  873. unsigned int slots;
  874. if (!net_eq(net, &init_net))
  875. return 0;
  876. slots = tcpmhash_entries;
  877. if (!slots) {
  878. if (totalram_pages >= 128 * 1024)
  879. slots = 16 * 1024;
  880. else
  881. slots = 8 * 1024;
  882. }
  883. tcp_metrics_hash_log = order_base_2(slots);
  884. size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log;
  885. tcp_metrics_hash = kvzalloc(size, GFP_KERNEL);
  886. if (!tcp_metrics_hash)
  887. return -ENOMEM;
  888. return 0;
  889. }
  890. static void __net_exit tcp_net_metrics_exit_batch(struct list_head *net_exit_list)
  891. {
  892. tcp_metrics_flush_all(NULL);
  893. }
  894. static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
  895. .init = tcp_net_metrics_init,
  896. .exit_batch = tcp_net_metrics_exit_batch,
  897. };
  898. void __init tcp_metrics_init(void)
  899. {
  900. int ret;
  901. ret = register_pernet_subsys(&tcp_net_metrics_ops);
  902. if (ret < 0)
  903. panic("Could not allocate the tcp_metrics hash table\n");
  904. ret = genl_register_family(&tcp_metrics_nl_family);
  905. if (ret < 0)
  906. panic("Could not register tcp_metrics generic netlink\n");
  907. }