aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hwsim/hwsim_utils.py
blob: 718d53f464e762f68ea03da5c1af9ddcf44b0c4b (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
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
# hwsim testing utilities
# Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
#
# This software may be distributed under the terms of the BSD license.
# See README for more details.

import os
import time
import logging
logger = logging.getLogger()

from wpasupplicant import WpaSupplicant

def run_connectivity_test(dev1, dev2, tos, dev1group=False, dev2group=False,
                          ifname1=None, ifname2=None, config=True, timeout=5,
                          multicast_to_unicast=False, broadcast=True,
                          send_len=None):
    addr1 = dev1.own_addr()
    if not dev1group and isinstance(dev1, WpaSupplicant):
        addr = dev1.get_driver_status_field('addr')
        if addr:
            addr1 = addr

    addr2 = dev2.own_addr()
    if not dev2group and isinstance(dev2, WpaSupplicant):
        addr = dev2.get_driver_status_field('addr')
        if addr:
            addr2 = addr

    dev1.dump_monitor()
    dev2.dump_monitor()

    if dev1.hostname is None and dev2.hostname is None:
        broadcast_retry_c = 1
    else:
        broadcast_retry_c = 10

    try:
        if config:
            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")

            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")

        cmd = "DATA_TEST_TX {} {} {}".format(addr2, addr1, tos)
        if send_len is not None:
            cmd += " len=" + str(send_len)
        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 unicast data delivery failed")
        if "DATA-TEST-RX {} {}".format(addr2, addr1) not in ev:
            raise Exception("Unexpected dev1->dev2 unicast data result")
        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")

        if broadcast:
            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 xrange(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 "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

        cmd = "DATA_TEST_TX {} {} {}".format(addr1, addr2, tos)
        if send_len is not None:
            cmd += " len=" + str(send_len)
        if dev2group:
            dev2.group_request(cmd)
        else:
            dev2.request(cmd)
        if dev1group:
            ev = dev1.wait_group_event(["DATA-TEST-RX"], timeout=timeout)
        else:
            ev = dev1.wait_event(["DATA-TEST-RX"], timeout=timeout)
        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")
        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")

        if broadcast:
            cmd = "DATA_TEST_TX ff:ff:ff:ff:ff:ff {} {}".format(addr2, tos)
            if send_len is not None:
                cmd += " len=" + str(send_len)
            for i in xrange(broadcast_retry_c):
                try:
                    if dev2group:
                        dev2.group_request(cmd)
                    else:
                        dev2.request(cmd)
                    if dev1group:
                        ev = dev1.wait_group_event(["DATA-TEST-RX"],
                                                   timeout=timeout)
                    else:
                        ev = dev1.wait_event(["DATA-TEST-RX"], timeout=timeout)
                    if ev is None:
                        raise Exception("dev2->dev1 broadcast data delivery failed")
                    if multicast_to_unicast:
                        if "DATA-TEST-RX ff:ff:ff:ff:ff:ff {}".format(addr2) in ev:
                            raise Exception("Unexpected dev2->dev1 broadcast data result: multicast to unicast conversion missing")
                        if "DATA-TEST-RX {} {}".format(addr1, addr2) not in ev:
                            raise Exception("Unexpected dev2->dev1 broadcast data result (multicast to unicast enabled)")
                    else:
                        if "DATA-TEST-RX ff:ff:ff:ff:ff:ff {}".format(addr2) not in ev:
                            raise Exception("Unexpected dev2->dev1 broadcast data result")
                    if send_len is not None:
                        if " len=" + str(send_len) not in ev:
                            raise Exception("Unexpected dev2->dev1 broadcast data length")
                    else:
                        if " len=" in ev:
                            raise Exception("Unexpected dev2->dev1 broadcast data length")
                    break
                except Exception as e:
                    if i == broadcast_retry_c - 1:
                        raise
    finally:
        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")

def test_connectivity(dev1, dev2, dscp=None, tos=None, max_tries=1,
                      dev1group=False, dev2group=False,
                      ifname1=None, ifname2=None, config=True, timeout=5,
                      multicast_to_unicast=False, success_expected=True,
                      broadcast=True, send_len=None):
    if dscp:
        tos = dscp << 2
    if not tos:
        tos = 0

    success = False
    last_err = None
    for i in range(0, max_tries):
        try:
            run_connectivity_test(dev1, dev2, tos, dev1group, dev2group,
                                  ifname1, ifname2, config=config,
                                  timeout=timeout,
                                  multicast_to_unicast=multicast_to_unicast,
                                  broadcast=broadcast, send_len=send_len)
            success = True
            break
        except Exception, e:
            last_err = e
            if i + 1 < max_tries:
                time.sleep(1)
    if success_expected and not success:
        raise Exception(last_err)
    if not success_expected and success:
        raise Exception("Unexpected connectivity detected")

def test_connectivity_iface(dev1, dev2, ifname, dscp=None, tos=None,
                            max_tries=1, timeout=5):
    test_connectivity(dev1, dev2, dscp, tos, ifname2=ifname,
                      max_tries=max_tries, timeout=timeout)

def test_connectivity_p2p(dev1, dev2, dscp=None, tos=None):
    test_connectivity(dev1, dev2, dscp, tos, dev1group=True, dev2group=True)

def test_connectivity_p2p_sta(dev1, dev2, dscp=None, tos=None):
    test_connectivity(dev1, dev2, dscp, tos, dev1group=True, dev2group=False)

def test_connectivity_sta(dev1, dev2, dscp=None, tos=None):
    test_connectivity(dev1, dev2, dscp, tos)

(PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL) = range(4)

def set_powersave(dev, val):
    phy = dev.get_driver_status_field("phyname")
    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")

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)

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")
    (res, data) = dev.cmd_execute([ 'iw', ifname, 'set', 'txpower',
                                    'fixed', str(tx_power)] )
    if res != 0:
        raise Exception("Failed to set RSSI to %d" % val)

def reset_rx_rssi(dev):
    set_rx_rssi(dev, -30)