$npx -y skills add mohitmishra786/low-level-dev-skills --skill af-xdpAF_XDP skill for high-performance XDP sockets. Use when creating AF_XDP sockets, configuring UMEM and XSK rings, XDP_REDIRECT programs, copy vs zero-copy mode, or comparing with DPDK. Activates on queries about AF_XDP, xsk_umem, XDP_REDIRECT, libbpf xsk, or zero-copy XDP.
| 1 | # AF_XDP |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide agents through AF_XDP sockets for high-performance packet I/O: socket creation, UMEM setup, fill/completion/RX/TX rings, XDP programs with `XDP_REDIRECT` to XSK, copy vs zero-copy modes, libbpf helpers, performance comparison with DPDK, and production use cases. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Building a userspace packet processor with lower overhead than raw sockets |
| 10 | - Redirecting XDP-filtered traffic to userspace without DPDK complexity |
| 11 | - Implementing a custom load balancer or IDS dataplane |
| 12 | - Comparing zero-copy vs copy mode on your NIC/driver |
| 13 | - Integrating with existing libbpf/XDP infrastructure |
| 14 | - Need kernel cooperation (firewall rules) plus userspace processing |
| 15 | |
| 16 | ## Workflow |
| 17 | |
| 18 | ### 1. Architecture overview |
| 19 | |
| 20 | ``` |
| 21 | NIC → XDP program (BPF) → XDP_REDIRECT → AF_XDP socket → userspace |
| 22 | ↓ |
| 23 | XDP_DROP/PASS/TX |
| 24 | ``` |
| 25 | |
| 26 | Components: |
| 27 | - **UMEM** — shared memory region for frames |
| 28 | - **Fill ring** — userspace provides empty frame addresses to kernel |
| 29 | - **Completion ring** — kernel returns completed TX frames |
| 30 | - **RX ring** — kernel delivers received packets |
| 31 | - **TX ring** — userspace submits packets for transmission |
| 32 | |
| 33 | ### 2. UMEM and XSK socket creation |
| 34 | |
| 35 | ```c |
| 36 | #include <bpf/xsk.h> |
| 37 | #include <bpf/libbpf.h> |
| 38 | #include <xdp/xsk.h> |
| 39 | |
| 40 | #define NUM_FRAMES 4096 |
| 41 | #define FRAME_SIZE XSK_UMEM__DEFAULT_FRAME_SIZE |
| 42 | #define RX_BATCH_SIZE 64 |
| 43 | |
| 44 | struct xsk_umem_info { |
| 45 | struct xsk_ring_prod fill; |
| 46 | struct xsk_ring_cons comp; |
| 47 | struct xsk_umem *umem; |
| 48 | void *buffer; |
| 49 | }; |
| 50 | |
| 51 | struct xsk_socket_info { |
| 52 | struct xsk_ring_cons rx; |
| 53 | struct xsk_ring_prod tx; |
| 54 | struct xsk_socket *xsk; |
| 55 | }; |
| 56 | |
| 57 | int xsk_setup(struct xsk_umem_info *umem_info, |
| 58 | struct xsk_socket_info *xsk_info, |
| 59 | int ifindex, int queue_id, int xsk_flags) |
| 60 | { |
| 61 | umem_info->buffer = mmap(NULL, NUM_FRAMES * FRAME_SIZE, |
| 62 | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 63 | |
| 64 | struct xsk_umem_config umem_cfg = { |
| 65 | .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS, |
| 66 | .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS, |
| 67 | .frame_size = FRAME_SIZE, |
| 68 | .frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM, |
| 69 | .flags = 0, |
| 70 | }; |
| 71 | |
| 72 | int ret = xsk_umem__create(&umem_info->umem, umem_info->buffer, |
| 73 | NUM_FRAMES * FRAME_SIZE, &umem_info->fill, &umem_info->comp, |
| 74 | &umem_cfg); |
| 75 | if (ret) |
| 76 | return ret; |
| 77 | |
| 78 | struct xsk_socket_config xsk_cfg = { |
| 79 | .rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS, |
| 80 | .tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS, |
| 81 | .libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD, |
| 82 | .xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST, |
| 83 | .bind_flags = xsk_flags, // XDP_ZEROCOPY or XDP_COPY |
| 84 | }; |
| 85 | |
| 86 | return xsk_socket__create(&xsk_info->xsk, "eth0", queue_id, |
| 87 | umem_info->umem, &xsk_info->rx, &xsk_info->tx, &xsk_cfg); |
| 88 | } |
| 89 | ``` |
| 90 | |
| 91 | ### 3. Populate fill ring |
| 92 | |
| 93 | ```c |
| 94 | void populate_fill_ring(struct xsk_umem_info *umem) { |
| 95 | uint32_t idx; |
| 96 | uint32_t ret = xsk_ring_prod__reserve(&umem->fill, RX_BATCH_SIZE, &idx); |
| 97 | for (uint32_t i = 0; i < ret; i++) |
| 98 | *xsk_ring_prod__fill_addr(&umem->fill, idx + i) = i * FRAME_SIZE; |
| 99 | xsk_ring_prod__submit(&umem->fill, ret); |
| 100 | } |
| 101 | ``` |
| 102 | |
| 103 | Must keep fill ring stocked — kernel drops packets if no buffers available. |
| 104 | |
| 105 | ### 4. XDP redirect program |
| 106 | |
| 107 | ```c |
| 108 | // xdp_redirect.c |
| 109 | #include <linux/bpf.h> |
| 110 | #include <bpf/bpf_helpers.h> |
| 111 | |
| 112 | struct { |
| 113 | __uint(type, BPF_MAP_TYPE_XSKMAP); |
| 114 | __uint(max_entries, 64); |
| 115 | __type(key, int); |
| 116 | __type(value, int); |
| 117 | } xsks_map SEC(".maps"); |
| 118 | |
| 119 | SEC("xdp") |
| 120 | int xdp_redirect_prog(struct xdp_md *ctx) |
| 121 | { |
| 122 | int index = ctx->rx_queue_index; |
| 123 | return bpf_redirect_map(&xsks_map, index, 0); |
| 124 | } |
| 125 | ``` |
| 126 | |
| 127 | ```bash |
| 128 | # Load and attach |
| 129 | bpftool prog load xdp_redirect.o /sys/fs/bpf/xdp_redirect |
| 130 | ip link set dev eth0 xdp obj xdp_redirect.o sec xdp |
| 131 | |
| 132 | # Pin xsks_map and update with socket fd |
| 133 | ``` |
| 134 | |
| 135 | ### 5. RX processing loop |
| 136 | |
| 137 | ```c |
| 138 | while (running) { |
| 139 | uint32_t idx_rx = 0, rcvd; |
| 140 | rcvd = xsk_ring_cons__peek(&xsk_info->rx, RX_BATCH_SIZE, &idx_rx); |
| 141 | if (!rcvd) |
| 142 | continue; |
| 143 | |
| 144 | for (uint32_t i = 0; i < rcvd; i++) { |
| 145 | const struct xdp_desc *desc = xsk_ring_cons__rx_desc(&xsk_info->rx, idx_rx + i); |
| 146 | uint64_t addr = desc->addr; |
| 147 | uint32_t len = desc->len; |
| 148 | uint8_t *pkt = (uint8_t *)xsk_umem__get_data(umem_info->buffer, addr); |
| 149 | process_packet(pkt, len); |
| 150 | } |
| 151 | xsk_ring_cons__release(&xsk_info->rx, rcvd); |
| 152 | |
| 153 | // Return frames to fill ring |
| 154 | refill_fill_ring(umem_info, rcvd); |
| 155 | } |
| 156 | ``` |
| 157 | |
| 158 | ### 6. Copy vs zero-copy |
| 159 | |
| 160 | | Mode | Flag | Requirements | |
| 161 | |------|------|--------------| |
| 162 | | Copy | `XDP_COPY` (default) | Any driver; kernel copies to UMEM | |
| 163 | | Zero-copy | `XDP_ZEROCOPY` | Driver support (i40e, ixgbe, mlx5, |