2 # HWSIM generic netlink controller code
3 # Copyright (c) 2014 Intel Corporation
5 # Author: Johannes Berg <johannes.berg@intel.com>
7 # This software may be distributed under the terms of the BSD license.
8 # See README for more details.
13 HWSIM_CMD_CREATE_RADIO
= 4
14 HWSIM_CMD_DESTROY_RADIO
= 5
16 HWSIM_ATTR_CHANNELS
= 9
17 HWSIM_ATTR_RADIO_ID
= 10
18 HWSIM_ATTR_SUPPORT_P2P_DEVICE
= 14
19 HWSIM_ATTR_USE_CHANCTX
= 15
21 # the controller class
22 class HWSimController(object):
24 self
._conn
= netlink
.Connection(netlink
.NETLINK_GENERIC
)
25 self
._fid
= netlink
.genl_controller
.get_family_id('MAC80211_HWSIM')
27 def create_radio(self
, n_channels
=None, use_chanctx
=False,
28 use_p2p_device
=False):
31 attrs
.append(netlink
.U32Attr(HWSIM_ATTR_CHANNELS
, n_channels
))
33 attrs
.append(netlink
.FlagAttr(HWSIM_ATTR_USE_CHANCTX
))
35 attrs
.append(netlink
.FlagAttr(HWSIM_ATTR_SUPPORT_P2P_DEVICE
))
37 msg
= netlink
.GenlMessage(self
._fid
, HWSIM_CMD_CREATE_RADIO
,
38 flags
= netlink
.NLM_F_REQUEST |
41 return msg
.send_and_recv(self
._conn
).ret
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
,
46 flags
= netlink
.NLM_F_REQUEST |
49 msg
.send_and_recv(self
._conn
)
51 class 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
_p
2p
_dev
= use_p2p_device
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
_p
2p
_dev
)
64 if self
._radio
_id
< 0:
65 raise Exception("Failed to create radio (err:%d)" % self
._radio
_id
)
67 iface
= os
.listdir('/sys/class/mac80211_hwsim/hwsim%d/net/' % self
._radio
_id
)[0]
68 except Exception as e
:
69 self
._controller
.destroy_radio(self
._radio
_id
)
71 return self
._radio
_id
, iface
73 def __exit__(self
, type, value
, traceback
):
74 self
._controller
.destroy_radio(self
._radio
_id
)
78 print 'Created radio %d' % c
.create_radio(n_channels
=args
.channels
,
79 use_chanctx
=args
.chanctx
)
82 print c
.destroy_radio(args
.radio
)
84 if __name__
== '__main__':
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,
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 ' +
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 ' +
104 parser_create
.set_defaults(func
=create
)
106 parser_destroy
= subparsers
.add_parser('destroy', help='destroy a radio')
107 parser_destroy
.add_argument('radio', metavar
='<radio>', type=int,
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
)
113 args
= parser
.parse_args()