hwsim.py 4.5 KB

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