[v2,4/5] dts: add ability to start/stop testpmd ports

Message ID 20240806124642.2580828-5-luca.vizzarro@arm.com (mailing list archive)
State New
Delegated to: Juraj Linkeš
Headers
Series dts: add pktgen and testpmd changes |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Luca Vizzarro Aug. 6, 2024, 12:46 p.m. UTC
Add testpmd commands to start and stop all the ports, so that they can
be configured. Because there is a distinction of commands that require
the ports to be stopped and started, also add decorators for commands
that require a specific state, removing this logic from the test
writer's duty.

Signed-off-by: Luca Vizzarro <luca.vizzarro@arm.com>
Reviewed-by: Paul Szczepanek <paul.szczepanek@arm.com>
---
 dts/framework/remote_session/testpmd_shell.py | 86 ++++++++++++++++++-
 1 file changed, 84 insertions(+), 2 deletions(-)
  

Comments

Jeremy Spewock Aug. 9, 2024, 3:13 p.m. UTC | #1
I tried to address this very same problem in the scatter expansion
patch series but, admittedly, I like your approach better. There is
one thing that Juraj and I discussed on that thread however that is
probably worth noting here regarding verification of the stopping and
starting ports. The main idea behind that being if the user calls a
testpmd method and specifically specifies they don't want to verify
the outcome of that method, but then we still verify whether we
stopped the ports or not, that could cause confusion. The compromise
we ended up settling for was just to make a parameter to the decorator
that allows you to statically decide which decorated methods should
verify the ports stopping and which shouldn't. It was mainly because
of the ability to specify "verify" as a kwarg or an arg and the types
being messy. The discussion we had about it starts here if you were
interested in reading it:
http://inbox.dpdk.org/dev/dff89e16-0173-4069-878c-d1c34df1ae13@pantheon.tech/

On Tue, Aug 6, 2024 at 8:49 AM Luca Vizzarro <luca.vizzarro@arm.com> wrote:
>
> Add testpmd commands to start and stop all the ports, so that they can
> be configured. Because there is a distinction of commands that require
> the ports to be stopped and started, also add decorators for commands
> that require a specific state, removing this logic from the test
> writer's duty.
>
> Signed-off-by: Luca Vizzarro <luca.vizzarro@arm.com>
> Reviewed-by: Paul Szczepanek <paul.szczepanek@arm.com>
> ---
<snip>
> +P = ParamSpec("P")
> +TestPmdShellMethod = Callable[Concatenate["TestPmdShell", P], Any]

Agggh, I spent so much time trying to figure out how to do exactly
this when I was working on the scatter series but I couldn't get a
nice typehint while specifying that I wanted a TestPmdShell first. I
had no idea that Concatenate was a type but I will definitely keep
that one in mind.

> +
>
>  class TestPmdDevice:
>      """The data of a device that testpmd can recognize.
> @@ -577,12 +581,51 @@ class TestPmdPortStats(TextParser):
>      tx_bps: int = field(metadata=TextParser.find_int(r"Tx-bps:\s+(\d+)"))
>
>
> +def requires_stopped_ports(func: TestPmdShellMethod) -> TestPmdShellMethod:
> +    """Decorator for :class:`TestPmdShell` commands methods that require stopped ports.
> +
> +    If the decorated method is called while the ports are started, then these are stopped before
> +    continuing.
> +    """
> +
> +    @functools.wraps(func)
> +    def _wrapper(self: "TestPmdShell", *args: P.args, **kwargs: P.kwargs):
> +        if self.ports_started:
> +            self._logger.debug("Ports need to be stopped to continue")
> +            self.stop_all_ports()
> +
> +        return func(self, *args, **kwargs)
> +
> +    return _wrapper
> +
> +
> +def requires_started_ports(func: TestPmdShellMethod) -> TestPmdShellMethod:
> +    """Decorator for :class:`TestPmdShell` commands methods that require started ports.
> +
> +    If the decorated method is called while the ports are stopped, then these are started before
> +    continuing.
> +    """
> +
> +    @functools.wraps(func)
> +    def _wrapper(self: "TestPmdShell", *args: P.args, **kwargs: P.kwargs):
> +        if not self.ports_started:
> +            self._logger.debug("Ports need to be started to continue")
> +            self.start_all_ports()
> +
> +        return func(self, *args, **kwargs)
> +
> +    return _wrapper
> +

I'm not sure how much it is necessary since it is pretty clear what
these decorators are trying to do with the parameter, but since these
are public methods I think not having an "Args:" section for the func
might trip up the doc generation.

> +
>  class TestPmdShell(DPDKShell):
>      """Testpmd interactive shell.
>
>      The testpmd shell users should never use
>      the :meth:`~.interactive_shell.InteractiveShell.send_command` method directly, but rather
>      call specialized methods. If there isn't one that satisfies a need, it should be added.
> +
> +    Attributes:
> +        ports_started: Indicates whether the ports are started.
>      """
>
>      _app_params: TestPmdParams
> @@ -619,6 +662,9 @@ def __init__(
>              name,
>          )
>
> +        self.ports_started = not self._app_params.disable_device_start
> +

It might be useful to add this attribute as a stub in the class just
to be clear on the typing. It also helps me understand things at more
of a glance personally.

> +    @requires_started_ports
>      def start(self, verify: bool = True) -> None:
>          """Start packet forwarding with the current configuration.
<snip>

