Skip to content

Commit f6f3bac

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
tools/bpf: bpftool: add net support
Add "bpftool net" support. Networking devices are enumerated to dump device index/name associated with xdp progs. For each networking device, tc classes and qdiscs are enumerated in order to check their bpf filters. In addition, root handle and clsact ingress/egress are also checked for bpf filters. Not all filter information is printed out. Only ifindex, kind, filter name, prog_id and tag are printed out, which are good enough to show attachment information. If the filter action is a bpf action, its bpf program id, bpf name and tag will be printed out as well. For example, $ ./bpftool net xdp [ ifindex 2 devname eth0 prog_id 198 ] tc_filters [ ifindex 2 kind qdisc_htb name prefix_matcher.o:[cls_prefix_matcher_htb] prog_id 111727 tag d08fe3b4319bc2fd act [] ifindex 2 kind qdisc_clsact_ingress name fbflow_icmp prog_id 130246 tag 3f265c7f26db62c9 act [] ifindex 2 kind qdisc_clsact_egress name prefix_matcher.o:[cls_prefix_matcher_clsact] prog_id 111726 tag 99a197826974c876 ifindex 2 kind qdisc_clsact_egress name cls_fg_dscp prog_id 108619 tag dc4630674fd72dcc act [] ifindex 2 kind qdisc_clsact_egress name fbflow_egress prog_id 130245 tag 72d2d830d6888d2c ] $ ./bpftool -jp net [{ "xdp": [{ "ifindex": 2, "devname": "eth0", "prog_id": 198 } ], "tc_filters": [{ "ifindex": 2, "kind": "qdisc_htb", "name": "prefix_matcher.o:[cls_prefix_matcher_htb]", "prog_id": 111727, "tag": "d08fe3b4319bc2fd", "act": [] },{ "ifindex": 2, "kind": "qdisc_clsact_ingress", "name": "fbflow_icmp", "prog_id": 130246, "tag": "3f265c7f26db62c9", "act": [] },{ "ifindex": 2, "kind": "qdisc_clsact_egress", "name": "prefix_matcher.o:[cls_prefix_matcher_clsact]", "prog_id": 111726, "tag": "99a197826974c876" },{ "ifindex": 2, "kind": "qdisc_clsact_egress", "name": "cls_fg_dscp", "prog_id": 108619, "tag": "dc4630674fd72dcc", "act": [] },{ "ifindex": 2, "kind": "qdisc_clsact_egress", "name": "fbflow_egress", "prog_id": 130245, "tag": "72d2d830d6888d2c" } ] } ] Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 36f1678 commit f6f3bac

File tree

8 files changed

+676
-7
lines changed

8 files changed

+676
-7
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
================
2+
bpftool-net
3+
================
4+
-------------------------------------------------------------------------------
5+
tool for inspection of netdev/tc related bpf prog attachments
6+
-------------------------------------------------------------------------------
7+
8+
:Manual section: 8
9+
10+
SYNOPSIS
11+
========
12+
13+
**bpftool** [*OPTIONS*] **net** *COMMAND*
14+
15+
*OPTIONS* := { [{ **-j** | **--json** }] [{ **-p** | **--pretty** }] }
16+
17+
*COMMANDS* :=
18+
{ **show** | **list** } [ **dev** name ] | **help**
19+
20+
NET COMMANDS
21+
============
22+
23+
| **bpftool** **net { show | list } [ dev name ]**
24+
| **bpftool** **net help**
25+
26+
DESCRIPTION
27+
===========
28+
**bpftool net { show | list } [ dev name ]**
29+
List all networking device driver and tc attachment in the system.
30+
31+
Output will start with all xdp program attachment, followed by
32+
all tc class/qdisc bpf program attachments. Both xdp programs and
33+
tc programs are ordered based on ifindex number. If multiple bpf
34+
programs attached to the same networking device through **tc filter**,
35+
the order will be first all bpf programs attached to tc classes, then
36+
all bpf programs attached to non clsact qdiscs, and finally all
37+
bpf programs attached to root and clsact qdisc.
38+
39+
**bpftool net help**
40+
Print short help message.
41+
42+
OPTIONS
43+
=======
44+
-h, --help
45+
Print short generic help message (similar to **bpftool help**).
46+
47+
-v, --version
48+
Print version number (similar to **bpftool version**).
49+
50+
-j, --json
51+
Generate JSON output. For commands that cannot produce JSON, this
52+
option has no effect.
53+
54+
-p, --pretty
55+
Generate human-readable JSON output. Implies **-j**.
56+
57+
EXAMPLES
58+
========
59+
60+
| **# bpftool net**
61+
62+
::
63+
64+
xdp [
65+
ifindex 2 devname eth0 prog_id 198
66+
]
67+
tc_filters [
68+
ifindex 2 kind qdisc_htb name prefix_matcher.o:[cls_prefix_matcher_htb]
69+
prog_id 111727 tag d08fe3b4319bc2fd act []
70+
ifindex 2 kind qdisc_clsact_ingress name fbflow_icmp
71+
prog_id 130246 tag 3f265c7f26db62c9 act []
72+
ifindex 2 kind qdisc_clsact_egress name prefix_matcher.o:[cls_prefix_matcher_clsact]
73+
prog_id 111726 tag 99a197826974c876
74+
ifindex 2 kind qdisc_clsact_egress name cls_fg_dscp
75+
prog_id 108619 tag dc4630674fd72dcc act []
76+
ifindex 2 kind qdisc_clsact_egress name fbflow_egress
77+
prog_id 130245 tag 72d2d830d6888d2c
78+
]
79+
80+
|
81+
| **# bpftool -jp net**
82+
83+
::
84+
85+
[{
86+
"xdp": [{
87+
"ifindex": 2,
88+
"devname": "eth0",
89+
"prog_id": 198
90+
}
91+
],
92+
"tc_filters": [{
93+
"ifindex": 2,
94+
"kind": "qdisc_htb",
95+
"name": "prefix_matcher.o:[cls_prefix_matcher_htb]",
96+
"prog_id": 111727,
97+
"tag": "d08fe3b4319bc2fd",
98+
"act": []
99+
},{
100+
"ifindex": 2,
101+
"kind": "qdisc_clsact_ingress",
102+
"name": "fbflow_icmp",
103+
"prog_id": 130246,
104+
"tag": "3f265c7f26db62c9",
105+
"act": []
106+
},{
107+
"ifindex": 2,
108+
"kind": "qdisc_clsact_egress",
109+
"name": "prefix_matcher.o:[cls_prefix_matcher_clsact]",
110+
"prog_id": 111726,
111+
"tag": "99a197826974c876"
112+
},{
113+
"ifindex": 2,
114+
"kind": "qdisc_clsact_egress",
115+
"name": "cls_fg_dscp",
116+
"prog_id": 108619,
117+
"tag": "dc4630674fd72dcc",
118+
"act": []
119+
},{
120+
"ifindex": 2,
121+
"kind": "qdisc_clsact_egress",
122+
"name": "fbflow_egress",
123+
"prog_id": 130245,
124+
"tag": "72d2d830d6888d2c"
125+
}
126+
]
127+
}
128+
]
129+
130+
131+
SEE ALSO
132+
========
133+
**bpftool**\ (8), **bpftool-prog**\ (8), **bpftool-map**\ (8)

