bpf.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. /*
  3. * common eBPF ELF operations.
  4. *
  5. * Copyright (C) 2013-2015 Alexei Starovoitov <[email protected]>
  6. * Copyright (C) 2015 Wang Nan <[email protected]>
  7. * Copyright (C) 2015 Huawei Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License (not later!)
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this program; if not, see <http://www.gnu.org/licenses>
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <memory.h>
  25. #include <unistd.h>
  26. #include <asm/unistd.h>
  27. #include <errno.h>
  28. #include <linux/bpf.h>
  29. #include <linux/filter.h>
  30. #include <linux/kernel.h>
  31. #include <limits.h>
  32. #include <sys/resource.h>
  33. #include "bpf.h"
  34. #include "libbpf.h"
  35. #include "libbpf_internal.h"
  36. /*
  37. * When building perf, unistd.h is overridden. __NR_bpf is
  38. * required to be defined explicitly.
  39. */
  40. #ifndef __NR_bpf
  41. # if defined(__i386__)
  42. # define __NR_bpf 357
  43. # elif defined(__x86_64__)
  44. # define __NR_bpf 321
  45. # elif defined(__aarch64__)
  46. # define __NR_bpf 280
  47. # elif defined(__sparc__)
  48. # define __NR_bpf 349
  49. # elif defined(__s390__)
  50. # define __NR_bpf 351
  51. # elif defined(__arc__)
  52. # define __NR_bpf 280
  53. # elif defined(__mips__) && defined(_ABIO32)
  54. # define __NR_bpf 4355
  55. # elif defined(__mips__) && defined(_ABIN32)
  56. # define __NR_bpf 6319
  57. # elif defined(__mips__) && defined(_ABI64)
  58. # define __NR_bpf 5315
  59. # else
  60. # error __NR_bpf not defined. libbpf does not support your arch.
  61. # endif
  62. #endif
  63. static inline __u64 ptr_to_u64(const void *ptr)
  64. {
  65. return (__u64) (unsigned long) ptr;
  66. }
  67. static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  68. unsigned int size)
  69. {
  70. return syscall(__NR_bpf, cmd, attr, size);
  71. }
  72. static inline int sys_bpf_fd(enum bpf_cmd cmd, union bpf_attr *attr,
  73. unsigned int size)
  74. {
  75. int fd;
  76. fd = sys_bpf(cmd, attr, size);
  77. return ensure_good_fd(fd);
  78. }
  79. int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)
  80. {
  81. int fd;
  82. do {
  83. fd = sys_bpf_fd(BPF_PROG_LOAD, attr, size);
  84. } while (fd < 0 && errno == EAGAIN && --attempts > 0);
  85. return fd;
  86. }
  87. /* Probe whether kernel switched from memlock-based (RLIMIT_MEMLOCK) to
  88. * memcg-based memory accounting for BPF maps and progs. This was done in [0].
  89. * We use the support for bpf_ktime_get_coarse_ns() helper, which was added in
  90. * the same 5.11 Linux release ([1]), to detect memcg-based accounting for BPF.
  91. *
  92. * [0] https://lore.kernel.org/bpf/[email protected]/
  93. * [1] d05512618056 ("bpf: Add bpf_ktime_get_coarse_ns helper")
  94. */
  95. int probe_memcg_account(void)
  96. {
  97. const size_t attr_sz = offsetofend(union bpf_attr, attach_btf_obj_fd);
  98. struct bpf_insn insns[] = {
  99. BPF_EMIT_CALL(BPF_FUNC_ktime_get_coarse_ns),
  100. BPF_EXIT_INSN(),
  101. };
  102. size_t insn_cnt = ARRAY_SIZE(insns);
  103. union bpf_attr attr;
  104. int prog_fd;
  105. /* attempt loading freplace trying to use custom BTF */
  106. memset(&attr, 0, attr_sz);
  107. attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  108. attr.insns = ptr_to_u64(insns);
  109. attr.insn_cnt = insn_cnt;
  110. attr.license = ptr_to_u64("GPL");
  111. prog_fd = sys_bpf_fd(BPF_PROG_LOAD, &attr, attr_sz);
  112. if (prog_fd >= 0) {
  113. close(prog_fd);
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. static bool memlock_bumped;
  119. static rlim_t memlock_rlim = RLIM_INFINITY;
  120. int libbpf_set_memlock_rlim(size_t memlock_bytes)
  121. {
  122. if (memlock_bumped)
  123. return libbpf_err(-EBUSY);
  124. memlock_rlim = memlock_bytes;
  125. return 0;
  126. }
  127. int bump_rlimit_memlock(void)
  128. {
  129. struct rlimit rlim;
  130. /* if kernel supports memcg-based accounting, skip bumping RLIMIT_MEMLOCK */
  131. if (memlock_bumped || kernel_supports(NULL, FEAT_MEMCG_ACCOUNT))
  132. return 0;
  133. memlock_bumped = true;
  134. /* zero memlock_rlim_max disables auto-bumping RLIMIT_MEMLOCK */
  135. if (memlock_rlim == 0)
  136. return 0;
  137. rlim.rlim_cur = rlim.rlim_max = memlock_rlim;
  138. if (setrlimit(RLIMIT_MEMLOCK, &rlim))
  139. return -errno;
  140. return 0;
  141. }
  142. int bpf_map_create(enum bpf_map_type map_type,
  143. const char *map_name,
  144. __u32 key_size,
  145. __u32 value_size,
  146. __u32 max_entries,
  147. const struct bpf_map_create_opts *opts)
  148. {
  149. const size_t attr_sz = offsetofend(union bpf_attr, map_extra);
  150. union bpf_attr attr;
  151. int fd;
  152. bump_rlimit_memlock();
  153. memset(&attr, 0, attr_sz);
  154. if (!OPTS_VALID(opts, bpf_map_create_opts))
  155. return libbpf_err(-EINVAL);
  156. attr.map_type = map_type;
  157. if (map_name && kernel_supports(NULL, FEAT_PROG_NAME))
  158. libbpf_strlcpy(attr.map_name, map_name, sizeof(attr.map_name));
  159. attr.key_size = key_size;
  160. attr.value_size = value_size;
  161. attr.max_entries = max_entries;
  162. attr.btf_fd = OPTS_GET(opts, btf_fd, 0);
  163. attr.btf_key_type_id = OPTS_GET(opts, btf_key_type_id, 0);
  164. attr.btf_value_type_id = OPTS_GET(opts, btf_value_type_id, 0);
  165. attr.btf_vmlinux_value_type_id = OPTS_GET(opts, btf_vmlinux_value_type_id, 0);
  166. attr.inner_map_fd = OPTS_GET(opts, inner_map_fd, 0);
  167. attr.map_flags = OPTS_GET(opts, map_flags, 0);
  168. attr.map_extra = OPTS_GET(opts, map_extra, 0);
  169. attr.numa_node = OPTS_GET(opts, numa_node, 0);
  170. attr.map_ifindex = OPTS_GET(opts, map_ifindex, 0);
  171. fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);
  172. return libbpf_err_errno(fd);
  173. }
  174. static void *
  175. alloc_zero_tailing_info(const void *orecord, __u32 cnt,
  176. __u32 actual_rec_size, __u32 expected_rec_size)
  177. {
  178. __u64 info_len = (__u64)actual_rec_size * cnt;
  179. void *info, *nrecord;
  180. int i;
  181. info = malloc(info_len);
  182. if (!info)
  183. return NULL;
  184. /* zero out bytes kernel does not understand */
  185. nrecord = info;
  186. for (i = 0; i < cnt; i++) {
  187. memcpy(nrecord, orecord, expected_rec_size);
  188. memset(nrecord + expected_rec_size, 0,
  189. actual_rec_size - expected_rec_size);
  190. orecord += actual_rec_size;
  191. nrecord += actual_rec_size;
  192. }
  193. return info;
  194. }
  195. int bpf_prog_load(enum bpf_prog_type prog_type,
  196. const char *prog_name, const char *license,
  197. const struct bpf_insn *insns, size_t insn_cnt,
  198. const struct bpf_prog_load_opts *opts)
  199. {
  200. const size_t attr_sz = offsetofend(union bpf_attr, fd_array);
  201. void *finfo = NULL, *linfo = NULL;
  202. const char *func_info, *line_info;
  203. __u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;
  204. __u32 func_info_rec_size, line_info_rec_size;
  205. int fd, attempts;
  206. union bpf_attr attr;
  207. char *log_buf;
  208. bump_rlimit_memlock();
  209. if (!OPTS_VALID(opts, bpf_prog_load_opts))
  210. return libbpf_err(-EINVAL);
  211. attempts = OPTS_GET(opts, attempts, 0);
  212. if (attempts < 0)
  213. return libbpf_err(-EINVAL);
  214. if (attempts == 0)
  215. attempts = PROG_LOAD_ATTEMPTS;
  216. memset(&attr, 0, attr_sz);
  217. attr.prog_type = prog_type;
  218. attr.expected_attach_type = OPTS_GET(opts, expected_attach_type, 0);
  219. attr.prog_btf_fd = OPTS_GET(opts, prog_btf_fd, 0);
  220. attr.prog_flags = OPTS_GET(opts, prog_flags, 0);
  221. attr.prog_ifindex = OPTS_GET(opts, prog_ifindex, 0);
  222. attr.kern_version = OPTS_GET(opts, kern_version, 0);
  223. if (prog_name && kernel_supports(NULL, FEAT_PROG_NAME))
  224. libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
  225. attr.license = ptr_to_u64(license);
  226. if (insn_cnt > UINT_MAX)
  227. return libbpf_err(-E2BIG);
  228. attr.insns = ptr_to_u64(insns);
  229. attr.insn_cnt = (__u32)insn_cnt;
  230. attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0);
  231. attach_btf_obj_fd = OPTS_GET(opts, attach_btf_obj_fd, 0);
  232. if (attach_prog_fd && attach_btf_obj_fd)
  233. return libbpf_err(-EINVAL);
  234. attr.attach_btf_id = OPTS_GET(opts, attach_btf_id, 0);
  235. if (attach_prog_fd)
  236. attr.attach_prog_fd = attach_prog_fd;
  237. else
  238. attr.attach_btf_obj_fd = attach_btf_obj_fd;
  239. log_buf = OPTS_GET(opts, log_buf, NULL);
  240. log_size = OPTS_GET(opts, log_size, 0);
  241. log_level = OPTS_GET(opts, log_level, 0);
  242. if (!!log_buf != !!log_size)
  243. return libbpf_err(-EINVAL);
  244. if (log_level > (4 | 2 | 1))
  245. return libbpf_err(-EINVAL);
  246. if (log_level && !log_buf)
  247. return libbpf_err(-EINVAL);
  248. func_info_rec_size = OPTS_GET(opts, func_info_rec_size, 0);
  249. func_info = OPTS_GET(opts, func_info, NULL);
  250. attr.func_info_rec_size = func_info_rec_size;
  251. attr.func_info = ptr_to_u64(func_info);
  252. attr.func_info_cnt = OPTS_GET(opts, func_info_cnt, 0);
  253. line_info_rec_size = OPTS_GET(opts, line_info_rec_size, 0);
  254. line_info = OPTS_GET(opts, line_info, NULL);
  255. attr.line_info_rec_size = line_info_rec_size;
  256. attr.line_info = ptr_to_u64(line_info);
  257. attr.line_info_cnt = OPTS_GET(opts, line_info_cnt, 0);
  258. attr.fd_array = ptr_to_u64(OPTS_GET(opts, fd_array, NULL));
  259. if (log_level) {
  260. attr.log_buf = ptr_to_u64(log_buf);
  261. attr.log_size = log_size;
  262. attr.log_level = log_level;
  263. }
  264. fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
  265. if (fd >= 0)
  266. return fd;
  267. /* After bpf_prog_load, the kernel may modify certain attributes
  268. * to give user space a hint how to deal with loading failure.
  269. * Check to see whether we can make some changes and load again.
  270. */
  271. while (errno == E2BIG && (!finfo || !linfo)) {
  272. if (!finfo && attr.func_info_cnt &&
  273. attr.func_info_rec_size < func_info_rec_size) {
  274. /* try with corrected func info records */
  275. finfo = alloc_zero_tailing_info(func_info,
  276. attr.func_info_cnt,
  277. func_info_rec_size,
  278. attr.func_info_rec_size);
  279. if (!finfo) {
  280. errno = E2BIG;
  281. goto done;
  282. }
  283. attr.func_info = ptr_to_u64(finfo);
  284. attr.func_info_rec_size = func_info_rec_size;
  285. } else if (!linfo && attr.line_info_cnt &&
  286. attr.line_info_rec_size < line_info_rec_size) {
  287. linfo = alloc_zero_tailing_info(line_info,
  288. attr.line_info_cnt,
  289. line_info_rec_size,
  290. attr.line_info_rec_size);
  291. if (!linfo) {
  292. errno = E2BIG;
  293. goto done;
  294. }
  295. attr.line_info = ptr_to_u64(linfo);
  296. attr.line_info_rec_size = line_info_rec_size;
  297. } else {
  298. break;
  299. }
  300. fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
  301. if (fd >= 0)
  302. goto done;
  303. }
  304. if (log_level == 0 && log_buf) {
  305. /* log_level == 0 with non-NULL log_buf requires retrying on error
  306. * with log_level == 1 and log_buf/log_buf_size set, to get details of
  307. * failure
  308. */
  309. attr.log_buf = ptr_to_u64(log_buf);
  310. attr.log_size = log_size;
  311. attr.log_level = 1;
  312. fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
  313. }
  314. done:
  315. /* free() doesn't affect errno, so we don't need to restore it */
  316. free(finfo);
  317. free(linfo);
  318. return libbpf_err_errno(fd);
  319. }
  320. int bpf_map_update_elem(int fd, const void *key, const void *value,
  321. __u64 flags)
  322. {
  323. const size_t attr_sz = offsetofend(union bpf_attr, flags);
  324. union bpf_attr attr;
  325. int ret;
  326. memset(&attr, 0, attr_sz);
  327. attr.map_fd = fd;
  328. attr.key = ptr_to_u64(key);
  329. attr.value = ptr_to_u64(value);
  330. attr.flags = flags;
  331. ret = sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, attr_sz);
  332. return libbpf_err_errno(ret);
  333. }
  334. int bpf_map_lookup_elem(int fd, const void *key, void *value)
  335. {
  336. const size_t attr_sz = offsetofend(union bpf_attr, flags);
  337. union bpf_attr attr;
  338. int ret;
  339. memset(&attr, 0, attr_sz);
  340. attr.map_fd = fd;
  341. attr.key = ptr_to_u64(key);
  342. attr.value = ptr_to_u64(value);
  343. ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
  344. return libbpf_err_errno(ret);
  345. }
  346. int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags)
  347. {
  348. const size_t attr_sz = offsetofend(union bpf_attr, flags);
  349. union bpf_attr attr;
  350. int ret;
  351. memset(&attr, 0, attr_sz);
  352. attr.map_fd = fd;
  353. attr.key = ptr_to_u64(key);
  354. attr.value = ptr_to_u64(value);
  355. attr.flags = flags;
  356. ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
  357. return libbpf_err_errno(ret);
  358. }
  359. int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value)
  360. {
  361. const size_t attr_sz = offsetofend(union bpf_attr, flags);
  362. union bpf_attr attr;
  363. int ret;
  364. memset(&attr, 0, attr_sz);
  365. attr.map_fd = fd;
  366. attr.key = ptr_to_u64(key);
  367. attr.value = ptr_to_u64(value);
  368. ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
  369. return libbpf_err_errno(ret);
  370. }
  371. int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, void *value, __u64 flags)
  372. {
  373. const size_t attr_sz = offsetofend(union bpf_attr, flags);
  374. union bpf_attr attr;
  375. int ret;
  376. memset(&attr, 0, attr_sz);
  377. attr.map_fd = fd;
  378. attr.key = ptr_to_u64(key);
  379. attr.value = ptr_to_u64(value);
  380. attr.flags = flags;
  381. ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
  382. return libbpf_err_errno(ret);
  383. }
  384. int bpf_map_delete_elem(int fd, const void *key)
  385. {
  386. const size_t attr_sz = offsetofend(union bpf_attr, flags);
  387. union bpf_attr attr;
  388. int ret;
  389. memset(&attr, 0, attr_sz);
  390. attr.map_fd = fd;
  391. attr.key = ptr_to_u64(key);
  392. ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
  393. return libbpf_err_errno(ret);
  394. }
  395. int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags)
  396. {
  397. const size_t attr_sz = offsetofend(union bpf_attr, flags);
  398. union bpf_attr attr;
  399. int ret;
  400. memset(&attr, 0, attr_sz);
  401. attr.map_fd = fd;
  402. attr.key = ptr_to_u64(key);
  403. attr.flags = flags;
  404. ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
  405. return libbpf_err_errno(ret);
  406. }
  407. int bpf_map_get_next_key(int fd, const void *key, void *next_key)
  408. {
  409. const size_t attr_sz = offsetofend(union bpf_attr, next_key);
  410. union bpf_attr attr;
  411. int ret;
  412. memset(&attr, 0, attr_sz);
  413. attr.map_fd = fd;
  414. attr.key = ptr_to_u64(key);
  415. attr.next_key = ptr_to_u64(next_key);
  416. ret = sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, attr_sz);
  417. return libbpf_err_errno(ret);
  418. }
  419. int bpf_map_freeze(int fd)
  420. {
  421. const size_t attr_sz = offsetofend(union bpf_attr, map_fd);
  422. union bpf_attr attr;
  423. int ret;
  424. memset(&attr, 0, attr_sz);
  425. attr.map_fd = fd;
  426. ret = sys_bpf(BPF_MAP_FREEZE, &attr, attr_sz);
  427. return libbpf_err_errno(ret);
  428. }
  429. static int bpf_map_batch_common(int cmd, int fd, void *in_batch,
  430. void *out_batch, void *keys, void *values,
  431. __u32 *count,
  432. const struct bpf_map_batch_opts *opts)
  433. {
  434. const size_t attr_sz = offsetofend(union bpf_attr, batch);
  435. union bpf_attr attr;
  436. int ret;
  437. if (!OPTS_VALID(opts, bpf_map_batch_opts))
  438. return libbpf_err(-EINVAL);
  439. memset(&attr, 0, attr_sz);
  440. attr.batch.map_fd = fd;
  441. attr.batch.in_batch = ptr_to_u64(in_batch);
  442. attr.batch.out_batch = ptr_to_u64(out_batch);
  443. attr.batch.keys = ptr_to_u64(keys);
  444. attr.batch.values = ptr_to_u64(values);
  445. attr.batch.count = *count;
  446. attr.batch.elem_flags = OPTS_GET(opts, elem_flags, 0);
  447. attr.batch.flags = OPTS_GET(opts, flags, 0);
  448. ret = sys_bpf(cmd, &attr, attr_sz);
  449. *count = attr.batch.count;
  450. return libbpf_err_errno(ret);
  451. }
  452. int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,
  453. const struct bpf_map_batch_opts *opts)
  454. {
  455. return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
  456. NULL, (void *)keys, NULL, count, opts);
  457. }
  458. int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, void *keys,
  459. void *values, __u32 *count,
  460. const struct bpf_map_batch_opts *opts)
  461. {
  462. return bpf_map_batch_common(BPF_MAP_LOOKUP_BATCH, fd, in_batch,
  463. out_batch, keys, values, count, opts);
  464. }
  465. int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
  466. void *keys, void *values, __u32 *count,
  467. const struct bpf_map_batch_opts *opts)
  468. {
  469. return bpf_map_batch_common(BPF_MAP_LOOKUP_AND_DELETE_BATCH,
  470. fd, in_batch, out_batch, keys, values,
  471. count, opts);
  472. }
  473. int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count,
  474. const struct bpf_map_batch_opts *opts)
  475. {
  476. return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
  477. (void *)keys, (void *)values, count, opts);
  478. }
  479. int bpf_obj_pin(int fd, const char *pathname)
  480. {
  481. const size_t attr_sz = offsetofend(union bpf_attr, file_flags);
  482. union bpf_attr attr;
  483. int ret;
  484. memset(&attr, 0, attr_sz);
  485. attr.pathname = ptr_to_u64((void *)pathname);
  486. attr.bpf_fd = fd;
  487. ret = sys_bpf(BPF_OBJ_PIN, &attr, attr_sz);
  488. return libbpf_err_errno(ret);
  489. }
  490. int bpf_obj_get(const char *pathname)
  491. {
  492. return bpf_obj_get_opts(pathname, NULL);
  493. }
  494. int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
  495. {
  496. const size_t attr_sz = offsetofend(union bpf_attr, file_flags);
  497. union bpf_attr attr;
  498. int fd;
  499. if (!OPTS_VALID(opts, bpf_obj_get_opts))
  500. return libbpf_err(-EINVAL);
  501. memset(&attr, 0, attr_sz);
  502. attr.pathname = ptr_to_u64((void *)pathname);
  503. attr.file_flags = OPTS_GET(opts, file_flags, 0);
  504. fd = sys_bpf_fd(BPF_OBJ_GET, &attr, attr_sz);
  505. return libbpf_err_errno(fd);
  506. }
  507. int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
  508. unsigned int flags)
  509. {
  510. DECLARE_LIBBPF_OPTS(bpf_prog_attach_opts, opts,
  511. .flags = flags,
  512. );
  513. return bpf_prog_attach_opts(prog_fd, target_fd, type, &opts);
  514. }
  515. int bpf_prog_attach_opts(int prog_fd, int target_fd,
  516. enum bpf_attach_type type,
  517. const struct bpf_prog_attach_opts *opts)
  518. {
  519. const size_t attr_sz = offsetofend(union bpf_attr, replace_bpf_fd);
  520. union bpf_attr attr;
  521. int ret;
  522. if (!OPTS_VALID(opts, bpf_prog_attach_opts))
  523. return libbpf_err(-EINVAL);
  524. memset(&attr, 0, attr_sz);
  525. attr.target_fd = target_fd;
  526. attr.attach_bpf_fd = prog_fd;
  527. attr.attach_type = type;
  528. attr.attach_flags = OPTS_GET(opts, flags, 0);
  529. attr.replace_bpf_fd = OPTS_GET(opts, replace_prog_fd, 0);
  530. ret = sys_bpf(BPF_PROG_ATTACH, &attr, attr_sz);
  531. return libbpf_err_errno(ret);
  532. }
  533. int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
  534. {
  535. const size_t attr_sz = offsetofend(union bpf_attr, replace_bpf_fd);
  536. union bpf_attr attr;
  537. int ret;
  538. memset(&attr, 0, attr_sz);
  539. attr.target_fd = target_fd;
  540. attr.attach_type = type;
  541. ret = sys_bpf(BPF_PROG_DETACH, &attr, attr_sz);
  542. return libbpf_err_errno(ret);
  543. }
  544. int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
  545. {
  546. const size_t attr_sz = offsetofend(union bpf_attr, replace_bpf_fd);
  547. union bpf_attr attr;
  548. int ret;
  549. memset(&attr, 0, attr_sz);
  550. attr.target_fd = target_fd;
  551. attr.attach_bpf_fd = prog_fd;
  552. attr.attach_type = type;
  553. ret = sys_bpf(BPF_PROG_DETACH, &attr, attr_sz);
  554. return libbpf_err_errno(ret);
  555. }
  556. int bpf_link_create(int prog_fd, int target_fd,
  557. enum bpf_attach_type attach_type,
  558. const struct bpf_link_create_opts *opts)
  559. {
  560. const size_t attr_sz = offsetofend(union bpf_attr, link_create);
  561. __u32 target_btf_id, iter_info_len;
  562. union bpf_attr attr;
  563. int fd, err;
  564. if (!OPTS_VALID(opts, bpf_link_create_opts))
  565. return libbpf_err(-EINVAL);
  566. iter_info_len = OPTS_GET(opts, iter_info_len, 0);
  567. target_btf_id = OPTS_GET(opts, target_btf_id, 0);
  568. /* validate we don't have unexpected combinations of non-zero fields */
  569. if (iter_info_len || target_btf_id) {
  570. if (iter_info_len && target_btf_id)
  571. return libbpf_err(-EINVAL);
  572. if (!OPTS_ZEROED(opts, target_btf_id))
  573. return libbpf_err(-EINVAL);
  574. }
  575. memset(&attr, 0, attr_sz);
  576. attr.link_create.prog_fd = prog_fd;
  577. attr.link_create.target_fd = target_fd;
  578. attr.link_create.attach_type = attach_type;
  579. attr.link_create.flags = OPTS_GET(opts, flags, 0);
  580. if (target_btf_id) {
  581. attr.link_create.target_btf_id = target_btf_id;
  582. goto proceed;
  583. }
  584. switch (attach_type) {
  585. case BPF_TRACE_ITER:
  586. attr.link_create.iter_info = ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
  587. attr.link_create.iter_info_len = iter_info_len;
  588. break;
  589. case BPF_PERF_EVENT:
  590. attr.link_create.perf_event.bpf_cookie = OPTS_GET(opts, perf_event.bpf_cookie, 0);
  591. if (!OPTS_ZEROED(opts, perf_event))
  592. return libbpf_err(-EINVAL);
  593. break;
  594. case BPF_TRACE_KPROBE_MULTI:
  595. attr.link_create.kprobe_multi.flags = OPTS_GET(opts, kprobe_multi.flags, 0);
  596. attr.link_create.kprobe_multi.cnt = OPTS_GET(opts, kprobe_multi.cnt, 0);
  597. attr.link_create.kprobe_multi.syms = ptr_to_u64(OPTS_GET(opts, kprobe_multi.syms, 0));
  598. attr.link_create.kprobe_multi.addrs = ptr_to_u64(OPTS_GET(opts, kprobe_multi.addrs, 0));
  599. attr.link_create.kprobe_multi.cookies = ptr_to_u64(OPTS_GET(opts, kprobe_multi.cookies, 0));
  600. if (!OPTS_ZEROED(opts, kprobe_multi))
  601. return libbpf_err(-EINVAL);
  602. break;
  603. case BPF_TRACE_FENTRY:
  604. case BPF_TRACE_FEXIT:
  605. case BPF_MODIFY_RETURN:
  606. case BPF_LSM_MAC:
  607. attr.link_create.tracing.cookie = OPTS_GET(opts, tracing.cookie, 0);
  608. if (!OPTS_ZEROED(opts, tracing))
  609. return libbpf_err(-EINVAL);
  610. break;
  611. default:
  612. if (!OPTS_ZEROED(opts, flags))
  613. return libbpf_err(-EINVAL);
  614. break;
  615. }
  616. proceed:
  617. fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
  618. if (fd >= 0)
  619. return fd;
  620. /* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
  621. * and other similar programs
  622. */
  623. err = -errno;
  624. if (err != -EINVAL)
  625. return libbpf_err(err);
  626. /* if user used features not supported by
  627. * BPF_RAW_TRACEPOINT_OPEN command, then just give up immediately
  628. */
  629. if (attr.link_create.target_fd || attr.link_create.target_btf_id)
  630. return libbpf_err(err);
  631. if (!OPTS_ZEROED(opts, sz))
  632. return libbpf_err(err);
  633. /* otherwise, for few select kinds of programs that can be
  634. * attached using BPF_RAW_TRACEPOINT_OPEN command, try that as
  635. * a fallback for older kernels
  636. */
  637. switch (attach_type) {
  638. case BPF_TRACE_RAW_TP:
  639. case BPF_LSM_MAC:
  640. case BPF_TRACE_FENTRY:
  641. case BPF_TRACE_FEXIT:
  642. case BPF_MODIFY_RETURN:
  643. return bpf_raw_tracepoint_open(NULL, prog_fd);
  644. default:
  645. return libbpf_err(err);
  646. }
  647. }
  648. int bpf_link_detach(int link_fd)
  649. {
  650. const size_t attr_sz = offsetofend(union bpf_attr, link_detach);
  651. union bpf_attr attr;
  652. int ret;
  653. memset(&attr, 0, attr_sz);
  654. attr.link_detach.link_fd = link_fd;
  655. ret = sys_bpf(BPF_LINK_DETACH, &attr, attr_sz);
  656. return libbpf_err_errno(ret);
  657. }
  658. int bpf_link_update(int link_fd, int new_prog_fd,
  659. const struct bpf_link_update_opts *opts)
  660. {
  661. const size_t attr_sz = offsetofend(union bpf_attr, link_update);
  662. union bpf_attr attr;
  663. int ret;
  664. if (!OPTS_VALID(opts, bpf_link_update_opts))
  665. return libbpf_err(-EINVAL);
  666. memset(&attr, 0, attr_sz);
  667. attr.link_update.link_fd = link_fd;
  668. attr.link_update.new_prog_fd = new_prog_fd;
  669. attr.link_update.flags = OPTS_GET(opts, flags, 0);
  670. attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0);
  671. ret = sys_bpf(BPF_LINK_UPDATE, &attr, attr_sz);
  672. return libbpf_err_errno(ret);
  673. }
  674. int bpf_iter_create(int link_fd)
  675. {
  676. const size_t attr_sz = offsetofend(union bpf_attr, iter_create);
  677. union bpf_attr attr;
  678. int fd;
  679. memset(&attr, 0, attr_sz);
  680. attr.iter_create.link_fd = link_fd;
  681. fd = sys_bpf_fd(BPF_ITER_CREATE, &attr, attr_sz);
  682. return libbpf_err_errno(fd);
  683. }
  684. int bpf_prog_query_opts(int target_fd,
  685. enum bpf_attach_type type,
  686. struct bpf_prog_query_opts *opts)
  687. {
  688. const size_t attr_sz = offsetofend(union bpf_attr, query);
  689. union bpf_attr attr;
  690. int ret;
  691. if (!OPTS_VALID(opts, bpf_prog_query_opts))
  692. return libbpf_err(-EINVAL);
  693. memset(&attr, 0, attr_sz);
  694. attr.query.target_fd = target_fd;
  695. attr.query.attach_type = type;
  696. attr.query.query_flags = OPTS_GET(opts, query_flags, 0);
  697. attr.query.prog_cnt = OPTS_GET(opts, prog_cnt, 0);
  698. attr.query.prog_ids = ptr_to_u64(OPTS_GET(opts, prog_ids, NULL));
  699. attr.query.prog_attach_flags = ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL));
  700. ret = sys_bpf(BPF_PROG_QUERY, &attr, attr_sz);
  701. OPTS_SET(opts, attach_flags, attr.query.attach_flags);
  702. OPTS_SET(opts, prog_cnt, attr.query.prog_cnt);
  703. return libbpf_err_errno(ret);
  704. }
  705. int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
  706. __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
  707. {
  708. LIBBPF_OPTS(bpf_prog_query_opts, opts);
  709. int ret;
  710. opts.query_flags = query_flags;
  711. opts.prog_ids = prog_ids;
  712. opts.prog_cnt = *prog_cnt;
  713. ret = bpf_prog_query_opts(target_fd, type, &opts);
  714. if (attach_flags)
  715. *attach_flags = opts.attach_flags;
  716. *prog_cnt = opts.prog_cnt;
  717. return libbpf_err_errno(ret);
  718. }
  719. int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
  720. {
  721. const size_t attr_sz = offsetofend(union bpf_attr, test);
  722. union bpf_attr attr;
  723. int ret;
  724. if (!OPTS_VALID(opts, bpf_test_run_opts))
  725. return libbpf_err(-EINVAL);
  726. memset(&attr, 0, attr_sz);
  727. attr.test.prog_fd = prog_fd;
  728. attr.test.batch_size = OPTS_GET(opts, batch_size, 0);
  729. attr.test.cpu = OPTS_GET(opts, cpu, 0);
  730. attr.test.flags = OPTS_GET(opts, flags, 0);
  731. attr.test.repeat = OPTS_GET(opts, repeat, 0);
  732. attr.test.duration = OPTS_GET(opts, duration, 0);
  733. attr.test.ctx_size_in = OPTS_GET(opts, ctx_size_in, 0);
  734. attr.test.ctx_size_out = OPTS_GET(opts, ctx_size_out, 0);
  735. attr.test.data_size_in = OPTS_GET(opts, data_size_in, 0);
  736. attr.test.data_size_out = OPTS_GET(opts, data_size_out, 0);
  737. attr.test.ctx_in = ptr_to_u64(OPTS_GET(opts, ctx_in, NULL));
  738. attr.test.ctx_out = ptr_to_u64(OPTS_GET(opts, ctx_out, NULL));
  739. attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL));
  740. attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL));
  741. ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, attr_sz);
  742. OPTS_SET(opts, data_size_out, attr.test.data_size_out);
  743. OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out);
  744. OPTS_SET(opts, duration, attr.test.duration);
  745. OPTS_SET(opts, retval, attr.test.retval);
  746. return libbpf_err_errno(ret);
  747. }
  748. static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)
  749. {
  750. const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
  751. union bpf_attr attr;
  752. int err;
  753. memset(&attr, 0, attr_sz);
  754. attr.start_id = start_id;
  755. err = sys_bpf(cmd, &attr, attr_sz);
  756. if (!err)
  757. *next_id = attr.next_id;
  758. return libbpf_err_errno(err);
  759. }
  760. int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
  761. {
  762. return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID);
  763. }
  764. int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
  765. {
  766. return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID);
  767. }
  768. int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id)
  769. {
  770. return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID);
  771. }
  772. int bpf_link_get_next_id(__u32 start_id, __u32 *next_id)
  773. {
  774. return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID);
  775. }
  776. int bpf_prog_get_fd_by_id(__u32 id)
  777. {
  778. const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
  779. union bpf_attr attr;
  780. int fd;
  781. memset(&attr, 0, attr_sz);
  782. attr.prog_id = id;
  783. fd = sys_bpf_fd(BPF_PROG_GET_FD_BY_ID, &attr, attr_sz);
  784. return libbpf_err_errno(fd);
  785. }
  786. int bpf_map_get_fd_by_id(__u32 id)
  787. {
  788. const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
  789. union bpf_attr attr;
  790. int fd;
  791. memset(&attr, 0, attr_sz);
  792. attr.map_id = id;
  793. fd = sys_bpf_fd(BPF_MAP_GET_FD_BY_ID, &attr, attr_sz);
  794. return libbpf_err_errno(fd);
  795. }
  796. int bpf_btf_get_fd_by_id(__u32 id)
  797. {
  798. const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
  799. union bpf_attr attr;
  800. int fd;
  801. memset(&attr, 0, attr_sz);
  802. attr.btf_id = id;
  803. fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz);
  804. return libbpf_err_errno(fd);
  805. }
  806. int bpf_link_get_fd_by_id(__u32 id)
  807. {
  808. const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
  809. union bpf_attr attr;
  810. int fd;
  811. memset(&attr, 0, attr_sz);
  812. attr.link_id = id;
  813. fd = sys_bpf_fd(BPF_LINK_GET_FD_BY_ID, &attr, attr_sz);
  814. return libbpf_err_errno(fd);
  815. }
  816. int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)
  817. {
  818. const size_t attr_sz = offsetofend(union bpf_attr, info);
  819. union bpf_attr attr;
  820. int err;
  821. memset(&attr, 0, attr_sz);
  822. attr.info.bpf_fd = bpf_fd;
  823. attr.info.info_len = *info_len;
  824. attr.info.info = ptr_to_u64(info);
  825. err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, attr_sz);
  826. if (!err)
  827. *info_len = attr.info.info_len;
  828. return libbpf_err_errno(err);
  829. }
  830. int bpf_raw_tracepoint_open(const char *name, int prog_fd)
  831. {
  832. const size_t attr_sz = offsetofend(union bpf_attr, raw_tracepoint);
  833. union bpf_attr attr;
  834. int fd;
  835. memset(&attr, 0, attr_sz);
  836. attr.raw_tracepoint.name = ptr_to_u64(name);
  837. attr.raw_tracepoint.prog_fd = prog_fd;
  838. fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, attr_sz);
  839. return libbpf_err_errno(fd);
  840. }
  841. int bpf_btf_load(const void *btf_data, size_t btf_size, const struct bpf_btf_load_opts *opts)
  842. {
  843. const size_t attr_sz = offsetofend(union bpf_attr, btf_log_level);
  844. union bpf_attr attr;
  845. char *log_buf;
  846. size_t log_size;
  847. __u32 log_level;
  848. int fd;
  849. bump_rlimit_memlock();
  850. memset(&attr, 0, attr_sz);
  851. if (!OPTS_VALID(opts, bpf_btf_load_opts))
  852. return libbpf_err(-EINVAL);
  853. log_buf = OPTS_GET(opts, log_buf, NULL);
  854. log_size = OPTS_GET(opts, log_size, 0);
  855. log_level = OPTS_GET(opts, log_level, 0);
  856. if (log_size > UINT_MAX)
  857. return libbpf_err(-EINVAL);
  858. if (log_size && !log_buf)
  859. return libbpf_err(-EINVAL);
  860. attr.btf = ptr_to_u64(btf_data);
  861. attr.btf_size = btf_size;
  862. /* log_level == 0 and log_buf != NULL means "try loading without
  863. * log_buf, but retry with log_buf and log_level=1 on error", which is
  864. * consistent across low-level and high-level BTF and program loading
  865. * APIs within libbpf and provides a sensible behavior in practice
  866. */
  867. if (log_level) {
  868. attr.btf_log_buf = ptr_to_u64(log_buf);
  869. attr.btf_log_size = (__u32)log_size;
  870. attr.btf_log_level = log_level;
  871. }
  872. fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
  873. if (fd < 0 && log_buf && log_level == 0) {
  874. attr.btf_log_buf = ptr_to_u64(log_buf);
  875. attr.btf_log_size = (__u32)log_size;
  876. attr.btf_log_level = 1;
  877. fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
  878. }
  879. return libbpf_err_errno(fd);
  880. }
  881. int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
  882. __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
  883. __u64 *probe_addr)
  884. {
  885. const size_t attr_sz = offsetofend(union bpf_attr, task_fd_query);
  886. union bpf_attr attr;
  887. int err;
  888. memset(&attr, 0, attr_sz);
  889. attr.task_fd_query.pid = pid;
  890. attr.task_fd_query.fd = fd;
  891. attr.task_fd_query.flags = flags;
  892. attr.task_fd_query.buf = ptr_to_u64(buf);
  893. attr.task_fd_query.buf_len = *buf_len;
  894. err = sys_bpf(BPF_TASK_FD_QUERY, &attr, attr_sz);
  895. *buf_len = attr.task_fd_query.buf_len;
  896. *prog_id = attr.task_fd_query.prog_id;
  897. *fd_type = attr.task_fd_query.fd_type;
  898. *probe_offset = attr.task_fd_query.probe_offset;
  899. *probe_addr = attr.task_fd_query.probe_addr;
  900. return libbpf_err_errno(err);
  901. }
  902. int bpf_enable_stats(enum bpf_stats_type type)
  903. {
  904. const size_t attr_sz = offsetofend(union bpf_attr, enable_stats);
  905. union bpf_attr attr;
  906. int fd;
  907. memset(&attr, 0, attr_sz);
  908. attr.enable_stats.type = type;
  909. fd = sys_bpf_fd(BPF_ENABLE_STATS, &attr, attr_sz);
  910. return libbpf_err_errno(fd);
  911. }
  912. int bpf_prog_bind_map(int prog_fd, int map_fd,
  913. const struct bpf_prog_bind_opts *opts)
  914. {
  915. const size_t attr_sz = offsetofend(union bpf_attr, prog_bind_map);
  916. union bpf_attr attr;
  917. int ret;
  918. if (!OPTS_VALID(opts, bpf_prog_bind_opts))
  919. return libbpf_err(-EINVAL);
  920. memset(&attr, 0, attr_sz);
  921. attr.prog_bind_map.prog_fd = prog_fd;
  922. attr.prog_bind_map.map_fd = map_fd;
  923. attr.prog_bind_map.flags = OPTS_GET(opts, flags, 0);
  924. ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, attr_sz);
  925. return libbpf_err_errno(ret);
  926. }