aboutsummaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorJesper Dangaard Brouer <brouer@redhat.com>2015-05-21 06:17:19 -0400
committerDavid S. Miller <davem@davemloft.net>2015-05-22 23:59:16 -0400
commitb64b0d1e64959691c1f4067a05fdb541d453ed6a (patch)
treed9db1b3e09c72c8101cc7ca4a0ab71142f620f8a /samples
parent4020726479ff799318a5aa188b81d79df86a0ea3 (diff)
pktgen: new pktgen helper functions for samples scripts
Preparing for removing existing samples/pktgen/ scripts, and replacing these with easier to use samples. This commit provides two helper shell files, that can be "included" by shell source'ing. Namely "functions.sh" and "parameters.sh". The parameters.sh file support easy and consistant parameter parsing across the sample scripts. Usage example is printed on errors. The functions.sh file provides, three new shell functions for configuring the different components of pktgen: pg_ctrl(), pg_thread() and pg_set(). A slightly improved version of the old pgset() function is also provided for backwards compat. The new functions correspond to pktgens different components. * pg_ctrl() control "pgctrl" (/proc/net/pktgen/pgctrl) * pg_thread() control the kernel threads and binding to devices * pg_set() control setup of individual devices These changes are borrowed from: https://github.com/netoptimizer/network-testing/tree/master/pktgen Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples')
-rw-r--r--samples/pktgen/README.rst43
-rw-r--r--samples/pktgen/functions.sh121
-rw-r--r--samples/pktgen/parameters.sh97
3 files changed, 261 insertions, 0 deletions
diff --git a/samples/pktgen/README.rst b/samples/pktgen/README.rst
new file mode 100644
index 000000000000..8365c4e5c513
--- /dev/null
+++ b/samples/pktgen/README.rst
@@ -0,0 +1,43 @@
1Sample and benchmark scripts for pktgen (packet generator)
2==========================================================
3This directory contains some pktgen sample and benchmark scripts, that
4can easily be copied and adjusted for your own use-case.
5
6General doc is located in kernel: Documentation/networking/pktgen.txt
7
8Helper include files
9====================
10This directory contains two helper shell files, that can be "included"
11by shell source'ing. Namely "functions.sh" and "parameters.sh".
12
13Common parameters
14-----------------
15The parameters.sh file support easy and consistant parameter parsing
16across the sample scripts. Usage example is printed on errors::
17
18 Usage: ./pktgen_sample01_simple.sh [-vx] -i ethX
19 -i : ($DEV) output interface/device (required)
20 -s : ($PKT_SIZE) packet size
21 -d : ($DEST_IP) destination IP
22 -m : ($DST_MAC) destination MAC-addr
23 -t : ($THREADS) threads to start
24 -c : ($SKB_CLONE) SKB clones send before alloc new SKB
25 -b : ($BURST) HW level bursting of SKBs
26 -v : ($VERBOSE) verbose
27 -x : ($DEBUG) debug
28
29The global variable being set is also listed. E.g. the required
30interface/device parameter "-i" sets variable $DEV.
31
32Common functions
33----------------
34The functions.sh file provides; Three different shell functions for
35configuring the different components of pktgen: pg_ctrl(), pg_thread()
36and pg_set().
37
38These functions correspond to pktgens different components.
39 * pg_ctrl() control "pgctrl" (/proc/net/pktgen/pgctrl)
40 * pg_thread() control the kernel threads and binding to devices
41 * pg_set() control setup of individual devices
42
43See sample scripts for usage examples.
diff --git a/samples/pktgen/functions.sh b/samples/pktgen/functions.sh
new file mode 100644
index 000000000000..205e4cde4601
--- /dev/null
+++ b/samples/pktgen/functions.sh
@@ -0,0 +1,121 @@
1#
2# Common functions used by pktgen scripts
3# - Depending on bash 3 (or higher) syntax
4#
5# Author: Jesper Dangaaard Brouer
6# License: GPL
7
8## -- General shell logging cmds --
9function err() {
10 local exitcode=$1
11 shift
12 echo "ERROR: $@" >&2
13 exit $exitcode
14}
15
16function warn() {
17 echo "WARN : $@" >&2
18}
19
20function info() {
21 if [[ -n "$VERBOSE" ]]; then
22 echo "INFO : $@" >&2
23 fi
24}
25
26## -- Pktgen proc config commands -- ##
27export PROC_DIR=/proc/net/pktgen
28#
29# Three different shell functions for configuring the different
30# components of pktgen:
31# pg_ctrl(), pg_thread() and pg_set().
32#
33# These functions correspond to pktgens different components.
34# * pg_ctrl() control "pgctrl" (/proc/net/pktgen/pgctrl)
35# * pg_thread() control the kernel threads and binding to devices
36# * pg_set() control setup of individual devices
37function pg_ctrl() {
38 local proc_file="pgctrl"
39 proc_cmd ${proc_file} "$@"
40}
41
42function pg_thread() {
43 local thread=$1
44 local proc_file="kpktgend_${thread}"
45 shift
46 proc_cmd ${proc_file} "$@"
47}
48
49function pg_set() {
50 local dev=$1
51 local proc_file="$dev"
52 shift
53 proc_cmd ${proc_file} "$@"
54}
55
56# More generic replacement for pgset(), that does not depend on global
57# variable for proc file.
58function proc_cmd() {
59 local result
60 local proc_file=$1
61 # after shift, the remaining args are contained in $@
62 shift
63 local proc_ctrl=${PROC_DIR}/$proc_file
64 if [[ ! -e "$proc_ctrl" ]]; then
65 err 3 "proc file:$proc_ctrl does not exists (dev added to thread?)"
66 else
67 if [[ ! -w "$proc_ctrl" ]]; then
68 err 4 "proc file:$proc_ctrl not writable, not root?!"
69 fi
70 fi
71
72 if [[ "$DEBUG" == "yes" ]]; then
73 echo "cmd: $@ > $proc_ctrl"
74 fi
75 # Quoting of "$@" is important for space expansion
76 echo "$@" > "$proc_ctrl"
77 local status=$?
78
79 result=$(grep "Result: OK:" $proc_ctrl)
80 # Due to pgctrl, cannot use exit code $? from grep
81 if [[ "$result" == "" ]]; then
82 grep "Result:" $proc_ctrl >&2
83 fi
84 if (( $status != 0 )); then
85 err 5 "Write error($status) occurred cmd: \"$@ > $proc_ctrl\""
86 fi
87}
88
89# Old obsolete "pgset" function, with slightly improved err handling
90function pgset() {
91 local result
92
93 if [[ "$DEBUG" == "yes" ]]; then
94 echo "cmd: $1 > $PGDEV"
95 fi
96 echo $1 > $PGDEV
97 local status=$?
98
99 result=`cat $PGDEV | fgrep "Result: OK:"`
100 if [[ "$result" == "" ]]; then
101 cat $PGDEV | fgrep Result:
102 fi
103 if (( $status != 0 )); then
104 err 5 "Write error($status) occurred cmd: \"$1 > $PGDEV\""
105 fi
106}
107
108## -- General shell tricks --
109
110function root_check_run_with_sudo() {
111 # Trick so, program can be run as normal user, will just use "sudo"
112 # call as root_check_run_as_sudo "$@"
113 if [ "$EUID" -ne 0 ]; then
114 if [ -x $0 ]; then # Directly executable use sudo
115 info "Not root, running with sudo"
116 sudo "$0" "$@"
117 exit $?
118 fi
119 err 4 "cannot perform sudo run of $0"
120 fi
121}
diff --git a/samples/pktgen/parameters.sh b/samples/pktgen/parameters.sh
new file mode 100644
index 000000000000..33b70fdd5a4a
--- /dev/null
+++ b/samples/pktgen/parameters.sh
@@ -0,0 +1,97 @@
1#
2# Common parameter parsing for pktgen scripts
3#
4
5function usage() {
6 echo ""
7 echo "Usage: $0 [-vx] -i ethX"
8 echo " -i : (\$DEV) output interface/device (required)"
9 echo " -s : (\$PKT_SIZE) packet size"
10 echo " -d : (\$DEST_IP) destination IP"
11 echo " -m : (\$DST_MAC) destination MAC-addr"
12 echo " -t : (\$THREADS) threads to start"
13 echo " -c : (\$SKB_CLONE) SKB clones send before alloc new SKB"
14 echo " -b : (\$BURST) HW level bursting of SKBs"
15 echo " -v : (\$VERBOSE) verbose"
16 echo " -x : (\$DEBUG) debug"
17 echo ""
18}
19
20## --- Parse command line arguments / parameters ---
21## echo "Commandline options:"
22while getopts "s:i:d:m:t:c:b:vxh" option; do
23 case $option in
24 i) # interface
25 export DEV=$OPTARG
26 info "Output device set to: DEV=$DEV"
27 ;;
28 s)
29 export PKT_SIZE=$OPTARG
30 info "Packet size set to: PKT_SIZE=$PKT_SIZE bytes"
31 ;;
32 d) # destination IP
33 export DEST_IP=$OPTARG
34 info "Destination IP set to: DEST_IP=$DEST_IP"
35 ;;
36 m) # MAC
37 export DST_MAC=$OPTARG
38 info "Destination MAC set to: DST_MAC=$DST_MAC"
39 ;;
40 t)
41 export THREADS=$OPTARG
42 export CPU_THREADS=$OPTARG
43 let "CPU_THREADS -= 1"
44 info "Number of threads to start: $THREADS (0 to $CPU_THREADS)"
45 ;;
46 c)
47 export CLONE_SKB=$OPTARG
48 info "CLONE_SKB=$CLONE_SKB"
49 ;;
50 b)
51 export BURST=$OPTARG
52 info "SKB bursting: BURST=$BURST"
53 ;;
54 v)
55 export VERBOSE=yes
56 info "Verbose mode: VERBOSE=$VERBOSE"
57 ;;
58 x)
59 export DEBUG=yes
60 info "Debug mode: DEBUG=$DEBUG"
61 ;;
62 h|?|*)
63 usage;
64 err 2 "[ERROR] Unknown parameters!!!"
65 esac
66done
67shift $(( $OPTIND - 1 ))
68
69if [ -z "$PKT_SIZE" ]; then
70 # NIC adds 4 bytes CRC
71 export PKT_SIZE=60
72 info "Default packet size set to: set to: $PKT_SIZE bytes"
73fi
74
75if [ -z "$THREADS" ]; then
76 # Zero CPU threads means one thread, because CPU numbers are zero indexed
77 export CPU_THREADS=0
78 export THREADS=1
79fi
80
81if [ -z "$DEV" ]; then
82 usage
83 err 2 "Please specify output device"
84fi
85
86if [ -z "$DST_MAC" ]; then
87 warn "Missing destination MAC address"
88fi
89
90if [ -z "$DEST_IP" ]; then
91 warn "Missing destination IP address"
92fi
93
94if [ ! -d /proc/net/pktgen ]; then
95 info "Loading kernel module: pktgen"
96 modprobe pktgen
97fi