aboutsummaryrefslogtreecommitdiffstats
path: root/resources/tools/iperf/iperf_client.py
blob: 9d9ed9b197f861ea1f8c1cbd855288d97d25f1ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/python3

# Copyright (c) 2021 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This module gets a bandwith limit together with other parameters, reads
the iPerf3 configuration and sends the traffic. At the end, it measures
the packet loss and latency.
"""

import argparse
import json
import sys
import time
import subprocess

def simple_burst(args):
    """Send traffic and measure throughput.

    :param args: Named arguments from command line.
    :type args: dict
    """
    if1_process = []
    if1_results = []
    cmd = None

    if args.rate and args.frame_size:
        iperf_frame_size = args.frame_size - 18
        iperf_rate = float(args.rate)
        bandwidth = \
            int(args.frame_size) * float(iperf_rate) / args.instances

    if args.warmup_time > 0:
        try:
            for i in range(0, args.instances):
                cmd = u"exec sudo "
                cmd += f"ip netns exec {args.namespace} " if args.namespace else u""
                cmd += f"iperf3 "
                cmd += f"--client {args.host} "
                cmd += f"--bind {args.bind} "
                if args.rate and args.frame_size:
                    cmd += f"--bandwidth {bandwidth} "
                    cmd += f"--length {iperf_frame_size} "
                cmd += f"--port {5201 + i} "
                cmd += f"--parallel {args.parallel} "
                cmd += f"--time {args.warmup_time} "
                if args.affinity:
                    cmd += f"--affinity {args.affinity} "
                if args.udp:
                    cmd += f"--udp "
                cmd += f"--zerocopy "
                cmd += f"--json"
                if1_process.append(
                    subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
                )
        finally:
            for i in range(0, args.instances):
                if1, _ = if1_process[i].communicate(
                    timeout=args.warmup_time + 60)

    if1_process = []
    if1_results = []
    cmd = None

    try:
        if args.async_start:
            args.duration += 999
        for i in range(0, args.instances):
            cmd = u"exec sudo "
            cmd += f"ip netns exec {args.namespace} " if args.namespace else u""
            cmd += f"iperf3 "
            cmd += f"--client {args.host} "
            cmd += f"--bind {args.bind} "
            if args.rate and args.frame_size:
                cmd += f"--bandwidth {bandwidth} "
                cmd += f"--length {iperf_frame_size} "
            cmd += f"--port {5201 + i} "
            cmd += f"--parallel {args.parallel} "
            cmd += f"--time {args.duration} "
            if args.affinity:
                cmd += f"--affinity {args.affinity} "
            if args.udp:
                cmd += f"--udp "
            cmd += f"--zerocopy "
            cmd += f"--json"
            if1_process.append(
                subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
            )
    finally:
        if args.async_start:
            for i in range(0, args.instances):
                print(if1_process[i].pid)
        else:
            for i in range(0, args.instances):
                if1, _ = if1_process[i].communicate(timeout=args.duration + 60)
                if1_results.append(json.loads(if1))
                if1_results[i][u"end"][u"command"] = cmd
                print(f"{json.dumps(if1_results[i]['end'], indent = 4)}")


def main():
    """Main function for the traffic generator using iPerf3.

    It verifies the given command line arguments and runs "simple_burst"
    function.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument(
        u"--namespace", required=False, type=str,
        help=u"Port netns name to run iPerf client on."
    )
    parser.add_argument(
        u"--host", required=True, type=str,
        help=u"Run in client mode, connecting to an iPerf server host."
    )
    parser.add_argument(
        u"--bind", required=True, type=str,
        help=u"Client bind IP address."
    )
    parser.add_argument(
        u"--udp", action=u"store_true", default=False,
        help=u"UDP test."
    )
    parser.add_argument(
        u"--affinity", required=False, type=str,
        help=u"Set the CPU affinity, if possible."
    )
    parser.add_argument(
        u"--duration", required=True, type=float,
        help=u"Duration of traffic run in seconds (-1=infinite)."
    )
    parser.add_argument(
        u"--frame_size", required=False,
        help=u"Size of a Frame without padding and IPG."
    )
    parser.add_argument(
        u"--rate", required=False,
        help=u"Traffic rate with included units (pps)."
    )
    parser.add_argument(
        u"--traffic_directions", default=1, type=int,
        help=u"Send bi- (2) or uni- (1) directional traffic."
    )
    parser.add_argument(
        u"--warmup_time", type=float, default=5.0,
        help=u"Traffic warm-up time in seconds, (0=disable)."
    )
    parser.add_argument(
        u"--async_start", action=u"store_true", default=False,
        help=u"Non-blocking call of the script."
    )
    parser.add_argument(
        u"--instances", default=1, type=int,
        help=u"The number of simultaneous client instances."
    )
    parser.add_argument(
        u"--parallel", default=8, type=int,
        help=u"The number of simultaneous client streams."
    )

    args = parser.parse_args()

    # Currently limiting to uni- directional traffic.
    if args.traffic_directions != 1:
        print(f"Currently only uni- directional traffic is supported!")
        sys.exit(1)

    simple_burst(args)


if __name__ == u"__main__":
    main()