bpf: btf: Tests for BPF_OBJ_GET_INFO_BY_FD and BPF_BTF_GET_FD_BY_ID

This patch adds test for BPF_BTF_GET_FD_BY_ID and the new
btf_id/btf_key_id/btf_value_id in the "struct bpf_map_info".

It also modifies the existing BPF_OBJ_GET_INFO_BY_FD test
to reflect the new "struct bpf_btf_info".

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Acked-by: Alexei Starovoitov <ast@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
This commit is contained in:
Martin KaFai Lau 2018-05-04 14:49:55 -07:00 committed by Daniel Borkmann
parent 7a01f6a3bd
commit cd8b89280c
3 changed files with 290 additions and 16 deletions

View File

@ -458,6 +458,16 @@ int bpf_map_get_fd_by_id(__u32 id)
return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
}
int bpf_btf_get_fd_by_id(__u32 id)
{
union bpf_attr attr;
bzero(&attr, sizeof(attr));
attr.btf_id = id;
return sys_bpf(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr));
}
int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
{
union bpf_attr attr;

View File

@ -98,6 +98,7 @@ int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
int bpf_prog_get_fd_by_id(__u32 id);
int bpf_map_get_fd_by_id(__u32 id);
int bpf_btf_get_fd_by_id(__u32 id);
int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len);
int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
__u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt);

View File

