Designing BPF kernel probe and Python monitor
The plan is to create a BPF C program attaching to tcp_sendmsg, filtering by destination port, and incrementing packet and byte counts in a map keyed by a fixed key. The Python script reads this map every second, computes deltas, and prints throughput. The BPF code will handle potential map initialization and use network-byte-order conversions with BCC macros.Providing complete BPF kernel probe and Python monitor
The final deliverable includes a standalone C BPF program attaching to tcp_sendmsg to count packets and bytes on a filtered TCP destination port, plus a Python script using BCC to load the program, read the map every second, and print throughput deltas. Both code parts are designed for clarity and direct use.
Below is a minimal example that uses a kprobe on tcp_sendmsg to count outgoing TCP “packets” (send calls) destined to a specific port and accumulate total bytes. A BPF hash map (counter_map) stores the running counters. The accompanying Python script loads this eBPF code with bcc, reads the map once per second, and prints the per‑second throughput.
eBPF (C) kernel probe (e.g. tcp_count.bpf.c)
#include <uapi/linux/ptrace.h>
#include <net/sock.h>
#include <net/inet_sock.h>
#include <bcc/proto.h>
// Change this to the port you want to monitor.
#define FILTER_PORT 8080
struct counter {
u64 packets;
u64 bytes;
};
// Single entry keyed by 0.
BPF_HASH(counter_map, u32, struct counter);
int kprobe__tcp_sendmsg(struct pt_regs *ctx, struct sock *sk, struct msghdr *msg, size_t size) {
// Destination port is stored in network byte order.
u16 dport = sk->__sk_common.skc_dport;
dport = ntohs(dport);
if (dport != FILTER_PORT)
return 0;
u32 key = 0; // Only one entry in the map.
struct counter *c = counter_map.lookup(&key);
if (!c) {
struct counter zero = {};
counter_map.update(&key, &zero);
c = counter_map.lookup(&key);
if (!c)
return 0; // Shouldn't happen
}
// Update counters
c->packets += 1;
c->bytes += size;
return 0;
}
Python userspace script (e.g. monitor.py)
#!/usr/bin/env python3
from bcc import BPF
import ctypes
import time
# Pick the port you want to monitor:
MONITOR_PORT = 8080
# Load/inline the above BPF C code, substituting FILTER_PORT
bpf_source = r"""
#include <uapi/linux/ptrace.h>
#include <net/sock.h>
#include <net/inet_sock.h>
#include <bcc/proto.h>
struct counter {
u64 packets;
u64 bytes;
};
BPF_HASH(counter_map, u32, struct counter);
#define FILTER_PORT %(PORT)d
int kprobe__tcp_sendmsg(struct pt_regs *ctx, struct sock *sk, struct msghdr *msg, size_t size) {
u16 dport = sk->__sk_common.skc_dport;
dport = ntohs(dport);
if (dport != FILTER_PORT)
return 0;
u32 key = 0;
struct counter *c = counter_map.lookup(&key);
if (!c) {
struct counter zero = {};
counter_map.update(&key, &zero);
c = counter_map.lookup(&key);
if (!c)
return 0;
}
c->packets += 1;
c->bytes += size;
return 0;
}
""" % {"PORT": MONITOR_PORT}
b = BPF(text=bpf_source) # Compile & load
counter_map = b.get_table("counter_map")
# For a single entry keyed by 0
key = ctypes.c_uint(0)
prev_packets = 0
prev_bytes = 0
print("Monitoring TCP send calls to port %d. Printing throughput every second..." % MONITOR_PORT)
while True:
try:
val = counter_map[key]
except KeyError:
val = None
packets = val.packets if val else 0
bytes_ = val.bytes if val else 0
print("Packets/s: %d, Bytes/s: %d"
% (packets - prev_packets, bytes_ - prev_bytes))
prev_packets = packets
prev_bytes = bytes_
time.sleep(1)
Notes:
- This example counts outgoing traffic (calls to
tcp_sendmsg). For inbound or more detailed parsing, attach to other functions or tracepoints and parse the skb/headers accordingly.
- The map key is hardcoded to
0 for a single global counter. If you need separate counters (e.g., per PID or per port), define a more complex key in the BPF program and adjust the user‑space map access accordingly.