Skip to content

Commit bdd1f9e

Browse files
Eric Dumazetdavem330
authored andcommitted
tcp: add tcpi_bytes_received to tcp_info
This patch tracks total number of payload bytes received on a TCP socket. This is the sum of all changes done to tp->rcv_nxt RFC4898 named this : tcpEStatsAppHCThruOctetsReceived This is a 64bit field, 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->bytes_received was placed near tp->rcv_nxt for best data locality and minimal performance impact. Signed-off-by: Eric Dumazet <[email protected]> Cc: Yuchung Cheng <[email protected]> Cc: Matt Mathis <[email protected]> Cc: Eric Salo <[email protected]> Cc: Martin Lau <[email protected]> Cc: Chris Rapier <[email protected]> Acked-by: Yuchung Cheng <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 0df48c2 commit bdd1f9e

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

include/linux/tcp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ struct tcp_sock {
145145
* read the code and the spec side by side (and laugh ...)
146146
* See RFC793 and RFC1122. The RFC writes these in capitals.
147147
*/
148+
u64 bytes_received; /* RFC4898 tcpEStatsAppHCThruOctetsReceived
149+
* sum(delta(rcv_nxt)), or how many bytes
150+
* were acked.
151+
*/
148152
u32 rcv_nxt; /* What we want to receive next */
149153
u32 copied_seq; /* Head of yet unread data */
150154
u32 rcv_wup; /* rcv_nxt on last window update sent */

include/uapi/linux/tcp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ struct tcp_info {
190190
__u64 tcpi_pacing_rate;
191191
__u64 tcpi_max_pacing_rate;
192192
__u64 tcpi_bytes_acked; /* RFC4898 tcpEStatsAppHCThruOctetsAcked */
193+
__u64 tcpi_bytes_received; /* RFC4898 tcpEStatsAppHCThruOctetsReceived */
193194
};
194195

195196
/* for TCP_MD5SIG socket option */

net/ipv4/tcp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,7 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)
26662666

26672667
spin_lock_bh(&sk->sk_lock.slock);
26682668
info->tcpi_bytes_acked = tp->bytes_acked;
2669+
info->tcpi_bytes_received = tp->bytes_received;
26692670
spin_unlock_bh(&sk->sk_lock.slock);
26702671
}
26712672
EXPORT_SYMBOL_GPL(tcp_get_info);

net/ipv4/tcp_fastopen.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ static bool tcp_fastopen_create_child(struct sock *sk,
206206
skb_set_owner_r(skb2, child);
207207
__skb_queue_tail(&child->sk_receive_queue, skb2);
208208
tp->syn_data_acked = 1;
209+
tp->bytes_received = end_seq - TCP_SKB_CB(skb)->seq - 1;
209210
} else {
210211
end_seq = TCP_SKB_CB(skb)->seq + 1;
211212
}

net/ipv4/tcp_input.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3289,6 +3289,15 @@ static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
32893289
tp->snd_una = ack;
32903290
}
32913291

3292+
/* If we update tp->rcv_nxt, also update tp->bytes_received */
3293+
static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
3294+
{
3295+
u32 delta = seq - tp->rcv_nxt;
3296+
3297+
tp->bytes_received += delta;
3298+
tp->rcv_nxt = seq;
3299+
}
3300+
32923301
/* Update our send window.
32933302
*
32943303
* Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
@@ -4245,7 +4254,7 @@ static void tcp_ofo_queue(struct sock *sk)
42454254

42464255
tail = skb_peek_tail(&sk->sk_receive_queue);
42474256
eaten = tail && tcp_try_coalesce(sk, tail, skb, &fragstolen);
4248-
tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
4257+
tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
42494258
if (!eaten)
42504259
__skb_queue_tail(&sk->sk_receive_queue, skb);
42514260
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
@@ -4413,7 +4422,7 @@ static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int
44134422
__skb_pull(skb, hdrlen);
44144423
eaten = (tail &&
44154424
tcp_try_coalesce(sk, tail, skb, fragstolen)) ? 1 : 0;
4416-
tcp_sk(sk)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
4425+
tcp_rcv_nxt_update(tcp_sk(sk), TCP_SKB_CB(skb)->end_seq);
44174426
if (!eaten) {
44184427
__skb_queue_tail(&sk->sk_receive_queue, skb);
44194428
skb_set_owner_r(skb, sk);
@@ -4506,7 +4515,7 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
45064515

45074516
eaten = tcp_queue_rcv(sk, skb, 0, &fragstolen);
45084517
}
4509-
tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
4518+
tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
45104519
if (skb->len)
45114520
tcp_event_data_recv(sk, skb);
45124521
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
@@ -5254,7 +5263,7 @@ void tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
52545263
tcp_rcv_rtt_measure_ts(sk, skb);
52555264

52565265
__skb_pull(skb, tcp_header_len);
5257-
tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
5266+
tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
52585267
NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITSTOUSER);
52595268
eaten = 1;
52605269
}

0 commit comments

Comments
 (0)