tools/bpf/bpftool/Documentation/bpftool.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SYNOPSIS
1616

1717
**bpftool** **version**
1818

19-
*OBJECT* := { **map** | **program** | **cgroup** | **perf** }
19+
*OBJECT* := { **map** | **program** | **cgroup** | **perf** | **net** }
2020

2121
*OPTIONS* := { { **-V** | **--version** } | { **-h** | **--help** }
2222
| { **-j** | **--json** } [{ **-p** | **--pretty** }] }
@@ -32,6 +32,8 @@ SYNOPSIS
3232

3333
*PERF-COMMANDS* := { **show** | **list** | **help** }
3434

35+
*NET-COMMANDS* := { **show** | **list** | **help** }
36+
3537
DESCRIPTION
3638
===========
3739
*bpftool* allows for inspection and simple modification of BPF objects
@@ -58,4 +60,4 @@ OPTIONS
5860
SEE ALSO
5961
========
6062
**bpftool-map**\ (8), **bpftool-prog**\ (8), **bpftool-cgroup**\ (8)
61-
**bpftool-perf**\ (8)
63+
**bpftool-perf**\ (8), **bpftool-net**\ (8)

tools/bpf/bpftool/bash-completion/bpftool

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,10 @@ _bpftool()
494494
_filedir
495495
return 0
496496
;;
497-
tree)
498-
_filedir
499-
return 0
500-
;;
497+
tree)
498+
_filedir
499+
return 0
500+
;;
501501
attach|detach)
502502
local ATTACH_TYPES='ingress egress sock_create sock_ops \
503503
device bind4 bind6 post_bind4 post_bind6 connect4 \
@@ -552,6 +552,15 @@ _bpftool()
552552
;;
553553
esac
554554
;;
555+
net)
556+
case $command in
557+
*)
558+
[[ $prev == $object ]] && \
559+
COMPREPLY=( $( compgen -W 'help \
560+
show list' -- "$cur" ) )
561+
;;
562+
esac
563+
;;
555564
esac
556565
} &&
557566
complete -F _bpftool bpftool

tools/bpf/bpftool/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static int do_help(int argc, char **argv)
8585
" %s batch file FILE\n"
8686
" %s version\n"
8787
"\n"
88-
" OBJECT := { prog | map | cgroup | perf }\n"
88+
" OBJECT := { prog | map | cgroup | perf | net }\n"
8989
" " HELP_SPEC_OPTIONS "\n"
9090
"",
9191
bin_name, bin_name, bin_name);
@@ -215,6 +215,7 @@ static const struct cmd cmds[] = {
215215
{ "map", do_map },
216216
{ "cgroup", do_cgroup },
217217
{ "perf", do_perf },
218+
{ "net", do_net },
218219
{ "version", do_version },
219220
{ 0 }
220221
};

tools/bpf/bpftool/main.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ int do_map(int argc, char **arg);
136136
int do_event_pipe(int argc, char **argv);
137137
int do_cgroup(int argc, char **arg);
138138
int do_perf(int argc, char **arg);
139+
int do_net(int argc, char **arg);
139140

140141
int prog_parse_fd(int *argc, char ***argv);
141142
int map_parse_fd(int *argc, char ***argv);
@@ -165,4 +166,10 @@ struct btf_dumper {
165166
*/
166167
int btf_dumper_type(const struct btf_dumper *d, __u32 type_id,
167168
const void *data);
169+
170+
struct nlattr;
171+
struct ifinfomsg;
172+
struct tcmsg;
173+
int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb);
174+
int do_filter_dump(struct tcmsg *ifinfo, struct nlattr **tb, const char *kind);
168175
#endif

0 commit comments

Comments
 (0)