tcp_metrics.c 31 KB

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