Skip to content

Commit 8e20822

Browse files
committed
bpftool: Add support for tc fd-based attach types
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, link ID when applicable as well as the attach priority. Example with tc BPF link: # ./bpftool net xdp: tc: lo(1) bpf/ingress tc_handler_in id 189 link 40 prio 1 lo(1) bpf/egress tc_handler_eg id 190 link 39 prio 1 flow_dissector: Example with tc BPF attach ops and also one instance of old-style cls_bpf: # ./bpftool net xdp: tc: lo(1) bpf/ingress tc_handler_in id 201 prio 1 lo(1) clsact/ingress tc_handler_old:[203] id 203 flow_dissector: Co-developed-by: Nikolay Aleksandrov <[email protected]> Signed-off-by: Nikolay Aleksandrov <[email protected]> Co-developed-by: Daniel Borkmann <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 565b5a2 commit 8e20822

File tree

1 file changed

+72
-4
lines changed

1 file changed

+72
-4
lines changed

tools/bpf/bpftool/net.c

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ static const char * const attach_type_strings[] = {
7474
[NET_ATTACH_TYPE_XDP_OFFLOAD] = "xdpoffload",
7575
};
7676

77+
static const char * const attach_loc_strings[] = {
78+
[BPF_NET_INGRESS] = "bpf/ingress",
79+
[BPF_NET_EGRESS] = "bpf/egress",
80+
};
81+
7782
const size_t net_attach_type_size = ARRAY_SIZE(attach_type_strings);
7883

7984
static enum net_attach_type parse_attach_type(const char *str)
@@ -420,8 +425,69 @@ static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb)
420425
filter_info->devname, filter_info->ifindex);
421426
}
422427

423-
static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
424-
struct ip_devname_ifindex *dev)
428+
static int __show_dev_tc_bpf_name(__u32 id, char *name, size_t len)
429+
{
430+
struct bpf_prog_info info = {};
431+
__u32 ilen = sizeof(info);
432+
int fd, ret;
433+
434+
fd = bpf_prog_get_fd_by_id(id);
435+
if (fd < 0)
436+
return fd;
437+
ret = bpf_obj_get_info_by_fd(fd, &info, &ilen);
438+
if (ret < 0)
439+
goto out;
440+
ret = -ENOENT;
441+
if (info.name) {
442+
get_prog_full_name(&info, fd, name, len);
443+
ret = 0;
444+
}
445+
out:
446+
close(fd);
447+
return ret;
448+
}
449+
450+
static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev,
451+
const enum bpf_attach_type loc)
452+
{
453+
__u32 i, prog_cnt, attach_flags = 0;
454+
char prog_name[MAX_PROG_FULL_NAME];
455+
struct bpf_query_info progs[64];
456+
int ret;
457+
458+
memset(progs, 0, sizeof(progs));
459+
prog_cnt = ARRAY_SIZE(progs);
460+
ret = bpf_prog_query(dev->ifindex, loc, 0, &attach_flags,
461+
progs, &prog_cnt);
462+
if (ret)
463+
return;
464+
for (i = 0; i < prog_cnt; i++) {
465+
NET_START_OBJECT;
466+
NET_DUMP_STR("devname", "%s", dev->devname);
467+
NET_DUMP_UINT("ifindex", "(%u)", dev->ifindex);
468+
NET_DUMP_STR("kind", " %s", attach_loc_strings[loc]);
469+
ret = __show_dev_tc_bpf_name(progs[i].prog_id,
470+
prog_name,
471+
sizeof(prog_name));
472+
if (!ret)
473+
NET_DUMP_STR("name", " %s", prog_name);
474+
NET_DUMP_UINT("id", " id %u", progs[i].prog_id);
475+
if (progs[i].link_id)
476+
NET_DUMP_UINT("link", " link %u",
477+
progs[i].link_id);
478+
NET_DUMP_UINT("prio", " prio %u", progs[i].prio);
479+
NET_END_OBJECT_FINAL;
480+
}
481+
}
482+
483+
static void show_dev_tc_bpf(struct ip_devname_ifindex *dev)
484+
{
485+
__show_dev_tc_bpf(dev, BPF_NET_INGRESS);
486+
__show_dev_tc_bpf(dev, BPF_NET_EGRESS);
487+
}
488+
489+
static int show_dev_tc_bpf_legacy(int sock, unsigned int nl_pid,
490+
struct ip_devname_ifindex *dev)
425491
{
426492
struct bpf_filter_t filter_info;
427493
struct bpf_tcinfo_t tcinfo;
@@ -686,8 +752,10 @@ static int do_show(int argc, char **argv)
686752
if (!ret) {
687753
NET_START_ARRAY("tc", "%s:\n");
688754
for (i = 0; i < dev_array.used_len; i++) {
689-
ret = show_dev_tc_bpf(sock, nl_pid,
690-
&dev_array.devices[i]);
755+
show_dev_tc_bpf(&dev_array.devices[i]);
756+
757+
ret = show_dev_tc_bpf_legacy(sock, nl_pid,
758+
&dev_array.devices[i]);
691759
if (ret)
692760
break;
693761
}

0 commit comments

Comments
 (0)