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