diff options
| author | Jesper Dangaard Brouer <brouer@redhat.com> | 2017-05-02 08:32:01 -0400 |
|---|---|---|
| committer | David S. Miller <davem@davemloft.net> | 2017-05-03 09:30:24 -0400 |
| commit | 6979bcc731f9680824a85a9efc43f36d01cec1b2 (patch) | |
| tree | 930220b0624efcd0c0b94e4c3dbaabf85202fc72 /samples | |
| parent | 156450d9d964447adfb44a231c634d2f5609d110 (diff) | |
samples/bpf: load_bpf.c make callback fixup more flexible
Do this change before others start to use this callback.
Change map_perf_test_user.c which seems to be the only user.
This patch extends capabilities of commit 9fd63d05f3e8 ("bpf:
Allow bpf sample programs (*_user.c) to change bpf_map_def").
Give fixup callback access to struct bpf_map_data, instead of
only stuct bpf_map_def. This add flexibility to allow userspace
to reassign the map file descriptor. This is very useful when
wanting to share maps between several bpf programs.
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples')
| -rw-r--r-- | samples/bpf/bpf_load.c | 17 | ||||
| -rw-r--r-- | samples/bpf/bpf_load.h | 10 | ||||
| -rw-r--r-- | samples/bpf/map_perf_test_user.c | 14 |
3 files changed, 23 insertions, 18 deletions
diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c index fedec29c7817..74456b3eb89a 100644 --- a/samples/bpf/bpf_load.c +++ b/samples/bpf/bpf_load.c | |||
| @@ -39,13 +39,6 @@ int event_fd[MAX_PROGS]; | |||
| 39 | int prog_cnt; | 39 | int prog_cnt; |
| 40 | int prog_array_fd = -1; | 40 | int prog_array_fd = -1; |
| 41 | 41 | ||
| 42 | /* Keeping relevant info on maps */ | ||
| 43 | struct bpf_map_data { | ||
| 44 | int fd; | ||
| 45 | char *name; | ||
| 46 | size_t elf_offset; | ||
| 47 | struct bpf_map_def def; | ||
| 48 | }; | ||
| 49 | struct bpf_map_data map_data[MAX_MAPS]; | 42 | struct bpf_map_data map_data[MAX_MAPS]; |
| 50 | int map_data_count = 0; | 43 | int map_data_count = 0; |
| 51 | 44 | ||
| @@ -202,8 +195,14 @@ static int load_maps(struct bpf_map_data *maps, int nr_maps, | |||
| 202 | int i; | 195 | int i; |
| 203 | 196 | ||
| 204 | for (i = 0; i < nr_maps; i++) { | 197 | for (i = 0; i < nr_maps; i++) { |
| 205 | if (fixup_map) | 198 | if (fixup_map) { |
| 206 | fixup_map(&maps[i].def, maps[i].name, i); | 199 | fixup_map(&maps[i], i); |
| 200 | /* Allow userspace to assign map FD prior to creation */ | ||
| 201 | if (maps[i].fd != -1) { | ||
| 202 | map_fd[i] = maps[i].fd; | ||
| 203 | continue; | ||
| 204 | } | ||
| 205 | } | ||
| 207 | 206 | ||
| 208 | if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS || | 207 | if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS || |
| 209 | maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) { | 208 | maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) { |
diff --git a/samples/bpf/bpf_load.h b/samples/bpf/bpf_load.h index 05822f83173a..4d4fd4678a64 100644 --- a/samples/bpf/bpf_load.h +++ b/samples/bpf/bpf_load.h | |||
| @@ -15,8 +15,14 @@ struct bpf_map_def { | |||
| 15 | unsigned int inner_map_idx; | 15 | unsigned int inner_map_idx; |
| 16 | }; | 16 | }; |
| 17 | 17 | ||
| 18 | typedef void (*fixup_map_cb)(struct bpf_map_def *map, const char *map_name, | 18 | struct bpf_map_data { |
| 19 | int idx); | 19 | int fd; |
| 20 | char *name; | ||
| 21 | size_t elf_offset; | ||
| 22 | struct bpf_map_def def; | ||
| 23 | }; | ||
| 24 | |||
| 25 | typedef void (*fixup_map_cb)(struct bpf_map_data *map, int idx); | ||
| 20 | 26 | ||
| 21 | extern int map_fd[MAX_MAPS]; | 27 | extern int map_fd[MAX_MAPS]; |
| 22 | extern int prog_fd[MAX_PROGS]; | 28 | extern int prog_fd[MAX_PROGS]; |
diff --git a/samples/bpf/map_perf_test_user.c b/samples/bpf/map_perf_test_user.c index 6ac778153315..1a8894b5ac51 100644 --- a/samples/bpf/map_perf_test_user.c +++ b/samples/bpf/map_perf_test_user.c | |||
| @@ -320,21 +320,21 @@ static void fill_lpm_trie(void) | |||
| 320 | assert(!r); | 320 | assert(!r); |
| 321 | } | 321 | } |
| 322 | 322 | ||
| 323 | static void fixup_map(struct bpf_map_def *map, const char *name, int idx) | 323 | static void fixup_map(struct bpf_map_data *map, int idx) |
| 324 | { | 324 | { |
| 325 | int i; | 325 | int i; |
| 326 | 326 | ||
| 327 | if (!strcmp("inner_lru_hash_map", name)) { | 327 | if (!strcmp("inner_lru_hash_map", map->name)) { |
| 328 | inner_lru_hash_idx = idx; | 328 | inner_lru_hash_idx = idx; |
| 329 | inner_lru_hash_size = map->max_entries; | 329 | inner_lru_hash_size = map->def.max_entries; |
| 330 | } | 330 | } |
| 331 | 331 | ||
| 332 | if (!strcmp("array_of_lru_hashs", name)) { | 332 | if (!strcmp("array_of_lru_hashs", map->name)) { |
| 333 | if (inner_lru_hash_idx == -1) { | 333 | if (inner_lru_hash_idx == -1) { |
| 334 | printf("inner_lru_hash_map must be defined before array_of_lru_hashs\n"); | 334 | printf("inner_lru_hash_map must be defined before array_of_lru_hashs\n"); |
| 335 | exit(1); | 335 | exit(1); |
| 336 | } | 336 | } |
| 337 | map->inner_map_idx = inner_lru_hash_idx; | 337 | map->def.inner_map_idx = inner_lru_hash_idx; |
| 338 | array_of_lru_hashs_idx = idx; | 338 | array_of_lru_hashs_idx = idx; |
| 339 | } | 339 | } |
| 340 | 340 | ||
| @@ -345,9 +345,9 @@ static void fixup_map(struct bpf_map_def *map, const char *name, int idx) | |||
| 345 | 345 | ||
| 346 | /* Only change the max_entries for the enabled test(s) */ | 346 | /* Only change the max_entries for the enabled test(s) */ |
| 347 | for (i = 0; i < NR_TESTS; i++) { | 347 | for (i = 0; i < NR_TESTS; i++) { |
| 348 | if (!strcmp(test_map_names[i], name) && | 348 | if (!strcmp(test_map_names[i], map->name) && |
| 349 | (check_test_flags(i))) { | 349 | (check_test_flags(i))) { |
| 350 | map->max_entries = num_map_entries; | 350 | map->def.max_entries = num_map_entries; |
| 351 | } | 351 | } |
| 352 | } | 352 | } |
| 353 | } | 353 | } |
