1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
4 #include <linux/bpf.h>
5 #include "bpf/libbpf_internal.h"
6 #include "test_raw_tp_test_run.skel.h"
7
test_raw_tp_test_run(void)8 void test_raw_tp_test_run(void)
9 {
10 int comm_fd = -1, err, nr_online, i, prog_fd;
11 __u64 args[2] = {0x1234ULL, 0x5678ULL};
12 int expected_retval = 0x1234 + 0x5678;
13 struct test_raw_tp_test_run *skel;
14 char buf[] = "new_name";
15 bool *online = NULL;
16 LIBBPF_OPTS(bpf_test_run_opts, opts,
17 .ctx_in = args,
18 .ctx_size_in = sizeof(args),
19 .flags = BPF_F_TEST_RUN_ON_CPU,
20 );
21
22 err = parse_cpu_mask_file("/sys/devices/system/cpu/online", &online,
23 &nr_online);
24 if (!ASSERT_OK(err, "parse_cpu_mask_file"))
25 return;
26
27 skel = test_raw_tp_test_run__open_and_load();
28 if (!ASSERT_OK_PTR(skel, "skel_open"))
29 goto cleanup;
30
31 err = test_raw_tp_test_run__attach(skel);
32 if (!ASSERT_OK(err, "skel_attach"))
33 goto cleanup;
34
35 comm_fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
36 if (!ASSERT_GE(comm_fd, 0, "open /proc/self/comm"))
37 goto cleanup;
38
39 err = write(comm_fd, buf, sizeof(buf));
40 ASSERT_GE(err, 0, "task rename");
41
42 ASSERT_NEQ(skel->bss->count, 0, "check_count");
43 ASSERT_EQ(skel->data->on_cpu, 0xffffffff, "check_on_cpu");
44
45 prog_fd = bpf_program__fd(skel->progs.rename);
46 opts.ctx_in = args;
47 opts.ctx_size_in = sizeof(__u64);
48
49 err = bpf_prog_test_run_opts(prog_fd, &opts);
50 ASSERT_NEQ(err, 0, "test_run should fail for too small ctx");
51
52 opts.ctx_size_in = sizeof(args);
53 err = bpf_prog_test_run_opts(prog_fd, &opts);
54 ASSERT_OK(err, "test_run");
55 ASSERT_EQ(opts.retval, expected_retval, "check_retval");
56
57 for (i = 0; i < nr_online; i++) {
58 if (!online[i])
59 continue;
60
61 opts.cpu = i;
62 opts.retval = 0;
63 err = bpf_prog_test_run_opts(prog_fd, &opts);
64 ASSERT_OK(err, "test_run_opts");
65 ASSERT_EQ(skel->data->on_cpu, i, "check_on_cpu");
66 ASSERT_EQ(opts.retval, expected_retval, "check_retval");
67 }
68
69 /* invalid cpu ID should fail with ENXIO */
70 opts.cpu = 0xffffffff;
71 err = bpf_prog_test_run_opts(prog_fd, &opts);
72 ASSERT_EQ(errno, ENXIO, "test_run_opts should fail with ENXIO");
73 ASSERT_ERR(err, "test_run_opts_fail");
74
75 /* non-zero cpu w/o BPF_F_TEST_RUN_ON_CPU should fail with EINVAL */
76 opts.cpu = 1;
77 opts.flags = 0;
78 err = bpf_prog_test_run_opts(prog_fd, &opts);
79 ASSERT_EQ(errno, EINVAL, "test_run_opts should fail with EINVAL");
80 ASSERT_ERR(err, "test_run_opts_fail");
81
82 cleanup:
83 close(comm_fd);
84 test_raw_tp_test_run__destroy(skel);
85 free(online);
86 }
87