libbpf.c 57 KB

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