do_hbm_test.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Copyright (c) 2019 Facebook
  5. #
  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.
  9. Usage() {
  10. echo "Script for testing HBM (Host Bandwidth Manager) framework."
  11. echo "It creates a cgroup to use for testing and load a BPF program to limit"
  12. echo "egress or ingress bandwidht. It then uses iperf3 or netperf to create"
  13. echo "loads. The output is the goodput in Mbps (unless -D was used)."
  14. echo ""
  15. echo "USAGE: $name [out] [-b=<prog>|--bpf=<prog>] [-c=<cc>|--cc=<cc>]"
  16. echo " [-D] [-d=<delay>|--delay=<delay>] [--debug] [-E] [--edt]"
  17. echo " [-f=<#flows>|--flows=<#flows>] [-h] [-i=<id>|--id=<id >]"
  18. echo " [-l] [-N] [--no_cn] [-p=<port>|--port=<port>] [-P]"
  19. echo " [-q=<qdisc>] [-R] [-s=<server>|--server=<server]"
  20. echo " [-S|--stats] -t=<time>|--time=<time>] [-w] [cubic|dctcp]"
  21. echo " Where:"
  22. echo " out egress (default)"
  23. echo " -b or --bpf BPF program filename to load and attach."
  24. echo " Default is hbm_out_kern.o for egress,"
  25. echo " -c or -cc TCP congestion control (cubic or dctcp)"
  26. echo " --debug print BPF trace buffer"
  27. echo " -d or --delay add a delay in ms using netem"
  28. echo " -D In addition to the goodput in Mbps, it also outputs"
  29. echo " other detailed information. This information is"
  30. echo " test dependent (i.e. iperf3 or netperf)."
  31. echo " -E enable ECN (not required for dctcp)"
  32. echo " --edt use fq's Earliest Departure Time (requires fq)"
  33. echo " -f or --flows number of concurrent flows (default=1)"
  34. echo " -i or --id cgroup id (an integer, default is 1)"
  35. echo " -N use netperf instead of iperf3"
  36. echo " --no_cn Do not return CN notifications"
  37. echo " -l do not limit flows using loopback"
  38. echo " -h Help"
  39. echo " -p or --port iperf3 port (default is 5201)"
  40. echo " -P use an iperf3 instance for each flow"
  41. echo " -q use the specified qdisc"
  42. echo " -r or --rate rate in Mbps (default 1s 1Gbps)"
  43. echo " -R Use TCP_RR for netperf. 1st flow has req"
  44. echo " size of 10KB, rest of 1MB. Reply in all"
  45. echo " cases is 1 byte."
  46. echo " More detailed output for each flow can be found"
  47. echo " in the files netperf.<cg>.<flow>, where <cg> is the"
  48. echo " cgroup id as specified with the -i flag, and <flow>"
  49. echo " is the flow id starting at 1 and increasing by 1 for"
  50. echo " flow (as specified by -f)."
  51. echo " -s or --server hostname of netperf server. Used to create netperf"
  52. echo " test traffic between to hosts (default is within host)"
  53. echo " netserver must be running on the host."
  54. echo " -S or --stats whether to update hbm stats (default is yes)."
  55. echo " -t or --time duration of iperf3 in seconds (default=5)"
  56. echo " -w Work conserving flag. cgroup can increase its"
  57. echo " bandwidth beyond the rate limit specified"
  58. echo " while there is available bandwidth. Current"
  59. echo " implementation assumes there is only one NIC"
  60. echo " (eth0), but can be extended to support multiple"
  61. echo " NICs."
  62. echo " cubic or dctcp specify which TCP CC to use"
  63. echo " "
  64. exit
  65. }
  66. #set -x
  67. debug_flag=0
  68. args="$@"
  69. name="$0"
  70. netem=0
  71. cc=x
  72. dir="-o"
  73. dir_name="out"
  74. dur=5
  75. flows=1
  76. id=1
  77. prog=""
  78. port=5201
  79. rate=1000
  80. multi_iperf=0
  81. flow_cnt=1
  82. use_netperf=0
  83. rr=0
  84. ecn=0
  85. details=0
  86. server=""
  87. qdisc=""
  88. flags=""
  89. do_stats=0
  90. function start_hbm () {
  91. rm -f hbm.out
  92. echo "./hbm $dir -n $id -r $rate -t $dur $flags $dbg $prog" > hbm.out
  93. echo " " >> hbm.out
  94. ./hbm $dir -n $id -r $rate -t $dur $flags $dbg $prog >> hbm.out 2>&1 &
  95. echo $!
  96. }
  97. processArgs () {
  98. for i in $args ; do
  99. case $i in
  100. # Support for upcomming ingress rate limiting
  101. #in) # support for upcoming ingress rate limiting
  102. # dir="-i"
  103. # dir_name="in"
  104. # ;;
  105. out)
  106. dir="-o"
  107. dir_name="out"
  108. ;;
  109. -b=*|--bpf=*)
  110. prog="${i#*=}"
  111. ;;
  112. -c=*|--cc=*)
  113. cc="${i#*=}"
  114. ;;
  115. --no_cn)
  116. flags="$flags --no_cn"
  117. ;;
  118. --debug)
  119. flags="$flags -d"
  120. debug_flag=1
  121. ;;
  122. -d=*|--delay=*)
  123. netem="${i#*=}"
  124. ;;
  125. -D)
  126. details=1
  127. ;;
  128. -E)
  129. ecn=1
  130. ;;
  131. --edt)
  132. flags="$flags --edt"
  133. qdisc="fq"
  134. ;;
  135. -f=*|--flows=*)
  136. flows="${i#*=}"
  137. ;;
  138. -i=*|--id=*)
  139. id="${i#*=}"
  140. ;;
  141. -l)
  142. flags="$flags -l"
  143. ;;
  144. -N)
  145. use_netperf=1
  146. ;;
  147. -p=*|--port=*)
  148. port="${i#*=}"
  149. ;;
  150. -P)
  151. multi_iperf=1
  152. ;;
  153. -q=*)
  154. qdisc="${i#*=}"
  155. ;;
  156. -r=*|--rate=*)
  157. rate="${i#*=}"
  158. ;;
  159. -R)
  160. rr=1
  161. ;;
  162. -s=*|--server=*)
  163. server="${i#*=}"
  164. ;;
  165. -S|--stats)
  166. flags="$flags -s"
  167. do_stats=1
  168. ;;
  169. -t=*|--time=*)
  170. dur="${i#*=}"
  171. ;;
  172. -w)
  173. flags="$flags -w"
  174. ;;
  175. cubic)
  176. cc=cubic
  177. ;;
  178. dctcp)
  179. cc=dctcp
  180. ;;
  181. *)
  182. echo "Unknown arg:$i"
  183. Usage
  184. ;;
  185. esac
  186. done
  187. }
  188. processArgs
  189. if [ $debug_flag -eq 1 ] ; then
  190. rm -f hbm_out.log
  191. fi
  192. hbm_pid=$(start_hbm)
  193. usleep 100000
  194. host=`hostname`
  195. cg_base_dir=/sys/fs/cgroup
  196. cg_dir="$cg_base_dir/cgroup-test-work-dir/hbm$id"
  197. echo $$ >> $cg_dir/cgroup.procs
  198. ulimit -l unlimited
  199. rm -f ss.out
  200. rm -f hbm.[0-9]*.$dir_name
  201. if [ $ecn -ne 0 ] ; then
  202. sysctl -w -q -n net.ipv4.tcp_ecn=1
  203. fi
  204. if [ $use_netperf -eq 0 ] ; then
  205. cur_cc=`sysctl -n net.ipv4.tcp_congestion_control`
  206. if [ "$cc" != "x" ] ; then
  207. sysctl -w -q -n net.ipv4.tcp_congestion_control=$cc
  208. fi
  209. fi
  210. if [ "$netem" -ne "0" ] ; then
  211. if [ "$qdisc" != "" ] ; then
  212. echo "WARNING: Ignoring -q options because -d option used"
  213. fi
  214. tc qdisc del dev lo root > /dev/null 2>&1
  215. tc qdisc add dev lo root netem delay $netem\ms > /dev/null 2>&1
  216. elif [ "$qdisc" != "" ] ; then
  217. tc qdisc del dev eth0 root > /dev/null 2>&1
  218. tc qdisc add dev eth0 root $qdisc > /dev/null 2>&1
  219. fi
  220. n=0
  221. m=$[$dur * 5]
  222. hn="::1"
  223. if [ $use_netperf -ne 0 ] ; then
  224. if [ "$server" != "" ] ; then
  225. hn=$server
  226. fi
  227. fi
  228. ( ping6 -i 0.2 -c $m $hn > ping.out 2>&1 ) &
  229. if [ $use_netperf -ne 0 ] ; then
  230. begNetserverPid=`ps ax | grep netserver | grep --invert-match "grep" | \
  231. awk '{ print $1 }'`
  232. if [ "$begNetserverPid" == "" ] ; then
  233. if [ "$server" == "" ] ; then
  234. ( ./netserver > /dev/null 2>&1) &
  235. usleep 100000
  236. fi
  237. fi
  238. flow_cnt=1
  239. if [ "$server" == "" ] ; then
  240. np_server=$host
  241. else
  242. np_server=$server
  243. fi
  244. if [ "$cc" == "x" ] ; then
  245. np_cc=""
  246. else
  247. np_cc="-K $cc,$cc"
  248. fi
  249. replySize=1
  250. while [ $flow_cnt -le $flows ] ; do
  251. if [ $rr -ne 0 ] ; then
  252. reqSize=1M
  253. if [ $flow_cnt -eq 1 ] ; then
  254. reqSize=10K
  255. fi
  256. if [ "$dir" == "-i" ] ; then
  257. replySize=$reqSize
  258. reqSize=1
  259. fi
  260. ( ./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 ) &
  261. else
  262. if [ "$dir" == "-i" ] ; then
  263. ( ./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 ) &
  264. else
  265. ( ./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 ) &
  266. fi
  267. fi
  268. flow_cnt=$[flow_cnt+1]
  269. done
  270. # sleep for duration of test (plus some buffer)
  271. n=$[dur+2]
  272. sleep $n
  273. # force graceful termination of netperf
  274. pids=`pgrep netperf`
  275. for p in $pids ; do
  276. kill -SIGALRM $p
  277. done
  278. flow_cnt=1
  279. rate=0
  280. if [ $details -ne 0 ] ; then
  281. echo ""
  282. echo "Details for HBM in cgroup $id"
  283. if [ $do_stats -eq 1 ] ; then
  284. if [ -e hbm.$id.$dir_name ] ; then
  285. cat hbm.$id.$dir_name
  286. fi
  287. fi
  288. fi
  289. while [ $flow_cnt -le $flows ] ; do
  290. if [ "$dir" == "-i" ] ; then
  291. r=`cat netperf.$id.$flow_cnt | grep -o "REMOTE_SEND_THROUGHPUT=[0-9]*" | grep -o "[0-9]*"`
  292. else
  293. r=`cat netperf.$id.$flow_cnt | grep -o "LOCAL_SEND_THROUGHPUT=[0-9]*" | grep -o "[0-9]*"`
  294. fi
  295. echo "rate for flow $flow_cnt: $r"
  296. rate=$[rate+r]
  297. if [ $details -ne 0 ] ; then
  298. echo "-----"
  299. echo "Details for cgroup $id, flow $flow_cnt"
  300. cat netperf.$id.$flow_cnt
  301. fi
  302. flow_cnt=$[flow_cnt+1]
  303. done
  304. if [ $details -ne 0 ] ; then
  305. echo ""
  306. delay=`grep "avg" ping.out | grep -o "= [0-9.]*/[0-9.]*" | grep -o "[0-9.]*$"`
  307. echo "PING AVG DELAY:$delay"
  308. echo "AGGREGATE_GOODPUT:$rate"
  309. else
  310. echo $rate
  311. fi
  312. elif [ $multi_iperf -eq 0 ] ; then
  313. (iperf3 -s -p $port -1 > /dev/null 2>&1) &
  314. usleep 100000
  315. iperf3 -c $host -p $port -i 0 -P $flows -f m -t $dur > iperf.$id
  316. rates=`grep receiver iperf.$id | grep -o "[0-9.]* Mbits" | grep -o "^[0-9]*"`
  317. rate=`echo $rates | grep -o "[0-9]*$"`
  318. if [ $details -ne 0 ] ; then
  319. echo ""
  320. echo "Details for HBM in cgroup $id"
  321. if [ $do_stats -eq 1 ] ; then
  322. if [ -e hbm.$id.$dir_name ] ; then
  323. cat hbm.$id.$dir_name
  324. fi
  325. fi
  326. delay=`grep "avg" ping.out | grep -o "= [0-9.]*/[0-9.]*" | grep -o "[0-9.]*$"`
  327. echo "PING AVG DELAY:$delay"
  328. echo "AGGREGATE_GOODPUT:$rate"
  329. else
  330. echo $rate
  331. fi
  332. else
  333. flow_cnt=1
  334. while [ $flow_cnt -le $flows ] ; do
  335. (iperf3 -s -p $port -1 > /dev/null 2>&1) &
  336. ( 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 ) &
  337. port=$[port+1]
  338. flow_cnt=$[flow_cnt+1]
  339. done
  340. n=$[dur+1]
  341. sleep $n
  342. flow_cnt=1
  343. rate=0
  344. if [ $details -ne 0 ] ; then
  345. echo ""
  346. echo "Details for HBM in cgroup $id"
  347. if [ $do_stats -eq 1 ] ; then
  348. if [ -e hbm.$id.$dir_name ] ; then
  349. cat hbm.$id.$dir_name
  350. fi
  351. fi
  352. fi
  353. while [ $flow_cnt -le $flows ] ; do
  354. r=`cat iperf3.$id.$flow_cnt`
  355. # echo "rate for flow $flow_cnt: $r"
  356. if [ $details -ne 0 ] ; then
  357. echo "Rate for cgroup $id, flow $flow_cnt LOCAL_SEND_THROUGHPUT=$r"
  358. fi
  359. rate=$[rate+r]
  360. flow_cnt=$[flow_cnt+1]
  361. done
  362. if [ $details -ne 0 ] ; then
  363. delay=`grep "avg" ping.out | grep -o "= [0-9.]*/[0-9.]*" | grep -o "[0-9.]*$"`
  364. echo "PING AVG DELAY:$delay"
  365. echo "AGGREGATE_GOODPUT:$rate"
  366. else
  367. echo $rate
  368. fi
  369. fi
  370. if [ $use_netperf -eq 0 ] ; then
  371. sysctl -w -q -n net.ipv4.tcp_congestion_control=$cur_cc
  372. fi
  373. if [ $ecn -ne 0 ] ; then
  374. sysctl -w -q -n net.ipv4.tcp_ecn=0
  375. fi
  376. if [ "$netem" -ne "0" ] ; then
  377. tc qdisc del dev lo root > /dev/null 2>&1
  378. fi
  379. if [ "$qdisc" != "" ] ; then
  380. tc qdisc del dev eth0 root > /dev/null 2>&1
  381. fi
  382. sleep 2
  383. hbmPid=`ps ax | grep "hbm " | grep --invert-match "grep" | awk '{ print $1 }'`
  384. if [ "$hbmPid" == "$hbm_pid" ] ; then
  385. kill $hbm_pid
  386. fi
  387. sleep 1
  388. # Detach any BPF programs that may have lingered
  389. ttx=`bpftool cgroup tree | grep hbm`
  390. v=2
  391. for x in $ttx ; do
  392. if [ "${x:0:36}" == "/sys/fs/cgroup/cgroup-test-work-dir/" ] ; then
  393. cg=$x ; v=0
  394. else
  395. if [ $v -eq 0 ] ; then
  396. id=$x ; v=1
  397. else
  398. if [ $v -eq 1 ] ; then
  399. type=$x ; bpftool cgroup detach $cg $type id $id
  400. v=0
  401. fi
  402. fi
  403. fi
  404. done
  405. if [ $use_netperf -ne 0 ] ; then
  406. if [ "$server" == "" ] ; then
  407. if [ "$begNetserverPid" == "" ] ; then
  408. netserverPid=`ps ax | grep netserver | grep --invert-match "grep" | awk '{ print $1 }'`
  409. if [ "$netserverPid" != "" ] ; then
  410. kill $netserverPid
  411. fi
  412. fi
  413. fi
  414. fi
  415. exit