Function Overview
The UDP Event API exports four C-compatible functions for UDP traffic monitoring:AddProbe()- Initialize and start UDP event tracingsetupBPF()- Internal BPF initialization (called by AddProbe)DequeuePerfEvent()- Retrieve UDP events from the queuegetStatus()- Check BPF initialization statuscleanup()- Detach probes and cleanup resources
AddProbe
Initializes the UDP event tracer and begins monitoring all UDP traffic system-wide.Signature
Parameters
Unlike the TCP Event API, the UDP version of
AddProbe() takes no parameters. The UDP tracer monitors all UDP traffic without filtering.Return Value
- Type:
void - This function does not return a value
Behavior
- Prints version information to stdout:
- Spawns a detached background thread that calls
setupBPF() - Returns immediately (non-blocking)
- The background thread continues running until
cleanup()is called
Threading
- Non-blocking: Returns immediately after spawning the background thread
- Detached thread: The spawned thread runs independently
- Thread ID: Stored internally for cleanup purposes
Example Usage
Notes
- Requires root privileges to attach kernel probes
- Should only be called once per application instance
- Use
getStatus()to verify initialization completed successfully
Source Reference
udpTracer.cc:388-392
setupBPF
Internal function that performs the actual BPF program initialization and kernel probe attachment.Signature
Parameters
None.Return Value
0- Success1- Error during initialization or probe attachment
Behavior
This function performs the following operations:- Initializes the BPF program with the embedded eBPF code
- Attaches kernel probes to the following functions:
ip6_datagram_connect- IPv6 UDP connectionip4_datagram_connect- IPv4 UDP connectionudp_recvmsg- UDP message reception (kprobe and kretprobe)udp_sendmsg- UDP message transmissionudp_destruct_sock- UDP socket destructionudpv6_sendmsg- IPv6 UDP message transmissionudpv6_recvmsg- IPv6 UDP message reception (kprobe and kretprobe)
- Opens the perf buffer for event communication
- Frees BCC compiler memory
- Increments the status flag
- Enters an infinite polling loop to receive events
Threading
- Blocking: Runs in an infinite loop polling for events
- Called by the background thread spawned by
AddProbe() - Does not return under normal operation
Notes
- This is an internal function typically not called directly by applications
- Errors are printed to stderr with descriptive messages
- The function stores the thread ID for cleanup purposes
- All attached probe names are stored in
fNamesVectorfor later detachment
Source Reference
udpTracer.cc:394-500
DequeuePerfEvent
Retrieves the next UDP event from the event queue. This is the primary function for consuming UDP events.Signature
Parameters
None.Return Value
A complete UDP event structure containing connection details, traffic statistics, and process information.
Behavior
- Blocking: If the queue is empty, the function blocks until an event becomes available
- Thread-safe: Uses mutex and condition variable for synchronization
- FIFO order: Events are returned in the order they were captured
- Event conversion: Converts internal
event_tstructure to consumer-facingudp_event_t
Time Adjustment
The function adjusts event timestamps to absolute system time:notSoLongAgo is calculated from system boot time to convert kernel time to epoch time.
Address Conversion
IP addresses are converted from binary to string format:- IPv4: Uses
inet_ntop(AF_INET, ...) - IPv6: Uses
inet_ntop(AF_INET6, ...)
Threading
- Blocking call: Suspends the calling thread until an event is available
- Condition variable: Uses pthread condition variable for efficient waiting
- Safe to call from multiple threads (though typically called from a single consumer thread)
Example Usage
Notes
- Events with unknown address families are skipped (error printed to stderr)
- The returned structure is a copy; modifications do not affect internal state
- Task name is limited to 16 characters (including null terminator)
Source Reference
udpTracer.cc:530-576
getStatus
Returns the initialization status of the BPF subsystem.Signature
Parameters
None.Return Value
0- BPF initialization in progress or not started1- BPF initialized successfully and ready to capture events
Behavior
- Thread-safe: Uses read-write lock for safe concurrent access
- Non-blocking: Returns immediately with current status
- Status is incremented to 1 when
setupBPF()completes initialization
Threading
- Safe to call from any thread
- Uses
pthread_rwlock_rdlock()for thread-safe read access
Example Usage
Notes
- Useful for startup synchronization
- Does not indicate if errors occurred during initialization
- Check stderr output for initialization errors
Source Reference
udpTracer.cc:502-507
cleanup
Detaches all kernel probes and releases resources. Should be called before application exit.Signature
Parameters
None.Return Value
- Type:
void - This function does not return a value
Behavior
- Prints “Cleaning up!” to stdout
- Iterates through all attached probes and detaches them
- Prints detachment status for each probe (success or error)
- Cancels the background polling thread if it exists
- Prints “bpf.detach_kprobe OK” confirmation
Probes Detached
The following kernel probes are detached:ip6_datagram_connectip4_datagram_connectudp_recvmsgudp_sendmsgudp_destruct_sockudpv6_sendmsgudpv6_recvmsgkretprobe__udpv6_recvmsg
Threading
- Cancels the background thread created by
AddProbe() - Uses
pthread_cancel()to terminate the polling loop
Example Usage
Notes
- Should be called before application exit to properly release kernel resources
- After calling
cleanup(), do not callDequeuePerfEvent()orgetStatus() - Detachment errors are reported to stderr but do not prevent cleanup from continuing
- Thread cancellation happens after all probes are detached
Source Reference
udpTracer.cc:606-623