1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
5 #include "bpf/libbpf_internal.h"
6 #include "test_raw_tp_test_run.skel.h"
8 void test_raw_tp_test_run(void)
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";
16 LIBBPF_OPTS(bpf_test_run_opts, opts,
18 .ctx_size_in = sizeof(args),
19 .flags = BPF_F_TEST_RUN_ON_CPU,
22 err = parse_cpu_mask_file("/sys/devices/system/cpu/online", &online,
24 if (!ASSERT_OK(err, "parse_cpu_mask_file"))
27 skel = test_raw_tp_test_run__open_and_load();
28 if (!ASSERT_OK_PTR(skel, "skel_open"))
31 err = test_raw_tp_test_run__attach(skel);
32 if (!ASSERT_OK(err, "skel_attach"))
35 comm_fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
36 if (!ASSERT_GE(comm_fd, 0, "open /proc/self/comm"))
39 err = write(comm_fd, buf, sizeof(buf));
40 ASSERT_GE(err, 0, "task rename");
42 ASSERT_NEQ(skel->bss->count, 0, "check_count");
43 ASSERT_EQ(skel->data->on_cpu, 0xffffffff, "check_on_cpu");
45 prog_fd = bpf_program__fd(skel->progs.rename);
47 opts.ctx_size_in = sizeof(__u64);
49 err = bpf_prog_test_run_opts(prog_fd, &opts);
50 ASSERT_NEQ(err, 0, "test_run should fail for too small ctx");
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");
57 for (i = 0; i < nr_online; i++) {
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");
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");
75 /* non-zero cpu w/o BPF_F_TEST_RUN_ON_CPU should fail with EINVAL */
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");
84 test_raw_tp_test_run__destroy(skel);