xref: /openbmc/linux/tools/lib/bpf/bpf.c (revision 4f139972b489f8bc2c821aa25ac65018d92af3f7)
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 # else
41 #  error __NR_bpf not defined. libbpf does not support your arch.
42 # endif
43 #endif
44 
45 static inline __u64 ptr_to_u64(const void *ptr)
46 {
47 	return (__u64) (unsigned long) ptr;
48 }
49 
50 static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
51 			  unsigned int size)
52 {
53 	return syscall(__NR_bpf, cmd, attr, size);
54 }
55 
56 int bpf_create_map(enum bpf_map_type map_type, int key_size,
57 		   int value_size, int max_entries, __u32 map_flags)
58 {
59 	union bpf_attr attr;
60 
61 	memset(&attr, '\0', sizeof(attr));
62 
63 	attr.map_type = map_type;
64 	attr.key_size = key_size;
65 	attr.value_size = value_size;
66 	attr.max_entries = max_entries;
67 	attr.map_flags = map_flags;
68 
69 	return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
70 }
71 
72 int bpf_create_map_in_map(enum bpf_map_type map_type, int key_size,
73 			  int inner_map_fd, int max_entries, __u32 map_flags)
74 {
75 	union bpf_attr attr;
76 
77 	memset(&attr, '\0', sizeof(attr));
78 
79 	attr.map_type = map_type;
80 	attr.key_size = key_size;
81 	attr.value_size = 4;
82 	attr.inner_map_fd = inner_map_fd;
83 	attr.max_entries = max_entries;
84 	attr.map_flags = map_flags;
85 
86 	return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
87 }
88 
89 int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
90 		     size_t insns_cnt, const char *license,
91 		     __u32 kern_version, char *log_buf, size_t log_buf_sz)
92 {
93 	int fd;
94 	union bpf_attr attr;
95 
96 	bzero(&attr, sizeof(attr));
97 	attr.prog_type = type;
98 	attr.insn_cnt = (__u32)insns_cnt;
99 	attr.insns = ptr_to_u64(insns);
100 	attr.license = ptr_to_u64(license);
101 	attr.log_buf = ptr_to_u64(NULL);
102 	attr.log_size = 0;
103 	attr.log_level = 0;
104 	attr.kern_version = kern_version;
105 
106 	fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
107 	if (fd >= 0 || !log_buf || !log_buf_sz)
108 		return fd;
109 
110 	/* Try again with log */
111 	attr.log_buf = ptr_to_u64(log_buf);
112 	attr.log_size = log_buf_sz;
113 	attr.log_level = 1;
114 	log_buf[0] = 0;
115 	return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
116 }
117 
118 int bpf_map_update_elem(int fd, const void *key, const void *value,
119 			__u64 flags)
120 {
121 	union bpf_attr attr;
122 
123 	bzero(&attr, sizeof(attr));
124 	attr.map_fd = fd;
125 	attr.key = ptr_to_u64(key);
126 	attr.value = ptr_to_u64(value);
127 	attr.flags = flags;
128 
129 	return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
130 }
131 
132 int bpf_map_lookup_elem(int fd, const void *key, void *value)
133 {
134 	union bpf_attr attr;
135 
136 	bzero(&attr, sizeof(attr));
137 	attr.map_fd = fd;
138 	attr.key = ptr_to_u64(key);
139 	attr.value = ptr_to_u64(value);
140 
141 	return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
142 }
143 
144 int bpf_map_delete_elem(int fd, const void *key)
145 {
146 	union bpf_attr attr;
147 
148 	bzero(&attr, sizeof(attr));
149 	attr.map_fd = fd;
150 	attr.key = ptr_to_u64(key);
151 
152 	return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
153 }
154 
155 int bpf_map_get_next_key(int fd, const void *key, void *next_key)
156 {
157 	union bpf_attr attr;
158 
159 	bzero(&attr, sizeof(attr));
160 	attr.map_fd = fd;
161 	attr.key = ptr_to_u64(key);
162 	attr.next_key = ptr_to_u64(next_key);
163 
164 	return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
165 }
166 
167 int bpf_obj_pin(int fd, const char *pathname)
168 {
169 	union bpf_attr attr;
170 
171 	bzero(&attr, sizeof(attr));
172 	attr.pathname = ptr_to_u64((void *)pathname);
173 	attr.bpf_fd = fd;
174 
175 	return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
176 }
177 
178 int bpf_obj_get(const char *pathname)
179 {
180 	union bpf_attr attr;
181 
182 	bzero(&attr, sizeof(attr));
183 	attr.pathname = ptr_to_u64((void *)pathname);
184 
185 	return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
186 }
187 
188 int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
189 		    unsigned int flags)
190 {
191 	union bpf_attr attr;
192 
193 	bzero(&attr, sizeof(attr));
194 	attr.target_fd	   = target_fd;
195 	attr.attach_bpf_fd = prog_fd;
196 	attr.attach_type   = type;
197 	attr.attach_flags  = flags;
198 
199 	return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
200 }
201 
202 int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
203 {
204 	union bpf_attr attr;
205 
206 	bzero(&attr, sizeof(attr));
207 	attr.target_fd	 = target_fd;
208 	attr.attach_type = type;
209 
210 	return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
211 }
212 
213 int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
214 		      void *data_out, __u32 *size_out, __u32 *retval,
215 		      __u32 *duration)
216 {
217 	union bpf_attr attr;
218 	int ret;
219 
220 	bzero(&attr, sizeof(attr));
221 	attr.test.prog_fd = prog_fd;
222 	attr.test.data_in = ptr_to_u64(data);
223 	attr.test.data_out = ptr_to_u64(data_out);
224 	attr.test.data_size_in = size;
225 	attr.test.repeat = repeat;
226 
227 	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
228 	if (size_out)
229 		*size_out = attr.test.data_size_out;
230 	if (retval)
231 		*retval = attr.test.retval;
232 	if (duration)
233 		*duration = attr.test.duration;
234 	return ret;
235 }
236