mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Adrian Moreno <amorenoz@redhat.com>
To: netdev@vger.kernel.org
Cc: aconole@redhat.com, echaudro@redhat.com, horms@kernel.org,
	i.maximets@ovn.org, Adrian Moreno <amorenoz@redhat.com>,
	Pravin B Shelar <pshelar@ovn.org>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Shuah Khan <shuah@kernel.org>,
	dev@openvswitch.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH net-next 7/8] selftests: openvswitch: add sample action.
Date: Wed, 24 Apr 2024 15:50:54 +0200	[thread overview]
Message-ID: <20240424135109.3524355-8-amorenoz@redhat.com> (raw)
In-Reply-To: <20240424135109.3524355-1-amorenoz@redhat.com>

Add sample action support to ovs-dpctl.py.

Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
---
 .../selftests/net/openvswitch/ovs-dpctl.py    | 96 ++++++++++++++++++-
 1 file changed, 95 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/net/openvswitch/ovs-dpctl.py b/tools/testing/selftests/net/openvswitch/ovs-dpctl.py
index 1dd057afd3fb..3a2dddc57e42 100644
--- a/tools/testing/selftests/net/openvswitch/ovs-dpctl.py
+++ b/tools/testing/selftests/net/openvswitch/ovs-dpctl.py
@@ -8,6 +8,7 @@ import argparse
 import errno
 import ipaddress
 import logging
+import math
 import multiprocessing
 import re
 import struct
@@ -58,6 +59,7 @@ OVS_FLOW_CMD_DEL = 2
 OVS_FLOW_CMD_GET = 3
 OVS_FLOW_CMD_SET = 4
 
+UINT32_MAX = 0xFFFFFFFF
 
 def macstr(mac):
     outstr = ":".join(["%02X" % i for i in mac])
@@ -285,7 +287,7 @@ class ovsactions(nla):
         ("OVS_ACTION_ATTR_SET", "none"),
         ("OVS_ACTION_ATTR_PUSH_VLAN", "none"),
         ("OVS_ACTION_ATTR_POP_VLAN", "flag"),
-        ("OVS_ACTION_ATTR_SAMPLE", "none"),
+        ("OVS_ACTION_ATTR_SAMPLE", "sample"),
         ("OVS_ACTION_ATTR_RECIRC", "uint32"),
         ("OVS_ACTION_ATTR_HASH", "none"),
         ("OVS_ACTION_ATTR_PUSH_MPLS", "none"),
@@ -306,6 +308,91 @@ class ovsactions(nla):
         ("OVS_ACTION_ATTR_DROP", "uint32"),
     )
 
+    class sample(nla):
+        nla_flags = NLA_F_NESTED
+
+        nla_map = (
+            ("OVS_SAMPLE_ATTR_UNSPEC", "none"),
+            ("OVS_SAMPLE_ATTR_PROBABILITY", "uint32"),
+            ("OVS_SAMPLE_ATTR_ACTIONS", "ovsactions"),
+            ("OVS_SAMPLE_ATTR_PSAMPLE_GROUP", "uint32"),
+            ("OVS_SAMPLE_ATTR_PSAMPLE_COOKIE", "array(uint8)"),
+        )
+
+        def dpstr(self, more=False):
+            args = []
+
+            args.append("sample={:.2f}%".format(
+                100 * self.get_attr("OVS_SAMPLE_ATTR_PROBABILITY") /
+                UINT32_MAX))
+
+            group = self.get_attr("OVS_SAMPLE_ATTR_PSAMPLE_GROUP")
+            cookie = self.get_attr("OVS_SAMPLE_ATTR_PSAMPLE_COOKIE")
+            actions = self.get_attr("OVS_SAMPLE_ATTR_ACTIONS")
+
+            if group:
+                args.append("group_id=%d" % group)
+            if cookie:
+                args.append("cookie=%s" %
+                            "".join(format(x, "02x") for x in cookie))
+            if actions:
+                args.append("actions(%s)" % actions.dpstr(more))
+
+            return "sample(%s)" % ",".join(args)
+
+        def parse(self, actstr):
+            """ Parses the input action string and populates the internal
+            attributes. The input string must start with "sample("
+
+            Returns the remaining action string.
+            Raises ValueError if the action string has invalid content.
+            """
+
+            def parse_nested_actions(actstr):
+                subacts = ovsactions()
+                parsed_len = subacts.parse(actstr)
+                return subacts, parsed_len
+
+            def percent_to_rate(percent):
+                percent = float(percent.strip('%'))
+                return int(math.floor(UINT32_MAX * (percent / 100.0) + .5))
+
+            for (key, attr, func) in (
+                ("sample", "OVS_SAMPLE_ATTR_PROBABILITY", percent_to_rate),
+                ("group_id", "OVS_SAMPLE_ATTR_PSAMPLE_GROUP", int),
+                ("cookie", "OVS_SAMPLE_ATTR_PSAMPLE_COOKIE",
+                    lambda x: list(bytearray.fromhex(x))),
+                ("actions", "OVS_SAMPLE_ATTR_ACTIONS", parse_nested_actions),
+            ):
+                if not actstr.startswith(key):
+                    continue
+
+                actstr = actstr[len(key) :]
+
+                if not func:
+                    self["attrs"].append([attr, None])
+                    continue
+
+                # The length of complex attributes cannot be determined
+                # beforehand and must be reported by the parsing func.
+                delim = actstr[0]
+                actstr = actstr[1:]
+                if delim == "=":
+                    pos = strcspn(actstr, ",)")
+                    datum = func(actstr[:pos])
+                elif delim == "(":
+                    datum, pos = func(actstr)
+
+                self["attrs"].append([attr, datum])
+                actstr = actstr[pos:]
+                actstr = actstr[strspn(actstr, ", ") :]
+
+            if actstr[0] != ")":
+                raise ValueError("Action str: '%s' unbalanced" % actstr)
+
+            return actstr[1:]
+
+
     class ctact(nla):
         nla_flags = NLA_F_NESTED
 
