2013-03-02 11:22:28 +01:00
|
|
|
# hwsim testing utilities
|
2014-03-15 09:42:46 +01:00
|
|
|
# Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
|
2013-03-02 11:22:28 +01:00
|
|
|
#
|
|
|
|
# This software may be distributed under the terms of the BSD license.
|
|
|
|
# See README for more details.
|
|
|
|
|
2013-05-11 10:34:49 +02:00
|
|
|
import os
|
2014-03-15 09:42:46 +01:00
|
|
|
import time
|
2013-03-02 11:22:28 +01:00
|
|
|
import logging
|
2013-10-31 11:46:42 +01:00
|
|
|
logger = logging.getLogger()
|
2013-03-02 11:22:28 +01:00
|
|
|
|
2014-10-19 19:57:08 +02:00
|
|
|
from wpasupplicant import WpaSupplicant
|
|
|
|
|
2023-12-12 09:07:02 +01:00
|
|
|
def _sync_carrier(dev, ifname):
|
|
|
|
ifname = ifname or dev.ifname
|
|
|
|
carrier_p2p = None
|
|
|
|
try:
|
|
|
|
with open(f'/sys/class/net/{ifname}/carrier', 'r') as f:
|
|
|
|
carrier_main = f.read().strip() == '1'
|
|
|
|
except FileNotFoundError:
|
|
|
|
return
|
|
|
|
if (isinstance(dev, WpaSupplicant) and dev.group_ifname and
|
|
|
|
dev.group_ifname != ifname):
|
|
|
|
ifname = dev.group_ifname
|
|
|
|
try:
|
|
|
|
with open(f'/sys/class/net/{ifname}/carrier', 'r') as f:
|
|
|
|
carrier_p2p = f.read().strip() == '1'
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
assert carrier_main or carrier_p2p, "sending data without carrier won't work"
|
|
|
|
|
2020-02-05 08:13:48 +01:00
|
|
|
def config_data_test(dev1, dev2, dev1group, dev2group, ifname1, ifname2):
|
|
|
|
cmd = "DATA_TEST_CONFIG 1"
|
|
|
|
if ifname1:
|
|
|
|
cmd = cmd + " ifname=" + ifname1
|
|
|
|
if dev1group:
|
|
|
|
res = dev1.group_request(cmd)
|
|
|
|
else:
|
|
|
|
res = dev1.request(cmd)
|
|
|
|
if "OK" not in res:
|
|
|
|
raise Exception("Failed to enable data test functionality")
|
|
|
|
|
2023-12-12 09:07:02 +01:00
|
|
|
_sync_carrier(dev1, ifname1)
|
|
|
|
|
2020-02-05 08:13:48 +01:00
|
|
|
cmd = "DATA_TEST_CONFIG 1"
|
|
|
|
if ifname2:
|
|
|
|
cmd = cmd + " ifname=" + ifname2
|
|
|
|
if dev2group:
|
|
|
|
res = dev2.group_request(cmd)
|
|
|
|
else:
|
|
|
|
res = dev2.request(cmd)
|
|
|
|
if "OK" not in res:
|
|
|
|
raise Exception("Failed to enable data test functionality")
|
|
|
|
|
2023-12-12 09:07:02 +01:00
|
|
|
_sync_carrier(dev2, ifname2)
|
|
|
|
|
2020-02-05 08:13:48 +01:00
|
|
|
def run_multicast_connectivity_test(dev1, dev2, tos=None,
|
|
|
|
dev1group=False, dev2group=False,
|
|
|
|
ifname1=None, ifname2=None,
|
|
|
|
config=True, timeout=5,
|
|
|
|
send_len=None, multicast_to_unicast=False,
|
|
|
|
broadcast_retry_c=1):
|
|
|
|
addr1 = dev1.get_addr(dev1group)
|
|
|
|
addr2 = dev2.get_addr(dev2group)
|
|
|
|
|
|
|
|
if config:
|
|
|
|
config_data_test(dev1, dev2, dev1group, dev2group, ifname1, ifname2)
|
|
|
|
|
|
|
|
cmd = "DATA_TEST_TX ff:ff:ff:ff:ff:ff {} {}".format(addr1, tos)
|
|
|
|
if send_len is not None:
|
|
|
|
cmd += " len=" + str(send_len)
|
|
|
|
for i in range(broadcast_retry_c):
|
|
|
|
try:
|
|
|
|
if dev1group:
|
|
|
|
dev1.group_request(cmd)
|
|
|
|
else:
|
|
|
|
dev1.request(cmd)
|
|
|
|
if dev2group:
|
|
|
|
ev = dev2.wait_group_event(["DATA-TEST-RX"],
|
|
|
|
timeout=timeout)
|
|
|
|
else:
|
|
|
|
ev = dev2.wait_event(["DATA-TEST-RX"], timeout=timeout)
|
|
|
|
if ev is None:
|
|
|
|
raise Exception("dev1->dev2 broadcast data delivery failed")
|
|
|
|
if multicast_to_unicast:
|
|
|
|
if "DATA-TEST-RX ff:ff:ff:ff:ff:ff {}".format(addr1) in ev:
|
|
|
|
raise Exception("Unexpected dev1->dev2 broadcast data result: multicast to unicast conversion missing")
|
|
|
|
if "DATA-TEST-RX {} {}".format(addr2, addr1) not in ev:
|
|
|
|
raise Exception("Unexpected dev1->dev2 broadcast data result (multicast to unicast enabled)")
|
|
|
|
else:
|
|
|
|
if "DATA-TEST-RX ff:ff:ff:ff:ff:ff {}".format(addr1) not in ev:
|
|
|
|
raise Exception("Unexpected dev1->dev2 broadcast data result")
|
|
|
|
if send_len is not None:
|
|
|
|
if " len=" + str(send_len) not in ev:
|
|
|
|
raise Exception("Unexpected dev1->dev2 broadcast data length")
|
|
|
|
else:
|
|
|
|
if " len=" in ev:
|
|
|
|
raise Exception("Unexpected dev1->dev2 broadcast data length")
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
if i == broadcast_retry_c - 1:
|
|
|
|
raise
|
|
|
|
|
2014-11-27 13:04:51 +01:00
|
|
|
def run_connectivity_test(dev1, dev2, tos, dev1group=False, dev2group=False,
|
2016-10-31 14:58:09 +01:00
|
|
|
ifname1=None, ifname2=None, config=True, timeout=5,
|
2018-12-24 22:56:25 +01:00
|
|
|
multicast_to_unicast=False, broadcast=True,
|
|
|
|
send_len=None):
|
2020-02-05 08:13:48 +01:00
|
|
|
addr1 = dev1.get_addr(dev1group)
|
|
|
|
addr2 = dev2.get_addr(dev2group)
|
2014-10-19 19:57:08 +02:00
|
|
|
|
2014-12-16 22:42:38 +01:00
|
|
|
dev1.dump_monitor()
|
|
|
|
dev2.dump_monitor()
|
|
|
|
|
2016-05-24 13:02:33 +02:00
|
|
|
if dev1.hostname is None and dev2.hostname is None:
|
|
|
|
broadcast_retry_c = 1
|
|
|
|
else:
|
|
|
|
broadcast_retry_c = 10
|
|
|
|
|
2014-10-19 19:57:08 +02:00
|
|
|
try:
|
2014-12-23 16:46:34 +01:00
|
|
|
if config:
|
2020-02-05 08:13:48 +01:00
|
|
|
config_data_test(dev1, dev2, dev1group, dev2group, ifname1, ifname2)
|
2014-10-19 19:57:08 +02:00
|
|
|
|
|
|
|
cmd = "DATA_TEST_TX {} {} {}".format(addr2, addr1, tos)
|
2018-12-24 22:56:25 +01:00
|
|
|
if send_len is not None:
|
|
|
|
cmd += " len=" + str(send_len)
|
2014-10-19 19:57:08 +02:00
|
|
|
if dev1group:
|
|
|
|
dev1.group_request(cmd)
|
|
|
|
else:
|
|
|
|
dev1.request(cmd)
|
|
|
|
if dev2group:
|
2015-02-08 12:08:32 +01:00
|
|
|
ev = dev2.wait_group_event(["DATA-TEST-RX"], timeout=timeout)
|
2014-10-19 19:57:08 +02:00
|
|
|
else:
|
2015-02-08 12:08:32 +01:00
|
|
|
ev = dev2.wait_event(["DATA-TEST-RX"], timeout=timeout)
|
2014-10-19 19:57:08 +02:00
|
|
|
if ev is None:
|
|
|
|
raise Exception("dev1->dev2 unicast data delivery failed")
|
|
|
|
if "DATA-TEST-RX {} {}".format(addr2, addr1) not in ev:
|
|
|
|
raise Exception("Unexpected dev1->dev2 unicast data result")
|
2018-12-24 22:56:25 +01:00
|
|
|
if send_len is not None:
|
|
|
|
if " len=" + str(send_len) not in ev:
|
|
|
|
raise Exception("Unexpected dev1->dev2 unicast data length")
|
|
|
|
else:
|
|
|
|
if " len=" in ev:
|
|
|
|
raise Exception("Unexpected dev1->dev2 unicast data length")
|
2014-10-19 19:57:08 +02:00
|
|
|
|
2017-10-14 16:50:25 +02:00
|
|
|
if broadcast:
|
2020-02-05 08:13:48 +01:00
|
|
|
run_multicast_connectivity_test(dev1, dev2, tos,
|
|
|
|
dev1group, dev2group,
|
|
|
|
ifname1, ifname2, False, timeout,
|
|
|
|
send_len, False, broadcast_retry_c)
|
2014-10-19 19:57:08 +02:00
|
|
|
|
|
|
|
cmd = "DATA_TEST_TX {} {} {}".format(addr1, addr2, tos)
|
2018-12-24 22:56:25 +01:00
|
|
|
if send_len is not None:
|
|
|
|
cmd += " len=" + str(send_len)
|
2014-10-19 19:57:08 +02:00
|
|
|
if dev2group:
|
|
|
|
dev2.group_request(cmd)
|
|
|
|
else:
|
|
|
|
dev2.request(cmd)
|
|
|
|
if dev1group:
|
2015-02-08 12:08:32 +01:00
|
|
|
ev = dev1.wait_group_event(["DATA-TEST-RX"], timeout=timeout)
|
2014-10-19 19:57:08 +02:00
|
|
|
else:
|
2015-02-08 12:08:32 +01:00
|
|
|
ev = dev1.wait_event(["DATA-TEST-RX"], timeout=timeout)
|
2014-10-19 19:57:08 +02:00
|
|
|
if ev is None:
|
|
|
|
raise Exception("dev2->dev1 unicast data delivery failed")
|
|
|
|
if "DATA-TEST-RX {} {}".format(addr1, addr2) not in ev:
|
|
|
|
raise Exception("Unexpected dev2->dev1 unicast data result")
|
2018-12-24 22:56:25 +01:00
|
|
|
if send_len is not None:
|
|
|
|
if " len=" + str(send_len) not in ev:
|
|
|
|
raise Exception("Unexpected dev2->dev1 unicast data length")
|
|
|
|
else:
|
|
|
|
if " len=" in ev:
|
|
|
|
raise Exception("Unexpected dev2->dev1 unicast data length")
|
2014-10-19 19:57:08 +02:00
|
|
|
|
2017-10-14 16:50:25 +02:00
|
|
|
if broadcast:
|
2020-02-05 08:13:48 +01:00
|
|
|
run_multicast_connectivity_test(dev2, dev1, tos,
|
|
|
|
dev2group, dev1group,
|
|
|
|
ifname2, ifname1, False, timeout,
|
|
|
|
send_len, multicast_to_unicast,
|
|
|
|
broadcast_retry_c)
|
|
|
|
|
2014-10-19 19:57:08 +02:00
|
|
|
finally:
|
2014-12-23 16:46:34 +01:00
|
|
|
if config:
|
|
|
|
if dev1group:
|
|
|
|
dev1.group_request("DATA_TEST_CONFIG 0")
|
|
|
|
else:
|
|
|
|
dev1.request("DATA_TEST_CONFIG 0")
|
|
|
|
if dev2group:
|
|
|
|
dev2.group_request("DATA_TEST_CONFIG 0")
|
|
|
|
else:
|
|
|
|
dev2.request("DATA_TEST_CONFIG 0")
|
2014-10-19 19:57:08 +02:00
|
|
|
|
2014-11-27 13:04:51 +01:00
|
|
|
def test_connectivity(dev1, dev2, dscp=None, tos=None, max_tries=1,
|
|
|
|
dev1group=False, dev2group=False,
|
2016-10-31 14:58:09 +01:00
|
|
|
ifname1=None, ifname2=None, config=True, timeout=5,
|
2017-10-14 16:50:25 +02:00
|
|
|
multicast_to_unicast=False, success_expected=True,
|
2018-12-24 22:56:25 +01:00
|
|
|
broadcast=True, send_len=None):
|
2014-10-19 19:57:08 +02:00
|
|
|
if dscp:
|
|
|
|
tos = dscp << 2
|
|
|
|
if not tos:
|
|
|
|
tos = 0
|
|
|
|
|
|
|
|
success = False
|
|
|
|
last_err = None
|
|
|
|
for i in range(0, max_tries):
|
|
|
|
try:
|
2014-11-27 13:04:51 +01:00
|
|
|
run_connectivity_test(dev1, dev2, tos, dev1group, dev2group,
|
2015-02-08 12:08:32 +01:00
|
|
|
ifname1, ifname2, config=config,
|
2016-10-31 14:58:09 +01:00
|
|
|
timeout=timeout,
|
2017-10-14 16:50:25 +02:00
|
|
|
multicast_to_unicast=multicast_to_unicast,
|
2018-12-24 22:56:25 +01:00
|
|
|
broadcast=broadcast, send_len=send_len)
|
2014-10-19 19:57:08 +02:00
|
|
|
success = True
|
|
|
|
break
|
2019-01-24 08:45:41 +01:00
|
|
|
except Exception as e:
|
2014-10-19 19:57:08 +02:00
|
|
|
last_err = e
|
|
|
|
if i + 1 < max_tries:
|
|
|
|
time.sleep(1)
|
2017-03-21 02:50:30 +01:00
|
|
|
if success_expected and not success:
|
2014-10-19 19:57:08 +02:00
|
|
|
raise Exception(last_err)
|
2017-03-21 02:50:30 +01:00
|
|
|
if not success_expected and success:
|
|
|
|
raise Exception("Unexpected connectivity detected")
|
2014-10-19 16:54:49 +02:00
|
|
|
|
2014-11-27 13:04:51 +01:00
|
|
|
def test_connectivity_iface(dev1, dev2, ifname, dscp=None, tos=None,
|
2016-12-02 11:53:07 +01:00
|
|
|
max_tries=1, timeout=5):
|
2014-11-27 13:04:51 +01:00
|
|
|
test_connectivity(dev1, dev2, dscp, tos, ifname2=ifname,
|
2016-12-02 11:53:07 +01:00
|
|
|
max_tries=max_tries, timeout=timeout)
|
2014-10-19 16:54:49 +02:00
|
|
|
|
2013-12-24 19:21:58 +01:00
|
|
|
def test_connectivity_p2p(dev1, dev2, dscp=None, tos=None):
|
2014-10-19 19:57:08 +02:00
|
|
|
test_connectivity(dev1, dev2, dscp, tos, dev1group=True, dev2group=True)
|
2013-03-27 12:57:48 +01:00
|
|
|
|
2013-12-24 19:21:58 +01:00
|
|
|
def test_connectivity_p2p_sta(dev1, dev2, dscp=None, tos=None):
|
2014-10-19 19:57:08 +02:00
|
|
|
test_connectivity(dev1, dev2, dscp, tos, dev1group=True, dev2group=False)
|
2013-09-01 10:56:55 +02:00
|
|
|
|
2013-12-24 19:21:58 +01:00
|
|
|
def test_connectivity_sta(dev1, dev2, dscp=None, tos=None):
|
2014-10-19 19:57:08 +02:00
|
|
|
test_connectivity(dev1, dev2, dscp, tos)
|
2015-01-09 19:55:45 +01:00
|
|
|
|
2019-01-24 08:45:44 +01:00
|
|
|
(PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL) = list(range(4))
|
2015-01-09 19:55:45 +01:00
|
|
|
|
|
|
|
def set_powersave(dev, val):
|
|
|
|
phy = dev.get_driver_status_field("phyname")
|
2016-06-23 19:16:30 +02:00
|
|
|
fname = '/sys/kernel/debug/ieee80211/%s/hwsim/ps' % phy
|
|
|
|
data = '%d' % val
|
|
|
|
(res, data) = dev.cmd_execute(["echo", data, ">", fname], shell=True)
|
|
|
|
if res != 0:
|
|
|
|
raise Exception("Failed to set power save for device")
|
2017-01-11 09:44:07 +01:00
|
|
|
|
|
|
|
def set_group_map(dev, val):
|
|
|
|
phy = dev.get_driver_status_field("phyname")
|
|
|
|
fname = '/sys/kernel/debug/ieee80211/%s/hwsim/group' % phy
|
|
|
|
data = '%d' % val
|
|
|
|
(res, data) = dev.cmd_execute(["echo", data, ">", fname], shell=True)
|
|
|
|
if res != 0:
|
|
|
|
raise Exception("Failed to set group map for %s" % phy)
|
2017-08-21 18:43:54 +02:00
|
|
|
|
|
|
|
def set_rx_rssi(dev, val):
|
|
|
|
"""
|
|
|
|
Configure signal strength when receiving transmitted frames.
|
|
|
|
mac80211_hwsim driver sets rssi to: TX power - 50
|
|
|
|
According to that set tx_power in order to get the desired RSSI.
|
|
|
|
Valid RSSI range: -50 to -30.
|
|
|
|
"""
|
|
|
|
tx_power = (val + 50) * 100
|
|
|
|
ifname = dev.get_driver_status_field("ifname")
|
2019-03-15 11:10:37 +01:00
|
|
|
(res, data) = dev.cmd_execute(['iw', ifname, 'set', 'txpower',
|
|
|
|
'fixed', str(tx_power)])
|
2017-08-21 18:43:54 +02:00
|
|
|
if res != 0:
|
|
|
|
raise Exception("Failed to set RSSI to %d" % val)
|
|
|
|
|
|
|
|
def reset_rx_rssi(dev):
|
|
|
|
set_rx_rssi(dev, -30)
|