tcp_metrics.c 27 KB

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