2023-08-30 16:31:07 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Copyright (c) 2022 Facebook */
|
|
|
|
|
|
|
|
#include <test_progs.h>
|
2023-10-24 11:59:35 +01:00
|
|
|
#include <network_helpers.h>
|
2023-08-30 16:31:07 +01:00
|
|
|
#include "dynptr_fail.skel.h"
|
|
|
|
#include "dynptr_success.skel.h"
|
|
|
|
|
2023-10-24 11:59:35 +01:00
|
|
|
enum test_setup_type {
|
|
|
|
SETUP_SYSCALL_SLEEP,
|
|
|
|
SETUP_SKB_PROG,
|
2023-08-30 16:31:07 +01:00
|
|
|
};
|
|
|
|
|
2023-10-24 11:59:35 +01:00
|
|
|
static struct {
|
|
|
|
const char *prog_name;
|
|
|
|
enum test_setup_type type;
|
|
|
|
} success_tests[] = {
|
|
|
|
{"test_read_write", SETUP_SYSCALL_SLEEP},
|
|
|
|
{"test_dynptr_data", SETUP_SYSCALL_SLEEP},
|
|
|
|
{"test_ringbuf", SETUP_SYSCALL_SLEEP},
|
|
|
|
{"test_skb_readonly", SETUP_SKB_PROG},
|
|
|
|
{"test_dynptr_skb_data", SETUP_SKB_PROG},
|
|
|
|
{"test_adjust", SETUP_SYSCALL_SLEEP},
|
|
|
|
{"test_adjust_err", SETUP_SYSCALL_SLEEP},
|
|
|
|
{"test_zero_size_dynptr", SETUP_SYSCALL_SLEEP},
|
|
|
|
{"test_dynptr_is_null", SETUP_SYSCALL_SLEEP},
|
|
|
|
{"test_dynptr_is_rdonly", SETUP_SKB_PROG},
|
|
|
|
{"test_dynptr_clone", SETUP_SKB_PROG},
|
|
|
|
{"test_dynptr_skb_no_buff", SETUP_SKB_PROG},
|
|
|
|
{"test_dynptr_skb_strcmp", SETUP_SKB_PROG},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void verify_success(const char *prog_name, enum test_setup_type setup_type)
|
2023-08-30 16:31:07 +01:00
|
|
|
{
|
|
|
|
struct dynptr_success *skel;
|
|
|
|
struct bpf_program *prog;
|
|
|
|
struct bpf_link *link;
|
2023-10-24 11:59:35 +01:00
|
|
|
int err;
|
2023-08-30 16:31:07 +01:00
|
|
|
|
|
|
|
skel = dynptr_success__open();
|
|
|
|
if (!ASSERT_OK_PTR(skel, "dynptr_success__open"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
skel->bss->pid = getpid();
|
|
|
|
|
|
|
|
prog = bpf_object__find_program_by_name(skel->obj, prog_name);
|
|
|
|
if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
|
|
|
|
goto cleanup;
|
|
|
|
|
2023-10-24 11:59:35 +01:00
|
|
|
bpf_program__set_autoload(prog, true);
|
|
|
|
|
|
|
|
err = dynptr_success__load(skel);
|
|
|
|
if (!ASSERT_OK(err, "dynptr_success__load"))
|
2023-08-30 16:31:07 +01:00
|
|
|
goto cleanup;
|
|
|
|
|
2023-10-24 11:59:35 +01:00
|
|
|
switch (setup_type) {
|
|
|
|
case SETUP_SYSCALL_SLEEP:
|
|
|
|
link = bpf_program__attach(prog);
|
|
|
|
if (!ASSERT_OK_PTR(link, "bpf_program__attach"))
|
|
|
|
goto cleanup;
|
2023-08-30 16:31:07 +01:00
|
|
|
|
2023-10-24 11:59:35 +01:00
|
|
|
usleep(1);
|
|
|
|
|
|
|
|
bpf_link__destroy(link);
|
|
|
|
break;
|
|
|
|
case SETUP_SKB_PROG:
|
|
|
|
{
|
|
|
|
int prog_fd;
|
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
LIBBPF_OPTS(bpf_test_run_opts, topts,
|
|
|
|
.data_in = &pkt_v4,
|
|
|
|
.data_size_in = sizeof(pkt_v4),
|
|
|
|
.data_out = buf,
|
|
|
|
.data_size_out = sizeof(buf),
|
|
|
|
.repeat = 1,
|
|
|
|
);
|
2023-08-30 16:31:07 +01:00
|
|
|
|
2023-10-24 11:59:35 +01:00
|
|
|
prog_fd = bpf_program__fd(prog);
|
|
|
|
if (!ASSERT_GE(prog_fd, 0, "prog_fd"))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
err = bpf_prog_test_run_opts(prog_fd, &topts);
|
|
|
|
|
|
|
|
if (!ASSERT_OK(err, "test_run"))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(skel->bss->err, 0, "err");
|
2023-08-30 16:31:07 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
dynptr_success__destroy(skel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_dynptr(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(success_tests); i++) {
|
2023-10-24 11:59:35 +01:00
|
|
|
if (!test__start_subtest(success_tests[i].prog_name))
|
2023-08-30 16:31:07 +01:00
|
|
|
continue;
|
|
|
|
|
2023-10-24 11:59:35 +01:00
|
|
|
verify_success(success_tests[i].prog_name, success_tests[i].type);
|
2023-08-30 16:31:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RUN_TESTS(dynptr_fail);
|
|
|
|
}
|