Skip to content

Commit 034565d

Browse files
committed
Merge branch 'bpf-bpftool-cleanups'
Quentin Monnet says: ==================== This series contains several minor fixes for bpftool source and documentation. The first patches focus on documentation: addition of an option in the page for "bpftool prog", clean up and update of the same page, and addition of an example of prog array map manipulation in "bpftool map" page. The last two fix warnings susceptible to appear when libbfd is not present (patch 4), or with additional warning flags passed to the compiler (last patch). ==================== Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2 parents bab89ad + c101189 commit 034565d

File tree

7 files changed

+124
-55
lines changed

7 files changed

+124
-55
lines changed

tools/bpf/bpftool/Documentation/bpftool-map.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,61 @@ The following three commands are equivalent:
170170
| **# bpftool map pin id 10 /sys/fs/bpf/map**
171171
| **# bpftool map del pinned /sys/fs/bpf/map key 13 00 07 00**
172172
173+
Note that map update can also be used in order to change the program references
174+
hold by a program array map. This can be used, for example, to change the
175+
programs used for tail-call jumps at runtime, without having to reload the
176+
entry-point program. Below is an example for this use case: we load a program
177+
defining a prog array map, and with a main function that contains a tail call
178+
to other programs that can be used either to "process" packets or to "debug"
179+
processing. Note that the prog array map MUST be pinned into the BPF virtual
180+
file system for the map update to work successfully, as kernel flushes prog
181+
array maps when they have no more references from user space (and the update
182+
would be lost as soon as bpftool exits).
183+
184+
|
185+
| **# bpftool prog loadall tail_calls.o /sys/fs/bpf/foo type xdp**
186+
| **# bpftool prog --bpffs**
187+
188+
::
189+
190+
545: xdp name main_func tag 674b4b5597193dc3 gpl
191+
loaded_at 2018-12-12T15:02:58+0000 uid 0
192+
xlated 240B jited 257B memlock 4096B map_ids 294
193+
pinned /sys/fs/bpf/foo/xdp
194+
546: xdp name bpf_func_process tag e369a529024751fc gpl
195+
loaded_at 2018-12-12T15:02:58+0000 uid 0
196+
xlated 200B jited 164B memlock 4096B
197+
pinned /sys/fs/bpf/foo/process
198+
547: xdp name bpf_func_debug tag 0b597868bc7f0976 gpl
199+
loaded_at 2018-12-12T15:02:58+0000 uid 0
200+
xlated 200B jited 164B memlock 4096B
201+
pinned /sys/fs/bpf/foo/debug
202+
203+
**# bpftool map**
204+
205+
::
206+
207+
294: prog_array name jmp_table flags 0x0
208+
key 4B value 4B max_entries 1 memlock 4096B
209+
owner_prog_type xdp owner jited
210+
211+
|
212+
| **# bpftool map pin id 294 /sys/fs/bpf/bar**
213+
| **# bpftool map dump pinned /sys/fs/bpf/bar**
214+
215+
::
216+
217+
Found 0 elements
218+
219+
|
220+
| **# bpftool map update pinned /sys/fs/bpf/bar key 0 0 0 0 value pinned /sys/fs/bpf/foo/debug**
221+
| **# bpftool map dump pinned /sys/fs/bpf/bar**
222+
223+
::
224+
225+
key: 00 00 00 00 value: 22 02 00 00
226+
Found 1 element
227+
173228
SEE ALSO
174229
========
175230
**bpf**\ (2),

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

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -158,83 +158,94 @@ OPTIONS
158158
When showing BPF programs, show file names of pinned
159159
programs.
160160

161+
-m, --mapcompat
162+
Allow loading maps with unknown map definitions.
163+
161164
EXAMPLES
162165
========
163166
**# bpftool prog show**
167+
164168
::
165169

166-
10: xdp name some_prog tag 005a3d2123620c8b gpl
167-
loaded_at Sep 29/20:11 uid 0
168-
xlated 528B jited 370B memlock 4096B map_ids 10
170+
10: xdp name some_prog tag 005a3d2123620c8b gpl
171+
loaded_at 2017-09-29T20:11:00+0000 uid 0
172+
xlated 528B jited 370B memlock 4096B map_ids 10
169173

