Skip to content

Commit 671dffa

Browse files
committed
Merge branch 'bpf-bpftool-improved-prog-load'
Jakub Kicinski says: ==================== This series starts with two minor clean ups to test_offload.py selftest script. The next 11 patches extend the abilities of bpftool prog load beyond the simple cgroup use cases. Three new parameters are added: - type - allows specifying program type, independent of how code sections are named; - map - allows reusing existing maps, instead of creating a new map on every program load; - dev - offload/binding to a device. A number of changes to libbpf is required to accomplish the task. The section - program type logic mapping is exposed. We should probably aim to use the libbpf program section naming everywhere. For reuse of maps we need to allow users to set FD for bpf map object in libbpf. Examples Load program my_xdp.o and pin it as /sys/fs/bpf/my_xdp, for xdp program type: $ bpftool prog load my_xdp.o /sys/fs/bpf/my_xdp \ type xdp As above but for offload: $ bpftool prog load my_xdp.o /sys/fs/bpf/my_xdp \ type xdp \ dev netdevsim0 Load program my_maps.o, but for the first map reuse map id 17, and for the map called "other_map" reuse pinned map /sys/fs/bpf/map0: $ bpftool prog load my_maps.o /sys/fs/bpf/prog \ map idx 0 id 17 \ map name other_map pinned /sys/fs/bpf/map0 v3: - fix return codes in patch 5; - rename libbpf_prog_type_by_string() -> libbpf_prog_type_by_name(); - fold file path into xattr in patch 8; - add patch 10; - use dup3() in patch 12; - depend on fd value in patch 12; - close old fd in patch 12. v2: - add compat for reallocarray(). ==================== Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2 parents d90c936 + 3ff5a4d commit 671dffa

18 files changed

Lines changed: 906 additions & 108 deletions

File tree

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,20 @@ 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*
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* }
32+
| *TYPE* := {
33+
| **socket** | **kprobe** | **kretprobe** | **classifier** | **action** |
34+
| **tracepoint** | **raw_tracepoint** | **xdp** | **perf_event** | **cgroup/skb** |
35+
| **cgroup/sock** | **cgroup/dev** | **lwt_in** | **lwt_out** | **lwt_xmit** |
36+
| **lwt_seg6local** | **sockops** | **sk_skb** | **sk_msg** | **lirc_mode2** |
37+
| **cgroup/bind4** | **cgroup/bind6** | **cgroup/post_bind4** | **cgroup/post_bind6** |
38+
| **cgroup/connect4** | **cgroup/connect6** | **cgroup/sendmsg4** | **cgroup/sendmsg6**
39+
| }
40+
3141

3242
DESCRIPTION
3343
===========
@@ -64,8 +74,19 @@ DESCRIPTION
6474

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

67-
**bpftool prog load** *OBJ* *FILE*
77+
**bpftool prog load** *OBJ* *FILE* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*]
6878
Load bpf program from binary *OBJ* and pin as *FILE*.
79+
**type** is optional, if not specified program type will be
80+
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.
88+
If **dev** *NAME* is specified program will be loaded onto
89+
given networking device (offload).
6990

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

@@ -159,6 +180,14 @@ EXAMPLES
159180
mov %rbx,0x0(%rbp)
160181
48 89 5d 00
161182

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+
|
162191
163192
SEE ALSO
164193
========

‎tools/bpf/bpftool/Makefile‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ INSTALL ?= install
5252
RM ?= rm -f
5353

5454
FEATURE_USER = .bpftool
55-
FEATURE_TESTS = libbfd disassembler-four-args
55+
FEATURE_TESTS = libbfd disassembler-four-args reallocarray
5656
FEATURE_DISPLAY = libbfd disassembler-four-args
5757

5858
check_feat := 1
@@ -75,6 +75,10 @@ ifeq ($(feature-disassembler-four-args), 1)
7575
CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
7676
endif
7777

78+
ifeq ($(feature-reallocarray), 0)
79+
CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
80+
endif
81+
7882
include $(wildcard $(OUTPUT)*.d)
7983

8084
all: $(OUTPUT)bpftool

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

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,35 @@ _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+
125+
_sysfs_get_netdevs()
126+
{
127+
COMPREPLY+=( $( compgen -W "$( ls /sys/class/net 2>/dev/null )" -- \
128+
"$cur" ) )
129+
}
130+
102131
# For bpftool map update: retrieve type of the map to update.
103132
_bpftool_map_update_map_type()
104133
{
@@ -214,12 +243,14 @@ _bpftool()
214243
# Completion depends on object and command in use
215244
case $object in
216245
prog)
217-
case $prev in
218-
id)
219-
_bpftool_get_prog_ids
220-
return 0
221-
;;
222-
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
223254

224255
local PROG_TYPE='id pinned tag'
225256
case $command in
@@ -262,8 +293,57 @@ _bpftool()
262293
return 0
263294
;;
264295
load)
265-
_filedir
266-
return 0
296+
local obj
297+
298+
if [[ ${#words[@]} -lt 6 ]]; then
299+
_filedir
300+
return 0
301+
fi
302+
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+
322+
case $prev in
323+
type)
324+
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" -- \
325+
"$cur" ) )
326+
return 0
327+
;;
328+
id)
329+
_bpftool_get_map_ids
330+
return 0
331+
;;
332+
pinned)
333+
_filedir
334+
return 0
335+
;;
336+
dev)
337+
_sysfs_get_netdevs
338+
return 0
339+
;;
340+
*)
341+
COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
342+
_bpftool_once_attr 'type'
343+
_bpftool_once_attr 'dev'
344+
return 0
345+
;;
346+
esac
267347
;;
268348
*)
269349
[[ $prev == $object ]] && \

‎tools/bpf/bpftool/main.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <linux/compiler.h>
4343
#include <linux/kernel.h>
4444
#include <linux/hashtable.h>
45+
#include <tools/libc_compat.h>
4546

4647
#include "json_writer.h"
4748

@@ -50,6 +51,21 @@
5051
#define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
5152
#define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
5253
#define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; })
54+
#define GET_ARG() ({ argc--; *argv++; })
55+
#define REQ_ARGS(cnt) \
56+
({ \
57+
int _cnt = (cnt); \
58+
bool _res; \
59+
\
60+
if (argc < _cnt) { \
61+
p_err("'%s' needs at least %d arguments, %d found", \
62+
argv[-1], _cnt, argc); \
63+
_res = false; \
64+
} else { \
65+
_res = true; \
66+
} \
67+
_res; \
68+
})
5369

5470
#define ERR_MAX_LEN 1024
5571

@@ -59,6 +75,8 @@
5975
"PROG := { id PROG_ID | pinned FILE | tag PROG_TAG }"
6076
#define HELP_SPEC_OPTIONS \
6177
"OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} }"
78+
#define HELP_SPEC_MAP \
79+
"MAP := { id MAP_ID | pinned FILE }"
6280

6381
enum bpf_obj_type {
6482
BPF_OBJ_UNKNOWN,
@@ -120,6 +138,7 @@ int do_cgroup(int argc, char **arg);
120138
int do_perf(int argc, char **arg);
121139

122140
int prog_parse_fd(int *argc, char ***argv);
141+
int map_parse_fd(int *argc, char ***argv);
123142
int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
124143

125144
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)