Linux iperf3

iperf3 homepage

download iperf3

iperf3是一款Linux下的网络吞吐量测量工具,它支持测量TCP、UDP、SCTP吞吐量。

iperf3有两种运行模式:client、server。
使用iperf3进行测试时,需要在一台服务器以server模式运行iperf3,在另一台机器以client模式运行iperf3.
默认情况下,数据由client端发送给server端,client端测量发送速率,server端测量接收速率。使用-R参数可以反转测试。

测试过程中,iperf3 client会首先与server端创建控制连接(TCP连接,服务器端默认端口是5201,可以使用-p参数修改默认端口),控制连接用来同步测试参数、控制测试的开始和结束、以及发送测试结果。

控制连接创建成功之后,就会通过创建其他的TCP、UDP、SCTP连接发送测试数据。

测试过程中,默认每隔一秒更新一次测试结果(可以使用-i参数设置更新间隔),运行结束后会打印一个最终结果。

所有选项

# iperf3的选项分为三种,
# 1、只有server能使用的选项
# 2、只有client能使用的选项
# 3、通用选项

[root@dell-per740-24 ~]# iperf3 -h
Usage: iperf3 [-s|-c host] [options]
       iperf3 [-h|--help] [-v|--version]

Server or Client:
  -p, --port      #         server port to listen on/connect to
  -f, --format   [kmgtKMGT] format to report: Kbits, Mbits, Gbits, Tbits
  -i, --interval  #         seconds between periodic throughput reports
  -F, --file name           xmit/recv the specified file
  -A, --affinity n/n,m      set CPU affinity
  -B, --bind      <host>    bind to the interface associated with the address <host>
  -V, --verbose             more detailed output
  -J, --json                output in JSON format
  --logfile f               send output to a log file
  --forceflush              force flushing output at every interval
  -d, --debug               emit debugging output
  -v, --version             show version information and quit
  -h, --help                show this message and quit
Server specific:
  -s, --server              run in server mode
  -D, --daemon              run the server as a daemon
  -I, --pidfile file        write PID file
  -1, --one-off             handle one client connection then exit
  --rsa-private-key-path    path to the RSA private key used to decrypt
                            authentication credentials
  --authorized-users-path   path to the configuration file containing user
                            credentials
Client specific:
  -c, --client    <host>    run in client mode, connecting to <host>
  --sctp                    use SCTP rather than TCP
  -X, --xbind <name>        bind SCTP association to links
  --nstreams      #         number of SCTP streams
  -u, --udp                 use UDP rather than TCP
  --connect-timeout #       timeout for control connection setup (ms)
  -b, --bitrate #[KMG][/#]  target bitrate in bits/sec (0 for unlimited)
                            (default 1 Mbit/sec for UDP, unlimited for TCP)
                            (optional slash and packet count for burst mode)
  --pacing-timer #[KMG]     set the timing for pacing, in microseconds (default 1000)
  --fq-rate #[KMG]          enable fair-queuing based socket pacing in
                            bits/sec (Linux only)
  -t, --time      #         time in seconds to transmit for (default 10 secs)
  -n, --bytes     #[KMG]    number of bytes to transmit (instead of -t)
  -k, --blockcount #[KMG]   number of blocks (packets) to transmit (instead of -t or -n)
  -l, --length    #[KMG]    length of buffer to read or write
                            (default 128 KB for TCP, dynamic or 1460 for UDP)
  --cport         <port>    bind to a specific client port (TCP and UDP, default: ephemeral port)
  -P, --parallel  #         number of parallel client streams to run
  -R, --reverse             run in reverse mode (server sends, client receives)
  -w, --window    #[KMG]    set window size / socket buffer size
  -C, --congestion <algo>   set TCP congestion control algorithm (Linux and FreeBSD only)
  -M, --set-mss   #         set TCP/SCTP maximum segment size (MTU - 40 bytes)
  -N, --no-delay            set TCP/SCTP no delay, disabling Nagle's Algorithm
  -4, --version4            only use IPv4
  -6, --version6            only use IPv6
  -S, --tos N               set the IP type of service, 0-255.
                            The usual prefixes for octal and hex can be used,
                            i.e. 52, 064 and 0x34 all specify the same value.
  --dscp N or --dscp val    set the IP dscp value, either 0-63 or symbolic.
                            Numeric values can be specified in decimal,
                            octal and hex (see --tos above).
  -L, --flowlabel N         set the IPv6 flow label (only supported on Linux)
  -Z, --zerocopy            use a 'zero copy' method of sending data
  -O, --omit N              omit the first n seconds
  -T, --title str           prefix every output line with this string
  --get-server-output       get results from server
  --udp-counters-64bit      use 64-bit counters in UDP test packets
  --username                username for authentication
  --rsa-public-key-path     path to the RSA public key used to encrypt
                            authentication credentials