170174
**# bpftool --json --pretty prog show**
171175

172176
::
173177

174-
{
175-
"programs": [{
176-
"id": 10,
177-
"type": "xdp",
178-
"tag": "005a3d2123620c8b",
179-
"gpl_compatible": true,
180-
"loaded_at": "Sep 29/20:11",
181-
"uid": 0,
182-
"bytes_xlated": 528,
183-
"jited": true,
184-
"bytes_jited": 370,
185-
"bytes_memlock": 4096,
186-
"map_ids": [10
187-
]
188-
}
189-
]
190-
}
178+
[{
179+
"id": 10,
180+
"type": "xdp",
181+
"tag": "005a3d2123620c8b",
182+
"gpl_compatible": true,
183+
"loaded_at": 1506715860,
184+
"uid": 0,
185+
"bytes_xlated": 528,
186+
"jited": true,
187+
"bytes_jited": 370,
188+
"bytes_memlock": 4096,
189+
"map_ids": [10
190+
]
191+
}
192+
]
191193

192194
|
193195
| **# bpftool prog dump xlated id 10 file /tmp/t**
194196
| **# ls -l /tmp/t**
195-
| -rw------- 1 root root 560 Jul 22 01:42 /tmp/t
196197
197-
**# bpftool prog dum jited tag 005a3d2123620c8b**
198+
::
199+
200+
-rw------- 1 root root 560 Jul 22 01:42 /tmp/t
201+
202+
**# bpftool prog dump jited tag 005a3d2123620c8b**
198203

199204
::
200205

201-
push %rbp
202-
mov %rsp,%rbp
203-
sub $0x228,%rsp
204-
sub $0x28,%rbp
205-
mov %rbx,0x0(%rbp)
206+
0: push %rbp
207+
1: mov %rsp,%rbp
208+
2: sub $0x228,%rsp
209+
3: sub $0x28,%rbp
210+
4: mov %rbx,0x0(%rbp)
206211

207212
|
208213
| **# mount -t bpf none /sys/fs/bpf/**
209214
| **# bpftool prog pin id 10 /sys/fs/bpf/prog**
210215
| **# bpftool prog load ./my_prog.o /sys/fs/bpf/prog2**
211216
| **# ls -l /sys/fs/bpf/**
212-
| -rw------- 1 root root 0 Jul 22 01:43 prog
213-
| -rw------- 1 root root 0 Jul 22 01:44 prog2
214217
215-
**# bpftool prog dum jited pinned /sys/fs/bpf/prog opcodes**
218+
::
219+
220+
-rw------- 1 root root 0 Jul 22 01:43 prog
221+
-rw------- 1 root root 0 Jul 22 01:44 prog2
222+
223+
**# bpftool prog dump jited pinned /sys/fs/bpf/prog opcodes**
216224

217225
::
218226

219-
push %rbp
220-
55
221-
mov %rsp,%rbp
222-
48 89 e5
223-
sub $0x228,%rsp
224-
48 81 ec 28 02 00 00
225-
sub $0x28,%rbp
226-
48 83 ed 28
227-
mov %rbx,0x0(%rbp)
228-
48 89 5d 00
227+
0: push %rbp
228+
55
229+
1: mov %rsp,%rbp
230+
48 89 e5
231+
4: sub $0x228,%rsp
232+
48 81 ec 28 02 00 00
233+
b: sub $0x28,%rbp
234+
48 83 ed 28
235+
f: mov %rbx,0x0(%rbp)
236+
48 89 5d 00
229237

230238
|
231239
| **# bpftool prog load xdp1_kern.o /sys/fs/bpf/xdp1 type xdp map name rxcnt id 7**
232240
| **# bpftool prog show pinned /sys/fs/bpf/xdp1**
233-
| 9: xdp name xdp_prog1 tag 539ec6ce11b52f98 gpl
234-
| loaded_at 2018-06-25T16:17:31-0700 uid 0
235-
| xlated 488B jited 336B memlock 4096B map_ids 7
236-
| **# rm /sys/fs/bpf/xdp1**
237-
|
241+
242+
::
243+
244+
9: xdp name xdp_prog1 tag 539ec6ce11b52f98 gpl
245+
loaded_at 2018-06-25T16:17:31-0700 uid 0
246+
xlated 488B jited 336B memlock 4096B map_ids 7
247+
248+
**# rm /sys/fs/bpf/xdp1**
238249

