tcp_metrics.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. sk_dst_confirm(sk);
  329. if (sysctl_tcp_nometrics_save || !dst)
  330. return;
  331. rcu_read_lock();
  332. if (icsk->icsk_backoff || !tp->srtt_us) {
  333. /* This session failed to estimate rtt. Why?
  334. * Probably, no packets returned in time. Reset our
  335. * results.
  336. */
  337. tm = tcp_get_metrics(sk, dst, false);
  338. if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
  339. tcp_metric_set(tm, TCP_METRIC_RTT, 0);
  340. goto out_unlock;
  341. } else
  342. tm = tcp_get_metrics(sk, dst, true);
  343. if (!tm)
  344. goto out_unlock;
  345. rtt = tcp_metric_get(tm, TCP_METRIC_RTT);
  346. m = rtt - tp->srtt_us;
  347. /* If newly calculated rtt larger than stored one, store new
  348. * one. Otherwise, use EWMA. Remember, rtt overestimation is
  349. * always better than underestimation.
  350. */
  351. if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
  352. if (m <= 0)
  353. rtt = tp->srtt_us;
  354. else
  355. rtt -= (m >> 3);
  356. tcp_metric_set(tm, TCP_METRIC_RTT, rtt);
  357. }
  358. if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
  359. unsigned long var;
  360. if (m < 0)
  361. m = -m;
  362. /* Scale deviation to rttvar fixed point */
  363. m >>= 1;
  364. if (m < tp->mdev_us)
  365. m = tp->mdev_us;
  366. var = tcp_metric_get(tm, TCP_METRIC_RTTVAR);
  367. if (m >= var)
  368. var = m;
  369. else
  370. var -= (var - m) >> 2;
  371. tcp_metric_set(tm, TCP_METRIC_RTTVAR, var);
  372. }
  373. if (tcp_in_initial_slowstart(tp)) {
  374. /* Slow start still did not finish. */
  375. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
  376. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  377. if (val && (tp->snd_cwnd >> 1) > val)
  378. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  379. tp->snd_cwnd >> 1);
  380. }
  381. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  382. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  383. if (tp->snd_cwnd > val)
  384. tcp_metric_set(tm, TCP_METRIC_CWND,
  385. tp->snd_cwnd);
  386. }
  387. } else if (!tcp_in_slow_start(tp) &&
  388. icsk->icsk_ca_state == TCP_CA_Open) {
  389. /* Cong. avoidance phase, cwnd is reliable. */
  390. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
  391. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  392. max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
  393. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  394. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  395. tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
  396. }
  397. } else {
  398. /* Else slow start did not finish, cwnd is non-sense,
  399. * ssthresh may be also invalid.
  400. */
  401. if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
  402. val = tcp_metric_get(tm, TCP_METRIC_CWND);
  403. tcp_metric_set(tm, TCP_METRIC_CWND,
  404. (val + tp->snd_ssthresh) >> 1);
  405. }
  406. if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
  407. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  408. if (val && tp->snd_ssthresh > val)
  409. tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
  410. tp->snd_ssthresh);
  411. }
  412. if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
  413. val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
  414. if (val < tp->reordering &&
  415. tp->reordering != net->ipv4.sysctl_tcp_reordering)
  416. tcp_metric_set(tm, TCP_METRIC_REORDERING,
  417. tp->reordering);
  418. }
  419. }
  420. tm->tcpm_stamp = jiffies;
  421. out_unlock:
  422. rcu_read_unlock();
  423. }
  424. /* Initialize metrics on socket. */
  425. void tcp_init_metrics(struct sock *sk)
  426. {
  427. struct dst_entry *dst = __sk_dst_get(sk);
  428. struct tcp_sock *tp = tcp_sk(sk);
  429. struct tcp_metrics_block *tm;
  430. u32 val, crtt = 0; /* cached RTT scaled by 8 */
  431. sk_dst_confirm(sk);
  432. if (!dst)
  433. goto reset;
  434. rcu_read_lock();
  435. tm = tcp_get_metrics(sk, dst, true);
  436. if (!tm) {
  437. rcu_read_unlock();
  438. goto reset;
  439. }
  440. if (tcp_metric_locked(tm, TCP_METRIC_CWND))
  441. tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
  442. val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
  443. if (val) {
  444. tp->snd_ssthresh = val;
  445. if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
  446. tp->snd_ssthresh = tp->snd_cwnd_clamp;
  447. } else {
  448. /* ssthresh may have been reduced unnecessarily during.
  449. * 3WHS. Restore it back to its initial default.
  450. */
  451. tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  452. }
  453. val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
  454. if (val && tp->reordering != val) {
  455. tcp_disable_fack(tp);
  456. tp->reordering = val;
  457. }
  458. crtt = tcp_metric_get(tm, TCP_METRIC_RTT);
  459. rcu_read_unlock();
  460. reset:
  461. /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
  462. * to seed the RTO for later data packets because SYN packets are
  463. * small. Use the per-dst cached values to seed the RTO but keep
  464. * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
  465. * Later the RTO will be updated immediately upon obtaining the first
  466. * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
  467. * influences the first RTO but not later RTT estimation.
  468. *
  469. * But if RTT is not available from the SYN (due to retransmits or
  470. * syn cookies) or the cache, force a conservative 3secs timeout.
  471. *
  472. * A bit of theory. RTT is time passed after "normal" sized packet
  473. * is sent until it is ACKed. In normal circumstances sending small
  474. * packets force peer to delay ACKs and calculation is correct too.
  475. * The algorithm is adaptive and, provided we follow specs, it
  476. * NEVER underestimate RTT. BUT! If peer tries to make some clever
  477. * tricks sort of "quick acks" for time long enough to decrease RTT
  478. * to low value, and then abruptly stops to do it and starts to delay
  479. * ACKs, wait for troubles.
  480. */
  481. if (crtt > tp->srtt_us) {
  482. /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
  483. crtt /= 8 * USEC_PER_SEC / HZ;
  484. inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
  485. } else if (tp->srtt_us == 0) {
  486. /* RFC6298: 5.7 We've failed to get a valid RTT sample from
  487. * 3WHS. This is most likely due to retransmission,
  488. * including spurious one. Reset the RTO back to 3secs
  489. * from the more aggressive 1sec to avoid more spurious
  490. * retransmission.
  491. */
  492. tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK);
  493. tp->mdev_us = tp->mdev_max_us = tp->rttvar_us;
  494. inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
  495. }
  496. /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
  497. * retransmitted. In light of RFC6298 more aggressive 1sec
  498. * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
  499. * retransmission has occurred.
  500. */
  501. if (tp->total_retrans > 1)
  502. tp->snd_cwnd = 1;
  503. else
  504. tp->snd_cwnd = tcp_init_cwnd(tp, dst);
  505. tp->snd_cwnd_stamp = tcp_time_stamp;
  506. }
  507. bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst,
  508. bool paws_check, bool timestamps)
  509. {
  510. struct tcp_metrics_block *tm;
  511. bool ret;
  512. if (!dst)
  513. return false;
  514. rcu_read_lock();
  515. tm = __tcp_get_metrics_req(req, dst);
  516. if (paws_check) {
  517. if (tm &&
  518. (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
  519. ((s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW ||
  520. !timestamps))
  521. ret = false;
  522. else
  523. ret = true;
  524. } else {
  525. if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
  526. ret = true;
  527. else
  528. ret = false;
  529. }
  530. rcu_read_unlock();
  531. return ret;
  532. }
  533. void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
  534. {
  535. struct tcp_metrics_block *tm;
  536. rcu_read_lock();
  537. tm = tcp_get_metrics(sk, dst, true);
  538. if (tm) {
  539. struct tcp_sock *tp = tcp_sk(sk);
  540. if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
  541. tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
  542. tp->rx_opt.ts_recent = tm->tcpm_ts;
  543. }
  544. }
  545. rcu_read_unlock();
  546. }
  547. EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
  548. /* VJ's idea. Save last timestamp seen from this destination and hold
  549. * it at least for normal timewait interval to use for duplicate
  550. * segment detection in subsequent connections, before they enter
  551. * synchronized state.
  552. */
  553. bool tcp_remember_stamp(struct sock *sk)
  554. {
  555. struct dst_entry *dst = __sk_dst_get(sk);
  556. bool ret = false;
  557. if (dst) {
  558. struct tcp_metrics_block *tm;
  559. rcu_read_lock();
  560. tm = tcp_get_metrics(sk, dst, true);
  561. if (tm) {
  562. struct tcp_sock *tp = tcp_sk(sk);
  563. if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
  564. ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
  565. tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
  566. tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
  567. tm->tcpm_ts = tp->rx_opt.ts_recent;
  568. }
  569. ret = true;
  570. }
  571. rcu_read_unlock();
  572. }
  573. return ret;
  574. }
  575. bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
  576. {
  577. struct tcp_metrics_block *tm;
  578. bool ret = false;
  579. rcu_read_lock();
  580. tm = __tcp_get_metrics_tw(tw);
  581. if (tm) {
  582. const struct tcp_timewait_sock *tcptw;
  583. struct sock *sk = (struct sock *) tw;
  584. tcptw = tcp_twsk(sk);
  585. if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
  586. ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
  587. tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
  588. tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
  589. tm->tcpm_ts = tcptw->tw_ts_recent;
  590. }
  591. ret = true;
  592. }
  593. rcu_read_unlock();
  594. return ret;
  595. }
  596. static DEFINE_SEQLOCK(fastopen_seqlock);
  597. void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
  598. struct tcp_fastopen_cookie *cookie,
  599. int *syn_loss, unsigned long *last_syn_loss)
  600. {
  601. struct tcp_metrics_block *tm;
  602. rcu_read_lock();
  603. tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
  604. if (tm) {
  605. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  606. unsigned int seq;
  607. do {
  608. seq = read_seqbegin(&fastopen_seqlock);
  609. if (tfom->mss)
  610. *mss = tfom->mss;
  611. *cookie = tfom->cookie;
  612. if (cookie->len <= 0 && tfom->try_exp == 1)
  613. cookie->exp = true;
  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. u16 try_exp)
  623. {
  624. struct dst_entry *dst = __sk_dst_get(sk);
  625. struct tcp_metrics_block *tm;
  626. if (!dst)
  627. return;
  628. rcu_read_lock();
  629. tm = tcp_get_metrics(sk, dst, true);
  630. if (tm) {
  631. struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
  632. write_seqlock_bh(&fastopen_seqlock);
  633. if (mss)
  634. tfom->mss = mss;
  635. if (cookie && cookie->len > 0)
  636. tfom->cookie = *cookie;
  637. else if (try_exp > tfom->try_exp &&
  638. tfom->cookie.len <= 0 && !tfom->cookie.exp)
  639. tfom->try_exp = try_exp;
  640. if (syn_lost) {
  641. ++tfom->syn_loss;
  642. tfom->last_syn_loss = jiffies;
  643. } else
  644. tfom->syn_loss = 0;
  645. write_sequnlock_bh(&fastopen_seqlock);
  646. }
  647. rcu_read_unlock();
  648. }
  649. static struct genl_family tcp_metrics_nl_family;
  650. static const struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
  651. [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
  652. [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
  653. .len = sizeof(struct in6_addr), },
  654. /* Following attributes are not received for GET/DEL,
  655. * we keep them for reference
  656. */
  657. #if 0
  658. [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
  659. [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
  660. [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
  661. [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
  662. [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
  663. [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
  664. [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
  665. [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
  666. .len = TCP_FASTOPEN_COOKIE_MAX, },
  667. #endif
  668. };
  669. /* Add attributes, caller cancels its header on failure */
  670. static int tcp_metrics_fill_info(struct sk_buff *msg,
  671. struct tcp_metrics_block *tm)
  672. {
  673. struct nlattr *nest;
  674. int i;
  675. switch (tm->tcpm_daddr.family) {
  676. case AF_INET:
  677. if (nla_put_in_addr(msg, TCP_METRICS_ATTR_ADDR_IPV4,
  678. inetpeer_get_addr_v4(&tm->tcpm_daddr)) < 0)
  679. goto nla_put_failure;
  680. if (nla_put_in_addr(msg, TCP_METRICS_ATTR_SADDR_IPV4,
  681. inetpeer_get_addr_v4(&tm->tcpm_saddr)) < 0)
  682. goto nla_put_failure;
  683. break;
  684. case AF_INET6:
  685. if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_ADDR_IPV6,
  686. inetpeer_get_addr_v6(&tm->tcpm_daddr)) < 0)
  687. goto nla_put_failure;
  688. if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_SADDR_IPV6,
  689. inetpeer_get_addr_v6(&tm->tcpm_saddr)) < 0)
  690. goto nla_put_failure;
  691. break;
  692. default:
  693. return -EAFNOSUPPORT;
  694. }
  695. if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
  696. jiffies - tm->tcpm_stamp,
  697. TCP_METRICS_ATTR_PAD) < 0)
  698. goto nla_put_failure;
  699. if (tm->tcpm_ts_stamp) {
  700. if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP,
  701. (s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0)
  702. goto nla_put_failure;
  703. if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL,
  704. tm->tcpm_ts) < 0)
  705. goto nla_put_failure;
  706. }
  707. {
  708. int n = 0;
  709. nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
  710. if (!nest)
  711. goto nla_put_failure;
  712. for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
  713. u32 val = tm->tcpm_vals[i];
  714. if (!val)
  715. continue;
  716. if (i == TCP_METRIC_RTT) {
  717. if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
  718. val) < 0)
  719. goto nla_put_failure;
  720. n++;
  721. val = max(val / 1000, 1U);
  722. }
  723. if (i == TCP_METRIC_RTTVAR) {
  724. if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
  725. val) < 0)
  726. goto nla_put_failure;
  727. n++;
  728. val = max(val / 1000, 1U);
  729. }
  730. if (nla_put_u32(msg, i + 1, val) < 0)
  731. goto nla_put_failure;
  732. n++;
  733. }
  734. if (n)
  735. nla_nest_end(msg, nest);
  736. else
  737. nla_nest_cancel(msg, nest);
  738. }
  739. {
  740. struct tcp_fastopen_metrics tfom_copy[1], *tfom;
  741. unsigned int seq;
  742. do {
  743. seq = read_seqbegin(&fastopen_seqlock);
  744. tfom_copy[0] = tm->tcpm_fastopen;
  745. } while (read_seqretry(&fastopen_seqlock, seq));
  746. tfom = tfom_copy;
  747. if (tfom->mss &&
  748. nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
  749. tfom->mss) < 0)
  750. goto nla_put_failure;
  751. if (tfom->syn_loss &&
  752. (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
  753. tfom->syn_loss) < 0 ||
  754. nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
  755. jiffies - tfom->last_syn_loss,
  756. TCP_METRICS_ATTR_PAD) < 0))
  757. goto nla_put_failure;
  758. if (tfom->cookie.len > 0 &&
  759. nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
  760. tfom->cookie.len, tfom->cookie.val) < 0)
  761. goto nla_put_failure;
  762. }
  763. return 0;
  764. nla_put_failure:
  765. return -EMSGSIZE;
  766. }
  767. static int tcp_metrics_dump_info(struct sk_buff *skb,
  768. struct netlink_callback *cb,
  769. struct tcp_metrics_block *tm)
  770. {
  771. void *hdr;
  772. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  773. &tcp_metrics_nl_family, NLM_F_MULTI,
  774. TCP_METRICS_CMD_GET);
  775. if (!hdr)
  776. return -EMSGSIZE;
  777. if (tcp_metrics_fill_info(skb, tm) < 0)
  778. goto nla_put_failure;
  779. genlmsg_end(skb, hdr);
  780. return 0;
  781. nla_put_failure:
  782. genlmsg_cancel(skb, hdr);
  783. return -EMSGSIZE;
  784. }
  785. static int tcp_metrics_nl_dump(struct sk_buff *skb,
  786. struct netlink_callback *cb)
  787. {
  788. struct net *net = sock_net(skb->sk);
  789. unsigned int max_rows = 1U << tcp_metrics_hash_log;
  790. unsigned int row, s_row = cb->args[0];
  791. int s_col = cb->args[1], col = s_col;
  792. for (row = s_row; row < max_rows; row++, s_col = 0) {
  793. struct tcp_metrics_block *tm;
  794. struct tcpm_hash_bucket *hb = tcp_metrics_hash + row;
  795. rcu_read_lock();
  796. for (col = 0, tm = rcu_dereference(hb->chain); tm;
  797. tm = rcu_dereference(tm->tcpm_next), col++) {
  798. if (!net_eq(tm_net(tm), net))
  799. continue;
  800. if (col < s_col)
  801. continue;
  802. if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
  803. rcu_read_unlock();
  804. goto done;
  805. }
  806. }
  807. rcu_read_unlock();
  808. }
  809. done:
  810. cb->args[0] = row;
  811. cb->args[1] = col;
  812. return skb->len;
  813. }
  814. static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  815. unsigned int *hash, int optional, int v4, int v6)
  816. {
  817. struct nlattr *a;
  818. a = info->attrs[v4];
  819. if (a) {
  820. inetpeer_set_addr_v4(addr, nla_get_in_addr(a));
  821. if (hash)
  822. *hash = ipv4_addr_hash(inetpeer_get_addr_v4(addr));
  823. return 0;
  824. }
  825. a = info->attrs[v6];
  826. if (a) {
  827. struct in6_addr in6;
  828. if (nla_len(a) != sizeof(struct in6_addr))
  829. return -EINVAL;
  830. in6 = nla_get_in6_addr(a);
  831. inetpeer_set_addr_v6(addr, &in6);
  832. if (hash)
  833. *hash = ipv6_addr_hash(inetpeer_get_addr_v6(addr));
  834. return 0;
  835. }
  836. return optional ? 1 : -EAFNOSUPPORT;
  837. }
  838. static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
  839. unsigned int *hash, int optional)
  840. {
  841. return __parse_nl_addr(info, addr, hash, optional,
  842. TCP_METRICS_ATTR_ADDR_IPV4,
  843. TCP_METRICS_ATTR_ADDR_IPV6);
  844. }
  845. static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
  846. {
  847. return __parse_nl_addr(info, addr, NULL, 0,
  848. TCP_METRICS_ATTR_SADDR_IPV4,
  849. TCP_METRICS_ATTR_SADDR_IPV6);
  850. }
  851. static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
  852. {
  853. struct tcp_metrics_block *tm;
  854. struct inetpeer_addr saddr, daddr;
  855. unsigned int hash;
  856. struct sk_buff *msg;
  857. struct net *net = genl_info_net(info);
  858. void *reply;
  859. int ret;
  860. bool src = true;
  861. ret = parse_nl_addr(info, &daddr, &hash, 0);
  862. if (ret < 0)
  863. return ret;
  864. ret = parse_nl_saddr(info, &saddr);
  865. if (ret < 0)
  866. src = false;
  867. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  868. if (!msg)
  869. return -ENOMEM;
  870. reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
  871. info->genlhdr->cmd);
  872. if (!reply)
  873. goto nla_put_failure;
  874. hash ^= net_hash_mix(net);
  875. hash = hash_32(hash, tcp_metrics_hash_log);
  876. ret = -ESRCH;
  877. rcu_read_lock();
  878. for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
  879. tm = rcu_dereference(tm->tcpm_next)) {
  880. if (addr_same(&tm->tcpm_daddr, &daddr) &&
  881. (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
  882. net_eq(tm_net(tm), net)) {
  883. ret = tcp_metrics_fill_info(msg, tm);
  884. break;
  885. }
  886. }
  887. rcu_read_unlock();
  888. if (ret < 0)
  889. goto out_free;
  890. genlmsg_end(msg, reply);
  891. return genlmsg_reply(msg, info);
  892. nla_put_failure:
  893. ret = -EMSGSIZE;
  894. out_free:
  895. nlmsg_free(msg);
  896. return ret;
  897. }
  898. static void tcp_metrics_flush_all(struct net *net)
  899. {
  900. unsigned int max_rows = 1U << tcp_metrics_hash_log;
  901. struct tcpm_hash_bucket *hb = tcp_metrics_hash;
  902. struct tcp_metrics_block *tm;
  903. unsigned int row;
  904. for (row = 0; row < max_rows; row++, hb++) {
  905. struct tcp_metrics_block __rcu **pp;
  906. spin_lock_bh(&tcp_metrics_lock);
  907. pp = &hb->chain;
  908. for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
  909. if (net_eq(tm_net(tm), net)) {
  910. *pp = tm->tcpm_next;
  911. kfree_rcu(tm, rcu_head);
  912. } else {
  913. pp = &tm->tcpm_next;
  914. }
  915. }
  916. spin_unlock_bh(&tcp_metrics_lock);
  917. }
  918. }
  919. static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
  920. {
  921. struct tcpm_hash_bucket *hb;
  922. struct tcp_metrics_block *tm;
  923. struct tcp_metrics_block __rcu **pp;
  924. struct inetpeer_addr saddr, daddr;
  925. unsigned int hash;
  926. struct net *net = genl_info_net(info);
  927. int ret;
  928. bool src = true, found = false;
  929. ret = parse_nl_addr(info, &daddr, &hash, 1);
  930. if (ret < 0)
  931. return ret;
  932. if (ret > 0) {
  933. tcp_metrics_flush_all(net);
  934. return 0;
  935. }
  936. ret = parse_nl_saddr(info, &saddr);
  937. if (ret < 0)
  938. src = false;
  939. hash ^= net_hash_mix(net);
  940. hash = hash_32(hash, tcp_metrics_hash_log);
  941. hb = tcp_metrics_hash + hash;
  942. pp = &hb->chain;
  943. spin_lock_bh(&tcp_metrics_lock);
  944. for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
  945. if (addr_same(&tm->tcpm_daddr, &daddr) &&
  946. (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
  947. net_eq(tm_net(tm), net)) {
  948. *pp = tm->tcpm_next;
  949. kfree_rcu(tm, rcu_head);
  950. found = true;
  951. } else {
  952. pp = &tm->tcpm_next;
  953. }
  954. }
  955. spin_unlock_bh(&tcp_metrics_lock);
  956. if (!found)
  957. return -ESRCH;
  958. return 0;
  959. }
  960. static const struct genl_ops tcp_metrics_nl_ops[] = {
  961. {
  962. .cmd = TCP_METRICS_CMD_GET,
  963. .doit = tcp_metrics_nl_cmd_get,
  964. .dumpit = tcp_metrics_nl_dump,
  965. .policy = tcp_metrics_nl_policy,
  966. },
  967. {
  968. .cmd = TCP_METRICS_CMD_DEL,
  969. .doit = tcp_metrics_nl_cmd_del,
  970. .policy = tcp_metrics_nl_policy,
  971. .flags = GENL_ADMIN_PERM,
  972. },
  973. };
  974. static struct genl_family tcp_metrics_nl_family __ro_after_init = {
  975. .hdrsize = 0,
  976. .name = TCP_METRICS_GENL_NAME,
  977. .version = TCP_METRICS_GENL_VERSION,
  978. .maxattr = TCP_METRICS_ATTR_MAX,
  979. .netnsok = true,
  980. .module = THIS_MODULE,
  981. .ops = tcp_metrics_nl_ops,
  982. .n_ops = ARRAY_SIZE(tcp_metrics_nl_ops),
  983. };
  984. static unsigned int tcpmhash_entries;
  985. static int __init set_tcpmhash_entries(char *str)
  986. {
  987. ssize_t ret;
  988. if (!str)
  989. return 0;
  990. ret = kstrtouint(str, 0, &tcpmhash_entries);
  991. if (ret)
  992. return 0;
  993. return 1;
  994. }
  995. __setup("tcpmhash_entries=", set_tcpmhash_entries);
  996. static int __net_init tcp_net_metrics_init(struct net *net)
  997. {
  998. size_t size;
  999. unsigned int slots;
  1000. if (!net_eq(net, &init_net))
  1001. return 0;
  1002. slots = tcpmhash_entries;
  1003. if (!slots) {
  1004. if (totalram_pages >= 128 * 1024)
  1005. slots = 16 * 1024;
  1006. else
  1007. slots = 8 * 1024;
  1008. }
  1009. tcp_metrics_hash_log = order_base_2(slots);
  1010. size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log;
  1011. tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
  1012. if (!tcp_metrics_hash)
  1013. tcp_metrics_hash = vzalloc(size);
  1014. if (!tcp_metrics_hash)
  1015. return -ENOMEM;
  1016. return 0;
  1017. }
  1018. static void __net_exit tcp_net_metrics_exit(struct net *net)
  1019. {
  1020. tcp_metrics_flush_all(net);
  1021. }
  1022. static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
  1023. .init = tcp_net_metrics_init,
  1024. .exit = tcp_net_metrics_exit,
  1025. };
  1026. void __init tcp_metrics_init(void)
  1027. {
  1028. int ret;
  1029. ret = register_pernet_subsys(&tcp_net_metrics_ops);
  1030. if (ret < 0)
  1031. panic("Could not allocate the tcp_metrics hash table\n");
  1032. ret = genl_register_family(&tcp_metrics_nl_family);
  1033. if (ret < 0)
  1034. panic("Could not register tcp_metrics generic netlink\n");
  1035. }