Wang Nan | e3ed2fe | 2015-07-01 02:14:03 +0000 | [diff] [blame] | 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. |
Wang Nan | 203d1ca | 2016-07-04 11:02:42 +0000 | [diff] [blame] | 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> |
Wang Nan | e3ed2fe | 2015-07-01 02:14:03 +0000 | [diff] [blame] | 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 | /* |
Masahiro Yamada | 0367105 | 2017-02-27 14:29:28 -0800 | [diff] [blame] | 30 | * When building perf, unistd.h is overridden. __NR_bpf is |
Wang Nan | 8f9e05f | 2016-01-11 13:47:57 +0000 | [diff] [blame] | 31 | * required to be defined explicitly. |
Wang Nan | e3ed2fe | 2015-07-01 02:14:03 +0000 | [diff] [blame] | 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 |
David S. Miller | b0c4780 | 2017-04-22 12:31:05 -0700 | [diff] [blame] | 40 | # elif defined(__sparc__) |
| 41 | # define __NR_bpf 349 |
Wang Nan | e3ed2fe | 2015-07-01 02:14:03 +0000 | [diff] [blame] | 42 | # else |
| 43 | # error __NR_bpf not defined. libbpf does not support your arch. |
| 44 | # endif |
| 45 | #endif |
| 46 | |
Mickaël Salaün | cdc6a4b | 2017-02-11 20:37:08 +0100 | [diff] [blame] | 47 | static inline __u64 ptr_to_u64(const void *ptr) |
Wang Nan | 7bf9836 | 2015-07-01 02:14:06 +0000 | [diff] [blame] | 48 | { |
| 49 | return (__u64) (unsigned long) ptr; |
| 50 | } |
| 51 | |
Mickaël Salaün | cdc6a4b | 2017-02-11 20:37:08 +0100 | [diff] [blame] | 52 | static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, |
| 53 | unsigned int size) |
Wang Nan | e3ed2fe | 2015-07-01 02:14:03 +0000 | [diff] [blame] | 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, |
Joe Stringer | a5580c7 | 2016-12-08 18:46:16 -0800 | [diff] [blame] | 59 | int value_size, int max_entries, __u32 map_flags) |
Wang Nan | e3ed2fe | 2015-07-01 02:14:03 +0000 | [diff] [blame] | 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; |
Joe Stringer | a5580c7 | 2016-12-08 18:46:16 -0800 | [diff] [blame] | 69 | attr.map_flags = map_flags; |
Wang Nan | e3ed2fe | 2015-07-01 02:14:03 +0000 | [diff] [blame] | 70 | |
| 71 | return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); |
| 72 | } |
Wang Nan | 7bf9836 | 2015-07-01 02:14:06 +0000 | [diff] [blame] | 73 | |
Martin KaFai Lau | fb30d4b | 2017-03-22 10:00:35 -0700 | [diff] [blame] | 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 | |
Mickaël Salaün | 2ee89fb | 2017-02-10 00:21:38 +0100 | [diff] [blame] | 91 | int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns, |
| 92 | size_t insns_cnt, const char *license, |
Joe Stringer | 83d994d | 2016-12-08 18:46:15 -0800 | [diff] [blame] | 93 | __u32 kern_version, char *log_buf, size_t log_buf_sz) |
Wang Nan | 7bf9836 | 2015-07-01 02:14:06 +0000 | [diff] [blame] | 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 | } |
He Kuang | 43798bf | 2015-11-24 13:36:08 +0000 | [diff] [blame] | 119 | |
David S. Miller | 91045f5 | 2017-05-10 11:42:48 -0700 | [diff] [blame] | 120 | int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns, |
| 121 | size_t insns_cnt, int strict_alignment, |
| 122 | const char *license, __u32 kern_version, |
Daniel Borkmann | d655490 | 2017-07-21 00:00:22 +0200 | [diff] [blame] | 123 | char *log_buf, size_t log_buf_sz, int log_level) |
David S. Miller | 91045f5 | 2017-05-10 11:42:48 -0700 | [diff] [blame] | 124 | { |
| 125 | union bpf_attr attr; |
| 126 | |
| 127 | bzero(&attr, sizeof(attr)); |
| 128 | attr.prog_type = type; |
| 129 | attr.insn_cnt = (__u32)insns_cnt; |
| 130 | attr.insns = ptr_to_u64(insns); |
| 131 | attr.license = ptr_to_u64(license); |
| 132 | attr.log_buf = ptr_to_u64(log_buf); |
| 133 | attr.log_size = log_buf_sz; |
Daniel Borkmann | d655490 | 2017-07-21 00:00:22 +0200 | [diff] [blame] | 134 | attr.log_level = log_level; |
David S. Miller | 91045f5 | 2017-05-10 11:42:48 -0700 | [diff] [blame] | 135 | log_buf[0] = 0; |
| 136 | attr.kern_version = kern_version; |
| 137 | attr.prog_flags = strict_alignment ? BPF_F_STRICT_ALIGNMENT : 0; |
| 138 | |
| 139 | return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr)); |
| 140 | } |
| 141 | |
Mickaël Salaün | 10ecc72 | 2017-02-10 00:21:39 +0100 | [diff] [blame] | 142 | int bpf_map_update_elem(int fd, const void *key, const void *value, |
Joe Stringer | 83d994d | 2016-12-08 18:46:15 -0800 | [diff] [blame] | 143 | __u64 flags) |
He Kuang | 43798bf | 2015-11-24 13:36:08 +0000 | [diff] [blame] | 144 | { |
| 145 | union bpf_attr attr; |
| 146 | |
| 147 | bzero(&attr, sizeof(attr)); |
| 148 | attr.map_fd = fd; |
| 149 | attr.key = ptr_to_u64(key); |
| 150 | attr.value = ptr_to_u64(value); |
| 151 | attr.flags = flags; |
| 152 | |
| 153 | return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); |
| 154 | } |
Wang Nan | 9742da0 | 2016-11-26 07:03:25 +0000 | [diff] [blame] | 155 | |
Mickaël Salaün | e5ff7c4 | 2017-02-10 00:21:40 +0100 | [diff] [blame] | 156 | int bpf_map_lookup_elem(int fd, const void *key, void *value) |
Wang Nan | 9742da0 | 2016-11-26 07:03:25 +0000 | [diff] [blame] | 157 | { |
| 158 | union bpf_attr attr; |
| 159 | |
| 160 | bzero(&attr, sizeof(attr)); |
| 161 | attr.map_fd = fd; |
| 162 | attr.key = ptr_to_u64(key); |
| 163 | attr.value = ptr_to_u64(value); |
| 164 | |
| 165 | return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); |
| 166 | } |
| 167 | |
Mickaël Salaün | e58383b | 2017-02-10 00:21:41 +0100 | [diff] [blame] | 168 | int bpf_map_delete_elem(int fd, const void *key) |
Wang Nan | 9742da0 | 2016-11-26 07:03:25 +0000 | [diff] [blame] | 169 | { |
| 170 | union bpf_attr attr; |
| 171 | |
| 172 | bzero(&attr, sizeof(attr)); |
| 173 | attr.map_fd = fd; |
| 174 | attr.key = ptr_to_u64(key); |
| 175 | |
| 176 | return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr)); |
| 177 | } |
| 178 | |
Mickaël Salaün | 5f155c2 | 2017-02-10 00:21:42 +0100 | [diff] [blame] | 179 | int bpf_map_get_next_key(int fd, const void *key, void *next_key) |
Wang Nan | 9742da0 | 2016-11-26 07:03:25 +0000 | [diff] [blame] | 180 | { |
| 181 | union bpf_attr attr; |
| 182 | |
| 183 | bzero(&attr, sizeof(attr)); |
| 184 | attr.map_fd = fd; |
| 185 | attr.key = ptr_to_u64(key); |
| 186 | attr.next_key = ptr_to_u64(next_key); |
| 187 | |
| 188 | return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); |
| 189 | } |
| 190 | |
| 191 | int bpf_obj_pin(int fd, const char *pathname) |
| 192 | { |
| 193 | union bpf_attr attr; |
| 194 | |
| 195 | bzero(&attr, sizeof(attr)); |
| 196 | attr.pathname = ptr_to_u64((void *)pathname); |
| 197 | attr.bpf_fd = fd; |
| 198 | |
| 199 | return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr)); |
| 200 | } |
| 201 | |
| 202 | int bpf_obj_get(const char *pathname) |
| 203 | { |
| 204 | union bpf_attr attr; |
| 205 | |
| 206 | bzero(&attr, sizeof(attr)); |
| 207 | attr.pathname = ptr_to_u64((void *)pathname); |
| 208 | |
| 209 | return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr)); |
| 210 | } |
Joe Stringer | 5dc880d | 2016-12-14 14:05:26 -0800 | [diff] [blame] | 211 | |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 212 | int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type, |
| 213 | unsigned int flags) |
Joe Stringer | 5dc880d | 2016-12-14 14:05:26 -0800 | [diff] [blame] | 214 | { |
| 215 | union bpf_attr attr; |
| 216 | |
| 217 | bzero(&attr, sizeof(attr)); |
| 218 | attr.target_fd = target_fd; |
| 219 | attr.attach_bpf_fd = prog_fd; |
| 220 | attr.attach_type = type; |
Alexei Starovoitov | 7f67763 | 2017-02-10 20:28:24 -0800 | [diff] [blame] | 221 | attr.attach_flags = flags; |
Joe Stringer | 5dc880d | 2016-12-14 14:05:26 -0800 | [diff] [blame] | 222 | |
| 223 | return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr)); |
| 224 | } |
| 225 | |
| 226 | int bpf_prog_detach(int target_fd, enum bpf_attach_type type) |
| 227 | { |
| 228 | union bpf_attr attr; |
| 229 | |
| 230 | bzero(&attr, sizeof(attr)); |
| 231 | attr.target_fd = target_fd; |
| 232 | attr.attach_type = type; |
| 233 | |
| 234 | return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr)); |
| 235 | } |
Alexei Starovoitov | 3084887 | 2017-03-30 21:45:39 -0700 | [diff] [blame] | 236 | |
| 237 | int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size, |
| 238 | void *data_out, __u32 *size_out, __u32 *retval, |
| 239 | __u32 *duration) |
| 240 | { |
| 241 | union bpf_attr attr; |
| 242 | int ret; |
| 243 | |
| 244 | bzero(&attr, sizeof(attr)); |
| 245 | attr.test.prog_fd = prog_fd; |
| 246 | attr.test.data_in = ptr_to_u64(data); |
| 247 | attr.test.data_out = ptr_to_u64(data_out); |
| 248 | attr.test.data_size_in = size; |
| 249 | attr.test.repeat = repeat; |
| 250 | |
| 251 | ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr)); |
| 252 | if (size_out) |
| 253 | *size_out = attr.test.data_size_out; |
| 254 | if (retval) |
| 255 | *retval = attr.test.retval; |
| 256 | if (duration) |
| 257 | *duration = attr.test.duration; |
| 258 | return ret; |
| 259 | } |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 260 | |
| 261 | int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id) |
| 262 | { |
| 263 | union bpf_attr attr; |
| 264 | int err; |
| 265 | |
| 266 | bzero(&attr, sizeof(attr)); |
| 267 | attr.start_id = start_id; |
| 268 | |
| 269 | err = sys_bpf(BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr)); |
| 270 | if (!err) |
| 271 | *next_id = attr.next_id; |
| 272 | |
| 273 | return err; |
| 274 | } |
| 275 | |
| 276 | int bpf_map_get_next_id(__u32 start_id, __u32 *next_id) |
| 277 | { |
| 278 | union bpf_attr attr; |
| 279 | int err; |
| 280 | |
| 281 | bzero(&attr, sizeof(attr)); |
| 282 | attr.start_id = start_id; |
| 283 | |
| 284 | err = sys_bpf(BPF_MAP_GET_NEXT_ID, &attr, sizeof(attr)); |
| 285 | if (!err) |
| 286 | *next_id = attr.next_id; |
| 287 | |
| 288 | return err; |
| 289 | } |
| 290 | |
| 291 | int bpf_prog_get_fd_by_id(__u32 id) |
| 292 | { |
| 293 | union bpf_attr attr; |
| 294 | |
| 295 | bzero(&attr, sizeof(attr)); |
| 296 | attr.prog_id = id; |
| 297 | |
| 298 | return sys_bpf(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr)); |
| 299 | } |
| 300 | |
| 301 | int bpf_map_get_fd_by_id(__u32 id) |
| 302 | { |
| 303 | union bpf_attr attr; |
| 304 | |
| 305 | bzero(&attr, sizeof(attr)); |
| 306 | attr.map_id = id; |
| 307 | |
| 308 | return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr)); |
| 309 | } |
| 310 | |
| 311 | int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len) |
| 312 | { |
| 313 | union bpf_attr attr; |
| 314 | int err; |
| 315 | |
| 316 | bzero(&attr, sizeof(attr)); |
Martin KaFai Lau | 95b9afd | 2017-06-05 12:15:53 -0700 | [diff] [blame] | 317 | attr.info.bpf_fd = prog_fd; |
| 318 | attr.info.info_len = *info_len; |
| 319 | attr.info.info = ptr_to_u64(info); |
| 320 | |
| 321 | err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr)); |
| 322 | if (!err) |
| 323 | *info_len = attr.info.info_len; |
| 324 | |
| 325 | return err; |
| 326 | } |