1 // SPDX-License-Identifier: GPL-2.0
2 #include "bpf/libbpf.h"
3 #include "changes_pkt_data_freplace.skel.h"
4 #include "changes_pkt_data.skel.h"
5 #include <test_progs.h>
6
print_verifier_log(const char * log)7 static void print_verifier_log(const char *log)
8 {
9 if (env.verbosity >= VERBOSE_VERY)
10 fprintf(stdout, "VERIFIER LOG:\n=============\n%s=============\n", log);
11 }
12
test_aux(const char * main_prog_name,const char * to_be_replaced,const char * replacement,bool expect_load)13 static void test_aux(const char *main_prog_name,
14 const char *to_be_replaced,
15 const char *replacement,
16 bool expect_load)
17 {
18 struct changes_pkt_data_freplace *freplace = NULL;
19 struct bpf_program *freplace_prog = NULL;
20 struct bpf_program *main_prog = NULL;
21 LIBBPF_OPTS(bpf_object_open_opts, opts);
22 struct changes_pkt_data *main = NULL;
23 char log[16*1024];
24 int err;
25
26 opts.kernel_log_buf = log;
27 opts.kernel_log_size = sizeof(log);
28 if (env.verbosity >= VERBOSE_SUPER)
29 opts.kernel_log_level = 1 | 2 | 4;
30 main = changes_pkt_data__open_opts(&opts);
31 if (!ASSERT_OK_PTR(main, "changes_pkt_data__open"))
32 goto out;
33 main_prog = bpf_object__find_program_by_name(main->obj, main_prog_name);
34 if (!ASSERT_OK_PTR(main_prog, "main_prog"))
35 goto out;
36 bpf_program__set_autoload(main_prog, true);
37 err = changes_pkt_data__load(main);
38 print_verifier_log(log);
39 if (!ASSERT_OK(err, "changes_pkt_data__load"))
40 goto out;
41 freplace = changes_pkt_data_freplace__open_opts(&opts);
42 if (!ASSERT_OK_PTR(freplace, "changes_pkt_data_freplace__open"))
43 goto out;
44 freplace_prog = bpf_object__find_program_by_name(freplace->obj, replacement);
45 if (!ASSERT_OK_PTR(freplace_prog, "freplace_prog"))
46 goto out;
47 bpf_program__set_autoload(freplace_prog, true);
48 bpf_program__set_autoattach(freplace_prog, true);
49 bpf_program__set_attach_target(freplace_prog,
50 bpf_program__fd(main_prog),
51 to_be_replaced);
52 err = changes_pkt_data_freplace__load(freplace);
53 print_verifier_log(log);
54 if (expect_load) {
55 ASSERT_OK(err, "changes_pkt_data_freplace__load");
56 } else {
57 ASSERT_ERR(err, "changes_pkt_data_freplace__load");
58 ASSERT_HAS_SUBSTR(log, "Extension program changes packet data", "error log");
59 }
60
61 out:
62 changes_pkt_data_freplace__destroy(freplace);
63 changes_pkt_data__destroy(main);
64 }
65
66 /* There are two global subprograms in both changes_pkt_data.skel.h:
67 * - one changes packet data;
68 * - another does not.
69 * It is ok to freplace subprograms that change packet data with those
70 * that either do or do not. It is only ok to freplace subprograms
71 * that do not change packet data with those that do not as well.
72 * The below tests check outcomes for each combination of such freplace.
73 * Also test a case when main subprogram itself is replaced and is a single
74 * subprogram in a program.
75 */
test_changes_pkt_data_freplace(void)76 void test_changes_pkt_data_freplace(void)
77 {
78 struct {
79 const char *main;
80 const char *to_be_replaced;
81 bool changes;
82 } mains[] = {
83 { "main_with_subprogs", "changes_pkt_data", true },
84 { "main_with_subprogs", "does_not_change_pkt_data", false },
85 { "main_changes", "main_changes", true },
86 { "main_does_not_change", "main_does_not_change", false },
87 };
88 struct {
89 const char *func;
90 bool changes;
91 } replacements[] = {
92 { "changes_pkt_data", true },
93 { "does_not_change_pkt_data", false }
94 };
95 char buf[64];
96
97 for (int i = 0; i < ARRAY_SIZE(mains); ++i) {
98 for (int j = 0; j < ARRAY_SIZE(replacements); ++j) {
99 snprintf(buf, sizeof(buf), "%s_with_%s",
100 mains[i].to_be_replaced, replacements[j].func);
101 if (!test__start_subtest(buf))
102 continue;
103 test_aux(mains[i].main, mains[i].to_be_replaced, replacements[j].func,
104 mains[i].changes || !replacements[j].changes);
105 }
106 }
107 }
108