diff options
author | Ole Troan <ot@cisco.com> | 2018-11-27 10:05:23 +0100 |
---|---|---|
committer | Ole Trøan <otroan@employees.org> | 2018-12-06 14:05:04 +0000 |
commit | 8c8acc027871f97370ee549306876690030c3bbb (patch) | |
tree | 36e3c8b05d8112105739a5e809ed7fa7d80ab785 /src/vpp-api | |
parent | 1f0dd7a0664bc217b7d69773574ac7eae3813bd7 (diff) |
API: Change ip4_address and ip6_address to use type alias.
Change-Id: Id8669bbadd1d6b2054865a310a654e9b38d1667d
Signed-off-by: Ole Troan <ot@cisco.com>
Diffstat (limited to 'src/vpp-api')
-rwxr-xr-x | src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py | 21 | ||||
-rw-r--r-- | src/vpp-api/python/vpp_papi/vpp_format.py | 16 | ||||
-rw-r--r-- | src/vpp-api/python/vpp_papi/vpp_serializer.py | 8 | ||||
-rwxr-xr-x | src/vpp-api/vapi/vapi_c_gen.py | 14 |
4 files changed, 39 insertions, 20 deletions
diff --git a/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py b/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py index 4b47e1eca7d..ba3190cadf9 100755 --- a/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py +++ b/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py @@ -3,6 +3,7 @@ import unittest from vpp_papi.vpp_serializer import VPPType, VPPEnumType from vpp_papi.vpp_serializer import VPPUnionType, VPPMessage +from vpp_papi.vpp_serializer import VPPTypeAlias from vpp_papi.vpp_format import VPPFormat from socket import inet_pton, AF_INET, AF_INET6 import logging @@ -94,8 +95,10 @@ class TestAddType(unittest.TestCase): af = VPPEnumType('vl_api_address_family_t', [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {"enumtype": "u32"}]) - ip4 = VPPType('vl_api_ip4_address_t', [['u8', 'address', 4]]) - ip6 = VPPType('vl_api_ip6_address_t', [['u8', 'address', 16]]) + ip4 = VPPTypeAlias('vl_api_ip4_address_t', {'type': 'u8', + 'length': 4}) + ip6 = VPPTypeAlias('vl_api_ip6_address_t', {'type': 'u8', + 'length': 16}) VPPUnionType('vl_api_address_union_t', [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]]) @@ -112,7 +115,6 @@ class TestAddType(unittest.TestCase): message_addr = VPPMessage('svs_address', [['vl_api_address_t', 'address']]) - b = message_addr.pack({'address': "1::1"}) self.assertEqual(len(b), 20) nt, size = message_addr.unpack(b) @@ -127,6 +129,19 @@ class TestAddType(unittest.TestCase): nt, size = message.unpack(b) self.assertEqual("1.1.1.1/24", VPPFormat.unformat(nt.prefix)) + message_array = VPPMessage('address_array', + [['vl_api_ip4_address_t', + 'addresses', 2]]) + b = message_array.pack({'addresses': ["1::1", "2::2"]}) + self.assertEqual(len(b), 8) + + message_array_vla = VPPMessage('address_array_vla', + [['u32', 'num'], + ['vl_api_ip4_address_t', + 'addresses', 0, 'num']]) + b = message_array_vla.pack({'addresses': ["1::1", "2::2"], 'num': 2}) + self.assertEqual(len(b), 12) + def test_zero_vla(self): '''Default zero'ed out for VLAs''' list = VPPType('vl_api_list_t', diff --git a/src/vpp-api/python/vpp_papi/vpp_format.py b/src/vpp-api/python/vpp_papi/vpp_format.py index c6f9477e03f..908606a92cc 100644 --- a/src/vpp-api/python/vpp_papi/vpp_format.py +++ b/src/vpp-api/python/vpp_papi/vpp_format.py @@ -32,7 +32,7 @@ class VPPFormat(object): @staticmethod def unformat_vl_api_ip6_prefix_t(args): - return "{}/{}".format(inet_ntop(AF_INET6, args.prefix.address), + return "{}/{}".format(inet_ntop(AF_INET6, args.prefix), args.len) @staticmethod @@ -43,7 +43,7 @@ class VPPFormat(object): @staticmethod def unformat_vl_api_ip4_prefix_t(args): - return "{}/{}".format(inet_ntop(AF_INET, args.prefix.address), + return "{}/{}".format(inet_ntop(AF_INET, args.prefix), args.len) @staticmethod @@ -57,18 +57,18 @@ class VPPFormat(object): @staticmethod def format_vl_api_address_t(args): try: - return {'un': {'ip6': {'address': inet_pton(AF_INET6, args)}}, + return {'un': {'ip6': inet_pton(AF_INET6, args)}, 'af': int(1)} except socket.error as e: - return {'un': {'ip4': {'address': inet_pton(AF_INET, args)}}, + return {'un': {'ip4': inet_pton(AF_INET, args)}, 'af': int(0)} @staticmethod def unformat_vl_api_address_t(arg): if arg.af == 1: - return inet_ntop(AF_INET6, arg.un.ip6.address) + return inet_ntop(AF_INET6, arg.un.ip6) if arg.af == 0: - return inet_ntop(AF_INET, arg.un.ip4.address) + return inet_ntop(AF_INET, arg.un.ip4) raise VPPFormatError @staticmethod @@ -81,11 +81,11 @@ class VPPFormat(object): def unformat_vl_api_prefix_t(arg): if arg.address.af == 1: return "{}/{}".format(inet_ntop(AF_INET6, - arg.address.un.ip6.address), + arg.address.un.ip6), arg.address_length) if arg.address.af == 0: return "{}/{}".format(inet_ntop(AF_INET, - arg.address.un.ip4.address), + arg.address.un.ip4), arg.address_length) raise VPPFormatError diff --git a/src/vpp-api/python/vpp_papi/vpp_serializer.py b/src/vpp-api/python/vpp_papi/vpp_serializer.py index f78972979cd..13721ff88d4 100644 --- a/src/vpp-api/python/vpp_papi/vpp_serializer.py +++ b/src/vpp-api/python/vpp_papi/vpp_serializer.py @@ -79,7 +79,7 @@ class FixedList_u8(object): self.packer = BaseTypes(field_type, num) self.size = self.packer.size - def pack(self, list, kwargs = None): + def pack(self, list, kwargs=None): """Packs a fixed length bytestring. Left-pads with zeros if input data is too short.""" if not list: @@ -290,7 +290,11 @@ def VPPTypeAlias(name, msgdef): if 'length' in msgdef: if msgdef['length'] == 0: raise ValueError() - types[name] = FixedList(name, msgdef['type'], msgdef['length']) + if msgdef['type'] == 'u8': + types[name] = FixedList_u8(name, msgdef['type'], + msgdef['length']) + else: + types[name] = FixedList(name, msgdef['type'], msgdef['length']) else: types[name] = t diff --git a/src/vpp-api/vapi/vapi_c_gen.py b/src/vpp-api/vapi/vapi_c_gen.py index 9939bc0556c..490b83028da 100755 --- a/src/vpp-api/vapi/vapi_c_gen.py +++ b/src/vpp-api/vapi/vapi_c_gen.py @@ -10,7 +10,7 @@ from vapi_json_parser import Field, Struct, Enum, Union, Message, JsonParser,\ class CField(Field): def get_c_name(self): - return self.name + return "vapi_type_%s" % self.name def get_c_def(self): if self.len is not None: @@ -100,14 +100,14 @@ class CField(Field): class CAlias(CField): def get_c_name(self): - return self.name + return "vapi_type_%s" % self.name def get_c_def(self): - return "typedef %s" % super(CAlias, self).get_c_def() - # if self.len is not None: - # return "typedef %s %s[%d];" % (self.type.get_c_name(), self.name, self.len) - # else: - # return "typedef %s %s;" % (self.type.get_c_name(), self.name) + if self.len is not None: + return "typedef %s vapi_type_%s[%d];" % (self.type.get_c_name(), self.name, self.len) + else: + return "typedef %s vapi_type_%s;" % (self.type.get_c_name(), self.name) + #return "typedef %s" % super(CAlias, self).get_c_def() # def needs_byte_swap |