]>
Commit | Line | Data |
---|---|---|
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 | ||
10 | import netlink | |
11 | ||
12 | # constants | |
13 | HWSIM_CMD_CREATE_RADIO = 4 | |
14 | HWSIM_CMD_DESTROY_RADIO = 5 | |
15 | ||
16 | HWSIM_ATTR_CHANNELS = 9 | |
17 | HWSIM_ATTR_RADIO_ID = 10 | |
be32ace4 | 18 | HWSIM_ATTR_USE_CHANCTX = 15 |
fe869124 JB |
19 | |
20 | # the controller class | |
21 | class HWSimController(object): | |
22 | def __init__(self): | |
23 | self._conn = netlink.Connection(netlink.NETLINK_GENERIC) | |
24 | self._fid = netlink.genl_controller.get_family_id('MAC80211_HWSIM') | |
25 | ||
be32ace4 | 26 | def create_radio(self, n_channels=None, use_chanctx=False): |
fe869124 JB |
27 | attrs = [] |
28 | if n_channels: | |
29 | attrs.append(netlink.U32Attr(HWSIM_ATTR_CHANNELS, n_channels)) | |
be32ace4 LC |
30 | if use_chanctx: |
31 | attrs.append(netlink.FlagAttr(HWSIM_ATTR_USE_CHANCTX)) | |
32 | ||
fe869124 JB |
33 | msg = netlink.GenlMessage(self._fid, HWSIM_CMD_CREATE_RADIO, |
34 | flags = netlink.NLM_F_REQUEST | | |
35 | netlink.NLM_F_ACK, | |
36 | attrs = attrs) | |
37 | return msg.send_and_recv(self._conn).ret | |
38 | ||
39 | def destroy_radio(self, radio_id): | |
40 | attrs = [netlink.U32Attr(HWSIM_ATTR_RADIO_ID, radio_id)] | |
41 | msg = netlink.GenlMessage(self._fid, HWSIM_CMD_DESTROY_RADIO, | |
42 | flags = netlink.NLM_F_REQUEST | | |
43 | netlink.NLM_F_ACK, | |
44 | attrs = attrs) | |
45 | msg.send_and_recv(self._conn) | |
46 | ||
be32ace4 LC |
47 | def create(args): |
48 | print 'Created radio %d' % c.create_radio(n_channels=args.channels, | |
49 | use_chanctx=args.chanctx) | |
50 | ||
51 | def destroy(args): | |
52 | print c.destroy_radio(args.radio) | |
53 | ||
fe869124 | 54 | if __name__ == '__main__': |
be32ace4 | 55 | import argparse |
fe869124 | 56 | c = HWSimController() |
be32ace4 LC |
57 | |
58 | parser = argparse.ArgumentParser(description='send hwsim control commands') | |
59 | subparsers = parser.add_subparsers(help="Commands", dest='command') | |
60 | parser_create = subparsers.add_parser('create', help='create a radio') | |
61 | parser_create.add_argument('--channels', metavar='<number_of_channels>', type=int, | |
62 | default=0, | |
63 | help='Number of concurrent channels supported ' + | |
64 | 'by the radio. If not specified, the number ' + | |
65 | 'of channels specified in the ' + | |
66 | 'mac80211_hwsim.channels module parameter is ' + | |
67 | 'used') | |
68 | parser_create.add_argument('--chanctx', action="store_true", | |
69 | help='Use channel contexts, regardless of ' + | |
70 | 'whether the number of channels is 1 or ' + | |
71 | 'greater. By default channel contexts are ' + | |
72 | 'only used if the number of channels is ' + | |
73 | 'greater than 1.') | |
74 | parser_create.set_defaults(func=create) | |
75 | ||
76 | parser_destroy = subparsers.add_parser('destroy', help='destroy a radio') | |
77 | parser_destroy.add_argument('radio', metavar='<radio>', type=int, | |
78 | default=0, | |
79 | help='The number of the radio to be ' + | |
80 | 'destroyed (i.e., 0 for phy0, 1 for phy1...)') | |
81 | parser_destroy.set_defaults(func=destroy) | |
82 | ||
83 | args = parser.parse_args() | |
84 | args.func(args) |