Skip to content

Commit e11c95e

Browse files
authored
[Kernel scoping 5/5] Fix tcp drops PxL script (#1688)
Summary: Updates the currently broken `tcp_drops` PxL script to use a different BPFtrace program depending on the kernel version of the host it is deployed to. Related issues: Fixes #1582 Type of change: /kind bug Test Plan: Verified that new script works on kernels >=5.19. `Note`: due to backported changes (i.e. the kprobe was removed in older versions of the kernel), the old bpftrace script may not work on some older kernels <5.19. Unfortunately, the new script may also not work on older kernels because of other unsupported features. More testing is required to modify the old `tcp_drops` script to work on kernels <5.19. --------- Signed-off-by: Benjamin Kilimnik <bkilimnik@pixielabs.ai>
1 parent a0fb9ee commit e11c95e

4 files changed

Lines changed: 74 additions & 172 deletions

File tree

src/pxl_scripts/bpftrace/tcp_drops/data.pxl

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import pxtrace
2020
import px
2121

2222
# Adapted from https://github.com/iovisor/bpftrace/blob/master/tools/tcpdrop.bt
23-
program = """
23+
# Due to backported changes (i.e. the kprobe:tcp_drop was removed in older versions of the kernel),
24+
# the old bpftrace script may not work on some older kernels <5.19.
25+
pre_519_program = pxtrace.TraceProgram("""
2426
// tcpdrop.bt Trace TCP kernel-dropped packets/segments.
2527
// For Linux, uses bpftrace and eBPF.
2628
//
@@ -83,14 +85,84 @@ kprobe:tcp_drop
8385
$statestr);
8486
}
8587
}
88+
""",
89+
max_kernel='5.18'
90+
)
91+
92+
post_519_program = pxtrace.TraceProgram(
8693
"""
94+
// tcpdrop.bt Trace TCP kernel-dropped packets/segments.
95+
// For Linux, uses bpftrace and eBPF.
96+
//
97+
// Copyright (c) 2018 Dale Hamel.
98+
// Licensed under the Apache License, Version 2.0 (the "License")
99+
100+
#include <linux/socket.h>
101+
#include <net/sock.h>
102+
103+
BEGIN
104+
{
105+
// See https://github.com/torvalds/linux/blob/master/include/net/tcp_states.h
106+
@tcp_states[1] = "ESTABLISHED";
107+
@tcp_states[2] = "SYN_SENT";
108+
@tcp_states[3] = "SYN_RECV";
109+
@tcp_states[4] = "FIN_WAIT1";
110+
@tcp_states[5] = "FIN_WAIT2";
111+
@tcp_states[6] = "TIME_WAIT";
112+
@tcp_states[7] = "CLOSE";
113+
@tcp_states[8] = "CLOSE_WAIT";
114+
@tcp_states[9] = "LAST_ACK";
115+
@tcp_states[10] = "LISTEN";
116+
@tcp_states[11] = "CLOSING";
117+
@tcp_states[12] = "NEW_SYN_RECV";
118+
}
119+
120+
tracepoint:skb:kfree_skb
121+
{
122+
$reason = args->reason;
123+
$skb = (struct sk_buff *)args->skbaddr;
124+
$sk = ((struct sock *) $skb->sk);
125+
$inet_family = $sk->__sk_common.skc_family;
126+
127+
if ($reason > SKB_DROP_REASON_NOT_SPECIFIED &&
128+
($inet_family == AF_INET || $inet_family == AF_INET6)) {
129+
if ($inet_family == AF_INET) {
130+
$daddr = ntop($sk->__sk_common.skc_daddr);
131+
$saddr = ntop($sk->__sk_common.skc_rcv_saddr);
132+
} else {
133+
$daddr = ntop($sk->__sk_common.skc_v6_daddr.in6_u.u6_addr8);
134+
$saddr = ntop($sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr8);
135+
}
136+
$lport = $sk->__sk_common.skc_num;
137+
$dport = $sk->__sk_common.skc_dport;
138+
139+
// Destination port is big endian, it must be flipped
140+
$dport = bswap($dport);
141+
142+
$state = $sk->__sk_common.skc_state;
143+
$statestr = @tcp_states[$state];
144+
145+
printf(\"time_:%llu pid:%u pid_start_time:%llu src_ip:%s src_port:%d dst_ip:%s dst_port:%d state:%s\",
146+
nsecs,
147+
pid,
148+
((struct task_struct*)curtask)->group_leader->start_time / 10000000,
149+
$saddr,
150+
$lport,
151+
$daddr,
152+
$dport,
153+
$statestr);
154+
}
155+
}
156+
""",
157+
min_kernel='5.19'
158+
)
87159

88160

89161
def tcp_drops_func():
90162
table_name = 'tcp_drop_table'
91163
pxtrace.UpsertTracepoint('tcp_drop_tracer',
92164
table_name,
93-
program,
165+
[pre_519_program, post_519_program],
94166
pxtrace.kprobe(),
95167
"10m")
96168

src/pxl_scripts/px/tcp_drops/data.pxl

Lines changed: 0 additions & 113 deletions
This file was deleted.

src/pxl_scripts/px/tcp_drops/manifest.yaml

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/pxl_scripts/px/tcp_drops/vis.json

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)