libbpf.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  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. void bpf_object__close(struct bpf_object *obj)
  1158. {
  1159. size_t i;
  1160. if (!obj)
  1161. return;
  1162. if (obj->clear_priv)
  1163. obj->clear_priv(obj, obj->priv);
  1164. bpf_object__elf_finish(obj);
  1165. bpf_object__unload(obj);
  1166. for (i = 0; i < obj->nr_maps; i++) {
  1167. zfree(&obj->maps[i].name);
  1168. if (obj->maps[i].clear_priv)
  1169. obj->maps[i].clear_priv(&obj->maps[i],
  1170. obj->maps[i].priv);
  1171. obj->maps[i].priv = NULL;
  1172. obj->maps[i].clear_priv = NULL;
  1173. }
  1174. zfree(&obj->maps);
  1175. obj->nr_maps = 0;
  1176. if (obj->programs && obj->nr_programs) {
  1177. for (i = 0; i < obj->nr_programs; i++)
  1178. bpf_program__exit(&obj->programs[i]);
  1179. }
  1180. zfree(&obj->programs);
  1181. list_del(&obj->list);
  1182. free(obj);
  1183. }
  1184. struct bpf_object *
  1185. bpf_object__next(struct bpf_object *prev)
  1186. {
  1187. struct bpf_object *next;
  1188. if (!prev)
  1189. next = list_first_entry(&bpf_objects_list,
  1190. struct bpf_object,
  1191. list);
  1192. else
  1193. next = list_next_entry(prev, list);
  1194. /* Empty list is noticed here so don't need checking on entry. */
  1195. if (&next->list == &bpf_objects_list)
  1196. return NULL;
  1197. return next;
  1198. }
  1199. const char *bpf_object__name(struct bpf_object *obj)
  1200. {
  1201. return obj ? obj->path : ERR_PTR(-EINVAL);
  1202. }
  1203. unsigned int bpf_object__kversion(struct bpf_object *obj)
  1204. {
  1205. return obj ? obj->kern_version : 0;
  1206. }
  1207. int bpf_object__set_priv(struct bpf_object *obj, void *priv,
  1208. bpf_object_clear_priv_t clear_priv)
  1209. {
  1210. if (obj->priv && obj->clear_priv)
  1211. obj->clear_priv(obj, obj->priv);
  1212. obj->priv = priv;
  1213. obj->clear_priv = clear_priv;
  1214. return 0;
  1215. }
  1216. void *bpf_object__priv(struct bpf_object *obj)
  1217. {
  1218. return obj ? obj->priv : ERR_PTR(-EINVAL);
  1219. }
  1220. struct bpf_program *
  1221. bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
  1222. {
  1223. size_t idx;
  1224. if (!obj->programs)
  1225. return NULL;
  1226. /* First handler */
  1227. if (prev == NULL)
  1228. return &obj->programs[0];
  1229. if (prev->obj != obj) {
  1230. pr_warning("error: program handler doesn't match object\n");
  1231. return NULL;
  1232. }
  1233. idx = (prev - obj->programs) + 1;
  1234. if (idx >= obj->nr_programs)
  1235. return NULL;
  1236. return &obj->programs[idx];
  1237. }
  1238. int bpf_program__set_priv(struct bpf_program *prog, void *priv,
  1239. bpf_program_clear_priv_t clear_priv)
  1240. {
  1241. if (prog->priv && prog->clear_priv)
  1242. prog->clear_priv(prog, prog->priv);
  1243. prog->priv = priv;
  1244. prog->clear_priv = clear_priv;
  1245. return 0;
  1246. }
  1247. void *bpf_program__priv(struct bpf_program *prog)
  1248. {
  1249. return prog ? prog->priv : ERR_PTR(-EINVAL);
  1250. }
  1251. const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
  1252. {
  1253. const char *title;
  1254. title = prog->section_name;
  1255. if (needs_copy) {
  1256. title = strdup(title);
  1257. if (!title) {
  1258. pr_warning("failed to strdup program title\n");
  1259. return ERR_PTR(-ENOMEM);
  1260. }
  1261. }
  1262. return title;
  1263. }
  1264. int bpf_program__fd(struct bpf_program *prog)
  1265. {
  1266. return bpf_program__nth_fd(prog, 0);
  1267. }
  1268. int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
  1269. bpf_program_prep_t prep)
  1270. {
  1271. int *instances_fds;
  1272. if (nr_instances <= 0 || !prep)
  1273. return -EINVAL;
  1274. if (prog->instances.nr > 0 || prog->instances.fds) {
  1275. pr_warning("Can't set pre-processor after loading\n");
  1276. return -EINVAL;
  1277. }
  1278. instances_fds = malloc(sizeof(int) * nr_instances);
  1279. if (!instances_fds) {
  1280. pr_warning("alloc memory failed for fds\n");
  1281. return -ENOMEM;
  1282. }
  1283. /* fill all fd with -1 */
  1284. memset(instances_fds, -1, sizeof(int) * nr_instances);
  1285. prog->instances.nr = nr_instances;
  1286. prog->instances.fds = instances_fds;
  1287. prog->preprocessor = prep;
  1288. return 0;
  1289. }
  1290. int bpf_program__nth_fd(struct bpf_program *prog, int n)
  1291. {
  1292. int fd;
  1293. if (n >= prog->instances.nr || n < 0) {
  1294. pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
  1295. n, prog->section_name, prog->instances.nr);
  1296. return -EINVAL;
  1297. }
  1298. fd = prog->instances.fds[n];
  1299. if (fd < 0) {
  1300. pr_warning("%dth instance of program '%s' is invalid\n",
  1301. n, prog->section_name);
  1302. return -ENOENT;
  1303. }
  1304. return fd;
  1305. }
  1306. static void bpf_program__set_type(struct bpf_program *prog,
  1307. enum bpf_prog_type type)
  1308. {
  1309. prog->type = type;
  1310. }
  1311. static bool bpf_program__is_type(struct bpf_program *prog,
  1312. enum bpf_prog_type type)
  1313. {
  1314. return prog ? (prog->type == type) : false;
  1315. }
  1316. #define BPF_PROG_TYPE_FNS(NAME, TYPE) \
  1317. int bpf_program__set_##NAME(struct bpf_program *prog) \
  1318. { \
  1319. if (!prog) \
  1320. return -EINVAL; \
  1321. bpf_program__set_type(prog, TYPE); \
  1322. return 0; \
  1323. } \
  1324. \
  1325. bool bpf_program__is_##NAME(struct bpf_program *prog) \
  1326. { \
  1327. return bpf_program__is_type(prog, TYPE); \
  1328. } \
  1329. BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
  1330. BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
  1331. BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
  1332. BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
  1333. BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
  1334. BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
  1335. BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
  1336. int bpf_map__fd(struct bpf_map *map)
  1337. {
  1338. return map ? map->fd : -EINVAL;
  1339. }
  1340. const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
  1341. {
  1342. return map ? &map->def : ERR_PTR(-EINVAL);
  1343. }
  1344. const char *bpf_map__name(struct bpf_map *map)
  1345. {
  1346. return map ? map->name : NULL;
  1347. }
  1348. int bpf_map__set_priv(struct bpf_map *map, void *priv,
  1349. bpf_map_clear_priv_t clear_priv)
  1350. {
  1351. if (!map)
  1352. return -EINVAL;
  1353. if (map->priv) {
  1354. if (map->clear_priv)
  1355. map->clear_priv(map, map->priv);
  1356. }
  1357. map->priv = priv;
  1358. map->clear_priv = clear_priv;
  1359. return 0;
  1360. }
  1361. void *bpf_map__priv(struct bpf_map *map)
  1362. {
  1363. return map ? map->priv : ERR_PTR(-EINVAL);
  1364. }
  1365. struct bpf_map *
  1366. bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
  1367. {
  1368. size_t idx;
  1369. struct bpf_map *s, *e;
  1370. if (!obj || !obj->maps)
  1371. return NULL;
  1372. s = obj->maps;
  1373. e = obj->maps + obj->nr_maps;
  1374. if (prev == NULL)
  1375. return s;
  1376. if ((prev < s) || (prev >= e)) {
  1377. pr_warning("error in %s: map handler doesn't belong to object\n",
  1378. __func__);
  1379. return NULL;
  1380. }
  1381. idx = (prev - obj->maps) + 1;
  1382. if (idx >= obj->nr_maps)
  1383. return NULL;
  1384. return &obj->maps[idx];
  1385. }
  1386. struct bpf_map *
  1387. bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
  1388. {
  1389. struct bpf_map *pos;
  1390. bpf_map__for_each(pos, obj) {
  1391. if (pos->name && !strcmp(pos->name, name))
  1392. return pos;
  1393. }
  1394. return NULL;
  1395. }
  1396. struct bpf_map *
  1397. bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
  1398. {
  1399. int i;
  1400. for (i = 0; i < obj->nr_maps; i++) {
  1401. if (obj->maps[i].offset == offset)
  1402. return &obj->maps[i];
  1403. }
  1404. return ERR_PTR(-ENOENT);
  1405. }
  1406. long libbpf_get_error(const void *ptr)
  1407. {
  1408. if (IS_ERR(ptr))
  1409. return PTR_ERR(ptr);
  1410. return 0;
  1411. }