Thursday, June 4, 2009

Pktgen – a tool to generate packets at very high speed in the kernel

My environment: RedHat Fedora Core 8 x86_64

Firstly check if your pktgen is running:

[root@pandaeatsbamboo pktgen]# ps -e | grep pktgen
8913 ?        00:01:55 kpktgend_0
8914 ?        00:01:55 kpktgend_1
8915 ?        00:00:01 kpktgend_2
8916 ?        00:00:01 kpktgend_3


I have a quad-core AMD CPU therefore you will see 4 processes there.  Pktgen creates 1 thread per CPU.

If you can’t see that, try to:
modprobe pktgen

I have 2 NIC card, eth0 and eth1, here is my sample shell script to kick start the pktgen:

#! /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 eth0"



 pgset "add_device eth0"



  echo "Setting max_before_softirq 10000"



 pgset "max_before_softirq 10000"



 



PGDEV=/proc/net/pktgen/kpktgend_1



  echo "Removing all devices"



 pgset "rem_device_all"



  echo "Adding eth1"



 pgset "add_device eth1"



  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 168.106.1.248"



 pgset "dst_mac  00:24:51:13:06:41"



 



PGDEV=/proc/net/pktgen/eth1



  echo "Configuring $PGDEV"



 pgset "$COUNT"



 pgset "$CLONE_SKB"



 pgset "$PKT_SIZE"



 pgset "$DELAY"



 pgset "dst 192.168.10.1"



 pgset "dst_mac  00:24:51:13:06:43"



 



# Time to run



PGDEV=/proc/net/pktgen/pgctrl



 



 echo "Running... ctrl^C to stop"



 pgset "start"



 echo "Done"




To view the statistics, check the files in /proc/net/pktgen/ethX





[root@pandaeatsbamboo pktgen]# less /proc/net/pktgen/eth0



Params: count 10000000  min_pkt_size: 60  max_pkt_size: 60



     frags: 0  delay: 0  clone_skb: 1000000  ifname: eth0



     flows: 0 flowlen: 0



     queue_map_min: 0  queue_map_max: 0



     dst_min: 168.106.1.248  dst_max:



     src_min:   src_max:



     src_mac: 00:1f:d0:50:19:6d dst_mac: 00:24:51:13:06:41



     udp_src_min: 9  udp_src_max: 9  udp_dst_min: 9  udp_dst_max: 9



     src_mac_count: 0  dst_mac_count: 0



     Flags:



Current:



     pkts-sofar: 10000000  errors: 0



     started: 1244025262231283us  stopped: 1244025310237392us idle: 11640574us



     seq_num: 10000001  cur_dst_mac_offset: 0  cur_src_mac_offset: 0



     cur_saddr: 0xfa016aa8  cur_daddr: 0xf8016aa8



     cur_udp_dst: 9  cur_udp_src: 9



     cur_queue_map: 0



     flows: 0



Result: OK: 48006109(c36365535+d11640574) usec, 10000000 (60byte,0frags)



  208306pps 99Mb/sec (99986880bps) errors: 0




To learn more you can visit here:



http://www.linuxfoundation.org/en/Net:Pktgen

1 comment:

Ulver said...

why your code has soo many blank lines?