pub fn create_socket(
if_index: u32,
if_queue: u32,
direction: Direction,
config: Option<XdpConfig>,
) -> Result<(Option<TxSocket>, Option<RxSocket>), Error>Expand description
Creates one or two sockets for AF_XDP packet processing.
This is the core function for setting up AF_XDP sockets. It handles UMEM allocation, ring configuration, and binding to a network interface queue.
§How it works
- Creates a raw
AF_XDPsocket. - Calls
setup_umemto create a memory-mapped UMEM region. - Sets the sizes for the Fill, Completion, TX, and RX rings via
setsockopt. - Retrieves the memory map offsets for the rings from the kernel.
- Memory-maps the required rings based on the specified
Direction. - Binds the socket to the given interface index and queue ID, enabling zero-copy and need-wakeup flags based on the config.
- Wraps the components in
TxSocketand/orRxSocketand returns them.
§Arguments
if_index- The index of the network interface to bind to.if_queue- The queue index of the interface to bind to.direction- The desired direction(s) for the socket (Tx,Rx, orBoth).config- Optional configuration for zero-copy, huge pages, etc.
§Returns
A tuple (Option<TxSocket>, Option<RxSocket>). The appropriate socket(s) will be
Some based on the direction.
§Safety
This function is unsafe because it directly interfaces with low-level Linux APIs. The caller must ensure the provided parameters are valid.