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