blob: ef83e8a084908f643187739ce5c35058bd79c2ff [file] [log] [blame]
Yonghong Songf6f3bac2018-09-05 16:58:06 -07001// SPDX-License-Identifier: GPL-2.0+
2// Copyright (C) 2018 Facebook
3
4#define _GNU_SOURCE
Yonghong Song7900efc2018-09-17 16:13:00 -07005#include <errno.h>
Yonghong Songf6f3bac2018-09-05 16:58:06 -07006#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9#include <libbpf.h>
10#include <net/if.h>
11#include <linux/if.h>
12#include <linux/rtnetlink.h>
13#include <linux/tc_act/tc_bpf.h>
14#include <sys/socket.h>
15
16#include <bpf.h>
17#include <nlattr.h>
18#include "main.h"
19#include "netlink_dumper.h"
20
Yonghong Song7900efc2018-09-17 16:13:00 -070021struct ip_devname_ifindex {
22 char devname[64];
23 int ifindex;
24};
25
Yonghong Songf6f3bac2018-09-05 16:58:06 -070026struct bpf_netdev_t {
Yonghong Song7900efc2018-09-17 16:13:00 -070027 struct ip_devname_ifindex *devices;
Yonghong Songf6f3bac2018-09-05 16:58:06 -070028 int used_len;
29 int array_len;
30 int filter_idx;
31};
32
33struct tc_kind_handle {
34 char kind[64];
35 int handle;
36};
37
38struct bpf_tcinfo_t {
39 struct tc_kind_handle *handle_array;
40 int used_len;
41 int array_len;
42 bool is_qdisc;
43};
44
Yonghong Song7900efc2018-09-17 16:13:00 -070045struct bpf_filter_t {
46 const char *kind;
47 const char *devname;
48 int ifindex;
49};
50
Yonghong Songf6f3bac2018-09-05 16:58:06 -070051static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
52{
53 struct bpf_netdev_t *netinfo = cookie;
54 struct ifinfomsg *ifinfo = msg;
55
56 if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index)
57 return 0;
58
59 if (netinfo->used_len == netinfo->array_len) {
Yonghong Song7900efc2018-09-17 16:13:00 -070060 netinfo->devices = realloc(netinfo->devices,
61 (netinfo->array_len + 16) *
62 sizeof(struct ip_devname_ifindex));
63 if (!netinfo->devices)
64 return -ENOMEM;
65
Yonghong Songf6f3bac2018-09-05 16:58:06 -070066 netinfo->array_len += 16;
67 }
Yonghong Song7900efc2018-09-17 16:13:00 -070068 netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index;
69 snprintf(netinfo->devices[netinfo->used_len].devname,
70 sizeof(netinfo->devices[netinfo->used_len].devname),
71 "%s",
72 tb[IFLA_IFNAME] ? nla_getattr_str(tb[IFLA_IFNAME]) : "");
73 netinfo->used_len++;
Yonghong Songf6f3bac2018-09-05 16:58:06 -070074
75 return do_xdp_dump(ifinfo, tb);
76}
77
78static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
79{
80 struct bpf_tcinfo_t *tcinfo = cookie;
81 struct tcmsg *info = msg;
82
83 if (tcinfo->is_qdisc) {
84 /* skip clsact qdisc */
85 if (tb[TCA_KIND] &&
86 strcmp(nla_data(tb[TCA_KIND]), "clsact") == 0)
87 return 0;
88 if (info->tcm_handle == 0)
89 return 0;
90 }
91
92 if (tcinfo->used_len == tcinfo->array_len) {
93 tcinfo->handle_array = realloc(tcinfo->handle_array,
94 (tcinfo->array_len + 16) * sizeof(struct tc_kind_handle));
Yonghong Song7900efc2018-09-17 16:13:00 -070095 if (!tcinfo->handle_array)
96 return -ENOMEM;
97
Yonghong Songf6f3bac2018-09-05 16:58:06 -070098 tcinfo->array_len += 16;
99 }
100 tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle;
101 snprintf(tcinfo->handle_array[tcinfo->used_len].kind,
102 sizeof(tcinfo->handle_array[tcinfo->used_len].kind),
Yonghong Song7900efc2018-09-17 16:13:00 -0700103 "%s",
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700104 tb[TCA_KIND] ? nla_getattr_str(tb[TCA_KIND]) : "unknown");
105 tcinfo->used_len++;
106
107 return 0;
108}
109
110static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb)
111{
Yonghong Song7900efc2018-09-17 16:13:00 -0700112 const struct bpf_filter_t *filter_info = cookie;
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700113
Yonghong Song7900efc2018-09-17 16:13:00 -0700114 return do_filter_dump((struct tcmsg *)msg, tb, filter_info->kind,
115 filter_info->devname, filter_info->ifindex);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700116}
117
Yonghong Song7900efc2018-09-17 16:13:00 -0700118static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
119 struct ip_devname_ifindex *dev)
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700120{
Yonghong Song7900efc2018-09-17 16:13:00 -0700121 struct bpf_filter_t filter_info;
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700122 struct bpf_tcinfo_t tcinfo;
Yonghong Song7900efc2018-09-17 16:13:00 -0700123 int i, handle, ret = 0;
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700124
125 tcinfo.handle_array = NULL;
126 tcinfo.used_len = 0;
127 tcinfo.array_len = 0;
128
129 tcinfo.is_qdisc = false;
Andrey Ignatovaae57782018-10-03 15:26:39 -0700130 ret = libbpf_nl_get_class(sock, nl_pid, dev->ifindex,
131 dump_class_qdisc_nlmsg, &tcinfo);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700132 if (ret)
Yonghong Song7900efc2018-09-17 16:13:00 -0700133 goto out;
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700134
135 tcinfo.is_qdisc = true;
Andrey Ignatovaae57782018-10-03 15:26:39 -0700136 ret = libbpf_nl_get_qdisc(sock, nl_pid, dev->ifindex,
137 dump_class_qdisc_nlmsg, &tcinfo);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700138 if (ret)
Yonghong Song7900efc2018-09-17 16:13:00 -0700139 goto out;
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700140
Yonghong Song7900efc2018-09-17 16:13:00 -0700141 filter_info.devname = dev->devname;
142 filter_info.ifindex = dev->ifindex;
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700143 for (i = 0; i < tcinfo.used_len; i++) {
Yonghong Song7900efc2018-09-17 16:13:00 -0700144 filter_info.kind = tcinfo.handle_array[i].kind;
Andrey Ignatovaae57782018-10-03 15:26:39 -0700145 ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex,
146 tcinfo.handle_array[i].handle,
147 dump_filter_nlmsg, &filter_info);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700148 if (ret)
Yonghong Song7900efc2018-09-17 16:13:00 -0700149 goto out;
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700150 }
151
152 /* root, ingress and egress handle */
153 handle = TC_H_ROOT;
Yonghong Song7900efc2018-09-17 16:13:00 -0700154 filter_info.kind = "root";
Andrey Ignatovaae57782018-10-03 15:26:39 -0700155 ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
156 dump_filter_nlmsg, &filter_info);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700157 if (ret)
Yonghong Song7900efc2018-09-17 16:13:00 -0700158 goto out;
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700159
160 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
Yonghong Song7900efc2018-09-17 16:13:00 -0700161 filter_info.kind = "clsact/ingress";
Andrey Ignatovaae57782018-10-03 15:26:39 -0700162 ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
163 dump_filter_nlmsg, &filter_info);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700164 if (ret)
Yonghong Song7900efc2018-09-17 16:13:00 -0700165 goto out;
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700166
167 handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
Yonghong Song7900efc2018-09-17 16:13:00 -0700168 filter_info.kind = "clsact/egress";
Andrey Ignatovaae57782018-10-03 15:26:39 -0700169 ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
170 dump_filter_nlmsg, &filter_info);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700171 if (ret)
Yonghong Song7900efc2018-09-17 16:13:00 -0700172 goto out;
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700173
Yonghong Song7900efc2018-09-17 16:13:00 -0700174out:
175 free(tcinfo.handle_array);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700176 return 0;
177}
178
179static int do_show(int argc, char **argv)
180{
181 int i, sock, ret, filter_idx = -1;
182 struct bpf_netdev_t dev_array;
183 unsigned int nl_pid;
184 char err_buf[256];
185
186 if (argc == 2) {
187 if (strcmp(argv[0], "dev") != 0)
188 usage();
189 filter_idx = if_nametoindex(argv[1]);
190 if (filter_idx == 0) {
191 fprintf(stderr, "invalid dev name %s\n", argv[1]);
192 return -1;
193 }
194 } else if (argc != 0) {
195 usage();
196 }
197
Andrey Ignatovaae57782018-10-03 15:26:39 -0700198 sock = libbpf_netlink_open(&nl_pid);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700199 if (sock < 0) {
200 fprintf(stderr, "failed to open netlink sock\n");
201 return -1;
202 }
203
Yonghong Song7900efc2018-09-17 16:13:00 -0700204 dev_array.devices = NULL;
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700205 dev_array.used_len = 0;
206 dev_array.array_len = 0;
207 dev_array.filter_idx = filter_idx;
208
209 if (json_output)
210 jsonw_start_array(json_wtr);
211 NET_START_OBJECT;
Yonghong Song7900efc2018-09-17 16:13:00 -0700212 NET_START_ARRAY("xdp", "%s:\n");
Andrey Ignatovaae57782018-10-03 15:26:39 -0700213 ret = libbpf_nl_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700214 NET_END_ARRAY("\n");
215
216 if (!ret) {
Yonghong Song7900efc2018-09-17 16:13:00 -0700217 NET_START_ARRAY("tc", "%s:\n");
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700218 for (i = 0; i < dev_array.used_len; i++) {
219 ret = show_dev_tc_bpf(sock, nl_pid,
Yonghong Song7900efc2018-09-17 16:13:00 -0700220 &dev_array.devices[i]);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700221 if (ret)
222 break;
223 }
224 NET_END_ARRAY("\n");
225 }
226 NET_END_OBJECT;
227 if (json_output)
228 jsonw_end_array(json_wtr);
229
230 if (ret) {
231 if (json_output)
232 jsonw_null(json_wtr);
233 libbpf_strerror(ret, err_buf, sizeof(err_buf));
234 fprintf(stderr, "Error: %s\n", err_buf);
235 }
Yonghong Song7900efc2018-09-17 16:13:00 -0700236 free(dev_array.devices);
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700237 close(sock);
238 return ret;
239}
240
241static int do_help(int argc, char **argv)
242{
243 if (json_output) {
244 jsonw_null(json_wtr);
245 return 0;
246 }
247
248 fprintf(stderr,
249 "Usage: %s %s { show | list } [dev <devname>]\n"
Yonghong Song7900efc2018-09-17 16:13:00 -0700250 " %s %s help\n"
251 "Note: Only xdp and tc attachments are supported now.\n"
252 " For progs attached to cgroups, use \"bpftool cgroup\"\n"
253 " to dump program attachments. For program types\n"
254 " sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"
255 " consult iproute2.\n",
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700256 bin_name, argv[-2], bin_name, argv[-2]);
257
258 return 0;
259}
260
261static const struct cmd cmds[] = {
262 { "show", do_show },
263 { "list", do_show },
264 { "help", do_help },
265 { 0 }
266};
267
268int do_net(int argc, char **argv)
269{
270 return cmd_select(cmds, argc, argv, do_help);
271}