From b5b7b8cdf9ac4fd2eeb563661700cb0670053954 Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Wed, 12 Oct 2022 14:04:48 -0700 Subject: [PATCH] grpc: updates to fix throughput parsing, accounting for new connection changes, now supports throughput for network to network links --- daemon/core/api/grpc/grpcutils.py | 32 +++++++++++++++++++------------ daemon/core/api/grpc/server.py | 6 ++---- daemon/core/gui/coreclient.py | 10 ++++------ daemon/core/nodes/base.py | 5 +++-- daemon/core/scripts/cleanup.py | 1 + 5 files changed, 30 insertions(+), 24 deletions(-) diff --git a/daemon/core/api/grpc/grpcutils.py b/daemon/core/api/grpc/grpcutils.py index d26c0bd9..5d8d7ceb 100644 --- a/daemon/core/api/grpc/grpcutils.py +++ b/daemon/core/api/grpc/grpcutils.py @@ -581,6 +581,24 @@ def convert_link( ) +def parse_proc_net_dev(lines: List[str]) -> Dict[str, Any]: + """ + Parse lines of output from /proc/net/dev. + + :param lines: lines of /proc/net/dev + :return: parsed device to tx/rx values + """ + stats = {} + for line in lines[2:]: + line = line.strip() + if not line: + continue + line = line.split() + line[0] = line[0].strip(":") + stats[line[0]] = {"rx": float(line[1]), "tx": float(line[9])} + return stats + + def get_net_stats() -> Dict[str, Dict]: """ Retrieve status about the current interfaces in the system @@ -588,18 +606,8 @@ def get_net_stats() -> Dict[str, Dict]: :return: send and receive status of the interfaces in the system """ with open("/proc/net/dev", "r") as f: - data = f.readlines()[2:] - - stats = {} - for line in data: - line = line.strip() - if not line: - continue - line = line.split() - line[0] = line[0].strip(":") - stats[line[0]] = {"rx": float(line[1]), "tx": float(line[9])} - - return stats + lines = f.readlines()[2:] + return parse_proc_net_dev(lines) def session_location(session: Session, location: core_pb2.SessionLocation) -> None: diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index 4d199ef6..3815d6c9 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -104,7 +104,7 @@ from core.services.coreservices import ServiceManager logger = logging.getLogger(__name__) _ONE_DAY_IN_SECONDS: int = 60 * 60 * 24 -_INTERFACE_REGEX: Pattern = re.compile(r"veth(?P[0-9a-fA-F]+)") +_INTERFACE_REGEX: Pattern = re.compile(r"beth(?P[0-9a-fA-F]+)") _MAX_WORKERS = 1000 @@ -482,7 +482,6 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): while self._is_running(context): now = time.monotonic() stats = get_net_stats() - # calculate average if last_check is not None: interval = now - last_check @@ -499,7 +498,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): (current_rxtx["tx"] - previous_rxtx["tx"]) * 8.0 / interval ) throughput = rx_kbps + tx_kbps - if key.startswith("veth"): + if key.startswith("beth"): key = key.split(".") node_id = _INTERFACE_REGEX.search(key[0]).group("node") node_id = int(node_id, base=16) @@ -525,7 +524,6 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): bridge_throughput.throughput = throughput except ValueError: pass - yield throughputs_event last_check = now diff --git a/daemon/core/gui/coreclient.py b/daemon/core/gui/coreclient.py index aab9cdb9..1b13def5 100644 --- a/daemon/core/gui/coreclient.py +++ b/daemon/core/gui/coreclient.py @@ -667,12 +667,10 @@ class CoreClient: self.links[edge.token] = edge src_node = edge.src.core_node dst_node = edge.dst.core_node - if nutils.is_container(src_node): - src_iface_id = edge.link.iface1.id - self.iface_to_edge[(src_node.id, src_iface_id)] = edge - if nutils.is_container(dst_node): - dst_iface_id = edge.link.iface2.id - self.iface_to_edge[(dst_node.id, dst_iface_id)] = edge + src_iface_id = edge.link.iface1.id + self.iface_to_edge[(src_node.id, src_iface_id)] = edge + dst_iface_id = edge.link.iface2.id + self.iface_to_edge[(dst_node.id, dst_iface_id)] = edge def get_wlan_configs(self) -> List[Tuple[int, Dict[str, str]]]: configs = [] diff --git a/daemon/core/nodes/base.py b/daemon/core/nodes/base.py index 7edbc198..d3adf5c3 100644 --- a/daemon/core/nodes/base.py +++ b/daemon/core/nodes/base.py @@ -276,8 +276,9 @@ class NodeBase(abc.ABC): mtu = DEFAULT_MTU if iface_data and iface_data.mtu is not None: mtu = iface_data.mtu - name = f"veth{self.id}.{iface_id}.{self.session.short_session_id()}" - localname = f"{name}p" + unique_name = f"{self.id}.{iface_id}.{self.session.short_session_id()}" + name = f"veth{unique_name}" + localname = f"beth{unique_name}" iface = CoreInterface( iface_id, name, diff --git a/daemon/core/scripts/cleanup.py b/daemon/core/scripts/cleanup.py index 080cd62b..3606fc13 100755 --- a/daemon/core/scripts/cleanup.py +++ b/daemon/core/scripts/cleanup.py @@ -68,6 +68,7 @@ def cleanup_interfaces() -> None: name = values[0] if ( name.startswith("veth") + or name.startswith("beth") or name.startswith("gt.") or name.startswith("b.") or name.startswith("ctrl")