tcp_metrics.c 30 KB

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