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>
9 #include <linux/hash.h>
10 #include <linux/tcp_metrics.h>
11 #include <linux/vmalloc.h>
13 #include <net/inet_connection_sock.h>
14 #include <net/net_namespace.h>
15 #include <net/request_sock.h>
16 #include <net/inetpeer.h>
21 #include <net/genetlink.h>
23 int sysctl_tcp_nometrics_save __read_mostly;
25 static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
26 const struct inetpeer_addr *daddr,
27 struct net *net, unsigned int hash);
29 struct tcp_fastopen_metrics {
31 u16 syn_loss:10; /* Recurring Fast Open SYN losses */
32 unsigned long last_syn_loss; /* Last Fast Open SYN loss */
33 struct tcp_fastopen_cookie cookie;
36 /* TCP_METRIC_MAX includes 2 extra fields for userspace compatibility
37 * Kernel only stores RTT and RTTVAR in usec resolution
39 #define TCP_METRIC_MAX_KERNEL (TCP_METRIC_MAX - 2)
41 struct tcp_metrics_block {
42 struct tcp_metrics_block __rcu *tcpm_next;
43 possible_net_t tcpm_net;
44 struct inetpeer_addr tcpm_saddr;
45 struct inetpeer_addr tcpm_daddr;
46 unsigned long tcpm_stamp;
50 u32 tcpm_vals[TCP_METRIC_MAX_KERNEL + 1];
51 struct tcp_fastopen_metrics tcpm_fastopen;
53 struct rcu_head rcu_head;
56 static inline struct net *tm_net(struct tcp_metrics_block *tm)
58 return read_pnet(&tm->tcpm_net);
61 static bool tcp_metric_locked(struct tcp_metrics_block *tm,
62 enum tcp_metric_index idx)
64 return tm->tcpm_lock & (1 << idx);
67 static u32 tcp_metric_get(struct tcp_metrics_block *tm,
68 enum tcp_metric_index idx)
70 return tm->tcpm_vals[idx];
73 static void tcp_metric_set(struct tcp_metrics_block *tm,
74 enum tcp_metric_index idx,
77 tm->tcpm_vals[idx] = val;
80 static bool addr_same(const struct inetpeer_addr *a,
81 const struct inetpeer_addr *b)
83 const struct in6_addr *a6, *b6;
85 if (a->family != b->family)
87 if (a->family == AF_INET)
88 return a->addr.a4 == b->addr.a4;
90 a6 = (const struct in6_addr *) &a->addr.a6[0];
91 b6 = (const struct in6_addr *) &b->addr.a6[0];
93 return ipv6_addr_equal(a6, b6);
96 struct tcpm_hash_bucket {
97 struct tcp_metrics_block __rcu *chain;
100 static DEFINE_SPINLOCK(tcp_metrics_lock);
102 static void tcpm_suck_dst(struct tcp_metrics_block *tm,
103 const struct dst_entry *dst,
109 tm->tcpm_stamp = jiffies;
112 if (dst_metric_locked(dst, RTAX_RTT))
113 val |= 1 << TCP_METRIC_RTT;
114 if (dst_metric_locked(dst, RTAX_RTTVAR))
115 val |= 1 << TCP_METRIC_RTTVAR;
116 if (dst_metric_locked(dst, RTAX_SSTHRESH))
117 val |= 1 << TCP_METRIC_SSTHRESH;
118 if (dst_metric_locked(dst, RTAX_CWND))
119 val |= 1 << TCP_METRIC_CWND;
120 if (dst_metric_locked(dst, RTAX_REORDERING))
121 val |= 1 << TCP_METRIC_REORDERING;
124 msval = dst_metric_raw(dst, RTAX_RTT);
125 tm->tcpm_vals[TCP_METRIC_RTT] = msval * USEC_PER_MSEC;
127 msval = dst_metric_raw(dst, RTAX_RTTVAR);
128 tm->tcpm_vals[TCP_METRIC_RTTVAR] = msval * USEC_PER_MSEC;
129 tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
130 tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
131 tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
133 tm->tcpm_ts_stamp = 0;
134 if (fastopen_clear) {
135 tm->tcpm_fastopen.mss = 0;
136 tm->tcpm_fastopen.syn_loss = 0;
137 tm->tcpm_fastopen.cookie.len = 0;
141 #define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
143 static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
145 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
146 tcpm_suck_dst(tm, dst, false);
149 #define TCP_METRICS_RECLAIM_DEPTH 5
150 #define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
152 static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
153 struct inetpeer_addr *saddr,
154 struct inetpeer_addr *daddr,
157 struct tcp_metrics_block *tm;
159 bool reclaim = false;
161 spin_lock_bh(&tcp_metrics_lock);
162 net = dev_net(dst->dev);
164 /* While waiting for the spin-lock the cache might have been populated
165 * with this entry and so we have to check again.
167 tm = __tcp_get_metrics(saddr, daddr, net, hash);
168 if (tm == TCP_METRICS_RECLAIM_PTR) {
173 tcpm_check_stamp(tm, dst);
177 if (unlikely(reclaim)) {
178 struct tcp_metrics_block *oldest;
180 oldest = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain);
181 for (tm = rcu_dereference(oldest->tcpm_next); tm;
182 tm = rcu_dereference(tm->tcpm_next)) {
183 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
188 tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
192 write_pnet(&tm->tcpm_net, net);
193 tm->tcpm_saddr = *saddr;
194 tm->tcpm_daddr = *daddr;
196 tcpm_suck_dst(tm, dst, true);
198 if (likely(!reclaim)) {
199 tm->tcpm_next = net->ipv4.tcp_metrics_hash[hash].chain;
200 rcu_assign_pointer(net->ipv4.tcp_metrics_hash[hash].chain, tm);
204 spin_unlock_bh(&tcp_metrics_lock);
208 static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
212 if (depth > TCP_METRICS_RECLAIM_DEPTH)
213 return TCP_METRICS_RECLAIM_PTR;
217 static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
218 const struct inetpeer_addr *daddr,
219 struct net *net, unsigned int hash)
221 struct tcp_metrics_block *tm;
224 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
225 tm = rcu_dereference(tm->tcpm_next)) {
226 if (addr_same(&tm->tcpm_saddr, saddr) &&
227 addr_same(&tm->tcpm_daddr, daddr) &&
228 net_eq(tm_net(tm), net))
232 return tcp_get_encode(tm, depth);
235 static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
236 struct dst_entry *dst)
238 struct tcp_metrics_block *tm;
239 struct inetpeer_addr saddr, daddr;
243 saddr.family = req->rsk_ops->family;
244 daddr.family = req->rsk_ops->family;
245 switch (daddr.family) {
247 saddr.addr.a4 = inet_rsk(req)->ir_loc_addr;
248 daddr.addr.a4 = inet_rsk(req)->ir_rmt_addr;
249 hash = (__force unsigned int) daddr.addr.a4;
251 #if IS_ENABLED(CONFIG_IPV6)
253 *(struct in6_addr *)saddr.addr.a6 = inet_rsk(req)->ir_v6_loc_addr;
254 *(struct in6_addr *)daddr.addr.a6 = inet_rsk(req)->ir_v6_rmt_addr;
255 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
262 net = dev_net(dst->dev);
263 hash ^= net_hash_mix(net);
264 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
266 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
267 tm = rcu_dereference(tm->tcpm_next)) {
268 if (addr_same(&tm->tcpm_saddr, &saddr) &&
269 addr_same(&tm->tcpm_daddr, &daddr) &&
270 net_eq(tm_net(tm), net))
273 tcpm_check_stamp(tm, dst);
277 static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
279 struct tcp_metrics_block *tm;
280 struct inetpeer_addr saddr, daddr;
284 if (tw->tw_family == AF_INET) {
285 saddr.family = AF_INET;
286 saddr.addr.a4 = tw->tw_rcv_saddr;
287 daddr.family = AF_INET;
288 daddr.addr.a4 = tw->tw_daddr;
289 hash = (__force unsigned int) daddr.addr.a4;
291 #if IS_ENABLED(CONFIG_IPV6)
292 else if (tw->tw_family == AF_INET6) {
293 if (ipv6_addr_v4mapped(&tw->tw_v6_daddr)) {
294 saddr.family = AF_INET;
295 saddr.addr.a4 = tw->tw_rcv_saddr;
296 daddr.family = AF_INET;
297 daddr.addr.a4 = tw->tw_daddr;
298 hash = (__force unsigned int) daddr.addr.a4;
300 saddr.family = AF_INET6;
301 *(struct in6_addr *)saddr.addr.a6 = tw->tw_v6_rcv_saddr;
302 daddr.family = AF_INET6;
303 *(struct in6_addr *)daddr.addr.a6 = tw->tw_v6_daddr;
304 hash = ipv6_addr_hash(&tw->tw_v6_daddr);
312 hash ^= net_hash_mix(net);
313 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
315 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
316 tm = rcu_dereference(tm->tcpm_next)) {
317 if (addr_same(&tm->tcpm_saddr, &saddr) &&
318 addr_same(&tm->tcpm_daddr, &daddr) &&
319 net_eq(tm_net(tm), net))
325 static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
326 struct dst_entry *dst,
329 struct tcp_metrics_block *tm;
330 struct inetpeer_addr saddr, daddr;
334 if (sk->sk_family == AF_INET) {
335 saddr.family = AF_INET;
336 saddr.addr.a4 = inet_sk(sk)->inet_saddr;
337 daddr.family = AF_INET;
338 daddr.addr.a4 = inet_sk(sk)->inet_daddr;
339 hash = (__force unsigned int) daddr.addr.a4;
341 #if IS_ENABLED(CONFIG_IPV6)
342 else if (sk->sk_family == AF_INET6) {
343 if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
344 saddr.family = AF_INET;
345 saddr.addr.a4 = inet_sk(sk)->inet_saddr;
346 daddr.family = AF_INET;
347 daddr.addr.a4 = inet_sk(sk)->inet_daddr;
348 hash = (__force unsigned int) daddr.addr.a4;
350 saddr.family = AF_INET6;
351 *(struct in6_addr *)saddr.addr.a6 = sk->sk_v6_rcv_saddr;
352 daddr.family = AF_INET6;
353 *(struct in6_addr *)daddr.addr.a6 = sk->sk_v6_daddr;
354 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
361 net = dev_net(dst->dev);
362 hash ^= net_hash_mix(net);
363 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
365 tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
366 if (tm == TCP_METRICS_RECLAIM_PTR)
369 tm = tcpm_new(dst, &saddr, &daddr, hash);
371 tcpm_check_stamp(tm, dst);
376 /* Save metrics learned by this TCP session. This function is called
377 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
378 * or goes from LAST-ACK to CLOSE.
380 void tcp_update_metrics(struct sock *sk)
382 const struct inet_connection_sock *icsk = inet_csk(sk);
383 struct dst_entry *dst = __sk_dst_get(sk);
384 struct tcp_sock *tp = tcp_sk(sk);
385 struct tcp_metrics_block *tm;
390 if (sysctl_tcp_nometrics_save || !dst)
393 if (dst->flags & DST_HOST)
397 if (icsk->icsk_backoff || !tp->srtt_us) {
398 /* This session failed to estimate rtt. Why?
399 * Probably, no packets returned in time. Reset our
402 tm = tcp_get_metrics(sk, dst, false);
403 if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
404 tcp_metric_set(tm, TCP_METRIC_RTT, 0);
407 tm = tcp_get_metrics(sk, dst, true);
412 rtt = tcp_metric_get(tm, TCP_METRIC_RTT);
413 m = rtt - tp->srtt_us;
415 /* If newly calculated rtt larger than stored one, store new
416 * one. Otherwise, use EWMA. Remember, rtt overestimation is
417 * always better than underestimation.
419 if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
424 tcp_metric_set(tm, TCP_METRIC_RTT, rtt);
427 if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
433 /* Scale deviation to rttvar fixed point */
438 var = tcp_metric_get(tm, TCP_METRIC_RTTVAR);
442 var -= (var - m) >> 2;
444 tcp_metric_set(tm, TCP_METRIC_RTTVAR, var);
447 if (tcp_in_initial_slowstart(tp)) {
448 /* Slow start still did not finish. */
449 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
450 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
451 if (val && (tp->snd_cwnd >> 1) > val)
452 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
455 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
456 val = tcp_metric_get(tm, TCP_METRIC_CWND);
457 if (tp->snd_cwnd > val)
458 tcp_metric_set(tm, TCP_METRIC_CWND,
461 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
462 icsk->icsk_ca_state == TCP_CA_Open) {
463 /* Cong. avoidance phase, cwnd is reliable. */
464 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
465 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
466 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
467 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
468 val = tcp_metric_get(tm, TCP_METRIC_CWND);
469 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
472 /* Else slow start did not finish, cwnd is non-sense,
473 * ssthresh may be also invalid.
475 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
476 val = tcp_metric_get(tm, TCP_METRIC_CWND);
477 tcp_metric_set(tm, TCP_METRIC_CWND,
478 (val + tp->snd_ssthresh) >> 1);
480 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
481 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
482 if (val && tp->snd_ssthresh > val)
483 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
486 if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
487 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
488 if (val < tp->reordering &&
489 tp->reordering != sysctl_tcp_reordering)
490 tcp_metric_set(tm, TCP_METRIC_REORDERING,
494 tm->tcpm_stamp = jiffies;
499 /* Initialize metrics on socket. */
501 void tcp_init_metrics(struct sock *sk)
503 struct dst_entry *dst = __sk_dst_get(sk);
504 struct tcp_sock *tp = tcp_sk(sk);
505 struct tcp_metrics_block *tm;
506 u32 val, crtt = 0; /* cached RTT scaled by 8 */
514 tm = tcp_get_metrics(sk, dst, true);
520 if (tcp_metric_locked(tm, TCP_METRIC_CWND))
521 tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
523 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
525 tp->snd_ssthresh = val;
526 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
527 tp->snd_ssthresh = tp->snd_cwnd_clamp;
529 /* ssthresh may have been reduced unnecessarily during.
530 * 3WHS. Restore it back to its initial default.
532 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
534 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
535 if (val && tp->reordering != val) {
536 tcp_disable_fack(tp);
537 tcp_disable_early_retrans(tp);
538 tp->reordering = val;
541 crtt = tcp_metric_get(tm, TCP_METRIC_RTT);
544 /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
545 * to seed the RTO for later data packets because SYN packets are
546 * small. Use the per-dst cached values to seed the RTO but keep
547 * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
548 * Later the RTO will be updated immediately upon obtaining the first
549 * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
550 * influences the first RTO but not later RTT estimation.
552 * But if RTT is not available from the SYN (due to retransmits or
553 * syn cookies) or the cache, force a conservative 3secs timeout.
555 * A bit of theory. RTT is time passed after "normal" sized packet
556 * is sent until it is ACKed. In normal circumstances sending small
557 * packets force peer to delay ACKs and calculation is correct too.
558 * The algorithm is adaptive and, provided we follow specs, it
559 * NEVER underestimate RTT. BUT! If peer tries to make some clever
560 * tricks sort of "quick acks" for time long enough to decrease RTT
561 * to low value, and then abruptly stops to do it and starts to delay
562 * ACKs, wait for troubles.
564 if (crtt > tp->srtt_us) {
565 /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
566 crtt /= 8 * USEC_PER_MSEC;
567 inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
568 } else if (tp->srtt_us == 0) {
569 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
570 * 3WHS. This is most likely due to retransmission,
571 * including spurious one. Reset the RTO back to 3secs
572 * from the more aggressive 1sec to avoid more spurious
575 tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK);
576 tp->mdev_us = tp->mdev_max_us = tp->rttvar_us;
578 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
580 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
581 * retransmitted. In light of RFC6298 more aggressive 1sec
582 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
583 * retransmission has occurred.
585 if (tp->total_retrans > 1)
588 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
589 tp->snd_cwnd_stamp = tcp_time_stamp;
592 bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst,
593 bool paws_check, bool timestamps)
595 struct tcp_metrics_block *tm;
602 tm = __tcp_get_metrics_req(req, dst);
605 (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
606 ((s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW ||
612 if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
621 EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
623 void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
625 struct tcp_metrics_block *tm;
628 tm = tcp_get_metrics(sk, dst, true);
630 struct tcp_sock *tp = tcp_sk(sk);
632 if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
633 tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
634 tp->rx_opt.ts_recent = tm->tcpm_ts;
639 EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
641 /* VJ's idea. Save last timestamp seen from this destination and hold
642 * it at least for normal timewait interval to use for duplicate
643 * segment detection in subsequent connections, before they enter
644 * synchronized state.
646 bool tcp_remember_stamp(struct sock *sk)
648 struct dst_entry *dst = __sk_dst_get(sk);
652 struct tcp_metrics_block *tm;
655 tm = tcp_get_metrics(sk, dst, true);
657 struct tcp_sock *tp = tcp_sk(sk);
659 if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
660 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
661 tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
662 tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
663 tm->tcpm_ts = tp->rx_opt.ts_recent;
672 bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
674 struct tcp_metrics_block *tm;
678 tm = __tcp_get_metrics_tw(tw);
680 const struct tcp_timewait_sock *tcptw;
681 struct sock *sk = (struct sock *) tw;
683 tcptw = tcp_twsk(sk);
684 if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
685 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
686 tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
687 tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
688 tm->tcpm_ts = tcptw->tw_ts_recent;
697 static DEFINE_SEQLOCK(fastopen_seqlock);
699 void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
700 struct tcp_fastopen_cookie *cookie,
701 int *syn_loss, unsigned long *last_syn_loss)
703 struct tcp_metrics_block *tm;
706 tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
708 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
712 seq = read_seqbegin(&fastopen_seqlock);
715 *cookie = tfom->cookie;
716 *syn_loss = tfom->syn_loss;
717 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
718 } while (read_seqretry(&fastopen_seqlock, seq));
723 void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
724 struct tcp_fastopen_cookie *cookie, bool syn_lost)
726 struct dst_entry *dst = __sk_dst_get(sk);
727 struct tcp_metrics_block *tm;
732 tm = tcp_get_metrics(sk, dst, true);
734 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
736 write_seqlock_bh(&fastopen_seqlock);
739 if (cookie && cookie->len > 0)
740 tfom->cookie = *cookie;
743 tfom->last_syn_loss = jiffies;
746 write_sequnlock_bh(&fastopen_seqlock);
751 static struct genl_family tcp_metrics_nl_family = {
752 .id = GENL_ID_GENERATE,
754 .name = TCP_METRICS_GENL_NAME,
755 .version = TCP_METRICS_GENL_VERSION,
756 .maxattr = TCP_METRICS_ATTR_MAX,
760 static struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
761 [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
762 [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
763 .len = sizeof(struct in6_addr), },
764 /* Following attributes are not received for GET/DEL,
765 * we keep them for reference
768 [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
769 [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
770 [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
771 [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
772 [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
773 [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
774 [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
775 [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
776 .len = TCP_FASTOPEN_COOKIE_MAX, },
780 /* Add attributes, caller cancels its header on failure */
781 static int tcp_metrics_fill_info(struct sk_buff *msg,
782 struct tcp_metrics_block *tm)
787 switch (tm->tcpm_daddr.family) {
789 if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4,
790 tm->tcpm_daddr.addr.a4) < 0)
791 goto nla_put_failure;
792 if (nla_put_be32(msg, TCP_METRICS_ATTR_SADDR_IPV4,
793 tm->tcpm_saddr.addr.a4) < 0)
794 goto nla_put_failure;
797 if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16,
798 tm->tcpm_daddr.addr.a6) < 0)
799 goto nla_put_failure;
800 if (nla_put(msg, TCP_METRICS_ATTR_SADDR_IPV6, 16,
801 tm->tcpm_saddr.addr.a6) < 0)
802 goto nla_put_failure;
805 return -EAFNOSUPPORT;
808 if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
809 jiffies - tm->tcpm_stamp) < 0)
810 goto nla_put_failure;
811 if (tm->tcpm_ts_stamp) {
812 if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP,
813 (s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0)
814 goto nla_put_failure;
815 if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL,
817 goto nla_put_failure;
823 nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
825 goto nla_put_failure;
826 for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
827 u32 val = tm->tcpm_vals[i];
831 if (i == TCP_METRIC_RTT) {
832 if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
834 goto nla_put_failure;
836 val = max(val / 1000, 1U);
838 if (i == TCP_METRIC_RTTVAR) {
839 if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
841 goto nla_put_failure;
843 val = max(val / 1000, 1U);
845 if (nla_put_u32(msg, i + 1, val) < 0)
846 goto nla_put_failure;
850 nla_nest_end(msg, nest);
852 nla_nest_cancel(msg, nest);
856 struct tcp_fastopen_metrics tfom_copy[1], *tfom;
860 seq = read_seqbegin(&fastopen_seqlock);
861 tfom_copy[0] = tm->tcpm_fastopen;
862 } while (read_seqretry(&fastopen_seqlock, seq));
866 nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
868 goto nla_put_failure;
869 if (tfom->syn_loss &&
870 (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
871 tfom->syn_loss) < 0 ||
872 nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
873 jiffies - tfom->last_syn_loss) < 0))
874 goto nla_put_failure;
875 if (tfom->cookie.len > 0 &&
876 nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
877 tfom->cookie.len, tfom->cookie.val) < 0)
878 goto nla_put_failure;
887 static int tcp_metrics_dump_info(struct sk_buff *skb,
888 struct netlink_callback *cb,
889 struct tcp_metrics_block *tm)
893 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
894 &tcp_metrics_nl_family, NLM_F_MULTI,
895 TCP_METRICS_CMD_GET);
899 if (tcp_metrics_fill_info(skb, tm) < 0)
900 goto nla_put_failure;
902 genlmsg_end(skb, hdr);
906 genlmsg_cancel(skb, hdr);
910 static int tcp_metrics_nl_dump(struct sk_buff *skb,
911 struct netlink_callback *cb)
913 struct net *net = sock_net(skb->sk);
914 unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
915 unsigned int row, s_row = cb->args[0];
916 int s_col = cb->args[1], col = s_col;
918 for (row = s_row; row < max_rows; row++, s_col = 0) {
919 struct tcp_metrics_block *tm;
920 struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash + row;
923 for (col = 0, tm = rcu_dereference(hb->chain); tm;
924 tm = rcu_dereference(tm->tcpm_next), col++) {
925 if (!net_eq(tm_net(tm), net))
929 if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
943 static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
944 unsigned int *hash, int optional, int v4, int v6)
950 addr->family = AF_INET;
951 addr->addr.a4 = nla_get_be32(a);
953 *hash = (__force unsigned int) addr->addr.a4;
958 if (nla_len(a) != sizeof(struct in6_addr))
960 addr->family = AF_INET6;
961 memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6));
963 *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
966 return optional ? 1 : -EAFNOSUPPORT;
969 static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
970 unsigned int *hash, int optional)
972 return __parse_nl_addr(info, addr, hash, optional,
973 TCP_METRICS_ATTR_ADDR_IPV4,
974 TCP_METRICS_ATTR_ADDR_IPV6);
977 static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
979 return __parse_nl_addr(info, addr, NULL, 0,
980 TCP_METRICS_ATTR_SADDR_IPV4,
981 TCP_METRICS_ATTR_SADDR_IPV6);
984 static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
986 struct tcp_metrics_block *tm;
987 struct inetpeer_addr saddr, daddr;
990 struct net *net = genl_info_net(info);
995 ret = parse_nl_addr(info, &daddr, &hash, 0);
999 ret = parse_nl_saddr(info, &saddr);
1003 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1007 reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
1008 info->genlhdr->cmd);
1010 goto nla_put_failure;
1012 hash ^= net_hash_mix(net);
1013 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
1016 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
1017 tm = rcu_dereference(tm->tcpm_next)) {
1018 if (addr_same(&tm->tcpm_daddr, &daddr) &&
1019 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
1020 net_eq(tm_net(tm), net)) {
1021 ret = tcp_metrics_fill_info(msg, tm);
1029 genlmsg_end(msg, reply);
1030 return genlmsg_reply(msg, info);
1040 #define deref_locked_genl(p) \
1041 rcu_dereference_protected(p, lockdep_genl_is_held() && \
1042 lockdep_is_held(&tcp_metrics_lock))
1044 #define deref_genl(p) rcu_dereference_protected(p, lockdep_genl_is_held())
1046 static void tcp_metrics_flush_all(struct net *net)
1048 unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
1049 struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash;
1050 struct tcp_metrics_block *tm;
1053 for (row = 0; row < max_rows; row++, hb++) {
1054 struct tcp_metrics_block __rcu **pp;
1055 spin_lock_bh(&tcp_metrics_lock);
1057 for (tm = deref_locked_genl(*pp); tm;
1058 tm = deref_locked_genl(*pp)) {
1059 if (net_eq(tm_net(tm), net)) {
1060 *pp = tm->tcpm_next;
1061 kfree_rcu(tm, rcu_head);
1063 pp = &tm->tcpm_next;
1066 spin_unlock_bh(&tcp_metrics_lock);
1070 static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
1072 struct tcpm_hash_bucket *hb;
1073 struct tcp_metrics_block *tm;
1074 struct tcp_metrics_block __rcu **pp;
1075 struct inetpeer_addr saddr, daddr;
1077 struct net *net = genl_info_net(info);
1079 bool src = true, found = false;
1081 ret = parse_nl_addr(info, &daddr, &hash, 1);
1085 tcp_metrics_flush_all(net);
1088 ret = parse_nl_saddr(info, &saddr);
1092 hash ^= net_hash_mix(net);
1093 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
1094 hb = net->ipv4.tcp_metrics_hash + hash;
1096 spin_lock_bh(&tcp_metrics_lock);
1097 for (tm = deref_locked_genl(*pp); tm; tm = deref_locked_genl(*pp)) {
1098 if (addr_same(&tm->tcpm_daddr, &daddr) &&
1099 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
1100 net_eq(tm_net(tm), net)) {
1101 *pp = tm->tcpm_next;
1102 kfree_rcu(tm, rcu_head);
1105 pp = &tm->tcpm_next;
1108 spin_unlock_bh(&tcp_metrics_lock);
1114 static const struct genl_ops tcp_metrics_nl_ops[] = {
1116 .cmd = TCP_METRICS_CMD_GET,
1117 .doit = tcp_metrics_nl_cmd_get,
1118 .dumpit = tcp_metrics_nl_dump,
1119 .policy = tcp_metrics_nl_policy,
1122 .cmd = TCP_METRICS_CMD_DEL,
1123 .doit = tcp_metrics_nl_cmd_del,
1124 .policy = tcp_metrics_nl_policy,
1125 .flags = GENL_ADMIN_PERM,
1129 static unsigned int tcpmhash_entries;
1130 static int __init set_tcpmhash_entries(char *str)
1137 ret = kstrtouint(str, 0, &tcpmhash_entries);
1143 __setup("tcpmhash_entries=", set_tcpmhash_entries);
1145 static int __net_init tcp_net_metrics_init(struct net *net)
1150 slots = tcpmhash_entries;
1152 if (totalram_pages >= 128 * 1024)
1158 net->ipv4.tcp_metrics_hash_log = order_base_2(slots);
1159 size = sizeof(struct tcpm_hash_bucket) << net->ipv4.tcp_metrics_hash_log;
1161 net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1162 if (!net->ipv4.tcp_metrics_hash)
1163 net->ipv4.tcp_metrics_hash = vzalloc(size);
1165 if (!net->ipv4.tcp_metrics_hash)
1171 static void __net_exit tcp_net_metrics_exit(struct net *net)
1175 for (i = 0; i < (1U << net->ipv4.tcp_metrics_hash_log) ; i++) {
1176 struct tcp_metrics_block *tm, *next;
1178 tm = rcu_dereference_protected(net->ipv4.tcp_metrics_hash[i].chain, 1);
1180 next = rcu_dereference_protected(tm->tcpm_next, 1);
1185 kvfree(net->ipv4.tcp_metrics_hash);
1188 static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
1189 .init = tcp_net_metrics_init,
1190 .exit = tcp_net_metrics_exit,
1193 void __init tcp_metrics_init(void)
1197 ret = register_pernet_subsys(&tcp_net_metrics_ops);
1199 panic("Could not allocate the tcp_metrics hash table\n");
1201 ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
1202 tcp_metrics_nl_ops);
1204 panic("Could not register tcp_metrics generic netlink\n");