diff options
Diffstat (limited to 'src')
43 files changed, 4955 insertions, 3989 deletions
diff --git a/src/plugins/map/examples/gen-rules.py b/src/plugins/map/examples/gen-rules.py index 7964aa9a359..d22d4633ef6 100755 --- a/src/plugins/map/examples/gen-rules.py +++ b/src/plugins/map/examples/gen-rules.py @@ -20,38 +20,63 @@ import sys # map add domain ip4-pfx <pfx> ip6-pfx ::/0 ip6-src <ip6-src> ea-bits-len 0 psid-offset 6 psid-len 6 # map add rule index <0> psid <psid> ip6-dst <ip6-dst> -def_ip4_pfx = '192.0.2.0/24' -def_ip6_pfx = '2001:db8::/32' -def_ip6_src = '2001:db8::1' +def_ip4_pfx = "192.0.2.0/24" +def_ip6_pfx = "2001:db8::/32" +def_ip6_src = "2001:db8::1" def_psid_offset = 6 def_psid_len = 6 def_ea_bits_len = 0 -parser = argparse.ArgumentParser(description='MAP VPP configuration generator') -parser.add_argument('-t', action="store", dest="mapmode") -parser.add_argument('-f', action="store", dest="format", default="vpp") -parser.add_argument('--ip4-prefix', action="store", dest="ip4_pfx", default=def_ip4_pfx) -parser.add_argument('--ip6-prefix', action="store", dest="ip6_pfx", default=def_ip6_pfx) -parser.add_argument('--ip6-src', action="store", dest="ip6_src", default=def_ip6_src) -parser.add_argument('--psid-len', action="store", dest="psid_len", default=def_psid_len) -parser.add_argument('--psid-offset', action="store", dest="psid_offset", default=def_psid_offset) -parser.add_argument('--ea-bits-len', action="store", dest="ea_bits_len", default=def_ea_bits_len) +parser = argparse.ArgumentParser(description="MAP VPP configuration generator") +parser.add_argument("-t", action="store", dest="mapmode") +parser.add_argument("-f", action="store", dest="format", default="vpp") +parser.add_argument("--ip4-prefix", action="store", dest="ip4_pfx", default=def_ip4_pfx) +parser.add_argument("--ip6-prefix", action="store", dest="ip6_pfx", default=def_ip6_pfx) +parser.add_argument("--ip6-src", action="store", dest="ip6_src", default=def_ip6_src) +parser.add_argument("--psid-len", action="store", dest="psid_len", default=def_psid_len) +parser.add_argument( + "--psid-offset", action="store", dest="psid_offset", default=def_psid_offset +) +parser.add_argument( + "--ea-bits-len", action="store", dest="ea_bits_len", default=def_ea_bits_len +) args = parser.parse_args() # # Print domain # def domain_print(i, ip4_pfx, ip6_pfx, ip6_src, eabits_len, psid_offset, psid_len): - if format == 'vpp': - print("map add domain ip4-pfx " + ip4_pfx + " ip6-pfx", ip6_pfx, "ip6-src " + ip6_src + - " ea-bits-len", eabits_len, "psid-offset", psid_offset, "psid-len", psid_len) - if format == 'confd': - print("vpp softwire softwire-instances softwire-instance", i, "br-ipv6 " + ip6_src + - " ipv6-prefix " + ip6_pfx + " ipv4-prefix " + ip4_pfx + - " ea-bits-len", eabits_len, "psid-offset", psid_offset, "psid-len", psid_len) - if format == 'xml': + if format == "vpp": + print( + "map add domain ip4-pfx " + ip4_pfx + " ip6-pfx", + ip6_pfx, + "ip6-src " + ip6_src + " ea-bits-len", + eabits_len, + "psid-offset", + psid_offset, + "psid-len", + psid_len, + ) + if format == "confd": + print( + "vpp softwire softwire-instances softwire-instance", + i, + "br-ipv6 " + + ip6_src + + " ipv6-prefix " + + ip6_pfx + + " ipv4-prefix " + + ip4_pfx + + " ea-bits-len", + eabits_len, + "psid-offset", + psid_offset, + "psid-len", + psid_len, + ) + if format == "xml": print("<softwire-instance>") - print("<id>", i, "</id>"); + print("<id>", i, "</id>") print(" <br-ipv6>" + ip6_src + "</br-ipv6>") print(" <ipv6-prefix>" + ip6_pfx + "</ipv6-prefix>") print(" <ipv4-prefix>" + ip4_pfx + "</ipv4-prefix>") @@ -59,32 +84,54 @@ def domain_print(i, ip4_pfx, ip6_pfx, ip6_src, eabits_len, psid_offset, psid_len print(" <psid-len>", psid_len, "</psid-len>") print(" <psid-offset>", psid_offset, "</psid-offset>") + def domain_print_end(): - if format == 'xml': + if format == "xml": print("</softwire-instance>") + def rule_print(i, psid, dst): - if format == 'vpp': + if format == "vpp": print("map add rule index", i, "psid", psid, "ip6-dst", dst) - if format == 'confd': + if format == "confd": print("binding", psid, "ipv6-addr", dst) - if format == 'xml': + if format == "xml": print(" <binding>") print(" <psid>", psid, "</psid>") print(" <ipv6-addr>", dst, "</ipv6-addr>") print(" </binding>") + # # Algorithmic mapping Shared IPv4 address # -def algo(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): - domain_print(0, ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len) +def algo( + ip4_pfx_str, + ip6_pfx_str, + ip6_src_str, + ea_bits_len, + psid_offset, + psid_len, + ip6_src_ecmp=False, +): + domain_print( + 0, ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len + ) domain_print_end() + # # 1:1 Full IPv4 address # -def lw46(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): +def lw46( + ip4_pfx_str, + ip6_pfx_str, + ip6_src_str, + ea_bits_len, + psid_offset, + psid_len, + ip6_src_ecmp=False, +): ip4_pfx = ipaddress.ip_network(ip4_pfx_str) ip6_src = ipaddress.ip_address(ip6_src_str) ip6_dst = ipaddress.ip_network(ip6_pfx_str) @@ -92,15 +139,26 @@ def lw46(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_l mod = ip4_pfx.num_addresses / 1024 for i in range(ip4_pfx.num_addresses): - domain_print(i, str(ip4_pfx[i]) + "/32", str(ip6_dst[i]) + "/128", str(ip6_src), 0, 0, 0) + domain_print( + i, str(ip4_pfx[i]) + "/32", str(ip6_dst[i]) + "/128", str(ip6_src), 0, 0, 0 + ) domain_print_end() if ip6_src_ecmp and not i % mod: ip6_src = ip6_src + 1 + # # 1:1 Shared IPv4 address, shared BR (16) VPP CLI # -def lw46_shared(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): +def lw46_shared( + ip4_pfx_str, + ip6_pfx_str, + ip6_src_str, + ea_bits_len, + psid_offset, + psid_len, + ip6_src_ecmp=False, +): ip4_pfx = ipaddress.ip_network(ip4_pfx_str) ip6_src = ipaddress.ip_address(ip6_src_str) ip6_dst = ipaddress.ip_network(ip6_pfx_str) @@ -109,7 +167,7 @@ def lw46_shared(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, for i in range(ip4_pfx.num_addresses): domain_print(i, str(ip4_pfx[i]) + "/32", "::/0", str(ip6_src), 0, 0, psid_len) for psid in range(0x1 << int(psid_len)): - rule_print(i, psid, str(ip6_dst[(i * (0x1<<int(psid_len))) + psid])) + rule_print(i, psid, str(ip6_dst[(i * (0x1 << int(psid_len))) + psid])) domain_print_end() if ip6_src_ecmp and not i % mod: ip6_src = ip6_src + 1 @@ -118,7 +176,15 @@ def lw46_shared(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, # # 1:1 Shared IPv4 address, shared BR # -def lw46_shared_b(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): +def lw46_shared_b( + ip4_pfx_str, + ip6_pfx_str, + ip6_src_str, + ea_bits_len, + psid_offset, + psid_len, + ip6_src_ecmp=False, +): ip4_pfx = ipaddress.ip_network(ip4_pfx_str) ip6_src = ipaddress.ip_address(ip6_src_str) ip6_dst = list(ipaddress.ip_network(ip6_pfx_str).subnets(new_prefix=56)) @@ -127,15 +193,16 @@ def lw46_shared_b(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offse for i in range(ip4_pfx.num_addresses): domain_print(i, str(ip4_pfx[i]) + "/32", "::/0", str(ip6_src), 0, 0, psid_len) for psid in range(0x1 << psid_len): - enduserprefix = list(ip6_dst.pop(0).subnets(new_prefix=64))[255-1] - rule_print(i, psid, enduserprefix[(i * (0x1<<psid_len)) + psid]) + enduserprefix = list(ip6_dst.pop(0).subnets(new_prefix=64))[255 - 1] + rule_print(i, psid, enduserprefix[(i * (0x1 << psid_len)) + psid]) domain_print_end() if ip6_src_ecmp and not i % mod: ip6_src = ip6_src + 1 def xml_header_print(): - print(''' + print( + """ <?xml version="1.0" encoding="UTF-8"?> <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <capabilities> @@ -156,10 +223,13 @@ def xml_header_print(): <softwire> <softwire-instances> - ''') + """ + ) + def xml_footer_print(): - print(''' + print( + """ </softwire-instances> </softwire> </vpp> @@ -175,12 +245,20 @@ def xml_footer_print(): </rpc> ]]>]]> - ''') + """ + ) format = args.format -if format == 'xml': +if format == "xml": xml_header_print() -globals()[args.mapmode](args.ip4_pfx, args.ip6_pfx, args.ip6_src, args.ea_bits_len, args.psid_offset, args.psid_len) -if format == 'xml': +globals()[args.mapmode]( + args.ip4_pfx, + args.ip6_pfx, + args.ip6_src, + args.ea_bits_len, + args.psid_offset, + args.psid_len, +) +if format == "xml": xml_footer_print() diff --git a/src/plugins/map/examples/test_map.py b/src/plugins/map/examples/test_map.py index 7a48964b3f2..02df64015a2 100755 --- a/src/plugins/map/examples/test_map.py +++ b/src/plugins/map/examples/test_map.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 -import time,argparse,sys,cmd, unittest +import time, argparse, sys, cmd, unittest from ipaddress import * -parser = argparse.ArgumentParser(description='VPP MAP test') -parser.add_argument('-i', nargs='*', action="store", dest="inputdir") +parser = argparse.ArgumentParser(description="VPP MAP test") +parser.add_argument("-i", nargs="*", action="store", dest="inputdir") args = parser.parse_args() for dir in args.inputdir: @@ -14,115 +14,150 @@ from vpp_papi import * # # 1:1 Shared IPv4 address, shared BR (16) VPP CLI # -def lw46_shared(ip4_pfx_str, ip6_pfx_str, ip6_src_str, ea_bits_len, psid_offset, psid_len, ip6_src_ecmp = False): +def lw46_shared( + ip4_pfx_str, + ip6_pfx_str, + ip6_src_str, + ea_bits_len, + psid_offset, + psid_len, + ip6_src_ecmp=False, +): ip4_pfx = ip_network(ip4_pfx_str) ip6_src = ip_address(ip6_src_str) ip6_dst = ip_network(ip6_pfx_str) - ip6_nul = IPv6Address(u'0::0') + ip6_nul = IPv6Address("0::0") mod = ip4_pfx.num_addresses / 1024 for i in range(ip4_pfx.num_addresses): a = time.clock() - t = map_add_domain(0, ip6_nul.packed, ip4_pfx[i].packed, ip6_src.packed, 0, 32, 128, ea_bits_len, psid_offset, psid_len, 0, 0) - #print "Return from map_add_domain", t + t = map_add_domain( + 0, + ip6_nul.packed, + ip4_pfx[i].packed, + ip6_src.packed, + 0, + 32, + 128, + ea_bits_len, + psid_offset, + psid_len, + 0, + 0, + ) + # print "Return from map_add_domain", t if t == None: - print "map_add_domain failed" + print("map_add_domain failed") continue if t.retval != 0: - print "map_add_domain failed", t + print(f"map_add_domain failed, {t}") continue for psid in range(0x1 << int(psid_len)): - r = map_add_del_rule(0, t.index, 1, (ip6_dst[(i * (0x1<<int(psid_len))) + psid]).packed, psid) - #print "Return from map_add_del_rule", r + r = map_add_del_rule( + 0, + t.index, + 1, + (ip6_dst[(i * (0x1 << int(psid_len))) + psid]).packed, + psid, + ) + # print "Return from map_add_del_rule", r if ip6_src_ecmp and not i % mod: ip6_src = ip6_src + 1 - print "Running time:", time.clock() - a + print(f"Running time: {time.clock() - a}") + class TestMAP(unittest.TestCase): - ''' + """ def test_delete_all(self): t = map_domain_dump(0) self.assertNotEqual(t, None) - print "Number of domains configured: ", len(t) + print(f"Number of domains configured: {len(t)}") for d in t: ts = map_del_domain(0, d.domainindex) self.assertNotEqual(ts, None) t = map_domain_dump(0) self.assertNotEqual(t, None) - print "Number of domains configured: ", len(t) - self.assertEqual(len(t), 0) + print(f"Number of domains configured: {len(t)}") + self.assertEqual(len(t), 0)/ - ''' + """ def test_a_million_rules(self): - ip4_pfx = u'192.0.2.0/24' - ip6_pfx = u'2001:db8::/32' - ip6_src = u'2001:db8::1' + ip4_pfx = "192.0.2.0/24" + ip6_pfx = "2001:db8::/32" + ip6_src = "2001:db8::1" psid_offset = 6 psid_len = 6 ea_bits_len = 0 lw46_shared(ip4_pfx, ip6_pfx, ip6_src, ea_bits_len, psid_offset, psid_len) + # # RX thread, that should sit on blocking vpe_api_read() -# +# # # # import threading -class RXThread (threading.Thread): + + +class RXThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): - print "Starting " + print("Starting ") i = 0 while True: msg = vpe_api_read() if msg: - #print msg - id = unpack('>H', msg[0:2]) - size = unpack('>H', msg[2:4]) - print "Received", id, "of size", size + # print msg + id = unpack(">H", msg[0:2]) + size = unpack(">H", msg[2:4]) + print(f"Received {id} of size {size}") i += 1 - #del msg + # del msg continue - #time.sleep(0.001) + # time.sleep(0.001) return + # Create RX thread rxthread = RXThread() rxthread.setDaemon(True) - -print "Connect", connect_to_vpe("client124") + +print(f"Connect {connect_to_vpe('client124')}") import timeit + rxthread.start() -print "After thread started" +print("After thread started") -#pneum_kill_thread() -print "After thread killed" +# pneum_kill_thread() +print("After thread killed") -#t = show_version(0) -#print "Result from show version", t +# t = show_version(0) +# print "Result from show version", t -print timeit.timeit('t = show_version(0)', number=1000, setup="from __main__ import show_version") +print( + f"{timeit.timeit('t = show_version(0)', number=1000, setup='from __main__ import show_version')}" +) time.sleep(10) -#print timeit.timeit('control_ping(0)', number=10, setup="from __main__ import control_ping") +# print timeit.timeit('control_ping(0)', number=10, setup="from __main__ import control_ping") disconnect_from_vpe() sys.exit() -print t.program, t.version,t.builddate,t.builddirectory +print(f"{t.program} {t.version}{t.builddate}{t.builddirectory}") -''' +""" t = map_domain_dump(0) if not t: @@ -131,11 +166,9 @@ if not t: for d in t: print("IP6 prefix:",str(IPv6Address(d.ip6prefix))) print( "IP4 prefix:",str(IPv4Address(d.ip4prefix))) -''' +""" suite = unittest.TestLoader().loadTestsFromTestCase(TestMAP) unittest.TextTestRunner(verbosity=2).run(suite) disconnect_from_vpe() - - diff --git a/src/plugins/map/gen-rules.py b/src/plugins/map/gen-rules.py index e43b8e155be..7e74f14ad6f 100755 --- a/src/plugins/map/gen-rules.py +++ b/src/plugins/map/gen-rules.py @@ -21,87 +21,142 @@ import sys # map add domain ip4-pfx <pfx> ip6-pfx ::/0 ip6-src <ip6-src> ea-bits-len 0 psid-offset 6 psid-len 6 # map add rule index <0> psid <psid> ip6-dst <ip6-dst> -parser = argparse.ArgumentParser(description='MAP VPP configuration generator') -parser.add_argument('-t', action="store", dest="mapmode") +parser = argparse.ArgumentParser(description="MAP VPP configuration generator") +parser.add_argument("-t", action="store", dest="mapmode") args = parser.parse_args() # # 1:1 Shared IPv4 address, shared BR # def shared11br(): - ip4_pfx = ipaddress.ip_network('20.0.0.0/16') - ip6_dst = ipaddress.ip_network('bbbb::/32') + ip4_pfx = ipaddress.ip_network("20.0.0.0/16") + ip6_dst = ipaddress.ip_network("bbbb::/32") psid_len = 6 for i in range(ip4_pfx.num_addresses): - print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-shared-src cccc:bbbb::1", - "ea-bits-len 0 psid-offset 6 psid-len", psid_len) + print( + "map add domain ip4-pfx " + + str(ip4_pfx[i]) + + "/32 ip6-pfx ::/0 ip6-shared-src cccc:bbbb::1", + "ea-bits-len 0 psid-offset 6 psid-len", + psid_len, + ) for psid in range(0x1 << psid_len): - print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) + print( + "map add rule index", + i, + "psid", + psid, + "ip6-dst", + ip6_dst[(i * (0x1 << psid_len)) + psid], + ) # # 1:1 Shared IPv4 address # def shared11(): - ip4_pfx = ipaddress.ip_network('20.0.0.0/16') - ip6_src = ipaddress.ip_network('cccc:bbbb::/64') - ip6_dst = ipaddress.ip_network('bbbb::/32') + ip4_pfx = ipaddress.ip_network("20.0.0.0/16") + ip6_src = ipaddress.ip_network("cccc:bbbb::/64") + ip6_dst = ipaddress.ip_network("bbbb::/32") psid_len = 6 for i in range(ip4_pfx.num_addresses): - print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src", ip6_src[i], - "ea-bits-len 0 psid-offset 6 psid-len", psid_len) + print( + "map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src", + ip6_src[i], + "ea-bits-len 0 psid-offset 6 psid-len", + psid_len, + ) for psid in range(0x1 << psid_len): - print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) + print( + "map add rule index", + i, + "psid", + psid, + "ip6-dst", + ip6_dst[(i * (0x1 << psid_len)) + psid], + ) + # # 1:1 Shared IPv4 address small # def smallshared11(): - ip4_pfx = ipaddress.ip_network('20.0.0.0/24') - ip6_src = ipaddress.ip_network('cccc:bbbb::/64') - ip6_dst = ipaddress.ip_network('bbbb::/32') + ip4_pfx = ipaddress.ip_network("20.0.0.0/24") + ip6_src = ipaddress.ip_network("cccc:bbbb::/64") + ip6_dst = ipaddress.ip_network("bbbb::/32") psid_len = 6 for i in range(ip4_pfx.num_addresses): - print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src", ip6_src[i], - "ea-bits-len 0 psid-offset 6 psid-len", psid_len) + print( + "map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx ::/0 ip6-src", + ip6_src[i], + "ea-bits-len 0 psid-offset 6 psid-len", + psid_len, + ) for psid in range(0x1 << psid_len): - print("map add rule index", i, "psid", psid, "ip6-dst", ip6_dst[(i * (0x1<<psid_len)) + psid]) + print( + "map add rule index", + i, + "psid", + psid, + "ip6-dst", + ip6_dst[(i * (0x1 << psid_len)) + psid], + ) + # # 1:1 Full IPv4 address # def full11(): - ip4_pfx = ipaddress.ip_network('20.0.0.0/16') - ip6_src = ipaddress.ip_network('cccc:bbbb::/64') - ip6_dst = ipaddress.ip_network('bbbb::/32') + ip4_pfx = ipaddress.ip_network("20.0.0.0/16") + ip6_src = ipaddress.ip_network("cccc:bbbb::/64") + ip6_dst = ipaddress.ip_network("bbbb::/32") psid_len = 0 for i in range(ip4_pfx.num_addresses): - print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx " + str(ip6_dst[i]) + "/128 ip6-src", ip6_src[i], - "ea-bits-len 0 psid-offset 0 psid-len 0") + print( + "map add domain ip4-pfx " + + str(ip4_pfx[i]) + + "/32 ip6-pfx " + + str(ip6_dst[i]) + + "/128 ip6-src", + ip6_src[i], + "ea-bits-len 0 psid-offset 0 psid-len 0", + ) + + def full11br(): - ip4_pfx = ipaddress.ip_network('20.0.0.0/16') - ip6_dst = ipaddress.ip_network('bbbb::/32') + ip4_pfx = ipaddress.ip_network("20.0.0.0/16") + ip6_dst = ipaddress.ip_network("bbbb::/32") psid_len = 0 for i in range(ip4_pfx.num_addresses): - print("map add domain ip4-pfx " + str(ip4_pfx[i]) + "/32 ip6-pfx " + str(ip6_dst[i]) + "/128 ip6-shared-src cccc:bbbb::1", - "ea-bits-len 0 psid-offset 0 psid-len 0") + print( + "map add domain ip4-pfx " + + str(ip4_pfx[i]) + + "/32 ip6-pfx " + + str(ip6_dst[i]) + + "/128 ip6-shared-src cccc:bbbb::1", + "ea-bits-len 0 psid-offset 0 psid-len 0", + ) + # # Algorithmic mapping Shared IPv4 address # def algo(): - print("map add domain ip4-pfx 20.0.0.0/24 ip6-pfx bbbb::/32 ip6-src cccc:bbbb::1 ea-bits-len 16 psid-offset 6 psid-len 8") - print("map add domain ip4-pfx 20.0.1.0/24 ip6-pfx bbbb:1::/32 ip6-src cccc:bbbb::2 ea-bits-len 8 psid-offset 0 psid-len 0") + print( + "map add domain ip4-pfx 20.0.0.0/24 ip6-pfx bbbb::/32 ip6-src cccc:bbbb::1 ea-bits-len 16 psid-offset 6 psid-len 8" + ) + print( + "map add domain ip4-pfx 20.0.1.0/24 ip6-pfx bbbb:1::/32 ip6-src cccc:bbbb::2 ea-bits-len 8 psid-offset 0 psid-len 0" + ) + # # IP4 forwarding # def ip4(): - ip4_pfx = ipaddress.ip_network('20.0.0.0/16') + ip4_pfx = ipaddress.ip_network("20.0.0.0/16") for i in range(ip4_pfx.num_addresses): - print("ip route add " + str(ip4_pfx[i]) + "/32 via 172.16.0.2") + print("ip route add " + str(ip4_pfx[i]) + "/32 via 172.16.0.2") globals()[args.mapmode]() - - diff --git a/src/plugins/nat/extras/nat_100ks.py b/src/plugins/nat/extras/nat_100ks.py index c85a4591cd3..4e8dc2486d6 100644 --- a/src/plugins/nat/extras/nat_100ks.py +++ b/src/plugins/nat/extras/nat_100ks.py @@ -1,35 +1,39 @@ from trex_stl_lib.api import * -class STLS1: - def create_stream (self): - base_pkt = Ether()/IP(dst="2.2.0.1")/UDP(dport=12) +class STLS1: + def create_stream(self): + base_pkt = Ether() / IP(dst="2.2.0.1") / UDP(dport=12) pad = Padding() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) - pad.load = '\x00' * pad_len + pad.load = "\x00" * pad_len vm = STLVM() - vm.tuple_var(name="tuple", ip_min="10.0.0.3", ip_max="10.0.3.234", port_min=1025, port_max=1124, limit_flows = 100000) + vm.tuple_var( + name="tuple", + ip_min="10.0.0.3", + ip_max="10.0.3.234", + port_min=1025, + port_max=1124, + limit_flows=100000, + ) vm.write(fv_name="tuple.ip", pkt_offset="IP.src") vm.fix_chksum() vm.write(fv_name="tuple.port", pkt_offset="UDP.sport") - pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm) + pkt = STLPktBuilder(pkt=base_pkt / pad, vm=vm) return STLStream(packet=pkt, mode=STLTXCont()) - def get_streams (self, direction = 0, **kwargs): + def get_streams(self, direction=0, **kwargs): return [self.create_stream()] # dynamic load - used for trex console or simulator def register(): return STLS1() - - - diff --git a/src/plugins/nat/extras/nat_10Ms.py b/src/plugins/nat/extras/nat_10Ms.py index 6ce62a0b5e7..96a18ec018a 100644 --- a/src/plugins/nat/extras/nat_10Ms.py +++ b/src/plugins/nat/extras/nat_10Ms.py @@ -1,35 +1,39 @@ from trex_stl_lib.api import * -class STLS1: - def create_stream (self): - base_pkt = Ether()/IP(dst="2.2.0.1")/UDP(dport=12) +class STLS1: + def create_stream(self): + base_pkt = Ether() / IP(dst="2.2.0.1") / UDP(dport=12) pad = Padding() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) - pad.load = '\x00' * pad_len + pad.load = "\x00" * pad_len vm = STLVM() - vm.tuple_var(name="tuple", ip_min="10.0.0.3", ip_max="10.1.134.162", port_min=1025, port_max=1124, limit_flows=10000000) + vm.tuple_var( + name="tuple", + ip_min="10.0.0.3", + ip_max="10.1.134.162", + port_min=1025, + port_max=1124, + limit_flows=10000000, + ) vm.write(fv_name="tuple.ip", pkt_offset="IP.src") vm.fix_chksum() vm.write(fv_name="tuple.port", pkt_offset="UDP.sport") - pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm) + pkt = STLPktBuilder(pkt=base_pkt / pad, vm=vm) return STLStream(packet=pkt, mode=STLTXCont()) - def get_streams (self, direction = 0, **kwargs): + def get_streams(self, direction=0, **kwargs): return [self.create_stream()] # dynamic load - used for trex console or simulator def register(): return STLS1() - - - diff --git a/src/plugins/nat/extras/nat_10ks.py b/src/plugins/nat/extras/nat_10ks.py index 33c7196eb9e..c210d5e81d8 100644 --- a/src/plugins/nat/extras/nat_10ks.py +++ b/src/plugins/nat/extras/nat_10ks.py @@ -1,35 +1,39 @@ from trex_stl_lib.api import * -class STLS1: - def create_stream (self): - base_pkt = Ether()/IP(dst="2.2.0.1")/UDP(dport=12) +class STLS1: + def create_stream(self): + base_pkt = Ether() / IP(dst="2.2.0.1") / UDP(dport=12) pad = Padding() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) - pad.load = '\x00' * pad_len + pad.load = "\x00" * pad_len vm = STLVM() - vm.tuple_var(name="tuple", ip_min="10.0.0.3", ip_max="10.0.0.102", port_min=1025, port_max=1124, limit_flows = 10000) + vm.tuple_var( + name="tuple", + ip_min="10.0.0.3", + ip_max="10.0.0.102", + port_min=1025, + port_max=1124, + limit_flows=10000, + ) vm.write(fv_name="tuple.ip", pkt_offset="IP.src") vm.fix_chksum() vm.write(fv_name="tuple.port", pkt_offset="UDP.sport") - pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm) + pkt = STLPktBuilder(pkt=base_pkt / pad, vm=vm) return STLStream(packet=pkt, mode=STLTXCont()) - def get_streams (self, direction = 0, **kwargs): + def get_streams(self, direction=0, **kwargs): return [self.create_stream()] # dynamic load - used for trex console or simulator def register(): return STLS1() - - - diff --git a/src/plugins/nat/extras/nat_1Ms.py b/src/plugins/nat/extras/nat_1Ms.py index 73a91a70985..7271cf73781 100644 --- a/src/plugins/nat/extras/nat_1Ms.py +++ b/src/plugins/nat/extras/nat_1Ms.py @@ -1,35 +1,39 @@ from trex_stl_lib.api import * -class STLS1: - def create_stream (self): - base_pkt = Ether()/IP(dst="2.2.0.1")/UDP(dport=12) +class STLS1: + def create_stream(self): + base_pkt = Ether() / IP(dst="2.2.0.1") / UDP(dport=12) pad = Padding() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) - pad.load = '\x00' * pad_len + pad.load = "\x00" * pad_len vm = STLVM() - vm.tuple_var(name="tuple", ip_min="10.0.0.3", ip_max="10.0.39.18", port_min=1025, port_max=1124, limit_flows = 1000000) + vm.tuple_var( + name="tuple", + ip_min="10.0.0.3", + ip_max="10.0.39.18", + port_min=1025, + port_max=1124, + limit_flows=1000000, + ) vm.write(fv_name="tuple.ip", pkt_offset="IP.src") vm.fix_chksum() vm.write(fv_name="tuple.port", pkt_offset="UDP.sport") - pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm) + pkt = STLPktBuilder(pkt=base_pkt / pad, vm=vm) return STLStream(packet=pkt, mode=STLTXCont()) - def get_streams (self, direction = 0, **kwargs): + def get_streams(self, direction=0, **kwargs): return [self.create_stream()] # dynamic load - used for trex console or simulator def register(): return STLS1() - - - diff --git a/src/plugins/nat/extras/nat_out2in_100ks.py b/src/plugins/nat/extras/nat_out2in_100ks.py index 55ab5d42ee1..911f2cefda4 100644 --- a/src/plugins/nat/extras/nat_out2in_100ks.py +++ b/src/plugins/nat/extras/nat_out2in_100ks.py @@ -1,35 +1,39 @@ from trex_stl_lib.api import * -class STLS1: - def create_stream (self): - base_pkt = Ether()/IP(src="2.2.0.1")/UDP(sport=12) +class STLS1: + def create_stream(self): + base_pkt = Ether() / IP(src="2.2.0.1") / UDP(sport=12) pad = Padding() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) - pad.load = '\x00' * pad_len + pad.load = "\x00" * pad_len vm = STLVM() - vm.tuple_var(name="tuple", ip_min="173.16.1.3", ip_max="173.16.4.234", port_min=1025, port_max=1124, limit_flows = 100000) + vm.tuple_var( + name="tuple", + ip_min="173.16.1.3", + ip_max="173.16.4.234", + port_min=1025, + port_max=1124, + limit_flows=100000, + ) vm.write(fv_name="tuple.ip", pkt_offset="IP.dst") vm.fix_chksum() vm.write(fv_name="tuple.port", pkt_offset="UDP.dport") - pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm) + pkt = STLPktBuilder(pkt=base_pkt / pad, vm=vm) return STLStream(packet=pkt, mode=STLTXCont()) - def get_streams (self, direction = 0, **kwargs): + def get_streams(self, direction=0, **kwargs): return [self.create_stream()] # dynamic load - used for trex console or simulator def register(): return STLS1() - - - diff --git a/src/plugins/nat/extras/nat_out2in_10Ms.py b/src/plugins/nat/extras/nat_out2in_10Ms.py index 48d3d199080..b3493641ea0 100644 --- a/src/plugins/nat/extras/nat_out2in_10Ms.py +++ b/src/plugins/nat/extras/nat_out2in_10Ms.py @@ -1,35 +1,39 @@ from trex_stl_lib.api import * -class STLS1: - def create_stream (self): - base_pkt = Ether()/IP(src="2.2.0.1")/UDP(sport=12) +class STLS1: + def create_stream(self): + base_pkt = Ether() / IP(src="2.2.0.1") / UDP(sport=12) pad = Padding() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) - pad.load = '\x00' * pad_len + pad.load = "\x00" * pad_len vm = STLVM() - vm.tuple_var(name="tuple", ip_min="173.16.1.3", ip_max="173.17.135.162", port_min=1025, port_max=1124, limit_flows = 10000000) + vm.tuple_var( + name="tuple", + ip_min="173.16.1.3", + ip_max="173.17.135.162", + port_min=1025, + port_max=1124, + limit_flows=10000000, + ) vm.write(fv_name="tuple.ip", pkt_offset="IP.dst") vm.fix_chksum() vm.write(fv_name="tuple.port", pkt_offset="UDP.dport") - pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm) + pkt = STLPktBuilder(pkt=base_pkt / pad, vm=vm) return STLStream(packet=pkt, mode=STLTXCont()) - def get_streams (self, direction = 0, **kwargs): + def get_streams(self, direction=0, **kwargs): return [self.create_stream()] # dynamic load - used for trex console or simulator def register(): return STLS1() - - - diff --git a/src/plugins/nat/extras/nat_out2in_10ks.py b/src/plugins/nat/extras/nat_out2in_10ks.py index e961504fcf9..abd82ce320d 100644 --- a/src/plugins/nat/extras/nat_out2in_10ks.py +++ b/src/plugins/nat/extras/nat_out2in_10ks.py @@ -1,35 +1,39 @@ from trex_stl_lib.api import * -class STLS1: - def create_stream (self): - base_pkt = Ether()/IP(src="2.2.0.1")/UDP(sport=12) +class STLS1: + def create_stream(self): + base_pkt = Ether() / IP(src="2.2.0.1") / UDP(sport=12) pad = Padding() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) - pad.load = '\x00' * pad_len + pad.load = "\x00" * pad_len vm = STLVM() - vm.tuple_var(name="tuple", ip_min="173.16.1.3", ip_max="173.16.1.102", port_min=1025, port_max=1124, limit_flows = 100000) + vm.tuple_var( + name="tuple", + ip_min="173.16.1.3", + ip_max="173.16.1.102", + port_min=1025, + port_max=1124, + limit_flows=100000, + ) vm.write(fv_name="tuple.ip", pkt_offset="IP.dst") vm.fix_chksum() vm.write(fv_name="tuple.port", pkt_offset="UDP.dport") - pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm) + pkt = STLPktBuilder(pkt=base_pkt / pad, vm=vm) return STLStream(packet=pkt, mode=STLTXCont()) - def get_streams (self, direction = 0, **kwargs): + def get_streams(self, direction=0, **kwargs): return [self.create_stream()] # dynamic load - used for trex console or simulator def register(): return STLS1() - - - diff --git a/src/plugins/nat/extras/nat_out2in_1Ms.py b/src/plugins/nat/extras/nat_out2in_1Ms.py index d2cb0810263..c08ef191e6c 100644 --- a/src/plugins/nat/extras/nat_out2in_1Ms.py +++ b/src/plugins/nat/extras/nat_out2in_1Ms.py @@ -1,35 +1,39 @@ from trex_stl_lib.api import * -class STLS1: - def create_stream (self): - base_pkt = Ether()/IP(src="2.2.0.1")/UDP(sport=12) +class STLS1: + def create_stream(self): + base_pkt = Ether() / IP(src="2.2.0.1") / UDP(sport=12) pad = Padding() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) - pad.load = '\x00' * pad_len + pad.load = "\x00" * pad_len vm = STLVM() - vm.tuple_var(name="tuple", ip_min="173.16.1.3", ip_max="173.16.40.18", port_min=1025, port_max=1124, limit_flows = 1000000) + vm.tuple_var( + name="tuple", + ip_min="173.16.1.3", + ip_max="173.16.40.18", + port_min=1025, + port_max=1124, + limit_flows=1000000, + ) vm.write(fv_name="tuple.ip", pkt_offset="IP.dst") vm.fix_chksum() vm.write(fv_name="tuple.port", pkt_offset="UDP.dport") - pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm) + pkt = STLPktBuilder(pkt=base_pkt / pad, vm=vm) return STLStream(packet=pkt, mode=STLTXCont()) - def get_streams (self, direction = 0, **kwargs): + def get_streams(self, direction=0, **kwargs): return [self.create_stream()] # dynamic load - used for trex console or simulator def register(): return STLS1() - - - diff --git a/src/plugins/nat/extras/nat_ses_open.py b/src/plugins/nat/extras/nat_ses_open.py index d614d4e7356..a267a6b67fb 100644 --- a/src/plugins/nat/extras/nat_ses_open.py +++ b/src/plugins/nat/extras/nat_ses_open.py @@ -1,44 +1,72 @@ from trex_stl_lib.api import * -class STLS1: - def __init__ (self): - self.ip_range = {'local': {'start': "10.0.0.3", 'end': "10.1.255.255"}, - 'external': {'start': "172.16.1.3", 'end': "172.16.1.3"}, - 'remote': {'start': "2.2.0.1", 'end': "2.2.0.1"}} - self.port_range = {'local': {'start': 1025, 'end': 65535}, - 'remote': {'start': 12, 'end': 12}} +class STLS1: + def __init__(self): + self.ip_range = { + "local": {"start": "10.0.0.3", "end": "10.1.255.255"}, + "external": {"start": "172.16.1.3", "end": "172.16.1.3"}, + "remote": {"start": "2.2.0.1", "end": "2.2.0.1"}, + } + self.port_range = { + "local": {"start": 1025, "end": 65535}, + "remote": {"start": 12, "end": 12}, + } - def create_stream (self, vm): - base_pkt = Ether()/IP()/UDP() + def create_stream(self, vm): + base_pkt = Ether() / IP() / UDP() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) pad = Padding() - pad.load = '\x00' * pad_len - base_pkt = base_pkt/pad - + pad.load = "\x00" * pad_len + base_pkt = base_pkt / pad + pkt = STLPktBuilder(pkt=base_pkt, vm=vm) return STLStream(packet=pkt, mode=STLTXCont()) - def get_streams (self, direction = 0, **kwargs): + def get_streams(self, direction=0, **kwargs): if direction == 0: - ip_src = self.ip_range['remote'] - ip_dst = self.ip_range['external'] - src_port = self.port_range['remote'] - dst_port = self.port_range['local'] + ip_src = self.ip_range["remote"] + ip_dst = self.ip_range["external"] + src_port = self.port_range["remote"] + dst_port = self.port_range["local"] else: - ip_src = self.ip_range['local'] - ip_dst = self.ip_range['remote'] - src_port = self.port_range['local'] - dst_port = self.port_range['remote'] + ip_src = self.ip_range["local"] + ip_dst = self.ip_range["remote"] + src_port = self.port_range["local"] + dst_port = self.port_range["remote"] vm = STLVM() - vm.var(name="ip_src", min_value=ip_src['start'], max_value=ip_src['end'], size=4, op="random") - vm.var(name="ip_dst", min_value=ip_dst['start'], max_value=ip_dst['end'], size=4, op="random") - vm.var(name="src_port", min_value=src_port['start'], max_value=src_port['end'], size=2, op="random") - vm.var(name="dst_port", min_value=dst_port['start'], max_value=dst_port['end'], size=2, op="random") + vm.var( + name="ip_src", + min_value=ip_src["start"], + max_value=ip_src["end"], + size=4, + op="random", + ) + vm.var( + name="ip_dst", + min_value=ip_dst["start"], + max_value=ip_dst["end"], + size=4, + op="random", + ) + vm.var( + name="src_port", + min_value=src_port["start"], + max_value=src_port["end"], + size=2, + op="random", + ) + vm.var( + name="dst_port", + min_value=dst_port["start"], + max_value=dst_port["end"], + size=2, + op="random", + ) vm.write(fv_name="ip_src", pkt_offset="IP.src") vm.write(fv_name="ip_dst", pkt_offset="IP.dst") @@ -47,12 +75,9 @@ class STLS1: vm.fix_chksum() - return [ self.create_stream(vm) ] + return [self.create_stream(vm)] # dynamic load - used for trex console or simulator def register(): return STLS1() - - - diff --git a/src/plugins/nat/extras/nat_static_gen_cfg.py b/src/plugins/nat/extras/nat_static_gen_cfg.py index 9e59bbfc0c2..009cf099582 100755 --- a/src/plugins/nat/extras/nat_static_gen_cfg.py +++ b/src/plugins/nat/extras/nat_static_gen_cfg.py @@ -2,24 +2,24 @@ import ipaddress import argparse -parser = argparse.ArgumentParser(description='Generate NAT plugin config.') -parser.add_argument('static_map_num', metavar='N', type=int, nargs=1, - help='number of static mappings') +parser = argparse.ArgumentParser(description="Generate NAT plugin config.") +parser.add_argument( + "static_map_num", metavar="N", type=int, nargs=1, help="number of static mappings" +) args = parser.parse_args() -file_name = 'nat_static_%s' % (args.static_map_num[0]) -outfile = open(file_name, 'w') +file_name = "nat_static_%s" % (args.static_map_num[0]) +outfile = open(file_name, "w") -outfile.write('set int ip address TenGigabitEthernet4/0/0 172.16.2.1/24\n') -outfile.write('set int ip address TenGigabitEthernet4/0/1 173.16.1.1/24\n') -outfile.write('set int state TenGigabitEthernet4/0/0 up\n') -outfile.write('set int state TenGigabitEthernet4/0/1 up\n') -outfile.write('ip route add 2.2.0.0/16 via 173.16.1.2 TenGigabitEthernet4/0/1\n') -outfile.write('ip route add 10.0.0.0/24 via 172.16.2.2 TenGigabitEthernet4/0/0\n') -outfile.write('set int nat44 in TenGigabitEthernet4/0/0 out TenGigabitEthernet4/0/1\n') - -for i in range (0, args.static_map_num[0]): - local = str(ipaddress.IPv4Address(u'10.0.0.3') + i) - external = str(ipaddress.IPv4Address(u'173.16.1.3') + i) - outfile.write('nat44 add static mapping local %s external %s\n' % (local, external)) +outfile.write("set int ip address TenGigabitEthernet4/0/0 172.16.2.1/24\n") +outfile.write("set int ip address TenGigabitEthernet4/0/1 173.16.1.1/24\n") +outfile.write("set int state TenGigabitEthernet4/0/0 up\n") +outfile.write("set int state TenGigabitEthernet4/0/1 up\n") +outfile.write("ip route add 2.2.0.0/16 via 173.16.1.2 TenGigabitEthernet4/0/1\n") +outfile.write("ip route add 10.0.0.0/24 via 172.16.2.2 TenGigabitEthernet4/0/0\n") +outfile.write("set int nat44 in TenGigabitEthernet4/0/0 out TenGigabitEthernet4/0/1\n") +for i in range(0, args.static_map_num[0]): + local = str(ipaddress.IPv4Address("10.0.0.3") + i) + external = str(ipaddress.IPv4Address("173.16.1.3") + i) + outfile.write("nat44 add static mapping local %s external %s\n" % (local, external)) diff --git a/src/plugins/nat/extras/nat_test_fast_path.py b/src/plugins/nat/extras/nat_test_fast_path.py index e869d40872a..fb880fb9e96 100644 --- a/src/plugins/nat/extras/nat_test_fast_path.py +++ b/src/plugins/nat/extras/nat_test_fast_path.py @@ -2,7 +2,6 @@ from trex_stl_lib.api import * class STLS1: - def create_stream(self): # base_pkt = Ether()/IP(dst="2.2.0.1")/UDP(dport=12) @@ -24,37 +23,46 @@ class STLS1: # return STLStream(packet=pkt, mode=STLTXCont()) - vm = STLScVmRaw([STLVmTupleGen(ip_min="10.0.0.1", ip_max="10.255.255.254", - port_min=1025, port_max=65535, - # name="stuple", limit_flows=10000), - name="stuple", limit_flows=100), - STLVmTupleGen(ip_min="2.0.0.1", ip_max="2.255.255.254", - port_min=1025, port_max=65535, - # name="dtuple", limit_flows=100000000), - name="dtuple", limit_flows=100), - - # write ip to packet IP.src - STLVmWrFlowVar(fv_name="stuple.ip", - pkt_offset="IP.src"), - STLVmWrFlowVar(fv_name="dtuple.ip", - pkt_offset="IP.dst"), - # fix checksum - STLVmFixIpv4(offset="IP"), - # write udp.port - STLVmWrFlowVar(fv_name="stuple.port", - pkt_offset="UDP.sport"), - STLVmWrFlowVar(fv_name="dtuple.port", - pkt_offset="UDP.dport"), - ] - ) - - base_pkt = Ether()/IP(src="16.0.0.1", dst="2.0.0.1")/UDP(dport=12, sport=1025) + vm = STLScVmRaw( + [ + STLVmTupleGen( + ip_min="10.0.0.1", + ip_max="10.255.255.254", + port_min=1025, + port_max=65535, + # name="stuple", limit_flows=10000), + name="stuple", + limit_flows=100, + ), + STLVmTupleGen( + ip_min="2.0.0.1", + ip_max="2.255.255.254", + port_min=1025, + port_max=65535, + # name="dtuple", limit_flows=100000000), + name="dtuple", + limit_flows=100, + ), + # write ip to packet IP.src + STLVmWrFlowVar(fv_name="stuple.ip", pkt_offset="IP.src"), + STLVmWrFlowVar(fv_name="dtuple.ip", pkt_offset="IP.dst"), + # fix checksum + STLVmFixIpv4(offset="IP"), + # write udp.port + STLVmWrFlowVar(fv_name="stuple.port", pkt_offset="UDP.sport"), + STLVmWrFlowVar(fv_name="dtuple.port", pkt_offset="UDP.dport"), + ] + ) + + base_pkt = ( + Ether() / IP(src="16.0.0.1", dst="2.0.0.1") / UDP(dport=12, sport=1025) + ) pad = Padding() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) - pad.load = '\x00' * pad_len + pad.load = "\x00" * pad_len - pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm) + pkt = STLPktBuilder(pkt=base_pkt / pad, vm=vm) return STLStream(packet=pkt, mode=STLTXCont()) diff --git a/src/plugins/nat/extras/nat_test_slow_path.py b/src/plugins/nat/extras/nat_test_slow_path.py index a6351b98adf..3145a2c6a59 100644 --- a/src/plugins/nat/extras/nat_test_slow_path.py +++ b/src/plugins/nat/extras/nat_test_slow_path.py @@ -2,7 +2,6 @@ from trex_stl_lib.api import * class STLS1: - def create_stream(self): # base_pkt = Ether()/IP(dst="2.2.0.1")/UDP(dport=12) @@ -24,35 +23,44 @@ class STLS1: # return STLStream(packet=pkt, mode=STLTXCont()) - vm = STLScVmRaw([STLVmTupleGen(ip_min="10.0.0.1", ip_max="10.255.255.254", - port_min=1025, port_max=65535, - name="stuple", limit_flows=10000), - STLVmTupleGen(ip_min="2.0.0.1", ip_max="2.255.255.254", - port_min=1025, port_max=65535, - name="dtuple", limit_flows=100000000), - - # write ip to packet IP.src - STLVmWrFlowVar(fv_name="stuple.ip", - pkt_offset="IP.src"), - STLVmWrFlowVar(fv_name="dtuple.ip", - pkt_offset="IP.dst"), - # fix checksum - STLVmFixIpv4(offset="IP"), - # write udp.port - STLVmWrFlowVar(fv_name="stuple.port", - pkt_offset="UDP.sport"), - STLVmWrFlowVar(fv_name="dtuple.port", - pkt_offset="UDP.dport"), - ] - ) - - base_pkt = Ether()/IP(src="16.0.0.1", dst="2.0.0.1")/UDP(dport=12, sport=1025) + vm = STLScVmRaw( + [ + STLVmTupleGen( + ip_min="10.0.0.1", + ip_max="10.255.255.254", + port_min=1025, + port_max=65535, + name="stuple", + limit_flows=10000, + ), + STLVmTupleGen( + ip_min="2.0.0.1", + ip_max="2.255.255.254", + port_min=1025, + port_max=65535, + name="dtuple", + limit_flows=100000000, + ), + # write ip to packet IP.src + STLVmWrFlowVar(fv_name="stuple.ip", pkt_offset="IP.src"), + STLVmWrFlowVar(fv_name="dtuple.ip", pkt_offset="IP.dst"), + # fix checksum + STLVmFixIpv4(offset="IP"), + # write udp.port + STLVmWrFlowVar(fv_name="stuple.port", pkt_offset="UDP.sport"), + STLVmWrFlowVar(fv_name="dtuple.port", pkt_offset="UDP.dport"), + ] + ) + + base_pkt = ( + Ether() / IP(src="16.0.0.1", dst="2.0.0.1") / UDP(dport=12, sport=1025) + ) pad = Padding() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) - pad.load = '\x00' * pad_len + pad.load = "\x00" * pad_len - pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm) + pkt = STLPktBuilder(pkt=base_pkt / pad, vm=vm) return STLStream(packet=pkt, mode=STLTXCont()) diff --git a/src/plugins/nat/extras/nat_test_slow_path_with_latency.py b/src/plugins/nat/extras/nat_test_slow_path_with_latency.py index 6c7663434c5..0c08e7a5e80 100644 --- a/src/plugins/nat/extras/nat_test_slow_path_with_latency.py +++ b/src/plugins/nat/extras/nat_test_slow_path_with_latency.py @@ -2,7 +2,6 @@ from trex_stl_lib.api import * class STLS1: - def create_stream(self, port_id): # base_pkt = Ether()/IP(dst="2.2.0.1")/UDP(dport=12) @@ -24,49 +23,61 @@ class STLS1: # return STLStream(packet=pkt, mode=STLTXCont()) - vm = STLScVmRaw([STLVmTupleGen(ip_min="10.0.0.1", ip_max="10.255.255.254", - port_min=1025, port_max=65535, - name="stuple", limit_flows=10000), - STLVmTupleGen(ip_min="2.0.0.1", ip_max="2.255.255.254", - port_min=1025, port_max=65535, - name="dtuple", limit_flows=100000000), - - # write ip to packet IP.src - STLVmWrFlowVar(fv_name="stuple.ip", - pkt_offset="IP.src"), - STLVmWrFlowVar(fv_name="dtuple.ip", - pkt_offset="IP.dst"), - # fix checksum - STLVmFixIpv4(offset="IP"), - # write udp.port - STLVmWrFlowVar(fv_name="stuple.port", - pkt_offset="UDP.sport"), - STLVmWrFlowVar(fv_name="dtuple.port", - pkt_offset="UDP.dport"), - ] - ) - - base_pkt = Ether()/IP(src="16.0.0.1", dst="2.0.0.1")/UDP(dport=12, sport=1025) + vm = STLScVmRaw( + [ + STLVmTupleGen( + ip_min="10.0.0.1", + ip_max="10.255.255.254", + port_min=1025, + port_max=65535, + name="stuple", + limit_flows=10000, + ), + STLVmTupleGen( + ip_min="2.0.0.1", + ip_max="2.255.255.254", + port_min=1025, + port_max=65535, + name="dtuple", + limit_flows=100000000, + ), + # write ip to packet IP.src + STLVmWrFlowVar(fv_name="stuple.ip", pkt_offset="IP.src"), + STLVmWrFlowVar(fv_name="dtuple.ip", pkt_offset="IP.dst"), + # fix checksum + STLVmFixIpv4(offset="IP"), + # write udp.port + STLVmWrFlowVar(fv_name="stuple.port", pkt_offset="UDP.sport"), + STLVmWrFlowVar(fv_name="dtuple.port", pkt_offset="UDP.dport"), + ] + ) + + base_pkt = ( + Ether() / IP(src="16.0.0.1", dst="2.0.0.1") / UDP(dport=12, sport=1025) + ) pad = Padding() if len(base_pkt) < 64: pad_len = 64 - len(base_pkt) - pad.load = '\x00' * pad_len + pad.load = "\x00" * pad_len - pad = max(0, 64 - len(base_pkt)) * 'x' - pad_latency = max(0, (64-4) - len(base_pkt)) * 'x' + pad = max(0, 64 - len(base_pkt)) * "x" + pad_latency = max(0, (64 - 4) - len(base_pkt)) * "x" - pkt = STLPktBuilder(pkt=base_pkt/pad, vm=vm) + pkt = STLPktBuilder(pkt=base_pkt / pad, vm=vm) - return [STLStream(packet=pkt, mode=STLTXCont()), - # latency stream - STLStream(packet = STLPktBuilder(pkt = base_pkt/pad_latency), - mode = STLTXCont(pps=1000), - flow_stats = STLFlowLatencyStats(pg_id = 12+port_id)) - ] + return [ + STLStream(packet=pkt, mode=STLTXCont()), + # latency stream + STLStream( + packet=STLPktBuilder(pkt=base_pkt / pad_latency), + mode=STLTXCont(pps=1000), + flow_stats=STLFlowLatencyStats(pg_id=12 + port_id), + ), + ] def get_streams(self, direction=0, **kwargs): # return [self.create_stream()] - return self.create_stream(kwargs['port_id']) + return self.create_stream(kwargs["port_id"]) # dynamic load - used for trex console or simulator diff --git a/src/plugins/nat/pnat/tests/test_genpackets.py b/src/plugins/nat/pnat/tests/test_genpackets.py index 9d32d3e3656..40867317078 100755 --- a/src/plugins/nat/pnat/tests/test_genpackets.py +++ b/src/plugins/nat/pnat/tests/test_genpackets.py @@ -6,33 +6,35 @@ from importlib.machinery import SourceFileLoader from scapy.all import * from scapy.contrib.geneve import GENEVE + def hexstring(p): s = bytes(p.__class__(p)) return ",".join("0x{:02x}".format(c) for c in s) + def output_test(filename, tests): (name, ext) = os.path.basename(filename).split(".") - print('/* DO NOT EDIT: automatically generated by test_genpackets.py */') - print('/* clang-format off */') - print('test_t tests_{}[] = {{'.format(name)) + print("/* DO NOT EDIT: automatically generated by test_genpackets.py */") + print("/* clang-format off */") + print("test_t tests_{}[] = {{".format(name)) for t in tests: - print(' {') + print(" {") print(' .name = "{}",'.format(t[0])) - print(' .nsend = {},'.format(len(t[1]))) - print(' .send = (char []){{{}}},'.format(hexstring(t[1]))) - print(' .nexpect = {},'.format(len(t[2]))) - print(' .expect = (char []){{{}}},'.format(hexstring(t[2]))) - print(' .expect_next_index = {}'.format(t[3])) - print(' },') - print('};') - print('/* clang-format on */') + print(" .nsend = {},".format(len(t[1]))) + print(" .send = (char []){{{}}},".format(hexstring(t[1]))) + print(" .nexpect = {},".format(len(t[2]))) + print(" .expect = (char []){{{}}},".format(hexstring(t[2]))) + print(" .expect_next_index = {}".format(t[3])) + print(" },") + print("};") + print("/* clang-format on */") + # Read tests from file for filename in sys.argv[1:]: with open(filename) as f: - content = f.read().replace('\n', '') + content = f.read().replace("\n", "") tests = eval(content) output_test(filename, tests) - diff --git a/src/plugins/srv6-mobile/extra/runner.py b/src/plugins/srv6-mobile/extra/runner.py index b18fdc32766..eee89d7d41a 100755 --- a/src/plugins/srv6-mobile/extra/runner.py +++ b/src/plugins/srv6-mobile/extra/runner.py @@ -1,10 +1,8 @@ #!/usr/bin/env python3 -from os.path import dirname, realpath, split,\ - join, isdir, exists +from os.path import dirname, realpath, split, join, isdir, exists from os import remove, system, mkdir -from logging import getLogger, basicConfig,\ - DEBUG, INFO, ERROR +from logging import getLogger, basicConfig, DEBUG, INFO, ERROR from argparse import ArgumentParser from atexit import register from shutil import rmtree @@ -18,10 +16,7 @@ from scapy.contrib.gtp import * from scapy.all import * -verbose_levels = { - 'error': ERROR, - 'debug': DEBUG, - 'info': INFO} +verbose_levels = {"error": ERROR, "debug": DEBUG, "info": INFO} class ContainerStartupError(Exception): @@ -87,10 +82,8 @@ class Container(object): image=image, name=name, privileged=True, - volumes={ - temp: { - 'bind': '/mnt', - 'mode': 'rw'}}) + volumes={temp: {"bind": "/mnt", "mode": "rw"}}, + ) obj = cls.get(client, name) if not obj: @@ -119,7 +112,7 @@ class Container(object): def vppctl_exec(self, cmd): ec, resp = self._ref.exec_run(cmd="{} {}".format(self.cmd, cmd)) - assert(ec == 0) + assert ec == 0 return resp def setup_host_interface(self, name, ip): @@ -134,8 +127,7 @@ class Container(object): self.vppctl_exec("create packet-generator interface pg0") self.vppctl_exec("set int mac address pg0 {}".format(local_mac)) self.vppctl_exec("set int ip addr pg0 {}".format(local_ip)) - self.vppctl_exec( - "set ip neighbor pg0 {} {}".format(remote_ip, remote_mac)) + self.vppctl_exec("set ip neighbor pg0 {} {}".format(remote_ip, remote_mac)) self.vppctl_exec("set int state pg0 up") def pg_create_interface4(self, local_ip, remote_ip, local_mac, remote_mac): @@ -158,24 +150,32 @@ class Container(object): self.vppctl_exec("set ip neighbor pg0 {} {}".format(remote_ip, remote_mac)) self.vppctl_exec("set int state pg0 up") - def pg_create_interface4_name(self, ifname, local_ip, remote_ip, local_mac, remote_mac): + def pg_create_interface4_name( + self, ifname, local_ip, remote_ip, local_mac, remote_mac + ): # remote_ip can't have subnet mask time.sleep(2) self.vppctl_exec("create packet-generator interface {}".format(ifname)) self.vppctl_exec("set int mac address {} {}".format(ifname, local_mac)) self.vppctl_exec("set int ip addr {} {}".format(ifname, local_ip)) - self.vppctl_exec("set ip neighbor {} {} {}".format(ifname, remote_ip, remote_mac)) + self.vppctl_exec( + "set ip neighbor {} {} {}".format(ifname, remote_ip, remote_mac) + ) self.vppctl_exec("set int state {} up".format(ifname)) - def pg_create_interface6_name(self, ifname, local_ip, remote_ip, local_mac, remote_mac): + def pg_create_interface6_name( + self, ifname, local_ip, remote_ip, local_mac, remote_mac + ): # remote_ip can't have subnet mask time.sleep(2) self.vppctl_exec("create packet-generator interface {}".format(ifname)) self.vppctl_exec("set int mac address {} {}".format(ifname, local_mac)) self.vppctl_exec("set int ip addr {} {}".format(ifname, local_ip)) - self.vppctl_exec("set ip neighbor {} {} {}".format(ifname, remote_ip, remote_mac)) + self.vppctl_exec( + "set ip neighbor {} {} {}".format(ifname, remote_ip, remote_mac) + ) self.vppctl_exec("set int state {} up".format(ifname)) def pg_enable(self): @@ -186,55 +186,56 @@ class Container(object): wrpcap(self.pg_input_file, stream) self.vppctl_exec( "packet-generator new name pg-stream " - "node ethernet-input pcap {}".format( - self.pg_input_file_in)) + "node ethernet-input pcap {}".format(self.pg_input_file_in) + ) def pg_start_capture(self): if exists(self.pg_output_file): remove(self.pg_output_file) self.vppctl_exec( - "packet-generator capture pg0 pcap {}".format( - self.pg_output_file_in)) + "packet-generator capture pg0 pcap {}".format(self.pg_output_file_in) + ) def pg_start_capture_name(self, ifname): if exists(self.pg_output_file): remove(self.pg_output_file) self.vppctl_exec( - "packet-generator capture {} pcap {}".format( - ifname, self.pg_output_file_in)) + "packet-generator capture {} pcap {}".format(ifname, self.pg_output_file_in) + ) def pg_read_packets(self): return rdpcap(self.pg_output_file) def set_ipv6_route(self, out_if_name, next_hop_ip, subnet): self.vppctl_exec( - "ip route add {} via host-{} {}".format( - subnet, out_if_name, next_hop_ip)) + "ip route add {} via host-{} {}".format(subnet, out_if_name, next_hop_ip) + ) def set_ipv6_route2(self, out_if_name, next_hop_ip, subnet): self.vppctl_exec( - "ip route add {} via {} {}".format( - subnet, out_if_name, next_hop_ip)) + "ip route add {} via {} {}".format(subnet, out_if_name, next_hop_ip) + ) def set_ip_pgroute(self, out_if_name, next_hop_ip, subnet): - self.vppctl_exec("ip route add {} via {} {}".format( - subnet, out_if_name, next_hop_ip)) + self.vppctl_exec( + "ip route add {} via {} {}".format(subnet, out_if_name, next_hop_ip) + ) def set_ipv6_pgroute(self, out_if_name, next_hop_ip, subnet): - self.vppctl_exec("ip route add {} via {} {}".format( - subnet, out_if_name, next_hop_ip)) + self.vppctl_exec( + "ip route add {} via {} {}".format(subnet, out_if_name, next_hop_ip) + ) def set_ipv6_default_route(self, out_if_name, next_hop_ip): self.vppctl_exec( - "ip route add ::/0 via host-{} {}".format( - out_if_name, next_hop_ip)) + "ip route add ::/0 via host-{} {}".format(out_if_name, next_hop_ip) + ) def enable_trace(self, count): self.vppctl_exec("trace add af-packet-input {}".format(count)) class Containers(object): - def __init__(self, client, image): self.client = client self.image = image @@ -247,37 +248,45 @@ class Containers(object): register(lambda: remove(path)) def build(self, path, vpp_path): - env = Environment(loader=FileSystemLoader(path), - autoescape=True, - trim_blocks=True) - - self.tmp_render(join(vpp_path, "Dockerfile"), - env.get_template("Dockerfile.j2"), - {'vpp_path': vpp_path}) - - self.tmp_render(join(vpp_path, "startup.conf"), - env.get_template("startup.conf.j2"), - {'vpp_path': vpp_path}) - - ref, _ = self.client.images.build(path=vpp_path, - tag=self.image, rm=True) + env = Environment( + loader=FileSystemLoader(path), autoescape=True, trim_blocks=True + ) + + self.tmp_render( + join(vpp_path, "Dockerfile"), + env.get_template("Dockerfile.j2"), + {"vpp_path": vpp_path}, + ) + + self.tmp_render( + join(vpp_path, "startup.conf"), + env.get_template("startup.conf.j2"), + {"vpp_path": vpp_path}, + ) + + ref, _ = self.client.images.build(path=vpp_path, tag=self.image, rm=True) return ref def release(self, path, vpp_path): - env = Environment(loader=FileSystemLoader(path), - autoescape=True, - trim_blocks=True) - - self.tmp_render(join(vpp_path, "Dockerfile"), - env.get_template("Dockerfile.j2.release"), - {'vpp_path': vpp_path}) - - self.tmp_render(join(vpp_path, "startup.conf"), - env.get_template("startup.conf.j2"), - {'vpp_path': vpp_path}) - - ref, _ = self.client.images.build(path=vpp_path, - tag="srv6m-release-image", rm=True) + env = Environment( + loader=FileSystemLoader(path), autoescape=True, trim_blocks=True + ) + + self.tmp_render( + join(vpp_path, "Dockerfile"), + env.get_template("Dockerfile.j2.release"), + {"vpp_path": vpp_path}, + ) + + self.tmp_render( + join(vpp_path, "startup.conf"), + env.get_template("startup.conf.j2"), + {"vpp_path": vpp_path}, + ) + + ref, _ = self.client.images.build( + path=vpp_path, tag="srv6m-release-image", rm=True + ) return ref def new(self, name): @@ -299,7 +308,6 @@ class Containers(object): class Network(object): - def __init__(self, ref, name): self._name = name self._ref = ref @@ -310,8 +318,7 @@ class Network(object): @classmethod def new(cls, client, name): - ref = client.networks.create(name, driver="bridge", - check_duplicate=True) + ref = client.networks.create(name, driver="bridge", check_duplicate=True) return cls(ref, name) @classmethod @@ -331,7 +338,6 @@ class Network(object): class Networks(object): - def __init__(self, client): self.client = client @@ -352,14 +358,9 @@ class Program(object): # for exmaple what the vpp is supposed to be # in our topoloty overview - instance_names = ["vpp-1", - "vpp-2", - "vpp-3", - "vpp-4"] + instance_names = ["vpp-1", "vpp-2", "vpp-3", "vpp-4"] - network_names = ["net-1", - "net-2", - "net-3"] + network_names = ["net-1", "net-2", "net-3"] def __init__(self, image=None, prefix=None): self.path = dirname(realpath(__file__)) @@ -469,15 +470,20 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface( local_ip="B::1/120", remote_ip="B::2", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="B::2") / ICMPv6EchoRequest()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="B::2") + / ICMPv6EchoRequest() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -516,16 +522,17 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface( local_ip="B::1/120", remote_ip="B::2", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr D1::") - c1.vppctl_exec( - "sr policy add bsid D1::999:1 next D2:: next D3:: next D4::") + c1.vppctl_exec("sr policy add bsid D1::999:1 next D2:: next D3:: next D4::") c1.vppctl_exec("sr steer l3 B::/120 via bsid D1::999:1") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -539,8 +546,11 @@ class Program(object): c3.set_ipv6_route("eth2", "A3::2", "D4::/128") c3.set_ipv6_route("eth1", "A2::1", "C::/120") - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="B::2") / ICMPv6EchoRequest()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="B::2") + / ICMPv6EchoRequest() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -561,7 +571,7 @@ class Program(object): for p in c4.pg_read_packets(): p.show2() - ''' T.Map is obsolete + """ T.Map is obsolete def test_tmap(self): # TESTS: # trace add af-packet-input 10 @@ -844,7 +854,7 @@ class Program(object): print("Receiving packet on {}:".format(c4.name)) for p in c4.pg_read_packets(): p.show2() - ''' + """ def test_gtp4(self): # TESTS: @@ -863,16 +873,20 @@ class Program(object): local_ip="172.16.0.1/30", remote_ip="172.16.0.2/30", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface4( local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4") + c1.vppctl_exec( + "sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4" + ) c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -880,8 +894,8 @@ class Program(object): c3.vppctl_exec("sr localsid address D3:: behavior end") c4.vppctl_exec( - "sr localsid prefix D4::/32 " - "behavior end.m.gtp4.e v4src_position 64") + "sr localsid prefix D4::/32 " "behavior end.m.gtp4.e v4src_position 64" + ) c2.set_ipv6_route("eth2", "A2::2", "D3::/128") c2.set_ipv6_route("eth1", "A1::1", "C::/120") @@ -889,17 +903,19 @@ class Program(object): c3.set_ipv6_route("eth1", "A2::1", "C::/120") c4.set_ip_pgroute("pg0", "1.0.0.1", "172.20.0.1/32") - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IP(src="172.20.0.2", dst="172.20.0.1") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - IP(src="172.99.0.1", dst="172.99.0.2") / - ICMP()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IP(src="172.20.0.2", dst="172.20.0.1") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / IP(src="172.99.0.1", dst="172.99.0.2") + / ICMP() + ) print("Sending packet on {}:".format(c1.name)) p.show2() - time.sleep(10) + time.sleep(10) c1.enable_trace(10) c4.enable_trace(10) @@ -933,16 +949,20 @@ class Program(object): local_ip="172.16.0.1/30", remote_ip="172.16.0.2/30", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface4( local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:1111:aaaa:bbbb::") - c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4") + c1.vppctl_exec( + "sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4" + ) c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid prefix D2:1111:aaaa::/48 behavior end usid 16") @@ -950,8 +970,8 @@ class Program(object): c3.vppctl_exec("sr localsid prefix D2:1111:bbbb::/48 behavior end usid 16") c4.vppctl_exec( - "sr localsid prefix D4::/32 " - "behavior end.m.gtp4.e v4src_position 64") + "sr localsid prefix D4::/32 " "behavior end.m.gtp4.e v4src_position 64" + ) c2.set_ipv6_route("eth2", "A2::2", "D2:1111:bbbb::/48") c2.set_ipv6_route("eth1", "A1::1", "C::/120") @@ -959,17 +979,19 @@ class Program(object): c3.set_ipv6_route("eth1", "A2::1", "C::/120") c4.set_ip_pgroute("pg0", "1.0.0.1", "172.20.0.1/32") - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IP(src="172.20.0.2", dst="172.20.0.1") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - IP(src="172.99.0.1", dst="172.99.0.2") / - ICMP()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IP(src="172.20.0.2", dst="172.20.0.1") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / IP(src="172.99.0.1", dst="172.99.0.2") + / ICMP() + ) print("Sending packet on {}:".format(c1.name)) p.show2() - time.sleep(10) + time.sleep(10) c1.enable_trace(10) c4.enable_trace(10) @@ -1003,16 +1025,20 @@ class Program(object): local_ip="172.16.0.1/30", remote_ip="172.16.0.2/30", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface4( local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4") + c1.vppctl_exec( + "sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4" + ) c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1020,8 +1046,8 @@ class Program(object): c3.vppctl_exec("sr localsid address D3:: behavior end") c4.vppctl_exec( - "sr localsid prefix D4::/32 " - "behavior end.m.gtp4.e v4src_position 64") + "sr localsid prefix D4::/32 " "behavior end.m.gtp4.e v4src_position 64" + ) c2.set_ipv6_route("eth2", "A2::2", "D3::/128") c2.set_ipv6_route("eth1", "A1::1", "C::/120") @@ -1029,13 +1055,15 @@ class Program(object): c3.set_ipv6_route("eth1", "A2::1", "C::/120") c4.set_ip_pgroute("pg0", "1.0.0.1", "172.20.0.1/32") - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IP(src="172.20.0.2", dst="172.20.0.1") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - GTPPDUSessionContainer(type=1, R=1, QFI=3) / - IP(src="172.99.0.1", dst="172.99.0.2") / - ICMP()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IP(src="172.20.0.2", dst="172.20.0.1") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / GTPPDUSessionContainer(type=1, R=1, QFI=3) + / IP(src="172.99.0.1", dst="172.99.0.2") + / ICMP() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1073,16 +1101,20 @@ class Program(object): local_ip="172.16.0.1/30", remote_ip="172.16.0.2/30", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface4( local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4") + c1.vppctl_exec( + "sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4" + ) c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1090,8 +1122,8 @@ class Program(object): c3.vppctl_exec("sr localsid address D3:: behavior end") c4.vppctl_exec( - "sr localsid prefix D4::/32 " - "behavior end.m.gtp4.e v4src_position 64") + "sr localsid prefix D4::/32 " "behavior end.m.gtp4.e v4src_position 64" + ) c2.set_ipv6_route("eth2", "A2::2", "D3::/128") c2.set_ipv6_route("eth1", "A1::1", "C::/120") @@ -1099,10 +1131,12 @@ class Program(object): c3.set_ipv6_route("eth1", "A2::1", "C::/120") c4.set_ip_pgroute("pg0", "1.0.0.1", "172.20.0.1/32") - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IP(src="172.20.0.2", dst="172.20.0.1") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="echo_request", S=1, teid=200, seq=200)) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IP(src="172.20.0.2", dst="172.20.0.1") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="echo_request", S=1, teid=200, seq=200) + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1140,16 +1174,20 @@ class Program(object): local_ip="172.16.0.1/30", remote_ip="172.16.0.2/30", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface4( local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4") + c1.vppctl_exec( + "sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4" + ) c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1157,8 +1195,8 @@ class Program(object): c3.vppctl_exec("sr localsid address D3:: behavior end") c4.vppctl_exec( - "sr localsid prefix D4::/32 " - "behavior end.m.gtp4.e v4src_position 64") + "sr localsid prefix D4::/32 " "behavior end.m.gtp4.e v4src_position 64" + ) c2.set_ipv6_route("eth2", "A2::2", "D3::/128") c2.set_ipv6_route("eth1", "A1::1", "C::/120") @@ -1166,10 +1204,12 @@ class Program(object): c3.set_ipv6_route("eth1", "A2::1", "C::/120") c4.set_ip_pgroute("pg0", "1.0.0.1", "172.20.0.1/32") - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IP(src="172.20.0.2", dst="172.20.0.1") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="echo_response", S=1, teid=200, seq=200)) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IP(src="172.20.0.2", dst="172.20.0.1") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="echo_response", S=1, teid=200, seq=200) + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1207,16 +1247,20 @@ class Program(object): local_ip="172.16.0.1/30", remote_ip="172.16.0.2/30", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface4( local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4") + c1.vppctl_exec( + "sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64 nhtype ipv4" + ) c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1224,8 +1268,8 @@ class Program(object): c3.vppctl_exec("sr localsid address D3:: behavior end") c4.vppctl_exec( - "sr localsid prefix D4::/32 " - "behavior end.m.gtp4.e v4src_position 64") + "sr localsid prefix D4::/32 " "behavior end.m.gtp4.e v4src_position 64" + ) c2.set_ipv6_route("eth2", "A2::2", "D3::/128") c2.set_ipv6_route("eth1", "A1::1", "C::/120") @@ -1233,12 +1277,15 @@ class Program(object): c3.set_ipv6_route("eth1", "A2::1", "C::/120") c4.set_ip_pgroute("pg0", "1.0.0.1", "172.20.0.1/32") - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IP(src="172.20.0.2", dst="172.20.0.1") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="error_indication", S=1, teid=200, seq=200)/ - IE_TEIDI(TEIDI=65535)/IE_GSNAddress(address="1.1.1.1")/ - IE_PrivateExtension(extention_value="z")) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IP(src="172.20.0.2", dst="172.20.0.1") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="error_indication", S=1, teid=200, seq=200) + / IE_TEIDI(TEIDI=65535) + / IE_GSNAddress(address="1.1.1.1") + / IE_PrivateExtension(extention_value="z") + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1276,16 +1323,20 @@ class Program(object): local_ip="172.16.0.1/30", remote_ip="172.16.0.2/30", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface4( local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64") + c1.vppctl_exec( + "sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64" + ) c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1293,8 +1344,8 @@ class Program(object): c3.vppctl_exec("sr localsid address D3:: behavior end") c4.vppctl_exec( - "sr localsid prefix D4::/32 " - "behavior end.m.gtp4.e v4src_position 64") + "sr localsid prefix D4::/32 " "behavior end.m.gtp4.e v4src_position 64" + ) c2.set_ipv6_route("eth2", "A2::2", "D3::/128") c2.set_ipv6_route("eth1", "A1::1", "C::/120") @@ -1302,12 +1353,14 @@ class Program(object): c3.set_ipv6_route("eth1", "A2::1", "C::/120") c4.set_ip_pgroute("pg0", "1.0.0.1", "172.20.0.1/32") - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IP(src="172.20.0.2", dst="172.20.0.1") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - IPv6(src="2001::1", dst="2002::1") / - ICMPv6EchoRequest()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IP(src="172.20.0.2", dst="172.20.0.1") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / IPv6(src="2001::1", dst="2002::1") + / ICMPv6EchoRequest() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1345,16 +1398,20 @@ class Program(object): local_ip="172.16.0.1/30", remote_ip="172.16.0.2/30", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface4( local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64") + c1.vppctl_exec( + "sr policy add bsid D5:: behavior t.m.gtp4.d D4::/32 v6src_prefix C1::/64" + ) c1.vppctl_exec("sr steer l3 172.20.0.1/32 via bsid D5::") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1362,8 +1419,8 @@ class Program(object): c3.vppctl_exec("sr localsid address D3:: behavior end") c4.vppctl_exec( - "sr localsid prefix D4::/32 " - "behavior end.m.gtp4.e v4src_position 64") + "sr localsid prefix D4::/32 " "behavior end.m.gtp4.e v4src_position 64" + ) c2.set_ipv6_route("eth2", "A2::2", "D3::/128") c2.set_ipv6_route("eth1", "A1::1", "C::/120") @@ -1371,13 +1428,15 @@ class Program(object): c3.set_ipv6_route("eth1", "A2::1", "C::/120") c4.set_ip_pgroute("pg0", "1.0.0.1", "172.20.0.1/32") - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IP(src="172.20.0.2", dst="172.20.0.1") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - GTPPDUSessionContainer(R=1, QFI=3) / - IPv6(src="2001::1", dst="2002::1") / - ICMPv6EchoRequest()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IP(src="172.20.0.2", dst="172.20.0.1") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / GTPPDUSessionContainer(R=1, QFI=3) + / IPv6(src="2001::1", dst="2002::1") + / ICMPv6EchoRequest() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1419,18 +1478,19 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface( local_ip="B::1/120", remote_ip="B::2", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") + c1.vppctl_exec("sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1447,12 +1507,14 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - IP(src="172.99.0.1", dst="172.99.0.2") / - ICMP()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / IP(src="172.99.0.1", dst="172.99.0.2") + / ICMP() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1494,18 +1556,19 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface( local_ip="B::1/120", remote_ip="B::2", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") + c1.vppctl_exec("sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1522,13 +1585,15 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - GTPPDUSessionContainer(type=1, R=1, QFI=3) / - IP(src="172.99.0.1", dst="172.99.0.2") / - ICMP()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / GTPPDUSessionContainer(type=1, R=1, QFI=3) + / IP(src="172.99.0.1", dst="172.99.0.2") + / ICMP() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1570,18 +1635,19 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface( local_ip="B::1/120", remote_ip="B::2", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") + c1.vppctl_exec("sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1598,10 +1664,12 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="echo_request", S=1, teid=200, seq=300)) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="echo_request", S=1, teid=200, seq=300) + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1643,18 +1711,19 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface( local_ip="B::1/120", remote_ip="B::2", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") + c1.vppctl_exec("sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1671,10 +1740,12 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="echo_response", S=1, teid=200, seq=300)) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="echo_response", S=1, teid=200, seq=300) + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1716,18 +1787,19 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface( local_ip="B::1/120", remote_ip="B::2", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") + c1.vppctl_exec("sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1744,12 +1816,15 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="error_indication", S=1, teid=200, seq=300)/ - IE_TEIDI(TEIDI=65535)/IE_GSNAddress(address="1.1.1.1")/ - IE_PrivateExtension(extention_value="z")) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="error_indication", S=1, teid=200, seq=300) + / IE_TEIDI(TEIDI=65535) + / IE_GSNAddress(address="1.1.1.1") + / IE_PrivateExtension(extention_value="z") + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1791,18 +1866,19 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface( local_ip="B::1/120", remote_ip="B::2", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") + c1.vppctl_exec("sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1819,12 +1895,14 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - IPv6(src="2001::1", dst="2002::1") / - ICMPv6EchoRequest()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / IPv6(src="2001::1", dst="2002::1") + / ICMPv6EchoRequest() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1866,18 +1944,19 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface( local_ip="B::1/120", remote_ip="B::2", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") + c1.vppctl_exec("sr localsid prefix D::/64 behavior end.m.gtp6.d.di D4::/64") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1894,13 +1973,15 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - GTPPDUSessionContainer(R=1, QFI=3) / - IPv6(src="2001::1", dst="2002::1") / - ICMPv6EchoRequest()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / GTPPDUSessionContainer(R=1, QFI=3) + / IPv6(src="2001::1", dst="2002::1") + / ICMPv6EchoRequest() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -1942,18 +2023,19 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface4( local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.d D4::/64") + c1.vppctl_exec("sr localsid prefix D::/64 behavior end.m.gtp6.d D4::/64") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -1971,12 +2053,14 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - IP(src="172.100.0.1", dst="172.200.0.1") / - ICMP()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / IP(src="172.100.0.1", dst="172.200.0.1") + / ICMP() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -2018,18 +2102,19 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface4( local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.d D4::/64") + c1.vppctl_exec("sr localsid prefix D::/64 behavior end.m.gtp6.d D4::/64") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -2047,13 +2132,15 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - GTPPDUSessionContainer(R=1, QFI=3) / - IP(src="172.100.0.1", dst="172.200.0.1") / - ICMP()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / GTPPDUSessionContainer(R=1, QFI=3) + / IP(src="172.100.0.1", dst="172.200.0.1") + / ICMP() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -2095,18 +2182,19 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface( local_ip="B::1/120", remote_ip="B::2", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.d D4::/64") + c1.vppctl_exec("sr localsid prefix D::/64 behavior end.m.gtp6.d D4::/64") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -2124,12 +2212,14 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - IPv6(src="2001::1", dst="2002::1") / - ICMPv6EchoRequest()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / IPv6(src="2001::1", dst="2002::1") + / ICMPv6EchoRequest() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -2171,18 +2261,19 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c4.pg_create_interface( local_ip="B::1/120", remote_ip="B::2", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D4:: next D2:: next D3::") - c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.d D4::/64") + c1.vppctl_exec("sr localsid prefix D::/64 behavior end.m.gtp6.d D4::/64") c2.vppctl_exec("sr localsid address D2:: behavior end") @@ -2200,13 +2291,15 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - GTPPDUSessionContainer(R=1, QFI=3) / - IPv6(src="2001::1", dst="2002::1") / - ICMPv6EchoRequest()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / GTPPDUSessionContainer(R=1, QFI=3) + / IPv6(src="2001::1", dst="2002::1") + / ICMPv6EchoRequest() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -2246,19 +2339,22 @@ class Program(object): local_ip="C::1/120", remote_ip="C::2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c1.pg_create_interface4_name( ifname="pg1", local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec( - "sr localsid prefix D::/64 behavior end.m.gtp6.dt46 fib-table 0 local-fib-table 0") + "sr localsid prefix D::/64 behavior end.m.gtp6.dt46 fib-table 0 local-fib-table 0" + ) c1.vppctl_exec("set ip neighbor pg1 1.0.0.1 aa:bb:cc:dd:ee:22") c1.set_ip_pgroute("pg1", "1.0.0.1", "172.200.0.1/32") @@ -2266,12 +2362,14 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IPv6(src="C::2", dst="D::2") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - IP(src="172.100.0.1", dst="172.200.0.1") / - ICMP()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IPv6(src="C::2", dst="D::2") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / IP(src="172.100.0.1", dst="172.200.0.1") + / ICMP() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -2310,14 +2408,16 @@ class Program(object): local_ip="172.16.0.1/30", remote_ip="172.16.0.2", local_mac="aa:bb:cc:dd:ee:01", - remote_mac="aa:bb:cc:dd:ee:02") + remote_mac="aa:bb:cc:dd:ee:02", + ) c1.pg_create_interface4_name( ifname="pg1", local_ip="1.0.0.2/30", remote_ip="1.0.0.1", local_mac="aa:bb:cc:dd:ee:11", - remote_mac="aa:bb:cc:dd:ee:22") + remote_mac="aa:bb:cc:dd:ee:22", + ) c1.vppctl_exec("set sr encaps source addr A1::1") c1.vppctl_exec("sr policy add bsid D5:: behavior t.m.gtp4.dt4 fib-table 0") @@ -2329,12 +2429,14 @@ class Program(object): print("Waiting...") time.sleep(30) - p = (Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") / - IP(src="172.20.0.2", dst="172.20.0.1") / - UDP(sport=2152, dport=2152) / - GTP_U_Header(gtp_type="g_pdu", teid=200) / - IP(src="172.100.0.1", dst="172.200.0.1") / - ICMP()) + p = ( + Ether(src="aa:bb:cc:dd:ee:02", dst="aa:bb:cc:dd:ee:01") + / IP(src="172.20.0.2", dst="172.20.0.1") + / UDP(sport=2152, dport=2152) + / GTP_U_Header(gtp_type="g_pdu", teid=200) + / IP(src="172.100.0.1", dst="172.200.0.1") + / ICMP() + ) print("Sending packet on {}:".format(c1.name)) p.show2() @@ -2360,17 +2462,21 @@ class Program(object): for i, name in enumerate(self.instance_names): name = self.get_name(name) - print("\t[{}] {} - {}".format( - i, name, - "running" if self.containers.get(name) else "missing")) + print( + "\t[{}] {} - {}".format( + i, name, "running" if self.containers.get(name) else "missing" + ) + ) print("Networks:") for i, name in enumerate(self.network_names): name = self.get_name(name) - print("\t[{}] {} - {}".format( - i, name, - "running" if self.networks.get(name) else "missing")) + print( + "\t[{}] {} - {}".format( + i, name, "running" if self.networks.get(name) else "missing" + ) + ) def build_image(self): print("VPP Path (build): {}".format(self.vpp_path)) @@ -2382,7 +2488,9 @@ class Program(object): system( "docker cp release-build:{}/vpp-package.tgz {}/".format( - self.vpp_path, self.vpp_path)) + self.vpp_path, self.vpp_path + ) + ) instance.rem() @@ -2408,39 +2516,30 @@ class Program(object): def get_args(): parser = ArgumentParser() - parser.add_argument("--verbose", choices=['error', 'debug', 'info']) + parser.add_argument("--verbose", choices=["error", "debug", "info"]) - parser.add_argument('--image', choices=['debug', 'release']) + parser.add_argument("--image", choices=["debug", "release"]) subparsers = parser.add_subparsers() - p1 = subparsers.add_parser( - "infra", help="Infrastructure related commands.") + p1 = subparsers.add_parser("infra", help="Infrastructure related commands.") p1.add_argument( - "op", - choices=[ - 'stop', - 'start', - 'status', - 'restart', - 'build', - 'release']) + "op", choices=["stop", "start", "status", "restart", "build", "release"] + ) p1.add_argument("--prefix") p1.add_argument("--image") p2 = subparsers.add_parser("cmd", help="Instance related commands.") - p2.add_argument("op", choices=['vppctl', 'bash']) + p2.add_argument("op", choices=["vppctl", "bash"]) p2.add_argument( - "index", - type=int, - help="Container instance index. (./runner.py infra status)") + "index", type=int, help="Container instance index. (./runner.py infra status)" + ) - p2.add_argument( - "--command", help="Only vppctl supports this optional argument.") + p2.add_argument("--command", help="Only vppctl supports this optional argument.") p3 = subparsers.add_parser("test", help="Test related commands.") @@ -2473,7 +2572,9 @@ def get_args(): "gtp6_ipv6", "gtp6_ipv6_5g", "gtp6_dt", - "gtp4_dt"]) + "gtp4_dt", + ], + ) args = parser.parse_args() if not hasattr(args, "op") or not args.op: @@ -2483,15 +2584,14 @@ def get_args(): return vars(args) -def main(op=None, prefix=None, verbose=None, - image=None, index=None, command=None): +def main(op=None, prefix=None, verbose=None, image=None, index=None, command=None): if verbose: basicConfig(level=verbose_levels[verbose]) - if image == 'release': + if image == "release": image = "srv6m-release-image" - elif image == 'debug': + elif image == "debug": image = "srv6m-image" else: image = "srv6m-image" @@ -2501,23 +2601,23 @@ def main(op=None, prefix=None, verbose=None, program = Program(image, prefix) try: - if op == 'build': + if op == "build": program.build_image() - elif op == 'release': + elif op == "release": program.release_image() - elif op == 'stop': + elif op == "stop": program.stop_containers() - elif op == 'start': + elif op == "start": program.start_containers() - elif op == 'status': + elif op == "status": program.status_containers() - elif op == 'vppctl': + elif op == "vppctl": program.vppctl(index, command) - elif op == 'bash': + elif op == "bash": program.bash(index) - elif op == 'ping': + elif op == "ping": program.test_ping() - elif op == 'srv6': + elif op == "srv6": program.test_srv6() # elif op == 'tmap': # program.test_tmap() @@ -2527,47 +2627,47 @@ def main(op=None, prefix=None, verbose=None, # program.test_tmap_ipv6() # elif op == 'tmap_ipv6_5g': # program.test_tmap_ipv6_5g() - elif op == 'gtp4': + elif op == "gtp4": program.test_gtp4() - elif op == 'gtp4_usid': + elif op == "gtp4_usid": program.test_gtp4_usid() - elif op == 'gtp4_5g': + elif op == "gtp4_5g": program.test_gtp4_5g() - elif op == 'gtp4_echo': + elif op == "gtp4_echo": program.test_gtp4_echo() - elif op == 'gtp4_reply': + elif op == "gtp4_reply": program.test_gtp4_reply() - elif op == 'gtp4_error': + elif op == "gtp4_error": program.test_gtp4_error() - elif op == 'gtp4_ipv6': + elif op == "gtp4_ipv6": program.test_gtp4_ipv6() - elif op == 'gtp4_ipv6_5g': + elif op == "gtp4_ipv6_5g": program.test_gtp4_ipv6_5g() - elif op == 'gtp6_drop_in': + elif op == "gtp6_drop_in": program.test_gtp6_drop_in() - elif op == 'gtp6_drop_in_5g': + elif op == "gtp6_drop_in_5g": program.test_gtp6_drop_in_5g() - elif op == 'gtp6_drop_in_echo': + elif op == "gtp6_drop_in_echo": program.test_gtp6_drop_in_echo() - elif op == 'gtp6_drop_in_reply': + elif op == "gtp6_drop_in_reply": program.test_gtp6_drop_in_reply() - elif op == 'gtp6_drop_in_error': + elif op == "gtp6_drop_in_error": program.test_gtp6_drop_in_error() - elif op == 'gtp6_drop_in_ipv6': + elif op == "gtp6_drop_in_ipv6": program.test_gtp6_drop_in_ipv6() - elif op == 'gtp6_drop_in_ipv6_5g': + elif op == "gtp6_drop_in_ipv6_5g": program.test_gtp6_drop_in_ipv6_5g() - elif op == 'gtp6': + elif op == "gtp6": program.test_gtp6() - elif op == 'gtp6_5g': + elif op == "gtp6_5g": program.test_gtp6_5g() - elif op == 'gtp6_ipv6': + elif op == "gtp6_ipv6": program.test_gtp6_ipv6() - elif op == 'gtp6_ipv6_5g': + elif op == "gtp6_ipv6_5g": program.test_gtp6_ipv6_5g() - elif op == 'gtp6_dt': + elif op == "gtp6_dt": program.test_gtp6_dt() - elif op == 'gtp4_dt': + elif op == "gtp4_dt": program.test_gtp4_dt() except Exception: diff --git a/src/scripts/fts.py b/src/scripts/fts.py index f2b877fb2c7..e8ff477c788 100755 --- a/src/scripts/fts.py +++ b/src/scripts/fts.py @@ -21,25 +21,28 @@ schema = { "name": {"type": "string"}, "description": {"type": "string"}, "maintainer": {"$ref": "#/definitions/maintainers"}, - "state": {"type": "string", - "enum": ["production", "experimental", "development"]}, + "state": { + "type": "string", + "enum": ["production", "experimental", "development"], + }, "features": {"$ref": "#/definitions/features"}, "missing": {"$ref": "#/definitions/features"}, - "properties": {"type": "array", - "items": {"type": "string", - "enum": ["API", "CLI", "STATS", - "MULTITHREAD"]}, - }, + "properties": { + "type": "array", + "items": {"type": "string", "enum": ["API", "CLI", "STATS", "MULTITHREAD"]}, + }, }, "additionalProperties": False, "definitions": { "maintainers": { - "anyof": [{ - "type": "array", - "items": {"type": "string"}, - "minItems": 1, - }, - {"type": "string"}], + "anyof": [ + { + "type": "array", + "items": {"type": "string"}, + "minItems": 1, + }, + {"type": "string"}, + ], }, "featureobject": { "type": "object", @@ -49,9 +52,12 @@ schema = { }, "features": { "type": "array", - "items": {"anyOf": [{"$ref": "#/definitions/featureobject"}, - {"type": "string"}, - ]}, + "items": { + "anyOf": [ + {"$ref": "#/definitions/featureobject"}, + {"type": "string"}, + ] + }, "minItems": 1, }, }, @@ -59,14 +65,15 @@ schema = { DEFAULT_REPO_LINK = "https://github.com/FDio/vpp/blob/master/" + def filelist_from_git_status(): filelist = [] - git_status = 'git status --porcelain */FEATURE*.yaml' + git_status = "git status --porcelain */FEATURE*.yaml" rv = run(git_status.split(), stdout=PIPE, stderr=PIPE) if rv.returncode != 0: sys.exit(rv.returncode) - for l in rv.stdout.decode('ascii').split('\n'): + for l in rv.stdout.decode("ascii").split("\n"): if len(l): filelist.append(l.split()[1]) return filelist @@ -74,24 +81,26 @@ def filelist_from_git_status(): def filelist_from_git_ls(): filelist = [] - git_ls = 'git ls-files :(top)*/FEATURE*.yaml' + git_ls = "git ls-files :(top)*/FEATURE*.yaml" rv = run(git_ls.split(), stdout=PIPE, stderr=PIPE) if rv.returncode != 0: sys.exit(rv.returncode) - for l in rv.stdout.decode('ascii').split('\n'): + for l in rv.stdout.decode("ascii").split("\n"): if len(l): filelist.append(l) return filelist + def version_from_git(): - git_describe = 'git describe' + git_describe = "git describe" rv = run(git_describe.split(), stdout=PIPE, stderr=PIPE) if rv.returncode != 0: sys.exit(rv.returncode) - return rv.stdout.decode('ascii').split('\n')[0] + return rv.stdout.decode("ascii").split("\n")[0] + -class MarkDown(): +class MarkDown: _dispatch = {} def __init__(self, stream): @@ -101,102 +110,115 @@ class MarkDown(): def print_maintainer(self, o): write = self.stream.write if type(o) is list: - write('Maintainers: ' + - ', '.join('{m}'.format(m=m) for m in - o) + ' \n') + write("Maintainers: " + ", ".join("{m}".format(m=m) for m in o) + " \n") else: - write('Maintainer: {o} \n'.format(o=o)) + write("Maintainer: {o} \n".format(o=o)) - _dispatch['maintainer'] = print_maintainer + _dispatch["maintainer"] = print_maintainer def print_features(self, o, indent=0): write = self.stream.write for f in o: - indentstr = ' ' * indent + indentstr = " " * indent if type(f) is dict: for k, v in f.items(): - write('{indentstr}- {k}\n'.format(indentstr=indentstr, k=k)) + write("{indentstr}- {k}\n".format(indentstr=indentstr, k=k)) self.print_features(v, indent + 2) else: - write('{indentstr}- {f}\n'.format(indentstr=indentstr, f=f)) - write('\n') - _dispatch['features'] = print_features + write("{indentstr}- {f}\n".format(indentstr=indentstr, f=f)) + write("\n") + + _dispatch["features"] = print_features def print_markdown_header(self, o): write = self.stream.write - write('## {o}\n'.format(o=o)) - _dispatch['markdown_header'] = print_markdown_header + write("## {o}\n".format(o=o)) + + _dispatch["markdown_header"] = print_markdown_header def print_name(self, o): write = self.stream.write - write('### {o}\n'.format(o=o)) + write("### {o}\n".format(o=o)) self.toc.append(o) - _dispatch['name'] = print_name + + _dispatch["name"] = print_name def print_description(self, o): write = self.stream.write - write('\n{o}\n\n'.format(o=o)) - _dispatch['description'] = print_description + write("\n{o}\n\n".format(o=o)) + + _dispatch["description"] = print_description def print_state(self, o): write = self.stream.write - write('Feature maturity level: {o} \n'.format(o=o)) - _dispatch['state'] = print_state + write("Feature maturity level: {o} \n".format(o=o)) + + _dispatch["state"] = print_state def print_properties(self, o): write = self.stream.write - write('Supports: {s} \n'.format(s=" ".join(o))) - _dispatch['properties'] = print_properties + write("Supports: {s} \n".format(s=" ".join(o))) + + _dispatch["properties"] = print_properties def print_missing(self, o): write = self.stream.write - write('\nNot yet implemented: \n') + write("\nNot yet implemented: \n") self.print_features(o) - _dispatch['missing'] = print_missing + + _dispatch["missing"] = print_missing def print_code(self, o): write = self.stream.write - write('Source Code: [{o}]({o}) \n'.format(o=o)) - _dispatch['code'] = print_code + write("Source Code: [{o}]({o}) \n".format(o=o)) + + _dispatch["code"] = print_code def print(self, t, o): write = self.stream.write if t in self._dispatch: - self._dispatch[t](self, o,) + self._dispatch[t]( + self, + o, + ) else: - write('NOT IMPLEMENTED: {t}\n') + write("NOT IMPLEMENTED: {t}\n") + def output_toc(toc, stream): write = stream.write - write('# VPP Supported Features\n') + write("# VPP Supported Features\n") for t in toc: - ref = t.lower().replace(' ', '-') - write('[{t}](#{ref}) \n'.format(t=t, ref=ref)) + ref = t.lower().replace(" ", "-") + write("[{t}](#{ref}) \n".format(t=t, ref=ref)) + def featuresort(k): - return k[1]['name'] + return k[1]["name"] + def featurelistsort(k): orderedfields = { - 'name': 0, - 'maintainer': 1, - 'description': 2, - 'features': 3, - 'state': 4, - 'properties': 5, - 'missing': 6, - 'code': 7, + "name": 0, + "maintainer": 1, + "description": 2, + "features": 3, + "state": 4, + "properties": 5, + "missing": 6, + "code": 7, } return orderedfields[k[0]] + def output_markdown(features, fields, notfields, repository_url): stream = StringIO() m = MarkDown(stream) - m.print('markdown_header', 'Feature Details:') + m.print("markdown_header", "Feature Details:") for path, featuredef in sorted(features.items(), key=featuresort): codeurl = urllib.parse.urljoin(repository_url, os.path.dirname(path)) - featuredef['code'] = codeurl + featuredef["code"] = codeurl for k, v in sorted(featuredef.items(), key=featurelistsort): if notfields: if k not in notfields: @@ -211,24 +233,45 @@ def output_markdown(features, fields, notfields, repository_url): output_toc(m.toc, tocstream) return tocstream, stream + def main(): - parser = argparse.ArgumentParser(description='VPP Feature List.') - parser.add_argument('--validate', dest='validate', action='store_true', - help='validate the FEATURE.yaml file') - parser.add_argument("--repolink", metavar="repolink", default=DEFAULT_REPO_LINK, - help="Link to public repository [%s]" % - DEFAULT_REPO_LINK) - parser.add_argument('--git-status', dest='git_status', action='store_true', - help='Get filelist from git status') - parser.add_argument('--all', dest='all', action='store_true', - help='Validate all files in repository') - parser.add_argument('--markdown', dest='markdown', action='store_true', - help='Output feature table in markdown') - parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), - default=sys.stdin) + parser = argparse.ArgumentParser(description="VPP Feature List.") + parser.add_argument( + "--validate", + dest="validate", + action="store_true", + help="validate the FEATURE.yaml file", + ) + parser.add_argument( + "--repolink", + metavar="repolink", + default=DEFAULT_REPO_LINK, + help="Link to public repository [%s]" % DEFAULT_REPO_LINK, + ) + parser.add_argument( + "--git-status", + dest="git_status", + action="store_true", + help="Get filelist from git status", + ) + parser.add_argument( + "--all", + dest="all", + action="store_true", + help="Validate all files in repository", + ) + parser.add_argument( + "--markdown", + dest="markdown", + action="store_true", + help="Output feature table in markdown", + ) + parser.add_argument( + "infile", nargs="?", type=argparse.FileType("r"), default=sys.stdin + ) group = parser.add_mutually_exclusive_group() - group.add_argument('--include', help='List of fields to include') - group.add_argument('--exclude', help='List of fields to exclude') + group.add_argument("--include", help="List of fields to include") + group.add_argument("--exclude", help="List of fields to exclude") args = parser.parse_args() features = {} @@ -240,11 +283,11 @@ def main(): filelist = args.infile if args.include: - fields = args.include.split(',') + fields = args.include.split(",") else: fields = [] if args.exclude: - notfields = args.exclude.split(',') + notfields = args.exclude.split(",") else: notfields = [] @@ -252,13 +295,15 @@ def main(): featurefile = featurefile.rstrip() # Load configuration file - with open(featurefile, encoding='utf-8') as f: + with open(featurefile, encoding="utf-8") as f: cfg = yaml.load(f, Loader=yaml.SafeLoader) try: validate(instance=cfg, schema=schema) except exceptions.ValidationError: - print('File does not validate: {featurefile}' \ - .format(featurefile=featurefile), file=sys.stderr) + print( + "File does not validate: {featurefile}".format(featurefile=featurefile), + file=sys.stderr, + ) raise features[featurefile] = cfg @@ -270,5 +315,5 @@ def main(): stream.close() -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/src/scripts/host-stack/cc_plots.py b/src/scripts/host-stack/cc_plots.py index 20f5bd6a3f1..f0296af7223 100755 --- a/src/scripts/host-stack/cc_plots.py +++ b/src/scripts/host-stack/cc_plots.py @@ -6,214 +6,242 @@ import argparse import matplotlib.pyplot as plt from matplotlib.lines import Line2D -class Point(): + +class Point: "CC event" + def __init__(self, x, y): self.x = x self.y = y + def listx(points): - return list(map(lambda pt: pt.x, points)) + return list(map(lambda pt: pt.x, points)) + def listy(points): - return list(map(lambda pt: pt.y, points)) + return list(map(lambda pt: pt.y, points)) + def plot_data(d): - plt.figure(1) - - cwndx = listx(d["cwnd"]) - cwndy = listy(d["cwnd"]) - congx = listx(d["congestion"]) - congy = listy(d["congestion"]) - rcvrdx = listx(d["recovered"]) - rcvrdy = listy(d["recovered"]) - rxttx = listx(d["rxtTimeout"]) - rxtty = listy(d["rxtTimeout"]) - - # cwnd/ssthresh/cc events - plt.subplot(311) - plt.title("cwnd/ssthresh") - pcwnd = plt.plot(cwndx, cwndy, 'r') - psst = plt.plot(cwndx, d["ssthresh"], 'y-') - pcong = plt.plot(congx, congy,'yo') - precov = plt.plot(rcvrdx, rcvrdy,'co') - prxtt = plt.plot(rxttx, rxtty,'mo') - - marker1 = Line2D(range(1), range(1), color="r") - marker2 = Line2D(range(1), range(1), color="y") - marker3 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="y") - marker4 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="c") - marker5 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="m") - plt.legend((marker1, marker2, marker3, marker4, marker5), - ('cwnd', 'ssthresh', 'congestion', 'recovered', 'rxt-timeout'), - loc=4) - axes = plt.gca() - axes.set_ylim([-20e4, max(cwndy) + 20e4]) - - # snd variables - plt.subplot(312) - plt.title("cc variables") - plt.plot(cwndx, d["space"], 'g-', markersize=1) - plt.plot(cwndx, d["flight"], 'b-', markersize=1) - plt.plot(cwndx, d["sacked"], 'm:', markersize=1) - plt.plot(cwndx, d["lost"], 'y:', markersize=1) - plt.plot(cwndx, d["cc-space"], 'k:', markersize=1) - plt.plot(cwndx, cwndy, 'ro', markersize=2) - - plt.plot(congx, congy, 'y^', markersize=10, markerfacecolor="y") - plt.plot(rcvrdx, rcvrdy, 'c^', markersize=10, markerfacecolor="c") - plt.plot(rxttx, rxtty, 'm^', markersize=10, markerfacecolor="m") - - #plt.plot(cwndx, d["snd_wnd"], 'ko', markersize=1) - plt.legend(("snd-space", "flight", "sacked", "lost", "cc-space", "cwnd", - "congestion", "recovered", "rxt-timeout"), - loc=1) - - # rto/srrt/rttvar - plt.subplot(313) - plt.title("rtt") - plt.plot(cwndx, d["srtt"], 'g-') - plt.plot(cwndx, [x/1000 for x in d["mrtt-us"]], 'r-') - plt.plot(cwndx, d["rttvar"], 'b-') - plt.legend(["srtt", "mrtt-us", "rttvar"]) - axes = plt.gca() - #plt.plot(cwndx, rto, 'r-') - #axes.set_ylim([0, int(max(rto[2:len(rto)])) + 50]) - - # show - plt.show() - -def find_pattern(file_path,session_idx): + plt.figure(1) + + cwndx = listx(d["cwnd"]) + cwndy = listy(d["cwnd"]) + congx = listx(d["congestion"]) + congy = listy(d["congestion"]) + rcvrdx = listx(d["recovered"]) + rcvrdy = listy(d["recovered"]) + rxttx = listx(d["rxtTimeout"]) + rxtty = listy(d["rxtTimeout"]) + + # cwnd/ssthresh/cc events + plt.subplot(311) + plt.title("cwnd/ssthresh") + pcwnd = plt.plot(cwndx, cwndy, "r") + psst = plt.plot(cwndx, d["ssthresh"], "y-") + pcong = plt.plot(congx, congy, "yo") + precov = plt.plot(rcvrdx, rcvrdy, "co") + prxtt = plt.plot(rxttx, rxtty, "mo") + + marker1 = Line2D(range(1), range(1), color="r") + marker2 = Line2D(range(1), range(1), color="y") + marker3 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="y") + marker4 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="c") + marker5 = Line2D(range(1), range(1), color="w", marker="o", markerfacecolor="m") + plt.legend( + (marker1, marker2, marker3, marker4, marker5), + ("cwnd", "ssthresh", "congestion", "recovered", "rxt-timeout"), + loc=4, + ) + axes = plt.gca() + axes.set_ylim([-20e4, max(cwndy) + 20e4]) + + # snd variables + plt.subplot(312) + plt.title("cc variables") + plt.plot(cwndx, d["space"], "g-", markersize=1) + plt.plot(cwndx, d["flight"], "b-", markersize=1) + plt.plot(cwndx, d["sacked"], "m:", markersize=1) + plt.plot(cwndx, d["lost"], "y:", markersize=1) + plt.plot(cwndx, d["cc-space"], "k:", markersize=1) + plt.plot(cwndx, cwndy, "ro", markersize=2) + + plt.plot(congx, congy, "y^", markersize=10, markerfacecolor="y") + plt.plot(rcvrdx, rcvrdy, "c^", markersize=10, markerfacecolor="c") + plt.plot(rxttx, rxtty, "m^", markersize=10, markerfacecolor="m") + + # plt.plot(cwndx, d["snd_wnd"], 'ko', markersize=1) + plt.legend( + ( + "snd-space", + "flight", + "sacked", + "lost", + "cc-space", + "cwnd", + "congestion", + "recovered", + "rxt-timeout", + ), + loc=1, + ) + + # rto/srrt/rttvar + plt.subplot(313) + plt.title("rtt") + plt.plot(cwndx, d["srtt"], "g-") + plt.plot(cwndx, [x / 1000 for x in d["mrtt-us"]], "r-") + plt.plot(cwndx, d["rttvar"], "b-") + plt.legend(["srtt", "mrtt-us", "rttvar"]) + axes = plt.gca() + # plt.plot(cwndx, rto, 'r-') + # axes.set_ylim([0, int(max(rto[2:len(rto)])) + 50]) + + # show + plt.show() + + +def find_pattern(file_path, session_idx): is_active_open = 1 listener_pattern = "l\[\d\]" - if (is_active_open): - initial_pattern = "\[\d\](\.\d+:\d+\->\.\d+:\d+)\s+open:\s" + if is_active_open: + initial_pattern = "\[\d\](\.\d+:\d+\->\.\d+:\d+)\s+open:\s" else: - initial_pattern = "\[\d\](\.\d+:\d+\->\.\d+:\d+)\s" + initial_pattern = "\[\d\](\.\d+:\d+\->\.\d+:\d+)\s" idx = 0 - f = open(file_path, 'r') + f = open(file_path, "r") for line in f: - # skip listener lines (server) - if (re.search(listener_pattern, line) != None): - continue - match = re.search(initial_pattern, line) - if (match == None): - continue - if (idx < session_idx): - idx += 1 - continue - filter_pattern = str(match.group(1)) + "\s+(.+)" - print ("pattern is %s" % filter_pattern) - f.close() - return filter_pattern - raise Exception ("Could not find initial pattern") + # skip listener lines (server) + if re.search(listener_pattern, line) != None: + continue + match = re.search(initial_pattern, line) + if match == None: + continue + if idx < session_idx: + idx += 1 + continue + filter_pattern = str(match.group(1)) + "\s+(.+)" + print("pattern is %s" % filter_pattern) + f.close() + return filter_pattern + raise Exception("Could not find initial pattern") + def compute_time(min, sec, msec): - return int(min)*60 + int(sec) + int(msec)/1000.0 + return int(min) * 60 + int(sec) + int(msec) / 1000.0 + def run(file_path, session_idx): filter_sessions = 1 filter_pattern = "" patterns = { - "time" : "^\d+:(\d+):(\d+):(\d+):\d+", - "listener" : "l\[\d\]", - "cc" : "cwnd (\d+) flight (\d+) space (\d+) ssthresh (\d+) snd_wnd (\d+)", - "cc-snd" : "cc_space (\d+) sacked (\d+) lost (\d+)", - "rtt" : "rto (\d+) srtt (\d+) mrtt-us (\d+) rttvar (\d+)", - "rxtt" : "rxt-timeout", - "congestion": "congestion", - "recovered" : "recovered", + "time": "^\d+:(\d+):(\d+):(\d+):\d+", + "listener": "l\[\d\]", + "cc": "cwnd (\d+) flight (\d+) space (\d+) ssthresh (\d+) snd_wnd (\d+)", + "cc-snd": "cc_space (\d+) sacked (\d+) lost (\d+)", + "rtt": "rto (\d+) srtt (\d+) mrtt-us (\d+) rttvar (\d+)", + "rxtt": "rxt-timeout", + "congestion": "congestion", + "recovered": "recovered", } d = { - "cwnd" : [], - "space" : [], - "flight" : [], - "ssthresh" : [], - "snd_wnd" : [], - "cc-space" : [], - "lost" : [], - "sacked" : [], - "rto" : [], - "srtt" : [], - "mrtt-us" : [], - "rttvar" : [], - "rxtTimeout" : [], - "congestion" : [], - "recovered" : [], + "cwnd": [], + "space": [], + "flight": [], + "ssthresh": [], + "snd_wnd": [], + "cc-space": [], + "lost": [], + "sacked": [], + "rto": [], + "srtt": [], + "mrtt-us": [], + "rttvar": [], + "rxtTimeout": [], + "congestion": [], + "recovered": [], } - if (filter_sessions): + if filter_sessions: filter_pattern = find_pattern(file_path, session_idx) - f = open(file_path, 'r') + f = open(file_path, "r") stats_index = 0 start_time = 0 for line in f: # skip listener lines (server) - if (re.search(patterns["listener"], line) != None): + if re.search(patterns["listener"], line) != None: continue # filter sessions - if (filter_sessions): + if filter_sessions: match = re.search(filter_pattern, line) - if (match == None): + if match == None: continue original_line = line line = match.group(1) - match = re.search (patterns["time"], original_line) - if (match == None): - print "something went wrong! no time!" - continue - time = compute_time (match.group(1), match.group(2), match.group(3)) - if (start_time == 0): - start_time = time + match = re.search(patterns["time"], original_line) + if match == None: + print("something went wrong! no time!") + continue + time = compute_time(match.group(1), match.group(2), match.group(3)) + if start_time == 0: + start_time = time time = time - start_time match = re.search(patterns["cc"], line) - if (match != None): - d["cwnd"].append(Point(time, int(match.group(1)))) - d["flight"].append(int(match.group(2))) - d["space"].append(int(match.group(3))) - d["ssthresh"].append(int(match.group(4))) - d["snd_wnd"].append(int(match.group(5))) - stats_index += 1 - continue + if match != None: + d["cwnd"].append(Point(time, int(match.group(1)))) + d["flight"].append(int(match.group(2))) + d["space"].append(int(match.group(3))) + d["ssthresh"].append(int(match.group(4))) + d["snd_wnd"].append(int(match.group(5))) + stats_index += 1 + continue match = re.search(patterns["cc-snd"], line) - if (match != None): - d["cc-space"].append(int(match.group(1))) - d["sacked"].append(int(match.group(2))) - d["lost"].append(int(match.group(3))) + if match != None: + d["cc-space"].append(int(match.group(1))) + d["sacked"].append(int(match.group(2))) + d["lost"].append(int(match.group(3))) match = re.search(patterns["rtt"], line) - if (match != None): - d["rto"].append(int(match.group(1))) - d["srtt"].append(int(match.group(2))) - d["mrtt-us"].append(int(match.group(3))) - d["rttvar"].append(int(match.group(4))) - if (stats_index == 0): - continue + if match != None: + d["rto"].append(int(match.group(1))) + d["srtt"].append(int(match.group(2))) + d["mrtt-us"].append(int(match.group(3))) + d["rttvar"].append(int(match.group(4))) + if stats_index == 0: + continue match = re.search(patterns["rxtt"], line) - if (match != None): - d["rxtTimeout"].append(Point(time, d["cwnd"][stats_index - 1].y + 1e4)) - continue + if match != None: + d["rxtTimeout"].append(Point(time, d["cwnd"][stats_index - 1].y + 1e4)) + continue match = re.search(patterns["congestion"], line) - if (match != None): - d["congestion"].append(Point(time, d["cwnd"][stats_index - 1].y - 1e4)) - continue + if match != None: + d["congestion"].append(Point(time, d["cwnd"][stats_index - 1].y - 1e4)) + continue match = re.search(patterns["recovered"], line) - if (match != None): - d["recovered"].append(Point(time, d["cwnd"][stats_index - 1].y)) - continue + if match != None: + d["recovered"].append(Point(time, d["cwnd"][stats_index - 1].y)) + continue plot_data(d) + if __name__ == "__main__": parser = argparse.ArgumentParser(description="Plot tcp cc logs") - parser.add_argument('-f', action='store', dest='file', required=True, - help="elog file in txt format") - parser.add_argument('-s', action='store', dest='session_index', default=0, - help="session index for which to plot cc logs" ) + parser.add_argument( + "-f", action="store", dest="file", required=True, help="elog file in txt format" + ) + parser.add_argument( + "-s", + action="store", + dest="session_index", + default=0, + help="session index for which to plot cc logs", + ) results = parser.parse_args() run(results.file, int(results.session_index)) diff --git a/src/scripts/vnet/uri/dummy_app.py b/src/scripts/vnet/uri/dummy_app.py index d96a378a193..7fab2d766ad 100755 --- a/src/scripts/vnet/uri/dummy_app.py +++ b/src/scripts/vnet/uri/dummy_app.py @@ -5,34 +5,41 @@ import sys import time import argparse -# action can be reflect or drop +# action can be reflect or drop action = "drop" test = 0 -def test_data (data, n_rcvd): - n_read = len (data); + +def test_data(data, n_rcvd): + n_read = len(data) for i in range(n_read): - expected = (n_rcvd + i) & 0xff - byte_got = ord (data[i]) - if (byte_got != expected): - print("Difference at byte {}. Expected {} got {}" - .format(n_rcvd + i, expected, byte_got)) + expected = (n_rcvd + i) & 0xFF + byte_got = ord(data[i]) + if byte_got != expected: + print( + "Difference at byte {}. Expected {} got {}".format( + n_rcvd + i, expected, byte_got + ) + ) return n_read -def handle_connection (connection, client_address): + +def handle_connection(connection, client_address): print("Received connection from {}".format(repr(client_address))) n_rcvd = 0 try: while True: data = connection.recv(4096) if not data: - break; - if (test == 1): - n_rcvd += test_data (data, n_rcvd) - if (action != "drop"): + break + if test == 1: + n_rcvd += test_data(data, n_rcvd) + if action != "drop": connection.sendall(data) finally: connection.close() + + def run_tcp_server(ip, port): print("Starting TCP server {}:{}".format(repr(ip), repr(port))) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -42,7 +49,9 @@ def run_tcp_server(ip, port): sock.listen(1) while True: connection, client_address = sock.accept() - handle_connection (connection, client_address) + handle_connection(connection, client_address) + + def run_udp_server(ip, port): print("Starting UDP server {}:{}".format(repr(ip), repr(port))) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -51,22 +60,25 @@ def run_udp_server(ip, port): sock.bind(server_address) while True: data, addr = sock.recvfrom(4096) - if (action != "drop"): - #snd_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - sock.sendto (data, addr) + if action != "drop": + # snd_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.sendto(data, addr) + def run_server(ip, port, proto): - if (proto == "tcp"): + if proto == "tcp": run_tcp_server(ip, port) - elif (proto == "udp"): + elif proto == "udp": run_udp_server(ip, port) + def prepare_data(power): buf = [] - for i in range (0, pow(2, power)): - buf.append(i & 0xff) + for i in range(0, pow(2, power)): + buf.append(i & 0xFF) return bytearray(buf) + def run_tcp_client(ip, port): print("Starting TCP client {}:{}".format(repr(ip), repr(port))) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -75,28 +87,33 @@ def run_tcp_client(ip, port): data = prepare_data(16) n_rcvd = 0 - n_sent = len (data) + n_sent = len(data) try: sock.sendall(data) timeout = time.time() + 2 while n_rcvd < n_sent and time.time() < timeout: tmp = sock.recv(1500) - tmp = bytearray (tmp) + tmp = bytearray(tmp) n_read = len(tmp) for i in range(n_read): - if (data[n_rcvd + i] != tmp[i]): - print("Difference at byte {}. Sent {} got {}" - .format(n_rcvd + i, data[n_rcvd + i], tmp[i])) + if data[n_rcvd + i] != tmp[i]: + print( + "Difference at byte {}. Sent {} got {}".format( + n_rcvd + i, data[n_rcvd + i], tmp[i] + ) + ) n_rcvd += n_read - if (n_rcvd < n_sent or n_rcvd > n_sent): + if n_rcvd < n_sent or n_rcvd > n_sent: print("Sent {} and got back {}".format(n_sent, n_rcvd)) else: - print("Got back what we've sent!!"); + print("Got back what we've sent!!") finally: sock.close() + + def run_udp_client(ip, port): print("Starting UDP client {}:{}".format(repr(ip), repr(port))) n_packets = 100 @@ -104,38 +121,43 @@ def run_udp_client(ip, port): server_address = (ip, int(port)) data = prepare_data(10) try: - for i in range (0, n_packets): + for i in range(0, n_packets): sock.sendto(data, server_address) finally: sock.close() + + def run_client(ip, port, proto): - if (proto == "tcp"): + if proto == "tcp": run_tcp_client(ip, port) - elif (proto == "udp"): + elif proto == "udp": run_udp_client(ip, port) + + def run(mode, ip, port, proto): - if (mode == "server"): - run_server (ip, port, proto) - elif (mode == "client"): - run_client (ip, port, proto) + if mode == "server": + run_server(ip, port, proto) + elif mode == "client": + run_client(ip, port, proto) else: raise Exception("Unknown mode. Only client and server supported") + if __name__ == "__main__": parser = argparse.ArgumentParser() - parser.add_argument('-m', action='store', dest='mode') - parser.add_argument('-i', action='store', dest='ip') - parser.add_argument('-p', action='store', dest='port') - parser.add_argument('-proto', action='store', dest='proto') - parser.add_argument('-a', action='store', dest='action') - parser.add_argument('-t', action='store', dest='test') + parser.add_argument("-m", action="store", dest="mode") + parser.add_argument("-i", action="store", dest="ip") + parser.add_argument("-p", action="store", dest="port") + parser.add_argument("-proto", action="store", dest="proto") + parser.add_argument("-a", action="store", dest="action") + parser.add_argument("-t", action="store", dest="test") results = parser.parse_args() action = results.action test = results.test run(results.mode, results.ip, results.port, results.proto) - #if (len(sys.argv)) < 4: + # if (len(sys.argv)) < 4: # raise Exception("Usage: ./dummy_app <mode> <ip> <port> [<action> <test>]") - #if (len(sys.argv) == 6): + # if (len(sys.argv) == 6): # action = sys.argv[4] # test = int(sys.argv[5]) - #run (sys.argv[1], sys.argv[2], int(sys.argv[3])) + # run (sys.argv[1], sys.argv[2], int(sys.argv[3])) diff --git a/src/tools/vppapigen/generate_go.py b/src/tools/vppapigen/generate_go.py index 1fb53c715e2..fa53bc3dca3 100755 --- a/src/tools/vppapigen/generate_go.py +++ b/src/tools/vppapigen/generate_go.py @@ -17,10 +17,12 @@ import sys def get_go_version(go_root): # Returns version of the installed Go - p = subprocess.Popen(["./go", "version"], - cwd=go_root + "/bin", - stdout=subprocess.PIPE, - universal_newlines=True, ) + p = subprocess.Popen( + ["./go", "version"], + cwd=go_root + "/bin", + stdout=subprocess.PIPE, + universal_newlines=True, + ) output, _ = p.communicate() output_fmt = output.replace("go version go", "", 1) @@ -29,10 +31,12 @@ def get_go_version(go_root): # Returns version of the installed binary API generator def get_binapi_gen_version(go_path): - p = subprocess.Popen(["./binapi-generator", "-version"], - cwd=go_path + "/bin", - stdout=subprocess.PIPE, - universal_newlines=True, ) + p = subprocess.Popen( + ["./binapi-generator", "-version"], + cwd=go_path + "/bin", + stdout=subprocess.PIPE, + universal_newlines=True, + ) output, _ = p.communicate() output_fmt = output.replace("govpp", "", 1) @@ -45,11 +49,12 @@ def install_golang(go_root): go_bin = go_root + "/bin/go" if os.path.exists(go_bin) and os.path.isfile(go_bin): - print('Go ' + get_go_version(go_root) + ' is already installed') + print("Go " + get_go_version(go_root) + " is already installed") return - filename = requests.get( - 'https://golang.org/VERSION?m=text').text + ".linux-amd64.tar.gz" + filename = ( + requests.get("https://golang.org/VERSION?m=text").text + ".linux-amd64.tar.gz" + ) url = "https://dl.google.com/go/" + filename print("Go binary not found, installing the latest version...") @@ -61,13 +66,13 @@ def install_golang(go_root): print("Aborting...") exit(1) - go_folders = ['src', 'pkg', 'bin'] + go_folders = ["src", "pkg", "bin"] for f in go_folders: if not os.path.exists(os.path.join(go_root, f)): os.makedirs(os.path.join(go_root, f)) r = requests.get(url) - with open("/tmp/" + filename, 'wb') as f: + with open("/tmp/" + filename, "wb") as f: f.write(r.content) go_tf = tarfile.open("/tmp/" + filename) @@ -78,32 +83,30 @@ def install_golang(go_root): go_tf.close() os.remove("/tmp/" + filename) - print('Go ' + get_go_version(go_root) + ' was installed') + print("Go " + get_go_version(go_root) + " was installed") # Installs latest binary API generator def install_binapi_gen(c, go_root, go_path): - os.environ['GO111MODULE'] = "on" - if (os.path.exists(go_root + "/bin/go") and - os.path.isfile(go_root + "/bin/go")): - p = subprocess.Popen(["./go", "get", - "git.fd.io/govpp.git/cmd/binapi-generator@" + c], - cwd=go_root + "/bin", - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True, ) + os.environ["GO111MODULE"] = "on" + if os.path.exists(go_root + "/bin/go") and os.path.isfile(go_root + "/bin/go"): + p = subprocess.Popen( + ["./go", "get", "git.fd.io/govpp.git/cmd/binapi-generator@" + c], + cwd=go_root + "/bin", + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ) _, error = p.communicate() if p.returncode != 0: - print("binapi generator installation failed: %d %s" % - (p.returncode, error)) + print("binapi generator installation failed: %d %s" % (p.returncode, error)) sys.exit(1) bg_ver = get_binapi_gen_version(go_path) - print('Installed binary API generator ' + bg_ver) + print("Installed binary API generator " + bg_ver) # Creates generated bindings using GoVPP binapigen to the target folder -def generate_api(output_dir, vpp_dir, api_list, import_prefix, - no_source, go_path): +def generate_api(output_dir, vpp_dir, api_list, import_prefix, no_source, go_path): json_dir = vpp_dir + "/build-root/install-vpp-native/vpp/share/vpp/api" if not os.path.exists(json_dir): @@ -115,19 +118,20 @@ def generate_api(output_dir, vpp_dir, api_list, import_prefix, if output_dir: cmd += ["--output-dir=" + output_dir] if len(api_list): - print("Following API files were requested by 'GO_API_FILES': " + - str(api_list)) - print("Note that dependency requirements may generate " - "additional API files") + print("Following API files were requested by 'GO_API_FILES': " + str(api_list)) + print("Note that dependency requirements may generate " "additional API files") cmd.append(api_list) if import_prefix: cmd.append("-import-prefix=" + import_prefix) if no_source: cmd.append("-no-source-path-info") - p = subprocess.Popen(cmd, cwd=go_path + "/bin", - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True, ) + p = subprocess.Popen( + cmd, + cwd=go_path + "/bin", + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ) out = p.communicate()[1] if p.returncode != 0: @@ -150,50 +154,70 @@ def main(): vpp_dir = root.parent.parent.parent parser = argparse.ArgumentParser() - parser.add_argument("-govpp-commit", "--govpp-commit", - help="GoVPP commit or branch " - "(defaults to v0.3.5-45-g671f16c)", - default="671f16c", # fixed GoVPP version - type=str) - parser.add_argument("-output-dir", "--output-dir", - help="output target directory for generated bindings", - type=str, - default=os.path.join(vpp_dir, "vppbinapi")) - parser.add_argument("-api-files", "--api-files", - help="api files to generate (without commas)", - nargs="+", - type=str, - default=[]) - parser.add_argument("-import-prefix", "--import-prefix", - help="prefix imports in the generated go code", - default="", - type=str) - parser.add_argument("-no-source-path-info", "--no-source-path-info", - help="disable source path info in generated files", - nargs='?', - const=True, - default=True) + parser.add_argument( + "-govpp-commit", + "--govpp-commit", + help="GoVPP commit or branch " "(defaults to v0.3.5-45-g671f16c)", + default="671f16c", # fixed GoVPP version + type=str, + ) + parser.add_argument( + "-output-dir", + "--output-dir", + help="output target directory for generated bindings", + type=str, + default=os.path.join(vpp_dir, "vppbinapi"), + ) + parser.add_argument( + "-api-files", + "--api-files", + help="api files to generate (without commas)", + nargs="+", + type=str, + default=[], + ) + parser.add_argument( + "-import-prefix", + "--import-prefix", + help="prefix imports in the generated go code", + default="", + type=str, + ) + parser.add_argument( + "-no-source-path-info", + "--no-source-path-info", + help="disable source path info in generated files", + nargs="?", + const=True, + default=True, + ) args = parser.parse_args() # go specific environment variables if "GOROOT" in os.environ: - go_root = os.environ['GOROOT'] + go_root = os.environ["GOROOT"] else: go_binary = shutil.which("go") if go_binary != "": go_binary_dir, _ = os.path.split(go_binary) go_root = os.path.join(go_binary_dir, "..") else: - go_root = os.environ['HOME'] + "/.go" + go_root = os.environ["HOME"] + "/.go" if "GOPATH" in os.environ: - go_path = os.environ['GOPATH'] + go_path = os.environ["GOPATH"] else: - go_path = os.environ['HOME'] + "/go" + go_path = os.environ["HOME"] + "/go" install_golang(go_root) install_binapi_gen(args.govpp_commit, go_root, go_path) - generate_api(args.output_dir, str(vpp_dir), args.api_files, - args.import_prefix, args.no_source_path_info, go_path) + generate_api( + args.output_dir, + str(vpp_dir), + args.api_files, + args.import_prefix, + args.no_source_path_info, + go_path, + ) if __name__ == "__main__": diff --git a/src/tools/vppapigen/generate_json.py b/src/tools/vppapigen/generate_json.py index 288e519edd2..e8041c5a3eb 100755 --- a/src/tools/vppapigen/generate_json.py +++ b/src/tools/vppapigen/generate_json.py @@ -16,30 +16,37 @@ import argparse import pathlib import subprocess -BASE_DIR = subprocess.check_output('git rev-parse --show-toplevel', - shell=True).strip().decode() + +BASE_DIR = ( + subprocess.check_output("git rev-parse --show-toplevel", shell=True) + .strip() + .decode() +) vppapigen_bin = pathlib.Path( - '%s/src/tools/vppapigen/vppapigen.py' % BASE_DIR).as_posix() + "%s/src/tools/vppapigen/vppapigen.py" % BASE_DIR +).as_posix() src_dir_depth = 3 output_path = pathlib.Path( - '%s/build-root/install-vpp-native/vpp/share/vpp/api/' % BASE_DIR) + "%s/build-root/install-vpp-native/vpp/share/vpp/api/" % BASE_DIR +) output_path_debug = pathlib.Path( - '%s/build-root/install-vpp_debug-native/vpp/share/vpp/api/' % BASE_DIR) + "%s/build-root/install-vpp_debug-native/vpp/share/vpp/api/" % BASE_DIR +) output_dir_map = { - 'plugins': 'plugins', - 'vlibmemory': 'core', - 'vnet': 'core', - 'vlib': 'core', - 'vpp': 'core', + "plugins": "plugins", + "vlibmemory": "core", + "vnet": "core", + "vlib": "core", + "vpp": "core", } def api_search_globs(src_dir): globs = [] for g in output_dir_map: - globs.extend(list(src_dir.glob('%s/**/*.api' % g))) + globs.extend(list(src_dir.glob("%s/**/*.api" % g))) return globs @@ -51,28 +58,41 @@ def api_files(src_dir): def vppapigen(vppapigen_bin, output_path, src_dir, src_file): try: subprocess.check_output( - [vppapigen_bin, '--includedir', src_dir.as_posix(), - '--input', src_file.as_posix(), 'JSON', - '--output', '%s/%s/%s.json' % ( - output_path, - output_dir_map[src_file.as_posix().split('/')[ - src_dir_depth + BASE_DIR.count('/') - 1]], - src_file.name)]) + [ + vppapigen_bin, + "--includedir", + src_dir.as_posix(), + "--input", + src_file.as_posix(), + "JSON", + "--output", + "%s/%s/%s.json" + % ( + output_path, + output_dir_map[ + src_file.as_posix().split("/")[ + src_dir_depth + BASE_DIR.count("/") - 1 + ] + ], + src_file.name, + ), + ] + ) except KeyError: - print('src_file: %s' % src_file) + print("src_file: %s" % src_file) raise def main(): - cliparser = argparse.ArgumentParser( - description='VPP API JSON definition generator') - cliparser.add_argument('--srcdir', action='store', - default='%s/src' % BASE_DIR), - cliparser.add_argument('--output', action='store', - help='directory to store files'), - cliparser.add_argument('--debug-target', action='store_true', - default=False, - help="'True' if -debug target"), + cliparser = argparse.ArgumentParser(description="VPP API JSON definition generator") + cliparser.add_argument("--srcdir", action="store", default="%s/src" % BASE_DIR), + cliparser.add_argument("--output", action="store", help="directory to store files"), + cliparser.add_argument( + "--debug-target", + action="store_true", + default=False, + help="'True' if -debug target", + ), args = cliparser.parse_args() src_dir = pathlib.Path(args.srcdir) @@ -86,13 +106,13 @@ def main(): for d in output_dir_map.values(): output_dir.joinpath(d).mkdir(exist_ok=True, parents=True) - for f in output_dir.glob('**/*.api.json'): + for f in output_dir.glob("**/*.api.json"): f.unlink() for f in api_files(src_dir): vppapigen(vppapigen_bin, output_dir, src_dir, f) - print('json files written to: %s/.' % output_dir) + print("json files written to: %s/." % output_dir) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/src/tools/vppapigen/test_vppapigen.py b/src/tools/vppapigen/test_vppapigen.py index c454ffc8638..20f6c6da10d 100755 --- a/src/tools/vppapigen/test_vppapigen.py +++ b/src/tools/vppapigen/test_vppapigen.py @@ -1,8 +1,7 @@ #!/usr/bin/env python3 import unittest -from vppapigen import VPPAPI, Option, ParseError, Union, foldup_crcs, \ - global_types +from vppapigen import VPPAPI, Option, ParseError, Union, foldup_crcs, global_types import vppapigen @@ -28,17 +27,17 @@ class TestUnion(unittest.TestCase): cls.parser = VPPAPI() def test_union(self): - test_string = ''' + test_string = """ union foo_union { u32 a; u8 b; }; - ''' + """ r = self.parser.parse_string(test_string) self.assertTrue(isinstance(r[0], Union)) def test_union_vla(self): - test_string = ''' + test_string = """ union foo_union_vla { u32 a; u8 b[a]; @@ -46,13 +45,13 @@ class TestUnion(unittest.TestCase): autoreply define foo { vl_api_foo_union_vla_t v; }; - ''' + """ r = self.parser.parse_string(test_string) self.assertTrue(isinstance(r[0], Union)) self.assertTrue(r[0].vla) s = self.parser.process(r) - test_string2 = ''' + test_string2 = """ union foo_union_vla2 { u32 a; u8 b[a]; @@ -61,10 +60,10 @@ class TestUnion(unittest.TestCase): autoreply define foo2 { vl_api_foo_union_vla2_t v; }; - ''' + """ self.assertRaises(ValueError, self.parser.parse_string, test_string2) - test_string3 = ''' + test_string3 = """ union foo_union_vla3 { u32 a; u8 b[a]; @@ -73,7 +72,7 @@ class TestUnion(unittest.TestCase): vl_api_foo_union_vla3_t v; u32 x; }; - ''' + """ self.assertRaises(ValueError, self.parser.parse_string, test_string3) @@ -83,10 +82,10 @@ class TestTypedef(unittest.TestCase): cls.parser = VPPAPI() def test_duplicatetype(self): - test_string = ''' + test_string = """ typedef foo1 { u8 dummy; }; typedef foo1 { u8 dummy; }; - ''' + """ self.assertRaises(KeyError, self.parser.parse_string, test_string) @@ -96,42 +95,42 @@ class TestDefine(unittest.TestCase): cls.parser = VPPAPI() def test_unknowntype(self): - test_string = 'define foo { foobar foo;};' + test_string = "define foo { foobar foo;};" with self.assertRaises(ParseError) as ctx: self.parser.parse_string(test_string) - self.assertIn('Undefined type: foobar', str(ctx.exception)) + self.assertIn("Undefined type: foobar", str(ctx.exception)) - test_string = 'define { u8 foo;};' + test_string = "define { u8 foo;};" with self.assertRaises(ParseError) as ctx: self.parser.parse_string(test_string) def test_flags(self): - test_string = ''' + test_string = """ manual_print dont_trace manual_endian define foo { u8 foo; }; define foo_reply {u32 context; i32 retval; }; - ''' + """ r = self.parser.parse_string(test_string) self.assertIsNotNone(r) s = self.parser.process(r) self.assertIsNotNone(s) - for d in s['Define']: - if d.name == 'foo': + for d in s["Define"]: + if d.name == "foo": self.assertTrue(d.dont_trace) self.assertTrue(d.manual_endian) self.assertTrue(d.manual_print) self.assertFalse(d.autoreply) - test_string = ''' + test_string = """ nonexisting_flag define foo { u8 foo; }; - ''' + """ with self.assertRaises(ParseError): self.parser.parse_string(test_string) def test_options(self): - test_string = ''' + test_string = """ define foo { option deprecated; u8 foo; }; define foo_reply {u32 context; i32 retval; }; - ''' + """ r = self.parser.parse_string(test_string) self.assertIsNotNone(r) s = self.parser.process(r) @@ -144,14 +143,14 @@ class TestService(unittest.TestCase): cls.parser = VPPAPI() def test_service(self): - test_string = ''' + test_string = """ autoreply define show_version { u8 foo;}; service { rpc show_version returns show_version_reply; }; - ''' + """ r = self.parser.parse_string(test_string) s = self.parser.process(r) - self.assertEqual(s['Service'][0].caller, 'show_version') - self.assertEqual(s['Service'][0].reply, 'show_version_reply') + self.assertEqual(s["Service"][0].caller, "show_version") + self.assertEqual(s["Service"][0].reply, "show_version_reply") def get_crc(apistring, name): @@ -159,52 +158,52 @@ def get_crc(apistring, name): parser = vppapigen.VPPAPI() r = parser.parse_string(apistring) s = parser.process(r) - foldup_crcs(s['Define']) - d = [f for f in s['Define'] if f.name == name] + foldup_crcs(s["Define"]) + d = [f for f in s["Define"] if f.name == name] return d[0].crc class TestCRC(unittest.TestCase): def test_crc(self): - test_string = ''' + test_string = """ typedef list { u8 foo; }; autoreply define foo { u8 foo; vl_api_list_t l;}; - ''' - crc = get_crc(test_string, 'foo') + """ + crc = get_crc(test_string, "foo") # modify underlying type - test_string = ''' + test_string = """ typedef list { u8 foo2; }; autoreply define foo { u8 foo; vl_api_list_t l;}; - ''' - crc2 = get_crc(test_string, 'foo') + """ + crc2 = get_crc(test_string, "foo") self.assertNotEqual(crc, crc2) # two user-defined types - test_string = ''' + test_string = """ typedef address { u8 foo2; }; typedef list { u8 foo2; vl_api_address_t add; }; autoreply define foo { u8 foo; vl_api_list_t l;}; - ''' - crc3 = get_crc(test_string, 'foo') + """ + crc3 = get_crc(test_string, "foo") - test_string = ''' + test_string = """ typedef address { u8 foo3; }; typedef list { u8 foo2; vl_api_address_t add; }; autoreply define foo { u8 foo; vl_api_list_t l;}; - ''' - crc4 = get_crc(test_string, 'foo') + """ + crc4 = get_crc(test_string, "foo") self.assertNotEqual(crc3, crc4) - test_string = ''' + test_string = """ typedef address { u8 foo3; }; typedef list { u8 foo2; vl_api_address_t add; u8 foo3; }; autoreply define foo { u8 foo; vl_api_list_t l;}; - ''' - crc5 = get_crc(test_string, 'foo') + """ + crc5 = get_crc(test_string, "foo") self.assertNotEqual(crc4, crc5) - test_string = ''' + test_string = """ typedef ip6_address { u8 foo; @@ -227,11 +226,11 @@ autoreply define sr_policy_add u32 fib_table; vl_api_srv6_sid_list_t sids; }; -''' +""" - crc = get_crc(test_string, 'sr_policy_add') + crc = get_crc(test_string, "sr_policy_add") - test_string = ''' + test_string = """ typedef ip6_address { u8 foo; @@ -253,14 +252,13 @@ autoreply define sr_policy_add u32 fib_table; vl_api_srv6_sid_list_t sids; }; -''' - crc2 = get_crc(test_string, 'sr_policy_add') +""" + crc2 = get_crc(test_string, "sr_policy_add") self.assertNotEqual(crc, crc2) class TestEnum(unittest.TestCase): - @classmethod def setUpClass(cls): cls.parser = VPPAPI() @@ -278,8 +276,8 @@ enum tunnel_mode : u8 r = self.parser.parse_string(test_string) self.assertIsNotNone(r) s = self.parser.process(r) - for o in s['types']: - if o.type == 'Enum': + for o in s["types"]: + if o.type == "Enum": self.assertEqual(o.name, "tunnel_mode") break else: @@ -298,8 +296,8 @@ enum virtio_flags { r = self.parser.parse_string(test_string) self.assertIsNotNone(r) s = self.parser.process(r) - for o in s['types']: - if o.type == 'Enum': + for o in s["types"]: + if o.type == "Enum": self.assertEqual(o.name, "virtio_flags") break else: @@ -307,7 +305,6 @@ enum virtio_flags { class TestEnumFlag(unittest.TestCase): - @classmethod def setUpClass(cls): cls.parser = VPPAPI() @@ -326,8 +323,9 @@ enumflag tunnel_mode_ef : u8 with self.assertRaises(TypeError) as ctx: r = self.parser.parse_string(test_string) - self.assertTrue(str(ctx.exception).startswith( - 'tunnel_mode_ef is not a flag enum.')) + self.assertTrue( + str(ctx.exception).startswith("tunnel_mode_ef is not a flag enum.") + ) def test_enumflag_as_enumflag(self): test_string = """\ @@ -342,13 +340,13 @@ enumflag virtio_flags_ef { r = self.parser.parse_string(test_string) self.assertIsNotNone(r) s = self.parser.process(r) - for o in s['types']: - if o.type == 'EnumFlag': + for o in s["types"]: + if o.type == "EnumFlag": self.assertEqual(o.name, "virtio_flags_ef") break else: self.fail() -if __name__ == '__main__': +if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/src/tools/vppapigen/vppapigen.py b/src/tools/vppapigen/vppapigen.py index 8415c28fb7b..c7bc68180a8 100755 --- a/src/tools/vppapigen/vppapigen.py +++ b/src/tools/vppapigen/vppapigen.py @@ -10,9 +10,10 @@ from subprocess import Popen, PIPE import ply.lex as lex import ply.yacc as yacc -assert sys.version_info >= (3, 5), \ - "Not supported Python version: {}".format(sys.version) -log = logging.getLogger('vppapigen') +assert sys.version_info >= (3, 5), "Not supported Python version: {}".format( + sys.version +) +log = logging.getLogger("vppapigen") # Ensure we don't leave temporary files around sys.dont_write_bytecode = True @@ -28,11 +29,10 @@ seen_imports = {} def global_type_add(name, obj): - '''Add new type to the dictionary of types ''' - type_name = 'vl_api_' + name + '_t' + """Add new type to the dictionary of types""" + type_name = "vl_api_" + name + "_t" if type_name in global_types: - raise KeyError("Attempted redefinition of {!r} with {!r}.".format( - name, obj)) + raise KeyError("Attempted redefinition of {!r} with {!r}.".format(name, obj)) global_types[type_name] = obj @@ -49,104 +49,104 @@ class VPPAPILexer: self.filename = filename reserved = { - 'service': 'SERVICE', - 'rpc': 'RPC', - 'returns': 'RETURNS', - 'null': 'NULL', - 'stream': 'STREAM', - 'events': 'EVENTS', - 'define': 'DEFINE', - 'typedef': 'TYPEDEF', - 'enum': 'ENUM', - 'enumflag': 'ENUMFLAG', - 'typeonly': 'TYPEONLY', - 'manual_print': 'MANUAL_PRINT', - 'manual_endian': 'MANUAL_ENDIAN', - 'dont_trace': 'DONT_TRACE', - 'autoreply': 'AUTOREPLY', - 'autoendian': 'AUTOENDIAN', - 'option': 'OPTION', - 'u8': 'U8', - 'u16': 'U16', - 'u32': 'U32', - 'u64': 'U64', - 'i8': 'I8', - 'i16': 'I16', - 'i32': 'I32', - 'i64': 'I64', - 'f64': 'F64', - 'bool': 'BOOL', - 'string': 'STRING', - 'import': 'IMPORT', - 'true': 'TRUE', - 'false': 'FALSE', - 'union': 'UNION', - 'counters': 'COUNTERS', - 'paths': 'PATHS', - 'units': 'UNITS', - 'severity': 'SEVERITY', - 'type': 'TYPE', - 'description': 'DESCRIPTION', + "service": "SERVICE", + "rpc": "RPC", + "returns": "RETURNS", + "null": "NULL", + "stream": "STREAM", + "events": "EVENTS", + "define": "DEFINE", + "typedef": "TYPEDEF", + "enum": "ENUM", + "enumflag": "ENUMFLAG", + "typeonly": "TYPEONLY", + "manual_print": "MANUAL_PRINT", + "manual_endian": "MANUAL_ENDIAN", + "dont_trace": "DONT_TRACE", + "autoreply": "AUTOREPLY", + "autoendian": "AUTOENDIAN", + "option": "OPTION", + "u8": "U8", + "u16": "U16", + "u32": "U32", + "u64": "U64", + "i8": "I8", + "i16": "I16", + "i32": "I32", + "i64": "I64", + "f64": "F64", + "bool": "BOOL", + "string": "STRING", + "import": "IMPORT", + "true": "TRUE", + "false": "FALSE", + "union": "UNION", + "counters": "COUNTERS", + "paths": "PATHS", + "units": "UNITS", + "severity": "SEVERITY", + "type": "TYPE", + "description": "DESCRIPTION", } - tokens = ['STRING_LITERAL', - 'ID', 'NUM'] + list(reserved.values()) + tokens = ["STRING_LITERAL", "ID", "NUM"] + list(reserved.values()) - t_ignore_LINE_COMMENT = '//.*' + t_ignore_LINE_COMMENT = "//.*" def t_FALSE(self, t): - r'false' + r"false" t.value = False return t def t_TRUE(self, t): - r'false' + r"false" t.value = True return t def t_NUM(self, t): - r'0[xX][0-9a-fA-F]+|-?\d+\.?\d*' - base = 16 if t.value.startswith('0x') else 10 - if '.' in t.value: + r"0[xX][0-9a-fA-F]+|-?\d+\.?\d*" + base = 16 if t.value.startswith("0x") else 10 + if "." in t.value: t.value = float(t.value) else: t.value = int(t.value, base) return t def t_ID(self, t): - r'[a-zA-Z_][a-zA-Z_0-9]*' + r"[a-zA-Z_][a-zA-Z_0-9]*" # Check for reserved words - t.type = VPPAPILexer.reserved.get(t.value, 'ID') + t.type = VPPAPILexer.reserved.get(t.value, "ID") return t # C string def t_STRING_LITERAL(self, t): - r'\"([^\\\n]|(\\.))*?\"' - t.value = str(t.value).replace("\"", "") + r"\"([^\\\n]|(\\.))*?\" " + t.value = str(t.value).replace('"', "") return t # C or C++ comment (ignore) def t_comment(self, t): - r'(/\*(.|\n)*?\*/)|(//.*)' - t.lexer.lineno += t.value.count('\n') + r"(/\*(.|\n)*?\*/)|(//.*)" + t.lexer.lineno += t.value.count("\n") # Error handling rule def t_error(self, t): - raise ParseError("Illegal character '{}' ({})" - "in {}: line {}".format(t.value[0], - hex(ord(t.value[0])), - self.filename, - t.lexer.lineno)) + raise ParseError( + "Illegal character '{}' ({})" + "in {}: line {}".format( + t.value[0], hex(ord(t.value[0])), self.filename, t.lexer.lineno + ) + ) # Define a rule so we can track line numbers def t_newline(self, t): - r'\n+' + r"\n+" t.lexer.lineno += len(t.value) literals = ":{}[];=.," # A string containing ignored characters (spaces and tabs) - t_ignore = ' \t' + t_ignore = " \t" def vla_mark_length_field(block): @@ -164,23 +164,25 @@ def vla_is_last_check(name, block): vla = True if i + 1 < len(block): raise ValueError( - 'VLA field "{}" must be the last field in message "{}"' - .format(b.fieldname, name)) - elif b.fieldtype.startswith('vl_api_'): + 'VLA field "{}" must be the last field in message "{}"'.format( + b.fieldname, name + ) + ) + elif b.fieldtype.startswith("vl_api_"): if global_types[b.fieldtype].vla: vla = True if i + 1 < len(block): raise ValueError( 'VLA field "{}" must be the last ' - 'field in message "{}"' - .format(b.fieldname, name)) - elif b.fieldtype == 'string' and b.length == 0: + 'field in message "{}"'.format(b.fieldname, name) + ) + elif b.fieldtype == "string" and b.length == 0: vla = True if i + 1 < len(block): raise ValueError( 'VLA field "{}" must be the last ' - 'field in message "{}"' - .format(b.fieldname, name)) + 'field in message "{}"'.format(b.fieldname, name) + ) return vla @@ -192,10 +194,9 @@ class Processable: class Service(Processable): - type = 'Service' + type = "Service" - def __init__(self, caller, reply, events=None, stream_message=None, - stream=False): + def __init__(self, caller, reply, events=None, stream_message=None, stream=False): self.caller = caller self.reply = reply self.stream = stream @@ -204,7 +205,7 @@ class Service(Processable): class Typedef(Processable): - type = 'Typedef' + type = "Typedef" def __init__(self, name, flags, block): self.name = name @@ -214,9 +215,9 @@ class Typedef(Processable): self.manual_print = False self.manual_endian = False for f in flags: - if f == 'manual_print': + if f == "manual_print": self.manual_print = True - elif f == 'manual_endian': + elif f == "manual_endian": self.manual_endian = True global_type_add(name, self) @@ -224,14 +225,14 @@ class Typedef(Processable): vla_mark_length_field(self.block) def process(self, result): - result['types'].append(self) + result["types"].append(self) def __repr__(self): return self.name + str(self.flags) + str(self.block) class Using(Processable): - type = 'Using' + type = "Using" def __init__(self, name, flags, alias): self.name = name @@ -243,16 +244,15 @@ class Using(Processable): self.manual_print = False self.manual_endian = False for f in flags: - if f == 'manual_print': + if f == "manual_print": self.manual_print = True - elif f == 'manual_endian': + elif f == "manual_endian": self.manual_endian = True if isinstance(alias, Array): - a = {'type': alias.fieldtype, - 'length': alias.length} + a = {"type": alias.fieldtype, "length": alias.length} else: - a = {'type': alias.fieldtype} + a = {"type": alias.fieldtype} self.alias = a self.using = alias @@ -265,14 +265,14 @@ class Using(Processable): global_type_add(name, self) def process(self, result): # -> Dict - result['types'].append(self) + result["types"].append(self) def __repr__(self): return self.name + str(self.alias) class Union(Processable): - type = 'Union' + type = "Union" def __init__(self, name, flags, block): self.manual_print = False @@ -280,9 +280,9 @@ class Union(Processable): self.name = name for f in flags: - if f == 'manual_print': + if f == "manual_print": self.manual_print = True - elif f == 'manual_endian': + elif f == "manual_endian": self.manual_endian = True self.block = block @@ -292,14 +292,14 @@ class Union(Processable): global_type_add(name, self) def process(self, result): - result['types'].append(self) + result["types"].append(self) def __repr__(self): return str(self.block) class Define(Processable): - type = 'Define' + type = "Define" def __init__(self, name, flags, block): self.name = name @@ -312,15 +312,15 @@ class Define(Processable): self.autoendian = 0 self.options = {} for f in flags: - if f == 'dont_trace': + if f == "dont_trace": self.dont_trace = True - elif f == 'manual_print': + elif f == "manual_print": self.manual_print = True - elif f == 'manual_endian': + elif f == "manual_endian": self.manual_endian = True - elif f == 'autoreply': + elif f == "autoreply": self.autoreply = True - elif f == 'autoendian': + elif f == "autoendian": self.autoendian = 1 remove = [] @@ -337,12 +337,11 @@ class Define(Processable): self.crc = str(block).encode() def autoreply_block(self, name, parent): - block = [Field('u32', 'context'), - Field('i32', 'retval')] + block = [Field("u32", "context"), Field("i32", "retval")] # inherit the parent's options for k, v in parent.options.items(): block.append(Option(k, v)) - return Define(name + '_reply', [], block) + return Define(name + "_reply", [], block) def process(self, result): # -> Dict tname = self.__class__.__name__ @@ -355,9 +354,9 @@ class Define(Processable): class Enum(Processable): - type = 'Enum' + type = "Enum" - def __init__(self, name, block, enumtype='u32'): + def __init__(self, name, block, enumtype="u32"): self.name = name self.enumtype = enumtype self.vla = False @@ -369,47 +368,50 @@ class Enum(Processable): bc_set = False for b in block: - if 'value' in b: - count = b['value'] + if "value" in b: + count = b["value"] else: count += 1 - block2.append([b['id'], count]) + block2.append([b["id"], count]) try: - if b['option']['backwards_compatible']: + if b["option"]["backwards_compatible"]: pass bc_set = True except KeyError: - block3.append([b['id'], count]) + block3.append([b["id"], count]) if bc_set: - raise ValueError("Backward compatible enum must " - "be last {!r} {!r}" - .format(name, b['id'])) + raise ValueError( + "Backward compatible enum must " + "be last {!r} {!r}".format(name, b["id"]) + ) self.block = block2 self.crc = str(block3).encode() global_type_add(name, self) def process(self, result): - result['types'].append(self) + result["types"].append(self) def __repr__(self): return self.name + str(self.block) class EnumFlag(Enum): - type = 'EnumFlag' + type = "EnumFlag" - def __init__(self, name, block, enumtype='u32'): + def __init__(self, name, block, enumtype="u32"): super(EnumFlag, self).__init__(name, block, enumtype) for b in self.block: if bin(b[1])[2:].count("1") > 1: - raise TypeError("%s is not a flag enum. No element in a " - "flag enum may have more than a " - "single bit set." % self.name) + raise TypeError( + "%s is not a flag enum. No element in a " + "flag enum may have more than a " + "single bit set." % self.name + ) class Import(Processable): - type = 'Import' + type = "Import" _initialized = False def __new__(cls, *args, **kwargs): @@ -440,7 +442,7 @@ class Import(Processable): class Option(Processable): - type = 'Option' + type = "Option" def __init__(self, option, value=None): self.option = option @@ -458,7 +460,7 @@ class Option(Processable): class Array(Processable): - type = 'Array' + type = "Array" def __init__(self, fieldtype, name, length, modern_vla=False): self.fieldtype = fieldtype @@ -474,12 +476,11 @@ class Array(Processable): self.vla = False def __repr__(self): - return str([self.fieldtype, self.fieldname, self.length, - self.lengthfield]) + return str([self.fieldtype, self.fieldname, self.length, self.lengthfield]) class Field(Processable): - type = 'Field' + type = "Field" def __init__(self, fieldtype, name, limit=None): # limit field has been expanded to an options dict. @@ -487,13 +488,14 @@ class Field(Processable): self.fieldtype = fieldtype self.is_lengthfield = False - if self.fieldtype == 'string': - raise ValueError("The string type {!r} is an " - "array type ".format(name)) + if self.fieldtype == "string": + raise ValueError("The string type {!r} is an " "array type ".format(name)) if name in keyword.kwlist: - raise ValueError("Fieldname {!r} is a python keyword and is not " - "accessible via the python API. ".format(name)) + raise ValueError( + "Fieldname {!r} is a python keyword and is not " + "accessible via the python API. ".format(name) + ) self.fieldname = name self.limit = limit @@ -502,35 +504,34 @@ class Field(Processable): class Counter(Processable): - type = 'Counter' + type = "Counter" def __init__(self, path, counter): self.name = path self.block = counter def process(self, result): # -> Dict - result['Counters'].append(self) + result["Counters"].append(self) class Paths(Processable): - type = 'Paths' + type = "Paths" def __init__(self, pathset): self.paths = pathset def __repr__(self): - return "%s(paths=%s)" % ( - self.__class__.__name__, self.paths - ) + return "%s(paths=%s)" % (self.__class__.__name__, self.paths) class Coord: - """ Coordinates of a syntactic element. Consists of: - - File name - - Line number - - (optional) column number, for the Lexer + """Coordinates of a syntactic element. Consists of: + - File name + - Line number + - (optional) column number, for the Lexer """ - __slots__ = ('file', 'line', 'column', '__weakref__') + + __slots__ = ("file", "line", "column", "__weakref__") def __init__(self, file, line, column=None): self.file = file @@ -568,49 +569,47 @@ class VPPAPIParser: self.logger.warning("%s: %s" % (coord, msg)) def _coord(self, lineno, column=None): - return Coord( - file=self.filename, - line=lineno, column=column) + return Coord(file=self.filename, line=lineno, column=column) def _token_coord(self, p, token_idx): - """ Returns the coordinates for the YaccProduction object 'p' indexed - with 'token_idx'. The coordinate includes the 'lineno' and - 'column'. Both follow the lex semantic, starting from 1. + """Returns the coordinates for the YaccProduction object 'p' indexed + with 'token_idx'. The coordinate includes the 'lineno' and + 'column'. Both follow the lex semantic, starting from 1. """ - last_cr = p.lexer.lexdata.rfind('\n', 0, p.lexpos(token_idx)) + last_cr = p.lexer.lexdata.rfind("\n", 0, p.lexpos(token_idx)) if last_cr < 0: last_cr = -1 - column = (p.lexpos(token_idx) - (last_cr)) + column = p.lexpos(token_idx) - (last_cr) return self._coord(p.lineno(token_idx), column) def p_slist(self, p): - '''slist : stmt - | slist stmt''' + """slist : stmt + | slist stmt""" if len(p) == 2: p[0] = [p[1]] else: p[0] = p[1] + [p[2]] def p_stmt(self, p): - '''stmt : define - | typedef - | option - | import - | enum - | enumflag - | union - | service - | paths - | counters''' + """stmt : define + | typedef + | option + | import + | enum + | enumflag + | union + | service + | paths + | counters""" p[0] = p[1] def p_import(self, p): - '''import : IMPORT STRING_LITERAL ';' ''' + """import : IMPORT STRING_LITERAL ';'""" p[0] = Import(p[2], revision=self.revision) def p_path_elements(self, p): - '''path_elements : path_element - | path_elements path_element''' + """path_elements : path_element + | path_elements path_element""" if len(p) == 2: p[0] = p[1] else: @@ -620,20 +619,20 @@ class VPPAPIParser: p[0] = p[1] + [p[2]] def p_path_element(self, p): - '''path_element : STRING_LITERAL STRING_LITERAL ';' ''' - p[0] = {'path': p[1], 'counter': p[2]} + """path_element : STRING_LITERAL STRING_LITERAL ';'""" + p[0] = {"path": p[1], "counter": p[2]} def p_paths(self, p): - '''paths : PATHS '{' path_elements '}' ';' ''' + """paths : PATHS '{' path_elements '}' ';'""" p[0] = Paths(p[3]) def p_counters(self, p): - '''counters : COUNTERS ID '{' counter_elements '}' ';' ''' + """counters : COUNTERS ID '{' counter_elements '}' ';'""" p[0] = Counter(p[2], p[4]) def p_counter_elements(self, p): - '''counter_elements : counter_element - | counter_elements counter_element''' + """counter_elements : counter_element + | counter_elements counter_element""" if len(p) == 2: p[0] = [p[1]] else: @@ -643,46 +642,47 @@ class VPPAPIParser: p[0] = p[1] + [p[2]] def p_counter_element(self, p): - '''counter_element : ID '{' counter_statements '}' ';' ''' - p[0] = {**{'name': p[1]}, **p[3]} + """counter_element : ID '{' counter_statements '}' ';'""" + p[0] = {**{"name": p[1]}, **p[3]} def p_counter_statements(self, p): - '''counter_statements : counter_statement - | counter_statements counter_statement''' + """counter_statements : counter_statement + | counter_statements counter_statement""" if len(p) == 2: p[0] = p[1] else: p[0] = {**p[1], **p[2]} def p_counter_statement(self, p): - '''counter_statement : SEVERITY ID ';' - | UNITS STRING_LITERAL ';' - | DESCRIPTION STRING_LITERAL ';' - | TYPE ID ';' ''' + """counter_statement : SEVERITY ID ';' + | UNITS STRING_LITERAL ';' + | DESCRIPTION STRING_LITERAL ';' + | TYPE ID ';'""" p[0] = {p[1]: p[2]} def p_service(self, p): - '''service : SERVICE '{' service_statements '}' ';' ''' + """service : SERVICE '{' service_statements '}' ';'""" p[0] = p[3] def p_service_statements(self, p): - '''service_statements : service_statement - | service_statements service_statement''' + """service_statements : service_statement + | service_statements service_statement""" if len(p) == 2: p[0] = [p[1]] else: p[0] = p[1] + [p[2]] def p_service_statement(self, p): - '''service_statement : RPC ID RETURNS NULL ';' - | RPC ID RETURNS ID ';' - | RPC ID RETURNS STREAM ID ';' - | RPC ID RETURNS ID EVENTS event_list ';' ''' + """service_statement : RPC ID RETURNS NULL ';' + | RPC ID RETURNS ID ';' + | RPC ID RETURNS STREAM ID ';' + | RPC ID RETURNS ID EVENTS event_list ';'""" if p[2] == p[4]: # Verify that caller and reply differ self._parse_error( - 'Reply ID ({}) should not be equal to Caller ID'.format(p[2]), - self._token_coord(p, 1)) + "Reply ID ({}) should not be equal to Caller ID".format(p[2]), + self._token_coord(p, 1), + ) if len(p) == 8: p[0] = Service(p[2], p[4], p[6]) elif len(p) == 7: @@ -691,280 +691,283 @@ class VPPAPIParser: p[0] = Service(p[2], p[4]) def p_service_statement2(self, p): - '''service_statement : RPC ID RETURNS ID STREAM ID ';' ''' + """service_statement : RPC ID RETURNS ID STREAM ID ';'""" p[0] = Service(p[2], p[4], stream_message=p[6], stream=True) def p_event_list(self, p): - '''event_list : events - | event_list events ''' + """event_list : events + | event_list events""" if len(p) == 2: p[0] = [p[1]] else: p[0] = p[1] + [p[2]] def p_event(self, p): - '''events : ID - | ID ',' ''' + """events : ID + | ID ','""" p[0] = p[1] def p_enum(self, p): - '''enum : ENUM ID '{' enum_statements '}' ';' ''' + """enum : ENUM ID '{' enum_statements '}' ';'""" p[0] = Enum(p[2], p[4]) def p_enum_type(self, p): - ''' enum : ENUM ID ':' enum_size '{' enum_statements '}' ';' ''' + """enum : ENUM ID ':' enum_size '{' enum_statements '}' ';'""" if len(p) == 9: p[0] = Enum(p[2], p[6], enumtype=p[4]) else: p[0] = Enum(p[2], p[4]) def p_enumflag(self, p): - '''enumflag : ENUMFLAG ID '{' enum_statements '}' ';' ''' + """enumflag : ENUMFLAG ID '{' enum_statements '}' ';'""" p[0] = EnumFlag(p[2], p[4]) def p_enumflag_type(self, p): - ''' enumflag : ENUMFLAG ID ':' enumflag_size '{' enum_statements '}' ';' ''' # noqa : E502 + """enumflag : ENUMFLAG ID ':' enumflag_size '{' enum_statements '}' ';'""" # noqa : E502 if len(p) == 9: p[0] = EnumFlag(p[2], p[6], enumtype=p[4]) else: p[0] = EnumFlag(p[2], p[4]) def p_enum_size(self, p): - ''' enum_size : U8 - | U16 - | U32 - | I8 - | I16 - | I32 ''' + """enum_size : U8 + | U16 + | U32 + | I8 + | I16 + | I32""" p[0] = p[1] def p_enumflag_size(self, p): - ''' enumflag_size : U8 - | U16 - | U32 ''' + """enumflag_size : U8 + | U16 + | U32""" p[0] = p[1] def p_define(self, p): - '''define : DEFINE ID '{' block_statements_opt '}' ';' ''' + """define : DEFINE ID '{' block_statements_opt '}' ';'""" self.fields = [] p[0] = Define(p[2], [], p[4]) def p_define_flist(self, p): - '''define : flist DEFINE ID '{' block_statements_opt '}' ';' ''' + """define : flist DEFINE ID '{' block_statements_opt '}' ';'""" # Legacy typedef - if 'typeonly' in p[1]: - self._parse_error('legacy typedef. use typedef: {} {}[{}];' - .format(p[1], p[2], p[4]), - self._token_coord(p, 1)) + if "typeonly" in p[1]: + self._parse_error( + "legacy typedef. use typedef: {} {}[{}];".format(p[1], p[2], p[4]), + self._token_coord(p, 1), + ) else: p[0] = Define(p[3], p[1], p[5]) def p_flist(self, p): - '''flist : flag - | flist flag''' + """flist : flag + | flist flag""" if len(p) == 2: p[0] = [p[1]] else: p[0] = p[1] + [p[2]] def p_flag(self, p): - '''flag : MANUAL_PRINT - | MANUAL_ENDIAN - | DONT_TRACE - | TYPEONLY - | AUTOENDIAN - | AUTOREPLY''' + """flag : MANUAL_PRINT + | MANUAL_ENDIAN + | DONT_TRACE + | TYPEONLY + | AUTOENDIAN + | AUTOREPLY""" if len(p) == 1: return p[0] = p[1] def p_typedef(self, p): - '''typedef : TYPEDEF ID '{' block_statements_opt '}' ';' ''' + """typedef : TYPEDEF ID '{' block_statements_opt '}' ';'""" p[0] = Typedef(p[2], [], p[4]) def p_typedef_flist(self, p): - '''typedef : flist TYPEDEF ID '{' block_statements_opt '}' ';' ''' + """typedef : flist TYPEDEF ID '{' block_statements_opt '}' ';'""" p[0] = Typedef(p[3], p[1], p[5]) def p_typedef_alias(self, p): - '''typedef : TYPEDEF declaration ''' + """typedef : TYPEDEF declaration""" p[0] = Using(p[2].fieldname, [], p[2]) def p_typedef_alias_flist(self, p): - '''typedef : flist TYPEDEF declaration ''' + """typedef : flist TYPEDEF declaration""" p[0] = Using(p[3].fieldname, p[1], p[3]) def p_block_statements_opt(self, p): - '''block_statements_opt : block_statements ''' + """block_statements_opt : block_statements""" p[0] = p[1] def p_block_statements(self, p): - '''block_statements : block_statement - | block_statements block_statement''' + """block_statements : block_statement + | block_statements block_statement""" if len(p) == 2: p[0] = [p[1]] else: p[0] = p[1] + [p[2]] def p_block_statement(self, p): - '''block_statement : declaration - | option ''' + """block_statement : declaration + | option""" p[0] = p[1] def p_enum_statements(self, p): - '''enum_statements : enum_statement - | enum_statements enum_statement''' + """enum_statements : enum_statement + | enum_statements enum_statement""" if len(p) == 2: p[0] = [p[1]] else: p[0] = p[1] + [p[2]] def p_enum_statement(self, p): - '''enum_statement : ID '=' NUM ',' - | ID ',' - | ID '[' field_options ']' ',' - | ID '=' NUM '[' field_options ']' ',' ''' + """enum_statement : ID '=' NUM ',' + | ID ',' + | ID '[' field_options ']' ',' + | ID '=' NUM '[' field_options ']' ','""" if len(p) == 3: - p[0] = {'id': p[1]} + p[0] = {"id": p[1]} elif len(p) == 5: - p[0] = {'id': p[1], 'value': p[3]} + p[0] = {"id": p[1], "value": p[3]} elif len(p) == 6: - p[0] = {'id': p[1], 'option': p[3]} + p[0] = {"id": p[1], "option": p[3]} elif len(p) == 8: - p[0] = {'id': p[1], 'value': p[3], 'option': p[5]} + p[0] = {"id": p[1], "value": p[3], "option": p[5]} else: - self._parse_error('ERROR', self._token_coord(p, 1)) + self._parse_error("ERROR", self._token_coord(p, 1)) def p_field_options(self, p): - '''field_options : field_option - | field_options field_option''' + """field_options : field_option + | field_options field_option""" if len(p) == 2: p[0] = p[1] else: p[0] = {**p[1], **p[2]} def p_field_option(self, p): - '''field_option : ID - | ID '=' assignee ',' - | ID '=' assignee + """field_option : ID + | ID '=' assignee ',' + | ID '=' assignee - ''' + """ if len(p) == 2: p[0] = {p[1]: None} else: p[0] = {p[1]: p[3]} def p_variable_name(self, p): - '''variable_name : ID - | TYPE - | SEVERITY - | DESCRIPTION - | COUNTERS - | PATHS - ''' + """variable_name : ID + | TYPE + | SEVERITY + | DESCRIPTION + | COUNTERS + | PATHS + """ p[0] = p[1] def p_declaration(self, p): - '''declaration : type_specifier variable_name ';' - | type_specifier variable_name '[' field_options ']' ';' - ''' + """declaration : type_specifier variable_name ';' + | type_specifier variable_name '[' field_options ']' ';' + """ if len(p) == 7: p[0] = Field(p[1], p[2], p[4]) elif len(p) == 4: p[0] = Field(p[1], p[2]) else: - self._parse_error('ERROR', self._token_coord(p, 1)) + self._parse_error("ERROR", self._token_coord(p, 1)) self.fields.append(p[2]) def p_declaration_array_vla(self, p): - '''declaration : type_specifier variable_name '[' ']' ';' ''' + """declaration : type_specifier variable_name '[' ']' ';'""" p[0] = Array(p[1], p[2], 0, modern_vla=True) def p_declaration_array(self, p): - '''declaration : type_specifier variable_name '[' NUM ']' ';' - | type_specifier variable_name '[' ID ']' ';' ''' + """declaration : type_specifier variable_name '[' NUM ']' ';' + | type_specifier variable_name '[' ID ']' ';'""" if len(p) != 7: return self._parse_error( - 'array: %s' % p.value, - self._coord(lineno=p.lineno)) + "array: %s" % p.value, self._coord(lineno=p.lineno) + ) # Make this error later if type(p[4]) is int and p[4] == 0: # XXX: Line number is wrong - self._parse_warning('Old Style VLA: {} {}[{}];' - .format(p[1], p[2], p[4]), - self._token_coord(p, 1)) + self._parse_warning( + "Old Style VLA: {} {}[{}];".format(p[1], p[2], p[4]), + self._token_coord(p, 1), + ) if type(p[4]) is str and p[4] not in self.fields: # Verify that length field exists - self._parse_error('Missing length field: {} {}[{}];' - .format(p[1], p[2], p[4]), - self._token_coord(p, 1)) + self._parse_error( + "Missing length field: {} {}[{}];".format(p[1], p[2], p[4]), + self._token_coord(p, 1), + ) p[0] = Array(p[1], p[2], p[4]) def p_option(self, p): - '''option : OPTION ID '=' assignee ';' - | OPTION ID ';' ''' + """option : OPTION ID '=' assignee ';' + | OPTION ID ';'""" if len(p) == 4: p[0] = Option(p[2]) else: p[0] = Option(p[2], p[4]) def p_assignee(self, p): - '''assignee : NUM - | TRUE - | FALSE - | STRING_LITERAL ''' + """assignee : NUM + | TRUE + | FALSE + | STRING_LITERAL""" p[0] = p[1] def p_type_specifier(self, p): - '''type_specifier : U8 - | U16 - | U32 - | U64 - | I8 - | I16 - | I32 - | I64 - | F64 - | BOOL - | STRING''' + """type_specifier : U8 + | U16 + | U32 + | U64 + | I8 + | I16 + | I32 + | I64 + | F64 + | BOOL + | STRING""" p[0] = p[1] # Do a second pass later to verify that user defined types are defined def p_typedef_specifier(self, p): - '''type_specifier : ID ''' + """type_specifier : ID""" if p[1] not in global_types: - self._parse_error('Undefined type: {}'.format(p[1]), - self._token_coord(p, 1)) + self._parse_error( + "Undefined type: {}".format(p[1]), self._token_coord(p, 1) + ) p[0] = p[1] def p_union(self, p): - '''union : UNION ID '{' block_statements_opt '}' ';' ''' + """union : UNION ID '{' block_statements_opt '}' ';'""" p[0] = Union(p[2], [], p[4]) def p_union_flist(self, p): - '''union : flist UNION ID '{' block_statements_opt '}' ';' ''' + """union : flist UNION ID '{' block_statements_opt '}' ';'""" p[0] = Union(p[3], p[1], p[5]) # Error rule for syntax errors def p_error(self, p): if p: - self._parse_error( - 'before: %s' % p.value, - self._coord(lineno=p.lineno)) + self._parse_error("before: %s" % p.value, self._coord(lineno=p.lineno)) else: - self._parse_error('At end of input', self.filename) - + self._parse_error("At end of input", self.filename) -class VPPAPI(): - def __init__(self, debug=False, filename='', logger=None, revision=None): +class VPPAPI: + def __init__(self, debug=False, filename="", logger=None, revision=None): self.lexer = lex.lex(module=VPPAPILexer(filename), debug=debug) - self.parser = yacc.yacc(module=VPPAPIParser(filename, logger, - revision=revision), - write_tables=False, debug=debug) + self.parser = yacc.yacc( + module=VPPAPIParser(filename, logger, revision=revision), + write_tables=False, + debug=debug, + ) self.logger = logger self.revision = revision self.filename = filename @@ -979,38 +982,40 @@ class VPPAPI(): def parse_filename(self, filename, debug=0): if self.revision: - git_show = 'git show {}:{}'.format(self.revision, filename) - proc = Popen(git_show.split(), stdout=PIPE, encoding='utf-8') + git_show = "git show {}:{}".format(self.revision, filename) + proc = Popen(git_show.split(), stdout=PIPE, encoding="utf-8") try: data, errs = proc.communicate() if proc.returncode != 0: - print('File not found: {}:{}' - .format(self.revision, filename), file=sys.stderr) + print( + "File not found: {}:{}".format(self.revision, filename), + file=sys.stderr, + ) sys.exit(2) return self.parse_string(data, debug=debug) except Exception: sys.exit(3) else: try: - with open(filename, encoding='utf-8') as fd: + with open(filename, encoding="utf-8") as fd: return self.parse_fd(fd, None) except FileNotFoundError: - print('File not found: {}'.format(filename), file=sys.stderr) + print("File not found: {}".format(filename), file=sys.stderr) sys.exit(2) def process(self, objs): s = {} - s['Option'] = {} - s['Define'] = [] - s['Service'] = [] - s['types'] = [] - s['Import'] = [] - s['Counters'] = [] - s['Paths'] = [] + s["Option"] = {} + s["Define"] = [] + s["Service"] = [] + s["types"] = [] + s["Import"] = [] + s["Counters"] = [] + s["Paths"] = [] crc = 0 for o in objs: try: - crc = binascii.crc32(o.crc, crc) & 0xffffffff + crc = binascii.crc32(o.crc, crc) & 0xFFFFFFFF except AttributeError: pass @@ -1021,82 +1026,84 @@ class VPPAPI(): else: o.process(s) - msgs = {d.name: d for d in s['Define']} - svcs = {s.caller: s for s in s['Service']} - replies = {s.reply: s for s in s['Service']} + msgs = {d.name: d for d in s["Define"]} + svcs = {s.caller: s for s in s["Service"]} + replies = {s.reply: s for s in s["Service"]} seen_services = {} - s['file_crc'] = crc + s["file_crc"] = crc for service in svcs: if service not in msgs: raise ValueError( - 'Service definition refers to unknown message' - ' definition: {}'.format(service)) - if svcs[service].reply != 'null' and \ - svcs[service].reply not in msgs: - raise ValueError('Service definition refers to unknown message' - ' definition in reply: {}' - .format(svcs[service].reply)) + "Service definition refers to unknown message" + " definition: {}".format(service) + ) + if svcs[service].reply != "null" and svcs[service].reply not in msgs: + raise ValueError( + "Service definition refers to unknown message" + " definition in reply: {}".format(svcs[service].reply) + ) if service in replies: - raise ValueError('Service definition refers to message' - ' marked as reply: {}'.format(service)) + raise ValueError( + "Service definition refers to message" + " marked as reply: {}".format(service) + ) for event in svcs[service].events: if event not in msgs: - raise ValueError('Service definition refers to unknown ' - 'event: {} in message: {}' - .format(event, service)) + raise ValueError( + "Service definition refers to unknown " + "event: {} in message: {}".format(event, service) + ) seen_services[event] = True # Create services implicitly for d in msgs: if d in seen_services: continue - if d.endswith('_reply'): + if d.endswith("_reply"): if d[:-6] in svcs: continue if d[:-6] not in msgs: - raise ValueError('{} missing calling message' - .format(d)) + raise ValueError("{} missing calling message".format(d)) continue - if d.endswith('_dump'): + if d.endswith("_dump"): if d in svcs: continue - if d[:-5]+'_details' in msgs: - s['Service'].append(Service(d, d[:-5]+'_details', - stream=True)) + if d[:-5] + "_details" in msgs: + s["Service"].append(Service(d, d[:-5] + "_details", stream=True)) else: - raise ValueError('{} missing details message' - .format(d)) + raise ValueError("{} missing details message".format(d)) continue - if d.endswith('_details'): - if d[:-8]+'_get' in msgs: - if d[:-8]+'_get' in svcs: + if d.endswith("_details"): + if d[:-8] + "_get" in msgs: + if d[:-8] + "_get" in svcs: continue - raise ValueError('{} should be in a stream service' - .format(d[:-8]+'_get')) - if d[:-8]+'_dump' in msgs: + raise ValueError( + "{} should be in a stream service".format(d[:-8] + "_get") + ) + if d[:-8] + "_dump" in msgs: continue - raise ValueError('{} missing dump or get message' - .format(d)) + raise ValueError("{} missing dump or get message".format(d)) if d in svcs: continue - if d+'_reply' in msgs: - s['Service'].append(Service(d, d+'_reply')) + if d + "_reply" in msgs: + s["Service"].append(Service(d, d + "_reply")) else: raise ValueError( - '{} missing reply message ({}) or service definition' - .format(d, d+'_reply')) + "{} missing reply message ({}) or service definition".format( + d, d + "_reply" + ) + ) return s def process_imports(self, objs, in_import, result): # -> List for o in objs: # Only allow the following object types from imported file - if in_import and not isinstance(o, (Enum, Import, Typedef, - Union, Using)): + if in_import and not isinstance(o, (Enum, Import, Typedef, Union, Using)): continue if isinstance(o, Import): result.append(o) @@ -1109,7 +1116,7 @@ class VPPAPI(): # Add message ids to each message. def add_msg_id(s): for o in s: - o.block.insert(0, Field('u16', '_vl_msg_id')) + o.block.insert(0, Field("u16", "_vl_msg_id")) return s @@ -1129,11 +1136,11 @@ def dirlist_get(): def foldup_blocks(block, crc): for b in block: # Look up CRC in user defined types - if b.fieldtype.startswith('vl_api_'): + if b.fieldtype.startswith("vl_api_"): # Recursively t = global_types[b.fieldtype] try: - crc = binascii.crc32(t.crc, crc) & 0xffffffff + crc = binascii.crc32(t.crc, crc) & 0xFFFFFFFF crc = foldup_blocks(t.block, crc) except AttributeError: pass @@ -1142,34 +1149,43 @@ def foldup_blocks(block, crc): def foldup_crcs(s): for f in s: - f.crc = foldup_blocks(f.block, - binascii.crc32(f.crc) & 0xffffffff) + f.crc = foldup_blocks(f.block, binascii.crc32(f.crc) & 0xFFFFFFFF) # # Main # def main(): - if sys.version_info < (3, 5,): - log.exception('vppapigen requires a supported version of python. ' - 'Please use version 3.5 or greater. ' - 'Using %s', sys.version) + if sys.version_info < ( + 3, + 5, + ): + log.exception( + "vppapigen requires a supported version of python. " + "Please use version 3.5 or greater. " + "Using %s", + sys.version, + ) return 1 - cliparser = argparse.ArgumentParser(description='VPP API generator') - cliparser.add_argument('--pluginpath', default="") - cliparser.add_argument('--includedir', action='append') - cliparser.add_argument('--outputdir', action='store') - cliparser.add_argument('--input') - cliparser.add_argument('--output', nargs='?', - type=argparse.FileType('w', encoding='UTF-8'), - default=sys.stdout) - - cliparser.add_argument('output_module', nargs='?', default='C') - cliparser.add_argument('--debug', action='store_true') - cliparser.add_argument('--show-name', nargs=1) - cliparser.add_argument('--git-revision', - help="Git revision to use for opening files") + cliparser = argparse.ArgumentParser(description="VPP API generator") + cliparser.add_argument("--pluginpath", default="") + cliparser.add_argument("--includedir", action="append") + cliparser.add_argument("--outputdir", action="store") + cliparser.add_argument("--input") + cliparser.add_argument( + "--output", + nargs="?", + type=argparse.FileType("w", encoding="UTF-8"), + default=sys.stdout, + ) + + cliparser.add_argument("output_module", nargs="?", default="C") + cliparser.add_argument("--debug", action="store_true") + cliparser.add_argument("--show-name", nargs=1) + cliparser.add_argument( + "--git-revision", help="Git revision to use for opening files" + ) args = cliparser.parse_args() dirlist_add(args.includedir) @@ -1182,7 +1198,7 @@ def main(): elif args.input: filename = args.input else: - filename = '' + filename = "" if args.debug: logging.basicConfig(stream=sys.stdout, level=logging.WARNING) @@ -1195,36 +1211,34 @@ def main(): from importlib.machinery import SourceFileLoader # Default path - pluginpath = '' + pluginpath = "" if not args.pluginpath: cand = [] cand.append(os.path.dirname(os.path.realpath(__file__))) - cand.append(os.path.dirname(os.path.realpath(__file__)) + - '/../share/vpp/') + cand.append(os.path.dirname(os.path.realpath(__file__)) + "/../share/vpp/") for c in cand: - c += '/' - if os.path.isfile('{}vppapigen_{}.py' - .format(c, args.output_module.lower())): + c += "/" + if os.path.isfile( + "{}vppapigen_{}.py".format(c, args.output_module.lower()) + ): pluginpath = c break else: - pluginpath = args.pluginpath + '/' - if pluginpath == '': - log.exception('Output plugin not found') + pluginpath = args.pluginpath + "/" + if pluginpath == "": + log.exception("Output plugin not found") return 1 - module_path = '{}vppapigen_{}.py'.format(pluginpath, - args.output_module.lower()) + module_path = "{}vppapigen_{}.py".format(pluginpath, args.output_module.lower()) try: - plugin = SourceFileLoader(args.output_module, - module_path).load_module() + plugin = SourceFileLoader(args.output_module, module_path).load_module() except Exception as err: - log.exception('Error importing output plugin: %s, %s', - module_path, err) + log.exception("Error importing output plugin: %s, %s", module_path, err) return 1 - parser = VPPAPI(debug=args.debug, filename=filename, logger=log, - revision=args.git_revision) + parser = VPPAPI( + debug=args.debug, filename=filename, logger=log, revision=args.git_revision + ) try: if not args.input: @@ -1232,7 +1246,7 @@ def main(): else: parsed_objects = parser.parse_filename(args.input, log) except ParseError as e: - print('Parse error: ', e, file=sys.stderr) + print("Parse error: ", e, file=sys.stderr) sys.exit(1) # Build a list of objects. Hash of lists. @@ -1250,32 +1264,33 @@ def main(): else: s = parser.process(parsed_objects) imports = parser.process_imports(parsed_objects, False, result) - s['imported'] = parser.process(imports) + s["imported"] = parser.process(imports) # Add msg_id field - s['Define'] = add_msg_id(s['Define']) + s["Define"] = add_msg_id(s["Define"]) # Fold up CRCs - foldup_crcs(s['Define']) + foldup_crcs(s["Define"]) # # Debug if args.debug: import pprint + pp = pprint.PrettyPrinter(indent=4, stream=sys.stderr) - for t in s['Define']: + for t in s["Define"]: pp.pprint([t.name, t.flags, t.block]) - for t in s['types']: + for t in s["types"]: pp.pprint([t.name, t.block]) result = plugin.run(args, filename, s) if result: print(result, file=args.output) else: - log.exception('Running plugin failed: %s %s', filename, result) + log.exception("Running plugin failed: %s %s", filename, result) return 1 return 0 -if __name__ == '__main__': +if __name__ == "__main__": sys.exit(main()) diff --git a/src/tools/vppapigen/vppapigen_c.py b/src/tools/vppapigen/vppapigen_c.py index f93e89843a3..fdbb7270a8a 100644 --- a/src/tools/vppapigen/vppapigen_c.py +++ b/src/tools/vppapigen/vppapigen_c.py @@ -18,10 +18,10 @@ # binary API format # -''' +""" This module creates C code for core VPP, VPP plugins and client side VAT and VAT2 tests. -''' +""" import datetime import itertools @@ -35,98 +35,103 @@ process_imports = False ############################################################################### -class ToJSON(): - '''Class to generate functions converting from VPP binary API to JSON.''' +class ToJSON: + """Class to generate functions converting from VPP binary API to JSON.""" + _dispatch = {} - noprint_fields = {'_vl_msg_id': None, - 'client_index': None, - 'context': None} - is_number = {'u8': None, - 'i8': None, - 'u16': None, - 'i16': None, - 'u32': None, - 'i32': None, - 'u64': None, - 'i64': None, - 'f64': None, - } + noprint_fields = {"_vl_msg_id": None, "client_index": None, "context": None} + is_number = { + "u8": None, + "i8": None, + "u16": None, + "i16": None, + "u32": None, + "i32": None, + "u64": None, + "i64": None, + "f64": None, + } def __init__(self, module, types, defines, imported_types, stream): self.stream = stream self.module = module self.defines = defines self.types = types - self.types_hash = {'vl_api_'+d.name+'_t': - d for d in types + imported_types} + self.types_hash = {"vl_api_" + d.name + "_t": d for d in types + imported_types} self.defines_hash = {d.name: d for d in defines} def header(self): - '''Output the top boilerplate.''' + """Output the top boilerplate.""" write = self.stream.write - write('#ifndef included_{}_api_tojson_h\n'.format(self.module)) - write('#define included_{}_api_tojson_h\n'.format(self.module)) - write('#include <vppinfra/cJSON.h>\n\n') - write('#include <vppinfra/jsonformat.h>\n\n') - if self.module == 'interface_types': - write('#define vl_printfun\n') - write('#include <vnet/interface_types.api.h>\n\n') + write("#ifndef included_{}_api_tojson_h\n".format(self.module)) + write("#define included_{}_api_tojson_h\n".format(self.module)) + write("#include <vppinfra/cJSON.h>\n\n") + write("#include <vppinfra/jsonformat.h>\n\n") + if self.module == "interface_types": + write("#define vl_printfun\n") + write("#include <vnet/interface_types.api.h>\n\n") def footer(self): - '''Output the bottom boilerplate.''' + """Output the bottom boilerplate.""" write = self.stream.write - write('#endif\n') + write("#endif\n") def get_base_type(self, t): vt_type = None try: vt = self.types_hash[t] - if vt.type == 'Using' and 'length' not in vt.alias: - vt_type = vt.alias['type'] + if vt.type == "Using" and "length" not in vt.alias: + vt_type = vt.alias["type"] except KeyError: vt = t return vt, vt_type def get_json_func(self, t): - '''Given the type, returns the function to use to create a - cJSON object''' + """Given the type, returns the function to use to create a + cJSON object""" vt, vt_type = self.get_base_type(t) if t in self.is_number or vt_type in self.is_number: - return 'cJSON_AddNumberToObject', '', False - if t == 'bool': - return 'cJSON_AddBoolToObject', '', False + return "cJSON_AddNumberToObject", "", False + if t == "bool": + return "cJSON_AddBoolToObject", "", False # Lookup type name check if it's enum - if vt.type == 'Enum' or vt.type == 'EnumFlag': - return '{t}_tojson'.format(t=t), '', True - return '{t}_tojson'.format(t=t), '&', True + if vt.type == "Enum" or vt.type == "EnumFlag": + return "{t}_tojson".format(t=t), "", True + return "{t}_tojson".format(t=t), "&", True def get_json_array_func(self, t): - '''Given a type returns the function to create a cJSON object - for arrays.''' + """Given a type returns the function to create a cJSON object + for arrays.""" if t in self.is_number: - return 'cJSON_CreateNumber', '' - if t == 'bool': - return 'cJSON_CreateBool', '' + return "cJSON_CreateNumber", "" + if t == "bool": + return "cJSON_CreateBool", "" vt, vt_type = self.get_base_type(t) - if vt.type == 'Enum' or vt.type == 'EnumFlag': - return '{t}_tojson'.format(t=t), '' - return '{t}_tojson'.format(t=t), '&' + if vt.type == "Enum" or vt.type == "EnumFlag": + return "{t}_tojson".format(t=t), "" + return "{t}_tojson".format(t=t), "&" def print_string(self, o): - '''Create cJSON object from vl_api_string_t''' + """Create cJSON object from vl_api_string_t""" write = self.stream.write if o.modern_vla: - write(' vl_api_string_cJSON_AddToObject(o, "{n}", &a->{n});\n' - .format(n=o.fieldname)) + write( + ' vl_api_string_cJSON_AddToObject(o, "{n}", &a->{n});\n'.format( + n=o.fieldname + ) + ) else: - write(' cJSON_AddStringToObject(o, "{n}", (char *)a->{n});\n' - .format(n=o.fieldname)) + write( + ' cJSON_AddStringToObject(o, "{n}", (char *)a->{n});\n'.format( + n=o.fieldname + ) + ) def print_field(self, o): - '''Called for every field in a typedef or define.''' + """Called for every field in a typedef or define.""" write = self.stream.write if o.fieldname in self.noprint_fields: return @@ -134,19 +139,21 @@ class ToJSON(): f, p, newobj = self.get_json_func(o.fieldtype) if newobj: - write(' cJSON_AddItemToObject(o, "{n}", {f}({p}a->{n}));\n' - .format(f=f, p=p, n=o.fieldname)) + write( + ' cJSON_AddItemToObject(o, "{n}", {f}({p}a->{n}));\n'.format( + f=f, p=p, n=o.fieldname + ) + ) else: - write(' {f}(o, "{n}", {p}a->{n});\n' - .format(f=f, p=p, n=o.fieldname)) + write(' {f}(o, "{n}", {p}a->{n});\n'.format(f=f, p=p, n=o.fieldname)) - _dispatch['Field'] = print_field + _dispatch["Field"] = print_field def print_array(self, o): - '''Converts a VPP API array to cJSON array.''' + """Converts a VPP API array to cJSON array.""" write = self.stream.write - forloop = '''\ + forloop = """\ {{ int i; cJSON *array = cJSON_AddArrayToObject(o, "{n}"); @@ -154,237 +161,263 @@ class ToJSON(): cJSON_AddItemToArray(array, {f}({p}a->{n}[i])); }} }} -''' +""" - if o.fieldtype == 'string': + if o.fieldtype == "string": self.print_string(o) return - lfield = 'a->' + o.lengthfield if o.lengthfield else o.length - if o.fieldtype == 'u8': - write(' {\n') + lfield = "a->" + o.lengthfield if o.lengthfield else o.length + if o.fieldtype == "u8": + write(" {\n") # What is length field doing here? - write(' u8 *s = format(0, "0x%U", format_hex_bytes, ' - '&a->{n}, {lfield});\n' - .format(n=o.fieldname, lfield=lfield)) - write(' cJSON_AddStringToObject(o, "{n}", (char *)s);\n' - .format(n=o.fieldname)) - write(' vec_free(s);\n') - write(' }\n') + write( + ' u8 *s = format(0, "0x%U", format_hex_bytes, ' + "&a->{n}, {lfield});\n".format(n=o.fieldname, lfield=lfield) + ) + write( + ' cJSON_AddStringToObject(o, "{n}", (char *)s);\n'.format( + n=o.fieldname + ) + ) + write(" vec_free(s);\n") + write(" }\n") return f, p = self.get_json_array_func(o.fieldtype) - write(forloop.format(lfield=lfield, - t=o.fieldtype, - n=o.fieldname, - f=f, - p=p)) + write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname, f=f, p=p)) - _dispatch['Array'] = print_array + _dispatch["Array"] = print_array def print_enum(self, o): - '''Create cJSON object (string) for VPP API enum''' + """Create cJSON object (string) for VPP API enum""" write = self.stream.write - write('static inline cJSON *vl_api_{name}_t_tojson ' - '(vl_api_{name}_t a) {{\n'.format(name=o.name)) + write( + "static inline cJSON *vl_api_{name}_t_tojson " + "(vl_api_{name}_t a) {{\n".format(name=o.name) + ) write(" switch(a) {\n") for b in o.block: write(" case %s:\n" % b[1]) write(' return cJSON_CreateString("{}");\n'.format(b[0])) write(' default: return cJSON_CreateString("Invalid ENUM");\n') - write(' }\n') - write(' return 0;\n') - write('}\n') + write(" }\n") + write(" return 0;\n") + write("}\n") - _dispatch['Enum'] = print_enum + _dispatch["Enum"] = print_enum def print_enum_flag(self, o): - '''Create cJSON object (string) for VPP API enum''' + """Create cJSON object (string) for VPP API enum""" write = self.stream.write - write('static inline cJSON *vl_api_{name}_t_tojson ' - '(vl_api_{name}_t a) {{\n'.format(name=o.name)) - write(' cJSON *array = cJSON_CreateArray();\n') + write( + "static inline cJSON *vl_api_{name}_t_tojson " + "(vl_api_{name}_t a) {{\n".format(name=o.name) + ) + write(" cJSON *array = cJSON_CreateArray();\n") for b in o.block: if b[1] == 0: - continue - write(' if (a & {})\n'.format(b[0])) + continue + write(" if (a & {})\n".format(b[0])) write( - ' cJSON_AddItemToArray(array, cJSON_CreateString("{}"));\n'.format(b[0])) - write(' return array;\n') - write('}\n') + ' cJSON_AddItemToArray(array, cJSON_CreateString("{}"));\n'.format( + b[0] + ) + ) + write(" return array;\n") + write("}\n") - _dispatch['EnumFlag'] = print_enum_flag + _dispatch["EnumFlag"] = print_enum_flag def print_typedef(self, o): - '''Create cJSON (dictionary) object from VPP API typedef''' + """Create cJSON (dictionary) object from VPP API typedef""" write = self.stream.write - write('static inline cJSON *vl_api_{name}_t_tojson ' - '(vl_api_{name}_t *a) {{\n'.format(name=o.name)) - write(' cJSON *o = cJSON_CreateObject();\n') + write( + "static inline cJSON *vl_api_{name}_t_tojson " + "(vl_api_{name}_t *a) {{\n".format(name=o.name) + ) + write(" cJSON *o = cJSON_CreateObject();\n") for t in o.block: self._dispatch[t.type](self, t) - write(' return o;\n') - write('}\n') + write(" return o;\n") + write("}\n") def print_define(self, o): - '''Create cJSON (dictionary) object from VPP API define''' + """Create cJSON (dictionary) object from VPP API define""" write = self.stream.write - write('static inline cJSON *vl_api_{name}_t_tojson ' - '(vl_api_{name}_t *a) {{\n'.format(name=o.name)) - write(' cJSON *o = cJSON_CreateObject();\n') - write(' cJSON_AddStringToObject(o, "_msgname", "{}");\n' - .format(o.name)) - write(' cJSON_AddStringToObject(o, "_crc", "{crc:08x}");\n' - .format(crc=o.crc)) + write( + "static inline cJSON *vl_api_{name}_t_tojson " + "(vl_api_{name}_t *a) {{\n".format(name=o.name) + ) + write(" cJSON *o = cJSON_CreateObject();\n") + write(' cJSON_AddStringToObject(o, "_msgname", "{}");\n'.format(o.name)) + write( + ' cJSON_AddStringToObject(o, "_crc", "{crc:08x}");\n'.format(crc=o.crc) + ) for t in o.block: self._dispatch[t.type](self, t) - write(' return o;\n') - write('}\n') + write(" return o;\n") + write("}\n") def print_using(self, o): - '''Create cJSON (dictionary) object from VPP API aliased type''' + """Create cJSON (dictionary) object from VPP API aliased type""" if o.manual_print: return write = self.stream.write - write('static inline cJSON *vl_api_{name}_t_tojson ' - '(vl_api_{name}_t *a) {{\n'.format(name=o.name)) - - write(' u8 *s = format(0, "%U", format_vl_api_{}_t, a);\n' - .format(o.name)) - write(' cJSON *o = cJSON_CreateString((char *)s);\n') - write(' vec_free(s);\n') - write(' return o;\n') - write('}\n') - - _dispatch['Typedef'] = print_typedef - _dispatch['Define'] = print_define - _dispatch['Using'] = print_using - _dispatch['Union'] = print_typedef + write( + "static inline cJSON *vl_api_{name}_t_tojson " + "(vl_api_{name}_t *a) {{\n".format(name=o.name) + ) + + write(' u8 *s = format(0, "%U", format_vl_api_{}_t, a);\n'.format(o.name)) + write(" cJSON *o = cJSON_CreateString((char *)s);\n") + write(" vec_free(s);\n") + write(" return o;\n") + write("}\n") + + _dispatch["Typedef"] = print_typedef + _dispatch["Define"] = print_define + _dispatch["Using"] = print_using + _dispatch["Union"] = print_typedef def generate_function(self, t): - '''Main entry point''' + """Main entry point""" write = self.stream.write if t.manual_print: - write('/* Manual print {} */\n'.format(t.name)) + write("/* Manual print {} */\n".format(t.name)) return self._dispatch[t.type](self, t) def generate_types(self): - '''Main entry point''' + """Main entry point""" for t in self.types: self.generate_function(t) def generate_defines(self): - '''Main entry point''' + """Main entry point""" for t in self.defines: self.generate_function(t) -class FromJSON(): - ''' +class FromJSON: + """ Parse JSON objects into VPP API binary message structures. - ''' + """ + _dispatch = {} - noprint_fields = {'_vl_msg_id': None, - 'client_index': None, - 'context': None} - is_number = {'u8': None, - 'i8': None, - 'u16': None, - 'i16': None, - 'u32': None, - 'i32': None, - 'u64': None, - 'i64': None, - 'f64': None, - } + noprint_fields = {"_vl_msg_id": None, "client_index": None, "context": None} + is_number = { + "u8": None, + "i8": None, + "u16": None, + "i16": None, + "u32": None, + "i32": None, + "u64": None, + "i64": None, + "f64": None, + } def __init__(self, module, types, defines, imported_types, stream): self.stream = stream self.module = module self.defines = defines self.types = types - self.types_hash = {'vl_api_'+d.name+'_t': - d for d in types + imported_types} + self.types_hash = {"vl_api_" + d.name + "_t": d for d in types + imported_types} self.defines_hash = {d.name: d for d in defines} def header(self): - '''Output the top boilerplate.''' + """Output the top boilerplate.""" write = self.stream.write - write('#ifndef included_{}_api_fromjson_h\n'.format(self.module)) - write('#define included_{}_api_fromjson_h\n'.format(self.module)) - write('#include <vppinfra/cJSON.h>\n\n') - write('#include <vppinfra/jsonformat.h>\n\n') + write("#ifndef included_{}_api_fromjson_h\n".format(self.module)) + write("#define included_{}_api_fromjson_h\n".format(self.module)) + write("#include <vppinfra/cJSON.h>\n\n") + write("#include <vppinfra/jsonformat.h>\n\n") write('#pragma GCC diagnostic ignored "-Wunused-label"\n') def is_base_type(self, t): - '''Check if a type is one of the VPP API base types''' + """Check if a type is one of the VPP API base types""" if t in self.is_number: return True - if t == 'bool': + if t == "bool": return True return False def footer(self): - '''Output the bottom boilerplate.''' + """Output the bottom boilerplate.""" write = self.stream.write - write('#endif\n') + write("#endif\n") def print_string(self, o, toplevel=False): - '''Convert JSON string to vl_api_string_t''' + """Convert JSON string to vl_api_string_t""" write = self.stream.write msgvar = "a" if toplevel else "*mp" msgsize = "l" if toplevel else "*len" if o.modern_vla: - write(' char *p = cJSON_GetStringValue(item);\n') - write(' size_t plen = strlen(p);\n') - write(' {msgvar} = cJSON_realloc({msgvar}, {msgsize} + plen, {msgsize});\n' - .format(msgvar=msgvar, msgsize=msgsize)) - write(' if ({msgvar} == 0) goto error;\n'.format(msgvar=msgvar)) - write(' vl_api_c_string_to_api_string(p, (void *){msgvar} + ' - '{msgsize} - sizeof(vl_api_string_t));\n' - .format(msgvar=msgvar, msgsize=msgsize)) - write(' {msgsize} += plen;\n'.format(msgsize=msgsize)) + write(" char *p = cJSON_GetStringValue(item);\n") + write(" size_t plen = strlen(p);\n") + write( + " {msgvar} = cJSON_realloc({msgvar}, {msgsize} + plen, {msgsize});\n".format( + msgvar=msgvar, msgsize=msgsize + ) + ) + write(" if ({msgvar} == 0) goto error;\n".format(msgvar=msgvar)) + write( + " vl_api_c_string_to_api_string(p, (void *){msgvar} + " + "{msgsize} - sizeof(vl_api_string_t));\n".format( + msgvar=msgvar, msgsize=msgsize + ) + ) + write(" {msgsize} += plen;\n".format(msgsize=msgsize)) else: - write(' strncpy_s((char *)a->{n}, sizeof(a->{n}), ' - 'cJSON_GetStringValue(item), sizeof(a->{n}) - 1);\n' - .format(n=o.fieldname)) + write( + " strncpy_s((char *)a->{n}, sizeof(a->{n}), " + "cJSON_GetStringValue(item), sizeof(a->{n}) - 1);\n".format( + n=o.fieldname + ) + ) def print_field(self, o, toplevel=False): - '''Called for every field in a typedef or define.''' + """Called for every field in a typedef or define.""" write = self.stream.write if o.fieldname in self.noprint_fields: return is_bt = self.is_base_type(o.fieldtype) - t = 'vl_api_{}'.format(o.fieldtype) if is_bt else o.fieldtype + t = "vl_api_{}".format(o.fieldtype) if is_bt else o.fieldtype msgvar = "(void **)&a" if toplevel else "mp" msgsize = "&l" if toplevel else "len" if is_bt: - write(' vl_api_{t}_fromjson(item, &a->{n});\n' - .format(t=o.fieldtype, n=o.fieldname)) + write( + " vl_api_{t}_fromjson(item, &a->{n});\n".format( + t=o.fieldtype, n=o.fieldname + ) + ) else: - write(' if ({t}_fromjson({msgvar}, ' - '{msgsize}, item, &a->{n}) < 0) goto error;\n' - .format(t=t, n=o.fieldname, msgvar=msgvar, msgsize=msgsize)) + write( + " if ({t}_fromjson({msgvar}, " + "{msgsize}, item, &a->{n}) < 0) goto error;\n".format( + t=t, n=o.fieldname, msgvar=msgvar, msgsize=msgsize + ) + ) - _dispatch['Field'] = print_field + _dispatch["Field"] = print_field def print_array(self, o, toplevel=False): - '''Convert JSON array to VPP API array''' + """Convert JSON array to VPP API array""" write = self.stream.write - forloop = '''\ + forloop = """\ {{ int i; cJSON *array = cJSON_GetObjectItem(o, "{n}"); @@ -395,8 +428,8 @@ class FromJSON(): {call} }} }} -''' - forloop_vla = '''\ +""" + forloop_vla = """\ {{ int i; cJSON *array = cJSON_GetObjectItem(o, "{n}"); @@ -410,271 +443,296 @@ class FromJSON(): {call} }} }} -''' +""" t = o.fieldtype - if o.fieldtype == 'string': + if o.fieldtype == "string": self.print_string(o, toplevel) return - lfield = 'a->' + o.lengthfield if o.lengthfield else o.length + lfield = "a->" + o.lengthfield if o.lengthfield else o.length msgvar = "(void **)&a" if toplevel else "mp" realloc = "a" if toplevel else "*mp" msgsize = "l" if toplevel else "*len" - if o.fieldtype == 'u8': + if o.fieldtype == "u8": if o.lengthfield: - write(' s = u8string_fromjson(o, "{}");\n' - .format(o.fieldname)) - write(' if (!s) goto error;\n') - write(' {} = vec_len(s);\n'.format(lfield)) - - write(' {realloc} = cJSON_realloc({realloc}, {msgsize} + ' - 'vec_len(s), {msgsize});\n'.format(msgvar=msgvar, msgsize=msgsize, realloc=realloc)) - write(' memcpy((void *){realloc} + {msgsize}, s, ' - 'vec_len(s));\n'.format(realloc=realloc, msgsize=msgsize)) - write(' {msgsize} += vec_len(s);\n'.format(msgsize=msgsize)) - - write(' vec_free(s);\n') + write(' s = u8string_fromjson(o, "{}");\n'.format(o.fieldname)) + write(" if (!s) goto error;\n") + write(" {} = vec_len(s);\n".format(lfield)) + + write( + " {realloc} = cJSON_realloc({realloc}, {msgsize} + " + "vec_len(s), {msgsize});\n".format( + msgvar=msgvar, msgsize=msgsize, realloc=realloc + ) + ) + write( + " memcpy((void *){realloc} + {msgsize}, s, " + "vec_len(s));\n".format(realloc=realloc, msgsize=msgsize) + ) + write(" {msgsize} += vec_len(s);\n".format(msgsize=msgsize)) + + write(" vec_free(s);\n") else: - write(' if (u8string_fromjson2(o, "{n}", a->{n}) < 0) goto error;\n' - .format(n=o.fieldname)) + write( + ' if (u8string_fromjson2(o, "{n}", a->{n}) < 0) goto error;\n'.format( + n=o.fieldname + ) + ) return is_bt = self.is_base_type(o.fieldtype) if o.lengthfield: if is_bt: - call = ('vl_api_{t}_fromjson(e, &d[i]);' - .format(t=o.fieldtype)) + call = "vl_api_{t}_fromjson(e, &d[i]);".format(t=o.fieldtype) else: - call = ('if ({t}_fromjson({msgvar}, len, e, &d[i]) < 0) goto error; ' - .format(t=o.fieldtype, msgvar=msgvar)) - write(forloop_vla.format(lfield=lfield, - t=o.fieldtype, - n=o.fieldname, - call=call, - realloc=realloc, - msgsize=msgsize)) + call = "if ({t}_fromjson({msgvar}, len, e, &d[i]) < 0) goto error; ".format( + t=o.fieldtype, msgvar=msgvar + ) + write( + forloop_vla.format( + lfield=lfield, + t=o.fieldtype, + n=o.fieldname, + call=call, + realloc=realloc, + msgsize=msgsize, + ) + ) else: if is_bt: - call = ('vl_api_{t}_fromjson(e, &a->{n}[i]);' - .format(t=t, n=o.fieldname)) + call = "vl_api_{t}_fromjson(e, &a->{n}[i]);".format(t=t, n=o.fieldname) else: - call = ('if ({}_fromjson({}, len, e, &a->{}[i]) < 0) goto error;' - .format(t, msgvar, o.fieldname)) - write(forloop.format(lfield=lfield, - t=t, - n=o.fieldname, - call=call, - msgvar=msgvar, - realloc=realloc, - msgsize=msgsize)) - - _dispatch['Array'] = print_array + call = "if ({}_fromjson({}, len, e, &a->{}[i]) < 0) goto error;".format( + t, msgvar, o.fieldname + ) + write( + forloop.format( + lfield=lfield, + t=t, + n=o.fieldname, + call=call, + msgvar=msgvar, + realloc=realloc, + msgsize=msgsize, + ) + ) + + _dispatch["Array"] = print_array def print_enum(self, o): - '''Convert to JSON enum(string) to VPP API enum (int)''' + """Convert to JSON enum(string) to VPP API enum (int)""" write = self.stream.write - write('static inline int vl_api_{n}_t_fromjson' - '(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n' - .format(n=o.name)) - write(' char *p = cJSON_GetStringValue(o);\n') + write( + "static inline int vl_api_{n}_t_fromjson" + "(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n".format(n=o.name) + ) + write(" char *p = cJSON_GetStringValue(o);\n") for b in o.block: - write(' if (strcmp(p, "{}") == 0) {{*a = {}; return 0;}}\n' - .format(b[0], b[1])) - write(' *a = 0;\n') - write(' return -1;\n') - write('}\n') + write( + ' if (strcmp(p, "{}") == 0) {{*a = {}; return 0;}}\n'.format( + b[0], b[1] + ) + ) + write(" *a = 0;\n") + write(" return -1;\n") + write("}\n") - _dispatch['Enum'] = print_enum + _dispatch["Enum"] = print_enum def print_enum_flag(self, o): - '''Convert to JSON enum(string) to VPP API enum (int)''' + """Convert to JSON enum(string) to VPP API enum (int)""" write = self.stream.write - write('static inline int vl_api_{n}_t_fromjson ' - '(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n' - .format(n=o.name)) - write(' int i;\n') - write(' *a = 0;\n') - write(' for (i = 0; i < cJSON_GetArraySize(o); i++) {\n') - write(' cJSON *e = cJSON_GetArrayItem(o, i);\n') - write(' char *p = cJSON_GetStringValue(e);\n') - write(' if (!p) return -1;\n') + write( + "static inline int vl_api_{n}_t_fromjson " + "(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n".format(n=o.name) + ) + write(" int i;\n") + write(" *a = 0;\n") + write(" for (i = 0; i < cJSON_GetArraySize(o); i++) {\n") + write(" cJSON *e = cJSON_GetArrayItem(o, i);\n") + write(" char *p = cJSON_GetStringValue(e);\n") + write(" if (!p) return -1;\n") for b in o.block: - write(' if (strcmp(p, "{}") == 0) *a |= {};\n' - .format(b[0], b[1])) - write(' }\n') - write(' return 0;\n') - write('}\n') + write(' if (strcmp(p, "{}") == 0) *a |= {};\n'.format(b[0], b[1])) + write(" }\n") + write(" return 0;\n") + write("}\n") - _dispatch['EnumFlag'] = print_enum_flag + _dispatch["EnumFlag"] = print_enum_flag def print_typedef(self, o): - '''Convert from JSON object to VPP API binary representation''' + """Convert from JSON object to VPP API binary representation""" write = self.stream.write - write('static inline int vl_api_{name}_t_fromjson (void **mp, ' - 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n' - .format(name=o.name)) - write(' cJSON *item __attribute__ ((unused));\n') - write(' u8 *s __attribute__ ((unused));\n') + write( + "static inline int vl_api_{name}_t_fromjson (void **mp, " + "int *len, cJSON *o, vl_api_{name}_t *a) {{\n".format(name=o.name) + ) + write(" cJSON *item __attribute__ ((unused));\n") + write(" u8 *s __attribute__ ((unused));\n") for t in o.block: - if t.type == 'Field' and t.is_lengthfield: + if t.type == "Field" and t.is_lengthfield: continue - write('\n item = cJSON_GetObjectItem(o, "{}");\n' - .format(t.fieldname)) - write(' if (!item) goto error;\n') + write('\n item = cJSON_GetObjectItem(o, "{}");\n'.format(t.fieldname)) + write(" if (!item) goto error;\n") self._dispatch[t.type](self, t) - write('\n return 0;\n') - write('\n error:\n') - write(' return -1;\n') - write('}\n') + write("\n return 0;\n") + write("\n error:\n") + write(" return -1;\n") + write("}\n") def print_union(self, o): - '''Convert JSON object to VPP API binary union''' + """Convert JSON object to VPP API binary union""" write = self.stream.write - write('static inline int vl_api_{name}_t_fromjson (void **mp, ' - 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n' - .format(name=o.name)) - write(' cJSON *item __attribute__ ((unused));\n') - write(' u8 *s __attribute__ ((unused));\n') + write( + "static inline int vl_api_{name}_t_fromjson (void **mp, " + "int *len, cJSON *o, vl_api_{name}_t *a) {{\n".format(name=o.name) + ) + write(" cJSON *item __attribute__ ((unused));\n") + write(" u8 *s __attribute__ ((unused));\n") for t in o.block: - if t.type == 'Field' and t.is_lengthfield: + if t.type == "Field" and t.is_lengthfield: continue - write(' item = cJSON_GetObjectItem(o, "{}");\n' - .format(t.fieldname)) - write(' if (item) {\n') + write(' item = cJSON_GetObjectItem(o, "{}");\n'.format(t.fieldname)) + write(" if (item) {\n") self._dispatch[t.type](self, t) - write(' };\n') - write('\n return 0;\n') - write('\n error:\n') - write(' return -1;\n') - write('}\n') + write(" };\n") + write("\n return 0;\n") + write("\n error:\n") + write(" return -1;\n") + write("}\n") def print_define(self, o): - '''Convert JSON object to VPP API message''' + """Convert JSON object to VPP API message""" write = self.stream.write error = 0 - write('static inline vl_api_{name}_t *vl_api_{name}_t_fromjson ' - '(cJSON *o, int *len) {{\n'.format(name=o.name)) - write(' cJSON *item __attribute__ ((unused));\n') - write(' u8 *s __attribute__ ((unused));\n') - write(' int l = sizeof(vl_api_{}_t);\n'.format(o.name)) - write(' vl_api_{}_t *a = cJSON_malloc(l);\n'.format(o.name)) - write('\n') + write( + "static inline vl_api_{name}_t *vl_api_{name}_t_fromjson " + "(cJSON *o, int *len) {{\n".format(name=o.name) + ) + write(" cJSON *item __attribute__ ((unused));\n") + write(" u8 *s __attribute__ ((unused));\n") + write(" int l = sizeof(vl_api_{}_t);\n".format(o.name)) + write(" vl_api_{}_t *a = cJSON_malloc(l);\n".format(o.name)) + write("\n") for t in o.block: if t.fieldname in self.noprint_fields: continue - if t.type == 'Field' and t.is_lengthfield: + if t.type == "Field" and t.is_lengthfield: continue - write(' item = cJSON_GetObjectItem(o, "{}");\n' - .format(t.fieldname)) - write(' if (!item) goto error;\n') + write(' item = cJSON_GetObjectItem(o, "{}");\n'.format(t.fieldname)) + write(" if (!item) goto error;\n") error += 1 self._dispatch[t.type](self, t, toplevel=True) - write('\n') + write("\n") - write(' *len = l;\n') - write(' return a;\n') + write(" *len = l;\n") + write(" return a;\n") if error: - write('\n error:\n') - write(' cJSON_free(a);\n') - write(' return 0;\n') - write('}\n') + write("\n error:\n") + write(" cJSON_free(a);\n") + write(" return 0;\n") + write("}\n") def print_using(self, o): - '''Convert JSON field to VPP type alias''' + """Convert JSON field to VPP type alias""" write = self.stream.write if o.manual_print: return t = o.using - write('static inline int vl_api_{name}_t_fromjson (void **mp, ' - 'int *len, cJSON *o, vl_api_{name}_t *a) {{\n' - .format(name=o.name)) - if 'length' in o.alias: - if t.fieldtype != 'u8': - raise ValueError("Error in processing type {} for {}" - .format(t.fieldtype, o.name)) - write(' vl_api_u8_string_fromjson(o, (u8 *)a, {});\n' - .format(o.alias['length'])) + write( + "static inline int vl_api_{name}_t_fromjson (void **mp, " + "int *len, cJSON *o, vl_api_{name}_t *a) {{\n".format(name=o.name) + ) + if "length" in o.alias: + if t.fieldtype != "u8": + raise ValueError( + "Error in processing type {} for {}".format(t.fieldtype, o.name) + ) + write( + " vl_api_u8_string_fromjson(o, (u8 *)a, {});\n".format( + o.alias["length"] + ) + ) else: - write(' vl_api_{t}_fromjson(o, ({t} *)a);\n' - .format(t=t.fieldtype)) + write(" vl_api_{t}_fromjson(o, ({t} *)a);\n".format(t=t.fieldtype)) - write(' return 0;\n') - write('}\n') + write(" return 0;\n") + write("}\n") - _dispatch['Typedef'] = print_typedef - _dispatch['Define'] = print_define - _dispatch['Using'] = print_using - _dispatch['Union'] = print_union + _dispatch["Typedef"] = print_typedef + _dispatch["Define"] = print_define + _dispatch["Using"] = print_using + _dispatch["Union"] = print_union def generate_function(self, t): - '''Main entry point''' + """Main entry point""" write = self.stream.write if t.manual_print: - write('/* Manual print {} */\n'.format(t.name)) + write("/* Manual print {} */\n".format(t.name)) return self._dispatch[t.type](self, t) def generate_types(self): - '''Main entry point''' + """Main entry point""" for t in self.types: self.generate_function(t) def generate_defines(self): - '''Main entry point''' + """Main entry point""" for t in self.defines: self.generate_function(t) def generate_tojson(s, modulename, stream): - '''Generate all functions to convert from API to JSON''' + """Generate all functions to convert from API to JSON""" write = stream.write - write('/* Imported API files */\n') - for i in s['Import']: - f = i.filename.replace('plugins/', '') - write('#include <{}_tojson.h>\n'.format(f)) + write("/* Imported API files */\n") + for i in s["Import"]: + f = i.filename.replace("plugins/", "") + write("#include <{}_tojson.h>\n".format(f)) - pp = ToJSON(modulename, s['types'], s['Define'], s['imported']['types'], - stream) + pp = ToJSON(modulename, s["types"], s["Define"], s["imported"]["types"], stream) pp.header() pp.generate_types() pp.generate_defines() pp.footer() - return '' + return "" def generate_fromjson(s, modulename, stream): - '''Generate all functions to convert from JSON to API''' + """Generate all functions to convert from JSON to API""" write = stream.write - write('/* Imported API files */\n') - for i in s['Import']: - f = i.filename.replace('plugins/', '') - write('#include <{}_fromjson.h>\n'.format(f)) + write("/* Imported API files */\n") + for i in s["Import"]: + f = i.filename.replace("plugins/", "") + write("#include <{}_fromjson.h>\n".format(f)) - pp = FromJSON(modulename, s['types'], s['Define'], s['imported']['types'], - stream) + pp = FromJSON(modulename, s["types"], s["Define"], s["imported"]["types"], stream) pp.header() pp.generate_types() pp.generate_defines() pp.footer() - return '' + return "" + ############################################################################### DATESTRING = datetime.datetime.utcfromtimestamp( - int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))) -TOP_BOILERPLATE = '''\ + int(os.environ.get("SOURCE_DATE_EPOCH", time.time())) +) +TOP_BOILERPLATE = """\ /* * VLIB API definitions {datestring} * Input file: {input_filename} @@ -693,45 +751,47 @@ TOP_BOILERPLATE = '''\ #endif #define VL_API_PACKED(x) x __attribute__ ((packed)) -''' +""" -BOTTOM_BOILERPLATE = '''\ +BOTTOM_BOILERPLATE = """\ /****** API CRC (whole file) *****/ #ifdef vl_api_version vl_api_version({input_filename}, {file_crc:#08x}) #endif -''' +""" def msg_ids(s): - '''Generate macro to map API message id to handler''' - output = '''\ + """Generate macro to map API message id to handler""" + output = """\ /****** Message ID / handler enum ******/ #ifdef vl_msg_id -''' +""" - for t in s['Define']: - output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \ - (t.name.upper(), t.name) + for t in s["Define"]: + output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % ( + t.name.upper(), + t.name, + ) output += "#endif" return output def msg_names(s): - '''Generate calls to name mapping macro''' - output = '''\ + """Generate calls to name mapping macro""" + output = """\ /****** Message names ******/ #ifdef vl_msg_name -''' +""" - for t in s['Define']: + for t in s["Define"]: dont_trace = 0 if t.dont_trace else 1 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace) output += "#endif" @@ -740,190 +800,215 @@ def msg_names(s): def msg_name_crc_list(s, suffix): - '''Generate list of names to CRC mappings''' - output = '''\ + """Generate list of names to CRC mappings""" + output = """\ /****** Message name, crc list ******/ #ifdef vl_msg_name_crc_list -''' +""" output += "#define foreach_vl_msg_name_crc_%s " % suffix - for t in s['Define']: - output += "\\\n_(VL_API_%s, %s, %08x) " % \ - (t.name.upper(), t.name, t.crc) + for t in s["Define"]: + output += "\\\n_(VL_API_%s, %s, %08x) " % (t.name.upper(), t.name, t.crc) output += "\n#endif" return output def api2c(fieldtype): - '''Map between API type names and internal VPP type names''' - mappingtable = {'string': 'vl_api_string_t', } + """Map between API type names and internal VPP type names""" + mappingtable = { + "string": "vl_api_string_t", + } if fieldtype in mappingtable: return mappingtable[fieldtype] return fieldtype def typedefs(filename): - '''Include in the main files to the types file''' - output = '''\ + """Include in the main files to the types file""" + output = """\ /****** Typedefs ******/ #ifdef vl_typedefs #include "{include}.api_types.h" #endif -'''.format(include=filename) +""".format( + include=filename + ) return output -FORMAT_STRINGS = {'u8': '%u', - 'bool': '%u', - 'i8': '%d', - 'u16': '%u', - 'i16': '%d', - 'u32': '%u', - 'i32': '%ld', - 'u64': '%llu', - 'i64': '%lld', - 'f64': '%.2f'} +FORMAT_STRINGS = { + "u8": "%u", + "bool": "%u", + "i8": "%d", + "u16": "%u", + "i16": "%d", + "u32": "%u", + "i32": "%ld", + "u64": "%llu", + "i64": "%lld", + "f64": "%.2f", +} + +class Printfun: + """Functions for pretty printing VPP API messages""" -class Printfun(): - '''Functions for pretty printing VPP API messages''' _dispatch = {} - noprint_fields = {'_vl_msg_id': None, - 'client_index': None, - 'context': None} + noprint_fields = {"_vl_msg_id": None, "client_index": None, "context": None} def __init__(self, stream): self.stream = stream @staticmethod def print_string(o, stream): - '''Pretty print a vl_api_string_t''' + """Pretty print a vl_api_string_t""" write = stream.write if o.modern_vla: - write(' if (vl_api_string_len(&a->{f}) > 0) {{\n' - .format(f=o.fieldname)) - write(' s = format(s, "\\n%U{f}: %U", ' - 'format_white_space, indent, ' - 'vl_api_format_string, (&a->{f}));\n'.format(f=o.fieldname)) - write(' } else {\n') - write(' s = format(s, "\\n%U{f}:", ' - 'format_white_space, indent);\n'.format(f=o.fieldname)) - write(' }\n') + write(" if (vl_api_string_len(&a->{f}) > 0) {{\n".format(f=o.fieldname)) + write( + ' s = format(s, "\\n%U{f}: %U", ' + "format_white_space, indent, " + "vl_api_format_string, (&a->{f}));\n".format(f=o.fieldname) + ) + write(" } else {\n") + write( + ' s = format(s, "\\n%U{f}:", ' + "format_white_space, indent);\n".format(f=o.fieldname) + ) + write(" }\n") else: - write(' s = format(s, "\\n%U{f}: %s", ' - 'format_white_space, indent, a->{f});\n' - .format(f=o.fieldname)) + write( + ' s = format(s, "\\n%U{f}: %s", ' + "format_white_space, indent, a->{f});\n".format(f=o.fieldname) + ) def print_field(self, o, stream): - '''Pretty print API field''' + """Pretty print API field""" write = stream.write if o.fieldname in self.noprint_fields: return if o.fieldtype in FORMAT_STRINGS: f = FORMAT_STRINGS[o.fieldtype] - write(' s = format(s, "\\n%U{n}: {f}", ' - 'format_white_space, indent, a->{n});\n' - .format(n=o.fieldname, f=f)) + write( + ' s = format(s, "\\n%U{n}: {f}", ' + "format_white_space, indent, a->{n});\n".format(n=o.fieldname, f=f) + ) else: - write(' s = format(s, "\\n%U{n}: %U", ' - 'format_white_space, indent, ' - 'format_{t}, &a->{n}, indent);\n' - .format(n=o.fieldname, t=o.fieldtype)) + write( + ' s = format(s, "\\n%U{n}: %U", ' + "format_white_space, indent, " + "format_{t}, &a->{n}, indent);\n".format(n=o.fieldname, t=o.fieldtype) + ) - _dispatch['Field'] = print_field + _dispatch["Field"] = print_field def print_array(self, o, stream): - '''Pretty print API array''' + """Pretty print API array""" write = stream.write - forloop = '''\ + forloop = """\ for (i = 0; i < {lfield}; i++) {{ s = format(s, "\\n%U{n}: %U", format_white_space, indent, format_{t}, &a->{n}[i], indent); }} -''' +""" - forloop_format = '''\ + forloop_format = """\ for (i = 0; i < {lfield}; i++) {{ s = format(s, "\\n%U{n}: {t}", format_white_space, indent, a->{n}[i]); }} -''' +""" - if o.fieldtype == 'string': + if o.fieldtype == "string": self.print_string(o, stream) return - if o.fieldtype == 'u8': + if o.fieldtype == "u8": if o.lengthfield: - write(' s = format(s, "\\n%U{n}: %U", format_white_space, ' - 'indent, format_hex_bytes, a->{n}, a->{lfield});\n' - .format(n=o.fieldname, lfield=o.lengthfield)) + write( + ' s = format(s, "\\n%U{n}: %U", format_white_space, ' + "indent, format_hex_bytes, a->{n}, a->{lfield});\n".format( + n=o.fieldname, lfield=o.lengthfield + ) + ) else: - write(' s = format(s, "\\n%U{n}: %U", format_white_space, ' - 'indent, format_hex_bytes, a, {lfield});\n' - .format(n=o.fieldname, lfield=o.length)) + write( + ' s = format(s, "\\n%U{n}: %U", format_white_space, ' + "indent, format_hex_bytes, a, {lfield});\n".format( + n=o.fieldname, lfield=o.length + ) + ) return - lfield = 'a->' + o.lengthfield if o.lengthfield else o.length + lfield = "a->" + o.lengthfield if o.lengthfield else o.length if o.fieldtype in FORMAT_STRINGS: - write(forloop_format.format(lfield=lfield, - t=FORMAT_STRINGS[o.fieldtype], - n=o.fieldname)) + write( + forloop_format.format( + lfield=lfield, t=FORMAT_STRINGS[o.fieldtype], n=o.fieldname + ) + ) else: write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname)) - _dispatch['Array'] = print_array + _dispatch["Array"] = print_array @staticmethod def print_alias(k, v, stream): - '''Pretty print type alias''' + """Pretty print type alias""" write = stream.write - if ('length' in v.alias and v.alias['length'] and - v.alias['type'] == 'u8'): - write(' return format(s, "%U", format_hex_bytes, a, {});\n' - .format(v.alias['length'])) - elif v.alias['type'] in FORMAT_STRINGS: - write(' return format(s, "{}", *a);\n' - .format(FORMAT_STRINGS[v.alias['type']])) + if "length" in v.alias and v.alias["length"] and v.alias["type"] == "u8": + write( + ' return format(s, "%U", format_hex_bytes, a, {});\n'.format( + v.alias["length"] + ) + ) + elif v.alias["type"] in FORMAT_STRINGS: + write( + ' return format(s, "{}", *a);\n'.format( + FORMAT_STRINGS[v.alias["type"]] + ) + ) else: - write(' return format(s, "{} (print not implemented)");\n' - .format(k)) + write(' return format(s, "{} (print not implemented)");\n'.format(k)) @staticmethod def print_enum(o, stream): - '''Pretty print API enum''' + """Pretty print API enum""" write = stream.write write(" switch(*a) {\n") for b in o: write(" case %s:\n" % b[1]) write(' return format(s, "{}");\n'.format(b[0])) - write(' }\n') + write(" }\n") - _dispatch['Enum'] = print_enum - _dispatch['EnumFlag'] = print_enum + _dispatch["Enum"] = print_enum + _dispatch["EnumFlag"] = print_enum def print_obj(self, o, stream): - '''Entry point''' + """Entry point""" write = stream.write if o.type in self._dispatch: self._dispatch[o.type](self, o, stream) else: - write(' s = format(s, "\\n{} {} {} (print not implemented");\n' - .format(o.type, o.fieldtype, o.fieldname)) + write( + ' s = format(s, "\\n{} {} {} (print not implemented");\n'.format( + o.type, o.fieldtype, o.fieldname + ) + ) def printfun(objs, stream, modulename): - '''Main entry point for pretty print function generation''' + """Main entry point for pretty print function generation""" write = stream.write - h = '''\ + h = """\ /****** Print functions *****/ #ifdef vl_printfun #ifndef included_{module}_printfun @@ -940,15 +1025,15 @@ def printfun(objs, stream, modulename): #include "{module}.api_tojson.h" #include "{module}.api_fromjson.h" -''' +""" - signature = '''\ + signature = """\ static inline void *vl_api_{name}_t_print{suffix} (vl_api_{name}_t *a, void *handle) {{ u8 *s = 0; u32 indent __attribute__((unused)) = 2; int i __attribute__((unused)); -''' +""" h = h.format(module=modulename) write(h) @@ -958,171 +1043,172 @@ static inline void *vl_api_{name}_t_print{suffix} (vl_api_{name}_t *a, void *han if t.manual_print: write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name) continue - write(signature.format(name=t.name, suffix='')) - write(' /* Message definition: vl_api_{}_t: */\n'.format(t.name)) - write(" s = format(s, \"vl_api_%s_t:\");\n" % t.name) + write(signature.format(name=t.name, suffix="")) + write(" /* Message definition: vl_api_{}_t: */\n".format(t.name)) + write(' s = format(s, "vl_api_%s_t:");\n' % t.name) for o in t.block: pp.print_obj(o, stream) - write(' vec_add1(s, 0);\n') - write(' vl_print (handle, (char *)s);\n') - write(' vec_free (s);\n') - write(' return handle;\n') - write('}\n\n') - - write(signature.format(name=t.name, suffix='_json')) - write(' cJSON * o = vl_api_{}_t_tojson(a);\n'.format(t.name)) - write(' (void)s;\n') - write(' char *out = cJSON_Print(o);\n') - write(' vl_print(handle, out);\n') - write(' cJSON_Delete(o);\n') - write(' cJSON_free(out);\n') - write(' return handle;\n') - write('}\n\n') + write(" vec_add1(s, 0);\n") + write(" vl_print (handle, (char *)s);\n") + write(" vec_free (s);\n") + write(" return handle;\n") + write("}\n\n") + + write(signature.format(name=t.name, suffix="_json")) + write(" cJSON * o = vl_api_{}_t_tojson(a);\n".format(t.name)) + write(" (void)s;\n") + write(" char *out = cJSON_Print(o);\n") + write(" vl_print(handle, out);\n") + write(" cJSON_Delete(o);\n") + write(" cJSON_free(out);\n") + write(" return handle;\n") + write("}\n\n") write("\n#endif") write("\n#endif /* vl_printfun */\n") - return '' + return "" def printfun_types(objs, stream, modulename): - '''Pretty print API types''' + """Pretty print API types""" write = stream.write pp = Printfun(stream) - h = '''\ + h = """\ /****** Print functions *****/ #ifdef vl_printfun #ifndef included_{module}_printfun_types #define included_{module}_printfun_types -''' +""" h = h.format(module=modulename) write(h) - signature = '''\ + signature = """\ static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args) {{ vl_api_{name}_t *a = va_arg (*args, vl_api_{name}_t *); u32 indent __attribute__((unused)) = va_arg (*args, u32); int i __attribute__((unused)); indent += 2; -''' +""" for t in objs: - if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag': + if t.__class__.__name__ == "Enum" or t.__class__.__name__ == "EnumFlag": write(signature.format(name=t.name)) pp.print_enum(t.block, stream) - write(' return s;\n') - write('}\n\n') + write(" return s;\n") + write("}\n\n") continue if t.manual_print: write("/***** manual: vl_api_%s_t_print *****/\n\n" % t.name) continue - if t.__class__.__name__ == 'Using': + if t.__class__.__name__ == "Using": write(signature.format(name=t.name)) pp.print_alias(t.name, t, stream) - write('}\n\n') + write("}\n\n") continue write(signature.format(name=t.name)) for o in t.block: pp.print_obj(o, stream) - write(' return s;\n') - write('}\n\n') + write(" return s;\n") + write("}\n\n") write("\n#endif") write("\n#endif /* vl_printfun_types */\n") def generate_imports(imports): - '''Add #include matching the API import statements''' - output = '/* Imported API files */\n' - output += '#ifndef vl_api_version\n' + """Add #include matching the API import statements""" + output = "/* Imported API files */\n" + output += "#ifndef vl_api_version\n" for i in imports: - s = i.filename.replace('plugins/', '') - output += '#include <{}.h>\n'.format(s) - output += '#endif\n' + s = i.filename.replace("plugins/", "") + output += "#include <{}.h>\n".format(s) + output += "#endif\n" return output ENDIAN_STRINGS = { - 'u16': 'clib_net_to_host_u16', - 'u32': 'clib_net_to_host_u32', - 'u64': 'clib_net_to_host_u64', - 'i16': 'clib_net_to_host_i16', - 'i32': 'clib_net_to_host_i32', - 'i64': 'clib_net_to_host_i64', - 'f64': 'clib_net_to_host_f64', + "u16": "clib_net_to_host_u16", + "u32": "clib_net_to_host_u32", + "u64": "clib_net_to_host_u64", + "i16": "clib_net_to_host_i16", + "i32": "clib_net_to_host_i32", + "i64": "clib_net_to_host_i64", + "f64": "clib_net_to_host_f64", } def endianfun_array(o): - '''Generate endian functions for arrays''' - forloop = '''\ + """Generate endian functions for arrays""" + forloop = """\ for (i = 0; i < {length}; i++) {{ a->{name}[i] = {format}(a->{name}[i]); }} -''' +""" - forloop_format = '''\ + forloop_format = """\ for (i = 0; i < {length}; i++) {{ {type}_endian(&a->{name}[i]); }} -''' +""" - output = '' - if o.fieldtype == 'u8' or o.fieldtype == 'string' or o.fieldtype == 'bool': - output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname) + output = "" + if o.fieldtype == "u8" or o.fieldtype == "string" or o.fieldtype == "bool": + output += " /* a->{n} = a->{n} (no-op) */\n".format(n=o.fieldname) else: - lfield = 'a->' + o.lengthfield if o.lengthfield else o.length + lfield = "a->" + o.lengthfield if o.lengthfield else o.length if o.fieldtype in ENDIAN_STRINGS: - output += (forloop - .format(length=lfield, - format=ENDIAN_STRINGS[o.fieldtype], - name=o.fieldname)) + output += forloop.format( + length=lfield, format=ENDIAN_STRINGS[o.fieldtype], name=o.fieldname + ) else: - output += (forloop_format - .format(length=lfield, type=o.fieldtype, - name=o.fieldname)) + output += forloop_format.format( + length=lfield, type=o.fieldtype, name=o.fieldname + ) return output -NO_ENDIAN_CONVERSION = {'client_index': None} +NO_ENDIAN_CONVERSION = {"client_index": None} def endianfun_obj(o): - '''Generate endian conversion function for type''' - output = '' - if o.type == 'Array': + """Generate endian conversion function for type""" + output = "" + if o.type == "Array": return endianfun_array(o) - if o.type != 'Field': - output += (' s = format(s, "\\n{} {} {} (print not implemented");\n' - .format(o.type, o.fieldtype, o.fieldname)) + if o.type != "Field": + output += ' s = format(s, "\\n{} {} {} (print not implemented");\n'.format( + o.type, o.fieldtype, o.fieldname + ) return output if o.fieldname in NO_ENDIAN_CONVERSION: - output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname) + output += " /* a->{n} = a->{n} (no-op) */\n".format(n=o.fieldname) return output if o.fieldtype in ENDIAN_STRINGS: - output += (' a->{name} = {format}(a->{name});\n' - .format(name=o.fieldname, - format=ENDIAN_STRINGS[o.fieldtype])) - elif o.fieldtype.startswith('vl_api_'): - output += (' {type}_endian(&a->{name});\n' - .format(type=o.fieldtype, name=o.fieldname)) + output += " a->{name} = {format}(a->{name});\n".format( + name=o.fieldname, format=ENDIAN_STRINGS[o.fieldtype] + ) + elif o.fieldtype.startswith("vl_api_"): + output += " {type}_endian(&a->{name});\n".format( + type=o.fieldtype, name=o.fieldname + ) else: - output += ' /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname) + output += " /* a->{n} = a->{n} (no-op) */\n".format(n=o.fieldname) return output def endianfun(objs, modulename): - '''Main entry point for endian function generation''' - output = '''\ + """Main entry point for endian function generation""" + output = """\ /****** Endian swap functions *****/\n\ #ifdef vl_endianfun @@ -1136,51 +1222,50 @@ def endianfun(objs, modulename): #define clib_net_to_host_uword clib_net_to_host_u32 #endif -''' +""" output = output.format(module=modulename) - signature = '''\ + signature = """\ static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a) {{ int i __attribute__((unused)); -''' +""" for t in objs: - if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag': + if t.__class__.__name__ == "Enum" or t.__class__.__name__ == "EnumFlag": output += signature.format(name=t.name) if t.enumtype in ENDIAN_STRINGS: - output += (' *a = {}(*a);\n' - .format(ENDIAN_STRINGS[t.enumtype])) + output += " *a = {}(*a);\n".format(ENDIAN_STRINGS[t.enumtype]) else: - output += (' /* a->{name} = a->{name} (no-op) */\n' - .format(name=t.name)) + output += " /* a->{name} = a->{name} (no-op) */\n".format( + name=t.name + ) - output += '}\n\n' + output += "}\n\n" continue if t.manual_endian: output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name continue - if t.__class__.__name__ == 'Using': + if t.__class__.__name__ == "Using": output += signature.format(name=t.name) - if ('length' in t.alias and t.alias['length'] and - t.alias['type'] == 'u8'): - output += (' /* a->{name} = a->{name} (no-op) */\n' - .format(name=t.name)) - elif t.alias['type'] in FORMAT_STRINGS: - output += (' *a = {}(*a);\n' - .format(ENDIAN_STRINGS[t.alias['type']])) + if "length" in t.alias and t.alias["length"] and t.alias["type"] == "u8": + output += " /* a->{name} = a->{name} (no-op) */\n".format( + name=t.name + ) + elif t.alias["type"] in FORMAT_STRINGS: + output += " *a = {}(*a);\n".format(ENDIAN_STRINGS[t.alias["type"]]) else: - output += ' /* Not Implemented yet {} */'.format(t.name) - output += '}\n\n' + output += " /* Not Implemented yet {} */".format(t.name) + output += "}\n\n" continue output += signature.format(name=t.name) for o in t.block: output += endianfun_obj(o) - output += '}\n\n' + output += "}\n\n" output += "\n#endif" output += "\n#endif /* vl_endianfun */\n\n" @@ -1189,32 +1274,32 @@ static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a) def calc_size_fun(objs, modulename): - '''Main entry point for calculate size function generation''' - output = '''\ + """Main entry point for calculate size function generation""" + output = """\ /****** Calculate size functions *****/\n\ #ifdef vl_calcsizefun #ifndef included_{module}_calcsizefun #define included_{module}_calcsizefun -''' +""" output = output.format(module=modulename) - signature = '''\ + signature = """\ /* calculate message size of message in network byte order */ static inline uword vl_api_{name}_t_calc_size (vl_api_{name}_t *a) {{ -''' +""" for o in objs: tname = o.__class__.__name__ output += signature.format(name=o.name) output += f" return sizeof(*a)" - if tname == 'Using': - if 'length' in o.alias: + if tname == "Using": + if "length" in o.alias: try: - tmp = int(o.alias['length']) + tmp = int(o.alias["length"]) if tmp == 0: raise (f"Unexpected length '0' for alias {o}") except: @@ -1224,36 +1309,44 @@ static inline uword vl_api_{name}_t_calc_size (vl_api_{name}_t *a) print(dir(o.alias)) print(o.alias) raise - elif tname == 'Enum' or tname == 'EnumFlag': + elif tname == "Enum" or tname == "EnumFlag": pass else: for b in o.block: - if b.type == 'Option': + if b.type == "Option": continue - elif b.type == 'Field': - if b.fieldtype.startswith('vl_api_'): + elif b.type == "Field": + if b.fieldtype.startswith("vl_api_"): output += f" - sizeof(a->{b.fieldname})" output += f" + {b.fieldtype}_calc_size(&a->{b.fieldname})" - elif b.type == 'Array': + elif b.type == "Array": if b.lengthfield: - m = list(filter(lambda x: x.fieldname == b.lengthfield, o.block)) + m = list( + filter(lambda x: x.fieldname == b.lengthfield, o.block) + ) if len(m) != 1: - raise Exception(f"Expected 1 match for field '{b.lengthfield}', got '{m}'") + raise Exception( + f"Expected 1 match for field '{b.lengthfield}', got '{m}'" + ) lf = m[0] if lf.fieldtype in ENDIAN_STRINGS: output += f" + {ENDIAN_STRINGS[lf.fieldtype]}(a->{b.lengthfield}) * sizeof(a->{b.fieldname}[0])" elif lf.fieldtype == "u8": - output += f" + a->{b.lengthfield} * sizeof(a->{b.fieldname}[0])" + output += ( + f" + a->{b.lengthfield} * sizeof(a->{b.fieldname}[0])" + ) else: - raise Exception(f"Don't know how to endian swap {lf.fieldtype}") + raise Exception( + f"Don't know how to endian swap {lf.fieldtype}" + ) else: # Fixed length strings decay to nul terminated u8 - if b.fieldtype == 'string': + if b.fieldtype == "string": if b.modern_vla: output += f" + vl_api_string_len(&a->{b.fieldname})" output += ";\n" - output += '}\n\n' + output += "}\n\n" output += "\n#endif" output += "\n#endif /* vl_calcsizefun */\n\n" @@ -1261,18 +1354,22 @@ static inline uword vl_api_{name}_t_calc_size (vl_api_{name}_t *a) def version_tuple(s, module): - '''Generate semantic version string''' - output = '''\ + """Generate semantic version string""" + output = """\ /****** Version tuple *****/ #ifdef vl_api_version_tuple -''' - if 'version' in s['Option']: - v = s['Option']['version'] - (major, minor, patch) = v.split('.') - output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \ - (module, major, minor, patch) +""" + if "version" in s["Option"]: + v = s["Option"]["version"] + (major, minor, patch) = v.split(".") + output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % ( + module, + major, + minor, + patch, + ) output += "\n#endif /* vl_api_version_tuple */\n\n" @@ -1280,131 +1377,145 @@ def version_tuple(s, module): def generate_include_enum(s, module, stream): - '''Generate <name>.api_enum.h''' + """Generate <name>.api_enum.h""" write = stream.write - if 'Define' in s: - write('typedef enum {\n') - for t in s['Define']: - write(' VL_API_{},\n'.format(t.name.upper())) - write(' VL_MSG_{}_LAST\n'.format(module.upper())) - write('}} vl_api_{}_enum_t;\n'.format(module)) + if "Define" in s: + write("typedef enum {\n") + for t in s["Define"]: + write(" VL_API_{},\n".format(t.name.upper())) + write(" VL_MSG_{}_LAST\n".format(module.upper())) + write("}} vl_api_{}_enum_t;\n".format(module)) def generate_include_counters(s, stream): - '''Include file for the counter data model types.''' + """Include file for the counter data model types.""" write = stream.write for counters in s: csetname = counters.name - write('typedef enum {\n') + write("typedef enum {\n") for c in counters.block: - write(' {}_ERROR_{},\n' - .format(csetname.upper(), c['name'].upper())) - write(' {}_N_ERROR\n'.format(csetname.upper())) - write('}} vl_counter_{}_enum_t;\n'.format(csetname)) + write(" {}_ERROR_{},\n".format(csetname.upper(), c["name"].upper())) + write(" {}_N_ERROR\n".format(csetname.upper())) + write("}} vl_counter_{}_enum_t;\n".format(csetname)) - write('extern vlib_error_desc_t {}_error_counters[];\n'.format(csetname)) + write("extern vlib_error_desc_t {}_error_counters[];\n".format(csetname)) def generate_include_types(s, module, stream): - '''Generate separate API _types file.''' + """Generate separate API _types file.""" write = stream.write - write('#ifndef included_{module}_api_types_h\n'.format(module=module)) - write('#define included_{module}_api_types_h\n'.format(module=module)) - - if 'version' in s['Option']: - v = s['Option']['version'] - (major, minor, patch) = v.split('.') - write('#define VL_API_{m}_API_VERSION_MAJOR {v}\n' - .format(m=module.upper(), v=major)) - write('#define VL_API_{m}_API_VERSION_MINOR {v}\n' - .format(m=module.upper(), v=minor)) - write('#define VL_API_{m}_API_VERSION_PATCH {v}\n' - .format(m=module.upper(), v=patch)) - - if 'Import' in s: - write('/* Imported API files */\n') - for i in s['Import']: - filename = i.filename.replace('plugins/', '') - write('#include <{}_types.h>\n'.format(filename)) - - for o in itertools.chain(s['types'], s['Define']): + write("#ifndef included_{module}_api_types_h\n".format(module=module)) + write("#define included_{module}_api_types_h\n".format(module=module)) + + if "version" in s["Option"]: + v = s["Option"]["version"] + (major, minor, patch) = v.split(".") + write( + "#define VL_API_{m}_API_VERSION_MAJOR {v}\n".format( + m=module.upper(), v=major + ) + ) + write( + "#define VL_API_{m}_API_VERSION_MINOR {v}\n".format( + m=module.upper(), v=minor + ) + ) + write( + "#define VL_API_{m}_API_VERSION_PATCH {v}\n".format( + m=module.upper(), v=patch + ) + ) + + if "Import" in s: + write("/* Imported API files */\n") + for i in s["Import"]: + filename = i.filename.replace("plugins/", "") + write("#include <{}_types.h>\n".format(filename)) + + for o in itertools.chain(s["types"], s["Define"]): tname = o.__class__.__name__ - if tname == 'Using': - if 'length' in o.alias: - write('typedef %s vl_api_%s_t[%s];\n' % - (o.alias['type'], o.name, o.alias['length'])) + if tname == "Using": + if "length" in o.alias: + write( + "typedef %s vl_api_%s_t[%s];\n" + % (o.alias["type"], o.name, o.alias["length"]) + ) else: - write('typedef %s vl_api_%s_t;\n' % (o.alias['type'], o.name)) - elif tname == 'Enum' or tname == 'EnumFlag': - if o.enumtype == 'u32': + write("typedef %s vl_api_%s_t;\n" % (o.alias["type"], o.name)) + elif tname == "Enum" or tname == "EnumFlag": + if o.enumtype == "u32": write("typedef enum {\n") else: write("typedef enum __attribute__((packed)) {\n") for b in o.block: write(" %s = %s,\n" % (b[0], b[1])) - write('} vl_api_%s_t;\n' % o.name) - if o.enumtype != 'u32': - size1 = 'sizeof(vl_api_%s_t)' % o.name - size2 = 'sizeof(%s)' % o.enumtype - err_str = 'size of API enum %s is wrong' % o.name - write('STATIC_ASSERT(%s == %s, "%s");\n' - % (size1, size2, err_str)) + write("} vl_api_%s_t;\n" % o.name) + if o.enumtype != "u32": + size1 = "sizeof(vl_api_%s_t)" % o.name + size2 = "sizeof(%s)" % o.enumtype + err_str = "size of API enum %s is wrong" % o.name + write('STATIC_ASSERT(%s == %s, "%s");\n' % (size1, size2, err_str)) else: - if tname == 'Union': - write("typedef union __attribute__ ((packed)) _vl_api_%s {\n" - % o.name) + if tname == "Union": + write("typedef union __attribute__ ((packed)) _vl_api_%s {\n" % o.name) else: - write(("typedef struct __attribute__ ((packed)) _vl_api_%s {\n") - % o.name) + write( + ("typedef struct __attribute__ ((packed)) _vl_api_%s {\n") % o.name + ) for b in o.block: - if b.type == 'Option': + if b.type == "Option": continue - if b.type == 'Field': - write(" %s %s;\n" % (api2c(b.fieldtype), - b.fieldname)) - elif b.type == 'Array': + if b.type == "Field": + write(" %s %s;\n" % (api2c(b.fieldtype), b.fieldname)) + elif b.type == "Array": if b.lengthfield: - write(" %s %s[0];\n" % (api2c(b.fieldtype), - b.fieldname)) + write(" %s %s[0];\n" % (api2c(b.fieldtype), b.fieldname)) else: # Fixed length strings decay to nul terminated u8 - if b.fieldtype == 'string': + if b.fieldtype == "string": if b.modern_vla: - write(' {} {};\n' - .format(api2c(b.fieldtype), - b.fieldname)) + write( + " {} {};\n".format( + api2c(b.fieldtype), b.fieldname + ) + ) else: - write(' u8 {}[{}];\n' - .format(b.fieldname, b.length)) + write(" u8 {}[{}];\n".format(b.fieldname, b.length)) else: - write(" %s %s[%s];\n" % - (api2c(b.fieldtype), b.fieldname, - b.length)) + write( + " %s %s[%s];\n" + % (api2c(b.fieldtype), b.fieldname, b.length) + ) else: - raise ValueError("Error in processing type {} for {}" - .format(b, o.name)) + raise ValueError( + "Error in processing type {} for {}".format(b, o.name) + ) - write('} vl_api_%s_t;\n' % o.name) - write(f'#define VL_API_{o.name.upper()}_IS_CONSTANT_SIZE ({0 if o.vla else 1})\n\n') + write("} vl_api_%s_t;\n" % o.name) + write( + f"#define VL_API_{o.name.upper()}_IS_CONSTANT_SIZE ({0 if o.vla else 1})\n\n" + ) - for t in s['Define']: - write('#define VL_API_{ID}_CRC "{n}_{crc:08x}"\n' - .format(n=t.name, ID=t.name.upper(), crc=t.crc)) + for t in s["Define"]: + write( + '#define VL_API_{ID}_CRC "{n}_{crc:08x}"\n'.format( + n=t.name, ID=t.name.upper(), crc=t.crc + ) + ) write("\n#endif\n") -def generate_c_boilerplate(services, defines, counters, file_crc, - module, stream): - '''VPP side plugin.''' +def generate_c_boilerplate(services, defines, counters, file_crc, module, stream): + """VPP side plugin.""" write = stream.write define_hash = {d.name: d for d in defines} - hdr = '''\ + hdr = """\ #define vl_endianfun /* define message structures */ #include "{module}.api.h" #undef vl_endianfun @@ -1419,89 +1530,98 @@ def generate_c_boilerplate(services, defines, counters, file_crc, #include "{module}.api.h" #undef vl_printfun -''' +""" write(hdr.format(module=module)) - write('static u16\n') - write('setup_message_id_table (void) {\n') - write(' api_main_t *am = my_api_main;\n') - write(' vl_msg_api_msg_config_t c;\n') - write(' u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", ' - 'VL_MSG_{m}_LAST);\n' - .format(module, crc=file_crc, m=module.upper())) + write("static u16\n") + write("setup_message_id_table (void) {\n") + write(" api_main_t *am = my_api_main;\n") + write(" vl_msg_api_msg_config_t c;\n") + write( + ' u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", ' + "VL_MSG_{m}_LAST);\n".format(module, crc=file_crc, m=module.upper()) + ) for d in defines: - write(' vl_msg_api_add_msg_name_crc (am, "{n}_{crc:08x}",\n' - ' VL_API_{ID} + msg_id_base);\n' - .format(n=d.name, ID=d.name.upper(), crc=d.crc)) + write( + ' vl_msg_api_add_msg_name_crc (am, "{n}_{crc:08x}",\n' + " VL_API_{ID} + msg_id_base);\n".format( + n=d.name, ID=d.name.upper(), crc=d.crc + ) + ) for s in services: d = define_hash[s.caller] - write(' c = (vl_msg_api_msg_config_t) ' - ' {{.id = VL_API_{ID} + msg_id_base,\n' - ' .name = "{n}",\n' - ' .handler = vl_api_{n}_t_handler,\n' - ' .cleanup = vl_noop_handler,\n' - ' .endian = vl_api_{n}_t_endian,\n' - ' .print = vl_api_{n}_t_print,\n' - ' .traced = 1,\n' - ' .replay = 1,\n' - ' .print_json = vl_api_{n}_t_print_json,\n' - ' .tojson = vl_api_{n}_t_tojson,\n' - ' .fromjson = vl_api_{n}_t_fromjson,\n' - ' .calc_size = vl_api_{n}_t_calc_size,\n' - ' .is_autoendian = {auto}}};\n' - .format(n=s.caller, ID=s.caller.upper(), - auto=d.autoendian)) - write(' vl_msg_api_config (&c);\n') + write( + " c = (vl_msg_api_msg_config_t) " + " {{.id = VL_API_{ID} + msg_id_base,\n" + ' .name = "{n}",\n' + " .handler = vl_api_{n}_t_handler,\n" + " .cleanup = vl_noop_handler,\n" + " .endian = vl_api_{n}_t_endian,\n" + " .print = vl_api_{n}_t_print,\n" + " .traced = 1,\n" + " .replay = 1,\n" + " .print_json = vl_api_{n}_t_print_json,\n" + " .tojson = vl_api_{n}_t_tojson,\n" + " .fromjson = vl_api_{n}_t_fromjson,\n" + " .calc_size = vl_api_{n}_t_calc_size,\n" + " .is_autoendian = {auto}}};\n".format( + n=s.caller, ID=s.caller.upper(), auto=d.autoendian + ) + ) + write(" vl_msg_api_config (&c);\n") try: d = define_hash[s.reply] - write(' c = (vl_msg_api_msg_config_t) ' - '{{.id = VL_API_{ID} + msg_id_base,\n' - ' .name = "{n}",\n' - ' .handler = 0,\n' - ' .cleanup = vl_noop_handler,\n' - ' .endian = vl_api_{n}_t_endian,\n' - ' .print = vl_api_{n}_t_print,\n' - ' .traced = 1,\n' - ' .replay = 1,\n' - ' .print_json = vl_api_{n}_t_print_json,\n' - ' .tojson = vl_api_{n}_t_tojson,\n' - ' .fromjson = vl_api_{n}_t_fromjson,\n' - ' .calc_size = vl_api_{n}_t_calc_size,\n' - ' .is_autoendian = {auto}}};\n' - .format(n=s.reply, ID=s.reply.upper(), - auto=d.autoendian)) - write(' vl_msg_api_config (&c);\n') + write( + " c = (vl_msg_api_msg_config_t) " + "{{.id = VL_API_{ID} + msg_id_base,\n" + ' .name = "{n}",\n' + " .handler = 0,\n" + " .cleanup = vl_noop_handler,\n" + " .endian = vl_api_{n}_t_endian,\n" + " .print = vl_api_{n}_t_print,\n" + " .traced = 1,\n" + " .replay = 1,\n" + " .print_json = vl_api_{n}_t_print_json,\n" + " .tojson = vl_api_{n}_t_tojson,\n" + " .fromjson = vl_api_{n}_t_fromjson,\n" + " .calc_size = vl_api_{n}_t_calc_size,\n" + " .is_autoendian = {auto}}};\n".format( + n=s.reply, ID=s.reply.upper(), auto=d.autoendian + ) + ) + write(" vl_msg_api_config (&c);\n") except KeyError: pass - write(' return msg_id_base;\n') - write('}\n') + write(" return msg_id_base;\n") + write("}\n") - severity = {'error': 'VL_COUNTER_SEVERITY_ERROR', - 'info': 'VL_COUNTER_SEVERITY_INFO', - 'warn': 'VL_COUNTER_SEVERITY_WARN'} + severity = { + "error": "VL_COUNTER_SEVERITY_ERROR", + "info": "VL_COUNTER_SEVERITY_INFO", + "warn": "VL_COUNTER_SEVERITY_WARN", + } for cnt in counters: csetname = cnt.name - write('vlib_error_desc_t {}_error_counters[] = {{\n'.format(csetname)) + write("vlib_error_desc_t {}_error_counters[] = {{\n".format(csetname)) for c in cnt.block: - write(' {\n') - write(' .name = "{}",\n'.format(c['name'])) - write(' .desc = "{}",\n'.format(c['description'])) - write(' .severity = {},\n'.format(severity[c['severity']])) - write(' },\n') - write('};\n') + write(" {\n") + write(' .name = "{}",\n'.format(c["name"])) + write(' .desc = "{}",\n'.format(c["description"])) + write(" .severity = {},\n".format(severity[c["severity"]])) + write(" },\n") + write("};\n") -def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, - stream): - '''Generate code for legacy style VAT. To be deleted.''' +def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, stream): + """Generate code for legacy style VAT. To be deleted.""" write = stream.write define_hash = {d.name: d for d in defines} - hdr = '''\ + hdr = """\ #define vl_endianfun /* define message structures */ #include "{module}.api.h" #undef vl_endianfun @@ -1516,7 +1636,7 @@ def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, #include "{module}.api.h" #undef vl_printfun -''' +""" write(hdr.format(module=module)) for s in services: @@ -1525,113 +1645,133 @@ def generate_c_test_boilerplate(services, defines, file_crc, module, plugin, except KeyError: continue if d.manual_print: - write('/*\n' - ' * Manual definition requested for: \n' - ' * vl_api_{n}_t_handler()\n' - ' */\n' - .format(n=s.reply)) + write( + "/*\n" + " * Manual definition requested for: \n" + " * vl_api_{n}_t_handler()\n" + " */\n".format(n=s.reply) + ) continue if not define_hash[s.caller].autoreply: - write('/* Generation not supported (vl_api_{n}_t_handler()) */\n' - .format(n=s.reply)) + write( + "/* Generation not supported (vl_api_{n}_t_handler()) */\n".format( + n=s.reply + ) + ) continue - write('#ifndef VL_API_{n}_T_HANDLER\n'.format(n=s.reply.upper())) - write('static void\n') - write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n' - .format(n=s.reply)) - write(' vat_main_t * vam = {}_test_main.vat_main;\n'.format(module)) - write(' i32 retval = ntohl(mp->retval);\n') - write(' if (vam->async_mode) {\n') - write(' vam->async_errors += (retval < 0);\n') - write(' } else {\n') - write(' vam->retval = retval;\n') - write(' vam->result_ready = 1;\n') - write(' }\n') - write('}\n') - write('#endif\n') + write("#ifndef VL_API_{n}_T_HANDLER\n".format(n=s.reply.upper())) + write("static void\n") + write("vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n".format(n=s.reply)) + write(" vat_main_t * vam = {}_test_main.vat_main;\n".format(module)) + write(" i32 retval = ntohl(mp->retval);\n") + write(" if (vam->async_mode) {\n") + write(" vam->async_errors += (retval < 0);\n") + write(" } else {\n") + write(" vam->retval = retval;\n") + write(" vam->result_ready = 1;\n") + write(" }\n") + write("}\n") + write("#endif\n") for e in s.events: if define_hash[e].manual_print: continue - write('static void\n') - write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=e)) + write("static void\n") + write("vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n".format(n=e)) write(' vl_print(0, "{n} event called:");\n'.format(n=e)) - write(' vl_api_{n}_t_print(mp, 0);\n'.format(n=e)) - write('}\n') + write(" vl_api_{n}_t_print(mp, 0);\n".format(n=e)) + write("}\n") - write('static void\n') - write('setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n') + write("static void\n") + write("setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n") for s in services: - write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, ' - ' "{n}",\n' - ' vl_api_{n}_t_handler, ' - ' vl_noop_handler,\n' - ' vl_api_{n}_t_endian, ' - ' vl_api_{n}_t_print,\n' - ' sizeof(vl_api_{n}_t), 1,\n' - ' vl_api_{n}_t_print_json,\n' - ' vl_api_{n}_t_tojson,\n' - ' vl_api_{n}_t_fromjson,\n' - ' vl_api_{n}_t_calc_size);\n' - .format(n=s.reply, ID=s.reply.upper())) - write(' hash_set_mem (vam->function_by_name, "{n}", api_{n});\n' - .format(n=s.caller)) + write( + " vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, " + ' "{n}",\n' + " vl_api_{n}_t_handler, " + " vl_noop_handler,\n" + " vl_api_{n}_t_endian, " + " vl_api_{n}_t_print,\n" + " sizeof(vl_api_{n}_t), 1,\n" + " vl_api_{n}_t_print_json,\n" + " vl_api_{n}_t_tojson,\n" + " vl_api_{n}_t_fromjson,\n" + " vl_api_{n}_t_calc_size);\n".format( + n=s.reply, ID=s.reply.upper() + ) + ) + write( + ' hash_set_mem (vam->function_by_name, "{n}", api_{n});\n'.format( + n=s.caller + ) + ) try: - write(' hash_set_mem (vam->help_by_name, "{n}", "{help}");\n' - .format(n=s.caller, - help=define_hash[s.caller].options['vat_help'])) + write( + ' hash_set_mem (vam->help_by_name, "{n}", "{help}");\n'.format( + n=s.caller, help=define_hash[s.caller].options["vat_help"] + ) + ) except KeyError: pass # Events for e in s.events: - write(' vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, ' - ' "{n}",\n' - ' vl_api_{n}_t_handler, ' - ' vl_noop_handler,\n' - ' vl_api_{n}_t_endian, ' - ' vl_api_{n}_t_print,\n' - ' sizeof(vl_api_{n}_t), 1,\n' - ' vl_api_{n}_t_print_json,\n' - ' vl_api_{n}_t_tojson,\n' - ' vl_api_{n}_t_fromjson,\n' - ' vl_api_{n}_t_calc_size);\n' - .format(n=e, ID=e.upper())) - - write('}\n') - write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n') - write('{\n') - write(' {n}_test_main_t * mainp = &{n}_test_main;\n'.format(n=module)) - write(' mainp->vat_main = vam;\n') - write(' mainp->msg_id_base = vl_client_get_first_plugin_msg_id ' - ' ("{n}_{crc:08x}");\n' - .format(n=module, crc=file_crc)) - write(' if (mainp->msg_id_base == (u16) ~0)\n') - write(' return clib_error_return (0, "{} plugin not loaded...");\n' - .format(module)) - write(' setup_message_id_table (vam, mainp->msg_id_base);\n') - write('#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n') - write(' VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n') - write('#endif\n') - write(' return 0;\n') - write('}\n') + write( + " vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, " + ' "{n}",\n' + " vl_api_{n}_t_handler, " + " vl_noop_handler,\n" + " vl_api_{n}_t_endian, " + " vl_api_{n}_t_print,\n" + " sizeof(vl_api_{n}_t), 1,\n" + " vl_api_{n}_t_print_json,\n" + " vl_api_{n}_t_tojson,\n" + " vl_api_{n}_t_fromjson,\n" + " vl_api_{n}_t_calc_size);\n".format( + n=e, ID=e.upper() + ) + ) + + write("}\n") + write("clib_error_t * vat_plugin_register (vat_main_t *vam)\n") + write("{\n") + write(" {n}_test_main_t * mainp = &{n}_test_main;\n".format(n=module)) + write(" mainp->vat_main = vam;\n") + write( + " mainp->msg_id_base = vl_client_get_first_plugin_msg_id " + ' ("{n}_{crc:08x}");\n'.format(n=module, crc=file_crc) + ) + write(" if (mainp->msg_id_base == (u16) ~0)\n") + write( + ' return clib_error_return (0, "{} plugin not loaded...");\n'.format( + module + ) + ) + write(" setup_message_id_table (vam, mainp->msg_id_base);\n") + write("#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n") + write(" VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n") + write("#endif\n") + write(" return 0;\n") + write("}\n") def apifunc(func): - '''Check if a method is generated already.''' + """Check if a method is generated already.""" + def _f(module, d, processed, *args): if d.name in processed: return None processed[d.name] = True return func(module, d, *args) + return _f def c_test_api_service(s, dump, stream): - '''Generate JSON code for a service.''' + """Generate JSON code for a service.""" write = stream.write - req_reply_template = '''\ + req_reply_template = """\ static cJSON * api_{n} (cJSON *o) {{ @@ -1664,8 +1804,8 @@ api_{n} (cJSON *o) return vl_api_{r}_t_tojson(rmp); }} -''' - dump_details_template = '''\ +""" + dump_details_template = """\ static cJSON * api_{n} (cJSON *o) {{ @@ -1719,8 +1859,8 @@ api_{n} (cJSON *o) return reply; }} -''' - gets_details_reply_template = '''\ +""" + gets_details_reply_template = """\ static cJSON * api_{n} (cJSON *o) {{ @@ -1769,32 +1909,42 @@ api_{n} (cJSON *o) return reply; }} -''' +""" if dump: if s.stream_message: - write(gets_details_reply_template - .format(n=s.caller, r=s.reply, N=s.caller.upper(), - R=s.reply.upper(), d=s.stream_message, - D=s.stream_message.upper())) + write( + gets_details_reply_template.format( + n=s.caller, + r=s.reply, + N=s.caller.upper(), + R=s.reply.upper(), + d=s.stream_message, + D=s.stream_message.upper(), + ) + ) else: - write(dump_details_template.format(n=s.caller, r=s.reply, - N=s.caller.upper(), - R=s.reply.upper())) + write( + dump_details_template.format( + n=s.caller, r=s.reply, N=s.caller.upper(), R=s.reply.upper() + ) + ) else: - write(req_reply_template.format(n=s.caller, r=s.reply, - N=s.caller.upper(), - R=s.reply.upper())) + write( + req_reply_template.format( + n=s.caller, r=s.reply, N=s.caller.upper(), R=s.reply.upper() + ) + ) def generate_c_test2_boilerplate(services, defines, module, stream): - '''Generate code for VAT2 plugin.''' + """Generate code for VAT2 plugin.""" write = stream.write define_hash = {d.name: d for d in defines} # replies = {} - hdr = '''\ + hdr = """\ #include <vlibapi/api.h> #include <vlibmemory/api.h> #include <vppinfra/error.h> @@ -1829,7 +1979,7 @@ def generate_c_test2_boilerplate(services, defines, module, stream): #include <vat2/vat2_helpers.h> -''' +""" write(hdr.format(module=module)) @@ -1838,123 +1988,123 @@ def generate_c_test2_boilerplate(services, defines, module, stream): continue c_test_api_service(s, s.stream, stream) - write('void vat2_register_function(char *, cJSON * (*)(cJSON *), cJSON * (*)(void *), u32);\n') + write( + "void vat2_register_function(char *, cJSON * (*)(cJSON *), cJSON * (*)(void *), u32);\n" + ) # write('__attribute__((constructor))') - write('clib_error_t *\n') - write('vat2_register_plugin (void) {\n') + write("clib_error_t *\n") + write("vat2_register_plugin (void) {\n") for s in services: if s.reply not in define_hash: continue crc = define_hash[s.caller].crc - write(' vat2_register_function("{n}", api_{n}, (cJSON * (*)(void *))vl_api_{n}_t_tojson, 0x{crc:08x});\n' - .format(n=s.caller, crc=crc)) - write(' return 0;\n') - write('}\n') + write( + ' vat2_register_function("{n}", api_{n}, (cJSON * (*)(void *))vl_api_{n}_t_tojson, 0x{crc:08x});\n'.format( + n=s.caller, crc=crc + ) + ) + write(" return 0;\n") + write("}\n") # # Plugin entry point # def run(args, apifilename, s): - '''Main plugin entry point.''' + """Main plugin entry point.""" stream = StringIO() if not args.outputdir: - sys.stderr.write('Missing --outputdir argument') + sys.stderr.write("Missing --outputdir argument") return None basename = os.path.basename(apifilename) filename, _ = os.path.splitext(basename) - modulename = filename.replace('.', '_') - filename_enum = os.path.join(args.outputdir + '/' + basename + '_enum.h') - filename_types = os.path.join(args.outputdir + '/' + basename + '_types.h') - filename_c = os.path.join(args.outputdir + '/' + basename + '.c') - filename_c_test = os.path.join(args.outputdir + '/' + basename + '_test.c') - filename_c_test2 = (os.path.join(args.outputdir + '/' + basename + - '_test2.c')) - filename_c_tojson = (os.path.join(args.outputdir + - '/' + basename + '_tojson.h')) - filename_c_fromjson = (os.path.join(args.outputdir + '/' + - basename + '_fromjson.h')) + modulename = filename.replace(".", "_") + filename_enum = os.path.join(args.outputdir + "/" + basename + "_enum.h") + filename_types = os.path.join(args.outputdir + "/" + basename + "_types.h") + filename_c = os.path.join(args.outputdir + "/" + basename + ".c") + filename_c_test = os.path.join(args.outputdir + "/" + basename + "_test.c") + filename_c_test2 = os.path.join(args.outputdir + "/" + basename + "_test2.c") + filename_c_tojson = os.path.join(args.outputdir + "/" + basename + "_tojson.h") + filename_c_fromjson = os.path.join(args.outputdir + "/" + basename + "_fromjson.h") # Generate separate types file st = StringIO() generate_include_types(s, modulename, st) - with open(filename_types, 'w') as fd: + with open(filename_types, "w") as fd: st.seek(0) shutil.copyfileobj(st, fd) st.close() # Generate separate enum file st = StringIO() - st.write('#ifndef included_{}_api_enum_h\n'.format(modulename)) - st.write('#define included_{}_api_enum_h\n'.format(modulename)) + st.write("#ifndef included_{}_api_enum_h\n".format(modulename)) + st.write("#define included_{}_api_enum_h\n".format(modulename)) generate_include_enum(s, modulename, st) - generate_include_counters(s['Counters'], st) - st.write('#endif\n') - with open(filename_enum, 'w') as fd: + generate_include_counters(s["Counters"], st) + st.write("#endif\n") + with open(filename_enum, "w") as fd: st.seek(0) shutil.copyfileobj(st, fd) st.close() # Generate separate C file st = StringIO() - generate_c_boilerplate(s['Service'], s['Define'], s['Counters'], - s['file_crc'], modulename, st) - with open(filename_c, 'w') as fd: + generate_c_boilerplate( + s["Service"], s["Define"], s["Counters"], s["file_crc"], modulename, st + ) + with open(filename_c, "w") as fd: st.seek(0) shutil.copyfileobj(st, fd) st.close() # Generate separate C test file st = StringIO() - plugin = bool('plugin' in apifilename) - generate_c_test_boilerplate(s['Service'], s['Define'], - s['file_crc'], - modulename, plugin, st) - with open(filename_c_test, 'w') as fd: + plugin = bool("plugin" in apifilename) + generate_c_test_boilerplate( + s["Service"], s["Define"], s["file_crc"], modulename, plugin, st + ) + with open(filename_c_test, "w") as fd: st.seek(0) shutil.copyfileobj(st, fd) st.close() # Fully autogenerated VATv2 C test file st = StringIO() - generate_c_test2_boilerplate(s['Service'], s['Define'], - modulename, st) - with open(filename_c_test2, 'w') as fd: + generate_c_test2_boilerplate(s["Service"], s["Define"], modulename, st) + with open(filename_c_test2, "w") as fd: st.seek(0) shutil.copyfileobj(st, fd) - st.close() # + st.close() # # Generate separate JSON file st = StringIO() generate_tojson(s, modulename, st) - with open(filename_c_tojson, 'w') as fd: + with open(filename_c_tojson, "w") as fd: st.seek(0) shutil.copyfileobj(st, fd) st.close() st = StringIO() generate_fromjson(s, modulename, st) - with open(filename_c_fromjson, 'w') as fd: + with open(filename_c_fromjson, "w") as fd: st.seek(0) shutil.copyfileobj(st, fd) st.close() - output = TOP_BOILERPLATE.format(datestring=DATESTRING, - input_filename=basename) - output += generate_imports(s['Import']) + output = TOP_BOILERPLATE.format(datestring=DATESTRING, input_filename=basename) + output += generate_imports(s["Import"]) output += msg_ids(s) output += msg_names(s) output += msg_name_crc_list(s, filename) output += typedefs(modulename) - printfun_types(s['types'], stream, modulename) - printfun(s['Define'], stream, modulename) + printfun_types(s["types"], stream, modulename) + printfun(s["Define"], stream, modulename) output += stream.getvalue() stream.close() - output += endianfun(s['types'] + s['Define'], modulename) - output += calc_size_fun(s['types'] + s['Define'], modulename) + output += endianfun(s["types"] + s["Define"], modulename) + output += calc_size_fun(s["types"] + s["Define"], modulename) output += version_tuple(s, basename) - output += BOTTOM_BOILERPLATE.format(input_filename=basename, - file_crc=s['file_crc']) + output += BOTTOM_BOILERPLATE.format(input_filename=basename, file_crc=s["file_crc"]) return output diff --git a/src/tools/vppapigen/vppapigen_crc.py b/src/tools/vppapigen/vppapigen_crc.py index 791e347292e..525f6c07efc 100644 --- a/src/tools/vppapigen/vppapigen_crc.py +++ b/src/tools/vppapigen/vppapigen_crc.py @@ -12,11 +12,10 @@ def run(args, input_filename, s): major = 0 minor = 0 patch = 0 - if 'version' in s['Option']: - v = s['Option']['version'] - (major, minor, patch) = v.split('.') - j['_version'] = {'major': major, 'minor': minor, 'patch': patch} - for t in s['Define']: - j[t.name] = {'crc': f'{t.crc:#08x}', 'version': major, - 'options': t.options} - return json.dumps(j, indent=4, separators=(',', ': ')) + if "version" in s["Option"]: + v = s["Option"]["version"] + (major, minor, patch) = v.split(".") + j["_version"] = {"major": major, "minor": minor, "patch": patch} + for t in s["Define"]: + j[t.name] = {"crc": f"{t.crc:#08x}", "version": major, "options": t.options} + return json.dumps(j, indent=4, separators=(",", ": ")) diff --git a/src/tools/vppapigen/vppapigen_json.py b/src/tools/vppapigen/vppapigen_json.py index 5fa839f9854..695b8cc7aa2 100644 --- a/src/tools/vppapigen/vppapigen_json.py +++ b/src/tools/vppapigen/vppapigen_json.py @@ -14,7 +14,7 @@ def walk_imports(s): def walk_counters(s, pathset): r = [] for e in s: - r2 = {'name': e.name, 'elements': e.block} + r2 = {"name": e.name, "elements": e.block} r.append(r2) r3 = [] @@ -31,7 +31,7 @@ def walk_enums(s): d.append(e.name) for b in e.block: d.append(b) - d.append({'enumtype': e.enumtype}) + d.append({"enumtype": e.enumtype}) r.append(d) return r @@ -39,13 +39,13 @@ def walk_enums(s): def walk_services(s): r = {} for e in s: - d = {'reply': e.reply} + d = {"reply": e.reply} if e.stream: - d['stream'] = True + d["stream"] = True if e.stream_message: - d['stream_msg'] = e.stream_message + d["stream_msg"] = e.stream_message if e.events: - d['events'] = e.events + d["events"] = e.events r[e.caller] = d return r @@ -56,28 +56,27 @@ def walk_defs(s, is_message=False): d = [] d.append(t.name) for b in t.block: - if b.type == 'Option': + if b.type == "Option": continue - if b.type == 'Field': + if b.type == "Field": if b.limit: d.append([b.fieldtype, b.fieldname, b.limit]) else: d.append([b.fieldtype, b.fieldname]) - elif b.type == 'Array': + elif b.type == "Array": if b.lengthfield: - d.append([b.fieldtype, b.fieldname, - b.length, b.lengthfield]) + d.append([b.fieldtype, b.fieldname, b.length, b.lengthfield]) else: d.append([b.fieldtype, b.fieldname, b.length]) - elif b.type == 'Union': + elif b.type == "Union": pass else: raise ValueError("Error in processing array type %s" % b) if is_message and t.crc: c = {} - c['crc'] = "{0:#0{1}x}".format(t.crc, 10) - c['options'] = t.options + c["crc"] = "{0:#0{1}x}".format(t.crc, 10) + c["options"] = t.options d.append(c) r.append(d) @@ -90,19 +89,19 @@ def walk_defs(s, is_message=False): def run(args, filename, s): j = {} - j['types'] = (walk_defs([o for o in s['types'] - if o.__class__.__name__ == 'Typedef'])) - j['messages'] = walk_defs(s['Define'], True) - j['unions'] = (walk_defs([o for o in s['types'] - if o.__class__.__name__ == 'Union'])) - j['enums'] = (walk_enums([o for o in s['types'] - if o.__class__.__name__ == 'Enum'])) - j['enumflags'] = (walk_enums([o for o in s['types'] - if o.__class__.__name__ == 'EnumFlag'])) - j['services'] = walk_services(s['Service']) - j['options'] = s['Option'] - j['aliases'] = {o.name:o.alias for o in s['types'] if o.__class__.__name__ == 'Using'} - j['vl_api_version'] = hex(s['file_crc']) - j['imports'] = walk_imports(i for i in s['Import']) - j['counters'], j['paths'] = walk_counters(s['Counters'], s['Paths']) - return json.dumps(j, indent=4, separators=(',', ': ')) + j["types"] = walk_defs([o for o in s["types"] if o.__class__.__name__ == "Typedef"]) + j["messages"] = walk_defs(s["Define"], True) + j["unions"] = walk_defs([o for o in s["types"] if o.__class__.__name__ == "Union"]) + j["enums"] = walk_enums([o for o in s["types"] if o.__class__.__name__ == "Enum"]) + j["enumflags"] = walk_enums( + [o for o in s["types"] if o.__class__.__name__ == "EnumFlag"] + ) + j["services"] = walk_services(s["Service"]) + j["options"] = s["Option"] + j["aliases"] = { + o.name: o.alias for o in s["types"] if o.__class__.__name__ == "Using" + } + j["vl_api_version"] = hex(s["file_crc"]) + j["imports"] = walk_imports(i for i in s["Import"]) + j["counters"], j["paths"] = walk_counters(s["Counters"], s["Paths"]) + return json.dumps(j, indent=4, separators=(",", ": ")) diff --git a/src/vpp-api/python/setup.py b/src/vpp-api/python/setup.py index 8bf6def2227..18637ba21b7 100644 --- a/src/vpp-api/python/setup.py +++ b/src/vpp-api/python/setup.py @@ -21,15 +21,16 @@ except ImportError: requirements = [] setup( - name='vpp_papi', - version='2.0.0', - description='VPP Python binding', - author='Ole Troan', - author_email='ot@cisco.com', - url='https://wiki.fd.io/view/VPP/Python_API', - license='Apache-2.0', - test_suite='vpp_papi.tests', + name="vpp_papi", + version="2.0.0", + description="VPP Python binding", + author="Ole Troan", + author_email="ot@cisco.com", + url="https://wiki.fd.io/view/VPP/Python_API", + license="Apache-2.0", + test_suite="vpp_papi.tests", install_requires=requirements, packages=find_packages(), - long_description='''VPP Python language binding.''', - zip_safe=True) + long_description="""VPP Python language binding.""", + zip_safe=True, +) diff --git a/src/vpp-api/python/vpp_papi/__init__.py b/src/vpp-api/python/vpp_papi/__init__.py index b2b4fc78fc1..f87b6480d4e 100644 --- a/src/vpp-api/python/vpp_papi/__init__.py +++ b/src/vpp-api/python/vpp_papi/__init__.py @@ -3,7 +3,7 @@ from .vpp_papi import VppEnum, VppEnumType, VppEnumFlag # noqa: F401 from .vpp_papi import VPPIOError, VPPRuntimeError, VPPValueError # noqa: F401 from .vpp_papi import VPPApiClient # noqa: F401 from .vpp_papi import VPPApiJSONFiles # noqa: F401 -from . macaddress import MACAddress, mac_pton, mac_ntop # noqa: F401 +from .macaddress import MACAddress, mac_pton, mac_ntop # noqa: F401 # sorted lexicographically from .vpp_serializer import BaseTypes # noqa: F401 @@ -11,6 +11,7 @@ from .vpp_serializer import VPPEnumType, VPPType, VPPTypeAlias # noqa: F401 from .vpp_serializer import VPPMessage, VPPUnionType # noqa: F401 import pkg_resources # part of setuptools + try: __version__ = pkg_resources.get_distribution("vpp_papi").version except (pkg_resources.DistributionNotFound): diff --git a/src/vpp-api/python/vpp_papi/macaddress.py b/src/vpp-api/python/vpp_papi/macaddress.py index c3b10a3c11e..8799bd7be24 100644 --- a/src/vpp-api/python/vpp_papi/macaddress.py +++ b/src/vpp-api/python/vpp_papi/macaddress.py @@ -18,20 +18,19 @@ import binascii def mac_pton(s): - '''Convert MAC address as text to binary''' - return binascii.unhexlify(s.replace(':', '')) + """Convert MAC address as text to binary""" + return binascii.unhexlify(s.replace(":", "")) def mac_ntop(binary): - '''Convert MAC address as binary to text''' - x = b':'.join(binascii.hexlify(binary)[i:i + 2] - for i in range(0, 12, 2)) - return str(x.decode('ascii')) + """Convert MAC address as binary to text""" + x = b":".join(binascii.hexlify(binary)[i : i + 2] for i in range(0, 12, 2)) + return str(x.decode("ascii")) -class MACAddress(): +class MACAddress: def __init__(self, mac): - '''MAC Address as a text-string (aa:bb:cc:dd:ee:ff) or 6 bytes''' + """MAC Address as a text-string (aa:bb:cc:dd:ee:ff) or 6 bytes""" # Of course Python 2 doesn't distinguish str from bytes if type(mac) is bytes and len(mac) == 6: self.mac_binary = mac @@ -51,7 +50,7 @@ class MACAddress(): return self.mac_string def __repr__(self): - return '%s(%s)' % (self.__class__.__name__, self.mac_string) + return "%s(%s)" % (self.__class__.__name__, self.mac_string) def __eq__(self, other): diff --git a/src/vpp-api/python/vpp_papi/tests/test_macaddress.py b/src/vpp-api/python/vpp_papi/tests/test_macaddress.py index 08e365afd92..e86ec75c76e 100644 --- a/src/vpp-api/python/vpp_papi/tests/test_macaddress.py +++ b/src/vpp-api/python/vpp_papi/tests/test_macaddress.py @@ -3,8 +3,6 @@ from vpp_papi import MACAddress class TestMacAddress(unittest.TestCase): - def test_eq(self): - mac = '11:22:33:44:55:66' - self.assertEqual(MACAddress(mac), - MACAddress(mac)) + mac = "11:22:33:44:55:66" + self.assertEqual(MACAddress(mac), MACAddress(mac)) diff --git a/src/vpp-api/python/vpp_papi/tests/test_vpp_format.py b/src/vpp-api/python/vpp_papi/tests/test_vpp_format.py index 5c179c02e0a..ae4d2c5126d 100644 --- a/src/vpp-api/python/vpp_papi/tests/test_vpp_format.py +++ b/src/vpp-api/python/vpp_papi/tests/test_vpp_format.py @@ -25,57 +25,64 @@ from vpp_papi import vpp_format from parameterized import parameterized -ip4_addr = '1.2.3.4' -ip4_addrn = b'\x01\x02\x03\x04' +ip4_addr = "1.2.3.4" +ip4_addrn = b"\x01\x02\x03\x04" ip4_prefix_len = 32 -ip4_prefix = '%s/%s' % (ip4_addr, ip4_prefix_len) +ip4_prefix = "%s/%s" % (ip4_addr, ip4_prefix_len) ipv4_network = ipaddress.IPv4Network(text_type(ip4_prefix)) -ip4_addr_format_vl_api_address_t = {'un': {'ip4': b'\x01\x02\x03\x04'}, - 'af': 0} -ip4_addr_format_vl_api_prefix_t = {'address': # noqa: E127,E501 - {'un': {'ip4': b'\x01\x02\x03\x04'}, - 'af': 0}, - 'len': ip4_prefix_len} -ip4_addr_format_vl_api_prefix_packed_t = {'address': b'\x01\x02\x03\x04', - 'len': ip4_prefix_len} - -ip6_addr = 'dead::' -ip6_addrn = b'\xde\xad\x00\x00\x00\x00\x00\x00' \ - b'\x00\x00\x00\x00\x00\x00\x00\x00' +ip4_addr_format_vl_api_address_t = {"un": {"ip4": b"\x01\x02\x03\x04"}, "af": 0} +ip4_addr_format_vl_api_prefix_t = { + "address": {"un": {"ip4": b"\x01\x02\x03\x04"}, "af": 0}, # noqa: E127,E501 + "len": ip4_prefix_len, +} +ip4_addr_format_vl_api_prefix_packed_t = { + "address": b"\x01\x02\x03\x04", + "len": ip4_prefix_len, +} + +ip6_addr = "dead::" +ip6_addrn = b"\xde\xad\x00\x00\x00\x00\x00\x00" b"\x00\x00\x00\x00\x00\x00\x00\x00" ip6_prefix_len = 127 -ip6_prefix = '%s/%s' % (ip6_addr, ip6_prefix_len) +ip6_prefix = "%s/%s" % (ip6_addr, ip6_prefix_len) ipv6_network = ipaddress.IPv6Network(text_type(ip6_prefix)) -ip6_addr_format_vl_api_address_t = {'un': {'ip6': b'\xde\xad\x00\x00' - b'\x00\x00\x00\x00' - b'\x00\x00\x00\x00' - b'\x00\x00\x00\x00'}, - 'af': 1} -ip6_addr_format_vl_api_prefix_t = {'address': # noqa: E127 - {'af': 1, - 'un': { - 'ip6': b'\xde\xad\x00\x00' - b'\x00\x00\x00\x00' - b'\x00\x00\x00\x00' - b'\x00\x00\x00\x00'}}, - 'len': ip6_prefix_len} -ip6_addr_format_vl_api_prefix_packed_t = {'address': b'\xde\xad\x00\x00' # noqa: E127,E501 - b'\x00\x00\x00\x00' - b'\x00\x00\x00\x00' - b'\x00\x00\x00\x00', - 'len': ip6_prefix_len} +ip6_addr_format_vl_api_address_t = { + "un": { + "ip6": b"\xde\xad\x00\x00" + b"\x00\x00\x00\x00" + b"\x00\x00\x00\x00" + b"\x00\x00\x00\x00" + }, + "af": 1, +} +ip6_addr_format_vl_api_prefix_t = { + "address": { # noqa: E127 + "af": 1, + "un": { + "ip6": b"\xde\xad\x00\x00" + b"\x00\x00\x00\x00" + b"\x00\x00\x00\x00" + b"\x00\x00\x00\x00" + }, + }, + "len": ip6_prefix_len, +} +ip6_addr_format_vl_api_prefix_packed_t = { + "address": b"\xde\xad\x00\x00" # noqa: E127,E501 + b"\x00\x00\x00\x00" + b"\x00\x00\x00\x00" + b"\x00\x00\x00\x00", + "len": ip6_prefix_len, +} class TestVppFormat(unittest.TestCase): - def test_format_vl_api_address_t(self): res = vpp_format.format_vl_api_address_t(ip4_addr) self.assertEqual(res, ip4_addr_format_vl_api_address_t) # PY2: raises socket.error # PY3: raises OSError - with self.assertRaises((TypeError, - socket.error, - OSError)): + with self.assertRaises((TypeError, socket.error, OSError)): res = vpp_format.format_vl_api_address_t(ip4_addrn) res = vpp_format.format_vl_api_address_t(ip6_addr) @@ -84,19 +91,14 @@ class TestVppFormat(unittest.TestCase): with self.assertRaises(TypeError): es = vpp_format.format_vl_api_address_t(ip6_addrn) - @parameterized.expand([('ip4 prefix', - ip4_prefix, - ip4_addr_format_vl_api_prefix_t), - ('ip6 prefix', - ip6_prefix, - ip6_addr_format_vl_api_prefix_t), - ('IPv4Network', - ipv4_network, - ip4_addr_format_vl_api_prefix_t), - ('IPv6Network', - ipv6_network, - ip6_addr_format_vl_api_prefix_t), - ]) + @parameterized.expand( + [ + ("ip4 prefix", ip4_prefix, ip4_addr_format_vl_api_prefix_t), + ("ip6 prefix", ip6_prefix, ip6_addr_format_vl_api_prefix_t), + ("IPv4Network", ipv4_network, ip4_addr_format_vl_api_prefix_t), + ("IPv6Network", ipv6_network, ip6_addr_format_vl_api_prefix_t), + ] + ) def test_format_vl_api_prefix_t(self, _, arg, expected): res = vpp_format.format_vl_api_prefix_t(arg) self.assertEqual(res, expected) diff --git a/src/vpp-api/python/vpp_papi/tests/test_vpp_papi.py b/src/vpp-api/python/vpp_papi/tests/test_vpp_papi.py index 99acb7c7469..2b21c83966a 100644 --- a/src/vpp-api/python/vpp_papi/tests/test_vpp_papi.py +++ b/src/vpp-api/python/vpp_papi/tests/test_vpp_papi.py @@ -24,7 +24,7 @@ from vpp_papi import vpp_transport_shmem class TestVppPapiVPPApiClient(unittest.TestCase): def test_getcontext(self): - vpp_papi.VPPApiClient.apidir = '.' + vpp_papi.VPPApiClient.apidir = "." c = vpp_papi.VPPApiClient(testmode=True, use_socket=True) # reset initialization at module load time. @@ -39,7 +39,7 @@ class TestVppPapiVPPApiClientMp(unittest.TestCase): # run_tests.py (eg. make test TEST_JOBS=10) def test_get_context_mp(self): - vpp_papi.VPPApiClient.apidir = '.' + vpp_papi.VPPApiClient.apidir = "." c = vpp_papi.VPPApiClient(testmode=True, use_socket=True) # reset initialization at module load time. @@ -243,11 +243,11 @@ class TestVppPapiLogging(unittest.TestCase): pass client = Vpp - with self.assertLogs('vpp_papi', level='DEBUG') as cm: + with self.assertLogs("vpp_papi", level="DEBUG") as cm: vpp_papi.vpp_atexit(client) - self.assertEqual(cm.output, ['DEBUG:vpp_papi:Cleaning up VPP on exit']) + self.assertEqual(cm.output, ["DEBUG:vpp_papi:Cleaning up VPP on exit"]) with self.assertRaises(AssertionError): - with self.assertLogs('vpp_papi.serializer', level='DEBUG') as cm: + with self.assertLogs("vpp_papi.serializer", level="DEBUG") as cm: vpp_papi.vpp_atexit(client) self.assertEqual(cm.output, []) 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 c9b3d672d6a..eee38f00632 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 @@ -13,61 +13,57 @@ from ipaddress import * class TestLimits(unittest.TestCase): def test_string(self): - fixed_string = VPPType('fixed_string', - [['string', 'name', 16]]) + fixed_string = VPPType("fixed_string", [["string", "name", 16]]) - b = fixed_string.pack({'name': 'foobar'}) + b = fixed_string.pack({"name": "foobar"}) self.assertEqual(len(b), 16) # Ensure string is nul terminated - self.assertEqual(b.decode('ascii')[6], '\x00') + self.assertEqual(b.decode("ascii")[6], "\x00") nt, size = fixed_string.unpack(b) self.assertEqual(size, 16) - self.assertEqual(nt.name, 'foobar') + self.assertEqual(nt.name, "foobar") # Empty string - b = fixed_string.pack({'name': ''}) + b = fixed_string.pack({"name": ""}) self.assertEqual(len(b), 16) nt, size = fixed_string.unpack(b) self.assertEqual(size, 16) - self.assertEqual(nt.name, '') + self.assertEqual(nt.name, "") # String too long with self.assertRaises(VPPSerializerValueError): - b = fixed_string.pack({'name': 'foobarfoobar1234'}) + b = fixed_string.pack({"name": "foobarfoobar1234"}) - variable_string = VPPType('variable_string', - [['string', 'name', 0]]) - b = variable_string.pack({'name': 'foobar'}) - self.assertEqual(len(b), 4 + len('foobar')) + variable_string = VPPType("variable_string", [["string", "name", 0]]) + b = variable_string.pack({"name": "foobar"}) + self.assertEqual(len(b), 4 + len("foobar")) nt, size = variable_string.unpack(b) - self.assertEqual(size, 4 + len('foobar')) - self.assertEqual(nt.name, 'foobar') - self.assertEqual(len(nt.name), len('foobar')) + self.assertEqual(size, 4 + len("foobar")) + self.assertEqual(nt.name, "foobar") + self.assertEqual(len(nt.name), len("foobar")) def test_limit(self): - limited_type = VPPType('limited_type_t', - [['string', 'name', 0, {'limit': 16}]]) - unlimited_type = VPPType('limited_type_t', - [['string', 'name', 0]]) + limited_type = VPPType("limited_type_t", [["string", "name", 0, {"limit": 16}]]) + unlimited_type = VPPType("limited_type_t", [["string", "name", 0]]) - b = limited_type.pack({'name': 'foobar'}) + b = limited_type.pack({"name": "foobar"}) self.assertEqual(len(b), 10) - b = unlimited_type.pack({'name': 'foobar'}) + b = unlimited_type.pack({"name": "foobar"}) self.assertEqual(len(b), 10) with self.assertRaises(VPPSerializerValueError): - b = limited_type.pack({'name': 'foobar'*3}) + b = limited_type.pack({"name": "foobar" * 3}) class TestDefaults(unittest.TestCase): def test_defaults(self): - default_type = VPPType('default_type_t', - [['u16', 'mtu', {'default': 1500, 'limit': 0}]]) - without_default_type = VPPType('without_default_type_t', - [['u16', 'mtu']]) + default_type = VPPType( + "default_type_t", [["u16", "mtu", {"default": 1500, "limit": 0}]] + ) + without_default_type = VPPType("without_default_type_t", [["u16", "mtu"]]) b = default_type.pack({}) self.assertEqual(len(b), 2) @@ -76,7 +72,7 @@ class TestDefaults(unittest.TestCase): self.assertEqual(nt.mtu, 1500) # distinguish between parameter 0 and parameter not passed - b = default_type.pack({'mtu': 0}) + b = default_type.pack({"mtu": 0}) self.assertEqual(len(b), 2) nt, size = default_type.unpack(b) self.assertEqual(len(b), size) @@ -90,13 +86,15 @@ class TestDefaults(unittest.TestCase): self.assertEqual(nt.mtu, 0) # default enum type - VPPEnumType('vl_api_enum_t', [["ADDRESS_IP4", 0], - ["ADDRESS_IP6", 1], - {"enumtype": "u32"}]) + VPPEnumType( + "vl_api_enum_t", + [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {"enumtype": "u32"}], + ) - default_with_enum = VPPType('default_enum_type_t', - [['u16', 'mtu'], ['vl_api_enum_t', - 'e', {'default': 1}]]) + default_with_enum = VPPType( + "default_enum_type_t", + [["u16", "mtu"], ["vl_api_enum_t", "e", {"default": 1}]], + ) b = default_with_enum.pack({}) self.assertEqual(len(b), 6) @@ -106,275 +104,275 @@ class TestDefaults(unittest.TestCase): class TestAddType(unittest.TestCase): - def test_union(self): - un = VPPUnionType('test_union', - [['u8', 'is_bool'], - ['u32', 'is_int']]) + un = VPPUnionType("test_union", [["u8", "is_bool"], ["u32", "is_int"]]) - b = un.pack({'is_int': 0x12345678}) + b = un.pack({"is_int": 0x12345678}) nt, size = un.unpack(b) self.assertEqual(len(b), size) self.assertEqual(nt.is_bool, 0x12) self.assertEqual(nt.is_int, 0x12345678) def test_address(self): - af = VPPEnumType('vl_api_address_family_t', [["ADDRESS_IP4", 0], - ["ADDRESS_IP6", 1], - {"enumtype": "u32"}]) - aff = VPPEnumFlagType('vl_api_address_family_flag_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']]) - - va_address_list = VPPType('list_addresses', - [['u8', 'count'], - ['vl_api_address_t', 'addresses', - 0, 'count']]) - - message_with_va_address_list = VPPType('msg_with_vla', - [['list_addresses', - 'vla_address'], - ['u8', 'is_cool']]) - - b = ip4.pack(inet_pton(AF_INET, '1.1.1.1')) + af = VPPEnumType( + "vl_api_address_family_t", + [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {"enumtype": "u32"}], + ) + aff = VPPEnumFlagType( + "vl_api_address_family_flag_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"]] + ) + + va_address_list = VPPType( + "list_addresses", + [["u8", "count"], ["vl_api_address_t", "addresses", 0, "count"]], + ) + + message_with_va_address_list = VPPType( + "msg_with_vla", [["list_addresses", "vla_address"], ["u8", "is_cool"]] + ) + + b = ip4.pack(inet_pton(AF_INET, "1.1.1.1")) self.assertEqual(len(b), 4) nt, size = ip4.unpack(b) - self.assertEqual(str(nt), '1.1.1.1') + self.assertEqual(str(nt), "1.1.1.1") - b = ip6.pack(inet_pton(AF_INET6, '1::1')) + b = ip6.pack(inet_pton(AF_INET6, "1::1")) self.assertEqual(len(b), 16) - b = address.pack({'af': af.ADDRESS_IP4, - 'un': - {'ip4': inet_pton(AF_INET, '2.2.2.2')}}) + b = address.pack( + {"af": af.ADDRESS_IP4, "un": {"ip4": inet_pton(AF_INET, "2.2.2.2")}} + ) self.assertEqual(len(b), 20) nt, size = address.unpack(b) - self.assertEqual(str(nt), '2.2.2.2') + self.assertEqual(str(nt), "2.2.2.2") # List of addresses address_list = [] for i in range(4): - address_list.append({'af': af.ADDRESS_IP4, - 'un': - {'ip4': inet_pton(AF_INET, '2.2.2.2')}}) - b = va_address_list.pack({'count': len(address_list), - 'addresses': address_list}) + address_list.append( + {"af": af.ADDRESS_IP4, "un": {"ip4": inet_pton(AF_INET, "2.2.2.2")}} + ) + b = va_address_list.pack( + {"count": len(address_list), "addresses": address_list} + ) self.assertEqual(len(b), 81) nt, size = va_address_list.unpack(b) - self.assertEqual(str(nt.addresses[0]), '2.2.2.2') - - b = message_with_va_address_list.pack({'vla_address': - {'count': len(address_list), - 'addresses': address_list}, - 'is_cool': 100}) + self.assertEqual(str(nt.addresses[0]), "2.2.2.2") + + b = message_with_va_address_list.pack( + { + "vla_address": {"count": len(address_list), "addresses": address_list}, + "is_cool": 100, + } + ) self.assertEqual(len(b), 82) 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']]) + 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')) + 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') + self.assertEqual(str(nt), "2.2.2.2/24") - b = address_with_prefix.pack(IPv6Interface('2::2/64')) + 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') + self.assertEqual(str(nt), "2::2/64") - b = address_with_prefix.pack(IPv4Network('2.2.2.2/24', strict=False)) + 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') + self.assertEqual(str(nt), "2.2.2.0/24") - b = address4_with_prefix.pack('2.2.2.2/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(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') + 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(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') + 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') + self.assertEqual(str(nt), "192.168.10.0/24") - b = awp_type.pack({'address': '1.2.3.4/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') + self.assertEqual(str(nt.address), "1.2.3.4/24") - b = awp_type.pack({'address': IPv4Interface('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') + 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], - {"enumtype": "u32"}]) - ip4 = VPPTypeAlias('vl_api_ip4_address_t', {'type': 'u8', - 'length': 4}) - b = ip4.pack('1.1.1.1') + 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}) + b = ip4.pack("1.1.1.1") self.assertEqual(len(b), 4) nt, size = ip4.unpack(b) - self.assertEqual(str(nt), '1.1.1.1') + self.assertEqual(str(nt), "1.1.1.1") - 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"]]) + 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']]) + 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']]) - message = VPPMessage('svs', - [['vl_api_prefix_t', 'prefix']]) - message_addr = VPPMessage('svs_address', - [['vl_api_address_t', 'address']]) + prefix = VPPType( + "vl_api_prefix_t", [["vl_api_address_t", "address"], ["u8", "len"]] + ) + message = VPPMessage("svs", [["vl_api_prefix_t", "prefix"]]) + message_addr = VPPMessage("svs_address", [["vl_api_address_t", "address"]]) - b = message_addr.pack({'address': "1::1"}) + b = message_addr.pack({"address": "1::1"}) self.assertEqual(len(b), 20) nt, size = message_addr.unpack(b) self.assertEqual("1::1", str(nt.address)) - b = message_addr.pack({'address': "1.1.1.1"}) + b = message_addr.pack({"address": "1.1.1.1"}) self.assertEqual(len(b), 20) nt, size = message_addr.unpack(b) self.assertEqual("1.1.1.1", str(nt.address)) - b = message.pack({'prefix': "1.1.1.0/24"}) + b = message.pack({"prefix": "1.1.1.0/24"}) self.assertEqual(len(b), 21) nt, size = message.unpack(b) self.assertEqual("1.1.1.0/24", str(nt.prefix)) - message_array = VPPMessage('address_array', - [['vl_api_ip6_address_t', - 'addresses', 2]]) - b = message_array.pack({'addresses': [IPv6Address(u"1::1"), "2::2"]}) + message_array = VPPMessage( + "address_array", [["vl_api_ip6_address_t", "addresses", 2]] + ) + b = message_array.pack({"addresses": [IPv6Address("1::1"), "2::2"]}) self.assertEqual(len(b), 32) - message_array_vla = VPPMessage('address_array_vla', - [['u32', 'num'], - ['vl_api_ip6_address_t', - 'addresses', 0, 'num']]) - b = message_array_vla.pack({'addresses': ["1::1", "2::2"], 'num': 2}) + message_array_vla = VPPMessage( + "address_array_vla", + [["u32", "num"], ["vl_api_ip6_address_t", "addresses", 0, "num"]], + ) + b = message_array_vla.pack({"addresses": ["1::1", "2::2"], "num": 2}) self.assertEqual(len(b), 36) - message_array4 = VPPMessage('address_array4', - [['vl_api_ip4_address_t', - 'addresses', 2]]) - b = message_array4.pack({'addresses': ["1.1.1.1", "2.2.2.2"]}) + message_array4 = VPPMessage( + "address_array4", [["vl_api_ip4_address_t", "addresses", 2]] + ) + b = message_array4.pack({"addresses": ["1.1.1.1", "2.2.2.2"]}) self.assertEqual(len(b), 8) - b = message_array4.pack({'addresses': [IPv4Address(u"1.1.1.1"), - "2.2.2.2"]}) + b = message_array4.pack({"addresses": [IPv4Address("1.1.1.1"), "2.2.2.2"]}) self.assertEqual(len(b), 8) - message = VPPMessage('address', [['vl_api_address_t', 'address']]) - b = message.pack({'address': '1::1'}) + message = VPPMessage("address", [["vl_api_address_t", "address"]]) + b = message.pack({"address": "1::1"}) self.assertEqual(len(b), 20) - b = message.pack({'address': '1.1.1.1'}) + b = message.pack({"address": "1.1.1.1"}) self.assertEqual(len(b), 20) - message = VPPMessage('prefix', [['vl_api_prefix_t', 'prefix']]) - b = message.pack({'prefix': '1::1/130'}) + message = VPPMessage("prefix", [["vl_api_prefix_t", "prefix"]]) + b = message.pack({"prefix": "1::1/130"}) self.assertEqual(len(b), 21) - b = message.pack({'prefix': IPv6Network(u'1::/119')}) + b = message.pack({"prefix": IPv6Network("1::/119")}) self.assertEqual(len(b), 21) - b = message.pack({'prefix': IPv4Network(u'1.1.0.0/16')}) + b = message.pack({"prefix": IPv4Network("1.1.0.0/16")}) self.assertEqual(len(b), 21) def test_zero_vla(self): - '''Default zero'ed out for VLAs''' - list = VPPType('vl_api_list_t', - [['u8', 'count', 10]]) + """Default zero'ed out for VLAs""" + list = VPPType("vl_api_list_t", [["u8", "count", 10]]) # Define an embedded VLA type - valist = VPPType('vl_api_valist_t', - [['u8', 'count'], - ['u8', 'string', 0, 'count']]) + valist = VPPType( + "vl_api_valist_t", [["u8", "count"], ["u8", "string", 0, "count"]] + ) # Define a message - vamessage = VPPMessage('vamsg', - [['vl_api_valist_t', 'valist'], - ['u8', 'is_something']]) + vamessage = VPPMessage( + "vamsg", [["vl_api_valist_t", "valist"], ["u8", "is_something"]] + ) - message = VPPMessage('msg', - [['vl_api_list_t', 'list'], - ['u8', 'is_something']]) + message = VPPMessage("msg", [["vl_api_list_t", "list"], ["u8", "is_something"]]) # Pack message without VLA specified - b = message.pack({'is_something': 1}) - b = vamessage.pack({'is_something': 1}) + b = message.pack({"is_something": 1}) + b = vamessage.pack({"is_something": 1}) def test_arrays(self): # Test cases @@ -382,254 +380,275 @@ class TestAddType(unittest.TestCase): # 2. Fixed list of variable length sub type # 3. Variable length type # - s = VPPType('str', [['u32', 'length'], - ['u8', 'string', 0, 'length']]) + s = VPPType("str", [["u32", "length"], ["u8", "string", 0, "length"]]) - ip4 = VPPType('ip4_address', [['u8', 'address', 4]]) - listip4 = VPPType('list_ip4_t', [['ip4_address', 'addresses', 4]]) - valistip4 = VPPType('list_ip4_t', - [['u8', 'count'], - ['ip4_address', 'addresses', 0, 'count']]) + ip4 = VPPType("ip4_address", [["u8", "address", 4]]) + listip4 = VPPType("list_ip4_t", [["ip4_address", "addresses", 4]]) + valistip4 = VPPType( + "list_ip4_t", [["u8", "count"], ["ip4_address", "addresses", 0, "count"]] + ) - valistip4_legacy = VPPType('list_ip4_t', - [['u8', 'foo'], - ['ip4_address', 'addresses', 0]]) + valistip4_legacy = VPPType( + "list_ip4_t", [["u8", "foo"], ["ip4_address", "addresses", 0]] + ) addresses = [] for i in range(4): - addresses.append({'address': inet_pton(AF_INET, '2.2.2.2')}) - b = listip4.pack({'addresses': addresses}) + addresses.append({"address": inet_pton(AF_INET, "2.2.2.2")}) + b = listip4.pack({"addresses": addresses}) self.assertEqual(len(b), 16) nt, size = listip4.unpack(b) - self.assertEqual(nt.addresses[0].address, - inet_pton(AF_INET, '2.2.2.2')) + self.assertEqual(nt.addresses[0].address, inet_pton(AF_INET, "2.2.2.2")) - b = valistip4.pack({'count': len(addresses), 'addresses': addresses}) + b = valistip4.pack({"count": len(addresses), "addresses": addresses}) self.assertEqual(len(b), 17) nt, size = valistip4.unpack(b) self.assertEqual(nt.count, 4) - self.assertEqual(nt.addresses[0].address, - inet_pton(AF_INET, '2.2.2.2')) + self.assertEqual(nt.addresses[0].address, inet_pton(AF_INET, "2.2.2.2")) - b = valistip4_legacy.pack({'foo': 1, 'addresses': addresses}) + b = valistip4_legacy.pack({"foo": 1, "addresses": addresses}) self.assertEqual(len(b), 17) nt, size = valistip4_legacy.unpack(b) self.assertEqual(len(nt.addresses), 4) - self.assertEqual(nt.addresses[0].address, - inet_pton(AF_INET, '2.2.2.2')) + self.assertEqual(nt.addresses[0].address, inet_pton(AF_INET, "2.2.2.2")) - string = 'foobar foobar' - b = s.pack({'length': len(string), 'string': string.encode('utf-8')}) + string = "foobar foobar" + b = s.pack({"length": len(string), "string": string.encode("utf-8")}) nt, size = s.unpack(b) self.assertEqual(len(b), size) def test_string(self): - s = VPPType('str', [['u32', 'length'], - ['u8', 'string', 0, 'length']]) + s = VPPType("str", [["u32", "length"], ["u8", "string", 0, "length"]]) - string = '' - b = s.pack({'length': len(string), 'string': string.encode('utf-8')}) + string = "" + b = s.pack({"length": len(string), "string": string.encode("utf-8")}) nt, size = s.unpack(b) self.assertEqual(len(b), size) def test_message(self): - foo = VPPMessage('foo', [['u16', '_vl_msg_id'], - ['u8', 'client_index'], - ['u8', 'something'], - {"crc": "0x559b9f3c"}]) - b = foo.pack({'_vl_msg_id': 1, 'client_index': 5, - 'something': 200}) + foo = VPPMessage( + "foo", + [ + ["u16", "_vl_msg_id"], + ["u8", "client_index"], + ["u8", "something"], + {"crc": "0x559b9f3c"}, + ], + ) + b = foo.pack({"_vl_msg_id": 1, "client_index": 5, "something": 200}) nt, size = foo.unpack(b) self.assertEqual(len(b), size) self.assertEqual(nt.something, 200) def test_abf(self): - fib_mpls_label = VPPType('vl_api_fib_mpls_label_t', - [['u8', 'is_uniform'], - ['u32', 'label'], - ['u8', 'ttl'], - ['u8', 'exp']]) + fib_mpls_label = VPPType( + "vl_api_fib_mpls_label_t", + [["u8", "is_uniform"], ["u32", "label"], ["u8", "ttl"], ["u8", "exp"]], + ) - label_stack = {'is_uniform': 0, - 'label': 0, - 'ttl': 0, - 'exp': 0} + label_stack = {"is_uniform": 0, "label": 0, "ttl": 0, "exp": 0} b = fib_mpls_label.pack(label_stack) self.assertEqual(len(b), 7) - fib_path = VPPType('vl_api_fib_path_t', - [['u32', 'sw_if_index'], - ['u32', 'table_id'], - ['u8', 'weight'], - ['u8', 'preference'], - ['u8', 'is_local'], - ['u8', 'is_drop'], - ['u8', 'is_udp_encap'], - ['u8', 'is_unreach'], - ['u8', 'is_prohibit'], - ['u8', 'is_resolve_host'], - ['u8', 'is_resolve_attached'], - ['u8', 'is_dvr'], - ['u8', 'is_source_lookup'], - ['u8', 'afi'], - ['u8', 'next_hop', 16], - ['u32', 'next_hop_id'], - ['u32', 'rpf_id'], - ['u32', 'via_label'], - ['u8', 'n_labels'], - ['vl_api_fib_mpls_label_t', 'label_stack', 16]]) + fib_path = VPPType( + "vl_api_fib_path_t", + [ + ["u32", "sw_if_index"], + ["u32", "table_id"], + ["u8", "weight"], + ["u8", "preference"], + ["u8", "is_local"], + ["u8", "is_drop"], + ["u8", "is_udp_encap"], + ["u8", "is_unreach"], + ["u8", "is_prohibit"], + ["u8", "is_resolve_host"], + ["u8", "is_resolve_attached"], + ["u8", "is_dvr"], + ["u8", "is_source_lookup"], + ["u8", "afi"], + ["u8", "next_hop", 16], + ["u32", "next_hop_id"], + ["u32", "rpf_id"], + ["u32", "via_label"], + ["u8", "n_labels"], + ["vl_api_fib_mpls_label_t", "label_stack", 16], + ], + ) label_stack_list = [] for i in range(16): label_stack_list.append(label_stack) - paths = {'is_udp_encap': 0, - 'next_hop': b'\x10\x02\x02\xac', - 'table_id': 0, - 'afi': 0, - 'weight': 1, - 'next_hop_id': 4294967295, - 'label_stack': label_stack_list, - 'n_labels': 0, - 'sw_if_index': 4294967295, - 'preference': 0} + paths = { + "is_udp_encap": 0, + "next_hop": b"\x10\x02\x02\xac", + "table_id": 0, + "afi": 0, + "weight": 1, + "next_hop_id": 4294967295, + "label_stack": label_stack_list, + "n_labels": 0, + "sw_if_index": 4294967295, + "preference": 0, + } b = fib_path.pack(paths) - self.assertEqual(len(b), (7*16) + 49) + self.assertEqual(len(b), (7 * 16) + 49) - abf_policy = VPPType('vl_api_abf_policy_t', - [['u32', 'policy_id'], - ['u32', 'acl_index'], - ['u8', 'n_paths'], - ['vl_api_fib_path_t', 'paths', 0, 'n_paths']]) + abf_policy = VPPType( + "vl_api_abf_policy_t", + [ + ["u32", "policy_id"], + ["u32", "acl_index"], + ["u8", "n_paths"], + ["vl_api_fib_path_t", "paths", 0, "n_paths"], + ], + ) - policy = { - 'n_paths': 1, - 'paths': [paths], - 'acl_index': 0, - 'policy_id': 10} + policy = {"n_paths": 1, "paths": [paths], "acl_index": 0, "policy_id": 10} b = abf_policy.pack(policy) - self.assertEqual(len(b), (7*16) + 49 + 9) - - abf_policy_add_del = VPPMessage('abf_policy_add_del', - [['u16', '_vl_msg_id'], - ['u32', 'client_index'], - ['u32', 'context'], - ['u8', 'is_add'], - ['vl_api_abf_policy_t', 'policy']]) - - b = abf_policy_add_del.pack({'is_add': 1, - 'context': 66, - '_vl_msg_id': 1066, - 'policy': policy}) + self.assertEqual(len(b), (7 * 16) + 49 + 9) + + abf_policy_add_del = VPPMessage( + "abf_policy_add_del", + [ + ["u16", "_vl_msg_id"], + ["u32", "client_index"], + ["u32", "context"], + ["u8", "is_add"], + ["vl_api_abf_policy_t", "policy"], + ], + ) + + b = abf_policy_add_del.pack( + {"is_add": 1, "context": 66, "_vl_msg_id": 1066, "policy": policy} + ) nt, size = abf_policy_add_del.unpack(b) - self.assertEqual(nt.policy.paths[0].next_hop, - b'\x10\x02\x02\xac\x00\x00\x00\x00' - b'\x00\x00\x00\x00\x00\x00\x00\x00') + self.assertEqual( + nt.policy.paths[0].next_hop, + b"\x10\x02\x02\xac\x00\x00\x00\x00" b"\x00\x00\x00\x00\x00\x00\x00\x00", + ) def test_bier(self): - bier_table_id = VPPType('vl_api_bier_table_id_t', - [['u8', 'bt_set'], - ['u8', 'bt_sub_domain'], - ['u8', 'bt_hdr_len_id']]) + bier_table_id = VPPType( + "vl_api_bier_table_id_t", + [["u8", "bt_set"], ["u8", "bt_sub_domain"], ["u8", "bt_hdr_len_id"]], + ) - bier_imp_add = VPPMessage('bier_imp_add', - [['u32', 'client_index'], - ['u32', 'context'], - ['vl_api_bier_table_id_t', 'bi_tbl_id'], - ['u16', 'bi_src'], - ['u8', 'bi_n_bytes'], - ['u8', 'bi_bytes', 0, 'bi_n_bytes']]) + bier_imp_add = VPPMessage( + "bier_imp_add", + [ + ["u32", "client_index"], + ["u32", "context"], + ["vl_api_bier_table_id_t", "bi_tbl_id"], + ["u16", "bi_src"], + ["u8", "bi_n_bytes"], + ["u8", "bi_bytes", 0, "bi_n_bytes"], + ], + ) - table_id = {'bt_set': 0, - 'bt_sub_domain': 0, - 'bt_hdr_len_id': 0} + table_id = {"bt_set": 0, "bt_sub_domain": 0, "bt_hdr_len_id": 0} - bibytes = b'foobar' + bibytes = b"foobar" - b = bier_imp_add.pack({'bi_tbl_id': table_id, - 'bi_n_bytes': len(bibytes), - 'bi_bytes': bibytes}) + b = bier_imp_add.pack( + {"bi_tbl_id": table_id, "bi_n_bytes": len(bibytes), "bi_bytes": bibytes} + ) self.assertEqual(len(b), 20) def test_lisp(self): - VPPEnumType('vl_api_eid_type_t', - [["EID_TYPE_API_PREFIX", 0], - ["EID_TYPE_API_MAC", 1], - ["EID_TYPE_API_NSH", 2], - {"enumtype": "u32"}]) - - VPPTypeAlias('vl_api_mac_address_t', {'type': 'u8', - 'length': 6}) - - VPPType('vl_api_nsh_t', - [["u32", "spi"], - ["u8", "si"]]) - - VPPEnumType('vl_api_address_family_t', [["ADDRESS_IP4", 0], - ["ADDRESS_IP6", 1], - {"enumtype": "u32"}]) - VPPTypeAlias('vl_api_ip4_address_t', {'type': 'u8', - 'length': 4}) - 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"]]) - - VPPType('vl_api_address_t', - [['vl_api_address_family_t', 'af'], - ['vl_api_address_union_t', 'un']]) - - VPPType('vl_api_prefix_t', - [['vl_api_address_t', 'address'], - ['u8', 'len']]) - - VPPUnionType('vl_api_eid_address_t', - [["vl_api_prefix_t", "prefix"], - ["vl_api_mac_address_t", "mac"], - ["vl_api_nsh_t", "nsh"]]) - - eid = VPPType('vl_api_eid_t', - [["vl_api_eid_type_t", "type"], - ["vl_api_eid_address_t", "address"]]) - - b = eid.pack({'type':1, - 'address': { - 'mac': MACAddress('aa:bb:cc:dd:ee:ff')}}) + VPPEnumType( + "vl_api_eid_type_t", + [ + ["EID_TYPE_API_PREFIX", 0], + ["EID_TYPE_API_MAC", 1], + ["EID_TYPE_API_NSH", 2], + {"enumtype": "u32"}, + ], + ) + + VPPTypeAlias("vl_api_mac_address_t", {"type": "u8", "length": 6}) + + VPPType("vl_api_nsh_t", [["u32", "spi"], ["u8", "si"]]) + + VPPEnumType( + "vl_api_address_family_t", + [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {"enumtype": "u32"}], + ) + VPPTypeAlias("vl_api_ip4_address_t", {"type": "u8", "length": 4}) + 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"]], + ) + + VPPType( + "vl_api_address_t", + [["vl_api_address_family_t", "af"], ["vl_api_address_union_t", "un"]], + ) + + VPPType("vl_api_prefix_t", [["vl_api_address_t", "address"], ["u8", "len"]]) + + VPPUnionType( + "vl_api_eid_address_t", + [ + ["vl_api_prefix_t", "prefix"], + ["vl_api_mac_address_t", "mac"], + ["vl_api_nsh_t", "nsh"], + ], + ) + + eid = VPPType( + "vl_api_eid_t", + [["vl_api_eid_type_t", "type"], ["vl_api_eid_address_t", "address"]], + ) + + b = eid.pack({"type": 1, "address": {"mac": MACAddress("aa:bb:cc:dd:ee:ff")}}) self.assertEqual(len(b), 25) nt, size = eid.unpack(b) - self.assertEqual(str(nt.address.mac), 'aa:bb:cc:dd:ee:ff') + self.assertEqual(str(nt.address.mac), "aa:bb:cc:dd:ee:ff") self.assertIsNone(nt.address.prefix) class TestVppSerializerLogging(unittest.TestCase): - def test_logger(self): # test logger name 'vpp_papi.serializer' with self.assertRaises(VPPSerializerValueError) as ctx: - with self.assertLogs('vpp_papi.serializer', level='DEBUG') as cm: - u = VPPUnionType('vl_api_eid_address_t', - [["vl_api_prefix_t", "prefix"], - ["vl_api_mac_address_t", "mac"], - ["vl_api_nsh_t", "nsh"]]) - self.assertEqual(cm.output, ["DEBUG:vpp_papi.serializer:Unknown union type vl_api_prefix_t"]) + with self.assertLogs("vpp_papi.serializer", level="DEBUG") as cm: + u = VPPUnionType( + "vl_api_eid_address_t", + [ + ["vl_api_prefix_t", "prefix"], + ["vl_api_mac_address_t", "mac"], + ["vl_api_nsh_t", "nsh"], + ], + ) + self.assertEqual( + cm.output, ["DEBUG:vpp_papi.serializer:Unknown union type vl_api_prefix_t"] + ) # test parent logger name 'vpp_papi' with self.assertRaises(VPPSerializerValueError) as ctx: - with self.assertLogs('vpp_papi', level='DEBUG') as cm: - u = VPPUnionType('vl_api_eid_address_t', - [["vl_api_prefix_t", "prefix"], - ["vl_api_mac_address_t", "mac"], - ["vl_api_nsh_t", "nsh"]]) - self.assertEqual(cm.output, ["DEBUG:vpp_papi.serializer:Unknown union type vl_api_prefix_t"]) - - -if __name__ == '__main__': + with self.assertLogs("vpp_papi", level="DEBUG") as cm: + u = VPPUnionType( + "vl_api_eid_address_t", + [ + ["vl_api_prefix_t", "prefix"], + ["vl_api_mac_address_t", "mac"], + ["vl_api_nsh_t", "nsh"], + ], + ) + self.assertEqual( + cm.output, ["DEBUG:vpp_papi.serializer:Unknown union type vl_api_prefix_t"] + ) + + +if __name__ == "__main__": unittest.main() diff --git a/src/vpp-api/python/vpp_papi/vpp_format.py b/src/vpp-api/python/vpp_papi/vpp_format.py index 0b85eb4fcb6..f80a781c753 100644 --- a/src/vpp-api/python/vpp_papi/vpp_format.py +++ b/src/vpp-api/python/vpp_papi/vpp_format.py @@ -25,8 +25,8 @@ ADDRESS_IP6 = 1 def verify_enum_hint(e): - return (e.ADDRESS_IP4.value == ADDRESS_IP4) and\ - (e.ADDRESS_IP6.value == ADDRESS_IP6) + return (e.ADDRESS_IP4.value == ADDRESS_IP4) and (e.ADDRESS_IP6.value == ADDRESS_IP6) + # # Type conversion for input arguments and return values @@ -35,146 +35,128 @@ def verify_enum_hint(e): def format_vl_api_address_t(args): try: - return {'un': {'ip6': inet_pton(AF_INET6, args)}, - 'af': ADDRESS_IP6} + return {"un": {"ip6": inet_pton(AF_INET6, args)}, "af": ADDRESS_IP6} # PY2: raises socket.error # PY3: raises OSError except (socket.error, OSError): - return {'un': {'ip4': inet_pton(AF_INET, args)}, - 'af': ADDRESS_IP4} + return {"un": {"ip4": inet_pton(AF_INET, args)}, "af": ADDRESS_IP4} def format_vl_api_prefix_t(args): if isinstance(args, (ipaddress.IPv4Network, ipaddress.IPv6Network)): - return {'address': format_vl_api_address_t( - str(args.network_address)), - 'len': int(args.prefixlen)} - p, length = args.split('/') - return {'address': format_vl_api_address_t(p), - 'len': int(length)} + return { + "address": format_vl_api_address_t(str(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_address_with_prefix_t(args): if isinstance(args, (ipaddress.IPv4Interface, ipaddress.IPv6Interface)): - return {'address': format_vl_api_address_t( - str(args.network_address)), - 'len': int(args.prefixlen)} - p, length = args.split('/') - return {'address': format_vl_api_address_t(p), - 'len': int(length)} + return { + "address": format_vl_api_address_t(str(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): - return {'address': args.network_address.packed, - 'len': int(args.prefixlen)} - p, length = args.split('/') - return {'address': inet_pton(AF_INET6, p), - 'len': int(length)} + 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_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)} + 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): - return {'address': args.network_address.packed, - 'len': int(args.prefixlen)} - p, length = args.split('/') - return {'address': inet_pton(AF_INET, p), - 'len': int(length)} + return {"address": args.network_address.packed, "len": int(args.prefixlen)} + p, length = args.split("/") + 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)} + 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': - { - 'IPv6Address': lambda o: o.packed, - 'str': lambda s: inet_pton(AF_INET6, s) + "vl_api_ip6_address_t": { + "IPv6Address": lambda o: o.packed, + "str": lambda s: inet_pton(AF_INET6, s), + }, + "vl_api_ip4_address_t": { + "IPv4Address": lambda o: o.packed, + "str": lambda s: inet_pton(AF_INET, s), }, - 'vl_api_ip4_address_t': - { - 'IPv4Address': lambda o: o.packed, - 'str': lambda s: inet_pton(AF_INET, s) + "vl_api_ip6_prefix_t": { + "IPv6Network": lambda o: { + "address": o.network_address.packed, + "len": o.prefixlen, + }, + "str": lambda s: format_vl_api_ip6_prefix_t(s), }, - 'vl_api_ip6_prefix_t': - { - 'IPv6Network': lambda o: {'address': o.network_address.packed, - 'len': o.prefixlen}, - 'str': lambda s: format_vl_api_ip6_prefix_t(s) + "vl_api_ip4_prefix_t": { + "IPv4Network": lambda o: { + "address": o.network_address.packed, + "len": o.prefixlen, + }, + "str": lambda s: format_vl_api_ip4_prefix_t(s), }, - 'vl_api_ip4_prefix_t': - { - 'IPv4Network': lambda o: {'address': o.network_address.packed, - 'len': o.prefixlen}, - 'str': lambda s: format_vl_api_ip4_prefix_t(s) + "vl_api_address_t": { + "IPv4Address": lambda o: {"af": ADDRESS_IP4, "un": {"ip4": o.packed}}, + "IPv6Address": lambda o: {"af": ADDRESS_IP6, "un": {"ip6": o.packed}}, + "str": lambda s: format_vl_api_address_t(s), }, - 'vl_api_address_t': - { - 'IPv4Address': lambda o: {'af': ADDRESS_IP4, 'un': {'ip4': o.packed}}, - 'IPv6Address': lambda o: {'af': ADDRESS_IP6, 'un': {'ip6': o.packed}}, - 'str': lambda s: format_vl_api_address_t(s) + "vl_api_prefix_t": { + "IPv4Network": lambda o: { + "address": {"af": ADDRESS_IP4, "un": {"ip4": o.network_address.packed}}, + "len": o.prefixlen, + }, + "IPv6Network": lambda o: { + "address": {"af": ADDRESS_IP6, "un": {"ip6": o.network_address.packed}}, + "len": o.prefixlen, + }, + "str": lambda s: format_vl_api_prefix_t(s), }, - 'vl_api_prefix_t': - { - 'IPv4Network': lambda o: {'address': - {'af': ADDRESS_IP4, 'un': - {'ip4': o.network_address.packed}}, - 'len': o.prefixlen}, - 'IPv6Network': lambda o: {'address': - {'af': ADDRESS_IP6, 'un': - {'ip6': o.network_address.packed}}, - '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_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_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_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, + "str": lambda s: macaddress.mac_pton(s), }, - 'vl_api_mac_address_t': - { - 'MACAddress': lambda o: o.packed, - 'str': lambda s: macaddress.mac_pton(s) + "vl_api_timestamp_t": { + "datetime.datetime": lambda o: ( + o - datetime.datetime(1970, 1, 1) + ).total_seconds() }, - 'vl_api_timestamp_t': - { - 'datetime.datetime': lambda o: - (o - datetime.datetime(1970, 1, 1)).total_seconds() - } } @@ -197,7 +179,7 @@ def unformat_api_prefix_t(o): 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)) + raise ValueError("Unknown instance {}", format(o)) def unformat_api_address_with_prefix_t(o): @@ -217,16 +199,20 @@ def unformat_api_ip6_address_with_prefix_t(o): conversion_unpacker_table = { - 'vl_api_ip6_address_t': lambda o: ipaddress.IPv6Address(o), - 'vl_api_ip6_prefix_t': lambda o: ipaddress.IPv6Network((o.address, o.len)), - 'vl_api_ip4_address_t': lambda o: ipaddress.IPv4Address(o), - '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), + "vl_api_ip6_address_t": lambda o: ipaddress.IPv6Address(o), + "vl_api_ip6_prefix_t": lambda o: ipaddress.IPv6Network((o.address, o.len)), + "vl_api_ip4_address_t": lambda o: ipaddress.IPv4Address(o), + "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_papi.py b/src/vpp-api/python/vpp_papi/vpp_papi.py index 3465f503e9e..1e5d23e59b7 100644 --- a/src/vpp-api/python/vpp_papi/vpp_papi.py +++ b/src/vpp-api/python/vpp_papi/vpp_papi.py @@ -30,13 +30,14 @@ import fnmatch import weakref import atexit import time -from . vpp_format import verify_enum_hint -from . vpp_serializer import VPPType, VPPEnumType, VPPEnumFlagType, VPPUnionType -from . vpp_serializer import VPPMessage, vpp_get_type, VPPTypeAlias +from .vpp_format import verify_enum_hint +from .vpp_serializer import VPPType, VPPEnumType, VPPEnumFlagType, VPPUnionType +from .vpp_serializer import VPPMessage, vpp_get_type, VPPTypeAlias try: import VppTransport except ModuleNotFoundError: + class V: """placeholder for VppTransport as the implementation is dependent on VPPAPIClient's initialization values @@ -44,15 +45,22 @@ except ModuleNotFoundError: VppTransport = V -from . vpp_transport_socket import VppTransport +from .vpp_transport_socket import VppTransport -logger = logging.getLogger('vpp_papi') +logger = logging.getLogger("vpp_papi") logger.addHandler(logging.NullHandler()) -__all__ = ('FuncWrapper', 'VppApiDynamicMethodHolder', - 'VppEnum', 'VppEnumType', 'VppEnumFlag', - 'VPPIOError', 'VPPRuntimeError', 'VPPValueError', - 'VPPApiClient', ) +__all__ = ( + "FuncWrapper", + "VppApiDynamicMethodHolder", + "VppEnum", + "VppEnumType", + "VppEnumFlag", + "VPPIOError", + "VPPRuntimeError", + "VPPValueError", + "VPPApiClient", +) def metaclass(metaclass): @@ -83,7 +91,7 @@ def vpp_atexit(vpp_weakref): """Clean up VPP connection on shutdown.""" vpp_instance = vpp_weakref() if vpp_instance and vpp_instance.transport.connected: - logger.debug('Cleaning up VPP on exit') + logger.debug("Cleaning up VPP on exit") vpp_instance.disconnect() @@ -98,9 +106,9 @@ def add_convenience_methods(): def _vapi_af_name(self): if 6 == self._version: - return 'ip6' + return "ip6" if 4 == self._version: - return 'ip4' + return "ip4" raise ValueError("Invalid _version.") ipaddress._IPAddressBase.vapi_af = property(_vapi_af) @@ -121,7 +129,7 @@ class FuncWrapper: return self._func(**kwargs) def __repr__(self): - return '<FuncWrapper(func=<%s(%s)>)>' % (self.__name__, self.__doc__) + return "<FuncWrapper(func=<%s(%s)>)>" % (self.__name__, self.__doc__) class VPPApiError(Exception): @@ -161,7 +169,8 @@ class VPPApiJSONFiles: # perhaps we're in the 'src/scripts' or 'src/vpp-api/python' dir; # in which case, plot a course to likely places in the src tree import __main__ as main - if hasattr(main, '__file__'): + + if hasattr(main, "__file__"): # get the path of the calling script localdir = os.path.dirname(os.path.realpath(main.__file__)) else: @@ -171,7 +180,7 @@ class VPPApiJSONFiles: def dmatch(dir): """Match dir against right-hand components of the script dir""" - d = dir.split('/') # param 'dir' assumes a / separator + d = dir.split("/") # param 'dir' assumes a / separator length = len(d) return len(localdir_s) > length and localdir_s[-length:] == d @@ -180,43 +189,45 @@ class VPPApiJSONFiles: 'variant' (typically '' or '_debug')""" # Since 'core' and 'plugin' files are staged # in separate directories, we target the parent dir. - return os.path.sep.join(( - srcdir, - 'build-root', - 'install-vpp%s-native' % variant, - 'vpp', - 'share', - 'vpp', - 'api', - )) + return os.path.sep.join( + ( + srcdir, + "build-root", + "install-vpp%s-native" % variant, + "vpp", + "share", + "vpp", + "api", + ) + ) srcdir = None - if dmatch('src/scripts'): + if dmatch("src/scripts"): srcdir = os.path.sep.join(localdir_s[:-2]) - elif dmatch('src/vpp-api/python'): + elif dmatch("src/vpp-api/python"): srcdir = os.path.sep.join(localdir_s[:-3]) - elif dmatch('test'): + elif dmatch("test"): # we're apparently running tests srcdir = os.path.sep.join(localdir_s[:-1]) if srcdir: # we're in the source tree, try both the debug and release # variants. - dirs.append(sdir(srcdir, '_debug')) - dirs.append(sdir(srcdir, '')) + dirs.append(sdir(srcdir, "_debug")) + dirs.append(sdir(srcdir, "")) # Test for staged copies of the scripts # For these, since we explicitly know if we're running a debug versus # release variant, target only the relevant directory - if dmatch('build-root/install-vpp_debug-native/vpp/bin'): + if dmatch("build-root/install-vpp_debug-native/vpp/bin"): srcdir = os.path.sep.join(localdir_s[:-4]) - dirs.append(sdir(srcdir, '_debug')) - if dmatch('build-root/install-vpp-native/vpp/bin'): + dirs.append(sdir(srcdir, "_debug")) + if dmatch("build-root/install-vpp-native/vpp/bin"): srcdir = os.path.sep.join(localdir_s[:-4]) - dirs.append(sdir(srcdir, '')) + dirs.append(sdir(srcdir, "")) # finally, try the location system packages typically install into - dirs.append(os.path.sep.join(('', 'usr', 'share', 'vpp', 'api'))) + dirs.append(os.path.sep.join(("", "usr", "share", "vpp", "api"))) # check the directories for existence; first one wins for dir in dirs: @@ -226,7 +237,7 @@ class VPPApiJSONFiles: return None @classmethod - def find_api_files(cls, api_dir=None, patterns='*'): # -> list + def find_api_files(cls, api_dir=None, patterns="*"): # -> list """Find API definition files from the given directory tree with the given pattern. If no directory is given then find_api_dir() is used to locate one. If no pattern is given then all definition files found @@ -252,9 +263,9 @@ class VPPApiJSONFiles: raise VPPApiError("api_dir cannot be located") if isinstance(patterns, list) or isinstance(patterns, tuple): - patterns = [p.strip() + '.api.json' for p in patterns] + patterns = [p.strip() + ".api.json" for p in patterns] else: - patterns = [p.strip() + '.api.json' for p in patterns.split(",")] + patterns = [p.strip() + ".api.json" for p in patterns.split(",")] api_files = [] for root, dirnames, files in os.walk(api_dir): @@ -281,39 +292,39 @@ class VPPApiJSONFiles: services = {} messages = {} try: - for t in api['enums']: - t[0] = 'vl_api_' + t[0] + '_t' - types[t[0]] = {'type': 'enum', 'data': t} + for t in api["enums"]: + t[0] = "vl_api_" + t[0] + "_t" + types[t[0]] = {"type": "enum", "data": t} except KeyError: pass try: - for t in api['enumflags']: - t[0] = 'vl_api_' + t[0] + '_t' - types[t[0]] = {'type': 'enum', 'data': t} + for t in api["enumflags"]: + t[0] = "vl_api_" + t[0] + "_t" + types[t[0]] = {"type": "enum", "data": t} except KeyError: pass try: - for t in api['unions']: - t[0] = 'vl_api_' + t[0] + '_t' - types[t[0]] = {'type': 'union', 'data': t} + for t in api["unions"]: + t[0] = "vl_api_" + t[0] + "_t" + types[t[0]] = {"type": "union", "data": t} except KeyError: pass try: - for t in api['types']: - t[0] = 'vl_api_' + t[0] + '_t' - types[t[0]] = {'type': 'type', 'data': t} + for t in api["types"]: + t[0] = "vl_api_" + t[0] + "_t" + types[t[0]] = {"type": "type", "data": t} except KeyError: pass try: - for t, v in api['aliases'].items(): - types['vl_api_' + t + '_t'] = {'type': 'alias', 'data': v} + for t, v in api["aliases"].items(): + types["vl_api_" + t + "_t"] = {"type": "alias", "data": v} except KeyError: pass try: - services.update(api['services']) + services.update(api["services"]) except KeyError: pass @@ -321,30 +332,30 @@ class VPPApiJSONFiles: while True: unresolved = {} for k, v in types.items(): - t = v['data'] + t = v["data"] if not vpp_get_type(k): - if v['type'] == 'enum': + if v["type"] == "enum": try: VPPEnumType(t[0], t[1:]) except ValueError: unresolved[k] = v if not vpp_get_type(k): - if v['type'] == 'enumflag': + if v["type"] == "enumflag": try: VPPEnumFlagType(t[0], t[1:]) except ValueError: unresolved[k] = v - elif v['type'] == 'union': + elif v["type"] == "union": try: VPPUnionType(t[0], t[1:]) except ValueError: unresolved[k] = v - elif v['type'] == 'type': + elif v["type"] == "type": try: VPPType(t[0], t[1:]) except ValueError: unresolved[k] = v - elif v['type'] == 'alias': + elif v["type"] == "alias": try: VPPTypeAlias(k, t) except ValueError: @@ -352,17 +363,16 @@ class VPPApiJSONFiles: if len(unresolved) == 0: break if i > 3: - raise VPPValueError('Unresolved type definitions {}' - .format(unresolved)) + raise VPPValueError("Unresolved type definitions {}".format(unresolved)) types = unresolved i += 1 try: - for m in api['messages']: + for m in api["messages"]: try: messages[m[0]] = VPPMessage(m[0], m[1:]) except VPPNotImplementedError: ### OLE FIXME - logger.error('Not implemented error for {}'.format(m[0])) + logger.error("Not implemented error for {}".format(m[0])) except KeyError: pass return messages, services @@ -380,6 +390,7 @@ class VPPApiClient: provides a means to register a callback function to receive these messages in a background thread. """ + apidir = None VPPApiError = VPPApiError VPPRuntimeError = VPPRuntimeError @@ -387,11 +398,18 @@ class VPPApiClient: VPPNotImplementedError = VPPNotImplementedError VPPIOError = VPPIOError - - def __init__(self, *, apifiles=None, testmode=False, async_thread=True, - logger=None, loglevel=None, - read_timeout=5, use_socket=True, - server_address='/run/vpp/api.sock'): + def __init__( + self, + *, + apifiles=None, + testmode=False, + async_thread=True, + logger=None, + loglevel=None, + read_timeout=5, + use_socket=True, + server_address="/run/vpp/api.sock", + ): """Create a VPP API object. apifiles is a list of files containing API @@ -406,7 +424,8 @@ class VPPApiClient: """ if logger is None: logger = logging.getLogger( - "{}.{}".format(__name__, self.__class__.__name__)) + "{}.{}".format(__name__, self.__class__.__name__) + ) if loglevel is not None: logger.setLevel(loglevel) self.logger = logger @@ -415,8 +434,7 @@ class VPPApiClient: self.services = {} self.id_names = [] self.id_msgdef = [] - self.header = VPPType('header', [['u16', 'msgid'], - ['u32', 'client_index']]) + self.header = VPPType("header", [["u16", "msgid"], ["u32", "client_index"]]) self.apifiles = [] self.event_callback = None self.message_queue = queue.Queue() @@ -449,13 +467,13 @@ class VPPApiClient: # Basic sanity check if len(self.messages) == 0 and not testmode: - raise VPPValueError(1, 'Missing JSON message definitions') - if not(verify_enum_hint(VppEnum.vl_api_address_family_t)): - raise VPPRuntimeError("Invalid address family hints. " - "Cannot continue.") + raise VPPValueError(1, "Missing JSON message definitions") + if not (verify_enum_hint(VppEnum.vl_api_address_family_t)): + raise VPPRuntimeError("Invalid address family hints. " "Cannot continue.") - self.transport = VppTransport(self, read_timeout=read_timeout, - server_address=server_address) + self.transport = VppTransport( + self, read_timeout=read_timeout, server_address=server_address + ) # Make sure we allow VPP to clean up the message rings. atexit.register(vpp_atexit, weakref.ref(self)) @@ -466,6 +484,7 @@ class VPPApiClient: class ContextId: """Multiprocessing-safe provider of unique context IDs.""" + def __init__(self): self.context = mp.Value(ctypes.c_uint, 0) self.lock = mp.Lock() @@ -475,6 +494,7 @@ class VPPApiClient: with self.lock: self.context.value += 1 return self.context.value + get_context = ContextId() def get_type(self, name): @@ -487,17 +507,20 @@ class VPPApiClient: return self._api def make_function(self, msg, i, multipart, do_async): - if (do_async): + if do_async: + def f(**kwargs): return self._call_vpp_async(i, msg, **kwargs) + else: + def f(**kwargs): return self._call_vpp(i, msg, multipart, **kwargs) f.__name__ = str(msg.name) - f.__doc__ = ", ".join(["%s %s" % - (msg.fieldtypes[j], k) - for j, k in enumerate(msg.fields)]) + f.__doc__ = ", ".join( + ["%s %s" % (msg.fieldtypes[j], k) for j, k in enumerate(msg.fields)] + ) f.msg = msg return f @@ -507,7 +530,7 @@ class VPPApiClient: self.id_msgdef = [None] * (self.vpp_dictionary_maxid + 1) self._api = VppApiDynamicMethodHolder() for name, msg in self.messages.items(): - n = name + '_' + msg.crc[2:] + n = name + "_" + msg.crc[2:] i = self.transport.get_msg_index(n) if i > 0: self.id_msgdef[i] = msg @@ -518,28 +541,25 @@ class VPPApiClient: f = self.make_function(msg, i, self.services[name], do_async) setattr(self._api, name, FuncWrapper(f)) else: - self.logger.debug( - 'No such message type or failed CRC checksum: %s', n) + self.logger.debug("No such message type or failed CRC checksum: %s", n) - def connect_internal(self, name, msg_handler, chroot_prefix, rx_qlen, - do_async): - pfx = chroot_prefix.encode('utf-8') if chroot_prefix else None + def connect_internal(self, name, msg_handler, chroot_prefix, rx_qlen, do_async): + pfx = chroot_prefix.encode("utf-8") if chroot_prefix else None - rv = self.transport.connect(name, pfx, - msg_handler, rx_qlen) + rv = self.transport.connect(name, pfx, msg_handler, rx_qlen) if rv != 0: - raise VPPIOError(2, 'Connect failed') + raise VPPIOError(2, "Connect failed") self.vpp_dictionary_maxid = self.transport.msg_table_max_index() self._register_functions(do_async=do_async) # Initialise control ping - crc = self.messages['control_ping'].crc + crc = self.messages["control_ping"].crc self.control_ping_index = self.transport.get_msg_index( - ('control_ping' + '_' + crc[2:])) - self.control_ping_msgdef = self.messages['control_ping'] + ("control_ping" + "_" + crc[2:]) + ) + self.control_ping_msgdef = self.messages["control_ping"] if self.async_thread: - self.event_thread = threading.Thread( - target=self.thread_msg_handler) + self.event_thread = threading.Thread(target=self.thread_msg_handler) self.event_thread.daemon = True self.event_thread.start() else: @@ -556,8 +576,9 @@ class VPPApiClient: client and server. """ msg_handler = self.transport.get_callback(do_async) - return self.connect_internal(name, msg_handler, chroot_prefix, rx_qlen, - do_async) + return self.connect_internal( + name, msg_handler, chroot_prefix, rx_qlen, do_async + ) def connect_sync(self, name, chroot_prefix=None, rx_qlen=32): """Attach to VPP in synchronous mode. Application must poll for events. @@ -568,8 +589,7 @@ class VPPApiClient: client and server. """ - return self.connect_internal(name, None, chroot_prefix, rx_qlen, - do_async=False) + return self.connect_internal(name, None, chroot_prefix, rx_qlen, do_async=False) def disconnect(self): """Detach from VPP.""" @@ -590,42 +610,43 @@ class VPPApiClient: # If we have a context, then use the context to find any # request waiting for a reply context = 0 - if hasattr(r, 'context') and r.context > 0: + if hasattr(r, "context") and r.context > 0: context = r.context if context == 0: # No context -> async notification that we feed to the callback self.message_queue.put_nowait(r) else: - raise VPPIOError(2, 'RPC reply message received in event handler') + raise VPPIOError(2, "RPC reply message received in event handler") def has_context(self, msg): if len(msg) < 10: return False - header = VPPType('header_with_context', [['u16', 'msgid'], - ['u32', 'client_index'], - ['u32', 'context']]) + header = VPPType( + "header_with_context", + [["u16", "msgid"], ["u32", "client_index"], ["u32", "context"]], + ) (i, ci, context), size = header.unpack(msg, 0) - if self.id_names[i] == 'rx_thread_exit': + if self.id_names[i] == "rx_thread_exit": return # # Decode message and returns a tuple. # msgobj = self.id_msgdef[i] - if 'context' in msgobj.field_by_name and context >= 0: + if "context" in msgobj.field_by_name and context >= 0: return True return False def decode_incoming_msg(self, msg, no_type_conversion=False): if not msg: - logger.warning('vpp_api.read failed') + logger.warning("vpp_api.read failed") return (i, ci), size = self.header.unpack(msg, 0) - if self.id_names[i] == 'rx_thread_exit': + if self.id_names[i] == "rx_thread_exit": return # @@ -633,7 +654,7 @@ class VPPApiClient: # msgobj = self.id_msgdef[i] if not msgobj: - raise VPPIOError(2, 'Reply message undefined') + raise VPPIOError(2, "Reply message undefined") r, size = msgobj.unpack(msg, ntc=no_type_conversion) return r @@ -654,41 +675,39 @@ class VPPApiClient: def _control_ping(self, context): """Send a ping command.""" - self._call_vpp_async(self.control_ping_index, - self.control_ping_msgdef, - context=context) + self._call_vpp_async( + self.control_ping_index, self.control_ping_msgdef, context=context + ) def validate_args(self, msg, kwargs): d = set(kwargs.keys()) - set(msg.field_by_name.keys()) if d: - raise VPPValueError('Invalid argument {} to {}' - .format(list(d), msg.name)) + raise VPPValueError("Invalid argument {} to {}".format(list(d), msg.name)) def _add_stat(self, name, ms): if not name in self.stats: - self.stats[name] = {'max': ms, 'count': 1, 'avg': ms} + self.stats[name] = {"max": ms, "count": 1, "avg": ms} else: - if ms > self.stats[name]['max']: - self.stats[name]['max'] = ms - self.stats[name]['count'] += 1 - n = self.stats[name]['count'] - self.stats[name]['avg'] = self.stats[name]['avg'] * (n - 1) / n + ms / n + if ms > self.stats[name]["max"]: + self.stats[name]["max"] = ms + self.stats[name]["count"] += 1 + n = self.stats[name]["count"] + self.stats[name]["avg"] = self.stats[name]["avg"] * (n - 1) / n + ms / n def get_stats(self): - s = '\n=== API PAPI STATISTICS ===\n' - s += '{:<30} {:>4} {:>6} {:>6}\n'.format('message', 'cnt', 'avg', 'max') - for n in sorted(self.stats.items(), key=lambda v: v[1]['avg'], reverse=True): - s += '{:<30} {:>4} {:>6.2f} {:>6.2f}\n'.format(n[0], n[1]['count'], - n[1]['avg'], n[1]['max']) + s = "\n=== API PAPI STATISTICS ===\n" + s += "{:<30} {:>4} {:>6} {:>6}\n".format("message", "cnt", "avg", "max") + for n in sorted(self.stats.items(), key=lambda v: v[1]["avg"], reverse=True): + s += "{:<30} {:>4} {:>6.2f} {:>6.2f}\n".format( + n[0], n[1]["count"], n[1]["avg"], n[1]["max"] + ) return s def get_field_options(self, msg, fld_name): # when there is an option, the msgdef has 3 elements. # ['u32', 'ring_size', {'default': 1024}] for _def in self.messages[msg].msgdef: - if isinstance(_def, list) and \ - len(_def) == 3 and \ - _def[1] == fld_name: + if isinstance(_def, list) and len(_def) == 3 and _def[1] == fld_name: return _def[2] def _call_vpp(self, i, msgdef, service, **kwargs): @@ -707,25 +726,26 @@ class VPPApiClient: no response within the timeout window. """ ts = time.time() - if 'context' not in kwargs: + if "context" not in kwargs: context = self.get_context() - kwargs['context'] = context + kwargs["context"] = context else: - context = kwargs['context'] - kwargs['_vl_msg_id'] = i + context = kwargs["context"] + kwargs["_vl_msg_id"] = i - no_type_conversion = kwargs.pop('_no_type_conversion', False) - timeout = kwargs.pop('_timeout', None) + no_type_conversion = kwargs.pop("_no_type_conversion", False) + timeout = kwargs.pop("_timeout", None) try: if self.transport.socket_index: - kwargs['client_index'] = self.transport.socket_index + kwargs["client_index"] = self.transport.socket_index except AttributeError: pass self.validate_args(msgdef, kwargs) - s = 'Calling {}({})'.format(msgdef.name, - ','.join(['{!r}:{!r}'.format(k, v) for k, v in kwargs.items()])) + s = "Calling {}({})".format( + msgdef.name, ",".join(["{!r}:{!r}".format(k, v) for k, v in kwargs.items()]) + ) self.logger.debug(s) b = msgdef.pack(kwargs) @@ -733,17 +753,17 @@ class VPPApiClient: self.transport.write(b) - msgreply = service['reply'] - stream = True if 'stream' in service else False + msgreply = service["reply"] + stream = True if "stream" in service else False if stream: - if 'stream_msg' in service: + if "stream_msg" in service: # New service['reply'] = _reply and service['stream_message'] = _details - stream_message = service['stream_msg'] - modern =True + stream_message = service["stream_msg"] + modern = True else: # Old service['reply'] = _details stream_message = msgreply - msgreply = 'control_ping_reply' + msgreply = "control_ping_reply" modern = False # Send a ping after the request - we use its response # to detect that we have seen all results. @@ -751,22 +771,22 @@ class VPPApiClient: # Block until we get a reply. rl = [] - while (True): + while True: r = self.read_blocking(no_type_conversion, timeout) if r is None: - raise VPPIOError(2, 'VPP API client: read failed') + raise VPPIOError(2, "VPP API client: read failed") msgname = type(r).__name__ if context not in r or r.context == 0 or context != r.context: # Message being queued self.message_queue.put_nowait(r) continue if msgname != msgreply and (stream and (msgname != stream_message)): - print('REPLY MISMATCH', msgreply, msgname, stream_message, stream) + print("REPLY MISMATCH", msgreply, msgname, stream_message, stream) if not stream: rl = r break if msgname == msgreply: - if modern: # Return both reply and list + if modern: # Return both reply and list rl = r, rl break @@ -774,7 +794,7 @@ class VPPApiClient: self.transport.resume() - s = 'Return value: {!r}'.format(r) + s = "Return value: {!r}".format(r) if len(s) > 80: s = s[:80] + "..." self.logger.debug(s) @@ -795,17 +815,17 @@ class VPPApiClient: The returned context will help with assigning which call the reply belongs to. """ - if 'context' not in kwargs: + if "context" not in kwargs: context = self.get_context() - kwargs['context'] = context + kwargs["context"] = context else: - context = kwargs['context'] + context = kwargs["context"] try: if self.transport.socket_index: - kwargs['client_index'] = self.transport.socket_index + kwargs["client_index"] = self.transport.socket_index except AttributeError: - kwargs['client_index'] = 0 - kwargs['_vl_msg_id'] = i + kwargs["client_index"] = 0 + kwargs["_vl_msg_id"] = i b = msg.pack(kwargs) self.transport.write(b) @@ -891,26 +911,34 @@ class VPPApiClient: """Return VPPs API message table as name_crc dictionary, filtered by message name list.""" - replies = [self.services[n]['reply'] for n in msglist] + replies = [self.services[n]["reply"] for n in msglist] message_table_filtered = {} for name in msglist + replies: - for k,v in self.transport.message_table.items(): + for k, v in self.transport.message_table.items(): if k.startswith(name): message_table_filtered[k] = v break return message_table_filtered def __repr__(self): - return "<VPPApiClient apifiles=%s, testmode=%s, async_thread=%s, " \ - "logger=%s, read_timeout=%s, " \ - "server_address='%s'>" % ( - self._apifiles, self.testmode, self.async_thread, - self.logger, self.read_timeout, self.server_address) + return ( + "<VPPApiClient apifiles=%s, testmode=%s, async_thread=%s, " + "logger=%s, read_timeout=%s, " + "server_address='%s'>" + % ( + self._apifiles, + self.testmode, + self.async_thread, + self.logger, + self.read_timeout, + self.server_address, + ) + ) def details_iter(self, f, **kwargs): cursor = 0 while True: - kwargs['cursor'] = cursor + kwargs["cursor"] = cursor rv, details = f(**kwargs) for d in details: yield d diff --git a/src/vpp-api/python/vpp_papi/vpp_serializer.py b/src/vpp-api/python/vpp_papi/vpp_serializer.py index 644aeac65c6..a99e16aa8f9 100644 --- a/src/vpp-api/python/vpp_papi/vpp_serializer.py +++ b/src/vpp-api/python/vpp_papi/vpp_serializer.py @@ -27,7 +27,7 @@ from . import vpp_format # logger = logging.getLogger('vpp_serializer') # logger.setLevel(logging.DEBUG) # -logger = logging.getLogger('vpp_papi.serializer') +logger = logging.getLogger("vpp_papi.serializer") def check(d): @@ -46,8 +46,7 @@ def conversion_required(data, field_type): def conversion_packer(data, field_type): t = type(data).__name__ - return types[field_type].pack(vpp_format. - conversion_table[field_type][t](data)) + return types[field_type].pack(vpp_format.conversion_table[field_type][t](data)) def conversion_unpacker(data, field_type): @@ -77,30 +76,33 @@ class Packer: return c._get_packer_with_options(f_type, options) except IndexError: raise VPPSerializerValueError( - "Options not supported for {}{} ({})". - format(f_type, types[f_type].__class__, - options)) + "Options not supported for {}{} ({})".format( + f_type, types[f_type].__class__, options + ) + ) class BaseTypes(Packer): def __init__(self, type, elements=0, options=None): self._type = type self._elements = elements - base_types = {'u8': '>B', - 'i8': '>b', - 'string': '>s', - 'u16': '>H', - 'i16': '>h', - 'u32': '>I', - 'i32': '>i', - 'u64': '>Q', - 'i64': '>q', - 'f64': '=d', - 'bool': '>?', - 'header': '>HI'} - - if elements > 0 and (type == 'u8' or type == 'string'): - self.packer = struct.Struct('>%ss' % elements) + base_types = { + "u8": ">B", + "i8": ">b", + "string": ">s", + "u16": ">H", + "i16": ">h", + "u32": ">I", + "i32": ">i", + "u64": ">Q", + "i64": ">q", + "f64": "=d", + "bool": ">?", + "header": ">HI", + } + + if elements > 0 and (type == "u8" or type == "string"): + self.packer = struct.Struct(">%ss" % elements) else: self.packer = struct.Struct(base_types[type]) self.size = self.packer.size @@ -108,8 +110,8 @@ class BaseTypes(Packer): def pack(self, data, kwargs=None): if data is None: # Default to zero if not specified - if self.options and 'default' in self.options: - data = self.options['default'] + if self.options and "default" in self.options: + data = self.options["default"] else: data = 0 return self.packer.pack(data) @@ -122,9 +124,11 @@ class BaseTypes(Packer): return BaseTypes(f_type, options=options) def __repr__(self): - return "BaseTypes(type=%s, elements=%s, options=%s)" % (self._type, - self._elements, - self.options) + return "BaseTypes(type=%s, elements=%s, options=%s)" % ( + self._type, + self._elements, + self.options, + ) class String(Packer): @@ -132,13 +136,15 @@ class String(Packer): self.name = name self.num = num self.size = 1 - self.length_field_packer = BaseTypes('u32') - self.limit = options['limit'] if 'limit' in options else num + self.length_field_packer = BaseTypes("u32") + self.limit = options["limit"] if "limit" in options else num self.fixed = True if num else False if self.fixed and not self.limit: raise VPPSerializerValueError( - "Invalid combination for: {}, {} fixed:{} limit:{}". - format(name, options, self.fixed, self.limit)) + "Invalid combination for: {}, {} fixed:{} limit:{}".format( + name, options, self.fixed, self.limit + ) + ) def pack(self, list, kwargs=None): if not list: @@ -147,34 +153,42 @@ class String(Packer): return self.length_field_packer.pack(0) + b"" if self.limit and len(list) > self.limit - 1: raise VPPSerializerValueError( - "Invalid argument length for: {}, {} maximum {}". - format(list, len(list), self.limit - 1)) + "Invalid argument length for: {}, {} maximum {}".format( + list, len(list), self.limit - 1 + ) + ) if self.fixed: - return list.encode('ascii').ljust(self.limit, b'\x00') - return self.length_field_packer.pack(len(list)) + list.encode('ascii') + return list.encode("ascii").ljust(self.limit, b"\x00") + return self.length_field_packer.pack(len(list)) + list.encode("ascii") def unpack(self, data, offset=0, result=None, ntc=False): if self.fixed: - p = BaseTypes('u8', self.num) + p = BaseTypes("u8", self.num) s = p.unpack(data, offset) - s2 = s[0].split(b'\0', 1)[0] - return (s2.decode('ascii'), self.num) + s2 = s[0].split(b"\0", 1)[0] + return (s2.decode("ascii"), self.num) - length, length_field_size = self.length_field_packer.unpack(data, - offset) + length, length_field_size = self.length_field_packer.unpack(data, offset) if length == 0: - return '', 0 - p = BaseTypes('u8', length) + return "", 0 + p = BaseTypes("u8", length) x, size = p.unpack(data, offset + length_field_size) - return (x.decode('ascii', errors='replace'), size + length_field_size) - - -types = {'u8': BaseTypes('u8'), 'i8': BaseTypes('i8'), - 'u16': BaseTypes('u16'), 'i16': BaseTypes('i16'), - 'u32': BaseTypes('u32'), 'i32': BaseTypes('i32'), - 'u64': BaseTypes('u64'), 'i64': BaseTypes('i64'), - 'f64': BaseTypes('f64'), - 'bool': BaseTypes('bool'), 'string': String} + return (x.decode("ascii", errors="replace"), size + length_field_size) + + +types = { + "u8": BaseTypes("u8"), + "i8": BaseTypes("i8"), + "u16": BaseTypes("u16"), + "i16": BaseTypes("i16"), + "u32": BaseTypes("u32"), + "i32": BaseTypes("i32"), + "u64": BaseTypes("u64"), + "i64": BaseTypes("i64"), + "f64": BaseTypes("f64"), + "bool": BaseTypes("bool"), + "string": String, +} class_types = {} @@ -202,32 +216,34 @@ class FixedList_u8(Packer): """Packs a fixed length bytestring. Left-pads with zeros if input data is too short.""" if not data: - return b'\x00' * self.size + return b"\x00" * self.size if len(data) > self.num: raise VPPSerializerValueError( 'Fixed list length error for "{}", got: {}' - ' expected: {}' - .format(self.name, len(data), self.num)) + " expected: {}".format(self.name, len(data), self.num) + ) try: return self.packer.pack(data) except struct.error: raise VPPSerializerValueError( - 'Packing failed for "{}" {}' - .format(self.name, kwargs)) + 'Packing failed for "{}" {}'.format(self.name, kwargs) + ) def unpack(self, data, offset=0, result=None, ntc=False): if len(data[offset:]) < self.num: raise VPPSerializerValueError( 'Invalid array length for "{}" got {}' - ' expected {}' - .format(self.name, len(data[offset:]), self.num)) + " expected {}".format(self.name, len(data[offset:]), self.num) + ) return self.packer.unpack(data, offset) def __repr__(self): return "FixedList_u8(name=%s, field_type=%s, num=%s)" % ( - self.name, self.field_type, self.num + self.name, + self.field_type, + self.num, ) @@ -242,8 +258,10 @@ class FixedList(Packer): def pack(self, list, kwargs): if len(list) != self.num: raise VPPSerializerValueError( - 'Fixed list length error, got: {} expected: {}' - .format(len(list), self.num)) + "Fixed list length error, got: {} expected: {}".format( + len(list), self.num + ) + ) b = bytes() for e in list: b += self.packer.pack(e) @@ -262,7 +280,10 @@ class FixedList(Packer): def __repr__(self): return "FixedList(name=%s, field_type=%s, num=%s)" % ( - self.name, self.field_type, self.num) + self.name, + self.field_type, + self.num, + ) class VLAList(Packer): @@ -279,13 +300,15 @@ class VLAList(Packer): return b"" if len(lst) != kwargs[self.length_field]: raise VPPSerializerValueError( - 'Variable length error, got: {} expected: {}' - .format(len(lst), kwargs[self.length_field])) + "Variable length error, got: {} expected: {}".format( + len(lst), kwargs[self.length_field] + ) + ) # u8 array if self.packer.size == 1: if isinstance(lst, list): - return b''.join(lst) + return b"".join(lst) return bytes(lst) b = bytes() @@ -300,8 +323,8 @@ class VLAList(Packer): # u8 array if self.packer.size == 1: if result[self.index] == 0: - return b'', 0 - p = BaseTypes('u8', result[self.index]) + return b"", 0 + p = BaseTypes("u8", result[self.index]) return p.unpack(data, offset, ntc=ntc) r = [] @@ -313,10 +336,12 @@ class VLAList(Packer): return r, total def __repr__(self): - return "VLAList(name=%s, field_type=%s, " \ - "len_field_name=%s, index=%s)" % ( - self.name, self.field_type, self.length_field, self.index - ) + return "VLAList(name=%s, field_type=%s, " "len_field_name=%s, index=%s)" % ( + self.name, + self.field_type, + self.length_field, + self.index, + ) class VLAList_legacy(Packer): @@ -340,7 +365,8 @@ class VLAList_legacy(Packer): # Return a list of arguments if (len(data) - offset) % self.packer.size: raise VPPSerializerValueError( - 'Legacy Variable Length Array length mismatch.') + "Legacy Variable Length Array length mismatch." + ) elements = int((len(data) - offset) / self.packer.size) r = [] for e in range(elements): @@ -351,9 +377,7 @@ class VLAList_legacy(Packer): return r, total def __repr__(self): - return "VLAList_legacy(name=%s, field_type=%s)" % ( - self.name, self.field_type - ) + return "VLAList_legacy(name=%s, field_type=%s)" % (self.name, self.field_type) # Will change to IntEnum after 21.04 release @@ -361,16 +385,16 @@ class VPPEnumType(Packer): output_class = IntFlag def __init__(self, name, msgdef, options=None): - self.size = types['u32'].size + self.size = types["u32"].size self.name = name - self.enumtype = 'u32' + self.enumtype = "u32" self.msgdef = msgdef e_hash = {} for f in msgdef: - if type(f) is dict and 'enumtype' in f: - if f['enumtype'] != 'u32': - self.size = types[f['enumtype']].size - self.enumtype = f['enumtype'] + if type(f) is dict and "enumtype" in f: + if f["enumtype"] != "u32": + self.size = types[f["enumtype"]].size + self.enumtype = f["enumtype"] continue ename, evalue = f e_hash[ename] = evalue @@ -387,8 +411,8 @@ class VPPEnumType(Packer): def pack(self, data, kwargs=None): if data is None: # Default to zero if not specified - if self.options and 'default' in self.options: - data = self.options['default'] + if self.options and "default" in self.options: + data = self.options["default"] else: data = 0 @@ -404,7 +428,10 @@ class VPPEnumType(Packer): def __repr__(self): return "%s(name=%s, msgdef=%s, options=%s)" % ( - self.__class__.__name__, self.name, self.msgdef, self.options + self.__class__.__name__, + self.name, + self.msgdef, + self.options, ) @@ -424,14 +451,13 @@ class VPPUnionType(Packer): fields = [] self.packers = collections.OrderedDict() for i, f in enumerate(msgdef): - if type(f) is dict and 'crc' in f: - self.crc = f['crc'] + if type(f) is dict and "crc" in f: + self.crc = f["crc"] continue f_type, f_name = f if f_type not in types: - logger.debug('Unknown union type {}'.format(f_type)) - raise VPPSerializerValueError( - 'Unknown message type {}'.format(f_type)) + logger.debug("Unknown union type {}".format(f_type)) + raise VPPSerializerValueError("Unknown message type {}".format(f_type)) fields.append(f_name) size = types[f_type].size self.packers[f_name] = types[f_type] @@ -445,14 +471,14 @@ class VPPUnionType(Packer): # Union of variable length? def pack(self, data, kwargs=None): if not data: - return b'\x00' * self.size + return b"\x00" * self.size for k, v in data.items(): logger.debug("Key: {} Value: {}".format(k, v)) b = self.packers[k].pack(v, kwargs) break r = bytearray(self.size) - r[:len(b)] = b + r[: len(b)] = b return r def unpack(self, data, offset=0, result=None, ntc=False): @@ -466,25 +492,24 @@ class VPPUnionType(Packer): return self.tuple._make(r), maxsize def __repr__(self): - return"VPPUnionType(name=%s, msgdef=%r)" % (self.name, self.msgdef) + return "VPPUnionType(name=%s, msgdef=%r)" % (self.name, self.msgdef) class VPPTypeAlias(Packer): def __init__(self, name, msgdef, options=None): self.name = name self.msgdef = msgdef - t = vpp_get_type(msgdef['type']) + t = vpp_get_type(msgdef["type"]) if not t: - raise ValueError('No such type: {}'.format(msgdef['type'])) - if 'length' in msgdef: - if msgdef['length'] == 0: + raise ValueError("No such type: {}".format(msgdef["type"])) + if "length" in msgdef: + if msgdef["length"] == 0: raise ValueError() - if msgdef['type'] == 'u8': - self.packer = FixedList_u8(name, msgdef['type'], - msgdef['length']) + if msgdef["type"] == "u8": + self.packer = FixedList_u8(name, msgdef["type"], msgdef["length"]) self.size = self.packer.size else: - self.packer = FixedList(name, msgdef['type'], msgdef['length']) + self.packer = FixedList(name, msgdef["type"], msgdef["length"]) else: self.packer = t self.size = t.size @@ -498,11 +523,11 @@ class VPPTypeAlias(Packer): try: return conversion_packer(data, self.name) # Python 2 and 3 raises different exceptions from inet_pton - except(OSError, socket.error, TypeError): + except (OSError, socket.error, TypeError): pass if data is None: # Default to zero if not specified - if self.options and 'default' in self.options: - data = self.options['default'] + if self.options and "default" in self.options: + data = self.options["default"] else: data = 0 @@ -525,7 +550,10 @@ class VPPTypeAlias(Packer): def __repr__(self): return "VPPTypeAlias(name=%s, msgdef=%s, options=%s)" % ( - self.name, self.msgdef, self.options) + self.name, + self.msgdef, + self.options, + ) class VPPType(Packer): @@ -539,17 +567,16 @@ class VPPType(Packer): self.field_by_name = {} size = 0 for i, f in enumerate(msgdef): - if type(f) is dict and 'crc' in f: - self.crc = f['crc'] + if type(f) is dict and "crc" in f: + self.crc = f["crc"] continue f_type, f_name = f[:2] self.fields.append(f_name) self.field_by_name[f_name] = None self.fieldtypes.append(f_type) if f_type not in types: - logger.debug('Unknown type {}'.format(f_type)) - raise VPPSerializerValueError( - 'Unknown message type {}'.format(f_type)) + logger.debug("Unknown type {}".format(f_type)) + raise VPPSerializerValueError("Unknown message type {}".format(f_type)) fieldlen = len(f) options = [x for x in f if type(x) is dict] @@ -561,16 +588,16 @@ class VPPType(Packer): if fieldlen == 3: # list list_elements = f[2] if list_elements == 0: - if f_type == 'string': + if f_type == "string": p = String(f_name, 0, self.options) else: p = VLAList_legacy(f_name, f_type) self.packers.append(p) - elif f_type == 'u8': + elif f_type == "u8": p = FixedList_u8(f_name, f_type, list_elements) self.packers.append(p) size += p.size - elif f_type == 'string': + elif f_type == "string": p = String(f_name, list_elements, self.options) self.packers.append(p) size += p.size @@ -584,7 +611,7 @@ class VPPType(Packer): self.packers.append(p) else: # default support for types that decay to basetype - if 'default' in self.options: + if "default" in self.options: p = self.get_packer_with_options(f_type, self.options) else: p = types[f_type] @@ -609,8 +636,8 @@ class VPPType(Packer): for i, a in enumerate(self.fields): if data and type(data) is not dict and a not in data: raise VPPSerializerValueError( - "Invalid argument: {} expected {}.{}". - format(data, self.name, a)) + "Invalid argument: {} expected {}.{}".format(data, self.name, a) + ) # Defaulting to zero. if not data or a not in data: # Default to 0 @@ -651,7 +678,9 @@ class VPPType(Packer): def __repr__(self): return "%s(name=%s, msgdef=%s)" % ( - self.__class__.__name__, self.name, self.msgdef + self.__class__.__name__, + self.name, + self.msgdef, ) diff --git a/src/vpp-api/python/vpp_papi/vpp_stats.py b/src/vpp-api/python/vpp_papi/vpp_stats.py index 0b1c701a430..4a342b68a8f 100755 --- a/src/vpp-api/python/vpp_papi/vpp_stats.py +++ b/src/vpp-api/python/vpp_papi/vpp_stats.py @@ -14,7 +14,7 @@ # limitations under the License. # -''' +""" This module implement Python access to the VPP statistics segment. It accesses the data structures directly in shared memory. VPP uses optimistic locking, so data structures may change underneath @@ -39,7 +39,7 @@ stat['/if/rx'][:, 1].sum_packets() - returns the sum of packet counters for interface 1 on all threads stat['/if/rx-miss'][:, 1].sum() - returns the sum of packet counters for interface 1 on all threads for simple counters -''' +""" import os import socket @@ -50,31 +50,36 @@ import time import unittest import re + def recv_fd(sock): - '''Get file descriptor for memory map''' - fds = array.array("i") # Array of ints + """Get file descriptor for memory map""" + fds = array.array("i") # Array of ints _, ancdata, _, _ = sock.recvmsg(0, socket.CMSG_LEN(4)) for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS: - fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) + fds.frombytes(cmsg_data[: len(cmsg_data) - (len(cmsg_data) % fds.itemsize)]) return list(fds)[0] -VEC_LEN_FMT = Struct('I') + +VEC_LEN_FMT = Struct("I") + + def get_vec_len(stats, vector_offset): - '''Equivalent to VPP vec_len()''' + """Equivalent to VPP vec_len()""" return VEC_LEN_FMT.unpack_from(stats.statseg, vector_offset - 8)[0] + def get_string(stats, ptr): - '''Get a string from a VPP vector''' + """Get a string from a VPP vector""" namevector = ptr - stats.base namevectorlen = get_vec_len(stats, namevector) if namevector + namevectorlen >= stats.size: - raise IOError('String overruns stats segment') - return stats.statseg[namevector:namevector+namevectorlen-1].decode('ascii') + raise IOError("String overruns stats segment") + return stats.statseg[namevector : namevector + namevectorlen - 1].decode("ascii") class StatsVector: - '''A class representing a VPP vector''' + """A class representing a VPP vector""" def __init__(self, stats, ptr, fmt): self.vec_start = ptr - stats.base @@ -86,28 +91,35 @@ class StatsVector: self.stats = stats if self.vec_start + self.vec_len * self.elementsize >= stats.size: - raise IOError('Vector overruns stats segment') + raise IOError("Vector overruns stats segment") def __iter__(self): with self.stats.lock: - return self.struct.iter_unpack(self.statseg[self.vec_start:self.vec_start + - self.elementsize*self.vec_len]) + return self.struct.iter_unpack( + self.statseg[ + self.vec_start : self.vec_start + self.elementsize * self.vec_len + ] + ) def __getitem__(self, index): if index > self.vec_len: - raise IOError('Index beyond end of vector') + raise IOError("Index beyond end of vector") with self.stats.lock: if self.fmtlen == 1: - return self.struct.unpack_from(self.statseg, self.vec_start + - (index * self.elementsize))[0] - return self.struct.unpack_from(self.statseg, self.vec_start + - (index * self.elementsize)) + return self.struct.unpack_from( + self.statseg, self.vec_start + (index * self.elementsize) + )[0] + return self.struct.unpack_from( + self.statseg, self.vec_start + (index * self.elementsize) + ) + + +class VPPStats: + """Main class implementing Python access to the VPP statistics segment""" -class VPPStats(): - '''Main class implementing Python access to the VPP statistics segment''' # pylint: disable=too-many-instance-attributes - shared_headerfmt = Struct('QPQQPP') - default_socketname = '/run/vpp/stats.sock' + shared_headerfmt = Struct("QPQQPP") + default_socketname = "/run/vpp/stats.sock" def __init__(self, socketname=default_socketname, timeout=10): self.socketname = socketname @@ -120,7 +132,7 @@ class VPPStats(): self.statseg = 0 def connect(self): - '''Connect to stats segment''' + """Connect to stats segment""" if self.connected: return sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET) @@ -130,61 +142,64 @@ class VPPStats(): sock.close() stat_result = os.fstat(mfd) - self.statseg = mmap.mmap(mfd, stat_result.st_size, mmap.PROT_READ, mmap.MAP_SHARED) + self.statseg = mmap.mmap( + mfd, stat_result.st_size, mmap.PROT_READ, mmap.MAP_SHARED + ) os.close(mfd) self.size = stat_result.st_size if self.version != 2: - raise Exception('Incompatbile stat segment version {}' - .format(self.version)) + raise Exception("Incompatbile stat segment version {}".format(self.version)) self.refresh() self.connected = True def disconnect(self): - '''Disconnect from stats segment''' + """Disconnect from stats segment""" if self.connected: self.statseg.close() self.connected = False @property def version(self): - '''Get version of stats segment''' + """Get version of stats segment""" return self.shared_headerfmt.unpack_from(self.statseg)[0] @property def base(self): - '''Get base pointer of stats segment''' + """Get base pointer of stats segment""" return self.shared_headerfmt.unpack_from(self.statseg)[1] @property def epoch(self): - '''Get current epoch value from stats segment''' + """Get current epoch value from stats segment""" return self.shared_headerfmt.unpack_from(self.statseg)[2] @property def in_progress(self): - '''Get value of in_progress from stats segment''' + """Get value of in_progress from stats segment""" return self.shared_headerfmt.unpack_from(self.statseg)[3] @property def directory_vector(self): - '''Get pointer of directory vector''' + """Get pointer of directory vector""" return self.shared_headerfmt.unpack_from(self.statseg)[4] - elementfmt = 'IQ128s' + elementfmt = "IQ128s" def refresh(self, blocking=True): - '''Refresh directory vector cache (epoch changed)''' + """Refresh directory vector cache (epoch changed)""" directory = {} directory_by_idx = {} while True: try: with self.lock: self.last_epoch = self.epoch - for i, direntry in enumerate(StatsVector(self, self.directory_vector, self.elementfmt)): - path_raw = direntry[2].find(b'\x00') - path = direntry[2][:path_raw].decode('ascii') + for i, direntry in enumerate( + StatsVector(self, self.directory_vector, self.elementfmt) + ): + path_raw = direntry[2].find(b"\x00") + path = direntry[2][:path_raw].decode("ascii") directory[path] = StatsEntry(direntry[0], direntry[1]) directory_by_idx[i] = path self.directory = directory @@ -210,14 +225,12 @@ class VPPStats(): def __iter__(self): return iter(self.directory.items()) - def set_errors(self, blocking=True): - '''Return dictionary of error counters > 0''' + """Return dictionary of error counters > 0""" if not self.connected: self.connect() - errors = {k: v for k, v in self.directory.items() - if k.startswith("/err/")} + errors = {k: v for k, v in self.directory.items() if k.startswith("/err/")} result = {} for k in errors: try: @@ -229,23 +242,23 @@ class VPPStats(): return result def set_errors_str(self, blocking=True): - '''Return all errors counters > 0 pretty printed''' - error_string = ['ERRORS:'] + """Return all errors counters > 0 pretty printed""" + error_string = ["ERRORS:"] error_counters = self.set_errors(blocking) for k in sorted(error_counters): - error_string.append('{:<60}{:>10}'.format(k, error_counters[k])) - return '%s\n' % '\n'.join(error_string) + error_string.append("{:<60}{:>10}".format(k, error_counters[k])) + return "%s\n" % "\n".join(error_string) def get_counter(self, name, blocking=True): - '''Alternative call to __getitem__''' + """Alternative call to __getitem__""" return self.__getitem__(name, blocking) def get_err_counter(self, name, blocking=True): - '''Alternative call to __getitem__''' + """Alternative call to __getitem__""" return self.__getitem__(name, blocking).sum() def ls(self, patterns): - '''Returns list of counters matching pattern''' + """Returns list of counters matching pattern""" # pylint: disable=invalid-name if not self.connected: self.connect() @@ -255,20 +268,24 @@ class VPPStats(): if self.last_epoch != self.epoch: self.refresh() - return [k for k, v in self.directory.items() - if any(re.match(pattern, k) for pattern in regex)] + return [ + k + for k, v in self.directory.items() + if any(re.match(pattern, k) for pattern in regex) + ] def dump(self, counters, blocking=True): - '''Given a list of counters return a dictionary of results''' + """Given a list of counters return a dictionary of results""" if not self.connected: self.connect() result = {} for cnt in counters: - result[cnt] = self.__getitem__(cnt,blocking) + result[cnt] = self.__getitem__(cnt, blocking) return result -class StatsLock(): - '''Stat segment optimistic locking''' + +class StatsLock: + """Stat segment optimistic locking""" def __init__(self, stats): self.stats = stats @@ -283,7 +300,7 @@ class StatsLock(): self.release() def acquire(self, blocking=True, timeout=-1): - '''Acquire the lock. Await in progress to go false. Record epoch.''' + """Acquire the lock. Await in progress to go false. Record epoch.""" self.epoch = self.stats.epoch if timeout > 0: start = time.monotonic() @@ -296,46 +313,49 @@ class StatsLock(): return True def release(self): - '''Check if data read while locked is valid''' + """Check if data read while locked is valid""" if self.stats.in_progress or self.stats.epoch != self.epoch: - raise IOError('Optimistic lock failed, retry') + raise IOError("Optimistic lock failed, retry") def locked(self): - '''Not used''' + """Not used""" class StatsCombinedList(list): - '''Column slicing for Combined counters list''' + """Column slicing for Combined counters list""" def __getitem__(self, item): - '''Supports partial numpy style 2d support. Slice by column [:,1]''' + """Supports partial numpy style 2d support. Slice by column [:,1]""" if isinstance(item, int): return list.__getitem__(self, item) return CombinedList([row[item[1]] for row in self]) + class CombinedList(list): - '''Combined Counters 2-dimensional by thread by index of packets/octets''' + """Combined Counters 2-dimensional by thread by index of packets/octets""" def packets(self): - '''Return column (2nd dimension). Packets for all threads''' + """Return column (2nd dimension). Packets for all threads""" return [pair[0] for pair in self] def octets(self): - '''Return column (2nd dimension). Octets for all threads''' + """Return column (2nd dimension). Octets for all threads""" return [pair[1] for pair in self] def sum_packets(self): - '''Return column (2nd dimension). Sum of all packets for all threads''' + """Return column (2nd dimension). Sum of all packets for all threads""" return sum(self.packets()) def sum_octets(self): - '''Return column (2nd dimension). Sum of all octets for all threads''' + """Return column (2nd dimension). Sum of all octets for all threads""" return sum(self.octets()) + class StatsTuple(tuple): - '''A Combined vector tuple (packets, octets)''' + """A Combined vector tuple (packets, octets)""" + def __init__(self, data): - self.dictionary = {'packets': data[0], 'bytes': data[1]} + self.dictionary = {"packets": data[0], "bytes": data[1]} super().__init__() def __repr__(self): @@ -344,28 +364,32 @@ class StatsTuple(tuple): def __getitem__(self, item): if isinstance(item, int): return tuple.__getitem__(self, item) - if item == 'packets': + if item == "packets": return tuple.__getitem__(self, 0) return tuple.__getitem__(self, 1) + class StatsSimpleList(list): - '''Simple Counters 2-dimensional by thread by index of packets''' + """Simple Counters 2-dimensional by thread by index of packets""" def __getitem__(self, item): - '''Supports partial numpy style 2d support. Slice by column [:,1]''' + """Supports partial numpy style 2d support. Slice by column [:,1]""" if isinstance(item, int): return list.__getitem__(self, item) return SimpleList([row[item[1]] for row in self]) + class SimpleList(list): - '''Simple counter''' + """Simple counter""" def sum(self): - '''Sum the vector''' + """Sum the vector""" return sum(self) -class StatsEntry(): - '''An individual stats entry''' + +class StatsEntry: + """An individual stats entry""" + # pylint: disable=unused-argument,no-self-use def __init__(self, stattype, statvalue): @@ -386,115 +410,128 @@ class StatsEntry(): self.function = self.illegal def illegal(self, stats): - '''Invalid or unknown counter type''' + """Invalid or unknown counter type""" return None def scalar(self, stats): - '''Scalar counter''' + """Scalar counter""" return self.value def simple(self, stats): - '''Simple counter''' + """Simple counter""" counter = StatsSimpleList() - for threads in StatsVector(stats, self.value, 'P'): - clist = [v[0] for v in StatsVector(stats, threads[0], 'Q')] + for threads in StatsVector(stats, self.value, "P"): + clist = [v[0] for v in StatsVector(stats, threads[0], "Q")] counter.append(clist) return counter def combined(self, stats): - '''Combined counter''' + """Combined counter""" counter = StatsCombinedList() - for threads in StatsVector(stats, self.value, 'P'): - clist = [StatsTuple(cnt) for cnt in StatsVector(stats, threads[0], 'QQ')] + for threads in StatsVector(stats, self.value, "P"): + clist = [StatsTuple(cnt) for cnt in StatsVector(stats, threads[0], "QQ")] counter.append(clist) return counter def name(self, stats): - '''Name counter''' + """Name counter""" counter = [] - for name in StatsVector(stats, self.value, 'P'): + for name in StatsVector(stats, self.value, "P"): if name[0]: counter.append(get_string(stats, name[0])) return counter - SYMLINK_FMT1 = Struct('II') - SYMLINK_FMT2 = Struct('Q') + SYMLINK_FMT1 = Struct("II") + SYMLINK_FMT2 = Struct("Q") + def symlink(self, stats): - '''Symlink counter''' + """Symlink counter""" b = self.SYMLINK_FMT2.pack(self.value) index1, index2 = self.SYMLINK_FMT1.unpack(b) name = stats.directory_by_idx[index1] - return stats[name][:,index2] + return stats[name][:, index2] def get_counter(self, stats): - '''Return a list of counters''' + """Return a list of counters""" if stats: return self.function(stats) + class TestStats(unittest.TestCase): - '''Basic statseg tests''' + """Basic statseg tests""" def setUp(self): - '''Connect to statseg''' + """Connect to statseg""" self.stat = VPPStats() self.stat.connect() self.profile = cProfile.Profile() self.profile.enable() def tearDown(self): - '''Disconnect from statseg''' + """Disconnect from statseg""" self.stat.disconnect() profile = Stats(self.profile) profile.strip_dirs() - profile.sort_stats('cumtime') + profile.sort_stats("cumtime") profile.print_stats() print("\n--->>>") def test_counters(self): - '''Test access to statseg''' - - print('/err/abf-input-ip4/missed', self.stat['/err/abf-input-ip4/missed']) - print('/sys/heartbeat', self.stat['/sys/heartbeat']) - print('/if/names', self.stat['/if/names']) - print('/if/rx-miss', self.stat['/if/rx-miss']) - print('/if/rx-miss', self.stat['/if/rx-miss'][1]) - print('/nat44-ed/out2in/slowpath/drops', self.stat['/nat44-ed/out2in/slowpath/drops']) + """Test access to statseg""" + + print("/err/abf-input-ip4/missed", self.stat["/err/abf-input-ip4/missed"]) + print("/sys/heartbeat", self.stat["/sys/heartbeat"]) + print("/if/names", self.stat["/if/names"]) + print("/if/rx-miss", self.stat["/if/rx-miss"]) + print("/if/rx-miss", self.stat["/if/rx-miss"][1]) + print( + "/nat44-ed/out2in/slowpath/drops", + self.stat["/nat44-ed/out2in/slowpath/drops"], + ) with self.assertRaises(KeyError): - print('NO SUCH COUNTER', self.stat['foobar']) - print('/if/rx', self.stat.get_counter('/if/rx')) - print('/err/ethernet-input/no_error', - self.stat.get_counter('/err/ethernet-input/no_error')) + print("NO SUCH COUNTER", self.stat["foobar"]) + print("/if/rx", self.stat.get_counter("/if/rx")) + print( + "/err/ethernet-input/no_error", + self.stat.get_counter("/err/ethernet-input/no_error"), + ) def test_column(self): - '''Test column slicing''' - - print('/if/rx-miss', self.stat['/if/rx-miss']) - print('/if/rx', self.stat['/if/rx']) # All interfaces for thread #1 - print('/if/rx thread #1', self.stat['/if/rx'][0]) # All interfaces for thread #1 - print('/if/rx thread #1, interface #1', - self.stat['/if/rx'][0][1]) # All interfaces for thread #1 - print('/if/rx if_index #1', self.stat['/if/rx'][:, 1]) - print('/if/rx if_index #1 packets', self.stat['/if/rx'][:, 1].packets()) - print('/if/rx if_index #1 packets', self.stat['/if/rx'][:, 1].sum_packets()) - print('/if/rx if_index #1 packets', self.stat['/if/rx'][:, 1].octets()) - print('/if/rx-miss', self.stat['/if/rx-miss']) - print('/if/rx-miss if_index #1 packets', self.stat['/if/rx-miss'][:, 1].sum()) - print('/if/rx if_index #1 packets', self.stat['/if/rx'][0][1]['packets']) + """Test column slicing""" + + print("/if/rx-miss", self.stat["/if/rx-miss"]) + print("/if/rx", self.stat["/if/rx"]) # All interfaces for thread #1 + print( + "/if/rx thread #1", self.stat["/if/rx"][0] + ) # All interfaces for thread #1 + print( + "/if/rx thread #1, interface #1", self.stat["/if/rx"][0][1] + ) # All interfaces for thread #1 + print("/if/rx if_index #1", self.stat["/if/rx"][:, 1]) + print("/if/rx if_index #1 packets", self.stat["/if/rx"][:, 1].packets()) + print("/if/rx if_index #1 packets", self.stat["/if/rx"][:, 1].sum_packets()) + print("/if/rx if_index #1 packets", self.stat["/if/rx"][:, 1].octets()) + print("/if/rx-miss", self.stat["/if/rx-miss"]) + print("/if/rx-miss if_index #1 packets", self.stat["/if/rx-miss"][:, 1].sum()) + print("/if/rx if_index #1 packets", self.stat["/if/rx"][0][1]["packets"]) def test_nat44(self): - '''Test the nat counters''' + """Test the nat counters""" - print('/nat44-ei/ha/del-event-recv', self.stat['/nat44-ei/ha/del-event-recv']) - print('/err/nat44-ei-ha/pkts-processed', self.stat['/err/nat44-ei-ha/pkts-processed'].sum()) + print("/nat44-ei/ha/del-event-recv", self.stat["/nat44-ei/ha/del-event-recv"]) + print( + "/err/nat44-ei-ha/pkts-processed", + self.stat["/err/nat44-ei-ha/pkts-processed"].sum(), + ) def test_legacy(self): - '''Legacy interface''' + """Legacy interface""" directory = self.stat.ls(["^/if", "/err/ip4-input", "/sys/node/ip4-input"]) data = self.stat.dump(directory) print(data) - print('Looking up sys node') + print("Looking up sys node") directory = self.stat.ls(["^/sys/node"]) - print('Dumping sys node') + print("Dumping sys node") data = self.stat.dump(directory) print(data) directory = self.stat.ls(["^/foobar"]) @@ -502,18 +539,19 @@ class TestStats(unittest.TestCase): print(data) def test_sys_nodes(self): - '''Test /sys/nodes''' - counters = self.stat.ls('^/sys/node') - print('COUNTERS:', counters) - print('/sys/node', self.stat.dump(counters)) - print('/net/route/to', self.stat['/net/route/to']) + """Test /sys/nodes""" + counters = self.stat.ls("^/sys/node") + print("COUNTERS:", counters) + print("/sys/node", self.stat.dump(counters)) + print("/net/route/to", self.stat["/net/route/to"]) def test_symlink(self): - '''Symbolic links''' - print('/interface/local0/rx', self.stat['/interfaces/local0/rx']) - print('/sys/nodes/unix-epoll-input', self.stat['/nodes/unix-epoll-input/calls']) + """Symbolic links""" + print("/interface/local0/rx", self.stat["/interfaces/local0/rx"]) + print("/sys/nodes/unix-epoll-input", self.stat["/nodes/unix-epoll-input/calls"]) + -if __name__ == '__main__': +if __name__ == "__main__": import cProfile from pstats import Stats diff --git a/src/vpp-api/python/vpp_papi/vpp_transport_socket.py b/src/vpp-api/python/vpp_papi/vpp_transport_socket.py index c82b8c365a1..3a8c332a00a 100644 --- a/src/vpp-api/python/vpp_papi/vpp_transport_socket.py +++ b/src/vpp-api/python/vpp_papi/vpp_transport_socket.py @@ -9,7 +9,7 @@ import multiprocessing import queue import logging -logger = logging.getLogger('vpp_papi.transport') +logger = logging.getLogger("vpp_papi.transport") logger.addHandler(logging.NullHandler()) @@ -26,7 +26,7 @@ class VppTransport: self.read_timeout = read_timeout if read_timeout > 0 else None self.parent = parent self.server_address = server_address - self.header = struct.Struct('>QII') + self.header = struct.Struct(">QII") self.message_table = {} # These queues can be accessed async. # They are always up, but replaced on connect. @@ -41,11 +41,10 @@ class VppTransport: def msg_thread_func(self): while True: try: - rlist, _, _ = select.select([self.socket, - self.sque._reader], [], []) + rlist, _, _ = select.select([self.socket, self.sque._reader], [], []) except socket.error: # Terminate thread - logging.error('select failed') + logging.error("select failed") self.q.put(None) return @@ -71,8 +70,7 @@ class VppTransport: else: self.parent.msg_handler_async(msg) else: - raise VppTransportSocketIOError( - 2, 'Unknown response from select') + raise VppTransportSocketIOError(2, "Unknown response from select") def connect(self, name, pfx, msg_handler, rx_qlen): # TODO: Reorder the actions and add "roll-backs", @@ -80,7 +78,8 @@ class VppTransport: if self.message_thread is not None: raise VppTransportSocketIOError( - 1, "PAPI socket transport connect: Need to disconnect first.") + 1, "PAPI socket transport connect: Need to disconnect first." + ) # Create a UDS socket self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) @@ -107,19 +106,17 @@ class VppTransport: self.message_thread = threading.Thread(target=self.msg_thread_func) # Initialise sockclnt_create - sockclnt_create = self.parent.messages['sockclnt_create'] - sockclnt_create_reply = self.parent.messages['sockclnt_create_reply'] + sockclnt_create = self.parent.messages["sockclnt_create"] + sockclnt_create_reply = self.parent.messages["sockclnt_create_reply"] - args = {'_vl_msg_id': 15, - 'name': name, - 'context': 124} + args = {"_vl_msg_id": 15, "name": name, "context": 124} b = sockclnt_create.pack(args) self.write(b) msg = self._read() hdr, length = self.parent.header.unpack(msg, 0) if hdr.msgid != 16: # TODO: Add first numeric argument. - raise VppTransportSocketIOError('Invalid reply message') + raise VppTransportSocketIOError("Invalid reply message") r, length = sockclnt_create_reply.unpack(msg) self.socket_index = r.index @@ -184,7 +181,7 @@ class VppTransport: def write(self, buf): """Send a binary-packed message to VPP.""" if not self.connected: - raise VppTransportSocketIOError(1, 'Not connected') + raise VppTransportSocketIOError(1, "Not connected") # Send header header = self.header.pack(0, len(buf), 0) @@ -192,8 +189,7 @@ class VppTransport: self.socket.sendall(header) self.socket.sendall(buf) except socket.error as err: - raise VppTransportSocketIOError(1, 'Sendall error: {err!r}'.format( - err=err)) + raise VppTransportSocketIOError(1, "Sendall error: {err!r}".format(err=err)) def _read_fixed(self, size): """Repeat receive until fixed size is read. Return empty on error.""" @@ -223,11 +219,11 @@ class VppTransport: msg = self._read_fixed(hdrlen) if hdrlen == len(msg): return msg - raise VppTransportSocketIOError(1, 'Unknown socket read error') + raise VppTransportSocketIOError(1, "Unknown socket read error") def read(self, timeout=None): if not self.connected: - raise VppTransportSocketIOError(1, 'Not connected') + raise VppTransportSocketIOError(1, "Not connected") if timeout is None: timeout = self.read_timeout try: diff --git a/src/vpp-api/vapi/vapi_c_gen.py b/src/vpp-api/vapi/vapi_c_gen.py index debd7345f60..2978ebd2077 100755 --- a/src/vpp-api/vapi/vapi_c_gen.py +++ b/src/vpp-api/vapi/vapi_c_gen.py @@ -5,8 +5,17 @@ import inspect import os import sys import logging -from vapi_json_parser import Field, Struct, Enum, Union, Message, JsonParser,\ - SimpleType, StructType, Alias +from vapi_json_parser import ( + Field, + Struct, + Enum, + Union, + Message, + JsonParser, + SimpleType, + StructType, + Alias, +) class CField(Field): @@ -14,54 +23,63 @@ class CField(Field): return "vapi_type_%s" % self.name def get_c_def(self): - if self.type.get_c_name() == 'vl_api_string_t': + if self.type.get_c_name() == "vl_api_string_t": if self.len: return "u8 %s[%d];" % (self.name, self.len) else: return "vl_api_string_t %s;" % (self.name) else: if self.len is not None and type(self.len) != dict: - return "%s %s[%d];" % (self.type.get_c_name(), self.name, - self.len) + return "%s %s[%d];" % (self.type.get_c_name(), self.name, self.len) else: return "%s %s;" % (self.type.get_c_name(), self.name) def get_swap_to_be_code(self, struct, var): if self.len is not None and type(self.len) != dict: if self.len > 0: - return "do { unsigned i; for (i = 0; i < %d; ++i) { %s } }"\ - " while(0);" % ( - self.len, - self.type.get_swap_to_be_code(struct, "%s[i]" % var)) + return ( + "do { unsigned i; for (i = 0; i < %d; ++i) { %s } }" + " while(0);" + % (self.len, self.type.get_swap_to_be_code(struct, "%s[i]" % var)) + ) else: if self.nelem_field.needs_byte_swap(): nelem_field = "%s(%s%s)" % ( self.nelem_field.type.get_swap_to_host_func_name(), - struct, self.nelem_field.name) + struct, + self.nelem_field.name, + ) else: nelem_field = "%s%s" % (struct, self.nelem_field.name) return ( "do { unsigned i; for (i = 0; i < %s; ++i) { %s } }" - " while(0);" % - (nelem_field, self.type.get_swap_to_be_code( - struct, "%s[i]" % var))) + " while(0);" + % ( + nelem_field, + self.type.get_swap_to_be_code(struct, "%s[i]" % var), + ) + ) return self.type.get_swap_to_be_code(struct, "%s" % var) def get_swap_to_host_code(self, struct, var): if self.len is not None and type(self.len) != dict: if self.len > 0: - return "do { unsigned i; for (i = 0; i < %d; ++i) { %s } }"\ - " while(0);" % ( - self.len, - self.type.get_swap_to_host_code(struct, "%s[i]" % var)) + return ( + "do { unsigned i; for (i = 0; i < %d; ++i) { %s } }" + " while(0);" + % (self.len, self.type.get_swap_to_host_code(struct, "%s[i]" % var)) + ) else: # nelem_field already swapped to host here... return ( "do { unsigned i; for (i = 0; i < %s%s; ++i) { %s } }" - " while(0);" % - (struct, self.nelem_field.name, - self.type.get_swap_to_host_code( - struct, "%s[i]" % var))) + " while(0);" + % ( + struct, + self.nelem_field.name, + self.type.get_swap_to_host_code(struct, "%s[i]" % var), + ) + ) return self.type.get_swap_to_host_code(struct, "%s" % var) def needs_byte_swap(self): @@ -82,10 +100,14 @@ class CField(Field): def get_vla_calc_size_code(self, prefix, path): if self.is_vla(): - result = ["sizeof(%s.%s[0]) * %s" % ( - ".".join([prefix] + path), - self.name, - self.get_vla_field_length_name(path))] + result = [ + "sizeof(%s.%s[0]) * %s" + % ( + ".".join([prefix] + path), + self.name, + self.get_vla_field_length_name(path), + ) + ] else: result = [] if self.type.has_vla(): @@ -96,10 +118,14 @@ class CField(Field): def get_vla_assign_code(self, prefix, path): result = [] if self.is_vla(): - result.append("%s.%s = %s" % ( - ".".join([prefix] + path), - self.nelem_field.name, - self.get_vla_field_length_name(path))) + result.append( + "%s.%s = %s" + % ( + ".".join([prefix] + path), + self.nelem_field.name, + self.get_vla_field_length_name(path), + ) + ) if self.type.has_vla(): t = self.type.get_vla_assign_code(prefix, path + [self.name]) result.extend(t) @@ -113,52 +139,75 @@ class CAlias(CField): def get_c_def(self): if self.len is not None: return "typedef %s vapi_type_%s[%d];" % ( - self.type.get_c_name(), self.name, self.len) + 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 vapi_type_%s;" % (self.type.get_c_name(), self.name) class CStruct(Struct): def get_c_def(self): - return "\n".join([ - "typedef struct __attribute__((__packed__)) {\n%s" % ( - "\n".join([" %s" % x.get_c_def() - for x in self.fields])), - "} %s;" % self.get_c_name()]) + return "\n".join( + [ + "typedef struct __attribute__((__packed__)) {\n%s" + % ("\n".join([" %s" % x.get_c_def() for x in self.fields])), + "} %s;" % self.get_c_name(), + ] + ) def get_vla_assign_code(self, prefix, path): - return [x for f in self.fields if f.has_vla() - for x in f.get_vla_assign_code(prefix, path)] + return [ + x + for f in self.fields + if f.has_vla() + for x in f.get_vla_assign_code(prefix, path) + ] def get_alloc_vla_param_names(self, path): - return [x for f in self.fields - if f.has_vla() - for x in f.get_alloc_vla_param_names(path)] + return [ + x + for f in self.fields + if f.has_vla() + for x in f.get_alloc_vla_param_names(path) + ] def get_vla_calc_size_code(self, prefix, path): - return [x for f in self.fields if f.has_vla() - for x in f.get_vla_calc_size_code(prefix, path)] + return [ + x + for f in self.fields + if f.has_vla() + for x in f.get_vla_calc_size_code(prefix, path) + ] -class CSimpleType (SimpleType): +class CSimpleType(SimpleType): swap_to_be_dict = { - 'i16': 'htobe16', 'u16': 'htobe16', - 'i32': 'htobe32', 'u32': 'htobe32', - 'i64': 'htobe64', 'u64': 'htobe64', + "i16": "htobe16", + "u16": "htobe16", + "i32": "htobe32", + "u32": "htobe32", + "i64": "htobe64", + "u64": "htobe64", } swap_to_host_dict = { - 'i16': 'be16toh', 'u16': 'be16toh', - 'i32': 'be32toh', 'u32': 'be32toh', - 'i64': 'be64toh', 'u64': 'be64toh', + "i16": "be16toh", + "u16": "be16toh", + "i32": "be32toh", + "u32": "be32toh", + "i64": "be64toh", + "u64": "be64toh", } __packed = "__attribute__((packed))" pack_dict = { - 'i8': __packed, 'u8': __packed, - 'i16': __packed, 'u16': __packed, + "i8": __packed, + "u8": __packed, + "i16": __packed, + "u16": __packed, } def get_c_name(self): @@ -175,15 +224,21 @@ class CSimpleType (SimpleType): def get_swap_to_be_code(self, struct, var, cast=None): x = "%s%s" % (struct, var) - return "%s = %s%s(%s);" % (x, - "(%s)" % cast if cast else "", - self.get_swap_to_be_func_name(), x) + return "%s = %s%s(%s);" % ( + x, + "(%s)" % cast if cast else "", + self.get_swap_to_be_func_name(), + x, + ) def get_swap_to_host_code(self, struct, var, cast=None): x = "%s%s" % (struct, var) - return "%s = %s%s(%s);" % (x, - "(%s)" % cast if cast else "", - self.get_swap_to_host_func_name(), x) + return "%s = %s%s(%s);" % ( + x, + "(%s)" % cast if cast else "", + self.get_swap_to_host_func_name(), + x, + ) def needs_byte_swap(self): try: @@ -205,7 +260,7 @@ class CEnum(Enum): return "typedef enum {\n%s\n} %s %s;" % ( "\n".join([" %s = %s," % (i, j) for i, j in self.value_pairs]), self.type.get_packed(), - self.get_c_name() + self.get_c_name(), ) def needs_byte_swap(self): @@ -224,16 +279,15 @@ class CUnion(Union): def get_c_def(self): return "typedef union {\n%s\n} %s;" % ( - "\n".join([" %s %s;" % (i.get_c_name(), j) - for i, j in self.type_pairs]), - self.get_c_name() + "\n".join([" %s %s;" % (i.get_c_name(), j) for i, j in self.type_pairs]), + self.get_c_name(), ) def needs_byte_swap(self): return False -class CStructType (StructType, CStruct): +class CStructType(StructType, CStruct): def get_c_name(self): return "vapi_type_%s" % self.name @@ -244,27 +298,36 @@ class CStructType (StructType, CStruct): return "%s_ntoh" % self.get_c_name() def get_swap_to_be_func_decl(self): - return "void %s(%s *msg)" % ( - self.get_swap_to_be_func_name(), self.get_c_name()) + return "void %s(%s *msg)" % (self.get_swap_to_be_func_name(), self.get_c_name()) def get_swap_to_be_func_def(self): return "%s\n{\n%s\n}" % ( self.get_swap_to_be_func_decl(), - "\n".join([ - " %s" % p.get_swap_to_be_code("msg->", "%s" % p.name) - for p in self.fields if p.needs_byte_swap()]), + "\n".join( + [ + " %s" % p.get_swap_to_be_code("msg->", "%s" % p.name) + for p in self.fields + if p.needs_byte_swap() + ] + ), ) def get_swap_to_host_func_decl(self): return "void %s(%s *msg)" % ( - self.get_swap_to_host_func_name(), self.get_c_name()) + self.get_swap_to_host_func_name(), + self.get_c_name(), + ) def get_swap_to_host_func_def(self): return "%s\n{\n%s\n}" % ( self.get_swap_to_host_func_decl(), - "\n".join([ - " %s" % p.get_swap_to_host_code("msg->", "%s" % p.name) - for p in self.fields if p.needs_byte_swap()]), + "\n".join( + [ + " %s" % p.get_swap_to_host_code("msg->", "%s" % p.name) + for p in self.fields + if p.needs_byte_swap() + ] + ), ) def get_swap_to_be_code(self, struct, var): @@ -280,13 +343,11 @@ class CStructType (StructType, CStruct): return False -class CMessage (Message): +class CMessage(Message): def __init__(self, logger, definition, json_parser): super(CMessage, self).__init__(logger, definition, json_parser) self.payload_members = [ - " %s" % p.get_c_def() - for p in self.fields - if p.type != self.header + " %s" % p.get_c_def() for p in self.fields if p.type != self.header ] def has_payload(self): @@ -305,46 +366,65 @@ class CMessage (Message): return "vapi_alloc_%s" % self.name def get_alloc_vla_param_names(self): - return [x for f in self.fields - if f.has_vla() - for x in f.get_alloc_vla_param_names([])] + return [ + x + for f in self.fields + if f.has_vla() + for x in f.get_alloc_vla_param_names([]) + ] def get_alloc_func_decl(self): return "%s* %s(struct vapi_ctx_s *ctx%s)" % ( self.get_c_name(), self.get_alloc_func_name(), - "".join([", size_t %s" % n for n in - self.get_alloc_vla_param_names()])) + "".join([", size_t %s" % n for n in self.get_alloc_vla_param_names()]), + ) def get_alloc_func_def(self): extra = [] - if self.header.has_field('client_index'): - extra.append( - " msg->header.client_index = vapi_get_client_index(ctx);") - if self.header.has_field('context'): + if self.header.has_field("client_index"): + extra.append(" msg->header.client_index = vapi_get_client_index(ctx);") + if self.header.has_field("context"): extra.append(" msg->header.context = 0;") - return "\n".join([ - "%s" % self.get_alloc_func_decl(), - "{", - " %s *msg = NULL;" % self.get_c_name(), - " const size_t size = sizeof(%s)%s;" % ( - self.get_c_name(), - "".join([" + %s" % x for f in self.fields if f.has_vla() - for x in f.get_vla_calc_size_code("msg->payload", - [])])), - " /* cast here required to play nicely with C++ world ... */", - " msg = (%s*)vapi_msg_alloc(ctx, size);" % self.get_c_name(), - " if (!msg) {", - " return NULL;", - " }", - ] + extra + [ - " msg->header._vl_msg_id = vapi_lookup_vl_msg_id(ctx, %s);" % - self.get_msg_id_name(), - "".join([" %s;\n" % line - for f in self.fields if f.has_vla() - for line in f.get_vla_assign_code("msg->payload", [])]), - " return msg;", - "}"]) + return "\n".join( + [ + "%s" % self.get_alloc_func_decl(), + "{", + " %s *msg = NULL;" % self.get_c_name(), + " const size_t size = sizeof(%s)%s;" + % ( + self.get_c_name(), + "".join( + [ + " + %s" % x + for f in self.fields + if f.has_vla() + for x in f.get_vla_calc_size_code("msg->payload", []) + ] + ), + ), + " /* cast here required to play nicely with C++ world ... */", + " msg = (%s*)vapi_msg_alloc(ctx, size);" % self.get_c_name(), + " if (!msg) {", + " return NULL;", + " }", + ] + + extra + + [ + " msg->header._vl_msg_id = vapi_lookup_vl_msg_id(ctx, %s);" + % self.get_msg_id_name(), + "".join( + [ + " %s;\n" % line + for f in self.fields + if f.has_vla() + for line in f.get_vla_assign_code("msg->payload", []) + ] + ), + " return msg;", + "}", + ] + ) def get_calc_msg_size_func_name(self): return "vapi_calc_%s_msg_size" % self.name @@ -352,21 +432,26 @@ class CMessage (Message): def get_calc_msg_size_func_decl(self): return "uword %s(%s *msg)" % ( self.get_calc_msg_size_func_name(), - self.get_c_name()) + self.get_c_name(), + ) def get_calc_msg_size_func_def(self): - return "\n".join([ - "%s" % self.get_calc_msg_size_func_decl(), - "{", - " return sizeof(*msg)%s;" % - "".join(["+ msg->payload.%s * sizeof(msg->payload.%s[0])" % ( - f.nelem_field.name, - f.name) - for f in self.fields - if f.nelem_field is not None - ]), - "}", - ]) + return "\n".join( + [ + "%s" % self.get_calc_msg_size_func_decl(), + "{", + " return sizeof(*msg)%s;" + % "".join( + [ + "+ msg->payload.%s * sizeof(msg->payload.%s[0])" + % (f.nelem_field.name, f.name) + for f in self.fields + if f.nelem_field is not None + ] + ), + "}", + ] + ) def get_verify_msg_size_func_name(self): return f"vapi_verify_{self.name}_msg_size" @@ -374,7 +459,8 @@ class CMessage (Message): def get_verify_msg_size_func_decl(self): return "int %s(%s *msg, uword buf_size)" % ( self.get_verify_msg_size_func_name(), - self.get_c_name()) + self.get_c_name(), + ) def get_verify_msg_size_func_def(self): return inspect.cleandoc( @@ -397,29 +483,39 @@ class CMessage (Message): }} return 0; }} - """) + """ + ) def get_c_def(self): if self.has_payload(): - return "\n".join([ - "typedef struct __attribute__ ((__packed__)) {", - "%s " % - "\n".join(self.payload_members), - "} %s;" % self.get_payload_struct_name(), - "", - "typedef struct __attribute__ ((__packed__)) {", - (" %s %s;" % (self.header.get_c_name(), - self.fields[0].name) - if self.header is not None else ""), - " %s payload;" % self.get_payload_struct_name(), - "} %s;" % self.get_c_name(), ]) + return "\n".join( + [ + "typedef struct __attribute__ ((__packed__)) {", + "%s " % "\n".join(self.payload_members), + "} %s;" % self.get_payload_struct_name(), + "", + "typedef struct __attribute__ ((__packed__)) {", + ( + " %s %s;" % (self.header.get_c_name(), self.fields[0].name) + if self.header is not None + else "" + ), + " %s payload;" % self.get_payload_struct_name(), + "} %s;" % self.get_c_name(), + ] + ) else: - return "\n".join([ - "typedef struct __attribute__ ((__packed__)) {", - (" %s %s;" % (self.header.get_c_name(), - self.fields[0].name) - if self.header is not None else ""), - "} %s;" % self.get_c_name(), ]) + return "\n".join( + [ + "typedef struct __attribute__ ((__packed__)) {", + ( + " %s %s;" % (self.header.get_c_name(), self.fields[0].name) + if self.header is not None + else "" + ), + "} %s;" % self.get_c_name(), + ] + ) def get_swap_payload_to_host_func_name(self): return "%s_payload_ntoh" % self.get_c_name() @@ -430,29 +526,37 @@ class CMessage (Message): def get_swap_payload_to_host_func_decl(self): return "void %s(%s *payload)" % ( self.get_swap_payload_to_host_func_name(), - self.get_payload_struct_name()) + self.get_payload_struct_name(), + ) def get_swap_payload_to_be_func_decl(self): return "void %s(%s *payload)" % ( self.get_swap_payload_to_be_func_name(), - self.get_payload_struct_name()) + self.get_payload_struct_name(), + ) def get_swap_payload_to_be_func_def(self): return "%s\n{\n%s\n}" % ( self.get_swap_payload_to_be_func_decl(), - "\n".join([ - " %s" % p.get_swap_to_be_code("payload->", "%s" % p.name) - for p in self.fields - if p.needs_byte_swap() and p.type != self.header]), + "\n".join( + [ + " %s" % p.get_swap_to_be_code("payload->", "%s" % p.name) + for p in self.fields + if p.needs_byte_swap() and p.type != self.header + ] + ), ) def get_swap_payload_to_host_func_def(self): return "%s\n{\n%s\n}" % ( self.get_swap_payload_to_host_func_decl(), - "\n".join([ - " %s" % p.get_swap_to_host_code("payload->", "%s" % p.name) - for p in self.fields - if p.needs_byte_swap() and p.type != self.header]), + "\n".join( + [ + " %s" % p.get_swap_to_host_code("payload->", "%s" % p.name) + for p in self.fields + if p.needs_byte_swap() and p.type != self.header + ] + ), ) def get_swap_to_host_func_name(self): @@ -463,37 +567,50 @@ class CMessage (Message): def get_swap_to_host_func_decl(self): return "void %s(%s *msg)" % ( - self.get_swap_to_host_func_name(), self.get_c_name()) + self.get_swap_to_host_func_name(), + self.get_c_name(), + ) def get_swap_to_be_func_decl(self): - return "void %s(%s *msg)" % ( - self.get_swap_to_be_func_name(), self.get_c_name()) + return "void %s(%s *msg)" % (self.get_swap_to_be_func_name(), self.get_c_name()) def get_swap_to_be_func_def(self): - return "\n".join([ - "%s" % self.get_swap_to_be_func_decl(), - "{", - (" VAPI_DBG(\"Swapping `%s'@%%p to big endian\", msg);" % - self.get_c_name()), - " %s(&msg->header);" % self.header.get_swap_to_be_func_name() - if self.header is not None else "", - " %s(&msg->payload);" % self.get_swap_payload_to_be_func_name() - if self.has_payload() else "", - "}", - ]) + return "\n".join( + [ + "%s" % self.get_swap_to_be_func_decl(), + "{", + ( + ' VAPI_DBG("Swapping `%s\'@%%p to big endian", msg);' + % self.get_c_name() + ), + " %s(&msg->header);" % self.header.get_swap_to_be_func_name() + if self.header is not None + else "", + " %s(&msg->payload);" % self.get_swap_payload_to_be_func_name() + if self.has_payload() + else "", + "}", + ] + ) def get_swap_to_host_func_def(self): - return "\n".join([ - "%s" % self.get_swap_to_host_func_decl(), - "{", - (" VAPI_DBG(\"Swapping `%s'@%%p to host byte order\", msg);" % - self.get_c_name()), - " %s(&msg->header);" % self.header.get_swap_to_host_func_name() - if self.header is not None else "", - " %s(&msg->payload);" % self.get_swap_payload_to_host_func_name() - if self.has_payload() else "", - "}", - ]) + return "\n".join( + [ + "%s" % self.get_swap_to_host_func_decl(), + "{", + ( + ' VAPI_DBG("Swapping `%s\'@%%p to host byte order", msg);' + % self.get_c_name() + ), + " %s(&msg->header);" % self.header.get_swap_to_host_func_name() + if self.header is not None + else "", + " %s(&msg->payload);" % self.get_swap_payload_to_host_func_name() + if self.has_payload() + else "", + "}", + ] + ) def get_op_func_name(self): return "vapi_%s" % self.name @@ -502,111 +619,126 @@ class CMessage (Message): if self.reply.has_payload(): return "vapi_error_e %s(%s)" % ( self.get_op_func_name(), - ",\n ".join([ - 'struct vapi_ctx_s *ctx', - '%s *msg' % self.get_c_name(), - 'vapi_error_e (*callback)(struct vapi_ctx_s *ctx', - ' void *callback_ctx', - ' vapi_error_e rv', - ' bool is_last', - ' %s *reply)' % - self.reply.get_payload_struct_name(), - 'void *callback_ctx', - ]) + ",\n ".join( + [ + "struct vapi_ctx_s *ctx", + "%s *msg" % self.get_c_name(), + "vapi_error_e (*callback)(struct vapi_ctx_s *ctx", + " void *callback_ctx", + " vapi_error_e rv", + " bool is_last", + " %s *reply)" + % self.reply.get_payload_struct_name(), + "void *callback_ctx", + ] + ), ) else: return "vapi_error_e %s(%s)" % ( self.get_op_func_name(), - ",\n ".join([ - 'struct vapi_ctx_s *ctx', - '%s *msg' % self.get_c_name(), - 'vapi_error_e (*callback)(struct vapi_ctx_s *ctx', - ' void *callback_ctx', - ' vapi_error_e rv', - ' bool is_last)', - 'void *callback_ctx', - ]) + ",\n ".join( + [ + "struct vapi_ctx_s *ctx", + "%s *msg" % self.get_c_name(), + "vapi_error_e (*callback)(struct vapi_ctx_s *ctx", + " void *callback_ctx", + " vapi_error_e rv", + " bool is_last)", + "void *callback_ctx", + ] + ), ) def get_op_func_def(self): - return "\n".join([ - "%s" % self.get_op_func_decl(), - "{", - " if (!msg || !callback) {", - " return VAPI_EINVAL;", - " }", - " if (vapi_is_nonblocking(ctx) && vapi_requests_full(ctx)) {", - " return VAPI_EAGAIN;", - " }", - " vapi_error_e rv;", - " if (VAPI_OK != (rv = vapi_producer_lock (ctx))) {", - " return rv;", - " }", - " u32 req_context = vapi_gen_req_context(ctx);", - " msg->header.context = req_context;", - " %s(msg);" % self.get_swap_to_be_func_name(), - (" if (VAPI_OK == (rv = vapi_send_with_control_ping " - "(ctx, msg, req_context))) {" - if self.reply_is_stream else - " if (VAPI_OK == (rv = vapi_send (ctx, msg))) {" - ), - (" vapi_store_request(ctx, req_context, %s, " - "(vapi_cb_t)callback, callback_ctx);" % - ("true" if self.reply_is_stream else "false")), - " if (VAPI_OK != vapi_producer_unlock (ctx)) {", - " abort (); /* this really shouldn't happen */", - " }", - " if (vapi_is_nonblocking(ctx)) {", - " rv = VAPI_OK;", - " } else {", - " rv = vapi_dispatch(ctx);", - " }", - " } else {", - " %s(msg);" % self.get_swap_to_host_func_name(), - " if (VAPI_OK != vapi_producer_unlock (ctx)) {", - " abort (); /* this really shouldn't happen */", - " }", - " }", - " return rv;", - "}", - "", - ]) + return "\n".join( + [ + "%s" % self.get_op_func_decl(), + "{", + " if (!msg || !callback) {", + " return VAPI_EINVAL;", + " }", + " if (vapi_is_nonblocking(ctx) && vapi_requests_full(ctx)) {", + " return VAPI_EAGAIN;", + " }", + " vapi_error_e rv;", + " if (VAPI_OK != (rv = vapi_producer_lock (ctx))) {", + " return rv;", + " }", + " u32 req_context = vapi_gen_req_context(ctx);", + " msg->header.context = req_context;", + " %s(msg);" % self.get_swap_to_be_func_name(), + ( + " if (VAPI_OK == (rv = vapi_send_with_control_ping " + "(ctx, msg, req_context))) {" + if self.reply_is_stream + else " if (VAPI_OK == (rv = vapi_send (ctx, msg))) {" + ), + ( + " vapi_store_request(ctx, req_context, %s, " + "(vapi_cb_t)callback, callback_ctx);" + % ("true" if self.reply_is_stream else "false") + ), + " if (VAPI_OK != vapi_producer_unlock (ctx)) {", + " abort (); /* this really shouldn't happen */", + " }", + " if (vapi_is_nonblocking(ctx)) {", + " rv = VAPI_OK;", + " } else {", + " rv = vapi_dispatch(ctx);", + " }", + " } else {", + " %s(msg);" % self.get_swap_to_host_func_name(), + " if (VAPI_OK != vapi_producer_unlock (ctx)) {", + " abort (); /* this really shouldn't happen */", + " }", + " }", + " return rv;", + "}", + "", + ] + ) def get_event_cb_func_decl(self): if not self.is_reply and not self.is_event: - raise Exception( - "Cannot register event callback for non-reply message") + raise Exception("Cannot register event callback for non-reply message") if self.has_payload(): - return "\n".join([ - "void vapi_set_%s_event_cb (" % - self.get_c_name(), - " struct vapi_ctx_s *ctx, ", - (" vapi_error_e (*callback)(struct vapi_ctx_s *ctx, " - "void *callback_ctx, %s *payload)," % - self.get_payload_struct_name()), - " void *callback_ctx)", - ]) + return "\n".join( + [ + "void vapi_set_%s_event_cb (" % self.get_c_name(), + " struct vapi_ctx_s *ctx, ", + ( + " vapi_error_e (*callback)(struct vapi_ctx_s *ctx, " + "void *callback_ctx, %s *payload)," + % self.get_payload_struct_name() + ), + " void *callback_ctx)", + ] + ) else: - return "\n".join([ - "void vapi_set_%s_event_cb (" % - self.get_c_name(), - " struct vapi_ctx_s *ctx, ", - " vapi_error_e (*callback)(struct vapi_ctx_s *ctx, " - "void *callback_ctx),", - " void *callback_ctx)", - ]) + return "\n".join( + [ + "void vapi_set_%s_event_cb (" % self.get_c_name(), + " struct vapi_ctx_s *ctx, ", + " vapi_error_e (*callback)(struct vapi_ctx_s *ctx, " + "void *callback_ctx),", + " void *callback_ctx)", + ] + ) def get_event_cb_func_def(self): if not self.is_reply and not self.is_event: - raise Exception( - "Cannot register event callback for non-reply function") - return "\n".join([ - "%s" % self.get_event_cb_func_decl(), - "{", - (" vapi_set_event_cb(ctx, %s, (vapi_event_cb)callback, " - "callback_ctx);" % - self.get_msg_id_name()), - "}"]) + raise Exception("Cannot register event callback for non-reply function") + return "\n".join( + [ + "%s" % self.get_event_cb_func_decl(), + "{", + ( + " vapi_set_event_cb(ctx, %s, (vapi_event_cb)callback, " + "callback_ctx);" % self.get_msg_id_name() + ), + "}", + ] + ) def get_c_metadata_struct_name(self): return "__vapi_metadata_%s" % self.name @@ -614,38 +746,41 @@ class CMessage (Message): def get_c_constructor(self): has_context = False if self.header is not None: - has_context = self.header.has_field('context') - return '\n'.join([ - 'static void __attribute__((constructor)) __vapi_constructor_%s()' - % self.name, - '{', - ' static const char name[] = "%s";' % self.name, - ' static const char name_with_crc[] = "%s_%s";' - % (self.name, self.crc[2:]), - ' static vapi_message_desc_t %s = {' % - self.get_c_metadata_struct_name(), - ' name,', - ' sizeof(name) - 1,', - ' name_with_crc,', - ' sizeof(name_with_crc) - 1,', - ' true,' if has_context else ' false,', - ' offsetof(%s, context),' % self.header.get_c_name() - if has_context else ' 0,', - (' offsetof(%s, payload),' % self.get_c_name()) - if self.has_payload() else ' VAPI_INVALID_MSG_ID,', - ' (verify_msg_size_fn_t)%s,' % - self.get_verify_msg_size_func_name(), - ' (generic_swap_fn_t)%s,' % self.get_swap_to_be_func_name(), - ' (generic_swap_fn_t)%s,' % self.get_swap_to_host_func_name(), - ' VAPI_INVALID_MSG_ID,', - ' };', - '', - ' %s = vapi_register_msg(&%s);' % - (self.get_msg_id_name(), self.get_c_metadata_struct_name()), - ' VAPI_DBG("Assigned msg id %%d to %s", %s);' % - (self.name, self.get_msg_id_name()), - '}', - ]) + has_context = self.header.has_field("context") + return "\n".join( + [ + "static void __attribute__((constructor)) __vapi_constructor_%s()" + % self.name, + "{", + ' static const char name[] = "%s";' % self.name, + ' static const char name_with_crc[] = "%s_%s";' + % (self.name, self.crc[2:]), + " static vapi_message_desc_t %s = {" + % self.get_c_metadata_struct_name(), + " name,", + " sizeof(name) - 1,", + " name_with_crc,", + " sizeof(name_with_crc) - 1,", + " true," if has_context else " false,", + " offsetof(%s, context)," % self.header.get_c_name() + if has_context + else " 0,", + (" offsetof(%s, payload)," % self.get_c_name()) + if self.has_payload() + else " VAPI_INVALID_MSG_ID,", + " (verify_msg_size_fn_t)%s," % self.get_verify_msg_size_func_name(), + " (generic_swap_fn_t)%s," % self.get_swap_to_be_func_name(), + " (generic_swap_fn_t)%s," % self.get_swap_to_host_func_name(), + " VAPI_INVALID_MSG_ID,", + " };", + "", + " %s = vapi_register_msg(&%s);" + % (self.get_msg_id_name(), self.get_c_metadata_struct_name()), + ' VAPI_DBG("Assigned msg id %%d to %s", %s);' + % (self.name, self.get_msg_id_name()), + "}", + ] + ) def emit_definition(parser, json_file, emitted, o): @@ -659,11 +794,13 @@ def emit_definition(parser, json_file, emitted, o): if hasattr(o, "reply"): emit_definition(parser, json_file, emitted, o.reply) if hasattr(o, "get_c_def"): - if (o not in parser.enums_by_json[json_file] and - o not in parser.types_by_json[json_file] and - o not in parser.unions_by_json[json_file] and - o.name not in parser.messages_by_json[json_file] and - o not in parser.aliases_by_json[json_file]): + if ( + o not in parser.enums_by_json[json_file] + and o not in parser.types_by_json[json_file] + and o not in parser.unions_by_json[json_file] + and o.name not in parser.messages_by_json[json_file] + and o not in parser.aliases_by_json[json_file] + ): return guard = "defined_%s" % o.get_c_name() print("#ifndef %s" % guard) @@ -673,11 +810,9 @@ def emit_definition(parser, json_file, emitted, o): function_attrs = "static inline " if o.name in parser.messages_by_json[json_file]: if o.has_payload(): - print("%s%s" % (function_attrs, - o.get_swap_payload_to_be_func_def())) + print("%s%s" % (function_attrs, o.get_swap_payload_to_be_func_def())) print("") - print("%s%s" % (function_attrs, - o.get_swap_payload_to_host_func_def())) + print("%s%s" % (function_attrs, o.get_swap_payload_to_host_func_def())) print("") print("%s%s" % (function_attrs, o.get_swap_to_be_func_def())) print("") @@ -711,8 +846,12 @@ def gen_json_unified_header(parser, logger, j, io, name): orig_stdout = sys.stdout sys.stdout = io include_guard = "__included_%s" % ( - j.replace(".", "_").replace("/", "_").replace("-", "_").replace( - "+", "_").replace("@", "_")) + j.replace(".", "_") + .replace("/", "_") + .replace("-", "_") + .replace("+", "_") + .replace("@", "_") + ) print("#ifndef %s" % include_guard) print("#define %s" % include_guard) print("") @@ -724,12 +863,14 @@ def gen_json_unified_header(parser, logger, j, io, name): print("#include <vapi/vapi_dbg.h>") print("") print("#ifdef __cplusplus") - print("extern \"C\" {") + print('extern "C" {') print("#endif") if name == "memclnt.api.vapi.h": print("") - print("static inline vapi_error_e vapi_send_with_control_ping " - "(vapi_ctx_t ctx, void * msg, u32 context);") + print( + "static inline vapi_error_e vapi_send_with_control_ping " + "(vapi_ctx_t ctx, void * msg, u32 context);" + ) elif name == "vlib.api.vapi.h": print("#include <vapi/memclnt.api.vapi.h>") else: @@ -738,12 +879,18 @@ def gen_json_unified_header(parser, logger, j, io, name): for m in parser.messages_by_json[j].values(): print("extern vapi_msg_id_t %s;" % m.get_msg_id_name()) print("") - print("#define DEFINE_VAPI_MSG_IDS_%s\\" % - f.replace(".", "_").replace("/", "_").replace("-", "_").upper()) - print("\\\n".join([ - " vapi_msg_id_t %s;" % m.get_msg_id_name() - for m in parser.messages_by_json[j].values() - ])) + print( + "#define DEFINE_VAPI_MSG_IDS_%s\\" + % f.replace(".", "_").replace("/", "_").replace("-", "_").upper() + ) + print( + "\\\n".join( + [ + " vapi_msg_id_t %s;" % m.get_msg_id_name() + for m in parser.messages_by_json[j].values() + ] + ) + ) print("") print("") emitted = [] @@ -802,12 +949,11 @@ def gen_c_unified_headers(parser, logger, prefix, remove_path): d, f = os.path.split(j) else: f = j - with open('%s%s' % (prefix, json_to_c_header_name(f)), "w") as io: - gen_json_unified_header( - parser, logger, j, io, json_to_c_header_name(f)) + with open("%s%s" % (prefix, json_to_c_header_name(f)), "w") as io: + gen_json_unified_header(parser, logger, j, io, json_to_c_header_name(f)) -if __name__ == '__main__': +if __name__ == "__main__": try: verbose = int(os.getenv("V", 0)) except: @@ -825,23 +971,30 @@ if __name__ == '__main__': logger.setLevel(log_level) argparser = argparse.ArgumentParser(description="VPP C API generator") - argparser.add_argument('files', metavar='api-file', action='append', - type=str, help='json api file' - '(may be specified multiple times)') - argparser.add_argument('--prefix', action='store', default=None, - help='path prefix') - argparser.add_argument('--remove-path', action='store_true', - help='remove path from filename') + argparser.add_argument( + "files", + metavar="api-file", + action="append", + type=str, + help="json api file" "(may be specified multiple times)", + ) + argparser.add_argument("--prefix", action="store", default=None, help="path prefix") + argparser.add_argument( + "--remove-path", action="store_true", help="remove path from filename" + ) args = argparser.parse_args() - jsonparser = JsonParser(logger, args.files, - simple_type_class=CSimpleType, - enum_class=CEnum, - union_class=CUnion, - struct_type_class=CStructType, - field_class=CField, - message_class=CMessage, - alias_class=CAlias) + jsonparser = JsonParser( + logger, + args.files, + simple_type_class=CSimpleType, + enum_class=CEnum, + union_class=CUnion, + struct_type_class=CStructType, + field_class=CField, + message_class=CMessage, + alias_class=CAlias, + ) # not using the model of having separate generated header and code files # with generated symbols present in shared library (per discussion with diff --git a/src/vpp-api/vapi/vapi_cpp_gen.py b/src/vpp-api/vapi/vapi_cpp_gen.py index 7bc2e7fecf0..33744a3d58c 100755 --- a/src/vpp-api/vapi/vapi_cpp_gen.py +++ b/src/vpp-api/vapi/vapi_cpp_gen.py @@ -4,8 +4,16 @@ import argparse import os import sys import logging -from vapi_c_gen import CField, CEnum, CStruct, CSimpleType, CStructType,\ - CMessage, json_to_c_header_name, CAlias +from vapi_c_gen import ( + CField, + CEnum, + CStruct, + CSimpleType, + CStructType, + CMessage, + json_to_c_header_name, + CAlias, +) from vapi_json_parser import JsonParser @@ -25,53 +33,64 @@ class CppAlias(CAlias): pass -class CppSimpleType (CSimpleType): +class CppSimpleType(CSimpleType): pass -class CppStructType (CStructType, CppStruct): +class CppStructType(CStructType, CppStruct): pass -class CppMessage (CMessage): +class CppMessage(CMessage): def get_swap_to_be_template_instantiation(self): - return "\n".join([ - "template <> inline void vapi_swap_to_be<%s>(%s *msg)" % - (self.get_c_name(), self.get_c_name()), - "{", - " %s(msg);" % self.get_swap_to_be_func_name(), - "}", - ]) + return "\n".join( + [ + "template <> inline void vapi_swap_to_be<%s>(%s *msg)" + % (self.get_c_name(), self.get_c_name()), + "{", + " %s(msg);" % self.get_swap_to_be_func_name(), + "}", + ] + ) def get_swap_to_host_template_instantiation(self): - return "\n".join([ - "template <> inline void vapi_swap_to_host<%s>(%s *msg)" % - (self.get_c_name(), self.get_c_name()), - "{", - " %s(msg);" % self.get_swap_to_host_func_name(), - "}", - ]) + return "\n".join( + [ + "template <> inline void vapi_swap_to_host<%s>(%s *msg)" + % (self.get_c_name(), self.get_c_name()), + "{", + " %s(msg);" % self.get_swap_to_host_func_name(), + "}", + ] + ) def get_alloc_template_instantiation(self): - return "\n".join([ - "template <> inline %s* vapi_alloc<%s%s>" - "(Connection &con%s)" % - (self.get_c_name(), self.get_c_name(), - ", size_t" * len(self.get_alloc_vla_param_names()), - "".join([", size_t %s" % n for n in - self.get_alloc_vla_param_names()]) - ), - "{", - " %s* result = %s(con.vapi_ctx%s);" % - (self.get_c_name(), self.get_alloc_func_name(), - "".join([", %s" % n - for n in self.get_alloc_vla_param_names()])), - "#if VAPI_CPP_DEBUG_LEAKS", - " con.on_shm_data_alloc(result);", - "#endif", - " return result;", - "}", - ]) + return "\n".join( + [ + "template <> inline %s* vapi_alloc<%s%s>" + "(Connection &con%s)" + % ( + self.get_c_name(), + self.get_c_name(), + ", size_t" * len(self.get_alloc_vla_param_names()), + "".join( + [", size_t %s" % n for n in self.get_alloc_vla_param_names()] + ), + ), + "{", + " %s* result = %s(con.vapi_ctx%s);" + % ( + self.get_c_name(), + self.get_alloc_func_name(), + "".join([", %s" % n for n in self.get_alloc_vla_param_names()]), + ), + "#if VAPI_CPP_DEBUG_LEAKS", + " con.on_shm_data_alloc(result);", + "#endif", + " return result;", + "}", + ] + ) def get_cpp_name(self): return "%s%s" % (self.name[0].upper(), self.name[1:]) @@ -86,51 +105,60 @@ class CppMessage (CMessage): template, self.get_c_name(), self.reply.get_c_name(), - "".join([", size_t"] * len(self.get_alloc_vla_param_names())) + "".join([", size_t"] * len(self.get_alloc_vla_param_names())), ) def get_req_template_instantiation(self): return "template class %s;" % self.get_req_template_name() def get_type_alias(self): - return "using %s = %s;" % ( - self.get_cpp_name(), self.get_req_template_name()) + return "using %s = %s;" % (self.get_cpp_name(), self.get_req_template_name()) def get_reply_template_name(self): return "Msg<%s>" % (self.get_c_name()) def get_reply_type_alias(self): - return "using %s = %s;" % ( - self.get_cpp_name(), self.get_reply_template_name()) + return "using %s = %s;" % (self.get_cpp_name(), self.get_reply_template_name()) def get_msg_class_instantiation(self): return "template class Msg<%s>;" % self.get_c_name() def get_get_msg_id_t_instantiation(self): - return "\n".join([ - ("template <> inline vapi_msg_id_t vapi_get_msg_id_t<%s>()" - % self.get_c_name()), - "{", - " return ::%s; " % self.get_msg_id_name(), - "}", - "", - ("template <> inline vapi_msg_id_t " - "vapi_get_msg_id_t<Msg<%s>>()" % self.get_c_name()), - "{", - " return ::%s; " % self.get_msg_id_name(), - "}", - ]) + return "\n".join( + [ + ( + "template <> inline vapi_msg_id_t vapi_get_msg_id_t<%s>()" + % self.get_c_name() + ), + "{", + " return ::%s; " % self.get_msg_id_name(), + "}", + "", + ( + "template <> inline vapi_msg_id_t " + "vapi_get_msg_id_t<Msg<%s>>()" % self.get_c_name() + ), + "{", + " return ::%s; " % self.get_msg_id_name(), + "}", + ] + ) def get_cpp_constructor(self): - return '\n'.join([ - ('static void __attribute__((constructor)) ' - '__vapi_cpp_constructor_%s()' - % self.name), - '{', - (' vapi::vapi_msg_set_msg_id<%s>(%s);' % ( - self.get_c_name(), self.get_msg_id_name())), - '}', - ]) + return "\n".join( + [ + ( + "static void __attribute__((constructor)) " + "__vapi_cpp_constructor_%s()" % self.name + ), + "{", + ( + " vapi::vapi_msg_set_msg_id<%s>(%s);" + % (self.get_c_name(), self.get_msg_id_name()) + ), + "}", + ] + ) def gen_json_header(parser, logger, j, io, gen_h_prefix, add_debug_comments): @@ -139,8 +167,8 @@ def gen_json_header(parser, logger, j, io, gen_h_prefix, add_debug_comments): sys.stdout = io d, f = os.path.split(j) include_guard = "__included_hpp_%s" % ( - f.replace(".", "_").replace("/", "_").replace("-", "_").replace( - "@", "_")) + f.replace(".", "_").replace("/", "_").replace("-", "_").replace("@", "_") + ) print("#ifndef %s" % include_guard) print("#define %s" % include_guard) print("") @@ -202,8 +230,9 @@ def json_to_cpp_header_name(json_name): raise Exception("Unexpected json name `%s'!" % json_name) -def gen_cpp_headers(parser, logger, prefix, gen_h_prefix, remove_path, - add_debug_comments=False): +def gen_cpp_headers( + parser, logger, prefix, gen_h_prefix, remove_path, add_debug_comments=False +): if prefix == "" or prefix is None: prefix = "" else: @@ -217,12 +246,11 @@ def gen_cpp_headers(parser, logger, prefix, gen_h_prefix, remove_path, d, f = os.path.split(j) else: f = j - with open('%s%s' % (prefix, json_to_cpp_header_name(f)), "w") as io: - gen_json_header(parser, logger, j, io, - gen_h_prefix, add_debug_comments) + with open("%s%s" % (prefix, json_to_cpp_header_name(f)), "w") as io: + gen_json_header(parser, logger, j, io, gen_h_prefix, add_debug_comments) -if __name__ == '__main__': +if __name__ == "__main__": try: verbose = int(os.getenv("V", 0)) except: @@ -240,27 +268,36 @@ if __name__ == '__main__': logger.setLevel(log_level) argparser = argparse.ArgumentParser(description="VPP C++ API generator") - argparser.add_argument('files', metavar='api-file', action='append', - type=str, help='json api file' - '(may be specified multiple times)') - argparser.add_argument('--prefix', action='store', default=None, - help='path prefix') - argparser.add_argument('--gen-h-prefix', action='store', default=None, - help='generated C header prefix') - argparser.add_argument('--remove-path', action='store_true', - help='remove path from filename') + argparser.add_argument( + "files", + metavar="api-file", + action="append", + type=str, + help="json api file" "(may be specified multiple times)", + ) + argparser.add_argument("--prefix", action="store", default=None, help="path prefix") + argparser.add_argument( + "--gen-h-prefix", action="store", default=None, help="generated C header prefix" + ) + argparser.add_argument( + "--remove-path", action="store_true", help="remove path from filename" + ) args = argparser.parse_args() - jsonparser = JsonParser(logger, args.files, - simple_type_class=CppSimpleType, - struct_type_class=CppStructType, - field_class=CppField, - enum_class=CppEnum, - message_class=CppMessage, - alias_class=CppAlias) - - gen_cpp_headers(jsonparser, logger, args.prefix, args.gen_h_prefix, - args.remove_path) + jsonparser = JsonParser( + logger, + args.files, + simple_type_class=CppSimpleType, + struct_type_class=CppStructType, + field_class=CppField, + enum_class=CppEnum, + message_class=CppMessage, + alias_class=CppAlias, + ) + + gen_cpp_headers( + jsonparser, logger, args.prefix, args.gen_h_prefix, args.remove_path + ) for e in jsonparser.exceptions: logger.warning(e) diff --git a/src/vpp-api/vapi/vapi_json_parser.py b/src/vpp-api/vapi/vapi_json_parser.py index 1383d456bf1..a323f15e7b6 100644 --- a/src/vpp-api/vapi/vapi_json_parser.py +++ b/src/vpp-api/vapi/vapi_json_parser.py @@ -3,7 +3,7 @@ import json -class ParseError (Exception): +class ParseError(Exception): pass @@ -13,14 +13,12 @@ magic_suffix = "_t" def remove_magic(what): if what.startswith(magic_prefix) and what.endswith(magic_suffix): - return what[len(magic_prefix): - len(magic_suffix)] + return what[len(magic_prefix) : -len(magic_suffix)] return what class Field(object): - - def __init__(self, field_name, field_type, array_len=None, - nelem_field=None): + def __init__(self, field_name, field_type, array_len=None, nelem_field=None): self.name = field_name self.type = field_type self.len = array_len @@ -30,17 +28,23 @@ class Field(object): if self.len is None: return "Field(name: %s, type: %s)" % (self.name, self.type) elif type(self.len) == dict: - return "Field(name: %s, type: %s, length: %s)" % (self.name, - self.type, - self.len) + return "Field(name: %s, type: %s, length: %s)" % ( + self.name, + self.type, + self.len, + ) elif self.len > 0: - return "Field(name: %s, type: %s, length: %s)" % (self.name, - self.type, - self.len) + return "Field(name: %s, type: %s, length: %s)" % ( + self.name, + self.type, + self.len, + ) else: - return ( - "Field(name: %s, type: %s, variable length stored in: %s)" % - (self.name, self.type, self.nelem_field)) + return "Field(name: %s, type: %s, variable length stored in: %s)" % ( + self.name, + self.type, + self.nelem_field, + ) def is_vla(self): return self.nelem_field is not None @@ -61,32 +65,38 @@ class Type(object): return self.name -class SimpleType (Type): - +class SimpleType(Type): def has_vla(self): return False def get_msg_header_defs(struct_type_class, field_class, json_parser, logger): return [ - struct_type_class(['msg_header1_t', - ['u16', '_vl_msg_id'], - ['u32', 'context'], - ], - json_parser, field_class, logger - ), - struct_type_class(['msg_header2_t', - ['u16', '_vl_msg_id'], - ['u32', 'client_index'], - ['u32', 'context'], - ], - json_parser, field_class, logger - ), + struct_type_class( + [ + "msg_header1_t", + ["u16", "_vl_msg_id"], + ["u32", "context"], + ], + json_parser, + field_class, + logger, + ), + struct_type_class( + [ + "msg_header2_t", + ["u16", "_vl_msg_id"], + ["u32", "client_index"], + ["u32", "context"], + ], + json_parser, + field_class, + logger, + ), ] class Struct(object): - def __init__(self, name, fields): self.name = name self.fields = fields @@ -112,7 +122,7 @@ class Enum(SimpleType): def __str__(self): return "Enum(%s, [%s])" % ( self.name, - "], [" .join(["%s => %s" % (i, j) for i, j in self.value_pairs]) + "], [".join(["%s => %s" % (i, j) for i, j in self.value_pairs]), ) @@ -126,7 +136,7 @@ class Union(Type): def __str__(self): return "Union(%s, [%s])" % ( self.name, - "], [" .join(["%s %s" % (i, j) for i, j in self.type_pairs]) + "], [".join(["%s %s" % (i, j) for i, j in self.type_pairs]), ) def has_vla(self): @@ -134,7 +144,6 @@ class Union(Type): class Message(object): - def __init__(self, logger, definition, json_parser): struct_type_class = json_parser.struct_type_class field_class = json_parser.field_class @@ -150,22 +159,24 @@ class Message(object): self.is_reply = json_parser.is_reply(self.name) self.is_event = json_parser.is_event(self.name) fields = [] - for header in get_msg_header_defs(struct_type_class, field_class, - json_parser, logger): + for header in get_msg_header_defs( + struct_type_class, field_class, json_parser, logger + ): logger.debug("Probing header `%s'" % header.name) if header.is_part_of_def(m[1:]): self.header = header logger.debug("Found header `%s'" % header.name) - fields.append(field_class(field_name='header', - field_type=self.header)) + fields.append(field_class(field_name="header", field_type=self.header)) ignore = False break if ignore and not self.is_event and not self.is_reply: - raise ParseError("While parsing message `%s': could not find all " - "common header fields" % name) + raise ParseError( + "While parsing message `%s': could not find all " + "common header fields" % name + ) for field in m[1:]: - if isinstance(field, dict) and 'crc' in field: - self.crc = field['crc'] + if isinstance(field, dict) and "crc" in field: + self.crc = field["crc"] logger.debug("Found CRC `%s'" % self.crc) continue else: @@ -175,25 +186,22 @@ class Message(object): if any(type(n) is dict for n in field): l -= 1 if l == 2: - if self.header is not None and\ - self.header.has_field(field[1]): + if self.header is not None and self.header.has_field(field[1]): continue - p = field_class(field_name=field[1], - field_type=field_type) + p = field_class(field_name=field[1], field_type=field_type) elif l == 3: - if field[2] == 0 and field[0] != 'string': + if field[2] == 0 and field[0] != "string": raise ParseError( "While parsing message `%s': variable length " "array `%s' doesn't have reference to member " - "containing the actual length" % ( - name, field[1])) - if field[0] == 'string' and field[2] > 0: - field_type = json_parser.lookup_type_like_id('u8') + "containing the actual length" % (name, field[1]) + ) + if field[0] == "string" and field[2] > 0: + field_type = json_parser.lookup_type_like_id("u8") p = field_class( - field_name=field[1], - field_type=field_type, - array_len=field[2]) + field_name=field[1], field_type=field_type, array_len=field[2] + ) elif l == 4: nelem_field = None for f in fields: @@ -203,17 +211,19 @@ class Message(object): raise ParseError( "While parsing message `%s': couldn't find " "variable length array `%s' member containing " - "the actual length `%s'" % ( - name, field[1], field[3])) + "the actual length `%s'" % (name, field[1], field[3]) + ) p = field_class( field_name=field[1], field_type=field_type, array_len=field[2], - nelem_field=nelem_field) + nelem_field=nelem_field, + ) else: - raise Exception("Don't know how to parse message " - "definition for message `%s': `%s'" % - (m, m[1:])) + raise Exception( + "Don't know how to parse message " + "definition for message `%s': `%s'" % (m, m[1:]) + ) logger.debug("Parsed field `%s'" % p) fields.append(p) self.fields = fields @@ -221,35 +231,36 @@ class Message(object): logger.debug("Parsed message: %s" % self) def __str__(self): - return "Message(%s, [%s], {crc: %s}" % \ - (self.name, - "], [".join([str(f) for f in self.fields]), - self.crc) - + return "Message(%s, [%s], {crc: %s}" % ( + self.name, + "], [".join([str(f) for f in self.fields]), + self.crc, + ) -class StructType (Type, Struct): +class StructType(Type, Struct): def __init__(self, definition, json_parser, field_class, logger): t = definition logger.debug("Parsing struct definition `%s'" % t) name = t[0] fields = [] for field in t[1:]: - if len(field) == 1 and 'crc' in field: - self.crc = field['crc'] + if len(field) == 1 and "crc" in field: + self.crc = field["crc"] continue field_type = json_parser.lookup_type_like_id(field[0]) logger.debug("Parsing type field `%s'" % field) if len(field) == 2: - p = field_class(field_name=field[1], - field_type=field_type) + p = field_class(field_name=field[1], field_type=field_type) elif len(field) == 3: if field[2] == 0: - raise ParseError("While parsing type `%s': array `%s' has " - "variable length" % (name, field[1])) - p = field_class(field_name=field[1], - field_type=field_type, - array_len=field[2]) + raise ParseError( + "While parsing type `%s': array `%s' has " + "variable length" % (name, field[1]) + ) + p = field_class( + field_name=field[1], field_type=field_type, array_len=field[2] + ) elif len(field) == 4: nelem_field = None for f in fields: @@ -259,23 +270,25 @@ class StructType (Type, Struct): raise ParseError( "While parsing message `%s': couldn't find " "variable length array `%s' member containing " - "the actual length `%s'" % ( - name, field[1], field[3])) - p = field_class(field_name=field[1], - field_type=field_type, - array_len=field[2], - nelem_field=nelem_field) + "the actual length `%s'" % (name, field[1], field[3]) + ) + p = field_class( + field_name=field[1], + field_type=field_type, + array_len=field[2], + nelem_field=nelem_field, + ) else: raise ParseError( "Don't know how to parse field `%s' of type definition " - "for type `%s'" % (field, t)) + "for type `%s'" % (field, t) + ) fields.append(p) Type.__init__(self, name) Struct.__init__(self, name, fields) def __str__(self): - return "StructType(%s, %s)" % (Type.__str__(self), - Struct.__str__(self)) + return "StructType(%s, %s)" % (Type.__str__(self), Struct.__str__(self)) def has_field(self, name): return name in self.field_names @@ -289,30 +302,47 @@ class StructType (Type, Struct): if field[0] != p.type.name: raise ParseError( "Unexpected field type `%s' (should be `%s'), " - "while parsing msg/def/field `%s/%s/%s'" % - (field[0], p.type, p.name, definition, field)) + "while parsing msg/def/field `%s/%s/%s'" + % (field[0], p.type, p.name, definition, field) + ) return True class JsonParser(object): - def __init__(self, logger, files, simple_type_class=SimpleType, - enum_class=Enum, union_class=Union, - struct_type_class=StructType, field_class=Field, - message_class=Message, alias_class=Alias): + def __init__( + self, + logger, + files, + simple_type_class=SimpleType, + enum_class=Enum, + union_class=Union, + struct_type_class=StructType, + field_class=Field, + message_class=Message, + alias_class=Alias, + ): self.services = {} self.messages = {} self.enums = {} self.unions = {} self.aliases = {} self.types = { - x: simple_type_class(x) for x in [ - 'i8', 'i16', 'i32', 'i64', - 'u8', 'u16', 'u32', 'u64', - 'f64', 'bool' + x: simple_type_class(x) + for x in [ + "i8", + "i16", + "i32", + "i64", + "u8", + "u16", + "u32", + "u64", + "f64", + "bool", ] } - self.types['string'] = simple_type_class('vl_api_string_t') + self.types["string"] = simple_type_class("vl_api_string_t") self.replies = set() self.events = set() self.simple_type_class = simple_type_class @@ -345,15 +375,15 @@ class JsonParser(object): self.messages_by_json[path] = {} with open(path) as f: j = json.load(f) - for k in j['services']: + for k in j["services"]: if k in self.services: raise ParseError("Duplicate service `%s'" % k) - self.services[k] = j['services'][k] + self.services[k] = j["services"][k] self.replies.add(self.services[k]["reply"]) if "events" in self.services[k]: for x in self.services[k]["events"]: self.events.add(x) - for e in j['enums']: + for e in j["enums"]: name = e[0] value_pairs = e[1:-1] enumtype = self.types[e[-1]["enumtype"]] @@ -365,14 +395,15 @@ class JsonParser(object): progress = 0 last_progress = 0 while True: - for u in j['unions']: + for u in j["unions"]: name = u[0] if name in self.unions: progress = progress + 1 continue try: - type_pairs = [[self.lookup_type_like_id(t), n] - for t, n in u[1:]] + type_pairs = [ + [self.lookup_type_like_id(t), n] for t, n in u[1:] + ] union = self.union_class(name, type_pairs, 0) progress = progress + 1 except ParseError as e: @@ -381,17 +412,16 @@ class JsonParser(object): self.unions[union.name] = union self.logger.debug("Parsed union: %s" % union) self.unions_by_json[path].append(union) - for t in j['types']: + for t in j["types"]: if t[0] in self.types: progress = progress + 1 continue try: - type_ = self.struct_type_class(t, self, - self.field_class, - self.logger) + type_ = self.struct_type_class( + t, self, self.field_class, self.logger + ) if type_.name in self.types: - raise ParseError( - "Duplicate type `%s'" % type_.name) + raise ParseError("Duplicate type `%s'" % type_.name) progress = progress + 1 except ParseError as e: exceptions.append(e) @@ -399,16 +429,16 @@ 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'].items(): + for name, body in j["aliases"].items(): if name in self.aliases: progress = progress + 1 continue - if 'length' in body: - array_len = body['length'] + if "length" in body: + array_len = body["length"] else: array_len = None try: - t = self.lookup_type_like_id(body['type']) + t = self.lookup_type_like_id(body["type"]) except ParseError as e: exceptions.append(e) continue @@ -430,14 +460,13 @@ class JsonParser(object): processed = [] while True: exceptions = [] - for m in j['messages']: + for m in j["messages"]: if m in processed: continue try: msg = self.message_class(self.logger, m, self) if msg.name in self.messages: - raise ParseError( - "Duplicate message `%s'" % msg.name) + raise ParseError("Duplicate message `%s'" % msg.name) except ParseError as e: exceptions.append(e) continue @@ -470,7 +499,8 @@ class JsonParser(object): return self.aliases[mundane_name] raise ParseError( "Could not find type, enum or union by magic name `%s' nor by " - "mundane name `%s'" % (name, mundane_name)) + "mundane name `%s'" % (name, mundane_name) + ) def is_reply(self, message): return message in self.replies @@ -479,7 +509,7 @@ class JsonParser(object): return message in self.events def get_reply(self, message): - return self.messages[self.services[message]['reply']] + return self.messages[self.services[message]["reply"]] def finalize_parsing(self): if len(self.messages) == 0: @@ -493,17 +523,14 @@ class JsonParser(object): try: m.reply = self.get_reply(n) if "stream" in self.services[m.name]: - m.reply_is_stream = \ - self.services[m.name]["stream"] + m.reply_is_stream = self.services[m.name]["stream"] else: m.reply_is_stream = False m.reply.request = m except: - raise ParseError( - "Cannot find reply to message `%s'" % n) + raise ParseError("Cannot find reply to message `%s'" % n) except ParseError as e: self.exceptions.append(e) remove.append(n) - self.messages_by_json[jn] = { - k: v for k, v in j.items() if k not in remove} + self.messages_by_json[jn] = {k: v for k, v in j.items() if k not in remove} |