[KMG] indicates options that support a K/M/G suffix for kilo-, mega-, or giga-

iperf3 homepage at: http://software.es.net/iperf/
Report bugs to:     https://github.com/esnet/iperf

-s和-c选项指定运行模式

# -s表示以server模式运行
iperf3 -s

# -c表示以client模式运行
iperf3 -c $server_ip

通用选项

# -p指定端口
iperf3 -s -p 10000             //配合-s表示在服务器端开启10000端口
iperf3 -c $server_ip -p 10000  //配合-c表示客户端要向服务端的10000端口发起连接

# -i指定测试结果更新间隔,默认1s
iperf3 -c $server_ip -i 10    //每10s显示一次测试结果
iperf3 -s -i 10

# -f指定测试结果中显示的速率的单位
[root@dell-per740-24 ~]# iperf3 -c 199.10.1.2 -f M -i 5 -t 10
Connecting to host 199.10.1.2, port 5201
[  5] local 199.10.1.1 port 49034 connected to 199.10.1.2 port 5201
[ ID] Interval           Transfer     Bitrate         Retr  Cwnd
[  5]   0.00-5.00   sec  5.44 GBytes  1114 MBytes/sec  8818    389 KBytes
[  5]   5.00-10.00  sec  5.44 GBytes  1115 MBytes/sec  9868    413 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bitrate         Retr
[  5]   0.00-10.00  sec  10.9 GBytes  1114 MBytes/sec  18686             sender
[  5]   0.00-10.04  sec  10.9 GBytes  1110 MBytes/sec                  receiver

iperf Done.
[root@dell-per740-24 ~]# iperf3 -c 199.10.1.2 -f G -i 5 -t 10
Connecting to host 199.10.1.2, port 5201
[  5] local 199.10.1.1 port 49038 connected to 199.10.1.2 port 5201
[ ID] Interval           Transfer     Bitrate         Retr  Cwnd
[  5]   0.00-5.00   sec  5.42 GBytes  1.08 GBytes/sec  10758    387 KBytes
[  5]   5.00-10.00  sec  5.44 GBytes  1.09 GBytes/sec  10192    407 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bitrate         Retr
[  5]   0.00-10.00  sec  10.9 GBytes  1.09 GBytes/sec  20950             sender
[  5]   0.00-10.04  sec  10.9 GBytes  1.08 GBytes/sec                  receiver

iperf Done.

# -A把进程绑定到某个cpu
iperf3 -s -A 0              //把server端进程绑定到0号cpu
iperf3 -c $server_ip -A 0   //把客户端进程绑定到0号cpu
iperf3 -c $server_ip -A 0,1 //在客户端同时指定本地以及server端的cpu亲和性 比如0代表本地cpu亲和性,1代表server端cpu亲和性

# -B绑定端口
iperf3 -s -B 1.1.1.1         //只接收针对该地址的连接
iperf3 -c 1.1.1.1 -B 1.1.1.2 //通过1.1.1.2连接服务器的1.1.1.1

# -V 显示更详细的输出
iperf3 -c 1.1.1.1 -V

# -J 以json形式显示输出
iperf3 -c 1.1.1.1 -J

server常用选项

# -s指定以server模式运行,-p指定监听端口
iperf3 -s -p 10000

# -D 指定server以Daemon模式运行,就是说进程会在后台运行
iperf3 -s -D

# -I 常与-D配合,用于把iperf3进程id写入文件
iperf3 -s -D -I ipef3_pid.txt

# -1 第一个客户端连接并测试完成之后就退出,否则会一直运行等待客户端连接
iperf3 -s -1

client常用选项

# -c 指定服务端ip
iperf3 -c 1.1.1.1

# -u 使用udp代替tcp
iperf3 -c 1.1.1.1 -u

# --sctp 使用sctp代替tcp
iperf3 -c 1.1.1.1 --sctp

# -b 设置发送速率,对于udp,默认是1Mbps,对于TCP和SCTP是尽力而为
iperf3 -c 1.1.1.1 -b 100M     

# -P 同时发送多个流
iperf3 -c 1.1.1.1 -P 2

# -b -P配合使用
iperf3 -c 1.1.1.1 -P 2 -b 100M //两个流总共200Mbps

# -t指定测试时间,默认10s
iperf3 -c 1.1.1.1 -t 60

# -n指定发送的bytes数来代替-t参数指定的时间
iperf3 -c 1.1.1.1 -n 1000M

# --cport 指定本地端口
iperf3 -c 1.1.1.1 --cport 10001  

# -R反转测试,client端收包,server端发包
iperf3 -c 1.1.1.1 -R