2 # Copyright (c) 2016, Tieto Corporation
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
8 from remotehost import Host
12 class TestSkip(Exception):
13 def __init__(self, reason):
18 # get host based on name
19 def get_host(devices, dev_name):
20 dev = config.get_device(devices, dev_name)
21 host = Host(host = dev['hostname'],
22 ifname = dev['ifname'],
28 # run setup_hw - hw specyfic
29 def setup_hw(hosts, setup_params):
31 setup_hw_host(host, setup_params)
33 def setup_hw_host(host, setup_params):
35 setup_hw = setup_params['setup_hw']
38 if setup_params['restart_device'] == True:
42 host.execute([setup_hw, "-I", host.ifname, restart])
46 # get traces - hw specific
47 def trace_start(hosts, setup_params):
49 trace_start_stop(host, setup_params, start=True)
51 def trace_stop(hosts, setup_params):
53 trace_start_stop(host, setup_params, start=False)
55 def trace_start_stop(host, setup_params, start):
56 if setup_params['trace'] == False:
59 start_trace = setup_params['trace_start']
60 stop_trace = setup_params['trace_stop']
65 trace_dir = setup_params['log_dir'] + host.ifname + "/remote_traces"
66 host.add_log(trace_dir + "/*")
67 host.execute([cmd, "-I", host.ifname, "-D", trace_dir])
72 def perf_start(hosts, setup_params):
74 perf_start_stop(host, setup_params, start=True)
76 def perf_stop(hosts, setup_params):
78 perf_start_stop(host, setup_params, start=False)
80 def perf_start_stop(host, setup_params, start):
81 if setup_params['perf'] == False:
84 perf_start = setup_params['perf_start']
85 perf_stop = setup_params['perf_stop']
90 perf_dir = setup_params['log_dir'] + host.ifname + "/remote_perf"
91 host.add_log(perf_dir + "/*")
92 host.execute([cmd, "-I", host.ifname, "-D", perf_dir])
96 # hostapd/wpa_supplicant helpers
97 def run_hostapd(host, setup_params):
100 tc_name = setup_params['tc_name']
101 log_dir = setup_params['log_dir']
102 log_file = log_dir + tc_name + "_hostapd_" + host.name + "_" + host.ifname + ".log"
103 host.execute(["rm", log_file])
104 log = " -f " + log_file
109 host.add_log(log_file)
110 status, buf = host.execute([setup_params['hostapd'], "-B", "-ddt", "-g", "udp:" + host.port, log])
112 raise Exception("Could not run hostapd: " + buf)
114 def run_wpasupplicant(host, setup_params):
117 tc_name = setup_params['tc_name']
118 log_dir = setup_params['log_dir']
119 log_file = log_dir + tc_name + "_wpa_supplicant_" + host.name + "_" + host.ifname + ".log"
120 host.execute(["rm", log_file])
121 log = " -f " + log_file
126 host.add_log(log_file)
127 status, buf = host.execute([setup_params['wpa_supplicant'], "-B", "-ddt", "-g", "udp:" + host.port, log])
129 raise Exception("Could not run wpa_supplicant: " + buf)
131 def get_ap_params(channel="1", bw="HT20", country="US", security="open", ht_capab=None, vht_capab=None):
132 ssid = "test_" + channel + "_" + security + "_" + bw
135 params = hostapd.b_only_params(channel, ssid, country)
137 params = hostapd.g_only_params(channel, ssid, country)
138 elif bw == "g_only_wmm":
139 params = hostapd.g_only_params(channel, ssid, country)
140 params['wmm_enabled'] = "1"
142 params = hostapd.a_only_params(channel, ssid, country)
143 elif bw == "a_only_wmm":
144 params = hostapd.a_only_params(channel, ssid, country)
145 params['wmm_enabled'] = "1"
147 params = hostapd.ht20_params(channel, ssid, country)
150 params['ht_capab'] = params['ht_capab'] + ht_capab
152 params['ht_capab'] = ht_capab
154 params = hostapd.ht40_plus_params(channel, ssid, country)
156 params['ht_capab'] = params['ht_capab'] + ht_capab
158 params = hostapd.ht40_minus_params(channel, ssid, country)
160 params['ht_capab'] = params['ht_capab'] + ht_capab
162 params = hostapd.ht40_plus_params(channel, ssid, country)
164 params['ht_capab'] = params['ht_capab'] + ht_capab
167 params['vht_capab'] = params['vht_capab'] + vht_capab
169 params['vht_capab'] = vht_capab
170 params['ieee80211ac'] = "1"
171 params['vht_oper_chwidth'] = "1"
172 params['vht_oper_centr_freq_seg0_idx'] = str(int(channel) + 6)
176 # now setup security params
177 if security == "tkip":
178 sec_params = hostapd.wpa_params(passphrase="testtest")
179 elif security == "ccmp":
180 sec_params = hostapd.wpa2_params(passphrase="testtest")
181 elif security == "mixed":
182 sec_params = hostapd.wpa_mixed_params(passphrase="testtest")
183 elif security == "wep":
184 sec_params = { "wep_key0" : "123456789a",
185 "wep_default_key" : "0",
187 elif security == "wep_shared":
188 sec_params = { "wep_key0" : "123456789a",
189 "wep_default_key" : "0",
194 params.update(sec_params)
199 def get_ipv4(client, ifname=None):
201 ifname = client.ifname
202 status, buf = client.execute(["ifconfig", ifname])
203 lines = buf.splitlines()
206 res = line.find("inet addr:")
212 addr = words[1].split(":")
217 def get_ipv6(client, ifname=None):
220 ifname = client.ifname
221 status, buf = client.execute(["ifconfig", ifname])
222 lines = buf.splitlines()
225 res = line.find("Scope:Link")
231 if words[0] == "inet6" and words[1] == "addr:":
233 addr = addr_mask.split("/")
238 def get_ip(client, addr_type="ipv6", iface=None):
239 if addr_type == "ipv6":
240 return get_ipv6(client, iface)
241 elif addr_type == "ipv4":
242 return get_ipv4(client, iface)
244 return "unknown addr_type: " + addr_type
246 def get_ipv4_addr(setup_params, number):
248 ipv4_base = setup_params['ipv4_test_net']
250 ipv4_base = "172.16.12.0"
252 parts = ipv4_base.split('.')
253 ipv4 = parts[0] + "." + parts[1] + "." + parts[2] + "." + str(number)
257 def get_mac_addr(host, iface=None):
260 status, buf = host.execute(["ifconfig", iface])
262 raise Exception("ifconfig " + iface)
270 raise Exception("Could not find HWaddr")
272 # connectivity/ping helpers
273 def get_ping_packet_loss(ping_res):
275 lines = ping_res.splitlines()
277 if line.find("packet loss") != -1:
284 sections = loss_line.split(",")
286 for section in sections:
287 if section.find("packet loss") != -1:
288 words = section.split()
293 def ac_to_ping_ac(qos):
306 def ping_run(host, ip, result, ifname=None, addr_type="ipv4", deadline="5", qos=None):
309 if addr_type == "ipv6":
314 ping = ping + ["-w", deadline, "-I", ifname]
316 ping = ping + ["-Q", ac_to_ping_ac(qos)]
319 flush_arp_cache(host)
321 thread = host.execute_run(ping, result)
324 def ping_wait(host, thread, timeout=None):
325 host.wait_execute_complete(thread, timeout)
327 raise Exception("ping thread still alive")
329 def flush_arp_cache(host):
330 host.execute(["ip", "-s", "-s", "neigh", "flush", "all"])
332 def check_connectivity(a, b, addr_type = "ipv4", deadline="5", qos=None):
333 addr_a = get_ip(a, addr_type)
334 addr_b = get_ip(b, addr_type)
336 if addr_type == "ipv4":
341 ping_a_b = ping + ["-w", deadline, "-I", a.ifname]
342 ping_b_a = ping + ["-w", deadline, "-I", b.ifname]
344 ping_a_b = ping_a_b + ["-Q", ac_to_ping_ac(qos)]
345 ping_b_a = ping_b_a + ["-Q", ac_to_ping_ac(qos)]
346 ping_a_b = ping_a_b + [addr_b]
347 ping_b_a = ping_b_a + [addr_a]
353 status, buf = a.execute(ping_a_b)
354 if status == 2 and ping == "ping6":
355 # tentative possible for a while, try again
357 status, buf = a.execute(ping_a_b)
359 raise Exception("ping " + a.name + "/" + a.ifname + " >> " + b.name + "/" + b.ifname)
361 a_b = get_ping_packet_loss(buf)
367 status, buf = b.execute(ping_b_a)
369 raise Exception("ping " + b.name + "/" + b.ifname + " >> " + a.name + "/" + a.ifname)
371 b_a = get_ping_packet_loss(buf)
373 if int(a_b[:-1]) > 40:
374 raise Exception("Too high packet lost: " + a_b)
376 if int(b_a[:-1]) > 40:
377 raise Exception("Too high packet lost: " + b_a)
383 def get_iperf_speed(iperf_res, pattern="Mbits/sec"):
384 lines = iperf_res.splitlines()
390 # first find last SUM line
392 res = line.find("[SUM]")
396 # next check SUM status
398 words = sum_line.split()
400 res = word.find(pattern)
402 return words[count - 1] + " " + pattern
405 # no SUM - one thread - find last line
407 res = line.find(pattern)
412 return "0 " + pattern
415 words = last_line.split()
417 res = word.find(pattern)
419 return words[count - 1] + " " + pattern
422 return "0 " + pattern
424 def ac_to_iperf_ac(qos):
437 def iperf_run(server, client, server_ip, client_res, server_res,
438 l4="udp", bw="30M", test_time="30", parallel="5",
439 qos="be", param=" -i 5 ", ifname=None, l3="ipv4",
440 port="5001", iperf="iperf"):
442 ifname = client.ifname
445 iperf_server = [iperf]
446 elif iperf == "iperf3":
447 iperf_server = [iperf, "-1"]
450 iperf_client = [iperf, "-c", server_ip, "-p", port]
451 iperf_server = iperf_server + ["-p", port]
453 iperf_client = [iperf, "-V", "-c", server_ip + "%" + ifname, "-p", port]
454 iperf_server = iperf_server + ["-V", "-p", port]
458 iperf_server = iperf_server + ["-s", "-f", "m", param]
459 iperf_client = iperf_client + ["-f", "m", "-t", test_time]
462 iperf_client = iperf_client + ["-P", parallel]
465 if iperf != "iperf3":
466 iperf_server = iperf_server + ["-u"]
467 iperf_client = iperf_client + ["-u", "-b", bw]
470 iperf_client = iperf_client + ["-Q", ac_to_iperf_ac(qos)]
472 flush_arp_cache(server)
473 flush_arp_cache(client)
475 server_thread = server.execute_run(iperf_server, server_res)
477 client_thread = client.execute_run(iperf_client, client_res)
479 return server_thread, client_thread
481 def iperf_wait(server, client, server_thread, client_thread, timeout=None, iperf="iperf"):
482 client.wait_execute_complete(client_thread, timeout)
483 if client_thread.isAlive():
484 raise Exception("iperf client thread still alive")
486 server.wait_execute_complete(server_thread, 5)
487 if server_thread.isAlive():
488 server.execute(["killall", "-s", "INT", iperf])
491 server.wait_execute_complete(server_thread, 5)
492 if server_thread.isAlive():
493 raise Execption("iperf server thread still alive")
497 def run_tp_test(server, client, l3="ipv4", iperf="iperf", l4="tcp", test_time="10", parallel="5",
498 qos="be", bw="30M", ifname=None, port="5001"):
502 server_ip = get_ip(server, l3)
504 server_thread, client_thread = iperf_run(server, client, server_ip, client_res, server_res,
505 l3=l3, iperf=iperf, l4=l4, test_time=test_time,
506 parallel=parallel, qos=qos, bw=bw, ifname=ifname,
508 iperf_wait(server, client, server_thread, client_thread, iperf=iperf, timeout=int(test_time) + 10)
510 if client_res[0] != 0:
511 raise Exception(iperf + " client: " + client_res[1])
512 if server_res[0] != 0:
513 raise Exception(iperf + " server: " + server_res[1])
514 if client_res[1] is None:
515 raise Exception(iperf + " client result issue")
516 if server_res[1] is None:
517 raise Exception(iperf + " server result issue")
520 result = server_res[1]
521 if iperf == "iperf3":
522 result = client_res[1]
524 speed = get_iperf_speed(result)
527 def get_iperf_bw(bw, parallel, spacial_streams=2):
530 elif bw == "g_only" or bw == "g_only_wmm" or bw == "a_only" or bw == "a_only_wmm":
533 max_tp = 72 * spacial_streams
534 elif bw == "HT40+" or bw == "HT40-":
535 max_tp = 150 * spacial_streams
537 max_tp = 433 * spacial_streams
541 max_tp = 1.2 * max_tp
543 return str(int(max_tp/int(parallel))) + "M"