Skip to content

Commit cf36a02

Browse files
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: Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 8918d91 commit cf36a02

File tree

2 files changed

+99
-16
lines changed

2 files changed

+99
-16
lines changed

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

+15-11
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

+84-5
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,80 @@ 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 const char *flags_strings(__u32 flags)
431+
{
432+
return json_output ? "none" : "";
433+
}
434+
435+
static int __show_dev_tc_bpf_name(__u32 id, char *name, size_t len)
436+
{
437+
struct bpf_prog_info info = {};
438+
__u32 ilen = sizeof(info);
439+
int fd, ret;
440+
441+
fd = bpf_prog_get_fd_by_id(id);
442+
if (fd < 0)
443+
return fd;
444+
ret = bpf_obj_get_info_by_fd(fd, &info, &ilen);
445+
if (ret < 0)
446+
goto out;
447+
ret = -ENOENT;
448+
if (info.name[0]) {
449+
get_prog_full_name(&info, fd, name, len);
450+
ret = 0;
451+
}
452+
out:
453+
close(fd);
454+
return ret;
455+
}
456+
457+
static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev,
458+
const enum bpf_attach_type loc)
459+
{
460+
__u32 prog_flags[64] = {}, link_flags[64] = {}, i;
461+
__u32 prog_ids[64] = {}, link_ids[64] = {};
462+
LIBBPF_OPTS(bpf_prog_query_opts, optq);
463+
char prog_name[MAX_PROG_FULL_NAME];
464+
int ret;
465+
466+
optq.prog_ids = prog_ids;
467+
optq.prog_attach_flags = prog_flags;
468+
optq.link_ids = link_ids;
469+
optq.link_attach_flags = link_flags;
470+
optq.count = ARRAY_SIZE(prog_ids);
471+
472+
ret = bpf_prog_query_opts(dev->ifindex, loc, &optq);
473+
if (ret)
474+
return;
475+
for (i = 0; i < optq.count; i++) {
476+
NET_START_OBJECT;
477+
NET_DUMP_STR("devname", "%s", dev->devname);
478+
NET_DUMP_UINT("ifindex", "(%u)", dev->ifindex);
479+
NET_DUMP_STR("kind", " %s", attach_loc_strings[loc]);
480+
ret = __show_dev_tc_bpf_name(prog_ids[i], prog_name,
481+
sizeof(prog_name));
482+
if (!ret)
483+
NET_DUMP_STR("name", " %s", prog_name);
484+
NET_DUMP_UINT("prog_id", " prog id %u", prog_ids[i]);
485+
if (prog_flags[i])
486+
NET_DUMP_STR("prog_flags", "%s", flags_strings(prog_flags[i]));
487+
if (link_ids[i])
488+
NET_DUMP_UINT("link_id", " link id %u",
489+
link_ids[i]);
490+
if (link_flags[i])
491+
NET_DUMP_STR("link_flags", "%s", flags_strings(link_flags[i]));
492+
NET_END_OBJECT_FINAL;
493+
}
494+
}
495+
496+
static void show_dev_tc_bpf(struct ip_devname_ifindex *dev)
497+
{
498+
__show_dev_tc_bpf(dev, BPF_TCX_INGRESS);
499+
__show_dev_tc_bpf(dev, BPF_TCX_EGRESS);
500+
}
501+
502+
static int show_dev_tc_bpf_classic(int sock, unsigned int nl_pid,
503+
struct ip_devname_ifindex *dev)
427504
{
428505
struct bpf_filter_t filter_info;
429506
struct bpf_tcinfo_t tcinfo;
@@ -790,8 +867,9 @@ static int do_show(int argc, char **argv)
790867
if (!ret) {
791868
NET_START_ARRAY("tc", "%s:\n");
792869
for (i = 0; i < dev_array.used_len; i++) {
793-
ret = show_dev_tc_bpf(sock, nl_pid,
794-
&dev_array.devices[i]);
870+
show_dev_tc_bpf(&dev_array.devices[i]);
871+
ret = show_dev_tc_bpf_classic(sock, nl_pid,
872+
&dev_array.devices[i]);
795873
if (ret)
796874
break;
797875
}
@@ -839,7 +917,8 @@ static int do_help(int argc, char **argv)
839917
" ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n"
840918
" " HELP_SPEC_OPTIONS " }\n"
841919
"\n"
842-
"Note: Only xdp and tc attachments are supported now.\n"
920+
"Note: Only xdp, tcx, tc, flow_dissector and netfilter attachments\n"
921+
" are currently supported.\n"
843922
" For progs attached to cgroups, use \"bpftool cgroup\"\n"
844923
" to dump program attachments. For program types\n"
845924
" sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"

0 commit comments

Comments
 (0)