mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00
selftests/bpf: Factor out get_xlated_program() helper
Both test_verifier and test_progs use get_xlated_program(), so moving the helper into testing_helpers.h to reuse it. Acked-by: Eduard Zingerman <eddyz87@gmail.com> Signed-off-by: Hou Tao <houtao1@huawei.com> Link: https://lore.kernel.org/r/20240105104819.3916743-3-houtao@huaweicloud.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
parent
7c05e7f3e7
commit
b4b7a4099b
4 changed files with 50 additions and 89 deletions
|
@ -626,50 +626,6 @@ err:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Request BPF program instructions after all rewrites are applied,
|
|
||||||
* e.g. verifier.c:convert_ctx_access() is done.
|
|
||||||
*/
|
|
||||||
static int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt)
|
|
||||||
{
|
|
||||||
struct bpf_prog_info info = {};
|
|
||||||
__u32 info_len = sizeof(info);
|
|
||||||
__u32 xlated_prog_len;
|
|
||||||
__u32 buf_element_size = sizeof(struct bpf_insn);
|
|
||||||
|
|
||||||
if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
|
|
||||||
perror("bpf_prog_get_info_by_fd failed");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
xlated_prog_len = info.xlated_prog_len;
|
|
||||||
if (xlated_prog_len % buf_element_size) {
|
|
||||||
printf("Program length %d is not multiple of %d\n",
|
|
||||||
xlated_prog_len, buf_element_size);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
*cnt = xlated_prog_len / buf_element_size;
|
|
||||||
*buf = calloc(*cnt, buf_element_size);
|
|
||||||
if (!buf) {
|
|
||||||
perror("can't allocate xlated program buffer");
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
bzero(&info, sizeof(info));
|
|
||||||
info.xlated_prog_len = xlated_prog_len;
|
|
||||||
info.xlated_prog_insns = (__u64)(unsigned long)*buf;
|
|
||||||
if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
|
|
||||||
perror("second bpf_prog_get_info_by_fd failed");
|
|
||||||
goto out_free_buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
out_free_buf:
|
|
||||||
free(*buf);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void print_insn(void *private_data, const char *fmt, ...)
|
static void print_insn(void *private_data, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
|
@ -1341,48 +1341,6 @@ static bool cmp_str_seq(const char *log, const char *exp)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct bpf_insn *get_xlated_program(int fd_prog, int *cnt)
|
|
||||||
{
|
|
||||||
__u32 buf_element_size = sizeof(struct bpf_insn);
|
|
||||||
struct bpf_prog_info info = {};
|
|
||||||
__u32 info_len = sizeof(info);
|
|
||||||
__u32 xlated_prog_len;
|
|
||||||
struct bpf_insn *buf;
|
|
||||||
|
|
||||||
if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
|
|
||||||
perror("bpf_prog_get_info_by_fd failed");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
xlated_prog_len = info.xlated_prog_len;
|
|
||||||
if (xlated_prog_len % buf_element_size) {
|
|
||||||
printf("Program length %d is not multiple of %d\n",
|
|
||||||
xlated_prog_len, buf_element_size);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
*cnt = xlated_prog_len / buf_element_size;
|
|
||||||
buf = calloc(*cnt, buf_element_size);
|
|
||||||
if (!buf) {
|
|
||||||
perror("can't allocate xlated program buffer");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
bzero(&info, sizeof(info));
|
|
||||||
info.xlated_prog_len = xlated_prog_len;
|
|
||||||
info.xlated_prog_insns = (__u64)(unsigned long)buf;
|
|
||||||
if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
|
|
||||||
perror("second bpf_prog_get_info_by_fd failed");
|
|
||||||
goto out_free_buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
return buf;
|
|
||||||
|
|
||||||
out_free_buf:
|
|
||||||
free(buf);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool is_null_insn(struct bpf_insn *insn)
|
static bool is_null_insn(struct bpf_insn *insn)
|
||||||
{
|
{
|
||||||
struct bpf_insn null_insn = {};
|
struct bpf_insn null_insn = {};
|
||||||
|
@ -1505,7 +1463,7 @@ static void print_insn(struct bpf_insn *buf, int cnt)
|
||||||
static bool check_xlated_program(struct bpf_test *test, int fd_prog)
|
static bool check_xlated_program(struct bpf_test *test, int fd_prog)
|
||||||
{
|
{
|
||||||
struct bpf_insn *buf;
|
struct bpf_insn *buf;
|
||||||
int cnt;
|
unsigned int cnt;
|
||||||
bool result = true;
|
bool result = true;
|
||||||
bool check_expected = !is_null_insn(test->expected_insns);
|
bool check_expected = !is_null_insn(test->expected_insns);
|
||||||
bool check_unexpected = !is_null_insn(test->unexpected_insns);
|
bool check_unexpected = !is_null_insn(test->unexpected_insns);
|
||||||
|
@ -1513,8 +1471,7 @@ static bool check_xlated_program(struct bpf_test *test, int fd_prog)
|
||||||
if (!check_expected && !check_unexpected)
|
if (!check_expected && !check_unexpected)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
buf = get_xlated_program(fd_prog, &cnt);
|
if (get_xlated_program(fd_prog, &buf, &cnt)) {
|
||||||
if (!buf) {
|
|
||||||
printf("FAIL: can't get xlated program\n");
|
printf("FAIL: can't get xlated program\n");
|
||||||
result = false;
|
result = false;
|
||||||
goto out;
|
goto out;
|
||||||
|
|
|
@ -387,3 +387,45 @@ int kern_sync_rcu(void)
|
||||||
{
|
{
|
||||||
return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0, 0);
|
return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt)
|
||||||
|
{
|
||||||
|
__u32 buf_element_size = sizeof(struct bpf_insn);
|
||||||
|
struct bpf_prog_info info = {};
|
||||||
|
__u32 info_len = sizeof(info);
|
||||||
|
__u32 xlated_prog_len;
|
||||||
|
|
||||||
|
if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
|
||||||
|
perror("bpf_prog_get_info_by_fd failed");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
xlated_prog_len = info.xlated_prog_len;
|
||||||
|
if (xlated_prog_len % buf_element_size) {
|
||||||
|
printf("Program length %u is not multiple of %u\n",
|
||||||
|
xlated_prog_len, buf_element_size);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*cnt = xlated_prog_len / buf_element_size;
|
||||||
|
*buf = calloc(*cnt, buf_element_size);
|
||||||
|
if (!buf) {
|
||||||
|
perror("can't allocate xlated program buffer");
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
bzero(&info, sizeof(info));
|
||||||
|
info.xlated_prog_len = xlated_prog_len;
|
||||||
|
info.xlated_prog_insns = (__u64)(unsigned long)*buf;
|
||||||
|
if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
|
||||||
|
perror("second bpf_prog_get_info_by_fd failed");
|
||||||
|
goto out_free_buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
out_free_buf:
|
||||||
|
free(*buf);
|
||||||
|
*buf = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
|
@ -46,4 +46,10 @@ static inline __u64 get_time_ns(void)
|
||||||
return (u64)t.tv_sec * 1000000000 + t.tv_nsec;
|
return (u64)t.tv_sec * 1000000000 + t.tv_nsec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct bpf_insn;
|
||||||
|
/* Request BPF program instructions after all rewrites are applied,
|
||||||
|
* e.g. verifier.c:convert_ctx_access() is done.
|
||||||
|
*/
|
||||||
|
int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt);
|
||||||
|
|
||||||
#endif /* __TESTING_HELPERS_H */
|
#endif /* __TESTING_HELPERS_H */
|
||||||
|
|
Loading…
Add table
Reference in a new issue