Skip to content

Commit a5dd498

Browse files
committed
Merge branch 'bpftool-JSON'
Jakub Kicinski says: ==================== tools: bpftool: Add JSON output to bpftool Quentin says: This series introduces support for JSON output to all bpftool commands. It adds option parsing, and several options are created: * -j, --json Switch to JSON output. * -p, --pretty Switch to JSON and print it in a human-friendly fashion. * -h, --help Print generic help message. * -V, --version Print version number. This code uses a "json_writer", which is a copy of the one written by Stephen Hemminger in iproute2. --- I don't know if there is an easy way to share the code for json_write without copying the file, so I am very open to suggestions on this matter. ==================== Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 4c4fde2 + 0641c3c commit a5dd498

11 files changed

Lines changed: 1209 additions & 184 deletions

File tree

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,29 @@ tool for inspection and simple manipulation of eBPF maps
1010
SYNOPSIS
1111
========
1212

13-
**bpftool** **map** *COMMAND*
13+
**bpftool** [*OPTIONS*] **map** *COMMAND*
14+
15+
*OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] }
1416

1517
*COMMANDS* :=
16-
{ show | dump | update | lookup | getnext | delete | pin | help }
18+
{ **show** | **dump** | **update** | **lookup** | **getnext** | **delete**
19+
| **pin** | **help** }
1720
1821
MAP COMMANDS
1922
=============
2023

21-
| **bpftool** map show [*MAP*]
22-
| **bpftool** map dump *MAP*
23-
| **bpftool** map update *MAP* key *BYTES* value *VALUE* [*UPDATE_FLAGS*]
24-
| **bpftool** map lookup *MAP* key *BYTES*
25-
| **bpftool** map getnext *MAP* [key *BYTES*]
26-
| **bpftool** map delete *MAP* key *BYTES*
27-
| **bpftool** map pin *MAP* *FILE*
28-
| **bpftool** map help
24+
| **bpftool** **map show** [*MAP*]
25+
| **bpftool** **map dump** *MAP*
26+
| **bpftool** **map update** *MAP* **key** *BYTES* **value** *VALUE* [*UPDATE_FLAGS*]
27+
| **bpftool** **map lookup** *MAP* **key** *BYTES*
28+
| **bpftool** **map getnext** *MAP* [**key** *BYTES*]
29+
| **bpftool** **map delete** *MAP* **key** *BYTES*
30+
| **bpftool** **map pin** *MAP* *FILE*
31+
| **bpftool** **map help**
2932
|
30-
| *MAP* := { id MAP_ID | pinned FILE }
31-
| *VALUE* := { BYTES | MAP | PROGRAM }
32-
| *UPDATE_FLAGS* := { any | exist | noexist }
33+
| *MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
34+
| *VALUE* := { *BYTES* | *MAP* | *PROGRAM* }
35+
| *UPDATE_FLAGS* := { **any** | **exist** | **noexist** }
3336
3437
DESCRIPTION
3538
===========
@@ -68,6 +71,21 @@ DESCRIPTION
6871
**bpftool map help**
6972
Print short help message.
7073

74+
OPTIONS
75+
=======
76+
-h, --help
77+
Print short generic help message (similar to **bpftool help**).
78+
79+
-v, --version
80+
Print version number (similar to **bpftool version**).
81+
82+
-j, --json
83+
Generate JSON output. For commands that cannot produce JSON, this
84+
option has no effect.
85+
86+
-p, --pretty
87+
Generate human-readable JSON output. Implies **-j**.
88+
7189
EXAMPLES
7290
========
7391
**# bpftool map show**

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

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@ tool for inspection and simple manipulation of eBPF progs
1010
SYNOPSIS
1111
========
1212

13-
| **bpftool** prog show [*PROG*]
14-
| **bpftool** prog dump xlated *PROG* [{file *FILE* | opcodes }]
15-
| **bpftool** prog dump jited *PROG* [{file *FILE* | opcodes }]
16-
| **bpftool** prog pin *PROG* *FILE*
17-
| **bpftool** prog help
13+
**bpftool** [*OPTIONS*] **prog** *COMMAND*
14+
15+
*OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] }
16+
17+
*COMMANDS* :=
18+
{ **show** | **dump xlated** | **dump jited** | **pin** | **help** }
19+
20+
MAP COMMANDS
21+
=============
22+
23+
| **bpftool** **prog show** [*PROG*]
24+
| **bpftool** **prog dump xlated** *PROG* [{**file** *FILE* | **opcodes**}]
25+
| **bpftool** **prog dump jited** *PROG* [{**file** *FILE* | **opcodes**}]
26+
| **bpftool** **prog pin** *PROG* *FILE*
27+
| **bpftool** **prog help**
1828
|
19-
| *PROG* := { id *PROG_ID* | pinned *FILE* | tag *PROG_TAG* }
29+
| *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* }
2030
2131
DESCRIPTION
2232
===========
@@ -50,6 +60,21 @@ DESCRIPTION
5060
**bpftool prog help**
5161
Print short help message.
5262

