|
| 1 | +.. _kernel_xdp: |
| 2 | + |
| 3 | +=== |
| 4 | +XDP |
| 5 | +=== |
| 6 | + |
| 7 | +.. contents:: :local: |
| 8 | + :depth: 3 |
| 9 | + |
| 10 | +Introduction |
| 11 | +============ |
| 12 | + |
| 13 | +XDP (eXpress Data Path) provides a framework for BPF that enables high-performance programmable packet processing in the Linux kernel. It runs the BPF program at the earliest possible point in software, namely at the moment the network driver receives the packet. |
| 14 | + |
| 15 | +XDP allows running a BPF program just before the skbs are allocated in the driver, the BPF program can look at the packet and return the following things. |
| 16 | + |
| 17 | +- XDP_DROP :- The packet is dropped right away, without wasting any resources. Useful for firewall etc. |
| 18 | +- XDP_ABORTED :- Similar to drop, an exception is generated. |
| 19 | +- XDP_PASS :- Pass the packet to kernel stack, i.e. the skbs are allocated and it works normally. |
| 20 | +- XDP_TX :- Send the packet back to same NIC with modification(if done by the program). |
| 21 | +- XDP_REDIRECT :- Send the packet to another NIC or to the user space through AF_XDP Socket(discussed below). |
| 22 | + |
| 23 | +.. Image:: /images/XDP-packet-processing.png |
| 24 | + |
| 25 | +As explained before, the XDP_REDIRECT sends packets directly to the user space. |
| 26 | +This works by using the AF_XDP socket type which was introduced specifically for this usecase. |
| 27 | + |
| 28 | +In this process, the packet is directly sent to the user space without going through the kernel network stack. |
| 29 | + |
| 30 | +.. Image:: /images/xdp-packet.png |
| 31 | + |
| 32 | +Use cases for XDP |
| 33 | +----------------- |
| 34 | + |
| 35 | +XDP is particularly useful for these common networking scenarios: |
| 36 | + |
| 37 | +1. **DDoS Mitigation**: High-speed packet filtering and dropping malicious traffic |
| 38 | +2. **Load Balancing**: Efficient traffic distribution across multiple servers |
| 39 | +3. **Packet Capture**: High-performance network monitoring without performance penalties |
| 40 | +4. **Firewalls**: Wire-speed packet filtering based on flexible rule sets |
| 41 | +5. **Network Analytics**: Real-time traffic analysis and monitoring |
| 42 | +6. **Custom Network Functions**: Specialized packet handling for unique requirements |
| 43 | + |
| 44 | +How to run XDP on EVM |
| 45 | +--------------------- |
| 46 | + |
| 47 | +The kernel configuration requires the following changes to use XDP: |
| 48 | + |
| 49 | +.. code-block:: console |
| 50 | +
|
| 51 | + CONFIG_DEBUG_INFO_BTF=y |
| 52 | + CONFIG_BPF_PRELOAD=y |
| 53 | + CONFIG_BPF_PRELOAD_UMD=y |
| 54 | + CONFIG_BPF_EVENTS=y |
| 55 | + CONFIG_BPF_LSM=y |
| 56 | + CONFIG_DEBUG_INFO_REDUCED=n |
| 57 | + CONFIG_FTRACE=y |
| 58 | + CONFIG_XDP_SOCKETS=y |
| 59 | +
|
| 60 | +Tools for debugging XDP Applications |
| 61 | +------------------------------------- |
| 62 | + |
| 63 | +Debugging tools for XDP development: |
| 64 | + |
| 65 | +- bpftool - For loading and managing BPF programs |
| 66 | +- xdpdump - For capturing XDP packet data |
| 67 | +- perf - For performance monitoring and analysis |
| 68 | +- bpftrace - For tracing BPF program execution |
| 69 | + |
| 70 | +AF_XDP Sockets |
| 71 | +============== |
| 72 | + |
| 73 | +AF_XDP is a socket address family specifically designed to work with the XDP framework. |
| 74 | +These sockets provide a high-performance interface for user space applications to receive |
| 75 | +and transmit network packets directly from the XDP layer, bypassing the traditional kernel networking stack. |
| 76 | + |
| 77 | +Key characteristics of AF_XDP sockets include: |
| 78 | + |
| 79 | +- Direct path from network driver to user space applications |
| 80 | +- Shared memory rings for efficient packet transfer |
| 81 | +- Minimal overhead compared to traditional socket interfaces |
| 82 | +- Optimized for high-throughput, low-latency applications |
| 83 | + |
| 84 | +How AF_XDP Works |
| 85 | +---------------- |
| 86 | + |
| 87 | +AF_XDP sockets operate through a shared memory mechanism: |
| 88 | + |
| 89 | +1. XDP program intercepts packets at driver level |
| 90 | +2. XDP_REDIRECT action sends packets to the socket |
| 91 | +3. Shared memory rings (RX/TX/FILL/COMPLETION) manage packet data |
| 92 | +4. Userspace application directly accesses the packet data |
| 93 | +5. Zero or minimal copying depending on the mode used |
| 94 | + |
| 95 | +The AF_XDP architecture uses several ring buffers: |
| 96 | + |
| 97 | +- **RX Ring**: Received packets ready for consumption |
| 98 | +- **TX Ring**: Packets to be transmitted |
| 99 | +- **FILL Ring**: Pre-allocated buffers for incoming packets |
| 100 | +- **COMPLETION Ring**: Tracks completed TX operations |
| 101 | + |
| 102 | +For more details on AF_XDP please refer to the official documentation: `AF_XDP <https://www.kernel.org/doc/html/latest/networking/af_xdp.html>`_. |
| 103 | + |
| 104 | +XDP Zero-Copy |
| 105 | +============= |
| 106 | + |
| 107 | +Introduction to Zero-Copy Mode |
| 108 | +------------------------------- |
| 109 | + |
| 110 | +Zero-copy mode is an optimization in AF_XDP that eliminates packet data copying between the kernel and user space. This results in significantly improved performance for high-throughput network applications. |
| 111 | + |
| 112 | +How Zero-Copy Works |
| 113 | +------------------- |
| 114 | + |
| 115 | +In standard XDP operation (copy mode), packet data is copied from kernel memory to user space memory when processed. Zero-copy mode eliminates this copy operation by: |
| 116 | + |
| 117 | +1. Using memory-mapped regions shared between the kernel and user space |
| 118 | +2. Allowing direct DMA from network hardware into memory accessible by user space applications |
| 119 | +3. Managing memory ownership through descriptor rings rather than data movement |
| 120 | + |
| 121 | +This approach provides several benefits: |
| 122 | + |
| 123 | +- Reduced CPU utilization |
| 124 | +- Lower memory bandwidth consumption |
| 125 | +- Decreased latency for packet processing |
| 126 | +- Improved overall throughput |
| 127 | + |
| 128 | +Performance Considerations |
| 129 | +-------------------------- |
| 130 | + |
| 131 | +When implementing XDP applications, consider these performance factors: |
| 132 | + |
| 133 | +1. **Memory Alignment**: Buffers should be aligned to page boundaries for optimal performance |
| 134 | +2. **Batch Processing**: Process multiple packets in batches when possible |
| 135 | +3. **Poll Mode**: Use poll() or similar mechanisms to avoid blocking on socket operations |
| 136 | +4. **Core Affinity**: Bind application threads to specific CPU cores to reduce cache contention |
| 137 | +5. **NUMA Awareness**: Consider NUMA topology when allocating memory for packet buffers |
| 138 | + |
| 139 | +Testing XDP on EVM |
| 140 | +================== |
| 141 | + |
| 142 | +The `xdp-tools <https://github.com/xdp-project/xdp-tools>`__ package provides |
| 143 | +utility tools for testing XDP and AF_XDP such as `xdp-bench`, `xdp-trafficgen` etc. |
| 144 | + |
| 145 | +TI SDK packages the latest version of ``xdp-tools`` utilities and provides it as part of the SDK. |
| 146 | +This allows users to easily test XDP functionality on EVM using these tools. |
| 147 | + |
| 148 | +Both CPSW and ICSSG Ethernet drivers supports Native XDP, Generic XDP, and Zero-copy mode. |
| 149 | + |
| 150 | +.. note:: |
| 151 | + |
| 152 | + In case of testing with CPSW please note that when running XDP in Zero-copy mode, non-XDP traffic will be dropped. |
| 153 | + |
| 154 | +**XDP Transmit test** — generate traffic using XDP (copy mode): |
| 155 | + |
| 156 | +.. code-block:: console |
| 157 | +
|
| 158 | + xdp-trafficgen udp -m ff:ff:ff:ff:ff:ff <interface> |
| 159 | +
|
| 160 | +**XDP Drop test** — receive and drop packets using XDP (copy mode): |
| 161 | + |
| 162 | +.. code-block:: console |
| 163 | +
|
| 164 | + xdp-bench xdp-bench drop <interface> |
| 165 | +
|
| 166 | +**XDP Pass test** — receive and pass packets through XDP allowing normal network stack processing (copy mode): |
| 167 | + |
| 168 | +.. code-block:: console |
| 169 | +
|
| 170 | + xdp-bench xdp-bench pass <interface> |
| 171 | +
|
| 172 | +**XDP TX test** — Hairpins (bounces back) received packets on the same interface (copy mode): |
| 173 | + |
| 174 | +.. code-block:: console |
| 175 | +
|
| 176 | + xdp-bench xdp-bench tx <interface> |
| 177 | +
|
| 178 | +**XDP Redirect test** — Redirects received packets on the from one interface to another (copy mode): |
| 179 | + |
| 180 | +.. code-block:: console |
| 181 | +
|
| 182 | + xdp-bench xdp-bench redirect <interface1> <interface2> |
| 183 | +
|
| 184 | +**XSK Drop test** — receive and drop packets using AF_XDP socket in zero-copy mode: |
| 185 | + |
| 186 | +.. code-block:: console |
| 187 | +
|
| 188 | + xdp-bench xsk-drop -q 0 -C zero-copy <interface> |
| 189 | +
|
| 190 | +**XSK Transmit test** — generate traffic using AF_XDP socket in zero-copy mode: |
| 191 | + |
| 192 | +.. code-block:: console |
| 193 | +
|
| 194 | + xdp-trafficgen xsk-udp -m ff:ff:ff:ff:ff:ff -q 0 -C zero-copy <interface>` |
| 195 | +
|
| 196 | +``xdpsock`` is deprecated and replaced by `xsk-trafficgen` and `xsk-bench` in `xdp-tools` package. |
| 197 | +For more details on zero copy testing refer to `xdpsock <https://github.com/xdp-project/bpf-examples/tree/main/AF_XDP-example>`_ |
| 198 | + |
| 199 | +Performance Comparison |
| 200 | +---------------------- |
| 201 | + |
| 202 | +Performance testing shows that zero-copy mode can provide substantial throughput improvements compared to copy mode: |
| 203 | + |
| 204 | +AF_XDP performance while using 64 byte packets for ICSSG (in Kpps): |
| 205 | + |
| 206 | +.. list-table:: |
| 207 | + :header-rows: 1 |
| 208 | + |
| 209 | + * - Benchmark |
| 210 | + - XDP-SKB |
| 211 | + - XDP-Native |
| 212 | + - XDP-Native(ZeroCopy) |
| 213 | + * - rxdrop |
| 214 | + - 253 |
| 215 | + - 473 |
| 216 | + - 656 |
| 217 | + * - txonly |
| 218 | + - 350 |
| 219 | + - 354 |
| 220 | + - 855 |
| 221 | + |
| 222 | +AF_XDP performance while using 64 byte packets for CPSW (in Kpps): |
| 223 | + |
| 224 | +.. list-table:: |
| 225 | + :header-rows: 1 |
| 226 | + |
| 227 | + * - Benchmark |
| 228 | + - XDP-SKB |
| 229 | + - XDP-Native |
| 230 | + - XDP-Native(ZeroCopy) |
| 231 | + * - rxdrop |
| 232 | + - 322 |
| 233 | + - 491 |
| 234 | + - 845 |
| 235 | + * - txonly |
| 236 | + - 390 |
| 237 | + - 394 |
| 238 | + - 723 |
0 commit comments