Skip to content

Commit 3ff5a4d

Browse files
Jakub Kicinskiborkmann
authored andcommitted
tools: bpftool: allow reuse of maps with bpftool prog load
Add map parameter to prog load which will allow reuse of existing maps instead of creating new ones. We need feature detection and compat code for reallocarray, since it's not available in many libc versions. Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com> Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 26736eb commit 3ff5a4d

5 files changed

Lines changed: 219 additions & 23 deletions

File tree

‎tools/bpf/bpftool/Documentation/bpftool-prog.rst‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ MAP COMMANDS
2424
| **bpftool** **prog dump xlated** *PROG* [{**file** *FILE* | **opcodes** | **visual**}]
2525
| **bpftool** **prog dump jited** *PROG* [{**file** *FILE* | **opcodes**}]
2626
| **bpftool** **prog pin** *PROG* *FILE*
27-
| **bpftool** **prog load** *OBJ* *FILE* [**type** *TYPE*] [**dev** *NAME*]
27+
| **bpftool** **prog load** *OBJ* *FILE* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*]
2828
| **bpftool** **prog help**
2929
|
30+
| *MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
3031
| *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* }
3132
| *TYPE* := {
3233
| **socket** | **kprobe** | **kretprobe** | **classifier** | **action** |
@@ -73,10 +74,17 @@ DESCRIPTION
7374

7475
Note: *FILE* must be located in *bpffs* mount.
7576

76-
**bpftool prog load** *OBJ* *FILE* [**type** *TYPE*] [**dev** *NAME*]
77+
**bpftool prog load** *OBJ* *FILE* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*]
7778
Load bpf program from binary *OBJ* and pin as *FILE*.
7879
**type** is optional, if not specified program type will be
7980
inferred from section names.
81+
By default bpftool will create new maps as declared in the ELF
82+
object being loaded. **map** parameter allows for the reuse
83+
of existing maps. It can be specified multiple times, each
84+
time for a different map. *IDX* refers to index of the map
85+
to be replaced in the ELF file counting from 0, while *NAME*
86+
allows to replace a map by name. *MAP* specifies the map to
87+
use, referring to it by **id** or through a **pinned** file.
8088
If **dev** *NAME* is specified program will be loaded onto
8189
given networking device (offload).
8290

@@ -172,6 +180,14 @@ EXAMPLES
172180
mov %rbx,0x0(%rbp)
173181
48 89 5d 00
174182

183+
|
184+
| **# bpftool prog load xdp1_kern.o /sys/fs/bpf/xdp1 type xdp map name rxcnt id 7**
185+
| **# bpftool prog show pinned /sys/fs/bpf/xdp1**
186+
| 9: xdp name xdp_prog1 tag 539ec6ce11b52f98 gpl
187+
| loaded_at 2018-06-25T16:17:31-0700 uid 0
188+
| xlated 488B jited 336B memlock 4096B map_ids 7
189+
| **# rm /sys/fs/bpf/xdp1**
190+
|
175191
176192
SEE ALSO
177193
========

‎tools/bpf/bpftool/bash-completion/bpftool‎

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,29 @@ _bpftool_get_prog_tags()
9999
command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
100100
}
101101

102+
_bpftool_get_obj_map_names()
103+
{
104+
local obj
105+
106+
obj=$1
107+
108+
maps=$(objdump -j maps -t $obj 2>/dev/null | \
109+
command awk '/g . maps/ {print $NF}')
110+
111+
COMPREPLY+=( $( compgen -W "$maps" -- "$cur" ) )
112+
}
113+
114+
_bpftool_get_obj_map_idxs()
115+
{
116+
local obj
117+
118+
obj=$1
119+
120+
nmaps=$(objdump -j maps -t $obj 2>/dev/null | grep -c 'g . maps')
121+
122+
COMPREPLY+=( $( compgen -W "$(seq 0 $((nmaps - 1)))" -- "$cur" ) )
123+
}
124+
102125
_sysfs_get_netdevs()
103126
{
104127
COMPREPLY+=( $( compgen -W "$( ls /sys/class/net 2>/dev/null )" -- \
@@ -220,12 +243,14 @@ _bpftool()
220243
# Completion depends on object and command in use
221244
case $object in
222245
prog)
223-
case $prev in
224-
id)
225-
_bpftool_get_prog_ids
226-
return 0
227-
;;
228-
esac
246+
if [[ $command != "load" ]]; then
247+
case $prev in
248+
id)
249+
_bpftool_get_prog_ids
250+
return 0
251+
;;
252+
esac
253+
fi
229254