> --
> 2.34.1
>
  
Juraj Linkeš Aug. 23, 2024, 12:16 p.m. UTC | #2
As Jeremy mentioned, adding the verify argument may be worthwhile, but 
maybe only if we actually identify a usecase where we wouldn't want to 
do the verification.

I also have two other points:
1. Do we want a decorator that does both - starting and stopping? Is the 
idea to have all methods that require a certain state to be decorated 
and in that case we don't need this? E.g. if there are multiple 
configuration steps, only the first one stops the ports (if started) and 
we start the ports only when a method needs that (such as starting pkt 
fwd)? I think this workflow should be documented in the class docstring 
(the important part being that starting and stopping of ports is done 
automatically).
2. Do we want decorators that start/stop just one port? I guess this is 
basically useless with the above workflow.
  
Luca Vizzarro Sept. 6, 2024, 4:41 p.m. UTC | #3
On 09/08/2024 16:13, Jeremy Spewock wrote:
> I tried to address this very same problem in the scatter expansion
> patch series but, admittedly, I like your approach better. There is
> one thing that Juraj and I discussed on that thread however that is
> probably worth noting here regarding verification of the stopping and
> starting ports. The main idea behind that being if the user calls a
> testpmd method and specifically specifies they don't want to verify
> the outcome of that method, but then we still verify whether we
> stopped the ports or not, that could cause confusion. The compromise
> we ended up settling for was just to make a parameter to the decorator
> that allows you to statically decide which decorated methods should
> verify the ports stopping and which shouldn't. It was mainly because
> of the ability to specify "verify" as a kwarg or an arg and the types
> being messy. The discussion we had about it starts here if you were
> interested in reading it:
> http://inbox.dpdk.org/dev/dff89e16-0173-4069-878c-d1c34df1ae13@pantheon.tech/

When it comes to starting and stopping ports, we may need to ensure that 
the operations were done correctly. Surely we don't want to start 
forwarding thinking that the ports are initialised, when they could be 
not... I guess the point here is that it may be important to save the 
correct state. Not sure, I guess verify could always be added later if 
needed.

> On Tue, Aug 6, 2024 at 8:49 AM Luca Vizzarro <luca.vizzarro@arm.com> wrote:
>>
>> Add testpmd commands to start and stop all the ports, so that they can
>> be configured. Because there is a distinction of commands that require
>> the ports to be stopped and started, also add decorators for commands
>> that require a specific state, removing this logic from the test
>> writer's duty.
>>
>> Signed-off-by: Luca Vizzarro <luca.vizzarro@arm.com>
>> Reviewed-by: Paul Szczepanek <paul.szczepanek@arm.com>
>> ---
> <snip>
>> +P = ParamSpec("P")
>> +TestPmdShellMethod = Callable[Concatenate["TestPmdShell", P], Any]
> 
> Agggh, I spent so much time trying to figure out how to do exactly
> this when I was working on the scatter series but I couldn't get a
> nice typehint while specifying that I wanted a TestPmdShell first. I
> had no idea that Concatenate was a type but I will definitely keep
> that one in mind.
> 

