DPDK 学习第一天

DPDK

环境: wsl2 Ubuntu (不建议用wsl)

编译安装

安装必要的工具

bash 复制代码
sudo apt install -y \
  build-essential \
  meson ninja-build \
  python3-pip python3-pyelftools \
  libnuma-dev \
  pciutils ethtool \
  git wget

下载安装dpdk

bash 复制代码
wget https://fast.dpdk.org/rel/dpdk-22.11.4.tar.xz
tar xf dpdk-22.11.4.tar.xz
cd dpdk-stable-22.11.4
mkdir build && cd build
meson setup ..
ninja
sudo ninja install

HugePage(大页内存)

bash 复制代码
sudo nano /etc/sysctl.conf
# 添加:
vm.nr_hugepages=1024

sudo nano /etc/fstab
# 添加:
hugetlbfs /dev/hugepages hugetlbfs defaults 0 0

重启后生效

题主用的是wsl Ubuntu, powershell 执行wsl --shutdown

  • 检查是否生效 mount | grep huge

  • mount | grep huge

Hello 程序

c 复制代码
#include <stdio.h>
#include <rte_eal.h>
#include <rte_lcore.h>

/* 每个 lcore 上执行的函数 */
static int
lcore_hello(__rte_unused void *arg)
{
    unsigned lcore_id;

    /* 获取当前执行代码的逻辑核 ID */
    lcore_id = rte_lcore_id();

    /* 打印当前核信息 */
    printf("hello from core %u\n", lcore_id);

    return 0;   /* 返回值会被 EAL 收集 */
}

/* 主函数 */
int
main(int argc, char **argv)
{
    int ret;
    unsigned lcore_id;

    /* ================================
     * 1. 初始化 EAL(核心入口)
     * ================================ */
    ret = rte_eal_init(argc, argv);
    if (ret < 0)
        rte_panic("Cannot init EAL\n");

    /*
     * rte_eal_init 做的事情:
     * - 解析命令行参数(如 -l coremask)
     * - 初始化 hugepage 内存
     * - 初始化多核环境(lcore)
     * - 初始化设备(PCI/VFIO)
     * - 建立主从线程模型
     */

    /* ================================
     * 2. 在每个 worker lcore 上启动任务
     * ================================ */
    RTE_LCORE_FOREACH_WORKER(lcore_id) {

        /*
         * 在指定 lcore 上异步执行函数
         *
         * 参数:
         *  - 函数指针(每个核执行的函数)
         *  - 参数(这里传 NULL)
         *  - 指定的 lcore ID
         */
        rte_eal_remote_launch(lcore_hello, NULL, lcore_id);
    }

    /* ================================
     * 3. 主核(main lcore)也执行同样函数
     * ================================ */
    lcore_hello(NULL);

    /* ================================
     * 4. 等待所有 worker 核执行完成
     * ================================ */
    rte_eal_mp_wait_lcore();

    /* ================================
     * 5. 释放 EAL 资源(⚠️ 建议打开)
     * ================================ */
    // rte_eal_cleanup();

    return 0;
}
  • wsl 上执行该程序有段错误,不知道具体原因,先把rte_eal_cleanup注释掉
  • 其他错误暂时忽略掉
相关推荐
夜月yeyue20 分钟前
KCP 与 UDP 可靠传输
linux·网络·单片机·网络协议·udp·php
一个向上的运维者20 分钟前
Docker 自定义网络中容器无法通过宿主机 IP 访问服务的完整排障记录
网络·tcp/ip·docker
utf8mb4安全女神37 分钟前
子网划分【概念+实操+理解】
运维·服务器·网络
码语智行1 小时前
拦截器、接口限流、过滤器、防重发/幂等性功能说明
开发语言·网络·python
志栋智能1 小时前
超自动化安全:构建智能安全运营的神经系统
大数据·运维·网络·人工智能·安全·自动化
华普微HOPERF1 小时前
LoRa模块,如何通过卫星通信补齐地面网络的覆盖盲区?
网络·嵌入式硬件·模块·卫星通信
键盘上的猫头鹰2 小时前
【Linux 基础教程(一)】概述、安装与网络配置:VMware + CentOS + NAT + XShell 远程连接
linux·网络·centos
网络研究院2 小时前
管理瘫痪、人员短缺:深度解析 NIST NVD 为什么审不动漏洞了?
网络·安全·漏洞·管理·危机
机汇五金_3 小时前
通信设备防雨箱如何兼顾防护与散热?
网络·python
初中就开始混世的大魔王3 小时前
5 Fast DDS-Discovery
网络·c++·算法·中间件