Commit 5577f24c authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'libbpf: add unified bpf_prog_load() low-level API'

Andrii Nakryiko says:

====================

This patch set adds unified OPTS-based low-level bpf_prog_load() API for
loading BPF programs directly into kernel without utilizing libbpf's
bpf_object abstractions. This OPTS-based interface allows for future
extensions without breaking backwards or forward API and ABI compatibility.
Similar approach will be used for other low-level APIs that require extensive
sets of parameters, like BPF_MAP_CREATE command.

First half of the patch set adds libbpf API, cleans up internal usage of
to-be-deprecated APIs, etc. Second half cleans up and converts selftests away
from using deprecated APIs. See individual patches for more details.

v1->v2:
  - dropped exposing sys_bpf() into public API (Alexei, Daniel);
  - also dropped bpftool/cgroup.c fix for unistd.h include because it's not
    necessary due to sys_bpf() staying as is.

Cc: Hengqi Chen <hengqi.chen@gmail.com>
====================
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents b8b5cb55 f19ddfe0
...@@ -467,7 +467,7 @@ static bool probe_bpf_syscall(const char *define_prefix) ...@@ -467,7 +467,7 @@ static bool probe_bpf_syscall(const char *define_prefix)
{ {
bool res; bool res;
bpf_load_program(BPF_PROG_TYPE_UNSPEC, NULL, 0, NULL, 0, NULL, 0); bpf_prog_load(BPF_PROG_TYPE_UNSPEC, NULL, NULL, NULL, 0, NULL);
res = (errno != ENOSYS); res = (errno != ENOSYS);
print_bool_feature("have_bpf_syscall", print_bool_feature("have_bpf_syscall",
......
This diff is collapsed.
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <stdint.h> #include <stdint.h>
#include "libbpf_common.h" #include "libbpf_common.h"
#include "libbpf_legacy.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
...@@ -71,6 +72,71 @@ LIBBPF_API int bpf_create_map_in_map(enum bpf_map_type map_type, ...@@ -71,6 +72,71 @@ LIBBPF_API int bpf_create_map_in_map(enum bpf_map_type map_type,
int inner_map_fd, int max_entries, int inner_map_fd, int max_entries,
__u32 map_flags); __u32 map_flags);
struct bpf_prog_load_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
/* libbpf can retry BPF_PROG_LOAD command if bpf() syscall returns
* -EAGAIN. This field determines how many attempts libbpf has to
* make. If not specified, libbpf will use default value of 5.
*/
int attempts;
enum bpf_attach_type expected_attach_type;
__u32 prog_btf_fd;
__u32 prog_flags;
__u32 prog_ifindex;
__u32 kern_version;
__u32 attach_btf_id;
__u32 attach_prog_fd;
__u32 attach_btf_obj_fd;
const int *fd_array;
/* .BTF.ext func info data */
const void *func_info;
__u32 func_info_cnt;
__u32 func_info_rec_size;
/* .BTF.ext line info data */
const void *line_info;
__u32 line_info_cnt;
__u32 line_info_rec_size;
/* verifier log options */
__u32 log_level;
__u32 log_size;
char *log_buf;
};
#define bpf_prog_load_opts__last_field log_buf
LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
const char *prog_name, const char *license,
const struct bpf_insn *insns, size_t insn_cnt,
const struct bpf_prog_load_opts *opts);
/* this "specialization" should go away in libbpf 1.0 */
LIBBPF_API int bpf_prog_load_v0_6_0(enum bpf_prog_type prog_type,
const char *prog_name, const char *license,
const struct bpf_insn *insns, size_t insn_cnt,
const struct bpf_prog_load_opts *opts);
/* This is an elaborate way to not conflict with deprecated bpf_prog_load()
* API, defined in libbpf.h. Once we hit libbpf 1.0, all this will be gone.
* With this approach, if someone is calling bpf_prog_load() with
* 4 arguments, they will use the deprecated API, which keeps backwards
* compatibility (both source code and binary). If bpf_prog_load() is called
* with 6 arguments, though, it gets redirected to __bpf_prog_load.
* So looking forward to libbpf 1.0 when this hack will be gone and
* __bpf_prog_load() will be called just bpf_prog_load().
*/
#ifndef bpf_prog_load
#define bpf_prog_load(...) ___libbpf_overload(___bpf_prog_load, __VA_ARGS__)
#define ___bpf_prog_load4(file, type, pobj, prog_fd) \
bpf_prog_load_deprecated(file, type, pobj, prog_fd)
#define ___bpf_prog_load6(prog_type, prog_name, license, insns, insn_cnt, opts) \
bpf_prog_load(prog_type, prog_name, license, insns, insn_cnt, opts)
#endif /* bpf_prog_load */
struct bpf_load_program_attr { struct bpf_load_program_attr {
enum bpf_prog_type prog_type; enum bpf_prog_type prog_type;
enum bpf_attach_type expected_attach_type; enum bpf_attach_type expected_attach_type;
...@@ -102,13 +168,15 @@ struct bpf_load_program_attr { ...@@ -102,13 +168,15 @@ struct bpf_load_program_attr {
/* Recommend log buffer size */ /* Recommend log buffer size */
#define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */ #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
LIBBPF_API int LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, LIBBPF_API int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
char *log_buf, size_t log_buf_sz); char *log_buf, size_t log_buf_sz);
LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
LIBBPF_API int bpf_load_program(enum bpf_prog_type type, LIBBPF_API int bpf_load_program(enum bpf_prog_type type,
const struct bpf_insn *insns, size_t insns_cnt, const struct bpf_insn *insns, size_t insns_cnt,
const char *license, __u32 kern_version, const char *license, __u32 kern_version,
char *log_buf, size_t log_buf_sz); char *log_buf, size_t log_buf_sz);
LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
LIBBPF_API int bpf_verify_program(enum bpf_prog_type type, LIBBPF_API int bpf_verify_program(enum bpf_prog_type type,
const struct bpf_insn *insns, const struct bpf_insn *insns,
size_t insns_cnt, __u32 prog_flags, size_t insns_cnt, __u32 prog_flags,
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#ifndef __BPF_GEN_INTERNAL_H #ifndef __BPF_GEN_INTERNAL_H
#define __BPF_GEN_INTERNAL_H #define __BPF_GEN_INTERNAL_H
#include "bpf.h"
struct ksym_relo_desc { struct ksym_relo_desc {
const char *name; const char *name;
int kind; int kind;
...@@ -50,8 +52,10 @@ int bpf_gen__finish(struct bpf_gen *gen); ...@@ -50,8 +52,10 @@ int bpf_gen__finish(struct bpf_gen *gen);
void bpf_gen__free(struct bpf_gen *gen); void bpf_gen__free(struct bpf_gen *gen);
void bpf_gen__load_btf(struct bpf_gen *gen, const void *raw_data, __u32 raw_size); void bpf_gen__load_btf(struct bpf_gen *gen, const void *raw_data, __u32 raw_size);
void bpf_gen__map_create(struct bpf_gen *gen, struct bpf_create_map_params *map_attr, int map_idx); void bpf_gen__map_create(struct bpf_gen *gen, struct bpf_create_map_params *map_attr, int map_idx);
struct bpf_prog_load_params; void bpf_gen__prog_load(struct bpf_gen *gen,
void bpf_gen__prog_load(struct bpf_gen *gen, struct bpf_prog_load_params *load_attr, int prog_idx); enum bpf_prog_type prog_type, const char *prog_name,
const char *license, struct bpf_insn *insns, size_t insn_cnt,
struct bpf_prog_load_opts *load_attr, int prog_idx);
void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *value, __u32 value_size); void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *value, __u32 value_size);
void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx); void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx);
void bpf_gen__record_attach_target(struct bpf_gen *gen, const char *name, enum bpf_attach_type type); void bpf_gen__record_attach_target(struct bpf_gen *gen, const char *name, enum bpf_attach_type type);
......
...@@ -901,27 +901,27 @@ static void cleanup_relos(struct bpf_gen *gen, int insns) ...@@ -901,27 +901,27 @@ static void cleanup_relos(struct bpf_gen *gen, int insns)
} }
void bpf_gen__prog_load(struct bpf_gen *gen, void bpf_gen__prog_load(struct bpf_gen *gen,
struct bpf_prog_load_params *load_attr, int prog_idx) enum bpf_prog_type prog_type, const char *prog_name,
const char *license, struct bpf_insn *insns, size_t insn_cnt,
struct bpf_prog_load_opts *load_attr, int prog_idx)
{ {
int attr_size = offsetofend(union bpf_attr, fd_array); int attr_size = offsetofend(union bpf_attr, fd_array);
int prog_load_attr, license, insns, func_info, line_info; int prog_load_attr, license_off, insns_off, func_info, line_info;
union bpf_attr attr; union bpf_attr attr;
memset(&attr, 0, attr_size); memset(&attr, 0, attr_size);
pr_debug("gen: prog_load: type %d insns_cnt %zd\n", pr_debug("gen: prog_load: type %d insns_cnt %zd\n", prog_type, insn_cnt);
load_attr->prog_type, load_attr->insn_cnt);
/* add license string to blob of bytes */ /* add license string to blob of bytes */
license = add_data(gen, load_attr->license, strlen(load_attr->license) + 1); license_off = add_data(gen, license, strlen(license) + 1);
/* add insns to blob of bytes */ /* add insns to blob of bytes */
insns = add_data(gen, load_attr->insns, insns_off = add_data(gen, insns, insn_cnt * sizeof(struct bpf_insn));
load_attr->insn_cnt * sizeof(struct bpf_insn));
attr.prog_type = load_attr->prog_type; attr.prog_type = prog_type;
attr.expected_attach_type = load_attr->expected_attach_type; attr.expected_attach_type = load_attr->expected_attach_type;
attr.attach_btf_id = load_attr->attach_btf_id; attr.attach_btf_id = load_attr->attach_btf_id;
attr.prog_ifindex = load_attr->prog_ifindex; attr.prog_ifindex = load_attr->prog_ifindex;
attr.kern_version = 0; attr.kern_version = 0;
attr.insn_cnt = (__u32)load_attr->insn_cnt; attr.insn_cnt = (__u32)insn_cnt;
attr.prog_flags = load_attr->prog_flags; attr.prog_flags = load_attr->prog_flags;
attr.func_info_rec_size = load_attr->func_info_rec_size; attr.func_info_rec_size = load_attr->func_info_rec_size;
...@@ -934,15 +934,15 @@ void bpf_gen__prog_load(struct bpf_gen *gen, ...@@ -934,15 +934,15 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
line_info = add_data(gen, load_attr->line_info, line_info = add_data(gen, load_attr->line_info,
attr.line_info_cnt * attr.line_info_rec_size); attr.line_info_cnt * attr.line_info_rec_size);
memcpy(attr.prog_name, load_attr->name, memcpy(attr.prog_name, prog_name,
min((unsigned)strlen(load_attr->name), BPF_OBJ_NAME_LEN - 1)); min((unsigned)strlen(prog_name), BPF_OBJ_NAME_LEN - 1));
prog_load_attr = add_data(gen, &attr, attr_size); prog_load_attr = add_data(gen, &attr, attr_size);
/* populate union bpf_attr with a pointer to license */ /* populate union bpf_attr with a pointer to license */
emit_rel_store(gen, attr_field(prog_load_attr, license), license); emit_rel_store(gen, attr_field(prog_load_attr, license), license_off);
/* populate union bpf_attr with a pointer to instructions */ /* populate union bpf_attr with a pointer to instructions */
emit_rel_store(gen, attr_field(prog_load_attr, insns), insns); emit_rel_store(gen, attr_field(prog_load_attr, insns), insns_off);
/* populate union bpf_attr with a pointer to func_info */ /* populate union bpf_attr with a pointer to func_info */
emit_rel_store(gen, attr_field(prog_load_attr, func_info), func_info); emit_rel_store(gen, attr_field(prog_load_attr, func_info), func_info);
...@@ -974,12 +974,12 @@ void bpf_gen__prog_load(struct bpf_gen *gen, ...@@ -974,12 +974,12 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_7, emit(gen, BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_7,
offsetof(union bpf_attr, attach_btf_obj_fd))); offsetof(union bpf_attr, attach_btf_obj_fd)));
} }
emit_relos(gen, insns); emit_relos(gen, insns_off);
/* emit PROG_LOAD command */ /* emit PROG_LOAD command */
emit_sys_bpf(gen, BPF_PROG_LOAD, prog_load_attr, attr_size); emit_sys_bpf(gen, BPF_PROG_LOAD, prog_load_attr, attr_size);
debug_ret(gen, "prog_load %s insn_cnt %d", attr.prog_name, attr.insn_cnt); debug_ret(gen, "prog_load %s insn_cnt %d", attr.prog_name, attr.insn_cnt);
/* successful or not, close btf module FDs used in extern ksyms and attach_btf_obj_fd */ /* successful or not, close btf module FDs used in extern ksyms and attach_btf_obj_fd */
cleanup_relos(gen, insns); cleanup_relos(gen, insns_off);
if (gen->attach_kind) if (gen->attach_kind)
emit_sys_close_blob(gen, emit_sys_close_blob(gen,
attr_field(prog_load_attr, attach_btf_obj_fd)); attr_field(prog_load_attr, attach_btf_obj_fd));
......
This diff is collapsed.
...@@ -431,7 +431,6 @@ bpf_program__attach_iter(const struct bpf_program *prog, ...@@ -431,7 +431,6 @@ bpf_program__attach_iter(const struct bpf_program *prog,
* one instance. In this case bpf_program__fd(prog) is equal to * one instance. In this case bpf_program__fd(prog) is equal to
* bpf_program__nth_fd(prog, 0). * bpf_program__nth_fd(prog, 0).
*/ */
LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__insns() for getting bpf_program instructions")
struct bpf_prog_prep_result { struct bpf_prog_prep_result {
/* /*
* If not NULL, load new instruction array. * If not NULL, load new instruction array.
...@@ -676,8 +675,9 @@ struct bpf_prog_load_attr { ...@@ -676,8 +675,9 @@ struct bpf_prog_load_attr {
LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
struct bpf_object **pobj, int *prog_fd); struct bpf_object **pobj, int *prog_fd);
LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type, LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__open() and bpf_object__load() instead")
struct bpf_object **pobj, int *prog_fd); LIBBPF_API int bpf_prog_load_deprecated(const char *file, enum bpf_prog_type type,
struct bpf_object **pobj, int *prog_fd);
/* XDP related API */ /* XDP related API */
struct xdp_link_info { struct xdp_link_info {
......
...@@ -395,6 +395,8 @@ LIBBPF_0.6.0 { ...@@ -395,6 +395,8 @@ LIBBPF_0.6.0 {
bpf_object__next_program; bpf_object__next_program;
bpf_object__prev_map; bpf_object__prev_map;
bpf_object__prev_program; bpf_object__prev_program;
bpf_prog_load_deprecated;
bpf_prog_load;
bpf_program__insn_cnt; bpf_program__insn_cnt;
bpf_program__insns; bpf_program__insns;
btf__add_btf; btf__add_btf;
......
...@@ -41,6 +41,18 @@ ...@@ -41,6 +41,18 @@
#define __LIBBPF_MARK_DEPRECATED_0_7(X) #define __LIBBPF_MARK_DEPRECATED_0_7(X)
#endif #endif
/* This set of internal macros allows to do "function overloading" based on
* number of arguments provided by used in backwards-compatible way during the
* transition to libbpf 1.0
* It's ugly but necessary evil that will be cleaned up when we get to 1.0.
* See bpf_prog_load() overload for example.
*/
#define ___libbpf_cat(A, B) A ## B
#define ___libbpf_select(NAME, NUM) ___libbpf_cat(NAME, NUM)
#define ___libbpf_nth(_1, _2, _3, _4, _5, _6, N, ...) N
#define ___libbpf_cnt(...) ___libbpf_nth(__VA_ARGS__, 6, 5, 4, 3, 2, 1)
#define ___libbpf_overload(NAME, ...) ___libbpf_select(NAME, ___libbpf_cnt(__VA_ARGS__))(__VA_ARGS__)
/* Helper macro to declare and initialize libbpf options struct /* Helper macro to declare and initialize libbpf options struct
* *
* This dance with uninitialized declaration, followed by memset to zero, * This dance with uninitialized declaration, followed by memset to zero,
...@@ -54,7 +66,7 @@ ...@@ -54,7 +66,7 @@
* including any extra padding, it with memset() and then assigns initial * including any extra padding, it with memset() and then assigns initial
* values provided by users in struct initializer-syntax as varargs. * values provided by users in struct initializer-syntax as varargs.
*/ */
#define DECLARE_LIBBPF_OPTS(TYPE, NAME, ...) \ #define LIBBPF_OPTS(TYPE, NAME, ...) \
struct TYPE NAME = ({ \ struct TYPE NAME = ({ \
memset(&NAME, 0, sizeof(struct TYPE)); \ memset(&NAME, 0, sizeof(struct TYPE)); \
(struct TYPE) { \ (struct TYPE) { \
......
...@@ -276,37 +276,6 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz); ...@@ -276,37 +276,6 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);
int libbpf__load_raw_btf(const char *raw_types, size_t types_len, int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
const char *str_sec, size_t str_len); const char *str_sec, size_t str_len);
struct bpf_prog_load_params {
enum bpf_prog_type prog_type;
enum bpf_attach_type expected_attach_type;
const char *name;
const struct bpf_insn *insns;
size_t insn_cnt;
const char *license;
__u32 kern_version;
__u32 attach_prog_fd;
__u32 attach_btf_obj_fd;
__u32 attach_btf_id;
__u32 prog_ifindex;
__u32 prog_btf_fd;
__u32 prog_flags;
__u32 func_info_rec_size;
const void *func_info;
__u32 func_info_cnt;
__u32 line_info_rec_size;
const void *line_info;
__u32 line_info_cnt;
__u32 log_level;
char *log_buf;
size_t log_buf_sz;
int *fd_array;
};
int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr);
struct bpf_create_map_params { struct bpf_create_map_params {
const char *name; const char *name;
enum bpf_map_type map_type; enum bpf_map_type map_type;
......
...@@ -69,6 +69,7 @@ enum libbpf_strict_mode { ...@@ -69,6 +69,7 @@ enum libbpf_strict_mode {
LIBBPF_API int libbpf_set_strict_mode(enum libbpf_strict_mode mode); LIBBPF_API int libbpf_set_strict_mode(enum libbpf_strict_mode mode);
#define DECLARE_LIBBPF_OPTS LIBBPF_OPTS
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
......
...@@ -68,21 +68,21 @@ static void ...@@ -68,21 +68,21 @@ static void
probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,
size_t insns_cnt, char *buf, size_t buf_len, __u32 ifindex) size_t insns_cnt, char *buf, size_t buf_len, __u32 ifindex)
{ {
struct bpf_load_program_attr xattr = {}; LIBBPF_OPTS(bpf_prog_load_opts, opts);
int fd; int fd;
switch (prog_type) { switch (prog_type) {
case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
xattr.expected_attach_type = BPF_CGROUP_INET4_CONNECT; opts.expected_attach_type = BPF_CGROUP_INET4_CONNECT;
break; break;
case BPF_PROG_TYPE_CGROUP_SOCKOPT: case BPF_PROG_TYPE_CGROUP_SOCKOPT:
xattr.expected_attach_type = BPF_CGROUP_GETSOCKOPT; opts.expected_attach_type = BPF_CGROUP_GETSOCKOPT;
break; break;
case BPF_PROG_TYPE_SK_LOOKUP: case BPF_PROG_TYPE_SK_LOOKUP:
xattr.expected_attach_type = BPF_SK_LOOKUP; opts.expected_attach_type = BPF_SK_LOOKUP;
break; break;
case BPF_PROG_TYPE_KPROBE: case BPF_PROG_TYPE_KPROBE:
xattr.kern_version = get_kernel_version(); opts.kern_version = get_kernel_version();
break; break;
case BPF_PROG_TYPE_UNSPEC: case BPF_PROG_TYPE_UNSPEC:
case BPF_PROG_TYPE_SOCKET_FILTER: case BPF_PROG_TYPE_SOCKET_FILTER:
...@@ -115,13 +115,11 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, ...@@ -115,13 +115,11 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,
break; break;
} }
xattr.prog_type = prog_type; opts.prog_ifindex = ifindex;
xattr.insns = insns; opts.log_buf = buf;
xattr.insns_cnt = insns_cnt; opts.log_size = buf_len;
xattr.license = "GPL";
xattr.prog_ifindex = ifindex;
fd = bpf_load_program_xattr(&xattr, buf, buf_len); fd = bpf_prog_load(prog_type, NULL, "GPL", insns, insns_cnt, NULL);
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);
} }
......
...@@ -364,7 +364,6 @@ int xsk_umem__create_v0_0_2(struct xsk_umem **umem_ptr, void *umem_area, ...@@ -364,7 +364,6 @@ int xsk_umem__create_v0_0_2(struct xsk_umem **umem_ptr, void *umem_area,
static enum xsk_prog get_xsk_prog(void) static enum xsk_prog get_xsk_prog(void)
{ {
enum xsk_prog detected = XSK_PROG_FALLBACK; enum xsk_prog detected = XSK_PROG_FALLBACK;
struct bpf_load_program_attr prog_attr;
struct bpf_create_map_attr map_attr; struct bpf_create_map_attr map_attr;
__u32 size_out, retval, duration; __u32 size_out, retval, duration;
char data_in = 0, data_out; char data_in = 0, data_out;
...@@ -375,7 +374,7 @@ static enum xsk_prog get_xsk_prog(void) ...@@ -375,7 +374,7 @@ static enum xsk_prog get_xsk_prog(void)
BPF_EMIT_CALL(BPF_FUNC_redirect_map), BPF_EMIT_CALL(BPF_FUNC_redirect_map),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
int prog_fd, map_fd, ret; int prog_fd, map_fd, ret, insn_cnt = ARRAY_SIZE(insns);
memset(&map_attr, 0, sizeof(map_attr)); memset(&map_attr, 0, sizeof(map_attr));
map_attr.map_type = BPF_MAP_TYPE_XSKMAP; map_attr.map_type = BPF_MAP_TYPE_XSKMAP;
...@@ -389,13 +388,7 @@ static enum xsk_prog get_xsk_prog(void) ...@@ -389,13 +388,7 @@ static enum xsk_prog get_xsk_prog(void)
insns[0].imm = map_fd; insns[0].imm = map_fd;
memset(&prog_attr, 0, sizeof(prog_attr)); prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "GPL", insns, insn_cnt, NULL);
prog_attr.prog_type = BPF_PROG_TYPE_XDP;
prog_attr.insns = insns;
prog_attr.insns_cnt = ARRAY_SIZE(insns);
prog_attr.license = "GPL";
prog_fd = bpf_load_program_xattr(&prog_attr, NULL, 0);
if (prog_fd < 0) { if (prog_fd < 0) {
close(map_fd); close(map_fd);
return detected; return detected;
...@@ -495,10 +488,13 @@ static int xsk_load_xdp_prog(struct xsk_socket *xsk) ...@@ -495,10 +488,13 @@ static int xsk_load_xdp_prog(struct xsk_socket *xsk)
}; };
struct bpf_insn *progs[] = {prog, prog_redirect_flags}; struct bpf_insn *progs[] = {prog, prog_redirect_flags};
enum xsk_prog option = get_xsk_prog(); enum xsk_prog option = get_xsk_prog();
LIBBPF_OPTS(bpf_prog_load_opts, opts,
.log_buf = log_buf,
.log_size = log_buf_size,
);
prog_fd = bpf_load_program(BPF_PROG_TYPE_XDP, progs[option], insns_cnt[option], prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "LGPL-2.1 or BSD-2-Clause",
"LGPL-2.1 or BSD-2-Clause", 0, log_buf, progs[option], insns_cnt[option], &opts);
log_buf_size);
if (prog_fd < 0) { if (prog_fd < 0) {
pr_warn("BPF log buffer:\n%s", log_buf); pr_warn("BPF log buffer:\n%s", log_buf);
return prog_fd; return prog_fd;
...@@ -725,14 +721,12 @@ static int xsk_link_lookup(int ifindex, __u32 *prog_id, int *link_fd) ...@@ -725,14 +721,12 @@ static int xsk_link_lookup(int ifindex, __u32 *prog_id, int *link_fd)
static bool xsk_probe_bpf_link(void) static bool xsk_probe_bpf_link(void)
{ {
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts, LIBBPF_OPTS(bpf_link_create_opts, opts, .flags = XDP_FLAGS_SKB_MODE);
.flags = XDP_FLAGS_SKB_MODE);
struct bpf_load_program_attr prog_attr;
struct bpf_insn insns[2] = { struct bpf_insn insns[2] = {
BPF_MOV64_IMM(BPF_REG_0, XDP_PASS), BPF_MOV64_IMM(BPF_REG_0, XDP_PASS),
BPF_EXIT_INSN() BPF_EXIT_INSN()
}; };
int prog_fd, link_fd = -1; int prog_fd, link_fd = -1, insn_cnt = ARRAY_SIZE(insns);
int ifindex_lo = 1; int ifindex_lo = 1;
bool ret = false; bool ret = false;
int err; int err;
...@@ -744,13 +738,7 @@ static bool xsk_probe_bpf_link(void) ...@@ -744,13 +738,7 @@ static bool xsk_probe_bpf_link(void)
if (link_fd >= 0) if (link_fd >= 0)
return true; return true;
memset(&prog_attr, 0, sizeof(prog_attr)); prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "GPL", insns, insn_cnt, NULL);
prog_attr.prog_type = BPF_PROG_TYPE_XDP;
prog_attr.insns = insns;
prog_attr.insns_cnt = ARRAY_SIZE(insns);
prog_attr.license = "GPL";
prog_fd = bpf_load_program_xattr(&prog_attr, NULL, 0);
if (prog_fd < 0) if (prog_fd < 0)
return ret; return ret;
......
...@@ -23,9 +23,7 @@ BPF_GCC ?= $(shell command -v bpf-gcc;) ...@@ -23,9 +23,7 @@ BPF_GCC ?= $(shell command -v bpf-gcc;)
SAN_CFLAGS ?= SAN_CFLAGS ?=
CFLAGS += -g -O0 -rdynamic -Wall $(GENFLAGS) $(SAN_CFLAGS) \ CFLAGS += -g -O0 -rdynamic -Wall $(GENFLAGS) $(SAN_CFLAGS) \
-I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \ -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \
-I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT) \ -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)
-Dbpf_prog_load=bpf_prog_test_load \
-Dbpf_load_program=bpf_test_load_program
LDLIBS += -lcap -lelf -lz -lrt -lpthread LDLIBS += -lcap -lelf -lz -lrt -lpthread
# Silence some warnings when compiled with clang # Silence some warnings when compiled with clang
...@@ -178,10 +176,6 @@ $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes ...@@ -178,10 +176,6 @@ $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes
$(Q)$(MAKE) $(submake_extras) -C bpf_testmod $(Q)$(MAKE) $(submake_extras) -C bpf_testmod
$(Q)cp bpf_testmod/bpf_testmod.ko $@ $(Q)cp bpf_testmod/bpf_testmod.ko $@
$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
$(call msg,CC,,$@)
$(Q)$(CC) -c $(CFLAGS) -o $@ $<
DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT) $(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
...@@ -194,18 +188,24 @@ $(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT) ...@@ -194,18 +188,24 @@ $(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
TEST_GEN_PROGS_EXTENDED += $(DEFAULT_BPFTOOL) TEST_GEN_PROGS_EXTENDED += $(DEFAULT_BPFTOOL)
$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ) $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ)
$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c $(OUTPUT)/test_dev_cgroup: cgroup_helpers.c testing_helpers.o
$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c $(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c testing_helpers.o
$(OUTPUT)/test_sock: cgroup_helpers.c $(OUTPUT)/test_sock: cgroup_helpers.c testing_helpers.o
$(OUTPUT)/test_sock_addr: cgroup_helpers.c $(OUTPUT)/test_sock_addr: cgroup_helpers.c testing_helpers.o
$(OUTPUT)/test_sockmap: cgroup_helpers.c $(OUTPUT)/test_sockmap: cgroup_helpers.c testing_helpers.o
$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c $(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c testing_helpers.o
$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c $(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c testing_helpers.o
$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c $(OUTPUT)/test_cgroup_storage: cgroup_helpers.c testing_helpers.o
$(OUTPUT)/test_sock_fields: cgroup_helpers.c $(OUTPUT)/test_sock_fields: cgroup_helpers.c testing_helpers.o
$(OUTPUT)/test_sysctl: cgroup_helpers.c $(OUTPUT)/test_sysctl: cgroup_helpers.c testing_helpers.o
$(OUTPUT)/test_tag: testing_helpers.o
$(OUTPUT)/test_lirc_mode2_user: testing_helpers.o
$(OUTPUT)/xdping: testing_helpers.o
$(OUTPUT)/flow_dissector_load: testing_helpers.o
$(OUTPUT)/test_maps: testing_helpers.o
$(OUTPUT)/test_verifier: testing_helpers.o
BPFTOOL ?= $(DEFAULT_BPFTOOL) BPFTOOL ?= $(DEFAULT_BPFTOOL)
$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \ $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include "testing_helpers.h"
static inline int bpf_flow_load(struct bpf_object **obj, static inline int bpf_flow_load(struct bpf_object **obj,
const char *path, const char *path,
...@@ -18,7 +19,7 @@ static inline int bpf_flow_load(struct bpf_object **obj, ...@@ -18,7 +19,7 @@ static inline int bpf_flow_load(struct bpf_object **obj,
int prog_array_fd; int prog_array_fd;
int ret, fd, i; int ret, fd, i;
ret = bpf_prog_load(path, BPF_PROG_TYPE_FLOW_DISSECTOR, obj, ret = bpf_prog_test_load(path, BPF_PROG_TYPE_FLOW_DISSECTOR, obj,
prog_fd); prog_fd);
if (ret) if (ret)
return ret; return ret;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include "cgroup_helpers.h" #include "cgroup_helpers.h"
#include "testing_helpers.h"
#include "bpf_rlimit.h" #include "bpf_rlimit.h"
#define CHECK(condition, tag, format...) ({ \ #define CHECK(condition, tag, format...) ({ \
...@@ -66,8 +67,8 @@ int main(int argc, char **argv) ...@@ -66,8 +67,8 @@ int main(int argc, char **argv)
if (CHECK(cgroup_fd < 0, "cgroup_setup_and_join", "err %d errno %d\n", cgroup_fd, errno)) if (CHECK(cgroup_fd < 0, "cgroup_setup_and_join", "err %d errno %d\n", cgroup_fd, errno))
return 1; return 1;
err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "bpf_prog_load", "err %d errno %d\n", err, errno)) if (CHECK(err, "bpf_prog_test_load", "err %d errno %d\n", err, errno))
goto cleanup_cgroup_env; goto cleanup_cgroup_env;
cgidmap_fd = bpf_find_map(__func__, obj, "cg_ids"); cgidmap_fd = bpf_find_map(__func__, obj, "cg_ids");
......
...@@ -594,6 +594,12 @@ static int do_test_single(struct bpf_align_test *test) ...@@ -594,6 +594,12 @@ static int do_test_single(struct bpf_align_test *test)
struct bpf_insn *prog = test->insns; struct bpf_insn *prog = test->insns;
int prog_type = test->prog_type; int prog_type = test->prog_type;
char bpf_vlog_copy[32768]; char bpf_vlog_copy[32768];
LIBBPF_OPTS(bpf_prog_load_opts, opts,
.prog_flags = BPF_F_STRICT_ALIGNMENT,
.log_buf = bpf_vlog,
.log_size = sizeof(bpf_vlog),
.log_level = 2,
);
const char *line_ptr; const char *line_ptr;
int cur_line = -1; int cur_line = -1;
int prog_len, i; int prog_len, i;
...@@ -601,9 +607,8 @@ static int do_test_single(struct bpf_align_test *test) ...@@ -601,9 +607,8 @@ static int do_test_single(struct bpf_align_test *test)
int ret; int ret;
prog_len = probe_filter_length(prog); prog_len = probe_filter_length(prog);
fd_prog = bpf_verify_program(prog_type ? : BPF_PROG_TYPE_SOCKET_FILTER, fd_prog = bpf_prog_load(prog_type ? : BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
prog, prog_len, BPF_F_STRICT_ALIGNMENT, prog, prog_len, &opts);
"GPL", 0, bpf_vlog, sizeof(bpf_vlog), 2);
if (fd_prog < 0 && test->result != REJECT) { if (fd_prog < 0 && test->result != REJECT) {
printf("Failed to load program.\n"); printf("Failed to load program.\n");
printf("%s", bpf_vlog); printf("%s", bpf_vlog);
......
...@@ -48,7 +48,7 @@ void serial_test_bpf_obj_id(void) ...@@ -48,7 +48,7 @@ void serial_test_bpf_obj_id(void)
bzero(zeros, sizeof(zeros)); bzero(zeros, sizeof(zeros));
for (i = 0; i < nr_iters; i++) { for (i = 0; i < nr_iters; i++) {
now = time(NULL); now = time(NULL);
err = bpf_prog_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, err = bpf_prog_test_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT,
&objs[i], &prog_fds[i]); &objs[i], &prog_fds[i]);
/* test_obj_id.o is a dumb prog. It should never fail /* test_obj_id.o is a dumb prog. It should never fail
* to load. * to load.
......
...@@ -16,7 +16,7 @@ static int prog_load(void) ...@@ -16,7 +16,7 @@ static int prog_load(void)
}; };
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
return bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB, return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
prog, insns_cnt, "GPL", 0, prog, insns_cnt, "GPL", 0,
bpf_log_buf, BPF_LOG_BUF_SIZE); bpf_log_buf, BPF_LOG_BUF_SIZE);
} }
......
...@@ -66,7 +66,7 @@ static int prog_load_cnt(int verdict, int val) ...@@ -66,7 +66,7 @@ static int prog_load_cnt(int verdict, int val)
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
int ret; int ret;
ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB, ret = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
prog, insns_cnt, "GPL", 0, prog, insns_cnt, "GPL", 0,
bpf_log_buf, BPF_LOG_BUF_SIZE); bpf_log_buf, BPF_LOG_BUF_SIZE);
......
...@@ -18,7 +18,7 @@ static int prog_load(int verdict) ...@@ -18,7 +18,7 @@ static int prog_load(int verdict)
}; };
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
return bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB, return bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
prog, insns_cnt, "GPL", 0, prog, insns_cnt, "GPL", 0,
bpf_log_buf, BPF_LOG_BUF_SIZE); bpf_log_buf, BPF_LOG_BUF_SIZE);
} }
......
...@@ -65,7 +65,7 @@ static void test_fexit_bpf2bpf_common(const char *obj_file, ...@@ -65,7 +65,7 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,
int err, tgt_fd, i; int err, tgt_fd, i;
struct btf *btf; struct btf *btf;
err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC, err = bpf_prog_test_load(target_obj_file, BPF_PROG_TYPE_UNSPEC,
&tgt_obj, &tgt_fd); &tgt_obj, &tgt_fd);
if (!ASSERT_OK(err, "tgt_prog_load")) if (!ASSERT_OK(err, "tgt_prog_load"))
return; return;
...@@ -224,7 +224,7 @@ static int test_second_attach(struct bpf_object *obj) ...@@ -224,7 +224,7 @@ static int test_second_attach(struct bpf_object *obj)
if (CHECK(!prog, "find_prog", "prog %s not found\n", prog_name)) if (CHECK(!prog, "find_prog", "prog %s not found\n", prog_name))
return -ENOENT; return -ENOENT;
err = bpf_prog_load(tgt_obj_file, BPF_PROG_TYPE_UNSPEC, err = bpf_prog_test_load(tgt_obj_file, BPF_PROG_TYPE_UNSPEC,
&tgt_obj, &tgt_fd); &tgt_obj, &tgt_fd);
if (CHECK(err, "second_prog_load", "file %s err %d errno %d\n", if (CHECK(err, "second_prog_load", "file %s err %d errno %d\n",
tgt_obj_file, err, errno)) tgt_obj_file, err, errno))
...@@ -274,7 +274,7 @@ static void test_fmod_ret_freplace(void) ...@@ -274,7 +274,7 @@ static void test_fmod_ret_freplace(void)
__u32 duration = 0; __u32 duration = 0;
int err, pkt_fd, attach_prog_fd; int err, pkt_fd, attach_prog_fd;
err = bpf_prog_load(tgt_name, BPF_PROG_TYPE_UNSPEC, err = bpf_prog_test_load(tgt_name, BPF_PROG_TYPE_UNSPEC,
&pkt_obj, &pkt_fd); &pkt_obj, &pkt_fd);
/* the target prog should load fine */ /* the target prog should load fine */
if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n",
...@@ -341,7 +341,7 @@ static void test_obj_load_failure_common(const char *obj_file, ...@@ -341,7 +341,7 @@ static void test_obj_load_failure_common(const char *obj_file,
int err, pkt_fd; int err, pkt_fd;
__u32 duration = 0; __u32 duration = 0;
err = bpf_prog_load(target_obj_file, BPF_PROG_TYPE_UNSPEC, err = bpf_prog_test_load(target_obj_file, BPF_PROG_TYPE_UNSPEC,
&pkt_obj, &pkt_fd); &pkt_obj, &pkt_fd);
/* the target prog should load fine */ /* the target prog should load fine */
if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n",
......
...@@ -20,34 +20,33 @@ void test_fexit_stress(void) ...@@ -20,34 +20,33 @@ void test_fexit_stress(void)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
struct bpf_load_program_attr load_attr = { LIBBPF_OPTS(bpf_prog_load_opts, trace_opts,
.prog_type = BPF_PROG_TYPE_TRACING,
.license = "GPL",
.insns = trace_program,
.insns_cnt = sizeof(trace_program) / sizeof(struct bpf_insn),
.expected_attach_type = BPF_TRACE_FEXIT, .expected_attach_type = BPF_TRACE_FEXIT,
}; .log_buf = error,
.log_size = sizeof(error),
);
const struct bpf_insn skb_program[] = { const struct bpf_insn skb_program[] = {
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
struct bpf_load_program_attr skb_load_attr = { LIBBPF_OPTS(bpf_prog_load_opts, skb_opts,
.prog_type = BPF_PROG_TYPE_SOCKET_FILTER, .log_buf = error,
.license = "GPL", .log_size = sizeof(error),
.insns = skb_program, );
.insns_cnt = sizeof(skb_program) / sizeof(struct bpf_insn),
};
err = libbpf_find_vmlinux_btf_id("bpf_fentry_test1", err = libbpf_find_vmlinux_btf_id("bpf_fentry_test1",
load_attr.expected_attach_type); trace_opts.expected_attach_type);
if (CHECK(err <= 0, "find_vmlinux_btf_id", "failed: %d\n", err)) if (CHECK(err <= 0, "find_vmlinux_btf_id", "failed: %d\n", err))
goto out; goto out;
load_attr.attach_btf_id = err; trace_opts.attach_btf_id = err;
for (i = 0; i < CNT; i++) { for (i = 0; i < CNT; i++) {
fexit_fd[i] = bpf_load_program_xattr(&load_attr, error, sizeof(error)); fexit_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL",
trace_program,
sizeof(trace_program) / sizeof(struct bpf_insn),
&trace_opts);
if (CHECK(fexit_fd[i] < 0, "fexit loaded", if (CHECK(fexit_fd[i] < 0, "fexit loaded",
"failed: %d errno %d\n", fexit_fd[i], errno)) "failed: %d errno %d\n", fexit_fd[i], errno))
goto out; goto out;
...@@ -57,7 +56,9 @@ void test_fexit_stress(void) ...@@ -57,7 +56,9 @@ void test_fexit_stress(void)
goto out; goto out;
} }
filter_fd = bpf_load_program_xattr(&skb_load_attr, error, sizeof(error)); filter_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
skb_program, sizeof(skb_program) / sizeof(struct bpf_insn),
&skb_opts);
if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n", if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n",
filter_fd, errno)) filter_fd, errno))
goto out; goto out;
......
...@@ -30,7 +30,7 @@ void serial_test_flow_dissector_load_bytes(void) ...@@ -30,7 +30,7 @@ void serial_test_flow_dissector_load_bytes(void)
/* make sure bpf_skb_load_bytes is not allowed from skb-less context /* make sure bpf_skb_load_bytes is not allowed from skb-less context
*/ */
fd = bpf_load_program(BPF_PROG_TYPE_FLOW_DISSECTOR, prog, fd = bpf_test_load_program(BPF_PROG_TYPE_FLOW_DISSECTOR, prog,
ARRAY_SIZE(prog), "GPL", 0, NULL, 0); ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
CHECK(fd < 0, CHECK(fd < 0,
"flow_dissector-bpf_skb_load_bytes-load", "flow_dissector-bpf_skb_load_bytes-load",
......
...@@ -47,9 +47,9 @@ static int load_prog(enum bpf_prog_type type) ...@@ -47,9 +47,9 @@ static int load_prog(enum bpf_prog_type type)
}; };
int fd; int fd;
fd = bpf_load_program(type, prog, ARRAY_SIZE(prog), "GPL", 0, NULL, 0); fd = bpf_test_load_program(type, prog, ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
if (CHECK_FAIL(fd < 0)) if (CHECK_FAIL(fd < 0))
perror("bpf_load_program"); perror("bpf_test_load_program");
return fd; return fd;
} }
......
...@@ -94,11 +94,11 @@ void test_get_stack_raw_tp(void) ...@@ -94,11 +94,11 @@ void test_get_stack_raw_tp(void)
struct bpf_map *map; struct bpf_map *map;
cpu_set_t cpu_set; cpu_set_t cpu_set;
err = bpf_prog_load(file_err, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_test_load(file_err, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err >= 0, "prog_load raw tp", "err %d errno %d\n", err, errno)) if (CHECK(err >= 0, "prog_load raw tp", "err %d errno %d\n", err, errno))
return; return;
err = bpf_prog_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno)) if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno))
return; return;
......
...@@ -136,7 +136,7 @@ void test_global_data(void) ...@@ -136,7 +136,7 @@ void test_global_data(void)
struct bpf_object *obj; struct bpf_object *obj;
int err, prog_fd; int err, prog_fd;
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
if (CHECK(err, "load program", "error %d loading %s\n", err, file)) if (CHECK(err, "load program", "error %d loading %s\n", err, file))
return; return;
......
...@@ -44,7 +44,7 @@ void test_global_func_args(void) ...@@ -44,7 +44,7 @@ void test_global_func_args(void)
struct bpf_object *obj; struct bpf_object *obj;
int err, prog_fd; int err, prog_fd;
err = bpf_prog_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd);
if (CHECK(err, "load program", "error %d loading %s\n", err, file)) if (CHECK(err, "load program", "error %d loading %s\n", err, file))
return; return;
......
...@@ -74,7 +74,7 @@ void serial_test_kfree_skb(void) ...@@ -74,7 +74,7 @@ void serial_test_kfree_skb(void)
const int zero = 0; const int zero = 0;
bool test_ok[2]; bool test_ok[2];
err = bpf_prog_load("./test_pkt_access.o", BPF_PROG_TYPE_SCHED_CLS, err = bpf_prog_test_load("./test_pkt_access.o", BPF_PROG_TYPE_SCHED_CLS,
&obj, &tattr.prog_fd); &obj, &tattr.prog_fd);
if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno)) if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno))
return; return;
......
...@@ -30,7 +30,7 @@ static void test_l4lb(const char *file) ...@@ -30,7 +30,7 @@ static void test_l4lb(const char *file)
char buf[128]; char buf[128];
u32 *magic = (u32 *)buf; u32 *magic = (u32 *)buf;
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
......
...@@ -27,7 +27,7 @@ void test_load_bytes_relative(void) ...@@ -27,7 +27,7 @@ void test_load_bytes_relative(void)
if (CHECK_FAIL(server_fd < 0)) if (CHECK_FAIL(server_fd < 0))
goto close_cgroup_fd; goto close_cgroup_fd;
err = bpf_prog_load("./load_bytes_relative.o", BPF_PROG_TYPE_CGROUP_SKB, err = bpf_prog_test_load("./load_bytes_relative.o", BPF_PROG_TYPE_CGROUP_SKB,
&obj, &prog_fd); &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
goto close_server_fd; goto close_server_fd;
......
...@@ -53,9 +53,9 @@ void test_map_lock(void) ...@@ -53,9 +53,9 @@ void test_map_lock(void)
int err = 0, key = 0, i; int err = 0, key = 0, i;
void *ret; void *ret;
err = bpf_prog_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd);
if (CHECK_FAIL(err)) { if (CHECK_FAIL(err)) {
printf("test_map_lock:bpf_prog_load errno %d\n", errno); printf("test_map_lock:bpf_prog_test_load errno %d\n", errno);
goto close_prog; goto close_prog;
} }
map_fd[0] = bpf_find_map(__func__, obj, "hash_map"); map_fd[0] = bpf_find_map(__func__, obj, "hash_map");
......
...@@ -9,7 +9,7 @@ void test_pkt_access(void) ...@@ -9,7 +9,7 @@ void test_pkt_access(void)
__u32 duration, retval; __u32 duration, retval;
int err, prog_fd; int err, prog_fd;
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
......
...@@ -9,7 +9,7 @@ void test_pkt_md_access(void) ...@@ -9,7 +9,7 @@ void test_pkt_md_access(void)
__u32 duration, retval; __u32 duration, retval;
int err, prog_fd; int err, prog_fd;
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
......
...@@ -27,7 +27,7 @@ static void test_queue_stack_map_by_type(int type) ...@@ -27,7 +27,7 @@ static void test_queue_stack_map_by_type(int type)
else else
return; return;
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
......
...@@ -18,15 +18,15 @@ void test_raw_tp_writable_reject_nbd_invalid(void) ...@@ -18,15 +18,15 @@ void test_raw_tp_writable_reject_nbd_invalid(void)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
struct bpf_load_program_attr load_attr = { LIBBPF_OPTS(bpf_prog_load_opts, opts,
.prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
.license = "GPL v2",
.insns = program,
.insns_cnt = sizeof(program) / sizeof(struct bpf_insn),
.log_level = 2, .log_level = 2,
}; .log_buf = error,
.log_size = sizeof(error),
);
bpf_fd = bpf_load_program_xattr(&load_attr, error, sizeof(error)); bpf_fd = bpf_prog_load(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, NULL, "GPL v2",
program, sizeof(program) / sizeof(struct bpf_insn),
&opts);
if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable load", if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable load",
"failed: %d errno %d\n", bpf_fd, errno)) "failed: %d errno %d\n", bpf_fd, errno))
return; return;
......
...@@ -17,15 +17,15 @@ void serial_test_raw_tp_writable_test_run(void) ...@@ -17,15 +17,15 @@ void serial_test_raw_tp_writable_test_run(void)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
struct bpf_load_program_attr load_attr = { LIBBPF_OPTS(bpf_prog_load_opts, trace_opts,
.prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
.license = "GPL v2",
.insns = trace_program,
.insns_cnt = sizeof(trace_program) / sizeof(struct bpf_insn),
.log_level = 2, .log_level = 2,
}; .log_buf = error,
.log_size = sizeof(error),
);
int bpf_fd = bpf_load_program_xattr(&load_attr, error, sizeof(error)); int bpf_fd = bpf_prog_load(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, NULL, "GPL v2",
trace_program, sizeof(trace_program) / sizeof(struct bpf_insn),
&trace_opts);
if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable loaded", if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable loaded",
"failed: %d errno %d\n", bpf_fd, errno)) "failed: %d errno %d\n", bpf_fd, errno))
return; return;
...@@ -35,15 +35,14 @@ void serial_test_raw_tp_writable_test_run(void) ...@@ -35,15 +35,14 @@ void serial_test_raw_tp_writable_test_run(void)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
struct bpf_load_program_attr skb_load_attr = { LIBBPF_OPTS(bpf_prog_load_opts, skb_opts,
.prog_type = BPF_PROG_TYPE_SOCKET_FILTER, .log_buf = error,
.license = "GPL v2", .log_size = sizeof(error),
.insns = skb_program, );
.insns_cnt = sizeof(skb_program) / sizeof(struct bpf_insn),
};
int filter_fd = int filter_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL v2",
bpf_load_program_xattr(&skb_load_attr, error, sizeof(error)); skb_program, sizeof(skb_program) / sizeof(struct bpf_insn),
&skb_opts);
if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n", if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n",
filter_fd, errno)) filter_fd, errno))
goto out_bpffd; goto out_bpffd;
......
...@@ -22,7 +22,7 @@ static void test_signal_pending_by_type(enum bpf_prog_type prog_type) ...@@ -22,7 +22,7 @@ static void test_signal_pending_by_type(enum bpf_prog_type prog_type)
prog[i] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0); prog[i] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0);
prog[ARRAY_SIZE(prog) - 1] = BPF_EXIT_INSN(); prog[ARRAY_SIZE(prog) - 1] = BPF_EXIT_INSN();
prog_fd = bpf_load_program(prog_type, prog, ARRAY_SIZE(prog), prog_fd = bpf_test_load_program(prog_type, prog, ARRAY_SIZE(prog),
"GPL", 0, NULL, 0); "GPL", 0, NULL, 0);
CHECK(prog_fd < 0, "test-run", "errno %d\n", errno); CHECK(prog_fd < 0, "test-run", "errno %d\n", errno);
......
...@@ -32,7 +32,7 @@ void test_skb_ctx(void) ...@@ -32,7 +32,7 @@ void test_skb_ctx(void)
int err; int err;
int i; int i;
err = bpf_prog_load("./test_skb_ctx.o", BPF_PROG_TYPE_SCHED_CLS, &obj, err = bpf_prog_test_load("./test_skb_ctx.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
&tattr.prog_fd); &tattr.prog_fd);
if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno)) if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
return; return;
......
...@@ -20,7 +20,7 @@ void test_skb_helpers(void) ...@@ -20,7 +20,7 @@ void test_skb_helpers(void)
struct bpf_object *obj; struct bpf_object *obj;
int err; int err;
err = bpf_prog_load("./test_skb_helpers.o", BPF_PROG_TYPE_SCHED_CLS, &obj, err = bpf_prog_test_load("./test_skb_helpers.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
&tattr.prog_fd); &tattr.prog_fd);
if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno)) if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
return; return;
......
...@@ -852,22 +852,21 @@ static struct sockopt_test { ...@@ -852,22 +852,21 @@ static struct sockopt_test {
static int load_prog(const struct bpf_insn *insns, static int load_prog(const struct bpf_insn *insns,
enum bpf_attach_type expected_attach_type) enum bpf_attach_type expected_attach_type)
{ {
struct bpf_load_program_attr attr = { LIBBPF_OPTS(bpf_prog_load_opts, opts,
.prog_type = BPF_PROG_TYPE_CGROUP_SOCKOPT,
.expected_attach_type = expected_attach_type, .expected_attach_type = expected_attach_type,
.insns = insns,
.license = "GPL",
.log_level = 2, .log_level = 2,
}; .log_buf = bpf_log_buf,
int fd; .log_size = sizeof(bpf_log_buf),
);
int fd, insns_cnt = 0;
for (; for (;
insns[attr.insns_cnt].code != (BPF_JMP | BPF_EXIT); insns[insns_cnt].code != (BPF_JMP | BPF_EXIT);
attr.insns_cnt++) { insns_cnt++) {
} }
attr.insns_cnt++; insns_cnt++;
fd = bpf_load_program_xattr(&attr, bpf_log_buf, sizeof(bpf_log_buf)); fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCKOPT, NULL, "GPL", insns, insns_cnt, &opts);
if (verbose && fd < 0) if (verbose && fd < 0)
fprintf(stderr, "%s\n", bpf_log_buf); fprintf(stderr, "%s\n", bpf_log_buf);
......
...@@ -24,9 +24,9 @@ void test_spinlock(void) ...@@ -24,9 +24,9 @@ void test_spinlock(void)
int err = 0, i; int err = 0, i;
void *ret; void *ret;
err = bpf_prog_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_CGROUP_SKB, &obj, &prog_fd);
if (CHECK_FAIL(err)) { if (CHECK_FAIL(err)) {
printf("test_spin_lock:bpf_prog_load errno %d\n", errno); printf("test_spin_lock:bpf_prog_test_load errno %d\n", errno);
goto close_prog; goto close_prog;
} }
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
......
...@@ -12,7 +12,7 @@ void test_stacktrace_map(void) ...@@ -12,7 +12,7 @@ void test_stacktrace_map(void)
struct bpf_object *obj; struct bpf_object *obj;
struct bpf_link *link; struct bpf_link *link;
err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno)) if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
return; return;
......
...@@ -12,7 +12,7 @@ void test_stacktrace_map_raw_tp(void) ...@@ -12,7 +12,7 @@ void test_stacktrace_map_raw_tp(void)
struct bpf_object *obj; struct bpf_object *obj;
struct bpf_link *link = NULL; struct bpf_link *link = NULL;
err = bpf_prog_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno)) if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno))
return; return;
......
...@@ -16,7 +16,7 @@ static void test_tailcall_1(void) ...@@ -16,7 +16,7 @@ static void test_tailcall_1(void)
char prog_name[32]; char prog_name[32];
char buff[128] = {}; char buff[128] = {};
err = bpf_prog_load("tailcall1.o", BPF_PROG_TYPE_SCHED_CLS, &obj, err = bpf_prog_test_load("tailcall1.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
&prog_fd); &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
...@@ -154,7 +154,7 @@ static void test_tailcall_2(void) ...@@ -154,7 +154,7 @@ static void test_tailcall_2(void)
char prog_name[32]; char prog_name[32];
char buff[128] = {}; char buff[128] = {};
err = bpf_prog_load("tailcall2.o", BPF_PROG_TYPE_SCHED_CLS, &obj, err = bpf_prog_test_load("tailcall2.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
&prog_fd); &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
...@@ -228,7 +228,7 @@ static void test_tailcall_count(const char *which) ...@@ -228,7 +228,7 @@ static void test_tailcall_count(const char *which)
__u32 retval, duration; __u32 retval, duration;
char buff[128] = {}; char buff[128] = {};
err = bpf_prog_load(which, BPF_PROG_TYPE_SCHED_CLS, &obj, err = bpf_prog_test_load(which, BPF_PROG_TYPE_SCHED_CLS, &obj,
&prog_fd); &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
...@@ -324,7 +324,7 @@ static void test_tailcall_4(void) ...@@ -324,7 +324,7 @@ static void test_tailcall_4(void)
char buff[128] = {}; char buff[128] = {};
char prog_name[32]; char prog_name[32];
err = bpf_prog_load("tailcall4.o", BPF_PROG_TYPE_SCHED_CLS, &obj, err = bpf_prog_test_load("tailcall4.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
&prog_fd); &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
...@@ -412,7 +412,7 @@ static void test_tailcall_5(void) ...@@ -412,7 +412,7 @@ static void test_tailcall_5(void)
char buff[128] = {}; char buff[128] = {};
char prog_name[32]; char prog_name[32];
err = bpf_prog_load("tailcall5.o", BPF_PROG_TYPE_SCHED_CLS, &obj, err = bpf_prog_test_load("tailcall5.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
&prog_fd); &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
...@@ -498,7 +498,7 @@ static void test_tailcall_bpf2bpf_1(void) ...@@ -498,7 +498,7 @@ static void test_tailcall_bpf2bpf_1(void)
__u32 retval, duration; __u32 retval, duration;
char prog_name[32]; char prog_name[32];
err = bpf_prog_load("tailcall_bpf2bpf1.o", BPF_PROG_TYPE_SCHED_CLS, err = bpf_prog_test_load("tailcall_bpf2bpf1.o", BPF_PROG_TYPE_SCHED_CLS,
&obj, &prog_fd); &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
...@@ -582,7 +582,7 @@ static void test_tailcall_bpf2bpf_2(void) ...@@ -582,7 +582,7 @@ static void test_tailcall_bpf2bpf_2(void)
__u32 retval, duration; __u32 retval, duration;
char buff[128] = {}; char buff[128] = {};
err = bpf_prog_load("tailcall_bpf2bpf2.o", BPF_PROG_TYPE_SCHED_CLS, err = bpf_prog_test_load("tailcall_bpf2bpf2.o", BPF_PROG_TYPE_SCHED_CLS,
&obj, &prog_fd); &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
...@@ -660,7 +660,7 @@ static void test_tailcall_bpf2bpf_3(void) ...@@ -660,7 +660,7 @@ static void test_tailcall_bpf2bpf_3(void)
__u32 retval, duration; __u32 retval, duration;
char prog_name[32]; char prog_name[32];
err = bpf_prog_load("tailcall_bpf2bpf3.o", BPF_PROG_TYPE_SCHED_CLS, err = bpf_prog_test_load("tailcall_bpf2bpf3.o", BPF_PROG_TYPE_SCHED_CLS,
&obj, &prog_fd); &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
...@@ -757,7 +757,7 @@ static void test_tailcall_bpf2bpf_4(bool noise) ...@@ -757,7 +757,7 @@ static void test_tailcall_bpf2bpf_4(bool noise)
__u32 retval, duration; __u32 retval, duration;
char prog_name[32]; char prog_name[32];
err = bpf_prog_load("tailcall_bpf2bpf4.o", BPF_PROG_TYPE_SCHED_CLS, err = bpf_prog_test_load("tailcall_bpf2bpf4.o", BPF_PROG_TYPE_SCHED_CLS,
&obj, &prog_fd); &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
......
...@@ -11,7 +11,7 @@ void test_task_fd_query_rawtp(void) ...@@ -11,7 +11,7 @@ void test_task_fd_query_rawtp(void)
__u32 duration = 0; __u32 duration = 0;
char buf[256]; char buf[256];
err = bpf_prog_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno)) if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno))
return; return;
......
...@@ -13,8 +13,8 @@ static void test_task_fd_query_tp_core(const char *probe_name, ...@@ -13,8 +13,8 @@ static void test_task_fd_query_tp_core(const char *probe_name,
__u32 duration = 0; __u32 duration = 0;
char buf[256]; char buf[256];
err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "bpf_prog_load", "err %d errno %d\n", err, errno)) if (CHECK(err, "bpf_prog_test_load", "err %d errno %d\n", err, errno))
goto close_prog; goto close_prog;
snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),
......
...@@ -8,7 +8,7 @@ void test_tcp_estats(void) ...@@ -8,7 +8,7 @@ void test_tcp_estats(void)
struct bpf_object *obj; struct bpf_object *obj;
__u32 duration = 0; __u32 duration = 0;
err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
CHECK(err, "", "err %d errno %d\n", err, errno); CHECK(err, "", "err %d errno %d\n", err, errno);
if (err) if (err)
return; return;
......
...@@ -35,7 +35,7 @@ void serial_test_tp_attach_query(void) ...@@ -35,7 +35,7 @@ void serial_test_tp_attach_query(void)
query = malloc(sizeof(*query) + sizeof(__u32) * num_progs); query = malloc(sizeof(*query) + sizeof(__u32) * num_progs);
for (i = 0; i < num_progs; i++) { for (i = 0; i < num_progs; i++) {
err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj[i], err = bpf_prog_test_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj[i],
&prog_fd[i]); &prog_fd[i]);
if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno)) if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
goto cleanup1; goto cleanup1;
......
...@@ -16,7 +16,7 @@ void test_xdp(void) ...@@ -16,7 +16,7 @@ void test_xdp(void)
__u32 duration, retval, size; __u32 duration, retval, size;
int err, prog_fd, map_fd; int err, prog_fd, map_fd;
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
......
...@@ -10,7 +10,7 @@ static void test_xdp_adjust_tail_shrink(void) ...@@ -10,7 +10,7 @@ static void test_xdp_adjust_tail_shrink(void)
int err, prog_fd; int err, prog_fd;
char buf[128]; char buf[128];
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
...@@ -38,7 +38,7 @@ static void test_xdp_adjust_tail_grow(void) ...@@ -38,7 +38,7 @@ static void test_xdp_adjust_tail_grow(void)
__u32 duration, retval, size, expect_sz; __u32 duration, retval, size, expect_sz;
int err, prog_fd; int err, prog_fd;
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
...@@ -75,7 +75,7 @@ static void test_xdp_adjust_tail_grow2(void) ...@@ -75,7 +75,7 @@ static void test_xdp_adjust_tail_grow2(void)
.data_size_out = 0, /* Per test */ .data_size_out = 0, /* Per test */
}; };
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &tattr.prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &tattr.prog_fd);
if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno)) if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
return; return;
......
...@@ -16,7 +16,7 @@ void serial_test_xdp_attach(void) ...@@ -16,7 +16,7 @@ void serial_test_xdp_attach(void)
len = sizeof(info); len = sizeof(info);
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj1, &fd1); err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj1, &fd1);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
err = bpf_obj_get_info_by_fd(fd1, &info, &len); err = bpf_obj_get_info_by_fd(fd1, &info, &len);
...@@ -24,7 +24,7 @@ void serial_test_xdp_attach(void) ...@@ -24,7 +24,7 @@ void serial_test_xdp_attach(void)
goto out_1; goto out_1;
id1 = info.id; id1 = info.id;
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj2, &fd2); err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj2, &fd2);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
goto out_1; goto out_1;
...@@ -34,7 +34,7 @@ void serial_test_xdp_attach(void) ...@@ -34,7 +34,7 @@ void serial_test_xdp_attach(void)
goto out_2; goto out_2;
id2 = info.id; id2 = info.id;
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj3, &fd3); err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj3, &fd3);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
goto out_2; goto out_2;
......
...@@ -29,7 +29,7 @@ void serial_test_xdp_info(void) ...@@ -29,7 +29,7 @@ void serial_test_xdp_info(void)
/* Setup prog */ /* Setup prog */
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
......
...@@ -9,7 +9,7 @@ void test_xdp_perf(void) ...@@ -9,7 +9,7 @@ void test_xdp_perf(void)
char in[128], out[128]; char in[128], out[128];
int err, prog_fd; int err, prog_fd;
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd); err = bpf_prog_test_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
return; return;
......
...@@ -73,7 +73,7 @@ int test_subprog2(struct args_subprog2 *ctx) ...@@ -73,7 +73,7 @@ int test_subprog2(struct args_subprog2 *ctx)
__builtin_preserve_access_index(&skb->len)); __builtin_preserve_access_index(&skb->len));
ret = ctx->ret; ret = ctx->ret;
/* bpf_prog_load() loads "test_pkt_access.o" with BPF_F_TEST_RND_HI32 /* bpf_prog_test_load() loads "test_pkt_access.o" with BPF_F_TEST_RND_HI32
* which randomizes upper 32 bits after BPF_ALU32 insns. * which randomizes upper 32 bits after BPF_ALU32 insns.
* Hence after 'w0 <<= 1' upper bits of $rax are random. * Hence after 'w0 <<= 1' upper bits of $rax are random.
* That is expected and correct. Trim them. * That is expected and correct. Trim them.
......
...@@ -448,7 +448,7 @@ static __always_inline int process_packet(void *data, __u64 off, void *data_end, ...@@ -448,7 +448,7 @@ static __always_inline int process_packet(void *data, __u64 off, void *data_end,
return bpf_redirect(ifindex, 0); return bpf_redirect(ifindex, 0);
} }
SEC("l4lb-demo") SEC("tc")
int balancer_ingress(struct __sk_buff *ctx) int balancer_ingress(struct __sk_buff *ctx)
{ {
void *data_end = (void *)(long)ctx->data_end; void *data_end = (void *)(long)ctx->data_end;
......
...@@ -447,7 +447,7 @@ static __noinline int process_packet(void *data, __u64 off, void *data_end, ...@@ -447,7 +447,7 @@ static __noinline int process_packet(void *data, __u64 off, void *data_end,
return bpf_redirect(ifindex, 0); return bpf_redirect(ifindex, 0);
} }
SEC("l4lb-demo") SEC("tc")
int balancer_ingress(struct __sk_buff *ctx) int balancer_ingress(struct __sk_buff *ctx)
{ {
void *data_end = (void *)(long)ctx->data_end; void *data_end = (void *)(long)ctx->data_end;
......
...@@ -30,7 +30,7 @@ struct { ...@@ -30,7 +30,7 @@ struct {
__type(value, struct array_elem); __type(value, struct array_elem);
} array_map SEC(".maps"); } array_map SEC(".maps");
SEC("map_lock_demo") SEC("cgroup/skb")
int bpf_map_lock_test(struct __sk_buff *skb) int bpf_map_lock_test(struct __sk_buff *skb)
{ {
struct hmap_elem zero = {}, *val; struct hmap_elem zero = {}, *val;
......
...@@ -24,7 +24,7 @@ struct { ...@@ -24,7 +24,7 @@ struct {
__uint(value_size, sizeof(__u32)); __uint(value_size, sizeof(__u32));
} map_out SEC(".maps"); } map_out SEC(".maps");
SEC("test") SEC("tc")
int _test(struct __sk_buff *skb) int _test(struct __sk_buff *skb)
{ {
void *data_end = (void *)(long)skb->data_end; void *data_end = (void *)(long)skb->data_end;
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";
SEC("skb_ctx") SEC("tc")
int process(struct __sk_buff *skb) int process(struct __sk_buff *skb)
{ {
#pragma clang loop unroll(full) #pragma clang loop unroll(full)
......
...@@ -45,7 +45,7 @@ struct { ...@@ -45,7 +45,7 @@ struct {
#define CREDIT_PER_NS(delta, rate) (((delta) * rate) >> 20) #define CREDIT_PER_NS(delta, rate) (((delta) * rate) >> 20)
SEC("spin_lock_demo") SEC("tc")
int bpf_sping_lock_test(struct __sk_buff *skb) int bpf_sping_lock_test(struct __sk_buff *skb)
{ {
volatile int credit = 0, max_credit = 100, pkt_len = 64; volatile int credit = 0, max_credit = 100, pkt_len = 64;
......
...@@ -244,7 +244,7 @@ static __always_inline void send_basic_event(struct sock *sk, ...@@ -244,7 +244,7 @@ static __always_inline void send_basic_event(struct sock *sk,
bpf_map_update_elem(&ev_record_map, &key, &ev, BPF_ANY); bpf_map_update_elem(&ev_record_map, &key, &ev, BPF_ANY);
} }
SEC("dummy_tracepoint") SEC("tp/dummy/tracepoint")
int _dummy_tracepoint(struct dummy_tracepoint_args *arg) int _dummy_tracepoint(struct dummy_tracepoint_args *arg)
{ {
if (!arg->sock) if (!arg->sock)
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "bpf_rlimit.h" #include "bpf_rlimit.h"
#include "cgroup_helpers.h" #include "cgroup_helpers.h"
#include "testing_helpers.h"
char bpf_log_buf[BPF_LOG_BUF_SIZE]; char bpf_log_buf[BPF_LOG_BUF_SIZE];
...@@ -66,7 +67,7 @@ int main(int argc, char **argv) ...@@ -66,7 +67,7 @@ int main(int argc, char **argv)
prog[0].imm = percpu_map_fd; prog[0].imm = percpu_map_fd;
prog[7].imm = map_fd; prog[7].imm = map_fd;
prog_fd = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB, prog_fd = bpf_test_load_program(BPF_PROG_TYPE_CGROUP_SKB,
prog, insns_cnt, "GPL", 0, prog, insns_cnt, "GPL", 0,
bpf_log_buf, BPF_LOG_BUF_SIZE); bpf_log_buf, BPF_LOG_BUF_SIZE);
if (prog_fd < 0) { if (prog_fd < 0) {
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include "cgroup_helpers.h" #include "cgroup_helpers.h"
#include "testing_helpers.h"
#include "bpf_rlimit.h" #include "bpf_rlimit.h"
#define DEV_CGROUP_PROG "./dev_cgroup.o" #define DEV_CGROUP_PROG "./dev_cgroup.o"
...@@ -27,7 +28,7 @@ int main(int argc, char **argv) ...@@ -27,7 +28,7 @@ int main(int argc, char **argv)
int prog_fd, cgroup_fd; int prog_fd, cgroup_fd;
__u32 prog_cnt; __u32 prog_cnt;
if (bpf_prog_load(DEV_CGROUP_PROG, BPF_PROG_TYPE_CGROUP_DEVICE, if (bpf_prog_test_load(DEV_CGROUP_PROG, BPF_PROG_TYPE_CGROUP_DEVICE,
&obj, &prog_fd)) { &obj, &prog_fd)) {
printf("Failed to load DEV_CGROUP program\n"); printf("Failed to load DEV_CGROUP program\n");
goto out; goto out;
......
...@@ -45,6 +45,8 @@ ...@@ -45,6 +45,8 @@
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include "testing_helpers.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct bpf_object *obj; struct bpf_object *obj;
...@@ -58,8 +60,8 @@ int main(int argc, char **argv) ...@@ -58,8 +60,8 @@ int main(int argc, char **argv)
return 2; return 2;
} }
ret = bpf_prog_load("test_lirc_mode2_kern.o", ret = bpf_prog_test_load("test_lirc_mode2_kern.o",
BPF_PROG_TYPE_LIRC_MODE2, &obj, &progfd); BPF_PROG_TYPE_LIRC_MODE2, &obj, &progfd);
if (ret) { if (ret) {
printf("Failed to load bpf program\n"); printf("Failed to load bpf program\n");
return 1; return 1;
......
...@@ -42,7 +42,6 @@ static int create_map(int map_type, int map_flags, unsigned int size) ...@@ -42,7 +42,6 @@ static int create_map(int map_type, int map_flags, unsigned int size)
static int bpf_map_lookup_elem_with_ref_bit(int fd, unsigned long long key, static int bpf_map_lookup_elem_with_ref_bit(int fd, unsigned long long key,
void *value) void *value)
{ {
struct bpf_load_program_attr prog;
struct bpf_create_map_attr map; struct bpf_create_map_attr map;
struct bpf_insn insns[] = { struct bpf_insn insns[] = {
BPF_LD_MAP_VALUE(BPF_REG_9, 0, 0), BPF_LD_MAP_VALUE(BPF_REG_9, 0, 0),
...@@ -76,13 +75,7 @@ static int bpf_map_lookup_elem_with_ref_bit(int fd, unsigned long long key, ...@@ -76,13 +75,7 @@ static int bpf_map_lookup_elem_with_ref_bit(int fd, unsigned long long key,
insns[0].imm = mfd; insns[0].imm = mfd;
memset(&prog, 0, sizeof(prog)); pfd = bpf_prog_load(BPF_PROG_TYPE_SCHED_CLS, NULL, "GPL", insns, ARRAY_SIZE(insns), NULL);
prog.prog_type = BPF_PROG_TYPE_SCHED_CLS;
prog.insns = insns;
prog.insns_cnt = ARRAY_SIZE(insns);
prog.license = "GPL";
pfd = bpf_load_program_xattr(&prog, NULL, 0);
if (pfd < 0) { if (pfd < 0) {
close(mfd); close(mfd);
return -1; return -1;
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "bpf_util.h" #include "bpf_util.h"
#include "bpf_rlimit.h" #include "bpf_rlimit.h"
#include "test_maps.h" #include "test_maps.h"
#include "testing_helpers.h"
#ifndef ENOTSUPP #ifndef ENOTSUPP
#define ENOTSUPP 524 #define ENOTSUPP 524
...@@ -830,21 +831,21 @@ static void test_sockmap(unsigned int tasks, void *data) ...@@ -830,21 +831,21 @@ static void test_sockmap(unsigned int tasks, void *data)
} }
/* Load SK_SKB program and Attach */ /* Load SK_SKB program and Attach */
err = bpf_prog_load(SOCKMAP_PARSE_PROG, err = bpf_prog_test_load(SOCKMAP_PARSE_PROG,
BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog); BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog);
if (err) { if (err) {
printf("Failed to load SK_SKB parse prog\n"); printf("Failed to load SK_SKB parse prog\n");
goto out_sockmap; goto out_sockmap;
} }
err = bpf_prog_load(SOCKMAP_TCP_MSG_PROG, err = bpf_prog_test_load(SOCKMAP_TCP_MSG_PROG,
BPF_PROG_TYPE_SK_MSG, &obj, &msg_prog); BPF_PROG_TYPE_SK_MSG, &obj, &msg_prog);
if (err) { if (err) {
printf("Failed to load SK_SKB msg prog\n"); printf("Failed to load SK_SKB msg prog\n");
goto out_sockmap; goto out_sockmap;
} }
err = bpf_prog_load(SOCKMAP_VERDICT_PROG, err = bpf_prog_test_load(SOCKMAP_VERDICT_PROG,
BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog); BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog);
if (err) { if (err) {
printf("Failed to load SK_SKB verdict prog\n"); printf("Failed to load SK_SKB verdict prog\n");
......
...@@ -328,18 +328,17 @@ static size_t probe_prog_length(const struct bpf_insn *fp) ...@@ -328,18 +328,17 @@ static size_t probe_prog_length(const struct bpf_insn *fp)
static int load_sock_prog(const struct bpf_insn *prog, static int load_sock_prog(const struct bpf_insn *prog,
enum bpf_attach_type attach_type) enum bpf_attach_type attach_type)
{ {
struct bpf_load_program_attr attr; LIBBPF_OPTS(bpf_prog_load_opts, opts);
int ret; int ret, insn_cnt;
memset(&attr, 0, sizeof(struct bpf_load_program_attr)); insn_cnt = probe_prog_length(prog);
attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
attr.expected_attach_type = attach_type; opts.expected_attach_type = attach_type;
attr.insns = prog; opts.log_buf = bpf_log_buf;
attr.insns_cnt = probe_prog_length(attr.insns); opts.log_size = BPF_LOG_BUF_SIZE;
attr.license = "GPL"; opts.log_level = 2;
attr.log_level = 2;
ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, NULL, "GPL", prog, insn_cnt, &opts);
ret = bpf_load_program_xattr(&attr, bpf_log_buf, BPF_LOG_BUF_SIZE);
if (verbose && ret < 0) if (verbose && ret < 0)
fprintf(stderr, "%s\n", bpf_log_buf); fprintf(stderr, "%s\n", bpf_log_buf);
......
...@@ -645,17 +645,14 @@ static int mk_sockaddr(int domain, const char *ip, unsigned short port, ...@@ -645,17 +645,14 @@ static int mk_sockaddr(int domain, const char *ip, unsigned short port,
static int load_insns(const struct sock_addr_test *test, static int load_insns(const struct sock_addr_test *test,
const struct bpf_insn *insns, size_t insns_cnt) const struct bpf_insn *insns, size_t insns_cnt)
{ {
struct bpf_load_program_attr load_attr; LIBBPF_OPTS(bpf_prog_load_opts, opts);
int ret; int ret;
memset(&load_attr, 0, sizeof(struct bpf_load_program_attr)); opts.expected_attach_type = test->expected_attach_type;
load_attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; opts.log_buf = bpf_log_buf;
load_attr.expected_attach_type = test->expected_attach_type; opts.log_size = BPF_LOG_BUF_SIZE;
load_attr.insns = insns;
load_attr.insns_cnt = insns_cnt;
load_attr.license = "GPL";
ret = bpf_load_program_xattr(&load_attr, bpf_log_buf, BPF_LOG_BUF_SIZE); ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, NULL, "GPL", insns, insns_cnt, &opts);
if (ret < 0 && test->expected_result != LOAD_REJECT) { if (ret < 0 && test->expected_result != LOAD_REJECT) {
log_err(">>> Loading program error.\n" log_err(">>> Loading program error.\n"
">>> Verifier output:\n%s\n-------\n", bpf_log_buf); ">>> Verifier output:\n%s\n-------\n", bpf_log_buf);
......
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/* Copyright (C) 2019 Netronome Systems, Inc. */
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <string.h>
int extra_prog_load_log_flags = 0;
int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
struct bpf_object **pobj, int *prog_fd)
{
struct bpf_prog_load_attr attr;
memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
attr.file = file;
attr.prog_type = type;
attr.expected_attach_type = 0;
attr.prog_flags = BPF_F_TEST_RND_HI32;
attr.log_level = extra_prog_load_log_flags;
return bpf_prog_load_xattr(&attr, pobj, prog_fd);
}
int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
size_t insns_cnt, const char *license,
__u32 kern_version, char *log_buf,
size_t log_buf_sz)
{
struct bpf_load_program_attr load_attr;
memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
load_attr.prog_type = type;
load_attr.expected_attach_type = 0;
load_attr.name = NULL;
load_attr.insns = insns;
load_attr.insns_cnt = insns_cnt;
load_attr.license = license;
load_attr.kern_version = kern_version;
load_attr.prog_flags = BPF_F_TEST_RND_HI32;
load_attr.log_level = extra_prog_load_log_flags;
return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz);
}
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "bpf_rlimit.h" #include "bpf_rlimit.h"
#include "bpf_util.h" #include "bpf_util.h"
#include "cgroup_helpers.h" #include "cgroup_helpers.h"
#include "testing_helpers.h"
#define CG_PATH "/foo" #define CG_PATH "/foo"
#define MAX_INSNS 512 #define MAX_INSNS 512
...@@ -1435,14 +1436,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test, ...@@ -1435,14 +1436,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test,
const char *sysctl_path) const char *sysctl_path)
{ {
struct bpf_insn *prog = test->insns; struct bpf_insn *prog = test->insns;
struct bpf_load_program_attr attr; LIBBPF_OPTS(bpf_prog_load_opts, opts);
int ret; int ret, insn_cnt;
memset(&attr, 0, sizeof(struct bpf_load_program_attr)); insn_cnt = probe_prog_length(prog);
attr.prog_type = BPF_PROG_TYPE_CGROUP_SYSCTL;
attr.insns = prog;
attr.insns_cnt = probe_prog_length(attr.insns);
attr.license = "GPL";
if (test->fixup_value_insn) { if (test->fixup_value_insn) {
char buf[128]; char buf[128];
...@@ -1465,7 +1462,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test, ...@@ -1465,7 +1462,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test,
return -1; return -1;
} }
ret = bpf_load_program_xattr(&attr, bpf_log_buf, BPF_LOG_BUF_SIZE); opts.log_buf = bpf_log_buf;
opts.log_size = BPF_LOG_BUF_SIZE;
ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SYSCTL, NULL, "GPL", prog, insn_cnt, &opts);
if (ret < 0 && test->result != LOAD_REJECT) { if (ret < 0 && test->result != LOAD_REJECT) {
log_err(">>> Loading program error.\n" log_err(">>> Loading program error.\n"
">>> Verifier output:\n%s\n-------\n", bpf_log_buf); ">>> Verifier output:\n%s\n-------\n", bpf_log_buf);
...@@ -1476,15 +1476,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test, ...@@ -1476,15 +1476,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test,
static int load_sysctl_prog_file(struct sysctl_test *test) static int load_sysctl_prog_file(struct sysctl_test *test)
{ {
struct bpf_prog_load_attr attr;
struct bpf_object *obj; struct bpf_object *obj;
int prog_fd; int prog_fd;
memset(&attr, 0, sizeof(struct bpf_prog_load_attr)); if (bpf_prog_test_load(test->prog_file, BPF_PROG_TYPE_CGROUP_SYSCTL, &obj, &prog_fd)) {
attr.file = test->prog_file;
attr.prog_type = BPF_PROG_TYPE_CGROUP_SYSCTL;
if (bpf_prog_load_xattr(&attr, &obj, &prog_fd)) {
if (test->result != LOAD_REJECT) if (test->result != LOAD_REJECT)
log_err(">>> Loading program (%s) error.\n", log_err(">>> Loading program (%s) error.\n",
test->prog_file); test->prog_file);
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "../../../include/linux/filter.h" #include "../../../include/linux/filter.h"
#include "bpf_rlimit.h" #include "bpf_rlimit.h"
#include "testing_helpers.h"
static struct bpf_insn prog[BPF_MAXINSNS]; static struct bpf_insn prog[BPF_MAXINSNS];
...@@ -57,7 +58,7 @@ static int bpf_try_load_prog(int insns, int fd_map, ...@@ -57,7 +58,7 @@ static int bpf_try_load_prog(int insns, int fd_map,
int fd_prog; int fd_prog;
bpf_filler(insns, fd_map); bpf_filler(insns, fd_map);
fd_prog = bpf_load_program(BPF_PROG_TYPE_SCHED_CLS, prog, insns, "", 0, fd_prog = bpf_test_load_program(BPF_PROG_TYPE_SCHED_CLS, prog, insns, "", 0,
NULL, 0); NULL, 0);
assert(fd_prog > 0); assert(fd_prog > 0);
if (fd_map > 0) if (fd_map > 0)
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "test_tcpnotify.h" #include "test_tcpnotify.h"
#include "trace_helpers.h" #include "trace_helpers.h"
#include "testing_helpers.h"
#define SOCKET_BUFFER_SIZE (getpagesize() < 8192L ? getpagesize() : 8192L) #define SOCKET_BUFFER_SIZE (getpagesize() < 8192L ? getpagesize() : 8192L)
...@@ -92,7 +93,7 @@ int main(int argc, char **argv) ...@@ -92,7 +93,7 @@ int main(int argc, char **argv)
if (cg_fd < 0) if (cg_fd < 0)
goto err; goto err;
if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) { if (bpf_prog_test_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
printf("FAILED: load_bpf_file failed for: %s\n", file); printf("FAILED: load_bpf_file failed for: %s\n", file);
goto err; goto err;
} }
......
...@@ -498,8 +498,7 @@ static int create_prog_dummy_simple(enum bpf_prog_type prog_type, int ret) ...@@ -498,8 +498,7 @@ static int create_prog_dummy_simple(enum bpf_prog_type prog_type, int ret)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
return bpf_load_program(prog_type, prog, return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL);
ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
} }
static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd, static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd,
...@@ -514,8 +513,7 @@ static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd, ...@@ -514,8 +513,7 @@ static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd,
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
return bpf_load_program(prog_type, prog, return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL);
ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
} }
static int create_prog_array(enum bpf_prog_type prog_type, uint32_t max_elem, static int create_prog_array(enum bpf_prog_type prog_type, uint32_t max_elem,
...@@ -1045,7 +1043,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv, ...@@ -1045,7 +1043,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
int fd_prog, expected_ret, alignment_prevented_execution; int fd_prog, expected_ret, alignment_prevented_execution;
int prog_len, prog_type = test->prog_type; int prog_len, prog_type = test->prog_type;
struct bpf_insn *prog = test->insns; struct bpf_insn *prog = test->insns;
struct bpf_load_program_attr attr; LIBBPF_OPTS(bpf_prog_load_opts, opts);
int run_errs, run_successes; int run_errs, run_successes;
int map_fds[MAX_NR_MAPS]; int map_fds[MAX_NR_MAPS];
const char *expected_err; const char *expected_err;
...@@ -1085,32 +1083,34 @@ static void do_test_single(struct bpf_test *test, bool unpriv, ...@@ -1085,32 +1083,34 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
test->result_unpriv : test->result; test->result_unpriv : test->result;
expected_err = unpriv && test->errstr_unpriv ? expected_err = unpriv && test->errstr_unpriv ?
test->errstr_unpriv : test->errstr; test->errstr_unpriv : test->errstr;
memset(&attr, 0, sizeof(attr));
attr.prog_type = prog_type; opts.expected_attach_type = test->expected_attach_type;
attr.expected_attach_type = test->expected_attach_type;
attr.insns = prog;
attr.insns_cnt = prog_len;
attr.license = "GPL";
if (verbose) if (verbose)
attr.log_level = 1; opts.log_level = 1;
else if (expected_ret == VERBOSE_ACCEPT) else if (expected_ret == VERBOSE_ACCEPT)
attr.log_level = 2; opts.log_level = 2;
else else
attr.log_level = 4; opts.log_level = 4;
attr.prog_flags = pflags; opts.prog_flags = pflags;
if (prog_type == BPF_PROG_TYPE_TRACING && test->kfunc) { if (prog_type == BPF_PROG_TYPE_TRACING && test->kfunc) {
attr.attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc, int attach_btf_id;
attr.expected_attach_type);
if (attr.attach_btf_id < 0) { attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc,
opts.expected_attach_type);
if (attach_btf_id < 0) {
printf("FAIL\nFailed to find BTF ID for '%s'!\n", printf("FAIL\nFailed to find BTF ID for '%s'!\n",
test->kfunc); test->kfunc);
(*errors)++; (*errors)++;
return; return;
} }
opts.attach_btf_id = attach_btf_id;
} }
fd_prog = bpf_load_program_xattr(&attr, bpf_vlog, sizeof(bpf_vlog)); opts.log_buf = bpf_vlog;
opts.log_size = sizeof(bpf_vlog);
fd_prog = bpf_prog_load(prog_type, NULL, "GPL", prog, prog_len, &opts);
saved_errno = errno; saved_errno = errno;
/* BPF_PROG_TYPE_TRACING requires more setup and /* BPF_PROG_TYPE_TRACING requires more setup and
......
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (C) 2019 Netronome Systems, Inc. */
/* Copyright (C) 2020 Facebook, Inc. */ /* Copyright (C) 2020 Facebook, Inc. */
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <errno.h> #include <errno.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include "testing_helpers.h" #include "testing_helpers.h"
int parse_num_list(const char *s, bool **num_set, int *num_set_len) int parse_num_list(const char *s, bool **num_set, int *num_set_len)
...@@ -78,3 +82,54 @@ __u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info) ...@@ -78,3 +82,54 @@ __u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info)
} }
return info->prog_id; return info->prog_id;
} }
int extra_prog_load_log_flags = 0;
int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
struct bpf_object **pobj, int *prog_fd)
{
struct bpf_object *obj;
struct bpf_program *prog;
int err;
obj = bpf_object__open(file);
if (!obj)
return -errno;
prog = bpf_object__next_program(obj, NULL);
if (!prog) {
err = -ENOENT;
goto err_out;
}
if (type != BPF_PROG_TYPE_UNSPEC)
bpf_program__set_type(prog, type);
err = bpf_object__load(obj);
if (err)
goto err_out;
*pobj = obj;
*prog_fd = bpf_program__fd(prog);
return 0;
err_out:
bpf_object__close(obj);
return err;
}
int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
size_t insns_cnt, const char *license,
__u32 kern_version, char *log_buf,
size_t log_buf_sz)
{
LIBBPF_OPTS(bpf_prog_load_opts, opts,
.kern_version = kern_version,
.prog_flags = BPF_F_TEST_RND_HI32,
.log_level = extra_prog_load_log_flags,
.log_buf = log_buf,
.log_size = log_buf_sz,
);
return bpf_prog_load(type, NULL, license, insns, insns_cnt, &opts);
}
...@@ -6,3 +6,9 @@ ...@@ -6,3 +6,9 @@
int parse_num_list(const char *s, bool **set, int *set_len); int parse_num_list(const char *s, bool **set, int *set_len);
__u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info); __u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info);
int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
struct bpf_object **pobj, int *prog_fd);
int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
size_t insns_cnt, const char *license,
__u32 kern_version, char *log_buf,
size_t log_buf_sz);
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "bpf/libbpf.h" #include "bpf/libbpf.h"
#include "xdping.h" #include "xdping.h"
#include "testing_helpers.h"
static int ifindex; static int ifindex;
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
...@@ -173,7 +174,7 @@ int main(int argc, char **argv) ...@@ -173,7 +174,7 @@ int main(int argc, char **argv)
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
if (bpf_prog_load(filename, BPF_PROG_TYPE_XDP, &obj, &prog_fd)) { if (bpf_prog_test_load(filename, BPF_PROG_TYPE_XDP, &obj, &prog_fd)) {
fprintf(stderr, "load of %s failed\n", filename); fprintf(stderr, "load of %s failed\n", filename);
return 1; return 1;
} }
......
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