From 75761b933f3e28494bae4b2cf5636a07ffe6ce18 Mon Sep 17 00:00:00 2001 From: Ole Troan Date: Wed, 11 Sep 2019 17:49:08 +0200 Subject: api: split vl_api_prefix into two One type for address with prefix and one type for prefix. Type: fix Signed-off-by: Ole Troan Change-Id: Icfec51d9b7d5cde1d69fbecdd97498688ab7b295 Signed-off-by: Ole Troan Signed-off-by: Klement Sekera Signed-off-by: Ole Troan --- .../python/vpp_papi/tests/test_vpp_serializer.py | 94 ++++++++++++++++++++++ src/vpp-api/python/vpp_papi/vpp_format.py | 72 ++++++++++++++++- src/vpp-api/python/vpp_papi/vpp_serializer.py | 20 ++++- src/vpp-api/vapi/vapi_c_gen.py | 4 +- src/vpp-api/vapi/vapi_json_parser.py | 30 ++++--- 5 files changed, 201 insertions(+), 19 deletions(-) (limited to 'src/vpp-api') 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 6ca8e6c4ff3..9ac3e78224c 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 @@ -104,6 +104,10 @@ class TestAddType(unittest.TestCase): [['vl_api_address_family_t', 'af'], ['vl_api_address_union_t', 'un']]) + prefix = VPPType('vl_api_prefix_t', + [['vl_api_address_t', 'address'], + ['u8', 'len']]) + va_address_list = VPPType('list_addresses', [['u8', 'count'], ['vl_api_address_t', 'addresses', @@ -151,6 +155,96 @@ class TestAddType(unittest.TestCase): nt, size = message_with_va_address_list.unpack(b) self.assertEqual(nt.is_cool, 100) + def test_address_with_prefix(self): + af = VPPEnumType('vl_api_address_family_t', [["ADDRESS_IP4", 0], + ["ADDRESS_IP6", 1], + {"enumtype": "u32"}]) + 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"]]) + + address = VPPType('vl_api_address_t', + [['vl_api_address_family_t', 'af'], + ['vl_api_address_union_t', 'un']]) + + + prefix = VPPType('vl_api_prefix_t', + [['vl_api_address_t', 'address'], + ['u8', 'len']]) + prefix4 = VPPType('vl_api_ip4_prefix_t', + [['vl_api_ip4_address_t', 'address'], + ['u8', 'len']]) + prefix6 = VPPType('vl_api_ip6_prefix_t', + [['vl_api_ip6_address_t', 'address'], + ['u8', 'len']]) + + address_with_prefix = VPPTypeAlias('vl_api_address_with_prefix_t', {'type': 'vl_api_prefix_t' }) + address4_with_prefix = VPPTypeAlias('vl_api_ip4_address_with_prefix_t', + {'type': 'vl_api_ip4_prefix_t' }) + address6_with_prefix = VPPTypeAlias('vl_api_ip6_address_with_prefix_t', + {'type': 'vl_api_ip6_prefix_t' }) + + awp_type = VPPType('foobar_t', + [['vl_api_address_with_prefix_t', 'address']]) + + # address with prefix + b = address_with_prefix.pack(IPv4Interface('2.2.2.2/24')) + self.assertEqual(len(b), 21) + nt, size = address_with_prefix.unpack(b) + self.assertTrue(isinstance(nt, IPv4Interface)) + self.assertEqual(str(nt), '2.2.2.2/24') + + b = address_with_prefix.pack(IPv6Interface('2::2/64')) + self.assertEqual(len(b), 21) + nt, size = address_with_prefix.unpack(b) + self.assertTrue(isinstance(nt, IPv6Interface)) + self.assertEqual(str(nt), '2::2/64') + + b = address_with_prefix.pack(IPv4Network('2.2.2.2/24', strict=False)) + self.assertEqual(len(b), 21) + nt, size = address_with_prefix.unpack(b) + self.assertTrue(isinstance(nt, IPv4Interface)) + self.assertEqual(str(nt), '2.2.2.0/24') + + b = address4_with_prefix.pack('2.2.2.2/24') + self.assertEqual(len(b), 5) + nt, size = address4_with_prefix.unpack(b) + self.assertTrue(isinstance(nt, IPv4Interface)) + self.assertEqual(str(nt), '2.2.2.2/24') + b = address4_with_prefix.pack(IPv4Interface('2.2.2.2/24')) + self.assertEqual(len(b), 5) + + b = address6_with_prefix.pack('2::2/64') + self.assertEqual(len(b), 17) + nt, size = address6_with_prefix.unpack(b) + self.assertTrue(isinstance(nt, IPv6Interface)) + self.assertEqual(str(nt), '2::2/64') + b = address6_with_prefix.pack(IPv6Interface('2::2/64')) + self.assertEqual(len(b), 17) + + b = prefix.pack('192.168.10.0/24') + self.assertEqual(len(b), 21) + nt, size = prefix.unpack(b) + self.assertTrue(isinstance(nt, IPv4Network)) + self.assertEqual(str(nt), '192.168.10.0/24') + + b = awp_type.pack({'address': '1.2.3.4/24'}) + self.assertEqual(len(b), 21) + nt, size = awp_type.unpack(b) + self.assertTrue(isinstance(nt.address, IPv4Interface)) + self.assertEqual(str(nt.address), '1.2.3.4/24') + + b = awp_type.pack({'address': IPv4Interface('1.2.3.4/24')}) + self.assertEqual(len(b), 21) + nt, size = awp_type.unpack(b) + self.assertTrue(isinstance(nt.address, IPv4Interface)) + self.assertEqual(str(nt.address), '1.2.3.4/24') + + def test_recursive_address(self): af = VPPEnumType('vl_api_address_family_t', [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], diff --git a/src/vpp-api/python/vpp_papi/vpp_format.py b/src/vpp-api/python/vpp_papi/vpp_format.py index 7d2dcba491a..3e836c9da6b 100644 --- a/src/vpp-api/python/vpp_papi/vpp_format.py +++ b/src/vpp-api/python/vpp_papi/vpp_format.py @@ -52,6 +52,15 @@ def format_vl_api_prefix_t(args): return {'address': format_vl_api_address_t(p), 'len': int(length)} +def format_vl_api_address_with_prefix_t(args): + if isinstance(args, (ipaddress.IPv4Interface, ipaddress.IPv6Interface)): + return {'address': format_vl_api_address_t( + text_type(args.network_address)), + 'len': int(args.prefixlen)} + p, length = args.split('/') + return {'address': format_vl_api_address_t(p), + 'len': int(length)} + def format_vl_api_ip6_prefix_t(args): if isinstance(args, ipaddress.IPv6Network): @@ -61,6 +70,13 @@ def format_vl_api_ip6_prefix_t(args): return {'address': inet_pton(AF_INET6, p), 'len': int(length)} +def format_vl_api_ip6_address_with_prefix_t(args): + if isinstance(args, ipaddress.IPv6Interface): + return {'address': args.network_address.packed, + 'len': int(args.prefixlen)} + p, length = args.split('/') + return {'address': inet_pton(AF_INET6, p), + 'len': int(length)} def format_vl_api_ip4_prefix_t(args): if isinstance(args, ipaddress.IPv4Network): @@ -70,6 +86,14 @@ def format_vl_api_ip4_prefix_t(args): return {'address': inet_pton(AF_INET, p), 'len': int(length)} +def format_vl_api_ip4_address_with_prefix_t(args): + if isinstance(args, ipaddress.IPv4Interface): + return {'address': args.network_address.packed, + 'len': int(args.prefixlen)} + p, length = args.split('/') + return {'address': inet_pton(AF_INET, p), + 'len': int(length)} + conversion_table = { 'vl_api_ip6_address_t': @@ -112,6 +136,30 @@ conversion_table = { 'len': o.prefixlen}, 'str': lambda s: format_vl_api_prefix_t(s) }, + 'vl_api_address_with_prefix_t': + { + 'IPv4Interface': lambda o: {'address': + {'af': ADDRESS_IP4, 'un': + {'ip4': o.packed}}, + 'len': o.network.prefixlen}, + 'IPv6Interface': lambda o: {'address': + {'af': ADDRESS_IP6, 'un': + {'ip6': o.packed}}, + 'len': o.network.prefixlen}, + 'str': lambda s: format_vl_api_address_with_prefix_t(s) + }, + 'vl_api_ip4_address_with_prefix_t': + { + 'IPv4Interface': lambda o: {'address': o.packed, + 'len': o.network.prefixlen}, + 'str': lambda s: format_vl_api_ip4_address_with_prefix_t(s) + }, + 'vl_api_ip6_address_with_prefix_t': + { + 'IPv6Interface': lambda o: {'address': o.packed, + 'len': o.network.prefixlen}, + 'str': lambda s: format_vl_api_ip6_address_with_prefix_t(s) + }, 'vl_api_mac_address_t': { 'MACAddress': lambda o: o.packed, @@ -130,14 +178,33 @@ def unformat_api_address_t(o): return ipaddress.IPv6Address(o.un.ip6) if o.af == 0: return ipaddress.IPv4Address(o.un.ip4) - + raise ValueError('Unknown address family {}'.format(o)) def unformat_api_prefix_t(o): + if o.address.af == 1: + return ipaddress.IPv6Network((o.address.un.ip6, o.len), False) + if o.address.af == 0: + return ipaddress.IPv4Network((o.address.un.ip4, o.len), False) + raise ValueError('Unknown address family {}'.format(o)) + if isinstance(o.address, ipaddress.IPv4Address): return ipaddress.IPv4Network((o.address, o.len), False) if isinstance(o.address, ipaddress.IPv6Address): return ipaddress.IPv6Network((o.address, o.len), False) + raise ValueError('Unknown instance {}', format(o)) + +def unformat_api_address_with_prefix_t(o): + if o.address.af == 1: + return ipaddress.IPv6Interface((o.address.un.ip6, o.len)) + if o.address.af == 0: + return ipaddress.IPv4Interface((o.address.un.ip4, o.len)) + raise ValueError('Unknown address family {}'.format(o)) + +def unformat_api_ip4_address_with_prefix_t(o): + return ipaddress.IPv4Interface((o.address, o.len)) +def unformat_api_ip6_address_with_prefix_t(o): + return ipaddress.IPv6Interface((o.address, o.len)) conversion_unpacker_table = { 'vl_api_ip6_address_t': lambda o: ipaddress.IPv6Address(o), @@ -146,6 +213,9 @@ conversion_unpacker_table = { 'vl_api_ip4_prefix_t': lambda o: ipaddress.IPv4Network((o.address, o.len)), 'vl_api_address_t': lambda o: unformat_api_address_t(o), 'vl_api_prefix_t': lambda o: unformat_api_prefix_t(o), + 'vl_api_address_with_prefix_t': lambda o: unformat_api_address_with_prefix_t(o), + 'vl_api_ip4_address_with_prefix_t': lambda o: unformat_api_ip4_address_with_prefix_t(o), + 'vl_api_ip6_address_with_prefix_t': lambda o: unformat_api_ip6_address_with_prefix_t(o), 'vl_api_mac_address_t': lambda o: macaddress.MACAddress(o), 'vl_api_timestamp_t': lambda o: datetime.datetime.fromtimestamp(o), 'vl_api_timedelta_t': lambda o: datetime.timedelta(seconds=o), diff --git a/src/vpp-api/python/vpp_papi/vpp_serializer.py b/src/vpp-api/python/vpp_papi/vpp_serializer.py index 443dd83ba68..c01b2d3bd8b 100644 --- a/src/vpp-api/python/vpp_papi/vpp_serializer.py +++ b/src/vpp-api/python/vpp_papi/vpp_serializer.py @@ -414,7 +414,7 @@ class VPPTypeAlias(object): self.name = name t = vpp_get_type(msgdef['type']) if not t: - raise ValueError() + raise ValueError('No such type: {}'.format(msgdef['type'])) if 'length' in msgdef: if msgdef['length'] == 0: raise ValueError() @@ -429,6 +429,7 @@ class VPPTypeAlias(object): self.size = t.size types[name] = self + self.toplevelconversion = False def __call__(self, args): self.options = args @@ -445,8 +446,13 @@ class VPPTypeAlias(object): return self.packer.pack(data, kwargs) def unpack(self, data, offset=0, result=None, ntc=False): + if ntc == False and self.name in vpp_format.conversion_unpacker_table: + # Disable type conversion for dependent types + ntc = True + self.toplevelconversion = True t, size = self.packer.unpack(data, offset, result, ntc=ntc) - if not ntc: + if self.toplevelconversion: + self.toplevelconversion = False return conversion_unpacker(t, self.name), size return t, size @@ -513,6 +519,7 @@ class VPPType(object): self.size = size self.tuple = collections.namedtuple(name, self.fields, rename=True) types[name] = self + self.toplevelconversion = False def __call__(self, args): self.options = args @@ -551,6 +558,11 @@ class VPPType(object): # Return a list of arguments result = [] total = 0 + if ntc == False and self.name in vpp_format.conversion_unpacker_table: + # Disable type conversion for dependent types + ntc = True + self.toplevelconversion = True + for p in self.packers: x, size = p.unpack(data, offset, result, ntc) if type(x) is tuple and len(x) == 1: @@ -559,7 +571,9 @@ class VPPType(object): offset += size total += size t = self.tuple._make(result) - if not ntc: + + if self.toplevelconversion: + self.toplevelconversion = False t = conversion_unpacker(t, self.name) return t, total diff --git a/src/vpp-api/vapi/vapi_c_gen.py b/src/vpp-api/vapi/vapi_c_gen.py index b9b9aa750a8..9a285eb98ad 100755 --- a/src/vpp-api/vapi/vapi_c_gen.py +++ b/src/vpp-api/vapi/vapi_c_gen.py @@ -713,12 +713,12 @@ def gen_json_unified_header(parser, logger, j, io, name): emitted = [] for e in parser.enums_by_json[j]: emit_definition(parser, j, emitted, e) - for a in parser.aliases_by_json[j]: - emit_definition(parser, j, emitted, a) for u in parser.unions_by_json[j]: emit_definition(parser, j, emitted, u) for t in parser.types_by_json[j]: emit_definition(parser, j, emitted, t) + for a in parser.aliases_by_json[j]: + emit_definition(parser, j, emitted, a) for m in parser.messages_by_json[j].values(): emit_definition(parser, j, emitted, m) diff --git a/src/vpp-api/vapi/vapi_json_parser.py b/src/vpp-api/vapi/vapi_json_parser.py index d7669365ce8..98143e26756 100644 --- a/src/vpp-api/vapi/vapi_json_parser.py +++ b/src/vpp-api/vapi/vapi_json_parser.py @@ -377,19 +377,6 @@ class JsonParser(object): self.unions[union.name] = union self.logger.debug("Parsed union: %s" % union) self.unions_by_json[path].append(union) - for name, body in j['aliases'].iteritems(): - if name in self.aliases: - progress = progress + 1 - continue - if 'length' in body: - array_len = body['length'] - else: - array_len = None - t = self.types[body['type']] - alias = self.alias_class(name, t, array_len) - self.aliases[name] = alias - self.logger.debug("Parsed alias: %s" % alias) - self.aliases_by_json[path].append(alias) for t in j['types']: if t[0] in self.types: progress = progress + 1 @@ -408,6 +395,23 @@ class JsonParser(object): self.types[type_.name] = type_ self.types_by_json[path].append(type_) self.logger.debug("Parsed type: %s" % type_) + for name, body in j['aliases'].iteritems(): + if name in self.aliases: + progress = progress + 1 + continue + if 'length' in body: + array_len = body['length'] + else: + array_len = None + try: + t = self.lookup_type_like_id(body['type']) + except ParseError as e: + exceptions.append(e) + continue + alias = self.alias_class(name, t, array_len) + self.aliases[name] = alias + self.logger.debug("Parsed alias: %s" % alias) + self.aliases_by_json[path].append(alias) if not exceptions: # finished parsing break -- cgit 1.2.3-korg