2018-10-06 06:40:00 +07:00
|
|
|
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
|
2018-07-11 04:43:04 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
|
|
|
|
* Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
|
|
|
|
* Copyright (C) 2015 Huawei Inc.
|
|
|
|
* Copyright (C) 2017 Nicira, Inc.
|
|
|
|
*/
|
|
|
|
|
2018-11-30 06:31:45 +07:00
|
|
|
#undef _GNU_SOURCE
|
2018-07-11 04:43:04 +07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "libbpf.h"
|
|
|
|
|
libbpf: Poison kernel-only integer types
It's been a recurring issue with types like u32 slipping into libbpf source
code accidentally. This is not detected during builds inside kernel source
tree, but becomes a compilation error in libbpf's Github repo. Libbpf is
supposed to use only __{s,u}{8,16,32,64} typedefs, so poison {s,u}{8,16,32,64}
explicitly in every .c file. Doing that in a bit more centralized way, e.g.,
inside libbpf_internal.h breaks selftests, which are both using kernel u32 and
libbpf_internal.h.
This patch also fixes a new u32 occurence in libbpf.c, added recently.
Fixes: 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200110181916.271446-1-andriin@fb.com
2020-01-11 01:19:16 +07:00
|
|
|
/* make sure libbpf doesn't use kernel-only integer typedefs */
|
|
|
|
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
|
|
|
|
|
2018-07-11 04:43:04 +07:00
|
|
|
#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
|
|
|
|
#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
|
|
|
|
#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
|
|
|
|
|
|
|
|
static const char *libbpf_strerror_table[NR_ERRNO] = {
|
|
|
|
[ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
|
|
|
|
[ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
|
|
|
|
[ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
|
|
|
|
[ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
|
|
|
|
[ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
|
|
|
|
[ERRCODE_OFFSET(RELOC)] = "Relocation failed",
|
|
|
|
[ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
|
|
|
|
[ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
|
|
|
|
[ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
|
|
|
|
[ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
|
|
|
|
[ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message",
|
|
|
|
[ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence",
|
tools/bpf: add more netlink functionalities in lib/bpf
This patch added a few netlink attribute parsing functions
and the netlink API functions to query networking links, tc classes,
tc qdiscs and tc filters. For example, the following API is
to get networking links:
int nl_get_link(int sock, unsigned int nl_pid,
dump_nlmsg_t dump_link_nlmsg,
void *cookie);
Note that when the API is called, the user also provided a
callback function with the following signature:
int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
The "cookie" is the parameter the user passed to the API and will
be available for the callback function.
The "msg" is the information about the result, e.g., ifinfomsg or
tcmsg. The "tb" is the parsed netlink attributes.
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-09-06 06:58:05 +07:00
|
|
|
[ERRCODE_OFFSET(NLPARSE)] = "Incorrect netlink message parsing",
|
2018-07-11 04:43:04 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
int libbpf_strerror(int err, char *buf, size_t size)
|
|
|
|
{
|
|
|
|
if (!buf || !size)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
err = err > 0 ? err : -err;
|
|
|
|
|
|
|
|
if (err < __LIBBPF_ERRNO__START) {
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = strerror_r(err, buf, size);
|
|
|
|
buf[size - 1] = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err < __LIBBPF_ERRNO__END) {
|
|
|
|
const char *msg;
|
|
|
|
|
|
|
|
msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
|
|
|
|
snprintf(buf, size, "%s", msg);
|
|
|
|
buf[size - 1] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buf, size, "Unknown libbpf error %d", err);
|
|
|
|
buf[size - 1] = '\0';
|
|
|
|
return -1;
|
|
|
|
}
|