Skip to content

Commit 88cda1c

Browse files
iamkafaidavem330
authored andcommitted
bpf: libbpf: Provide basic API support to specify BPF obj name
This patch extends the libbpf to provide API support to allow specifying BPF object name. In tools/lib/bpf/libbpf, the C symbol of the function and the map is used. Regarding section name, all maps are under the same section named "maps". Hence, section name is not a good choice for map's name. To be consistent with map, bpf_prog also follows and uses its function symbol as the prog's name. This patch adds logic to collect function's symbols in libbpf. There is existing codes to collect the map's symbols and no change is needed. The bpf_load_program_name() and bpf_map_create_name() are added to take the name argument. For the other bpf_map_create_xxx() variants, a name argument is directly added to them. In samples/bpf, bpf_load.c in particular, the symbol is also used as the map's name and the map symbols has already been collected in the existing code. For bpf_prog, bpf_load.c does not collect the function symbol name. We can consider to collect them later if there is a need to continue supporting the bpf_load.c. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Alexei Starovoitov <ast@fb.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ad5b177 commit 88cda1c

7 files changed

Lines changed: 157 additions & 47 deletions

File tree

‎samples/bpf/bpf_load.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,15 @@ static int load_maps(struct bpf_map_data *maps, int nr_maps,
221221
int inner_map_fd = map_fd[maps[i].def.inner_map_idx];
222222

223223
map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type,
224+
maps[i].name,
224225
maps[i].def.key_size,
225226
inner_map_fd,
226227
maps[i].def.max_entries,
227228
maps[i].def.map_flags,
228229
numa_node);
229230
} else {
230231
map_fd[i] = bpf_create_map_node(maps[i].def.type,
232+
maps[i].name,
231233
maps[i].def.key_size,
232234
maps[i].def.value_size,
233235
maps[i].def.max_entries,

‎samples/bpf/map_perf_test_user.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ static void do_test_lru(enum test_type test, int cpu)
137137

138138
inner_lru_map_fds[cpu] =
139139
bpf_create_map_node(BPF_MAP_TYPE_LRU_HASH,
140+
test_map_names[INNER_LRU_HASH_PREALLOC],
140141
sizeof(uint32_t),
141142
sizeof(long),
142143
inner_lru_hash_size, 0,

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ enum bpf_attach_type {
175175
/* Specify numa node during map creation */
176176
#define BPF_F_NUMA_NODE (1U << 2)
177177

178+
#define BPF_OBJ_NAME_LEN 16U
179+
178180
union bpf_attr {
179181
struct { /* anonymous struct used by BPF_MAP_CREATE command */
180182
__u32 map_type; /* one of enum bpf_map_type */
@@ -188,6 +190,7 @@ union bpf_attr {
188190
__u32 numa_node; /* numa node (effective only if
189191
* BPF_F_NUMA_NODE is set).
190192
*/
193+
__u8 map_name[BPF_OBJ_NAME_LEN];
191194
};
192195

193196
struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -210,6 +213,7 @@ union bpf_attr {
210213
__aligned_u64 log_buf; /* user supplied buffer */
211214
__u32 kern_version; /* checked when prog_type=kprobe */
212215
__u32 prog_flags;
216+
__u8 prog_name[BPF_OBJ_NAME_LEN];
213217
};
214218

215219
struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -812,6 +816,11 @@ struct bpf_prog_info {
812816
__u32 xlated_prog_len;
813817
__aligned_u64 jited_prog_insns;
814818
__aligned_u64 xlated_prog_insns;
819+
__u64 load_time; /* ns since boottime */
820+
__u32 created_by_uid;
821+
__u32 nr_map_ids;
822+
__aligned_u64 map_ids;
823+
__u8 name[BPF_OBJ_NAME_LEN];
815824
} __attribute__((aligned(8)));
816825

817826
struct bpf_map_info {
@@ -821,6 +830,7 @@ struct bpf_map_info {
821830
__u32 value_size;
822831
__u32 max_entries;
823832
__u32 map_flags;
833+
__u8 name[BPF_OBJ_NAME_LEN];
824834
} __attribute__((aligned(8)));
825835

826836
/* User bpf_sock_ops struct to access socket values and specify request ops

‎tools/lib/bpf/bpf.c‎

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
# endif
4747
#endif
4848

49+
#define min(x, y) ((x) < (y) ? (x) : (y))
50+
4951
static inline __u64 ptr_to_u64(const void *ptr)
5052
{
5153
return (__u64) (unsigned long) ptr;
@@ -57,10 +59,11 @@ static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
5759
return syscall(__NR_bpf, cmd, attr, size);
5860
}
5961

60-
int bpf_create_map_node(enum bpf_map_type map_type, int key_size,
61-
int value_size, int max_entries, __u32 map_flags,
62-
int node)
62+
int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
63+
int key_size, int value_size, int max_entries,
64+
__u32 map_flags, int node)
6365
{
66+
__u32 name_len = name ? strlen(name) : 0;
6467
union bpf_attr attr;
6568

6669
memset(&attr, '\0', sizeof(attr));
@@ -70,6 +73,8 @@ int bpf_create_map_node(enum bpf_map_type map_type, int key_size,
7073
attr.value_size = value_size;
7174
attr.max_entries = max_entries;
7275
attr.map_flags = map_flags;
76+
memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
77+
7378
if (node >= 0) {
7479
attr.map_flags |= BPF_F_NUMA_NODE;
7580
attr.numa_node = node;
@@ -81,14 +86,23 @@ int bpf_create_map_node(enum bpf_map_type map_type, int key_size,
8186
int bpf_create_map(enum bpf_map_type map_type, int key_size,
8287
int value_size, int max_entries, __u32 map_flags)
8388
{
84-
return bpf_create_map_node(map_type, key_size, value_size,
89+
return bpf_create_map_node(map_type, NULL, key_size, value_size,
8590
max_entries, map_flags, -1);
8691
}
8792

88-
int bpf_create_map_in_map_node(enum bpf_map_type map_type, int key_size,
89-
int inner_map_fd, int max_entries,
93+
int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
94+
int key_size, int value_size, int max_entries,
95+
__u32 map_flags)
96+
{
97+
return bpf_create_map_node(map_type, name, key_size, value_size,
98+
max_entries, map_flags, -1);
99+
}
100+
101+
int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
102+
int key_size, int inner_map_fd, int max_entries,
90103
__u32 map_flags, int node)
91104
{
105+
__u32 name_len = name ? strlen(name) : 0;
92106
union bpf_attr attr;
93107

94108
memset(&attr, '\0', sizeof(attr));
@@ -99,6 +113,8 @@ int bpf_create_map_in_map_node(enum bpf_map_type map_type, int key_size,
99113
attr.inner_map_fd = inner_map_fd;
100114
attr.max_entries = max_entries;
101115
attr.map_flags = map_flags;
116+
memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
117+
102118
if (node >= 0) {
103119
attr.map_flags |= BPF_F_NUMA_NODE;
104120
attr.numa_node = node;
@@ -107,19 +123,24 @@ int bpf_create_map_in_map_node(enum bpf_map_type map_type, int key_size,
107123
return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
108124
}
109125

110-
int bpf_create_map_in_map(enum bpf_map_type map_type, int key_size,
111-
int inner_map_fd, int max_entries, __u32 map_flags)
126+
int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
127+
int key_size, int inner_map_fd, int max_entries,
128+
__u32 map_flags)
112129
{
113-
return bpf_create_map_in_map_node(map_type, key_size, inner_map_fd,
114-
max_entries, map_flags, -1);
130+
return bpf_create_map_in_map_node(map_type, name, key_size,
131+
inner_map_fd, max_entries, map_flags,
132+
-1);
115133
}
116134

117-
int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
118-
size_t insns_cnt, const char *license,
119-
__u32 kern_version, char *log_buf, size_t log_buf_sz)
135+
int bpf_load_program_name(enum bpf_prog_type type, const char *name,
136+
const struct bpf_insn *insns,
137+
size_t insns_cnt, const char *license,
138+
__u32 kern_version, char *log_buf,
139+
size_t log_buf_sz)
120140
{
121141
int fd;
122142
union bpf_attr attr;
143+
__u32 name_len = name ? strlen(name) : 0;
123144

124145
bzero(&attr, sizeof(attr));
125146
attr.prog_type = type;
@@ -130,6 +151,7 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
130151
attr.log_size = 0;
131152
attr.log_level = 0;
132153
attr.kern_version = kern_version;
154+
memcpy(attr.prog_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
133155

134156
fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
135157
if (fd >= 0 || !log_buf || !log_buf_sz)
@@ -143,6 +165,15 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
143165
return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
144166
}
145167

168+
int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
169+
size_t insns_cnt, const char *license,
170+
__u32 kern_version, char *log_buf,
171+
size_t log_buf_sz)
172+
{
173+
return bpf_load_program_name(type, NULL, insns, insns_cnt, license,
174+
kern_version, log_buf, log_buf_sz);
175+
}
176+
146177
int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
147178
size_t insns_cnt, int strict_alignment,
148179
const char *license, __u32 kern_version,

‎tools/lib/bpf/bpf.h‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,28 @@
2424
#include <linux/bpf.h>
2525
#include <stddef.h>
2626

27-
int bpf_create_map_node(enum bpf_map_type map_type, int key_size,
28-
int value_size, int max_entries, __u32 map_flags,
29-
int node);
27+
int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
28+
int key_size, int value_size, int max_entries,
29+
__u32 map_flags, int node);
30+
int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
31+
int key_size, int value_size, int max_entries,
32+
__u32 map_flags);
3033
int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
3134
int max_entries, __u32 map_flags);
32-
int bpf_create_map_in_map_node(enum bpf_map_type map_type, int key_size,
33-
int inner_map_fd, int max_entries,
35+
int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
36+
int key_size, int inner_map_fd, int max_entries,
3437
__u32 map_flags, int node);
35-
int bpf_create_map_in_map(enum bpf_map_type map_type, int key_size,
36-
int inner_map_fd, int max_entries, __u32 map_flags);
38+
int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
39+
int key_size, int inner_map_fd, int max_entries,
40+
__u32 map_flags);
3741

3842
/* Recommend log buffer size */
3943
#define BPF_LOG_BUF_SIZE 65536
44+
int bpf_load_program_name(enum bpf_prog_type type, const char *name,
45+
const struct bpf_insn *insns,
46+
size_t insns_cnt, const char *license,
47+
__u32 kern_version, char *log_buf,
48+
size_t log_buf_sz);
4049
int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
4150
size_t insns_cnt, const char *license,
4251
__u32 kern_version, char *log_buf,

0 commit comments

Comments
 (0)