Glad I could help! :D

>> +def requires_started_ports(func: TestPmdShellMethod) -> TestPmdShellMethod:
>> +    """Decorator for :class:`TestPmdShell` commands methods that require started ports.
>> +
>> +    If the decorated method is called while the ports are stopped, then these are started before
>> +    continuing.
>> +    """
>> +
>> +    @functools.wraps(func)
>> +    def _wrapper(self: "TestPmdShell", *args: P.args, **kwargs: P.kwargs):
>> +        if not self.ports_started:
>> +            self._logger.debug("Ports need to be started to continue")
>> +            self.start_all_ports()
>> +
>> +        return func(self, *args, **kwargs)
>> +
>> +    return _wrapper
>> +
> 
> I'm not sure how much it is necessary since it is pretty clear what
> these decorators are trying to do with the parameter, but since these
> are public methods I think not having an "Args:" section for the func
> might trip up the doc generation.

We have several methods that have one-liner docstrings... so not really 
sure. I was hoping to have the doc generation merged as testing what's 
correct and what's not is currently complicated.

>>
>> +        self.ports_started = not self._app_params.disable_device_start
>> +
> 
> It might be useful to add this attribute as a stub in the class just
> to be clear on the typing. It also helps me understand things at more
> of a glance personally.

You make a fair point here.
  
Luca Vizzarro Sept. 6, 2024, 4:46 p.m. UTC | #4
On 23/08/2024 13:16, Juraj Linkeš wrote:
> As Jeremy mentioned, adding the verify argument may be worthwhile, but 
> maybe only if we actually identify a usecase where we wouldn't want to 
> do the verification.

Yeah, as I pointed out, it feels unlikely to pretend that they are 
started (or stopped). I think that could cause my unpredicted behaviour 
and confusion. But also as said already, it can always be added on demand.

> I also have two other points:
> 1. Do we want a decorator that does both - starting and stopping? Is the 
> idea to have all methods that require a certain state to be decorated 
> and in that case we don't need this? E.g. if there are multiple 
> configuration steps, only the first one stops the ports (if started) and 
> we start the ports only when a method needs that (such as starting pkt 
> fwd)? I think this workflow should be documented in the class docstring 
> (the important part being that starting and stopping of ports is done 
> automatically).

The way I envisioned these decorators is by using a lazy approach. I 
don't think it may be right to eagerly restart ports after stopping 
them, because maybe we want to run another command after that will stop 
them again. So only start and stop as needed. Ideally every port that 
requires a specific state of the ports need to be decorated. I went 
through all the methods in the class to check which would and which 
wouldn't, and it seems alright like this.

> 2. Do we want decorators that start/stop just one port? I guess this is 
> basically useless with the above workflow.

Would it actually matter? We are not really using the other ports in 
parallel here I believe. May bring unnecessary complexity. I am thinking 
that maybe if needed it could be added later.
  

Patch

diff --git a/dts/framework/remote_session/testpmd_shell.py b/dts/framework/remote_session/testpmd_shell.py
index 43e9f56517..ca24b28070 100644
--- a/dts/framework/remote_session/testpmd_shell.py
+++ b/dts/framework/remote_session/testpmd_shell.py
@@ -14,16 +14,17 @@ 
     testpmd_shell.close()
 """
 
+import functools
 import re
 import time
 from dataclasses import dataclass, field
 from enum import Flag, auto
 from pathlib import PurePath
-from typing import ClassVar
+from typing import Any, Callable, ClassVar, Concatenate, ParamSpec
 
 from typing_extensions import Self, Unpack
 
-from framework.exception import InteractiveCommandExecutionError
+from framework.exception import InteractiveCommandExecutionError, InternalError
 from framework.params.testpmd import SimpleForwardingModes, TestPmdParams
 from framework.params.types import TestPmdParamsDict
 from framework.parser import ParserFn, TextParser
@@ -33,6 +34,9 @@ 
 from framework.testbed_model.sut_node import SutNode
 from framework.utils import StrEnum
 
+P = ParamSpec("P")
+TestPmdShellMethod = Callable[Concatenate["TestPmdShell", P], Any]
+
 
 class TestPmdDevice:
     """The data of a device that testpmd can recognize.
