pktgen是linux内核自带的发包工具,省却了用户态socket的参与, 纯粹在内核构造skb送netdev的txqueue上,可以达到极高pps。pktgen只有UDP协议,适合做吞吐量测试
安装pktgen,一般只需要 insmod /lib/modules/uname-r
/pktgen.ko,
该目录下没有的话就去内核menuconfig选中PKTGEN,编译出kmod。
加载后 ps -aux | grep kpktgend,会发现与cpu核个数相等的内核线程已经启动。
在用户态通过/proc/net/pktgen/下几个文件配置内核中的线程和报文参数, kpktgend_*表示你可以为每个核绑定一个内核线程做不同配置的报文发送。
配置和启动脚本:
#! /bin/sh
lsmod | grep pktgen || { echo -e " Should load pktgen.ko first! "
exit
}
# User Config Here -----------------------------------------------------------
MYDEV=eth0 # use MYDEV to send pkts
PKT_STEP=10000000 # 100pps STEP unit is nanosecond, 0 means the MAX speed
#PKT_STEP=1000000 # 1000pps
#PKT_STEP=100000 # 10000pps
PKT_LEN=1000
DST_IP="2.2.2.100"
DST_MAC="02:5D:AE:BA:E0:00"
echo -e "@@@@ flow pps= `[ $PKT_STEP -gt 0 ] && echo $((1000000000 / $PKT_STEP)) || echo line rate`"
echo -e "@@@@ pkt len = $PKT_LEN"
CLONE_SKB=1000000 # num of identical copies of the same pkt, 0 means alloc for every skb
SIRQ_AFTER_SEND_PKTNUM=10000 #do_softirq after send NUM pkts
# CAUTION PGDEV differ in diff config stage
pgset()
{
local result
echo $1 > $PGDEV
result=`cat $PGDEV | fgrep "Result: OK:"`
if [ "$result" = "" ]; then
cat $PGDEV | fgrep Result:
fi
}
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_0
echo "Configuring $PGDEV"
#echo "Removing all devices"
pgset "rem_device_all"
echo "Adding $MYDEV to pktgen"
pgset "add_device $MYDEV"
pgset "max_before_softirq $SIRQ_AFTER_SEND_PKTNUM"
# device config
# delay 0 means maximum speed.
PGDEV=/proc/net/pktgen/${MYDEV}
echo "Configuring $PGDEV"
# COUNT 0 means forever
pgset "count 0"
pgset "clone_skb $CLONE_SKB"
# NIC plus 4 bytes CRC
pgset "pkt_size $PKT_LEN"
pgset "delay $PKT_STEP"
pgset "dst $DST_IP"
pgset "dst_mac $DST_MAC"
# Time to run
PGDEV=/proc/net/pktgen/pgctrl
echo "Running... ctrl^C to stop"
pgset "start"
echo "Done"
# refer to /proc/net/pktgen/${MYDEV} for sending statistic
附参数列表: