tcp_metrics.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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, bool paws_check)
  509. {
  510. struct tcp_metrics_block *tm;
  511. bool ret;
  512. if (!dst)
  513. return false;
  514. rcu_read_lock();
  515. tm = __tcp_get_metrics_req(req, dst);
  516. if (paws_check) {
  517. if (tm &&
  518. (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
  519. (s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW)
  520. ret = false;
  521. else
  522. ret = true;
  523. } else {
  524. if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
  525. ret = true;
  526. else
  527. ret = false;
  528. }
  529. rcu_read_unlock();
  530. return ret;
  531. }
  532. EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
  533. void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
  534. {
  535. struct tcp_metrics_block *tm;
  536. rcu_read_lock();
  537. tm = tcp_get_metrics(sk, dst, true);
  538. if (tm) {
  539. struct tcp_sock *tp = tcp_sk(sk);
  540. if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
  541. tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
  542. tp->rx_opt.ts_recent = tm->tcpm_ts;
  543. }
  544. }
  545. rcu_read_unlock();
  546. }
  547. EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
  548. /* VJ's idea. Save last timestamp seen from this destination and hold
  549. * it at least for normal timewait interval to use for duplicate
  550. * segment detection in subsequent connections, before they enter
  551. * synchronized state.
  552. */
  553. bool tcp_remember_stamp(struct sock *sk)
  554. {
  555. struct dst_entry *dst = __sk_dst_get(sk);
  556. bool ret = false;
  557. if (dst) {
  558. struct tcp_metrics_block *tm;
  559. rcu_read_lock();
  560. tm = tcp_get_metrics(sk, dst, true);
  561. if (tm) {
  562. struct tcp_sock *tp = tcp_sk(sk);
  563. if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
  564. ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
  565. tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
  566. tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
  567. tm->tcpm_ts = tp->rx_opt.ts_recent;
  568. }
  569. ret = true;
  570. }
  571. rcu_read_unlock();
  572. }
  573. return ret;
  574. }
  575. bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
  576. {
  577. struct tcp_metrics_block *tm;
  578. bool ret = false;
  579. rcu_read_lock();
  580. tm = __tcp_get_metrics_tw(tw);
  581. if (tm) {
  582. const struct tcp_timewait_sock *tcptw;
  583. struct sock *sk = (struct sock *) tw;
  584. tcptw = tcp_twsk(sk);
  585. if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
  586. ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
  587. tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
  588. tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
  589. tm->tcpm_ts = tcptw->tw_ts_recent;
  590. }
  591. ret = true;
  592. }
  593. rcu_read_unlock();
  594. return ret;
  595. }
  596. static DEFINE_SEQLOCK(fastopen_seqlock);
  597. void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
  598. struct tcp_fastopen_cookie *cookie,
  599. int *syn_loss, unsigned long *last_syn_loss)
  600. {
  601. struct tcp_metrics_block *tm;
  602. rcu_read_lock();
  603. tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
  604. if (tm) {
  605. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  606. unsigned int seq;
  607. do {
  608. seq = read_seqbegin(&fastopen_seqlock);
  609. if (tfom->mss)
  610. *mss = tfom->mss;
  611. *cookie = tfom->cookie;
  612. *syn_loss = tfom->syn_loss;
  613. *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
  614. } while (read_seqretry(&fastopen_seqlock, seq));
  615. }
  616. rcu_read_unlock();
  617. }
  618. void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
  619. struct tcp_fastopen_cookie *cookie, bool syn_lost)
  620. {
  621. struct dst_entry *dst = __sk_dst_get(sk);
  622. struct tcp_metrics_block *tm;
  623. if (!dst)
  624. return;
  625. rcu_read_lock();
  626. tm = tcp_get_metrics(sk, dst, true);
  627. if (tm) {
  628. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  629. write_seqlock_bh(&fastopen_seqlock);
  630. if (mss)
  631. tfom->mss = mss;
  632. if (cookie && cookie->len > 0)
  633. tfom->cookie = *cookie;
  634. if (syn_lost) {
  635. ++tfom->syn_loss;
  636. tfom->last_syn_loss = jiffies;
  637. } else
  638. tfom->syn_loss = 0;
  639. write_sequnlock_bh(&fastopen_seqlock);
  640. }
  641. rcu_read_unlock();
  642. }
  643. static struct genl_family tcp_metrics_nl_family = {
  644. .id = GENL_ID_GENERATE,
  645. .hdrsize = 0,
  646. .name = TCP_METRICS_GENL_NAME,
  647. .version = TCP_METRICS_GENL_VERSION,
  648. .maxattr = TCP_METRICS_ATTR_MAX,
  649. .netnsok = true,
  650. };
  651. static struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
  652. [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
  653. [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
  654. .len = sizeof(struct in6_addr), },
  655. /* Following attributes are not received for GET/DEL,
  656. * we keep them for reference
  657. */
  658. #if 0
  659. [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
  660. [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
  661. [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
  662. [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
  663. [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
  664. [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
  665. [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
  666. [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
  667. .len = TCP_FASTOPEN_COOKIE_MAX, },
  668. #endif
  669. };
  670. /* Add attributes, caller cancels its header on failure */
  671. static int tcp_metrics_fill_info(struct sk_buff *msg,
  672. struct tcp_metrics_block *tm)
  673. {
  674. struct nlattr *nest;
  675. int i;
  676. switch (tm->tcpm_daddr.family) {
  677. case AF_INET:
  678. if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4,
  679. tm->tcpm_daddr.addr.a4) < 0)
  680. goto nla_put_failure;
  681. if (nla_put_be32(msg, TCP_METRICS_ATTR_SADDR_IPV4,
  682. tm->tcpm_saddr.addr.a4) < 0)
  683. goto nla_put_failure;
  684. break;
  685. case AF_INET6:
  686. if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16,
  687. tm->tcpm_daddr.addr.a6) < 0)
  688. goto nla_put_failure;
  689. if (nla_put(msg, TCP_METRICS_ATTR_SADDR_IPV6, 16,
  690. tm->tcpm_saddr.addr.a6) < 0)
  691. goto nla_put_failure;
  692. break;
  693. default:
  694. return -EAFNOSUPPORT;
  695. }
  696. if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
  697. jiffies - tm->tcpm_stamp) < 0)
  698. goto nla_put_failure;
  699. if (tm->tcpm_ts_stamp) {
  700. if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP,
  701. (s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0)
  702. goto nla_put_failure;
  703. if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL,
  704. tm->tcpm_ts) < 0)
  705. goto nla_put_failure;
  706. }
  707. {
  708. int n = 0;
  709. nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
  710. if (!nest)
  711. goto nla_put_failure;
  712. for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
  713. u32 val = tm->tcpm_vals[i];
  714. if (!val)
  715. continue;
  716. if (i == TCP_METRIC_RTT) {
  717. if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
  718. val) < 0)
  719. goto nla_put_failure;
  720. n++;
  721. val = max(val / 1000, 1U);
  722. }
  723. if (i == TCP_METRIC_RTTVAR) {
  724. if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
  725. val) < 0)
  726. goto nla_put_failure;
  727. n++;
  728. val = max(val / 1000, 1U);
  729. }
  730. if (nla_put_u32(msg, i + 1, val) < 0)
  731. goto nla_put_failure;
  732. n++;
  733. }
  734. if (n)
  735. nla_nest_end(msg, nest);
  736. else
  737. nla_nest_cancel(msg, nest);
  738. }
  739. {
  740. struct tcp_fastopen_metrics tfom_copy[1], *tfom;
  741. unsigned int seq;
  742. do {
  743. seq = read_seqbegin(&fastopen_seqlock);
  744. tfom_copy[0] = tm->tcpm_fastopen;
  745. } while (read_seqretry(&fastopen_seqlock, seq));
  746. tfom = tfom_copy;
  747. if (tfom->mss &&
  748. nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
  749. tfom->mss) < 0)
  750. goto nla_put_failure;
  751. if (tfom->syn_loss &&
  752. (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
  753. tfom->syn_loss) < 0 ||
  754. nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
  755. jiffies - tfom->last_syn_loss) < 0))
  756. goto nla_put_failure;
  757. if (tfom->cookie.len > 0 &&
  758. nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
  759. tfom->cookie.len, tfom->cookie.val) < 0)
  760. goto nla_put_failure;
  761. }
  762. return 0;
  763. nla_put_failure:
  764. return -EMSGSIZE;
  765. }
  766. static int tcp_metrics_dump_info(struct sk_buff *skb,
  767. struct netlink_callback *cb,
  768. struct tcp_metrics_block *tm)
  769. {
  770. void *hdr;
  771. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  772. &tcp_metrics_nl_family, NLM_F_MULTI,
  773. TCP_METRICS_CMD_GET);
  774. if (!hdr)
  775. return -EMSGSIZE;
  776. if (tcp_metrics_fill_info(skb, tm) < 0)
  777. goto nla_put_failure;
  778. return genlmsg_end(skb, hdr);
  779. nla_put_failure:
  780. genlmsg_cancel(skb, hdr);
  781. return -EMSGSIZE;
  782. }
  783. static int tcp_metrics_nl_dump(struct sk_buff *skb,
  784. struct netlink_callback *cb)
  785. {
  786. struct net *net = sock_net(skb->sk);
  787. unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
  788. unsigned int row, s_row = cb->args[0];
  789. int s_col = cb->args[1], col = s_col;
  790. for (row = s_row; row < max_rows; row++, s_col = 0) {
  791. struct tcp_metrics_block *tm;
  792. struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash + row;
  793. rcu_read_lock();
  794. for (col = 0, tm = rcu_dereference(hb->chain); tm;
  795. tm = rcu_dereference(tm->tcpm_next), col++) {
  796. if (col < s_col)
  797. continue;
  798. if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
  799. rcu_read_unlock();
  800. goto done;
  801. }
  802. }
  803. rcu_read_unlock();
  804. }
  805. done:
  806. cb->args[0] = row;
  807. cb->args[1] = col;
  808. return skb->len;
  809. }
  810. static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  811. unsigned int *hash, int optional, int v4, int v6)
  812. {
  813. struct nlattr *a;
  814. a = info->attrs[v4];
  815. if (a) {
  816. addr->family = AF_INET;
  817. addr->addr.a4 = nla_get_be32(a);
  818. if (hash)
  819. *hash = (__force unsigned int) addr->addr.a4;
  820. return 0;
  821. }
  822. a = info->attrs[v6];
  823. if (a) {
  824. if (nla_len(a) != sizeof(struct in6_addr))
  825. return -EINVAL;
  826. addr->family = AF_INET6;
  827. memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6));
  828. if (hash)
  829. *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
  830. return 0;
  831. }
  832. return optional ? 1 : -EAFNOSUPPORT;
  833. }
  834. static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  835. unsigned int *hash, int optional)
  836. {
  837. return __parse_nl_addr(info, addr, hash, optional,
  838. TCP_METRICS_ATTR_ADDR_IPV4,
  839. TCP_METRICS_ATTR_ADDR_IPV6);
  840. }
  841. static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
  842. {
  843. return __parse_nl_addr(info, addr, NULL, 0,
  844. TCP_METRICS_ATTR_SADDR_IPV4,
  845. TCP_METRICS_ATTR_SADDR_IPV6);
  846. }
  847. static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
  848. {
  849. struct tcp_metrics_block *tm;
  850. struct inetpeer_addr saddr, daddr;
  851. unsigned int hash;
  852. struct sk_buff *msg;
  853. struct net *net = genl_info_net(info);
  854. void *reply;
  855. int ret;
  856. bool src = true;
  857. ret = parse_nl_addr(info, &daddr, &hash, 0);
  858. if (ret < 0)
  859. return ret;
  860. ret = parse_nl_saddr(info, &saddr);
  861. if (ret < 0)
  862. src = false;
  863. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  864. if (!msg)
  865. return -ENOMEM;
  866. reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
  867. info->genlhdr->cmd);
  868. if (!reply)
  869. goto nla_put_failure;
  870. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  871. ret = -ESRCH;
  872. rcu_read_lock();
  873. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  874. tm = rcu_dereference(tm->tcpm_next)) {
  875. if (addr_same(&tm->tcpm_daddr, &daddr) &&
  876. (!src || addr_same(&tm->tcpm_saddr, &saddr))) {
  877. ret = tcp_metrics_fill_info(msg, tm);
  878. break;
  879. }
  880. }
  881. rcu_read_unlock();
  882. if (ret < 0)
  883. goto out_free;
  884. genlmsg_end(msg, reply);
  885. return genlmsg_reply(msg, info);
  886. nla_put_failure:
  887. ret = -EMSGSIZE;
  888. out_free:
  889. nlmsg_free(msg);
  890. return ret;
  891. }
  892. #define deref_locked_genl(p) \
  893. rcu_dereference_protected(p, lockdep_genl_is_held() && \
  894. lockdep_is_held(&tcp_metrics_lock))
  895. #define deref_genl(p) rcu_dereference_protected(p, lockdep_genl_is_held())
  896. static int tcp_metrics_flush_all(struct net *net)
  897. {
  898. unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
  899. struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash;
  900. struct tcp_metrics_block *tm;
  901. unsigned int row;
  902. for (row = 0; row < max_rows; row++, hb++) {
  903. spin_lock_bh(&tcp_metrics_lock);
  904. tm = deref_locked_genl(hb->chain);
  905. if (tm)
  906. hb->chain = NULL;
  907. spin_unlock_bh(&tcp_metrics_lock);
  908. while (tm) {
  909. struct tcp_metrics_block *next;
  910. next = deref_genl(tm->tcpm_next);
  911. kfree_rcu(tm, rcu_head);
  912. tm = next;
  913. }
  914. }
  915. return 0;
  916. }
  917. static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
  918. {
  919. struct tcpm_hash_bucket *hb;
  920. struct tcp_metrics_block *tm;
  921. struct tcp_metrics_block __rcu **pp;
  922. struct inetpeer_addr saddr, daddr;
  923. unsigned int hash;
  924. struct net *net = genl_info_net(info);
  925. int ret;
  926. bool src = true, found = false;
  927. ret = parse_nl_addr(info, &daddr, &hash, 1);
  928. if (ret < 0)
  929. return ret;
  930. if (ret > 0)
  931. return tcp_metrics_flush_all(net);
  932. ret = parse_nl_saddr(info, &saddr);
  933. if (ret < 0)
  934. src = false;
  935. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  936. hb = net->ipv4.tcp_metrics_hash + hash;
  937. pp = &hb->chain;
  938. spin_lock_bh(&tcp_metrics_lock);
  939. for (tm = deref_locked_genl(*pp); tm; tm = deref_locked_genl(*pp)) {
  940. if (addr_same(&tm->tcpm_daddr, &daddr) &&
  941. (!src || addr_same(&tm->tcpm_saddr, &saddr))) {
  942. *pp = tm->tcpm_next;
  943. kfree_rcu(tm, rcu_head);
  944. found = true;
  945. } else {
  946. pp = &tm->tcpm_next;
  947. }
  948. }
  949. spin_unlock_bh(&tcp_metrics_lock);
  950. if (!found)
  951. return -ESRCH;
  952. return 0;
  953. }
  954. static const struct genl_ops tcp_metrics_nl_ops[] = {
  955. {
  956. .cmd = TCP_METRICS_CMD_GET,
  957. .doit = tcp_metrics_nl_cmd_get,
  958. .dumpit = tcp_metrics_nl_dump,
  959. .policy = tcp_metrics_nl_policy,
  960. .flags = GENL_ADMIN_PERM,
  961. },
  962. {
  963. .cmd = TCP_METRICS_CMD_DEL,
  964. .doit = tcp_metrics_nl_cmd_del,
  965. .policy = tcp_metrics_nl_policy,
  966. .flags = GENL_ADMIN_PERM,
  967. },
  968. };
  969. static unsigned int tcpmhash_entries;
  970. static int __init set_tcpmhash_entries(char *str)
  971. {
  972. ssize_t ret;
  973. if (!str)
  974. return 0;
  975. ret = kstrtouint(str, 0, &tcpmhash_entries);
  976. if (ret)
  977. return 0;
  978. return 1;
  979. }
  980. __setup("tcpmhash_entries=", set_tcpmhash_entries);
  981. static int __net_init tcp_net_metrics_init(struct net *net)
  982. {
  983. size_t size;
  984. unsigned int slots;
  985. slots = tcpmhash_entries;
  986. if (!slots) {
  987. if (totalram_pages >= 128 * 1024)
  988. slots = 16 * 1024;
  989. else
  990. slots = 8 * 1024;
  991. }
  992. net->ipv4.tcp_metrics_hash_log = order_base_2(slots);
  993. size = sizeof(struct tcpm_hash_bucket) << net->ipv4.tcp_metrics_hash_log;
  994. net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
  995. if (!net->ipv4.tcp_metrics_hash)
  996. net->ipv4.tcp_metrics_hash = vzalloc(size);
  997. if (!net->ipv4.tcp_metrics_hash)
  998. return -ENOMEM;
  999. return 0;
  1000. }
  1001. static void __net_exit tcp_net_metrics_exit(struct net *net)
  1002. {
  1003. unsigned int i;
  1004. for (i = 0; i < (1U << net->ipv4.tcp_metrics_hash_log) ; i++) {
  1005. struct tcp_metrics_block *tm, *next;
  1006. tm = rcu_dereference_protected(net->ipv4.tcp_metrics_hash[i].chain, 1);
  1007. while (tm) {
  1008. next = rcu_dereference_protected(tm->tcpm_next, 1);
  1009. kfree(tm);
  1010. tm = next;
  1011. }
  1012. }
  1013. kvfree(net->ipv4.tcp_metrics_hash);
  1014. }
  1015. static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
  1016. .init = tcp_net_metrics_init,
  1017. .exit = tcp_net_metrics_exit,
  1018. };
  1019. void __init tcp_metrics_init(void)
  1020. {
  1021. int ret;
  1022. ret = register_pernet_subsys(&tcp_net_metrics_ops);
  1023. if (ret < 0)
  1024. goto cleanup;
  1025. ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
  1026. tcp_metrics_nl_ops);
  1027. if (ret < 0)
  1028. goto cleanup_subsys;
  1029. return;
  1030. cleanup_subsys:
  1031. unregister_pernet_subsys(&tcp_net_metrics_ops);
  1032. cleanup:
  1033. return;
  1034. }