1# QoS Mapping tests
2# Copyright (c) 2013-2016, Jouni Malinen <j@w1.fi>
3#
4# This software may be distributed under the terms of the BSD license.
5# See README for more details.
6
7from remotehost import remote_compatible
8import time
9import logging
10logger = logging.getLogger()
11
12import hwsim_utils
13import hostapd
14from utils import HwsimSkip, alloc_fail, fail_test
15from wlantest import Wlantest
16
17def check_qos_map(ap, hapd, dev, sta, dscp, tid, ap_tid=None):
18    if not ap_tid:
19        ap_tid = tid
20    bssid = ap['bssid']
21    wt = Wlantest()
22    wt.clear_sta_counters(bssid, sta)
23    hwsim_utils.test_connectivity(dev, hapd, dscp=dscp, config=False)
24    sleep_time = 0.02 if dev.hostname is None else 0.2
25    time.sleep(sleep_time)
26    tx = wt.get_tx_tid(bssid, sta, tid)
27    if tx == 0:
28        [tx, rx] = wt.get_tid_counters(bssid, sta)
29        logger.info("Expected TX DSCP " + str(dscp) + " with TID " + str(tid) + " but counters: " + str(tx))
30        raise Exception("No STA->AP data frame using the expected TID")
31    rx = wt.get_rx_tid(bssid, sta, ap_tid)
32    if rx == 0:
33        [tx, rx] = wt.get_tid_counters(bssid, sta)
34        logger.info("Expected RX DSCP " + str(dscp) + " with TID " + str(ap_tid) + " but counters: " + str(rx))
35        raise Exception("No AP->STA data frame using the expected TID")
36
37@remote_compatible
38def test_ap_qosmap(dev, apdev):
39    """QoS mapping"""
40    drv_flags = dev[0].get_driver_status_field("capa.flags")
41    if int(drv_flags, 0) & 0x40000000 == 0:
42        raise HwsimSkip("Driver does not support QoS Map")
43    ssid = "test-qosmap"
44    params = {"ssid": ssid}
45    params['qos_map_set'] = '53,2,22,6,8,15,0,7,255,255,16,31,32,39,255,255,40,47,48,55'
46    hapd = hostapd.add_ap(apdev[0], params)
47    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
48    time.sleep(0.1)
49    addr = dev[0].p2p_interface_addr()
50    dev[0].request("DATA_TEST_CONFIG 1")
51    hapd.request("DATA_TEST_CONFIG 1")
52    Wlantest.setup(hapd)
53    check_qos_map(apdev[0], hapd, dev[0], addr, 53, 2)
54    check_qos_map(apdev[0], hapd, dev[0], addr, 22, 6)
55    check_qos_map(apdev[0], hapd, dev[0], addr, 8, 0)
56    check_qos_map(apdev[0], hapd, dev[0], addr, 15, 0)
57    check_qos_map(apdev[0], hapd, dev[0], addr, 0, 1)
58    check_qos_map(apdev[0], hapd, dev[0], addr, 7, 1)
59    check_qos_map(apdev[0], hapd, dev[0], addr, 16, 3)
60    check_qos_map(apdev[0], hapd, dev[0], addr, 31, 3)
61    check_qos_map(apdev[0], hapd, dev[0], addr, 32, 4)
62    check_qos_map(apdev[0], hapd, dev[0], addr, 39, 4)
63    check_qos_map(apdev[0], hapd, dev[0], addr, 40, 6)
64    check_qos_map(apdev[0], hapd, dev[0], addr, 47, 6)
65    check_qos_map(apdev[0], hapd, dev[0], addr, 48, 7)
66    check_qos_map(apdev[0], hapd, dev[0], addr, 55, 7)
67    hapd.request("SET_QOS_MAP_SET 22,6,8,15,0,7,255,255,16,31,32,39,255,255,40,47,48,55")
68    hapd.request("SEND_QOS_MAP_CONF " + dev[0].get_status_field("address"))
69    check_qos_map(apdev[0], hapd, dev[0], addr, 53, 7)
70    check_qos_map(apdev[0], hapd, dev[0], addr, 22, 6)
71    check_qos_map(apdev[0], hapd, dev[0], addr, 48, 7)
72    check_qos_map(apdev[0], hapd, dev[0], addr, 55, 7)
73    check_qos_map(apdev[0], hapd, dev[0], addr, 56, 56 >> 3)
74    check_qos_map(apdev[0], hapd, dev[0], addr, 63, 63 >> 3)
75    dev[0].request("DATA_TEST_CONFIG 0")
76    hapd.request("DATA_TEST_CONFIG 0")
77
78@remote_compatible
79def test_ap_qosmap_default(dev, apdev):
80    """QoS mapping with default values"""
81    ssid = "test-qosmap-default"
82    params = {"ssid": ssid}
83    hapd = hostapd.add_ap(apdev[0], params)
84    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
85    addr = dev[0].p2p_interface_addr()
86    dev[0].request("DATA_TEST_CONFIG 1")
87    hapd.request("DATA_TEST_CONFIG 1")
88    Wlantest.setup(hapd)
89    for dscp in [0, 7, 8, 15, 16, 23, 24, 31, 32, 39, 40, 47, 48, 55, 56, 63]:
90        check_qos_map(apdev[0], hapd, dev[0], addr, dscp, dscp >> 3)
91    dev[0].request("DATA_TEST_CONFIG 0")
92    hapd.request("DATA_TEST_CONFIG 0")
93
94@remote_compatible
95def test_ap_qosmap_default_acm(dev, apdev):
96    """QoS mapping with default values and ACM=1 for VO/VI"""
97    ssid = "test-qosmap-default"
98    params = {"ssid": ssid,
99              "wmm_ac_bk_aifs": "7",
100              "wmm_ac_bk_cwmin": "4",
101              "wmm_ac_bk_cwmax": "10",
102              "wmm_ac_bk_txop_limit": "0",
103              "wmm_ac_bk_acm": "0",
104              "wmm_ac_be_aifs": "3",
105              "wmm_ac_be_cwmin": "4",
106              "wmm_ac_be_cwmax": "10",
107              "wmm_ac_be_txop_limit": "0",
108              "wmm_ac_be_acm": "0",
109              "wmm_ac_vi_aifs": "2",
110              "wmm_ac_vi_cwmin": "3",
111              "wmm_ac_vi_cwmax": "4",
112              "wmm_ac_vi_txop_limit": "94",
113              "wmm_ac_vi_acm": "1",
114              "wmm_ac_vo_aifs": "2",
115              "wmm_ac_vo_cwmin": "2",
116              "wmm_ac_vo_cwmax": "2",
117              "wmm_ac_vo_txop_limit": "47",
118              "wmm_ac_vo_acm": "1"}
119    hapd = hostapd.add_ap(apdev[0], params)
120    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
121    addr = dev[0].p2p_interface_addr()
122    dev[0].request("DATA_TEST_CONFIG 1")
123    hapd.request("DATA_TEST_CONFIG 1")
124    Wlantest.setup(hapd)
125    for dscp in [0, 7, 8, 15, 16, 23, 24, 31, 32, 39, 40, 47, 48, 55, 56, 63]:
126        ap_tid = dscp >> 3
127        tid = ap_tid
128        # downgrade VI/VO to BE
129        if tid in [4, 5, 6, 7]:
130            tid = 3
131        check_qos_map(apdev[0], hapd, dev[0], addr, dscp, tid, ap_tid)
132    dev[0].request("DATA_TEST_CONFIG 0")
133    hapd.request("DATA_TEST_CONFIG 0")
134
135@remote_compatible
136def test_ap_qosmap_invalid(dev, apdev):
137    """QoS mapping ctrl_iface error handling"""
138    ssid = "test-qosmap"
139    params = {"ssid": ssid}
140    hapd = hostapd.add_ap(apdev[0], params)
141    if "FAIL" not in hapd.request("SEND_QOS_MAP_CONF 00:11:22:33:44:55"):
142        raise Exception("Unexpected SEND_QOS_MAP_CONF success")
143    if "FAIL" not in hapd.request("SET_QOS_MAP_SET "):
144        raise Exception("Unexpected SET_QOS_MAP_SET success")
145    if "FAIL" not in hapd.request("SET_QOS_MAP_SET 1,2,3"):
146        raise Exception("Unexpected SET_QOS_MAP_SET success")
147    if "FAIL" not in hapd.request("SET_QOS_MAP_SET 1,-2,3"):
148        raise Exception("Unexpected SET_QOS_MAP_SET success")
149    if "FAIL" not in hapd.request("SET_QOS_MAP_SET 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"):
150        raise Exception("Unexpected SET_QOS_MAP_SET success")
151    if "FAIL" not in hapd.request("SET_QOS_MAP_SET 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21"):
152        raise Exception("Unexpected SET_QOS_MAP_SET success")
153
154    if "FAIL" in hapd.request("SET_QOS_MAP_SET 22,6,8,15,0,7,255,255,16,31,32,39,255,255,40,47,48,55"):
155        raise Exception("Unexpected SET_QOS_MAP_SET failure")
156    if "FAIL" not in hapd.request("SEND_QOS_MAP_CONF 00:11:22:33:44:55"):
157        raise Exception("Unexpected SEND_QOS_MAP_CONF success")
158    if "FAIL" not in hapd.request("SEND_QOS_MAP_CONF 00:11:22:33:44"):
159        raise Exception("Unexpected SEND_QOS_MAP_CONF success")
160
161    with fail_test(hapd, 1, "hostapd_ctrl_iface_set_qos_map_set"):
162        if "FAIL" not in hapd.request("SET_QOS_MAP_SET 22,6,8,15,0,7,255,255,16,31,32,39,255,255,40,47,48,55"):
163            raise Exception("SET_QOS_MAP_SET accepted during forced driver failure")
164
165    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
166    with alloc_fail(hapd, 1,
167                    "wpabuf_alloc;hostapd_ctrl_iface_send_qos_map_conf"):
168        if "FAIL" not in hapd.request("SEND_QOS_MAP_CONF " + dev[0].own_addr()):
169            raise Exception("SEND_QOS_MAP_CONF accepted during OOM")
170