18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci/* 48c2ecf20Sopenharmony_ci * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 68c2ecf20Sopenharmony_ci * Copyright (C) 2015 Huawei Inc. 78c2ecf20Sopenharmony_ci * Copyright (C) 2017 Nicira, Inc. 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#undef _GNU_SOURCE 118c2ecf20Sopenharmony_ci#include <stdio.h> 128c2ecf20Sopenharmony_ci#include <string.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include "libbpf.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci/* make sure libbpf doesn't use kernel-only integer typedefs */ 178c2ecf20Sopenharmony_ci#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START) 208c2ecf20Sopenharmony_ci#define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c) 218c2ecf20Sopenharmony_ci#define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START) 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistatic const char *libbpf_strerror_table[NR_ERRNO] = { 248c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf", 258c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid", 268c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost", 278c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch", 288c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf", 298c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(RELOC)] = "Relocation failed", 308c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", 318c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", 328c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", 338c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type", 348c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message", 358c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence", 368c2ecf20Sopenharmony_ci [ERRCODE_OFFSET(NLPARSE)] = "Incorrect netlink message parsing", 378c2ecf20Sopenharmony_ci}; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ciint libbpf_strerror(int err, char *buf, size_t size) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci if (!buf || !size) 428c2ecf20Sopenharmony_ci return -1; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci err = err > 0 ? err : -err; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci if (err < __LIBBPF_ERRNO__START) { 478c2ecf20Sopenharmony_ci int ret; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci ret = strerror_r(err, buf, size); 508c2ecf20Sopenharmony_ci buf[size - 1] = '\0'; 518c2ecf20Sopenharmony_ci return ret; 528c2ecf20Sopenharmony_ci } 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci if (err < __LIBBPF_ERRNO__END) { 558c2ecf20Sopenharmony_ci const char *msg; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci msg = libbpf_strerror_table[ERRNO_OFFSET(err)]; 588c2ecf20Sopenharmony_ci snprintf(buf, size, "%s", msg); 598c2ecf20Sopenharmony_ci buf[size - 1] = '\0'; 608c2ecf20Sopenharmony_ci return 0; 618c2ecf20Sopenharmony_ci } 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci snprintf(buf, size, "Unknown libbpf error %d", err); 648c2ecf20Sopenharmony_ci buf[size - 1] = '\0'; 658c2ecf20Sopenharmony_ci return -1; 668c2ecf20Sopenharmony_ci} 67