libbpf.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * Common eBPF ELF object loading operations.
  4. *
  5. * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
  6. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  7. * Copyright (C) 2015 Huawei Inc.
  8. * Copyright (C) 2017 Nicira, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License (not later!)
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this program; if not, see <http://www.gnu.org/licenses>
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <libgen.h>
  27. #include <inttypes.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include <errno.h>
  32. #include <asm/unistd.h>
  33. #include <linux/err.h>
  34. #include <linux/kernel.h>
  35. #include <linux/bpf.h>
  36. #include <linux/list.h>
  37. #include <linux/limits.h>
  38. #include <sys/stat.h>
  39. #include <sys/types.h>
  40. #include <sys/vfs.h>
  41. #include <libelf.h>
  42. #include <gelf.h>
  43. #include "libbpf.h"
  44. #include "bpf.h"
  45. #ifndef EM_BPF
  46. #define EM_BPF 247
  47. #endif
  48. #ifndef BPF_FS_MAGIC
  49. #define BPF_FS_MAGIC 0xcafe4a11
  50. #endif
  51. #define __printf(a, b) __attribute__((format(printf, a, b)))
  52. __printf(1, 2)
  53. static int __base_pr(const char *format, ...)
  54. {
  55. va_list args;
  56. int err;
  57. va_start(args, format);
  58. err = vfprintf(stderr, format, args);
  59. va_end(args);
  60. return err;
  61. }
  62. static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
  63. static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
  64. static __printf(1, 2) libbpf_print_fn_t __pr_debug;
  65. #define __pr(func, fmt, ...) \
  66. do { \
  67. if ((func)) \
  68. (func)("libbpf: " fmt, ##__VA_ARGS__); \
  69. } while (0)
  70. #define pr_warning(fmt, ...) __pr(__pr_warning, fmt, ##__VA_ARGS__)
  71. #define pr_info(fmt, ...) __pr(__pr_info, fmt, ##__VA_ARGS__)
  72. #define pr_debug(fmt, ...) __pr(__pr_debug, fmt, ##__VA_ARGS__)
  73. void libbpf_set_print(libbpf_print_fn_t warn,
  74. libbpf_print_fn_t info,
  75. libbpf_print_fn_t debug)
  76. {
  77. __pr_warning = warn;
  78. __pr_info = info;
  79. __pr_debug = debug;
  80. }
  81. #define STRERR_BUFSIZE 128
  82. #define ERRNO_OFFSET(e) ((e) - __LIBBPF_ERRNO__START)
  83. #define ERRCODE_OFFSET(c) ERRNO_OFFSET(LIBBPF_ERRNO__##c)
  84. #define NR_ERRNO (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
  85. static const char *libbpf_strerror_table[NR_ERRNO] = {
  86. [ERRCODE_OFFSET(LIBELF)] = "Something wrong in libelf",
  87. [ERRCODE_OFFSET(FORMAT)] = "BPF object format invalid",
  88. [ERRCODE_OFFSET(KVERSION)] = "'version' section incorrect or lost",
  89. [ERRCODE_OFFSET(ENDIAN)] = "Endian mismatch",
  90. [ERRCODE_OFFSET(INTERNAL)] = "Internal error in libbpf",
  91. [ERRCODE_OFFSET(RELOC)] = "Relocation failed",
  92. [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading",
  93. [ERRCODE_OFFSET(PROG2BIG)] = "Program too big",
  94. [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version",
  95. [ERRCODE_OFFSET(PROGTYPE)] = "Kernel doesn't support this program type",
  96. [ERRCODE_OFFSET(WRNGPID)] = "Wrong pid in netlink message",
  97. [ERRCODE_OFFSET(INVSEQ)] = "Invalid netlink sequence",
  98. };
  99. int libbpf_strerror(int err, char *buf, size_t size)
  100. {
  101. if (!buf || !size)
  102. return -1;
  103. err = err > 0 ? err : -err;
  104. if (err < __LIBBPF_ERRNO__START) {
  105. int ret;
  106. ret = strerror_r(err, buf, size);
  107. buf[size - 1] = '\0';
  108. return ret;
  109. }
  110. if (err < __LIBBPF_ERRNO__END) {
  111. const char *msg;
  112. msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
  113. snprintf(buf, size, "%s", msg);
  114. buf[size - 1] = '\0';
  115. return 0;
  116. }
  117. snprintf(buf, size, "Unknown libbpf error %d", err);
  118. buf[size - 1] = '\0';
  119. return -1;
  120. }
  121. #define CHECK_ERR(action, err, out) do { \
  122. err = action; \
  123. if (err) \
  124. goto out; \
  125. } while(0)
  126. /* Copied from tools/perf/util/util.h */
  127. #ifndef zfree
  128. # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
  129. #endif
  130. #ifndef zclose
  131. # define zclose(fd) ({ \
  132. int ___err = 0; \
  133. if ((fd) >= 0) \
  134. ___err = close((fd)); \
  135. fd = -1; \
  136. ___err; })
  137. #endif
  138. #ifdef HAVE_LIBELF_MMAP_SUPPORT
  139. # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
  140. #else
  141. # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
  142. #endif
  143. /*
  144. * bpf_prog should be a better name but it has been used in
  145. * linux/filter.h.
  146. */
  147. struct bpf_program {
  148. /* Index in elf obj file, for relocation use. */
  149. int idx;
  150. char *name;
  151. char *section_name;
  152. struct bpf_insn *insns;
  153. size_t insns_cnt, main_prog_cnt;
  154. enum bpf_prog_type type;
  155. struct reloc_desc {
  156. enum {
  157. RELO_LD64,
  158. RELO_CALL,
  159. } type;
  160. int insn_idx;
  161. union {
  162. int map_idx;
  163. int text_off;
  164. };
  165. } *reloc_desc;
  166. int nr_reloc;
  167. struct {
  168. int nr;
  169. int *fds;
  170. } instances;
  171. bpf_program_prep_t preprocessor;
  172. struct bpf_object *obj;
  173. void *priv;
  174. bpf_program_clear_priv_t clear_priv;
  175. enum bpf_attach_type expected_attach_type;
  176. };
  177. struct bpf_map {
  178. int fd;
  179. char *name;
  180. size_t offset;
  181. struct bpf_map_def def;
  182. void *priv;
  183. bpf_map_clear_priv_t clear_priv;
  184. };
  185. static LIST_HEAD(bpf_objects_list);
  186. struct bpf_object {
  187. char license[64];
  188. u32 kern_version;
  189. struct bpf_program *programs;
  190. size_t nr_programs;
  191. struct bpf_map *maps;
  192. size_t nr_maps;
  193. bool loaded;
  194. /*
  195. * Information when doing elf related work. Only valid if fd
  196. * is valid.
  197. */
  198. struct {
  199. int fd;
  200. void *obj_buf;
  201. size_t obj_buf_sz;
  202. Elf *elf;
  203. GElf_Ehdr ehdr;
  204. Elf_Data *symbols;
  205. size_t strtabidx;
  206. struct {
  207. GElf_Shdr shdr;
  208. Elf_Data *data;
  209. } *reloc;
  210. int nr_reloc;
  211. int maps_shndx;
  212. int text_shndx;
  213. } efile;
  214. /*
  215. * All loaded bpf_object is linked in a list, which is
  216. * hidden to caller. bpf_objects__<func> handlers deal with
  217. * all objects.
  218. */
  219. struct list_head list;
  220. void *priv;
  221. bpf_object_clear_priv_t clear_priv;
  222. char path[];
  223. };
  224. #define obj_elf_valid(o) ((o)->efile.elf)
  225. static void bpf_program__unload(struct bpf_program *prog)
  226. {
  227. int i;
  228. if (!prog)
  229. return;
  230. /*
  231. * If the object is opened but the program was never loaded,
  232. * it is possible that prog->instances.nr == -1.
  233. */
  234. if (prog->instances.nr > 0) {
  235. for (i = 0; i < prog->instances.nr; i++)
  236. zclose(prog->instances.fds[i]);
  237. } else if (prog->instances.nr != -1) {
  238. pr_warning("Internal error: instances.nr is %d\n",
  239. prog->instances.nr);
  240. }
  241. prog->instances.nr = -1;
  242. zfree(&prog->instances.fds);
  243. }
  244. static void bpf_program__exit(struct bpf_program *prog)
  245. {
  246. if (!prog)
  247. return;
  248. if (prog->clear_priv)
  249. prog->clear_priv(prog, prog->priv);
  250. prog->priv = NULL;
  251. prog->clear_priv = NULL;
  252. bpf_program__unload(prog);
  253. zfree(&prog->name);
  254. zfree(&prog->section_name);
  255. zfree(&prog->insns);
  256. zfree(&prog->reloc_desc);
  257. prog->nr_reloc = 0;
  258. prog->insns_cnt = 0;
  259. prog->idx = -1;
  260. }
  261. static int
  262. bpf_program__init(void *data, size_t size, char *section_name, int idx,
  263. struct bpf_program *prog)
  264. {
  265. if (size < sizeof(struct bpf_insn)) {
  266. pr_warning("corrupted section '%s'\n", section_name);
  267. return -EINVAL;
  268. }
  269. bzero(prog, sizeof(*prog));
  270. prog->section_name = strdup(section_name);
  271. if (!prog->section_name) {
  272. pr_warning("failed to alloc name for prog under section(%d) %s\n",
  273. idx, section_name);
  274. goto errout;
  275. }
  276. prog->insns = malloc(size);
  277. if (!prog->insns) {
  278. pr_warning("failed to alloc insns for prog under section %s\n",
  279. section_name);
  280. goto errout;
  281. }
  282. prog->insns_cnt = size / sizeof(struct bpf_insn);
  283. memcpy(prog->insns, data,
  284. prog->insns_cnt * sizeof(struct bpf_insn));
  285. prog->idx = idx;
  286. prog->instances.fds = NULL;
  287. prog->instances.nr = -1;
  288. prog->type = BPF_PROG_TYPE_KPROBE;
  289. return 0;
  290. errout:
  291. bpf_program__exit(prog);
  292. return -ENOMEM;
  293. }
  294. static int
  295. bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
  296. char *section_name, int idx)
  297. {
  298. struct bpf_program prog, *progs;
  299. int nr_progs, err;
  300. err = bpf_program__init(data, size, section_name, idx, &prog);
  301. if (err)
  302. return err;
  303. progs = obj->programs;
  304. nr_progs = obj->nr_programs;
  305. progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
  306. if (!progs) {
  307. /*
  308. * In this case the original obj->programs
  309. * is still valid, so don't need special treat for
  310. * bpf_close_object().
  311. */
  312. pr_warning("failed to alloc a new program under section '%s'\n",
  313. section_name);
  314. bpf_program__exit(&prog);
  315. return -ENOMEM;
  316. }
  317. pr_debug("found program %s\n", prog.section_name);
  318. obj->programs = progs;
  319. obj->nr_programs = nr_progs + 1;
  320. prog.obj = obj;
  321. progs[nr_progs] = prog;
  322. return 0;
  323. }
  324. static int
  325. bpf_object__init_prog_names(struct bpf_object *obj)
  326. {
  327. Elf_Data *symbols = obj->efile.symbols;
  328. struct bpf_program *prog;
  329. size_t pi, si;
  330. for (pi = 0; pi < obj->nr_programs; pi++) {
  331. const char *name = NULL;
  332. prog = &obj->programs[pi];
  333. if (prog->idx == obj->efile.text_shndx) {
  334. name = ".text";
  335. goto skip_search;
  336. }
  337. for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
  338. si++) {
  339. GElf_Sym sym;
  340. if (!gelf_getsym(symbols, si, &sym))
  341. continue;
  342. if (sym.st_shndx != prog->idx)
  343. continue;
  344. if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
  345. continue;
  346. name = elf_strptr(obj->efile.elf,
  347. obj->efile.strtabidx,
  348. sym.st_name);
  349. if (!name) {
  350. pr_warning("failed to get sym name string for prog %s\n",
  351. prog->section_name);
  352. return -LIBBPF_ERRNO__LIBELF;
  353. }
  354. }
  355. if (!name) {
  356. pr_warning("failed to find sym for prog %s\n",
  357. prog->section_name);
  358. return -EINVAL;
  359. }
  360. skip_search:
  361. prog->name = strdup(name);
  362. if (!prog->name) {
  363. pr_warning("failed to allocate memory for prog sym %s\n",
  364. name);
  365. return -ENOMEM;
  366. }
  367. }
  368. return 0;
  369. }
  370. static struct bpf_object *bpf_object__new(const char *path,
  371. void *obj_buf,
  372. size_t obj_buf_sz)
  373. {
  374. struct bpf_object *obj;
  375. obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
  376. if (!obj) {
  377. pr_warning("alloc memory failed for %s\n", path);
  378. return ERR_PTR(-ENOMEM);
  379. }
  380. strcpy(obj->path, path);
  381. obj->efile.fd = -1;
  382. /*
  383. * Caller of this function should also calls
  384. * bpf_object__elf_finish() after data collection to return
  385. * obj_buf to user. If not, we should duplicate the buffer to
  386. * avoid user freeing them before elf finish.
  387. */
  388. obj->efile.obj_buf = obj_buf;
  389. obj->efile.obj_buf_sz = obj_buf_sz;
  390. obj->efile.maps_shndx = -1;
  391. obj->loaded = false;
  392. INIT_LIST_HEAD(&obj->list);
  393. list_add(&obj->list, &bpf_objects_list);
  394. return obj;
  395. }
  396. static void bpf_object__elf_finish(struct bpf_object *obj)
  397. {
  398. if (!obj_elf_valid(obj))
  399. return;
  400. if (obj->efile.elf) {
  401. elf_end(obj->efile.elf);
  402. obj->efile.elf = NULL;
  403. }
  404. obj->efile.symbols = NULL;
  405. zfree(&obj->efile.reloc);
  406. obj->efile.nr_reloc = 0;
  407. zclose(obj->efile.fd);
  408. obj->efile.obj_buf = NULL;
  409. obj->efile.obj_buf_sz = 0;
  410. }
  411. static int bpf_object__elf_init(struct bpf_object *obj)
  412. {
  413. int err = 0;
  414. GElf_Ehdr *ep;
  415. if (obj_elf_valid(obj)) {
  416. pr_warning("elf init: internal error\n");
  417. return -LIBBPF_ERRNO__LIBELF;
  418. }
  419. if (obj->efile.obj_buf_sz > 0) {
  420. /*
  421. * obj_buf should have been validated by
  422. * bpf_object__open_buffer().
  423. */
  424. obj->efile.elf = elf_memory(obj->efile.obj_buf,
  425. obj->efile.obj_buf_sz);
  426. } else {
  427. obj->efile.fd = open(obj->path, O_RDONLY);
  428. if (obj->efile.fd < 0) {
  429. pr_warning("failed to open %s: %s\n", obj->path,
  430. strerror(errno));
  431. return -errno;
  432. }
  433. obj->efile.elf = elf_begin(obj->efile.fd,
  434. LIBBPF_ELF_C_READ_MMAP,
  435. NULL);
  436. }
  437. if (!obj->efile.elf) {
  438. pr_warning("failed to open %s as ELF file\n",
  439. obj->path);
  440. err = -LIBBPF_ERRNO__LIBELF;
  441. goto errout;
  442. }
  443. if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
  444. pr_warning("failed to get EHDR from %s\n",
  445. obj->path);
  446. err = -LIBBPF_ERRNO__FORMAT;
  447. goto errout;
  448. }
  449. ep = &obj->efile.ehdr;
  450. /* Old LLVM set e_machine to EM_NONE */
  451. if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
  452. pr_warning("%s is not an eBPF object file\n",
  453. obj->path);
  454. err = -LIBBPF_ERRNO__FORMAT;
  455. goto errout;
  456. }
  457. return 0;
  458. errout:
  459. bpf_object__elf_finish(obj);
  460. return err;
  461. }
  462. static int
  463. bpf_object__check_endianness(struct bpf_object *obj)
  464. {
  465. static unsigned int const endian = 1;
  466. switch (obj->efile.ehdr.e_ident[EI_DATA]) {
  467. case ELFDATA2LSB:
  468. /* We are big endian, BPF obj is little endian. */
  469. if (*(unsigned char const *)&endian != 1)
  470. goto mismatch;
  471. break;
  472. case ELFDATA2MSB:
  473. /* We are little endian, BPF obj is big endian. */
  474. if (*(unsigned char const *)&endian != 0)
  475. goto mismatch;
  476. break;
  477. default:
  478. return -LIBBPF_ERRNO__ENDIAN;
  479. }
  480. return 0;
  481. mismatch:
  482. pr_warning("Error: endianness mismatch.\n");
  483. return -LIBBPF_ERRNO__ENDIAN;
  484. }
  485. static int
  486. bpf_object__init_license(struct bpf_object *obj,
  487. void *data, size_t size)
  488. {
  489. memcpy(obj->license, data,
  490. min(size, sizeof(obj->license) - 1));
  491. pr_debug("license of %s is %s\n", obj->path, obj->license);
  492. return 0;
  493. }
  494. static int
  495. bpf_object__init_kversion(struct bpf_object *obj,
  496. void *data, size_t size)
  497. {
  498. u32 kver;
  499. if (size != sizeof(kver)) {
  500. pr_warning("invalid kver section in %s\n", obj->path);
  501. return -LIBBPF_ERRNO__FORMAT;
  502. }
  503. memcpy(&kver, data, sizeof(kver));
  504. obj->kern_version = kver;
  505. pr_debug("kernel version of %s is %x\n", obj->path,
  506. obj->kern_version);
  507. return 0;
  508. }
  509. static int compare_bpf_map(const void *_a, const void *_b)
  510. {
  511. const struct bpf_map *a = _a;
  512. const struct bpf_map *b = _b;
  513. return a->offset - b->offset;
  514. }
  515. static int
  516. bpf_object__init_maps(struct bpf_object *obj)
  517. {
  518. int i, map_idx, map_def_sz, nr_maps = 0;
  519. Elf_Scn *scn;
  520. Elf_Data *data;
  521. Elf_Data *symbols = obj->efile.symbols;
  522. if (obj->efile.maps_shndx < 0)
  523. return -EINVAL;
  524. if (!symbols)
  525. return -EINVAL;
  526. scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
  527. if (scn)
  528. data = elf_getdata(scn, NULL);
  529. if (!scn || !data) {
  530. pr_warning("failed to get Elf_Data from map section %d\n",
  531. obj->efile.maps_shndx);
  532. return -EINVAL;
  533. }
  534. /*
  535. * Count number of maps. Each map has a name.
  536. * Array of maps is not supported: only the first element is
  537. * considered.
  538. *
  539. * TODO: Detect array of map and report error.
  540. */
  541. for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
  542. GElf_Sym sym;
  543. if (!gelf_getsym(symbols, i, &sym))
  544. continue;
  545. if (sym.st_shndx != obj->efile.maps_shndx)
  546. continue;
  547. nr_maps++;
  548. }
  549. /* Alloc obj->maps and fill nr_maps. */
  550. pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
  551. nr_maps, data->d_size);
  552. if (!nr_maps)
  553. return 0;
  554. /* Assume equally sized map definitions */
  555. map_def_sz = data->d_size / nr_maps;
  556. if (!data->d_size || (data->d_size % nr_maps) != 0) {
  557. pr_warning("unable to determine map definition size "
  558. "section %s, %d maps in %zd bytes\n",
  559. obj->path, nr_maps, data->d_size);
  560. return -EINVAL;
  561. }
  562. obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
  563. if (!obj->maps) {
  564. pr_warning("alloc maps for object failed\n");
  565. return -ENOMEM;
  566. }
  567. obj->nr_maps = nr_maps;
  568. /*
  569. * fill all fd with -1 so won't close incorrect
  570. * fd (fd=0 is stdin) when failure (zclose won't close
  571. * negative fd)).
  572. */
  573. for (i = 0; i < nr_maps; i++)
  574. obj->maps[i].fd = -1;
  575. /*
  576. * Fill obj->maps using data in "maps" section.
  577. */
  578. for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
  579. GElf_Sym sym;
  580. const char *map_name;
  581. struct bpf_map_def *def;
  582. if (!gelf_getsym(symbols, i, &sym))
  583. continue;
  584. if (sym.st_shndx != obj->efile.maps_shndx)
  585. continue;
  586. map_name = elf_strptr(obj->efile.elf,
  587. obj->efile.strtabidx,
  588. sym.st_name);
  589. obj->maps[map_idx].offset = sym.st_value;
  590. if (sym.st_value + map_def_sz > data->d_size) {
  591. pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
  592. obj->path, map_name);
  593. return -EINVAL;
  594. }
  595. obj->maps[map_idx].name = strdup(map_name);
  596. if (!obj->maps[map_idx].name) {
  597. pr_warning("failed to alloc map name\n");
  598. return -ENOMEM;
  599. }
  600. pr_debug("map %d is \"%s\"\n", map_idx,
  601. obj->maps[map_idx].name);
  602. def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
  603. /*
  604. * If the definition of the map in the object file fits in
  605. * bpf_map_def, copy it. Any extra fields in our version
  606. * of bpf_map_def will default to zero as a result of the
  607. * calloc above.
  608. */
  609. if (map_def_sz <= sizeof(struct bpf_map_def)) {
  610. memcpy(&obj->maps[map_idx].def, def, map_def_sz);
  611. } else {
  612. /*
  613. * Here the map structure being read is bigger than what
  614. * we expect, truncate if the excess bits are all zero.
  615. * If they are not zero, reject this map as
  616. * incompatible.
  617. */
  618. char *b;
  619. for (b = ((char *)def) + sizeof(struct bpf_map_def);
  620. b < ((char *)def) + map_def_sz; b++) {
  621. if (*b != 0) {
  622. pr_warning("maps section in %s: \"%s\" "
  623. "has unrecognized, non-zero "
  624. "options\n",
  625. obj->path, map_name);
  626. return -EINVAL;
  627. }
  628. }
  629. memcpy(&obj->maps[map_idx].def, def,
  630. sizeof(struct bpf_map_def));
  631. }
  632. map_idx++;
  633. }
  634. qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
  635. return 0;
  636. }
  637. static bool section_have_execinstr(struct bpf_object *obj, int idx)
  638. {
  639. Elf_Scn *scn;
  640. GElf_Shdr sh;
  641. scn = elf_getscn(obj->efile.elf, idx);
  642. if (!scn)
  643. return false;
  644. if (gelf_getshdr(scn, &sh) != &sh)
  645. return false;
  646. if (sh.sh_flags & SHF_EXECINSTR)
  647. return true;
  648. return false;
  649. }
  650. static int bpf_object__elf_collect(struct bpf_object *obj)
  651. {
  652. Elf *elf = obj->efile.elf;
  653. GElf_Ehdr *ep = &obj->efile.ehdr;
  654. Elf_Scn *scn = NULL;
  655. int idx = 0, err = 0;
  656. /* Elf is corrupted/truncated, avoid calling elf_strptr. */
  657. if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
  658. pr_warning("failed to get e_shstrndx from %s\n",
  659. obj->path);
  660. return -LIBBPF_ERRNO__FORMAT;
  661. }
  662. while ((scn = elf_nextscn(elf, scn)) != NULL) {
  663. char *name;
  664. GElf_Shdr sh;
  665. Elf_Data *data;
  666. idx++;
  667. if (gelf_getshdr(scn, &sh) != &sh) {
  668. pr_warning("failed to get section(%d) header from %s\n",
  669. idx, obj->path);
  670. err = -LIBBPF_ERRNO__FORMAT;
  671. goto out;
  672. }
  673. name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
  674. if (!name) {
  675. pr_warning("failed to get section(%d) name from %s\n",
  676. idx, obj->path);
  677. err = -LIBBPF_ERRNO__FORMAT;
  678. goto out;
  679. }
  680. data = elf_getdata(scn, 0);
  681. if (!data) {
  682. pr_warning("failed to get section(%d) data from %s(%s)\n",
  683. idx, name, obj->path);
  684. err = -LIBBPF_ERRNO__FORMAT;
  685. goto out;
  686. }
  687. pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
  688. idx, name, (unsigned long)data->d_size,
  689. (int)sh.sh_link, (unsigned long)sh.sh_flags,
  690. (int)sh.sh_type);
  691. if (strcmp(name, "license") == 0)
  692. err = bpf_object__init_license(obj,
  693. data->d_buf,
  694. data->d_size);
  695. else if (strcmp(name, "version") == 0)
  696. err = bpf_object__init_kversion(obj,
  697. data->d_buf,
  698. data->d_size);
  699. else if (strcmp(name, "maps") == 0)
  700. obj->efile.maps_shndx = idx;
  701. else if (sh.sh_type == SHT_SYMTAB) {
  702. if (obj->efile.symbols) {
  703. pr_warning("bpf: multiple SYMTAB in %s\n",
  704. obj->path);
  705. err = -LIBBPF_ERRNO__FORMAT;
  706. } else {
  707. obj->efile.symbols = data;
  708. obj->efile.strtabidx = sh.sh_link;
  709. }
  710. } else if ((sh.sh_type == SHT_PROGBITS) &&
  711. (sh.sh_flags & SHF_EXECINSTR) &&
  712. (data->d_size > 0)) {
  713. if (strcmp(name, ".text") == 0)
  714. obj->efile.text_shndx = idx;
  715. err = bpf_object__add_program(obj, data->d_buf,
  716. data->d_size, name, idx);
  717. if (err) {
  718. char errmsg[STRERR_BUFSIZE];
  719. strerror_r(-err, errmsg, sizeof(errmsg));
  720. pr_warning("failed to alloc program %s (%s): %s",
  721. name, obj->path, errmsg);
  722. }
  723. } else if (sh.sh_type == SHT_REL) {
  724. void *reloc = obj->efile.reloc;
  725. int nr_reloc = obj->efile.nr_reloc + 1;
  726. int sec = sh.sh_info; /* points to other section */
  727. /* Only do relo for section with exec instructions */
  728. if (!section_have_execinstr(obj, sec)) {
  729. pr_debug("skip relo %s(%d) for section(%d)\n",
  730. name, idx, sec);
  731. continue;
  732. }
  733. reloc = realloc(reloc,
  734. sizeof(*obj->efile.reloc) * nr_reloc);
  735. if (!reloc) {
  736. pr_warning("realloc failed\n");
  737. err = -ENOMEM;
  738. } else {
  739. int n = nr_reloc - 1;
  740. obj->efile.reloc = reloc;
  741. obj->efile.nr_reloc = nr_reloc;
  742. obj->efile.reloc[n].shdr = sh;
  743. obj->efile.reloc[n].data = data;
  744. }
  745. } else {
  746. pr_debug("skip section(%d) %s\n", idx, name);
  747. }
  748. if (err)
  749. goto out;
  750. }
  751. if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
  752. pr_warning("Corrupted ELF file: index of strtab invalid\n");
  753. return LIBBPF_ERRNO__FORMAT;
  754. }
  755. if (obj->efile.maps_shndx >= 0) {
  756. err = bpf_object__init_maps(obj);
  757. if (err)
  758. goto out;
  759. }
  760. err = bpf_object__init_prog_names(obj);
  761. out:
  762. return err;
  763. }
  764. static struct bpf_program *
  765. bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
  766. {
  767. struct bpf_program *prog;
  768. size_t i;
  769. for (i = 0; i < obj->nr_programs; i++) {
  770. prog = &obj->programs[i];
  771. if (prog->idx == idx)
  772. return prog;
  773. }
  774. return NULL;
  775. }
  776. static int
  777. bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
  778. Elf_Data *data, struct bpf_object *obj)
  779. {
  780. Elf_Data *symbols = obj->efile.symbols;
  781. int text_shndx = obj->efile.text_shndx;
  782. int maps_shndx = obj->efile.maps_shndx;
  783. struct bpf_map *maps = obj->maps;
  784. size_t nr_maps = obj->nr_maps;
  785. int i, nrels;
  786. pr_debug("collecting relocating info for: '%s'\n",
  787. prog->section_name);
  788. nrels = shdr->sh_size / shdr->sh_entsize;
  789. prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
  790. if (!prog->reloc_desc) {
  791. pr_warning("failed to alloc memory in relocation\n");
  792. return -ENOMEM;
  793. }
  794. prog->nr_reloc = nrels;
  795. for (i = 0; i < nrels; i++) {
  796. GElf_Sym sym;
  797. GElf_Rel rel;
  798. unsigned int insn_idx;
  799. struct bpf_insn *insns = prog->insns;
  800. size_t map_idx;
  801. if (!gelf_getrel(data, i, &rel)) {
  802. pr_warning("relocation: failed to get %d reloc\n", i);
  803. return -LIBBPF_ERRNO__FORMAT;
  804. }
  805. if (!gelf_getsym(symbols,
  806. GELF_R_SYM(rel.r_info),
  807. &sym)) {
  808. pr_warning("relocation: symbol %"PRIx64" not found\n",
  809. GELF_R_SYM(rel.r_info));
  810. return -LIBBPF_ERRNO__FORMAT;
  811. }
  812. pr_debug("relo for %lld value %lld name %d\n",
  813. (long long) (rel.r_info >> 32),
  814. (long long) sym.st_value, sym.st_name);
  815. if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx) {
  816. pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
  817. prog->section_name, sym.st_shndx);
  818. return -LIBBPF_ERRNO__RELOC;
  819. }
  820. insn_idx = rel.r_offset / sizeof(struct bpf_insn);
  821. pr_debug("relocation: insn_idx=%u\n", insn_idx);
  822. if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
  823. if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
  824. pr_warning("incorrect bpf_call opcode\n");
  825. return -LIBBPF_ERRNO__RELOC;
  826. }
  827. prog->reloc_desc[i].type = RELO_CALL;
  828. prog->reloc_desc[i].insn_idx = insn_idx;
  829. prog->reloc_desc[i].text_off = sym.st_value;
  830. continue;
  831. }
  832. if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
  833. pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
  834. insn_idx, insns[insn_idx].code);
  835. return -LIBBPF_ERRNO__RELOC;
  836. }
  837. /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
  838. for (map_idx = 0; map_idx < nr_maps; map_idx++) {
  839. if (maps[map_idx].offset == sym.st_value) {
  840. pr_debug("relocation: find map %zd (%s) for insn %u\n",
  841. map_idx, maps[map_idx].name, insn_idx);
  842. break;
  843. }
  844. }
  845. if (map_idx >= nr_maps) {
  846. pr_warning("bpf relocation: map_idx %d large than %d\n",
  847. (int)map_idx, (int)nr_maps - 1);
  848. return -LIBBPF_ERRNO__RELOC;
  849. }
  850. prog->reloc_desc[i].type = RELO_LD64;
  851. prog->reloc_desc[i].insn_idx = insn_idx;
  852. prog->reloc_desc[i].map_idx = map_idx;
  853. }
  854. return 0;
  855. }
  856. static int
  857. bpf_object__create_maps(struct bpf_object *obj)
  858. {
  859. unsigned int i;
  860. for (i = 0; i < obj->nr_maps; i++) {
  861. struct bpf_map_def *def = &obj->maps[i].def;
  862. int *pfd = &obj->maps[i].fd;
  863. *pfd = bpf_create_map_name(def->type,
  864. obj->maps[i].name,
  865. def->key_size,
  866. def->value_size,
  867. def->max_entries,
  868. def->map_flags);
  869. if (*pfd < 0) {
  870. size_t j;
  871. int err = *pfd;
  872. pr_warning("failed to create map (name: '%s'): %s\n",
  873. obj->maps[i].name,
  874. strerror(errno));
  875. for (j = 0; j < i; j++)
  876. zclose(obj->maps[j].fd);
  877. return err;
  878. }
  879. pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
  880. }
  881. return 0;
  882. }
  883. static int
  884. bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
  885. struct reloc_desc *relo)
  886. {
  887. struct bpf_insn *insn, *new_insn;
  888. struct bpf_program *text;
  889. size_t new_cnt;
  890. if (relo->type != RELO_CALL)
  891. return -LIBBPF_ERRNO__RELOC;
  892. if (prog->idx == obj->efile.text_shndx) {
  893. pr_warning("relo in .text insn %d into off %d\n",
  894. relo->insn_idx, relo->text_off);
  895. return -LIBBPF_ERRNO__RELOC;
  896. }
  897. if (prog->main_prog_cnt == 0) {
  898. text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
  899. if (!text) {
  900. pr_warning("no .text section found yet relo into text exist\n");
  901. return -LIBBPF_ERRNO__RELOC;
  902. }
  903. new_cnt = prog->insns_cnt + text->insns_cnt;
  904. new_insn = realloc(prog->insns, new_cnt * sizeof(*insn));
  905. if (!new_insn) {
  906. pr_warning("oom in prog realloc\n");
  907. return -ENOMEM;
  908. }
  909. memcpy(new_insn + prog->insns_cnt, text->insns,
  910. text->insns_cnt * sizeof(*insn));
  911. prog->insns = new_insn;
  912. prog->main_prog_cnt = prog->insns_cnt;
  913. prog->insns_cnt = new_cnt;
  914. pr_debug("added %zd insn from %s to prog %s\n",
  915. text->insns_cnt, text->section_name,
  916. prog->section_name);
  917. }
  918. insn = &prog->insns[relo->insn_idx];
  919. insn->imm += prog->main_prog_cnt - relo->insn_idx;
  920. return 0;
  921. }
  922. static int
  923. bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
  924. {
  925. int i, err;
  926. if (!prog || !prog->reloc_desc)
  927. return 0;
  928. for (i = 0; i < prog->nr_reloc; i++) {
  929. if (prog->reloc_desc[i].type == RELO_LD64) {
  930. struct bpf_insn *insns = prog->insns;
  931. int insn_idx, map_idx;
  932. insn_idx = prog->reloc_desc[i].insn_idx;
  933. map_idx = prog->reloc_desc[i].map_idx;
  934. if (insn_idx >= (int)prog->insns_cnt) {
  935. pr_warning("relocation out of range: '%s'\n",
  936. prog->section_name);
  937. return -LIBBPF_ERRNO__RELOC;
  938. }
  939. insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
  940. insns[insn_idx].imm = obj->maps[map_idx].fd;
  941. } else {
  942. err = bpf_program__reloc_text(prog, obj,
  943. &prog->reloc_desc[i]);
  944. if (err)
  945. return err;
  946. }
  947. }
  948. zfree(&prog->reloc_desc);
  949. prog->nr_reloc = 0;
  950. return 0;
  951. }
  952. static int
  953. bpf_object__relocate(struct bpf_object *obj)
  954. {
  955. struct bpf_program *prog;
  956. size_t i;
  957. int err;
  958. for (i = 0; i < obj->nr_programs; i++) {
  959. prog = &obj->programs[i];
  960. err = bpf_program__relocate(prog, obj);
  961. if (err) {
  962. pr_warning("failed to relocate '%s'\n",
  963. prog->section_name);
  964. return err;
  965. }
  966. }
  967. return 0;
  968. }
  969. static int bpf_object__collect_reloc(struct bpf_object *obj)
  970. {
  971. int i, err;
  972. if (!obj_elf_valid(obj)) {
  973. pr_warning("Internal error: elf object is closed\n");
  974. return -LIBBPF_ERRNO__INTERNAL;
  975. }
  976. for (i = 0; i < obj->efile.nr_reloc; i++) {
  977. GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
  978. Elf_Data *data = obj->efile.reloc[i].data;
  979. int idx = shdr->sh_info;
  980. struct bpf_program *prog;
  981. if (shdr->sh_type != SHT_REL) {
  982. pr_warning("internal error at %d\n", __LINE__);
  983. return -LIBBPF_ERRNO__INTERNAL;
  984. }
  985. prog = bpf_object__find_prog_by_idx(obj, idx);
  986. if (!prog) {
  987. pr_warning("relocation failed: no section(%d)\n", idx);
  988. return -LIBBPF_ERRNO__RELOC;
  989. }
  990. err = bpf_program__collect_reloc(prog,
  991. shdr, data,
  992. obj);
  993. if (err)
  994. return err;
  995. }
  996. return 0;
  997. }
  998. static int
  999. load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
  1000. const char *name, struct bpf_insn *insns, int insns_cnt,
  1001. char *license, u32 kern_version, int *pfd)
  1002. {
  1003. struct bpf_load_program_attr load_attr;
  1004. char *log_buf;
  1005. int ret;
  1006. memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
  1007. load_attr.prog_type = type;
  1008. load_attr.expected_attach_type = expected_attach_type;
  1009. load_attr.name = name;
  1010. load_attr.insns = insns;
  1011. load_attr.insns_cnt = insns_cnt;
  1012. load_attr.license = license;
  1013. load_attr.kern_version = kern_version;
  1014. if (!load_attr.insns || !load_attr.insns_cnt)
  1015. return -EINVAL;
  1016. log_buf = malloc(BPF_LOG_BUF_SIZE);
  1017. if (!log_buf)
  1018. pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
  1019. ret = bpf_load_program_xattr(&load_attr, log_buf, BPF_LOG_BUF_SIZE);
  1020. if (ret >= 0) {
  1021. *pfd = ret;
  1022. ret = 0;
  1023. goto out;
  1024. }
  1025. ret = -LIBBPF_ERRNO__LOAD;
  1026. pr_warning("load bpf program failed: %s\n", strerror(errno));
  1027. if (log_buf && log_buf[0] != '\0') {
  1028. ret = -LIBBPF_ERRNO__VERIFY;
  1029. pr_warning("-- BEGIN DUMP LOG ---\n");
  1030. pr_warning("\n%s\n", log_buf);
  1031. pr_warning("-- END LOG --\n");
  1032. } else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
  1033. pr_warning("Program too large (%zu insns), at most %d insns\n",
  1034. load_attr.insns_cnt, BPF_MAXINSNS);
  1035. ret = -LIBBPF_ERRNO__PROG2BIG;
  1036. } else {
  1037. /* Wrong program type? */
  1038. if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
  1039. int fd;
  1040. load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
  1041. load_attr.expected_attach_type = 0;
  1042. fd = bpf_load_program_xattr(&load_attr, NULL, 0);
  1043. if (fd >= 0) {
  1044. close(fd);
  1045. ret = -LIBBPF_ERRNO__PROGTYPE;
  1046. goto out;
  1047. }
  1048. }
  1049. if (log_buf)
  1050. ret = -LIBBPF_ERRNO__KVER;
  1051. }
  1052. out:
  1053. free(log_buf);
  1054. return ret;
  1055. }
  1056. static int
  1057. bpf_program__load(struct bpf_program *prog,
  1058. char *license, u32 kern_version)
  1059. {
  1060. int err = 0, fd, i;
  1061. if (prog->instances.nr < 0 || !prog->instances.fds) {
  1062. if (prog->preprocessor) {
  1063. pr_warning("Internal error: can't load program '%s'\n",
  1064. prog->section_name);
  1065. return -LIBBPF_ERRNO__INTERNAL;
  1066. }
  1067. prog->instances.fds = malloc(sizeof(int));
  1068. if (!prog->instances.fds) {
  1069. pr_warning("Not enough memory for BPF fds\n");
  1070. return -ENOMEM;
  1071. }
  1072. prog->instances.nr = 1;
  1073. prog->instances.fds[0] = -1;
  1074. }
  1075. if (!prog->preprocessor) {
  1076. if (prog->instances.nr != 1) {
  1077. pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
  1078. prog->section_name, prog->instances.nr);
  1079. }
  1080. err = load_program(prog->type, prog->expected_attach_type,
  1081. prog->name, prog->insns, prog->insns_cnt,
  1082. license, kern_version, &fd);
  1083. if (!err)
  1084. prog->instances.fds[0] = fd;
  1085. goto out;
  1086. }
  1087. for (i = 0; i < prog->instances.nr; i++) {
  1088. struct bpf_prog_prep_result result;
  1089. bpf_program_prep_t preprocessor = prog->preprocessor;
  1090. bzero(&result, sizeof(result));
  1091. err = preprocessor(prog, i, prog->insns,
  1092. prog->insns_cnt, &result);
  1093. if (err) {
  1094. pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
  1095. i, prog->section_name);
  1096. goto out;
  1097. }
  1098. if (!result.new_insn_ptr || !result.new_insn_cnt) {
  1099. pr_debug("Skip loading the %dth instance of program '%s'\n",
  1100. i, prog->section_name);
  1101. prog->instances.fds[i] = -1;
  1102. if (result.pfd)
  1103. *result.pfd = -1;
  1104. continue;
  1105. }
  1106. err = load_program(prog->type, prog->expected_attach_type,
  1107. prog->name, result.new_insn_ptr,
  1108. result.new_insn_cnt,
  1109. license, kern_version, &fd);
  1110. if (err) {
  1111. pr_warning("Loading the %dth instance of program '%s' failed\n",
  1112. i, prog->section_name);
  1113. goto out;
  1114. }
  1115. if (result.pfd)
  1116. *result.pfd = fd;
  1117. prog->instances.fds[i] = fd;
  1118. }
  1119. out:
  1120. if (err)
  1121. pr_warning("failed to load program '%s'\n",
  1122. prog->section_name);
  1123. zfree(&prog->insns);
  1124. prog->insns_cnt = 0;
  1125. return err;
  1126. }
  1127. static int
  1128. bpf_object__load_progs(struct bpf_object *obj)
  1129. {
  1130. size_t i;
  1131. int err;
  1132. for (i = 0; i < obj->nr_programs; i++) {
  1133. if (obj->programs[i].idx == obj->efile.text_shndx)
  1134. continue;
  1135. err = bpf_program__load(&obj->programs[i],
  1136. obj->license,
  1137. obj->kern_version);
  1138. if (err)
  1139. return err;
  1140. }
  1141. return 0;
  1142. }
  1143. static int bpf_object__validate(struct bpf_object *obj)
  1144. {
  1145. if (obj->kern_version == 0) {
  1146. pr_warning("%s doesn't provide kernel version\n",
  1147. obj->path);
  1148. return -LIBBPF_ERRNO__KVERSION;
  1149. }
  1150. return 0;
  1151. }
  1152. static struct bpf_object *
  1153. __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
  1154. {
  1155. struct bpf_object *obj;
  1156. int err;
  1157. if (elf_version(EV_CURRENT) == EV_NONE) {
  1158. pr_warning("failed to init libelf for %s\n", path);
  1159. return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
  1160. }
  1161. obj = bpf_object__new(path, obj_buf, obj_buf_sz);
  1162. if (IS_ERR(obj))
  1163. return obj;
  1164. CHECK_ERR(bpf_object__elf_init(obj), err, out);
  1165. CHECK_ERR(bpf_object__check_endianness(obj), err, out);
  1166. CHECK_ERR(bpf_object__elf_collect(obj), err, out);
  1167. CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
  1168. CHECK_ERR(bpf_object__validate(obj), err, out);
  1169. bpf_object__elf_finish(obj);
  1170. return obj;
  1171. out:
  1172. bpf_object__close(obj);
  1173. return ERR_PTR(err);
  1174. }
  1175. struct bpf_object *bpf_object__open(const char *path)
  1176. {
  1177. /* param validation */
  1178. if (!path)
  1179. return NULL;
  1180. pr_debug("loading %s\n", path);
  1181. return __bpf_object__open(path, NULL, 0);
  1182. }
  1183. struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  1184. size_t obj_buf_sz,
  1185. const char *name)
  1186. {
  1187. char tmp_name[64];
  1188. /* param validation */
  1189. if (!obj_buf || obj_buf_sz <= 0)
  1190. return NULL;
  1191. if (!name) {
  1192. snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
  1193. (unsigned long)obj_buf,
  1194. (unsigned long)obj_buf_sz);
  1195. tmp_name[sizeof(tmp_name) - 1] = '\0';
  1196. name = tmp_name;
  1197. }
  1198. pr_debug("loading object '%s' from buffer\n",
  1199. name);
  1200. return __bpf_object__open(name, obj_buf, obj_buf_sz);
  1201. }
  1202. int bpf_object__unload(struct bpf_object *obj)
  1203. {
  1204. size_t i;
  1205. if (!obj)
  1206. return -EINVAL;
  1207. for (i = 0; i < obj->nr_maps; i++)
  1208. zclose(obj->maps[i].fd);
  1209. for (i = 0; i < obj->nr_programs; i++)
  1210. bpf_program__unload(&obj->programs[i]);
  1211. return 0;
  1212. }
  1213. int bpf_object__load(struct bpf_object *obj)
  1214. {
  1215. int err;
  1216. if (!obj)
  1217. return -EINVAL;
  1218. if (obj->loaded) {
  1219. pr_warning("object should not be loaded twice\n");
  1220. return -EINVAL;
  1221. }
  1222. obj->loaded = true;
  1223. CHECK_ERR(bpf_object__create_maps(obj), err, out);
  1224. CHECK_ERR(bpf_object__relocate(obj), err, out);
  1225. CHECK_ERR(bpf_object__load_progs(obj), err, out);
  1226. return 0;
  1227. out:
  1228. bpf_object__unload(obj);
  1229. pr_warning("failed to load object '%s'\n", obj->path);
  1230. return err;
  1231. }
  1232. static int check_path(const char *path)
  1233. {
  1234. struct statfs st_fs;
  1235. char *dname, *dir;
  1236. int err = 0;
  1237. if (path == NULL)
  1238. return -EINVAL;
  1239. dname = strdup(path);
  1240. if (dname == NULL)
  1241. return -ENOMEM;
  1242. dir = dirname(dname);
  1243. if (statfs(dir, &st_fs)) {
  1244. pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
  1245. err = -errno;
  1246. }
  1247. free(dname);
  1248. if (!err && st_fs.f_type != BPF_FS_MAGIC) {
  1249. pr_warning("specified path %s is not on BPF FS\n", path);
  1250. err = -EINVAL;
  1251. }
  1252. return err;
  1253. }
  1254. int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
  1255. int instance)
  1256. {
  1257. int err;
  1258. err = check_path(path);
  1259. if (err)
  1260. return err;
  1261. if (prog == NULL) {
  1262. pr_warning("invalid program pointer\n");
  1263. return -EINVAL;
  1264. }
  1265. if (instance < 0 || instance >= prog->instances.nr) {
  1266. pr_warning("invalid prog instance %d of prog %s (max %d)\n",
  1267. instance, prog->section_name, prog->instances.nr);
  1268. return -EINVAL;
  1269. }
  1270. if (bpf_obj_pin(prog->instances.fds[instance], path)) {
  1271. pr_warning("failed to pin program: %s\n", strerror(errno));
  1272. return -errno;
  1273. }
  1274. pr_debug("pinned program '%s'\n", path);
  1275. return 0;
  1276. }
  1277. static int make_dir(const char *path)
  1278. {
  1279. int err = 0;
  1280. if (mkdir(path, 0700) && errno != EEXIST)
  1281. err = -errno;
  1282. if (err)
  1283. pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
  1284. return err;
  1285. }
  1286. int bpf_program__pin(struct bpf_program *prog, const char *path)
  1287. {
  1288. int i, err;
  1289. err = check_path(path);
  1290. if (err)
  1291. return err;
  1292. if (prog == NULL) {
  1293. pr_warning("invalid program pointer\n");
  1294. return -EINVAL;
  1295. }
  1296. if (prog->instances.nr <= 0) {
  1297. pr_warning("no instances of prog %s to pin\n",
  1298. prog->section_name);
  1299. return -EINVAL;
  1300. }
  1301. err = make_dir(path);
  1302. if (err)
  1303. return err;
  1304. for (i = 0; i < prog->instances.nr; i++) {
  1305. char buf[PATH_MAX];
  1306. int len;
  1307. len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
  1308. if (len < 0)
  1309. return -EINVAL;
  1310. else if (len >= PATH_MAX)
  1311. return -ENAMETOOLONG;
  1312. err = bpf_program__pin_instance(prog, buf, i);
  1313. if (err)
  1314. return err;
  1315. }
  1316. return 0;
  1317. }
  1318. int bpf_map__pin(struct bpf_map *map, const char *path)
  1319. {
  1320. int err;
  1321. err = check_path(path);
  1322. if (err)
  1323. return err;
  1324. if (map == NULL) {
  1325. pr_warning("invalid map pointer\n");
  1326. return -EINVAL;
  1327. }
  1328. if (bpf_obj_pin(map->fd, path)) {
  1329. pr_warning("failed to pin map: %s\n", strerror(errno));
  1330. return -errno;
  1331. }
  1332. pr_debug("pinned map '%s'\n", path);
  1333. return 0;
  1334. }
  1335. int bpf_object__pin(struct bpf_object *obj, const char *path)
  1336. {
  1337. struct bpf_program *prog;
  1338. struct bpf_map *map;
  1339. int err;
  1340. if (!obj)
  1341. return -ENOENT;
  1342. if (!obj->loaded) {
  1343. pr_warning("object not yet loaded; load it first\n");
  1344. return -ENOENT;
  1345. }
  1346. err = make_dir(path);
  1347. if (err)
  1348. return err;
  1349. bpf_map__for_each(map, obj) {
  1350. char buf[PATH_MAX];
  1351. int len;
  1352. len = snprintf(buf, PATH_MAX, "%s/%s", path,
  1353. bpf_map__name(map));
  1354. if (len < 0)
  1355. return -EINVAL;
  1356. else if (len >= PATH_MAX)
  1357. return -ENAMETOOLONG;
  1358. err = bpf_map__pin(map, buf);
  1359. if (err)
  1360. return err;
  1361. }
  1362. bpf_object__for_each_program(prog, obj) {
  1363. char buf[PATH_MAX];
  1364. int len;
  1365. len = snprintf(buf, PATH_MAX, "%s/%s", path,
  1366. prog->section_name);
  1367. if (len < 0)
  1368. return -EINVAL;
  1369. else if (len >= PATH_MAX)
  1370. return -ENAMETOOLONG;
  1371. err = bpf_program__pin(prog, buf);
  1372. if (err)
  1373. return err;
  1374. }
  1375. return 0;
  1376. }
  1377. void bpf_object__close(struct bpf_object *obj)
  1378. {
  1379. size_t i;
  1380. if (!obj)
  1381. return;
  1382. if (obj->clear_priv)
  1383. obj->clear_priv(obj, obj->priv);
  1384. bpf_object__elf_finish(obj);
  1385. bpf_object__unload(obj);
  1386. for (i = 0; i < obj->nr_maps; i++) {
  1387. zfree(&obj->maps[i].name);
  1388. if (obj->maps[i].clear_priv)
  1389. obj->maps[i].clear_priv(&obj->maps[i],
  1390. obj->maps[i].priv);
  1391. obj->maps[i].priv = NULL;
  1392. obj->maps[i].clear_priv = NULL;
  1393. }
  1394. zfree(&obj->maps);
  1395. obj->nr_maps = 0;
  1396. if (obj->programs && obj->nr_programs) {
  1397. for (i = 0; i < obj->nr_programs; i++)
  1398. bpf_program__exit(&obj->programs[i]);
  1399. }
  1400. zfree(&obj->programs);
  1401. list_del(&obj->list);
  1402. free(obj);
  1403. }
  1404. struct bpf_object *
  1405. bpf_object__next(struct bpf_object *prev)
  1406. {
  1407. struct bpf_object *next;
  1408. if (!prev)
  1409. next = list_first_entry(&bpf_objects_list,
  1410. struct bpf_object,
  1411. list);
  1412. else
  1413. next = list_next_entry(prev, list);
  1414. /* Empty list is noticed here so don't need checking on entry. */
  1415. if (&next->list == &bpf_objects_list)
  1416. return NULL;
  1417. return next;
  1418. }
  1419. const char *bpf_object__name(struct bpf_object *obj)
  1420. {
  1421. return obj ? obj->path : ERR_PTR(-EINVAL);
  1422. }
  1423. unsigned int bpf_object__kversion(struct bpf_object *obj)
  1424. {
  1425. return obj ? obj->kern_version : 0;
  1426. }
  1427. int bpf_object__set_priv(struct bpf_object *obj, void *priv,
  1428. bpf_object_clear_priv_t clear_priv)
  1429. {
  1430. if (obj->priv && obj->clear_priv)
  1431. obj->clear_priv(obj, obj->priv);
  1432. obj->priv = priv;
  1433. obj->clear_priv = clear_priv;
  1434. return 0;
  1435. }
  1436. void *bpf_object__priv(struct bpf_object *obj)
  1437. {
  1438. return obj ? obj->priv : ERR_PTR(-EINVAL);
  1439. }
  1440. struct bpf_program *
  1441. bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
  1442. {
  1443. size_t idx;
  1444. if (!obj->programs)
  1445. return NULL;
  1446. /* First handler */
  1447. if (prev == NULL)
  1448. return &obj->programs[0];
  1449. if (prev->obj != obj) {
  1450. pr_warning("error: program handler doesn't match object\n");
  1451. return NULL;
  1452. }
  1453. idx = (prev - obj->programs) + 1;
  1454. if (idx >= obj->nr_programs)
  1455. return NULL;
  1456. return &obj->programs[idx];
  1457. }
  1458. int bpf_program__set_priv(struct bpf_program *prog, void *priv,
  1459. bpf_program_clear_priv_t clear_priv)
  1460. {
  1461. if (prog->priv && prog->clear_priv)
  1462. prog->clear_priv(prog, prog->priv);
  1463. prog->priv = priv;
  1464. prog->clear_priv = clear_priv;
  1465. return 0;
  1466. }
  1467. void *bpf_program__priv(struct bpf_program *prog)
  1468. {
  1469. return prog ? prog->priv : ERR_PTR(-EINVAL);
  1470. }
  1471. const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
  1472. {
  1473. const char *title;
  1474. title = prog->section_name;
  1475. if (needs_copy) {
  1476. title = strdup(title);
  1477. if (!title) {
  1478. pr_warning("failed to strdup program title\n");
  1479. return ERR_PTR(-ENOMEM);
  1480. }
  1481. }
  1482. return title;
  1483. }
  1484. int bpf_program__fd(struct bpf_program *prog)
  1485. {
  1486. return bpf_program__nth_fd(prog, 0);
  1487. }
  1488. int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
  1489. bpf_program_prep_t prep)
  1490. {
  1491. int *instances_fds;
  1492. if (nr_instances <= 0 || !prep)
  1493. return -EINVAL;
  1494. if (prog->instances.nr > 0 || prog->instances.fds) {
  1495. pr_warning("Can't set pre-processor after loading\n");
  1496. return -EINVAL;
  1497. }
  1498. instances_fds = malloc(sizeof(int) * nr_instances);
  1499. if (!instances_fds) {
  1500. pr_warning("alloc memory failed for fds\n");
  1501. return -ENOMEM;
  1502. }
  1503. /* fill all fd with -1 */
  1504. memset(instances_fds, -1, sizeof(int) * nr_instances);
  1505. prog->instances.nr = nr_instances;
  1506. prog->instances.fds = instances_fds;
  1507. prog->preprocessor = prep;
  1508. return 0;
  1509. }
  1510. int bpf_program__nth_fd(struct bpf_program *prog, int n)
  1511. {
  1512. int fd;
  1513. if (n >= prog->instances.nr || n < 0) {
  1514. pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
  1515. n, prog->section_name, prog->instances.nr);
  1516. return -EINVAL;
  1517. }
  1518. fd = prog->instances.fds[n];
  1519. if (fd < 0) {
  1520. pr_warning("%dth instance of program '%s' is invalid\n",
  1521. n, prog->section_name);
  1522. return -ENOENT;
  1523. }
  1524. return fd;
  1525. }
  1526. void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
  1527. {
  1528. prog->type = type;
  1529. }
  1530. static bool bpf_program__is_type(struct bpf_program *prog,
  1531. enum bpf_prog_type type)
  1532. {
  1533. return prog ? (prog->type == type) : false;
  1534. }
  1535. #define BPF_PROG_TYPE_FNS(NAME, TYPE) \
  1536. int bpf_program__set_##NAME(struct bpf_program *prog) \
  1537. { \
  1538. if (!prog) \
  1539. return -EINVAL; \
  1540. bpf_program__set_type(prog, TYPE); \
  1541. return 0; \
  1542. } \
  1543. \
  1544. bool bpf_program__is_##NAME(struct bpf_program *prog) \
  1545. { \
  1546. return bpf_program__is_type(prog, TYPE); \
  1547. } \
  1548. BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
  1549. BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
  1550. BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
  1551. BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
  1552. BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
  1553. BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
  1554. BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
  1555. BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
  1556. static void bpf_program__set_expected_attach_type(struct bpf_program *prog,
  1557. enum bpf_attach_type type)
  1558. {
  1559. prog->expected_attach_type = type;
  1560. }
  1561. #define BPF_PROG_SEC_FULL(string, ptype, atype) \
  1562. { string, sizeof(string) - 1, ptype, atype }
  1563. #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_FULL(string, ptype, 0)
  1564. #define BPF_S_PROG_SEC(string, ptype) \
  1565. BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK, ptype)
  1566. #define BPF_SA_PROG_SEC(string, ptype) \
  1567. BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, ptype)
  1568. static const struct {
  1569. const char *sec;
  1570. size_t len;
  1571. enum bpf_prog_type prog_type;
  1572. enum bpf_attach_type expected_attach_type;
  1573. } section_names[] = {
  1574. BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
  1575. BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE),
  1576. BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE),
  1577. BPF_PROG_SEC("classifier", BPF_PROG_TYPE_SCHED_CLS),
  1578. BPF_PROG_SEC("action", BPF_PROG_TYPE_SCHED_ACT),
  1579. BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT),
  1580. BPF_PROG_SEC("raw_tracepoint/", BPF_PROG_TYPE_RAW_TRACEPOINT),
  1581. BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP),
  1582. BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT),
  1583. BPF_PROG_SEC("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB),
  1584. BPF_PROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK),
  1585. BPF_PROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE),
  1586. BPF_PROG_SEC("lwt_in", BPF_PROG_TYPE_LWT_IN),
  1587. BPF_PROG_SEC("lwt_out", BPF_PROG_TYPE_LWT_OUT),
  1588. BPF_PROG_SEC("lwt_xmit", BPF_PROG_TYPE_LWT_XMIT),
  1589. BPF_PROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS),
  1590. BPF_PROG_SEC("sk_skb", BPF_PROG_TYPE_SK_SKB),
  1591. BPF_PROG_SEC("sk_msg", BPF_PROG_TYPE_SK_MSG),
  1592. BPF_SA_PROG_SEC("cgroup/bind4", BPF_CGROUP_INET4_BIND),
  1593. BPF_SA_PROG_SEC("cgroup/bind6", BPF_CGROUP_INET6_BIND),
  1594. BPF_SA_PROG_SEC("cgroup/connect4", BPF_CGROUP_INET4_CONNECT),
  1595. BPF_SA_PROG_SEC("cgroup/connect6", BPF_CGROUP_INET6_CONNECT),
  1596. BPF_S_PROG_SEC("cgroup/post_bind4", BPF_CGROUP_INET4_POST_BIND),
  1597. BPF_S_PROG_SEC("cgroup/post_bind6", BPF_CGROUP_INET6_POST_BIND),
  1598. };
  1599. #undef BPF_PROG_SEC
  1600. #undef BPF_PROG_SEC_FULL
  1601. #undef BPF_S_PROG_SEC
  1602. #undef BPF_SA_PROG_SEC
  1603. static int bpf_program__identify_section(struct bpf_program *prog)
  1604. {
  1605. int i;
  1606. if (!prog->section_name)
  1607. goto err;
  1608. for (i = 0; i < ARRAY_SIZE(section_names); i++)
  1609. if (strncmp(prog->section_name, section_names[i].sec,
  1610. section_names[i].len) == 0)
  1611. return i;
  1612. err:
  1613. pr_warning("failed to guess program type based on section name %s\n",
  1614. prog->section_name);
  1615. return -1;
  1616. }
  1617. int bpf_map__fd(struct bpf_map *map)
  1618. {
  1619. return map ? map->fd : -EINVAL;
  1620. }
  1621. const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
  1622. {
  1623. return map ? &map->def : ERR_PTR(-EINVAL);
  1624. }
  1625. const char *bpf_map__name(struct bpf_map *map)
  1626. {
  1627. return map ? map->name : NULL;
  1628. }
  1629. int bpf_map__set_priv(struct bpf_map *map, void *priv,
  1630. bpf_map_clear_priv_t clear_priv)
  1631. {
  1632. if (!map)
  1633. return -EINVAL;
  1634. if (map->priv) {
  1635. if (map->clear_priv)
  1636. map->clear_priv(map, map->priv);
  1637. }
  1638. map->priv = priv;
  1639. map->clear_priv = clear_priv;
  1640. return 0;
  1641. }
  1642. void *bpf_map__priv(struct bpf_map *map)
  1643. {
  1644. return map ? map->priv : ERR_PTR(-EINVAL);
  1645. }
  1646. struct bpf_map *
  1647. bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
  1648. {
  1649. size_t idx;
  1650. struct bpf_map *s, *e;
  1651. if (!obj || !obj->maps)
  1652. return NULL;
  1653. s = obj->maps;
  1654. e = obj->maps + obj->nr_maps;
  1655. if (prev == NULL)
  1656. return s;
  1657. if ((prev < s) || (prev >= e)) {
  1658. pr_warning("error in %s: map handler doesn't belong to object\n",
  1659. __func__);
  1660. return NULL;
  1661. }
  1662. idx = (prev - obj->maps) + 1;
  1663. if (idx >= obj->nr_maps)
  1664. return NULL;
  1665. return &obj->maps[idx];
  1666. }
  1667. struct bpf_map *
  1668. bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
  1669. {
  1670. struct bpf_map *pos;
  1671. bpf_map__for_each(pos, obj) {
  1672. if (pos->name && !strcmp(pos->name, name))
  1673. return pos;
  1674. }
  1675. return NULL;
  1676. }
  1677. struct bpf_map *
  1678. bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
  1679. {
  1680. int i;
  1681. for (i = 0; i < obj->nr_maps; i++) {
  1682. if (obj->maps[i].offset == offset)
  1683. return &obj->maps[i];
  1684. }
  1685. return ERR_PTR(-ENOENT);
  1686. }
  1687. long libbpf_get_error(const void *ptr)
  1688. {
  1689. if (IS_ERR(ptr))
  1690. return PTR_ERR(ptr);
  1691. return 0;
  1692. }
  1693. int bpf_prog_load(const char *file, enum bpf_prog_type type,
  1694. struct bpf_object **pobj, int *prog_fd)
  1695. {
  1696. struct bpf_prog_load_attr attr;
  1697. memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
  1698. attr.file = file;
  1699. attr.prog_type = type;
  1700. attr.expected_attach_type = 0;
  1701. return bpf_prog_load_xattr(&attr, pobj, prog_fd);
  1702. }
  1703. int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
  1704. struct bpf_object **pobj, int *prog_fd)
  1705. {
  1706. struct bpf_program *prog, *first_prog = NULL;
  1707. enum bpf_attach_type expected_attach_type;
  1708. enum bpf_prog_type prog_type;
  1709. struct bpf_object *obj;
  1710. int section_idx;
  1711. int err;
  1712. if (!attr)
  1713. return -EINVAL;
  1714. obj = bpf_object__open(attr->file);
  1715. if (IS_ERR(obj))
  1716. return -ENOENT;
  1717. bpf_object__for_each_program(prog, obj) {
  1718. /*
  1719. * If type is not specified, try to guess it based on
  1720. * section name.
  1721. */
  1722. prog_type = attr->prog_type;
  1723. expected_attach_type = attr->expected_attach_type;
  1724. if (prog_type == BPF_PROG_TYPE_UNSPEC) {
  1725. section_idx = bpf_program__identify_section(prog);
  1726. if (section_idx < 0) {
  1727. bpf_object__close(obj);
  1728. return -EINVAL;
  1729. }
  1730. prog_type = section_names[section_idx].prog_type;
  1731. expected_attach_type =
  1732. section_names[section_idx].expected_attach_type;
  1733. }
  1734. bpf_program__set_type(prog, prog_type);
  1735. bpf_program__set_expected_attach_type(prog,
  1736. expected_attach_type);
  1737. if (prog->idx != obj->efile.text_shndx && !first_prog)
  1738. first_prog = prog;
  1739. }
  1740. if (!first_prog) {
  1741. pr_warning("object file doesn't contain bpf program\n");
  1742. bpf_object__close(obj);
  1743. return -ENOENT;
  1744. }
  1745. err = bpf_object__load(obj);
  1746. if (err) {
  1747. bpf_object__close(obj);
  1748. return -EINVAL;
  1749. }
  1750. *pobj = obj;
  1751. *prog_fd = bpf_program__fd(first_prog);
  1752. return 0;
  1753. }