对例子IP Fragmentation的熟悉,使用,以及源码分析。
功能:
该例子的功能有二:
一: 将IP分片?
二: 根据路由表,做包转发。 路由表如下:
IP_FRAG: Socket : adding route 100.10.0.0/ (port )
IP_FRAG: Socket : adding route 100.20.0.0/ (port )
IP_FRAG: Socket : adding route 100.30.0.0/ (port )
IP_FRAG: Socket : adding route 100.40.0.0/ (port )
IP_FRAG: Socket : adding route 100.50.0.0/ (port )
IP_FRAG: Socket : adding route 100.60.0.0/ (port )
IP_FRAG: Socket : adding route 100.70.0.0/ (port )
IP_FRAG: Socket : adding route 100.80.0.0/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
问题一:
main()函数大概是这样的:标红的三行将与下面叙述的事情相关
int
main(int argc, char **argv)
{
... ...
/* init EAL */
ret = rte_eal_init(argc, argv);
if (ret < )
rte_exit(EXIT_FAILURE, "rte_eal_init failed");
... ... /* launch per-lcore init on every lcore */
rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
RTE_LCORE_FOREACH_SLAVE(lcore_id) {
if (rte_eal_wait_lcore(lcore_id) < 0)
return -;
} return ;
}
其中,函数 rte_eal_wait_lcore 的实现如下:
/*
* Wait until a lcore finished its job.
*/
int
rte_eal_wait_lcore(unsigned slave_id)
{
if (lcore_config[slave_id].state == WAIT)
return ; while (lcore_config[slave_id].state != WAIT &&
lcore_config[slave_id].state != FINISHED); rte_rmb(); /* we are in finished state, go to wait state */
lcore_config[slave_id].state = WAIT;
return lcore_config[slave_id].ret;
}
阅读红色部分,可以很明显的发现,这是一个死循环啊!!! 从字面意义上来看,main函数在完成了remote_launch之后,主进程会在这个函数里等等子进程结束。
这样的话,用一个死循环来等,难道不会有问题吗??? 所以我要的debug它一下看看怎么回事。 于是,为了达到这个目的,我分别经历了下文中的问题二三四。终于debug成功了。解答如下:
解答起来其实也很简单,只需要看下 rte_eal_mp_remote_launch() 函数的代码,就明白了。它的代码如下:
/*
67 * Check that every SLAVE lcores are in WAIT state, then call
68 * rte_eal_remote_launch() for all of them. If call_master is true
69 * (set to CALL_MASTER), also call the function on the master lcore.
70 */
int
rte_eal_mp_remote_launch(int (*f)(void *), void *arg,
enum rte_rmt_call_master_t call_master)
{
int lcore_id;
int master = rte_get_master_lcore(); /* check state of lcores */
RTE_LCORE_FOREACH_SLAVE(lcore_id) {
if (lcore_config[lcore_id].state != WAIT)
return -EBUSY;
} /* send messages to cores */
RTE_LCORE_FOREACH_SLAVE(lcore_id) {
rte_eal_remote_launch(f, arg, lcore_id);
} 89 if (call_master == CALL_MASTER) {
90 lcore_config[master].ret = f(arg);
lcore_config[master].state = FINISHED;
} return ;
}
从第90行可以看出。主进程在这里进入了业务逻辑,所以直到程序退出之前。它都没有机会执行前边的那个死循环。也就是说,主进程当进入死循环的时候,也说明其他进程即将结束。并不会存在长期空跑CPU的情况。 不过,如果业务逻辑写错了呢? 子进程并没有如逾期退出的话,是否会进入循环? 这里暂时先留下这个疑问。
另一个需要纪录下来的东西是。所有有需要的函数,实际上在rte_eal_init() 函数中便都创建完成了。remote_launch()函数实际上只是为其他进程传递一个启动运行的消息。
具体消息内容,目前我没有深入分析。
问题二:
运行不起来,启用DEBUG,gdb跟踪一下。
这个makefile也是很那难用的。摸索了一下,有几个命令,比较有用的如下:
[root@dpdk dpdk]# make help
[root@dpdk dpdk]# make V=yes D=yes
以上命令并没有用,到各模块的MAKEFILE里,将-O3手工改成-g,重新编译,才奏效。
问题三:
通过gdb发现,启动不了跟网卡特效有关系。
a。初始化函数中默认的参数是启用 硬checksum 等 offload 特性的。由于我模拟的网卡不支持,只能关掉。
static const struct rte_eth_conf port_conf = {
.rxmode = {
.max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
.split_hdr_size = ,
.header_split = , /**< Header Split disabled */
.hw_ip_checksum = 0, /**< IP checksum offload enabled */
.hw_vlan_filter = , /**< VLAN filtering disabled */
.jumbo_frame = , /**< Jumbo Frame Support enabled */
.hw_strip_crc = , /**< CRC stripped by hardware */
},
.txmode = {
.mq_mode = ETH_MQ_TX_NONE,
},
};
b. 另一处修改
/* init one TX queue per couple (lcore,port) */
queueid = ;
for (lcore_id = ; lcore_id < RTE_MAX_LCORE; lcore_id++) {
if (rte_lcore_is_enabled(lcore_id) == )
continue; socket = (int) rte_lcore_to_socket_id(lcore_id);
printf("txq=%u,%d ", lcore_id, queueid);
fflush(stdout); rte_eth_dev_info_get(portid, &dev_info);
txconf = &dev_info.default_txconf;
txconf->txq_flags = | ETH_TXQ_FLAGS_NOXSUMS;
ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
socket, txconf);
if (ret < ) {
printf("\n");
rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
"err=%d, port=%d\n", ret, portid);
} qconf = &lcore_queue_conf[lcore_id];
qconf->tx_queue_id[portid] = queueid;
queueid++;
}
c. 我之前模拟的网卡不支持多队列,经过学习研究,让 qemu/kvm 支持了多队列。另写了一篇,如下:
[Virtualization][qemu][kvm][virtio] 使用 QEMU/KVM 模拟网卡多队列
启动成功:
[root@dpdk build]# ./ip_fragmentation -l , -- -p
EAL: Detected lcore(s)
EAL: Probing VFIO support...
EAL: WARNING: cpu flags constant_tsc=yes nonstop_tsc=no -> using unreliable clock cycles !
PMD: bnxt_rte_pmd_init() called for (null)
EAL: PCI device ::03.0 on NUMA socket -
EAL: probe driver: 1af4: rte_virtio_pmd
EAL: PCI device ::04.0 on NUMA socket -
EAL: probe driver: 1af4: rte_virtio_pmd
EAL: PCI device ::05.0 on NUMA socket -
EAL: probe driver: 1af4: rte_virtio_pmd
IP_FRAG: Creating direct mempool on socket
IP_FRAG: Creating indirect mempool on socket
IP_FRAG: Creating LPM table on socket
IP_FRAG: Creating LPM6 table on socket
Initializing port on lcore ... Address::::::
txq=, txq=,
Initializing port on lcore ... Address::::::
txq=, txq=, IP_FRAG: Socket : adding route 100.10.0.0/ (port )
IP_FRAG: Socket : adding route 100.20.0.0/ (port )
IP_FRAG: Socket : adding route 100.30.0.0/ (port )
IP_FRAG: Socket : adding route 100.40.0.0/ (port )
IP_FRAG: Socket : adding route 100.50.0.0/ (port )
IP_FRAG: Socket : adding route 100.60.0.0/ (port )
IP_FRAG: Socket : adding route 100.70.0.0/ (port )
IP_FRAG: Socket : adding route 100.80.0.0/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port )
IP_FRAG: Socket : adding route :::::::/ (port ) Checking link status
done
Port Link Up - speed Mbps - full-duplex
Port Link Up - speed Mbps - full-duplex
IP_FRAG: entering main loop on lcore
IP_FRAG: -- lcoreid= portid=
IP_FRAG: entering main loop on lcore
IP_FRAG: -- lcoreid= portid=
问题四:
如何查看编译选项,使用的静态库。修改编译选项,启动debug等? 唯一的办法是makefile。结构还是很清晰的。但是,依然需要花很长的时间读。
打印编译命令的方法如下:
修改文件 /sdk/@dpdk/dpdk-stable-16.07.1 mk/internal/rte.compile-pre.mk 中的 C_TO_O_DO 变量: 第101行,为新增内容。
C_TO_O_DO = @set -e; \
echo $(C_TO_O_DISP); \
echo $(C_TO_O); \
$(C_TO_O) && \
$(PMDINFO_TO_O) && \
echo $(C_TO_O_CMD) > $(call obj2cmd,$(@)) && \
sed 's,'$@':,dep_'$@' =,' $(call obj2dep,$(@)).tmp > $(call obj2dep,$(@)) && \
rm -f $(call obj2dep,$(@)).tmp
打印链接命令的方法如下:
修改文件 /sdk/@dpdk/dpdk-stable-16.07.1 mk/rte.app.mk 中的 O_TO_EXE_DO 变量: 第209行,为新增内容。
O_TO_EXE_DO = @set -e; \
echo $(O_TO_EXE_DISP); \
echo $(O_TO_EXE); \
$(O_TO_EXE) && \
echo $(O_TO_EXE_CMD) > $(call exe2cmd,$(@))
实现效果如下:
[root@dpdk ip_fragmentation]# make
echo "xxxxccccxxxx"
xxxxccccxxxx
CC main.o
gcc -Wp,-MD,./.main.o.d.tmp -m64 -pthread -march=native -DRTE_MACHINE_CPUFLAG_SSE -DRTE_MACHINE_CPUFLAG_SSE2 -DRTE_MACHINE_CPUFLAG_SSE3 -DRTE_MACHINE_CPUFLAG_SSSE3 -DRTE_MACHINE_CPUFLAG_SSE4_1 -DRTE_MACHINE_CPUFLAG_SSE4_2 -I/root/src/sdk/@dpdk/dpdk-stable-16.07./examples/ip_fragmentation/build/include -I/root/dpdk//x86_64-native-linuxapp-gcc/include -include /root/dpdk//x86_64-native-linuxapp-gcc/include/rte_config.h -g -W -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wpointer-arith -Wcast-align -Wnested-externs -Wcast-qual -Wformat-nonliteral -Wformat-security -Wundef -Wwrite-strings -Wno-return-type -o main.o -c /root/src/sdk/@dpdk/dpdk-stable-16.07.1/examples/ip_fragmentation/main.c
LD ip_fragmentation
gcc -o ip_fragmentation -m64 -pthread -march=native -DRTE_MACHINE_CPUFLAG_SSE -DRTE_MACHINE_CPUFLAG_SSE2 -DRTE_MACHINE_CPUFLAG_SSE3 -DRTE_MACHINE_CPUFLAG_SSSE3 -DRTE_MACHINE_CPUFLAG_SSE4_1 -DRTE_MACHINE_CPUFLAG_SSE4_2 -I/root/src/sdk/@dpdk/dpdk-stable-16.07./examples/ip_fragmentation/build/include -I/root/dpdk//x86_64-native-linuxapp-gcc/include -include /root/dpdk//x86_64-native-linuxapp-gcc/include/rte_config.h -g -W -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wpointer-arith -Wcast-align -Wnested-externs -Wcast-qual -Wformat-nonliteral -Wformat-security -Wundef -Wwrite-strings main.o -L/root/dpdk//x86_64-native-linuxapp-gcc/lib -Wl,-lrte_kni -Wl,-lrte_pipeline -Wl,-lrte_table -Wl,-lrte_port -Wl,-lrte_pdump -Wl,-lrte_distributor -Wl,-lrte_reorder -Wl,-lrte_ip_frag -Wl,-lrte_meter -Wl,-lrte_sched -Wl,-lrte_lpm -Wl,--whole-archive -Wl,-lrte_acl -Wl,--no-whole-archive -Wl,-lrte_jobstats -Wl,-lrte_power -Wl,--whole-archive -Wl,-lrte_timer -Wl,-lrte_hash -Wl,-lrte_vhost -Wl,-lrte_kvargs -Wl,-lrte_mbuf -Wl,-lethdev -Wl,-lrte_cryptodev -Wl,-lrte_mempool -Wl,-lrte_ring -Wl,-lrte_eal -Wl,-lrte_cmdline -Wl,-lrte_cfgfile -Wl,-lrte_pmd_bond -Wl,-lrte_pmd_af_packet -Wl,-lrte_pmd_bnxt -Wl,-lrte_pmd_cxgbe -Wl,-lrte_pmd_e1000 -Wl,-lrte_pmd_ena -Wl,-lrte_pmd_enic -Wl,-lrte_pmd_fm10k -Wl,-lrte_pmd_i40e -Wl,-lrte_pmd_ixgbe -Wl,-lrte_pmd_null -Wl,-lrte_pmd_ring -Wl,-lrte_pmd_virtio -Wl,-lrte_pmd_vhost -Wl,-lrte_pmd_vmxnet3_uio -Wl,-lrte_pmd_null_crypto -Wl,--no-whole-archive -Wl,-lrt -Wl,-lm -Wl,-ldl -Wl,-export-dynamic -Wl,-export-dynamic -L/root/src/sdk/@dpdk/dpdk-stable-16.07.1/examples/ip_fragmentation/build/lib -L/root/dpdk//x86_64-native-linuxapp-gcc/lib -Wl,--as-needed -Wl,-Map=ip_fragmentation.map -Wl,--cref
INSTALL-APP ip_fragmentation
INSTALL-MAP ip_fragmentation.map
[root@dpdk ip_fragmentation]#
问题五
启动例子程序之后,做发包测试。发现所有包都被源端口转发回来。通过代码可以看到,默认的路由规则就是源端口转发回来。
a。怎么方便的发包呢? 除了已知的 tcpreplay 可以在端口上将包原样转发以外。还可以使用 tcpreplay-edit 对包内容做一下修改后在发送。我就是通过这种方法来测试例子中的路由表的。
/home/tong/Data [tong@T7] [:]
> sudo tcpreplay-edit -i tap-dpdk- -D 0.0.0.0/:100.20.0.0/ --enet-dmac=::::: -L1 oicq-bak.pcap
Actual: packets ( bytes) sent in 30.01 seconds.
Rated: 5.3 Bps, 0.000 Mbps, 0.06 pps
Flows: flows, 0.03 fps, flow packets, non-flow
Statistics for network device: tap-dpdk-
Attempted packets:
Successful packets:
Failed packets:
Truncated packets:
Retried packets (ENOBUFS):
Retried packets (EAGAIN):
b。通过 debug 发现收包之后的结构如下:
其中,红色部分的结构代表了报文类型。很显然,可以看出来,程序在这个时间点,并没有识别到,该报文是IPv4 or IPv6。在后续做转发的代码逻辑里,会对IP类型进行判断,在此结构体数值下,该代码判断为既不是v4,也不是v6。故进入了默认路由,从源端口发了出来。
所以,到目前为止,并不知道为什么报类型没有被识别。可以有三种情况,1,代码依赖了硬件来处理,而我的模拟网卡不能处理。2. pmd处理,又要我是虚拟机,故没有处理。3. 例子代码有误。应该在代码某处,调用一个识别的函数进行处理。
总之,在还未完全高清楚一个常规包处理流程和逻辑时。该问题暂搁置。
(gdb) p *m
$ = {cacheline0 = 0x7fffd4dd4300, buf_addr = 0x7fffd4dd4380, buf_physaddr = , buf_len = , rearm_data = 0x7fffd4dd4312 "\216", data_off = , {
refcnt_atomic = {cnt = }, refcnt = }, nb_segs = '\001', port = '\000', ol_flags = , rx_descriptor_fields1 = 0x7fffd4dd4320, {packet_type = 0, {
l2_type = 0, l3_type = 0, l4_type = 0, tun_type = 0, inner_l2_type = 0, inner_l3_type = 0, inner_l4_type = 0}}, pkt_len = , data_len = , vlan_tci = ,
hash = {rss = , fdir = {{{hash = , id = }, lo = }, hi = }, sched = {lo = , hi = }, usr = }, seqn = , vlan_tci_outer = , cacheline1 = 0x7fffd4dd4340, {
userdata = 0x0, udata64 = }, pool = 0x7fffd60436c0, next = 0x0, {tx_offload = , {l2_len = , l3_len = , l4_len = , tso_segsz = , outer_l3_len = ,
outer_l2_len = }}, priv_size = , timesync = }
(gdb)
c. 改了qemu,模拟了硬件checksum,仍不行。通过debug分析代码,好像是virtio pmd driver就不支持硬件checksum。新的qemu命令如下(注意红色部分)。并没有找到方法怎么在guest里通过查看的方式确认网卡是否支持 checksum offload.
/home/tong/VM/dpdk [tong@T7] [:]
> cat start-multiqueue.sh
sudo qemu-system-x86_64 -nographic -vnc 127.0.0.1: -enable-kvm \
-m 2G -cpu Nehalem -smp cores=,threads=,sockets= \
-numa node,mem=1G,cpus=-,nodeid= \
-numa node,mem=1G,cpus=-,nodeid= \
-drive file=disk.img,if=virtio \
-net nic,vlan=,model=virtio,macaddr='00:00:00:01:00:00' \
-device virtio-net-pci,netdev=dev1,mac='00:00:00:01:00:01',vectors=,mq=on,csum=on,guest_csum=on \
-device virtio-net-pci,netdev=dev2,mac='00:00:00:01:00:02',vectors=,mq=on,csum=on,guest_csum=on \
-device virtio-net-pci,netdev=dev3,mac='00:00:00:01:00:03',vectors=,mq=on,csum=on,guest_csum=on \
-net tap,vlan=,ifname=tap-dpdk-ctrl \
-netdev tap,ifname=tap-dpdk-,script=no,downscript=no,vhost=on,queues=,id=dev1 \
-netdev tap,ifname=tap-dpdk-,script=no,downscript=no,vhost=on,queues=,id=dev2 \
-netdev tap,ifname=tap-dpdk-,script=no,downscript=no,vhost=on,queues=,id=dev3 &
# -device vfio-pci,host='0000:00:19.0' \
#ne2k_pci,i82551,i82557b,i82559er,rtl8139,e1000,pcnet,virtio /home/tong/VM/dpdk [tong@T7] [:]
>