1# SPDX-License-Identifier: BSD-3-Clause 2# Copyright(c) 2010-2014 Intel Corporation 3# Copyright(c) 2022-2023 PANTHEON.tech s.r.o. 4# Copyright(c) 2022-2023 University of New Hampshire 5# Copyright(c) 2024 Arm Limited 6 7"""Common functionality for node management. 8 9A node is any host/server DTS connects to. 10 11The base class, :class:`Node`, provides features common to all nodes and is supposed 12to be extended by subclasses with features specific to each node type. 13The :func:`~Node.skip_setup` decorator can be used without subclassing. 14""" 15 16from abc import ABC 17from ipaddress import IPv4Interface, IPv6Interface 18from typing import Union 19 20from framework.config import ( 21 OS, 22 DPDKBuildConfiguration, 23 NodeConfiguration, 24 TestRunConfiguration, 25) 26from framework.exception import ConfigurationError 27from framework.logger import DTSLogger, get_dts_logger 28 29from .cpu import ( 30 LogicalCore, 31 LogicalCoreCount, 32 LogicalCoreList, 33 LogicalCoreListFilter, 34 lcore_filter, 35) 36from .linux_session import LinuxSession 37from .os_session import OSSession 38from .port import Port 39 40 41class Node(ABC): 42 """The base class for node management. 43 44 It shouldn't be instantiated, but rather subclassed. 45 It implements common methods to manage any node: 46 47 * Connection to the node, 48 * Hugepages setup. 49 50 Attributes: 51 main_session: The primary OS-aware remote session used to communicate with the node. 52 config: The node configuration. 53 name: The name of the node. 54 lcores: The list of logical cores that DTS can use on the node. 55 It's derived from logical cores present on the node and the test run configuration. 56 ports: The ports of this node specified in the test run configuration. 57 """ 58 59 main_session: OSSession 60 config: NodeConfiguration 61 name: str 62 lcores: list[LogicalCore] 63 ports: list[Port] 64 _logger: DTSLogger 65 _other_sessions: list[OSSession] 66 _test_run_config: TestRunConfiguration 67 68 def __init__(self, node_config: NodeConfiguration): 69 """Connect to the node and gather info during initialization. 70 71 Extra gathered information: 72 73 * The list of available logical CPUs. This is then filtered by 74 the ``lcores`` configuration in the YAML test run configuration file, 75 * Information about ports from the YAML test run configuration file. 76 77 Args: 78 node_config: The node's test run configuration. 79 """ 80 self.config = node_config 81 self.name = node_config.name 82 self._logger = get_dts_logger(self.name) 83 self.main_session = create_session(self.config, self.name, self._logger) 84 85 self._logger.info(f"Connected to node: {self.name}") 86 87 self._get_remote_cpus() 88 # filter the node lcores according to the test run configuration 89 self.lcores = LogicalCoreListFilter( 90 self.lcores, LogicalCoreList(self.config.lcores) 91 ).filter() 92 93 self._other_sessions = [] 94 self._init_ports() 95 96 def _init_ports(self) -> None: 97 self.ports = [Port(self.name, port_config) for port_config in self.config.ports] 98 self.main_session.update_ports(self.ports) 99 100 def set_up_test_run( 101 self, 102 test_run_config: TestRunConfiguration, 103 dpdk_build_config: DPDKBuildConfiguration, 104 ) -> None: 105 """Test run setup steps. 106 107 Configure hugepages on all DTS node types. Additional steps can be added by 108 extending the method in subclasses with the use of super(). 109 110 Args: 111 test_run_config: A test run configuration according to which 112 the setup steps will be taken. 113 dpdk_build_config: The build configuration of DPDK. 114 """ 115 self._setup_hugepages() 116 117 def tear_down_test_run(self) -> None: 118 """Test run teardown steps. 119 120 There are currently no common execution teardown steps common to all DTS node types. 121 Additional steps can be added by extending the method in subclasses with the use of super(). 122 """ 123 124 def create_session(self, name: str) -> OSSession: 125 """Create and return a new OS-aware remote session. 126 127 The returned session won't be used by the node creating it. The session must be used by 128 the caller. The session will be maintained for the entire lifecycle of the node object, 129 at the end of which the session will be cleaned up automatically. 130 131 Note: 132 Any number of these supplementary sessions may be created. 133 134 Args: 135 name: The name of the session. 136 137 Returns: 138 A new OS-aware remote session. 139 """ 140 session_name = f"{self.name} {name}" 141 connection = create_session( 142 self.config, 143 session_name, 144 get_dts_logger(session_name), 145 ) 146 self._other_sessions.append(connection) 147 return connection 148 149 def filter_lcores( 150 self, 151 filter_specifier: LogicalCoreCount | LogicalCoreList, 152 ascending: bool = True, 153 ) -> list[LogicalCore]: 154 """Filter the node's logical cores that DTS can use. 155 156 Logical cores that DTS can use are the ones that are present on the node, but filtered 157 according to the test run configuration. The `filter_specifier` will filter cores from 158 those logical cores. 159 160 Args: 161 filter_specifier: Two different filters can be used, one that specifies the number 162 of logical cores per core, cores per socket and the number of sockets, 163 and another one that specifies a logical core list. 164 ascending: If :data:`True`, use cores with the lowest numerical id first and continue 165 in ascending order. If :data:`False`, start with the highest id and continue 166 in descending order. This ordering affects which sockets to consider first as well. 167 168 Returns: 169 The filtered logical cores. 170 """ 171 self._logger.debug(f"Filtering {filter_specifier} from {self.lcores}.") 172 return lcore_filter( 173 self.lcores, 174 filter_specifier, 175 ascending, 176 ).filter() 177 178 def _get_remote_cpus(self) -> None: 179 """Scan CPUs in the remote OS and store a list of LogicalCores.""" 180 self._logger.info("Getting CPU information.") 181 self.lcores = self.main_session.get_remote_cpus(self.config.use_first_core) 182 183 def _setup_hugepages(self) -> None: 184 """Setup hugepages on the node. 185 186 Configure the hugepages only if they're specified in the node's test run configuration. 187 """ 188 if self.config.hugepages: 189 self.main_session.setup_hugepages( 190 self.config.hugepages.number_of, 191 self.main_session.hugepage_size, 192 self.config.hugepages.force_first_numa, 193 ) 194 195 def configure_port_state(self, port: Port, enable: bool = True) -> None: 196 """Enable/disable `port`. 197 198 Args: 199 port: The port to enable/disable. 200 enable: :data:`True` to enable, :data:`False` to disable. 201 """ 202 self.main_session.configure_port_state(port, enable) 203 204 def configure_port_ip_address( 205 self, 206 address: Union[IPv4Interface, IPv6Interface], 207 port: Port, 208 delete: bool = False, 209 ) -> None: 210 """Add an IP address to `port` on this node. 211 212 Args: 213 address: The IP address with mask in CIDR format. Can be either IPv4 or IPv6. 214 port: The port to which to add the address. 215 delete: If :data:`True`, will delete the address from the port instead of adding it. 216 """ 217 self.main_session.configure_port_ip_address(address, port, delete) 218 219 def close(self) -> None: 220 """Close all connections and free other resources.""" 221 if self.main_session: 222 self.main_session.close() 223 for session in self._other_sessions: 224 session.close() 225 226 227def create_session(node_config: NodeConfiguration, name: str, logger: DTSLogger) -> OSSession: 228 """Factory for OS-aware sessions. 229 230 Args: 231 node_config: The test run configuration of the node to connect to. 232 name: The name of the session. 233 logger: The logger instance this session will use. 234 """ 235 match node_config.os: 236 case OS.linux: 237 return LinuxSession(node_config, name, logger) 238 case _: 239 raise ConfigurationError(f"Unsupported OS {node_config.os}") 240