Skip to contentSkip to conversation

trace: add tracepoint for tcp_sendmsg_locked()

PATCH Applied Breno Leitao 5 participants 7 messages Apr 8, 2025 – Apr 11, 2025

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

v3 cover trace: add tracepoint for tcp_sendmsg_locked()

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, and it might be a good idea to have such feature upstream.

---
Changes in v3:
- Fixed identation (Kuniyuki Iwashi)
- Removed duplicated entry in the commit message (Kuniyuki Iwashi)
- Added tcp_sendmsg_locked in bpf_raw_tp_null_args[] (Kuniyuki Iwashi)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Change to a full tracepoint inside tcp_sendmsg_locked(), heavily inspired in David's patch
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Breno Leitao (2):
net: pass const to msg_data_left()
trace: tcp: Add tracepoint for tcp_sendmsg_locked()

 include/linux/socket.h     |  2 +-
 include/trace/events/tcp.h | 24 ++++++++++++++++++++++++
 kernel/bpf/btf.c           |  1 +
 net/ipv4/tcp.c             |  2 ++
 4 files changed, 28 insertions(+), 1 deletion(-)

---
base-commit: 1a9239bb4253f9076b5b4b2a1a4e8d7defd77a95
change-id: 20250224-tcpsendmsg-4f0a236751d7

Best regards,

Signature · 1 line
Breno Leitao <[email protected]>
#1 lore raw

PATCH v3 1/2 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.

---
 include/linux/socket.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/socket.h +1 -1
index c3322eb3d6865..3b262487ec060 100644
@@ -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);
 }
Signature · 1 line
2.47.1
#2 lore raw

PATCH v3 2/2 trace: tcp: Add tracepoint for tcp_sendmsg_locked()

In reply to Breno Leitao

Add a tracepoint to monitor TCP send operations, enabling detailed visibility into TCP message transmission.

Create a new tracepoint within the tcp_sendmsg_locked function, capturing traditional fields along with size_goal, which indicates the optimal data size for a single TCP segment. Additionally, a reference to the struct sock sk is passed, allowing direct access for BPF programs. The implementation is largely based on David's patch[1] and suggestions.

---
 include/trace/events/tcp.h | 24 ++++++++++++++++++++++++
 kernel/bpf/btf.c           |  1 +
 net/ipv4/tcp.c             |  2 ++
 3 files changed, 27 insertions(+)
include/trace/events/tcp.h +24 -0
index 1a40c41ff8c30..75d3d53a3832c 100644
@@ -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(const void *, skb_addr)
+		__field(int, skb_len)
+		__field(int, msg_left)
+		__field(int, size_goal)
+	),
+
+	TP_fast_assign(
+		__entry->skb_addr = skb;
+		__entry->skb_len = skb ? skb->len : 0;
+		__entry->msg_left = msg_data_left(msg);
+		__entry->size_goal = size_goal;
+	),
+
+	TP_printk("skb_addr %p skb_len %d msg_left %d size_goal %d",
+		  __entry->skb_addr, __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),
kernel/bpf/btf.c +1 -0
index eacb701bc2be2..475a1317ad275 100644
@@ -6518,6 +6518,7 @@ static const struct bpf_raw_tp_null_args raw_tp_null_args[] = {
 	{ "xprt_put_cong", 0x10 },
 	/* tcp */
 	{ "tcp_send_reset", 0x11 },
+	{ "tcp_sendmsg_locked", 0x100 },
 	/* tegra_apb_dma */
 	{ "tegra_dma_tx_status", 0x100 },
 	/* timer_migration */
net/ipv4/tcp.c +2 -0
index ea8de00f669d0..270ce2c8c2d54 100644
@@ -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;
 
Signature · 1 line
2.47.1
Reviewed-by on v3 2/2
#3 lore raw

Re: [PATCH net-next v3 2/2] trace: tcp: Add tracepoint for tcp_sendmsg_locked()

Quoted 9 lines · Breno Leitao

Add a tracepoint to monitor TCP send operations, enabling detailed visibility into TCP message transmission.

Create a new tracepoint within the tcp_sendmsg_locked function, capturing traditional fields along with size_goal, which indicates the optimal data size for a single TCP segment. Additionally, a reference to the struct sock sk is passed, allowing direct access for BPF programs. The implementation is largely based on David's patch[1] and suggestions.

Quoted 8 lines
---
 include/trace/events/tcp.h | 24 ++++++++++++++++++++++++
 kernel/bpf/btf.c           |  1 +
 net/ipv4/tcp.c             |  2 ++
 3 files changed, 27 insertions(+)
Reviewed-by on v3 2/2
#4 lore raw

Re: [PATCH net-next v3 2/2] trace: tcp: Add tracepoint for tcp_sendmsg_locked()

In reply to Breno Leitao

From: Breno Leitao <[email protected]>
Date: Tue, 08 Apr 2025 11:32:02 -0700

Quoted 11 lines

Add a tracepoint to monitor TCP send operations, enabling detailed visibility into TCP message transmission.

Create a new tracepoint within the tcp_sendmsg_locked function, capturing traditional fields along with size_goal, which indicates the optimal data size for a single TCP segment. Additionally, a reference to the struct sock sk is passed, allowing direct access for BPF programs. The implementation is largely based on David's patch[1] and suggestions.

Reviewed-by on v3 2/2
#5 lore raw

Re: [PATCH net-next v3 2/2] trace: tcp: Add tracepoint for tcp_sendmsg_locked()

In reply to Breno Leitao
Quoted 4 lines · Breno Leitao

Add a tracepoint to monitor TCP send operations, enabling detailed visibility into TCP message transmission.

Quoted 8 lines

Create a new tracepoint within the tcp_sendmsg_locked function, capturing traditional fields along with size_goal, which indicates the optimal data size for a single TCP segment. Additionally, a reference to the struct sock sk is passed, allowing direct access for BPF programs. The implementation is largely based on David's patch[1] and suggestions.

#6 lore raw
In reply to Breno Leitao
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Tue, 08 Apr 2025 11:32:00 -0700 you wrote:

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, and it might be a good idea to have such feature upstream.

[...]

Here is the summary with links:
- [net-next,v3,1/2] net: pass const to msg_data_left()

    https://git.kernel.org/netdev/net-next/c/b1e904999542
  - [net-next,v3,2/2] trace: tcp: Add tracepoint for tcp_sendmsg_locked()
    https://git.kernel.org/netdev/net-next/c/0f08335ade71

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html