trace: tcp: Add tracepoint for tcp_sendmsg()
Subsystems (4)
PATCH 1/1 trace: tcp: Add tracepoint for tcp_sendmsg()
Add a lightweight tracepoint to monitor TCP sendmsg operations, enabling the tracing of TCP messages being sent.
Meta has been using BPF programs to monitor this function for years, indicating significant interest in observing this important functionality. Adding a proper tracepoint provides a stable API for all users who need visibility into TCP message transmission.
The implementation uses DECLARE_TRACE instead of TRACE_EVENT to avoid creating unnecessary trace event infrastructure and tracefs exports, keeping the implementation minimal while stabilizing the API.
Given that this patch creates a rawtracepoint, you could hook into it using regular tooling, like bpftrace, using regular rawtracepoint infrastructure, such as:
rawtracepoint:tcp_sendmsg_tp {
....
}--- include/trace/events/tcp.h | 5 +++++ net/ipv4/tcp.c | 2 ++ 2 files changed, 7 insertions(+)
include/trace/events/tcp.h +5 -0
@@ -259,6 +259,11 @@ TRACE_EVENT(tcp_retransmit_synack, __entry->saddr_v6, __entry->daddr_v6) ); +DECLARE_TRACE (tcp_sendmsg_tp, + TP_PROTO(const struct sock *sk, const struct msghdr *msg, size_t size), +TP_ARGS (sk, msg, size) +); + DECLARE_TRACE(tcp_cwnd_reduction_tp, TP_PROTO(const struct sock *sk, int newly_acked_sacked, int newly_lost, int flag),
net/ipv4/tcp.c +2 -0
@@ -1362,6 +1362,8 @@ int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) {int ret; +trace_tcp_sendmsg_tp (sk, msg, size); +lock_sock (sk); ret =tcp_sendmsg_locked (sk, msg, size);release_sock (sk);
---
base-commit: e13b6da7045f997e1a5a5efd61d40e63c4fc20e8
change-id: 20250224-tcpsendmsg-4f0a236751d7
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
Quoted 13 lines · Breno Leitao
Add a lightweight tracepoint to monitor TCP sendmsg operations, enabling the tracing of TCP messages being sent.
Meta has been using BPF programs to monitor this function for years, indicating significant interest in observing this important functionality. Adding a proper tracepoint provides a stable API for all users who need visibility into TCP message transmission.
The implementation uses
DECLARE_TRACEinstead ofTRACE_EVENTto avoid creating unnecessary trace event infrastructure and tracefs exports, keeping the implementation minimal while stabilizing the API.
Quoted 7 lines
Given that this patch creates a rawtracepoint, you could hook into it using regular tooling, like bpftrace, using regular rawtracepoint infrastructure, such as:
rawtracepoint:tcp_sendmsg_tp { .... }
I would expect tcp_sendmsg() being stable enough ?
kprobe:tcp_sendmsg {
}
Quoted 20 lines
________________________________________
Quoted 17 lines · Breno Leitao
Add a lightweight tracepoint to monitor TCP sendmsg operations, enabling the tracing of TCP messages being sent.
Meta has been using BPF programs to monitor this function for years, indicating significant interest in observing this important functionality. Adding a proper tracepoint provides a stable API for all users who need visibility into TCP message transmission.
The implementation uses
DECLARE_TRACEinstead ofTRACE_EVENTto avoid creating unnecessary trace event infrastructure and tracefs exports, keeping the implementation minimal while stabilizing the API.Given that this patch creates a rawtracepoint, you could hook into it using regular tooling, like bpftrace, using regular rawtracepoint infrastructure, such as:
Quoted 8 lines
Quoted 3 lines
rawtracepoint:tcp_sendmsg_tp { .... }I would expect
tcp_sendmsg()being stable enough ?kprobe:tcp_sendmsg {
}
In LTO mode, tcp_sendmsg could be inlined cross files. For example,
net/ipv4/tcp.c:
int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
net/ipv4/tcp_bpf.c:
...
return tcp_sendmsg(sk, msg, size);
net/ipv6/af_inet6.c:
...
return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg, ...)And this does happen in our production environment.
Quoted 41 lines · Yonghong Song
Quoted 21 lines
________________________________________
Quoted 17 lines · Breno Leitao
Add a lightweight tracepoint to monitor TCP sendmsg operations, enabling the tracing of TCP messages being sent.
Meta has been using BPF programs to monitor this function for years, indicating significant interest in observing this important functionality. Adding a proper tracepoint provides a stable API for all users who need visibility into TCP message transmission.
The implementation uses
DECLARE_TRACEinstead ofTRACE_EVENTto avoid creating unnecessary trace event infrastructure and tracefs exports, keeping the implementation minimal while stabilizing the API.Given that this patch creates a rawtracepoint, you could hook into it using regular tooling, like bpftrace, using regular rawtracepoint infrastructure, such as:
Quoted 8 lines
Quoted 3 lines
rawtracepoint:tcp_sendmsg_tp { .... }I would expect
tcp_sendmsg()being stable enough ?kprobe:tcp_sendmsg {
}In LTO mode,
tcp_sendmsgcould be inlined cross files. For example,net/ipv4/tcp.c: int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) net/ipv4/tcp_bpf.c: ... return tcp_sendmsg(sk, msg, size); net/ipv6/af_inet6.c: ... return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg, ...)
Quoted 1 line
And this does happen in our production environment.
And we do not have a way to make the kprobe work even if LTO decided to inline a function ?
This seems like a tracing or LTO issue, this could be addressed there in a generic way
and avoid many other patches to work around this.
Hello Eric,
Quoted 42 lines · Eric Dumazet
Quoted 41 lines · Yonghong Song
Quoted 21 lines
________________________________________
Quoted 17 lines · Breno Leitao
Add a lightweight tracepoint to monitor TCP sendmsg operations, enabling the tracing of TCP messages being sent.
Meta has been using BPF programs to monitor this function for years, indicating significant interest in observing this important functionality. Adding a proper tracepoint provides a stable API for all users who need visibility into TCP message transmission.
The implementation uses
DECLARE_TRACEinstead ofTRACE_EVENTto avoid creating unnecessary trace event infrastructure and tracefs exports, keeping the implementation minimal while stabilizing the API.Given that this patch creates a rawtracepoint, you could hook into it using regular tooling, like bpftrace, using regular rawtracepoint infrastructure, such as:
Quoted 8 lines
Quoted 3 lines
rawtracepoint:tcp_sendmsg_tp { .... }I would expect
tcp_sendmsg()being stable enough ?kprobe:tcp_sendmsg {
}In LTO mode,
tcp_sendmsgcould be inlined cross files. For example,net/ipv4/tcp.c: int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) net/ipv4/tcp_bpf.c: ... return tcp_sendmsg(sk, msg, size); net/ipv6/af_inet6.c: ... return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg, ...)
Quoted 8 lines
Quoted 1 line
And this does happen in our production environment.
And we do not have a way to make the kprobe work even if LTO decided to inline a function ?
This seems like a tracing or LTO issue, this could be addressed there in a generic way
and avoid many other patches to work around this.
I understand that the {raw}tracepoint ensures the compiler cannot interfere with these hook points. For everything else, we rely on the hope that the compiler behaves favorably, which is far from ideal.
Masami Hiramatsu (Google)
maintainerQuoted 42 lines · On Mon, 24 Feb 2025 20:23:00 +0100 Eric Dumazet <[email protected]> wrote:
Quoted 41 lines · Yonghong Song
Quoted 21 lines
________________________________________
Quoted 17 lines · Breno Leitao
Add a lightweight tracepoint to monitor TCP sendmsg operations, enabling the tracing of TCP messages being sent.
Meta has been using BPF programs to monitor this function for years, indicating significant interest in observing this important functionality. Adding a proper tracepoint provides a stable API for all users who need visibility into TCP message transmission.
The implementation uses
DECLARE_TRACEinstead ofTRACE_EVENTto avoid creating unnecessary trace event infrastructure and tracefs exports, keeping the implementation minimal while stabilizing the API.Given that this patch creates a rawtracepoint, you could hook into it using regular tooling, like bpftrace, using regular rawtracepoint infrastructure, such as:
Quoted 8 lines
Quoted 3 lines
rawtracepoint:tcp_sendmsg_tp { .... }I would expect
tcp_sendmsg()being stable enough ?kprobe:tcp_sendmsg {
}In LTO mode,
tcp_sendmsgcould be inlined cross files. For example,net/ipv4/tcp.c: int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) net/ipv4/tcp_bpf.c: ... return tcp_sendmsg(sk, msg, size); net/ipv6/af_inet6.c: ... return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg, ...)
Quoted 4 lines
Quoted 1 line
And this does happen in our production environment.
And we do not have a way to make the kprobe work even if LTO decided to inline a function ?
There is perf probe command to set it up based on the debuginfo. But the function parameters sometimes optimized out and no more accessible. (You can try.)
Thus if you needs this tracepoint not temporarily nor debugging, I would recommend to use tracepoint (or trace event).
Thank you,
Quoted 4 lines
This seems like a tracing or LTO issue, this could be addressed there in a generic way
and avoid many other patches to work around this.
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Quoted 18 lines · Eric Dumazet
Quoted 17 lines · Breno Leitao
Add a lightweight tracepoint to monitor TCP sendmsg operations, enabling the tracing of TCP messages being sent.
Meta has been using BPF programs to monitor this function for years, indicating significant interest in observing this important functionality. Adding a proper tracepoint provides a stable API for all users who need visibility into TCP message transmission.
The implementation uses
DECLARE_TRACEinstead ofTRACE_EVENTto avoid creating unnecessary trace event infrastructure and tracefs exports, keeping the implementation minimal while stabilizing the API.Given that this patch creates a rawtracepoint, you could hook into it using regular tooling, like bpftrace, using regular rawtracepoint infrastructure, such as:
Quoted 8 lines
Quoted 3 lines
rawtracepoint:tcp_sendmsg_tp { .... }I would expect
tcp_sendmsg()being stable enough ?kprobe:tcp_sendmsg {
}
Also, if a tracepoint is added, inside of tcp_sendmsg_locked would cover more use cases (see kernel references to it).
We have a patch for a couple years now with a tracepoint inside the
while (msg_data_left(msg)) {
}
loop which is more useful than just entry to sendmsg.
Hello David,
Quoted 23 lines · David Ahern
Quoted 22 lines · Eric Dumazet
Quoted 20 lines · Breno Leitao
Add a lightweight tracepoint to monitor TCP sendmsg operations, enabling the tracing of TCP messages being sent.
Meta has been using BPF programs to monitor this function for years, indicating significant interest in observing this important functionality. Adding a proper tracepoint provides a stable API for all users who need visibility into TCP message transmission.
The implementation uses
DECLARE_TRACEinstead ofTRACE_EVENTto avoid creating unnecessary trace event infrastructure and tracefs exports, keeping the implementation minimal while stabilizing the API.Given that this patch creates a rawtracepoint, you could hook into it using regular tooling, like bpftrace, using regular rawtracepoint infrastructure, such as:
rawtracepoint:tcp_sendmsg_tp { .... }
Quoted 7 lines
Quoted 4 lines
I would expect
tcp_sendmsg()being stable enough ?kprobe:tcp_sendmsg {
}Also, if a tracepoint is added, inside of
tcp_sendmsg_lockedwould cover more use cases (see kernel references to it).
Agree, this seems to provide more useful information
Quoted 1 line
We have a patch for a couple years now with a tracepoint inside the
Sorry, where do you have this patch? is it downstream?
Quoted 4 lines
while (
msg_data_left(msg)) {
}loop which is more useful than just entry to sendmsg.
Do you mean something like the following?
include/trace/events/tcp.h +5 -0
@@ -259,6 +259,11 @@ TRACE_EVENT(tcp_retransmit_synack, __entry->saddr_v6, __entry->daddr_v6) ); +DECLARE_TRACE (tcp_sendmsg_tp, + TP_PROTO(const struct sock *sk, const struct msghdr *msg, size_t size, ssize_t copied), +TP_ARGS (sk, msg, size, copied) +); + DECLARE_TRACE(tcp_cwnd_reduction_tp, TP_PROTO(const struct sock *sk, int newly_acked_sacked, int newly_lost, int flag),
net/ipv4/tcp.c +2 -0
@@ -1290,6 +1290,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)sk_mem_charge (sk, copy); } +trace_tcp_sendmsg_tp (sk, msg, size, copy); +if (!copied)TCP_SKB_CB (skb)->tcp_flags &= ~TCPHDR_PSH ;
Quoted 8 lines · Breno Leitao
Quoted 2 lines
Also, if a tracepoint is added, inside of
tcp_sendmsg_lockedwould cover more use cases (see kernel references to it).Agree, this seems to provide more useful information
Quoted 1 line
We have a patch for a couple years now with a tracepoint inside the
Sorry, where do you have this patch? is it downstream?
company tree. Attached. Where to put tracepoints and what arguments to supply so that it is beneficial to multiple users is always a touchy subject :-), so I have not tried to push the patch out. sock arg should be added to it for example.
The key is to see how tcp_sendmsg_locked breaks up the buffers, and then another one in tcp_write_xmit to see when the actual push out happens. At the time I was looking at latency in the stack - from sendmsg call to driver pushing descriptors to hardware.
From 2298ca66c15bae6a95698abd8d029b9271fbefa3 Mon Sep 17 00:00:00 2001
From: David Ahern <[email protected]>
Date: Fri, 24 Mar 2023 22:07:53 +0000
Subject: [PATCH] tcp: Add tracepoints to tcp_write_xmit and tcp_sendmsg_locked
--- include/trace/events/tcp.h | 57 ++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp.c | 4 +++ net/ipv4/tcp_output.c | 1 + 3 files changed, 62 insertions(+)
include/trace/events/tcp.h +57 -0
@@ -187,6 +187,63 @@ DEFINE_EVENT(tcp_event_sk, tcp_rcv_space_adjust,TP_ARGS (sk) ); +TRACE_EVENT (tcp_sendmsg_locked, + + TP_PROTO(struct msghdr *msg, struct sk_buff *skb, int size_goal), + +TP_ARGS (msg, skb, size_goal), + +TP_STRUCT__entry ( + __field(__u64, skb) + __field(int, skb_len) + __field(int, msg_left) +__field (int, size_goal) + ), + +TP_fast_assign ( + __entry->skb = (__u64)skb; + __entry->skb_len = skb ? skb->len : 0; + __entry->msg_left = msg_data_left(msg); + __entry->size_goal = size_goal; + ), + +TP_printk ("skb %llx len %d msg_left %d size_goal %d" , + __entry->skb, __entry->skb_len, + __entry->msg_left, __entry->size_goal) +); + +TRACE_EVENT (tcp_write_xmit, + + TP_PROTO(struct sock *sk, unsigned int mss_now, int nonagle, u32 max_segs), + +TP_ARGS (sk, mss_now, nonagle,max_segs), + +TP_STRUCT__entry ( + __field(__u64, tcp_wstamp_ns) + __field(__u64, tcp_clock_cache) + __field(unsigned int, mss_now) + __field(unsigned int, max_segs) + __field(int, nonagle) +__field (int, sk_pacing) + ), + +TP_fast_assign ( + struct tcp_sock *tp = tcp_sk(sk); + + __entry->mss_now = mss_now; + __entry->nonagle = nonagle; + __entry->max_segs = max_segs; + __entry->sk_pacing = tcp_needs_internal_pacing(sk); + __entry->tcp_wstamp_ns = tp->tcp_wstamp_ns; + __entry->tcp_clock_cache = tp->tcp_clock_cache; + ), + +TP_printk ("mss %u segs %u nonagle %d sk_pacing %d tcp_wstamp_ns %lld tcp_clock_cache %lld" , + __entry->mss_now, __entry->max_segs, + __entry->nonagle, __entry->sk_pacing, + __entry->tcp_wstamp_ns, __entry->tcp_clock_cache) +); + TRACE_EVENT(tcp_retransmit_synack, TP_PROTO(const struct sock *sk, const struct request_sock *req),
net/ipv4/tcp.c +3 -0
@@ -279,6 +279,7 @@#include <linux/uaccess.h> #include <asm/ioctls.h> #include <net/busy_poll.h> +#include <trace/events/tcp.h> /* Track pending CMSGs. */ enum {
@@ -1312,6 +1313,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)if (skb) copy = size_goal - skb->len; +trace_tcp_sendmsg_locked (msg, skb, size_goal); +if (copy <=0 || !tcp_skb_can_collapse_to (skb)) {bool first_skb;
net/ipv4/tcp_output.c +1 -0
@@ -2623,6 +2623,7 @@ static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle, } max_segs =tcp_tso_segs (sk, mss_now); +trace_tcp_write_xmit (sk, mss_now, nonagle, max_segs);while ((skb =tcp_send_head (sk))) {unsigned int limit;
Signature · 1 line
2.43.0
Hello David,
Quoted 6 lines · David Ahern
Quoted 5 lines · Breno Leitao
Quoted 2 lines
Also, if a tracepoint is added, inside of
tcp_sendmsg_lockedwould cover more use cases (see kernel references to it).Agree, this seems to provide more useful information
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
We have a patch for a couple years now with a tracepoint inside the
Sorry, where do you have this patch? is it downstream?
company tree. Attached. Where to put tracepoints and what arguments to supply so that it is beneficial to multiple users is always a touchy subject :-)
Thanks. I would like to state that this would be useful for Meta as well.
Right now, we (Meta) are using nasty noinline attribute in tcp_sendmsg() in order to make the API stable, and this tracepoint would solve this problem avoiding the noinline hack. So, at least two type of users would benefit from it.
Quoted 2 lines
so I have not tried to push the patch out. sock arg should be added to it for example.
True, if it becomes a tracepoint instead of a rawtracepoint, the sock arg might be useful.
How would you recommend me proceeding in this case?
Thanks
--breno
Quoted 26 lines · Breno Leitao
Hello David,
Quoted 6 lines · David Ahern
Quoted 5 lines · Breno Leitao
Quoted 2 lines
Also, if a tracepoint is added, inside of
tcp_sendmsg_lockedwould cover more use cases (see kernel references to it).Agree, this seems to provide more useful information
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
We have a patch for a couple years now with a tracepoint inside the
Sorry, where do you have this patch? is it downstream?
company tree. Attached. Where to put tracepoints and what arguments to supply so that it is beneficial to multiple users is always a touchy subject :-)
Thanks. I would like to state that this would be useful for Meta as well.
Right now, we (Meta) are using nasty
noinlineattribute intcp_sendmsg()in order to make the API stable, and this tracepoint would solve this problem avoiding thenoinlinehack. So, at least two type of users would benefit from it.
Quoted 7 lines
Quoted 2 lines
so I have not tried to push the patch out. sock arg should be added to it for example.
True, if it becomes a tracepoint instead of a rawtracepoint, the sock arg might be useful.
How would you recommend me proceeding in this case?
In 2022, Menglong Dong added __fix_address
Then later , Yafang Shao added noinline_for_tracing .
Would one of them be sufficient ?
Quoted 33 lines · Eric Dumazet
Quoted 32 lines · Breno Leitao
Hello David,
Quoted 6 lines · David Ahern
Quoted 5 lines · Breno Leitao
Quoted 2 lines
Also, if a tracepoint is added, inside of
tcp_sendmsg_lockedwould cover more use cases (see kernel references to it).Agree, this seems to provide more useful information
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
We have a patch for a couple years now with a tracepoint inside the
Sorry, where do you have this patch? is it downstream?
company tree. Attached. Where to put tracepoints and what arguments to supply so that it is beneficial to multiple users is always a touchy subject :-)
Thanks. I would like to state that this would be useful for Meta as well.
Right now, we (Meta) are using nasty
noinlineattribute intcp_sendmsg()in order to make the API stable, and this tracepoint would solve this problem avoiding thenoinlinehack. So, at least two type of users would benefit from it.Quoted 2 lines
so I have not tried to push the patch out. sock arg should be added to it for example.
True, if it becomes a tracepoint instead of a rawtracepoint, the sock arg might be useful.
Quoted 7 lines
Quoted 1 line
How would you recommend me proceeding in this case?
In 2022, Menglong Dong added
__fix_addressThen later , Yafang Shao added
noinline_for_tracing.Would one of them be sufficient ?
tcp_sendmsg_locked should not be getting inlined; it is the sendmsg_locked handler and directly called by several subsystems.
ie., moving the tracepoint to tcp_sendmsg_locked should solve the inline problem. From there, the question is inside the loop or at entry to the function. Inside the loop has been very helpful for me.
Quoted 5 lines · David Ahern
Quoted 4 lines · Eric Dumazet
In 2022, Menglong Dong added
__fix_addressThen later , Yafang Shao added
noinline_for_tracing.
Quoted 8 lines
Quoted 1 line
Would one of them be sufficient ?
tcp_sendmsg_lockedshould not be getting inlined; it is thesendmsg_lockedhandler and directly called by several subsystems.ie., moving the tracepoint to
tcp_sendmsg_lockedshould solve the inline problem. From there, the question is inside the loop or at entry to the function. Inside the loop has been very helpful for me.
Inside the loop would work for me as well.
Quoted 5 lines · David Ahern
On 2/26/25 11:27 AM, Eric Dumazet wrote:
ie., moving the tracepoint to
tcp_sendmsg_lockedshould solve the inline problem. From there, the question is inside the loop or at entry to the function. Inside the loop has been very helpful for me.
I am happy to get it inside the loop. I am planning to send the following patch when the MW opens. How does it sound?
trace: tcp: Add tracepoint for tcp_sendmsg_locked()
Add a tracepoint to monitor TCP sendmsg operations, enabling the tracing of TCP messages being sent.
Meta has been using BPF programs to monitor tcp_sendmsg() for years, indicating significant interest in observing this important functionality. Adding a proper tracepoint provides a stable API for all users who need visibility into TCP message transmission.
David Ahern is using a similar functionality with a custom patch[1]. So, this means we have more than a single use case for this request.
The implementation adopts David's approach[1] for greater flexibility compared to the initial proposal.
include/trace/events/tcp.h +23 -0
@@ -259,6 +259,29 @@ TRACE_EVENT(tcp_retransmit_synack, __entry->saddr_v6, __entry->daddr_v6) ); +TRACE_EVENT (tcp_sendmsg_locked, + TP_PROTO(struct msghdr *msg, struct sk_buff *skb, int size_goal), + +TP_ARGS (msg, skb, size_goal), + +TP_STRUCT__entry ( + __field(__u64, skb) + __field(int, skb_len) + __field(int, msg_left) +__field (int, size_goal) + ), + +TP_fast_assign ( + __entry->skb = (__u64)skb; + __entry->skb_len = skb ? skb->len : 0; + __entry->msg_left = msg_data_left(msg); + __entry->size_goal = size_goal; + ), + +TP_printk ("skb %llx skb_len %d msg_left %d size_goal %d" , __entry->skb, + __entry->skb_len, __entry->msg_left, __entry->size_goal) +); + DECLARE_TRACE(tcp_cwnd_reduction_tp, TP_PROTO(const struct sock *sk, int newly_acked_sacked, int newly_lost, int flag),
net/ipv4/tcp.c +2 -0
@@ -1160,6 +1160,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)if (skb) copy = size_goal - skb->len; +trace_tcp_sendmsg_locked (msg, skb, size_goal); +if (copy <=0 || !tcp_skb_can_collapse_to (skb)) {bool first_skb;
include/trace/events/tcp.h TRACE_EVENT(tcp_retransmit_synack, · line 263
Quoted 10 lines · Breno Leitao
include/trace/events/tcp.h +2
-0@@ -259,6 +259,29 @@ TRACE_EVENT(tcp_retransmit_synack, __entry->saddr_v6, __entry->daddr_v6) ); +TRACE_EVENT(tcp_sendmsg_locked, + TP_PROTO(struct msghdr *msg, struct sk_buff *skb, int size_goal),
How about passing in the sk reference here; not needed for trace entries, but makes it directly accessible for bpf programs.
Otherwise, LGTM.
Steven Rostedt
maintainerQuoted 2 lines · On Wed, 2 Apr 2025 08:11:03 -0600 David Ahern <[email protected]> wrote:
How about passing in the sk reference here; not needed for trace entries, but makes it directly accessible for bpf programs.
Not to mention event probes (dynamic trace events that can be attached to existing events and the fields can be dereferenced). One day I'll add documentation about event probes as they have been in the kernel since 5.15! :-p
-- Steve
Quoted 9 lines · David Ahern
Quoted 8 lines · Breno Leitao
include/trace/events/tcp.h +0
-0@@ -259,6 +259,29 @@ TRACE_EVENT(tcp_retransmit_synack, __entry->saddr_v6, __entry->daddr_v6) );
Quoted 5 lines
include/trace/events/tcp.h TRACE_EVENT(tcp_retransmit_synack, · line 263
Quoted 2 lines
include/trace/events/tcp.h +2
-0+TRACE_EVENT(tcp_sendmsg_locked, + TP_PROTO(struct msghdr *msg, struct sk_buff *skb, int size_goal),How about passing in the sk reference here; not needed for trace entries, but makes it directly accessible for bpf programs.
Right, so, without the trace entries, the output of /sys/kernel/debug/tracing/events/tcp/tcp_sendmsg_locked/format doesn't show it, right?
field:__u64 skb; offset:8; size:8; signed:0; field:int skb_len; offset:16; size:4; signed:1; field:int msg_left; offset:20; size:4; signed:1; field:int size_goal; offset:24; size:4; signed:1;
This is what I've been testing now:
trace: tcp: Add tracepoint for tcp_sendmsg_locked() Add a tracepoint to monitor TCP sendmsg operations, enabling the tracing of TCP messages being sent. Meta has been using BPF programs to monitor tcp_sendmsg() for years, indicating significant interest in observing this important functionality. Adding a proper tracepoint provides a stable API for all users who need visibility into TCP message transmission. David Ahern is using a similar functionality with a custom patch[1]. So, this means we have more than a single use case for this request. The implementation adopts David's approach[1] for greater flexibility compared to the initial proposal. Link: https://lore.kernel.org/all/[email protected]/ [1] Signed-off-by: Breno Leitao <[email protected]> diff --git a/include/trace/events/tcp.h b/include/trace/events/tcp.h index 1a40c41ff8c30..21529d5faf3b1 100644 --- a/include/trace/events/tcp.h +++ b/include/trace/events/tcp.h @@ -259,6 +259,30 @@ TRACE_EVENT(tcp_retransmit_synack, __entry->saddr_v6, __entry->daddr_v6) ); +TRACE_EVENT(tcp_sendmsg_locked, + TP_PROTO(const struct sock *sk, const struct msghdr *msg, + const struct sk_buff *skb, int size_goal), + + TP_ARGS(sk, msg, skb, size_goal), + + TP_STRUCT__entry( + __field(__u64, skb) + __field(int, skb_len) + __field(int, msg_left) + __field(int, size_goal) + ), + + TP_fast_assign( + __entry->skb = (__u64)skb; + __entry->skb_len = skb ? skb->len : 0; + __entry->msg_left = msg_data_left(msg); + __entry->size_goal = size_goal; + ), + + TP_printk("skb %llx skb_len %d msg_left %d size_goal %d", __entry->skb, + __entry->skb_len, __entry->msg_left, __entry->size_goal) +); + DECLARE_TRACE(tcp_cwnd_reduction_tp, TP_PROTO(const struct sock *sk, int newly_acked_sacked, int newly_lost, int flag), diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index ea8de00f669d0..270ce2c8c2d54 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -1160,6 +1160,8 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) if (skb) copy = size_goal - skb->len; + trace_tcp_sendmsg_locked(sk, msg, skb, size_goal); + if (copy <= 0 || !tcp_skb_can_collapse_to(skb)) { bool first_skb;
Plus this small dependency:
net: pass const to msg_data_left()
The msg_data_left() function doesn't modify the struct msghdr parameter, so mark it as const. This allows the function to be used with const references, improving type safety and making the API more flexible.
diff --git a/include/linux/socket.h b/include/linux/socket.h
index c3322eb3d6865..3b262487ec060 100644
--- a/include/linux/socket.h
+++ b/include/linux/socket.h
@@ -168,7 +168,7 @@ static inline struct cmsghdr * cmsg_nxthdr (struct msghdr *__msg, struct cmsghdr
return __cmsg_nxthdr(__msg->msg_control, __msg->msg_controllen, __cmsg);
}
-static inline size_t msg_data_left(struct msghdr *msg) +static inline size_t msg_data_left(const struct msghdr *msg) {
return iov_iter_count(&msg->msg_iter);
}
Thanks for your help,
Breno
Hi David,
Quoted 5 lines · David Ahern
Quoted 3 lines · Breno Leitao
Quoted 2 lines
Also, if a tracepoint is added, inside of
tcp_sendmsg_lockedwould cover more use cases (see kernel references to it).
Quoted 8 lines
Quoted 5 lines
Agree, this seems to provide more useful information
Quoted 1 line
We have a patch for a couple years now with a tracepoint inside the
Sorry, where do you have this patch? is it downstream?
company tree. Attached. Where to put tracepoints and what arguments to supply so that it is beneficial to multiple users is always a touchy
Right. I am always eager to establish a standard evaluation/method which developers have common sense in. It's really hard because I gave it a try before. Maintainers seem not to like to see too many tracepoints appearing in the stack.
Quoted 5 lines
subject :-), so I have not tried to push the patch out. sock arg should be added to it for example.
The key is to see how
tcp_sendmsg_lockedbreaks up the buffers, and then another one intcp_write_xmitto see when the actual push out happens.
Agreed on this point because a fine-grained BPF program can take advantage of it. But it seems another small topic that is probably different from what the original motivation from Breno is in this
patch: I guess, making the tcp_sendmsg_locked non-inlined can allow the BPF program to calculate the delta between when tcp_sendmsg_locked starts and when tcp_sendmsg_locked ends? I don't know. Probably as Eric said, using noinline or something like this is simpler?
Quoted 2 lines
At the time I was looking at latency in the stack - from sendmsg call to driver pushing descriptors to hardware.
So do I.
Thanks,
Jason