Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp1159383lqj; Mon, 3 Jun 2024 11:59:12 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUXi6m2oVNPUItDl56AWngCQeB+eNIcszxYCIKBv5IhNEfo2EgRQasOpkwTBjrYBQ8e5MtDx+Qmpascj5gpC51qLF8aPKEcmLPVdtJL4w== X-Google-Smtp-Source: AGHT+IFin5aHlgZyaAnXft42HE//r75SkOj27lhDea+chJ5AZV4QmfTwqVqaRzSU5TzNTB7juk4O X-Received: by 2002:aca:2b14:0:b0:3c9:924c:8304 with SMTP id 5614622812f47-3d1e31eafd4mr10307645b6e.0.1717441152065; Mon, 03 Jun 2024 11:59:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717441152; cv=pass; d=google.com; s=arc-20160816; b=I/HaQiO46ANp6jwSR/3GxmRrwgj2y6QqqGExyGXMwMcMUhUeNgU5gcMGAm3pP5gFO7 /3fveJqZDuHyUUGVKuObur4VD5GCkoS36sgyZyXNS8KD89rfZ+lAVXkK2WR+GE51AKSl xk8Uzdz14V1cgPGUGxtO8mLNmKeeWVYNo1Qb02Sd5MtO76Km4MgkTAHV9UBNH3j5TI5u MTn5VBoxy7Ftvpd5D6HupSEGZY96vUrKgPs8XZ7POm33FxNCt6f7YrTONhjQeJXZfSzo SNMOrYpSPmXdqLFdxXNyys4ifwYKIBMZs7H13hUaH3xgYX3DiEOCVX11Y7zHxl0wbDeJ jcxQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=alkIBUL7mgK4tYFa5uTDNnOwfjh38ynFKzKPMUHF7D0=; fh=73LExhb8KHcGvc1MBBeSHQuV+anJGif2P4HKEaRZnZY=; b=1D/naNcExNDFr4W4VPJHu0BDfbh0pHiLk7H1lg+znSil+x5hki8xKGQdlenWDNkUc+ 0NibudPerL8XL7uV/x7dO+YbV6MsqoqYHcrHGoZhFs4gYkmllAYXjjVIOoGma9suEcgS lUwjzFkH6bCC+1wdVZGnPJsphXJsEbabxFTYEd+BGHSo1NoZAf9DavyOC4Id77Tqi955 oMtUZ8whGFfeIrLx4NxKLipqWTLQKdXAs4WDy96BuPlbU5W71rcauKOP7vHnUKltd6RT Zw02IvcJdvLpFqDQbiKVsa09+RU+Z0+pZ16FbowEN8HmVTqUgz3CWn42uMu7/yvD6VYP X+3w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QCTrov+s; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-199590-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199590-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6ae4b43da30si13057296d6.558.2024.06.03.11.59.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 11:59:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-199590-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QCTrov+s; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-199590-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199590-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B2D221C21C91 for ; Mon, 3 Jun 2024 18:59:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4C013139D0E; Mon, 3 Jun 2024 18:57:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QCTrov+s" 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 887FD13C69D for ; Mon, 3 Jun 2024 18:57: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=1717441071; cv=none; b=aSUuWz5onY3wUdYbyQpNv6aGIOwBHnnjz7P40N+UeMTg6C34zMp2H2cBCxqJvNd2Z0bhwBiVTuGAYfRkd1fuA4lDVqlU1Y7aTVAczsdCYEbGB33SDZjbdnl6MBtlxVHBIjmLxFuh2mKvItP4LjOeIbq4qi5lwPGzrQM+8iOK61A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717441071; c=relaxed/simple; bh=jp8Eh7J9m39a/4D/ctyzUD+qlf21hxniSfaNvzMpW7M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jjH6gn8z+tELW4TW8zhbX9lbjSlCSUm/ws19yZtVCrw9eXmH/f0T9Ww0yZR3wjaaDUu5ytfUF81L9ggAiscbl+fS3kTgNnXYEezVi7khA6HTlsMnfkCFtM+NZsV1AFXMGNXTU62dib1eU0r+BuO6Gz0uGCkXYQolQt2meUJ03UQ= 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=QCTrov+s; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717441068; 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=alkIBUL7mgK4tYFa5uTDNnOwfjh38ynFKzKPMUHF7D0=; b=QCTrov+s4HKPfq81AbQWLuNIZdJpbb8L9nHCks20y2b+D8ZeYKlT8Nlq8ZtE3plnzqBd/U DioThPW9cV6l839upBF9mzCQroz0D6/jbfqo0ZaLUxBt+IZgeM9uNWUpIVUHJoD50+KD0t 6nC/Tgz/bPCaEuLigZHl3uC6jeQ4DYs= Received: from mx-prod-mc-01.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-19-5hoUAj2YMv2Jwrl7hk9Sow-1; Mon, 03 Jun 2024 14:57:46 -0400 X-MC-Unique: 5hoUAj2YMv2Jwrl7hk9Sow-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BE92C1918E85; Mon, 3 Jun 2024 18:57:44 +0000 (UTC) Received: from antares.redhat.com (unknown [10.39.193.112]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1EF1C1955F6E; Mon, 3 Jun 2024 18:57: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 v2 8/9] selftests: openvswitch: add emit_sample action Date: Mon, 3 Jun 2024 20:56:42 +0200 Message-ID: <20240603185647.2310748-9-amorenoz@redhat.com> In-Reply-To: <20240603185647.2310748-1-amorenoz@redhat.com> References: <20240603185647.2310748-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: 8bit X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Add sample and emit_sample 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/testing/selftests/net/openvswitch/ovs-dpctl.py index a2395c3f37a1..f8b5362aac8c 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]) @@ -267,6 +269,75 @@ def parse_extract_field( return str_skipped, data +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 string) + + Returns a list of attributes and the remaining string. + """ + def parse_attr(actstr, key, func): + actstr = actstr[len(key) :] + + if not func: + return None, actstr + + delim = actstr[0] + actstr = actstr[1:] + + if delim == "=": + pos = strcspn(actstr, ",)") + ret = func(actstr[:pos]) + else: + ret = func(actstr) + + if isinstance(ret, tuple): + (datum, actstr) = ret + else: + datum = ret + actstr = actstr[strcspn(actstr, ",)"):] + + if delim == "(": + if not actstr or actstr[0] != ")": + raise ValueError("Action contains unbalanced parentheses") + + actstr = actstr[1:] + + actstr = actstr[strspn(actstr, ", ") :] + + return datum, actstr + + attrs = [] + attr_desc = list(attr_desc) + while actstr and actstr[0] != ")" and attr_desc: + found = False + for i, (key, attr, func) in enumerate(attr_desc): + if actstr.startswith(key): + datum, actstr = parse_attr(actstr, key, func) + attrs.append([attr, datum]) + found = True + del attr_desc[i] + + if not found: + raise ValueError("Unknown attribute: '%s'" % actstr) + + actstr = actstr[strspn(actstr, ", ") :] + + if actstr[0] != ")": + 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_EMIT_SAMPLE", "emit_sample"), ) + class emit_sample(nla): + nla_flags = NLA_F_NESTED + + nla_map = ( + ("OVS_EMIT_SAMPLE_ATTR_UNSPEC", "none"), + ("OVS_EMIT_SAMPLE_ATTR_GROUP", "uint32"), + ("OVS_EMIT_SAMPLE_ATTR_COOKIE", "array(uint8)"), + ) + + def dpstr(self, more=False): + args = "group=%d" % self.get_attr("OVS_EMIT_SAMPLE_ATTR_GROUP") + + cookie = self.get_attr("OVS_EMIT_SAMPLE_ATTR_COOKIE") + if cookie: + args += ",cookie(%s)" % \ + "".join(format(x, "02x") for x in cookie) + + return "emit_sample(%s)" % args + + def parse(self, actstr): + desc = ( + ("group", "OVS_EMIT_SAMPLE_ATTR_GROUP", int), + ("cookie", "OVS_EMIT_SAMPLE_ATTR_COOKIE", + lambda x: list(bytearray.fromhex(x))) + ) + + attrs, actstr = parse_attrs(actstr, desc) + + for attr in attrs: + self["attrs"].append(attr) + + return actstr + + 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"), + ) + + def dpstr(self, more=False): + args = [] + + args.append("sample={:.2f}%".format( + 100 * self.get_attr("OVS_SAMPLE_ATTR_PROBABILITY") / + UINT32_MAX)) + + actions = 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 = ovsactions() + parsed_len = subacts.parse(actstr) + return subacts, actstr[parsed_len :] + + def percent_to_rate(percent): + percent = float(percent.strip('%')) + return int(math.floor(UINT32_MAX * (percent / 100.0) + .5)) + + desc = ( + ("sample", "OVS_SAMPLE_ATTR_PROBABILITY", percent_to_rate), + ("actions", "OVS_SAMPLE_ATTR_ACTIONS", parse_nested_actions), + ) + attrs, actstr = parse_attrs(actstr, desc) + + for attr in attrs: + self["attrs"].append(attr) + + return actstr + class ctact(nla): nla_flags = NLA_F_NESTED @@ -643,6 +791,18 @@ 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 + + elif parse_starts_block(actstr, "emit_sample(", False): + emitact = self.emit_sample() + actstr = emitact.parse(actstr[len("emit_sample(") : ]) + self["attrs"].append(["OVS_ACTION_ATTR_EMIT_SAMPLE", emitact]) + parsed = True + actstr = actstr[strspn(actstr, ", ") :] while parencount > 0: parencount -= 1 -- 2.45.1