bpf_load.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <libelf.h>
  7. #include <gelf.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <stdbool.h>
  12. #include <stdlib.h>
  13. #include <linux/bpf.h>
  14. #include <linux/filter.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/netlink.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/types.h>
  19. #include <sys/socket.h>
  20. #include <sys/syscall.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/mman.h>
  23. #include <poll.h>
  24. #include <ctype.h>
  25. #include <assert.h>
  26. #include <bpf/bpf.h>
  27. #include "bpf_load.h"
  28. #include "perf-sys.h"
  29. #define DEBUGFS "/sys/kernel/debug/tracing/"
  30. static char license[128];
  31. static int kern_version;
  32. static bool processed_sec[128];
  33. char bpf_log_buf[BPF_LOG_BUF_SIZE];
  34. int map_fd[MAX_MAPS];
  35. int prog_fd[MAX_PROGS];
  36. int event_fd[MAX_PROGS];
  37. int prog_cnt;
  38. int prog_array_fd = -1;
  39. struct bpf_map_data map_data[MAX_MAPS];
  40. int map_data_count;
  41. static int populate_prog_array(const char *event, int prog_fd)
  42. {
  43. int ind = atoi(event), err;
  44. err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
  45. if (err < 0) {
  46. printf("failed to store prog_fd in prog_array\n");
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. static int write_kprobe_events(const char *val)
  52. {
  53. int fd, ret, flags;
  54. if (val == NULL)
  55. return -1;
  56. else if (val[0] == '\0')
  57. flags = O_WRONLY | O_TRUNC;
  58. else
  59. flags = O_WRONLY | O_APPEND;
  60. fd = open(DEBUGFS "kprobe_events", flags);
  61. ret = write(fd, val, strlen(val));
  62. close(fd);
  63. return ret;
  64. }
  65. static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
  66. {
  67. bool is_socket = strncmp(event, "socket", 6) == 0;
  68. bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
  69. bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
  70. bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
  71. bool is_raw_tracepoint = strncmp(event, "raw_tracepoint/", 15) == 0;
  72. bool is_xdp = strncmp(event, "xdp", 3) == 0;
  73. bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
  74. bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
  75. bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
  76. bool is_sockops = strncmp(event, "sockops", 7) == 0;
  77. bool is_sk_skb = strncmp(event, "sk_skb", 6) == 0;
  78. bool is_sk_msg = strncmp(event, "sk_msg", 6) == 0;
  79. size_t insns_cnt = size / sizeof(struct bpf_insn);
  80. enum bpf_prog_type prog_type;
  81. char buf[256];
  82. int fd, efd, err, id;
  83. struct perf_event_attr attr = {};
  84. attr.type = PERF_TYPE_TRACEPOINT;
  85. attr.sample_type = PERF_SAMPLE_RAW;
  86. attr.sample_period = 1;
  87. attr.wakeup_events = 1;
  88. if (is_socket) {
  89. prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  90. } else if (is_kprobe || is_kretprobe) {
  91. prog_type = BPF_PROG_TYPE_KPROBE;
  92. } else if (is_tracepoint) {
  93. prog_type = BPF_PROG_TYPE_TRACEPOINT;
  94. } else if (is_raw_tracepoint) {
  95. prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT;
  96. } else if (is_xdp) {
  97. prog_type = BPF_PROG_TYPE_XDP;
  98. } else if (is_perf_event) {
  99. prog_type = BPF_PROG_TYPE_PERF_EVENT;
  100. } else if (is_cgroup_skb) {
  101. prog_type = BPF_PROG_TYPE_CGROUP_SKB;
  102. } else if (is_cgroup_sk) {
  103. prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
  104. } else if (is_sockops) {
  105. prog_type = BPF_PROG_TYPE_SOCK_OPS;
  106. } else if (is_sk_skb) {
  107. prog_type = BPF_PROG_TYPE_SK_SKB;
  108. } else if (is_sk_msg) {
  109. prog_type = BPF_PROG_TYPE_SK_MSG;
  110. } else {
  111. printf("Unknown event '%s'\n", event);
  112. return -1;
  113. }
  114. if (prog_cnt == MAX_PROGS)
  115. return -1;
  116. fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
  117. bpf_log_buf, BPF_LOG_BUF_SIZE);
  118. if (fd < 0) {
  119. printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
  120. return -1;
  121. }
  122. prog_fd[prog_cnt++] = fd;
  123. if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
  124. return 0;
  125. if (is_socket || is_sockops || is_sk_skb || is_sk_msg) {
  126. if (is_socket)
  127. event += 6;
  128. else
  129. event += 7;
  130. if (*event != '/')
  131. return 0;
  132. event++;
  133. if (!isdigit(*event)) {
  134. printf("invalid prog number\n");
  135. return -1;
  136. }
  137. return populate_prog_array(event, fd);
  138. }
  139. if (is_raw_tracepoint) {
  140. efd = bpf_raw_tracepoint_open(event + 15, fd);
  141. if (efd < 0) {
  142. printf("tracepoint %s %s\n", event + 15, strerror(errno));
  143. return -1;
  144. }
  145. event_fd[prog_cnt - 1] = efd;
  146. return 0;
  147. }
  148. if (is_kprobe || is_kretprobe) {
  149. bool need_normal_check = true;
  150. const char *event_prefix = "";
  151. if (is_kprobe)
  152. event += 7;
  153. else
  154. event += 10;
  155. if (*event == 0) {
  156. printf("event name cannot be empty\n");
  157. return -1;
  158. }
  159. if (isdigit(*event))
  160. return populate_prog_array(event, fd);
  161. #ifdef __x86_64__
  162. if (strncmp(event, "sys_", 4) == 0) {
  163. snprintf(buf, sizeof(buf), "%c:__x64_%s __x64_%s",
  164. is_kprobe ? 'p' : 'r', event, event);
  165. err = write_kprobe_events(buf);
  166. if (err >= 0) {
  167. need_normal_check = false;
  168. event_prefix = "__x64_";
  169. }
  170. }
  171. #endif
  172. if (need_normal_check) {
  173. snprintf(buf, sizeof(buf), "%c:%s %s",
  174. is_kprobe ? 'p' : 'r', event, event);
  175. err = write_kprobe_events(buf);
  176. if (err < 0) {
  177. printf("failed to create kprobe '%s' error '%s'\n",
  178. event, strerror(errno));
  179. return -1;
  180. }
  181. }
  182. strcpy(buf, DEBUGFS);
  183. strcat(buf, "events/kprobes/");
  184. strcat(buf, event_prefix);
  185. strcat(buf, event);
  186. strcat(buf, "/id");
  187. } else if (is_tracepoint) {
  188. event += 11;
  189. if (*event == 0) {
  190. printf("event name cannot be empty\n");
  191. return -1;
  192. }
  193. strcpy(buf, DEBUGFS);
  194. strcat(buf, "events/");
  195. strcat(buf, event);
  196. strcat(buf, "/id");
  197. }
  198. efd = open(buf, O_RDONLY, 0);
  199. if (efd < 0) {
  200. printf("failed to open event %s\n", event);
  201. return -1;
  202. }
  203. err = read(efd, buf, sizeof(buf));
  204. if (err < 0 || err >= sizeof(buf)) {
  205. printf("read from '%s' failed '%s'\n", event, strerror(errno));
  206. return -1;
  207. }
  208. close(efd);
  209. buf[err] = 0;
  210. id = atoi(buf);
  211. attr.config = id;
  212. efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
  213. if (efd < 0) {
  214. printf("event %d fd %d err %s\n", id, efd, strerror(errno));
  215. return -1;
  216. }
  217. event_fd[prog_cnt - 1] = efd;
  218. err = ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
  219. if (err < 0) {
  220. printf("ioctl PERF_EVENT_IOC_ENABLE failed err %s\n",
  221. strerror(errno));
  222. return -1;
  223. }
  224. err = ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
  225. if (err < 0) {
  226. printf("ioctl PERF_EVENT_IOC_SET_BPF failed err %s\n",
  227. strerror(errno));
  228. return -1;
  229. }
  230. return 0;
  231. }
  232. static int load_maps(struct bpf_map_data *maps, int nr_maps,
  233. fixup_map_cb fixup_map)
  234. {
  235. int i, numa_node;
  236. for (i = 0; i < nr_maps; i++) {
  237. if (fixup_map) {
  238. fixup_map(&maps[i], i);
  239. /* Allow userspace to assign map FD prior to creation */
  240. if (maps[i].fd != -1) {
  241. map_fd[i] = maps[i].fd;
  242. continue;
  243. }
  244. }
  245. numa_node = maps[i].def.map_flags & BPF_F_NUMA_NODE ?
  246. maps[i].def.numa_node : -1;
  247. if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
  248. maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) {
  249. int inner_map_fd = map_fd[maps[i].def.inner_map_idx];
  250. map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type,
  251. maps[i].name,
  252. maps[i].def.key_size,
  253. inner_map_fd,
  254. maps[i].def.max_entries,
  255. maps[i].def.map_flags,
  256. numa_node);
  257. } else {
  258. map_fd[i] = bpf_create_map_node(maps[i].def.type,
  259. maps[i].name,
  260. maps[i].def.key_size,
  261. maps[i].def.value_size,
  262. maps[i].def.max_entries,
  263. maps[i].def.map_flags,
  264. numa_node);
  265. }
  266. if (map_fd[i] < 0) {
  267. printf("failed to create map %d (%s): %d %s\n",
  268. i, maps[i].name, errno, strerror(errno));
  269. return 1;
  270. }
  271. maps[i].fd = map_fd[i];
  272. if (maps[i].def.type == BPF_MAP_TYPE_PROG_ARRAY)
  273. prog_array_fd = map_fd[i];
  274. }
  275. return 0;
  276. }
  277. static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
  278. GElf_Shdr *shdr, Elf_Data **data)
  279. {
  280. Elf_Scn *scn;
  281. scn = elf_getscn(elf, i);
  282. if (!scn)
  283. return 1;
  284. if (gelf_getshdr(scn, shdr) != shdr)
  285. return 2;
  286. *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
  287. if (!*shname || !shdr->sh_size)
  288. return 3;
  289. *data = elf_getdata(scn, 0);
  290. if (!*data || elf_getdata(scn, *data) != NULL)
  291. return 4;
  292. return 0;
  293. }
  294. static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
  295. GElf_Shdr *shdr, struct bpf_insn *insn,
  296. struct bpf_map_data *maps, int nr_maps)
  297. {
  298. int i, nrels;
  299. nrels = shdr->sh_size / shdr->sh_entsize;
  300. for (i = 0; i < nrels; i++) {
  301. GElf_Sym sym;
  302. GElf_Rel rel;
  303. unsigned int insn_idx;
  304. bool match = false;
  305. int j, map_idx;
  306. gelf_getrel(data, i, &rel);
  307. insn_idx = rel.r_offset / sizeof(struct bpf_insn);
  308. gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
  309. if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
  310. printf("invalid relo for insn[%d].code 0x%x\n",
  311. insn_idx, insn[insn_idx].code);
  312. return 1;
  313. }
  314. insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
  315. /* Match FD relocation against recorded map_data[] offset */
  316. for (map_idx = 0; map_idx < nr_maps; map_idx++) {
  317. if (maps[map_idx].elf_offset == sym.st_value) {
  318. match = true;
  319. break;
  320. }
  321. }
  322. if (match) {
  323. insn[insn_idx].imm = maps[map_idx].fd;
  324. } else {
  325. printf("invalid relo for insn[%d] no map_data match\n",
  326. insn_idx);
  327. return 1;
  328. }
  329. }
  330. return 0;
  331. }
  332. static int cmp_symbols(const void *l, const void *r)
  333. {
  334. const GElf_Sym *lsym = (const GElf_Sym *)l;
  335. const GElf_Sym *rsym = (const GElf_Sym *)r;
  336. if (lsym->st_value < rsym->st_value)
  337. return -1;
  338. else if (lsym->st_value > rsym->st_value)
  339. return 1;
  340. else
  341. return 0;
  342. }
  343. static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
  344. Elf *elf, Elf_Data *symbols, int strtabidx)
  345. {
  346. int map_sz_elf, map_sz_copy;
  347. bool validate_zero = false;
  348. Elf_Data *data_maps;
  349. int i, nr_maps;
  350. GElf_Sym *sym;
  351. Elf_Scn *scn;
  352. int copy_sz;
  353. if (maps_shndx < 0)
  354. return -EINVAL;
  355. if (!symbols)
  356. return -EINVAL;
  357. /* Get data for maps section via elf index */
  358. scn = elf_getscn(elf, maps_shndx);
  359. if (scn)
  360. data_maps = elf_getdata(scn, NULL);
  361. if (!scn || !data_maps) {
  362. printf("Failed to get Elf_Data from maps section %d\n",
  363. maps_shndx);
  364. return -EINVAL;
  365. }
  366. /* For each map get corrosponding symbol table entry */
  367. sym = calloc(MAX_MAPS+1, sizeof(GElf_Sym));
  368. for (i = 0, nr_maps = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
  369. assert(nr_maps < MAX_MAPS+1);
  370. if (!gelf_getsym(symbols, i, &sym[nr_maps]))
  371. continue;
  372. if (sym[nr_maps].st_shndx != maps_shndx)
  373. continue;
  374. /* Only increment iif maps section */
  375. nr_maps++;
  376. }
  377. /* Align to map_fd[] order, via sort on offset in sym.st_value */
  378. qsort(sym, nr_maps, sizeof(GElf_Sym), cmp_symbols);
  379. /* Keeping compatible with ELF maps section changes
  380. * ------------------------------------------------
  381. * The program size of struct bpf_load_map_def is known by loader
  382. * code, but struct stored in ELF file can be different.
  383. *
  384. * Unfortunately sym[i].st_size is zero. To calculate the
  385. * struct size stored in the ELF file, assume all struct have
  386. * the same size, and simply divide with number of map
  387. * symbols.
  388. */
  389. map_sz_elf = data_maps->d_size / nr_maps;
  390. map_sz_copy = sizeof(struct bpf_load_map_def);
  391. if (map_sz_elf < map_sz_copy) {
  392. /*
  393. * Backward compat, loading older ELF file with
  394. * smaller struct, keeping remaining bytes zero.
  395. */
  396. map_sz_copy = map_sz_elf;
  397. } else if (map_sz_elf > map_sz_copy) {
  398. /*
  399. * Forward compat, loading newer ELF file with larger
  400. * struct with unknown features. Assume zero means
  401. * feature not used. Thus, validate rest of struct
  402. * data is zero.
  403. */
  404. validate_zero = true;
  405. }
  406. /* Memcpy relevant part of ELF maps data to loader maps */
  407. for (i = 0; i < nr_maps; i++) {
  408. struct bpf_load_map_def *def;
  409. unsigned char *addr, *end;
  410. const char *map_name;
  411. size_t offset;
  412. map_name = elf_strptr(elf, strtabidx, sym[i].st_name);
  413. maps[i].name = strdup(map_name);
  414. if (!maps[i].name) {
  415. printf("strdup(%s): %s(%d)\n", map_name,
  416. strerror(errno), errno);
  417. free(sym);
  418. return -errno;
  419. }
  420. /* Symbol value is offset into ELF maps section data area */
  421. offset = sym[i].st_value;
  422. def = (struct bpf_load_map_def *)(data_maps->d_buf + offset);
  423. maps[i].elf_offset = offset;
  424. memset(&maps[i].def, 0, sizeof(struct bpf_load_map_def));
  425. memcpy(&maps[i].def, def, map_sz_copy);
  426. /* Verify no newer features were requested */
  427. if (validate_zero) {
  428. addr = (unsigned char *) def + map_sz_copy;
  429. end = (unsigned char *) def + map_sz_elf;
  430. for (; addr < end; addr++) {
  431. if (*addr != 0) {
  432. free(sym);
  433. return -EFBIG;
  434. }
  435. }
  436. }
  437. }
  438. free(sym);
  439. return nr_maps;
  440. }
  441. static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
  442. {
  443. int fd, i, ret, maps_shndx = -1, strtabidx = -1;
  444. Elf *elf;
  445. GElf_Ehdr ehdr;
  446. GElf_Shdr shdr, shdr_prog;
  447. Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
  448. char *shname, *shname_prog;
  449. int nr_maps = 0;
  450. /* reset global variables */
  451. kern_version = 0;
  452. memset(license, 0, sizeof(license));
  453. memset(processed_sec, 0, sizeof(processed_sec));
  454. if (elf_version(EV_CURRENT) == EV_NONE)
  455. return 1;
  456. fd = open(path, O_RDONLY, 0);
  457. if (fd < 0)
  458. return 1;
  459. elf = elf_begin(fd, ELF_C_READ, NULL);
  460. if (!elf)
  461. return 1;
  462. if (gelf_getehdr(elf, &ehdr) != &ehdr)
  463. return 1;
  464. /* clear all kprobes */
  465. i = write_kprobe_events("");
  466. /* scan over all elf sections to get license and map info */
  467. for (i = 1; i < ehdr.e_shnum; i++) {
  468. if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
  469. continue;
  470. if (0) /* helpful for llvm debugging */
  471. printf("section %d:%s data %p size %zd link %d flags %d\n",
  472. i, shname, data->d_buf, data->d_size,
  473. shdr.sh_link, (int) shdr.sh_flags);
  474. if (strcmp(shname, "license") == 0) {
  475. processed_sec[i] = true;
  476. memcpy(license, data->d_buf, data->d_size);
  477. } else if (strcmp(shname, "version") == 0) {
  478. processed_sec[i] = true;
  479. if (data->d_size != sizeof(int)) {
  480. printf("invalid size of version section %zd\n",
  481. data->d_size);
  482. return 1;
  483. }
  484. memcpy(&kern_version, data->d_buf, sizeof(int));
  485. } else if (strcmp(shname, "maps") == 0) {
  486. int j;
  487. maps_shndx = i;
  488. data_maps = data;
  489. for (j = 0; j < MAX_MAPS; j++)
  490. map_data[j].fd = -1;
  491. } else if (shdr.sh_type == SHT_SYMTAB) {
  492. strtabidx = shdr.sh_link;
  493. symbols = data;
  494. }
  495. }
  496. ret = 1;
  497. if (!symbols) {
  498. printf("missing SHT_SYMTAB section\n");
  499. goto done;
  500. }
  501. if (data_maps) {
  502. nr_maps = load_elf_maps_section(map_data, maps_shndx,
  503. elf, symbols, strtabidx);
  504. if (nr_maps < 0) {
  505. printf("Error: Failed loading ELF maps (errno:%d):%s\n",
  506. nr_maps, strerror(-nr_maps));
  507. goto done;
  508. }
  509. if (load_maps(map_data, nr_maps, fixup_map))
  510. goto done;
  511. map_data_count = nr_maps;
  512. processed_sec[maps_shndx] = true;
  513. }
  514. /* process all relo sections, and rewrite bpf insns for maps */
  515. for (i = 1; i < ehdr.e_shnum; i++) {
  516. if (processed_sec[i])
  517. continue;
  518. if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
  519. continue;
  520. if (shdr.sh_type == SHT_REL) {
  521. struct bpf_insn *insns;
  522. /* locate prog sec that need map fixup (relocations) */
  523. if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
  524. &shdr_prog, &data_prog))
  525. continue;
  526. if (shdr_prog.sh_type != SHT_PROGBITS ||
  527. !(shdr_prog.sh_flags & SHF_EXECINSTR))
  528. continue;
  529. insns = (struct bpf_insn *) data_prog->d_buf;
  530. processed_sec[i] = true; /* relo section */
  531. if (parse_relo_and_apply(data, symbols, &shdr, insns,
  532. map_data, nr_maps))
  533. continue;
  534. }
  535. }
  536. /* load programs */
  537. for (i = 1; i < ehdr.e_shnum; i++) {
  538. if (processed_sec[i])
  539. continue;
  540. if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
  541. continue;
  542. if (memcmp(shname, "kprobe/", 7) == 0 ||
  543. memcmp(shname, "kretprobe/", 10) == 0 ||
  544. memcmp(shname, "tracepoint/", 11) == 0 ||
  545. memcmp(shname, "raw_tracepoint/", 15) == 0 ||
  546. memcmp(shname, "xdp", 3) == 0 ||
  547. memcmp(shname, "perf_event", 10) == 0 ||
  548. memcmp(shname, "socket", 6) == 0 ||
  549. memcmp(shname, "cgroup/", 7) == 0 ||
  550. memcmp(shname, "sockops", 7) == 0 ||
  551. memcmp(shname, "sk_skb", 6) == 0 ||
  552. memcmp(shname, "sk_msg", 6) == 0) {
  553. ret = load_and_attach(shname, data->d_buf,
  554. data->d_size);
  555. if (ret != 0)
  556. goto done;
  557. }
  558. }
  559. done:
  560. close(fd);
  561. return ret;
  562. }
  563. int load_bpf_file(char *path)
  564. {
  565. return do_load_bpf_file(path, NULL);
  566. }
  567. int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
  568. {
  569. return do_load_bpf_file(path, fixup_map);
  570. }