summaryrefslogtreecommitdiff
path: root/tools/bpf/bpftool/net.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/bpf/bpftool/net.c')
-rw-r--r--tools/bpf/bpftool/net.c103
1 files changed, 71 insertions, 32 deletions
diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
index 77dd73dd9ade..ed205ee57655 100644
--- a/tools/bpf/bpftool/net.c
+++ b/tools/bpf/bpftool/net.c
@@ -2,6 +2,7 @@
// Copyright (C) 2018 Facebook
#define _GNU_SOURCE
+#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@@ -17,8 +18,13 @@
#include "main.h"
#include "netlink_dumper.h"
+struct ip_devname_ifindex {
+ char devname[64];
+ int ifindex;
+};
+
struct bpf_netdev_t {
- int *ifindex_array;
+ struct ip_devname_ifindex *devices;
int used_len;
int array_len;
int filter_idx;
@@ -36,6 +42,12 @@ struct bpf_tcinfo_t {
bool is_qdisc;
};
+struct bpf_filter_t {
+ const char *kind;
+ const char *devname;
+ int ifindex;
+};
+
static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
{
struct bpf_netdev_t *netinfo = cookie;
@@ -45,11 +57,20 @@ static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
return 0;
if (netinfo->used_len == netinfo->array_len) {
- netinfo->ifindex_array = realloc(netinfo->ifindex_array,
- (netinfo->array_len + 16) * sizeof(int));
+ netinfo->devices = realloc(netinfo->devices,
+ (netinfo->array_len + 16) *
+ sizeof(struct ip_devname_ifindex));
+ if (!netinfo->devices)
+ return -ENOMEM;
+
netinfo->array_len += 16;
}
- netinfo->ifindex_array[netinfo->used_len++] = ifinfo->ifi_index;
+ netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index;
+ snprintf(netinfo->devices[netinfo->used_len].devname,
+ sizeof(netinfo->devices[netinfo->used_len].devname),
+ "%s",
+ tb[IFLA_IFNAME] ? nla_getattr_str(tb[IFLA_IFNAME]) : "");
+ netinfo->used_len++;
return do_xdp_dump(ifinfo, tb);
}
@@ -71,13 +92,15 @@ static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
if (tcinfo->used_len == tcinfo->array_len) {
tcinfo->handle_array = realloc(tcinfo->handle_array,
(tcinfo->array_len + 16) * sizeof(struct tc_kind_handle));
+ if (!tcinfo->handle_array)
+ return -ENOMEM;
+
tcinfo->array_len += 16;
}
tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle;
snprintf(tcinfo->handle_array[tcinfo->used_len].kind,
sizeof(tcinfo->handle_array[tcinfo->used_len].kind),
- "%s_%s",
- tcinfo->is_qdisc ? "qdisc" : "class",
+ "%s",
tb[TCA_KIND] ? nla_getattr_str(tb[TCA_KIND]) : "unknown");
tcinfo->used_len++;
@@ -86,60 +109,71 @@ static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb)
{
- const char *kind = cookie;
+ const struct bpf_filter_t *filter_info = cookie;
- return do_filter_dump((struct tcmsg *)msg, tb, kind);
+ return do_filter_dump((struct tcmsg *)msg, tb, filter_info->kind,
+ filter_info->devname, filter_info->ifindex);
}
-static int show_dev_tc_bpf(int sock, unsigned int nl_pid, int ifindex)
+static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
+ struct ip_devname_ifindex *dev)
{
+ struct bpf_filter_t filter_info;
struct bpf_tcinfo_t tcinfo;
- int i, handle, ret;
+ int i, handle, ret = 0;
tcinfo.handle_array = NULL;
tcinfo.used_len = 0;
tcinfo.array_len = 0;
tcinfo.is_qdisc = false;
- ret = nl_get_class(sock, nl_pid, ifindex, dump_class_qdisc_nlmsg,
+ ret = nl_get_class(sock, nl_pid, dev->ifindex, dump_class_qdisc_nlmsg,
&tcinfo);
if (ret)
- return ret;
+ goto out;
tcinfo.is_qdisc = true;
- ret = nl_get_qdisc(sock, nl_pid, ifindex, dump_class_qdisc_nlmsg,
+ ret = nl_get_qdisc(sock, nl_pid, dev->ifindex, dump_class_qdisc_nlmsg,
&tcinfo);
if (ret)
- return ret;
+ goto out;
+ filter_info.devname = dev->devname;
+ filter_info.ifindex = dev->ifindex;
for (i = 0; i < tcinfo.used_len; i++) {
- ret = nl_get_filter(sock, nl_pid, ifindex,
+ filter_info.kind = tcinfo.handle_array[i].kind;
+ ret = nl_get_filter(sock, nl_pid, dev->ifindex,
tcinfo.handle_array[i].handle,
dump_filter_nlmsg,
- tcinfo.handle_array[i].kind);
+ &filter_info);
if (ret)
- return ret;
+ goto out;
}
/* root, ingress and egress handle */
handle = TC_H_ROOT;
- ret = nl_get_filter(sock, nl_pid, ifindex, handle, dump_filter_nlmsg,
- "root");
+ filter_info.kind = "root";
+ ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle,
+ dump_filter_nlmsg, &filter_info);
if (ret)
- return ret;
+ goto out;
handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
- ret = nl_get_filter(sock, nl_pid, ifindex, handle, dump_filter_nlmsg,
- "qdisc_clsact_ingress");
+ filter_info.kind = "clsact/ingress";
+ ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle,
+ dump_filter_nlmsg, &filter_info);
if (ret)
- return ret;
+ goto out;
handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
- ret = nl_get_filter(sock, nl_pid, ifindex, handle, dump_filter_nlmsg,
- "qdisc_clsact_egress");
+ filter_info.kind = "clsact/egress";
+ ret = nl_get_filter(sock, nl_pid, dev->ifindex, handle,
+ dump_filter_nlmsg, &filter_info);
if (ret)
- return ret;
+ goto out;
+out:
+ free(tcinfo.handle_array);
return 0;
}
@@ -168,7 +202,7 @@ static int do_show(int argc, char **argv)
return -1;
}
- dev_array.ifindex_array = NULL;
+ dev_array.devices = NULL;
dev_array.used_len = 0;
dev_array.array_len = 0;
dev_array.filter_idx = filter_idx;
@@ -176,15 +210,15 @@ static int do_show(int argc, char **argv)
if (json_output)
jsonw_start_array(json_wtr);
NET_START_OBJECT;
- NET_START_ARRAY("xdp", "\n");
+ NET_START_ARRAY("xdp", "%s:\n");
ret = nl_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
NET_END_ARRAY("\n");
if (!ret) {
- NET_START_ARRAY("tc_filters", "\n");
+ 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.ifindex_array[i]);
+ &dev_array.devices[i]);
if (ret)
break;
}
@@ -200,7 +234,7 @@ static int do_show(int argc, char **argv)
libbpf_strerror(ret, err_buf, sizeof(err_buf));
fprintf(stderr, "Error: %s\n", err_buf);
}
- free(dev_array.ifindex_array);
+ free(dev_array.devices);
close(sock);
return ret;
}
@@ -214,7 +248,12 @@ static int do_help(int argc, char **argv)
fprintf(stderr,
"Usage: %s %s { show | list } [dev <devname>]\n"
- " %s %s help\n",
+ " %s %s help\n"
+ "Note: Only xdp and tc attachments are supported now.\n"
+ " For progs attached to cgroups, use \"bpftool cgroup\"\n"
+ " to dump program attachments. For program types\n"
+ " sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"
+ " consult iproute2.\n",
bin_name, argv[-2], bin_name, argv[-2]);
return 0;