All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hou Tao <houtao1@huawei.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Martin KaFai Lau <kafai@fb.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next 5/5] selftests/bpf: test return value handling for struct_ops prog
Date: Thu, 30 Sep 2021 19:08:46 +0800	[thread overview]
Message-ID: <765d949b-f19d-bac6-ca60-75237ff1989e@huawei.com> (raw)
In-Reply-To: <CAEf4BzaE4_c7fcMCfFe7nukivVrFgpPZcbr5z-FfSa=erNKiTw@mail.gmail.com>

Hi,

On 9/29/2021 7:19 AM, Andrii Nakryiko wrote:
> On Mon, Sep 27, 2021 at 7:38 PM Hou Tao <houtao1@huawei.com> wrote:
>> Running a BPF_PROG_TYPE_STRUCT_OPS prog for dummy_st_ops::init()
>> through bpf_prog_test_run(). Three test cases are added:
>> (1) attach dummy_st_ops should fail
>> (2) function return value of bpf_dummy_ops::init() is expected
>> (3) pointer argument of bpf_dummy_ops::init() works as expected
>>
>> Signed-off-by: Hou Tao <houtao1@huawei.com>
>> ---
>>  .../selftests/bpf/prog_tests/dummy_st_ops.c   | 81 +++++++++++++++++++
>>  .../selftests/bpf/progs/dummy_st_ops.c        | 33 ++++++++
>>  2 files changed, 114 insertions(+)
>>  create mode 100644 tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c
>>  create mode 100644 tools/testing/selftests/bpf/progs/dummy_st_ops.c
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c b/tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c
>> new file mode 100644
>> index 000000000000..4b1b52b847e6
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/prog_tests/dummy_st_ops.c
>> @@ -0,0 +1,81 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/* Copyright (C) 2021. Huawei Technologies Co., Ltd */
>> +#include <test_progs.h>
>> +#include "dummy_st_ops.skel.h"
>> +
>> +/* Need to keep consistent with definitions in include/linux/bpf_dummy_ops.h */
>> +struct bpf_dummy_ops_state {
>> +       int val;
>> +};
>> +
>> +static void test_dummy_st_ops_attach(void)
>> +{
>> +       struct dummy_st_ops *skel;
>> +       struct bpf_link *link;
>> +
>> +       skel = dummy_st_ops__open_and_load();
>> +       if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
>> +               goto out;
> no need for __destroy() as we haven't created skeleton, so this could
> be just a return
Will do.
>> +
>> +       link = bpf_map__attach_struct_ops(skel->maps.dummy_1);
>> +       if (!ASSERT_EQ(libbpf_get_error(link), -EOPNOTSUPP,
>> +                      "dummy_st_ops_attach"))
>> +               goto out;
> nit: unless you expect to add something here soon, probably doing
> ASSERT_EQ() and let it fall through to out: and destroy would be a bit
> more readable
Make sense. Will do.
>
>> +out:
>> +       dummy_st_ops__destroy(skel);
>> +}
>> +
>> +static void test_dummy_init_ret_value(void)
>> +{
>> +       struct dummy_st_ops *skel;
>> +       int err, fd;
>> +       __u32 duration = 0, retval = 0;
>> +
>> +       skel = dummy_st_ops__open_and_load();
>> +       if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
>> +               goto out;
> same, just return is fine and no need for out: label
OK. Will do in v2.
>> +
>> +       fd = bpf_program__fd(skel->progs.init_1);
>> +       err = bpf_prog_test_run(fd, 1, NULL, 0,
>> +                               NULL, NULL, &retval, &duration);
>> +       ASSERT_OK(err, "test_run");
>> +       ASSERT_EQ(retval, 0xf2f3f4f5, "test_ret");
>> +out:
>> +       dummy_st_ops__destroy(skel);
>> +}
>> +
>> +static void test_dummy_init_ptr_arg(void)
>> +{
>> +       struct dummy_st_ops *skel;
>> +       int err, fd;
>> +       __u32 duration = 0, retval = 0;
>> +       struct bpf_dummy_ops_state in_state, out_state;
>> +       __u32 state_size;
>> +
>> +       skel = dummy_st_ops__open_and_load();
>> +       if (!ASSERT_OK_PTR(skel, "dummy_st_ops_load"))
>> +               goto out;
> here as well
OK. Will do.
>
>> +
>> +       fd = bpf_program__fd(skel->progs.init_1);
>> +       memset(&in_state, 0, sizeof(in_state));
>> +       in_state.val = 0xbeef;
>> +       memset(&out_state, 0, sizeof(out_state));
>> +       err = bpf_prog_test_run(fd, 1, &in_state, sizeof(in_state),
>> +                               &out_state, &state_size, &retval, &duration);
>> +       ASSERT_OK(err, "test_run");
>> +       ASSERT_EQ(state_size, sizeof(out_state), "test_data_out");
>> +       ASSERT_EQ(out_state.val, 0x5a, "test_ptr_ret");
>> +       ASSERT_EQ(retval, in_state.val, "test_ret");
>> +out:
>> +       dummy_st_ops__destroy(skel);
>> +}
>> +
>> +void test_dummy_st_ops(void)
>> +{
>> +       if (test__start_subtest("dummy_st_ops_attach"))
>> +               test_dummy_st_ops_attach();
>> +       if (test__start_subtest("dummy_init_ret_value"))
>> +               test_dummy_init_ret_value();
>> +       if (test__start_subtest("dummy_init_ptr_arg"))
>> +               test_dummy_init_ptr_arg();
>> +}
>> diff --git a/tools/testing/selftests/bpf/progs/dummy_st_ops.c b/tools/testing/selftests/bpf/progs/dummy_st_ops.c
>> new file mode 100644
>> index 000000000000..133c328f082a
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/progs/dummy_st_ops.c
>> @@ -0,0 +1,33 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/* Copyright (C) 2021. Huawei Technologies Co., Ltd */
>> +#include <linux/bpf.h>
>> +#include <bpf/bpf_helpers.h>
>> +#include <bpf/bpf_tracing.h>
>> +
>> +struct bpf_dummy_ops_state {
>> +       int val;
>> +} __attribute__((preserve_access_index));
>> +
>> +struct bpf_dummy_ops {
>> +       int (*init)(struct bpf_dummy_ops_state *state);
>> +};
>> +
>> +char _liencse[] SEC("license") = "GPL";
> typo: _license (but it doesn't matter to libbpf, it looks at the
> section name only
Will fix.
>
>> +
>> +SEC("struct_ops/init_1")
>> +int BPF_PROG(init_1, struct bpf_dummy_ops_state *state)
>> +{
>> +       int ret;
>> +
>> +       if (!state)
>> +               return 0xf2f3f4f5;
>> +
>> +       ret = state->val;
>> +       state->val = 0x5a;
>> +       return ret;
>> +}
>> +
>> +SEC(".struct_ops")
>> +struct bpf_dummy_ops dummy_1 = {
>> +       .init = (void *)init_1,
>> +};
>> --
>> 2.29.2
>>
> .


      reply	other threads:[~2021-09-30 11:08 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-28  2:52 [PATCH bpf-next 0/5] introduce dummy BPF STRUCT_OPS Hou Tao
2021-09-28  2:52 ` [PATCH bpf-next 1/5] bpf: add dummy BPF STRUCT_OPS for test purpose Hou Tao
2021-09-28  2:52 ` [PATCH bpf-next 2/5] bpf: factor out a helper to prepare trampoline for struct_ops prog Hou Tao
2021-09-29 17:56   ` Martin KaFai Lau
2021-09-30 10:17     ` Hou Tao
2021-10-01 17:39       ` Martin KaFai Lau
2021-09-28  2:52 ` [PATCH bpf-next 3/5] bpf: do .test_run in dummy BPF STRUCT_OPS Hou Tao
2021-09-29 18:55   ` Martin KaFai Lau
2021-09-30 11:05     ` Hou Tao
2021-10-01 19:09       ` Martin KaFai Lau
2021-09-28  2:52 ` [PATCH bpf-next 4/5] bpf: hook .test_run for struct_ops program Hou Tao
2021-09-28  2:52 ` [PATCH bpf-next 5/5] selftests/bpf: test return value handling for struct_ops prog Hou Tao
2021-09-28 23:19   ` Andrii Nakryiko
2021-09-30 11:08     ` Hou Tao [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=765d949b-f19d-bac6-ca60-75237ff1989e@huawei.com \
    --to=houtao1@huawei.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kafai@fb.com \
    --cc=netdev@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.