tcp_metrics.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. struct tcp_metrics_block {
  32. struct tcp_metrics_block __rcu *tcpm_next;
  33. struct inetpeer_addr tcpm_saddr;
  34. struct inetpeer_addr tcpm_daddr;
  35. unsigned long tcpm_stamp;
  36. u32 tcpm_ts;
  37. u32 tcpm_ts_stamp;
  38. u32 tcpm_lock;
  39. u32 tcpm_vals[TCP_METRIC_MAX + 1];
  40. struct tcp_fastopen_metrics tcpm_fastopen;
  41. struct rcu_head rcu_head;
  42. };
  43. static bool tcp_metric_locked(struct tcp_metrics_block *tm,
  44. enum tcp_metric_index idx)
  45. {
  46. return tm->tcpm_lock & (1 << idx);
  47. }
  48. static u32 tcp_metric_get(struct tcp_metrics_block *tm,
  49. enum tcp_metric_index idx)
  50. {
  51. return tm->tcpm_vals[idx];
  52. }
  53. static u32 tcp_metric_get_jiffies(struct tcp_metrics_block *tm,
  54. enum tcp_metric_index idx)
  55. {
  56. return msecs_to_jiffies(tm->tcpm_vals[idx]);
  57. }
  58. static void tcp_metric_set(struct tcp_metrics_block *tm,
  59. enum tcp_metric_index idx,
  60. u32 val)
  61. {
  62. tm->tcpm_vals[idx] = val;
  63. }
  64. static void tcp_metric_set_msecs(struct tcp_metrics_block *tm,
  65. enum tcp_metric_index idx,
  66. u32 val)
  67. {
  68. tm->tcpm_vals[idx] = jiffies_to_msecs(val);
  69. }
  70. static bool addr_same(const struct inetpeer_addr *a,
  71. const struct inetpeer_addr *b)
  72. {
  73. const struct in6_addr *a6, *b6;
  74. if (a->family != b->family)
  75. return false;
  76. if (a->family == AF_INET)
  77. return a->addr.a4 == b->addr.a4;
  78. a6 = (const struct in6_addr *) &a->addr.a6[0];
  79. b6 = (const struct in6_addr *) &b->addr.a6[0];
  80. return ipv6_addr_equal(a6, b6);
  81. }
  82. struct tcpm_hash_bucket {
  83. struct tcp_metrics_block __rcu *chain;
  84. };
  85. static DEFINE_SPINLOCK(tcp_metrics_lock);
  86. static void tcpm_suck_dst(struct tcp_metrics_block *tm, struct dst_entry *dst,
  87. bool fastopen_clear)
  88. {
  89. u32 val;
  90. tm->tcpm_stamp = jiffies;
  91. val = 0;
  92. if (dst_metric_locked(dst, RTAX_RTT))
  93. val |= 1 << TCP_METRIC_RTT;
  94. if (dst_metric_locked(dst, RTAX_RTTVAR))
  95. val |= 1 << TCP_METRIC_RTTVAR;
  96. if (dst_metric_locked(dst, RTAX_SSTHRESH))
  97. val |= 1 << TCP_METRIC_SSTHRESH;
  98. if (dst_metric_locked(dst, RTAX_CWND))
  99. val |= 1 << TCP_METRIC_CWND;
  100. if (dst_metric_locked(dst, RTAX_REORDERING))
  101. val |= 1 << TCP_METRIC_REORDERING;
  102. tm->tcpm_lock = val;
  103. tm->tcpm_vals[TCP_METRIC_RTT] = dst_metric_raw(dst, RTAX_RTT);
  104. tm->tcpm_vals[TCP_METRIC_RTTVAR] = dst_metric_raw(dst, RTAX_RTTVAR);
  105. tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
  106. tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
  107. tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
  108. tm->tcpm_ts = 0;
  109. tm->tcpm_ts_stamp = 0;
  110. if (fastopen_clear) {
  111. tm->tcpm_fastopen.mss = 0;
  112. tm->tcpm_fastopen.syn_loss = 0;
  113. tm->tcpm_fastopen.cookie.len = 0;
  114. }
  115. }
  116. #define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
  117. static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
  118. {
  119. if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
  120. tcpm_suck_dst(tm, dst, false);
  121. }
  122. #define TCP_METRICS_RECLAIM_DEPTH 5
  123. #define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
  124. static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
  125. struct inetpeer_addr *saddr,
  126. struct inetpeer_addr *daddr,
  127. unsigned int hash)
  128. {
  129. struct tcp_metrics_block *tm;
  130. struct net *net;
  131. bool reclaim = false;
  132. spin_lock_bh(&tcp_metrics_lock);
  133. net = dev_net(dst->dev);
  134. /* While waiting for the spin-lock the cache might have been populated
  135. * with this entry and so we have to check again.
  136. */
  137. tm = __tcp_get_metrics(saddr, daddr, net, hash);
  138. if (tm == TCP_METRICS_RECLAIM_PTR) {
  139. reclaim = true;
  140. tm = NULL;
  141. }
  142. if (tm) {
  143. tcpm_check_stamp(tm, dst);
  144. goto out_unlock;
  145. }
  146. if (unlikely(reclaim)) {
  147. struct tcp_metrics_block *oldest;
  148. oldest = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain);
  149. for (tm = rcu_dereference(oldest->tcpm_next); tm;
  150. tm = rcu_dereference(tm->tcpm_next)) {
  151. if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
  152. oldest = tm;
  153. }
  154. tm = oldest;
  155. } else {
  156. tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
  157. if (!tm)
  158. goto out_unlock;
  159. }
  160. tm->tcpm_saddr = *saddr;
  161. tm->tcpm_daddr = *daddr;
  162. tcpm_suck_dst(tm, dst, true);
  163. if (likely(!reclaim)) {
  164. tm->tcpm_next = net->ipv4.tcp_metrics_hash[hash].chain;
  165. rcu_assign_pointer(net->ipv4.tcp_metrics_hash[hash].chain, tm);
  166. }
  167. out_unlock:
  168. spin_unlock_bh(&tcp_metrics_lock);
  169. return tm;
  170. }
  171. static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
  172. {
  173. if (tm)
  174. return tm;
  175. if (depth > TCP_METRICS_RECLAIM_DEPTH)
  176. return TCP_METRICS_RECLAIM_PTR;
  177. return NULL;
  178. }
  179. static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
  180. const struct inetpeer_addr *daddr,
  181. struct net *net, unsigned int hash)
  182. {
  183. struct tcp_metrics_block *tm;
  184. int depth = 0;
  185. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  186. tm = rcu_dereference(tm->tcpm_next)) {
  187. if (addr_same(&tm->tcpm_saddr, saddr) &&
  188. addr_same(&tm->tcpm_daddr, daddr))
  189. break;
  190. depth++;
  191. }
  192. return tcp_get_encode(tm, depth);
  193. }
  194. static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
  195. struct dst_entry *dst)
  196. {
  197. struct tcp_metrics_block *tm;
  198. struct inetpeer_addr saddr, daddr;
  199. unsigned int hash;
  200. struct net *net;
  201. saddr.family = req->rsk_ops->family;
  202. daddr.family = req->rsk_ops->family;
  203. switch (daddr.family) {
  204. case AF_INET:
  205. saddr.addr.a4 = inet_rsk(req)->ir_loc_addr;
  206. daddr.addr.a4 = inet_rsk(req)->ir_rmt_addr;
  207. hash = (__force unsigned int) daddr.addr.a4;
  208. break;
  209. #if IS_ENABLED(CONFIG_IPV6)
  210. case AF_INET6:
  211. *(struct in6_addr *)saddr.addr.a6 = inet_rsk(req)->ir_v6_loc_addr;
  212. *(struct in6_addr *)daddr.addr.a6 = inet_rsk(req)->ir_v6_rmt_addr;
  213. hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
  214. break;
  215. #endif
  216. default:
  217. return NULL;
  218. }
  219. net = dev_net(dst->dev);
  220. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  221. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  222. tm = rcu_dereference(tm->tcpm_next)) {
  223. if (addr_same(&tm->tcpm_saddr, &saddr) &&
  224. addr_same(&tm->tcpm_daddr, &daddr))
  225. break;
  226. }
  227. tcpm_check_stamp(tm, dst);
  228. return tm;
  229. }
  230. static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
  231. {
  232. struct tcp_metrics_block *tm;
  233. struct inetpeer_addr saddr, daddr;
  234. unsigned int hash;
  235. struct net *net;
  236. if (tw->tw_family == AF_INET) {
  237. saddr.family = AF_INET;
  238. saddr.addr.a4 = tw->tw_rcv_saddr;
  239. daddr.family = AF_INET;
  240. daddr.addr.a4 = tw->tw_daddr;
  241. hash = (__force unsigned int) daddr.addr.a4;
  242. }
  243. #if IS_ENABLED(CONFIG_IPV6)
  244. else if (tw->tw_family == AF_INET6) {
  245. if (ipv6_addr_v4mapped(&tw->tw_v6_daddr)) {
  246. saddr.family = AF_INET;
  247. saddr.addr.a4 = tw->tw_rcv_saddr;
  248. daddr.family = AF_INET;
  249. daddr.addr.a4 = tw->tw_daddr;
  250. hash = (__force unsigned int) daddr.addr.a4;
  251. } else {
  252. saddr.family = AF_INET6;
  253. *(struct in6_addr *)saddr.addr.a6 = tw->tw_v6_rcv_saddr;
  254. daddr.family = AF_INET6;
  255. *(struct in6_addr *)daddr.addr.a6 = tw->tw_v6_daddr;
  256. hash = ipv6_addr_hash(&tw->tw_v6_daddr);
  257. }
  258. }
  259. #endif
  260. else
  261. return NULL;
  262. net = twsk_net(tw);
  263. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  264. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  265. tm = rcu_dereference(tm->tcpm_next)) {
  266. if (addr_same(&tm->tcpm_saddr, &saddr) &&
  267. addr_same(&tm->tcpm_daddr, &daddr))
  268. break;
  269. }
  270. return tm;
  271. }
  272. static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
  273. struct dst_entry *dst,
  274. bool create)
  275. {
  276. struct tcp_metrics_block *tm;
  277. struct inetpeer_addr saddr, daddr;
  278. unsigned int hash;
  279. struct net *net;
  280. if (sk->sk_family == AF_INET) {
  281. saddr.family = AF_INET;
  282. saddr.addr.a4 = inet_sk(sk)->inet_saddr;
  283. daddr.family = AF_INET;
  284. daddr.addr.a4 = inet_sk(sk)->inet_daddr;
  285. hash = (__force unsigned int) daddr.addr.a4;
  286. }
  287. #if IS_ENABLED(CONFIG_IPV6)
  288. else if (sk->sk_family == AF_INET6) {
  289. if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
  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. } else {
  296. saddr.family = AF_INET6;
  297. *(struct in6_addr *)saddr.addr.a6 = sk->sk_v6_rcv_saddr;
  298. daddr.family = AF_INET6;
  299. *(struct in6_addr *)daddr.addr.a6 = sk->sk_v6_daddr;
  300. hash = ipv6_addr_hash(&sk->sk_v6_daddr);
  301. }
  302. }
  303. #endif
  304. else
  305. return NULL;
  306. net = dev_net(dst->dev);
  307. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  308. tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
  309. if (tm == TCP_METRICS_RECLAIM_PTR)
  310. tm = NULL;
  311. if (!tm && create)
  312. tm = tcpm_new(dst, &saddr, &daddr, hash);
  313. else
  314. tcpm_check_stamp(tm, dst);
  315. return tm;
  316. }
  317. /* Save metrics learned by this TCP session. This function is called
  318. * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
  319. * or goes from LAST-ACK to CLOSE.
  320. */
  321. void tcp_update_metrics(struct sock *sk)
  322. {
  323. const struct inet_connection_sock *icsk = inet_csk(sk);
  324. struct dst_entry *dst = __sk_dst_get(sk);
  325. struct tcp_sock *tp = tcp_sk(sk);
  326. struct tcp_metrics_block *tm;
  327. unsigned long rtt;
  328. u32 val;
  329. int m;
  330. if (sysctl_tcp_nometrics_save || !dst)
  331. return;
  332. if (dst->flags & DST_HOST)
  333. dst_confirm(dst);
  334. rcu_read_lock();
  335. if (icsk->icsk_backoff || !tp->srtt) {
  336. /* This session failed to estimate rtt. Why?
  337. * Probably, no packets returned in time. Reset our
  338. * results.
  339. */
  340. tm = tcp_get_metrics(sk, dst, false);
  341. if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
  342. tcp_metric_set(tm, TCP_METRIC_RTT, 0);
  343. goto out_unlock;
  344. } else
  345. tm = tcp_get_metrics(sk, dst, true);
  346. if (!tm)
  347. goto out_unlock;
  348. rtt = tcp_metric_get_jiffies(tm, TCP_METRIC_RTT);
  349. m = rtt - tp->srtt;
  350. /* If newly calculated rtt larger than stored one, store new
  351. * one. Otherwise, use EWMA. Remember, rtt overestimation is
  352. * always better than underestimation.
  353. */
  354. if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
  355. if (m <= 0)
  356. rtt = tp->srtt;
  357. else
  358. rtt -= (m >> 3);
  359. tcp_metric_set_msecs(tm, TCP_METRIC_RTT, rtt);
  360. }
  361. if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
  362. unsigned long var;
  363. if (m < 0)
  364. m = -m;
  365. /* Scale deviation to rttvar fixed point */
  366. m >>= 1;
  367. if (m < tp->mdev)
  368. m = tp->mdev;
  369. var = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
  370. if (m >= var)
  371. var = m;
  372. else
  373. var -= (var - m) >> 2;
  374. tcp_metric_set_msecs(tm, TCP_METRIC_RTTVAR, var);
  375. }
  376. if (tcp_in_initial_slowstart(tp)) {
  377. /* Slow start still did not finish. */
  378. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
  379. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  380. if (val && (tp->snd_cwnd >> 1) > val)
  381. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  382. tp->snd_cwnd >> 1);
  383. }
  384. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  385. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  386. if (tp->snd_cwnd > val)
  387. tcp_metric_set(tm, TCP_METRIC_CWND,
  388. tp->snd_cwnd);
  389. }
  390. } else if (tp->snd_cwnd > tp->snd_ssthresh &&
  391. icsk->icsk_ca_state == TCP_CA_Open) {
  392. /* Cong. avoidance phase, cwnd is reliable. */
  393. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
  394. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  395. max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
  396. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  397. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  398. tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
  399. }
  400. } else {
  401. /* Else slow start did not finish, cwnd is non-sense,
  402. * ssthresh may be also invalid.
  403. */
  404. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  405. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  406. tcp_metric_set(tm, TCP_METRIC_CWND,
  407. (val + tp->snd_ssthresh) >> 1);
  408. }
  409. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
  410. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  411. if (val && tp->snd_ssthresh > val)
  412. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  413. tp->snd_ssthresh);
  414. }
  415. if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
  416. val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
  417. if (val < tp->reordering &&
  418. tp->reordering != sysctl_tcp_reordering)
  419. tcp_metric_set(tm, TCP_METRIC_REORDERING,
  420. tp->reordering);
  421. }
  422. }
  423. tm->tcpm_stamp = jiffies;
  424. out_unlock:
  425. rcu_read_unlock();
  426. }
  427. /* Initialize metrics on socket. */
  428. void tcp_init_metrics(struct sock *sk)
  429. {
  430. struct dst_entry *dst = __sk_dst_get(sk);
  431. struct tcp_sock *tp = tcp_sk(sk);
  432. struct tcp_metrics_block *tm;
  433. u32 val, crtt = 0; /* cached RTT scaled by 8 */
  434. if (dst == NULL)
  435. goto reset;
  436. dst_confirm(dst);
  437. rcu_read_lock();
  438. tm = tcp_get_metrics(sk, dst, true);
  439. if (!tm) {
  440. rcu_read_unlock();
  441. goto reset;
  442. }
  443. if (tcp_metric_locked(tm, TCP_METRIC_CWND))
  444. tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
  445. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  446. if (val) {
  447. tp->snd_ssthresh = val;
  448. if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
  449. tp->snd_ssthresh = tp->snd_cwnd_clamp;
  450. } else {
  451. /* ssthresh may have been reduced unnecessarily during.
  452. * 3WHS. Restore it back to its initial default.
  453. */
  454. tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  455. }
  456. val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
  457. if (val && tp->reordering != val) {
  458. tcp_disable_fack(tp);
  459. tcp_disable_early_retrans(tp);
  460. tp->reordering = val;
  461. }
  462. crtt = tcp_metric_get_jiffies(tm, TCP_METRIC_RTT);
  463. rcu_read_unlock();
  464. reset:
  465. /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
  466. * to seed the RTO for later data packets because SYN packets are
  467. * small. Use the per-dst cached values to seed the RTO but keep
  468. * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
  469. * Later the RTO will be updated immediately upon obtaining the first
  470. * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
  471. * influences the first RTO but not later RTT estimation.
  472. *
  473. * But if RTT is not available from the SYN (due to retransmits or
  474. * syn cookies) or the cache, force a conservative 3secs timeout.
  475. *
  476. * A bit of theory. RTT is time passed after "normal" sized packet
  477. * is sent until it is ACKed. In normal circumstances sending small
  478. * packets force peer to delay ACKs and calculation is correct too.
  479. * The algorithm is adaptive and, provided we follow specs, it
  480. * NEVER underestimate RTT. BUT! If peer tries to make some clever
  481. * tricks sort of "quick acks" for time long enough to decrease RTT
  482. * to low value, and then abruptly stops to do it and starts to delay
  483. * ACKs, wait for troubles.
  484. */
  485. if (crtt > tp->srtt) {
  486. /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
  487. crtt >>= 3;
  488. inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
  489. } else if (tp->srtt == 0) {
  490. /* RFC6298: 5.7 We've failed to get a valid RTT sample from
  491. * 3WHS. This is most likely due to retransmission,
  492. * including spurious one. Reset the RTO back to 3secs
  493. * from the more aggressive 1sec to avoid more spurious
  494. * retransmission.
  495. */
  496. tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_FALLBACK;
  497. inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
  498. }
  499. /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
  500. * retransmitted. In light of RFC6298 more aggressive 1sec
  501. * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
  502. * retransmission has occurred.
  503. */
  504. if (tp->total_retrans > 1)
  505. tp->snd_cwnd = 1;
  506. else
  507. tp->snd_cwnd = tcp_init_cwnd(tp, dst);
  508. tp->snd_cwnd_stamp = tcp_time_stamp;
  509. }
  510. bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check)
  511. {
  512. struct tcp_metrics_block *tm;
  513. bool ret;
  514. if (!dst)
  515. return false;
  516. rcu_read_lock();
  517. tm = __tcp_get_metrics_req(req, dst);
  518. if (paws_check) {
  519. if (tm &&
  520. (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
  521. (s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW)
  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 + 1; i++) {
  715. if (!tm->tcpm_vals[i])
  716. continue;
  717. if (nla_put_u32(msg, i + 1, tm->tcpm_vals[i]) < 0)
  718. goto nla_put_failure;
  719. n++;
  720. }
  721. if (n)
  722. nla_nest_end(msg, nest);
  723. else
  724. nla_nest_cancel(msg, nest);
  725. }
  726. {
  727. struct tcp_fastopen_metrics tfom_copy[1], *tfom;
  728. unsigned int seq;
  729. do {
  730. seq = read_seqbegin(&fastopen_seqlock);
  731. tfom_copy[0] = tm->tcpm_fastopen;
  732. } while (read_seqretry(&fastopen_seqlock, seq));
  733. tfom = tfom_copy;
  734. if (tfom->mss &&
  735. nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
  736. tfom->mss) < 0)
  737. goto nla_put_failure;
  738. if (tfom->syn_loss &&
  739. (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
  740. tfom->syn_loss) < 0 ||
  741. nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
  742. jiffies - tfom->last_syn_loss) < 0))
  743. goto nla_put_failure;
  744. if (tfom->cookie.len > 0 &&
  745. nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
  746. tfom->cookie.len, tfom->cookie.val) < 0)
  747. goto nla_put_failure;
  748. }
  749. return 0;
  750. nla_put_failure:
  751. return -EMSGSIZE;
  752. }
  753. static int tcp_metrics_dump_info(struct sk_buff *skb,
  754. struct netlink_callback *cb,
  755. struct tcp_metrics_block *tm)
  756. {
  757. void *hdr;
  758. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  759. &tcp_metrics_nl_family, NLM_F_MULTI,
  760. TCP_METRICS_CMD_GET);
  761. if (!hdr)
  762. return -EMSGSIZE;
  763. if (tcp_metrics_fill_info(skb, tm) < 0)
  764. goto nla_put_failure;
  765. return genlmsg_end(skb, hdr);
  766. nla_put_failure:
  767. genlmsg_cancel(skb, hdr);
  768. return -EMSGSIZE;
  769. }
  770. static int tcp_metrics_nl_dump(struct sk_buff *skb,
  771. struct netlink_callback *cb)
  772. {
  773. struct net *net = sock_net(skb->sk);
  774. unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
  775. unsigned int row, s_row = cb->args[0];
  776. int s_col = cb->args[1], col = s_col;
  777. for (row = s_row; row < max_rows; row++, s_col = 0) {
  778. struct tcp_metrics_block *tm;
  779. struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash + row;
  780. rcu_read_lock();
  781. for (col = 0, tm = rcu_dereference(hb->chain); tm;
  782. tm = rcu_dereference(tm->tcpm_next), col++) {
  783. if (col < s_col)
  784. continue;
  785. if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
  786. rcu_read_unlock();
  787. goto done;
  788. }
  789. }
  790. rcu_read_unlock();
  791. }
  792. done:
  793. cb->args[0] = row;
  794. cb->args[1] = col;
  795. return skb->len;
  796. }
  797. static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  798. unsigned int *hash, int optional, int v4, int v6)
  799. {
  800. struct nlattr *a;
  801. a = info->attrs[v4];
  802. if (a) {
  803. addr->family = AF_INET;
  804. addr->addr.a4 = nla_get_be32(a);
  805. if (hash)
  806. *hash = (__force unsigned int) addr->addr.a4;
  807. return 0;
  808. }
  809. a = info->attrs[v6];
  810. if (a) {
  811. if (nla_len(a) != sizeof(struct in6_addr))
  812. return -EINVAL;
  813. addr->family = AF_INET6;
  814. memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6));
  815. if (hash)
  816. *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
  817. return 0;
  818. }
  819. return optional ? 1 : -EAFNOSUPPORT;
  820. }
  821. static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  822. unsigned int *hash, int optional)
  823. {
  824. return __parse_nl_addr(info, addr, hash, optional,
  825. TCP_METRICS_ATTR_ADDR_IPV4,
  826. TCP_METRICS_ATTR_ADDR_IPV6);
  827. }
  828. static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
  829. {
  830. return __parse_nl_addr(info, addr, NULL, 0,
  831. TCP_METRICS_ATTR_SADDR_IPV4,
  832. TCP_METRICS_ATTR_SADDR_IPV6);
  833. }
  834. static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
  835. {
  836. struct tcp_metrics_block *tm;
  837. struct inetpeer_addr saddr, daddr;
  838. unsigned int hash;
  839. struct sk_buff *msg;
  840. struct net *net = genl_info_net(info);
  841. void *reply;
  842. int ret;
  843. bool src = true;
  844. ret = parse_nl_addr(info, &daddr, &hash, 0);
  845. if (ret < 0)
  846. return ret;
  847. ret = parse_nl_saddr(info, &saddr);
  848. if (ret < 0)
  849. src = false;
  850. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  851. if (!msg)
  852. return -ENOMEM;
  853. reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
  854. info->genlhdr->cmd);
  855. if (!reply)
  856. goto nla_put_failure;
  857. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  858. ret = -ESRCH;
  859. rcu_read_lock();
  860. for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
  861. tm = rcu_dereference(tm->tcpm_next)) {
  862. if (addr_same(&tm->tcpm_daddr, &daddr) &&
  863. (!src || addr_same(&tm->tcpm_saddr, &saddr))) {
  864. ret = tcp_metrics_fill_info(msg, tm);
  865. break;
  866. }
  867. }
  868. rcu_read_unlock();
  869. if (ret < 0)
  870. goto out_free;
  871. genlmsg_end(msg, reply);
  872. return genlmsg_reply(msg, info);
  873. nla_put_failure:
  874. ret = -EMSGSIZE;
  875. out_free:
  876. nlmsg_free(msg);
  877. return ret;
  878. }
  879. #define deref_locked_genl(p) \
  880. rcu_dereference_protected(p, lockdep_genl_is_held() && \
  881. lockdep_is_held(&tcp_metrics_lock))
  882. #define deref_genl(p) rcu_dereference_protected(p, lockdep_genl_is_held())
  883. static int tcp_metrics_flush_all(struct net *net)
  884. {
  885. unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
  886. struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash;
  887. struct tcp_metrics_block *tm;
  888. unsigned int row;
  889. for (row = 0; row < max_rows; row++, hb++) {
  890. spin_lock_bh(&tcp_metrics_lock);
  891. tm = deref_locked_genl(hb->chain);
  892. if (tm)
  893. hb->chain = NULL;
  894. spin_unlock_bh(&tcp_metrics_lock);
  895. while (tm) {
  896. struct tcp_metrics_block *next;
  897. next = deref_genl(tm->tcpm_next);
  898. kfree_rcu(tm, rcu_head);
  899. tm = next;
  900. }
  901. }
  902. return 0;
  903. }
  904. static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
  905. {
  906. struct tcpm_hash_bucket *hb;
  907. struct tcp_metrics_block *tm;
  908. struct tcp_metrics_block __rcu **pp;
  909. struct inetpeer_addr saddr, daddr;
  910. unsigned int hash;
  911. struct net *net = genl_info_net(info);
  912. int ret;
  913. bool src = true, found = false;
  914. ret = parse_nl_addr(info, &daddr, &hash, 1);
  915. if (ret < 0)
  916. return ret;
  917. if (ret > 0)
  918. return tcp_metrics_flush_all(net);
  919. ret = parse_nl_saddr(info, &saddr);
  920. if (ret < 0)
  921. src = false;
  922. hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
  923. hb = net->ipv4.tcp_metrics_hash + hash;
  924. pp = &hb->chain;
  925. spin_lock_bh(&tcp_metrics_lock);
  926. for (tm = deref_locked_genl(*pp); tm; tm = deref_locked_genl(*pp)) {
  927. if (addr_same(&tm->tcpm_daddr, &daddr) &&
  928. (!src || addr_same(&tm->tcpm_saddr, &saddr))) {
  929. *pp = tm->tcpm_next;
  930. kfree_rcu(tm, rcu_head);
  931. found = true;
  932. } else {
  933. pp = &tm->tcpm_next;
  934. }
  935. }
  936. spin_unlock_bh(&tcp_metrics_lock);
  937. if (!found)
  938. return -ESRCH;
  939. return 0;
  940. }
  941. static const struct genl_ops tcp_metrics_nl_ops[] = {
  942. {
  943. .cmd = TCP_METRICS_CMD_GET,
  944. .doit = tcp_metrics_nl_cmd_get,
  945. .dumpit = tcp_metrics_nl_dump,
  946. .policy = tcp_metrics_nl_policy,
  947. .flags = GENL_ADMIN_PERM,
  948. },
  949. {
  950. .cmd = TCP_METRICS_CMD_DEL,
  951. .doit = tcp_metrics_nl_cmd_del,
  952. .policy = tcp_metrics_nl_policy,
  953. .flags = GENL_ADMIN_PERM,
  954. },
  955. };
  956. static unsigned int tcpmhash_entries;
  957. static int __init set_tcpmhash_entries(char *str)
  958. {
  959. ssize_t ret;
  960. if (!str)
  961. return 0;
  962. ret = kstrtouint(str, 0, &tcpmhash_entries);
  963. if (ret)
  964. return 0;
  965. return 1;
  966. }
  967. __setup("tcpmhash_entries=", set_tcpmhash_entries);
  968. static int __net_init tcp_net_metrics_init(struct net *net)
  969. {
  970. size_t size;
  971. unsigned int slots;
  972. slots = tcpmhash_entries;
  973. if (!slots) {
  974. if (totalram_pages >= 128 * 1024)
  975. slots = 16 * 1024;
  976. else
  977. slots = 8 * 1024;
  978. }
  979. net->ipv4.tcp_metrics_hash_log = order_base_2(slots);
  980. size = sizeof(struct tcpm_hash_bucket) << net->ipv4.tcp_metrics_hash_log;
  981. net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
  982. if (!net->ipv4.tcp_metrics_hash)
  983. net->ipv4.tcp_metrics_hash = vzalloc(size);
  984. if (!net->ipv4.tcp_metrics_hash)
  985. return -ENOMEM;
  986. return 0;
  987. }
  988. static void __net_exit tcp_net_metrics_exit(struct net *net)
  989. {
  990. unsigned int i;
  991. for (i = 0; i < (1U << net->ipv4.tcp_metrics_hash_log) ; i++) {
  992. struct tcp_metrics_block *tm, *next;
  993. tm = rcu_dereference_protected(net->ipv4.tcp_metrics_hash[i].chain, 1);
  994. while (tm) {
  995. next = rcu_dereference_protected(tm->tcpm_next, 1);
  996. kfree(tm);
  997. tm = next;
  998. }
  999. }
  1000. if (is_vmalloc_addr(net->ipv4.tcp_metrics_hash))
  1001. vfree(net->ipv4.tcp_metrics_hash);
  1002. else
  1003. kfree(net->ipv4.tcp_metrics_hash);
  1004. }
  1005. static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
  1006. .init = tcp_net_metrics_init,
  1007. .exit = tcp_net_metrics_exit,
  1008. };
  1009. void __init tcp_metrics_init(void)
  1010. {
  1011. int ret;
  1012. ret = register_pernet_subsys(&tcp_net_metrics_ops);
  1013. if (ret < 0)
  1014. goto cleanup;
  1015. ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
  1016. tcp_metrics_nl_ops);
  1017. if (ret < 0)
  1018. goto cleanup_subsys;
  1019. return;
  1020. cleanup_subsys:
  1021. unregister_pernet_subsys(&tcp_net_metrics_ops);
  1022. cleanup:
  1023. return;
  1024. }