From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 2130345751;
	Tue,  6 Aug 2024 18:42:00 +0200 (CEST)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 058FA40685;
	Tue,  6 Aug 2024 18:42:00 +0200 (CEST)
Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com
 [209.85.222.178])
 by mails.dpdk.org (Postfix) with ESMTP id EB25640270
 for <dev@dpdk.org>; Tue,  6 Aug 2024 18:41:58 +0200 (CEST)
Received: by mail-qk1-f178.google.com with SMTP id
 af79cd13be357-7a1d0ad7113so57199685a.2
 for <dev@dpdk.org>; Tue, 06 Aug 2024 09:41:58 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=iol.unh.edu; s=unh-iol; t=1722962518; x=1723567318; 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=j5juHj72MiNvAX4z66PnEc1Uo5MXJeRHkvlC2wv2cwY=;
 b=VTjEKvckdBP1sN5bY3liF6FT+vBEPQHXyHBGBRkbqQmAIZ1HwKL785Dl9R4EKhymy3
 C5QY+qGBHa6lLwZXi1Z1PEHymd6KyLR0Q40wkQgC5uTgi4hNz8i7u3V+Ceei3I8H46UW
 HdVHKafQ7ewyE1+jGqIwNDTea3G16K4ROR4cY=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1722962518; x=1723567318;
 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=j5juHj72MiNvAX4z66PnEc1Uo5MXJeRHkvlC2wv2cwY=;
 b=UHfVgnOepFf62HPJAOwQRaKKbbDwTdaPxsmoo81X6bqZ6BBGBNiggpDXtL/YG/jycS
 HiMum8Nra4cy/cGPwDVGIKFoV1XMywoJJETS2hRtESQqkwIzj2TfyZ5S0a9UMjzCEQfY
 KDOwfCE1HqfSbg6jgnHw6mj+Fv2PyHdwNq8DVnm6y0gxRyqjs6pHwUtHZ9Eio3MOCkpn
 VWanVTB9nGa3lrkXvyvoayCwNwyzrNZPsQP/8Djr+ah4V3dl9bjDbDd2nEgCnSctvB7d
 7rRCXupOIiKJ0CMTb3vVoKyPKVK+L6vlp4yhQCyZvMzqeNPdYcXzFgzVWLRf8LqDsdZb
 lpbQ==
X-Gm-Message-State: AOJu0YwAY5Xq0xuTsY7/jZtmsUS/o3slQv2VTvE8syDTZaRZajbRLzsH
 X5RjonVmaitHcOGNTAwt2J/THT8ECVKNajjZIVR3uiV3LbQgYht8E5HwhNPiANU=
X-Google-Smtp-Source: AGHT+IF76u68KakmPy5WvmOjRmcCW18BTLm/J1oTUtM6hhNdl6HwmgKuZZuhwjS1G8798D+ZrTDcQQ==
X-Received: by 2002:a05:620a:40d5:b0:7a1:d880:a479 with SMTP id
 af79cd13be357-7a34ef3116fmr2035341085a.26.1722962517846; 
 Tue, 06 Aug 2024 09:41:57 -0700 (PDT)
Received: from dean-laptop.iol.unh.edu
 ([2606:4100:3880:1271:f9bd:24da:464d:6294])
 by smtp.gmail.com with ESMTPSA id
 af79cd13be357-7a35f3fc336sm326142285a.22.2024.08.06.09.41.56
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Tue, 06 Aug 2024 09:41:57 -0700 (PDT)
From: Dean Marx <dmarx@iol.unh.edu>
To: probb@iol.unh.edu, npratte@iol.unh.edu, jspewock@iol.unh.edu,
 luca.vizzarro@arm.com, yoan.picchi@foss.arm.com,
 Honnappa.Nagarahalli@arm.com, paul.szczepanek@arm.com,
 juraj.linkes@pantheon.tech
Cc: dev@dpdk.org,
	Dean Marx <dmarx@iol.unh.edu>
