-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug_xdp.py
More file actions
39 lines (33 loc) · 1.01 KB
/
debug_xdp.py
File metadata and controls
39 lines (33 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/python3
from bcc import BPF
import time
# --- CONFIGURATION ---
device = "wlo1" # Your Wi-Fi interface
# --- Kernel Code ---
program = """
int hello_packet(struct xdp_md *ctx) {
// Print this for EVERY packet, no matter what it is
bpf_trace_printk("I see a packet! Len: %d\\n", ctx->data_end - ctx->data);
return XDP_PASS;
}
"""
# --- Python Logic ---
print(f"Loading 'Hello World' XDP on {device}...")
b = BPF(text=program)
fn = b.load_func("hello_packet", BPF.XDP)
# Force SKB (Generic) mode. This simulates XDP in software.
# It is slower but works on almost all hardware.
print("Attaching in SKB/Generic Mode...")
try:
b.attach_xdp(device, fn, flags=BPF.XDP_FLAGS_SKB_MODE)
except Exception as e:
print(f"Failed to attach: {e}")
exit()
print("Running! Watch the logs now.")
print("Run this in another terminal: sudo cat /sys/kernel/debug/tracing/trace_pipe")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nDetaching...")
b.remove_xdp(device, 0)