From nobody Sat Feb 7 15:10:36 2026 Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (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 47A2218DB35; Mon, 28 Apr 2025 08:18:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745828306; cv=none; b=VCpncUNX1RJhyWaVUyEZZgADQ1513OwK59tQiROAnGhYHDF5tKAaDAbgelYhoLHIEZ0wK/UOrZ3iUYNmQHaJN4E7iSj2Bi4l6dwQEJ5RY8M1hnmv01VNvGHWHmsvhmalzlFyqFG7Rp31qQTms0RmB1XtQxkKtD5Rc11WiH6NkXs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745828306; c=relaxed/simple; bh=IMJE7V9CVXOWAC+XyzS2qXoKTYeaPy4ubI1JKh8jn9o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=haB6V2hdvHIep9dbHPH9lPmZTy30exe/AQpGN+yLj5ZhN1+3kYWtA1J5Hcs5NOAaktGLQSFDUdUuXzsTRQljNrFxDGEH7ddf2Kq3b5v4ajskpq6kzLouUlKojMo/hF0gVgJkfTyg+/fRBY7kucP5TaiONx3xkwat/lMjoUAvq74= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=ZKeJ9jKB; arc=none smtp.client-ip=91.218.175.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="ZKeJ9jKB" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1745828301; 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=La6440s7eHKYWjOaUhORIlr3h2SS6qaflsCpheO/PtQ=; b=ZKeJ9jKBrWm2iWByTW0FEsf9ZQEJ1C/gXyVtG0KS80uWaAxGbGTAo3Zexb3Oip/tFP3rjV IvQWM+GcZ56ws0qFC++XnwZDnGV6p3/NkspAxOqoqo1x8tg8ui/+aw+djVZALbiLuwcI3A h18sHpm4TBlncQO8Gb4fihG733zEYBo= From: Jiayuan Chen To: bpf@vger.kernel.org Cc: mrpre@163.com, Jiayuan Chen , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jonathan Corbet , Jakub Sitnicki , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Kuniyuki Iwashima , Willem de Bruijn , Mykola Lysenko , Shuah Khan , Jiapeng Chong , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v1 1/3] bpf, sockmap: Introduce a new kfunc for sockmap Date: Mon, 28 Apr 2025 16:16:52 +0800 Message-ID: <20250428081744.52375-2-jiayuan.chen@linux.dev> In-Reply-To: <20250428081744.52375-1-jiayuan.chen@linux.dev> References: <20250428081744.52375-1-jiayuan.chen@linux.dev> 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-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Since the helper list is effectively frozen and the existing helpers cannot be extended, we add a new kfunc instead which simply set the redir_cpu to psock. The new kfunc is used to set redir_cpu to psock. All these changes conform to the kfuncs.rst documentation. Signed-off-by: Jiayuan Chen --- Documentation/bpf/map_sockmap.rst | 14 +++++++++++ include/linux/skmsg.h | 3 +++ kernel/bpf/btf.c | 3 +++ net/core/skmsg.c | 1 + net/core/sock_map.c | 39 +++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+) diff --git a/Documentation/bpf/map_sockmap.rst b/Documentation/bpf/map_sock= map.rst index 2d630686a00b..eca3dfc1c85f 100644 --- a/Documentation/bpf/map_sockmap.rst +++ b/Documentation/bpf/map_sockmap.rst @@ -212,6 +212,20 @@ following cases: =20 Returns 0 =20 +bpf_sk_skb_set_redirect_cpu() +^^^^^^^^^^^^^^^^^^^^^^ +.. code-block:: c + + int bpf_sk_skb_set_redirect_cpu(struct __sk_buff *s, int redir_cpu) + +This kfunc ``bpf_sk_skb_set_redirect_cpu()`` is available to +``BPF_PROG_TYPE_SK_SKB`` BPF programs. It sets the CPU affinity, allowing = the +sockmap packet redirecting process to run on the specified CPU as much as +possible, helping users reduce the interference between the sockmap redire= cting +background thread and other threads. + +Returns 0 on success, or a negative error in case of failure. + bpf_msg_cork_bytes() ^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: c diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h index 0b9095a281b8..b888481a845d 100644 --- a/include/linux/skmsg.h +++ b/include/linux/skmsg.h @@ -16,6 +16,8 @@ #define MAX_MSG_FRAGS MAX_SKB_FRAGS #define NR_MSG_FRAG_IDS (MAX_MSG_FRAGS + 1) =20 +#define BPF_SK_REDIR_CPU_UNSET -1 + enum __sk_action { __SK_DROP =3D 0, __SK_PASS, @@ -86,6 +88,7 @@ struct sk_psock { u32 apply_bytes; u32 cork_bytes; u32 eval; + s32 redir_cpu; bool redir_ingress; /* undefined if sk_redir is null */ struct sk_msg *cork; struct sk_psock_progs progs; diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index a91822bae043..2a8f59e2c639 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -219,6 +219,7 @@ enum btf_kfunc_hook { BTF_KFUNC_HOOK_LWT, BTF_KFUNC_HOOK_NETFILTER, BTF_KFUNC_HOOK_KPROBE, + BTF_KFUNC_HOOK_SK_MSG, BTF_KFUNC_HOOK_MAX, }; =20 @@ -8649,6 +8650,8 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_= type prog_type) return BTF_KFUNC_HOOK_SCHED_ACT; case BPF_PROG_TYPE_SK_SKB: return BTF_KFUNC_HOOK_SK_SKB; + case BPF_PROG_TYPE_SK_MSG: + return BTF_KFUNC_HOOK_SK_MSG; case BPF_PROG_TYPE_SOCKET_FILTER: return BTF_KFUNC_HOOK_SOCKET_FILTER; case BPF_PROG_TYPE_LWT_OUT: diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 276934673066..292752c783b5 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -741,6 +741,7 @@ struct sk_psock *sk_psock_init(struct sock *sk, int nod= e) psock->saved_destroy =3D prot->destroy; psock->saved_close =3D prot->close; psock->saved_write_space =3D sk->sk_write_space; + psock->redir_cpu =3D BPF_SK_REDIR_CPU_UNSET; =20 INIT_LIST_HEAD(&psock->link); spin_lock_init(&psock->link_lock); diff --git a/net/core/sock_map.c b/net/core/sock_map.c index 82a14f131d00..9f1e531a3807 100644 --- a/net/core/sock_map.c +++ b/net/core/sock_map.c @@ -701,6 +701,45 @@ const struct bpf_func_proto bpf_msg_redirect_map_proto= =3D { .arg4_type =3D ARG_ANYTHING, }; =20 +__bpf_kfunc_start_defs(); + +__bpf_kfunc int bpf_sk_skb_set_redirect_cpu(struct __sk_buff *s, int redir= _cpu) +{ + struct sk_buff *skb =3D (struct sk_buff *)s; + struct sock *sk =3D skb->sk; + struct sk_psock *psock; + + WARN_ON_ONCE(!rcu_read_lock_held()); + + if (!sk || redir_cpu >=3D num_possible_cpus()) + return -EINVAL; + + psock =3D sk_psock(sk); + if (!psock) + return -ENOENT; + + psock->redir_cpu =3D redir_cpu; + return 0; +} + +__bpf_kfunc_end_defs(); + +BTF_KFUNCS_START(bpf_sk_sockmap_kfunc_ids) +BTF_ID_FLAGS(func, bpf_sk_skb_set_redirect_cpu) +BTF_KFUNCS_END(bpf_sk_sockmap_kfunc_ids) + +static const struct btf_kfunc_id_set bpf_sk_sockmap_kfunc_set =3D { + .owner =3D THIS_MODULE, + .set =3D &bpf_sk_sockmap_kfunc_ids, +}; + +static int init_sockmap_subsystem(void) +{ + return register_btf_kfunc_id_set(BPF_PROG_TYPE_SK_SKB, &bpf_sk_sockmap_kf= unc_set); +} + +late_initcall(init_sockmap_subsystem); + struct sock_map_seq_info { struct bpf_map *map; struct sock *sk; --=20 2.47.1 From nobody Sat Feb 7 15:10:36 2026 Received: from out-179.mta0.migadu.com (out-179.mta0.migadu.com [91.218.175.179]) (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 B428825E46C; Mon, 28 Apr 2025 08:18:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745828327; cv=none; b=OAtY0oi7YEQkRQnXaRKPUiROsEhHvF98WKQrq2L9nfMF+QbnJDINV3icWYiwTMh+ozTH4T1qDawZyTF6LpSTExGj2z2Nxu0QsNPDP/xvr1042w+0DRTOaph6fIT396FCxCv7N65Izo8/euQZVMZlsGE6SFlOkwJTFwutpjiaGsc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745828327; c=relaxed/simple; bh=shrVW23MJmXPTGr7Fi7hfalJrHhCvaQGn7EfPJfZHaM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SutkgpnJJqvrMasdS6TE2cVyfUjGEHrBQjilQECXJ2qT+WN1C0twIEkHs3Gl8Zn2Pe9uoWQW7uQfyFr2jbCOrCZ5n27k44ufwHsvPhB1YZRIvownL867W474U2zOb53nrDF9l7m+8Q6mUHBysLnwLMU68kc0eYXimEBJh++VNFM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=BgXCvYfE; arc=none smtp.client-ip=91.218.175.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="BgXCvYfE" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1745828323; 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=lDD4f3PJohVRbIgegKL4gSaxyAoWqWwnyjgGj9h1ygw=; b=BgXCvYfEYOgYlLJ1CvN2ual2TU/chQyaWi5wpad8tR3nAaFu7FsOpzpDnCkM1r5iD1nYuz DEfR8J8CpxP5bRA4HrjZeSA3QvIzZWgzZ7QGvQgyQYNJBZb733wqORpCqt/GeuuQ5r/AfA DEUcrhF7AxAa0GiFgJDIMCP0TT/DpIw= From: Jiayuan Chen To: bpf@vger.kernel.org Cc: mrpre@163.com, Jiayuan Chen , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jonathan Corbet , Jakub Sitnicki , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Kuniyuki Iwashima , Willem de Bruijn , Mykola Lysenko , Shuah Khan , Jiapeng Chong , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v1 2/3] bpf, sockmap: Affinitize workqueue to a specific CPU Date: Mon, 28 Apr 2025 16:16:53 +0800 Message-ID: <20250428081744.52375-3-jiayuan.chen@linux.dev> In-Reply-To: <20250428081744.52375-1-jiayuan.chen@linux.dev> References: <20250428081744.52375-1-jiayuan.chen@linux.dev> 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-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Introduce a sk_psock_schedule_delayed_work() wrapper function, which calls schedule_delayed_work_on() to specify the CPU for running the workqueue if the BPF program has set the redirect CPU using bpf_sk_skb_set_redirect_cpu(). Otherwise, it falls back to the original logic. Signed-off-by: Jiayuan Chen --- include/linux/skmsg.h | 12 ++++++++++++ net/core/skmsg.c | 9 +++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h index b888481a845d..21c7dd47186f 100644 --- a/include/linux/skmsg.h +++ b/include/linux/skmsg.h @@ -396,6 +396,18 @@ static inline void sk_psock_report_error(struct sk_pso= ck *psock, int err) sk_error_report(sk); } =20 +static inline void sk_psock_schedule_delayed_work(struct sk_psock *psock, + int delay) +{ + s32 redir_cpu =3D psock->redir_cpu; + + if (redir_cpu !=3D BPF_SK_REDIR_CPU_UNSET) + schedule_delayed_work_on(redir_cpu, &psock->work, + delay); + else + schedule_delayed_work(&psock->work, delay); +} + struct sk_psock *sk_psock_init(struct sock *sk, int node); void sk_psock_stop(struct sk_psock *psock); =20 diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 292752c783b5..af00c09263a8 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -689,7 +689,7 @@ static void sk_psock_backlog(struct work_struct *work) * other work that might be here. */ if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) - schedule_delayed_work(&psock->work, 1); + sk_psock_schedule_delayed_work(psock, 1); goto end; } /* Hard errors break pipe and stop xmit. */ @@ -940,6 +940,7 @@ static int sk_psock_skb_redirect(struct sk_psock *from,= struct sk_buff *skb) sock_drop(from->sk, skb); return -EIO; } + psock_other->redir_cpu =3D from->redir_cpu; spin_lock_bh(&psock_other->ingress_lock); if (!sk_psock_test_state(psock_other, SK_PSOCK_TX_ENABLED)) { spin_unlock_bh(&psock_other->ingress_lock); @@ -949,7 +950,7 @@ static int sk_psock_skb_redirect(struct sk_psock *from,= struct sk_buff *skb) } =20 skb_queue_tail(&psock_other->ingress_skb, skb); - schedule_delayed_work(&psock_other->work, 0); + sk_psock_schedule_delayed_work(psock_other, 0); spin_unlock_bh(&psock_other->ingress_lock); return 0; } @@ -1027,7 +1028,7 @@ static int sk_psock_verdict_apply(struct sk_psock *ps= ock, struct sk_buff *skb, spin_lock_bh(&psock->ingress_lock); if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) { skb_queue_tail(&psock->ingress_skb, skb); - schedule_delayed_work(&psock->work, 0); + sk_psock_schedule_delayed_work(psock, 0); err =3D 0; } spin_unlock_bh(&psock->ingress_lock); @@ -1059,7 +1060,7 @@ static void sk_psock_write_space(struct sock *sk) psock =3D sk_psock(sk); if (likely(psock)) { if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) - schedule_delayed_work(&psock->work, 0); + sk_psock_schedule_delayed_work(psock, 0); write_space =3D psock->saved_write_space; } rcu_read_unlock(); --=20 2.47.1 From nobody Sat Feb 7 15:10:36 2026 Received: from out-173.mta0.migadu.com (out-173.mta0.migadu.com [91.218.175.173]) (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 0DBE92356BC for ; Mon, 28 Apr 2025 08:19:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745828353; cv=none; b=WXu1FKDqoZordvDgsF3ye12j2LFeooeJplWiIWgod/sgGh/h9kN3a0lSBRo10ny5h8sC/kb/jf8aM8mqt1OVHuxFLsS4NIzo/rAEccEPGkYPa8OfE/JlXkmjoxC/+4ARnjHNkaZ2cjUcSA8a7t6eZAZ0TmLKTbY+Qr5XMqyu7K4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745828353; c=relaxed/simple; bh=0sGaXTtD20xzhNz4rmeWyme7BvH88i16MX9d4W1Y3CQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ekMglyPJrSpsGA4u1+snH1PBiO8DS7XRZTQCmWFsqtXlVcpcz0AYyAFnyUrDIqrMPAwP//x3wK+cjcyGdcQ45FW+sdK31pBwON4CV+ivIir/VJJtw+6f49qV7ni5p11adplSm2PlZMAq5u9nqcfMiqKhIvNsaPfKhY7duSQvQbs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=mFJK0/iX; arc=none smtp.client-ip=91.218.175.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="mFJK0/iX" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1745828349; 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=Fg/btEvaUcqT4NM4dYYa4R9u9WjGLVmndHIgtbzlFXc=; b=mFJK0/iXSOt5fj3xjadJLjSr81Jzb3GynBBFsr5KHqT0x5plfJlh+zMBgV6u+BWLX6J/on Z1DQYEu83m7V578FTrWJZp3poMihriyfGZvrhL3orFlBxG7S2JNcZZI4eCKIVrvRQeXBfN tQVSe3DLCCJKPkD/jCVFYI8t/3rq36M= From: Jiayuan Chen To: bpf@vger.kernel.org Cc: mrpre@163.com, Jiayuan Chen , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jonathan Corbet , Jakub Sitnicki , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Kuniyuki Iwashima , Willem de Bruijn , Mykola Lysenko , Shuah Khan , Jiapeng Chong , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v1 3/3] selftest/bpf/benchs: Add cpu-affinity for sockmap bench Date: Mon, 28 Apr 2025 16:16:54 +0800 Message-ID: <20250428081744.52375-4-jiayuan.chen@linux.dev> In-Reply-To: <20250428081744.52375-1-jiayuan.chen@linux.dev> References: <20250428081744.52375-1-jiayuan.chen@linux.dev> 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-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Add cpu-affinity for sockmap bench. Also add no-verify args to avoid validating data for performance enhancements. Signed-off-by: Jiayuan Chen --- .../selftests/bpf/benchs/bench_sockmap.c | 35 +++++++++++++++++-- tools/testing/selftests/bpf/bpf_kfuncs.h | 6 ++++ .../selftests/bpf/progs/bench_sockmap_prog.c | 7 ++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/bpf/benchs/bench_sockmap.c b/tools/tes= ting/selftests/bpf/benchs/bench_sockmap.c index 8ebf563a67a2..e004a618822a 100644 --- a/tools/testing/selftests/bpf/benchs/bench_sockmap.c +++ b/tools/testing/selftests/bpf/benchs/bench_sockmap.c @@ -43,6 +43,8 @@ enum SOCKMAP_ARG_FLAG { ARG_FW_TX_VERDICT_INGRESS, ARG_FW_TX_VERDICT_EGRESS, ARG_CTL_RX_STRP, + ARG_CTL_CPU_AFFINITY, + ARG_CTL_NO_VERIFY, ARG_CONSUMER_DELAY_TIME, ARG_PRODUCER_DURATION, }; @@ -109,6 +111,8 @@ static struct socmap_ctx { int delay_consumer; int prod_run_time; int strp_size; + int cpu_affinity; + int skip_verify; } ctx =3D { .prod_send =3D 0, .user_read =3D 0, @@ -118,6 +122,8 @@ static struct socmap_ctx { .delay_consumer =3D 0, .prod_run_time =3D 0, .strp_size =3D 0, + .cpu_affinity =3D 0, + .skip_verify =3D 0, }; =20 static void bench_sockmap_prog_destroy(void) @@ -235,11 +241,18 @@ static int create_sockets(void) static void validate(void) { if (env.consumer_cnt !=3D 2 || env.producer_cnt !=3D 1 || - !env.affinity) + !env.affinity) { + fprintf(stderr, "argument '-c 2 -p 1 -a' is necessary\n"); goto err; + } + + if (!ctx.cpu_affinity && env.nr_cpus < 4) { + fprintf(stderr, "4 CPU are needed to test cpu-affinity\n"); + goto err; + } + return; err: - fprintf(stderr, "argument '-c 2 -p 1 -a' is necessary"); exit(1); } =20 @@ -327,6 +340,9 @@ static void setup(void) exit(1); } =20 + if (ctx.cpu_affinity) + ctx.skel->data->redir_cpu =3D 3; + if (create_sockets()) { fprintf(stderr, "create_net_mode error\n"); goto err; @@ -367,9 +383,12 @@ static void measure(struct bench_res *res) =20 static void verify_data(int *check_pos, char *buf, int rcv) { + if (ctx.skip_verify) + return; + for (int i =3D 0 ; i < rcv; i++) { if (buf[i] !=3D snd_data[(*check_pos) % DATA_REPEAT_SIZE]) { - fprintf(stderr, "verify data fail"); + fprintf(stderr, "verify data fail\n"); exit(1); } (*check_pos)++; @@ -553,6 +572,10 @@ static const struct argp_option opts[] =3D { "delay consumer start"}, { "producer-duration", ARG_PRODUCER_DURATION, "SEC", 0, "producer duration"}, + { "cpu-affinity", ARG_CTL_CPU_AFFINITY, NULL, 0, + "set cpu-affinity for sockmap backlog thread"}, + { "no-verify", ARG_CTL_NO_VERIFY, NULL, 0, + "skip data validation for performance enhancements"}, {}, }; =20 @@ -571,6 +594,12 @@ static error_t parse_arg(int key, char *arg, struct ar= gp_state *state) case ARG_CTL_RX_STRP: ctx.strp_size =3D strtol(arg, NULL, 10); break; + case ARG_CTL_CPU_AFFINITY: + ctx.cpu_affinity =3D 1; + break; + case ARG_CTL_NO_VERIFY: + ctx.skip_verify =3D 1; + break; default: return ARGP_ERR_UNKNOWN; } diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selft= ests/bpf/bpf_kfuncs.h index 8215c9b3115e..173329c5d034 100644 --- a/tools/testing/selftests/bpf/bpf_kfuncs.h +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h @@ -92,4 +92,10 @@ extern int bpf_set_dentry_xattr(struct dentry *dentry, c= onst char *name__str, const struct bpf_dynptr *value_p, int flags) __ksym __weak; extern int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name= __str) __ksym __weak; =20 +/* Description + * Set sockmap redir cpu + * Returns + * Error code + */ +extern int bpf_sk_skb_set_redirect_cpu(struct __sk_buff *skb, int redir_cp= u) __ksym; #endif diff --git a/tools/testing/selftests/bpf/progs/bench_sockmap_prog.c b/tools= /testing/selftests/bpf/progs/bench_sockmap_prog.c index 079bf3794b3a..dd1a11cb4f48 100644 --- a/tools/testing/selftests/bpf/progs/bench_sockmap_prog.c +++ b/tools/testing/selftests/bpf/progs/bench_sockmap_prog.c @@ -2,11 +2,15 @@ #include #include #include +#include +#include "bpf_kfuncs.h" =20 long process_byte =3D 0; int verdict_dir =3D 0; int dropped =3D 0; int pkt_size =3D 0; +int redir_cpu =3D -1; + struct { __uint(type, BPF_MAP_TYPE_SOCKMAP); __uint(max_entries, 20); @@ -33,6 +37,9 @@ int prog_skb_verdict(struct __sk_buff *skb) int one =3D 1; int ret =3D bpf_sk_redirect_map(skb, &sock_map_rx, one, verdict_dir); =20 + if (redir_cpu !=3D -1) + bpf_sk_skb_set_redirect_cpu(skb, redir_cpu); + if (ret =3D=3D SK_DROP) dropped++; __sync_fetch_and_add(&process_byte, skb->len); --=20 2.47.1