DPDK(五):发包工具------pktgen

来源:互联网 发布:淘宝3c认证可以用其他 编辑:程序博客网 时间:2024/06/05 15:49

测试DPDK不可或缺的一个发包工具,一般需要一台实体测试仪,家中环境简单,只能使用软发包工具了,找到一款叫做pktgen,其实也可以自己用pcap或者raw socket实现,看介绍使用pktgen性能很不错,也进行了绑核,不再造轮子了。

        pktgen的原理以及使用可以参考http://www.lenky.info/archives/2012/02/1165。

        看一下我的测试环境,虚拟机环境上三个网卡绑到了同一个网桥上,相当于将三个网卡连上了光纤,分别是eth2、eth3、eth4,如下图所示:

eth2                       eth3                     eth4

  |                               |                           |

  |                               |                          |

 ----------------------------------------------------      veth8

        网卡配置信息如下:

        

root@ubuntu:/home# ifconfigeth0      Link encap:Ethernet  HWaddr 00:0c:29:41:a2:1f            inet addr:192.168.91.128  Bcast:192.168.91.255  Mask:255.255.255.0          inet6 addr: fe80::20c:29ff:fe41:a21f/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:113 errors:0 dropped:0 overruns:0 frame:0          TX packets:303 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:16138 (16.1 KB)  TX bytes:48250 (48.2 KB)eth1      Link encap:Ethernet  HWaddr 00:50:56:38:e6:f6            inet addr:192.168.174.128  Bcast:192.168.174.255  Mask:255.255.255.0          inet6 addr: fe80::250:56ff:fe38:e6f6/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:5352 errors:0 dropped:0 overruns:0 frame:0          TX packets:7505 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:347980 (347.9 KB)  TX bytes:12763065 (12.7 MB)eth2      Link encap:Ethernet  HWaddr 00:50:56:36:8d:e6            inet addr:192.168.19.129  Bcast:192.168.19.255  Mask:255.255.255.0          inet6 addr: fe80::250:56ff:fe36:8de6/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:299 errors:0 dropped:0 overruns:0 frame:0          TX packets:5313663 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:39253 (39.2 KB)  TX bytes:318866365 (318.8 MB)eth3      Link encap:Ethernet  HWaddr 00:0c:29:41:a2:3d            inet addr:192.168.19.128  Bcast:192.168.19.255  Mask:255.255.255.0          inet6 addr: fe80::20c:29ff:fe41:a23d/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:5305660 errors:0 dropped:0 overruns:0 frame:0          TX packets:125 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:318399640 (318.3 MB)  TX bytes:14485 (14.4 KB)eth4      Link encap:Ethernet  HWaddr 00:50:56:3c:96:c7            inet addr:192.168.19.130  Bcast:192.168.19.255  Mask:255.255.255.0          inet6 addr: fe80::250:56ff:fe3c:96c7/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:561 errors:0 dropped:0 overruns:0 frame:0          TX packets:67 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:94079 (94.0 KB)  TX bytes:10020 (10.0 KB)          Interrupt:19 Base address:0x2000 lo        Link encap:Local Loopback            inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host          UP LOOPBACK RUNNING  MTU:65536  Metric:1          RX packets:68 errors:0 dropped:0 overruns:0 frame:0          TX packets:68 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0           RX bytes:5750 (5.7 KB)  TX bytes:5750 (5.7 KB)

现在我想从eth2发送报文到eth3,最终目标是使用DPDK绑定eth3,然后进行二层转发测试将报文转发到eth4。

配置文件如下( pkggen.conf ):

#!/bin/sh # FileName: pktgen-eth5-eth6.conf# modprobe pktgenfunction pgset(){    local result     echo $1 > $PGDEV     result=`cat $PGDEV | fgrep "Result: OK:"`    if [ "$result" = "" ]; then         cat $PGDEV | fgrep Result:    fi}function pg(){    echo inject > $PGDEV    cat $PGDEV} # Config Start Here -----------------------------------------------------------  # thread config# Each CPU has own thread. Two CPU exammple. We add eth1, eth2 respectivly. PGDEV=/proc/net/pktgen/kpktgend_2  echo "Removing all devices" pgset "rem_device_all"  echo "Adding eth2" pgset "add_device eth2"  # device config# delay 0 means maximum speed. CLONE_SKB="clone_skb 1000000"# NIC adds 4 bytes CRCPKT_SIZE="pkt_size 60" # COUNT 0 means forever#COUNT="count 0"COUNT="count 0"DELAY="delay 0" PGDEV=/proc/net/pktgen/eth2  echo "Configuring $PGDEV" pgset "$COUNT" pgset "$CLONE_SKB" pgset "$PKT_SIZE" pgset "$DELAY" pgset "dst 192.168.19.128" pgset "dst_mac 00:0c:29:41:a2:3d"  # Time to runPGDEV=/proc/net/pktgen/pgctrl  echo "Running... ctrl^C to stop" pgset "start" echo "Done" # Result can be vieved in /proc/net/pktgen/eth[2]

步骤:
(1)、modprobe pktgen

(2)、sh pkggen.conf 

(3)、tcpdump查看eth3上的报文情况,可以收到报文

root@ubuntu:/opt/code/dpdk-1.8.0/examples# tcpdump -vv  -i  eth3  -c 10tcpdump: listening on eth3, link-type EN10MB (Ethernet), capture size 262144 bytes00:37:41.068632 IP (tos 0x0, ttl 32, id 4, offset 0, flags [none], proto UDP (17), length 46)    192.168.19.129.discard > 192.168.19.128.discard: [no cksum] UDP, length 1800:37:41.068638 IP (tos 0x0, ttl 32, id 4, offset 0, flags [none], proto UDP (17), length 46)    192.168.19.129.discard > 192.168.19.128.discard: [no cksum] UDP, length 1800:37:41.068640 IP (tos 0x0, ttl 32, id 4, offset 0, flags [none], proto UDP (17), length 46)    192.168.19.129.discard > 192.168.19.128.discard: [no cksum] UDP, length 1800:37:41.068642 IP (tos 0x0, ttl 32, id 4, offset 0, flags [none], proto UDP (17), length 46)    192.168.19.129.discard > 192.168.19.128.discard: [no cksum] UDP, length 1800:37:41.068757 IP (tos 0x0, ttl 32, id 4, offset 0, flags [none], proto UDP (17), length 46)    192.168.19.129.discard > 192.168.19.128.discard: [no cksum] UDP, length 1800:37:41.068762 IP (tos 0x0, ttl 32, id 4, offset 0, flags [none], proto UDP (17), length 46)    192.168.19.129.discard > 192.168.19.128.discard: [no cksum] UDP, length 1800:37:41.068968 IP (tos 0x0, ttl 32, id 4, offset 0, flags [none], proto UDP (17), length 46)    192.168.19.129.discard > 192.168.19.128.discard: [no cksum] UDP, length 1800:37:41.184195 IP (tos 0x0, ttl 32, id 4, offset 0, flags [none], proto UDP (17), length 46)    192.168.19.129.discard > 192.168.19.128.discard: [no cksum] UDP, length 1800:37:41.194487 IP (tos 0x0, ttl 32, id 4, offset 0, flags [none], proto UDP (17), length 46)    192.168.19.129.discard > 192.168.19.128.discard: [no cksum] UDP, length 1800:37:41.194493 IP (tos 0x0, ttl 32, id 4, offset 0, flags [none], proto UDP (17), length 46)    192.168.19.129.discard > 192.168.19.128.discard: [no cksum] UDP, length 1810 packets captured3525 packets received by filter3188 packets dropped by kernel