@@ -637,6 +724,13 @@ class ovsactions(nla):
                 self["attrs"].append(["OVS_ACTION_ATTR_CT", ctact])
                 parsed = True
 
+            elif parse_starts_block(actstr, "sample(", False):
+                sampleact = self.sample()
+                actstr = sampleact.parse(actstr[len("sample(") : ])
+                self["attrs"].append(["OVS_ACTION_ATTR_SAMPLE", sampleact])
+                parsed = True
+
+
             actstr = actstr[strspn(actstr, ", ") :]
             while parencount > 0:
                 parencount -= 1
-- 
2.44.0


  parent reply	other threads:[~2024-04-24 13:54 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-24 13:50 [PATCH net-next 0/8] net: openvswitch: Add sample multicasting Adrian Moreno
2024-04-24 13:50 ` [PATCH net-next 1/8] net: netlink: export genl private pointer getters Adrian Moreno
2024-04-24 13:50 ` [PATCH net-next 2/8] net: psample: add multicast filtering on group_id Adrian Moreno
2024-04-24 14:54   ` Jiri Pirko
2024-04-25  7:23     ` Adrian Moreno
2024-04-24 13:50 ` [PATCH net-next 3/8] net: psample: add user cookie Adrian Moreno
2024-04-25  7:32   ` Ido Schimmel
2024-04-25  8:09     ` Adrian Moreno
2024-04-24 13:50 ` [PATCH net-next 4/8] net: psample: add tracepoint Adrian Moreno
2024-04-25  7:18   ` Ido Schimmel
2024-04-25  8:06     ` Adrian Moreno
2024-04-25 15:25       ` Ido Schimmel
2024-04-29  5:33         ` Adrian Moreno
2024-04-30 12:53           ` Ido Schimmel
2024-04-24 13:50 ` [PATCH net-next 5/8] net: sched: act_sample: add action cookie to sample Adrian Moreno
2024-04-25  7:39   ` Ido Schimmel
2024-04-25 21:43   ` Jamal Hadi Salim
2024-04-24 13:50 ` [PATCH net-next 6/8] net:openvswitch: add psample support Adrian Moreno
2024-04-30  7:29   ` Dan Carpenter
2024-05-03  9:43   ` Eelco Chaudron
2024-05-07 14:18     ` Adrian Moreno
2024-05-08  9:48       ` Eelco Chaudron
2024-05-08 15:25       ` Aaron Conole
2024-04-24 13:50 ` Adrian Moreno [this message]
2024-04-24 13:50 ` [PATCH net-next 8/8] selftests: openvswitch: add psample test Adrian Moreno

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240424135109.3524355-8-amorenoz@redhat.com \
    --to=amorenoz@redhat.com \
    --cc=aconole@redhat.com \
    --cc=davem@davemloft.net \
    --cc=dev@openvswitch.org \
    --cc=echaudro@redhat.com \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=i.maximets@ovn.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pshelar@ovn.org \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®