From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 18B1B45A11; Mon, 23 Sep 2024 17:03:15 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8940C40611; Mon, 23 Sep 2024 17:02:24 +0200 (CEST) Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) by mails.dpdk.org (Postfix) with ESMTP id EC35A402CB for ; Mon, 23 Sep 2024 17:02:21 +0200 (CEST) Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a8d6ac24a3bso51713966b.1 for ; Mon, 23 Sep 2024 08:02:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pantheon.tech; s=google; t=1727103741; x=1727708541; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yg28yBVVBmK5qESwuD1M+H3/EMTOGQJsDDucU0hyCaY=; b=j44k6QUlaHmsOVnYWLpv21Nkcw9QoPiwqPNgS+uhjvnJc4u6scNGob6/AL1F/fUmMx Q1c2v6lcMVVdOGAewetDAK7VSqg0EI/XpxOBD6i7wHqjrpDEvjKGLaoW0hRxrijhHx/g oKZuoV4AUu+x2XznkP3qNKOv8YTePbXgoIlWbebTOuxNuaAPK94Q4YivEQHJgIBeK02I 5jJAZ67sd27dVzSG0xVlOvc/mIXM9EUE0nr78/QyyB51PM1Gg+gPi46IaQNQZzrU47A7 jRc8Ztpmu7ybIe121YDgYQF7TdK5WEFKtdRq5U5n/swwFAt5Yof5dQDUvpvKu3cn0Yxq BmOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727103741; x=1727708541; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yg28yBVVBmK5qESwuD1M+H3/EMTOGQJsDDucU0hyCaY=; b=qr4ulB9hCxwUKWcxIcYmLv3nL0SHXBJNiAh/W9eU2df1+SEn7nShd9i0XuAPNUKCCZ RKj9L88lECa9vl01dHzIw9t4RlDd2mjHFvgfMzlDEsqpyyeIMiwNkUcztS3w2zBjHAzH Q+P1sl7z4EVzUWuPDST1qyEENTzY51V1iUF7Xi1VzDMCH4bJEu9UgVgmBsana7/rsi26 oW//aYmL/YW9cfhXh0sF7j7W2Alf8C4ECLmonFQ7Lm6pezosA17AxcGCGfyRZBFp6eNw phoNSMfcjI4iKQrUhDG9Qo0el6V0yQj7ryRnJANqKNo1dEfS3ueOU6+2eJXbmeFj1+vW Z2qg== X-Gm-Message-State: AOJu0YzdIL31myDIrFTyqhjfaYUa8fcQ59opUMnlTMzlTv45uTxU6qWw 3HFOzFxYK8bEdGoCkILF5yFkWIhX0cEPh6k/t4r3H06hIeRf64UjJ8HgmUAU3fo= X-Google-Smtp-Source: AGHT+IHE6O6SYaCn7wvMnDb9DR4jD48GLWGeq9y/n4NO4RAygNGmHAIoq+Stj8YyL/Dc9eZELbckKw== X-Received: by 2002:a17:907:a43:b0:a8d:505a:5458 with SMTP id a640c23a62f3a-a90d3514408mr1174317066b.10.1727103741198; Mon, 23 Sep 2024 08:02:21 -0700 (PDT) Received: from jlinkes-PT-Latitude-5530.. ([84.245.121.62]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a90612b3ba7sm1235318866b.119.2024.09.23.08.02.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2024 08:02:20 -0700 (PDT) From: =?UTF-8?q?Juraj=20Linke=C5=A1?= To: thomas@monjalon.net, Honnappa.Nagarahalli@arm.com, paul.szczepanek@arm.com, Luca.Vizzarro@arm.com, alex.chapman@arm.com, probb@iol.unh.edu, jspewock@iol.unh.edu, npratte@iol.unh.edu, dmarx@iol.unh.edu Cc: dev@dpdk.org, =?UTF-8?q?Juraj=20Linke=C5=A1?= Subject: [PATCH v4 06/11] dts: add NIC capability support Date: Mon, 23 Sep 2024 17:02:05 +0200 Message-ID: <20240923150210.57269-6-juraj.linkes@pantheon.tech> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240923150210.57269-1-juraj.linkes@pantheon.tech> References: <20240301155416.96960-1-juraj.linkes@pantheon.tech> <20240923150210.57269-1-juraj.linkes@pantheon.tech> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Some test cases or suites may be testing a NIC feature that is not supported on all NICs, so add support for marking test cases or suites as requiring NIC capabilities. The marking is done with a decorator, which populates the internal required_capabilities attribute of TestProtocol. The NIC capability itself is a wrapper around the NicCapability defined in testpmd_shell. The reason is Enums cannot be extended and the class implements the methods of its abstract base superclass. The decorator API is designed to be simple to use. The arguments passed to it are all from the testpmd shell. Everything else (even the actual capability object creation) is done internally. Signed-off-by: Juraj Linkeš Reviewed-by: Dean Marx --- dts/framework/remote_session/testpmd_shell.py | 55 +++++- dts/framework/testbed_model/capability.py | 167 +++++++++++++++++- 2 files changed, 220 insertions(+), 2 deletions(-) diff --git a/dts/framework/remote_session/testpmd_shell.py b/dts/framework/remote_session/testpmd_shell.py index 77902a468d..3401adcc28 100644 --- a/dts/framework/remote_session/testpmd_shell.py +++ b/dts/framework/remote_session/testpmd_shell.py @@ -17,10 +17,16 @@ import functools import re import time +from collections.abc import Callable, MutableSet from dataclasses import dataclass, field from enum import Flag, auto from pathlib import PurePath -from typing import Any, Callable, ClassVar, Concatenate, ParamSpec +from typing import TYPE_CHECKING, Any, ClassVar, Concatenate, ParamSpec, TypeAlias + +if TYPE_CHECKING: + from enum import Enum as NoAliasEnum +else: + from aenum import NoAliasEnum from typing_extensions import Self, Unpack @@ -37,6 +43,12 @@ P = ParamSpec("P") TestPmdShellMethod = Callable[Concatenate["TestPmdShell", P], Any] +TestPmdShellCapabilityMethod: TypeAlias = Callable[ + ["TestPmdShell", MutableSet["NicCapability"], MutableSet["NicCapability"]], None +] + +TestPmdShellDecorator: TypeAlias = Callable[[TestPmdShellMethod], TestPmdShellMethod] + class TestPmdDevice: """The data of a device that testpmd can recognize. @@ -986,3 +998,44 @@ def _close(self) -> None: self.stop() self.send_command("quit", "Bye...") return super()._close() + + +class NicCapability(NoAliasEnum): + """A mapping between capability names and the associated :class:`TestPmdShell` methods. + + The :class:`TestPmdShell` capability checking method executes the command that checks + whether the capability is supported. + A decorator may optionally be added to the method that will add and remove configuration + that's necessary to retrieve the capability support status. + The Enum members may be assigned the method itself or a tuple of + (capability_checking_method, decorator_function). + + The signature of each :class:`TestPmdShell` capability checking method must be:: + + fn(self, supported_capabilities: MutableSet, unsupported_capabilities: MutableSet) -> None + + The capability checking method must get whether a capability is supported or not + from a testpmd command. If multiple capabilities can be obtained from a testpmd command, + each should be obtained in the method. These capabilities should then + be added to `supported_capabilities` or `unsupported_capabilities` based on their support. + + The two dictionaries are shared across all capability discovery function calls in a given + test run so that we don't call the same function multiple times. + """ + + def __call__( + self, + testpmd_shell: TestPmdShell, + supported_capabilities: MutableSet[Self], + unsupported_capabilities: MutableSet[Self], + ) -> None: + """Execute the associated capability retrieval function. + + Args: + testpmd_shell: :class:`TestPmdShell` object to which the function will be bound to. + supported_capabilities: The dictionary storing the supported capabilities + of a given test run. + unsupported_capabilities: The dictionary storing the unsupported capabilities + of a given test run. + """ + self.value(testpmd_shell, supported_capabilities, unsupported_capabilities) diff --git a/dts/framework/testbed_model/capability.py b/dts/framework/testbed_model/capability.py index 8899f07f76..fceec4440e 100644 --- a/dts/framework/testbed_model/capability.py +++ b/dts/framework/testbed_model/capability.py @@ -5,14 +5,29 @@ This module provides a protocol that defines the common attributes of test cases and suites and support for test environment capabilities. + +Many test cases are testing features not available on all hardware. + +The module also allows developers to mark test cases or suites as requiring certain +hardware capabilities with the :func:`requires` decorator. """ from abc import ABC, abstractmethod -from collections.abc import Sequence +from collections.abc import MutableSet, Sequence +from dataclasses import dataclass from typing import Callable, ClassVar, Protocol from typing_extensions import Self +from framework.logger import get_dts_logger +from framework.remote_session.testpmd_shell import ( + NicCapability, + TestPmdShell, + TestPmdShellCapabilityMethod, + TestPmdShellDecorator, + TestPmdShellMethod, +) + from .sut_node import SutNode from .topology import Topology @@ -96,6 +111,134 @@ def __hash__(self) -> int: """The subclasses must be hashable so that they can be stored in sets.""" +@dataclass +class DecoratedNicCapability(Capability): + """A wrapper around :class:`~framework.remote_session.testpmd_shell.NicCapability`. + + New instances should be created with the :meth:`create_unique` class method to ensure + there are no duplicate instances. + + Attributes: + nic_capability: The NIC capability that defines each instance. + capability_fn: The capability retrieval function of `nic_capability`. + capability_decorator: The decorator function of `nic_capability`. + This function will wrap `capability_fn`. + """ + + nic_capability: NicCapability + capability_fn: TestPmdShellCapabilityMethod + capability_decorator: TestPmdShellDecorator | None + _unique_capabilities: ClassVar[dict[NicCapability, Self]] = {} + + @classmethod + def get_unique(cls, nic_capability: NicCapability) -> "DecoratedNicCapability": + """Get the capability uniquely identified by `nic_capability`. + + This is a factory method that implements a quasi-enum pattern. + The instances of this class are stored in an internal class variable, + `_unique_capabilities`. + + If an instance identified by `nic_capability` doesn't exist, + it is created and added to `_unique_capabilities`. + If it exists, it is returned so that a new identical instance is not created. + + Args: + nic_capability: The NIC capability. + + Returns: + The capability uniquely identified by `nic_capability`. + """ + decorator_fn = None + if isinstance(nic_capability.value, tuple): + capability_fn, decorator_fn = nic_capability.value + else: + capability_fn = nic_capability.value + + if nic_capability not in cls._unique_capabilities: + cls._unique_capabilities[nic_capability] = cls( + nic_capability, capability_fn, decorator_fn + ) + return cls._unique_capabilities[nic_capability] + + @classmethod + def get_supported_capabilities( + cls, sut_node: SutNode, topology: "Topology" + ) -> set["DecoratedNicCapability"]: + """Overrides :meth:`~Capability.get_supported_capabilities`. + + The capabilities are first sorted by decorators, then reduced into a single function which + is then passed to the decorator. This way we execute each decorator only once. + Each capability is first checked whether it's supported/unsupported + before executing its `capability_fn` so that each capability is retrieved only once. + """ + supported_conditional_capabilities: set["DecoratedNicCapability"] = set() + logger = get_dts_logger(f"{sut_node.name}.{cls.__name__}") + if topology.type is topology.type.no_link: + logger.debug( + "No links available in the current topology, not getting NIC capabilities." + ) + return supported_conditional_capabilities + logger.debug( + f"Checking which NIC capabilities from {cls.capabilities_to_check} are supported." + ) + if cls.capabilities_to_check: + capabilities_to_check_map = cls._get_decorated_capabilities_map() + with TestPmdShell( + sut_node, privileged=True, disable_device_start=True + ) as testpmd_shell: + for conditional_capability_fn, capabilities in capabilities_to_check_map.items(): + supported_capabilities: set[NicCapability] = set() + unsupported_capabilities: set[NicCapability] = set() + capability_fn = cls._reduce_capabilities( + capabilities, supported_capabilities, unsupported_capabilities + ) + if conditional_capability_fn: + capability_fn = conditional_capability_fn(capability_fn) + capability_fn(testpmd_shell) + for capability in capabilities: + if capability.nic_capability in supported_capabilities: + supported_conditional_capabilities.add(capability) + + logger.debug(f"Found supported capabilities {supported_conditional_capabilities}.") + return supported_conditional_capabilities + + @classmethod + def _get_decorated_capabilities_map( + cls, + ) -> dict[TestPmdShellDecorator | None, set["DecoratedNicCapability"]]: + capabilities_map: dict[TestPmdShellDecorator | None, set["DecoratedNicCapability"]] = {} + for capability in cls.capabilities_to_check: + if capability.capability_decorator not in capabilities_map: + capabilities_map[capability.capability_decorator] = set() + capabilities_map[capability.capability_decorator].add(capability) + + return capabilities_map + + @classmethod + def _reduce_capabilities( + cls, + capabilities: set["DecoratedNicCapability"], + supported_capabilities: MutableSet, + unsupported_capabilities: MutableSet, + ) -> TestPmdShellMethod: + def reduced_fn(testpmd_shell: TestPmdShell) -> None: + for capability in capabilities: + if capability not in supported_capabilities | unsupported_capabilities: + capability.capability_fn( + testpmd_shell, supported_capabilities, unsupported_capabilities + ) + + return reduced_fn + + def __hash__(self) -> int: + """Instances are identified by :attr:`nic_capability` and :attr:`capability_decorator`.""" + return hash(self.nic_capability) + + def __repr__(self) -> str: + """Easy to read string of :attr:`nic_capability` and :attr:`capability_decorator`.""" + return f"{self.nic_capability}" + + class TestProtocol(Protocol): """Common test suite and test case attributes.""" @@ -116,6 +259,28 @@ def get_test_cases(cls, test_case_sublist: Sequence[str] | None = None) -> tuple raise NotImplementedError() +def requires( + *nic_capabilities: NicCapability, +) -> Callable[[type[TestProtocol]], type[TestProtocol]]: + """A decorator that adds the required capabilities to a test case or test suite. + + Args: + nic_capabilities: The NIC capabilities that are required by the test case or test suite. + + Returns: + The decorated test case or test suite. + """ + + def add_required_capability(test_case_or_suite: type[TestProtocol]) -> type[TestProtocol]: + for nic_capability in nic_capabilities: + decorated_nic_capability = DecoratedNicCapability.get_unique(nic_capability) + decorated_nic_capability.add_to_required(test_case_or_suite) + + return test_case_or_suite + + return add_required_capability + + def get_supported_capabilities( sut_node: SutNode, topology_config: Topology, -- 2.43.0