Introdução ao XDP - como o programa eBPF encaminha pacotes para outras placas de rede

1. Ambiente de teste:

Consulte o artigo Transform Raspberry Pi em uma placa de rede sem fio (3) ----- compartilhe a rede sem fio, converta a rede sem fio em uma rede com fio e deixe o dispositivo de rede com fio se conectar à rede sem fio para formar o seguinte teste rede:

                                                     +- 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    |                                                       
                                                     +-------------+          |             |
                                                                              +-------------+

insira a descrição da imagem aqui

2. A função implementada usa bpf_redirect para encaminhar diretamente a mensagem recebida para outra placa de rede

No ambiente de teste, deixamos

  • eth1 encaminha o endereço IP de destino IPv4 recebido 10.0.0.10 diretamente de eth0
  • eth0 encaminha o endereço IP de destino IPv4 recebido 10.0.0.4 diretamente de eth1
  • Outras mensagens são enviadas para a pilha de protocolo TCP/IP do kernel para processamento

código mostra como abaixo:

#include <stdio.h>
#include <linux/bpf.h>
#include <net/ethernet.h>
#include <linux/if_vlan.h>
#include <netinet/in.h>
#include <linux/ip.h>
#include <bpf/bpf_helpers.h>
#include <net/if.h>

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

__section("prog")
int xdp_ip_filter(struct xdp_md *ctx)
{
    
    
    void *end = (void *)(long)ctx->data_end;
    void *data = (void *)(long)ctx->data;
    int ip_src;
    int ip_dst;
    long int offset;
    short int eth_type;
    char info_fmt1[] = "Dst Addr: xx.xx.%d.%d";
    char info_fmt2[] = "Src Addr: xx.xx.%d.%d";
    char info_fmt3[] = "------------------";

    static int i = 0;
    static int j = 0;
    unsigned char *saddrpoint = 0;
    unsigned char *daddrpoint = 0;

    struct ethhdr *eth = data;
    offset = sizeof(*eth);

    unsigned int indexofeth0 = 3;
    unsigned int indexofeth1 = 4;


    if (data + offset > end) {
    
    
    return XDP_ABORTED;
    }
    eth_type = eth->h_proto;
   
    /* 这里其实是有缺陷的,直接把收到的所有报文都当做不带VLAN的IPV4报文去解析,但因为测试环境简单,里面的报文也简单,所以这个处理基本上也问题不大 */

    struct iphdr *iph = data + offset;
    offset += sizeof(struct iphdr);
    /*在读取之前,确保你要读取的子节在数据包的长度范围内  */
    if (iph + 1 > end) {
    
    
        return XDP_ABORTED;
    }
    /*ip_src = iph->saddr;*/
    ip_dst = iph->daddr;

    /*saddrpoint = (unsigned char*)(&ip_src);
    daddrpoint =(unsigned char*)(&ip_dst);*/
    
    /* 发往10.0.0.10的直接通过eth0发送出去 */
    if(ip_dst == 0xa00000a)
    {
    
    
        return bpf_redirect(indexofeth0,0);
    }

    /* 发往10.0.0.4的直接通过eth1发送出去 */
    if(ip_dst == 0x400000a)
    {
    
    
        return bpf_redirect(indexofeth1,0);
    }
    /* 其它报文上送内核协议栈处理 */
    return XDP_PASS;
}

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

Como obter: O índice da placa de rede, você pode compilar o seguinte programa com gcc e obter o índice da placa de rede de eth0 e eth1 após a execução:

#include <stdio.h>
#include <linux/bpf.h>
#include <net/ethernet.h>
#include <linux/if_vlan.h>
#include <netinet/in.h>
#include <linux/ip.h>
#include <bpf/bpf_helpers.h>
#include <net/if.h>

int main(struct xdp_md *ctx)
{
    
    
   unsigned int indexofeth0 = 0;
   unsigned int indexofeth1 = 0;
   
   indexofeth0 = if_nametoindex("eth0");
   indexofeth1 = if_nametoindex("eth1");

   printf("%d, %d", indexofeth0, indexofeth1);
   return 1;
}

3. Etapas do teste e resultados do teste

Quando o nível XDP BPF não estiver carregado, use iperf3 para transmitir de 10.0.0.4 a 10.0.0.10, e a quantidade de tráfego é de cerca de 65 Mbps: Execute o seguinte
insira a descrição da imagem aqui
comando para carregar o programa XDP BPF recém-programado em eth0 e eth1 e teste duas vezes com iperf3, verifica-se que o tráfego de dados é de cerca de 70 Mbps:

clang -O2 -Wall -target bpf -c xdp-example.c -o xdp-example.o
sudo ip link set dev eth0 xdp obj xdp-example.o
sudo ip link set dev eth1 xdp obj xdp-example.o

insira a descrição da imagem aqui

Descobrimos que o encaminhamento direto do XDP tem um desempenho um pouco melhor do que o encaminhamento da camada 2 da ponte do Linux.

Observe que após a reinicialização do sistema, os índices NIC de eth0 e eth1 serão alterados e precisam ser obtidos novamente após cada reinicialização.

Acho que você gosta

Origin blog.csdn.net/meihualing/article/details/130815688
Recomendado
Clasificación