Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (c) 2019 Facebook */
0003 #include <test_progs.h>
0004 #include <linux/bpf.h>
0005 #include "bpf/libbpf_internal.h"
0006 #include "test_raw_tp_test_run.skel.h"
0007 
0008 void test_raw_tp_test_run(void)
0009 {
0010     int comm_fd = -1, err, nr_online, i, prog_fd;
0011     __u64 args[2] = {0x1234ULL, 0x5678ULL};
0012     int expected_retval = 0x1234 + 0x5678;
0013     struct test_raw_tp_test_run *skel;
0014     char buf[] = "new_name";
0015     bool *online = NULL;
0016     LIBBPF_OPTS(bpf_test_run_opts, opts,
0017         .ctx_in = args,
0018         .ctx_size_in = sizeof(args),
0019         .flags = BPF_F_TEST_RUN_ON_CPU,
0020     );
0021 
0022     err = parse_cpu_mask_file("/sys/devices/system/cpu/online", &online,
0023                   &nr_online);
0024     if (!ASSERT_OK(err, "parse_cpu_mask_file"))
0025         return;
0026 
0027     skel = test_raw_tp_test_run__open_and_load();
0028     if (!ASSERT_OK_PTR(skel, "skel_open"))
0029         goto cleanup;
0030 
0031     err = test_raw_tp_test_run__attach(skel);
0032     if (!ASSERT_OK(err, "skel_attach"))
0033         goto cleanup;
0034 
0035     comm_fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
0036     if (!ASSERT_GE(comm_fd, 0, "open /proc/self/comm"))
0037         goto cleanup;
0038 
0039     err = write(comm_fd, buf, sizeof(buf));
0040     ASSERT_GE(err, 0, "task rename");
0041 
0042     ASSERT_NEQ(skel->bss->count, 0, "check_count");
0043     ASSERT_EQ(skel->data->on_cpu, 0xffffffff, "check_on_cpu");
0044 
0045     prog_fd = bpf_program__fd(skel->progs.rename);
0046     opts.ctx_in = args;
0047     opts.ctx_size_in = sizeof(__u64);
0048 
0049     err = bpf_prog_test_run_opts(prog_fd, &opts);
0050     ASSERT_NEQ(err, 0, "test_run should fail for too small ctx");
0051 
0052     opts.ctx_size_in = sizeof(args);
0053     err = bpf_prog_test_run_opts(prog_fd, &opts);
0054     ASSERT_OK(err, "test_run");
0055     ASSERT_EQ(opts.retval, expected_retval, "check_retval");
0056 
0057     for (i = 0; i < nr_online; i++) {
0058         if (!online[i])
0059             continue;
0060 
0061         opts.cpu = i;
0062         opts.retval = 0;
0063         err = bpf_prog_test_run_opts(prog_fd, &opts);
0064         ASSERT_OK(err, "test_run_opts");
0065         ASSERT_EQ(skel->data->on_cpu, i, "check_on_cpu");
0066         ASSERT_EQ(opts.retval, expected_retval, "check_retval");
0067     }
0068 
0069     /* invalid cpu ID should fail with ENXIO */
0070     opts.cpu = 0xffffffff;
0071     err = bpf_prog_test_run_opts(prog_fd, &opts);
0072     ASSERT_EQ(errno, ENXIO, "test_run_opts should fail with ENXIO");
0073     ASSERT_ERR(err, "test_run_opts_fail");
0074 
0075     /* non-zero cpu w/o BPF_F_TEST_RUN_ON_CPU should fail with EINVAL */
0076     opts.cpu = 1;
0077     opts.flags = 0;
0078     err = bpf_prog_test_run_opts(prog_fd, &opts);
0079     ASSERT_EQ(errno, EINVAL, "test_run_opts should fail with EINVAL");
0080     ASSERT_ERR(err, "test_run_opts_fail");
0081 
0082 cleanup:
0083     close(comm_fd);
0084     test_raw_tp_test_run__destroy(skel);
0085     free(online);
0086 }