aboutsummaryrefslogtreecommitdiffstats
path: root/src/vpp-api/python/vpp_papi
diff options
context:
space:
mode:
authorOle Troan <ot@cisco.com>2019-09-11 17:49:08 +0200
committerAndrew Yourtchenko <ayourtch@gmail.com>2019-09-19 14:24:54 +0000
commit75761b933f3e28494bae4b2cf5636a07ffe6ce18 (patch)
treea9663623eba8a5c26c6bcceab1606c946ce29754 /src/vpp-api/python/vpp_papi
parentfbc388986e3f38aaedff87b843137be81b7d4f91 (diff)
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 <ot@cisco.com> Change-Id: Icfec51d9b7d5cde1d69fbecdd97498688ab7b295 Signed-off-by: Ole Troan <ot@cisco.com> Signed-off-by: Klement Sekera <ksekera@cisco.com> Signed-off-by: Ole Troan <ot@cisco.com>
Diffstat (limited to 'src/vpp-api/python/vpp_papi')
-rwxr-xr-xsrc/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py94
-rw-r--r--src/vpp-api/python/vpp_papi/vpp_format.py72
-rw-r--r--src/vpp-api/python/vpp_papi/vpp_serializer.py20
3 files changed, 182 insertions, 4 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 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