xref: /openbmc/linux/tools/lib/bpf/bpf.c (revision 239480ab)
1 /*
2  * common eBPF ELF operations.
3  *
4  * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
5  * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
6  * Copyright (C) 2015 Huawei Inc.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation;
11  * version 2.1 of the License (not later!)
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this program; if not,  see <http://www.gnu.org/licenses>
20  */
21 
22 #include <stdlib.h>
23 #include <memory.h>
24 #include <unistd.h>
25 #include <asm/unistd.h>
26 #include <linux/bpf.h>
27 #include "bpf.h"
28 
29 /*
30  * When building perf, unistd.h is overridden. __NR_bpf is
31  * required to be defined explicitly.
32  */
33 #ifndef __NR_bpf
34 # if defined(__i386__)
35 #  define __NR_bpf 357
36 # elif defined(__x86_64__)
37 #  define __NR_bpf 321
38 # elif defined(__aarch64__)
39 #  define __NR_bpf 280
40 # elif defined(__sparc__)
41 #  define __NR_bpf 349
42 # else
43 #  error __NR_bpf not defined. libbpf does not support your arch.
44 # endif
45 #endif
46 
47 static inline __u64 ptr_to_u64(const void *ptr)
48 {
49 	return (__u64) (unsigned long) ptr;
50 }
51 
52 static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
53 			  unsigned int size)
54 {
55 	return syscall(__NR_bpf, cmd, attr, size);
56 }
57 
58 int bpf_create_map(enum bpf_map_type map_type, int key_size,
59 		   int value_size, int max_entries, __u32 map_flags)
60 {
61 	union bpf_attr attr;
62 
63 	memset(&attr, '\0', sizeof(attr));
64 
65 	attr.map_type = map_type;
66 	attr.key_size = key_size;
67 	attr.value_size = value_size;
68 	attr.max_entries = max_entries;
69 	attr.map_flags = map_flags;
70 
71 	return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
72 }
73 
74 int bpf_create_map_in_map(enum bpf_map_type map_type, int key_size,
75 			  int inner_map_fd, int max_entries, __u32 map_flags)
76 {
77 	union bpf_attr attr;
78 
79 	memset(&attr, '\0', sizeof(attr));
80 
81 	attr.map_type = map_type;
82 	attr.key_size = key_size;
83 	attr.value_size = 4;
84 	attr.inner_map_fd = inner_map_fd;
85 	attr.max_entries = max_entries;
86 	attr.map_flags = map_flags;
87 
88 	return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
89 }
90 
91 int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
92 		     size_t insns_cnt, const char *license,
93 		     __u32 kern_version, char *log_buf, size_t log_buf_sz)
94 {
95 	int fd;
96 	union bpf_attr attr;
97 
98 	bzero(&attr, sizeof(attr));
99 	attr.prog_type = type;
100 	attr.insn_cnt = (__u32)insns_cnt;
101 	attr.insns = ptr_to_u64(insns);
102 	attr.license = ptr_to_u64(license);
103 	attr.log_buf = ptr_to_u64(NULL);
104 	attr.log_size = 0;
105 	attr.log_level = 0;
106 	attr.kern_version = kern_version;
107 
108 	fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
109 	if (fd >= 0 || !log_buf || !log_buf_sz)
110 		return fd;
111 
112 	/* Try again with log */
113 	attr.log_buf = ptr_to_u64(log_buf);
114 	attr.log_size = log_buf_sz;
115 	attr.log_level = 1;
116 	log_buf[0] = 0;
117 	return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
118 }
119 
120 int bpf_map_update_elem(int fd, const void *key, const void *value,
121 			__u64 flags)
122 {
123 	union bpf_attr attr;
124 
125 	bzero(&attr, sizeof(attr));
126 	attr.map_fd = fd;
127 	attr.key = ptr_to_u64(key);
128 	attr.value = ptr_to_u64(value);
129 	attr.flags = flags;
130 
131 	return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
132 }
133 
134 int bpf_map_lookup_elem(int fd, const void *key, void *value)
135 {
136 	union bpf_attr attr;
137 
138 	bzero(&attr, sizeof(attr));
139 	attr.map_fd = fd;
140 	attr.key = ptr_to_u64(key);
141 	attr.value = ptr_to_u64(value);
142 
143 	return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
144 }
145 
146 int bpf_map_delete_elem(int fd, const void *key)
147 {
148 	union bpf_attr attr;
149 
150 	bzero(&attr, sizeof(attr));
151 	attr.map_fd = fd;
152 	attr.key = ptr_to_u64(key);
153 
154 	return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
155 }
156 
157 int bpf_map_get_next_key(int fd, const void *key, void *next_key)
158 {
159 	union bpf_attr attr;
160 
161 	bzero(&attr, sizeof(attr));
162 	attr.map_fd = fd;
163 	attr.key = ptr_to_u64(key);
164 	attr.next_key = ptr_to_u64(next_key);
165 
166 	return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
167 }
168 
169 int bpf_obj_pin(int fd, const char *pathname)
170 {
171 	union bpf_attr attr;
172 
173 	bzero(&attr, sizeof(attr));
174 	attr.pathname = ptr_to_u64((void *)pathname);
175 	attr.bpf_fd = fd;
176 
177 	return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
178 }
179 
180 int bpf_obj_get(const char *pathname)
181 {
182 	union bpf_attr attr;
183 
184 	bzero(&attr, sizeof(attr));
185 	attr.pathname = ptr_to_u64((void *)pathname);
186 
187 	return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
188 }
189 
190 int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
191 		    unsigned int flags)
192 {
193 	union bpf_attr attr;
194 
195 	bzero(&attr, sizeof(attr));
196 	attr.target_fd	   = target_fd;
197 	attr.attach_bpf_fd = prog_fd;
198 	attr.attach_type   = type;
199 	attr.attach_flags  = flags;
200 
201 	return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
202 }
203 
204 int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
205 {
206 	union bpf_attr attr;
207 
208 	bzero(&attr, sizeof(attr));
209 	attr.target_fd	 = target_fd;
210 	attr.attach_type = type;
211 
212 	return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
213 }
214 
215 int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
216 		      void *data_out, __u32 *size_out, __u32 *retval,
217 		      __u32 *duration)
218 {
219 	union bpf_attr attr;
220 	int ret;
221 
222 	bzero(&attr, sizeof(attr));
223 	attr.test.prog_fd = prog_fd;
224 	attr.test.data_in = ptr_to_u64(data);
225 	attr.test.data_out = ptr_to_u64(data_out);
226 	attr.test.data_size_in = size;
227 	attr.test.repeat = repeat;
228 
229 	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
230 	if (size_out)
231 		*size_out = attr.test.data_size_out;
232 	if (retval)
233 		*retval = attr.test.retval;
234 	if (duration)
235 		*duration = attr.test.duration;
236 	return ret;
237 }
238