standardize ssh auth
[yardstick.git] / yardstick / benchmark / scenarios / networking / pktgen_benchmark.bash
1 #!/bin/sh
2
3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 set -e
13
14 # Commandline arguments
15 DST_IP=$1         # destination IP address
16 NUM_PORTS=$2      # number of source ports
17 PKT_SIZE=$3       # packet size
18 DURATION=$4       # test duration (seconds)
19
20 # Configuration
21 UDP_SRC_MIN=1000                               # UDP source port min
22 UDP_SRC_MAX=$(( UDP_SRC_MIN + NUM_PORTS - 1 )) # UDP source port max
23 UDP_DST_MIN=1000                               # UDP destination port min
24 UDP_DST_MAX=$(( UDP_DST_MIN + NUM_PORTS ))     # UDP destination port max
25
26 # helper function to send commands to pktgen
27 pgset()
28 {
29     local result
30
31     echo $1 > $PGDEV
32
33     result=$(cat $PGDEV | fgrep "Result: OK:")
34     if [ "$result" = "" ]; then
35         cat $PGDEV | fgrep "Result:" >/dev/stderr
36         exit 1
37     fi
38 }
39
40 # configure pktgen (see pktgen doc for details)
41 pgconfig()
42 {
43     #
44     # Thread commands
45     #
46
47     PGDEV=/proc/net/pktgen/kpktgend_0
48
49     # Remove all devices from this thread
50     pgset "rem_device_all"
51
52     # Add device to thread
53     pgset "add_device $DEV"
54
55     #
56     # Device commands
57     #
58
59     PGDEV=/proc/net/pktgen/$DEV
60
61     # 0 means continious sends untill explicitly stopped
62     pgset "count 0"
63
64     # use single SKB for all transmits
65     pgset "clone_skb 0"
66
67     # packet size, NIC adds 4 bytes CRC
68     pgset "pkt_size $PKT_SIZE"
69
70     # random address within the min-max range
71     pgset "flag IPDST_RND UDPSRC_RND UDPDST_RND"
72
73     # destination IP
74     pgset "dst_min $DST_IP"
75     pgset "dst_max $DST_IP"
76
77     # destination MAC address
78     pgset "dst_mac $MAC"
79
80     # source UDP port range
81     pgset "udp_src_min $UDP_SRC_MIN"
82     pgset "udp_src_max $UDP_SRC_MAX"
83
84     # destination UDP port range
85     pgset "udp_dst_min $UDP_DST_MIN"
86     pgset "udp_dst_max $UDP_DST_MAX"
87 }
88
89 # run pktgen
90 pgrun()
91 {
92     # Time to run, result can be vieved in /proc/net/pktgen/$DEV
93     PGDEV=/proc/net/pktgen/pgctrl
94     # Will hang, Ctrl-C or SIGINT to stop
95     pgset "start" start
96 }
97
98 # run pktgen for ${DURATION} seconds
99 run_test()
100 {
101     pgrun &
102     pid=$!
103
104     sleep $DURATION
105
106     kill -INT $pid
107
108     wait; sleep 1
109 }
110
111 # write the result to stdout in json format
112 output_json()
113 {
114     sent=$(awk '/^Result:/{print $5}' <$PGDEV)
115     pps=$(awk 'match($0,/'\([0-9]+\)pps'/, a) {print a[1]}' <$PGDEV)
116     errors=$(awk '/errors:/{print $5}' <$PGDEV)
117
118     flows=$(( NUM_PORTS * (NUM_PORTS + 1) ))
119
120     echo { '"packets_sent"':$sent , '"packets_per_second"':$pps, '"flows"':$flows, '"errors"':$errors }
121 }
122
123 # main entry
124 main()
125 {
126     modprobe pktgen
127
128     ping -c 3 $DST_IP >/dev/null
129
130     # destination MAC address
131     MAC=`arp -n | grep -w $DST_IP | awk '{print $3}'`
132
133     # outgoing interface
134     DEV=`arp -n | grep -w $DST_IP | awk '{print $5}'`
135
136     # setup the test
137     pgconfig
138
139     # run the test
140     run_test >/dev/null
141
142     PGDEV=/proc/net/pktgen/$DEV
143
144     # check result
145     result=$(cat $PGDEV | fgrep "Result: OK:")
146     if [ "$result" = "" ]; then
147          cat $PGDEV | fgrep Result: >/dev/stderr
148          exit 1
149     fi
150
151     # output result
152     output_json
153 }
154
155 main
156