1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 // Copyright (C) 2018 Facebook
12 #include <linux/rtnetlink.h>
13 #include <linux/tc_act/tc_bpf.h>
14 #include <sys/socket.h>
19 #include "netlink_dumper.h"
21 struct ip_devname_ifindex
{
27 struct ip_devname_ifindex
*devices
;
33 struct tc_kind_handle
{
39 struct tc_kind_handle
*handle_array
;
51 static int dump_link_nlmsg(void *cookie
, void *msg
, struct nlattr
**tb
)
53 struct bpf_netdev_t
*netinfo
= cookie
;
54 struct ifinfomsg
*ifinfo
= msg
;
56 if (netinfo
->filter_idx
> 0 && netinfo
->filter_idx
!= ifinfo
->ifi_index
)
59 if (netinfo
->used_len
== netinfo
->array_len
) {
60 netinfo
->devices
= realloc(netinfo
->devices
,
61 (netinfo
->array_len
+ 16) *
62 sizeof(struct ip_devname_ifindex
));
63 if (!netinfo
->devices
)
66 netinfo
->array_len
+= 16;
68 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
),
73 ? libbpf_nla_getattr_str(tb
[IFLA_IFNAME
])
77 return do_xdp_dump(ifinfo
, tb
);
80 static int dump_class_qdisc_nlmsg(void *cookie
, void *msg
, struct nlattr
**tb
)
82 struct bpf_tcinfo_t
*tcinfo
= cookie
;
83 struct tcmsg
*info
= msg
;
85 if (tcinfo
->is_qdisc
) {
86 /* skip clsact qdisc */
88 strcmp(libbpf_nla_data(tb
[TCA_KIND
]), "clsact") == 0)
90 if (info
->tcm_handle
== 0)
94 if (tcinfo
->used_len
== tcinfo
->array_len
) {
95 tcinfo
->handle_array
= realloc(tcinfo
->handle_array
,
96 (tcinfo
->array_len
+ 16) * sizeof(struct tc_kind_handle
));
97 if (!tcinfo
->handle_array
)
100 tcinfo
->array_len
+= 16;
102 tcinfo
->handle_array
[tcinfo
->used_len
].handle
= info
->tcm_handle
;
103 snprintf(tcinfo
->handle_array
[tcinfo
->used_len
].kind
,
104 sizeof(tcinfo
->handle_array
[tcinfo
->used_len
].kind
),
107 ? libbpf_nla_getattr_str(tb
[TCA_KIND
])
114 static int dump_filter_nlmsg(void *cookie
, void *msg
, struct nlattr
**tb
)
116 const struct bpf_filter_t
*filter_info
= cookie
;
118 return do_filter_dump((struct tcmsg
*)msg
, tb
, filter_info
->kind
,
119 filter_info
->devname
, filter_info
->ifindex
);
122 static int show_dev_tc_bpf(int sock
, unsigned int nl_pid
,
123 struct ip_devname_ifindex
*dev
)
125 struct bpf_filter_t filter_info
;
126 struct bpf_tcinfo_t tcinfo
;
127 int i
, handle
, ret
= 0;
129 tcinfo
.handle_array
= NULL
;
131 tcinfo
.array_len
= 0;
133 tcinfo
.is_qdisc
= false;
134 ret
= libbpf_nl_get_class(sock
, nl_pid
, dev
->ifindex
,
135 dump_class_qdisc_nlmsg
, &tcinfo
);
139 tcinfo
.is_qdisc
= true;
140 ret
= libbpf_nl_get_qdisc(sock
, nl_pid
, dev
->ifindex
,
141 dump_class_qdisc_nlmsg
, &tcinfo
);
145 filter_info
.devname
= dev
->devname
;
146 filter_info
.ifindex
= dev
->ifindex
;
147 for (i
= 0; i
< tcinfo
.used_len
; i
++) {
148 filter_info
.kind
= tcinfo
.handle_array
[i
].kind
;
149 ret
= libbpf_nl_get_filter(sock
, nl_pid
, dev
->ifindex
,
150 tcinfo
.handle_array
[i
].handle
,
151 dump_filter_nlmsg
, &filter_info
);
156 /* root, ingress and egress handle */
158 filter_info
.kind
= "root";
159 ret
= libbpf_nl_get_filter(sock
, nl_pid
, dev
->ifindex
, handle
,
160 dump_filter_nlmsg
, &filter_info
);
164 handle
= TC_H_MAKE(TC_H_CLSACT
, TC_H_MIN_INGRESS
);
165 filter_info
.kind
= "clsact/ingress";
166 ret
= libbpf_nl_get_filter(sock
, nl_pid
, dev
->ifindex
, handle
,
167 dump_filter_nlmsg
, &filter_info
);
171 handle
= TC_H_MAKE(TC_H_CLSACT
, TC_H_MIN_EGRESS
);
172 filter_info
.kind
= "clsact/egress";
173 ret
= libbpf_nl_get_filter(sock
, nl_pid
, dev
->ifindex
, handle
,
174 dump_filter_nlmsg
, &filter_info
);
179 free(tcinfo
.handle_array
);
183 static int do_show(int argc
, char **argv
)
185 int i
, sock
, ret
, filter_idx
= -1;
186 struct bpf_netdev_t dev_array
;
191 if (strcmp(argv
[0], "dev") != 0)
193 filter_idx
= if_nametoindex(argv
[1]);
194 if (filter_idx
== 0) {
195 fprintf(stderr
, "invalid dev name %s\n", argv
[1]);
198 } else if (argc
!= 0) {
202 sock
= libbpf_netlink_open(&nl_pid
);
204 fprintf(stderr
, "failed to open netlink sock\n");
208 dev_array
.devices
= NULL
;
209 dev_array
.used_len
= 0;
210 dev_array
.array_len
= 0;
211 dev_array
.filter_idx
= filter_idx
;
214 jsonw_start_array(json_wtr
);
216 NET_START_ARRAY("xdp", "%s:\n");
217 ret
= libbpf_nl_get_link(sock
, nl_pid
, dump_link_nlmsg
, &dev_array
);
221 NET_START_ARRAY("tc", "%s:\n");
222 for (i
= 0; i
< dev_array
.used_len
; i
++) {
223 ret
= show_dev_tc_bpf(sock
, nl_pid
,
224 &dev_array
.devices
[i
]);
232 jsonw_end_array(json_wtr
);
236 jsonw_null(json_wtr
);
237 libbpf_strerror(ret
, err_buf
, sizeof(err_buf
));
238 fprintf(stderr
, "Error: %s\n", err_buf
);
240 free(dev_array
.devices
);
245 static int do_help(int argc
, char **argv
)
248 jsonw_null(json_wtr
);
253 "Usage: %s %s { show | list } [dev <devname>]\n"
255 "Note: Only xdp and tc attachments are supported now.\n"
256 " For progs attached to cgroups, use \"bpftool cgroup\"\n"
257 " to dump program attachments. For program types\n"
258 " sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"
259 " consult iproute2.\n",
260 bin_name
, argv
[-2], bin_name
, argv
[-2]);
265 static const struct cmd cmds
[] = {
272 int do_net(int argc
, char **argv
)
274 return cmd_select(cmds
, argc
, argv
, do_help
);