create_socket

Function create_socket 

Source
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

  1. Creates a raw AF_XDP socket.
  2. Calls setup_umem to create a memory-mapped UMEM region.
  3. Sets the sizes for the Fill, Completion, TX, and RX rings via setsockopt.
  4. Retrieves the memory map offsets for the rings from the kernel.
  5. Memory-maps the required rings based on the specified Direction.
  6. Binds the socket to the given interface index and queue ID, enabling zero-copy and need-wakeup flags based on the config.
  7. Wraps the components in TxSocket and/or RxSocket and 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, or Both).
  • 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.