Skip to content

Commit

Permalink
tools: bpftool: allow reuse of maps with bpftool prog load
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jakub Kicinski authored and Daniel Borkmann committed Jul 11, 2018
1 parent 26736eb commit 3ff5a4d
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 23 deletions.
20 changes: 18 additions & 2 deletions tools/bpf/bpftool/Documentation/bpftool-prog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ MAP COMMANDS
| **bpftool** **prog dump xlated** *PROG* [{**file** *FILE* | **opcodes** | **visual**}]
| **bpftool** **prog dump jited** *PROG* [{**file** *FILE* | **opcodes**}]
| **bpftool** **prog pin** *PROG* *FILE*
| **bpftool** **prog load** *OBJ* *FILE* [**type** *TYPE*] [**dev** *NAME*]
| **bpftool** **prog load** *OBJ* *FILE* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*]
| **bpftool** **prog help**
|
| *MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
| *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* }
| *TYPE* := {
| **socket** | **kprobe** | **kretprobe** | **classifier** | **action** |
Expand Down Expand Up @@ -73,10 +74,17 @@ DESCRIPTION

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

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

Expand Down Expand Up @@ -172,6 +180,14 @@ EXAMPLES
mov %rbx,0x0(%rbp)
48 89 5d 00

|
| **# bpftool prog load xdp1_kern.o /sys/fs/bpf/xdp1 type xdp map name rxcnt id 7**
| **# bpftool prog show pinned /sys/fs/bpf/xdp1**
| 9: xdp name xdp_prog1 tag 539ec6ce11b52f98 gpl
| loaded_at 2018-06-25T16:17:31-0700 uid 0
| xlated 488B jited 336B memlock 4096B map_ids 7
| **# rm /sys/fs/bpf/xdp1**
|
SEE ALSO
========
Expand Down
67 changes: 61 additions & 6 deletions tools/bpf/bpftool/bash-completion/bpftool
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ _bpftool_get_prog_tags()
command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
}

_bpftool_get_obj_map_names()
{
local obj

obj=$1

maps=$(objdump -j maps -t $obj 2>/dev/null | \
command awk '/g . maps/ {print $NF}')

COMPREPLY+=( $( compgen -W "$maps" -- "$cur" ) )
}

_bpftool_get_obj_map_idxs()
{
local obj

obj=$1

nmaps=$(objdump -j maps -t $obj 2>/dev/null | grep -c 'g . maps')

COMPREPLY+=( $( compgen -W "$(seq 0 $((nmaps - 1)))" -- "$cur" ) )
}

_sysfs_get_netdevs()
{
COMPREPLY+=( $( compgen -W "$( ls /sys/class/net 2>/dev/null )" -- \
Expand Down Expand Up @@ -220,12 +243,14 @@ _bpftool()
# Completion depends on object and command in use
case $object in
prog)
case $prev in
id)
_bpftool_get_prog_ids
return 0
;;
esac
if [[ $command != "load" ]]; then
case $prev in
id)
_bpftool_get_prog_ids
return 0
;;
esac
fi

local PROG_TYPE='id pinned tag'
case $command in
Expand Down Expand Up @@ -268,22 +293,52 @@ _bpftool()
return 0
;;
load)
local obj

if [[ ${#words[@]} -lt 6 ]]; then
_filedir
return 0
fi

obj=${words[3]}

if [[ ${words[-4]} == "map" ]]; then
COMPREPLY=( $( compgen -W "id pinned" -- "$cur" ) )
return 0
fi
if [[ ${words[-3]} == "map" ]]; then
if [[ ${words[-2]} == "idx" ]]; then
_bpftool_get_obj_map_idxs $obj
elif [[ ${words[-2]} == "name" ]]; then
_bpftool_get_obj_map_names $obj
fi
return 0
fi
if [[ ${words[-2]} == "map" ]]; then
COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )
return 0
fi

case $prev in
type)
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" -- \
"$cur" ) )
return 0
;;
id)
_bpftool_get_map_ids
return 0
;;
pinned)
_filedir
return 0
;;
dev)
_sysfs_get_netdevs
return 0
;;
*)
COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
_bpftool_once_attr 'type'
_bpftool_once_attr 'dev'
return 0
Expand Down
3 changes: 3 additions & 0 deletions tools/bpf/bpftool/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
"PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
#define HELP_SPEC_OPTIONS \
"OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} }"
#define HELP_SPEC_MAP \
"MAP := { id MAP_ID | pinned FILE }"

enum bpf_obj_type {
BPF_OBJ_UNKNOWN,
Expand Down Expand Up @@ -136,6 +138,7 @@ int do_cgroup(int argc, char **arg);
int do_perf(int argc, char **arg);

int prog_parse_fd(int *argc, char ***argv);
int map_parse_fd(int *argc, char ***argv);
int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);

void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
Expand Down
4 changes: 2 additions & 2 deletions tools/bpf/bpftool/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static void *alloc_value(struct bpf_map_info *info)
return malloc(info->value_size);
}

static int map_parse_fd(int *argc, char ***argv)
int map_parse_fd(int *argc, char ***argv)
{
int fd;

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

0 comments on commit 3ff5a4d

Please sign in to comment.