1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 /* Copyright (c) 2021 Facebook */
3 #ifndef __SKEL_INTERNAL_H
4 #define __SKEL_INTERNAL_H
7 #include <sys/syscall.h>
10 /* This file is a base header for auto-generated *.lskel.h files.
11 * Its contents will change and may become part of auto-generation in the future.
13 * The layout of bpf_[map|prog]_desc and bpf_loader_ctx is feature dependent
14 * and will change from one version of libbpf to another and features
15 * requested during loader program generation.
19 /* input for the loader prog */
21 __aligned_u64 initial_value;
24 /* output of the loader prog */
30 struct bpf_prog_desc {
34 struct bpf_loader_ctx {
41 struct bpf_load_and_run_opts {
42 struct bpf_loader_ctx *ctx;
50 static inline int skel_sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
53 return syscall(__NR_bpf, cmd, attr, size);
56 static inline int skel_closenz(int fd)
63 static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
65 int map_fd = -1, prog_fd = -1, key = 0, err;
68 map_fd = bpf_create_map_name(BPF_MAP_TYPE_ARRAY, "__loader.map", 4,
71 opts->errstr = "failed to create loader map";
76 err = bpf_map_update_elem(map_fd, &key, opts->data, 0);
78 opts->errstr = "failed to update loader map";
83 memset(&attr, 0, sizeof(attr));
84 attr.prog_type = BPF_PROG_TYPE_SYSCALL;
85 attr.insns = (long) opts->insns;
86 attr.insn_cnt = opts->insns_sz / sizeof(struct bpf_insn);
87 attr.license = (long) "Dual BSD/GPL";
88 memcpy(attr.prog_name, "__loader.prog", sizeof("__loader.prog"));
89 attr.fd_array = (long) &map_fd;
90 attr.log_level = opts->ctx->log_level;
91 attr.log_size = opts->ctx->log_size;
92 attr.log_buf = opts->ctx->log_buf;
93 attr.prog_flags = BPF_F_SLEEPABLE;
94 prog_fd = skel_sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
96 opts->errstr = "failed to load loader prog";
101 memset(&attr, 0, sizeof(attr));
102 attr.test.prog_fd = prog_fd;
103 attr.test.ctx_in = (long) opts->ctx;
104 attr.test.ctx_size_in = opts->ctx->sz;
105 err = skel_sys_bpf(BPF_PROG_RUN, &attr, sizeof(attr));
106 if (err < 0 || (int)attr.test.retval < 0) {
107 opts->errstr = "failed to execute loader prog";
111 err = (int)attr.test.retval;