|
| 1 | +#!/usr/bin/env python3 |
| 2 | +#------------------------------------------------------------------------------- |
| 3 | +# SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: BSD-3-Clause |
| 6 | +# |
| 7 | +#------------------------------------------------------------------------------- |
| 8 | + |
| 9 | +import arg_utils |
| 10 | +import c_struct |
| 11 | +import c_macro |
| 12 | +import c_include |
| 13 | +import argparse |
| 14 | +import pickle |
| 15 | +import re |
| 16 | +import networkx as nx |
| 17 | + |
| 18 | +import logging |
| 19 | +logger = logging.getLogger("TF-M.{}".format(__name__)) |
| 20 | + |
| 21 | +def add_arguments(parser : argparse.ArgumentParser, |
| 22 | + prefix : str = "", |
| 23 | + required : bool = True, |
| 24 | + ) -> None: |
| 25 | + |
| 26 | + return arg_utils.add_prefixed_argument(parser, "routing_tables", prefix=prefix, |
| 27 | + help="Path to routing tables pickle", |
| 28 | + type=Routing_tables.from_config_file, |
| 29 | + required=required) |
| 30 | + |
| 31 | +def parse_args(args : argparse.Namespace, |
| 32 | + prefix : str = "", |
| 33 | + ) -> dict: |
| 34 | + out = {} |
| 35 | + |
| 36 | + if "routing_tables" not in out.keys(): |
| 37 | + out |= arg_utils.parse_args_automatically(args, ["routing_tables"], prefix) |
| 38 | + |
| 39 | + return out |
| 40 | + |
| 41 | + |
| 42 | +def load_graph(filename): |
| 43 | + assert(filename[-4:] == ".tgf") |
| 44 | + |
| 45 | + with open(filename, 'rt') as graph_file: |
| 46 | + lines = graph_file.readlines() |
| 47 | + |
| 48 | + graph = nx.DiGraph() |
| 49 | + |
| 50 | + re_line = re.compile(r"(\d+) (\d+) Send (\d+) Receive (\d+)") |
| 51 | + re_node = re.compile(r"(\d+) ([a-zA-Z0-9]+)") |
| 52 | + edge = [0] * 2 |
| 53 | + |
| 54 | + for line in lines: |
| 55 | + line_match = re_line.match(line) |
| 56 | + node_match = re_node.match(line) |
| 57 | + if line_match is not None: |
| 58 | + edge[0], edge[1], send, recieve = line_match.groups() |
| 59 | + graph.add_edge(int(edge[0]), int(edge[1]), send=int(send), recieve=int(recieve)) |
| 60 | + elif node_match is not None: |
| 61 | + graph.add_node(int(node_match.groups()[0])) |
| 62 | + else: |
| 63 | + assert line.strip() == '' |
| 64 | + |
| 65 | + return graph |
| 66 | + |
| 67 | +def routing_tables_from_graph(graph, rse_id, num_nodes): |
| 68 | + send_table = [0] * num_nodes |
| 69 | + recieve_table = [0] * num_nodes |
| 70 | + |
| 71 | + for destination in range(num_nodes): |
| 72 | + if destination is rse_id: |
| 73 | + continue |
| 74 | + |
| 75 | + logger.info("Finding path from {} to {}".format(rse_id, destination)) |
| 76 | + path = nx.shortest_path(graph, rse_id, destination) |
| 77 | + nexthop = path[1] |
| 78 | + send_table[destination] = graph[rse_id][nexthop]['send'] |
| 79 | + recieve_table[destination] = graph[rse_id][nexthop]['recieve'] |
| 80 | + |
| 81 | + send_table_bytes = bytes(0) |
| 82 | + for table_entry in send_table: |
| 83 | + send_table_bytes += table_entry.to_bytes(1, byteorder='little') |
| 84 | + |
| 85 | + recieve_table_bytes = bytes(0) |
| 86 | + for table_entry in recieve_table: |
| 87 | + recieve_table_bytes += table_entry.to_bytes(1, byteorder='little') |
| 88 | + |
| 89 | + return send_table_bytes, recieve_table_bytes |
| 90 | + |
| 91 | +class Routing_tables: |
| 92 | + def __init__(self, routing_tables): |
| 93 | + self.routing_tables = routing_tables |
| 94 | + |
| 95 | + @staticmethod |
| 96 | + def from_h_file(h_file_path, includes, defines): |
| 97 | + routing_tables_struct = c_struct.C_struct.from_h_file(h_file_path, |
| 98 | + "rse_whole_system_routing_tables_t", |
| 99 | + includes, defines) |
| 100 | + |
| 101 | + return Routing_tables(routing_tables_struct) |
| 102 | + |
| 103 | + @staticmethod |
| 104 | + def from_config_file(file_path): |
| 105 | + with open(file_path, "rb") as f: |
| 106 | + return pickle.load(f) |
| 107 | + |
| 108 | + def get_number_rses(self): |
| 109 | + return self.routing_tables.routing_table.get_size() // self.routing_tables.routing_table[0].get_size() |
| 110 | + |
| 111 | + def get_routing_tables_source(self): |
| 112 | + return self.routing_tables.get_value_str() |
| 113 | + |
| 114 | + def get_routing_tables_bytes(self): |
| 115 | + return self.routing_tables.to_bytes() |
| 116 | + |
| 117 | + def set_routing_tables_bytes(self, value): |
| 118 | + self.routing_tables.set_value_from_bytes(value) |
| 119 | + |
| 120 | + def get_rse_routing_table_bytes(self, rse_id): |
| 121 | + return self.routing_tables.routing_table[rse_id].to_bytes() |
| 122 | + |
| 123 | + def set_rse_routing_table_bytes(self, rse_id, send, receive): |
| 124 | + self.routing_tables.routing_table[rse_id].send.set_value_from_bytes(send) |
| 125 | + self.routing_tables.routing_table[rse_id].receive.set_value_from_bytes(receive) |
| 126 | + |
| 127 | + def to_config_file(self, file_path): |
| 128 | + with open(file_path, "wb") as f: |
| 129 | + pickle.dump(self, f) |
| 130 | + |
| 131 | +script_description = """ |
| 132 | +This script takes an input of the header file containing the RSE routing tables |
| 133 | +definition and uses the TGF file definition to populate this structure with the |
| 134 | +defined routing tables. This structure is then output to a pickle file where it |
| 135 | +can be used by other scripts (such as the OTP config generation script) |
| 136 | +""" |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + parser = argparse.ArgumentParser(allow_abbrev=False, |
| 140 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 141 | + description=script_description) |
| 142 | + parser.add_argument("--rse_routing_tables_h_file", help="path to rse_routing_tables.h", type=arg_utils.arg_type_filepath, required=True) |
| 143 | + parser.add_argument("--compile_commands_file", help="path to compile_commands.json", type=arg_utils.arg_type_filepath, required=True) |
| 144 | + parser.add_argument("--topology_graph_file", help="The topology graph file expressing the system", type=arg_utils.arg_type_filepath, required=True) |
| 145 | + parser.add_argument("--routing_tables_output_file", help="file to output routing tables to", required=True) |
| 146 | + parser.add_argument("--log_level", help="log level", required=False, default="ERROR", choices=logging._levelToName.values())# |
| 147 | + |
| 148 | + args = parser.parse_args() |
| 149 | + logging.getLogger("TF-M").setLevel(args.log_level) |
| 150 | + logger.addHandler(logging.StreamHandler()) |
| 151 | + |
| 152 | + includes = c_include.get_includes(args.compile_commands_file, "rse_handshake.c") |
| 153 | + defines = c_include.get_defines(args.compile_commands_file, "rse_handshake.c") |
| 154 | + |
| 155 | + routing_tables = Routing_tables.from_h_file(args.rse_routing_tables_h_file, includes, defines) |
| 156 | + |
| 157 | + graph = load_graph(args.topology_graph_file) |
| 158 | + number_rses = routing_tables.get_number_rses() |
| 159 | + number_nodes = graph.number_of_nodes() |
| 160 | + # Assumes that RSE nodes are defined before any other components |
| 161 | + for rse_id in range(number_rses): |
| 162 | + send_table_bytes, receive_table_bytes = routing_tables_from_graph(graph, rse_id, number_nodes) |
| 163 | + routing_tables.set_rse_routing_table_bytes(rse_id, send_table_bytes, receive_table_bytes) |
| 164 | + |
| 165 | + routing_tables.to_config_file(args.routing_tables_output_file) |
0 commit comments