Skip to content

Commit 9fd63d0

Browse files
iamkafaidavem330
authored andcommitted
bpf: Allow bpf sample programs (*_user.c) to change bpf_map_def
The current bpf_map_def is statically defined during compile time. This patch allows the *_user.c program to change it during runtime. It is done by adding load_bpf_file_fixup_map() which takes a callback. The callback will be called before creating each map so that it has a chance to modify the bpf_map_def. The current usecase is to change max_entries in map_perf_test. It is interesting to test with a much bigger map size in some cases (e.g. the following patch on bpf_lru_map.c). However, it is hard to find one size to fit all testing environment. Hence, it is handy to take the max_entries as a cmdline arg and then configure the bpf_map_def during runtime. This patch adds two cmdline args. One is to configure the map's max_entries. Another is to configure the max_cnt which controls how many times a syscall is called. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent bf8db5d commit 9fd63d0

3 files changed

Lines changed: 201 additions & 74 deletions

File tree

‎samples/bpf/bpf_load.c‎

Lines changed: 96 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <sys/mman.h>
2222
#include <poll.h>
2323
#include <ctype.h>
24+
#include <assert.h>
2425
#include "libbpf.h"
2526
#include "bpf_load.h"
2627
#include "perf-sys.h"
@@ -37,15 +38,6 @@ int event_fd[MAX_PROGS];
3738
int prog_cnt;
3839
int prog_array_fd = -1;
3940

40-
struct bpf_map_def {
41-
unsigned int type;
42-
unsigned int key_size;
43-
unsigned int value_size;
44-
unsigned int max_entries;
45-
unsigned int map_flags;
46-
unsigned int inner_map_idx;
47-
};
48-
4941
static int populate_prog_array(const char *event, int prog_fd)
5042
{
5143
int ind = atoi(event), err;
@@ -193,11 +185,14 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
193185
return 0;
194186
}
195187

196-
static int load_maps(struct bpf_map_def *maps, int len)
188+
static int load_maps(struct bpf_map_def *maps, int len,
189+
const char **map_names, fixup_map_cb fixup_map)
197190
{
198191
int i;
199192

200193
for (i = 0; i < len / sizeof(struct bpf_map_def); i++) {
194+
if (fixup_map)
195+
fixup_map(&maps[i], map_names[i], i);
201196

202197
if (maps[i].type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
203198
maps[i].type == BPF_MAP_TYPE_HASH_OF_MAPS) {
@@ -280,14 +275,64 @@ static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
280275
return 0;
281276
}
282277

283-
int load_bpf_file(char *path)
278+
static int cmp_symbols(const void *l, const void *r)
279+
{
280+
const GElf_Sym *lsym = (const GElf_Sym *)l;
281+
const GElf_Sym *rsym = (const GElf_Sym *)r;
282+
283+
if (lsym->st_value < rsym->st_value)
284+
return -1;
285+
else if (lsym->st_value > rsym->st_value)
286+
return 1;
287+
else
288+
return 0;
289+
}
290+
291+
static int get_sorted_map_names(Elf *elf, Elf_Data *symbols, int maps_shndx,
292+
int strtabidx, char **map_names)
293+
{
294+
GElf_Sym map_symbols[MAX_MAPS];
295+
int i, nr_maps = 0;
296+
297+
for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
298+
assert(nr_maps < MAX_MAPS);
299+
if (!gelf_getsym(symbols, i, &map_symbols[nr_maps]))
300+
continue;
301+
if (map_symbols[nr_maps].st_shndx != maps_shndx)
302+
continue;
303+
nr_maps++;
304+
}
305+
306+
qsort(map_symbols, nr_maps, sizeof(GElf_Sym), cmp_symbols);
307+
308+
for (i = 0; i < nr_maps; i++) {
309+
char *map_name;
310+
311+
map_name = elf_strptr(elf, strtabidx, map_symbols[i].st_name);
312+
if (!map_name) {
313+
printf("cannot get map symbol\n");
314+
return 1;
315+
}
316+
317+
map_names[i] = strdup(map_name);
318+
if (!map_names[i]) {
319+
printf("strdup(%s): %s(%d)\n", map_name,
320+
strerror(errno), errno);
321+
return 1;
322+
}
323+
}
324+
325+
return 0;
326+
}
327+
328+
static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
284329
{
285-
int fd, i;
330+
int fd, i, ret, maps_shndx = -1, strtabidx = -1;
286331
Elf *elf;
287332
GElf_Ehdr ehdr;
288333
GElf_Shdr shdr, shdr_prog;
289-
Elf_Data *data, *data_prog, *symbols = NULL;
290-
char *shname, *shname_prog;
334+
Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
335+
char *shname, *shname_prog, *map_names[MAX_MAPS] = { NULL };
291336

292337
/* reset global variables */
293338
kern_version = 0;
@@ -335,14 +380,33 @@ int load_bpf_file(char *path)
335380
}
336381
memcpy(&kern_version, data->d_buf, sizeof(int));
337382
} else if (strcmp(shname, "maps") == 0) {
338-
processed_sec[i] = true;
339-
if (load_maps(data->d_buf, data->d_size))
340-
return 1;
383+
maps_shndx = i;
384+
data_maps = data;
341385
} else if (shdr.sh_type == SHT_SYMTAB) {
386+
strtabidx = shdr.sh_link;
342387
symbols = data;
343388
}
344389
}
345390

391+
ret = 1;
392+
393+
if (!symbols) {
394+
printf("missing SHT_SYMTAB section\n");
395+
goto done;
396+
}
397+
398+
if (data_maps) {
399+
if (get_sorted_map_names(elf, symbols, maps_shndx, strtabidx,
400+
map_names))
401+
goto done;
402+
403+
if (load_maps(data_maps->d_buf, data_maps->d_size,
404+
(const char **)map_names, fixup_map))
405+
goto done;
406+
407+
processed_sec[maps_shndx] = true;
408+
}
409+
346410
/* load programs that need map fixup (relocations) */
347411
for (i = 1; i < ehdr.e_shnum; i++) {
348412
if (processed_sec[i])
@@ -399,8 +463,22 @@ int load_bpf_file(char *path)
399463
load_and_attach(shname, data->d_buf, data->d_size);
400464
}
401465

466+
ret = 0;
467+
done:
468+
for (i = 0; i < MAX_MAPS; i++)
469+
free(map_names[i]);
402470
close(fd);
403-
return 0;
471+
return ret;
472+
}
473+
474+
int load_bpf_file(char *path)
475+
{
476+
return do_load_bpf_file(path, NULL);
477+
}
478+
479+
int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
480+
{
481+
return do_load_bpf_file(path, fixup_map);
404482
}
405483

406484
void read_trace_pipe(void)

‎samples/bpf/bpf_load.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
#define MAX_MAPS 32
77
#define MAX_PROGS 32
88

9+
struct bpf_map_def {
10+
unsigned int type;
11+
unsigned int key_size;
12+
unsigned int value_size;
13+
unsigned int max_entries;
14+
unsigned int map_flags;
15+
unsigned int inner_map_idx;
16+
};
17+
18+
typedef void (*fixup_map_cb)(struct bpf_map_def *map, const char *map_name,
19+
int idx);
20+
921
extern int map_fd[MAX_MAPS];
1022
extern int prog_fd[MAX_PROGS];
1123
extern int event_fd[MAX_PROGS];
@@ -25,6 +37,7 @@ extern int prog_cnt;
2537
* returns zero on success
2638
*/
2739
int load_bpf_file(char *path);
40+
int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map);
2841

2942
void read_trace_pipe(void);
3043
struct ksym {

0 commit comments

Comments
 (0)