tcp_metrics.c 27 KB

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