63+
OPTIONS
64+
=======
65+
-h, --help
66+
Print short generic help message (similar to **bpftool help**).
67+
68+
-v, --version
69+
Print version number (similar to **bpftool version**).
70+
71+
-j, --json
72+
Generate JSON output. For commands that cannot produce JSON, this
73+
option has no effect.
74+
75+
-p, --pretty
76+
Generate human-readable JSON output. Implies **-j**.
77+
5378
EXAMPLES
5479
========
5580
**# bpftool prog show**
@@ -59,13 +84,33 @@ EXAMPLES
5984
loaded_at Sep 29/20:11 uid 0
6085
xlated 528B jited 370B memlock 4096B map_ids 10
6186

87+
**# bpftool --json --pretty prog show**
88+
89+
::
90+
91+
{
92+
"programs": [{
93+
"id": 10,
94+
"type": "xdp",
95+
"tag": "005a3d2123620c8b",
96+
"loaded_at": "Sep 29/20:11",
97+
"uid": 0,
98+
"bytes_xlated": 528,
99+
"jited": true,
100+
"bytes_jited": 370,
101+
"bytes_memlock": 4096,
102+
"map_ids": [10
103+
]
104+
}
105+
]
106+
}
107+
62108
|
63109
| **# bpftool prog dump xlated id 10 file /tmp/t**
64110
| **# ls -l /tmp/t**
65111
| -rw------- 1 root root 560 Jul 22 01:42 /tmp/t
66112
67-
|
68-
| **# bpftool prog dum jited pinned /sys/fs/bpf/prog**
113+
**# bpftool prog dum jited tag 005a3d2123620c8b**
69114

70115
::
71116

@@ -75,6 +120,26 @@ EXAMPLES
75120
sub $0x28,%rbp
76121
mov %rbx,0x0(%rbp)
77122

123+
|
124+
| **# mount -t bpf none /sys/fs/bpf/**
125+
| **# bpftool prog pin id 10 /sys/fs/bpf/prog**
126+
| **# ls -l /sys/fs/bpf/**
127+
| -rw------- 1 root root 0 Jul 22 01:43 prog
128+
129+
**# bpftool prog dum jited pinned /sys/fs/bpf/prog opcodes**
130+
131+
::
132+
133+
push %rbp
134+
55
135+
mov %rsp,%rbp
136+
48 89 e5
137+
sub $0x228,%rsp
138+
48 81 ec 28 02 00 00
139+
sub $0x28,%rbp
140+
48 83 ed 28
141+
mov %rbx,0x0(%rbp)
142+
48 89 5d 00
78143

79144

80145
SEE ALSO

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ tool for inspection and simple manipulation of eBPF programs and maps
1010
SYNOPSIS
1111
========
1212

13-
**bpftool** *OBJECT* { *COMMAND* | help }
13+
**bpftool** [*OPTIONS*] *OBJECT* { *COMMAND* | **help** }
1414

15-
**bpftool** batch file *FILE*
15+
**bpftool** **batch file** *FILE*
1616

17-
**bpftool** version
17+
**bpftool** **version**
1818

1919
*OBJECT* := { **map** | **program** }
2020

21+
*OPTIONS* := { { **-V** | **--version** } | { **-h** | **--help** }
22+
| { **-j** | **--json** } [{ **-p** | **--pretty** }] }
23+
2124
*MAP-COMMANDS* :=
22-
{ show | dump | update | lookup | getnext | delete | pin | help }
25+
{ **show** | **dump** | **update** | **lookup** | **getnext** | **delete**
26+
| **pin** | **help** }
2327
24-
*PROG-COMMANDS* := { show | dump jited | dump xlated | pin | help }
28+
*PROG-COMMANDS* := { **show** | **dump jited** | **dump xlated** | **pin**
29+
| **help** }
2530
2631
DESCRIPTION
2732
===========
@@ -31,6 +36,21 @@ DESCRIPTION
3136
Note that format of the output of all tools is not guaranteed to be
3237
stable and should not be depended upon.
3338

