XDP入门--eBPF程序如何转发报文到其它网卡

1、测试环境:

参照把树莓派改造成无线网卡(3)-----共享无线网络,无线网络转换成有线网络,让有线网络设备连上无线网络一文,形成如下测试网络:

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

在这里插入图片描述

2、实现的功能,使用bpf_redirect直接转发收到的报文到另外一张网卡

在测试环境中,我们让

  • eth1把收到的IPv4的目标IP地址为10.0.0.10的直接从eth0转发出去
  • eth0把收到的IPv4的目标IP地址为10.0.0.4的直接从eth1转发出去
  • 其它报文上送内核TCP/IP协议栈处理

代码如下:

#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";

如何获取: 网卡的index,可以用gcc编译如下程序,运行后得到eth0和eth1的网卡index:

#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、测试步骤与测试结果

在没有加载XDP BPF程度时,从10.0.0.4往10.0.0.10用iperf3打流,数量流量约为65Mbps:
在这里插入图片描述
运行以下命令,在eth0和eth1都加载刚才编程的XDP BPF程序,两次用iperf3测试,发现数据流量约为70Mbps:

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

在这里插入图片描述

我们发现走XDP直接转发,比走Linux网桥的二层转发,性能还是稍有提高。

注意,系统重启后,eth0和eth1的网卡index是会变化的,需要每次重启后重新获取。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值