]>
git.ipfire.org Git - thirdparty/hostap.git/blob - tests/remote/rutils.py
70ad82c877d878c9c905b0a3bde0ce4191ea982d
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.
9 from remotehost
import Host
13 class TestSkip(Exception):
14 def __init__(self
, reason
):
19 # get host based on name
20 def get_host(devices
, dev_name
):
21 dev
= config
.get_device(devices
, dev_name
)
22 host
= Host(host
=dev
['hostname'],
29 # Run setup_hw - hardware specific
30 def setup_hw_host_iface(host
, iface
, setup_params
, force_restart
=False):
32 setup_hw
= setup_params
['setup_hw']
35 if setup_params
['restart_device'] == True:
43 host
.execute([setup_hw
, "-I", iface
, restart
])
47 def setup_hw_host(host
, setup_params
, force_restart
=False):
48 ifaces
= re
.split('; | |, ', host
.ifname
)
50 setup_hw_host_iface(host
, iface
, setup_params
, force_restart
)
52 def setup_hw(hosts
, setup_params
, force_restart
=False):
54 setup_hw_host(host
, setup_params
, force_restart
)
56 # get traces - hw specific
57 def trace_start(hosts
, setup_params
):
59 trace_start_stop(host
, setup_params
, start
=True)
61 def trace_stop(hosts
, setup_params
):
63 trace_start_stop(host
, setup_params
, start
=False)
65 def trace_start_stop(host
, setup_params
, start
):
66 if setup_params
['trace'] == False:
69 start_trace
= setup_params
['trace_start']
70 stop_trace
= setup_params
['trace_stop']
75 trace_dir
= setup_params
['log_dir'] + host
.ifname
+ "/remote_traces"
76 host
.add_log(trace_dir
+ "/*")
77 host
.execute([cmd
, "-I", host
.ifname
, "-D", trace_dir
])
82 def perf_start(hosts
, setup_params
):
84 perf_start_stop(host
, setup_params
, start
=True)
86 def perf_stop(hosts
, setup_params
):
88 perf_start_stop(host
, setup_params
, start
=False)
90 def perf_start_stop(host
, setup_params
, start
):
91 if setup_params
['perf'] == False:
94 perf_start
= setup_params
['perf_start']
95 perf_stop
= setup_params
['perf_stop']
100 perf_dir
= setup_params
['log_dir'] + host
.ifname
+ "/remote_perf"
101 host
.add_log(perf_dir
+ "/*")
102 host
.execute([cmd
, "-I", host
.ifname
, "-D", perf_dir
])
106 # hostapd/wpa_supplicant helpers
107 def run_hostapd(host
, setup_params
):
110 tc_name
= setup_params
['tc_name']
111 log_dir
= setup_params
['log_dir']
112 log_file
= log_dir
+ tc_name
+ "_hostapd_" + host
.name
+ "_" + host
.ifname
+ ".log"
113 host
.execute(["rm", log_file
])
114 log
= " -f " + log_file
119 host
.add_log(log_file
)
120 status
, buf
= host
.execute([setup_params
['hostapd'], "-B", "-ddt", "-g", "udp:" + host
.port
, log
])
122 raise Exception("Could not run hostapd: " + buf
)
124 def run_wpasupplicant(host
, setup_params
):
127 tc_name
= setup_params
['tc_name']
128 log_dir
= setup_params
['log_dir']
129 log_file
= log_dir
+ tc_name
+ "_wpa_supplicant_" + host
.name
+ "_" + host
.ifname
+ ".log"
130 host
.execute(["rm", log_file
])
131 log
= " -f " + log_file
136 host
.add_log(log_file
)
137 status
, buf
= host
.execute([setup_params
['wpa_supplicant'], "-B", "-ddt", "-g", "udp:" + host
.port
, log
])
139 raise Exception("Could not run wpa_supplicant: " + buf
)
141 def kill_wpasupplicant(host
, setup_params
):
142 host
.execute(['killall', setup_params
['wpa_supplicant']])
144 def kill_hostapd(host
, setup_params
):
145 host
.execute(['killall', setup_params
['hostapd']])
147 def get_ap_params(channel
="1", bw
="HT20", country
="US", security
="open", ht_capab
=None, vht_capab
=None):
148 ssid
= "test_" + channel
+ "_" + security
+ "_" + bw
151 params
= hostapd
.b_only_params(channel
, ssid
, country
)
153 params
= hostapd
.g_only_params(channel
, ssid
, country
)
154 elif bw
== "g_only_wmm":
155 params
= hostapd
.g_only_params(channel
, ssid
, country
)
156 params
['wmm_enabled'] = "1"
158 params
= hostapd
.a_only_params(channel
, ssid
, country
)
159 elif bw
== "a_only_wmm":
160 params
= hostapd
.a_only_params(channel
, ssid
, country
)
161 params
['wmm_enabled'] = "1"
163 params
= hostapd
.ht20_params(channel
, ssid
, country
)
166 params
['ht_capab'] = params
['ht_capab'] + ht_capab
168 params
['ht_capab'] = ht_capab
170 params
= hostapd
.ht40_plus_params(channel
, ssid
, country
)
172 params
['ht_capab'] = params
['ht_capab'] + ht_capab
174 params
= hostapd
.ht40_minus_params(channel
, ssid
, country
)
176 params
['ht_capab'] = params
['ht_capab'] + ht_capab
178 params
= hostapd
.ht40_plus_params(channel
, ssid
, country
)
180 params
['ht_capab'] = params
['ht_capab'] + ht_capab
183 params
['vht_capab'] = params
['vht_capab'] + vht_capab
185 params
['vht_capab'] = vht_capab
186 params
['ieee80211ac'] = "1"
187 params
['vht_oper_chwidth'] = "1"
188 params
['vht_oper_centr_freq_seg0_idx'] = str(int(channel
) + 6)
192 # now setup security params
193 if security
== "tkip":
194 sec_params
= hostapd
.wpa_params(passphrase
="testtest")
195 elif security
== "ccmp":
196 sec_params
= hostapd
.wpa2_params(passphrase
="testtest")
197 elif security
== "mixed":
198 sec_params
= hostapd
.wpa_mixed_params(passphrase
="testtest")
199 elif security
== "wep":
200 sec_params
= {"wep_key0" : "123456789a",
201 "wep_default_key" : "0",
203 elif security
== "wep_shared":
204 sec_params
= {"wep_key0" : "123456789a",
205 "wep_default_key" : "0",
210 params
.update(sec_params
)
215 def get_ipv4(client
, ifname
=None):
217 ifname
= client
.ifname
218 status
, buf
= client
.execute(["ifconfig", ifname
])
219 lines
= buf
.splitlines()
222 res
= line
.find("inet addr:")
228 addr
= words
[1].split(":")
233 def get_ipv6(client
, ifname
=None):
236 ifname
= client
.ifname
237 status
, buf
= client
.execute(["ifconfig", ifname
])
238 lines
= buf
.splitlines()
241 res
= line
.find("Scope:Link")
247 if words
[0] == "inet6" and words
[1] == "addr:":
249 addr
= addr_mask
.split("/")
254 def get_ip(client
, addr_type
="ipv6", iface
=None):
255 if addr_type
== "ipv6":
256 return get_ipv6(client
, iface
)
257 elif addr_type
== "ipv4":
258 return get_ipv4(client
, iface
)
260 return "unknown addr_type: " + addr_type
262 def get_ipv4_addr(setup_params
, number
):
264 ipv4_base
= setup_params
['ipv4_test_net']
266 ipv4_base
= "172.16.12.0"
268 parts
= ipv4_base
.split('.')
269 ipv4
= parts
[0] + "." + parts
[1] + "." + parts
[2] + "." + str(number
)
273 def get_mac_addr(host
, iface
=None):
276 status
, buf
= host
.execute(["ifconfig", iface
])
278 raise Exception("ifconfig " + iface
)
286 raise Exception("Could not find HWaddr")
288 # connectivity/ping helpers
289 def get_ping_packet_loss(ping_res
):
291 lines
= ping_res
.splitlines()
293 if line
.find("packet loss") != -1:
300 sections
= loss_line
.split(",")
302 for section
in sections
:
303 if section
.find("packet loss") != -1:
304 words
= section
.split()
309 def ac_to_ping_ac(qos
):
322 def ping_run(host
, ip
, result
, ifname
=None, addr_type
="ipv4", deadline
="5", qos
=None):
325 if addr_type
== "ipv6":
330 ping
= ping
+ ["-w", deadline
, "-I", ifname
]
332 ping
= ping
+ ["-Q", ac_to_ping_ac(qos
)]
335 flush_arp_cache(host
)
337 thread
= host
.execute_run(ping
, result
)
340 def ping_wait(host
, thread
, timeout
=None):
341 host
.wait_execute_complete(thread
, timeout
)
343 raise Exception("ping thread still alive")
345 def flush_arp_cache(host
):
346 host
.execute(["ip", "-s", "-s", "neigh", "flush", "all"])
348 def check_connectivity(a
, b
, addr_type
="ipv4", deadline
="5", qos
=None):
349 addr_a
= get_ip(a
, addr_type
)
350 addr_b
= get_ip(b
, addr_type
)
352 if addr_type
== "ipv4":
357 ping_a_b
= ping
+ ["-w", deadline
, "-I", a
.ifname
]
358 ping_b_a
= ping
+ ["-w", deadline
, "-I", b
.ifname
]
360 ping_a_b
= ping_a_b
+ ["-Q", ac_to_ping_ac(qos
)]
361 ping_b_a
= ping_b_a
+ ["-Q", ac_to_ping_ac(qos
)]
362 ping_a_b
= ping_a_b
+ [addr_b
]
363 ping_b_a
= ping_b_a
+ [addr_a
]
369 status
, buf
= a
.execute(ping_a_b
)
370 if status
== 2 and ping
== "ping6":
371 # tentative possible for a while, try again
373 status
, buf
= a
.execute(ping_a_b
)
375 raise Exception("ping " + a
.name
+ "/" + a
.ifname
+ " >> " + b
.name
+ "/" + b
.ifname
)
377 a_b
= get_ping_packet_loss(buf
)
383 status
, buf
= b
.execute(ping_b_a
)
385 raise Exception("ping " + b
.name
+ "/" + b
.ifname
+ " >> " + a
.name
+ "/" + a
.ifname
)
387 b_a
= get_ping_packet_loss(buf
)
389 if int(a_b
[:-1]) > 40:
390 raise Exception("Too high packet lost: " + a_b
)
392 if int(b_a
[:-1]) > 40:
393 raise Exception("Too high packet lost: " + b_a
)
399 def get_iperf_speed(iperf_res
, pattern
="Mbits/sec"):
400 lines
= iperf_res
.splitlines()
406 # first find last SUM line
408 res
= line
.find("[SUM]")
412 # next check SUM status
414 words
= sum_line
.split()
416 res
= word
.find(pattern
)
418 return words
[count
- 1] + " " + pattern
421 # no SUM - one thread - find last line
423 res
= line
.find(pattern
)
428 return "0 " + pattern
431 words
= last_line
.split()
433 res
= word
.find(pattern
)
435 return words
[count
- 1] + " " + pattern
438 return "0 " + pattern
440 def ac_to_iperf_ac(qos
):
453 def iperf_run(server
, client
, server_ip
, client_res
, server_res
,
454 l4
="udp", bw
="30M", test_time
="30", parallel
="5",
455 qos
="be", param
=" -i 5 ", ifname
=None, l3
="ipv4",
456 port
="5001", iperf
="iperf"):
458 ifname
= client
.ifname
461 iperf_server
= [iperf
]
462 elif iperf
== "iperf3":
463 iperf_server
= [iperf
, "-1"]
466 iperf_client
= [iperf
, "-c", server_ip
, "-p", port
]
467 iperf_server
= iperf_server
+ ["-p", port
]
469 iperf_client
= [iperf
, "-V", "-c", server_ip
+ "%" + ifname
, "-p", port
]
470 iperf_server
= iperf_server
+ ["-V", "-p", port
]
474 iperf_server
= iperf_server
+ ["-s", "-f", "m", param
]
475 iperf_client
= iperf_client
+ ["-f", "m", "-t", test_time
]
478 iperf_client
= iperf_client
+ ["-P", parallel
]
481 if iperf
!= "iperf3":
482 iperf_server
= iperf_server
+ ["-u"]
483 iperf_client
= iperf_client
+ ["-u", "-b", bw
]
486 iperf_client
= iperf_client
+ ["-Q", ac_to_iperf_ac(qos
)]
488 flush_arp_cache(server
)
489 flush_arp_cache(client
)
491 server_thread
= server
.execute_run(iperf_server
, server_res
)
493 client_thread
= client
.execute_run(iperf_client
, client_res
)
495 return server_thread
, client_thread
497 def iperf_wait(server
, client
, server_thread
, client_thread
, timeout
=None, iperf
="iperf"):
498 client
.wait_execute_complete(client_thread
, timeout
)
499 if client_thread
.isAlive():
500 raise Exception("iperf client thread still alive")
502 server
.wait_execute_complete(server_thread
, 5)
503 if server_thread
.isAlive():
504 server
.execute(["killall", "-s", "INT", iperf
])
507 server
.wait_execute_complete(server_thread
, 5)
508 if server_thread
.isAlive():
509 raise Exception("iperf server thread still alive")
513 def run_tp_test(server
, client
, l3
="ipv4", iperf
="iperf", l4
="tcp", test_time
="10", parallel
="5",
514 qos
="be", bw
="30M", ifname
=None, port
="5001"):
518 server_ip
= get_ip(server
, l3
)
520 server_thread
, client_thread
= iperf_run(server
, client
, server_ip
, client_res
, server_res
,
521 l3
=l3
, iperf
=iperf
, l4
=l4
, test_time
=test_time
,
522 parallel
=parallel
, qos
=qos
, bw
=bw
, ifname
=ifname
,
524 iperf_wait(server
, client
, server_thread
, client_thread
, iperf
=iperf
, timeout
=int(test_time
) + 10)
526 if client_res
[0] != 0:
527 raise Exception(iperf
+ " client: " + client_res
[1])
528 if server_res
[0] != 0:
529 raise Exception(iperf
+ " server: " + server_res
[1])
530 if client_res
[1] is None:
531 raise Exception(iperf
+ " client result issue")
532 if server_res
[1] is None:
533 raise Exception(iperf
+ " server result issue")
536 result
= server_res
[1]
537 if iperf
== "iperf3":
538 result
= client_res
[1]
540 speed
= get_iperf_speed(result
)
543 def get_iperf_bw(bw
, parallel
, spacial_streams
=2):
546 elif bw
== "g_only" or bw
== "g_only_wmm" or bw
== "a_only" or bw
== "a_only_wmm":
549 max_tp
= 72 * spacial_streams
550 elif bw
== "HT40+" or bw
== "HT40-":
551 max_tp
= 150 * spacial_streams
553 max_tp
= 433 * spacial_streams
557 max_tp
= 1.2 * max_tp
559 return str(int(max_tp
/int(parallel
))) + "M"