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
10import netlink, os
11
12# constants
13HWSIM_CMD_CREATE_RADIO = 4
14HWSIM_CMD_DESTROY_RADIO = 5
15
16HWSIM_ATTR_CHANNELS = 9
17HWSIM_ATTR_RADIO_ID = 10
18HWSIM_ATTR_SUPPORT_P2P_DEVICE = 14
19HWSIM_ATTR_USE_CHANCTX = 15
20
21# the controller class
22class HWSimController(object):
23    def __init__(self):
24        self._conn = netlink.Connection(netlink.NETLINK_GENERIC)
25        self._fid = netlink.genl_controller.get_family_id(b'MAC80211_HWSIM')
26
27    def create_radio(self, n_channels=None, use_chanctx=False,
28                     use_p2p_device=False):
29        attrs = []
30        if n_channels:
31            attrs.append(netlink.U32Attr(HWSIM_ATTR_CHANNELS, n_channels))
32        if use_chanctx:
33            attrs.append(netlink.FlagAttr(HWSIM_ATTR_USE_CHANCTX))
34        if use_p2p_device:
35            attrs.append(netlink.FlagAttr(HWSIM_ATTR_SUPPORT_P2P_DEVICE))
36
37        msg = netlink.GenlMessage(self._fid, HWSIM_CMD_CREATE_RADIO,
38                                  flags=netlink.NLM_F_REQUEST |
39                                        netlink.NLM_F_ACK,
40                                  attrs=attrs)
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,
46                                  flags=netlink.NLM_F_REQUEST |
47                                        netlink.NLM_F_ACK,
48                                  attrs=attrs)
49        msg.send_and_recv(self._conn)
50
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]
68        except Exception as e:
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
77def create(args):
78    print('Created radio %d' % c.create_radio(n_channels=args.channels,
79                                              use_chanctx=args.chanctx))
80
81def destroy(args):
82    print(c.destroy_radio(args.radio))
83
84if __name__ == '__main__':
85    import argparse
86    c = HWSimController()
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)
115