tcp_metrics.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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;
  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. val = tcp_metric_get(tm, TCP_METRIC_RTT);
  416. if (val == 0 || tp->srtt == 0) {
  417. rcu_read_unlock();
  418. goto reset;
  419. }
  420. /* Initial rtt is determined from SYN,SYN-ACK.
  421. * The segment is small and rtt may appear much
  422. * less than real one. Use per-dst memory
  423. * to make it more realistic.
  424. *
  425. * A bit of theory. RTT is time passed after "normal" sized packet
  426. * is sent until it is ACKed. In normal circumstances sending small
  427. * packets force peer to delay ACKs and calculation is correct too.
  428. * The algorithm is adaptive and, provided we follow specs, it
  429. * NEVER underestimate RTT. BUT! If peer tries to make some clever
  430. * tricks sort of "quick acks" for time long enough to decrease RTT
  431. * to low value, and then abruptly stops to do it and starts to delay
  432. * ACKs, wait for troubles.
  433. */
  434. val = msecs_to_jiffies(val);
  435. if (val > tp->srtt) {
  436. tp->srtt = val;
  437. tp->rtt_seq = tp->snd_nxt;
  438. }
  439. val = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
  440. if (val > tp->mdev) {
  441. tp->mdev = val;
  442. tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
  443. }
  444. rcu_read_unlock();
  445. tcp_set_rto(sk);
  446. reset:
  447. if (tp->srtt == 0) {
  448. /* RFC6298: 5.7 We've failed to get a valid RTT sample from
  449. * 3WHS. This is most likely due to retransmission,
  450. * including spurious one. Reset the RTO back to 3secs
  451. * from the more aggressive 1sec to avoid more spurious
  452. * retransmission.
  453. */
  454. tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_FALLBACK;
  455. inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
  456. }
  457. /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
  458. * retransmitted. In light of RFC6298 more aggressive 1sec
  459. * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
  460. * retransmission has occurred.
  461. */
  462. if (tp->total_retrans > 1)
  463. tp->snd_cwnd = 1;
  464. else
  465. tp->snd_cwnd = tcp_init_cwnd(tp, dst);
  466. tp->snd_cwnd_stamp = tcp_time_stamp;
  467. }
  468. bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check)
  469. {
  470. struct tcp_metrics_block *tm;
  471. bool ret;
  472. if (!dst)
  473. return false;
  474. rcu_read_lock();
  475. tm = __tcp_get_metrics_req(req, dst);
  476. if (paws_check) {
  477. if (tm &&
  478. (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
  479. (s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW)
  480. ret = false;
  481. else
  482. ret = true;
  483. } else {
  484. if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
  485. ret = true;
  486. else
  487. ret = false;
  488. }
  489. rcu_read_unlock();
  490. return ret;
  491. }
  492. EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
  493. void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
  494. {
  495. struct tcp_metrics_block *tm;
  496. rcu_read_lock();
  497. tm = tcp_get_metrics(sk, dst, true);
  498. if (tm) {
  499. struct tcp_sock *tp = tcp_sk(sk);
  500. if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
  501. tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
  502. tp->rx_opt.ts_recent = tm->tcpm_ts;
  503. }
  504. }
  505. rcu_read_unlock();
  506. }
  507. EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
  508. /* VJ's idea. Save last timestamp seen from this destination and hold
  509. * it at least for normal timewait interval to use for duplicate
  510. * segment detection in subsequent connections, before they enter
  511. * synchronized state.
  512. */
  513. bool tcp_remember_stamp(struct sock *sk)
  514. {
  515. struct dst_entry *dst = __sk_dst_get(sk);
  516. bool ret = false;
  517. if (dst) {
  518. struct tcp_metrics_block *tm;
  519. rcu_read_lock();
  520. tm = tcp_get_metrics(sk, dst, true);
  521. if (tm) {
  522. struct tcp_sock *tp = tcp_sk(sk);
  523. if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
  524. ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
  525. tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
  526. tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
  527. tm->tcpm_ts = tp->rx_opt.ts_recent;
  528. }
  529. ret = true;
  530. }
  531. rcu_read_unlock();
  532. }
  533. return ret;
  534. }
  535. bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
  536. {
  537. struct tcp_metrics_block *tm;
  538. bool ret = false;
  539. rcu_read_lock();
  540. tm = __tcp_get_metrics_tw(tw);
  541. if (tm) {
  542. const struct tcp_timewait_sock *tcptw;
  543. struct sock *sk = (struct sock *) tw;
  544. tcptw = tcp_twsk(sk);
  545. if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
  546. ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
  547. tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
  548. tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
  549. tm->tcpm_ts = tcptw->tw_ts_recent;
  550. }
  551. ret = true;
  552. }
  553. rcu_read_unlock();
  554. return ret;
  555. }
  556. static DEFINE_SEQLOCK(fastopen_seqlock);
  557. void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
  558. struct tcp_fastopen_cookie *cookie,
  559. int *syn_loss, unsigned long *last_syn_loss)
  560. {
  561. struct tcp_metrics_block *tm;
  562. rcu_read_lock();
  563. tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
  564. if (tm) {
  565. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  566. unsigned int seq;
  567. do {
  568. seq = read_seqbegin(&fastopen_seqlock);
  569. if (tfom->mss)
  570. *mss = tfom->mss;
  571. *cookie = tfom->cookie;
  572. *syn_loss = tfom->syn_loss;
  573. *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
  574. } while (read_seqretry(&fastopen_seqlock, seq));
  575. }
  576. rcu_read_unlock();
  577. }
  578. void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
  579. struct tcp_fastopen_cookie *cookie, bool syn_lost)
  580. {
  581. struct tcp_metrics_block *tm;
  582. rcu_read_lock();
  583. tm = tcp_get_metrics(sk, __sk_dst_get(sk), true);
  584. if (tm) {
  585. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  586. write_seqlock_bh(&fastopen_seqlock);
  587. tfom->mss = mss;
  588. if (cookie->len > 0)
  589. tfom->cookie = *cookie;
  590. if (syn_lost) {
  591. ++tfom->syn_loss;
  592. tfom->last_syn_loss = jiffies;
  593. } else
  594. tfom->syn_loss = 0;
  595. write_sequnlock_bh(&fastopen_seqlock);
  596. }
  597. rcu_read_unlock();
  598. }
  599. static struct genl_family tcp_metrics_nl_family = {
  600. .id = GENL_ID_GENERATE,
  601. .hdrsize = 0,
  602. .name = TCP_METRICS_GENL_NAME,
  603. .version = TCP_METRICS_GENL_VERSION,
  604. .maxattr = TCP_METRICS_ATTR_MAX,
  605. .netnsok = true,
  606. };
  607. static struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
  608. [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
  609. [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
  610. .len = sizeof(struct in6_addr), },
  611. /* Following attributes are not received for GET/DEL,
  612. * we keep them for reference
  613. */
  614. #if 0
  615. [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
  616. [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
  617. [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
  618. [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
  619. [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
  620. [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
  621. [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
  622. [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
  623. .len = TCP_FASTOPEN_COOKIE_MAX, },
  624. #endif
  625. };
  626. /* Add attributes, caller cancels its header on failure */
  627. static int tcp_metrics_fill_info(struct sk_buff *msg,
  628. struct tcp_metrics_block *tm)
  629. {
  630. struct nlattr *nest;
  631. int i;
  632. switch (tm->tcpm_addr.family) {
  633. case AF_INET:
  634. if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4,
  635. tm->tcpm_addr.addr.a4) < 0)
  636. goto nla_put_failure;
  637. break;
  638. case AF_INET6:
  639. if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16,
  640. tm->tcpm_addr.addr.a6) < 0)
  641. goto nla_put_failure;
  642. break;
  643. default:
  644. return -EAFNOSUPPORT;
  645. }
  646. if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
  647. jiffies - tm->tcpm_stamp) < 0)
  648. goto nla_put_failure;
  649. if (tm->tcpm_ts_stamp) {
  650. if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP,
  651. (s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0)
  652. goto nla_put_failure;
  653. if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL,
  654. tm->tcpm_ts) < 0)
  655. goto nla_put_failure;
  656. }
  657. {
  658. int n = 0;
  659. nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
  660. if (!nest)
  661. goto nla_put_failure;
  662. for (i = 0; i < TCP_METRIC_MAX + 1; i++) {
  663. if (!tm->tcpm_vals[i])
  664. continue;
  665. if (nla_put_u32(msg, i + 1, tm->tcpm_vals[i]) < 0)
  666. goto nla_put_failure;
  667. n++;
  668. }
  669. if (n)
  670. nla_nest_end(msg, nest);
  671. else
  672. nla_nest_cancel(msg, nest);
  673. }
  674. {
  675. struct tcp_fastopen_metrics tfom_copy[1], *tfom;
  676. unsigned int seq;
  677. do {
  678. seq = read_seqbegin(&fastopen_seqlock);
  679. tfom_copy[0] = tm->tcpm_fastopen;
  680. } while (read_seqretry(&fastopen_seqlock, seq));
  681. tfom = tfom_copy;
  682. if (tfom->mss &&
  683. nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
  684. tfom->mss) < 0)
  685. goto nla_put_failure;
  686. if (tfom->syn_loss &&
  687. (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
  688. tfom->syn_loss) < 0 ||
  689. nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
  690. jiffies - tfom->last_syn_loss) < 0))
  691. goto nla_put_failure;
  692. if (tfom->cookie.len > 0 &&
  693. nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
  694. tfom->cookie.len, tfom->cookie.val) < 0)
  695. goto nla_put_failure;
  696. }
  697. return 0;
  698. nla_put_failure:
  699. return -EMSGSIZE;
  700. }
  701. static int tcp_metrics_dump_info(struct sk_buff *skb,
  702. struct netlink_callback *cb,
  703. struct tcp_metrics_block *tm)
  704. {
  705. void *hdr;
  706. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  707. &tcp_metrics_nl_family, NLM_F_MULTI,
  708. TCP_METRICS_CMD_GET);
  709. if (!hdr)
  710. return -EMSGSIZE;
  711. if (tcp_metrics_fill_info(skb, tm) < 0)
  712. goto nla_put_failure;
  713. return genlmsg_end(skb, hdr);
  714. nla_put_failure:
  715. genlmsg_cancel(skb, hdr);
  716. return -EMSGSIZE;
  717. }
  718. static int tcp_metrics_nl_dump(struct sk_buff *skb,
  719. struct netlink_callback *cb)
  720. {
  721. struct net *net = sock_net(skb->sk);
  722. unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
  723. unsigned int row, s_row = cb->args[0];
  724. int s_col = cb->args[1], col = s_col;
  725. for (row = s_row; row < max_rows; row++, s_col = 0) {
  726. struct tcp_metrics_block *tm;
  727. struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash + row;
  728. rcu_read_lock();
  729. for (col = 0, tm = rcu_dereference(hb->chain); tm;
  730. tm = rcu_dereference(tm->tcpm_next), col++) {
  731. if (col < s_col)
  732. continue;
  733. if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
  734. rcu_read_unlock();
  735. goto done;
  736. }
  737. }
  738. rcu_read_unlock();
  739. }
  740. done:
  741. cb->args[0] = row;
  742. cb->args[1] = col;
  743. return skb->len;
  744. }
  745. static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  746. unsigned int *hash, int optional)
  747. {
  748. struct nlattr *a;
  749. a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV4];
  750. if (a) {
  751. addr->family = AF_INET;
  752. addr->addr.a4 = nla_get_be32(a);
  753. *hash = (__force unsigned int) addr->addr.a4;
  754. return 0;
  755. }
  756. a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV6];
  757. if (a) {
  758. if (nla_len(a) != sizeof(struct in6_addr))
  759. return -EINVAL;
  760. addr->family = AF_INET6;
  761. memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6));
  762. *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
  763. return 0;
  764. }
  765. return optional ? 1 : -EAFNOSUPPORT;
  766. }
  767. static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
  768. {
  769. struct tcp_metrics_block *tm;
  770. struct inetpeer_addr addr;
  771. unsigned int hash;
  772. struct sk_buff *msg;
  773. struct net *net = genl_info_net(info);
  774. void *reply;
  775. int ret;
  776. ret = parse_nl_addr(info, &addr, &hash, 0);
  777. if (ret < 0)
  778. return ret;
  779. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  780. if (!msg)
  781. return -ENOMEM;
  782. reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
  783. info->genlhdr->cmd);
  784. if (!reply)
  785. goto nla_put_failure;
  786. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  787. ret = -ESRCH;
  788. rcu_read_lock();
  789. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  790. tm = rcu_dereference(tm->tcpm_next)) {
  791. if (addr_same(&tm->tcpm_addr, &addr)) {
  792. ret = tcp_metrics_fill_info(msg, tm);
  793. break;
  794. }
  795. }
  796. rcu_read_unlock();
  797. if (ret < 0)
  798. goto out_free;
  799. genlmsg_end(msg, reply);
  800. return genlmsg_reply(msg, info);
  801. nla_put_failure:
  802. ret = -EMSGSIZE;
  803. out_free:
  804. nlmsg_free(msg);
  805. return ret;
  806. }
  807. #define deref_locked_genl(p) \
  808. rcu_dereference_protected(p, lockdep_genl_is_held() && \
  809. lockdep_is_held(&tcp_metrics_lock))
  810. #define deref_genl(p) rcu_dereference_protected(p, lockdep_genl_is_held())
  811. static int tcp_metrics_flush_all(struct net *net)
  812. {
  813. unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
  814. struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash;
  815. struct tcp_metrics_block *tm;
  816. unsigned int row;
  817. for (row = 0; row < max_rows; row++, hb++) {
  818. spin_lock_bh(&tcp_metrics_lock);
  819. tm = deref_locked_genl(hb->chain);
  820. if (tm)
  821. hb->chain = NULL;
  822. spin_unlock_bh(&tcp_metrics_lock);
  823. while (tm) {
  824. struct tcp_metrics_block *next;
  825. next = deref_genl(tm->tcpm_next);
  826. kfree_rcu(tm, rcu_head);
  827. tm = next;
  828. }
  829. }
  830. return 0;
  831. }
  832. static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
  833. {
  834. struct tcpm_hash_bucket *hb;
  835. struct tcp_metrics_block *tm;
  836. struct tcp_metrics_block __rcu **pp;
  837. struct inetpeer_addr addr;
  838. unsigned int hash;
  839. struct net *net = genl_info_net(info);
  840. int ret;
  841. ret = parse_nl_addr(info, &addr, &hash, 1);
  842. if (ret < 0)
  843. return ret;
  844. if (ret > 0)
  845. return tcp_metrics_flush_all(net);
  846. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  847. hb = net->ipv4.tcp_metrics_hash + hash;
  848. pp = &hb->chain;
  849. spin_lock_bh(&tcp_metrics_lock);
  850. for (tm = deref_locked_genl(*pp); tm;
  851. pp = &tm->tcpm_next, tm = deref_locked_genl(*pp)) {
  852. if (addr_same(&tm->tcpm_addr, &addr)) {
  853. *pp = tm->tcpm_next;
  854. break;
  855. }
  856. }
  857. spin_unlock_bh(&tcp_metrics_lock);
  858. if (!tm)
  859. return -ESRCH;
  860. kfree_rcu(tm, rcu_head);
  861. return 0;
  862. }
  863. static struct genl_ops tcp_metrics_nl_ops[] = {
  864. {
  865. .cmd = TCP_METRICS_CMD_GET,
  866. .doit = tcp_metrics_nl_cmd_get,
  867. .dumpit = tcp_metrics_nl_dump,
  868. .policy = tcp_metrics_nl_policy,
  869. .flags = GENL_ADMIN_PERM,
  870. },
  871. {
  872. .cmd = TCP_METRICS_CMD_DEL,
  873. .doit = tcp_metrics_nl_cmd_del,
  874. .policy = tcp_metrics_nl_policy,
  875. .flags = GENL_ADMIN_PERM,
  876. },
  877. };
  878. static unsigned int tcpmhash_entries;
  879. static int __init set_tcpmhash_entries(char *str)
  880. {
  881. ssize_t ret;
  882. if (!str)
  883. return 0;
  884. ret = kstrtouint(str, 0, &tcpmhash_entries);
  885. if (ret)
  886. return 0;
  887. return 1;
  888. }
  889. __setup("tcpmhash_entries=", set_tcpmhash_entries);
  890. static int __net_init tcp_net_metrics_init(struct net *net)
  891. {
  892. size_t size;
  893. unsigned int slots;
  894. slots = tcpmhash_entries;
  895. if (!slots) {
  896. if (totalram_pages >= 128 * 1024)
  897. slots = 16 * 1024;
  898. else
  899. slots = 8 * 1024;
  900. }
  901. net->ipv4.tcp_metrics_hash_log = order_base_2(slots);
  902. size = sizeof(struct tcpm_hash_bucket) << net->ipv4.tcp_metrics_hash_log;
  903. net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
  904. if (!net->ipv4.tcp_metrics_hash)
  905. net->ipv4.tcp_metrics_hash = vzalloc(size);
  906. if (!net->ipv4.tcp_metrics_hash)
  907. return -ENOMEM;
  908. return 0;
  909. }
  910. static void __net_exit tcp_net_metrics_exit(struct net *net)
  911. {
  912. unsigned int i;
  913. for (i = 0; i < (1U << net->ipv4.tcp_metrics_hash_log) ; i++) {
  914. struct tcp_metrics_block *tm, *next;
  915. tm = rcu_dereference_protected(net->ipv4.tcp_metrics_hash[i].chain, 1);
  916. while (tm) {
  917. next = rcu_dereference_protected(tm->tcpm_next, 1);
  918. kfree(tm);
  919. tm = next;
  920. }
  921. }
  922. if (is_vmalloc_addr(net->ipv4.tcp_metrics_hash))
  923. vfree(net->ipv4.tcp_metrics_hash);
  924. else
  925. kfree(net->ipv4.tcp_metrics_hash);
  926. }
  927. static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
  928. .init = tcp_net_metrics_init,
  929. .exit = tcp_net_metrics_exit,
  930. };
  931. void __init tcp_metrics_init(void)
  932. {
  933. int ret;
  934. ret = register_pernet_subsys(&tcp_net_metrics_ops);
  935. if (ret < 0)
  936. goto cleanup;
  937. ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
  938. tcp_metrics_nl_ops,
  939. ARRAY_SIZE(tcp_metrics_nl_ops));
  940. if (ret < 0)
  941. goto cleanup_subsys;
  942. return;
  943. cleanup_subsys:
  944. unregister_pernet_subsys(&tcp_net_metrics_ops);
  945. cleanup:
  946. return;
  947. }