tests: VHT/HT preference in BSS selection
[mech_eap.git] / tests / hwsim / test_ap_vht.py
1 # Test cases for VHT operations with hostapd
2 # Copyright (c) 2014, Qualcomm Atheros, Inc.
3 # Copyright (c) 2013, Intel Corporation
4 #
5 # This software may be distributed under the terms of the BSD license.
6 # See README for more details.
7
8 import logging
9 logger = logging.getLogger()
10 import os
11 import subprocess, time
12
13 import hwsim_utils
14 import hostapd
15 from utils import HwsimSkip
16 from test_dfs import wait_dfs_event
17 from test_ap_csa import csa_supported
18
19 def vht_supported():
20     cmd = subprocess.Popen(["iw", "reg", "get"], stdout=subprocess.PIPE)
21     reg = cmd.stdout.read()
22     if "@ 80)" in reg or "@ 160)" in reg:
23         return True
24     return False
25
26 def test_ap_vht80(dev, apdev):
27     """VHT with 80 MHz channel width"""
28     try:
29         hapd = None
30         params = { "ssid": "vht",
31                    "country_code": "FI",
32                    "hw_mode": "a",
33                    "channel": "36",
34                    "ht_capab": "[HT40+]",
35                    "ieee80211n": "1",
36                    "ieee80211ac": "1",
37                    "vht_oper_chwidth": "1",
38                    "vht_oper_centr_freq_seg0_idx": "42" }
39         hapd = hostapd.add_ap(apdev[0]['ifname'], params)
40         bssid = apdev[0]['bssid']
41
42         dev[0].connect("vht", key_mgmt="NONE", scan_freq="5180")
43         hwsim_utils.test_connectivity(dev[0], hapd)
44         est = dev[0].get_bss(bssid)['est_throughput']
45         if est != "390001":
46             raise Exception("Unexpected BSS est_throughput: " + est)
47     except Exception, e:
48         if isinstance(e, Exception) and str(e) == "AP startup failed":
49             if not vht_supported():
50                 raise HwsimSkip("80 MHz channel not supported in regulatory information")
51         raise
52     finally:
53         dev[0].request("DISCONNECT")
54         if hapd:
55             hapd.request("DISABLE")
56         subprocess.call(['iw', 'reg', 'set', '00'])
57         dev[0].flush_scan_cache()
58
59 def test_ap_vht80_params(dev, apdev):
60     """VHT with 80 MHz channel width and number of optional features enabled"""
61     try:
62         hapd = None
63         params = { "ssid": "vht",
64                    "country_code": "FI",
65                    "hw_mode": "a",
66                    "channel": "36",
67                    "ht_capab": "[HT40+][SHORT-GI-40][DSS_CCK-40]",
68                    "ieee80211n": "1",
69                    "ieee80211ac": "1",
70                    "vht_oper_chwidth": "1",
71                    "vht_capab": "[MAX-MPDU-11454][RXLDPC][SHORT-GI-80][TX-STBC-2BY1][RX-STBC-1][MAX-A-MPDU-LEN-EXP0]",
72                    "vht_oper_centr_freq_seg0_idx": "42",
73                    "require_vht": "1" }
74         hapd = hostapd.add_ap(apdev[0]['ifname'], params)
75
76         dev[1].connect("vht", key_mgmt="NONE", scan_freq="5180",
77                        disable_vht="1", wait_connect=False)
78         dev[0].connect("vht", key_mgmt="NONE", scan_freq="5180")
79         ev = dev[1].wait_event(["CTRL-EVENT-ASSOC-REJECT"])
80         if ev is None:
81             raise Exception("Association rejection timed out")
82         if "status_code=104" not in ev:
83             raise Exception("Unexpected rejection status code")
84         dev[1].request("DISCONNECT")
85         hwsim_utils.test_connectivity(dev[0], hapd)
86     except Exception, e:
87         if isinstance(e, Exception) and str(e) == "AP startup failed":
88             if not vht_supported():
89                 raise HwsimSkip("80 MHz channel not supported in regulatory information")
90         raise
91     finally:
92         dev[0].request("DISCONNECT")
93         dev[1].request("DISCONNECT")
94         if hapd:
95             hapd.request("DISABLE")
96         subprocess.call(['iw', 'reg', 'set', '00'])
97         dev[0].flush_scan_cache()
98         dev[1].flush_scan_cache()
99
100 def test_ap_vht_20(devs, apdevs):
101     """VHT and 20 MHz channel"""
102     dev = devs[0]
103     ap = apdevs[0]
104     try:
105         hapd = None
106         params = { "ssid": "test-vht20",
107                    "country_code": "DE",
108                    "hw_mode": "a",
109                    "channel": "36",
110                    "ieee80211n": "1",
111                    "ieee80211ac": "1",
112                    "ht_capab": "",
113                    "vht_capab": "",
114                    "vht_oper_chwidth": "0",
115                    "vht_oper_centr_freq_seg0_idx": "0",
116                    "supported_rates": "60 120 240 360 480 540",
117                    "require_vht": "1",
118                  }
119         hapd = hostapd.add_ap(ap['ifname'], params)
120         dev.connect("test-vht20", scan_freq="5180", key_mgmt="NONE")
121         hwsim_utils.test_connectivity(dev, hapd)
122     finally:
123         dev.request("DISCONNECT")
124         if hapd:
125             hapd.request("DISABLE")
126         subprocess.call(['iw', 'reg', 'set', '00'])
127         dev.flush_scan_cache()
128
129 def test_ap_vht_40(devs, apdevs):
130     """VHT and 40 MHz channel"""
131     dev = devs[0]
132     ap = apdevs[0]
133     try:
134         hapd = None
135         params = { "ssid": "test-vht40",
136                    "country_code": "DE",
137                    "hw_mode": "a",
138                    "channel": "36",
139                    "ieee80211n": "1",
140                    "ieee80211ac": "1",
141                    "ht_capab": "[HT40+]",
142                    "vht_capab": "",
143                    "vht_oper_chwidth": "0",
144                    "vht_oper_centr_freq_seg0_idx": "0",
145                  }
146         hapd = hostapd.add_ap(ap['ifname'], params)
147         dev.connect("test-vht40", scan_freq="5180", key_mgmt="NONE")
148         hwsim_utils.test_connectivity(dev, hapd)
149     finally:
150         dev.request("DISCONNECT")
151         if hapd:
152             hapd.request("DISABLE")
153         subprocess.call(['iw', 'reg', 'set', '00'])
154         dev.flush_scan_cache()
155
156 def test_ap_vht_capab_not_supported(dev, apdev):
157     """VHT configuration with driver not supporting all vht_capab entries"""
158     try:
159         params = { "ssid": "vht",
160                    "country_code": "FI",
161                    "hw_mode": "a",
162                    "channel": "36",
163                    "ht_capab": "[HT40+][SHORT-GI-40][DSS_CCK-40]",
164                    "ieee80211n": "1",
165                    "ieee80211ac": "1",
166                    "vht_oper_chwidth": "1",
167                    "vht_capab": "[MAX-MPDU-7991][MAX-MPDU-11454][VHT160][VHT160-80PLUS80][RXLDPC][SHORT-GI-80][SHORT-GI-160][TX-STBC-2BY1][RX-STBC-1][RX-STBC-12][RX-STBC-123][RX-STBC-1234][SU-BEAMFORMER][SU-BEAMFORMEE][BF-ANTENNA-2][SOUNDING-DIMENSION-2][MU-BEAMFORMER][MU-BEAMFORMEE][VHT-TXOP-PS][HTC-VHT][MAX-A-MPDU-LEN-EXP0][MAX-A-MPDU-LEN-EXP7][VHT-LINK-ADAPT2][VHT-LINK-ADAPT3][RX-ANTENNA-PATTERN][TX-ANTENNA-PATTERN]",
168                    "vht_oper_centr_freq_seg0_idx": "42",
169                    "require_vht": "1" }
170         hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)
171         ev = hapd.wait_event(["AP-DISABLED"], timeout=5)
172         if ev is None:
173             raise Exception("Startup failure not reported")
174         for i in range(1, 7):
175             if "OK" not in hapd.request("SET vht_capab [MAX-A-MPDU-LEN-EXP%d]" % i):
176                 raise Exception("Unexpected SET failure")
177     finally:
178         subprocess.call(['iw', 'reg', 'set', '00'])
179
180 def test_ap_vht160(dev, apdev):
181     """VHT with 160 MHz channel width"""
182     try:
183         hapd = None
184         hapd2 = None
185         params = { "ssid": "vht",
186                    "country_code": "FI",
187                    "hw_mode": "a",
188                    "channel": "36",
189                    "ht_capab": "[HT40+]",
190                    "ieee80211n": "1",
191                    "ieee80211ac": "1",
192                    "vht_oper_chwidth": "2",
193                    "vht_oper_centr_freq_seg0_idx": "50",
194                    'ieee80211d': '1',
195                    'ieee80211h': '1' }
196         hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)
197
198         ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
199         if "DFS-CAC-START" not in ev:
200             raise Exception("Unexpected DFS event")
201
202         state = hapd.get_status_field("state")
203         if state != "DFS":
204             if state == "DISABLED" and not os.path.exists("dfs"):
205                 # Not all systems have recent enough CRDA version and
206                 # wireless-regdb changes to support 160 MHz and DFS. For now,
207                 # do not report failures for this test case.
208                 raise HwsimSkip("CRDA or wireless-regdb did not support 160 MHz")
209             raise Exception("Unexpected interface state: " + state)
210
211         params = { "ssid": "vht2",
212                    "country_code": "FI",
213                    "hw_mode": "a",
214                    "channel": "100",
215                    "ht_capab": "[HT40+]",
216                    "ieee80211n": "1",
217                    "ieee80211ac": "1",
218                    "vht_oper_chwidth": "2",
219                    "vht_oper_centr_freq_seg0_idx": "114",
220                    'ieee80211d': '1',
221                    'ieee80211h': '1' }
222         hapd2 = hostapd.add_ap(apdev[1]['ifname'], params, wait_enabled=False)
223
224         ev = wait_dfs_event(hapd2, "DFS-CAC-START", 5)
225         if "DFS-CAC-START" not in ev:
226             raise Exception("Unexpected DFS event(2)")
227
228         state = hapd2.get_status_field("state")
229         if state != "DFS":
230             raise Exception("Unexpected interface state(2): " + state)
231
232         logger.info("Waiting for CAC to complete")
233
234         ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
235         if "success=1" not in ev:
236             raise Exception("CAC failed")
237         if "freq=5180" not in ev:
238             raise Exception("Unexpected DFS freq result")
239
240         ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
241         if not ev:
242             raise Exception("AP setup timed out")
243
244         state = hapd.get_status_field("state")
245         if state != "ENABLED":
246             raise Exception("Unexpected interface state")
247
248         ev = wait_dfs_event(hapd2, "DFS-CAC-COMPLETED", 70)
249         if "success=1" not in ev:
250             raise Exception("CAC failed(2)")
251         if "freq=5500" not in ev:
252             raise Exception("Unexpected DFS freq result(2)")
253
254         ev = hapd2.wait_event(["AP-ENABLED"], timeout=5)
255         if not ev:
256             raise Exception("AP setup timed out(2)")
257
258         state = hapd2.get_status_field("state")
259         if state != "ENABLED":
260             raise Exception("Unexpected interface state(2)")
261
262         freq = hapd2.get_status_field("freq")
263         if freq != "5500":
264             raise Exception("Unexpected frequency(2)")
265
266         dev[0].connect("vht", key_mgmt="NONE", scan_freq="5180")
267         hwsim_utils.test_connectivity(dev[0], hapd)
268         sig = dev[0].request("SIGNAL_POLL").splitlines()
269         if "FREQUENCY=5180" not in sig:
270             raise Exception("Unexpected SIGNAL_POLL value(1): " + str(sig))
271         if "WIDTH=160 MHz" not in sig:
272             raise Exception("Unexpected SIGNAL_POLL value(2): " + str(sig))
273         dev[1].connect("vht2", key_mgmt="NONE", scan_freq="5500")
274         hwsim_utils.test_connectivity(dev[1], hapd2)
275         sig = dev[1].request("SIGNAL_POLL").splitlines()
276         if "FREQUENCY=5500" not in sig:
277             raise Exception("Unexpected SIGNAL_POLL value(1): " + str(sig))
278         if "WIDTH=160 MHz" not in sig:
279             raise Exception("Unexpected SIGNAL_POLL value(2): " + str(sig))
280     except Exception, e:
281         if isinstance(e, Exception) and str(e) == "AP startup failed":
282             if not vht_supported():
283                 raise HwsimSkip("80/160 MHz channel not supported in regulatory information")
284         raise
285     finally:
286         dev[0].request("DISCONNECT")
287         dev[1].request("DISCONNECT")
288         if hapd:
289             hapd.request("DISABLE")
290         if hapd2:
291             hapd2.request("DISABLE")
292         subprocess.call(['iw', 'reg', 'set', '00'])
293         dev[0].flush_scan_cache()
294         dev[1].flush_scan_cache()
295
296 def test_ap_vht80plus80(dev, apdev):
297     """VHT with 80+80 MHz channel width"""
298     try:
299         hapd = None
300         hapd2 = None
301         params = { "ssid": "vht",
302                    "country_code": "US",
303                    "hw_mode": "a",
304                    "channel": "52",
305                    "ht_capab": "[HT40+]",
306                    "ieee80211n": "1",
307                    "ieee80211ac": "1",
308                    "vht_oper_chwidth": "3",
309                    "vht_oper_centr_freq_seg0_idx": "58",
310                    "vht_oper_centr_freq_seg1_idx": "155",
311                    'ieee80211d': '1',
312                    'ieee80211h': '1' }
313         hapd = hostapd.add_ap(apdev[0]['ifname'], params, wait_enabled=False)
314         # This will actually fail since DFS on 80+80 is not yet supported
315         ev = hapd.wait_event(["AP-DISABLED"], timeout=5)
316         # ignore result to avoid breaking the test once 80+80 DFS gets enabled
317
318         params = { "ssid": "vht2",
319                    "country_code": "US",
320                    "hw_mode": "a",
321                    "channel": "36",
322                    "ht_capab": "[HT40+]",
323                    "ieee80211n": "1",
324                    "ieee80211ac": "1",
325                    "vht_oper_chwidth": "3",
326                    "vht_oper_centr_freq_seg0_idx": "42",
327                    "vht_oper_centr_freq_seg1_idx": "155" }
328         hapd2 = hostapd.add_ap(apdev[1]['ifname'], params, wait_enabled=False)
329
330         ev = hapd2.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=5)
331         if not ev:
332             raise Exception("AP setup timed out(2)")
333         if "AP-DISABLED" in ev:
334             # Assume this failed due to missing regulatory update for now
335             raise HwsimSkip("80+80 MHz channel not supported in regulatory information")
336
337         state = hapd2.get_status_field("state")
338         if state != "ENABLED":
339             raise Exception("Unexpected interface state(2)")
340
341         dev[1].connect("vht2", key_mgmt="NONE", scan_freq="5180")
342         hwsim_utils.test_connectivity(dev[1], hapd2)
343         sig = dev[1].request("SIGNAL_POLL").splitlines()
344         if "FREQUENCY=5180" not in sig:
345             raise Exception("Unexpected SIGNAL_POLL value(1): " + str(sig))
346         if "WIDTH=80+80 MHz" not in sig:
347             raise Exception("Unexpected SIGNAL_POLL value(2): " + str(sig))
348         if "CENTER_FRQ1=5210" not in sig:
349             raise Exception("Unexpected SIGNAL_POLL value(3): " + str(sig))
350         if "CENTER_FRQ2=5775" not in sig:
351             raise Exception("Unexpected SIGNAL_POLL value(4): " + str(sig))
352     except Exception, e:
353         if isinstance(e, Exception) and str(e) == "AP startup failed":
354             if not vht_supported():
355                 raise HwsimSkip("80/160 MHz channel not supported in regulatory information")
356         raise
357     finally:
358         dev[0].request("DISCONNECT")
359         dev[1].request("DISCONNECT")
360         if hapd:
361             hapd.request("DISABLE")
362         if hapd2:
363             hapd2.request("DISABLE")
364         subprocess.call(['iw', 'reg', 'set', '00'])
365         dev[0].flush_scan_cache()
366         dev[1].flush_scan_cache()
367
368 def test_ap_vht80_csa(dev, apdev):
369     """VHT with 80 MHz channel width and CSA"""
370     csa_supported(dev[0])
371     try:
372         hapd = None
373         params = { "ssid": "vht",
374                    "country_code": "US",
375                    "hw_mode": "a",
376                    "channel": "149",
377                    "ht_capab": "[HT40+]",
378                    "ieee80211n": "1",
379                    "ieee80211ac": "1",
380                    "vht_oper_chwidth": "1",
381                    "vht_oper_centr_freq_seg0_idx": "155" }
382         hapd = hostapd.add_ap(apdev[0]['ifname'], params)
383
384         dev[0].connect("vht", key_mgmt="NONE", scan_freq="5745")
385         hwsim_utils.test_connectivity(dev[0], hapd)
386
387         hapd.request("CHAN_SWITCH 5 5180 ht vht blocktx center_freq1=5210 sec_channel_offset=1 bandwidth=80")
388         ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=10)
389         if ev is None:
390             raise Exception("CSA finished event timed out")
391         if "freq=5180" not in ev:
392             raise Exception("Unexpected channel in CSA finished event")
393         time.sleep(0.5)
394         hwsim_utils.test_connectivity(dev[0], hapd)
395
396         hapd.request("CHAN_SWITCH 5 5745")
397         ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=10)
398         if ev is None:
399             raise Exception("CSA finished event timed out")
400         if "freq=5745" not in ev:
401             raise Exception("Unexpected channel in CSA finished event")
402         time.sleep(0.5)
403         hwsim_utils.test_connectivity(dev[0], hapd)
404
405         # This CSA to same channel will fail in kernel, so use this only for
406         # extra code coverage.
407         hapd.request("CHAN_SWITCH 5 5745")
408         hapd.wait_event(["AP-CSA-FINISHED"], timeout=1)
409     except Exception, e:
410         if isinstance(e, Exception) and str(e) == "AP startup failed":
411             if not vht_supported():
412                 raise HwsimSkip("80 MHz channel not supported in regulatory information")
413         raise
414     finally:
415         dev[0].request("DISCONNECT")
416         if hapd:
417             hapd.request("DISABLE")
418         subprocess.call(['iw', 'reg', 'set', '00'])
419         dev[0].flush_scan_cache()
420
421 def test_ap_vht_on_24ghz(dev, apdev):
422     """Subset of VHT features on 2.4 GHz"""
423     hapd = None
424     params = { "ssid": "test-vht-2g",
425                "hw_mode": "g",
426                "channel": "1",
427                "ieee80211n": "1",
428                "vendor_vht": "1",
429                "vht_capab": "[MAX-MPDU-11454]",
430                "vht_oper_chwidth": "0",
431                "vht_oper_centr_freq_seg0_idx": "1"
432     }
433     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
434     try:
435         if "OK" not in dev[0].request("VENDOR_ELEM_ADD 13 dd1300904c0400bf0c3240820feaff0000eaff0000"):
436             raise Exception("Failed to add vendor element")
437         dev[0].connect("test-vht-2g", scan_freq="2412", key_mgmt="NONE")
438         hwsim_utils.test_connectivity(dev[0], hapd)
439         sta = hapd.get_sta(dev[0].own_addr())
440         if '[VENDOR_VHT]' not in sta['flags']:
441             raise Exception("No VENDOR_VHT STA flag")
442
443         dev[1].connect("test-vht-2g", scan_freq="2412", key_mgmt="NONE")
444         sta = hapd.get_sta(dev[1].own_addr())
445         if '[VENDOR_VHT]' in sta['flags']:
446             raise Exception("Unexpected VENDOR_VHT STA flag")
447     finally:
448         dev[0].request("VENDOR_ELEM_REMOVE 13 *")
449
450 def test_prefer_vht40(dev, apdev):
451     """Preference on VHT40 over HT40"""
452     try:
453         hapd2 = None
454
455         params = { "ssid": "test",
456                    "country_code": "FI",
457                    "hw_mode": "a",
458                    "channel": "36",
459                    "ieee80211n": "1",
460                    "ht_capab": "[HT40+]" }
461         hapd = hostapd.add_ap(apdev[0]['ifname'], params)
462         bssid = apdev[0]['bssid']
463
464         params = { "ssid": "test",
465                    "country_code": "FI",
466                    "hw_mode": "a",
467                    "channel": "36",
468                    "ieee80211n": "1",
469                    "ieee80211ac": "1",
470                    "ht_capab": "[HT40+]",
471                    "vht_capab": "",
472                    "vht_oper_chwidth": "0",
473                    "vht_oper_centr_freq_seg0_idx": "0",
474                  }
475         hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)
476         bssid2 = apdev[1]['bssid']
477
478         dev[0].scan_for_bss(bssid, freq=5180)
479         dev[0].scan_for_bss(bssid2, freq=5180)
480         dev[0].connect("test", scan_freq="5180", key_mgmt="NONE")
481         if dev[0].get_status_field('bssid') != bssid2:
482             raise Exception("Unexpected BSS selected")
483
484         est = dev[0].get_bss(bssid)['est_throughput']
485         if est != "135000":
486             raise Exception("Unexpected BSS0 est_throughput: " + est)
487
488         est = dev[0].get_bss(bssid2)['est_throughput']
489         if est != "135001":
490             raise Exception("Unexpected BSS1 est_throughput: " + est)
491     finally:
492         dev[0].request("DISCONNECT")
493         if hapd2:
494             hapd2.request("DISABLE")
495         subprocess.call(['iw', 'reg', 'set', '00'])
496         dev[0].flush_scan_cache()