summaryrefslogtreecommitdiff
path: root/tools/bpf/bpftool/net.c
diff options
context:
space:
mode:
authorYonghong Song <yhs@fb.com>2018-09-17 16:13:00 -0700
committerDaniel Borkmann <daniel@iogearbox.net>2018-09-18 17:42:31 +0200
commit7900efc19214e326913dc0f0e8ded24adc0018f2 (patch)
tree9dcb527a1ec501454909874b3c4c410c76f61c54 /tools/bpf/bpftool/net.c
parent70e88c758a6b8544b5e0d982e55d1e36f9aa0b85 (diff)
tools/bpf: bpftool: improve output format for bpftool net
This is a followup patch for Commit f6f3bac08ff9 ("tools/bpf: bpftool: add net support"). Some improvements are made for the bpftool net output. Specially, plain output is more concise such that per attachment should nicely fit in one line. Compared to previous output, the prog tag is removed since it can be easily obtained with program id. Similar to xdp attachments, the device name is added to tc attachments. The bpf program attached through shared block mechanism is supported as well. $ ip link add dev v1 type veth peer name v2 $ tc qdisc add dev v1 ingress_block 10 egress_block 20 clsact $ tc qdisc add dev v2 ingress_block 10 egress_block 20 clsact $ tc filter add block 10 protocol ip prio 25 bpf obj bpf_shared.o sec ingress flowid 1:1 $ tc filter add block 20 protocol ip prio 30 bpf obj bpf_cyclic.o sec classifier flowid 1:1 $ bpftool net xdp: tc: v2(7) clsact/ingress bpf_shared.o:[ingress] id 23 v2(7) clsact/egress bpf_cyclic.o:[classifier] id 24 v1(8) clsact/ingress bpf_shared.o:[ingress] id 23 v1(8) clsact/egress bpf_cyclic.o:[classifier] id 24 The documentation and "bpftool net help" are updated to make it clear that current implementation only supports xdp and tc attachments. For programs attached to cgroups, "bpftool cgroup" can be used to dump attachments. For other programs e.g. sk_{filter,skb,msg,reuseport} and lwt/seg6, iproute2 tools should be used. The new output: $ bpftool net xdp: eth0(2) driver id 198 tc: eth0(2) clsact/ingress fbflow_icmp id 335 act [{icmp_action id 336}] eth0(2) clsact/egress fbflow_egress id 334 $ bpftool -jp net [{ "xdp": [{ "devname": "eth0", "ifindex": 2, "mode": "driver", "id": 198 } ], "tc": [{ "devname": "eth0", "ifindex": 2, "kind": "clsact/ingress", "name": "fbflow_icmp", "id": 335, "act": [{ "name": "icmp_action", "id": 336 } ] },{ "devname": "eth0", "ifindex": 2, "kind": "clsact/egress", "name": "fbflow_egress", "id": 334 } ] } ] Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
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;