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 struct tcp_fastopen_metrics {
27 u16 syn_loss:10; /* Recurring Fast Open SYN losses */
28 unsigned long last_syn_loss; /* Last Fast Open SYN loss */
29 struct tcp_fastopen_cookie cookie;
32 struct tcp_metrics_block {
33 struct tcp_metrics_block __rcu *tcpm_next;
34 struct inetpeer_addr tcpm_saddr;
35 struct inetpeer_addr tcpm_daddr;
36 unsigned long tcpm_stamp;
40 u32 tcpm_vals[TCP_METRIC_MAX + 1];
41 struct tcp_fastopen_metrics tcpm_fastopen;
43 struct rcu_head rcu_head;
46 static bool tcp_metric_locked(struct tcp_metrics_block *tm,
47 enum tcp_metric_index idx)
49 return tm->tcpm_lock & (1 << idx);
52 static u32 tcp_metric_get(struct tcp_metrics_block *tm,
53 enum tcp_metric_index idx)
55 return tm->tcpm_vals[idx];
58 static u32 tcp_metric_get_jiffies(struct tcp_metrics_block *tm,
59 enum tcp_metric_index idx)
61 return msecs_to_jiffies(tm->tcpm_vals[idx]);
64 static void tcp_metric_set(struct tcp_metrics_block *tm,
65 enum tcp_metric_index idx,
68 tm->tcpm_vals[idx] = val;
71 static void tcp_metric_set_msecs(struct tcp_metrics_block *tm,
72 enum tcp_metric_index idx,
75 tm->tcpm_vals[idx] = jiffies_to_msecs(val);
78 static bool addr_same(const struct inetpeer_addr *a,
79 const struct inetpeer_addr *b)
81 const struct in6_addr *a6, *b6;
83 if (a->family != b->family)
85 if (a->family == AF_INET)
86 return a->addr.a4 == b->addr.a4;
88 a6 = (const struct in6_addr *) &a->addr.a6[0];
89 b6 = (const struct in6_addr *) &b->addr.a6[0];
91 return ipv6_addr_equal(a6, b6);
94 struct tcpm_hash_bucket {
95 struct tcp_metrics_block __rcu *chain;
98 static DEFINE_SPINLOCK(tcp_metrics_lock);
100 static void tcpm_suck_dst(struct tcp_metrics_block *tm, struct dst_entry *dst,
105 tm->tcpm_stamp = jiffies;
108 if (dst_metric_locked(dst, RTAX_RTT))
109 val |= 1 << TCP_METRIC_RTT;
110 if (dst_metric_locked(dst, RTAX_RTTVAR))
111 val |= 1 << TCP_METRIC_RTTVAR;
112 if (dst_metric_locked(dst, RTAX_SSTHRESH))
113 val |= 1 << TCP_METRIC_SSTHRESH;
114 if (dst_metric_locked(dst, RTAX_CWND))
115 val |= 1 << TCP_METRIC_CWND;
116 if (dst_metric_locked(dst, RTAX_REORDERING))
117 val |= 1 << TCP_METRIC_REORDERING;
120 tm->tcpm_vals[TCP_METRIC_RTT] = dst_metric_raw(dst, RTAX_RTT);
121 tm->tcpm_vals[TCP_METRIC_RTTVAR] = dst_metric_raw(dst, RTAX_RTTVAR);
122 tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
123 tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
124 tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
126 tm->tcpm_ts_stamp = 0;
127 if (fastopen_clear) {
128 tm->tcpm_fastopen.mss = 0;
129 tm->tcpm_fastopen.syn_loss = 0;
130 tm->tcpm_fastopen.cookie.len = 0;
134 static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
135 struct inetpeer_addr *saddr,
136 struct inetpeer_addr *daddr,
140 struct tcp_metrics_block *tm;
143 spin_lock_bh(&tcp_metrics_lock);
144 net = dev_net(dst->dev);
145 if (unlikely(reclaim)) {
146 struct tcp_metrics_block *oldest;
148 oldest = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain);
149 for (tm = rcu_dereference(oldest->tcpm_next); tm;
150 tm = rcu_dereference(tm->tcpm_next)) {
151 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
156 tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
160 tm->tcpm_saddr = *saddr;
161 tm->tcpm_daddr = *daddr;
163 tcpm_suck_dst(tm, dst, true);
165 if (likely(!reclaim)) {
166 tm->tcpm_next = net->ipv4.tcp_metrics_hash[hash].chain;
167 rcu_assign_pointer(net->ipv4.tcp_metrics_hash[hash].chain, tm);
171 spin_unlock_bh(&tcp_metrics_lock);
175 #define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
177 static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
179 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
180 tcpm_suck_dst(tm, dst, false);
183 #define TCP_METRICS_RECLAIM_DEPTH 5
184 #define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
186 static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
190 if (depth > TCP_METRICS_RECLAIM_DEPTH)
191 return TCP_METRICS_RECLAIM_PTR;
195 static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
196 const struct inetpeer_addr *daddr,
197 struct net *net, unsigned int hash)
199 struct tcp_metrics_block *tm;
202 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
203 tm = rcu_dereference(tm->tcpm_next)) {
204 if (addr_same(&tm->tcpm_saddr, saddr) &&
205 addr_same(&tm->tcpm_daddr, daddr))
209 return tcp_get_encode(tm, depth);
212 static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
213 struct dst_entry *dst)
215 struct tcp_metrics_block *tm;
216 struct inetpeer_addr saddr, daddr;
220 saddr.family = req->rsk_ops->family;
221 daddr.family = req->rsk_ops->family;
222 switch (daddr.family) {
224 saddr.addr.a4 = inet_rsk(req)->ir_loc_addr;
225 daddr.addr.a4 = inet_rsk(req)->ir_rmt_addr;
226 hash = (__force unsigned int) daddr.addr.a4;
228 #if IS_ENABLED(CONFIG_IPV6)
230 *(struct in6_addr *)saddr.addr.a6 = inet_rsk(req)->ir_v6_loc_addr;
231 *(struct in6_addr *)daddr.addr.a6 = inet_rsk(req)->ir_v6_rmt_addr;
232 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
239 net = dev_net(dst->dev);
240 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
242 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
243 tm = rcu_dereference(tm->tcpm_next)) {
244 if (addr_same(&tm->tcpm_saddr, &saddr) &&
245 addr_same(&tm->tcpm_daddr, &daddr))
248 tcpm_check_stamp(tm, dst);
252 static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
254 struct tcp_metrics_block *tm;
255 struct inetpeer_addr saddr, daddr;
259 saddr.family = tw->tw_family;
260 daddr.family = tw->tw_family;
261 switch (daddr.family) {
263 saddr.addr.a4 = tw->tw_rcv_saddr;
264 daddr.addr.a4 = tw->tw_daddr;
265 hash = (__force unsigned int) daddr.addr.a4;
267 #if IS_ENABLED(CONFIG_IPV6)
269 *(struct in6_addr *)saddr.addr.a6 = tw->tw_v6_rcv_saddr;
270 *(struct in6_addr *)daddr.addr.a6 = tw->tw_v6_daddr;
271 hash = ipv6_addr_hash(&tw->tw_v6_daddr);
279 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
281 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
282 tm = rcu_dereference(tm->tcpm_next)) {
283 if (addr_same(&tm->tcpm_saddr, &saddr) &&
284 addr_same(&tm->tcpm_daddr, &daddr))
290 static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
291 struct dst_entry *dst,
294 struct tcp_metrics_block *tm;
295 struct inetpeer_addr saddr, daddr;
300 saddr.family = sk->sk_family;
301 daddr.family = sk->sk_family;
302 switch (daddr.family) {
304 saddr.addr.a4 = inet_sk(sk)->inet_saddr;
305 daddr.addr.a4 = inet_sk(sk)->inet_daddr;
306 hash = (__force unsigned int) daddr.addr.a4;
308 #if IS_ENABLED(CONFIG_IPV6)
310 *(struct in6_addr *)saddr.addr.a6 = sk->sk_v6_rcv_saddr;
311 *(struct in6_addr *)daddr.addr.a6 = sk->sk_v6_daddr;
312 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
319 net = dev_net(dst->dev);
320 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
322 tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
324 if (tm == TCP_METRICS_RECLAIM_PTR) {
329 tm = tcpm_new(dst, &saddr, &daddr, hash, reclaim);
331 tcpm_check_stamp(tm, dst);
336 /* Save metrics learned by this TCP session. This function is called
337 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
338 * or goes from LAST-ACK to CLOSE.
340 void tcp_update_metrics(struct sock *sk)
342 const struct inet_connection_sock *icsk = inet_csk(sk);
343 struct dst_entry *dst = __sk_dst_get(sk);
344 struct tcp_sock *tp = tcp_sk(sk);
345 struct tcp_metrics_block *tm;
350 if (sysctl_tcp_nometrics_save || !dst)
353 if (dst->flags & DST_HOST)
357 if (icsk->icsk_backoff || !tp->srtt) {
358 /* This session failed to estimate rtt. Why?
359 * Probably, no packets returned in time. Reset our
362 tm = tcp_get_metrics(sk, dst, false);
363 if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
364 tcp_metric_set(tm, TCP_METRIC_RTT, 0);
367 tm = tcp_get_metrics(sk, dst, true);
372 rtt = tcp_metric_get_jiffies(tm, TCP_METRIC_RTT);
375 /* If newly calculated rtt larger than stored one, store new
376 * one. Otherwise, use EWMA. Remember, rtt overestimation is
377 * always better than underestimation.
379 if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
384 tcp_metric_set_msecs(tm, TCP_METRIC_RTT, rtt);
387 if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
393 /* Scale deviation to rttvar fixed point */
398 var = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
402 var -= (var - m) >> 2;
404 tcp_metric_set_msecs(tm, TCP_METRIC_RTTVAR, var);
407 if (tcp_in_initial_slowstart(tp)) {
408 /* Slow start still did not finish. */
409 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
410 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
411 if (val && (tp->snd_cwnd >> 1) > val)
412 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
415 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
416 val = tcp_metric_get(tm, TCP_METRIC_CWND);
417 if (tp->snd_cwnd > val)
418 tcp_metric_set(tm, TCP_METRIC_CWND,
421 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
422 icsk->icsk_ca_state == TCP_CA_Open) {
423 /* Cong. avoidance phase, cwnd is reliable. */
424 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
425 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
426 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
427 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
428 val = tcp_metric_get(tm, TCP_METRIC_CWND);
429 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
432 /* Else slow start did not finish, cwnd is non-sense,
433 * ssthresh may be also invalid.
435 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
436 val = tcp_metric_get(tm, TCP_METRIC_CWND);
437 tcp_metric_set(tm, TCP_METRIC_CWND,
438 (val + tp->snd_ssthresh) >> 1);
440 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
441 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
442 if (val && tp->snd_ssthresh > val)
443 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
446 if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
447 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
448 if (val < tp->reordering &&
449 tp->reordering != sysctl_tcp_reordering)
450 tcp_metric_set(tm, TCP_METRIC_REORDERING,
454 tm->tcpm_stamp = jiffies;
459 /* Initialize metrics on socket. */
461 void tcp_init_metrics(struct sock *sk)
463 struct dst_entry *dst = __sk_dst_get(sk);
464 struct tcp_sock *tp = tcp_sk(sk);
465 struct tcp_metrics_block *tm;
466 u32 val, crtt = 0; /* cached RTT scaled by 8 */
474 tm = tcp_get_metrics(sk, dst, true);
480 if (tcp_metric_locked(tm, TCP_METRIC_CWND))
481 tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
483 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
485 tp->snd_ssthresh = val;
486 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
487 tp->snd_ssthresh = tp->snd_cwnd_clamp;
489 /* ssthresh may have been reduced unnecessarily during.
490 * 3WHS. Restore it back to its initial default.
492 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
494 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
495 if (val && tp->reordering != val) {
496 tcp_disable_fack(tp);
497 tcp_disable_early_retrans(tp);
498 tp->reordering = val;
501 crtt = tcp_metric_get_jiffies(tm, TCP_METRIC_RTT);
504 /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
505 * to seed the RTO for later data packets because SYN packets are
506 * small. Use the per-dst cached values to seed the RTO but keep
507 * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
508 * Later the RTO will be updated immediately upon obtaining the first
509 * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
510 * influences the first RTO but not later RTT estimation.
512 * But if RTT is not available from the SYN (due to retransmits or
513 * syn cookies) or the cache, force a conservative 3secs timeout.
515 * A bit of theory. RTT is time passed after "normal" sized packet
516 * is sent until it is ACKed. In normal circumstances sending small
517 * packets force peer to delay ACKs and calculation is correct too.
518 * The algorithm is adaptive and, provided we follow specs, it
519 * NEVER underestimate RTT. BUT! If peer tries to make some clever
520 * tricks sort of "quick acks" for time long enough to decrease RTT
521 * to low value, and then abruptly stops to do it and starts to delay
522 * ACKs, wait for troubles.
524 if (crtt > tp->srtt) {
525 /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
527 inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
528 } else if (tp->srtt == 0) {
529 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
530 * 3WHS. This is most likely due to retransmission,
531 * including spurious one. Reset the RTO back to 3secs
532 * from the more aggressive 1sec to avoid more spurious
535 tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_FALLBACK;
536 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
538 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
539 * retransmitted. In light of RFC6298 more aggressive 1sec
540 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
541 * retransmission has occurred.
543 if (tp->total_retrans > 1)
546 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
547 tp->snd_cwnd_stamp = tcp_time_stamp;
550 bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check)
552 struct tcp_metrics_block *tm;
559 tm = __tcp_get_metrics_req(req, dst);
562 (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
563 (s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW)
568 if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
577 EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
579 void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
581 struct tcp_metrics_block *tm;
584 tm = tcp_get_metrics(sk, dst, true);
586 struct tcp_sock *tp = tcp_sk(sk);
588 if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
589 tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
590 tp->rx_opt.ts_recent = tm->tcpm_ts;
595 EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
597 /* VJ's idea. Save last timestamp seen from this destination and hold
598 * it at least for normal timewait interval to use for duplicate
599 * segment detection in subsequent connections, before they enter
600 * synchronized state.
602 bool tcp_remember_stamp(struct sock *sk)
604 struct dst_entry *dst = __sk_dst_get(sk);
608 struct tcp_metrics_block *tm;
611 tm = tcp_get_metrics(sk, dst, true);
613 struct tcp_sock *tp = tcp_sk(sk);
615 if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
616 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
617 tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
618 tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
619 tm->tcpm_ts = tp->rx_opt.ts_recent;
628 bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
630 struct tcp_metrics_block *tm;
634 tm = __tcp_get_metrics_tw(tw);
636 const struct tcp_timewait_sock *tcptw;
637 struct sock *sk = (struct sock *) tw;
639 tcptw = tcp_twsk(sk);
640 if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
641 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
642 tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
643 tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
644 tm->tcpm_ts = tcptw->tw_ts_recent;
653 static DEFINE_SEQLOCK(fastopen_seqlock);
655 void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
656 struct tcp_fastopen_cookie *cookie,
657 int *syn_loss, unsigned long *last_syn_loss)
659 struct tcp_metrics_block *tm;
662 tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
664 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
668 seq = read_seqbegin(&fastopen_seqlock);
671 *cookie = tfom->cookie;
672 *syn_loss = tfom->syn_loss;
673 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
674 } while (read_seqretry(&fastopen_seqlock, seq));
679 void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
680 struct tcp_fastopen_cookie *cookie, bool syn_lost)
682 struct dst_entry *dst = __sk_dst_get(sk);
683 struct tcp_metrics_block *tm;
688 tm = tcp_get_metrics(sk, dst, true);
690 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
692 write_seqlock_bh(&fastopen_seqlock);
695 if (cookie && cookie->len > 0)
696 tfom->cookie = *cookie;
699 tfom->last_syn_loss = jiffies;
702 write_sequnlock_bh(&fastopen_seqlock);
707 static struct genl_family tcp_metrics_nl_family = {
708 .id = GENL_ID_GENERATE,
710 .name = TCP_METRICS_GENL_NAME,
711 .version = TCP_METRICS_GENL_VERSION,
712 .maxattr = TCP_METRICS_ATTR_MAX,
716 static struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
717 [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
718 [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
719 .len = sizeof(struct in6_addr), },
720 /* Following attributes are not received for GET/DEL,
721 * we keep them for reference
724 [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
725 [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
726 [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
727 [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
728 [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
729 [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
730 [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
731 [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
732 .len = TCP_FASTOPEN_COOKIE_MAX, },
736 /* Add attributes, caller cancels its header on failure */
737 static int tcp_metrics_fill_info(struct sk_buff *msg,
738 struct tcp_metrics_block *tm)
743 switch (tm->tcpm_daddr.family) {
745 if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4,
746 tm->tcpm_daddr.addr.a4) < 0)
747 goto nla_put_failure;
750 if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16,
751 tm->tcpm_daddr.addr.a6) < 0)
752 goto nla_put_failure;
755 return -EAFNOSUPPORT;
758 if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
759 jiffies - tm->tcpm_stamp) < 0)
760 goto nla_put_failure;
761 if (tm->tcpm_ts_stamp) {
762 if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP,
763 (s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0)
764 goto nla_put_failure;
765 if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL,
767 goto nla_put_failure;
773 nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
775 goto nla_put_failure;
776 for (i = 0; i < TCP_METRIC_MAX + 1; i++) {
777 if (!tm->tcpm_vals[i])
779 if (nla_put_u32(msg, i + 1, tm->tcpm_vals[i]) < 0)
780 goto nla_put_failure;
784 nla_nest_end(msg, nest);
786 nla_nest_cancel(msg, nest);
790 struct tcp_fastopen_metrics tfom_copy[1], *tfom;
794 seq = read_seqbegin(&fastopen_seqlock);
795 tfom_copy[0] = tm->tcpm_fastopen;
796 } while (read_seqretry(&fastopen_seqlock, seq));
800 nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
802 goto nla_put_failure;
803 if (tfom->syn_loss &&
804 (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
805 tfom->syn_loss) < 0 ||
806 nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
807 jiffies - tfom->last_syn_loss) < 0))
808 goto nla_put_failure;
809 if (tfom->cookie.len > 0 &&
810 nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
811 tfom->cookie.len, tfom->cookie.val) < 0)
812 goto nla_put_failure;
821 static int tcp_metrics_dump_info(struct sk_buff *skb,
822 struct netlink_callback *cb,
823 struct tcp_metrics_block *tm)
827 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
828 &tcp_metrics_nl_family, NLM_F_MULTI,
829 TCP_METRICS_CMD_GET);
833 if (tcp_metrics_fill_info(skb, tm) < 0)
834 goto nla_put_failure;
836 return genlmsg_end(skb, hdr);
839 genlmsg_cancel(skb, hdr);
843 static int tcp_metrics_nl_dump(struct sk_buff *skb,
844 struct netlink_callback *cb)
846 struct net *net = sock_net(skb->sk);
847 unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
848 unsigned int row, s_row = cb->args[0];
849 int s_col = cb->args[1], col = s_col;
851 for (row = s_row; row < max_rows; row++, s_col = 0) {
852 struct tcp_metrics_block *tm;
853 struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash + row;
856 for (col = 0, tm = rcu_dereference(hb->chain); tm;
857 tm = rcu_dereference(tm->tcpm_next), col++) {
860 if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
874 static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
875 unsigned int *hash, int optional)
879 a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV4];
881 addr->family = AF_INET;
882 addr->addr.a4 = nla_get_be32(a);
883 *hash = (__force unsigned int) addr->addr.a4;
886 a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV6];
888 if (nla_len(a) != sizeof(struct in6_addr))
890 addr->family = AF_INET6;
891 memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6));
892 *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
895 return optional ? 1 : -EAFNOSUPPORT;
898 static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
900 struct tcp_metrics_block *tm;
901 struct inetpeer_addr daddr;
904 struct net *net = genl_info_net(info);
908 ret = parse_nl_addr(info, &daddr, &hash, 0);
912 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
916 reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
919 goto nla_put_failure;
921 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
924 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
925 tm = rcu_dereference(tm->tcpm_next)) {
926 if (addr_same(&tm->tcpm_daddr, &daddr)) {
927 ret = tcp_metrics_fill_info(msg, tm);
935 genlmsg_end(msg, reply);
936 return genlmsg_reply(msg, info);
946 #define deref_locked_genl(p) \
947 rcu_dereference_protected(p, lockdep_genl_is_held() && \
948 lockdep_is_held(&tcp_metrics_lock))
950 #define deref_genl(p) rcu_dereference_protected(p, lockdep_genl_is_held())
952 static int tcp_metrics_flush_all(struct net *net)
954 unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
955 struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash;
956 struct tcp_metrics_block *tm;
959 for (row = 0; row < max_rows; row++, hb++) {
960 spin_lock_bh(&tcp_metrics_lock);
961 tm = deref_locked_genl(hb->chain);
964 spin_unlock_bh(&tcp_metrics_lock);
966 struct tcp_metrics_block *next;
968 next = deref_genl(tm->tcpm_next);
969 kfree_rcu(tm, rcu_head);
976 static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
978 struct tcpm_hash_bucket *hb;
979 struct tcp_metrics_block *tm;
980 struct tcp_metrics_block __rcu **pp;
981 struct inetpeer_addr daddr;
983 struct net *net = genl_info_net(info);
986 ret = parse_nl_addr(info, &daddr, &hash, 1);
990 return tcp_metrics_flush_all(net);
992 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
993 hb = net->ipv4.tcp_metrics_hash + hash;
995 spin_lock_bh(&tcp_metrics_lock);
996 for (tm = deref_locked_genl(*pp); tm;
997 pp = &tm->tcpm_next, tm = deref_locked_genl(*pp)) {
998 if (addr_same(&tm->tcpm_daddr, &daddr)) {
1003 spin_unlock_bh(&tcp_metrics_lock);
1006 kfree_rcu(tm, rcu_head);
1010 static const struct genl_ops tcp_metrics_nl_ops[] = {
1012 .cmd = TCP_METRICS_CMD_GET,
1013 .doit = tcp_metrics_nl_cmd_get,
1014 .dumpit = tcp_metrics_nl_dump,
1015 .policy = tcp_metrics_nl_policy,
1016 .flags = GENL_ADMIN_PERM,
1019 .cmd = TCP_METRICS_CMD_DEL,
1020 .doit = tcp_metrics_nl_cmd_del,
1021 .policy = tcp_metrics_nl_policy,
1022 .flags = GENL_ADMIN_PERM,
1026 static unsigned int tcpmhash_entries;
1027 static int __init set_tcpmhash_entries(char *str)
1034 ret = kstrtouint(str, 0, &tcpmhash_entries);
1040 __setup("tcpmhash_entries=", set_tcpmhash_entries);
1042 static int __net_init tcp_net_metrics_init(struct net *net)
1047 slots = tcpmhash_entries;
1049 if (totalram_pages >= 128 * 1024)
1055 net->ipv4.tcp_metrics_hash_log = order_base_2(slots);
1056 size = sizeof(struct tcpm_hash_bucket) << net->ipv4.tcp_metrics_hash_log;
1058 net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1059 if (!net->ipv4.tcp_metrics_hash)
1060 net->ipv4.tcp_metrics_hash = vzalloc(size);
1062 if (!net->ipv4.tcp_metrics_hash)
1068 static void __net_exit tcp_net_metrics_exit(struct net *net)
1072 for (i = 0; i < (1U << net->ipv4.tcp_metrics_hash_log) ; i++) {
1073 struct tcp_metrics_block *tm, *next;
1075 tm = rcu_dereference_protected(net->ipv4.tcp_metrics_hash[i].chain, 1);
1077 next = rcu_dereference_protected(tm->tcpm_next, 1);
1082 if (is_vmalloc_addr(net->ipv4.tcp_metrics_hash))
1083 vfree(net->ipv4.tcp_metrics_hash);
1085 kfree(net->ipv4.tcp_metrics_hash);
1088 static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
1089 .init = tcp_net_metrics_init,
1090 .exit = tcp_net_metrics_exit,
1093 void __init tcp_metrics_init(void)
1097 ret = register_pernet_subsys(&tcp_net_metrics_ops);
1100 ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
1101 tcp_metrics_nl_ops);
1103 goto cleanup_subsys;
1107 unregister_pernet_subsys(&tcp_net_metrics_ops);