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 20HWSIM_ATTR_MLO_SUPPORT = 25 21 22# the controller class 23class HWSimController(object): 24 def __init__(self): 25 self._conn = netlink.Connection(netlink.NETLINK_GENERIC) 26 self._fid = netlink.genl_controller.get_family_id(b'MAC80211_HWSIM') 27 28 def create_radio(self, n_channels=None, use_chanctx=False, 29 use_p2p_device=False, use_mlo=False): 30 attrs = [] 31 if n_channels: 32 attrs.append(netlink.U32Attr(HWSIM_ATTR_CHANNELS, n_channels)) 33 if use_chanctx: 34 attrs.append(netlink.FlagAttr(HWSIM_ATTR_USE_CHANCTX)) 35 if use_p2p_device: 36 attrs.append(netlink.FlagAttr(HWSIM_ATTR_SUPPORT_P2P_DEVICE)) 37 if use_mlo: 38 attrs.append(netlink.FlagAttr(HWSIM_ATTR_MLO_SUPPORT)) 39 40 msg = netlink.GenlMessage(self._fid, HWSIM_CMD_CREATE_RADIO, 41 flags=netlink.NLM_F_REQUEST | 42 netlink.NLM_F_ACK, 43 attrs=attrs) 44 return msg.send_and_recv(self._conn).ret 45 46 def destroy_radio(self, radio_id): 47 attrs = [netlink.U32Attr(HWSIM_ATTR_RADIO_ID, radio_id)] 48 msg = netlink.GenlMessage(self._fid, HWSIM_CMD_DESTROY_RADIO, 49 flags=netlink.NLM_F_REQUEST | 50 netlink.NLM_F_ACK, 51 attrs=attrs) 52 msg.send_and_recv(self._conn) 53 54class HWSimRadio(object): 55 def __init__(self, n_channels=None, use_chanctx=False, 56 use_p2p_device=False, use_mlo=False): 57 self._controller = HWSimController() 58 self._n_channels = n_channels 59 self._use_chanctx = use_chanctx 60 self._use_p2p_dev = use_p2p_device 61 self._use_mlo = use_mlo 62 63 def __enter__(self): 64 self._radio_id = self._controller.create_radio( 65 n_channels=self._n_channels, 66 use_chanctx=self._use_chanctx, 67 use_p2p_device=self._use_p2p_dev, 68 use_mlo=self._use_mlo) 69 if self._radio_id < 0: 70 raise Exception("Failed to create radio (err:%d)" % self._radio_id) 71 try: 72 iface = os.listdir('/sys/class/mac80211_hwsim/hwsim%d/net/' % self._radio_id)[0] 73 except Exception as e: 74 self._controller.destroy_radio(self._radio_id) 75 raise e 76 return self._radio_id, iface 77 78 def __exit__(self, type, value, traceback): 79 self._controller.destroy_radio(self._radio_id) 80 81 82def create(args): 83 print('Created radio %d' % c.create_radio(n_channels=args.channels, 84 use_chanctx=args.chanctx)) 85 86def destroy(args): 87 print(c.destroy_radio(args.radio)) 88 89if __name__ == '__main__': 90 import argparse 91 c = HWSimController() 92 93 parser = argparse.ArgumentParser(description='send hwsim control commands') 94 subparsers = parser.add_subparsers(help="Commands", dest='command') 95 parser_create = subparsers.add_parser('create', help='create a radio') 96 parser_create.add_argument('--channels', metavar='<number_of_channels>', type=int, 97 default=0, 98 help='Number of concurrent channels supported ' + 99 'by the radio. If not specified, the number ' + 100 'of channels specified in the ' + 101 'mac80211_hwsim.channels module parameter is ' + 102 'used') 103 parser_create.add_argument('--chanctx', action="store_true", 104 help='Use channel contexts, regardless of ' + 105 'whether the number of channels is 1 or ' + 106 'greater. By default channel contexts are ' + 107 'only used if the number of channels is ' + 108 'greater than 1.') 109 parser_create.set_defaults(func=create) 110 111 parser_destroy = subparsers.add_parser('destroy', help='destroy a radio') 112 parser_destroy.add_argument('radio', metavar='<radio>', type=int, 113 default=0, 114 help='The number of the radio to be ' + 115 'destroyed (i.e., 0 for phy0, 1 for phy1...)') 116 parser_destroy.set_defaults(func=destroy) 117 118 args = parser.parse_args() 119 args.func(args) 120