1 // SPDX-License-Identifier: GPL-2.0
3 #include <test_progs.h>
4 #include "struct_ops_autocreate.skel.h"
5 #include "struct_ops_autocreate2.skel.h"
7 static void cant_load_full_object(void)
9 struct struct_ops_autocreate *skel;
13 skel = struct_ops_autocreate__open();
14 if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open"))
17 if (start_libbpf_log_capture())
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
24 err = struct_ops_autocreate__load(skel);
25 log = stop_libbpf_log_capture();
26 if (!ASSERT_ERR(err, "struct_ops_autocreate__load"))
29 ASSERT_HAS_SUBSTR(log, "libbpf: struct_ops init_kern", "init_kern message");
30 ASSERT_EQ(err, -ENOTSUP, "errno should be ENOTSUP");
34 struct_ops_autocreate__destroy(skel);
37 static int check_test_1_link(struct struct_ops_autocreate *skel, struct bpf_map *map)
39 struct bpf_link *link;
42 link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
43 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
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);
52 static void can_load_partial_object(void)
54 struct struct_ops_autocreate *skel;
57 skel = struct_ops_autocreate__open();
58 if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts"))
61 err = bpf_map__set_autocreate(skel->maps.testmod_2, false);
62 if (!ASSERT_OK(err, "bpf_map__set_autocreate"))
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");
68 err = struct_ops_autocreate__load(skel);
69 if (ASSERT_OK(err, "struct_ops_autocreate__load"))
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");
75 check_test_1_link(skel, skel->maps.testmod_1);
78 struct_ops_autocreate__destroy(skel);
81 static void optional_maps(void)
83 struct struct_ops_autocreate *skel;
86 skel = struct_ops_autocreate__open();
87 if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open"))
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");
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"))
101 err = struct_ops_autocreate__load(skel);
102 if (ASSERT_OK(err, "struct_ops_autocreate__load"))
105 check_test_1_link(skel, skel->maps.optional_map2);
108 struct_ops_autocreate__destroy(skel);
111 /* Swap test_mod1->test_1 program from 'bar' to 'foo' using shadow vars.
112 * test_mod1 load should enable autoload for 'foo'.
114 static void autoload_and_shadow_vars(void)
116 struct struct_ops_autocreate2 *skel = NULL;
117 struct bpf_link *link = NULL;
120 skel = struct_ops_autocreate2__open();
121 if (!ASSERT_OK_PTR(skel, "struct_ops_autocreate__open_opts"))
124 ASSERT_FALSE(bpf_program__autoload(skel->progs.foo), "foo default autoload");
125 ASSERT_FALSE(bpf_program__autoload(skel->progs.bar), "bar default autoload");
127 /* loading map testmod_1 would switch foo's autoload to true */
128 skel->struct_ops.testmod_1->test_1 = skel->progs.foo;
130 err = struct_ops_autocreate2__load(skel);
131 if (ASSERT_OK(err, "struct_ops_autocreate__load"))
134 ASSERT_TRUE(bpf_program__autoload(skel->progs.foo), "foo actual autoload");
135 ASSERT_FALSE(bpf_program__autoload(skel->progs.bar), "bar actual autoload");
137 link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
138 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
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");
145 bpf_link__destroy(link);
146 struct_ops_autocreate2__destroy(skel);
149 void test_struct_ops_autocreate(void)
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"))