tcp: add tcpi_segs_in and tcpi_segs_out to tcp_info
authorMarcelo Ricardo Leitner <mleitner@redhat.com>
Wed, 20 May 2015 23:35:41 +0000 (16:35 -0700)
committerDanny Wood <danwood76@gmail.com>
Thu, 15 Jul 2021 09:45:36 +0000 (10:45 +0100)
This patch tracks the total number of inbound and outbound segments on a
TCP socket. One may use this number to have an idea on connection
quality when compared against the retransmissions.

RFC4898 named these : tcpEStatsPerfSegsIn and tcpEStatsPerfSegsOut

These are a 32bit field each and can be fetched both from TCP_INFO
getsockopt() if one has a handle on a TCP socket, or from inet_diag
netlink facility (iproute2/ss patch will follow)

Note that tp->segs_out was placed near tp->snd_nxt for good data
locality and minimal performance impact, while tp->segs_in was placed
near tp->bytes_received for the same reason.

Join work with Eric Dumazet.

Note that received SYN are accounted on the listener, but sent SYNACK
are not accounted.

Signed-off-by: Marcelo Ricardo Leitner <mleitner@redhat.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Change-Id: I7bf746ad3f9fcc01f672f7dc110aa0c43d9ee042

include/linux/tcp.h
include/uapi/linux/tcp.h
net/ipv4/tcp.c
net/ipv4/tcp_ipv4.c
net/ipv4/tcp_minisocks.c
net/ipv4/tcp_output.c
net/ipv6/tcp_ipv6.c

index 9e43ae2a972b38fab2a2fb3dbb001b96c876b6fe..32edfbe4b7e13ea0cbd8c0adb1243c6ca5aa3a15 100644 (file)
@@ -152,11 +152,16 @@ struct tcp_sock {
                                 * sum(delta(rcv_nxt)), or how many bytes
                                 * were acked.
                                 */
+       u32     segs_in;        /* RFC4898 tcpEStatsPerfSegsIn
+                                * total number of segments in.
+                                */
        u32     rcv_nxt;        /* What we want to receive next         */
        u32     copied_seq;     /* Head of yet unread data              */
        u32     rcv_wup;        /* rcv_nxt on last window update sent   */
        u32     snd_nxt;        /* Next sequence we send                */
-
+       u32     segs_out;       /* RFC4898 tcpEStatsPerfSegsOut
+                                * The total number of segments sent.
+                                */
        u64     bytes_acked;    /* RFC4898 tcpEStatsAppHCThruOctetsAcked
                                 * sum(delta(snd_una)), or how many bytes
                                 * were acked.
index 2028627fd75ce44ff5efe5e2bf9d7ea95738ce08..8399689208ca9dd26c17ce6e78bad5e855f6492f 100644 (file)
@@ -188,8 +188,10 @@ struct tcp_info {
 
        __u64   tcpi_pacing_rate;
        __u64   tcpi_max_pacing_rate;
-       __u64   tcpi_bytes_acked; /* RFC4898 tcpEStatsAppHCThruOctetsAcked */
+       __u64   tcpi_bytes_acked;    /* RFC4898 tcpEStatsAppHCThruOctetsAcked */
        __u64   tcpi_bytes_received; /* RFC4898 tcpEStatsAppHCThruOctetsReceived */
+       __u32   tcpi_segs_out;       /* RFC4898 tcpEStatsPerfSegsOut */
+       __u32   tcpi_segs_in;        /* RFC4898 tcpEStatsPerfSegsIn */
 };
 
 /* for TCP_MD5SIG socket option */
index 42e4a00f4328b6d00473ab8bc9055f09430c7f4b..219d4dfe8d8c263e183cf0b013f389db02c91c4e 100644 (file)
@@ -2779,6 +2779,8 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)
        spin_lock_bh(&sk->sk_lock.slock);
        info->tcpi_bytes_acked = tp->bytes_acked;
        info->tcpi_bytes_received = tp->bytes_received;
+       info->tcpi_segs_out = tp->segs_out;
+       info->tcpi_segs_in = tp->segs_in;
        spin_unlock_bh(&sk->sk_lock.slock);
 }
 EXPORT_SYMBOL_GPL(tcp_get_info);
index 64800d40a358976daaae6bb3fb15c8269c0399c6..899780236bfd26b957046204a349ba6665e37bcc 100644 (file)
@@ -2054,6 +2054,7 @@ process:
        skb->dev = NULL;
 
        bh_lock_sock_nested(sk);
+       tcp_sk(sk)->segs_in += max_t(u16, 1, skb_shinfo(skb)->gso_segs);
        ret = 0;
        if (!sock_owned_by_user(sk)) {
 #ifdef CONFIG_NET_DMA
index aa797ae1c93acc92f72013aed3f42aa50fd8757e..5633f2a55b4c71b39d9c9f616452d6037fa4b595 100644 (file)
@@ -393,6 +393,7 @@ struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req,
 
                newtp->rcv_wup = newtp->copied_seq =
                newtp->rcv_nxt = treq->rcv_isn + 1;
+               newtp->segs_in = 0;
 
                newtp->snd_sml = newtp->snd_una =
                newtp->snd_nxt = newtp->snd_up = treq->snt_isn + 1;
index 5ed4db68cd4d838a2eb8383643f2df997ecab5b1..dfe06004ccca9a48aa7dc71c0d04ac4b324d7182 100644 (file)
@@ -958,6 +958,8 @@ static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,
                TCP_ADD_STATS(sock_net(sk), TCP_MIB_OUTSEGS,
                              tcp_skb_pcount(skb));
 
+       tp->segs_out += tcp_skb_pcount(skb);
+
        err = icsk->icsk_af_ops->queue_xmit(skb, &inet->cork.fl);
        if (likely(err <= 0))
                return err;
index c35b4ff323e00d4edaa13ea972b51c5c0b7ae266..18404777fdb77bdc364fff19dd8253f2fe299990 100644 (file)
@@ -1522,6 +1522,7 @@ process:
        skb->dev = NULL;
 
        bh_lock_sock_nested(sk);
+       tcp_sk(sk)->segs_in += max_t(u16, 1, skb_shinfo(skb)->gso_segs);
        ret = 0;
        if (!sock_owned_by_user(sk)) {
 #ifdef CONFIG_NET_DMA