18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
28c2ecf20Sopenharmony_ci/* Copyright (C) 2019 Netronome Systems, Inc. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <bpf/bpf.h>
58c2ecf20Sopenharmony_ci#include <bpf/libbpf.h>
68c2ecf20Sopenharmony_ci#include <string.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ciint extra_prog_load_log_flags = 0;
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciint bpf_prog_test_load(const char *file, enum bpf_prog_type type,
118c2ecf20Sopenharmony_ci		       struct bpf_object **pobj, int *prog_fd)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	struct bpf_prog_load_attr attr;
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci	memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
168c2ecf20Sopenharmony_ci	attr.file = file;
178c2ecf20Sopenharmony_ci	attr.prog_type = type;
188c2ecf20Sopenharmony_ci	attr.expected_attach_type = 0;
198c2ecf20Sopenharmony_ci	attr.prog_flags = BPF_F_TEST_RND_HI32;
208c2ecf20Sopenharmony_ci	attr.log_level = extra_prog_load_log_flags;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	return bpf_prog_load_xattr(&attr, pobj, prog_fd);
238c2ecf20Sopenharmony_ci}
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciint bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
268c2ecf20Sopenharmony_ci			  size_t insns_cnt, const char *license,
278c2ecf20Sopenharmony_ci			  __u32 kern_version, char *log_buf,
288c2ecf20Sopenharmony_ci		     size_t log_buf_sz)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	struct bpf_load_program_attr load_attr;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
338c2ecf20Sopenharmony_ci	load_attr.prog_type = type;
348c2ecf20Sopenharmony_ci	load_attr.expected_attach_type = 0;
358c2ecf20Sopenharmony_ci	load_attr.name = NULL;
368c2ecf20Sopenharmony_ci	load_attr.insns = insns;
378c2ecf20Sopenharmony_ci	load_attr.insns_cnt = insns_cnt;
388c2ecf20Sopenharmony_ci	load_attr.license = license;
398c2ecf20Sopenharmony_ci	load_attr.kern_version = kern_version;
408c2ecf20Sopenharmony_ci	load_attr.prog_flags = BPF_F_TEST_RND_HI32;
418c2ecf20Sopenharmony_ci	load_attr.log_level = extra_prog_load_log_flags;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz);
448c2ecf20Sopenharmony_ci}
45