From 15b16695c673426e334777e7d794794862ab914a Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Tue, 4 Sep 2018 19:04:27 +0800 Subject: [PATCH] frontend: add artiq_route --- artiq/frontend/artiq_route.py | 83 +++++++++++++++++++++++++++++++++++ setup.py | 1 + 2 files changed, 84 insertions(+) create mode 100755 artiq/frontend/artiq_route.py diff --git a/artiq/frontend/artiq_route.py b/artiq/frontend/artiq_route.py new file mode 100755 index 000000000..c818237ce --- /dev/null +++ b/artiq/frontend/artiq_route.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 + +import argparse + + +def get_argparser(): + parser = argparse.ArgumentParser(description="ARTIQ DRTIO routing table " + "manipulation tool") + + parser.add_argument("file", metavar="FILE", type=str, + help="target file") + + action = parser.add_subparsers(dest="action") + action.required = True + + action.add_parser("init", help="create a new empty routing table") + + action.add_parser("show", help="show contents of routing table") + + a_set = action.add_parser("set", help="set routing table entry") + a_set.add_argument("destination", metavar="DESTINATION", type=int, + help="destination to operate on") + a_set.add_argument("hop", metavar="HOP", type=int, nargs="*", + help="hop(s) to the destination") + + return parser + + +ENTRY_COUNT = 256 +MAX_HOPS = 32 + + +def init(filename): + with open(filename, "wb") as f: + f.write(b"\xff"*(ENTRY_COUNT*MAX_HOPS)) + + +def show_routes(filename): + routes = [] + with open(filename, "rb") as f: + for i in range(ENTRY_COUNT): + hops = [int.from_bytes(f.read(1), "big") for j in range(MAX_HOPS)] + routes.append(hops) + + for destination, route in enumerate(routes): + if route[0] != 0xff: + fmt = "{:3d}:".format(destination) + for hop in route: + if hop == 0xff: + break + fmt += " {:3d}".format(hop) + print(fmt) + + +def set_route(filename, destination, hops): + with open(filename, "r+b") as f: + if destination >= ENTRY_COUNT: + raise ValueError("destination must be less than {}".format(ENTRY_COUNT)) + f.seek(destination*MAX_HOPS) + + if len(hops) + 1 >= MAX_HOPS: + raise ValueError("too many hops") + for hop in hops: + if hop >= 0xff: + raise ValueError("all hops must be less than 255") + + hops = hops + [0xff]*(MAX_HOPS-len(hops)) + f.write(bytes(hops)) + + +def main(): + args = get_argparser().parse_args() + if args.action == "init": + init(args.file) + elif args.action == "show": + show_routes(args.file) + elif args.action == "set": + set_route(args.file, args.destination, args.hop) + else: + raise ValueError + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index a85f23ae5..b4c25b027 100755 --- a/setup.py +++ b/setup.py @@ -30,6 +30,7 @@ console_scripts = [ "artiq_master = artiq.frontend.artiq_master:main", "artiq_mkfs = artiq.frontend.artiq_mkfs:main", "artiq_session = artiq.frontend.artiq_session:main", + "artiq_route = artiq.frontend.artiq_route:main", "artiq_rpctool = artiq.frontend.artiq_rpctool:main", "artiq_run = artiq.frontend.artiq_run:main", "artiq_flash = artiq.frontend.artiq_flash:main",