diff mbox series

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

Message ID 20211121135440.3205482-3-hengqi.chen@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Support static initialization of BPF_MAP_TYPE_PROG_ARRAY | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 8 maintainers not CCed: kafai@fb.com songliubraving@fb.com shuah@kernel.org john.fastabend@gmail.com yhs@fb.com linux-kselftest@vger.kernel.org netdev@vger.kernel.org kpsingh@kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: Improper SPDX comment style for 'tools/testing/selftests/bpf/prog_tests/prog_array_init.c', please use '//' instead WARNING: Improper SPDX comment style for 'tools/testing/selftests/bpf/progs/test_prog_array_init.c', please use '//' instead WARNING: Missing or malformed SPDX-License-Identifier tag in line 1 WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next fail VM_Test

Commit Message

Hengqi Chen Nov. 21, 2021, 1:54 p.m. UTC
Add testcase for BPF_MAP_TYPE_PROG_ARRAY static initialization.

Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
---
 .../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

--
2.30.2

Comments

Andrii Nakryiko Nov. 23, 2021, 3:28 a.m. UTC | #1
On Sun, Nov 21, 2021 at 5:55 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>
> Add testcase for BPF_MAP_TYPE_PROG_ARRAY static initialization.
>
> Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
> ---

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
Hengqi Chen Nov. 24, 2021, 4:18 p.m. UTC | #2
On 11/23/21 11:28 AM, Andrii Nakryiko wrote:
> On Sun, Nov 21, 2021 at 5:55 AM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>>
>> Add testcase for BPF_MAP_TYPE_PROG_ARRAY static initialization.
>>
>> Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
>> ---
> 
> 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).
> 

Will implement what you suggest here. Thanks.

>>  .../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
> 

Will do.

>> +       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.
> 

Ack.

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

---Hengqi
diff mbox series

Patch

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)
+{
+	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_tail_call(ctx, &prog_array_init, 1);
+	return 0;
+}