162306a36Sopenharmony_ci// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci/* 462306a36Sopenharmony_ci * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 562306a36Sopenharmony_ci * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 662306a36Sopenharmony_ci * Copyright (C) 2015 Huawei Inc. 762306a36Sopenharmony_ci * Copyright (C) 2017 Nicira, Inc. 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#undef _GNU_SOURCE 1162306a36Sopenharmony_ci#include <stdio.h> 1262306a36Sopenharmony_ci#include <string.h> 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include "libbpf.h" 1562306a36Sopenharmony_ci#include "libbpf_internal.h" 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci/* make sure libbpf doesn't use kernel-only integer typedefs */ 1862306a36Sopenharmony_ci#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START) 2162306a36Sopenharmony_ci#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c) 2262306a36Sopenharmony_ci#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START) 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_cistatic const char *libbpf_strerror_table[NR_ERRNO] = { 2562306a36Sopenharmony_ci [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf", 2662306a36Sopenharmony_ci [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid", 2762306a36Sopenharmony_ci [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost", 2862306a36Sopenharmony_ci [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch", 2962306a36Sopenharmony_ci [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf", 3062306a36Sopenharmony_ci [ERRCODE_OFFSET(RELOC)] = "Relocation failed", 3162306a36Sopenharmony_ci [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", 3262306a36Sopenharmony_ci [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", 3362306a36Sopenharmony_ci [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", 3462306a36Sopenharmony_ci [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type", 3562306a36Sopenharmony_ci [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message", 3662306a36Sopenharmony_ci [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence", 3762306a36Sopenharmony_ci [ERRCODE_OFFSET(NLPARSE)] = "Incorrect netlink message parsing", 3862306a36Sopenharmony_ci}; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ciint libbpf_strerror(int err, char *buf, size_t size) 4162306a36Sopenharmony_ci{ 4262306a36Sopenharmony_ci int ret; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci if (!buf || !size) 4562306a36Sopenharmony_ci return libbpf_err(-EINVAL); 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci err = err > 0 ? err : -err; 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci if (err < __LIBBPF_ERRNO__START) { 5062306a36Sopenharmony_ci ret = strerror_r(err, buf, size); 5162306a36Sopenharmony_ci buf[size - 1] = '\0'; 5262306a36Sopenharmony_ci return libbpf_err_errno(ret); 5362306a36Sopenharmony_ci } 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci if (err < __LIBBPF_ERRNO__END) { 5662306a36Sopenharmony_ci const char *msg; 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci msg = libbpf_strerror_table[ERRNO_OFFSET(err)]; 5962306a36Sopenharmony_ci ret = snprintf(buf, size, "%s", msg); 6062306a36Sopenharmony_ci buf[size - 1] = '\0'; 6162306a36Sopenharmony_ci /* The length of the buf and msg is positive. 6262306a36Sopenharmony_ci * A negative number may be returned only when the 6362306a36Sopenharmony_ci * size exceeds INT_MAX. Not likely to appear. 6462306a36Sopenharmony_ci */ 6562306a36Sopenharmony_ci if (ret >= size) 6662306a36Sopenharmony_ci return libbpf_err(-ERANGE); 6762306a36Sopenharmony_ci return 0; 6862306a36Sopenharmony_ci } 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci ret = snprintf(buf, size, "Unknown libbpf error %d", err); 7162306a36Sopenharmony_ci buf[size - 1] = '\0'; 7262306a36Sopenharmony_ci if (ret >= size) 7362306a36Sopenharmony_ci return libbpf_err(-ERANGE); 7462306a36Sopenharmony_ci return libbpf_err(-ENOENT); 7562306a36Sopenharmony_ci} 76