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
5
6 #include <unistd.h>
7 #include <sys/syscall.h>
8 #include <sys/mman.h>
9
10 #ifndef __NR_bpf
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
17 # endif
18 #endif
19
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.
22 *
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.
26 */
27 struct bpf_map_desc {
28 union {
29 /* input for the loader prog */
30 struct {
31 __aligned_u64 initial_value;
32 __u32 max_entries;
33 };
34 /* output of the loader prog */
35 struct {
36 int map_fd;
37 };
38 };
39 };
40 struct bpf_prog_desc {
41 int prog_fd;
42 };
43
44 struct bpf_loader_ctx {
45 size_t sz;
46 __u32 log_level;
47 __u32 log_size;
48 __u64 log_buf;
49 };
50
51 struct bpf_load_and_run_opts {
52 struct bpf_loader_ctx *ctx;
53 const void *data;
54 const void *insns;
55 __u32 data_sz;
56 __u32 insns_sz;
57 const char *errstr;
58 };
59
skel_sys_bpf(enum bpf_cmd cmd,union bpf_attr * attr,unsigned int size)60 static inline int skel_sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
61 unsigned int size)
62 {
63 return syscall(__NR_bpf, cmd, attr, size);
64 }
65
skel_closenz(int fd)66 static inline int skel_closenz(int fd)
67 {
68 if (fd > 0)
69 return close(fd);
70 return -EINVAL;
71 }
72
73 #ifndef offsetofend
74 #define offsetofend(TYPE, MEMBER) \
75 (offsetof(TYPE, MEMBER) + sizeof((((TYPE *)0)->MEMBER)))
76 #endif
77
skel_map_create(enum bpf_map_type map_type,const char * map_name,__u32 key_size,__u32 value_size,__u32 max_entries)78 static inline int skel_map_create(enum bpf_map_type map_type,
79 const char *map_name,
80 __u32 key_size,
81 __u32 value_size,
82 __u32 max_entries)
83 {
84 const size_t attr_sz = offsetofend(union bpf_attr, map_extra);
85 union bpf_attr attr;
86
87 memset(&attr, 0, attr_sz);
88
89 attr.map_type = map_type;
90 strncpy(attr.map_name, map_name, sizeof(attr.map_name));
91 attr.key_size = key_size;
92 attr.value_size = value_size;
93 attr.max_entries = max_entries;
94
95 return skel_sys_bpf(BPF_MAP_CREATE, &attr, attr_sz);
96 }
97
skel_map_update_elem(int fd,const void * key,const void * value,__u64 flags)98 static inline int skel_map_update_elem(int fd, const void *key,
99 const void *value, __u64 flags)
100 {
101 const size_t attr_sz = offsetofend(union bpf_attr, flags);
102 union bpf_attr attr;
103
104 memset(&attr, 0, attr_sz);
105 attr.map_fd = fd;
106 attr.key = (long) key;
107 attr.value = (long) value;
108 attr.flags = flags;
109
110 return skel_sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, attr_sz);
111 }
112
skel_raw_tracepoint_open(const char * name,int prog_fd)113 static inline int skel_raw_tracepoint_open(const char *name, int prog_fd)
114 {
115 const size_t attr_sz = offsetofend(union bpf_attr, raw_tracepoint.prog_fd);
116 union bpf_attr attr;
117
118 memset(&attr, 0, attr_sz);
119 attr.raw_tracepoint.name = (long) name;
120 attr.raw_tracepoint.prog_fd = prog_fd;
121
122 return skel_sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, attr_sz);
123 }
124
skel_link_create(int prog_fd,int target_fd,enum bpf_attach_type attach_type)125 static inline int skel_link_create(int prog_fd, int target_fd,
126 enum bpf_attach_type attach_type)
127 {
128 const size_t attr_sz = offsetofend(union bpf_attr, link_create.iter_info_len);
129 union bpf_attr attr;
130
131 memset(&attr, 0, attr_sz);
132 attr.link_create.prog_fd = prog_fd;
133 attr.link_create.target_fd = target_fd;
134 attr.link_create.attach_type = attach_type;
135
136 return skel_sys_bpf(BPF_LINK_CREATE, &attr, attr_sz);
137 }
138
bpf_load_and_run(struct bpf_load_and_run_opts * opts)139 static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
140 {
141 int map_fd = -1, prog_fd = -1, key = 0, err;
142 union bpf_attr attr;
143
144 map_fd = skel_map_create(BPF_MAP_TYPE_ARRAY, "__loader.map", 4, opts->data_sz, 1);
145 if (map_fd < 0) {
146 opts->errstr = "failed to create loader map";
147 err = -errno;
148 goto out;
149 }
150
151 err = skel_map_update_elem(map_fd, &key, opts->data, 0);
152 if (err < 0) {
153 opts->errstr = "failed to update loader map";
154 err = -errno;
155 goto out;
156 }
157
158 memset(&attr, 0, sizeof(attr));
159 attr.prog_type = BPF_PROG_TYPE_SYSCALL;
160 attr.insns = (long) opts->insns;
161 attr.insn_cnt = opts->insns_sz / sizeof(struct bpf_insn);
162 attr.license = (long) "Dual BSD/GPL";
163 memcpy(attr.prog_name, "__loader.prog", sizeof("__loader.prog"));
164 attr.fd_array = (long) &map_fd;
165 attr.log_level = opts->ctx->log_level;
166 attr.log_size = opts->ctx->log_size;
167 attr.log_buf = opts->ctx->log_buf;
168 attr.prog_flags = BPF_F_SLEEPABLE;
169 prog_fd = skel_sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
170 if (prog_fd < 0) {
171 opts->errstr = "failed to load loader prog";
172 err = -errno;
173 goto out;
174 }
175
176 memset(&attr, 0, sizeof(attr));
177 attr.test.prog_fd = prog_fd;
178 attr.test.ctx_in = (long) opts->ctx;
179 attr.test.ctx_size_in = opts->ctx->sz;
180 err = skel_sys_bpf(BPF_PROG_RUN, &attr, sizeof(attr));
181 if (err < 0 || (int)attr.test.retval < 0) {
182 opts->errstr = "failed to execute loader prog";
183 if (err < 0) {
184 err = -errno;
185 } else {
186 err = (int)attr.test.retval;
187 errno = -err;
188 }
189 goto out;
190 }
191 err = 0;
192 out:
193 if (map_fd >= 0)
194 close(map_fd);
195 if (prog_fd >= 0)
196 close(prog_fd);
197 return err;
198 }
199
200 #endif
201