Re: [PATCH bpf-next 2/2] selftests/bpf: Test BPF_MAP_TYPE_PROG_ARRAY static initialization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Nov 21, 2021 at 5:55 AM Hengqi Chen <hengqi.chen@xxxxxxxxx> wrote:
>
> Add testcase for BPF_MAP_TYPE_PROG_ARRAY static initialization.
>
> Signed-off-by: Hengqi Chen <hengqi.chen@xxxxxxxxx>
> ---

It's a bit too minimal, let's actually trigger the program and make
sure that tail call program gets executed. Please also make sure that
you filter by pid like other tracing progs do (I suggest using
usleep(1) and raw_tracepoint program for sys_enter, as the simplest
set up).

>  .../bpf/prog_tests/prog_array_init.c          | 27 +++++++++++++++++
>  .../bpf/progs/test_prog_array_init.c          | 30 +++++++++++++++++++
>  2 files changed, 57 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/prog_array_init.c
>  create mode 100644 tools/testing/selftests/bpf/progs/test_prog_array_init.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/prog_array_init.c b/tools/testing/selftests/bpf/prog_tests/prog_array_init.c
> new file mode 100644
> index 000000000000..2fbf6946a0b6
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/prog_array_init.c
> @@ -0,0 +1,27 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/* Copyright (c) 2021 Hengqi Chen */
> +
> +#include <test_progs.h>
> +#include <sys/un.h>
> +#include "test_prog_array_init.skel.h"
> +
> +void test_prog_array_init(void)
> +{
> +       struct test_prog_array_init *skel;
> +       int err;
> +
> +       skel = test_prog_array_init__open();
> +       if (!ASSERT_OK_PTR(skel, "could not open BPF object"))
> +               return;
> +
> +       err = test_prog_array_init__load(skel);
> +       if (!ASSERT_OK(err, "could not load BPF object"))
> +               goto cleanup;
> +
> +       err = test_prog_array_init__attach(skel);
> +       if (!ASSERT_OK(err, "could not attach BPF object"))
> +               goto cleanup;
> +
> +cleanup:
> +       test_prog_array_init__destroy(skel);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/test_prog_array_init.c b/tools/testing/selftests/bpf/progs/test_prog_array_init.c
> new file mode 100644
> index 000000000000..e97204dd5443
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_prog_array_init.c
> @@ -0,0 +1,30 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/* Copyright (c) 2021 Hengqi Chen */
> +
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +SEC("socket")
> +int tailcall_1(void *ctx)
> +{

let's add some global variable that will be set by the tail call
program, so that we know that correct slot and correct program was set

> +       return 0;
> +}
> +
> +struct {
> +       __uint(type, BPF_MAP_TYPE_PROG_ARRAY);
> +       __uint(max_entries, 2);
> +       __uint(key_size, sizeof(__u32));
> +       __array(values, int (void *));
> +} prog_array_init SEC(".maps") = {
> +       .values = {
> +               [1] = (void *)&tailcall_1,
> +       },
> +};
> +
> +SEC("socket")
> +int BPF_PROG(entry)
> +{

BPF_PROG doesn't really help, it just hides ctx which you are actually
using below, so let's just stick to `int entry(void *ctx)` here.

> +       bpf_tail_call(ctx, &prog_array_init, 1);
> +       return 0;
> +}
> --
> 2.30.2



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux