Expand description
§AF_XDP Socket Creation and Configuration
§Purpose
This file contains the logic for creating and configuring AF_XDP sockets. It provides a high-level API to set up sockets for transmit-only, receive-only, or bidirectional packet processing, abstracting away many of the low-level details.
§How it works
It uses libc syscalls to create a raw AF_XDP socket. It then allocates a UMEM
(Userspace Memory) region for zero-copy data transfers, configures the necessary
rings (TX, RX, Fill, Completion) with appropriate sizes, maps them into memory,
and binds the socket to a specific network interface and queue. The logic handles
different UMEM and ring configurations based on whether the socket is for TX, RX,
or both.
§Main components
create_socket(): The core unsafe function that handles the detailed setup logic.create_tx_socket(),create_rx_socket(),create_bi_socket(): Safe public functions that wrapcreate_socketfor specific use cases.setup_umem(): A helper function to allocate and register the UMEM with the kernel.ring_offsets(): A helper to query the kernel for the memory map offsets of the rings.XdpConfig,Direction: Public structs and enums for socket configuration.
Structs§
- XdpConfig
- Configuration options for creating an AF_XDP socket.
Enums§
- Direction
- Specifies the direction of an AF_XDP socket.
Functions§
- create_
bi_ socket - Creates a pair of sockets (
TxSocket,RxSocket) for bidirectional communication. - create_
rx_ socket - Creates an
RxSocketfor receiving packets. - create_
socket - Creates one or two sockets for AF_XDP packet processing.
- create_
tx_ socket - Creates a
TxSocketfor sending packets. - ring_
offsets - Retrieves the memory map offsets for the AF_XDP rings from the kernel.
- setup_
umem - Allocates and registers the UMEM (Userspace Memory) region with the kernel.