pktgen——the packet generator within linux kernel

简介

pktgen是Linux内核内置的好性能测试工具,是当前测试网卡发送速率的最好工具,也可以用来构造包来测试其他网络设备,尤其是测试使用Linux网络协议栈的路由器和交换机。由于pktgen是内核内置的,使用内核空间,所以它可以达到高带宽、高发包速率,以此更好地测试路由器、交换机和其他网络设备。

准备工作

运行pktgen有两种方式:

  • 在内核空间运行程序
  • 作为模块加载使用

推荐将其作为模块使用,这也最常用的。加载以及卸载命令如下,切记要使用root身份运行。

modprobe pktgen		// 加载模块
rmmod pktgen				// 卸载模块

一旦加载pktgen模块后,会自动为每一个CPU创建一个内核线程,并将该线程绑定在对应的CPU上。与此同时,在/proc/net/pktgen/目录下,为每一个线程创建一个kpktgend_X文件(其中的X为CPU编号),用来控制和监控这些线程,以及一个pgctrl文件,用来控制pktgen程序。当卸载pktgen模块时,/proc/net/pktgen文件夹会被自动删除。

同时,当将某个网卡绑定在某个线程时,也会在/proc/net/pktgen/目录下自动创建以该网卡名称为名的文件,用于记录此网卡设备的配置信息和运行信息。

例如,在一个双核系统中,在/proc/net/pktgen/目录下会自动创建如下文件:kpktgend_0、kpktgend_1和pgctrl。将设备(例如eth0、eth1)绑定在kpktgend_X(X=0、1)线程上后,该目录下会自动创建eth0、eth1这两个文件。

测试程序可以配置成一直运行,或者在发送固定数量的包之后结束。也可以使用快捷键Ctrl+C终止运行。

运行过程以及运行结束后,可以通过/proc/net/pktgen/目录下的文件查看设备运行信息。

配置命令

Pgcontrol commands

命令含义
startStarts sending on all threads
stopAborts packet injection. Ctrl-C also aborts generator. Note: Use count 0 (forever) and stop the run with Ctrl-C when multiple devices are assigned to one pktgen thread. This avoids some devices finishing before others and skewing the results. We are primarily interested in how many packets all devices can send at the same time, not absolute number of packets each NIC sent.

Threads commands

命令含义
add_deviceAdd a device to thread i.e eth0
rem_device_allRemoves all devices from this thread
max_before_softirqdo_softirq() after sending a number of packets

Device commands

命令含义
debug
clone_skbNumber of identical copies of the same packet 0 means alloc for each skb. For DoS etc we must alloc new skb’s.
clear_counters `normally handled automatically
pkt_sizeLink packet size minus CRC (4)
min_pkt_sizeRange pkt_size setting If < max_pkt_size, then cycle through the port range.
max_pkt_size
fragsNumber of fragments for a packet
countNumber of packets to send. Use zero for continious sending
delayArtificial gap inserted between packets in nanoseconds
dstIP destination address i.e 10.0.0.1
dst_minSame as dst If < dst_max, then cycle through the port range.
dst_maxMaximum destination IP. i.e 10.0.0…1
src_minMinimum (or only) source IP. i.e. 10.0.0.254 If < src_max, then cycle through the port range.
src_maxMaximum source IP.
dst6IPV6 destination address i.e fec0::1
src6IPV6 source address i.e fec0::2
dstmacMAC destination adress 00:00:00:00:00:00
srcmacMAC source adress. If omitted it’s automatically taken from source device
src_mac_countNumber of MACs we’ll range through. Minimum’ MAC is what you set with srcmac.
dst_mac_countNumber of MACs we’ll range through. Minimum’ MAC is what you set with dstmac.
udp_src_minUDP source port min, If < udp_src_max, then cycle through the port range.
udp_src_maxUDP source port max.
udp_dst_minUDP destination port min, If < udp_dst_max, then cycle through the port range.
udp_dst_maxUDP destination port max.
flowsNumber of concurrent flows
flowlenLength of a flow
rate 300Mset rate to 300 Mb/s
ratep 1000000set rate to 1Mpps

Flags

命令含义
IPSRC_RNDIP Source is random (between min/max),
IPDST_RND Etc
TXSIZE_RND
UDPSRC_RND
UDPDST_RND
MACSRC_RND
MACDST_RND

配置示例

以下是一个单CPU单NIC的实例。

#!/bin/sh
function 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 respectively.
PGDEV=/proc/net/pktgen/kpktgend_0
echo "Removing all devices"
pgset "rem_device_all"
echo "Adding ens38"
pgset "add_device ens38"
echo "Setting max_before_softirq 10000"
pgset "max_before_softirq 10000"
# device config
# delay is inter packet gap. 0 means maximum speed.
CLONE_SKB="clone_skb 000000"
# NIC adds 4 bytes CRC
PKT_SIZE="pkt_size 60"
# COUNT 0 means forever
#COUNT="count 0"
COUNT="count 10000000"
delay="delay 0"

PGDEV=/proc/net/pktgen/ens38
echo "Configuring $PGDEV"
pgset "$COUNT"
pgset "$CLONE_SKB"
pgset "$PKT_SIZE"
pgset "$delay"
pgset "dst 10.10.11.2"
pgset "dst_mac 00:04:23:08:91:dc"
# Time to run
PGDEV=/proc/net/pktgen/pgctrl
echo "Running... ctrl^C to stop"
pgset "start"
echo "Done"
# Result can be vieved in /proc/net/pktgen/ens38

结果展示

运行完成后,查看ems38文件,如下图所示:

在这里插入图片描述
此时,发包速率为155131pps,74Mbps,感觉有点慢,这是因为包太小了。将60改为10000,重新测试,截图如下:

在这里插入图片描述
此时,发包速率为2252pps,18016Mbps。我这里是用虚拟机跑的,具体能跑多快,还是和设备硬件有关。

相关链接

  1. https://juliofaracco.wordpress.com/2015/06/14/pktgen-a-kernel-space-traffic-generator-for-testing-network-throughput/
  2. https://www.kernel.org/doc/Documentation/networking/pktgen.txt
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值