1 // SPDX-License-Identifier: LGPL-2.1 2 3 /* 4 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 6 * Copyright (C) 2015 Huawei Inc. 7 * Copyright (C) 2017 Nicira, Inc. 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU Lesser General Public 11 * License as published by the Free Software Foundation; 12 * version 2.1 of the License (not later!) 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this program; if not, see <http://www.gnu.org/licenses> 21 */ 22 23 #include <stdio.h> 24 #include <string.h> 25 26 #include "libbpf.h" 27 28 #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START) 29 #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c) 30 #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START) 31 32 static const char *libbpf_strerror_table[NR_ERRNO] = { 33 [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf", 34 [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid", 35 [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost", 36 [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch", 37 [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf", 38 [ERRCODE_OFFSET(RELOC)] = "Relocation failed", 39 [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", 40 [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", 41 [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", 42 [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type", 43 [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message", 44 [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence", 45 }; 46 47 int libbpf_strerror(int err, char *buf, size_t size) 48 { 49 if (!buf || !size) 50 return -1; 51 52 err = err > 0 ? err : -err; 53 54 if (err < __LIBBPF_ERRNO__START) { 55 int ret; 56 57 ret = strerror_r(err, buf, size); 58 buf[size - 1] = '\0'; 59 return ret; 60 } 61 62 if (err < __LIBBPF_ERRNO__END) { 63 const char *msg; 64 65 msg = libbpf_strerror_table[ERRNO_OFFSET(err)]; 66 snprintf(buf, size, "%s", msg); 67 buf[size - 1] = '\0'; 68 return 0; 69 } 70 71 snprintf(buf, size, "Unknown libbpf error %d", err); 72 buf[size - 1] = '\0'; 73 return -1; 74 } 75