Overview
The UDP tracer attaches to multiple kernel functions to track UDP activity:udp_sendmsg/udpv6_sendmsg- Capture outbound UDP trafficudp_recvmsg/udpv6_recvmsg- Capture inbound UDP trafficip4_datagram_connect/ip6_datagram_connect- Track socket connectionsudp_destruct_sock- Capture socket destruction events
Event Structure
Each UDP event contains the following fields:Initializing the UDP Tracer
1
Load the shared library
Load the UDP event interceptor library:
2
Resolve the AddProbe function
Get the
AddProbe function:Unlike TCP monitoring, UDP’s
AddProbe() takes no arguments - the BPF program is embedded in the library.3
Resolve the DequeuePerfEvent function
Get the event dequeue function:
4
Resolve additional functions
Get status checking and cleanup functions:
5
Attach the BPF probes
Call This internally attaches kprobes to:
AddProbe() to attach all UDP monitoring probes:ip6_datagram_connectip4_datagram_connectudp_recvmsg(entry and return)udp_sendmsgudp_destruct_sockudpv6_recvmsg(entry and return)udpv6_sendmsg
6
Wait for initialization
Wait for all probes to attach:
Complete Monitoring Example
Here’s a complete example based on the test implementation:IPv4 vs IPv6 Handling
The UDP tracer seamlessly handles both IPv4 and IPv6 traffic through separate probe points:IPv4 Probes
IPv6 Probes
Packet vs Byte Counting
The UDP tracer provides both packet counts and byte counts:Byte Counting
- rx_b: Total bytes received across all UDP packets
- tx_b: Total bytes sent across all UDP packets
Packet Counting
- rxPkts: Number of UDP packets received
- txPkts: Number of UDP packets sent
Byte counts represent application-level payload data, not including UDP/IP headers.
Understanding Event Aggregation
UDP events are aggregated per socket. The tracer maintains state using a BPF hash map:- When
udp_sendmsgis called - When
udp_recvmsgcompletes - When the socket is destroyed (
udp_destruct_sock)
Interpreting Event Data
Process Information
- pid: Process ID that owns the socket
- UserId: User ID of the process (from
bpf_get_current_uid_gid()) - task: Process name (up to 16 characters)
Connection Endpoints
- SADDR/SPT: Source IP address and port
- DADDR/DPT: Destination IP address and port
- family: Address family (2 = IPv4, 10 = IPv6)
Timestamps
- EventTime: Nanosecond timestamp adjusted for system boot time
Example Output
When running the UDP tracer, you’ll see output like this:Event Queue Management
The tracer maintains an internal event queue with a maximum size:Cleanup and Shutdown
1
Setup signal handler
Register handlers for graceful shutdown:
2
Call cleanup function
The cleanup function detaches all kprobes:This detaches probes from:
ip6_datagram_connectip4_datagram_connectudp_recvmsgudp_sendmsgudp_destruct_sockudpv6_sendmsgudpv6_recvmsgkretprobe__udpv6_recvmsg
3
Close library handle
Close the dynamic library:
Best Practices
High-Frequency Events
UDP can generate events at very high rates. Ensure your processing loop is efficient.
Event Aggregation
Events are per-socket aggregates. Don’t assume one event per packet.
Signal Handling
Always implement signal handlers to ensure proper cleanup.
Root Privileges
eBPF requires root or CAP_BPF capabilities to load probes.
Troubleshooting
Library not found
Library not found
Verify the library is installed:If not found, reinstall or update the
SOFILE path.Permission denied
Permission denied
Run with elevated privileges:
No events appearing
No events appearing
- Verify probes attached successfully (check console output)
- Generate UDP traffic:
dig example.comorping6 ipv6.google.com - Check kernel logs:
sudo dmesg | tail
Events being shed
Events being shed
If you see “Shedding UDP events” messages:
- Process events faster in your main loop
- Consider filtering events at the BPF level
- Increase
MAXQSIZEand recompile if needed
Missing IPv6 events
Missing IPv6 events
Ensure IPv6 is enabled:Should return 0 (enabled).
Advanced Usage
Filtering Specific Ports
Modify the BPF program to filter events by port:Custom Event Processing
Process events based on traffic patterns:Next Steps
TCP Monitoring
Learn how to monitor TCP connections
Building from Source
Customize and build the interceptor
Testing
Run tests and verify functionality
API Reference
Detailed UDP API documentation