1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 // Copyright (C) 2018 Facebook
12 #include <bpf/libbpf.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/socket.h>
16 #include <linux/tc_act/tc_bpf.h>
17 #include <sys/socket.h>
19 #include <sys/types.h>
21 #include "bpf/nlattr.h"
23 #include "netlink_dumper.h"
26 #define SOL_NETLINK 270
29 struct ip_devname_ifindex
{
35 struct ip_devname_ifindex
*devices
;
41 struct tc_kind_handle
{
47 struct tc_kind_handle
*handle_array
;
59 struct bpf_attach_info
{
60 __u32 flow_dissector_id
;
63 enum net_attach_type
{
65 NET_ATTACH_TYPE_XDP_GENERIC
,
66 NET_ATTACH_TYPE_XDP_DRIVER
,
67 NET_ATTACH_TYPE_XDP_OFFLOAD
,
70 static const char * const attach_type_strings
[] = {
71 [NET_ATTACH_TYPE_XDP
] = "xdp",
72 [NET_ATTACH_TYPE_XDP_GENERIC
] = "xdpgeneric",
73 [NET_ATTACH_TYPE_XDP_DRIVER
] = "xdpdrv",
74 [NET_ATTACH_TYPE_XDP_OFFLOAD
] = "xdpoffload",
77 const size_t net_attach_type_size
= ARRAY_SIZE(attach_type_strings
);
79 static enum net_attach_type
parse_attach_type(const char *str
)
81 enum net_attach_type type
;
83 for (type
= 0; type
< net_attach_type_size
; type
++) {
84 if (attach_type_strings
[type
] &&
85 is_prefix(str
, attach_type_strings
[type
]))
89 return net_attach_type_size
;
92 typedef int (*dump_nlmsg_t
)(void *cookie
, void *msg
, struct nlattr
**tb
);
94 typedef int (*__dump_nlmsg_t
)(struct nlmsghdr
*nlmsg
, dump_nlmsg_t
, void *cookie
);
96 static int netlink_open(__u32
*nl_pid
)
98 struct sockaddr_nl sa
;
103 memset(&sa
, 0, sizeof(sa
));
104 sa
.nl_family
= AF_NETLINK
;
106 sock
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
110 if (setsockopt(sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
111 &one
, sizeof(one
)) < 0) {
112 p_err("Netlink error reporting not supported");
115 if (bind(sock
, (struct sockaddr
*)&sa
, sizeof(sa
)) < 0) {
120 addrlen
= sizeof(sa
);
121 if (getsockname(sock
, (struct sockaddr
*)&sa
, &addrlen
) < 0) {
126 if (addrlen
!= sizeof(sa
)) {
127 ret
= -LIBBPF_ERRNO__INTERNAL
;
139 static int netlink_recv(int sock
, __u32 nl_pid
, __u32 seq
,
140 __dump_nlmsg_t _fn
, dump_nlmsg_t fn
,
143 bool multipart
= true;
144 struct nlmsgerr
*err
;
151 len
= recv(sock
, buf
, sizeof(buf
), 0);
160 for (nh
= (struct nlmsghdr
*)buf
; NLMSG_OK(nh
, len
);
161 nh
= NLMSG_NEXT(nh
, len
)) {
162 if (nh
->nlmsg_pid
!= nl_pid
) {
163 ret
= -LIBBPF_ERRNO__WRNGPID
;
166 if (nh
->nlmsg_seq
!= seq
) {
167 ret
= -LIBBPF_ERRNO__INVSEQ
;
170 if (nh
->nlmsg_flags
& NLM_F_MULTI
)
172 switch (nh
->nlmsg_type
) {
174 err
= (struct nlmsgerr
*)NLMSG_DATA(nh
);
178 libbpf_nla_dump_errormsg(nh
);
186 ret
= _fn(nh
, fn
, cookie
);
197 static int __dump_class_nlmsg(struct nlmsghdr
*nlh
,
198 dump_nlmsg_t dump_class_nlmsg
,
201 struct nlattr
*tb
[TCA_MAX
+ 1], *attr
;
202 struct tcmsg
*t
= NLMSG_DATA(nlh
);
205 len
= nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*t
));
206 attr
= (struct nlattr
*) ((void *) t
+ NLMSG_ALIGN(sizeof(*t
)));
207 if (libbpf_nla_parse(tb
, TCA_MAX
, attr
, len
, NULL
) != 0)
208 return -LIBBPF_ERRNO__NLPARSE
;
210 return dump_class_nlmsg(cookie
, t
, tb
);
213 static int netlink_get_class(int sock
, unsigned int nl_pid
, int ifindex
,
214 dump_nlmsg_t dump_class_nlmsg
, void *cookie
)
220 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
221 .nlh
.nlmsg_type
= RTM_GETTCLASS
,
222 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
223 .t
.tcm_family
= AF_UNSPEC
,
224 .t
.tcm_ifindex
= ifindex
,
226 int seq
= time(NULL
);
228 req
.nlh
.nlmsg_seq
= seq
;
229 if (send(sock
, &req
, req
.nlh
.nlmsg_len
, 0) < 0)
232 return netlink_recv(sock
, nl_pid
, seq
, __dump_class_nlmsg
,
233 dump_class_nlmsg
, cookie
);
236 static int __dump_qdisc_nlmsg(struct nlmsghdr
*nlh
,
237 dump_nlmsg_t dump_qdisc_nlmsg
,
240 struct nlattr
*tb
[TCA_MAX
+ 1], *attr
;
241 struct tcmsg
*t
= NLMSG_DATA(nlh
);
244 len
= nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*t
));
245 attr
= (struct nlattr
*) ((void *) t
+ NLMSG_ALIGN(sizeof(*t
)));
246 if (libbpf_nla_parse(tb
, TCA_MAX
, attr
, len
, NULL
) != 0)
247 return -LIBBPF_ERRNO__NLPARSE
;
249 return dump_qdisc_nlmsg(cookie
, t
, tb
);
252 static int netlink_get_qdisc(int sock
, unsigned int nl_pid
, int ifindex
,
253 dump_nlmsg_t dump_qdisc_nlmsg
, void *cookie
)
259 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
260 .nlh
.nlmsg_type
= RTM_GETQDISC
,
261 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
262 .t
.tcm_family
= AF_UNSPEC
,
263 .t
.tcm_ifindex
= ifindex
,
265 int seq
= time(NULL
);
267 req
.nlh
.nlmsg_seq
= seq
;
268 if (send(sock
, &req
, req
.nlh
.nlmsg_len
, 0) < 0)
271 return netlink_recv(sock
, nl_pid
, seq
, __dump_qdisc_nlmsg
,
272 dump_qdisc_nlmsg
, cookie
);
275 static int __dump_filter_nlmsg(struct nlmsghdr
*nlh
,
276 dump_nlmsg_t dump_filter_nlmsg
,
279 struct nlattr
*tb
[TCA_MAX
+ 1], *attr
;
280 struct tcmsg
*t
= NLMSG_DATA(nlh
);
283 len
= nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*t
));
284 attr
= (struct nlattr
*) ((void *) t
+ NLMSG_ALIGN(sizeof(*t
)));
285 if (libbpf_nla_parse(tb
, TCA_MAX
, attr
, len
, NULL
) != 0)
286 return -LIBBPF_ERRNO__NLPARSE
;
288 return dump_filter_nlmsg(cookie
, t
, tb
);
291 static int netlink_get_filter(int sock
, unsigned int nl_pid
, int ifindex
, int handle
,
292 dump_nlmsg_t dump_filter_nlmsg
, void *cookie
)
298 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
299 .nlh
.nlmsg_type
= RTM_GETTFILTER
,
300 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
301 .t
.tcm_family
= AF_UNSPEC
,
302 .t
.tcm_ifindex
= ifindex
,
303 .t
.tcm_parent
= handle
,
305 int seq
= time(NULL
);
307 req
.nlh
.nlmsg_seq
= seq
;
308 if (send(sock
, &req
, req
.nlh
.nlmsg_len
, 0) < 0)
311 return netlink_recv(sock
, nl_pid
, seq
, __dump_filter_nlmsg
,
312 dump_filter_nlmsg
, cookie
);
315 static int __dump_link_nlmsg(struct nlmsghdr
*nlh
,
316 dump_nlmsg_t dump_link_nlmsg
, void *cookie
)
318 struct nlattr
*tb
[IFLA_MAX
+ 1], *attr
;
319 struct ifinfomsg
*ifi
= NLMSG_DATA(nlh
);
322 len
= nlh
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
323 attr
= (struct nlattr
*) ((void *) ifi
+ NLMSG_ALIGN(sizeof(*ifi
)));
324 if (libbpf_nla_parse(tb
, IFLA_MAX
, attr
, len
, NULL
) != 0)
325 return -LIBBPF_ERRNO__NLPARSE
;
327 return dump_link_nlmsg(cookie
, ifi
, tb
);
330 static int netlink_get_link(int sock
, unsigned int nl_pid
,
331 dump_nlmsg_t dump_link_nlmsg
, void *cookie
)
335 struct ifinfomsg ifm
;
337 .nlh
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
338 .nlh
.nlmsg_type
= RTM_GETLINK
,
339 .nlh
.nlmsg_flags
= NLM_F_DUMP
| NLM_F_REQUEST
,
340 .ifm
.ifi_family
= AF_PACKET
,
342 int seq
= time(NULL
);
344 req
.nlh
.nlmsg_seq
= seq
;
345 if (send(sock
, &req
, req
.nlh
.nlmsg_len
, 0) < 0)
348 return netlink_recv(sock
, nl_pid
, seq
, __dump_link_nlmsg
,
349 dump_link_nlmsg
, cookie
);
352 static int dump_link_nlmsg(void *cookie
, void *msg
, struct nlattr
**tb
)
354 struct bpf_netdev_t
*netinfo
= cookie
;
355 struct ifinfomsg
*ifinfo
= msg
;
357 if (netinfo
->filter_idx
> 0 && netinfo
->filter_idx
!= ifinfo
->ifi_index
)
360 if (netinfo
->used_len
== netinfo
->array_len
) {
361 netinfo
->devices
= realloc(netinfo
->devices
,
362 (netinfo
->array_len
+ 16) *
363 sizeof(struct ip_devname_ifindex
));
364 if (!netinfo
->devices
)
367 netinfo
->array_len
+= 16;
369 netinfo
->devices
[netinfo
->used_len
].ifindex
= ifinfo
->ifi_index
;
370 snprintf(netinfo
->devices
[netinfo
->used_len
].devname
,
371 sizeof(netinfo
->devices
[netinfo
->used_len
].devname
),
374 ? libbpf_nla_getattr_str(tb
[IFLA_IFNAME
])
378 return do_xdp_dump(ifinfo
, tb
);
381 static int dump_class_qdisc_nlmsg(void *cookie
, void *msg
, struct nlattr
**tb
)
383 struct bpf_tcinfo_t
*tcinfo
= cookie
;
384 struct tcmsg
*info
= msg
;
386 if (tcinfo
->is_qdisc
) {
387 /* skip clsact qdisc */
389 strcmp(libbpf_nla_data(tb
[TCA_KIND
]), "clsact") == 0)
391 if (info
->tcm_handle
== 0)
395 if (tcinfo
->used_len
== tcinfo
->array_len
) {
396 tcinfo
->handle_array
= realloc(tcinfo
->handle_array
,
397 (tcinfo
->array_len
+ 16) * sizeof(struct tc_kind_handle
));
398 if (!tcinfo
->handle_array
)
401 tcinfo
->array_len
+= 16;
403 tcinfo
->handle_array
[tcinfo
->used_len
].handle
= info
->tcm_handle
;
404 snprintf(tcinfo
->handle_array
[tcinfo
->used_len
].kind
,
405 sizeof(tcinfo
->handle_array
[tcinfo
->used_len
].kind
),
408 ? libbpf_nla_getattr_str(tb
[TCA_KIND
])
415 static int dump_filter_nlmsg(void *cookie
, void *msg
, struct nlattr
**tb
)
417 const struct bpf_filter_t
*filter_info
= cookie
;
419 return do_filter_dump((struct tcmsg
*)msg
, tb
, filter_info
->kind
,
420 filter_info
->devname
, filter_info
->ifindex
);
423 static int show_dev_tc_bpf(int sock
, unsigned int nl_pid
,
424 struct ip_devname_ifindex
*dev
)
426 struct bpf_filter_t filter_info
;
427 struct bpf_tcinfo_t tcinfo
;
428 int i
, handle
, ret
= 0;
430 tcinfo
.handle_array
= NULL
;
432 tcinfo
.array_len
= 0;
434 tcinfo
.is_qdisc
= false;
435 ret
= netlink_get_class(sock
, nl_pid
, dev
->ifindex
,
436 dump_class_qdisc_nlmsg
, &tcinfo
);
440 tcinfo
.is_qdisc
= true;
441 ret
= netlink_get_qdisc(sock
, nl_pid
, dev
->ifindex
,
442 dump_class_qdisc_nlmsg
, &tcinfo
);
446 filter_info
.devname
= dev
->devname
;
447 filter_info
.ifindex
= dev
->ifindex
;
448 for (i
= 0; i
< tcinfo
.used_len
; i
++) {
449 filter_info
.kind
= tcinfo
.handle_array
[i
].kind
;
450 ret
= netlink_get_filter(sock
, nl_pid
, dev
->ifindex
,
451 tcinfo
.handle_array
[i
].handle
,
452 dump_filter_nlmsg
, &filter_info
);
457 /* root, ingress and egress handle */
459 filter_info
.kind
= "root";
460 ret
= netlink_get_filter(sock
, nl_pid
, dev
->ifindex
, handle
,
461 dump_filter_nlmsg
, &filter_info
);
465 handle
= TC_H_MAKE(TC_H_CLSACT
, TC_H_MIN_INGRESS
);
466 filter_info
.kind
= "clsact/ingress";
467 ret
= netlink_get_filter(sock
, nl_pid
, dev
->ifindex
, handle
,
468 dump_filter_nlmsg
, &filter_info
);
472 handle
= TC_H_MAKE(TC_H_CLSACT
, TC_H_MIN_EGRESS
);
473 filter_info
.kind
= "clsact/egress";
474 ret
= netlink_get_filter(sock
, nl_pid
, dev
->ifindex
, handle
,
475 dump_filter_nlmsg
, &filter_info
);
480 free(tcinfo
.handle_array
);
484 static int query_flow_dissector(struct bpf_attach_info
*attach_info
)
492 fd
= open("/proc/self/ns/net", O_RDONLY
);
494 p_err("can't open /proc/self/ns/net: %s",
498 prog_cnt
= ARRAY_SIZE(prog_ids
);
499 err
= bpf_prog_query(fd
, BPF_FLOW_DISSECTOR
, 0,
500 &attach_flags
, prog_ids
, &prog_cnt
);
503 if (errno
== EINVAL
) {
504 /* Older kernel's don't support querying
505 * flow dissector programs.
510 p_err("can't query prog: %s", strerror(errno
));
515 attach_info
->flow_dissector_id
= prog_ids
[0];
520 static int net_parse_dev(int *argc
, char ***argv
)
524 if (is_prefix(**argv
, "dev")) {
527 ifindex
= if_nametoindex(**argv
);
529 p_err("invalid devname %s", **argv
);
533 p_err("expected 'dev', got: '%s'?", **argv
);
540 static int do_attach_detach_xdp(int progfd
, enum net_attach_type attach_type
,
541 int ifindex
, bool overwrite
)
546 flags
= XDP_FLAGS_UPDATE_IF_NOEXIST
;
547 if (attach_type
== NET_ATTACH_TYPE_XDP_GENERIC
)
548 flags
|= XDP_FLAGS_SKB_MODE
;
549 if (attach_type
== NET_ATTACH_TYPE_XDP_DRIVER
)
550 flags
|= XDP_FLAGS_DRV_MODE
;
551 if (attach_type
== NET_ATTACH_TYPE_XDP_OFFLOAD
)
552 flags
|= XDP_FLAGS_HW_MODE
;
554 return bpf_set_link_xdp_fd(ifindex
, progfd
, flags
);
557 static int do_attach(int argc
, char **argv
)
559 enum net_attach_type attach_type
;
560 int progfd
, ifindex
, err
= 0;
561 bool overwrite
= false;
563 /* parse attach args */
567 attach_type
= parse_attach_type(*argv
);
568 if (attach_type
== net_attach_type_size
) {
569 p_err("invalid net attach/detach type: %s", *argv
);
574 progfd
= prog_parse_fd(&argc
, &argv
);
578 ifindex
= net_parse_dev(&argc
, &argv
);
585 if (is_prefix(*argv
, "overwrite")) {
588 p_err("expected 'overwrite', got: '%s'?", *argv
);
594 /* attach xdp prog */
595 if (is_prefix("xdp", attach_type_strings
[attach_type
]))
596 err
= do_attach_detach_xdp(progfd
, attach_type
, ifindex
,
599 p_err("interface %s attach failed: %s",
600 attach_type_strings
[attach_type
], strerror(-err
));
605 jsonw_null(json_wtr
);
611 static int do_detach(int argc
, char **argv
)
613 enum net_attach_type attach_type
;
614 int progfd
, ifindex
, err
= 0;
616 /* parse detach args */
620 attach_type
= parse_attach_type(*argv
);
621 if (attach_type
== net_attach_type_size
) {
622 p_err("invalid net attach/detach type: %s", *argv
);
627 ifindex
= net_parse_dev(&argc
, &argv
);
631 /* detach xdp prog */
633 if (is_prefix("xdp", attach_type_strings
[attach_type
]))
634 err
= do_attach_detach_xdp(progfd
, attach_type
, ifindex
, NULL
);
637 p_err("interface %s detach failed: %s",
638 attach_type_strings
[attach_type
], strerror(-err
));
643 jsonw_null(json_wtr
);
648 static int do_show(int argc
, char **argv
)
650 struct bpf_attach_info attach_info
= {};
651 int i
, sock
, ret
, filter_idx
= -1;
652 struct bpf_netdev_t dev_array
;
653 unsigned int nl_pid
= 0;
657 filter_idx
= net_parse_dev(&argc
, &argv
);
660 } else if (argc
!= 0) {
664 ret
= query_flow_dissector(&attach_info
);
668 sock
= netlink_open(&nl_pid
);
670 fprintf(stderr
, "failed to open netlink sock\n");
674 dev_array
.devices
= NULL
;
675 dev_array
.used_len
= 0;
676 dev_array
.array_len
= 0;
677 dev_array
.filter_idx
= filter_idx
;
680 jsonw_start_array(json_wtr
);
682 NET_START_ARRAY("xdp", "%s:\n");
683 ret
= netlink_get_link(sock
, nl_pid
, dump_link_nlmsg
, &dev_array
);
687 NET_START_ARRAY("tc", "%s:\n");
688 for (i
= 0; i
< dev_array
.used_len
; i
++) {
689 ret
= show_dev_tc_bpf(sock
, nl_pid
,
690 &dev_array
.devices
[i
]);
697 NET_START_ARRAY("flow_dissector", "%s:\n");
698 if (attach_info
.flow_dissector_id
> 0)
699 NET_DUMP_UINT("id", "id %u", attach_info
.flow_dissector_id
);
704 jsonw_end_array(json_wtr
);
708 jsonw_null(json_wtr
);
709 libbpf_strerror(ret
, err_buf
, sizeof(err_buf
));
710 fprintf(stderr
, "Error: %s\n", err_buf
);
712 free(dev_array
.devices
);
717 static int do_help(int argc
, char **argv
)
720 jsonw_null(json_wtr
);
725 "Usage: %1$s %2$s { show | list } [dev <devname>]\n"
726 " %1$s %2$s attach ATTACH_TYPE PROG dev <devname> [ overwrite ]\n"
727 " %1$s %2$s detach ATTACH_TYPE dev <devname>\n"
730 " " HELP_SPEC_PROGRAM
"\n"
731 " ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n"
733 "Note: Only xdp and tc attachments are supported now.\n"
734 " For progs attached to cgroups, use \"bpftool cgroup\"\n"
735 " to dump program attachments. For program types\n"
736 " sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"
737 " consult iproute2.\n"
744 static const struct cmd cmds
[] = {
747 { "attach", do_attach
},
748 { "detach", do_detach
},
753 int do_net(int argc
, char **argv
)
755 return cmd_select(cmds
, argc
, argv
, do_help
);