1d85aedacSKumar Kartikeya Dwivedi // SPDX-License-Identifier: GPL-2.0
2*898f55f5SEduard Zingerman #include <regex.h>
3d85aedacSKumar Kartikeya Dwivedi #include <test_progs.h>
4d85aedacSKumar Kartikeya Dwivedi #include <network_helpers.h>
5d85aedacSKumar Kartikeya Dwivedi
6d85aedacSKumar Kartikeya Dwivedi #include "test_spin_lock.skel.h"
7c48748aeSKumar Kartikeya Dwivedi #include "test_spin_lock_fail.skel.h"
8c48748aeSKumar Kartikeya Dwivedi
9c48748aeSKumar Kartikeya Dwivedi static char log_buf[1024 * 1024];
10c48748aeSKumar Kartikeya Dwivedi
11c48748aeSKumar Kartikeya Dwivedi static struct {
12c48748aeSKumar Kartikeya Dwivedi const char *prog_name;
13c48748aeSKumar Kartikeya Dwivedi const char *err_msg;
14c48748aeSKumar Kartikeya Dwivedi } spin_lock_fail_tests[] = {
15c48748aeSKumar Kartikeya Dwivedi { "lock_id_kptr_preserve",
16c48748aeSKumar Kartikeya Dwivedi "5: (bf) r1 = r0 ; R0_w=ptr_foo(id=2,ref_obj_id=2,off=0,imm=0) "
17c48748aeSKumar Kartikeya Dwivedi "R1_w=ptr_foo(id=2,ref_obj_id=2,off=0,imm=0) refs=2\n6: (85) call bpf_this_cpu_ptr#154\n"
18c48748aeSKumar Kartikeya Dwivedi "R1 type=ptr_ expected=percpu_ptr_" },
19c48748aeSKumar Kartikeya Dwivedi { "lock_id_global_zero",
20c48748aeSKumar Kartikeya Dwivedi "; R1_w=map_value(off=0,ks=4,vs=4,imm=0)\n2: (85) call bpf_this_cpu_ptr#154\n"
21c48748aeSKumar Kartikeya Dwivedi "R1 type=map_value expected=percpu_ptr_" },
22c48748aeSKumar Kartikeya Dwivedi { "lock_id_mapval_preserve",
23*898f55f5SEduard Zingerman "[0-9]\\+: (bf) r1 = r0 ;"
24*898f55f5SEduard Zingerman " R0_w=map_value(id=1,off=0,ks=4,vs=8,imm=0)"
25*898f55f5SEduard Zingerman " R1_w=map_value(id=1,off=0,ks=4,vs=8,imm=0)\n"
26*898f55f5SEduard Zingerman "[0-9]\\+: (85) call bpf_this_cpu_ptr#154\n"
27c48748aeSKumar Kartikeya Dwivedi "R1 type=map_value expected=percpu_ptr_" },
28c48748aeSKumar Kartikeya Dwivedi { "lock_id_innermapval_preserve",
29*898f55f5SEduard Zingerman "[0-9]\\+: (bf) r1 = r0 ;"
30*898f55f5SEduard Zingerman " R0=map_value(id=2,off=0,ks=4,vs=8,imm=0)"
31*898f55f5SEduard Zingerman " R1_w=map_value(id=2,off=0,ks=4,vs=8,imm=0)\n"
32*898f55f5SEduard Zingerman "[0-9]\\+: (85) call bpf_this_cpu_ptr#154\n"
33c48748aeSKumar Kartikeya Dwivedi "R1 type=map_value expected=percpu_ptr_" },
34c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_kptr_kptr", "bpf_spin_unlock of different lock" },
35c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_kptr_global", "bpf_spin_unlock of different lock" },
36c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_kptr_mapval", "bpf_spin_unlock of different lock" },
37c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_kptr_innermapval", "bpf_spin_unlock of different lock" },
38c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_global_global", "bpf_spin_unlock of different lock" },
39c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_global_kptr", "bpf_spin_unlock of different lock" },
40c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_global_mapval", "bpf_spin_unlock of different lock" },
41c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_global_innermapval", "bpf_spin_unlock of different lock" },
42c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_mapval_mapval", "bpf_spin_unlock of different lock" },
43c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_mapval_kptr", "bpf_spin_unlock of different lock" },
44c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_mapval_global", "bpf_spin_unlock of different lock" },
45c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_mapval_innermapval", "bpf_spin_unlock of different lock" },
46c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_innermapval_innermapval1", "bpf_spin_unlock of different lock" },
47c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_innermapval_innermapval2", "bpf_spin_unlock of different lock" },
48c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_innermapval_kptr", "bpf_spin_unlock of different lock" },
49c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_innermapval_global", "bpf_spin_unlock of different lock" },
50c48748aeSKumar Kartikeya Dwivedi { "lock_id_mismatch_innermapval_mapval", "bpf_spin_unlock of different lock" },
51c48748aeSKumar Kartikeya Dwivedi };
52c48748aeSKumar Kartikeya Dwivedi
match_regex(const char * pattern,const char * string)53*898f55f5SEduard Zingerman static int match_regex(const char *pattern, const char *string)
54*898f55f5SEduard Zingerman {
55*898f55f5SEduard Zingerman int err, rc;
56*898f55f5SEduard Zingerman regex_t re;
57*898f55f5SEduard Zingerman
58*898f55f5SEduard Zingerman err = regcomp(&re, pattern, REG_NOSUB);
59*898f55f5SEduard Zingerman if (err) {
60*898f55f5SEduard Zingerman char errbuf[512];
61*898f55f5SEduard Zingerman
62*898f55f5SEduard Zingerman regerror(err, &re, errbuf, sizeof(errbuf));
63*898f55f5SEduard Zingerman PRINT_FAIL("Can't compile regex: %s\n", errbuf);
64*898f55f5SEduard Zingerman return -1;
65*898f55f5SEduard Zingerman }
66*898f55f5SEduard Zingerman rc = regexec(&re, string, 0, NULL, 0);
67*898f55f5SEduard Zingerman regfree(&re);
68*898f55f5SEduard Zingerman return rc == 0 ? 1 : 0;
69*898f55f5SEduard Zingerman }
70*898f55f5SEduard Zingerman
test_spin_lock_fail_prog(const char * prog_name,const char * err_msg)71c48748aeSKumar Kartikeya Dwivedi static void test_spin_lock_fail_prog(const char *prog_name, const char *err_msg)
72c48748aeSKumar Kartikeya Dwivedi {
73c48748aeSKumar Kartikeya Dwivedi LIBBPF_OPTS(bpf_object_open_opts, opts, .kernel_log_buf = log_buf,
74c48748aeSKumar Kartikeya Dwivedi .kernel_log_size = sizeof(log_buf),
75c48748aeSKumar Kartikeya Dwivedi .kernel_log_level = 1);
76c48748aeSKumar Kartikeya Dwivedi struct test_spin_lock_fail *skel;
77c48748aeSKumar Kartikeya Dwivedi struct bpf_program *prog;
78c48748aeSKumar Kartikeya Dwivedi int ret;
79c48748aeSKumar Kartikeya Dwivedi
80c48748aeSKumar Kartikeya Dwivedi skel = test_spin_lock_fail__open_opts(&opts);
81c48748aeSKumar Kartikeya Dwivedi if (!ASSERT_OK_PTR(skel, "test_spin_lock_fail__open_opts"))
82c48748aeSKumar Kartikeya Dwivedi return;
83c48748aeSKumar Kartikeya Dwivedi
84c48748aeSKumar Kartikeya Dwivedi prog = bpf_object__find_program_by_name(skel->obj, prog_name);
85c48748aeSKumar Kartikeya Dwivedi if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
86c48748aeSKumar Kartikeya Dwivedi goto end;
87c48748aeSKumar Kartikeya Dwivedi
88c48748aeSKumar Kartikeya Dwivedi bpf_program__set_autoload(prog, true);
89c48748aeSKumar Kartikeya Dwivedi
90c48748aeSKumar Kartikeya Dwivedi ret = test_spin_lock_fail__load(skel);
91c48748aeSKumar Kartikeya Dwivedi if (!ASSERT_ERR(ret, "test_spin_lock_fail__load must fail"))
92c48748aeSKumar Kartikeya Dwivedi goto end;
93c48748aeSKumar Kartikeya Dwivedi
9497c11d6eSKumar Kartikeya Dwivedi /* Skip check if JIT does not support kfuncs */
9597c11d6eSKumar Kartikeya Dwivedi if (strstr(log_buf, "JIT does not support calling kernel function")) {
9697c11d6eSKumar Kartikeya Dwivedi test__skip();
9797c11d6eSKumar Kartikeya Dwivedi goto end;
9897c11d6eSKumar Kartikeya Dwivedi }
9997c11d6eSKumar Kartikeya Dwivedi
100*898f55f5SEduard Zingerman ret = match_regex(err_msg, log_buf);
101*898f55f5SEduard Zingerman if (!ASSERT_GE(ret, 0, "match_regex"))
102*898f55f5SEduard Zingerman goto end;
103*898f55f5SEduard Zingerman
104*898f55f5SEduard Zingerman if (!ASSERT_TRUE(ret, "no match for expected error message")) {
105c48748aeSKumar Kartikeya Dwivedi fprintf(stderr, "Expected: %s\n", err_msg);
106c48748aeSKumar Kartikeya Dwivedi fprintf(stderr, "Verifier: %s\n", log_buf);
107c48748aeSKumar Kartikeya Dwivedi }
108c48748aeSKumar Kartikeya Dwivedi
109c48748aeSKumar Kartikeya Dwivedi end:
110c48748aeSKumar Kartikeya Dwivedi test_spin_lock_fail__destroy(skel);
111c48748aeSKumar Kartikeya Dwivedi }
112d85aedacSKumar Kartikeya Dwivedi
spin_lock_thread(void * arg)113d85aedacSKumar Kartikeya Dwivedi static void *spin_lock_thread(void *arg)
114d85aedacSKumar Kartikeya Dwivedi {
115d85aedacSKumar Kartikeya Dwivedi int err, prog_fd = *(u32 *) arg;
116d85aedacSKumar Kartikeya Dwivedi LIBBPF_OPTS(bpf_test_run_opts, topts,
117d85aedacSKumar Kartikeya Dwivedi .data_in = &pkt_v4,
118d85aedacSKumar Kartikeya Dwivedi .data_size_in = sizeof(pkt_v4),
119d85aedacSKumar Kartikeya Dwivedi .repeat = 10000,
120d85aedacSKumar Kartikeya Dwivedi );
121d85aedacSKumar Kartikeya Dwivedi
122d85aedacSKumar Kartikeya Dwivedi err = bpf_prog_test_run_opts(prog_fd, &topts);
123d85aedacSKumar Kartikeya Dwivedi ASSERT_OK(err, "test_run");
124d85aedacSKumar Kartikeya Dwivedi ASSERT_OK(topts.retval, "test_run retval");
125d85aedacSKumar Kartikeya Dwivedi pthread_exit(arg);
126d85aedacSKumar Kartikeya Dwivedi }
127d85aedacSKumar Kartikeya Dwivedi
test_spin_lock_success(void)128c48748aeSKumar Kartikeya Dwivedi void test_spin_lock_success(void)
129d85aedacSKumar Kartikeya Dwivedi {
130d85aedacSKumar Kartikeya Dwivedi struct test_spin_lock *skel;
131d85aedacSKumar Kartikeya Dwivedi pthread_t thread_id[4];
132d85aedacSKumar Kartikeya Dwivedi int prog_fd, i;
133d85aedacSKumar Kartikeya Dwivedi void *ret;
134d85aedacSKumar Kartikeya Dwivedi
135d85aedacSKumar Kartikeya Dwivedi skel = test_spin_lock__open_and_load();
136d85aedacSKumar Kartikeya Dwivedi if (!ASSERT_OK_PTR(skel, "test_spin_lock__open_and_load"))
137d85aedacSKumar Kartikeya Dwivedi return;
138d85aedacSKumar Kartikeya Dwivedi prog_fd = bpf_program__fd(skel->progs.bpf_spin_lock_test);
139d85aedacSKumar Kartikeya Dwivedi for (i = 0; i < 4; i++) {
140d85aedacSKumar Kartikeya Dwivedi int err;
141d85aedacSKumar Kartikeya Dwivedi
142d85aedacSKumar Kartikeya Dwivedi err = pthread_create(&thread_id[i], NULL, &spin_lock_thread, &prog_fd);
143d85aedacSKumar Kartikeya Dwivedi if (!ASSERT_OK(err, "pthread_create"))
144d85aedacSKumar Kartikeya Dwivedi goto end;
145d85aedacSKumar Kartikeya Dwivedi }
146d85aedacSKumar Kartikeya Dwivedi
147d85aedacSKumar Kartikeya Dwivedi for (i = 0; i < 4; i++) {
148d85aedacSKumar Kartikeya Dwivedi if (!ASSERT_OK(pthread_join(thread_id[i], &ret), "pthread_join"))
149d85aedacSKumar Kartikeya Dwivedi goto end;
150d85aedacSKumar Kartikeya Dwivedi if (!ASSERT_EQ(ret, &prog_fd, "ret == prog_fd"))
151d85aedacSKumar Kartikeya Dwivedi goto end;
152d85aedacSKumar Kartikeya Dwivedi }
153d85aedacSKumar Kartikeya Dwivedi end:
154d85aedacSKumar Kartikeya Dwivedi test_spin_lock__destroy(skel);
155d85aedacSKumar Kartikeya Dwivedi }
156c48748aeSKumar Kartikeya Dwivedi
test_spin_lock(void)157c48748aeSKumar Kartikeya Dwivedi void test_spin_lock(void)
158c48748aeSKumar Kartikeya Dwivedi {
159c48748aeSKumar Kartikeya Dwivedi int i;
160c48748aeSKumar Kartikeya Dwivedi
161c48748aeSKumar Kartikeya Dwivedi test_spin_lock_success();
162c48748aeSKumar Kartikeya Dwivedi
163c48748aeSKumar Kartikeya Dwivedi for (i = 0; i < ARRAY_SIZE(spin_lock_fail_tests); i++) {
164c48748aeSKumar Kartikeya Dwivedi if (!test__start_subtest(spin_lock_fail_tests[i].prog_name))
165c48748aeSKumar Kartikeya Dwivedi continue;
166c48748aeSKumar Kartikeya Dwivedi test_spin_lock_fail_prog(spin_lock_fail_tests[i].prog_name,
167c48748aeSKumar Kartikeya Dwivedi spin_lock_fail_tests[i].err_msg);
168c48748aeSKumar Kartikeya Dwivedi }
169c48748aeSKumar Kartikeya Dwivedi }
170