aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf/bpf.c
diff options
context:
space:
mode:
authorEric Leblond <eric@regit.org>2018-01-30 15:55:02 -0500
committerAlexei Starovoitov <ast@kernel.org>2018-02-02 20:53:48 -0500
commitbbf48c18ee0cd18b53712aa09aefa29b64b3976e (patch)
treeb8cb67b2922bdd88e43371158a0094fcf09129f3 /tools/lib/bpf/bpf.c
parent949abbe88436c000cc63fce2bdfeb48b7d06a7df (diff)
libbpf: add error reporting in XDP
Parse netlink ext attribute to get the error message returned by the card. Code is partially take from libnl. We add netlink.h to the uapi include of tools. And we need to avoid include of userspace netlink header to have a successful build of sample so nlattr.h has a define to avoid the inclusion. Using a direct define could have been an issue as NLMSGERR_ATTR_MAX can change in the future. We also define SOL_NETLINK if not defined to avoid to have to copy socket.h for a fixed value. Signed-off-by: Eric Leblond <eric@regit.org> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'tools/lib/bpf/bpf.c')
-rw-r--r--tools/lib/bpf/bpf.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index bf2772566240..9c88f6e4156d 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -32,6 +32,10 @@
32#include <sys/socket.h> 32#include <sys/socket.h>
33#include <errno.h> 33#include <errno.h>
34 34
35#ifndef SOL_NETLINK
36#define SOL_NETLINK 270
37#endif
38
35/* 39/*
36 * When building perf, unistd.h is overridden. __NR_bpf is 40 * When building perf, unistd.h is overridden. __NR_bpf is
37 * required to be defined explicitly. 41 * required to be defined explicitly.
@@ -436,6 +440,7 @@ int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
436 struct nlmsghdr *nh; 440 struct nlmsghdr *nh;
437 struct nlmsgerr *err; 441 struct nlmsgerr *err;
438 socklen_t addrlen; 442 socklen_t addrlen;
443 int one = 1;
439 444
440 memset(&sa, 0, sizeof(sa)); 445 memset(&sa, 0, sizeof(sa));
441 sa.nl_family = AF_NETLINK; 446 sa.nl_family = AF_NETLINK;
@@ -445,6 +450,11 @@ int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
445 return -errno; 450 return -errno;
446 } 451 }
447 452
453 if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
454 &one, sizeof(one)) < 0) {
455 fprintf(stderr, "Netlink error reporting not supported\n");
456 }
457
448 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) { 458 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
449 ret = -errno; 459 ret = -errno;
450 goto cleanup; 460 goto cleanup;
@@ -521,6 +531,7 @@ int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
521 if (!err->error) 531 if (!err->error)
522 continue; 532 continue;
523 ret = err->error; 533 ret = err->error;
534 nla_dump_errormsg(nh);
524 goto cleanup; 535 goto cleanup;
525 case NLMSG_DONE: 536 case NLMSG_DONE:
526 break; 537 break;