tcp_metrics.c 30 KB

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