Getting started with XDP--hello world of eBPF

Through the previous XDP, traffic control/tc/qdisc and netfilter in Linux network architecture (packet flow in Netfilter and General Network), we already know that XDP (eXpress Data Path) is a set of fast data processing framework corresponding to DPDK, which is A high-performance, programmable network packet processing framework is provided in the Linux Kernel. It enables Kernel to perform targeted high-speed processing of data packets when they reach L2 (network card driver layer), without the need to "follow the rules" and enter the TCP/IP protocol stack of the Linux kernel for processing.

1. The following two pictures can very well illustrate the position of XDP in the network data processing architecture of the Linux kernel.

insert image description here
insert image description here

2. XDP provides a programmable and flexible processing method. The XDP program can specify the subsequent processing method of the driver program for the message through the XDP action code:

  1. XDP_ABORTED:
    Discard packets. The difference from XDP_DROP is that XDP_ABORTED will use trace_xdp_exception to record error behavior.
  2. XDP_DROP:
    The packet is directly discarded at the network card driver layer, and the data packet will no longer be sent to the kernel TCP/IP protocol stack for processing.
  3. The XDP_PASS
    message continues to be sent to the kernel TCP/IP protocol stack for processing, and the processing method at this time is consistent with the traditional method.
  4. XDP_TX:
    Send the message from the same network card that received the message
  5. XDP_REDIRECT:
    Redirect packets to other network cards or CPUs. Combined with AF_XDP, packets can be sent directly to user space, and the application level can directly take over packets, similar to DPDK.

3. An example of directly discarding received packets in XDP

This example is verified on the Raspberry Pi system.

3.1, install clang

sudo apt install clang

3.2, write XDP program

Every second message is discarded, and the remaining message is sent to the kernel protocol stack for processing.

// file: xdp-helloworld.c

#include <linux/bpf.h>

#ifndef __section
# define __section(NAME)                  \
   __attribute__((section(NAME), used))
#endif



__section("prog")
int xdp_drop(struct xdp_md *ctx)
{
    
    
    static int example_count = 1;

    example_count++;

    if (example_count%2)
    {
    
    
        return XDP_DROP;
    }
    else
    {
    
    
        return XDP_PASS;
    }
}

char __license[] __section("license") = "GPL";


3.3. Set up the compilation environment and compile the XDP level

Note that depending on whether your environment is arm architecture or X86 architecture, you need to soft link the asm directory under /usr/include/xxxx/asm different xxxx to the /usr/include/asm directory.

cd /usr/include/

sudo ln -s ./arm-linux-gnueabihf/asm asm
sudo ln -s ./arm-linux-gnueabihf/bits/ bits

clang -O2 -Wall -target bpf -c xdp-helloworld.c -o xdp-helloworld.o

3.4. Load XDP program and verify

Our test environment is as follows:

                                                     +- RPi -------+          +- old pc1----+
                                                     |         Eth0+----------+ Eth0        |    
                 +- Router ----+                     |  DHCP server|          | 10.0.0.10   |
                 | Firewall    |                     |   10.0.0.1  |          |             |
(Internet)---WAN-+ DHCP server +-WLAN AP-+-)))   (((-+ WLAN        |          +-------------+
                 | 192.168.3.1 |                     |             |          
                 +-------------+                     |             |          +- old pc2----+
                                                     |         Eth1+----------+ Eth0        |   
                                                     |             |          | 10.0.0.4    |                                                       
                                                     +-------------+          |             |
                                                                              +-------------+

We load the XDP program in Eth1 of RPi, and ping old pc1(10.0.0.4) from old pc2(10.0.0.2). After the above-mentioned program of XDP is successfully loaded, eth1 of RPi will receive the packets at intervals of one Just drop one. We observed the ping icmp message on the old pc2, and used wireshark to capture the packet. We found that there was no response received for every interval. It indicates that Eth1 of RPi has discarded received packets at intervals.

sudo ip link set dev eth1 xdp obj xdp-helloworld.o

insert image description here

3.5. Uninstall the XDP program

After uninstalling the above programs of XDP, you will find that the ping returns to normal. It means that eth1 of RPi resumes normal function.

sudo ip link set dev eth1 xdp off

Guess you like

Origin blog.csdn.net/meihualing/article/details/130781598