libbpf.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  1. /*
  2. * Common eBPF ELF object loading 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. * Copyright (C) 2017 Nicira, 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 <stdio.h>
  24. #include <stdarg.h>
  25. #include <libgen.h>
  26. #include <inttypes.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <errno.h>
  31. #include <asm/unistd.h>
  32. #include <linux/err.h>
  33. #include <linux/kernel.h>
  34. #include <linux/bpf.h>
  35. #include <linux/list.h>
  36. #include <linux/limits.h>
  37. #include <sys/stat.h>
  38. #include <sys/types.h>
  39. #include <sys/vfs.h>
  40. #include <libelf.h>
  41. #include <gelf.h>
  42. #include "libbpf.h"
  43. #include "bpf.h"
  44. #ifndef EM_BPF
  45. #define EM_BPF 247
  46. #endif
  47. #ifndef BPF_FS_MAGIC
  48. #define BPF_FS_MAGIC 0xcafe4a11
  49. #endif
  50. #define __printf(a, b) __attribute__((format(printf, a, b)))
  51. __printf(1, 2)
  52. static int __base_pr(const char *format, ...)
  53. {
  54. va_list args;
  55. int err;
  56. va_start(args, format);
  57. err = vfprintf(stderr, format, args);
  58. va_end(args);
  59. return err;
  60. }
  61. static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
  62. static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
  63. static __printf(1, 2) libbpf_print_fn_t __pr_debug;
  64. #define __pr(func, fmt, ...) \
  65. do { \
  66. if ((func)) \
  67. (func)("libbpf: " fmt, ##__VA_ARGS__); \
  68. } while (0)
  69. #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
  70. #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
  71. #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
  72. void libbpf_set_print(libbpf_print_fn_t warn,
  73. libbpf_print_fn_t info,
  74. libbpf_print_fn_t debug)
  75. {
  76. __pr_warning = warn;
  77. __pr_info = info;
  78. __pr_debug = debug;
  79. }
  80. #define STRERR_BUFSIZE 128
  81. #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
  82. #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
  83. #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
  84. static const char *libbpf_strerror_table[NR_ERRNO] = {
  85. [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
  86. [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
  87. [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
  88. [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
  89. [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
  90. [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
  91. [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
  92. [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
  93. [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
  94. [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
  95. };
  96. int libbpf_strerror(int err, char *buf, size_t size)
  97. {
  98. if (!buf || !size)
  99. return -1;
  100. err = err > 0 ? err : -err;
  101. if (err < __LIBBPF_ERRNO__START) {
  102. int ret;
  103. ret = strerror_r(err, buf, size);
  104. buf[size - 1] = '\0';
  105. return ret;
  106. }
  107. if (err < __LIBBPF_ERRNO__END) {
  108. const char *msg;
  109. msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
  110. snprintf(buf, size, "%s", msg);
  111. buf[size - 1] = '\0';
  112. return 0;
  113. }
  114. snprintf(buf, size, "Unknown libbpf error %d", err);
  115. buf[size - 1] = '\0';
  116. return -1;
  117. }
  118. #define CHECK_ERR(action, err, out) do { \
  119. err = action; \
  120. if (err) \
  121. goto out; \
  122. } while(0)
  123. /* Copied from tools/perf/util/util.h */
  124. #ifndef zfree
  125. # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
  126. #endif
  127. #ifndef zclose
  128. # define zclose(fd) ({ \
  129. int ___err = 0; \
  130. if ((fd) >= 0) \
  131. ___err = close((fd)); \
  132. fd = -1; \
  133. ___err; })
  134. #endif
  135. #ifdef HAVE_LIBELF_MMAP_SUPPORT
  136. # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
  137. #else
  138. # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
  139. #endif
  140. /*
  141. * bpf_prog should be a better name but it has been used in
  142. * linux/filter.h.
  143. */
  144. struct bpf_program {
  145. /* Index in elf obj file, for relocation use. */
  146. int idx;
  147. char *section_name;
  148. struct bpf_insn *insns;
  149. size_t insns_cnt;
  150. enum bpf_prog_type type;
  151. struct {
  152. int insn_idx;
  153. int map_idx;
  154. } *reloc_desc;
  155. int nr_reloc;
  156. struct {
  157. int nr;
  158. int *fds;
  159. } instances;
  160. bpf_program_prep_t preprocessor;
  161. struct bpf_object *obj;
  162. void *priv;
  163. bpf_program_clear_priv_t clear_priv;
  164. };
  165. struct bpf_map {
  166. int fd;
  167. char *name;
  168. size_t offset;
  169. struct bpf_map_def def;
  170. void *priv;
  171. bpf_map_clear_priv_t clear_priv;
  172. };
  173. static LIST_HEAD(bpf_objects_list);
  174. struct bpf_object {
  175. char license[64];
  176. u32 kern_version;
  177. struct bpf_program *programs;
  178. size_t nr_programs;
  179. struct bpf_map *maps;
  180. size_t nr_maps;
  181. bool loaded;
  182. /*
  183. * Information when doing elf related work. Only valid if fd
  184. * is valid.
  185. */
  186. struct {
  187. int fd;
  188. void *obj_buf;
  189. size_t obj_buf_sz;
  190. Elf *elf;
  191. GElf_Ehdr ehdr;
  192. Elf_Data *symbols;
  193. size_t strtabidx;
  194. struct {
  195. GElf_Shdr shdr;
  196. Elf_Data *data;
  197. } *reloc;
  198. int nr_reloc;
  199. int maps_shndx;
  200. } efile;
  201. /*
  202. * All loaded bpf_object is linked in a list, which is
  203. * hidden to caller. bpf_objects__<func> handlers deal with
  204. * all objects.
  205. */
  206. struct list_head list;
  207. void *priv;
  208. bpf_object_clear_priv_t clear_priv;
  209. char path[];
  210. };
  211. #define obj_elf_valid(o) ((o)->efile.elf)
  212. static void bpf_program__unload(struct bpf_program *prog)
  213. {
  214. int i;
  215. if (!prog)
  216. return;
  217. /*
  218. * If the object is opened but the program was never loaded,
  219. * it is possible that prog->instances.nr == -1.
  220. */
  221. if (prog->instances.nr > 0) {
  222. for (i = 0; i < prog->instances.nr; i++)
  223. zclose(prog->instances.fds[i]);
  224. } else if (prog->instances.nr != -1) {
  225. pr_warning("Internal error: instances.nr is %d\n",
  226. prog->instances.nr);
  227. }
  228. prog->instances.nr = -1;
  229. zfree(&prog->instances.fds);
  230. }
  231. static void bpf_program__exit(struct bpf_program *prog)
  232. {
  233. if (!prog)
  234. return;
  235. if (prog->clear_priv)
  236. prog->clear_priv(prog, prog->priv);
  237. prog->priv = NULL;
  238. prog->clear_priv = NULL;
  239. bpf_program__unload(prog);
  240. zfree(&prog->section_name);
  241. zfree(&prog->insns);
  242. zfree(&prog->reloc_desc);
  243. prog->nr_reloc = 0;
  244. prog->insns_cnt = 0;
  245. prog->idx = -1;
  246. }
  247. static int
  248. bpf_program__init(void *data, size_t size, char *name, int idx,
  249. struct bpf_program *prog)
  250. {
  251. if (size < sizeof(struct bpf_insn)) {
  252. pr_warning("corrupted section '%s'\n", name);
  253. return -EINVAL;
  254. }
  255. bzero(prog, sizeof(*prog));
  256. prog->section_name = strdup(name);
  257. if (!prog->section_name) {
  258. pr_warning("failed to alloc name for prog %s\n",
  259. name);
  260. goto errout;
  261. }
  262. prog->insns = malloc(size);
  263. if (!prog->insns) {
  264. pr_warning("failed to alloc insns for %s\n", name);
  265. goto errout;
  266. }
  267. prog->insns_cnt = size / sizeof(struct bpf_insn);
  268. memcpy(prog->insns, data,
  269. prog->insns_cnt * sizeof(struct bpf_insn));
  270. prog->idx = idx;
  271. prog->instances.fds = NULL;
  272. prog->instances.nr = -1;
  273. prog->type = BPF_PROG_TYPE_KPROBE;
  274. return 0;
  275. errout:
  276. bpf_program__exit(prog);
  277. return -ENOMEM;
  278. }
  279. static int
  280. bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
  281. char *name, int idx)
  282. {
  283. struct bpf_program prog, *progs;
  284. int nr_progs, err;
  285. err = bpf_program__init(data, size, name, idx, &prog);
  286. if (err)
  287. return err;
  288. progs = obj->programs;
  289. nr_progs = obj->nr_programs;
  290. progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
  291. if (!progs) {
  292. /*
  293. * In this case the original obj->programs
  294. * is still valid, so don't need special treat for
  295. * bpf_close_object().
  296. */
  297. pr_warning("failed to alloc a new program '%s'\n",
  298. name);
  299. bpf_program__exit(&prog);
  300. return -ENOMEM;
  301. }
  302. pr_debug("found program %s\n", prog.section_name);
  303. obj->programs = progs;
  304. obj->nr_programs = nr_progs + 1;
  305. prog.obj = obj;
  306. progs[nr_progs] = prog;
  307. return 0;
  308. }
  309. static struct bpf_object *bpf_object__new(const char *path,
  310. void *obj_buf,
  311. size_t obj_buf_sz)
  312. {
  313. struct bpf_object *obj;
  314. obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
  315. if (!obj) {
  316. pr_warning("alloc memory failed for %s\n", path);
  317. return ERR_PTR(-ENOMEM);
  318. }
  319. strcpy(obj->path, path);
  320. obj->efile.fd = -1;
  321. /*
  322. * Caller of this function should also calls
  323. * bpf_object__elf_finish() after data collection to return
  324. * obj_buf to user. If not, we should duplicate the buffer to
  325. * avoid user freeing them before elf finish.
  326. */
  327. obj->efile.obj_buf = obj_buf;
  328. obj->efile.obj_buf_sz = obj_buf_sz;
  329. obj->efile.maps_shndx = -1;
  330. obj->loaded = false;
  331. INIT_LIST_HEAD(&obj->list);
  332. list_add(&obj->list, &bpf_objects_list);
  333. return obj;
  334. }
  335. static void bpf_object__elf_finish(struct bpf_object *obj)
  336. {
  337. if (!obj_elf_valid(obj))
  338. return;
  339. if (obj->efile.elf) {
  340. elf_end(obj->efile.elf);
  341. obj->efile.elf = NULL;
  342. }
  343. obj->efile.symbols = NULL;
  344. zfree(&obj->efile.reloc);
  345. obj->efile.nr_reloc = 0;
  346. zclose(obj->efile.fd);
  347. obj->efile.obj_buf = NULL;
  348. obj->efile.obj_buf_sz = 0;
  349. }
  350. static int bpf_object__elf_init(struct bpf_object *obj)
  351. {
  352. int err = 0;
  353. GElf_Ehdr *ep;
  354. if (obj_elf_valid(obj)) {
  355. pr_warning("elf init: internal error\n");
  356. return -LIBBPF_ERRNO__LIBELF;
  357. }
  358. if (obj->efile.obj_buf_sz > 0) {
  359. /*
  360. * obj_buf should have been validated by
  361. * bpf_object__open_buffer().
  362. */
  363. obj->efile.elf = elf_memory(obj->efile.obj_buf,
  364. obj->efile.obj_buf_sz);
  365. } else {
  366. obj->efile.fd = open(obj->path, O_RDONLY);
  367. if (obj->efile.fd < 0) {
  368. pr_warning("failed to open %s: %s\n", obj->path,
  369. strerror(errno));
  370. return -errno;
  371. }
  372. obj->efile.elf = elf_begin(obj->efile.fd,
  373. LIBBPF_ELF_C_READ_MMAP,
  374. NULL);
  375. }
  376. if (!obj->efile.elf) {
  377. pr_warning("failed to open %s as ELF file\n",
  378. obj->path);
  379. err = -LIBBPF_ERRNO__LIBELF;
  380. goto errout;
  381. }
  382. if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
  383. pr_warning("failed to get EHDR from %s\n",
  384. obj->path);
  385. err = -LIBBPF_ERRNO__FORMAT;
  386. goto errout;
  387. }
  388. ep = &obj->efile.ehdr;
  389. /* Old LLVM set e_machine to EM_NONE */
  390. if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
  391. pr_warning("%s is not an eBPF object file\n",
  392. obj->path);
  393. err = -LIBBPF_ERRNO__FORMAT;
  394. goto errout;
  395. }
  396. return 0;
  397. errout:
  398. bpf_object__elf_finish(obj);
  399. return err;
  400. }
  401. static int
  402. bpf_object__check_endianness(struct bpf_object *obj)
  403. {
  404. static unsigned int const endian = 1;
  405. switch (obj->efile.ehdr.e_ident[EI_DATA]) {
  406. case ELFDATA2LSB:
  407. /* We are big endian, BPF obj is little endian. */
  408. if (*(unsigned char const *)&endian != 1)
  409. goto mismatch;
  410. break;
  411. case ELFDATA2MSB:
  412. /* We are little endian, BPF obj is big endian. */
  413. if (*(unsigned char const *)&endian != 0)
  414. goto mismatch;
  415. break;
  416. default:
  417. return -LIBBPF_ERRNO__ENDIAN;
  418. }
  419. return 0;
  420. mismatch:
  421. pr_warning("Error: endianness mismatch.\n");
  422. return -LIBBPF_ERRNO__ENDIAN;
  423. }
  424. static int
  425. bpf_object__init_license(struct bpf_object *obj,
  426. void *data, size_t size)
  427. {
  428. memcpy(obj->license, data,
  429. min(size, sizeof(obj->license) - 1));
  430. pr_debug("license of %s is %s\n", obj->path, obj->license);
  431. return 0;
  432. }
  433. static int
  434. bpf_object__init_kversion(struct bpf_object *obj,
  435. void *data, size_t size)
  436. {
  437. u32 kver;
  438. if (size != sizeof(kver)) {
  439. pr_warning("invalid kver section in %s\n", obj->path);
  440. return -LIBBPF_ERRNO__FORMAT;
  441. }
  442. memcpy(&kver, data, sizeof(kver));
  443. obj->kern_version = kver;
  444. pr_debug("kernel version of %s is %x\n", obj->path,
  445. obj->kern_version);
  446. return 0;
  447. }
  448. static int
  449. bpf_object__validate_maps(struct bpf_object *obj)
  450. {
  451. int i;
  452. /*
  453. * If there's only 1 map, the only error case should have been
  454. * catched in bpf_object__init_maps().
  455. */
  456. if (!obj->maps || !obj->nr_maps || (obj->nr_maps == 1))
  457. return 0;
  458. for (i = 1; i < obj->nr_maps; i++) {
  459. const struct bpf_map *a = &obj->maps[i - 1];
  460. const struct bpf_map *b = &obj->maps[i];
  461. if (b->offset - a->offset < sizeof(struct bpf_map_def)) {
  462. pr_warning("corrupted map section in %s: map \"%s\" too small\n",
  463. obj->path, a->name);
  464. return -EINVAL;
  465. }
  466. }
  467. return 0;
  468. }
  469. static int compare_bpf_map(const void *_a, const void *_b)
  470. {
  471. const struct bpf_map *a = _a;
  472. const struct bpf_map *b = _b;
  473. return a->offset - b->offset;
  474. }
  475. static int
  476. bpf_object__init_maps(struct bpf_object *obj)
  477. {
  478. int i, map_idx, nr_maps = 0;
  479. Elf_Scn *scn;
  480. Elf_Data *data;
  481. Elf_Data *symbols = obj->efile.symbols;
  482. if (obj->efile.maps_shndx < 0)
  483. return -EINVAL;
  484. if (!symbols)
  485. return -EINVAL;
  486. scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
  487. if (scn)
  488. data = elf_getdata(scn, NULL);
  489. if (!scn || !data) {
  490. pr_warning("failed to get Elf_Data from map section %d\n",
  491. obj->efile.maps_shndx);
  492. return -EINVAL;
  493. }
  494. /*
  495. * Count number of maps. Each map has a name.
  496. * Array of maps is not supported: only the first element is
  497. * considered.
  498. *
  499. * TODO: Detect array of map and report error.
  500. */
  501. for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
  502. GElf_Sym sym;
  503. if (!gelf_getsym(symbols, i, &sym))
  504. continue;
  505. if (sym.st_shndx != obj->efile.maps_shndx)
  506. continue;
  507. nr_maps++;
  508. }
  509. /* Alloc obj->maps and fill nr_maps. */
  510. pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
  511. nr_maps, data->d_size);
  512. if (!nr_maps)
  513. return 0;
  514. obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
  515. if (!obj->maps) {
  516. pr_warning("alloc maps for object failed\n");
  517. return -ENOMEM;
  518. }
  519. obj->nr_maps = nr_maps;
  520. /*
  521. * fill all fd with -1 so won't close incorrect
  522. * fd (fd=0 is stdin) when failure (zclose won't close
  523. * negative fd)).
  524. */
  525. for (i = 0; i < nr_maps; i++)
  526. obj->maps[i].fd = -1;
  527. /*
  528. * Fill obj->maps using data in "maps" section.
  529. */
  530. for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
  531. GElf_Sym sym;
  532. const char *map_name;
  533. struct bpf_map_def *def;
  534. if (!gelf_getsym(symbols, i, &sym))
  535. continue;
  536. if (sym.st_shndx != obj->efile.maps_shndx)
  537. continue;
  538. map_name = elf_strptr(obj->efile.elf,
  539. obj->efile.strtabidx,
  540. sym.st_name);
  541. obj->maps[map_idx].offset = sym.st_value;
  542. if (sym.st_value + sizeof(struct bpf_map_def) > data->d_size) {
  543. pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
  544. obj->path, map_name);
  545. return -EINVAL;
  546. }
  547. obj->maps[map_idx].name = strdup(map_name);
  548. if (!obj->maps[map_idx].name) {
  549. pr_warning("failed to alloc map name\n");
  550. return -ENOMEM;
  551. }
  552. pr_debug("map %d is \"%s\"\n", map_idx,
  553. obj->maps[map_idx].name);
  554. def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
  555. obj->maps[map_idx].def = *def;
  556. map_idx++;
  557. }
  558. qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
  559. return bpf_object__validate_maps(obj);
  560. }
  561. static int bpf_object__elf_collect(struct bpf_object *obj)
  562. {
  563. Elf *elf = obj->efile.elf;
  564. GElf_Ehdr *ep = &obj->efile.ehdr;
  565. Elf_Scn *scn = NULL;
  566. int idx = 0, err = 0;
  567. /* Elf is corrupted/truncated, avoid calling elf_strptr. */
  568. if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
  569. pr_warning("failed to get e_shstrndx from %s\n",
  570. obj->path);
  571. return -LIBBPF_ERRNO__FORMAT;
  572. }
  573. while ((scn = elf_nextscn(elf, scn)) != NULL) {
  574. char *name;
  575. GElf_Shdr sh;
  576. Elf_Data *data;
  577. idx++;
  578. if (gelf_getshdr(scn, &sh) != &sh) {
  579. pr_warning("failed to get section header from %s\n",
  580. obj->path);
  581. err = -LIBBPF_ERRNO__FORMAT;
  582. goto out;
  583. }
  584. name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
  585. if (!name) {
  586. pr_warning("failed to get section name from %s\n",
  587. obj->path);
  588. err = -LIBBPF_ERRNO__FORMAT;
  589. goto out;
  590. }
  591. data = elf_getdata(scn, 0);
  592. if (!data) {
  593. pr_warning("failed to get section data from %s(%s)\n",
  594. name, obj->path);
  595. err = -LIBBPF_ERRNO__FORMAT;
  596. goto out;
  597. }
  598. pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
  599. name, (unsigned long)data->d_size,
  600. (int)sh.sh_link, (unsigned long)sh.sh_flags,
  601. (int)sh.sh_type);
  602. if (strcmp(name, "license") == 0)
  603. err = bpf_object__init_license(obj,
  604. data->d_buf,
  605. data->d_size);
  606. else if (strcmp(name, "version") == 0)
  607. err = bpf_object__init_kversion(obj,
  608. data->d_buf,
  609. data->d_size);
  610. else if (strcmp(name, "maps") == 0)
  611. obj->efile.maps_shndx = idx;
  612. else if (sh.sh_type == SHT_SYMTAB) {
  613. if (obj->efile.symbols) {
  614. pr_warning("bpf: multiple SYMTAB in %s\n",
  615. obj->path);
  616. err = -LIBBPF_ERRNO__FORMAT;
  617. } else {
  618. obj->efile.symbols = data;
  619. obj->efile.strtabidx = sh.sh_link;
  620. }
  621. } else if ((sh.sh_type == SHT_PROGBITS) &&
  622. (sh.sh_flags & SHF_EXECINSTR) &&
  623. (data->d_size > 0)) {
  624. err = bpf_object__add_program(obj, data->d_buf,
  625. data->d_size, name, idx);
  626. if (err) {
  627. char errmsg[STRERR_BUFSIZE];
  628. strerror_r(-err, errmsg, sizeof(errmsg));
  629. pr_warning("failed to alloc program %s (%s): %s",
  630. name, obj->path, errmsg);
  631. }
  632. } else if (sh.sh_type == SHT_REL) {
  633. void *reloc = obj->efile.reloc;
  634. int nr_reloc = obj->efile.nr_reloc + 1;
  635. reloc = realloc(reloc,
  636. sizeof(*obj->efile.reloc) * nr_reloc);
  637. if (!reloc) {
  638. pr_warning("realloc failed\n");
  639. err = -ENOMEM;
  640. } else {
  641. int n = nr_reloc - 1;
  642. obj->efile.reloc = reloc;
  643. obj->efile.nr_reloc = nr_reloc;
  644. obj->efile.reloc[n].shdr = sh;
  645. obj->efile.reloc[n].data = data;
  646. }
  647. }
  648. if (err)
  649. goto out;
  650. }
  651. if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
  652. pr_warning("Corrupted ELF file: index of strtab invalid\n");
  653. return LIBBPF_ERRNO__FORMAT;
  654. }
  655. if (obj->efile.maps_shndx >= 0)
  656. err = bpf_object__init_maps(obj);
  657. out:
  658. return err;
  659. }
  660. static struct bpf_program *
  661. bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
  662. {
  663. struct bpf_program *prog;
  664. size_t i;
  665. for (i = 0; i < obj->nr_programs; i++) {
  666. prog = &obj->programs[i];
  667. if (prog->idx == idx)
  668. return prog;
  669. }
  670. return NULL;
  671. }
  672. static int
  673. bpf_program__collect_reloc(struct bpf_program *prog,
  674. size_t nr_maps, GElf_Shdr *shdr,
  675. Elf_Data *data, Elf_Data *symbols,
  676. int maps_shndx, struct bpf_map *maps)
  677. {
  678. int i, nrels;
  679. pr_debug("collecting relocating info for: '%s'\n",
  680. prog->section_name);
  681. nrels = shdr->sh_size / shdr->sh_entsize;
  682. prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
  683. if (!prog->reloc_desc) {
  684. pr_warning("failed to alloc memory in relocation\n");
  685. return -ENOMEM;
  686. }
  687. prog->nr_reloc = nrels;
  688. for (i = 0; i < nrels; i++) {
  689. GElf_Sym sym;
  690. GElf_Rel rel;
  691. unsigned int insn_idx;
  692. struct bpf_insn *insns = prog->insns;
  693. size_t map_idx;
  694. if (!gelf_getrel(data, i, &rel)) {
  695. pr_warning("relocation: failed to get %d reloc\n", i);
  696. return -LIBBPF_ERRNO__FORMAT;
  697. }
  698. if (!gelf_getsym(symbols,
  699. GELF_R_SYM(rel.r_info),
  700. &sym)) {
  701. pr_warning("relocation: symbol %"PRIx64" not found\n",
  702. GELF_R_SYM(rel.r_info));
  703. return -LIBBPF_ERRNO__FORMAT;
  704. }
  705. if (sym.st_shndx != maps_shndx) {
  706. pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
  707. prog->section_name, sym.st_shndx);
  708. return -LIBBPF_ERRNO__RELOC;
  709. }
  710. insn_idx = rel.r_offset / sizeof(struct bpf_insn);
  711. pr_debug("relocation: insn_idx=%u\n", insn_idx);
  712. if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
  713. pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
  714. insn_idx, insns[insn_idx].code);
  715. return -LIBBPF_ERRNO__RELOC;
  716. }
  717. /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
  718. for (map_idx = 0; map_idx < nr_maps; map_idx++) {
  719. if (maps[map_idx].offset == sym.st_value) {
  720. pr_debug("relocation: find map %zd (%s) for insn %u\n",
  721. map_idx, maps[map_idx].name, insn_idx);
  722. break;
  723. }
  724. }
  725. if (map_idx >= nr_maps) {
  726. pr_warning("bpf relocation: map_idx %d large than %d\n",
  727. (int)map_idx, (int)nr_maps - 1);
  728. return -LIBBPF_ERRNO__RELOC;
  729. }
  730. prog->reloc_desc[i].insn_idx = insn_idx;
  731. prog->reloc_desc[i].map_idx = map_idx;
  732. }
  733. return 0;
  734. }
  735. static int
  736. bpf_object__create_maps(struct bpf_object *obj)
  737. {
  738. unsigned int i;
  739. for (i = 0; i < obj->nr_maps; i++) {
  740. struct bpf_map_def *def = &obj->maps[i].def;
  741. int *pfd = &obj->maps[i].fd;
  742. *pfd = bpf_create_map(def->type,
  743. def->key_size,
  744. def->value_size,
  745. def->max_entries,
  746. 0);
  747. if (*pfd < 0) {
  748. size_t j;
  749. int err = *pfd;
  750. pr_warning("failed to create map (name: '%s'): %s\n",
  751. obj->maps[i].name,
  752. strerror(errno));
  753. for (j = 0; j < i; j++)
  754. zclose(obj->maps[j].fd);
  755. return err;
  756. }
  757. pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
  758. }
  759. return 0;
  760. }
  761. static int
  762. bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
  763. {
  764. int i;
  765. if (!prog || !prog->reloc_desc)
  766. return 0;
  767. for (i = 0; i < prog->nr_reloc; i++) {
  768. int insn_idx, map_idx;
  769. struct bpf_insn *insns = prog->insns;
  770. insn_idx = prog->reloc_desc[i].insn_idx;
  771. map_idx = prog->reloc_desc[i].map_idx;
  772. if (insn_idx >= (int)prog->insns_cnt) {
  773. pr_warning("relocation out of range: '%s'\n",
  774. prog->section_name);
  775. return -LIBBPF_ERRNO__RELOC;
  776. }
  777. insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
  778. insns[insn_idx].imm = obj->maps[map_idx].fd;
  779. }
  780. zfree(&prog->reloc_desc);
  781. prog->nr_reloc = 0;
  782. return 0;
  783. }
  784. static int
  785. bpf_object__relocate(struct bpf_object *obj)
  786. {
  787. struct bpf_program *prog;
  788. size_t i;
  789. int err;
  790. for (i = 0; i < obj->nr_programs; i++) {
  791. prog = &obj->programs[i];
  792. err = bpf_program__relocate(prog, obj);
  793. if (err) {
  794. pr_warning("failed to relocate '%s'\n",
  795. prog->section_name);
  796. return err;
  797. }
  798. }
  799. return 0;
  800. }
  801. static int bpf_object__collect_reloc(struct bpf_object *obj)
  802. {
  803. int i, err;
  804. if (!obj_elf_valid(obj)) {
  805. pr_warning("Internal error: elf object is closed\n");
  806. return -LIBBPF_ERRNO__INTERNAL;
  807. }
  808. for (i = 0; i < obj->efile.nr_reloc; i++) {
  809. GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
  810. Elf_Data *data = obj->efile.reloc[i].data;
  811. int idx = shdr->sh_info;
  812. struct bpf_program *prog;
  813. size_t nr_maps = obj->nr_maps;
  814. if (shdr->sh_type != SHT_REL) {
  815. pr_warning("internal error at %d\n", __LINE__);
  816. return -LIBBPF_ERRNO__INTERNAL;
  817. }
  818. prog = bpf_object__find_prog_by_idx(obj, idx);
  819. if (!prog) {
  820. pr_warning("relocation failed: no %d section\n",
  821. idx);
  822. return -LIBBPF_ERRNO__RELOC;
  823. }
  824. err = bpf_program__collect_reloc(prog, nr_maps,
  825. shdr, data,
  826. obj->efile.symbols,
  827. obj->efile.maps_shndx,
  828. obj->maps);
  829. if (err)
  830. return err;
  831. }
  832. return 0;
  833. }
  834. static int
  835. load_program(enum bpf_prog_type type, struct bpf_insn *insns,
  836. int insns_cnt, char *license, u32 kern_version, int *pfd)
  837. {
  838. int ret;
  839. char *log_buf;
  840. if (!insns || !insns_cnt)
  841. return -EINVAL;
  842. log_buf = malloc(BPF_LOG_BUF_SIZE);
  843. if (!log_buf)
  844. pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
  845. ret = bpf_load_program(type, insns, insns_cnt, license,
  846. kern_version, log_buf, BPF_LOG_BUF_SIZE);
  847. if (ret >= 0) {
  848. *pfd = ret;
  849. ret = 0;
  850. goto out;
  851. }
  852. ret = -LIBBPF_ERRNO__LOAD;
  853. pr_warning("load bpf program failed: %s\n", strerror(errno));
  854. if (log_buf && log_buf[0] != '\0') {
  855. ret = -LIBBPF_ERRNO__VERIFY;
  856. pr_warning("-- BEGIN DUMP LOG ---\n");
  857. pr_warning("\n%s\n", log_buf);
  858. pr_warning("-- END LOG --\n");
  859. } else if (insns_cnt >= BPF_MAXINSNS) {
  860. pr_warning("Program too large (%d insns), at most %d insns\n",
  861. insns_cnt, BPF_MAXINSNS);
  862. ret = -LIBBPF_ERRNO__PROG2BIG;
  863. } else {
  864. /* Wrong program type? */
  865. if (type != BPF_PROG_TYPE_KPROBE) {
  866. int fd;
  867. fd = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
  868. insns_cnt, license, kern_version,
  869. NULL, 0);
  870. if (fd >= 0) {
  871. close(fd);
  872. ret = -LIBBPF_ERRNO__PROGTYPE;
  873. goto out;
  874. }
  875. }
  876. if (log_buf)
  877. ret = -LIBBPF_ERRNO__KVER;
  878. }
  879. out:
  880. free(log_buf);
  881. return ret;
  882. }
  883. static int
  884. bpf_program__load(struct bpf_program *prog,
  885. char *license, u32 kern_version)
  886. {
  887. int err = 0, fd, i;
  888. if (prog->instances.nr < 0 || !prog->instances.fds) {
  889. if (prog->preprocessor) {
  890. pr_warning("Internal error: can't load program '%s'\n",
  891. prog->section_name);
  892. return -LIBBPF_ERRNO__INTERNAL;
  893. }
  894. prog->instances.fds = malloc(sizeof(int));
  895. if (!prog->instances.fds) {
  896. pr_warning("Not enough memory for BPF fds\n");
  897. return -ENOMEM;
  898. }
  899. prog->instances.nr = 1;
  900. prog->instances.fds[0] = -1;
  901. }
  902. if (!prog->preprocessor) {
  903. if (prog->instances.nr != 1) {
  904. pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
  905. prog->section_name, prog->instances.nr);
  906. }
  907. err = load_program(prog->type, prog->insns, prog->insns_cnt,
  908. license, kern_version, &fd);
  909. if (!err)
  910. prog->instances.fds[0] = fd;
  911. goto out;
  912. }
  913. for (i = 0; i < prog->instances.nr; i++) {
  914. struct bpf_prog_prep_result result;
  915. bpf_program_prep_t preprocessor = prog->preprocessor;
  916. bzero(&result, sizeof(result));
  917. err = preprocessor(prog, i, prog->insns,
  918. prog->insns_cnt, &result);
  919. if (err) {
  920. pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
  921. i, prog->section_name);
  922. goto out;
  923. }
  924. if (!result.new_insn_ptr || !result.new_insn_cnt) {
  925. pr_debug("Skip loading the %dth instance of program '%s'\n",
  926. i, prog->section_name);
  927. prog->instances.fds[i] = -1;
  928. if (result.pfd)
  929. *result.pfd = -1;
  930. continue;
  931. }
  932. err = load_program(prog->type, result.new_insn_ptr,
  933. result.new_insn_cnt,
  934. license, kern_version, &fd);
  935. if (err) {
  936. pr_warning("Loading the %dth instance of program '%s' failed\n",
  937. i, prog->section_name);
  938. goto out;
  939. }
  940. if (result.pfd)
  941. *result.pfd = fd;
  942. prog->instances.fds[i] = fd;
  943. }
  944. out:
  945. if (err)
  946. pr_warning("failed to load program '%s'\n",
  947. prog->section_name);
  948. zfree(&prog->insns);
  949. prog->insns_cnt = 0;
  950. return err;
  951. }
  952. static int
  953. bpf_object__load_progs(struct bpf_object *obj)
  954. {
  955. size_t i;
  956. int err;
  957. for (i = 0; i < obj->nr_programs; i++) {
  958. err = bpf_program__load(&obj->programs[i],
  959. obj->license,
  960. obj->kern_version);
  961. if (err)
  962. return err;
  963. }
  964. return 0;
  965. }
  966. static int bpf_object__validate(struct bpf_object *obj)
  967. {
  968. if (obj->kern_version == 0) {
  969. pr_warning("%s doesn't provide kernel version\n",
  970. obj->path);
  971. return -LIBBPF_ERRNO__KVERSION;
  972. }
  973. return 0;
  974. }
  975. static struct bpf_object *
  976. __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
  977. {
  978. struct bpf_object *obj;
  979. int err;
  980. if (elf_version(EV_CURRENT) == EV_NONE) {
  981. pr_warning("failed to init libelf for %s\n", path);
  982. return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
  983. }
  984. obj = bpf_object__new(path, obj_buf, obj_buf_sz);
  985. if (IS_ERR(obj))
  986. return obj;
  987. CHECK_ERR(bpf_object__elf_init(obj), err, out);
  988. CHECK_ERR(bpf_object__check_endianness(obj), err, out);
  989. CHECK_ERR(bpf_object__elf_collect(obj), err, out);
  990. CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
  991. CHECK_ERR(bpf_object__validate(obj), err, out);
  992. bpf_object__elf_finish(obj);
  993. return obj;
  994. out:
  995. bpf_object__close(obj);
  996. return ERR_PTR(err);
  997. }
  998. struct bpf_object *bpf_object__open(const char *path)
  999. {
  1000. /* param validation */
  1001. if (!path)
  1002. return NULL;
  1003. pr_debug("loading %s\n", path);
  1004. return __bpf_object__open(path, NULL, 0);
  1005. }
  1006. struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  1007. size_t obj_buf_sz,
  1008. const char *name)
  1009. {
  1010. char tmp_name[64];
  1011. /* param validation */
  1012. if (!obj_buf || obj_buf_sz <= 0)
  1013. return NULL;
  1014. if (!name) {
  1015. snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
  1016. (unsigned long)obj_buf,
  1017. (unsigned long)obj_buf_sz);
  1018. tmp_name[sizeof(tmp_name) - 1] = '\0';
  1019. name = tmp_name;
  1020. }
  1021. pr_debug("loading object '%s' from buffer\n",
  1022. name);
  1023. return __bpf_object__open(name, obj_buf, obj_buf_sz);
  1024. }
  1025. int bpf_object__unload(struct bpf_object *obj)
  1026. {
  1027. size_t i;
  1028. if (!obj)
  1029. return -EINVAL;
  1030. for (i = 0; i < obj->nr_maps; i++)
  1031. zclose(obj->maps[i].fd);
  1032. for (i = 0; i < obj->nr_programs; i++)
  1033. bpf_program__unload(&obj->programs[i]);
  1034. return 0;
  1035. }
  1036. int bpf_object__load(struct bpf_object *obj)
  1037. {
  1038. int err;
  1039. if (!obj)
  1040. return -EINVAL;
  1041. if (obj->loaded) {
  1042. pr_warning("object should not be loaded twice\n");
  1043. return -EINVAL;
  1044. }
  1045. obj->loaded = true;
  1046. CHECK_ERR(bpf_object__create_maps(obj), err, out);
  1047. CHECK_ERR(bpf_object__relocate(obj), err, out);
  1048. CHECK_ERR(bpf_object__load_progs(obj), err, out);
  1049. return 0;
  1050. out:
  1051. bpf_object__unload(obj);
  1052. pr_warning("failed to load object '%s'\n", obj->path);
  1053. return err;
  1054. }
  1055. static int check_path(const char *path)
  1056. {
  1057. struct statfs st_fs;
  1058. char *dname, *dir;
  1059. int err = 0;
  1060. if (path == NULL)
  1061. return -EINVAL;
  1062. dname = strdup(path);
  1063. if (dname == NULL)
  1064. return -ENOMEM;
  1065. dir = dirname(dname);
  1066. if (statfs(dir, &st_fs)) {
  1067. pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
  1068. err = -errno;
  1069. }
  1070. free(dname);
  1071. if (!err && st_fs.f_type != BPF_FS_MAGIC) {
  1072. pr_warning("specified path %s is not on BPF FS\n", path);
  1073. err = -EINVAL;
  1074. }
  1075. return err;
  1076. }
  1077. int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
  1078. int instance)
  1079. {
  1080. int err;
  1081. err = check_path(path);
  1082. if (err)
  1083. return err;
  1084. if (prog == NULL) {
  1085. pr_warning("invalid program pointer\n");
  1086. return -EINVAL;
  1087. }
  1088. if (instance < 0 || instance >= prog->instances.nr) {
  1089. pr_warning("invalid prog instance %d of prog %s (max %d)\n",
  1090. instance, prog->section_name, prog->instances.nr);
  1091. return -EINVAL;
  1092. }
  1093. if (bpf_obj_pin(prog->instances.fds[instance], path)) {
  1094. pr_warning("failed to pin program: %s\n", strerror(errno));
  1095. return -errno;
  1096. }
  1097. pr_debug("pinned program '%s'\n", path);
  1098. return 0;
  1099. }
  1100. static int make_dir(const char *path)
  1101. {
  1102. int err = 0;
  1103. if (mkdir(path, 0700) && errno != EEXIST)
  1104. err = -errno;
  1105. if (err)
  1106. pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
  1107. return err;
  1108. }
  1109. int bpf_program__pin(struct bpf_program *prog, const char *path)
  1110. {
  1111. int i, err;
  1112. err = check_path(path);
  1113. if (err)
  1114. return err;
  1115. if (prog == NULL) {
  1116. pr_warning("invalid program pointer\n");
  1117. return -EINVAL;
  1118. }
  1119. if (prog->instances.nr <= 0) {
  1120. pr_warning("no instances of prog %s to pin\n",
  1121. prog->section_name);
  1122. return -EINVAL;
  1123. }
  1124. err = make_dir(path);
  1125. if (err)
  1126. return err;
  1127. for (i = 0; i < prog->instances.nr; i++) {
  1128. char buf[PATH_MAX];
  1129. int len;
  1130. len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
  1131. if (len < 0)
  1132. return -EINVAL;
  1133. else if (len >= PATH_MAX)
  1134. return -ENAMETOOLONG;
  1135. err = bpf_program__pin_instance(prog, buf, i);
  1136. if (err)
  1137. return err;
  1138. }
  1139. return 0;
  1140. }
  1141. int bpf_map__pin(struct bpf_map *map, const char *path)
  1142. {
  1143. int err;
  1144. err = check_path(path);
  1145. if (err)
  1146. return err;
  1147. if (map == NULL) {
  1148. pr_warning("invalid map pointer\n");
  1149. return -EINVAL;
  1150. }
  1151. if (bpf_obj_pin(map->fd, path)) {
  1152. pr_warning("failed to pin map: %s\n", strerror(errno));
  1153. return -errno;
  1154. }
  1155. pr_debug("pinned map '%s'\n", path);
  1156. return 0;
  1157. }
  1158. int bpf_object__pin(struct bpf_object *obj, const char *path)
  1159. {
  1160. struct bpf_program *prog;
  1161. struct bpf_map *map;
  1162. int err;
  1163. if (!obj)
  1164. return -ENOENT;
  1165. if (!obj->loaded) {
  1166. pr_warning("object not yet loaded; load it first\n");
  1167. return -ENOENT;
  1168. }
  1169. err = make_dir(path);
  1170. if (err)
  1171. return err;
  1172. bpf_map__for_each(map, obj) {
  1173. char buf[PATH_MAX];
  1174. int len;
  1175. len = snprintf(buf, PATH_MAX, "%s/%s", path,
  1176. bpf_map__name(map));
  1177. if (len < 0)
  1178. return -EINVAL;
  1179. else if (len >= PATH_MAX)
  1180. return -ENAMETOOLONG;
  1181. err = bpf_map__pin(map, buf);
  1182. if (err)
  1183. return err;
  1184. }
  1185. bpf_object__for_each_program(prog, obj) {
  1186. char buf[PATH_MAX];
  1187. int len;
  1188. len = snprintf(buf, PATH_MAX, "%s/%s", path,
  1189. prog->section_name);
  1190. if (len < 0)
  1191. return -EINVAL;
  1192. else if (len >= PATH_MAX)
  1193. return -ENAMETOOLONG;
  1194. err = bpf_program__pin(prog, buf);
  1195. if (err)
  1196. return err;
  1197. }
  1198. return 0;
  1199. }
  1200. void bpf_object__close(struct bpf_object *obj)
  1201. {
  1202. size_t i;
  1203. if (!obj)
  1204. return;
  1205. if (obj->clear_priv)
  1206. obj->clear_priv(obj, obj->priv);
  1207. bpf_object__elf_finish(obj);
  1208. bpf_object__unload(obj);
  1209. for (i = 0; i < obj->nr_maps; i++) {
  1210. zfree(&obj->maps[i].name);
  1211. if (obj->maps[i].clear_priv)
  1212. obj->maps[i].clear_priv(&obj->maps[i],
  1213. obj->maps[i].priv);
  1214. obj->maps[i].priv = NULL;
  1215. obj->maps[i].clear_priv = NULL;
  1216. }
  1217. zfree(&obj->maps);
  1218. obj->nr_maps = 0;
  1219. if (obj->programs && obj->nr_programs) {
  1220. for (i = 0; i < obj->nr_programs; i++)
  1221. bpf_program__exit(&obj->programs[i]);
  1222. }
  1223. zfree(&obj->programs);
  1224. list_del(&obj->list);
  1225. free(obj);
  1226. }
  1227. struct bpf_object *
  1228. bpf_object__next(struct bpf_object *prev)
  1229. {
  1230. struct bpf_object *next;
  1231. if (!prev)
  1232. next = list_first_entry(&bpf_objects_list,
  1233. struct bpf_object,
  1234. list);
  1235. else
  1236. next = list_next_entry(prev, list);
  1237. /* Empty list is noticed here so don't need checking on entry. */
  1238. if (&next->list == &bpf_objects_list)
  1239. return NULL;
  1240. return next;
  1241. }
  1242. const char *bpf_object__name(struct bpf_object *obj)
  1243. {
  1244. return obj ? obj->path : ERR_PTR(-EINVAL);
  1245. }
  1246. unsigned int bpf_object__kversion(struct bpf_object *obj)
  1247. {
  1248. return obj ? obj->kern_version : 0;
  1249. }
  1250. int bpf_object__set_priv(struct bpf_object *obj, void *priv,
  1251. bpf_object_clear_priv_t clear_priv)
  1252. {
  1253. if (obj->priv && obj->clear_priv)
  1254. obj->clear_priv(obj, obj->priv);
  1255. obj->priv = priv;
  1256. obj->clear_priv = clear_priv;
  1257. return 0;
  1258. }
  1259. void *bpf_object__priv(struct bpf_object *obj)
  1260. {
  1261. return obj ? obj->priv : ERR_PTR(-EINVAL);
  1262. }
  1263. struct bpf_program *
  1264. bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
  1265. {
  1266. size_t idx;
  1267. if (!obj->programs)
  1268. return NULL;
  1269. /* First handler */
  1270. if (prev == NULL)
  1271. return &obj->programs[0];
  1272. if (prev->obj != obj) {
  1273. pr_warning("error: program handler doesn't match object\n");
  1274. return NULL;
  1275. }
  1276. idx = (prev - obj->programs) + 1;
  1277. if (idx >= obj->nr_programs)
  1278. return NULL;
  1279. return &obj->programs[idx];
  1280. }
  1281. int bpf_program__set_priv(struct bpf_program *prog, void *priv,
  1282. bpf_program_clear_priv_t clear_priv)
  1283. {
  1284. if (prog->priv && prog->clear_priv)
  1285. prog->clear_priv(prog, prog->priv);
  1286. prog->priv = priv;
  1287. prog->clear_priv = clear_priv;
  1288. return 0;
  1289. }
  1290. void *bpf_program__priv(struct bpf_program *prog)
  1291. {
  1292. return prog ? prog->priv : ERR_PTR(-EINVAL);
  1293. }
  1294. const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
  1295. {
  1296. const char *title;
  1297. title = prog->section_name;
  1298. if (needs_copy) {
  1299. title = strdup(title);
  1300. if (!title) {
  1301. pr_warning("failed to strdup program title\n");
  1302. return ERR_PTR(-ENOMEM);
  1303. }
  1304. }
  1305. return title;
  1306. }
  1307. int bpf_program__fd(struct bpf_program *prog)
  1308. {
  1309. return bpf_program__nth_fd(prog, 0);
  1310. }
  1311. int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
  1312. bpf_program_prep_t prep)
  1313. {
  1314. int *instances_fds;
  1315. if (nr_instances <= 0 || !prep)
  1316. return -EINVAL;
  1317. if (prog->instances.nr > 0 || prog->instances.fds) {
  1318. pr_warning("Can't set pre-processor after loading\n");
  1319. return -EINVAL;
  1320. }
  1321. instances_fds = malloc(sizeof(int) * nr_instances);
  1322. if (!instances_fds) {
  1323. pr_warning("alloc memory failed for fds\n");
  1324. return -ENOMEM;
  1325. }
  1326. /* fill all fd with -1 */
  1327. memset(instances_fds, -1, sizeof(int) * nr_instances);
  1328. prog->instances.nr = nr_instances;
  1329. prog->instances.fds = instances_fds;
  1330. prog->preprocessor = prep;
  1331. return 0;
  1332. }
  1333. int bpf_program__nth_fd(struct bpf_program *prog, int n)
  1334. {
  1335. int fd;
  1336. if (n >= prog->instances.nr || n < 0) {
  1337. pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
  1338. n, prog->section_name, prog->instances.nr);
  1339. return -EINVAL;
  1340. }
  1341. fd = prog->instances.fds[n];
  1342. if (fd < 0) {
  1343. pr_warning("%dth instance of program '%s' is invalid\n",
  1344. n, prog->section_name);
  1345. return -ENOENT;
  1346. }
  1347. return fd;
  1348. }
  1349. void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
  1350. {
  1351. prog->type = type;
  1352. }
  1353. static bool bpf_program__is_type(struct bpf_program *prog,
  1354. enum bpf_prog_type type)
  1355. {
  1356. return prog ? (prog->type == type) : false;
  1357. }
  1358. #define BPF_PROG_TYPE_FNS(NAME, TYPE) \
  1359. int bpf_program__set_##NAME(struct bpf_program *prog) \
  1360. { \
  1361. if (!prog) \
  1362. return -EINVAL; \
  1363. bpf_program__set_type(prog, TYPE); \
  1364. return 0; \
  1365. } \
  1366. \
  1367. bool bpf_program__is_##NAME(struct bpf_program *prog) \
  1368. { \
  1369. return bpf_program__is_type(prog, TYPE); \
  1370. } \
  1371. BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
  1372. BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
  1373. BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
  1374. BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
  1375. BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
  1376. BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
  1377. BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
  1378. int bpf_map__fd(struct bpf_map *map)
  1379. {
  1380. return map ? map->fd : -EINVAL;
  1381. }
  1382. const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
  1383. {
  1384. return map ? &map->def : ERR_PTR(-EINVAL);
  1385. }
  1386. const char *bpf_map__name(struct bpf_map *map)
  1387. {
  1388. return map ? map->name : NULL;
  1389. }
  1390. int bpf_map__set_priv(struct bpf_map *map, void *priv,
  1391. bpf_map_clear_priv_t clear_priv)
  1392. {
  1393. if (!map)
  1394. return -EINVAL;
  1395. if (map->priv) {
  1396. if (map->clear_priv)
  1397. map->clear_priv(map, map->priv);
  1398. }
  1399. map->priv = priv;
  1400. map->clear_priv = clear_priv;
  1401. return 0;
  1402. }
  1403. void *bpf_map__priv(struct bpf_map *map)
  1404. {
  1405. return map ? map->priv : ERR_PTR(-EINVAL);
  1406. }
  1407. struct bpf_map *
  1408. bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
  1409. {
  1410. size_t idx;
  1411. struct bpf_map *s, *e;
  1412. if (!obj || !obj->maps)
  1413. return NULL;
  1414. s = obj->maps;
  1415. e = obj->maps + obj->nr_maps;
  1416. if (prev == NULL)
  1417. return s;
  1418. if ((prev < s) || (prev >= e)) {
  1419. pr_warning("error in %s: map handler doesn't belong to object\n",
  1420. __func__);
  1421. return NULL;
  1422. }
  1423. idx = (prev - obj->maps) + 1;
  1424. if (idx >= obj->nr_maps)
  1425. return NULL;
  1426. return &obj->maps[idx];
  1427. }
  1428. struct bpf_map *
  1429. bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
  1430. {
  1431. struct bpf_map *pos;
  1432. bpf_map__for_each(pos, obj) {
  1433. if (pos->name && !strcmp(pos->name, name))
  1434. return pos;
  1435. }
  1436. return NULL;
  1437. }
  1438. struct bpf_map *
  1439. bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
  1440. {
  1441. int i;
  1442. for (i = 0; i < obj->nr_maps; i++) {
  1443. if (obj->maps[i].offset == offset)
  1444. return &obj->maps[i];
  1445. }
  1446. return ERR_PTR(-ENOENT);
  1447. }
  1448. long libbpf_get_error(const void *ptr)
  1449. {
  1450. if (IS_ERR(ptr))
  1451. return PTR_ERR(ptr);
  1452. return 0;
  1453. }