@@ -577,12 +581,51 @@  class TestPmdPortStats(TextParser):
     tx_bps: int = field(metadata=TextParser.find_int(r"Tx-bps:\s+(\d+)"))
 
 
+def requires_stopped_ports(func: TestPmdShellMethod) -> TestPmdShellMethod:
+    """Decorator for :class:`TestPmdShell` commands methods that require stopped ports.
+
+    If the decorated method is called while the ports are started, then these are stopped before
+    continuing.
+    """
+
+    @functools.wraps(func)
+    def _wrapper(self: "TestPmdShell", *args: P.args, **kwargs: P.kwargs):
+        if self.ports_started:
+            self._logger.debug("Ports need to be stopped to continue")
+            self.stop_all_ports()
+
+        return func(self, *args, **kwargs)
+
+    return _wrapper
+
+
+def requires_started_ports(func: TestPmdShellMethod) -> TestPmdShellMethod:
+    """Decorator for :class:`TestPmdShell` commands methods that require started ports.
+
+    If the decorated method is called while the ports are stopped, then these are started before
+    continuing.
+    """
+
+    @functools.wraps(func)
+    def _wrapper(self: "TestPmdShell", *args: P.args, **kwargs: P.kwargs):
+        if not self.ports_started:
+            self._logger.debug("Ports need to be started to continue")
+            self.start_all_ports()
+
+        return func(self, *args, **kwargs)
+
+    return _wrapper
+
+
 class TestPmdShell(DPDKShell):
     """Testpmd interactive shell.
 
     The testpmd shell users should never use
     the :meth:`~.interactive_shell.InteractiveShell.send_command` method directly, but rather
     call specialized methods. If there isn't one that satisfies a need, it should be added.
+
+    Attributes:
+        ports_started: Indicates whether the ports are started.
     """
 
     _app_params: TestPmdParams
@@ -619,6 +662,9 @@  def __init__(
             name,
         )
 
+        self.ports_started = not self._app_params.disable_device_start
+
+    @requires_started_ports
     def start(self, verify: bool = True) -> None:
         """Start packet forwarding with the current configuration.
 
@@ -723,6 +769,42 @@  def set_forward_mode(self, mode: SimpleForwardingModes, verify: bool = True):
                 f"Test pmd failed to set fwd mode to {mode.value}"
             )
 
+    def stop_all_ports(self, verify: bool = True) -> None:
+        """Stops all the ports.
+
+        Args:
+            verify: If :data:`True`, the output of the command will be checked for a successful
+                execution.
+
+        Raises:
+            InteractiveCommandExecutionError: If `verify` is :data:`True` and the ports were not
+                stopped successfully.
+        """
+        self._logger.debug("Stopping all the ports...")
+        output = self.send_command("port stop all")
+        if verify and not output.strip().endswith("Done"):
+            raise InteractiveCommandExecutionError("Ports were not stopped successfully")
+
+        self.ports_started = False
+
+    def start_all_ports(self, verify: bool = True) -> None:
+        """Starts all the ports.
+
+        Args:
+            verify: If :data:`True`, the output of the command will be checked for a successful
+                execution.
+
+        Raises:
+            InteractiveCommandExecutionError: If `verify` is :data:`True` and the ports were not
+                started successfully.
+        """
+        self._logger.debug("Starting all the ports...")
+        output = self.send_command("port start all")
+        if verify and not output.strip().endswith("Done"):
+            raise InteractiveCommandExecutionError("Ports were not started successfully")
+
+        self.ports_started = True
+
     def show_port_info_all(self) -> list[TestPmdPort]:
         """Returns the information of all the ports.