2 # SPDX-License-Identifier: GPL-2.0
4 # Copyright (c) 2019 Facebook
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of version 2 of the GNU General Public
8 # License as published by the Free Software Foundation.
11 echo "Script for testing HBM (Host Bandwidth Manager) framework."
12 echo "It creates a cgroup to use for testing and load a BPF program to limit"
13 echo "egress or ingress bandwidth. It then uses iperf3 or netperf to create"
14 echo "loads. The output is the goodput in Mbps (unless -D was used)."
16 echo "USAGE: $name [out] [-b=<prog>|--bpf=<prog>] [-c=<cc>|--cc=<cc>]"
17 echo " [-D] [-d=<delay>|--delay=<delay>] [--debug] [-E] [--edt]"
18 echo " [-f=<#flows>|--flows=<#flows>] [-h] [-i=<id>|--id=<id >]"
19 echo " [-l] [-N] [--no_cn] [-p=<port>|--port=<port>] [-P]"
20 echo " [-q=<qdisc>] [-R] [-s=<server>|--server=<server]"
21 echo " [-S|--stats] -t=<time>|--time=<time>] [-w] [cubic|dctcp]"
23 echo " out egress (default)"
24 echo " -b or --bpf BPF program filename to load and attach."
25 echo " Default is hbm_out_kern.o for egress,"
26 echo " -c or -cc TCP congestion control (cubic or dctcp)"
27 echo " --debug print BPF trace buffer"
28 echo " -d or --delay add a delay in ms using netem"
29 echo " -D In addition to the goodput in Mbps, it also outputs"
30 echo " other detailed information. This information is"
31 echo " test dependent (i.e. iperf3 or netperf)."
32 echo " -E enable ECN (not required for dctcp)"
33 echo " --edt use fq's Earliest Departure Time (requires fq)"
34 echo " -f or --flows number of concurrent flows (default=1)"
35 echo " -i or --id cgroup id (an integer, default is 1)"
36 echo " -N use netperf instead of iperf3"
37 echo " --no_cn Do not return CN notifications"
38 echo " -l do not limit flows using loopback"
40 echo " -p or --port iperf3 port (default is 5201)"
41 echo " -P use an iperf3 instance for each flow"
42 echo " -q use the specified qdisc"
43 echo " -r or --rate rate in Mbps (default 1s 1Gbps)"
44 echo " -R Use TCP_RR for netperf. 1st flow has req"
45 echo " size of 10KB, rest of 1MB. Reply in all"
46 echo " cases is 1 byte."
47 echo " More detailed output for each flow can be found"
48 echo " in the files netperf.<cg>.<flow>, where <cg> is the"
49 echo " cgroup id as specified with the -i flag, and <flow>"
50 echo " is the flow id starting at 1 and increasing by 1 for"
51 echo " flow (as specified by -f)."
52 echo " -s or --server hostname of netperf server. Used to create netperf"
53 echo " test traffic between to hosts (default is within host)"
54 echo " netserver must be running on the host."
55 echo " -S or --stats whether to update hbm stats (default is yes)."
56 echo " -t or --time duration of iperf3 in seconds (default=5)"
57 echo " -w Work conserving flag. cgroup can increase its"
58 echo " bandwidth beyond the rate limit specified"
59 echo " while there is available bandwidth. Current"
60 echo " implementation assumes there is only one NIC"
61 echo " (eth0), but can be extended to support multiple"
63 echo " cubic or dctcp specify which TCP CC to use"
95 function config_bpffs () {
96 if mount | grep $BPFFS > /dev/null; then
97 echo "bpffs already mounted"
99 echo "bpffs not mounted. Mounting..."
100 mount -t bpf none $BPFFS
104 function start_hbm () {
106 echo "./hbm $dir -n $id -r $rate -t $dur $flags $dbg $prog" > hbm.out
108 ./hbm $dir -n $id -r $rate -t $dur $flags $dbg $prog >> hbm.out 2>&1 &
115 # Support for upcomming ingress rate limiting
116 #in) # support for upcoming ingress rate limiting
131 flags="$flags --no_cn"
197 echo "Unknown arg:$i"
207 if [ $debug_flag -eq 1 ] ; then
215 cg_base_dir=/sys/fs/cgroup/unified
216 cg_dir="$cg_base_dir/cgroup-test-work-dir/hbm$id"
218 echo $$ >> $cg_dir/cgroup.procs
223 rm -f hbm.[0-9]*.$dir_name
224 if [ $ecn -ne 0 ] ; then
225 sysctl -w -q -n net.ipv4.tcp_ecn=1
228 if [ $use_netperf -eq 0 ] ; then
229 cur_cc=`sysctl -n net.ipv4.tcp_congestion_control`
230 if [ "$cc" != "x" ] ; then
231 sysctl -w -q -n net.ipv4.tcp_congestion_control=$cc
235 if [ "$netem" -ne "0" ] ; then
236 if [ "$qdisc" != "" ] ; then
237 echo "WARNING: Ignoring -q options because -d option used"
239 tc qdisc del dev lo root > /dev/null 2>&1
240 tc qdisc add dev lo root netem delay $netem\ms > /dev/null 2>&1
241 elif [ "$qdisc" != "" ] ; then
242 tc qdisc del dev eth0 root > /dev/null 2>&1
243 tc qdisc add dev eth0 root $qdisc > /dev/null 2>&1
249 if [ $use_netperf -ne 0 ] ; then
250 if [ "$server" != "" ] ; then
255 ( ping6 -i 0.2 -c $m $hn > ping.out 2>&1 ) &
257 if [ $use_netperf -ne 0 ] ; then
258 begNetserverPid=`ps ax | grep netserver | grep --invert-match "grep" | \
260 if [ "$begNetserverPid" == "" ] ; then
261 if [ "$server" == "" ] ; then
262 ( ./netserver > /dev/null 2>&1) &
267 if [ "$server" == "" ] ; then
272 if [ "$cc" == "x" ] ; then
278 while [ $flow_cnt -le $flows ] ; do
279 if [ $rr -ne 0 ] ; then
281 if [ $flow_cnt -eq 1 ] ; then
284 if [ "$dir" == "-i" ] ; then
288 ( ./netperf -H $np_server -l $dur -f m -j -t TCP_RR -- -r $reqSize,$replySize $np_cc -k P50_lATENCY,P90_LATENCY,LOCAL_TRANSPORT_RETRANS,REMOTE_TRANSPORT_RETRANS,LOCAL_SEND_THROUGHPUT,LOCAL_RECV_THROUGHPUT,REQUEST_SIZE,RESPONSE_SIZE > netperf.$id.$flow_cnt ) &
290 if [ "$dir" == "-i" ] ; then
291 ( ./netperf -H $np_server -l $dur -f m -j -t TCP_RR -- -r 1,10M $np_cc -k P50_LATENCY,P90_LATENCY,LOCAL_TRANSPORT_RETRANS,LOCAL_SEND_THROUGHPUT,REMOTE_TRANSPORT_RETRANS,REMOTE_SEND_THROUGHPUT,REQUEST_SIZE,RESPONSE_SIZE > netperf.$id.$flow_cnt ) &
293 ( ./netperf -H $np_server -l $dur -f m -j -t TCP_STREAM -- $np_cc -k P50_lATENCY,P90_LATENCY,LOCAL_TRANSPORT_RETRANS,LOCAL_SEND_THROUGHPUT,REQUEST_SIZE,RESPONSE_SIZE > netperf.$id.$flow_cnt ) &
296 flow_cnt=$[flow_cnt+1]
299 # sleep for duration of test (plus some buffer)
303 # force graceful termination of netperf
311 if [ $details -ne 0 ] ; then
313 echo "Details for HBM in cgroup $id"
314 if [ $do_stats -eq 1 ] ; then
315 if [ -e hbm.$id.$dir_name ] ; then
316 cat hbm.$id.$dir_name
320 while [ $flow_cnt -le $flows ] ; do
321 if [ "$dir" == "-i" ] ; then
322 r=`cat netperf.$id.$flow_cnt | grep -o "REMOTE_SEND_THROUGHPUT=[0-9]*" | grep -o "[0-9]*"`
324 r=`cat netperf.$id.$flow_cnt | grep -o "LOCAL_SEND_THROUGHPUT=[0-9]*" | grep -o "[0-9]*"`
326 echo "rate for flow $flow_cnt: $r"
328 if [ $details -ne 0 ] ; then
330 echo "Details for cgroup $id, flow $flow_cnt"
331 cat netperf.$id.$flow_cnt
333 flow_cnt=$[flow_cnt+1]
335 if [ $details -ne 0 ] ; then
337 delay=`grep "avg" ping.out | grep -o "= [0-9.]*/[0-9.]*" | grep -o "[0-9.]*$"`
338 echo "PING AVG DELAY:$delay"
339 echo "AGGREGATE_GOODPUT:$rate"
343 elif [ $multi_iperf -eq 0 ] ; then
344 (iperf3 -s -p $port -1 > /dev/null 2>&1) &
346 iperf3 -c $host -p $port -i 0 -P $flows -f m -t $dur > iperf.$id
347 rates=`grep receiver iperf.$id | grep -o "[0-9.]* Mbits" | grep -o "^[0-9]*"`
348 rate=`echo $rates | grep -o "[0-9]*$"`
350 if [ $details -ne 0 ] ; then
352 echo "Details for HBM in cgroup $id"
353 if [ $do_stats -eq 1 ] ; then
354 if [ -e hbm.$id.$dir_name ] ; then
355 cat hbm.$id.$dir_name
358 delay=`grep "avg" ping.out | grep -o "= [0-9.]*/[0-9.]*" | grep -o "[0-9.]*$"`
359 echo "PING AVG DELAY:$delay"
360 echo "AGGREGATE_GOODPUT:$rate"
366 while [ $flow_cnt -le $flows ] ; do
367 (iperf3 -s -p $port -1 > /dev/null 2>&1) &
368 ( iperf3 -c $host -p $port -i 0 -P 1 -f m -t $dur | grep receiver | grep -o "[0-9.]* Mbits" | grep -o "^[0-9]*" | grep -o "[0-9]*$" > iperf3.$id.$flow_cnt ) &
370 flow_cnt=$[flow_cnt+1]
376 if [ $details -ne 0 ] ; then
378 echo "Details for HBM in cgroup $id"
379 if [ $do_stats -eq 1 ] ; then
380 if [ -e hbm.$id.$dir_name ] ; then
381 cat hbm.$id.$dir_name
386 while [ $flow_cnt -le $flows ] ; do
387 r=`cat iperf3.$id.$flow_cnt`
388 # echo "rate for flow $flow_cnt: $r"
389 if [ $details -ne 0 ] ; then
390 echo "Rate for cgroup $id, flow $flow_cnt LOCAL_SEND_THROUGHPUT=$r"
393 flow_cnt=$[flow_cnt+1]
395 if [ $details -ne 0 ] ; then
396 delay=`grep "avg" ping.out | grep -o "= [0-9.]*/[0-9.]*" | grep -o "[0-9.]*$"`
397 echo "PING AVG DELAY:$delay"
398 echo "AGGREGATE_GOODPUT:$rate"
404 if [ $use_netperf -eq 0 ] ; then
405 sysctl -w -q -n net.ipv4.tcp_congestion_control=$cur_cc
407 if [ $ecn -ne 0 ] ; then
408 sysctl -w -q -n net.ipv4.tcp_ecn=0
410 if [ "$netem" -ne "0" ] ; then
411 tc qdisc del dev lo root > /dev/null 2>&1
413 if [ "$qdisc" != "" ] ; then
414 tc qdisc del dev eth0 root > /dev/null 2>&1
418 hbmPid=`ps ax | grep "hbm " | grep --invert-match "grep" | awk '{ print $1 }'`
419 if [ "$hbmPid" == "$hbm_pid" ] ; then
425 # Detach any pinned BPF programs that may have lingered
428 if [ $use_netperf -ne 0 ] ; then
429 if [ "$server" == "" ] ; then
430 if [ "$begNetserverPid" == "" ] ; then
431 netserverPid=`ps ax | grep netserver | grep --invert-match "grep" | awk '{ print $1 }'`
432 if [ "$netserverPid" != "" ] ; then