From nobody Sun Dec 14 19:15:58 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 645CC15820C for ; Fri, 28 Jun 2024 11:06:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719572811; cv=none; b=nOi6ojs3lKa16DqKwj9a22TYgOlRvGt9csPrJbts7tZoCXdBeZmjKJV/xCHL6FTA11jG2jK2un14Z1UXncSGW+PI74OiJaOK/hR/XONnOYIa9SrHSd6aGKZ6NZIT/AWyjvHmKehZGVH0OhOpPo3bJ7BjytNw8pLP960xfWnyaus= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719572811; c=relaxed/simple; bh=OiU+/BS9eUxTW1HGCZtrPJkUg6q9RjfXWDRhEHV0YiM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kNLE+UzQVtznHalKOrXu8fsFz1j6EgERl17VysJajP2vGvOYqNEV7jVS4vdgur7ly8KTNTaJo1E4YZCZvnSfPTnGQBWjbZnk0sF/hL9bGktsk1Snwq+OQx3eaneB791F8FwjLH2JH87eXfYQAHa4HQXeT24fNYv+omigmWlm3cQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=foin8sF8; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="foin8sF8" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719572808; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eD6ant/vl8AM2GLeDzaVRZlnU/b9wAppQ2l+ypA4tgU=; b=foin8sF8pFzmBeQze8CQWdAughZwpnAkdylVBpMBMZsQSMASGVtgutut+kDDkUGL8aeuiZ d+A39yaTbIt+u429Jv2K3AiOhBiP/FAWvUr47EB58vusKq2tPQYEECyY6FM0iHGb0VHW9e 8T0zu1fle0x2ia1SoTt9LHf+Eea5JDs= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-633-l8hvcfhHPhKzhIFPdC8YtQ-1; Fri, 28 Jun 2024 07:06:45 -0400 X-MC-Unique: l8hvcfhHPhKzhIFPdC8YtQ-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1D7121944D20; Fri, 28 Jun 2024 11:06:44 +0000 (UTC) Received: from antares.redhat.com (unknown [10.39.194.173]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C5A6019560B2; Fri, 28 Jun 2024 11:06:39 +0000 (UTC) From: Adrian Moreno To: netdev@vger.kernel.org Cc: aconole@redhat.com, echaudro@redhat.com, horms@kernel.org, i.maximets@ovn.org, dev@openvswitch.org, Adrian Moreno , Pravin B Shelar , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v6 07/10] selftests: openvswitch: add psample action Date: Fri, 28 Jun 2024 13:05:43 +0200 Message-ID: <20240628110559.3893562-8-amorenoz@redhat.com> In-Reply-To: <20240628110559.3893562-1-amorenoz@redhat.com> References: <20240628110559.3893562-1-amorenoz@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Add sample and psample action support to ovs-dpctl.py. Refactor common attribute parsing logic into an external function. Signed-off-by: Adrian Moreno --- .../selftests/net/openvswitch/ovs-dpctl.py | 162 +++++++++++++++++- 1 file changed, 161 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/net/openvswitch/ovs-dpctl.py b/tools/t= esting/selftests/net/openvswitch/ovs-dpctl.py index 9f8dec2f6539..4dc6ceca7e1e 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 =3D 2 OVS_FLOW_CMD_GET =3D 3 OVS_FLOW_CMD_SET =3D 4 =20 +UINT32_MAX =3D 0xFFFFFFFF =20 def macstr(mac): outstr =3D ":".join(["%02X" % i for i in mac]) @@ -267,6 +269,75 @@ def parse_extract_field( return str_skipped, data =20 =20 +def parse_attrs(actstr, attr_desc): + """Parses the given action string and returns a list of netlink + attributes based on a list of attribute descriptions. + + Each element in the attribute description list is a tuple such as: + (name, attr_name, parse_func) + where: + name: is the string representing the attribute + attr_name: is the name of the attribute as defined in the uAPI. + parse_func: is a callable accepting a string and returning either + a single object (the parsed attribute value) or a tuple of + two values (the parsed attribute value and the remaining strin= g) + + Returns a list of attributes and the remaining string. + """ + def parse_attr(actstr, key, func): + actstr =3D actstr[len(key) :] + + if not func: + return None, actstr + + delim =3D actstr[0] + actstr =3D actstr[1:] + + if delim =3D=3D "=3D": + pos =3D strcspn(actstr, ",)") + ret =3D func(actstr[:pos]) + else: + ret =3D func(actstr) + + if isinstance(ret, tuple): + (datum, actstr) =3D ret + else: + datum =3D ret + actstr =3D actstr[strcspn(actstr, ",)"):] + + if delim =3D=3D "(": + if not actstr or actstr[0] !=3D ")": + raise ValueError("Action contains unbalanced parentheses") + + actstr =3D actstr[1:] + + actstr =3D actstr[strspn(actstr, ", ") :] + + return datum, actstr + + attrs =3D [] + attr_desc =3D list(attr_desc) + while actstr and actstr[0] !=3D ")" and attr_desc: + found =3D False + for i, (key, attr, func) in enumerate(attr_desc): + if actstr.startswith(key): + datum, actstr =3D parse_attr(actstr, key, func) + attrs.append([attr, datum]) + found =3D True + del attr_desc[i] + + if not found: + raise ValueError("Unknown attribute: '%s'" % actstr) + + actstr =3D actstr[strspn(actstr, ", ") :] + + if actstr[0] !=3D ")": + raise ValueError("Action string contains extra garbage or has " + "unbalanced parenthesis: '%s'" % actstr) + + return attrs, actstr[1:] + + class ovs_dp_msg(genlmsg): # include the OVS version # We need a custom header rather than just being able to rely on @@ -285,7 +356,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"), @@ -304,8 +375,85 @@ class ovsactions(nla): ("OVS_ACTION_ATTR_ADD_MPLS", "none"), ("OVS_ACTION_ATTR_DEC_TTL", "none"), ("OVS_ACTION_ATTR_DROP", "uint32"), + ("OVS_ACTION_ATTR_PSAMPLE", "psample"), ) =20 + class psample(nla): + nla_flags =3D NLA_F_NESTED + + nla_map =3D ( + ("OVS_PSAMPLE_ATTR_UNSPEC", "none"), + ("OVS_PSAMPLE_ATTR_GROUP", "uint32"), + ("OVS_PSAMPLE_ATTR_COOKIE", "array(uint8)"), + ) + + def dpstr(self, more=3DFalse): + args =3D "group=3D%d" % self.get_attr("OVS_PSAMPLE_ATTR_GROUP") + + cookie =3D self.get_attr("OVS_PSAMPLE_ATTR_COOKIE") + if cookie: + args +=3D ",cookie(%s)" % \ + "".join(format(x, "02x") for x in cookie) + + return "psample(%s)" % args + + def parse(self, actstr): + desc =3D ( + ("group", "OVS_PSAMPLE_ATTR_GROUP", int), + ("cookie", "OVS_PSAMPLE_ATTR_COOKIE", + lambda x: list(bytearray.fromhex(x))) + ) + + attrs, actstr =3D parse_attrs(actstr, desc) + + for attr in attrs: + self["attrs"].append(attr) + + return actstr + + class sample(nla): + nla_flags =3D NLA_F_NESTED + + nla_map =3D ( + ("OVS_SAMPLE_ATTR_UNSPEC", "none"), + ("OVS_SAMPLE_ATTR_PROBABILITY", "uint32"), + ("OVS_SAMPLE_ATTR_ACTIONS", "ovsactions"), + ) + + def dpstr(self, more=3DFalse): + args =3D [] + + args.append("sample=3D{:.2f}%".format( + 100 * self.get_attr("OVS_SAMPLE_ATTR_PROBABILITY") / + UINT32_MAX)) + + actions =3D self.get_attr("OVS_SAMPLE_ATTR_ACTIONS") + if actions: + args.append("actions(%s)" % actions.dpstr(more)) + + return "sample(%s)" % ",".join(args) + + def parse(self, actstr): + def parse_nested_actions(actstr): + subacts =3D ovsactions() + parsed_len =3D subacts.parse(actstr) + return subacts, actstr[parsed_len :] + + def percent_to_rate(percent): + percent =3D float(percent.strip('%')) + return int(math.floor(UINT32_MAX * (percent / 100.0) + .5)) + + desc =3D ( + ("sample", "OVS_SAMPLE_ATTR_PROBABILITY", percent_to_rate), + ("actions", "OVS_SAMPLE_ATTR_ACTIONS", parse_nested_action= s), + ) + attrs, actstr =3D parse_attrs(actstr, desc) + + for attr in attrs: + self["attrs"].append(attr) + + return actstr + class ctact(nla): nla_flags =3D NLA_F_NESTED =20 @@ -637,6 +785,18 @@ class ovsactions(nla): self["attrs"].append(["OVS_ACTION_ATTR_CT", ctact]) parsed =3D True =20 + elif parse_starts_block(actstr, "sample(", False): + sampleact =3D self.sample() + actstr =3D sampleact.parse(actstr[len("sample(") : ]) + self["attrs"].append(["OVS_ACTION_ATTR_SAMPLE", sampleact]) + parsed =3D True + + elif parse_starts_block(actstr, "psample(", False): + psampleact =3D self.psample() + actstr =3D psampleact.parse(actstr[len("psample(") : ]) + self["attrs"].append(["OVS_ACTION_ATTR_PSAMPLE", psampleac= t]) + parsed =3D True + actstr =3D actstr[strspn(actstr, ", ") :] while parencount > 0: parencount -=3D 1 --=20 2.45.2