From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id B5CBC45656; Fri, 19 Jul 2024 20:29:15 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8391340E4C; Fri, 19 Jul 2024 20:29:15 +0200 (CEST) Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by mails.dpdk.org (Postfix) with ESMTP id 86A7540E18 for ; Fri, 19 Jul 2024 20:29:14 +0200 (CEST) Received: from mail.maildlp.com (unknown [172.18.186.231]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4WQdRz0sLLz6K93S; Sat, 20 Jul 2024 02:26:55 +0800 (CST) Received: from frapeml100008.china.huawei.com (unknown [7.182.85.131]) by mail.maildlp.com (Postfix) with ESMTPS id AFDFE140A36; Sat, 20 Jul 2024 02:29:12 +0800 (CST) Received: from frapeml500007.china.huawei.com (7.182.85.172) by frapeml100008.china.huawei.com (7.182.85.131) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Fri, 19 Jul 2024 20:29:12 +0200 Received: from frapeml500007.china.huawei.com ([7.182.85.172]) by frapeml500007.china.huawei.com ([7.182.85.172]) with mapi id 15.01.2507.039; Fri, 19 Jul 2024 20:29:12 +0200 From: Konstantin Ananyev To: Stephen Hemminger CC: Konstantin Ananyev , "dev@dpdk.org" Subject: RE: BPF standardization Thread-Topic: BPF standardization Thread-Index: AQHasuiDB+5zO0SqFEuIA3Uk/IasZbH+CG3ggABuJoCAADVO0A== Date: Fri, 19 Jul 2024 18:29:12 +0000 Message-ID: References: <20240530162326.02218863@hermes.local> <956072b13b7b45fe8ce676989580f3e0@huawei.com> <20240719101323.5e880752@hermes.local> In-Reply-To: <20240719101323.5e880752@hermes.local> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.206.138.42] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org > This was taken from how kernel converts cBPF and the prologue it generate= s. > Surprising that verifier gags? Indeed it looks strange... Have to admit I didn't dig into how linux converter works... Below is the patch I used to test it from DPDK UT - might be you'll spot so= mething obvious here. =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Subject: [PATCH 2/2] test/bpf: call linux verifier for converted cBPF filte= r To ensure DPDK/kernel compatibility for converted cBPF filters, etc. Signed-off-by: Konstantin Ananyev --- app/test/meson.build | 1 + app/test/test_bpf.c | 11 +++++++++ app/test/test_bpf.h | 12 ++++++++++ app/test/test_bpf_verify_linux.c | 39 ++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 app/test/test_bpf.h create mode 100644 app/test/test_bpf_verify_linux.c diff --git a/app/test/meson.build b/app/test/meson.build index e29258e6ec..d46e688db5 100644 --- a/app/test/meson.build +++ b/app/test/meson.build @@ -35,6 +35,7 @@ source_file_deps =3D { 'test_bitops.c': [], 'test_bitratestats.c': ['metrics', 'bitratestats', 'ethdev'] + sample_= packet_forward_deps, 'test_bpf.c': ['bpf', 'net'], + 'test_bpf_verify_linux.c': [], 'test_byteorder.c': [], # 'test_cfgfile.c': ['cfgfile'], 'test_cksum.c': ['net'], diff --git a/app/test/test_bpf.c b/app/test/test_bpf.c index 7819d6aba9..749f9de20c 100644 --- a/app/test/test_bpf.c +++ b/app/test/test_bpf.c @@ -15,6 +15,7 @@ #include #include #include "test.h" +#include "test_bpf.h" =20 #if !defined(RTE_LIB_BPF) =20 @@ -3431,10 +3432,13 @@ static const char * const sample_filters[] =3D { static int test_bpf_filter(pcap_t *pcap, const char *s) { + int32_t rc; struct bpf_program fcode; struct rte_bpf_prm *prm =3D NULL; struct rte_bpf *bpf =3D NULL; =20 + static char logbuf[UINT16_MAX + 1]; + if (pcap_compile(pcap, &fcode, s, 1, PCAP_NETMASK_UNKNOWN)) { printf("%s@%d: pcap_compile('%s') failed: %s;\n", __func__, __LINE__, s, pcap_geterr(pcap)); @@ -3451,6 +3455,13 @@ test_bpf_filter(pcap_t *pcap, const char *s) printf("bpf convert for \"%s\" produced:\n", s); rte_bpf_dump(stdout, prm->ins, prm->nb_ins); =20 + rc =3D bpf_linux_verify(prm, logbuf, sizeof(logbuf)); + printf("%s@%d: linux verifier reports: %d(%s);\n", + __func__, __LINE__, rc, strerror(-rc)); + printf("linux verifier log: %s\n", logbuf); + if (rc !=3D 0) + goto error; + bpf =3D rte_bpf_load(prm); if (bpf =3D=3D NULL) { printf("%s@%d: failed to load bpf code, error=3D%d(%s);\n", diff --git a/app/test/test_bpf.h b/app/test/test_bpf.h new file mode 100644 index 0000000000..e5be2be920 --- /dev/null +++ b/app/test/test_bpf.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef _TEST_BPF_H_ +#define _TEST_BPF_H_ + +#include + +int bpf_linux_verify(const struct rte_bpf_prm *prm, char *logbuf, + uint32_t lbsz); + +#endif diff --git a/app/test/test_bpf_verify_linux.c b/app/test/test_bpf_verify_li= nux.c new file mode 100644 index 0000000000..2a42d61fb5 --- /dev/null +++ b/app/test/test_bpf_verify_linux.c @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "test_bpf.h" + +int +bpf_linux_verify(const struct rte_bpf_prm *prm, char *logbuf, uint32_t lbs= z) +{ + int32_t rc; + union bpf_attr attr; + + memset(&attr, 0, sizeof(attr)); + attr.prog_type =3D BPF_PROG_TYPE_SOCKET_FILTER; + attr.insn_cnt =3D prm->nb_ins; + attr.insns =3D (uintptr_t)prm->ins; + attr.license =3D (uintptr_t)"GPL"; + attr.log_buf =3D (uintptr_t)logbuf; + attr.log_size =3D lbsz; + attr.log_level =3D (logbuf =3D=3D NULL) ? 0 : 3; + + rc =3D syscall(SYS_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); + if (rc < 0) + rc =3D -errno; + else { + /* closed fd for BPF prog */ + close(rc); + rc =3D 0; + } + + return rc; +} --=20 2.35.3