239250
SEE ALSO
240251
========

tools/bpf/bpftool/common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define BPF_FS_MAGIC 0xcafe4a11
2929
#endif
3030

31-
void p_err(const char *fmt, ...)
31+
void __printf(1, 2) p_err(const char *fmt, ...)
3232
{
3333
va_list ap;
3434

@@ -46,7 +46,7 @@ void p_err(const char *fmt, ...)
4646
va_end(ap);
4747
}
4848

49-
void p_info(const char *fmt, ...)
49+
void __printf(1, 2) p_info(const char *fmt, ...)
5050
{
5151
va_list ap;
5252

tools/bpf/bpftool/json_writer.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <malloc.h>
2121
#include <inttypes.h>
2222
#include <stdint.h>
23+
#include <linux/compiler.h>
2324

2425
#include "json_writer.h"
2526

@@ -157,15 +158,16 @@ void jsonw_name(json_writer_t *self, const char *name)
157158
putc(' ', self->out);
158159
}
159160

160-
void jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap)
161+
void __printf(2, 0)
162+
jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap)
161163
{
162164
jsonw_eor(self);
163165
putc('"', self->out);
164166
vfprintf(self->out, fmt, ap);
165167
putc('"', self->out);
166168
}
167169

168-
void jsonw_printf(json_writer_t *self, const char *fmt, ...)
170+
void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...)
169171
{
170172
va_list ap;
171173

tools/bpf/bpftool/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ int prog_parse_fd(int *argc, char ***argv);
147147
int map_parse_fd(int *argc, char ***argv);
148148
int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
149149

150-
#ifdef HAVE_LIBBFD_SUPPORT
151150
struct bpf_prog_linfo;
151+
#ifdef HAVE_LIBBFD_SUPPORT
152152
void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
153153
const char *arch, const char *disassembler_options,
154154
const struct btf *btf,

tools/bpf/bpftool/prog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static const char * const attach_type_strings[] = {
3232
[__MAX_BPF_ATTACH_TYPE] = NULL,
3333
};
3434

35-
enum bpf_attach_type parse_attach_type(const char *str)
35+
static enum bpf_attach_type parse_attach_type(const char *str)
3636
{
3737
enum bpf_attach_type type;
3838

@@ -798,7 +798,7 @@ struct map_replace {
798798
char *name;
799799
};
800800

801-
int map_replace_compar(const void *p1, const void *p2)
801+
static int map_replace_compar(const void *p1, const void *p2)
802802
{
803803
const struct map_replace *a = p1, *b = p2;
804804

tools/bpf/bpftool/xlated_dumper.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct kernel_sym *kernel_syms_search(struct dump_data *dd,
8181
sizeof(*dd->sym_mapping), kernel_syms_cmp) : NULL;
8282
}
8383

84-
static void print_insn(void *private_data, const char *fmt, ...)
84+
static void __printf(2, 3) print_insn(void *private_data, const char *fmt, ...)
8585
{
8686
va_list args;
8787

@@ -90,7 +90,7 @@ static void print_insn(void *private_data, const char *fmt, ...)
9090
va_end(args);
9191
}
9292

93-
static void
93+
static void __printf(2, 3)
9494
print_insn_for_graph(void *private_data, const char *fmt, ...)
9595
{
9696
char buf[64], *p;
@@ -121,7 +121,8 @@ print_insn_for_graph(void *private_data, const char *fmt, ...)
121121
printf("%s", buf);
122122
}
123123

124-
static void print_insn_json(void *private_data, const char *fmt, ...)
124+
static void __printf(2, 3)
125+
print_insn_json(void *private_data, const char *fmt, ...)
125126
{
126127
unsigned int l = strlen(fmt);
127128
char chomped_fmt[l];

0 commit comments

Comments
 (0)