Commit 21703cf7 authored by Alexei Starovoitov's avatar Alexei Starovoitov

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: default avatarAlexei Starovoitov <ast@kernel.org>
parents a720a2a0 9c6c0449
...@@ -713,6 +713,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -713,6 +713,7 @@ static int do_skeleton(int argc, char **argv)
#ifndef %2$s \n\ #ifndef %2$s \n\
#define %2$s \n\ #define %2$s \n\
\n\ \n\
#include <errno.h> \n\
#include <stdlib.h> \n\ #include <stdlib.h> \n\
#include <bpf/libbpf.h> \n\ #include <bpf/libbpf.h> \n\
\n\ \n\
...@@ -793,18 +794,23 @@ static int do_skeleton(int argc, char **argv) ...@@ -793,18 +794,23 @@ static int do_skeleton(int argc, char **argv)
%1$s__open_opts(const struct bpf_object_open_opts *opts) \n\ %1$s__open_opts(const struct bpf_object_open_opts *opts) \n\
{ \n\ { \n\
struct %1$s *obj; \n\ struct %1$s *obj; \n\
int err; \n\
\n\ \n\
obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\ obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\
if (!obj) \n\ if (!obj) { \n\
errno = ENOMEM; \n\
return NULL; \n\ return NULL; \n\
if (%1$s__create_skeleton(obj)) \n\ } \n\
goto err; \n\ \n\
if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\ err = %1$s__create_skeleton(obj); \n\
goto err; \n\ err = err ?: bpf_object__open_skeleton(obj->skeleton, opts);\n\
if (err) \n\
goto err_out; \n\
\n\ \n\
return obj; \n\ return obj; \n\
err: \n\ err_out: \n\
%1$s__destroy(obj); \n\ %1$s__destroy(obj); \n\
errno = -err; \n\
return NULL; \n\ return NULL; \n\
} \n\ } \n\
\n\ \n\
...@@ -824,12 +830,15 @@ static int do_skeleton(int argc, char **argv) ...@@ -824,12 +830,15 @@ static int do_skeleton(int argc, char **argv)
%1$s__open_and_load(void) \n\ %1$s__open_and_load(void) \n\
{ \n\ { \n\
struct %1$s *obj; \n\ struct %1$s *obj; \n\
int err; \n\
\n\ \n\
obj = %1$s__open(); \n\ obj = %1$s__open(); \n\
if (!obj) \n\ if (!obj) \n\
return NULL; \n\ return NULL; \n\
if (%1$s__load(obj)) { \n\ err = %1$s__load(obj); \n\
if (err) { \n\
%1$s__destroy(obj); \n\ %1$s__destroy(obj); \n\
errno = -err; \n\
return NULL; \n\ return NULL; \n\
} \n\ } \n\
return obj; \n\ return obj; \n\
...@@ -860,7 +869,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -860,7 +869,7 @@ static int do_skeleton(int argc, char **argv)
\n\ \n\
s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\ s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\
if (!s) \n\ if (!s) \n\
return -1; \n\ goto err; \n\
obj->skeleton = s; \n\ obj->skeleton = s; \n\
\n\ \n\
s->sz = sizeof(*s); \n\ s->sz = sizeof(*s); \n\
...@@ -949,7 +958,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -949,7 +958,7 @@ static int do_skeleton(int argc, char **argv)
return 0; \n\ return 0; \n\
err: \n\ err: \n\
bpf_object__destroy_skeleton(s); \n\ bpf_object__destroy_skeleton(s); \n\
return -1; \n\ return -ENOMEM; \n\
} \n\ } \n\
\n\ \n\
#endif /* %s */ \n\ #endif /* %s */ \n\
......
...@@ -229,6 +229,7 @@ install_headers: $(BPF_HELPER_DEFS) ...@@ -229,6 +229,7 @@ install_headers: $(BPF_HELPER_DEFS)
$(call do_install,libbpf.h,$(prefix)/include/bpf,644); \ $(call do_install,libbpf.h,$(prefix)/include/bpf,644); \
$(call do_install,btf.h,$(prefix)/include/bpf,644); \ $(call do_install,btf.h,$(prefix)/include/bpf,644); \
$(call do_install,libbpf_common.h,$(prefix)/include/bpf,644); \ $(call do_install,libbpf_common.h,$(prefix)/include/bpf,644); \
$(call do_install,libbpf_legacy.h,$(prefix)/include/bpf,644); \
$(call do_install,xsk.h,$(prefix)/include/bpf,644); \ $(call do_install,xsk.h,$(prefix)/include/bpf,644); \
$(call do_install,bpf_helpers.h,$(prefix)/include/bpf,644); \ $(call do_install,bpf_helpers.h,$(prefix)/include/bpf,644); \
$(call do_install,$(BPF_HELPER_DEFS),$(prefix)/include/bpf,644); \ $(call do_install,$(BPF_HELPER_DEFS),$(prefix)/include/bpf,644); \
......
This diff is collapsed.
...@@ -106,7 +106,7 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info) ...@@ -106,7 +106,7 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
nr_linfo = info->nr_line_info; nr_linfo = info->nr_line_info;
if (!nr_linfo) if (!nr_linfo)
return NULL; return errno = EINVAL, NULL;
/* /*
* The min size that bpf_prog_linfo has to access for * The min size that bpf_prog_linfo has to access for
...@@ -114,11 +114,11 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info) ...@@ -114,11 +114,11 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
*/ */
if (info->line_info_rec_size < if (info->line_info_rec_size <
offsetof(struct bpf_line_info, file_name_off)) offsetof(struct bpf_line_info, file_name_off))
return NULL; return errno = EINVAL, NULL;
prog_linfo = calloc(1, sizeof(*prog_linfo)); prog_linfo = calloc(1, sizeof(*prog_linfo));
if (!prog_linfo) if (!prog_linfo)
return NULL; return errno = ENOMEM, NULL;
/* Copy xlated line_info */ /* Copy xlated line_info */
prog_linfo->nr_linfo = nr_linfo; prog_linfo->nr_linfo = nr_linfo;
...@@ -174,7 +174,7 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info) ...@@ -174,7 +174,7 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
err_free: err_free:
bpf_prog_linfo__free(prog_linfo); bpf_prog_linfo__free(prog_linfo);
return NULL; return errno = EINVAL, NULL;
} }
const struct bpf_line_info * const struct bpf_line_info *
...@@ -186,11 +186,11 @@ bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo, ...@@ -186,11 +186,11 @@ bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
const __u64 *jited_linfo; const __u64 *jited_linfo;
if (func_idx >= prog_linfo->nr_jited_func) if (func_idx >= prog_linfo->nr_jited_func)
return NULL; return errno = ENOENT, NULL;
nr_linfo = prog_linfo->nr_jited_linfo_per_func[func_idx]; nr_linfo = prog_linfo->nr_jited_linfo_per_func[func_idx];
if (nr_skip >= nr_linfo) if (nr_skip >= nr_linfo)
return NULL; return errno = ENOENT, NULL;
start = prog_linfo->jited_linfo_func_idx[func_idx] + nr_skip; start = prog_linfo->jited_linfo_func_idx[func_idx] + nr_skip;
jited_rec_size = prog_linfo->jited_rec_size; jited_rec_size = prog_linfo->jited_rec_size;
...@@ -198,7 +198,7 @@ bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo, ...@@ -198,7 +198,7 @@ bpf_prog_linfo__lfind_addr_func(const struct bpf_prog_linfo *prog_linfo,
(start * jited_rec_size); (start * jited_rec_size);
jited_linfo = raw_jited_linfo; jited_linfo = raw_jited_linfo;
if (addr < *jited_linfo) if (addr < *jited_linfo)
return NULL; return errno = ENOENT, NULL;
nr_linfo -= nr_skip; nr_linfo -= nr_skip;
rec_size = prog_linfo->rec_size; rec_size = prog_linfo->rec_size;
...@@ -225,13 +225,13 @@ bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo, ...@@ -225,13 +225,13 @@ bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,
nr_linfo = prog_linfo->nr_linfo; nr_linfo = prog_linfo->nr_linfo;
if (nr_skip >= nr_linfo) if (nr_skip >= nr_linfo)
return NULL; return errno = ENOENT, NULL;
rec_size = prog_linfo->rec_size; rec_size = prog_linfo->rec_size;
raw_linfo = prog_linfo->raw_linfo + (nr_skip * rec_size); raw_linfo = prog_linfo->raw_linfo + (nr_skip * rec_size);
linfo = raw_linfo; linfo = raw_linfo;
if (insn_off < linfo->insn_off) if (insn_off < linfo->insn_off)
return NULL; return errno = ENOENT, NULL;
nr_linfo -= nr_skip; nr_linfo -= nr_skip;
for (i = 0; i < nr_linfo; i++) { for (i = 0; i < nr_linfo; i++) {
......
This diff is collapsed.
...@@ -128,7 +128,7 @@ struct btf_dump *btf_dump__new(const struct btf *btf, ...@@ -128,7 +128,7 @@ struct btf_dump *btf_dump__new(const struct btf *btf,
d = calloc(1, sizeof(struct btf_dump)); d = calloc(1, sizeof(struct btf_dump));
if (!d) if (!d)
return ERR_PTR(-ENOMEM); return libbpf_err_ptr(-ENOMEM);
d->btf = btf; d->btf = btf;
d->btf_ext = btf_ext; d->btf_ext = btf_ext;
...@@ -156,7 +156,7 @@ struct btf_dump *btf_dump__new(const struct btf *btf, ...@@ -156,7 +156,7 @@ struct btf_dump *btf_dump__new(const struct btf *btf,
return d; return d;
err: err:
btf_dump__free(d); btf_dump__free(d);
return ERR_PTR(err); return libbpf_err_ptr(err);
} }
static int btf_dump_resize(struct btf_dump *d) static int btf_dump_resize(struct btf_dump *d)
...@@ -236,16 +236,16 @@ int btf_dump__dump_type(struct btf_dump *d, __u32 id) ...@@ -236,16 +236,16 @@ int btf_dump__dump_type(struct btf_dump *d, __u32 id)
int err, i; int err, i;
if (id > btf__get_nr_types(d->btf)) if (id > btf__get_nr_types(d->btf))
return -EINVAL; return libbpf_err(-EINVAL);
err = btf_dump_resize(d); err = btf_dump_resize(d);
if (err) if (err)
return err; return libbpf_err(err);
d->emit_queue_cnt = 0; d->emit_queue_cnt = 0;
err = btf_dump_order_type(d, id, false); err = btf_dump_order_type(d, id, false);
if (err < 0) if (err < 0)
return err; return libbpf_err(err);
for (i = 0; i < d->emit_queue_cnt; i++) for (i = 0; i < d->emit_queue_cnt; i++)
btf_dump_emit_type(d, d->emit_queue[i], 0 /*top-level*/); btf_dump_emit_type(d, d->emit_queue[i], 0 /*top-level*/);
...@@ -1075,11 +1075,11 @@ int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id, ...@@ -1075,11 +1075,11 @@ int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
int lvl, err; int lvl, err;
if (!OPTS_VALID(opts, btf_dump_emit_type_decl_opts)) if (!OPTS_VALID(opts, btf_dump_emit_type_decl_opts))
return -EINVAL; return libbpf_err(-EINVAL);
err = btf_dump_resize(d); err = btf_dump_resize(d);
if (err) if (err)
return -EINVAL; return libbpf_err(err);
fname = OPTS_GET(opts, field_name, ""); fname = OPTS_GET(opts, field_name, "");
lvl = OPTS_GET(opts, indent_level, 0); lvl = OPTS_GET(opts, indent_level, 0);
......
This diff is collapsed.
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <linux/bpf.h> #include <linux/bpf.h>
#include "libbpf_common.h" #include "libbpf_common.h"
#include "libbpf_legacy.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
......
...@@ -370,3 +370,8 @@ LIBBPF_0.4.0 { ...@@ -370,3 +370,8 @@ LIBBPF_0.4.0 {
bpf_tc_hook_destroy; bpf_tc_hook_destroy;
bpf_tc_query; bpf_tc_query;
} LIBBPF_0.3.0; } LIBBPF_0.3.0;
LIBBPF_0.5.0 {
global:
libbpf_set_strict_mode;
} LIBBPF_0.4.0;
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <string.h> #include <string.h>
#include "libbpf.h" #include "libbpf.h"
#include "libbpf_internal.h"
/* make sure libbpf doesn't use kernel-only integer typedefs */ /* make sure libbpf doesn't use kernel-only integer typedefs */
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
...@@ -39,7 +40,7 @@ static const char *libbpf_strerror_table[NR_ERRNO] = { ...@@ -39,7 +40,7 @@ static const char *libbpf_strerror_table[NR_ERRNO] = {
int libbpf_strerror(int err, char *buf, size_t size) int libbpf_strerror(int err, char *buf, size_t size)
{ {
if (!buf || !size) if (!buf || !size)
return -1; return libbpf_err(-EINVAL);
err = err > 0 ? err : -err; err = err > 0 ? err : -err;
...@@ -48,7 +49,7 @@ int libbpf_strerror(int err, char *buf, size_t size) ...@@ -48,7 +49,7 @@ int libbpf_strerror(int err, char *buf, size_t size)
ret = strerror_r(err, buf, size); ret = strerror_r(err, buf, size);
buf[size - 1] = '\0'; buf[size - 1] = '\0';
return ret; return libbpf_err_errno(ret);
} }
if (err < __LIBBPF_ERRNO__END) { if (err < __LIBBPF_ERRNO__END) {
...@@ -62,5 +63,5 @@ int libbpf_strerror(int err, char *buf, size_t size) ...@@ -62,5 +63,5 @@ int libbpf_strerror(int err, char *buf, size_t size)
snprintf(buf, size, "Unknown libbpf error %d", err); snprintf(buf, size, "Unknown libbpf error %d", err);
buf[size - 1] = '\0'; buf[size - 1] = '\0';
return -1; return libbpf_err(-ENOENT);
} }
...@@ -11,6 +11,9 @@ ...@@ -11,6 +11,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <errno.h>
#include <linux/err.h>
#include "libbpf_legacy.h"
/* make sure libbpf doesn't use kernel-only integer typedefs */ /* make sure libbpf doesn't use kernel-only integer typedefs */
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
...@@ -436,4 +439,54 @@ int btf_type_visit_str_offs(struct btf_type *t, str_off_visit_fn visit, void *ct ...@@ -436,4 +439,54 @@ int btf_type_visit_str_offs(struct btf_type *t, str_off_visit_fn visit, void *ct
int btf_ext_visit_type_ids(struct btf_ext *btf_ext, type_id_visit_fn visit, void *ctx); int btf_ext_visit_type_ids(struct btf_ext *btf_ext, type_id_visit_fn visit, void *ctx);
int btf_ext_visit_str_offs(struct btf_ext *btf_ext, str_off_visit_fn visit, void *ctx); int btf_ext_visit_str_offs(struct btf_ext *btf_ext, str_off_visit_fn visit, void *ctx);
extern enum libbpf_strict_mode libbpf_mode;
/* handle direct returned errors */
static inline int libbpf_err(int ret)
{
if (ret < 0)
errno = -ret;
return ret;
}
/* handle errno-based (e.g., syscall or libc) errors according to libbpf's
* strict mode settings
*/
static inline int libbpf_err_errno(int ret)
{
if (libbpf_mode & LIBBPF_STRICT_DIRECT_ERRS)
/* errno is already assumed to be set on error */
return ret < 0 ? -errno : ret;
/* legacy: on error return -1 directly and don't touch errno */
return ret;
}
/* handle error for pointer-returning APIs, err is assumed to be < 0 always */
static inline void *libbpf_err_ptr(int err)
{
/* set errno on error, this doesn't break anything */
errno = -err;
if (libbpf_mode & LIBBPF_STRICT_CLEAN_PTRS)
return NULL;
/* legacy: encode err as ptr */
return ERR_PTR(err);
}
/* handle pointer-returning APIs' error handling */
static inline void *libbpf_ptr(void *ret)
{
/* set errno on error, this doesn't break anything */
if (IS_ERR(ret))
errno = -PTR_ERR(ret);
if (libbpf_mode & LIBBPF_STRICT_CLEAN_PTRS)
return IS_ERR(ret) ? NULL : ret;
/* legacy: pass-through original pointer */
return ret;
}
#endif /* __LIBBPF_LIBBPF_INTERNAL_H */ #endif /* __LIBBPF_LIBBPF_INTERNAL_H */
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
/*
* Libbpf legacy APIs (either discouraged or deprecated, as mentioned in [0])
*
* [0] https://docs.google.com/document/d/1UyjTZuPFWiPFyKk1tV5an11_iaRuec6U-ZESZ54nNTY
*
* Copyright (C) 2021 Facebook
*/
#ifndef __LIBBPF_LEGACY_BPF_H
#define __LIBBPF_LEGACY_BPF_H
#include <linux/bpf.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "libbpf_common.h"
#ifdef __cplusplus
extern "C" {
#endif
enum libbpf_strict_mode {
/* Turn on all supported strict features of libbpf to simulate libbpf
* v1.0 behavior.
* This will be the default behavior in libbpf v1.0.
*/
LIBBPF_STRICT_ALL = 0xffffffff,
/*
* Disable any libbpf 1.0 behaviors. This is the default before libbpf
* v1.0. It won't be supported anymore in v1.0, please update your
* code so that it handles LIBBPF_STRICT_ALL mode before libbpf v1.0.
*/
LIBBPF_STRICT_NONE = 0x00,
/*
* Return NULL pointers on error, not ERR_PTR(err).
* Additionally, libbpf also always sets errno to corresponding Exx
* (positive) error code.
*/
LIBBPF_STRICT_CLEAN_PTRS = 0x01,
/*
* Return actual error codes from low-level APIs directly, not just -1.
* Additionally, libbpf also always sets errno to corresponding Exx
* (positive) error code.
*/
LIBBPF_STRICT_DIRECT_ERRS = 0x02,
__LIBBPF_STRICT_LAST,
};
LIBBPF_API int libbpf_set_strict_mode(enum libbpf_strict_mode mode);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __LIBBPF_LEGACY_BPF_H */
...@@ -220,16 +220,16 @@ struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts ...@@ -220,16 +220,16 @@ struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts
int err; int err;
if (!OPTS_VALID(opts, bpf_linker_opts)) if (!OPTS_VALID(opts, bpf_linker_opts))
return NULL; return errno = EINVAL, NULL;
if (elf_version(EV_CURRENT) == EV_NONE) { if (elf_version(EV_CURRENT) == EV_NONE) {
pr_warn_elf("libelf initialization failed"); pr_warn_elf("libelf initialization failed");
return NULL; return errno = EINVAL, NULL;
} }
linker = calloc(1, sizeof(*linker)); linker = calloc(1, sizeof(*linker));
if (!linker) if (!linker)
return NULL; return errno = ENOMEM, NULL;
linker->fd = -1; linker->fd = -1;
...@@ -241,7 +241,7 @@ struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts ...@@ -241,7 +241,7 @@ struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts
err_out: err_out:
bpf_linker__free(linker); bpf_linker__free(linker);
return NULL; return errno = -err, NULL;
} }
static struct dst_sec *add_dst_sec(struct bpf_linker *linker, const char *sec_name) static struct dst_sec *add_dst_sec(struct bpf_linker *linker, const char *sec_name)
...@@ -444,10 +444,10 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, ...@@ -444,10 +444,10 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename,
int err = 0; int err = 0;
if (!OPTS_VALID(opts, bpf_linker_file_opts)) if (!OPTS_VALID(opts, bpf_linker_file_opts))
return -EINVAL; return libbpf_err(-EINVAL);
if (!linker->elf) if (!linker->elf)
return -EINVAL; return libbpf_err(-EINVAL);
err = err ?: linker_load_obj_file(linker, filename, opts, &obj); err = err ?: linker_load_obj_file(linker, filename, opts, &obj);
err = err ?: linker_append_sec_data(linker, &obj); err = err ?: linker_append_sec_data(linker, &obj);
...@@ -467,7 +467,7 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename, ...@@ -467,7 +467,7 @@ int bpf_linker__add_file(struct bpf_linker *linker, const char *filename,
if (obj.fd >= 0) if (obj.fd >= 0)
close(obj.fd); close(obj.fd);
return err; return libbpf_err(err);
} }
static bool is_dwarf_sec_name(const char *name) static bool is_dwarf_sec_name(const char *name)
...@@ -2548,11 +2548,11 @@ int bpf_linker__finalize(struct bpf_linker *linker) ...@@ -2548,11 +2548,11 @@ int bpf_linker__finalize(struct bpf_linker *linker)
int err, i; int err, i;
if (!linker->elf) if (!linker->elf)
return -EINVAL; return libbpf_err(-EINVAL);
err = finalize_btf(linker); err = finalize_btf(linker);
if (err) if (err)
return err; return libbpf_err(err);
/* Finalize strings */ /* Finalize strings */
strs_sz = strset__data_size(linker->strtab_strs); strs_sz = strset__data_size(linker->strtab_strs);
...@@ -2584,14 +2584,14 @@ int bpf_linker__finalize(struct bpf_linker *linker) ...@@ -2584,14 +2584,14 @@ int bpf_linker__finalize(struct bpf_linker *linker)
if (elf_update(linker->elf, ELF_C_NULL) < 0) { if (elf_update(linker->elf, ELF_C_NULL) < 0) {
err = -errno; err = -errno;
pr_warn_elf("failed to finalize ELF layout"); pr_warn_elf("failed to finalize ELF layout");
return err; return libbpf_err(err);
} }
/* Write out final ELF contents */ /* Write out final ELF contents */
if (elf_update(linker->elf, ELF_C_WRITE) < 0) { if (elf_update(linker->elf, ELF_C_WRITE) < 0) {
err = -errno; err = -errno;
pr_warn_elf("failed to write ELF contents"); pr_warn_elf("failed to write ELF contents");
return err; return libbpf_err(err);
} }
elf_end(linker->elf); elf_end(linker->elf);
......
...@@ -225,22 +225,26 @@ static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd, ...@@ -225,22 +225,26 @@ static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd,
int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags, int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
const struct bpf_xdp_set_link_opts *opts) const struct bpf_xdp_set_link_opts *opts)
{ {
int old_fd = -1; int old_fd = -1, ret;
if (!OPTS_VALID(opts, bpf_xdp_set_link_opts)) if (!OPTS_VALID(opts, bpf_xdp_set_link_opts))
return -EINVAL; return libbpf_err(-EINVAL);
if (OPTS_HAS(opts, old_fd)) { if (OPTS_HAS(opts, old_fd)) {
old_fd = OPTS_GET(opts, old_fd, -1); old_fd = OPTS_GET(opts, old_fd, -1);
flags |= XDP_FLAGS_REPLACE; flags |= XDP_FLAGS_REPLACE;
} }
return __bpf_set_link_xdp_fd_replace(ifindex, fd, old_fd, flags); ret = __bpf_set_link_xdp_fd_replace(ifindex, fd, old_fd, flags);
return libbpf_err(ret);
} }
int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags) int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
{ {
return __bpf_set_link_xdp_fd_replace(ifindex, fd, 0, flags); int ret;
ret = __bpf_set_link_xdp_fd_replace(ifindex, fd, 0, flags);
return libbpf_err(ret);
} }
static int __dump_link_nlmsg(struct nlmsghdr *nlh, static int __dump_link_nlmsg(struct nlmsghdr *nlh,
...@@ -321,13 +325,13 @@ int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info, ...@@ -321,13 +325,13 @@ int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
}; };
if (flags & ~XDP_FLAGS_MASK || !info_size) if (flags & ~XDP_FLAGS_MASK || !info_size)
return -EINVAL; return libbpf_err(-EINVAL);
/* Check whether the single {HW,DRV,SKB} mode is set */ /* Check whether the single {HW,DRV,SKB} mode is set */
flags &= (XDP_FLAGS_SKB_MODE | XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE); flags &= (XDP_FLAGS_SKB_MODE | XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE);
mask = flags - 1; mask = flags - 1;
if (flags && flags & mask) if (flags && flags & mask)
return -EINVAL; return libbpf_err(-EINVAL);
xdp_id.ifindex = ifindex; xdp_id.ifindex = ifindex;
xdp_id.flags = flags; xdp_id.flags = flags;
...@@ -341,7 +345,7 @@ int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info, ...@@ -341,7 +345,7 @@ int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
memset((void *) info + sz, 0, info_size - sz); memset((void *) info + sz, 0, info_size - sz);
} }
return ret; return libbpf_err(ret);
} }
static __u32 get_xdp_id(struct xdp_link_info *info, __u32 flags) static __u32 get_xdp_id(struct xdp_link_info *info, __u32 flags)
...@@ -369,7 +373,7 @@ int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags) ...@@ -369,7 +373,7 @@ int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags)
if (!ret) if (!ret)
*prog_id = get_xdp_id(&info, flags); *prog_id = get_xdp_id(&info, flags);
return ret; return libbpf_err(ret);
} }
typedef int (*qdisc_config_t)(struct nlmsghdr *nh, struct tcmsg *t, typedef int (*qdisc_config_t)(struct nlmsghdr *nh, struct tcmsg *t,
...@@ -463,11 +467,14 @@ static int tc_qdisc_delete(struct bpf_tc_hook *hook) ...@@ -463,11 +467,14 @@ static int tc_qdisc_delete(struct bpf_tc_hook *hook)
int bpf_tc_hook_create(struct bpf_tc_hook *hook) int bpf_tc_hook_create(struct bpf_tc_hook *hook)
{ {
int ret;
if (!hook || !OPTS_VALID(hook, bpf_tc_hook) || if (!hook || !OPTS_VALID(hook, bpf_tc_hook) ||
OPTS_GET(hook, ifindex, 0) <= 0) OPTS_GET(hook, ifindex, 0) <= 0)
return -EINVAL; return libbpf_err(-EINVAL);
return tc_qdisc_create_excl(hook); ret = tc_qdisc_create_excl(hook);
return libbpf_err(ret);
} }
static int __bpf_tc_detach(const struct bpf_tc_hook *hook, static int __bpf_tc_detach(const struct bpf_tc_hook *hook,
...@@ -478,18 +485,18 @@ int bpf_tc_hook_destroy(struct bpf_tc_hook *hook) ...@@ -478,18 +485,18 @@ int bpf_tc_hook_destroy(struct bpf_tc_hook *hook)
{ {
if (!hook || !OPTS_VALID(hook, bpf_tc_hook) || if (!hook || !OPTS_VALID(hook, bpf_tc_hook) ||
OPTS_GET(hook, ifindex, 0) <= 0) OPTS_GET(hook, ifindex, 0) <= 0)
return -EINVAL; return libbpf_err(-EINVAL);
switch (OPTS_GET(hook, attach_point, 0)) { switch (OPTS_GET(hook, attach_point, 0)) {
case BPF_TC_INGRESS: case BPF_TC_INGRESS:
case BPF_TC_EGRESS: case BPF_TC_EGRESS:
return __bpf_tc_detach(hook, NULL, true); return libbpf_err(__bpf_tc_detach(hook, NULL, true));
case BPF_TC_INGRESS | BPF_TC_EGRESS: case BPF_TC_INGRESS | BPF_TC_EGRESS:
return tc_qdisc_delete(hook); return libbpf_err(tc_qdisc_delete(hook));
case BPF_TC_CUSTOM: case BPF_TC_CUSTOM:
return -EOPNOTSUPP; return libbpf_err(-EOPNOTSUPP);
default: default:
return -EINVAL; return libbpf_err(-EINVAL);
} }
} }
...@@ -574,7 +581,7 @@ int bpf_tc_attach(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts) ...@@ -574,7 +581,7 @@ int bpf_tc_attach(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts)
if (!hook || !opts || if (!hook || !opts ||
!OPTS_VALID(hook, bpf_tc_hook) || !OPTS_VALID(hook, bpf_tc_hook) ||
!OPTS_VALID(opts, bpf_tc_opts)) !OPTS_VALID(opts, bpf_tc_opts))
return -EINVAL; return libbpf_err(-EINVAL);
ifindex = OPTS_GET(hook, ifindex, 0); ifindex = OPTS_GET(hook, ifindex, 0);
parent = OPTS_GET(hook, parent, 0); parent = OPTS_GET(hook, parent, 0);
...@@ -587,11 +594,11 @@ int bpf_tc_attach(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts) ...@@ -587,11 +594,11 @@ int bpf_tc_attach(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts)
flags = OPTS_GET(opts, flags, 0); flags = OPTS_GET(opts, flags, 0);
if (ifindex <= 0 || !prog_fd || prog_id) if (ifindex <= 0 || !prog_fd || prog_id)
return -EINVAL; return libbpf_err(-EINVAL);
if (priority > UINT16_MAX) if (priority > UINT16_MAX)
return -EINVAL; return libbpf_err(-EINVAL);
if (flags & ~BPF_TC_F_REPLACE) if (flags & ~BPF_TC_F_REPLACE)
return -EINVAL; return libbpf_err(-EINVAL);
flags = (flags & BPF_TC_F_REPLACE) ? NLM_F_REPLACE : NLM_F_EXCL; flags = (flags & BPF_TC_F_REPLACE) ? NLM_F_REPLACE : NLM_F_EXCL;
protocol = ETH_P_ALL; protocol = ETH_P_ALL;
...@@ -608,32 +615,32 @@ int bpf_tc_attach(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts) ...@@ -608,32 +615,32 @@ int bpf_tc_attach(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts)
ret = tc_get_tcm_parent(attach_point, &parent); ret = tc_get_tcm_parent(attach_point, &parent);
if (ret < 0) if (ret < 0)
return ret; return libbpf_err(ret);
req.tc.tcm_parent = parent; req.tc.tcm_parent = parent;
ret = nlattr_add(&req.nh, sizeof(req), TCA_KIND, "bpf", sizeof("bpf")); ret = nlattr_add(&req.nh, sizeof(req), TCA_KIND, "bpf", sizeof("bpf"));
if (ret < 0) if (ret < 0)
return ret; return libbpf_err(ret);
nla = nlattr_begin_nested(&req.nh, sizeof(req), TCA_OPTIONS); nla = nlattr_begin_nested(&req.nh, sizeof(req), TCA_OPTIONS);
if (!nla) if (!nla)
return -EMSGSIZE; return libbpf_err(-EMSGSIZE);
ret = tc_add_fd_and_name(&req.nh, sizeof(req), prog_fd); ret = tc_add_fd_and_name(&req.nh, sizeof(req), prog_fd);
if (ret < 0) if (ret < 0)
return ret; return libbpf_err(ret);
bpf_flags = TCA_BPF_FLAG_ACT_DIRECT; bpf_flags = TCA_BPF_FLAG_ACT_DIRECT;
ret = nlattr_add(&req.nh, sizeof(req), TCA_BPF_FLAGS, &bpf_flags, ret = nlattr_add(&req.nh, sizeof(req), TCA_BPF_FLAGS, &bpf_flags,
sizeof(bpf_flags)); sizeof(bpf_flags));
if (ret < 0) if (ret < 0)
return ret; return libbpf_err(ret);
nlattr_end_nested(&req.nh, nla); nlattr_end_nested(&req.nh, nla);
info.opts = opts; info.opts = opts;
ret = libbpf_netlink_send_recv(&req.nh, get_tc_info, NULL, &info); ret = libbpf_netlink_send_recv(&req.nh, get_tc_info, NULL, &info);
if (ret < 0) if (ret < 0)
return ret; return libbpf_err(ret);
if (!info.processed) if (!info.processed)
return -ENOENT; return libbpf_err(-ENOENT);
return ret; return ret;
} }
...@@ -708,7 +715,13 @@ static int __bpf_tc_detach(const struct bpf_tc_hook *hook, ...@@ -708,7 +715,13 @@ static int __bpf_tc_detach(const struct bpf_tc_hook *hook,
int bpf_tc_detach(const struct bpf_tc_hook *hook, int bpf_tc_detach(const struct bpf_tc_hook *hook,
const struct bpf_tc_opts *opts) const struct bpf_tc_opts *opts)
{ {
return !opts ? -EINVAL : __bpf_tc_detach(hook, opts, false); int ret;
if (!opts)
return libbpf_err(-EINVAL);
ret = __bpf_tc_detach(hook, opts, false);
return libbpf_err(ret);
} }
int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts) int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts)
...@@ -725,7 +738,7 @@ int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts) ...@@ -725,7 +738,7 @@ int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts)
if (!hook || !opts || if (!hook || !opts ||
!OPTS_VALID(hook, bpf_tc_hook) || !OPTS_VALID(hook, bpf_tc_hook) ||
!OPTS_VALID(opts, bpf_tc_opts)) !OPTS_VALID(opts, bpf_tc_opts))
return -EINVAL; return libbpf_err(-EINVAL);
ifindex = OPTS_GET(hook, ifindex, 0); ifindex = OPTS_GET(hook, ifindex, 0);
parent = OPTS_GET(hook, parent, 0); parent = OPTS_GET(hook, parent, 0);
...@@ -739,9 +752,9 @@ int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts) ...@@ -739,9 +752,9 @@ int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts)
if (ifindex <= 0 || flags || prog_fd || prog_id || if (ifindex <= 0 || flags || prog_fd || prog_id ||
!handle || !priority) !handle || !priority)
return -EINVAL; return libbpf_err(-EINVAL);
if (priority > UINT16_MAX) if (priority > UINT16_MAX)
return -EINVAL; return libbpf_err(-EINVAL);
protocol = ETH_P_ALL; protocol = ETH_P_ALL;
...@@ -756,19 +769,19 @@ int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts) ...@@ -756,19 +769,19 @@ int bpf_tc_query(const struct bpf_tc_hook *hook, struct bpf_tc_opts *opts)
ret = tc_get_tcm_parent(attach_point, &parent); ret = tc_get_tcm_parent(attach_point, &parent);
if (ret < 0) if (ret < 0)
return ret; return libbpf_err(ret);
req.tc.tcm_parent = parent; req.tc.tcm_parent = parent;
ret = nlattr_add(&req.nh, sizeof(req), TCA_KIND, "bpf", sizeof("bpf")); ret = nlattr_add(&req.nh, sizeof(req), TCA_KIND, "bpf", sizeof("bpf"));
if (ret < 0) if (ret < 0)
return ret; return libbpf_err(ret);
info.opts = opts; info.opts = opts;
ret = libbpf_netlink_send_recv(&req.nh, get_tc_info, NULL, &info); ret = libbpf_netlink_send_recv(&req.nh, get_tc_info, NULL, &info);
if (ret < 0) if (ret < 0)
return ret; return libbpf_err(ret);
if (!info.processed) if (!info.processed)
return -ENOENT; return libbpf_err(-ENOENT);
return ret; return ret;
} }
...@@ -69,23 +69,23 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd, ...@@ -69,23 +69,23 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
err = -errno; err = -errno;
pr_warn("ringbuf: failed to get map info for fd=%d: %d\n", pr_warn("ringbuf: failed to get map info for fd=%d: %d\n",
map_fd, err); map_fd, err);
return err; return libbpf_err(err);
} }
if (info.type != BPF_MAP_TYPE_RINGBUF) { if (info.type != BPF_MAP_TYPE_RINGBUF) {
pr_warn("ringbuf: map fd=%d is not BPF_MAP_TYPE_RINGBUF\n", pr_warn("ringbuf: map fd=%d is not BPF_MAP_TYPE_RINGBUF\n",
map_fd); map_fd);
return -EINVAL; return libbpf_err(-EINVAL);
} }
tmp = libbpf_reallocarray(rb->rings, rb->ring_cnt + 1, sizeof(*rb->rings)); tmp = libbpf_reallocarray(rb->rings, rb->ring_cnt + 1, sizeof(*rb->rings));
if (!tmp) if (!tmp)
return -ENOMEM; return libbpf_err(-ENOMEM);
rb->rings = tmp; rb->rings = tmp;
tmp = libbpf_reallocarray(rb->events, rb->ring_cnt + 1, sizeof(*rb->events)); tmp = libbpf_reallocarray(rb->events, rb->ring_cnt + 1, sizeof(*rb->events));
if (!tmp) if (!tmp)
return -ENOMEM; return libbpf_err(-ENOMEM);
rb->events = tmp; rb->events = tmp;
r = &rb->rings[rb->ring_cnt]; r = &rb->rings[rb->ring_cnt];
...@@ -103,7 +103,7 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd, ...@@ -103,7 +103,7 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
err = -errno; err = -errno;
pr_warn("ringbuf: failed to mmap consumer page for map fd=%d: %d\n", pr_warn("ringbuf: failed to mmap consumer page for map fd=%d: %d\n",
map_fd, err); map_fd, err);
return err; return libbpf_err(err);
} }
r->consumer_pos = tmp; r->consumer_pos = tmp;
...@@ -118,7 +118,7 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd, ...@@ -118,7 +118,7 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
ringbuf_unmap_ring(rb, r); ringbuf_unmap_ring(rb, r);
pr_warn("ringbuf: failed to mmap data pages for map fd=%d: %d\n", pr_warn("ringbuf: failed to mmap data pages for map fd=%d: %d\n",
map_fd, err); map_fd, err);
return err; return libbpf_err(err);
} }
r->producer_pos = tmp; r->producer_pos = tmp;
r->data = tmp + rb->page_size; r->data = tmp + rb->page_size;
...@@ -133,7 +133,7 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd, ...@@ -133,7 +133,7 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd,
ringbuf_unmap_ring(rb, r); ringbuf_unmap_ring(rb, r);
pr_warn("ringbuf: failed to epoll add map fd=%d: %d\n", pr_warn("ringbuf: failed to epoll add map fd=%d: %d\n",
map_fd, err); map_fd, err);
return err; return libbpf_err(err);
} }
rb->ring_cnt++; rb->ring_cnt++;
...@@ -165,11 +165,11 @@ ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx, ...@@ -165,11 +165,11 @@ ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
int err; int err;
if (!OPTS_VALID(opts, ring_buffer_opts)) if (!OPTS_VALID(opts, ring_buffer_opts))
return NULL; return errno = EINVAL, NULL;
rb = calloc(1, sizeof(*rb)); rb = calloc(1, sizeof(*rb));
if (!rb) if (!rb)
return NULL; return errno = ENOMEM, NULL;
rb->page_size = getpagesize(); rb->page_size = getpagesize();
...@@ -188,7 +188,7 @@ ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx, ...@@ -188,7 +188,7 @@ ring_buffer__new(int map_fd, ring_buffer_sample_fn sample_cb, void *ctx,
err_out: err_out:
ring_buffer__free(rb); ring_buffer__free(rb);
return NULL; return errno = -err, NULL;
} }
static inline int roundup_len(__u32 len) static inline int roundup_len(__u32 len)
...@@ -260,7 +260,7 @@ int ring_buffer__consume(struct ring_buffer *rb) ...@@ -260,7 +260,7 @@ int ring_buffer__consume(struct ring_buffer *rb)
err = ringbuf_process_ring(ring); err = ringbuf_process_ring(ring);
if (err < 0) if (err < 0)
return err; return libbpf_err(err);
res += err; res += err;
} }
if (res > INT_MAX) if (res > INT_MAX)
...@@ -279,7 +279,7 @@ int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms) ...@@ -279,7 +279,7 @@ int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms)
cnt = epoll_wait(rb->epoll_fd, rb->events, rb->ring_cnt, timeout_ms); cnt = epoll_wait(rb->epoll_fd, rb->events, rb->ring_cnt, timeout_ms);
if (cnt < 0) if (cnt < 0)
return -errno; return libbpf_err(-errno);
for (i = 0; i < cnt; i++) { for (i = 0; i < cnt; i++) {
__u32 ring_id = rb->events[i].data.fd; __u32 ring_id = rb->events[i].data.fd;
...@@ -287,7 +287,7 @@ int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms) ...@@ -287,7 +287,7 @@ int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms)
err = ringbuf_process_ring(ring); err = ringbuf_process_ring(ring);
if (err < 0) if (err < 0)
return err; return libbpf_err(err);
res += err; res += err;
} }
if (res > INT_MAX) if (res > INT_MAX)
......
...@@ -43,6 +43,7 @@ void setup_libbpf() ...@@ -43,6 +43,7 @@ void setup_libbpf()
{ {
int err; int err;
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
libbpf_set_print(libbpf_print_fn); libbpf_set_print(libbpf_print_fn);
err = bump_memlock_rlimit(); err = bump_memlock_rlimit();
......
...@@ -65,7 +65,7 @@ static void attach_bpf(struct bpf_program *prog) ...@@ -65,7 +65,7 @@ static void attach_bpf(struct bpf_program *prog)
struct bpf_link *link; struct bpf_link *link;
link = bpf_program__attach(prog); link = bpf_program__attach(prog);
if (IS_ERR(link)) { if (!link) {
fprintf(stderr, "failed to attach program!\n"); fprintf(stderr, "failed to attach program!\n");
exit(1); exit(1);
} }
......
...@@ -181,7 +181,7 @@ static void ringbuf_libbpf_setup() ...@@ -181,7 +181,7 @@ static void ringbuf_libbpf_setup()
} }
link = bpf_program__attach(ctx->skel->progs.bench_ringbuf); link = bpf_program__attach(ctx->skel->progs.bench_ringbuf);
if (IS_ERR(link)) { if (!link) {
fprintf(stderr, "failed to attach program!\n"); fprintf(stderr, "failed to attach program!\n");
exit(1); exit(1);
} }
...@@ -271,7 +271,7 @@ static void ringbuf_custom_setup() ...@@ -271,7 +271,7 @@ static void ringbuf_custom_setup()
} }
link = bpf_program__attach(ctx->skel->progs.bench_ringbuf); link = bpf_program__attach(ctx->skel->progs.bench_ringbuf);
if (IS_ERR(link)) { if (!link) {
fprintf(stderr, "failed to attach program\n"); fprintf(stderr, "failed to attach program\n");
exit(1); exit(1);
} }
...@@ -430,7 +430,7 @@ static void perfbuf_libbpf_setup() ...@@ -430,7 +430,7 @@ static void perfbuf_libbpf_setup()
} }
link = bpf_program__attach(ctx->skel->progs.bench_perfbuf); link = bpf_program__attach(ctx->skel->progs.bench_perfbuf);
if (IS_ERR(link)) { if (!link) {
fprintf(stderr, "failed to attach program\n"); fprintf(stderr, "failed to attach program\n");
exit(1); exit(1);
} }
......
...@@ -60,7 +60,7 @@ static void attach_bpf(struct bpf_program *prog) ...@@ -60,7 +60,7 @@ static void attach_bpf(struct bpf_program *prog)
struct bpf_link *link; struct bpf_link *link;
link = bpf_program__attach(prog); link = bpf_program__attach(prog);
if (IS_ERR(link)) { if (!link) {
fprintf(stderr, "failed to attach program!\n"); fprintf(stderr, "failed to attach program!\n");
exit(1); exit(1);
} }
......
...@@ -85,16 +85,14 @@ void test_attach_probe(void) ...@@ -85,16 +85,14 @@ void test_attach_probe(void)
kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe, kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe,
false /* retprobe */, false /* retprobe */,
SYS_NANOSLEEP_KPROBE_NAME); SYS_NANOSLEEP_KPROBE_NAME);
if (CHECK(IS_ERR(kprobe_link), "attach_kprobe", if (!ASSERT_OK_PTR(kprobe_link, "attach_kprobe"))
"err %ld\n", PTR_ERR(kprobe_link)))
goto cleanup; goto cleanup;
skel->links.handle_kprobe = kprobe_link; skel->links.handle_kprobe = kprobe_link;
kretprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kretprobe, kretprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kretprobe,
true /* retprobe */, true /* retprobe */,
SYS_NANOSLEEP_KPROBE_NAME); SYS_NANOSLEEP_KPROBE_NAME);
if (CHECK(IS_ERR(kretprobe_link), "attach_kretprobe", if (!ASSERT_OK_PTR(kretprobe_link, "attach_kretprobe"))
"err %ld\n", PTR_ERR(kretprobe_link)))
goto cleanup; goto cleanup;
skel->links.handle_kretprobe = kretprobe_link; skel->links.handle_kretprobe = kretprobe_link;
...@@ -103,8 +101,7 @@ void test_attach_probe(void) ...@@ -103,8 +101,7 @@ void test_attach_probe(void)
0 /* self pid */, 0 /* self pid */,
"/proc/self/exe", "/proc/self/exe",
uprobe_offset); uprobe_offset);
if (CHECK(IS_ERR(uprobe_link), "attach_uprobe", if (!ASSERT_OK_PTR(uprobe_link, "attach_uprobe"))
"err %ld\n", PTR_ERR(uprobe_link)))
goto cleanup; goto cleanup;
skel->links.handle_uprobe = uprobe_link; skel->links.handle_uprobe = uprobe_link;
...@@ -113,8 +110,7 @@ void test_attach_probe(void) ...@@ -113,8 +110,7 @@ void test_attach_probe(void)
-1 /* any pid */, -1 /* any pid */,
"/proc/self/exe", "/proc/self/exe",
uprobe_offset); uprobe_offset);
if (CHECK(IS_ERR(uretprobe_link), "attach_uretprobe", if (!ASSERT_OK_PTR(uretprobe_link, "attach_uretprobe"))
"err %ld\n", PTR_ERR(uretprobe_link)))
goto cleanup; goto cleanup;
skel->links.handle_uretprobe = uretprobe_link; skel->links.handle_uretprobe = uretprobe_link;
......
...@@ -47,7 +47,7 @@ static void do_dummy_read(struct bpf_program *prog) ...@@ -47,7 +47,7 @@ static void do_dummy_read(struct bpf_program *prog)
int iter_fd, len; int iter_fd, len;
link = bpf_program__attach_iter(prog, NULL); link = bpf_program__attach_iter(prog, NULL);
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) if (!ASSERT_OK_PTR(link, "attach_iter"))
return; return;
iter_fd = bpf_iter_create(bpf_link__fd(link)); iter_fd = bpf_iter_create(bpf_link__fd(link));
...@@ -201,7 +201,7 @@ static int do_btf_read(struct bpf_iter_task_btf *skel) ...@@ -201,7 +201,7 @@ static int do_btf_read(struct bpf_iter_task_btf *skel)
int ret = 0; int ret = 0;
link = bpf_program__attach_iter(prog, NULL); link = bpf_program__attach_iter(prog, NULL);
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) if (!ASSERT_OK_PTR(link, "attach_iter"))
return ret; return ret;
iter_fd = bpf_iter_create(bpf_link__fd(link)); iter_fd = bpf_iter_create(bpf_link__fd(link));
...@@ -396,7 +396,7 @@ static void test_file_iter(void) ...@@ -396,7 +396,7 @@ static void test_file_iter(void)
return; return;
link = bpf_program__attach_iter(skel1->progs.dump_task, NULL); link = bpf_program__attach_iter(skel1->progs.dump_task, NULL);
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) if (!ASSERT_OK_PTR(link, "attach_iter"))
goto out; goto out;
/* unlink this path if it exists. */ /* unlink this path if it exists. */
...@@ -502,7 +502,7 @@ static void test_overflow(bool test_e2big_overflow, bool ret1) ...@@ -502,7 +502,7 @@ static void test_overflow(bool test_e2big_overflow, bool ret1)
skel->bss->map2_id = map_info.id; skel->bss->map2_id = map_info.id;
link = bpf_program__attach_iter(skel->progs.dump_bpf_map, NULL); link = bpf_program__attach_iter(skel->progs.dump_bpf_map, NULL);
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) if (!ASSERT_OK_PTR(link, "attach_iter"))
goto free_map2; goto free_map2;
iter_fd = bpf_iter_create(bpf_link__fd(link)); iter_fd = bpf_iter_create(bpf_link__fd(link));
...@@ -607,14 +607,12 @@ static void test_bpf_hash_map(void) ...@@ -607,14 +607,12 @@ static void test_bpf_hash_map(void)
opts.link_info = &linfo; opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo); opts.link_info_len = sizeof(linfo);
link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
if (CHECK(!IS_ERR(link), "attach_iter", if (!ASSERT_ERR_PTR(link, "attach_iter"))
"attach_iter for hashmap2 unexpected succeeded\n"))
goto out; goto out;
linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap3); linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap3);
link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
if (CHECK(!IS_ERR(link), "attach_iter", if (!ASSERT_ERR_PTR(link, "attach_iter"))
"attach_iter for hashmap3 unexpected succeeded\n"))
goto out; goto out;
/* hashmap1 should be good, update map values here */ /* hashmap1 should be good, update map values here */
...@@ -636,7 +634,7 @@ static void test_bpf_hash_map(void) ...@@ -636,7 +634,7 @@ static void test_bpf_hash_map(void)
linfo.map.map_fd = map_fd; linfo.map.map_fd = map_fd;
link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) if (!ASSERT_OK_PTR(link, "attach_iter"))
goto out; goto out;
iter_fd = bpf_iter_create(bpf_link__fd(link)); iter_fd = bpf_iter_create(bpf_link__fd(link));
...@@ -727,7 +725,7 @@ static void test_bpf_percpu_hash_map(void) ...@@ -727,7 +725,7 @@ static void test_bpf_percpu_hash_map(void)
opts.link_info = &linfo; opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo); opts.link_info_len = sizeof(linfo);
link = bpf_program__attach_iter(skel->progs.dump_bpf_percpu_hash_map, &opts); link = bpf_program__attach_iter(skel->progs.dump_bpf_percpu_hash_map, &opts);
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) if (!ASSERT_OK_PTR(link, "attach_iter"))
goto out; goto out;
iter_fd = bpf_iter_create(bpf_link__fd(link)); iter_fd = bpf_iter_create(bpf_link__fd(link));
...@@ -798,7 +796,7 @@ static void test_bpf_array_map(void) ...@@ -798,7 +796,7 @@ static void test_bpf_array_map(void)
opts.link_info = &linfo; opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo); opts.link_info_len = sizeof(linfo);
link = bpf_program__attach_iter(skel->progs.dump_bpf_array_map, &opts); link = bpf_program__attach_iter(skel->progs.dump_bpf_array_map, &opts);
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) if (!ASSERT_OK_PTR(link, "attach_iter"))
goto out; goto out;
iter_fd = bpf_iter_create(bpf_link__fd(link)); iter_fd = bpf_iter_create(bpf_link__fd(link));
...@@ -894,7 +892,7 @@ static void test_bpf_percpu_array_map(void) ...@@ -894,7 +892,7 @@ static void test_bpf_percpu_array_map(void)
opts.link_info = &linfo; opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo); opts.link_info_len = sizeof(linfo);
link = bpf_program__attach_iter(skel->progs.dump_bpf_percpu_array_map, &opts); link = bpf_program__attach_iter(skel->progs.dump_bpf_percpu_array_map, &opts);
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) if (!ASSERT_OK_PTR(link, "attach_iter"))
goto out; goto out;
iter_fd = bpf_iter_create(bpf_link__fd(link)); iter_fd = bpf_iter_create(bpf_link__fd(link));
...@@ -957,7 +955,7 @@ static void test_bpf_sk_storage_delete(void) ...@@ -957,7 +955,7 @@ static void test_bpf_sk_storage_delete(void)
opts.link_info_len = sizeof(linfo); opts.link_info_len = sizeof(linfo);
link = bpf_program__attach_iter(skel->progs.delete_bpf_sk_storage_map, link = bpf_program__attach_iter(skel->progs.delete_bpf_sk_storage_map,
&opts); &opts);
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) if (!ASSERT_OK_PTR(link, "attach_iter"))
goto out; goto out;
iter_fd = bpf_iter_create(bpf_link__fd(link)); iter_fd = bpf_iter_create(bpf_link__fd(link));
...@@ -1075,7 +1073,7 @@ static void test_bpf_sk_storage_map(void) ...@@ -1075,7 +1073,7 @@ static void test_bpf_sk_storage_map(void)
opts.link_info = &linfo; opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo); opts.link_info_len = sizeof(linfo);
link = bpf_program__attach_iter(skel->progs.dump_bpf_sk_storage_map, &opts); link = bpf_program__attach_iter(skel->progs.dump_bpf_sk_storage_map, &opts);
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) if (!ASSERT_OK_PTR(link, "attach_iter"))
goto out; goto out;
iter_fd = bpf_iter_create(bpf_link__fd(link)); iter_fd = bpf_iter_create(bpf_link__fd(link));
...@@ -1128,7 +1126,7 @@ static void test_rdonly_buf_out_of_bound(void) ...@@ -1128,7 +1126,7 @@ static void test_rdonly_buf_out_of_bound(void)
opts.link_info = &linfo; opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo); opts.link_info_len = sizeof(linfo);
link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
if (CHECK(!IS_ERR(link), "attach_iter", "unexpected success\n")) if (!ASSERT_ERR_PTR(link, "attach_iter"))
bpf_link__destroy(link); bpf_link__destroy(link);
bpf_iter_test_kern5__destroy(skel); bpf_iter_test_kern5__destroy(skel);
...@@ -1186,8 +1184,7 @@ static void test_task_vma(void) ...@@ -1186,8 +1184,7 @@ static void test_task_vma(void)
skel->links.proc_maps = bpf_program__attach_iter( skel->links.proc_maps = bpf_program__attach_iter(
skel->progs.proc_maps, NULL); skel->progs.proc_maps, NULL);
if (CHECK(IS_ERR(skel->links.proc_maps), "bpf_program__attach_iter", if (!ASSERT_OK_PTR(skel->links.proc_maps, "bpf_program__attach_iter")) {
"attach iterator failed\n")) {
skel->links.proc_maps = NULL; skel->links.proc_maps = NULL;
goto out; goto out;
} }
......
...@@ -82,7 +82,7 @@ static void *server(void *arg) ...@@ -82,7 +82,7 @@ static void *server(void *arg)
bytes, total_bytes, nr_sent, errno); bytes, total_bytes, nr_sent, errno);
done: done:
if (fd != -1) if (fd >= 0)
close(fd); close(fd);
if (err) { if (err) {
WRITE_ONCE(stop, 1); WRITE_ONCE(stop, 1);
...@@ -191,8 +191,7 @@ static void test_cubic(void) ...@@ -191,8 +191,7 @@ static void test_cubic(void)
return; return;
link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic); link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);
if (CHECK(IS_ERR(link), "bpf_map__attach_struct_ops", "err:%ld\n", if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
PTR_ERR(link))) {
bpf_cubic__destroy(cubic_skel); bpf_cubic__destroy(cubic_skel);
return; return;
} }
...@@ -213,8 +212,7 @@ static void test_dctcp(void) ...@@ -213,8 +212,7 @@ static void test_dctcp(void)
return; return;
link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp); link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp);
if (CHECK(IS_ERR(link), "bpf_map__attach_struct_ops", "err:%ld\n", if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
PTR_ERR(link))) {
bpf_dctcp__destroy(dctcp_skel); bpf_dctcp__destroy(dctcp_skel);
return; return;
} }
......
...@@ -32,8 +32,9 @@ static int btf_dump_all_types(const struct btf *btf, ...@@ -32,8 +32,9 @@ static int btf_dump_all_types(const struct btf *btf,
int err = 0, id; int err = 0, id;
d = btf_dump__new(btf, NULL, opts, btf_dump_printf); d = btf_dump__new(btf, NULL, opts, btf_dump_printf);
if (IS_ERR(d)) err = libbpf_get_error(d);
return PTR_ERR(d); if (err)
return err;
for (id = 1; id <= type_cnt; id++) { for (id = 1; id <= type_cnt; id++) {
err = btf_dump__dump_type(d, id); err = btf_dump__dump_type(d, id);
...@@ -56,8 +57,7 @@ static int test_btf_dump_case(int n, struct btf_dump_test_case *t) ...@@ -56,8 +57,7 @@ static int test_btf_dump_case(int n, struct btf_dump_test_case *t)
snprintf(test_file, sizeof(test_file), "%s.o", t->file); snprintf(test_file, sizeof(test_file), "%s.o", t->file);
btf = btf__parse_elf(test_file, NULL); btf = btf__parse_elf(test_file, NULL);
if (CHECK(IS_ERR(btf), "btf_parse_elf", if (!ASSERT_OK_PTR(btf, "btf_parse_elf")) {
"failed to load test BTF: %ld\n", PTR_ERR(btf))) {
err = -PTR_ERR(btf); err = -PTR_ERR(btf);
btf = NULL; btf = NULL;
goto done; goto done;
......
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
#include <bpf/btf.h> #include <bpf/btf.h>
#include "btf_helpers.h" #include "btf_helpers.h"
static int duration = 0;
void test_btf_write() { void test_btf_write() {
const struct btf_var_secinfo *vi; const struct btf_var_secinfo *vi;
const struct btf_type *t; const struct btf_type *t;
...@@ -16,7 +14,7 @@ void test_btf_write() { ...@@ -16,7 +14,7 @@ void test_btf_write() {
int id, err, str_off; int id, err, str_off;
btf = btf__new_empty(); btf = btf__new_empty();
if (CHECK(IS_ERR(btf), "new_empty", "failed: %ld\n", PTR_ERR(btf))) if (!ASSERT_OK_PTR(btf, "new_empty"))
return; return;
str_off = btf__find_str(btf, "int"); str_off = btf__find_str(btf, "int");
......
...@@ -102,8 +102,7 @@ static void test_egress_only(int parent_cgroup_fd, int child_cgroup_fd) ...@@ -102,8 +102,7 @@ static void test_egress_only(int parent_cgroup_fd, int child_cgroup_fd)
*/ */
parent_link = bpf_program__attach_cgroup(obj->progs.egress, parent_link = bpf_program__attach_cgroup(obj->progs.egress,
parent_cgroup_fd); parent_cgroup_fd);
if (CHECK(IS_ERR(parent_link), "parent-cg-attach", if (!ASSERT_OK_PTR(parent_link, "parent-cg-attach"))
"err %ld", PTR_ERR(parent_link)))
goto close_bpf_object; goto close_bpf_object;
err = connect_send(CHILD_CGROUP); err = connect_send(CHILD_CGROUP);
if (CHECK(err, "first-connect-send", "errno %d", errno)) if (CHECK(err, "first-connect-send", "errno %d", errno))
...@@ -126,8 +125,7 @@ static void test_egress_only(int parent_cgroup_fd, int child_cgroup_fd) ...@@ -126,8 +125,7 @@ static void test_egress_only(int parent_cgroup_fd, int child_cgroup_fd)
*/ */
child_link = bpf_program__attach_cgroup(obj->progs.egress, child_link = bpf_program__attach_cgroup(obj->progs.egress,
child_cgroup_fd); child_cgroup_fd);
if (CHECK(IS_ERR(child_link), "child-cg-attach", if (!ASSERT_OK_PTR(child_link, "child-cg-attach"))
"err %ld", PTR_ERR(child_link)))
goto close_bpf_object; goto close_bpf_object;
err = connect_send(CHILD_CGROUP); err = connect_send(CHILD_CGROUP);
if (CHECK(err, "second-connect-send", "errno %d", errno)) if (CHECK(err, "second-connect-send", "errno %d", errno))
...@@ -147,10 +145,8 @@ static void test_egress_only(int parent_cgroup_fd, int child_cgroup_fd) ...@@ -147,10 +145,8 @@ static void test_egress_only(int parent_cgroup_fd, int child_cgroup_fd)
goto close_bpf_object; goto close_bpf_object;
close_bpf_object: close_bpf_object:
if (!IS_ERR(parent_link)) bpf_link__destroy(parent_link);
bpf_link__destroy(parent_link); bpf_link__destroy(child_link);
if (!IS_ERR(child_link))
bpf_link__destroy(child_link);
cg_storage_multi_egress_only__destroy(obj); cg_storage_multi_egress_only__destroy(obj);
} }
...@@ -176,18 +172,15 @@ static void test_isolated(int parent_cgroup_fd, int child_cgroup_fd) ...@@ -176,18 +172,15 @@ static void test_isolated(int parent_cgroup_fd, int child_cgroup_fd)
*/ */
parent_egress1_link = bpf_program__attach_cgroup(obj->progs.egress1, parent_egress1_link = bpf_program__attach_cgroup(obj->progs.egress1,
parent_cgroup_fd); parent_cgroup_fd);
if (CHECK(IS_ERR(parent_egress1_link), "parent-egress1-cg-attach", if (!ASSERT_OK_PTR(parent_egress1_link, "parent-egress1-cg-attach"))
"err %ld", PTR_ERR(parent_egress1_link)))
goto close_bpf_object; goto close_bpf_object;
parent_egress2_link = bpf_program__attach_cgroup(obj->progs.egress2, parent_egress2_link = bpf_program__attach_cgroup(obj->progs.egress2,
parent_cgroup_fd); parent_cgroup_fd);
if (CHECK(IS_ERR(parent_egress2_link), "parent-egress2-cg-attach", if (!ASSERT_OK_PTR(parent_egress2_link, "parent-egress2-cg-attach"))
"err %ld", PTR_ERR(parent_egress2_link)))
goto close_bpf_object; goto close_bpf_object;
parent_ingress_link = bpf_program__attach_cgroup(obj->progs.ingress, parent_ingress_link = bpf_program__attach_cgroup(obj->progs.ingress,
parent_cgroup_fd); parent_cgroup_fd);
if (CHECK(IS_ERR(parent_ingress_link), "parent-ingress-cg-attach", if (!ASSERT_OK_PTR(parent_ingress_link, "parent-ingress-cg-attach"))
"err %ld", PTR_ERR(parent_ingress_link)))
goto close_bpf_object; goto close_bpf_object;
err = connect_send(CHILD_CGROUP); err = connect_send(CHILD_CGROUP);
if (CHECK(err, "first-connect-send", "errno %d", errno)) if (CHECK(err, "first-connect-send", "errno %d", errno))
...@@ -221,18 +214,15 @@ static void test_isolated(int parent_cgroup_fd, int child_cgroup_fd) ...@@ -221,18 +214,15 @@ static void test_isolated(int parent_cgroup_fd, int child_cgroup_fd)
*/ */
child_egress1_link = bpf_program__attach_cgroup(obj->progs.egress1, child_egress1_link = bpf_program__attach_cgroup(obj->progs.egress1,
child_cgroup_fd); child_cgroup_fd);
if (CHECK(IS_ERR(child_egress1_link), "child-egress1-cg-attach", if (!ASSERT_OK_PTR(child_egress1_link, "child-egress1-cg-attach"))
"err %ld", PTR_ERR(child_egress1_link)))
goto close_bpf_object; goto close_bpf_object;
child_egress2_link = bpf_program__attach_cgroup(obj->progs.egress2, child_egress2_link = bpf_program__attach_cgroup(obj->progs.egress2,
child_cgroup_fd); child_cgroup_fd);
if (CHECK(IS_ERR(child_egress2_link), "child-egress2-cg-attach", if (!ASSERT_OK_PTR(child_egress2_link, "child-egress2-cg-attach"))
"err %ld", PTR_ERR(child_egress2_link)))
goto close_bpf_object; goto close_bpf_object;
child_ingress_link = bpf_program__attach_cgroup(obj->progs.ingress, child_ingress_link = bpf_program__attach_cgroup(obj->progs.ingress,
child_cgroup_fd); child_cgroup_fd);
if (CHECK(IS_ERR(child_ingress_link), "child-ingress-cg-attach", if (!ASSERT_OK_PTR(child_ingress_link, "child-ingress-cg-attach"))
"err %ld", PTR_ERR(child_ingress_link)))
goto close_bpf_object; goto close_bpf_object;
err = connect_send(CHILD_CGROUP); err = connect_send(CHILD_CGROUP);
if (CHECK(err, "second-connect-send", "errno %d", errno)) if (CHECK(err, "second-connect-send", "errno %d", errno))
...@@ -264,18 +254,12 @@ static void test_isolated(int parent_cgroup_fd, int child_cgroup_fd) ...@@ -264,18 +254,12 @@ static void test_isolated(int parent_cgroup_fd, int child_cgroup_fd)
goto close_bpf_object; goto close_bpf_object;
close_bpf_object: close_bpf_object:
if (!IS_ERR(parent_egress1_link)) bpf_link__destroy(parent_egress1_link);
bpf_link__destroy(parent_egress1_link); bpf_link__destroy(parent_egress2_link);
if (!IS_ERR(parent_egress2_link)) bpf_link__destroy(parent_ingress_link);
bpf_link__destroy(parent_egress2_link); bpf_link__destroy(child_egress1_link);
if (!IS_ERR(parent_ingress_link)) bpf_link__destroy(child_egress2_link);
bpf_link__destroy(parent_ingress_link); bpf_link__destroy(child_ingress_link);
if (!IS_ERR(child_egress1_link))
bpf_link__destroy(child_egress1_link);
if (!IS_ERR(child_egress2_link))
bpf_link__destroy(child_egress2_link);
if (!IS_ERR(child_ingress_link))
bpf_link__destroy(child_ingress_link);
cg_storage_multi_isolated__destroy(obj); cg_storage_multi_isolated__destroy(obj);
} }
...@@ -301,18 +285,15 @@ static void test_shared(int parent_cgroup_fd, int child_cgroup_fd) ...@@ -301,18 +285,15 @@ static void test_shared(int parent_cgroup_fd, int child_cgroup_fd)
*/ */
parent_egress1_link = bpf_program__attach_cgroup(obj->progs.egress1, parent_egress1_link = bpf_program__attach_cgroup(obj->progs.egress1,
parent_cgroup_fd); parent_cgroup_fd);
if (CHECK(IS_ERR(parent_egress1_link), "parent-egress1-cg-attach", if (!ASSERT_OK_PTR(parent_egress1_link, "parent-egress1-cg-attach"))
"err %ld", PTR_ERR(parent_egress1_link)))
goto close_bpf_object; goto close_bpf_object;
parent_egress2_link = bpf_program__attach_cgroup(obj->progs.egress2, parent_egress2_link = bpf_program__attach_cgroup(obj->progs.egress2,
parent_cgroup_fd); parent_cgroup_fd);
if (CHECK(IS_ERR(parent_egress2_link), "parent-egress2-cg-attach", if (!ASSERT_OK_PTR(parent_egress2_link, "parent-egress2-cg-attach"))
"err %ld", PTR_ERR(parent_egress2_link)))
goto close_bpf_object; goto close_bpf_object;
parent_ingress_link = bpf_program__attach_cgroup(obj->progs.ingress, parent_ingress_link = bpf_program__attach_cgroup(obj->progs.ingress,
parent_cgroup_fd); parent_cgroup_fd);
if (CHECK(IS_ERR(parent_ingress_link), "parent-ingress-cg-attach", if (!ASSERT_OK_PTR(parent_ingress_link, "parent-ingress-cg-attach"))
"err %ld", PTR_ERR(parent_ingress_link)))
goto close_bpf_object; goto close_bpf_object;
err = connect_send(CHILD_CGROUP); err = connect_send(CHILD_CGROUP);
if (CHECK(err, "first-connect-send", "errno %d", errno)) if (CHECK(err, "first-connect-send", "errno %d", errno))
...@@ -338,18 +319,15 @@ static void test_shared(int parent_cgroup_fd, int child_cgroup_fd) ...@@ -338,18 +319,15 @@ static void test_shared(int parent_cgroup_fd, int child_cgroup_fd)
*/ */
child_egress1_link = bpf_program__attach_cgroup(obj->progs.egress1, child_egress1_link = bpf_program__attach_cgroup(obj->progs.egress1,
child_cgroup_fd); child_cgroup_fd);
if (CHECK(IS_ERR(child_egress1_link), "child-egress1-cg-attach", if (!ASSERT_OK_PTR(child_egress1_link, "child-egress1-cg-attach"))
"err %ld", PTR_ERR(child_egress1_link)))
goto close_bpf_object; goto close_bpf_object;
child_egress2_link = bpf_program__attach_cgroup(obj->progs.egress2, child_egress2_link = bpf_program__attach_cgroup(obj->progs.egress2,
child_cgroup_fd); child_cgroup_fd);
if (CHECK(IS_ERR(child_egress2_link), "child-egress2-cg-attach", if (!ASSERT_OK_PTR(child_egress2_link, "child-egress2-cg-attach"))
"err %ld", PTR_ERR(child_egress2_link)))
goto close_bpf_object; goto close_bpf_object;
child_ingress_link = bpf_program__attach_cgroup(obj->progs.ingress, child_ingress_link = bpf_program__attach_cgroup(obj->progs.ingress,
child_cgroup_fd); child_cgroup_fd);
if (CHECK(IS_ERR(child_ingress_link), "child-ingress-cg-attach", if (!ASSERT_OK_PTR(child_ingress_link, "child-ingress-cg-attach"))
"err %ld", PTR_ERR(child_ingress_link)))
goto close_bpf_object; goto close_bpf_object;
err = connect_send(CHILD_CGROUP); err = connect_send(CHILD_CGROUP);
if (CHECK(err, "second-connect-send", "errno %d", errno)) if (CHECK(err, "second-connect-send", "errno %d", errno))
...@@ -375,18 +353,12 @@ static void test_shared(int parent_cgroup_fd, int child_cgroup_fd) ...@@ -375,18 +353,12 @@ static void test_shared(int parent_cgroup_fd, int child_cgroup_fd)
goto close_bpf_object; goto close_bpf_object;
close_bpf_object: close_bpf_object:
if (!IS_ERR(parent_egress1_link)) bpf_link__destroy(parent_egress1_link);
bpf_link__destroy(parent_egress1_link); bpf_link__destroy(parent_egress2_link);
if (!IS_ERR(parent_egress2_link)) bpf_link__destroy(parent_ingress_link);
bpf_link__destroy(parent_egress2_link); bpf_link__destroy(child_egress1_link);
if (!IS_ERR(parent_ingress_link)) bpf_link__destroy(child_egress2_link);
bpf_link__destroy(parent_ingress_link); bpf_link__destroy(child_ingress_link);
if (!IS_ERR(child_egress1_link))
bpf_link__destroy(child_egress1_link);
if (!IS_ERR(child_egress2_link))
bpf_link__destroy(child_egress2_link);
if (!IS_ERR(child_ingress_link))
bpf_link__destroy(child_ingress_link);
cg_storage_multi_shared__destroy(obj); cg_storage_multi_shared__destroy(obj);
} }
......
...@@ -167,7 +167,7 @@ void test_cgroup_attach_multi(void) ...@@ -167,7 +167,7 @@ void test_cgroup_attach_multi(void)
prog_cnt = 2; prog_cnt = 2;
CHECK_FAIL(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, CHECK_FAIL(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS,
BPF_F_QUERY_EFFECTIVE, &attach_flags, BPF_F_QUERY_EFFECTIVE, &attach_flags,
prog_ids, &prog_cnt) != -1); prog_ids, &prog_cnt) >= 0);
CHECK_FAIL(errno != ENOSPC); CHECK_FAIL(errno != ENOSPC);
CHECK_FAIL(prog_cnt != 4); CHECK_FAIL(prog_cnt != 4);
/* check that prog_ids are returned even when buffer is too small */ /* check that prog_ids are returned even when buffer is too small */
......
...@@ -65,8 +65,7 @@ void test_cgroup_link(void) ...@@ -65,8 +65,7 @@ void test_cgroup_link(void)
for (i = 0; i < cg_nr; i++) { for (i = 0; i < cg_nr; i++) {
links[i] = bpf_program__attach_cgroup(skel->progs.egress, links[i] = bpf_program__attach_cgroup(skel->progs.egress,
cgs[i].fd); cgs[i].fd);
if (CHECK(IS_ERR(links[i]), "cg_attach", "i: %d, err: %ld\n", if (!ASSERT_OK_PTR(links[i], "cg_attach"))
i, PTR_ERR(links[i])))
goto cleanup; goto cleanup;
} }
...@@ -121,8 +120,7 @@ void test_cgroup_link(void) ...@@ -121,8 +120,7 @@ void test_cgroup_link(void)
links[last_cg] = bpf_program__attach_cgroup(skel->progs.egress, links[last_cg] = bpf_program__attach_cgroup(skel->progs.egress,
cgs[last_cg].fd); cgs[last_cg].fd);
if (CHECK(IS_ERR(links[last_cg]), "cg_attach", "err: %ld\n", if (!ASSERT_OK_PTR(links[last_cg], "cg_attach"))
PTR_ERR(links[last_cg])))
goto cleanup; goto cleanup;
ping_and_check(cg_nr + 1, 0); ping_and_check(cg_nr + 1, 0);
...@@ -147,7 +145,7 @@ void test_cgroup_link(void) ...@@ -147,7 +145,7 @@ void test_cgroup_link(void)
/* attempt to mix in with multi-attach bpf_link */ /* attempt to mix in with multi-attach bpf_link */
tmp_link = bpf_program__attach_cgroup(skel->progs.egress, tmp_link = bpf_program__attach_cgroup(skel->progs.egress,
cgs[last_cg].fd); cgs[last_cg].fd);
if (CHECK(!IS_ERR(tmp_link), "cg_attach_fail", "unexpected success!\n")) { if (!ASSERT_ERR_PTR(tmp_link, "cg_attach_fail")) {
bpf_link__destroy(tmp_link); bpf_link__destroy(tmp_link);
goto cleanup; goto cleanup;
} }
...@@ -165,8 +163,7 @@ void test_cgroup_link(void) ...@@ -165,8 +163,7 @@ void test_cgroup_link(void)
/* attach back link-based one */ /* attach back link-based one */
links[last_cg] = bpf_program__attach_cgroup(skel->progs.egress, links[last_cg] = bpf_program__attach_cgroup(skel->progs.egress,
cgs[last_cg].fd); cgs[last_cg].fd);
if (CHECK(IS_ERR(links[last_cg]), "cg_attach", "err: %ld\n", if (!ASSERT_OK_PTR(links[last_cg], "cg_attach"))
PTR_ERR(links[last_cg])))
goto cleanup; goto cleanup;
ping_and_check(cg_nr, 0); ping_and_check(cg_nr, 0);
...@@ -249,8 +246,7 @@ void test_cgroup_link(void) ...@@ -249,8 +246,7 @@ void test_cgroup_link(void)
BPF_CGROUP_INET_EGRESS); BPF_CGROUP_INET_EGRESS);
for (i = 0; i < cg_nr; i++) { for (i = 0; i < cg_nr; i++) {
if (!IS_ERR(links[i])) bpf_link__destroy(links[i]);
bpf_link__destroy(links[i]);
} }
test_cgroup_link__destroy(skel); test_cgroup_link__destroy(skel);
......
...@@ -60,7 +60,7 @@ static void run_cgroup_bpf_test(const char *cg_path, int out_sk) ...@@ -60,7 +60,7 @@ static void run_cgroup_bpf_test(const char *cg_path, int out_sk)
goto cleanup; goto cleanup;
link = bpf_program__attach_cgroup(skel->progs.ingress_lookup, cgfd); link = bpf_program__attach_cgroup(skel->progs.ingress_lookup, cgfd);
if (CHECK(IS_ERR(link), "cgroup_attach", "err: %ld\n", PTR_ERR(link))) if (!ASSERT_OK_PTR(link, "cgroup_attach"))
goto cleanup; goto cleanup;
run_lookup_test(&skel->bss->g_serv_port, out_sk); run_lookup_test(&skel->bss->g_serv_port, out_sk);
......
...@@ -53,7 +53,7 @@ static void test_check_mtu_xdp_attach(void) ...@@ -53,7 +53,7 @@ static void test_check_mtu_xdp_attach(void)
prog = skel->progs.xdp_use_helper_basic; prog = skel->progs.xdp_use_helper_basic;
link = bpf_program__attach_xdp(prog, IFINDEX_LO); link = bpf_program__attach_xdp(prog, IFINDEX_LO);
if (CHECK(IS_ERR(link), "link_attach", "failed: %ld\n", PTR_ERR(link))) if (!ASSERT_OK_PTR(link, "link_attach"))
goto out; goto out;
skel->links.xdp_use_helper_basic = link; skel->links.xdp_use_helper_basic = link;
......
...@@ -369,8 +369,7 @@ static int setup_type_id_case_local(struct core_reloc_test_case *test) ...@@ -369,8 +369,7 @@ static int setup_type_id_case_local(struct core_reloc_test_case *test)
const char *name; const char *name;
int i; int i;
if (CHECK(IS_ERR(local_btf), "local_btf", "failed: %ld\n", PTR_ERR(local_btf)) || if (!ASSERT_OK_PTR(local_btf, "local_btf") || !ASSERT_OK_PTR(targ_btf, "targ_btf")) {
CHECK(IS_ERR(targ_btf), "targ_btf", "failed: %ld\n", PTR_ERR(targ_btf))) {
btf__free(local_btf); btf__free(local_btf);
btf__free(targ_btf); btf__free(targ_btf);
return -EINVAL; return -EINVAL;
...@@ -848,8 +847,7 @@ void test_core_reloc(void) ...@@ -848,8 +847,7 @@ void test_core_reloc(void)
} }
obj = bpf_object__open_file(test_case->bpf_obj_file, NULL); obj = bpf_object__open_file(test_case->bpf_obj_file, NULL);
if (CHECK(IS_ERR(obj), "obj_open", "failed to open '%s': %ld\n", if (!ASSERT_OK_PTR(obj, "obj_open"))
test_case->bpf_obj_file, PTR_ERR(obj)))
continue; continue;
probe_name = "raw_tracepoint/sys_enter"; probe_name = "raw_tracepoint/sys_enter";
...@@ -899,8 +897,7 @@ void test_core_reloc(void) ...@@ -899,8 +897,7 @@ void test_core_reloc(void)
data->my_pid_tgid = my_pid_tgid; data->my_pid_tgid = my_pid_tgid;
link = bpf_program__attach_raw_tracepoint(prog, tp_name); link = bpf_program__attach_raw_tracepoint(prog, tp_name);
if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n", if (!ASSERT_OK_PTR(link, "attach_raw_tp"))
PTR_ERR(link)))
goto cleanup; goto cleanup;
/* trigger test run */ /* trigger test run */
...@@ -941,10 +938,8 @@ void test_core_reloc(void) ...@@ -941,10 +938,8 @@ void test_core_reloc(void)
CHECK_FAIL(munmap(mmap_data, mmap_sz)); CHECK_FAIL(munmap(mmap_data, mmap_sz));
mmap_data = NULL; mmap_data = NULL;
} }
if (!IS_ERR_OR_NULL(link)) { bpf_link__destroy(link);
bpf_link__destroy(link); link = NULL;
link = NULL;
}
bpf_object__close(obj); bpf_object__close(obj);
} }
} }
...@@ -146,10 +146,8 @@ static void test_fexit_bpf2bpf_common(const char *obj_file, ...@@ -146,10 +146,8 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,
close_prog: close_prog:
for (i = 0; i < prog_cnt; i++) for (i = 0; i < prog_cnt; i++)
if (!IS_ERR_OR_NULL(link[i])) bpf_link__destroy(link[i]);
bpf_link__destroy(link[i]); bpf_object__close(obj);
if (!IS_ERR_OR_NULL(obj))
bpf_object__close(obj);
bpf_object__close(tgt_obj); bpf_object__close(tgt_obj);
free(link); free(link);
free(prog); free(prog);
...@@ -231,7 +229,7 @@ static int test_second_attach(struct bpf_object *obj) ...@@ -231,7 +229,7 @@ static int test_second_attach(struct bpf_object *obj)
return err; return err;
link = bpf_program__attach_freplace(prog, tgt_fd, tgt_name); link = bpf_program__attach_freplace(prog, tgt_fd, tgt_name);
if (CHECK(IS_ERR(link), "second_link", "failed to attach second link prog_fd %d tgt_fd %d\n", bpf_program__fd(prog), tgt_fd)) if (!ASSERT_OK_PTR(link, "second_link"))
goto out; goto out;
err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6), err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6),
...@@ -283,9 +281,7 @@ static void test_fmod_ret_freplace(void) ...@@ -283,9 +281,7 @@ static void test_fmod_ret_freplace(void)
opts.attach_prog_fd = pkt_fd; opts.attach_prog_fd = pkt_fd;
freplace_obj = bpf_object__open_file(freplace_name, &opts); freplace_obj = bpf_object__open_file(freplace_name, &opts);
if (CHECK(IS_ERR_OR_NULL(freplace_obj), "freplace_obj_open", if (!ASSERT_OK_PTR(freplace_obj, "freplace_obj_open"))
"failed to open %s: %ld\n", freplace_name,
PTR_ERR(freplace_obj)))
goto out; goto out;
err = bpf_object__load(freplace_obj); err = bpf_object__load(freplace_obj);
...@@ -294,14 +290,12 @@ static void test_fmod_ret_freplace(void) ...@@ -294,14 +290,12 @@ static void test_fmod_ret_freplace(void)
prog = bpf_program__next(NULL, freplace_obj); prog = bpf_program__next(NULL, freplace_obj);
freplace_link = bpf_program__attach_trace(prog); freplace_link = bpf_program__attach_trace(prog);
if (CHECK(IS_ERR(freplace_link), "freplace_attach_trace", "failed to link\n")) if (!ASSERT_OK_PTR(freplace_link, "freplace_attach_trace"))
goto out; goto out;
opts.attach_prog_fd = bpf_program__fd(prog); opts.attach_prog_fd = bpf_program__fd(prog);
fmod_obj = bpf_object__open_file(fmod_ret_name, &opts); fmod_obj = bpf_object__open_file(fmod_ret_name, &opts);
if (CHECK(IS_ERR_OR_NULL(fmod_obj), "fmod_obj_open", if (!ASSERT_OK_PTR(fmod_obj, "fmod_obj_open"))
"failed to open %s: %ld\n", fmod_ret_name,
PTR_ERR(fmod_obj)))
goto out; goto out;
err = bpf_object__load(fmod_obj); err = bpf_object__load(fmod_obj);
...@@ -350,9 +344,7 @@ static void test_obj_load_failure_common(const char *obj_file, ...@@ -350,9 +344,7 @@ static void test_obj_load_failure_common(const char *obj_file,
); );
obj = bpf_object__open_file(obj_file, &opts); obj = bpf_object__open_file(obj_file, &opts);
if (CHECK(IS_ERR_OR_NULL(obj), "obj_open", if (!ASSERT_OK_PTR(obj, "obj_open"))
"failed to open %s: %ld\n", obj_file,
PTR_ERR(obj)))
goto close_prog; goto close_prog;
/* It should fail to load the program */ /* It should fail to load the program */
...@@ -361,8 +353,7 @@ static void test_obj_load_failure_common(const char *obj_file, ...@@ -361,8 +353,7 @@ static void test_obj_load_failure_common(const char *obj_file,
goto close_prog; goto close_prog;
close_prog: close_prog:
if (!IS_ERR_OR_NULL(obj)) bpf_object__close(obj);
bpf_object__close(obj);
bpf_object__close(pkt_obj); bpf_object__close(pkt_obj);
} }
......
...@@ -541,7 +541,7 @@ static void test_skb_less_link_create(struct bpf_flow *skel, int tap_fd) ...@@ -541,7 +541,7 @@ static void test_skb_less_link_create(struct bpf_flow *skel, int tap_fd)
return; return;
link = bpf_program__attach_netns(skel->progs._dissect, net_fd); link = bpf_program__attach_netns(skel->progs._dissect, net_fd);
if (CHECK(IS_ERR(link), "attach_netns", "err %ld\n", PTR_ERR(link))) if (!ASSERT_OK_PTR(link, "attach_netns"))
goto out_close; goto out_close;
run_tests_skb_less(tap_fd, skel->maps.last_dissection); run_tests_skb_less(tap_fd, skel->maps.last_dissection);
......
...@@ -134,9 +134,9 @@ static void test_link_create_link_create(int netns, int prog1, int prog2) ...@@ -134,9 +134,9 @@ static void test_link_create_link_create(int netns, int prog1, int prog2)
/* Expect failure creating link when another link exists */ /* Expect failure creating link when another link exists */
errno = 0; errno = 0;
link2 = bpf_link_create(prog2, netns, BPF_FLOW_DISSECTOR, &opts); link2 = bpf_link_create(prog2, netns, BPF_FLOW_DISSECTOR, &opts);
if (CHECK_FAIL(link2 != -1 || errno != E2BIG)) if (CHECK_FAIL(link2 >= 0 || errno != E2BIG))
perror("bpf_prog_attach(prog2) expected E2BIG"); perror("bpf_prog_attach(prog2) expected E2BIG");
if (link2 != -1) if (link2 >= 0)
close(link2); close(link2);
CHECK_FAIL(query_attached_prog_id(netns) != query_prog_id(prog1)); CHECK_FAIL(query_attached_prog_id(netns) != query_prog_id(prog1));
...@@ -159,9 +159,9 @@ static void test_prog_attach_link_create(int netns, int prog1, int prog2) ...@@ -159,9 +159,9 @@ static void test_prog_attach_link_create(int netns, int prog1, int prog2)
/* Expect failure creating link when prog attached */ /* Expect failure creating link when prog attached */
errno = 0; errno = 0;
link = bpf_link_create(prog2, netns, BPF_FLOW_DISSECTOR, &opts); link = bpf_link_create(prog2, netns, BPF_FLOW_DISSECTOR, &opts);
if (CHECK_FAIL(link != -1 || errno != EEXIST)) if (CHECK_FAIL(link >= 0 || errno != EEXIST))
perror("bpf_link_create(prog2) expected EEXIST"); perror("bpf_link_create(prog2) expected EEXIST");
if (link != -1) if (link >= 0)
close(link); close(link);
CHECK_FAIL(query_attached_prog_id(netns) != query_prog_id(prog1)); CHECK_FAIL(query_attached_prog_id(netns) != query_prog_id(prog1));
...@@ -623,7 +623,7 @@ static void run_tests(int netns) ...@@ -623,7 +623,7 @@ static void run_tests(int netns)
} }
out_close: out_close:
for (i = 0; i < ARRAY_SIZE(progs); i++) { for (i = 0; i < ARRAY_SIZE(progs); i++) {
if (progs[i] != -1) if (progs[i] >= 0)
CHECK_FAIL(close(progs[i])); CHECK_FAIL(close(progs[i]));
} }
} }
......
...@@ -121,12 +121,12 @@ void test_get_stack_raw_tp(void) ...@@ -121,12 +121,12 @@ void test_get_stack_raw_tp(void)
goto close_prog; goto close_prog;
link = bpf_program__attach_raw_tracepoint(prog, "sys_enter"); link = bpf_program__attach_raw_tracepoint(prog, "sys_enter");
if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n", PTR_ERR(link))) if (!ASSERT_OK_PTR(link, "attach_raw_tp"))
goto close_prog; goto close_prog;
pb_opts.sample_cb = get_stack_print_output; pb_opts.sample_cb = get_stack_print_output;
pb = perf_buffer__new(bpf_map__fd(map), 8, &pb_opts); pb = perf_buffer__new(bpf_map__fd(map), 8, &pb_opts);
if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb))) if (!ASSERT_OK_PTR(pb, "perf_buf__new"))
goto close_prog; goto close_prog;
/* trigger some syscall action */ /* trigger some syscall action */
...@@ -141,9 +141,7 @@ void test_get_stack_raw_tp(void) ...@@ -141,9 +141,7 @@ void test_get_stack_raw_tp(void)
} }
close_prog: close_prog:
if (!IS_ERR_OR_NULL(link)) bpf_link__destroy(link);
bpf_link__destroy(link); perf_buffer__free(pb);
if (!IS_ERR_OR_NULL(pb))
perf_buffer__free(pb);
bpf_object__close(obj); bpf_object__close(obj);
} }
...@@ -48,8 +48,7 @@ void test_get_stackid_cannot_attach(void) ...@@ -48,8 +48,7 @@ void test_get_stackid_cannot_attach(void)
skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu, skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
pmu_fd); pmu_fd);
CHECK(!IS_ERR(skel->links.oncpu), "attach_perf_event_no_callchain", ASSERT_ERR_PTR(skel->links.oncpu, "attach_perf_event_no_callchain");
"should have failed\n");
close(pmu_fd); close(pmu_fd);
/* add PERF_SAMPLE_CALLCHAIN, attach should succeed */ /* add PERF_SAMPLE_CALLCHAIN, attach should succeed */
...@@ -65,8 +64,7 @@ void test_get_stackid_cannot_attach(void) ...@@ -65,8 +64,7 @@ void test_get_stackid_cannot_attach(void)
skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu, skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
pmu_fd); pmu_fd);
CHECK(IS_ERR(skel->links.oncpu), "attach_perf_event_callchain", ASSERT_OK_PTR(skel->links.oncpu, "attach_perf_event_callchain");
"err: %ld\n", PTR_ERR(skel->links.oncpu));
close(pmu_fd); close(pmu_fd);
/* add exclude_callchain_kernel, attach should fail */ /* add exclude_callchain_kernel, attach should fail */
...@@ -82,8 +80,7 @@ void test_get_stackid_cannot_attach(void) ...@@ -82,8 +80,7 @@ void test_get_stackid_cannot_attach(void)
skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu, skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
pmu_fd); pmu_fd);
CHECK(!IS_ERR(skel->links.oncpu), "attach_perf_event_exclude_callchain_kernel", ASSERT_ERR_PTR(skel->links.oncpu, "attach_perf_event_exclude_callchain_kernel");
"should have failed\n");
close(pmu_fd); close(pmu_fd);
cleanup: cleanup:
......
...@@ -48,8 +48,7 @@ static void test_hashmap_generic(void) ...@@ -48,8 +48,7 @@ static void test_hashmap_generic(void)
struct hashmap *map; struct hashmap *map;
map = hashmap__new(hash_fn, equal_fn, NULL); map = hashmap__new(hash_fn, equal_fn, NULL);
if (CHECK(IS_ERR(map), "hashmap__new", if (!ASSERT_OK_PTR(map, "hashmap__new"))
"failed to create map: %ld\n", PTR_ERR(map)))
return; return;
for (i = 0; i < ELEM_CNT; i++) { for (i = 0; i < ELEM_CNT; i++) {
...@@ -267,8 +266,7 @@ static void test_hashmap_multimap(void) ...@@ -267,8 +266,7 @@ static void test_hashmap_multimap(void)
/* force collisions */ /* force collisions */
map = hashmap__new(collision_hash_fn, equal_fn, NULL); map = hashmap__new(collision_hash_fn, equal_fn, NULL);
if (CHECK(IS_ERR(map), "hashmap__new", if (!ASSERT_OK_PTR(map, "hashmap__new"))
"failed to create map: %ld\n", PTR_ERR(map)))
return; return;
/* set up multimap: /* set up multimap:
...@@ -339,8 +337,7 @@ static void test_hashmap_empty() ...@@ -339,8 +337,7 @@ static void test_hashmap_empty()
/* force collisions */ /* force collisions */
map = hashmap__new(hash_fn, equal_fn, NULL); map = hashmap__new(hash_fn, equal_fn, NULL);
if (CHECK(IS_ERR(map), "hashmap__new", if (!ASSERT_OK_PTR(map, "hashmap__new"))
"failed to create map: %ld\n", PTR_ERR(map)))
goto cleanup; goto cleanup;
if (CHECK(hashmap__size(map) != 0, "hashmap__size", if (CHECK(hashmap__size(map) != 0, "hashmap__size",
......
...@@ -97,15 +97,13 @@ void test_kfree_skb(void) ...@@ -97,15 +97,13 @@ void test_kfree_skb(void)
goto close_prog; goto close_prog;
link = bpf_program__attach_raw_tracepoint(prog, NULL); link = bpf_program__attach_raw_tracepoint(prog, NULL);
if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n", PTR_ERR(link))) if (!ASSERT_OK_PTR(link, "attach_raw_tp"))
goto close_prog; goto close_prog;
link_fentry = bpf_program__attach_trace(fentry); link_fentry = bpf_program__attach_trace(fentry);
if (CHECK(IS_ERR(link_fentry), "attach fentry", "err %ld\n", if (!ASSERT_OK_PTR(link_fentry, "attach fentry"))
PTR_ERR(link_fentry)))
goto close_prog; goto close_prog;
link_fexit = bpf_program__attach_trace(fexit); link_fexit = bpf_program__attach_trace(fexit);
if (CHECK(IS_ERR(link_fexit), "attach fexit", "err %ld\n", if (!ASSERT_OK_PTR(link_fexit, "attach fexit"))
PTR_ERR(link_fexit)))
goto close_prog; goto close_prog;
perf_buf_map = bpf_object__find_map_by_name(obj2, "perf_buf_map"); perf_buf_map = bpf_object__find_map_by_name(obj2, "perf_buf_map");
...@@ -116,7 +114,7 @@ void test_kfree_skb(void) ...@@ -116,7 +114,7 @@ void test_kfree_skb(void)
pb_opts.sample_cb = on_sample; pb_opts.sample_cb = on_sample;
pb_opts.ctx = &passed; pb_opts.ctx = &passed;
pb = perf_buffer__new(bpf_map__fd(perf_buf_map), 1, &pb_opts); pb = perf_buffer__new(bpf_map__fd(perf_buf_map), 1, &pb_opts);
if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb))) if (!ASSERT_OK_PTR(pb, "perf_buf__new"))
goto close_prog; goto close_prog;
memcpy(skb.cb, &cb, sizeof(cb)); memcpy(skb.cb, &cb, sizeof(cb));
...@@ -144,12 +142,9 @@ void test_kfree_skb(void) ...@@ -144,12 +142,9 @@ void test_kfree_skb(void)
CHECK_FAIL(!test_ok[0] || !test_ok[1]); CHECK_FAIL(!test_ok[0] || !test_ok[1]);
close_prog: close_prog:
perf_buffer__free(pb); perf_buffer__free(pb);
if (!IS_ERR_OR_NULL(link)) bpf_link__destroy(link);
bpf_link__destroy(link); bpf_link__destroy(link_fentry);
if (!IS_ERR_OR_NULL(link_fentry)) bpf_link__destroy(link_fexit);
bpf_link__destroy(link_fentry);
if (!IS_ERR_OR_NULL(link_fexit))
bpf_link__destroy(link_fexit);
bpf_object__close(obj); bpf_object__close(obj);
bpf_object__close(obj2); bpf_object__close(obj2);
} }
...@@ -87,8 +87,7 @@ void test_ksyms_btf(void) ...@@ -87,8 +87,7 @@ void test_ksyms_btf(void)
struct btf *btf; struct btf *btf;
btf = libbpf_find_kernel_btf(); btf = libbpf_find_kernel_btf();
if (CHECK(IS_ERR(btf), "btf_exists", "failed to load kernel BTF: %ld\n", if (!ASSERT_OK_PTR(btf, "btf_exists"))
PTR_ERR(btf)))
return; return;
percpu_datasec = btf__find_by_name_kind(btf, ".data..percpu", percpu_datasec = btf__find_by_name_kind(btf, ".data..percpu",
......
...@@ -17,7 +17,7 @@ void test_link_pinning_subtest(struct bpf_program *prog, ...@@ -17,7 +17,7 @@ void test_link_pinning_subtest(struct bpf_program *prog,
int err, i; int err, i;
link = bpf_program__attach(prog); link = bpf_program__attach(prog);
if (CHECK(IS_ERR(link), "link_attach", "err: %ld\n", PTR_ERR(link))) if (!ASSERT_OK_PTR(link, "link_attach"))
goto cleanup; goto cleanup;
bss->in = 1; bss->in = 1;
...@@ -51,7 +51,7 @@ void test_link_pinning_subtest(struct bpf_program *prog, ...@@ -51,7 +51,7 @@ void test_link_pinning_subtest(struct bpf_program *prog,
/* re-open link from BPFFS */ /* re-open link from BPFFS */
link = bpf_link__open(link_pin_path); link = bpf_link__open(link_pin_path);
if (CHECK(IS_ERR(link), "link_open", "err: %ld\n", PTR_ERR(link))) if (!ASSERT_OK_PTR(link, "link_open"))
goto cleanup; goto cleanup;
CHECK(strcmp(link_pin_path, bpf_link__pin_path(link)), "pin_path2", CHECK(strcmp(link_pin_path, bpf_link__pin_path(link)), "pin_path2",
...@@ -84,8 +84,7 @@ void test_link_pinning_subtest(struct bpf_program *prog, ...@@ -84,8 +84,7 @@ void test_link_pinning_subtest(struct bpf_program *prog,
CHECK(i == 10000, "link_attached", "got to iteration #%d\n", i); CHECK(i == 10000, "link_attached", "got to iteration #%d\n", i);
cleanup: cleanup:
if (!IS_ERR(link)) bpf_link__destroy(link);
bpf_link__destroy(link);
} }
void test_link_pinning(void) void test_link_pinning(void)
......
...@@ -38,13 +38,13 @@ void test_obj_name(void) ...@@ -38,13 +38,13 @@ void test_obj_name(void)
fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
CHECK((tests[i].success && fd < 0) || CHECK((tests[i].success && fd < 0) ||
(!tests[i].success && fd != -1) || (!tests[i].success && fd >= 0) ||
(!tests[i].success && errno != tests[i].expected_errno), (!tests[i].success && errno != tests[i].expected_errno),
"check-bpf-prog-name", "check-bpf-prog-name",
"fd %d(%d) errno %d(%d)\n", "fd %d(%d) errno %d(%d)\n",
fd, tests[i].success, errno, tests[i].expected_errno); fd, tests[i].success, errno, tests[i].expected_errno);
if (fd != -1) if (fd >= 0)
close(fd); close(fd);
/* test different attr.map_name during BPF_MAP_CREATE */ /* test different attr.map_name during BPF_MAP_CREATE */
...@@ -59,13 +59,13 @@ void test_obj_name(void) ...@@ -59,13 +59,13 @@ void test_obj_name(void)
memcpy(attr.map_name, tests[i].name, ncopy); memcpy(attr.map_name, tests[i].name, ncopy);
fd = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); fd = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
CHECK((tests[i].success && fd < 0) || CHECK((tests[i].success && fd < 0) ||
(!tests[i].success && fd != -1) || (!tests[i].success && fd >= 0) ||
(!tests[i].success && errno != tests[i].expected_errno), (!tests[i].success && errno != tests[i].expected_errno),
"check-bpf-map-name", "check-bpf-map-name",
"fd %d(%d) errno %d(%d)\n", "fd %d(%d) errno %d(%d)\n",
fd, tests[i].success, errno, tests[i].expected_errno); fd, tests[i].success, errno, tests[i].expected_errno);
if (fd != -1) if (fd >= 0)
close(fd); close(fd);
} }
} }
...@@ -74,7 +74,7 @@ static void test_perf_branches_common(int perf_fd, ...@@ -74,7 +74,7 @@ static void test_perf_branches_common(int perf_fd,
/* attach perf_event */ /* attach perf_event */
link = bpf_program__attach_perf_event(skel->progs.perf_branches, perf_fd); link = bpf_program__attach_perf_event(skel->progs.perf_branches, perf_fd);
if (CHECK(IS_ERR(link), "attach_perf_event", "err %ld\n", PTR_ERR(link))) if (!ASSERT_OK_PTR(link, "attach_perf_event"))
goto out_destroy_skel; goto out_destroy_skel;
/* generate some branches on cpu 0 */ /* generate some branches on cpu 0 */
...@@ -119,7 +119,7 @@ static void test_perf_branches_hw(void) ...@@ -119,7 +119,7 @@ static void test_perf_branches_hw(void)
* Some setups don't support branch records (virtual machines, !x86), * Some setups don't support branch records (virtual machines, !x86),
* so skip test in this case. * so skip test in this case.
*/ */
if (pfd == -1) { if (pfd < 0) {
if (errno == ENOENT || errno == EOPNOTSUPP) { if (errno == ENOENT || errno == EOPNOTSUPP) {
printf("%s:SKIP:no PERF_SAMPLE_BRANCH_STACK\n", printf("%s:SKIP:no PERF_SAMPLE_BRANCH_STACK\n",
__func__); __func__);
......
...@@ -80,7 +80,7 @@ void test_perf_buffer(void) ...@@ -80,7 +80,7 @@ void test_perf_buffer(void)
pb_opts.sample_cb = on_sample; pb_opts.sample_cb = on_sample;
pb_opts.ctx = &cpu_seen; pb_opts.ctx = &cpu_seen;
pb = perf_buffer__new(bpf_map__fd(skel->maps.perf_buf_map), 1, &pb_opts); pb = perf_buffer__new(bpf_map__fd(skel->maps.perf_buf_map), 1, &pb_opts);
if (CHECK(IS_ERR(pb), "perf_buf__new", "err %ld\n", PTR_ERR(pb))) if (!ASSERT_OK_PTR(pb, "perf_buf__new"))
goto out_close; goto out_close;
CHECK(perf_buffer__epoll_fd(pb) < 0, "epoll_fd", CHECK(perf_buffer__epoll_fd(pb) < 0, "epoll_fd",
......
...@@ -97,8 +97,7 @@ void test_perf_event_stackmap(void) ...@@ -97,8 +97,7 @@ void test_perf_event_stackmap(void)
skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu, skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
pmu_fd); pmu_fd);
if (CHECK(IS_ERR(skel->links.oncpu), "attach_perf_event", if (!ASSERT_OK_PTR(skel->links.oncpu, "attach_perf_event")) {
"err %ld\n", PTR_ERR(skel->links.oncpu))) {
close(pmu_fd); close(pmu_fd);
goto cleanup; goto cleanup;
} }
......
...@@ -15,7 +15,7 @@ void test_reference_tracking(void) ...@@ -15,7 +15,7 @@ void test_reference_tracking(void)
int err = 0; int err = 0;
obj = bpf_object__open_file(file, &open_opts); obj = bpf_object__open_file(file, &open_opts);
if (CHECK_FAIL(IS_ERR(obj))) if (!ASSERT_OK_PTR(obj, "obj_open_file"))
return; return;
if (CHECK(strcmp(bpf_object__name(obj), obj_name), "obj_name", if (CHECK(strcmp(bpf_object__name(obj), obj_name), "obj_name",
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment