def test_new_message_by_name(self): # It is fine to create a message by using a family name. # It will be looked up later. netlink.create_genl_message_type('Foo', 'BAR') # But we should not be able to register twice! with self.assertRaises(AssertionError): netlink.create_genl_message_type('Foo', 'BAR')
def test_assert_on_message_redefinition(self): # Assert when redefining a MessageType with self.assertRaises(AssertionError): netlink.create_genl_message_type( 'Foo', 12345, ) # Ok to create new MessageType ID. # class_name is only used in repr, it does not technically need to be # unique. netlink.create_genl_message_type('Foo', 12346)
def main(): c = NetlinkSocket() print(c.sock) print(c.port_id) print(c.seq) ListA = netlink.create_attr_list_type( 'ListA', ('SOME_SHORT', netlink.U16Type), ('SOME_STRING', netlink.NulStringType), ) ListB = netlink.create_attr_list_type( 'ListB', ('ANOTHER_STRING', netlink.NulStringType), ('ANOTHER_SHORT', netlink.U16Type), ('LIST_A', ListA), ) msg = netlink.create_genl_message_type( 'Msg', 'SPECIFIED_KERNEL_NAME', ('COMMAND_1', ListA), ('COMMAND_2', None), ('COMMAND_3', ListB), ) print(msg) c._send(msg) recv = c._recv() print(recv)
def test(): ListA = netlink.create_attr_list_type( 'ListA', ('SOME_SHORT', netlink.U16Type), ('SOME_STRING', netlink.NulStringType), ) ListB = netlink.create_attr_list_type( 'ListB', ('ANOTHER_STRING', netlink.NulStringType), ('ANOTHER_SHORT', netlink.U16Type), ('LIST_A', ListA), ) Msg = netlink.create_genl_message_type( 'Msg', 'SPECIFIED_KERNEL_NAME', ('COMMAND_1', ListA), ('COMMAND_2', None), ('COMMAND_3', ListB), ) print(Msg) sock = netlink.NetlinkSocket() #sock = NetlinkSocket() print(sock) sock.send( Msg('command_1', attr_list=ListA(another_string='foo', another_short=10))) reply = sock.recv()[0] reply.get_attr_list().get('some_short')
('TIMEOUT_TCP', netlink.U32Type), ('TIMEOUT_TCP_FIN', netlink.U32Type), ('TIMEOUT_UDP', netlink.U32Type), ) IpvsMessage = netlink.create_genl_message_type( 'IpvsMessage', 'IPVS', ('NEW_SERVICE', IpvsCmdAttrList), ('SET_SERVICE', IpvsCmdAttrList), ('DEL_SERVICE', IpvsCmdAttrList), ('GET_SERVICE', IpvsCmdAttrList), ('NEW_DEST', IpvsCmdAttrList), ('SET_DEST', IpvsCmdAttrList), ('DEL_DEST', IpvsCmdAttrList), ('GET_DEST', IpvsCmdAttrList), ('NEW_DAEMON', IpvsCmdAttrList), ('DEL_DAEMON', IpvsCmdAttrList), ('GET_DAEMON', IpvsCmdAttrList), ('SET_CONFIG', IpvsCmdAttrList), ('GET_CONFIG', IpvsCmdAttrList), ('SET_INFO', IpvsCmdAttrList), ('GET_INFO', IpvsCmdAttrList), ('ZERO', IpvsCmdAttrList), ('FLUSH', IpvsCmdAttrList), required_modules=['ip_vs'], ) def verbose(f): def g(self, *args, **kwargs): if self.verbose:
('AGGR_PID', netlink.RecursiveSelf), ('AGGR_TGID', netlink.RecursiveSelf), ('NULL', netlink.IgnoreType), ) TaskstatsAttrList = netlink.create_attr_list_type( 'TaskstatsAttrList', ('PID', netlink.U32Type), ('TGID', netlink.U32Type), ('REGISTER_CPUMASK', netlink.IgnoreType), ('DEREGISTER_CPUMASK', netlink.IgnoreType), ) TaskstatsMessage = netlink.create_genl_message_type( 'TaskstatsMessage', 'TASKSTATS', ('GET', TaskstatsAttrList), ('NEW', TaskstatsType), required_modules=[], ) class TaskstatsClient: """A python client to interact with taskstats """ def __init__(self, verbose=False): self.verbose = verbose self.nlsock = netlink.NetlinkSocket() def get_pid_stats(self, pid): replies = self.nlsock.query(TaskstatsMessage( 'GET', flags=netlink.MessageFlags.ACK_REQUEST, attr_list=TaskstatsAttrList(pid=pid)
('CGROUP_STATS', Cgroupstats)) CgroupstatsCmdAttrList = netlink.create_attr_list_type( 'CgroupstatsAttrList', ('CGROUPSTATS_CMD_ATTR_FD', netlink.U32Type), ) CgroupstatsMessage = netlink.create_genl_message_type( # the first field of the cgroupstats struct in the kernel is initialised to # __TASKSTATS_CMD_MAX, which in turn has the value of 3. The second field, # which is the GET command, will have a value of 4. In order for us to # correctly pass this to the kernel, we need to pad the message. As our # fields are initialised with values starting from 1, we need to insert # 3 padding fields in order to have the GET command correspond to 4. # see http://lxr.free-electrons.com/source/include/uapi/linux/cgroupstats.h 'CgroupstatsMessage', 'TASKSTATS', ('PADDING1', None), ('PADDING2', None), ('PADDING3', None), ('GET', CgroupstatsCmdAttrList), ('NEW', CgroupstatsType), required_modules=[], ) class CgroupstatsClient(object): """A python client to interact with cgroupstats """ def __init__(self, verbose=False): self.verbose = verbose
('AGGR_TGID', netlink.RecursiveSelf), ('NULL', netlink.IgnoreType), ) TaskstatsAttrList = netlink.create_attr_list_type( 'TaskstatsAttrList', ('PID', netlink.U32Type), ('TGID', netlink.U32Type), ('REGISTER_CPUMASK', netlink.IgnoreType), ('DEREGISTER_CPUMASK', netlink.IgnoreType), ) TaskstatsMessage = netlink.create_genl_message_type( 'TaskstatsMessage', 'TASKSTATS', ('GET', TaskstatsAttrList), ('NEW', TaskstatsType), required_modules=[], ) class TaskstatsClient(object): """A python client to interact with taskstats """ def __init__(self, verbose=False): self.verbose = verbose self.nlsock = netlink.NetlinkSocket() def get_pid_stats(self, pid): replies = self.nlsock.query( TaskstatsMessage('GET',
('MINUTEMAN_ATTR_BE_REACH', netlink.U8Type), ('MINUTEMAN_ATTR_BE_CONSECUTIVE_FAILURES', netlink.U64Type), ('MINUTEMAN_ATTR_BE_LAST_FAILURE', netlink.U64Type), ('MINUTEMAN_ATTR_BE_PENDING', netlink.U64Type), ('MINUTEMAN_ATTR_BE_TOTAL_FAILURES', netlink.U64Type), ('MINUTEMAN_ATTR_BE_TOTAL_SUCCESSES', netlink.U64Type), ('MINUTEMAN_ATTR_BE_NOW', netlink.U64Type), ) MinutemanMessage = netlink.create_genl_message_type( 'Minuteman', 'MINUTEMAN', ('MINUTEMAN_CMD_NOOP', MinutemanAttrList), ('MINUTEMAN_CMD_ADD_VIP', MinutemanAttrList), ('MINUTEMAN_CMD_DEL_VIP', MinutemanAttrList), ('MINUTEMAN_CMD_ADD_BE', MinutemanAttrList), ('MINUTEMAN_CMD_DEL_BE', MinutemanAttrList), ('MINUTEMAN_CMD_SET_BE', MinutemanAttrList), ('MINUTEMAN_CMD_ATTACH_BE', MinutemanAttrList), ('MINUTEMAN_CMD_DETACH_BE', MinutemanAttrList), ) be1_ip = struct.unpack('!I', socket.inet_aton("33.33.33.1"))[0] be1_port = 3333 be2_ip = struct.unpack('!I', socket.inet_aton("33.33.33.1"))[0] be2_port = 3334 be3_ip = struct.unpack('!I', socket.inet_aton("4.2.2.2"))[0] be3_port = 3333 vip1_ip = struct.unpack('!I', socket.inet_aton("1.2.3.4"))[0] vip1_port = 5000 vip2_ip = struct.unpack('!I', socket.inet_aton("2.2.3.4"))[0]
class MessageTypeTestCase(unittest.TestCase): ''' Test MessageType class. ''' AttrListTest = netlink.create_attr_list_type( 'AttrListTest', ('U8TYPE', netlink.U8Type), ('U16TYPE', netlink.U16Type), ('U32TYPE', netlink.U32Type), ('U64TYPE', netlink.U64Type), ('I32TYPE', netlink.I32Type), ('NET16TYPE', netlink.Net16Type), ('NET32TYPE', netlink.Net32Type), ('IGNORETYPE', netlink.IgnoreType), ('BINARYTYPE', netlink.BinaryType), ('NULSTRINGTYPE', netlink.NulStringType), ('RECURSIVESELF', netlink.RecursiveSelf), ) CtrlMessageTest = netlink.create_genl_message_type( 'CtrlMessageTest', 12345, ('CMD1', AttrListTest), ('CMD2', None), ) def test_msg_type_init(self): attr = self.AttrListTest() # Raises a KeyError when the command is not supported by the message. with self.assertRaises(KeyError): self.CtrlMessageTest('CMD', attr_list=attr) # A message can be initialized with a CMD string ctrl = self.CtrlMessageTest('CMD1', attr_list=attr) self.assertEqual(ctrl.cmd, 1) # Or a command ID. ctrl2 = self.CtrlMessageTest(1, attr_list=attr) self.assertEqual(ctrl.cmd, ctrl2.cmd) # When initialized without an attribute list, it will create a default # attr_list for us. ctrl = self.CtrlMessageTest('CMD1') self.assertIsInstance(ctrl.attr_list, self.AttrListTest) # Unless the operation is not supported ctrl = self.CtrlMessageTest('CMD2') self.assertIsNone(ctrl.attr_list) def test_get_attr_list(self): ctrl = self.CtrlMessageTest('CMD1') self.assertIsInstance(ctrl.get_attr_list(), self.AttrListTest) def test_assert_on_message_redefinition(self): # Assert when redefining a MessageType with self.assertRaises(AssertionError): netlink.create_genl_message_type( 'Foo', 12345, ) # Ok to create new MessageType ID. # class_name is only used in repr, it does not technically need to be # unique. netlink.create_genl_message_type('Foo', 12346) def test_new_message_by_name(self): # It is fine to create a message by using a family name. # It will be looked up later. netlink.create_genl_message_type('Foo', 'BAR') # But we should not be able to register twice! with self.assertRaises(AssertionError): netlink.create_genl_message_type('Foo', 'BAR') def test_packing(self): attr = self.AttrListTest( u64type=2, binarytype=b'ABCD', nulstringtype='abcd', ) ctrl = self.CtrlMessageTest('CMD1', attr_list=attr) self.assertEqual(ctrl.cmd, 1) ctrl2 = pack_unpack(self.CtrlMessageTest, ctrl) # Check that we have the same command. self.assertEqual(ctrl.cmd, ctrl2.cmd) # And that we have the same attributes. self.assertEqual( ctrl.attr_list.get('binarytype'), ctrl2.attr_list.get('binarytype') )
import threading from gnlpy import netlink import GBOperationMessage GB_NL_NAME = 'GREYBUS' GBNetlinkCmdAttrList = netlink.create_attr_list_type( 'GBNetlinkCmdAttrList', ('GB_NL_A_DATA', netlink.BinaryType), ('GB_NL_A_CPORT', netlink.U16Type) ) GBNetlinkMessage = netlink.create_genl_message_type( 'GBNetlinkMessage', GB_NL_NAME, ('GB_NL_C_MSG', GBNetlinkCmdAttrList), ('GB_NL_C_HD_RESET', GBNetlinkCmdAttrList), required_modules=[], ) ### # Alternative 1: we use a callback and expose a sync # write method to communicate between the SVC and the AP # # This makes the AP communication the "odd man out" in that # GBSVCHandler would not be able to inherit from GBHandler # # #self._netlink_message_callback( cport, msg ) ## # Alternative 2: we use a socketpair to communicate between