hwsim.py 3.4 KB

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