DPDK-實戰之helloworld

 這個是最簡單的使用dpdk開發套件的例程。

 源碼分析: 


#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <sys/queue.h>
//以上頭開發環境glibc的相關頭文件
#include <rte_memory.h>
#include <rte_memzone.h>
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_debug.h>
/*以上爲dpdk自己編寫的一些公共庫頭文件,如內存池、線程、抽象環境等工具,DPDK有自己的開發風格,適應之。*/
static int
lcore_hello(__attribute__((unused)) void *arg)/* 此處有一個字節對齊操作,此處不做詳細分析。*/
{
 
    unsigned lcore_id;
    lcore_id = rte_lcore_id(); //獲取邏輯覈編號,並輸出邏輯核id,返回,線程退出。
    printf("hello from core %u\n", lcore_id);
    return 0;
}
 
int
main(int argc, char **argv)
{
    int ret;
    unsigned lcore_id;
    /* 相關初始化工作,如命令含參數處理,自動檢測環境相關條件。以及相關庫平臺初始化工作*/
    ret = rte_eal_init(argc, argv);
    if (ret < 0)
        rte_panic("Cannot init EAL\n");
 
    /* 每個從邏輯核調用回調函數lcore_hello輸出相關信息。 */
    RTE_LCORE_FOREACH_SLAVE(lcore_id) {
        rte_eal_remote_launch(lcore_hello, NULL, lcore_id);
    }
 
    /* 再次調用主邏輯核輸出相關信息。 */
    lcore_hello(NULL);
    /* 等待所有從邏輯核調用返回,相當於主線程阻塞等待。*/
    rte_eal_mp_wait_lcore();
    return 0;
}

通用makefile

#判斷相關環境變量是否設置
ifeq ($(RTE_SDK),)
$(error "Please define RTE_SDK environment variable")
endif
 
# 默認的平臺目標
RTE_TARGET ?= x86_64-native-linuxapp-gcc
 
include $(RTE_SDK)/mk/rte.vars.mk
 
# binary name
APP = helloworld
 
# all source are stored in SRCS-y
SRCS-y := main.c
 
CFLAGS += -O3
CFLAGS += $(WERROR_FLAGS)
 
include $(RTE_SDK)/mk/rte.extapp.mk

可帶參數運行:

./build/helloworld -c COREMASK -n NUM

解釋:coremask:十六進制掩碼錶示分配的邏輯內核(例如1111表示f代表四個邏輯核同時運行);m:內存通道數。

也可以通過--master-lcore=1,指定lcore1爲主線程。

如下輸入./helloworld -c f -n 2 --master-lcore=2輸出情況:

[root@localhost build]# ./helloworld -c f -n 2 --master-lcore=2
EAL: Detected 4 lcore(s)
EAL: Detected 1 NUMA nodes
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'PA'
EAL: No available hugepages reported in hugepages-1048576kB
EAL: Probing VFIO support...
EAL: PCI device 0000:02:01.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:02.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:06.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:07.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
hello from core 0
hello from core 1
hello from core 3
hello from core 2

如下是對參數帶參數運行的解釋:

[root@dev build]# ./helloworld --help
EAL: Detected lcore 0 as core 0 on socket 0
EAL: Detected lcore 1 as core 1 on socket 0
EAL: Support maximum 128 logical core(s) by configuration.
EAL: Detected 2 lcore(s)

Usage: ./helloworld [options]

EAL common options:
  -c COREMASK         邏輯核16進制掩碼
  -l CORELIST         列出運行時邏輯核列表           
                      參數格式 <c1>[-c2][,c3[-c4],...]
                      where c1, c2, etc are core indexes between 0 and 128
  --lcores COREMAP    映射邏輯核到物理邏輯核集合中
                      The argument format is
                            '<lcores[@cpus]>[<,lcores[@cpus]>...]'
                      lcores and cpus list are grouped by '(' and ')'
                      Within the group, '-' is used for range separator,
                      ',' is used for single number separator.
                      '( )' can be omitted for single element group,
                      '@' can be omitted if cpus and lcores have the same value
  --master-lcore ID   指定主線程邏輯核id
  -n CHANNELS         指定內存通道數
  -m MB               指定內存分配 (類似 --socket-mem)
  -r RANKS            Force number of memory ranks (don't detect) 強制內存參數
  -b, --pci-blacklist 將PCI網絡設備列入黑名單,防止EAL環境使用這些PCI設備,參數格式爲<domain:bus:devid.func>
  -w, --pci-whitelist 將PCI網絡設備列入白名單,僅僅用指定的PCI設備,參數格式爲<[domain:]bus:devid.func>
  --vdev              添加一塊虛擬設備,這個參數格式爲 <driver><id>[,key=val,...]
                      (例如: --vdev=eth_pcap0,iface=eth2).
  -d LIB.so|DIR       添加驅動活驅動目錄(can be used multiple times)
  --vmware-tsc-map    Use VMware TSC map instead of native RDTSC
  --proc-type         進程的類型 (primary|secondary|auto)
  --syslog            設定syslog日誌
  --log-level         設定默認日誌級別
  -v                  啓動時顯示版本信息
  -h, --help          This help

EAL options for DEBUG use only: 調試
  --huge-unlink       在初始化後去掉大頁面文件連接
  --no-huge           用 malloc 代替 hugetlbfs
  --no-pci            關閉 PCI
  --no-hpet           關閉 HPET
  --no-shconf         不共享配置(mmap'd files)

EAL Linux options: 選項
  --socket-mem        內存分配
  --huge-dir          大頁面掛載目錄
  --file-prefix       頁表文件前綴
  --base-virtaddr     虛擬地址基址
  --create-uio-dev    Create /dev/uioX (usually done by hotplug)
  --vfio-intr         Interrupt mode for VFIO (legacy|msi|msix)
  --xen-dom0          Support running on Xen dom0 without hugetlbfs</span>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章