From nobody Sat May 18 07:09:03 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 8419415E1F3 for ; Wed, 24 Apr 2024 13:54:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966878; cv=none; b=Ue8vPUspBCUH9ViE1XD62JJm7+v3dWMQ9aH3iR6iLo+WrbvSRSMfuGN288MGa/otjegtaRbE6C6vde5JthRNck1DZXtd6jWZoRKHsgVer+RZUjAWORsNWMCxVgrXdBKxRgZ++f2D6SDeh/exhbkZAELkfZYLkpra7lel3NYrukc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966878; c=relaxed/simple; bh=aaAS8vV4wb9QvjU6bclPinSt2ufTufAeAaQH7gzZLTM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O2vGnqXRKM1A60QnrunAZ4cyQ+DWHc0mYw/d56fRSbqKT5ES2bSGRLVeP75ZxYzbcPt5d8s7VoSChDV5k0e8rV7q65yqcSCBCX5VS9a8XvQ3M42lbCzOxo/hxioZ907j/RHembHur7VAQemxd1R9d9g0Ka3ebbySksYzyJZVcT0= 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=PtFH6AJ/; arc=none smtp.client-ip=170.10.133.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="PtFH6AJ/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713966876; 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=rmUaRXIMcsJ+dXovFl57XFZEcsYZXt5cyjvVIml/sc4=; b=PtFH6AJ/f6c6RSnBZ0ZxWLh2y+ElHbYC9CeKwROKX5qo532060+gxEnpWOuyFl4sLb0zD3 0/DXPgYctlWzyeo/Jx77MrjZrZv/ZSQpcuBeupdxTbt33oh3jqka7QnZAStSgZenPSdQgX x84sciqXq9ffgfYWGD/HygCwqnojBoU= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-208-HJzfv7WJMfWcK7JLXcIesg-1; Wed, 24 Apr 2024 09:54:33 -0400 X-MC-Unique: HJzfv7WJMfWcK7JLXcIesg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id A4DDD299E740; Wed, 24 Apr 2024 13:54:32 +0000 (UTC) Received: from antares.redhat.com (unknown [10.39.193.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id 218A91C0654B; Wed, 24 Apr 2024 13:54:29 +0000 (UTC) From: Adrian Moreno To: netdev@vger.kernel.org Cc: aconole@redhat.com, echaudro@redhat.com, horms@kernel.org, i.maximets@ovn.org, Adrian Moreno , Jakub Kicinski , "David S. Miller" , Eric Dumazet , Paolo Abeni , Jiri Pirko , Jacob Keller , Johannes Berg , Ido Schimmel , Stanislaw Gruszka , linux-kernel@vger.kernel.org Subject: [PATCH net-next 1/8] net: netlink: export genl private pointer getters Date: Wed, 24 Apr 2024 15:50:48 +0200 Message-ID: <20240424135109.3524355-2-amorenoz@redhat.com> In-Reply-To: <20240424135109.3524355-1-amorenoz@redhat.com> References: <20240424135109.3524355-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.4.1 on 10.11.54.7 Content-Type: text/plain; charset="utf-8" Both "__genl_sk_priv_get" and "genl_sk_priv_get" are useful functions to handle private pointers attached to genetlink sockets. Export them so they can be used in modules. Reviewed-by: Jakub Kicinski Signed-off-by: Adrian Moreno --- net/netlink/genetlink.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c index feb54c63a116..beafa415a9f5 100644 --- a/net/netlink/genetlink.c +++ b/net/netlink/genetlink.c @@ -726,6 +726,7 @@ void *__genl_sk_priv_get(struct genl_family *family, st= ruct sock *sk) return ERR_PTR(-EINVAL); return xa_load(family->sock_privs, (unsigned long) sk); } +EXPORT_SYMBOL(__genl_sk_priv_get); =20 /** * genl_sk_priv_get - Get family private pointer for socket @@ -764,6 +765,7 @@ void *genl_sk_priv_get(struct genl_family *family, stru= ct sock *sk) } return priv; } +EXPORT_SYMBOL(genl_sk_priv_get); =20 /** * genl_register_family - register a generic netlink family --=20 2.44.0 From nobody Sat May 18 07:09:03 2024 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 CC2FC15E7EE for ; Wed, 24 Apr 2024 13:54:40 +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=1713966882; cv=none; b=L4E0AWzk6af/PmLDHPrZqf5QDXa2ob4iFwXqKVN4fQBXYYVQ+yer0/6YiM/HemBSnU7i+s0p3d+xsc5MdTlkjKW6lQomHam+f/5RwSKInHF0/Ay+zlPucqX+iiKSgY0QZpsWuEylprd0pRTuL9a2HKVcbybynPMCwltYW+zFlCE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966882; c=relaxed/simple; bh=gxTGezLHWXY8b7sIhgnJbShbHkUHWXR6geF6I6OEDao=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Qpr3Cm07VbxBIViC/UXZTUGqROv7hr/0daoaP0upqUf1SBkZhLhAIxNxLuDxi2VwAvIjJGhu63RWtfAeIY6QoN/3EZ98JOZ+KZrwhpfvG6aAEvfrJD5b19WnwhPkdBBnmY36IySFjVFzcFB1EY5kX2+M7ruDpBd3iR1VplbvbtA= 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=UBEzUrLY; 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="UBEzUrLY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713966880; 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=mvfMKL+8UkJyc+6NdO80ILmMaNmzG0sr9rlaAL4MQIg=; b=UBEzUrLYP1KsCFrnT3zjqKBcErNlcII2ybg9kYZuWE1D9WXCpPYP6nMDljzrqdhSvAkgZL xPx76MNATkXz6G5gUG2WPzZbtZht3pWXUeqmIBATJVnc6MH7HZNHQAuQ3WQKecb2mD/SlV HsS18hsDTFvssmfqdBxqDW1L43ugV2o= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-651-tSYvXOl7N2OL-BBPH0PrrQ-1; Wed, 24 Apr 2024 09:54:35 -0400 X-MC-Unique: tSYvXOl7N2OL-BBPH0PrrQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 148D73C0252B; Wed, 24 Apr 2024 13:54:35 +0000 (UTC) Received: from antares.redhat.com (unknown [10.39.193.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3B8881C0F120; Wed, 24 Apr 2024 13:54:33 +0000 (UTC) From: Adrian Moreno To: netdev@vger.kernel.org Cc: aconole@redhat.com, echaudro@redhat.com, horms@kernel.org, i.maximets@ovn.org, Adrian Moreno , Yotam Gigi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-kernel@vger.kernel.org Subject: [PATCH net-next 2/8] net: psample: add multicast filtering on group_id Date: Wed, 24 Apr 2024 15:50:49 +0200 Message-ID: <20240424135109.3524355-3-amorenoz@redhat.com> In-Reply-To: <20240424135109.3524355-1-amorenoz@redhat.com> References: <20240424135109.3524355-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.4.1 on 10.11.54.7 Content-Type: text/plain; charset="utf-8" Packet samples can come from several places (e.g: different tc sample actions), typically using the sample group (PSAMPLE_ATTR_SAMPLE_GROUP) to differentiate them. Likewise, sample consumers that listen on the multicast group may only be interested on a single group. However, they are currently forced to receive all samples and discard the ones that are not relevant, causing unnecessary overhead. Allow users to filter on the desired group_id by adding a new command PSAMPLE_SET_FILTER that can be used to pass the desired group id. Store this filter on the per-socket private pointer and use it for filtering multicasted samples. Signed-off-by: Adrian Moreno --- include/uapi/linux/psample.h | 1 + net/psample/psample.c | 110 +++++++++++++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 6 deletions(-) diff --git a/include/uapi/linux/psample.h b/include/uapi/linux/psample.h index e585db5bf2d2..9d62983af0a4 100644 --- a/include/uapi/linux/psample.h +++ b/include/uapi/linux/psample.h @@ -28,6 +28,7 @@ enum psample_command { PSAMPLE_CMD_GET_GROUP, PSAMPLE_CMD_NEW_GROUP, PSAMPLE_CMD_DEL_GROUP, + PSAMPLE_CMD_SET_FILTER, }; =20 enum psample_tunnel_key_attr { diff --git a/net/psample/psample.c b/net/psample/psample.c index a5d9b8446f77..f5f77515b969 100644 --- a/net/psample/psample.c +++ b/net/psample/psample.c @@ -98,13 +98,77 @@ static int psample_nl_cmd_get_group_dumpit(struct sk_bu= ff *msg, return msg->len; } =20 -static const struct genl_small_ops psample_nl_ops[] =3D { +struct psample_obj_desc { + struct rcu_head rcu; + u32 group_num; +}; + +struct psample_nl_sock_priv { + struct psample_obj_desc __rcu *filter; + spinlock_t filter_lock; /* Protects filter. */ +}; + +static void psample_nl_sock_priv_init(void *priv) +{ + struct psample_nl_sock_priv *sk_priv =3D priv; + + spin_lock_init(&sk_priv->filter_lock); +} + +static void psample_nl_sock_priv_destroy(void *priv) +{ + struct psample_nl_sock_priv *sk_priv =3D priv; + struct psample_obj_desc *filter; + + filter =3D rcu_dereference_protected(sk_priv->filter, true); + kfree_rcu(filter, rcu); +} + +static int psample_nl_set_filter_doit(struct sk_buff *skb, + struct genl_info *info) +{ + struct psample_obj_desc *filter =3D NULL; + struct psample_nl_sock_priv *sk_priv; + struct nlattr **attrs =3D info->attrs; + + if (attrs[PSAMPLE_ATTR_SAMPLE_GROUP]) { + filter =3D kzalloc(sizeof(*filter), GFP_KERNEL); + filter->group_num =3D + nla_get_u32(attrs[PSAMPLE_ATTR_SAMPLE_GROUP]); + } + + sk_priv =3D genl_sk_priv_get(&psample_nl_family, NETLINK_CB(skb).sk); + if (IS_ERR(sk_priv)) { + kfree(filter); + return PTR_ERR(sk_priv); + } + + spin_lock(&sk_priv->filter_lock); + filter =3D rcu_replace_pointer(sk_priv->filter, filter, + lockdep_is_held(&sk_priv->filter_lock)); + spin_unlock(&sk_priv->filter_lock); + kfree_rcu(filter, rcu); + return 0; +} + +static const struct nla_policy +psample_set_filter_policy[PSAMPLE_ATTR_SAMPLE_GROUP + 1] =3D { + [PSAMPLE_ATTR_SAMPLE_GROUP] =3D { .type =3D NLA_U32, }, +}; + +static const struct genl_ops psample_nl_ops[] =3D { { .cmd =3D PSAMPLE_CMD_GET_GROUP, .validate =3D GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, .dumpit =3D psample_nl_cmd_get_group_dumpit, /* can be retrieved by unprivileged users */ - } + }, + { + .cmd =3D PSAMPLE_CMD_SET_FILTER, + .doit =3D psample_nl_set_filter_doit, + .policy =3D psample_set_filter_policy, + .flags =3D 0, + }, }; =20 static struct genl_family psample_nl_family __ro_after_init =3D { @@ -114,10 +178,13 @@ static struct genl_family psample_nl_family __ro_afte= r_init =3D { .netnsok =3D true, .module =3D THIS_MODULE, .mcgrps =3D psample_nl_mcgrps, - .small_ops =3D psample_nl_ops, - .n_small_ops =3D ARRAY_SIZE(psample_nl_ops), + .ops =3D psample_nl_ops, + .n_ops =3D ARRAY_SIZE(psample_nl_ops), .resv_start_op =3D PSAMPLE_CMD_GET_GROUP + 1, .n_mcgrps =3D ARRAY_SIZE(psample_nl_mcgrps), + .sock_priv_size =3D sizeof(struct psample_nl_sock_priv), + .sock_priv_init =3D psample_nl_sock_priv_init, + .sock_priv_destroy =3D psample_nl_sock_priv_destroy, }; =20 static void psample_group_notify(struct psample_group *group, @@ -360,6 +427,32 @@ static int psample_tunnel_meta_len(struct ip_tunnel_in= fo *tun_info) } #endif =20 +static inline void psample_nl_obj_desc_init(struct psample_obj_desc *desc, + u32 group_num) +{ + memset(desc, 0, sizeof(*desc)); + desc->group_num =3D group_num; +} + +static int psample_nl_sample_filter(struct sock *dsk, struct sk_buff *skb, + void *data) +{ + struct psample_obj_desc *desc =3D data; + struct psample_nl_sock_priv *sk_priv; + struct psample_obj_desc *filter; + int ret =3D 0; + + rcu_read_lock(); + sk_priv =3D __genl_sk_priv_get(&psample_nl_family, dsk); + if (!IS_ERR_OR_NULL(sk_priv)) { + filter =3D rcu_dereference(sk_priv->filter); + if (filter && desc) + ret =3D (filter->group_num !=3D desc->group_num); + } + rcu_read_unlock(); + return ret; +} + void psample_sample_packet(struct psample_group *group, struct sk_buff *sk= b, u32 sample_rate, const struct psample_metadata *md) { @@ -370,6 +463,7 @@ void psample_sample_packet(struct psample_group *group,= struct sk_buff *skb, #ifdef CONFIG_INET struct ip_tunnel_info *tun_info; #endif + struct psample_obj_desc desc; struct sk_buff *nl_skb; int data_len; int meta_len; @@ -487,8 +581,12 @@ void psample_sample_packet(struct psample_group *group= , struct sk_buff *skb, #endif =20 genlmsg_end(nl_skb, data); - genlmsg_multicast_netns(&psample_nl_family, group->net, nl_skb, 0, - PSAMPLE_NL_MCGRP_SAMPLE, GFP_ATOMIC); + psample_nl_obj_desc_init(&desc, group->group_num); + genlmsg_multicast_netns_filtered(&psample_nl_family, + group->net, nl_skb, 0, + PSAMPLE_NL_MCGRP_SAMPLE, + GFP_ATOMIC, psample_nl_sample_filter, + &desc); =20 return; error: --=20 2.44.0 From nobody Sat May 18 07:09:03 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 7862515ECE4 for ; Wed, 24 Apr 2024 13:54:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966885; cv=none; b=fVz6qreNk55x9e2lOPPrDMRy5wgZfawgaY8dE0FGzQm02RkyIN533DXQGXfzGOIu2gvHDGaxCBPYKW3yKjJT9x2grybl5sx3hb3MsmAquizTacVDtjwhnulnXxwwPWql0m46zmLzTuA8TMllzEsygVOMdA+0oTxb3ouNOLPuwu0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966885; c=relaxed/simple; bh=UOWkQ6MFWCBRbY+FwMYzxJ5JdvgUYAvrEWMlto0lo14=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DrpQ7lTT43+3PXfmeVJvNHauqbh4nH+L9g+jciVDr9sCjqCc1RLFqYTVlMLOQN5G4dPUafgZkFYSAGbts0HoULRVBa1LcrNA7ryv7GExIa08C1N1YPwARGEueUJEDmLU6oMw9/WB7921P+NJ6nvakvkAclpyY4YoLrx4DkBeoCk= 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=ZLZCny2u; arc=none smtp.client-ip=170.10.133.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="ZLZCny2u" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713966883; 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=vikpMJxJ9Ds+2aUp1Td8HwJZsh9X5P4rCQC4urDpxnw=; b=ZLZCny2uQAklGJiyg5kS2mzXtlbML6HUnobY1aaKrNPmc811Sqmw8ev/bq3PjfWRsXIqUB yTPDiTlSuXfP/AB2pgqYWeOir3VbJm68KpTvp1kSoUrpvl7aItmfiBdWn78Dv5aExsBqpA bQ8hVGpVy67yrKmttOq/8R4BpivqgsM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-516-gdQN8hpGOlq2BtCoLX2ZrA-1; Wed, 24 Apr 2024 09:54:38 -0400 X-MC-Unique: gdQN8hpGOlq2BtCoLX2ZrA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 837FB834FEE; Wed, 24 Apr 2024 13:54:37 +0000 (UTC) Received: from antares.redhat.com (unknown [10.39.193.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id A82F71C060D0; Wed, 24 Apr 2024 13:54:35 +0000 (UTC) From: Adrian Moreno To: netdev@vger.kernel.org Cc: aconole@redhat.com, echaudro@redhat.com, horms@kernel.org, i.maximets@ovn.org, Adrian Moreno , Yotam Gigi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-kernel@vger.kernel.org Subject: [PATCH net-next 3/8] net: psample: add user cookie Date: Wed, 24 Apr 2024 15:50:50 +0200 Message-ID: <20240424135109.3524355-4-amorenoz@redhat.com> In-Reply-To: <20240424135109.3524355-1-amorenoz@redhat.com> References: <20240424135109.3524355-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.4.1 on 10.11.54.7 Content-Type: text/plain; charset="utf-8" Add a user cookie to the sample metadata so that sample emitters can provide more contextual information to samples. If present, send the user cookie in a new attribute: PSAMPLE_ATTR_USER_COOKIE. Signed-off-by: Adrian Moreno --- include/net/psample.h | 2 ++ include/uapi/linux/psample.h | 1 + net/psample/psample.c | 12 +++++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/net/psample.h b/include/net/psample.h index 0509d2d6be67..2ac71260a546 100644 --- a/include/net/psample.h +++ b/include/net/psample.h @@ -25,6 +25,8 @@ struct psample_metadata { out_tc_occ_valid:1, latency_valid:1, unused:5; + const u8 *user_cookie; + u32 user_cookie_len; }; =20 struct psample_group *psample_group_get(struct net *net, u32 group_num); diff --git a/include/uapi/linux/psample.h b/include/uapi/linux/psample.h index 9d62983af0a4..83dc919c4023 100644 --- a/include/uapi/linux/psample.h +++ b/include/uapi/linux/psample.h @@ -19,6 +19,7 @@ enum { PSAMPLE_ATTR_LATENCY, /* u64, nanoseconds */ PSAMPLE_ATTR_TIMESTAMP, /* u64, nanoseconds */ PSAMPLE_ATTR_PROTO, /* u16 */ + PSAMPLE_ATTR_USER_COOKIE, /* binary, user provided data */ =20 __PSAMPLE_ATTR_MAX }; diff --git a/net/psample/psample.c b/net/psample/psample.c index f5f77515b969..476aaad7a885 100644 --- a/net/psample/psample.c +++ b/net/psample/psample.c @@ -480,7 +480,8 @@ void psample_sample_packet(struct psample_group *group,= struct sk_buff *skb, nla_total_size(sizeof(u32)) + /* group_num */ nla_total_size(sizeof(u32)) + /* seq */ nla_total_size_64bit(sizeof(u64)) + /* timestamp */ - nla_total_size(sizeof(u16)); /* protocol */ + nla_total_size(sizeof(u16)) + /* protocol */ + nla_total_size(md->user_cookie_len); /* user_cookie */ =20 #ifdef CONFIG_INET tun_info =3D skb_tunnel_info(skb); @@ -579,6 +580,15 @@ void psample_sample_packet(struct psample_group *group= , struct sk_buff *skb, goto error; } #endif + if (md->user_cookie && md->user_cookie_len) { + int nla_len =3D nla_total_size(md->user_cookie_len); + struct nlattr *nla; + + nla =3D skb_put(nl_skb, nla_len); + nla->nla_type =3D PSAMPLE_ATTR_USER_COOKIE; + nla->nla_len =3D nla_attr_size(md->user_cookie_len); + memcpy(nla_data(nla), md->user_cookie, md->user_cookie_len); + } =20 genlmsg_end(nl_skb, data); psample_nl_obj_desc_init(&desc, group->group_num); --=20 2.44.0 From nobody Sat May 18 07:09:03 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 E0DD415EFA8 for ; Wed, 24 Apr 2024 13:54:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966888; cv=none; b=mWOF0o9huT336RcfhbkXOInxflPd6kOFkrKu+WCKy/KXp1+NAyWPneiYT/xgz73fHCg6rfQ/5wGJnoRqnDDwRev+TYx4t5Y1egYlm2sFSUL2lxeKFRlTChSaJoc/Ofk60HZRXDSNfGKJoXpzf3Rww9MeYqtbj0+Z+BCBKM7WOvs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966888; c=relaxed/simple; bh=psco6awsRgaMV9T/niM4W2JCE6T6vm6u5bIdA5YcY4o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CXUfqy6Co23TaH6S+MLMGkWjsb4MZdWPbv6KyzyF6Nwxkpc4HRoQrz0Q3vMUWhxFaLz0Wtjf6dzY3mNK0ZZsW62HIUx5NvCu6Qn1EWZ53zj2+uWrbZafZSApK9b2+R6mSFyodcYlI+Fdcj18vlgEHpUMY6L7vWdyBEuW/Kw6cyk= 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=DuwtFUAv; arc=none smtp.client-ip=170.10.133.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="DuwtFUAv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713966886; 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=80uJMF+CJMwXdgnnjS23JUC2Vtnpylf1OILSwz1dwGo=; b=DuwtFUAvP3/hIFdfWE0Y9jpYJxfrFNs/f5UZq0zTweb0LR/NrqRlgN1ykLa9cOmDE1XRnA 2edAEtLK2pQ/l4gEVnwaUkCOX0nDl/x4ONm8fuw76ay88OAdyIYJq+Mh7WK23ESo1LwYGe Cx9p+k8EEzsq3KePJeWNI72Cgf/541k= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-528-T36Ifq7pMmir5GN6AExaSw-1; Wed, 24 Apr 2024 09:54:41 -0400 X-MC-Unique: T36Ifq7pMmir5GN6AExaSw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 222911839FA0; Wed, 24 Apr 2024 13:54:41 +0000 (UTC) Received: from antares.redhat.com (unknown [10.39.193.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4CCB21C060D0; Wed, 24 Apr 2024 13:54: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, Adrian Moreno , Yotam Gigi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-kernel@vger.kernel.org Subject: [PATCH net-next 4/8] net: psample: add tracepoint Date: Wed, 24 Apr 2024 15:50:51 +0200 Message-ID: <20240424135109.3524355-5-amorenoz@redhat.com> In-Reply-To: <20240424135109.3524355-1-amorenoz@redhat.com> References: <20240424135109.3524355-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.4.1 on 10.11.54.7 Content-Type: text/plain; charset="utf-8" Currently there are no widely-available tools to dump the metadata and group information when a packet is sampled, making it difficult to troubleshoot related issues. This makes psample use the event tracing framework to log the sampling of a packet so that it's easier to quickly identify the source (i.e: group) and context (i.e: metadata) of a packet being sampled. This patch creates some checkpatch splats, but the style of the tracepoint definition mimics that of other modules so it seems acceptable. Signed-off-by: Adrian Moreno --- net/psample/psample.c | 9 +++++++ net/psample/trace.h | 62 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 net/psample/trace.h diff --git a/net/psample/psample.c b/net/psample/psample.c index 476aaad7a885..92db8ffa2ba2 100644 --- a/net/psample/psample.c +++ b/net/psample/psample.c @@ -18,6 +18,12 @@ #include #include =20 +#ifndef __CHECKER__ +#define CREATE_TRACE_POINTS +#endif + +#include "trace.h" + #define PSAMPLE_MAX_PACKET_SIZE 0xffff =20 static LIST_HEAD(psample_groups_list); @@ -470,6 +476,9 @@ void psample_sample_packet(struct psample_group *group,= struct sk_buff *skb, void *data; int ret; =20 + if (trace_psample_sample_packet_enabled()) + trace_psample_sample_packet(group, skb, sample_rate, md); + meta_len =3D (in_ifindex ? nla_total_size(sizeof(u16)) : 0) + (out_ifindex ? nla_total_size(sizeof(u16)) : 0) + (md->out_tc_valid ? nla_total_size(sizeof(u16)) : 0) + diff --git a/net/psample/trace.h b/net/psample/trace.h new file mode 100644 index 000000000000..2d32a846989b --- /dev/null +++ b/net/psample/trace.h @@ -0,0 +1,62 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM psample + +#if !defined(_TRACE_PSAMPLE_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_PSAMPLE__H + +#include +#include +#include + +TRACE_EVENT(psample_sample_packet, + + TP_PROTO(struct psample_group *group, struct sk_buff *skb, + u32 sample_rate, const struct psample_metadata *md), + + TP_ARGS(group, skb, sample_rate, md), + + TP_STRUCT__entry( + __field(u32, group_num) + __field(u32, refcount) + __field(u32, seq) + __field(void *, skbaddr) + __field(unsigned int, len) + __field(unsigned int, data_len) + __field(u32, sample_rate) + __field(int, in_ifindex) + __field(int, out_ifindex) + __field(const void *, user_cookie) + __field(u32, user_cookie_len) + ), + + TP_fast_assign( + __entry->group_num =3D group->group_num; + __entry->refcount =3D group->refcount; + __entry->seq =3D group->seq; + __entry->skbaddr =3D skb; + __entry->len =3D skb->len; + __entry->data_len =3D skb->data_len; + __entry->sample_rate =3D sample_rate; + __entry->in_ifindex =3D md->in_ifindex; + __entry->out_ifindex =3D md->out_ifindex; + __entry->user_cookie =3D &md->user_cookie[0]; + __entry->user_cookie_len =3D md->user_cookie_len; + ), + + TP_printk("group_num=3D%u refcount=3D%u seq=3D%u skbaddr=3D%p len=3D%u da= ta_len=3D%u sample_rate=3D%u in_ifindex=3D%d out_ifindex=3D%d user_cookie= =3D%p user_cookie_len=3D%u", + __entry->group_num, __entry->refcount, __entry->seq, + __entry->skbaddr, __entry->len, __entry->data_len, + __entry->sample_rate, __entry->in_ifindex, + __entry->out_ifindex, __entry->user_cookie, + __entry->user_cookie_len) +); + +#endif /* _TRACE_PSAMPLE_H */ + +/* This part must be outside protection */ +#undef TRACE_INCLUDE_PATH +#define TRACE_INCLUDE_PATH ../../net/psample +#undef TRACE_INCLUDE_FILE +#define TRACE_INCLUDE_FILE trace +#include --=20 2.44.0 From nobody Sat May 18 07:09:03 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 E3F5B15EFD5 for ; Wed, 24 Apr 2024 13:54:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966891; cv=none; b=MG6O8dsRLusM2Y6Sw2Lc1GplBNA4KciyTWNnwv3AaKbucGBhNNn6nhLUiaPTYByY/qvcPicIIMCgkBRVjtg9dyC9Ehsa1nVyJpn+zuen/qig7PZDUUgG6qLkYdd6WqrFBIQp7sqFftC6gSCRFpeVyNFPYPPtk0//DgS9c5bz2B4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966891; c=relaxed/simple; bh=n+RkZRic6ie2G6KEet41avGQwCdtOj5Z3lDU2WqxhVM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R3Y5BwxGPRp31dIAlDhuDwHF0SEZ6Hv21g0T7ae6jWXKLqAj06aw+NaKsHTxdCI5eo5xaxX0Q2Mhy+lM4p7uMXbC2xIYm08N68JyBo2aVhoDPVEWC80OWX77SRjV24//g8S3cjVJXQoVrKwPhtQPaW5EQsfoYvOPeOwQUjEFDKQ= 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=GU1GAapl; arc=none smtp.client-ip=170.10.133.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="GU1GAapl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713966888; 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=LGEwg3DBg8RjmiswLmLzz0YVtT2CJ7TbhBWyCbgcMoQ=; b=GU1GAaplvtz3/85uOfSdzyVHg8m2UCr3sYA/a43HOfH3TRcBL+uqGR3WmmQM3lQ4i3XnDD TGgkyIqoOULGFFac1ZGPp03CZdiYDct0wI0+YUzNTiiF+BKwCnwWQB5lxkn3JoyqVpgw0X WN4cT+oG8LS9VauhSJvViroMdeW0YCc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-656-Vzx_EQ-XMa24eb4s9v5pQA-1; Wed, 24 Apr 2024 09:54:44 -0400 X-MC-Unique: Vzx_EQ-XMa24eb4s9v5pQA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id EC52A104D537; Wed, 24 Apr 2024 13:54:43 +0000 (UTC) Received: from antares.redhat.com (unknown [10.39.193.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id 902EC1C060D0; Wed, 24 Apr 2024 13:54:41 +0000 (UTC) From: Adrian Moreno To: netdev@vger.kernel.org Cc: aconole@redhat.com, echaudro@redhat.com, horms@kernel.org, i.maximets@ovn.org, Adrian Moreno , Jamal Hadi Salim , Cong Wang , Jiri Pirko , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-kernel@vger.kernel.org Subject: [PATCH net-next 5/8] net: sched: act_sample: add action cookie to sample Date: Wed, 24 Apr 2024 15:50:52 +0200 Message-ID: <20240424135109.3524355-6-amorenoz@redhat.com> In-Reply-To: <20240424135109.3524355-1-amorenoz@redhat.com> References: <20240424135109.3524355-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.4.1 on 10.11.54.7 Content-Type: text/plain; charset="utf-8" If the action has a user_cookie, pass it along to the sample so it can be easily identified. Signed-off-by: Adrian Moreno Reviewed-by: Ido Schimmel Reviewed-by: Jamal Hadi Salim --- net/sched/act_sample.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/net/sched/act_sample.c b/net/sched/act_sample.c index a69b53d54039..5c3f86ec964a 100644 --- a/net/sched/act_sample.c +++ b/net/sched/act_sample.c @@ -165,9 +165,11 @@ TC_INDIRECT_SCOPE int tcf_sample_act(struct sk_buff *s= kb, const struct tc_action *a, struct tcf_result *res) { + u8 cookie_data[TC_COOKIE_MAX_SIZE] =3D {}; struct tcf_sample *s =3D to_sample(a); struct psample_group *psample_group; struct psample_metadata md =3D {}; + struct tc_cookie *user_cookie; int retval; =20 tcf_lastuse_update(&s->tcf_tm); @@ -189,6 +191,16 @@ TC_INDIRECT_SCOPE int tcf_sample_act(struct sk_buff *s= kb, if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev)) skb_push(skb, skb->mac_len); =20 + rcu_read_lock(); + user_cookie =3D rcu_dereference(a->user_cookie); + if (user_cookie) { + memcpy(cookie_data, user_cookie->data, + user_cookie->len); + md.user_cookie =3D cookie_data; + md.user_cookie_len =3D user_cookie->len; + } + rcu_read_unlock(); + md.trunc_size =3D s->truncate ? s->trunc_size : skb->len; psample_sample_packet(psample_group, skb, s->rate, &md); =20 --=20 2.44.0 From nobody Sat May 18 07:09:03 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 DE9FC15E1FD for ; Wed, 24 Apr 2024 13:54:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966898; cv=none; b=a1tvg1ACDWgG/iFpLcwDtIMW5V02kgwM4yXnBPR9E+XpbUBalIp+RMYYxKUGphqhDJxUfyhJxRVzMUCMI9gAJqz8GVoQIJ7DSV92XK36i324wJU1GfUSLrrbXFY23ukqJTk4OZKh88Z5wArveOkYTTxnmof7zmOZtDz5pnqk3l0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966898; c=relaxed/simple; bh=4NcjaWSsSdX3L3qcZFJzNNlvPctHO6umf9/Defd/OAY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=So70/0WW5HjyzuVgfGVruYR04ZCGRHC5jF2OGb+NsX143AL1sNaC9gozrNTW8LupiWOuh8LMcVs3o6fmwfD9FjNGEVNupOSJ2LfD5TBymXkA9i1oV4f05Qt9nCtxTzuFd1X0BI97PQ3VNXIck7hgO5YouAs5DYPcoqFrmePRxxw= 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=NiZ/p5LM; arc=none smtp.client-ip=170.10.133.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="NiZ/p5LM" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713966895; 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=jIPqwhfGK1wcmX33k6tBJ0wJeSbk/ZBhsvCTykKHqkw=; b=NiZ/p5LMl5oc1Feji6su5aDoau1p4c3taNE4LHw3Aa/KtaFY7ipY5nGB6/w2gO1SPcsqqy wyeXwiHNhTSrWEKHPeAuf16qIYjGLYoX2j6b2HU8pWmY7Db6uq3Y93F/gFk8SFe8DksA4B V7Qx8QQMktA7fjcV48o8w6BUaNVuIaQ= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-70-_LhlJPW2PiOSXqOiu7ySag-1; Wed, 24 Apr 2024 09:54:51 -0400 X-MC-Unique: _LhlJPW2PiOSXqOiu7ySag-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 35ABB3C02B54; Wed, 24 Apr 2024 13:54:51 +0000 (UTC) Received: from antares.redhat.com (unknown [10.39.193.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1D8CE1C06798; Wed, 24 Apr 2024 13:54:48 +0000 (UTC) From: Adrian Moreno To: netdev@vger.kernel.org Cc: aconole@redhat.com, echaudro@redhat.com, horms@kernel.org, i.maximets@ovn.org, Adrian Moreno , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Pravin B Shelar , Donald Hunter , linux-kernel@vger.kernel.org, dev@openvswitch.org Subject: [PATCH net-next 6/8] net:openvswitch: add psample support Date: Wed, 24 Apr 2024 15:50:53 +0200 Message-ID: <20240424135109.3524355-7-amorenoz@redhat.com> In-Reply-To: <20240424135109.3524355-1-amorenoz@redhat.com> References: <20240424135109.3524355-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.4.1 on 10.11.54.7 Content-Type: text/plain; charset="utf-8" Add support for psample sampling via two new attributes to the OVS_ACTION_ATTR_SAMPLE action. OVS_SAMPLE_ATTR_PSAMPLE_GROUP used to pass an integer psample group_id. OVS_SAMPLE_ATTR_PSAMPLE_COOKIE used to pass a variable-length binary cookie that will be forwared to psample. The maximum length of the user-defined cookie is set to 16, same as tc_cookie, to discourage using cookies that will not be offloadable. In order to simplify the internal processing of the action and given the maximum size of the cookie is relatively small, add both fields to the internal-only struct sample_arg. The presence of a group_id mandates that the action shall called the psample module to multicast the packet with such group_id and the user-provided cookie if present. This behavior is orthonogal to also executing the nested actions if present. Signed-off-by: Adrian Moreno --- Documentation/netlink/specs/ovs_flow.yaml | 6 ++ include/uapi/linux/openvswitch.h | 49 ++++++++++---- net/openvswitch/actions.c | 51 +++++++++++++-- net/openvswitch/flow_netlink.c | 80 ++++++++++++++++++----- 4 files changed, 153 insertions(+), 33 deletions(-) diff --git a/Documentation/netlink/specs/ovs_flow.yaml b/Documentation/netl= ink/specs/ovs_flow.yaml index 4fdfc6b5cae9..5543c2937225 100644 --- a/Documentation/netlink/specs/ovs_flow.yaml +++ b/Documentation/netlink/specs/ovs_flow.yaml @@ -825,6 +825,12 @@ attribute-sets: name: actions type: nest nested-attributes: action-attrs + - + name: psample_group + type: u32 + - + name: psample_cookie + type: binary - name: userspace-attrs enum-name: ovs-userspace-attr diff --git a/include/uapi/linux/openvswitch.h b/include/uapi/linux/openvswi= tch.h index efc82c318fa2..e9cd6f3a952d 100644 --- a/include/uapi/linux/openvswitch.h +++ b/include/uapi/linux/openvswitch.h @@ -639,6 +639,7 @@ enum ovs_flow_attr { #define OVS_UFID_F_OMIT_MASK (1 << 1) #define OVS_UFID_F_OMIT_ACTIONS (1 << 2) =20 +#define OVS_PSAMPLE_COOKIE_MAX_SIZE 16 /** * enum ovs_sample_attr - Attributes for %OVS_ACTION_ATTR_SAMPLE action. * @OVS_SAMPLE_ATTR_PROBABILITY: 32-bit fraction of packets to sample with @@ -646,15 +647,27 @@ enum ovs_flow_attr { * %UINT32_MAX samples all packets and intermediate values sample intermed= iate * fractions of packets. * @OVS_SAMPLE_ATTR_ACTIONS: Set of actions to execute in sampling event. - * Actions are passed as nested attributes. + * Actions are passed as nested attributes. Optional if + * OVS_SAMPLE_ATTR_PSAMPLE_GROUP is set. + * @OVS_SAMPLE_ATTR_PSAMPLE_GROUP: A 32-bit number to be used as psample g= roup. + * Optional if OVS_SAMPLE_ATTR_ACTIONS is set. + * @OVS_SAMPLE_ATTR_PSAMPLE_COOKIE: A variable-length binary cookie that, = if + * provided, will be copied to the psample cookie. * - * Executes the specified actions with the given probability on a per-pack= et - * basis. + * Either OVS_SAMPLE_ATTR_PSAMPLE_GROUP or OVS_SAMPLE_ATTR_ACTIONS must be + * specified. + * + * Executes the specified actions and/or sends the packet to psample + * with the given probability on a per-packet basis. */ enum ovs_sample_attr { OVS_SAMPLE_ATTR_UNSPEC, - OVS_SAMPLE_ATTR_PROBABILITY, /* u32 number */ - OVS_SAMPLE_ATTR_ACTIONS, /* Nested OVS_ACTION_ATTR_* attributes. */ + OVS_SAMPLE_ATTR_PROBABILITY, /* u32 number */ + OVS_SAMPLE_ATTR_ACTIONS, /* Nested OVS_ACTION_ATTR_ + * attributes. + */ + OVS_SAMPLE_ATTR_PSAMPLE_GROUP, /* u32 number */ + OVS_SAMPLE_ATTR_PSAMPLE_COOKIE, /* binary */ __OVS_SAMPLE_ATTR_MAX, =20 #ifdef __KERNEL__ @@ -665,13 +678,27 @@ enum ovs_sample_attr { #define OVS_SAMPLE_ATTR_MAX (__OVS_SAMPLE_ATTR_MAX - 1) =20 #ifdef __KERNEL__ + +/* Definition for flags in struct sample_arg. */ +enum { + /* When set, actions in sample will not change the flows. */ + OVS_SAMPLE_ARG_FLAG_EXEC =3D 1 << 0, + /* When set, the packet will be sent to psample. */ + OVS_SAMPLE_ARG_FLAG_PSAMPLE =3D 1 << 1, +}; + struct sample_arg { - bool exec; /* When true, actions in sample will not - * change flow keys. False otherwise. - */ - u32 probability; /* Same value as - * 'OVS_SAMPLE_ATTR_PROBABILITY'. - */ + u16 flags; /* Flags that modify the behavior of the + * action. See SAMPLE_ARG_FLAG_*. + */ + u32 probability; /* Same value as + * 'OVS_SAMPLE_ATTR_PROBABILITY'. + */ + u32 group_id; /* Same value as + * 'OVS_SAMPLE_ATTR_PSAMPLE_GROUP'. + */ + u8 cookie_len; /* Length of psample cookie. */ + char cookie[OVS_PSAMPLE_COOKIE_MAX_SIZE]; /* psample cookie data. */ }; #endif =20 diff --git a/net/openvswitch/actions.c b/net/openvswitch/actions.c index 6fcd7e2ca81f..eb3166986fd2 100644 --- a/net/openvswitch/actions.c +++ b/net/openvswitch/actions.c @@ -24,6 +24,7 @@ #include #include #include +#include #include =20 #include "datapath.h" @@ -1025,6 +1026,34 @@ static int dec_ttl_exception_handler(struct datapath= *dp, struct sk_buff *skb, return 0; } =20 +static int ovs_psample_packet(struct datapath *dp, struct sw_flow_key *key, + const struct sample_arg *arg, + struct sk_buff *skb) +{ + struct psample_group psample_group =3D {}; + struct psample_metadata md =3D {}; + struct vport *input_vport; + u32 rate; + + psample_group.group_num =3D arg->group_id; + psample_group.net =3D ovs_dp_get_net(dp); + + input_vport =3D ovs_vport_rcu(dp, key->phy.in_port); + if (!input_vport) + input_vport =3D ovs_vport_rcu(dp, OVSP_LOCAL); + + md.in_ifindex =3D input_vport->dev->ifindex; + md.user_cookie =3D arg->cookie_len ? &arg->cookie[0] : NULL; + md.user_cookie_len =3D arg->cookie_len; + md.trunc_size =3D skb->len; + + rate =3D arg->probability ? U32_MAX / arg->probability : 0; + + psample_sample_packet(&psample_group, skb, rate, &md); + + return 0; +} + /* When 'last' is true, sample() should always consume the 'skb'. * Otherwise, sample() should keep 'skb' intact regardless what * actions are executed within sample(). @@ -1033,11 +1062,12 @@ static int sample(struct datapath *dp, struct sk_bu= ff *skb, struct sw_flow_key *key, const struct nlattr *attr, bool last) { - struct nlattr *actions; + const struct sample_arg *arg; struct nlattr *sample_arg; int rem =3D nla_len(attr); - const struct sample_arg *arg; + struct nlattr *actions; bool clone_flow_key; + int ret; =20 /* The first action is always 'OVS_SAMPLE_ATTR_ARG'. */ sample_arg =3D nla_data(attr); @@ -1051,9 +1081,20 @@ static int sample(struct datapath *dp, struct sk_buf= f *skb, return 0; } =20 - clone_flow_key =3D !arg->exec; - return clone_execute(dp, skb, key, 0, actions, rem, last, - clone_flow_key); + if (arg->flags & OVS_SAMPLE_ARG_FLAG_PSAMPLE) { + ret =3D ovs_psample_packet(dp, key, arg, skb); + if (ret) + return ret; + } + + if (nla_ok(actions, rem)) { + clone_flow_key =3D !(arg->flags & OVS_SAMPLE_ARG_FLAG_EXEC); + ret =3D clone_execute(dp, skb, key, 0, actions, rem, last, + clone_flow_key); + } else if (last) { + ovs_kfree_skb_reason(skb, OVS_DROP_LAST_ACTION); + } + return ret; } =20 /* When 'last' is true, clone() should always consume the 'skb'. diff --git a/net/openvswitch/flow_netlink.c b/net/openvswitch/flow_netlink.c index f224d9bcea5e..1a348d3905fc 100644 --- a/net/openvswitch/flow_netlink.c +++ b/net/openvswitch/flow_netlink.c @@ -2561,6 +2561,9 @@ static int __ovs_nla_copy_actions(struct net *net, co= nst struct nlattr *attr, u32 mpls_label_count, bool log, u32 depth); =20 +static int copy_action(const struct nlattr *from, + struct sw_flow_actions **sfa, bool log); + static int validate_and_copy_sample(struct net *net, const struct nlattr *= attr, const struct sw_flow_key *key, struct sw_flow_actions **sfa, @@ -2569,10 +2572,10 @@ static int validate_and_copy_sample(struct net *net= , const struct nlattr *attr, u32 depth) { const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1]; - const struct nlattr *probability, *actions; + const struct nlattr *probability, *actions, *group, *cookie; + struct sample_arg arg =3D {}; const struct nlattr *a; int rem, start, err; - struct sample_arg arg; =20 memset(attrs, 0, sizeof(attrs)); nla_for_each_nested(a, attr, rem) { @@ -2589,7 +2592,19 @@ static int validate_and_copy_sample(struct net *net,= const struct nlattr *attr, return -EINVAL; =20 actions =3D attrs[OVS_SAMPLE_ATTR_ACTIONS]; - if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN)) + if (actions && (!nla_len(actions) || nla_len(actions) < NLA_HDRLEN)) + return -EINVAL; + + group =3D attrs[OVS_SAMPLE_ATTR_PSAMPLE_GROUP]; + if (group && nla_len(group) !=3D sizeof(u32)) + return -EINVAL; + + cookie =3D attrs[OVS_SAMPLE_ATTR_PSAMPLE_COOKIE]; + if (cookie && + (!group || nla_len(cookie) > OVS_PSAMPLE_COOKIE_MAX_SIZE)) + return -EINVAL; + + if (!group && !actions) return -EINVAL; =20 /* validation done, copy sample action. */ @@ -2608,7 +2623,19 @@ static int validate_and_copy_sample(struct net *net,= const struct nlattr *attr, * If the sample is the last action, it can always be excuted * rather than deferred. */ - arg.exec =3D last || !actions_may_change_flow(actions); + if (actions && (last || !actions_may_change_flow(actions))) + arg.flags |=3D OVS_SAMPLE_ARG_FLAG_EXEC; + + if (group) { + arg.flags |=3D OVS_SAMPLE_ARG_FLAG_PSAMPLE; + arg.group_id =3D nla_get_u32(group); + } + + if (cookie) { + memcpy(&arg.cookie[0], nla_data(cookie), nla_len(cookie)); + arg.cookie_len =3D nla_len(cookie); + } + arg.probability =3D nla_get_u32(probability); =20 err =3D ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg), @@ -2616,12 +2643,13 @@ static int validate_and_copy_sample(struct net *net= , const struct nlattr *attr, if (err) return err; =20 - err =3D __ovs_nla_copy_actions(net, actions, key, sfa, - eth_type, vlan_tci, mpls_label_count, log, - depth + 1); - - if (err) - return err; + if (actions) { + err =3D __ovs_nla_copy_actions(net, actions, key, sfa, + eth_type, vlan_tci, + mpls_label_count, log, depth + 1); + if (err) + return err; + } =20 add_nested_action_end(*sfa, start); =20 @@ -3553,20 +3581,38 @@ static int sample_action_to_attr(const struct nlatt= r *attr, goto out; } =20 - ac_start =3D nla_nest_start_noflag(skb, OVS_SAMPLE_ATTR_ACTIONS); - if (!ac_start) { - err =3D -EMSGSIZE; - goto out; + if (arg->flags & OVS_SAMPLE_ARG_FLAG_PSAMPLE) { + if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PSAMPLE_GROUP, + arg->group_id)) { + err =3D -EMSGSIZE; + goto out; + } + + if (arg->cookie_len && + nla_put(skb, OVS_SAMPLE_ATTR_PSAMPLE_COOKIE, + arg->cookie_len, &arg->cookie[0])) { + err =3D -EMSGSIZE; + goto out; + } } =20 - err =3D ovs_nla_put_actions(actions, rem, skb); + if (nla_ok(actions, rem)) { + ac_start =3D nla_nest_start_noflag(skb, OVS_SAMPLE_ATTR_ACTIONS); + if (!ac_start) { + err =3D -EMSGSIZE; + goto out; + } + err =3D ovs_nla_put_actions(actions, rem, skb); + } =20 out: if (err) { - nla_nest_cancel(skb, ac_start); + if (ac_start) + nla_nest_cancel(skb, ac_start); nla_nest_cancel(skb, start); } else { - nla_nest_end(skb, ac_start); + if (ac_start) + nla_nest_end(skb, ac_start); nla_nest_end(skb, start); } =20 --=20 2.44.0 From nobody Sat May 18 07:09:03 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 B61D215F409 for ; Wed, 24 Apr 2024 13:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966900; cv=none; b=rNmvvB9ad4EK6uz1+YPhagmVzIxC0N0LH5Go2OwNrJHDFrhrNsLgMmB+LC76nHqsH2s0tGbGBN9fOx0Kn+Zoo8bdIL3V4hEkQallL20AgIB06QQg0ETo4k/dg+Lda3F2r9Dt+NinGkp678xayrz482O8A7HYKLDUiERoCBcbMcE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966900; c=relaxed/simple; bh=mCBdU1/Y+iR5BPM7dVWuIGMSVDs5uBgokHyUvZNcpYU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KWMW4G5nBGuTDQqyScYMUTkszKjz7j4uEdbDblELYRgEjd1+3bom4xZ8OoIzhLyumYO8HPNHT1dS4ucG4Ii8GFtk00HXK8iyTQLrVq4ajEUpPRGm+Bw6z/0jbkY/kp7rPO9IV1YnZz4lE1uGX2r5OOkH1H4XhpUYM3d/+KNT5L4= 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=Je8uenkB; arc=none smtp.client-ip=170.10.133.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="Je8uenkB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713966897; 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=z8Yzpk3KTnFdMHRu4agPz/+OCowB+XjwxhNrbJUlANE=; b=Je8uenkBT/1lxDYl3GRx0Q82aXUMwiYo41v8StN2qNrpq7S8YFpmKFfUy0QNwhWcaPScuv eQo3bSgZir5GmhEB0B/+lnqe637AudijjYafODMLJ9xccix+jWJXkJGn+CikuUaURabssp 49QsQg1L0JoThWyUhAfMOgTkZpWltnc= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-436-gVewJ_sWPXmhNDPqLA0nCg-1; Wed, 24 Apr 2024 09:54:54 -0400 X-MC-Unique: gVewJ_sWPXmhNDPqLA0nCg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id DC6F43C02B49; Wed, 24 Apr 2024 13:54:53 +0000 (UTC) Received: from antares.redhat.com (unknown [10.39.193.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id ACE0E1C060D0; Wed, 24 Apr 2024 13:54:51 +0000 (UTC) From: Adrian Moreno To: netdev@vger.kernel.org Cc: aconole@redhat.com, echaudro@redhat.com, horms@kernel.org, i.maximets@ovn.org, Adrian Moreno , Pravin B Shelar , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan , 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 Message-ID: <20240424135109.3524355-8-amorenoz@redhat.com> In-Reply-To: <20240424135109.3524355-1-amorenoz@redhat.com> References: <20240424135109.3524355-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.4.1 on 10.11.54.7 Content-Type: text/plain; charset="utf-8" Add sample action support to ovs-dpctl.py. Signed-off-by: Adrian Moreno --- .../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/t= esting/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 =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]) @@ -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"), ) =20 + 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"), + ("OVS_SAMPLE_ATTR_PSAMPLE_GROUP", "uint32"), + ("OVS_SAMPLE_ATTR_PSAMPLE_COOKIE", "array(uint8)"), + ) + + def dpstr(self, more=3DFalse): + args =3D [] + + args.append("sample=3D{:.2f}%".format( + 100 * self.get_attr("OVS_SAMPLE_ATTR_PROBABILITY") / + UINT32_MAX)) + + group =3D self.get_attr("OVS_SAMPLE_ATTR_PSAMPLE_GROUP") + cookie =3D self.get_attr("OVS_SAMPLE_ATTR_PSAMPLE_COOKIE") + actions =3D self.get_attr("OVS_SAMPLE_ATTR_ACTIONS") + + if group: + args.append("group_id=3D%d" % group) + if cookie: + args.append("cookie=3D%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 =3D ovsactions() + parsed_len =3D subacts.parse(actstr) + return subacts, parsed_len + + def percent_to_rate(percent): + percent =3D 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_action= s), + ): + if not actstr.startswith(key): + continue + + actstr =3D 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 =3D actstr[0] + actstr =3D actstr[1:] + if delim =3D=3D "=3D": + pos =3D strcspn(actstr, ",)") + datum =3D func(actstr[:pos]) + elif delim =3D=3D "(": + datum, pos =3D func(actstr) + + self["attrs"].append([attr, datum]) + actstr =3D actstr[pos:] + actstr =3D actstr[strspn(actstr, ", ") :] + + if actstr[0] !=3D ")": + raise ValueError("Action str: '%s' unbalanced" % actstr) + + return actstr[1:] + + class ctact(nla): nla_flags =3D NLA_F_NESTED =20 @@ -637,6 +724,13 @@ 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 + + actstr =3D actstr[strspn(actstr, ", ") :] while parencount > 0: parencount -=3D 1 --=20 2.44.0 From nobody Sat May 18 07:09:03 2024 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 CEE2D15FA9C for ; Wed, 24 Apr 2024 13:55:01 +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=1713966903; cv=none; b=tBUqF76gVvrtSLJUiJG/RrGPJ8gDP/rStP7d3ot8Zd57++hU8gtDOhqETINs853wKhgY44SHXQL9rS1OOEXCiLTPBw+i3iAEIB5Ce/OOHyFkmqkhCfitBFyPLOFtILCw1AUsrYMj1aF/4aHaD0x+07H3TaOAZlPf63iA1FoIeCI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713966903; c=relaxed/simple; bh=XeNqMhdDvrQ3e9k2Sc71pAHggkwcCI7HMO1vYqq4+y4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=m2r3gsZKlV4foMT7iP6Vxr1yO02LEjz6BapuDTQ7mRVNesiX8fQEyzTWZ/APwCosQq8U5GU4ciDvMtDlH5LcpZgL7HcJ/mTvWj+dNoGRy3Z1wwn96W5TJzNsdepjZwOiw4zxumh18yTu9xznwuUXXEK1r2vlEqb7JSzYXx9oUak= 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=DjIo36PE; 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="DjIo36PE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713966900; 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=NRfG44Ph4wnW5G/uRG2RMT/4P6AMh9CGQOP3sRR/P0s=; b=DjIo36PEbE1vQn2JArJLWOkvAqv78Ke+ox4jvpZV93feDSuBqD6bEL/0oDgpt/KUp80DA4 nefmyY4SyLrU0eaZl+GzSO3x0Ji8vLjqUiMtq768PyH+6Vw5JAu8SXhAfp7jVXfrnbTrxo zPIWFql3T7rf3xEMu/5Sh9oDB9NDlnA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-411-aHlfTTb1N_y3PKS5ssXMKg-1; Wed, 24 Apr 2024 09:54:57 -0400 X-MC-Unique: aHlfTTb1N_y3PKS5ssXMKg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id BFC47823F1D; Wed, 24 Apr 2024 13:54:56 +0000 (UTC) Received: from antares.redhat.com (unknown [10.39.193.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8AB1C1C060D0; Wed, 24 Apr 2024 13:54:54 +0000 (UTC) From: Adrian Moreno To: netdev@vger.kernel.org Cc: aconole@redhat.com, echaudro@redhat.com, horms@kernel.org, i.maximets@ovn.org, Adrian Moreno , Pravin B Shelar , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan , dev@openvswitch.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next 8/8] selftests: openvswitch: add psample test Date: Wed, 24 Apr 2024 15:50:55 +0200 Message-ID: <20240424135109.3524355-9-amorenoz@redhat.com> In-Reply-To: <20240424135109.3524355-1-amorenoz@redhat.com> References: <20240424135109.3524355-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.4.1 on 10.11.54.7 Content-Type: text/plain; charset="utf-8" Add a test to verify sampling packets via psample works. In order to do that, create a subcommand in ovs-dpctl.py to listen to on the psample multicast group and print samples. In order to also test simultaneous sFlow and psample actions, add missing parsing support for "userspace" action (via refactoring the one in sample). Signed-off-by: Adrian Moreno --- .../selftests/net/openvswitch/openvswitch.sh | 97 +++++++++- .../selftests/net/openvswitch/ovs-dpctl.py | 167 ++++++++++++++---- 2 files changed, 231 insertions(+), 33 deletions(-) diff --git a/tools/testing/selftests/net/openvswitch/openvswitch.sh b/tools= /testing/selftests/net/openvswitch/openvswitch.sh index 5cae53543849..7a2307a384a9 100755 --- a/tools/testing/selftests/net/openvswitch/openvswitch.sh +++ b/tools/testing/selftests/net/openvswitch/openvswitch.sh @@ -20,7 +20,8 @@ tests=3D" nat_related_v4 ip4-nat-related: ICMP related matches work with SNAT netlink_checks ovsnl: validate netlink attrs and settings upcall_interfaces ovs: test the upcall interfaces - drop_reason drop: test drop reasons are emitted" + drop_reason drop: test drop reasons are emitted + psample psample: Sampling packets with psample" =20 info() { [ $VERBOSE =3D 0 ] || echo $* @@ -170,6 +171,19 @@ ovs_drop_reason_count() return `echo "$perf_output" | grep "$pattern" | wc -l` } =20 +ovs_test_flow_fails () { + ERR_MSG=3D"Flow actions may not be safe on all matching packets" + + PRE_TEST=3D$(dmesg | grep -c "${ERR_MSG}") + ovs_add_flow $@ &> /dev/null $@ && return 1 + POST_TEST=3D$(dmesg | grep -c "${ERR_MSG}") + + if [ "$PRE_TEST" =3D=3D "$POST_TEST" ]; then + return 1 + fi + return 0 +} + usage() { echo echo "$0 [OPTIONS] [TEST]..." @@ -184,6 +198,87 @@ usage() { exit 1 } =20 + +# psample test +# - samples packets with psample +test_psample() { + sbx_add "test_psample" || return $? + + # Add a datapath with per-vport dispatching. + ovs_add_dp "test_psample" psample -V 2:1 || return 1 + + info "create namespaces" + ovs_add_netns_and_veths "test_psample" "psample" \ + client c0 c1 172.31.110.10/24 -u || return 1 + ovs_add_netns_and_veths "test_psample" "psample" \ + server s0 s1 172.31.110.20/24 -u || return 1 + + # Check if psample actions can be configured. + ovs_add_flow "test_psample" psample \ + 'in_port(1),eth(),eth_type(0x0806),arp()' 'sample(sample=3D100%,group_id= =3D1,cookie=3D0102)' + if [ $? =3D=3D 1 ]; then + info "no support for psample - skipping" + ovs_exit_sig + return $ksft_skip + fi + + ovs_del_flows "test_psample" psample + + # Allow ARP + ovs_add_flow "test_psample" psample \ + 'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1 + ovs_add_flow "test_psample" psample \ + 'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1 + + # Test action verification. + OLDIFS=3D$IFS + IFS=3D'*' + min_key=3D'in_port(1),eth(),eth_type(0x800),ipv4()' + for testcase in \ + "cookie to large"*"sample(sample=3D100%,group_id=3D1,cookie=3D1615141312= 111009080706050403020100)" \ + "no group or action"*"sample(sample=3D100%)" \ + "no group or action with cookie"*"sample(sample=3D100%,cookie=3Ddeadbeef= )"; + do + set -- $testcase; + ovs_test_flow_fails "test_psample" psample $min_key $2 + if [ $? =3D=3D 1 ]; then + info "failed - $1" + return 1 + fi + done + IFS=3D$OLDIFS + + # Sample all traffic. In this case the sample action only has psample + # arguments. + ovs_add_flow "test_psample" psample \ + "in_port(1),eth(),eth_type(0x0800),ipv4(src=3D172.31.110.10,proto=3D1),ic= mp()" "sample(sample=3D100%,group_id=3D1,cookie=3Dc0ffee),2" + + # Sample all traffic. In this case the sample action has both psample + # arguments and an upcall emulating simultaneous psample and + # sFlow / IPFIX. + nlpid=3D$(grep -E "listening on upcall packet handler" $ovs_dir/s0.out | = cut -d ":" -f 2 | tr -d ' ') + ovs_add_flow "test_psample" psample \ + "in_port(2),eth(),eth_type(0x0800),ipv4(src=3D172.31.110.20,proto=3D1),ic= mp()" "sample(sample=3D100%,group_id=3D2,cookie=3Deeff0c,actions(userspace(= pid=3D${nlpid},userdata=3Deeff0c))),1" + + # Record psample data. + python3 $ovs_base/ovs-dpctl.py psample >$ovs_dir/psample.out 2>$ovs_dir/= psample.err & + pid=3D$! + on_exit "ovs_sbx test_psample kill -TERM $pid 2>/dev/null" + + # Send a single ping. + sleep 1 + ovs_sbx "test_psample" ip netns exec client ping -I c1 172.31.110.20 -c 1= || return 1 + sleep 1 + + # We should have received one userspace action upcall and 2 psample packe= ts. + grep -E "userspace action command" $ovs_dir/s0.out >/dev/null 2>&1 || ret= urn 1 + + grep -E "rate:1,group:1,cookie:c0ffee" $ovs_dir/psample.out >/dev/null 2>= &1 || return 1 + grep -E "rate:1,group:2,cookie:eeff0c" $ovs_dir/psample.out >/dev/null 2>= &1 || return 1 + + return 0 +} + # drop_reason test # - drop packets and verify the right drop reason is reported test_drop_reason() { diff --git a/tools/testing/selftests/net/openvswitch/ovs-dpctl.py b/tools/t= esting/selftests/net/openvswitch/ovs-dpctl.py index 3a2dddc57e42..2fb5bcfe9c36 100644 --- a/tools/testing/selftests/net/openvswitch/ovs-dpctl.py +++ b/tools/testing/selftests/net/openvswitch/ovs-dpctl.py @@ -27,8 +27,10 @@ try: from pyroute2.netlink import genlmsg from pyroute2.netlink import nla from pyroute2.netlink import nlmsg_atoms - from pyroute2.netlink.exceptions import NetlinkError + from pyroute2.netlink.event import EventSocket from pyroute2.netlink.generic import GenericNetlinkSocket + from pyroute2.netlink.nlsocket import Marshal + from pyroute2.netlink.exceptions import NetlinkError import pyroute2 =20 except ModuleNotFoundError: @@ -269,6 +271,47 @@ def parse_extract_field( return str_skipped, data =20 =20 +def parse_attributes(actstr, attributes): + """Parses actstr according to attribute description. attributes must be + a list of tuples (name, attribute, parse_func), e.g: + ("pid", OVS_USERSPACE_ATTR_PID, int) + + Returns a list of parsed attributes followed by the remaining string. + """ + attrs =3D [] + for (key, attr, func) in attributes: + if not actstr.startswith(key): + continue + + actstr =3D actstr[len(key) :] + + if not func: + attrs.append([attr]) + continue + + # The length of complex attributes cannot be determined + # beforehand and must be reported by the parsing func. + delim =3D actstr[0] + actstr =3D actstr[1:] + if delim =3D=3D "=3D": + pos =3D strcspn(actstr, ",)") + datum =3D func(actstr[:pos]) + elif delim =3D=3D "(": + datum, pos =3D func(actstr) + + attrs.append([attr, datum]) + actstr =3D actstr[pos:] + + if delim =3D=3D "(": + actstr =3D actstr[1:] + + actstr =3D actstr[strspn(actstr, ", ") :] + + if actstr[0] !=3D ")": + raise ValueError("Action str: '%s' unbalanced" % 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 @@ -357,41 +400,19 @@ class ovsactions(nla): percent =3D float(percent.strip('%')) return int(math.floor(UINT32_MAX * (percent / 100.0) + .5)) =20 - for (key, attr, func) in ( + attrs_desc =3D ( ("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_action= s), - ): - if not actstr.startswith(key): - continue - - actstr =3D 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 =3D actstr[0] - actstr =3D actstr[1:] - if delim =3D=3D "=3D": - pos =3D strcspn(actstr, ",)") - datum =3D func(actstr[:pos]) - elif delim =3D=3D "(": - datum, pos =3D func(actstr) - - self["attrs"].append([attr, datum]) - actstr =3D actstr[pos:] - actstr =3D actstr[strspn(actstr, ", ") :] - - if actstr[0] !=3D ")": - raise ValueError("Action str: '%s' unbalanced" % actstr) + ) =20 - return actstr[1:] + attrs, actstr =3D parse_attributes(actstr, attrs_desc) + for attr in attrs: + self["attrs"].append(attr) =20 + return actstr =20 class ctact(nla): nla_flags =3D NLA_F_NESTED @@ -521,6 +542,18 @@ class ovsactions(nla): print_str +=3D ")" return print_str =20 + def parse(self, actstr): + attrs_desc =3D ( + ("pid", "OVS_USERSPACE_ATTR_PID", int), + ("userdata", "OVS_USERSPACE_ATTR_USERDATA", + lambda x: list(bytearray.fromhex(x))), + ("egress_tun_port", "OVS_USERSPACE_ATTR_EGRESS_TUN_PORT", = int) + ) + attrs, actstr =3D parse_attributes(actstr, attrs_desc) + for attr in attrs: + self["attrs"].append(attr) + return actstr + def dpstr(self, more=3DFalse): print_str =3D "" =20 @@ -730,6 +763,11 @@ class ovsactions(nla): self["attrs"].append(["OVS_ACTION_ATTR_SAMPLE", sampleact]) parsed =3D True =20 + elif parse_starts_block(actstr, "userspace(", False): + uact =3D self.userspace() + actstr =3D uact.parse(actstr[len("userpsace(") : ]) + self["attrs"].append(["OVS_ACTION_ATTR_USERSPACE", uact]) + parsed =3D True =20 actstr =3D actstr[strspn(actstr, ", ") :] while parencount > 0: @@ -2112,10 +2150,70 @@ class OvsFlow(GenericNetlinkSocket): print("MISS upcall[%d/%s]: %s" % (seq, pktpres, keystr), flush=3DT= rue) =20 def execute(self, packetmsg): - print("userspace execute command") + print("userspace execute command", flush=3DTrue) =20 def action(self, packetmsg): - print("userspace action command") + print("userspace action command", flush=3DTrue) + + +class psample_sample(genlmsg): + nla_map =3D ( + ("PSAMPLE_ATTR_IIFINDEX", "none"), + ("PSAMPLE_ATTR_OIFINDEX", "none"), + ("PSAMPLE_ATTR_ORIGSIZE", "none"), + ("PSAMPLE_ATTR_SAMPLE_GROUP", "uint32"), + ("PSAMPLE_ATTR_GROUP_SEQ", "none"), + ("PSAMPLE_ATTR_SAMPLE_RATE", "uint32"), + ("PSAMPLE_ATTR_DATA", "array(uint8)"), + ("PSAMPLE_ATTR_GROUP_REFCOUNT", "none"), + ("PSAMPLE_ATTR_TUNNEL", "none"), + ("PSAMPLE_ATTR_PAD", "none"), + ("PSAMPLE_ATTR_OUT_TC", "none"), + ("PSAMPLE_ATTR_OUT_TC_OCC", "none"), + ("PSAMPLE_ATTR_LATENCY", "none"), + ("PSAMPLE_ATTR_TIMESTAMP", "none"), + ("PSAMPLE_ATTR_PROTO", "none"), + ("PSAMPLE_ATTR_USER_COOKIE", "array(uint8)"), + ) + + def dpstr(self): + fields =3D [] + data =3D "" + for (attr, value) in self["attrs"]: + if attr =3D=3D "PSAMPLE_ATTR_SAMPLE_GROUP": + fields.append("group:%d" % value) + if attr =3D=3D "PSAMPLE_ATTR_SAMPLE_RATE": + fields.append("rate:%d" % value) + if attr =3D=3D "PSAMPLE_ATTR_USER_COOKIE": + value =3D "".join(format(x, "02x") for x in value) + fields.append("cookie:%s" % value) + if attr =3D=3D "PSAMPLE_ATTR_DATA" and len(value) > 0: + data =3D "data:%s" % "".join(format(x, "02x") for x in val= ue) + + return ("%s %s" % (",".join(fields), data)).strip() + + +class psample_msg(Marshal): + PSAMPLE_CMD_SAMPLE =3D 0 + PSAMPLE_CMD_GET_GROUP =3D 1 + PSAMPLE_CMD_NEW_GROUP =3D 2 + PSAMPLE_CMD_DEL_GROUP =3D 3 + PSAMPLE_CMD_SET_FILTER =3D 4 + msg_map =3D {PSAMPLE_CMD_SAMPLE: psample_sample} + + +class Psample(EventSocket): + genl_family =3D "psample" + mcast_groups =3D ["packets"] + marshal_class =3D psample_msg + + def read_samples(self): + while True: + try: + for msg in self.get(): + print(msg.dpstr(), flush=3DTrue) + except NetlinkError as ne: + raise ne =20 =20 def print_ovsdp_full(dp_lookup_rep, ifindex, ndb=3DNDB(), vpl=3DOvsVport()= ): @@ -2175,7 +2273,7 @@ def main(argv): help=3D"Increment 'verbose' output counter.", default=3D0, ) - subparsers =3D parser.add_subparsers() + subparsers =3D parser.add_subparsers(dest=3D"subcommand") =20 showdpcmd =3D subparsers.add_parser("show") showdpcmd.add_argument( @@ -2232,6 +2330,8 @@ def main(argv): delfscmd =3D subparsers.add_parser("del-flows") delfscmd.add_argument("flsbr", help=3D"Datapath name") =20 + subparsers.add_parser("psample") + args =3D parser.parse_args() =20 if args.verbose > 0: @@ -2246,6 +2346,9 @@ def main(argv): =20 sys.setrecursionlimit(100000) =20 + if args.subcommand =3D=3D "psample": + Psample().read_samples() + if hasattr(args, "showdp"): found =3D False for iface in ndb.interfaces: --=20 2.44.0