pktgen的安裝與使用


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.

來源:http://hi.baidu.com/niwi/blog/item/aeddf8d4eead4b18a08bb71e.html

發佈了6 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章