DPDK程序结合网络助手接收数据

news/2024/5/18 14:41:57 标签: 网络, c语言, udp, linux

网络调试工具:https://download.csdn.net/download/hdsHDS6/88390999?spm=1001.2014.3001.5503

DPDK代码:

#include <stdio.h>
#include <string.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_ip.h>
#include <rte_tcp.h>
#include <rte_udp.h>
#include <rte_mbuf.h>
#include <rte_mempool.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#define NB_MBUF 512
#define MAX_PKT_BURST 32
#define SELF_PROTO_TYPE 0x0888
static struct rte_eth_conf port_conf = {
      .rxmode = {
            .split_hdr_size = 0
      }
};
// cat /proc/interrupts
void init_port(struct rte_mempool *mbuf_pool){
      uint16_t nb_ports = 0;
      int ret = 0;
      int portid = 0;
      struct rte_eth_dev_info dev_info;
      struct rte_ether_addr addr;
      const int num_rx_queues = 1;
      const int num_tx_queues = 0;
      nb_ports = rte_eth_dev_count_avail();
      if(nb_ports == 0){
            rte_exit(EXIT_FAILURE, "No support eth found\n");
      }
      for(portid = 0; portid < nb_ports; portid++){
            ret = rte_eth_macaddr_get(portid, &addr);
            if (ret != 0){
                  rte_exit(EXIT_FAILURE, "macaddr get failed\n");
            } 
            printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
                        " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
                        portid, RTE_ETHER_ADDR_BYTES(&addr));
            ret = rte_eth_dev_info_get(portid, &dev_info);
            ret = rte_eth_dev_configure(portid, num_rx_queues, num_tx_queues, &port_conf);
            ret = rte_eth_rx_queue_setup(portid, 0, 128, rte_eth_dev_socket_id(portid), NULL, mbuf_pool);
            ret = rte_eth_dev_start(portid);
            if (ret < 0) {
                  rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n", ret, portid);
            }
      }   
}
/*
      发送方注意设置LP信息
*/
int
main(int argc, char **argv)
{
      int ret;
	unsigned lcore_id;
      int i = 0;
      int portid = 0;
      int nb_rx = 0;
      /* 初始化环境 */
	ret = rte_eal_init(argc, argv);
	if (ret < 0)
		rte_panic("Cannot init EAL\n");
      /* 创建内存池 */
      struct rte_mempool *mbuf_pool = rte_pktmbuf_pool_create("my pool", NB_MBUF, 32, 0,
			RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
	if (mbuf_pool == NULL){
            return -1;
      }
      init_port(mbuf_pool);
      while(1){
            struct rte_mbuf* pkts_burst[MAX_PKT_BURST];
            nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst, MAX_PKT_BURST);
            if(nb_rx == 0){
                  sleep(1);
                  continue;
            }
            // printf("recv data start : %d \n", nb_rx);
            for(i = 0; i < nb_rx; i++){
                  // ether
                  struct rte_ether_hdr *hdr = rte_pktmbuf_mtod(pkts_burst[i], struct rte_ether_hdr *);
                  // ip
                  // printf("ether_type = %x \n", hdr->ether_type);
                  if(hdr->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)){
                        struct rte_ipv4_hdr *iphdr =  rte_pktmbuf_mtod_offset(pkts_burst[i], struct rte_ipv4_hdr *, sizeof(struct rte_ether_hdr));
                        // upd
                        if(iphdr->next_proto_id == IPPROTO_UDP){
                              // (struct rte_udp_hdr *)RTE_PTR_ADD(iphdr, sizeof(struct rte_ipv4_hdr));
                              struct rte_udp_hdr* udphdr = (struct rte_udp_hdr*)(iphdr + 1);
                              uint16_t length = ntohs(udphdr->dgram_len);
                              *(char*)(udphdr + length) = '\0';
                              struct in_addr addr;
                              addr.s_addr = iphdr->src_addr;
                              printf("src:%s:%d \n", inet_ntoa(addr), ntohs(udphdr->src_port));
                              addr.s_addr = iphdr->dst_addr;
                              printf("dst:%s:%d, %s \n", inet_ntoa(addr), ntohs(udphdr->dst_port), (char*)(udphdr+1));
                        }else if(iphdr->next_proto_id == IPPROTO_TCP){
                              struct rte_tcp_hdr* tcphdr = (struct rte_tcp_hdr*)(iphdr + 1);
                              struct in_addr addr;
                              addr.s_addr = iphdr->src_addr;
                              printf("src:%s:%d \n", inet_ntoa(addr), ntohs(tcphdr->src_port));
                              addr.s_addr = iphdr->dst_addr;
                              printf("dst:%s:%d, date = %s \n", inet_ntoa(addr), ntohs(tcphdr->dst_port), (char*)(tcphdr+1));
                        }
                  }else if(hdr->ether_type == rte_cpu_to_be_16(SELF_PROTO_TYPE)){
                        char *data =  rte_pktmbuf_mtod_offset(pkts_burst[i], char *, sizeof(struct rte_ether_hdr));
                        printf("recv data: %s \n", data);
                  }
                  rte_pktmbuf_free(pkts_burst[i]);
            }
      }
      return 0;
}

