libbpf.c 56 KB

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