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')
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 main(): print("heh") ovsAttrList = netlink.create_attr_list_type( 'ovsAttrList', )
class AttrListTestCase(unittest.TestCase): ''' Test AttrListType 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), ) def test_getter_no_default(self): a = self.AttrListTest() # Raises an exception if no default are given. with self.assertRaises(KeyError): a.get('FOO') # Returns the default otherwise. self.assertEqual(a.get('FOO', 5), 5) def test_packing(self): a = self.AttrListTest( u64type=2, binarytype=b'ABCD', nulstringtype='abcd', ) b = pack_unpack(self.AttrListTest, a) self.assertEqual(b.get('u64type'), 2) self.assertEqual(b.get('binarytype'), b'ABCD') self.assertEqual(b.get('nulstringtype'), 'abcd') def test_recursive_self(self): a = self.AttrListTest( recursiveself=self.AttrListTest( nulstringtype='abcd', ) ) # Confirms our AttrListType structures get properly filed. self.assertEqual(a.get('recursiveself').get('nulstringtype'), 'abcd') # Confirmd that we can properly pack and unpack the AttrListType. b = pack_unpack(self.AttrListTest, a) self.assertEqual(b.get('recursiveself').get('nulstringtype'), 'abcd')
# Virtual Service flags IPVS_SVC_F_ONEPACKET = 0x0004 # These are attr_list_types which are nestable. The command attribute list # is ultimately referenced by the messages which are passed down to the # kernel via netlink. These structures must match the type and ordering # that the kernel expects. IpvsStatsAttrList = netlink.create_attr_list_type( 'IpvsStatsAttrList', ('CONNS', netlink.U32Type), ('INPKTS', netlink.U32Type), ('OUTPKTS', netlink.U32Type), ('INBYTES', netlink.U64Type), ('OUTBYTES', netlink.U64Type), ('CPS', netlink.U32Type), ('INPPS', netlink.U32Type), ('OUTPPS', netlink.U32Type), ('INBPS', netlink.U32Type), ('OUTBPS', netlink.U32Type), ) IpvsStatsAttrList64 = netlink.create_attr_list_type( 'IpvsStatsAttrList64', ('CONNS', netlink.U64Type), ('INPKTS', netlink.U64Type), ('OUTPKTS', netlink.U64Type), ('INBYTES', netlink.U64Type), ('OUTBYTES', netlink.U64Type), ('CPS', netlink.U64Type),
arr = ['%s=%s' % (f, repr(self.__dict__[f])) for f in self.fields] return 'TaskStats(%s)' % ', '.join(arr) @staticmethod def unpack(val): fmt = 'HIBBQQQQQQQQ32sQxxxIIIIIQQQQQQQQQQQQQQQQQQQQQQQ' attrs = dict(zip(Taskstats.fields, struct.unpack(fmt, val))) assert attrs['version'] == 8, "Bad version: %d" % attrs["version"] attrs['comm'] = attrs['comm'].rstrip('\0') return Taskstats(**attrs) TaskstatsType = netlink.create_attr_list_type( 'TaskstatsType', ('PID', netlink.U32Type), ('TGID', netlink.U32Type), ('STATS', Taskstats), ('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',
IPVS_TUNNELING, IPVS_ROUTING ]) # These are attr_list_types which are nestable. The command attribute list # is ultimately referenced by the messages which are passed down to the # kernel via netlink. These structures must match the type and ordering # that the kernel expects. IpvsStatsAttrList = netlink.create_attr_list_type( 'IpvsStatsAttrList', ('CONNS', netlink.U32Type), ('INPKTS', netlink.U32Type), ('OUTPKTS', netlink.U32Type), ('INBYTES', netlink.U64Type), ('OUTBYTES', netlink.U64Type), ('CPS', netlink.U32Type), ('INPPS', netlink.U32Type), ('OUTPPS', netlink.U32Type), ('INBPS', netlink.U32Type), ('OUTBPS', netlink.U32Type), ) IpvsStatsAttrList64 = netlink.create_attr_list_type( 'IpvsStatsAttrList64', ('CONNS', netlink.U64Type), ('INPKTS', netlink.U64Type), ('OUTPKTS', netlink.U64Type), ('INBYTES', netlink.U64Type), ('OUTBYTES', netlink.U64Type), ('CPS', netlink.U64Type),
def __init__(self, **kwargs): self.__dict__.update(kwargs) def __repr__(self): arr = ['%s=%s' % (f, repr(self.__dict__[f])) for f in self.__fields__] return 'Cgroupstats(%s)' % ', '.join(arr) @staticmethod def unpack(val): fmt = 'QQQQQ' # as per __fields__ above, and they're uint64 each attrs = dict(zip(Cgroupstats.__fields__, struct.unpack(fmt, val))) return Cgroupstats(**attrs) CgroupstatsType = netlink.create_attr_list_type('CgroupstatsType', ('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',
return 'TaskStats(%s)' % ', '.join(arr) @staticmethod def unpack(val): fmt = 'HIBBQQQQQQQQ32sQxxxIIIIIQQQQQQQQQQQQQQQQQQQQQQQ' attrs = dict(zip(Taskstats.__fields__, struct.unpack(fmt, val))) assert attrs['version'] == 8, "Bad version: %d" % attrs["version"] attrs['comm'] = attrs['comm'].rstrip('\0') return Taskstats(**attrs) TaskstatsType = netlink.create_attr_list_type( 'TaskstatsType', ('PID', netlink.U32Type), ('TGID', netlink.U32Type), ('STATS', Taskstats), ('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',
import gnlpy.netlink as netlink import socket, struct from pprint import pprint MinutemanVIPAttrList = netlink.create_attr_list_type('MinutemanVIPAttrList', ('MINUTEMAN_ATTR_VIP', netlink.IgnoreType), ('minuteman_attr_vip_IP', netlink.Net32Type), ('minuteman_attr_vip_PORT', netlink.Net16Type), ('minuteman_attr_vip_BE', netlink.IgnoreType), ('MINUTEMAN_ATTR_BE', netlink.IgnoreType), ('MINUTEMAN_ATTR_BE_IP', netlink.Net32Type), ('MINUTEMAN_ATTR_BE_PORT', netlink.Net16Type), ('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), ) MinutemanBEAttrList = netlink.create_attr_list_type('MinutemanBEAttrList', ('MINUTEMAN_ATTR_VIP', netlink.IgnoreType), ('minuteman_attr_vip_IP', netlink.Net32Type), ('minuteman_attr_vip_PORT', netlink.Net16Type), ('minuteman_attr_vip_BE', netlink.IgnoreType), ('MINUTEMAN_ATTR_BE', netlink.IgnoreType), ('MINUTEMAN_ATTR_BE_IP', netlink.Net32Type), ('MINUTEMAN_ATTR_BE_PORT', netlink.Net16Type), ('MINUTEMAN_ATTR_BE_REACH', netlink.U8Type), ('MINUTEMAN_ATTR_BE_CONSECUTIVE_FAILURES', netlink.U64Type), ('MINUTEMAN_ATTR_BE_LAST_FAILURE', netlink.U64Type),
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 select import socket import struct 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 # #