From nobody Sun Feb 8 05:41:24 2026 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (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 4FBD24A2E1B for ; Wed, 21 Jan 2026 15:18:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.84.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769008727; cv=none; b=eG9vdre+vnj9Xf9XrE/SMvWFvYqsl5+S6id273ICf/VUDr503uFD0itBUceVUQ0j8+jwaWQMhHz1WoyPsSF/1xU3cGkm1Sjy3xoUUVPXiQzsi4tCWtzSiW/1Ab95d2sdEbKuZMG/Hgj49+k5OuVYIi2aLm+M1Fn5NVAsjiFatgI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769008727; c=relaxed/simple; bh=s3LoNVyEK3LWLXQdT41NpN32UCJrj9eDAuKgVxcxjno=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HCFTm8sn5LWmjC2Dn7Lbp4ZXyNYsVCtgYltfgC8cCn0ZP2bFJHseUA8TuzEMV1LtLkK2iU9LCvAW/X8gYceTTTd2X4pE21xJg/KqPqV/09/7jKLplefrZuMp9q+8fdJ+tV1DpwKzgYCJfJ0VvXOBTw1C8tsVPmhkU7f1bMcbM94= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=EnY1FVYh; arc=none smtp.client-ip=185.246.84.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="EnY1FVYh" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id D8D961A29AA; Wed, 21 Jan 2026 15:18:43 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id AE23D6070A; Wed, 21 Jan 2026 15:18:43 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 00DBD119B169E; Wed, 21 Jan 2026 16:18:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1769008722; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=JKAJXd82I47jf+RsYKeETkBTanMheP0mUAVKhE8TaX4=; b=EnY1FVYhcA1SRmHJz88HJPqQIgvWH5GcbraGbd7mR1hyJBjC1HuxuEbaxUiUywOgkhPdp7 4jfleosG1QP3DC34g2LCQKPvaoJHbd5f4b4yzkKSSflx5yfbym4P8/MkNCVYPpGWYsPU66 o8ieFPapJS7O6VbB0pC5wcpN1NFgA85ryAlXmhOOBke+lOMngSSjoAkwqaXb3f7sDEgmwx ExTFFpDkcDcCs4vUSjTdZBBCdR31131E+wzAVTaSFlA4FxZi74K9iiyhaqoOdJyxKdzYzi wNkqzqYzIPu2JK/dIfZcSVSN0WM76T0+QW/IRdrbk2n/TN9uUrcaalm4uM9Umg== From: =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= Date: Wed, 21 Jan 2026 16:18:15 +0100 Subject: [PATCH bpf-next v2 2/4] bpf/selftests: add a few helpers for bpftool testing Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260121-bpftool-tests-v2-2-64edb47e91ae@bootlin.com> References: <20260121-bpftool-tests-v2-0-64edb47e91ae@bootlin.com> In-Reply-To: <20260121-bpftool-tests-v2-0-64edb47e91ae@bootlin.com> To: Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Quentin Monnet Cc: ebpf@linuxfoundation.org, Bastien Curutchet , Thomas Petazzoni , linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Alexis_Lothor=C3=A9_=28eBPF_Foundation=29?= X-Mailer: b4 0.14.3 X-Last-TLS-Session-Version: TLSv1.3 In order to integrate some bpftool tests into test_progs, define a few specific helpers that allow to execute bpftool commands, while possibly retrieving the command output. Those helpers most notably set the path to the bpftool binary under test. This version checks different possible paths relative to the directories where the different test_progs runners are executed, as we want to make sure not to accidentally use a bootstrap version of the binary. Signed-off-by: Alexis Lothor=C3=A9 (eBPF Foundation) --- Changes in v2: - Drop the new runner from commit, keep only the new bpftool-specific helpers - replace hardcoded path with a detected (and cached) path --- tools/testing/selftests/bpf/Makefile | 1 + tools/testing/selftests/bpf/bpftool_helpers.c | 74 +++++++++++++++++++++++= ++++ tools/testing/selftests/bpf/bpftool_helpers.h | 11 ++++ 3 files changed, 86 insertions(+) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests= /bpf/Makefile index 0270b0a6dd79..18d5e258841a 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -730,6 +730,7 @@ $(VERIFY_SIG_HDR): $(VERIFICATION_CERT) TRUNNER_TESTS_DIR :=3D prog_tests TRUNNER_BPF_PROGS_DIR :=3D progs TRUNNER_EXTRA_SOURCES :=3D btf_helpers.c \ + bpftool_helpers.c \ cap_helpers.c \ cgroup_helpers.c \ disasm.c \ diff --git a/tools/testing/selftests/bpf/bpftool_helpers.c b/tools/testing/= selftests/bpf/bpftool_helpers.c new file mode 100644 index 000000000000..a5824945a4a5 --- /dev/null +++ b/tools/testing/selftests/bpf/bpftool_helpers.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "bpftool_helpers.h" +#include +#include +#include + +#define BPFTOOL_PATH_MAX_LEN 64 +#define BPFTOOL_FULL_CMD_MAX_LEN 512 + +#define BPFTOOL_DEFAULT_PATH "tools/sbin/bpftool" + +static int detect_bpftool_path(char *buffer) +{ + char tmp[BPFTOOL_PATH_MAX_LEN]; + + /* Check default bpftool location (will work if we are running the + * default flavor of test_progs) + */ + snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "./%s", BPFTOOL_DEFAULT_PATH); + if (access(tmp, X_OK) =3D=3D 0) { + strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN); + return 0; + } + + /* Check alternate bpftool location (will work if we are running a + * specific flavor of test_progs, e.g. cpuv4 or no_alu32) + */ + snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "../%s", BPFTOOL_DEFAULT_PATH); + if (access(tmp, X_OK) =3D=3D 0) { + strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN); + return 0; + } + + /* Failed to find bpftool binary */ + return 1; +} + +static int run_command(char *args, char *output_buf, size_t output_max_len) +{ + static char bpftool_path[BPFTOOL_PATH_MAX_LEN] =3D {0}; + bool suppress_output =3D !(output_buf && output_max_len); + char command[BPFTOOL_FULL_CMD_MAX_LEN]; + FILE *f; + int ret; + + /* Detect and cache bpftool binary location */ + if (bpftool_path[0] =3D=3D 0 && detect_bpftool_path(bpftool_path)) + return 1; + + ret =3D snprintf(command, BPFTOOL_FULL_CMD_MAX_LEN, "%s %s%s", + bpftool_path, args, + suppress_output ? " > /dev/null 2>&1" : ""); + + f =3D popen(command, "r"); + if (!f) + return 1; + + if (!suppress_output) + fread(output_buf, 1, output_max_len, f); + ret =3D pclose(f); + + return ret; +} + +int run_bpftool_command(char *args) +{ + return run_command(args, NULL, 0); +} + +int get_bpftool_command_output(char *args, char *output_buf, size_t output= _max_len) +{ + return run_command(args, output_buf, output_max_len); +} + diff --git a/tools/testing/selftests/bpf/bpftool_helpers.h b/tools/testing/= selftests/bpf/bpftool_helpers.h new file mode 100644 index 000000000000..dec1ba201410 --- /dev/null +++ b/tools/testing/selftests/bpf/bpftool_helpers.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#pragma once + +#include +#include +#include + +#define MAX_BPFTOOL_CMD_LEN (256) + +int run_bpftool_command(char *args); +int get_bpftool_command_output(char *args, char *output_buf, size_t output= _max_len); --=20 2.52.0