转载地址:http://ocelot1985-163-com.iteye.com/blog/981992
pktgen的安装与使用
系统环境:fefora core 12
如果你和我一样,在pktgen面前是个新人。是不是也曾遇到下面的问题:
(1)以为pktgen和tcpdump一样是Linux下的工具软件;
(2)后来,明白了pktgen不是工具,而是内核模块,但是不知道如何加载;
(3)加载后,却不会使用,并且一度一位pktgen无法与网络接口eth0建立映射关系;
那么,我写下的东西可能对你学习pktgen有所帮助。因为,我也在google上搜了一同,却没有很好的对于pktgen讲解的文档。
A 首先,pktgen是Linux下的一个内核模块,并不是工具软件。所以,不能通过在命令行输入pktgen的方式运行。
那么,我的系统默认pktgen不是自动加载进内核,所以,如果你和我有一样的情况,就需要自己手动添加pktgen入内核。
查看pktgen是否在内核命令:
$ lsmod | grep pktgen
加载pktgen模块入内核的命令:
$ modprobe pktgen
此时,就会不禁想到,加载的模块文件到底在哪。我的系统位于
/lib/modules/内核版本号/kernel/net/core/pktgen.ko
B 加载成功后,看看pktgen模块的线程是不是已经运行了(更准确的说,是不是处于sleep状态)。
$ ps aux | grep pktgen
看到如下内容,说明pktgen的线程已经启动了。线程个数与cpu核数相关,本机cpu双核,所以两个线程。
root 2061 0.0 0.0 0 0 ? S< Mar30 0:28 [kpktgend_0]
root 2062 0.0 0.0 0 0 ? S< Mar30 0:00 [kpktgend_1]
C 然后,pktgen.txt上说可以检测下面三个文件的信息来了解pktgen
/proc/net/pktgen/pgctrl
/proc/net/pktgen/kpktgend_X
/proc/net/pktgen/ethX
但是,我发现自己主机中并没有 /proc/net/pktgen/ethX(X代表编号,例如接口eth0或eth1)。此时,我以为是不是
pktgen没有和网络接口eth0建立关系呢?难道是模块有问题?
其实,并不是这样,因为pktgen的运行需要脚本来驱动。因为我运行了网上的实例脚本pktgen.conf-1-1,但是也没有获得预期的结果。
/proc/net/pktgen/ethX还是不存在。一度认为pktgen.c编码有问题,因为我看到了本机中,之有eth0网络接口。命令如下:
$ dmesg | grep pktgen
输出结果:
pktgen: no such netdevice: "eth1"
pktgen.conf-1-1下载地址:
ftp://robur.slu.se/pub/Linux/net-development/pktgen-testing/examples/
D 最后我终于发现,既不是源码pktgen.c的问题,也不是pktgen模块的问题。原因在于,驱动脚本pktgen.conf-1-1中的网络接口配置与我的本机的不符所致。
pktgen.conf-1-1源码如下:
#! /bin/sh
#modprobe pktgen
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 respectivly.
PGDEV=/proc/net/pktgen/kpktgend_0
echo "Removing all devices"
pgset "rem_device_all"
echo "Adding eth1"
pgset "add_device eth1" # 就在这一句,默认的配置网络接口为eth1,但是我的本机并没有这个接口,而是eth0。所以,应该自己手动修改。
echo "Setting max_before_softirq 10000"
pgset "max_before_softirq 10000"
# device config
# delay 0 means maximum speed.
CLONE_SKB="clone_skb 1000000"
# 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/eth0
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/eth1
E 驱动脚本也有自己的一些命令方式,或者语法。下面列出一些规则:
同时,我自己也要进一步学习,也不是全懂。
** Pgcontrol commands:
start
stop
** Thread commands:
add_device
rem_device_all
max_before_softirq
** Device commands:
count
clone_skb
debug
frags
delay
src_mac_count
dst_mac_count
pkt_size
min_pkt_size
max_pkt_size
mpls
udp_src_min
udp_src_max
udp_dst_min
udp_dst_max
flag
IPSRC_RND
TXSIZE_RND
IPDST_RND
UDPSRC_RND
UDPDST_RND
MACSRC_RND
MACDST_RND
dst_min
dst_max
src_min
src_max
dst_mac
src_mac
clear_counters
dst6
src6
flows
flowlen
F 再写一些实例。
pgset "clone_skb 1" sets the number of copies of the same packet
pgset "clone_skb 0" use single SKB for all transmits
pgset "pkt_size 9014" sets packet size to 9014
pgset "frags 5" packet will consist of 5 fragments
pgset "count 200000" sets number of packets to send, set to zero
for continuous sends until explicitly stopped.
pgset "delay 5000" adds delay to hard_start_xmit(). nanoseconds
pgset "dst 10.0.0.1" sets IP destination address
(BEWARE! This generator is very aggressive!)
pgset "dst_min 10.0.0.1" Same as dst
pgset "dst_max 10.0.0.254" Set the maximum destination IP.
pgset "src_min 10.0.0.1" Set the minimum (or only) source IP.
pgset "src_max 10.0.0.254" Set the maximum source IP.
pgset "dst6 fec0::1" IPV6 destination address
pgset "src6 fec0::2" IPV6 source address
pgset "dstmac 00:00:00:00:00:00" sets MAC destination address
pgset "srcmac 00:00:00:00:00:00" sets MAC source address
pgset "queue_map_min 0" Sets the min value of tx queue interval
pgset "queue_map_max 7" Sets the max value of tx queue interval, for multiqueue devices
To select queue 1 of a given device,
use queue_map_min=1 and queue_map_max=1
pgset "src_mac_count 1" Sets the number of MACs we'll range through.
The 'minimum' MAC is what you set with srcmac.
pgset "dst_mac_count 1" Sets the number of MACs we'll range through.
The 'minimum' MAC is what you set with dstmac.
pgset "flag [name]" Set a flag to determine behaviour. Current flags
are: IPSRC_RND #IP Source is random (between min/max),
IPDST_RND, UDPSRC_RND,
UDPDST_RND, MACSRC_RND, MACDST_RND
MPLS_RND, VID_RND, SVID_RND
QUEUE_MAP_RND # queue map random
QUEUE_MAP_CPU # queue map mirrors smp_processor_id()
pgset "udp_src_min 9" set UDP source port min, If < udp_src_max, then
cycle through the port range.
pgset "udp_src_max 9" set UDP source port max.
pgset "udp_dst_min 9" set UDP destination port min, If < udp_dst_max, then
cycle through the port range.
pgset "udp_dst_max 9" set UDP destination port max.
pgset "mpls 0001000a,0002000a,0000000a" set MPLS labels (in this example
outer label=16,middle label=32,
inner label=0 (IPv4 NULL)) Note that
there must be no spaces between the
arguments. Leading zeros are required.
Do not set the bottom of stack bit,
that's done automatically. If you do
set the bottom of stack bit, that
indicates that you want to randomly
generate that address and the flag
MPLS_RND will be turned on. You
can have any mix of random and fixed
labels in the label stack.
pgset "mpls 0" turn off mpls (or any invalid argument works too!)
pgset "vlan_id 77" set VLAN ID 0-4095
pgset "vlan_p 3" set priority bit 0-7 (default 0)
pgset "vlan_cfi 0" set canonical format identifier 0-1 (default 0)
pgset "svlan_id 22" set SVLAN ID 0-4095
pgset "svlan_p 3" set priority bit 0-7 (default 0)
pgset "svlan_cfi 0" set canonical format identifier 0-1 (default 0)
pgset "vlan_id 9999" > 4095 remove vlan and svlan tags
pgset "svlan 9999" > 4095 remove svlan tag
pgset "tos XX" set former IPv4 TOS field (e.g. "tos 28" for AF11 no ECN, default 00)
pgset "traffic_class XX" set former IPv6 TRAFFIC CLASS (e.g. "traffic_class B8" for EF no ECN, default 00)
pgset stop aborts injection. Also, ^C aborts generator.
系统环境:fefora core 12
如果你和我一样,在pktgen面前是个新人。是不是也曾遇到下面的问题:
(1)以为pktgen和tcpdump一样是Linux下的工具软件;
(2)后来,明白了pktgen不是工具,而是内核模块,但是不知道如何加载;
(3)加载后,却不会使用,并且一度一位pktgen无法与网络接口eth0建立映射关系;
那么,我写下的东西可能对你学习pktgen有所帮助。因为,我也在google上搜了一同,却没有很好的对于pktgen讲解的文档。
A 首先,pktgen是Linux下的一个内核模块,并不是工具软件。所以,不能通过在命令行输入pktgen的方式运行。
那么,我的系统默认pktgen不是自动加载进内核,所以,如果你和我有一样的情况,就需要自己手动添加pktgen入内核。
查看pktgen是否在内核命令:
$ lsmod | grep pktgen
加载pktgen模块入内核的命令:
$ modprobe pktgen
此时,就会不禁想到,加载的模块文件到底在哪。我的系统位于
/lib/modules/内核版本号/kernel/net/core/pktgen.ko
B 加载成功后,看看pktgen模块的线程是不是已经运行了(更准确的说,是不是处于sleep状态)。
$ ps aux | grep pktgen
看到如下内容,说明pktgen的线程已经启动了。线程个数与cpu核数相关,本机cpu双核,所以两个线程。
root 2061 0.0 0.0 0 0 ? S< Mar30 0:28 [kpktgend_0]
root 2062 0.0 0.0 0 0 ? S< Mar30 0:00 [kpktgend_1]
C 然后,pktgen.txt上说可以检测下面三个文件的信息来了解pktgen
/proc/net/pktgen/pgctrl
/proc/net/pktgen/kpktgend_X
/proc/net/pktgen/ethX
但是,我发现自己主机中并没有 /proc/net/pktgen/ethX(X代表编号,例如接口eth0或eth1)。此时,我以为是不是
pktgen没有和网络接口eth0建立关系呢?难道是模块有问题?
其实,并不是这样,因为pktgen的运行需要脚本来驱动。因为我运行了网上的实例脚本pktgen.conf-1-1,但是也没有获得预期的结果。
/proc/net/pktgen/ethX还是不存在。一度认为pktgen.c编码有问题,因为我看到了本机中,之有eth0网络接口。命令如下:
$ dmesg | grep pktgen
输出结果:
pktgen: no such netdevice: "eth1"
pktgen.conf-1-1下载地址:
ftp://robur.slu.se/pub/Linux/net-development/pktgen-testing/examples/
D 最后我终于发现,既不是源码pktgen.c的问题,也不是pktgen模块的问题。原因在于,驱动脚本pktgen.conf-1-1中的网络接口配置与我的本机的不符所致。
pktgen.conf-1-1源码如下:
#! /bin/sh
#modprobe pktgen
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 respectivly.
PGDEV=/proc/net/pktgen/kpktgend_0
echo "Removing all devices"
pgset "rem_device_all"
echo "Adding eth1"
pgset "add_device eth1" # 就在这一句,默认的配置网络接口为eth1,但是我的本机并没有这个接口,而是eth0。所以,应该自己手动修改。
echo "Setting max_before_softirq 10000"
pgset "max_before_softirq 10000"
# device config
# delay 0 means maximum speed.
CLONE_SKB="clone_skb 1000000"
# 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/eth0
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/eth1
E 驱动脚本也有自己的一些命令方式,或者语法。下面列出一些规则:
同时,我自己也要进一步学习,也不是全懂。
** Pgcontrol commands:
start
stop
** Thread commands:
add_device
rem_device_all
max_before_softirq
** Device commands:
count
clone_skb
debug
frags
delay
src_mac_count
dst_mac_count
pkt_size
min_pkt_size
max_pkt_size
mpls
udp_src_min
udp_src_max
udp_dst_min
udp_dst_max
flag
IPSRC_RND
TXSIZE_RND
IPDST_RND
UDPSRC_RND
UDPDST_RND
MACSRC_RND
MACDST_RND
dst_min
dst_max
src_min
src_max
dst_mac
src_mac
clear_counters
dst6
src6
flows
flowlen
F 再写一些实例。
pgset "clone_skb 1" sets the number of copies of the same packet
pgset "clone_skb 0" use single SKB for all transmits
pgset "pkt_size 9014" sets packet size to 9014
pgset "frags 5" packet will consist of 5 fragments
pgset "count 200000" sets number of packets to send, set to zero
for continuous sends until explicitly stopped.
pgset "delay 5000" adds delay to hard_start_xmit(). nanoseconds
pgset "dst 10.0.0.1" sets IP destination address
(BEWARE! This generator is very aggressive!)
pgset "dst_min 10.0.0.1" Same as dst
pgset "dst_max 10.0.0.254" Set the maximum destination IP.
pgset "src_min 10.0.0.1" Set the minimum (or only) source IP.
pgset "src_max 10.0.0.254" Set the maximum source IP.
pgset "dst6 fec0::1" IPV6 destination address
pgset "src6 fec0::2" IPV6 source address
pgset "dstmac 00:00:00:00:00:00" sets MAC destination address
pgset "srcmac 00:00:00:00:00:00" sets MAC source address
pgset "queue_map_min 0" Sets the min value of tx queue interval
pgset "queue_map_max 7" Sets the max value of tx queue interval, for multiqueue devices
To select queue 1 of a given device,
use queue_map_min=1 and queue_map_max=1
pgset "src_mac_count 1" Sets the number of MACs we'll range through.
The 'minimum' MAC is what you set with srcmac.
pgset "dst_mac_count 1" Sets the number of MACs we'll range through.
The 'minimum' MAC is what you set with dstmac.
pgset "flag [name]" Set a flag to determine behaviour. Current flags
are: IPSRC_RND #IP Source is random (between min/max),
IPDST_RND, UDPSRC_RND,
UDPDST_RND, MACSRC_RND, MACDST_RND
MPLS_RND, VID_RND, SVID_RND
QUEUE_MAP_RND # queue map random
QUEUE_MAP_CPU # queue map mirrors smp_processor_id()
pgset "udp_src_min 9" set UDP source port min, If < udp_src_max, then
cycle through the port range.
pgset "udp_src_max 9" set UDP source port max.
pgset "udp_dst_min 9" set UDP destination port min, If < udp_dst_max, then
cycle through the port range.
pgset "udp_dst_max 9" set UDP destination port max.
pgset "mpls 0001000a,0002000a,0000000a" set MPLS labels (in this example
outer label=16,middle label=32,
inner label=0 (IPv4 NULL)) Note that
there must be no spaces between the
arguments. Leading zeros are required.
Do not set the bottom of stack bit,
that's done automatically. If you do
set the bottom of stack bit, that
indicates that you want to randomly
generate that address and the flag
MPLS_RND will be turned on. You
can have any mix of random and fixed
labels in the label stack.
pgset "mpls 0" turn off mpls (or any invalid argument works too!)
pgset "vlan_id 77" set VLAN ID 0-4095
pgset "vlan_p 3" set priority bit 0-7 (default 0)
pgset "vlan_cfi 0" set canonical format identifier 0-1 (default 0)
pgset "svlan_id 22" set SVLAN ID 0-4095
pgset "svlan_p 3" set priority bit 0-7 (default 0)
pgset "svlan_cfi 0" set canonical format identifier 0-1 (default 0)
pgset "vlan_id 9999" > 4095 remove vlan and svlan tags
pgset "svlan 9999" > 4095 remove svlan tag
pgset "tos XX" set former IPv4 TOS field (e.g. "tos 28" for AF11 no ECN, default 00)
pgset "traffic_class XX" set former IPv6 TRAFFIC CLASS (e.g. "traffic_class B8" for EF no ECN, default 00)
pgset stop aborts injection. Also, ^C aborts generator.