Makefile文件

# SPDX-License-Identifier: BSD-3-Clause
# Copyright(c) 2010-2014 Intel Corporation

# binary name
APP = recv

# all source are stored in SRCS-y
SRCS-y := main.c

PKGCONF ?= pkg-config

# Build using pkg-config variables if possible
ifneq ($(shell $(PKGCONF) --exists libdpdk && echo 0),0)
$(error "no installation of DPDK found")
endif

all: shared
.PHONY: shared static
shared: build/$(APP)-shared
	ln -sf $(APP)-shared build/$(APP)
static: build/$(APP)-static
	ln -sf $(APP)-static build/$(APP)

PC_FILE := $(shell $(PKGCONF) --path libdpdk 2>/dev/null)
CFLAGS += -O3 $(shell $(PKGCONF) --cflags libdpdk)
# Add flag to allow experimental API as l2fwd uses rte_ethdev_set_ptype API
CFLAGS += -DALLOW_EXPERIMENTAL_API
LDFLAGS_SHARED = $(shell $(PKGCONF) --libs libdpdk)
LDFLAGS_STATIC = $(shell $(PKGCONF) --static --libs libdpdk)

ifeq ($(MAKECMDGOALS),static)
# check for broken pkg-config
ifeq ($(shell echo $(LDFLAGS_STATIC) | grep 'whole-archive.*l:lib.*no-whole-archive'),)
$(warning "pkg-config output list does not contain drivers between 'whole-archive'/'no-whole-archive' flags.")
$(error "Cannot generate statically-linked binaries with this version of pkg-config")
endif
endif

build/$(APP)-shared: $(SRCS-y) Makefile $(PC_FILE) | build
	$(CC) $(CFLAGS) $(SRCS-y) -o $@ $(LDFLAGS) $(LDFLAGS_SHARED)

build/$(APP)-static: $(SRCS-y) Makefile $(PC_FILE) | build
	$(CC) $(CFLAGS) $(SRCS-y) -o $@ $(LDFLAGS) $(LDFLAGS_STATIC)

build:
	@mkdir -p $@

.PHONY: clean
clean:
	rm -f build/$(APP) build/$(APP)-static build/$(APP)-shared
	test -d build && rmdir -p build || true

虚拟机设置

网络适配器2和3用于DPDK的绑定,并设置为桥接模式,使两个网口与物理机处于同一局域网

编译:

加载驱动绑定网卡:

#! /bin/bash

eth1=$1
eth2=$2
DPDK_PATH=$3
DPDK_KMOD_PATH=$4
if [ -z "$DPDK_PATH" ];then
      DPDK_PATH="/root/dpdk-22.07/usertools"
fi

if [ -z "$DPDK_KMOD_PATH" ];then
      DPDK_KMOD_PATH="/root/dpdk-22.07/kmod"
fi

# 关闭网口
ifconfig $eth1 down
ifconfig $eth2 down

# 大页
echo 512 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages

# 加载uio驱动
modprobe uio
insmod $DPDK_KMOD_PATH/igb_uio.ko intr_mode=legacy
# 加载kni驱动
insmod $DPDK_KMOD_PATH/rte_kni.ko kthread_mode=multiple

# 绑定网口
$DPDK_PATH/dpdk-devbind.py -b igb_uio "$eth1" "$eth2"
$DPDK_PATH/dpdk-devbind.py -s

运行:

可以获取到端口1的MAC地址,因为在代码中是端口1在接收数据

windows上设置静态IP与MAC的映射

命令:

netsh i i show in

添加静态IP与MAC的映射

命令:

netsh  -c  "i  i" add neighbors  16  "192.168.1.7" "00-0c-29-c3-b0-c0"

arp -a

利用网络调试工具发送UDP数据:

本地主机地址要与DPDK绑定的网络适配器同属于一个局域网


http://www.niftyadmin.cn/n/5067244.html

相关文章

1.springcloudalibaba nacos2.2.3部署

前言 nacos是springcloudalibaba体系的注册中心&#xff0c;演示如何搭建最新稳定版本的linux搭建。 前置条件&#xff0c;安装好jdk1.8 一、二进制压缩包下载 1.1 下载压缩包 nacos下载 点击下载下载后得到二进制包如下 nacos-2.2.3.tar.gz二、安装步骤 2.1.解压二进制…

案例题--Web应用考点

案例题--Web应用考点 负载均衡技术 在选择题中没有考察过web的相关知识&#xff0c;主要就是在案例分析题中考察 负载均衡技术 应用层负载均衡技术 传输层负载均衡技术 就近的找到距离最近的服务器&#xff0c;并进行分发 使用户就近获取所需内容&#xff0c;降低网络拥塞 …

苹果签名是什么?有几种?优势是什么?什么场合需要应用到?

随着iOS设备在全球的普及&#xff0c;许多用户和开发者都十分关注苹果签名&#xff08;Apple Signing&#xff09;这个概念。在这篇文章中&#xff0c;我们将详细介绍苹果签名是什么&#xff0c;如何更方便地获取苹果签名&#xff0c;以及如何使用苹果签名。 一、苹果签名是什…

回收站里面删除的照片如何恢复?

现在拍照已经成为人们生活中的一种方式&#xff0c;照片为我们保留了许多珍贵而美好的回忆。大家通常会把重要的照片保存在硬盘里&#xff0c;但当不小心把照片移入回收站并彻底删除时&#xff0c;情况就有点糟糕了。那么&#xff0c;回收站里删除的照片还有办法恢复吗&#xf…

面试系列 - Java常见算法(二)

目录 一、排序算法 1、插入排序&#xff08;Insertion Sort&#xff09; 2、归并排序&#xff08;Merge Sort&#xff09; 二、图形算法 1、最短路径算法&#xff08;Dijkstra算法、Floyd-Warshall算法&#xff09; Dijkstra算法 Floyd-Warshall算法 2、最小生成树算法&…

<学习笔记>从零开始自学Python-之-常用库篇(十二)Matplotlib

Matplotlib 是Python中类似 MATLAB的绘图工具&#xff0c;Matplotlib是Python中最常用的可视化工具之一&#xff0c;可以非常方便地创建2D图表和一些基本的3D图表&#xff0c;可根据数据集&#xff08;DataFrame&#xff0c;Series&#xff09;自行定义x,y轴&#xff0c;绘制图…

Java判断字符串是否是有效的括号

给定一个只包括 (&#xff0c;)&#xff0c;{&#xff0c;}&#xff0c;[&#xff0c;] 的字符串 s &#xff0c;判断字符串是否有效。 有效字符串需要满足&#xff1a; 左括号必须用相同类型的右括号闭合。例如&#xff1a;"[]","()","{}" 左括…

批量png图片格式转eps格式

问题描述&#xff1a; 在利用Latex排版论文格式时&#xff0c;当插入图片的格式要求为eps格式 &#xff0c;当然也适用于其它文件格式转换 解决方法&#xff1a; 推荐一格好用的免费在线格式转换工具&#xff1a;https://cdkm.com/cn/ 操作步骤&#xff1a; step1:打开网址 ste…