libbpf.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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: %s\n",
  751. strerror(errno));
  752. for (j = 0; j < i; j++)
  753. zclose(obj->maps[j].fd);
  754. return err;
  755. }
  756. pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
  757. }
  758. return 0;
  759. }
  760. static int
  761. bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
  762. {
  763. int i;
  764. if (!prog || !prog->reloc_desc)
  765. return 0;
  766. for (i = 0; i < prog->nr_reloc; i++) {
  767. int insn_idx, map_idx;
  768. struct bpf_insn *insns = prog->insns;
  769. insn_idx = prog->reloc_desc[i].insn_idx;
  770. map_idx = prog->reloc_desc[i].map_idx;
  771. if (insn_idx >= (int)prog->insns_cnt) {
  772. pr_warning("relocation out of range: '%s'\n",
  773. prog->section_name);
  774. return -LIBBPF_ERRNO__RELOC;
  775. }
  776. insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
  777. insns[insn_idx].imm = obj->maps[map_idx].fd;
  778. }
  779. zfree(&prog->reloc_desc);
  780. prog->nr_reloc = 0;
  781. return 0;
  782. }
  783. static int
  784. bpf_object__relocate(struct bpf_object *obj)
  785. {
  786. struct bpf_program *prog;
  787. size_t i;
  788. int err;
  789. for (i = 0; i < obj->nr_programs; i++) {
  790. prog = &obj->programs[i];
  791. err = bpf_program__relocate(prog, obj);
  792. if (err) {
  793. pr_warning("failed to relocate '%s'\n",
  794. prog->section_name);
  795. return err;
  796. }
  797. }
  798. return 0;
  799. }
  800. static int bpf_object__collect_reloc(struct bpf_object *obj)
  801. {
  802. int i, err;
  803. if (!obj_elf_valid(obj)) {
  804. pr_warning("Internal error: elf object is closed\n");
  805. return -LIBBPF_ERRNO__INTERNAL;
  806. }
  807. for (i = 0; i < obj->efile.nr_reloc; i++) {
  808. GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
  809. Elf_Data *data = obj->efile.reloc[i].data;
  810. int idx = shdr->sh_info;
  811. struct bpf_program *prog;
  812. size_t nr_maps = obj->nr_maps;
  813. if (shdr->sh_type != SHT_REL) {
  814. pr_warning("internal error at %d\n", __LINE__);
  815. return -LIBBPF_ERRNO__INTERNAL;
  816. }
  817. prog = bpf_object__find_prog_by_idx(obj, idx);
  818. if (!prog) {
  819. pr_warning("relocation failed: no %d section\n",
  820. idx);
  821. return -LIBBPF_ERRNO__RELOC;
  822. }
  823. err = bpf_program__collect_reloc(prog, nr_maps,
  824. shdr, data,
  825. obj->efile.symbols,
  826. obj->efile.maps_shndx,
  827. obj->maps);
  828. if (err)
  829. return err;
  830. }
  831. return 0;
  832. }
  833. static int
  834. load_program(enum bpf_prog_type type, struct bpf_insn *insns,
  835. int insns_cnt, char *license, u32 kern_version, int *pfd)
  836. {
  837. int ret;
  838. char *log_buf;
  839. if (!insns || !insns_cnt)
  840. return -EINVAL;
  841. log_buf = malloc(BPF_LOG_BUF_SIZE);
  842. if (!log_buf)
  843. pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
  844. ret = bpf_load_program(type, insns, insns_cnt, license,
  845. kern_version, log_buf, BPF_LOG_BUF_SIZE);
  846. if (ret >= 0) {
  847. *pfd = ret;
  848. ret = 0;
  849. goto out;
  850. }
  851. ret = -LIBBPF_ERRNO__LOAD;
  852. pr_warning("load bpf program failed: %s\n", strerror(errno));
  853. if (log_buf && log_buf[0] != '\0') {
  854. ret = -LIBBPF_ERRNO__VERIFY;
  855. pr_warning("-- BEGIN DUMP LOG ---\n");
  856. pr_warning("\n%s\n", log_buf);
  857. pr_warning("-- END LOG --\n");
  858. } else if (insns_cnt >= BPF_MAXINSNS) {
  859. pr_warning("Program too large (%d insns), at most %d insns\n",
  860. insns_cnt, BPF_MAXINSNS);
  861. ret = -LIBBPF_ERRNO__PROG2BIG;
  862. } else {
  863. /* Wrong program type? */
  864. if (type != BPF_PROG_TYPE_KPROBE) {
  865. int fd;
  866. fd = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
  867. insns_cnt, license, kern_version,
  868. NULL, 0);
  869. if (fd >= 0) {
  870. close(fd);
  871. ret = -LIBBPF_ERRNO__PROGTYPE;
  872. goto out;
  873. }
  874. }
  875. if (log_buf)
  876. ret = -LIBBPF_ERRNO__KVER;
  877. }
  878. out:
  879. free(log_buf);
  880. return ret;
  881. }
  882. static int
  883. bpf_program__load(struct bpf_program *prog,
  884. char *license, u32 kern_version)
  885. {
  886. int err = 0, fd, i;
  887. if (prog->instances.nr < 0 || !prog->instances.fds) {
  888. if (prog->preprocessor) {
  889. pr_warning("Internal error: can't load program '%s'\n",
  890. prog->section_name);
  891. return -LIBBPF_ERRNO__INTERNAL;
  892. }
  893. prog->instances.fds = malloc(sizeof(int));
  894. if (!prog->instances.fds) {
  895. pr_warning("Not enough memory for BPF fds\n");
  896. return -ENOMEM;
  897. }
  898. prog->instances.nr = 1;
  899. prog->instances.fds[0] = -1;
  900. }
  901. if (!prog->preprocessor) {
  902. if (prog->instances.nr != 1) {
  903. pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
  904. prog->section_name, prog->instances.nr);
  905. }
  906. err = load_program(prog->type, prog->insns, prog->insns_cnt,
  907. license, kern_version, &fd);
  908. if (!err)
  909. prog->instances.fds[0] = fd;
  910. goto out;
  911. }
  912. for (i = 0; i < prog->instances.nr; i++) {
  913. struct bpf_prog_prep_result result;
  914. bpf_program_prep_t preprocessor = prog->preprocessor;
  915. bzero(&result, sizeof(result));
  916. err = preprocessor(prog, i, prog->insns,
  917. prog->insns_cnt, &result);
  918. if (err) {
  919. pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
  920. i, prog->section_name);
  921. goto out;
  922. }
  923. if (!result.new_insn_ptr || !result.new_insn_cnt) {
  924. pr_debug("Skip loading the %dth instance of program '%s'\n",
  925. i, prog->section_name);
  926. prog->instances.fds[i] = -1;
  927. if (result.pfd)
  928. *result.pfd = -1;
  929. continue;
  930. }
  931. err = load_program(prog->type, result.new_insn_ptr,
  932. result.new_insn_cnt,
  933. license, kern_version, &fd);
  934. if (err) {
  935. pr_warning("Loading the %dth instance of program '%s' failed\n",
  936. i, prog->section_name);
  937. goto out;
  938. }
  939. if (result.pfd)
  940. *result.pfd = fd;
  941. prog->instances.fds[i] = fd;
  942. }
  943. out:
  944. if (err)
  945. pr_warning("failed to load program '%s'\n",
  946. prog->section_name);
  947. zfree(&prog->insns);
  948. prog->insns_cnt = 0;
  949. return err;
  950. }
  951. static int
  952. bpf_object__load_progs(struct bpf_object *obj)
  953. {
  954. size_t i;
  955. int err;
  956. for (i = 0; i < obj->nr_programs; i++) {
  957. err = bpf_program__load(&obj->programs[i],
  958. obj->license,
  959. obj->kern_version);
  960. if (err)
  961. return err;
  962. }
  963. return 0;
  964. }
  965. static int bpf_object__validate(struct bpf_object *obj)
  966. {
  967. if (obj->kern_version == 0) {
  968. pr_warning("%s doesn't provide kernel version\n",
  969. obj->path);
  970. return -LIBBPF_ERRNO__KVERSION;
  971. }
  972. return 0;
  973. }
  974. static struct bpf_object *
  975. __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
  976. {
  977. struct bpf_object *obj;
  978. int err;
  979. if (elf_version(EV_CURRENT) == EV_NONE) {
  980. pr_warning("failed to init libelf for %s\n", path);
  981. return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
  982. }
  983. obj = bpf_object__new(path, obj_buf, obj_buf_sz);
  984. if (IS_ERR(obj))
  985. return obj;
  986. CHECK_ERR(bpf_object__elf_init(obj), err, out);
  987. CHECK_ERR(bpf_object__check_endianness(obj), err, out);
  988. CHECK_ERR(bpf_object__elf_collect(obj), err, out);
  989. CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
  990. CHECK_ERR(bpf_object__validate(obj), err, out);
  991. bpf_object__elf_finish(obj);
  992. return obj;
  993. out:
  994. bpf_object__close(obj);
  995. return ERR_PTR(err);
  996. }
  997. struct bpf_object *bpf_object__open(const char *path)
  998. {
  999. /* param validation */
  1000. if (!path)
  1001. return NULL;
  1002. pr_debug("loading %s\n", path);
  1003. return __bpf_object__open(path, NULL, 0);
  1004. }
  1005. struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  1006. size_t obj_buf_sz,
  1007. const char *name)
  1008. {
  1009. char tmp_name[64];
  1010. /* param validation */
  1011. if (!obj_buf || obj_buf_sz <= 0)
  1012. return NULL;
  1013. if (!name) {
  1014. snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
  1015. (unsigned long)obj_buf,
  1016. (unsigned long)obj_buf_sz);
  1017. tmp_name[sizeof(tmp_name) - 1] = '\0';
  1018. name = tmp_name;
  1019. }
  1020. pr_debug("loading object '%s' from buffer\n",
  1021. name);
  1022. return __bpf_object__open(name, obj_buf, obj_buf_sz);
  1023. }
  1024. int bpf_object__unload(struct bpf_object *obj)
  1025. {
  1026. size_t i;
  1027. if (!obj)
  1028. return -EINVAL;
  1029. for (i = 0; i < obj->nr_maps; i++)
  1030. zclose(obj->maps[i].fd);
  1031. for (i = 0; i < obj->nr_programs; i++)
  1032. bpf_program__unload(&obj->programs[i]);
  1033. return 0;
  1034. }
  1035. int bpf_object__load(struct bpf_object *obj)
  1036. {
  1037. int err;
  1038. if (!obj)
  1039. return -EINVAL;
  1040. if (obj->loaded) {
  1041. pr_warning("object should not be loaded twice\n");
  1042. return -EINVAL;
  1043. }
  1044. obj->loaded = true;
  1045. CHECK_ERR(bpf_object__create_maps(obj), err, out);
  1046. CHECK_ERR(bpf_object__relocate(obj), err, out);
  1047. CHECK_ERR(bpf_object__load_progs(obj), err, out);
  1048. return 0;
  1049. out:
  1050. bpf_object__unload(obj);
  1051. pr_warning("failed to load object '%s'\n", obj->path);
  1052. return err;
  1053. }
  1054. static int check_path(const char *path)
  1055. {
  1056. struct statfs st_fs;
  1057. char *dname, *dir;
  1058. int err = 0;
  1059. if (path == NULL)
  1060. return -EINVAL;
  1061. dname = strdup(path);
  1062. if (dname == NULL)
  1063. return -ENOMEM;
  1064. dir = dirname(dname);
  1065. if (statfs(dir, &st_fs)) {
  1066. pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
  1067. err = -errno;
  1068. }
  1069. free(dname);
  1070. if (!err && st_fs.f_type != BPF_FS_MAGIC) {
  1071. pr_warning("specified path %s is not on BPF FS\n", path);
  1072. err = -EINVAL;
  1073. }
  1074. return err;
  1075. }
  1076. int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
  1077. int instance)
  1078. {
  1079. int err;
  1080. err = check_path(path);
  1081. if (err)
  1082. return err;
  1083. if (prog == NULL) {
  1084. pr_warning("invalid program pointer\n");
  1085. return -EINVAL;
  1086. }
  1087. if (instance < 0 || instance >= prog->instances.nr) {
  1088. pr_warning("invalid prog instance %d of prog %s (max %d)\n",
  1089. instance, prog->section_name, prog->instances.nr);
  1090. return -EINVAL;
  1091. }
  1092. if (bpf_obj_pin(prog->instances.fds[instance], path)) {
  1093. pr_warning("failed to pin program: %s\n", strerror(errno));
  1094. return -errno;
  1095. }
  1096. pr_debug("pinned program '%s'\n", path);
  1097. return 0;
  1098. }
  1099. static int make_dir(const char *path)
  1100. {
  1101. int err = 0;
  1102. if (mkdir(path, 0700) && errno != EEXIST)
  1103. err = -errno;
  1104. if (err)
  1105. pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
  1106. return err;
  1107. }
  1108. int bpf_program__pin(struct bpf_program *prog, const char *path)
  1109. {
  1110. int i, err;
  1111. err = check_path(path);
  1112. if (err)
  1113. return err;
  1114. if (prog == NULL) {
  1115. pr_warning("invalid program pointer\n");
  1116. return -EINVAL;
  1117. }
  1118. if (prog->instances.nr <= 0) {
  1119. pr_warning("no instances of prog %s to pin\n",
  1120. prog->section_name);
  1121. return -EINVAL;
  1122. }
  1123. err = make_dir(path);
  1124. if (err)
  1125. return err;
  1126. for (i = 0; i < prog->instances.nr; i++) {
  1127. char buf[PATH_MAX];
  1128. int len;
  1129. len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
  1130. if (len < 0)
  1131. return -EINVAL;
  1132. else if (len >= PATH_MAX)
  1133. return -ENAMETOOLONG;
  1134. err = bpf_program__pin_instance(prog, buf, i);
  1135. if (err)
  1136. return err;
  1137. }
  1138. return 0;
  1139. }
  1140. int bpf_map__pin(struct bpf_map *map, const char *path)
  1141. {
  1142. int err;
  1143. err = check_path(path);
  1144. if (err)
  1145. return err;
  1146. if (map == NULL) {
  1147. pr_warning("invalid map pointer\n");
  1148. return -EINVAL;
  1149. }
  1150. if (bpf_obj_pin(map->fd, path)) {
  1151. pr_warning("failed to pin map: %s\n", strerror(errno));
  1152. return -errno;
  1153. }
  1154. pr_debug("pinned map '%s'\n", path);
  1155. return 0;
  1156. }
  1157. int bpf_object__pin(struct bpf_object *obj, const char *path)
  1158. {
  1159. struct bpf_program *prog;
  1160. struct bpf_map *map;
  1161. int err;
  1162. if (!obj)
  1163. return -ENOENT;
  1164. if (!obj->loaded) {
  1165. pr_warning("object not yet loaded; load it first\n");
  1166. return -ENOENT;
  1167. }
  1168. err = make_dir(path);
  1169. if (err)
  1170. return err;
  1171. bpf_map__for_each(map, obj) {
  1172. char buf[PATH_MAX];
  1173. int len;
  1174. len = snprintf(buf, PATH_MAX, "%s/%s", path,
  1175. bpf_map__name(map));
  1176. if (len < 0)
  1177. return -EINVAL;
  1178. else if (len >= PATH_MAX)
  1179. return -ENAMETOOLONG;
  1180. err = bpf_map__pin(map, buf);
  1181. if (err)
  1182. return err;
  1183. }
  1184. bpf_object__for_each_program(prog, obj) {
  1185. char buf[PATH_MAX];
  1186. int len;
  1187. len = snprintf(buf, PATH_MAX, "%s/%s", path,
  1188. prog->section_name);
  1189. if (len < 0)
  1190. return -EINVAL;
  1191. else if (len >= PATH_MAX)
  1192. return -ENAMETOOLONG;
  1193. err = bpf_program__pin(prog, buf);
  1194. if (err)
  1195. return err;
  1196. }
  1197. return 0;
  1198. }
  1199. void bpf_object__close(struct bpf_object *obj)
  1200. {
  1201. size_t i;
  1202. if (!obj)
  1203. return;
  1204. if (obj->clear_priv)
  1205. obj->clear_priv(obj, obj->priv);
  1206. bpf_object__elf_finish(obj);
  1207. bpf_object__unload(obj);
  1208. for (i = 0; i < obj->nr_maps; i++) {
  1209. zfree(&obj->maps[i].name);
  1210. if (obj->maps[i].clear_priv)
  1211. obj->maps[i].clear_priv(&obj->maps[i],
  1212. obj->maps[i].priv);
  1213. obj->maps[i].priv = NULL;
  1214. obj->maps[i].clear_priv = NULL;
  1215. }
  1216. zfree(&obj->maps);
  1217. obj->nr_maps = 0;
  1218. if (obj->programs && obj->nr_programs) {
  1219. for (i = 0; i < obj->nr_programs; i++)
  1220. bpf_program__exit(&obj->programs[i]);
  1221. }
  1222. zfree(&obj->programs);
  1223. list_del(&obj->list);
  1224. free(obj);
  1225. }
  1226. struct bpf_object *
  1227. bpf_object__next(struct bpf_object *prev)
  1228. {
  1229. struct bpf_object *next;
  1230. if (!prev)
  1231. next = list_first_entry(&bpf_objects_list,
  1232. struct bpf_object,
  1233. list);
  1234. else
  1235. next = list_next_entry(prev, list);
  1236. /* Empty list is noticed here so don't need checking on entry. */
  1237. if (&next->list == &bpf_objects_list)
  1238. return NULL;
  1239. return next;
  1240. }
  1241. const char *bpf_object__name(struct bpf_object *obj)
  1242. {
  1243. return obj ? obj->path : ERR_PTR(-EINVAL);
  1244. }
  1245. unsigned int bpf_object__kversion(struct bpf_object *obj)
  1246. {
  1247. return obj ? obj->kern_version : 0;
  1248. }
  1249. int bpf_object__set_priv(struct bpf_object *obj, void *priv,
  1250. bpf_object_clear_priv_t clear_priv)
  1251. {
  1252. if (obj->priv && obj->clear_priv)
  1253. obj->clear_priv(obj, obj->priv);
  1254. obj->priv = priv;
  1255. obj->clear_priv = clear_priv;
  1256. return 0;
  1257. }
  1258. void *bpf_object__priv(struct bpf_object *obj)
  1259. {
  1260. return obj ? obj->priv : ERR_PTR(-EINVAL);
  1261. }
  1262. struct bpf_program *
  1263. bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
  1264. {
  1265. size_t idx;
  1266. if (!obj->programs)
  1267. return NULL;
  1268. /* First handler */
  1269. if (prev == NULL)
  1270. return &obj->programs[0];
  1271. if (prev->obj != obj) {
  1272. pr_warning("error: program handler doesn't match object\n");
  1273. return NULL;
  1274. }
  1275. idx = (prev - obj->programs) + 1;
  1276. if (idx >= obj->nr_programs)
  1277. return NULL;
  1278. return &obj->programs[idx];
  1279. }
  1280. int bpf_program__set_priv(struct bpf_program *prog, void *priv,
  1281. bpf_program_clear_priv_t clear_priv)
  1282. {
  1283. if (prog->priv && prog->clear_priv)
  1284. prog->clear_priv(prog, prog->priv);
  1285. prog->priv = priv;
  1286. prog->clear_priv = clear_priv;
  1287. return 0;
  1288. }
  1289. void *bpf_program__priv(struct bpf_program *prog)
  1290. {
  1291. return prog ? prog->priv : ERR_PTR(-EINVAL);
  1292. }
  1293. const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
  1294. {
  1295. const char *title;
  1296. title = prog->section_name;
  1297. if (needs_copy) {
  1298. title = strdup(title);
  1299. if (!title) {
  1300. pr_warning("failed to strdup program title\n");
  1301. return ERR_PTR(-ENOMEM);
  1302. }
  1303. }
  1304. return title;
  1305. }
  1306. int bpf_program__fd(struct bpf_program *prog)
  1307. {
  1308. return bpf_program__nth_fd(prog, 0);
  1309. }
  1310. int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
  1311. bpf_program_prep_t prep)
  1312. {
  1313. int *instances_fds;
  1314. if (nr_instances <= 0 || !prep)
  1315. return -EINVAL;
  1316. if (prog->instances.nr > 0 || prog->instances.fds) {
  1317. pr_warning("Can't set pre-processor after loading\n");
  1318. return -EINVAL;
  1319. }
  1320. instances_fds = malloc(sizeof(int) * nr_instances);
  1321. if (!instances_fds) {
  1322. pr_warning("alloc memory failed for fds\n");
  1323. return -ENOMEM;
  1324. }
  1325. /* fill all fd with -1 */
  1326. memset(instances_fds, -1, sizeof(int) * nr_instances);
  1327. prog->instances.nr = nr_instances;
  1328. prog->instances.fds = instances_fds;
  1329. prog->preprocessor = prep;
  1330. return 0;
  1331. }
  1332. int bpf_program__nth_fd(struct bpf_program *prog, int n)
  1333. {
  1334. int fd;
  1335. if (n >= prog->instances.nr || n < 0) {
  1336. pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
  1337. n, prog->section_name, prog->instances.nr);
  1338. return -EINVAL;
  1339. }
  1340. fd = prog->instances.fds[n];
  1341. if (fd < 0) {
  1342. pr_warning("%dth instance of program '%s' is invalid\n",
  1343. n, prog->section_name);
  1344. return -ENOENT;
  1345. }
  1346. return fd;
  1347. }
  1348. void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
  1349. {
  1350. prog->type = type;
  1351. }
  1352. static bool bpf_program__is_type(struct bpf_program *prog,
  1353. enum bpf_prog_type type)
  1354. {
  1355. return prog ? (prog->type == type) : false;
  1356. }
  1357. #define BPF_PROG_TYPE_FNS(NAME, TYPE) \
  1358. int bpf_program__set_##NAME(struct bpf_program *prog) \
  1359. { \
  1360. if (!prog) \
  1361. return -EINVAL; \
  1362. bpf_program__set_type(prog, TYPE); \
  1363. return 0; \
  1364. } \
  1365. \
  1366. bool bpf_program__is_##NAME(struct bpf_program *prog) \
  1367. { \
  1368. return bpf_program__is_type(prog, TYPE); \
  1369. } \
  1370. BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
  1371. BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
  1372. BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
  1373. BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
  1374. BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
  1375. BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
  1376. BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
  1377. int bpf_map__fd(struct bpf_map *map)
  1378. {
  1379. return map ? map->fd : -EINVAL;
  1380. }
  1381. const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
  1382. {
  1383. return map ? &map->def : ERR_PTR(-EINVAL);
  1384. }
  1385. const char *bpf_map__name(struct bpf_map *map)
  1386. {
  1387. return map ? map->name : NULL;
  1388. }
  1389. int bpf_map__set_priv(struct bpf_map *map, void *priv,
  1390. bpf_map_clear_priv_t clear_priv)
  1391. {
  1392. if (!map)
  1393. return -EINVAL;
  1394. if (map->priv) {
  1395. if (map->clear_priv)
  1396. map->clear_priv(map, map->priv);
  1397. }
  1398. map->priv = priv;
  1399. map->clear_priv = clear_priv;
  1400. return 0;
  1401. }
  1402. void *bpf_map__priv(struct bpf_map *map)
  1403. {
  1404. return map ? map->priv : ERR_PTR(-EINVAL);
  1405. }
  1406. struct bpf_map *
  1407. bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
  1408. {
  1409. size_t idx;
  1410. struct bpf_map *s, *e;
  1411. if (!obj || !obj->maps)
  1412. return NULL;
  1413. s = obj->maps;
  1414. e = obj->maps + obj->nr_maps;
  1415. if (prev == NULL)
  1416. return s;
  1417. if ((prev < s) || (prev >= e)) {
  1418. pr_warning("error in %s: map handler doesn't belong to object\n",
  1419. __func__);
  1420. return NULL;
  1421. }
  1422. idx = (prev - obj->maps) + 1;
  1423. if (idx >= obj->nr_maps)
  1424. return NULL;
  1425. return &obj->maps[idx];
  1426. }
  1427. struct bpf_map *
  1428. bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
  1429. {
  1430. struct bpf_map *pos;
  1431. bpf_map__for_each(pos, obj) {
  1432. if (pos->name && !strcmp(pos->name, name))
  1433. return pos;
  1434. }
  1435. return NULL;
  1436. }
  1437. struct bpf_map *
  1438. bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
  1439. {
  1440. int i;
  1441. for (i = 0; i < obj->nr_maps; i++) {
  1442. if (obj->maps[i].offset == offset)
  1443. return &obj->maps[i];
  1444. }
  1445. return ERR_PTR(-ENOENT);
  1446. }
  1447. long libbpf_get_error(const void *ptr)
  1448. {
  1449. if (IS_ERR(ptr))
  1450. return PTR_ERR(ptr);
  1451. return 0;
  1452. }