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_addr
;
35 unsigned long tcpm_stamp
;
39 u32 tcpm_vals
[TCP_METRIC_MAX
+ 1];
40 struct tcp_fastopen_metrics tcpm_fastopen
;
42 struct rcu_head rcu_head
;
45 static bool tcp_metric_locked(struct tcp_metrics_block
*tm
,
46 enum tcp_metric_index idx
)
48 return tm
->tcpm_lock
& (1 << idx
);
51 static u32
tcp_metric_get(struct tcp_metrics_block
*tm
,
52 enum tcp_metric_index idx
)
54 return tm
->tcpm_vals
[idx
];
57 static u32
tcp_metric_get_jiffies(struct tcp_metrics_block
*tm
,
58 enum tcp_metric_index idx
)
60 return msecs_to_jiffies(tm
->tcpm_vals
[idx
]);
63 static void tcp_metric_set(struct tcp_metrics_block
*tm
,
64 enum tcp_metric_index idx
,
67 tm
->tcpm_vals
[idx
] = val
;
70 static void tcp_metric_set_msecs(struct tcp_metrics_block
*tm
,
71 enum tcp_metric_index idx
,
74 tm
->tcpm_vals
[idx
] = jiffies_to_msecs(val
);
77 static bool addr_same(const struct inetpeer_addr
*a
,
78 const struct inetpeer_addr
*b
)
80 const struct in6_addr
*a6
, *b6
;
82 if (a
->family
!= b
->family
)
84 if (a
->family
== AF_INET
)
85 return a
->addr
.a4
== b
->addr
.a4
;
87 a6
= (const struct in6_addr
*) &a
->addr
.a6
[0];
88 b6
= (const struct in6_addr
*) &b
->addr
.a6
[0];
90 return ipv6_addr_equal(a6
, b6
);
93 struct tcpm_hash_bucket
{
94 struct tcp_metrics_block __rcu
*chain
;
97 static DEFINE_SPINLOCK(tcp_metrics_lock
);
99 static void tcpm_suck_dst(struct tcp_metrics_block
*tm
, struct dst_entry
*dst
,
104 tm
->tcpm_stamp
= jiffies
;
107 if (dst_metric_locked(dst
, RTAX_RTT
))
108 val
|= 1 << TCP_METRIC_RTT
;
109 if (dst_metric_locked(dst
, RTAX_RTTVAR
))
110 val
|= 1 << TCP_METRIC_RTTVAR
;
111 if (dst_metric_locked(dst
, RTAX_SSTHRESH
))
112 val
|= 1 << TCP_METRIC_SSTHRESH
;
113 if (dst_metric_locked(dst
, RTAX_CWND
))
114 val
|= 1 << TCP_METRIC_CWND
;
115 if (dst_metric_locked(dst
, RTAX_REORDERING
))
116 val
|= 1 << TCP_METRIC_REORDERING
;
119 tm
->tcpm_vals
[TCP_METRIC_RTT
] = dst_metric_raw(dst
, RTAX_RTT
);
120 tm
->tcpm_vals
[TCP_METRIC_RTTVAR
] = dst_metric_raw(dst
, RTAX_RTTVAR
);
121 tm
->tcpm_vals
[TCP_METRIC_SSTHRESH
] = dst_metric_raw(dst
, RTAX_SSTHRESH
);
122 tm
->tcpm_vals
[TCP_METRIC_CWND
] = dst_metric_raw(dst
, RTAX_CWND
);
123 tm
->tcpm_vals
[TCP_METRIC_REORDERING
] = dst_metric_raw(dst
, RTAX_REORDERING
);
125 tm
->tcpm_ts_stamp
= 0;
126 if (fastopen_clear
) {
127 tm
->tcpm_fastopen
.mss
= 0;
128 tm
->tcpm_fastopen
.syn_loss
= 0;
129 tm
->tcpm_fastopen
.cookie
.len
= 0;
133 static struct tcp_metrics_block
*tcpm_new(struct dst_entry
*dst
,
134 struct inetpeer_addr
*addr
,
138 struct tcp_metrics_block
*tm
;
141 spin_lock_bh(&tcp_metrics_lock
);
142 net
= dev_net(dst
->dev
);
143 if (unlikely(reclaim
)) {
144 struct tcp_metrics_block
*oldest
;
146 oldest
= rcu_dereference(net
->ipv4
.tcp_metrics_hash
[hash
].chain
);
147 for (tm
= rcu_dereference(oldest
->tcpm_next
); tm
;
148 tm
= rcu_dereference(tm
->tcpm_next
)) {
149 if (time_before(tm
->tcpm_stamp
, oldest
->tcpm_stamp
))
154 tm
= kmalloc(sizeof(*tm
), GFP_ATOMIC
);
158 tm
->tcpm_addr
= *addr
;
160 tcpm_suck_dst(tm
, dst
, true);
162 if (likely(!reclaim
)) {
163 tm
->tcpm_next
= net
->ipv4
.tcp_metrics_hash
[hash
].chain
;
164 rcu_assign_pointer(net
->ipv4
.tcp_metrics_hash
[hash
].chain
, tm
);
168 spin_unlock_bh(&tcp_metrics_lock
);
172 #define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
174 static void tcpm_check_stamp(struct tcp_metrics_block
*tm
, struct dst_entry
*dst
)
176 if (tm
&& unlikely(time_after(jiffies
, tm
->tcpm_stamp
+ TCP_METRICS_TIMEOUT
)))
177 tcpm_suck_dst(tm
, dst
, false);
180 #define TCP_METRICS_RECLAIM_DEPTH 5
181 #define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
183 static struct tcp_metrics_block
*tcp_get_encode(struct tcp_metrics_block
*tm
, int depth
)
187 if (depth
> TCP_METRICS_RECLAIM_DEPTH
)
188 return TCP_METRICS_RECLAIM_PTR
;
192 static struct tcp_metrics_block
*__tcp_get_metrics(const struct inetpeer_addr
*addr
,
193 struct net
*net
, unsigned int hash
)
195 struct tcp_metrics_block
*tm
;
198 for (tm
= rcu_dereference(net
->ipv4
.tcp_metrics_hash
[hash
].chain
); tm
;
199 tm
= rcu_dereference(tm
->tcpm_next
)) {
200 if (addr_same(&tm
->tcpm_addr
, addr
))
204 return tcp_get_encode(tm
, depth
);
207 static struct tcp_metrics_block
*__tcp_get_metrics_req(struct request_sock
*req
,
208 struct dst_entry
*dst
)
210 struct tcp_metrics_block
*tm
;
211 struct inetpeer_addr addr
;
215 addr
.family
= req
->rsk_ops
->family
;
216 switch (addr
.family
) {
218 addr
.addr
.a4
= inet_rsk(req
)->rmt_addr
;
219 hash
= (__force
unsigned int) addr
.addr
.a4
;
222 *(struct in6_addr
*)addr
.addr
.a6
= inet6_rsk(req
)->rmt_addr
;
223 hash
= ipv6_addr_hash(&inet6_rsk(req
)->rmt_addr
);
229 net
= dev_net(dst
->dev
);
230 hash
= hash_32(hash
, net
->ipv4
.tcp_metrics_hash_log
);
232 for (tm
= rcu_dereference(net
->ipv4
.tcp_metrics_hash
[hash
].chain
); tm
;
233 tm
= rcu_dereference(tm
->tcpm_next
)) {
234 if (addr_same(&tm
->tcpm_addr
, &addr
))
237 tcpm_check_stamp(tm
, dst
);
241 static struct tcp_metrics_block
*__tcp_get_metrics_tw(struct inet_timewait_sock
*tw
)
243 struct tcp_metrics_block
*tm
;
244 struct inetpeer_addr addr
;
248 addr
.family
= tw
->tw_family
;
249 switch (addr
.family
) {
251 addr
.addr
.a4
= tw
->tw_daddr
;
252 hash
= (__force
unsigned int) addr
.addr
.a4
;
255 *(struct in6_addr
*)addr
.addr
.a6
= tw
->tw_v6_daddr
;
256 hash
= ipv6_addr_hash(&tw
->tw_v6_daddr
);
263 hash
= hash_32(hash
, net
->ipv4
.tcp_metrics_hash_log
);
265 for (tm
= rcu_dereference(net
->ipv4
.tcp_metrics_hash
[hash
].chain
); tm
;
266 tm
= rcu_dereference(tm
->tcpm_next
)) {
267 if (addr_same(&tm
->tcpm_addr
, &addr
))
273 static struct tcp_metrics_block
*tcp_get_metrics(struct sock
*sk
,
274 struct dst_entry
*dst
,
277 struct tcp_metrics_block
*tm
;
278 struct inetpeer_addr addr
;
283 addr
.family
= sk
->sk_family
;
284 switch (addr
.family
) {
286 addr
.addr
.a4
= inet_sk(sk
)->inet_daddr
;
287 hash
= (__force
unsigned int) addr
.addr
.a4
;
290 *(struct in6_addr
*)addr
.addr
.a6
= sk
->sk_v6_daddr
;
291 hash
= ipv6_addr_hash(&sk
->sk_v6_daddr
);
297 net
= dev_net(dst
->dev
);
298 hash
= hash_32(hash
, net
->ipv4
.tcp_metrics_hash_log
);
300 tm
= __tcp_get_metrics(&addr
, net
, hash
);
302 if (tm
== TCP_METRICS_RECLAIM_PTR
) {
307 tm
= tcpm_new(dst
, &addr
, hash
, reclaim
);
309 tcpm_check_stamp(tm
, dst
);
314 /* Save metrics learned by this TCP session. This function is called
315 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
316 * or goes from LAST-ACK to CLOSE.
318 void tcp_update_metrics(struct sock
*sk
)
320 const struct inet_connection_sock
*icsk
= inet_csk(sk
);
321 struct dst_entry
*dst
= __sk_dst_get(sk
);
322 struct tcp_sock
*tp
= tcp_sk(sk
);
323 struct tcp_metrics_block
*tm
;
328 if (sysctl_tcp_nometrics_save
|| !dst
)
331 if (dst
->flags
& DST_HOST
)
335 if (icsk
->icsk_backoff
|| !tp
->srtt
) {
336 /* This session failed to estimate rtt. Why?
337 * Probably, no packets returned in time. Reset our
340 tm
= tcp_get_metrics(sk
, dst
, false);
341 if (tm
&& !tcp_metric_locked(tm
, TCP_METRIC_RTT
))
342 tcp_metric_set(tm
, TCP_METRIC_RTT
, 0);
345 tm
= tcp_get_metrics(sk
, dst
, true);
350 rtt
= tcp_metric_get_jiffies(tm
, TCP_METRIC_RTT
);
353 /* If newly calculated rtt larger than stored one, store new
354 * one. Otherwise, use EWMA. Remember, rtt overestimation is
355 * always better than underestimation.
357 if (!tcp_metric_locked(tm
, TCP_METRIC_RTT
)) {
362 tcp_metric_set_msecs(tm
, TCP_METRIC_RTT
, rtt
);
365 if (!tcp_metric_locked(tm
, TCP_METRIC_RTTVAR
)) {
371 /* Scale deviation to rttvar fixed point */
376 var
= tcp_metric_get_jiffies(tm
, TCP_METRIC_RTTVAR
);
380 var
-= (var
- m
) >> 2;
382 tcp_metric_set_msecs(tm
, TCP_METRIC_RTTVAR
, var
);
385 if (tcp_in_initial_slowstart(tp
)) {
386 /* Slow start still did not finish. */
387 if (!tcp_metric_locked(tm
, TCP_METRIC_SSTHRESH
)) {
388 val
= tcp_metric_get(tm
, TCP_METRIC_SSTHRESH
);
389 if (val
&& (tp
->snd_cwnd
>> 1) > val
)
390 tcp_metric_set(tm
, TCP_METRIC_SSTHRESH
,
393 if (!tcp_metric_locked(tm
, TCP_METRIC_CWND
)) {
394 val
= tcp_metric_get(tm
, TCP_METRIC_CWND
);
395 if (tp
->snd_cwnd
> val
)
396 tcp_metric_set(tm
, TCP_METRIC_CWND
,
399 } else if (tp
->snd_cwnd
> tp
->snd_ssthresh
&&
400 icsk
->icsk_ca_state
== TCP_CA_Open
) {
401 /* Cong. avoidance phase, cwnd is reliable. */
402 if (!tcp_metric_locked(tm
, TCP_METRIC_SSTHRESH
))
403 tcp_metric_set(tm
, TCP_METRIC_SSTHRESH
,
404 max(tp
->snd_cwnd
>> 1, tp
->snd_ssthresh
));
405 if (!tcp_metric_locked(tm
, TCP_METRIC_CWND
)) {
406 val
= tcp_metric_get(tm
, TCP_METRIC_CWND
);
407 tcp_metric_set(tm
, TCP_METRIC_CWND
, (val
+ tp
->snd_cwnd
) >> 1);
410 /* Else slow start did not finish, cwnd is non-sense,
411 * ssthresh may be also invalid.
413 if (!tcp_metric_locked(tm
, TCP_METRIC_CWND
)) {
414 val
= tcp_metric_get(tm
, TCP_METRIC_CWND
);
415 tcp_metric_set(tm
, TCP_METRIC_CWND
,
416 (val
+ tp
->snd_ssthresh
) >> 1);
418 if (!tcp_metric_locked(tm
, TCP_METRIC_SSTHRESH
)) {
419 val
= tcp_metric_get(tm
, TCP_METRIC_SSTHRESH
);
420 if (val
&& tp
->snd_ssthresh
> val
)
421 tcp_metric_set(tm
, TCP_METRIC_SSTHRESH
,
424 if (!tcp_metric_locked(tm
, TCP_METRIC_REORDERING
)) {
425 val
= tcp_metric_get(tm
, TCP_METRIC_REORDERING
);
426 if (val
< tp
->reordering
&&
427 tp
->reordering
!= sysctl_tcp_reordering
)
428 tcp_metric_set(tm
, TCP_METRIC_REORDERING
,
432 tm
->tcpm_stamp
= jiffies
;
437 /* Initialize metrics on socket. */
439 void tcp_init_metrics(struct sock
*sk
)
441 struct dst_entry
*dst
= __sk_dst_get(sk
);
442 struct tcp_sock
*tp
= tcp_sk(sk
);
443 struct tcp_metrics_block
*tm
;
444 u32 val
, crtt
= 0; /* cached RTT scaled by 8 */
452 tm
= tcp_get_metrics(sk
, dst
, true);
458 if (tcp_metric_locked(tm
, TCP_METRIC_CWND
))
459 tp
->snd_cwnd_clamp
= tcp_metric_get(tm
, TCP_METRIC_CWND
);
461 val
= tcp_metric_get(tm
, TCP_METRIC_SSTHRESH
);
463 tp
->snd_ssthresh
= val
;
464 if (tp
->snd_ssthresh
> tp
->snd_cwnd_clamp
)
465 tp
->snd_ssthresh
= tp
->snd_cwnd_clamp
;
467 /* ssthresh may have been reduced unnecessarily during.
468 * 3WHS. Restore it back to its initial default.
470 tp
->snd_ssthresh
= TCP_INFINITE_SSTHRESH
;
472 val
= tcp_metric_get(tm
, TCP_METRIC_REORDERING
);
473 if (val
&& tp
->reordering
!= val
) {
474 tcp_disable_fack(tp
);
475 tcp_disable_early_retrans(tp
);
476 tp
->reordering
= val
;
479 crtt
= tcp_metric_get_jiffies(tm
, TCP_METRIC_RTT
);
482 /* The initial RTT measurement from the SYN/SYN-ACK is not ideal
483 * to seed the RTO for later data packets because SYN packets are
484 * small. Use the per-dst cached values to seed the RTO but keep
485 * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
486 * Later the RTO will be updated immediately upon obtaining the first
487 * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
488 * influences the first RTO but not later RTT estimation.
490 * But if RTT is not available from the SYN (due to retransmits or
491 * syn cookies) or the cache, force a conservative 3secs timeout.
493 * A bit of theory. RTT is time passed after "normal" sized packet
494 * is sent until it is ACKed. In normal circumstances sending small
495 * packets force peer to delay ACKs and calculation is correct too.
496 * The algorithm is adaptive and, provided we follow specs, it
497 * NEVER underestimate RTT. BUT! If peer tries to make some clever
498 * tricks sort of "quick acks" for time long enough to decrease RTT
499 * to low value, and then abruptly stops to do it and starts to delay
500 * ACKs, wait for troubles.
502 if (crtt
> tp
->srtt
) {
503 /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
505 inet_csk(sk
)->icsk_rto
= crtt
+ max(2 * crtt
, tcp_rto_min(sk
));
506 } else if (tp
->srtt
== 0) {
507 /* RFC6298: 5.7 We've failed to get a valid RTT sample from
508 * 3WHS. This is most likely due to retransmission,
509 * including spurious one. Reset the RTO back to 3secs
510 * from the more aggressive 1sec to avoid more spurious
513 tp
->mdev
= tp
->mdev_max
= tp
->rttvar
= TCP_TIMEOUT_FALLBACK
;
514 inet_csk(sk
)->icsk_rto
= TCP_TIMEOUT_FALLBACK
;
516 /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
517 * retransmitted. In light of RFC6298 more aggressive 1sec
518 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
519 * retransmission has occurred.
521 if (tp
->total_retrans
> 1)
524 tp
->snd_cwnd
= tcp_init_cwnd(tp
, dst
);
525 tp
->snd_cwnd_stamp
= tcp_time_stamp
;
528 bool tcp_peer_is_proven(struct request_sock
*req
, struct dst_entry
*dst
, bool paws_check
)
530 struct tcp_metrics_block
*tm
;
537 tm
= __tcp_get_metrics_req(req
, dst
);
540 (u32
)get_seconds() - tm
->tcpm_ts_stamp
< TCP_PAWS_MSL
&&
541 (s32
)(tm
->tcpm_ts
- req
->ts_recent
) > TCP_PAWS_WINDOW
)
546 if (tm
&& tcp_metric_get(tm
, TCP_METRIC_RTT
) && tm
->tcpm_ts_stamp
)
555 EXPORT_SYMBOL_GPL(tcp_peer_is_proven
);
557 void tcp_fetch_timewait_stamp(struct sock
*sk
, struct dst_entry
*dst
)
559 struct tcp_metrics_block
*tm
;
562 tm
= tcp_get_metrics(sk
, dst
, true);
564 struct tcp_sock
*tp
= tcp_sk(sk
);
566 if ((u32
)get_seconds() - tm
->tcpm_ts_stamp
<= TCP_PAWS_MSL
) {
567 tp
->rx_opt
.ts_recent_stamp
= tm
->tcpm_ts_stamp
;
568 tp
->rx_opt
.ts_recent
= tm
->tcpm_ts
;
573 EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp
);
575 /* VJ's idea. Save last timestamp seen from this destination and hold
576 * it at least for normal timewait interval to use for duplicate
577 * segment detection in subsequent connections, before they enter
578 * synchronized state.
580 bool tcp_remember_stamp(struct sock
*sk
)
582 struct dst_entry
*dst
= __sk_dst_get(sk
);
586 struct tcp_metrics_block
*tm
;
589 tm
= tcp_get_metrics(sk
, dst
, true);
591 struct tcp_sock
*tp
= tcp_sk(sk
);
593 if ((s32
)(tm
->tcpm_ts
- tp
->rx_opt
.ts_recent
) <= 0 ||
594 ((u32
)get_seconds() - tm
->tcpm_ts_stamp
> TCP_PAWS_MSL
&&
595 tm
->tcpm_ts_stamp
<= (u32
)tp
->rx_opt
.ts_recent_stamp
)) {
596 tm
->tcpm_ts_stamp
= (u32
)tp
->rx_opt
.ts_recent_stamp
;
597 tm
->tcpm_ts
= tp
->rx_opt
.ts_recent
;
606 bool tcp_tw_remember_stamp(struct inet_timewait_sock
*tw
)
608 struct tcp_metrics_block
*tm
;
612 tm
= __tcp_get_metrics_tw(tw
);
614 const struct tcp_timewait_sock
*tcptw
;
615 struct sock
*sk
= (struct sock
*) tw
;
617 tcptw
= tcp_twsk(sk
);
618 if ((s32
)(tm
->tcpm_ts
- tcptw
->tw_ts_recent
) <= 0 ||
619 ((u32
)get_seconds() - tm
->tcpm_ts_stamp
> TCP_PAWS_MSL
&&
620 tm
->tcpm_ts_stamp
<= (u32
)tcptw
->tw_ts_recent_stamp
)) {
621 tm
->tcpm_ts_stamp
= (u32
)tcptw
->tw_ts_recent_stamp
;
622 tm
->tcpm_ts
= tcptw
->tw_ts_recent
;
631 static DEFINE_SEQLOCK(fastopen_seqlock
);
633 void tcp_fastopen_cache_get(struct sock
*sk
, u16
*mss
,
634 struct tcp_fastopen_cookie
*cookie
,
635 int *syn_loss
, unsigned long *last_syn_loss
)
637 struct tcp_metrics_block
*tm
;
640 tm
= tcp_get_metrics(sk
, __sk_dst_get(sk
), false);
642 struct tcp_fastopen_metrics
*tfom
= &tm
->tcpm_fastopen
;
646 seq
= read_seqbegin(&fastopen_seqlock
);
649 *cookie
= tfom
->cookie
;
650 *syn_loss
= tfom
->syn_loss
;
651 *last_syn_loss
= *syn_loss
? tfom
->last_syn_loss
: 0;
652 } while (read_seqretry(&fastopen_seqlock
, seq
));
657 void tcp_fastopen_cache_set(struct sock
*sk
, u16 mss
,
658 struct tcp_fastopen_cookie
*cookie
, bool syn_lost
)
660 struct tcp_metrics_block
*tm
;
663 tm
= tcp_get_metrics(sk
, __sk_dst_get(sk
), true);
665 struct tcp_fastopen_metrics
*tfom
= &tm
->tcpm_fastopen
;
667 write_seqlock_bh(&fastopen_seqlock
);
670 tfom
->cookie
= *cookie
;
673 tfom
->last_syn_loss
= jiffies
;
676 write_sequnlock_bh(&fastopen_seqlock
);
681 static struct genl_family tcp_metrics_nl_family
= {
682 .id
= GENL_ID_GENERATE
,
684 .name
= TCP_METRICS_GENL_NAME
,
685 .version
= TCP_METRICS_GENL_VERSION
,
686 .maxattr
= TCP_METRICS_ATTR_MAX
,
690 static struct nla_policy tcp_metrics_nl_policy
[TCP_METRICS_ATTR_MAX
+ 1] = {
691 [TCP_METRICS_ATTR_ADDR_IPV4
] = { .type
= NLA_U32
, },
692 [TCP_METRICS_ATTR_ADDR_IPV6
] = { .type
= NLA_BINARY
,
693 .len
= sizeof(struct in6_addr
), },
694 /* Following attributes are not received for GET/DEL,
695 * we keep them for reference
698 [TCP_METRICS_ATTR_AGE
] = { .type
= NLA_MSECS
, },
699 [TCP_METRICS_ATTR_TW_TSVAL
] = { .type
= NLA_U32
, },
700 [TCP_METRICS_ATTR_TW_TS_STAMP
] = { .type
= NLA_S32
, },
701 [TCP_METRICS_ATTR_VALS
] = { .type
= NLA_NESTED
, },
702 [TCP_METRICS_ATTR_FOPEN_MSS
] = { .type
= NLA_U16
, },
703 [TCP_METRICS_ATTR_FOPEN_SYN_DROPS
] = { .type
= NLA_U16
, },
704 [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS
] = { .type
= NLA_MSECS
, },
705 [TCP_METRICS_ATTR_FOPEN_COOKIE
] = { .type
= NLA_BINARY
,
706 .len
= TCP_FASTOPEN_COOKIE_MAX
, },
710 /* Add attributes, caller cancels its header on failure */
711 static int tcp_metrics_fill_info(struct sk_buff
*msg
,
712 struct tcp_metrics_block
*tm
)
717 switch (tm
->tcpm_addr
.family
) {
719 if (nla_put_be32(msg
, TCP_METRICS_ATTR_ADDR_IPV4
,
720 tm
->tcpm_addr
.addr
.a4
) < 0)
721 goto nla_put_failure
;
724 if (nla_put(msg
, TCP_METRICS_ATTR_ADDR_IPV6
, 16,
725 tm
->tcpm_addr
.addr
.a6
) < 0)
726 goto nla_put_failure
;
729 return -EAFNOSUPPORT
;
732 if (nla_put_msecs(msg
, TCP_METRICS_ATTR_AGE
,
733 jiffies
- tm
->tcpm_stamp
) < 0)
734 goto nla_put_failure
;
735 if (tm
->tcpm_ts_stamp
) {
736 if (nla_put_s32(msg
, TCP_METRICS_ATTR_TW_TS_STAMP
,
737 (s32
) (get_seconds() - tm
->tcpm_ts_stamp
)) < 0)
738 goto nla_put_failure
;
739 if (nla_put_u32(msg
, TCP_METRICS_ATTR_TW_TSVAL
,
741 goto nla_put_failure
;
747 nest
= nla_nest_start(msg
, TCP_METRICS_ATTR_VALS
);
749 goto nla_put_failure
;
750 for (i
= 0; i
< TCP_METRIC_MAX
+ 1; i
++) {
751 if (!tm
->tcpm_vals
[i
])
753 if (nla_put_u32(msg
, i
+ 1, tm
->tcpm_vals
[i
]) < 0)
754 goto nla_put_failure
;
758 nla_nest_end(msg
, nest
);
760 nla_nest_cancel(msg
, nest
);
764 struct tcp_fastopen_metrics tfom_copy
[1], *tfom
;
768 seq
= read_seqbegin(&fastopen_seqlock
);
769 tfom_copy
[0] = tm
->tcpm_fastopen
;
770 } while (read_seqretry(&fastopen_seqlock
, seq
));
774 nla_put_u16(msg
, TCP_METRICS_ATTR_FOPEN_MSS
,
776 goto nla_put_failure
;
777 if (tfom
->syn_loss
&&
778 (nla_put_u16(msg
, TCP_METRICS_ATTR_FOPEN_SYN_DROPS
,
779 tfom
->syn_loss
) < 0 ||
780 nla_put_msecs(msg
, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS
,
781 jiffies
- tfom
->last_syn_loss
) < 0))
782 goto nla_put_failure
;
783 if (tfom
->cookie
.len
> 0 &&
784 nla_put(msg
, TCP_METRICS_ATTR_FOPEN_COOKIE
,
785 tfom
->cookie
.len
, tfom
->cookie
.val
) < 0)
786 goto nla_put_failure
;
795 static int tcp_metrics_dump_info(struct sk_buff
*skb
,
796 struct netlink_callback
*cb
,
797 struct tcp_metrics_block
*tm
)
801 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
802 &tcp_metrics_nl_family
, NLM_F_MULTI
,
803 TCP_METRICS_CMD_GET
);
807 if (tcp_metrics_fill_info(skb
, tm
) < 0)
808 goto nla_put_failure
;
810 return genlmsg_end(skb
, hdr
);
813 genlmsg_cancel(skb
, hdr
);
817 static int tcp_metrics_nl_dump(struct sk_buff
*skb
,
818 struct netlink_callback
*cb
)
820 struct net
*net
= sock_net(skb
->sk
);
821 unsigned int max_rows
= 1U << net
->ipv4
.tcp_metrics_hash_log
;
822 unsigned int row
, s_row
= cb
->args
[0];
823 int s_col
= cb
->args
[1], col
= s_col
;
825 for (row
= s_row
; row
< max_rows
; row
++, s_col
= 0) {
826 struct tcp_metrics_block
*tm
;
827 struct tcpm_hash_bucket
*hb
= net
->ipv4
.tcp_metrics_hash
+ row
;
830 for (col
= 0, tm
= rcu_dereference(hb
->chain
); tm
;
831 tm
= rcu_dereference(tm
->tcpm_next
), col
++) {
834 if (tcp_metrics_dump_info(skb
, cb
, tm
) < 0) {
848 static int parse_nl_addr(struct genl_info
*info
, struct inetpeer_addr
*addr
,
849 unsigned int *hash
, int optional
)
853 a
= info
->attrs
[TCP_METRICS_ATTR_ADDR_IPV4
];
855 addr
->family
= AF_INET
;
856 addr
->addr
.a4
= nla_get_be32(a
);
857 *hash
= (__force
unsigned int) addr
->addr
.a4
;
860 a
= info
->attrs
[TCP_METRICS_ATTR_ADDR_IPV6
];
862 if (nla_len(a
) != sizeof(struct in6_addr
))
864 addr
->family
= AF_INET6
;
865 memcpy(addr
->addr
.a6
, nla_data(a
), sizeof(addr
->addr
.a6
));
866 *hash
= ipv6_addr_hash((struct in6_addr
*) addr
->addr
.a6
);
869 return optional
? 1 : -EAFNOSUPPORT
;
872 static int tcp_metrics_nl_cmd_get(struct sk_buff
*skb
, struct genl_info
*info
)
874 struct tcp_metrics_block
*tm
;
875 struct inetpeer_addr addr
;
878 struct net
*net
= genl_info_net(info
);
882 ret
= parse_nl_addr(info
, &addr
, &hash
, 0);
886 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
890 reply
= genlmsg_put_reply(msg
, info
, &tcp_metrics_nl_family
, 0,
893 goto nla_put_failure
;
895 hash
= hash_32(hash
, net
->ipv4
.tcp_metrics_hash_log
);
898 for (tm
= rcu_dereference(net
->ipv4
.tcp_metrics_hash
[hash
].chain
); tm
;
899 tm
= rcu_dereference(tm
->tcpm_next
)) {
900 if (addr_same(&tm
->tcpm_addr
, &addr
)) {
901 ret
= tcp_metrics_fill_info(msg
, tm
);
909 genlmsg_end(msg
, reply
);
910 return genlmsg_reply(msg
, info
);
920 #define deref_locked_genl(p) \
921 rcu_dereference_protected(p, lockdep_genl_is_held() && \
922 lockdep_is_held(&tcp_metrics_lock))
924 #define deref_genl(p) rcu_dereference_protected(p, lockdep_genl_is_held())
926 static int tcp_metrics_flush_all(struct net
*net
)
928 unsigned int max_rows
= 1U << net
->ipv4
.tcp_metrics_hash_log
;
929 struct tcpm_hash_bucket
*hb
= net
->ipv4
.tcp_metrics_hash
;
930 struct tcp_metrics_block
*tm
;
933 for (row
= 0; row
< max_rows
; row
++, hb
++) {
934 spin_lock_bh(&tcp_metrics_lock
);
935 tm
= deref_locked_genl(hb
->chain
);
938 spin_unlock_bh(&tcp_metrics_lock
);
940 struct tcp_metrics_block
*next
;
942 next
= deref_genl(tm
->tcpm_next
);
943 kfree_rcu(tm
, rcu_head
);
950 static int tcp_metrics_nl_cmd_del(struct sk_buff
*skb
, struct genl_info
*info
)
952 struct tcpm_hash_bucket
*hb
;
953 struct tcp_metrics_block
*tm
;
954 struct tcp_metrics_block __rcu
**pp
;
955 struct inetpeer_addr addr
;
957 struct net
*net
= genl_info_net(info
);
960 ret
= parse_nl_addr(info
, &addr
, &hash
, 1);
964 return tcp_metrics_flush_all(net
);
966 hash
= hash_32(hash
, net
->ipv4
.tcp_metrics_hash_log
);
967 hb
= net
->ipv4
.tcp_metrics_hash
+ hash
;
969 spin_lock_bh(&tcp_metrics_lock
);
970 for (tm
= deref_locked_genl(*pp
); tm
;
971 pp
= &tm
->tcpm_next
, tm
= deref_locked_genl(*pp
)) {
972 if (addr_same(&tm
->tcpm_addr
, &addr
)) {
977 spin_unlock_bh(&tcp_metrics_lock
);
980 kfree_rcu(tm
, rcu_head
);
984 static struct genl_ops tcp_metrics_nl_ops
[] = {
986 .cmd
= TCP_METRICS_CMD_GET
,
987 .doit
= tcp_metrics_nl_cmd_get
,
988 .dumpit
= tcp_metrics_nl_dump
,
989 .policy
= tcp_metrics_nl_policy
,
990 .flags
= GENL_ADMIN_PERM
,
993 .cmd
= TCP_METRICS_CMD_DEL
,
994 .doit
= tcp_metrics_nl_cmd_del
,
995 .policy
= tcp_metrics_nl_policy
,
996 .flags
= GENL_ADMIN_PERM
,
1000 static unsigned int tcpmhash_entries
;
1001 static int __init
set_tcpmhash_entries(char *str
)
1008 ret
= kstrtouint(str
, 0, &tcpmhash_entries
);
1014 __setup("tcpmhash_entries=", set_tcpmhash_entries
);
1016 static int __net_init
tcp_net_metrics_init(struct net
*net
)
1021 slots
= tcpmhash_entries
;
1023 if (totalram_pages
>= 128 * 1024)
1029 net
->ipv4
.tcp_metrics_hash_log
= order_base_2(slots
);
1030 size
= sizeof(struct tcpm_hash_bucket
) << net
->ipv4
.tcp_metrics_hash_log
;
1032 net
->ipv4
.tcp_metrics_hash
= kzalloc(size
, GFP_KERNEL
| __GFP_NOWARN
);
1033 if (!net
->ipv4
.tcp_metrics_hash
)
1034 net
->ipv4
.tcp_metrics_hash
= vzalloc(size
);
1036 if (!net
->ipv4
.tcp_metrics_hash
)
1042 static void __net_exit
tcp_net_metrics_exit(struct net
*net
)
1046 for (i
= 0; i
< (1U << net
->ipv4
.tcp_metrics_hash_log
) ; i
++) {
1047 struct tcp_metrics_block
*tm
, *next
;
1049 tm
= rcu_dereference_protected(net
->ipv4
.tcp_metrics_hash
[i
].chain
, 1);
1051 next
= rcu_dereference_protected(tm
->tcpm_next
, 1);
1056 if (is_vmalloc_addr(net
->ipv4
.tcp_metrics_hash
))
1057 vfree(net
->ipv4
.tcp_metrics_hash
);
1059 kfree(net
->ipv4
.tcp_metrics_hash
);
1062 static __net_initdata
struct pernet_operations tcp_net_metrics_ops
= {
1063 .init
= tcp_net_metrics_init
,
1064 .exit
= tcp_net_metrics_exit
,
1067 void __init
tcp_metrics_init(void)
1071 ret
= register_pernet_subsys(&tcp_net_metrics_ops
);
1074 ret
= genl_register_family_with_ops(&tcp_metrics_nl_family
,
1076 ARRAY_SIZE(tcp_metrics_nl_ops
));
1078 goto cleanup_subsys
;
1082 unregister_pernet_subsys(&tcp_net_metrics_ops
);