Skip to content

Commit a32634b

Browse files
borkmannKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
bpftool: Extend net dump with tcx progs
Add support to dump fd-based attach types via bpftool. This includes both the tc BPF link and attach ops programs. Dumped information contain the attach location, function entry name, program ID and link ID when applicable. Example with tc BPF link: # ./bpftool net xdp: tc: bond0(4) tcx/ingress cil_from_netdev prog_id 784 link_id 10 bond0(4) tcx/egress cil_to_netdev prog_id 804 link_id 11 flow_dissector: netfilter: Example with tc BPF attach ops: # ./bpftool net xdp: tc: bond0(4) tcx/ingress cil_from_netdev prog_id 654 bond0(4) tcx/egress cil_to_netdev prog_id 672 flow_dissector: netfilter: Currently, permanent flags are not yet supported, so 'unknown' ones are dumped via NET_DUMP_UINT_ONLY() and once we do have permanent ones, we dump them as human readable string. Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: Quentin Monnet <[email protected]>
1 parent b97d0c1 commit a32634b

File tree

3 files changed

+116
-16
lines changed

3 files changed

+116
-16
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
bpftool-net
55
================
66
-------------------------------------------------------------------------------
7-
tool for inspection of netdev/tc related bpf prog attachments
7+
tool for inspection of networking related bpf prog attachments
88
-------------------------------------------------------------------------------
99

1010
:Manual section: 8
@@ -37,10 +37,13 @@ DESCRIPTION
3737
**bpftool net { show | list }** [ **dev** *NAME* ]
3838
List bpf program attachments in the kernel networking subsystem.
3939

40-
Currently, only device driver xdp attachments and tc filter
41-
classification/action attachments are implemented, i.e., for
42-
program types **BPF_PROG_TYPE_SCHED_CLS**,
43-
**BPF_PROG_TYPE_SCHED_ACT** and **BPF_PROG_TYPE_XDP**.
40+
Currently, device driver xdp attachments, tcx and old-style tc
41+
classifier/action attachments, flow_dissector as well as netfilter
42+
attachments are implemented, i.e., for
43+
program types **BPF_PROG_TYPE_XDP**, **BPF_PROG_TYPE_SCHED_CLS**,
44+
**BPF_PROG_TYPE_SCHED_ACT**, **BPF_PROG_TYPE_FLOW_DISSECTOR**,
45+
**BPF_PROG_TYPE_NETFILTER**.
46+
4447
For programs attached to a particular cgroup, e.g.,
4548
**BPF_PROG_TYPE_CGROUP_SKB**, **BPF_PROG_TYPE_CGROUP_SOCK**,
4649
**BPF_PROG_TYPE_SOCK_OPS** and **BPF_PROG_TYPE_CGROUP_SOCK_ADDR**,
@@ -49,12 +52,13 @@ DESCRIPTION
4952
bpf programs, users should consult other tools, e.g., iproute2.
5053

5154
The current output will start with all xdp program attachments, followed by
52-
all tc class/qdisc bpf program attachments. Both xdp programs and
53-
tc programs are ordered based on ifindex number. If multiple bpf
54-
programs attached to the same networking device through **tc filter**,
55-
the order will be first all bpf programs attached to tc classes, then
56-
all bpf programs attached to non clsact qdiscs, and finally all
57-
bpf programs attached to root and clsact qdisc.
55+
all tcx, then tc class/qdisc bpf program attachments, then flow_dissector
56+
and finally netfilter programs. Both xdp programs and tcx/tc programs are
57+
ordered based on ifindex number. If multiple bpf programs attached
58+
to the same networking device through **tc**, the order will be first
59+
all bpf programs attached to tcx, then tc classes, then all bpf programs
60+
attached to non clsact qdiscs, and finally all bpf programs attached
61+
to root and clsact qdisc.
5862

5963
**bpftool** **net attach** *ATTACH_TYPE* *PROG* **dev** *NAME* [ **overwrite** ]
6064
Attach bpf program *PROG* to network interface *NAME* with

tools/bpf/bpftool/net.c

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ static const char * const attach_type_strings[] = {
7676
[NET_ATTACH_TYPE_XDP_OFFLOAD] = "xdpoffload",
7777
};
7878

79+
static const char * const attach_loc_strings[] = {
80+
[BPF_TCX_INGRESS] = "tcx/ingress",
81+
[BPF_TCX_EGRESS] = "tcx/egress",
82+
};
83+
7984
const size_t net_attach_type_size = ARRAY_SIZE(attach_type_strings);
8085

8186
static enum net_attach_type parse_attach_type(const char *str)
@@ -422,8 +427,89 @@ static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb)
422427
filter_info->devname, filter_info->ifindex);
423428
}
424429

425-
static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
426-
struct ip_devname_ifindex *dev)
430+
static int __show_dev_tc_bpf_name(__u32 id, char *name, size_t len)
431+
{
432+
struct bpf_prog_info info = {};
433+
__u32 ilen = sizeof(info);
434+
int fd, ret;
435+
436+
fd = bpf_prog_get_fd_by_id(id);
437+
if (fd < 0)
438+
return fd;
439+
ret = bpf_obj_get_info_by_fd(fd, &info, &ilen);
440+
if (ret < 0)
441+
goto out;
442+
ret = -ENOENT;
443+
if (info.name[0]) {
444+
get_prog_full_name(&info, fd, name, len);
445+
ret = 0;
446+
}
447+
out:
448+
close(fd);
449+
return ret;
450+
}
451+
452+
static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev,
453+
const enum bpf_attach_type loc)
454+
{
455+
__u32 prog_flags[64] = {}, link_flags[64] = {}, i, j;
456+
__u32 prog_ids[64] = {}, link_ids[64] = {};
457+
LIBBPF_OPTS(bpf_prog_query_opts, optq);
458+
char prog_name[MAX_PROG_FULL_NAME];
459+
int ret;
460+
461+
optq.prog_ids = prog_ids;
462+
optq.prog_attach_flags = prog_flags;
463+
optq.link_ids = link_ids;
464+
optq.link_attach_flags = link_flags;
465+
optq.count = ARRAY_SIZE(prog_ids);
466+
467+
ret = bpf_prog_query_opts(dev->ifindex, loc, &optq);
468+
if (ret)
469+
return;
470+
for (i = 0; i < optq.count; i++) {
471+
NET_START_OBJECT;
472+
NET_DUMP_STR("devname", "%s", dev->devname);
473+
NET_DUMP_UINT("ifindex", "(%u)", dev->ifindex);
474+
NET_DUMP_STR("kind", " %s", attach_loc_strings[loc]);
475+
ret = __show_dev_tc_bpf_name(prog_ids[i], prog_name,
476+
sizeof(prog_name));
477+
if (!ret)
478+
NET_DUMP_STR("name", " %s", prog_name);
479+
NET_DUMP_UINT("prog_id", " prog_id %u ", prog_ids[i]);
480+
if (prog_flags[i] || json_output) {
481+
NET_START_ARRAY("prog_flags", "%s ");
482+
for (j = 0; prog_flags[i] && j < 32; j++) {
483+
if (!(prog_flags[i] & (1 << j)))
484+
continue;
485+
NET_DUMP_UINT_ONLY(1 << j);
486+
}
487+
NET_END_ARRAY("");
488+
}
489+
if (link_ids[i] || json_output) {
490+
NET_DUMP_UINT("link_id", "link_id %u ", link_ids[i]);
491+
if (link_flags[i] || json_output) {
492+
NET_START_ARRAY("link_flags", "%s ");
493+
for (j = 0; link_flags[i] && j < 32; j++) {
494+
if (!(link_flags[i] & (1 << j)))
495+
continue;
496+
NET_DUMP_UINT_ONLY(1 << j);
497+
}
498+
NET_END_ARRAY("");
499+
}
500+
}
501+
NET_END_OBJECT_FINAL;
502+
}
503+
}
504+
505+
static void show_dev_tc_bpf(struct ip_devname_ifindex *dev)
506+
{
507+
__show_dev_tc_bpf(dev, BPF_TCX_INGRESS);
508+
__show_dev_tc_bpf(dev, BPF_TCX_EGRESS);
509+
}
510+
511+
static int show_dev_tc_bpf_classic(int sock, unsigned int nl_pid,
512+
struct ip_devname_ifindex *dev)
427513
{
428514
struct bpf_filter_t filter_info;
429515
struct bpf_tcinfo_t tcinfo;
@@ -790,8 +876,9 @@ static int do_show(int argc, char **argv)
790876
if (!ret) {
791877
NET_START_ARRAY("tc", "%s:\n");
792878
for (i = 0; i < dev_array.used_len; i++) {
793-
ret = show_dev_tc_bpf(sock, nl_pid,
794-
&dev_array.devices[i]);
879+
show_dev_tc_bpf(&dev_array.devices[i]);
880+
ret = show_dev_tc_bpf_classic(sock, nl_pid,
881+
&dev_array.devices[i]);
795882
if (ret)
796883
break;
797884
}
@@ -839,7 +926,8 @@ static int do_help(int argc, char **argv)
839926
" ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n"
840927
" " HELP_SPEC_OPTIONS " }\n"
841928
"\n"
842-
"Note: Only xdp and tc attachments are supported now.\n"
929+
"Note: Only xdp, tcx, tc, flow_dissector and netfilter attachments\n"
930+
" are currently supported.\n"
843931
" For progs attached to cgroups, use \"bpftool cgroup\"\n"
844932
" to dump program attachments. For program types\n"
845933
" sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"

tools/bpf/bpftool/netlink_dumper.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@
7676
fprintf(stdout, fmt_str, val); \
7777
}
7878

79+
#define NET_DUMP_UINT_ONLY(str) \
80+
{ \
81+
if (json_output) \
82+
jsonw_uint(json_wtr, str); \
83+
else \
84+
fprintf(stdout, "%u ", str); \
85+
}
86+
7987
#define NET_DUMP_STR(name, fmt_str, str) \
8088
{ \
8189
if (json_output) \

0 commit comments

Comments
 (0)