tcp_metrics.c 30 KB

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