Skip to content

Commit 0df48c2

Browse files
edumazetdavem330
authored andcommitted
tcp: add tcpi_bytes_acked to tcp_info
This patch tracks total number of bytes acked for a TCP socket. This is the sum of all changes done to tp->snd_una, and allows for precise tracking of delivered data. RFC4898 named this : tcpEStatsAppHCThruOctetsAcked 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_acked was placed near tp->snd_una for best data locality and minimal performance impact. Signed-off-by: Eric Dumazet <edumazet@google.com> Acked-by: Yuchung Cheng <ycheng@google.com> Cc: Matt Mathis <mattmathis@google.com> Cc: Eric Salo <salo@google.com> Cc: Martin Lau <kafai@fb.com> Cc: Chris Rapier <rapier@psc.edu> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 2decb26 commit 0df48c2

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

include/linux/tcp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ struct tcp_sock {
150150
u32 rcv_wup; /* rcv_nxt on last window update sent */
151151
u32 snd_nxt; /* Next sequence we send */
152152

153+
u64 bytes_acked; /* RFC4898 tcpEStatsAppHCThruOctetsAcked
154+
* sum(delta(snd_una)), or how many bytes
155+
* were acked.
156+
*/
153157
u32 snd_una; /* First byte we want an ack for */
154158
u32 snd_sml; /* Last byte of the most recently transmitted small packet */
155159
u32 rcv_tstamp; /* timestamp of last received ACK (for keepalives) */

include/net/tcp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
576576
}
577577

578578
/* tcp.c */
579-
void tcp_get_info(const struct sock *, struct tcp_info *);
579+
void tcp_get_info(struct sock *, struct tcp_info *);
580580

581581
/* Read 'sendfile()'-style from a TCP socket */
582582
typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,

include/uapi/linux/tcp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ struct tcp_info {
189189

190190
__u64 tcpi_pacing_rate;
191191
__u64 tcpi_max_pacing_rate;
192+
__u64 tcpi_bytes_acked; /* RFC4898 tcpEStatsAppHCThruOctetsAcked */
192193
};
193194

194195
/* for TCP_MD5SIG socket option */

net/ipv4/tcp.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,7 @@ EXPORT_SYMBOL(compat_tcp_setsockopt);
25922592
#endif
25932593

25942594
/* Return information about state of tcp endpoint in API format. */
2595-
void tcp_get_info(const struct sock *sk, struct tcp_info *info)
2595+
void tcp_get_info(struct sock *sk, struct tcp_info *info)
25962596
{
25972597
const struct tcp_sock *tp = tcp_sk(sk);
25982598
const struct inet_connection_sock *icsk = inet_csk(sk);
@@ -2663,6 +2663,10 @@ void tcp_get_info(const struct sock *sk, struct tcp_info *info)
26632663

26642664
rate = READ_ONCE(sk->sk_max_pacing_rate);
26652665
info->tcpi_max_pacing_rate = rate != ~0U ? rate : ~0ULL;
2666+
2667+
spin_lock_bh(&sk->sk_lock.slock);
2668+
info->tcpi_bytes_acked = tp->bytes_acked;
2669+
spin_unlock_bh(&sk->sk_lock.slock);
26662670
}
26672671
EXPORT_SYMBOL_GPL(tcp_get_info);
26682672

net/ipv4/tcp_input.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3280,6 +3280,15 @@ static inline bool tcp_may_update_window(const struct tcp_sock *tp,
32803280
(ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd);
32813281
}
32823282

3283+
/* If we update tp->snd_una, also update tp->bytes_acked */
3284+
static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
3285+
{
3286+
u32 delta = ack - tp->snd_una;
3287+
3288+
tp->bytes_acked += delta;
3289+
tp->snd_una = ack;
3290+
}
3291+
32833292
/* Update our send window.
32843293
*
32853294
* Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
@@ -3315,7 +3324,7 @@ static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32
33153324
}
33163325
}
33173326

3318-
tp->snd_una = ack;
3327+
tcp_snd_una_update(tp, ack);
33193328

33203329
return flag;
33213330
}
@@ -3497,7 +3506,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
34973506
* Note, we use the fact that SND.UNA>=SND.WL2.
34983507
*/
34993508
tcp_update_wl(tp, ack_seq);
3500-
tp->snd_una = ack;
3509+
tcp_snd_una_update(tp, ack);
35013510
flag |= FLAG_WIN_UPDATE;
35023511

35033512
tcp_in_ack_event(sk, CA_ACK_WIN_UPDATE);

0 commit comments

Comments
 (0)