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 *addr,
26 struct net *net, unsigned int hash);
28 struct tcp_fastopen_metrics {
30 u16 syn_loss:10; /* Recurring Fast Open SYN losses */
31 unsigned long last_syn_loss; /* Last Fast Open SYN loss */
32 struct tcp_fastopen_cookie cookie;
35 struct tcp_metrics_block {
36 struct tcp_metrics_block __rcu *tcpm_next;
37 struct inetpeer_addr tcpm_addr;
38 unsigned long tcpm_stamp;
42 u32 tcpm_vals[TCP_METRIC_MAX + 1];
43 struct tcp_fastopen_metrics tcpm_fastopen;
45 struct rcu_head rcu_head;
48 static bool tcp_metric_locked(struct tcp_metrics_block *tm,
49 enum tcp_metric_index idx)
51 return tm->tcpm_lock & (1 << idx);
54 static u32 tcp_metric_get(struct tcp_metrics_block *tm,
55 enum tcp_metric_index idx)
57 return tm->tcpm_vals[idx];
60 static u32 tcp_metric_get_jiffies(struct tcp_metrics_block *tm,
61 enum tcp_metric_index idx)
63 return msecs_to_jiffies(tm->tcpm_vals[idx]);
66 static void tcp_metric_set(struct tcp_metrics_block *tm,
67 enum tcp_metric_index idx,
70 tm->tcpm_vals[idx] = val;
73 static void tcp_metric_set_msecs(struct tcp_metrics_block *tm,
74 enum tcp_metric_index idx,
77 tm->tcpm_vals[idx] = jiffies_to_msecs(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, struct dst_entry *dst,
107 tm->tcpm_stamp = jiffies;
110 if (dst_metric_locked(dst, RTAX_RTT))
111 val |= 1 << TCP_METRIC_RTT;
112 if (dst_metric_locked(dst, RTAX_RTTVAR))
113 val |= 1 << TCP_METRIC_RTTVAR;
114 if (dst_metric_locked(dst, RTAX_SSTHRESH))
115 val |= 1 << TCP_METRIC_SSTHRESH;
116 if (dst_metric_locked(dst, RTAX_CWND))
117 val |= 1 << TCP_METRIC_CWND;
118 if (dst_metric_locked(dst, RTAX_REORDERING))
119 val |= 1 << TCP_METRIC_REORDERING;
122 tm->tcpm_vals[TCP_METRIC_RTT] = dst_metric_raw(dst, RTAX_RTT);
123 tm->tcpm_vals[TCP_METRIC_RTTVAR] = dst_metric_raw(dst, RTAX_RTTVAR);
124 tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
125 tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
126 tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
128 tm->tcpm_ts_stamp = 0;
129 if (fastopen_clear) {
130 tm->tcpm_fastopen.mss = 0;
131 tm->tcpm_fastopen.syn_loss = 0;
132 tm->tcpm_fastopen.cookie.len = 0;
136 #define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
138 static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
140 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
141 tcpm_suck_dst(tm, dst, false);
144 #define TCP_METRICS_RECLAIM_DEPTH 5
145 #define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
147 static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
148 struct inetpeer_addr *addr,
151 struct tcp_metrics_block *tm;
153 bool reclaim = false;
155 spin_lock_bh(&tcp_metrics_lock);
156 net = dev_net(dst->dev);
158 /* While waiting for the spin-lock the cache might have been populated
159 * with this entry and so we have to check again.
161 tm = __tcp_get_metrics(addr, net, hash);
162 if (tm == TCP_METRICS_RECLAIM_PTR) {
167 tcpm_check_stamp(tm, dst);
171 if (unlikely(reclaim)) {
172 struct tcp_metrics_block *oldest;
174 oldest = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain);
175 for (tm = rcu_dereference(oldest->tcpm_next); tm;
176 tm = rcu_dereference(tm->tcpm_next)) {
177 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
182 tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
186 tm->tcpm_addr = *addr;
188 tcpm_suck_dst(tm, dst, true);
190 if (likely(!reclaim)) {
191 tm->tcpm_next = net->ipv4.tcp_metrics_hash[hash].chain;
192 rcu_assign_pointer(net->ipv4.tcp_metrics_hash[hash].chain, tm);
196 spin_unlock_bh(&tcp_metrics_lock);
200 static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
204 if (depth > TCP_METRICS_RECLAIM_DEPTH)
205 return TCP_METRICS_RECLAIM_PTR;
209 static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *addr,
210 struct net *net, unsigned int hash)
212 struct tcp_metrics_block *tm;
215 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
216 tm = rcu_dereference(tm->tcpm_next)) {
217 if (addr_same(&tm->tcpm_addr, addr))
221 return tcp_get_encode(tm, depth);
224 static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
225 struct dst_entry *dst)
227 struct tcp_metrics_block *tm;
228 struct inetpeer_addr addr;
232 addr.family = req->rsk_ops->family;
233 switch (addr.family) {
235 addr.addr.a4 = inet_rsk(req)->rmt_addr;
236 hash = (__force unsigned int) addr.addr.a4;
239 *(struct in6_addr *)addr.addr.a6 = inet6_rsk(req)->rmt_addr;
240 hash = ipv6_addr_hash(&inet6_rsk(req)->rmt_addr);
246 net = dev_net(dst->dev);
247 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
249 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
250 tm = rcu_dereference(tm->tcpm_next)) {
251 if (addr_same(&tm->tcpm_addr, &addr))
254 tcpm_check_stamp(tm, dst);
258 static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
260 struct inet6_timewait_sock *tw6;
261 struct tcp_metrics_block *tm;
262 struct inetpeer_addr addr;
266 addr.family = tw->tw_family;
267 switch (addr.family) {
269 addr.addr.a4 = tw->tw_daddr;
270 hash = (__force unsigned int) addr.addr.a4;
273 tw6 = inet6_twsk((struct sock *)tw);
274 *(struct in6_addr *)addr.addr.a6 = tw6->tw_v6_daddr;
275 hash = ipv6_addr_hash(&tw6->tw_v6_daddr);
282 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
284 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
285 tm = rcu_dereference(tm->tcpm_next)) {
286 if (addr_same(&tm->tcpm_addr, &addr))
292 static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
293 struct dst_entry *dst,
296 struct tcp_metrics_block *tm;
297 struct inetpeer_addr addr;
301 addr.family = sk->sk_family;
302 switch (addr.family) {
304 addr.addr.a4 = inet_sk(sk)->inet_daddr;
305 hash = (__force unsigned int) addr.addr.a4;
308 *(struct in6_addr *)addr.addr.a6 = inet6_sk(sk)->daddr;
309 hash = ipv6_addr_hash(&inet6_sk(sk)->daddr);
315 net = dev_net(dst->dev);
316 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
318 tm = __tcp_get_metrics(&addr, net, hash);
319 if (tm == TCP_METRICS_RECLAIM_PTR)
322 tm = tcpm_new(dst, &addr, hash);
324 tcpm_check_stamp(tm, dst);
329 /* Save metrics learned by this TCP session. This function is called
330 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
331 * or goes from LAST-ACK to CLOSE.
333 void tcp_update_metrics(struct sock *sk)
335 const struct inet_connection_sock *icsk = inet_csk(sk);
336 struct dst_entry *dst = __sk_dst_get(sk);
337 struct tcp_sock *tp = tcp_sk(sk);
338 struct tcp_metrics_block *tm;
343 if (sysctl_tcp_nometrics_save || !dst)
346 if (dst->flags & DST_HOST)
350 if (icsk->icsk_backoff || !tp->srtt) {
351 /* This session failed to estimate rtt. Why?
352 * Probably, no packets returned in time. Reset our
355 tm = tcp_get_metrics(sk, dst, false);
356 if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
357 tcp_metric_set(tm, TCP_METRIC_RTT, 0);
360 tm = tcp_get_metrics(sk, dst, true);
365 rtt = tcp_metric_get_jiffies(tm, TCP_METRIC_RTT);
368 /* If newly calculated rtt larger than stored one, store new
369 * one. Otherwise, use EWMA. Remember, rtt overestimation is
370 * always better than underestimation.
372 if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
377 tcp_metric_set_msecs(tm, TCP_METRIC_RTT, rtt);
380 if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
386 /* Scale deviation to rttvar fixed point */
391 var = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
395 var -= (var - m) >> 2;
397 tcp_metric_set_msecs(tm, TCP_METRIC_RTTVAR, var);
400 if (tcp_in_initial_slowstart(tp)) {
401 /* Slow start still did not finish. */
402 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
403 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
404 if (val && (tp->snd_cwnd >> 1) > val)
405 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
408 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
409 val = tcp_metric_get(tm, TCP_METRIC_CWND);
410 if (tp->snd_cwnd > val)
411 tcp_metric_set(tm, TCP_METRIC_CWND,
414 } else if (tp->snd_cwnd > tp->snd_ssthresh &&
415 icsk->icsk_ca_state == TCP_CA_Open) {
416 /* Cong. avoidance phase, cwnd is reliable. */
417 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
418 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
419 max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
420 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
421 val = tcp_metric_get(tm, TCP_METRIC_CWND);
422 tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
425 /* Else slow start did not finish, cwnd is non-sense,
426 * ssthresh may be also invalid.
428 if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
429 val = tcp_metric_get(tm, TCP_METRIC_CWND);
430 tcp_metric_set(tm, TCP_METRIC_CWND,
431 (val + tp->snd_ssthresh) >> 1);
433 if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
434 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
435 if (val && tp->snd_ssthresh > val)
436 tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
439 if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
440 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
441 if (val < tp->reordering &&
442 tp->reordering != sysctl_tcp_reordering)
443 tcp_metric_set(tm, TCP_METRIC_REORDERING,
447 tm->tcpm_stamp = jiffies;
452 /* Initialize metrics on socket. */
454 void tcp_init_metrics(struct sock *sk)
456 struct dst_entry *dst = __sk_dst_get(sk);
457 struct tcp_sock *tp = tcp_sk(sk);
458 struct tcp_metrics_block *tm;
467 tm = tcp_get_metrics(sk, dst, true);
473 if (tcp_metric_locked(tm, TCP_METRIC_CWND))
474 tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
476 val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
478 tp->snd_ssthresh = val;
479 if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
480 tp->snd_ssthresh = tp->snd_cwnd_clamp;
482 /* ssthresh may have been reduced unnecessarily during.
483 * 3WHS. Restore it back to its initial default.
485 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
487 val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
488 if (val && tp->reordering != val) {
489 tcp_disable_fack(tp);
490 tcp_disable_early_retrans(tp);
491 tp->reordering = val;
494 val = tcp_metric_get(tm, TCP_METRIC_RTT);
495 if (val == 0 || tp->srtt == 0) {
499 /* Initial rtt is determined from SYN,SYN-ACK.
500 * The segment is small and rtt may appear much
501 * less than real one. Use per-dst memory
502 * to make it more realistic.
504 * A bit of theory. RTT is time passed after "normal" sized packet
505 * is sent until it is ACKed. In normal circumstances sending small
506 * packets force peer to delay ACKs and calculation is correct too.
507 * The algorithm is adaptive and, provided we follow specs, it
508 * NEVER underestimate RTT. BUT! If peer tries to make some clever
509 * tricks sort of "quick acks" for time long enough to decrease RTT
510 * to low value, and then abruptly stops to do it and starts to delay
511 * ACKs, wait for troubles.
513 val = msecs_to_jiffies(val);
514 if (val > tp->srtt) {
516 tp->rtt_seq = tp->snd_nxt;
518 val = tcp_metric_get_jiffies(tm, TCP_METRIC_RTTVAR);
519 if (val > tp->mdev) {
521 tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
528 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
529 * 3WHS. This is most likely due to retransmission,
530 * including spurious one. Reset the RTO back to 3secs
531 * from the more aggressive 1sec to avoid more spurious
534 tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_FALLBACK;
535 inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
537 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
538 * retransmitted. In light of RFC6298 more aggressive 1sec
539 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
540 * retransmission has occurred.
542 if (tp->total_retrans > 1)
545 tp->snd_cwnd = tcp_init_cwnd(tp, dst);
546 tp->snd_cwnd_stamp = tcp_time_stamp;
549 bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check)
551 struct tcp_metrics_block *tm;
558 tm = __tcp_get_metrics_req(req, dst);
561 (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
562 (s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW)
567 if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
576 EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
578 void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
580 struct tcp_metrics_block *tm;
583 tm = tcp_get_metrics(sk, dst, true);
585 struct tcp_sock *tp = tcp_sk(sk);
587 if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
588 tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
589 tp->rx_opt.ts_recent = tm->tcpm_ts;
594 EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);
596 /* VJ's idea. Save last timestamp seen from this destination and hold
597 * it at least for normal timewait interval to use for duplicate
598 * segment detection in subsequent connections, before they enter
599 * synchronized state.
601 bool tcp_remember_stamp(struct sock *sk)
603 struct dst_entry *dst = __sk_dst_get(sk);
607 struct tcp_metrics_block *tm;
610 tm = tcp_get_metrics(sk, dst, true);
612 struct tcp_sock *tp = tcp_sk(sk);
614 if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
615 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
616 tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
617 tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
618 tm->tcpm_ts = tp->rx_opt.ts_recent;
627 bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
629 struct tcp_metrics_block *tm;
633 tm = __tcp_get_metrics_tw(tw);
635 const struct tcp_timewait_sock *tcptw;
636 struct sock *sk = (struct sock *) tw;
638 tcptw = tcp_twsk(sk);
639 if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
640 ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
641 tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
642 tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
643 tm->tcpm_ts = tcptw->tw_ts_recent;
652 static DEFINE_SEQLOCK(fastopen_seqlock);
654 void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
655 struct tcp_fastopen_cookie *cookie,
656 int *syn_loss, unsigned long *last_syn_loss)
658 struct tcp_metrics_block *tm;
661 tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
663 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
667 seq = read_seqbegin(&fastopen_seqlock);
670 *cookie = tfom->cookie;
671 *syn_loss = tfom->syn_loss;
672 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
673 } while (read_seqretry(&fastopen_seqlock, seq));
678 void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
679 struct tcp_fastopen_cookie *cookie, bool syn_lost)
681 struct dst_entry *dst = __sk_dst_get(sk);
682 struct tcp_metrics_block *tm;
687 tm = tcp_get_metrics(sk, dst, true);
689 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
691 write_seqlock_bh(&fastopen_seqlock);
694 tfom->cookie = *cookie;
697 tfom->last_syn_loss = jiffies;
700 write_sequnlock_bh(&fastopen_seqlock);
705 static struct genl_family tcp_metrics_nl_family = {
706 .id = GENL_ID_GENERATE,
708 .name = TCP_METRICS_GENL_NAME,
709 .version = TCP_METRICS_GENL_VERSION,
710 .maxattr = TCP_METRICS_ATTR_MAX,
714 static struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
715 [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, },
716 [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY,
717 .len = sizeof(struct in6_addr), },
718 /* Following attributes are not received for GET/DEL,
719 * we keep them for reference
722 [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, },
723 [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, },
724 [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, },
725 [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, },
726 [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, },
727 [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, },
728 [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, },
729 [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY,
730 .len = TCP_FASTOPEN_COOKIE_MAX, },
734 /* Add attributes, caller cancels its header on failure */
735 static int tcp_metrics_fill_info(struct sk_buff *msg,
736 struct tcp_metrics_block *tm)
741 switch (tm->tcpm_addr.family) {
743 if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4,
744 tm->tcpm_addr.addr.a4) < 0)
745 goto nla_put_failure;
748 if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16,
749 tm->tcpm_addr.addr.a6) < 0)
750 goto nla_put_failure;
753 return -EAFNOSUPPORT;
756 if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
757 jiffies - tm->tcpm_stamp) < 0)
758 goto nla_put_failure;
759 if (tm->tcpm_ts_stamp) {
760 if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP,
761 (s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0)
762 goto nla_put_failure;
763 if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL,
765 goto nla_put_failure;
771 nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
773 goto nla_put_failure;
774 for (i = 0; i < TCP_METRIC_MAX + 1; i++) {
775 if (!tm->tcpm_vals[i])
777 if (nla_put_u32(msg, i + 1, tm->tcpm_vals[i]) < 0)
778 goto nla_put_failure;
782 nla_nest_end(msg, nest);
784 nla_nest_cancel(msg, nest);
788 struct tcp_fastopen_metrics tfom_copy[1], *tfom;
792 seq = read_seqbegin(&fastopen_seqlock);
793 tfom_copy[0] = tm->tcpm_fastopen;
794 } while (read_seqretry(&fastopen_seqlock, seq));
798 nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
800 goto nla_put_failure;
801 if (tfom->syn_loss &&
802 (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
803 tfom->syn_loss) < 0 ||
804 nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
805 jiffies - tfom->last_syn_loss) < 0))
806 goto nla_put_failure;
807 if (tfom->cookie.len > 0 &&
808 nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
809 tfom->cookie.len, tfom->cookie.val) < 0)
810 goto nla_put_failure;
819 static int tcp_metrics_dump_info(struct sk_buff *skb,
820 struct netlink_callback *cb,
821 struct tcp_metrics_block *tm)
825 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
826 &tcp_metrics_nl_family, NLM_F_MULTI,
827 TCP_METRICS_CMD_GET);
831 if (tcp_metrics_fill_info(skb, tm) < 0)
832 goto nla_put_failure;
834 return genlmsg_end(skb, hdr);
837 genlmsg_cancel(skb, hdr);
841 static int tcp_metrics_nl_dump(struct sk_buff *skb,
842 struct netlink_callback *cb)
844 struct net *net = sock_net(skb->sk);
845 unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
846 unsigned int row, s_row = cb->args[0];
847 int s_col = cb->args[1], col = s_col;
849 for (row = s_row; row < max_rows; row++, s_col = 0) {
850 struct tcp_metrics_block *tm;
851 struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash + row;
854 for (col = 0, tm = rcu_dereference(hb->chain); tm;
855 tm = rcu_dereference(tm->tcpm_next), col++) {
858 if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
872 static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
873 unsigned int *hash, int optional)
877 a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV4];
879 addr->family = AF_INET;
880 addr->addr.a4 = nla_get_be32(a);
881 *hash = (__force unsigned int) addr->addr.a4;
884 a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV6];
886 if (nla_len(a) != sizeof(struct in6_addr))
888 addr->family = AF_INET6;
889 memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6));
890 *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
893 return optional ? 1 : -EAFNOSUPPORT;
896 static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
898 struct tcp_metrics_block *tm;
899 struct inetpeer_addr addr;
902 struct net *net = genl_info_net(info);
906 ret = parse_nl_addr(info, &addr, &hash, 0);
910 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
914 reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
917 goto nla_put_failure;
919 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
922 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
923 tm = rcu_dereference(tm->tcpm_next)) {
924 if (addr_same(&tm->tcpm_addr, &addr)) {
925 ret = tcp_metrics_fill_info(msg, tm);
933 genlmsg_end(msg, reply);
934 return genlmsg_reply(msg, info);
944 #define deref_locked_genl(p) \
945 rcu_dereference_protected(p, lockdep_genl_is_held() && \
946 lockdep_is_held(&tcp_metrics_lock))
948 #define deref_genl(p) rcu_dereference_protected(p, lockdep_genl_is_held())
950 static int tcp_metrics_flush_all(struct net *net)
952 unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log;
953 struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash;
954 struct tcp_metrics_block *tm;
957 for (row = 0; row < max_rows; row++, hb++) {
958 spin_lock_bh(&tcp_metrics_lock);
959 tm = deref_locked_genl(hb->chain);
962 spin_unlock_bh(&tcp_metrics_lock);
964 struct tcp_metrics_block *next;
966 next = deref_genl(tm->tcpm_next);
967 kfree_rcu(tm, rcu_head);
974 static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
976 struct tcpm_hash_bucket *hb;
977 struct tcp_metrics_block *tm;
978 struct tcp_metrics_block __rcu **pp;
979 struct inetpeer_addr addr;
981 struct net *net = genl_info_net(info);
984 ret = parse_nl_addr(info, &addr, &hash, 1);
988 return tcp_metrics_flush_all(net);
990 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
991 hb = net->ipv4.tcp_metrics_hash + hash;
993 spin_lock_bh(&tcp_metrics_lock);
994 for (tm = deref_locked_genl(*pp); tm;
995 pp = &tm->tcpm_next, tm = deref_locked_genl(*pp)) {
996 if (addr_same(&tm->tcpm_addr, &addr)) {
1001 spin_unlock_bh(&tcp_metrics_lock);
1004 kfree_rcu(tm, rcu_head);
1008 static struct genl_ops tcp_metrics_nl_ops[] = {
1010 .cmd = TCP_METRICS_CMD_GET,
1011 .doit = tcp_metrics_nl_cmd_get,
1012 .dumpit = tcp_metrics_nl_dump,
1013 .policy = tcp_metrics_nl_policy,
1014 .flags = GENL_ADMIN_PERM,
1017 .cmd = TCP_METRICS_CMD_DEL,
1018 .doit = tcp_metrics_nl_cmd_del,
1019 .policy = tcp_metrics_nl_policy,
1020 .flags = GENL_ADMIN_PERM,
1024 static unsigned int tcpmhash_entries;
1025 static int __init set_tcpmhash_entries(char *str)
1032 ret = kstrtouint(str, 0, &tcpmhash_entries);
1038 __setup("tcpmhash_entries=", set_tcpmhash_entries);
1040 static int __net_init tcp_net_metrics_init(struct net *net)
1045 slots = tcpmhash_entries;
1047 if (totalram_pages >= 128 * 1024)
1053 net->ipv4.tcp_metrics_hash_log = order_base_2(slots);
1054 size = sizeof(struct tcpm_hash_bucket) << net->ipv4.tcp_metrics_hash_log;
1056 net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1057 if (!net->ipv4.tcp_metrics_hash)
1058 net->ipv4.tcp_metrics_hash = vzalloc(size);
1060 if (!net->ipv4.tcp_metrics_hash)
1066 static void __net_exit tcp_net_metrics_exit(struct net *net)
1070 for (i = 0; i < (1U << net->ipv4.tcp_metrics_hash_log) ; i++) {
1071 struct tcp_metrics_block *tm, *next;
1073 tm = rcu_dereference_protected(net->ipv4.tcp_metrics_hash[i].chain, 1);
1075 next = rcu_dereference_protected(tm->tcpm_next, 1);
1080 if (is_vmalloc_addr(net->ipv4.tcp_metrics_hash))
1081 vfree(net->ipv4.tcp_metrics_hash);
1083 kfree(net->ipv4.tcp_metrics_hash);
1086 static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
1087 .init = tcp_net_metrics_init,
1088 .exit = tcp_net_metrics_exit,
1091 void __init tcp_metrics_init(void)
1095 ret = register_pernet_subsys(&tcp_net_metrics_ops);
1098 ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
1100 ARRAY_SIZE(tcp_metrics_nl_ops));
1102 goto cleanup_subsys;
1106 unregister_pernet_subsys(&tcp_net_metrics_ops);