Skip to content

Commit 21703cf

Browse files
author
Alexei Starovoitov
committed
Merge branch 'libbpf: error reporting changes for v1.0'
Andrii Nakryiko says: ==================== Implement error reporting changes discussed in "Libbpf: the road to v1.0" ([0]) document. Libbpf gets a new API, libbpf_set_strict_mode() which accepts a set of flags that turn on a set of libbpf 1.0 changes, that might be potentially breaking. It's possible to opt-in into all current and future 1.0 features by specifying LIBBPF_STRICT_ALL flag. When some of the 1.0 "features" are requested, libbpf APIs might behave differently. In this patch set a first set of changes are implemented, all related to the way libbpf returns errors. See individual patches for details. Patch #1 adds a no-op libbpf_set_strict_mode() functionality to enable updating selftests. Patch #2 gets rid of all the bad code patterns that will break in libbpf 1.0 (exact -1 comparison for low-level APIs, direct IS_ERR() macro usage to check pointer-returning APIs for error, etc). These changes make selftest work in both legacy and 1.0 libbpf modes. Selftests also opt-in into 100% libbpf 1.0 mode to automatically gain all the subsequent changes, which will come in follow up patches. Patch #3 streamlines error reporting for low-level APIs wrapping bpf() syscall. Patch #4 streamlines errors for all the rest APIs. Patch #5 ensures that BPF skeletons propagate errors properly as well, as currently on error some APIs will return NULL with no way of checking exact error code. [0] https://docs.google.com/document/d/1UyjTZuPFWiPFyKk1tV5an11_iaRuec6U-ZESZ54nNTY v1->v2: - move libbpf_set_strict_mode() implementation to patch #1, where it belongs (Alexei); - add acks, slight rewording of commit messages. ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents a720a2a + 9c6c044 commit 21703cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1123
-952
lines changed

tools/bpf/bpftool/gen.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ static int do_skeleton(int argc, char **argv)
713713
#ifndef %2$s \n\
714714
#define %2$s \n\
715715
\n\
716+
#include <errno.h> \n\
716717
#include <stdlib.h> \n\
717718
#include <bpf/libbpf.h> \n\
718719
\n\
@@ -793,18 +794,23 @@ static int do_skeleton(int argc, char **argv)
793794
%1$s__open_opts(const struct bpf_object_open_opts *opts) \n\
794795
{ \n\
795796
struct %1$s *obj; \n\
797+
int err; \n\
796798
\n\
797799
obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\
798-
if (!obj) \n\
800+
if (!obj) { \n\
801+
errno = ENOMEM; \n\
799802
return NULL; \n\
800-
if (%1$s__create_skeleton(obj)) \n\
801-
goto err; \n\
802-
if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\
803-
goto err; \n\
803+
} \n\
804+
\n\
805+
err = %1$s__create_skeleton(obj); \n\
806+
err = err ?: bpf_object__open_skeleton(obj->skeleton, opts);\n\
807+
if (err) \n\
808+
goto err_out; \n\
804809
\n\
805810
return obj; \n\
806-
err: \n\
811+
err_out: \n\
807812
%1$s__destroy(obj); \n\
813+
errno = -err; \n\
808814
return NULL; \n\
809815
} \n\
810816
\n\
@@ -824,12 +830,15 @@ static int do_skeleton(int argc, char **argv)
824830
%1$s__open_and_load(void) \n\
825831
{ \n\
826832
struct %1$s *obj; \n\
833+
int err; \n\
827834
\n\
828835
obj = %1$s__open(); \n\
829836
if (!obj) \n\
830837
return NULL; \n\
831-
if (%1$s__load(obj)) { \n\
838+
err = %1$s__load(obj); \n\
839+
if (err) { \n\
832840
%1$s__destroy(obj); \n\
841+
errno = -err; \n\
833842
return NULL; \n\
834843
} \n\
835844
return obj; \n\
@@ -860,7 +869,7 @@ static int do_skeleton(int argc, char **argv)
860869
\n\
861870
s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\
862871
if (!s) \n\
863-
return -1; \n\
872+
goto err; \n\
864873
obj->skeleton = s; \n\
865874
\n\
866875
s->sz = sizeof(*s); \n\
@@ -949,7 +958,7 @@ static int do_skeleton(int argc, char **argv)
949958
return 0; \n\
950959
err: \n\
951960
bpf_object__destroy_skeleton(s); \n\
952-
return -1; \n\
961+
return -ENOMEM; \n\
953962
} \n\
954963
\n\
955964
#endif /* %s */ \n\

tools/lib/bpf/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ install_headers: $(BPF_HELPER_DEFS)
229229
$(call do_install,libbpf.h,$(prefix)/include/bpf,644); \
230230
$(call do_install,btf.h,$(prefix)/include/bpf,644); \
231231
$(call do_install,libbpf_common.h,$(prefix)/include/bpf,644); \
232+
$(call do_install,libbpf_legacy.h,$(prefix)/include/bpf,644); \
232233
$(call do_install,xsk.h,$(prefix)/include/bpf,644); \
233234
$(call do_install,bpf_helpers.h,$(prefix)/include/bpf,644); \
234235
$(call do_install,$(BPF_HELPER_DEFS),$(prefix)/include/bpf,644); \

0 commit comments

Comments
 (0)