From: Lorenzo Bianconi <lorenzo@kernel.org>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Alexei Starovoitov <ast@kernel.org>
Subject: Re: [PATCH bpf-next 2/2] bpf: selftests: get rid of CHECK macro in xdp_bpf2bpf.c
Date: Wed, 19 Jan 2022 22:53:51 +0100 [thread overview]
Message-ID: <YeiIb3IkSl90BuaF@lore-desk> (raw)
In-Reply-To: <CAEf4BzZVYLGHX1zexH0wuAXD_OLAA3Kv2LSi7eCQNw=VS1B0ZA@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 5857 bytes --]
> On Wed, Jan 19, 2022 at 8:58 AM Lorenzo Bianconi <lorenzo@kernel.org> wrote:
> >
> > Rely on ASSERT* macros and get rid of deprecated CHECK ones in
> > xdp_bpf2bpf bpf selftest.
> >
> > Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
> > ---
> > .../selftests/bpf/prog_tests/xdp_bpf2bpf.c | 43 ++++++++-----------
> > 1 file changed, 17 insertions(+), 26 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c b/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
> > index c98a897ad692..951ce1fc535d 100644
> > --- a/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
> > @@ -12,25 +12,21 @@ struct meta {
> >
> > static void on_sample(void *ctx, int cpu, void *data, __u32 size)
> > {
> > - int duration = 0;
> > struct meta *meta = (struct meta *)data;
> > struct ipv4_packet *trace_pkt_v4 = data + sizeof(*meta);
> >
> > - if (CHECK(size < sizeof(pkt_v4) + sizeof(*meta),
> > - "check_size", "size %u < %zu\n",
> > - size, sizeof(pkt_v4) + sizeof(*meta)))
> > + if (!ASSERT_GE(size, sizeof(pkt_v4) + sizeof(*meta), "check_size"))
> > return;
> >
> > - if (CHECK(meta->ifindex != if_nametoindex("lo"), "check_meta_ifindex",
> > - "meta->ifindex = %d\n", meta->ifindex))
> > + if (!ASSERT_EQ(meta->ifindex, if_nametoindex("lo"),
> > + "check_meta_ifindex"))
> > return;
> >
> > - if (CHECK(meta->pkt_len != sizeof(pkt_v4), "check_meta_pkt_len",
> > - "meta->pkt_len = %zd\n", sizeof(pkt_v4)))
> > + if (!ASSERT_EQ(meta->pkt_len, sizeof(pkt_v4), "check_meta_pkt_len"))
> > return;
> >
> > - if (CHECK(memcmp(trace_pkt_v4, &pkt_v4, sizeof(pkt_v4)),
> > - "check_packet_content", "content not the same\n"))
> > + if (!ASSERT_EQ(memcmp(trace_pkt_v4, &pkt_v4, sizeof(pkt_v4)),
> > + 0, "check_packet_content"))
> > return;
> >
>
> we can simplify and make it easier to follow by not doing early
> returns. Just a sequence of ASSERTs would be compact and nice.
ack, fine. I will fix it in v2.
>
> > *(bool *)ctx = true;
> > @@ -52,7 +48,7 @@ void test_xdp_bpf2bpf(void)
> >
> > /* Load XDP program to introspect */
> > pkt_skel = test_xdp__open_and_load();
> > - if (CHECK(!pkt_skel, "pkt_skel_load", "test_xdp skeleton failed\n"))
> > + if (!ASSERT_OK_PTR(pkt_skel, "test_xdp__open_and_load"))
> > return;
> >
> > pkt_fd = bpf_program__fd(pkt_skel->progs._xdp_tx_iptunnel);
> > @@ -62,7 +58,7 @@ void test_xdp_bpf2bpf(void)
> >
> > /* Load trace program */
> > ftrace_skel = test_xdp_bpf2bpf__open();
> > - if (CHECK(!ftrace_skel, "__open", "ftrace skeleton failed\n"))
> > + if (!ASSERT_OK_PTR(ftrace_skel, "test_xdp_bpf2bpf__open"))
> > goto out;
> >
> > /* Demonstrate the bpf_program__set_attach_target() API rather than
> > @@ -77,11 +73,11 @@ void test_xdp_bpf2bpf(void)
> > bpf_program__set_attach_target(prog, pkt_fd, "_xdp_tx_iptunnel");
> >
> > err = test_xdp_bpf2bpf__load(ftrace_skel);
> > - if (CHECK(err, "__load", "ftrace skeleton failed\n"))
> > + if (!ASSERT_OK(err, "test_xdp_bpf2bpf__load"))
> > goto out;
> >
> > err = test_xdp_bpf2bpf__attach(ftrace_skel);
> > - if (CHECK(err, "ftrace_attach", "ftrace attach failed: %d\n", err))
> > + if (!ASSERT_OK(err, "test_xdp_bpf2bpf__attach"))
> > goto out;
> >
> > /* Set up perf buffer */
> > @@ -94,30 +90,25 @@ void test_xdp_bpf2bpf(void)
> > err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4),
> > buf, &size, &retval, &duration);
> > memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph));
> > - if (CHECK(err || retval != XDP_TX || size != 74 ||
> > - iph.protocol != IPPROTO_IPIP, "ipv4",
> > - "err %d errno %d retval %d size %d\n",
> > - err, errno, retval, size))
> > + if (!ASSERT_OK(err || retval != XDP_TX || size != 74 ||
> > + iph.protocol != IPPROTO_IPIP, "ipv4"))
> > goto out;
> >
> > /* Make sure bpf_xdp_output() was triggered and it sent the expected
> > * data to the perf ring buffer.
> > */
> > err = perf_buffer__poll(pb, 100);
> > - if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
> > + if (!ASSERT_GE(err, 0, "perf_buffer__poll"))
> > goto out;
> >
> > - CHECK_FAIL(!passed);
> > + ASSERT_TRUE(passed, "test passed");
> >
> > /* Verify test results */
> > - if (CHECK(ftrace_skel->bss->test_result_fentry != if_nametoindex("lo"),
> > - "result", "fentry failed err %llu\n",
> > - ftrace_skel->bss->test_result_fentry))
> > + if (!ASSERT_EQ(ftrace_skel->bss->test_result_fentry, if_nametoindex("lo"),
> > + "fentry result"))
> > goto out;
> >
> > - CHECK(ftrace_skel->bss->test_result_fexit != XDP_TX, "result",
> > - "fexit failed err %llu\n", ftrace_skel->bss->test_result_fexit);
> > -
> > + ASSERT_EQ(ftrace_skel->bss->test_result_fexit, XDP_TX, "fexit result");
> > out:
> > if (pb)
>
> while at it, please drop this if. perf_buffer__free() handles NULLs
> and error pointers.
ack, fine. I will fix it in v2.
Regards,
Lorenzo
>
> > perf_buffer__free(pb);
> > --
> > 2.34.1
> >
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
prev parent reply other threads:[~2022-01-19 21:54 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-19 16:58 [PATCH bpf-next 0/2] rely on ASSERT marcos in xdp_bpf2bpf.c/xdp_adjust_tail.c Lorenzo Bianconi
2022-01-19 16:58 ` [PATCH bpf-next 1/2] bpf: selftests: get rid of CHECK macro in xdp_adjust_tail.c Lorenzo Bianconi
2022-01-19 18:52 ` Andrii Nakryiko
2022-01-19 21:54 ` Lorenzo Bianconi
2022-01-19 16:58 ` [PATCH bpf-next 2/2] bpf: selftests: get rid of CHECK macro in xdp_bpf2bpf.c Lorenzo Bianconi
2022-01-19 18:54 ` Andrii Nakryiko
2022-01-19 21:53 ` Lorenzo Bianconi [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YeiIb3IkSl90BuaF@lore-desk \
--to=lorenzo@kernel.org \
--cc=andrii.nakryiko@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.