Commit 61746dbe authored by Martin KaFai Lau's avatar Martin KaFai Lau Committed by Daniel Borkmann

bpf: btf: Add tests for the btf uapi changes

This patch does the followings:
1. Modify libbpf and test_btf to reflect the uapi changes in btf
2. Add test for the btf_header changes
3. Add tests for array->index_type
4. Add err_str check to the tests
5. Fix a 4 bytes hole in "struct test #1" by swapping "m" and "n"
Signed-off-by: default avatarMartin KaFai Lau <kafai@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent f03b15d3
...@@ -89,8 +89,8 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr) ...@@ -89,8 +89,8 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
min(name_len, BPF_OBJ_NAME_LEN - 1)); min(name_len, BPF_OBJ_NAME_LEN - 1));
attr.numa_node = create_attr->numa_node; attr.numa_node = create_attr->numa_node;
attr.btf_fd = create_attr->btf_fd; attr.btf_fd = create_attr->btf_fd;
attr.btf_key_id = create_attr->btf_key_id; attr.btf_key_type_id = create_attr->btf_key_type_id;
attr.btf_value_id = create_attr->btf_value_id; attr.btf_value_type_id = create_attr->btf_value_type_id;
attr.map_ifindex = create_attr->map_ifindex; attr.map_ifindex = create_attr->map_ifindex;
return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
......
...@@ -36,8 +36,8 @@ struct bpf_create_map_attr { ...@@ -36,8 +36,8 @@ struct bpf_create_map_attr {
__u32 max_entries; __u32 max_entries;
__u32 numa_node; __u32 numa_node;
__u32 btf_fd; __u32 btf_fd;
__u32 btf_key_id; __u32 btf_key_type_id;
__u32 btf_value_id; __u32 btf_value_type_id;
__u32 map_ifindex; __u32 map_ifindex;
}; };
......
...@@ -35,9 +35,8 @@ struct btf { ...@@ -35,9 +35,8 @@ struct btf {
static const char *btf_name_by_offset(const struct btf *btf, uint32_t offset) static const char *btf_name_by_offset(const struct btf *btf, uint32_t offset)
{ {
if (!BTF_STR_TBL_ELF_ID(offset) && if (offset < btf->hdr->str_len)
BTF_STR_OFFSET(offset) < btf->hdr->str_len) return &btf->strings[offset];
return &btf->strings[BTF_STR_OFFSET(offset)];
else else
return NULL; return NULL;
} }
......
...@@ -216,8 +216,8 @@ struct bpf_map { ...@@ -216,8 +216,8 @@ struct bpf_map {
size_t offset; size_t offset;
int map_ifindex; int map_ifindex;
struct bpf_map_def def; struct bpf_map_def def;
uint32_t btf_key_id; uint32_t btf_key_type_id;
uint32_t btf_value_id; uint32_t btf_value_type_id;
void *priv; void *priv;
bpf_map_clear_priv_t clear_priv; bpf_map_clear_priv_t clear_priv;
}; };
...@@ -1074,8 +1074,8 @@ static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf) ...@@ -1074,8 +1074,8 @@ static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf)
return -EINVAL; return -EINVAL;
} }
map->btf_key_id = key_id; map->btf_key_type_id = key_id;
map->btf_value_id = value_id; map->btf_value_type_id = value_id;
return 0; return 0;
} }
...@@ -1100,24 +1100,24 @@ bpf_object__create_maps(struct bpf_object *obj) ...@@ -1100,24 +1100,24 @@ bpf_object__create_maps(struct bpf_object *obj)
create_attr.value_size = def->value_size; create_attr.value_size = def->value_size;
create_attr.max_entries = def->max_entries; create_attr.max_entries = def->max_entries;
create_attr.btf_fd = 0; create_attr.btf_fd = 0;
create_attr.btf_key_id = 0; create_attr.btf_key_type_id = 0;
create_attr.btf_value_id = 0; create_attr.btf_value_type_id = 0;
if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) { if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) {
create_attr.btf_fd = btf__fd(obj->btf); create_attr.btf_fd = btf__fd(obj->btf);
create_attr.btf_key_id = map->btf_key_id; create_attr.btf_key_type_id = map->btf_key_type_id;
create_attr.btf_value_id = map->btf_value_id; create_attr.btf_value_type_id = map->btf_value_type_id;
} }
*pfd = bpf_create_map_xattr(&create_attr); *pfd = bpf_create_map_xattr(&create_attr);
if (*pfd < 0 && create_attr.btf_key_id) { if (*pfd < 0 && create_attr.btf_key_type_id) {
pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n", pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
map->name, strerror(errno), errno); map->name, strerror(errno), errno);
create_attr.btf_fd = 0; create_attr.btf_fd = 0;
create_attr.btf_key_id = 0; create_attr.btf_key_type_id = 0;
create_attr.btf_value_id = 0; create_attr.btf_value_type_id = 0;
map->btf_key_id = 0; map->btf_key_type_id = 0;
map->btf_value_id = 0; map->btf_value_type_id = 0;
*pfd = bpf_create_map_xattr(&create_attr); *pfd = bpf_create_map_xattr(&create_attr);
} }
...@@ -2085,14 +2085,14 @@ const char *bpf_map__name(struct bpf_map *map) ...@@ -2085,14 +2085,14 @@ const char *bpf_map__name(struct bpf_map *map)
return map ? map->name : NULL; return map ? map->name : NULL;
} }
uint32_t bpf_map__btf_key_id(const struct bpf_map *map) uint32_t bpf_map__btf_key_type_id(const struct bpf_map *map)
{ {
return map ? map->btf_key_id : 0; return map ? map->btf_key_type_id : 0;
} }
uint32_t bpf_map__btf_value_id(const struct bpf_map *map) uint32_t bpf_map__btf_value_type_id(const struct bpf_map *map)
{ {
return map ? map->btf_value_id : 0; return map ? map->btf_value_type_id : 0;
} }
int bpf_map__set_priv(struct bpf_map *map, void *priv, int bpf_map__set_priv(struct bpf_map *map, void *priv,
......
...@@ -244,8 +244,8 @@ bpf_map__next(struct bpf_map *map, struct bpf_object *obj); ...@@ -244,8 +244,8 @@ bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
int bpf_map__fd(struct bpf_map *map); int bpf_map__fd(struct bpf_map *map);
const struct bpf_map_def *bpf_map__def(struct bpf_map *map); const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
const char *bpf_map__name(struct bpf_map *map); const char *bpf_map__name(struct bpf_map *map);
uint32_t bpf_map__btf_key_id(const struct bpf_map *map); uint32_t bpf_map__btf_key_type_id(const struct bpf_map *map);
uint32_t bpf_map__btf_value_id(const struct bpf_map *map); uint32_t bpf_map__btf_value_type_id(const struct bpf_map *map);
typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
int bpf_map__set_priv(struct bpf_map *map, void *priv, int bpf_map__set_priv(struct bpf_map *map, void *priv,
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment