diff options
| author | Eduard Zingerman <[email protected]> | 2024-03-06 12:45:23 +0200 | 
|---|---|---|
| committer | Andrii Nakryiko <[email protected]> | 2024-03-06 15:18:15 -0800 | 
| commit | 651d49f15b2a84b3bcfe950fa99c3672b9619dbd (patch) | |
| tree | eea5bdf4b6fe6d7b31445339fba8e799bf8ad623 /tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c | |
| parent | fe9d049c3da06373a1a35914b7f695509e4cb1fe (diff) | |
selftests/bpf: Verify struct_ops autoload/autocreate sync
Check that autocreate flags of struct_ops map cause autoload of
struct_ops corresponding programs:
- when struct_ops program is referenced only from a map for which
  autocreate is set to false, that program should not be loaded;
- when struct_ops program with autoload == false is set to be used
  from a map with autocreate == true using shadow var,
  that program should be loaded;
- when struct_ops program is not referenced from any map object load
  should fail.
Signed-off-by: Eduard Zingerman <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
Diffstat (limited to 'tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c')
| -rw-r--r-- | tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c | 51 | 
1 files changed, 47 insertions, 4 deletions
| diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c index a5c8bb9fcb2c..78948dc6445c 100644 --- a/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c +++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_autocreate.c @@ -2,6 +2,7 @@  #include <test_progs.h>  #include "struct_ops_autocreate.skel.h" +#include "struct_ops_autocreate2.skel.h"  static void cant_load_full_object(void)  { @@ -43,18 +44,20 @@ static void can_load_partial_object(void)  	if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts"))  		return; -	err = bpf_program__set_autoload(skel->progs.test_2, false); -	if (!ASSERT_OK(err, "bpf_program__set_autoload")) -		goto cleanup; -  	err = bpf_map__set_autocreate(skel->maps.testmod_2, false);  	if (!ASSERT_OK(err, "bpf_map__set_autocreate"))  		goto cleanup; +	ASSERT_TRUE(bpf_program__autoload(skel->progs.test_1), "test_1 default autoload"); +	ASSERT_TRUE(bpf_program__autoload(skel->progs.test_2), "test_2 default autoload"); +  	err = struct_ops_autocreate__load(skel);  	if (ASSERT_OK(err, "struct_ops_autocreate__load"))  		goto cleanup; +	ASSERT_TRUE(bpf_program__autoload(skel->progs.test_1), "test_1 actual autoload"); +	ASSERT_FALSE(bpf_program__autoload(skel->progs.test_2), "test_2 actual autoload"); +  	link = bpf_map__attach_struct_ops(skel->maps.testmod_1);  	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))  		goto cleanup; @@ -67,10 +70,50 @@ cleanup:  	struct_ops_autocreate__destroy(skel);  } +/* Swap test_mod1->test_1 program from 'bar' to 'foo' using shadow vars. + * test_mod1 load should enable autoload for 'foo'. + */ +static void autoload_and_shadow_vars(void) +{ +	struct struct_ops_autocreate2 *skel = NULL; +	struct bpf_link *link = NULL; +	int err; + +	skel = struct_ops_autocreate2__open(); +	if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts")) +		return; + +	ASSERT_FALSE(bpf_program__autoload(skel->progs.foo), "foo default autoload"); +	ASSERT_FALSE(bpf_program__autoload(skel->progs.bar), "bar default autoload"); + +	/* loading map testmod_1 would switch foo's autoload to true */ +	skel->struct_ops.testmod_1->test_1 = skel->progs.foo; + +	err = struct_ops_autocreate2__load(skel); +	if (ASSERT_OK(err, "struct_ops_autocreate__load")) +		goto cleanup; + +	ASSERT_TRUE(bpf_program__autoload(skel->progs.foo), "foo actual autoload"); +	ASSERT_FALSE(bpf_program__autoload(skel->progs.bar), "bar actual autoload"); + +	link = bpf_map__attach_struct_ops(skel->maps.testmod_1); +	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) +		goto cleanup; + +	/* test_1() would be called from bpf_dummy_reg2() in bpf_testmod.c */ +	err = ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result"); + +cleanup: +	bpf_link__destroy(link); +	struct_ops_autocreate2__destroy(skel); +} +  void test_struct_ops_autocreate(void)  {  	if (test__start_subtest("cant_load_full_object"))  		cant_load_full_object();  	if (test__start_subtest("can_load_partial_object"))  		can_load_partial_object(); +	if (test__start_subtest("autoload_and_shadow_vars")) +		autoload_and_shadow_vars();  } |