]> git.ipfire.org Git - thirdparty/hostap.git/blame - tests/hwsim/hwsim.py
tests: sigma_dut controlled STA and beacon protection
[thirdparty/hostap.git] / tests / hwsim / hwsim.py
CommitLineData
fe869124
JB
1#
2# HWSIM generic netlink controller code
3# Copyright (c) 2014 Intel Corporation
4#
5# Author: Johannes Berg <johannes.berg@intel.com>
6#
7# This software may be distributed under the terms of the BSD license.
8# See README for more details.
9
8a5cc2fe 10import netlink, os
fe869124
JB
11
12# constants
fab49f61
JM
13HWSIM_CMD_CREATE_RADIO = 4
14HWSIM_CMD_DESTROY_RADIO = 5
fe869124 15
fab49f61
JM
16HWSIM_ATTR_CHANNELS = 9
17HWSIM_ATTR_RADIO_ID = 10
18HWSIM_ATTR_SUPPORT_P2P_DEVICE = 14
19HWSIM_ATTR_USE_CHANCTX = 15
fe869124
JB
20
21# the controller class
22class HWSimController(object):
23 def __init__(self):
24 self._conn = netlink.Connection(netlink.NETLINK_GENERIC)
e285418b 25 self._fid = netlink.genl_controller.get_family_id(b'MAC80211_HWSIM')
fe869124 26
91d7a9a1
JB
27 def create_radio(self, n_channels=None, use_chanctx=False,
28 use_p2p_device=False):
fe869124
JB
29 attrs = []
30 if n_channels:
31 attrs.append(netlink.U32Attr(HWSIM_ATTR_CHANNELS, n_channels))
be32ace4
LC
32 if use_chanctx:
33 attrs.append(netlink.FlagAttr(HWSIM_ATTR_USE_CHANCTX))
91d7a9a1
JB
34 if use_p2p_device:
35 attrs.append(netlink.FlagAttr(HWSIM_ATTR_SUPPORT_P2P_DEVICE))
be32ace4 36
fe869124 37 msg = netlink.GenlMessage(self._fid, HWSIM_CMD_CREATE_RADIO,
fab49f61
JM
38 flags=netlink.NLM_F_REQUEST |
39 netlink.NLM_F_ACK,
40 attrs=attrs)
fe869124
JB
41 return msg.send_and_recv(self._conn).ret
42
43 def destroy_radio(self, radio_id):
44 attrs = [netlink.U32Attr(HWSIM_ATTR_RADIO_ID, radio_id)]
45 msg = netlink.GenlMessage(self._fid, HWSIM_CMD_DESTROY_RADIO,
fab49f61
JM
46 flags=netlink.NLM_F_REQUEST |
47 netlink.NLM_F_ACK,
48 attrs=attrs)
fe869124
JB
49 msg.send_and_recv(self._conn)
50
8a5cc2fe
JB
51class HWSimRadio(object):
52 def __init__(self, n_channels=None, use_chanctx=False,
53 use_p2p_device=False):
54 self._controller = HWSimController()
55 self._n_channels = n_channels
56 self._use_chanctx = use_chanctx
57 self._use_p2p_dev = use_p2p_device
58
59 def __enter__(self):
60 self._radio_id = self._controller.create_radio(
61 n_channels=self._n_channels,
62 use_chanctx=self._use_chanctx,
63 use_p2p_device=self._use_p2p_dev)
64 if self._radio_id < 0:
65 raise Exception("Failed to create radio (err:%d)" % self._radio_id)
66 try:
67 iface = os.listdir('/sys/class/mac80211_hwsim/hwsim%d/net/' % self._radio_id)[0]
bab493b9 68 except Exception as e:
8a5cc2fe
JB
69 self._controller.destroy_radio(self._radio_id)
70 raise e
71 return self._radio_id, iface
72
73 def __exit__(self, type, value, traceback):
74 self._controller.destroy_radio(self._radio_id)
75
76
be32ace4 77def create(args):
89896c00
MH
78 print('Created radio %d' % c.create_radio(n_channels=args.channels,
79 use_chanctx=args.chanctx))
be32ace4
LC
80
81def destroy(args):
89896c00 82 print(c.destroy_radio(args.radio))
be32ace4 83
fe869124 84if __name__ == '__main__':
be32ace4 85 import argparse
fe869124 86 c = HWSimController()
be32ace4
LC
87
88 parser = argparse.ArgumentParser(description='send hwsim control commands')
89 subparsers = parser.add_subparsers(help="Commands", dest='command')
90 parser_create = subparsers.add_parser('create', help='create a radio')
91 parser_create.add_argument('--channels', metavar='<number_of_channels>', type=int,
92 default=0,
93 help='Number of concurrent channels supported ' +
94 'by the radio. If not specified, the number ' +
95 'of channels specified in the ' +
96 'mac80211_hwsim.channels module parameter is ' +
97 'used')
98 parser_create.add_argument('--chanctx', action="store_true",
99 help='Use channel contexts, regardless of ' +
100 'whether the number of channels is 1 or ' +
101 'greater. By default channel contexts are ' +
102 'only used if the number of channels is ' +
103 'greater than 1.')
104 parser_create.set_defaults(func=create)
105
106 parser_destroy = subparsers.add_parser('destroy', help='destroy a radio')
107 parser_destroy.add_argument('radio', metavar='<radio>', type=int,
108 default=0,
109 help='The number of the radio to be ' +
110 'destroyed (i.e., 0 for phy0, 1 for phy1...)')
111 parser_destroy.set_defaults(func=destroy)
112
113 args = parser.parse_args()
114 args.func(args)