Skip to content

Commit

Permalink
tools/tcptop: Check the presence of tcp_sendpage before attaching it
Browse files Browse the repository at this point in the history
In recent kernels, tcp_sendpage() has been replaced by
sendmsg(MSG_SPLICE_PAGES). Check the presence of the k(ret)probes
before attaching them.

Signed-off-by: Jerome Marchand <[email protected]>
  • Loading branch information
jeromemarchand authored and yonghong-song committed Aug 5, 2023
1 parent 37c1300 commit ddf4961
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions tools/tcptop.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def range_check(string):
return 0;
}
int kretprobe__tcp_sendmsg(struct pt_regs *ctx)
int tcp_send_ret(struct pt_regs *ctx)
{
int size = PT_REGS_RC(ctx);
if (size > 0)
Expand All @@ -175,16 +175,7 @@ def range_check(string):
return 0;
}
int kretprobe__tcp_sendpage(struct pt_regs *ctx)
{
int size = PT_REGS_RC(ctx);
if (size > 0)
return tcp_sendstat(size);
else
return 0;
}
static int tcp_send_entry(struct sock *sk)
int tcp_send_entry(struct pt_regs *ctx, struct sock *sk)
{
if (container_should_be_filtered()) {
return 0;
Expand All @@ -198,17 +189,6 @@ def range_check(string):
return 0;
}
int kprobe__tcp_sendmsg(struct pt_regs *ctx, struct sock *sk,
struct msghdr *msg, size_t size)
{
return tcp_send_entry(sk);
}
int kprobe__tcp_sendpage(struct pt_regs *ctx, struct sock *sk,
struct page *page, int offset, size_t size)
{
return tcp_send_entry(sk);
}
/*
* tcp_recvmsg() would be obvious to trace, but is less suitable because:
* - we'd need to trace both entry and return, to have both sock and size
Expand Down Expand Up @@ -300,6 +280,12 @@ def get_ipv6_session_key(k):
# initialize BPF
b = BPF(text=bpf_text)

b.attach_kprobe(event='tcp_sendmsg', fn_name='tcp_send_entry')
b.attach_kretprobe(event='tcp_sendmsg', fn_name='tcp_send_ret')
if BPF.get_kprobe_functions(b'tcp_sendpage'):
b.attach_kprobe(event='tcp_sendpage', fn_name='tcp_send_entry')
b.attach_kretprobe(event='tcp_sendpage', fn_name='tcp_send_ret')

ipv4_send_bytes = b["ipv4_send_bytes"]
ipv4_recv_bytes = b["ipv4_recv_bytes"]
ipv6_send_bytes = b["ipv6_send_bytes"]
Expand Down

0 comments on commit ddf4961

Please sign in to comment.