1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <test_progs.h> 4 #include "struct_ops_autocreate.skel.h" 5 #include "struct_ops_autocreate2.skel.h" 6 7 static void cant_load_full_object(void) 8 { 9 struct struct_ops_autocreate *skel; 10 char *log = NULL; 11 int err; 12 13 skel = struct_ops_autocreate__open(); 14 if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open")) 15 return; 16 17 if (start_libbpf_log_capture()) 18 goto cleanup; 19 /* The testmod_2 map BTF type (struct bpf_testmod_ops___v2) doesn't 20 * match the BTF of the actual struct bpf_testmod_ops defined in the 21 * kernel, so we should fail to load it if we don't disable autocreate 22 * for that map. 23 */ 24 err = struct_ops_autocreate__load(skel); 25 log = stop_libbpf_log_capture(); 26 if (!ASSERT_ERR(err, "struct_ops_autocreate__load")) 27 goto cleanup; 28 29 ASSERT_HAS_SUBSTR(log, "libbpf: struct_ops init_kern", "init_kern message"); 30 ASSERT_EQ(err, -ENOTSUP, "errno should be ENOTSUP"); 31 32 cleanup: 33 free(log); 34 struct_ops_autocreate__destroy(skel); 35 } 36 37 static int check_test_1_link(struct struct_ops_autocreate *skel, struct bpf_map *map) 38 { 39 struct bpf_link *link; 40 int err; 41 42 link = bpf_map__attach_struct_ops(skel->maps.testmod_1); 43 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) 44 return -1; 45 46 /* test_1() would be called from bpf_dummy_reg2() in bpf_testmod.c */ 47 err = ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result"); 48 bpf_link__destroy(link); 49 return err; 50 } 51 52 static void can_load_partial_object(void) 53 { 54 struct struct_ops_autocreate *skel; 55 int err; 56 57 skel = struct_ops_autocreate__open(); 58 if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts")) 59 return; 60 61 err = bpf_map__set_autocreate(skel->maps.testmod_2, false); 62 if (!ASSERT_OK(err, "bpf_map__set_autocreate")) 63 goto cleanup; 64 65 ASSERT_TRUE(bpf_program__autoload(skel->progs.test_1), "test_1 default autoload"); 66 ASSERT_TRUE(bpf_program__autoload(skel->progs.test_2), "test_2 default autoload"); 67 68 err = struct_ops_autocreate__load(skel); 69 if (ASSERT_OK(err, "struct_ops_autocreate__load")) 70 goto cleanup; 71 72 ASSERT_TRUE(bpf_program__autoload(skel->progs.test_1), "test_1 actual autoload"); 73 ASSERT_FALSE(bpf_program__autoload(skel->progs.test_2), "test_2 actual autoload"); 74 75 check_test_1_link(skel, skel->maps.testmod_1); 76 77 cleanup: 78 struct_ops_autocreate__destroy(skel); 79 } 80 81 static void optional_maps(void) 82 { 83 struct struct_ops_autocreate *skel; 84 int err; 85 86 skel = struct_ops_autocreate__open(); 87 if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open")) 88 return; 89 90 ASSERT_TRUE(bpf_map__autocreate(skel->maps.testmod_1), "testmod_1 autocreate"); 91 ASSERT_TRUE(bpf_map__autocreate(skel->maps.testmod_2), "testmod_2 autocreate"); 92 ASSERT_FALSE(bpf_map__autocreate(skel->maps.optional_map), "optional_map autocreate"); 93 ASSERT_FALSE(bpf_map__autocreate(skel->maps.optional_map2), "optional_map2 autocreate"); 94 95 err = bpf_map__set_autocreate(skel->maps.testmod_1, false); 96 err |= bpf_map__set_autocreate(skel->maps.testmod_2, false); 97 err |= bpf_map__set_autocreate(skel->maps.optional_map2, true); 98 if (!ASSERT_OK(err, "bpf_map__set_autocreate")) 99 goto cleanup; 100 101 err = struct_ops_autocreate__load(skel); 102 if (ASSERT_OK(err, "struct_ops_autocreate__load")) 103 goto cleanup; 104 105 check_test_1_link(skel, skel->maps.optional_map2); 106 107 cleanup: 108 struct_ops_autocreate__destroy(skel); 109 } 110 111 /* Swap test_mod1->test_1 program from 'bar' to 'foo' using shadow vars. 112 * test_mod1 load should enable autoload for 'foo'. 113 */ 114 static void autoload_and_shadow_vars(void) 115 { 116 struct struct_ops_autocreate2 *skel = NULL; 117 struct bpf_link *link = NULL; 118 int err; 119 120 skel = struct_ops_autocreate2__open(); 121 if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts")) 122 return; 123 124 ASSERT_FALSE(bpf_program__autoload(skel->progs.foo), "foo default autoload"); 125 ASSERT_FALSE(bpf_program__autoload(skel->progs.bar), "bar default autoload"); 126 127 /* loading map testmod_1 would switch foo's autoload to true */ 128 skel->struct_ops.testmod_1->test_1 = skel->progs.foo; 129 130 err = struct_ops_autocreate2__load(skel); 131 if (ASSERT_OK(err, "struct_ops_autocreate__load")) 132 goto cleanup; 133 134 ASSERT_TRUE(bpf_program__autoload(skel->progs.foo), "foo actual autoload"); 135 ASSERT_FALSE(bpf_program__autoload(skel->progs.bar), "bar actual autoload"); 136 137 link = bpf_map__attach_struct_ops(skel->maps.testmod_1); 138 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) 139 goto cleanup; 140 141 /* test_1() would be called from bpf_dummy_reg2() in bpf_testmod.c */ 142 err = ASSERT_EQ(skel->bss->test_1_result, 42, "test_1_result"); 143 144 cleanup: 145 bpf_link__destroy(link); 146 struct_ops_autocreate2__destroy(skel); 147 } 148 149 void test_struct_ops_autocreate(void) 150 { 151 if (test__start_subtest("cant_load_full_object")) 152 cant_load_full_object(); 153 if (test__start_subtest("can_load_partial_object")) 154 can_load_partial_object(); 155 if (test__start_subtest("autoload_and_shadow_vars")) 156 autoload_and_shadow_vars(); 157 if (test__start_subtest("optional_maps")) 158 optional_maps(); 159 } 160