@ -1047,9 +1047,13 @@ struct btf_get_info_test {
const char *str_sec;
__u32 raw_types[MAX_NR_RAW_TYPES];
__u32 str_sec_size;
int info_size_delta;
int btf_size_delta;
int (*special_test)(unsigned int test_num);
};
static int test_big_btf_info(unsigned int test_num);
static int test_btf_id(unsigned int test_num);
const struct btf_get_info_test get_info_tests[] = {
{
.descr = "== raw_btf_size+1",
@ -1060,7 +1064,7 @@ const struct btf_get_info_test get_info_tests[] = {
},
.str_sec = "",
.str_sec_size = sizeof(""),
.info_size_delta = 1,
.btf_size_delta = 1,
},
{
.descr = "== raw_btf_size-3",
@ -1071,19 +1075,51 @@ const struct btf_get_info_test get_info_tests[] = {
},
.str_sec = "",
.str_sec_size = sizeof(""),
.info_size_delta = -3,
.btf_size_delta = -3,
},
{
.descr = "Large bpf_btf_info",
.raw_types = {
/* int */ /* [1] */
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
BTF_END_RAW,
},
.str_sec = "",
.str_sec_size = sizeof(""),
.special_test = test_big_btf_info,
},
{
.descr = "BTF ID",
.raw_types = {
/* int */ /* [1] */
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
/* unsigned int */ /* [2] */
BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
BTF_END_RAW,
},
.str_sec = "",
.str_sec_size = sizeof(""),
.special_test = test_btf_id,
},
};
static int do_test_get_info(unsigned int test_num)
static inline __u64 ptr_to_u64(const void *ptr)
{
return (__u64)(unsigned long)ptr;
}
static int test_big_btf_info(unsigned int test_num)
{
const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
unsigned int raw_btf_size, user_btf_size, expected_nbytes;
uint8_t *raw_btf = NULL, *user_btf = NULL;
unsigned int raw_btf_size;
struct {
struct bpf_btf_info info;
uint64_t garbage;
} info_garbage;
struct bpf_btf_info *info;
int btf_fd = -1, err;
fprintf(stderr, "BTF GET_INFO_BY_ID test[%u] (%s): ",
test_num, test->descr);
uint32_t info_len;
raw_btf = btf_raw_create(&hdr_tmpl,
test->raw_types,
@ -1110,19 +1146,246 @@ static int do_test_get_info(unsigned int test_num)
goto done;
}
user_btf_size = (int)raw_btf_size + test->info_size_delta;
/*
* GET_INFO should error out if the userspace info
* has non zero tailing bytes.
*/
info = &info_garbage.info;
memset(info, 0, sizeof(*info));
info_garbage.garbage = 0xdeadbeef;
info_len = sizeof(info_garbage);
info->btf = ptr_to_u64(user_btf);
info->btf_size = raw_btf_size;
err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
if (CHECK(!err, "!err")) {
err = -1;
goto done;
}
/*
* GET_INFO should succeed even info_len is larger than
* the kernel supported as long as tailing bytes are zero.
* The kernel supported info len should also be returned
* to userspace.
*/
info_garbage.garbage = 0;
err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
if (CHECK(err || info_len != sizeof(*info),
"err:%d errno:%d info_len:%u sizeof(*info):%lu",
err, errno, info_len, sizeof(*info))) {
err = -1;
goto done;
}
fprintf(stderr, "OK");
done:
if (*btf_log_buf && (err || args.always_log))
fprintf(stderr, "\n%s", btf_log_buf);
free(raw_btf);
free(user_btf);
if (btf_fd != -1)
close(btf_fd);
return err;
}
static int test_btf_id(unsigned int test_num)
{
const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
struct bpf_create_map_attr create_attr = {};
uint8_t *raw_btf = NULL, *user_btf[2] = {};
int btf_fd[2] = {-1, -1}, map_fd = -1;
struct bpf_map_info map_info = {};
struct bpf_btf_info info[2] = {};
unsigned int raw_btf_size;
uint32_t info_len;
int err, i, ret;
raw_btf = btf_raw_create(&hdr_tmpl,
test->raw_types,
test->str_sec,
test->str_sec_size,
&raw_btf_size);
if (!raw_btf)
return -1;
*btf_log_buf = '\0';
for (i = 0; i < 2; i++) {
user_btf[i] = malloc(raw_btf_size);
if (CHECK(!user_btf[i], "!user_btf[%d]", i)) {
err = -1;
goto done;
}
info[i].btf = ptr_to_u64(user_btf[i]);
info[i].btf_size = raw_btf_size;
}
btf_fd[0] = bpf_load_btf(raw_btf, raw_btf_size,
btf_log_buf, BTF_LOG_BUF_SIZE,
args.always_log);
if (CHECK(btf_fd[0] == -1, "errno:%d", errno)) {
err = -1;
goto done;
}
/* Test BPF_OBJ_GET_INFO_BY_ID on btf_id */
info_len = sizeof(info[0]);
err = bpf_obj_get_info_by_fd(btf_fd[0], &info[0], &info_len);
if (CHECK(err, "errno:%d", errno)) {
err = -1;
goto done;
}
btf_fd[1] = bpf_btf_get_fd_by_id(info[0].id);
if (CHECK(btf_fd[1] == -1, "errno:%d", errno)) {
err = -1;
goto done;
}
ret = 0;
err = bpf_obj_get_info_by_fd(btf_fd[1], &info[1], &info_len);
if (CHECK(err || info[0].id != info[1].id ||
info[0].btf_size != info[1].btf_size ||
(ret = memcmp(user_btf[0], user_btf[1], info[0].btf_size)),
"err:%d errno:%d id0:%u id1:%u btf_size0:%u btf_size1:%u memcmp:%d",
err, errno, info[0].id, info[1].id,
info[0].btf_size, info[1].btf_size, ret)) {
err = -1;
goto done;
}
/* Test btf members in struct bpf_map_info */
create_attr.name = "test_btf_id";
create_attr.map_type = BPF_MAP_TYPE_ARRAY;
create_attr.key_size = sizeof(int);
create_attr.value_size = sizeof(unsigned int);
create_attr.max_entries = 4;
create_attr.btf_fd = btf_fd[0];
create_attr.btf_key_id = 1;
create_attr.btf_value_id = 2;
map_fd = bpf_create_map_xattr(&create_attr);
if (CHECK(map_fd == -1, "errno:%d", errno)) {
err = -1;
goto done;
}
info_len = sizeof(map_info);
err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
if (CHECK(err || map_info.btf_id != info[0].id ||
map_info.btf_key_id != 1 || map_info.btf_value_id != 2,
"err:%d errno:%d info.id:%u btf_id:%u btf_key_id:%u btf_value_id:%u",
err, errno, info[0].id, map_info.btf_id, map_info.btf_key_id,
map_info.btf_value_id)) {
err = -1;
goto done;
}
for (i = 0; i < 2; i++) {
close(btf_fd[i]);
btf_fd[i] = -1;
}
/* Test BTF ID is removed from the kernel */
btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
if (CHECK(btf_fd[0] == -1, "errno:%d", errno)) {
err = -1;
goto done;
}
close(btf_fd[0]);
btf_fd[0] = -1;
/* The map holds the last ref to BTF and its btf_id */
close(map_fd);
map_fd = -1;
btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
if (CHECK(btf_fd[0] != -1, "BTF lingers")) {
err = -1;
goto done;
}
fprintf(stderr, "OK");
done:
if (*btf_log_buf && (err || args.always_log))
fprintf(stderr, "\n%s", btf_log_buf);
free(raw_btf);
if (map_fd != -1)
close(map_fd);
for (i = 0; i < 2; i++) {
free(user_btf[i]);
if (btf_fd[i] != -1)
close(btf_fd[i]);
}
return err;
}
static int do_test_get_info(unsigned int test_num)
{
const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
unsigned int raw_btf_size, user_btf_size, expected_nbytes;
uint8_t *raw_btf = NULL, *user_btf = NULL;
struct bpf_btf_info info = {};
int btf_fd = -1, err, ret;
uint32_t info_len;
fprintf(stderr, "BTF GET_INFO test[%u] (%s): ",
test_num, test->descr);
if (test->special_test)
return test->special_test(test_num);
raw_btf = btf_raw_create(&hdr_tmpl,
test->raw_types,
test->str_sec,
test->str_sec_size,
&raw_btf_size);
if (!raw_btf)
return -1;
*btf_log_buf = '\0';
user_btf = malloc(raw_btf_size);
if (CHECK(!user_btf, "!user_btf")) {
err = -1;
goto done;
}
btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
btf_log_buf, BTF_LOG_BUF_SIZE,
args.always_log);
if (CHECK(btf_fd == -1, "errno:%d", errno)) {
err = -1;
goto done;
}
user_btf_size = (int)raw_btf_size + test->btf_size_delta;
expected_nbytes = min(raw_btf_size, user_btf_size);
if (raw_btf_size > expected_nbytes)
memset(user_btf + expected_nbytes, 0xff,
raw_btf_size - expected_nbytes);
err = bpf_obj_get_info_by_fd(btf_fd, user_btf, &user_btf_size);
if (CHECK(err || user_btf_size != raw_btf_size ||
memcmp(raw_btf, user_btf, expected_nbytes),
"err:%d(errno:%d) raw_btf_size:%u user_btf_size:%u expected_nbytes:%u memcmp:%d",
err, errno,
raw_btf_size, user_btf_size, expected_nbytes,
memcmp(raw_btf, user_btf, expected_nbytes))) {
info_len = sizeof(info);
info.btf = ptr_to_u64(user_btf);
info.btf_size = user_btf_size;
ret = 0;
err = bpf_obj_get_info_by_fd(btf_fd, &info, &info_len);
if (CHECK(err || !info.id || info_len != sizeof(info) ||
info.btf_size != raw_btf_size ||
(ret = memcmp(raw_btf, user_btf, expected_nbytes)),
"err:%d errno:%d info.id:%u info_len:%u sizeof(info):%lu raw_btf_size:%u info.btf_size:%u expected_nbytes:%u memcmp:%d",
err, errno, info.id, info_len, sizeof(info),
raw_btf_size, info.btf_size, expected_nbytes, ret)) {
err = -1;
goto done;
}