Subject: [PATCH v2] dts: add flow rule dataclass to testpmd shell
Date: Tue,  6 Aug 2024 12:42:08 -0400
Message-ID: <20240806164208.3351-1-dmarx@iol.unh.edu>
X-Mailer: git-send-email 2.44.0
In-Reply-To: <20240726142213.3109-1-dmarx@iol.unh.edu>
References: <20240726142213.3109-1-dmarx@iol.unh.edu>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

add dataclass for passing in flow rule creation arguments, as well as a
__str__ method for converting to a sendable testpmd command. Add
flow_create method to TestPmdShell class for initializing flow rules.

Signed-off-by: Dean Marx <dmarx@iol.unh.edu>
---
 dts/framework/remote_session/testpmd_shell.py | 58 ++++++++++++++++++-
 1 file changed, 57 insertions(+), 1 deletion(-)

diff --git a/dts/framework/remote_session/testpmd_shell.py b/dts/framework/remote_session/testpmd_shell.py
index 43e9f56517..59b2bb914b 100644
--- a/dts/framework/remote_session/testpmd_shell.py
+++ b/dts/framework/remote_session/testpmd_shell.py
@@ -19,7 +19,7 @@
 from dataclasses import dataclass, field
 from enum import Flag, auto
 from pathlib import PurePath
-from typing import ClassVar
+from typing import ClassVar, Optional
 
 from typing_extensions import Self, Unpack
 
@@ -577,6 +577,43 @@ class TestPmdPortStats(TextParser):
     tx_bps: int = field(metadata=TextParser.find_int(r"Tx-bps:\s+(\d+)"))
 
 
+@dataclass
+class flow_func:
+    """Dataclass for setting flow rule parameters."""
+
+    #:
+    port_id: int
+    #:
+    ingress: bool
+    #:
+    pattern: str
+    #:
+    actions: str
+
+    #:
+    group_id: Optional[int] = None
+    #:
+    priority_level: Optional[int] = None
+    #:
+    user_id: Optional[int] = None
+
+    def __str__(self) -> str:
+        """Returns the string representation of a flow_func instance.
+
+        In this case, a properly formatted flow create command that can be sent to testpmd.
+        """
+        ret = []
+        ret.append(f"flow create {self.port_id} ")
+        ret.append(f"group {self.group_id} " if self.group_id is not None else "")
+        ret.append(f"priority {self.priority_level} " if self.priority_level is not None else "")
+        ret.append("ingress " if self.ingress else "egress ")
+        ret.append(f"user_id {self.user_id} " if self.user_id is not None else "")
+        ret.append(f"pattern {self.pattern} ")
+        ret.append(" / end actions ")
+        ret.append(f"{self.actions} / end")
+        return "".join(ret)
+
+
 class TestPmdShell(DPDKShell):
     """Testpmd interactive shell.
 
@@ -806,6 +843,25 @@ def show_port_stats(self, port_id: int) -> TestPmdPortStats:
 
         return TestPmdPortStats.parse(output)
 
+    def flow_create(self, cmd: flow_func, verify: bool = True) -> None:
+        """Creates a flow rule in the testpmd session.
+
+        Args:
+            cmd: String from flow_func instance to send as a flow rule.
+            verify: If :data:`True`, the output of the command is scanned
+            to ensure the flow rule was created successfully.
+
+        Raises:
+            InteractiveCommandExecutionError: If flow rule is invalid.
+        """
+        flow_output = self.send_command(str(cmd))
+        if verify:
+            if "created" not in flow_output:
+                self._logger.debug(f"Failed to create flow rule:\n{flow_output}")
+                raise InteractiveCommandExecutionError(
+                    f"Failed to create flow rule:\n{flow_output}"
+                )
+
     def _close(self) -> None:
         """Overrides :meth:`~.interactive_shell.close`."""
         self.stop()
-- 
2.44.0