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>
11 # if defined(__mips__) && defined(_ABIO32)
12 # define __NR_bpf 4355
13 # elif defined(__mips__) && defined(_ABIN32)
14 # define __NR_bpf 6319
15 # elif defined(__mips__) && defined(_ABI64)
16 # define __NR_bpf 5315
20 /* This file is a base header for auto-generated *.lskel.h files.
21 * Its contents will change and may become part of auto-generation in the future.
23 * The layout of bpf_[map|prog]_desc and bpf_loader_ctx is feature dependent
24 * and will change from one version of libbpf to another and features
25 * requested during loader program generation.
29 /* input for the loader prog */
31 __aligned_u64 initial_value;
34 /* output of the loader prog */
40 struct bpf_prog_desc {
44 struct bpf_loader_ctx {
51 struct bpf_load_and_run_opts {
52 struct bpf_loader_ctx *ctx;
60 static inline int skel_sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
63 return syscall(__NR_bpf, cmd, attr, size);
66 static inline int skel_closenz(int fd)
73 static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
75 int map_fd = -1, prog_fd = -1, key = 0, err;
78 map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "__loader.map", 4, opts->data_sz, 1, NULL);
80 opts->errstr = "failed to create loader map";
85 err = bpf_map_update_elem(map_fd, &key, opts->data, 0);
87 opts->errstr = "failed to update loader map";
92 memset(&attr, 0, sizeof(attr));
93 attr.prog_type = BPF_PROG_TYPE_SYSCALL;
94 attr.insns = (long) opts->insns;
95 attr.insn_cnt = opts->insns_sz / sizeof(struct bpf_insn);
96 attr.license = (long) "Dual BSD/GPL";
97 memcpy(attr.prog_name, "__loader.prog", sizeof("__loader.prog"));
98 attr.fd_array = (long) &map_fd;
99 attr.log_level = opts->ctx->log_level;
100 attr.log_size = opts->ctx->log_size;
101 attr.log_buf = opts->ctx->log_buf;
102 attr.prog_flags = BPF_F_SLEEPABLE;
103 prog_fd = skel_sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
105 opts->errstr = "failed to load loader prog";
110 memset(&attr, 0, sizeof(attr));
111 attr.test.prog_fd = prog_fd;
112 attr.test.ctx_in = (long) opts->ctx;
113 attr.test.ctx_size_in = opts->ctx->sz;
114 err = skel_sys_bpf(BPF_PROG_RUN, &attr, sizeof(attr));
115 if (err < 0 || (int)attr.test.retval < 0) {
116 opts->errstr = "failed to execute loader prog";
120 err = (int)attr.test.retval;