230255
local PROG_TYPE='id pinned tag'
231256
case $command in
@@ -268,22 +293,52 @@ _bpftool()
268293
return 0
269294
;;
270295
load)
296+
local obj
297+
271298
if [[ ${#words[@]} -lt 6 ]]; then
272299
_filedir
273300
return 0
274301
fi
275302

303+
obj=${words[3]}
304+
305+
if [[ ${words[-4]} == "map" ]]; then
306+
COMPREPLY=( $( compgen -W "id pinned" -- "$cur" ) )
307+
return 0
308+
fi
309+
if [[ ${words[-3]} == "map" ]]; then
310+
if [[ ${words[-2]} == "idx" ]]; then
311+
_bpftool_get_obj_map_idxs $obj
312+
elif [[ ${words[-2]} == "name" ]]; then
313+
_bpftool_get_obj_map_names $obj
314+
fi
315+
return 0
316+
fi
317+
if [[ ${words[-2]} == "map" ]]; then
318+
COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )
319+
return 0
320+
fi
321+
276322
case $prev in
277323
type)
278324
COMPREPLY=( $( compgen -W "socket kprobe kretprobe classifier action tracepoint raw_tracepoint xdp perf_event cgroup/skb cgroup/sock cgroup/dev lwt_in lwt_out lwt_xmit lwt_seg6local sockops sk_skb sk_msg lirc_mode2 cgroup/bind4 cgroup/bind6 cgroup/connect4 cgroup/connect6 cgroup/sendmsg4 cgroup/sendmsg6 cgroup/post_bind4 cgroup/post_bind6" -- \
279325
"$cur" ) )
280326
return 0
281327
;;
328+
id)
329+
_bpftool_get_map_ids
330+
return 0
331+
;;
332+
pinned)
333+
_filedir
334+
return 0
335+
;;
282336
dev)
283337
_sysfs_get_netdevs
284338
return 0
285339
;;
286340
*)
341+
COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
287342
_bpftool_once_attr 'type'
288343
_bpftool_once_attr 'dev'
289344
return 0

‎tools/bpf/bpftool/main.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
"PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
7676
#define HELP_SPEC_OPTIONS \
7777
"OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} }"
78+
#define HELP_SPEC_MAP \
79+
"MAP := { id MAP_ID | pinned FILE }"
7880

7981
enum bpf_obj_type {
8082
BPF_OBJ_UNKNOWN,
@@ -136,6 +138,7 @@ int do_cgroup(int argc, char **arg);
136138
int do_perf(int argc, char **arg);
137139

138140
int prog_parse_fd(int *argc, char ***argv);
141+
int map_parse_fd(int *argc, char ***argv);
139142
int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
140143

141144
void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,

‎tools/bpf/bpftool/map.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void *alloc_value(struct bpf_map_info *info)
9393
return malloc(info->value_size);
9494
}
9595

96-
static int map_parse_fd(int *argc, char ***argv)
96+
int map_parse_fd(int *argc, char ***argv)
9797
{
9898
int fd;
9999

@@ -824,7 +824,7 @@ static int do_help(int argc, char **argv)
824824
" %s %s event_pipe MAP [cpu N index M]\n"
825825
" %s %s help\n"
826826
"\n"
827-
" MAP := { id MAP_ID | pinned FILE }\n"
827+
" " HELP_SPEC_MAP "\n"
828828
" DATA := { [hex] BYTES }\n"
829829
" " HELP_SPEC_PROGRAM "\n"
830830
" VALUE := { DATA | MAP | PROG }\n"

0 commit comments

Comments
 (0)