tcp_metrics.c 27 KB

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