1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
# SPDX-License-Identifier: BSD-3-Clause
# Copyright(c) 2010-2014 Intel Corporation
# Copyright(c) 2023 PANTHEON.tech s.r.o.
# Copyright(c) 2023 University of New Hampshire
# Copyright(c) 2024 Arm Limited
"""System under test (DPDK + hardware) node.
A system under test (SUT) is the combination of DPDK
and the hardware we're testing with DPDK (NICs, crypto and other devices).
An SUT node is where this SUT runs.
"""
import os
import time
from dataclasses import dataclass
from pathlib import Path, PurePath
from framework.config import (
DPDKBuildConfiguration,
DPDKBuildOptionsConfiguration,
DPDKPrecompiledBuildConfiguration,
DPDKUncompiledBuildConfiguration,
LocalDPDKTarballLocation,
LocalDPDKTreeLocation,
RemoteDPDKTarballLocation,
RemoteDPDKTreeLocation,
SutNodeConfiguration,
TestRunConfiguration,
)
from framework.exception import ConfigurationError, RemoteFileNotFoundError
from framework.params.eal import EalParams
from framework.remote_session.remote_session import CommandResult
from framework.utils import MesonArgs, TarCompressionFormat
from .node import Node
from .os_session import OSSession, OSSessionInfo
from .virtual_device import VirtualDevice
@dataclass(slots=True, frozen=True)
class DPDKBuildInfo:
"""Various versions and other information about a DPDK build.
Attributes:
dpdk_version: The DPDK version that was built.
compiler_version: The version of the compiler used to build DPDK.
"""
dpdk_version: str | None
compiler_version: str | None
class SutNode(Node):
"""The system under test node.
The SUT node extends :class:`Node` with DPDK specific features:
* Managing DPDK source tree on the remote SUT,
* Building the DPDK from source or using a pre-built version,
* Gathering of DPDK build info,
* The running of DPDK apps, interactively or one-time execution,
* DPDK apps cleanup.
Building DPDK from source uses `build` configuration inside `dpdk_build` of configuration.
Attributes:
config: The SUT node configuration.
virtual_devices: The virtual devices used on the node.
"""
config: SutNodeConfiguration
virtual_devices: list[VirtualDevice]
dpdk_prefix_list: list[str]
dpdk_timestamp: str
_env_vars: dict
_remote_tmp_dir: PurePath
__remote_dpdk_tree_path: str | PurePath | None
_remote_dpdk_build_dir: PurePath | None
_app_compile_timeout: float
_dpdk_kill_session: OSSession | None
_dpdk_version: str | None
_node_info: OSSessionInfo | None
_compiler_version: str | None
_path_to_devbind_script: PurePath | None
_ports_bound_to_dpdk: bool
def __init__(self, node_config: SutNodeConfiguration):
"""Extend the constructor with SUT node specifics.
Args:
node_config: The SUT node's test run configuration.
"""
super().__init__(node_config)
self.virtual_devices = []
self.dpdk_prefix_list = []
self._env_vars = {}
self._remote_tmp_dir = self.main_session.get_remote_tmp_dir()
self.__remote_dpdk_tree_path = None
self._remote_dpdk_build_dir = None
self._app_compile_timeout = 90
self._dpdk_kill_session = None
self.dpdk_timestamp = (
f"{str(os.getpid())}_{time.strftime('%Y%m%d%H%M%S', time.localtime())}"
)
self._dpdk_version = None
self._node_info = None
self._compiler_version = None
self._path_to_devbind_script = None
self._ports_bound_to_dpdk = False
self._logger.info(f"Created node: {self.name}")
@property
def _remote_dpdk_tree_path(self) -> str | PurePath:
"""The remote DPDK tree path."""
if self.__remote_dpdk_tree_path:
return self.__remote_dpdk_tree_path
self._logger.warning(
"Failed to get remote dpdk tree path because we don't know the "
"location on the SUT node."
)
return ""
@property
def remote_dpdk_build_dir(self) -> str | PurePath:
"""The remote DPDK build dir path."""
if self._remote_dpdk_build_dir:
return self._remote_dpdk_build_dir
self._logger.warning(
"Failed to get remote dpdk build dir because we don't know the "
"location on the SUT node."
)
return ""
@property
def dpdk_version(self) -> str | None:
"""Last built DPDK version."""
if self._dpdk_version is None:
self._dpdk_version = self.main_session.get_dpdk_version(self._remote_dpdk_tree_path)
return self._dpdk_version
@property
def node_info(self) -> OSSessionInfo:
"""Additional node information."""
if self._node_info is None:
self._node_info = self.main_session.get_node_info()
return self._node_info
@property
def compiler_version(self) -> str | None:
"""The node's compiler version."""
if self._compiler_version is None:
self._logger.warning("The `compiler_version` is None because a pre-built DPDK is used.")
return self._compiler_version
@compiler_version.setter
def compiler_version(self, value: str) -> None:
"""Set the `compiler_version` used on the SUT node.
Args:
value: The node's compiler version.
"""
self._compiler_version = value
@property
def path_to_devbind_script(self) -> PurePath | str:
"""The path to the dpdk-devbind.py script on the node."""
if self._path_to_devbind_script is None:
self._path_to_devbind_script = self.main_session.join_remote_path(
self._remote_dpdk_tree_path, "usertools", "dpdk-devbind.py"
)
return self._path_to_devbind_script
def get_dpdk_build_info(self) -> DPDKBuildInfo:
"""Get additional DPDK build information.
Returns:
The DPDK build information,
"""
return DPDKBuildInfo(dpdk_version=self.dpdk_version, compiler_version=self.compiler_version)
def set_up_test_run(
self,
test_run_config: TestRunConfiguration,
dpdk_build_config: DPDKBuildConfiguration,
) -> None:
"""Extend the test run setup with vdev config and DPDK build set up.
This method extends the setup process by configuring virtual devices and preparing the DPDK
environment based on the provided configuration.
Args:
test_run_config: A test run configuration according to which
the setup steps will be taken.
dpdk_build_config: The build configuration of DPDK.
"""
super().set_up_test_run(test_run_config, dpdk_build_config)
for vdev in test_run_config.system_under_test_node.vdevs:
self.virtual_devices.append(VirtualDevice(vdev))
self._set_up_dpdk(dpdk_build_config)
def tear_down_test_run(self) -> None:
"""Extend the test run teardown with virtual device teardown and DPDK teardown."""
super().tear_down_test_run()
self.virtual_devices = []
self._tear_down_dpdk()
def _set_up_dpdk(
self,
dpdk_build_config: DPDKBuildConfiguration,
) -> None:
"""Set up DPDK the SUT node and bind ports.
DPDK setup includes setting all internals needed for the build, the copying of DPDK
sources and then building DPDK or using the exist ones from the `dpdk_location`. The drivers
are bound to those that DPDK needs.
Args:
dpdk_build_config: A DPDK build configuration to test.
"""
match dpdk_build_config.dpdk_location:
case RemoteDPDKTreeLocation(dpdk_tree=dpdk_tree):
self._set_remote_dpdk_tree_path(dpdk_tree)
case LocalDPDKTreeLocation(dpdk_tree=dpdk_tree):
self._copy_dpdk_tree(dpdk_tree)
case RemoteDPDKTarballLocation(tarball=tarball):
self._validate_remote_dpdk_tarball(tarball)
self._prepare_and_extract_dpdk_tarball(tarball)
case LocalDPDKTarballLocation(tarball=tarball):
remote_tarball = self._copy_dpdk_tarball_to_remote(tarball)
self._prepare_and_extract_dpdk_tarball(remote_tarball)
match dpdk_build_config:
case DPDKPrecompiledBuildConfiguration(precompiled_build_dir=build_dir):
self._set_remote_dpdk_build_dir(build_dir)
case DPDKUncompiledBuildConfiguration(build_options=build_options):
self._configure_dpdk_build(build_options)
self._build_dpdk()
self.bind_ports_to_driver()
def _tear_down_dpdk(self) -> None:
"""Reset DPDK variables and bind port driver to the OS driver."""
self._env_vars = {}
self.__remote_dpdk_tree_path = None
self._remote_dpdk_build_dir = None
self._dpdk_version = None
self.compiler_version = None
self.bind_ports_to_driver(for_dpdk=False)
def _set_remote_dpdk_tree_path(self, dpdk_tree: PurePath):
"""Set the path to the remote DPDK source tree based on the provided DPDK location.
Verify DPDK source tree existence on the SUT node, if exists sets the
`_remote_dpdk_tree_path` property, otherwise sets nothing.
Args:
dpdk_tree: The path to the DPDK source tree directory.
Raises:
RemoteFileNotFoundError: If the DPDK source tree is expected to be on the SUT node but
is not found.
"""
if not self.main_session.remote_path_exists(dpdk_tree):
raise RemoteFileNotFoundError(
f"Remote DPDK source tree '{dpdk_tree}' not found in SUT node."
)
if not self.main_session.is_remote_dir(dpdk_tree):
raise ConfigurationError(f"Remote DPDK source tree '{dpdk_tree}' must be a directory.")
self.__remote_dpdk_tree_path = dpdk_tree
def _copy_dpdk_tree(self, dpdk_tree_path: Path) -> None:
"""Copy the DPDK source tree to the SUT.
Args:
dpdk_tree_path: The path to DPDK source tree on local filesystem.
"""
self._logger.info(
f"Copying DPDK source tree to SUT: '{dpdk_tree_path}' into '{self._remote_tmp_dir}'."
)
self.main_session.copy_dir_to(
dpdk_tree_path,
self._remote_tmp_dir,
exclude=[".git", "*.o"],
compress_format=TarCompressionFormat.gzip,
)
self.__remote_dpdk_tree_path = self.main_session.join_remote_path(
self._remote_tmp_dir, PurePath(dpdk_tree_path).name
)
def _validate_remote_dpdk_tarball(self, dpdk_tarball: PurePath) -> None:
"""Validate the DPDK tarball on the SUT node.
Args:
dpdk_tarball: The path to the DPDK tarball on the SUT node.
Raises:
RemoteFileNotFoundError: If the `dpdk_tarball` is expected to be on the SUT node but is
not found.
ConfigurationError: If the `dpdk_tarball` is a valid path but not a valid tar archive.
"""
if not self.main_session.remote_path_exists(dpdk_tarball):
raise RemoteFileNotFoundError(f"Remote DPDK tarball '{dpdk_tarball}' not found in SUT.")
if not self.main_session.is_remote_tarfile(dpdk_tarball):
raise ConfigurationError(f"Remote DPDK tarball '{dpdk_tarball}' must be a tar archive.")
def _copy_dpdk_tarball_to_remote(self, dpdk_tarball: Path) -> PurePath:
"""Copy the local DPDK tarball to the SUT node.
Args:
dpdk_tarball: The local path to the DPDK tarball.
Returns:
The path of the copied tarball on the SUT node.
"""
self._logger.info(
f"Copying DPDK tarball to SUT: '{dpdk_tarball}' into '{self._remote_tmp_dir}'."
)
self.main_session.copy_to(dpdk_tarball, self._remote_tmp_dir)
return self.main_session.join_remote_path(self._remote_tmp_dir, dpdk_tarball.name)
def _prepare_and_extract_dpdk_tarball(self, remote_tarball_path: PurePath) -> None:
"""Prepare the remote DPDK tree path and extract the tarball.
This method extracts the remote tarball and sets the `_remote_dpdk_tree_path` property to
the path of the extracted DPDK tree on the SUT node.
Args:
remote_tarball_path: The path to the DPDK tarball on the SUT node.
"""
def remove_tarball_suffix(remote_tarball_path: PurePath) -> PurePath:
"""Remove the tarball suffix from the path.
Args:
remote_tarball_path: The path to the remote tarball.
Returns:
The path without the tarball suffix.
"""
if len(remote_tarball_path.suffixes) > 1:
if remote_tarball_path.suffixes[-2] == ".tar":
suffixes_to_remove = "".join(remote_tarball_path.suffixes[-2:])
return PurePath(str(remote_tarball_path).replace(suffixes_to_remove, ""))
return remote_tarball_path.with_suffix("")
tarball_top_dir = self.main_session.get_tarball_top_dir(remote_tarball_path)
self.__remote_dpdk_tree_path = self.main_session.join_remote_path(
remote_tarball_path.parent,
tarball_top_dir or remove_tarball_suffix(remote_tarball_path),
)
self._logger.info(
"Extracting DPDK tarball on SUT: "
f"'{remote_tarball_path}' into '{self._remote_dpdk_tree_path}'."
)
self.main_session.extract_remote_tarball(
remote_tarball_path,
self._remote_dpdk_tree_path,
)
def _set_remote_dpdk_build_dir(self, build_dir: str):
"""Set the `remote_dpdk_build_dir` on the SUT.
Check existence on the SUT node and sets the
`remote_dpdk_build_dir` property by joining the `_remote_dpdk_tree_path` and `build_dir`.
Otherwise, sets nothing.
Args:
build_dir: DPDK has been pre-built and the build directory is located
in a subdirectory of `dpdk_tree` or `tarball` root directory.
Raises:
RemoteFileNotFoundError: If the `build_dir` is expected but does not exist on the SUT
node.
"""
remote_dpdk_build_dir = self.main_session.join_remote_path(
self._remote_dpdk_tree_path, build_dir
)
if not self.main_session.remote_path_exists(remote_dpdk_build_dir):
raise RemoteFileNotFoundError(
f"Remote DPDK build dir '{remote_dpdk_build_dir}' not found in SUT node."
)
self._remote_dpdk_build_dir = PurePath(remote_dpdk_build_dir)
def _configure_dpdk_build(self, dpdk_build_config: DPDKBuildOptionsConfiguration) -> None:
"""Populate common environment variables and set the DPDK build related properties.
This method sets `compiler_version` for additional information and `remote_dpdk_build_dir`
from DPDK build config name.
Args:
dpdk_build_config: A DPDK build configuration to test.
"""
self._env_vars = {}
self._env_vars.update(self.main_session.get_dpdk_build_env_vars(dpdk_build_config.arch))
if compiler_wrapper := dpdk_build_config.compiler_wrapper:
self._env_vars["CC"] = f"'{compiler_wrapper} {dpdk_build_config.compiler.name}'"
else:
self._env_vars["CC"] = dpdk_build_config.compiler.name
self.compiler_version = self.main_session.get_compiler_version(
dpdk_build_config.compiler.name
)
self._remote_dpdk_build_dir = self.main_session.join_remote_path(
self._remote_dpdk_tree_path, dpdk_build_config.name
)
def _build_dpdk(self) -> None:
"""Build DPDK.
Uses the already configured DPDK build configuration. Assumes that the
`_remote_dpdk_tree_path` has already been set on the SUT node.
"""
self.main_session.build_dpdk(
self._env_vars,
MesonArgs(default_library="static", enable_kmods=True, libdir="lib"),
self._remote_dpdk_tree_path,
self.remote_dpdk_build_dir,
)
def build_dpdk_app(self, app_name: str, **meson_dpdk_args: str | bool) -> PurePath:
"""Build one or all DPDK apps.
Requires DPDK to be already built on the SUT node.
Args:
app_name: The name of the DPDK app to build.
When `app_name` is ``all``, build all example apps.
meson_dpdk_args: The arguments found in ``meson_options.txt`` in root DPDK directory.
Do not use ``-D`` with them.
Returns:
The directory path of the built app. If building all apps, return
the path to the examples directory (where all apps reside).
"""
self.main_session.build_dpdk(
self._env_vars,
MesonArgs(examples=app_name, **meson_dpdk_args), # type: ignore [arg-type]
# ^^ https://github.com/python/mypy/issues/11583
self._remote_dpdk_tree_path,
self.remote_dpdk_build_dir,
rebuild=True,
timeout=self._app_compile_timeout,
)
if app_name == "all":
return self.main_session.join_remote_path(self.remote_dpdk_build_dir, "examples")
return self.main_session.join_remote_path(
self.remote_dpdk_build_dir, "examples", f"dpdk-{app_name}"
)
def kill_cleanup_dpdk_apps(self) -> None:
"""Kill all dpdk applications on the SUT, then clean up hugepages."""
if self._dpdk_kill_session and self._dpdk_kill_session.is_alive():
# we can use the session if it exists and responds
self._dpdk_kill_session.kill_cleanup_dpdk_apps(self.dpdk_prefix_list)
else:
# otherwise, we need to (re)create it
self._dpdk_kill_session = self.create_session("dpdk_kill")
self.dpdk_prefix_list = []
def run_dpdk_app(
self, app_path: PurePath, eal_params: EalParams, timeout: float = 30
) -> CommandResult:
"""Run DPDK application on the remote node.
The application is not run interactively - the command that starts the application
is executed and then the call waits for it to finish execution.
Args:
app_path: The remote path to the DPDK application.
eal_params: EAL parameters to run the DPDK application with.
timeout: Wait at most this long in seconds for `command` execution to complete.
Returns:
The result of the DPDK app execution.
"""
return self.main_session.send_command(
f"{app_path} {eal_params}", timeout, privileged=True, verify=True
)
def configure_ipv4_forwarding(self, enable: bool) -> None:
"""Enable/disable IPv4 forwarding on the node.
Args:
enable: If :data:`True`, enable the forwarding, otherwise disable it.
"""
self.main_session.configure_ipv4_forwarding(enable)
def bind_ports_to_driver(self, for_dpdk: bool = True) -> None:
"""Bind all ports on the SUT to a driver.
Args:
for_dpdk: If :data:`True`, binds ports to os_driver_for_dpdk.
If :data:`False`, binds to os_driver.
"""
if self._ports_bound_to_dpdk == for_dpdk:
return
for port in self.ports:
driver = port.os_driver_for_dpdk if for_dpdk else port.os_driver
self.main_session.send_command(
f"{self.path_to_devbind_script} -b {driver} --force {port.pci}",
privileged=True,
verify=True,
)
self._ports_bound_to_dpdk = for_dpdk
|