libbpf.c 54 KB

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