Skip to content

Commit

Permalink
bpftool: Add support for tc fd-based attach types
Browse files Browse the repository at this point in the history
[ commit msg tbd ]

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]>
Signed-off-by: Daniel Borkmann <[email protected]>
  • Loading branch information
borkmann committed May 24, 2023
1 parent 5305823 commit eaddb47
Showing 1 changed file with 88 additions and 4 deletions.
92 changes: 88 additions & 4 deletions tools/bpf/bpftool/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ static const char * const attach_type_strings[] = {
[NET_ATTACH_TYPE_XDP_OFFLOAD] = "xdpoffload",
};

static const char * const attach_loc_strings[] = {
[BPF_TCX_INGRESS] = "bpf/ingress",
[BPF_TCX_EGRESS] = "bpf/egress",
};

const size_t net_attach_type_size = ARRAY_SIZE(attach_type_strings);

static enum net_attach_type parse_attach_type(const char *str)
Expand Down Expand Up @@ -422,8 +427,86 @@ static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb)
filter_info->devname, filter_info->ifindex);
}

static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
struct ip_devname_ifindex *dev)
static const char *flags_strings(__u32 flags)
{
if (flags == (BPF_F_FIRST | BPF_F_LAST))
return json_output ? "first,last" : " first last";
if (flags & BPF_F_FIRST)
return json_output ? "first" : " first";
if (flags & BPF_F_LAST)
return json_output ? "last" : " last";
return json_output ? "none" : "";
}

static int __show_dev_tc_bpf_name(__u32 id, char *name, size_t len)
{
struct bpf_prog_info info = {};
__u32 ilen = sizeof(info);
int fd, ret;

fd = bpf_prog_get_fd_by_id(id);
if (fd < 0)
return fd;
ret = bpf_obj_get_info_by_fd(fd, &info, &ilen);
if (ret < 0)
goto out;
ret = -ENOENT;
if (info.name) {
get_prog_full_name(&info, fd, name, len);
ret = 0;
}
out:
close(fd);
return ret;
}

static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev,
const enum bpf_attach_type loc)
{
DECLARE_LIBBPF_OPTS(bpf_prog_query_opts, optq);
__u32 prog_flags[64] = {}, link_flags[64] = {}, i;
__u32 prog_ids[64] = {}, link_ids[64] = {};
char prog_name[MAX_PROG_FULL_NAME];
int ret;

optq.prog_ids = prog_ids;
optq.prog_attach_flags = prog_flags;
optq.link_ids = link_ids;
optq.link_attach_flags = link_flags;
optq.count = ARRAY_SIZE(prog_ids);

ret = bpf_prog_query_opts(dev->ifindex, loc, &optq);
if (ret)
return;
for (i = 0; i < optq.count; i++) {
NET_START_OBJECT;
NET_DUMP_STR("devname", "%s", dev->devname);
NET_DUMP_UINT("ifindex", "(%u)", dev->ifindex);
NET_DUMP_STR("kind", " %s", attach_loc_strings[loc]);
ret = __show_dev_tc_bpf_name(prog_ids[i], prog_name,
sizeof(prog_name));
if (!ret)
NET_DUMP_STR("name", " %s", prog_name);
NET_DUMP_UINT("prog_id", " prog id %u", prog_ids[i]);
if (prog_flags[i])
NET_DUMP_STR("prog_flags", "%s", flags_strings(prog_flags[i]));
if (link_ids[i])
NET_DUMP_UINT("link_id", " link id %u",
link_ids[i]);
if (link_flags[i])
NET_DUMP_STR("link_flags", "%s", flags_strings(link_flags[i]));
NET_END_OBJECT_FINAL;
}
}

static void show_dev_tc_bpf(struct ip_devname_ifindex *dev)
{
__show_dev_tc_bpf(dev, BPF_TCX_INGRESS);
__show_dev_tc_bpf(dev, BPF_TCX_EGRESS);
}

static int show_dev_tc_bpf_classic(int sock, unsigned int nl_pid,
struct ip_devname_ifindex *dev)
{
struct bpf_filter_t filter_info;
struct bpf_tcinfo_t tcinfo;
Expand Down Expand Up @@ -790,8 +873,9 @@ static int do_show(int argc, char **argv)
if (!ret) {
NET_START_ARRAY("tc", "%s:\n");
for (i = 0; i < dev_array.used_len; i++) {
ret = show_dev_tc_bpf(sock, nl_pid,
&dev_array.devices[i]);
show_dev_tc_bpf(&dev_array.devices[i]);
ret = show_dev_tc_bpf_classic(sock, nl_pid,
&dev_array.devices[i]);
if (ret)
break;
}
Expand Down

0 comments on commit eaddb47

Please sign in to comment.