39+
OPTIONS
40+
=======
41+
-h, --help
42+
Print short help message (similar to **bpftool help**).
43+
44+
-v, --version
45+
Print version number (similar to **bpftool version**).
46+
47+
-j, --json
48+
Generate JSON output. For commands that cannot produce JSON, this
49+
option has no effect.
50+
51+
-p, --pretty
52+
Generate human-readable JSON output. Implies **-j**.
53+
3454
SEE ALSO
3555
========
3656
**bpftool-map**\ (8), **bpftool-prog**\ (8)

‎tools/bpf/bpftool/common.c‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
6666

6767
fd = bpf_obj_get(path);
6868
if (fd < 0) {
69-
err("bpf obj get (%s): %s\n", path,
70-
errno == EACCES && !is_bpffs(dirname(path)) ?
69+
p_err("bpf obj get (%s): %s", path,
70+
errno == EACCES && !is_bpffs(dirname(path)) ?
7171
"directory not in bpf file system (bpffs)" :
7272
strerror(errno));
7373
return -1;
@@ -79,7 +79,7 @@ int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
7979
return type;
8080
}
8181
if (type != exp_type) {
82-
err("incorrect object type: %s\n", get_fd_type_name(type));
82+
p_err("incorrect object type: %s", get_fd_type_name(type));
8383
close(fd);
8484
return -1;
8585
}
@@ -95,14 +95,14 @@ int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
9595
int fd;
9696

9797
if (!is_prefix(*argv, "id")) {
98-
err("expected 'id' got %s\n", *argv);
98+
p_err("expected 'id' got %s", *argv);
9999
return -1;
100100
}
101101
NEXT_ARG();
102102

103103
id = strtoul(*argv, &endptr, 0);
104104
if (*endptr) {
105-
err("can't parse %s as ID\n", *argv);
105+
p_err("can't parse %s as ID", *argv);
106106
return -1;
107107
}
108108
NEXT_ARG();
@@ -112,15 +112,15 @@ int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
112112

113113
fd = get_fd_by_id(id);
114114
if (fd < 0) {
115-
err("can't get prog by id (%u): %s\n", id, strerror(errno));
115+
p_err("can't get prog by id (%u): %s", id, strerror(errno));
116116
return -1;
117117
}
118118

119119
err = bpf_obj_pin(fd, *argv);
120120
close(fd);
121121
if (err) {
122-
err("can't pin the object (%s): %s\n", *argv,
123-
errno == EACCES && !is_bpffs(dirname(*argv)) ?
122+
p_err("can't pin the object (%s): %s", *argv,
123+
errno == EACCES && !is_bpffs(dirname(*argv)) ?
124124
"directory not in bpf file system (bpffs)" :
125125
strerror(errno));
126126
return -1;
@@ -153,11 +153,11 @@ int get_fd_type(int fd)
153153

154154
n = readlink(path, buf, sizeof(buf));
155155
if (n < 0) {
156-
err("can't read link type: %s\n", strerror(errno));
156+
p_err("can't read link type: %s", strerror(errno));
157157
return -1;
158158
}
159159
if (n == sizeof(path)) {
160-
err("can't read link type: path too long!\n");
160+
p_err("can't read link type: path too long!");
161161
return -1;
162162
}
163163

@@ -181,7 +181,7 @@ char *get_fdinfo(int fd, const char *key)
181181

182182
fdi = fopen(path, "r");
183183
if (!fdi) {
184-
err("can't open fdinfo: %s\n", strerror(errno));
184+
p_err("can't open fdinfo: %s", strerror(errno));
185185
return NULL;
186186
}
187187

@@ -196,7 +196,7 @@ char *get_fdinfo(int fd, const char *key)
196196

197197
value = strchr(line, '\t');
198198
if (!value || !value[1]) {
199-
err("malformed fdinfo!?\n");
199+
p_err("malformed fdinfo!?");
200200
free(line);
201201
return NULL;
202202
}
@@ -209,8 +209,18 @@ char *get_fdinfo(int fd, const char *key)
209209
return line;
210210
}
211211

212-
err("key '%s' not found in fdinfo\n", key);
212+
p_err("key '%s' not found in fdinfo", key);
213213
free(line);
214214
fclose(fdi);
215215
return NULL;
216216
}
217+
218+
void print_hex_data_json(uint8_t *data, size_t len)
219+
{
220+
unsigned int i;
221+
222+
jsonw_start_array(json_wtr);
223+
for (i = 0; i < len; i++)
224+
jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
225+
jsonw_end_array(json_wtr);
226+
}

0 commit comments

Comments
 (0)