An advanced eBPF-based tool that hooks into kernel TCP retransmission events to analyze packet drop behavior and correlate retransmission outcomes with Traffic Control (TC) filtering decisions.
ReBPF implements a sophisticated three-point hooking system using eBPF to monitor and analyze TCP packet retransmissions. The program tracks packet identifiers at retransmission entry, attempts selective dropping via Traffic Control, and captures the actual retransmission results to determine how TC drop decisions affect kernel return codes.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Kernel Space β
β β
β βββββββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββββββ β
β βfentry/ β β TC β βfexit/ β β
β βtcp_retransmit_skb β β (Egress) β βtcp_retransmit_skb β β
β β β β β β β β
β β1. Capture packet ID βββββΆβ2. Find packet & βββββΆβ3. Get return value β β
β β & identifiers β β decide: β β from kernel func β β
β β β β TC_ACT_OK or β β β β
β β β β TC_ACT_SHOT β β β β
β βββββββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββββββ β
β β β β β
βββββββββββββββΌββββββββββββββββββββββββββΌββββββββββββββββββββββββββΌββββββββββββ
β β β
β β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Ring Buffer β
β (retransmit events) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Userspace β
β Go Application β
β β
β β’ Read ring buffer events β
β β’ Correlate fentry/fexit data β
β β’ Print TC drop decision impact on return codes β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- fentry/tcp_retransmit_skb: Captures packet identifiers before retransmission attempt
- TC (Traffic Control): Searches for target packets and makes drop/pass decisions
- fexit/tcp_retransmit_skb: Retrieves the actual return value from the kernel function
- Ring Buffer: High-performance communication channel for event data
- Go Userspace: Correlates events and analyzes the impact of TC decisions on return codes
- β Three-point hooking system - fentry/tcp_retransmit_skb, TC, fexit/tcp_retransmit_skb
- β Packet identification tracking - Records packet identifiers at retransmission entry
- β Traffic Control integration - Finds and selectively drops packets at TC layer
- β Return code analysis - Correlates TC drop decisions with kernel function return values
- β Ring buffer communication - Efficient kernel-to-userspace event streaming
- β Real-time monitoring - Live analysis of retransmission behavior and drop impacts
- Linux kernel 5.8+ with eBPF support
- Go 1.19+
clangandllvmfor eBPF compilation- Root privileges (required for eBPF program loading)
make generatemake build-rebpfsudo ./rebpfTo customize packet filtering, edit the filter settings in internal/probe/probe.go:
func (p *probe) attachPrograms() error {
// Configure target IP and port
targetIP := "172.17.0.2" // Change to your target IP
targetPort := 5201 // Change to your target port
matchIp, err := parseIPv4ToBe32(targetIP)
if err != nil {
return err
}
err = p.bpfObjects.probeVariables.MatchIp.Set(matchIp)
err = p.bpfObjects.probeVariables.MatchPort.Set(htons(uint16(targetPort)))
return nil
}The program monitors TCP retransmissions and correlates TC drop decisions with kernel return codes. When running, you'll see output showing:
- Packet identifiers captured at fentry/tcp_retransmit_skb
- TC drop decisions (TC_ACT_OK or TC_ACT_SHOT)
- Corresponding return codes from fexit/tcp_retransmit_skb
- Analysis of how TC drops affect retransmission outcomes
ReBPF/
βββ bpf/
β βββ rebpf.bpf.c # eBPF kernel programs
β βββ common.h # Shared structures and definitions
βββ internal/
β βββ probe/
β β βββ probe.go # Main userspace logic
β βββ packet/
β βββ packet.go # Packet parsing and formatting
βββ scripts/
β βββ netem.sh # Network emulation scripts for testing
βββ Makefile # Build automation
βββ README.md
# Terminal 1: Start ReBPF
sudo ./rebpf
# Terminal 2: Generate traffic with packet loss to trigger retransmissions
iperf3 -s -p 5201 &
# Add network conditions to force retransmissions
sudo tc qdisc add dev lo root netem loss 2% delay 100ms
# Generate TCP traffic
iperf3 -c 127.0.0.1 -p 5201 -t 30Check if eBPF programs are loaded:
sudo bpftool prog list | grep tcpView kernel logs:
sudo dmesg | tail -10- fentry/tcp_retransmit_skb: Records packet identification information before retransmission
- TC (Traffic Control): Intercepts packets and makes drop/pass decisions (TC_ACT_OK vs TC_ACT_SHOT)
- fexit/tcp_retransmit_skb: Captures the actual return value from the kernel function
- Entry Hook: When
tcp_retransmit_skbis called, the fentry hook captures packet identifiers - TC Processing: Traffic Control layer searches for the identified packet and decides whether to drop it
- Exit Hook: The fexit hook retrieves the return value from
tcp_retransmit_skb - Event Correlation: Ring buffer events are sent to userspace for analysis
- Result Analysis: Go application correlates the TC decision with the actual kernel return code
The program reveals the relationship between:
- TC Drop Actions: When TC returns
TC_ACT_SHOT(drop) vsTC_ACT_OK(pass) - Kernel Return Codes: The actual return value from
tcp_retransmit_skb - Retransmission Outcomes: Understanding how TC drops affect the retransmission process
- Low overhead monitoring: eBPF hooks operate with minimal performance impact
- Efficient event correlation: Ring buffers enable fast fentry/fexit event matching
- Real-time analysis: Immediate correlation of TC decisions with kernel return codes
- Selective targeting: Focuses only on retransmission events, reducing noise
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
Permission Denied
# Ensure you're running with root privileges
sudo ./rebpfeBPF Program Load Failed
# Check kernel version and eBPF support
uname -r
ls /sys/kernel/debug/tracing/events/syscalls/No Packets Captured
# Verify network activity and filter configuration
sudo netstat -tuln | grep 5201- Built with cilium/ebpf Go library
- Inspired by modern network observability and kernel analysis tools
- Thanks to the eBPF community for excellent documentation and examples
The eBPF program rebpf.bpf.c also contains an optional redirect_to_loopback TC program that can be used to redirect matching packets to the loopback interface. This is useful for forcing selected packets back onto lo for inspection (for example, with tcpdump) without modifying the application or network namespace configuration.
Key details:
redirect_to_loopbackruns in thetc(Traffic Control) context and rewrites the destination MAC/address as needed for the loopback device, then usesbpf_redirect()to send the packet to the target interface index.- The TC program updates the destination MAC on the packet (so the kernel will accept/route it on the loopback device) and issues
bpf_redirect(ifindex, 0)to redirect the skb to the interface with the specified ifindex. - To discover the numeric ifindex of the destination (loopback) interface you can use the small helper program
get_ifindex.cincluded in the repository; it prints the ifindex for a given interface name.
How to use it:
- Build and load the eBPF objects as usual (
make generate) and start the Go userspace program. - In userspace (
internal/probe/probe.go), attach theRedirectToLoopbackprogram instead ofDropRetransmitif you want to enable redirection. Concretely, replace the DropRetransmit attach call with the RedirectToLoopback attach (the generated Go objects expose the TC program name). For example:
// attach RedirectToLoopback TC program instead of DropRetransmit
redirectLink, err := link.AttachTC(link.TCOptions{ /* attach options to clsact/egress or ingress */ Program: probe.bpfObjects.RedirectToLoopback })
if err != nil {
log.Printf("failed to attach RedirectToLoopback: %v", err)
return err
}
probe.redirectLink = redirectLink- Once the program is attached and the redirect is enabled, you can observe redirected packets on the loopback interface with
tcpdump:
sudo tcpdump -i lo -n port 5201Notes and caveats:
- Redirecting packets to
lomay require adjusting the packet's MAC and/or network headers depending on kernel expectations;redirect_to_loopbackhandles the MAC rewrite before callingbpf_redirect(). - Attaching
RedirectToLoopbackis an alternative to dropping packets at TC time β it allows you to inspect the exact packets that would have been dropped by redirecting them to the loopback interface instead.
MIT