aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf
diff options
context:
space:
mode:
authorMartin KaFai Lau <kafai@fb.com>2018-05-22 18:04:24 -0400
committerDaniel Borkmann <daniel@iogearbox.net>2018-05-23 06:03:32 -0400
commit61746dbe1aa27c9e23293621665b8442dfed7698 (patch)
tree10cf3d584e25f564d0cbc3653eb428dd21962f1d /tools/lib/bpf
parentf03b15d34bd805e57bf69523b4dca7af10e9eeb1 (diff)
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: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/lib/bpf')
-rw-r--r--tools/lib/bpf/bpf.c4
-rw-r--r--tools/lib/bpf/bpf.h4
-rw-r--r--tools/lib/bpf/btf.c5
-rw-r--r--tools/lib/bpf/libbpf.c34
-rw-r--r--tools/lib/bpf/libbpf.h4
5 files changed, 25 insertions, 26 deletions
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 6a8a00097fd8..442b4cdfeb71 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -89,8 +89,8 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
89 min(name_len, BPF_OBJ_NAME_LEN - 1)); 89 min(name_len, BPF_OBJ_NAME_LEN - 1));
90 attr.numa_node = create_attr->numa_node; 90 attr.numa_node = create_attr->numa_node;
91 attr.btf_fd = create_attr->btf_fd; 91 attr.btf_fd = create_attr->btf_fd;
92 attr.btf_key_id = create_attr->btf_key_id; 92 attr.btf_key_type_id = create_attr->btf_key_type_id;
93 attr.btf_value_id = create_attr->btf_value_id; 93 attr.btf_value_type_id = create_attr->btf_value_type_id;
94 attr.map_ifindex = create_attr->map_ifindex; 94 attr.map_ifindex = create_attr->map_ifindex;
95 95
96 return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); 96 return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 15bff7728cf1..d12344f66d4e 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -36,8 +36,8 @@ struct bpf_create_map_attr {
36 __u32 max_entries; 36 __u32 max_entries;
37 __u32 numa_node; 37 __u32 numa_node;
38 __u32 btf_fd; 38 __u32 btf_fd;
39 __u32 btf_key_id; 39 __u32 btf_key_type_id;
40 __u32 btf_value_id; 40 __u32 btf_value_type_id;
41 __u32 map_ifindex; 41 __u32 map_ifindex;
42}; 42};
43 43
diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 2bac710e3194..8c54a4b6f187 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -35,9 +35,8 @@ struct btf {
35 35
36static const char *btf_name_by_offset(const struct btf *btf, uint32_t offset) 36static const char *btf_name_by_offset(const struct btf *btf, uint32_t offset)
37{ 37{
38 if (!BTF_STR_TBL_ELF_ID(offset) && 38 if (offset < btf->hdr->str_len)
39 BTF_STR_OFFSET(offset) < btf->hdr->str_len) 39 return &btf->strings[offset];
40 return &btf->strings[BTF_STR_OFFSET(offset)];
41 else 40 else
42 return NULL; 41 return NULL;
43} 42}
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 3dbe217bf23e..8f1707dbfcfa 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -216,8 +216,8 @@ struct bpf_map {
216 size_t offset; 216 size_t offset;
217 int map_ifindex; 217 int map_ifindex;
218 struct bpf_map_def def; 218 struct bpf_map_def def;
219 uint32_t btf_key_id; 219 uint32_t btf_key_type_id;
220 uint32_t btf_value_id; 220 uint32_t btf_value_type_id;
221 void *priv; 221 void *priv;
222 bpf_map_clear_priv_t clear_priv; 222 bpf_map_clear_priv_t clear_priv;
223}; 223};
@@ -1074,8 +1074,8 @@ static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf)
1074 return -EINVAL; 1074 return -EINVAL;
1075 } 1075 }
1076 1076
1077 map->btf_key_id = key_id; 1077 map->btf_key_type_id = key_id;
1078 map->btf_value_id = value_id; 1078 map->btf_value_type_id = value_id;
1079 1079
1080 return 0; 1080 return 0;
1081} 1081}
@@ -1100,24 +1100,24 @@ bpf_object__create_maps(struct bpf_object *obj)
1100 create_attr.value_size = def->value_size; 1100 create_attr.value_size = def->value_size;
1101 create_attr.max_entries = def->max_entries; 1101 create_attr.max_entries = def->max_entries;
1102 create_attr.btf_fd = 0; 1102 create_attr.btf_fd = 0;
1103 create_attr.btf_key_id = 0; 1103 create_attr.btf_key_type_id = 0;
1104 create_attr.btf_value_id = 0; 1104 create_attr.btf_value_type_id = 0;
1105 1105
1106 if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) { 1106 if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) {
1107 create_attr.btf_fd = btf__fd(obj->btf); 1107 create_attr.btf_fd = btf__fd(obj->btf);
1108 create_attr.btf_key_id = map->btf_key_id; 1108 create_attr.btf_key_type_id = map->btf_key_type_id;
1109 create_attr.btf_value_id = map->btf_value_id; 1109 create_attr.btf_value_type_id = map->btf_value_type_id;
1110 } 1110 }
1111 1111
1112 *pfd = bpf_create_map_xattr(&create_attr); 1112 *pfd = bpf_create_map_xattr(&create_attr);
1113 if (*pfd < 0 && create_attr.btf_key_id) { 1113 if (*pfd < 0 && create_attr.btf_key_type_id) {
1114 pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n", 1114 pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
1115 map->name, strerror(errno), errno); 1115 map->name, strerror(errno), errno);
1116 create_attr.btf_fd = 0; 1116 create_attr.btf_fd = 0;
1117 create_attr.btf_key_id = 0; 1117 create_attr.btf_key_type_id = 0;
1118 create_attr.btf_value_id = 0; 1118 create_attr.btf_value_type_id = 0;
1119 map->btf_key_id = 0; 1119 map->btf_key_type_id = 0;
1120 map->btf_value_id = 0; 1120 map->btf_value_type_id = 0;
1121 *pfd = bpf_create_map_xattr(&create_attr); 1121 *pfd = bpf_create_map_xattr(&create_attr);
1122 } 1122 }
1123 1123
@@ -2085,14 +2085,14 @@ const char *bpf_map__name(struct bpf_map *map)
2085 return map ? map->name : NULL; 2085 return map ? map->name : NULL;
2086} 2086}
2087 2087
2088uint32_t bpf_map__btf_key_id(const struct bpf_map *map) 2088uint32_t bpf_map__btf_key_type_id(const struct bpf_map *map)
2089{ 2089{
2090 return map ? map->btf_key_id : 0; 2090 return map ? map->btf_key_type_id : 0;
2091} 2091}
2092 2092
2093uint32_t bpf_map__btf_value_id(const struct bpf_map *map) 2093uint32_t bpf_map__btf_value_type_id(const struct bpf_map *map)
2094{ 2094{
2095 return map ? map->btf_value_id : 0; 2095 return map ? map->btf_value_type_id : 0;
2096} 2096}
2097 2097
2098int bpf_map__set_priv(struct bpf_map *map, void *priv, 2098int bpf_map__set_priv(struct bpf_map *map, void *priv,
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index cd3fd8d782c7..09976531aa74 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -244,8 +244,8 @@ bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
244int bpf_map__fd(struct bpf_map *map); 244int bpf_map__fd(struct bpf_map *map);
245const struct bpf_map_def *bpf_map__def(struct bpf_map *map); 245const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
246const char *bpf_map__name(struct bpf_map *map); 246const char *bpf_map__name(struct bpf_map *map);
247uint32_t bpf_map__btf_key_id(const struct bpf_map *map); 247uint32_t bpf_map__btf_key_type_id(const struct bpf_map *map);
248uint32_t bpf_map__btf_value_id(const struct bpf_map *map); 248uint32_t bpf_map__btf_value_type_id(const struct bpf_map *map);
249 249
250typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); 250typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
251int bpf_map__set_priv(struct bpf_map *map, void *priv, 251int bpf_map__set_priv(struct bpf_map *map, void *priv,