-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathrun.py
executable file
·280 lines (220 loc) · 8.88 KB
/
run.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/python3
# import stl_path
from tokenize import String
from unittest import result
from zipfile import Path
from trex_stl_lib.api import *
import numpy as np
from scapy.contrib.gtp import *
import time
import json
import argparse
import subprocess
from collections import defaultdict
item = defaultdict(dict)
def create_udp_pkt_flow(size, ip_min, ip_max, nflows, field):
print("{} flow will be generated...".format(nflows))
base_pkt = Ether()/IP(src="16.0.0.1", dst="10.1.3.27")/UDP(dport=1234)
pad = max(0, size - len(base_pkt)) * 'x'
return STLPktBuilder(pkt=base_pkt/pad, vm=create_vm(ip_min, ip_max, nflows, field))
# TODO navarrothiago - update packet
def create_gtp_pkt_flow(size, ip_min, ip_max, nflows, field):
print("{} flow will be generated...".format(nflows))
base_pkt = Ether()/IP(src="172.20.16.99", dst="192.168.15.12")/UDP(dport=2152) / \
GTP_U_Header(teid=100) / \
IP(src="10.10.10.10", dst="10.1.3.27", version=4)/UDP(dport=1234)
pad = max(0, size - len(base_pkt)) * 'x'
return STLPktBuilder(pkt=base_pkt/pad, vm=create_vm(ip_min, ip_max, nflows, field))
def create_vm(ip_min, ip_max, nflows, field):
vm = STLVM()
# create a tuple var
vm.tuple_var(name="tuple", ip_min=ip_min, ip_max=ip_max,
port_min=1234, port_max=1234, limit_flows=nflows)
# write fields
vm.write(fv_name="tuple.ip", pkt_offset="IP.{}".format(field))
vm.fix_chksum()
# vm.write(fv_name="tuple.port", pkt_offset="UDP.sport")
return vm
def create_pkt(size):
# simple packet creation
print("Only one flow will be generated...")
pkt = Ether()/IP(src="10.1.2.27", dst="10.1.3.27")/UDP(dport=1234)
pad = max(0, size - len(pkt)) * 'x'
return STLPktBuilder(pkt=pkt/pad)
def simple_burst(streams, m, duration):
# create client
# c = STLClient()
# username/server can be changed those are the default
# username = common.get_current_user(),
# server = "localhost"
c = STLClient(server="localhost", sync_port=1235, async_port=1236)
passed = True
try:
# turn this on for some information
# c.set_verbose("debug")
# create one streams
# s1 = STLStream(packet=create_pkt(size, nflows), mode=STLTXCont())
# connect to server
c.connect()
# prepare our ports (my machine has 0 <--> 1 with static route)
# Acquire port 0 for $USER
c.reset(ports=[0, 1])
# add both streams to ports
c.add_streams(streams, ports=[0])
# clear the stats before injecting
c.clear_stats()
# set port 1 as promiscuous mode
c.set_port_attr(ports=[1], promiscuous=True)
# choose rate and start traffic for 10 seconds on 5 mpps
print("Running " + m + " on ports 0 for {} seconds...".format(duration))
c.start(ports=[0], mult=m, duration=duration)
run_mpstat(duration/2)
# block until done
c.wait_on_traffic(ports=[0])
# read the stats after the test
stats = c.get_stats()
# print(json.dumps(stats[0], indent=4, separators=(',', ': '), sort_keys=True))
# print(json.dumps(stats[1], indent=4, separators=(',', ': '), sort_keys=True))
# print("\n")
# print("Packets sent 0 --> 1: {0} pkts".format(stats[0]["opackets"]))
# print("Rx Mpps 0 --> 1: {0} Mpps".format(float(stats[1]["rx_pps"])/1000000))
item["throughput"] = float(stats[1]["rx_pps"])/1000000
item["loss"] = float(stats[0]["opackets"] - stats[1]["ipackets"])/stats[0]["opackets"]
print("")
print("Throughput: {} Mpps".format(item["throughput"]))
if (stats[0]["opackets"] > 100):
passed = True
else:
passed = False
except STLError as e:
passed = False
print(e)
finally:
c.disconnect()
if passed:
print("\nTest has passed :-)\n")
else:
print("\nTest has failed :-(\n")
def run_mpstat(duration):
global current_test
cmd = 'ssh india mpstat -P ALL {} 1 -o JSON'.format(int(duration))
print("Running mpstat... {}".format(cmd))
output = os.popen(cmd).read()
# print(json.loads(output))
item["mpstat"] = json.loads(output)
# TODO navarrothiago - create a class ethtool.
def run_ethtool_set_rx_queue(number_rx_queue, password):
print("Setting NIC rx queue size...")
ifaces = ["enp3s0f0", "enp3s0f1"]
dicIface = {}
for iface in ifaces:
dicIface[iface] = int(number_rx_queue)
cmd = 'echo {} | ssh india sudo -S ethtool -L {} combined {}'.format(
password, iface, int(number_rx_queue))
print("Running ethtool... {}".format(cmd))
os.popen(cmd)
cmd = 'echo {} | ssh india sudo -S ethtool -l {} '.format(
password, iface)
print("Running ethtool... {}".format(cmd))
print(os.popen(cmd).read())
# TODO navarrothiago - Check if it is worth to store this value
# d[current_test]["rx_queue"] = number_rx_queue
item["ethtool"] = dicIface
print("Setting NIC rx queue size...DONE")
print()
def setup_test_case(name):
global current_test
current_test = name
print("Setup TestCase: {}".format(name))
# TODO navarrothiago - create a file to parse the report.
def get_throughputs(json_output):
dict_output = json.load(json_output)
array = []
for item in dict_output["items"]:
array.append(item["throughput"])
return np.array(array)
# Parse the args.
parser = argparse.ArgumentParser()
parser.add_argument('-s',
'--size',
type=int,
default=64,
help="The packets length in the stream")
parser.add_argument('-m',
'--multiplier',
default='100%',
help="The throughput in mpps on port 0 (e.g. 14mpps, 90%, 1kbps")
parser.add_argument('-d',
'--duration',
type=int,
default=10,
help="The duration of the transmission in second")
parser.add_argument('-f',
'--flows',
default='udp',
help="The flows type (i.e. udp or gtp)")
parser.add_argument("-a",
"--auto",
help="Ignore all arguments and run in mode automatic",
action="store_true")
parser.add_argument('-p',
'--password',
default="",
help="Password of the DUT host")
args = parser.parse_args()
json_output = {
"items": []
}
current_test = ""
flow_list = [1000]
# flow_list = np.geomspace(1, 1000, num=4, dtype=int)
# n_rx_queues = np.arange(1, 13, 1)
n_rx_queues = np.arange(1, 7, 1)
# n_rx_queues = np.geomspace(1, 10, num=1, dtype=int)
timestr = time.strftime("%Y%m%d-%H%M%S")
test_dict = {
"udp": {
"createFlows": create_udp_pkt_flow,
"testCaseName": "DownlinkMaxThoughtput"
},
"gtp": {
"createFlows": create_gtp_pkt_flow,
"testCaseName": "UplinkMaxThoughtput"
}
}
# TODO navarrothiago - the number of flows does not correspond to the what it supposed to be.
test_case_name = test_dict[args.flows]["testCaseName"]
for flow in flow_list:
for rx_size in n_rx_queues:
item = defaultdict(dict)
run_ethtool_set_rx_queue(rx_size, args.password)
for i in {0, 1}:
tx_data_rate=0
if i == 0:
print("Executing with max throughput in order to find the saturation.")
print("The packet loss and CPU load will increase!!")
tx_data_rate=args.multiplier
else:
print("Executing with the target throughput in order to avoiding packet loss")
print("The packet loss and CPU load will be fine now!!")
tx_data_rate=str(item["throughput"]) + "mpps"
test_case = "{}-{}-{}flow-{}rx".format(timestr, test_case_name, flow, rx_size)
# test_case = test_case_name
item["testCase"] = test_case
setup_test_case("{}".format(test_case))
s1 = STLStream(packet=test_dict[args.flows]["createFlows"](args.size, "16.0.0.1",
"16.0.0.254", int(flow), "src"), mode=STLTXCont())
simple_burst([s1], tx_data_rate, args.duration)
if i != 0:
json_output["items"].append(item)
reports_path = "tests/reports"
filename = "{}-{}.json".format(timestr, test_case)
file_to_open = os.path.join(reports_path, filename)
with open(file_to_open, "w") as dump_file:
json.dump(json_output, dump_file, indent=2,
separators=(',', ': '), sort_keys=True)
# Update the last report.
tmp_link = os.path.join(reports_path, test_case_name + ".tmp")
target_link = os.path.join(reports_path, test_case_name + ".json")
os.symlink(filename, tmp_link)
os.rename(tmp_link, target_link)